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