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