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