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