xref: /openbmc/linux/drivers/scsi/fcoe/fcoe.c (revision bf361707)
1a703e490SVasu Dev /*
2af7f85d9SChris Leech  * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
3a703e490SVasu Dev  *
4a703e490SVasu Dev  * This program is free software; you can redistribute it and/or modify it
5a703e490SVasu Dev  * under the terms and conditions of the GNU General Public License,
6a703e490SVasu Dev  * version 2, as published by the Free Software Foundation.
7a703e490SVasu Dev  *
8a703e490SVasu Dev  * This program is distributed in the hope it will be useful, but WITHOUT
9a703e490SVasu Dev  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10a703e490SVasu Dev  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11a703e490SVasu Dev  * more details.
12a703e490SVasu Dev  *
13a703e490SVasu Dev  * You should have received a copy of the GNU General Public License along with
14a703e490SVasu Dev  * this program; if not, write to the Free Software Foundation, Inc.,
15a703e490SVasu Dev  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16a703e490SVasu Dev  *
17a703e490SVasu Dev  * Maintained at www.Open-FCoE.org
18a703e490SVasu Dev  */
19a703e490SVasu Dev 
20a703e490SVasu Dev #include <linux/module.h>
21a703e490SVasu Dev #include <linux/version.h>
22a703e490SVasu Dev #include <linux/spinlock.h>
23a703e490SVasu Dev #include <linux/netdevice.h>
24a703e490SVasu Dev #include <linux/etherdevice.h>
25a703e490SVasu Dev #include <linux/ethtool.h>
26a703e490SVasu Dev #include <linux/if_ether.h>
27a703e490SVasu Dev #include <linux/if_vlan.h>
28a703e490SVasu Dev #include <linux/crc32.h>
29a703e490SVasu Dev #include <linux/cpu.h>
30a703e490SVasu Dev #include <linux/fs.h>
31a703e490SVasu Dev #include <linux/sysfs.h>
32a703e490SVasu Dev #include <linux/ctype.h>
33a703e490SVasu Dev #include <scsi/scsi_tcq.h>
34a703e490SVasu Dev #include <scsi/scsicam.h>
35a703e490SVasu Dev #include <scsi/scsi_transport.h>
36a703e490SVasu Dev #include <scsi/scsi_transport_fc.h>
37a703e490SVasu Dev #include <net/rtnetlink.h>
38a703e490SVasu Dev 
39a703e490SVasu Dev #include <scsi/fc/fc_encaps.h>
4097c8389dSJoe Eykholt #include <scsi/fc/fc_fip.h>
41a703e490SVasu Dev 
42a703e490SVasu Dev #include <scsi/libfc.h>
43a703e490SVasu Dev #include <scsi/fc_frame.h>
44a703e490SVasu Dev #include <scsi/libfcoe.h>
45a703e490SVasu Dev 
46fdd78027SVasu Dev #include "fcoe.h"
47fdd78027SVasu Dev 
48a703e490SVasu Dev MODULE_AUTHOR("Open-FCoE.org");
49a703e490SVasu Dev MODULE_DESCRIPTION("FCoE");
509b34ecffSVasu Dev MODULE_LICENSE("GPL v2");
51a703e490SVasu Dev 
5205cc7390SYi Zou /* Performance tuning parameters for fcoe */
5305cc7390SYi Zou static unsigned int fcoe_ddp_min;
5405cc7390SYi Zou module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
5505cc7390SYi Zou MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for "	\
5605cc7390SYi Zou 		 "Direct Data Placement (DDP).");
5705cc7390SYi Zou 
58dfc1d0feSChris Leech DEFINE_MUTEX(fcoe_config_mutex);
59dfc1d0feSChris Leech 
60e7a51997SJoe Eykholt /* fcoe_percpu_clean completion.  Waiter protected by fcoe_create_mutex */
61e7a51997SJoe Eykholt static DECLARE_COMPLETION(fcoe_flush_completion);
62e7a51997SJoe Eykholt 
63a703e490SVasu Dev /* fcoe host list */
64090eb6c4SChris Leech /* must only by accessed under the RTNL mutex */
65a703e490SVasu Dev LIST_HEAD(fcoe_hostlist);
66a703e490SVasu Dev DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
67a703e490SVasu Dev 
68dd3fd72eSChris Leech /* Function Prototypes */
691875f27eSRobert Love static int fcoe_reset(struct Scsi_Host *);
70fdd78027SVasu Dev static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71fdd78027SVasu Dev static int fcoe_rcv(struct sk_buff *, struct net_device *,
72fdd78027SVasu Dev 		    struct packet_type *, struct net_device *);
731875f27eSRobert Love static int fcoe_percpu_receive_thread(void *);
741875f27eSRobert Love static void fcoe_clean_pending_queue(struct fc_lport *);
751875f27eSRobert Love static void fcoe_percpu_clean(struct fc_lport *);
761875f27eSRobert Love static int fcoe_link_ok(struct fc_lport *);
77fdd78027SVasu Dev 
78fdd78027SVasu Dev static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79fdd78027SVasu Dev static int fcoe_hostlist_add(const struct fc_lport *);
80fdd78027SVasu Dev 
814bb6b515SVasu Dev static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
82a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83a703e490SVasu Dev static void fcoe_dev_setup(void);
84a703e490SVasu Dev static void fcoe_dev_cleanup(void);
851875f27eSRobert Love static struct fcoe_interface
861875f27eSRobert Love *fcoe_hostlist_lookup_port(const struct net_device *);
87a703e490SVasu Dev 
881875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
891875f27eSRobert Love 			 struct packet_type *, struct net_device *);
901875f27eSRobert Love 
911875f27eSRobert Love static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
921875f27eSRobert Love static void fcoe_update_src_mac(struct fc_lport *, u8 *);
931875f27eSRobert Love static u8 *fcoe_get_src_mac(struct fc_lport *);
941875f27eSRobert Love static void fcoe_destroy_work(struct work_struct *);
951875f27eSRobert Love 
961875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
971875f27eSRobert Love 			  unsigned int);
981875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *, u16);
991875f27eSRobert Love 
1001875f27eSRobert Love static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
1011875f27eSRobert Love 
1021875f27eSRobert Love static int fcoe_create(const char *, struct kernel_param *);
1031875f27eSRobert Love static int fcoe_destroy(const char *, struct kernel_param *);
1041875f27eSRobert Love 
1051875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
1061875f27eSRobert Love 				      u32 did, struct fc_frame *,
1071875f27eSRobert Love 				      unsigned int op,
1081875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
1091875f27eSRobert Love 						   struct fc_frame *,
1101875f27eSRobert Love 						   void *),
1111875f27eSRobert Love 				      void *, u32 timeout);
1121875f27eSRobert Love 
1131875f27eSRobert Love module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1141875f27eSRobert Love __MODULE_PARM_TYPE(create, "string");
1151875f27eSRobert Love MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1161875f27eSRobert Love module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1171875f27eSRobert Love __MODULE_PARM_TYPE(destroy, "string");
1181875f27eSRobert Love MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1191875f27eSRobert Love 
1201875f27eSRobert Love /* notification function for packets from net device */
121a703e490SVasu Dev static struct notifier_block fcoe_notifier = {
122a703e490SVasu Dev 	.notifier_call = fcoe_device_notification,
123a703e490SVasu Dev };
124a703e490SVasu Dev 
1251875f27eSRobert Love /* notification function for CPU hotplug events */
1261875f27eSRobert Love static struct notifier_block fcoe_cpu_notifier = {
1271875f27eSRobert Love 	.notifier_call = fcoe_cpu_callback,
1281875f27eSRobert Love };
1291875f27eSRobert Love 
130e9084bb8SChris Leech static struct scsi_transport_template *fcoe_transport_template;
131e9084bb8SChris Leech static struct scsi_transport_template *fcoe_vport_transport_template;
132a703e490SVasu Dev 
1331875f27eSRobert Love static int fcoe_vport_destroy(struct fc_vport *);
1341875f27eSRobert Love static int fcoe_vport_create(struct fc_vport *, bool disabled);
1351875f27eSRobert Love static int fcoe_vport_disable(struct fc_vport *, bool disable);
1361875f27eSRobert Love static void fcoe_set_vport_symbolic_name(struct fc_vport *);
1371875f27eSRobert Love 
1381875f27eSRobert Love static struct libfc_function_template fcoe_libfc_fcn_templ = {
1391875f27eSRobert Love 	.frame_send = fcoe_xmit,
1401875f27eSRobert Love 	.ddp_setup = fcoe_ddp_setup,
1411875f27eSRobert Love 	.ddp_done = fcoe_ddp_done,
1421875f27eSRobert Love 	.elsct_send = fcoe_elsct_send,
1431875f27eSRobert Love };
1449a05753bSChris Leech 
145a703e490SVasu Dev struct fc_function_template fcoe_transport_function = {
146a703e490SVasu Dev 	.show_host_node_name = 1,
147a703e490SVasu Dev 	.show_host_port_name = 1,
148a703e490SVasu Dev 	.show_host_supported_classes = 1,
149a703e490SVasu Dev 	.show_host_supported_fc4s = 1,
150a703e490SVasu Dev 	.show_host_active_fc4s = 1,
151a703e490SVasu Dev 	.show_host_maxframe_size = 1,
152a703e490SVasu Dev 
153a703e490SVasu Dev 	.show_host_port_id = 1,
154a703e490SVasu Dev 	.show_host_supported_speeds = 1,
155a703e490SVasu Dev 	.get_host_speed = fc_get_host_speed,
156a703e490SVasu Dev 	.show_host_speed = 1,
157a703e490SVasu Dev 	.show_host_port_type = 1,
158a703e490SVasu Dev 	.get_host_port_state = fc_get_host_port_state,
159a703e490SVasu Dev 	.show_host_port_state = 1,
160a703e490SVasu Dev 	.show_host_symbolic_name = 1,
161a703e490SVasu Dev 
162a703e490SVasu Dev 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
163a703e490SVasu Dev 	.show_rport_maxframe_size = 1,
164a703e490SVasu Dev 	.show_rport_supported_classes = 1,
165a703e490SVasu Dev 
166a703e490SVasu Dev 	.show_host_fabric_name = 1,
167a703e490SVasu Dev 	.show_starget_node_name = 1,
168a703e490SVasu Dev 	.show_starget_port_name = 1,
169a703e490SVasu Dev 	.show_starget_port_id = 1,
170a703e490SVasu Dev 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
171a703e490SVasu Dev 	.show_rport_dev_loss_tmo = 1,
172a703e490SVasu Dev 	.get_fc_host_stats = fc_get_host_stats,
173a703e490SVasu Dev 	.issue_fc_host_lip = fcoe_reset,
174a703e490SVasu Dev 
175a703e490SVasu Dev 	.terminate_rport_io = fc_rport_terminate_io,
1769a05753bSChris Leech 
1779a05753bSChris Leech 	.vport_create = fcoe_vport_create,
1789a05753bSChris Leech 	.vport_delete = fcoe_vport_destroy,
1799a05753bSChris Leech 	.vport_disable = fcoe_vport_disable,
180dc8596d3SChris Leech 	.set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
181a51ab396SSteve Ma 
182a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
183a703e490SVasu Dev };
184a703e490SVasu Dev 
185e9084bb8SChris Leech struct fc_function_template fcoe_vport_transport_function = {
186e9084bb8SChris Leech 	.show_host_node_name = 1,
187e9084bb8SChris Leech 	.show_host_port_name = 1,
188e9084bb8SChris Leech 	.show_host_supported_classes = 1,
189e9084bb8SChris Leech 	.show_host_supported_fc4s = 1,
190e9084bb8SChris Leech 	.show_host_active_fc4s = 1,
191e9084bb8SChris Leech 	.show_host_maxframe_size = 1,
192e9084bb8SChris Leech 
193e9084bb8SChris Leech 	.show_host_port_id = 1,
194e9084bb8SChris Leech 	.show_host_supported_speeds = 1,
195e9084bb8SChris Leech 	.get_host_speed = fc_get_host_speed,
196e9084bb8SChris Leech 	.show_host_speed = 1,
197e9084bb8SChris Leech 	.show_host_port_type = 1,
198e9084bb8SChris Leech 	.get_host_port_state = fc_get_host_port_state,
199e9084bb8SChris Leech 	.show_host_port_state = 1,
200e9084bb8SChris Leech 	.show_host_symbolic_name = 1,
201e9084bb8SChris Leech 
202e9084bb8SChris Leech 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
203e9084bb8SChris Leech 	.show_rport_maxframe_size = 1,
204e9084bb8SChris Leech 	.show_rport_supported_classes = 1,
205e9084bb8SChris Leech 
206e9084bb8SChris Leech 	.show_host_fabric_name = 1,
207e9084bb8SChris Leech 	.show_starget_node_name = 1,
208e9084bb8SChris Leech 	.show_starget_port_name = 1,
209e9084bb8SChris Leech 	.show_starget_port_id = 1,
210e9084bb8SChris Leech 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
211e9084bb8SChris Leech 	.show_rport_dev_loss_tmo = 1,
212e9084bb8SChris Leech 	.get_fc_host_stats = fc_get_host_stats,
213e9084bb8SChris Leech 	.issue_fc_host_lip = fcoe_reset,
214e9084bb8SChris Leech 
215e9084bb8SChris Leech 	.terminate_rport_io = fc_rport_terminate_io,
216a51ab396SSteve Ma 
217a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
218e9084bb8SChris Leech };
219e9084bb8SChris Leech 
220a703e490SVasu Dev static struct scsi_host_template fcoe_shost_template = {
221a703e490SVasu Dev 	.module = THIS_MODULE,
222a703e490SVasu Dev 	.name = "FCoE Driver",
223a703e490SVasu Dev 	.proc_name = FCOE_NAME,
224a703e490SVasu Dev 	.queuecommand = fc_queuecommand,
225a703e490SVasu Dev 	.eh_abort_handler = fc_eh_abort,
226a703e490SVasu Dev 	.eh_device_reset_handler = fc_eh_device_reset,
227a703e490SVasu Dev 	.eh_host_reset_handler = fc_eh_host_reset,
228a703e490SVasu Dev 	.slave_alloc = fc_slave_alloc,
229a703e490SVasu Dev 	.change_queue_depth = fc_change_queue_depth,
230a703e490SVasu Dev 	.change_queue_type = fc_change_queue_type,
231a703e490SVasu Dev 	.this_id = -1,
23214caf44cSVasu Dev 	.cmd_per_lun = 3,
233a703e490SVasu Dev 	.can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
234a703e490SVasu Dev 	.use_clustering = ENABLE_CLUSTERING,
235a703e490SVasu Dev 	.sg_tablesize = SG_ALL,
236a703e490SVasu Dev 	.max_sectors = 0xffff,
237a703e490SVasu Dev };
238a703e490SVasu Dev 
23954b649f8SChris Leech /**
2401875f27eSRobert Love  * fcoe_interface_setup() - Setup a FCoE interface
2411875f27eSRobert Love  * @fcoe:   The new FCoE interface
2421875f27eSRobert Love  * @netdev: The net device that the fcoe interface is on
24354b649f8SChris Leech  *
24454b649f8SChris Leech  * Returns : 0 for success
2452e70e241SChris Leech  * Locking: must be called with the RTNL mutex held
24654b649f8SChris Leech  */
24754b649f8SChris Leech static int fcoe_interface_setup(struct fcoe_interface *fcoe,
24854b649f8SChris Leech 				struct net_device *netdev)
24954b649f8SChris Leech {
25054b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
25154b649f8SChris Leech 	struct netdev_hw_addr *ha;
25254b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
253b7a727f1SYi Zou 	const struct net_device_ops *ops;
25454b649f8SChris Leech 
25554b649f8SChris Leech 	fcoe->netdev = netdev;
25654b649f8SChris Leech 
257b7a727f1SYi Zou 	/* Let LLD initialize for FCoE */
258b7a727f1SYi Zou 	ops = netdev->netdev_ops;
259b7a727f1SYi Zou 	if (ops->ndo_fcoe_enable) {
260b7a727f1SYi Zou 		if (ops->ndo_fcoe_enable(netdev))
261b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
262b7a727f1SYi Zou 					" specific feature for LLD.\n");
263b7a727f1SYi Zou 	}
264b7a727f1SYi Zou 
26554b649f8SChris Leech 	/* Do not support for bonding device */
26654b649f8SChris Leech 	if ((netdev->priv_flags & IFF_MASTER_ALB) ||
26754b649f8SChris Leech 	    (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
26854b649f8SChris Leech 	    (netdev->priv_flags & IFF_MASTER_8023AD)) {
26959d92516Sjohn fastabend 		FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
27054b649f8SChris Leech 		return -EOPNOTSUPP;
27154b649f8SChris Leech 	}
27254b649f8SChris Leech 
27354b649f8SChris Leech 	/* look for SAN MAC address, if multiple SAN MACs exist, only
27454b649f8SChris Leech 	 * use the first one for SPMA */
27554b649f8SChris Leech 	rcu_read_lock();
27654b649f8SChris Leech 	for_each_dev_addr(netdev, ha) {
27754b649f8SChris Leech 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
278bf361707SYi Zou 		    (is_valid_ether_addr(ha->addr))) {
27954b649f8SChris Leech 			memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
28054b649f8SChris Leech 			fip->spma = 1;
28154b649f8SChris Leech 			break;
28254b649f8SChris Leech 		}
28354b649f8SChris Leech 	}
28454b649f8SChris Leech 	rcu_read_unlock();
28554b649f8SChris Leech 
28654b649f8SChris Leech 	/* setup Source Mac Address */
28754b649f8SChris Leech 	if (!fip->spma)
28854b649f8SChris Leech 		memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
28954b649f8SChris Leech 
29054b649f8SChris Leech 	/*
29154b649f8SChris Leech 	 * Add FCoE MAC address as second unicast MAC address
29254b649f8SChris Leech 	 * or enter promiscuous mode if not capable of listening
29354b649f8SChris Leech 	 * for multiple unicast MACs.
29454b649f8SChris Leech 	 */
29554b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
29654b649f8SChris Leech 	dev_unicast_add(netdev, flogi_maddr);
29754b649f8SChris Leech 	if (fip->spma)
29854b649f8SChris Leech 		dev_unicast_add(netdev, fip->ctl_src_addr);
29954b649f8SChris Leech 	dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
30054b649f8SChris Leech 
30154b649f8SChris Leech 	/*
30254b649f8SChris Leech 	 * setup the receive function from ethernet driver
30354b649f8SChris Leech 	 * on the ethertype for the given device
30454b649f8SChris Leech 	 */
30554b649f8SChris Leech 	fcoe->fcoe_packet_type.func = fcoe_rcv;
30654b649f8SChris Leech 	fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
30754b649f8SChris Leech 	fcoe->fcoe_packet_type.dev = netdev;
30854b649f8SChris Leech 	dev_add_pack(&fcoe->fcoe_packet_type);
30954b649f8SChris Leech 
31054b649f8SChris Leech 	fcoe->fip_packet_type.func = fcoe_fip_recv;
31154b649f8SChris Leech 	fcoe->fip_packet_type.type = htons(ETH_P_FIP);
31254b649f8SChris Leech 	fcoe->fip_packet_type.dev = netdev;
31354b649f8SChris Leech 	dev_add_pack(&fcoe->fip_packet_type);
31454b649f8SChris Leech 
31554b649f8SChris Leech 	return 0;
31654b649f8SChris Leech }
31754b649f8SChris Leech 
318a703e490SVasu Dev /**
3191875f27eSRobert Love  * fcoe_interface_create() - Create a FCoE interface on a net device
3201875f27eSRobert Love  * @netdev: The net device to create the FCoE interface on
321030f4e00SChris Leech  *
322030f4e00SChris Leech  * Returns: pointer to a struct fcoe_interface or NULL on error
323030f4e00SChris Leech  */
324030f4e00SChris Leech static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
325030f4e00SChris Leech {
326030f4e00SChris Leech 	struct fcoe_interface *fcoe;
32759d92516Sjohn fastabend 	int err;
328030f4e00SChris Leech 
329030f4e00SChris Leech 	fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
330030f4e00SChris Leech 	if (!fcoe) {
331030f4e00SChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
332030f4e00SChris Leech 		return NULL;
333030f4e00SChris Leech 	}
334030f4e00SChris Leech 
3352e70e241SChris Leech 	dev_hold(netdev);
336030f4e00SChris Leech 	kref_init(&fcoe->kref);
33754b649f8SChris Leech 
33854b649f8SChris Leech 	/*
33954b649f8SChris Leech 	 * Initialize FIP.
34054b649f8SChris Leech 	 */
34154b649f8SChris Leech 	fcoe_ctlr_init(&fcoe->ctlr);
34254b649f8SChris Leech 	fcoe->ctlr.send = fcoe_fip_send;
34354b649f8SChris Leech 	fcoe->ctlr.update_mac = fcoe_update_src_mac;
34411b56188SChris Leech 	fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
34554b649f8SChris Leech 
34659d92516Sjohn fastabend 	err = fcoe_interface_setup(fcoe, netdev);
34759d92516Sjohn fastabend 	if (err) {
34859d92516Sjohn fastabend 		fcoe_ctlr_destroy(&fcoe->ctlr);
34959d92516Sjohn fastabend 		kfree(fcoe);
35059d92516Sjohn fastabend 		dev_put(netdev);
35159d92516Sjohn fastabend 		return NULL;
35259d92516Sjohn fastabend 	}
353030f4e00SChris Leech 
354030f4e00SChris Leech 	return fcoe;
355030f4e00SChris Leech }
356030f4e00SChris Leech 
357030f4e00SChris Leech /**
3581875f27eSRobert Love  * fcoe_interface_cleanup() - Clean up a FCoE interface
3591875f27eSRobert Love  * @fcoe: The FCoE interface to be cleaned up
3602e70e241SChris Leech  *
3612e70e241SChris Leech  * Caller must be holding the RTNL mutex
36254b649f8SChris Leech  */
36354b649f8SChris Leech void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
36454b649f8SChris Leech {
36554b649f8SChris Leech 	struct net_device *netdev = fcoe->netdev;
36654b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
36754b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
368b7a727f1SYi Zou 	const struct net_device_ops *ops;
36954b649f8SChris Leech 
37054b649f8SChris Leech 	/*
37154b649f8SChris Leech 	 * Don't listen for Ethernet packets anymore.
37254b649f8SChris Leech 	 * synchronize_net() ensures that the packet handlers are not running
37354b649f8SChris Leech 	 * on another CPU. dev_remove_pack() would do that, this calls the
37454b649f8SChris Leech 	 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
37554b649f8SChris Leech 	 */
37654b649f8SChris Leech 	__dev_remove_pack(&fcoe->fcoe_packet_type);
37754b649f8SChris Leech 	__dev_remove_pack(&fcoe->fip_packet_type);
37854b649f8SChris Leech 	synchronize_net();
37954b649f8SChris Leech 
38054b649f8SChris Leech 	/* Delete secondary MAC addresses */
38154b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
38254b649f8SChris Leech 	dev_unicast_delete(netdev, flogi_maddr);
38354b649f8SChris Leech 	if (fip->spma)
38454b649f8SChris Leech 		dev_unicast_delete(netdev, fip->ctl_src_addr);
38554b649f8SChris Leech 	dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
386b7a727f1SYi Zou 
387b7a727f1SYi Zou 	/* Tell the LLD we are done w/ FCoE */
388b7a727f1SYi Zou 	ops = netdev->netdev_ops;
389b7a727f1SYi Zou 	if (ops->ndo_fcoe_disable) {
390b7a727f1SYi Zou 		if (ops->ndo_fcoe_disable(netdev))
391b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
392b7a727f1SYi Zou 					" specific feature for LLD.\n");
393b7a727f1SYi Zou 	}
39454b649f8SChris Leech }
39554b649f8SChris Leech 
39654b649f8SChris Leech /**
397030f4e00SChris Leech  * fcoe_interface_release() - fcoe_port kref release function
3981875f27eSRobert Love  * @kref: Embedded reference count in an fcoe_interface struct
399030f4e00SChris Leech  */
400030f4e00SChris Leech static void fcoe_interface_release(struct kref *kref)
401030f4e00SChris Leech {
402030f4e00SChris Leech 	struct fcoe_interface *fcoe;
4032e70e241SChris Leech 	struct net_device *netdev;
404030f4e00SChris Leech 
405030f4e00SChris Leech 	fcoe = container_of(kref, struct fcoe_interface, kref);
4062e70e241SChris Leech 	netdev = fcoe->netdev;
4072e70e241SChris Leech 	/* tear-down the FCoE controller */
4082e70e241SChris Leech 	fcoe_ctlr_destroy(&fcoe->ctlr);
409030f4e00SChris Leech 	kfree(fcoe);
4102e70e241SChris Leech 	dev_put(netdev);
411030f4e00SChris Leech }
412030f4e00SChris Leech 
413030f4e00SChris Leech /**
4141875f27eSRobert Love  * fcoe_interface_get() - Get a reference to a FCoE interface
4151875f27eSRobert Love  * @fcoe: The FCoE interface to be held
416030f4e00SChris Leech  */
417030f4e00SChris Leech static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
418030f4e00SChris Leech {
419030f4e00SChris Leech 	kref_get(&fcoe->kref);
420030f4e00SChris Leech }
421030f4e00SChris Leech 
422030f4e00SChris Leech /**
4231875f27eSRobert Love  * fcoe_interface_put() - Put a reference to a FCoE interface
4241875f27eSRobert Love  * @fcoe: The FCoE interface to be released
425030f4e00SChris Leech  */
426030f4e00SChris Leech static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
427030f4e00SChris Leech {
428030f4e00SChris Leech 	kref_put(&fcoe->kref, fcoe_interface_release);
429030f4e00SChris Leech }
430030f4e00SChris Leech 
431030f4e00SChris Leech /**
4321875f27eSRobert Love  * fcoe_fip_recv() - Handler for received FIP frames
4331875f27eSRobert Love  * @skb:      The receive skb
4341875f27eSRobert Love  * @netdev:   The associated net device
4351875f27eSRobert Love  * @ptype:    The packet_type structure which was used to register this handler
4361875f27eSRobert Love  * @orig_dev: The original net_device the the skb was received on.
4371875f27eSRobert Love  *	      (in case dev is a bond)
438ab6b85c1SVasu Dev  *
439ab6b85c1SVasu Dev  * Returns: 0 for success
440ab6b85c1SVasu Dev  */
4411875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
442ab6b85c1SVasu Dev 			 struct packet_type *ptype,
443ab6b85c1SVasu Dev 			 struct net_device *orig_dev)
444ab6b85c1SVasu Dev {
445259ad85dSChris Leech 	struct fcoe_interface *fcoe;
446ab6b85c1SVasu Dev 
447259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
4483fe9a0baSChris Leech 	fcoe_ctlr_recv(&fcoe->ctlr, skb);
449ab6b85c1SVasu Dev 	return 0;
450ab6b85c1SVasu Dev }
451ab6b85c1SVasu Dev 
452ab6b85c1SVasu Dev /**
4531875f27eSRobert Love  * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
4541875f27eSRobert Love  * @fip: The FCoE controller
4551875f27eSRobert Love  * @skb: The FIP packet to be sent
456ab6b85c1SVasu Dev  */
457ab6b85c1SVasu Dev static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
458ab6b85c1SVasu Dev {
4593fe9a0baSChris Leech 	skb->dev = fcoe_from_ctlr(fip)->netdev;
460ab6b85c1SVasu Dev 	dev_queue_xmit(skb);
461ab6b85c1SVasu Dev }
462ab6b85c1SVasu Dev 
463ab6b85c1SVasu Dev /**
4641875f27eSRobert Love  * fcoe_update_src_mac() - Update the Ethernet MAC filters
4651875f27eSRobert Love  * @lport: The local port to update the source MAC on
4661875f27eSRobert Love  * @addr:  Unicast MAC address to add
467ab6b85c1SVasu Dev  *
468ab6b85c1SVasu Dev  * Remove any previously-set unicast MAC filter.
469ab6b85c1SVasu Dev  * Add secondary FCoE MAC address filter for our OUI.
470ab6b85c1SVasu Dev  */
47111b56188SChris Leech static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
472ab6b85c1SVasu Dev {
47311b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
47411b56188SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
475ab6b85c1SVasu Dev 
476ab6b85c1SVasu Dev 	rtnl_lock();
47711b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
47811b56188SChris Leech 		dev_unicast_delete(fcoe->netdev, port->data_src_addr);
47911b56188SChris Leech 	if (!is_zero_ether_addr(addr))
48011b56188SChris Leech 		dev_unicast_add(fcoe->netdev, addr);
48111b56188SChris Leech 	memcpy(port->data_src_addr, addr, ETH_ALEN);
482ab6b85c1SVasu Dev 	rtnl_unlock();
483ab6b85c1SVasu Dev }
484ab6b85c1SVasu Dev 
485ab6b85c1SVasu Dev /**
48611b56188SChris Leech  * fcoe_get_src_mac() - return the Ethernet source address for an lport
48711b56188SChris Leech  * @lport: libfc lport
48811b56188SChris Leech  */
48911b56188SChris Leech static u8 *fcoe_get_src_mac(struct fc_lport *lport)
49011b56188SChris Leech {
49111b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
49211b56188SChris Leech 
49311b56188SChris Leech 	return port->data_src_addr;
49411b56188SChris Leech }
49511b56188SChris Leech 
49611b56188SChris Leech /**
4971875f27eSRobert Love  * fcoe_lport_config() - Set up a local port
4981875f27eSRobert Love  * @lport: The local port to be setup
499a703e490SVasu Dev  *
500a703e490SVasu Dev  * Returns: 0 for success
501a703e490SVasu Dev  */
5021875f27eSRobert Love static int fcoe_lport_config(struct fc_lport *lport)
503a703e490SVasu Dev {
5041875f27eSRobert Love 	lport->link_up = 0;
5051875f27eSRobert Love 	lport->qfull = 0;
5061875f27eSRobert Love 	lport->max_retry_count = 3;
5071875f27eSRobert Love 	lport->max_rport_retry_count = 3;
5081875f27eSRobert Love 	lport->e_d_tov = 2 * 1000;	/* FC-FS default */
5091875f27eSRobert Love 	lport->r_a_tov = 2 * 2 * 1000;
5101875f27eSRobert Love 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
511a703e490SVasu Dev 				 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
5121875f27eSRobert Love 	lport->does_npiv = 1;
513a703e490SVasu Dev 
5141875f27eSRobert Love 	fc_lport_init_stats(lport);
515a703e490SVasu Dev 
516a703e490SVasu Dev 	/* lport fc_lport related configuration */
5171875f27eSRobert Love 	fc_lport_config(lport);
518a703e490SVasu Dev 
519a703e490SVasu Dev 	/* offload related configuration */
5201875f27eSRobert Love 	lport->crc_offload = 0;
5211875f27eSRobert Love 	lport->seq_offload = 0;
5221875f27eSRobert Love 	lport->lro_enabled = 0;
5231875f27eSRobert Love 	lport->lro_xid = 0;
5241875f27eSRobert Love 	lport->lso_max = 0;
525a703e490SVasu Dev 
526a703e490SVasu Dev 	return 0;
527a703e490SVasu Dev }
528a703e490SVasu Dev 
529a703e490SVasu Dev /**
5301875f27eSRobert Love  * fcoe_queue_timer() - The fcoe queue timer
5311875f27eSRobert Love  * @lport: The local port
5321047f221SVasu Dev  *
5331047f221SVasu Dev  * Calls fcoe_check_wait_queue on timeout
5341047f221SVasu Dev  */
5351875f27eSRobert Love static void fcoe_queue_timer(ulong lport)
5361047f221SVasu Dev {
5371875f27eSRobert Love 	fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
5381047f221SVasu Dev }
5391047f221SVasu Dev 
5401047f221SVasu Dev /**
5411875f27eSRobert Love  * fcoe_netdev_config() - Set up net devive for SW FCoE
5421875f27eSRobert Love  * @lport:  The local port that is associated with the net device
5431875f27eSRobert Love  * @netdev: The associated net device
544a703e490SVasu Dev  *
5451875f27eSRobert Love  * Must be called after fcoe_lport_config() as it will use local port mutex
546a703e490SVasu Dev  *
547a703e490SVasu Dev  * Returns: 0 for success
548a703e490SVasu Dev  */
5491875f27eSRobert Love static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
550a703e490SVasu Dev {
551a703e490SVasu Dev 	u32 mfs;
552a703e490SVasu Dev 	u64 wwnn, wwpn;
55325024989SChris Leech 	struct fcoe_interface *fcoe;
554014f5c3fSChris Leech 	struct fcoe_port *port;
555a703e490SVasu Dev 
556a703e490SVasu Dev 	/* Setup lport private data to point to fcoe softc */
5571875f27eSRobert Love 	port = lport_priv(lport);
55825024989SChris Leech 	fcoe = port->fcoe;
559a703e490SVasu Dev 
560a703e490SVasu Dev 	/*
561a703e490SVasu Dev 	 * Determine max frame size based on underlying device and optional
562a703e490SVasu Dev 	 * user-configured limit.  If the MFS is too low, fcoe_link_ok()
563a703e490SVasu Dev 	 * will return 0, so do this first.
564a703e490SVasu Dev 	 */
5657221d7e5SYi Zou 	mfs = netdev->mtu;
5667221d7e5SYi Zou 	if (netdev->features & NETIF_F_FCOE_MTU) {
5677221d7e5SYi Zou 		mfs = FCOE_MTU;
5687221d7e5SYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
5697221d7e5SYi Zou 	}
5707221d7e5SYi Zou 	mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
5711875f27eSRobert Love 	if (fc_set_mfs(lport, mfs))
572a703e490SVasu Dev 		return -EINVAL;
573a703e490SVasu Dev 
574a703e490SVasu Dev 	/* offload features support */
5751d1b88dcSVasu Dev 	if (netdev->features & NETIF_F_SG)
5761875f27eSRobert Love 		lport->sg_supp = 1;
577a703e490SVasu Dev 
578a703e490SVasu Dev 	if (netdev->features & NETIF_F_FCOE_CRC) {
5791875f27eSRobert Love 		lport->crc_offload = 1;
580d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
581a703e490SVasu Dev 	}
582a703e490SVasu Dev 	if (netdev->features & NETIF_F_FSO) {
5831875f27eSRobert Love 		lport->seq_offload = 1;
5841875f27eSRobert Love 		lport->lso_max = netdev->gso_max_size;
585d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
5861875f27eSRobert Love 				lport->lso_max);
587a703e490SVasu Dev 	}
588a703e490SVasu Dev 	if (netdev->fcoe_ddp_xid) {
5891875f27eSRobert Love 		lport->lro_enabled = 1;
5901875f27eSRobert Love 		lport->lro_xid = netdev->fcoe_ddp_xid;
591d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
5921875f27eSRobert Love 				lport->lro_xid);
593a703e490SVasu Dev 	}
594014f5c3fSChris Leech 	skb_queue_head_init(&port->fcoe_pending_queue);
595014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
5961875f27eSRobert Love 	setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
597a703e490SVasu Dev 
5981875f27eSRobert Love 	if (!lport->vport) {
5991d1b88dcSVasu Dev 		wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
6001875f27eSRobert Love 		fc_set_wwnn(lport, wwnn);
601a703e490SVasu Dev 		/* XXX - 3rd arg needs to be vlan id */
6021d1b88dcSVasu Dev 		wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
6031875f27eSRobert Love 		fc_set_wwpn(lport, wwpn);
6049a05753bSChris Leech 	}
605a703e490SVasu Dev 
606a703e490SVasu Dev 	return 0;
607a703e490SVasu Dev }
608a703e490SVasu Dev 
609a703e490SVasu Dev /**
6101875f27eSRobert Love  * fcoe_shost_config() - Set up the SCSI host associated with a local port
6111875f27eSRobert Love  * @lport: The local port
6121875f27eSRobert Love  * @shost: The SCSI host to associate with the local port
6131875f27eSRobert Love  * @dev:   The device associated with the SCSI host
614a703e490SVasu Dev  *
615a703e490SVasu Dev  * Must be called after fcoe_lport_config() and fcoe_netdev_config()
616a703e490SVasu Dev  *
617a703e490SVasu Dev  * Returns: 0 for success
618a703e490SVasu Dev  */
6191875f27eSRobert Love static int fcoe_shost_config(struct fc_lport *lport, struct Scsi_Host *shost,
620a703e490SVasu Dev 			     struct device *dev)
621a703e490SVasu Dev {
622a703e490SVasu Dev 	int rc = 0;
623a703e490SVasu Dev 
624a703e490SVasu Dev 	/* lport scsi host config */
6251875f27eSRobert Love 	lport->host->max_lun = FCOE_MAX_LUN;
6261875f27eSRobert Love 	lport->host->max_id = FCOE_MAX_FCP_TARGET;
6271875f27eSRobert Love 	lport->host->max_channel = 0;
6281875f27eSRobert Love 	if (lport->vport)
6291875f27eSRobert Love 		lport->host->transportt = fcoe_vport_transport_template;
630e9084bb8SChris Leech 	else
6311875f27eSRobert Love 		lport->host->transportt = fcoe_transport_template;
632a703e490SVasu Dev 
633a703e490SVasu Dev 	/* add the new host to the SCSI-ml */
6341875f27eSRobert Love 	rc = scsi_add_host(lport->host, dev);
635a703e490SVasu Dev 	if (rc) {
6361875f27eSRobert Love 		FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
637d5488eb9SRobert Love 				"error on scsi_add_host\n");
638a703e490SVasu Dev 		return rc;
639a703e490SVasu Dev 	}
6409a05753bSChris Leech 
6411875f27eSRobert Love 	if (!lport->vport)
6421875f27eSRobert Love 		fc_host_max_npiv_vports(lport->host) = USHORT_MAX;
6439a05753bSChris Leech 
6441875f27eSRobert Love 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
6455baa17c3SChris Leech 		 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
6461875f27eSRobert Love 		 fcoe_netdev(lport)->name);
647a703e490SVasu Dev 
648a703e490SVasu Dev 	return 0;
649a703e490SVasu Dev }
650a703e490SVasu Dev 
6511875f27eSRobert Love /**
6521875f27eSRobert Love  * fcoe_oem_match() - The match routine for the offloaded exchange manager
6531875f27eSRobert Love  * @fp: The I/O frame
654d7179680SVasu Dev  *
6551875f27eSRobert Love  * This routine will be associated with an exchange manager (EM). When
6561875f27eSRobert Love  * the libfc exchange handling code is looking for an EM to use it will
6571875f27eSRobert Love  * call this routine and pass it the frame that it wishes to send. This
6581875f27eSRobert Love  * routine will return True if the associated EM is to be used and False
6591875f27eSRobert Love  * if the echange code should continue looking for an EM.
6601875f27eSRobert Love  *
6611875f27eSRobert Love  * The offload EM that this routine is associated with will handle any
6621875f27eSRobert Love  * packets that are for SCSI read requests.
6631875f27eSRobert Love  *
6641875f27eSRobert Love  * Returns: True for read types I/O, otherwise returns false.
665d7179680SVasu Dev  */
666d7179680SVasu Dev bool fcoe_oem_match(struct fc_frame *fp)
667d7179680SVasu Dev {
66805cc7390SYi Zou 	return fc_fcp_is_read(fr_fsp(fp)) &&
66905cc7390SYi Zou 		(fr_fsp(fp)->data_len > fcoe_ddp_min);
670d7179680SVasu Dev }
671d7179680SVasu Dev 
672a703e490SVasu Dev /**
6731875f27eSRobert Love  * fcoe_em_config() - Allocate and configure an exchange manager
6741875f27eSRobert Love  * @lport: The local port that the new EM will be associated with
675a703e490SVasu Dev  *
676a703e490SVasu Dev  * Returns: 0 on success
677a703e490SVasu Dev  */
6781875f27eSRobert Love static inline int fcoe_em_config(struct fc_lport *lport)
679a703e490SVasu Dev {
6801875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
68125024989SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
68225024989SChris Leech 	struct fcoe_interface *oldfcoe = NULL;
6831d1b88dcSVasu Dev 	struct net_device *old_real_dev, *cur_real_dev;
684d7179680SVasu Dev 	u16 min_xid = FCOE_MIN_XID;
685d7179680SVasu Dev 	u16 max_xid = FCOE_MAX_XID;
686d7179680SVasu Dev 
687d7179680SVasu Dev 	/*
688d7179680SVasu Dev 	 * Check if need to allocate an em instance for
689d7179680SVasu Dev 	 * offload exchange ids to be shared across all VN_PORTs/lport.
690d7179680SVasu Dev 	 */
6911875f27eSRobert Love 	if (!lport->lro_enabled || !lport->lro_xid ||
6921875f27eSRobert Love 	    (lport->lro_xid >= max_xid)) {
6931875f27eSRobert Love 		lport->lro_xid = 0;
694d7179680SVasu Dev 		goto skip_oem;
695d7179680SVasu Dev 	}
696d7179680SVasu Dev 
697d7179680SVasu Dev 	/*
698d7179680SVasu Dev 	 * Reuse existing offload em instance in case
6991d1b88dcSVasu Dev 	 * it is already allocated on real eth device
700d7179680SVasu Dev 	 */
70125024989SChris Leech 	if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
70225024989SChris Leech 		cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
7031d1b88dcSVasu Dev 	else
70425024989SChris Leech 		cur_real_dev = fcoe->netdev;
7051d1b88dcSVasu Dev 
70625024989SChris Leech 	list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
70725024989SChris Leech 		if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
70825024989SChris Leech 			old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
7091d1b88dcSVasu Dev 		else
71025024989SChris Leech 			old_real_dev = oldfcoe->netdev;
7111d1b88dcSVasu Dev 
7121d1b88dcSVasu Dev 		if (cur_real_dev == old_real_dev) {
713991cbb60SChris Leech 			fcoe->oem = oldfcoe->oem;
714d7179680SVasu Dev 			break;
715d7179680SVasu Dev 		}
716d7179680SVasu Dev 	}
717d7179680SVasu Dev 
718991cbb60SChris Leech 	if (fcoe->oem) {
7191875f27eSRobert Love 		if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
720d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to add "
721d7179680SVasu Dev 			       "offload em:%p on interface:%s\n",
722991cbb60SChris Leech 			       fcoe->oem, fcoe->netdev->name);
723a703e490SVasu Dev 			return -ENOMEM;
724d7179680SVasu Dev 		}
725d7179680SVasu Dev 	} else {
7261875f27eSRobert Love 		fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
7271875f27eSRobert Love 					      FCOE_MIN_XID, lport->lro_xid,
728d7179680SVasu Dev 					      fcoe_oem_match);
729991cbb60SChris Leech 		if (!fcoe->oem) {
730d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to allocate "
731d7179680SVasu Dev 			       "em for offload exches on interface:%s\n",
73225024989SChris Leech 			       fcoe->netdev->name);
733d7179680SVasu Dev 			return -ENOMEM;
734d7179680SVasu Dev 		}
735d7179680SVasu Dev 	}
736d7179680SVasu Dev 
737d7179680SVasu Dev 	/*
738d7179680SVasu Dev 	 * Exclude offload EM xid range from next EM xid range.
739d7179680SVasu Dev 	 */
7401875f27eSRobert Love 	min_xid += lport->lro_xid + 1;
741d7179680SVasu Dev 
742d7179680SVasu Dev skip_oem:
7431875f27eSRobert Love 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
744d7179680SVasu Dev 		printk(KERN_ERR "fcoe_em_config: failed to "
74525024989SChris Leech 		       "allocate em on interface %s\n", fcoe->netdev->name);
746d7179680SVasu Dev 		return -ENOMEM;
747d7179680SVasu Dev 	}
748a703e490SVasu Dev 
749a703e490SVasu Dev 	return 0;
750a703e490SVasu Dev }
751a703e490SVasu Dev 
752a703e490SVasu Dev /**
7531875f27eSRobert Love  * fcoe_if_destroy() - Tear down a SW FCoE instance
7541875f27eSRobert Love  * @lport: The local port to be destroyed
755a703e490SVasu Dev  */
756af7f85d9SChris Leech static void fcoe_if_destroy(struct fc_lport *lport)
757a703e490SVasu Dev {
758014f5c3fSChris Leech 	struct fcoe_port *port = lport_priv(lport);
759014f5c3fSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
76025024989SChris Leech 	struct net_device *netdev = fcoe->netdev;
761a703e490SVasu Dev 
762d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
763a703e490SVasu Dev 
764a703e490SVasu Dev 	/* Logout of the fabric */
765af7f85d9SChris Leech 	fc_fabric_logoff(lport);
766a703e490SVasu Dev 
767a703e490SVasu Dev 	/* Cleanup the fc_lport */
768af7f85d9SChris Leech 	fc_lport_destroy(lport);
769af7f85d9SChris Leech 	fc_fcp_destroy(lport);
770a703e490SVasu Dev 
77154b649f8SChris Leech 	/* Stop the transmit retry timer */
77254b649f8SChris Leech 	del_timer_sync(&port->timer);
77354b649f8SChris Leech 
77454b649f8SChris Leech 	/* Free existing transmit skbs */
77554b649f8SChris Leech 	fcoe_clean_pending_queue(lport);
77654b649f8SChris Leech 
77711b56188SChris Leech 	rtnl_lock();
77811b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
77911b56188SChris Leech 		dev_unicast_delete(netdev, port->data_src_addr);
78011b56188SChris Leech 	rtnl_unlock();
78111b56188SChris Leech 
78254b649f8SChris Leech 	/* receives may not be stopped until after this */
78354b649f8SChris Leech 	fcoe_interface_put(fcoe);
78454b649f8SChris Leech 
78554b649f8SChris Leech 	/* Free queued packets for the per-CPU receive threads */
78654b649f8SChris Leech 	fcoe_percpu_clean(lport);
78754b649f8SChris Leech 
788a703e490SVasu Dev 	/* Detach from the scsi-ml */
789af7f85d9SChris Leech 	fc_remove_host(lport->host);
790af7f85d9SChris Leech 	scsi_remove_host(lport->host);
791a703e490SVasu Dev 
792a703e490SVasu Dev 	/* There are no more rports or I/O, free the EM */
793af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
794a703e490SVasu Dev 
795a703e490SVasu Dev 	/* Free memory used by statistical counters */
796af7f85d9SChris Leech 	fc_lport_free_stats(lport);
797a703e490SVasu Dev 
7982e70e241SChris Leech 	/* Release the Scsi_Host */
799af7f85d9SChris Leech 	scsi_host_put(lport->host);
800a703e490SVasu Dev }
801a703e490SVasu Dev 
8021875f27eSRobert Love /**
8031875f27eSRobert Love  * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
8041875f27eSRobert Love  * @lport: The local port to setup DDP for
8051875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
8061875f27eSRobert Love  * @sgl:   The scatterlist describing this transfer
8071875f27eSRobert Love  * @sgc:   The number of sg items
808a703e490SVasu Dev  *
8091875f27eSRobert Love  * Returns: 0 if the DDP context was not configured
810a703e490SVasu Dev  */
8111875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
812a703e490SVasu Dev 			  struct scatterlist *sgl, unsigned int sgc)
813a703e490SVasu Dev {
8141875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
815a703e490SVasu Dev 
8161875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
8171875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
8181875f27eSRobert Love 							      xid, sgl,
8191875f27eSRobert Love 							      sgc);
820a703e490SVasu Dev 
821a703e490SVasu Dev 	return 0;
822a703e490SVasu Dev }
823a703e490SVasu Dev 
824a703e490SVasu Dev /**
8251875f27eSRobert Love  * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
8261875f27eSRobert Love  * @lport: The local port to complete DDP on
8271875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
828a703e490SVasu Dev  *
8291875f27eSRobert Love  * Returns: the length of data that have been completed by DDP
8301875f27eSRobert Love  */
8311875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
8321875f27eSRobert Love {
8331875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
8341875f27eSRobert Love 
8351875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_done)
8361875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
8371875f27eSRobert Love 	return 0;
8381875f27eSRobert Love }
8391875f27eSRobert Love 
8401875f27eSRobert Love /**
8411875f27eSRobert Love  * fcoe_if_create() - Create a FCoE instance on an interface
8421875f27eSRobert Love  * @fcoe:   The FCoE interface to create a local port on
8431875f27eSRobert Love  * @parent: The device pointer to be the parent in sysfs for the SCSI host
8441875f27eSRobert Love  * @npiv:   Indicates if the port is a vport or not
8451875f27eSRobert Love  *
8461875f27eSRobert Love  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
847a703e490SVasu Dev  *
848af7f85d9SChris Leech  * Returns: The allocated fc_lport or an error pointer
849a703e490SVasu Dev  */
850030f4e00SChris Leech static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
8519a05753bSChris Leech 				       struct device *parent, int npiv)
852a703e490SVasu Dev {
8531875f27eSRobert Love 	struct net_device *netdev = fcoe->netdev;
854af7f85d9SChris Leech 	struct fc_lport *lport = NULL;
855014f5c3fSChris Leech 	struct fcoe_port *port;
856a703e490SVasu Dev 	struct Scsi_Host *shost;
8571875f27eSRobert Love 	int rc;
8589a05753bSChris Leech 	/*
8599a05753bSChris Leech 	 * parent is only a vport if npiv is 1,
8609a05753bSChris Leech 	 * but we'll only use vport in that case so go ahead and set it
8619a05753bSChris Leech 	 */
8629a05753bSChris Leech 	struct fc_vport *vport = dev_to_vport(parent);
863a703e490SVasu Dev 
864d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Create Interface\n");
865a703e490SVasu Dev 
8669a05753bSChris Leech 	if (!npiv) {
86786221969SChris Leech 		lport = libfc_host_alloc(&fcoe_shost_template,
868014f5c3fSChris Leech 					 sizeof(struct fcoe_port));
8699a05753bSChris Leech 	} else	{
8709a05753bSChris Leech 		lport = libfc_vport_create(vport,
8719a05753bSChris Leech 					   sizeof(struct fcoe_port));
8729a05753bSChris Leech 	}
87386221969SChris Leech 	if (!lport) {
874014f5c3fSChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
875014f5c3fSChris Leech 		rc = -ENOMEM;
876030f4e00SChris Leech 		goto out;
877014f5c3fSChris Leech 	}
87886221969SChris Leech 	shost = lport->host;
879014f5c3fSChris Leech 	port = lport_priv(lport);
8802e70e241SChris Leech 	port->lport = lport;
881014f5c3fSChris Leech 	port->fcoe = fcoe;
8822e70e241SChris Leech 	INIT_WORK(&port->destroy_work, fcoe_destroy_work);
883a703e490SVasu Dev 
8841875f27eSRobert Love 	/* configure a fc_lport including the exchange manager */
885af7f85d9SChris Leech 	rc = fcoe_lport_config(lport);
886a703e490SVasu Dev 	if (rc) {
887d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
888d5488eb9SRobert Love 				"interface\n");
889a703e490SVasu Dev 		goto out_host_put;
890a703e490SVasu Dev 	}
891a703e490SVasu Dev 
8929a05753bSChris Leech 	if (npiv) {
8939a05753bSChris Leech 		FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
8949a05753bSChris Leech 				vport->node_name, vport->port_name);
8959a05753bSChris Leech 		fc_set_wwnn(lport, vport->node_name);
8969a05753bSChris Leech 		fc_set_wwpn(lport, vport->port_name);
8979a05753bSChris Leech 	}
8989a05753bSChris Leech 
899ab6b85c1SVasu Dev 	/* configure lport network properties */
900af7f85d9SChris Leech 	rc = fcoe_netdev_config(lport, netdev);
901ab6b85c1SVasu Dev 	if (rc) {
902d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
903d5488eb9SRobert Love 				"interface\n");
90454b649f8SChris Leech 		goto out_lp_destroy;
905ab6b85c1SVasu Dev 	}
90697c8389dSJoe Eykholt 
907a703e490SVasu Dev 	/* configure lport scsi host properties */
908af7f85d9SChris Leech 	rc = fcoe_shost_config(lport, shost, parent);
909a703e490SVasu Dev 	if (rc) {
910d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
911d5488eb9SRobert Love 				"interface\n");
91254b649f8SChris Leech 		goto out_lp_destroy;
913a703e490SVasu Dev 	}
914a703e490SVasu Dev 
915a703e490SVasu Dev 	/* Initialize the library */
916af7f85d9SChris Leech 	rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
917a703e490SVasu Dev 	if (rc) {
918d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
919d5488eb9SRobert Love 				"interface\n");
920a703e490SVasu Dev 		goto out_lp_destroy;
921a703e490SVasu Dev 	}
922a703e490SVasu Dev 
9239a05753bSChris Leech 	if (!npiv) {
924e8af4d43SVasu Dev 		/*
925e8af4d43SVasu Dev 		 * fcoe_em_alloc() and fcoe_hostlist_add() both
9269a05753bSChris Leech 		 * need to be atomic with respect to other changes to the
9279a05753bSChris Leech 		 * hostlist since fcoe_em_alloc() looks for an existing EM
928e8af4d43SVasu Dev 		 * instance on host list updated by fcoe_hostlist_add().
929c863df33SChris Leech 		 *
9309a05753bSChris Leech 		 * This is currently handled through the fcoe_config_mutex
9319a05753bSChris Leech 		 * begin held.
932e8af4d43SVasu Dev 		 */
933c863df33SChris Leech 
93496316099SVasu Dev 		/* lport exch manager allocation */
935af7f85d9SChris Leech 		rc = fcoe_em_config(lport);
93696316099SVasu Dev 		if (rc) {
9379a05753bSChris Leech 			FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
9389a05753bSChris Leech 					"for the interface\n");
93996316099SVasu Dev 			goto out_lp_destroy;
94096316099SVasu Dev 		}
9419a05753bSChris Leech 	}
94296316099SVasu Dev 
943030f4e00SChris Leech 	fcoe_interface_get(fcoe);
944af7f85d9SChris Leech 	return lport;
945a703e490SVasu Dev 
946a703e490SVasu Dev out_lp_destroy:
947af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
948a703e490SVasu Dev out_host_put:
949af7f85d9SChris Leech 	scsi_host_put(lport->host);
950af7f85d9SChris Leech out:
951af7f85d9SChris Leech 	return ERR_PTR(rc);
952a703e490SVasu Dev }
953a703e490SVasu Dev 
954a703e490SVasu Dev /**
9551875f27eSRobert Love  * fcoe_if_init() - Initialization routine for fcoe.ko
9561875f27eSRobert Love  *
9571875f27eSRobert Love  * Attaches the SW FCoE transport to the FC transport
958a703e490SVasu Dev  *
959a703e490SVasu Dev  * Returns: 0 on success
960a703e490SVasu Dev  */
961a703e490SVasu Dev static int __init fcoe_if_init(void)
962a703e490SVasu Dev {
963a703e490SVasu Dev 	/* attach to scsi transport */
964e9084bb8SChris Leech 	fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
965e9084bb8SChris Leech 	fcoe_vport_transport_template =
966e9084bb8SChris Leech 		fc_attach_transport(&fcoe_vport_transport_function);
967a703e490SVasu Dev 
968e9084bb8SChris Leech 	if (!fcoe_transport_template) {
969d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
970a703e490SVasu Dev 		return -ENODEV;
971a703e490SVasu Dev 	}
972a703e490SVasu Dev 
973a703e490SVasu Dev 	return 0;
974a703e490SVasu Dev }
975a703e490SVasu Dev 
976a703e490SVasu Dev /**
9771875f27eSRobert Love  * fcoe_if_exit() - Tear down fcoe.ko
9781875f27eSRobert Love  *
9791875f27eSRobert Love  * Detaches the SW FCoE transport from the FC transport
980a703e490SVasu Dev  *
981a703e490SVasu Dev  * Returns: 0 on success
982a703e490SVasu Dev  */
983a703e490SVasu Dev int __exit fcoe_if_exit(void)
984a703e490SVasu Dev {
985e9084bb8SChris Leech 	fc_release_transport(fcoe_transport_template);
986e9084bb8SChris Leech 	fc_release_transport(fcoe_vport_transport_template);
987e9084bb8SChris Leech 	fcoe_transport_template = NULL;
988e9084bb8SChris Leech 	fcoe_vport_transport_template = NULL;
989a703e490SVasu Dev 	return 0;
990a703e490SVasu Dev }
991a703e490SVasu Dev 
992a703e490SVasu Dev /**
9931875f27eSRobert Love  * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
9941875f27eSRobert Love  * @cpu: The CPU index of the CPU to create a receive thread for
995a703e490SVasu Dev  */
996a703e490SVasu Dev static void fcoe_percpu_thread_create(unsigned int cpu)
997a703e490SVasu Dev {
998a703e490SVasu Dev 	struct fcoe_percpu_s *p;
999a703e490SVasu Dev 	struct task_struct *thread;
1000a703e490SVasu Dev 
1001a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1002a703e490SVasu Dev 
1003a703e490SVasu Dev 	thread = kthread_create(fcoe_percpu_receive_thread,
1004a703e490SVasu Dev 				(void *)p, "fcoethread/%d", cpu);
1005a703e490SVasu Dev 
1006e7a51997SJoe Eykholt 	if (likely(!IS_ERR(thread))) {
1007a703e490SVasu Dev 		kthread_bind(thread, cpu);
1008a703e490SVasu Dev 		wake_up_process(thread);
1009a703e490SVasu Dev 
1010a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1011a703e490SVasu Dev 		p->thread = thread;
1012a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1013a703e490SVasu Dev 	}
1014a703e490SVasu Dev }
1015a703e490SVasu Dev 
1016a703e490SVasu Dev /**
10171875f27eSRobert Love  * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
10181875f27eSRobert Love  * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1019a703e490SVasu Dev  *
1020a703e490SVasu Dev  * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1021a703e490SVasu Dev  * current CPU's Rx thread. If the thread being destroyed is bound to
1022a703e490SVasu Dev  * the CPU processing this context the skbs will be freed.
1023a703e490SVasu Dev  */
1024a703e490SVasu Dev static void fcoe_percpu_thread_destroy(unsigned int cpu)
1025a703e490SVasu Dev {
1026a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1027a703e490SVasu Dev 	struct task_struct *thread;
1028a703e490SVasu Dev 	struct page *crc_eof;
1029a703e490SVasu Dev 	struct sk_buff *skb;
1030a703e490SVasu Dev #ifdef CONFIG_SMP
1031a703e490SVasu Dev 	struct fcoe_percpu_s *p0;
1032a703e490SVasu Dev 	unsigned targ_cpu = smp_processor_id();
1033a703e490SVasu Dev #endif /* CONFIG_SMP */
1034a703e490SVasu Dev 
1035d5488eb9SRobert Love 	FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1036a703e490SVasu Dev 
1037a703e490SVasu Dev 	/* Prevent any new skbs from being queued for this CPU. */
1038a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1039a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1040a703e490SVasu Dev 	thread = p->thread;
1041a703e490SVasu Dev 	p->thread = NULL;
1042a703e490SVasu Dev 	crc_eof = p->crc_eof_page;
1043a703e490SVasu Dev 	p->crc_eof_page = NULL;
1044a703e490SVasu Dev 	p->crc_eof_offset = 0;
1045a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1046a703e490SVasu Dev 
1047a703e490SVasu Dev #ifdef CONFIG_SMP
1048a703e490SVasu Dev 	/*
1049a703e490SVasu Dev 	 * Don't bother moving the skb's if this context is running
1050a703e490SVasu Dev 	 * on the same CPU that is having its thread destroyed. This
1051a703e490SVasu Dev 	 * can easily happen when the module is removed.
1052a703e490SVasu Dev 	 */
1053a703e490SVasu Dev 	if (cpu != targ_cpu) {
1054a703e490SVasu Dev 		p0 = &per_cpu(fcoe_percpu, targ_cpu);
1055a703e490SVasu Dev 		spin_lock_bh(&p0->fcoe_rx_list.lock);
1056a703e490SVasu Dev 		if (p0->thread) {
1057d5488eb9SRobert Love 			FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1058a703e490SVasu Dev 				 cpu, targ_cpu);
1059a703e490SVasu Dev 
1060a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1061a703e490SVasu Dev 				__skb_queue_tail(&p0->fcoe_rx_list, skb);
1062a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1063a703e490SVasu Dev 		} else {
1064a703e490SVasu Dev 			/*
1065a703e490SVasu Dev 			 * The targeted CPU is not initialized and cannot accept
1066a703e490SVasu Dev 			 * new	skbs. Unlock the targeted CPU and drop the skbs
1067a703e490SVasu Dev 			 * on the CPU that is going offline.
1068a703e490SVasu Dev 			 */
1069a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1070a703e490SVasu Dev 				kfree_skb(skb);
1071a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1072a703e490SVasu Dev 		}
1073a703e490SVasu Dev 	} else {
1074a703e490SVasu Dev 		/*
1075a703e490SVasu Dev 		 * This scenario occurs when the module is being removed
1076a703e490SVasu Dev 		 * and all threads are being destroyed. skbs will continue
1077a703e490SVasu Dev 		 * to be shifted from the CPU thread that is being removed
1078a703e490SVasu Dev 		 * to the CPU thread associated with the CPU that is processing
1079a703e490SVasu Dev 		 * the module removal. Once there is only one CPU Rx thread it
1080a703e490SVasu Dev 		 * will reach this case and we will drop all skbs and later
1081a703e490SVasu Dev 		 * stop the thread.
1082a703e490SVasu Dev 		 */
1083a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1084a703e490SVasu Dev 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1085a703e490SVasu Dev 			kfree_skb(skb);
1086a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1087a703e490SVasu Dev 	}
1088a703e490SVasu Dev #else
1089a703e490SVasu Dev 	/*
1090dd3fd72eSChris Leech 	 * This a non-SMP scenario where the singular Rx thread is
1091a703e490SVasu Dev 	 * being removed. Free all skbs and stop the thread.
1092a703e490SVasu Dev 	 */
1093a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1094a703e490SVasu Dev 	while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1095a703e490SVasu Dev 		kfree_skb(skb);
1096a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1097a703e490SVasu Dev #endif
1098a703e490SVasu Dev 
1099a703e490SVasu Dev 	if (thread)
1100a703e490SVasu Dev 		kthread_stop(thread);
1101a703e490SVasu Dev 
1102a703e490SVasu Dev 	if (crc_eof)
1103a703e490SVasu Dev 		put_page(crc_eof);
1104a703e490SVasu Dev }
1105a703e490SVasu Dev 
1106a703e490SVasu Dev /**
11071875f27eSRobert Love  * fcoe_cpu_callback() - Handler for CPU hotplug events
11081875f27eSRobert Love  * @nfb:    The callback data block
11091875f27eSRobert Love  * @action: The event triggering the callback
11101875f27eSRobert Love  * @hcpu:   The index of the CPU that the event is for
1111a703e490SVasu Dev  *
11121875f27eSRobert Love  * This creates or destroys per-CPU data for fcoe
1113a703e490SVasu Dev  *
1114a703e490SVasu Dev  * Returns NOTIFY_OK always.
1115a703e490SVasu Dev  */
1116a703e490SVasu Dev static int fcoe_cpu_callback(struct notifier_block *nfb,
1117a703e490SVasu Dev 			     unsigned long action, void *hcpu)
1118a703e490SVasu Dev {
1119a703e490SVasu Dev 	unsigned cpu = (unsigned long)hcpu;
1120a703e490SVasu Dev 
1121a703e490SVasu Dev 	switch (action) {
1122a703e490SVasu Dev 	case CPU_ONLINE:
1123a703e490SVasu Dev 	case CPU_ONLINE_FROZEN:
1124d5488eb9SRobert Love 		FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1125a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
1126a703e490SVasu Dev 		break;
1127a703e490SVasu Dev 	case CPU_DEAD:
1128a703e490SVasu Dev 	case CPU_DEAD_FROZEN:
1129d5488eb9SRobert Love 		FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1130a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
1131a703e490SVasu Dev 		break;
1132a703e490SVasu Dev 	default:
1133a703e490SVasu Dev 		break;
1134a703e490SVasu Dev 	}
1135a703e490SVasu Dev 	return NOTIFY_OK;
1136a703e490SVasu Dev }
1137a703e490SVasu Dev 
1138a703e490SVasu Dev /**
11391875f27eSRobert Love  * fcoe_rcv() - Receive packets from a net device
11401875f27eSRobert Love  * @skb:    The received packet
11411875f27eSRobert Love  * @netdev: The net device that the packet was received on
11421875f27eSRobert Love  * @ptype:  The packet type context
11431875f27eSRobert Love  * @olddev: The last device net device
1144a703e490SVasu Dev  *
11451875f27eSRobert Love  * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
11461875f27eSRobert Love  * FC frame and passes the frame to libfc.
1147a703e490SVasu Dev  *
1148a703e490SVasu Dev  * Returns: 0 for success
1149a703e490SVasu Dev  */
11501875f27eSRobert Love int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1151a703e490SVasu Dev 	     struct packet_type *ptype, struct net_device *olddev)
1152a703e490SVasu Dev {
11531875f27eSRobert Love 	struct fc_lport *lport;
1154a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1155259ad85dSChris Leech 	struct fcoe_interface *fcoe;
1156a703e490SVasu Dev 	struct fc_frame_header *fh;
1157a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1158b2f0091fSVasu Dev 	unsigned int cpu;
1159a703e490SVasu Dev 
1160259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
11611875f27eSRobert Love 	lport = fcoe->ctlr.lp;
11621875f27eSRobert Love 	if (unlikely(!lport)) {
11631875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1164a703e490SVasu Dev 		goto err2;
1165a703e490SVasu Dev 	}
11661875f27eSRobert Love 	if (!lport->link_up)
116797c8389dSJoe Eykholt 		goto err2;
1168a703e490SVasu Dev 
11691875f27eSRobert Love 	FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1170d5488eb9SRobert Love 			"data:%p tail:%p end:%p sum:%d dev:%s",
1171d5488eb9SRobert Love 			skb->len, skb->data_len, skb->head, skb->data,
1172d5488eb9SRobert Love 			skb_tail_pointer(skb), skb_end_pointer(skb),
1173d5488eb9SRobert Love 			skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1174a703e490SVasu Dev 
1175a703e490SVasu Dev 	/* check for FCOE packet type */
1176a703e490SVasu Dev 	if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
11771875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "Wrong FC type frame");
1178a703e490SVasu Dev 		goto err;
1179a703e490SVasu Dev 	}
1180a703e490SVasu Dev 
1181a703e490SVasu Dev 	/*
1182a703e490SVasu Dev 	 * Check for minimum frame length, and make sure required FCoE
1183a703e490SVasu Dev 	 * and FC headers are pulled into the linear data area.
1184a703e490SVasu Dev 	 */
1185a703e490SVasu Dev 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1186a703e490SVasu Dev 		     !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1187a703e490SVasu Dev 		goto err;
1188a703e490SVasu Dev 
1189a703e490SVasu Dev 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1190a703e490SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
1191a703e490SVasu Dev 
1192a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
11931875f27eSRobert Love 	fr->fr_dev = lport;
1194a703e490SVasu Dev 	fr->ptype = ptype;
1195a703e490SVasu Dev 
1196a703e490SVasu Dev 	/*
1197b2f0091fSVasu Dev 	 * In case the incoming frame's exchange is originated from
1198b2f0091fSVasu Dev 	 * the initiator, then received frame's exchange id is ANDed
1199b2f0091fSVasu Dev 	 * with fc_cpu_mask bits to get the same cpu on which exchange
1200b2f0091fSVasu Dev 	 * was originated, otherwise just use the current cpu.
1201a703e490SVasu Dev 	 */
1202b2f0091fSVasu Dev 	if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1203b2f0091fSVasu Dev 		cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1204b2f0091fSVasu Dev 	else
1205b2f0091fSVasu Dev 		cpu = smp_processor_id();
1206a703e490SVasu Dev 
1207a703e490SVasu Dev 	fps = &per_cpu(fcoe_percpu, cpu);
1208a703e490SVasu Dev 	spin_lock_bh(&fps->fcoe_rx_list.lock);
1209a703e490SVasu Dev 	if (unlikely(!fps->thread)) {
1210a703e490SVasu Dev 		/*
1211a703e490SVasu Dev 		 * The targeted CPU is not ready, let's target
1212a703e490SVasu Dev 		 * the first CPU now. For non-SMP systems this
1213a703e490SVasu Dev 		 * will check the same CPU twice.
1214a703e490SVasu Dev 		 */
12151875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1216d5488eb9SRobert Love 				"ready for incoming skb- using first online "
1217d5488eb9SRobert Love 				"CPU.\n");
1218a703e490SVasu Dev 
1219a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1220a703e490SVasu Dev 		cpu = first_cpu(cpu_online_map);
1221a703e490SVasu Dev 		fps = &per_cpu(fcoe_percpu, cpu);
1222a703e490SVasu Dev 		spin_lock_bh(&fps->fcoe_rx_list.lock);
1223a703e490SVasu Dev 		if (!fps->thread) {
1224a703e490SVasu Dev 			spin_unlock_bh(&fps->fcoe_rx_list.lock);
1225a703e490SVasu Dev 			goto err;
1226a703e490SVasu Dev 		}
1227a703e490SVasu Dev 	}
1228a703e490SVasu Dev 
1229a703e490SVasu Dev 	/*
1230a703e490SVasu Dev 	 * We now have a valid CPU that we're targeting for
1231a703e490SVasu Dev 	 * this skb. We also have this receive thread locked,
1232a703e490SVasu Dev 	 * so we're free to queue skbs into it's queue.
1233a703e490SVasu Dev 	 */
1234a703e490SVasu Dev 	__skb_queue_tail(&fps->fcoe_rx_list, skb);
1235a703e490SVasu Dev 	if (fps->fcoe_rx_list.qlen == 1)
1236a703e490SVasu Dev 		wake_up_process(fps->thread);
1237a703e490SVasu Dev 
1238a703e490SVasu Dev 	spin_unlock_bh(&fps->fcoe_rx_list.lock);
1239a703e490SVasu Dev 
1240a703e490SVasu Dev 	return 0;
1241a703e490SVasu Dev err:
12421875f27eSRobert Love 	fc_lport_get_stats(lport)->ErrorFrames++;
1243a703e490SVasu Dev 
1244a703e490SVasu Dev err2:
1245a703e490SVasu Dev 	kfree_skb(skb);
1246a703e490SVasu Dev 	return -1;
1247a703e490SVasu Dev }
1248a703e490SVasu Dev 
1249a703e490SVasu Dev /**
12501875f27eSRobert Love  * fcoe_start_io() - Start FCoE I/O
12511875f27eSRobert Love  * @skb: The packet to be transmitted
12521875f27eSRobert Love  *
12531875f27eSRobert Love  * This routine is called from the net device to start transmitting
12541875f27eSRobert Love  * FCoE packets.
1255a703e490SVasu Dev  *
1256a703e490SVasu Dev  * Returns: 0 for success
1257a703e490SVasu Dev  */
1258a703e490SVasu Dev static inline int fcoe_start_io(struct sk_buff *skb)
1259a703e490SVasu Dev {
1260a703e490SVasu Dev 	int rc;
1261a703e490SVasu Dev 
1262a703e490SVasu Dev 	skb_get(skb);
1263a703e490SVasu Dev 	rc = dev_queue_xmit(skb);
1264a703e490SVasu Dev 	if (rc != 0)
1265a703e490SVasu Dev 		return rc;
1266a703e490SVasu Dev 	kfree_skb(skb);
1267a703e490SVasu Dev 	return 0;
1268a703e490SVasu Dev }
1269a703e490SVasu Dev 
1270a703e490SVasu Dev /**
12711875f27eSRobert Love  * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
12721875f27eSRobert Love  * @skb:  The packet to be transmitted
12731875f27eSRobert Love  * @tlen: The total length of the trailer
12741875f27eSRobert Love  *
12751875f27eSRobert Love  * This routine allocates a page for frame trailers. The page is re-used if
12761875f27eSRobert Love  * there is enough room left on it for the current trailer. If there isn't
12771875f27eSRobert Love  * enough buffer left a new page is allocated for the trailer. Reference to
12781875f27eSRobert Love  * the page from this function as well as the skbs using the page fragments
12791875f27eSRobert Love  * ensure that the page is freed at the appropriate time.
1280a703e490SVasu Dev  *
1281a703e490SVasu Dev  * Returns: 0 for success
1282a703e490SVasu Dev  */
1283a703e490SVasu Dev static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1284a703e490SVasu Dev {
1285a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1286a703e490SVasu Dev 	struct page *page;
1287a703e490SVasu Dev 
1288a703e490SVasu Dev 	fps = &get_cpu_var(fcoe_percpu);
1289a703e490SVasu Dev 	page = fps->crc_eof_page;
1290a703e490SVasu Dev 	if (!page) {
1291a703e490SVasu Dev 		page = alloc_page(GFP_ATOMIC);
1292a703e490SVasu Dev 		if (!page) {
1293a703e490SVasu Dev 			put_cpu_var(fcoe_percpu);
1294a703e490SVasu Dev 			return -ENOMEM;
1295a703e490SVasu Dev 		}
1296a703e490SVasu Dev 		fps->crc_eof_page = page;
1297a703e490SVasu Dev 		fps->crc_eof_offset = 0;
1298a703e490SVasu Dev 	}
1299a703e490SVasu Dev 
1300a703e490SVasu Dev 	get_page(page);
1301a703e490SVasu Dev 	skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1302a703e490SVasu Dev 			   fps->crc_eof_offset, tlen);
1303a703e490SVasu Dev 	skb->len += tlen;
1304a703e490SVasu Dev 	skb->data_len += tlen;
1305a703e490SVasu Dev 	skb->truesize += tlen;
1306a703e490SVasu Dev 	fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1307a703e490SVasu Dev 
1308a703e490SVasu Dev 	if (fps->crc_eof_offset >= PAGE_SIZE) {
1309a703e490SVasu Dev 		fps->crc_eof_page = NULL;
1310a703e490SVasu Dev 		fps->crc_eof_offset = 0;
1311a703e490SVasu Dev 		put_page(page);
1312a703e490SVasu Dev 	}
1313a703e490SVasu Dev 	put_cpu_var(fcoe_percpu);
1314a703e490SVasu Dev 	return 0;
1315a703e490SVasu Dev }
1316a703e490SVasu Dev 
1317a703e490SVasu Dev /**
13181875f27eSRobert Love  * fcoe_fc_crc() - Calculates the CRC for a given frame
13191875f27eSRobert Love  * @fp: The frame to be checksumed
1320a703e490SVasu Dev  *
13211875f27eSRobert Love  * This uses crc32() routine to calculate the CRC for a frame
13221875f27eSRobert Love  *
13231875f27eSRobert Love  * Return: The 32 bit CRC value
1324a703e490SVasu Dev  */
1325a703e490SVasu Dev u32 fcoe_fc_crc(struct fc_frame *fp)
1326a703e490SVasu Dev {
1327a703e490SVasu Dev 	struct sk_buff *skb = fp_skb(fp);
1328a703e490SVasu Dev 	struct skb_frag_struct *frag;
1329a703e490SVasu Dev 	unsigned char *data;
1330a703e490SVasu Dev 	unsigned long off, len, clen;
1331a703e490SVasu Dev 	u32 crc;
1332a703e490SVasu Dev 	unsigned i;
1333a703e490SVasu Dev 
1334a703e490SVasu Dev 	crc = crc32(~0, skb->data, skb_headlen(skb));
1335a703e490SVasu Dev 
1336a703e490SVasu Dev 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1337a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[i];
1338a703e490SVasu Dev 		off = frag->page_offset;
1339a703e490SVasu Dev 		len = frag->size;
1340a703e490SVasu Dev 		while (len > 0) {
1341a703e490SVasu Dev 			clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1342a703e490SVasu Dev 			data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1343a703e490SVasu Dev 					   KM_SKB_DATA_SOFTIRQ);
1344a703e490SVasu Dev 			crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1345a703e490SVasu Dev 			kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1346a703e490SVasu Dev 			off += clen;
1347a703e490SVasu Dev 			len -= clen;
1348a703e490SVasu Dev 		}
1349a703e490SVasu Dev 	}
1350a703e490SVasu Dev 	return crc;
1351a703e490SVasu Dev }
1352a703e490SVasu Dev 
1353a703e490SVasu Dev /**
13541875f27eSRobert Love  * fcoe_xmit() - Transmit a FCoE frame
13551875f27eSRobert Love  * @lport: The local port that the frame is to be transmitted for
13561875f27eSRobert Love  * @fp:	   The frame to be transmitted
1357a703e490SVasu Dev  *
1358a703e490SVasu Dev  * Return: 0 for success
1359a703e490SVasu Dev  */
13601875f27eSRobert Love int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1361a703e490SVasu Dev {
13624bb6b515SVasu Dev 	int wlen;
1363a703e490SVasu Dev 	u32 crc;
1364a703e490SVasu Dev 	struct ethhdr *eh;
1365a703e490SVasu Dev 	struct fcoe_crc_eof *cp;
1366a703e490SVasu Dev 	struct sk_buff *skb;
1367a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1368a703e490SVasu Dev 	struct fc_frame_header *fh;
1369a703e490SVasu Dev 	unsigned int hlen;		/* header length implies the version */
1370a703e490SVasu Dev 	unsigned int tlen;		/* trailer length */
1371a703e490SVasu Dev 	unsigned int elen;		/* eth header, may include vlan */
13721875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
13733fe9a0baSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
1374a703e490SVasu Dev 	u8 sof, eof;
1375a703e490SVasu Dev 	struct fcoe_hdr *hp;
1376a703e490SVasu Dev 
1377a703e490SVasu Dev 	WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1378a703e490SVasu Dev 
1379a703e490SVasu Dev 	fh = fc_frame_header_get(fp);
138097c8389dSJoe Eykholt 	skb = fp_skb(fp);
138197c8389dSJoe Eykholt 	wlen = skb->len / FCOE_WORD_TO_BYTE;
138297c8389dSJoe Eykholt 
13831875f27eSRobert Love 	if (!lport->link_up) {
13843caf02eeSDan Carpenter 		kfree_skb(skb);
138597c8389dSJoe Eykholt 		return 0;
1386a703e490SVasu Dev 	}
1387a703e490SVasu Dev 
138897c8389dSJoe Eykholt 	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
13891875f27eSRobert Love 	    fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
139097c8389dSJoe Eykholt 		return 0;
139197c8389dSJoe Eykholt 
1392a703e490SVasu Dev 	sof = fr_sof(fp);
1393a703e490SVasu Dev 	eof = fr_eof(fp);
1394a703e490SVasu Dev 
13954e57e1cbSVasu Dev 	elen = sizeof(struct ethhdr);
1396a703e490SVasu Dev 	hlen = sizeof(struct fcoe_hdr);
1397a703e490SVasu Dev 	tlen = sizeof(struct fcoe_crc_eof);
1398a703e490SVasu Dev 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1399a703e490SVasu Dev 
1400a703e490SVasu Dev 	/* crc offload */
14011875f27eSRobert Love 	if (likely(lport->crc_offload)) {
1402a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_PARTIAL;
1403a703e490SVasu Dev 		skb->csum_start = skb_headroom(skb);
1404a703e490SVasu Dev 		skb->csum_offset = skb->len;
1405a703e490SVasu Dev 		crc = 0;
1406a703e490SVasu Dev 	} else {
1407a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_NONE;
1408a703e490SVasu Dev 		crc = fcoe_fc_crc(fp);
1409a703e490SVasu Dev 	}
1410a703e490SVasu Dev 
1411014f5c3fSChris Leech 	/* copy port crc and eof to the skb buff */
1412a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1413a703e490SVasu Dev 		skb_frag_t *frag;
1414a703e490SVasu Dev 		if (fcoe_get_paged_crc_eof(skb, tlen)) {
1415a703e490SVasu Dev 			kfree_skb(skb);
1416a703e490SVasu Dev 			return -ENOMEM;
1417a703e490SVasu Dev 		}
1418a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1419a703e490SVasu Dev 		cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1420a703e490SVasu Dev 			+ frag->page_offset;
1421a703e490SVasu Dev 	} else {
1422a703e490SVasu Dev 		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1423a703e490SVasu Dev 	}
1424a703e490SVasu Dev 
1425a703e490SVasu Dev 	memset(cp, 0, sizeof(*cp));
1426a703e490SVasu Dev 	cp->fcoe_eof = eof;
1427a703e490SVasu Dev 	cp->fcoe_crc32 = cpu_to_le32(~crc);
1428a703e490SVasu Dev 
1429a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1430a703e490SVasu Dev 		kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1431a703e490SVasu Dev 		cp = NULL;
1432a703e490SVasu Dev 	}
1433a703e490SVasu Dev 
1434014f5c3fSChris Leech 	/* adjust skb network/transport offsets to match mac/fcoe/port */
1435a703e490SVasu Dev 	skb_push(skb, elen + hlen);
1436a703e490SVasu Dev 	skb_reset_mac_header(skb);
1437a703e490SVasu Dev 	skb_reset_network_header(skb);
1438a703e490SVasu Dev 	skb->mac_len = elen;
1439a703e490SVasu Dev 	skb->protocol = htons(ETH_P_FCOE);
14403fe9a0baSChris Leech 	skb->dev = fcoe->netdev;
1441a703e490SVasu Dev 
1442a703e490SVasu Dev 	/* fill up mac and fcoe headers */
1443a703e490SVasu Dev 	eh = eth_hdr(skb);
1444a703e490SVasu Dev 	eh->h_proto = htons(ETH_P_FCOE);
14453fe9a0baSChris Leech 	if (fcoe->ctlr.map_dest)
1446a703e490SVasu Dev 		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1447a703e490SVasu Dev 	else
1448a703e490SVasu Dev 		/* insert GW address */
14493fe9a0baSChris Leech 		memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1450a703e490SVasu Dev 
14513fe9a0baSChris Leech 	if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
14523fe9a0baSChris Leech 		memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1453a703e490SVasu Dev 	else
145411b56188SChris Leech 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1455a703e490SVasu Dev 
1456a703e490SVasu Dev 	hp = (struct fcoe_hdr *)(eh + 1);
1457a703e490SVasu Dev 	memset(hp, 0, sizeof(*hp));
1458a703e490SVasu Dev 	if (FC_FCOE_VER)
1459a703e490SVasu Dev 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1460a703e490SVasu Dev 	hp->fcoe_sof = sof;
1461a703e490SVasu Dev 
1462a703e490SVasu Dev 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
14631875f27eSRobert Love 	if (lport->seq_offload && fr_max_payload(fp)) {
1464a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1465a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1466a703e490SVasu Dev 	} else {
1467a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = 0;
1468a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = 0;
1469a703e490SVasu Dev 	}
1470a703e490SVasu Dev 	/* update tx stats: regardless if LLD fails */
14711875f27eSRobert Love 	stats = fc_lport_get_stats(lport);
1472a703e490SVasu Dev 	stats->TxFrames++;
1473a703e490SVasu Dev 	stats->TxWords += wlen;
1474a703e490SVasu Dev 
1475a703e490SVasu Dev 	/* send down to lld */
14761875f27eSRobert Love 	fr_dev(fp) = lport;
1477014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen)
14781875f27eSRobert Love 		fcoe_check_wait_queue(lport, skb);
14794bb6b515SVasu Dev 	else if (fcoe_start_io(skb))
14801875f27eSRobert Love 		fcoe_check_wait_queue(lport, skb);
1481a703e490SVasu Dev 
1482a703e490SVasu Dev 	return 0;
1483a703e490SVasu Dev }
1484a703e490SVasu Dev 
1485a703e490SVasu Dev /**
14861875f27eSRobert Love  * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
14871875f27eSRobert Love  * @skb: The completed skb (argument required by destructor)
1488e7a51997SJoe Eykholt  */
1489e7a51997SJoe Eykholt static void fcoe_percpu_flush_done(struct sk_buff *skb)
1490e7a51997SJoe Eykholt {
1491e7a51997SJoe Eykholt 	complete(&fcoe_flush_completion);
1492e7a51997SJoe Eykholt }
1493e7a51997SJoe Eykholt 
1494e7a51997SJoe Eykholt /**
14951875f27eSRobert Love  * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
14961875f27eSRobert Love  * @arg: The per-CPU context
1497a703e490SVasu Dev  *
1498a703e490SVasu Dev  * Return: 0 for success
1499a703e490SVasu Dev  */
1500a703e490SVasu Dev int fcoe_percpu_receive_thread(void *arg)
1501a703e490SVasu Dev {
1502a703e490SVasu Dev 	struct fcoe_percpu_s *p = arg;
1503a703e490SVasu Dev 	u32 fr_len;
15041875f27eSRobert Love 	struct fc_lport *lport;
1505a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1506a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1507a703e490SVasu Dev 	struct fc_frame_header *fh;
1508a703e490SVasu Dev 	struct sk_buff *skb;
1509a703e490SVasu Dev 	struct fcoe_crc_eof crc_eof;
1510a703e490SVasu Dev 	struct fc_frame *fp;
1511a703e490SVasu Dev 	u8 *mac = NULL;
1512014f5c3fSChris Leech 	struct fcoe_port *port;
1513a703e490SVasu Dev 	struct fcoe_hdr *hp;
1514a703e490SVasu Dev 
1515a703e490SVasu Dev 	set_user_nice(current, -20);
1516a703e490SVasu Dev 
1517a703e490SVasu Dev 	while (!kthread_should_stop()) {
1518a703e490SVasu Dev 
1519a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1520a703e490SVasu Dev 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1521a703e490SVasu Dev 			set_current_state(TASK_INTERRUPTIBLE);
1522a703e490SVasu Dev 			spin_unlock_bh(&p->fcoe_rx_list.lock);
1523a703e490SVasu Dev 			schedule();
1524a703e490SVasu Dev 			set_current_state(TASK_RUNNING);
1525a703e490SVasu Dev 			if (kthread_should_stop())
1526a703e490SVasu Dev 				return 0;
1527a703e490SVasu Dev 			spin_lock_bh(&p->fcoe_rx_list.lock);
1528a703e490SVasu Dev 		}
1529a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1530a703e490SVasu Dev 		fr = fcoe_dev_from_skb(skb);
15311875f27eSRobert Love 		lport = fr->fr_dev;
15321875f27eSRobert Love 		if (unlikely(!lport)) {
1533e7a51997SJoe Eykholt 			if (skb->destructor != fcoe_percpu_flush_done)
1534e7a51997SJoe Eykholt 				FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1535a703e490SVasu Dev 			kfree_skb(skb);
1536a703e490SVasu Dev 			continue;
1537a703e490SVasu Dev 		}
1538a703e490SVasu Dev 
1539d5488eb9SRobert Love 		FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1540d5488eb9SRobert Love 				"head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1541a703e490SVasu Dev 				skb->len, skb->data_len,
1542a703e490SVasu Dev 				skb->head, skb->data, skb_tail_pointer(skb),
1543a703e490SVasu Dev 				skb_end_pointer(skb), skb->csum,
1544a703e490SVasu Dev 				skb->dev ? skb->dev->name : "<NULL>");
1545a703e490SVasu Dev 
1546a703e490SVasu Dev 		/*
1547a703e490SVasu Dev 		 * Save source MAC address before discarding header.
1548a703e490SVasu Dev 		 */
15491875f27eSRobert Love 		port = lport_priv(lport);
1550a703e490SVasu Dev 		if (skb_is_nonlinear(skb))
1551a703e490SVasu Dev 			skb_linearize(skb);	/* not ideal */
155297c8389dSJoe Eykholt 		mac = eth_hdr(skb)->h_source;
1553a703e490SVasu Dev 
1554a703e490SVasu Dev 		/*
1555a703e490SVasu Dev 		 * Frame length checks and setting up the header pointers
1556a703e490SVasu Dev 		 * was done in fcoe_rcv already.
1557a703e490SVasu Dev 		 */
1558a703e490SVasu Dev 		hp = (struct fcoe_hdr *) skb_network_header(skb);
1559a703e490SVasu Dev 		fh = (struct fc_frame_header *) skb_transport_header(skb);
1560a703e490SVasu Dev 
15611875f27eSRobert Love 		stats = fc_lport_get_stats(lport);
1562a703e490SVasu Dev 		if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1563a703e490SVasu Dev 			if (stats->ErrorFrames < 5)
1564d5488eb9SRobert Love 				printk(KERN_WARNING "fcoe: FCoE version "
1565a703e490SVasu Dev 				       "mismatch: The frame has "
1566a703e490SVasu Dev 				       "version %x, but the "
1567a703e490SVasu Dev 				       "initiator supports version "
1568a703e490SVasu Dev 				       "%x\n", FC_FCOE_DECAPS_VER(hp),
1569a703e490SVasu Dev 				       FC_FCOE_VER);
1570a703e490SVasu Dev 			stats->ErrorFrames++;
1571a703e490SVasu Dev 			kfree_skb(skb);
1572a703e490SVasu Dev 			continue;
1573a703e490SVasu Dev 		}
1574a703e490SVasu Dev 
1575a703e490SVasu Dev 		skb_pull(skb, sizeof(struct fcoe_hdr));
1576a703e490SVasu Dev 		fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1577a703e490SVasu Dev 
1578a703e490SVasu Dev 		stats->RxFrames++;
1579a703e490SVasu Dev 		stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1580a703e490SVasu Dev 
1581a703e490SVasu Dev 		fp = (struct fc_frame *)skb;
1582a703e490SVasu Dev 		fc_frame_init(fp);
15831875f27eSRobert Love 		fr_dev(fp) = lport;
1584a703e490SVasu Dev 		fr_sof(fp) = hp->fcoe_sof;
1585a703e490SVasu Dev 
1586a703e490SVasu Dev 		/* Copy out the CRC and EOF trailer for access */
1587a703e490SVasu Dev 		if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1588a703e490SVasu Dev 			kfree_skb(skb);
1589a703e490SVasu Dev 			continue;
1590a703e490SVasu Dev 		}
1591a703e490SVasu Dev 		fr_eof(fp) = crc_eof.fcoe_eof;
1592a703e490SVasu Dev 		fr_crc(fp) = crc_eof.fcoe_crc32;
1593a703e490SVasu Dev 		if (pskb_trim(skb, fr_len)) {
1594a703e490SVasu Dev 			kfree_skb(skb);
1595a703e490SVasu Dev 			continue;
1596a703e490SVasu Dev 		}
1597a703e490SVasu Dev 
1598a703e490SVasu Dev 		/*
1599a703e490SVasu Dev 		 * We only check CRC if no offload is available and if it is
1600a703e490SVasu Dev 		 * it's solicited data, in which case, the FCP layer would
1601a703e490SVasu Dev 		 * check it during the copy.
1602a703e490SVasu Dev 		 */
16031875f27eSRobert Love 		if (lport->crc_offload &&
16041875f27eSRobert Love 		    skb->ip_summed == CHECKSUM_UNNECESSARY)
1605a703e490SVasu Dev 			fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1606a703e490SVasu Dev 		else
1607a703e490SVasu Dev 			fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1608a703e490SVasu Dev 
1609a703e490SVasu Dev 		fh = fc_frame_header_get(fp);
1610a703e490SVasu Dev 		if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1611a703e490SVasu Dev 		    fh->fh_type == FC_TYPE_FCP) {
16121875f27eSRobert Love 			fc_exch_recv(lport, fp);
1613a703e490SVasu Dev 			continue;
1614a703e490SVasu Dev 		}
1615a703e490SVasu Dev 		if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1616a703e490SVasu Dev 			if (le32_to_cpu(fr_crc(fp)) !=
1617a703e490SVasu Dev 			    ~crc32(~0, skb->data, fr_len)) {
1618d5488eb9SRobert Love 				if (stats->InvalidCRCCount < 5)
1619a703e490SVasu Dev 					printk(KERN_WARNING "fcoe: dropping "
1620a703e490SVasu Dev 					       "frame with CRC error\n");
1621a703e490SVasu Dev 				stats->InvalidCRCCount++;
1622a703e490SVasu Dev 				stats->ErrorFrames++;
1623a703e490SVasu Dev 				fc_frame_free(fp);
1624a703e490SVasu Dev 				continue;
1625a703e490SVasu Dev 			}
1626a703e490SVasu Dev 			fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1627a703e490SVasu Dev 		}
16281875f27eSRobert Love 		fc_exch_recv(lport, fp);
1629a703e490SVasu Dev 	}
1630a703e490SVasu Dev 	return 0;
1631a703e490SVasu Dev }
1632a703e490SVasu Dev 
1633a703e490SVasu Dev /**
16341875f27eSRobert Love  * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
16351875f27eSRobert Love  * @lport: The local port whose backlog is to be cleared
1636a703e490SVasu Dev  *
16371875f27eSRobert Love  * This empties the wait_queue, dequeues the head of the wait_queue queue
16381875f27eSRobert Love  * and calls fcoe_start_io() for each packet. If all skb have been
16391875f27eSRobert Love  * transmitted it returns the qlen. If an error occurs it restores
16401875f27eSRobert Love  * wait_queue (to try again later) and returns -1.
1641a703e490SVasu Dev  *
16421875f27eSRobert Love  * The wait_queue is used when the skb transmit fails. The failed skb
16431875f27eSRobert Love  * will go in the wait_queue which will be emptied by the timer function or
1644a703e490SVasu Dev  * by the next skb transmit.
1645a703e490SVasu Dev  */
16461875f27eSRobert Love static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
1647a703e490SVasu Dev {
16481875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
16494bb6b515SVasu Dev 	int rc;
1650a703e490SVasu Dev 
1651014f5c3fSChris Leech 	spin_lock_bh(&port->fcoe_pending_queue.lock);
16524bb6b515SVasu Dev 
16534bb6b515SVasu Dev 	if (skb)
1654014f5c3fSChris Leech 		__skb_queue_tail(&port->fcoe_pending_queue, skb);
16554bb6b515SVasu Dev 
1656014f5c3fSChris Leech 	if (port->fcoe_pending_queue_active)
1657a703e490SVasu Dev 		goto out;
1658014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 1;
1659a703e490SVasu Dev 
1660014f5c3fSChris Leech 	while (port->fcoe_pending_queue.qlen) {
1661a703e490SVasu Dev 		/* keep qlen > 0 until fcoe_start_io succeeds */
1662014f5c3fSChris Leech 		port->fcoe_pending_queue.qlen++;
1663014f5c3fSChris Leech 		skb = __skb_dequeue(&port->fcoe_pending_queue);
1664a703e490SVasu Dev 
1665014f5c3fSChris Leech 		spin_unlock_bh(&port->fcoe_pending_queue.lock);
1666a703e490SVasu Dev 		rc = fcoe_start_io(skb);
1667014f5c3fSChris Leech 		spin_lock_bh(&port->fcoe_pending_queue.lock);
1668a703e490SVasu Dev 
1669a703e490SVasu Dev 		if (rc) {
1670014f5c3fSChris Leech 			__skb_queue_head(&port->fcoe_pending_queue, skb);
1671a703e490SVasu Dev 			/* undo temporary increment above */
1672014f5c3fSChris Leech 			port->fcoe_pending_queue.qlen--;
1673a703e490SVasu Dev 			break;
1674a703e490SVasu Dev 		}
1675a703e490SVasu Dev 		/* undo temporary increment above */
1676014f5c3fSChris Leech 		port->fcoe_pending_queue.qlen--;
1677a703e490SVasu Dev 	}
1678a703e490SVasu Dev 
1679014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
16801875f27eSRobert Love 		lport->qfull = 0;
1681014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1682014f5c3fSChris Leech 		mod_timer(&port->timer, jiffies + 2);
1683014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
1684a703e490SVasu Dev out:
1685014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
16861875f27eSRobert Love 		lport->qfull = 1;
1687014f5c3fSChris Leech 	spin_unlock_bh(&port->fcoe_pending_queue.lock);
16884bb6b515SVasu Dev 	return;
1689a703e490SVasu Dev }
1690a703e490SVasu Dev 
1691a703e490SVasu Dev /**
16921875f27eSRobert Love  * fcoe_dev_setup() - Setup the link change notification interface
1693a703e490SVasu Dev  */
1694b0d428adSRandy Dunlap static void fcoe_dev_setup(void)
1695a703e490SVasu Dev {
1696a703e490SVasu Dev 	register_netdevice_notifier(&fcoe_notifier);
1697a703e490SVasu Dev }
1698a703e490SVasu Dev 
1699a703e490SVasu Dev /**
17001875f27eSRobert Love  * fcoe_dev_cleanup() - Cleanup the link change notification interface
1701a703e490SVasu Dev  */
1702a703e490SVasu Dev static void fcoe_dev_cleanup(void)
1703a703e490SVasu Dev {
1704a703e490SVasu Dev 	unregister_netdevice_notifier(&fcoe_notifier);
1705a703e490SVasu Dev }
1706a703e490SVasu Dev 
1707a703e490SVasu Dev /**
17081875f27eSRobert Love  * fcoe_device_notification() - Handler for net device events
17091875f27eSRobert Love  * @notifier: The context of the notification
17101875f27eSRobert Love  * @event:    The type of event
17111875f27eSRobert Love  * @ptr:      The net device that the event was on
1712a703e490SVasu Dev  *
17131875f27eSRobert Love  * This function is called by the Ethernet driver in case of link change event.
1714a703e490SVasu Dev  *
1715a703e490SVasu Dev  * Returns: 0 for success
1716a703e490SVasu Dev  */
1717a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *notifier,
1718a703e490SVasu Dev 				    ulong event, void *ptr)
1719a703e490SVasu Dev {
17201875f27eSRobert Love 	struct fc_lport *lport = NULL;
17211d1b88dcSVasu Dev 	struct net_device *netdev = ptr;
1722014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
17232e70e241SChris Leech 	struct fcoe_port *port;
1724a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
172597c8389dSJoe Eykholt 	u32 link_possible = 1;
1726a703e490SVasu Dev 	u32 mfs;
1727a703e490SVasu Dev 	int rc = NOTIFY_OK;
1728a703e490SVasu Dev 
1729014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
173025024989SChris Leech 		if (fcoe->netdev == netdev) {
17311875f27eSRobert Love 			lport = fcoe->ctlr.lp;
1732a703e490SVasu Dev 			break;
1733a703e490SVasu Dev 		}
1734a703e490SVasu Dev 	}
17351875f27eSRobert Love 	if (!lport) {
1736a703e490SVasu Dev 		rc = NOTIFY_DONE;
1737a703e490SVasu Dev 		goto out;
1738a703e490SVasu Dev 	}
1739a703e490SVasu Dev 
1740a703e490SVasu Dev 	switch (event) {
1741a703e490SVasu Dev 	case NETDEV_DOWN:
1742a703e490SVasu Dev 	case NETDEV_GOING_DOWN:
174397c8389dSJoe Eykholt 		link_possible = 0;
1744a703e490SVasu Dev 		break;
1745a703e490SVasu Dev 	case NETDEV_UP:
1746a703e490SVasu Dev 	case NETDEV_CHANGE:
1747a703e490SVasu Dev 		break;
1748a703e490SVasu Dev 	case NETDEV_CHANGEMTU:
17497221d7e5SYi Zou 		if (netdev->features & NETIF_F_FCOE_MTU)
17507221d7e5SYi Zou 			break;
17511d1b88dcSVasu Dev 		mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1752a703e490SVasu Dev 				     sizeof(struct fcoe_crc_eof));
1753a703e490SVasu Dev 		if (mfs >= FC_MIN_MAX_FRAME)
17541875f27eSRobert Love 			fc_set_mfs(lport, mfs);
1755a703e490SVasu Dev 		break;
1756a703e490SVasu Dev 	case NETDEV_REGISTER:
1757a703e490SVasu Dev 		break;
17582e70e241SChris Leech 	case NETDEV_UNREGISTER:
17592e70e241SChris Leech 		list_del(&fcoe->list);
17602e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
17612e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
17622e70e241SChris Leech 		schedule_work(&port->destroy_work);
17632e70e241SChris Leech 		goto out;
17642e70e241SChris Leech 		break;
1765a703e490SVasu Dev 	default:
17661d1b88dcSVasu Dev 		FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1767d5488eb9SRobert Love 				"from netdev netlink\n", event);
1768a703e490SVasu Dev 	}
17691875f27eSRobert Love 	if (link_possible && !fcoe_link_ok(lport))
17703fe9a0baSChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
17713fe9a0baSChris Leech 	else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
17721875f27eSRobert Love 		stats = fc_lport_get_stats(lport);
1773a703e490SVasu Dev 		stats->LinkFailureCount++;
17741875f27eSRobert Love 		fcoe_clean_pending_queue(lport);
1775a703e490SVasu Dev 	}
1776a703e490SVasu Dev out:
1777a703e490SVasu Dev 	return rc;
1778a703e490SVasu Dev }
1779a703e490SVasu Dev 
1780a703e490SVasu Dev /**
17811875f27eSRobert Love  * fcoe_if_to_netdev() - Parse a name buffer to get a net device
17821875f27eSRobert Love  * @buffer: The name of the net device
1783a703e490SVasu Dev  *
17841875f27eSRobert Love  * Returns: NULL or a ptr to net_device
1785a703e490SVasu Dev  */
1786a703e490SVasu Dev static struct net_device *fcoe_if_to_netdev(const char *buffer)
1787a703e490SVasu Dev {
1788a703e490SVasu Dev 	char *cp;
1789a703e490SVasu Dev 	char ifname[IFNAMSIZ + 2];
1790a703e490SVasu Dev 
1791a703e490SVasu Dev 	if (buffer) {
1792a703e490SVasu Dev 		strlcpy(ifname, buffer, IFNAMSIZ);
1793a703e490SVasu Dev 		cp = ifname + strlen(ifname);
1794a703e490SVasu Dev 		while (--cp >= ifname && *cp == '\n')
1795a703e490SVasu Dev 			*cp = '\0';
1796a703e490SVasu Dev 		return dev_get_by_name(&init_net, ifname);
1797a703e490SVasu Dev 	}
1798a703e490SVasu Dev 	return NULL;
1799a703e490SVasu Dev }
1800a703e490SVasu Dev 
1801a703e490SVasu Dev /**
18021875f27eSRobert Love  * fcoe_destroy() - Destroy a FCoE interface
18031875f27eSRobert Love  * @buffer: The name of the Ethernet interface to be destroyed
18041875f27eSRobert Love  * @kp:	    The associated kernel parameter
18051875f27eSRobert Love  *
18061875f27eSRobert Love  * Called from sysfs.
1807a703e490SVasu Dev  *
1808a703e490SVasu Dev  * Returns: 0 for success
1809a703e490SVasu Dev  */
1810a703e490SVasu Dev static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1811a703e490SVasu Dev {
1812030f4e00SChris Leech 	struct fcoe_interface *fcoe;
18132e70e241SChris Leech 	struct net_device *netdev;
18148eca355fSMike Christie 	int rc = 0;
1815a703e490SVasu Dev 
1816dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
1817dfc1d0feSChris Leech #ifdef CONFIG_FCOE_MODULE
1818dfc1d0feSChris Leech 	/*
1819dfc1d0feSChris Leech 	 * Make sure the module has been initialized, and is not about to be
1820dfc1d0feSChris Leech 	 * removed.  Module paramter sysfs files are writable before the
1821dfc1d0feSChris Leech 	 * module_init function is called and after module_exit.
1822dfc1d0feSChris Leech 	 */
1823dfc1d0feSChris Leech 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1824dfc1d0feSChris Leech 		rc = -ENODEV;
1825dfc1d0feSChris Leech 		goto out_nodev;
1826dfc1d0feSChris Leech 	}
1827dfc1d0feSChris Leech #endif
1828dfc1d0feSChris Leech 
1829a703e490SVasu Dev 	netdev = fcoe_if_to_netdev(buffer);
1830a703e490SVasu Dev 	if (!netdev) {
1831a703e490SVasu Dev 		rc = -ENODEV;
1832a703e490SVasu Dev 		goto out_nodev;
1833a703e490SVasu Dev 	}
18342e70e241SChris Leech 
1835090eb6c4SChris Leech 	rtnl_lock();
18362e70e241SChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
18372e70e241SChris Leech 	if (!fcoe) {
1838090eb6c4SChris Leech 		rtnl_unlock();
1839a703e490SVasu Dev 		rc = -ENODEV;
1840a703e490SVasu Dev 		goto out_putdev;
1841a703e490SVasu Dev 	}
18422e70e241SChris Leech 	list_del(&fcoe->list);
18432e70e241SChris Leech 	fcoe_interface_cleanup(fcoe);
18442e70e241SChris Leech 	rtnl_unlock();
18452e70e241SChris Leech 	fcoe_if_destroy(fcoe->ctlr.lp);
1846a703e490SVasu Dev out_putdev:
1847a703e490SVasu Dev 	dev_put(netdev);
1848a703e490SVasu Dev out_nodev:
1849dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
1850a703e490SVasu Dev 	return rc;
1851a703e490SVasu Dev }
1852a703e490SVasu Dev 
18531875f27eSRobert Love /**
18541875f27eSRobert Love  * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
18551875f27eSRobert Love  * @work: Handle to the FCoE port to be destroyed
18561875f27eSRobert Love  */
18572e70e241SChris Leech static void fcoe_destroy_work(struct work_struct *work)
18582e70e241SChris Leech {
18592e70e241SChris Leech 	struct fcoe_port *port;
18602e70e241SChris Leech 
18612e70e241SChris Leech 	port = container_of(work, struct fcoe_port, destroy_work);
18622e70e241SChris Leech 	mutex_lock(&fcoe_config_mutex);
18632e70e241SChris Leech 	fcoe_if_destroy(port->lport);
18642e70e241SChris Leech 	mutex_unlock(&fcoe_config_mutex);
18652e70e241SChris Leech }
18662e70e241SChris Leech 
1867a703e490SVasu Dev /**
18681875f27eSRobert Love  * fcoe_create() - Create a fcoe interface
18691875f27eSRobert Love  * @buffer: The name of the Ethernet interface to create on
18701875f27eSRobert Love  * @kp:	    The associated kernel param
18711875f27eSRobert Love  *
18721875f27eSRobert Love  * Called from sysfs.
1873a703e490SVasu Dev  *
1874a703e490SVasu Dev  * Returns: 0 for success
1875a703e490SVasu Dev  */
1876a703e490SVasu Dev static int fcoe_create(const char *buffer, struct kernel_param *kp)
1877a703e490SVasu Dev {
1878a703e490SVasu Dev 	int rc;
1879030f4e00SChris Leech 	struct fcoe_interface *fcoe;
1880af7f85d9SChris Leech 	struct fc_lport *lport;
1881a703e490SVasu Dev 	struct net_device *netdev;
1882a703e490SVasu Dev 
1883dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
1884dfc1d0feSChris Leech #ifdef CONFIG_FCOE_MODULE
1885dfc1d0feSChris Leech 	/*
1886dfc1d0feSChris Leech 	 * Make sure the module has been initialized, and is not about to be
1887dfc1d0feSChris Leech 	 * removed.  Module paramter sysfs files are writable before the
1888dfc1d0feSChris Leech 	 * module_init function is called and after module_exit.
1889dfc1d0feSChris Leech 	 */
1890dfc1d0feSChris Leech 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1891dfc1d0feSChris Leech 		rc = -ENODEV;
1892dfc1d0feSChris Leech 		goto out_nodev;
1893dfc1d0feSChris Leech 	}
1894dfc1d0feSChris Leech #endif
1895dfc1d0feSChris Leech 
18962e70e241SChris Leech 	rtnl_lock();
1897a703e490SVasu Dev 	netdev = fcoe_if_to_netdev(buffer);
1898a703e490SVasu Dev 	if (!netdev) {
1899a703e490SVasu Dev 		rc = -ENODEV;
1900a703e490SVasu Dev 		goto out_nodev;
1901a703e490SVasu Dev 	}
19022e70e241SChris Leech 
1903a703e490SVasu Dev 	/* look for existing lport */
1904a703e490SVasu Dev 	if (fcoe_hostlist_lookup(netdev)) {
1905a703e490SVasu Dev 		rc = -EEXIST;
1906a703e490SVasu Dev 		goto out_putdev;
1907a703e490SVasu Dev 	}
1908a703e490SVasu Dev 
1909030f4e00SChris Leech 	fcoe = fcoe_interface_create(netdev);
1910030f4e00SChris Leech 	if (!fcoe) {
1911030f4e00SChris Leech 		rc = -ENOMEM;
1912030f4e00SChris Leech 		goto out_putdev;
1913030f4e00SChris Leech 	}
1914030f4e00SChris Leech 
19159a05753bSChris Leech 	lport = fcoe_if_create(fcoe, &netdev->dev, 0);
1916af7f85d9SChris Leech 	if (IS_ERR(lport)) {
1917d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1918a703e490SVasu Dev 		       netdev->name);
1919a703e490SVasu Dev 		rc = -EIO;
19202e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
1921030f4e00SChris Leech 		goto out_free;
1922a703e490SVasu Dev 	}
1923030f4e00SChris Leech 
192454b649f8SChris Leech 	/* Make this the "master" N_Port */
192554b649f8SChris Leech 	fcoe->ctlr.lp = lport;
1926030f4e00SChris Leech 
1927c863df33SChris Leech 	/* add to lports list */
1928c863df33SChris Leech 	fcoe_hostlist_add(lport);
1929c863df33SChris Leech 
193054b649f8SChris Leech 	/* start FIP Discovery and FLOGI */
193154b649f8SChris Leech 	lport->boot_time = jiffies;
193254b649f8SChris Leech 	fc_fabric_login(lport);
193354b649f8SChris Leech 	if (!fcoe_link_ok(lport))
193454b649f8SChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
193554b649f8SChris Leech 
193654b649f8SChris Leech 	rc = 0;
1937030f4e00SChris Leech out_free:
193854b649f8SChris Leech 	/*
193954b649f8SChris Leech 	 * Release from init in fcoe_interface_create(), on success lport
194054b649f8SChris Leech 	 * should be holding a reference taken in fcoe_if_create().
194154b649f8SChris Leech 	 */
1942030f4e00SChris Leech 	fcoe_interface_put(fcoe);
1943a703e490SVasu Dev out_putdev:
1944a703e490SVasu Dev 	dev_put(netdev);
1945a703e490SVasu Dev out_nodev:
19462e70e241SChris Leech 	rtnl_unlock();
1947dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
1948a703e490SVasu Dev 	return rc;
1949a703e490SVasu Dev }
1950a703e490SVasu Dev 
1951a703e490SVasu Dev /**
19521875f27eSRobert Love  * fcoe_link_ok() - Check if the link is OK for a local port
19531875f27eSRobert Love  * @lport: The local port to check link on
1954a703e490SVasu Dev  *
1955a703e490SVasu Dev  * Any permanently-disqualifying conditions have been previously checked.
1956a703e490SVasu Dev  * This also updates the speed setting, which may change with link for 100/1000.
1957a703e490SVasu Dev  *
1958a703e490SVasu Dev  * This function should probably be checking for PAUSE support at some point
1959a703e490SVasu Dev  * in the future. Currently Per-priority-pause is not determinable using
1960a703e490SVasu Dev  * ethtool, so we shouldn't be restrictive until that problem is resolved.
1961a703e490SVasu Dev  *
1962a703e490SVasu Dev  * Returns: 0 if link is OK for use by FCoE.
1963a703e490SVasu Dev  *
1964a703e490SVasu Dev  */
19651875f27eSRobert Love int fcoe_link_ok(struct fc_lport *lport)
1966a703e490SVasu Dev {
19671875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
19681875f27eSRobert Love 	struct net_device *netdev = port->fcoe->netdev;
1969a703e490SVasu Dev 	struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1970a703e490SVasu Dev 
19711875f27eSRobert Love 	if ((netdev->flags & IFF_UP) && netif_carrier_ok(netdev) &&
19721875f27eSRobert Love 	    (!dev_ethtool_get_settings(netdev, &ecmd))) {
19731875f27eSRobert Love 		lport->link_supported_speeds &=
1974a703e490SVasu Dev 			~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1975a703e490SVasu Dev 		if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1976a703e490SVasu Dev 				      SUPPORTED_1000baseT_Full))
19771875f27eSRobert Love 			lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1978a703e490SVasu Dev 		if (ecmd.supported & SUPPORTED_10000baseT_Full)
19791875f27eSRobert Love 			lport->link_supported_speeds |=
1980a703e490SVasu Dev 				FC_PORTSPEED_10GBIT;
1981a703e490SVasu Dev 		if (ecmd.speed == SPEED_1000)
19821875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_1GBIT;
1983a703e490SVasu Dev 		if (ecmd.speed == SPEED_10000)
19841875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_10GBIT;
1985a703e490SVasu Dev 
19862f718d64SYi Zou 		return 0;
19872f718d64SYi Zou 	}
19882f718d64SYi Zou 	return -1;
1989a703e490SVasu Dev }
1990a703e490SVasu Dev 
1991a703e490SVasu Dev /**
19921875f27eSRobert Love  * fcoe_percpu_clean() - Clear all pending skbs for an local port
19931875f27eSRobert Love  * @lport: The local port whose skbs are to be cleared
1994e7a51997SJoe Eykholt  *
1995e7a51997SJoe Eykholt  * Must be called with fcoe_create_mutex held to single-thread completion.
1996e7a51997SJoe Eykholt  *
1997e7a51997SJoe Eykholt  * This flushes the pending skbs by adding a new skb to each queue and
1998e7a51997SJoe Eykholt  * waiting until they are all freed.  This assures us that not only are
1999e7a51997SJoe Eykholt  * there no packets that will be handled by the lport, but also that any
2000e7a51997SJoe Eykholt  * threads already handling packet have returned.
2001a703e490SVasu Dev  */
20021875f27eSRobert Love void fcoe_percpu_clean(struct fc_lport *lport)
2003a703e490SVasu Dev {
2004a703e490SVasu Dev 	struct fcoe_percpu_s *pp;
2005a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
2006a703e490SVasu Dev 	struct sk_buff_head *list;
2007a703e490SVasu Dev 	struct sk_buff *skb, *next;
2008a703e490SVasu Dev 	struct sk_buff *head;
2009a703e490SVasu Dev 	unsigned int cpu;
2010a703e490SVasu Dev 
2011a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2012a703e490SVasu Dev 		pp = &per_cpu(fcoe_percpu, cpu);
2013a703e490SVasu Dev 		spin_lock_bh(&pp->fcoe_rx_list.lock);
2014a703e490SVasu Dev 		list = &pp->fcoe_rx_list;
2015a703e490SVasu Dev 		head = list->next;
2016a703e490SVasu Dev 		for (skb = head; skb != (struct sk_buff *)list;
2017a703e490SVasu Dev 		     skb = next) {
2018a703e490SVasu Dev 			next = skb->next;
2019a703e490SVasu Dev 			fr = fcoe_dev_from_skb(skb);
20201875f27eSRobert Love 			if (fr->fr_dev == lport) {
2021a703e490SVasu Dev 				__skb_unlink(skb, list);
2022a703e490SVasu Dev 				kfree_skb(skb);
2023a703e490SVasu Dev 			}
2024a703e490SVasu Dev 		}
2025e7a51997SJoe Eykholt 
2026e7a51997SJoe Eykholt 		if (!pp->thread || !cpu_online(cpu)) {
2027a703e490SVasu Dev 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2028e7a51997SJoe Eykholt 			continue;
2029e7a51997SJoe Eykholt 		}
2030e7a51997SJoe Eykholt 
2031e7a51997SJoe Eykholt 		skb = dev_alloc_skb(0);
2032e7a51997SJoe Eykholt 		if (!skb) {
2033e7a51997SJoe Eykholt 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2034e7a51997SJoe Eykholt 			continue;
2035e7a51997SJoe Eykholt 		}
2036e7a51997SJoe Eykholt 		skb->destructor = fcoe_percpu_flush_done;
2037e7a51997SJoe Eykholt 
2038e7a51997SJoe Eykholt 		__skb_queue_tail(&pp->fcoe_rx_list, skb);
2039e7a51997SJoe Eykholt 		if (pp->fcoe_rx_list.qlen == 1)
2040e7a51997SJoe Eykholt 			wake_up_process(pp->thread);
2041e7a51997SJoe Eykholt 		spin_unlock_bh(&pp->fcoe_rx_list.lock);
2042e7a51997SJoe Eykholt 
2043e7a51997SJoe Eykholt 		wait_for_completion(&fcoe_flush_completion);
2044a703e490SVasu Dev 	}
2045a703e490SVasu Dev }
2046a703e490SVasu Dev 
2047a703e490SVasu Dev /**
2048a703e490SVasu Dev  * fcoe_clean_pending_queue() - Dequeue a skb and free it
20491875f27eSRobert Love  * @lport: The local port to dequeue a skb on
2050a703e490SVasu Dev  */
20511875f27eSRobert Love void fcoe_clean_pending_queue(struct fc_lport *lport)
2052a703e490SVasu Dev {
20531875f27eSRobert Love 	struct fcoe_port  *port = lport_priv(lport);
2054a703e490SVasu Dev 	struct sk_buff *skb;
2055a703e490SVasu Dev 
2056014f5c3fSChris Leech 	spin_lock_bh(&port->fcoe_pending_queue.lock);
2057014f5c3fSChris Leech 	while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2058014f5c3fSChris Leech 		spin_unlock_bh(&port->fcoe_pending_queue.lock);
2059a703e490SVasu Dev 		kfree_skb(skb);
2060014f5c3fSChris Leech 		spin_lock_bh(&port->fcoe_pending_queue.lock);
2061a703e490SVasu Dev 	}
2062014f5c3fSChris Leech 	spin_unlock_bh(&port->fcoe_pending_queue.lock);
2063a703e490SVasu Dev }
2064a703e490SVasu Dev 
2065a703e490SVasu Dev /**
20661875f27eSRobert Love  * fcoe_reset() - Reset a local port
20671875f27eSRobert Love  * @shost: The SCSI host associated with the local port to be reset
2068a703e490SVasu Dev  *
20691875f27eSRobert Love  * Returns: Always 0 (return value required by FC transport template)
2070a703e490SVasu Dev  */
2071a703e490SVasu Dev int fcoe_reset(struct Scsi_Host *shost)
2072a703e490SVasu Dev {
2073a703e490SVasu Dev 	struct fc_lport *lport = shost_priv(shost);
2074a703e490SVasu Dev 	fc_lport_reset(lport);
2075a703e490SVasu Dev 	return 0;
2076a703e490SVasu Dev }
2077a703e490SVasu Dev 
2078a703e490SVasu Dev /**
20791875f27eSRobert Love  * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
20801875f27eSRobert Love  * @netdev: The net device used as a key
2081a703e490SVasu Dev  *
20821875f27eSRobert Love  * Locking: Must be called with the RNL mutex held.
20831875f27eSRobert Love  *
20841875f27eSRobert Love  * Returns: NULL or the FCoE interface
2085a703e490SVasu Dev  */
2086014f5c3fSChris Leech static struct fcoe_interface *
20871875f27eSRobert Love fcoe_hostlist_lookup_port(const struct net_device *netdev)
2088a703e490SVasu Dev {
2089014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2090a703e490SVasu Dev 
2091014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
20921875f27eSRobert Love 		if (fcoe->netdev == netdev)
2093014f5c3fSChris Leech 			return fcoe;
2094a703e490SVasu Dev 	}
2095a703e490SVasu Dev 	return NULL;
2096a703e490SVasu Dev }
2097a703e490SVasu Dev 
2098a703e490SVasu Dev /**
20991875f27eSRobert Love  * fcoe_hostlist_lookup() - Find the local port associated with a
21001875f27eSRobert Love  *			    given net device
21011875f27eSRobert Love  * @netdev: The netdevice used as a key
2102a703e490SVasu Dev  *
21031875f27eSRobert Love  * Locking: Must be called with the RTNL mutex held
21041875f27eSRobert Love  *
21051875f27eSRobert Love  * Returns: NULL or the local port
2106a703e490SVasu Dev  */
2107090eb6c4SChris Leech static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2108a703e490SVasu Dev {
2109014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2110a703e490SVasu Dev 
2111014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
21123fe9a0baSChris Leech 	return (fcoe) ? fcoe->ctlr.lp : NULL;
2113a703e490SVasu Dev }
2114a703e490SVasu Dev 
2115a703e490SVasu Dev /**
21161875f27eSRobert Love  * fcoe_hostlist_add() - Add the FCoE interface identified by a local
21171875f27eSRobert Love  *			 port to the hostlist
21181875f27eSRobert Love  * @lport: The local port that identifies the FCoE interface to be added
21191875f27eSRobert Love  *
21201875f27eSRobert Love  * Locking: must be called with the RTNL mutex held
2121a703e490SVasu Dev  *
2122a703e490SVasu Dev  * Returns: 0 for success
2123a703e490SVasu Dev  */
2124090eb6c4SChris Leech static int fcoe_hostlist_add(const struct fc_lport *lport)
2125a703e490SVasu Dev {
2126014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2127014f5c3fSChris Leech 	struct fcoe_port *port;
2128a703e490SVasu Dev 
2129014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2130014f5c3fSChris Leech 	if (!fcoe) {
2131014f5c3fSChris Leech 		port = lport_priv(lport);
2132014f5c3fSChris Leech 		fcoe = port->fcoe;
2133014f5c3fSChris Leech 		list_add_tail(&fcoe->list, &fcoe_hostlist);
2134a703e490SVasu Dev 	}
2135a703e490SVasu Dev 	return 0;
2136a703e490SVasu Dev }
2137a703e490SVasu Dev 
2138a703e490SVasu Dev /**
21391875f27eSRobert Love  * fcoe_init() - Initialize fcoe.ko
2140a703e490SVasu Dev  *
21411875f27eSRobert Love  * Returns: 0 on success, or a negative value on failure
2142a703e490SVasu Dev  */
2143a703e490SVasu Dev static int __init fcoe_init(void)
2144a703e490SVasu Dev {
21451875f27eSRobert Love 	struct fcoe_percpu_s *p;
2146a703e490SVasu Dev 	unsigned int cpu;
2147a703e490SVasu Dev 	int rc = 0;
2148a703e490SVasu Dev 
2149dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2150dfc1d0feSChris Leech 
2151a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2152a703e490SVasu Dev 		p = &per_cpu(fcoe_percpu, cpu);
2153a703e490SVasu Dev 		skb_queue_head_init(&p->fcoe_rx_list);
2154a703e490SVasu Dev 	}
2155a703e490SVasu Dev 
2156a703e490SVasu Dev 	for_each_online_cpu(cpu)
2157a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
2158a703e490SVasu Dev 
2159a703e490SVasu Dev 	/* Initialize per CPU interrupt thread */
2160a703e490SVasu Dev 	rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2161a703e490SVasu Dev 	if (rc)
2162a703e490SVasu Dev 		goto out_free;
2163a703e490SVasu Dev 
2164a703e490SVasu Dev 	/* Setup link change notification */
2165a703e490SVasu Dev 	fcoe_dev_setup();
2166a703e490SVasu Dev 
21675892c32fSChris Leech 	rc = fcoe_if_init();
21685892c32fSChris Leech 	if (rc)
21695892c32fSChris Leech 		goto out_free;
2170a703e490SVasu Dev 
2171dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2172a703e490SVasu Dev 	return 0;
2173a703e490SVasu Dev 
2174a703e490SVasu Dev out_free:
2175a703e490SVasu Dev 	for_each_online_cpu(cpu) {
2176a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2177a703e490SVasu Dev 	}
2178dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2179a703e490SVasu Dev 	return rc;
2180a703e490SVasu Dev }
2181a703e490SVasu Dev module_init(fcoe_init);
2182a703e490SVasu Dev 
2183a703e490SVasu Dev /**
21841875f27eSRobert Love  * fcoe_exit() - Clean up fcoe.ko
2185a703e490SVasu Dev  *
21861875f27eSRobert Love  * Returns: 0 on success or a  negative value on failure
2187a703e490SVasu Dev  */
2188a703e490SVasu Dev static void __exit fcoe_exit(void)
2189a703e490SVasu Dev {
2190014f5c3fSChris Leech 	struct fcoe_interface *fcoe, *tmp;
21912e70e241SChris Leech 	struct fcoe_port *port;
21921875f27eSRobert Love 	unsigned int cpu;
2193a703e490SVasu Dev 
2194dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2195dfc1d0feSChris Leech 
2196a703e490SVasu Dev 	fcoe_dev_cleanup();
2197a703e490SVasu Dev 
2198a703e490SVasu Dev 	/* releases the associated fcoe hosts */
2199090eb6c4SChris Leech 	rtnl_lock();
2200090eb6c4SChris Leech 	list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2201c863df33SChris Leech 		list_del(&fcoe->list);
22022e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
22032e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
22042e70e241SChris Leech 		schedule_work(&port->destroy_work);
2205c863df33SChris Leech 	}
2206090eb6c4SChris Leech 	rtnl_unlock();
2207a703e490SVasu Dev 
2208a703e490SVasu Dev 	unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2209a703e490SVasu Dev 
2210014f5c3fSChris Leech 	for_each_online_cpu(cpu)
2211a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2212a703e490SVasu Dev 
2213dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
22142e70e241SChris Leech 
22152e70e241SChris Leech 	/* flush any asyncronous interface destroys,
22162e70e241SChris Leech 	 * this should happen after the netdev notifier is unregistered */
22172e70e241SChris Leech 	flush_scheduled_work();
22189a05753bSChris Leech 	/* That will flush out all the N_Ports on the hostlist, but now we
22199a05753bSChris Leech 	 * may have NPIV VN_Ports scheduled for destruction */
22209a05753bSChris Leech 	flush_scheduled_work();
22212e70e241SChris Leech 
22222e70e241SChris Leech 	/* detach from scsi transport
22232e70e241SChris Leech 	 * must happen after all destroys are done, therefor after the flush */
22242e70e241SChris Leech 	fcoe_if_exit();
2225a703e490SVasu Dev }
2226a703e490SVasu Dev module_exit(fcoe_exit);
222711b56188SChris Leech 
222811b56188SChris Leech /**
222911b56188SChris Leech  * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
223011b56188SChris Leech  * @seq: active sequence in the FLOGI or FDISC exchange
223111b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
223211b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
223311b56188SChris Leech  *
223411b56188SChris Leech  * This handles MAC address managment for FCoE, then passes control on to
223511b56188SChris Leech  * the libfc FLOGI response handler.
223611b56188SChris Leech  */
223711b56188SChris Leech static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
223811b56188SChris Leech {
223911b56188SChris Leech 	struct fcoe_ctlr *fip = arg;
224011b56188SChris Leech 	struct fc_exch *exch = fc_seq_exch(seq);
224111b56188SChris Leech 	struct fc_lport *lport = exch->lp;
224211b56188SChris Leech 	u8 *mac;
224311b56188SChris Leech 
224411b56188SChris Leech 	if (IS_ERR(fp))
224511b56188SChris Leech 		goto done;
224611b56188SChris Leech 
224711b56188SChris Leech 	mac = fr_cb(fp)->granted_mac;
224811b56188SChris Leech 	if (is_zero_ether_addr(mac)) {
224911b56188SChris Leech 		/* pre-FIP */
2250386309ceSJoe Eykholt 		if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
225111b56188SChris Leech 			fc_frame_free(fp);
225211b56188SChris Leech 			return;
225311b56188SChris Leech 		}
225411b56188SChris Leech 	}
2255386309ceSJoe Eykholt 	fcoe_update_src_mac(lport, mac);
225611b56188SChris Leech done:
225711b56188SChris Leech 	fc_lport_flogi_resp(seq, fp, lport);
225811b56188SChris Leech }
225911b56188SChris Leech 
226011b56188SChris Leech /**
226111b56188SChris Leech  * fcoe_logo_resp() - FCoE specific LOGO response handler
226211b56188SChris Leech  * @seq: active sequence in the LOGO exchange
226311b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
226411b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
226511b56188SChris Leech  *
226611b56188SChris Leech  * This handles MAC address managment for FCoE, then passes control on to
226711b56188SChris Leech  * the libfc LOGO response handler.
226811b56188SChris Leech  */
226911b56188SChris Leech static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
227011b56188SChris Leech {
2271386309ceSJoe Eykholt 	struct fc_lport *lport = arg;
227211b56188SChris Leech 	static u8 zero_mac[ETH_ALEN] = { 0 };
227311b56188SChris Leech 
227411b56188SChris Leech 	if (!IS_ERR(fp))
2275386309ceSJoe Eykholt 		fcoe_update_src_mac(lport, zero_mac);
227611b56188SChris Leech 	fc_lport_logo_resp(seq, fp, lport);
227711b56188SChris Leech }
227811b56188SChris Leech 
227911b56188SChris Leech /**
228011b56188SChris Leech  * fcoe_elsct_send - FCoE specific ELS handler
228111b56188SChris Leech  *
228211b56188SChris Leech  * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
228311b56188SChris Leech  * using FCoE specific response handlers and passing the FIP controller as
228411b56188SChris Leech  * the argument (the lport is still available from the exchange).
228511b56188SChris Leech  *
228611b56188SChris Leech  * Most of the work here is just handed off to the libfc routine.
228711b56188SChris Leech  */
22881875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
22891875f27eSRobert Love 				      struct fc_frame *fp, unsigned int op,
22901875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
22911875f27eSRobert Love 						   struct fc_frame *,
22921875f27eSRobert Love 						   void *),
229311b56188SChris Leech 				      void *arg, u32 timeout)
229411b56188SChris Leech {
229511b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
229611b56188SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
229711b56188SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
229811b56188SChris Leech 	struct fc_frame_header *fh = fc_frame_header_get(fp);
229911b56188SChris Leech 
230011b56188SChris Leech 	switch (op) {
230111b56188SChris Leech 	case ELS_FLOGI:
230211b56188SChris Leech 	case ELS_FDISC:
230311b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
230411b56188SChris Leech 				     fip, timeout);
230511b56188SChris Leech 	case ELS_LOGO:
230611b56188SChris Leech 		/* only hook onto fabric logouts, not port logouts */
230711b56188SChris Leech 		if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
230811b56188SChris Leech 			break;
230911b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2310386309ceSJoe Eykholt 				     lport, timeout);
231111b56188SChris Leech 	}
231211b56188SChris Leech 	return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
231311b56188SChris Leech }
231411b56188SChris Leech 
23159a05753bSChris Leech /**
23169a05753bSChris Leech  * fcoe_vport_create() - create an fc_host/scsi_host for a vport
23179a05753bSChris Leech  * @vport: fc_vport object to create a new fc_host for
23189a05753bSChris Leech  * @disabled: start the new fc_host in a disabled state by default?
23199a05753bSChris Leech  *
23209a05753bSChris Leech  * Returns: 0 for success
23219a05753bSChris Leech  */
23229a05753bSChris Leech static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
23239a05753bSChris Leech {
23249a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
23259a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
23269a05753bSChris Leech 	struct fcoe_port *port = lport_priv(n_port);
23279a05753bSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
23289a05753bSChris Leech 	struct net_device *netdev = fcoe->netdev;
23299a05753bSChris Leech 	struct fc_lport *vn_port;
23309a05753bSChris Leech 
23319a05753bSChris Leech 	mutex_lock(&fcoe_config_mutex);
23329a05753bSChris Leech 	vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
23339a05753bSChris Leech 	mutex_unlock(&fcoe_config_mutex);
23349a05753bSChris Leech 
23359a05753bSChris Leech 	if (IS_ERR(vn_port)) {
23369a05753bSChris Leech 		printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
23379a05753bSChris Leech 		       netdev->name);
23389a05753bSChris Leech 		return -EIO;
23399a05753bSChris Leech 	}
23409a05753bSChris Leech 
23419a05753bSChris Leech 	if (disabled) {
23429a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
23439a05753bSChris Leech 	} else {
23449a05753bSChris Leech 		vn_port->boot_time = jiffies;
23459a05753bSChris Leech 		fc_fabric_login(vn_port);
23469a05753bSChris Leech 		fc_vport_setlink(vn_port);
23479a05753bSChris Leech 	}
23489a05753bSChris Leech 	return 0;
23499a05753bSChris Leech }
23509a05753bSChris Leech 
23519a05753bSChris Leech /**
23529a05753bSChris Leech  * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
23539a05753bSChris Leech  * @vport: fc_vport object that is being destroyed
23549a05753bSChris Leech  *
23559a05753bSChris Leech  * Returns: 0 for success
23569a05753bSChris Leech  */
23579a05753bSChris Leech static int fcoe_vport_destroy(struct fc_vport *vport)
23589a05753bSChris Leech {
23599a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
23609a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
23619a05753bSChris Leech 	struct fc_lport *vn_port = vport->dd_data;
23629a05753bSChris Leech 	struct fcoe_port *port = lport_priv(vn_port);
23639a05753bSChris Leech 
23649a05753bSChris Leech 	mutex_lock(&n_port->lp_mutex);
23659a05753bSChris Leech 	list_del(&vn_port->list);
23669a05753bSChris Leech 	mutex_unlock(&n_port->lp_mutex);
23679a05753bSChris Leech 	schedule_work(&port->destroy_work);
23689a05753bSChris Leech 	return 0;
23699a05753bSChris Leech }
23709a05753bSChris Leech 
23719a05753bSChris Leech /**
23729a05753bSChris Leech  * fcoe_vport_disable() - change vport state
23739a05753bSChris Leech  * @vport: vport to bring online/offline
23749a05753bSChris Leech  * @disable: should the vport be disabled?
23759a05753bSChris Leech  */
23769a05753bSChris Leech static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
23779a05753bSChris Leech {
23789a05753bSChris Leech 	struct fc_lport *lport = vport->dd_data;
23799a05753bSChris Leech 
23809a05753bSChris Leech 	if (disable) {
23819a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
23829a05753bSChris Leech 		fc_fabric_logoff(lport);
23839a05753bSChris Leech 	} else {
23849a05753bSChris Leech 		lport->boot_time = jiffies;
23859a05753bSChris Leech 		fc_fabric_login(lport);
23869a05753bSChris Leech 		fc_vport_setlink(lport);
23879a05753bSChris Leech 	}
23889a05753bSChris Leech 
23899a05753bSChris Leech 	return 0;
23909a05753bSChris Leech }
23919a05753bSChris Leech 
2392dc8596d3SChris Leech /**
2393dc8596d3SChris Leech  * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2394dc8596d3SChris Leech  * @vport: fc_vport with a new symbolic name string
2395dc8596d3SChris Leech  *
2396dc8596d3SChris Leech  * After generating a new symbolic name string, a new RSPN_ID request is
2397dc8596d3SChris Leech  * sent to the name server.  There is no response handler, so if it fails
2398dc8596d3SChris Leech  * for some reason it will not be retried.
2399dc8596d3SChris Leech  */
2400dc8596d3SChris Leech static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2401dc8596d3SChris Leech {
2402dc8596d3SChris Leech 	struct fc_lport *lport = vport->dd_data;
2403dc8596d3SChris Leech 	struct fc_frame *fp;
2404dc8596d3SChris Leech 	size_t len;
2405dc8596d3SChris Leech 
2406dc8596d3SChris Leech 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2407dc8596d3SChris Leech 		 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2408dc8596d3SChris Leech 		 fcoe_netdev(lport)->name, vport->symbolic_name);
2409dc8596d3SChris Leech 
2410dc8596d3SChris Leech 	if (lport->state != LPORT_ST_READY)
2411dc8596d3SChris Leech 		return;
2412dc8596d3SChris Leech 
2413dc8596d3SChris Leech 	len = strnlen(fc_host_symbolic_name(lport->host), 255);
2414dc8596d3SChris Leech 	fp = fc_frame_alloc(lport,
2415dc8596d3SChris Leech 			    sizeof(struct fc_ct_hdr) +
2416dc8596d3SChris Leech 			    sizeof(struct fc_ns_rspn) + len);
2417dc8596d3SChris Leech 	if (!fp)
2418dc8596d3SChris Leech 		return;
2419dc8596d3SChris Leech 	lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2420dc8596d3SChris Leech 			     NULL, NULL, lport->e_d_tov);
2421dc8596d3SChris Leech }
2422