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