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