xref: /openbmc/linux/drivers/scsi/fcoe/fcoe.c (revision 80e736f8)
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>
295a0e3ad6STejun Heo #include <linux/slab.h>
30a703e490SVasu Dev #include <linux/cpu.h>
31a703e490SVasu Dev #include <linux/fs.h>
32a703e490SVasu Dev #include <linux/sysfs.h>
33a703e490SVasu Dev #include <linux/ctype.h>
34a703e490SVasu Dev #include <scsi/scsi_tcq.h>
35a703e490SVasu Dev #include <scsi/scsicam.h>
36a703e490SVasu Dev #include <scsi/scsi_transport.h>
37a703e490SVasu Dev #include <scsi/scsi_transport_fc.h>
38a703e490SVasu Dev #include <net/rtnetlink.h>
39a703e490SVasu Dev 
40a703e490SVasu Dev #include <scsi/fc/fc_encaps.h>
4197c8389dSJoe Eykholt #include <scsi/fc/fc_fip.h>
42a703e490SVasu Dev 
43a703e490SVasu Dev #include <scsi/libfc.h>
44a703e490SVasu Dev #include <scsi/fc_frame.h>
45a703e490SVasu Dev #include <scsi/libfcoe.h>
46a703e490SVasu Dev 
47fdd78027SVasu Dev #include "fcoe.h"
48fdd78027SVasu Dev 
49a703e490SVasu Dev MODULE_AUTHOR("Open-FCoE.org");
50a703e490SVasu Dev MODULE_DESCRIPTION("FCoE");
519b34ecffSVasu Dev MODULE_LICENSE("GPL v2");
52a703e490SVasu Dev 
5305cc7390SYi Zou /* Performance tuning parameters for fcoe */
5405cc7390SYi Zou static unsigned int fcoe_ddp_min;
5505cc7390SYi Zou module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
5605cc7390SYi Zou MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for "	\
5705cc7390SYi Zou 		 "Direct Data Placement (DDP).");
5805cc7390SYi Zou 
59dfc1d0feSChris Leech DEFINE_MUTEX(fcoe_config_mutex);
60dfc1d0feSChris Leech 
61e7a51997SJoe Eykholt /* fcoe_percpu_clean completion.  Waiter protected by fcoe_create_mutex */
62e7a51997SJoe Eykholt static DECLARE_COMPLETION(fcoe_flush_completion);
63e7a51997SJoe Eykholt 
64a703e490SVasu Dev /* fcoe host list */
65090eb6c4SChris Leech /* must only by accessed under the RTNL mutex */
66a703e490SVasu Dev LIST_HEAD(fcoe_hostlist);
67a703e490SVasu Dev DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
68a703e490SVasu Dev 
69dd3fd72eSChris Leech /* Function Prototypes */
701875f27eSRobert Love static int fcoe_reset(struct Scsi_Host *);
71fdd78027SVasu Dev static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
72fdd78027SVasu Dev static int fcoe_rcv(struct sk_buff *, struct net_device *,
73fdd78027SVasu Dev 		    struct packet_type *, struct net_device *);
741875f27eSRobert Love static int fcoe_percpu_receive_thread(void *);
751875f27eSRobert Love static void fcoe_clean_pending_queue(struct fc_lport *);
761875f27eSRobert Love static void fcoe_percpu_clean(struct fc_lport *);
775e4f8fe7SRobert Love static int fcoe_link_speed_update(struct fc_lport *);
781875f27eSRobert Love static int fcoe_link_ok(struct fc_lport *);
79fdd78027SVasu Dev 
80fdd78027SVasu Dev static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
81fdd78027SVasu Dev static int fcoe_hostlist_add(const struct fc_lport *);
82fdd78027SVasu Dev 
834bb6b515SVasu Dev static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
84a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *, ulong, void *);
85a703e490SVasu Dev static void fcoe_dev_setup(void);
86a703e490SVasu Dev static void fcoe_dev_cleanup(void);
871875f27eSRobert Love static struct fcoe_interface
881875f27eSRobert Love *fcoe_hostlist_lookup_port(const struct net_device *);
89a703e490SVasu Dev 
901875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
911875f27eSRobert Love 			 struct packet_type *, struct net_device *);
921875f27eSRobert Love 
931875f27eSRobert Love static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
941875f27eSRobert Love static void fcoe_update_src_mac(struct fc_lport *, u8 *);
951875f27eSRobert Love static u8 *fcoe_get_src_mac(struct fc_lport *);
961875f27eSRobert Love static void fcoe_destroy_work(struct work_struct *);
971875f27eSRobert Love 
981875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
991875f27eSRobert Love 			  unsigned int);
1001875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *, u16);
1011875f27eSRobert Love 
1021875f27eSRobert Love static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
1031875f27eSRobert Love 
1041875f27eSRobert Love static int fcoe_create(const char *, struct kernel_param *);
1051875f27eSRobert Love static int fcoe_destroy(const char *, struct kernel_param *);
10655a66d3cSVasu Dev static int fcoe_enable(const char *, struct kernel_param *);
10755a66d3cSVasu Dev static int fcoe_disable(const char *, struct kernel_param *);
1081875f27eSRobert Love 
1091875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
1101875f27eSRobert Love 				      u32 did, struct fc_frame *,
1111875f27eSRobert Love 				      unsigned int op,
1121875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
1131875f27eSRobert Love 						   struct fc_frame *,
1141875f27eSRobert Love 						   void *),
1151875f27eSRobert Love 				      void *, u32 timeout);
116859b7b64SChris Leech static void fcoe_recv_frame(struct sk_buff *skb);
1171875f27eSRobert Love 
118b84056bfSYi Zou static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
119b84056bfSYi Zou 
1203067817aSVasu Dev module_param_call(create, fcoe_create, NULL, (void *)FIP_MODE_FABRIC, S_IWUSR);
1211875f27eSRobert Love __MODULE_PARM_TYPE(create, "string");
12255a66d3cSVasu Dev MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
1231dd454d9SJoe Eykholt module_param_call(create_vn2vn, fcoe_create, NULL,
1241dd454d9SJoe Eykholt 		  (void *)FIP_MODE_VN2VN, S_IWUSR);
1251dd454d9SJoe Eykholt __MODULE_PARM_TYPE(create_vn2vn, "string");
1261dd454d9SJoe Eykholt MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
1271dd454d9SJoe Eykholt 		 "on an Ethernet interface");
1281875f27eSRobert Love module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1291875f27eSRobert Love __MODULE_PARM_TYPE(destroy, "string");
13055a66d3cSVasu Dev MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
13155a66d3cSVasu Dev module_param_call(enable, fcoe_enable, NULL, NULL, S_IWUSR);
13255a66d3cSVasu Dev __MODULE_PARM_TYPE(enable, "string");
13355a66d3cSVasu Dev MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface.");
13455a66d3cSVasu Dev module_param_call(disable, fcoe_disable, NULL, NULL, S_IWUSR);
13555a66d3cSVasu Dev __MODULE_PARM_TYPE(disable, "string");
13655a66d3cSVasu Dev MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface.");
1371875f27eSRobert Love 
1381875f27eSRobert Love /* notification function for packets from net device */
139a703e490SVasu Dev static struct notifier_block fcoe_notifier = {
140a703e490SVasu Dev 	.notifier_call = fcoe_device_notification,
141a703e490SVasu Dev };
142a703e490SVasu Dev 
1431875f27eSRobert Love /* notification function for CPU hotplug events */
1441875f27eSRobert Love static struct notifier_block fcoe_cpu_notifier = {
1451875f27eSRobert Love 	.notifier_call = fcoe_cpu_callback,
1461875f27eSRobert Love };
1471875f27eSRobert Love 
148e9084bb8SChris Leech static struct scsi_transport_template *fcoe_transport_template;
149e9084bb8SChris Leech static struct scsi_transport_template *fcoe_vport_transport_template;
150a703e490SVasu Dev 
1511875f27eSRobert Love static int fcoe_vport_destroy(struct fc_vport *);
1521875f27eSRobert Love static int fcoe_vport_create(struct fc_vport *, bool disabled);
1531875f27eSRobert Love static int fcoe_vport_disable(struct fc_vport *, bool disable);
1541875f27eSRobert Love static void fcoe_set_vport_symbolic_name(struct fc_vport *);
1557d65b0dfSJoe Eykholt static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
1561875f27eSRobert Love 
1571875f27eSRobert Love static struct libfc_function_template fcoe_libfc_fcn_templ = {
1581875f27eSRobert Love 	.frame_send = fcoe_xmit,
1591875f27eSRobert Love 	.ddp_setup = fcoe_ddp_setup,
1601875f27eSRobert Love 	.ddp_done = fcoe_ddp_done,
1611875f27eSRobert Love 	.elsct_send = fcoe_elsct_send,
162b84056bfSYi Zou 	.get_lesb = fcoe_get_lesb,
1637d65b0dfSJoe Eykholt 	.lport_set_port_id = fcoe_set_port_id,
1641875f27eSRobert Love };
1659a05753bSChris Leech 
166a703e490SVasu Dev struct fc_function_template fcoe_transport_function = {
167a703e490SVasu Dev 	.show_host_node_name = 1,
168a703e490SVasu Dev 	.show_host_port_name = 1,
169a703e490SVasu Dev 	.show_host_supported_classes = 1,
170a703e490SVasu Dev 	.show_host_supported_fc4s = 1,
171a703e490SVasu Dev 	.show_host_active_fc4s = 1,
172a703e490SVasu Dev 	.show_host_maxframe_size = 1,
173a703e490SVasu Dev 
174a703e490SVasu Dev 	.show_host_port_id = 1,
175a703e490SVasu Dev 	.show_host_supported_speeds = 1,
176a703e490SVasu Dev 	.get_host_speed = fc_get_host_speed,
177a703e490SVasu Dev 	.show_host_speed = 1,
178a703e490SVasu Dev 	.show_host_port_type = 1,
179a703e490SVasu Dev 	.get_host_port_state = fc_get_host_port_state,
180a703e490SVasu Dev 	.show_host_port_state = 1,
181a703e490SVasu Dev 	.show_host_symbolic_name = 1,
182a703e490SVasu Dev 
183a703e490SVasu Dev 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
184a703e490SVasu Dev 	.show_rport_maxframe_size = 1,
185a703e490SVasu Dev 	.show_rport_supported_classes = 1,
186a703e490SVasu Dev 
187a703e490SVasu Dev 	.show_host_fabric_name = 1,
188a703e490SVasu Dev 	.show_starget_node_name = 1,
189a703e490SVasu Dev 	.show_starget_port_name = 1,
190a703e490SVasu Dev 	.show_starget_port_id = 1,
191a703e490SVasu Dev 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
192a703e490SVasu Dev 	.show_rport_dev_loss_tmo = 1,
193a703e490SVasu Dev 	.get_fc_host_stats = fc_get_host_stats,
194a703e490SVasu Dev 	.issue_fc_host_lip = fcoe_reset,
195a703e490SVasu Dev 
196a703e490SVasu Dev 	.terminate_rport_io = fc_rport_terminate_io,
1979a05753bSChris Leech 
1989a05753bSChris Leech 	.vport_create = fcoe_vport_create,
1999a05753bSChris Leech 	.vport_delete = fcoe_vport_destroy,
2009a05753bSChris Leech 	.vport_disable = fcoe_vport_disable,
201dc8596d3SChris Leech 	.set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
202a51ab396SSteve Ma 
203a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
204a703e490SVasu Dev };
205a703e490SVasu Dev 
206e9084bb8SChris Leech struct fc_function_template fcoe_vport_transport_function = {
207e9084bb8SChris Leech 	.show_host_node_name = 1,
208e9084bb8SChris Leech 	.show_host_port_name = 1,
209e9084bb8SChris Leech 	.show_host_supported_classes = 1,
210e9084bb8SChris Leech 	.show_host_supported_fc4s = 1,
211e9084bb8SChris Leech 	.show_host_active_fc4s = 1,
212e9084bb8SChris Leech 	.show_host_maxframe_size = 1,
213e9084bb8SChris Leech 
214e9084bb8SChris Leech 	.show_host_port_id = 1,
215e9084bb8SChris Leech 	.show_host_supported_speeds = 1,
216e9084bb8SChris Leech 	.get_host_speed = fc_get_host_speed,
217e9084bb8SChris Leech 	.show_host_speed = 1,
218e9084bb8SChris Leech 	.show_host_port_type = 1,
219e9084bb8SChris Leech 	.get_host_port_state = fc_get_host_port_state,
220e9084bb8SChris Leech 	.show_host_port_state = 1,
221e9084bb8SChris Leech 	.show_host_symbolic_name = 1,
222e9084bb8SChris Leech 
223e9084bb8SChris Leech 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
224e9084bb8SChris Leech 	.show_rport_maxframe_size = 1,
225e9084bb8SChris Leech 	.show_rport_supported_classes = 1,
226e9084bb8SChris Leech 
227e9084bb8SChris Leech 	.show_host_fabric_name = 1,
228e9084bb8SChris Leech 	.show_starget_node_name = 1,
229e9084bb8SChris Leech 	.show_starget_port_name = 1,
230e9084bb8SChris Leech 	.show_starget_port_id = 1,
231e9084bb8SChris Leech 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
232e9084bb8SChris Leech 	.show_rport_dev_loss_tmo = 1,
233e9084bb8SChris Leech 	.get_fc_host_stats = fc_get_host_stats,
234e9084bb8SChris Leech 	.issue_fc_host_lip = fcoe_reset,
235e9084bb8SChris Leech 
236e9084bb8SChris Leech 	.terminate_rport_io = fc_rport_terminate_io,
237a51ab396SSteve Ma 
238a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
239e9084bb8SChris Leech };
240e9084bb8SChris Leech 
241a703e490SVasu Dev static struct scsi_host_template fcoe_shost_template = {
242a703e490SVasu Dev 	.module = THIS_MODULE,
243a703e490SVasu Dev 	.name = "FCoE Driver",
244a703e490SVasu Dev 	.proc_name = FCOE_NAME,
245a703e490SVasu Dev 	.queuecommand = fc_queuecommand,
246a703e490SVasu Dev 	.eh_abort_handler = fc_eh_abort,
247a703e490SVasu Dev 	.eh_device_reset_handler = fc_eh_device_reset,
248a703e490SVasu Dev 	.eh_host_reset_handler = fc_eh_host_reset,
249a703e490SVasu Dev 	.slave_alloc = fc_slave_alloc,
250a703e490SVasu Dev 	.change_queue_depth = fc_change_queue_depth,
251a703e490SVasu Dev 	.change_queue_type = fc_change_queue_type,
252a703e490SVasu Dev 	.this_id = -1,
25314caf44cSVasu Dev 	.cmd_per_lun = 3,
254a703e490SVasu Dev 	.can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
255a703e490SVasu Dev 	.use_clustering = ENABLE_CLUSTERING,
256a703e490SVasu Dev 	.sg_tablesize = SG_ALL,
257a703e490SVasu Dev 	.max_sectors = 0xffff,
258a703e490SVasu Dev };
259a703e490SVasu Dev 
26054b649f8SChris Leech /**
2611875f27eSRobert Love  * fcoe_interface_setup() - Setup a FCoE interface
2621875f27eSRobert Love  * @fcoe:   The new FCoE interface
2631875f27eSRobert Love  * @netdev: The net device that the fcoe interface is on
26454b649f8SChris Leech  *
26554b649f8SChris Leech  * Returns : 0 for success
2662e70e241SChris Leech  * Locking: must be called with the RTNL mutex held
26754b649f8SChris Leech  */
26854b649f8SChris Leech static int fcoe_interface_setup(struct fcoe_interface *fcoe,
26954b649f8SChris Leech 				struct net_device *netdev)
27054b649f8SChris Leech {
27154b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
27254b649f8SChris Leech 	struct netdev_hw_addr *ha;
2735bab87e6SYi Zou 	struct net_device *real_dev;
27454b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
275b7a727f1SYi Zou 	const struct net_device_ops *ops;
27654b649f8SChris Leech 
27754b649f8SChris Leech 	fcoe->netdev = netdev;
27854b649f8SChris Leech 
279b7a727f1SYi Zou 	/* Let LLD initialize for FCoE */
280b7a727f1SYi Zou 	ops = netdev->netdev_ops;
281b7a727f1SYi Zou 	if (ops->ndo_fcoe_enable) {
282b7a727f1SYi Zou 		if (ops->ndo_fcoe_enable(netdev))
283b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
284b7a727f1SYi Zou 					" specific feature for LLD.\n");
285b7a727f1SYi Zou 	}
286b7a727f1SYi Zou 
28754b649f8SChris Leech 	/* Do not support for bonding device */
28854b649f8SChris Leech 	if ((netdev->priv_flags & IFF_MASTER_ALB) ||
28954b649f8SChris Leech 	    (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
29054b649f8SChris Leech 	    (netdev->priv_flags & IFF_MASTER_8023AD)) {
29159d92516Sjohn fastabend 		FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
29254b649f8SChris Leech 		return -EOPNOTSUPP;
29354b649f8SChris Leech 	}
29454b649f8SChris Leech 
29554b649f8SChris Leech 	/* look for SAN MAC address, if multiple SAN MACs exist, only
29654b649f8SChris Leech 	 * use the first one for SPMA */
2975bab87e6SYi Zou 	real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
2985bab87e6SYi Zou 		vlan_dev_real_dev(netdev) : netdev;
29954b649f8SChris Leech 	rcu_read_lock();
3005bab87e6SYi Zou 	for_each_dev_addr(real_dev, ha) {
30154b649f8SChris Leech 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
302bf361707SYi Zou 		    (is_valid_ether_addr(ha->addr))) {
30354b649f8SChris Leech 			memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
30454b649f8SChris Leech 			fip->spma = 1;
30554b649f8SChris Leech 			break;
30654b649f8SChris Leech 		}
30754b649f8SChris Leech 	}
30854b649f8SChris Leech 	rcu_read_unlock();
30954b649f8SChris Leech 
31054b649f8SChris Leech 	/* setup Source Mac Address */
31154b649f8SChris Leech 	if (!fip->spma)
31254b649f8SChris Leech 		memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
31354b649f8SChris Leech 
31454b649f8SChris Leech 	/*
31554b649f8SChris Leech 	 * Add FCoE MAC address as second unicast MAC address
31654b649f8SChris Leech 	 * or enter promiscuous mode if not capable of listening
31754b649f8SChris Leech 	 * for multiple unicast MACs.
31854b649f8SChris Leech 	 */
31954b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
320a748ee24SJiri Pirko 	dev_uc_add(netdev, flogi_maddr);
32154b649f8SChris Leech 	if (fip->spma)
322a748ee24SJiri Pirko 		dev_uc_add(netdev, fip->ctl_src_addr);
323e10f8c66SJoe Eykholt 	if (fip->mode == FIP_MODE_VN2VN) {
324e10f8c66SJoe Eykholt 		dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
325e10f8c66SJoe Eykholt 		dev_mc_add(netdev, FIP_ALL_P2P_MACS);
326e10f8c66SJoe Eykholt 	} else
32722bedad3SJiri Pirko 		dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
32854b649f8SChris Leech 
32954b649f8SChris Leech 	/*
33054b649f8SChris Leech 	 * setup the receive function from ethernet driver
33154b649f8SChris Leech 	 * on the ethertype for the given device
33254b649f8SChris Leech 	 */
33354b649f8SChris Leech 	fcoe->fcoe_packet_type.func = fcoe_rcv;
33454b649f8SChris Leech 	fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
33554b649f8SChris Leech 	fcoe->fcoe_packet_type.dev = netdev;
33654b649f8SChris Leech 	dev_add_pack(&fcoe->fcoe_packet_type);
33754b649f8SChris Leech 
33854b649f8SChris Leech 	fcoe->fip_packet_type.func = fcoe_fip_recv;
33954b649f8SChris Leech 	fcoe->fip_packet_type.type = htons(ETH_P_FIP);
34054b649f8SChris Leech 	fcoe->fip_packet_type.dev = netdev;
34154b649f8SChris Leech 	dev_add_pack(&fcoe->fip_packet_type);
34254b649f8SChris Leech 
34354b649f8SChris Leech 	return 0;
34454b649f8SChris Leech }
34554b649f8SChris Leech 
346a703e490SVasu Dev /**
3471875f27eSRobert Love  * fcoe_interface_create() - Create a FCoE interface on a net device
3481875f27eSRobert Love  * @netdev: The net device to create the FCoE interface on
3491dd454d9SJoe Eykholt  * @fip_mode: The mode to use for FIP
350030f4e00SChris Leech  *
351030f4e00SChris Leech  * Returns: pointer to a struct fcoe_interface or NULL on error
352030f4e00SChris Leech  */
3531dd454d9SJoe Eykholt static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
3541dd454d9SJoe Eykholt 						    enum fip_state fip_mode)
355030f4e00SChris Leech {
356030f4e00SChris Leech 	struct fcoe_interface *fcoe;
35759d92516Sjohn fastabend 	int err;
358030f4e00SChris Leech 
359030f4e00SChris Leech 	fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
360030f4e00SChris Leech 	if (!fcoe) {
361030f4e00SChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
362030f4e00SChris Leech 		return NULL;
363030f4e00SChris Leech 	}
364030f4e00SChris Leech 
3652e70e241SChris Leech 	dev_hold(netdev);
366030f4e00SChris Leech 	kref_init(&fcoe->kref);
36754b649f8SChris Leech 
36854b649f8SChris Leech 	/*
36954b649f8SChris Leech 	 * Initialize FIP.
37054b649f8SChris Leech 	 */
3711dd454d9SJoe Eykholt 	fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
37254b649f8SChris Leech 	fcoe->ctlr.send = fcoe_fip_send;
37354b649f8SChris Leech 	fcoe->ctlr.update_mac = fcoe_update_src_mac;
37411b56188SChris Leech 	fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
37554b649f8SChris Leech 
37659d92516Sjohn fastabend 	err = fcoe_interface_setup(fcoe, netdev);
37759d92516Sjohn fastabend 	if (err) {
37859d92516Sjohn fastabend 		fcoe_ctlr_destroy(&fcoe->ctlr);
37959d92516Sjohn fastabend 		kfree(fcoe);
38059d92516Sjohn fastabend 		dev_put(netdev);
38159d92516Sjohn fastabend 		return NULL;
38259d92516Sjohn fastabend 	}
383030f4e00SChris Leech 
384030f4e00SChris Leech 	return fcoe;
385030f4e00SChris Leech }
386030f4e00SChris Leech 
387030f4e00SChris Leech /**
3881875f27eSRobert Love  * fcoe_interface_cleanup() - Clean up a FCoE interface
3891875f27eSRobert Love  * @fcoe: The FCoE interface to be cleaned up
3902e70e241SChris Leech  *
3912e70e241SChris Leech  * Caller must be holding the RTNL mutex
39254b649f8SChris Leech  */
39354b649f8SChris Leech void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
39454b649f8SChris Leech {
39554b649f8SChris Leech 	struct net_device *netdev = fcoe->netdev;
39654b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
39754b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
398b7a727f1SYi Zou 	const struct net_device_ops *ops;
39954b649f8SChris Leech 
40054b649f8SChris Leech 	/*
40154b649f8SChris Leech 	 * Don't listen for Ethernet packets anymore.
40254b649f8SChris Leech 	 * synchronize_net() ensures that the packet handlers are not running
40354b649f8SChris Leech 	 * on another CPU. dev_remove_pack() would do that, this calls the
40454b649f8SChris Leech 	 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
40554b649f8SChris Leech 	 */
40654b649f8SChris Leech 	__dev_remove_pack(&fcoe->fcoe_packet_type);
40754b649f8SChris Leech 	__dev_remove_pack(&fcoe->fip_packet_type);
40854b649f8SChris Leech 	synchronize_net();
40954b649f8SChris Leech 
41054b649f8SChris Leech 	/* Delete secondary MAC addresses */
41154b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
412a748ee24SJiri Pirko 	dev_uc_del(netdev, flogi_maddr);
41354b649f8SChris Leech 	if (fip->spma)
414a748ee24SJiri Pirko 		dev_uc_del(netdev, fip->ctl_src_addr);
415e10f8c66SJoe Eykholt 	if (fip->mode == FIP_MODE_VN2VN) {
416e10f8c66SJoe Eykholt 		dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
417e10f8c66SJoe Eykholt 		dev_mc_del(netdev, FIP_ALL_P2P_MACS);
418e10f8c66SJoe Eykholt 	} else
41922bedad3SJiri Pirko 		dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
420b7a727f1SYi Zou 
421b7a727f1SYi Zou 	/* Tell the LLD we are done w/ FCoE */
422b7a727f1SYi Zou 	ops = netdev->netdev_ops;
423b7a727f1SYi Zou 	if (ops->ndo_fcoe_disable) {
424b7a727f1SYi Zou 		if (ops->ndo_fcoe_disable(netdev))
425b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
426b7a727f1SYi Zou 					" specific feature for LLD.\n");
427b7a727f1SYi Zou 	}
42854b649f8SChris Leech }
42954b649f8SChris Leech 
43054b649f8SChris Leech /**
431030f4e00SChris Leech  * fcoe_interface_release() - fcoe_port kref release function
4321875f27eSRobert Love  * @kref: Embedded reference count in an fcoe_interface struct
433030f4e00SChris Leech  */
434030f4e00SChris Leech static void fcoe_interface_release(struct kref *kref)
435030f4e00SChris Leech {
436030f4e00SChris Leech 	struct fcoe_interface *fcoe;
4372e70e241SChris Leech 	struct net_device *netdev;
438030f4e00SChris Leech 
439030f4e00SChris Leech 	fcoe = container_of(kref, struct fcoe_interface, kref);
4402e70e241SChris Leech 	netdev = fcoe->netdev;
4412e70e241SChris Leech 	/* tear-down the FCoE controller */
4422e70e241SChris Leech 	fcoe_ctlr_destroy(&fcoe->ctlr);
443030f4e00SChris Leech 	kfree(fcoe);
4442e70e241SChris Leech 	dev_put(netdev);
445030f4e00SChris Leech }
446030f4e00SChris Leech 
447030f4e00SChris Leech /**
4481875f27eSRobert Love  * fcoe_interface_get() - Get a reference to a FCoE interface
4491875f27eSRobert Love  * @fcoe: The FCoE interface to be held
450030f4e00SChris Leech  */
451030f4e00SChris Leech static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
452030f4e00SChris Leech {
453030f4e00SChris Leech 	kref_get(&fcoe->kref);
454030f4e00SChris Leech }
455030f4e00SChris Leech 
456030f4e00SChris Leech /**
4571875f27eSRobert Love  * fcoe_interface_put() - Put a reference to a FCoE interface
4581875f27eSRobert Love  * @fcoe: The FCoE interface to be released
459030f4e00SChris Leech  */
460030f4e00SChris Leech static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
461030f4e00SChris Leech {
462030f4e00SChris Leech 	kref_put(&fcoe->kref, fcoe_interface_release);
463030f4e00SChris Leech }
464030f4e00SChris Leech 
465030f4e00SChris Leech /**
4661875f27eSRobert Love  * fcoe_fip_recv() - Handler for received FIP frames
4671875f27eSRobert Love  * @skb:      The receive skb
4681875f27eSRobert Love  * @netdev:   The associated net device
4691875f27eSRobert Love  * @ptype:    The packet_type structure which was used to register this handler
4701875f27eSRobert Love  * @orig_dev: The original net_device the the skb was received on.
4711875f27eSRobert Love  *	      (in case dev is a bond)
472ab6b85c1SVasu Dev  *
473ab6b85c1SVasu Dev  * Returns: 0 for success
474ab6b85c1SVasu Dev  */
4751875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
476ab6b85c1SVasu Dev 			 struct packet_type *ptype,
477ab6b85c1SVasu Dev 			 struct net_device *orig_dev)
478ab6b85c1SVasu Dev {
479259ad85dSChris Leech 	struct fcoe_interface *fcoe;
480ab6b85c1SVasu Dev 
481259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
4823fe9a0baSChris Leech 	fcoe_ctlr_recv(&fcoe->ctlr, skb);
483ab6b85c1SVasu Dev 	return 0;
484ab6b85c1SVasu Dev }
485ab6b85c1SVasu Dev 
486ab6b85c1SVasu Dev /**
4871875f27eSRobert Love  * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
4881875f27eSRobert Love  * @fip: The FCoE controller
4891875f27eSRobert Love  * @skb: The FIP packet to be sent
490ab6b85c1SVasu Dev  */
491ab6b85c1SVasu Dev static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
492ab6b85c1SVasu Dev {
4933fe9a0baSChris Leech 	skb->dev = fcoe_from_ctlr(fip)->netdev;
494ab6b85c1SVasu Dev 	dev_queue_xmit(skb);
495ab6b85c1SVasu Dev }
496ab6b85c1SVasu Dev 
497ab6b85c1SVasu Dev /**
4981875f27eSRobert Love  * fcoe_update_src_mac() - Update the Ethernet MAC filters
4991875f27eSRobert Love  * @lport: The local port to update the source MAC on
5001875f27eSRobert Love  * @addr:  Unicast MAC address to add
501ab6b85c1SVasu Dev  *
502ab6b85c1SVasu Dev  * Remove any previously-set unicast MAC filter.
503ab6b85c1SVasu Dev  * Add secondary FCoE MAC address filter for our OUI.
504ab6b85c1SVasu Dev  */
50511b56188SChris Leech static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
506ab6b85c1SVasu Dev {
50711b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
50811b56188SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
509ab6b85c1SVasu Dev 
510ab6b85c1SVasu Dev 	rtnl_lock();
51111b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
512a748ee24SJiri Pirko 		dev_uc_del(fcoe->netdev, port->data_src_addr);
51311b56188SChris Leech 	if (!is_zero_ether_addr(addr))
514a748ee24SJiri Pirko 		dev_uc_add(fcoe->netdev, addr);
51511b56188SChris Leech 	memcpy(port->data_src_addr, addr, ETH_ALEN);
516ab6b85c1SVasu Dev 	rtnl_unlock();
517ab6b85c1SVasu Dev }
518ab6b85c1SVasu Dev 
519ab6b85c1SVasu Dev /**
52011b56188SChris Leech  * fcoe_get_src_mac() - return the Ethernet source address for an lport
52111b56188SChris Leech  * @lport: libfc lport
52211b56188SChris Leech  */
52311b56188SChris Leech static u8 *fcoe_get_src_mac(struct fc_lport *lport)
52411b56188SChris Leech {
52511b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
52611b56188SChris Leech 
52711b56188SChris Leech 	return port->data_src_addr;
52811b56188SChris Leech }
52911b56188SChris Leech 
53011b56188SChris Leech /**
5311875f27eSRobert Love  * fcoe_lport_config() - Set up a local port
5321875f27eSRobert Love  * @lport: The local port to be setup
533a703e490SVasu Dev  *
534a703e490SVasu Dev  * Returns: 0 for success
535a703e490SVasu Dev  */
5361875f27eSRobert Love static int fcoe_lport_config(struct fc_lport *lport)
537a703e490SVasu Dev {
5381875f27eSRobert Love 	lport->link_up = 0;
5391875f27eSRobert Love 	lport->qfull = 0;
5401875f27eSRobert Love 	lport->max_retry_count = 3;
5411875f27eSRobert Love 	lport->max_rport_retry_count = 3;
5421875f27eSRobert Love 	lport->e_d_tov = 2 * 1000;	/* FC-FS default */
5431875f27eSRobert Love 	lport->r_a_tov = 2 * 2 * 1000;
5441875f27eSRobert Love 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
545a703e490SVasu Dev 				 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
5461875f27eSRobert Love 	lport->does_npiv = 1;
547a703e490SVasu Dev 
5481875f27eSRobert Love 	fc_lport_init_stats(lport);
549a703e490SVasu Dev 
550a703e490SVasu Dev 	/* lport fc_lport related configuration */
5511875f27eSRobert Love 	fc_lport_config(lport);
552a703e490SVasu Dev 
553a703e490SVasu Dev 	/* offload related configuration */
5541875f27eSRobert Love 	lport->crc_offload = 0;
5551875f27eSRobert Love 	lport->seq_offload = 0;
5561875f27eSRobert Love 	lport->lro_enabled = 0;
5571875f27eSRobert Love 	lport->lro_xid = 0;
5581875f27eSRobert Love 	lport->lso_max = 0;
559a703e490SVasu Dev 
560a703e490SVasu Dev 	return 0;
561a703e490SVasu Dev }
562a703e490SVasu Dev 
563a703e490SVasu Dev /**
5641875f27eSRobert Love  * fcoe_queue_timer() - The fcoe queue timer
5651875f27eSRobert Love  * @lport: The local port
5661047f221SVasu Dev  *
5671047f221SVasu Dev  * Calls fcoe_check_wait_queue on timeout
5681047f221SVasu Dev  */
5691875f27eSRobert Love static void fcoe_queue_timer(ulong lport)
5701047f221SVasu Dev {
5711875f27eSRobert Love 	fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
5721047f221SVasu Dev }
5731047f221SVasu Dev 
5741047f221SVasu Dev /**
575dcece412SYi Zou  * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
576dcece412SYi Zou  * @netdev: the associated net device
577dcece412SYi Zou  * @wwn: the output WWN
578dcece412SYi Zou  * @type: the type of WWN (WWPN or WWNN)
579dcece412SYi Zou  *
580dcece412SYi Zou  * Returns: 0 for success
581dcece412SYi Zou  */
582dcece412SYi Zou static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
583dcece412SYi Zou {
584dcece412SYi Zou 	const struct net_device_ops *ops = netdev->netdev_ops;
585dcece412SYi Zou 
586dcece412SYi Zou 	if (ops->ndo_fcoe_get_wwn)
587dcece412SYi Zou 		return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
588dcece412SYi Zou 	return -EINVAL;
589dcece412SYi Zou }
590dcece412SYi Zou 
591dcece412SYi Zou /**
59254a5b21dSYi Zou  * fcoe_netdev_features_change - Updates the lport's offload flags based
59354a5b21dSYi Zou  * on the LLD netdev's FCoE feature flags
59454a5b21dSYi Zou  */
59554a5b21dSYi Zou static void fcoe_netdev_features_change(struct fc_lport *lport,
59654a5b21dSYi Zou 					struct net_device *netdev)
59754a5b21dSYi Zou {
59854a5b21dSYi Zou 	mutex_lock(&lport->lp_mutex);
59954a5b21dSYi Zou 
60054a5b21dSYi Zou 	if (netdev->features & NETIF_F_SG)
60154a5b21dSYi Zou 		lport->sg_supp = 1;
60254a5b21dSYi Zou 	else
60354a5b21dSYi Zou 		lport->sg_supp = 0;
60454a5b21dSYi Zou 
60554a5b21dSYi Zou 	if (netdev->features & NETIF_F_FCOE_CRC) {
60654a5b21dSYi Zou 		lport->crc_offload = 1;
60754a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
60854a5b21dSYi Zou 	} else {
60954a5b21dSYi Zou 		lport->crc_offload = 0;
61054a5b21dSYi Zou 	}
61154a5b21dSYi Zou 
61254a5b21dSYi Zou 	if (netdev->features & NETIF_F_FSO) {
61354a5b21dSYi Zou 		lport->seq_offload = 1;
61454a5b21dSYi Zou 		lport->lso_max = netdev->gso_max_size;
61554a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
61654a5b21dSYi Zou 				lport->lso_max);
61754a5b21dSYi Zou 	} else {
61854a5b21dSYi Zou 		lport->seq_offload = 0;
61954a5b21dSYi Zou 		lport->lso_max = 0;
62054a5b21dSYi Zou 	}
62154a5b21dSYi Zou 
62254a5b21dSYi Zou 	if (netdev->fcoe_ddp_xid) {
62354a5b21dSYi Zou 		lport->lro_enabled = 1;
62454a5b21dSYi Zou 		lport->lro_xid = netdev->fcoe_ddp_xid;
62554a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
62654a5b21dSYi Zou 				lport->lro_xid);
62754a5b21dSYi Zou 	} else {
62854a5b21dSYi Zou 		lport->lro_enabled = 0;
62954a5b21dSYi Zou 		lport->lro_xid = 0;
63054a5b21dSYi Zou 	}
63154a5b21dSYi Zou 
63254a5b21dSYi Zou 	mutex_unlock(&lport->lp_mutex);
63354a5b21dSYi Zou }
63454a5b21dSYi Zou 
63554a5b21dSYi Zou /**
6361875f27eSRobert Love  * fcoe_netdev_config() - Set up net devive for SW FCoE
6371875f27eSRobert Love  * @lport:  The local port that is associated with the net device
6381875f27eSRobert Love  * @netdev: The associated net device
639a703e490SVasu Dev  *
6401875f27eSRobert Love  * Must be called after fcoe_lport_config() as it will use local port mutex
641a703e490SVasu Dev  *
642a703e490SVasu Dev  * Returns: 0 for success
643a703e490SVasu Dev  */
6441875f27eSRobert Love static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
645a703e490SVasu Dev {
646a703e490SVasu Dev 	u32 mfs;
647a703e490SVasu Dev 	u64 wwnn, wwpn;
64825024989SChris Leech 	struct fcoe_interface *fcoe;
649014f5c3fSChris Leech 	struct fcoe_port *port;
650a703e490SVasu Dev 
651a703e490SVasu Dev 	/* Setup lport private data to point to fcoe softc */
6521875f27eSRobert Love 	port = lport_priv(lport);
65325024989SChris Leech 	fcoe = port->fcoe;
654a703e490SVasu Dev 
655a703e490SVasu Dev 	/*
656a703e490SVasu Dev 	 * Determine max frame size based on underlying device and optional
657a703e490SVasu Dev 	 * user-configured limit.  If the MFS is too low, fcoe_link_ok()
658a703e490SVasu Dev 	 * will return 0, so do this first.
659a703e490SVasu Dev 	 */
6607221d7e5SYi Zou 	mfs = netdev->mtu;
6617221d7e5SYi Zou 	if (netdev->features & NETIF_F_FCOE_MTU) {
6627221d7e5SYi Zou 		mfs = FCOE_MTU;
6637221d7e5SYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
6647221d7e5SYi Zou 	}
6657221d7e5SYi Zou 	mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
6661875f27eSRobert Love 	if (fc_set_mfs(lport, mfs))
667a703e490SVasu Dev 		return -EINVAL;
668a703e490SVasu Dev 
669a703e490SVasu Dev 	/* offload features support */
67054a5b21dSYi Zou 	fcoe_netdev_features_change(lport, netdev);
671a703e490SVasu Dev 
672014f5c3fSChris Leech 	skb_queue_head_init(&port->fcoe_pending_queue);
673014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
6741875f27eSRobert Love 	setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
675a703e490SVasu Dev 
6765e4f8fe7SRobert Love 	fcoe_link_speed_update(lport);
6775e4f8fe7SRobert Love 
6781875f27eSRobert Love 	if (!lport->vport) {
679dcece412SYi Zou 		if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
68075ea89efSYi Zou 			wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
6811875f27eSRobert Love 		fc_set_wwnn(lport, wwnn);
682dcece412SYi Zou 		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
683dcece412SYi Zou 			wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
684cf4aebcaSVasu Dev 						 2, 0);
6851875f27eSRobert Love 		fc_set_wwpn(lport, wwpn);
6869a05753bSChris Leech 	}
687a703e490SVasu Dev 
688a703e490SVasu Dev 	return 0;
689a703e490SVasu Dev }
690a703e490SVasu Dev 
691a703e490SVasu Dev /**
6921875f27eSRobert Love  * fcoe_shost_config() - Set up the SCSI host associated with a local port
6931875f27eSRobert Love  * @lport: The local port
6941875f27eSRobert Love  * @dev:   The device associated with the SCSI host
695a703e490SVasu Dev  *
696a703e490SVasu Dev  * Must be called after fcoe_lport_config() and fcoe_netdev_config()
697a703e490SVasu Dev  *
698a703e490SVasu Dev  * Returns: 0 for success
699a703e490SVasu Dev  */
7008ba00a4bSVasu Dev static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
701a703e490SVasu Dev {
702a703e490SVasu Dev 	int rc = 0;
703a703e490SVasu Dev 
704a703e490SVasu Dev 	/* lport scsi host config */
7051875f27eSRobert Love 	lport->host->max_lun = FCOE_MAX_LUN;
7061875f27eSRobert Love 	lport->host->max_id = FCOE_MAX_FCP_TARGET;
7071875f27eSRobert Love 	lport->host->max_channel = 0;
708da87bfabSVasu Dev 	lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
709da87bfabSVasu Dev 
7101875f27eSRobert Love 	if (lport->vport)
7111875f27eSRobert Love 		lport->host->transportt = fcoe_vport_transport_template;
712e9084bb8SChris Leech 	else
7131875f27eSRobert Love 		lport->host->transportt = fcoe_transport_template;
714a703e490SVasu Dev 
715a703e490SVasu Dev 	/* add the new host to the SCSI-ml */
7161875f27eSRobert Love 	rc = scsi_add_host(lport->host, dev);
717a703e490SVasu Dev 	if (rc) {
7181875f27eSRobert Love 		FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
719d5488eb9SRobert Love 				"error on scsi_add_host\n");
720a703e490SVasu Dev 		return rc;
721a703e490SVasu Dev 	}
7229a05753bSChris Leech 
7231875f27eSRobert Love 	if (!lport->vport)
7244be929beSAlexey Dobriyan 		fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
7259a05753bSChris Leech 
7261875f27eSRobert Love 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
7275baa17c3SChris Leech 		 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
7281875f27eSRobert Love 		 fcoe_netdev(lport)->name);
729a703e490SVasu Dev 
730a703e490SVasu Dev 	return 0;
731a703e490SVasu Dev }
732a703e490SVasu Dev 
7331875f27eSRobert Love /**
7341875f27eSRobert Love  * fcoe_oem_match() - The match routine for the offloaded exchange manager
7351875f27eSRobert Love  * @fp: The I/O frame
736d7179680SVasu Dev  *
7371875f27eSRobert Love  * This routine will be associated with an exchange manager (EM). When
7381875f27eSRobert Love  * the libfc exchange handling code is looking for an EM to use it will
7391875f27eSRobert Love  * call this routine and pass it the frame that it wishes to send. This
7401875f27eSRobert Love  * routine will return True if the associated EM is to be used and False
7411875f27eSRobert Love  * if the echange code should continue looking for an EM.
7421875f27eSRobert Love  *
7431875f27eSRobert Love  * The offload EM that this routine is associated with will handle any
7441875f27eSRobert Love  * packets that are for SCSI read requests.
7451875f27eSRobert Love  *
7461875f27eSRobert Love  * Returns: True for read types I/O, otherwise returns false.
747d7179680SVasu Dev  */
748d7179680SVasu Dev bool fcoe_oem_match(struct fc_frame *fp)
749d7179680SVasu Dev {
75005cc7390SYi Zou 	return fc_fcp_is_read(fr_fsp(fp)) &&
75105cc7390SYi Zou 		(fr_fsp(fp)->data_len > fcoe_ddp_min);
752d7179680SVasu Dev }
753d7179680SVasu Dev 
754a703e490SVasu Dev /**
7551875f27eSRobert Love  * fcoe_em_config() - Allocate and configure an exchange manager
7561875f27eSRobert Love  * @lport: The local port that the new EM will be associated with
757a703e490SVasu Dev  *
758a703e490SVasu Dev  * Returns: 0 on success
759a703e490SVasu Dev  */
7601875f27eSRobert Love static inline int fcoe_em_config(struct fc_lport *lport)
761a703e490SVasu Dev {
7621875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
76325024989SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
76425024989SChris Leech 	struct fcoe_interface *oldfcoe = NULL;
7651d1b88dcSVasu Dev 	struct net_device *old_real_dev, *cur_real_dev;
766d7179680SVasu Dev 	u16 min_xid = FCOE_MIN_XID;
767d7179680SVasu Dev 	u16 max_xid = FCOE_MAX_XID;
768d7179680SVasu Dev 
769d7179680SVasu Dev 	/*
770d7179680SVasu Dev 	 * Check if need to allocate an em instance for
771d7179680SVasu Dev 	 * offload exchange ids to be shared across all VN_PORTs/lport.
772d7179680SVasu Dev 	 */
7731875f27eSRobert Love 	if (!lport->lro_enabled || !lport->lro_xid ||
7741875f27eSRobert Love 	    (lport->lro_xid >= max_xid)) {
7751875f27eSRobert Love 		lport->lro_xid = 0;
776d7179680SVasu Dev 		goto skip_oem;
777d7179680SVasu Dev 	}
778d7179680SVasu Dev 
779d7179680SVasu Dev 	/*
780d7179680SVasu Dev 	 * Reuse existing offload em instance in case
7811d1b88dcSVasu Dev 	 * it is already allocated on real eth device
782d7179680SVasu Dev 	 */
78325024989SChris Leech 	if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
78425024989SChris Leech 		cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
7851d1b88dcSVasu Dev 	else
78625024989SChris Leech 		cur_real_dev = fcoe->netdev;
7871d1b88dcSVasu Dev 
78825024989SChris Leech 	list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
78925024989SChris Leech 		if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
79025024989SChris Leech 			old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
7911d1b88dcSVasu Dev 		else
79225024989SChris Leech 			old_real_dev = oldfcoe->netdev;
7931d1b88dcSVasu Dev 
7941d1b88dcSVasu Dev 		if (cur_real_dev == old_real_dev) {
795991cbb60SChris Leech 			fcoe->oem = oldfcoe->oem;
796d7179680SVasu Dev 			break;
797d7179680SVasu Dev 		}
798d7179680SVasu Dev 	}
799d7179680SVasu Dev 
800991cbb60SChris Leech 	if (fcoe->oem) {
8011875f27eSRobert Love 		if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
802d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to add "
803d7179680SVasu Dev 			       "offload em:%p on interface:%s\n",
804991cbb60SChris Leech 			       fcoe->oem, fcoe->netdev->name);
805a703e490SVasu Dev 			return -ENOMEM;
806d7179680SVasu Dev 		}
807d7179680SVasu Dev 	} else {
8081875f27eSRobert Love 		fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
8091875f27eSRobert Love 					      FCOE_MIN_XID, lport->lro_xid,
810d7179680SVasu Dev 					      fcoe_oem_match);
811991cbb60SChris Leech 		if (!fcoe->oem) {
812d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to allocate "
813d7179680SVasu Dev 			       "em for offload exches on interface:%s\n",
81425024989SChris Leech 			       fcoe->netdev->name);
815d7179680SVasu Dev 			return -ENOMEM;
816d7179680SVasu Dev 		}
817d7179680SVasu Dev 	}
818d7179680SVasu Dev 
819d7179680SVasu Dev 	/*
820d7179680SVasu Dev 	 * Exclude offload EM xid range from next EM xid range.
821d7179680SVasu Dev 	 */
8221875f27eSRobert Love 	min_xid += lport->lro_xid + 1;
823d7179680SVasu Dev 
824d7179680SVasu Dev skip_oem:
8251875f27eSRobert Love 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
826d7179680SVasu Dev 		printk(KERN_ERR "fcoe_em_config: failed to "
82725024989SChris Leech 		       "allocate em on interface %s\n", fcoe->netdev->name);
828d7179680SVasu Dev 		return -ENOMEM;
829d7179680SVasu Dev 	}
830a703e490SVasu Dev 
831a703e490SVasu Dev 	return 0;
832a703e490SVasu Dev }
833a703e490SVasu Dev 
834a703e490SVasu Dev /**
8351875f27eSRobert Love  * fcoe_if_destroy() - Tear down a SW FCoE instance
8361875f27eSRobert Love  * @lport: The local port to be destroyed
83734ce27bcSVasu Dev  *
83834ce27bcSVasu Dev  * Locking: must be called with the RTNL mutex held and RTNL mutex
83934ce27bcSVasu Dev  * needed to be dropped by this function since not dropping RTNL
84034ce27bcSVasu Dev  * would cause circular locking warning on synchronous fip worker
84134ce27bcSVasu Dev  * cancelling thru fcoe_interface_put invoked by this function.
84234ce27bcSVasu Dev  *
843a703e490SVasu Dev  */
844af7f85d9SChris Leech static void fcoe_if_destroy(struct fc_lport *lport)
845a703e490SVasu Dev {
846014f5c3fSChris Leech 	struct fcoe_port *port = lport_priv(lport);
847014f5c3fSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
84825024989SChris Leech 	struct net_device *netdev = fcoe->netdev;
849a703e490SVasu Dev 
850d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
851a703e490SVasu Dev 
852a703e490SVasu Dev 	/* Logout of the fabric */
853af7f85d9SChris Leech 	fc_fabric_logoff(lport);
854a703e490SVasu Dev 
855a703e490SVasu Dev 	/* Cleanup the fc_lport */
856af7f85d9SChris Leech 	fc_lport_destroy(lport);
857a703e490SVasu Dev 
85854b649f8SChris Leech 	/* Stop the transmit retry timer */
85954b649f8SChris Leech 	del_timer_sync(&port->timer);
86054b649f8SChris Leech 
86154b649f8SChris Leech 	/* Free existing transmit skbs */
86254b649f8SChris Leech 	fcoe_clean_pending_queue(lport);
86354b649f8SChris Leech 
86411b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
865a748ee24SJiri Pirko 		dev_uc_del(netdev, port->data_src_addr);
86611b56188SChris Leech 	rtnl_unlock();
86711b56188SChris Leech 
86854b649f8SChris Leech 	/* receives may not be stopped until after this */
86954b649f8SChris Leech 	fcoe_interface_put(fcoe);
87054b649f8SChris Leech 
87154b649f8SChris Leech 	/* Free queued packets for the per-CPU receive threads */
87254b649f8SChris Leech 	fcoe_percpu_clean(lport);
87354b649f8SChris Leech 
874a703e490SVasu Dev 	/* Detach from the scsi-ml */
875af7f85d9SChris Leech 	fc_remove_host(lport->host);
876af7f85d9SChris Leech 	scsi_remove_host(lport->host);
877a703e490SVasu Dev 
87880e736f8SYi Zou 	/* Destroy lport scsi_priv */
87980e736f8SYi Zou 	fc_fcp_destroy(lport);
88080e736f8SYi Zou 
881a703e490SVasu Dev 	/* There are no more rports or I/O, free the EM */
882af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
883a703e490SVasu Dev 
884a703e490SVasu Dev 	/* Free memory used by statistical counters */
885af7f85d9SChris Leech 	fc_lport_free_stats(lport);
886a703e490SVasu Dev 
8872e70e241SChris Leech 	/* Release the Scsi_Host */
888af7f85d9SChris Leech 	scsi_host_put(lport->host);
88915af974dSVasu Dev 	module_put(THIS_MODULE);
890a703e490SVasu Dev }
891a703e490SVasu Dev 
8921875f27eSRobert Love /**
8931875f27eSRobert Love  * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
8941875f27eSRobert Love  * @lport: The local port to setup DDP for
8951875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
8961875f27eSRobert Love  * @sgl:   The scatterlist describing this transfer
8971875f27eSRobert Love  * @sgc:   The number of sg items
898a703e490SVasu Dev  *
8991875f27eSRobert Love  * Returns: 0 if the DDP context was not configured
900a703e490SVasu Dev  */
9011875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
902a703e490SVasu Dev 			  struct scatterlist *sgl, unsigned int sgc)
903a703e490SVasu Dev {
9041875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
905a703e490SVasu Dev 
9061875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
9071875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
9081875f27eSRobert Love 							      xid, sgl,
9091875f27eSRobert Love 							      sgc);
910a703e490SVasu Dev 
911a703e490SVasu Dev 	return 0;
912a703e490SVasu Dev }
913a703e490SVasu Dev 
914a703e490SVasu Dev /**
9151875f27eSRobert Love  * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
9161875f27eSRobert Love  * @lport: The local port to complete DDP on
9171875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
918a703e490SVasu Dev  *
9191875f27eSRobert Love  * Returns: the length of data that have been completed by DDP
9201875f27eSRobert Love  */
9211875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
9221875f27eSRobert Love {
9231875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
9241875f27eSRobert Love 
9251875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_done)
9261875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
9271875f27eSRobert Love 	return 0;
9281875f27eSRobert Love }
9291875f27eSRobert Love 
9301875f27eSRobert Love /**
9311875f27eSRobert Love  * fcoe_if_create() - Create a FCoE instance on an interface
9321875f27eSRobert Love  * @fcoe:   The FCoE interface to create a local port on
9331875f27eSRobert Love  * @parent: The device pointer to be the parent in sysfs for the SCSI host
9341875f27eSRobert Love  * @npiv:   Indicates if the port is a vport or not
9351875f27eSRobert Love  *
9361875f27eSRobert Love  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
937a703e490SVasu Dev  *
938af7f85d9SChris Leech  * Returns: The allocated fc_lport or an error pointer
939a703e490SVasu Dev  */
940030f4e00SChris Leech static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
9419a05753bSChris Leech 				       struct device *parent, int npiv)
942a703e490SVasu Dev {
9431875f27eSRobert Love 	struct net_device *netdev = fcoe->netdev;
944af7f85d9SChris Leech 	struct fc_lport *lport = NULL;
945014f5c3fSChris Leech 	struct fcoe_port *port;
9461875f27eSRobert Love 	int rc;
9479a05753bSChris Leech 	/*
9489a05753bSChris Leech 	 * parent is only a vport if npiv is 1,
9499a05753bSChris Leech 	 * but we'll only use vport in that case so go ahead and set it
9509a05753bSChris Leech 	 */
9519a05753bSChris Leech 	struct fc_vport *vport = dev_to_vport(parent);
952a703e490SVasu Dev 
953d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Create Interface\n");
954a703e490SVasu Dev 
9559a05753bSChris Leech 	if (!npiv) {
95686221969SChris Leech 		lport = libfc_host_alloc(&fcoe_shost_template,
957014f5c3fSChris Leech 					 sizeof(struct fcoe_port));
9589a05753bSChris Leech 	} else	{
9599a05753bSChris Leech 		lport = libfc_vport_create(vport,
9609a05753bSChris Leech 					   sizeof(struct fcoe_port));
9619a05753bSChris Leech 	}
96286221969SChris Leech 	if (!lport) {
963014f5c3fSChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
964014f5c3fSChris Leech 		rc = -ENOMEM;
965030f4e00SChris Leech 		goto out;
966014f5c3fSChris Leech 	}
967014f5c3fSChris Leech 	port = lport_priv(lport);
9682e70e241SChris Leech 	port->lport = lport;
969014f5c3fSChris Leech 	port->fcoe = fcoe;
9702e70e241SChris Leech 	INIT_WORK(&port->destroy_work, fcoe_destroy_work);
971a703e490SVasu Dev 
9721875f27eSRobert Love 	/* configure a fc_lport including the exchange manager */
973af7f85d9SChris Leech 	rc = fcoe_lport_config(lport);
974a703e490SVasu Dev 	if (rc) {
975d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
976d5488eb9SRobert Love 				"interface\n");
977a703e490SVasu Dev 		goto out_host_put;
978a703e490SVasu Dev 	}
979a703e490SVasu Dev 
9809a05753bSChris Leech 	if (npiv) {
9819f8f3aa6SChris Leech 		FCOE_NETDEV_DBG(netdev, "Setting vport names, "
9829f8f3aa6SChris Leech 				"%16.16llx %16.16llx\n",
9839a05753bSChris Leech 				vport->node_name, vport->port_name);
9849a05753bSChris Leech 		fc_set_wwnn(lport, vport->node_name);
9859a05753bSChris Leech 		fc_set_wwpn(lport, vport->port_name);
9869a05753bSChris Leech 	}
9879a05753bSChris Leech 
988ab6b85c1SVasu Dev 	/* configure lport network properties */
989af7f85d9SChris Leech 	rc = fcoe_netdev_config(lport, netdev);
990ab6b85c1SVasu Dev 	if (rc) {
991d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
992d5488eb9SRobert Love 				"interface\n");
99354b649f8SChris Leech 		goto out_lp_destroy;
994ab6b85c1SVasu Dev 	}
99597c8389dSJoe Eykholt 
996a703e490SVasu Dev 	/* configure lport scsi host properties */
9978ba00a4bSVasu Dev 	rc = fcoe_shost_config(lport, parent);
998a703e490SVasu Dev 	if (rc) {
999d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1000d5488eb9SRobert Love 				"interface\n");
100154b649f8SChris Leech 		goto out_lp_destroy;
1002a703e490SVasu Dev 	}
1003a703e490SVasu Dev 
1004a703e490SVasu Dev 	/* Initialize the library */
1005e10f8c66SJoe Eykholt 	rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
1006a703e490SVasu Dev 	if (rc) {
1007d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1008d5488eb9SRobert Love 				"interface\n");
1009a703e490SVasu Dev 		goto out_lp_destroy;
1010a703e490SVasu Dev 	}
1011a703e490SVasu Dev 
10129a05753bSChris Leech 	if (!npiv) {
1013e8af4d43SVasu Dev 		/*
1014e8af4d43SVasu Dev 		 * fcoe_em_alloc() and fcoe_hostlist_add() both
10159a05753bSChris Leech 		 * need to be atomic with respect to other changes to the
10169a05753bSChris Leech 		 * hostlist since fcoe_em_alloc() looks for an existing EM
1017e8af4d43SVasu Dev 		 * instance on host list updated by fcoe_hostlist_add().
1018c863df33SChris Leech 		 *
10199a05753bSChris Leech 		 * This is currently handled through the fcoe_config_mutex
10209a05753bSChris Leech 		 * begin held.
1021e8af4d43SVasu Dev 		 */
1022c863df33SChris Leech 
102396316099SVasu Dev 		/* lport exch manager allocation */
1024af7f85d9SChris Leech 		rc = fcoe_em_config(lport);
102596316099SVasu Dev 		if (rc) {
10269a05753bSChris Leech 			FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
10279a05753bSChris Leech 					"for the interface\n");
102896316099SVasu Dev 			goto out_lp_destroy;
102996316099SVasu Dev 		}
10309a05753bSChris Leech 	}
103196316099SVasu Dev 
1032030f4e00SChris Leech 	fcoe_interface_get(fcoe);
1033af7f85d9SChris Leech 	return lport;
1034a703e490SVasu Dev 
1035a703e490SVasu Dev out_lp_destroy:
1036af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
1037a703e490SVasu Dev out_host_put:
1038af7f85d9SChris Leech 	scsi_host_put(lport->host);
1039af7f85d9SChris Leech out:
1040af7f85d9SChris Leech 	return ERR_PTR(rc);
1041a703e490SVasu Dev }
1042a703e490SVasu Dev 
1043a703e490SVasu Dev /**
10441875f27eSRobert Love  * fcoe_if_init() - Initialization routine for fcoe.ko
10451875f27eSRobert Love  *
10461875f27eSRobert Love  * Attaches the SW FCoE transport to the FC transport
1047a703e490SVasu Dev  *
1048a703e490SVasu Dev  * Returns: 0 on success
1049a703e490SVasu Dev  */
1050a703e490SVasu Dev static int __init fcoe_if_init(void)
1051a703e490SVasu Dev {
1052a703e490SVasu Dev 	/* attach to scsi transport */
1053e9084bb8SChris Leech 	fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
1054e9084bb8SChris Leech 	fcoe_vport_transport_template =
1055e9084bb8SChris Leech 		fc_attach_transport(&fcoe_vport_transport_function);
1056a703e490SVasu Dev 
1057e9084bb8SChris Leech 	if (!fcoe_transport_template) {
1058d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1059a703e490SVasu Dev 		return -ENODEV;
1060a703e490SVasu Dev 	}
1061a703e490SVasu Dev 
1062a703e490SVasu Dev 	return 0;
1063a703e490SVasu Dev }
1064a703e490SVasu Dev 
1065a703e490SVasu Dev /**
10661875f27eSRobert Love  * fcoe_if_exit() - Tear down fcoe.ko
10671875f27eSRobert Love  *
10681875f27eSRobert Love  * Detaches the SW FCoE transport from the FC transport
1069a703e490SVasu Dev  *
1070a703e490SVasu Dev  * Returns: 0 on success
1071a703e490SVasu Dev  */
1072a703e490SVasu Dev int __exit fcoe_if_exit(void)
1073a703e490SVasu Dev {
1074e9084bb8SChris Leech 	fc_release_transport(fcoe_transport_template);
1075e9084bb8SChris Leech 	fc_release_transport(fcoe_vport_transport_template);
1076e9084bb8SChris Leech 	fcoe_transport_template = NULL;
1077e9084bb8SChris Leech 	fcoe_vport_transport_template = NULL;
1078a703e490SVasu Dev 	return 0;
1079a703e490SVasu Dev }
1080a703e490SVasu Dev 
1081a703e490SVasu Dev /**
10821875f27eSRobert Love  * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
10831875f27eSRobert Love  * @cpu: The CPU index of the CPU to create a receive thread for
1084a703e490SVasu Dev  */
1085a703e490SVasu Dev static void fcoe_percpu_thread_create(unsigned int cpu)
1086a703e490SVasu Dev {
1087a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1088a703e490SVasu Dev 	struct task_struct *thread;
1089a703e490SVasu Dev 
1090a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1091a703e490SVasu Dev 
1092a703e490SVasu Dev 	thread = kthread_create(fcoe_percpu_receive_thread,
1093a703e490SVasu Dev 				(void *)p, "fcoethread/%d", cpu);
1094a703e490SVasu Dev 
1095e7a51997SJoe Eykholt 	if (likely(!IS_ERR(thread))) {
1096a703e490SVasu Dev 		kthread_bind(thread, cpu);
1097a703e490SVasu Dev 		wake_up_process(thread);
1098a703e490SVasu Dev 
1099a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1100a703e490SVasu Dev 		p->thread = thread;
1101a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1102a703e490SVasu Dev 	}
1103a703e490SVasu Dev }
1104a703e490SVasu Dev 
1105a703e490SVasu Dev /**
11061875f27eSRobert Love  * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
11071875f27eSRobert Love  * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1108a703e490SVasu Dev  *
1109a703e490SVasu Dev  * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1110a703e490SVasu Dev  * current CPU's Rx thread. If the thread being destroyed is bound to
1111a703e490SVasu Dev  * the CPU processing this context the skbs will be freed.
1112a703e490SVasu Dev  */
1113a703e490SVasu Dev static void fcoe_percpu_thread_destroy(unsigned int cpu)
1114a703e490SVasu Dev {
1115a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1116a703e490SVasu Dev 	struct task_struct *thread;
1117a703e490SVasu Dev 	struct page *crc_eof;
1118a703e490SVasu Dev 	struct sk_buff *skb;
1119a703e490SVasu Dev #ifdef CONFIG_SMP
1120a703e490SVasu Dev 	struct fcoe_percpu_s *p0;
1121f018b73aSJoe Eykholt 	unsigned targ_cpu = get_cpu();
1122a703e490SVasu Dev #endif /* CONFIG_SMP */
1123a703e490SVasu Dev 
1124d5488eb9SRobert Love 	FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1125a703e490SVasu Dev 
1126a703e490SVasu Dev 	/* Prevent any new skbs from being queued for this CPU. */
1127a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1128a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1129a703e490SVasu Dev 	thread = p->thread;
1130a703e490SVasu Dev 	p->thread = NULL;
1131a703e490SVasu Dev 	crc_eof = p->crc_eof_page;
1132a703e490SVasu Dev 	p->crc_eof_page = NULL;
1133a703e490SVasu Dev 	p->crc_eof_offset = 0;
1134a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1135a703e490SVasu Dev 
1136a703e490SVasu Dev #ifdef CONFIG_SMP
1137a703e490SVasu Dev 	/*
1138a703e490SVasu Dev 	 * Don't bother moving the skb's if this context is running
1139a703e490SVasu Dev 	 * on the same CPU that is having its thread destroyed. This
1140a703e490SVasu Dev 	 * can easily happen when the module is removed.
1141a703e490SVasu Dev 	 */
1142a703e490SVasu Dev 	if (cpu != targ_cpu) {
1143a703e490SVasu Dev 		p0 = &per_cpu(fcoe_percpu, targ_cpu);
1144a703e490SVasu Dev 		spin_lock_bh(&p0->fcoe_rx_list.lock);
1145a703e490SVasu Dev 		if (p0->thread) {
1146d5488eb9SRobert Love 			FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1147a703e490SVasu Dev 				 cpu, targ_cpu);
1148a703e490SVasu Dev 
1149a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1150a703e490SVasu Dev 				__skb_queue_tail(&p0->fcoe_rx_list, skb);
1151a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1152a703e490SVasu Dev 		} else {
1153a703e490SVasu Dev 			/*
1154a703e490SVasu Dev 			 * The targeted CPU is not initialized and cannot accept
1155a703e490SVasu Dev 			 * new	skbs. Unlock the targeted CPU and drop the skbs
1156a703e490SVasu Dev 			 * on the CPU that is going offline.
1157a703e490SVasu Dev 			 */
1158a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1159a703e490SVasu Dev 				kfree_skb(skb);
1160a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1161a703e490SVasu Dev 		}
1162a703e490SVasu Dev 	} else {
1163a703e490SVasu Dev 		/*
1164a703e490SVasu Dev 		 * This scenario occurs when the module is being removed
1165a703e490SVasu Dev 		 * and all threads are being destroyed. skbs will continue
1166a703e490SVasu Dev 		 * to be shifted from the CPU thread that is being removed
1167a703e490SVasu Dev 		 * to the CPU thread associated with the CPU that is processing
1168a703e490SVasu Dev 		 * the module removal. Once there is only one CPU Rx thread it
1169a703e490SVasu Dev 		 * will reach this case and we will drop all skbs and later
1170a703e490SVasu Dev 		 * stop the thread.
1171a703e490SVasu Dev 		 */
1172a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1173a703e490SVasu Dev 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1174a703e490SVasu Dev 			kfree_skb(skb);
1175a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1176a703e490SVasu Dev 	}
1177f018b73aSJoe Eykholt 	put_cpu();
1178a703e490SVasu Dev #else
1179a703e490SVasu Dev 	/*
1180dd3fd72eSChris Leech 	 * This a non-SMP scenario where the singular Rx thread is
1181a703e490SVasu Dev 	 * being removed. Free all skbs and stop the thread.
1182a703e490SVasu Dev 	 */
1183a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1184a703e490SVasu Dev 	while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1185a703e490SVasu Dev 		kfree_skb(skb);
1186a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1187a703e490SVasu Dev #endif
1188a703e490SVasu Dev 
1189a703e490SVasu Dev 	if (thread)
1190a703e490SVasu Dev 		kthread_stop(thread);
1191a703e490SVasu Dev 
1192a703e490SVasu Dev 	if (crc_eof)
1193a703e490SVasu Dev 		put_page(crc_eof);
1194a703e490SVasu Dev }
1195a703e490SVasu Dev 
1196a703e490SVasu Dev /**
11971875f27eSRobert Love  * fcoe_cpu_callback() - Handler for CPU hotplug events
11981875f27eSRobert Love  * @nfb:    The callback data block
11991875f27eSRobert Love  * @action: The event triggering the callback
12001875f27eSRobert Love  * @hcpu:   The index of the CPU that the event is for
1201a703e490SVasu Dev  *
12021875f27eSRobert Love  * This creates or destroys per-CPU data for fcoe
1203a703e490SVasu Dev  *
1204a703e490SVasu Dev  * Returns NOTIFY_OK always.
1205a703e490SVasu Dev  */
1206a703e490SVasu Dev static int fcoe_cpu_callback(struct notifier_block *nfb,
1207a703e490SVasu Dev 			     unsigned long action, void *hcpu)
1208a703e490SVasu Dev {
1209a703e490SVasu Dev 	unsigned cpu = (unsigned long)hcpu;
1210a703e490SVasu Dev 
1211a703e490SVasu Dev 	switch (action) {
1212a703e490SVasu Dev 	case CPU_ONLINE:
1213a703e490SVasu Dev 	case CPU_ONLINE_FROZEN:
1214d5488eb9SRobert Love 		FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1215a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
1216a703e490SVasu Dev 		break;
1217a703e490SVasu Dev 	case CPU_DEAD:
1218a703e490SVasu Dev 	case CPU_DEAD_FROZEN:
1219d5488eb9SRobert Love 		FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1220a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
1221a703e490SVasu Dev 		break;
1222a703e490SVasu Dev 	default:
1223a703e490SVasu Dev 		break;
1224a703e490SVasu Dev 	}
1225a703e490SVasu Dev 	return NOTIFY_OK;
1226a703e490SVasu Dev }
1227a703e490SVasu Dev 
1228a703e490SVasu Dev /**
12291875f27eSRobert Love  * fcoe_rcv() - Receive packets from a net device
12301875f27eSRobert Love  * @skb:    The received packet
12311875f27eSRobert Love  * @netdev: The net device that the packet was received on
12321875f27eSRobert Love  * @ptype:  The packet type context
12331875f27eSRobert Love  * @olddev: The last device net device
1234a703e490SVasu Dev  *
12351875f27eSRobert Love  * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
12361875f27eSRobert Love  * FC frame and passes the frame to libfc.
1237a703e490SVasu Dev  *
1238a703e490SVasu Dev  * Returns: 0 for success
1239a703e490SVasu Dev  */
12401875f27eSRobert Love int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1241a703e490SVasu Dev 	     struct packet_type *ptype, struct net_device *olddev)
1242a703e490SVasu Dev {
12431875f27eSRobert Love 	struct fc_lport *lport;
1244a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1245259ad85dSChris Leech 	struct fcoe_interface *fcoe;
1246a703e490SVasu Dev 	struct fc_frame_header *fh;
1247a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1248519e5135SVasu Dev 	struct ethhdr *eh;
1249b2f0091fSVasu Dev 	unsigned int cpu;
1250a703e490SVasu Dev 
1251259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
12521875f27eSRobert Love 	lport = fcoe->ctlr.lp;
12531875f27eSRobert Love 	if (unlikely(!lport)) {
12541875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1255a703e490SVasu Dev 		goto err2;
1256a703e490SVasu Dev 	}
12571875f27eSRobert Love 	if (!lport->link_up)
125897c8389dSJoe Eykholt 		goto err2;
1259a703e490SVasu Dev 
12601875f27eSRobert Love 	FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1261d5488eb9SRobert Love 			"data:%p tail:%p end:%p sum:%d dev:%s",
1262d5488eb9SRobert Love 			skb->len, skb->data_len, skb->head, skb->data,
1263d5488eb9SRobert Love 			skb_tail_pointer(skb), skb_end_pointer(skb),
1264d5488eb9SRobert Love 			skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1265a703e490SVasu Dev 
1266519e5135SVasu Dev 	eh = eth_hdr(skb);
1267519e5135SVasu Dev 
1268519e5135SVasu Dev 	if (is_fip_mode(&fcoe->ctlr) &&
1269519e5135SVasu Dev 	    compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1270519e5135SVasu Dev 		FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1271519e5135SVasu Dev 				eh->h_source);
1272a703e490SVasu Dev 		goto err;
1273a703e490SVasu Dev 	}
1274a703e490SVasu Dev 
1275a703e490SVasu Dev 	/*
1276a703e490SVasu Dev 	 * Check for minimum frame length, and make sure required FCoE
1277a703e490SVasu Dev 	 * and FC headers are pulled into the linear data area.
1278a703e490SVasu Dev 	 */
1279a703e490SVasu Dev 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1280a703e490SVasu Dev 		     !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1281a703e490SVasu Dev 		goto err;
1282a703e490SVasu Dev 
1283a703e490SVasu Dev 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1284a703e490SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
1285a703e490SVasu Dev 
12860ee31cb5SRobert Love 	if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
12870ee31cb5SRobert Love 		FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
12880ee31cb5SRobert Love 				eh->h_dest);
12890ee31cb5SRobert Love 		goto err;
12900ee31cb5SRobert Love 	}
12910ee31cb5SRobert Love 
1292a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
12931875f27eSRobert Love 	fr->fr_dev = lport;
1294a703e490SVasu Dev 	fr->ptype = ptype;
1295a703e490SVasu Dev 
1296a703e490SVasu Dev 	/*
1297b2f0091fSVasu Dev 	 * In case the incoming frame's exchange is originated from
1298b2f0091fSVasu Dev 	 * the initiator, then received frame's exchange id is ANDed
1299b2f0091fSVasu Dev 	 * with fc_cpu_mask bits to get the same cpu on which exchange
1300b2f0091fSVasu Dev 	 * was originated, otherwise just use the current cpu.
1301a703e490SVasu Dev 	 */
1302b2f0091fSVasu Dev 	if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1303b2f0091fSVasu Dev 		cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1304b2f0091fSVasu Dev 	else
1305b2f0091fSVasu Dev 		cpu = smp_processor_id();
1306a703e490SVasu Dev 
1307a703e490SVasu Dev 	fps = &per_cpu(fcoe_percpu, cpu);
1308a703e490SVasu Dev 	spin_lock_bh(&fps->fcoe_rx_list.lock);
1309a703e490SVasu Dev 	if (unlikely(!fps->thread)) {
1310a703e490SVasu Dev 		/*
1311a703e490SVasu Dev 		 * The targeted CPU is not ready, let's target
1312a703e490SVasu Dev 		 * the first CPU now. For non-SMP systems this
1313a703e490SVasu Dev 		 * will check the same CPU twice.
1314a703e490SVasu Dev 		 */
13151875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1316d5488eb9SRobert Love 				"ready for incoming skb- using first online "
1317d5488eb9SRobert Love 				"CPU.\n");
1318a703e490SVasu Dev 
1319a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
13206957177fSRusty Russell 		cpu = cpumask_first(cpu_online_mask);
1321a703e490SVasu Dev 		fps = &per_cpu(fcoe_percpu, cpu);
1322a703e490SVasu Dev 		spin_lock_bh(&fps->fcoe_rx_list.lock);
1323a703e490SVasu Dev 		if (!fps->thread) {
1324a703e490SVasu Dev 			spin_unlock_bh(&fps->fcoe_rx_list.lock);
1325a703e490SVasu Dev 			goto err;
1326a703e490SVasu Dev 		}
1327a703e490SVasu Dev 	}
1328a703e490SVasu Dev 
1329a703e490SVasu Dev 	/*
1330a703e490SVasu Dev 	 * We now have a valid CPU that we're targeting for
1331a703e490SVasu Dev 	 * this skb. We also have this receive thread locked,
1332a703e490SVasu Dev 	 * so we're free to queue skbs into it's queue.
1333a703e490SVasu Dev 	 */
1334859b7b64SChris Leech 
1335859b7b64SChris Leech 	/* If this is a SCSI-FCP frame, and this is already executing on the
1336859b7b64SChris Leech 	 * correct CPU, and the queue for this CPU is empty, then go ahead
1337859b7b64SChris Leech 	 * and process the frame directly in the softirq context.
1338859b7b64SChris Leech 	 * This lets us process completions without context switching from the
1339859b7b64SChris Leech 	 * NET_RX softirq, to our receive processing thread, and then back to
1340859b7b64SChris Leech 	 * BLOCK softirq context.
1341859b7b64SChris Leech 	 */
1342859b7b64SChris Leech 	if (fh->fh_type == FC_TYPE_FCP &&
1343859b7b64SChris Leech 	    cpu == smp_processor_id() &&
1344859b7b64SChris Leech 	    skb_queue_empty(&fps->fcoe_rx_list)) {
1345859b7b64SChris Leech 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1346859b7b64SChris Leech 		fcoe_recv_frame(skb);
1347859b7b64SChris Leech 	} else {
1348a703e490SVasu Dev 		__skb_queue_tail(&fps->fcoe_rx_list, skb);
1349a703e490SVasu Dev 		if (fps->fcoe_rx_list.qlen == 1)
1350a703e490SVasu Dev 			wake_up_process(fps->thread);
1351a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1352859b7b64SChris Leech 	}
1353a703e490SVasu Dev 
1354a703e490SVasu Dev 	return 0;
1355a703e490SVasu Dev err:
1356f018b73aSJoe Eykholt 	per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1357f018b73aSJoe Eykholt 	put_cpu();
1358a703e490SVasu Dev err2:
1359a703e490SVasu Dev 	kfree_skb(skb);
1360a703e490SVasu Dev 	return -1;
1361a703e490SVasu Dev }
1362a703e490SVasu Dev 
1363a703e490SVasu Dev /**
13641875f27eSRobert Love  * fcoe_start_io() - Start FCoE I/O
13651875f27eSRobert Love  * @skb: The packet to be transmitted
13661875f27eSRobert Love  *
13671875f27eSRobert Love  * This routine is called from the net device to start transmitting
13681875f27eSRobert Love  * FCoE packets.
1369a703e490SVasu Dev  *
1370a703e490SVasu Dev  * Returns: 0 for success
1371a703e490SVasu Dev  */
1372a703e490SVasu Dev static inline int fcoe_start_io(struct sk_buff *skb)
1373a703e490SVasu Dev {
137418fa11efSChris Leech 	struct sk_buff *nskb;
1375a703e490SVasu Dev 	int rc;
1376a703e490SVasu Dev 
137718fa11efSChris Leech 	nskb = skb_clone(skb, GFP_ATOMIC);
137818fa11efSChris Leech 	rc = dev_queue_xmit(nskb);
1379a703e490SVasu Dev 	if (rc != 0)
1380a703e490SVasu Dev 		return rc;
1381a703e490SVasu Dev 	kfree_skb(skb);
1382a703e490SVasu Dev 	return 0;
1383a703e490SVasu Dev }
1384a703e490SVasu Dev 
1385a703e490SVasu Dev /**
13861875f27eSRobert Love  * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
13871875f27eSRobert Love  * @skb:  The packet to be transmitted
13881875f27eSRobert Love  * @tlen: The total length of the trailer
13891875f27eSRobert Love  *
13901875f27eSRobert Love  * This routine allocates a page for frame trailers. The page is re-used if
13911875f27eSRobert Love  * there is enough room left on it for the current trailer. If there isn't
13921875f27eSRobert Love  * enough buffer left a new page is allocated for the trailer. Reference to
13931875f27eSRobert Love  * the page from this function as well as the skbs using the page fragments
13941875f27eSRobert Love  * ensure that the page is freed at the appropriate time.
1395a703e490SVasu Dev  *
1396a703e490SVasu Dev  * Returns: 0 for success
1397a703e490SVasu Dev  */
1398a703e490SVasu Dev static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1399a703e490SVasu Dev {
1400a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1401a703e490SVasu Dev 	struct page *page;
1402a703e490SVasu Dev 
1403a703e490SVasu Dev 	fps = &get_cpu_var(fcoe_percpu);
1404a703e490SVasu Dev 	page = fps->crc_eof_page;
1405a703e490SVasu Dev 	if (!page) {
1406a703e490SVasu Dev 		page = alloc_page(GFP_ATOMIC);
1407a703e490SVasu Dev 		if (!page) {
1408a703e490SVasu Dev 			put_cpu_var(fcoe_percpu);
1409a703e490SVasu Dev 			return -ENOMEM;
1410a703e490SVasu Dev 		}
1411a703e490SVasu Dev 		fps->crc_eof_page = page;
1412a703e490SVasu Dev 		fps->crc_eof_offset = 0;
1413a703e490SVasu Dev 	}
1414a703e490SVasu Dev 
1415a703e490SVasu Dev 	get_page(page);
1416a703e490SVasu Dev 	skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1417a703e490SVasu Dev 			   fps->crc_eof_offset, tlen);
1418a703e490SVasu Dev 	skb->len += tlen;
1419a703e490SVasu Dev 	skb->data_len += tlen;
1420a703e490SVasu Dev 	skb->truesize += tlen;
1421a703e490SVasu Dev 	fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1422a703e490SVasu Dev 
1423a703e490SVasu Dev 	if (fps->crc_eof_offset >= PAGE_SIZE) {
1424a703e490SVasu Dev 		fps->crc_eof_page = NULL;
1425a703e490SVasu Dev 		fps->crc_eof_offset = 0;
1426a703e490SVasu Dev 		put_page(page);
1427a703e490SVasu Dev 	}
1428a703e490SVasu Dev 	put_cpu_var(fcoe_percpu);
1429a703e490SVasu Dev 	return 0;
1430a703e490SVasu Dev }
1431a703e490SVasu Dev 
1432a703e490SVasu Dev /**
14331875f27eSRobert Love  * fcoe_fc_crc() - Calculates the CRC for a given frame
14341875f27eSRobert Love  * @fp: The frame to be checksumed
1435a703e490SVasu Dev  *
14361875f27eSRobert Love  * This uses crc32() routine to calculate the CRC for a frame
14371875f27eSRobert Love  *
14381875f27eSRobert Love  * Return: The 32 bit CRC value
1439a703e490SVasu Dev  */
1440a703e490SVasu Dev u32 fcoe_fc_crc(struct fc_frame *fp)
1441a703e490SVasu Dev {
1442a703e490SVasu Dev 	struct sk_buff *skb = fp_skb(fp);
1443a703e490SVasu Dev 	struct skb_frag_struct *frag;
1444a703e490SVasu Dev 	unsigned char *data;
1445a703e490SVasu Dev 	unsigned long off, len, clen;
1446a703e490SVasu Dev 	u32 crc;
1447a703e490SVasu Dev 	unsigned i;
1448a703e490SVasu Dev 
1449a703e490SVasu Dev 	crc = crc32(~0, skb->data, skb_headlen(skb));
1450a703e490SVasu Dev 
1451a703e490SVasu Dev 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1452a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[i];
1453a703e490SVasu Dev 		off = frag->page_offset;
1454a703e490SVasu Dev 		len = frag->size;
1455a703e490SVasu Dev 		while (len > 0) {
1456a703e490SVasu Dev 			clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1457a703e490SVasu Dev 			data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1458a703e490SVasu Dev 					   KM_SKB_DATA_SOFTIRQ);
1459a703e490SVasu Dev 			crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1460a703e490SVasu Dev 			kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1461a703e490SVasu Dev 			off += clen;
1462a703e490SVasu Dev 			len -= clen;
1463a703e490SVasu Dev 		}
1464a703e490SVasu Dev 	}
1465a703e490SVasu Dev 	return crc;
1466a703e490SVasu Dev }
1467a703e490SVasu Dev 
1468a703e490SVasu Dev /**
14691875f27eSRobert Love  * fcoe_xmit() - Transmit a FCoE frame
14701875f27eSRobert Love  * @lport: The local port that the frame is to be transmitted for
14711875f27eSRobert Love  * @fp:	   The frame to be transmitted
1472a703e490SVasu Dev  *
1473a703e490SVasu Dev  * Return: 0 for success
1474a703e490SVasu Dev  */
14751875f27eSRobert Love int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1476a703e490SVasu Dev {
14774bb6b515SVasu Dev 	int wlen;
1478a703e490SVasu Dev 	u32 crc;
1479a703e490SVasu Dev 	struct ethhdr *eh;
1480a703e490SVasu Dev 	struct fcoe_crc_eof *cp;
1481a703e490SVasu Dev 	struct sk_buff *skb;
1482a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1483a703e490SVasu Dev 	struct fc_frame_header *fh;
1484a703e490SVasu Dev 	unsigned int hlen;		/* header length implies the version */
1485a703e490SVasu Dev 	unsigned int tlen;		/* trailer length */
1486a703e490SVasu Dev 	unsigned int elen;		/* eth header, may include vlan */
14871875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
14883fe9a0baSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
1489a703e490SVasu Dev 	u8 sof, eof;
1490a703e490SVasu Dev 	struct fcoe_hdr *hp;
1491a703e490SVasu Dev 
1492a703e490SVasu Dev 	WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1493a703e490SVasu Dev 
1494a703e490SVasu Dev 	fh = fc_frame_header_get(fp);
149597c8389dSJoe Eykholt 	skb = fp_skb(fp);
149697c8389dSJoe Eykholt 	wlen = skb->len / FCOE_WORD_TO_BYTE;
149797c8389dSJoe Eykholt 
14981875f27eSRobert Love 	if (!lport->link_up) {
14993caf02eeSDan Carpenter 		kfree_skb(skb);
150097c8389dSJoe Eykholt 		return 0;
1501a703e490SVasu Dev 	}
1502a703e490SVasu Dev 
15039860eeb4SJoe Eykholt 	if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
15041875f27eSRobert Love 	    fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
150597c8389dSJoe Eykholt 		return 0;
150697c8389dSJoe Eykholt 
1507a703e490SVasu Dev 	sof = fr_sof(fp);
1508a703e490SVasu Dev 	eof = fr_eof(fp);
1509a703e490SVasu Dev 
15104e57e1cbSVasu Dev 	elen = sizeof(struct ethhdr);
1511a703e490SVasu Dev 	hlen = sizeof(struct fcoe_hdr);
1512a703e490SVasu Dev 	tlen = sizeof(struct fcoe_crc_eof);
1513a703e490SVasu Dev 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1514a703e490SVasu Dev 
1515a703e490SVasu Dev 	/* crc offload */
15161875f27eSRobert Love 	if (likely(lport->crc_offload)) {
1517a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_PARTIAL;
1518a703e490SVasu Dev 		skb->csum_start = skb_headroom(skb);
1519a703e490SVasu Dev 		skb->csum_offset = skb->len;
1520a703e490SVasu Dev 		crc = 0;
1521a703e490SVasu Dev 	} else {
1522a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_NONE;
1523a703e490SVasu Dev 		crc = fcoe_fc_crc(fp);
1524a703e490SVasu Dev 	}
1525a703e490SVasu Dev 
1526014f5c3fSChris Leech 	/* copy port crc and eof to the skb buff */
1527a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1528a703e490SVasu Dev 		skb_frag_t *frag;
1529a703e490SVasu Dev 		if (fcoe_get_paged_crc_eof(skb, tlen)) {
1530a703e490SVasu Dev 			kfree_skb(skb);
1531a703e490SVasu Dev 			return -ENOMEM;
1532a703e490SVasu Dev 		}
1533a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1534a703e490SVasu Dev 		cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1535a703e490SVasu Dev 			+ frag->page_offset;
1536a703e490SVasu Dev 	} else {
1537a703e490SVasu Dev 		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1538a703e490SVasu Dev 	}
1539a703e490SVasu Dev 
1540a703e490SVasu Dev 	memset(cp, 0, sizeof(*cp));
1541a703e490SVasu Dev 	cp->fcoe_eof = eof;
1542a703e490SVasu Dev 	cp->fcoe_crc32 = cpu_to_le32(~crc);
1543a703e490SVasu Dev 
1544a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1545a703e490SVasu Dev 		kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1546a703e490SVasu Dev 		cp = NULL;
1547a703e490SVasu Dev 	}
1548a703e490SVasu Dev 
1549014f5c3fSChris Leech 	/* adjust skb network/transport offsets to match mac/fcoe/port */
1550a703e490SVasu Dev 	skb_push(skb, elen + hlen);
1551a703e490SVasu Dev 	skb_reset_mac_header(skb);
1552a703e490SVasu Dev 	skb_reset_network_header(skb);
1553a703e490SVasu Dev 	skb->mac_len = elen;
1554a703e490SVasu Dev 	skb->protocol = htons(ETH_P_FCOE);
15553fe9a0baSChris Leech 	skb->dev = fcoe->netdev;
1556a703e490SVasu Dev 
1557a703e490SVasu Dev 	/* fill up mac and fcoe headers */
1558a703e490SVasu Dev 	eh = eth_hdr(skb);
1559a703e490SVasu Dev 	eh->h_proto = htons(ETH_P_FCOE);
15603fe9a0baSChris Leech 	memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1561cd229e42SJoe Eykholt 	if (fcoe->ctlr.map_dest)
1562cd229e42SJoe Eykholt 		memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1563a703e490SVasu Dev 
15643fe9a0baSChris Leech 	if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
15653fe9a0baSChris Leech 		memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1566a703e490SVasu Dev 	else
156711b56188SChris Leech 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1568a703e490SVasu Dev 
1569a703e490SVasu Dev 	hp = (struct fcoe_hdr *)(eh + 1);
1570a703e490SVasu Dev 	memset(hp, 0, sizeof(*hp));
1571a703e490SVasu Dev 	if (FC_FCOE_VER)
1572a703e490SVasu Dev 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1573a703e490SVasu Dev 	hp->fcoe_sof = sof;
1574a703e490SVasu Dev 
1575a703e490SVasu Dev 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
15761875f27eSRobert Love 	if (lport->seq_offload && fr_max_payload(fp)) {
1577a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1578a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1579a703e490SVasu Dev 	} else {
1580a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = 0;
1581a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = 0;
1582a703e490SVasu Dev 	}
1583a703e490SVasu Dev 	/* update tx stats: regardless if LLD fails */
1584f018b73aSJoe Eykholt 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1585a703e490SVasu Dev 	stats->TxFrames++;
1586a703e490SVasu Dev 	stats->TxWords += wlen;
1587f018b73aSJoe Eykholt 	put_cpu();
1588a703e490SVasu Dev 
1589a703e490SVasu Dev 	/* send down to lld */
15901875f27eSRobert Love 	fr_dev(fp) = lport;
1591014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen)
15921875f27eSRobert Love 		fcoe_check_wait_queue(lport, skb);
15934bb6b515SVasu Dev 	else if (fcoe_start_io(skb))
15941875f27eSRobert Love 		fcoe_check_wait_queue(lport, skb);
1595a703e490SVasu Dev 
1596a703e490SVasu Dev 	return 0;
1597a703e490SVasu Dev }
1598a703e490SVasu Dev 
1599a703e490SVasu Dev /**
16001875f27eSRobert Love  * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
16011875f27eSRobert Love  * @skb: The completed skb (argument required by destructor)
1602e7a51997SJoe Eykholt  */
1603e7a51997SJoe Eykholt static void fcoe_percpu_flush_done(struct sk_buff *skb)
1604e7a51997SJoe Eykholt {
1605e7a51997SJoe Eykholt 	complete(&fcoe_flush_completion);
1606e7a51997SJoe Eykholt }
1607e7a51997SJoe Eykholt 
1608e7a51997SJoe Eykholt /**
1609859b7b64SChris Leech  * fcoe_recv_frame() - process a single received frame
1610859b7b64SChris Leech  * @skb: frame to process
1611a703e490SVasu Dev  */
1612859b7b64SChris Leech static void fcoe_recv_frame(struct sk_buff *skb)
1613a703e490SVasu Dev {
1614a703e490SVasu Dev 	u32 fr_len;
16151875f27eSRobert Love 	struct fc_lport *lport;
1616a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1617a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1618a703e490SVasu Dev 	struct fc_frame_header *fh;
1619a703e490SVasu Dev 	struct fcoe_crc_eof crc_eof;
1620a703e490SVasu Dev 	struct fc_frame *fp;
1621014f5c3fSChris Leech 	struct fcoe_port *port;
1622a703e490SVasu Dev 	struct fcoe_hdr *hp;
1623a703e490SVasu Dev 
1624a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
16251875f27eSRobert Love 	lport = fr->fr_dev;
16261875f27eSRobert Love 	if (unlikely(!lport)) {
1627e7a51997SJoe Eykholt 		if (skb->destructor != fcoe_percpu_flush_done)
1628e7a51997SJoe Eykholt 			FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1629a703e490SVasu Dev 		kfree_skb(skb);
1630859b7b64SChris Leech 		return;
1631a703e490SVasu Dev 	}
1632a703e490SVasu Dev 
1633d5488eb9SRobert Love 	FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1634d5488eb9SRobert Love 			"head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1635a703e490SVasu Dev 			skb->len, skb->data_len,
1636a703e490SVasu Dev 			skb->head, skb->data, skb_tail_pointer(skb),
1637a703e490SVasu Dev 			skb_end_pointer(skb), skb->csum,
1638a703e490SVasu Dev 			skb->dev ? skb->dev->name : "<NULL>");
1639a703e490SVasu Dev 
16401875f27eSRobert Love 	port = lport_priv(lport);
1641a703e490SVasu Dev 	if (skb_is_nonlinear(skb))
1642a703e490SVasu Dev 		skb_linearize(skb);	/* not ideal */
1643a703e490SVasu Dev 
1644a703e490SVasu Dev 	/*
1645a703e490SVasu Dev 	 * Frame length checks and setting up the header pointers
1646a703e490SVasu Dev 	 * was done in fcoe_rcv already.
1647a703e490SVasu Dev 	 */
1648a703e490SVasu Dev 	hp = (struct fcoe_hdr *) skb_network_header(skb);
1649a703e490SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
1650a703e490SVasu Dev 
1651f018b73aSJoe Eykholt 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1652a703e490SVasu Dev 	if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1653a703e490SVasu Dev 		if (stats->ErrorFrames < 5)
1654d5488eb9SRobert Love 			printk(KERN_WARNING "fcoe: FCoE version "
1655a703e490SVasu Dev 			       "mismatch: The frame has "
1656a703e490SVasu Dev 			       "version %x, but the "
1657a703e490SVasu Dev 			       "initiator supports version "
1658a703e490SVasu Dev 			       "%x\n", FC_FCOE_DECAPS_VER(hp),
1659a703e490SVasu Dev 			       FC_FCOE_VER);
1660f018b73aSJoe Eykholt 		goto drop;
1661a703e490SVasu Dev 	}
1662a703e490SVasu Dev 
1663a703e490SVasu Dev 	skb_pull(skb, sizeof(struct fcoe_hdr));
1664a703e490SVasu Dev 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1665a703e490SVasu Dev 
1666a703e490SVasu Dev 	stats->RxFrames++;
1667a703e490SVasu Dev 	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1668a703e490SVasu Dev 
1669a703e490SVasu Dev 	fp = (struct fc_frame *)skb;
1670a703e490SVasu Dev 	fc_frame_init(fp);
16711875f27eSRobert Love 	fr_dev(fp) = lport;
1672a703e490SVasu Dev 	fr_sof(fp) = hp->fcoe_sof;
1673a703e490SVasu Dev 
1674a703e490SVasu Dev 	/* Copy out the CRC and EOF trailer for access */
1675f018b73aSJoe Eykholt 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1676f018b73aSJoe Eykholt 		goto drop;
1677a703e490SVasu Dev 	fr_eof(fp) = crc_eof.fcoe_eof;
1678a703e490SVasu Dev 	fr_crc(fp) = crc_eof.fcoe_crc32;
1679f018b73aSJoe Eykholt 	if (pskb_trim(skb, fr_len))
1680f018b73aSJoe Eykholt 		goto drop;
1681a703e490SVasu Dev 
1682a703e490SVasu Dev 	/*
1683a703e490SVasu Dev 	 * We only check CRC if no offload is available and if it is
1684a703e490SVasu Dev 	 * it's solicited data, in which case, the FCP layer would
1685a703e490SVasu Dev 	 * check it during the copy.
1686a703e490SVasu Dev 	 */
16871875f27eSRobert Love 	if (lport->crc_offload &&
16881875f27eSRobert Love 	    skb->ip_summed == CHECKSUM_UNNECESSARY)
1689a703e490SVasu Dev 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1690a703e490SVasu Dev 	else
1691a703e490SVasu Dev 		fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1692a703e490SVasu Dev 
1693a703e490SVasu Dev 	fh = fc_frame_header_get(fp);
1694f018b73aSJoe Eykholt 	if ((fh->fh_r_ctl != FC_RCTL_DD_SOL_DATA ||
1695f018b73aSJoe Eykholt 	    fh->fh_type != FC_TYPE_FCP) &&
1696f018b73aSJoe Eykholt 	    (fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
1697a703e490SVasu Dev 		if (le32_to_cpu(fr_crc(fp)) !=
1698a703e490SVasu Dev 		    ~crc32(~0, skb->data, fr_len)) {
1699d5488eb9SRobert Love 			if (stats->InvalidCRCCount < 5)
1700a703e490SVasu Dev 				printk(KERN_WARNING "fcoe: dropping "
1701a703e490SVasu Dev 				       "frame with CRC error\n");
1702a703e490SVasu Dev 			stats->InvalidCRCCount++;
1703f018b73aSJoe Eykholt 			goto drop;
1704a703e490SVasu Dev 		}
1705a703e490SVasu Dev 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1706a703e490SVasu Dev 	}
1707f018b73aSJoe Eykholt 	put_cpu();
17081875f27eSRobert Love 	fc_exch_recv(lport, fp);
1709f018b73aSJoe Eykholt 	return;
1710f018b73aSJoe Eykholt 
1711f018b73aSJoe Eykholt drop:
1712f018b73aSJoe Eykholt 	stats->ErrorFrames++;
1713f018b73aSJoe Eykholt 	put_cpu();
1714f018b73aSJoe Eykholt 	kfree_skb(skb);
1715a703e490SVasu Dev }
1716859b7b64SChris Leech 
1717859b7b64SChris Leech /**
1718859b7b64SChris Leech  * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1719859b7b64SChris Leech  * @arg: The per-CPU context
1720859b7b64SChris Leech  *
1721859b7b64SChris Leech  * Return: 0 for success
1722859b7b64SChris Leech  */
1723859b7b64SChris Leech int fcoe_percpu_receive_thread(void *arg)
1724859b7b64SChris Leech {
1725859b7b64SChris Leech 	struct fcoe_percpu_s *p = arg;
1726859b7b64SChris Leech 	struct sk_buff *skb;
1727859b7b64SChris Leech 
1728859b7b64SChris Leech 	set_user_nice(current, -20);
1729859b7b64SChris Leech 
1730859b7b64SChris Leech 	while (!kthread_should_stop()) {
1731859b7b64SChris Leech 
1732859b7b64SChris Leech 		spin_lock_bh(&p->fcoe_rx_list.lock);
1733859b7b64SChris Leech 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1734859b7b64SChris Leech 			set_current_state(TASK_INTERRUPTIBLE);
1735859b7b64SChris Leech 			spin_unlock_bh(&p->fcoe_rx_list.lock);
1736859b7b64SChris Leech 			schedule();
1737859b7b64SChris Leech 			set_current_state(TASK_RUNNING);
1738859b7b64SChris Leech 			if (kthread_should_stop())
1739859b7b64SChris Leech 				return 0;
1740859b7b64SChris Leech 			spin_lock_bh(&p->fcoe_rx_list.lock);
1741859b7b64SChris Leech 		}
1742859b7b64SChris Leech 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1743859b7b64SChris Leech 		fcoe_recv_frame(skb);
1744859b7b64SChris Leech 	}
1745a703e490SVasu Dev 	return 0;
1746a703e490SVasu Dev }
1747a703e490SVasu Dev 
1748a703e490SVasu Dev /**
17491875f27eSRobert Love  * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
17501875f27eSRobert Love  * @lport: The local port whose backlog is to be cleared
1751a703e490SVasu Dev  *
17521875f27eSRobert Love  * This empties the wait_queue, dequeues the head of the wait_queue queue
17531875f27eSRobert Love  * and calls fcoe_start_io() for each packet. If all skb have been
17541875f27eSRobert Love  * transmitted it returns the qlen. If an error occurs it restores
17551875f27eSRobert Love  * wait_queue (to try again later) and returns -1.
1756a703e490SVasu Dev  *
17571875f27eSRobert Love  * The wait_queue is used when the skb transmit fails. The failed skb
17581875f27eSRobert Love  * will go in the wait_queue which will be emptied by the timer function or
1759a703e490SVasu Dev  * by the next skb transmit.
1760a703e490SVasu Dev  */
17611875f27eSRobert Love static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
1762a703e490SVasu Dev {
17631875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
17644bb6b515SVasu Dev 	int rc;
1765a703e490SVasu Dev 
1766014f5c3fSChris Leech 	spin_lock_bh(&port->fcoe_pending_queue.lock);
17674bb6b515SVasu Dev 
17684bb6b515SVasu Dev 	if (skb)
1769014f5c3fSChris Leech 		__skb_queue_tail(&port->fcoe_pending_queue, skb);
17704bb6b515SVasu Dev 
1771014f5c3fSChris Leech 	if (port->fcoe_pending_queue_active)
1772a703e490SVasu Dev 		goto out;
1773014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 1;
1774a703e490SVasu Dev 
1775014f5c3fSChris Leech 	while (port->fcoe_pending_queue.qlen) {
1776a703e490SVasu Dev 		/* keep qlen > 0 until fcoe_start_io succeeds */
1777014f5c3fSChris Leech 		port->fcoe_pending_queue.qlen++;
1778014f5c3fSChris Leech 		skb = __skb_dequeue(&port->fcoe_pending_queue);
1779a703e490SVasu Dev 
1780014f5c3fSChris Leech 		spin_unlock_bh(&port->fcoe_pending_queue.lock);
1781a703e490SVasu Dev 		rc = fcoe_start_io(skb);
1782014f5c3fSChris Leech 		spin_lock_bh(&port->fcoe_pending_queue.lock);
1783a703e490SVasu Dev 
1784a703e490SVasu Dev 		if (rc) {
1785014f5c3fSChris Leech 			__skb_queue_head(&port->fcoe_pending_queue, skb);
1786a703e490SVasu Dev 			/* undo temporary increment above */
1787014f5c3fSChris Leech 			port->fcoe_pending_queue.qlen--;
1788a703e490SVasu Dev 			break;
1789a703e490SVasu Dev 		}
1790a703e490SVasu Dev 		/* undo temporary increment above */
1791014f5c3fSChris Leech 		port->fcoe_pending_queue.qlen--;
1792a703e490SVasu Dev 	}
1793a703e490SVasu Dev 
1794014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
17951875f27eSRobert Love 		lport->qfull = 0;
1796014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1797014f5c3fSChris Leech 		mod_timer(&port->timer, jiffies + 2);
1798014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
1799a703e490SVasu Dev out:
1800014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
18011875f27eSRobert Love 		lport->qfull = 1;
1802014f5c3fSChris Leech 	spin_unlock_bh(&port->fcoe_pending_queue.lock);
18034bb6b515SVasu Dev 	return;
1804a703e490SVasu Dev }
1805a703e490SVasu Dev 
1806a703e490SVasu Dev /**
18071875f27eSRobert Love  * fcoe_dev_setup() - Setup the link change notification interface
1808a703e490SVasu Dev  */
1809b0d428adSRandy Dunlap static void fcoe_dev_setup(void)
1810a703e490SVasu Dev {
1811a703e490SVasu Dev 	register_netdevice_notifier(&fcoe_notifier);
1812a703e490SVasu Dev }
1813a703e490SVasu Dev 
1814a703e490SVasu Dev /**
18151875f27eSRobert Love  * fcoe_dev_cleanup() - Cleanup the link change notification interface
1816a703e490SVasu Dev  */
1817a703e490SVasu Dev static void fcoe_dev_cleanup(void)
1818a703e490SVasu Dev {
1819a703e490SVasu Dev 	unregister_netdevice_notifier(&fcoe_notifier);
1820a703e490SVasu Dev }
1821a703e490SVasu Dev 
1822a703e490SVasu Dev /**
18231875f27eSRobert Love  * fcoe_device_notification() - Handler for net device events
18241875f27eSRobert Love  * @notifier: The context of the notification
18251875f27eSRobert Love  * @event:    The type of event
18261875f27eSRobert Love  * @ptr:      The net device that the event was on
1827a703e490SVasu Dev  *
18281875f27eSRobert Love  * This function is called by the Ethernet driver in case of link change event.
1829a703e490SVasu Dev  *
1830a703e490SVasu Dev  * Returns: 0 for success
1831a703e490SVasu Dev  */
1832a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *notifier,
1833a703e490SVasu Dev 				    ulong event, void *ptr)
1834a703e490SVasu Dev {
18351875f27eSRobert Love 	struct fc_lport *lport = NULL;
18361d1b88dcSVasu Dev 	struct net_device *netdev = ptr;
1837014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
18382e70e241SChris Leech 	struct fcoe_port *port;
1839a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
184097c8389dSJoe Eykholt 	u32 link_possible = 1;
1841a703e490SVasu Dev 	u32 mfs;
1842a703e490SVasu Dev 	int rc = NOTIFY_OK;
1843a703e490SVasu Dev 
1844014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
184525024989SChris Leech 		if (fcoe->netdev == netdev) {
18461875f27eSRobert Love 			lport = fcoe->ctlr.lp;
1847a703e490SVasu Dev 			break;
1848a703e490SVasu Dev 		}
1849a703e490SVasu Dev 	}
18501875f27eSRobert Love 	if (!lport) {
1851a703e490SVasu Dev 		rc = NOTIFY_DONE;
1852a703e490SVasu Dev 		goto out;
1853a703e490SVasu Dev 	}
1854a703e490SVasu Dev 
1855a703e490SVasu Dev 	switch (event) {
1856a703e490SVasu Dev 	case NETDEV_DOWN:
1857a703e490SVasu Dev 	case NETDEV_GOING_DOWN:
185897c8389dSJoe Eykholt 		link_possible = 0;
1859a703e490SVasu Dev 		break;
1860a703e490SVasu Dev 	case NETDEV_UP:
1861a703e490SVasu Dev 	case NETDEV_CHANGE:
1862a703e490SVasu Dev 		break;
1863a703e490SVasu Dev 	case NETDEV_CHANGEMTU:
18647221d7e5SYi Zou 		if (netdev->features & NETIF_F_FCOE_MTU)
18657221d7e5SYi Zou 			break;
18661d1b88dcSVasu Dev 		mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1867a703e490SVasu Dev 				     sizeof(struct fcoe_crc_eof));
1868a703e490SVasu Dev 		if (mfs >= FC_MIN_MAX_FRAME)
18691875f27eSRobert Love 			fc_set_mfs(lport, mfs);
1870a703e490SVasu Dev 		break;
1871a703e490SVasu Dev 	case NETDEV_REGISTER:
1872a703e490SVasu Dev 		break;
18732e70e241SChris Leech 	case NETDEV_UNREGISTER:
18742e70e241SChris Leech 		list_del(&fcoe->list);
18752e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
18762e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
18772e70e241SChris Leech 		schedule_work(&port->destroy_work);
18782e70e241SChris Leech 		goto out;
18792e70e241SChris Leech 		break;
188054a5b21dSYi Zou 	case NETDEV_FEAT_CHANGE:
188154a5b21dSYi Zou 		fcoe_netdev_features_change(lport, netdev);
188254a5b21dSYi Zou 		break;
1883a703e490SVasu Dev 	default:
18841d1b88dcSVasu Dev 		FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1885d5488eb9SRobert Love 				"from netdev netlink\n", event);
1886a703e490SVasu Dev 	}
18875e4f8fe7SRobert Love 
18885e4f8fe7SRobert Love 	fcoe_link_speed_update(lport);
18895e4f8fe7SRobert Love 
18901875f27eSRobert Love 	if (link_possible && !fcoe_link_ok(lport))
18913fe9a0baSChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
18923fe9a0baSChris Leech 	else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1893f018b73aSJoe Eykholt 		stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1894a703e490SVasu Dev 		stats->LinkFailureCount++;
1895f018b73aSJoe Eykholt 		put_cpu();
18961875f27eSRobert Love 		fcoe_clean_pending_queue(lport);
1897a703e490SVasu Dev 	}
1898a703e490SVasu Dev out:
1899a703e490SVasu Dev 	return rc;
1900a703e490SVasu Dev }
1901a703e490SVasu Dev 
1902a703e490SVasu Dev /**
19031875f27eSRobert Love  * fcoe_if_to_netdev() - Parse a name buffer to get a net device
19041875f27eSRobert Love  * @buffer: The name of the net device
1905a703e490SVasu Dev  *
19061875f27eSRobert Love  * Returns: NULL or a ptr to net_device
1907a703e490SVasu Dev  */
1908a703e490SVasu Dev static struct net_device *fcoe_if_to_netdev(const char *buffer)
1909a703e490SVasu Dev {
1910a703e490SVasu Dev 	char *cp;
1911a703e490SVasu Dev 	char ifname[IFNAMSIZ + 2];
1912a703e490SVasu Dev 
1913a703e490SVasu Dev 	if (buffer) {
1914a703e490SVasu Dev 		strlcpy(ifname, buffer, IFNAMSIZ);
1915a703e490SVasu Dev 		cp = ifname + strlen(ifname);
1916a703e490SVasu Dev 		while (--cp >= ifname && *cp == '\n')
1917a703e490SVasu Dev 			*cp = '\0';
1918a703e490SVasu Dev 		return dev_get_by_name(&init_net, ifname);
1919a703e490SVasu Dev 	}
1920a703e490SVasu Dev 	return NULL;
1921a703e490SVasu Dev }
1922a703e490SVasu Dev 
1923a703e490SVasu Dev /**
192455a66d3cSVasu Dev  * fcoe_disable() - Disables a FCoE interface
192555a66d3cSVasu Dev  * @buffer: The name of the Ethernet interface to be disabled
192655a66d3cSVasu Dev  * @kp:	    The associated kernel parameter
192755a66d3cSVasu Dev  *
192855a66d3cSVasu Dev  * Called from sysfs.
192955a66d3cSVasu Dev  *
193055a66d3cSVasu Dev  * Returns: 0 for success
193155a66d3cSVasu Dev  */
193255a66d3cSVasu Dev static int fcoe_disable(const char *buffer, struct kernel_param *kp)
193355a66d3cSVasu Dev {
193455a66d3cSVasu Dev 	struct fcoe_interface *fcoe;
193555a66d3cSVasu Dev 	struct net_device *netdev;
193655a66d3cSVasu Dev 	int rc = 0;
193755a66d3cSVasu Dev 
193855a66d3cSVasu Dev 	mutex_lock(&fcoe_config_mutex);
193955a66d3cSVasu Dev #ifdef CONFIG_FCOE_MODULE
194055a66d3cSVasu Dev 	/*
194155a66d3cSVasu Dev 	 * Make sure the module has been initialized, and is not about to be
194255a66d3cSVasu Dev 	 * removed.  Module paramter sysfs files are writable before the
194355a66d3cSVasu Dev 	 * module_init function is called and after module_exit.
194455a66d3cSVasu Dev 	 */
194555a66d3cSVasu Dev 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
194655a66d3cSVasu Dev 		rc = -ENODEV;
194755a66d3cSVasu Dev 		goto out_nodev;
194855a66d3cSVasu Dev 	}
194955a66d3cSVasu Dev #endif
195055a66d3cSVasu Dev 
195155a66d3cSVasu Dev 	netdev = fcoe_if_to_netdev(buffer);
195255a66d3cSVasu Dev 	if (!netdev) {
195355a66d3cSVasu Dev 		rc = -ENODEV;
195455a66d3cSVasu Dev 		goto out_nodev;
195555a66d3cSVasu Dev 	}
195655a66d3cSVasu Dev 
195734ce27bcSVasu Dev 	if (!rtnl_trylock()) {
195834ce27bcSVasu Dev 		dev_put(netdev);
195934ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
196034ce27bcSVasu Dev 		return restart_syscall();
196134ce27bcSVasu Dev 	}
196234ce27bcSVasu Dev 
196355a66d3cSVasu Dev 	fcoe = fcoe_hostlist_lookup_port(netdev);
196455a66d3cSVasu Dev 	rtnl_unlock();
196555a66d3cSVasu Dev 
19669ee50e48SChris Leech 	if (fcoe) {
19679ee50e48SChris Leech 		fcoe_ctlr_link_down(&fcoe->ctlr);
19689d4cbc05SVasu Dev 		fcoe_clean_pending_queue(fcoe->ctlr.lp);
19699ee50e48SChris Leech 	} else
197055a66d3cSVasu Dev 		rc = -ENODEV;
197155a66d3cSVasu Dev 
197255a66d3cSVasu Dev 	dev_put(netdev);
197355a66d3cSVasu Dev out_nodev:
197455a66d3cSVasu Dev 	mutex_unlock(&fcoe_config_mutex);
197555a66d3cSVasu Dev 	return rc;
197655a66d3cSVasu Dev }
197755a66d3cSVasu Dev 
197855a66d3cSVasu Dev /**
197955a66d3cSVasu Dev  * fcoe_enable() - Enables a FCoE interface
198055a66d3cSVasu Dev  * @buffer: The name of the Ethernet interface to be enabled
198155a66d3cSVasu Dev  * @kp:     The associated kernel parameter
198255a66d3cSVasu Dev  *
198355a66d3cSVasu Dev  * Called from sysfs.
198455a66d3cSVasu Dev  *
198555a66d3cSVasu Dev  * Returns: 0 for success
198655a66d3cSVasu Dev  */
198755a66d3cSVasu Dev static int fcoe_enable(const char *buffer, struct kernel_param *kp)
198855a66d3cSVasu Dev {
198955a66d3cSVasu Dev 	struct fcoe_interface *fcoe;
199055a66d3cSVasu Dev 	struct net_device *netdev;
199155a66d3cSVasu Dev 	int rc = 0;
199255a66d3cSVasu Dev 
199355a66d3cSVasu Dev 	mutex_lock(&fcoe_config_mutex);
199455a66d3cSVasu Dev #ifdef CONFIG_FCOE_MODULE
199555a66d3cSVasu Dev 	/*
199655a66d3cSVasu Dev 	 * Make sure the module has been initialized, and is not about to be
199755a66d3cSVasu Dev 	 * removed.  Module paramter sysfs files are writable before the
199855a66d3cSVasu Dev 	 * module_init function is called and after module_exit.
199955a66d3cSVasu Dev 	 */
200055a66d3cSVasu Dev 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
200155a66d3cSVasu Dev 		rc = -ENODEV;
200255a66d3cSVasu Dev 		goto out_nodev;
200355a66d3cSVasu Dev 	}
200455a66d3cSVasu Dev #endif
200555a66d3cSVasu Dev 
200655a66d3cSVasu Dev 	netdev = fcoe_if_to_netdev(buffer);
200755a66d3cSVasu Dev 	if (!netdev) {
200855a66d3cSVasu Dev 		rc = -ENODEV;
200955a66d3cSVasu Dev 		goto out_nodev;
201055a66d3cSVasu Dev 	}
201155a66d3cSVasu Dev 
201234ce27bcSVasu Dev 	if (!rtnl_trylock()) {
201334ce27bcSVasu Dev 		dev_put(netdev);
201434ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
201534ce27bcSVasu Dev 		return restart_syscall();
201634ce27bcSVasu Dev 	}
201734ce27bcSVasu Dev 
201855a66d3cSVasu Dev 	fcoe = fcoe_hostlist_lookup_port(netdev);
201955a66d3cSVasu Dev 	rtnl_unlock();
202055a66d3cSVasu Dev 
20219d4cbc05SVasu Dev 	if (!fcoe)
202255a66d3cSVasu Dev 		rc = -ENODEV;
20239d4cbc05SVasu Dev 	else if (!fcoe_link_ok(fcoe->ctlr.lp))
20249d4cbc05SVasu Dev 		fcoe_ctlr_link_up(&fcoe->ctlr);
202555a66d3cSVasu Dev 
202655a66d3cSVasu Dev 	dev_put(netdev);
202755a66d3cSVasu Dev out_nodev:
202855a66d3cSVasu Dev 	mutex_unlock(&fcoe_config_mutex);
202955a66d3cSVasu Dev 	return rc;
203055a66d3cSVasu Dev }
203155a66d3cSVasu Dev 
203255a66d3cSVasu Dev /**
20331875f27eSRobert Love  * fcoe_destroy() - Destroy a FCoE interface
20341875f27eSRobert Love  * @buffer: The name of the Ethernet interface to be destroyed
20351875f27eSRobert Love  * @kp:	    The associated kernel parameter
20361875f27eSRobert Love  *
20371875f27eSRobert Love  * Called from sysfs.
2038a703e490SVasu Dev  *
2039a703e490SVasu Dev  * Returns: 0 for success
2040a703e490SVasu Dev  */
2041a703e490SVasu Dev static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
2042a703e490SVasu Dev {
2043030f4e00SChris Leech 	struct fcoe_interface *fcoe;
20442e70e241SChris Leech 	struct net_device *netdev;
20458eca355fSMike Christie 	int rc = 0;
2046a703e490SVasu Dev 
2047dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2048dfc1d0feSChris Leech #ifdef CONFIG_FCOE_MODULE
2049dfc1d0feSChris Leech 	/*
2050dfc1d0feSChris Leech 	 * Make sure the module has been initialized, and is not about to be
2051dfc1d0feSChris Leech 	 * removed.  Module paramter sysfs files are writable before the
2052dfc1d0feSChris Leech 	 * module_init function is called and after module_exit.
2053dfc1d0feSChris Leech 	 */
2054dfc1d0feSChris Leech 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2055dfc1d0feSChris Leech 		rc = -ENODEV;
2056dfc1d0feSChris Leech 		goto out_nodev;
2057dfc1d0feSChris Leech 	}
2058dfc1d0feSChris Leech #endif
2059dfc1d0feSChris Leech 
2060a703e490SVasu Dev 	netdev = fcoe_if_to_netdev(buffer);
2061a703e490SVasu Dev 	if (!netdev) {
2062a703e490SVasu Dev 		rc = -ENODEV;
2063a703e490SVasu Dev 		goto out_nodev;
2064a703e490SVasu Dev 	}
20652e70e241SChris Leech 
206634ce27bcSVasu Dev 	if (!rtnl_trylock()) {
206734ce27bcSVasu Dev 		dev_put(netdev);
206834ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
206934ce27bcSVasu Dev 		return restart_syscall();
207034ce27bcSVasu Dev 	}
207134ce27bcSVasu Dev 
20722e70e241SChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
20732e70e241SChris Leech 	if (!fcoe) {
2074090eb6c4SChris Leech 		rtnl_unlock();
2075a703e490SVasu Dev 		rc = -ENODEV;
2076a703e490SVasu Dev 		goto out_putdev;
2077a703e490SVasu Dev 	}
20782e70e241SChris Leech 	fcoe_interface_cleanup(fcoe);
207954a5b21dSYi Zou 	list_del(&fcoe->list);
208034ce27bcSVasu Dev 	/* RTNL mutex is dropped by fcoe_if_destroy */
20812e70e241SChris Leech 	fcoe_if_destroy(fcoe->ctlr.lp);
20826409ea65SRob Love 
2083a703e490SVasu Dev out_putdev:
2084a703e490SVasu Dev 	dev_put(netdev);
2085a703e490SVasu Dev out_nodev:
2086dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2087a703e490SVasu Dev 	return rc;
2088a703e490SVasu Dev }
2089a703e490SVasu Dev 
20901875f27eSRobert Love /**
20911875f27eSRobert Love  * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
20921875f27eSRobert Love  * @work: Handle to the FCoE port to be destroyed
20931875f27eSRobert Love  */
20942e70e241SChris Leech static void fcoe_destroy_work(struct work_struct *work)
20952e70e241SChris Leech {
20962e70e241SChris Leech 	struct fcoe_port *port;
20972e70e241SChris Leech 
20982e70e241SChris Leech 	port = container_of(work, struct fcoe_port, destroy_work);
20992e70e241SChris Leech 	mutex_lock(&fcoe_config_mutex);
210034ce27bcSVasu Dev 	rtnl_lock();
210134ce27bcSVasu Dev 	/* RTNL mutex is dropped by fcoe_if_destroy */
21022e70e241SChris Leech 	fcoe_if_destroy(port->lport);
21032e70e241SChris Leech 	mutex_unlock(&fcoe_config_mutex);
21042e70e241SChris Leech }
21052e70e241SChris Leech 
2106a703e490SVasu Dev /**
21071875f27eSRobert Love  * fcoe_create() - Create a fcoe interface
21081875f27eSRobert Love  * @buffer: The name of the Ethernet interface to create on
21091875f27eSRobert Love  * @kp:	    The associated kernel param
21101875f27eSRobert Love  *
21111875f27eSRobert Love  * Called from sysfs.
2112a703e490SVasu Dev  *
2113a703e490SVasu Dev  * Returns: 0 for success
2114a703e490SVasu Dev  */
2115a703e490SVasu Dev static int fcoe_create(const char *buffer, struct kernel_param *kp)
2116a703e490SVasu Dev {
21171dd454d9SJoe Eykholt 	enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
2118a703e490SVasu Dev 	int rc;
2119030f4e00SChris Leech 	struct fcoe_interface *fcoe;
2120af7f85d9SChris Leech 	struct fc_lport *lport;
2121a703e490SVasu Dev 	struct net_device *netdev;
2122a703e490SVasu Dev 
2123dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
212434ce27bcSVasu Dev 
212534ce27bcSVasu Dev 	if (!rtnl_trylock()) {
212634ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
212734ce27bcSVasu Dev 		return restart_syscall();
212834ce27bcSVasu Dev 	}
212934ce27bcSVasu Dev 
2130dfc1d0feSChris Leech #ifdef CONFIG_FCOE_MODULE
2131dfc1d0feSChris Leech 	/*
2132dfc1d0feSChris Leech 	 * Make sure the module has been initialized, and is not about to be
2133dfc1d0feSChris Leech 	 * removed.  Module paramter sysfs files are writable before the
2134dfc1d0feSChris Leech 	 * module_init function is called and after module_exit.
2135dfc1d0feSChris Leech 	 */
2136dfc1d0feSChris Leech 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2137dfc1d0feSChris Leech 		rc = -ENODEV;
2138721cafafSVasu Dev 		goto out_nomod;
2139dfc1d0feSChris Leech 	}
2140dfc1d0feSChris Leech #endif
2141dfc1d0feSChris Leech 
21426409ea65SRob Love 	if (!try_module_get(THIS_MODULE)) {
21436409ea65SRob Love 		rc = -EINVAL;
21446409ea65SRob Love 		goto out_nomod;
21456409ea65SRob Love 	}
21466409ea65SRob Love 
2147a703e490SVasu Dev 	netdev = fcoe_if_to_netdev(buffer);
2148a703e490SVasu Dev 	if (!netdev) {
2149a703e490SVasu Dev 		rc = -ENODEV;
2150a703e490SVasu Dev 		goto out_nodev;
2151a703e490SVasu Dev 	}
21522e70e241SChris Leech 
2153a703e490SVasu Dev 	/* look for existing lport */
2154a703e490SVasu Dev 	if (fcoe_hostlist_lookup(netdev)) {
2155a703e490SVasu Dev 		rc = -EEXIST;
2156a703e490SVasu Dev 		goto out_putdev;
2157a703e490SVasu Dev 	}
2158a703e490SVasu Dev 
21591dd454d9SJoe Eykholt 	fcoe = fcoe_interface_create(netdev, fip_mode);
2160030f4e00SChris Leech 	if (!fcoe) {
2161030f4e00SChris Leech 		rc = -ENOMEM;
2162030f4e00SChris Leech 		goto out_putdev;
2163030f4e00SChris Leech 	}
2164030f4e00SChris Leech 
21659a05753bSChris Leech 	lport = fcoe_if_create(fcoe, &netdev->dev, 0);
2166af7f85d9SChris Leech 	if (IS_ERR(lport)) {
2167d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2168a703e490SVasu Dev 		       netdev->name);
2169a703e490SVasu Dev 		rc = -EIO;
21702e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
2171030f4e00SChris Leech 		goto out_free;
2172a703e490SVasu Dev 	}
2173030f4e00SChris Leech 
217454b649f8SChris Leech 	/* Make this the "master" N_Port */
217554b649f8SChris Leech 	fcoe->ctlr.lp = lport;
2176030f4e00SChris Leech 
2177c863df33SChris Leech 	/* add to lports list */
2178c863df33SChris Leech 	fcoe_hostlist_add(lport);
2179c863df33SChris Leech 
218054b649f8SChris Leech 	/* start FIP Discovery and FLOGI */
218154b649f8SChris Leech 	lport->boot_time = jiffies;
218254b649f8SChris Leech 	fc_fabric_login(lport);
218354b649f8SChris Leech 	if (!fcoe_link_ok(lport))
218454b649f8SChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
218554b649f8SChris Leech 
218654b649f8SChris Leech 	/*
218754b649f8SChris Leech 	 * Release from init in fcoe_interface_create(), on success lport
218854b649f8SChris Leech 	 * should be holding a reference taken in fcoe_if_create().
218954b649f8SChris Leech 	 */
2190030f4e00SChris Leech 	fcoe_interface_put(fcoe);
21916409ea65SRob Love 	dev_put(netdev);
21926409ea65SRob Love 	rtnl_unlock();
21936409ea65SRob Love 	mutex_unlock(&fcoe_config_mutex);
21946409ea65SRob Love 
21956409ea65SRob Love 	return 0;
21966409ea65SRob Love out_free:
21976409ea65SRob Love 	fcoe_interface_put(fcoe);
2198a703e490SVasu Dev out_putdev:
2199a703e490SVasu Dev 	dev_put(netdev);
2200a703e490SVasu Dev out_nodev:
22016409ea65SRob Love 	module_put(THIS_MODULE);
22026409ea65SRob Love out_nomod:
220334ce27bcSVasu Dev 	rtnl_unlock();
2204dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2205a703e490SVasu Dev 	return rc;
2206a703e490SVasu Dev }
2207a703e490SVasu Dev 
2208a703e490SVasu Dev /**
22095e4f8fe7SRobert Love  * fcoe_link_speed_update() - Update the supported and actual link speeds
22105e4f8fe7SRobert Love  * @lport: The local port to update speeds for
2211a703e490SVasu Dev  *
22125e4f8fe7SRobert Love  * Returns: 0 if the ethtool query was successful
22135e4f8fe7SRobert Love  *          -1 if the ethtool query failed
2214a703e490SVasu Dev  */
22155e4f8fe7SRobert Love int fcoe_link_speed_update(struct fc_lport *lport)
2216a703e490SVasu Dev {
22171875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
22181875f27eSRobert Love 	struct net_device *netdev = port->fcoe->netdev;
2219a703e490SVasu Dev 	struct ethtool_cmd ecmd = { ETHTOOL_GSET };
2220a703e490SVasu Dev 
22215e4f8fe7SRobert Love 	if (!dev_ethtool_get_settings(netdev, &ecmd)) {
22221875f27eSRobert Love 		lport->link_supported_speeds &=
2223a703e490SVasu Dev 			~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2224a703e490SVasu Dev 		if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2225a703e490SVasu Dev 				      SUPPORTED_1000baseT_Full))
22261875f27eSRobert Love 			lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2227a703e490SVasu Dev 		if (ecmd.supported & SUPPORTED_10000baseT_Full)
22281875f27eSRobert Love 			lport->link_supported_speeds |=
2229a703e490SVasu Dev 				FC_PORTSPEED_10GBIT;
2230a703e490SVasu Dev 		if (ecmd.speed == SPEED_1000)
22311875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_1GBIT;
2232a703e490SVasu Dev 		if (ecmd.speed == SPEED_10000)
22331875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_10GBIT;
2234a703e490SVasu Dev 
22352f718d64SYi Zou 		return 0;
22362f718d64SYi Zou 	}
22372f718d64SYi Zou 	return -1;
2238a703e490SVasu Dev }
2239a703e490SVasu Dev 
2240a703e490SVasu Dev /**
22415e4f8fe7SRobert Love  * fcoe_link_ok() - Check if the link is OK for a local port
22425e4f8fe7SRobert Love  * @lport: The local port to check link on
22435e4f8fe7SRobert Love  *
22445e4f8fe7SRobert Love  * Returns: 0 if link is UP and OK, -1 if not
22455e4f8fe7SRobert Love  *
22465e4f8fe7SRobert Love  */
22475e4f8fe7SRobert Love int fcoe_link_ok(struct fc_lport *lport)
22485e4f8fe7SRobert Love {
22495e4f8fe7SRobert Love 	struct fcoe_port *port = lport_priv(lport);
22505e4f8fe7SRobert Love 	struct net_device *netdev = port->fcoe->netdev;
22515e4f8fe7SRobert Love 
22525e4f8fe7SRobert Love 	if (netif_oper_up(netdev))
22535e4f8fe7SRobert Love 		return 0;
22545e4f8fe7SRobert Love 	return -1;
22555e4f8fe7SRobert Love }
22565e4f8fe7SRobert Love 
22575e4f8fe7SRobert Love /**
22581875f27eSRobert Love  * fcoe_percpu_clean() - Clear all pending skbs for an local port
22591875f27eSRobert Love  * @lport: The local port whose skbs are to be cleared
2260e7a51997SJoe Eykholt  *
2261e7a51997SJoe Eykholt  * Must be called with fcoe_create_mutex held to single-thread completion.
2262e7a51997SJoe Eykholt  *
2263e7a51997SJoe Eykholt  * This flushes the pending skbs by adding a new skb to each queue and
2264e7a51997SJoe Eykholt  * waiting until they are all freed.  This assures us that not only are
2265e7a51997SJoe Eykholt  * there no packets that will be handled by the lport, but also that any
2266e7a51997SJoe Eykholt  * threads already handling packet have returned.
2267a703e490SVasu Dev  */
22681875f27eSRobert Love void fcoe_percpu_clean(struct fc_lport *lport)
2269a703e490SVasu Dev {
2270a703e490SVasu Dev 	struct fcoe_percpu_s *pp;
2271a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
2272a703e490SVasu Dev 	struct sk_buff_head *list;
2273a703e490SVasu Dev 	struct sk_buff *skb, *next;
2274a703e490SVasu Dev 	struct sk_buff *head;
2275a703e490SVasu Dev 	unsigned int cpu;
2276a703e490SVasu Dev 
2277a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2278a703e490SVasu Dev 		pp = &per_cpu(fcoe_percpu, cpu);
2279a703e490SVasu Dev 		spin_lock_bh(&pp->fcoe_rx_list.lock);
2280a703e490SVasu Dev 		list = &pp->fcoe_rx_list;
2281a703e490SVasu Dev 		head = list->next;
2282a703e490SVasu Dev 		for (skb = head; skb != (struct sk_buff *)list;
2283a703e490SVasu Dev 		     skb = next) {
2284a703e490SVasu Dev 			next = skb->next;
2285a703e490SVasu Dev 			fr = fcoe_dev_from_skb(skb);
22861875f27eSRobert Love 			if (fr->fr_dev == lport) {
2287a703e490SVasu Dev 				__skb_unlink(skb, list);
2288a703e490SVasu Dev 				kfree_skb(skb);
2289a703e490SVasu Dev 			}
2290a703e490SVasu Dev 		}
2291e7a51997SJoe Eykholt 
2292e7a51997SJoe Eykholt 		if (!pp->thread || !cpu_online(cpu)) {
2293a703e490SVasu Dev 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2294e7a51997SJoe Eykholt 			continue;
2295e7a51997SJoe Eykholt 		}
2296e7a51997SJoe Eykholt 
2297e7a51997SJoe Eykholt 		skb = dev_alloc_skb(0);
2298e7a51997SJoe Eykholt 		if (!skb) {
2299e7a51997SJoe Eykholt 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2300e7a51997SJoe Eykholt 			continue;
2301e7a51997SJoe Eykholt 		}
2302e7a51997SJoe Eykholt 		skb->destructor = fcoe_percpu_flush_done;
2303e7a51997SJoe Eykholt 
2304e7a51997SJoe Eykholt 		__skb_queue_tail(&pp->fcoe_rx_list, skb);
2305e7a51997SJoe Eykholt 		if (pp->fcoe_rx_list.qlen == 1)
2306e7a51997SJoe Eykholt 			wake_up_process(pp->thread);
2307e7a51997SJoe Eykholt 		spin_unlock_bh(&pp->fcoe_rx_list.lock);
2308e7a51997SJoe Eykholt 
2309e7a51997SJoe Eykholt 		wait_for_completion(&fcoe_flush_completion);
2310a703e490SVasu Dev 	}
2311a703e490SVasu Dev }
2312a703e490SVasu Dev 
2313a703e490SVasu Dev /**
2314a703e490SVasu Dev  * fcoe_clean_pending_queue() - Dequeue a skb and free it
23151875f27eSRobert Love  * @lport: The local port to dequeue a skb on
2316a703e490SVasu Dev  */
23171875f27eSRobert Love void fcoe_clean_pending_queue(struct fc_lport *lport)
2318a703e490SVasu Dev {
23191875f27eSRobert Love 	struct fcoe_port  *port = lport_priv(lport);
2320a703e490SVasu Dev 	struct sk_buff *skb;
2321a703e490SVasu Dev 
2322014f5c3fSChris Leech 	spin_lock_bh(&port->fcoe_pending_queue.lock);
2323014f5c3fSChris Leech 	while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2324014f5c3fSChris Leech 		spin_unlock_bh(&port->fcoe_pending_queue.lock);
2325a703e490SVasu Dev 		kfree_skb(skb);
2326014f5c3fSChris Leech 		spin_lock_bh(&port->fcoe_pending_queue.lock);
2327a703e490SVasu Dev 	}
2328014f5c3fSChris Leech 	spin_unlock_bh(&port->fcoe_pending_queue.lock);
2329a703e490SVasu Dev }
2330a703e490SVasu Dev 
2331a703e490SVasu Dev /**
23321875f27eSRobert Love  * fcoe_reset() - Reset a local port
23331875f27eSRobert Love  * @shost: The SCSI host associated with the local port to be reset
2334a703e490SVasu Dev  *
23351875f27eSRobert Love  * Returns: Always 0 (return value required by FC transport template)
2336a703e490SVasu Dev  */
2337a703e490SVasu Dev int fcoe_reset(struct Scsi_Host *shost)
2338a703e490SVasu Dev {
2339a703e490SVasu Dev 	struct fc_lport *lport = shost_priv(shost);
2340a703e490SVasu Dev 	fc_lport_reset(lport);
2341a703e490SVasu Dev 	return 0;
2342a703e490SVasu Dev }
2343a703e490SVasu Dev 
2344a703e490SVasu Dev /**
23451875f27eSRobert Love  * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
23461875f27eSRobert Love  * @netdev: The net device used as a key
2347a703e490SVasu Dev  *
23481875f27eSRobert Love  * Locking: Must be called with the RNL mutex held.
23491875f27eSRobert Love  *
23501875f27eSRobert Love  * Returns: NULL or the FCoE interface
2351a703e490SVasu Dev  */
2352014f5c3fSChris Leech static struct fcoe_interface *
23531875f27eSRobert Love fcoe_hostlist_lookup_port(const struct net_device *netdev)
2354a703e490SVasu Dev {
2355014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2356a703e490SVasu Dev 
2357014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
23581875f27eSRobert Love 		if (fcoe->netdev == netdev)
2359014f5c3fSChris Leech 			return fcoe;
2360a703e490SVasu Dev 	}
2361a703e490SVasu Dev 	return NULL;
2362a703e490SVasu Dev }
2363a703e490SVasu Dev 
2364a703e490SVasu Dev /**
23651875f27eSRobert Love  * fcoe_hostlist_lookup() - Find the local port associated with a
23661875f27eSRobert Love  *			    given net device
23671875f27eSRobert Love  * @netdev: The netdevice used as a key
2368a703e490SVasu Dev  *
23691875f27eSRobert Love  * Locking: Must be called with the RTNL mutex held
23701875f27eSRobert Love  *
23711875f27eSRobert Love  * Returns: NULL or the local port
2372a703e490SVasu Dev  */
2373090eb6c4SChris Leech static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2374a703e490SVasu Dev {
2375014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2376a703e490SVasu Dev 
2377014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
23783fe9a0baSChris Leech 	return (fcoe) ? fcoe->ctlr.lp : NULL;
2379a703e490SVasu Dev }
2380a703e490SVasu Dev 
2381a703e490SVasu Dev /**
23821875f27eSRobert Love  * fcoe_hostlist_add() - Add the FCoE interface identified by a local
23831875f27eSRobert Love  *			 port to the hostlist
23841875f27eSRobert Love  * @lport: The local port that identifies the FCoE interface to be added
23851875f27eSRobert Love  *
23861875f27eSRobert Love  * Locking: must be called with the RTNL mutex held
2387a703e490SVasu Dev  *
2388a703e490SVasu Dev  * Returns: 0 for success
2389a703e490SVasu Dev  */
2390090eb6c4SChris Leech static int fcoe_hostlist_add(const struct fc_lport *lport)
2391a703e490SVasu Dev {
2392014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2393014f5c3fSChris Leech 	struct fcoe_port *port;
2394a703e490SVasu Dev 
2395014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2396014f5c3fSChris Leech 	if (!fcoe) {
2397014f5c3fSChris Leech 		port = lport_priv(lport);
2398014f5c3fSChris Leech 		fcoe = port->fcoe;
2399014f5c3fSChris Leech 		list_add_tail(&fcoe->list, &fcoe_hostlist);
2400a703e490SVasu Dev 	}
2401a703e490SVasu Dev 	return 0;
2402a703e490SVasu Dev }
2403a703e490SVasu Dev 
2404a703e490SVasu Dev /**
24051875f27eSRobert Love  * fcoe_init() - Initialize fcoe.ko
2406a703e490SVasu Dev  *
24071875f27eSRobert Love  * Returns: 0 on success, or a negative value on failure
2408a703e490SVasu Dev  */
2409a703e490SVasu Dev static int __init fcoe_init(void)
2410a703e490SVasu Dev {
24111875f27eSRobert Love 	struct fcoe_percpu_s *p;
2412a703e490SVasu Dev 	unsigned int cpu;
2413a703e490SVasu Dev 	int rc = 0;
2414a703e490SVasu Dev 
2415dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2416dfc1d0feSChris Leech 
2417a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2418a703e490SVasu Dev 		p = &per_cpu(fcoe_percpu, cpu);
2419a703e490SVasu Dev 		skb_queue_head_init(&p->fcoe_rx_list);
2420a703e490SVasu Dev 	}
2421a703e490SVasu Dev 
2422a703e490SVasu Dev 	for_each_online_cpu(cpu)
2423a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
2424a703e490SVasu Dev 
2425a703e490SVasu Dev 	/* Initialize per CPU interrupt thread */
2426a703e490SVasu Dev 	rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2427a703e490SVasu Dev 	if (rc)
2428a703e490SVasu Dev 		goto out_free;
2429a703e490SVasu Dev 
2430a703e490SVasu Dev 	/* Setup link change notification */
2431a703e490SVasu Dev 	fcoe_dev_setup();
2432a703e490SVasu Dev 
24335892c32fSChris Leech 	rc = fcoe_if_init();
24345892c32fSChris Leech 	if (rc)
24355892c32fSChris Leech 		goto out_free;
2436a703e490SVasu Dev 
2437dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2438a703e490SVasu Dev 	return 0;
2439a703e490SVasu Dev 
2440a703e490SVasu Dev out_free:
2441a703e490SVasu Dev 	for_each_online_cpu(cpu) {
2442a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2443a703e490SVasu Dev 	}
2444dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2445a703e490SVasu Dev 	return rc;
2446a703e490SVasu Dev }
2447a703e490SVasu Dev module_init(fcoe_init);
2448a703e490SVasu Dev 
2449a703e490SVasu Dev /**
24501875f27eSRobert Love  * fcoe_exit() - Clean up fcoe.ko
2451a703e490SVasu Dev  *
24521875f27eSRobert Love  * Returns: 0 on success or a  negative value on failure
2453a703e490SVasu Dev  */
2454a703e490SVasu Dev static void __exit fcoe_exit(void)
2455a703e490SVasu Dev {
2456014f5c3fSChris Leech 	struct fcoe_interface *fcoe, *tmp;
24572e70e241SChris Leech 	struct fcoe_port *port;
24581875f27eSRobert Love 	unsigned int cpu;
2459a703e490SVasu Dev 
2460dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2461dfc1d0feSChris Leech 
2462a703e490SVasu Dev 	fcoe_dev_cleanup();
2463a703e490SVasu Dev 
2464a703e490SVasu Dev 	/* releases the associated fcoe hosts */
2465090eb6c4SChris Leech 	rtnl_lock();
2466090eb6c4SChris Leech 	list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2467c863df33SChris Leech 		list_del(&fcoe->list);
24682e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
24692e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
24702e70e241SChris Leech 		schedule_work(&port->destroy_work);
2471c863df33SChris Leech 	}
2472090eb6c4SChris Leech 	rtnl_unlock();
2473a703e490SVasu Dev 
2474a703e490SVasu Dev 	unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2475a703e490SVasu Dev 
2476014f5c3fSChris Leech 	for_each_online_cpu(cpu)
2477a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2478a703e490SVasu Dev 
2479dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
24802e70e241SChris Leech 
24812e70e241SChris Leech 	/* flush any asyncronous interface destroys,
24822e70e241SChris Leech 	 * this should happen after the netdev notifier is unregistered */
24832e70e241SChris Leech 	flush_scheduled_work();
24849a05753bSChris Leech 	/* That will flush out all the N_Ports on the hostlist, but now we
24859a05753bSChris Leech 	 * may have NPIV VN_Ports scheduled for destruction */
24869a05753bSChris Leech 	flush_scheduled_work();
24872e70e241SChris Leech 
24882e70e241SChris Leech 	/* detach from scsi transport
24892e70e241SChris Leech 	 * must happen after all destroys are done, therefor after the flush */
24902e70e241SChris Leech 	fcoe_if_exit();
2491a703e490SVasu Dev }
2492a703e490SVasu Dev module_exit(fcoe_exit);
249311b56188SChris Leech 
249411b56188SChris Leech /**
249511b56188SChris Leech  * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
249611b56188SChris Leech  * @seq: active sequence in the FLOGI or FDISC exchange
249711b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
249811b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
249911b56188SChris Leech  *
250065155b37SUwe Kleine-König  * This handles MAC address management for FCoE, then passes control on to
250111b56188SChris Leech  * the libfc FLOGI response handler.
250211b56188SChris Leech  */
250311b56188SChris Leech static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
250411b56188SChris Leech {
250511b56188SChris Leech 	struct fcoe_ctlr *fip = arg;
250611b56188SChris Leech 	struct fc_exch *exch = fc_seq_exch(seq);
250711b56188SChris Leech 	struct fc_lport *lport = exch->lp;
250811b56188SChris Leech 	u8 *mac;
250911b56188SChris Leech 
251011b56188SChris Leech 	if (IS_ERR(fp))
251111b56188SChris Leech 		goto done;
251211b56188SChris Leech 
251311b56188SChris Leech 	mac = fr_cb(fp)->granted_mac;
251411b56188SChris Leech 	if (is_zero_ether_addr(mac)) {
251511b56188SChris Leech 		/* pre-FIP */
2516386309ceSJoe Eykholt 		if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
251711b56188SChris Leech 			fc_frame_free(fp);
251811b56188SChris Leech 			return;
251911b56188SChris Leech 		}
252011b56188SChris Leech 	}
2521386309ceSJoe Eykholt 	fcoe_update_src_mac(lport, mac);
252211b56188SChris Leech done:
252311b56188SChris Leech 	fc_lport_flogi_resp(seq, fp, lport);
252411b56188SChris Leech }
252511b56188SChris Leech 
252611b56188SChris Leech /**
252711b56188SChris Leech  * fcoe_logo_resp() - FCoE specific LOGO response handler
252811b56188SChris Leech  * @seq: active sequence in the LOGO exchange
252911b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
253011b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
253111b56188SChris Leech  *
253265155b37SUwe Kleine-König  * This handles MAC address management for FCoE, then passes control on to
253311b56188SChris Leech  * the libfc LOGO response handler.
253411b56188SChris Leech  */
253511b56188SChris Leech static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
253611b56188SChris Leech {
2537386309ceSJoe Eykholt 	struct fc_lport *lport = arg;
253811b56188SChris Leech 	static u8 zero_mac[ETH_ALEN] = { 0 };
253911b56188SChris Leech 
254011b56188SChris Leech 	if (!IS_ERR(fp))
2541386309ceSJoe Eykholt 		fcoe_update_src_mac(lport, zero_mac);
254211b56188SChris Leech 	fc_lport_logo_resp(seq, fp, lport);
254311b56188SChris Leech }
254411b56188SChris Leech 
254511b56188SChris Leech /**
254611b56188SChris Leech  * fcoe_elsct_send - FCoE specific ELS handler
254711b56188SChris Leech  *
254811b56188SChris Leech  * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
254911b56188SChris Leech  * using FCoE specific response handlers and passing the FIP controller as
255011b56188SChris Leech  * the argument (the lport is still available from the exchange).
255111b56188SChris Leech  *
255211b56188SChris Leech  * Most of the work here is just handed off to the libfc routine.
255311b56188SChris Leech  */
25541875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
25551875f27eSRobert Love 				      struct fc_frame *fp, unsigned int op,
25561875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
25571875f27eSRobert Love 						   struct fc_frame *,
25581875f27eSRobert Love 						   void *),
255911b56188SChris Leech 				      void *arg, u32 timeout)
256011b56188SChris Leech {
256111b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
256211b56188SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
256311b56188SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
256411b56188SChris Leech 	struct fc_frame_header *fh = fc_frame_header_get(fp);
256511b56188SChris Leech 
256611b56188SChris Leech 	switch (op) {
256711b56188SChris Leech 	case ELS_FLOGI:
256811b56188SChris Leech 	case ELS_FDISC:
2569e10f8c66SJoe Eykholt 		if (lport->point_to_multipoint)
2570e10f8c66SJoe Eykholt 			break;
257111b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
257211b56188SChris Leech 				     fip, timeout);
257311b56188SChris Leech 	case ELS_LOGO:
257411b56188SChris Leech 		/* only hook onto fabric logouts, not port logouts */
257511b56188SChris Leech 		if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
257611b56188SChris Leech 			break;
257711b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2578386309ceSJoe Eykholt 				     lport, timeout);
257911b56188SChris Leech 	}
258011b56188SChris Leech 	return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
258111b56188SChris Leech }
258211b56188SChris Leech 
25839a05753bSChris Leech /**
25849a05753bSChris Leech  * fcoe_vport_create() - create an fc_host/scsi_host for a vport
25859a05753bSChris Leech  * @vport: fc_vport object to create a new fc_host for
25869a05753bSChris Leech  * @disabled: start the new fc_host in a disabled state by default?
25879a05753bSChris Leech  *
25889a05753bSChris Leech  * Returns: 0 for success
25899a05753bSChris Leech  */
25909a05753bSChris Leech static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
25919a05753bSChris Leech {
25929a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
25939a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
25949a05753bSChris Leech 	struct fcoe_port *port = lport_priv(n_port);
25959a05753bSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
25969a05753bSChris Leech 	struct net_device *netdev = fcoe->netdev;
25979a05753bSChris Leech 	struct fc_lport *vn_port;
25989a05753bSChris Leech 
25999a05753bSChris Leech 	mutex_lock(&fcoe_config_mutex);
26009a05753bSChris Leech 	vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
26019a05753bSChris Leech 	mutex_unlock(&fcoe_config_mutex);
26029a05753bSChris Leech 
26039a05753bSChris Leech 	if (IS_ERR(vn_port)) {
26049a05753bSChris Leech 		printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
26059a05753bSChris Leech 		       netdev->name);
26069a05753bSChris Leech 		return -EIO;
26079a05753bSChris Leech 	}
26089a05753bSChris Leech 
26099a05753bSChris Leech 	if (disabled) {
26109a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
26119a05753bSChris Leech 	} else {
26129a05753bSChris Leech 		vn_port->boot_time = jiffies;
26139a05753bSChris Leech 		fc_fabric_login(vn_port);
26149a05753bSChris Leech 		fc_vport_setlink(vn_port);
26159a05753bSChris Leech 	}
26169a05753bSChris Leech 	return 0;
26179a05753bSChris Leech }
26189a05753bSChris Leech 
26199a05753bSChris Leech /**
26209a05753bSChris Leech  * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
26219a05753bSChris Leech  * @vport: fc_vport object that is being destroyed
26229a05753bSChris Leech  *
26239a05753bSChris Leech  * Returns: 0 for success
26249a05753bSChris Leech  */
26259a05753bSChris Leech static int fcoe_vport_destroy(struct fc_vport *vport)
26269a05753bSChris Leech {
26279a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
26289a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
26299a05753bSChris Leech 	struct fc_lport *vn_port = vport->dd_data;
26309a05753bSChris Leech 	struct fcoe_port *port = lport_priv(vn_port);
26319a05753bSChris Leech 
26329a05753bSChris Leech 	mutex_lock(&n_port->lp_mutex);
26339a05753bSChris Leech 	list_del(&vn_port->list);
26349a05753bSChris Leech 	mutex_unlock(&n_port->lp_mutex);
26359a05753bSChris Leech 	schedule_work(&port->destroy_work);
26369a05753bSChris Leech 	return 0;
26379a05753bSChris Leech }
26389a05753bSChris Leech 
26399a05753bSChris Leech /**
26409a05753bSChris Leech  * fcoe_vport_disable() - change vport state
26419a05753bSChris Leech  * @vport: vport to bring online/offline
26429a05753bSChris Leech  * @disable: should the vport be disabled?
26439a05753bSChris Leech  */
26449a05753bSChris Leech static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
26459a05753bSChris Leech {
26469a05753bSChris Leech 	struct fc_lport *lport = vport->dd_data;
26479a05753bSChris Leech 
26489a05753bSChris Leech 	if (disable) {
26499a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
26509a05753bSChris Leech 		fc_fabric_logoff(lport);
26519a05753bSChris Leech 	} else {
26529a05753bSChris Leech 		lport->boot_time = jiffies;
26539a05753bSChris Leech 		fc_fabric_login(lport);
26549a05753bSChris Leech 		fc_vport_setlink(lport);
26559a05753bSChris Leech 	}
26569a05753bSChris Leech 
26579a05753bSChris Leech 	return 0;
26589a05753bSChris Leech }
26599a05753bSChris Leech 
2660dc8596d3SChris Leech /**
2661dc8596d3SChris Leech  * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2662dc8596d3SChris Leech  * @vport: fc_vport with a new symbolic name string
2663dc8596d3SChris Leech  *
2664dc8596d3SChris Leech  * After generating a new symbolic name string, a new RSPN_ID request is
2665dc8596d3SChris Leech  * sent to the name server.  There is no response handler, so if it fails
2666dc8596d3SChris Leech  * for some reason it will not be retried.
2667dc8596d3SChris Leech  */
2668dc8596d3SChris Leech static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2669dc8596d3SChris Leech {
2670dc8596d3SChris Leech 	struct fc_lport *lport = vport->dd_data;
2671dc8596d3SChris Leech 	struct fc_frame *fp;
2672dc8596d3SChris Leech 	size_t len;
2673dc8596d3SChris Leech 
2674dc8596d3SChris Leech 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2675dc8596d3SChris Leech 		 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2676dc8596d3SChris Leech 		 fcoe_netdev(lport)->name, vport->symbolic_name);
2677dc8596d3SChris Leech 
2678dc8596d3SChris Leech 	if (lport->state != LPORT_ST_READY)
2679dc8596d3SChris Leech 		return;
2680dc8596d3SChris Leech 
2681dc8596d3SChris Leech 	len = strnlen(fc_host_symbolic_name(lport->host), 255);
2682dc8596d3SChris Leech 	fp = fc_frame_alloc(lport,
2683dc8596d3SChris Leech 			    sizeof(struct fc_ct_hdr) +
2684dc8596d3SChris Leech 			    sizeof(struct fc_ns_rspn) + len);
2685dc8596d3SChris Leech 	if (!fp)
2686dc8596d3SChris Leech 		return;
2687dc8596d3SChris Leech 	lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2688b94f8951SJoe Eykholt 			     NULL, NULL, 3 * lport->r_a_tov);
2689dc8596d3SChris Leech }
2690b84056bfSYi Zou 
2691b84056bfSYi Zou /**
2692b84056bfSYi Zou  * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2693b84056bfSYi Zou  * @lport: the local port
2694b84056bfSYi Zou  * @fc_lesb: the link error status block
2695b84056bfSYi Zou  */
2696b84056bfSYi Zou static void fcoe_get_lesb(struct fc_lport *lport,
2697b84056bfSYi Zou 			 struct fc_els_lesb *fc_lesb)
2698b84056bfSYi Zou {
2699b84056bfSYi Zou 	unsigned int cpu;
2700b84056bfSYi Zou 	u32 lfc, vlfc, mdac;
2701b84056bfSYi Zou 	struct fcoe_dev_stats *devst;
2702b84056bfSYi Zou 	struct fcoe_fc_els_lesb *lesb;
270328172739SEric Dumazet 	struct rtnl_link_stats64 temp;
2704b84056bfSYi Zou 	struct net_device *netdev = fcoe_netdev(lport);
2705b84056bfSYi Zou 
2706b84056bfSYi Zou 	lfc = 0;
2707b84056bfSYi Zou 	vlfc = 0;
2708b84056bfSYi Zou 	mdac = 0;
2709b84056bfSYi Zou 	lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2710b84056bfSYi Zou 	memset(lesb, 0, sizeof(*lesb));
2711b84056bfSYi Zou 	for_each_possible_cpu(cpu) {
2712b84056bfSYi Zou 		devst = per_cpu_ptr(lport->dev_stats, cpu);
2713b84056bfSYi Zou 		lfc += devst->LinkFailureCount;
2714b84056bfSYi Zou 		vlfc += devst->VLinkFailureCount;
2715b84056bfSYi Zou 		mdac += devst->MissDiscAdvCount;
2716b84056bfSYi Zou 	}
2717b84056bfSYi Zou 	lesb->lesb_link_fail = htonl(lfc);
2718b84056bfSYi Zou 	lesb->lesb_vlink_fail = htonl(vlfc);
2719b84056bfSYi Zou 	lesb->lesb_miss_fka = htonl(mdac);
272028172739SEric Dumazet 	lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2721b84056bfSYi Zou }
27227d65b0dfSJoe Eykholt 
27237d65b0dfSJoe Eykholt /**
27247d65b0dfSJoe Eykholt  * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
27257d65b0dfSJoe Eykholt  * @lport: the local port
27267d65b0dfSJoe Eykholt  * @port_id: the port ID
27277d65b0dfSJoe Eykholt  * @fp: the received frame, if any, that caused the port_id to be set.
27287d65b0dfSJoe Eykholt  *
27297d65b0dfSJoe Eykholt  * This routine handles the case where we received a FLOGI and are
27307d65b0dfSJoe Eykholt  * entering point-to-point mode.  We need to call fcoe_ctlr_recv_flogi()
27317d65b0dfSJoe Eykholt  * so it can set the non-mapped mode and gateway address.
27327d65b0dfSJoe Eykholt  *
27337d65b0dfSJoe Eykholt  * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
27347d65b0dfSJoe Eykholt  */
27357d65b0dfSJoe Eykholt static void fcoe_set_port_id(struct fc_lport *lport,
27367d65b0dfSJoe Eykholt 			     u32 port_id, struct fc_frame *fp)
27377d65b0dfSJoe Eykholt {
27387d65b0dfSJoe Eykholt 	struct fcoe_port *port = lport_priv(lport);
27397d65b0dfSJoe Eykholt 	struct fcoe_interface *fcoe = port->fcoe;
27407d65b0dfSJoe Eykholt 
27417d65b0dfSJoe Eykholt 	if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
27427d65b0dfSJoe Eykholt 		fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
27437d65b0dfSJoe Eykholt }
2744