Merge git://git.infradead.org/users/dwmw2/libraid-2.6 into for-linus
[pandora-kernel.git] / drivers / scsi / fcoe / libfcoe.c
1 /*
2  * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
3  * Copyright (c) 2009 Intel Corporation.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Maintained at www.Open-FCoE.org
19  */
20
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/timer.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/errno.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.h>
35 #include <net/rtnetlink.h>
36
37 #include <scsi/fc/fc_els.h>
38 #include <scsi/fc/fc_fs.h>
39 #include <scsi/fc/fc_fip.h>
40 #include <scsi/fc/fc_encaps.h>
41 #include <scsi/fc/fc_fcoe.h>
42 #include <scsi/fc/fc_fcp.h>
43
44 #include <scsi/libfc.h>
45 #include <scsi/libfcoe.h>
46
47 MODULE_AUTHOR("Open-FCoE.org");
48 MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
49 MODULE_LICENSE("GPL v2");
50
51 #define FCOE_CTLR_MIN_FKA       500             /* min keep alive (mS) */
52 #define FCOE_CTLR_DEF_FKA       FIP_DEF_FKA     /* default keep alive (mS) */
53
54 static void fcoe_ctlr_timeout(unsigned long);
55 static void fcoe_ctlr_timer_work(struct work_struct *);
56 static void fcoe_ctlr_recv_work(struct work_struct *);
57
58 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
59 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
60 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
61 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
62
63 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
64 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
65 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
66 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
67
68 unsigned int libfcoe_debug_logging;
69 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
70 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
71
72 #define LIBFCOE_LOGGING     0x01 /* General logging, not categorized */
73 #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
74
75 #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD)               \
76 do {                                                    \
77         if (unlikely(libfcoe_debug_logging & LEVEL))    \
78                 do {                                    \
79                         CMD;                            \
80                 } while (0);                            \
81 } while (0)
82
83 #define LIBFCOE_DBG(fmt, args...)                                       \
84         LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING,                          \
85                               printk(KERN_INFO "libfcoe: " fmt, ##args);)
86
87 #define LIBFCOE_FIP_DBG(fip, fmt, args...)                              \
88         LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING,                      \
89                               printk(KERN_INFO "host%d: fip: " fmt,     \
90                                      (fip)->lp->host->host_no, ##args);)
91
92 static const char *fcoe_ctlr_states[] = {
93         [FIP_ST_DISABLED] =     "DISABLED",
94         [FIP_ST_LINK_WAIT] =    "LINK_WAIT",
95         [FIP_ST_AUTO] =         "AUTO",
96         [FIP_ST_NON_FIP] =      "NON_FIP",
97         [FIP_ST_ENABLED] =      "ENABLED",
98         [FIP_ST_VNMP_START] =   "VNMP_START",
99         [FIP_ST_VNMP_PROBE1] =  "VNMP_PROBE1",
100         [FIP_ST_VNMP_PROBE2] =  "VNMP_PROBE2",
101         [FIP_ST_VNMP_CLAIM] =   "VNMP_CLAIM",
102         [FIP_ST_VNMP_UP] =      "VNMP_UP",
103 };
104
105 static const char *fcoe_ctlr_state(enum fip_state state)
106 {
107         const char *cp = "unknown";
108
109         if (state < ARRAY_SIZE(fcoe_ctlr_states))
110                 cp = fcoe_ctlr_states[state];
111         if (!cp)
112                 cp = "unknown";
113         return cp;
114 }
115
116 /**
117  * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
118  * @fip: The FCoE controller
119  * @state: The new state
120  */
121 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
122 {
123         if (state == fip->state)
124                 return;
125         if (fip->lp)
126                 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
127                         fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
128         fip->state = state;
129 }
130
131 /**
132  * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
133  * @fcf: The FCF to check
134  *
135  * Return non-zero if FCF fcoe_size has been validated.
136  */
137 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
138 {
139         return (fcf->flags & FIP_FL_SOL) != 0;
140 }
141
142 /**
143  * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
144  * @fcf: The FCF to check
145  *
146  * Return non-zero if the FCF is usable.
147  */
148 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
149 {
150         u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
151
152         return (fcf->flags & flags) == flags;
153 }
154
155 /**
156  * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
157  * @fip: The FCoE controller
158  */
159 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
160 {
161         if (fip->mode == FIP_MODE_VN2VN)
162                 hton24(fip->dest_addr, FIP_VN_FC_MAP);
163         else
164                 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
165         hton24(fip->dest_addr + 3, 0);
166         fip->map_dest = 1;
167 }
168
169 /**
170  * fcoe_ctlr_init() - Initialize the FCoE Controller instance
171  * @fip: The FCoE controller to initialize
172  */
173 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
174 {
175         fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
176         fip->mode = mode;
177         INIT_LIST_HEAD(&fip->fcfs);
178         mutex_init(&fip->ctlr_mutex);
179         fip->flogi_oxid = FC_XID_UNKNOWN;
180         setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
181         INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
182         INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
183         skb_queue_head_init(&fip->fip_recv_list);
184 }
185 EXPORT_SYMBOL(fcoe_ctlr_init);
186
187 /**
188  * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
189  * @fip: The FCoE controller whose FCFs are to be reset
190  *
191  * Called with &fcoe_ctlr lock held.
192  */
193 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
194 {
195         struct fcoe_fcf *fcf;
196         struct fcoe_fcf *next;
197
198         fip->sel_fcf = NULL;
199         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
200                 list_del(&fcf->list);
201                 kfree(fcf);
202         }
203         fip->fcf_count = 0;
204         fip->sel_time = 0;
205 }
206
207 /**
208  * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
209  * @fip: The FCoE controller to tear down
210  *
211  * This is called by FCoE drivers before freeing the &fcoe_ctlr.
212  *
213  * The receive handler will have been deleted before this to guarantee
214  * that no more recv_work will be scheduled.
215  *
216  * The timer routine will simply return once we set FIP_ST_DISABLED.
217  * This guarantees that no further timeouts or work will be scheduled.
218  */
219 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
220 {
221         cancel_work_sync(&fip->recv_work);
222         skb_queue_purge(&fip->fip_recv_list);
223
224         mutex_lock(&fip->ctlr_mutex);
225         fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
226         fcoe_ctlr_reset_fcfs(fip);
227         mutex_unlock(&fip->ctlr_mutex);
228         del_timer_sync(&fip->timer);
229         cancel_work_sync(&fip->timer_work);
230 }
231 EXPORT_SYMBOL(fcoe_ctlr_destroy);
232
233 /**
234  * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
235  * @fip: The FCoE controller to get the maximum FCoE size from
236  *
237  * Returns the maximum packet size including the FCoE header and trailer,
238  * but not including any Ethernet or VLAN headers.
239  */
240 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
241 {
242         /*
243          * Determine the max FCoE frame size allowed, including
244          * FCoE header and trailer.
245          * Note:  lp->mfs is currently the payload size, not the frame size.
246          */
247         return fip->lp->mfs + sizeof(struct fc_frame_header) +
248                 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
249 }
250
251 /**
252  * fcoe_ctlr_solicit() - Send a FIP solicitation
253  * @fip: The FCoE controller to send the solicitation on
254  * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
255  */
256 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
257 {
258         struct sk_buff *skb;
259         struct fip_sol {
260                 struct ethhdr eth;
261                 struct fip_header fip;
262                 struct {
263                         struct fip_mac_desc mac;
264                         struct fip_wwn_desc wwnn;
265                         struct fip_size_desc size;
266                 } __attribute__((packed)) desc;
267         }  __attribute__((packed)) *sol;
268         u32 fcoe_size;
269
270         skb = dev_alloc_skb(sizeof(*sol));
271         if (!skb)
272                 return;
273
274         sol = (struct fip_sol *)skb->data;
275
276         memset(sol, 0, sizeof(*sol));
277         memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
278         memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
279         sol->eth.h_proto = htons(ETH_P_FIP);
280
281         sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
282         sol->fip.fip_op = htons(FIP_OP_DISC);
283         sol->fip.fip_subcode = FIP_SC_SOL;
284         sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
285         sol->fip.fip_flags = htons(FIP_FL_FPMA);
286         if (fip->spma)
287                 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
288
289         sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
290         sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
291         memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
292
293         sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
294         sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
295         put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
296
297         fcoe_size = fcoe_ctlr_fcoe_size(fip);
298         sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
299         sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
300         sol->desc.size.fd_size = htons(fcoe_size);
301
302         skb_put(skb, sizeof(*sol));
303         skb->protocol = htons(ETH_P_FIP);
304         skb_reset_mac_header(skb);
305         skb_reset_network_header(skb);
306         fip->send(fip, skb);
307
308         if (!fcf)
309                 fip->sol_time = jiffies;
310 }
311
312 /**
313  * fcoe_ctlr_link_up() - Start FCoE controller
314  * @fip: The FCoE controller to start
315  *
316  * Called from the LLD when the network link is ready.
317  */
318 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
319 {
320         mutex_lock(&fip->ctlr_mutex);
321         if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
322                 mutex_unlock(&fip->ctlr_mutex);
323                 fc_linkup(fip->lp);
324         } else if (fip->state == FIP_ST_LINK_WAIT) {
325                 fcoe_ctlr_set_state(fip, fip->mode);
326                 switch (fip->mode) {
327                 default:
328                         LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
329                         /* fall-through */
330                 case FIP_MODE_AUTO:
331                         LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
332                         /* fall-through */
333                 case FIP_MODE_FABRIC:
334                 case FIP_MODE_NON_FIP:
335                         mutex_unlock(&fip->ctlr_mutex);
336                         fc_linkup(fip->lp);
337                         fcoe_ctlr_solicit(fip, NULL);
338                         break;
339                 case FIP_MODE_VN2VN:
340                         fcoe_ctlr_vn_start(fip);
341                         mutex_unlock(&fip->ctlr_mutex);
342                         fc_linkup(fip->lp);
343                         break;
344                 }
345         } else
346                 mutex_unlock(&fip->ctlr_mutex);
347 }
348 EXPORT_SYMBOL(fcoe_ctlr_link_up);
349
350 /**
351  * fcoe_ctlr_reset() - Reset a FCoE controller
352  * @fip:       The FCoE controller to reset
353  */
354 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
355 {
356         fcoe_ctlr_reset_fcfs(fip);
357         del_timer(&fip->timer);
358         fip->ctlr_ka_time = 0;
359         fip->port_ka_time = 0;
360         fip->sol_time = 0;
361         fip->flogi_oxid = FC_XID_UNKNOWN;
362         fcoe_ctlr_map_dest(fip);
363 }
364
365 /**
366  * fcoe_ctlr_link_down() - Stop a FCoE controller
367  * @fip: The FCoE controller to be stopped
368  *
369  * Returns non-zero if the link was up and now isn't.
370  *
371  * Called from the LLD when the network link is not ready.
372  * There may be multiple calls while the link is down.
373  */
374 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
375 {
376         int link_dropped;
377
378         LIBFCOE_FIP_DBG(fip, "link down.\n");
379         mutex_lock(&fip->ctlr_mutex);
380         fcoe_ctlr_reset(fip);
381         link_dropped = fip->state != FIP_ST_LINK_WAIT;
382         fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
383         mutex_unlock(&fip->ctlr_mutex);
384
385         if (link_dropped)
386                 fc_linkdown(fip->lp);
387         return link_dropped;
388 }
389 EXPORT_SYMBOL(fcoe_ctlr_link_down);
390
391 /**
392  * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
393  * @fip:   The FCoE controller to send the FKA on
394  * @lport: libfc fc_lport to send from
395  * @ports: 0 for controller keep-alive, 1 for port keep-alive
396  * @sa:    The source MAC address
397  *
398  * A controller keep-alive is sent every fka_period (typically 8 seconds).
399  * The source MAC is the native MAC address.
400  *
401  * A port keep-alive is sent every 90 seconds while logged in.
402  * The source MAC is the assigned mapped source address.
403  * The destination is the FCF's F-port.
404  */
405 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
406                                       struct fc_lport *lport,
407                                       int ports, u8 *sa)
408 {
409         struct sk_buff *skb;
410         struct fip_kal {
411                 struct ethhdr eth;
412                 struct fip_header fip;
413                 struct fip_mac_desc mac;
414         } __attribute__((packed)) *kal;
415         struct fip_vn_desc *vn;
416         u32 len;
417         struct fc_lport *lp;
418         struct fcoe_fcf *fcf;
419
420         fcf = fip->sel_fcf;
421         lp = fip->lp;
422         if (!fcf || (ports && !lp->port_id))
423                 return;
424
425         len = sizeof(*kal) + ports * sizeof(*vn);
426         skb = dev_alloc_skb(len);
427         if (!skb)
428                 return;
429
430         kal = (struct fip_kal *)skb->data;
431         memset(kal, 0, len);
432         memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
433         memcpy(kal->eth.h_source, sa, ETH_ALEN);
434         kal->eth.h_proto = htons(ETH_P_FIP);
435
436         kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
437         kal->fip.fip_op = htons(FIP_OP_CTRL);
438         kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
439         kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
440                                      ports * sizeof(*vn)) / FIP_BPW);
441         kal->fip.fip_flags = htons(FIP_FL_FPMA);
442         if (fip->spma)
443                 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
444
445         kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
446         kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
447         memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
448         if (ports) {
449                 vn = (struct fip_vn_desc *)(kal + 1);
450                 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
451                 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
452                 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
453                 hton24(vn->fd_fc_id, lport->port_id);
454                 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
455         }
456         skb_put(skb, len);
457         skb->protocol = htons(ETH_P_FIP);
458         skb_reset_mac_header(skb);
459         skb_reset_network_header(skb);
460         fip->send(fip, skb);
461 }
462
463 /**
464  * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
465  * @fip:   The FCoE controller for the ELS frame
466  * @dtype: The FIP descriptor type for the frame
467  * @skb:   The FCoE ELS frame including FC header but no FCoE headers
468  * @d_id:  The destination port ID.
469  *
470  * Returns non-zero error code on failure.
471  *
472  * The caller must check that the length is a multiple of 4.
473  *
474  * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
475  * Headroom includes the FIP encapsulation description, FIP header, and
476  * Ethernet header.  The tailroom is for the FIP MAC descriptor.
477  */
478 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
479                             u8 dtype, struct sk_buff *skb, u32 d_id)
480 {
481         struct fip_encaps_head {
482                 struct ethhdr eth;
483                 struct fip_header fip;
484                 struct fip_encaps encaps;
485         } __attribute__((packed)) *cap;
486         struct fc_frame_header *fh;
487         struct fip_mac_desc *mac;
488         struct fcoe_fcf *fcf;
489         size_t dlen;
490         u16 fip_flags;
491         u8 op;
492
493         fh = (struct fc_frame_header *)skb->data;
494         op = *(u8 *)(fh + 1);
495         dlen = sizeof(struct fip_encaps) + skb->len;    /* len before push */
496         cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
497         memset(cap, 0, sizeof(*cap));
498
499         if (lport->point_to_multipoint) {
500                 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
501                         return -ENODEV;
502                 fip_flags = 0;
503         } else {
504                 fcf = fip->sel_fcf;
505                 if (!fcf)
506                         return -ENODEV;
507                 fip_flags = fcf->flags;
508                 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
509                                          FIP_FL_FPMA;
510                 if (!fip_flags)
511                         return -ENODEV;
512                 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
513         }
514         memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
515         cap->eth.h_proto = htons(ETH_P_FIP);
516
517         cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
518         cap->fip.fip_op = htons(FIP_OP_LS);
519         if (op == ELS_LS_ACC || op == ELS_LS_RJT)
520                 cap->fip.fip_subcode = FIP_SC_REP;
521         else
522                 cap->fip.fip_subcode = FIP_SC_REQ;
523         cap->fip.fip_flags = htons(fip_flags);
524
525         cap->encaps.fd_desc.fip_dtype = dtype;
526         cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
527
528         if (op != ELS_LS_RJT) {
529                 dlen += sizeof(*mac);
530                 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
531                 memset(mac, 0, sizeof(*mac));
532                 mac->fd_desc.fip_dtype = FIP_DT_MAC;
533                 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
534                 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
535                         memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
536                 } else if (fip->mode == FIP_MODE_VN2VN) {
537                         hton24(mac->fd_mac, FIP_VN_FC_MAP);
538                         hton24(mac->fd_mac + 3, fip->port_id);
539                 } else if (fip_flags & FIP_FL_SPMA) {
540                         LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
541                         memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
542                 } else {
543                         LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
544                         /* FPMA only FLOGI.  Must leave the MAC desc zeroed. */
545                 }
546         }
547         cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
548
549         skb->protocol = htons(ETH_P_FIP);
550         skb_reset_mac_header(skb);
551         skb_reset_network_header(skb);
552         return 0;
553 }
554
555 /**
556  * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
557  * @fip:        FCoE controller.
558  * @lport:      libfc fc_lport to send from
559  * @skb:        FCoE ELS frame including FC header but no FCoE headers.
560  *
561  * Returns a non-zero error code if the frame should not be sent.
562  * Returns zero if the caller should send the frame with FCoE encapsulation.
563  *
564  * The caller must check that the length is a multiple of 4.
565  * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
566  * The the skb must also be an fc_frame.
567  */
568 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
569                        struct sk_buff *skb)
570 {
571         struct fc_frame *fp;
572         struct fc_frame_header *fh;
573         u16 old_xid;
574         u8 op;
575         u8 mac[ETH_ALEN];
576
577         fp = container_of(skb, struct fc_frame, skb);
578         fh = (struct fc_frame_header *)skb->data;
579         op = *(u8 *)(fh + 1);
580
581         if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
582                 old_xid = fip->flogi_oxid;
583                 fip->flogi_oxid = ntohs(fh->fh_ox_id);
584                 if (fip->state == FIP_ST_AUTO) {
585                         if (old_xid == FC_XID_UNKNOWN)
586                                 fip->flogi_count = 0;
587                         fip->flogi_count++;
588                         if (fip->flogi_count < 3)
589                                 goto drop;
590                         fcoe_ctlr_map_dest(fip);
591                         return 0;
592                 }
593                 if (fip->state == FIP_ST_NON_FIP)
594                         fcoe_ctlr_map_dest(fip);
595         }
596
597         if (fip->state == FIP_ST_NON_FIP)
598                 return 0;
599         if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
600                 goto drop;
601         switch (op) {
602         case ELS_FLOGI:
603                 op = FIP_DT_FLOGI;
604                 break;
605         case ELS_FDISC:
606                 if (ntoh24(fh->fh_s_id))
607                         return 0;
608                 op = FIP_DT_FDISC;
609                 break;
610         case ELS_LOGO:
611                 if (fip->mode == FIP_MODE_VN2VN) {
612                         if (fip->state != FIP_ST_VNMP_UP)
613                                 return -EINVAL;
614                         if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
615                                 return -EINVAL;
616                 } else {
617                         if (fip->state != FIP_ST_ENABLED)
618                                 return 0;
619                         if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
620                                 return 0;
621                 }
622                 op = FIP_DT_LOGO;
623                 break;
624         case ELS_LS_ACC:
625                 /*
626                  * If non-FIP, we may have gotten an SID by accepting an FLOGI
627                  * from a point-to-point connection.  Switch to using
628                  * the source mac based on the SID.  The destination
629                  * MAC in this case would have been set by receving the
630                  * FLOGI.
631                  */
632                 if (fip->state == FIP_ST_NON_FIP) {
633                         if (fip->flogi_oxid == FC_XID_UNKNOWN)
634                                 return 0;
635                         fip->flogi_oxid = FC_XID_UNKNOWN;
636                         fc_fcoe_set_mac(mac, fh->fh_d_id);
637                         fip->update_mac(lport, mac);
638                 }
639                 /* fall through */
640         case ELS_LS_RJT:
641                 op = fr_encaps(fp);
642                 if (op)
643                         break;
644                 return 0;
645         default:
646                 if (fip->state != FIP_ST_ENABLED &&
647                     fip->state != FIP_ST_VNMP_UP)
648                         goto drop;
649                 return 0;
650         }
651         LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
652                         op, ntoh24(fh->fh_d_id));
653         if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
654                 goto drop;
655         fip->send(fip, skb);
656         return -EINPROGRESS;
657 drop:
658         kfree_skb(skb);
659         return -EINVAL;
660 }
661 EXPORT_SYMBOL(fcoe_ctlr_els_send);
662
663 /**
664  * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
665  * @fip: The FCoE controller to free FCFs on
666  *
667  * Called with lock held and preemption disabled.
668  *
669  * An FCF is considered old if we have missed two advertisements.
670  * That is, there have been no valid advertisement from it for 2.5
671  * times its keep-alive period.
672  *
673  * In addition, determine the time when an FCF selection can occur.
674  *
675  * Also, increment the MissDiscAdvCount when no advertisement is received
676  * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
677  *
678  * Returns the time in jiffies for the next call.
679  */
680 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
681 {
682         struct fcoe_fcf *fcf;
683         struct fcoe_fcf *next;
684         unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
685         unsigned long deadline;
686         unsigned long sel_time = 0;
687         struct fcoe_dev_stats *stats;
688
689         stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
690
691         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
692                 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
693                 if (fip->sel_fcf == fcf) {
694                         if (time_after(jiffies, deadline)) {
695                                 stats->MissDiscAdvCount++;
696                                 printk(KERN_INFO "libfcoe: host%d: "
697                                        "Missing Discovery Advertisement "
698                                        "for fab %16.16llx count %lld\n",
699                                        fip->lp->host->host_no, fcf->fabric_name,
700                                        stats->MissDiscAdvCount);
701                         } else if (time_after(next_timer, deadline))
702                                 next_timer = deadline;
703                 }
704
705                 deadline += fcf->fka_period;
706                 if (time_after_eq(jiffies, deadline)) {
707                         if (fip->sel_fcf == fcf)
708                                 fip->sel_fcf = NULL;
709                         list_del(&fcf->list);
710                         WARN_ON(!fip->fcf_count);
711                         fip->fcf_count--;
712                         kfree(fcf);
713                         stats->VLinkFailureCount++;
714                 } else {
715                         if (time_after(next_timer, deadline))
716                                 next_timer = deadline;
717                         if (fcoe_ctlr_mtu_valid(fcf) &&
718                             (!sel_time || time_before(sel_time, fcf->time)))
719                                 sel_time = fcf->time;
720                 }
721         }
722         put_cpu();
723         if (sel_time && !fip->sel_fcf && !fip->sel_time) {
724                 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
725                 fip->sel_time = sel_time;
726         }
727
728         return next_timer;
729 }
730
731 /**
732  * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
733  * @fip: The FCoE controller receiving the advertisement
734  * @skb: The received FIP advertisement frame
735  * @fcf: The resulting FCF entry
736  *
737  * Returns zero on a valid parsed advertisement,
738  * otherwise returns non zero value.
739  */
740 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
741                                struct sk_buff *skb, struct fcoe_fcf *fcf)
742 {
743         struct fip_header *fiph;
744         struct fip_desc *desc = NULL;
745         struct fip_wwn_desc *wwn;
746         struct fip_fab_desc *fab;
747         struct fip_fka_desc *fka;
748         unsigned long t;
749         size_t rlen;
750         size_t dlen;
751         u32 desc_mask;
752
753         memset(fcf, 0, sizeof(*fcf));
754         fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
755
756         fiph = (struct fip_header *)skb->data;
757         fcf->flags = ntohs(fiph->fip_flags);
758
759         /*
760          * mask of required descriptors. validating each one clears its bit.
761          */
762         desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
763                         BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
764
765         rlen = ntohs(fiph->fip_dl_len) * 4;
766         if (rlen + sizeof(*fiph) > skb->len)
767                 return -EINVAL;
768
769         desc = (struct fip_desc *)(fiph + 1);
770         while (rlen > 0) {
771                 dlen = desc->fip_dlen * FIP_BPW;
772                 if (dlen < sizeof(*desc) || dlen > rlen)
773                         return -EINVAL;
774                 /* Drop Adv if there are duplicate critical descriptors */
775                 if ((desc->fip_dtype < 32) &&
776                     !(desc_mask & 1U << desc->fip_dtype)) {
777                         LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
778                                         "Descriptors in FIP adv\n");
779                         return -EINVAL;
780                 }
781                 switch (desc->fip_dtype) {
782                 case FIP_DT_PRI:
783                         if (dlen != sizeof(struct fip_pri_desc))
784                                 goto len_err;
785                         fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
786                         desc_mask &= ~BIT(FIP_DT_PRI);
787                         break;
788                 case FIP_DT_MAC:
789                         if (dlen != sizeof(struct fip_mac_desc))
790                                 goto len_err;
791                         memcpy(fcf->fcf_mac,
792                                ((struct fip_mac_desc *)desc)->fd_mac,
793                                ETH_ALEN);
794                         if (!is_valid_ether_addr(fcf->fcf_mac)) {
795                                 LIBFCOE_FIP_DBG(fip,
796                                         "Invalid MAC addr %pM in FIP adv\n",
797                                         fcf->fcf_mac);
798                                 return -EINVAL;
799                         }
800                         desc_mask &= ~BIT(FIP_DT_MAC);
801                         break;
802                 case FIP_DT_NAME:
803                         if (dlen != sizeof(struct fip_wwn_desc))
804                                 goto len_err;
805                         wwn = (struct fip_wwn_desc *)desc;
806                         fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
807                         desc_mask &= ~BIT(FIP_DT_NAME);
808                         break;
809                 case FIP_DT_FAB:
810                         if (dlen != sizeof(struct fip_fab_desc))
811                                 goto len_err;
812                         fab = (struct fip_fab_desc *)desc;
813                         fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
814                         fcf->vfid = ntohs(fab->fd_vfid);
815                         fcf->fc_map = ntoh24(fab->fd_map);
816                         desc_mask &= ~BIT(FIP_DT_FAB);
817                         break;
818                 case FIP_DT_FKA:
819                         if (dlen != sizeof(struct fip_fka_desc))
820                                 goto len_err;
821                         fka = (struct fip_fka_desc *)desc;
822                         if (fka->fd_flags & FIP_FKA_ADV_D)
823                                 fcf->fd_flags = 1;
824                         t = ntohl(fka->fd_fka_period);
825                         if (t >= FCOE_CTLR_MIN_FKA)
826                                 fcf->fka_period = msecs_to_jiffies(t);
827                         desc_mask &= ~BIT(FIP_DT_FKA);
828                         break;
829                 case FIP_DT_MAP_OUI:
830                 case FIP_DT_FCOE_SIZE:
831                 case FIP_DT_FLOGI:
832                 case FIP_DT_FDISC:
833                 case FIP_DT_LOGO:
834                 case FIP_DT_ELP:
835                 default:
836                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
837                                         "in FIP adv\n", desc->fip_dtype);
838                         /* standard says ignore unknown descriptors >= 128 */
839                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
840                                 return -EINVAL;
841                         break;
842                 }
843                 desc = (struct fip_desc *)((char *)desc + dlen);
844                 rlen -= dlen;
845         }
846         if (!fcf->fc_map || (fcf->fc_map & 0x10000))
847                 return -EINVAL;
848         if (!fcf->switch_name)
849                 return -EINVAL;
850         if (desc_mask) {
851                 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
852                                 desc_mask);
853                 return -EINVAL;
854         }
855         return 0;
856
857 len_err:
858         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
859                         desc->fip_dtype, dlen);
860         return -EINVAL;
861 }
862
863 /**
864  * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
865  * @fip: The FCoE controller receiving the advertisement
866  * @skb: The received FIP packet
867  */
868 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
869 {
870         struct fcoe_fcf *fcf;
871         struct fcoe_fcf new;
872         struct fcoe_fcf *found;
873         unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
874         int first = 0;
875         int mtu_valid;
876
877         if (fcoe_ctlr_parse_adv(fip, skb, &new))
878                 return;
879
880         mutex_lock(&fip->ctlr_mutex);
881         first = list_empty(&fip->fcfs);
882         found = NULL;
883         list_for_each_entry(fcf, &fip->fcfs, list) {
884                 if (fcf->switch_name == new.switch_name &&
885                     fcf->fabric_name == new.fabric_name &&
886                     fcf->fc_map == new.fc_map &&
887                     compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
888                         found = fcf;
889                         break;
890                 }
891         }
892         if (!found) {
893                 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
894                         goto out;
895
896                 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
897                 if (!fcf)
898                         goto out;
899
900                 fip->fcf_count++;
901                 memcpy(fcf, &new, sizeof(new));
902                 list_add(&fcf->list, &fip->fcfs);
903         } else {
904                 /*
905                  * Update the FCF's keep-alive descriptor flags.
906                  * Other flag changes from new advertisements are
907                  * ignored after a solicited advertisement is
908                  * received and the FCF is selectable (usable).
909                  */
910                 fcf->fd_flags = new.fd_flags;
911                 if (!fcoe_ctlr_fcf_usable(fcf))
912                         fcf->flags = new.flags;
913
914                 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
915                         fip->ctlr_ka_time -= fcf->fka_period;
916                         fip->ctlr_ka_time += new.fka_period;
917                         if (time_before(fip->ctlr_ka_time, fip->timer.expires))
918                                 mod_timer(&fip->timer, fip->ctlr_ka_time);
919                 }
920                 fcf->fka_period = new.fka_period;
921                 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
922         }
923         mtu_valid = fcoe_ctlr_mtu_valid(fcf);
924         fcf->time = jiffies;
925         if (!found) {
926                 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
927                                 "map %x val %d\n",
928                                 fcf->fabric_name, fcf->fc_map, mtu_valid);
929         }
930
931         /*
932          * If this advertisement is not solicited and our max receive size
933          * hasn't been verified, send a solicited advertisement.
934          */
935         if (!mtu_valid)
936                 fcoe_ctlr_solicit(fip, fcf);
937
938         /*
939          * If its been a while since we did a solicit, and this is
940          * the first advertisement we've received, do a multicast
941          * solicitation to gather as many advertisements as we can
942          * before selection occurs.
943          */
944         if (first && time_after(jiffies, fip->sol_time + sol_tov))
945                 fcoe_ctlr_solicit(fip, NULL);
946
947         /*
948          * If this is the first validated FCF, note the time and
949          * set a timer to trigger selection.
950          */
951         if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
952                 fip->sel_time = jiffies +
953                         msecs_to_jiffies(FCOE_CTLR_START_DELAY);
954                 if (!timer_pending(&fip->timer) ||
955                     time_before(fip->sel_time, fip->timer.expires))
956                         mod_timer(&fip->timer, fip->sel_time);
957         }
958 out:
959         mutex_unlock(&fip->ctlr_mutex);
960 }
961
962 /**
963  * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
964  * @fip: The FCoE controller which received the packet
965  * @skb: The received FIP packet
966  */
967 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
968 {
969         struct fc_lport *lport = fip->lp;
970         struct fip_header *fiph;
971         struct fc_frame *fp = (struct fc_frame *)skb;
972         struct fc_frame_header *fh = NULL;
973         struct fip_desc *desc;
974         struct fip_encaps *els;
975         struct fcoe_dev_stats *stats;
976         enum fip_desc_type els_dtype = 0;
977         u8 els_op;
978         u8 sub;
979         u8 granted_mac[ETH_ALEN] = { 0 };
980         size_t els_len = 0;
981         size_t rlen;
982         size_t dlen;
983         u32 desc_mask = 0;
984         u32 desc_cnt = 0;
985
986         fiph = (struct fip_header *)skb->data;
987         sub = fiph->fip_subcode;
988         if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
989                 goto drop;
990
991         rlen = ntohs(fiph->fip_dl_len) * 4;
992         if (rlen + sizeof(*fiph) > skb->len)
993                 goto drop;
994
995         desc = (struct fip_desc *)(fiph + 1);
996         while (rlen > 0) {
997                 desc_cnt++;
998                 dlen = desc->fip_dlen * FIP_BPW;
999                 if (dlen < sizeof(*desc) || dlen > rlen)
1000                         goto drop;
1001                 /* Drop ELS if there are duplicate critical descriptors */
1002                 if (desc->fip_dtype < 32) {
1003                         if (desc_mask & 1U << desc->fip_dtype) {
1004                                 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1005                                                 "Descriptors in FIP ELS\n");
1006                                 goto drop;
1007                         }
1008                         desc_mask |= (1 << desc->fip_dtype);
1009                 }
1010                 switch (desc->fip_dtype) {
1011                 case FIP_DT_MAC:
1012                         if (desc_cnt == 1) {
1013                                 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1014                                                 "received out of order\n");
1015                                 goto drop;
1016                         }
1017
1018                         if (dlen != sizeof(struct fip_mac_desc))
1019                                 goto len_err;
1020                         memcpy(granted_mac,
1021                                ((struct fip_mac_desc *)desc)->fd_mac,
1022                                ETH_ALEN);
1023                         break;
1024                 case FIP_DT_FLOGI:
1025                 case FIP_DT_FDISC:
1026                 case FIP_DT_LOGO:
1027                 case FIP_DT_ELP:
1028                         if (desc_cnt != 1) {
1029                                 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1030                                                 "received out of order\n");
1031                                 goto drop;
1032                         }
1033                         if (fh)
1034                                 goto drop;
1035                         if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1036                                 goto len_err;
1037                         els_len = dlen - sizeof(*els);
1038                         els = (struct fip_encaps *)desc;
1039                         fh = (struct fc_frame_header *)(els + 1);
1040                         els_dtype = desc->fip_dtype;
1041                         break;
1042                 default:
1043                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1044                                         "in FIP adv\n", desc->fip_dtype);
1045                         /* standard says ignore unknown descriptors >= 128 */
1046                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1047                                 goto drop;
1048                         if (desc_cnt <= 2) {
1049                                 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1050                                                 "received out of order\n");
1051                                 goto drop;
1052                         }
1053                         break;
1054                 }
1055                 desc = (struct fip_desc *)((char *)desc + dlen);
1056                 rlen -= dlen;
1057         }
1058
1059         if (!fh)
1060                 goto drop;
1061         els_op = *(u8 *)(fh + 1);
1062
1063         if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1064             sub == FIP_SC_REP && els_op == ELS_LS_ACC &&
1065             fip->mode != FIP_MODE_VN2VN) {
1066                 if (!is_valid_ether_addr(granted_mac)) {
1067                         LIBFCOE_FIP_DBG(fip,
1068                                 "Invalid MAC address %pM in FIP ELS\n",
1069                                 granted_mac);
1070                         goto drop;
1071                 }
1072                 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1073
1074                 if (fip->flogi_oxid == ntohs(fh->fh_ox_id))
1075                         fip->flogi_oxid = FC_XID_UNKNOWN;
1076         }
1077
1078         if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1079             (!(1U << FIP_DT_MAC & desc_mask)))) {
1080                 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1081                                 "in FIP ELS\n");
1082                 goto drop;
1083         }
1084
1085         /*
1086          * Convert skb into an fc_frame containing only the ELS.
1087          */
1088         skb_pull(skb, (u8 *)fh - skb->data);
1089         skb_trim(skb, els_len);
1090         fp = (struct fc_frame *)skb;
1091         fc_frame_init(fp);
1092         fr_sof(fp) = FC_SOF_I3;
1093         fr_eof(fp) = FC_EOF_T;
1094         fr_dev(fp) = lport;
1095         fr_encaps(fp) = els_dtype;
1096
1097         stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1098         stats->RxFrames++;
1099         stats->RxWords += skb->len / FIP_BPW;
1100         put_cpu();
1101
1102         fc_exch_recv(lport, fp);
1103         return;
1104
1105 len_err:
1106         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1107                         desc->fip_dtype, dlen);
1108 drop:
1109         kfree_skb(skb);
1110 }
1111
1112 /**
1113  * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1114  * @fip: The FCoE controller that received the frame
1115  * @fh:  The received FIP header
1116  *
1117  * There may be multiple VN_Port descriptors.
1118  * The overall length has already been checked.
1119  */
1120 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1121                                      struct fip_header *fh)
1122 {
1123         struct fip_desc *desc;
1124         struct fip_mac_desc *mp;
1125         struct fip_wwn_desc *wp;
1126         struct fip_vn_desc *vp;
1127         size_t rlen;
1128         size_t dlen;
1129         struct fcoe_fcf *fcf = fip->sel_fcf;
1130         struct fc_lport *lport = fip->lp;
1131         struct fc_lport *vn_port = NULL;
1132         u32 desc_mask;
1133         int is_vn_port = 0;
1134
1135         LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1136
1137         if (!fcf || !lport->port_id)
1138                 return;
1139
1140         /*
1141          * mask of required descriptors.  Validating each one clears its bit.
1142          */
1143         desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1144
1145         rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1146         desc = (struct fip_desc *)(fh + 1);
1147         while (rlen >= sizeof(*desc)) {
1148                 dlen = desc->fip_dlen * FIP_BPW;
1149                 if (dlen > rlen)
1150                         return;
1151                 /* Drop CVL if there are duplicate critical descriptors */
1152                 if ((desc->fip_dtype < 32) &&
1153                     !(desc_mask & 1U << desc->fip_dtype)) {
1154                         LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1155                                         "Descriptors in FIP CVL\n");
1156                         return;
1157                 }
1158                 switch (desc->fip_dtype) {
1159                 case FIP_DT_MAC:
1160                         mp = (struct fip_mac_desc *)desc;
1161                         if (dlen < sizeof(*mp))
1162                                 return;
1163                         if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1164                                 return;
1165                         desc_mask &= ~BIT(FIP_DT_MAC);
1166                         break;
1167                 case FIP_DT_NAME:
1168                         wp = (struct fip_wwn_desc *)desc;
1169                         if (dlen < sizeof(*wp))
1170                                 return;
1171                         if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1172                                 return;
1173                         desc_mask &= ~BIT(FIP_DT_NAME);
1174                         break;
1175                 case FIP_DT_VN_ID:
1176                         vp = (struct fip_vn_desc *)desc;
1177                         if (dlen < sizeof(*vp))
1178                                 return;
1179                         if (compare_ether_addr(vp->fd_mac,
1180                                                fip->get_src_addr(lport)) == 0 &&
1181                             get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
1182                             ntoh24(vp->fd_fc_id) == lport->port_id) {
1183                                 desc_mask &= ~BIT(FIP_DT_VN_ID);
1184                                 break;
1185                         }
1186                         /* check if clr_vlink is for NPIV port */
1187                         mutex_lock(&lport->lp_mutex);
1188                         list_for_each_entry(vn_port, &lport->vports, list) {
1189                                 if (compare_ether_addr(vp->fd_mac,
1190                                     fip->get_src_addr(vn_port)) == 0 &&
1191                                     (get_unaligned_be64(&vp->fd_wwpn)
1192                                                         == vn_port->wwpn) &&
1193                                     (ntoh24(vp->fd_fc_id) ==
1194                                             fc_host_port_id(vn_port->host))) {
1195                                         desc_mask &= ~BIT(FIP_DT_VN_ID);
1196                                         is_vn_port = 1;
1197                                         break;
1198                                 }
1199                         }
1200                         mutex_unlock(&lport->lp_mutex);
1201
1202                         break;
1203                 default:
1204                         /* standard says ignore unknown descriptors >= 128 */
1205                         if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1206                                 return;
1207                         break;
1208                 }
1209                 desc = (struct fip_desc *)((char *)desc + dlen);
1210                 rlen -= dlen;
1211         }
1212
1213         /*
1214          * reset only if all required descriptors were present and valid.
1215          */
1216         if (desc_mask) {
1217                 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1218                                 desc_mask);
1219         } else {
1220                 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1221
1222                 if (is_vn_port)
1223                         fc_lport_reset(vn_port);
1224                 else {
1225                         mutex_lock(&fip->ctlr_mutex);
1226                         per_cpu_ptr(lport->dev_stats,
1227                                     get_cpu())->VLinkFailureCount++;
1228                         put_cpu();
1229                         fcoe_ctlr_reset(fip);
1230                         mutex_unlock(&fip->ctlr_mutex);
1231
1232                         fc_lport_reset(fip->lp);
1233                         fcoe_ctlr_solicit(fip, NULL);
1234                 }
1235         }
1236 }
1237
1238 /**
1239  * fcoe_ctlr_recv() - Receive a FIP packet
1240  * @fip: The FCoE controller that received the packet
1241  * @skb: The received FIP packet
1242  *
1243  * This may be called from either NET_RX_SOFTIRQ or IRQ.
1244  */
1245 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1246 {
1247         skb_queue_tail(&fip->fip_recv_list, skb);
1248         schedule_work(&fip->recv_work);
1249 }
1250 EXPORT_SYMBOL(fcoe_ctlr_recv);
1251
1252 /**
1253  * fcoe_ctlr_recv_handler() - Receive a FIP frame
1254  * @fip: The FCoE controller that received the frame
1255  * @skb: The received FIP frame
1256  *
1257  * Returns non-zero if the frame is dropped.
1258  */
1259 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1260 {
1261         struct fip_header *fiph;
1262         struct ethhdr *eh;
1263         enum fip_state state;
1264         u16 op;
1265         u8 sub;
1266
1267         if (skb_linearize(skb))
1268                 goto drop;
1269         if (skb->len < sizeof(*fiph))
1270                 goto drop;
1271         eh = eth_hdr(skb);
1272         if (fip->mode == FIP_MODE_VN2VN) {
1273                 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1274                     compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1275                     compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1276                         goto drop;
1277         } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1278                    compare_ether_addr(eh->h_dest, fcoe_all_enode))
1279                 goto drop;
1280         fiph = (struct fip_header *)skb->data;
1281         op = ntohs(fiph->fip_op);
1282         sub = fiph->fip_subcode;
1283
1284         if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1285                 goto drop;
1286         if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1287                 goto drop;
1288
1289         mutex_lock(&fip->ctlr_mutex);
1290         state = fip->state;
1291         if (state == FIP_ST_AUTO) {
1292                 fip->map_dest = 0;
1293                 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1294                 state = FIP_ST_ENABLED;
1295                 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1296         }
1297         mutex_unlock(&fip->ctlr_mutex);
1298
1299         if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1300                 return fcoe_ctlr_vn_recv(fip, skb);
1301
1302         if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1303             state != FIP_ST_VNMP_CLAIM)
1304                 goto drop;
1305
1306         if (op == FIP_OP_LS) {
1307                 fcoe_ctlr_recv_els(fip, skb);   /* consumes skb */
1308                 return 0;
1309         }
1310
1311         if (state != FIP_ST_ENABLED)
1312                 goto drop;
1313
1314         if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1315                 fcoe_ctlr_recv_adv(fip, skb);
1316         else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1317                 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1318         kfree_skb(skb);
1319         return 0;
1320 drop:
1321         kfree_skb(skb);
1322         return -1;
1323 }
1324
1325 /**
1326  * fcoe_ctlr_select() - Select the best FCF (if possible)
1327  * @fip: The FCoE controller
1328  *
1329  * If there are conflicting advertisements, no FCF can be chosen.
1330  *
1331  * Called with lock held.
1332  */
1333 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1334 {
1335         struct fcoe_fcf *fcf;
1336         struct fcoe_fcf *best = NULL;
1337
1338         list_for_each_entry(fcf, &fip->fcfs, list) {
1339                 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1340                                 "VFID %d map %x val %d\n",
1341                                 fcf->fabric_name, fcf->vfid,
1342                                 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1343                 if (!fcoe_ctlr_fcf_usable(fcf)) {
1344                         LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1345                                         "map %x %svalid %savailable\n",
1346                                         fcf->fabric_name, fcf->fc_map,
1347                                         (fcf->flags & FIP_FL_SOL) ? "" : "in",
1348                                         (fcf->flags & FIP_FL_AVAIL) ?
1349                                         "" : "un");
1350                         continue;
1351                 }
1352                 if (!best) {
1353                         best = fcf;
1354                         continue;
1355                 }
1356                 if (fcf->fabric_name != best->fabric_name ||
1357                     fcf->vfid != best->vfid ||
1358                     fcf->fc_map != best->fc_map) {
1359                         LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1360                                         "or FC-MAP\n");
1361                         return;
1362                 }
1363                 if (fcf->pri < best->pri)
1364                         best = fcf;
1365         }
1366         fip->sel_fcf = best;
1367 }
1368
1369 /**
1370  * fcoe_ctlr_timeout() - FIP timeout handler
1371  * @arg: The FCoE controller that timed out
1372  */
1373 static void fcoe_ctlr_timeout(unsigned long arg)
1374 {
1375         struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1376
1377         schedule_work(&fip->timer_work);
1378 }
1379
1380 /**
1381  * fcoe_ctlr_timer_work() - Worker thread function for timer work
1382  * @work: Handle to a FCoE controller
1383  *
1384  * Ages FCFs.  Triggers FCF selection if possible.
1385  * Sends keep-alives and resets.
1386  */
1387 static void fcoe_ctlr_timer_work(struct work_struct *work)
1388 {
1389         struct fcoe_ctlr *fip;
1390         struct fc_lport *vport;
1391         u8 *mac;
1392         u8 reset = 0;
1393         u8 send_ctlr_ka = 0;
1394         u8 send_port_ka = 0;
1395         struct fcoe_fcf *sel;
1396         struct fcoe_fcf *fcf;
1397         unsigned long next_timer;
1398
1399         fip = container_of(work, struct fcoe_ctlr, timer_work);
1400         if (fip->mode == FIP_MODE_VN2VN)
1401                 return fcoe_ctlr_vn_timeout(fip);
1402         mutex_lock(&fip->ctlr_mutex);
1403         if (fip->state == FIP_ST_DISABLED) {
1404                 mutex_unlock(&fip->ctlr_mutex);
1405                 return;
1406         }
1407
1408         fcf = fip->sel_fcf;
1409         next_timer = fcoe_ctlr_age_fcfs(fip);
1410
1411         sel = fip->sel_fcf;
1412         if (!sel && fip->sel_time) {
1413                 if (time_after_eq(jiffies, fip->sel_time)) {
1414                         fcoe_ctlr_select(fip);
1415                         sel = fip->sel_fcf;
1416                         fip->sel_time = 0;
1417                 } else if (time_after(next_timer, fip->sel_time))
1418                         next_timer = fip->sel_time;
1419         }
1420
1421         if (sel != fcf) {
1422                 fcf = sel;              /* the old FCF may have been freed */
1423                 if (sel) {
1424                         printk(KERN_INFO "libfcoe: host%d: FIP selected "
1425                                "Fibre-Channel Forwarder MAC %pM\n",
1426                                fip->lp->host->host_no, sel->fcf_mac);
1427                         memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1428                         fip->map_dest = 0;
1429                         fip->port_ka_time = jiffies +
1430                                 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1431                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1432                         if (time_after(next_timer, fip->ctlr_ka_time))
1433                                 next_timer = fip->ctlr_ka_time;
1434                 } else {
1435                         printk(KERN_NOTICE "libfcoe: host%d: "
1436                                "FIP Fibre-Channel Forwarder timed out.  "
1437                                "Starting FCF discovery.\n",
1438                                fip->lp->host->host_no);
1439                         reset = 1;
1440                 }
1441         }
1442
1443         if (sel && !sel->fd_flags) {
1444                 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1445                         fip->ctlr_ka_time = jiffies + sel->fka_period;
1446                         send_ctlr_ka = 1;
1447                 }
1448                 if (time_after(next_timer, fip->ctlr_ka_time))
1449                         next_timer = fip->ctlr_ka_time;
1450
1451                 if (time_after_eq(jiffies, fip->port_ka_time)) {
1452                         fip->port_ka_time = jiffies +
1453                                 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1454                         send_port_ka = 1;
1455                 }
1456                 if (time_after(next_timer, fip->port_ka_time))
1457                         next_timer = fip->port_ka_time;
1458         }
1459         if (!list_empty(&fip->fcfs))
1460                 mod_timer(&fip->timer, next_timer);
1461         mutex_unlock(&fip->ctlr_mutex);
1462
1463         if (reset) {
1464                 fc_lport_reset(fip->lp);
1465                 /* restart things with a solicitation */
1466                 fcoe_ctlr_solicit(fip, NULL);
1467         }
1468
1469         if (send_ctlr_ka)
1470                 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1471
1472         if (send_port_ka) {
1473                 mutex_lock(&fip->lp->lp_mutex);
1474                 mac = fip->get_src_addr(fip->lp);
1475                 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1476                 list_for_each_entry(vport, &fip->lp->vports, list) {
1477                         mac = fip->get_src_addr(vport);
1478                         fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1479                 }
1480                 mutex_unlock(&fip->lp->lp_mutex);
1481         }
1482 }
1483
1484 /**
1485  * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1486  * @recv_work: Handle to a FCoE controller
1487  */
1488 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1489 {
1490         struct fcoe_ctlr *fip;
1491         struct sk_buff *skb;
1492
1493         fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1494         while ((skb = skb_dequeue(&fip->fip_recv_list)))
1495                 fcoe_ctlr_recv_handler(fip, skb);
1496 }
1497
1498 /**
1499  * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1500  * @fip: The FCoE controller
1501  * @fp:  The FC frame to snoop
1502  *
1503  * Snoop potential response to FLOGI or even incoming FLOGI.
1504  *
1505  * The caller has checked that we are waiting for login as indicated
1506  * by fip->flogi_oxid != FC_XID_UNKNOWN.
1507  *
1508  * The caller is responsible for freeing the frame.
1509  * Fill in the granted_mac address.
1510  *
1511  * Return non-zero if the frame should not be delivered to libfc.
1512  */
1513 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1514                          struct fc_frame *fp)
1515 {
1516         struct fc_frame_header *fh;
1517         u8 op;
1518         u8 *sa;
1519
1520         sa = eth_hdr(&fp->skb)->h_source;
1521         fh = fc_frame_header_get(fp);
1522         if (fh->fh_type != FC_TYPE_ELS)
1523                 return 0;
1524
1525         op = fc_frame_payload_op(fp);
1526         if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1527             fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1528
1529                 mutex_lock(&fip->ctlr_mutex);
1530                 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1531                         mutex_unlock(&fip->ctlr_mutex);
1532                         return -EINVAL;
1533                 }
1534                 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1535                 LIBFCOE_FIP_DBG(fip,
1536                                 "received FLOGI LS_ACC using non-FIP mode\n");
1537
1538                 /*
1539                  * FLOGI accepted.
1540                  * If the src mac addr is FC_OUI-based, then we mark the
1541                  * address_mode flag to use FC_OUI-based Ethernet DA.
1542                  * Otherwise we use the FCoE gateway addr
1543                  */
1544                 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1545                         fcoe_ctlr_map_dest(fip);
1546                 } else {
1547                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1548                         fip->map_dest = 0;
1549                 }
1550                 fip->flogi_oxid = FC_XID_UNKNOWN;
1551                 mutex_unlock(&fip->ctlr_mutex);
1552                 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1553         } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1554                 /*
1555                  * Save source MAC for point-to-point responses.
1556                  */
1557                 mutex_lock(&fip->ctlr_mutex);
1558                 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1559                         memcpy(fip->dest_addr, sa, ETH_ALEN);
1560                         fip->map_dest = 0;
1561                         if (fip->state == FIP_ST_AUTO)
1562                                 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1563                                                 "Setting non-FIP mode\n");
1564                         fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1565                 }
1566                 mutex_unlock(&fip->ctlr_mutex);
1567         }
1568         return 0;
1569 }
1570 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1571
1572 /**
1573  * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1574  * @mac:    The MAC address to convert
1575  * @scheme: The scheme to use when converting
1576  * @port:   The port indicator for converting
1577  *
1578  * Returns: u64 fc world wide name
1579  */
1580 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1581                       unsigned int scheme, unsigned int port)
1582 {
1583         u64 wwn;
1584         u64 host_mac;
1585
1586         /* The MAC is in NO, so flip only the low 48 bits */
1587         host_mac = ((u64) mac[0] << 40) |
1588                 ((u64) mac[1] << 32) |
1589                 ((u64) mac[2] << 24) |
1590                 ((u64) mac[3] << 16) |
1591                 ((u64) mac[4] << 8) |
1592                 (u64) mac[5];
1593
1594         WARN_ON(host_mac >= (1ULL << 48));
1595         wwn = host_mac | ((u64) scheme << 60);
1596         switch (scheme) {
1597         case 1:
1598                 WARN_ON(port != 0);
1599                 break;
1600         case 2:
1601                 WARN_ON(port >= 0xfff);
1602                 wwn |= (u64) port << 48;
1603                 break;
1604         default:
1605                 WARN_ON(1);
1606                 break;
1607         }
1608
1609         return wwn;
1610 }
1611 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1612
1613 /**
1614  * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1615  * @rdata: libfc remote port
1616  */
1617 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1618 {
1619         return (struct fcoe_rport *)(rdata + 1);
1620 }
1621
1622 /**
1623  * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1624  * @fip: The FCoE controller
1625  * @sub: sub-opcode for probe request, reply, or advertisement.
1626  * @dest: The destination Ethernet MAC address
1627  * @min_len: minimum size of the Ethernet payload to be sent
1628  */
1629 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1630                               enum fip_vn2vn_subcode sub,
1631                               const u8 *dest, size_t min_len)
1632 {
1633         struct sk_buff *skb;
1634         struct fip_frame {
1635                 struct ethhdr eth;
1636                 struct fip_header fip;
1637                 struct fip_mac_desc mac;
1638                 struct fip_wwn_desc wwnn;
1639                 struct fip_vn_desc vn;
1640         } __attribute__((packed)) *frame;
1641         struct fip_fc4_feat *ff;
1642         struct fip_size_desc *size;
1643         u32 fcp_feat;
1644         size_t len;
1645         size_t dlen;
1646
1647         len = sizeof(*frame);
1648         dlen = 0;
1649         if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1650                 dlen = sizeof(struct fip_fc4_feat) +
1651                        sizeof(struct fip_size_desc);
1652                 len += dlen;
1653         }
1654         dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1655         len = max(len, min_len + sizeof(struct ethhdr));
1656
1657         skb = dev_alloc_skb(len);
1658         if (!skb)
1659                 return;
1660
1661         frame = (struct fip_frame *)skb->data;
1662         memset(frame, 0, len);
1663         memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1664         memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1665         frame->eth.h_proto = htons(ETH_P_FIP);
1666
1667         frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1668         frame->fip.fip_op = htons(FIP_OP_VN2VN);
1669         frame->fip.fip_subcode = sub;
1670         frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1671
1672         frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1673         frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1674         memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1675
1676         frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1677         frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1678         put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1679
1680         frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1681         frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1682         hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1683         hton24(frame->vn.fd_mac + 3, fip->port_id);
1684         hton24(frame->vn.fd_fc_id, fip->port_id);
1685         put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1686
1687         /*
1688          * For claims, add FC-4 features.
1689          * TBD: Add interface to get fc-4 types and features from libfc.
1690          */
1691         if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1692                 ff = (struct fip_fc4_feat *)(frame + 1);
1693                 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1694                 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1695                 ff->fd_fts = fip->lp->fcts;
1696
1697                 fcp_feat = 0;
1698                 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1699                         fcp_feat |= FCP_FEAT_INIT;
1700                 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1701                         fcp_feat |= FCP_FEAT_TARG;
1702                 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1703                 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1704
1705                 size = (struct fip_size_desc *)(ff + 1);
1706                 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1707                 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1708                 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1709         }
1710
1711         skb_put(skb, len);
1712         skb->protocol = htons(ETH_P_FIP);
1713         skb_reset_mac_header(skb);
1714         skb_reset_network_header(skb);
1715
1716         fip->send(fip, skb);
1717 }
1718
1719 /**
1720  * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1721  * @lport: The lport which is receiving the event
1722  * @rdata: remote port private data
1723  * @event: The event that occured
1724  *
1725  * Locking Note:  The rport lock must not be held when calling this function.
1726  */
1727 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1728                                         struct fc_rport_priv *rdata,
1729                                         enum fc_rport_event event)
1730 {
1731         struct fcoe_ctlr *fip = lport->disc.priv;
1732         struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1733
1734         LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1735                         rdata->ids.port_id, event);
1736
1737         mutex_lock(&fip->ctlr_mutex);
1738         switch (event) {
1739         case RPORT_EV_READY:
1740                 frport->login_count = 0;
1741                 break;
1742         case RPORT_EV_LOGO:
1743         case RPORT_EV_FAILED:
1744         case RPORT_EV_STOP:
1745                 frport->login_count++;
1746                 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1747                         LIBFCOE_FIP_DBG(fip,
1748                                         "rport FLOGI limited port_id %6.6x\n",
1749                                         rdata->ids.port_id);
1750                         lport->tt.rport_logoff(rdata);
1751                 }
1752                 break;
1753         default:
1754                 break;
1755         }
1756         mutex_unlock(&fip->ctlr_mutex);
1757 }
1758
1759 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1760         .event_callback = fcoe_ctlr_vn_rport_callback,
1761 };
1762
1763 /**
1764  * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1765  * @fip: The FCoE controller
1766  *
1767  * Called with ctlr_mutex held.
1768  */
1769 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1770 {
1771         mutex_lock(&lport->disc.disc_mutex);
1772         lport->disc.disc_callback = NULL;
1773         mutex_unlock(&lport->disc.disc_mutex);
1774 }
1775
1776 /**
1777  * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1778  * @fip: The FCoE controller
1779  *
1780  * Called through the local port template for discovery.
1781  * Called without the ctlr_mutex held.
1782  */
1783 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1784 {
1785         struct fcoe_ctlr *fip = lport->disc.priv;
1786
1787         mutex_lock(&fip->ctlr_mutex);
1788         fcoe_ctlr_disc_stop_locked(lport);
1789         mutex_unlock(&fip->ctlr_mutex);
1790 }
1791
1792 /**
1793  * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1794  * @fip: The FCoE controller
1795  *
1796  * Called through the local port template for discovery.
1797  * Called without the ctlr_mutex held.
1798  */
1799 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1800 {
1801         fcoe_ctlr_disc_stop(lport);
1802         lport->tt.rport_flush_queue();
1803         synchronize_rcu();
1804 }
1805
1806 /**
1807  * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1808  * @fip: The FCoE controller
1809  *
1810  * Called with fcoe_ctlr lock held.
1811  */
1812 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1813 {
1814         unsigned long wait;
1815         u32 port_id;
1816
1817         fcoe_ctlr_disc_stop_locked(fip->lp);
1818
1819         /*
1820          * Get proposed port ID.
1821          * If this is the first try after link up, use any previous port_id.
1822          * If there was none, use the low bits of the port_name.
1823          * On subsequent tries, get the next random one.
1824          * Don't use reserved IDs, use another non-zero value, just as random.
1825          */
1826         port_id = fip->port_id;
1827         if (fip->probe_tries)
1828                 port_id = prandom32(&fip->rnd_state) & 0xffff;
1829         else if (!port_id)
1830                 port_id = fip->lp->wwpn & 0xffff;
1831         if (!port_id || port_id == 0xffff)
1832                 port_id = 1;
1833         fip->port_id = port_id;
1834
1835         if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1836                 fip->probe_tries++;
1837                 wait = random32() % FIP_VN_PROBE_WAIT;
1838         } else
1839                 wait = FIP_VN_RLIM_INT;
1840         mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1841         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1842 }
1843
1844 /**
1845  * fcoe_ctlr_vn_start() - Start in VN2VN mode
1846  * @fip: The FCoE controller
1847  *
1848  * Called with fcoe_ctlr lock held.
1849  */
1850 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1851 {
1852         fip->probe_tries = 0;
1853         prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1854         fcoe_ctlr_vn_restart(fip);
1855 }
1856
1857 /**
1858  * fcoe_ctlr_vn_parse - parse probe request or response
1859  * @fip: The FCoE controller
1860  * @skb: incoming packet
1861  * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1862  *
1863  * Returns non-zero error number on error.
1864  * Does not consume the packet.
1865  */
1866 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1867                               struct sk_buff *skb,
1868                               struct fc_rport_priv *rdata)
1869 {
1870         struct fip_header *fiph;
1871         struct fip_desc *desc = NULL;
1872         struct fip_mac_desc *macd = NULL;
1873         struct fip_wwn_desc *wwn = NULL;
1874         struct fip_vn_desc *vn = NULL;
1875         struct fip_size_desc *size = NULL;
1876         struct fcoe_rport *frport;
1877         size_t rlen;
1878         size_t dlen;
1879         u32 desc_mask = 0;
1880         u32 dtype;
1881         u8 sub;
1882
1883         memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1884         frport = fcoe_ctlr_rport(rdata);
1885
1886         fiph = (struct fip_header *)skb->data;
1887         frport->flags = ntohs(fiph->fip_flags);
1888
1889         sub = fiph->fip_subcode;
1890         switch (sub) {
1891         case FIP_SC_VN_PROBE_REQ:
1892         case FIP_SC_VN_PROBE_REP:
1893         case FIP_SC_VN_BEACON:
1894                 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1895                             BIT(FIP_DT_VN_ID);
1896                 break;
1897         case FIP_SC_VN_CLAIM_NOTIFY:
1898         case FIP_SC_VN_CLAIM_REP:
1899                 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1900                             BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1901                             BIT(FIP_DT_FCOE_SIZE);
1902                 break;
1903         default:
1904                 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1905                 return -EINVAL;
1906         }
1907
1908         rlen = ntohs(fiph->fip_dl_len) * 4;
1909         if (rlen + sizeof(*fiph) > skb->len)
1910                 return -EINVAL;
1911
1912         desc = (struct fip_desc *)(fiph + 1);
1913         while (rlen > 0) {
1914                 dlen = desc->fip_dlen * FIP_BPW;
1915                 if (dlen < sizeof(*desc) || dlen > rlen)
1916                         return -EINVAL;
1917
1918                 dtype = desc->fip_dtype;
1919                 if (dtype < 32) {
1920                         if (!(desc_mask & BIT(dtype))) {
1921                                 LIBFCOE_FIP_DBG(fip,
1922                                                 "unexpected or duplicated desc "
1923                                                 "desc type %u in "
1924                                                 "FIP VN2VN subtype %u\n",
1925                                                 dtype, sub);
1926                                 return -EINVAL;
1927                         }
1928                         desc_mask &= ~BIT(dtype);
1929                 }
1930
1931                 switch (dtype) {
1932                 case FIP_DT_MAC:
1933                         if (dlen != sizeof(struct fip_mac_desc))
1934                                 goto len_err;
1935                         macd = (struct fip_mac_desc *)desc;
1936                         if (!is_valid_ether_addr(macd->fd_mac)) {
1937                                 LIBFCOE_FIP_DBG(fip,
1938                                         "Invalid MAC addr %pM in FIP VN2VN\n",
1939                                          macd->fd_mac);
1940                                 return -EINVAL;
1941                         }
1942                         memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1943                         break;
1944                 case FIP_DT_NAME:
1945                         if (dlen != sizeof(struct fip_wwn_desc))
1946                                 goto len_err;
1947                         wwn = (struct fip_wwn_desc *)desc;
1948                         rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1949                         break;
1950                 case FIP_DT_VN_ID:
1951                         if (dlen != sizeof(struct fip_vn_desc))
1952                                 goto len_err;
1953                         vn = (struct fip_vn_desc *)desc;
1954                         memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1955                         rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1956                         rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1957                         break;
1958                 case FIP_DT_FC4F:
1959                         if (dlen != sizeof(struct fip_fc4_feat))
1960                                 goto len_err;
1961                         break;
1962                 case FIP_DT_FCOE_SIZE:
1963                         if (dlen != sizeof(struct fip_size_desc))
1964                                 goto len_err;
1965                         size = (struct fip_size_desc *)desc;
1966                         frport->fcoe_len = ntohs(size->fd_size);
1967                         break;
1968                 default:
1969                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1970                                         "in FIP probe\n", dtype);
1971                         /* standard says ignore unknown descriptors >= 128 */
1972                         if (dtype < FIP_DT_VENDOR_BASE)
1973                                 return -EINVAL;
1974                         break;
1975                 }
1976                 desc = (struct fip_desc *)((char *)desc + dlen);
1977                 rlen -= dlen;
1978         }
1979         return 0;
1980
1981 len_err:
1982         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1983                         dtype, dlen);
1984         return -EINVAL;
1985 }
1986
1987 /**
1988  * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
1989  * @fip: The FCoE controller
1990  *
1991  * Called with ctlr_mutex held.
1992  */
1993 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
1994 {
1995         fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
1996         fip->sol_time = jiffies;
1997 }
1998
1999 /**
2000  * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2001  * @fip: The FCoE controller
2002  * @rdata: parsed remote port with frport from the probe request
2003  *
2004  * Called with ctlr_mutex held.
2005  */
2006 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2007                                    struct fc_rport_priv *rdata)
2008 {
2009         struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2010
2011         if (rdata->ids.port_id != fip->port_id)
2012                 return;
2013
2014         switch (fip->state) {
2015         case FIP_ST_VNMP_CLAIM:
2016         case FIP_ST_VNMP_UP:
2017                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2018                                   frport->enode_mac, 0);
2019                 break;
2020         case FIP_ST_VNMP_PROBE1:
2021         case FIP_ST_VNMP_PROBE2:
2022                 /*
2023                  * Decide whether to reply to the Probe.
2024                  * Our selected address is never a "recorded" one, so
2025                  * only reply if our WWPN is greater and the
2026                  * Probe's REC bit is not set.
2027                  * If we don't reply, we will change our address.
2028                  */
2029                 if (fip->lp->wwpn > rdata->ids.port_name &&
2030                     !(frport->flags & FIP_FL_REC_OR_P2P)) {
2031                         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2032                                           frport->enode_mac, 0);
2033                         break;
2034                 }
2035                 /* fall through */
2036         case FIP_ST_VNMP_START:
2037                 fcoe_ctlr_vn_restart(fip);
2038                 break;
2039         default:
2040                 break;
2041         }
2042 }
2043
2044 /**
2045  * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2046  * @fip: The FCoE controller
2047  * @rdata: parsed remote port with frport from the probe request
2048  *
2049  * Called with ctlr_mutex held.
2050  */
2051 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2052                                    struct fc_rport_priv *rdata)
2053 {
2054         if (rdata->ids.port_id != fip->port_id)
2055                 return;
2056         switch (fip->state) {
2057         case FIP_ST_VNMP_START:
2058         case FIP_ST_VNMP_PROBE1:
2059         case FIP_ST_VNMP_PROBE2:
2060         case FIP_ST_VNMP_CLAIM:
2061                 fcoe_ctlr_vn_restart(fip);
2062                 break;
2063         case FIP_ST_VNMP_UP:
2064                 fcoe_ctlr_vn_send_claim(fip);
2065                 break;
2066         default:
2067                 break;
2068         }
2069 }
2070
2071 /**
2072  * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2073  * @fip: The FCoE controller
2074  * @new: newly-parsed remote port with frport as a template for new rdata
2075  *
2076  * Called with ctlr_mutex held.
2077  */
2078 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2079 {
2080         struct fc_lport *lport = fip->lp;
2081         struct fc_rport_priv *rdata;
2082         struct fc_rport_identifiers *ids;
2083         struct fcoe_rport *frport;
2084         u32 port_id;
2085
2086         port_id = new->ids.port_id;
2087         if (port_id == fip->port_id)
2088                 return;
2089
2090         mutex_lock(&lport->disc.disc_mutex);
2091         rdata = lport->tt.rport_create(lport, port_id);
2092         if (!rdata) {
2093                 mutex_unlock(&lport->disc.disc_mutex);
2094                 return;
2095         }
2096
2097         rdata->ops = &fcoe_ctlr_vn_rport_ops;
2098         rdata->disc_id = lport->disc.disc_id;
2099
2100         ids = &rdata->ids;
2101         if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2102             (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2103                 lport->tt.rport_logoff(rdata);
2104         ids->port_name = new->ids.port_name;
2105         ids->node_name = new->ids.node_name;
2106         mutex_unlock(&lport->disc.disc_mutex);
2107
2108         frport = fcoe_ctlr_rport(rdata);
2109         LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2110                         port_id, frport->fcoe_len ? "old" : "new");
2111         *frport = *fcoe_ctlr_rport(new);
2112         frport->time = 0;
2113 }
2114
2115 /**
2116  * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2117  * @fip: The FCoE controller
2118  * @port_id:  The port_id of the remote VN_node
2119  * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2120  *
2121  * Returns non-zero error if no remote port found.
2122  */
2123 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2124 {
2125         struct fc_lport *lport = fip->lp;
2126         struct fc_rport_priv *rdata;
2127         struct fcoe_rport *frport;
2128         int ret = -1;
2129
2130         rcu_read_lock();
2131         rdata = lport->tt.rport_lookup(lport, port_id);
2132         if (rdata) {
2133                 frport = fcoe_ctlr_rport(rdata);
2134                 memcpy(mac, frport->enode_mac, ETH_ALEN);
2135                 ret = 0;
2136         }
2137         rcu_read_unlock();
2138         return ret;
2139 }
2140
2141 /**
2142  * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2143  * @fip: The FCoE controller
2144  * @new: newly-parsed remote port with frport as a template for new rdata
2145  *
2146  * Called with ctlr_mutex held.
2147  */
2148 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2149                                       struct fc_rport_priv *new)
2150 {
2151         struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2152
2153         if (frport->flags & FIP_FL_REC_OR_P2P) {
2154                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2155                 return;
2156         }
2157         switch (fip->state) {
2158         case FIP_ST_VNMP_START:
2159         case FIP_ST_VNMP_PROBE1:
2160         case FIP_ST_VNMP_PROBE2:
2161                 if (new->ids.port_id == fip->port_id)
2162                         fcoe_ctlr_vn_restart(fip);
2163                 break;
2164         case FIP_ST_VNMP_CLAIM:
2165         case FIP_ST_VNMP_UP:
2166                 if (new->ids.port_id == fip->port_id) {
2167                         if (new->ids.port_name > fip->lp->wwpn) {
2168                                 fcoe_ctlr_vn_restart(fip);
2169                                 break;
2170                         }
2171                         fcoe_ctlr_vn_send_claim(fip);
2172                         break;
2173                 }
2174                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2175                                   min((u32)frport->fcoe_len,
2176                                       fcoe_ctlr_fcoe_size(fip)));
2177                 fcoe_ctlr_vn_add(fip, new);
2178                 break;
2179         default:
2180                 break;
2181         }
2182 }
2183
2184 /**
2185  * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2186  * @fip: The FCoE controller that received the frame
2187  * @new: newly-parsed remote port with frport from the Claim Response
2188  *
2189  * Called with ctlr_mutex held.
2190  */
2191 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2192                                     struct fc_rport_priv *new)
2193 {
2194         LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2195                         new->ids.port_id, fcoe_ctlr_state(fip->state));
2196         if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2197                 fcoe_ctlr_vn_add(fip, new);
2198 }
2199
2200 /**
2201  * fcoe_ctlr_vn_beacon() - handle received beacon.
2202  * @fip: The FCoE controller that received the frame
2203  * @new: newly-parsed remote port with frport from the Beacon
2204  *
2205  * Called with ctlr_mutex held.
2206  */
2207 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2208                                 struct fc_rport_priv *new)
2209 {
2210         struct fc_lport *lport = fip->lp;
2211         struct fc_rport_priv *rdata;
2212         struct fcoe_rport *frport;
2213
2214         frport = fcoe_ctlr_rport(new);
2215         if (frport->flags & FIP_FL_REC_OR_P2P) {
2216                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2217                 return;
2218         }
2219         mutex_lock(&lport->disc.disc_mutex);
2220         rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2221         if (rdata)
2222                 kref_get(&rdata->kref);
2223         mutex_unlock(&lport->disc.disc_mutex);
2224         if (rdata) {
2225                 if (rdata->ids.node_name == new->ids.node_name &&
2226                     rdata->ids.port_name == new->ids.port_name) {
2227                         frport = fcoe_ctlr_rport(rdata);
2228                         if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2229                                 lport->tt.rport_login(rdata);
2230                         frport->time = jiffies;
2231                 }
2232                 kref_put(&rdata->kref, lport->tt.rport_destroy);
2233                 return;
2234         }
2235         if (fip->state != FIP_ST_VNMP_UP)
2236                 return;
2237
2238         /*
2239          * Beacon from a new neighbor.
2240          * Send a claim notify if one hasn't been sent recently.
2241          * Don't add the neighbor yet.
2242          */
2243         LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2244                         new->ids.port_id);
2245         if (time_after(jiffies,
2246                        fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2247                 fcoe_ctlr_vn_send_claim(fip);
2248 }
2249
2250 /**
2251  * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2252  * @fip: The FCoE controller
2253  *
2254  * Called with ctlr_mutex held.
2255  * Called only in state FIP_ST_VNMP_UP.
2256  * Returns the soonest time for next age-out or a time far in the future.
2257  */
2258 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2259 {
2260         struct fc_lport *lport = fip->lp;
2261         struct fc_rport_priv *rdata;
2262         struct fcoe_rport *frport;
2263         unsigned long next_time;
2264         unsigned long deadline;
2265
2266         next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2267         mutex_lock(&lport->disc.disc_mutex);
2268         list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2269                 frport = fcoe_ctlr_rport(rdata);
2270                 if (!frport->time)
2271                         continue;
2272                 deadline = frport->time +
2273                            msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2274                 if (time_after_eq(jiffies, deadline)) {
2275                         frport->time = 0;
2276                         LIBFCOE_FIP_DBG(fip,
2277                                 "port %16.16llx fc_id %6.6x beacon expired\n",
2278                                 rdata->ids.port_name, rdata->ids.port_id);
2279                         lport->tt.rport_logoff(rdata);
2280                 } else if (time_before(deadline, next_time))
2281                         next_time = deadline;
2282         }
2283         mutex_unlock(&lport->disc.disc_mutex);
2284         return next_time;
2285 }
2286
2287 /**
2288  * fcoe_ctlr_vn_recv() - Receive a FIP frame
2289  * @fip: The FCoE controller that received the frame
2290  * @skb: The received FIP frame
2291  *
2292  * Returns non-zero if the frame is dropped.
2293  * Always consumes the frame.
2294  */
2295 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2296 {
2297         struct fip_header *fiph;
2298         enum fip_vn2vn_subcode sub;
2299         union {
2300                 struct fc_rport_priv rdata;
2301                 struct fcoe_rport frport;
2302         } buf;
2303         int rc;
2304
2305         fiph = (struct fip_header *)skb->data;
2306         sub = fiph->fip_subcode;
2307
2308         rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2309         if (rc) {
2310                 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2311                 goto drop;
2312         }
2313
2314         mutex_lock(&fip->ctlr_mutex);
2315         switch (sub) {
2316         case FIP_SC_VN_PROBE_REQ:
2317                 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2318                 break;
2319         case FIP_SC_VN_PROBE_REP:
2320                 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2321                 break;
2322         case FIP_SC_VN_CLAIM_NOTIFY:
2323                 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2324                 break;
2325         case FIP_SC_VN_CLAIM_REP:
2326                 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2327                 break;
2328         case FIP_SC_VN_BEACON:
2329                 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2330                 break;
2331         default:
2332                 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2333                 rc = -1;
2334                 break;
2335         }
2336         mutex_unlock(&fip->ctlr_mutex);
2337 drop:
2338         kfree_skb(skb);
2339         return rc;
2340 }
2341
2342 /**
2343  * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2344  * @lport: The local port
2345  * @fp: The received frame
2346  *
2347  * This should never be called since we don't see RSCNs or other
2348  * fabric-generated ELSes.
2349  */
2350 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2351 {
2352         struct fc_seq_els_data rjt_data;
2353
2354         rjt_data.reason = ELS_RJT_UNSUP;
2355         rjt_data.explan = ELS_EXPL_NONE;
2356         lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2357         fc_frame_free(fp);
2358 }
2359
2360 /**
2361  * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2362  * @fip: The FCoE controller
2363  *
2364  * This sets a flag indicating that remote ports should be created
2365  * and started for the peers we discover.  We use the disc_callback
2366  * pointer as that flag.  Peers already discovered are created here.
2367  *
2368  * The lport lock is held during this call. The callback must be done
2369  * later, without holding either the lport or discovery locks.
2370  * The fcoe_ctlr lock may also be held during this call.
2371  */
2372 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2373                                                   enum fc_disc_event),
2374                                  struct fc_lport *lport)
2375 {
2376         struct fc_disc *disc = &lport->disc;
2377         struct fcoe_ctlr *fip = disc->priv;
2378
2379         mutex_lock(&disc->disc_mutex);
2380         disc->disc_callback = callback;
2381         disc->disc_id = (disc->disc_id + 2) | 1;
2382         disc->pending = 1;
2383         schedule_work(&fip->timer_work);
2384         mutex_unlock(&disc->disc_mutex);
2385 }
2386
2387 /**
2388  * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2389  * @fip: The FCoE controller
2390  *
2391  * Starts the FLOGI and PLOGI login process to each discovered rport for which
2392  * we've received at least one beacon.
2393  * Performs the discovery complete callback.
2394  */
2395 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2396 {
2397         struct fc_lport *lport = fip->lp;
2398         struct fc_disc *disc = &lport->disc;
2399         struct fc_rport_priv *rdata;
2400         struct fcoe_rport *frport;
2401         void (*callback)(struct fc_lport *, enum fc_disc_event);
2402
2403         mutex_lock(&disc->disc_mutex);
2404         callback = disc->pending ? disc->disc_callback : NULL;
2405         disc->pending = 0;
2406         list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2407                 frport = fcoe_ctlr_rport(rdata);
2408                 if (frport->time)
2409                         lport->tt.rport_login(rdata);
2410         }
2411         mutex_unlock(&disc->disc_mutex);
2412         if (callback)
2413                 callback(lport, DISC_EV_SUCCESS);
2414 }
2415
2416 /**
2417  * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2418  * @fip: The FCoE controller
2419  */
2420 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2421 {
2422         unsigned long next_time;
2423         u8 mac[ETH_ALEN];
2424         u32 new_port_id = 0;
2425
2426         mutex_lock(&fip->ctlr_mutex);
2427         switch (fip->state) {
2428         case FIP_ST_VNMP_START:
2429                 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2430                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2431                 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2432                 break;
2433         case FIP_ST_VNMP_PROBE1:
2434                 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2435                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2436                 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2437                 break;
2438         case FIP_ST_VNMP_PROBE2:
2439                 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2440                 new_port_id = fip->port_id;
2441                 hton24(mac, FIP_VN_FC_MAP);
2442                 hton24(mac + 3, new_port_id);
2443                 fcoe_ctlr_map_dest(fip);
2444                 fip->update_mac(fip->lp, mac);
2445                 fcoe_ctlr_vn_send_claim(fip);
2446                 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2447                 break;
2448         case FIP_ST_VNMP_CLAIM:
2449                 /*
2450                  * This may be invoked either by starting discovery so don't
2451                  * go to the next state unless it's been long enough.
2452                  */
2453                 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2454                 if (time_after_eq(jiffies, next_time)) {
2455                         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2456                         fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2457                                           fcoe_all_vn2vn, 0);
2458                         next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2459                         fip->port_ka_time = next_time;
2460                 }
2461                 fcoe_ctlr_vn_disc(fip);
2462                 break;
2463         case FIP_ST_VNMP_UP:
2464                 next_time = fcoe_ctlr_vn_age(fip);
2465                 if (time_after_eq(jiffies, fip->port_ka_time)) {
2466                         fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2467                                           fcoe_all_vn2vn, 0);
2468                         fip->port_ka_time = jiffies +
2469                                  msecs_to_jiffies(FIP_VN_BEACON_INT +
2470                                         (random32() % FIP_VN_BEACON_FUZZ));
2471                 }
2472                 if (time_before(fip->port_ka_time, next_time))
2473                         next_time = fip->port_ka_time;
2474                 break;
2475         case FIP_ST_LINK_WAIT:
2476                 goto unlock;
2477         default:
2478                 WARN(1, "unexpected state %d", fip->state);
2479                 goto unlock;
2480         }
2481         mod_timer(&fip->timer, next_time);
2482 unlock:
2483         mutex_unlock(&fip->ctlr_mutex);
2484
2485         /* If port ID is new, notify local port after dropping ctlr_mutex */
2486         if (new_port_id)
2487                 fc_lport_set_local_id(fip->lp, new_port_id);
2488 }
2489
2490 /**
2491  * fcoe_libfc_config() - Sets up libfc related properties for local port
2492  * @lp: The local port to configure libfc for
2493  * @fip: The FCoE controller in use by the local port
2494  * @tt: The libfc function template
2495  * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2496  *
2497  * Returns : 0 for success
2498  */
2499 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2500                       const struct libfc_function_template *tt, int init_fcp)
2501 {
2502         /* Set the function pointers set by the LLDD */
2503         memcpy(&lport->tt, tt, sizeof(*tt));
2504         if (init_fcp && fc_fcp_init(lport))
2505                 return -ENOMEM;
2506         fc_exch_init(lport);
2507         fc_elsct_init(lport);
2508         fc_lport_init(lport);
2509         if (fip->mode == FIP_MODE_VN2VN)
2510                 lport->rport_priv_size = sizeof(struct fcoe_rport);
2511         fc_rport_init(lport);
2512         if (fip->mode == FIP_MODE_VN2VN) {
2513                 lport->point_to_multipoint = 1;
2514                 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2515                 lport->tt.disc_start = fcoe_ctlr_disc_start;
2516                 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2517                 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2518                 mutex_init(&lport->disc.disc_mutex);
2519                 INIT_LIST_HEAD(&lport->disc.rports);
2520                 lport->disc.priv = fip;
2521         } else {
2522                 fc_disc_init(lport);
2523         }
2524         return 0;
2525 }
2526 EXPORT_SYMBOL_GPL(fcoe_libfc_config);