[SCSI] libfcoe, fcoe: libfcoe NPIV support
[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/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(fmt, args...)                                   \
78         LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING,                      \
79                               printk(KERN_INFO "fip: " fmt, ##args);)
80
81 /*
82  * Return non-zero if FCF fcoe_size has been validated.
83  */
84 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
85 {
86         return (fcf->flags & FIP_FL_SOL) != 0;
87 }
88
89 /*
90  * Return non-zero if the FCF is usable.
91  */
92 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
93 {
94         u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
95
96         return (fcf->flags & flags) == flags;
97 }
98
99 /**
100  * fcoe_ctlr_init() - Initialize the FCoE Controller instance.
101  * @fip:        FCoE controller.
102  */
103 void fcoe_ctlr_init(struct fcoe_ctlr *fip)
104 {
105         fip->state = FIP_ST_LINK_WAIT;
106         INIT_LIST_HEAD(&fip->fcfs);
107         spin_lock_init(&fip->lock);
108         fip->flogi_oxid = FC_XID_UNKNOWN;
109         setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
110         INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
111         INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
112         skb_queue_head_init(&fip->fip_recv_list);
113 }
114 EXPORT_SYMBOL(fcoe_ctlr_init);
115
116 /**
117  * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller.
118  * @fip:        FCoE controller.
119  *
120  * Called with &fcoe_ctlr lock held.
121  */
122 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
123 {
124         struct fcoe_fcf *fcf;
125         struct fcoe_fcf *next;
126
127         fip->sel_fcf = NULL;
128         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
129                 list_del(&fcf->list);
130                 kfree(fcf);
131         }
132         fip->fcf_count = 0;
133         fip->sel_time = 0;
134 }
135
136 /**
137  * fcoe_ctlr_destroy() - Disable and tear-down the FCoE controller.
138  * @fip:        FCoE controller.
139  *
140  * This is called by FCoE drivers before freeing the &fcoe_ctlr.
141  *
142  * The receive handler will have been deleted before this to guarantee
143  * that no more recv_work will be scheduled.
144  *
145  * The timer routine will simply return once we set FIP_ST_DISABLED.
146  * This guarantees that no further timeouts or work will be scheduled.
147  */
148 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
149 {
150         cancel_work_sync(&fip->recv_work);
151         spin_lock_bh(&fip->fip_recv_list.lock);
152         __skb_queue_purge(&fip->fip_recv_list);
153         spin_unlock_bh(&fip->fip_recv_list.lock);
154
155         spin_lock_bh(&fip->lock);
156         fip->state = FIP_ST_DISABLED;
157         fcoe_ctlr_reset_fcfs(fip);
158         spin_unlock_bh(&fip->lock);
159         del_timer_sync(&fip->timer);
160         cancel_work_sync(&fip->link_work);
161 }
162 EXPORT_SYMBOL(fcoe_ctlr_destroy);
163
164 /**
165  * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
166  * @fip:        FCoE controller.
167  *
168  * Returns the maximum packet size including the FCoE header and trailer,
169  * but not including any Ethernet or VLAN headers.
170  */
171 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
172 {
173         /*
174          * Determine the max FCoE frame size allowed, including
175          * FCoE header and trailer.
176          * Note:  lp->mfs is currently the payload size, not the frame size.
177          */
178         return fip->lp->mfs + sizeof(struct fc_frame_header) +
179                 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
180 }
181
182 /**
183  * fcoe_ctlr_solicit() - Send a solicitation.
184  * @fip:        FCoE controller.
185  * @fcf:        Destination FCF.  If NULL, a multicast solicitation is sent.
186  */
187 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
188 {
189         struct sk_buff *skb;
190         struct fip_sol {
191                 struct ethhdr eth;
192                 struct fip_header fip;
193                 struct {
194                         struct fip_mac_desc mac;
195                         struct fip_wwn_desc wwnn;
196                         struct fip_size_desc size;
197                 } __attribute__((packed)) desc;
198         }  __attribute__((packed)) *sol;
199         u32 fcoe_size;
200
201         skb = dev_alloc_skb(sizeof(*sol));
202         if (!skb)
203                 return;
204
205         sol = (struct fip_sol *)skb->data;
206
207         memset(sol, 0, sizeof(*sol));
208         memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
209         memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
210         sol->eth.h_proto = htons(ETH_P_FIP);
211
212         sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
213         sol->fip.fip_op = htons(FIP_OP_DISC);
214         sol->fip.fip_subcode = FIP_SC_SOL;
215         sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
216         sol->fip.fip_flags = htons(FIP_FL_FPMA);
217         if (fip->spma)
218                 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
219
220         sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
221         sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
222         memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
223
224         sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
225         sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
226         put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
227
228         fcoe_size = fcoe_ctlr_fcoe_size(fip);
229         sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
230         sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
231         sol->desc.size.fd_size = htons(fcoe_size);
232
233         skb_put(skb, sizeof(*sol));
234         skb->protocol = htons(ETH_P_FIP);
235         skb_reset_mac_header(skb);
236         skb_reset_network_header(skb);
237         fip->send(fip, skb);
238
239         if (!fcf)
240                 fip->sol_time = jiffies;
241 }
242
243 /**
244  * fcoe_ctlr_link_up() - Start FCoE controller.
245  * @fip:        FCoE controller.
246  *
247  * Called from the LLD when the network link is ready.
248  */
249 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
250 {
251         spin_lock_bh(&fip->lock);
252         if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
253                 fip->last_link = 1;
254                 fip->link = 1;
255                 spin_unlock_bh(&fip->lock);
256                 fc_linkup(fip->lp);
257         } else if (fip->state == FIP_ST_LINK_WAIT) {
258                 fip->state = FIP_ST_AUTO;
259                 fip->last_link = 1;
260                 fip->link = 1;
261                 spin_unlock_bh(&fip->lock);
262                 LIBFCOE_FIP_DBG("%s", "setting AUTO mode.\n");
263                 fc_linkup(fip->lp);
264                 fcoe_ctlr_solicit(fip, NULL);
265         } else
266                 spin_unlock_bh(&fip->lock);
267 }
268 EXPORT_SYMBOL(fcoe_ctlr_link_up);
269
270 /**
271  * fcoe_ctlr_reset() - Reset FIP.
272  * @fip:        FCoE controller.
273  * @new_state:  FIP state to be entered.
274  *
275  * Returns non-zero if the link was up and now isn't.
276  */
277 static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
278 {
279         struct fc_lport *lp = fip->lp;
280         int link_dropped;
281
282         spin_lock_bh(&fip->lock);
283         fcoe_ctlr_reset_fcfs(fip);
284         del_timer(&fip->timer);
285         fip->state = new_state;
286         fip->ctlr_ka_time = 0;
287         fip->port_ka_time = 0;
288         fip->sol_time = 0;
289         fip->flogi_oxid = FC_XID_UNKNOWN;
290         fip->map_dest = 0;
291         fip->last_link = 0;
292         link_dropped = fip->link;
293         fip->link = 0;
294         spin_unlock_bh(&fip->lock);
295
296         if (link_dropped)
297                 fc_linkdown(lp);
298
299         if (new_state == FIP_ST_ENABLED) {
300                 fcoe_ctlr_solicit(fip, NULL);
301                 fc_linkup(lp);
302                 link_dropped = 0;
303         }
304         return link_dropped;
305 }
306
307 /**
308  * fcoe_ctlr_link_down() - Stop FCoE controller.
309  * @fip:        FCoE controller.
310  *
311  * Returns non-zero if the link was up and now isn't.
312  *
313  * Called from the LLD when the network link is not ready.
314  * There may be multiple calls while the link is down.
315  */
316 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
317 {
318         return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
319 }
320 EXPORT_SYMBOL(fcoe_ctlr_link_down);
321
322 /**
323  * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
324  * @fip:        FCoE controller.
325  * @lport:      libfc fc_lport to send from
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,
337                                       struct fc_lport *lport,
338                                       int ports, u8 *sa)
339 {
340         struct sk_buff *skb;
341         struct fip_kal {
342                 struct ethhdr eth;
343                 struct fip_header fip;
344                 struct fip_mac_desc mac;
345         } __attribute__((packed)) *kal;
346         struct fip_vn_desc *vn;
347         u32 len;
348         struct fc_lport *lp;
349         struct fcoe_fcf *fcf;
350
351         fcf = fip->sel_fcf;
352         lp = fip->lp;
353         if (!fcf || !fc_host_port_id(lp->host))
354                 return;
355
356         len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
357         BUG_ON(len < sizeof(*kal) + sizeof(*vn));
358         skb = dev_alloc_skb(len);
359         if (!skb)
360                 return;
361
362         kal = (struct fip_kal *)skb->data;
363         memset(kal, 0, len);
364         memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
365         memcpy(kal->eth.h_source, sa, ETH_ALEN);
366         kal->eth.h_proto = htons(ETH_P_FIP);
367
368         kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
369         kal->fip.fip_op = htons(FIP_OP_CTRL);
370         kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
371         kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
372                                     ports * sizeof(*vn)) / FIP_BPW);
373         kal->fip.fip_flags = htons(FIP_FL_FPMA);
374         if (fip->spma)
375                 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
376
377         kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
378         kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
379         memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
380         if (ports) {
381                 vn = (struct fip_vn_desc *)(kal + 1);
382                 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
383                 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
384                 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
385                 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
386                 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
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  * @lport:      libfc fc_lport to use for the source address
399  * @dtype:      FIP descriptor type for the frame.
400  * @skb:        FCoE ELS frame including FC header but no FCoE headers.
401  *
402  * Returns non-zero error code on failure.
403  *
404  * The caller must check that the length is a multiple of 4.
405  *
406  * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
407  * Headroom includes the FIP encapsulation description, FIP header, and
408  * Ethernet header.  The tailroom is for the FIP MAC descriptor.
409  */
410 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
411                             u8 dtype, struct sk_buff *skb)
412 {
413         struct fip_encaps_head {
414                 struct ethhdr eth;
415                 struct fip_header fip;
416                 struct fip_encaps encaps;
417         } __attribute__((packed)) *cap;
418         struct fip_mac_desc *mac;
419         struct fcoe_fcf *fcf;
420         size_t dlen;
421         u16 fip_flags;
422
423         fcf = fip->sel_fcf;
424         if (!fcf)
425                 return -ENODEV;
426
427         /* set flags according to both FCF and lport's capability on SPMA */
428         fip_flags = fcf->flags;
429         fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
430         if (!fip_flags)
431                 return -ENODEV;
432
433         dlen = sizeof(struct fip_encaps) + skb->len;    /* len before push */
434         cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
435
436         memset(cap, 0, sizeof(*cap));
437         memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
438         memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
439         cap->eth.h_proto = htons(ETH_P_FIP);
440
441         cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
442         cap->fip.fip_op = htons(FIP_OP_LS);
443         cap->fip.fip_subcode = FIP_SC_REQ;
444         cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
445         cap->fip.fip_flags = htons(fip_flags);
446
447         cap->encaps.fd_desc.fip_dtype = dtype;
448         cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
449
450         mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
451         memset(mac, 0, sizeof(mac));
452         mac->fd_desc.fip_dtype = FIP_DT_MAC;
453         mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
454         if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC)
455                 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
456         else if (fip->spma)
457                 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
458
459         skb->protocol = htons(ETH_P_FIP);
460         skb_reset_mac_header(skb);
461         skb_reset_network_header(skb);
462         return 0;
463 }
464
465 /**
466  * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
467  * @fip:        FCoE controller.
468  * @lport:      libfc fc_lport to send from
469  * @skb:        FCoE ELS frame including FC header but no FCoE headers.
470  *
471  * Returns a non-zero error code if the frame should not be sent.
472  * Returns zero if the caller should send the frame with FCoE encapsulation.
473  *
474  * The caller must check that the length is a multiple of 4.
475  * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
476  */
477 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
478                        struct sk_buff *skb)
479 {
480         struct fc_frame_header *fh;
481         u16 old_xid;
482         u8 op;
483         u8 mac[ETH_ALEN];
484
485         fh = (struct fc_frame_header *)skb->data;
486         op = *(u8 *)(fh + 1);
487
488         if (op == ELS_FLOGI) {
489                 old_xid = fip->flogi_oxid;
490                 fip->flogi_oxid = ntohs(fh->fh_ox_id);
491                 if (fip->state == FIP_ST_AUTO) {
492                         if (old_xid == FC_XID_UNKNOWN)
493                                 fip->flogi_count = 0;
494                         fip->flogi_count++;
495                         if (fip->flogi_count < 3)
496                                 goto drop;
497                         fip->map_dest = 1;
498                         return 0;
499                 }
500                 if (fip->state == FIP_ST_NON_FIP)
501                         fip->map_dest = 1;
502         }
503
504         if (fip->state == FIP_ST_NON_FIP)
505                 return 0;
506
507         switch (op) {
508         case ELS_FLOGI:
509                 op = FIP_DT_FLOGI;
510                 break;
511         case ELS_FDISC:
512                 if (ntoh24(fh->fh_s_id))
513                         return 0;
514                 op = FIP_DT_FDISC;
515                 break;
516         case ELS_LOGO:
517                 if (fip->state != FIP_ST_ENABLED)
518                         return 0;
519                 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
520                         return 0;
521                 op = FIP_DT_LOGO;
522                 break;
523         case ELS_LS_ACC:
524                 if (fip->flogi_oxid == FC_XID_UNKNOWN)
525                         return 0;
526                 if (!ntoh24(fh->fh_s_id))
527                         return 0;
528                 if (fip->state == FIP_ST_AUTO)
529                         return 0;
530                 /*
531                  * Here we must've gotten an SID by accepting an FLOGI
532                  * from a point-to-point connection.  Switch to using
533                  * the source mac based on the SID.  The destination
534                  * MAC in this case would have been set by receving the
535                  * FLOGI.
536                  */
537                 fip->flogi_oxid = FC_XID_UNKNOWN;
538                 fc_fcoe_set_mac(mac, fh->fh_d_id);
539                 fip->update_mac(lport, mac);
540                 return 0;
541         default:
542                 if (fip->state != FIP_ST_ENABLED)
543                         goto drop;
544                 return 0;
545         }
546         if (fcoe_ctlr_encaps(fip, lport, op, skb))
547                 goto drop;
548         fip->send(fip, skb);
549         return -EINPROGRESS;
550 drop:
551         kfree_skb(skb);
552         return -EINVAL;
553 }
554 EXPORT_SYMBOL(fcoe_ctlr_els_send);
555
556 /*
557  * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
558  * @fip:        FCoE controller.
559  *
560  * Called with lock held.
561  *
562  * An FCF is considered old if we have missed three advertisements.
563  * That is, there have been no valid advertisement from it for three
564  * times its keep-alive period including fuzz.
565  *
566  * In addition, determine the time when an FCF selection can occur.
567  */
568 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
569 {
570         struct fcoe_fcf *fcf;
571         struct fcoe_fcf *next;
572         unsigned long sel_time = 0;
573
574         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
575                 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
576                                msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
577                         if (fip->sel_fcf == fcf)
578                                 fip->sel_fcf = NULL;
579                         list_del(&fcf->list);
580                         WARN_ON(!fip->fcf_count);
581                         fip->fcf_count--;
582                         kfree(fcf);
583                 } else if (fcoe_ctlr_mtu_valid(fcf) &&
584                            (!sel_time || time_before(sel_time, fcf->time))) {
585                         sel_time = fcf->time;
586                 }
587         }
588         if (sel_time) {
589                 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
590                 fip->sel_time = sel_time;
591                 if (time_before(sel_time, fip->timer.expires))
592                         mod_timer(&fip->timer, sel_time);
593         } else {
594                 fip->sel_time = 0;
595         }
596 }
597
598 /**
599  * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
600  * @skb:        received FIP advertisement frame
601  * @fcf:        resulting FCF entry.
602  *
603  * Returns zero on a valid parsed advertisement,
604  * otherwise returns non zero value.
605  */
606 static int fcoe_ctlr_parse_adv(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("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("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 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:        FCoE controller.
704  * @skb:        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(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("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:        FCoE controller.
799  * @skb:        Received FIP packet.
800  */
801 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
802 {
803         struct fc_lport *lp = 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("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("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) = lp;
890
891         stats = fc_lport_get_stats(lp);
892         stats->RxFrames++;
893         stats->RxWords += skb->len / FIP_BPW;
894
895         fc_exch_recv(lp, fp);
896         return;
897
898 len_err:
899         LIBFCOE_FIP_DBG("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:        FCoE controller.
908  * @fh:         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 *lp = fip->lp;
924         u32     desc_mask;
925
926         LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
927         if (!fcf)
928                 return;
929         if (!fcf || !fc_host_port_id(lp->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(lp)) == 0 &&
966                             get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
967                             ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
968                                 desc_mask &= ~BIT(FIP_DT_VN_ID);
969                         break;
970                 default:
971                         /* standard says ignore unknown descriptors >= 128 */
972                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
973                                 return;
974                         break;
975                 }
976                 desc = (struct fip_desc *)((char *)desc + dlen);
977                 rlen -= dlen;
978         }
979
980         /*
981          * reset only if all required descriptors were present and valid.
982          */
983         if (desc_mask) {
984                 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask);
985         } else {
986                 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
987                 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
988         }
989 }
990
991 /**
992  * fcoe_ctlr_recv() - Receive a FIP frame.
993  * @fip:        FCoE controller.
994  * @skb:        Received FIP packet.
995  *
996  * This is called from NET_RX_SOFTIRQ.
997  */
998 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
999 {
1000         spin_lock_bh(&fip->fip_recv_list.lock);
1001         __skb_queue_tail(&fip->fip_recv_list, skb);
1002         spin_unlock_bh(&fip->fip_recv_list.lock);
1003         schedule_work(&fip->recv_work);
1004 }
1005 EXPORT_SYMBOL(fcoe_ctlr_recv);
1006
1007 /**
1008  * fcoe_ctlr_recv_handler() - Receive a FIP frame.
1009  * @fip:        FCoE controller.
1010  * @skb:        Received FIP packet.
1011  *
1012  * Returns non-zero if the frame is dropped.
1013  */
1014 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1015 {
1016         struct fip_header *fiph;
1017         struct ethhdr *eh;
1018         enum fip_state state;
1019         u16 op;
1020         u8 sub;
1021
1022         if (skb_linearize(skb))
1023                 goto drop;
1024         if (skb->len < sizeof(*fiph))
1025                 goto drop;
1026         eh = eth_hdr(skb);
1027         if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1028             compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1029                 goto drop;
1030         fiph = (struct fip_header *)skb->data;
1031         op = ntohs(fiph->fip_op);
1032         sub = fiph->fip_subcode;
1033
1034         if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1035                 goto drop;
1036         if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1037                 goto drop;
1038
1039         spin_lock_bh(&fip->lock);
1040         state = fip->state;
1041         if (state == FIP_ST_AUTO) {
1042                 fip->map_dest = 0;
1043                 fip->state = FIP_ST_ENABLED;
1044                 state = FIP_ST_ENABLED;
1045                 LIBFCOE_FIP_DBG("Using FIP mode\n");
1046         }
1047         spin_unlock_bh(&fip->lock);
1048         if (state != FIP_ST_ENABLED)
1049                 goto drop;
1050
1051         if (op == FIP_OP_LS) {
1052                 fcoe_ctlr_recv_els(fip, skb);   /* consumes skb */
1053                 return 0;
1054         }
1055         if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1056                 fcoe_ctlr_recv_adv(fip, skb);
1057         else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1058                 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1059         kfree_skb(skb);
1060         return 0;
1061 drop:
1062         kfree_skb(skb);
1063         return -1;
1064 }
1065
1066 /**
1067  * fcoe_ctlr_select() - Select the best FCF, if possible.
1068  * @fip:        FCoE controller.
1069  *
1070  * If there are conflicting advertisements, no FCF can be chosen.
1071  *
1072  * Called with lock held.
1073  */
1074 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1075 {
1076         struct fcoe_fcf *fcf;
1077         struct fcoe_fcf *best = NULL;
1078
1079         list_for_each_entry(fcf, &fip->fcfs, list) {
1080                 LIBFCOE_FIP_DBG("consider FCF for fab %llx VFID %d map %x "
1081                                 "val %d\n", fcf->fabric_name, fcf->vfid,
1082                                 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1083                 if (!fcoe_ctlr_fcf_usable(fcf)) {
1084                         LIBFCOE_FIP_DBG("FCF for fab %llx map %x %svalid "
1085                                         "%savailable\n", fcf->fabric_name,
1086                                         fcf->fc_map, (fcf->flags & FIP_FL_SOL)
1087                                         ? "" : "in", (fcf->flags & FIP_FL_AVAIL)
1088                                         ? "" : "un");
1089                         continue;
1090                 }
1091                 if (!best) {
1092                         best = fcf;
1093                         continue;
1094                 }
1095                 if (fcf->fabric_name != best->fabric_name ||
1096                     fcf->vfid != best->vfid ||
1097                     fcf->fc_map != best->fc_map) {
1098                         LIBFCOE_FIP_DBG("Conflicting fabric, VFID, "
1099                                         "or FC-MAP\n");
1100                         return;
1101                 }
1102                 if (fcf->pri < best->pri)
1103                         best = fcf;
1104         }
1105         fip->sel_fcf = best;
1106 }
1107
1108 /**
1109  * fcoe_ctlr_timeout() - FIP timer function.
1110  * @arg:        &fcoe_ctlr pointer.
1111  *
1112  * Ages FCFs.  Triggers FCF selection if possible.  Sends keep-alives.
1113  */
1114 static void fcoe_ctlr_timeout(unsigned long arg)
1115 {
1116         struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1117         struct fcoe_fcf *sel;
1118         struct fcoe_fcf *fcf;
1119         unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
1120
1121         spin_lock_bh(&fip->lock);
1122         if (fip->state == FIP_ST_DISABLED) {
1123                 spin_unlock_bh(&fip->lock);
1124                 return;
1125         }
1126
1127         fcf = fip->sel_fcf;
1128         fcoe_ctlr_age_fcfs(fip);
1129
1130         sel = fip->sel_fcf;
1131         if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1132                 fcoe_ctlr_select(fip);
1133                 sel = fip->sel_fcf;
1134                 fip->sel_time = 0;
1135         }
1136
1137         if (sel != fcf) {
1138                 fcf = sel;              /* the old FCF may have been freed */
1139                 if (sel) {
1140                         printk(KERN_INFO "libfcoe: host%d: FIP selected "
1141                                "Fibre-Channel Forwarder MAC %pM\n",
1142                                fip->lp->host->host_no, sel->fcf_mac);
1143                         memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1144                         fip->port_ka_time = jiffies +
1145                                             msecs_to_jiffies(FIP_VN_KA_PERIOD);
1146                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1147                         fip->link = 1;
1148                 } else {
1149                         printk(KERN_NOTICE "libfcoe: host%d: "
1150                                "FIP Fibre-Channel Forwarder timed out.  "
1151                                "Starting FCF discovery.\n",
1152                                fip->lp->host->host_no);
1153                         fip->link = 0;
1154                 }
1155                 schedule_work(&fip->link_work);
1156         }
1157
1158         if (sel) {
1159                 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1160                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1161                         fip->send_ctlr_ka = 1;
1162                 }
1163                 if (time_after(next_timer, fip->ctlr_ka_time))
1164                         next_timer = fip->ctlr_ka_time;
1165
1166                 if (time_after_eq(jiffies, fip->port_ka_time)) {
1167                         fip->port_ka_time += jiffies +
1168                                         msecs_to_jiffies(FIP_VN_KA_PERIOD);
1169                         fip->send_port_ka = 1;
1170                 }
1171                 if (time_after(next_timer, fip->port_ka_time))
1172                         next_timer = fip->port_ka_time;
1173                 mod_timer(&fip->timer, next_timer);
1174         } else if (fip->sel_time) {
1175                 next_timer = fip->sel_time +
1176                                 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1177                 mod_timer(&fip->timer, next_timer);
1178         }
1179         if (fip->send_ctlr_ka || fip->send_port_ka)
1180                 schedule_work(&fip->link_work);
1181         spin_unlock_bh(&fip->lock);
1182 }
1183
1184 /**
1185  * fcoe_ctlr_link_work() - worker thread function for link changes.
1186  * @work:       pointer to link_work member inside &fcoe_ctlr.
1187  *
1188  * See if the link status has changed and if so, report it.
1189  *
1190  * This is here because fc_linkup() and fc_linkdown() must not
1191  * be called from the timer directly, since they use a mutex.
1192  */
1193 static void fcoe_ctlr_link_work(struct work_struct *work)
1194 {
1195         struct fcoe_ctlr *fip;
1196         struct fc_lport *vport;
1197         u8 *mac;
1198         int link;
1199         int last_link;
1200
1201         fip = container_of(work, struct fcoe_ctlr, link_work);
1202         spin_lock_bh(&fip->lock);
1203         last_link = fip->last_link;
1204         link = fip->link;
1205         fip->last_link = link;
1206         spin_unlock_bh(&fip->lock);
1207
1208         if (last_link != link) {
1209                 if (link)
1210                         fc_linkup(fip->lp);
1211                 else
1212                         fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1213         }
1214
1215         if (fip->send_ctlr_ka) {
1216                 fip->send_ctlr_ka = 0;
1217                 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1218         }
1219         if (fip->send_port_ka) {
1220                 fip->send_port_ka = 0;
1221                 mutex_lock(&fip->lp->lp_mutex);
1222                 mac = fip->get_src_addr(fip->lp);
1223                 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1224                 list_for_each_entry(vport, &fip->lp->vports, list) {
1225                         mac = fip->get_src_addr(vport);
1226                         fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1227                 }
1228                 mutex_unlock(&fip->lp->lp_mutex);
1229         }
1230 }
1231
1232 /**
1233  * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1234  * @recv_work:  pointer to recv_work member inside &fcoe_ctlr.
1235  */
1236 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1237 {
1238         struct fcoe_ctlr *fip;
1239         struct sk_buff *skb;
1240
1241         fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1242         spin_lock_bh(&fip->fip_recv_list.lock);
1243         while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1244                 spin_unlock_bh(&fip->fip_recv_list.lock);
1245                 fcoe_ctlr_recv_handler(fip, skb);
1246                 spin_lock_bh(&fip->fip_recv_list.lock);
1247         }
1248         spin_unlock_bh(&fip->fip_recv_list.lock);
1249 }
1250
1251 /**
1252  * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1253  * @fip:        FCoE controller.
1254  * @lport:      libfc fc_lport instance received on
1255  * @fp:         FC frame.
1256  * @sa:         Ethernet source MAC address from received FCoE frame.
1257  *
1258  * Snoop potential response to FLOGI or even incoming FLOGI.
1259  *
1260  * The caller has checked that we are waiting for login as indicated
1261  * by fip->flogi_oxid != FC_XID_UNKNOWN.
1262  *
1263  * The caller is responsible for freeing the frame.
1264  *
1265  * Return non-zero if the frame should not be delivered to libfc.
1266  */
1267 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1268                          struct fc_frame *fp, u8 *sa)
1269 {
1270         struct fc_frame_header *fh;
1271         u8 op;
1272         u8 mac[ETH_ALEN];
1273
1274         fh = fc_frame_header_get(fp);
1275         if (fh->fh_type != FC_TYPE_ELS)
1276                 return 0;
1277
1278         op = fc_frame_payload_op(fp);
1279         if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1280             fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1281
1282                 spin_lock_bh(&fip->lock);
1283                 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1284                         spin_unlock_bh(&fip->lock);
1285                         return -EINVAL;
1286                 }
1287                 fip->state = FIP_ST_NON_FIP;
1288                 LIBFCOE_FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
1289
1290                 /*
1291                  * FLOGI accepted.
1292                  * If the src mac addr is FC_OUI-based, then we mark the
1293                  * address_mode flag to use FC_OUI-based Ethernet DA.
1294                  * Otherwise we use the FCoE gateway addr
1295                  */
1296                 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1297                         fip->map_dest = 1;
1298                 } else {
1299                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1300                         fip->map_dest = 0;
1301                 }
1302                 fip->flogi_oxid = FC_XID_UNKNOWN;
1303                 fc_fcoe_set_mac(mac, fh->fh_d_id);
1304                 fip->update_mac(lport, mac);
1305                 spin_unlock_bh(&fip->lock);
1306         } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1307                 /*
1308                  * Save source MAC for point-to-point responses.
1309                  */
1310                 spin_lock_bh(&fip->lock);
1311                 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1312                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1313                         fip->map_dest = 0;
1314                         if (fip->state == FIP_ST_NON_FIP)
1315                                 LIBFCOE_FIP_DBG("received FLOGI REQ, "
1316                                                 "using non-FIP mode\n");
1317                         fip->state = FIP_ST_NON_FIP;
1318                 }
1319                 spin_unlock_bh(&fip->lock);
1320         }
1321         return 0;
1322 }
1323 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1324
1325 /**
1326  * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1327  * @mac: mac address
1328  * @scheme: check port
1329  * @port: port indicator for converting
1330  *
1331  * Returns: u64 fc world wide name
1332  */
1333 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1334                       unsigned int scheme, unsigned int port)
1335 {
1336         u64 wwn;
1337         u64 host_mac;
1338
1339         /* The MAC is in NO, so flip only the low 48 bits */
1340         host_mac = ((u64) mac[0] << 40) |
1341                 ((u64) mac[1] << 32) |
1342                 ((u64) mac[2] << 24) |
1343                 ((u64) mac[3] << 16) |
1344                 ((u64) mac[4] << 8) |
1345                 (u64) mac[5];
1346
1347         WARN_ON(host_mac >= (1ULL << 48));
1348         wwn = host_mac | ((u64) scheme << 60);
1349         switch (scheme) {
1350         case 1:
1351                 WARN_ON(port != 0);
1352                 break;
1353         case 2:
1354                 WARN_ON(port >= 0xfff);
1355                 wwn |= (u64) port << 48;
1356                 break;
1357         default:
1358                 WARN_ON(1);
1359                 break;
1360         }
1361
1362         return wwn;
1363 }
1364 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1365
1366 /**
1367  * fcoe_libfc_config() - sets up libfc related properties for lport
1368  * @lp: ptr to the fc_lport
1369  * @tt: libfc function template
1370  *
1371  * Returns : 0 for success
1372  */
1373 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1374 {
1375         /* Set the function pointers set by the LLDD */
1376         memcpy(&lp->tt, tt, sizeof(*tt));
1377         if (fc_fcp_init(lp))
1378                 return -ENOMEM;
1379         fc_exch_init(lp);
1380         fc_elsct_init(lp);
1381         fc_lport_init(lp);
1382         fc_rport_init(lp);
1383         fc_disc_init(lp);
1384
1385         return 0;
1386 }
1387 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1388