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