blob: 452b42169c48792f554317c9dfff33c16ea9e3e7 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
Chris Leechaf7f85d2009-08-25 13:59:24 -07002 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Robert Love85b4aa42008-12-09 15:10:24 -08003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/version.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/ethtool.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
Robert Love85b4aa42008-12-09 15:10:24 -080028#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Robert Love85b4aa42008-12-09 15:10:24 -080030#include <linux/cpu.h>
31#include <linux/fs.h>
32#include <linux/sysfs.h>
33#include <linux/ctype.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsicam.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_fc.h>
38#include <net/rtnetlink.h>
39
40#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070041#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080042
43#include <scsi/libfc.h>
44#include <scsi/fc_frame.h>
45#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080046
Vasu Devfdd780272009-03-17 11:42:24 -070047#include "fcoe.h"
48
Robert Love85b4aa42008-12-09 15:10:24 -080049MODULE_AUTHOR("Open-FCoE.org");
50MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070051MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080052
Yi Zou05cc7392009-08-25 13:59:03 -070053/* Performance tuning parameters for fcoe */
54static unsigned int fcoe_ddp_min;
55module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
57 "Direct Data Placement (DDP).");
58
Chris Leechdfc1d0f2009-08-25 14:00:13 -070059DEFINE_MUTEX(fcoe_config_mutex);
60
Joe Eykholte7a51992009-08-25 14:04:08 -070061/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
62static DECLARE_COMPLETION(fcoe_flush_completion);
63
Robert Love85b4aa42008-12-09 15:10:24 -080064/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070065/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080066LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070067DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080068
Chris Leechdd3fd722009-04-21 16:27:36 -070069/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080070static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd780272009-03-17 11:42:24 -070071static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
72static int fcoe_rcv(struct sk_buff *, struct net_device *,
73 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080074static int fcoe_percpu_receive_thread(void *);
75static void fcoe_clean_pending_queue(struct fc_lport *);
76static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070077static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080078static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd780272009-03-17 11:42:24 -070079
80static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
81static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd780272009-03-17 11:42:24 -070082
Vasu Dev4bb6b512009-05-06 10:52:34 -070083static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080084static int fcoe_device_notification(struct notifier_block *, ulong, void *);
85static void fcoe_dev_setup(void);
86static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080087static struct fcoe_interface
88*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080089
Robert Love1875f272009-11-03 11:47:50 -080090static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
91 struct packet_type *, struct net_device *);
92
93static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
94static void fcoe_update_src_mac(struct fc_lport *, u8 *);
95static u8 *fcoe_get_src_mac(struct fc_lport *);
96static void fcoe_destroy_work(struct work_struct *);
97
98static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
99 unsigned int);
100static int fcoe_ddp_done(struct fc_lport *, u16);
101
102static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
103
104static int fcoe_create(const char *, struct kernel_param *);
105static int fcoe_destroy(const char *, struct kernel_param *);
Vasu Dev55a66d32009-12-10 09:59:31 -0800106static int fcoe_enable(const char *, struct kernel_param *);
107static int fcoe_disable(const char *, struct kernel_param *);
Robert Love1875f272009-11-03 11:47:50 -0800108
Robert Love1875f272009-11-03 11:47:50 -0800109static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
110 u32 did, struct fc_frame *,
111 unsigned int op,
112 void (*resp)(struct fc_seq *,
113 struct fc_frame *,
114 void *),
115 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800116static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800117
Yi Zoub84056b2009-11-20 14:55:19 -0800118static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
119
Vasu Dev30678172010-10-08 17:12:25 -0700120module_param_call(create, fcoe_create, NULL, (void *)FIP_MODE_FABRIC, S_IWUSR);
Robert Love1875f272009-11-03 11:47:50 -0800121__MODULE_PARM_TYPE(create, "string");
Vasu Dev55a66d32009-12-10 09:59:31 -0800122MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700123module_param_call(create_vn2vn, fcoe_create, NULL,
124 (void *)FIP_MODE_VN2VN, S_IWUSR);
125__MODULE_PARM_TYPE(create_vn2vn, "string");
126MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
127 "on an Ethernet interface");
Robert Love1875f272009-11-03 11:47:50 -0800128module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
129__MODULE_PARM_TYPE(destroy, "string");
Vasu Dev55a66d32009-12-10 09:59:31 -0800130MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
131module_param_call(enable, fcoe_enable, NULL, NULL, S_IWUSR);
132__MODULE_PARM_TYPE(enable, "string");
133MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface.");
134module_param_call(disable, fcoe_disable, NULL, NULL, S_IWUSR);
135__MODULE_PARM_TYPE(disable, "string");
136MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface.");
Robert Love1875f272009-11-03 11:47:50 -0800137
138/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800139static struct notifier_block fcoe_notifier = {
140 .notifier_call = fcoe_device_notification,
141};
142
Robert Love1875f272009-11-03 11:47:50 -0800143/* notification function for CPU hotplug events */
144static struct notifier_block fcoe_cpu_notifier = {
145 .notifier_call = fcoe_cpu_callback,
146};
147
Chris Leeche9084bb2009-11-03 11:46:34 -0800148static struct scsi_transport_template *fcoe_transport_template;
149static struct scsi_transport_template *fcoe_vport_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700150
Robert Love1875f272009-11-03 11:47:50 -0800151static int fcoe_vport_destroy(struct fc_vport *);
152static int fcoe_vport_create(struct fc_vport *, bool disabled);
153static int fcoe_vport_disable(struct fc_vport *, bool disable);
154static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800155static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love1875f272009-11-03 11:47:50 -0800156
157static struct libfc_function_template fcoe_libfc_fcn_templ = {
158 .frame_send = fcoe_xmit,
159 .ddp_setup = fcoe_ddp_setup,
160 .ddp_done = fcoe_ddp_done,
161 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800162 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800163 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800164};
Chris Leech9a057532009-11-03 11:46:40 -0800165
Vasu Dev7f349142009-03-27 09:06:31 -0700166struct fc_function_template fcoe_transport_function = {
167 .show_host_node_name = 1,
168 .show_host_port_name = 1,
169 .show_host_supported_classes = 1,
170 .show_host_supported_fc4s = 1,
171 .show_host_active_fc4s = 1,
172 .show_host_maxframe_size = 1,
173
174 .show_host_port_id = 1,
175 .show_host_supported_speeds = 1,
176 .get_host_speed = fc_get_host_speed,
177 .show_host_speed = 1,
178 .show_host_port_type = 1,
179 .get_host_port_state = fc_get_host_port_state,
180 .show_host_port_state = 1,
181 .show_host_symbolic_name = 1,
182
183 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
184 .show_rport_maxframe_size = 1,
185 .show_rport_supported_classes = 1,
186
187 .show_host_fabric_name = 1,
188 .show_starget_node_name = 1,
189 .show_starget_port_name = 1,
190 .show_starget_port_id = 1,
191 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
192 .show_rport_dev_loss_tmo = 1,
193 .get_fc_host_stats = fc_get_host_stats,
194 .issue_fc_host_lip = fcoe_reset,
195
196 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800197
198 .vport_create = fcoe_vport_create,
199 .vport_delete = fcoe_vport_destroy,
200 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800201 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800202
203 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700204};
205
Chris Leeche9084bb2009-11-03 11:46:34 -0800206struct fc_function_template fcoe_vport_transport_function = {
207 .show_host_node_name = 1,
208 .show_host_port_name = 1,
209 .show_host_supported_classes = 1,
210 .show_host_supported_fc4s = 1,
211 .show_host_active_fc4s = 1,
212 .show_host_maxframe_size = 1,
213
214 .show_host_port_id = 1,
215 .show_host_supported_speeds = 1,
216 .get_host_speed = fc_get_host_speed,
217 .show_host_speed = 1,
218 .show_host_port_type = 1,
219 .get_host_port_state = fc_get_host_port_state,
220 .show_host_port_state = 1,
221 .show_host_symbolic_name = 1,
222
223 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
224 .show_rport_maxframe_size = 1,
225 .show_rport_supported_classes = 1,
226
227 .show_host_fabric_name = 1,
228 .show_starget_node_name = 1,
229 .show_starget_port_name = 1,
230 .show_starget_port_id = 1,
231 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
232 .show_rport_dev_loss_tmo = 1,
233 .get_fc_host_stats = fc_get_host_stats,
234 .issue_fc_host_lip = fcoe_reset,
235
236 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800237
238 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800239};
240
Vasu Dev7f349142009-03-27 09:06:31 -0700241static struct scsi_host_template fcoe_shost_template = {
242 .module = THIS_MODULE,
243 .name = "FCoE Driver",
244 .proc_name = FCOE_NAME,
245 .queuecommand = fc_queuecommand,
246 .eh_abort_handler = fc_eh_abort,
247 .eh_device_reset_handler = fc_eh_device_reset,
248 .eh_host_reset_handler = fc_eh_host_reset,
249 .slave_alloc = fc_slave_alloc,
250 .change_queue_depth = fc_change_queue_depth,
251 .change_queue_type = fc_change_queue_type,
252 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700253 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700254 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
255 .use_clustering = ENABLE_CLUSTERING,
256 .sg_tablesize = SG_ALL,
257 .max_sectors = 0xffff,
258};
259
Chris Leech54b649f2009-08-25 14:00:07 -0700260/**
Robert Love1875f272009-11-03 11:47:50 -0800261 * fcoe_interface_setup() - Setup a FCoE interface
262 * @fcoe: The new FCoE interface
263 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700264 *
265 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700266 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700267 */
268static int fcoe_interface_setup(struct fcoe_interface *fcoe,
269 struct net_device *netdev)
270{
271 struct fcoe_ctlr *fip = &fcoe->ctlr;
272 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800273 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700274 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700275 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700276
277 fcoe->netdev = netdev;
278
Yi Zoub7a727f2009-10-21 16:28:03 -0700279 /* Let LLD initialize for FCoE */
280 ops = netdev->netdev_ops;
281 if (ops->ndo_fcoe_enable) {
282 if (ops->ndo_fcoe_enable(netdev))
283 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
284 " specific feature for LLD.\n");
285 }
286
Chris Leech54b649f2009-08-25 14:00:07 -0700287 /* Do not support for bonding device */
288 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
289 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
290 (netdev->priv_flags & IFF_MASTER_8023AD)) {
john fastabend59d92512009-11-03 11:48:44 -0800291 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700292 return -EOPNOTSUPP;
293 }
294
295 /* look for SAN MAC address, if multiple SAN MACs exist, only
296 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800297 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
298 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700299 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800300 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700301 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800302 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700303 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
304 fip->spma = 1;
305 break;
306 }
307 }
308 rcu_read_unlock();
309
310 /* setup Source Mac Address */
311 if (!fip->spma)
312 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
313
314 /*
315 * Add FCoE MAC address as second unicast MAC address
316 * or enter promiscuous mode if not capable of listening
317 * for multiple unicast MACs.
318 */
Chris Leech54b649f2009-08-25 14:00:07 -0700319 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000320 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700321 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000322 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700323 if (fip->mode == FIP_MODE_VN2VN) {
324 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
325 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
326 } else
327 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700328
329 /*
330 * setup the receive function from ethernet driver
331 * on the ethertype for the given device
332 */
333 fcoe->fcoe_packet_type.func = fcoe_rcv;
334 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
335 fcoe->fcoe_packet_type.dev = netdev;
336 dev_add_pack(&fcoe->fcoe_packet_type);
337
338 fcoe->fip_packet_type.func = fcoe_fip_recv;
339 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
340 fcoe->fip_packet_type.dev = netdev;
341 dev_add_pack(&fcoe->fip_packet_type);
342
343 return 0;
344}
345
Vasu Dev7f349142009-03-27 09:06:31 -0700346/**
Robert Love1875f272009-11-03 11:47:50 -0800347 * fcoe_interface_create() - Create a FCoE interface on a net device
348 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700349 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700350 *
351 * Returns: pointer to a struct fcoe_interface or NULL on error
352 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700353static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
354 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700355{
356 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800357 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700358
Robert Love7287fb92011-01-28 16:03:47 -0800359 if (!try_module_get(THIS_MODULE)) {
360 FCOE_NETDEV_DBG(netdev,
361 "Could not get a reference to the module\n");
362 fcoe = ERR_PTR(-EBUSY);
363 goto out;
364 }
365
Chris Leech030f4e02009-08-25 14:00:02 -0700366 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
367 if (!fcoe) {
368 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800369 fcoe = ERR_PTR(-ENOMEM);
370 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700371 }
372
Chris Leech2e70e242009-08-25 14:00:23 -0700373 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700374 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700375
376 /*
377 * Initialize FIP.
378 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700379 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700380 fcoe->ctlr.send = fcoe_fip_send;
381 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800382 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700383
john fastabend59d92512009-11-03 11:48:44 -0800384 err = fcoe_interface_setup(fcoe, netdev);
385 if (err) {
386 fcoe_ctlr_destroy(&fcoe->ctlr);
387 kfree(fcoe);
388 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800389 fcoe = ERR_PTR(err);
390 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800391 }
Chris Leech030f4e02009-08-25 14:00:02 -0700392
Robert Love7287fb92011-01-28 16:03:47 -0800393 goto out;
394
395out_nomod:
396 module_put(THIS_MODULE);
397out:
Chris Leech030f4e02009-08-25 14:00:02 -0700398 return fcoe;
399}
400
401/**
Robert Love1875f272009-11-03 11:47:50 -0800402 * fcoe_interface_cleanup() - Clean up a FCoE interface
403 * @fcoe: The FCoE interface to be cleaned up
Chris Leech2e70e242009-08-25 14:00:23 -0700404 *
405 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700406 */
407void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
408{
409 struct net_device *netdev = fcoe->netdev;
410 struct fcoe_ctlr *fip = &fcoe->ctlr;
411 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700412 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700413
414 /*
415 * Don't listen for Ethernet packets anymore.
416 * synchronize_net() ensures that the packet handlers are not running
417 * on another CPU. dev_remove_pack() would do that, this calls the
418 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
419 */
420 __dev_remove_pack(&fcoe->fcoe_packet_type);
421 __dev_remove_pack(&fcoe->fip_packet_type);
422 synchronize_net();
423
Chris Leech54b649f2009-08-25 14:00:07 -0700424 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700425 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000426 dev_uc_del(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700427 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000428 dev_uc_del(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700429 if (fip->mode == FIP_MODE_VN2VN) {
430 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
431 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
432 } else
433 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
Yi Zoub7a727f2009-10-21 16:28:03 -0700434
435 /* Tell the LLD we are done w/ FCoE */
436 ops = netdev->netdev_ops;
437 if (ops->ndo_fcoe_disable) {
438 if (ops->ndo_fcoe_disable(netdev))
439 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
440 " specific feature for LLD.\n");
441 }
Chris Leech54b649f2009-08-25 14:00:07 -0700442}
443
444/**
Chris Leech030f4e02009-08-25 14:00:02 -0700445 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800446 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700447 */
448static void fcoe_interface_release(struct kref *kref)
449{
450 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700451 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700452
453 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700454 netdev = fcoe->netdev;
455 /* tear-down the FCoE controller */
456 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700457 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700458 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800459 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700460}
461
462/**
Robert Love1875f272009-11-03 11:47:50 -0800463 * fcoe_interface_get() - Get a reference to a FCoE interface
464 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700465 */
466static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
467{
468 kref_get(&fcoe->kref);
469}
470
471/**
Robert Love1875f272009-11-03 11:47:50 -0800472 * fcoe_interface_put() - Put a reference to a FCoE interface
473 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700474 */
475static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
476{
477 kref_put(&fcoe->kref, fcoe_interface_release);
478}
479
480/**
Robert Love1875f272009-11-03 11:47:50 -0800481 * fcoe_fip_recv() - Handler for received FIP frames
482 * @skb: The receive skb
483 * @netdev: The associated net device
484 * @ptype: The packet_type structure which was used to register this handler
485 * @orig_dev: The original net_device the the skb was received on.
486 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000487 *
488 * Returns: 0 for success
489 */
Robert Love1875f272009-11-03 11:47:50 -0800490static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000491 struct packet_type *ptype,
492 struct net_device *orig_dev)
493{
Chris Leech259ad852009-08-25 13:59:41 -0700494 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000495
Chris Leech259ad852009-08-25 13:59:41 -0700496 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700497 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000498 return 0;
499}
500
501/**
Robert Love1875f272009-11-03 11:47:50 -0800502 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
503 * @fip: The FCoE controller
504 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000505 */
506static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
507{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700508 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000509 dev_queue_xmit(skb);
510}
511
512/**
Robert Love1875f272009-11-03 11:47:50 -0800513 * fcoe_update_src_mac() - Update the Ethernet MAC filters
514 * @lport: The local port to update the source MAC on
515 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000516 *
517 * Remove any previously-set unicast MAC filter.
518 * Add secondary FCoE MAC address filter for our OUI.
519 */
Chris Leech11b56182009-11-03 11:46:29 -0800520static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000521{
Chris Leech11b56182009-11-03 11:46:29 -0800522 struct fcoe_port *port = lport_priv(lport);
523 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000524
Vasu Devab6b85c2009-05-17 12:33:08 +0000525 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800526 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000527 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800528 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000529 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800530 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000531 rtnl_unlock();
532}
533
534/**
Chris Leech11b56182009-11-03 11:46:29 -0800535 * fcoe_get_src_mac() - return the Ethernet source address for an lport
536 * @lport: libfc lport
537 */
538static u8 *fcoe_get_src_mac(struct fc_lport *lport)
539{
540 struct fcoe_port *port = lport_priv(lport);
541
542 return port->data_src_addr;
543}
544
545/**
Robert Love1875f272009-11-03 11:47:50 -0800546 * fcoe_lport_config() - Set up a local port
547 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700548 *
549 * Returns: 0 for success
550 */
Robert Love1875f272009-11-03 11:47:50 -0800551static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700552{
Robert Love1875f272009-11-03 11:47:50 -0800553 lport->link_up = 0;
554 lport->qfull = 0;
555 lport->max_retry_count = 3;
556 lport->max_rport_retry_count = 3;
557 lport->e_d_tov = 2 * 1000; /* FC-FS default */
558 lport->r_a_tov = 2 * 2 * 1000;
559 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
560 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
561 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700562
Robert Love1875f272009-11-03 11:47:50 -0800563 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700564
565 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800566 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700567
568 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800569 lport->crc_offload = 0;
570 lport->seq_offload = 0;
571 lport->lro_enabled = 0;
572 lport->lro_xid = 0;
573 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700574
575 return 0;
576}
577
578/**
Robert Love1875f272009-11-03 11:47:50 -0800579 * fcoe_queue_timer() - The fcoe queue timer
580 * @lport: The local port
Vasu Dev1047f222009-05-06 10:52:40 -0700581 *
582 * Calls fcoe_check_wait_queue on timeout
Vasu Dev1047f222009-05-06 10:52:40 -0700583 */
Robert Love1875f272009-11-03 11:47:50 -0800584static void fcoe_queue_timer(ulong lport)
Vasu Dev1047f222009-05-06 10:52:40 -0700585{
Robert Love1875f272009-11-03 11:47:50 -0800586 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
Vasu Dev1047f222009-05-06 10:52:40 -0700587}
588
589/**
Yi Zoudcece412009-11-20 15:22:21 -0800590 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
591 * @netdev: the associated net device
592 * @wwn: the output WWN
593 * @type: the type of WWN (WWPN or WWNN)
594 *
595 * Returns: 0 for success
596 */
597static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
598{
599 const struct net_device_ops *ops = netdev->netdev_ops;
600
601 if (ops->ndo_fcoe_get_wwn)
602 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
603 return -EINVAL;
604}
605
606/**
Yi Zou54a5b212010-07-20 15:21:17 -0700607 * fcoe_netdev_features_change - Updates the lport's offload flags based
608 * on the LLD netdev's FCoE feature flags
609 */
610static void fcoe_netdev_features_change(struct fc_lport *lport,
611 struct net_device *netdev)
612{
613 mutex_lock(&lport->lp_mutex);
614
615 if (netdev->features & NETIF_F_SG)
616 lport->sg_supp = 1;
617 else
618 lport->sg_supp = 0;
619
620 if (netdev->features & NETIF_F_FCOE_CRC) {
621 lport->crc_offload = 1;
622 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
623 } else {
624 lport->crc_offload = 0;
625 }
626
627 if (netdev->features & NETIF_F_FSO) {
628 lport->seq_offload = 1;
629 lport->lso_max = netdev->gso_max_size;
630 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
631 lport->lso_max);
632 } else {
633 lport->seq_offload = 0;
634 lport->lso_max = 0;
635 }
636
637 if (netdev->fcoe_ddp_xid) {
638 lport->lro_enabled = 1;
639 lport->lro_xid = netdev->fcoe_ddp_xid;
640 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
641 lport->lro_xid);
642 } else {
643 lport->lro_enabled = 0;
644 lport->lro_xid = 0;
645 }
646
647 mutex_unlock(&lport->lp_mutex);
648}
649
650/**
Robert Love1875f272009-11-03 11:47:50 -0800651 * fcoe_netdev_config() - Set up net devive for SW FCoE
652 * @lport: The local port that is associated with the net device
653 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700654 *
Robert Love1875f272009-11-03 11:47:50 -0800655 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700656 *
Robert Love1875f272009-11-03 11:47:50 -0800657 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700658 */
Robert Love1875f272009-11-03 11:47:50 -0800659static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700660{
661 u32 mfs;
662 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700663 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700664 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700665
666 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800667 port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700668 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700669
670 /*
671 * Determine max frame size based on underlying device and optional
672 * user-configured limit. If the MFS is too low, fcoe_link_ok()
673 * will return 0, so do this first.
674 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700675 mfs = netdev->mtu;
676 if (netdev->features & NETIF_F_FCOE_MTU) {
677 mfs = FCOE_MTU;
678 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
679 }
680 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800681 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700682 return -EINVAL;
683
Vasu Dev7f349142009-03-27 09:06:31 -0700684 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700685 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700686
Chris Leech014f5c32009-08-25 13:59:30 -0700687 skb_queue_head_init(&port->fcoe_pending_queue);
688 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800689 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700690
Robert Love5e4f8fe2010-05-07 15:18:35 -0700691 fcoe_link_speed_update(lport);
692
Robert Love1875f272009-11-03 11:47:50 -0800693 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800694 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
695 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800696 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800697 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
698 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700699 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800700 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800701 }
Vasu Dev7f349142009-03-27 09:06:31 -0700702
Vasu Dev7f349142009-03-27 09:06:31 -0700703 return 0;
704}
705
706/**
Robert Love1875f272009-11-03 11:47:50 -0800707 * fcoe_shost_config() - Set up the SCSI host associated with a local port
708 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800709 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700710 *
711 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
712 *
Robert Love1875f272009-11-03 11:47:50 -0800713 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700714 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700715static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700716{
717 int rc = 0;
718
719 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800720 lport->host->max_lun = FCOE_MAX_LUN;
721 lport->host->max_id = FCOE_MAX_FCP_TARGET;
722 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700723 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
724
Robert Love1875f272009-11-03 11:47:50 -0800725 if (lport->vport)
726 lport->host->transportt = fcoe_vport_transport_template;
Chris Leeche9084bb2009-11-03 11:46:34 -0800727 else
Robert Love1875f272009-11-03 11:47:50 -0800728 lport->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700729
730 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800731 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700732 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800733 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700734 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700735 return rc;
736 }
Chris Leech9a057532009-11-03 11:46:40 -0800737
Robert Love1875f272009-11-03 11:47:50 -0800738 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700739 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800740
Robert Love1875f272009-11-03 11:47:50 -0800741 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800742 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800743 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700744
745 return 0;
746}
747
Robert Love1875f272009-11-03 11:47:50 -0800748/**
749 * fcoe_oem_match() - The match routine for the offloaded exchange manager
750 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700751 *
Robert Love1875f272009-11-03 11:47:50 -0800752 * This routine will be associated with an exchange manager (EM). When
753 * the libfc exchange handling code is looking for an EM to use it will
754 * call this routine and pass it the frame that it wishes to send. This
755 * routine will return True if the associated EM is to be used and False
756 * if the echange code should continue looking for an EM.
757 *
758 * The offload EM that this routine is associated with will handle any
759 * packets that are for SCSI read requests.
760 *
761 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700762 */
763bool fcoe_oem_match(struct fc_frame *fp)
764{
Yi Zou05cc7392009-08-25 13:59:03 -0700765 return fc_fcp_is_read(fr_fsp(fp)) &&
766 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700767}
768
Vasu Dev7f349142009-03-27 09:06:31 -0700769/**
Robert Love1875f272009-11-03 11:47:50 -0800770 * fcoe_em_config() - Allocate and configure an exchange manager
771 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700772 *
Robert Love1875f272009-11-03 11:47:50 -0800773 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700774 */
Robert Love1875f272009-11-03 11:47:50 -0800775static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700776{
Robert Love1875f272009-11-03 11:47:50 -0800777 struct fcoe_port *port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700778 struct fcoe_interface *fcoe = port->fcoe;
779 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700780 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700781 u16 min_xid = FCOE_MIN_XID;
782 u16 max_xid = FCOE_MAX_XID;
783
784 /*
785 * Check if need to allocate an em instance for
786 * offload exchange ids to be shared across all VN_PORTs/lport.
787 */
Robert Love1875f272009-11-03 11:47:50 -0800788 if (!lport->lro_enabled || !lport->lro_xid ||
789 (lport->lro_xid >= max_xid)) {
790 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700791 goto skip_oem;
792 }
793
794 /*
795 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700796 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700797 */
Chris Leech25024982009-08-25 13:59:35 -0700798 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
799 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700800 else
Chris Leech25024982009-08-25 13:59:35 -0700801 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700802
Chris Leech25024982009-08-25 13:59:35 -0700803 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700804 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
805 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700806 else
Chris Leech25024982009-08-25 13:59:35 -0700807 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700808
809 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700810 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700811 break;
812 }
813 }
814
Chris Leech991cbb62009-08-25 13:59:51 -0700815 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800816 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700817 printk(KERN_ERR "fcoe_em_config: failed to add "
818 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700819 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700820 return -ENOMEM;
821 }
822 } else {
Robert Love1875f272009-11-03 11:47:50 -0800823 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
824 FCOE_MIN_XID, lport->lro_xid,
825 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700826 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700827 printk(KERN_ERR "fcoe_em_config: failed to allocate "
828 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700829 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700830 return -ENOMEM;
831 }
832 }
833
834 /*
835 * Exclude offload EM xid range from next EM xid range.
836 */
Robert Love1875f272009-11-03 11:47:50 -0800837 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700838
839skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800840 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700841 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700842 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700843 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700844 }
Vasu Dev7f349142009-03-27 09:06:31 -0700845
846 return 0;
847}
848
849/**
Robert Love1875f272009-11-03 11:47:50 -0800850 * fcoe_if_destroy() - Tear down a SW FCoE instance
851 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700852 *
853 * Locking: must be called with the RTNL mutex held and RTNL mutex
854 * needed to be dropped by this function since not dropping RTNL
855 * would cause circular locking warning on synchronous fip worker
856 * cancelling thru fcoe_interface_put invoked by this function.
857 *
Vasu Dev7f349142009-03-27 09:06:31 -0700858 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700859static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700860{
Chris Leech014f5c32009-08-25 13:59:30 -0700861 struct fcoe_port *port = lport_priv(lport);
862 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700863 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700864
Robert Loved5488eb2009-06-10 15:30:59 -0700865 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700866
Vasu Dev7f349142009-03-27 09:06:31 -0700867 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700868 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700869
Vasu Dev7f349142009-03-27 09:06:31 -0700870 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700871 fc_lport_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700872
Chris Leech54b649f2009-08-25 14:00:07 -0700873 /* Stop the transmit retry timer */
874 del_timer_sync(&port->timer);
875
876 /* Free existing transmit skbs */
877 fcoe_clean_pending_queue(lport);
878
Chris Leech11b56182009-11-03 11:46:29 -0800879 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000880 dev_uc_del(netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800881 rtnl_unlock();
882
Chris Leech54b649f2009-08-25 14:00:07 -0700883 /* receives may not be stopped until after this */
884 fcoe_interface_put(fcoe);
885
886 /* Free queued packets for the per-CPU receive threads */
887 fcoe_percpu_clean(lport);
888
Vasu Dev7f349142009-03-27 09:06:31 -0700889 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700890 fc_remove_host(lport->host);
891 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700892
Yi Zou80e736f2010-11-30 16:18:07 -0800893 /* Destroy lport scsi_priv */
894 fc_fcp_destroy(lport);
895
Vasu Dev7f349142009-03-27 09:06:31 -0700896 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700897 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700898
Vasu Dev7f349142009-03-27 09:06:31 -0700899 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700900 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700901
Chris Leech2e70e242009-08-25 14:00:23 -0700902 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700903 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700904}
905
Robert Love1875f272009-11-03 11:47:50 -0800906/**
907 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
908 * @lport: The local port to setup DDP for
909 * @xid: The exchange ID for this DDP transfer
910 * @sgl: The scatterlist describing this transfer
911 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700912 *
Robert Love1875f272009-11-03 11:47:50 -0800913 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700914 */
Robert Love1875f272009-11-03 11:47:50 -0800915static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
916 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700917{
Robert Love1875f272009-11-03 11:47:50 -0800918 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700919
Robert Love1875f272009-11-03 11:47:50 -0800920 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
921 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
922 xid, sgl,
923 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700924
925 return 0;
926}
927
Vasu Dev7f349142009-03-27 09:06:31 -0700928/**
Robert Love1875f272009-11-03 11:47:50 -0800929 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
930 * @lport: The local port to complete DDP on
931 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700932 *
Robert Love1875f272009-11-03 11:47:50 -0800933 * Returns: the length of data that have been completed by DDP
934 */
935static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
936{
937 struct net_device *netdev = fcoe_netdev(lport);
938
939 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
940 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
941 return 0;
942}
943
944/**
945 * fcoe_if_create() - Create a FCoE instance on an interface
946 * @fcoe: The FCoE interface to create a local port on
947 * @parent: The device pointer to be the parent in sysfs for the SCSI host
948 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700949 *
Robert Love1875f272009-11-03 11:47:50 -0800950 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
951 *
952 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700953 */
Chris Leech030f4e02009-08-25 14:00:02 -0700954static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800955 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700956{
Robert Love1875f272009-11-03 11:47:50 -0800957 struct net_device *netdev = fcoe->netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700958 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700959 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -0800960 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800961 /*
962 * parent is only a vport if npiv is 1,
963 * but we'll only use vport in that case so go ahead and set it
964 */
965 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700966
Robert Loved5488eb2009-06-10 15:30:59 -0700967 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700968
Chris Leech9a057532009-11-03 11:46:40 -0800969 if (!npiv) {
970 lport = libfc_host_alloc(&fcoe_shost_template,
971 sizeof(struct fcoe_port));
972 } else {
973 lport = libfc_vport_create(vport,
974 sizeof(struct fcoe_port));
975 }
Chris Leech86221962009-11-03 11:46:08 -0800976 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700977 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
978 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700979 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700980 }
Chris Leech014f5c32009-08-25 13:59:30 -0700981 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700982 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700983 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700984 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700985
Robert Love1875f272009-11-03 11:47:50 -0800986 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700987 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700988 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700989 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
990 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700991 goto out_host_put;
992 }
993
Chris Leech9a057532009-11-03 11:46:40 -0800994 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700995 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
996 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -0800997 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800998 fc_set_wwnn(lport, vport->node_name);
999 fc_set_wwpn(lport, vport->port_name);
1000 }
1001
Vasu Devab6b85c2009-05-17 12:33:08 +00001002 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001003 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001004 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001005 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1006 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001007 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001008 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001009
Vasu Dev7f349142009-03-27 09:06:31 -07001010 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001011 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001012 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001013 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1014 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001015 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001016 }
1017
Vasu Dev7f349142009-03-27 09:06:31 -07001018 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001019 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001020 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001021 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1022 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001023 goto out_lp_destroy;
1024 }
1025
Chris Leech9a057532009-11-03 11:46:40 -08001026 if (!npiv) {
1027 /*
1028 * fcoe_em_alloc() and fcoe_hostlist_add() both
1029 * need to be atomic with respect to other changes to the
1030 * hostlist since fcoe_em_alloc() looks for an existing EM
1031 * instance on host list updated by fcoe_hostlist_add().
1032 *
1033 * This is currently handled through the fcoe_config_mutex
1034 * begin held.
1035 */
Chris Leechc863df32009-08-25 14:00:18 -07001036
Chris Leech9a057532009-11-03 11:46:40 -08001037 /* lport exch manager allocation */
1038 rc = fcoe_em_config(lport);
1039 if (rc) {
1040 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
Robert Love1875f272009-11-03 11:47:50 -08001041 "for the interface\n");
Chris Leech9a057532009-11-03 11:46:40 -08001042 goto out_lp_destroy;
1043 }
Vasu Dev96316092009-07-29 17:05:00 -07001044 }
1045
Chris Leech030f4e02009-08-25 14:00:02 -07001046 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001047 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001048
1049out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001050 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001051out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001052 scsi_host_put(lport->host);
1053out:
1054 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001055}
1056
1057/**
Robert Love1875f272009-11-03 11:47:50 -08001058 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001059 *
Robert Love1875f272009-11-03 11:47:50 -08001060 * Attaches the SW FCoE transport to the FC transport
1061 *
1062 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001063 */
1064static int __init fcoe_if_init(void)
1065{
1066 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -08001067 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
1068 fcoe_vport_transport_template =
1069 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -07001070
Chris Leeche9084bb2009-11-03 11:46:34 -08001071 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -07001072 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001073 return -ENODEV;
1074 }
1075
1076 return 0;
1077}
1078
1079/**
Robert Love1875f272009-11-03 11:47:50 -08001080 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001081 *
Robert Love1875f272009-11-03 11:47:50 -08001082 * Detaches the SW FCoE transport from the FC transport
1083 *
1084 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001085 */
1086int __exit fcoe_if_exit(void)
1087{
Chris Leeche9084bb2009-11-03 11:46:34 -08001088 fc_release_transport(fcoe_transport_template);
1089 fc_release_transport(fcoe_vport_transport_template);
1090 fcoe_transport_template = NULL;
1091 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001092 return 0;
1093}
1094
Robert Love85b4aa42008-12-09 15:10:24 -08001095/**
Robert Love1875f272009-11-03 11:47:50 -08001096 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1097 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001098 */
1099static void fcoe_percpu_thread_create(unsigned int cpu)
1100{
1101 struct fcoe_percpu_s *p;
1102 struct task_struct *thread;
1103
1104 p = &per_cpu(fcoe_percpu, cpu);
1105
1106 thread = kthread_create(fcoe_percpu_receive_thread,
1107 (void *)p, "fcoethread/%d", cpu);
1108
Joe Eykholte7a51992009-08-25 14:04:08 -07001109 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001110 kthread_bind(thread, cpu);
1111 wake_up_process(thread);
1112
1113 spin_lock_bh(&p->fcoe_rx_list.lock);
1114 p->thread = thread;
1115 spin_unlock_bh(&p->fcoe_rx_list.lock);
1116 }
1117}
1118
1119/**
Robert Love1875f272009-11-03 11:47:50 -08001120 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1121 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001122 *
1123 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1124 * current CPU's Rx thread. If the thread being destroyed is bound to
1125 * the CPU processing this context the skbs will be freed.
1126 */
1127static void fcoe_percpu_thread_destroy(unsigned int cpu)
1128{
1129 struct fcoe_percpu_s *p;
1130 struct task_struct *thread;
1131 struct page *crc_eof;
1132 struct sk_buff *skb;
1133#ifdef CONFIG_SMP
1134 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001135 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001136#endif /* CONFIG_SMP */
1137
Robert Loved5488eb2009-06-10 15:30:59 -07001138 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001139
1140 /* Prevent any new skbs from being queued for this CPU. */
1141 p = &per_cpu(fcoe_percpu, cpu);
1142 spin_lock_bh(&p->fcoe_rx_list.lock);
1143 thread = p->thread;
1144 p->thread = NULL;
1145 crc_eof = p->crc_eof_page;
1146 p->crc_eof_page = NULL;
1147 p->crc_eof_offset = 0;
1148 spin_unlock_bh(&p->fcoe_rx_list.lock);
1149
1150#ifdef CONFIG_SMP
1151 /*
1152 * Don't bother moving the skb's if this context is running
1153 * on the same CPU that is having its thread destroyed. This
1154 * can easily happen when the module is removed.
1155 */
1156 if (cpu != targ_cpu) {
1157 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1158 spin_lock_bh(&p0->fcoe_rx_list.lock);
1159 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001160 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1161 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001162
1163 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1164 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1165 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1166 } else {
1167 /*
1168 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001169 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001170 * on the CPU that is going offline.
1171 */
1172 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1173 kfree_skb(skb);
1174 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1175 }
1176 } else {
1177 /*
1178 * This scenario occurs when the module is being removed
1179 * and all threads are being destroyed. skbs will continue
1180 * to be shifted from the CPU thread that is being removed
1181 * to the CPU thread associated with the CPU that is processing
1182 * the module removal. Once there is only one CPU Rx thread it
1183 * will reach this case and we will drop all skbs and later
1184 * stop the thread.
1185 */
1186 spin_lock_bh(&p->fcoe_rx_list.lock);
1187 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1188 kfree_skb(skb);
1189 spin_unlock_bh(&p->fcoe_rx_list.lock);
1190 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001191 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001192#else
1193 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001194 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001195 * being removed. Free all skbs and stop the thread.
1196 */
1197 spin_lock_bh(&p->fcoe_rx_list.lock);
1198 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1199 kfree_skb(skb);
1200 spin_unlock_bh(&p->fcoe_rx_list.lock);
1201#endif
1202
1203 if (thread)
1204 kthread_stop(thread);
1205
1206 if (crc_eof)
1207 put_page(crc_eof);
1208}
1209
1210/**
Robert Love1875f272009-11-03 11:47:50 -08001211 * fcoe_cpu_callback() - Handler for CPU hotplug events
1212 * @nfb: The callback data block
1213 * @action: The event triggering the callback
1214 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001215 *
Robert Love1875f272009-11-03 11:47:50 -08001216 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001217 *
1218 * Returns NOTIFY_OK always.
1219 */
1220static int fcoe_cpu_callback(struct notifier_block *nfb,
1221 unsigned long action, void *hcpu)
1222{
1223 unsigned cpu = (unsigned long)hcpu;
1224
1225 switch (action) {
1226 case CPU_ONLINE:
1227 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001228 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001229 fcoe_percpu_thread_create(cpu);
1230 break;
1231 case CPU_DEAD:
1232 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001233 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001234 fcoe_percpu_thread_destroy(cpu);
1235 break;
1236 default:
1237 break;
1238 }
1239 return NOTIFY_OK;
1240}
1241
Robert Love8976f422009-03-17 11:41:46 -07001242/**
Robert Love1875f272009-11-03 11:47:50 -08001243 * fcoe_rcv() - Receive packets from a net device
1244 * @skb: The received packet
1245 * @netdev: The net device that the packet was received on
1246 * @ptype: The packet type context
1247 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001248 *
Robert Love1875f272009-11-03 11:47:50 -08001249 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1250 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001251 *
1252 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001253 */
Robert Love1875f272009-11-03 11:47:50 -08001254int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001255 struct packet_type *ptype, struct net_device *olddev)
1256{
Robert Love1875f272009-11-03 11:47:50 -08001257 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001258 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001259 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001260 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001261 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001262 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001263 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001264
Chris Leech259ad852009-08-25 13:59:41 -07001265 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001266 lport = fcoe->ctlr.lp;
1267 if (unlikely(!lport)) {
1268 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001269 goto err2;
1270 }
Robert Love1875f272009-11-03 11:47:50 -08001271 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001272 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001273
Robert Love1875f272009-11-03 11:47:50 -08001274 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001275 "data:%p tail:%p end:%p sum:%d dev:%s",
1276 skb->len, skb->data_len, skb->head, skb->data,
1277 skb_tail_pointer(skb), skb_end_pointer(skb),
1278 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001279
Vasu Dev519e5132010-07-20 15:19:32 -07001280 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001281
1282 if (is_fip_mode(&fcoe->ctlr) &&
1283 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1284 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1285 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001286 goto err;
1287 }
1288
1289 /*
1290 * Check for minimum frame length, and make sure required FCoE
1291 * and FC headers are pulled into the linear data area.
1292 */
1293 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001294 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001295 goto err;
1296
1297 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1298 fh = (struct fc_frame_header *) skb_transport_header(skb);
1299
Robert Love0ee31cb2010-10-08 17:12:46 -07001300 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1301 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1302 eh->h_dest);
1303 goto err;
1304 }
1305
Robert Love85b4aa42008-12-09 15:10:24 -08001306 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001307 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001308 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001309
Robert Love85b4aa42008-12-09 15:10:24 -08001310 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001311 * In case the incoming frame's exchange is originated from
1312 * the initiator, then received frame's exchange id is ANDed
1313 * with fc_cpu_mask bits to get the same cpu on which exchange
1314 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001315 */
Vasu Devb2f00912009-08-25 13:58:53 -07001316 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1317 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1318 else
1319 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001320
Robert Love8976f422009-03-17 11:41:46 -07001321 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001322 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001323 if (unlikely(!fps->thread)) {
1324 /*
1325 * The targeted CPU is not ready, let's target
1326 * the first CPU now. For non-SMP systems this
1327 * will check the same CPU twice.
1328 */
Robert Love1875f272009-11-03 11:47:50 -08001329 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001330 "ready for incoming skb- using first online "
1331 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001332
1333 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001334 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001335 fps = &per_cpu(fcoe_percpu, cpu);
1336 spin_lock_bh(&fps->fcoe_rx_list.lock);
1337 if (!fps->thread) {
1338 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1339 goto err;
1340 }
1341 }
1342
1343 /*
1344 * We now have a valid CPU that we're targeting for
1345 * this skb. We also have this receive thread locked,
1346 * so we're free to queue skbs into it's queue.
1347 */
Robert Love85b4aa42008-12-09 15:10:24 -08001348
Chris Leech859b7b62009-11-20 14:54:47 -08001349 /* If this is a SCSI-FCP frame, and this is already executing on the
1350 * correct CPU, and the queue for this CPU is empty, then go ahead
1351 * and process the frame directly in the softirq context.
1352 * This lets us process completions without context switching from the
1353 * NET_RX softirq, to our receive processing thread, and then back to
1354 * BLOCK softirq context.
1355 */
1356 if (fh->fh_type == FC_TYPE_FCP &&
1357 cpu == smp_processor_id() &&
1358 skb_queue_empty(&fps->fcoe_rx_list)) {
1359 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1360 fcoe_recv_frame(skb);
1361 } else {
1362 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1363 if (fps->fcoe_rx_list.qlen == 1)
1364 wake_up_process(fps->thread);
1365 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1366 }
Robert Love85b4aa42008-12-09 15:10:24 -08001367
1368 return 0;
1369err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001370 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1371 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001372err2:
1373 kfree_skb(skb);
1374 return -1;
1375}
Robert Love85b4aa42008-12-09 15:10:24 -08001376
1377/**
Robert Love1875f272009-11-03 11:47:50 -08001378 * fcoe_start_io() - Start FCoE I/O
1379 * @skb: The packet to be transmitted
1380 *
1381 * This routine is called from the net device to start transmitting
1382 * FCoE packets.
Robert Love85b4aa42008-12-09 15:10:24 -08001383 *
1384 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001385 */
Robert Love85b4aa42008-12-09 15:10:24 -08001386static inline int fcoe_start_io(struct sk_buff *skb)
1387{
Chris Leech18fa11e2009-11-03 11:50:05 -08001388 struct sk_buff *nskb;
Robert Love85b4aa42008-12-09 15:10:24 -08001389 int rc;
1390
Chris Leech18fa11e2009-11-03 11:50:05 -08001391 nskb = skb_clone(skb, GFP_ATOMIC);
1392 rc = dev_queue_xmit(nskb);
Robert Love85b4aa42008-12-09 15:10:24 -08001393 if (rc != 0)
1394 return rc;
1395 kfree_skb(skb);
1396 return 0;
1397}
1398
1399/**
Robert Love1875f272009-11-03 11:47:50 -08001400 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1401 * @skb: The packet to be transmitted
1402 * @tlen: The total length of the trailer
1403 *
1404 * This routine allocates a page for frame trailers. The page is re-used if
1405 * there is enough room left on it for the current trailer. If there isn't
1406 * enough buffer left a new page is allocated for the trailer. Reference to
1407 * the page from this function as well as the skbs using the page fragments
1408 * ensure that the page is freed at the appropriate time.
Robert Love85b4aa42008-12-09 15:10:24 -08001409 *
1410 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001411 */
Robert Love85b4aa42008-12-09 15:10:24 -08001412static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1413{
1414 struct fcoe_percpu_s *fps;
1415 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001416
Robert Love5e5e92d2009-03-17 11:41:35 -07001417 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001418 page = fps->crc_eof_page;
1419 if (!page) {
1420 page = alloc_page(GFP_ATOMIC);
1421 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001422 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001423 return -ENOMEM;
1424 }
1425 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001426 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001427 }
1428
1429 get_page(page);
1430 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1431 fps->crc_eof_offset, tlen);
1432 skb->len += tlen;
1433 skb->data_len += tlen;
1434 skb->truesize += tlen;
1435 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1436
1437 if (fps->crc_eof_offset >= PAGE_SIZE) {
1438 fps->crc_eof_page = NULL;
1439 fps->crc_eof_offset = 0;
1440 put_page(page);
1441 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001442 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001443 return 0;
1444}
1445
1446/**
Robert Love1875f272009-11-03 11:47:50 -08001447 * fcoe_fc_crc() - Calculates the CRC for a given frame
1448 * @fp: The frame to be checksumed
Robert Love85b4aa42008-12-09 15:10:24 -08001449 *
Robert Love1875f272009-11-03 11:47:50 -08001450 * This uses crc32() routine to calculate the CRC for a frame
1451 *
1452 * Return: The 32 bit CRC value
Robert Love34f42a02009-02-27 10:55:45 -08001453 */
Robert Love85b4aa42008-12-09 15:10:24 -08001454u32 fcoe_fc_crc(struct fc_frame *fp)
1455{
1456 struct sk_buff *skb = fp_skb(fp);
1457 struct skb_frag_struct *frag;
1458 unsigned char *data;
1459 unsigned long off, len, clen;
1460 u32 crc;
1461 unsigned i;
1462
1463 crc = crc32(~0, skb->data, skb_headlen(skb));
1464
1465 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1466 frag = &skb_shinfo(skb)->frags[i];
1467 off = frag->page_offset;
1468 len = frag->size;
1469 while (len > 0) {
1470 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1471 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1472 KM_SKB_DATA_SOFTIRQ);
1473 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1474 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1475 off += clen;
1476 len -= clen;
1477 }
1478 }
1479 return crc;
1480}
Robert Love85b4aa42008-12-09 15:10:24 -08001481
1482/**
Robert Love1875f272009-11-03 11:47:50 -08001483 * fcoe_xmit() - Transmit a FCoE frame
1484 * @lport: The local port that the frame is to be transmitted for
1485 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001486 *
Robert Love1875f272009-11-03 11:47:50 -08001487 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001488 */
Robert Love1875f272009-11-03 11:47:50 -08001489int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001490{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001491 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001492 u32 crc;
1493 struct ethhdr *eh;
1494 struct fcoe_crc_eof *cp;
1495 struct sk_buff *skb;
1496 struct fcoe_dev_stats *stats;
1497 struct fc_frame_header *fh;
1498 unsigned int hlen; /* header length implies the version */
1499 unsigned int tlen; /* trailer length */
1500 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001501 struct fcoe_port *port = lport_priv(lport);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001502 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001503 u8 sof, eof;
1504 struct fcoe_hdr *hp;
1505
1506 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1507
Robert Love85b4aa42008-12-09 15:10:24 -08001508 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001509 skb = fp_skb(fp);
1510 wlen = skb->len / FCOE_WORD_TO_BYTE;
1511
Robert Love1875f272009-11-03 11:47:50 -08001512 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001513 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001514 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001515 }
1516
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001517 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001518 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001519 return 0;
1520
Robert Love85b4aa42008-12-09 15:10:24 -08001521 sof = fr_sof(fp);
1522 eof = fr_eof(fp);
1523
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001524 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001525 hlen = sizeof(struct fcoe_hdr);
1526 tlen = sizeof(struct fcoe_crc_eof);
1527 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1528
1529 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001530 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001531 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001532 skb->csum_start = skb_headroom(skb);
1533 skb->csum_offset = skb->len;
1534 crc = 0;
1535 } else {
1536 skb->ip_summed = CHECKSUM_NONE;
1537 crc = fcoe_fc_crc(fp);
1538 }
1539
Chris Leech014f5c32009-08-25 13:59:30 -07001540 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001541 if (skb_is_nonlinear(skb)) {
1542 skb_frag_t *frag;
1543 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001544 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001545 return -ENOMEM;
1546 }
1547 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1548 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1549 + frag->page_offset;
1550 } else {
1551 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1552 }
1553
1554 memset(cp, 0, sizeof(*cp));
1555 cp->fcoe_eof = eof;
1556 cp->fcoe_crc32 = cpu_to_le32(~crc);
1557
1558 if (skb_is_nonlinear(skb)) {
1559 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1560 cp = NULL;
1561 }
1562
Chris Leech014f5c32009-08-25 13:59:30 -07001563 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001564 skb_push(skb, elen + hlen);
1565 skb_reset_mac_header(skb);
1566 skb_reset_network_header(skb);
1567 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001568 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001569 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001570
1571 /* fill up mac and fcoe headers */
1572 eh = eth_hdr(skb);
1573 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001574 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001575 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001576 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001577
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001578 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1579 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001580 else
Chris Leech11b56182009-11-03 11:46:29 -08001581 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001582
1583 hp = (struct fcoe_hdr *)(eh + 1);
1584 memset(hp, 0, sizeof(*hp));
1585 if (FC_FCOE_VER)
1586 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1587 hp->fcoe_sof = sof;
1588
Yi Zou39ca9a02009-02-27 14:07:15 -08001589 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001590 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001591 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1592 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1593 } else {
1594 skb_shinfo(skb)->gso_type = 0;
1595 skb_shinfo(skb)->gso_size = 0;
1596 }
Robert Love85b4aa42008-12-09 15:10:24 -08001597 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001598 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001599 stats->TxFrames++;
1600 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001601 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001602
1603 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001604 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001605 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001606 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001607 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001608 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001609
1610 return 0;
1611}
Robert Love85b4aa42008-12-09 15:10:24 -08001612
Robert Love34f42a02009-02-27 10:55:45 -08001613/**
Robert Love1875f272009-11-03 11:47:50 -08001614 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1615 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001616 */
1617static void fcoe_percpu_flush_done(struct sk_buff *skb)
1618{
1619 complete(&fcoe_flush_completion);
1620}
1621
1622/**
Chris Leech859b7b62009-11-20 14:54:47 -08001623 * fcoe_recv_frame() - process a single received frame
1624 * @skb: frame to process
1625 */
1626static void fcoe_recv_frame(struct sk_buff *skb)
1627{
1628 u32 fr_len;
1629 struct fc_lport *lport;
1630 struct fcoe_rcv_info *fr;
1631 struct fcoe_dev_stats *stats;
1632 struct fc_frame_header *fh;
1633 struct fcoe_crc_eof crc_eof;
1634 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001635 struct fcoe_port *port;
1636 struct fcoe_hdr *hp;
1637
1638 fr = fcoe_dev_from_skb(skb);
1639 lport = fr->fr_dev;
1640 if (unlikely(!lport)) {
1641 if (skb->destructor != fcoe_percpu_flush_done)
1642 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1643 kfree_skb(skb);
1644 return;
1645 }
1646
1647 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1648 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1649 skb->len, skb->data_len,
1650 skb->head, skb->data, skb_tail_pointer(skb),
1651 skb_end_pointer(skb), skb->csum,
1652 skb->dev ? skb->dev->name : "<NULL>");
1653
Chris Leech859b7b62009-11-20 14:54:47 -08001654 port = lport_priv(lport);
1655 if (skb_is_nonlinear(skb))
1656 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001657
1658 /*
1659 * Frame length checks and setting up the header pointers
1660 * was done in fcoe_rcv already.
1661 */
1662 hp = (struct fcoe_hdr *) skb_network_header(skb);
1663 fh = (struct fc_frame_header *) skb_transport_header(skb);
1664
Joe Eykholtf018b732010-03-12 16:08:55 -08001665 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001666 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1667 if (stats->ErrorFrames < 5)
1668 printk(KERN_WARNING "fcoe: FCoE version "
1669 "mismatch: The frame has "
1670 "version %x, but the "
1671 "initiator supports version "
1672 "%x\n", FC_FCOE_DECAPS_VER(hp),
1673 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001674 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001675 }
1676
1677 skb_pull(skb, sizeof(struct fcoe_hdr));
1678 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1679
1680 stats->RxFrames++;
1681 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1682
1683 fp = (struct fc_frame *)skb;
1684 fc_frame_init(fp);
1685 fr_dev(fp) = lport;
1686 fr_sof(fp) = hp->fcoe_sof;
1687
1688 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001689 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1690 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001691 fr_eof(fp) = crc_eof.fcoe_eof;
1692 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001693 if (pskb_trim(skb, fr_len))
1694 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001695
1696 /*
1697 * We only check CRC if no offload is available and if it is
1698 * it's solicited data, in which case, the FCP layer would
1699 * check it during the copy.
1700 */
1701 if (lport->crc_offload &&
1702 skb->ip_summed == CHECKSUM_UNNECESSARY)
1703 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1704 else
1705 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1706
1707 fh = fc_frame_header_get(fp);
Joe Eykholtf018b732010-03-12 16:08:55 -08001708 if ((fh->fh_r_ctl != FC_RCTL_DD_SOL_DATA ||
1709 fh->fh_type != FC_TYPE_FCP) &&
1710 (fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
Chris Leech859b7b62009-11-20 14:54:47 -08001711 if (le32_to_cpu(fr_crc(fp)) !=
1712 ~crc32(~0, skb->data, fr_len)) {
1713 if (stats->InvalidCRCCount < 5)
1714 printk(KERN_WARNING "fcoe: dropping "
1715 "frame with CRC error\n");
1716 stats->InvalidCRCCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001717 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001718 }
1719 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1720 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001721 put_cpu();
Chris Leech859b7b62009-11-20 14:54:47 -08001722 fc_exch_recv(lport, fp);
Joe Eykholtf018b732010-03-12 16:08:55 -08001723 return;
1724
1725drop:
1726 stats->ErrorFrames++;
1727 put_cpu();
1728 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001729}
1730
1731/**
Robert Love1875f272009-11-03 11:47:50 -08001732 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1733 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001734 *
1735 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001736 */
1737int fcoe_percpu_receive_thread(void *arg)
1738{
1739 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001740 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001741
Robert Love4469c192009-02-27 10:56:38 -08001742 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001743
1744 while (!kthread_should_stop()) {
1745
1746 spin_lock_bh(&p->fcoe_rx_list.lock);
1747 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1748 set_current_state(TASK_INTERRUPTIBLE);
1749 spin_unlock_bh(&p->fcoe_rx_list.lock);
1750 schedule();
1751 set_current_state(TASK_RUNNING);
1752 if (kthread_should_stop())
1753 return 0;
1754 spin_lock_bh(&p->fcoe_rx_list.lock);
1755 }
1756 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001757 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001758 }
1759 return 0;
1760}
1761
1762/**
Robert Love1875f272009-11-03 11:47:50 -08001763 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1764 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001765 *
Robert Love1875f272009-11-03 11:47:50 -08001766 * This empties the wait_queue, dequeues the head of the wait_queue queue
1767 * and calls fcoe_start_io() for each packet. If all skb have been
1768 * transmitted it returns the qlen. If an error occurs it restores
1769 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001770 *
Robert Love1875f272009-11-03 11:47:50 -08001771 * The wait_queue is used when the skb transmit fails. The failed skb
1772 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001773 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001774 */
Robert Love1875f272009-11-03 11:47:50 -08001775static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001776{
Robert Love1875f272009-11-03 11:47:50 -08001777 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001778 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001779
Chris Leech014f5c32009-08-25 13:59:30 -07001780 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001781
1782 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001783 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001784
Chris Leech014f5c32009-08-25 13:59:30 -07001785 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001786 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001787 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001788
Chris Leech014f5c32009-08-25 13:59:30 -07001789 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001790 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001791 port->fcoe_pending_queue.qlen++;
1792 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001793
Chris Leech014f5c32009-08-25 13:59:30 -07001794 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001795 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001796 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001797
1798 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001799 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001800 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001801 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001802 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001803 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001804 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001805 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001806 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001807
Chris Leech014f5c32009-08-25 13:59:30 -07001808 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001809 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001810 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1811 mod_timer(&port->timer, jiffies + 2);
1812 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001813out:
Chris Leech014f5c32009-08-25 13:59:30 -07001814 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001815 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001816 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001817 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001818}
1819
1820/**
Robert Love1875f272009-11-03 11:47:50 -08001821 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001822 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001823static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001824{
Robert Love85b4aa42008-12-09 15:10:24 -08001825 register_netdevice_notifier(&fcoe_notifier);
1826}
1827
1828/**
Robert Love1875f272009-11-03 11:47:50 -08001829 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001830 */
Robert Love85b4aa42008-12-09 15:10:24 -08001831static void fcoe_dev_cleanup(void)
1832{
1833 unregister_netdevice_notifier(&fcoe_notifier);
1834}
1835
1836/**
Robert Love1875f272009-11-03 11:47:50 -08001837 * fcoe_device_notification() - Handler for net device events
1838 * @notifier: The context of the notification
1839 * @event: The type of event
1840 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001841 *
Robert Love1875f272009-11-03 11:47:50 -08001842 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001843 *
1844 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001845 */
Robert Love85b4aa42008-12-09 15:10:24 -08001846static int fcoe_device_notification(struct notifier_block *notifier,
1847 ulong event, void *ptr)
1848{
Robert Love1875f272009-11-03 11:47:50 -08001849 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001850 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001851 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001852 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001853 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001854 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001855 u32 mfs;
1856 int rc = NOTIFY_OK;
1857
Chris Leech014f5c32009-08-25 13:59:30 -07001858 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001859 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001860 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001861 break;
1862 }
1863 }
Robert Love1875f272009-11-03 11:47:50 -08001864 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001865 rc = NOTIFY_DONE;
1866 goto out;
1867 }
1868
Robert Love85b4aa42008-12-09 15:10:24 -08001869 switch (event) {
1870 case NETDEV_DOWN:
1871 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001872 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001873 break;
1874 case NETDEV_UP:
1875 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001876 break;
1877 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001878 if (netdev->features & NETIF_F_FCOE_MTU)
1879 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001880 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1881 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001882 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001883 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001884 break;
1885 case NETDEV_REGISTER:
1886 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001887 case NETDEV_UNREGISTER:
1888 list_del(&fcoe->list);
1889 port = lport_priv(fcoe->ctlr.lp);
1890 fcoe_interface_cleanup(fcoe);
1891 schedule_work(&port->destroy_work);
1892 goto out;
1893 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001894 case NETDEV_FEAT_CHANGE:
1895 fcoe_netdev_features_change(lport, netdev);
1896 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001897 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001898 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001899 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001900 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001901
1902 fcoe_link_speed_update(lport);
1903
Robert Love1875f272009-11-03 11:47:50 -08001904 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001905 fcoe_ctlr_link_up(&fcoe->ctlr);
1906 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001907 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001908 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001909 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001910 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001911 }
1912out:
1913 return rc;
1914}
1915
1916/**
Robert Love1875f272009-11-03 11:47:50 -08001917 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
1918 * @buffer: The name of the net device
Robert Love85b4aa42008-12-09 15:10:24 -08001919 *
Robert Love1875f272009-11-03 11:47:50 -08001920 * Returns: NULL or a ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001921 */
Robert Love85b4aa42008-12-09 15:10:24 -08001922static struct net_device *fcoe_if_to_netdev(const char *buffer)
1923{
1924 char *cp;
1925 char ifname[IFNAMSIZ + 2];
1926
1927 if (buffer) {
1928 strlcpy(ifname, buffer, IFNAMSIZ);
1929 cp = ifname + strlen(ifname);
1930 while (--cp >= ifname && *cp == '\n')
1931 *cp = '\0';
1932 return dev_get_by_name(&init_net, ifname);
1933 }
1934 return NULL;
1935}
1936
1937/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001938 * fcoe_disable() - Disables a FCoE interface
1939 * @buffer: The name of the Ethernet interface to be disabled
1940 * @kp: The associated kernel parameter
1941 *
1942 * Called from sysfs.
1943 *
1944 * Returns: 0 for success
1945 */
1946static int fcoe_disable(const char *buffer, struct kernel_param *kp)
1947{
1948 struct fcoe_interface *fcoe;
1949 struct net_device *netdev;
1950 int rc = 0;
1951
1952 mutex_lock(&fcoe_config_mutex);
1953#ifdef CONFIG_FCOE_MODULE
1954 /*
1955 * Make sure the module has been initialized, and is not about to be
1956 * removed. Module paramter sysfs files are writable before the
1957 * module_init function is called and after module_exit.
1958 */
1959 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1960 rc = -ENODEV;
1961 goto out_nodev;
1962 }
1963#endif
1964
1965 netdev = fcoe_if_to_netdev(buffer);
1966 if (!netdev) {
1967 rc = -ENODEV;
1968 goto out_nodev;
1969 }
1970
Vasu Dev34ce27b2010-05-07 15:18:46 -07001971 if (!rtnl_trylock()) {
1972 dev_put(netdev);
1973 mutex_unlock(&fcoe_config_mutex);
1974 return restart_syscall();
1975 }
1976
Vasu Dev55a66d32009-12-10 09:59:31 -08001977 fcoe = fcoe_hostlist_lookup_port(netdev);
1978 rtnl_unlock();
1979
Chris Leech9ee50e42010-04-09 14:22:23 -07001980 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001981 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001982 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001983 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001984 rc = -ENODEV;
1985
1986 dev_put(netdev);
1987out_nodev:
1988 mutex_unlock(&fcoe_config_mutex);
1989 return rc;
1990}
1991
1992/**
1993 * fcoe_enable() - Enables a FCoE interface
1994 * @buffer: The name of the Ethernet interface to be enabled
1995 * @kp: The associated kernel parameter
1996 *
1997 * Called from sysfs.
1998 *
1999 * Returns: 0 for success
2000 */
2001static int fcoe_enable(const char *buffer, struct kernel_param *kp)
2002{
2003 struct fcoe_interface *fcoe;
2004 struct net_device *netdev;
2005 int rc = 0;
2006
2007 mutex_lock(&fcoe_config_mutex);
2008#ifdef CONFIG_FCOE_MODULE
2009 /*
2010 * Make sure the module has been initialized, and is not about to be
2011 * removed. Module paramter sysfs files are writable before the
2012 * module_init function is called and after module_exit.
2013 */
2014 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2015 rc = -ENODEV;
2016 goto out_nodev;
2017 }
2018#endif
2019
2020 netdev = fcoe_if_to_netdev(buffer);
2021 if (!netdev) {
2022 rc = -ENODEV;
2023 goto out_nodev;
2024 }
2025
Vasu Dev34ce27b2010-05-07 15:18:46 -07002026 if (!rtnl_trylock()) {
2027 dev_put(netdev);
2028 mutex_unlock(&fcoe_config_mutex);
2029 return restart_syscall();
2030 }
2031
Vasu Dev55a66d32009-12-10 09:59:31 -08002032 fcoe = fcoe_hostlist_lookup_port(netdev);
2033 rtnl_unlock();
2034
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002035 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002036 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002037 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2038 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002039
2040 dev_put(netdev);
2041out_nodev:
2042 mutex_unlock(&fcoe_config_mutex);
2043 return rc;
2044}
2045
2046/**
Robert Love1875f272009-11-03 11:47:50 -08002047 * fcoe_destroy() - Destroy a FCoE interface
2048 * @buffer: The name of the Ethernet interface to be destroyed
2049 * @kp: The associated kernel parameter
2050 *
2051 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002052 *
2053 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002054 */
Robert Love85b4aa42008-12-09 15:10:24 -08002055static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
2056{
Chris Leech030f4e02009-08-25 14:00:02 -07002057 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002058 struct net_device *netdev;
Mike Christie8eca355f2009-10-21 16:27:44 -07002059 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002060
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002061 mutex_lock(&fcoe_config_mutex);
2062#ifdef CONFIG_FCOE_MODULE
2063 /*
2064 * Make sure the module has been initialized, and is not about to be
2065 * removed. Module paramter sysfs files are writable before the
2066 * module_init function is called and after module_exit.
2067 */
2068 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2069 rc = -ENODEV;
2070 goto out_nodev;
2071 }
2072#endif
2073
Robert Love85b4aa42008-12-09 15:10:24 -08002074 netdev = fcoe_if_to_netdev(buffer);
2075 if (!netdev) {
2076 rc = -ENODEV;
2077 goto out_nodev;
2078 }
Chris Leech2e70e242009-08-25 14:00:23 -07002079
Vasu Dev34ce27b2010-05-07 15:18:46 -07002080 if (!rtnl_trylock()) {
2081 dev_put(netdev);
2082 mutex_unlock(&fcoe_config_mutex);
2083 return restart_syscall();
2084 }
2085
Chris Leech2e70e242009-08-25 14:00:23 -07002086 fcoe = fcoe_hostlist_lookup_port(netdev);
2087 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07002088 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002089 rc = -ENODEV;
2090 goto out_putdev;
2091 }
Chris Leech2e70e242009-08-25 14:00:23 -07002092 fcoe_interface_cleanup(fcoe);
Yi Zou54a5b212010-07-20 15:21:17 -07002093 list_del(&fcoe->list);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002094 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002095 fcoe_if_destroy(fcoe->ctlr.lp);
Rob Love6409ea652010-01-21 10:16:05 -08002096
Robert Love85b4aa42008-12-09 15:10:24 -08002097out_putdev:
2098 dev_put(netdev);
2099out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002100 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002101 return rc;
2102}
2103
Robert Love1875f272009-11-03 11:47:50 -08002104/**
2105 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2106 * @work: Handle to the FCoE port to be destroyed
2107 */
Chris Leech2e70e242009-08-25 14:00:23 -07002108static void fcoe_destroy_work(struct work_struct *work)
2109{
2110 struct fcoe_port *port;
2111
2112 port = container_of(work, struct fcoe_port, destroy_work);
2113 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002114 rtnl_lock();
2115 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002116 fcoe_if_destroy(port->lport);
2117 mutex_unlock(&fcoe_config_mutex);
2118}
2119
Robert Love85b4aa42008-12-09 15:10:24 -08002120/**
Robert Love1875f272009-11-03 11:47:50 -08002121 * fcoe_create() - Create a fcoe interface
2122 * @buffer: The name of the Ethernet interface to create on
2123 * @kp: The associated kernel param
2124 *
2125 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002126 *
2127 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002128 */
Robert Love85b4aa42008-12-09 15:10:24 -08002129static int fcoe_create(const char *buffer, struct kernel_param *kp)
2130{
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002131 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
Robert Love85b4aa42008-12-09 15:10:24 -08002132 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07002133 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002134 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002135 struct net_device *netdev;
2136
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002137 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002138
2139 if (!rtnl_trylock()) {
2140 mutex_unlock(&fcoe_config_mutex);
2141 return restart_syscall();
2142 }
2143
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002144#ifdef CONFIG_FCOE_MODULE
2145 /*
2146 * Make sure the module has been initialized, and is not about to be
2147 * removed. Module paramter sysfs files are writable before the
2148 * module_init function is called and after module_exit.
2149 */
2150 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2151 rc = -ENODEV;
Robert Love7287fb92011-01-28 16:03:47 -08002152 goto out_nodev;
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002153 }
2154#endif
2155
Robert Love85b4aa42008-12-09 15:10:24 -08002156 netdev = fcoe_if_to_netdev(buffer);
2157 if (!netdev) {
2158 rc = -ENODEV;
2159 goto out_nodev;
2160 }
Chris Leech2e70e242009-08-25 14:00:23 -07002161
Robert Love85b4aa42008-12-09 15:10:24 -08002162 /* look for existing lport */
2163 if (fcoe_hostlist_lookup(netdev)) {
2164 rc = -EEXIST;
2165 goto out_putdev;
2166 }
Robert Love85b4aa42008-12-09 15:10:24 -08002167
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002168 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002169 if (IS_ERR(fcoe)) {
2170 rc = PTR_ERR(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002171 goto out_putdev;
2172 }
2173
Chris Leech9a057532009-11-03 11:46:40 -08002174 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002175 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002176 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002177 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002178 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002179 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002180 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002181 }
Chris Leech030f4e02009-08-25 14:00:02 -07002182
Chris Leech54b649f2009-08-25 14:00:07 -07002183 /* Make this the "master" N_Port */
2184 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002185
Chris Leechc863df32009-08-25 14:00:18 -07002186 /* add to lports list */
2187 fcoe_hostlist_add(lport);
2188
Chris Leech54b649f2009-08-25 14:00:07 -07002189 /* start FIP Discovery and FLOGI */
2190 lport->boot_time = jiffies;
2191 fc_fabric_login(lport);
2192 if (!fcoe_link_ok(lport))
2193 fcoe_ctlr_link_up(&fcoe->ctlr);
2194
Chris Leech54b649f2009-08-25 14:00:07 -07002195 /*
2196 * Release from init in fcoe_interface_create(), on success lport
2197 * should be holding a reference taken in fcoe_if_create().
2198 */
Chris Leech030f4e02009-08-25 14:00:02 -07002199 fcoe_interface_put(fcoe);
Rob Love6409ea652010-01-21 10:16:05 -08002200 dev_put(netdev);
2201 rtnl_unlock();
2202 mutex_unlock(&fcoe_config_mutex);
2203
2204 return 0;
2205out_free:
2206 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002207out_putdev:
2208 dev_put(netdev);
2209out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002210 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002211 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002212 return rc;
2213}
2214
Robert Love34f42a02009-02-27 10:55:45 -08002215/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002216 * fcoe_link_speed_update() - Update the supported and actual link speeds
2217 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002218 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002219 * Returns: 0 if the ethtool query was successful
2220 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002221 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002222int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002223{
Robert Love1875f272009-11-03 11:47:50 -08002224 struct fcoe_port *port = lport_priv(lport);
2225 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08002226 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002227
Robert Love5e4f8fe2010-05-07 15:18:35 -07002228 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002229 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002230 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2231 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2232 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002233 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002234 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002235 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002236 FC_PORTSPEED_10GBIT;
2237 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002238 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002239 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002240 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002241
Yi Zou2f718d62009-07-29 17:04:01 -07002242 return 0;
2243 }
2244 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002245}
Robert Love85b4aa42008-12-09 15:10:24 -08002246
Robert Love34f42a02009-02-27 10:55:45 -08002247/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002248 * fcoe_link_ok() - Check if the link is OK for a local port
2249 * @lport: The local port to check link on
2250 *
2251 * Returns: 0 if link is UP and OK, -1 if not
2252 *
2253 */
2254int fcoe_link_ok(struct fc_lport *lport)
2255{
2256 struct fcoe_port *port = lport_priv(lport);
2257 struct net_device *netdev = port->fcoe->netdev;
2258
2259 if (netif_oper_up(netdev))
2260 return 0;
2261 return -1;
2262}
2263
2264/**
Robert Love1875f272009-11-03 11:47:50 -08002265 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2266 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002267 *
2268 * Must be called with fcoe_create_mutex held to single-thread completion.
2269 *
2270 * This flushes the pending skbs by adding a new skb to each queue and
2271 * waiting until they are all freed. This assures us that not only are
2272 * there no packets that will be handled by the lport, but also that any
2273 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002274 */
Robert Love1875f272009-11-03 11:47:50 -08002275void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002276{
Robert Love85b4aa42008-12-09 15:10:24 -08002277 struct fcoe_percpu_s *pp;
2278 struct fcoe_rcv_info *fr;
2279 struct sk_buff_head *list;
2280 struct sk_buff *skb, *next;
2281 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002282 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002283
Robert Love5e5e92d2009-03-17 11:41:35 -07002284 for_each_possible_cpu(cpu) {
2285 pp = &per_cpu(fcoe_percpu, cpu);
2286 spin_lock_bh(&pp->fcoe_rx_list.lock);
2287 list = &pp->fcoe_rx_list;
2288 head = list->next;
2289 for (skb = head; skb != (struct sk_buff *)list;
2290 skb = next) {
2291 next = skb->next;
2292 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002293 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002294 __skb_unlink(skb, list);
2295 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002296 }
Robert Love85b4aa42008-12-09 15:10:24 -08002297 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002298
2299 if (!pp->thread || !cpu_online(cpu)) {
2300 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2301 continue;
2302 }
2303
2304 skb = dev_alloc_skb(0);
2305 if (!skb) {
2306 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2307 continue;
2308 }
2309 skb->destructor = fcoe_percpu_flush_done;
2310
2311 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2312 if (pp->fcoe_rx_list.qlen == 1)
2313 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002314 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002315
2316 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002317 }
2318}
Robert Love85b4aa42008-12-09 15:10:24 -08002319
2320/**
Robert Love34f42a02009-02-27 10:55:45 -08002321 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002322 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002323 */
Robert Love1875f272009-11-03 11:47:50 -08002324void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002325{
Robert Love1875f272009-11-03 11:47:50 -08002326 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002327 struct sk_buff *skb;
2328
Chris Leech014f5c32009-08-25 13:59:30 -07002329 spin_lock_bh(&port->fcoe_pending_queue.lock);
2330 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2331 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002332 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002333 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002334 }
Chris Leech014f5c32009-08-25 13:59:30 -07002335 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002336}
Robert Love85b4aa42008-12-09 15:10:24 -08002337
2338/**
Robert Love1875f272009-11-03 11:47:50 -08002339 * fcoe_reset() - Reset a local port
2340 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002341 *
Robert Love1875f272009-11-03 11:47:50 -08002342 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002343 */
2344int fcoe_reset(struct Scsi_Host *shost)
2345{
2346 struct fc_lport *lport = shost_priv(shost);
2347 fc_lport_reset(lport);
2348 return 0;
2349}
Robert Love85b4aa42008-12-09 15:10:24 -08002350
Robert Love34f42a02009-02-27 10:55:45 -08002351/**
Robert Love1875f272009-11-03 11:47:50 -08002352 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2353 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002354 *
Robert Love1875f272009-11-03 11:47:50 -08002355 * Locking: Must be called with the RNL mutex held.
2356 *
2357 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002358 */
Chris Leech014f5c32009-08-25 13:59:30 -07002359static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002360fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002361{
Chris Leech014f5c32009-08-25 13:59:30 -07002362 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002363
Chris Leech014f5c32009-08-25 13:59:30 -07002364 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002365 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002366 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002367 }
Robert Love85b4aa42008-12-09 15:10:24 -08002368 return NULL;
2369}
2370
Robert Love34f42a02009-02-27 10:55:45 -08002371/**
Robert Love1875f272009-11-03 11:47:50 -08002372 * fcoe_hostlist_lookup() - Find the local port associated with a
2373 * given net device
2374 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002375 *
Robert Love1875f272009-11-03 11:47:50 -08002376 * Locking: Must be called with the RTNL mutex held
2377 *
2378 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002379 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002380static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002381{
Chris Leech014f5c32009-08-25 13:59:30 -07002382 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002383
Chris Leech014f5c32009-08-25 13:59:30 -07002384 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002385 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002386}
Robert Love85b4aa42008-12-09 15:10:24 -08002387
Robert Love34f42a02009-02-27 10:55:45 -08002388/**
Robert Love1875f272009-11-03 11:47:50 -08002389 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2390 * port to the hostlist
2391 * @lport: The local port that identifies the FCoE interface to be added
2392 *
2393 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002394 *
2395 * Returns: 0 for success
2396 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002397static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002398{
Chris Leech014f5c32009-08-25 13:59:30 -07002399 struct fcoe_interface *fcoe;
2400 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002401
Chris Leech014f5c32009-08-25 13:59:30 -07002402 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2403 if (!fcoe) {
2404 port = lport_priv(lport);
2405 fcoe = port->fcoe;
2406 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002407 }
2408 return 0;
2409}
Robert Love85b4aa42008-12-09 15:10:24 -08002410
Robert Love34f42a02009-02-27 10:55:45 -08002411/**
Robert Love1875f272009-11-03 11:47:50 -08002412 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002413 *
Robert Love1875f272009-11-03 11:47:50 -08002414 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002415 */
Robert Love85b4aa42008-12-09 15:10:24 -08002416static int __init fcoe_init(void)
2417{
Robert Love1875f272009-11-03 11:47:50 -08002418 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002419 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002420 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002421
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002422 mutex_lock(&fcoe_config_mutex);
2423
Robert Love38eccab2009-03-17 11:41:30 -07002424 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002425 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002426 skb_queue_head_init(&p->fcoe_rx_list);
2427 }
2428
Robert Love8976f422009-03-17 11:41:46 -07002429 for_each_online_cpu(cpu)
2430 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002431
Robert Love8976f422009-03-17 11:41:46 -07002432 /* Initialize per CPU interrupt thread */
2433 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2434 if (rc)
2435 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002436
Robert Love8976f422009-03-17 11:41:46 -07002437 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002438 fcoe_dev_setup();
2439
Chris Leech5892c322009-08-25 13:59:14 -07002440 rc = fcoe_if_init();
2441 if (rc)
2442 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002443
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002444 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002445 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002446
2447out_free:
2448 for_each_online_cpu(cpu) {
2449 fcoe_percpu_thread_destroy(cpu);
2450 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002451 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002452 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002453}
2454module_init(fcoe_init);
2455
2456/**
Robert Love1875f272009-11-03 11:47:50 -08002457 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002458 *
Robert Love1875f272009-11-03 11:47:50 -08002459 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002460 */
Robert Love85b4aa42008-12-09 15:10:24 -08002461static void __exit fcoe_exit(void)
2462{
Chris Leech014f5c32009-08-25 13:59:30 -07002463 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002464 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002465 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002466
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002467 mutex_lock(&fcoe_config_mutex);
2468
Robert Love85b4aa42008-12-09 15:10:24 -08002469 fcoe_dev_cleanup();
2470
Vasu Dev5919a592009-03-27 09:03:29 -07002471 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002472 rtnl_lock();
2473 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002474 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002475 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002476 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002477 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002478 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002479 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002480
Robert Love8976f422009-03-17 11:41:46 -07002481 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2482
Chris Leech014f5c32009-08-25 13:59:30 -07002483 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002484 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002485
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002486 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002487
2488 /* flush any asyncronous interface destroys,
2489 * this should happen after the netdev notifier is unregistered */
2490 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002491 /* That will flush out all the N_Ports on the hostlist, but now we
2492 * may have NPIV VN_Ports scheduled for destruction */
2493 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002494
2495 /* detach from scsi transport
2496 * must happen after all destroys are done, therefor after the flush */
2497 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002498}
2499module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002500
2501/**
2502 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2503 * @seq: active sequence in the FLOGI or FDISC exchange
2504 * @fp: response frame, or error encoded in a pointer (timeout)
2505 * @arg: pointer the the fcoe_ctlr structure
2506 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002507 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002508 * the libfc FLOGI response handler.
2509 */
2510static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2511{
2512 struct fcoe_ctlr *fip = arg;
2513 struct fc_exch *exch = fc_seq_exch(seq);
2514 struct fc_lport *lport = exch->lp;
2515 u8 *mac;
2516
2517 if (IS_ERR(fp))
2518 goto done;
2519
2520 mac = fr_cb(fp)->granted_mac;
2521 if (is_zero_ether_addr(mac)) {
2522 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002523 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002524 fc_frame_free(fp);
2525 return;
2526 }
Chris Leech11b56182009-11-03 11:46:29 -08002527 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002528 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002529done:
2530 fc_lport_flogi_resp(seq, fp, lport);
2531}
2532
2533/**
2534 * fcoe_logo_resp() - FCoE specific LOGO response handler
2535 * @seq: active sequence in the LOGO exchange
2536 * @fp: response frame, or error encoded in a pointer (timeout)
2537 * @arg: pointer the the fcoe_ctlr structure
2538 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002539 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002540 * the libfc LOGO response handler.
2541 */
2542static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2543{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002544 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002545 static u8 zero_mac[ETH_ALEN] = { 0 };
2546
2547 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002548 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002549 fc_lport_logo_resp(seq, fp, lport);
2550}
2551
2552/**
2553 * fcoe_elsct_send - FCoE specific ELS handler
2554 *
2555 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2556 * using FCoE specific response handlers and passing the FIP controller as
2557 * the argument (the lport is still available from the exchange).
2558 *
2559 * Most of the work here is just handed off to the libfc routine.
2560 */
Robert Love1875f272009-11-03 11:47:50 -08002561static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2562 struct fc_frame *fp, unsigned int op,
2563 void (*resp)(struct fc_seq *,
2564 struct fc_frame *,
2565 void *),
2566 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002567{
2568 struct fcoe_port *port = lport_priv(lport);
2569 struct fcoe_interface *fcoe = port->fcoe;
2570 struct fcoe_ctlr *fip = &fcoe->ctlr;
2571 struct fc_frame_header *fh = fc_frame_header_get(fp);
2572
2573 switch (op) {
2574 case ELS_FLOGI:
2575 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002576 if (lport->point_to_multipoint)
2577 break;
Chris Leech11b56182009-11-03 11:46:29 -08002578 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2579 fip, timeout);
2580 case ELS_LOGO:
2581 /* only hook onto fabric logouts, not port logouts */
2582 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2583 break;
2584 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002585 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002586 }
2587 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2588}
2589
Chris Leech9a057532009-11-03 11:46:40 -08002590/**
2591 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2592 * @vport: fc_vport object to create a new fc_host for
2593 * @disabled: start the new fc_host in a disabled state by default?
2594 *
2595 * Returns: 0 for success
2596 */
2597static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2598{
2599 struct Scsi_Host *shost = vport_to_shost(vport);
2600 struct fc_lport *n_port = shost_priv(shost);
2601 struct fcoe_port *port = lport_priv(n_port);
2602 struct fcoe_interface *fcoe = port->fcoe;
2603 struct net_device *netdev = fcoe->netdev;
2604 struct fc_lport *vn_port;
2605
2606 mutex_lock(&fcoe_config_mutex);
2607 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2608 mutex_unlock(&fcoe_config_mutex);
2609
2610 if (IS_ERR(vn_port)) {
2611 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2612 netdev->name);
2613 return -EIO;
2614 }
2615
2616 if (disabled) {
2617 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2618 } else {
2619 vn_port->boot_time = jiffies;
2620 fc_fabric_login(vn_port);
2621 fc_vport_setlink(vn_port);
2622 }
2623 return 0;
2624}
2625
2626/**
2627 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2628 * @vport: fc_vport object that is being destroyed
2629 *
2630 * Returns: 0 for success
2631 */
2632static int fcoe_vport_destroy(struct fc_vport *vport)
2633{
2634 struct Scsi_Host *shost = vport_to_shost(vport);
2635 struct fc_lport *n_port = shost_priv(shost);
2636 struct fc_lport *vn_port = vport->dd_data;
2637 struct fcoe_port *port = lport_priv(vn_port);
2638
2639 mutex_lock(&n_port->lp_mutex);
2640 list_del(&vn_port->list);
2641 mutex_unlock(&n_port->lp_mutex);
2642 schedule_work(&port->destroy_work);
2643 return 0;
2644}
2645
2646/**
2647 * fcoe_vport_disable() - change vport state
2648 * @vport: vport to bring online/offline
2649 * @disable: should the vport be disabled?
2650 */
2651static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2652{
2653 struct fc_lport *lport = vport->dd_data;
2654
2655 if (disable) {
2656 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2657 fc_fabric_logoff(lport);
2658 } else {
2659 lport->boot_time = jiffies;
2660 fc_fabric_login(lport);
2661 fc_vport_setlink(lport);
2662 }
2663
2664 return 0;
2665}
2666
Chris Leechdc8596d2009-11-03 11:47:18 -08002667/**
2668 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2669 * @vport: fc_vport with a new symbolic name string
2670 *
2671 * After generating a new symbolic name string, a new RSPN_ID request is
2672 * sent to the name server. There is no response handler, so if it fails
2673 * for some reason it will not be retried.
2674 */
2675static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2676{
2677 struct fc_lport *lport = vport->dd_data;
2678 struct fc_frame *fp;
2679 size_t len;
2680
2681 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2682 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2683 fcoe_netdev(lport)->name, vport->symbolic_name);
2684
2685 if (lport->state != LPORT_ST_READY)
2686 return;
2687
2688 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2689 fp = fc_frame_alloc(lport,
2690 sizeof(struct fc_ct_hdr) +
2691 sizeof(struct fc_ns_rspn) + len);
2692 if (!fp)
2693 return;
2694 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002695 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002696}
Yi Zoub84056b2009-11-20 14:55:19 -08002697
2698/**
2699 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2700 * @lport: the local port
2701 * @fc_lesb: the link error status block
2702 */
2703static void fcoe_get_lesb(struct fc_lport *lport,
2704 struct fc_els_lesb *fc_lesb)
2705{
2706 unsigned int cpu;
2707 u32 lfc, vlfc, mdac;
2708 struct fcoe_dev_stats *devst;
2709 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002710 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002711 struct net_device *netdev = fcoe_netdev(lport);
2712
2713 lfc = 0;
2714 vlfc = 0;
2715 mdac = 0;
2716 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2717 memset(lesb, 0, sizeof(*lesb));
2718 for_each_possible_cpu(cpu) {
2719 devst = per_cpu_ptr(lport->dev_stats, cpu);
2720 lfc += devst->LinkFailureCount;
2721 vlfc += devst->VLinkFailureCount;
2722 mdac += devst->MissDiscAdvCount;
2723 }
2724 lesb->lesb_link_fail = htonl(lfc);
2725 lesb->lesb_vlink_fail = htonl(vlfc);
2726 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002727 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002728}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002729
2730/**
2731 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2732 * @lport: the local port
2733 * @port_id: the port ID
2734 * @fp: the received frame, if any, that caused the port_id to be set.
2735 *
2736 * This routine handles the case where we received a FLOGI and are
2737 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2738 * so it can set the non-mapped mode and gateway address.
2739 *
2740 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2741 */
2742static void fcoe_set_port_id(struct fc_lport *lport,
2743 u32 port_id, struct fc_frame *fp)
2744{
2745 struct fcoe_port *port = lport_priv(lport);
2746 struct fcoe_interface *fcoe = port->fcoe;
2747
2748 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2749 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2750}