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