xref: /openbmc/linux/drivers/scsi/fcoe/fcoe.c (revision 165c68d5)
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>
342ca32b48STejun Heo #include <linux/workqueue.h>
35a703e490SVasu Dev #include <scsi/scsi_tcq.h>
36a703e490SVasu Dev #include <scsi/scsicam.h>
37a703e490SVasu Dev #include <scsi/scsi_transport.h>
38a703e490SVasu Dev #include <scsi/scsi_transport_fc.h>
39a703e490SVasu Dev #include <net/rtnetlink.h>
40a703e490SVasu Dev 
41a703e490SVasu Dev #include <scsi/fc/fc_encaps.h>
4297c8389dSJoe Eykholt #include <scsi/fc/fc_fip.h>
43a703e490SVasu Dev 
44a703e490SVasu Dev #include <scsi/libfc.h>
45a703e490SVasu Dev #include <scsi/fc_frame.h>
46a703e490SVasu Dev #include <scsi/libfcoe.h>
47a703e490SVasu Dev 
48fdd78027SVasu Dev #include "fcoe.h"
49fdd78027SVasu Dev 
50a703e490SVasu Dev MODULE_AUTHOR("Open-FCoE.org");
51a703e490SVasu Dev MODULE_DESCRIPTION("FCoE");
529b34ecffSVasu Dev MODULE_LICENSE("GPL v2");
53a703e490SVasu Dev 
5405cc7390SYi Zou /* Performance tuning parameters for fcoe */
5505cc7390SYi Zou static unsigned int fcoe_ddp_min;
5605cc7390SYi Zou module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
5705cc7390SYi Zou MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for "	\
5805cc7390SYi Zou 		 "Direct Data Placement (DDP).");
5905cc7390SYi Zou 
60dfc1d0feSChris Leech DEFINE_MUTEX(fcoe_config_mutex);
61dfc1d0feSChris Leech 
622ca32b48STejun Heo static struct workqueue_struct *fcoe_wq;
632ca32b48STejun Heo 
64e7a51997SJoe Eykholt /* fcoe_percpu_clean completion.  Waiter protected by fcoe_create_mutex */
65e7a51997SJoe Eykholt static DECLARE_COMPLETION(fcoe_flush_completion);
66e7a51997SJoe Eykholt 
67a703e490SVasu Dev /* fcoe host list */
68090eb6c4SChris Leech /* must only by accessed under the RTNL mutex */
69a703e490SVasu Dev LIST_HEAD(fcoe_hostlist);
70a703e490SVasu Dev DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
71a703e490SVasu Dev 
72dd3fd72eSChris Leech /* Function Prototypes */
731875f27eSRobert Love static int fcoe_reset(struct Scsi_Host *);
74fdd78027SVasu Dev static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
75fdd78027SVasu Dev static int fcoe_rcv(struct sk_buff *, struct net_device *,
76fdd78027SVasu Dev 		    struct packet_type *, struct net_device *);
771875f27eSRobert Love static int fcoe_percpu_receive_thread(void *);
781875f27eSRobert Love static void fcoe_percpu_clean(struct fc_lport *);
795e4f8fe7SRobert Love static int fcoe_link_speed_update(struct fc_lport *);
801875f27eSRobert Love static int fcoe_link_ok(struct fc_lport *);
81fdd78027SVasu Dev 
82fdd78027SVasu Dev static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
83fdd78027SVasu Dev static int fcoe_hostlist_add(const struct fc_lport *);
84fdd78027SVasu Dev 
85a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86a703e490SVasu Dev static void fcoe_dev_setup(void);
87a703e490SVasu Dev static void fcoe_dev_cleanup(void);
881875f27eSRobert Love static struct fcoe_interface
891875f27eSRobert Love *fcoe_hostlist_lookup_port(const struct net_device *);
90a703e490SVasu Dev 
911875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
921875f27eSRobert Love 			 struct packet_type *, struct net_device *);
931875f27eSRobert Love 
941875f27eSRobert Love static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
951875f27eSRobert Love static void fcoe_update_src_mac(struct fc_lport *, u8 *);
961875f27eSRobert Love static u8 *fcoe_get_src_mac(struct fc_lport *);
971875f27eSRobert Love static void fcoe_destroy_work(struct work_struct *);
981875f27eSRobert Love 
991875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
1001875f27eSRobert Love 			  unsigned int);
1011875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *, u16);
10271f89491SYi Zou static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
10371f89491SYi Zou 			   unsigned int);
1041875f27eSRobert Love static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
1051875f27eSRobert Love 
10678a58246SYi Zou static bool fcoe_match(struct net_device *netdev);
10778a58246SYi Zou static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
10878a58246SYi Zou static int fcoe_destroy(struct net_device *netdev);
10978a58246SYi Zou static int fcoe_enable(struct net_device *netdev);
11078a58246SYi Zou static int fcoe_disable(struct net_device *netdev);
1111875f27eSRobert Love 
1121875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
1131875f27eSRobert Love 				      u32 did, struct fc_frame *,
1141875f27eSRobert Love 				      unsigned int op,
1151875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
1161875f27eSRobert Love 						   struct fc_frame *,
1171875f27eSRobert Love 						   void *),
1181875f27eSRobert Love 				      void *, u32 timeout);
119859b7b64SChris Leech static void fcoe_recv_frame(struct sk_buff *skb);
1201875f27eSRobert Love 
121b84056bfSYi Zou static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
122b84056bfSYi Zou 
1231875f27eSRobert Love /* notification function for packets from net device */
124a703e490SVasu Dev static struct notifier_block fcoe_notifier = {
125a703e490SVasu Dev 	.notifier_call = fcoe_device_notification,
126a703e490SVasu Dev };
127a703e490SVasu Dev 
1281875f27eSRobert Love /* notification function for CPU hotplug events */
1291875f27eSRobert Love static struct notifier_block fcoe_cpu_notifier = {
1301875f27eSRobert Love 	.notifier_call = fcoe_cpu_callback,
1311875f27eSRobert Love };
1321875f27eSRobert Love 
1338ca86f84SYi Zou static struct scsi_transport_template *fcoe_nport_scsi_transport;
1348ca86f84SYi Zou static struct scsi_transport_template *fcoe_vport_scsi_transport;
135a703e490SVasu Dev 
1361875f27eSRobert Love static int fcoe_vport_destroy(struct fc_vport *);
1371875f27eSRobert Love static int fcoe_vport_create(struct fc_vport *, bool disabled);
1381875f27eSRobert Love static int fcoe_vport_disable(struct fc_vport *, bool disable);
1391875f27eSRobert Love static void fcoe_set_vport_symbolic_name(struct fc_vport *);
1407d65b0dfSJoe Eykholt static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
141bdf25218SNeerav Parikh static int fcoe_validate_vport_create(struct fc_vport *);
1421875f27eSRobert Love 
1431875f27eSRobert Love static struct libfc_function_template fcoe_libfc_fcn_templ = {
1441875f27eSRobert Love 	.frame_send = fcoe_xmit,
1451875f27eSRobert Love 	.ddp_setup = fcoe_ddp_setup,
1461875f27eSRobert Love 	.ddp_done = fcoe_ddp_done,
14771f89491SYi Zou 	.ddp_target = fcoe_ddp_target,
1481875f27eSRobert Love 	.elsct_send = fcoe_elsct_send,
149b84056bfSYi Zou 	.get_lesb = fcoe_get_lesb,
1507d65b0dfSJoe Eykholt 	.lport_set_port_id = fcoe_set_port_id,
1511875f27eSRobert Love };
1529a05753bSChris Leech 
1538ca86f84SYi Zou struct fc_function_template fcoe_nport_fc_functions = {
154a703e490SVasu Dev 	.show_host_node_name = 1,
155a703e490SVasu Dev 	.show_host_port_name = 1,
156a703e490SVasu Dev 	.show_host_supported_classes = 1,
157a703e490SVasu Dev 	.show_host_supported_fc4s = 1,
158a703e490SVasu Dev 	.show_host_active_fc4s = 1,
159a703e490SVasu Dev 	.show_host_maxframe_size = 1,
160a703e490SVasu Dev 
161a703e490SVasu Dev 	.show_host_port_id = 1,
162a703e490SVasu Dev 	.show_host_supported_speeds = 1,
163a703e490SVasu Dev 	.get_host_speed = fc_get_host_speed,
164a703e490SVasu Dev 	.show_host_speed = 1,
165a703e490SVasu Dev 	.show_host_port_type = 1,
166a703e490SVasu Dev 	.get_host_port_state = fc_get_host_port_state,
167a703e490SVasu Dev 	.show_host_port_state = 1,
168a703e490SVasu Dev 	.show_host_symbolic_name = 1,
169a703e490SVasu Dev 
170a703e490SVasu Dev 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
171a703e490SVasu Dev 	.show_rport_maxframe_size = 1,
172a703e490SVasu Dev 	.show_rport_supported_classes = 1,
173a703e490SVasu Dev 
174a703e490SVasu Dev 	.show_host_fabric_name = 1,
175a703e490SVasu Dev 	.show_starget_node_name = 1,
176a703e490SVasu Dev 	.show_starget_port_name = 1,
177a703e490SVasu Dev 	.show_starget_port_id = 1,
178a703e490SVasu Dev 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
179a703e490SVasu Dev 	.show_rport_dev_loss_tmo = 1,
180a703e490SVasu Dev 	.get_fc_host_stats = fc_get_host_stats,
181a703e490SVasu Dev 	.issue_fc_host_lip = fcoe_reset,
182a703e490SVasu Dev 
183a703e490SVasu Dev 	.terminate_rport_io = fc_rport_terminate_io,
1849a05753bSChris Leech 
1859a05753bSChris Leech 	.vport_create = fcoe_vport_create,
1869a05753bSChris Leech 	.vport_delete = fcoe_vport_destroy,
1879a05753bSChris Leech 	.vport_disable = fcoe_vport_disable,
188dc8596d3SChris Leech 	.set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
189a51ab396SSteve Ma 
190a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
191a703e490SVasu Dev };
192a703e490SVasu Dev 
1938ca86f84SYi Zou struct fc_function_template fcoe_vport_fc_functions = {
194e9084bb8SChris Leech 	.show_host_node_name = 1,
195e9084bb8SChris Leech 	.show_host_port_name = 1,
196e9084bb8SChris Leech 	.show_host_supported_classes = 1,
197e9084bb8SChris Leech 	.show_host_supported_fc4s = 1,
198e9084bb8SChris Leech 	.show_host_active_fc4s = 1,
199e9084bb8SChris Leech 	.show_host_maxframe_size = 1,
200e9084bb8SChris Leech 
201e9084bb8SChris Leech 	.show_host_port_id = 1,
202e9084bb8SChris Leech 	.show_host_supported_speeds = 1,
203e9084bb8SChris Leech 	.get_host_speed = fc_get_host_speed,
204e9084bb8SChris Leech 	.show_host_speed = 1,
205e9084bb8SChris Leech 	.show_host_port_type = 1,
206e9084bb8SChris Leech 	.get_host_port_state = fc_get_host_port_state,
207e9084bb8SChris Leech 	.show_host_port_state = 1,
208e9084bb8SChris Leech 	.show_host_symbolic_name = 1,
209e9084bb8SChris Leech 
210e9084bb8SChris Leech 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
211e9084bb8SChris Leech 	.show_rport_maxframe_size = 1,
212e9084bb8SChris Leech 	.show_rport_supported_classes = 1,
213e9084bb8SChris Leech 
214e9084bb8SChris Leech 	.show_host_fabric_name = 1,
215e9084bb8SChris Leech 	.show_starget_node_name = 1,
216e9084bb8SChris Leech 	.show_starget_port_name = 1,
217e9084bb8SChris Leech 	.show_starget_port_id = 1,
218e9084bb8SChris Leech 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
219e9084bb8SChris Leech 	.show_rport_dev_loss_tmo = 1,
220e9084bb8SChris Leech 	.get_fc_host_stats = fc_get_host_stats,
221e9084bb8SChris Leech 	.issue_fc_host_lip = fcoe_reset,
222e9084bb8SChris Leech 
223e9084bb8SChris Leech 	.terminate_rport_io = fc_rport_terminate_io,
224a51ab396SSteve Ma 
225a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
226e9084bb8SChris Leech };
227e9084bb8SChris Leech 
228a703e490SVasu Dev static struct scsi_host_template fcoe_shost_template = {
229a703e490SVasu Dev 	.module = THIS_MODULE,
230a703e490SVasu Dev 	.name = "FCoE Driver",
231a703e490SVasu Dev 	.proc_name = FCOE_NAME,
232a703e490SVasu Dev 	.queuecommand = fc_queuecommand,
233a703e490SVasu Dev 	.eh_abort_handler = fc_eh_abort,
234a703e490SVasu Dev 	.eh_device_reset_handler = fc_eh_device_reset,
235a703e490SVasu Dev 	.eh_host_reset_handler = fc_eh_host_reset,
236a703e490SVasu Dev 	.slave_alloc = fc_slave_alloc,
237a703e490SVasu Dev 	.change_queue_depth = fc_change_queue_depth,
238a703e490SVasu Dev 	.change_queue_type = fc_change_queue_type,
239a703e490SVasu Dev 	.this_id = -1,
24014caf44cSVasu Dev 	.cmd_per_lun = 3,
241a703e490SVasu Dev 	.can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
242a703e490SVasu Dev 	.use_clustering = ENABLE_CLUSTERING,
243a703e490SVasu Dev 	.sg_tablesize = SG_ALL,
244a703e490SVasu Dev 	.max_sectors = 0xffff,
245a703e490SVasu Dev };
246a703e490SVasu Dev 
24754b649f8SChris Leech /**
2481875f27eSRobert Love  * fcoe_interface_setup() - Setup a FCoE interface
2491875f27eSRobert Love  * @fcoe:   The new FCoE interface
2501875f27eSRobert Love  * @netdev: The net device that the fcoe interface is on
25154b649f8SChris Leech  *
25254b649f8SChris Leech  * Returns : 0 for success
2532e70e241SChris Leech  * Locking: must be called with the RTNL mutex held
25454b649f8SChris Leech  */
25554b649f8SChris Leech static int fcoe_interface_setup(struct fcoe_interface *fcoe,
25654b649f8SChris Leech 				struct net_device *netdev)
25754b649f8SChris Leech {
25854b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
25954b649f8SChris Leech 	struct netdev_hw_addr *ha;
2605bab87e6SYi Zou 	struct net_device *real_dev;
26154b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
262b7a727f1SYi Zou 	const struct net_device_ops *ops;
26354b649f8SChris Leech 
26454b649f8SChris Leech 	fcoe->netdev = netdev;
26554b649f8SChris Leech 
266b7a727f1SYi Zou 	/* Let LLD initialize for FCoE */
267b7a727f1SYi Zou 	ops = netdev->netdev_ops;
268b7a727f1SYi Zou 	if (ops->ndo_fcoe_enable) {
269b7a727f1SYi Zou 		if (ops->ndo_fcoe_enable(netdev))
270b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
271b7a727f1SYi Zou 					" specific feature for LLD.\n");
272b7a727f1SYi Zou 	}
273b7a727f1SYi Zou 
27454b649f8SChris Leech 	/* Do not support for bonding device */
275cc8bdf06SJiri Pirko 	if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
27659d92516Sjohn fastabend 		FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
27754b649f8SChris Leech 		return -EOPNOTSUPP;
27854b649f8SChris Leech 	}
27954b649f8SChris Leech 
28054b649f8SChris Leech 	/* look for SAN MAC address, if multiple SAN MACs exist, only
28154b649f8SChris Leech 	 * use the first one for SPMA */
2825bab87e6SYi Zou 	real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
2835bab87e6SYi Zou 		vlan_dev_real_dev(netdev) : netdev;
28454b649f8SChris Leech 	rcu_read_lock();
2855bab87e6SYi Zou 	for_each_dev_addr(real_dev, ha) {
28654b649f8SChris Leech 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
287bf361707SYi Zou 		    (is_valid_ether_addr(ha->addr))) {
28854b649f8SChris Leech 			memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
28954b649f8SChris Leech 			fip->spma = 1;
29054b649f8SChris Leech 			break;
29154b649f8SChris Leech 		}
29254b649f8SChris Leech 	}
29354b649f8SChris Leech 	rcu_read_unlock();
29454b649f8SChris Leech 
29554b649f8SChris Leech 	/* setup Source Mac Address */
29654b649f8SChris Leech 	if (!fip->spma)
29754b649f8SChris Leech 		memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
29854b649f8SChris Leech 
29954b649f8SChris Leech 	/*
30054b649f8SChris Leech 	 * Add FCoE MAC address as second unicast MAC address
30154b649f8SChris Leech 	 * or enter promiscuous mode if not capable of listening
30254b649f8SChris Leech 	 * for multiple unicast MACs.
30354b649f8SChris Leech 	 */
30454b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
305a748ee24SJiri Pirko 	dev_uc_add(netdev, flogi_maddr);
30654b649f8SChris Leech 	if (fip->spma)
307a748ee24SJiri Pirko 		dev_uc_add(netdev, fip->ctl_src_addr);
308e10f8c66SJoe Eykholt 	if (fip->mode == FIP_MODE_VN2VN) {
309e10f8c66SJoe Eykholt 		dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
310e10f8c66SJoe Eykholt 		dev_mc_add(netdev, FIP_ALL_P2P_MACS);
311e10f8c66SJoe Eykholt 	} else
31222bedad3SJiri Pirko 		dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
31354b649f8SChris Leech 
31454b649f8SChris Leech 	/*
31554b649f8SChris Leech 	 * setup the receive function from ethernet driver
31654b649f8SChris Leech 	 * on the ethertype for the given device
31754b649f8SChris Leech 	 */
31854b649f8SChris Leech 	fcoe->fcoe_packet_type.func = fcoe_rcv;
31954b649f8SChris Leech 	fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
32054b649f8SChris Leech 	fcoe->fcoe_packet_type.dev = netdev;
32154b649f8SChris Leech 	dev_add_pack(&fcoe->fcoe_packet_type);
32254b649f8SChris Leech 
32354b649f8SChris Leech 	fcoe->fip_packet_type.func = fcoe_fip_recv;
32454b649f8SChris Leech 	fcoe->fip_packet_type.type = htons(ETH_P_FIP);
32554b649f8SChris Leech 	fcoe->fip_packet_type.dev = netdev;
32654b649f8SChris Leech 	dev_add_pack(&fcoe->fip_packet_type);
32754b649f8SChris Leech 
32854b649f8SChris Leech 	return 0;
32954b649f8SChris Leech }
33054b649f8SChris Leech 
331a703e490SVasu Dev /**
3321875f27eSRobert Love  * fcoe_interface_create() - Create a FCoE interface on a net device
3331875f27eSRobert Love  * @netdev: The net device to create the FCoE interface on
3341dd454d9SJoe Eykholt  * @fip_mode: The mode to use for FIP
335030f4e00SChris Leech  *
336030f4e00SChris Leech  * Returns: pointer to a struct fcoe_interface or NULL on error
337030f4e00SChris Leech  */
3381dd454d9SJoe Eykholt static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
3391dd454d9SJoe Eykholt 						    enum fip_state fip_mode)
340030f4e00SChris Leech {
341030f4e00SChris Leech 	struct fcoe_interface *fcoe;
34259d92516Sjohn fastabend 	int err;
343030f4e00SChris Leech 
3447287fb91SRobert Love 	if (!try_module_get(THIS_MODULE)) {
3457287fb91SRobert Love 		FCOE_NETDEV_DBG(netdev,
3467287fb91SRobert Love 				"Could not get a reference to the module\n");
3477287fb91SRobert Love 		fcoe = ERR_PTR(-EBUSY);
3487287fb91SRobert Love 		goto out;
3497287fb91SRobert Love 	}
3507287fb91SRobert Love 
351030f4e00SChris Leech 	fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
352030f4e00SChris Leech 	if (!fcoe) {
353030f4e00SChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
3547287fb91SRobert Love 		fcoe = ERR_PTR(-ENOMEM);
3557287fb91SRobert Love 		goto out_nomod;
356030f4e00SChris Leech 	}
357030f4e00SChris Leech 
3582e70e241SChris Leech 	dev_hold(netdev);
359030f4e00SChris Leech 	kref_init(&fcoe->kref);
36054b649f8SChris Leech 
36154b649f8SChris Leech 	/*
36254b649f8SChris Leech 	 * Initialize FIP.
36354b649f8SChris Leech 	 */
3641dd454d9SJoe Eykholt 	fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
36554b649f8SChris Leech 	fcoe->ctlr.send = fcoe_fip_send;
36654b649f8SChris Leech 	fcoe->ctlr.update_mac = fcoe_update_src_mac;
36711b56188SChris Leech 	fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
36854b649f8SChris Leech 
36959d92516Sjohn fastabend 	err = fcoe_interface_setup(fcoe, netdev);
37059d92516Sjohn fastabend 	if (err) {
37159d92516Sjohn fastabend 		fcoe_ctlr_destroy(&fcoe->ctlr);
37259d92516Sjohn fastabend 		kfree(fcoe);
37359d92516Sjohn fastabend 		dev_put(netdev);
3747287fb91SRobert Love 		fcoe = ERR_PTR(err);
3757287fb91SRobert Love 		goto out_nomod;
37659d92516Sjohn fastabend 	}
377030f4e00SChris Leech 
3787287fb91SRobert Love 	goto out;
3797287fb91SRobert Love 
3807287fb91SRobert Love out_nomod:
3817287fb91SRobert Love 	module_put(THIS_MODULE);
3827287fb91SRobert Love out:
383030f4e00SChris Leech 	return fcoe;
384030f4e00SChris Leech }
385030f4e00SChris Leech 
386030f4e00SChris Leech /**
387030f4e00SChris Leech  * fcoe_interface_release() - fcoe_port kref release function
3881875f27eSRobert Love  * @kref: Embedded reference count in an fcoe_interface struct
389030f4e00SChris Leech  */
390030f4e00SChris Leech static void fcoe_interface_release(struct kref *kref)
391030f4e00SChris Leech {
392030f4e00SChris Leech 	struct fcoe_interface *fcoe;
3932e70e241SChris Leech 	struct net_device *netdev;
394030f4e00SChris Leech 
395030f4e00SChris Leech 	fcoe = container_of(kref, struct fcoe_interface, kref);
3962e70e241SChris Leech 	netdev = fcoe->netdev;
3972e70e241SChris Leech 	/* tear-down the FCoE controller */
3982e70e241SChris Leech 	fcoe_ctlr_destroy(&fcoe->ctlr);
399030f4e00SChris Leech 	kfree(fcoe);
4002e70e241SChris Leech 	dev_put(netdev);
4017287fb91SRobert Love 	module_put(THIS_MODULE);
402030f4e00SChris Leech }
403030f4e00SChris Leech 
404030f4e00SChris Leech /**
4051875f27eSRobert Love  * fcoe_interface_get() - Get a reference to a FCoE interface
4061875f27eSRobert Love  * @fcoe: The FCoE interface to be held
407030f4e00SChris Leech  */
408030f4e00SChris Leech static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
409030f4e00SChris Leech {
410030f4e00SChris Leech 	kref_get(&fcoe->kref);
411030f4e00SChris Leech }
412030f4e00SChris Leech 
413030f4e00SChris Leech /**
4141875f27eSRobert Love  * fcoe_interface_put() - Put a reference to a FCoE interface
4151875f27eSRobert Love  * @fcoe: The FCoE interface to be released
416030f4e00SChris Leech  */
417030f4e00SChris Leech static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
418030f4e00SChris Leech {
419030f4e00SChris Leech 	kref_put(&fcoe->kref, fcoe_interface_release);
420030f4e00SChris Leech }
421030f4e00SChris Leech 
422030f4e00SChris Leech /**
423f04ca1b6SVasu Dev  * fcoe_interface_cleanup() - Clean up a FCoE interface
424f04ca1b6SVasu Dev  * @fcoe: The FCoE interface to be cleaned up
425f04ca1b6SVasu Dev  *
426f04ca1b6SVasu Dev  * Caller must be holding the RTNL mutex
427f04ca1b6SVasu Dev  */
428f04ca1b6SVasu Dev void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
429f04ca1b6SVasu Dev {
430f04ca1b6SVasu Dev 	struct net_device *netdev = fcoe->netdev;
431f04ca1b6SVasu Dev 	struct fcoe_ctlr *fip = &fcoe->ctlr;
432f04ca1b6SVasu Dev 	u8 flogi_maddr[ETH_ALEN];
433f04ca1b6SVasu Dev 	const struct net_device_ops *ops;
434f04ca1b6SVasu Dev 
435f04ca1b6SVasu Dev 	/*
436f04ca1b6SVasu Dev 	 * Don't listen for Ethernet packets anymore.
437f04ca1b6SVasu Dev 	 * synchronize_net() ensures that the packet handlers are not running
438f04ca1b6SVasu Dev 	 * on another CPU. dev_remove_pack() would do that, this calls the
439f04ca1b6SVasu Dev 	 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
440f04ca1b6SVasu Dev 	 */
441f04ca1b6SVasu Dev 	__dev_remove_pack(&fcoe->fcoe_packet_type);
442f04ca1b6SVasu Dev 	__dev_remove_pack(&fcoe->fip_packet_type);
443f04ca1b6SVasu Dev 	synchronize_net();
444f04ca1b6SVasu Dev 
445f04ca1b6SVasu Dev 	/* Delete secondary MAC addresses */
446f04ca1b6SVasu Dev 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
447f04ca1b6SVasu Dev 	dev_uc_del(netdev, flogi_maddr);
448f04ca1b6SVasu Dev 	if (fip->spma)
449f04ca1b6SVasu Dev 		dev_uc_del(netdev, fip->ctl_src_addr);
450f04ca1b6SVasu Dev 	if (fip->mode == FIP_MODE_VN2VN) {
451f04ca1b6SVasu Dev 		dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
452f04ca1b6SVasu Dev 		dev_mc_del(netdev, FIP_ALL_P2P_MACS);
453f04ca1b6SVasu Dev 	} else
454f04ca1b6SVasu Dev 		dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
455f04ca1b6SVasu Dev 
456f04ca1b6SVasu Dev 	/* Tell the LLD we are done w/ FCoE */
457f04ca1b6SVasu Dev 	ops = netdev->netdev_ops;
458f04ca1b6SVasu Dev 	if (ops->ndo_fcoe_disable) {
459f04ca1b6SVasu Dev 		if (ops->ndo_fcoe_disable(netdev))
460f04ca1b6SVasu Dev 			FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
461f04ca1b6SVasu Dev 					" specific feature for LLD.\n");
462f04ca1b6SVasu Dev 	}
463b2085a4eSNeerav Parikh 
464b2085a4eSNeerav Parikh 	/* Release the self-reference taken during fcoe_interface_create() */
465f04ca1b6SVasu Dev 	fcoe_interface_put(fcoe);
466f04ca1b6SVasu Dev }
467f04ca1b6SVasu Dev 
468f04ca1b6SVasu Dev /**
4691875f27eSRobert Love  * fcoe_fip_recv() - Handler for received FIP frames
4701875f27eSRobert Love  * @skb:      The receive skb
4711875f27eSRobert Love  * @netdev:   The associated net device
4721875f27eSRobert Love  * @ptype:    The packet_type structure which was used to register this handler
4731875f27eSRobert Love  * @orig_dev: The original net_device the the skb was received on.
4741875f27eSRobert Love  *	      (in case dev is a bond)
475ab6b85c1SVasu Dev  *
476ab6b85c1SVasu Dev  * Returns: 0 for success
477ab6b85c1SVasu Dev  */
4781875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
479ab6b85c1SVasu Dev 			 struct packet_type *ptype,
480ab6b85c1SVasu Dev 			 struct net_device *orig_dev)
481ab6b85c1SVasu Dev {
482259ad85dSChris Leech 	struct fcoe_interface *fcoe;
483ab6b85c1SVasu Dev 
484259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
4853fe9a0baSChris Leech 	fcoe_ctlr_recv(&fcoe->ctlr, skb);
486ab6b85c1SVasu Dev 	return 0;
487ab6b85c1SVasu Dev }
488ab6b85c1SVasu Dev 
489ab6b85c1SVasu Dev /**
490980f5156SVasu Dev  * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
491980f5156SVasu Dev  * @port: The FCoE port
492980f5156SVasu Dev  * @skb: The FIP/FCoE packet to be sent
493980f5156SVasu Dev  */
494980f5156SVasu Dev static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
495980f5156SVasu Dev {
496980f5156SVasu Dev 	if (port->fcoe_pending_queue.qlen)
497980f5156SVasu Dev 		fcoe_check_wait_queue(port->lport, skb);
498980f5156SVasu Dev 	else if (fcoe_start_io(skb))
499980f5156SVasu Dev 		fcoe_check_wait_queue(port->lport, skb);
500980f5156SVasu Dev }
501980f5156SVasu Dev 
502980f5156SVasu Dev /**
5031875f27eSRobert Love  * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
5041875f27eSRobert Love  * @fip: The FCoE controller
5051875f27eSRobert Love  * @skb: The FIP packet to be sent
506ab6b85c1SVasu Dev  */
507ab6b85c1SVasu Dev static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
508ab6b85c1SVasu Dev {
5093fe9a0baSChris Leech 	skb->dev = fcoe_from_ctlr(fip)->netdev;
510980f5156SVasu Dev 	fcoe_port_send(lport_priv(fip->lp), skb);
511ab6b85c1SVasu Dev }
512ab6b85c1SVasu Dev 
513ab6b85c1SVasu Dev /**
5141875f27eSRobert Love  * fcoe_update_src_mac() - Update the Ethernet MAC filters
5151875f27eSRobert Love  * @lport: The local port to update the source MAC on
5161875f27eSRobert Love  * @addr:  Unicast MAC address to add
517ab6b85c1SVasu Dev  *
518ab6b85c1SVasu Dev  * Remove any previously-set unicast MAC filter.
519ab6b85c1SVasu Dev  * Add secondary FCoE MAC address filter for our OUI.
520ab6b85c1SVasu Dev  */
52111b56188SChris Leech static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
522ab6b85c1SVasu Dev {
52311b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
5248597ae8bSBhanu Prakash Gollapudi 	struct fcoe_interface *fcoe = port->priv;
525ab6b85c1SVasu Dev 
526ab6b85c1SVasu Dev 	rtnl_lock();
52711b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
528a748ee24SJiri Pirko 		dev_uc_del(fcoe->netdev, port->data_src_addr);
52911b56188SChris Leech 	if (!is_zero_ether_addr(addr))
530a748ee24SJiri Pirko 		dev_uc_add(fcoe->netdev, addr);
53111b56188SChris Leech 	memcpy(port->data_src_addr, addr, ETH_ALEN);
532ab6b85c1SVasu Dev 	rtnl_unlock();
533ab6b85c1SVasu Dev }
534ab6b85c1SVasu Dev 
535ab6b85c1SVasu Dev /**
53611b56188SChris Leech  * fcoe_get_src_mac() - return the Ethernet source address for an lport
53711b56188SChris Leech  * @lport: libfc lport
53811b56188SChris Leech  */
53911b56188SChris Leech static u8 *fcoe_get_src_mac(struct fc_lport *lport)
54011b56188SChris Leech {
54111b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
54211b56188SChris Leech 
54311b56188SChris Leech 	return port->data_src_addr;
54411b56188SChris Leech }
54511b56188SChris Leech 
54611b56188SChris Leech /**
5471875f27eSRobert Love  * fcoe_lport_config() - Set up a local port
5481875f27eSRobert Love  * @lport: The local port to be setup
549a703e490SVasu Dev  *
550a703e490SVasu Dev  * Returns: 0 for success
551a703e490SVasu Dev  */
5521875f27eSRobert Love static int fcoe_lport_config(struct fc_lport *lport)
553a703e490SVasu Dev {
5541875f27eSRobert Love 	lport->link_up = 0;
5551875f27eSRobert Love 	lport->qfull = 0;
5561875f27eSRobert Love 	lport->max_retry_count = 3;
5571875f27eSRobert Love 	lport->max_rport_retry_count = 3;
5581875f27eSRobert Love 	lport->e_d_tov = 2 * 1000;	/* FC-FS default */
5591875f27eSRobert Love 	lport->r_a_tov = 2 * 2 * 1000;
5601875f27eSRobert Love 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
561a703e490SVasu Dev 				 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
5621875f27eSRobert Love 	lport->does_npiv = 1;
563a703e490SVasu Dev 
5641875f27eSRobert Love 	fc_lport_init_stats(lport);
565a703e490SVasu Dev 
566a703e490SVasu Dev 	/* lport fc_lport related configuration */
5671875f27eSRobert Love 	fc_lport_config(lport);
568a703e490SVasu Dev 
569a703e490SVasu Dev 	/* offload related configuration */
5701875f27eSRobert Love 	lport->crc_offload = 0;
5711875f27eSRobert Love 	lport->seq_offload = 0;
5721875f27eSRobert Love 	lport->lro_enabled = 0;
5731875f27eSRobert Love 	lport->lro_xid = 0;
5741875f27eSRobert Love 	lport->lso_max = 0;
575a703e490SVasu Dev 
576a703e490SVasu Dev 	return 0;
577a703e490SVasu Dev }
578a703e490SVasu Dev 
579a703e490SVasu Dev /**
580dcece412SYi Zou  * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
581dcece412SYi Zou  * @netdev: the associated net device
582dcece412SYi Zou  * @wwn: the output WWN
583dcece412SYi Zou  * @type: the type of WWN (WWPN or WWNN)
584dcece412SYi Zou  *
585dcece412SYi Zou  * Returns: 0 for success
586dcece412SYi Zou  */
587dcece412SYi Zou static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
588dcece412SYi Zou {
589dcece412SYi Zou 	const struct net_device_ops *ops = netdev->netdev_ops;
590dcece412SYi Zou 
591dcece412SYi Zou 	if (ops->ndo_fcoe_get_wwn)
592dcece412SYi Zou 		return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
593dcece412SYi Zou 	return -EINVAL;
594dcece412SYi Zou }
595dcece412SYi Zou 
596dcece412SYi Zou /**
59754a5b21dSYi Zou  * fcoe_netdev_features_change - Updates the lport's offload flags based
59854a5b21dSYi Zou  * on the LLD netdev's FCoE feature flags
59954a5b21dSYi Zou  */
60054a5b21dSYi Zou static void fcoe_netdev_features_change(struct fc_lport *lport,
60154a5b21dSYi Zou 					struct net_device *netdev)
60254a5b21dSYi Zou {
60354a5b21dSYi Zou 	mutex_lock(&lport->lp_mutex);
60454a5b21dSYi Zou 
60554a5b21dSYi Zou 	if (netdev->features & NETIF_F_SG)
60654a5b21dSYi Zou 		lport->sg_supp = 1;
60754a5b21dSYi Zou 	else
60854a5b21dSYi Zou 		lport->sg_supp = 0;
60954a5b21dSYi Zou 
61054a5b21dSYi Zou 	if (netdev->features & NETIF_F_FCOE_CRC) {
61154a5b21dSYi Zou 		lport->crc_offload = 1;
61254a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
61354a5b21dSYi Zou 	} else {
61454a5b21dSYi Zou 		lport->crc_offload = 0;
61554a5b21dSYi Zou 	}
61654a5b21dSYi Zou 
61754a5b21dSYi Zou 	if (netdev->features & NETIF_F_FSO) {
61854a5b21dSYi Zou 		lport->seq_offload = 1;
61954a5b21dSYi Zou 		lport->lso_max = netdev->gso_max_size;
62054a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
62154a5b21dSYi Zou 				lport->lso_max);
62254a5b21dSYi Zou 	} else {
62354a5b21dSYi Zou 		lport->seq_offload = 0;
62454a5b21dSYi Zou 		lport->lso_max = 0;
62554a5b21dSYi Zou 	}
62654a5b21dSYi Zou 
62754a5b21dSYi Zou 	if (netdev->fcoe_ddp_xid) {
62854a5b21dSYi Zou 		lport->lro_enabled = 1;
62954a5b21dSYi Zou 		lport->lro_xid = netdev->fcoe_ddp_xid;
63054a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
63154a5b21dSYi Zou 				lport->lro_xid);
63254a5b21dSYi Zou 	} else {
63354a5b21dSYi Zou 		lport->lro_enabled = 0;
63454a5b21dSYi Zou 		lport->lro_xid = 0;
63554a5b21dSYi Zou 	}
63654a5b21dSYi Zou 
63754a5b21dSYi Zou 	mutex_unlock(&lport->lp_mutex);
63854a5b21dSYi Zou }
63954a5b21dSYi Zou 
64054a5b21dSYi Zou /**
6411875f27eSRobert Love  * fcoe_netdev_config() - Set up net devive for SW FCoE
6421875f27eSRobert Love  * @lport:  The local port that is associated with the net device
6431875f27eSRobert Love  * @netdev: The associated net device
644a703e490SVasu Dev  *
6451875f27eSRobert Love  * Must be called after fcoe_lport_config() as it will use local port mutex
646a703e490SVasu Dev  *
647a703e490SVasu Dev  * Returns: 0 for success
648a703e490SVasu Dev  */
6491875f27eSRobert Love static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
650a703e490SVasu Dev {
651a703e490SVasu Dev 	u32 mfs;
652a703e490SVasu Dev 	u64 wwnn, wwpn;
65325024989SChris Leech 	struct fcoe_interface *fcoe;
654014f5c3fSChris Leech 	struct fcoe_port *port;
655a703e490SVasu Dev 
656a703e490SVasu Dev 	/* Setup lport private data to point to fcoe softc */
6571875f27eSRobert Love 	port = lport_priv(lport);
6588597ae8bSBhanu Prakash Gollapudi 	fcoe = port->priv;
659a703e490SVasu Dev 
660a703e490SVasu Dev 	/*
661a703e490SVasu Dev 	 * Determine max frame size based on underlying device and optional
662a703e490SVasu Dev 	 * user-configured limit.  If the MFS is too low, fcoe_link_ok()
663a703e490SVasu Dev 	 * will return 0, so do this first.
664a703e490SVasu Dev 	 */
6657221d7e5SYi Zou 	mfs = netdev->mtu;
6667221d7e5SYi Zou 	if (netdev->features & NETIF_F_FCOE_MTU) {
6677221d7e5SYi Zou 		mfs = FCOE_MTU;
6687221d7e5SYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
6697221d7e5SYi Zou 	}
6707221d7e5SYi Zou 	mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
6711875f27eSRobert Love 	if (fc_set_mfs(lport, mfs))
672a703e490SVasu Dev 		return -EINVAL;
673a703e490SVasu Dev 
674a703e490SVasu Dev 	/* offload features support */
67554a5b21dSYi Zou 	fcoe_netdev_features_change(lport, netdev);
676a703e490SVasu Dev 
677014f5c3fSChris Leech 	skb_queue_head_init(&port->fcoe_pending_queue);
678014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
6791875f27eSRobert Love 	setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
680a703e490SVasu Dev 
6815e4f8fe7SRobert Love 	fcoe_link_speed_update(lport);
6825e4f8fe7SRobert Love 
6831875f27eSRobert Love 	if (!lport->vport) {
684dcece412SYi Zou 		if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
68575ea89efSYi Zou 			wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
6861875f27eSRobert Love 		fc_set_wwnn(lport, wwnn);
687dcece412SYi Zou 		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
688dcece412SYi Zou 			wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
689cf4aebcaSVasu Dev 						 2, 0);
6901875f27eSRobert Love 		fc_set_wwpn(lport, wwpn);
6919a05753bSChris Leech 	}
692a703e490SVasu Dev 
693a703e490SVasu Dev 	return 0;
694a703e490SVasu Dev }
695a703e490SVasu Dev 
696a703e490SVasu Dev /**
6971875f27eSRobert Love  * fcoe_shost_config() - Set up the SCSI host associated with a local port
6981875f27eSRobert Love  * @lport: The local port
6991875f27eSRobert Love  * @dev:   The device associated with the SCSI host
700a703e490SVasu Dev  *
701a703e490SVasu Dev  * Must be called after fcoe_lport_config() and fcoe_netdev_config()
702a703e490SVasu Dev  *
703a703e490SVasu Dev  * Returns: 0 for success
704a703e490SVasu Dev  */
7058ba00a4bSVasu Dev static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
706a703e490SVasu Dev {
707a703e490SVasu Dev 	int rc = 0;
708a703e490SVasu Dev 
709a703e490SVasu Dev 	/* lport scsi host config */
7101875f27eSRobert Love 	lport->host->max_lun = FCOE_MAX_LUN;
7111875f27eSRobert Love 	lport->host->max_id = FCOE_MAX_FCP_TARGET;
7121875f27eSRobert Love 	lport->host->max_channel = 0;
713da87bfabSVasu Dev 	lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
714da87bfabSVasu Dev 
7151875f27eSRobert Love 	if (lport->vport)
7168ca86f84SYi Zou 		lport->host->transportt = fcoe_vport_scsi_transport;
717e9084bb8SChris Leech 	else
7188ca86f84SYi Zou 		lport->host->transportt = fcoe_nport_scsi_transport;
719a703e490SVasu Dev 
720a703e490SVasu Dev 	/* add the new host to the SCSI-ml */
7211875f27eSRobert Love 	rc = scsi_add_host(lport->host, dev);
722a703e490SVasu Dev 	if (rc) {
7231875f27eSRobert Love 		FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
724d5488eb9SRobert Love 				"error on scsi_add_host\n");
725a703e490SVasu Dev 		return rc;
726a703e490SVasu Dev 	}
7279a05753bSChris Leech 
7281875f27eSRobert Love 	if (!lport->vport)
7294be929beSAlexey Dobriyan 		fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
7309a05753bSChris Leech 
7311875f27eSRobert Love 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
7325baa17c3SChris Leech 		 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
7331875f27eSRobert Love 		 fcoe_netdev(lport)->name);
734a703e490SVasu Dev 
735a703e490SVasu Dev 	return 0;
736a703e490SVasu Dev }
737a703e490SVasu Dev 
7381875f27eSRobert Love /**
7391875f27eSRobert Love  * fcoe_oem_match() - The match routine for the offloaded exchange manager
7401875f27eSRobert Love  * @fp: The I/O frame
741d7179680SVasu Dev  *
7421875f27eSRobert Love  * This routine will be associated with an exchange manager (EM). When
7431875f27eSRobert Love  * the libfc exchange handling code is looking for an EM to use it will
7441875f27eSRobert Love  * call this routine and pass it the frame that it wishes to send. This
7451875f27eSRobert Love  * routine will return True if the associated EM is to be used and False
7461875f27eSRobert Love  * if the echange code should continue looking for an EM.
7471875f27eSRobert Love  *
7481875f27eSRobert Love  * The offload EM that this routine is associated with will handle any
7491875f27eSRobert Love  * packets that are for SCSI read requests.
7501875f27eSRobert Love  *
7511ff9918bSKiran Patil  * This has been enhanced to work when FCoE stack is operating in target
7521ff9918bSKiran Patil  * mode.
7531ff9918bSKiran Patil  *
7541875f27eSRobert Love  * Returns: True for read types I/O, otherwise returns false.
755d7179680SVasu Dev  */
756d7179680SVasu Dev bool fcoe_oem_match(struct fc_frame *fp)
757d7179680SVasu Dev {
7581ff9918bSKiran Patil 	struct fc_frame_header *fh = fc_frame_header_get(fp);
7591ff9918bSKiran Patil 	struct fcp_cmnd *fcp;
7601ff9918bSKiran Patil 
7611ff9918bSKiran Patil 	if (fc_fcp_is_read(fr_fsp(fp)) &&
7621ff9918bSKiran Patil 	    (fr_fsp(fp)->data_len > fcoe_ddp_min))
7631ff9918bSKiran Patil 		return true;
7641ff9918bSKiran Patil 	else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
7651ff9918bSKiran Patil 		fcp = fc_frame_payload_get(fp, sizeof(*fcp));
7661ff9918bSKiran Patil 		if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
7671ff9918bSKiran Patil 		    fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
7681ff9918bSKiran Patil 		    (fcp->fc_flags & FCP_CFL_WRDATA))
7691ff9918bSKiran Patil 			return true;
7701ff9918bSKiran Patil 	}
7711ff9918bSKiran Patil 	return false;
772d7179680SVasu Dev }
773d7179680SVasu Dev 
774a703e490SVasu Dev /**
7751875f27eSRobert Love  * fcoe_em_config() - Allocate and configure an exchange manager
7761875f27eSRobert Love  * @lport: The local port that the new EM will be associated with
777a703e490SVasu Dev  *
778a703e490SVasu Dev  * Returns: 0 on success
779a703e490SVasu Dev  */
7801875f27eSRobert Love static inline int fcoe_em_config(struct fc_lport *lport)
781a703e490SVasu Dev {
7821875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
7838597ae8bSBhanu Prakash Gollapudi 	struct fcoe_interface *fcoe = port->priv;
78425024989SChris Leech 	struct fcoe_interface *oldfcoe = NULL;
7851d1b88dcSVasu Dev 	struct net_device *old_real_dev, *cur_real_dev;
786d7179680SVasu Dev 	u16 min_xid = FCOE_MIN_XID;
787d7179680SVasu Dev 	u16 max_xid = FCOE_MAX_XID;
788d7179680SVasu Dev 
789d7179680SVasu Dev 	/*
790d7179680SVasu Dev 	 * Check if need to allocate an em instance for
791d7179680SVasu Dev 	 * offload exchange ids to be shared across all VN_PORTs/lport.
792d7179680SVasu Dev 	 */
7931875f27eSRobert Love 	if (!lport->lro_enabled || !lport->lro_xid ||
7941875f27eSRobert Love 	    (lport->lro_xid >= max_xid)) {
7951875f27eSRobert Love 		lport->lro_xid = 0;
796d7179680SVasu Dev 		goto skip_oem;
797d7179680SVasu Dev 	}
798d7179680SVasu Dev 
799d7179680SVasu Dev 	/*
800d7179680SVasu Dev 	 * Reuse existing offload em instance in case
8011d1b88dcSVasu Dev 	 * it is already allocated on real eth device
802d7179680SVasu Dev 	 */
80325024989SChris Leech 	if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
80425024989SChris Leech 		cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
8051d1b88dcSVasu Dev 	else
80625024989SChris Leech 		cur_real_dev = fcoe->netdev;
8071d1b88dcSVasu Dev 
80825024989SChris Leech 	list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
80925024989SChris Leech 		if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
81025024989SChris Leech 			old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
8111d1b88dcSVasu Dev 		else
81225024989SChris Leech 			old_real_dev = oldfcoe->netdev;
8131d1b88dcSVasu Dev 
8141d1b88dcSVasu Dev 		if (cur_real_dev == old_real_dev) {
815991cbb60SChris Leech 			fcoe->oem = oldfcoe->oem;
816d7179680SVasu Dev 			break;
817d7179680SVasu Dev 		}
818d7179680SVasu Dev 	}
819d7179680SVasu Dev 
820991cbb60SChris Leech 	if (fcoe->oem) {
8211875f27eSRobert Love 		if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
822d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to add "
823d7179680SVasu Dev 			       "offload em:%p on interface:%s\n",
824991cbb60SChris Leech 			       fcoe->oem, fcoe->netdev->name);
825a703e490SVasu Dev 			return -ENOMEM;
826d7179680SVasu Dev 		}
827d7179680SVasu Dev 	} else {
8281875f27eSRobert Love 		fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
8291875f27eSRobert Love 					      FCOE_MIN_XID, lport->lro_xid,
830d7179680SVasu Dev 					      fcoe_oem_match);
831991cbb60SChris Leech 		if (!fcoe->oem) {
832d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to allocate "
833d7179680SVasu Dev 			       "em for offload exches on interface:%s\n",
83425024989SChris Leech 			       fcoe->netdev->name);
835d7179680SVasu Dev 			return -ENOMEM;
836d7179680SVasu Dev 		}
837d7179680SVasu Dev 	}
838d7179680SVasu Dev 
839d7179680SVasu Dev 	/*
840d7179680SVasu Dev 	 * Exclude offload EM xid range from next EM xid range.
841d7179680SVasu Dev 	 */
8421875f27eSRobert Love 	min_xid += lport->lro_xid + 1;
843d7179680SVasu Dev 
844d7179680SVasu Dev skip_oem:
8451875f27eSRobert Love 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
846d7179680SVasu Dev 		printk(KERN_ERR "fcoe_em_config: failed to "
84725024989SChris Leech 		       "allocate em on interface %s\n", fcoe->netdev->name);
848d7179680SVasu Dev 		return -ENOMEM;
849d7179680SVasu Dev 	}
850a703e490SVasu Dev 
851a703e490SVasu Dev 	return 0;
852a703e490SVasu Dev }
853a703e490SVasu Dev 
854a703e490SVasu Dev /**
8551875f27eSRobert Love  * fcoe_if_destroy() - Tear down a SW FCoE instance
8561875f27eSRobert Love  * @lport: The local port to be destroyed
85734ce27bcSVasu Dev  *
858a703e490SVasu Dev  */
859af7f85d9SChris Leech static void fcoe_if_destroy(struct fc_lport *lport)
860a703e490SVasu Dev {
861b2085a4eSNeerav Parikh 	struct fcoe_port *port = lport_priv(lport);
862b2085a4eSNeerav Parikh 	struct fcoe_interface *fcoe = port->priv;
863b2085a4eSNeerav Parikh 	struct net_device *netdev = fcoe->netdev;
864b2085a4eSNeerav Parikh 
865b2085a4eSNeerav Parikh 	FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
866b2085a4eSNeerav Parikh 
867b2085a4eSNeerav Parikh 	/* Logout of the fabric */
868b2085a4eSNeerav Parikh 	fc_fabric_logoff(lport);
869b2085a4eSNeerav Parikh 
870b2085a4eSNeerav Parikh 	/* Cleanup the fc_lport */
871b2085a4eSNeerav Parikh 	fc_lport_destroy(lport);
872b2085a4eSNeerav Parikh 
873b2085a4eSNeerav Parikh 	/* Stop the transmit retry timer */
874b2085a4eSNeerav Parikh 	del_timer_sync(&port->timer);
875b2085a4eSNeerav Parikh 
876b2085a4eSNeerav Parikh 	/* Free existing transmit skbs */
877b2085a4eSNeerav Parikh 	fcoe_clean_pending_queue(lport);
878b2085a4eSNeerav Parikh 
879b2085a4eSNeerav Parikh 	rtnl_lock();
880b2085a4eSNeerav Parikh 	if (!is_zero_ether_addr(port->data_src_addr))
881b2085a4eSNeerav Parikh 		dev_uc_del(netdev, port->data_src_addr);
882b2085a4eSNeerav Parikh 	rtnl_unlock();
883b2085a4eSNeerav Parikh 
884b2085a4eSNeerav Parikh 	/* Release reference held in fcoe_if_create() */
885b2085a4eSNeerav Parikh 	fcoe_interface_put(fcoe);
886b2085a4eSNeerav Parikh 
88754b649f8SChris Leech 	/* Free queued packets for the per-CPU receive threads */
88854b649f8SChris Leech 	fcoe_percpu_clean(lport);
88954b649f8SChris Leech 
890a703e490SVasu Dev 	/* Detach from the scsi-ml */
891af7f85d9SChris Leech 	fc_remove_host(lport->host);
892af7f85d9SChris Leech 	scsi_remove_host(lport->host);
893a703e490SVasu Dev 
89480e736f8SYi Zou 	/* Destroy lport scsi_priv */
89580e736f8SYi Zou 	fc_fcp_destroy(lport);
89680e736f8SYi Zou 
897a703e490SVasu Dev 	/* There are no more rports or I/O, free the EM */
898af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
899a703e490SVasu Dev 
900a703e490SVasu Dev 	/* Free memory used by statistical counters */
901af7f85d9SChris Leech 	fc_lport_free_stats(lport);
902a703e490SVasu Dev 
9032e70e241SChris Leech 	/* Release the Scsi_Host */
904af7f85d9SChris Leech 	scsi_host_put(lport->host);
905a703e490SVasu Dev }
906a703e490SVasu Dev 
9071875f27eSRobert Love /**
9081875f27eSRobert Love  * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
9091875f27eSRobert Love  * @lport: The local port to setup DDP for
9101875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
9111875f27eSRobert Love  * @sgl:   The scatterlist describing this transfer
9121875f27eSRobert Love  * @sgc:   The number of sg items
913a703e490SVasu Dev  *
9141875f27eSRobert Love  * Returns: 0 if the DDP context was not configured
915a703e490SVasu Dev  */
9161875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
917a703e490SVasu Dev 			  struct scatterlist *sgl, unsigned int sgc)
918a703e490SVasu Dev {
9191875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
920a703e490SVasu Dev 
9211875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
9221875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
9231875f27eSRobert Love 							      xid, sgl,
9241875f27eSRobert Love 							      sgc);
925a703e490SVasu Dev 
926a703e490SVasu Dev 	return 0;
927a703e490SVasu Dev }
928a703e490SVasu Dev 
929a703e490SVasu Dev /**
93071f89491SYi Zou  * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
93171f89491SYi Zou  * @lport: The local port to setup DDP for
93271f89491SYi Zou  * @xid:   The exchange ID for this DDP transfer
93371f89491SYi Zou  * @sgl:   The scatterlist describing this transfer
93471f89491SYi Zou  * @sgc:   The number of sg items
93571f89491SYi Zou  *
93671f89491SYi Zou  * Returns: 0 if the DDP context was not configured
93771f89491SYi Zou  */
93871f89491SYi Zou static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
93971f89491SYi Zou 			   struct scatterlist *sgl, unsigned int sgc)
94071f89491SYi Zou {
94171f89491SYi Zou 	struct net_device *netdev = fcoe_netdev(lport);
94271f89491SYi Zou 
94371f89491SYi Zou 	if (netdev->netdev_ops->ndo_fcoe_ddp_target)
94471f89491SYi Zou 		return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
94571f89491SYi Zou 							       sgl, sgc);
94671f89491SYi Zou 
94771f89491SYi Zou 	return 0;
94871f89491SYi Zou }
94971f89491SYi Zou 
95071f89491SYi Zou 
95171f89491SYi Zou /**
9521875f27eSRobert Love  * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
9531875f27eSRobert Love  * @lport: The local port to complete DDP on
9541875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
955a703e490SVasu Dev  *
9561875f27eSRobert Love  * Returns: the length of data that have been completed by DDP
9571875f27eSRobert Love  */
9581875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
9591875f27eSRobert Love {
9601875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
9611875f27eSRobert Love 
9621875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_done)
9631875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
9641875f27eSRobert Love 	return 0;
9651875f27eSRobert Love }
9661875f27eSRobert Love 
9671875f27eSRobert Love /**
9681875f27eSRobert Love  * fcoe_if_create() - Create a FCoE instance on an interface
9691875f27eSRobert Love  * @fcoe:   The FCoE interface to create a local port on
9701875f27eSRobert Love  * @parent: The device pointer to be the parent in sysfs for the SCSI host
9711875f27eSRobert Love  * @npiv:   Indicates if the port is a vport or not
9721875f27eSRobert Love  *
9731875f27eSRobert Love  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
974a703e490SVasu Dev  *
975af7f85d9SChris Leech  * Returns: The allocated fc_lport or an error pointer
976a703e490SVasu Dev  */
977030f4e00SChris Leech static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
9789a05753bSChris Leech 				       struct device *parent, int npiv)
979a703e490SVasu Dev {
9801875f27eSRobert Love 	struct net_device *netdev = fcoe->netdev;
98172fa396bSVasu Dev 	struct fc_lport *lport, *n_port;
982014f5c3fSChris Leech 	struct fcoe_port *port;
98372fa396bSVasu Dev 	struct Scsi_Host *shost;
9841875f27eSRobert Love 	int rc;
9859a05753bSChris Leech 	/*
9869a05753bSChris Leech 	 * parent is only a vport if npiv is 1,
9879a05753bSChris Leech 	 * but we'll only use vport in that case so go ahead and set it
9889a05753bSChris Leech 	 */
9899a05753bSChris Leech 	struct fc_vport *vport = dev_to_vport(parent);
990a703e490SVasu Dev 
991d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Create Interface\n");
992a703e490SVasu Dev 
99372fa396bSVasu Dev 	if (!npiv)
99472fa396bSVasu Dev 		lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
99572fa396bSVasu Dev 	else
99672fa396bSVasu Dev 		lport = libfc_vport_create(vport, sizeof(*port));
99772fa396bSVasu Dev 
99886221969SChris Leech 	if (!lport) {
999014f5c3fSChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1000014f5c3fSChris Leech 		rc = -ENOMEM;
1001030f4e00SChris Leech 		goto out;
1002014f5c3fSChris Leech 	}
1003014f5c3fSChris Leech 	port = lport_priv(lport);
10042e70e241SChris Leech 	port->lport = lport;
10058597ae8bSBhanu Prakash Gollapudi 	port->priv = fcoe;
10068597ae8bSBhanu Prakash Gollapudi 	port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
10078597ae8bSBhanu Prakash Gollapudi 	port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
10082e70e241SChris Leech 	INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1009a703e490SVasu Dev 
10101875f27eSRobert Love 	/* configure a fc_lport including the exchange manager */
1011af7f85d9SChris Leech 	rc = fcoe_lport_config(lport);
1012a703e490SVasu Dev 	if (rc) {
1013d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1014d5488eb9SRobert Love 				"interface\n");
1015a703e490SVasu Dev 		goto out_host_put;
1016a703e490SVasu Dev 	}
1017a703e490SVasu Dev 
10189a05753bSChris Leech 	if (npiv) {
10199f8f3aa6SChris Leech 		FCOE_NETDEV_DBG(netdev, "Setting vport names, "
10209f8f3aa6SChris Leech 				"%16.16llx %16.16llx\n",
10219a05753bSChris Leech 				vport->node_name, vport->port_name);
10229a05753bSChris Leech 		fc_set_wwnn(lport, vport->node_name);
10239a05753bSChris Leech 		fc_set_wwpn(lport, vport->port_name);
10249a05753bSChris Leech 	}
10259a05753bSChris Leech 
1026ab6b85c1SVasu Dev 	/* configure lport network properties */
1027af7f85d9SChris Leech 	rc = fcoe_netdev_config(lport, netdev);
1028ab6b85c1SVasu Dev 	if (rc) {
1029d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1030d5488eb9SRobert Love 				"interface\n");
103154b649f8SChris Leech 		goto out_lp_destroy;
1032ab6b85c1SVasu Dev 	}
103397c8389dSJoe Eykholt 
1034a703e490SVasu Dev 	/* configure lport scsi host properties */
10358ba00a4bSVasu Dev 	rc = fcoe_shost_config(lport, parent);
1036a703e490SVasu Dev 	if (rc) {
1037d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1038d5488eb9SRobert Love 				"interface\n");
103954b649f8SChris Leech 		goto out_lp_destroy;
1040a703e490SVasu Dev 	}
1041a703e490SVasu Dev 
1042a703e490SVasu Dev 	/* Initialize the library */
1043e10f8c66SJoe Eykholt 	rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
1044a703e490SVasu Dev 	if (rc) {
1045d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1046d5488eb9SRobert Love 				"interface\n");
1047a703e490SVasu Dev 		goto out_lp_destroy;
1048a703e490SVasu Dev 	}
1049a703e490SVasu Dev 
1050e8af4d43SVasu Dev 	/*
1051e8af4d43SVasu Dev 	 * fcoe_em_alloc() and fcoe_hostlist_add() both
10529a05753bSChris Leech 	 * need to be atomic with respect to other changes to the
10539a05753bSChris Leech 	 * hostlist since fcoe_em_alloc() looks for an existing EM
1054e8af4d43SVasu Dev 	 * instance on host list updated by fcoe_hostlist_add().
1055c863df33SChris Leech 	 *
10569a05753bSChris Leech 	 * This is currently handled through the fcoe_config_mutex
10579a05753bSChris Leech 	 * begin held.
1058e8af4d43SVasu Dev 	 */
105972fa396bSVasu Dev 	if (!npiv)
106096316099SVasu Dev 		/* lport exch manager allocation */
1061af7f85d9SChris Leech 		rc = fcoe_em_config(lport);
106272fa396bSVasu Dev 	else {
106372fa396bSVasu Dev 		shost = vport_to_shost(vport);
106472fa396bSVasu Dev 		n_port = shost_priv(shost);
106572fa396bSVasu Dev 		rc = fc_exch_mgr_list_clone(n_port, lport);
106696316099SVasu Dev 	}
106772fa396bSVasu Dev 
106872fa396bSVasu Dev 	if (rc) {
106972fa396bSVasu Dev 		FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
107072fa396bSVasu Dev 		goto out_lp_destroy;
10719a05753bSChris Leech 	}
107296316099SVasu Dev 
1073030f4e00SChris Leech 	fcoe_interface_get(fcoe);
1074af7f85d9SChris Leech 	return lport;
1075a703e490SVasu Dev 
1076a703e490SVasu Dev out_lp_destroy:
1077af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
1078a703e490SVasu Dev out_host_put:
1079af7f85d9SChris Leech 	scsi_host_put(lport->host);
1080af7f85d9SChris Leech out:
1081af7f85d9SChris Leech 	return ERR_PTR(rc);
1082a703e490SVasu Dev }
1083a703e490SVasu Dev 
1084a703e490SVasu Dev /**
10851875f27eSRobert Love  * fcoe_if_init() - Initialization routine for fcoe.ko
10861875f27eSRobert Love  *
10871875f27eSRobert Love  * Attaches the SW FCoE transport to the FC transport
1088a703e490SVasu Dev  *
1089a703e490SVasu Dev  * Returns: 0 on success
1090a703e490SVasu Dev  */
1091a703e490SVasu Dev static int __init fcoe_if_init(void)
1092a703e490SVasu Dev {
1093a703e490SVasu Dev 	/* attach to scsi transport */
10948ca86f84SYi Zou 	fcoe_nport_scsi_transport =
10958ca86f84SYi Zou 		fc_attach_transport(&fcoe_nport_fc_functions);
10968ca86f84SYi Zou 	fcoe_vport_scsi_transport =
10978ca86f84SYi Zou 		fc_attach_transport(&fcoe_vport_fc_functions);
1098a703e490SVasu Dev 
10998ca86f84SYi Zou 	if (!fcoe_nport_scsi_transport) {
1100d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1101a703e490SVasu Dev 		return -ENODEV;
1102a703e490SVasu Dev 	}
1103a703e490SVasu Dev 
1104a703e490SVasu Dev 	return 0;
1105a703e490SVasu Dev }
1106a703e490SVasu Dev 
1107a703e490SVasu Dev /**
11081875f27eSRobert Love  * fcoe_if_exit() - Tear down fcoe.ko
11091875f27eSRobert Love  *
11101875f27eSRobert Love  * Detaches the SW FCoE transport from the FC transport
1111a703e490SVasu Dev  *
1112a703e490SVasu Dev  * Returns: 0 on success
1113a703e490SVasu Dev  */
1114a703e490SVasu Dev int __exit fcoe_if_exit(void)
1115a703e490SVasu Dev {
11168ca86f84SYi Zou 	fc_release_transport(fcoe_nport_scsi_transport);
11178ca86f84SYi Zou 	fc_release_transport(fcoe_vport_scsi_transport);
11188ca86f84SYi Zou 	fcoe_nport_scsi_transport = NULL;
11198ca86f84SYi Zou 	fcoe_vport_scsi_transport = NULL;
1120a703e490SVasu Dev 	return 0;
1121a703e490SVasu Dev }
1122a703e490SVasu Dev 
1123a703e490SVasu Dev /**
11241875f27eSRobert Love  * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
11251875f27eSRobert Love  * @cpu: The CPU index of the CPU to create a receive thread for
1126a703e490SVasu Dev  */
1127a703e490SVasu Dev static void fcoe_percpu_thread_create(unsigned int cpu)
1128a703e490SVasu Dev {
1129a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1130a703e490SVasu Dev 	struct task_struct *thread;
1131a703e490SVasu Dev 
1132a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1133a703e490SVasu Dev 
1134a703e490SVasu Dev 	thread = kthread_create(fcoe_percpu_receive_thread,
1135a703e490SVasu Dev 				(void *)p, "fcoethread/%d", cpu);
1136a703e490SVasu Dev 
1137e7a51997SJoe Eykholt 	if (likely(!IS_ERR(thread))) {
1138a703e490SVasu Dev 		kthread_bind(thread, cpu);
1139a703e490SVasu Dev 		wake_up_process(thread);
1140a703e490SVasu Dev 
1141a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1142a703e490SVasu Dev 		p->thread = thread;
1143a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1144a703e490SVasu Dev 	}
1145a703e490SVasu Dev }
1146a703e490SVasu Dev 
1147a703e490SVasu Dev /**
11481875f27eSRobert Love  * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
11491875f27eSRobert Love  * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1150a703e490SVasu Dev  *
1151a703e490SVasu Dev  * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1152a703e490SVasu Dev  * current CPU's Rx thread. If the thread being destroyed is bound to
1153a703e490SVasu Dev  * the CPU processing this context the skbs will be freed.
1154a703e490SVasu Dev  */
1155a703e490SVasu Dev static void fcoe_percpu_thread_destroy(unsigned int cpu)
1156a703e490SVasu Dev {
1157a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1158a703e490SVasu Dev 	struct task_struct *thread;
1159a703e490SVasu Dev 	struct page *crc_eof;
1160a703e490SVasu Dev 	struct sk_buff *skb;
1161a703e490SVasu Dev #ifdef CONFIG_SMP
1162a703e490SVasu Dev 	struct fcoe_percpu_s *p0;
1163f018b73aSJoe Eykholt 	unsigned targ_cpu = get_cpu();
1164a703e490SVasu Dev #endif /* CONFIG_SMP */
1165a703e490SVasu Dev 
1166d5488eb9SRobert Love 	FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1167a703e490SVasu Dev 
1168a703e490SVasu Dev 	/* Prevent any new skbs from being queued for this CPU. */
1169a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1170a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1171a703e490SVasu Dev 	thread = p->thread;
1172a703e490SVasu Dev 	p->thread = NULL;
1173a703e490SVasu Dev 	crc_eof = p->crc_eof_page;
1174a703e490SVasu Dev 	p->crc_eof_page = NULL;
1175a703e490SVasu Dev 	p->crc_eof_offset = 0;
1176a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1177a703e490SVasu Dev 
1178a703e490SVasu Dev #ifdef CONFIG_SMP
1179a703e490SVasu Dev 	/*
1180a703e490SVasu Dev 	 * Don't bother moving the skb's if this context is running
1181a703e490SVasu Dev 	 * on the same CPU that is having its thread destroyed. This
1182a703e490SVasu Dev 	 * can easily happen when the module is removed.
1183a703e490SVasu Dev 	 */
1184a703e490SVasu Dev 	if (cpu != targ_cpu) {
1185a703e490SVasu Dev 		p0 = &per_cpu(fcoe_percpu, targ_cpu);
1186a703e490SVasu Dev 		spin_lock_bh(&p0->fcoe_rx_list.lock);
1187a703e490SVasu Dev 		if (p0->thread) {
1188d5488eb9SRobert Love 			FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1189a703e490SVasu Dev 				 cpu, targ_cpu);
1190a703e490SVasu Dev 
1191a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1192a703e490SVasu Dev 				__skb_queue_tail(&p0->fcoe_rx_list, skb);
1193a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1194a703e490SVasu Dev 		} else {
1195a703e490SVasu Dev 			/*
1196a703e490SVasu Dev 			 * The targeted CPU is not initialized and cannot accept
1197a703e490SVasu Dev 			 * new	skbs. Unlock the targeted CPU and drop the skbs
1198a703e490SVasu Dev 			 * on the CPU that is going offline.
1199a703e490SVasu Dev 			 */
1200a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1201a703e490SVasu Dev 				kfree_skb(skb);
1202a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1203a703e490SVasu Dev 		}
1204a703e490SVasu Dev 	} else {
1205a703e490SVasu Dev 		/*
1206a703e490SVasu Dev 		 * This scenario occurs when the module is being removed
1207a703e490SVasu Dev 		 * and all threads are being destroyed. skbs will continue
1208a703e490SVasu Dev 		 * to be shifted from the CPU thread that is being removed
1209a703e490SVasu Dev 		 * to the CPU thread associated with the CPU that is processing
1210a703e490SVasu Dev 		 * the module removal. Once there is only one CPU Rx thread it
1211a703e490SVasu Dev 		 * will reach this case and we will drop all skbs and later
1212a703e490SVasu Dev 		 * stop the thread.
1213a703e490SVasu Dev 		 */
1214a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1215a703e490SVasu Dev 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1216a703e490SVasu Dev 			kfree_skb(skb);
1217a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1218a703e490SVasu Dev 	}
1219f018b73aSJoe Eykholt 	put_cpu();
1220a703e490SVasu Dev #else
1221a703e490SVasu Dev 	/*
1222dd3fd72eSChris Leech 	 * This a non-SMP scenario where the singular Rx thread is
1223a703e490SVasu Dev 	 * being removed. Free all skbs and stop the thread.
1224a703e490SVasu Dev 	 */
1225a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1226a703e490SVasu Dev 	while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1227a703e490SVasu Dev 		kfree_skb(skb);
1228a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1229a703e490SVasu Dev #endif
1230a703e490SVasu Dev 
1231a703e490SVasu Dev 	if (thread)
1232a703e490SVasu Dev 		kthread_stop(thread);
1233a703e490SVasu Dev 
1234a703e490SVasu Dev 	if (crc_eof)
1235a703e490SVasu Dev 		put_page(crc_eof);
1236a703e490SVasu Dev }
1237a703e490SVasu Dev 
1238a703e490SVasu Dev /**
12391875f27eSRobert Love  * fcoe_cpu_callback() - Handler for CPU hotplug events
12401875f27eSRobert Love  * @nfb:    The callback data block
12411875f27eSRobert Love  * @action: The event triggering the callback
12421875f27eSRobert Love  * @hcpu:   The index of the CPU that the event is for
1243a703e490SVasu Dev  *
12441875f27eSRobert Love  * This creates or destroys per-CPU data for fcoe
1245a703e490SVasu Dev  *
1246a703e490SVasu Dev  * Returns NOTIFY_OK always.
1247a703e490SVasu Dev  */
1248a703e490SVasu Dev static int fcoe_cpu_callback(struct notifier_block *nfb,
1249a703e490SVasu Dev 			     unsigned long action, void *hcpu)
1250a703e490SVasu Dev {
1251a703e490SVasu Dev 	unsigned cpu = (unsigned long)hcpu;
1252a703e490SVasu Dev 
1253a703e490SVasu Dev 	switch (action) {
1254a703e490SVasu Dev 	case CPU_ONLINE:
1255a703e490SVasu Dev 	case CPU_ONLINE_FROZEN:
1256d5488eb9SRobert Love 		FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1257a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
1258a703e490SVasu Dev 		break;
1259a703e490SVasu Dev 	case CPU_DEAD:
1260a703e490SVasu Dev 	case CPU_DEAD_FROZEN:
1261d5488eb9SRobert Love 		FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1262a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
1263a703e490SVasu Dev 		break;
1264a703e490SVasu Dev 	default:
1265a703e490SVasu Dev 		break;
1266a703e490SVasu Dev 	}
1267a703e490SVasu Dev 	return NOTIFY_OK;
1268a703e490SVasu Dev }
1269a703e490SVasu Dev 
1270a703e490SVasu Dev /**
1271064287eeSKiran Patil  * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1272064287eeSKiran Patil  *			command.
1273064287eeSKiran Patil  *
1274d272281cSVasu Dev  * This routine selects next CPU based on cpumask to distribute
1275d272281cSVasu Dev  * incoming requests in round robin.
1276064287eeSKiran Patil  *
1277d272281cSVasu Dev  * Returns: int CPU number
1278064287eeSKiran Patil  */
1279d272281cSVasu Dev static inline unsigned int fcoe_select_cpu(void)
1280064287eeSKiran Patil {
1281064287eeSKiran Patil 	static unsigned int selected_cpu;
1282064287eeSKiran Patil 
1283064287eeSKiran Patil 	selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1284064287eeSKiran Patil 	if (selected_cpu >= nr_cpu_ids)
1285064287eeSKiran Patil 		selected_cpu = cpumask_first(cpu_online_mask);
1286d272281cSVasu Dev 
1287064287eeSKiran Patil 	return selected_cpu;
1288064287eeSKiran Patil }
1289064287eeSKiran Patil 
1290064287eeSKiran Patil /**
12911875f27eSRobert Love  * fcoe_rcv() - Receive packets from a net device
12921875f27eSRobert Love  * @skb:    The received packet
12931875f27eSRobert Love  * @netdev: The net device that the packet was received on
12941875f27eSRobert Love  * @ptype:  The packet type context
12951875f27eSRobert Love  * @olddev: The last device net device
1296a703e490SVasu Dev  *
12971875f27eSRobert Love  * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
12981875f27eSRobert Love  * FC frame and passes the frame to libfc.
1299a703e490SVasu Dev  *
1300a703e490SVasu Dev  * Returns: 0 for success
1301a703e490SVasu Dev  */
13021875f27eSRobert Love int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1303a703e490SVasu Dev 	     struct packet_type *ptype, struct net_device *olddev)
1304a703e490SVasu Dev {
13051875f27eSRobert Love 	struct fc_lport *lport;
1306a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1307259ad85dSChris Leech 	struct fcoe_interface *fcoe;
1308a703e490SVasu Dev 	struct fc_frame_header *fh;
1309a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1310519e5135SVasu Dev 	struct ethhdr *eh;
1311b2f0091fSVasu Dev 	unsigned int cpu;
1312a703e490SVasu Dev 
1313259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
13141875f27eSRobert Love 	lport = fcoe->ctlr.lp;
13151875f27eSRobert Love 	if (unlikely(!lport)) {
13161875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1317a703e490SVasu Dev 		goto err2;
1318a703e490SVasu Dev 	}
13191875f27eSRobert Love 	if (!lport->link_up)
132097c8389dSJoe Eykholt 		goto err2;
1321a703e490SVasu Dev 
13221875f27eSRobert Love 	FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1323d5488eb9SRobert Love 			"data:%p tail:%p end:%p sum:%d dev:%s",
1324d5488eb9SRobert Love 			skb->len, skb->data_len, skb->head, skb->data,
1325d5488eb9SRobert Love 			skb_tail_pointer(skb), skb_end_pointer(skb),
1326d5488eb9SRobert Love 			skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1327a703e490SVasu Dev 
1328519e5135SVasu Dev 	eh = eth_hdr(skb);
1329519e5135SVasu Dev 
1330519e5135SVasu Dev 	if (is_fip_mode(&fcoe->ctlr) &&
1331519e5135SVasu Dev 	    compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1332519e5135SVasu Dev 		FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1333519e5135SVasu Dev 				eh->h_source);
1334a703e490SVasu Dev 		goto err;
1335a703e490SVasu Dev 	}
1336a703e490SVasu Dev 
1337a703e490SVasu Dev 	/*
1338a703e490SVasu Dev 	 * Check for minimum frame length, and make sure required FCoE
1339a703e490SVasu Dev 	 * and FC headers are pulled into the linear data area.
1340a703e490SVasu Dev 	 */
1341a703e490SVasu Dev 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1342a703e490SVasu Dev 		     !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1343a703e490SVasu Dev 		goto err;
1344a703e490SVasu Dev 
1345a703e490SVasu Dev 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1346a703e490SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
1347a703e490SVasu Dev 
13480ee31cb5SRobert Love 	if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
13490ee31cb5SRobert Love 		FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
13500ee31cb5SRobert Love 				eh->h_dest);
13510ee31cb5SRobert Love 		goto err;
13520ee31cb5SRobert Love 	}
13530ee31cb5SRobert Love 
1354a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
13551875f27eSRobert Love 	fr->fr_dev = lport;
1356a703e490SVasu Dev 
1357a703e490SVasu Dev 	/*
1358b2f0091fSVasu Dev 	 * In case the incoming frame's exchange is originated from
1359b2f0091fSVasu Dev 	 * the initiator, then received frame's exchange id is ANDed
1360b2f0091fSVasu Dev 	 * with fc_cpu_mask bits to get the same cpu on which exchange
1361d272281cSVasu Dev 	 * was originated, otherwise select cpu using rx exchange id
1362d272281cSVasu Dev 	 * or fcoe_select_cpu().
1363a703e490SVasu Dev 	 */
1364b2f0091fSVasu Dev 	if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1365b2f0091fSVasu Dev 		cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1366064287eeSKiran Patil 	else {
1367d272281cSVasu Dev 		if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1368d272281cSVasu Dev 			cpu = fcoe_select_cpu();
1369d272281cSVasu Dev 		else
137029bdd2bbSKiran Patil 			cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1371064287eeSKiran Patil 	}
1372324f6678SVasu Dev 
1373324f6678SVasu Dev 	if (cpu >= nr_cpu_ids)
1374324f6678SVasu Dev 		goto err;
1375324f6678SVasu Dev 
1376a703e490SVasu Dev 	fps = &per_cpu(fcoe_percpu, cpu);
1377a703e490SVasu Dev 	spin_lock_bh(&fps->fcoe_rx_list.lock);
1378a703e490SVasu Dev 	if (unlikely(!fps->thread)) {
1379a703e490SVasu Dev 		/*
1380a703e490SVasu Dev 		 * The targeted CPU is not ready, let's target
1381a703e490SVasu Dev 		 * the first CPU now. For non-SMP systems this
1382a703e490SVasu Dev 		 * will check the same CPU twice.
1383a703e490SVasu Dev 		 */
13841875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1385d5488eb9SRobert Love 				"ready for incoming skb- using first online "
1386d5488eb9SRobert Love 				"CPU.\n");
1387a703e490SVasu Dev 
1388a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
13896957177fSRusty Russell 		cpu = cpumask_first(cpu_online_mask);
1390a703e490SVasu Dev 		fps = &per_cpu(fcoe_percpu, cpu);
1391a703e490SVasu Dev 		spin_lock_bh(&fps->fcoe_rx_list.lock);
1392a703e490SVasu Dev 		if (!fps->thread) {
1393a703e490SVasu Dev 			spin_unlock_bh(&fps->fcoe_rx_list.lock);
1394a703e490SVasu Dev 			goto err;
1395a703e490SVasu Dev 		}
1396a703e490SVasu Dev 	}
1397a703e490SVasu Dev 
1398a703e490SVasu Dev 	/*
1399a703e490SVasu Dev 	 * We now have a valid CPU that we're targeting for
1400a703e490SVasu Dev 	 * this skb. We also have this receive thread locked,
1401a703e490SVasu Dev 	 * so we're free to queue skbs into it's queue.
1402a703e490SVasu Dev 	 */
1403859b7b64SChris Leech 
1404859b7b64SChris Leech 	/* If this is a SCSI-FCP frame, and this is already executing on the
1405859b7b64SChris Leech 	 * correct CPU, and the queue for this CPU is empty, then go ahead
1406859b7b64SChris Leech 	 * and process the frame directly in the softirq context.
1407859b7b64SChris Leech 	 * This lets us process completions without context switching from the
1408859b7b64SChris Leech 	 * NET_RX softirq, to our receive processing thread, and then back to
1409859b7b64SChris Leech 	 * BLOCK softirq context.
1410859b7b64SChris Leech 	 */
1411859b7b64SChris Leech 	if (fh->fh_type == FC_TYPE_FCP &&
1412859b7b64SChris Leech 	    cpu == smp_processor_id() &&
1413859b7b64SChris Leech 	    skb_queue_empty(&fps->fcoe_rx_list)) {
1414859b7b64SChris Leech 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1415859b7b64SChris Leech 		fcoe_recv_frame(skb);
1416859b7b64SChris Leech 	} else {
1417a703e490SVasu Dev 		__skb_queue_tail(&fps->fcoe_rx_list, skb);
1418a703e490SVasu Dev 		if (fps->fcoe_rx_list.qlen == 1)
1419a703e490SVasu Dev 			wake_up_process(fps->thread);
1420a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1421859b7b64SChris Leech 	}
1422a703e490SVasu Dev 
1423a703e490SVasu Dev 	return 0;
1424a703e490SVasu Dev err:
1425f018b73aSJoe Eykholt 	per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1426f018b73aSJoe Eykholt 	put_cpu();
1427a703e490SVasu Dev err2:
1428a703e490SVasu Dev 	kfree_skb(skb);
1429a703e490SVasu Dev 	return -1;
1430a703e490SVasu Dev }
1431a703e490SVasu Dev 
1432a703e490SVasu Dev /**
14338597ae8bSBhanu Prakash Gollapudi  * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
14341875f27eSRobert Love  * @skb:  The packet to be transmitted
14351875f27eSRobert Love  * @tlen: The total length of the trailer
14361875f27eSRobert Love  *
1437a703e490SVasu Dev  * Returns: 0 for success
1438a703e490SVasu Dev  */
14398597ae8bSBhanu Prakash Gollapudi static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1440a703e490SVasu Dev {
1441a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
14428597ae8bSBhanu Prakash Gollapudi 	int rc;
1443a703e490SVasu Dev 
1444a703e490SVasu Dev 	fps = &get_cpu_var(fcoe_percpu);
14458597ae8bSBhanu Prakash Gollapudi 	rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1446a703e490SVasu Dev 	put_cpu_var(fcoe_percpu);
1447a703e490SVasu Dev 
14488597ae8bSBhanu Prakash Gollapudi 	return rc;
1449a703e490SVasu Dev }
1450a703e490SVasu Dev 
1451a703e490SVasu Dev /**
14521875f27eSRobert Love  * fcoe_xmit() - Transmit a FCoE frame
14531875f27eSRobert Love  * @lport: The local port that the frame is to be transmitted for
14541875f27eSRobert Love  * @fp:	   The frame to be transmitted
1455a703e490SVasu Dev  *
1456a703e490SVasu Dev  * Return: 0 for success
1457a703e490SVasu Dev  */
14581875f27eSRobert Love int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1459a703e490SVasu Dev {
14604bb6b515SVasu Dev 	int wlen;
1461a703e490SVasu Dev 	u32 crc;
1462a703e490SVasu Dev 	struct ethhdr *eh;
1463a703e490SVasu Dev 	struct fcoe_crc_eof *cp;
1464a703e490SVasu Dev 	struct sk_buff *skb;
1465a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1466a703e490SVasu Dev 	struct fc_frame_header *fh;
1467a703e490SVasu Dev 	unsigned int hlen;		/* header length implies the version */
1468a703e490SVasu Dev 	unsigned int tlen;		/* trailer length */
1469a703e490SVasu Dev 	unsigned int elen;		/* eth header, may include vlan */
14701875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
14718597ae8bSBhanu Prakash Gollapudi 	struct fcoe_interface *fcoe = port->priv;
1472a703e490SVasu Dev 	u8 sof, eof;
1473a703e490SVasu Dev 	struct fcoe_hdr *hp;
1474a703e490SVasu Dev 
1475a703e490SVasu Dev 	WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1476a703e490SVasu Dev 
1477a703e490SVasu Dev 	fh = fc_frame_header_get(fp);
147897c8389dSJoe Eykholt 	skb = fp_skb(fp);
147997c8389dSJoe Eykholt 	wlen = skb->len / FCOE_WORD_TO_BYTE;
148097c8389dSJoe Eykholt 
14811875f27eSRobert Love 	if (!lport->link_up) {
14823caf02eeSDan Carpenter 		kfree_skb(skb);
148397c8389dSJoe Eykholt 		return 0;
1484a703e490SVasu Dev 	}
1485a703e490SVasu Dev 
14869860eeb4SJoe Eykholt 	if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
14871875f27eSRobert Love 	    fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
148897c8389dSJoe Eykholt 		return 0;
148997c8389dSJoe Eykholt 
1490a703e490SVasu Dev 	sof = fr_sof(fp);
1491a703e490SVasu Dev 	eof = fr_eof(fp);
1492a703e490SVasu Dev 
14934e57e1cbSVasu Dev 	elen = sizeof(struct ethhdr);
1494a703e490SVasu Dev 	hlen = sizeof(struct fcoe_hdr);
1495a703e490SVasu Dev 	tlen = sizeof(struct fcoe_crc_eof);
1496a703e490SVasu Dev 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1497a703e490SVasu Dev 
1498a703e490SVasu Dev 	/* crc offload */
14991875f27eSRobert Love 	if (likely(lport->crc_offload)) {
1500a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_PARTIAL;
1501a703e490SVasu Dev 		skb->csum_start = skb_headroom(skb);
1502a703e490SVasu Dev 		skb->csum_offset = skb->len;
1503a703e490SVasu Dev 		crc = 0;
1504a703e490SVasu Dev 	} else {
1505a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_NONE;
1506a703e490SVasu Dev 		crc = fcoe_fc_crc(fp);
1507a703e490SVasu Dev 	}
1508a703e490SVasu Dev 
1509014f5c3fSChris Leech 	/* copy port crc and eof to the skb buff */
1510a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1511a703e490SVasu Dev 		skb_frag_t *frag;
15128597ae8bSBhanu Prakash Gollapudi 		if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1513a703e490SVasu Dev 			kfree_skb(skb);
1514a703e490SVasu Dev 			return -ENOMEM;
1515a703e490SVasu Dev 		}
1516a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1517165c68d5SIan Campbell 		cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
1518a703e490SVasu Dev 			+ frag->page_offset;
1519a703e490SVasu Dev 	} else {
1520a703e490SVasu Dev 		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1521a703e490SVasu Dev 	}
1522a703e490SVasu Dev 
1523a703e490SVasu Dev 	memset(cp, 0, sizeof(*cp));
1524a703e490SVasu Dev 	cp->fcoe_eof = eof;
1525a703e490SVasu Dev 	cp->fcoe_crc32 = cpu_to_le32(~crc);
1526a703e490SVasu Dev 
1527a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1528a703e490SVasu Dev 		kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1529a703e490SVasu Dev 		cp = NULL;
1530a703e490SVasu Dev 	}
1531a703e490SVasu Dev 
1532014f5c3fSChris Leech 	/* adjust skb network/transport offsets to match mac/fcoe/port */
1533a703e490SVasu Dev 	skb_push(skb, elen + hlen);
1534a703e490SVasu Dev 	skb_reset_mac_header(skb);
1535a703e490SVasu Dev 	skb_reset_network_header(skb);
1536a703e490SVasu Dev 	skb->mac_len = elen;
1537a703e490SVasu Dev 	skb->protocol = htons(ETH_P_FCOE);
15383fe9a0baSChris Leech 	skb->dev = fcoe->netdev;
1539a703e490SVasu Dev 
1540a703e490SVasu Dev 	/* fill up mac and fcoe headers */
1541a703e490SVasu Dev 	eh = eth_hdr(skb);
1542a703e490SVasu Dev 	eh->h_proto = htons(ETH_P_FCOE);
15433fe9a0baSChris Leech 	memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1544cd229e42SJoe Eykholt 	if (fcoe->ctlr.map_dest)
1545cd229e42SJoe Eykholt 		memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1546a703e490SVasu Dev 
15473fe9a0baSChris Leech 	if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
15483fe9a0baSChris Leech 		memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1549a703e490SVasu Dev 	else
155011b56188SChris Leech 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1551a703e490SVasu Dev 
1552a703e490SVasu Dev 	hp = (struct fcoe_hdr *)(eh + 1);
1553a703e490SVasu Dev 	memset(hp, 0, sizeof(*hp));
1554a703e490SVasu Dev 	if (FC_FCOE_VER)
1555a703e490SVasu Dev 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1556a703e490SVasu Dev 	hp->fcoe_sof = sof;
1557a703e490SVasu Dev 
1558a703e490SVasu Dev 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
15591875f27eSRobert Love 	if (lport->seq_offload && fr_max_payload(fp)) {
1560a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1561a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1562a703e490SVasu Dev 	} else {
1563a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = 0;
1564a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = 0;
1565a703e490SVasu Dev 	}
1566a703e490SVasu Dev 	/* update tx stats: regardless if LLD fails */
1567f018b73aSJoe Eykholt 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1568a703e490SVasu Dev 	stats->TxFrames++;
1569a703e490SVasu Dev 	stats->TxWords += wlen;
1570f018b73aSJoe Eykholt 	put_cpu();
1571a703e490SVasu Dev 
1572a703e490SVasu Dev 	/* send down to lld */
15731875f27eSRobert Love 	fr_dev(fp) = lport;
1574980f5156SVasu Dev 	fcoe_port_send(port, skb);
1575a703e490SVasu Dev 	return 0;
1576a703e490SVasu Dev }
1577a703e490SVasu Dev 
1578a703e490SVasu Dev /**
15791875f27eSRobert Love  * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
15801875f27eSRobert Love  * @skb: The completed skb (argument required by destructor)
1581e7a51997SJoe Eykholt  */
1582e7a51997SJoe Eykholt static void fcoe_percpu_flush_done(struct sk_buff *skb)
1583e7a51997SJoe Eykholt {
1584e7a51997SJoe Eykholt 	complete(&fcoe_flush_completion);
1585e7a51997SJoe Eykholt }
1586e7a51997SJoe Eykholt 
1587e7a51997SJoe Eykholt /**
158852ee8321SVasu Dev  * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
158952ee8321SVasu Dev  * @lport: The local port the frame was received on
159052ee8321SVasu Dev  * @fp:	   The received frame
159152ee8321SVasu Dev  *
159252ee8321SVasu Dev  * Return: 0 on passing filtering checks
159352ee8321SVasu Dev  */
159452ee8321SVasu Dev static inline int fcoe_filter_frames(struct fc_lport *lport,
159552ee8321SVasu Dev 				     struct fc_frame *fp)
159652ee8321SVasu Dev {
159752ee8321SVasu Dev 	struct fcoe_interface *fcoe;
159852ee8321SVasu Dev 	struct fc_frame_header *fh;
159952ee8321SVasu Dev 	struct sk_buff *skb = (struct sk_buff *)fp;
160052ee8321SVasu Dev 	struct fcoe_dev_stats *stats;
160152ee8321SVasu Dev 
160252ee8321SVasu Dev 	/*
160352ee8321SVasu Dev 	 * We only check CRC if no offload is available and if it is
160452ee8321SVasu Dev 	 * it's solicited data, in which case, the FCP layer would
160552ee8321SVasu Dev 	 * check it during the copy.
160652ee8321SVasu Dev 	 */
160752ee8321SVasu Dev 	if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
160852ee8321SVasu Dev 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
160952ee8321SVasu Dev 	else
161052ee8321SVasu Dev 		fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
161152ee8321SVasu Dev 
161252ee8321SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
161352ee8321SVasu Dev 	fh = fc_frame_header_get(fp);
161452ee8321SVasu Dev 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
161552ee8321SVasu Dev 		return 0;
161652ee8321SVasu Dev 
16178597ae8bSBhanu Prakash Gollapudi 	fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
161852ee8321SVasu Dev 	if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
161952ee8321SVasu Dev 	    ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
162052ee8321SVasu Dev 		FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
162152ee8321SVasu Dev 		return -EINVAL;
162252ee8321SVasu Dev 	}
162352ee8321SVasu Dev 
1624f2f96d20SDan Carpenter 	if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
162552ee8321SVasu Dev 	    le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
162652ee8321SVasu Dev 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
162752ee8321SVasu Dev 		return 0;
162852ee8321SVasu Dev 	}
162952ee8321SVasu Dev 
163052ee8321SVasu Dev 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
163152ee8321SVasu Dev 	stats->InvalidCRCCount++;
163252ee8321SVasu Dev 	if (stats->InvalidCRCCount < 5)
163352ee8321SVasu Dev 		printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
163452ee8321SVasu Dev 	return -EINVAL;
163552ee8321SVasu Dev }
163652ee8321SVasu Dev 
163752ee8321SVasu Dev /**
1638859b7b64SChris Leech  * fcoe_recv_frame() - process a single received frame
1639859b7b64SChris Leech  * @skb: frame to process
1640a703e490SVasu Dev  */
1641859b7b64SChris Leech static void fcoe_recv_frame(struct sk_buff *skb)
1642a703e490SVasu Dev {
1643a703e490SVasu Dev 	u32 fr_len;
16441875f27eSRobert Love 	struct fc_lport *lport;
1645a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1646a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1647a703e490SVasu Dev 	struct fcoe_crc_eof crc_eof;
1648a703e490SVasu Dev 	struct fc_frame *fp;
1649014f5c3fSChris Leech 	struct fcoe_port *port;
1650a703e490SVasu Dev 	struct fcoe_hdr *hp;
1651a703e490SVasu Dev 
1652a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
16531875f27eSRobert Love 	lport = fr->fr_dev;
16541875f27eSRobert Love 	if (unlikely(!lport)) {
1655e7a51997SJoe Eykholt 		if (skb->destructor != fcoe_percpu_flush_done)
1656e7a51997SJoe Eykholt 			FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1657a703e490SVasu Dev 		kfree_skb(skb);
1658859b7b64SChris Leech 		return;
1659a703e490SVasu Dev 	}
1660a703e490SVasu Dev 
1661d5488eb9SRobert Love 	FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1662d5488eb9SRobert Love 			"head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1663a703e490SVasu Dev 			skb->len, skb->data_len,
1664a703e490SVasu Dev 			skb->head, skb->data, skb_tail_pointer(skb),
1665a703e490SVasu Dev 			skb_end_pointer(skb), skb->csum,
1666a703e490SVasu Dev 			skb->dev ? skb->dev->name : "<NULL>");
1667a703e490SVasu Dev 
16681875f27eSRobert Love 	port = lport_priv(lport);
1669a703e490SVasu Dev 	if (skb_is_nonlinear(skb))
1670a703e490SVasu Dev 		skb_linearize(skb);	/* not ideal */
1671a703e490SVasu Dev 
1672a703e490SVasu Dev 	/*
1673a703e490SVasu Dev 	 * Frame length checks and setting up the header pointers
1674a703e490SVasu Dev 	 * was done in fcoe_rcv already.
1675a703e490SVasu Dev 	 */
1676a703e490SVasu Dev 	hp = (struct fcoe_hdr *) skb_network_header(skb);
1677a703e490SVasu Dev 
1678f018b73aSJoe Eykholt 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1679a703e490SVasu Dev 	if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1680a703e490SVasu Dev 		if (stats->ErrorFrames < 5)
1681d5488eb9SRobert Love 			printk(KERN_WARNING "fcoe: FCoE version "
1682a703e490SVasu Dev 			       "mismatch: The frame has "
1683a703e490SVasu Dev 			       "version %x, but the "
1684a703e490SVasu Dev 			       "initiator supports version "
1685a703e490SVasu Dev 			       "%x\n", FC_FCOE_DECAPS_VER(hp),
1686a703e490SVasu Dev 			       FC_FCOE_VER);
1687f018b73aSJoe Eykholt 		goto drop;
1688a703e490SVasu Dev 	}
1689a703e490SVasu Dev 
1690a703e490SVasu Dev 	skb_pull(skb, sizeof(struct fcoe_hdr));
1691a703e490SVasu Dev 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1692a703e490SVasu Dev 
1693a703e490SVasu Dev 	stats->RxFrames++;
1694a703e490SVasu Dev 	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1695a703e490SVasu Dev 
1696a703e490SVasu Dev 	fp = (struct fc_frame *)skb;
1697a703e490SVasu Dev 	fc_frame_init(fp);
16981875f27eSRobert Love 	fr_dev(fp) = lport;
1699a703e490SVasu Dev 	fr_sof(fp) = hp->fcoe_sof;
1700a703e490SVasu Dev 
1701a703e490SVasu Dev 	/* Copy out the CRC and EOF trailer for access */
1702f018b73aSJoe Eykholt 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1703f018b73aSJoe Eykholt 		goto drop;
1704a703e490SVasu Dev 	fr_eof(fp) = crc_eof.fcoe_eof;
1705a703e490SVasu Dev 	fr_crc(fp) = crc_eof.fcoe_crc32;
1706f018b73aSJoe Eykholt 	if (pskb_trim(skb, fr_len))
1707f018b73aSJoe Eykholt 		goto drop;
1708a703e490SVasu Dev 
170952ee8321SVasu Dev 	if (!fcoe_filter_frames(lport, fp)) {
1710f018b73aSJoe Eykholt 		put_cpu();
17111875f27eSRobert Love 		fc_exch_recv(lport, fp);
1712f018b73aSJoe Eykholt 		return;
171352ee8321SVasu Dev 	}
1714f018b73aSJoe Eykholt drop:
1715f018b73aSJoe Eykholt 	stats->ErrorFrames++;
1716f018b73aSJoe Eykholt 	put_cpu();
1717f018b73aSJoe Eykholt 	kfree_skb(skb);
1718a703e490SVasu Dev }
1719859b7b64SChris Leech 
1720859b7b64SChris Leech /**
1721859b7b64SChris Leech  * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1722859b7b64SChris Leech  * @arg: The per-CPU context
1723859b7b64SChris Leech  *
1724859b7b64SChris Leech  * Return: 0 for success
1725859b7b64SChris Leech  */
1726859b7b64SChris Leech int fcoe_percpu_receive_thread(void *arg)
1727859b7b64SChris Leech {
1728859b7b64SChris Leech 	struct fcoe_percpu_s *p = arg;
1729859b7b64SChris Leech 	struct sk_buff *skb;
1730859b7b64SChris Leech 
1731859b7b64SChris Leech 	set_user_nice(current, -20);
1732859b7b64SChris Leech 
1733859b7b64SChris Leech 	while (!kthread_should_stop()) {
1734859b7b64SChris Leech 
1735859b7b64SChris Leech 		spin_lock_bh(&p->fcoe_rx_list.lock);
1736859b7b64SChris Leech 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1737859b7b64SChris Leech 			set_current_state(TASK_INTERRUPTIBLE);
1738859b7b64SChris Leech 			spin_unlock_bh(&p->fcoe_rx_list.lock);
1739859b7b64SChris Leech 			schedule();
1740859b7b64SChris Leech 			set_current_state(TASK_RUNNING);
1741859b7b64SChris Leech 			if (kthread_should_stop())
1742859b7b64SChris Leech 				return 0;
1743859b7b64SChris Leech 			spin_lock_bh(&p->fcoe_rx_list.lock);
1744859b7b64SChris Leech 		}
1745859b7b64SChris Leech 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1746859b7b64SChris Leech 		fcoe_recv_frame(skb);
1747859b7b64SChris Leech 	}
1748a703e490SVasu Dev 	return 0;
1749a703e490SVasu Dev }
1750a703e490SVasu Dev 
1751a703e490SVasu Dev /**
17521875f27eSRobert Love  * fcoe_dev_setup() - Setup the link change notification interface
1753a703e490SVasu Dev  */
1754b0d428adSRandy Dunlap static void fcoe_dev_setup(void)
1755a703e490SVasu Dev {
1756a703e490SVasu Dev 	register_netdevice_notifier(&fcoe_notifier);
1757a703e490SVasu Dev }
1758a703e490SVasu Dev 
1759a703e490SVasu Dev /**
17601875f27eSRobert Love  * fcoe_dev_cleanup() - Cleanup the link change notification interface
1761a703e490SVasu Dev  */
1762a703e490SVasu Dev static void fcoe_dev_cleanup(void)
1763a703e490SVasu Dev {
1764a703e490SVasu Dev 	unregister_netdevice_notifier(&fcoe_notifier);
1765a703e490SVasu Dev }
1766a703e490SVasu Dev 
1767a703e490SVasu Dev /**
17681875f27eSRobert Love  * fcoe_device_notification() - Handler for net device events
17691875f27eSRobert Love  * @notifier: The context of the notification
17701875f27eSRobert Love  * @event:    The type of event
17711875f27eSRobert Love  * @ptr:      The net device that the event was on
1772a703e490SVasu Dev  *
17731875f27eSRobert Love  * This function is called by the Ethernet driver in case of link change event.
1774a703e490SVasu Dev  *
1775a703e490SVasu Dev  * Returns: 0 for success
1776a703e490SVasu Dev  */
1777a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *notifier,
1778a703e490SVasu Dev 				    ulong event, void *ptr)
1779a703e490SVasu Dev {
17801875f27eSRobert Love 	struct fc_lport *lport = NULL;
17811d1b88dcSVasu Dev 	struct net_device *netdev = ptr;
1782014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
17832e70e241SChris Leech 	struct fcoe_port *port;
1784a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
178597c8389dSJoe Eykholt 	u32 link_possible = 1;
1786a703e490SVasu Dev 	u32 mfs;
1787a703e490SVasu Dev 	int rc = NOTIFY_OK;
1788a703e490SVasu Dev 
1789014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
179025024989SChris Leech 		if (fcoe->netdev == netdev) {
17911875f27eSRobert Love 			lport = fcoe->ctlr.lp;
1792a703e490SVasu Dev 			break;
1793a703e490SVasu Dev 		}
1794a703e490SVasu Dev 	}
17951875f27eSRobert Love 	if (!lport) {
1796a703e490SVasu Dev 		rc = NOTIFY_DONE;
1797a703e490SVasu Dev 		goto out;
1798a703e490SVasu Dev 	}
1799a703e490SVasu Dev 
1800a703e490SVasu Dev 	switch (event) {
1801a703e490SVasu Dev 	case NETDEV_DOWN:
1802a703e490SVasu Dev 	case NETDEV_GOING_DOWN:
180397c8389dSJoe Eykholt 		link_possible = 0;
1804a703e490SVasu Dev 		break;
1805a703e490SVasu Dev 	case NETDEV_UP:
1806a703e490SVasu Dev 	case NETDEV_CHANGE:
1807a703e490SVasu Dev 		break;
1808a703e490SVasu Dev 	case NETDEV_CHANGEMTU:
18097221d7e5SYi Zou 		if (netdev->features & NETIF_F_FCOE_MTU)
18107221d7e5SYi Zou 			break;
18111d1b88dcSVasu Dev 		mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1812a703e490SVasu Dev 				     sizeof(struct fcoe_crc_eof));
1813a703e490SVasu Dev 		if (mfs >= FC_MIN_MAX_FRAME)
18141875f27eSRobert Love 			fc_set_mfs(lport, mfs);
1815a703e490SVasu Dev 		break;
1816a703e490SVasu Dev 	case NETDEV_REGISTER:
1817a703e490SVasu Dev 		break;
18182e70e241SChris Leech 	case NETDEV_UNREGISTER:
18192e70e241SChris Leech 		list_del(&fcoe->list);
18202e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
18212ca32b48STejun Heo 		queue_work(fcoe_wq, &port->destroy_work);
18222e70e241SChris Leech 		goto out;
18232e70e241SChris Leech 		break;
182454a5b21dSYi Zou 	case NETDEV_FEAT_CHANGE:
182554a5b21dSYi Zou 		fcoe_netdev_features_change(lport, netdev);
182654a5b21dSYi Zou 		break;
1827a703e490SVasu Dev 	default:
18281d1b88dcSVasu Dev 		FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1829d5488eb9SRobert Love 				"from netdev netlink\n", event);
1830a703e490SVasu Dev 	}
18315e4f8fe7SRobert Love 
18325e4f8fe7SRobert Love 	fcoe_link_speed_update(lport);
18335e4f8fe7SRobert Love 
18341875f27eSRobert Love 	if (link_possible && !fcoe_link_ok(lport))
18353fe9a0baSChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
18363fe9a0baSChris Leech 	else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1837f018b73aSJoe Eykholt 		stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1838a703e490SVasu Dev 		stats->LinkFailureCount++;
1839f018b73aSJoe Eykholt 		put_cpu();
18401875f27eSRobert Love 		fcoe_clean_pending_queue(lport);
1841a703e490SVasu Dev 	}
1842a703e490SVasu Dev out:
1843a703e490SVasu Dev 	return rc;
1844a703e490SVasu Dev }
1845a703e490SVasu Dev 
1846a703e490SVasu Dev /**
184755a66d3cSVasu Dev  * fcoe_disable() - Disables a FCoE interface
184878a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
184955a66d3cSVasu Dev  *
185078a58246SYi Zou  * Called from fcoe transport.
185155a66d3cSVasu Dev  *
185255a66d3cSVasu Dev  * Returns: 0 for success
185355a66d3cSVasu Dev  */
185478a58246SYi Zou static int fcoe_disable(struct net_device *netdev)
185555a66d3cSVasu Dev {
185655a66d3cSVasu Dev 	struct fcoe_interface *fcoe;
185755a66d3cSVasu Dev 	int rc = 0;
185855a66d3cSVasu Dev 
185955a66d3cSVasu Dev 	mutex_lock(&fcoe_config_mutex);
186055a66d3cSVasu Dev 
1861ee5df628SRobert Love 	rtnl_lock();
186255a66d3cSVasu Dev 	fcoe = fcoe_hostlist_lookup_port(netdev);
186355a66d3cSVasu Dev 	rtnl_unlock();
186455a66d3cSVasu Dev 
18659ee50e48SChris Leech 	if (fcoe) {
18669ee50e48SChris Leech 		fcoe_ctlr_link_down(&fcoe->ctlr);
18679d4cbc05SVasu Dev 		fcoe_clean_pending_queue(fcoe->ctlr.lp);
18689ee50e48SChris Leech 	} else
186955a66d3cSVasu Dev 		rc = -ENODEV;
187055a66d3cSVasu Dev 
187155a66d3cSVasu Dev 	mutex_unlock(&fcoe_config_mutex);
187255a66d3cSVasu Dev 	return rc;
187355a66d3cSVasu Dev }
187455a66d3cSVasu Dev 
187555a66d3cSVasu Dev /**
187655a66d3cSVasu Dev  * fcoe_enable() - Enables a FCoE interface
187778a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
187855a66d3cSVasu Dev  *
187978a58246SYi Zou  * Called from fcoe transport.
188055a66d3cSVasu Dev  *
188155a66d3cSVasu Dev  * Returns: 0 for success
188255a66d3cSVasu Dev  */
188378a58246SYi Zou static int fcoe_enable(struct net_device *netdev)
188455a66d3cSVasu Dev {
188555a66d3cSVasu Dev 	struct fcoe_interface *fcoe;
188655a66d3cSVasu Dev 	int rc = 0;
188755a66d3cSVasu Dev 
188855a66d3cSVasu Dev 	mutex_lock(&fcoe_config_mutex);
1889ee5df628SRobert Love 	rtnl_lock();
189055a66d3cSVasu Dev 	fcoe = fcoe_hostlist_lookup_port(netdev);
189155a66d3cSVasu Dev 	rtnl_unlock();
189255a66d3cSVasu Dev 
18939d4cbc05SVasu Dev 	if (!fcoe)
189455a66d3cSVasu Dev 		rc = -ENODEV;
18959d4cbc05SVasu Dev 	else if (!fcoe_link_ok(fcoe->ctlr.lp))
18969d4cbc05SVasu Dev 		fcoe_ctlr_link_up(&fcoe->ctlr);
189755a66d3cSVasu Dev 
189855a66d3cSVasu Dev 	mutex_unlock(&fcoe_config_mutex);
189955a66d3cSVasu Dev 	return rc;
190055a66d3cSVasu Dev }
190155a66d3cSVasu Dev 
190255a66d3cSVasu Dev /**
19031875f27eSRobert Love  * fcoe_destroy() - Destroy a FCoE interface
190478a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
19051875f27eSRobert Love  *
190678a58246SYi Zou  * Called from fcoe transport
1907a703e490SVasu Dev  *
1908a703e490SVasu Dev  * Returns: 0 for success
1909a703e490SVasu Dev  */
191078a58246SYi Zou static int fcoe_destroy(struct net_device *netdev)
1911a703e490SVasu Dev {
1912030f4e00SChris Leech 	struct fcoe_interface *fcoe;
1913f04ca1b6SVasu Dev 	struct fc_lport *lport;
1914b2085a4eSNeerav Parikh 	struct fcoe_port *port;
19158eca355fSMike Christie 	int rc = 0;
1916a703e490SVasu Dev 
1917dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
1918ee5df628SRobert Love 	rtnl_lock();
19192e70e241SChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
19202e70e241SChris Leech 	if (!fcoe) {
1921a703e490SVasu Dev 		rc = -ENODEV;
192278a58246SYi Zou 		goto out_nodev;
1923a703e490SVasu Dev 	}
1924f04ca1b6SVasu Dev 	lport = fcoe->ctlr.lp;
1925b2085a4eSNeerav Parikh 	port = lport_priv(lport);
192654a5b21dSYi Zou 	list_del(&fcoe->list);
1927b2085a4eSNeerav Parikh 	queue_work(fcoe_wq, &port->destroy_work);
1928a703e490SVasu Dev out_nodev:
1929b2085a4eSNeerav Parikh 	rtnl_unlock();
1930dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
1931a703e490SVasu Dev 	return rc;
1932a703e490SVasu Dev }
1933a703e490SVasu Dev 
19341875f27eSRobert Love /**
19351875f27eSRobert Love  * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
19361875f27eSRobert Love  * @work: Handle to the FCoE port to be destroyed
19371875f27eSRobert Love  */
19382e70e241SChris Leech static void fcoe_destroy_work(struct work_struct *work)
19392e70e241SChris Leech {
19402e70e241SChris Leech 	struct fcoe_port *port;
1941b2085a4eSNeerav Parikh 	struct fcoe_interface *fcoe;
1942b2085a4eSNeerav Parikh 	int npiv = 0;
19432e70e241SChris Leech 
19442e70e241SChris Leech 	port = container_of(work, struct fcoe_port, destroy_work);
19452e70e241SChris Leech 	mutex_lock(&fcoe_config_mutex);
1946b2085a4eSNeerav Parikh 
1947b2085a4eSNeerav Parikh 	/* set if this is an NPIV port */
1948b2085a4eSNeerav Parikh 	npiv = port->lport->vport ? 1 : 0;
1949b2085a4eSNeerav Parikh 
1950b2085a4eSNeerav Parikh 	fcoe = port->priv;
19512e70e241SChris Leech 	fcoe_if_destroy(port->lport);
1952b2085a4eSNeerav Parikh 
1953b2085a4eSNeerav Parikh 	/* Do not tear down the fcoe interface for NPIV port */
1954b2085a4eSNeerav Parikh 	if (!npiv) {
1955b2085a4eSNeerav Parikh 		rtnl_lock();
1956b2085a4eSNeerav Parikh 		fcoe_interface_cleanup(fcoe);
1957b2085a4eSNeerav Parikh 		rtnl_unlock();
1958b2085a4eSNeerav Parikh 	}
1959b2085a4eSNeerav Parikh 
19602e70e241SChris Leech 	mutex_unlock(&fcoe_config_mutex);
19612e70e241SChris Leech }
19622e70e241SChris Leech 
1963a703e490SVasu Dev /**
196478a58246SYi Zou  * fcoe_match() - Check if the FCoE is supported on the given netdevice
196578a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
19661875f27eSRobert Love  *
196778a58246SYi Zou  * Called from fcoe transport.
196878a58246SYi Zou  *
196978a58246SYi Zou  * Returns: always returns true as this is the default FCoE transport,
197078a58246SYi Zou  * i.e., support all netdevs.
197178a58246SYi Zou  */
197278a58246SYi Zou static bool fcoe_match(struct net_device *netdev)
197378a58246SYi Zou {
197478a58246SYi Zou 	return true;
197578a58246SYi Zou }
197678a58246SYi Zou 
197778a58246SYi Zou /**
197878a58246SYi Zou  * fcoe_create() - Create a fcoe interface
197978a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
198078a58246SYi Zou  * @fip_mode: The FIP mode for this creation
198178a58246SYi Zou  *
198278a58246SYi Zou  * Called from fcoe transport
1983a703e490SVasu Dev  *
1984a703e490SVasu Dev  * Returns: 0 for success
1985a703e490SVasu Dev  */
198678a58246SYi Zou static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
1987a703e490SVasu Dev {
1988b2085a4eSNeerav Parikh 	int rc = 0;
1989030f4e00SChris Leech 	struct fcoe_interface *fcoe;
1990af7f85d9SChris Leech 	struct fc_lport *lport;
1991a703e490SVasu Dev 
1992dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
1993ee5df628SRobert Love 	rtnl_lock();
199434ce27bcSVasu Dev 
1995a703e490SVasu Dev 	/* look for existing lport */
1996a703e490SVasu Dev 	if (fcoe_hostlist_lookup(netdev)) {
1997a703e490SVasu Dev 		rc = -EEXIST;
199878a58246SYi Zou 		goto out_nodev;
1999a703e490SVasu Dev 	}
2000a703e490SVasu Dev 
20011dd454d9SJoe Eykholt 	fcoe = fcoe_interface_create(netdev, fip_mode);
20027287fb91SRobert Love 	if (IS_ERR(fcoe)) {
20037287fb91SRobert Love 		rc = PTR_ERR(fcoe);
200478a58246SYi Zou 		goto out_nodev;
2005030f4e00SChris Leech 	}
2006030f4e00SChris Leech 
20079a05753bSChris Leech 	lport = fcoe_if_create(fcoe, &netdev->dev, 0);
2008af7f85d9SChris Leech 	if (IS_ERR(lport)) {
2009d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2010a703e490SVasu Dev 		       netdev->name);
2011a703e490SVasu Dev 		rc = -EIO;
20122e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
2013b2085a4eSNeerav Parikh 		goto out_nodev;
2014a703e490SVasu Dev 	}
2015030f4e00SChris Leech 
201654b649f8SChris Leech 	/* Make this the "master" N_Port */
201754b649f8SChris Leech 	fcoe->ctlr.lp = lport;
2018030f4e00SChris Leech 
2019c863df33SChris Leech 	/* add to lports list */
2020c863df33SChris Leech 	fcoe_hostlist_add(lport);
2021c863df33SChris Leech 
202254b649f8SChris Leech 	/* start FIP Discovery and FLOGI */
202354b649f8SChris Leech 	lport->boot_time = jiffies;
202454b649f8SChris Leech 	fc_fabric_login(lport);
202554b649f8SChris Leech 	if (!fcoe_link_ok(lport))
202654b649f8SChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
202754b649f8SChris Leech 
2028a703e490SVasu Dev out_nodev:
202934ce27bcSVasu Dev 	rtnl_unlock();
2030dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2031a703e490SVasu Dev 	return rc;
2032a703e490SVasu Dev }
2033a703e490SVasu Dev 
2034a703e490SVasu Dev /**
20355e4f8fe7SRobert Love  * fcoe_link_speed_update() - Update the supported and actual link speeds
20365e4f8fe7SRobert Love  * @lport: The local port to update speeds for
2037a703e490SVasu Dev  *
20385e4f8fe7SRobert Love  * Returns: 0 if the ethtool query was successful
20395e4f8fe7SRobert Love  *          -1 if the ethtool query failed
2040a703e490SVasu Dev  */
20415e4f8fe7SRobert Love int fcoe_link_speed_update(struct fc_lport *lport)
2042a703e490SVasu Dev {
20438597ae8bSBhanu Prakash Gollapudi 	struct net_device *netdev = fcoe_netdev(lport);
20448ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd;
2045a703e490SVasu Dev 
20465e4f8fe7SRobert Love 	if (!dev_ethtool_get_settings(netdev, &ecmd)) {
20471875f27eSRobert Love 		lport->link_supported_speeds &=
2048a703e490SVasu Dev 			~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2049a703e490SVasu Dev 		if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2050a703e490SVasu Dev 				      SUPPORTED_1000baseT_Full))
20511875f27eSRobert Love 			lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2052a703e490SVasu Dev 		if (ecmd.supported & SUPPORTED_10000baseT_Full)
20531875f27eSRobert Love 			lport->link_supported_speeds |=
2054a703e490SVasu Dev 				FC_PORTSPEED_10GBIT;
20558ae6dacaSDavid Decotigny 		switch (ethtool_cmd_speed(&ecmd)) {
20568ae6dacaSDavid Decotigny 		case SPEED_1000:
20571875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_1GBIT;
20588ae6dacaSDavid Decotigny 			break;
20598ae6dacaSDavid Decotigny 		case SPEED_10000:
20601875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_10GBIT;
20618ae6dacaSDavid Decotigny 			break;
20628ae6dacaSDavid Decotigny 		}
20632f718d64SYi Zou 		return 0;
20642f718d64SYi Zou 	}
20652f718d64SYi Zou 	return -1;
2066a703e490SVasu Dev }
2067a703e490SVasu Dev 
2068a703e490SVasu Dev /**
20695e4f8fe7SRobert Love  * fcoe_link_ok() - Check if the link is OK for a local port
20705e4f8fe7SRobert Love  * @lport: The local port to check link on
20715e4f8fe7SRobert Love  *
20725e4f8fe7SRobert Love  * Returns: 0 if link is UP and OK, -1 if not
20735e4f8fe7SRobert Love  *
20745e4f8fe7SRobert Love  */
20755e4f8fe7SRobert Love int fcoe_link_ok(struct fc_lport *lport)
20765e4f8fe7SRobert Love {
20778597ae8bSBhanu Prakash Gollapudi 	struct net_device *netdev = fcoe_netdev(lport);
20785e4f8fe7SRobert Love 
20795e4f8fe7SRobert Love 	if (netif_oper_up(netdev))
20805e4f8fe7SRobert Love 		return 0;
20815e4f8fe7SRobert Love 	return -1;
20825e4f8fe7SRobert Love }
20835e4f8fe7SRobert Love 
20845e4f8fe7SRobert Love /**
20851875f27eSRobert Love  * fcoe_percpu_clean() - Clear all pending skbs for an local port
20861875f27eSRobert Love  * @lport: The local port whose skbs are to be cleared
2087e7a51997SJoe Eykholt  *
2088e7a51997SJoe Eykholt  * Must be called with fcoe_create_mutex held to single-thread completion.
2089e7a51997SJoe Eykholt  *
2090e7a51997SJoe Eykholt  * This flushes the pending skbs by adding a new skb to each queue and
2091e7a51997SJoe Eykholt  * waiting until they are all freed.  This assures us that not only are
2092e7a51997SJoe Eykholt  * there no packets that will be handled by the lport, but also that any
2093e7a51997SJoe Eykholt  * threads already handling packet have returned.
2094a703e490SVasu Dev  */
20951875f27eSRobert Love void fcoe_percpu_clean(struct fc_lport *lport)
2096a703e490SVasu Dev {
2097a703e490SVasu Dev 	struct fcoe_percpu_s *pp;
2098a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
2099a703e490SVasu Dev 	struct sk_buff_head *list;
2100a703e490SVasu Dev 	struct sk_buff *skb, *next;
2101a703e490SVasu Dev 	struct sk_buff *head;
2102a703e490SVasu Dev 	unsigned int cpu;
2103a703e490SVasu Dev 
2104a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2105a703e490SVasu Dev 		pp = &per_cpu(fcoe_percpu, cpu);
2106a703e490SVasu Dev 		spin_lock_bh(&pp->fcoe_rx_list.lock);
2107a703e490SVasu Dev 		list = &pp->fcoe_rx_list;
2108a703e490SVasu Dev 		head = list->next;
2109a703e490SVasu Dev 		for (skb = head; skb != (struct sk_buff *)list;
2110a703e490SVasu Dev 		     skb = next) {
2111a703e490SVasu Dev 			next = skb->next;
2112a703e490SVasu Dev 			fr = fcoe_dev_from_skb(skb);
21131875f27eSRobert Love 			if (fr->fr_dev == lport) {
2114a703e490SVasu Dev 				__skb_unlink(skb, list);
2115a703e490SVasu Dev 				kfree_skb(skb);
2116a703e490SVasu Dev 			}
2117a703e490SVasu Dev 		}
2118e7a51997SJoe Eykholt 
2119e7a51997SJoe Eykholt 		if (!pp->thread || !cpu_online(cpu)) {
2120a703e490SVasu Dev 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2121e7a51997SJoe Eykholt 			continue;
2122e7a51997SJoe Eykholt 		}
2123e7a51997SJoe Eykholt 
2124e7a51997SJoe Eykholt 		skb = dev_alloc_skb(0);
2125e7a51997SJoe Eykholt 		if (!skb) {
2126e7a51997SJoe Eykholt 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2127e7a51997SJoe Eykholt 			continue;
2128e7a51997SJoe Eykholt 		}
2129e7a51997SJoe Eykholt 		skb->destructor = fcoe_percpu_flush_done;
2130e7a51997SJoe Eykholt 
2131e7a51997SJoe Eykholt 		__skb_queue_tail(&pp->fcoe_rx_list, skb);
2132e7a51997SJoe Eykholt 		if (pp->fcoe_rx_list.qlen == 1)
2133e7a51997SJoe Eykholt 			wake_up_process(pp->thread);
2134e7a51997SJoe Eykholt 		spin_unlock_bh(&pp->fcoe_rx_list.lock);
2135e7a51997SJoe Eykholt 
2136e7a51997SJoe Eykholt 		wait_for_completion(&fcoe_flush_completion);
2137a703e490SVasu Dev 	}
2138a703e490SVasu Dev }
2139a703e490SVasu Dev 
2140a703e490SVasu Dev /**
21411875f27eSRobert Love  * fcoe_reset() - Reset a local port
21421875f27eSRobert Love  * @shost: The SCSI host associated with the local port to be reset
2143a703e490SVasu Dev  *
21441875f27eSRobert Love  * Returns: Always 0 (return value required by FC transport template)
2145a703e490SVasu Dev  */
2146a703e490SVasu Dev int fcoe_reset(struct Scsi_Host *shost)
2147a703e490SVasu Dev {
2148a703e490SVasu Dev 	struct fc_lport *lport = shost_priv(shost);
2149d2f80952SVasu Dev 	struct fcoe_port *port = lport_priv(lport);
2150d2f80952SVasu Dev 	struct fcoe_interface *fcoe = port->priv;
2151d2f80952SVasu Dev 
2152d2f80952SVasu Dev 	fcoe_ctlr_link_down(&fcoe->ctlr);
2153d2f80952SVasu Dev 	fcoe_clean_pending_queue(fcoe->ctlr.lp);
2154d2f80952SVasu Dev 	if (!fcoe_link_ok(fcoe->ctlr.lp))
2155d2f80952SVasu Dev 		fcoe_ctlr_link_up(&fcoe->ctlr);
2156a703e490SVasu Dev 	return 0;
2157a703e490SVasu Dev }
2158a703e490SVasu Dev 
2159a703e490SVasu Dev /**
21601875f27eSRobert Love  * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
21611875f27eSRobert Love  * @netdev: The net device used as a key
2162a703e490SVasu Dev  *
21631875f27eSRobert Love  * Locking: Must be called with the RNL mutex held.
21641875f27eSRobert Love  *
21651875f27eSRobert Love  * Returns: NULL or the FCoE interface
2166a703e490SVasu Dev  */
2167014f5c3fSChris Leech static struct fcoe_interface *
21681875f27eSRobert Love fcoe_hostlist_lookup_port(const struct net_device *netdev)
2169a703e490SVasu Dev {
2170014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2171a703e490SVasu Dev 
2172014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
21731875f27eSRobert Love 		if (fcoe->netdev == netdev)
2174014f5c3fSChris Leech 			return fcoe;
2175a703e490SVasu Dev 	}
2176a703e490SVasu Dev 	return NULL;
2177a703e490SVasu Dev }
2178a703e490SVasu Dev 
2179a703e490SVasu Dev /**
21801875f27eSRobert Love  * fcoe_hostlist_lookup() - Find the local port associated with a
21811875f27eSRobert Love  *			    given net device
21821875f27eSRobert Love  * @netdev: The netdevice used as a key
2183a703e490SVasu Dev  *
21841875f27eSRobert Love  * Locking: Must be called with the RTNL mutex held
21851875f27eSRobert Love  *
21861875f27eSRobert Love  * Returns: NULL or the local port
2187a703e490SVasu Dev  */
2188090eb6c4SChris Leech static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2189a703e490SVasu Dev {
2190014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2191a703e490SVasu Dev 
2192014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
21933fe9a0baSChris Leech 	return (fcoe) ? fcoe->ctlr.lp : NULL;
2194a703e490SVasu Dev }
2195a703e490SVasu Dev 
2196a703e490SVasu Dev /**
21971875f27eSRobert Love  * fcoe_hostlist_add() - Add the FCoE interface identified by a local
21981875f27eSRobert Love  *			 port to the hostlist
21991875f27eSRobert Love  * @lport: The local port that identifies the FCoE interface to be added
22001875f27eSRobert Love  *
22011875f27eSRobert Love  * Locking: must be called with the RTNL mutex held
2202a703e490SVasu Dev  *
2203a703e490SVasu Dev  * Returns: 0 for success
2204a703e490SVasu Dev  */
2205090eb6c4SChris Leech static int fcoe_hostlist_add(const struct fc_lport *lport)
2206a703e490SVasu Dev {
2207014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2208014f5c3fSChris Leech 	struct fcoe_port *port;
2209a703e490SVasu Dev 
2210014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2211014f5c3fSChris Leech 	if (!fcoe) {
2212014f5c3fSChris Leech 		port = lport_priv(lport);
22138597ae8bSBhanu Prakash Gollapudi 		fcoe = port->priv;
2214014f5c3fSChris Leech 		list_add_tail(&fcoe->list, &fcoe_hostlist);
2215a703e490SVasu Dev 	}
2216a703e490SVasu Dev 	return 0;
2217a703e490SVasu Dev }
2218a703e490SVasu Dev 
221978a58246SYi Zou 
222078a58246SYi Zou static struct fcoe_transport fcoe_sw_transport = {
222178a58246SYi Zou 	.name = {FCOE_TRANSPORT_DEFAULT},
222278a58246SYi Zou 	.attached = false,
222378a58246SYi Zou 	.list = LIST_HEAD_INIT(fcoe_sw_transport.list),
222478a58246SYi Zou 	.match = fcoe_match,
222578a58246SYi Zou 	.create = fcoe_create,
222678a58246SYi Zou 	.destroy = fcoe_destroy,
222778a58246SYi Zou 	.enable = fcoe_enable,
222878a58246SYi Zou 	.disable = fcoe_disable,
222978a58246SYi Zou };
223078a58246SYi Zou 
2231a703e490SVasu Dev /**
22321875f27eSRobert Love  * fcoe_init() - Initialize fcoe.ko
2233a703e490SVasu Dev  *
22341875f27eSRobert Love  * Returns: 0 on success, or a negative value on failure
2235a703e490SVasu Dev  */
2236a703e490SVasu Dev static int __init fcoe_init(void)
2237a703e490SVasu Dev {
22381875f27eSRobert Love 	struct fcoe_percpu_s *p;
2239a703e490SVasu Dev 	unsigned int cpu;
2240a703e490SVasu Dev 	int rc = 0;
2241a703e490SVasu Dev 
22422ca32b48STejun Heo 	fcoe_wq = alloc_workqueue("fcoe", 0, 0);
22432ca32b48STejun Heo 	if (!fcoe_wq)
22442ca32b48STejun Heo 		return -ENOMEM;
22452ca32b48STejun Heo 
224678a58246SYi Zou 	/* register as a fcoe transport */
224778a58246SYi Zou 	rc = fcoe_transport_attach(&fcoe_sw_transport);
224878a58246SYi Zou 	if (rc) {
224978a58246SYi Zou 		printk(KERN_ERR "failed to register an fcoe transport, check "
225078a58246SYi Zou 			"if libfcoe is loaded\n");
225178a58246SYi Zou 		return rc;
225278a58246SYi Zou 	}
225378a58246SYi Zou 
2254dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2255dfc1d0feSChris Leech 
2256a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2257a703e490SVasu Dev 		p = &per_cpu(fcoe_percpu, cpu);
2258a703e490SVasu Dev 		skb_queue_head_init(&p->fcoe_rx_list);
2259a703e490SVasu Dev 	}
2260a703e490SVasu Dev 
2261a703e490SVasu Dev 	for_each_online_cpu(cpu)
2262a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
2263a703e490SVasu Dev 
2264a703e490SVasu Dev 	/* Initialize per CPU interrupt thread */
2265a703e490SVasu Dev 	rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2266a703e490SVasu Dev 	if (rc)
2267a703e490SVasu Dev 		goto out_free;
2268a703e490SVasu Dev 
2269a703e490SVasu Dev 	/* Setup link change notification */
2270a703e490SVasu Dev 	fcoe_dev_setup();
2271a703e490SVasu Dev 
22725892c32fSChris Leech 	rc = fcoe_if_init();
22735892c32fSChris Leech 	if (rc)
22745892c32fSChris Leech 		goto out_free;
2275a703e490SVasu Dev 
2276dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2277a703e490SVasu Dev 	return 0;
2278a703e490SVasu Dev 
2279a703e490SVasu Dev out_free:
2280a703e490SVasu Dev 	for_each_online_cpu(cpu) {
2281a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2282a703e490SVasu Dev 	}
2283dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
22842ca32b48STejun Heo 	destroy_workqueue(fcoe_wq);
2285a703e490SVasu Dev 	return rc;
2286a703e490SVasu Dev }
2287a703e490SVasu Dev module_init(fcoe_init);
2288a703e490SVasu Dev 
2289a703e490SVasu Dev /**
22901875f27eSRobert Love  * fcoe_exit() - Clean up fcoe.ko
2291a703e490SVasu Dev  *
22921875f27eSRobert Love  * Returns: 0 on success or a  negative value on failure
2293a703e490SVasu Dev  */
2294a703e490SVasu Dev static void __exit fcoe_exit(void)
2295a703e490SVasu Dev {
2296014f5c3fSChris Leech 	struct fcoe_interface *fcoe, *tmp;
22972e70e241SChris Leech 	struct fcoe_port *port;
22981875f27eSRobert Love 	unsigned int cpu;
2299a703e490SVasu Dev 
2300dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2301dfc1d0feSChris Leech 
2302a703e490SVasu Dev 	fcoe_dev_cleanup();
2303a703e490SVasu Dev 
2304a703e490SVasu Dev 	/* releases the associated fcoe hosts */
2305090eb6c4SChris Leech 	rtnl_lock();
2306090eb6c4SChris Leech 	list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2307c863df33SChris Leech 		list_del(&fcoe->list);
23082e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
23092ca32b48STejun Heo 		queue_work(fcoe_wq, &port->destroy_work);
2310c863df33SChris Leech 	}
2311090eb6c4SChris Leech 	rtnl_unlock();
2312a703e490SVasu Dev 
2313a703e490SVasu Dev 	unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2314a703e490SVasu Dev 
2315014f5c3fSChris Leech 	for_each_online_cpu(cpu)
2316a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2317a703e490SVasu Dev 
2318dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
23192e70e241SChris Leech 
23202ca32b48STejun Heo 	/*
23212ca32b48STejun Heo 	 * destroy_work's may be chained but destroy_workqueue()
23222ca32b48STejun Heo 	 * can take care of them. Just kill the fcoe_wq.
23232ca32b48STejun Heo 	 */
23242ca32b48STejun Heo 	destroy_workqueue(fcoe_wq);
23252e70e241SChris Leech 
23262ca32b48STejun Heo 	/*
23272ca32b48STejun Heo 	 * Detaching from the scsi transport must happen after all
23282ca32b48STejun Heo 	 * destroys are done on the fcoe_wq. destroy_workqueue will
23292ca32b48STejun Heo 	 * enusre the fcoe_wq is flushed.
23302ca32b48STejun Heo 	 */
23312e70e241SChris Leech 	fcoe_if_exit();
233278a58246SYi Zou 
233378a58246SYi Zou 	/* detach from fcoe transport */
233478a58246SYi Zou 	fcoe_transport_detach(&fcoe_sw_transport);
2335a703e490SVasu Dev }
2336a703e490SVasu Dev module_exit(fcoe_exit);
233711b56188SChris Leech 
233811b56188SChris Leech /**
233911b56188SChris Leech  * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
234011b56188SChris Leech  * @seq: active sequence in the FLOGI or FDISC exchange
234111b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
234211b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
234311b56188SChris Leech  *
234465155b37SUwe Kleine-König  * This handles MAC address management for FCoE, then passes control on to
234511b56188SChris Leech  * the libfc FLOGI response handler.
234611b56188SChris Leech  */
234711b56188SChris Leech static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
234811b56188SChris Leech {
234911b56188SChris Leech 	struct fcoe_ctlr *fip = arg;
235011b56188SChris Leech 	struct fc_exch *exch = fc_seq_exch(seq);
235111b56188SChris Leech 	struct fc_lport *lport = exch->lp;
235211b56188SChris Leech 	u8 *mac;
235311b56188SChris Leech 
235411b56188SChris Leech 	if (IS_ERR(fp))
235511b56188SChris Leech 		goto done;
235611b56188SChris Leech 
235711b56188SChris Leech 	mac = fr_cb(fp)->granted_mac;
235811b56188SChris Leech 	if (is_zero_ether_addr(mac)) {
235911b56188SChris Leech 		/* pre-FIP */
2360386309ceSJoe Eykholt 		if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
236111b56188SChris Leech 			fc_frame_free(fp);
236211b56188SChris Leech 			return;
236311b56188SChris Leech 		}
236411b56188SChris Leech 	}
2365386309ceSJoe Eykholt 	fcoe_update_src_mac(lport, mac);
236611b56188SChris Leech done:
236711b56188SChris Leech 	fc_lport_flogi_resp(seq, fp, lport);
236811b56188SChris Leech }
236911b56188SChris Leech 
237011b56188SChris Leech /**
237111b56188SChris Leech  * fcoe_logo_resp() - FCoE specific LOGO response handler
237211b56188SChris Leech  * @seq: active sequence in the LOGO exchange
237311b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
237411b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
237511b56188SChris Leech  *
237665155b37SUwe Kleine-König  * This handles MAC address management for FCoE, then passes control on to
237711b56188SChris Leech  * the libfc LOGO response handler.
237811b56188SChris Leech  */
237911b56188SChris Leech static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
238011b56188SChris Leech {
2381386309ceSJoe Eykholt 	struct fc_lport *lport = arg;
238211b56188SChris Leech 	static u8 zero_mac[ETH_ALEN] = { 0 };
238311b56188SChris Leech 
238411b56188SChris Leech 	if (!IS_ERR(fp))
2385386309ceSJoe Eykholt 		fcoe_update_src_mac(lport, zero_mac);
238611b56188SChris Leech 	fc_lport_logo_resp(seq, fp, lport);
238711b56188SChris Leech }
238811b56188SChris Leech 
238911b56188SChris Leech /**
239011b56188SChris Leech  * fcoe_elsct_send - FCoE specific ELS handler
239111b56188SChris Leech  *
239211b56188SChris Leech  * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
239311b56188SChris Leech  * using FCoE specific response handlers and passing the FIP controller as
239411b56188SChris Leech  * the argument (the lport is still available from the exchange).
239511b56188SChris Leech  *
239611b56188SChris Leech  * Most of the work here is just handed off to the libfc routine.
239711b56188SChris Leech  */
23981875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
23991875f27eSRobert Love 				      struct fc_frame *fp, unsigned int op,
24001875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
24011875f27eSRobert Love 						   struct fc_frame *,
24021875f27eSRobert Love 						   void *),
240311b56188SChris Leech 				      void *arg, u32 timeout)
240411b56188SChris Leech {
240511b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
24068597ae8bSBhanu Prakash Gollapudi 	struct fcoe_interface *fcoe = port->priv;
240711b56188SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
240811b56188SChris Leech 	struct fc_frame_header *fh = fc_frame_header_get(fp);
240911b56188SChris Leech 
241011b56188SChris Leech 	switch (op) {
241111b56188SChris Leech 	case ELS_FLOGI:
241211b56188SChris Leech 	case ELS_FDISC:
2413e10f8c66SJoe Eykholt 		if (lport->point_to_multipoint)
2414e10f8c66SJoe Eykholt 			break;
241511b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
241611b56188SChris Leech 				     fip, timeout);
241711b56188SChris Leech 	case ELS_LOGO:
241811b56188SChris Leech 		/* only hook onto fabric logouts, not port logouts */
241911b56188SChris Leech 		if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
242011b56188SChris Leech 			break;
242111b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2422386309ceSJoe Eykholt 				     lport, timeout);
242311b56188SChris Leech 	}
242411b56188SChris Leech 	return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
242511b56188SChris Leech }
242611b56188SChris Leech 
24279a05753bSChris Leech /**
24289a05753bSChris Leech  * fcoe_vport_create() - create an fc_host/scsi_host for a vport
24299a05753bSChris Leech  * @vport: fc_vport object to create a new fc_host for
24309a05753bSChris Leech  * @disabled: start the new fc_host in a disabled state by default?
24319a05753bSChris Leech  *
24329a05753bSChris Leech  * Returns: 0 for success
24339a05753bSChris Leech  */
24349a05753bSChris Leech static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
24359a05753bSChris Leech {
24369a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
24379a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
24389a05753bSChris Leech 	struct fcoe_port *port = lport_priv(n_port);
24398597ae8bSBhanu Prakash Gollapudi 	struct fcoe_interface *fcoe = port->priv;
24409a05753bSChris Leech 	struct net_device *netdev = fcoe->netdev;
24419a05753bSChris Leech 	struct fc_lport *vn_port;
2442bdf25218SNeerav Parikh 	int rc;
2443bdf25218SNeerav Parikh 	char buf[32];
2444bdf25218SNeerav Parikh 
2445bdf25218SNeerav Parikh 	rc = fcoe_validate_vport_create(vport);
2446bdf25218SNeerav Parikh 	if (rc) {
2447bdf25218SNeerav Parikh 		wwn_to_str(vport->port_name, buf, sizeof(buf));
2448bdf25218SNeerav Parikh 		printk(KERN_ERR "fcoe: Failed to create vport, "
2449bdf25218SNeerav Parikh 			"WWPN (0x%s) already exists\n",
2450bdf25218SNeerav Parikh 			buf);
2451bdf25218SNeerav Parikh 		return rc;
2452bdf25218SNeerav Parikh 	}
24539a05753bSChris Leech 
24549a05753bSChris Leech 	mutex_lock(&fcoe_config_mutex);
24559a05753bSChris Leech 	vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
24569a05753bSChris Leech 	mutex_unlock(&fcoe_config_mutex);
24579a05753bSChris Leech 
24589a05753bSChris Leech 	if (IS_ERR(vn_port)) {
24599a05753bSChris Leech 		printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
24609a05753bSChris Leech 		       netdev->name);
24619a05753bSChris Leech 		return -EIO;
24629a05753bSChris Leech 	}
24639a05753bSChris Leech 
24649a05753bSChris Leech 	if (disabled) {
24659a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
24669a05753bSChris Leech 	} else {
24679a05753bSChris Leech 		vn_port->boot_time = jiffies;
24689a05753bSChris Leech 		fc_fabric_login(vn_port);
24699a05753bSChris Leech 		fc_vport_setlink(vn_port);
24709a05753bSChris Leech 	}
24719a05753bSChris Leech 	return 0;
24729a05753bSChris Leech }
24739a05753bSChris Leech 
24749a05753bSChris Leech /**
24759a05753bSChris Leech  * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
24769a05753bSChris Leech  * @vport: fc_vport object that is being destroyed
24779a05753bSChris Leech  *
24789a05753bSChris Leech  * Returns: 0 for success
24799a05753bSChris Leech  */
24809a05753bSChris Leech static int fcoe_vport_destroy(struct fc_vport *vport)
24819a05753bSChris Leech {
24829a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
24839a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
24849a05753bSChris Leech 	struct fc_lport *vn_port = vport->dd_data;
24859a05753bSChris Leech 	struct fcoe_port *port = lport_priv(vn_port);
24869a05753bSChris Leech 
24879a05753bSChris Leech 	mutex_lock(&n_port->lp_mutex);
24889a05753bSChris Leech 	list_del(&vn_port->list);
24899a05753bSChris Leech 	mutex_unlock(&n_port->lp_mutex);
24902ca32b48STejun Heo 	queue_work(fcoe_wq, &port->destroy_work);
24919a05753bSChris Leech 	return 0;
24929a05753bSChris Leech }
24939a05753bSChris Leech 
24949a05753bSChris Leech /**
24959a05753bSChris Leech  * fcoe_vport_disable() - change vport state
24969a05753bSChris Leech  * @vport: vport to bring online/offline
24979a05753bSChris Leech  * @disable: should the vport be disabled?
24989a05753bSChris Leech  */
24999a05753bSChris Leech static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
25009a05753bSChris Leech {
25019a05753bSChris Leech 	struct fc_lport *lport = vport->dd_data;
25029a05753bSChris Leech 
25039a05753bSChris Leech 	if (disable) {
25049a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
25059a05753bSChris Leech 		fc_fabric_logoff(lport);
25069a05753bSChris Leech 	} else {
25079a05753bSChris Leech 		lport->boot_time = jiffies;
25089a05753bSChris Leech 		fc_fabric_login(lport);
25099a05753bSChris Leech 		fc_vport_setlink(lport);
25109a05753bSChris Leech 	}
25119a05753bSChris Leech 
25129a05753bSChris Leech 	return 0;
25139a05753bSChris Leech }
25149a05753bSChris Leech 
2515dc8596d3SChris Leech /**
2516dc8596d3SChris Leech  * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2517dc8596d3SChris Leech  * @vport: fc_vport with a new symbolic name string
2518dc8596d3SChris Leech  *
2519dc8596d3SChris Leech  * After generating a new symbolic name string, a new RSPN_ID request is
2520dc8596d3SChris Leech  * sent to the name server.  There is no response handler, so if it fails
2521dc8596d3SChris Leech  * for some reason it will not be retried.
2522dc8596d3SChris Leech  */
2523dc8596d3SChris Leech static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2524dc8596d3SChris Leech {
2525dc8596d3SChris Leech 	struct fc_lport *lport = vport->dd_data;
2526dc8596d3SChris Leech 	struct fc_frame *fp;
2527dc8596d3SChris Leech 	size_t len;
2528dc8596d3SChris Leech 
2529dc8596d3SChris Leech 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2530dc8596d3SChris Leech 		 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2531dc8596d3SChris Leech 		 fcoe_netdev(lport)->name, vport->symbolic_name);
2532dc8596d3SChris Leech 
2533dc8596d3SChris Leech 	if (lport->state != LPORT_ST_READY)
2534dc8596d3SChris Leech 		return;
2535dc8596d3SChris Leech 
2536dc8596d3SChris Leech 	len = strnlen(fc_host_symbolic_name(lport->host), 255);
2537dc8596d3SChris Leech 	fp = fc_frame_alloc(lport,
2538dc8596d3SChris Leech 			    sizeof(struct fc_ct_hdr) +
2539dc8596d3SChris Leech 			    sizeof(struct fc_ns_rspn) + len);
2540dc8596d3SChris Leech 	if (!fp)
2541dc8596d3SChris Leech 		return;
2542dc8596d3SChris Leech 	lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2543b94f8951SJoe Eykholt 			     NULL, NULL, 3 * lport->r_a_tov);
2544dc8596d3SChris Leech }
2545b84056bfSYi Zou 
2546b84056bfSYi Zou /**
2547b84056bfSYi Zou  * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2548b84056bfSYi Zou  * @lport: the local port
2549b84056bfSYi Zou  * @fc_lesb: the link error status block
2550b84056bfSYi Zou  */
2551b84056bfSYi Zou static void fcoe_get_lesb(struct fc_lport *lport,
2552b84056bfSYi Zou 			 struct fc_els_lesb *fc_lesb)
2553b84056bfSYi Zou {
2554b84056bfSYi Zou 	unsigned int cpu;
2555b84056bfSYi Zou 	u32 lfc, vlfc, mdac;
2556b84056bfSYi Zou 	struct fcoe_dev_stats *devst;
2557b84056bfSYi Zou 	struct fcoe_fc_els_lesb *lesb;
255828172739SEric Dumazet 	struct rtnl_link_stats64 temp;
2559b84056bfSYi Zou 	struct net_device *netdev = fcoe_netdev(lport);
2560b84056bfSYi Zou 
2561b84056bfSYi Zou 	lfc = 0;
2562b84056bfSYi Zou 	vlfc = 0;
2563b84056bfSYi Zou 	mdac = 0;
2564b84056bfSYi Zou 	lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2565b84056bfSYi Zou 	memset(lesb, 0, sizeof(*lesb));
2566b84056bfSYi Zou 	for_each_possible_cpu(cpu) {
2567b84056bfSYi Zou 		devst = per_cpu_ptr(lport->dev_stats, cpu);
2568b84056bfSYi Zou 		lfc += devst->LinkFailureCount;
2569b84056bfSYi Zou 		vlfc += devst->VLinkFailureCount;
2570b84056bfSYi Zou 		mdac += devst->MissDiscAdvCount;
2571b84056bfSYi Zou 	}
2572b84056bfSYi Zou 	lesb->lesb_link_fail = htonl(lfc);
2573b84056bfSYi Zou 	lesb->lesb_vlink_fail = htonl(vlfc);
2574b84056bfSYi Zou 	lesb->lesb_miss_fka = htonl(mdac);
257528172739SEric Dumazet 	lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2576b84056bfSYi Zou }
25777d65b0dfSJoe Eykholt 
25787d65b0dfSJoe Eykholt /**
25797d65b0dfSJoe Eykholt  * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
25807d65b0dfSJoe Eykholt  * @lport: the local port
25817d65b0dfSJoe Eykholt  * @port_id: the port ID
25827d65b0dfSJoe Eykholt  * @fp: the received frame, if any, that caused the port_id to be set.
25837d65b0dfSJoe Eykholt  *
25847d65b0dfSJoe Eykholt  * This routine handles the case where we received a FLOGI and are
25857d65b0dfSJoe Eykholt  * entering point-to-point mode.  We need to call fcoe_ctlr_recv_flogi()
25867d65b0dfSJoe Eykholt  * so it can set the non-mapped mode and gateway address.
25877d65b0dfSJoe Eykholt  *
25887d65b0dfSJoe Eykholt  * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
25897d65b0dfSJoe Eykholt  */
25907d65b0dfSJoe Eykholt static void fcoe_set_port_id(struct fc_lport *lport,
25917d65b0dfSJoe Eykholt 			     u32 port_id, struct fc_frame *fp)
25927d65b0dfSJoe Eykholt {
25937d65b0dfSJoe Eykholt 	struct fcoe_port *port = lport_priv(lport);
25948597ae8bSBhanu Prakash Gollapudi 	struct fcoe_interface *fcoe = port->priv;
25957d65b0dfSJoe Eykholt 
25967d65b0dfSJoe Eykholt 	if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
25977d65b0dfSJoe Eykholt 		fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
25987d65b0dfSJoe Eykholt }
2599bdf25218SNeerav Parikh 
2600bdf25218SNeerav Parikh /**
2601bdf25218SNeerav Parikh  * fcoe_validate_vport_create() - Validate a vport before creating it
2602bdf25218SNeerav Parikh  * @vport: NPIV port to be created
2603bdf25218SNeerav Parikh  *
2604bdf25218SNeerav Parikh  * This routine is meant to add validation for a vport before creating it
2605bdf25218SNeerav Parikh  * via fcoe_vport_create().
2606bdf25218SNeerav Parikh  * Current validations are:
2607bdf25218SNeerav Parikh  *      - WWPN supplied is unique for given lport
2608bdf25218SNeerav Parikh  *
2609bdf25218SNeerav Parikh  *
2610bdf25218SNeerav Parikh */
2611bdf25218SNeerav Parikh static int fcoe_validate_vport_create(struct fc_vport *vport)
2612bdf25218SNeerav Parikh {
2613bdf25218SNeerav Parikh 	struct Scsi_Host *shost = vport_to_shost(vport);
2614bdf25218SNeerav Parikh 	struct fc_lport *n_port = shost_priv(shost);
2615bdf25218SNeerav Parikh 	struct fc_lport *vn_port;
2616bdf25218SNeerav Parikh 	int rc = 0;
2617bdf25218SNeerav Parikh 	char buf[32];
2618bdf25218SNeerav Parikh 
2619bdf25218SNeerav Parikh 	mutex_lock(&n_port->lp_mutex);
2620bdf25218SNeerav Parikh 
2621bdf25218SNeerav Parikh 	wwn_to_str(vport->port_name, buf, sizeof(buf));
2622bdf25218SNeerav Parikh 	/* Check if the wwpn is not same as that of the lport */
2623bdf25218SNeerav Parikh 	if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
2624bdf25218SNeerav Parikh 		FCOE_DBG("vport WWPN 0x%s is same as that of the "
2625bdf25218SNeerav Parikh 			"base port WWPN\n", buf);
2626bdf25218SNeerav Parikh 		rc = -EINVAL;
2627bdf25218SNeerav Parikh 		goto out;
2628bdf25218SNeerav Parikh 	}
2629bdf25218SNeerav Parikh 
2630bdf25218SNeerav Parikh 	/* Check if there is any existing vport with same wwpn */
2631bdf25218SNeerav Parikh 	list_for_each_entry(vn_port, &n_port->vports, list) {
2632bdf25218SNeerav Parikh 		if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
2633bdf25218SNeerav Parikh 			FCOE_DBG("vport with given WWPN 0x%s already "
2634bdf25218SNeerav Parikh 			"exists\n", buf);
2635bdf25218SNeerav Parikh 			rc = -EINVAL;
2636bdf25218SNeerav Parikh 			break;
2637bdf25218SNeerav Parikh 		}
2638bdf25218SNeerav Parikh 	}
2639bdf25218SNeerav Parikh 
2640bdf25218SNeerav Parikh out:
2641bdf25218SNeerav Parikh 	mutex_unlock(&n_port->lp_mutex);
2642bdf25218SNeerav Parikh 
2643bdf25218SNeerav Parikh 	return rc;
2644bdf25218SNeerav Parikh }
2645