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