Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[pandora-kernel.git] / drivers / scsi / fcoe / libfcoe.c
1 /*
2  * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
3  * Copyright (c) 2009 Intel Corporation.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Maintained at www.Open-FCoE.org
19  */
20
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/timer.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/errno.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.h>
35 #include <net/rtnetlink.h>
36
37 #include <scsi/fc/fc_els.h>
38 #include <scsi/fc/fc_fs.h>
39 #include <scsi/fc/fc_fip.h>
40 #include <scsi/fc/fc_encaps.h>
41 #include <scsi/fc/fc_fcoe.h>
42
43 #include <scsi/libfc.h>
44 #include <scsi/libfcoe.h>
45
46 MODULE_AUTHOR("Open-FCoE.org");
47 MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
48 MODULE_LICENSE("GPL v2");
49
50 #define FCOE_CTLR_MIN_FKA       500             /* min keep alive (mS) */
51 #define FCOE_CTLR_DEF_FKA       FIP_DEF_FKA     /* default keep alive (mS) */
52
53 static void fcoe_ctlr_timeout(unsigned long);
54 static void fcoe_ctlr_timer_work(struct work_struct *);
55 static void fcoe_ctlr_recv_work(struct work_struct *);
56
57 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
58
59 unsigned int libfcoe_debug_logging;
60 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
61 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
62
63 #define LIBFCOE_LOGGING     0x01 /* General logging, not categorized */
64 #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
65
66 #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD)               \
67 do {                                                    \
68         if (unlikely(libfcoe_debug_logging & LEVEL))    \
69                 do {                                    \
70                         CMD;                            \
71                 } while (0);                            \
72 } while (0)
73
74 #define LIBFCOE_DBG(fmt, args...)                                       \
75         LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING,                          \
76                               printk(KERN_INFO "libfcoe: " fmt, ##args);)
77
78 #define LIBFCOE_FIP_DBG(fip, fmt, args...)                              \
79         LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING,                      \
80                               printk(KERN_INFO "host%d: fip: " fmt,     \
81                                      (fip)->lp->host->host_no, ##args);)
82
83 /**
84  * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
85  * @fcf: The FCF to check
86  *
87  * Return non-zero if FCF fcoe_size has been validated.
88  */
89 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
90 {
91         return (fcf->flags & FIP_FL_SOL) != 0;
92 }
93
94 /**
95  * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
96  * @fcf: The FCF to check
97  *
98  * Return non-zero if the FCF is usable.
99  */
100 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
101 {
102         u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
103
104         return (fcf->flags & flags) == flags;
105 }
106
107 /**
108  * fcoe_ctlr_init() - Initialize the FCoE Controller instance
109  * @fip: The FCoE controller to initialize
110  */
111 void fcoe_ctlr_init(struct fcoe_ctlr *fip)
112 {
113         fip->state = FIP_ST_LINK_WAIT;
114         fip->mode = FIP_ST_AUTO;
115         INIT_LIST_HEAD(&fip->fcfs);
116         spin_lock_init(&fip->lock);
117         fip->flogi_oxid = FC_XID_UNKNOWN;
118         setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
119         INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
120         INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
121         skb_queue_head_init(&fip->fip_recv_list);
122 }
123 EXPORT_SYMBOL(fcoe_ctlr_init);
124
125 /**
126  * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
127  * @fip: The FCoE controller whose FCFs are to be reset
128  *
129  * Called with &fcoe_ctlr lock held.
130  */
131 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
132 {
133         struct fcoe_fcf *fcf;
134         struct fcoe_fcf *next;
135
136         fip->sel_fcf = NULL;
137         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
138                 list_del(&fcf->list);
139                 kfree(fcf);
140         }
141         fip->fcf_count = 0;
142         fip->sel_time = 0;
143 }
144
145 /**
146  * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
147  * @fip: The FCoE controller to tear down
148  *
149  * This is called by FCoE drivers before freeing the &fcoe_ctlr.
150  *
151  * The receive handler will have been deleted before this to guarantee
152  * that no more recv_work will be scheduled.
153  *
154  * The timer routine will simply return once we set FIP_ST_DISABLED.
155  * This guarantees that no further timeouts or work will be scheduled.
156  */
157 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
158 {
159         cancel_work_sync(&fip->recv_work);
160         skb_queue_purge(&fip->fip_recv_list);
161
162         spin_lock_bh(&fip->lock);
163         fip->state = FIP_ST_DISABLED;
164         fcoe_ctlr_reset_fcfs(fip);
165         spin_unlock_bh(&fip->lock);
166         del_timer_sync(&fip->timer);
167         cancel_work_sync(&fip->timer_work);
168 }
169 EXPORT_SYMBOL(fcoe_ctlr_destroy);
170
171 /**
172  * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
173  * @fip: The FCoE controller to get the maximum FCoE size from
174  *
175  * Returns the maximum packet size including the FCoE header and trailer,
176  * but not including any Ethernet or VLAN headers.
177  */
178 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
179 {
180         /*
181          * Determine the max FCoE frame size allowed, including
182          * FCoE header and trailer.
183          * Note:  lp->mfs is currently the payload size, not the frame size.
184          */
185         return fip->lp->mfs + sizeof(struct fc_frame_header) +
186                 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
187 }
188
189 /**
190  * fcoe_ctlr_solicit() - Send a FIP solicitation
191  * @fip: The FCoE controller to send the solicitation on
192  * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
193  */
194 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
195 {
196         struct sk_buff *skb;
197         struct fip_sol {
198                 struct ethhdr eth;
199                 struct fip_header fip;
200                 struct {
201                         struct fip_mac_desc mac;
202                         struct fip_wwn_desc wwnn;
203                         struct fip_size_desc size;
204                 } __attribute__((packed)) desc;
205         }  __attribute__((packed)) *sol;
206         u32 fcoe_size;
207
208         skb = dev_alloc_skb(sizeof(*sol));
209         if (!skb)
210                 return;
211
212         sol = (struct fip_sol *)skb->data;
213
214         memset(sol, 0, sizeof(*sol));
215         memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
216         memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
217         sol->eth.h_proto = htons(ETH_P_FIP);
218
219         sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
220         sol->fip.fip_op = htons(FIP_OP_DISC);
221         sol->fip.fip_subcode = FIP_SC_SOL;
222         sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
223         sol->fip.fip_flags = htons(FIP_FL_FPMA);
224         if (fip->spma)
225                 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
226
227         sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
228         sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
229         memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
230
231         sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
232         sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
233         put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
234
235         fcoe_size = fcoe_ctlr_fcoe_size(fip);
236         sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
237         sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
238         sol->desc.size.fd_size = htons(fcoe_size);
239
240         skb_put(skb, sizeof(*sol));
241         skb->protocol = htons(ETH_P_FIP);
242         skb_reset_mac_header(skb);
243         skb_reset_network_header(skb);
244         fip->send(fip, skb);
245
246         if (!fcf)
247                 fip->sol_time = jiffies;
248 }
249
250 /**
251  * fcoe_ctlr_link_up() - Start FCoE controller
252  * @fip: The FCoE controller to start
253  *
254  * Called from the LLD when the network link is ready.
255  */
256 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
257 {
258         spin_lock_bh(&fip->lock);
259         if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
260                 spin_unlock_bh(&fip->lock);
261                 fc_linkup(fip->lp);
262         } else if (fip->state == FIP_ST_LINK_WAIT) {
263                 fip->state = fip->mode;
264                 spin_unlock_bh(&fip->lock);
265                 if (fip->state == FIP_ST_AUTO)
266                         LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
267                 fc_linkup(fip->lp);
268                 fcoe_ctlr_solicit(fip, NULL);
269         } else
270                 spin_unlock_bh(&fip->lock);
271 }
272 EXPORT_SYMBOL(fcoe_ctlr_link_up);
273
274 /**
275  * fcoe_ctlr_reset() - Reset a FCoE controller
276  * @fip:       The FCoE controller to reset
277  */
278 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
279 {
280         fcoe_ctlr_reset_fcfs(fip);
281         del_timer(&fip->timer);
282         fip->ctlr_ka_time = 0;
283         fip->port_ka_time = 0;
284         fip->sol_time = 0;
285         fip->flogi_oxid = FC_XID_UNKNOWN;
286         fip->map_dest = 0;
287 }
288
289 /**
290  * fcoe_ctlr_link_down() - Stop a FCoE controller
291  * @fip: The FCoE controller to be stopped
292  *
293  * Returns non-zero if the link was up and now isn't.
294  *
295  * Called from the LLD when the network link is not ready.
296  * There may be multiple calls while the link is down.
297  */
298 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
299 {
300         int link_dropped;
301
302         LIBFCOE_FIP_DBG(fip, "link down.\n");
303         spin_lock_bh(&fip->lock);
304         fcoe_ctlr_reset(fip);
305         link_dropped = fip->state != FIP_ST_LINK_WAIT;
306         fip->state = FIP_ST_LINK_WAIT;
307         spin_unlock_bh(&fip->lock);
308
309         if (link_dropped)
310                 fc_linkdown(fip->lp);
311         return link_dropped;
312 }
313 EXPORT_SYMBOL(fcoe_ctlr_link_down);
314
315 /**
316  * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
317  * @fip:   The FCoE controller to send the FKA on
318  * @lport: libfc fc_lport to send from
319  * @ports: 0 for controller keep-alive, 1 for port keep-alive
320  * @sa:    The source MAC address
321  *
322  * A controller keep-alive is sent every fka_period (typically 8 seconds).
323  * The source MAC is the native MAC address.
324  *
325  * A port keep-alive is sent every 90 seconds while logged in.
326  * The source MAC is the assigned mapped source address.
327  * The destination is the FCF's F-port.
328  */
329 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
330                                       struct fc_lport *lport,
331                                       int ports, u8 *sa)
332 {
333         struct sk_buff *skb;
334         struct fip_kal {
335                 struct ethhdr eth;
336                 struct fip_header fip;
337                 struct fip_mac_desc mac;
338         } __attribute__((packed)) *kal;
339         struct fip_vn_desc *vn;
340         u32 len;
341         struct fc_lport *lp;
342         struct fcoe_fcf *fcf;
343
344         fcf = fip->sel_fcf;
345         lp = fip->lp;
346         if (!fcf || !lp->port_id)
347                 return;
348
349         len = sizeof(*kal) + ports * sizeof(*vn);
350         skb = dev_alloc_skb(len);
351         if (!skb)
352                 return;
353
354         kal = (struct fip_kal *)skb->data;
355         memset(kal, 0, len);
356         memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
357         memcpy(kal->eth.h_source, sa, ETH_ALEN);
358         kal->eth.h_proto = htons(ETH_P_FIP);
359
360         kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
361         kal->fip.fip_op = htons(FIP_OP_CTRL);
362         kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
363         kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
364                                      ports * sizeof(*vn)) / FIP_BPW);
365         kal->fip.fip_flags = htons(FIP_FL_FPMA);
366         if (fip->spma)
367                 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
368
369         kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
370         kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
371         memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
372         if (ports) {
373                 vn = (struct fip_vn_desc *)(kal + 1);
374                 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
375                 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
376                 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
377                 hton24(vn->fd_fc_id, lport->port_id);
378                 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
379         }
380         skb_put(skb, len);
381         skb->protocol = htons(ETH_P_FIP);
382         skb_reset_mac_header(skb);
383         skb_reset_network_header(skb);
384         fip->send(fip, skb);
385 }
386
387 /**
388  * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
389  * @fip:   The FCoE controller for the ELS frame
390  * @dtype: The FIP descriptor type for the frame
391  * @skb:   The FCoE ELS frame including FC header but no FCoE headers
392  *
393  * Returns non-zero error code on failure.
394  *
395  * The caller must check that the length is a multiple of 4.
396  *
397  * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
398  * Headroom includes the FIP encapsulation description, FIP header, and
399  * Ethernet header.  The tailroom is for the FIP MAC descriptor.
400  */
401 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
402                             u8 dtype, struct sk_buff *skb)
403 {
404         struct fip_encaps_head {
405                 struct ethhdr eth;
406                 struct fip_header fip;
407                 struct fip_encaps encaps;
408         } __attribute__((packed)) *cap;
409         struct fip_mac_desc *mac;
410         struct fcoe_fcf *fcf;
411         size_t dlen;
412         u16 fip_flags;
413
414         fcf = fip->sel_fcf;
415         if (!fcf)
416                 return -ENODEV;
417
418         /* set flags according to both FCF and lport's capability on SPMA */
419         fip_flags = fcf->flags;
420         fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
421         if (!fip_flags)
422                 return -ENODEV;
423
424         dlen = sizeof(struct fip_encaps) + skb->len;    /* len before push */
425         cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
426
427         memset(cap, 0, sizeof(*cap));
428         memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
429         memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
430         cap->eth.h_proto = htons(ETH_P_FIP);
431
432         cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
433         cap->fip.fip_op = htons(FIP_OP_LS);
434         cap->fip.fip_subcode = FIP_SC_REQ;
435         cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
436         cap->fip.fip_flags = htons(fip_flags);
437
438         cap->encaps.fd_desc.fip_dtype = dtype;
439         cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
440
441         mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
442         memset(mac, 0, sizeof(*mac));
443         mac->fd_desc.fip_dtype = FIP_DT_MAC;
444         mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
445         if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
446                 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
447         } else if (fip_flags & FIP_FL_SPMA) {
448                 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
449                 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
450         } else {
451                 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
452                 /* FPMA only FLOGI must leave the MAC desc set to all 0s */
453         }
454
455         skb->protocol = htons(ETH_P_FIP);
456         skb_reset_mac_header(skb);
457         skb_reset_network_header(skb);
458         return 0;
459 }
460
461 /**
462  * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
463  * @fip:        FCoE controller.
464  * @lport:      libfc fc_lport to send from
465  * @skb:        FCoE ELS frame including FC header but no FCoE headers.
466  *
467  * Returns a non-zero error code if the frame should not be sent.
468  * Returns zero if the caller should send the frame with FCoE encapsulation.
469  *
470  * The caller must check that the length is a multiple of 4.
471  * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
472  */
473 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
474                        struct sk_buff *skb)
475 {
476         struct fc_frame_header *fh;
477         u16 old_xid;
478         u8 op;
479         u8 mac[ETH_ALEN];
480
481         fh = (struct fc_frame_header *)skb->data;
482         op = *(u8 *)(fh + 1);
483
484         if (op == ELS_FLOGI) {
485                 old_xid = fip->flogi_oxid;
486                 fip->flogi_oxid = ntohs(fh->fh_ox_id);
487                 if (fip->state == FIP_ST_AUTO) {
488                         if (old_xid == FC_XID_UNKNOWN)
489                                 fip->flogi_count = 0;
490                         fip->flogi_count++;
491                         if (fip->flogi_count < 3)
492                                 goto drop;
493                         fip->map_dest = 1;
494                         return 0;
495                 }
496                 if (fip->state == FIP_ST_NON_FIP)
497                         fip->map_dest = 1;
498         }
499
500         if (fip->state == FIP_ST_NON_FIP)
501                 return 0;
502         if (!fip->sel_fcf)
503                 goto drop;
504
505         switch (op) {
506         case ELS_FLOGI:
507                 op = FIP_DT_FLOGI;
508                 break;
509         case ELS_FDISC:
510                 if (ntoh24(fh->fh_s_id))
511                         return 0;
512                 op = FIP_DT_FDISC;
513                 break;
514         case ELS_LOGO:
515                 if (fip->state != FIP_ST_ENABLED)
516                         return 0;
517                 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
518                         return 0;
519                 op = FIP_DT_LOGO;
520                 break;
521         case ELS_LS_ACC:
522                 if (fip->flogi_oxid == FC_XID_UNKNOWN)
523                         return 0;
524                 if (!ntoh24(fh->fh_s_id))
525                         return 0;
526                 if (fip->state == FIP_ST_AUTO)
527                         return 0;
528                 /*
529                  * Here we must've gotten an SID by accepting an FLOGI
530                  * from a point-to-point connection.  Switch to using
531                  * the source mac based on the SID.  The destination
532                  * MAC in this case would have been set by receving the
533                  * FLOGI.
534                  */
535                 fip->flogi_oxid = FC_XID_UNKNOWN;
536                 fc_fcoe_set_mac(mac, fh->fh_d_id);
537                 fip->update_mac(lport, mac);
538                 return 0;
539         default:
540                 if (fip->state != FIP_ST_ENABLED)
541                         goto drop;
542                 return 0;
543         }
544         if (fcoe_ctlr_encaps(fip, lport, op, skb))
545                 goto drop;
546         fip->send(fip, skb);
547         return -EINPROGRESS;
548 drop:
549         kfree_skb(skb);
550         return -EINVAL;
551 }
552 EXPORT_SYMBOL(fcoe_ctlr_els_send);
553
554 /**
555  * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
556  * @fip: The FCoE controller to free FCFs on
557  *
558  * Called with lock held and preemption disabled.
559  *
560  * An FCF is considered old if we have missed three advertisements.
561  * That is, there have been no valid advertisement from it for three
562  * times its keep-alive period including fuzz.
563  *
564  * In addition, determine the time when an FCF selection can occur.
565  *
566  * Also, increment the MissDiscAdvCount when no advertisement is received
567  * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
568  */
569 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
570 {
571         struct fcoe_fcf *fcf;
572         struct fcoe_fcf *next;
573         unsigned long sel_time = 0;
574         unsigned long mda_time = 0;
575         struct fcoe_dev_stats *stats;
576
577         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
578                 mda_time = fcf->fka_period + (fcf->fka_period >> 1);
579                 if ((fip->sel_fcf == fcf) &&
580                     (time_after(jiffies, fcf->time + mda_time))) {
581                         mod_timer(&fip->timer, jiffies + mda_time);
582                         stats = per_cpu_ptr(fip->lp->dev_stats,
583                                             smp_processor_id());
584                         stats->MissDiscAdvCount++;
585                         printk(KERN_INFO "libfcoe: host%d: Missing Discovery "
586                                "Advertisement for fab %16.16llx count %lld\n",
587                                fip->lp->host->host_no, fcf->fabric_name,
588                                stats->MissDiscAdvCount);
589                 }
590                 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
591                                msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
592                         if (fip->sel_fcf == fcf)
593                                 fip->sel_fcf = NULL;
594                         list_del(&fcf->list);
595                         WARN_ON(!fip->fcf_count);
596                         fip->fcf_count--;
597                         kfree(fcf);
598                         stats = per_cpu_ptr(fip->lp->dev_stats,
599                                             smp_processor_id());
600                         stats->VLinkFailureCount++;
601                 } else if (fcoe_ctlr_mtu_valid(fcf) &&
602                            (!sel_time || time_before(sel_time, fcf->time))) {
603                         sel_time = fcf->time;
604                 }
605         }
606         if (sel_time) {
607                 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
608                 fip->sel_time = sel_time;
609                 if (time_before(sel_time, fip->timer.expires))
610                         mod_timer(&fip->timer, sel_time);
611         } else {
612                 fip->sel_time = 0;
613         }
614 }
615
616 /**
617  * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
618  * @fip: The FCoE controller receiving the advertisement
619  * @skb: The received FIP advertisement frame
620  * @fcf: The resulting FCF entry
621  *
622  * Returns zero on a valid parsed advertisement,
623  * otherwise returns non zero value.
624  */
625 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
626                                struct sk_buff *skb, struct fcoe_fcf *fcf)
627 {
628         struct fip_header *fiph;
629         struct fip_desc *desc = NULL;
630         struct fip_wwn_desc *wwn;
631         struct fip_fab_desc *fab;
632         struct fip_fka_desc *fka;
633         unsigned long t;
634         size_t rlen;
635         size_t dlen;
636
637         memset(fcf, 0, sizeof(*fcf));
638         fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
639
640         fiph = (struct fip_header *)skb->data;
641         fcf->flags = ntohs(fiph->fip_flags);
642
643         rlen = ntohs(fiph->fip_dl_len) * 4;
644         if (rlen + sizeof(*fiph) > skb->len)
645                 return -EINVAL;
646
647         desc = (struct fip_desc *)(fiph + 1);
648         while (rlen > 0) {
649                 dlen = desc->fip_dlen * FIP_BPW;
650                 if (dlen < sizeof(*desc) || dlen > rlen)
651                         return -EINVAL;
652                 switch (desc->fip_dtype) {
653                 case FIP_DT_PRI:
654                         if (dlen != sizeof(struct fip_pri_desc))
655                                 goto len_err;
656                         fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
657                         break;
658                 case FIP_DT_MAC:
659                         if (dlen != sizeof(struct fip_mac_desc))
660                                 goto len_err;
661                         memcpy(fcf->fcf_mac,
662                                ((struct fip_mac_desc *)desc)->fd_mac,
663                                ETH_ALEN);
664                         if (!is_valid_ether_addr(fcf->fcf_mac)) {
665                                 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
666                                                 "in FIP adv\n");
667                                 return -EINVAL;
668                         }
669                         break;
670                 case FIP_DT_NAME:
671                         if (dlen != sizeof(struct fip_wwn_desc))
672                                 goto len_err;
673                         wwn = (struct fip_wwn_desc *)desc;
674                         fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
675                         break;
676                 case FIP_DT_FAB:
677                         if (dlen != sizeof(struct fip_fab_desc))
678                                 goto len_err;
679                         fab = (struct fip_fab_desc *)desc;
680                         fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
681                         fcf->vfid = ntohs(fab->fd_vfid);
682                         fcf->fc_map = ntoh24(fab->fd_map);
683                         break;
684                 case FIP_DT_FKA:
685                         if (dlen != sizeof(struct fip_fka_desc))
686                                 goto len_err;
687                         fka = (struct fip_fka_desc *)desc;
688                         if (fka->fd_flags & FIP_FKA_ADV_D)
689                                 fcf->fd_flags = 1;
690                         t = ntohl(fka->fd_fka_period);
691                         if (t >= FCOE_CTLR_MIN_FKA)
692                                 fcf->fka_period = msecs_to_jiffies(t);
693                         break;
694                 case FIP_DT_MAP_OUI:
695                 case FIP_DT_FCOE_SIZE:
696                 case FIP_DT_FLOGI:
697                 case FIP_DT_FDISC:
698                 case FIP_DT_LOGO:
699                 case FIP_DT_ELP:
700                 default:
701                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
702                                         "in FIP adv\n", desc->fip_dtype);
703                         /* standard says ignore unknown descriptors >= 128 */
704                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
705                                 return -EINVAL;
706                         continue;
707                 }
708                 desc = (struct fip_desc *)((char *)desc + dlen);
709                 rlen -= dlen;
710         }
711         if (!fcf->fc_map || (fcf->fc_map & 0x10000))
712                 return -EINVAL;
713         if (!fcf->switch_name || !fcf->fabric_name)
714                 return -EINVAL;
715         return 0;
716
717 len_err:
718         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
719                         desc->fip_dtype, dlen);
720         return -EINVAL;
721 }
722
723 /**
724  * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
725  * @fip: The FCoE controller receiving the advertisement
726  * @skb: The received FIP packet
727  */
728 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
729 {
730         struct fcoe_fcf *fcf;
731         struct fcoe_fcf new;
732         struct fcoe_fcf *found;
733         unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
734         int first = 0;
735         int mtu_valid;
736
737         if (fcoe_ctlr_parse_adv(fip, skb, &new))
738                 return;
739
740         spin_lock_bh(&fip->lock);
741         first = list_empty(&fip->fcfs);
742         found = NULL;
743         list_for_each_entry(fcf, &fip->fcfs, list) {
744                 if (fcf->switch_name == new.switch_name &&
745                     fcf->fabric_name == new.fabric_name &&
746                     fcf->fc_map == new.fc_map &&
747                     compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
748                         found = fcf;
749                         break;
750                 }
751         }
752         if (!found) {
753                 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
754                         goto out;
755
756                 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
757                 if (!fcf)
758                         goto out;
759
760                 fip->fcf_count++;
761                 memcpy(fcf, &new, sizeof(new));
762                 list_add(&fcf->list, &fip->fcfs);
763         } else {
764                 /*
765                  * Flags in advertisements are ignored once the FCF is
766                  * selected.  Flags in unsolicited advertisements are
767                  * ignored after a usable solicited advertisement
768                  * has been received.
769                  */
770                 if (fcf == fip->sel_fcf) {
771                         fip->ctlr_ka_time -= fcf->fka_period;
772                         fip->ctlr_ka_time += new.fka_period;
773                         if (time_before(fip->ctlr_ka_time, fip->timer.expires))
774                                 mod_timer(&fip->timer, fip->ctlr_ka_time);
775                 } else if (!fcoe_ctlr_fcf_usable(fcf))
776                         fcf->flags = new.flags;
777                 fcf->fka_period = new.fka_period;
778                 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
779         }
780         mtu_valid = fcoe_ctlr_mtu_valid(fcf);
781         fcf->time = jiffies;
782         if (!found) {
783                 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
784                                 "map %x val %d\n",
785                                 fcf->fabric_name, fcf->fc_map, mtu_valid);
786         }
787
788         /*
789          * If this advertisement is not solicited and our max receive size
790          * hasn't been verified, send a solicited advertisement.
791          */
792         if (!mtu_valid)
793                 fcoe_ctlr_solicit(fip, fcf);
794
795         /*
796          * If its been a while since we did a solicit, and this is
797          * the first advertisement we've received, do a multicast
798          * solicitation to gather as many advertisements as we can
799          * before selection occurs.
800          */
801         if (first && time_after(jiffies, fip->sol_time + sol_tov))
802                 fcoe_ctlr_solicit(fip, NULL);
803
804         /*
805          * If this is the first validated FCF, note the time and
806          * set a timer to trigger selection.
807          */
808         if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
809                 fip->sel_time = jiffies +
810                         msecs_to_jiffies(FCOE_CTLR_START_DELAY);
811                 if (!timer_pending(&fip->timer) ||
812                     time_before(fip->sel_time, fip->timer.expires))
813                         mod_timer(&fip->timer, fip->sel_time);
814         }
815 out:
816         spin_unlock_bh(&fip->lock);
817 }
818
819 /**
820  * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
821  * @fip: The FCoE controller which received the packet
822  * @skb: The received FIP packet
823  */
824 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
825 {
826         struct fc_lport *lport = fip->lp;
827         struct fip_header *fiph;
828         struct fc_frame *fp = (struct fc_frame *)skb;
829         struct fc_frame_header *fh = NULL;
830         struct fip_desc *desc;
831         struct fip_encaps *els;
832         struct fcoe_dev_stats *stats;
833         enum fip_desc_type els_dtype = 0;
834         u8 els_op;
835         u8 sub;
836         u8 granted_mac[ETH_ALEN] = { 0 };
837         size_t els_len = 0;
838         size_t rlen;
839         size_t dlen;
840
841         fiph = (struct fip_header *)skb->data;
842         sub = fiph->fip_subcode;
843         if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
844                 goto drop;
845
846         rlen = ntohs(fiph->fip_dl_len) * 4;
847         if (rlen + sizeof(*fiph) > skb->len)
848                 goto drop;
849
850         desc = (struct fip_desc *)(fiph + 1);
851         while (rlen > 0) {
852                 dlen = desc->fip_dlen * FIP_BPW;
853                 if (dlen < sizeof(*desc) || dlen > rlen)
854                         goto drop;
855                 switch (desc->fip_dtype) {
856                 case FIP_DT_MAC:
857                         if (dlen != sizeof(struct fip_mac_desc))
858                                 goto len_err;
859                         memcpy(granted_mac,
860                                ((struct fip_mac_desc *)desc)->fd_mac,
861                                ETH_ALEN);
862                         if (!is_valid_ether_addr(granted_mac)) {
863                                 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
864                                                 "in FIP ELS\n");
865                                 goto drop;
866                         }
867                         memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
868                         break;
869                 case FIP_DT_FLOGI:
870                 case FIP_DT_FDISC:
871                 case FIP_DT_LOGO:
872                 case FIP_DT_ELP:
873                         if (fh)
874                                 goto drop;
875                         if (dlen < sizeof(*els) + sizeof(*fh) + 1)
876                                 goto len_err;
877                         els_len = dlen - sizeof(*els);
878                         els = (struct fip_encaps *)desc;
879                         fh = (struct fc_frame_header *)(els + 1);
880                         els_dtype = desc->fip_dtype;
881                         break;
882                 default:
883                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
884                                         "in FIP adv\n", desc->fip_dtype);
885                         /* standard says ignore unknown descriptors >= 128 */
886                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
887                                 goto drop;
888                         continue;
889                 }
890                 desc = (struct fip_desc *)((char *)desc + dlen);
891                 rlen -= dlen;
892         }
893
894         if (!fh)
895                 goto drop;
896         els_op = *(u8 *)(fh + 1);
897
898         if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
899             fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
900             els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac))
901                 fip->flogi_oxid = FC_XID_UNKNOWN;
902
903         /*
904          * Convert skb into an fc_frame containing only the ELS.
905          */
906         skb_pull(skb, (u8 *)fh - skb->data);
907         skb_trim(skb, els_len);
908         fp = (struct fc_frame *)skb;
909         fc_frame_init(fp);
910         fr_sof(fp) = FC_SOF_I3;
911         fr_eof(fp) = FC_EOF_T;
912         fr_dev(fp) = lport;
913
914         stats = per_cpu_ptr(lport->dev_stats, get_cpu());
915         stats->RxFrames++;
916         stats->RxWords += skb->len / FIP_BPW;
917         put_cpu();
918
919         fc_exch_recv(lport, fp);
920         return;
921
922 len_err:
923         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
924                         desc->fip_dtype, dlen);
925 drop:
926         kfree_skb(skb);
927 }
928
929 /**
930  * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
931  * @fip: The FCoE controller that received the frame
932  * @fh:  The received FIP header
933  *
934  * There may be multiple VN_Port descriptors.
935  * The overall length has already been checked.
936  */
937 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
938                                      struct fip_header *fh)
939 {
940         struct fip_desc *desc;
941         struct fip_mac_desc *mp;
942         struct fip_wwn_desc *wp;
943         struct fip_vn_desc *vp;
944         size_t rlen;
945         size_t dlen;
946         struct fcoe_fcf *fcf = fip->sel_fcf;
947         struct fc_lport *lport = fip->lp;
948         u32     desc_mask;
949
950         LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
951
952         if (!fcf || !lport->port_id)
953                 return;
954
955         /*
956          * mask of required descriptors.  Validating each one clears its bit.
957          */
958         desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
959
960         rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
961         desc = (struct fip_desc *)(fh + 1);
962         while (rlen >= sizeof(*desc)) {
963                 dlen = desc->fip_dlen * FIP_BPW;
964                 if (dlen > rlen)
965                         return;
966                 switch (desc->fip_dtype) {
967                 case FIP_DT_MAC:
968                         mp = (struct fip_mac_desc *)desc;
969                         if (dlen < sizeof(*mp))
970                                 return;
971                         if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
972                                 return;
973                         desc_mask &= ~BIT(FIP_DT_MAC);
974                         break;
975                 case FIP_DT_NAME:
976                         wp = (struct fip_wwn_desc *)desc;
977                         if (dlen < sizeof(*wp))
978                                 return;
979                         if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
980                                 return;
981                         desc_mask &= ~BIT(FIP_DT_NAME);
982                         break;
983                 case FIP_DT_VN_ID:
984                         vp = (struct fip_vn_desc *)desc;
985                         if (dlen < sizeof(*vp))
986                                 return;
987                         if (compare_ether_addr(vp->fd_mac,
988                                                fip->get_src_addr(lport)) == 0 &&
989                             get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
990                             ntoh24(vp->fd_fc_id) == lport->port_id)
991                                 desc_mask &= ~BIT(FIP_DT_VN_ID);
992                         break;
993                 default:
994                         /* standard says ignore unknown descriptors >= 128 */
995                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
996                                 return;
997                         break;
998                 }
999                 desc = (struct fip_desc *)((char *)desc + dlen);
1000                 rlen -= dlen;
1001         }
1002
1003         /*
1004          * reset only if all required descriptors were present and valid.
1005          */
1006         if (desc_mask) {
1007                 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1008                                 desc_mask);
1009         } else {
1010                 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1011
1012                 spin_lock_bh(&fip->lock);
1013                 per_cpu_ptr(lport->dev_stats,
1014                             smp_processor_id())->VLinkFailureCount++;
1015                 fcoe_ctlr_reset(fip);
1016                 spin_unlock_bh(&fip->lock);
1017
1018                 fc_lport_reset(fip->lp);
1019                 fcoe_ctlr_solicit(fip, NULL);
1020         }
1021 }
1022
1023 /**
1024  * fcoe_ctlr_recv() - Receive a FIP packet
1025  * @fip: The FCoE controller that received the packet
1026  * @skb: The received FIP packet
1027  *
1028  * This may be called from either NET_RX_SOFTIRQ or IRQ.
1029  */
1030 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1031 {
1032         skb_queue_tail(&fip->fip_recv_list, skb);
1033         schedule_work(&fip->recv_work);
1034 }
1035 EXPORT_SYMBOL(fcoe_ctlr_recv);
1036
1037 /**
1038  * fcoe_ctlr_recv_handler() - Receive a FIP frame
1039  * @fip: The FCoE controller that received the frame
1040  * @skb: The received FIP frame
1041  *
1042  * Returns non-zero if the frame is dropped.
1043  */
1044 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1045 {
1046         struct fip_header *fiph;
1047         struct ethhdr *eh;
1048         enum fip_state state;
1049         u16 op;
1050         u8 sub;
1051
1052         if (skb_linearize(skb))
1053                 goto drop;
1054         if (skb->len < sizeof(*fiph))
1055                 goto drop;
1056         eh = eth_hdr(skb);
1057         if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1058             compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1059                 goto drop;
1060         fiph = (struct fip_header *)skb->data;
1061         op = ntohs(fiph->fip_op);
1062         sub = fiph->fip_subcode;
1063
1064         if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1065                 goto drop;
1066         if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1067                 goto drop;
1068
1069         spin_lock_bh(&fip->lock);
1070         state = fip->state;
1071         if (state == FIP_ST_AUTO) {
1072                 fip->map_dest = 0;
1073                 fip->state = FIP_ST_ENABLED;
1074                 state = FIP_ST_ENABLED;
1075                 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1076         }
1077         spin_unlock_bh(&fip->lock);
1078         if (state != FIP_ST_ENABLED)
1079                 goto drop;
1080
1081         if (op == FIP_OP_LS) {
1082                 fcoe_ctlr_recv_els(fip, skb);   /* consumes skb */
1083                 return 0;
1084         }
1085         if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1086                 fcoe_ctlr_recv_adv(fip, skb);
1087         else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1088                 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1089         kfree_skb(skb);
1090         return 0;
1091 drop:
1092         kfree_skb(skb);
1093         return -1;
1094 }
1095
1096 /**
1097  * fcoe_ctlr_select() - Select the best FCF (if possible)
1098  * @fip: The FCoE controller
1099  *
1100  * If there are conflicting advertisements, no FCF can be chosen.
1101  *
1102  * Called with lock held.
1103  */
1104 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1105 {
1106         struct fcoe_fcf *fcf;
1107         struct fcoe_fcf *best = NULL;
1108
1109         list_for_each_entry(fcf, &fip->fcfs, list) {
1110                 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1111                                 "VFID %d map %x val %d\n",
1112                                 fcf->fabric_name, fcf->vfid,
1113                                 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1114                 if (!fcoe_ctlr_fcf_usable(fcf)) {
1115                         LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1116                                         "map %x %svalid %savailable\n",
1117                                         fcf->fabric_name, fcf->fc_map,
1118                                         (fcf->flags & FIP_FL_SOL) ? "" : "in",
1119                                         (fcf->flags & FIP_FL_AVAIL) ?
1120                                         "" : "un");
1121                         continue;
1122                 }
1123                 if (!best) {
1124                         best = fcf;
1125                         continue;
1126                 }
1127                 if (fcf->fabric_name != best->fabric_name ||
1128                     fcf->vfid != best->vfid ||
1129                     fcf->fc_map != best->fc_map) {
1130                         LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1131                                         "or FC-MAP\n");
1132                         return;
1133                 }
1134                 if (fcf->pri < best->pri)
1135                         best = fcf;
1136         }
1137         fip->sel_fcf = best;
1138 }
1139
1140 /**
1141  * fcoe_ctlr_timeout() - FIP timeout handler
1142  * @arg: The FCoE controller that timed out
1143  *
1144  * Ages FCFs.  Triggers FCF selection if possible.  Sends keep-alives.
1145  */
1146 static void fcoe_ctlr_timeout(unsigned long arg)
1147 {
1148         struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1149         struct fcoe_fcf *sel;
1150         struct fcoe_fcf *fcf;
1151         unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
1152
1153         spin_lock_bh(&fip->lock);
1154         if (fip->state == FIP_ST_DISABLED) {
1155                 spin_unlock_bh(&fip->lock);
1156                 return;
1157         }
1158
1159         fcf = fip->sel_fcf;
1160         fcoe_ctlr_age_fcfs(fip);
1161
1162         sel = fip->sel_fcf;
1163         if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1164                 fcoe_ctlr_select(fip);
1165                 sel = fip->sel_fcf;
1166                 fip->sel_time = 0;
1167         }
1168
1169         if (sel != fcf) {
1170                 fcf = sel;              /* the old FCF may have been freed */
1171                 if (sel) {
1172                         printk(KERN_INFO "libfcoe: host%d: FIP selected "
1173                                "Fibre-Channel Forwarder MAC %pM\n",
1174                                fip->lp->host->host_no, sel->fcf_mac);
1175                         memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1176                         fip->port_ka_time = jiffies +
1177                                 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1178                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1179                 } else {
1180                         printk(KERN_NOTICE "libfcoe: host%d: "
1181                                "FIP Fibre-Channel Forwarder timed out.  "
1182                                "Starting FCF discovery.\n",
1183                                fip->lp->host->host_no);
1184                         fip->reset_req = 1;
1185                         schedule_work(&fip->timer_work);
1186                 }
1187         }
1188
1189         if (sel && !sel->fd_flags) {
1190                 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1191                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1192                         fip->send_ctlr_ka = 1;
1193                 }
1194                 if (time_after(next_timer, fip->ctlr_ka_time))
1195                         next_timer = fip->ctlr_ka_time;
1196
1197                 if (time_after_eq(jiffies, fip->port_ka_time)) {
1198                         fip->port_ka_time = jiffies +
1199                                 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1200                         fip->send_port_ka = 1;
1201                 }
1202                 if (time_after(next_timer, fip->port_ka_time))
1203                         next_timer = fip->port_ka_time;
1204                 mod_timer(&fip->timer, next_timer);
1205         } else if (fip->sel_time) {
1206                 next_timer = fip->sel_time +
1207                         msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1208                 mod_timer(&fip->timer, next_timer);
1209         }
1210         if (fip->send_ctlr_ka || fip->send_port_ka)
1211                 schedule_work(&fip->timer_work);
1212         spin_unlock_bh(&fip->lock);
1213 }
1214
1215 /**
1216  * fcoe_ctlr_timer_work() - Worker thread function for timer work
1217  * @work: Handle to a FCoE controller
1218  *
1219  * Sends keep-alives and resets which must not
1220  * be called from the timer directly, since they use a mutex.
1221  */
1222 static void fcoe_ctlr_timer_work(struct work_struct *work)
1223 {
1224         struct fcoe_ctlr *fip;
1225         struct fc_lport *vport;
1226         u8 *mac;
1227         int reset;
1228
1229         fip = container_of(work, struct fcoe_ctlr, timer_work);
1230         spin_lock_bh(&fip->lock);
1231         reset = fip->reset_req;
1232         fip->reset_req = 0;
1233         spin_unlock_bh(&fip->lock);
1234
1235         if (reset)
1236                 fc_lport_reset(fip->lp);
1237
1238         if (fip->send_ctlr_ka) {
1239                 fip->send_ctlr_ka = 0;
1240                 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1241         }
1242         if (fip->send_port_ka) {
1243                 fip->send_port_ka = 0;
1244                 mutex_lock(&fip->lp->lp_mutex);
1245                 mac = fip->get_src_addr(fip->lp);
1246                 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1247                 list_for_each_entry(vport, &fip->lp->vports, list) {
1248                         mac = fip->get_src_addr(vport);
1249                         fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1250                 }
1251                 mutex_unlock(&fip->lp->lp_mutex);
1252         }
1253 }
1254
1255 /**
1256  * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1257  * @recv_work: Handle to a FCoE controller
1258  */
1259 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1260 {
1261         struct fcoe_ctlr *fip;
1262         struct sk_buff *skb;
1263
1264         fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1265         while ((skb = skb_dequeue(&fip->fip_recv_list)))
1266                 fcoe_ctlr_recv_handler(fip, skb);
1267 }
1268
1269 /**
1270  * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1271  * @fip: The FCoE controller
1272  * @fp:  The FC frame to snoop
1273  *
1274  * Snoop potential response to FLOGI or even incoming FLOGI.
1275  *
1276  * The caller has checked that we are waiting for login as indicated
1277  * by fip->flogi_oxid != FC_XID_UNKNOWN.
1278  *
1279  * The caller is responsible for freeing the frame.
1280  * Fill in the granted_mac address.
1281  *
1282  * Return non-zero if the frame should not be delivered to libfc.
1283  */
1284 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1285                          struct fc_frame *fp)
1286 {
1287         struct fc_frame_header *fh;
1288         u8 op;
1289         u8 *sa;
1290
1291         sa = eth_hdr(&fp->skb)->h_source;
1292         fh = fc_frame_header_get(fp);
1293         if (fh->fh_type != FC_TYPE_ELS)
1294                 return 0;
1295
1296         op = fc_frame_payload_op(fp);
1297         if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1298             fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1299
1300                 spin_lock_bh(&fip->lock);
1301                 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1302                         spin_unlock_bh(&fip->lock);
1303                         return -EINVAL;
1304                 }
1305                 fip->state = FIP_ST_NON_FIP;
1306                 LIBFCOE_FIP_DBG(fip,
1307                                 "received FLOGI LS_ACC using non-FIP mode\n");
1308
1309                 /*
1310                  * FLOGI accepted.
1311                  * If the src mac addr is FC_OUI-based, then we mark the
1312                  * address_mode flag to use FC_OUI-based Ethernet DA.
1313                  * Otherwise we use the FCoE gateway addr
1314                  */
1315                 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1316                         fip->map_dest = 1;
1317                 } else {
1318                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1319                         fip->map_dest = 0;
1320                 }
1321                 fip->flogi_oxid = FC_XID_UNKNOWN;
1322                 spin_unlock_bh(&fip->lock);
1323                 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1324         } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1325                 /*
1326                  * Save source MAC for point-to-point responses.
1327                  */
1328                 spin_lock_bh(&fip->lock);
1329                 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1330                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1331                         fip->map_dest = 0;
1332                         if (fip->state == FIP_ST_AUTO)
1333                                 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1334                                                 "Setting non-FIP mode\n");
1335                         fip->state = FIP_ST_NON_FIP;
1336                 }
1337                 spin_unlock_bh(&fip->lock);
1338         }
1339         return 0;
1340 }
1341 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1342
1343 /**
1344  * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1345  * @mac:    The MAC address to convert
1346  * @scheme: The scheme to use when converting
1347  * @port:   The port indicator for converting
1348  *
1349  * Returns: u64 fc world wide name
1350  */
1351 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1352                       unsigned int scheme, unsigned int port)
1353 {
1354         u64 wwn;
1355         u64 host_mac;
1356
1357         /* The MAC is in NO, so flip only the low 48 bits */
1358         host_mac = ((u64) mac[0] << 40) |
1359                 ((u64) mac[1] << 32) |
1360                 ((u64) mac[2] << 24) |
1361                 ((u64) mac[3] << 16) |
1362                 ((u64) mac[4] << 8) |
1363                 (u64) mac[5];
1364
1365         WARN_ON(host_mac >= (1ULL << 48));
1366         wwn = host_mac | ((u64) scheme << 60);
1367         switch (scheme) {
1368         case 1:
1369                 WARN_ON(port != 0);
1370                 break;
1371         case 2:
1372                 WARN_ON(port >= 0xfff);
1373                 wwn |= (u64) port << 48;
1374                 break;
1375         default:
1376                 WARN_ON(1);
1377                 break;
1378         }
1379
1380         return wwn;
1381 }
1382 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1383
1384 /**
1385  * fcoe_libfc_config() - Sets up libfc related properties for local port
1386  * @lp: The local port to configure libfc for
1387  * @tt: The libfc function template
1388  *
1389  * Returns : 0 for success
1390  */
1391 int fcoe_libfc_config(struct fc_lport *lport,
1392                       struct libfc_function_template *tt)
1393 {
1394         /* Set the function pointers set by the LLDD */
1395         memcpy(&lport->tt, tt, sizeof(*tt));
1396         if (fc_fcp_init(lport))
1397                 return -ENOMEM;
1398         fc_exch_init(lport);
1399         fc_elsct_init(lport);
1400         fc_lport_init(lport);
1401         fc_rport_init(lport);
1402         fc_disc_init(lport);
1403
1404         return 0;
1405 }
1406 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1407