xref: /openbmc/linux/drivers/scsi/fcoe/fcoe.c (revision 78a58246)
1a703e490SVasu Dev /*
2af7f85d9SChris Leech  * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
3a703e490SVasu Dev  *
4a703e490SVasu Dev  * This program is free software; you can redistribute it and/or modify it
5a703e490SVasu Dev  * under the terms and conditions of the GNU General Public License,
6a703e490SVasu Dev  * version 2, as published by the Free Software Foundation.
7a703e490SVasu Dev  *
8a703e490SVasu Dev  * This program is distributed in the hope it will be useful, but WITHOUT
9a703e490SVasu Dev  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10a703e490SVasu Dev  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11a703e490SVasu Dev  * more details.
12a703e490SVasu Dev  *
13a703e490SVasu Dev  * You should have received a copy of the GNU General Public License along with
14a703e490SVasu Dev  * this program; if not, write to the Free Software Foundation, Inc.,
15a703e490SVasu Dev  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16a703e490SVasu Dev  *
17a703e490SVasu Dev  * Maintained at www.Open-FCoE.org
18a703e490SVasu Dev  */
19a703e490SVasu Dev 
20a703e490SVasu Dev #include <linux/module.h>
21a703e490SVasu Dev #include <linux/version.h>
22a703e490SVasu Dev #include <linux/spinlock.h>
23a703e490SVasu Dev #include <linux/netdevice.h>
24a703e490SVasu Dev #include <linux/etherdevice.h>
25a703e490SVasu Dev #include <linux/ethtool.h>
26a703e490SVasu Dev #include <linux/if_ether.h>
27a703e490SVasu Dev #include <linux/if_vlan.h>
28a703e490SVasu Dev #include <linux/crc32.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30a703e490SVasu Dev #include <linux/cpu.h>
31a703e490SVasu Dev #include <linux/fs.h>
32a703e490SVasu Dev #include <linux/sysfs.h>
33a703e490SVasu Dev #include <linux/ctype.h>
34a703e490SVasu Dev #include <scsi/scsi_tcq.h>
35a703e490SVasu Dev #include <scsi/scsicam.h>
36a703e490SVasu Dev #include <scsi/scsi_transport.h>
37a703e490SVasu Dev #include <scsi/scsi_transport_fc.h>
38a703e490SVasu Dev #include <net/rtnetlink.h>
39a703e490SVasu Dev 
40a703e490SVasu Dev #include <scsi/fc/fc_encaps.h>
4197c8389dSJoe Eykholt #include <scsi/fc/fc_fip.h>
42a703e490SVasu Dev 
43a703e490SVasu Dev #include <scsi/libfc.h>
44a703e490SVasu Dev #include <scsi/fc_frame.h>
45a703e490SVasu Dev #include <scsi/libfcoe.h>
46a703e490SVasu Dev 
47fdd78027SVasu Dev #include "fcoe.h"
48fdd78027SVasu Dev 
49a703e490SVasu Dev MODULE_AUTHOR("Open-FCoE.org");
50a703e490SVasu Dev MODULE_DESCRIPTION("FCoE");
519b34ecffSVasu Dev MODULE_LICENSE("GPL v2");
52a703e490SVasu Dev 
5305cc7390SYi Zou /* Performance tuning parameters for fcoe */
5405cc7390SYi Zou static unsigned int fcoe_ddp_min;
5505cc7390SYi Zou module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
5605cc7390SYi Zou MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for "	\
5705cc7390SYi Zou 		 "Direct Data Placement (DDP).");
5805cc7390SYi Zou 
59dfc1d0feSChris Leech DEFINE_MUTEX(fcoe_config_mutex);
60dfc1d0feSChris Leech 
61e7a51997SJoe Eykholt /* fcoe_percpu_clean completion.  Waiter protected by fcoe_create_mutex */
62e7a51997SJoe Eykholt static DECLARE_COMPLETION(fcoe_flush_completion);
63e7a51997SJoe Eykholt 
64a703e490SVasu Dev /* fcoe host list */
65090eb6c4SChris Leech /* must only by accessed under the RTNL mutex */
66a703e490SVasu Dev LIST_HEAD(fcoe_hostlist);
67a703e490SVasu Dev DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
68a703e490SVasu Dev 
69dd3fd72eSChris Leech /* Function Prototypes */
701875f27eSRobert Love static int fcoe_reset(struct Scsi_Host *);
71fdd78027SVasu Dev static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
72fdd78027SVasu Dev static int fcoe_rcv(struct sk_buff *, struct net_device *,
73fdd78027SVasu Dev 		    struct packet_type *, struct net_device *);
741875f27eSRobert Love static int fcoe_percpu_receive_thread(void *);
751875f27eSRobert Love static void fcoe_clean_pending_queue(struct fc_lport *);
761875f27eSRobert Love static void fcoe_percpu_clean(struct fc_lport *);
775e4f8fe7SRobert Love static int fcoe_link_speed_update(struct fc_lport *);
781875f27eSRobert Love static int fcoe_link_ok(struct fc_lport *);
79fdd78027SVasu Dev 
80fdd78027SVasu Dev static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
81fdd78027SVasu Dev static int fcoe_hostlist_add(const struct fc_lport *);
82fdd78027SVasu Dev 
834bb6b515SVasu Dev static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
84a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *, ulong, void *);
85a703e490SVasu Dev static void fcoe_dev_setup(void);
86a703e490SVasu Dev static void fcoe_dev_cleanup(void);
871875f27eSRobert Love static struct fcoe_interface
881875f27eSRobert Love *fcoe_hostlist_lookup_port(const struct net_device *);
89a703e490SVasu Dev 
901875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
911875f27eSRobert Love 			 struct packet_type *, struct net_device *);
921875f27eSRobert Love 
931875f27eSRobert Love static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
941875f27eSRobert Love static void fcoe_update_src_mac(struct fc_lport *, u8 *);
951875f27eSRobert Love static u8 *fcoe_get_src_mac(struct fc_lport *);
961875f27eSRobert Love static void fcoe_destroy_work(struct work_struct *);
971875f27eSRobert Love 
981875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
991875f27eSRobert Love 			  unsigned int);
1001875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *, u16);
1011875f27eSRobert Love 
1021875f27eSRobert Love static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
1031875f27eSRobert Love 
10478a58246SYi Zou static bool fcoe_match(struct net_device *netdev);
10578a58246SYi Zou static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
10678a58246SYi Zou static int fcoe_destroy(struct net_device *netdev);
10778a58246SYi Zou static int fcoe_enable(struct net_device *netdev);
10878a58246SYi Zou static int fcoe_disable(struct net_device *netdev);
1091875f27eSRobert Love 
1101875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
1111875f27eSRobert Love 				      u32 did, struct fc_frame *,
1121875f27eSRobert Love 				      unsigned int op,
1131875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
1141875f27eSRobert Love 						   struct fc_frame *,
1151875f27eSRobert Love 						   void *),
1161875f27eSRobert Love 				      void *, u32 timeout);
117859b7b64SChris Leech static void fcoe_recv_frame(struct sk_buff *skb);
1181875f27eSRobert Love 
119b84056bfSYi Zou static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
120b84056bfSYi Zou 
1211875f27eSRobert Love /* notification function for packets from net device */
122a703e490SVasu Dev static struct notifier_block fcoe_notifier = {
123a703e490SVasu Dev 	.notifier_call = fcoe_device_notification,
124a703e490SVasu Dev };
125a703e490SVasu Dev 
1261875f27eSRobert Love /* notification function for CPU hotplug events */
1271875f27eSRobert Love static struct notifier_block fcoe_cpu_notifier = {
1281875f27eSRobert Love 	.notifier_call = fcoe_cpu_callback,
1291875f27eSRobert Love };
1301875f27eSRobert Love 
1318ca86f84SYi Zou static struct scsi_transport_template *fcoe_nport_scsi_transport;
1328ca86f84SYi Zou static struct scsi_transport_template *fcoe_vport_scsi_transport;
133a703e490SVasu Dev 
1341875f27eSRobert Love static int fcoe_vport_destroy(struct fc_vport *);
1351875f27eSRobert Love static int fcoe_vport_create(struct fc_vport *, bool disabled);
1361875f27eSRobert Love static int fcoe_vport_disable(struct fc_vport *, bool disable);
1371875f27eSRobert Love static void fcoe_set_vport_symbolic_name(struct fc_vport *);
1387d65b0dfSJoe Eykholt static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
1391875f27eSRobert Love 
1401875f27eSRobert Love static struct libfc_function_template fcoe_libfc_fcn_templ = {
1411875f27eSRobert Love 	.frame_send = fcoe_xmit,
1421875f27eSRobert Love 	.ddp_setup = fcoe_ddp_setup,
1431875f27eSRobert Love 	.ddp_done = fcoe_ddp_done,
1441875f27eSRobert Love 	.elsct_send = fcoe_elsct_send,
145b84056bfSYi Zou 	.get_lesb = fcoe_get_lesb,
1467d65b0dfSJoe Eykholt 	.lport_set_port_id = fcoe_set_port_id,
1471875f27eSRobert Love };
1489a05753bSChris Leech 
1498ca86f84SYi Zou struct fc_function_template fcoe_nport_fc_functions = {
150a703e490SVasu Dev 	.show_host_node_name = 1,
151a703e490SVasu Dev 	.show_host_port_name = 1,
152a703e490SVasu Dev 	.show_host_supported_classes = 1,
153a703e490SVasu Dev 	.show_host_supported_fc4s = 1,
154a703e490SVasu Dev 	.show_host_active_fc4s = 1,
155a703e490SVasu Dev 	.show_host_maxframe_size = 1,
156a703e490SVasu Dev 
157a703e490SVasu Dev 	.show_host_port_id = 1,
158a703e490SVasu Dev 	.show_host_supported_speeds = 1,
159a703e490SVasu Dev 	.get_host_speed = fc_get_host_speed,
160a703e490SVasu Dev 	.show_host_speed = 1,
161a703e490SVasu Dev 	.show_host_port_type = 1,
162a703e490SVasu Dev 	.get_host_port_state = fc_get_host_port_state,
163a703e490SVasu Dev 	.show_host_port_state = 1,
164a703e490SVasu Dev 	.show_host_symbolic_name = 1,
165a703e490SVasu Dev 
166a703e490SVasu Dev 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
167a703e490SVasu Dev 	.show_rport_maxframe_size = 1,
168a703e490SVasu Dev 	.show_rport_supported_classes = 1,
169a703e490SVasu Dev 
170a703e490SVasu Dev 	.show_host_fabric_name = 1,
171a703e490SVasu Dev 	.show_starget_node_name = 1,
172a703e490SVasu Dev 	.show_starget_port_name = 1,
173a703e490SVasu Dev 	.show_starget_port_id = 1,
174a703e490SVasu Dev 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
175a703e490SVasu Dev 	.show_rport_dev_loss_tmo = 1,
176a703e490SVasu Dev 	.get_fc_host_stats = fc_get_host_stats,
177a703e490SVasu Dev 	.issue_fc_host_lip = fcoe_reset,
178a703e490SVasu Dev 
179a703e490SVasu Dev 	.terminate_rport_io = fc_rport_terminate_io,
1809a05753bSChris Leech 
1819a05753bSChris Leech 	.vport_create = fcoe_vport_create,
1829a05753bSChris Leech 	.vport_delete = fcoe_vport_destroy,
1839a05753bSChris Leech 	.vport_disable = fcoe_vport_disable,
184dc8596d3SChris Leech 	.set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
185a51ab396SSteve Ma 
186a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
187a703e490SVasu Dev };
188a703e490SVasu Dev 
1898ca86f84SYi Zou struct fc_function_template fcoe_vport_fc_functions = {
190e9084bb8SChris Leech 	.show_host_node_name = 1,
191e9084bb8SChris Leech 	.show_host_port_name = 1,
192e9084bb8SChris Leech 	.show_host_supported_classes = 1,
193e9084bb8SChris Leech 	.show_host_supported_fc4s = 1,
194e9084bb8SChris Leech 	.show_host_active_fc4s = 1,
195e9084bb8SChris Leech 	.show_host_maxframe_size = 1,
196e9084bb8SChris Leech 
197e9084bb8SChris Leech 	.show_host_port_id = 1,
198e9084bb8SChris Leech 	.show_host_supported_speeds = 1,
199e9084bb8SChris Leech 	.get_host_speed = fc_get_host_speed,
200e9084bb8SChris Leech 	.show_host_speed = 1,
201e9084bb8SChris Leech 	.show_host_port_type = 1,
202e9084bb8SChris Leech 	.get_host_port_state = fc_get_host_port_state,
203e9084bb8SChris Leech 	.show_host_port_state = 1,
204e9084bb8SChris Leech 	.show_host_symbolic_name = 1,
205e9084bb8SChris Leech 
206e9084bb8SChris Leech 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
207e9084bb8SChris Leech 	.show_rport_maxframe_size = 1,
208e9084bb8SChris Leech 	.show_rport_supported_classes = 1,
209e9084bb8SChris Leech 
210e9084bb8SChris Leech 	.show_host_fabric_name = 1,
211e9084bb8SChris Leech 	.show_starget_node_name = 1,
212e9084bb8SChris Leech 	.show_starget_port_name = 1,
213e9084bb8SChris Leech 	.show_starget_port_id = 1,
214e9084bb8SChris Leech 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
215e9084bb8SChris Leech 	.show_rport_dev_loss_tmo = 1,
216e9084bb8SChris Leech 	.get_fc_host_stats = fc_get_host_stats,
217e9084bb8SChris Leech 	.issue_fc_host_lip = fcoe_reset,
218e9084bb8SChris Leech 
219e9084bb8SChris Leech 	.terminate_rport_io = fc_rport_terminate_io,
220a51ab396SSteve Ma 
221a51ab396SSteve Ma 	.bsg_request = fc_lport_bsg_request,
222e9084bb8SChris Leech };
223e9084bb8SChris Leech 
224a703e490SVasu Dev static struct scsi_host_template fcoe_shost_template = {
225a703e490SVasu Dev 	.module = THIS_MODULE,
226a703e490SVasu Dev 	.name = "FCoE Driver",
227a703e490SVasu Dev 	.proc_name = FCOE_NAME,
228a703e490SVasu Dev 	.queuecommand = fc_queuecommand,
229a703e490SVasu Dev 	.eh_abort_handler = fc_eh_abort,
230a703e490SVasu Dev 	.eh_device_reset_handler = fc_eh_device_reset,
231a703e490SVasu Dev 	.eh_host_reset_handler = fc_eh_host_reset,
232a703e490SVasu Dev 	.slave_alloc = fc_slave_alloc,
233a703e490SVasu Dev 	.change_queue_depth = fc_change_queue_depth,
234a703e490SVasu Dev 	.change_queue_type = fc_change_queue_type,
235a703e490SVasu Dev 	.this_id = -1,
23614caf44cSVasu Dev 	.cmd_per_lun = 3,
237a703e490SVasu Dev 	.can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
238a703e490SVasu Dev 	.use_clustering = ENABLE_CLUSTERING,
239a703e490SVasu Dev 	.sg_tablesize = SG_ALL,
240a703e490SVasu Dev 	.max_sectors = 0xffff,
241a703e490SVasu Dev };
242a703e490SVasu Dev 
24354b649f8SChris Leech /**
2441875f27eSRobert Love  * fcoe_interface_setup() - Setup a FCoE interface
2451875f27eSRobert Love  * @fcoe:   The new FCoE interface
2461875f27eSRobert Love  * @netdev: The net device that the fcoe interface is on
24754b649f8SChris Leech  *
24854b649f8SChris Leech  * Returns : 0 for success
2492e70e241SChris Leech  * Locking: must be called with the RTNL mutex held
25054b649f8SChris Leech  */
25154b649f8SChris Leech static int fcoe_interface_setup(struct fcoe_interface *fcoe,
25254b649f8SChris Leech 				struct net_device *netdev)
25354b649f8SChris Leech {
25454b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
25554b649f8SChris Leech 	struct netdev_hw_addr *ha;
2565bab87e6SYi Zou 	struct net_device *real_dev;
25754b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
258b7a727f1SYi Zou 	const struct net_device_ops *ops;
25954b649f8SChris Leech 
26054b649f8SChris Leech 	fcoe->netdev = netdev;
26154b649f8SChris Leech 
262b7a727f1SYi Zou 	/* Let LLD initialize for FCoE */
263b7a727f1SYi Zou 	ops = netdev->netdev_ops;
264b7a727f1SYi Zou 	if (ops->ndo_fcoe_enable) {
265b7a727f1SYi Zou 		if (ops->ndo_fcoe_enable(netdev))
266b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
267b7a727f1SYi Zou 					" specific feature for LLD.\n");
268b7a727f1SYi Zou 	}
269b7a727f1SYi Zou 
27054b649f8SChris Leech 	/* Do not support for bonding device */
27154b649f8SChris Leech 	if ((netdev->priv_flags & IFF_MASTER_ALB) ||
27254b649f8SChris Leech 	    (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
27354b649f8SChris Leech 	    (netdev->priv_flags & IFF_MASTER_8023AD)) {
27459d92516Sjohn fastabend 		FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
27554b649f8SChris Leech 		return -EOPNOTSUPP;
27654b649f8SChris Leech 	}
27754b649f8SChris Leech 
27854b649f8SChris Leech 	/* look for SAN MAC address, if multiple SAN MACs exist, only
27954b649f8SChris Leech 	 * use the first one for SPMA */
2805bab87e6SYi Zou 	real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
2815bab87e6SYi Zou 		vlan_dev_real_dev(netdev) : netdev;
28254b649f8SChris Leech 	rcu_read_lock();
2835bab87e6SYi Zou 	for_each_dev_addr(real_dev, ha) {
28454b649f8SChris Leech 		if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
285bf361707SYi Zou 		    (is_valid_ether_addr(ha->addr))) {
28654b649f8SChris Leech 			memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
28754b649f8SChris Leech 			fip->spma = 1;
28854b649f8SChris Leech 			break;
28954b649f8SChris Leech 		}
29054b649f8SChris Leech 	}
29154b649f8SChris Leech 	rcu_read_unlock();
29254b649f8SChris Leech 
29354b649f8SChris Leech 	/* setup Source Mac Address */
29454b649f8SChris Leech 	if (!fip->spma)
29554b649f8SChris Leech 		memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
29654b649f8SChris Leech 
29754b649f8SChris Leech 	/*
29854b649f8SChris Leech 	 * Add FCoE MAC address as second unicast MAC address
29954b649f8SChris Leech 	 * or enter promiscuous mode if not capable of listening
30054b649f8SChris Leech 	 * for multiple unicast MACs.
30154b649f8SChris Leech 	 */
30254b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
303a748ee24SJiri Pirko 	dev_uc_add(netdev, flogi_maddr);
30454b649f8SChris Leech 	if (fip->spma)
305a748ee24SJiri Pirko 		dev_uc_add(netdev, fip->ctl_src_addr);
306e10f8c66SJoe Eykholt 	if (fip->mode == FIP_MODE_VN2VN) {
307e10f8c66SJoe Eykholt 		dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
308e10f8c66SJoe Eykholt 		dev_mc_add(netdev, FIP_ALL_P2P_MACS);
309e10f8c66SJoe Eykholt 	} else
31022bedad3SJiri Pirko 		dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
31154b649f8SChris Leech 
31254b649f8SChris Leech 	/*
31354b649f8SChris Leech 	 * setup the receive function from ethernet driver
31454b649f8SChris Leech 	 * on the ethertype for the given device
31554b649f8SChris Leech 	 */
31654b649f8SChris Leech 	fcoe->fcoe_packet_type.func = fcoe_rcv;
31754b649f8SChris Leech 	fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
31854b649f8SChris Leech 	fcoe->fcoe_packet_type.dev = netdev;
31954b649f8SChris Leech 	dev_add_pack(&fcoe->fcoe_packet_type);
32054b649f8SChris Leech 
32154b649f8SChris Leech 	fcoe->fip_packet_type.func = fcoe_fip_recv;
32254b649f8SChris Leech 	fcoe->fip_packet_type.type = htons(ETH_P_FIP);
32354b649f8SChris Leech 	fcoe->fip_packet_type.dev = netdev;
32454b649f8SChris Leech 	dev_add_pack(&fcoe->fip_packet_type);
32554b649f8SChris Leech 
32654b649f8SChris Leech 	return 0;
32754b649f8SChris Leech }
32854b649f8SChris Leech 
329a703e490SVasu Dev /**
3301875f27eSRobert Love  * fcoe_interface_create() - Create a FCoE interface on a net device
3311875f27eSRobert Love  * @netdev: The net device to create the FCoE interface on
3321dd454d9SJoe Eykholt  * @fip_mode: The mode to use for FIP
333030f4e00SChris Leech  *
334030f4e00SChris Leech  * Returns: pointer to a struct fcoe_interface or NULL on error
335030f4e00SChris Leech  */
3361dd454d9SJoe Eykholt static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
3371dd454d9SJoe Eykholt 						    enum fip_state fip_mode)
338030f4e00SChris Leech {
339030f4e00SChris Leech 	struct fcoe_interface *fcoe;
34059d92516Sjohn fastabend 	int err;
341030f4e00SChris Leech 
3427287fb91SRobert Love 	if (!try_module_get(THIS_MODULE)) {
3437287fb91SRobert Love 		FCOE_NETDEV_DBG(netdev,
3447287fb91SRobert Love 				"Could not get a reference to the module\n");
3457287fb91SRobert Love 		fcoe = ERR_PTR(-EBUSY);
3467287fb91SRobert Love 		goto out;
3477287fb91SRobert Love 	}
3487287fb91SRobert Love 
349030f4e00SChris Leech 	fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
350030f4e00SChris Leech 	if (!fcoe) {
351030f4e00SChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
3527287fb91SRobert Love 		fcoe = ERR_PTR(-ENOMEM);
3537287fb91SRobert Love 		goto out_nomod;
354030f4e00SChris Leech 	}
355030f4e00SChris Leech 
3562e70e241SChris Leech 	dev_hold(netdev);
357030f4e00SChris Leech 	kref_init(&fcoe->kref);
35854b649f8SChris Leech 
35954b649f8SChris Leech 	/*
36054b649f8SChris Leech 	 * Initialize FIP.
36154b649f8SChris Leech 	 */
3621dd454d9SJoe Eykholt 	fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
36354b649f8SChris Leech 	fcoe->ctlr.send = fcoe_fip_send;
36454b649f8SChris Leech 	fcoe->ctlr.update_mac = fcoe_update_src_mac;
36511b56188SChris Leech 	fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
36654b649f8SChris Leech 
36759d92516Sjohn fastabend 	err = fcoe_interface_setup(fcoe, netdev);
36859d92516Sjohn fastabend 	if (err) {
36959d92516Sjohn fastabend 		fcoe_ctlr_destroy(&fcoe->ctlr);
37059d92516Sjohn fastabend 		kfree(fcoe);
37159d92516Sjohn fastabend 		dev_put(netdev);
3727287fb91SRobert Love 		fcoe = ERR_PTR(err);
3737287fb91SRobert Love 		goto out_nomod;
37459d92516Sjohn fastabend 	}
375030f4e00SChris Leech 
3767287fb91SRobert Love 	goto out;
3777287fb91SRobert Love 
3787287fb91SRobert Love out_nomod:
3797287fb91SRobert Love 	module_put(THIS_MODULE);
3807287fb91SRobert Love out:
381030f4e00SChris Leech 	return fcoe;
382030f4e00SChris Leech }
383030f4e00SChris Leech 
384030f4e00SChris Leech /**
3851875f27eSRobert Love  * fcoe_interface_cleanup() - Clean up a FCoE interface
3861875f27eSRobert Love  * @fcoe: The FCoE interface to be cleaned up
3872e70e241SChris Leech  *
3882e70e241SChris Leech  * Caller must be holding the RTNL mutex
38954b649f8SChris Leech  */
39054b649f8SChris Leech void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
39154b649f8SChris Leech {
39254b649f8SChris Leech 	struct net_device *netdev = fcoe->netdev;
39354b649f8SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
39454b649f8SChris Leech 	u8 flogi_maddr[ETH_ALEN];
395b7a727f1SYi Zou 	const struct net_device_ops *ops;
39654b649f8SChris Leech 
39754b649f8SChris Leech 	/*
39854b649f8SChris Leech 	 * Don't listen for Ethernet packets anymore.
39954b649f8SChris Leech 	 * synchronize_net() ensures that the packet handlers are not running
40054b649f8SChris Leech 	 * on another CPU. dev_remove_pack() would do that, this calls the
40154b649f8SChris Leech 	 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
40254b649f8SChris Leech 	 */
40354b649f8SChris Leech 	__dev_remove_pack(&fcoe->fcoe_packet_type);
40454b649f8SChris Leech 	__dev_remove_pack(&fcoe->fip_packet_type);
40554b649f8SChris Leech 	synchronize_net();
40654b649f8SChris Leech 
40754b649f8SChris Leech 	/* Delete secondary MAC addresses */
40854b649f8SChris Leech 	memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
409a748ee24SJiri Pirko 	dev_uc_del(netdev, flogi_maddr);
41054b649f8SChris Leech 	if (fip->spma)
411a748ee24SJiri Pirko 		dev_uc_del(netdev, fip->ctl_src_addr);
412e10f8c66SJoe Eykholt 	if (fip->mode == FIP_MODE_VN2VN) {
413e10f8c66SJoe Eykholt 		dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
414e10f8c66SJoe Eykholt 		dev_mc_del(netdev, FIP_ALL_P2P_MACS);
415e10f8c66SJoe Eykholt 	} else
41622bedad3SJiri Pirko 		dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
417b7a727f1SYi Zou 
418b7a727f1SYi Zou 	/* Tell the LLD we are done w/ FCoE */
419b7a727f1SYi Zou 	ops = netdev->netdev_ops;
420b7a727f1SYi Zou 	if (ops->ndo_fcoe_disable) {
421b7a727f1SYi Zou 		if (ops->ndo_fcoe_disable(netdev))
422b7a727f1SYi Zou 			FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
423b7a727f1SYi Zou 					" specific feature for LLD.\n");
424b7a727f1SYi Zou 	}
42554b649f8SChris Leech }
42654b649f8SChris Leech 
42754b649f8SChris Leech /**
428030f4e00SChris Leech  * fcoe_interface_release() - fcoe_port kref release function
4291875f27eSRobert Love  * @kref: Embedded reference count in an fcoe_interface struct
430030f4e00SChris Leech  */
431030f4e00SChris Leech static void fcoe_interface_release(struct kref *kref)
432030f4e00SChris Leech {
433030f4e00SChris Leech 	struct fcoe_interface *fcoe;
4342e70e241SChris Leech 	struct net_device *netdev;
435030f4e00SChris Leech 
436030f4e00SChris Leech 	fcoe = container_of(kref, struct fcoe_interface, kref);
4372e70e241SChris Leech 	netdev = fcoe->netdev;
4382e70e241SChris Leech 	/* tear-down the FCoE controller */
4392e70e241SChris Leech 	fcoe_ctlr_destroy(&fcoe->ctlr);
440030f4e00SChris Leech 	kfree(fcoe);
4412e70e241SChris Leech 	dev_put(netdev);
4427287fb91SRobert Love 	module_put(THIS_MODULE);
443030f4e00SChris Leech }
444030f4e00SChris Leech 
445030f4e00SChris Leech /**
4461875f27eSRobert Love  * fcoe_interface_get() - Get a reference to a FCoE interface
4471875f27eSRobert Love  * @fcoe: The FCoE interface to be held
448030f4e00SChris Leech  */
449030f4e00SChris Leech static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
450030f4e00SChris Leech {
451030f4e00SChris Leech 	kref_get(&fcoe->kref);
452030f4e00SChris Leech }
453030f4e00SChris Leech 
454030f4e00SChris Leech /**
4551875f27eSRobert Love  * fcoe_interface_put() - Put a reference to a FCoE interface
4561875f27eSRobert Love  * @fcoe: The FCoE interface to be released
457030f4e00SChris Leech  */
458030f4e00SChris Leech static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
459030f4e00SChris Leech {
460030f4e00SChris Leech 	kref_put(&fcoe->kref, fcoe_interface_release);
461030f4e00SChris Leech }
462030f4e00SChris Leech 
463030f4e00SChris Leech /**
4641875f27eSRobert Love  * fcoe_fip_recv() - Handler for received FIP frames
4651875f27eSRobert Love  * @skb:      The receive skb
4661875f27eSRobert Love  * @netdev:   The associated net device
4671875f27eSRobert Love  * @ptype:    The packet_type structure which was used to register this handler
4681875f27eSRobert Love  * @orig_dev: The original net_device the the skb was received on.
4691875f27eSRobert Love  *	      (in case dev is a bond)
470ab6b85c1SVasu Dev  *
471ab6b85c1SVasu Dev  * Returns: 0 for success
472ab6b85c1SVasu Dev  */
4731875f27eSRobert Love static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
474ab6b85c1SVasu Dev 			 struct packet_type *ptype,
475ab6b85c1SVasu Dev 			 struct net_device *orig_dev)
476ab6b85c1SVasu Dev {
477259ad85dSChris Leech 	struct fcoe_interface *fcoe;
478ab6b85c1SVasu Dev 
479259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
4803fe9a0baSChris Leech 	fcoe_ctlr_recv(&fcoe->ctlr, skb);
481ab6b85c1SVasu Dev 	return 0;
482ab6b85c1SVasu Dev }
483ab6b85c1SVasu Dev 
484ab6b85c1SVasu Dev /**
4851875f27eSRobert Love  * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
4861875f27eSRobert Love  * @fip: The FCoE controller
4871875f27eSRobert Love  * @skb: The FIP packet to be sent
488ab6b85c1SVasu Dev  */
489ab6b85c1SVasu Dev static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
490ab6b85c1SVasu Dev {
4913fe9a0baSChris Leech 	skb->dev = fcoe_from_ctlr(fip)->netdev;
492ab6b85c1SVasu Dev 	dev_queue_xmit(skb);
493ab6b85c1SVasu Dev }
494ab6b85c1SVasu Dev 
495ab6b85c1SVasu Dev /**
4961875f27eSRobert Love  * fcoe_update_src_mac() - Update the Ethernet MAC filters
4971875f27eSRobert Love  * @lport: The local port to update the source MAC on
4981875f27eSRobert Love  * @addr:  Unicast MAC address to add
499ab6b85c1SVasu Dev  *
500ab6b85c1SVasu Dev  * Remove any previously-set unicast MAC filter.
501ab6b85c1SVasu Dev  * Add secondary FCoE MAC address filter for our OUI.
502ab6b85c1SVasu Dev  */
50311b56188SChris Leech static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
504ab6b85c1SVasu Dev {
50511b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
50611b56188SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
507ab6b85c1SVasu Dev 
508ab6b85c1SVasu Dev 	rtnl_lock();
50911b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
510a748ee24SJiri Pirko 		dev_uc_del(fcoe->netdev, port->data_src_addr);
51111b56188SChris Leech 	if (!is_zero_ether_addr(addr))
512a748ee24SJiri Pirko 		dev_uc_add(fcoe->netdev, addr);
51311b56188SChris Leech 	memcpy(port->data_src_addr, addr, ETH_ALEN);
514ab6b85c1SVasu Dev 	rtnl_unlock();
515ab6b85c1SVasu Dev }
516ab6b85c1SVasu Dev 
517ab6b85c1SVasu Dev /**
51811b56188SChris Leech  * fcoe_get_src_mac() - return the Ethernet source address for an lport
51911b56188SChris Leech  * @lport: libfc lport
52011b56188SChris Leech  */
52111b56188SChris Leech static u8 *fcoe_get_src_mac(struct fc_lport *lport)
52211b56188SChris Leech {
52311b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
52411b56188SChris Leech 
52511b56188SChris Leech 	return port->data_src_addr;
52611b56188SChris Leech }
52711b56188SChris Leech 
52811b56188SChris Leech /**
5291875f27eSRobert Love  * fcoe_lport_config() - Set up a local port
5301875f27eSRobert Love  * @lport: The local port to be setup
531a703e490SVasu Dev  *
532a703e490SVasu Dev  * Returns: 0 for success
533a703e490SVasu Dev  */
5341875f27eSRobert Love static int fcoe_lport_config(struct fc_lport *lport)
535a703e490SVasu Dev {
5361875f27eSRobert Love 	lport->link_up = 0;
5371875f27eSRobert Love 	lport->qfull = 0;
5381875f27eSRobert Love 	lport->max_retry_count = 3;
5391875f27eSRobert Love 	lport->max_rport_retry_count = 3;
5401875f27eSRobert Love 	lport->e_d_tov = 2 * 1000;	/* FC-FS default */
5411875f27eSRobert Love 	lport->r_a_tov = 2 * 2 * 1000;
5421875f27eSRobert Love 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
543a703e490SVasu Dev 				 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
5441875f27eSRobert Love 	lport->does_npiv = 1;
545a703e490SVasu Dev 
5461875f27eSRobert Love 	fc_lport_init_stats(lport);
547a703e490SVasu Dev 
548a703e490SVasu Dev 	/* lport fc_lport related configuration */
5491875f27eSRobert Love 	fc_lport_config(lport);
550a703e490SVasu Dev 
551a703e490SVasu Dev 	/* offload related configuration */
5521875f27eSRobert Love 	lport->crc_offload = 0;
5531875f27eSRobert Love 	lport->seq_offload = 0;
5541875f27eSRobert Love 	lport->lro_enabled = 0;
5551875f27eSRobert Love 	lport->lro_xid = 0;
5561875f27eSRobert Love 	lport->lso_max = 0;
557a703e490SVasu Dev 
558a703e490SVasu Dev 	return 0;
559a703e490SVasu Dev }
560a703e490SVasu Dev 
561a703e490SVasu Dev /**
5621875f27eSRobert Love  * fcoe_queue_timer() - The fcoe queue timer
5631875f27eSRobert Love  * @lport: The local port
5641047f221SVasu Dev  *
5651047f221SVasu Dev  * Calls fcoe_check_wait_queue on timeout
5661047f221SVasu Dev  */
5671875f27eSRobert Love static void fcoe_queue_timer(ulong lport)
5681047f221SVasu Dev {
5691875f27eSRobert Love 	fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
5701047f221SVasu Dev }
5711047f221SVasu Dev 
5721047f221SVasu Dev /**
573dcece412SYi Zou  * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
574dcece412SYi Zou  * @netdev: the associated net device
575dcece412SYi Zou  * @wwn: the output WWN
576dcece412SYi Zou  * @type: the type of WWN (WWPN or WWNN)
577dcece412SYi Zou  *
578dcece412SYi Zou  * Returns: 0 for success
579dcece412SYi Zou  */
580dcece412SYi Zou static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
581dcece412SYi Zou {
582dcece412SYi Zou 	const struct net_device_ops *ops = netdev->netdev_ops;
583dcece412SYi Zou 
584dcece412SYi Zou 	if (ops->ndo_fcoe_get_wwn)
585dcece412SYi Zou 		return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
586dcece412SYi Zou 	return -EINVAL;
587dcece412SYi Zou }
588dcece412SYi Zou 
589dcece412SYi Zou /**
59054a5b21dSYi Zou  * fcoe_netdev_features_change - Updates the lport's offload flags based
59154a5b21dSYi Zou  * on the LLD netdev's FCoE feature flags
59254a5b21dSYi Zou  */
59354a5b21dSYi Zou static void fcoe_netdev_features_change(struct fc_lport *lport,
59454a5b21dSYi Zou 					struct net_device *netdev)
59554a5b21dSYi Zou {
59654a5b21dSYi Zou 	mutex_lock(&lport->lp_mutex);
59754a5b21dSYi Zou 
59854a5b21dSYi Zou 	if (netdev->features & NETIF_F_SG)
59954a5b21dSYi Zou 		lport->sg_supp = 1;
60054a5b21dSYi Zou 	else
60154a5b21dSYi Zou 		lport->sg_supp = 0;
60254a5b21dSYi Zou 
60354a5b21dSYi Zou 	if (netdev->features & NETIF_F_FCOE_CRC) {
60454a5b21dSYi Zou 		lport->crc_offload = 1;
60554a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
60654a5b21dSYi Zou 	} else {
60754a5b21dSYi Zou 		lport->crc_offload = 0;
60854a5b21dSYi Zou 	}
60954a5b21dSYi Zou 
61054a5b21dSYi Zou 	if (netdev->features & NETIF_F_FSO) {
61154a5b21dSYi Zou 		lport->seq_offload = 1;
61254a5b21dSYi Zou 		lport->lso_max = netdev->gso_max_size;
61354a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
61454a5b21dSYi Zou 				lport->lso_max);
61554a5b21dSYi Zou 	} else {
61654a5b21dSYi Zou 		lport->seq_offload = 0;
61754a5b21dSYi Zou 		lport->lso_max = 0;
61854a5b21dSYi Zou 	}
61954a5b21dSYi Zou 
62054a5b21dSYi Zou 	if (netdev->fcoe_ddp_xid) {
62154a5b21dSYi Zou 		lport->lro_enabled = 1;
62254a5b21dSYi Zou 		lport->lro_xid = netdev->fcoe_ddp_xid;
62354a5b21dSYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
62454a5b21dSYi Zou 				lport->lro_xid);
62554a5b21dSYi Zou 	} else {
62654a5b21dSYi Zou 		lport->lro_enabled = 0;
62754a5b21dSYi Zou 		lport->lro_xid = 0;
62854a5b21dSYi Zou 	}
62954a5b21dSYi Zou 
63054a5b21dSYi Zou 	mutex_unlock(&lport->lp_mutex);
63154a5b21dSYi Zou }
63254a5b21dSYi Zou 
63354a5b21dSYi Zou /**
6341875f27eSRobert Love  * fcoe_netdev_config() - Set up net devive for SW FCoE
6351875f27eSRobert Love  * @lport:  The local port that is associated with the net device
6361875f27eSRobert Love  * @netdev: The associated net device
637a703e490SVasu Dev  *
6381875f27eSRobert Love  * Must be called after fcoe_lport_config() as it will use local port mutex
639a703e490SVasu Dev  *
640a703e490SVasu Dev  * Returns: 0 for success
641a703e490SVasu Dev  */
6421875f27eSRobert Love static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
643a703e490SVasu Dev {
644a703e490SVasu Dev 	u32 mfs;
645a703e490SVasu Dev 	u64 wwnn, wwpn;
64625024989SChris Leech 	struct fcoe_interface *fcoe;
647014f5c3fSChris Leech 	struct fcoe_port *port;
648a703e490SVasu Dev 
649a703e490SVasu Dev 	/* Setup lport private data to point to fcoe softc */
6501875f27eSRobert Love 	port = lport_priv(lport);
65125024989SChris Leech 	fcoe = port->fcoe;
652a703e490SVasu Dev 
653a703e490SVasu Dev 	/*
654a703e490SVasu Dev 	 * Determine max frame size based on underlying device and optional
655a703e490SVasu Dev 	 * user-configured limit.  If the MFS is too low, fcoe_link_ok()
656a703e490SVasu Dev 	 * will return 0, so do this first.
657a703e490SVasu Dev 	 */
6587221d7e5SYi Zou 	mfs = netdev->mtu;
6597221d7e5SYi Zou 	if (netdev->features & NETIF_F_FCOE_MTU) {
6607221d7e5SYi Zou 		mfs = FCOE_MTU;
6617221d7e5SYi Zou 		FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
6627221d7e5SYi Zou 	}
6637221d7e5SYi Zou 	mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
6641875f27eSRobert Love 	if (fc_set_mfs(lport, mfs))
665a703e490SVasu Dev 		return -EINVAL;
666a703e490SVasu Dev 
667a703e490SVasu Dev 	/* offload features support */
66854a5b21dSYi Zou 	fcoe_netdev_features_change(lport, netdev);
669a703e490SVasu Dev 
670014f5c3fSChris Leech 	skb_queue_head_init(&port->fcoe_pending_queue);
671014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
6721875f27eSRobert Love 	setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
673a703e490SVasu Dev 
6745e4f8fe7SRobert Love 	fcoe_link_speed_update(lport);
6755e4f8fe7SRobert Love 
6761875f27eSRobert Love 	if (!lport->vport) {
677dcece412SYi Zou 		if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
67875ea89efSYi Zou 			wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
6791875f27eSRobert Love 		fc_set_wwnn(lport, wwnn);
680dcece412SYi Zou 		if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
681dcece412SYi Zou 			wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
682cf4aebcaSVasu Dev 						 2, 0);
6831875f27eSRobert Love 		fc_set_wwpn(lport, wwpn);
6849a05753bSChris Leech 	}
685a703e490SVasu Dev 
686a703e490SVasu Dev 	return 0;
687a703e490SVasu Dev }
688a703e490SVasu Dev 
689a703e490SVasu Dev /**
6901875f27eSRobert Love  * fcoe_shost_config() - Set up the SCSI host associated with a local port
6911875f27eSRobert Love  * @lport: The local port
6921875f27eSRobert Love  * @dev:   The device associated with the SCSI host
693a703e490SVasu Dev  *
694a703e490SVasu Dev  * Must be called after fcoe_lport_config() and fcoe_netdev_config()
695a703e490SVasu Dev  *
696a703e490SVasu Dev  * Returns: 0 for success
697a703e490SVasu Dev  */
6988ba00a4bSVasu Dev static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
699a703e490SVasu Dev {
700a703e490SVasu Dev 	int rc = 0;
701a703e490SVasu Dev 
702a703e490SVasu Dev 	/* lport scsi host config */
7031875f27eSRobert Love 	lport->host->max_lun = FCOE_MAX_LUN;
7041875f27eSRobert Love 	lport->host->max_id = FCOE_MAX_FCP_TARGET;
7051875f27eSRobert Love 	lport->host->max_channel = 0;
706da87bfabSVasu Dev 	lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
707da87bfabSVasu Dev 
7081875f27eSRobert Love 	if (lport->vport)
7098ca86f84SYi Zou 		lport->host->transportt = fcoe_vport_scsi_transport;
710e9084bb8SChris Leech 	else
7118ca86f84SYi Zou 		lport->host->transportt = fcoe_nport_scsi_transport;
712a703e490SVasu Dev 
713a703e490SVasu Dev 	/* add the new host to the SCSI-ml */
7141875f27eSRobert Love 	rc = scsi_add_host(lport->host, dev);
715a703e490SVasu Dev 	if (rc) {
7161875f27eSRobert Love 		FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
717d5488eb9SRobert Love 				"error on scsi_add_host\n");
718a703e490SVasu Dev 		return rc;
719a703e490SVasu Dev 	}
7209a05753bSChris Leech 
7211875f27eSRobert Love 	if (!lport->vport)
7224be929beSAlexey Dobriyan 		fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
7239a05753bSChris Leech 
7241875f27eSRobert Love 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
7255baa17c3SChris Leech 		 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
7261875f27eSRobert Love 		 fcoe_netdev(lport)->name);
727a703e490SVasu Dev 
728a703e490SVasu Dev 	return 0;
729a703e490SVasu Dev }
730a703e490SVasu Dev 
7311875f27eSRobert Love /**
7321875f27eSRobert Love  * fcoe_oem_match() - The match routine for the offloaded exchange manager
7331875f27eSRobert Love  * @fp: The I/O frame
734d7179680SVasu Dev  *
7351875f27eSRobert Love  * This routine will be associated with an exchange manager (EM). When
7361875f27eSRobert Love  * the libfc exchange handling code is looking for an EM to use it will
7371875f27eSRobert Love  * call this routine and pass it the frame that it wishes to send. This
7381875f27eSRobert Love  * routine will return True if the associated EM is to be used and False
7391875f27eSRobert Love  * if the echange code should continue looking for an EM.
7401875f27eSRobert Love  *
7411875f27eSRobert Love  * The offload EM that this routine is associated with will handle any
7421875f27eSRobert Love  * packets that are for SCSI read requests.
7431875f27eSRobert Love  *
7441875f27eSRobert Love  * Returns: True for read types I/O, otherwise returns false.
745d7179680SVasu Dev  */
746d7179680SVasu Dev bool fcoe_oem_match(struct fc_frame *fp)
747d7179680SVasu Dev {
74805cc7390SYi Zou 	return fc_fcp_is_read(fr_fsp(fp)) &&
74905cc7390SYi Zou 		(fr_fsp(fp)->data_len > fcoe_ddp_min);
750d7179680SVasu Dev }
751d7179680SVasu Dev 
752a703e490SVasu Dev /**
7531875f27eSRobert Love  * fcoe_em_config() - Allocate and configure an exchange manager
7541875f27eSRobert Love  * @lport: The local port that the new EM will be associated with
755a703e490SVasu Dev  *
756a703e490SVasu Dev  * Returns: 0 on success
757a703e490SVasu Dev  */
7581875f27eSRobert Love static inline int fcoe_em_config(struct fc_lport *lport)
759a703e490SVasu Dev {
7601875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
76125024989SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
76225024989SChris Leech 	struct fcoe_interface *oldfcoe = NULL;
7631d1b88dcSVasu Dev 	struct net_device *old_real_dev, *cur_real_dev;
764d7179680SVasu Dev 	u16 min_xid = FCOE_MIN_XID;
765d7179680SVasu Dev 	u16 max_xid = FCOE_MAX_XID;
766d7179680SVasu Dev 
767d7179680SVasu Dev 	/*
768d7179680SVasu Dev 	 * Check if need to allocate an em instance for
769d7179680SVasu Dev 	 * offload exchange ids to be shared across all VN_PORTs/lport.
770d7179680SVasu Dev 	 */
7711875f27eSRobert Love 	if (!lport->lro_enabled || !lport->lro_xid ||
7721875f27eSRobert Love 	    (lport->lro_xid >= max_xid)) {
7731875f27eSRobert Love 		lport->lro_xid = 0;
774d7179680SVasu Dev 		goto skip_oem;
775d7179680SVasu Dev 	}
776d7179680SVasu Dev 
777d7179680SVasu Dev 	/*
778d7179680SVasu Dev 	 * Reuse existing offload em instance in case
7791d1b88dcSVasu Dev 	 * it is already allocated on real eth device
780d7179680SVasu Dev 	 */
78125024989SChris Leech 	if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
78225024989SChris Leech 		cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
7831d1b88dcSVasu Dev 	else
78425024989SChris Leech 		cur_real_dev = fcoe->netdev;
7851d1b88dcSVasu Dev 
78625024989SChris Leech 	list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
78725024989SChris Leech 		if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
78825024989SChris Leech 			old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
7891d1b88dcSVasu Dev 		else
79025024989SChris Leech 			old_real_dev = oldfcoe->netdev;
7911d1b88dcSVasu Dev 
7921d1b88dcSVasu Dev 		if (cur_real_dev == old_real_dev) {
793991cbb60SChris Leech 			fcoe->oem = oldfcoe->oem;
794d7179680SVasu Dev 			break;
795d7179680SVasu Dev 		}
796d7179680SVasu Dev 	}
797d7179680SVasu Dev 
798991cbb60SChris Leech 	if (fcoe->oem) {
7991875f27eSRobert Love 		if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
800d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to add "
801d7179680SVasu Dev 			       "offload em:%p on interface:%s\n",
802991cbb60SChris Leech 			       fcoe->oem, fcoe->netdev->name);
803a703e490SVasu Dev 			return -ENOMEM;
804d7179680SVasu Dev 		}
805d7179680SVasu Dev 	} else {
8061875f27eSRobert Love 		fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
8071875f27eSRobert Love 					      FCOE_MIN_XID, lport->lro_xid,
808d7179680SVasu Dev 					      fcoe_oem_match);
809991cbb60SChris Leech 		if (!fcoe->oem) {
810d7179680SVasu Dev 			printk(KERN_ERR "fcoe_em_config: failed to allocate "
811d7179680SVasu Dev 			       "em for offload exches on interface:%s\n",
81225024989SChris Leech 			       fcoe->netdev->name);
813d7179680SVasu Dev 			return -ENOMEM;
814d7179680SVasu Dev 		}
815d7179680SVasu Dev 	}
816d7179680SVasu Dev 
817d7179680SVasu Dev 	/*
818d7179680SVasu Dev 	 * Exclude offload EM xid range from next EM xid range.
819d7179680SVasu Dev 	 */
8201875f27eSRobert Love 	min_xid += lport->lro_xid + 1;
821d7179680SVasu Dev 
822d7179680SVasu Dev skip_oem:
8231875f27eSRobert Love 	if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
824d7179680SVasu Dev 		printk(KERN_ERR "fcoe_em_config: failed to "
82525024989SChris Leech 		       "allocate em on interface %s\n", fcoe->netdev->name);
826d7179680SVasu Dev 		return -ENOMEM;
827d7179680SVasu Dev 	}
828a703e490SVasu Dev 
829a703e490SVasu Dev 	return 0;
830a703e490SVasu Dev }
831a703e490SVasu Dev 
832a703e490SVasu Dev /**
8331875f27eSRobert Love  * fcoe_if_destroy() - Tear down a SW FCoE instance
8341875f27eSRobert Love  * @lport: The local port to be destroyed
83534ce27bcSVasu Dev  *
83634ce27bcSVasu Dev  * Locking: must be called with the RTNL mutex held and RTNL mutex
83734ce27bcSVasu Dev  * needed to be dropped by this function since not dropping RTNL
83834ce27bcSVasu Dev  * would cause circular locking warning on synchronous fip worker
83934ce27bcSVasu Dev  * cancelling thru fcoe_interface_put invoked by this function.
84034ce27bcSVasu Dev  *
841a703e490SVasu Dev  */
842af7f85d9SChris Leech static void fcoe_if_destroy(struct fc_lport *lport)
843a703e490SVasu Dev {
844014f5c3fSChris Leech 	struct fcoe_port *port = lport_priv(lport);
845014f5c3fSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
84625024989SChris Leech 	struct net_device *netdev = fcoe->netdev;
847a703e490SVasu Dev 
848d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
849a703e490SVasu Dev 
850a703e490SVasu Dev 	/* Logout of the fabric */
851af7f85d9SChris Leech 	fc_fabric_logoff(lport);
852a703e490SVasu Dev 
853a703e490SVasu Dev 	/* Cleanup the fc_lport */
854af7f85d9SChris Leech 	fc_lport_destroy(lport);
855a703e490SVasu Dev 
85654b649f8SChris Leech 	/* Stop the transmit retry timer */
85754b649f8SChris Leech 	del_timer_sync(&port->timer);
85854b649f8SChris Leech 
85954b649f8SChris Leech 	/* Free existing transmit skbs */
86054b649f8SChris Leech 	fcoe_clean_pending_queue(lport);
86154b649f8SChris Leech 
86211b56188SChris Leech 	if (!is_zero_ether_addr(port->data_src_addr))
863a748ee24SJiri Pirko 		dev_uc_del(netdev, port->data_src_addr);
86411b56188SChris Leech 	rtnl_unlock();
86511b56188SChris Leech 
86654b649f8SChris Leech 	/* receives may not be stopped until after this */
86754b649f8SChris Leech 	fcoe_interface_put(fcoe);
86854b649f8SChris Leech 
86954b649f8SChris Leech 	/* Free queued packets for the per-CPU receive threads */
87054b649f8SChris Leech 	fcoe_percpu_clean(lport);
87154b649f8SChris Leech 
872a703e490SVasu Dev 	/* Detach from the scsi-ml */
873af7f85d9SChris Leech 	fc_remove_host(lport->host);
874af7f85d9SChris Leech 	scsi_remove_host(lport->host);
875a703e490SVasu Dev 
87680e736f8SYi Zou 	/* Destroy lport scsi_priv */
87780e736f8SYi Zou 	fc_fcp_destroy(lport);
87880e736f8SYi Zou 
879a703e490SVasu Dev 	/* There are no more rports or I/O, free the EM */
880af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
881a703e490SVasu Dev 
882a703e490SVasu Dev 	/* Free memory used by statistical counters */
883af7f85d9SChris Leech 	fc_lport_free_stats(lport);
884a703e490SVasu Dev 
8852e70e241SChris Leech 	/* Release the Scsi_Host */
886af7f85d9SChris Leech 	scsi_host_put(lport->host);
887a703e490SVasu Dev }
888a703e490SVasu Dev 
8891875f27eSRobert Love /**
8901875f27eSRobert Love  * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
8911875f27eSRobert Love  * @lport: The local port to setup DDP for
8921875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
8931875f27eSRobert Love  * @sgl:   The scatterlist describing this transfer
8941875f27eSRobert Love  * @sgc:   The number of sg items
895a703e490SVasu Dev  *
8961875f27eSRobert Love  * Returns: 0 if the DDP context was not configured
897a703e490SVasu Dev  */
8981875f27eSRobert Love static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
899a703e490SVasu Dev 			  struct scatterlist *sgl, unsigned int sgc)
900a703e490SVasu Dev {
9011875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
902a703e490SVasu Dev 
9031875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
9041875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
9051875f27eSRobert Love 							      xid, sgl,
9061875f27eSRobert Love 							      sgc);
907a703e490SVasu Dev 
908a703e490SVasu Dev 	return 0;
909a703e490SVasu Dev }
910a703e490SVasu Dev 
911a703e490SVasu Dev /**
9121875f27eSRobert Love  * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
9131875f27eSRobert Love  * @lport: The local port to complete DDP on
9141875f27eSRobert Love  * @xid:   The exchange ID for this DDP transfer
915a703e490SVasu Dev  *
9161875f27eSRobert Love  * Returns: the length of data that have been completed by DDP
9171875f27eSRobert Love  */
9181875f27eSRobert Love static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
9191875f27eSRobert Love {
9201875f27eSRobert Love 	struct net_device *netdev = fcoe_netdev(lport);
9211875f27eSRobert Love 
9221875f27eSRobert Love 	if (netdev->netdev_ops->ndo_fcoe_ddp_done)
9231875f27eSRobert Love 		return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
9241875f27eSRobert Love 	return 0;
9251875f27eSRobert Love }
9261875f27eSRobert Love 
9271875f27eSRobert Love /**
9281875f27eSRobert Love  * fcoe_if_create() - Create a FCoE instance on an interface
9291875f27eSRobert Love  * @fcoe:   The FCoE interface to create a local port on
9301875f27eSRobert Love  * @parent: The device pointer to be the parent in sysfs for the SCSI host
9311875f27eSRobert Love  * @npiv:   Indicates if the port is a vport or not
9321875f27eSRobert Love  *
9331875f27eSRobert Love  * Creates a fc_lport instance and a Scsi_Host instance and configure them.
934a703e490SVasu Dev  *
935af7f85d9SChris Leech  * Returns: The allocated fc_lport or an error pointer
936a703e490SVasu Dev  */
937030f4e00SChris Leech static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
9389a05753bSChris Leech 				       struct device *parent, int npiv)
939a703e490SVasu Dev {
9401875f27eSRobert Love 	struct net_device *netdev = fcoe->netdev;
941af7f85d9SChris Leech 	struct fc_lport *lport = NULL;
942014f5c3fSChris Leech 	struct fcoe_port *port;
9431875f27eSRobert Love 	int rc;
9449a05753bSChris Leech 	/*
9459a05753bSChris Leech 	 * parent is only a vport if npiv is 1,
9469a05753bSChris Leech 	 * but we'll only use vport in that case so go ahead and set it
9479a05753bSChris Leech 	 */
9489a05753bSChris Leech 	struct fc_vport *vport = dev_to_vport(parent);
949a703e490SVasu Dev 
950d5488eb9SRobert Love 	FCOE_NETDEV_DBG(netdev, "Create Interface\n");
951a703e490SVasu Dev 
9529a05753bSChris Leech 	if (!npiv) {
95386221969SChris Leech 		lport = libfc_host_alloc(&fcoe_shost_template,
954014f5c3fSChris Leech 					 sizeof(struct fcoe_port));
9559a05753bSChris Leech 	} else	{
9569a05753bSChris Leech 		lport = libfc_vport_create(vport,
9579a05753bSChris Leech 					   sizeof(struct fcoe_port));
9589a05753bSChris Leech 	}
95986221969SChris Leech 	if (!lport) {
960014f5c3fSChris Leech 		FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
961014f5c3fSChris Leech 		rc = -ENOMEM;
962030f4e00SChris Leech 		goto out;
963014f5c3fSChris Leech 	}
964014f5c3fSChris Leech 	port = lport_priv(lport);
9652e70e241SChris Leech 	port->lport = lport;
966014f5c3fSChris Leech 	port->fcoe = fcoe;
9672e70e241SChris Leech 	INIT_WORK(&port->destroy_work, fcoe_destroy_work);
968a703e490SVasu Dev 
9691875f27eSRobert Love 	/* configure a fc_lport including the exchange manager */
970af7f85d9SChris Leech 	rc = fcoe_lport_config(lport);
971a703e490SVasu Dev 	if (rc) {
972d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
973d5488eb9SRobert Love 				"interface\n");
974a703e490SVasu Dev 		goto out_host_put;
975a703e490SVasu Dev 	}
976a703e490SVasu Dev 
9779a05753bSChris Leech 	if (npiv) {
9789f8f3aa6SChris Leech 		FCOE_NETDEV_DBG(netdev, "Setting vport names, "
9799f8f3aa6SChris Leech 				"%16.16llx %16.16llx\n",
9809a05753bSChris Leech 				vport->node_name, vport->port_name);
9819a05753bSChris Leech 		fc_set_wwnn(lport, vport->node_name);
9829a05753bSChris Leech 		fc_set_wwpn(lport, vport->port_name);
9839a05753bSChris Leech 	}
9849a05753bSChris Leech 
985ab6b85c1SVasu Dev 	/* configure lport network properties */
986af7f85d9SChris Leech 	rc = fcoe_netdev_config(lport, netdev);
987ab6b85c1SVasu Dev 	if (rc) {
988d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
989d5488eb9SRobert Love 				"interface\n");
99054b649f8SChris Leech 		goto out_lp_destroy;
991ab6b85c1SVasu Dev 	}
99297c8389dSJoe Eykholt 
993a703e490SVasu Dev 	/* configure lport scsi host properties */
9948ba00a4bSVasu Dev 	rc = fcoe_shost_config(lport, parent);
995a703e490SVasu Dev 	if (rc) {
996d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
997d5488eb9SRobert Love 				"interface\n");
99854b649f8SChris Leech 		goto out_lp_destroy;
999a703e490SVasu Dev 	}
1000a703e490SVasu Dev 
1001a703e490SVasu Dev 	/* Initialize the library */
1002e10f8c66SJoe Eykholt 	rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
1003a703e490SVasu Dev 	if (rc) {
1004d5488eb9SRobert Love 		FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1005d5488eb9SRobert Love 				"interface\n");
1006a703e490SVasu Dev 		goto out_lp_destroy;
1007a703e490SVasu Dev 	}
1008a703e490SVasu Dev 
10099a05753bSChris Leech 	if (!npiv) {
1010e8af4d43SVasu Dev 		/*
1011e8af4d43SVasu Dev 		 * fcoe_em_alloc() and fcoe_hostlist_add() both
10129a05753bSChris Leech 		 * need to be atomic with respect to other changes to the
10139a05753bSChris Leech 		 * hostlist since fcoe_em_alloc() looks for an existing EM
1014e8af4d43SVasu Dev 		 * instance on host list updated by fcoe_hostlist_add().
1015c863df33SChris Leech 		 *
10169a05753bSChris Leech 		 * This is currently handled through the fcoe_config_mutex
10179a05753bSChris Leech 		 * begin held.
1018e8af4d43SVasu Dev 		 */
1019c863df33SChris Leech 
102096316099SVasu Dev 		/* lport exch manager allocation */
1021af7f85d9SChris Leech 		rc = fcoe_em_config(lport);
102296316099SVasu Dev 		if (rc) {
10239a05753bSChris Leech 			FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
10249a05753bSChris Leech 					"for the interface\n");
102596316099SVasu Dev 			goto out_lp_destroy;
102696316099SVasu Dev 		}
10279a05753bSChris Leech 	}
102896316099SVasu Dev 
1029030f4e00SChris Leech 	fcoe_interface_get(fcoe);
1030af7f85d9SChris Leech 	return lport;
1031a703e490SVasu Dev 
1032a703e490SVasu Dev out_lp_destroy:
1033af7f85d9SChris Leech 	fc_exch_mgr_free(lport);
1034a703e490SVasu Dev out_host_put:
1035af7f85d9SChris Leech 	scsi_host_put(lport->host);
1036af7f85d9SChris Leech out:
1037af7f85d9SChris Leech 	return ERR_PTR(rc);
1038a703e490SVasu Dev }
1039a703e490SVasu Dev 
1040a703e490SVasu Dev /**
10411875f27eSRobert Love  * fcoe_if_init() - Initialization routine for fcoe.ko
10421875f27eSRobert Love  *
10431875f27eSRobert Love  * Attaches the SW FCoE transport to the FC transport
1044a703e490SVasu Dev  *
1045a703e490SVasu Dev  * Returns: 0 on success
1046a703e490SVasu Dev  */
1047a703e490SVasu Dev static int __init fcoe_if_init(void)
1048a703e490SVasu Dev {
1049a703e490SVasu Dev 	/* attach to scsi transport */
10508ca86f84SYi Zou 	fcoe_nport_scsi_transport =
10518ca86f84SYi Zou 		fc_attach_transport(&fcoe_nport_fc_functions);
10528ca86f84SYi Zou 	fcoe_vport_scsi_transport =
10538ca86f84SYi Zou 		fc_attach_transport(&fcoe_vport_fc_functions);
1054a703e490SVasu Dev 
10558ca86f84SYi Zou 	if (!fcoe_nport_scsi_transport) {
1056d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1057a703e490SVasu Dev 		return -ENODEV;
1058a703e490SVasu Dev 	}
1059a703e490SVasu Dev 
1060a703e490SVasu Dev 	return 0;
1061a703e490SVasu Dev }
1062a703e490SVasu Dev 
1063a703e490SVasu Dev /**
10641875f27eSRobert Love  * fcoe_if_exit() - Tear down fcoe.ko
10651875f27eSRobert Love  *
10661875f27eSRobert Love  * Detaches the SW FCoE transport from the FC transport
1067a703e490SVasu Dev  *
1068a703e490SVasu Dev  * Returns: 0 on success
1069a703e490SVasu Dev  */
1070a703e490SVasu Dev int __exit fcoe_if_exit(void)
1071a703e490SVasu Dev {
10728ca86f84SYi Zou 	fc_release_transport(fcoe_nport_scsi_transport);
10738ca86f84SYi Zou 	fc_release_transport(fcoe_vport_scsi_transport);
10748ca86f84SYi Zou 	fcoe_nport_scsi_transport = NULL;
10758ca86f84SYi Zou 	fcoe_vport_scsi_transport = NULL;
1076a703e490SVasu Dev 	return 0;
1077a703e490SVasu Dev }
1078a703e490SVasu Dev 
1079a703e490SVasu Dev /**
10801875f27eSRobert Love  * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
10811875f27eSRobert Love  * @cpu: The CPU index of the CPU to create a receive thread for
1082a703e490SVasu Dev  */
1083a703e490SVasu Dev static void fcoe_percpu_thread_create(unsigned int cpu)
1084a703e490SVasu Dev {
1085a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1086a703e490SVasu Dev 	struct task_struct *thread;
1087a703e490SVasu Dev 
1088a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1089a703e490SVasu Dev 
1090a703e490SVasu Dev 	thread = kthread_create(fcoe_percpu_receive_thread,
1091a703e490SVasu Dev 				(void *)p, "fcoethread/%d", cpu);
1092a703e490SVasu Dev 
1093e7a51997SJoe Eykholt 	if (likely(!IS_ERR(thread))) {
1094a703e490SVasu Dev 		kthread_bind(thread, cpu);
1095a703e490SVasu Dev 		wake_up_process(thread);
1096a703e490SVasu Dev 
1097a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1098a703e490SVasu Dev 		p->thread = thread;
1099a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1100a703e490SVasu Dev 	}
1101a703e490SVasu Dev }
1102a703e490SVasu Dev 
1103a703e490SVasu Dev /**
11041875f27eSRobert Love  * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
11051875f27eSRobert Love  * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
1106a703e490SVasu Dev  *
1107a703e490SVasu Dev  * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1108a703e490SVasu Dev  * current CPU's Rx thread. If the thread being destroyed is bound to
1109a703e490SVasu Dev  * the CPU processing this context the skbs will be freed.
1110a703e490SVasu Dev  */
1111a703e490SVasu Dev static void fcoe_percpu_thread_destroy(unsigned int cpu)
1112a703e490SVasu Dev {
1113a703e490SVasu Dev 	struct fcoe_percpu_s *p;
1114a703e490SVasu Dev 	struct task_struct *thread;
1115a703e490SVasu Dev 	struct page *crc_eof;
1116a703e490SVasu Dev 	struct sk_buff *skb;
1117a703e490SVasu Dev #ifdef CONFIG_SMP
1118a703e490SVasu Dev 	struct fcoe_percpu_s *p0;
1119f018b73aSJoe Eykholt 	unsigned targ_cpu = get_cpu();
1120a703e490SVasu Dev #endif /* CONFIG_SMP */
1121a703e490SVasu Dev 
1122d5488eb9SRobert Love 	FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
1123a703e490SVasu Dev 
1124a703e490SVasu Dev 	/* Prevent any new skbs from being queued for this CPU. */
1125a703e490SVasu Dev 	p = &per_cpu(fcoe_percpu, cpu);
1126a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1127a703e490SVasu Dev 	thread = p->thread;
1128a703e490SVasu Dev 	p->thread = NULL;
1129a703e490SVasu Dev 	crc_eof = p->crc_eof_page;
1130a703e490SVasu Dev 	p->crc_eof_page = NULL;
1131a703e490SVasu Dev 	p->crc_eof_offset = 0;
1132a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1133a703e490SVasu Dev 
1134a703e490SVasu Dev #ifdef CONFIG_SMP
1135a703e490SVasu Dev 	/*
1136a703e490SVasu Dev 	 * Don't bother moving the skb's if this context is running
1137a703e490SVasu Dev 	 * on the same CPU that is having its thread destroyed. This
1138a703e490SVasu Dev 	 * can easily happen when the module is removed.
1139a703e490SVasu Dev 	 */
1140a703e490SVasu Dev 	if (cpu != targ_cpu) {
1141a703e490SVasu Dev 		p0 = &per_cpu(fcoe_percpu, targ_cpu);
1142a703e490SVasu Dev 		spin_lock_bh(&p0->fcoe_rx_list.lock);
1143a703e490SVasu Dev 		if (p0->thread) {
1144d5488eb9SRobert Love 			FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1145a703e490SVasu Dev 				 cpu, targ_cpu);
1146a703e490SVasu Dev 
1147a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1148a703e490SVasu Dev 				__skb_queue_tail(&p0->fcoe_rx_list, skb);
1149a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1150a703e490SVasu Dev 		} else {
1151a703e490SVasu Dev 			/*
1152a703e490SVasu Dev 			 * The targeted CPU is not initialized and cannot accept
1153a703e490SVasu Dev 			 * new	skbs. Unlock the targeted CPU and drop the skbs
1154a703e490SVasu Dev 			 * on the CPU that is going offline.
1155a703e490SVasu Dev 			 */
1156a703e490SVasu Dev 			while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1157a703e490SVasu Dev 				kfree_skb(skb);
1158a703e490SVasu Dev 			spin_unlock_bh(&p0->fcoe_rx_list.lock);
1159a703e490SVasu Dev 		}
1160a703e490SVasu Dev 	} else {
1161a703e490SVasu Dev 		/*
1162a703e490SVasu Dev 		 * This scenario occurs when the module is being removed
1163a703e490SVasu Dev 		 * and all threads are being destroyed. skbs will continue
1164a703e490SVasu Dev 		 * to be shifted from the CPU thread that is being removed
1165a703e490SVasu Dev 		 * to the CPU thread associated with the CPU that is processing
1166a703e490SVasu Dev 		 * the module removal. Once there is only one CPU Rx thread it
1167a703e490SVasu Dev 		 * will reach this case and we will drop all skbs and later
1168a703e490SVasu Dev 		 * stop the thread.
1169a703e490SVasu Dev 		 */
1170a703e490SVasu Dev 		spin_lock_bh(&p->fcoe_rx_list.lock);
1171a703e490SVasu Dev 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1172a703e490SVasu Dev 			kfree_skb(skb);
1173a703e490SVasu Dev 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1174a703e490SVasu Dev 	}
1175f018b73aSJoe Eykholt 	put_cpu();
1176a703e490SVasu Dev #else
1177a703e490SVasu Dev 	/*
1178dd3fd72eSChris Leech 	 * This a non-SMP scenario where the singular Rx thread is
1179a703e490SVasu Dev 	 * being removed. Free all skbs and stop the thread.
1180a703e490SVasu Dev 	 */
1181a703e490SVasu Dev 	spin_lock_bh(&p->fcoe_rx_list.lock);
1182a703e490SVasu Dev 	while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1183a703e490SVasu Dev 		kfree_skb(skb);
1184a703e490SVasu Dev 	spin_unlock_bh(&p->fcoe_rx_list.lock);
1185a703e490SVasu Dev #endif
1186a703e490SVasu Dev 
1187a703e490SVasu Dev 	if (thread)
1188a703e490SVasu Dev 		kthread_stop(thread);
1189a703e490SVasu Dev 
1190a703e490SVasu Dev 	if (crc_eof)
1191a703e490SVasu Dev 		put_page(crc_eof);
1192a703e490SVasu Dev }
1193a703e490SVasu Dev 
1194a703e490SVasu Dev /**
11951875f27eSRobert Love  * fcoe_cpu_callback() - Handler for CPU hotplug events
11961875f27eSRobert Love  * @nfb:    The callback data block
11971875f27eSRobert Love  * @action: The event triggering the callback
11981875f27eSRobert Love  * @hcpu:   The index of the CPU that the event is for
1199a703e490SVasu Dev  *
12001875f27eSRobert Love  * This creates or destroys per-CPU data for fcoe
1201a703e490SVasu Dev  *
1202a703e490SVasu Dev  * Returns NOTIFY_OK always.
1203a703e490SVasu Dev  */
1204a703e490SVasu Dev static int fcoe_cpu_callback(struct notifier_block *nfb,
1205a703e490SVasu Dev 			     unsigned long action, void *hcpu)
1206a703e490SVasu Dev {
1207a703e490SVasu Dev 	unsigned cpu = (unsigned long)hcpu;
1208a703e490SVasu Dev 
1209a703e490SVasu Dev 	switch (action) {
1210a703e490SVasu Dev 	case CPU_ONLINE:
1211a703e490SVasu Dev 	case CPU_ONLINE_FROZEN:
1212d5488eb9SRobert Love 		FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1213a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
1214a703e490SVasu Dev 		break;
1215a703e490SVasu Dev 	case CPU_DEAD:
1216a703e490SVasu Dev 	case CPU_DEAD_FROZEN:
1217d5488eb9SRobert Love 		FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1218a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
1219a703e490SVasu Dev 		break;
1220a703e490SVasu Dev 	default:
1221a703e490SVasu Dev 		break;
1222a703e490SVasu Dev 	}
1223a703e490SVasu Dev 	return NOTIFY_OK;
1224a703e490SVasu Dev }
1225a703e490SVasu Dev 
1226a703e490SVasu Dev /**
12271875f27eSRobert Love  * fcoe_rcv() - Receive packets from a net device
12281875f27eSRobert Love  * @skb:    The received packet
12291875f27eSRobert Love  * @netdev: The net device that the packet was received on
12301875f27eSRobert Love  * @ptype:  The packet type context
12311875f27eSRobert Love  * @olddev: The last device net device
1232a703e490SVasu Dev  *
12331875f27eSRobert Love  * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
12341875f27eSRobert Love  * FC frame and passes the frame to libfc.
1235a703e490SVasu Dev  *
1236a703e490SVasu Dev  * Returns: 0 for success
1237a703e490SVasu Dev  */
12381875f27eSRobert Love int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1239a703e490SVasu Dev 	     struct packet_type *ptype, struct net_device *olddev)
1240a703e490SVasu Dev {
12411875f27eSRobert Love 	struct fc_lport *lport;
1242a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1243259ad85dSChris Leech 	struct fcoe_interface *fcoe;
1244a703e490SVasu Dev 	struct fc_frame_header *fh;
1245a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1246519e5135SVasu Dev 	struct ethhdr *eh;
1247b2f0091fSVasu Dev 	unsigned int cpu;
1248a703e490SVasu Dev 
1249259ad85dSChris Leech 	fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
12501875f27eSRobert Love 	lport = fcoe->ctlr.lp;
12511875f27eSRobert Love 	if (unlikely(!lport)) {
12521875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
1253a703e490SVasu Dev 		goto err2;
1254a703e490SVasu Dev 	}
12551875f27eSRobert Love 	if (!lport->link_up)
125697c8389dSJoe Eykholt 		goto err2;
1257a703e490SVasu Dev 
12581875f27eSRobert Love 	FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
1259d5488eb9SRobert Love 			"data:%p tail:%p end:%p sum:%d dev:%s",
1260d5488eb9SRobert Love 			skb->len, skb->data_len, skb->head, skb->data,
1261d5488eb9SRobert Love 			skb_tail_pointer(skb), skb_end_pointer(skb),
1262d5488eb9SRobert Love 			skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1263a703e490SVasu Dev 
1264519e5135SVasu Dev 	eh = eth_hdr(skb);
1265519e5135SVasu Dev 
1266519e5135SVasu Dev 	if (is_fip_mode(&fcoe->ctlr) &&
1267519e5135SVasu Dev 	    compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1268519e5135SVasu Dev 		FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1269519e5135SVasu Dev 				eh->h_source);
1270a703e490SVasu Dev 		goto err;
1271a703e490SVasu Dev 	}
1272a703e490SVasu Dev 
1273a703e490SVasu Dev 	/*
1274a703e490SVasu Dev 	 * Check for minimum frame length, and make sure required FCoE
1275a703e490SVasu Dev 	 * and FC headers are pulled into the linear data area.
1276a703e490SVasu Dev 	 */
1277a703e490SVasu Dev 	if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1278a703e490SVasu Dev 		     !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1279a703e490SVasu Dev 		goto err;
1280a703e490SVasu Dev 
1281a703e490SVasu Dev 	skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1282a703e490SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
1283a703e490SVasu Dev 
12840ee31cb5SRobert Love 	if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
12850ee31cb5SRobert Love 		FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
12860ee31cb5SRobert Love 				eh->h_dest);
12870ee31cb5SRobert Love 		goto err;
12880ee31cb5SRobert Love 	}
12890ee31cb5SRobert Love 
1290a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
12911875f27eSRobert Love 	fr->fr_dev = lport;
1292a703e490SVasu Dev 	fr->ptype = ptype;
1293a703e490SVasu Dev 
1294a703e490SVasu Dev 	/*
1295b2f0091fSVasu Dev 	 * In case the incoming frame's exchange is originated from
1296b2f0091fSVasu Dev 	 * the initiator, then received frame's exchange id is ANDed
1297b2f0091fSVasu Dev 	 * with fc_cpu_mask bits to get the same cpu on which exchange
1298b2f0091fSVasu Dev 	 * was originated, otherwise just use the current cpu.
1299a703e490SVasu Dev 	 */
1300b2f0091fSVasu Dev 	if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1301b2f0091fSVasu Dev 		cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1302b2f0091fSVasu Dev 	else
1303b2f0091fSVasu Dev 		cpu = smp_processor_id();
1304a703e490SVasu Dev 
1305a703e490SVasu Dev 	fps = &per_cpu(fcoe_percpu, cpu);
1306a703e490SVasu Dev 	spin_lock_bh(&fps->fcoe_rx_list.lock);
1307a703e490SVasu Dev 	if (unlikely(!fps->thread)) {
1308a703e490SVasu Dev 		/*
1309a703e490SVasu Dev 		 * The targeted CPU is not ready, let's target
1310a703e490SVasu Dev 		 * the first CPU now. For non-SMP systems this
1311a703e490SVasu Dev 		 * will check the same CPU twice.
1312a703e490SVasu Dev 		 */
13131875f27eSRobert Love 		FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
1314d5488eb9SRobert Love 				"ready for incoming skb- using first online "
1315d5488eb9SRobert Love 				"CPU.\n");
1316a703e490SVasu Dev 
1317a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
13186957177fSRusty Russell 		cpu = cpumask_first(cpu_online_mask);
1319a703e490SVasu Dev 		fps = &per_cpu(fcoe_percpu, cpu);
1320a703e490SVasu Dev 		spin_lock_bh(&fps->fcoe_rx_list.lock);
1321a703e490SVasu Dev 		if (!fps->thread) {
1322a703e490SVasu Dev 			spin_unlock_bh(&fps->fcoe_rx_list.lock);
1323a703e490SVasu Dev 			goto err;
1324a703e490SVasu Dev 		}
1325a703e490SVasu Dev 	}
1326a703e490SVasu Dev 
1327a703e490SVasu Dev 	/*
1328a703e490SVasu Dev 	 * We now have a valid CPU that we're targeting for
1329a703e490SVasu Dev 	 * this skb. We also have this receive thread locked,
1330a703e490SVasu Dev 	 * so we're free to queue skbs into it's queue.
1331a703e490SVasu Dev 	 */
1332859b7b64SChris Leech 
1333859b7b64SChris Leech 	/* If this is a SCSI-FCP frame, and this is already executing on the
1334859b7b64SChris Leech 	 * correct CPU, and the queue for this CPU is empty, then go ahead
1335859b7b64SChris Leech 	 * and process the frame directly in the softirq context.
1336859b7b64SChris Leech 	 * This lets us process completions without context switching from the
1337859b7b64SChris Leech 	 * NET_RX softirq, to our receive processing thread, and then back to
1338859b7b64SChris Leech 	 * BLOCK softirq context.
1339859b7b64SChris Leech 	 */
1340859b7b64SChris Leech 	if (fh->fh_type == FC_TYPE_FCP &&
1341859b7b64SChris Leech 	    cpu == smp_processor_id() &&
1342859b7b64SChris Leech 	    skb_queue_empty(&fps->fcoe_rx_list)) {
1343859b7b64SChris Leech 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1344859b7b64SChris Leech 		fcoe_recv_frame(skb);
1345859b7b64SChris Leech 	} else {
1346a703e490SVasu Dev 		__skb_queue_tail(&fps->fcoe_rx_list, skb);
1347a703e490SVasu Dev 		if (fps->fcoe_rx_list.qlen == 1)
1348a703e490SVasu Dev 			wake_up_process(fps->thread);
1349a703e490SVasu Dev 		spin_unlock_bh(&fps->fcoe_rx_list.lock);
1350859b7b64SChris Leech 	}
1351a703e490SVasu Dev 
1352a703e490SVasu Dev 	return 0;
1353a703e490SVasu Dev err:
1354f018b73aSJoe Eykholt 	per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1355f018b73aSJoe Eykholt 	put_cpu();
1356a703e490SVasu Dev err2:
1357a703e490SVasu Dev 	kfree_skb(skb);
1358a703e490SVasu Dev 	return -1;
1359a703e490SVasu Dev }
1360a703e490SVasu Dev 
1361a703e490SVasu Dev /**
13621875f27eSRobert Love  * fcoe_start_io() - Start FCoE I/O
13631875f27eSRobert Love  * @skb: The packet to be transmitted
13641875f27eSRobert Love  *
13651875f27eSRobert Love  * This routine is called from the net device to start transmitting
13661875f27eSRobert Love  * FCoE packets.
1367a703e490SVasu Dev  *
1368a703e490SVasu Dev  * Returns: 0 for success
1369a703e490SVasu Dev  */
1370a703e490SVasu Dev static inline int fcoe_start_io(struct sk_buff *skb)
1371a703e490SVasu Dev {
137218fa11efSChris Leech 	struct sk_buff *nskb;
1373a703e490SVasu Dev 	int rc;
1374a703e490SVasu Dev 
137518fa11efSChris Leech 	nskb = skb_clone(skb, GFP_ATOMIC);
137618fa11efSChris Leech 	rc = dev_queue_xmit(nskb);
1377a703e490SVasu Dev 	if (rc != 0)
1378a703e490SVasu Dev 		return rc;
1379a703e490SVasu Dev 	kfree_skb(skb);
1380a703e490SVasu Dev 	return 0;
1381a703e490SVasu Dev }
1382a703e490SVasu Dev 
1383a703e490SVasu Dev /**
13841875f27eSRobert Love  * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
13851875f27eSRobert Love  * @skb:  The packet to be transmitted
13861875f27eSRobert Love  * @tlen: The total length of the trailer
13871875f27eSRobert Love  *
13881875f27eSRobert Love  * This routine allocates a page for frame trailers. The page is re-used if
13891875f27eSRobert Love  * there is enough room left on it for the current trailer. If there isn't
13901875f27eSRobert Love  * enough buffer left a new page is allocated for the trailer. Reference to
13911875f27eSRobert Love  * the page from this function as well as the skbs using the page fragments
13921875f27eSRobert Love  * ensure that the page is freed at the appropriate time.
1393a703e490SVasu Dev  *
1394a703e490SVasu Dev  * Returns: 0 for success
1395a703e490SVasu Dev  */
1396a703e490SVasu Dev static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1397a703e490SVasu Dev {
1398a703e490SVasu Dev 	struct fcoe_percpu_s *fps;
1399a703e490SVasu Dev 	struct page *page;
1400a703e490SVasu Dev 
1401a703e490SVasu Dev 	fps = &get_cpu_var(fcoe_percpu);
1402a703e490SVasu Dev 	page = fps->crc_eof_page;
1403a703e490SVasu Dev 	if (!page) {
1404a703e490SVasu Dev 		page = alloc_page(GFP_ATOMIC);
1405a703e490SVasu Dev 		if (!page) {
1406a703e490SVasu Dev 			put_cpu_var(fcoe_percpu);
1407a703e490SVasu Dev 			return -ENOMEM;
1408a703e490SVasu Dev 		}
1409a703e490SVasu Dev 		fps->crc_eof_page = page;
1410a703e490SVasu Dev 		fps->crc_eof_offset = 0;
1411a703e490SVasu Dev 	}
1412a703e490SVasu Dev 
1413a703e490SVasu Dev 	get_page(page);
1414a703e490SVasu Dev 	skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1415a703e490SVasu Dev 			   fps->crc_eof_offset, tlen);
1416a703e490SVasu Dev 	skb->len += tlen;
1417a703e490SVasu Dev 	skb->data_len += tlen;
1418a703e490SVasu Dev 	skb->truesize += tlen;
1419a703e490SVasu Dev 	fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1420a703e490SVasu Dev 
1421a703e490SVasu Dev 	if (fps->crc_eof_offset >= PAGE_SIZE) {
1422a703e490SVasu Dev 		fps->crc_eof_page = NULL;
1423a703e490SVasu Dev 		fps->crc_eof_offset = 0;
1424a703e490SVasu Dev 		put_page(page);
1425a703e490SVasu Dev 	}
1426a703e490SVasu Dev 	put_cpu_var(fcoe_percpu);
1427a703e490SVasu Dev 	return 0;
1428a703e490SVasu Dev }
1429a703e490SVasu Dev 
1430a703e490SVasu Dev /**
14311875f27eSRobert Love  * fcoe_fc_crc() - Calculates the CRC for a given frame
14321875f27eSRobert Love  * @fp: The frame to be checksumed
1433a703e490SVasu Dev  *
14341875f27eSRobert Love  * This uses crc32() routine to calculate the CRC for a frame
14351875f27eSRobert Love  *
14361875f27eSRobert Love  * Return: The 32 bit CRC value
1437a703e490SVasu Dev  */
1438a703e490SVasu Dev u32 fcoe_fc_crc(struct fc_frame *fp)
1439a703e490SVasu Dev {
1440a703e490SVasu Dev 	struct sk_buff *skb = fp_skb(fp);
1441a703e490SVasu Dev 	struct skb_frag_struct *frag;
1442a703e490SVasu Dev 	unsigned char *data;
1443a703e490SVasu Dev 	unsigned long off, len, clen;
1444a703e490SVasu Dev 	u32 crc;
1445a703e490SVasu Dev 	unsigned i;
1446a703e490SVasu Dev 
1447a703e490SVasu Dev 	crc = crc32(~0, skb->data, skb_headlen(skb));
1448a703e490SVasu Dev 
1449a703e490SVasu Dev 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1450a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[i];
1451a703e490SVasu Dev 		off = frag->page_offset;
1452a703e490SVasu Dev 		len = frag->size;
1453a703e490SVasu Dev 		while (len > 0) {
1454a703e490SVasu Dev 			clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1455a703e490SVasu Dev 			data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1456a703e490SVasu Dev 					   KM_SKB_DATA_SOFTIRQ);
1457a703e490SVasu Dev 			crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1458a703e490SVasu Dev 			kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1459a703e490SVasu Dev 			off += clen;
1460a703e490SVasu Dev 			len -= clen;
1461a703e490SVasu Dev 		}
1462a703e490SVasu Dev 	}
1463a703e490SVasu Dev 	return crc;
1464a703e490SVasu Dev }
1465a703e490SVasu Dev 
1466a703e490SVasu Dev /**
14671875f27eSRobert Love  * fcoe_xmit() - Transmit a FCoE frame
14681875f27eSRobert Love  * @lport: The local port that the frame is to be transmitted for
14691875f27eSRobert Love  * @fp:	   The frame to be transmitted
1470a703e490SVasu Dev  *
1471a703e490SVasu Dev  * Return: 0 for success
1472a703e490SVasu Dev  */
14731875f27eSRobert Love int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1474a703e490SVasu Dev {
14754bb6b515SVasu Dev 	int wlen;
1476a703e490SVasu Dev 	u32 crc;
1477a703e490SVasu Dev 	struct ethhdr *eh;
1478a703e490SVasu Dev 	struct fcoe_crc_eof *cp;
1479a703e490SVasu Dev 	struct sk_buff *skb;
1480a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1481a703e490SVasu Dev 	struct fc_frame_header *fh;
1482a703e490SVasu Dev 	unsigned int hlen;		/* header length implies the version */
1483a703e490SVasu Dev 	unsigned int tlen;		/* trailer length */
1484a703e490SVasu Dev 	unsigned int elen;		/* eth header, may include vlan */
14851875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
14863fe9a0baSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
1487a703e490SVasu Dev 	u8 sof, eof;
1488a703e490SVasu Dev 	struct fcoe_hdr *hp;
1489a703e490SVasu Dev 
1490a703e490SVasu Dev 	WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1491a703e490SVasu Dev 
1492a703e490SVasu Dev 	fh = fc_frame_header_get(fp);
149397c8389dSJoe Eykholt 	skb = fp_skb(fp);
149497c8389dSJoe Eykholt 	wlen = skb->len / FCOE_WORD_TO_BYTE;
149597c8389dSJoe Eykholt 
14961875f27eSRobert Love 	if (!lport->link_up) {
14973caf02eeSDan Carpenter 		kfree_skb(skb);
149897c8389dSJoe Eykholt 		return 0;
1499a703e490SVasu Dev 	}
1500a703e490SVasu Dev 
15019860eeb4SJoe Eykholt 	if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
15021875f27eSRobert Love 	    fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
150397c8389dSJoe Eykholt 		return 0;
150497c8389dSJoe Eykholt 
1505a703e490SVasu Dev 	sof = fr_sof(fp);
1506a703e490SVasu Dev 	eof = fr_eof(fp);
1507a703e490SVasu Dev 
15084e57e1cbSVasu Dev 	elen = sizeof(struct ethhdr);
1509a703e490SVasu Dev 	hlen = sizeof(struct fcoe_hdr);
1510a703e490SVasu Dev 	tlen = sizeof(struct fcoe_crc_eof);
1511a703e490SVasu Dev 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1512a703e490SVasu Dev 
1513a703e490SVasu Dev 	/* crc offload */
15141875f27eSRobert Love 	if (likely(lport->crc_offload)) {
1515a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_PARTIAL;
1516a703e490SVasu Dev 		skb->csum_start = skb_headroom(skb);
1517a703e490SVasu Dev 		skb->csum_offset = skb->len;
1518a703e490SVasu Dev 		crc = 0;
1519a703e490SVasu Dev 	} else {
1520a703e490SVasu Dev 		skb->ip_summed = CHECKSUM_NONE;
1521a703e490SVasu Dev 		crc = fcoe_fc_crc(fp);
1522a703e490SVasu Dev 	}
1523a703e490SVasu Dev 
1524014f5c3fSChris Leech 	/* copy port crc and eof to the skb buff */
1525a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1526a703e490SVasu Dev 		skb_frag_t *frag;
1527a703e490SVasu Dev 		if (fcoe_get_paged_crc_eof(skb, tlen)) {
1528a703e490SVasu Dev 			kfree_skb(skb);
1529a703e490SVasu Dev 			return -ENOMEM;
1530a703e490SVasu Dev 		}
1531a703e490SVasu Dev 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1532a703e490SVasu Dev 		cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1533a703e490SVasu Dev 			+ frag->page_offset;
1534a703e490SVasu Dev 	} else {
1535a703e490SVasu Dev 		cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1536a703e490SVasu Dev 	}
1537a703e490SVasu Dev 
1538a703e490SVasu Dev 	memset(cp, 0, sizeof(*cp));
1539a703e490SVasu Dev 	cp->fcoe_eof = eof;
1540a703e490SVasu Dev 	cp->fcoe_crc32 = cpu_to_le32(~crc);
1541a703e490SVasu Dev 
1542a703e490SVasu Dev 	if (skb_is_nonlinear(skb)) {
1543a703e490SVasu Dev 		kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1544a703e490SVasu Dev 		cp = NULL;
1545a703e490SVasu Dev 	}
1546a703e490SVasu Dev 
1547014f5c3fSChris Leech 	/* adjust skb network/transport offsets to match mac/fcoe/port */
1548a703e490SVasu Dev 	skb_push(skb, elen + hlen);
1549a703e490SVasu Dev 	skb_reset_mac_header(skb);
1550a703e490SVasu Dev 	skb_reset_network_header(skb);
1551a703e490SVasu Dev 	skb->mac_len = elen;
1552a703e490SVasu Dev 	skb->protocol = htons(ETH_P_FCOE);
15533fe9a0baSChris Leech 	skb->dev = fcoe->netdev;
1554a703e490SVasu Dev 
1555a703e490SVasu Dev 	/* fill up mac and fcoe headers */
1556a703e490SVasu Dev 	eh = eth_hdr(skb);
1557a703e490SVasu Dev 	eh->h_proto = htons(ETH_P_FCOE);
15583fe9a0baSChris Leech 	memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1559cd229e42SJoe Eykholt 	if (fcoe->ctlr.map_dest)
1560cd229e42SJoe Eykholt 		memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1561a703e490SVasu Dev 
15623fe9a0baSChris Leech 	if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
15633fe9a0baSChris Leech 		memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1564a703e490SVasu Dev 	else
156511b56188SChris Leech 		memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1566a703e490SVasu Dev 
1567a703e490SVasu Dev 	hp = (struct fcoe_hdr *)(eh + 1);
1568a703e490SVasu Dev 	memset(hp, 0, sizeof(*hp));
1569a703e490SVasu Dev 	if (FC_FCOE_VER)
1570a703e490SVasu Dev 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1571a703e490SVasu Dev 	hp->fcoe_sof = sof;
1572a703e490SVasu Dev 
1573a703e490SVasu Dev 	/* fcoe lso, mss is in max_payload which is non-zero for FCP data */
15741875f27eSRobert Love 	if (lport->seq_offload && fr_max_payload(fp)) {
1575a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1576a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1577a703e490SVasu Dev 	} else {
1578a703e490SVasu Dev 		skb_shinfo(skb)->gso_type = 0;
1579a703e490SVasu Dev 		skb_shinfo(skb)->gso_size = 0;
1580a703e490SVasu Dev 	}
1581a703e490SVasu Dev 	/* update tx stats: regardless if LLD fails */
1582f018b73aSJoe Eykholt 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1583a703e490SVasu Dev 	stats->TxFrames++;
1584a703e490SVasu Dev 	stats->TxWords += wlen;
1585f018b73aSJoe Eykholt 	put_cpu();
1586a703e490SVasu Dev 
1587a703e490SVasu Dev 	/* send down to lld */
15881875f27eSRobert Love 	fr_dev(fp) = lport;
1589014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen)
15901875f27eSRobert Love 		fcoe_check_wait_queue(lport, skb);
15914bb6b515SVasu Dev 	else if (fcoe_start_io(skb))
15921875f27eSRobert Love 		fcoe_check_wait_queue(lport, skb);
1593a703e490SVasu Dev 
1594a703e490SVasu Dev 	return 0;
1595a703e490SVasu Dev }
1596a703e490SVasu Dev 
1597a703e490SVasu Dev /**
15981875f27eSRobert Love  * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
15991875f27eSRobert Love  * @skb: The completed skb (argument required by destructor)
1600e7a51997SJoe Eykholt  */
1601e7a51997SJoe Eykholt static void fcoe_percpu_flush_done(struct sk_buff *skb)
1602e7a51997SJoe Eykholt {
1603e7a51997SJoe Eykholt 	complete(&fcoe_flush_completion);
1604e7a51997SJoe Eykholt }
1605e7a51997SJoe Eykholt 
1606e7a51997SJoe Eykholt /**
160752ee8321SVasu Dev  * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
160852ee8321SVasu Dev  * @lport: The local port the frame was received on
160952ee8321SVasu Dev  * @fp:	   The received frame
161052ee8321SVasu Dev  *
161152ee8321SVasu Dev  * Return: 0 on passing filtering checks
161252ee8321SVasu Dev  */
161352ee8321SVasu Dev static inline int fcoe_filter_frames(struct fc_lport *lport,
161452ee8321SVasu Dev 				     struct fc_frame *fp)
161552ee8321SVasu Dev {
161652ee8321SVasu Dev 	struct fcoe_interface *fcoe;
161752ee8321SVasu Dev 	struct fc_frame_header *fh;
161852ee8321SVasu Dev 	struct sk_buff *skb = (struct sk_buff *)fp;
161952ee8321SVasu Dev 	struct fcoe_dev_stats *stats;
162052ee8321SVasu Dev 
162152ee8321SVasu Dev 	/*
162252ee8321SVasu Dev 	 * We only check CRC if no offload is available and if it is
162352ee8321SVasu Dev 	 * it's solicited data, in which case, the FCP layer would
162452ee8321SVasu Dev 	 * check it during the copy.
162552ee8321SVasu Dev 	 */
162652ee8321SVasu Dev 	if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
162752ee8321SVasu Dev 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
162852ee8321SVasu Dev 	else
162952ee8321SVasu Dev 		fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
163052ee8321SVasu Dev 
163152ee8321SVasu Dev 	fh = (struct fc_frame_header *) skb_transport_header(skb);
163252ee8321SVasu Dev 	fh = fc_frame_header_get(fp);
163352ee8321SVasu Dev 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
163452ee8321SVasu Dev 		return 0;
163552ee8321SVasu Dev 
163652ee8321SVasu Dev 	fcoe = ((struct fcoe_port *)lport_priv(lport))->fcoe;
163752ee8321SVasu Dev 	if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
163852ee8321SVasu Dev 	    ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
163952ee8321SVasu Dev 		FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
164052ee8321SVasu Dev 		return -EINVAL;
164152ee8321SVasu Dev 	}
164252ee8321SVasu Dev 
164352ee8321SVasu Dev 	if (!fr_flags(fp) & FCPHF_CRC_UNCHECKED ||
164452ee8321SVasu Dev 	    le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
164552ee8321SVasu Dev 		fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
164652ee8321SVasu Dev 		return 0;
164752ee8321SVasu Dev 	}
164852ee8321SVasu Dev 
164952ee8321SVasu Dev 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
165052ee8321SVasu Dev 	stats->InvalidCRCCount++;
165152ee8321SVasu Dev 	if (stats->InvalidCRCCount < 5)
165252ee8321SVasu Dev 		printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
165352ee8321SVasu Dev 	return -EINVAL;
165452ee8321SVasu Dev }
165552ee8321SVasu Dev 
165652ee8321SVasu Dev /**
1657859b7b64SChris Leech  * fcoe_recv_frame() - process a single received frame
1658859b7b64SChris Leech  * @skb: frame to process
1659a703e490SVasu Dev  */
1660859b7b64SChris Leech static void fcoe_recv_frame(struct sk_buff *skb)
1661a703e490SVasu Dev {
1662a703e490SVasu Dev 	u32 fr_len;
16631875f27eSRobert Love 	struct fc_lport *lport;
1664a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
1665a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
1666a703e490SVasu Dev 	struct fcoe_crc_eof crc_eof;
1667a703e490SVasu Dev 	struct fc_frame *fp;
1668014f5c3fSChris Leech 	struct fcoe_port *port;
1669a703e490SVasu Dev 	struct fcoe_hdr *hp;
1670a703e490SVasu Dev 
1671a703e490SVasu Dev 	fr = fcoe_dev_from_skb(skb);
16721875f27eSRobert Love 	lport = fr->fr_dev;
16731875f27eSRobert Love 	if (unlikely(!lport)) {
1674e7a51997SJoe Eykholt 		if (skb->destructor != fcoe_percpu_flush_done)
1675e7a51997SJoe Eykholt 			FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1676a703e490SVasu Dev 		kfree_skb(skb);
1677859b7b64SChris Leech 		return;
1678a703e490SVasu Dev 	}
1679a703e490SVasu Dev 
1680d5488eb9SRobert Love 	FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1681d5488eb9SRobert Love 			"head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1682a703e490SVasu Dev 			skb->len, skb->data_len,
1683a703e490SVasu Dev 			skb->head, skb->data, skb_tail_pointer(skb),
1684a703e490SVasu Dev 			skb_end_pointer(skb), skb->csum,
1685a703e490SVasu Dev 			skb->dev ? skb->dev->name : "<NULL>");
1686a703e490SVasu Dev 
16871875f27eSRobert Love 	port = lport_priv(lport);
1688a703e490SVasu Dev 	if (skb_is_nonlinear(skb))
1689a703e490SVasu Dev 		skb_linearize(skb);	/* not ideal */
1690a703e490SVasu Dev 
1691a703e490SVasu Dev 	/*
1692a703e490SVasu Dev 	 * Frame length checks and setting up the header pointers
1693a703e490SVasu Dev 	 * was done in fcoe_rcv already.
1694a703e490SVasu Dev 	 */
1695a703e490SVasu Dev 	hp = (struct fcoe_hdr *) skb_network_header(skb);
1696a703e490SVasu Dev 
1697f018b73aSJoe Eykholt 	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1698a703e490SVasu Dev 	if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1699a703e490SVasu Dev 		if (stats->ErrorFrames < 5)
1700d5488eb9SRobert Love 			printk(KERN_WARNING "fcoe: FCoE version "
1701a703e490SVasu Dev 			       "mismatch: The frame has "
1702a703e490SVasu Dev 			       "version %x, but the "
1703a703e490SVasu Dev 			       "initiator supports version "
1704a703e490SVasu Dev 			       "%x\n", FC_FCOE_DECAPS_VER(hp),
1705a703e490SVasu Dev 			       FC_FCOE_VER);
1706f018b73aSJoe Eykholt 		goto drop;
1707a703e490SVasu Dev 	}
1708a703e490SVasu Dev 
1709a703e490SVasu Dev 	skb_pull(skb, sizeof(struct fcoe_hdr));
1710a703e490SVasu Dev 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1711a703e490SVasu Dev 
1712a703e490SVasu Dev 	stats->RxFrames++;
1713a703e490SVasu Dev 	stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1714a703e490SVasu Dev 
1715a703e490SVasu Dev 	fp = (struct fc_frame *)skb;
1716a703e490SVasu Dev 	fc_frame_init(fp);
17171875f27eSRobert Love 	fr_dev(fp) = lport;
1718a703e490SVasu Dev 	fr_sof(fp) = hp->fcoe_sof;
1719a703e490SVasu Dev 
1720a703e490SVasu Dev 	/* Copy out the CRC and EOF trailer for access */
1721f018b73aSJoe Eykholt 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1722f018b73aSJoe Eykholt 		goto drop;
1723a703e490SVasu Dev 	fr_eof(fp) = crc_eof.fcoe_eof;
1724a703e490SVasu Dev 	fr_crc(fp) = crc_eof.fcoe_crc32;
1725f018b73aSJoe Eykholt 	if (pskb_trim(skb, fr_len))
1726f018b73aSJoe Eykholt 		goto drop;
1727a703e490SVasu Dev 
172852ee8321SVasu Dev 	if (!fcoe_filter_frames(lport, fp)) {
1729f018b73aSJoe Eykholt 		put_cpu();
17301875f27eSRobert Love 		fc_exch_recv(lport, fp);
1731f018b73aSJoe Eykholt 		return;
173252ee8321SVasu Dev 	}
1733f018b73aSJoe Eykholt drop:
1734f018b73aSJoe Eykholt 	stats->ErrorFrames++;
1735f018b73aSJoe Eykholt 	put_cpu();
1736f018b73aSJoe Eykholt 	kfree_skb(skb);
1737a703e490SVasu Dev }
1738859b7b64SChris Leech 
1739859b7b64SChris Leech /**
1740859b7b64SChris Leech  * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1741859b7b64SChris Leech  * @arg: The per-CPU context
1742859b7b64SChris Leech  *
1743859b7b64SChris Leech  * Return: 0 for success
1744859b7b64SChris Leech  */
1745859b7b64SChris Leech int fcoe_percpu_receive_thread(void *arg)
1746859b7b64SChris Leech {
1747859b7b64SChris Leech 	struct fcoe_percpu_s *p = arg;
1748859b7b64SChris Leech 	struct sk_buff *skb;
1749859b7b64SChris Leech 
1750859b7b64SChris Leech 	set_user_nice(current, -20);
1751859b7b64SChris Leech 
1752859b7b64SChris Leech 	while (!kthread_should_stop()) {
1753859b7b64SChris Leech 
1754859b7b64SChris Leech 		spin_lock_bh(&p->fcoe_rx_list.lock);
1755859b7b64SChris Leech 		while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1756859b7b64SChris Leech 			set_current_state(TASK_INTERRUPTIBLE);
1757859b7b64SChris Leech 			spin_unlock_bh(&p->fcoe_rx_list.lock);
1758859b7b64SChris Leech 			schedule();
1759859b7b64SChris Leech 			set_current_state(TASK_RUNNING);
1760859b7b64SChris Leech 			if (kthread_should_stop())
1761859b7b64SChris Leech 				return 0;
1762859b7b64SChris Leech 			spin_lock_bh(&p->fcoe_rx_list.lock);
1763859b7b64SChris Leech 		}
1764859b7b64SChris Leech 		spin_unlock_bh(&p->fcoe_rx_list.lock);
1765859b7b64SChris Leech 		fcoe_recv_frame(skb);
1766859b7b64SChris Leech 	}
1767a703e490SVasu Dev 	return 0;
1768a703e490SVasu Dev }
1769a703e490SVasu Dev 
1770a703e490SVasu Dev /**
17711875f27eSRobert Love  * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
17721875f27eSRobert Love  * @lport: The local port whose backlog is to be cleared
1773a703e490SVasu Dev  *
17741875f27eSRobert Love  * This empties the wait_queue, dequeues the head of the wait_queue queue
17751875f27eSRobert Love  * and calls fcoe_start_io() for each packet. If all skb have been
17761875f27eSRobert Love  * transmitted it returns the qlen. If an error occurs it restores
17771875f27eSRobert Love  * wait_queue (to try again later) and returns -1.
1778a703e490SVasu Dev  *
17791875f27eSRobert Love  * The wait_queue is used when the skb transmit fails. The failed skb
17801875f27eSRobert Love  * will go in the wait_queue which will be emptied by the timer function or
1781a703e490SVasu Dev  * by the next skb transmit.
1782a703e490SVasu Dev  */
17831875f27eSRobert Love static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
1784a703e490SVasu Dev {
17851875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
17864bb6b515SVasu Dev 	int rc;
1787a703e490SVasu Dev 
1788014f5c3fSChris Leech 	spin_lock_bh(&port->fcoe_pending_queue.lock);
17894bb6b515SVasu Dev 
17904bb6b515SVasu Dev 	if (skb)
1791014f5c3fSChris Leech 		__skb_queue_tail(&port->fcoe_pending_queue, skb);
17924bb6b515SVasu Dev 
1793014f5c3fSChris Leech 	if (port->fcoe_pending_queue_active)
1794a703e490SVasu Dev 		goto out;
1795014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 1;
1796a703e490SVasu Dev 
1797014f5c3fSChris Leech 	while (port->fcoe_pending_queue.qlen) {
1798a703e490SVasu Dev 		/* keep qlen > 0 until fcoe_start_io succeeds */
1799014f5c3fSChris Leech 		port->fcoe_pending_queue.qlen++;
1800014f5c3fSChris Leech 		skb = __skb_dequeue(&port->fcoe_pending_queue);
1801a703e490SVasu Dev 
1802014f5c3fSChris Leech 		spin_unlock_bh(&port->fcoe_pending_queue.lock);
1803a703e490SVasu Dev 		rc = fcoe_start_io(skb);
1804014f5c3fSChris Leech 		spin_lock_bh(&port->fcoe_pending_queue.lock);
1805a703e490SVasu Dev 
1806a703e490SVasu Dev 		if (rc) {
1807014f5c3fSChris Leech 			__skb_queue_head(&port->fcoe_pending_queue, skb);
1808a703e490SVasu Dev 			/* undo temporary increment above */
1809014f5c3fSChris Leech 			port->fcoe_pending_queue.qlen--;
1810a703e490SVasu Dev 			break;
1811a703e490SVasu Dev 		}
1812a703e490SVasu Dev 		/* undo temporary increment above */
1813014f5c3fSChris Leech 		port->fcoe_pending_queue.qlen--;
1814a703e490SVasu Dev 	}
1815a703e490SVasu Dev 
1816014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
18171875f27eSRobert Love 		lport->qfull = 0;
1818014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1819014f5c3fSChris Leech 		mod_timer(&port->timer, jiffies + 2);
1820014f5c3fSChris Leech 	port->fcoe_pending_queue_active = 0;
1821a703e490SVasu Dev out:
1822014f5c3fSChris Leech 	if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
18231875f27eSRobert Love 		lport->qfull = 1;
1824014f5c3fSChris Leech 	spin_unlock_bh(&port->fcoe_pending_queue.lock);
18254bb6b515SVasu Dev 	return;
1826a703e490SVasu Dev }
1827a703e490SVasu Dev 
1828a703e490SVasu Dev /**
18291875f27eSRobert Love  * fcoe_dev_setup() - Setup the link change notification interface
1830a703e490SVasu Dev  */
1831b0d428adSRandy Dunlap static void fcoe_dev_setup(void)
1832a703e490SVasu Dev {
1833a703e490SVasu Dev 	register_netdevice_notifier(&fcoe_notifier);
1834a703e490SVasu Dev }
1835a703e490SVasu Dev 
1836a703e490SVasu Dev /**
18371875f27eSRobert Love  * fcoe_dev_cleanup() - Cleanup the link change notification interface
1838a703e490SVasu Dev  */
1839a703e490SVasu Dev static void fcoe_dev_cleanup(void)
1840a703e490SVasu Dev {
1841a703e490SVasu Dev 	unregister_netdevice_notifier(&fcoe_notifier);
1842a703e490SVasu Dev }
1843a703e490SVasu Dev 
1844a703e490SVasu Dev /**
18451875f27eSRobert Love  * fcoe_device_notification() - Handler for net device events
18461875f27eSRobert Love  * @notifier: The context of the notification
18471875f27eSRobert Love  * @event:    The type of event
18481875f27eSRobert Love  * @ptr:      The net device that the event was on
1849a703e490SVasu Dev  *
18501875f27eSRobert Love  * This function is called by the Ethernet driver in case of link change event.
1851a703e490SVasu Dev  *
1852a703e490SVasu Dev  * Returns: 0 for success
1853a703e490SVasu Dev  */
1854a703e490SVasu Dev static int fcoe_device_notification(struct notifier_block *notifier,
1855a703e490SVasu Dev 				    ulong event, void *ptr)
1856a703e490SVasu Dev {
18571875f27eSRobert Love 	struct fc_lport *lport = NULL;
18581d1b88dcSVasu Dev 	struct net_device *netdev = ptr;
1859014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
18602e70e241SChris Leech 	struct fcoe_port *port;
1861a703e490SVasu Dev 	struct fcoe_dev_stats *stats;
186297c8389dSJoe Eykholt 	u32 link_possible = 1;
1863a703e490SVasu Dev 	u32 mfs;
1864a703e490SVasu Dev 	int rc = NOTIFY_OK;
1865a703e490SVasu Dev 
1866014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
186725024989SChris Leech 		if (fcoe->netdev == netdev) {
18681875f27eSRobert Love 			lport = fcoe->ctlr.lp;
1869a703e490SVasu Dev 			break;
1870a703e490SVasu Dev 		}
1871a703e490SVasu Dev 	}
18721875f27eSRobert Love 	if (!lport) {
1873a703e490SVasu Dev 		rc = NOTIFY_DONE;
1874a703e490SVasu Dev 		goto out;
1875a703e490SVasu Dev 	}
1876a703e490SVasu Dev 
1877a703e490SVasu Dev 	switch (event) {
1878a703e490SVasu Dev 	case NETDEV_DOWN:
1879a703e490SVasu Dev 	case NETDEV_GOING_DOWN:
188097c8389dSJoe Eykholt 		link_possible = 0;
1881a703e490SVasu Dev 		break;
1882a703e490SVasu Dev 	case NETDEV_UP:
1883a703e490SVasu Dev 	case NETDEV_CHANGE:
1884a703e490SVasu Dev 		break;
1885a703e490SVasu Dev 	case NETDEV_CHANGEMTU:
18867221d7e5SYi Zou 		if (netdev->features & NETIF_F_FCOE_MTU)
18877221d7e5SYi Zou 			break;
18881d1b88dcSVasu Dev 		mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1889a703e490SVasu Dev 				     sizeof(struct fcoe_crc_eof));
1890a703e490SVasu Dev 		if (mfs >= FC_MIN_MAX_FRAME)
18911875f27eSRobert Love 			fc_set_mfs(lport, mfs);
1892a703e490SVasu Dev 		break;
1893a703e490SVasu Dev 	case NETDEV_REGISTER:
1894a703e490SVasu Dev 		break;
18952e70e241SChris Leech 	case NETDEV_UNREGISTER:
18962e70e241SChris Leech 		list_del(&fcoe->list);
18972e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
18982e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
18992e70e241SChris Leech 		schedule_work(&port->destroy_work);
19002e70e241SChris Leech 		goto out;
19012e70e241SChris Leech 		break;
190254a5b21dSYi Zou 	case NETDEV_FEAT_CHANGE:
190354a5b21dSYi Zou 		fcoe_netdev_features_change(lport, netdev);
190454a5b21dSYi Zou 		break;
1905a703e490SVasu Dev 	default:
19061d1b88dcSVasu Dev 		FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1907d5488eb9SRobert Love 				"from netdev netlink\n", event);
1908a703e490SVasu Dev 	}
19095e4f8fe7SRobert Love 
19105e4f8fe7SRobert Love 	fcoe_link_speed_update(lport);
19115e4f8fe7SRobert Love 
19121875f27eSRobert Love 	if (link_possible && !fcoe_link_ok(lport))
19133fe9a0baSChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
19143fe9a0baSChris Leech 	else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1915f018b73aSJoe Eykholt 		stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1916a703e490SVasu Dev 		stats->LinkFailureCount++;
1917f018b73aSJoe Eykholt 		put_cpu();
19181875f27eSRobert Love 		fcoe_clean_pending_queue(lport);
1919a703e490SVasu Dev 	}
1920a703e490SVasu Dev out:
1921a703e490SVasu Dev 	return rc;
1922a703e490SVasu Dev }
1923a703e490SVasu Dev 
1924a703e490SVasu Dev /**
192555a66d3cSVasu Dev  * fcoe_disable() - Disables a FCoE interface
192678a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
192755a66d3cSVasu Dev  *
192878a58246SYi Zou  * Called from fcoe transport.
192955a66d3cSVasu Dev  *
193055a66d3cSVasu Dev  * Returns: 0 for success
193155a66d3cSVasu Dev  */
193278a58246SYi Zou static int fcoe_disable(struct net_device *netdev)
193355a66d3cSVasu Dev {
193455a66d3cSVasu Dev 	struct fcoe_interface *fcoe;
193555a66d3cSVasu Dev 	int rc = 0;
193655a66d3cSVasu Dev 
193755a66d3cSVasu Dev 	mutex_lock(&fcoe_config_mutex);
193855a66d3cSVasu Dev #ifdef CONFIG_FCOE_MODULE
193955a66d3cSVasu Dev 	/*
194055a66d3cSVasu Dev 	 * Make sure the module has been initialized, and is not about to be
194155a66d3cSVasu Dev 	 * removed.  Module paramter sysfs files are writable before the
194255a66d3cSVasu Dev 	 * module_init function is called and after module_exit.
194355a66d3cSVasu Dev 	 */
194455a66d3cSVasu Dev 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
194555a66d3cSVasu Dev 		rc = -ENODEV;
194655a66d3cSVasu Dev 		goto out_nodev;
194755a66d3cSVasu Dev 	}
194855a66d3cSVasu Dev #endif
194955a66d3cSVasu Dev 
195034ce27bcSVasu Dev 	if (!rtnl_trylock()) {
195134ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
195278a58246SYi Zou 		return -ERESTARTSYS;
195334ce27bcSVasu Dev 	}
195434ce27bcSVasu Dev 
195555a66d3cSVasu Dev 	fcoe = fcoe_hostlist_lookup_port(netdev);
195655a66d3cSVasu Dev 	rtnl_unlock();
195755a66d3cSVasu Dev 
19589ee50e48SChris Leech 	if (fcoe) {
19599ee50e48SChris Leech 		fcoe_ctlr_link_down(&fcoe->ctlr);
19609d4cbc05SVasu Dev 		fcoe_clean_pending_queue(fcoe->ctlr.lp);
19619ee50e48SChris Leech 	} else
196255a66d3cSVasu Dev 		rc = -ENODEV;
196355a66d3cSVasu Dev 
196455a66d3cSVasu Dev out_nodev:
196555a66d3cSVasu Dev 	mutex_unlock(&fcoe_config_mutex);
196655a66d3cSVasu Dev 	return rc;
196755a66d3cSVasu Dev }
196855a66d3cSVasu Dev 
196955a66d3cSVasu Dev /**
197055a66d3cSVasu Dev  * fcoe_enable() - Enables a FCoE interface
197178a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
197255a66d3cSVasu Dev  *
197378a58246SYi Zou  * Called from fcoe transport.
197455a66d3cSVasu Dev  *
197555a66d3cSVasu Dev  * Returns: 0 for success
197655a66d3cSVasu Dev  */
197778a58246SYi Zou static int fcoe_enable(struct net_device *netdev)
197855a66d3cSVasu Dev {
197955a66d3cSVasu Dev 	struct fcoe_interface *fcoe;
198055a66d3cSVasu Dev 	int rc = 0;
198155a66d3cSVasu Dev 
198255a66d3cSVasu Dev 	mutex_lock(&fcoe_config_mutex);
198355a66d3cSVasu Dev #ifdef CONFIG_FCOE_MODULE
198455a66d3cSVasu Dev 	/*
198555a66d3cSVasu Dev 	 * Make sure the module has been initialized, and is not about to be
198655a66d3cSVasu Dev 	 * removed.  Module paramter sysfs files are writable before the
198755a66d3cSVasu Dev 	 * module_init function is called and after module_exit.
198855a66d3cSVasu Dev 	 */
198955a66d3cSVasu Dev 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
199055a66d3cSVasu Dev 		rc = -ENODEV;
199155a66d3cSVasu Dev 		goto out_nodev;
199255a66d3cSVasu Dev 	}
199355a66d3cSVasu Dev #endif
199434ce27bcSVasu Dev 	if (!rtnl_trylock()) {
199534ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
199678a58246SYi Zou 		return -ERESTARTSYS;
199734ce27bcSVasu Dev 	}
199834ce27bcSVasu Dev 
199955a66d3cSVasu Dev 	fcoe = fcoe_hostlist_lookup_port(netdev);
200055a66d3cSVasu Dev 	rtnl_unlock();
200155a66d3cSVasu Dev 
20029d4cbc05SVasu Dev 	if (!fcoe)
200355a66d3cSVasu Dev 		rc = -ENODEV;
20049d4cbc05SVasu Dev 	else if (!fcoe_link_ok(fcoe->ctlr.lp))
20059d4cbc05SVasu Dev 		fcoe_ctlr_link_up(&fcoe->ctlr);
200655a66d3cSVasu Dev 
200755a66d3cSVasu Dev out_nodev:
200855a66d3cSVasu Dev 	mutex_unlock(&fcoe_config_mutex);
200955a66d3cSVasu Dev 	return rc;
201055a66d3cSVasu Dev }
201155a66d3cSVasu Dev 
201255a66d3cSVasu Dev /**
20131875f27eSRobert Love  * fcoe_destroy() - Destroy a FCoE interface
201478a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
20151875f27eSRobert Love  *
201678a58246SYi Zou  * Called from fcoe transport
2017a703e490SVasu Dev  *
2018a703e490SVasu Dev  * Returns: 0 for success
2019a703e490SVasu Dev  */
202078a58246SYi Zou static int fcoe_destroy(struct net_device *netdev)
2021a703e490SVasu Dev {
2022030f4e00SChris Leech 	struct fcoe_interface *fcoe;
20238eca355fSMike Christie 	int rc = 0;
2024a703e490SVasu Dev 
2025dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2026dfc1d0feSChris Leech #ifdef CONFIG_FCOE_MODULE
2027dfc1d0feSChris Leech 	/*
2028dfc1d0feSChris Leech 	 * Make sure the module has been initialized, and is not about to be
2029dfc1d0feSChris Leech 	 * removed.  Module paramter sysfs files are writable before the
2030dfc1d0feSChris Leech 	 * module_init function is called and after module_exit.
2031dfc1d0feSChris Leech 	 */
2032dfc1d0feSChris Leech 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2033dfc1d0feSChris Leech 		rc = -ENODEV;
2034dfc1d0feSChris Leech 		goto out_nodev;
2035dfc1d0feSChris Leech 	}
2036dfc1d0feSChris Leech #endif
203734ce27bcSVasu Dev 	if (!rtnl_trylock()) {
203834ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
203978a58246SYi Zou 		return -ERESTARTSYS;
204034ce27bcSVasu Dev 	}
204134ce27bcSVasu Dev 
20422e70e241SChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
20432e70e241SChris Leech 	if (!fcoe) {
2044090eb6c4SChris Leech 		rtnl_unlock();
2045a703e490SVasu Dev 		rc = -ENODEV;
204678a58246SYi Zou 		goto out_nodev;
2047a703e490SVasu Dev 	}
20482e70e241SChris Leech 	fcoe_interface_cleanup(fcoe);
204954a5b21dSYi Zou 	list_del(&fcoe->list);
205034ce27bcSVasu Dev 	/* RTNL mutex is dropped by fcoe_if_destroy */
20512e70e241SChris Leech 	fcoe_if_destroy(fcoe->ctlr.lp);
2052a703e490SVasu Dev out_nodev:
2053dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2054a703e490SVasu Dev 	return rc;
2055a703e490SVasu Dev }
2056a703e490SVasu Dev 
20571875f27eSRobert Love /**
20581875f27eSRobert Love  * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
20591875f27eSRobert Love  * @work: Handle to the FCoE port to be destroyed
20601875f27eSRobert Love  */
20612e70e241SChris Leech static void fcoe_destroy_work(struct work_struct *work)
20622e70e241SChris Leech {
20632e70e241SChris Leech 	struct fcoe_port *port;
20642e70e241SChris Leech 
20652e70e241SChris Leech 	port = container_of(work, struct fcoe_port, destroy_work);
20662e70e241SChris Leech 	mutex_lock(&fcoe_config_mutex);
206734ce27bcSVasu Dev 	rtnl_lock();
206834ce27bcSVasu Dev 	/* RTNL mutex is dropped by fcoe_if_destroy */
20692e70e241SChris Leech 	fcoe_if_destroy(port->lport);
20702e70e241SChris Leech 	mutex_unlock(&fcoe_config_mutex);
20712e70e241SChris Leech }
20722e70e241SChris Leech 
2073a703e490SVasu Dev /**
207478a58246SYi Zou  * fcoe_match() - Check if the FCoE is supported on the given netdevice
207578a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
20761875f27eSRobert Love  *
207778a58246SYi Zou  * Called from fcoe transport.
207878a58246SYi Zou  *
207978a58246SYi Zou  * Returns: always returns true as this is the default FCoE transport,
208078a58246SYi Zou  * i.e., support all netdevs.
208178a58246SYi Zou  */
208278a58246SYi Zou static bool fcoe_match(struct net_device *netdev)
208378a58246SYi Zou {
208478a58246SYi Zou 	return true;
208578a58246SYi Zou }
208678a58246SYi Zou 
208778a58246SYi Zou /**
208878a58246SYi Zou  * fcoe_create() - Create a fcoe interface
208978a58246SYi Zou  * @netdev  : The net_device object the Ethernet interface to create on
209078a58246SYi Zou  * @fip_mode: The FIP mode for this creation
209178a58246SYi Zou  *
209278a58246SYi Zou  * Called from fcoe transport
2093a703e490SVasu Dev  *
2094a703e490SVasu Dev  * Returns: 0 for success
2095a703e490SVasu Dev  */
209678a58246SYi Zou static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2097a703e490SVasu Dev {
2098a703e490SVasu Dev 	int rc;
2099030f4e00SChris Leech 	struct fcoe_interface *fcoe;
2100af7f85d9SChris Leech 	struct fc_lport *lport;
2101a703e490SVasu Dev 
2102dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
210334ce27bcSVasu Dev 
210434ce27bcSVasu Dev 	if (!rtnl_trylock()) {
210534ce27bcSVasu Dev 		mutex_unlock(&fcoe_config_mutex);
210678a58246SYi Zou 		return -ERESTARTSYS;
210734ce27bcSVasu Dev 	}
210834ce27bcSVasu Dev 
2109dfc1d0feSChris Leech #ifdef CONFIG_FCOE_MODULE
2110dfc1d0feSChris Leech 	/*
2111dfc1d0feSChris Leech 	 * Make sure the module has been initialized, and is not about to be
2112dfc1d0feSChris Leech 	 * removed.  Module paramter sysfs files are writable before the
2113dfc1d0feSChris Leech 	 * module_init function is called and after module_exit.
2114dfc1d0feSChris Leech 	 */
2115dfc1d0feSChris Leech 	if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2116dfc1d0feSChris Leech 		rc = -ENODEV;
21177287fb91SRobert Love 		goto out_nodev;
2118dfc1d0feSChris Leech 	}
2119dfc1d0feSChris Leech #endif
2120dfc1d0feSChris Leech 
2121a703e490SVasu Dev 	/* look for existing lport */
2122a703e490SVasu Dev 	if (fcoe_hostlist_lookup(netdev)) {
2123a703e490SVasu Dev 		rc = -EEXIST;
212478a58246SYi Zou 		goto out_nodev;
2125a703e490SVasu Dev 	}
2126a703e490SVasu Dev 
21271dd454d9SJoe Eykholt 	fcoe = fcoe_interface_create(netdev, fip_mode);
21287287fb91SRobert Love 	if (IS_ERR(fcoe)) {
21297287fb91SRobert Love 		rc = PTR_ERR(fcoe);
213078a58246SYi Zou 		goto out_nodev;
2131030f4e00SChris Leech 	}
2132030f4e00SChris Leech 
21339a05753bSChris Leech 	lport = fcoe_if_create(fcoe, &netdev->dev, 0);
2134af7f85d9SChris Leech 	if (IS_ERR(lport)) {
2135d5488eb9SRobert Love 		printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2136a703e490SVasu Dev 		       netdev->name);
2137a703e490SVasu Dev 		rc = -EIO;
21382e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
2139030f4e00SChris Leech 		goto out_free;
2140a703e490SVasu Dev 	}
2141030f4e00SChris Leech 
214254b649f8SChris Leech 	/* Make this the "master" N_Port */
214354b649f8SChris Leech 	fcoe->ctlr.lp = lport;
2144030f4e00SChris Leech 
2145c863df33SChris Leech 	/* add to lports list */
2146c863df33SChris Leech 	fcoe_hostlist_add(lport);
2147c863df33SChris Leech 
214854b649f8SChris Leech 	/* start FIP Discovery and FLOGI */
214954b649f8SChris Leech 	lport->boot_time = jiffies;
215054b649f8SChris Leech 	fc_fabric_login(lport);
215154b649f8SChris Leech 	if (!fcoe_link_ok(lport))
215254b649f8SChris Leech 		fcoe_ctlr_link_up(&fcoe->ctlr);
215354b649f8SChris Leech 
215454b649f8SChris Leech 	/*
215554b649f8SChris Leech 	 * Release from init in fcoe_interface_create(), on success lport
215654b649f8SChris Leech 	 * should be holding a reference taken in fcoe_if_create().
215754b649f8SChris Leech 	 */
2158030f4e00SChris Leech 	fcoe_interface_put(fcoe);
21596409ea65SRob Love 	rtnl_unlock();
21606409ea65SRob Love 	mutex_unlock(&fcoe_config_mutex);
21616409ea65SRob Love 
21626409ea65SRob Love 	return 0;
21636409ea65SRob Love out_free:
21646409ea65SRob Love 	fcoe_interface_put(fcoe);
2165a703e490SVasu Dev out_nodev:
216634ce27bcSVasu Dev 	rtnl_unlock();
2167dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2168a703e490SVasu Dev 	return rc;
2169a703e490SVasu Dev }
2170a703e490SVasu Dev 
2171a703e490SVasu Dev /**
21725e4f8fe7SRobert Love  * fcoe_link_speed_update() - Update the supported and actual link speeds
21735e4f8fe7SRobert Love  * @lport: The local port to update speeds for
2174a703e490SVasu Dev  *
21755e4f8fe7SRobert Love  * Returns: 0 if the ethtool query was successful
21765e4f8fe7SRobert Love  *          -1 if the ethtool query failed
2177a703e490SVasu Dev  */
21785e4f8fe7SRobert Love int fcoe_link_speed_update(struct fc_lport *lport)
2179a703e490SVasu Dev {
21801875f27eSRobert Love 	struct fcoe_port *port = lport_priv(lport);
21811875f27eSRobert Love 	struct net_device *netdev = port->fcoe->netdev;
2182a703e490SVasu Dev 	struct ethtool_cmd ecmd = { ETHTOOL_GSET };
2183a703e490SVasu Dev 
21845e4f8fe7SRobert Love 	if (!dev_ethtool_get_settings(netdev, &ecmd)) {
21851875f27eSRobert Love 		lport->link_supported_speeds &=
2186a703e490SVasu Dev 			~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2187a703e490SVasu Dev 		if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2188a703e490SVasu Dev 				      SUPPORTED_1000baseT_Full))
21891875f27eSRobert Love 			lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2190a703e490SVasu Dev 		if (ecmd.supported & SUPPORTED_10000baseT_Full)
21911875f27eSRobert Love 			lport->link_supported_speeds |=
2192a703e490SVasu Dev 				FC_PORTSPEED_10GBIT;
2193a703e490SVasu Dev 		if (ecmd.speed == SPEED_1000)
21941875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_1GBIT;
2195a703e490SVasu Dev 		if (ecmd.speed == SPEED_10000)
21961875f27eSRobert Love 			lport->link_speed = FC_PORTSPEED_10GBIT;
2197a703e490SVasu Dev 
21982f718d64SYi Zou 		return 0;
21992f718d64SYi Zou 	}
22002f718d64SYi Zou 	return -1;
2201a703e490SVasu Dev }
2202a703e490SVasu Dev 
2203a703e490SVasu Dev /**
22045e4f8fe7SRobert Love  * fcoe_link_ok() - Check if the link is OK for a local port
22055e4f8fe7SRobert Love  * @lport: The local port to check link on
22065e4f8fe7SRobert Love  *
22075e4f8fe7SRobert Love  * Returns: 0 if link is UP and OK, -1 if not
22085e4f8fe7SRobert Love  *
22095e4f8fe7SRobert Love  */
22105e4f8fe7SRobert Love int fcoe_link_ok(struct fc_lport *lport)
22115e4f8fe7SRobert Love {
22125e4f8fe7SRobert Love 	struct fcoe_port *port = lport_priv(lport);
22135e4f8fe7SRobert Love 	struct net_device *netdev = port->fcoe->netdev;
22145e4f8fe7SRobert Love 
22155e4f8fe7SRobert Love 	if (netif_oper_up(netdev))
22165e4f8fe7SRobert Love 		return 0;
22175e4f8fe7SRobert Love 	return -1;
22185e4f8fe7SRobert Love }
22195e4f8fe7SRobert Love 
22205e4f8fe7SRobert Love /**
22211875f27eSRobert Love  * fcoe_percpu_clean() - Clear all pending skbs for an local port
22221875f27eSRobert Love  * @lport: The local port whose skbs are to be cleared
2223e7a51997SJoe Eykholt  *
2224e7a51997SJoe Eykholt  * Must be called with fcoe_create_mutex held to single-thread completion.
2225e7a51997SJoe Eykholt  *
2226e7a51997SJoe Eykholt  * This flushes the pending skbs by adding a new skb to each queue and
2227e7a51997SJoe Eykholt  * waiting until they are all freed.  This assures us that not only are
2228e7a51997SJoe Eykholt  * there no packets that will be handled by the lport, but also that any
2229e7a51997SJoe Eykholt  * threads already handling packet have returned.
2230a703e490SVasu Dev  */
22311875f27eSRobert Love void fcoe_percpu_clean(struct fc_lport *lport)
2232a703e490SVasu Dev {
2233a703e490SVasu Dev 	struct fcoe_percpu_s *pp;
2234a703e490SVasu Dev 	struct fcoe_rcv_info *fr;
2235a703e490SVasu Dev 	struct sk_buff_head *list;
2236a703e490SVasu Dev 	struct sk_buff *skb, *next;
2237a703e490SVasu Dev 	struct sk_buff *head;
2238a703e490SVasu Dev 	unsigned int cpu;
2239a703e490SVasu Dev 
2240a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2241a703e490SVasu Dev 		pp = &per_cpu(fcoe_percpu, cpu);
2242a703e490SVasu Dev 		spin_lock_bh(&pp->fcoe_rx_list.lock);
2243a703e490SVasu Dev 		list = &pp->fcoe_rx_list;
2244a703e490SVasu Dev 		head = list->next;
2245a703e490SVasu Dev 		for (skb = head; skb != (struct sk_buff *)list;
2246a703e490SVasu Dev 		     skb = next) {
2247a703e490SVasu Dev 			next = skb->next;
2248a703e490SVasu Dev 			fr = fcoe_dev_from_skb(skb);
22491875f27eSRobert Love 			if (fr->fr_dev == lport) {
2250a703e490SVasu Dev 				__skb_unlink(skb, list);
2251a703e490SVasu Dev 				kfree_skb(skb);
2252a703e490SVasu Dev 			}
2253a703e490SVasu Dev 		}
2254e7a51997SJoe Eykholt 
2255e7a51997SJoe Eykholt 		if (!pp->thread || !cpu_online(cpu)) {
2256a703e490SVasu Dev 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2257e7a51997SJoe Eykholt 			continue;
2258e7a51997SJoe Eykholt 		}
2259e7a51997SJoe Eykholt 
2260e7a51997SJoe Eykholt 		skb = dev_alloc_skb(0);
2261e7a51997SJoe Eykholt 		if (!skb) {
2262e7a51997SJoe Eykholt 			spin_unlock_bh(&pp->fcoe_rx_list.lock);
2263e7a51997SJoe Eykholt 			continue;
2264e7a51997SJoe Eykholt 		}
2265e7a51997SJoe Eykholt 		skb->destructor = fcoe_percpu_flush_done;
2266e7a51997SJoe Eykholt 
2267e7a51997SJoe Eykholt 		__skb_queue_tail(&pp->fcoe_rx_list, skb);
2268e7a51997SJoe Eykholt 		if (pp->fcoe_rx_list.qlen == 1)
2269e7a51997SJoe Eykholt 			wake_up_process(pp->thread);
2270e7a51997SJoe Eykholt 		spin_unlock_bh(&pp->fcoe_rx_list.lock);
2271e7a51997SJoe Eykholt 
2272e7a51997SJoe Eykholt 		wait_for_completion(&fcoe_flush_completion);
2273a703e490SVasu Dev 	}
2274a703e490SVasu Dev }
2275a703e490SVasu Dev 
2276a703e490SVasu Dev /**
2277a703e490SVasu Dev  * fcoe_clean_pending_queue() - Dequeue a skb and free it
22781875f27eSRobert Love  * @lport: The local port to dequeue a skb on
2279a703e490SVasu Dev  */
22801875f27eSRobert Love void fcoe_clean_pending_queue(struct fc_lport *lport)
2281a703e490SVasu Dev {
22821875f27eSRobert Love 	struct fcoe_port  *port = lport_priv(lport);
2283a703e490SVasu Dev 	struct sk_buff *skb;
2284a703e490SVasu Dev 
2285014f5c3fSChris Leech 	spin_lock_bh(&port->fcoe_pending_queue.lock);
2286014f5c3fSChris Leech 	while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2287014f5c3fSChris Leech 		spin_unlock_bh(&port->fcoe_pending_queue.lock);
2288a703e490SVasu Dev 		kfree_skb(skb);
2289014f5c3fSChris Leech 		spin_lock_bh(&port->fcoe_pending_queue.lock);
2290a703e490SVasu Dev 	}
2291014f5c3fSChris Leech 	spin_unlock_bh(&port->fcoe_pending_queue.lock);
2292a703e490SVasu Dev }
2293a703e490SVasu Dev 
2294a703e490SVasu Dev /**
22951875f27eSRobert Love  * fcoe_reset() - Reset a local port
22961875f27eSRobert Love  * @shost: The SCSI host associated with the local port to be reset
2297a703e490SVasu Dev  *
22981875f27eSRobert Love  * Returns: Always 0 (return value required by FC transport template)
2299a703e490SVasu Dev  */
2300a703e490SVasu Dev int fcoe_reset(struct Scsi_Host *shost)
2301a703e490SVasu Dev {
2302a703e490SVasu Dev 	struct fc_lport *lport = shost_priv(shost);
2303a703e490SVasu Dev 	fc_lport_reset(lport);
2304a703e490SVasu Dev 	return 0;
2305a703e490SVasu Dev }
2306a703e490SVasu Dev 
2307a703e490SVasu Dev /**
23081875f27eSRobert Love  * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
23091875f27eSRobert Love  * @netdev: The net device used as a key
2310a703e490SVasu Dev  *
23111875f27eSRobert Love  * Locking: Must be called with the RNL mutex held.
23121875f27eSRobert Love  *
23131875f27eSRobert Love  * Returns: NULL or the FCoE interface
2314a703e490SVasu Dev  */
2315014f5c3fSChris Leech static struct fcoe_interface *
23161875f27eSRobert Love fcoe_hostlist_lookup_port(const struct net_device *netdev)
2317a703e490SVasu Dev {
2318014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2319a703e490SVasu Dev 
2320014f5c3fSChris Leech 	list_for_each_entry(fcoe, &fcoe_hostlist, list) {
23211875f27eSRobert Love 		if (fcoe->netdev == netdev)
2322014f5c3fSChris Leech 			return fcoe;
2323a703e490SVasu Dev 	}
2324a703e490SVasu Dev 	return NULL;
2325a703e490SVasu Dev }
2326a703e490SVasu Dev 
2327a703e490SVasu Dev /**
23281875f27eSRobert Love  * fcoe_hostlist_lookup() - Find the local port associated with a
23291875f27eSRobert Love  *			    given net device
23301875f27eSRobert Love  * @netdev: The netdevice used as a key
2331a703e490SVasu Dev  *
23321875f27eSRobert Love  * Locking: Must be called with the RTNL mutex held
23331875f27eSRobert Love  *
23341875f27eSRobert Love  * Returns: NULL or the local port
2335a703e490SVasu Dev  */
2336090eb6c4SChris Leech static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2337a703e490SVasu Dev {
2338014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2339a703e490SVasu Dev 
2340014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(netdev);
23413fe9a0baSChris Leech 	return (fcoe) ? fcoe->ctlr.lp : NULL;
2342a703e490SVasu Dev }
2343a703e490SVasu Dev 
2344a703e490SVasu Dev /**
23451875f27eSRobert Love  * fcoe_hostlist_add() - Add the FCoE interface identified by a local
23461875f27eSRobert Love  *			 port to the hostlist
23471875f27eSRobert Love  * @lport: The local port that identifies the FCoE interface to be added
23481875f27eSRobert Love  *
23491875f27eSRobert Love  * Locking: must be called with the RTNL mutex held
2350a703e490SVasu Dev  *
2351a703e490SVasu Dev  * Returns: 0 for success
2352a703e490SVasu Dev  */
2353090eb6c4SChris Leech static int fcoe_hostlist_add(const struct fc_lport *lport)
2354a703e490SVasu Dev {
2355014f5c3fSChris Leech 	struct fcoe_interface *fcoe;
2356014f5c3fSChris Leech 	struct fcoe_port *port;
2357a703e490SVasu Dev 
2358014f5c3fSChris Leech 	fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2359014f5c3fSChris Leech 	if (!fcoe) {
2360014f5c3fSChris Leech 		port = lport_priv(lport);
2361014f5c3fSChris Leech 		fcoe = port->fcoe;
2362014f5c3fSChris Leech 		list_add_tail(&fcoe->list, &fcoe_hostlist);
2363a703e490SVasu Dev 	}
2364a703e490SVasu Dev 	return 0;
2365a703e490SVasu Dev }
2366a703e490SVasu Dev 
236778a58246SYi Zou 
236878a58246SYi Zou static struct fcoe_transport fcoe_sw_transport = {
236978a58246SYi Zou 	.name = {FCOE_TRANSPORT_DEFAULT},
237078a58246SYi Zou 	.attached = false,
237178a58246SYi Zou 	.list = LIST_HEAD_INIT(fcoe_sw_transport.list),
237278a58246SYi Zou 	.match = fcoe_match,
237378a58246SYi Zou 	.create = fcoe_create,
237478a58246SYi Zou 	.destroy = fcoe_destroy,
237578a58246SYi Zou 	.enable = fcoe_enable,
237678a58246SYi Zou 	.disable = fcoe_disable,
237778a58246SYi Zou };
237878a58246SYi Zou 
2379a703e490SVasu Dev /**
23801875f27eSRobert Love  * fcoe_init() - Initialize fcoe.ko
2381a703e490SVasu Dev  *
23821875f27eSRobert Love  * Returns: 0 on success, or a negative value on failure
2383a703e490SVasu Dev  */
2384a703e490SVasu Dev static int __init fcoe_init(void)
2385a703e490SVasu Dev {
23861875f27eSRobert Love 	struct fcoe_percpu_s *p;
2387a703e490SVasu Dev 	unsigned int cpu;
2388a703e490SVasu Dev 	int rc = 0;
2389a703e490SVasu Dev 
239078a58246SYi Zou 	/* register as a fcoe transport */
239178a58246SYi Zou 	rc = fcoe_transport_attach(&fcoe_sw_transport);
239278a58246SYi Zou 	if (rc) {
239378a58246SYi Zou 		printk(KERN_ERR "failed to register an fcoe transport, check "
239478a58246SYi Zou 			"if libfcoe is loaded\n");
239578a58246SYi Zou 		return rc;
239678a58246SYi Zou 	}
239778a58246SYi Zou 
2398dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2399dfc1d0feSChris Leech 
2400a703e490SVasu Dev 	for_each_possible_cpu(cpu) {
2401a703e490SVasu Dev 		p = &per_cpu(fcoe_percpu, cpu);
2402a703e490SVasu Dev 		skb_queue_head_init(&p->fcoe_rx_list);
2403a703e490SVasu Dev 	}
2404a703e490SVasu Dev 
2405a703e490SVasu Dev 	for_each_online_cpu(cpu)
2406a703e490SVasu Dev 		fcoe_percpu_thread_create(cpu);
2407a703e490SVasu Dev 
2408a703e490SVasu Dev 	/* Initialize per CPU interrupt thread */
2409a703e490SVasu Dev 	rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2410a703e490SVasu Dev 	if (rc)
2411a703e490SVasu Dev 		goto out_free;
2412a703e490SVasu Dev 
2413a703e490SVasu Dev 	/* Setup link change notification */
2414a703e490SVasu Dev 	fcoe_dev_setup();
2415a703e490SVasu Dev 
24165892c32fSChris Leech 	rc = fcoe_if_init();
24175892c32fSChris Leech 	if (rc)
24185892c32fSChris Leech 		goto out_free;
2419a703e490SVasu Dev 
2420dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2421a703e490SVasu Dev 	return 0;
2422a703e490SVasu Dev 
2423a703e490SVasu Dev out_free:
2424a703e490SVasu Dev 	for_each_online_cpu(cpu) {
2425a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2426a703e490SVasu Dev 	}
2427dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
2428a703e490SVasu Dev 	return rc;
2429a703e490SVasu Dev }
2430a703e490SVasu Dev module_init(fcoe_init);
2431a703e490SVasu Dev 
2432a703e490SVasu Dev /**
24331875f27eSRobert Love  * fcoe_exit() - Clean up fcoe.ko
2434a703e490SVasu Dev  *
24351875f27eSRobert Love  * Returns: 0 on success or a  negative value on failure
2436a703e490SVasu Dev  */
2437a703e490SVasu Dev static void __exit fcoe_exit(void)
2438a703e490SVasu Dev {
2439014f5c3fSChris Leech 	struct fcoe_interface *fcoe, *tmp;
24402e70e241SChris Leech 	struct fcoe_port *port;
24411875f27eSRobert Love 	unsigned int cpu;
2442a703e490SVasu Dev 
2443dfc1d0feSChris Leech 	mutex_lock(&fcoe_config_mutex);
2444dfc1d0feSChris Leech 
2445a703e490SVasu Dev 	fcoe_dev_cleanup();
2446a703e490SVasu Dev 
2447a703e490SVasu Dev 	/* releases the associated fcoe hosts */
2448090eb6c4SChris Leech 	rtnl_lock();
2449090eb6c4SChris Leech 	list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2450c863df33SChris Leech 		list_del(&fcoe->list);
24512e70e241SChris Leech 		port = lport_priv(fcoe->ctlr.lp);
24522e70e241SChris Leech 		fcoe_interface_cleanup(fcoe);
24532e70e241SChris Leech 		schedule_work(&port->destroy_work);
2454c863df33SChris Leech 	}
2455090eb6c4SChris Leech 	rtnl_unlock();
2456a703e490SVasu Dev 
2457a703e490SVasu Dev 	unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2458a703e490SVasu Dev 
2459014f5c3fSChris Leech 	for_each_online_cpu(cpu)
2460a703e490SVasu Dev 		fcoe_percpu_thread_destroy(cpu);
2461a703e490SVasu Dev 
2462dfc1d0feSChris Leech 	mutex_unlock(&fcoe_config_mutex);
24632e70e241SChris Leech 
24642e70e241SChris Leech 	/* flush any asyncronous interface destroys,
24652e70e241SChris Leech 	 * this should happen after the netdev notifier is unregistered */
24662e70e241SChris Leech 	flush_scheduled_work();
24679a05753bSChris Leech 	/* That will flush out all the N_Ports on the hostlist, but now we
24689a05753bSChris Leech 	 * may have NPIV VN_Ports scheduled for destruction */
24699a05753bSChris Leech 	flush_scheduled_work();
24702e70e241SChris Leech 
24712e70e241SChris Leech 	/* detach from scsi transport
24722e70e241SChris Leech 	 * must happen after all destroys are done, therefor after the flush */
24732e70e241SChris Leech 	fcoe_if_exit();
247478a58246SYi Zou 
247578a58246SYi Zou 	/* detach from fcoe transport */
247678a58246SYi Zou 	fcoe_transport_detach(&fcoe_sw_transport);
2477a703e490SVasu Dev }
2478a703e490SVasu Dev module_exit(fcoe_exit);
247911b56188SChris Leech 
248011b56188SChris Leech /**
248111b56188SChris Leech  * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
248211b56188SChris Leech  * @seq: active sequence in the FLOGI or FDISC exchange
248311b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
248411b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
248511b56188SChris Leech  *
248665155b37SUwe Kleine-König  * This handles MAC address management for FCoE, then passes control on to
248711b56188SChris Leech  * the libfc FLOGI response handler.
248811b56188SChris Leech  */
248911b56188SChris Leech static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
249011b56188SChris Leech {
249111b56188SChris Leech 	struct fcoe_ctlr *fip = arg;
249211b56188SChris Leech 	struct fc_exch *exch = fc_seq_exch(seq);
249311b56188SChris Leech 	struct fc_lport *lport = exch->lp;
249411b56188SChris Leech 	u8 *mac;
249511b56188SChris Leech 
249611b56188SChris Leech 	if (IS_ERR(fp))
249711b56188SChris Leech 		goto done;
249811b56188SChris Leech 
249911b56188SChris Leech 	mac = fr_cb(fp)->granted_mac;
250011b56188SChris Leech 	if (is_zero_ether_addr(mac)) {
250111b56188SChris Leech 		/* pre-FIP */
2502386309ceSJoe Eykholt 		if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
250311b56188SChris Leech 			fc_frame_free(fp);
250411b56188SChris Leech 			return;
250511b56188SChris Leech 		}
250611b56188SChris Leech 	}
2507386309ceSJoe Eykholt 	fcoe_update_src_mac(lport, mac);
250811b56188SChris Leech done:
250911b56188SChris Leech 	fc_lport_flogi_resp(seq, fp, lport);
251011b56188SChris Leech }
251111b56188SChris Leech 
251211b56188SChris Leech /**
251311b56188SChris Leech  * fcoe_logo_resp() - FCoE specific LOGO response handler
251411b56188SChris Leech  * @seq: active sequence in the LOGO exchange
251511b56188SChris Leech  * @fp: response frame, or error encoded in a pointer (timeout)
251611b56188SChris Leech  * @arg: pointer the the fcoe_ctlr structure
251711b56188SChris Leech  *
251865155b37SUwe Kleine-König  * This handles MAC address management for FCoE, then passes control on to
251911b56188SChris Leech  * the libfc LOGO response handler.
252011b56188SChris Leech  */
252111b56188SChris Leech static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
252211b56188SChris Leech {
2523386309ceSJoe Eykholt 	struct fc_lport *lport = arg;
252411b56188SChris Leech 	static u8 zero_mac[ETH_ALEN] = { 0 };
252511b56188SChris Leech 
252611b56188SChris Leech 	if (!IS_ERR(fp))
2527386309ceSJoe Eykholt 		fcoe_update_src_mac(lport, zero_mac);
252811b56188SChris Leech 	fc_lport_logo_resp(seq, fp, lport);
252911b56188SChris Leech }
253011b56188SChris Leech 
253111b56188SChris Leech /**
253211b56188SChris Leech  * fcoe_elsct_send - FCoE specific ELS handler
253311b56188SChris Leech  *
253411b56188SChris Leech  * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
253511b56188SChris Leech  * using FCoE specific response handlers and passing the FIP controller as
253611b56188SChris Leech  * the argument (the lport is still available from the exchange).
253711b56188SChris Leech  *
253811b56188SChris Leech  * Most of the work here is just handed off to the libfc routine.
253911b56188SChris Leech  */
25401875f27eSRobert Love static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
25411875f27eSRobert Love 				      struct fc_frame *fp, unsigned int op,
25421875f27eSRobert Love 				      void (*resp)(struct fc_seq *,
25431875f27eSRobert Love 						   struct fc_frame *,
25441875f27eSRobert Love 						   void *),
254511b56188SChris Leech 				      void *arg, u32 timeout)
254611b56188SChris Leech {
254711b56188SChris Leech 	struct fcoe_port *port = lport_priv(lport);
254811b56188SChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
254911b56188SChris Leech 	struct fcoe_ctlr *fip = &fcoe->ctlr;
255011b56188SChris Leech 	struct fc_frame_header *fh = fc_frame_header_get(fp);
255111b56188SChris Leech 
255211b56188SChris Leech 	switch (op) {
255311b56188SChris Leech 	case ELS_FLOGI:
255411b56188SChris Leech 	case ELS_FDISC:
2555e10f8c66SJoe Eykholt 		if (lport->point_to_multipoint)
2556e10f8c66SJoe Eykholt 			break;
255711b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
255811b56188SChris Leech 				     fip, timeout);
255911b56188SChris Leech 	case ELS_LOGO:
256011b56188SChris Leech 		/* only hook onto fabric logouts, not port logouts */
256111b56188SChris Leech 		if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
256211b56188SChris Leech 			break;
256311b56188SChris Leech 		return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2564386309ceSJoe Eykholt 				     lport, timeout);
256511b56188SChris Leech 	}
256611b56188SChris Leech 	return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
256711b56188SChris Leech }
256811b56188SChris Leech 
25699a05753bSChris Leech /**
25709a05753bSChris Leech  * fcoe_vport_create() - create an fc_host/scsi_host for a vport
25719a05753bSChris Leech  * @vport: fc_vport object to create a new fc_host for
25729a05753bSChris Leech  * @disabled: start the new fc_host in a disabled state by default?
25739a05753bSChris Leech  *
25749a05753bSChris Leech  * Returns: 0 for success
25759a05753bSChris Leech  */
25769a05753bSChris Leech static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
25779a05753bSChris Leech {
25789a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
25799a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
25809a05753bSChris Leech 	struct fcoe_port *port = lport_priv(n_port);
25819a05753bSChris Leech 	struct fcoe_interface *fcoe = port->fcoe;
25829a05753bSChris Leech 	struct net_device *netdev = fcoe->netdev;
25839a05753bSChris Leech 	struct fc_lport *vn_port;
25849a05753bSChris Leech 
25859a05753bSChris Leech 	mutex_lock(&fcoe_config_mutex);
25869a05753bSChris Leech 	vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
25879a05753bSChris Leech 	mutex_unlock(&fcoe_config_mutex);
25889a05753bSChris Leech 
25899a05753bSChris Leech 	if (IS_ERR(vn_port)) {
25909a05753bSChris Leech 		printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
25919a05753bSChris Leech 		       netdev->name);
25929a05753bSChris Leech 		return -EIO;
25939a05753bSChris Leech 	}
25949a05753bSChris Leech 
25959a05753bSChris Leech 	if (disabled) {
25969a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
25979a05753bSChris Leech 	} else {
25989a05753bSChris Leech 		vn_port->boot_time = jiffies;
25999a05753bSChris Leech 		fc_fabric_login(vn_port);
26009a05753bSChris Leech 		fc_vport_setlink(vn_port);
26019a05753bSChris Leech 	}
26029a05753bSChris Leech 	return 0;
26039a05753bSChris Leech }
26049a05753bSChris Leech 
26059a05753bSChris Leech /**
26069a05753bSChris Leech  * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
26079a05753bSChris Leech  * @vport: fc_vport object that is being destroyed
26089a05753bSChris Leech  *
26099a05753bSChris Leech  * Returns: 0 for success
26109a05753bSChris Leech  */
26119a05753bSChris Leech static int fcoe_vport_destroy(struct fc_vport *vport)
26129a05753bSChris Leech {
26139a05753bSChris Leech 	struct Scsi_Host *shost = vport_to_shost(vport);
26149a05753bSChris Leech 	struct fc_lport *n_port = shost_priv(shost);
26159a05753bSChris Leech 	struct fc_lport *vn_port = vport->dd_data;
26169a05753bSChris Leech 	struct fcoe_port *port = lport_priv(vn_port);
26179a05753bSChris Leech 
26189a05753bSChris Leech 	mutex_lock(&n_port->lp_mutex);
26199a05753bSChris Leech 	list_del(&vn_port->list);
26209a05753bSChris Leech 	mutex_unlock(&n_port->lp_mutex);
26219a05753bSChris Leech 	schedule_work(&port->destroy_work);
26229a05753bSChris Leech 	return 0;
26239a05753bSChris Leech }
26249a05753bSChris Leech 
26259a05753bSChris Leech /**
26269a05753bSChris Leech  * fcoe_vport_disable() - change vport state
26279a05753bSChris Leech  * @vport: vport to bring online/offline
26289a05753bSChris Leech  * @disable: should the vport be disabled?
26299a05753bSChris Leech  */
26309a05753bSChris Leech static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
26319a05753bSChris Leech {
26329a05753bSChris Leech 	struct fc_lport *lport = vport->dd_data;
26339a05753bSChris Leech 
26349a05753bSChris Leech 	if (disable) {
26359a05753bSChris Leech 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
26369a05753bSChris Leech 		fc_fabric_logoff(lport);
26379a05753bSChris Leech 	} else {
26389a05753bSChris Leech 		lport->boot_time = jiffies;
26399a05753bSChris Leech 		fc_fabric_login(lport);
26409a05753bSChris Leech 		fc_vport_setlink(lport);
26419a05753bSChris Leech 	}
26429a05753bSChris Leech 
26439a05753bSChris Leech 	return 0;
26449a05753bSChris Leech }
26459a05753bSChris Leech 
2646dc8596d3SChris Leech /**
2647dc8596d3SChris Leech  * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2648dc8596d3SChris Leech  * @vport: fc_vport with a new symbolic name string
2649dc8596d3SChris Leech  *
2650dc8596d3SChris Leech  * After generating a new symbolic name string, a new RSPN_ID request is
2651dc8596d3SChris Leech  * sent to the name server.  There is no response handler, so if it fails
2652dc8596d3SChris Leech  * for some reason it will not be retried.
2653dc8596d3SChris Leech  */
2654dc8596d3SChris Leech static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2655dc8596d3SChris Leech {
2656dc8596d3SChris Leech 	struct fc_lport *lport = vport->dd_data;
2657dc8596d3SChris Leech 	struct fc_frame *fp;
2658dc8596d3SChris Leech 	size_t len;
2659dc8596d3SChris Leech 
2660dc8596d3SChris Leech 	snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2661dc8596d3SChris Leech 		 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2662dc8596d3SChris Leech 		 fcoe_netdev(lport)->name, vport->symbolic_name);
2663dc8596d3SChris Leech 
2664dc8596d3SChris Leech 	if (lport->state != LPORT_ST_READY)
2665dc8596d3SChris Leech 		return;
2666dc8596d3SChris Leech 
2667dc8596d3SChris Leech 	len = strnlen(fc_host_symbolic_name(lport->host), 255);
2668dc8596d3SChris Leech 	fp = fc_frame_alloc(lport,
2669dc8596d3SChris Leech 			    sizeof(struct fc_ct_hdr) +
2670dc8596d3SChris Leech 			    sizeof(struct fc_ns_rspn) + len);
2671dc8596d3SChris Leech 	if (!fp)
2672dc8596d3SChris Leech 		return;
2673dc8596d3SChris Leech 	lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2674b94f8951SJoe Eykholt 			     NULL, NULL, 3 * lport->r_a_tov);
2675dc8596d3SChris Leech }
2676b84056bfSYi Zou 
2677b84056bfSYi Zou /**
2678b84056bfSYi Zou  * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2679b84056bfSYi Zou  * @lport: the local port
2680b84056bfSYi Zou  * @fc_lesb: the link error status block
2681b84056bfSYi Zou  */
2682b84056bfSYi Zou static void fcoe_get_lesb(struct fc_lport *lport,
2683b84056bfSYi Zou 			 struct fc_els_lesb *fc_lesb)
2684b84056bfSYi Zou {
2685b84056bfSYi Zou 	unsigned int cpu;
2686b84056bfSYi Zou 	u32 lfc, vlfc, mdac;
2687b84056bfSYi Zou 	struct fcoe_dev_stats *devst;
2688b84056bfSYi Zou 	struct fcoe_fc_els_lesb *lesb;
268928172739SEric Dumazet 	struct rtnl_link_stats64 temp;
2690b84056bfSYi Zou 	struct net_device *netdev = fcoe_netdev(lport);
2691b84056bfSYi Zou 
2692b84056bfSYi Zou 	lfc = 0;
2693b84056bfSYi Zou 	vlfc = 0;
2694b84056bfSYi Zou 	mdac = 0;
2695b84056bfSYi Zou 	lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2696b84056bfSYi Zou 	memset(lesb, 0, sizeof(*lesb));
2697b84056bfSYi Zou 	for_each_possible_cpu(cpu) {
2698b84056bfSYi Zou 		devst = per_cpu_ptr(lport->dev_stats, cpu);
2699b84056bfSYi Zou 		lfc += devst->LinkFailureCount;
2700b84056bfSYi Zou 		vlfc += devst->VLinkFailureCount;
2701b84056bfSYi Zou 		mdac += devst->MissDiscAdvCount;
2702b84056bfSYi Zou 	}
2703b84056bfSYi Zou 	lesb->lesb_link_fail = htonl(lfc);
2704b84056bfSYi Zou 	lesb->lesb_vlink_fail = htonl(vlfc);
2705b84056bfSYi Zou 	lesb->lesb_miss_fka = htonl(mdac);
270628172739SEric Dumazet 	lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
2707b84056bfSYi Zou }
27087d65b0dfSJoe Eykholt 
27097d65b0dfSJoe Eykholt /**
27107d65b0dfSJoe Eykholt  * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
27117d65b0dfSJoe Eykholt  * @lport: the local port
27127d65b0dfSJoe Eykholt  * @port_id: the port ID
27137d65b0dfSJoe Eykholt  * @fp: the received frame, if any, that caused the port_id to be set.
27147d65b0dfSJoe Eykholt  *
27157d65b0dfSJoe Eykholt  * This routine handles the case where we received a FLOGI and are
27167d65b0dfSJoe Eykholt  * entering point-to-point mode.  We need to call fcoe_ctlr_recv_flogi()
27177d65b0dfSJoe Eykholt  * so it can set the non-mapped mode and gateway address.
27187d65b0dfSJoe Eykholt  *
27197d65b0dfSJoe Eykholt  * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
27207d65b0dfSJoe Eykholt  */
27217d65b0dfSJoe Eykholt static void fcoe_set_port_id(struct fc_lport *lport,
27227d65b0dfSJoe Eykholt 			     u32 port_id, struct fc_frame *fp)
27237d65b0dfSJoe Eykholt {
27247d65b0dfSJoe Eykholt 	struct fcoe_port *port = lport_priv(lport);
27257d65b0dfSJoe Eykholt 	struct fcoe_interface *fcoe = port->fcoe;
27267d65b0dfSJoe Eykholt 
27277d65b0dfSJoe Eykholt 	if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
27287d65b0dfSJoe Eykholt 		fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
27297d65b0dfSJoe Eykholt }
2730