[SCSI] libfcoe: formatting and comment cleanups
[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  * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
83  * @fcf: The FCF to check
84  *
85  * Return non-zero if FCF fcoe_size has been validated.
86  */
87 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
88 {
89         return (fcf->flags & FIP_FL_SOL) != 0;
90 }
91
92 /**
93  * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
94  * @fcf: The FCF to check
95  *
96  * Return non-zero if the FCF is usable.
97  */
98 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
99 {
100         u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
101
102         return (fcf->flags & flags) == flags;
103 }
104
105 /**
106  * fcoe_ctlr_init() - Initialize the FCoE Controller instance
107  * @fip: The FCoE controller to initialize
108  */
109 void fcoe_ctlr_init(struct fcoe_ctlr *fip)
110 {
111         fip->state = FIP_ST_LINK_WAIT;
112         INIT_LIST_HEAD(&fip->fcfs);
113         spin_lock_init(&fip->lock);
114         fip->flogi_oxid = FC_XID_UNKNOWN;
115         setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
116         INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
117         INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
118         skb_queue_head_init(&fip->fip_recv_list);
119 }
120 EXPORT_SYMBOL(fcoe_ctlr_init);
121
122 /**
123  * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
124  * @fip: The FCoE controller whose FCFs are to be reset
125  *
126  * Called with &fcoe_ctlr lock held.
127  */
128 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
129 {
130         struct fcoe_fcf *fcf;
131         struct fcoe_fcf *next;
132
133         fip->sel_fcf = NULL;
134         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
135                 list_del(&fcf->list);
136                 kfree(fcf);
137         }
138         fip->fcf_count = 0;
139         fip->sel_time = 0;
140 }
141
142 /**
143  * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
144  * @fip: The FCoE controller to tear down
145  *
146  * This is called by FCoE drivers before freeing the &fcoe_ctlr.
147  *
148  * The receive handler will have been deleted before this to guarantee
149  * that no more recv_work will be scheduled.
150  *
151  * The timer routine will simply return once we set FIP_ST_DISABLED.
152  * This guarantees that no further timeouts or work will be scheduled.
153  */
154 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
155 {
156         cancel_work_sync(&fip->recv_work);
157         spin_lock_bh(&fip->fip_recv_list.lock);
158         __skb_queue_purge(&fip->fip_recv_list);
159         spin_unlock_bh(&fip->fip_recv_list.lock);
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_ST_AUTO;
265                 fip->last_link = 1;
266                 fip->link = 1;
267                 spin_unlock_bh(&fip->lock);
268                 LIBFCOE_FIP_DBG("%s", "setting AUTO mode.\n");
269                 fc_linkup(fip->lp);
270                 fcoe_ctlr_solicit(fip, NULL);
271         } else
272                 spin_unlock_bh(&fip->lock);
273 }
274 EXPORT_SYMBOL(fcoe_ctlr_link_up);
275
276 /**
277  * fcoe_ctlr_reset() - Reset a FCoE controller
278  * @fip:       The FCoE controller to reset
279  * @new_state: The FIP state to be entered
280  *
281  * Returns non-zero if the link was up and now isn't.
282  */
283 static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
284 {
285         struct fc_lport *lport = fip->lp;
286         int link_dropped;
287
288         spin_lock_bh(&fip->lock);
289         fcoe_ctlr_reset_fcfs(fip);
290         del_timer(&fip->timer);
291         fip->state = new_state;
292         fip->ctlr_ka_time = 0;
293         fip->port_ka_time = 0;
294         fip->sol_time = 0;
295         fip->flogi_oxid = FC_XID_UNKNOWN;
296         fip->map_dest = 0;
297         fip->last_link = 0;
298         link_dropped = fip->link;
299         fip->link = 0;
300         spin_unlock_bh(&fip->lock);
301
302         if (link_dropped)
303                 fc_linkdown(lport);
304
305         if (new_state == FIP_ST_ENABLED) {
306                 fcoe_ctlr_solicit(fip, NULL);
307                 fc_linkup(lport);
308                 link_dropped = 0;
309         }
310         return link_dropped;
311 }
312
313 /**
314  * fcoe_ctlr_link_down() - Stop a FCoE controller
315  * @fip: The FCoE controller to be stopped
316  *
317  * Returns non-zero if the link was up and now isn't.
318  *
319  * Called from the LLD when the network link is not ready.
320  * There may be multiple calls while the link is down.
321  */
322 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
323 {
324         return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
325 }
326 EXPORT_SYMBOL(fcoe_ctlr_link_down);
327
328 /**
329  * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
330  * @fip:   The FCoE controller to send the FKA on
331  * @lport: libfc fc_lport to send from
332  * @ports: 0 for controller keep-alive, 1 for port keep-alive
333  * @sa:    The source MAC address
334  *
335  * A controller keep-alive is sent every fka_period (typically 8 seconds).
336  * The source MAC is the native MAC address.
337  *
338  * A port keep-alive is sent every 90 seconds while logged in.
339  * The source MAC is the assigned mapped source address.
340  * The destination is the FCF's F-port.
341  */
342 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
343                                       struct fc_lport *lport,
344                                       int ports, u8 *sa)
345 {
346         struct sk_buff *skb;
347         struct fip_kal {
348                 struct ethhdr eth;
349                 struct fip_header fip;
350                 struct fip_mac_desc mac;
351         } __attribute__((packed)) *kal;
352         struct fip_vn_desc *vn;
353         u32 len;
354         struct fc_lport *lp;
355         struct fcoe_fcf *fcf;
356
357         fcf = fip->sel_fcf;
358         lp = fip->lp;
359         if (!fcf || !fc_host_port_id(lp->host))
360                 return;
361
362         len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
363         BUG_ON(len < sizeof(*kal) + sizeof(*vn));
364         skb = dev_alloc_skb(len);
365         if (!skb)
366                 return;
367
368         kal = (struct fip_kal *)skb->data;
369         memset(kal, 0, len);
370         memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
371         memcpy(kal->eth.h_source, sa, ETH_ALEN);
372         kal->eth.h_proto = htons(ETH_P_FIP);
373
374         kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
375         kal->fip.fip_op = htons(FIP_OP_CTRL);
376         kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
377         kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
378                                      ports * sizeof(*vn)) / FIP_BPW);
379         kal->fip.fip_flags = htons(FIP_FL_FPMA);
380         if (fip->spma)
381                 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
382
383         kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
384         kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
385         memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
386         if (ports) {
387                 vn = (struct fip_vn_desc *)(kal + 1);
388                 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
389                 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
390                 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
391                 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
392                 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
393         }
394         skb_put(skb, len);
395         skb->protocol = htons(ETH_P_FIP);
396         skb_reset_mac_header(skb);
397         skb_reset_network_header(skb);
398         fip->send(fip, skb);
399 }
400
401 /**
402  * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
403  * @fip:   The FCoE controller for the ELS frame
404  * @dtype: The FIP descriptor type for the frame
405  * @skb:   The FCoE ELS frame including FC header but no FCoE headers
406  *
407  * Returns non-zero error code on failure.
408  *
409  * The caller must check that the length is a multiple of 4.
410  *
411  * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
412  * Headroom includes the FIP encapsulation description, FIP header, and
413  * Ethernet header.  The tailroom is for the FIP MAC descriptor.
414  */
415 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
416                             u8 dtype, struct sk_buff *skb)
417 {
418         struct fip_encaps_head {
419                 struct ethhdr eth;
420                 struct fip_header fip;
421                 struct fip_encaps encaps;
422         } __attribute__((packed)) *cap;
423         struct fip_mac_desc *mac;
424         struct fcoe_fcf *fcf;
425         size_t dlen;
426         u16 fip_flags;
427
428         fcf = fip->sel_fcf;
429         if (!fcf)
430                 return -ENODEV;
431
432         /* set flags according to both FCF and lport's capability on SPMA */
433         fip_flags = fcf->flags;
434         fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
435         if (!fip_flags)
436                 return -ENODEV;
437
438         dlen = sizeof(struct fip_encaps) + skb->len;    /* len before push */
439         cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
440
441         memset(cap, 0, sizeof(*cap));
442         memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
443         memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
444         cap->eth.h_proto = htons(ETH_P_FIP);
445
446         cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
447         cap->fip.fip_op = htons(FIP_OP_LS);
448         cap->fip.fip_subcode = FIP_SC_REQ;
449         cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
450         cap->fip.fip_flags = htons(fip_flags);
451
452         cap->encaps.fd_desc.fip_dtype = dtype;
453         cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
454
455         mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
456         memset(mac, 0, sizeof(mac));
457         mac->fd_desc.fip_dtype = FIP_DT_MAC;
458         mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
459         if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC)
460                 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
461         else if (fip->spma)
462                 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
463
464         skb->protocol = htons(ETH_P_FIP);
465         skb_reset_mac_header(skb);
466         skb_reset_network_header(skb);
467         return 0;
468 }
469
470 /**
471  * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
472  * @fip:        FCoE controller.
473  * @lport:      libfc fc_lport to send from
474  * @skb:        FCoE ELS frame including FC header but no FCoE headers.
475  *
476  * Returns a non-zero error code if the frame should not be sent.
477  * Returns zero if the caller should send the frame with FCoE encapsulation.
478  *
479  * The caller must check that the length is a multiple of 4.
480  * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
481  */
482 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
483                        struct sk_buff *skb)
484 {
485         struct fc_frame_header *fh;
486         u16 old_xid;
487         u8 op;
488         u8 mac[ETH_ALEN];
489
490         fh = (struct fc_frame_header *)skb->data;
491         op = *(u8 *)(fh + 1);
492
493         if (op == ELS_FLOGI) {
494                 old_xid = fip->flogi_oxid;
495                 fip->flogi_oxid = ntohs(fh->fh_ox_id);
496                 if (fip->state == FIP_ST_AUTO) {
497                         if (old_xid == FC_XID_UNKNOWN)
498                                 fip->flogi_count = 0;
499                         fip->flogi_count++;
500                         if (fip->flogi_count < 3)
501                                 goto drop;
502                         fip->map_dest = 1;
503                         return 0;
504                 }
505                 if (fip->state == FIP_ST_NON_FIP)
506                         fip->map_dest = 1;
507         }
508
509         if (fip->state == FIP_ST_NON_FIP)
510                 return 0;
511
512         switch (op) {
513         case ELS_FLOGI:
514                 op = FIP_DT_FLOGI;
515                 break;
516         case ELS_FDISC:
517                 if (ntoh24(fh->fh_s_id))
518                         return 0;
519                 op = FIP_DT_FDISC;
520                 break;
521         case ELS_LOGO:
522                 if (fip->state != FIP_ST_ENABLED)
523                         return 0;
524                 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
525                         return 0;
526                 op = FIP_DT_LOGO;
527                 break;
528         case ELS_LS_ACC:
529                 if (fip->flogi_oxid == FC_XID_UNKNOWN)
530                         return 0;
531                 if (!ntoh24(fh->fh_s_id))
532                         return 0;
533                 if (fip->state == FIP_ST_AUTO)
534                         return 0;
535                 /*
536                  * Here we must've gotten an SID by accepting an FLOGI
537                  * from a point-to-point connection.  Switch to using
538                  * the source mac based on the SID.  The destination
539                  * MAC in this case would have been set by receving the
540                  * FLOGI.
541                  */
542                 fip->flogi_oxid = FC_XID_UNKNOWN;
543                 fc_fcoe_set_mac(mac, fh->fh_d_id);
544                 fip->update_mac(lport, mac);
545                 return 0;
546         default:
547                 if (fip->state != FIP_ST_ENABLED)
548                         goto drop;
549                 return 0;
550         }
551         if (fcoe_ctlr_encaps(fip, lport, op, skb))
552                 goto drop;
553         fip->send(fip, skb);
554         return -EINPROGRESS;
555 drop:
556         kfree_skb(skb);
557         return -EINVAL;
558 }
559 EXPORT_SYMBOL(fcoe_ctlr_els_send);
560
561 /**
562  * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
563  * @fip: The FCoE controller to free FCFs on
564  *
565  * Called with lock held.
566  *
567  * An FCF is considered old if we have missed three advertisements.
568  * That is, there have been no valid advertisement from it for three
569  * times its keep-alive period including fuzz.
570  *
571  * In addition, determine the time when an FCF selection can occur.
572  */
573 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
574 {
575         struct fcoe_fcf *fcf;
576         struct fcoe_fcf *next;
577         unsigned long sel_time = 0;
578
579         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
580                 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
581                                msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
582                         if (fip->sel_fcf == fcf)
583                                 fip->sel_fcf = NULL;
584                         list_del(&fcf->list);
585                         WARN_ON(!fip->fcf_count);
586                         fip->fcf_count--;
587                         kfree(fcf);
588                 } else if (fcoe_ctlr_mtu_valid(fcf) &&
589                            (!sel_time || time_before(sel_time, fcf->time))) {
590                         sel_time = fcf->time;
591                 }
592         }
593         if (sel_time) {
594                 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
595                 fip->sel_time = sel_time;
596                 if (time_before(sel_time, fip->timer.expires))
597                         mod_timer(&fip->timer, sel_time);
598         } else {
599                 fip->sel_time = 0;
600         }
601 }
602
603 /**
604  * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
605  * @skb: The received FIP advertisement frame
606  * @fcf: The resulting FCF entry
607  *
608  * Returns zero on a valid parsed advertisement,
609  * otherwise returns non zero value.
610  */
611 static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
612 {
613         struct fip_header *fiph;
614         struct fip_desc *desc = NULL;
615         struct fip_wwn_desc *wwn;
616         struct fip_fab_desc *fab;
617         struct fip_fka_desc *fka;
618         unsigned long t;
619         size_t rlen;
620         size_t dlen;
621
622         memset(fcf, 0, sizeof(*fcf));
623         fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
624
625         fiph = (struct fip_header *)skb->data;
626         fcf->flags = ntohs(fiph->fip_flags);
627
628         rlen = ntohs(fiph->fip_dl_len) * 4;
629         if (rlen + sizeof(*fiph) > skb->len)
630                 return -EINVAL;
631
632         desc = (struct fip_desc *)(fiph + 1);
633         while (rlen > 0) {
634                 dlen = desc->fip_dlen * FIP_BPW;
635                 if (dlen < sizeof(*desc) || dlen > rlen)
636                         return -EINVAL;
637                 switch (desc->fip_dtype) {
638                 case FIP_DT_PRI:
639                         if (dlen != sizeof(struct fip_pri_desc))
640                                 goto len_err;
641                         fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
642                         break;
643                 case FIP_DT_MAC:
644                         if (dlen != sizeof(struct fip_mac_desc))
645                                 goto len_err;
646                         memcpy(fcf->fcf_mac,
647                                ((struct fip_mac_desc *)desc)->fd_mac,
648                                ETH_ALEN);
649                         if (!is_valid_ether_addr(fcf->fcf_mac)) {
650                                 LIBFCOE_FIP_DBG("Invalid MAC address "
651                                                 "in FIP adv\n");
652                                 return -EINVAL;
653                         }
654                         break;
655                 case FIP_DT_NAME:
656                         if (dlen != sizeof(struct fip_wwn_desc))
657                                 goto len_err;
658                         wwn = (struct fip_wwn_desc *)desc;
659                         fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
660                         break;
661                 case FIP_DT_FAB:
662                         if (dlen != sizeof(struct fip_fab_desc))
663                                 goto len_err;
664                         fab = (struct fip_fab_desc *)desc;
665                         fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
666                         fcf->vfid = ntohs(fab->fd_vfid);
667                         fcf->fc_map = ntoh24(fab->fd_map);
668                         break;
669                 case FIP_DT_FKA:
670                         if (dlen != sizeof(struct fip_fka_desc))
671                                 goto len_err;
672                         fka = (struct fip_fka_desc *)desc;
673                         t = ntohl(fka->fd_fka_period);
674                         if (t >= FCOE_CTLR_MIN_FKA)
675                                 fcf->fka_period = msecs_to_jiffies(t);
676                         break;
677                 case FIP_DT_MAP_OUI:
678                 case FIP_DT_FCOE_SIZE:
679                 case FIP_DT_FLOGI:
680                 case FIP_DT_FDISC:
681                 case FIP_DT_LOGO:
682                 case FIP_DT_ELP:
683                 default:
684                         LIBFCOE_FIP_DBG("unexpected descriptor type %x "
685                                         "in FIP adv\n", desc->fip_dtype);
686                         /* standard says ignore unknown descriptors >= 128 */
687                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
688                                 return -EINVAL;
689                         continue;
690                 }
691                 desc = (struct fip_desc *)((char *)desc + dlen);
692                 rlen -= dlen;
693         }
694         if (!fcf->fc_map || (fcf->fc_map & 0x10000))
695                 return -EINVAL;
696         if (!fcf->switch_name || !fcf->fabric_name)
697                 return -EINVAL;
698         return 0;
699
700 len_err:
701         LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
702                         desc->fip_dtype, dlen);
703         return -EINVAL;
704 }
705
706 /**
707  * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
708  * @fip: The FCoE controller receiving the advertisement
709  * @skb: The received FIP packet
710  */
711 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
712 {
713         struct fcoe_fcf *fcf;
714         struct fcoe_fcf new;
715         struct fcoe_fcf *found;
716         unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
717         int first = 0;
718         int mtu_valid;
719
720         if (fcoe_ctlr_parse_adv(skb, &new))
721                 return;
722
723         spin_lock_bh(&fip->lock);
724         first = list_empty(&fip->fcfs);
725         found = NULL;
726         list_for_each_entry(fcf, &fip->fcfs, list) {
727                 if (fcf->switch_name == new.switch_name &&
728                     fcf->fabric_name == new.fabric_name &&
729                     fcf->fc_map == new.fc_map &&
730                     compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
731                         found = fcf;
732                         break;
733                 }
734         }
735         if (!found) {
736                 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
737                         goto out;
738
739                 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
740                 if (!fcf)
741                         goto out;
742
743                 fip->fcf_count++;
744                 memcpy(fcf, &new, sizeof(new));
745                 list_add(&fcf->list, &fip->fcfs);
746         } else {
747                 /*
748                  * Flags in advertisements are ignored once the FCF is
749                  * selected.  Flags in unsolicited advertisements are
750                  * ignored after a usable solicited advertisement
751                  * has been received.
752                  */
753                 if (fcf == fip->sel_fcf) {
754                         fip->ctlr_ka_time -= fcf->fka_period;
755                         fip->ctlr_ka_time += new.fka_period;
756                         if (time_before(fip->ctlr_ka_time, fip->timer.expires))
757                                 mod_timer(&fip->timer, fip->ctlr_ka_time);
758                 } else if (!fcoe_ctlr_fcf_usable(fcf))
759                         fcf->flags = new.flags;
760                 fcf->fka_period = new.fka_period;
761                 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
762         }
763         mtu_valid = fcoe_ctlr_mtu_valid(fcf);
764         fcf->time = jiffies;
765         if (!found) {
766                 LIBFCOE_FIP_DBG("New FCF for fab %llx map %x val %d\n",
767                                 fcf->fabric_name, fcf->fc_map, mtu_valid);
768         }
769
770         /*
771          * If this advertisement is not solicited and our max receive size
772          * hasn't been verified, send a solicited advertisement.
773          */
774         if (!mtu_valid)
775                 fcoe_ctlr_solicit(fip, fcf);
776
777         /*
778          * If its been a while since we did a solicit, and this is
779          * the first advertisement we've received, do a multicast
780          * solicitation to gather as many advertisements as we can
781          * before selection occurs.
782          */
783         if (first && time_after(jiffies, fip->sol_time + sol_tov))
784                 fcoe_ctlr_solicit(fip, NULL);
785
786         /*
787          * If this is the first validated FCF, note the time and
788          * set a timer to trigger selection.
789          */
790         if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
791                 fip->sel_time = jiffies +
792                         msecs_to_jiffies(FCOE_CTLR_START_DELAY);
793                 if (!timer_pending(&fip->timer) ||
794                     time_before(fip->sel_time, fip->timer.expires))
795                         mod_timer(&fip->timer, fip->sel_time);
796         }
797 out:
798         spin_unlock_bh(&fip->lock);
799 }
800
801 /**
802  * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
803  * @fip: The FCoE controller which received the packet
804  * @skb: The received FIP packet
805  */
806 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
807 {
808         struct fc_lport *lport = fip->lp;
809         struct fip_header *fiph;
810         struct fc_frame *fp = (struct fc_frame *)skb;
811         struct fc_frame_header *fh = NULL;
812         struct fip_desc *desc;
813         struct fip_encaps *els;
814         struct fcoe_dev_stats *stats;
815         enum fip_desc_type els_dtype = 0;
816         u8 els_op;
817         u8 sub;
818         u8 granted_mac[ETH_ALEN] = { 0 };
819         size_t els_len = 0;
820         size_t rlen;
821         size_t dlen;
822
823         fiph = (struct fip_header *)skb->data;
824         sub = fiph->fip_subcode;
825         if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
826                 goto drop;
827
828         rlen = ntohs(fiph->fip_dl_len) * 4;
829         if (rlen + sizeof(*fiph) > skb->len)
830                 goto drop;
831
832         desc = (struct fip_desc *)(fiph + 1);
833         while (rlen > 0) {
834                 dlen = desc->fip_dlen * FIP_BPW;
835                 if (dlen < sizeof(*desc) || dlen > rlen)
836                         goto drop;
837                 switch (desc->fip_dtype) {
838                 case FIP_DT_MAC:
839                         if (dlen != sizeof(struct fip_mac_desc))
840                                 goto len_err;
841                         memcpy(granted_mac,
842                                ((struct fip_mac_desc *)desc)->fd_mac,
843                                ETH_ALEN);
844                         if (!is_valid_ether_addr(granted_mac)) {
845                                 LIBFCOE_FIP_DBG("Invalid MAC address "
846                                                 "in FIP ELS\n");
847                                 goto drop;
848                         }
849                         memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
850                         break;
851                 case FIP_DT_FLOGI:
852                 case FIP_DT_FDISC:
853                 case FIP_DT_LOGO:
854                 case FIP_DT_ELP:
855                         if (fh)
856                                 goto drop;
857                         if (dlen < sizeof(*els) + sizeof(*fh) + 1)
858                                 goto len_err;
859                         els_len = dlen - sizeof(*els);
860                         els = (struct fip_encaps *)desc;
861                         fh = (struct fc_frame_header *)(els + 1);
862                         els_dtype = desc->fip_dtype;
863                         break;
864                 default:
865                         LIBFCOE_FIP_DBG("unexpected descriptor type %x "
866                                         "in FIP adv\n", desc->fip_dtype);
867                         /* standard says ignore unknown descriptors >= 128 */
868                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
869                                 goto drop;
870                         continue;
871                 }
872                 desc = (struct fip_desc *)((char *)desc + dlen);
873                 rlen -= dlen;
874         }
875
876         if (!fh)
877                 goto drop;
878         els_op = *(u8 *)(fh + 1);
879
880         if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
881             fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
882             els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac))
883                 fip->flogi_oxid = FC_XID_UNKNOWN;
884
885         /*
886          * Convert skb into an fc_frame containing only the ELS.
887          */
888         skb_pull(skb, (u8 *)fh - skb->data);
889         skb_trim(skb, els_len);
890         fp = (struct fc_frame *)skb;
891         fc_frame_init(fp);
892         fr_sof(fp) = FC_SOF_I3;
893         fr_eof(fp) = FC_EOF_T;
894         fr_dev(fp) = lport;
895
896         stats = fc_lport_get_stats(lport);
897         stats->RxFrames++;
898         stats->RxWords += skb->len / FIP_BPW;
899
900         fc_exch_recv(lport, fp);
901         return;
902
903 len_err:
904         LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
905                         desc->fip_dtype, dlen);
906 drop:
907         kfree_skb(skb);
908 }
909
910 /**
911  * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
912  * @fip: The FCoE controller that received the frame
913  * @fh:  The received FIP header
914  *
915  * There may be multiple VN_Port descriptors.
916  * The overall length has already been checked.
917  */
918 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
919                                      struct fip_header *fh)
920 {
921         struct fip_desc *desc;
922         struct fip_mac_desc *mp;
923         struct fip_wwn_desc *wp;
924         struct fip_vn_desc *vp;
925         size_t rlen;
926         size_t dlen;
927         struct fcoe_fcf *fcf = fip->sel_fcf;
928         struct fc_lport *lport = fip->lp;
929         u32     desc_mask;
930
931         LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
932         if (!fcf)
933                 return;
934         if (!fcf || !fc_host_port_id(lport->host))
935                 return;
936
937         /*
938          * mask of required descriptors.  Validating each one clears its bit.
939          */
940         desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
941
942         rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
943         desc = (struct fip_desc *)(fh + 1);
944         while (rlen >= sizeof(*desc)) {
945                 dlen = desc->fip_dlen * FIP_BPW;
946                 if (dlen > rlen)
947                         return;
948                 switch (desc->fip_dtype) {
949                 case FIP_DT_MAC:
950                         mp = (struct fip_mac_desc *)desc;
951                         if (dlen < sizeof(*mp))
952                                 return;
953                         if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
954                                 return;
955                         desc_mask &= ~BIT(FIP_DT_MAC);
956                         break;
957                 case FIP_DT_NAME:
958                         wp = (struct fip_wwn_desc *)desc;
959                         if (dlen < sizeof(*wp))
960                                 return;
961                         if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
962                                 return;
963                         desc_mask &= ~BIT(FIP_DT_NAME);
964                         break;
965                 case FIP_DT_VN_ID:
966                         vp = (struct fip_vn_desc *)desc;
967                         if (dlen < sizeof(*vp))
968                                 return;
969                         if (compare_ether_addr(vp->fd_mac,
970                                                fip->get_src_addr(lport)) == 0 &&
971                             get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
972                             ntoh24(vp->fd_fc_id) ==
973                             fc_host_port_id(lport->host))
974                                 desc_mask &= ~BIT(FIP_DT_VN_ID);
975                         break;
976                 default:
977                         /* standard says ignore unknown descriptors >= 128 */
978                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
979                                 return;
980                         break;
981                 }
982                 desc = (struct fip_desc *)((char *)desc + dlen);
983                 rlen -= dlen;
984         }
985
986         /*
987          * reset only if all required descriptors were present and valid.
988          */
989         if (desc_mask) {
990                 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask);
991         } else {
992                 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
993                 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
994         }
995 }
996
997 /**
998  * fcoe_ctlr_recv() - Receive a FIP packet
999  * @fip: The FCoE controller that received the packet
1000  * @skb: The received FIP packet
1001  *
1002  * This is called from NET_RX_SOFTIRQ.
1003  */
1004 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1005 {
1006         spin_lock_bh(&fip->fip_recv_list.lock);
1007         __skb_queue_tail(&fip->fip_recv_list, skb);
1008         spin_unlock_bh(&fip->fip_recv_list.lock);
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("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("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("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("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                         fip->link = 1;
1154                 } else {
1155                         printk(KERN_NOTICE "libfcoe: host%d: "
1156                                "FIP Fibre-Channel Forwarder timed out.  "
1157                                "Starting FCF discovery.\n",
1158                                fip->lp->host->host_no);
1159                         fip->link = 0;
1160                 }
1161                 schedule_work(&fip->link_work);
1162         }
1163
1164         if (sel) {
1165                 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1166                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1167                         fip->send_ctlr_ka = 1;
1168                 }
1169                 if (time_after(next_timer, fip->ctlr_ka_time))
1170                         next_timer = fip->ctlr_ka_time;
1171
1172                 if (time_after_eq(jiffies, fip->port_ka_time)) {
1173                         fip->port_ka_time += jiffies +
1174                                 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1175                         fip->send_port_ka = 1;
1176                 }
1177                 if (time_after(next_timer, fip->port_ka_time))
1178                         next_timer = fip->port_ka_time;
1179                 mod_timer(&fip->timer, next_timer);
1180         } else if (fip->sel_time) {
1181                 next_timer = fip->sel_time +
1182                         msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1183                 mod_timer(&fip->timer, next_timer);
1184         }
1185         if (fip->send_ctlr_ka || fip->send_port_ka)
1186                 schedule_work(&fip->link_work);
1187         spin_unlock_bh(&fip->lock);
1188 }
1189
1190 /**
1191  * fcoe_ctlr_link_work() - Worker thread function for link changes
1192  * @work: Handle to a FCoE controller
1193  *
1194  * See if the link status has changed and if so, report it.
1195  *
1196  * This is here because fc_linkup() and fc_linkdown() must not
1197  * be called from the timer directly, since they use a mutex.
1198  */
1199 static void fcoe_ctlr_link_work(struct work_struct *work)
1200 {
1201         struct fcoe_ctlr *fip;
1202         struct fc_lport *vport;
1203         u8 *mac;
1204         int link;
1205         int last_link;
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         spin_unlock_bh(&fip->lock);
1213
1214         if (last_link != link) {
1215                 if (link)
1216                         fc_linkup(fip->lp);
1217                 else
1218                         fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1219         }
1220
1221         if (fip->send_ctlr_ka) {
1222                 fip->send_ctlr_ka = 0;
1223                 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1224         }
1225         if (fip->send_port_ka) {
1226                 fip->send_port_ka = 0;
1227                 mutex_lock(&fip->lp->lp_mutex);
1228                 mac = fip->get_src_addr(fip->lp);
1229                 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1230                 list_for_each_entry(vport, &fip->lp->vports, list) {
1231                         mac = fip->get_src_addr(vport);
1232                         fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1233                 }
1234                 mutex_unlock(&fip->lp->lp_mutex);
1235         }
1236 }
1237
1238 /**
1239  * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1240  * @recv_work: Handle to a FCoE controller
1241  */
1242 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1243 {
1244         struct fcoe_ctlr *fip;
1245         struct sk_buff *skb;
1246
1247         fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1248         spin_lock_bh(&fip->fip_recv_list.lock);
1249         while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1250                 spin_unlock_bh(&fip->fip_recv_list.lock);
1251                 fcoe_ctlr_recv_handler(fip, skb);
1252                 spin_lock_bh(&fip->fip_recv_list.lock);
1253         }
1254         spin_unlock_bh(&fip->fip_recv_list.lock);
1255 }
1256
1257 /**
1258  * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response or request
1259  * @fip: The FCoE controller
1260  * @fp:  The FC frame to snoop
1261  * @sa:  Ethernet source MAC address from received FCoE frame
1262  *
1263  * Snoop potential response to FLOGI or even incoming FLOGI.
1264  *
1265  * The caller has checked that we are waiting for login as indicated
1266  * by fip->flogi_oxid != FC_XID_UNKNOWN.
1267  *
1268  * The caller is responsible for freeing the frame.
1269  *
1270  * Return non-zero if the frame should not be delivered to libfc.
1271  */
1272 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1273                          struct fc_frame *fp, u8 *sa)
1274 {
1275         struct fc_frame_header *fh;
1276         u8 op;
1277         u8 mac[ETH_ALEN];
1278
1279         fh = fc_frame_header_get(fp);
1280         if (fh->fh_type != FC_TYPE_ELS)
1281                 return 0;
1282
1283         op = fc_frame_payload_op(fp);
1284         if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1285             fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1286
1287                 spin_lock_bh(&fip->lock);
1288                 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1289                         spin_unlock_bh(&fip->lock);
1290                         return -EINVAL;
1291                 }
1292                 fip->state = FIP_ST_NON_FIP;
1293                 LIBFCOE_FIP_DBG("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                 fc_fcoe_set_mac(mac, fh->fh_d_id);
1309                 fip->update_mac(lport, mac);
1310                 spin_unlock_bh(&fip->lock);
1311         } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1312                 /*
1313                  * Save source MAC for point-to-point responses.
1314                  */
1315                 spin_lock_bh(&fip->lock);
1316                 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1317                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1318                         fip->map_dest = 0;
1319                         if (fip->state == FIP_ST_NON_FIP)
1320                                 LIBFCOE_FIP_DBG("received FLOGI REQ, "
1321                                                 "using non-FIP mode\n");
1322                         fip->state = FIP_ST_NON_FIP;
1323                 }
1324                 spin_unlock_bh(&fip->lock);
1325         }
1326         return 0;
1327 }
1328 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1329
1330 /**
1331  * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1332  * @mac:    The MAC address to convert
1333  * @scheme: The scheme to use when converting
1334  * @port:   The port indicator for converting
1335  *
1336  * Returns: u64 fc world wide name
1337  */
1338 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1339                       unsigned int scheme, unsigned int port)
1340 {
1341         u64 wwn;
1342         u64 host_mac;
1343
1344         /* The MAC is in NO, so flip only the low 48 bits */
1345         host_mac = ((u64) mac[0] << 40) |
1346                 ((u64) mac[1] << 32) |
1347                 ((u64) mac[2] << 24) |
1348                 ((u64) mac[3] << 16) |
1349                 ((u64) mac[4] << 8) |
1350                 (u64) mac[5];
1351
1352         WARN_ON(host_mac >= (1ULL << 48));
1353         wwn = host_mac | ((u64) scheme << 60);
1354         switch (scheme) {
1355         case 1:
1356                 WARN_ON(port != 0);
1357                 break;
1358         case 2:
1359                 WARN_ON(port >= 0xfff);
1360                 wwn |= (u64) port << 48;
1361                 break;
1362         default:
1363                 WARN_ON(1);
1364                 break;
1365         }
1366
1367         return wwn;
1368 }
1369 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1370
1371 /**
1372  * fcoe_libfc_config() - Sets up libfc related properties for local port
1373  * @lp: The local port to configure libfc for
1374  * @tt: The libfc function template
1375  *
1376  * Returns : 0 for success
1377  */
1378 int fcoe_libfc_config(struct fc_lport *lport,
1379                       struct libfc_function_template *tt)
1380 {
1381         /* Set the function pointers set by the LLDD */
1382         memcpy(&lport->tt, tt, sizeof(*tt));
1383         if (fc_fcp_init(lport))
1384                 return -ENOMEM;
1385         fc_exch_init(lport);
1386         fc_elsct_init(lport);
1387         fc_lport_init(lport);
1388         fc_rport_init(lport);
1389         fc_disc_init(lport);
1390
1391         return 0;
1392 }
1393 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1394