xref: /linux/drivers/scsi/fcoe/fcoe.c (revision af1737dab07c861c3de4576f19dcfbd8f2329eb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
4  *
5  * Maintained at www.Open-FCoE.org
6  */
7 
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <linux/crc32.h>
16 #include <linux/slab.h>
17 #include <linux/cpu.h>
18 #include <linux/fs.h>
19 #include <linux/sysfs.h>
20 #include <linux/ctype.h>
21 #include <linux/workqueue.h>
22 #include <net/dcbnl.h>
23 #include <net/dcbevent.h>
24 #include <scsi/scsi_tcq.h>
25 #include <scsi/scsicam.h>
26 #include <scsi/scsi_transport.h>
27 #include <scsi/scsi_transport_fc.h>
28 #include <net/netdev_lock.h>
29 #include <net/rtnetlink.h>
30 
31 #include <scsi/fc/fc_encaps.h>
32 #include <scsi/fc/fc_fip.h>
33 #include <scsi/fc/fc_fcoe.h>
34 
35 #include <scsi/libfc.h>
36 #include <scsi/fc_frame.h>
37 #include <scsi/libfcoe.h>
38 
39 #include "fcoe.h"
40 
41 MODULE_AUTHOR("Open-FCoE.org");
42 MODULE_DESCRIPTION("FCoE");
43 MODULE_LICENSE("GPL v2");
44 
45 /* Performance tuning parameters for fcoe */
46 static unsigned int fcoe_ddp_min = 4096;
47 module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
48 MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for "	\
49 		 "Direct Data Placement (DDP).");
50 
51 unsigned int fcoe_debug_logging;
52 module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
53 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
54 
55 static unsigned int fcoe_e_d_tov = 2 * 1000;
56 module_param_named(e_d_tov, fcoe_e_d_tov, int, S_IRUGO|S_IWUSR);
57 MODULE_PARM_DESC(e_d_tov, "E_D_TOV in ms, default 2000");
58 
59 static unsigned int fcoe_r_a_tov = 2 * 2 * 1000;
60 module_param_named(r_a_tov, fcoe_r_a_tov, int, S_IRUGO|S_IWUSR);
61 MODULE_PARM_DESC(r_a_tov, "R_A_TOV in ms, default 4000");
62 
63 static DEFINE_MUTEX(fcoe_config_mutex);
64 
65 static struct workqueue_struct *fcoe_wq;
66 
67 /* fcoe host list */
68 /* must only by accessed under the RTNL mutex */
69 static LIST_HEAD(fcoe_hostlist);
70 static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
71 
72 /* Function Prototypes */
73 static int fcoe_reset(struct Scsi_Host *);
74 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
75 static int fcoe_rcv(struct sk_buff *, struct net_device *,
76 		    struct packet_type *, struct net_device *);
77 static void fcoe_percpu_clean(struct fc_lport *);
78 static int fcoe_link_ok(struct fc_lport *);
79 
80 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
81 static int fcoe_hostlist_add(const struct fc_lport *);
82 static void fcoe_hostlist_del(const struct fc_lport *);
83 
84 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
85 static void fcoe_dev_setup(void);
86 static void fcoe_dev_cleanup(void);
87 static struct fcoe_interface
88 *fcoe_hostlist_lookup_port(const struct net_device *);
89 
90 static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
91 			 struct packet_type *, struct net_device *);
92 static int fcoe_fip_vlan_recv(struct sk_buff *, struct net_device *,
93 			      struct packet_type *, struct net_device *);
94 
95 static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
96 static void fcoe_update_src_mac(struct fc_lport *, u8 *);
97 static u8 *fcoe_get_src_mac(struct fc_lport *);
98 static void fcoe_destroy_work(struct work_struct *);
99 
100 static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
101 			  unsigned int);
102 static int fcoe_ddp_done(struct fc_lport *, u16);
103 static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
104 			   unsigned int);
105 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
106 				     ulong event, void *ptr);
107 
108 static bool fcoe_match(struct net_device *netdev);
109 static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode);
110 static int fcoe_destroy(struct net_device *netdev);
111 static int fcoe_enable(struct net_device *netdev);
112 static int fcoe_disable(struct net_device *netdev);
113 
114 /* fcoe_syfs control interface handlers */
115 static int fcoe_ctlr_alloc(struct net_device *netdev);
116 static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev);
117 static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev);
118 
119 static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
120 				      u32 did, struct fc_frame *,
121 				      unsigned int op,
122 				      void (*resp)(struct fc_seq *,
123 						   struct fc_frame *,
124 						   void *),
125 				      void *, u32 timeout);
126 static void fcoe_recv_frame(struct sk_buff *skb);
127 
128 /* notification function for packets from net device */
129 static struct notifier_block fcoe_notifier = {
130 	.notifier_call = fcoe_device_notification,
131 };
132 
133 /* notification function for DCB events */
134 static struct notifier_block dcb_notifier = {
135 	.notifier_call = fcoe_dcb_app_notification,
136 };
137 
138 static struct scsi_transport_template *fcoe_nport_scsi_transport;
139 static struct scsi_transport_template *fcoe_vport_scsi_transport;
140 
141 static int fcoe_vport_destroy(struct fc_vport *);
142 static int fcoe_vport_create(struct fc_vport *, bool disabled);
143 static int fcoe_vport_disable(struct fc_vport *, bool disable);
144 static void fcoe_set_vport_symbolic_name(struct fc_vport *);
145 static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
146 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
147 static void fcoe_vport_remove(struct fc_lport *);
148 
149 static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
150 	.set_fcoe_ctlr_mode = fcoe_ctlr_mode,
151 	.set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
152 	.get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
153 	.get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
154 	.get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
155 	.get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
156 	.get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
157 	.get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
158 
159 	.get_fcoe_fcf_selected = fcoe_fcf_get_selected,
160 	.get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
161 };
162 
163 static struct libfc_function_template fcoe_libfc_fcn_templ = {
164 	.frame_send = fcoe_xmit,
165 	.ddp_setup = fcoe_ddp_setup,
166 	.ddp_done = fcoe_ddp_done,
167 	.ddp_target = fcoe_ddp_target,
168 	.elsct_send = fcoe_elsct_send,
169 	.get_lesb = fcoe_get_lesb,
170 	.lport_set_port_id = fcoe_set_port_id,
171 };
172 
173 static struct fc_function_template fcoe_nport_fc_functions = {
174 	.show_host_node_name = 1,
175 	.show_host_port_name = 1,
176 	.show_host_supported_classes = 1,
177 	.show_host_supported_fc4s = 1,
178 	.show_host_active_fc4s = 1,
179 	.show_host_maxframe_size = 1,
180 	.show_host_serial_number = 1,
181 	.show_host_manufacturer = 1,
182 	.show_host_model = 1,
183 	.show_host_model_description = 1,
184 	.show_host_hardware_version = 1,
185 	.show_host_driver_version = 1,
186 	.show_host_firmware_version = 1,
187 	.show_host_optionrom_version = 1,
188 
189 	.show_host_port_id = 1,
190 	.show_host_supported_speeds = 1,
191 	.get_host_speed = fc_get_host_speed,
192 	.show_host_speed = 1,
193 	.show_host_port_type = 1,
194 	.get_host_port_state = fc_get_host_port_state,
195 	.show_host_port_state = 1,
196 	.show_host_symbolic_name = 1,
197 
198 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
199 	.show_rport_maxframe_size = 1,
200 	.show_rport_supported_classes = 1,
201 
202 	.show_host_fabric_name = 1,
203 	.show_starget_node_name = 1,
204 	.show_starget_port_name = 1,
205 	.show_starget_port_id = 1,
206 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
207 	.show_rport_dev_loss_tmo = 1,
208 	.get_fc_host_stats = fc_get_host_stats,
209 	.issue_fc_host_lip = fcoe_reset,
210 
211 	.terminate_rport_io = fc_rport_terminate_io,
212 
213 	.vport_create = fcoe_vport_create,
214 	.vport_delete = fcoe_vport_destroy,
215 	.vport_disable = fcoe_vport_disable,
216 	.set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
217 
218 	.bsg_request = fc_lport_bsg_request,
219 };
220 
221 static struct fc_function_template fcoe_vport_fc_functions = {
222 	.show_host_node_name = 1,
223 	.show_host_port_name = 1,
224 	.show_host_supported_classes = 1,
225 	.show_host_supported_fc4s = 1,
226 	.show_host_active_fc4s = 1,
227 	.show_host_maxframe_size = 1,
228 	.show_host_serial_number = 1,
229 	.show_host_manufacturer = 1,
230 	.show_host_model = 1,
231 	.show_host_model_description = 1,
232 	.show_host_hardware_version = 1,
233 	.show_host_driver_version = 1,
234 	.show_host_firmware_version = 1,
235 	.show_host_optionrom_version = 1,
236 
237 	.show_host_port_id = 1,
238 	.show_host_supported_speeds = 1,
239 	.get_host_speed = fc_get_host_speed,
240 	.show_host_speed = 1,
241 	.show_host_port_type = 1,
242 	.get_host_port_state = fc_get_host_port_state,
243 	.show_host_port_state = 1,
244 	.show_host_symbolic_name = 1,
245 
246 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
247 	.show_rport_maxframe_size = 1,
248 	.show_rport_supported_classes = 1,
249 
250 	.show_host_fabric_name = 1,
251 	.show_starget_node_name = 1,
252 	.show_starget_port_name = 1,
253 	.show_starget_port_id = 1,
254 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
255 	.show_rport_dev_loss_tmo = 1,
256 	.get_fc_host_stats = fc_get_host_stats,
257 	.issue_fc_host_lip = fcoe_reset,
258 
259 	.terminate_rport_io = fc_rport_terminate_io,
260 
261 	.bsg_request = fc_lport_bsg_request,
262 };
263 
264 static const struct scsi_host_template fcoe_shost_template = {
265 	.module = THIS_MODULE,
266 	.name = "FCoE Driver",
267 	.proc_name = FCOE_NAME,
268 	.queuecommand = fc_queuecommand,
269 	.eh_timed_out = fc_eh_timed_out,
270 	.eh_abort_handler = fc_eh_abort,
271 	.eh_device_reset_handler = fc_eh_device_reset,
272 	.eh_host_reset_handler = fc_eh_host_reset,
273 	.sdev_init = fc_sdev_init,
274 	.change_queue_depth = scsi_change_queue_depth,
275 	.this_id = -1,
276 	.cmd_per_lun = 3,
277 	.can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
278 	.sg_tablesize = SG_ALL,
279 	.max_sectors = 0xffff,
280 	.track_queue_depth = 1,
281 	.cmd_size = sizeof(struct libfc_cmd_priv),
282 };
283 
284 /**
285  * fcoe_interface_setup() - Setup a FCoE interface
286  * @fcoe:   The new FCoE interface
287  * @netdev: The net device that the fcoe interface is on
288  *
289  * Returns : 0 for success
290  * Locking: must be called with the RTNL mutex held
291  */
292 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
293 				struct net_device *netdev)
294 {
295 	struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
296 	struct netdev_hw_addr *ha;
297 	struct net_device *real_dev;
298 	static const u8 flogi_maddr[ETH_ALEN] = FC_FCOE_FLOGI_MAC;
299 	const struct net_device_ops *ops;
300 
301 	fcoe->netdev = netdev;
302 
303 	/* Let LLD initialize for FCoE */
304 	ops = netdev->netdev_ops;
305 	if (ops->ndo_fcoe_enable) {
306 		if (ops->ndo_fcoe_enable(netdev))
307 			FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
308 					" specific feature for LLD.\n");
309 	}
310 
311 	/* Do not support for bonding device */
312 	if (netif_is_bond_master(netdev)) {
313 		FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
314 		return -EOPNOTSUPP;
315 	}
316 
317 	/* look for SAN MAC address, if multiple SAN MACs exist, only
318 	 * use the first one for SPMA */
319 	real_dev = is_vlan_dev(netdev) ? vlan_dev_real_dev(netdev) : netdev;
320 	fcoe->realdev = real_dev;
321 	rcu_read_lock();
322 	for_each_dev_addr(real_dev, ha) {
323 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
324 		    (is_valid_ether_addr(ha->addr))) {
325 			memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
326 			fip->spma = 1;
327 			break;
328 		}
329 	}
330 	rcu_read_unlock();
331 
332 	/* setup Source Mac Address */
333 	if (!fip->spma)
334 		memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
335 
336 	/*
337 	 * Add FCoE MAC address as second unicast MAC address
338 	 * or enter promiscuous mode if not capable of listening
339 	 * for multiple unicast MACs.
340 	 */
341 	dev_uc_add(netdev, flogi_maddr);
342 	if (fip->spma)
343 		dev_uc_add(netdev, fip->ctl_src_addr);
344 	if (fip->mode == FIP_MODE_VN2VN) {
345 		dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
346 		dev_mc_add(netdev, FIP_ALL_P2P_MACS);
347 	} else
348 		dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
349 
350 	/*
351 	 * setup the receive function from ethernet driver
352 	 * on the ethertype for the given device
353 	 */
354 	fcoe->fcoe_packet_type.func = fcoe_rcv;
355 	fcoe->fcoe_packet_type.type = htons(ETH_P_FCOE);
356 	fcoe->fcoe_packet_type.dev = netdev;
357 	dev_add_pack(&fcoe->fcoe_packet_type);
358 
359 	fcoe->fip_packet_type.func = fcoe_fip_recv;
360 	fcoe->fip_packet_type.type = htons(ETH_P_FIP);
361 	fcoe->fip_packet_type.dev = netdev;
362 	dev_add_pack(&fcoe->fip_packet_type);
363 
364 	if (netdev != real_dev) {
365 		fcoe->fip_vlan_packet_type.func = fcoe_fip_vlan_recv;
366 		fcoe->fip_vlan_packet_type.type = htons(ETH_P_FIP);
367 		fcoe->fip_vlan_packet_type.dev = real_dev;
368 		dev_add_pack(&fcoe->fip_vlan_packet_type);
369 	}
370 	return 0;
371 }
372 
373 /**
374  * fcoe_interface_create() - Create a FCoE interface on a net device
375  * @netdev: The net device to create the FCoE interface on
376  * @fip_mode: The mode to use for FIP
377  *
378  * Returns: pointer to a struct fcoe_interface or NULL on error
379  */
380 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
381 						    enum fip_mode fip_mode)
382 {
383 	struct fcoe_ctlr_device *ctlr_dev;
384 	struct fcoe_ctlr *ctlr;
385 	struct fcoe_interface *fcoe;
386 	int size;
387 	int err;
388 
389 	if (!try_module_get(THIS_MODULE)) {
390 		FCOE_NETDEV_DBG(netdev,
391 				"Could not get a reference to the module\n");
392 		fcoe = ERR_PTR(-EBUSY);
393 		goto out;
394 	}
395 
396 	size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
397 	ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
398 					size);
399 	if (!ctlr_dev) {
400 		FCOE_DBG("Failed to add fcoe_ctlr_device\n");
401 		fcoe = ERR_PTR(-ENOMEM);
402 		goto out_putmod;
403 	}
404 
405 	ctlr = fcoe_ctlr_device_priv(ctlr_dev);
406 	ctlr->cdev = ctlr_dev;
407 	fcoe = fcoe_ctlr_priv(ctlr);
408 
409 	dev_hold(netdev);
410 
411 	/*
412 	 * Initialize FIP.
413 	 */
414 	fcoe_ctlr_init(ctlr, fip_mode);
415 	ctlr->send = fcoe_fip_send;
416 	ctlr->update_mac = fcoe_update_src_mac;
417 	ctlr->get_src_addr = fcoe_get_src_mac;
418 
419 	err = fcoe_interface_setup(fcoe, netdev);
420 	if (err) {
421 		fcoe_ctlr_destroy(ctlr);
422 		fcoe_ctlr_device_delete(ctlr_dev);
423 		dev_put(netdev);
424 		fcoe = ERR_PTR(err);
425 		goto out_putmod;
426 	}
427 
428 	goto out;
429 
430 out_putmod:
431 	module_put(THIS_MODULE);
432 out:
433 	return fcoe;
434 }
435 
436 /**
437  * fcoe_interface_remove() - remove FCoE interface from netdev
438  * @fcoe: The FCoE interface to be cleaned up
439  *
440  * Caller must be holding the RTNL mutex
441  */
442 static void fcoe_interface_remove(struct fcoe_interface *fcoe)
443 {
444 	struct net_device *netdev = fcoe->netdev;
445 	struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
446 	static const u8 flogi_maddr[ETH_ALEN] = FC_FCOE_FLOGI_MAC;
447 	const struct net_device_ops *ops;
448 
449 	/*
450 	 * Don't listen for Ethernet packets anymore.
451 	 * synchronize_net() ensures that the packet handlers are not running
452 	 * on another CPU. dev_remove_pack() would do that, this calls the
453 	 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
454 	 */
455 	__dev_remove_pack(&fcoe->fcoe_packet_type);
456 	__dev_remove_pack(&fcoe->fip_packet_type);
457 	if (netdev != fcoe->realdev)
458 		__dev_remove_pack(&fcoe->fip_vlan_packet_type);
459 	synchronize_net();
460 
461 	/* Delete secondary MAC addresses */
462 	dev_uc_del(netdev, flogi_maddr);
463 	if (fip->spma)
464 		dev_uc_del(netdev, fip->ctl_src_addr);
465 	if (fip->mode == FIP_MODE_VN2VN) {
466 		dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
467 		dev_mc_del(netdev, FIP_ALL_P2P_MACS);
468 	} else
469 		dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
470 
471 	/* Tell the LLD we are done w/ FCoE */
472 	ops = netdev->netdev_ops;
473 	if (ops->ndo_fcoe_disable) {
474 		if (ops->ndo_fcoe_disable(netdev))
475 			FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
476 					" specific feature for LLD.\n");
477 	}
478 	fcoe->removed = 1;
479 }
480 
481 
482 /**
483  * fcoe_interface_cleanup() - Clean up a FCoE interface
484  * @fcoe: The FCoE interface to be cleaned up
485  */
486 static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
487 {
488 	struct net_device *netdev = fcoe->netdev;
489 	struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
490 
491 	/* Release the self-reference taken during fcoe_interface_create() */
492 	/* tear-down the FCoE controller */
493 	fcoe_ctlr_destroy(fip);
494 	scsi_host_put(fip->lp->host);
495 	dev_put(netdev);
496 	module_put(THIS_MODULE);
497 }
498 
499 /**
500  * fcoe_fip_recv() - Handler for received FIP frames
501  * @skb:      The receive skb
502  * @netdev:   The associated net device
503  * @ptype:    The packet_type structure which was used to register this handler
504  * @orig_dev: The original net_device the skb was received on.
505  *	      (in case dev is a bond)
506  *
507  * Returns: 0 for success
508  */
509 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
510 			 struct packet_type *ptype,
511 			 struct net_device *orig_dev)
512 {
513 	struct fcoe_interface *fcoe;
514 	struct fcoe_ctlr *ctlr;
515 
516 	fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
517 	ctlr = fcoe_to_ctlr(fcoe);
518 	fcoe_ctlr_recv(ctlr, skb);
519 	return 0;
520 }
521 
522 /**
523  * fcoe_fip_vlan_recv() - Handler for received FIP VLAN discovery frames
524  * @skb:      The receive skb
525  * @netdev:   The associated net device
526  * @ptype:    The packet_type structure which was used to register this handler
527  * @orig_dev: The original net_device the skb was received on.
528  *	      (in case dev is a bond)
529  *
530  * Returns: 0 for success
531  */
532 static int fcoe_fip_vlan_recv(struct sk_buff *skb, struct net_device *netdev,
533 			      struct packet_type *ptype,
534 			      struct net_device *orig_dev)
535 {
536 	struct fcoe_interface *fcoe;
537 	struct fcoe_ctlr *ctlr;
538 
539 	fcoe = container_of(ptype, struct fcoe_interface, fip_vlan_packet_type);
540 	ctlr = fcoe_to_ctlr(fcoe);
541 	fcoe_ctlr_recv(ctlr, skb);
542 	return 0;
543 }
544 
545 /**
546  * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
547  * @port: The FCoE port
548  * @skb: The FIP/FCoE packet to be sent
549  */
550 static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
551 {
552 	if (port->fcoe_pending_queue.qlen)
553 		fcoe_check_wait_queue(port->lport, skb);
554 	else if (fcoe_start_io(skb))
555 		fcoe_check_wait_queue(port->lport, skb);
556 }
557 
558 /**
559  * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
560  * @fip: The FCoE controller
561  * @skb: The FIP packet to be sent
562  */
563 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
564 {
565 	struct fcoe_interface *fcoe = fcoe_from_ctlr(fip);
566 	struct fip_frame {
567 		struct ethhdr eth;
568 		struct fip_header fip;
569 	} __packed *frame;
570 
571 	/*
572 	 * Use default VLAN for FIP VLAN discovery protocol
573 	 */
574 	frame = (struct fip_frame *)skb->data;
575 	if (ntohs(frame->eth.h_proto) == ETH_P_FIP &&
576 	    ntohs(frame->fip.fip_op) == FIP_OP_VLAN &&
577 	    fcoe->realdev != fcoe->netdev)
578 		skb->dev = fcoe->realdev;
579 	else
580 		skb->dev = fcoe->netdev;
581 	fcoe_port_send(lport_priv(fip->lp), skb);
582 }
583 
584 /**
585  * fcoe_update_src_mac() - Update the Ethernet MAC filters
586  * @lport: The local port to update the source MAC on
587  * @addr:  Unicast MAC address to add
588  *
589  * Remove any previously-set unicast MAC filter.
590  * Add secondary FCoE MAC address filter for our OUI.
591  */
592 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
593 {
594 	struct fcoe_port *port = lport_priv(lport);
595 	struct fcoe_interface *fcoe = port->priv;
596 
597 	if (!is_zero_ether_addr(port->data_src_addr))
598 		dev_uc_del(fcoe->netdev, port->data_src_addr);
599 	if (!is_zero_ether_addr(addr))
600 		dev_uc_add(fcoe->netdev, addr);
601 	memcpy(port->data_src_addr, addr, ETH_ALEN);
602 }
603 
604 /**
605  * fcoe_get_src_mac() - return the Ethernet source address for an lport
606  * @lport: libfc lport
607  */
608 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
609 {
610 	struct fcoe_port *port = lport_priv(lport);
611 
612 	return port->data_src_addr;
613 }
614 
615 /**
616  * fcoe_lport_config() - Set up a local port
617  * @lport: The local port to be setup
618  *
619  * Returns: 0 for success
620  */
621 static int fcoe_lport_config(struct fc_lport *lport)
622 {
623 	lport->link_up = 0;
624 	lport->qfull = 0;
625 	lport->max_retry_count = 3;
626 	lport->max_rport_retry_count = 3;
627 	lport->e_d_tov = fcoe_e_d_tov;
628 	lport->r_a_tov = fcoe_r_a_tov;
629 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
630 				 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
631 	lport->does_npiv = 1;
632 
633 	fc_lport_init_stats(lport);
634 
635 	/* lport fc_lport related configuration */
636 	fc_lport_config(lport);
637 
638 	/* offload related configuration */
639 	lport->crc_offload = 0;
640 	lport->seq_offload = 0;
641 	lport->lro_enabled = 0;
642 	lport->lro_xid = 0;
643 	lport->lso_max = 0;
644 
645 	return 0;
646 }
647 
648 /*
649  * fcoe_netdev_features_change - Updates the lport's offload flags based
650  * on the LLD netdev's FCoE feature flags
651  */
652 static void fcoe_netdev_features_change(struct fc_lport *lport,
653 					struct net_device *netdev)
654 {
655 	mutex_lock(&lport->lp_mutex);
656 
657 	if (netdev->features & NETIF_F_SG)
658 		lport->sg_supp = 1;
659 	else
660 		lport->sg_supp = 0;
661 
662 	if (netdev->features & NETIF_F_FCOE_CRC) {
663 		lport->crc_offload = 1;
664 		FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
665 	} else {
666 		lport->crc_offload = 0;
667 	}
668 
669 	if (netdev->features & NETIF_F_FSO) {
670 		lport->seq_offload = 1;
671 		lport->lso_max = min(netdev->gso_max_size, GSO_LEGACY_MAX_SIZE);
672 		FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
673 				lport->lso_max);
674 	} else {
675 		lport->seq_offload = 0;
676 		lport->lso_max = 0;
677 	}
678 
679 	if (netdev->fcoe_ddp_xid) {
680 		lport->lro_enabled = 1;
681 		lport->lro_xid = netdev->fcoe_ddp_xid;
682 		FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
683 				lport->lro_xid);
684 	} else {
685 		lport->lro_enabled = 0;
686 		lport->lro_xid = 0;
687 	}
688 
689 	mutex_unlock(&lport->lp_mutex);
690 }
691 
692 /**
693  * fcoe_netdev_config() - Set up net devive for SW FCoE
694  * @lport:  The local port that is associated with the net device
695  * @netdev: The associated net device
696  *
697  * Must be called after fcoe_lport_config() as it will use local port mutex
698  *
699  * Returns: 0 for success
700  */
701 static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
702 {
703 	u32 mfs;
704 	u64 wwnn, wwpn;
705 	struct fcoe_interface *fcoe;
706 	struct fcoe_ctlr *ctlr;
707 	struct fcoe_port *port;
708 
709 	/* Setup lport private data to point to fcoe softc */
710 	port = lport_priv(lport);
711 	fcoe = port->priv;
712 	ctlr = fcoe_to_ctlr(fcoe);
713 
714 	/* Figure out the VLAN ID, if any */
715 	if (is_vlan_dev(netdev))
716 		lport->vlan = vlan_dev_vlan_id(netdev);
717 	else
718 		lport->vlan = 0;
719 
720 	/*
721 	 * Determine max frame size based on underlying device and optional
722 	 * user-configured limit.  If the MFS is too low, fcoe_link_ok()
723 	 * will return 0, so do this first.
724 	 */
725 	mfs = netdev->mtu;
726 	if (netdev->fcoe_mtu) {
727 		mfs = FCOE_MTU;
728 		FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
729 	}
730 	mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
731 	if (fc_set_mfs(lport, mfs))
732 		return -EINVAL;
733 
734 	/* offload features support */
735 	fcoe_netdev_features_change(lport, netdev);
736 
737 	skb_queue_head_init(&port->fcoe_pending_queue);
738 	port->fcoe_pending_queue_active = 0;
739 	timer_setup(&port->timer, fcoe_queue_timer, 0);
740 
741 	netdev_lock_ops(netdev);
742 	fcoe_link_speed_update(lport);
743 	netdev_unlock_ops(netdev);
744 
745 	if (!lport->vport) {
746 		if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
747 			wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
748 		fc_set_wwnn(lport, wwnn);
749 		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
750 			wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
751 						 2, 0);
752 		fc_set_wwpn(lport, wwpn);
753 	}
754 
755 	return 0;
756 }
757 
758 /**
759  * fcoe_shost_config() - Set up the SCSI host associated with a local port
760  * @lport: The local port
761  * @dev:   The device associated with the SCSI host
762  *
763  * Must be called after fcoe_lport_config() and fcoe_netdev_config()
764  *
765  * Returns: 0 for success
766  */
767 static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
768 {
769 	int rc = 0;
770 
771 	/* lport scsi host config */
772 	lport->host->max_lun = FCOE_MAX_LUN;
773 	lport->host->max_id = FCOE_MAX_FCP_TARGET;
774 	lport->host->max_channel = 0;
775 	lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
776 
777 	if (lport->vport)
778 		lport->host->transportt = fcoe_vport_scsi_transport;
779 	else
780 		lport->host->transportt = fcoe_nport_scsi_transport;
781 
782 	/* add the new host to the SCSI-ml */
783 	rc = scsi_add_host(lport->host, dev);
784 	if (rc) {
785 		FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
786 				"error on scsi_add_host\n");
787 		return rc;
788 	}
789 
790 	if (!lport->vport)
791 		fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
792 
793 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
794 		 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
795 		 fcoe_netdev(lport)->name);
796 
797 	return 0;
798 }
799 
800 
801 /**
802  * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
803  * @lport:  The local port that is associated with the net device
804  * @netdev: The associated net device
805  *
806  * Must be called after fcoe_shost_config() as it will use local port mutex
807  *
808  */
809 static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
810 {
811 	struct fcoe_interface *fcoe;
812 	struct fcoe_port *port;
813 	struct net_device *realdev;
814 	int rc;
815 
816 	port = lport_priv(lport);
817 	fcoe = port->priv;
818 	realdev = fcoe->realdev;
819 
820 	/* No FDMI state m/c for NPIV ports */
821 	if (lport->vport)
822 		return;
823 
824 	if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
825 		struct netdev_fcoe_hbainfo *fdmi;
826 		fdmi = kzalloc_obj(*fdmi);
827 		if (!fdmi)
828 			return;
829 
830 		rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
831 							       fdmi);
832 		if (rc) {
833 			printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
834 					"information from netdev.\n");
835 			return;
836 		}
837 
838 		snprintf(fc_host_serial_number(lport->host),
839 			 FC_SERIAL_NUMBER_SIZE,
840 			 "%s",
841 			 fdmi->serial_number);
842 		snprintf(fc_host_manufacturer(lport->host),
843 			 FC_SERIAL_NUMBER_SIZE,
844 			 "%s",
845 			 fdmi->manufacturer);
846 		snprintf(fc_host_model(lport->host),
847 			 FC_SYMBOLIC_NAME_SIZE,
848 			 "%s",
849 			 fdmi->model);
850 		snprintf(fc_host_model_description(lport->host),
851 			 FC_SYMBOLIC_NAME_SIZE,
852 			 "%s",
853 			 fdmi->model_description);
854 		snprintf(fc_host_hardware_version(lport->host),
855 			 FC_VERSION_STRING_SIZE,
856 			 "%s",
857 			 fdmi->hardware_version);
858 		snprintf(fc_host_driver_version(lport->host),
859 			 FC_VERSION_STRING_SIZE,
860 			 "%s",
861 			 fdmi->driver_version);
862 		snprintf(fc_host_optionrom_version(lport->host),
863 			 FC_VERSION_STRING_SIZE,
864 			 "%s",
865 			 fdmi->optionrom_version);
866 		snprintf(fc_host_firmware_version(lport->host),
867 			 FC_VERSION_STRING_SIZE,
868 			 "%s",
869 			 fdmi->firmware_version);
870 
871 		/* Enable FDMI lport states */
872 		lport->fdmi_enabled = 1;
873 		kfree(fdmi);
874 	} else {
875 		lport->fdmi_enabled = 0;
876 		printk(KERN_INFO "fcoe: No FDMI support.\n");
877 	}
878 }
879 
880 /**
881  * fcoe_oem_match() - The match routine for the offloaded exchange manager
882  * @fp: The I/O frame
883  *
884  * This routine will be associated with an exchange manager (EM). When
885  * the libfc exchange handling code is looking for an EM to use it will
886  * call this routine and pass it the frame that it wishes to send. This
887  * routine will return True if the associated EM is to be used and False
888  * if the echange code should continue looking for an EM.
889  *
890  * The offload EM that this routine is associated with will handle any
891  * packets that are for SCSI read requests.
892  *
893  * This has been enhanced to work when FCoE stack is operating in target
894  * mode.
895  *
896  * Returns: True for read types I/O, otherwise returns false.
897  */
898 static bool fcoe_oem_match(struct fc_frame *fp)
899 {
900 	struct fc_frame_header *fh = fc_frame_header_get(fp);
901 	struct fcp_cmnd *fcp;
902 
903 	if (fc_fcp_is_read(fr_fsp(fp)) &&
904 	    (fr_fsp(fp)->data_len > fcoe_ddp_min))
905 		return true;
906 	else if ((fr_fsp(fp) == NULL) &&
907 		 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
908 		 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
909 		fcp = fc_frame_payload_get(fp, sizeof(*fcp));
910 		if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
911 		    (ntohl(fcp->fc_dl) > fcoe_ddp_min))
912 			return true;
913 	}
914 	return false;
915 }
916 
917 /**
918  * fcoe_em_config() - Allocate and configure an exchange manager
919  * @lport: The local port that the new EM will be associated with
920  *
921  * Returns: 0 on success
922  */
923 static inline int fcoe_em_config(struct fc_lport *lport)
924 {
925 	struct fcoe_port *port = lport_priv(lport);
926 	struct fcoe_interface *fcoe = port->priv;
927 	struct fcoe_interface *oldfcoe = NULL;
928 	struct net_device *old_real_dev, *cur_real_dev;
929 	u16 min_xid = FCOE_MIN_XID;
930 	u16 max_xid = FCOE_MAX_XID;
931 
932 	/*
933 	 * Check if need to allocate an em instance for
934 	 * offload exchange ids to be shared across all VN_PORTs/lport.
935 	 */
936 	if (!lport->lro_enabled || !lport->lro_xid ||
937 	    (lport->lro_xid >= max_xid)) {
938 		lport->lro_xid = 0;
939 		goto skip_oem;
940 	}
941 
942 	/*
943 	 * Reuse existing offload em instance in case
944 	 * it is already allocated on real eth device
945 	 */
946 	if (is_vlan_dev(fcoe->netdev))
947 		cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
948 	else
949 		cur_real_dev = fcoe->netdev;
950 
951 	list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
952 		if (is_vlan_dev(oldfcoe->netdev))
953 			old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
954 		else
955 			old_real_dev = oldfcoe->netdev;
956 
957 		if (cur_real_dev == old_real_dev) {
958 			fcoe->oem = oldfcoe->oem;
959 			break;
960 		}
961 	}
962 
963 	if (fcoe->oem) {
964 		if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
965 			printk(KERN_ERR "fcoe_em_config: failed to add "
966 			       "offload em:%p on interface:%s\n",
967 			       fcoe->oem, fcoe->netdev->name);
968 			return -ENOMEM;
969 		}
970 	} else {
971 		fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
972 					      FCOE_MIN_XID, lport->lro_xid,
973 					      fcoe_oem_match);
974 		if (!fcoe->oem) {
975 			printk(KERN_ERR "fcoe_em_config: failed to allocate "
976 			       "em for offload exches on interface:%s\n",
977 			       fcoe->netdev->name);
978 			return -ENOMEM;
979 		}
980 	}
981 
982 	/*
983 	 * Exclude offload EM xid range from next EM xid range.
984 	 */
985 	min_xid += lport->lro_xid + 1;
986 
987 skip_oem:
988 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
989 		printk(KERN_ERR "fcoe_em_config: failed to "
990 		       "allocate em on interface %s\n", fcoe->netdev->name);
991 		return -ENOMEM;
992 	}
993 
994 	return 0;
995 }
996 
997 /**
998  * fcoe_if_destroy() - Tear down a SW FCoE instance
999  * @lport: The local port to be destroyed
1000  *
1001  * Locking: Must be called with the RTNL mutex held.
1002  *
1003  */
1004 static void fcoe_if_destroy(struct fc_lport *lport)
1005 {
1006 	struct fcoe_port *port = lport_priv(lport);
1007 	struct fcoe_interface *fcoe = port->priv;
1008 	struct net_device *netdev = fcoe->netdev;
1009 
1010 	FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
1011 
1012 	/* Logout of the fabric */
1013 	fc_fabric_logoff(lport);
1014 
1015 	/* Cleanup the fc_lport */
1016 	fc_lport_destroy(lport);
1017 
1018 	/* Stop the transmit retry timer */
1019 	timer_delete_sync(&port->timer);
1020 
1021 	/* Free existing transmit skbs */
1022 	fcoe_clean_pending_queue(lport);
1023 
1024 	if (!is_zero_ether_addr(port->data_src_addr))
1025 		dev_uc_del(netdev, port->data_src_addr);
1026 	if (lport->vport)
1027 		synchronize_net();
1028 	else
1029 		fcoe_interface_remove(fcoe);
1030 
1031 	/* Free queued packets for the per-CPU receive threads */
1032 	fcoe_percpu_clean(lport);
1033 
1034 	/* Detach from the scsi-ml */
1035 	fc_remove_host(lport->host);
1036 	scsi_remove_host(lport->host);
1037 
1038 	/* Destroy lport scsi_priv */
1039 	fc_fcp_destroy(lport);
1040 
1041 	/* There are no more rports or I/O, free the EM */
1042 	fc_exch_mgr_free(lport);
1043 
1044 	/* Free memory used by statistical counters */
1045 	fc_lport_free_stats(lport);
1046 
1047 	/*
1048 	 * Release the Scsi_Host for vport but hold on to
1049 	 * master lport until it fcoe interface fully cleaned-up.
1050 	 */
1051 	if (lport->vport)
1052 		scsi_host_put(lport->host);
1053 }
1054 
1055 /**
1056  * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1057  * @lport: The local port to setup DDP for
1058  * @xid:   The exchange ID for this DDP transfer
1059  * @sgl:   The scatterlist describing this transfer
1060  * @sgc:   The number of sg items
1061  *
1062  * Returns: 0 if the DDP context was not configured
1063  */
1064 static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1065 			  struct scatterlist *sgl, unsigned int sgc)
1066 {
1067 	struct net_device *netdev = fcoe_netdev(lport);
1068 
1069 	if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1070 		return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1071 							      xid, sgl,
1072 							      sgc);
1073 
1074 	return 0;
1075 }
1076 
1077 /**
1078  * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1079  * @lport: The local port to setup DDP for
1080  * @xid:   The exchange ID for this DDP transfer
1081  * @sgl:   The scatterlist describing this transfer
1082  * @sgc:   The number of sg items
1083  *
1084  * Returns: 0 if the DDP context was not configured
1085  */
1086 static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1087 			   struct scatterlist *sgl, unsigned int sgc)
1088 {
1089 	struct net_device *netdev = fcoe_netdev(lport);
1090 
1091 	if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1092 		return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1093 							       sgl, sgc);
1094 
1095 	return 0;
1096 }
1097 
1098 
1099 /**
1100  * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1101  * @lport: The local port to complete DDP on
1102  * @xid:   The exchange ID for this DDP transfer
1103  *
1104  * Returns: the length of data that have been completed by DDP
1105  */
1106 static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1107 {
1108 	struct net_device *netdev = fcoe_netdev(lport);
1109 
1110 	if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1111 		return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1112 	return 0;
1113 }
1114 
1115 /**
1116  * fcoe_if_create() - Create a FCoE instance on an interface
1117  * @fcoe:   The FCoE interface to create a local port on
1118  * @parent: The device pointer to be the parent in sysfs for the SCSI host
1119  * @npiv:   Indicates if the port is a vport or not
1120  *
1121  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1122  *
1123  * Returns: The allocated fc_lport or an error pointer
1124  */
1125 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
1126 				       struct device *parent, int npiv)
1127 {
1128 	struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1129 	struct net_device *netdev = fcoe->netdev;
1130 	struct fc_lport *lport, *n_port;
1131 	struct fcoe_port *port;
1132 	struct Scsi_Host *shost;
1133 	int rc;
1134 	/*
1135 	 * parent is only a vport if npiv is 1,
1136 	 * but we'll only use vport in that case so go ahead and set it
1137 	 */
1138 	struct fc_vport *vport = dev_to_vport(parent);
1139 
1140 	FCOE_NETDEV_DBG(netdev, "Create Interface\n");
1141 
1142 	if (!npiv)
1143 		lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1144 	else
1145 		lport = libfc_vport_create(vport, sizeof(*port));
1146 
1147 	if (!lport) {
1148 		FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1149 		rc = -ENOMEM;
1150 		goto out;
1151 	}
1152 	port = lport_priv(lport);
1153 	port->lport = lport;
1154 	port->priv = fcoe;
1155 	port->get_netdev = fcoe_netdev;
1156 	port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1157 	port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1158 	INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1159 
1160 	/*
1161 	 * Need to add the lport to the hostlist
1162 	 * so we catch NETDEV_CHANGE events.
1163 	 */
1164 	fcoe_hostlist_add(lport);
1165 
1166 	/* configure a fc_lport including the exchange manager */
1167 	rc = fcoe_lport_config(lport);
1168 	if (rc) {
1169 		FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1170 				"interface\n");
1171 		goto out_host_put;
1172 	}
1173 
1174 	if (npiv) {
1175 		FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1176 				"%16.16llx %16.16llx\n",
1177 				vport->node_name, vport->port_name);
1178 		fc_set_wwnn(lport, vport->node_name);
1179 		fc_set_wwpn(lport, vport->port_name);
1180 	}
1181 
1182 	/* configure lport network properties */
1183 	rc = fcoe_netdev_config(lport, netdev);
1184 	if (rc) {
1185 		FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1186 				"interface\n");
1187 		goto out_lp_destroy;
1188 	}
1189 
1190 	/* configure lport scsi host properties */
1191 	rc = fcoe_shost_config(lport, parent);
1192 	if (rc) {
1193 		FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1194 				"interface\n");
1195 		goto out_lp_destroy;
1196 	}
1197 
1198 	/* Initialize the library */
1199 	rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
1200 	if (rc) {
1201 		FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1202 				"interface\n");
1203 		goto out_lp_destroy;
1204 	}
1205 
1206 	/* Initialized FDMI information */
1207 	fcoe_fdmi_info(lport, netdev);
1208 
1209 	/*
1210 	 * fcoe_em_alloc() and fcoe_hostlist_add() both
1211 	 * need to be atomic with respect to other changes to the
1212 	 * hostlist since fcoe_em_alloc() looks for an existing EM
1213 	 * instance on host list updated by fcoe_hostlist_add().
1214 	 *
1215 	 * This is currently handled through the fcoe_config_mutex
1216 	 * begin held.
1217 	 */
1218 	if (!npiv)
1219 		/* lport exch manager allocation */
1220 		rc = fcoe_em_config(lport);
1221 	else {
1222 		shost = vport_to_shost(vport);
1223 		n_port = shost_priv(shost);
1224 		rc = fc_exch_mgr_list_clone(n_port, lport);
1225 	}
1226 
1227 	if (rc) {
1228 		FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1229 		goto out_lp_destroy;
1230 	}
1231 
1232 	return lport;
1233 
1234 out_lp_destroy:
1235 	fc_exch_mgr_free(lport);
1236 out_host_put:
1237 	fcoe_hostlist_del(lport);
1238 	scsi_host_put(lport->host);
1239 out:
1240 	return ERR_PTR(rc);
1241 }
1242 
1243 /**
1244  * fcoe_if_init() - Initialization routine for fcoe.ko
1245  *
1246  * Attaches the SW FCoE transport to the FC transport
1247  *
1248  * Returns: 0 on success
1249  */
1250 static int __init fcoe_if_init(void)
1251 {
1252 	/* attach to scsi transport */
1253 	fcoe_nport_scsi_transport =
1254 		fc_attach_transport(&fcoe_nport_fc_functions);
1255 	if (!fcoe_nport_scsi_transport)
1256 		goto err;
1257 
1258 	fcoe_vport_scsi_transport =
1259 		fc_attach_transport(&fcoe_vport_fc_functions);
1260 	if (!fcoe_vport_scsi_transport)
1261 		goto err_vport;
1262 
1263 	return 0;
1264 
1265 err_vport:
1266 	fc_release_transport(fcoe_nport_scsi_transport);
1267 err:
1268 	printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1269 	return -ENODEV;
1270 }
1271 
1272 /**
1273  * fcoe_if_exit() - Tear down fcoe.ko
1274  *
1275  * Detaches the SW FCoE transport from the FC transport
1276  *
1277  * Returns: 0 on success
1278  */
1279 static int __exit fcoe_if_exit(void)
1280 {
1281 	fc_release_transport(fcoe_nport_scsi_transport);
1282 	fc_release_transport(fcoe_vport_scsi_transport);
1283 	fcoe_nport_scsi_transport = NULL;
1284 	fcoe_vport_scsi_transport = NULL;
1285 	return 0;
1286 }
1287 
1288 static void fcoe_thread_cleanup_local(unsigned int cpu)
1289 {
1290 	struct page *crc_eof;
1291 	struct fcoe_percpu_s *p;
1292 
1293 	p = per_cpu_ptr(&fcoe_percpu, cpu);
1294 	spin_lock_bh(&p->fcoe_rx_list.lock);
1295 	crc_eof = p->crc_eof_page;
1296 	p->crc_eof_page = NULL;
1297 	p->crc_eof_offset = 0;
1298 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1299 
1300 	if (crc_eof)
1301 		put_page(crc_eof);
1302 	flush_work(&p->work);
1303 }
1304 
1305 /**
1306  * fcoe_rcv() - Receive packets from a net device
1307  * @skb:    The received packet
1308  * @netdev: The net device that the packet was received on
1309  * @ptype:  The packet type context
1310  * @olddev: The last device net device
1311  *
1312  * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1313  * FC frame and passes the frame to libfc.
1314  *
1315  * Returns: 0 for success
1316  */
1317 static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1318 	     struct packet_type *ptype, struct net_device *olddev)
1319 {
1320 	struct fc_lport *lport;
1321 	struct fcoe_rcv_info *fr;
1322 	struct fcoe_ctlr *ctlr;
1323 	struct fcoe_interface *fcoe;
1324 	struct fc_frame_header *fh;
1325 	struct fcoe_percpu_s *fps;
1326 	struct ethhdr *eh;
1327 	unsigned int cpu;
1328 
1329 	fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1330 	ctlr = fcoe_to_ctlr(fcoe);
1331 	lport = ctlr->lp;
1332 	if (unlikely(!lport)) {
1333 		FCOE_NETDEV_DBG(netdev, "Cannot find hba structure\n");
1334 		goto err2;
1335 	}
1336 	if (!lport->link_up)
1337 		goto err2;
1338 
1339 	FCOE_NETDEV_DBG(netdev,
1340 			"skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
1341 			skb->len, skb->data_len, skb->head, skb->data,
1342 			skb_tail_pointer(skb), skb_end_pointer(skb),
1343 			skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1344 
1345 
1346 	skb = skb_share_check(skb, GFP_ATOMIC);
1347 
1348 	if (skb == NULL)
1349 		return NET_RX_DROP;
1350 
1351 	eh = eth_hdr(skb);
1352 
1353 	if (is_fip_mode(ctlr) &&
1354 	    !ether_addr_equal(eh->h_source, ctlr->dest_addr)) {
1355 		FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1356 				eh->h_source);
1357 		goto err;
1358 	}
1359 
1360 	/*
1361 	 * Check for minimum frame length, and make sure required FCoE
1362 	 * and FC headers are pulled into the linear data area.
1363 	 */
1364 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1365 		     !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1366 		goto err;
1367 
1368 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1369 	fh = (struct fc_frame_header *) skb_transport_header(skb);
1370 
1371 	if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1372 		FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1373 				eh->h_dest);
1374 		goto err;
1375 	}
1376 
1377 	fr = fcoe_dev_from_skb(skb);
1378 	fr->fr_dev = lport;
1379 
1380 	/*
1381 	 * In case the incoming frame's exchange is originated from
1382 	 * the initiator, then received frame's exchange id is ANDed
1383 	 * with fc_cpu_mask bits to get the same cpu on which exchange
1384 	 * was originated, otherwise select cpu using rx exchange id
1385 	 * or fcoe_select_cpu().
1386 	 */
1387 	if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1388 		cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1389 	else {
1390 		if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1391 			cpu = skb->alloc_cpu;
1392 		else
1393 			cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1394 	}
1395 
1396 	if (cpu >= nr_cpu_ids)
1397 		goto err;
1398 
1399 	fps = &per_cpu(fcoe_percpu, cpu);
1400 	spin_lock(&fps->fcoe_rx_list.lock);
1401 	/*
1402 	 * We now have a valid CPU that we're targeting for
1403 	 * this skb. We also have this receive thread locked,
1404 	 * so we're free to queue skbs into it's queue.
1405 	 */
1406 
1407 	/*
1408 	 * Note: We used to have a set of conditions under which we would
1409 	 * call fcoe_recv_frame directly, rather than queuing to the rx list
1410 	 * as it could save a few cycles, but doing so is prohibited, as
1411 	 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1412 	 * in softirq context.
1413 	 */
1414 	__skb_queue_tail(&fps->fcoe_rx_list, skb);
1415 	schedule_work_on(cpu, &fps->work);
1416 	spin_unlock(&fps->fcoe_rx_list.lock);
1417 
1418 	return NET_RX_SUCCESS;
1419 err:
1420 	this_cpu_inc(lport->stats->ErrorFrames);
1421 err2:
1422 	kfree_skb(skb);
1423 	return NET_RX_DROP;
1424 }
1425 
1426 /**
1427  * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1428  * @skb:  The packet to be transmitted
1429  * @tlen: The total length of the trailer
1430  *
1431  * Returns: 0 for success
1432  */
1433 static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1434 {
1435 	struct fcoe_percpu_s *fps;
1436 	int rc;
1437 
1438 	local_lock(&fcoe_percpu.lock);
1439 	fps = this_cpu_ptr(&fcoe_percpu);
1440 	rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1441 	local_unlock(&fcoe_percpu.lock);
1442 
1443 	return rc;
1444 }
1445 
1446 /**
1447  * fcoe_xmit() - Transmit a FCoE frame
1448  * @lport: The local port that the frame is to be transmitted for
1449  * @fp:	   The frame to be transmitted
1450  *
1451  * Return: 0 for success
1452  */
1453 static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1454 {
1455 	int wlen;
1456 	u32 crc;
1457 	struct ethhdr *eh;
1458 	struct fcoe_crc_eof *cp;
1459 	struct sk_buff *skb;
1460 	struct fc_frame_header *fh;
1461 	unsigned int hlen;		/* header length implies the version */
1462 	unsigned int tlen;		/* trailer length */
1463 	unsigned int elen;		/* eth header, may include vlan */
1464 	struct fcoe_port *port = lport_priv(lport);
1465 	struct fcoe_interface *fcoe = port->priv;
1466 	struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1467 	u8 sof, eof;
1468 	struct fcoe_hdr *hp;
1469 
1470 	WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1471 
1472 	fh = fc_frame_header_get(fp);
1473 	skb = fp_skb(fp);
1474 
1475 	if (!lport->link_up) {
1476 		kfree_skb(skb);
1477 		return 0;
1478 	}
1479 
1480 	if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1481 	    fcoe_ctlr_els_send(ctlr, lport, skb))
1482 		return 0;
1483 
1484 	sof = fr_sof(fp);
1485 	eof = fr_eof(fp);
1486 
1487 	elen = sizeof(struct ethhdr);
1488 	hlen = sizeof(struct fcoe_hdr);
1489 	tlen = sizeof(struct fcoe_crc_eof);
1490 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1491 
1492 	/* crc offload */
1493 	if (likely(lport->crc_offload)) {
1494 		skb->ip_summed = CHECKSUM_PARTIAL;
1495 		skb->csum_start = skb_headroom(skb);
1496 		skb->csum_offset = skb->len;
1497 		crc = 0;
1498 	} else {
1499 		skb->ip_summed = CHECKSUM_NONE;
1500 		crc = fcoe_fc_crc(fp);
1501 	}
1502 
1503 	/* copy port crc and eof to the skb buff */
1504 	if (skb_is_nonlinear(skb)) {
1505 		skb_frag_t *frag;
1506 		if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1507 			kfree_skb(skb);
1508 			return -ENOMEM;
1509 		}
1510 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1511 		cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
1512 	} else {
1513 		cp = skb_put(skb, tlen);
1514 	}
1515 
1516 	memset(cp, 0, sizeof(*cp));
1517 	cp->fcoe_eof = eof;
1518 	cp->fcoe_crc32 = cpu_to_le32(~crc);
1519 
1520 	if (skb_is_nonlinear(skb)) {
1521 		kunmap_atomic(cp);
1522 		cp = NULL;
1523 	}
1524 
1525 	/* adjust skb network/transport offsets to match mac/fcoe/port */
1526 	skb_push(skb, elen + hlen);
1527 	skb_reset_mac_header(skb);
1528 	skb_reset_network_header(skb);
1529 	skb->mac_len = elen;
1530 	skb->protocol = htons(ETH_P_FCOE);
1531 	skb->priority = fcoe->priority;
1532 
1533 	if (is_vlan_dev(fcoe->netdev) &&
1534 	    fcoe->realdev->features & NETIF_F_HW_VLAN_CTAG_TX) {
1535 		/* must set skb->dev before calling vlan_put_tag */
1536 		skb->dev = fcoe->realdev;
1537 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1538 				       vlan_dev_vlan_id(fcoe->netdev));
1539 	} else
1540 		skb->dev = fcoe->netdev;
1541 
1542 	/* fill up mac and fcoe headers */
1543 	eh = eth_hdr(skb);
1544 	eh->h_proto = htons(ETH_P_FCOE);
1545 	memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1546 	if (ctlr->map_dest)
1547 		memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1548 
1549 	if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1550 		memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
1551 	else
1552 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1553 
1554 	hp = (struct fcoe_hdr *)(eh + 1);
1555 	memset(hp, 0, sizeof(*hp));
1556 	if (FC_FCOE_VER)
1557 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1558 	hp->fcoe_sof = sof;
1559 
1560 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1561 	if (lport->seq_offload && fr_max_payload(fp)) {
1562 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1563 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1564 	} else {
1565 		skb_shinfo(skb)->gso_type = 0;
1566 		skb_shinfo(skb)->gso_size = 0;
1567 	}
1568 	/* update tx stats: regardless if LLD fails */
1569 	this_cpu_inc(lport->stats->TxFrames);
1570 	this_cpu_add(lport->stats->TxWords, wlen);
1571 
1572 	/* send down to lld */
1573 	fr_dev(fp) = lport;
1574 	fcoe_port_send(port, skb);
1575 	return 0;
1576 }
1577 
1578 /**
1579  * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1580  * @lport: The local port the frame was received on
1581  * @fp:	   The received frame
1582  *
1583  * Return: 0 on passing filtering checks
1584  */
1585 static inline int fcoe_filter_frames(struct fc_lport *lport,
1586 				     struct fc_frame *fp)
1587 {
1588 	struct fcoe_ctlr *ctlr;
1589 	struct fcoe_interface *fcoe;
1590 	struct fc_frame_header *fh;
1591 	struct sk_buff *skb = (struct sk_buff *)fp;
1592 
1593 	/*
1594 	 * We only check CRC if no offload is available and if it is
1595 	 * it's solicited data, in which case, the FCP layer would
1596 	 * check it during the copy.
1597 	 */
1598 	if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1599 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1600 	else
1601 		fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1602 
1603 	fh = fc_frame_header_get(fp);
1604 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1605 		return 0;
1606 
1607 	fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1608 	ctlr = fcoe_to_ctlr(fcoe);
1609 	if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1610 	    ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1611 		FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1612 		return -EINVAL;
1613 	}
1614 
1615 	if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1616 	    le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1617 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1618 		return 0;
1619 	}
1620 
1621 	if (this_cpu_inc_return(lport->stats->InvalidCRCCount) < 5)
1622 		printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1623 	return -EINVAL;
1624 }
1625 
1626 /**
1627  * fcoe_recv_frame() - process a single received frame
1628  * @skb: frame to process
1629  */
1630 static void fcoe_recv_frame(struct sk_buff *skb)
1631 {
1632 	u32 fr_len;
1633 	struct fc_lport *lport;
1634 	struct fcoe_rcv_info *fr;
1635 	struct fcoe_crc_eof crc_eof;
1636 	struct fc_frame *fp;
1637 	struct fcoe_hdr *hp;
1638 
1639 	fr = fcoe_dev_from_skb(skb);
1640 	lport = fr->fr_dev;
1641 	if (unlikely(!lport)) {
1642 		FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb\n");
1643 		kfree_skb(skb);
1644 		return;
1645 	}
1646 
1647 	FCOE_NETDEV_DBG(skb->dev,
1648 			"skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
1649 			skb->len, skb->data_len,
1650 			skb->head, skb->data, skb_tail_pointer(skb),
1651 			skb_end_pointer(skb), skb->csum,
1652 			skb->dev ? skb->dev->name : "<NULL>");
1653 
1654 	skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
1655 
1656 	/*
1657 	 * Frame length checks and setting up the header pointers
1658 	 * was done in fcoe_rcv already.
1659 	 */
1660 	hp = (struct fcoe_hdr *) skb_network_header(skb);
1661 
1662 	if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1663 		struct fc_stats *stats;
1664 
1665 		stats = per_cpu_ptr(lport->stats, raw_smp_processor_id());
1666 		if (READ_ONCE(stats->ErrorFrames) < 5)
1667 			printk(KERN_WARNING "fcoe: FCoE version "
1668 			       "mismatch: The frame has "
1669 			       "version %x, but the "
1670 			       "initiator supports version "
1671 			       "%x\n", FC_FCOE_DECAPS_VER(hp),
1672 			       FC_FCOE_VER);
1673 		goto drop;
1674 	}
1675 
1676 	skb_pull(skb, sizeof(struct fcoe_hdr));
1677 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1678 
1679 	this_cpu_inc(lport->stats->RxFrames);
1680 	this_cpu_add(lport->stats->RxWords, fr_len / FCOE_WORD_TO_BYTE);
1681 
1682 	fp = (struct fc_frame *)skb;
1683 	fc_frame_init(fp);
1684 	fr_dev(fp) = lport;
1685 	fr_sof(fp) = hp->fcoe_sof;
1686 
1687 	/* Copy out the CRC and EOF trailer for access */
1688 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1689 		goto drop;
1690 	fr_eof(fp) = crc_eof.fcoe_eof;
1691 	fr_crc(fp) = crc_eof.fcoe_crc32;
1692 	if (pskb_trim(skb, fr_len))
1693 		goto drop;
1694 
1695 	if (!fcoe_filter_frames(lport, fp)) {
1696 		fc_exch_recv(lport, fp);
1697 		return;
1698 	}
1699 drop:
1700 	this_cpu_inc(lport->stats->ErrorFrames);
1701 	kfree_skb(skb);
1702 }
1703 
1704 /**
1705  * fcoe_receive_work() - The per-CPU worker
1706  * @work: The work struct
1707  *
1708  */
1709 static void fcoe_receive_work(struct work_struct *work)
1710 {
1711 	struct fcoe_percpu_s *p;
1712 	struct sk_buff *skb;
1713 	struct sk_buff_head tmp;
1714 
1715 	p = container_of(work, struct fcoe_percpu_s, work);
1716 	skb_queue_head_init(&tmp);
1717 
1718 	spin_lock_bh(&p->fcoe_rx_list.lock);
1719 	skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1720 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1721 
1722 	if (!skb_queue_len(&tmp))
1723 		return;
1724 
1725 	while ((skb = __skb_dequeue(&tmp)))
1726 		fcoe_recv_frame(skb);
1727 }
1728 
1729 /**
1730  * fcoe_dev_setup() - Setup the link change notification interface
1731  */
1732 static void fcoe_dev_setup(void)
1733 {
1734 	register_dcbevent_notifier(&dcb_notifier);
1735 	register_netdevice_notifier(&fcoe_notifier);
1736 }
1737 
1738 /**
1739  * fcoe_dev_cleanup() - Cleanup the link change notification interface
1740  */
1741 static void fcoe_dev_cleanup(void)
1742 {
1743 	unregister_dcbevent_notifier(&dcb_notifier);
1744 	unregister_netdevice_notifier(&fcoe_notifier);
1745 }
1746 
1747 static struct fcoe_interface *
1748 fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1749 {
1750 	struct fcoe_interface *fcoe;
1751 	struct net_device *real_dev;
1752 
1753 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1754 		if (is_vlan_dev(fcoe->netdev))
1755 			real_dev = vlan_dev_real_dev(fcoe->netdev);
1756 		else
1757 			real_dev = fcoe->netdev;
1758 
1759 		if (netdev == real_dev)
1760 			return fcoe;
1761 	}
1762 	return NULL;
1763 }
1764 
1765 static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1766 				     ulong event, void *ptr)
1767 {
1768 	struct dcb_app_type *entry = ptr;
1769 	struct fcoe_ctlr *ctlr;
1770 	struct fcoe_interface *fcoe;
1771 	struct net_device *netdev;
1772 	int prio;
1773 
1774 	if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1775 		return NOTIFY_OK;
1776 
1777 	netdev = dev_get_by_index(&init_net, entry->ifindex);
1778 	if (!netdev)
1779 		return NOTIFY_OK;
1780 
1781 	fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1782 	dev_put(netdev);
1783 	if (!fcoe)
1784 		return NOTIFY_OK;
1785 
1786 	ctlr = fcoe_to_ctlr(fcoe);
1787 
1788 	if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1789 		prio = ffs(entry->app.priority) - 1;
1790 	else
1791 		prio = entry->app.priority;
1792 
1793 	if (prio < 0)
1794 		return NOTIFY_OK;
1795 
1796 	if (entry->app.protocol == ETH_P_FIP ||
1797 	    entry->app.protocol == ETH_P_FCOE)
1798 		ctlr->priority = prio;
1799 
1800 	if (entry->app.protocol == ETH_P_FCOE)
1801 		fcoe->priority = prio;
1802 
1803 	return NOTIFY_OK;
1804 }
1805 
1806 /**
1807  * fcoe_device_notification() - Handler for net device events
1808  * @notifier: The context of the notification
1809  * @event:    The type of event
1810  * @ptr:      The net device that the event was on
1811  *
1812  * This function is called by the Ethernet driver in case of link change event.
1813  *
1814  * Returns: 0 for success
1815  */
1816 static int fcoe_device_notification(struct notifier_block *notifier,
1817 				    ulong event, void *ptr)
1818 {
1819 	struct fcoe_ctlr_device *cdev;
1820 	struct fc_lport *lport = NULL;
1821 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1822 	struct fcoe_ctlr *ctlr;
1823 	struct fcoe_interface *fcoe;
1824 	u32 link_possible = 1;
1825 	u32 mfs;
1826 	int rc = NOTIFY_OK;
1827 
1828 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1829 		if (fcoe->netdev == netdev) {
1830 			ctlr = fcoe_to_ctlr(fcoe);
1831 			lport = ctlr->lp;
1832 			break;
1833 		}
1834 	}
1835 	if (!lport) {
1836 		rc = NOTIFY_DONE;
1837 		goto out;
1838 	}
1839 
1840 	switch (event) {
1841 	case NETDEV_DOWN:
1842 	case NETDEV_GOING_DOWN:
1843 		link_possible = 0;
1844 		break;
1845 	case NETDEV_UP:
1846 	case NETDEV_CHANGE:
1847 		fcoe_link_speed_update(lport);
1848 		break;
1849 	case NETDEV_CHANGEMTU:
1850 		if (netdev->fcoe_mtu)
1851 			break;
1852 		mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1853 				     sizeof(struct fcoe_crc_eof));
1854 		if (mfs >= FC_MIN_MAX_FRAME)
1855 			fc_set_mfs(lport, mfs);
1856 		break;
1857 	case NETDEV_REGISTER:
1858 		break;
1859 	case NETDEV_UNREGISTER:
1860 		list_del(&fcoe->list);
1861 		fcoe_vport_remove(lport);
1862 		mutex_lock(&fcoe_config_mutex);
1863 		fcoe_if_destroy(lport);
1864 		if (!fcoe->removed)
1865 			fcoe_interface_remove(fcoe);
1866 		fcoe_interface_cleanup(fcoe);
1867 		mutex_unlock(&fcoe_config_mutex);
1868 		fcoe_ctlr_device_delete(fcoe_ctlr_to_ctlr_dev(ctlr));
1869 		goto out;
1870 	case NETDEV_FEAT_CHANGE:
1871 		fcoe_netdev_features_change(lport, netdev);
1872 		break;
1873 	default:
1874 		FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1875 				"from netdev netlink\n", event);
1876 	}
1877 
1878 	cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
1879 
1880 	if (link_possible && !fcoe_link_ok(lport)) {
1881 		switch (cdev->enabled) {
1882 		case FCOE_CTLR_DISABLED:
1883 			pr_info("Link up while interface is disabled.\n");
1884 			break;
1885 		case FCOE_CTLR_ENABLED:
1886 		case FCOE_CTLR_UNUSED:
1887 			fcoe_ctlr_link_up(ctlr);
1888 		}
1889 	} else if (fcoe_ctlr_link_down(ctlr)) {
1890 		switch (cdev->enabled) {
1891 		case FCOE_CTLR_DISABLED:
1892 			pr_info("Link down while interface is disabled.\n");
1893 			break;
1894 		case FCOE_CTLR_ENABLED:
1895 		case FCOE_CTLR_UNUSED:
1896 			this_cpu_inc(lport->stats->LinkFailureCount);
1897 			fcoe_clean_pending_queue(lport);
1898 		}
1899 	}
1900 out:
1901 	return rc;
1902 }
1903 
1904 /**
1905  * fcoe_disable() - Disables a FCoE interface
1906  * @netdev  : The net_device object the Ethernet interface to create on
1907  *
1908  * Called from fcoe transport.
1909  *
1910  * Returns: 0 for success
1911  *
1912  * Deprecated: use fcoe_ctlr_enabled()
1913  */
1914 static int fcoe_disable(struct net_device *netdev)
1915 {
1916 	struct fcoe_ctlr *ctlr;
1917 	struct fcoe_interface *fcoe;
1918 	int rc = 0;
1919 
1920 	mutex_lock(&fcoe_config_mutex);
1921 
1922 	rtnl_lock();
1923 	fcoe = fcoe_hostlist_lookup_port(netdev);
1924 	rtnl_unlock();
1925 
1926 	if (fcoe) {
1927 		ctlr = fcoe_to_ctlr(fcoe);
1928 		fcoe_ctlr_link_down(ctlr);
1929 		fcoe_clean_pending_queue(ctlr->lp);
1930 	} else
1931 		rc = -ENODEV;
1932 
1933 	mutex_unlock(&fcoe_config_mutex);
1934 	return rc;
1935 }
1936 
1937 /**
1938  * fcoe_enable() - Enables a FCoE interface
1939  * @netdev  : The net_device object the Ethernet interface to create on
1940  *
1941  * Called from fcoe transport.
1942  *
1943  * Returns: 0 for success
1944  */
1945 static int fcoe_enable(struct net_device *netdev)
1946 {
1947 	struct fcoe_ctlr *ctlr;
1948 	struct fcoe_interface *fcoe;
1949 	int rc = 0;
1950 
1951 	mutex_lock(&fcoe_config_mutex);
1952 	rtnl_lock();
1953 	fcoe = fcoe_hostlist_lookup_port(netdev);
1954 	rtnl_unlock();
1955 
1956 	if (!fcoe) {
1957 		rc = -ENODEV;
1958 		goto out;
1959 	}
1960 
1961 	ctlr = fcoe_to_ctlr(fcoe);
1962 
1963 	if (!fcoe_link_ok(ctlr->lp))
1964 		fcoe_ctlr_link_up(ctlr);
1965 
1966 out:
1967 	mutex_unlock(&fcoe_config_mutex);
1968 	return rc;
1969 }
1970 
1971 /**
1972  * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
1973  * @cdev: The FCoE Controller that is being enabled or disabled
1974  *
1975  * fcoe_sysfs will ensure that the state of 'enabled' has
1976  * changed, so no checking is necessary here. This routine simply
1977  * calls fcoe_enable or fcoe_disable, both of which are deprecated.
1978  * When those routines are removed the functionality can be merged
1979  * here.
1980  */
1981 static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
1982 {
1983 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
1984 	struct fc_lport *lport = ctlr->lp;
1985 	struct net_device *netdev = fcoe_netdev(lport);
1986 
1987 	switch (cdev->enabled) {
1988 	case FCOE_CTLR_ENABLED:
1989 		return fcoe_enable(netdev);
1990 	case FCOE_CTLR_DISABLED:
1991 		return fcoe_disable(netdev);
1992 	case FCOE_CTLR_UNUSED:
1993 	default:
1994 		return -ENOTSUPP;
1995 	}
1996 }
1997 
1998 /**
1999  * fcoe_ctlr_mode() - Switch FIP mode
2000  * @ctlr_dev: The FCoE Controller that is being modified
2001  *
2002  * When the FIP mode has been changed we need to update
2003  * the multicast addresses to ensure we get the correct
2004  * frames.
2005  */
2006 static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev)
2007 {
2008 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2009 	struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2010 
2011 	if (ctlr_dev->mode == FIP_CONN_TYPE_VN2VN &&
2012 	    ctlr->mode != FIP_MODE_VN2VN) {
2013 		dev_mc_del(fcoe->netdev, FIP_ALL_ENODE_MACS);
2014 		dev_mc_add(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2015 		dev_mc_add(fcoe->netdev, FIP_ALL_P2P_MACS);
2016 	} else if (ctlr->mode != FIP_MODE_FABRIC) {
2017 		dev_mc_del(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2018 		dev_mc_del(fcoe->netdev, FIP_ALL_P2P_MACS);
2019 		dev_mc_add(fcoe->netdev, FIP_ALL_ENODE_MACS);
2020 	}
2021 	fcoe_ctlr_set_fip_mode(ctlr_dev);
2022 }
2023 
2024 /**
2025  * fcoe_destroy() - Destroy a FCoE interface
2026  * @netdev  : The net_device object the Ethernet interface to create on
2027  *
2028  * Called from fcoe transport
2029  *
2030  * Returns: 0 for success
2031  */
2032 static int fcoe_destroy(struct net_device *netdev)
2033 {
2034 	struct fcoe_ctlr *ctlr;
2035 	struct fcoe_interface *fcoe;
2036 	struct fc_lport *lport;
2037 	struct fcoe_port *port;
2038 	int rc = 0;
2039 
2040 	mutex_lock(&fcoe_config_mutex);
2041 	rtnl_lock();
2042 	fcoe = fcoe_hostlist_lookup_port(netdev);
2043 	if (!fcoe) {
2044 		rc = -ENODEV;
2045 		goto out_nodev;
2046 	}
2047 	ctlr = fcoe_to_ctlr(fcoe);
2048 	lport = ctlr->lp;
2049 	port = lport_priv(lport);
2050 	list_del(&fcoe->list);
2051 	queue_work(fcoe_wq, &port->destroy_work);
2052 out_nodev:
2053 	rtnl_unlock();
2054 	mutex_unlock(&fcoe_config_mutex);
2055 	return rc;
2056 }
2057 
2058 /**
2059  * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2060  * @work: Handle to the FCoE port to be destroyed
2061  */
2062 static void fcoe_destroy_work(struct work_struct *work)
2063 {
2064 	struct fcoe_ctlr_device *cdev;
2065 	struct fcoe_ctlr *ctlr;
2066 	struct fcoe_port *port;
2067 	struct fcoe_interface *fcoe;
2068 
2069 	port = container_of(work, struct fcoe_port, destroy_work);
2070 
2071 	fcoe_vport_remove(port->lport);
2072 
2073 	mutex_lock(&fcoe_config_mutex);
2074 
2075 	fcoe = port->priv;
2076 	ctlr = fcoe_to_ctlr(fcoe);
2077 	cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2078 
2079 	rtnl_lock();
2080 	fcoe_if_destroy(port->lport);
2081 	if (!fcoe->removed)
2082 		fcoe_interface_remove(fcoe);
2083 	rtnl_unlock();
2084 	fcoe_interface_cleanup(fcoe);
2085 
2086 	mutex_unlock(&fcoe_config_mutex);
2087 
2088 	fcoe_ctlr_device_delete(cdev);
2089 }
2090 
2091 /**
2092  * fcoe_match() - Check if the FCoE is supported on the given netdevice
2093  * @netdev  : The net_device object the Ethernet interface to create on
2094  *
2095  * Called from fcoe transport.
2096  *
2097  * Returns: always returns true as this is the default FCoE transport,
2098  * i.e., support all netdevs.
2099  */
2100 static bool fcoe_match(struct net_device *netdev)
2101 {
2102 	return true;
2103 }
2104 
2105 /**
2106  * fcoe_dcb_create() - Initialize DCB attributes and hooks
2107  * @fcoe:   The new FCoE interface
2108  */
2109 static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2110 {
2111 	int ctlr_prio = TC_PRIO_BESTEFFORT;
2112 	int fcoe_prio = TC_PRIO_INTERACTIVE;
2113 	struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2114 #ifdef CONFIG_DCB
2115 	int dcbx;
2116 	u8 fup, up;
2117 	struct net_device *netdev = fcoe->realdev;
2118 	struct dcb_app app = {
2119 				.priority = 0,
2120 				.protocol = ETH_P_FCOE
2121 			     };
2122 
2123 	/* setup DCB priority attributes. */
2124 	if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2125 		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2126 
2127 		if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2128 			app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2129 			up = dcb_ieee_getapp_mask(netdev, &app);
2130 			app.protocol = ETH_P_FIP;
2131 			fup = dcb_ieee_getapp_mask(netdev, &app);
2132 		} else {
2133 			app.selector = DCB_APP_IDTYPE_ETHTYPE;
2134 			up = dcb_getapp(netdev, &app);
2135 			app.protocol = ETH_P_FIP;
2136 			fup = dcb_getapp(netdev, &app);
2137 		}
2138 
2139 		fcoe_prio = ffs(up) ? ffs(up) - 1 : 0;
2140 		ctlr_prio = ffs(fup) ? ffs(fup) - 1 : fcoe_prio;
2141 	}
2142 #endif
2143 	fcoe->priority = fcoe_prio;
2144 	ctlr->priority = ctlr_prio;
2145 }
2146 
2147 enum fcoe_create_link_state {
2148 	FCOE_CREATE_LINK_DOWN,
2149 	FCOE_CREATE_LINK_UP,
2150 };
2151 
2152 /**
2153  * _fcoe_create() - (internal) Create a fcoe interface
2154  * @netdev  :   The net_device object the Ethernet interface to create on
2155  * @fip_mode:   The FIP mode for this creation
2156  * @link_state: The ctlr link state on creation
2157  *
2158  * Called from either the libfcoe 'create' module parameter
2159  * via fcoe_create or from fcoe_syfs's ctlr_create file.
2160  *
2161  * libfcoe's 'create' module parameter is deprecated so some
2162  * consolidation of code can be done when that interface is
2163  * removed.
2164  */
2165 static int _fcoe_create(struct net_device *netdev, enum fip_mode fip_mode,
2166 			enum fcoe_create_link_state link_state)
2167 {
2168 	int rc = 0;
2169 	struct fcoe_ctlr_device *ctlr_dev;
2170 	struct fcoe_ctlr *ctlr;
2171 	struct fcoe_interface *fcoe;
2172 	struct fc_lport *lport;
2173 
2174 	mutex_lock(&fcoe_config_mutex);
2175 	rtnl_lock();
2176 
2177 	/* look for existing lport */
2178 	if (fcoe_hostlist_lookup(netdev)) {
2179 		rc = -EEXIST;
2180 		goto out_nodev;
2181 	}
2182 
2183 	fcoe = fcoe_interface_create(netdev, fip_mode);
2184 	if (IS_ERR(fcoe)) {
2185 		rc = PTR_ERR(fcoe);
2186 		goto out_nodev;
2187 	}
2188 
2189 	ctlr = fcoe_to_ctlr(fcoe);
2190 	ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2191 	lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
2192 	if (IS_ERR(lport)) {
2193 		printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2194 		       netdev->name);
2195 		rc = -EIO;
2196 		if (!fcoe->removed)
2197 			fcoe_interface_remove(fcoe);
2198 		rtnl_unlock();
2199 		fcoe_interface_cleanup(fcoe);
2200 		mutex_unlock(&fcoe_config_mutex);
2201 		fcoe_ctlr_device_delete(ctlr_dev);
2202 		return rc;
2203 	}
2204 
2205 	/* Make this the "master" N_Port */
2206 	ctlr->lp = lport;
2207 
2208 	/* setup DCB priority attributes. */
2209 	fcoe_dcb_create(fcoe);
2210 
2211 	/* start FIP Discovery and FLOGI */
2212 	lport->boot_time = jiffies;
2213 	fc_fabric_login(lport);
2214 
2215 	/*
2216 	 * If the fcoe_ctlr_device is to be set to DISABLED
2217 	 * it must be done after the lport is added to the
2218 	 * hostlist, but before the rtnl_lock is released.
2219 	 * This is because the rtnl_lock protects the
2220 	 * hostlist that fcoe_device_notification uses. If
2221 	 * the FCoE Controller is intended to be created
2222 	 * DISABLED then 'enabled' needs to be considered
2223 	 * handling link events. 'enabled' must be set
2224 	 * before the lport can be found in the hostlist
2225 	 * when a link up event is received.
2226 	 */
2227 	if (link_state == FCOE_CREATE_LINK_UP)
2228 		ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2229 	else
2230 		ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2231 
2232 	if (link_state == FCOE_CREATE_LINK_UP &&
2233 	    !fcoe_link_ok(lport)) {
2234 		rtnl_unlock();
2235 		fcoe_ctlr_link_up(ctlr);
2236 		mutex_unlock(&fcoe_config_mutex);
2237 		return rc;
2238 	}
2239 
2240 out_nodev:
2241 	rtnl_unlock();
2242 	mutex_unlock(&fcoe_config_mutex);
2243 
2244 	return rc;
2245 }
2246 
2247 /**
2248  * fcoe_create() - Create a fcoe interface
2249  * @netdev  : The net_device object the Ethernet interface to create on
2250  * @fip_mode: The FIP mode for this creation
2251  *
2252  * Called from fcoe transport
2253  *
2254  * Returns: 0 for success
2255  */
2256 static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode)
2257 {
2258 	return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2259 }
2260 
2261 /**
2262  * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2263  * @netdev: The net_device to be used by the allocated FCoE Controller
2264  *
2265  * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2266  * in a link_down state. The allows the user an opportunity to configure
2267  * the FCoE Controller from sysfs before enabling the FCoE Controller.
2268  *
2269  * Creating in with this routine starts the FCoE Controller in Fabric
2270  * mode. The user can change to VN2VN or another mode before enabling.
2271  */
2272 static int fcoe_ctlr_alloc(struct net_device *netdev)
2273 {
2274 	return _fcoe_create(netdev, FIP_MODE_FABRIC,
2275 			    FCOE_CREATE_LINK_DOWN);
2276 }
2277 
2278 /**
2279  * fcoe_link_ok() - Check if the link is OK for a local port
2280  * @lport: The local port to check link on
2281  *
2282  * Returns: 0 if link is UP and OK, -1 if not
2283  *
2284  */
2285 static int fcoe_link_ok(struct fc_lport *lport)
2286 {
2287 	struct net_device *netdev = fcoe_netdev(lport);
2288 
2289 	if (netif_oper_up(netdev))
2290 		return 0;
2291 	return -1;
2292 }
2293 
2294 /**
2295  * fcoe_percpu_clean() - Clear all pending skbs for an local port
2296  * @lport: The local port whose skbs are to be cleared
2297  *
2298  * Must be called with fcoe_create_mutex held to single-thread completion.
2299  *
2300  * This flushes the pending skbs by flush the work item for each CPU. The work
2301  * item on each possible CPU is flushed because we may have used the per-CPU
2302  * struct of an offline CPU.
2303  */
2304 static void fcoe_percpu_clean(struct fc_lport *lport)
2305 {
2306 	struct fcoe_percpu_s *pp;
2307 	unsigned int cpu;
2308 
2309 	for_each_possible_cpu(cpu) {
2310 		pp = &per_cpu(fcoe_percpu, cpu);
2311 
2312 		flush_work(&pp->work);
2313 	}
2314 }
2315 
2316 /**
2317  * fcoe_reset() - Reset a local port
2318  * @shost: The SCSI host associated with the local port to be reset
2319  *
2320  * Returns: Always 0 (return value required by FC transport template)
2321  */
2322 static int fcoe_reset(struct Scsi_Host *shost)
2323 {
2324 	struct fc_lport *lport = shost_priv(shost);
2325 	struct fcoe_port *port = lport_priv(lport);
2326 	struct fcoe_interface *fcoe = port->priv;
2327 	struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2328 	struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2329 
2330 	fcoe_ctlr_link_down(ctlr);
2331 	fcoe_clean_pending_queue(ctlr->lp);
2332 
2333 	if (cdev->enabled != FCOE_CTLR_DISABLED &&
2334 	    !fcoe_link_ok(ctlr->lp))
2335 		fcoe_ctlr_link_up(ctlr);
2336 	return 0;
2337 }
2338 
2339 /**
2340  * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2341  * @netdev: The net device used as a key
2342  *
2343  * Locking: Must be called with the RNL mutex held.
2344  *
2345  * Returns: NULL or the FCoE interface
2346  */
2347 static struct fcoe_interface *
2348 fcoe_hostlist_lookup_port(const struct net_device *netdev)
2349 {
2350 	struct fcoe_interface *fcoe;
2351 
2352 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2353 		if (fcoe->netdev == netdev)
2354 			return fcoe;
2355 	}
2356 	return NULL;
2357 }
2358 
2359 /**
2360  * fcoe_hostlist_lookup() - Find the local port associated with a
2361  *			    given net device
2362  * @netdev: The netdevice used as a key
2363  *
2364  * Locking: Must be called with the RTNL mutex held
2365  *
2366  * Returns: NULL or the local port
2367  */
2368 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2369 {
2370 	struct fcoe_ctlr *ctlr;
2371 	struct fcoe_interface *fcoe;
2372 
2373 	fcoe = fcoe_hostlist_lookup_port(netdev);
2374 	ctlr = fcoe_to_ctlr(fcoe);
2375 	return (fcoe) ? ctlr->lp : NULL;
2376 }
2377 
2378 /**
2379  * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2380  *			 port to the hostlist
2381  * @lport: The local port that identifies the FCoE interface to be added
2382  *
2383  * Locking: must be called with the RTNL mutex held
2384  *
2385  * Returns: 0 for success
2386  */
2387 static int fcoe_hostlist_add(const struct fc_lport *lport)
2388 {
2389 	struct fcoe_interface *fcoe;
2390 	struct fcoe_port *port;
2391 
2392 	fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2393 	if (!fcoe) {
2394 		port = lport_priv(lport);
2395 		fcoe = port->priv;
2396 		list_add_tail(&fcoe->list, &fcoe_hostlist);
2397 	}
2398 	return 0;
2399 }
2400 
2401 /**
2402  * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
2403  *			 port to the hostlist
2404  * @lport: The local port that identifies the FCoE interface to be added
2405  *
2406  * Locking: must be called with the RTNL mutex held
2407  *
2408  */
2409 static void fcoe_hostlist_del(const struct fc_lport *lport)
2410 {
2411 	struct fcoe_interface *fcoe;
2412 	struct fcoe_port *port;
2413 
2414 	port = lport_priv(lport);
2415 	fcoe = port->priv;
2416 	list_del(&fcoe->list);
2417 	return;
2418 }
2419 
2420 static struct fcoe_transport fcoe_sw_transport = {
2421 	.name = {FCOE_TRANSPORT_DEFAULT},
2422 	.attached = false,
2423 	.list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2424 	.match = fcoe_match,
2425 	.alloc = fcoe_ctlr_alloc,
2426 	.create = fcoe_create,
2427 	.destroy = fcoe_destroy,
2428 	.enable = fcoe_enable,
2429 	.disable = fcoe_disable,
2430 };
2431 
2432 /**
2433  * fcoe_init() - Initialize fcoe.ko
2434  *
2435  * Returns: 0 on success, or a negative value on failure
2436  */
2437 static int __init fcoe_init(void)
2438 {
2439 	struct fcoe_percpu_s *p;
2440 	unsigned int cpu;
2441 	int rc = 0;
2442 
2443 	fcoe_wq = alloc_workqueue("fcoe", WQ_PERCPU, 0);
2444 	if (!fcoe_wq)
2445 		return -ENOMEM;
2446 
2447 	/* register as a fcoe transport */
2448 	rc = fcoe_transport_attach(&fcoe_sw_transport);
2449 	if (rc) {
2450 		printk(KERN_ERR "failed to register an fcoe transport, check "
2451 			"if libfcoe is loaded\n");
2452 		goto out_destroy;
2453 	}
2454 
2455 	mutex_lock(&fcoe_config_mutex);
2456 
2457 	for_each_possible_cpu(cpu) {
2458 		p = per_cpu_ptr(&fcoe_percpu, cpu);
2459 		INIT_WORK(&p->work, fcoe_receive_work);
2460 		skb_queue_head_init(&p->fcoe_rx_list);
2461 		local_lock_init(&p->lock);
2462 	}
2463 
2464 	/* Setup link change notification */
2465 	fcoe_dev_setup();
2466 
2467 	rc = fcoe_if_init();
2468 	if (rc)
2469 		goto out_free;
2470 
2471 	mutex_unlock(&fcoe_config_mutex);
2472 	return 0;
2473 
2474 out_free:
2475 	mutex_unlock(&fcoe_config_mutex);
2476 	fcoe_transport_detach(&fcoe_sw_transport);
2477 out_destroy:
2478 	destroy_workqueue(fcoe_wq);
2479 	return rc;
2480 }
2481 module_init(fcoe_init);
2482 
2483 /**
2484  * fcoe_exit() - Clean up fcoe.ko
2485  *
2486  * Returns: 0 on success or a  negative value on failure
2487  */
2488 static void __exit fcoe_exit(void)
2489 {
2490 	struct fcoe_interface *fcoe, *tmp;
2491 	struct fcoe_ctlr *ctlr;
2492 	struct fcoe_port *port;
2493 	unsigned int cpu;
2494 
2495 	mutex_lock(&fcoe_config_mutex);
2496 
2497 	fcoe_dev_cleanup();
2498 
2499 	/* releases the associated fcoe hosts */
2500 	rtnl_lock();
2501 	list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2502 		ctlr = fcoe_to_ctlr(fcoe);
2503 		port = lport_priv(ctlr->lp);
2504 		fcoe_hostlist_del(port->lport);
2505 		queue_work(fcoe_wq, &port->destroy_work);
2506 	}
2507 	rtnl_unlock();
2508 
2509 	for_each_possible_cpu(cpu)
2510 		fcoe_thread_cleanup_local(cpu);
2511 
2512 	mutex_unlock(&fcoe_config_mutex);
2513 
2514 	/*
2515 	 * destroy_work's may be chained but destroy_workqueue()
2516 	 * can take care of them. Just kill the fcoe_wq.
2517 	 */
2518 	destroy_workqueue(fcoe_wq);
2519 
2520 	/*
2521 	 * Detaching from the scsi transport must happen after all
2522 	 * destroys are done on the fcoe_wq. destroy_workqueue will
2523 	 * enusre the fcoe_wq is flushed.
2524 	 */
2525 	fcoe_if_exit();
2526 
2527 	/* detach from fcoe transport */
2528 	fcoe_transport_detach(&fcoe_sw_transport);
2529 }
2530 module_exit(fcoe_exit);
2531 
2532 /**
2533  * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2534  * @seq: active sequence in the FLOGI or FDISC exchange
2535  * @fp: response frame, or error encoded in a pointer (timeout)
2536  * @arg: pointer to the fcoe_ctlr structure
2537  *
2538  * This handles MAC address management for FCoE, then passes control on to
2539  * the libfc FLOGI response handler.
2540  */
2541 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2542 {
2543 	struct fcoe_ctlr *fip = arg;
2544 	struct fc_exch *exch = fc_seq_exch(seq);
2545 	struct fc_lport *lport = exch->lp;
2546 	u8 *mac;
2547 
2548 	if (IS_ERR(fp))
2549 		goto done;
2550 
2551 	mac = fr_cb(fp)->granted_mac;
2552 	/* pre-FIP */
2553 	if (is_zero_ether_addr(mac))
2554 		fcoe_ctlr_recv_flogi(fip, lport, fp);
2555 	else
2556 		fcoe_update_src_mac(lport, mac);
2557 done:
2558 	fc_lport_flogi_resp(seq, fp, lport);
2559 }
2560 
2561 /**
2562  * fcoe_logo_resp() - FCoE specific LOGO response handler
2563  * @seq: active sequence in the LOGO exchange
2564  * @fp: response frame, or error encoded in a pointer (timeout)
2565  * @arg: pointer to the fcoe_ctlr structure
2566  *
2567  * This handles MAC address management for FCoE, then passes control on to
2568  * the libfc LOGO response handler.
2569  */
2570 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2571 {
2572 	struct fc_lport *lport = arg;
2573 	static u8 zero_mac[ETH_ALEN] = { 0 };
2574 
2575 	if (!IS_ERR(fp))
2576 		fcoe_update_src_mac(lport, zero_mac);
2577 	fc_lport_logo_resp(seq, fp, lport);
2578 }
2579 
2580 /*
2581  * fcoe_elsct_send - FCoE specific ELS handler
2582  *
2583  * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2584  * using FCoE specific response handlers and passing the FIP controller as
2585  * the argument (the lport is still available from the exchange).
2586  *
2587  * Most of the work here is just handed off to the libfc routine.
2588  */
2589 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2590 				      struct fc_frame *fp, unsigned int op,
2591 				      void (*resp)(struct fc_seq *,
2592 						   struct fc_frame *,
2593 						   void *),
2594 				      void *arg, u32 timeout)
2595 {
2596 	struct fcoe_port *port = lport_priv(lport);
2597 	struct fcoe_interface *fcoe = port->priv;
2598 	struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
2599 	struct fc_frame_header *fh = fc_frame_header_get(fp);
2600 
2601 	switch (op) {
2602 	case ELS_FLOGI:
2603 	case ELS_FDISC:
2604 		if (lport->point_to_multipoint)
2605 			break;
2606 		return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2607 				     fip, timeout);
2608 	case ELS_LOGO:
2609 		/* only hook onto fabric logouts, not port logouts */
2610 		if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2611 			break;
2612 		return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2613 				     lport, timeout);
2614 	}
2615 	return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2616 }
2617 
2618 /**
2619  * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2620  * @vport: fc_vport object to create a new fc_host for
2621  * @disabled: start the new fc_host in a disabled state by default?
2622  *
2623  * Returns: 0 for success
2624  */
2625 static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2626 {
2627 	struct Scsi_Host *shost = vport_to_shost(vport);
2628 	struct fc_lport *n_port = shost_priv(shost);
2629 	struct fcoe_port *port = lport_priv(n_port);
2630 	struct fcoe_interface *fcoe = port->priv;
2631 	struct net_device *netdev = fcoe->netdev;
2632 	struct fc_lport *vn_port;
2633 	int rc;
2634 	char buf[32];
2635 
2636 	rc = fcoe_validate_vport_create(vport);
2637 	if (rc) {
2638 		fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2639 		printk(KERN_ERR "fcoe: Failed to create vport, "
2640 			"WWPN (0x%s) already exists\n",
2641 			buf);
2642 		return rc;
2643 	}
2644 
2645 	mutex_lock(&fcoe_config_mutex);
2646 	rtnl_lock();
2647 	vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2648 	rtnl_unlock();
2649 	mutex_unlock(&fcoe_config_mutex);
2650 
2651 	if (IS_ERR(vn_port)) {
2652 		printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2653 		       netdev->name);
2654 		return -EIO;
2655 	}
2656 
2657 	if (disabled) {
2658 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
2659 	} else {
2660 		vn_port->boot_time = jiffies;
2661 		fc_fabric_login(vn_port);
2662 		fc_vport_setlink(vn_port);
2663 	}
2664 	return 0;
2665 }
2666 
2667 /**
2668  * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2669  * @vport: fc_vport object that is being destroyed
2670  *
2671  * Returns: 0 for success
2672  */
2673 static int fcoe_vport_destroy(struct fc_vport *vport)
2674 {
2675 	struct Scsi_Host *shost = vport_to_shost(vport);
2676 	struct fc_lport *n_port = shost_priv(shost);
2677 	struct fc_lport *vn_port = vport->dd_data;
2678 
2679 	mutex_lock(&n_port->lp_mutex);
2680 	list_del(&vn_port->list);
2681 	mutex_unlock(&n_port->lp_mutex);
2682 
2683 	mutex_lock(&fcoe_config_mutex);
2684 	rtnl_lock();
2685 	fcoe_if_destroy(vn_port);
2686 	rtnl_unlock();
2687 	mutex_unlock(&fcoe_config_mutex);
2688 
2689 	return 0;
2690 }
2691 
2692 /**
2693  * fcoe_vport_remove() - remove attached vports
2694  * @lport: lport for which the vports should be removed
2695  */
2696 static void fcoe_vport_remove(struct fc_lport *lport)
2697 {
2698 	struct Scsi_Host *shost;
2699 	struct fc_host_attrs *fc_host;
2700 	unsigned long flags;
2701 	struct fc_vport *vport;
2702 	struct fc_vport *next_vport;
2703 
2704 	shost = lport->host;
2705 	fc_host = shost_to_fc_host(shost);
2706 
2707 	/* Loop through all the vports and mark them for deletion */
2708 	spin_lock_irqsave(shost->host_lock, flags);
2709 	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2710 		if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2711 			continue;
2712 		} else {
2713 			vport->flags |= FC_VPORT_DELETING;
2714 			queue_work(fc_host_work_q(shost),
2715 				   &vport->vport_delete_work);
2716 		}
2717 	}
2718 	spin_unlock_irqrestore(shost->host_lock, flags);
2719 
2720 	flush_workqueue(fc_host_work_q(shost));
2721 }
2722 
2723 /**
2724  * fcoe_vport_disable() - change vport state
2725  * @vport: vport to bring online/offline
2726  * @disable: should the vport be disabled?
2727  */
2728 static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2729 {
2730 	struct fc_lport *lport = vport->dd_data;
2731 
2732 	if (disable) {
2733 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
2734 		fc_fabric_logoff(lport);
2735 	} else {
2736 		lport->boot_time = jiffies;
2737 		fc_fabric_login(lport);
2738 		fc_vport_setlink(lport);
2739 	}
2740 
2741 	return 0;
2742 }
2743 
2744 /**
2745  * fcoe_set_vport_symbolic_name() - append vport string to symbolic name
2746  * @vport: fc_vport with a new symbolic name string
2747  *
2748  * After generating a new symbolic name string, a new RSPN_ID request is
2749  * sent to the name server.  There is no response handler, so if it fails
2750  * for some reason it will not be retried.
2751  */
2752 static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2753 {
2754 	struct fc_lport *lport = vport->dd_data;
2755 	struct fc_frame *fp;
2756 	size_t len;
2757 
2758 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2759 		 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2760 		 fcoe_netdev(lport)->name, vport->symbolic_name);
2761 
2762 	if (lport->state != LPORT_ST_READY)
2763 		return;
2764 
2765 	len = strnlen(fc_host_symbolic_name(lport->host), 255);
2766 	fp = fc_frame_alloc(lport,
2767 			    sizeof(struct fc_ct_hdr) +
2768 			    sizeof(struct fc_ns_rspn) + len);
2769 	if (!fp)
2770 		return;
2771 	lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2772 			     NULL, NULL, 3 * lport->r_a_tov);
2773 }
2774 
2775 static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2776 {
2777 	struct fcoe_ctlr_device *ctlr_dev =
2778 		fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2779 	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2780 	struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2781 
2782 	fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2783 }
2784 
2785 /**
2786  * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2787  * @lport: the local port
2788  * @port_id: the port ID
2789  * @fp: the received frame, if any, that caused the port_id to be set.
2790  *
2791  * This routine handles the case where we received a FLOGI and are
2792  * entering point-to-point mode.  We need to call fcoe_ctlr_recv_flogi()
2793  * so it can set the non-mapped mode and gateway address.
2794  *
2795  * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2796  */
2797 static void fcoe_set_port_id(struct fc_lport *lport,
2798 			     u32 port_id, struct fc_frame *fp)
2799 {
2800 	struct fcoe_port *port = lport_priv(lport);
2801 	struct fcoe_interface *fcoe = port->priv;
2802 	struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2803 
2804 	if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2805 		fcoe_ctlr_recv_flogi(ctlr, lport, fp);
2806 }
2807