blob: d1146994a1b5d292fe8fe3703b42b528eb474413 [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/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001623 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1624 * @lport: The local port the frame was received on
1625 * @fp: The received frame
1626 *
1627 * Return: 0 on passing filtering checks
1628 */
1629static inline int fcoe_filter_frames(struct fc_lport *lport,
1630 struct fc_frame *fp)
1631{
1632 struct fcoe_interface *fcoe;
1633 struct fc_frame_header *fh;
1634 struct sk_buff *skb = (struct sk_buff *)fp;
1635 struct fcoe_dev_stats *stats;
1636
1637 /*
1638 * We only check CRC if no offload is available and if it is
1639 * it's solicited data, in which case, the FCP layer would
1640 * check it during the copy.
1641 */
1642 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1643 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1644 else
1645 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1646
1647 fh = (struct fc_frame_header *) skb_transport_header(skb);
1648 fh = fc_frame_header_get(fp);
1649 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1650 return 0;
1651
1652 fcoe = ((struct fcoe_port *)lport_priv(lport))->fcoe;
1653 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1654 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1655 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1656 return -EINVAL;
1657 }
1658
1659 if (!fr_flags(fp) & FCPHF_CRC_UNCHECKED ||
1660 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1661 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1662 return 0;
1663 }
1664
1665 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1666 stats->InvalidCRCCount++;
1667 if (stats->InvalidCRCCount < 5)
1668 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1669 return -EINVAL;
1670}
1671
1672/**
Chris Leech859b7b62009-11-20 14:54:47 -08001673 * fcoe_recv_frame() - process a single received frame
1674 * @skb: frame to process
1675 */
1676static void fcoe_recv_frame(struct sk_buff *skb)
1677{
1678 u32 fr_len;
1679 struct fc_lport *lport;
1680 struct fcoe_rcv_info *fr;
1681 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001682 struct fcoe_crc_eof crc_eof;
1683 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001684 struct fcoe_port *port;
1685 struct fcoe_hdr *hp;
1686
1687 fr = fcoe_dev_from_skb(skb);
1688 lport = fr->fr_dev;
1689 if (unlikely(!lport)) {
1690 if (skb->destructor != fcoe_percpu_flush_done)
1691 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1692 kfree_skb(skb);
1693 return;
1694 }
1695
1696 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1697 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1698 skb->len, skb->data_len,
1699 skb->head, skb->data, skb_tail_pointer(skb),
1700 skb_end_pointer(skb), skb->csum,
1701 skb->dev ? skb->dev->name : "<NULL>");
1702
Chris Leech859b7b62009-11-20 14:54:47 -08001703 port = lport_priv(lport);
1704 if (skb_is_nonlinear(skb))
1705 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001706
1707 /*
1708 * Frame length checks and setting up the header pointers
1709 * was done in fcoe_rcv already.
1710 */
1711 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001712
Joe Eykholtf018b732010-03-12 16:08:55 -08001713 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001714 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1715 if (stats->ErrorFrames < 5)
1716 printk(KERN_WARNING "fcoe: FCoE version "
1717 "mismatch: The frame has "
1718 "version %x, but the "
1719 "initiator supports version "
1720 "%x\n", FC_FCOE_DECAPS_VER(hp),
1721 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001722 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001723 }
1724
1725 skb_pull(skb, sizeof(struct fcoe_hdr));
1726 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1727
1728 stats->RxFrames++;
1729 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1730
1731 fp = (struct fc_frame *)skb;
1732 fc_frame_init(fp);
1733 fr_dev(fp) = lport;
1734 fr_sof(fp) = hp->fcoe_sof;
1735
1736 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001737 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1738 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001739 fr_eof(fp) = crc_eof.fcoe_eof;
1740 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001741 if (pskb_trim(skb, fr_len))
1742 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001743
Vasu Dev52ee8322011-01-28 16:03:52 -08001744 if (!fcoe_filter_frames(lport, fp)) {
1745 put_cpu();
1746 fc_exch_recv(lport, fp);
1747 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001748 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001749drop:
1750 stats->ErrorFrames++;
1751 put_cpu();
1752 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001753}
1754
1755/**
Robert Love1875f272009-11-03 11:47:50 -08001756 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1757 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001758 *
1759 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001760 */
1761int fcoe_percpu_receive_thread(void *arg)
1762{
1763 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001764 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001765
Robert Love4469c192009-02-27 10:56:38 -08001766 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001767
1768 while (!kthread_should_stop()) {
1769
1770 spin_lock_bh(&p->fcoe_rx_list.lock);
1771 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1772 set_current_state(TASK_INTERRUPTIBLE);
1773 spin_unlock_bh(&p->fcoe_rx_list.lock);
1774 schedule();
1775 set_current_state(TASK_RUNNING);
1776 if (kthread_should_stop())
1777 return 0;
1778 spin_lock_bh(&p->fcoe_rx_list.lock);
1779 }
1780 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001781 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001782 }
1783 return 0;
1784}
1785
1786/**
Robert Love1875f272009-11-03 11:47:50 -08001787 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1788 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001789 *
Robert Love1875f272009-11-03 11:47:50 -08001790 * This empties the wait_queue, dequeues the head of the wait_queue queue
1791 * and calls fcoe_start_io() for each packet. If all skb have been
1792 * transmitted it returns the qlen. If an error occurs it restores
1793 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001794 *
Robert Love1875f272009-11-03 11:47:50 -08001795 * The wait_queue is used when the skb transmit fails. The failed skb
1796 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001797 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001798 */
Robert Love1875f272009-11-03 11:47:50 -08001799static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001800{
Robert Love1875f272009-11-03 11:47:50 -08001801 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001802 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001803
Chris Leech014f5c32009-08-25 13:59:30 -07001804 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001805
1806 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001807 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001808
Chris Leech014f5c32009-08-25 13:59:30 -07001809 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001810 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001811 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001812
Chris Leech014f5c32009-08-25 13:59:30 -07001813 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001814 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001815 port->fcoe_pending_queue.qlen++;
1816 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001817
Chris Leech014f5c32009-08-25 13:59:30 -07001818 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001819 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001820 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001821
1822 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001823 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001824 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001825 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001826 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001827 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001828 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001829 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001830 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001831
Chris Leech014f5c32009-08-25 13:59:30 -07001832 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001833 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001834 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1835 mod_timer(&port->timer, jiffies + 2);
1836 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001837out:
Chris Leech014f5c32009-08-25 13:59:30 -07001838 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001839 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001840 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001841 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001842}
1843
1844/**
Robert Love1875f272009-11-03 11:47:50 -08001845 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001846 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001847static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001848{
Robert Love85b4aa42008-12-09 15:10:24 -08001849 register_netdevice_notifier(&fcoe_notifier);
1850}
1851
1852/**
Robert Love1875f272009-11-03 11:47:50 -08001853 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001854 */
Robert Love85b4aa42008-12-09 15:10:24 -08001855static void fcoe_dev_cleanup(void)
1856{
1857 unregister_netdevice_notifier(&fcoe_notifier);
1858}
1859
1860/**
Robert Love1875f272009-11-03 11:47:50 -08001861 * fcoe_device_notification() - Handler for net device events
1862 * @notifier: The context of the notification
1863 * @event: The type of event
1864 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001865 *
Robert Love1875f272009-11-03 11:47:50 -08001866 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001867 *
1868 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001869 */
Robert Love85b4aa42008-12-09 15:10:24 -08001870static int fcoe_device_notification(struct notifier_block *notifier,
1871 ulong event, void *ptr)
1872{
Robert Love1875f272009-11-03 11:47:50 -08001873 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001874 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001875 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001876 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001877 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001878 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001879 u32 mfs;
1880 int rc = NOTIFY_OK;
1881
Chris Leech014f5c32009-08-25 13:59:30 -07001882 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001883 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001884 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001885 break;
1886 }
1887 }
Robert Love1875f272009-11-03 11:47:50 -08001888 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001889 rc = NOTIFY_DONE;
1890 goto out;
1891 }
1892
Robert Love85b4aa42008-12-09 15:10:24 -08001893 switch (event) {
1894 case NETDEV_DOWN:
1895 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001896 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001897 break;
1898 case NETDEV_UP:
1899 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001900 break;
1901 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001902 if (netdev->features & NETIF_F_FCOE_MTU)
1903 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001904 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1905 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001906 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001907 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001908 break;
1909 case NETDEV_REGISTER:
1910 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001911 case NETDEV_UNREGISTER:
1912 list_del(&fcoe->list);
1913 port = lport_priv(fcoe->ctlr.lp);
1914 fcoe_interface_cleanup(fcoe);
1915 schedule_work(&port->destroy_work);
1916 goto out;
1917 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001918 case NETDEV_FEAT_CHANGE:
1919 fcoe_netdev_features_change(lport, netdev);
1920 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001921 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001922 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001923 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001924 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001925
1926 fcoe_link_speed_update(lport);
1927
Robert Love1875f272009-11-03 11:47:50 -08001928 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001929 fcoe_ctlr_link_up(&fcoe->ctlr);
1930 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001931 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001932 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001933 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001934 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001935 }
1936out:
1937 return rc;
1938}
1939
1940/**
Robert Love1875f272009-11-03 11:47:50 -08001941 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
1942 * @buffer: The name of the net device
Robert Love85b4aa42008-12-09 15:10:24 -08001943 *
Robert Love1875f272009-11-03 11:47:50 -08001944 * Returns: NULL or a ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001945 */
Robert Love85b4aa42008-12-09 15:10:24 -08001946static struct net_device *fcoe_if_to_netdev(const char *buffer)
1947{
1948 char *cp;
1949 char ifname[IFNAMSIZ + 2];
1950
1951 if (buffer) {
1952 strlcpy(ifname, buffer, IFNAMSIZ);
1953 cp = ifname + strlen(ifname);
1954 while (--cp >= ifname && *cp == '\n')
1955 *cp = '\0';
1956 return dev_get_by_name(&init_net, ifname);
1957 }
1958 return NULL;
1959}
1960
1961/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001962 * fcoe_disable() - Disables a FCoE interface
1963 * @buffer: The name of the Ethernet interface to be disabled
1964 * @kp: The associated kernel parameter
1965 *
1966 * Called from sysfs.
1967 *
1968 * Returns: 0 for success
1969 */
1970static int fcoe_disable(const char *buffer, struct kernel_param *kp)
1971{
1972 struct fcoe_interface *fcoe;
1973 struct net_device *netdev;
1974 int rc = 0;
1975
1976 mutex_lock(&fcoe_config_mutex);
1977#ifdef CONFIG_FCOE_MODULE
1978 /*
1979 * Make sure the module has been initialized, and is not about to be
1980 * removed. Module paramter sysfs files are writable before the
1981 * module_init function is called and after module_exit.
1982 */
1983 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1984 rc = -ENODEV;
1985 goto out_nodev;
1986 }
1987#endif
1988
1989 netdev = fcoe_if_to_netdev(buffer);
1990 if (!netdev) {
1991 rc = -ENODEV;
1992 goto out_nodev;
1993 }
1994
Vasu Dev34ce27b2010-05-07 15:18:46 -07001995 if (!rtnl_trylock()) {
1996 dev_put(netdev);
1997 mutex_unlock(&fcoe_config_mutex);
1998 return restart_syscall();
1999 }
2000
Vasu Dev55a66d32009-12-10 09:59:31 -08002001 fcoe = fcoe_hostlist_lookup_port(netdev);
2002 rtnl_unlock();
2003
Chris Leech9ee50e42010-04-09 14:22:23 -07002004 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07002005 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002006 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002007 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002008 rc = -ENODEV;
2009
2010 dev_put(netdev);
2011out_nodev:
2012 mutex_unlock(&fcoe_config_mutex);
2013 return rc;
2014}
2015
2016/**
2017 * fcoe_enable() - Enables a FCoE interface
2018 * @buffer: The name of the Ethernet interface to be enabled
2019 * @kp: The associated kernel parameter
2020 *
2021 * Called from sysfs.
2022 *
2023 * Returns: 0 for success
2024 */
2025static int fcoe_enable(const char *buffer, struct kernel_param *kp)
2026{
2027 struct fcoe_interface *fcoe;
2028 struct net_device *netdev;
2029 int rc = 0;
2030
2031 mutex_lock(&fcoe_config_mutex);
2032#ifdef CONFIG_FCOE_MODULE
2033 /*
2034 * Make sure the module has been initialized, and is not about to be
2035 * removed. Module paramter sysfs files are writable before the
2036 * module_init function is called and after module_exit.
2037 */
2038 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2039 rc = -ENODEV;
2040 goto out_nodev;
2041 }
2042#endif
2043
2044 netdev = fcoe_if_to_netdev(buffer);
2045 if (!netdev) {
2046 rc = -ENODEV;
2047 goto out_nodev;
2048 }
2049
Vasu Dev34ce27b2010-05-07 15:18:46 -07002050 if (!rtnl_trylock()) {
2051 dev_put(netdev);
2052 mutex_unlock(&fcoe_config_mutex);
2053 return restart_syscall();
2054 }
2055
Vasu Dev55a66d32009-12-10 09:59:31 -08002056 fcoe = fcoe_hostlist_lookup_port(netdev);
2057 rtnl_unlock();
2058
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002059 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002060 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002061 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2062 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002063
2064 dev_put(netdev);
2065out_nodev:
2066 mutex_unlock(&fcoe_config_mutex);
2067 return rc;
2068}
2069
2070/**
Robert Love1875f272009-11-03 11:47:50 -08002071 * fcoe_destroy() - Destroy a FCoE interface
2072 * @buffer: The name of the Ethernet interface to be destroyed
2073 * @kp: The associated kernel parameter
2074 *
2075 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002076 *
2077 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002078 */
Robert Love85b4aa42008-12-09 15:10:24 -08002079static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
2080{
Chris Leech030f4e02009-08-25 14:00:02 -07002081 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002082 struct net_device *netdev;
Mike Christie8eca355f2009-10-21 16:27:44 -07002083 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002084
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002085 mutex_lock(&fcoe_config_mutex);
2086#ifdef CONFIG_FCOE_MODULE
2087 /*
2088 * Make sure the module has been initialized, and is not about to be
2089 * removed. Module paramter sysfs files are writable before the
2090 * module_init function is called and after module_exit.
2091 */
2092 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2093 rc = -ENODEV;
2094 goto out_nodev;
2095 }
2096#endif
2097
Robert Love85b4aa42008-12-09 15:10:24 -08002098 netdev = fcoe_if_to_netdev(buffer);
2099 if (!netdev) {
2100 rc = -ENODEV;
2101 goto out_nodev;
2102 }
Chris Leech2e70e242009-08-25 14:00:23 -07002103
Vasu Dev34ce27b2010-05-07 15:18:46 -07002104 if (!rtnl_trylock()) {
2105 dev_put(netdev);
2106 mutex_unlock(&fcoe_config_mutex);
2107 return restart_syscall();
2108 }
2109
Chris Leech2e70e242009-08-25 14:00:23 -07002110 fcoe = fcoe_hostlist_lookup_port(netdev);
2111 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07002112 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002113 rc = -ENODEV;
2114 goto out_putdev;
2115 }
Chris Leech2e70e242009-08-25 14:00:23 -07002116 fcoe_interface_cleanup(fcoe);
Yi Zou54a5b212010-07-20 15:21:17 -07002117 list_del(&fcoe->list);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002118 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002119 fcoe_if_destroy(fcoe->ctlr.lp);
Rob Love6409ea652010-01-21 10:16:05 -08002120
Robert Love85b4aa42008-12-09 15:10:24 -08002121out_putdev:
2122 dev_put(netdev);
2123out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002124 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002125 return rc;
2126}
2127
Robert Love1875f272009-11-03 11:47:50 -08002128/**
2129 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2130 * @work: Handle to the FCoE port to be destroyed
2131 */
Chris Leech2e70e242009-08-25 14:00:23 -07002132static void fcoe_destroy_work(struct work_struct *work)
2133{
2134 struct fcoe_port *port;
2135
2136 port = container_of(work, struct fcoe_port, destroy_work);
2137 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002138 rtnl_lock();
2139 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002140 fcoe_if_destroy(port->lport);
2141 mutex_unlock(&fcoe_config_mutex);
2142}
2143
Robert Love85b4aa42008-12-09 15:10:24 -08002144/**
Robert Love1875f272009-11-03 11:47:50 -08002145 * fcoe_create() - Create a fcoe interface
2146 * @buffer: The name of the Ethernet interface to create on
2147 * @kp: The associated kernel param
2148 *
2149 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002150 *
2151 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002152 */
Robert Love85b4aa42008-12-09 15:10:24 -08002153static int fcoe_create(const char *buffer, struct kernel_param *kp)
2154{
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002155 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
Robert Love85b4aa42008-12-09 15:10:24 -08002156 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07002157 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002158 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002159 struct net_device *netdev;
2160
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002161 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002162
2163 if (!rtnl_trylock()) {
2164 mutex_unlock(&fcoe_config_mutex);
2165 return restart_syscall();
2166 }
2167
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002168#ifdef CONFIG_FCOE_MODULE
2169 /*
2170 * Make sure the module has been initialized, and is not about to be
2171 * removed. Module paramter sysfs files are writable before the
2172 * module_init function is called and after module_exit.
2173 */
2174 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2175 rc = -ENODEV;
Robert Love7287fb92011-01-28 16:03:47 -08002176 goto out_nodev;
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002177 }
2178#endif
2179
Robert Love85b4aa42008-12-09 15:10:24 -08002180 netdev = fcoe_if_to_netdev(buffer);
2181 if (!netdev) {
2182 rc = -ENODEV;
2183 goto out_nodev;
2184 }
Chris Leech2e70e242009-08-25 14:00:23 -07002185
Robert Love85b4aa42008-12-09 15:10:24 -08002186 /* look for existing lport */
2187 if (fcoe_hostlist_lookup(netdev)) {
2188 rc = -EEXIST;
2189 goto out_putdev;
2190 }
Robert Love85b4aa42008-12-09 15:10:24 -08002191
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002192 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002193 if (IS_ERR(fcoe)) {
2194 rc = PTR_ERR(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002195 goto out_putdev;
2196 }
2197
Chris Leech9a057532009-11-03 11:46:40 -08002198 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002199 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002200 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002201 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002202 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002203 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002204 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002205 }
Chris Leech030f4e02009-08-25 14:00:02 -07002206
Chris Leech54b649f2009-08-25 14:00:07 -07002207 /* Make this the "master" N_Port */
2208 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002209
Chris Leechc863df32009-08-25 14:00:18 -07002210 /* add to lports list */
2211 fcoe_hostlist_add(lport);
2212
Chris Leech54b649f2009-08-25 14:00:07 -07002213 /* start FIP Discovery and FLOGI */
2214 lport->boot_time = jiffies;
2215 fc_fabric_login(lport);
2216 if (!fcoe_link_ok(lport))
2217 fcoe_ctlr_link_up(&fcoe->ctlr);
2218
Chris Leech54b649f2009-08-25 14:00:07 -07002219 /*
2220 * Release from init in fcoe_interface_create(), on success lport
2221 * should be holding a reference taken in fcoe_if_create().
2222 */
Chris Leech030f4e02009-08-25 14:00:02 -07002223 fcoe_interface_put(fcoe);
Rob Love6409ea652010-01-21 10:16:05 -08002224 dev_put(netdev);
2225 rtnl_unlock();
2226 mutex_unlock(&fcoe_config_mutex);
2227
2228 return 0;
2229out_free:
2230 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002231out_putdev:
2232 dev_put(netdev);
2233out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002234 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002235 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002236 return rc;
2237}
2238
Robert Love34f42a02009-02-27 10:55:45 -08002239/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002240 * fcoe_link_speed_update() - Update the supported and actual link speeds
2241 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002242 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002243 * Returns: 0 if the ethtool query was successful
2244 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002245 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002246int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002247{
Robert Love1875f272009-11-03 11:47:50 -08002248 struct fcoe_port *port = lport_priv(lport);
2249 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08002250 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002251
Robert Love5e4f8fe2010-05-07 15:18:35 -07002252 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002253 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002254 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2255 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2256 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002257 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002258 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002259 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002260 FC_PORTSPEED_10GBIT;
2261 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002262 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002263 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002264 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002265
Yi Zou2f718d62009-07-29 17:04:01 -07002266 return 0;
2267 }
2268 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002269}
Robert Love85b4aa42008-12-09 15:10:24 -08002270
Robert Love34f42a02009-02-27 10:55:45 -08002271/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002272 * fcoe_link_ok() - Check if the link is OK for a local port
2273 * @lport: The local port to check link on
2274 *
2275 * Returns: 0 if link is UP and OK, -1 if not
2276 *
2277 */
2278int fcoe_link_ok(struct fc_lport *lport)
2279{
2280 struct fcoe_port *port = lport_priv(lport);
2281 struct net_device *netdev = port->fcoe->netdev;
2282
2283 if (netif_oper_up(netdev))
2284 return 0;
2285 return -1;
2286}
2287
2288/**
Robert Love1875f272009-11-03 11:47:50 -08002289 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2290 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002291 *
2292 * Must be called with fcoe_create_mutex held to single-thread completion.
2293 *
2294 * This flushes the pending skbs by adding a new skb to each queue and
2295 * waiting until they are all freed. This assures us that not only are
2296 * there no packets that will be handled by the lport, but also that any
2297 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002298 */
Robert Love1875f272009-11-03 11:47:50 -08002299void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002300{
Robert Love85b4aa42008-12-09 15:10:24 -08002301 struct fcoe_percpu_s *pp;
2302 struct fcoe_rcv_info *fr;
2303 struct sk_buff_head *list;
2304 struct sk_buff *skb, *next;
2305 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002306 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002307
Robert Love5e5e92d2009-03-17 11:41:35 -07002308 for_each_possible_cpu(cpu) {
2309 pp = &per_cpu(fcoe_percpu, cpu);
2310 spin_lock_bh(&pp->fcoe_rx_list.lock);
2311 list = &pp->fcoe_rx_list;
2312 head = list->next;
2313 for (skb = head; skb != (struct sk_buff *)list;
2314 skb = next) {
2315 next = skb->next;
2316 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002317 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002318 __skb_unlink(skb, list);
2319 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002320 }
Robert Love85b4aa42008-12-09 15:10:24 -08002321 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002322
2323 if (!pp->thread || !cpu_online(cpu)) {
2324 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2325 continue;
2326 }
2327
2328 skb = dev_alloc_skb(0);
2329 if (!skb) {
2330 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2331 continue;
2332 }
2333 skb->destructor = fcoe_percpu_flush_done;
2334
2335 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2336 if (pp->fcoe_rx_list.qlen == 1)
2337 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002338 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002339
2340 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002341 }
2342}
Robert Love85b4aa42008-12-09 15:10:24 -08002343
2344/**
Robert Love34f42a02009-02-27 10:55:45 -08002345 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002346 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002347 */
Robert Love1875f272009-11-03 11:47:50 -08002348void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002349{
Robert Love1875f272009-11-03 11:47:50 -08002350 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002351 struct sk_buff *skb;
2352
Chris Leech014f5c32009-08-25 13:59:30 -07002353 spin_lock_bh(&port->fcoe_pending_queue.lock);
2354 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2355 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002356 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002357 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002358 }
Chris Leech014f5c32009-08-25 13:59:30 -07002359 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002360}
Robert Love85b4aa42008-12-09 15:10:24 -08002361
2362/**
Robert Love1875f272009-11-03 11:47:50 -08002363 * fcoe_reset() - Reset a local port
2364 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002365 *
Robert Love1875f272009-11-03 11:47:50 -08002366 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002367 */
2368int fcoe_reset(struct Scsi_Host *shost)
2369{
2370 struct fc_lport *lport = shost_priv(shost);
2371 fc_lport_reset(lport);
2372 return 0;
2373}
Robert Love85b4aa42008-12-09 15:10:24 -08002374
Robert Love34f42a02009-02-27 10:55:45 -08002375/**
Robert Love1875f272009-11-03 11:47:50 -08002376 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2377 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002378 *
Robert Love1875f272009-11-03 11:47:50 -08002379 * Locking: Must be called with the RNL mutex held.
2380 *
2381 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002382 */
Chris Leech014f5c32009-08-25 13:59:30 -07002383static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002384fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002385{
Chris Leech014f5c32009-08-25 13:59:30 -07002386 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002387
Chris Leech014f5c32009-08-25 13:59:30 -07002388 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002389 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002390 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002391 }
Robert Love85b4aa42008-12-09 15:10:24 -08002392 return NULL;
2393}
2394
Robert Love34f42a02009-02-27 10:55:45 -08002395/**
Robert Love1875f272009-11-03 11:47:50 -08002396 * fcoe_hostlist_lookup() - Find the local port associated with a
2397 * given net device
2398 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002399 *
Robert Love1875f272009-11-03 11:47:50 -08002400 * Locking: Must be called with the RTNL mutex held
2401 *
2402 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002403 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002404static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002405{
Chris Leech014f5c32009-08-25 13:59:30 -07002406 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002407
Chris Leech014f5c32009-08-25 13:59:30 -07002408 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002409 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002410}
Robert Love85b4aa42008-12-09 15:10:24 -08002411
Robert Love34f42a02009-02-27 10:55:45 -08002412/**
Robert Love1875f272009-11-03 11:47:50 -08002413 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2414 * port to the hostlist
2415 * @lport: The local port that identifies the FCoE interface to be added
2416 *
2417 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002418 *
2419 * Returns: 0 for success
2420 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002421static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002422{
Chris Leech014f5c32009-08-25 13:59:30 -07002423 struct fcoe_interface *fcoe;
2424 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002425
Chris Leech014f5c32009-08-25 13:59:30 -07002426 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2427 if (!fcoe) {
2428 port = lport_priv(lport);
2429 fcoe = port->fcoe;
2430 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002431 }
2432 return 0;
2433}
Robert Love85b4aa42008-12-09 15:10:24 -08002434
Robert Love34f42a02009-02-27 10:55:45 -08002435/**
Robert Love1875f272009-11-03 11:47:50 -08002436 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002437 *
Robert Love1875f272009-11-03 11:47:50 -08002438 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002439 */
Robert Love85b4aa42008-12-09 15:10:24 -08002440static int __init fcoe_init(void)
2441{
Robert Love1875f272009-11-03 11:47:50 -08002442 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002443 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002444 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002445
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002446 mutex_lock(&fcoe_config_mutex);
2447
Robert Love38eccab2009-03-17 11:41:30 -07002448 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002449 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002450 skb_queue_head_init(&p->fcoe_rx_list);
2451 }
2452
Robert Love8976f422009-03-17 11:41:46 -07002453 for_each_online_cpu(cpu)
2454 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002455
Robert Love8976f422009-03-17 11:41:46 -07002456 /* Initialize per CPU interrupt thread */
2457 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2458 if (rc)
2459 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002460
Robert Love8976f422009-03-17 11:41:46 -07002461 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002462 fcoe_dev_setup();
2463
Chris Leech5892c322009-08-25 13:59:14 -07002464 rc = fcoe_if_init();
2465 if (rc)
2466 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002467
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002468 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002469 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002470
2471out_free:
2472 for_each_online_cpu(cpu) {
2473 fcoe_percpu_thread_destroy(cpu);
2474 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002475 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002476 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002477}
2478module_init(fcoe_init);
2479
2480/**
Robert Love1875f272009-11-03 11:47:50 -08002481 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002482 *
Robert Love1875f272009-11-03 11:47:50 -08002483 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002484 */
Robert Love85b4aa42008-12-09 15:10:24 -08002485static void __exit fcoe_exit(void)
2486{
Chris Leech014f5c32009-08-25 13:59:30 -07002487 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002488 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002489 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002490
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002491 mutex_lock(&fcoe_config_mutex);
2492
Robert Love85b4aa42008-12-09 15:10:24 -08002493 fcoe_dev_cleanup();
2494
Vasu Dev5919a592009-03-27 09:03:29 -07002495 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002496 rtnl_lock();
2497 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002498 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002499 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002500 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002501 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002502 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002503 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002504
Robert Love8976f422009-03-17 11:41:46 -07002505 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2506
Chris Leech014f5c32009-08-25 13:59:30 -07002507 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002508 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002509
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002510 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002511
2512 /* flush any asyncronous interface destroys,
2513 * this should happen after the netdev notifier is unregistered */
2514 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002515 /* That will flush out all the N_Ports on the hostlist, but now we
2516 * may have NPIV VN_Ports scheduled for destruction */
2517 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002518
2519 /* detach from scsi transport
2520 * must happen after all destroys are done, therefor after the flush */
2521 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002522}
2523module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002524
2525/**
2526 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2527 * @seq: active sequence in the FLOGI or FDISC exchange
2528 * @fp: response frame, or error encoded in a pointer (timeout)
2529 * @arg: pointer the the fcoe_ctlr structure
2530 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002531 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002532 * the libfc FLOGI response handler.
2533 */
2534static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2535{
2536 struct fcoe_ctlr *fip = arg;
2537 struct fc_exch *exch = fc_seq_exch(seq);
2538 struct fc_lport *lport = exch->lp;
2539 u8 *mac;
2540
2541 if (IS_ERR(fp))
2542 goto done;
2543
2544 mac = fr_cb(fp)->granted_mac;
2545 if (is_zero_ether_addr(mac)) {
2546 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002547 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002548 fc_frame_free(fp);
2549 return;
2550 }
Chris Leech11b56182009-11-03 11:46:29 -08002551 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002552 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002553done:
2554 fc_lport_flogi_resp(seq, fp, lport);
2555}
2556
2557/**
2558 * fcoe_logo_resp() - FCoE specific LOGO response handler
2559 * @seq: active sequence in the LOGO exchange
2560 * @fp: response frame, or error encoded in a pointer (timeout)
2561 * @arg: pointer the the fcoe_ctlr structure
2562 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002563 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002564 * the libfc LOGO response handler.
2565 */
2566static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2567{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002568 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002569 static u8 zero_mac[ETH_ALEN] = { 0 };
2570
2571 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002572 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002573 fc_lport_logo_resp(seq, fp, lport);
2574}
2575
2576/**
2577 * fcoe_elsct_send - FCoE specific ELS handler
2578 *
2579 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2580 * using FCoE specific response handlers and passing the FIP controller as
2581 * the argument (the lport is still available from the exchange).
2582 *
2583 * Most of the work here is just handed off to the libfc routine.
2584 */
Robert Love1875f272009-11-03 11:47:50 -08002585static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2586 struct fc_frame *fp, unsigned int op,
2587 void (*resp)(struct fc_seq *,
2588 struct fc_frame *,
2589 void *),
2590 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002591{
2592 struct fcoe_port *port = lport_priv(lport);
2593 struct fcoe_interface *fcoe = port->fcoe;
2594 struct fcoe_ctlr *fip = &fcoe->ctlr;
2595 struct fc_frame_header *fh = fc_frame_header_get(fp);
2596
2597 switch (op) {
2598 case ELS_FLOGI:
2599 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002600 if (lport->point_to_multipoint)
2601 break;
Chris Leech11b56182009-11-03 11:46:29 -08002602 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2603 fip, timeout);
2604 case ELS_LOGO:
2605 /* only hook onto fabric logouts, not port logouts */
2606 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2607 break;
2608 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002609 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002610 }
2611 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2612}
2613
Chris Leech9a057532009-11-03 11:46:40 -08002614/**
2615 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2616 * @vport: fc_vport object to create a new fc_host for
2617 * @disabled: start the new fc_host in a disabled state by default?
2618 *
2619 * Returns: 0 for success
2620 */
2621static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2622{
2623 struct Scsi_Host *shost = vport_to_shost(vport);
2624 struct fc_lport *n_port = shost_priv(shost);
2625 struct fcoe_port *port = lport_priv(n_port);
2626 struct fcoe_interface *fcoe = port->fcoe;
2627 struct net_device *netdev = fcoe->netdev;
2628 struct fc_lport *vn_port;
2629
2630 mutex_lock(&fcoe_config_mutex);
2631 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2632 mutex_unlock(&fcoe_config_mutex);
2633
2634 if (IS_ERR(vn_port)) {
2635 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2636 netdev->name);
2637 return -EIO;
2638 }
2639
2640 if (disabled) {
2641 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2642 } else {
2643 vn_port->boot_time = jiffies;
2644 fc_fabric_login(vn_port);
2645 fc_vport_setlink(vn_port);
2646 }
2647 return 0;
2648}
2649
2650/**
2651 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2652 * @vport: fc_vport object that is being destroyed
2653 *
2654 * Returns: 0 for success
2655 */
2656static int fcoe_vport_destroy(struct fc_vport *vport)
2657{
2658 struct Scsi_Host *shost = vport_to_shost(vport);
2659 struct fc_lport *n_port = shost_priv(shost);
2660 struct fc_lport *vn_port = vport->dd_data;
2661 struct fcoe_port *port = lport_priv(vn_port);
2662
2663 mutex_lock(&n_port->lp_mutex);
2664 list_del(&vn_port->list);
2665 mutex_unlock(&n_port->lp_mutex);
2666 schedule_work(&port->destroy_work);
2667 return 0;
2668}
2669
2670/**
2671 * fcoe_vport_disable() - change vport state
2672 * @vport: vport to bring online/offline
2673 * @disable: should the vport be disabled?
2674 */
2675static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2676{
2677 struct fc_lport *lport = vport->dd_data;
2678
2679 if (disable) {
2680 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2681 fc_fabric_logoff(lport);
2682 } else {
2683 lport->boot_time = jiffies;
2684 fc_fabric_login(lport);
2685 fc_vport_setlink(lport);
2686 }
2687
2688 return 0;
2689}
2690
Chris Leechdc8596d2009-11-03 11:47:18 -08002691/**
2692 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2693 * @vport: fc_vport with a new symbolic name string
2694 *
2695 * After generating a new symbolic name string, a new RSPN_ID request is
2696 * sent to the name server. There is no response handler, so if it fails
2697 * for some reason it will not be retried.
2698 */
2699static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2700{
2701 struct fc_lport *lport = vport->dd_data;
2702 struct fc_frame *fp;
2703 size_t len;
2704
2705 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2706 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2707 fcoe_netdev(lport)->name, vport->symbolic_name);
2708
2709 if (lport->state != LPORT_ST_READY)
2710 return;
2711
2712 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2713 fp = fc_frame_alloc(lport,
2714 sizeof(struct fc_ct_hdr) +
2715 sizeof(struct fc_ns_rspn) + len);
2716 if (!fp)
2717 return;
2718 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002719 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002720}
Yi Zoub84056b2009-11-20 14:55:19 -08002721
2722/**
2723 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2724 * @lport: the local port
2725 * @fc_lesb: the link error status block
2726 */
2727static void fcoe_get_lesb(struct fc_lport *lport,
2728 struct fc_els_lesb *fc_lesb)
2729{
2730 unsigned int cpu;
2731 u32 lfc, vlfc, mdac;
2732 struct fcoe_dev_stats *devst;
2733 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002734 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002735 struct net_device *netdev = fcoe_netdev(lport);
2736
2737 lfc = 0;
2738 vlfc = 0;
2739 mdac = 0;
2740 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2741 memset(lesb, 0, sizeof(*lesb));
2742 for_each_possible_cpu(cpu) {
2743 devst = per_cpu_ptr(lport->dev_stats, cpu);
2744 lfc += devst->LinkFailureCount;
2745 vlfc += devst->VLinkFailureCount;
2746 mdac += devst->MissDiscAdvCount;
2747 }
2748 lesb->lesb_link_fail = htonl(lfc);
2749 lesb->lesb_vlink_fail = htonl(vlfc);
2750 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002751 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002752}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002753
2754/**
2755 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2756 * @lport: the local port
2757 * @port_id: the port ID
2758 * @fp: the received frame, if any, that caused the port_id to be set.
2759 *
2760 * This routine handles the case where we received a FLOGI and are
2761 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2762 * so it can set the non-mapped mode and gateway address.
2763 *
2764 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2765 */
2766static void fcoe_set_port_id(struct fc_lport *lport,
2767 u32 port_id, struct fc_frame *fp)
2768{
2769 struct fcoe_port *port = lport_priv(lport);
2770 struct fcoe_interface *fcoe = port->fcoe;
2771
2772 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2773 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2774}