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