blob: 6d6c8bf5d155cabeacf441f26adb712d63331f5c [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
Yi Zou8ca86f82011-01-28 16:05:11 -0800148static struct scsi_transport_template *fcoe_nport_scsi_transport;
149static struct scsi_transport_template *fcoe_vport_scsi_transport;
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
Yi Zou8ca86f82011-01-28 16:05:11 -0800166struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700167 .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
Yi Zou8ca86f82011-01-28 16:05:11 -0800206struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800207 .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)
Yi Zou8ca86f82011-01-28 16:05:11 -0800726 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800727 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800728 lport->host->transportt = fcoe_nport_scsi_transport;
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 */
Yi Zou8ca86f82011-01-28 16:05:11 -08001067 fcoe_nport_scsi_transport =
1068 fc_attach_transport(&fcoe_nport_fc_functions);
1069 fcoe_vport_scsi_transport =
1070 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001071
Yi Zou8ca86f82011-01-28 16:05:11 -08001072 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001073 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001074 return -ENODEV;
1075 }
1076
1077 return 0;
1078}
1079
1080/**
Robert Love1875f272009-11-03 11:47:50 -08001081 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001082 *
Robert Love1875f272009-11-03 11:47:50 -08001083 * Detaches the SW FCoE transport from the FC transport
1084 *
1085 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001086 */
1087int __exit fcoe_if_exit(void)
1088{
Yi Zou8ca86f82011-01-28 16:05:11 -08001089 fc_release_transport(fcoe_nport_scsi_transport);
1090 fc_release_transport(fcoe_vport_scsi_transport);
1091 fcoe_nport_scsi_transport = NULL;
1092 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001093 return 0;
1094}
1095
Robert Love85b4aa42008-12-09 15:10:24 -08001096/**
Robert Love1875f272009-11-03 11:47:50 -08001097 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1098 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001099 */
1100static void fcoe_percpu_thread_create(unsigned int cpu)
1101{
1102 struct fcoe_percpu_s *p;
1103 struct task_struct *thread;
1104
1105 p = &per_cpu(fcoe_percpu, cpu);
1106
1107 thread = kthread_create(fcoe_percpu_receive_thread,
1108 (void *)p, "fcoethread/%d", cpu);
1109
Joe Eykholte7a51992009-08-25 14:04:08 -07001110 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001111 kthread_bind(thread, cpu);
1112 wake_up_process(thread);
1113
1114 spin_lock_bh(&p->fcoe_rx_list.lock);
1115 p->thread = thread;
1116 spin_unlock_bh(&p->fcoe_rx_list.lock);
1117 }
1118}
1119
1120/**
Robert Love1875f272009-11-03 11:47:50 -08001121 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1122 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001123 *
1124 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1125 * current CPU's Rx thread. If the thread being destroyed is bound to
1126 * the CPU processing this context the skbs will be freed.
1127 */
1128static void fcoe_percpu_thread_destroy(unsigned int cpu)
1129{
1130 struct fcoe_percpu_s *p;
1131 struct task_struct *thread;
1132 struct page *crc_eof;
1133 struct sk_buff *skb;
1134#ifdef CONFIG_SMP
1135 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001136 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001137#endif /* CONFIG_SMP */
1138
Robert Loved5488eb2009-06-10 15:30:59 -07001139 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001140
1141 /* Prevent any new skbs from being queued for this CPU. */
1142 p = &per_cpu(fcoe_percpu, cpu);
1143 spin_lock_bh(&p->fcoe_rx_list.lock);
1144 thread = p->thread;
1145 p->thread = NULL;
1146 crc_eof = p->crc_eof_page;
1147 p->crc_eof_page = NULL;
1148 p->crc_eof_offset = 0;
1149 spin_unlock_bh(&p->fcoe_rx_list.lock);
1150
1151#ifdef CONFIG_SMP
1152 /*
1153 * Don't bother moving the skb's if this context is running
1154 * on the same CPU that is having its thread destroyed. This
1155 * can easily happen when the module is removed.
1156 */
1157 if (cpu != targ_cpu) {
1158 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1159 spin_lock_bh(&p0->fcoe_rx_list.lock);
1160 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001161 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1162 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001163
1164 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1165 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1166 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1167 } else {
1168 /*
1169 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001170 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001171 * on the CPU that is going offline.
1172 */
1173 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1174 kfree_skb(skb);
1175 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1176 }
1177 } else {
1178 /*
1179 * This scenario occurs when the module is being removed
1180 * and all threads are being destroyed. skbs will continue
1181 * to be shifted from the CPU thread that is being removed
1182 * to the CPU thread associated with the CPU that is processing
1183 * the module removal. Once there is only one CPU Rx thread it
1184 * will reach this case and we will drop all skbs and later
1185 * stop the thread.
1186 */
1187 spin_lock_bh(&p->fcoe_rx_list.lock);
1188 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1189 kfree_skb(skb);
1190 spin_unlock_bh(&p->fcoe_rx_list.lock);
1191 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001192 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001193#else
1194 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001195 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001196 * being removed. Free all skbs and stop the thread.
1197 */
1198 spin_lock_bh(&p->fcoe_rx_list.lock);
1199 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1200 kfree_skb(skb);
1201 spin_unlock_bh(&p->fcoe_rx_list.lock);
1202#endif
1203
1204 if (thread)
1205 kthread_stop(thread);
1206
1207 if (crc_eof)
1208 put_page(crc_eof);
1209}
1210
1211/**
Robert Love1875f272009-11-03 11:47:50 -08001212 * fcoe_cpu_callback() - Handler for CPU hotplug events
1213 * @nfb: The callback data block
1214 * @action: The event triggering the callback
1215 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001216 *
Robert Love1875f272009-11-03 11:47:50 -08001217 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001218 *
1219 * Returns NOTIFY_OK always.
1220 */
1221static int fcoe_cpu_callback(struct notifier_block *nfb,
1222 unsigned long action, void *hcpu)
1223{
1224 unsigned cpu = (unsigned long)hcpu;
1225
1226 switch (action) {
1227 case CPU_ONLINE:
1228 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001229 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001230 fcoe_percpu_thread_create(cpu);
1231 break;
1232 case CPU_DEAD:
1233 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001234 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001235 fcoe_percpu_thread_destroy(cpu);
1236 break;
1237 default:
1238 break;
1239 }
1240 return NOTIFY_OK;
1241}
1242
Robert Love8976f422009-03-17 11:41:46 -07001243/**
Robert Love1875f272009-11-03 11:47:50 -08001244 * fcoe_rcv() - Receive packets from a net device
1245 * @skb: The received packet
1246 * @netdev: The net device that the packet was received on
1247 * @ptype: The packet type context
1248 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001249 *
Robert Love1875f272009-11-03 11:47:50 -08001250 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1251 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001252 *
1253 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001254 */
Robert Love1875f272009-11-03 11:47:50 -08001255int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001256 struct packet_type *ptype, struct net_device *olddev)
1257{
Robert Love1875f272009-11-03 11:47:50 -08001258 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001259 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001260 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001261 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001262 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001263 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001264 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001265
Chris Leech259ad852009-08-25 13:59:41 -07001266 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001267 lport = fcoe->ctlr.lp;
1268 if (unlikely(!lport)) {
1269 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001270 goto err2;
1271 }
Robert Love1875f272009-11-03 11:47:50 -08001272 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001273 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001274
Robert Love1875f272009-11-03 11:47:50 -08001275 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001276 "data:%p tail:%p end:%p sum:%d dev:%s",
1277 skb->len, skb->data_len, skb->head, skb->data,
1278 skb_tail_pointer(skb), skb_end_pointer(skb),
1279 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001280
Vasu Dev519e5132010-07-20 15:19:32 -07001281 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001282
1283 if (is_fip_mode(&fcoe->ctlr) &&
1284 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1285 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1286 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001287 goto err;
1288 }
1289
1290 /*
1291 * Check for minimum frame length, and make sure required FCoE
1292 * and FC headers are pulled into the linear data area.
1293 */
1294 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001295 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001296 goto err;
1297
1298 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1299 fh = (struct fc_frame_header *) skb_transport_header(skb);
1300
Robert Love0ee31cb2010-10-08 17:12:46 -07001301 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1302 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1303 eh->h_dest);
1304 goto err;
1305 }
1306
Robert Love85b4aa42008-12-09 15:10:24 -08001307 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001308 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001309 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001310
Robert Love85b4aa42008-12-09 15:10:24 -08001311 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001312 * In case the incoming frame's exchange is originated from
1313 * the initiator, then received frame's exchange id is ANDed
1314 * with fc_cpu_mask bits to get the same cpu on which exchange
1315 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001316 */
Vasu Devb2f00912009-08-25 13:58:53 -07001317 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1318 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1319 else
1320 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001321
Robert Love8976f422009-03-17 11:41:46 -07001322 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001323 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001324 if (unlikely(!fps->thread)) {
1325 /*
1326 * The targeted CPU is not ready, let's target
1327 * the first CPU now. For non-SMP systems this
1328 * will check the same CPU twice.
1329 */
Robert Love1875f272009-11-03 11:47:50 -08001330 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001331 "ready for incoming skb- using first online "
1332 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001333
1334 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001335 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001336 fps = &per_cpu(fcoe_percpu, cpu);
1337 spin_lock_bh(&fps->fcoe_rx_list.lock);
1338 if (!fps->thread) {
1339 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1340 goto err;
1341 }
1342 }
1343
1344 /*
1345 * We now have a valid CPU that we're targeting for
1346 * this skb. We also have this receive thread locked,
1347 * so we're free to queue skbs into it's queue.
1348 */
Robert Love85b4aa42008-12-09 15:10:24 -08001349
Chris Leech859b7b62009-11-20 14:54:47 -08001350 /* If this is a SCSI-FCP frame, and this is already executing on the
1351 * correct CPU, and the queue for this CPU is empty, then go ahead
1352 * and process the frame directly in the softirq context.
1353 * This lets us process completions without context switching from the
1354 * NET_RX softirq, to our receive processing thread, and then back to
1355 * BLOCK softirq context.
1356 */
1357 if (fh->fh_type == FC_TYPE_FCP &&
1358 cpu == smp_processor_id() &&
1359 skb_queue_empty(&fps->fcoe_rx_list)) {
1360 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1361 fcoe_recv_frame(skb);
1362 } else {
1363 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1364 if (fps->fcoe_rx_list.qlen == 1)
1365 wake_up_process(fps->thread);
1366 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1367 }
Robert Love85b4aa42008-12-09 15:10:24 -08001368
1369 return 0;
1370err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001371 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1372 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001373err2:
1374 kfree_skb(skb);
1375 return -1;
1376}
Robert Love85b4aa42008-12-09 15:10:24 -08001377
1378/**
Robert Love1875f272009-11-03 11:47:50 -08001379 * fcoe_start_io() - Start FCoE I/O
1380 * @skb: The packet to be transmitted
1381 *
1382 * This routine is called from the net device to start transmitting
1383 * FCoE packets.
Robert Love85b4aa42008-12-09 15:10:24 -08001384 *
1385 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001386 */
Robert Love85b4aa42008-12-09 15:10:24 -08001387static inline int fcoe_start_io(struct sk_buff *skb)
1388{
Chris Leech18fa11e2009-11-03 11:50:05 -08001389 struct sk_buff *nskb;
Robert Love85b4aa42008-12-09 15:10:24 -08001390 int rc;
1391
Chris Leech18fa11e2009-11-03 11:50:05 -08001392 nskb = skb_clone(skb, GFP_ATOMIC);
1393 rc = dev_queue_xmit(nskb);
Robert Love85b4aa42008-12-09 15:10:24 -08001394 if (rc != 0)
1395 return rc;
1396 kfree_skb(skb);
1397 return 0;
1398}
1399
1400/**
Robert Love1875f272009-11-03 11:47:50 -08001401 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1402 * @skb: The packet to be transmitted
1403 * @tlen: The total length of the trailer
1404 *
1405 * This routine allocates a page for frame trailers. The page is re-used if
1406 * there is enough room left on it for the current trailer. If there isn't
1407 * enough buffer left a new page is allocated for the trailer. Reference to
1408 * the page from this function as well as the skbs using the page fragments
1409 * ensure that the page is freed at the appropriate time.
Robert Love85b4aa42008-12-09 15:10:24 -08001410 *
1411 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001412 */
Robert Love85b4aa42008-12-09 15:10:24 -08001413static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1414{
1415 struct fcoe_percpu_s *fps;
1416 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001417
Robert Love5e5e92d2009-03-17 11:41:35 -07001418 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001419 page = fps->crc_eof_page;
1420 if (!page) {
1421 page = alloc_page(GFP_ATOMIC);
1422 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001423 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001424 return -ENOMEM;
1425 }
1426 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001427 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001428 }
1429
1430 get_page(page);
1431 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1432 fps->crc_eof_offset, tlen);
1433 skb->len += tlen;
1434 skb->data_len += tlen;
1435 skb->truesize += tlen;
1436 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1437
1438 if (fps->crc_eof_offset >= PAGE_SIZE) {
1439 fps->crc_eof_page = NULL;
1440 fps->crc_eof_offset = 0;
1441 put_page(page);
1442 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001443 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001444 return 0;
1445}
1446
1447/**
Robert Love1875f272009-11-03 11:47:50 -08001448 * fcoe_fc_crc() - Calculates the CRC for a given frame
1449 * @fp: The frame to be checksumed
Robert Love85b4aa42008-12-09 15:10:24 -08001450 *
Robert Love1875f272009-11-03 11:47:50 -08001451 * This uses crc32() routine to calculate the CRC for a frame
1452 *
1453 * Return: The 32 bit CRC value
Robert Love34f42a02009-02-27 10:55:45 -08001454 */
Robert Love85b4aa42008-12-09 15:10:24 -08001455u32 fcoe_fc_crc(struct fc_frame *fp)
1456{
1457 struct sk_buff *skb = fp_skb(fp);
1458 struct skb_frag_struct *frag;
1459 unsigned char *data;
1460 unsigned long off, len, clen;
1461 u32 crc;
1462 unsigned i;
1463
1464 crc = crc32(~0, skb->data, skb_headlen(skb));
1465
1466 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1467 frag = &skb_shinfo(skb)->frags[i];
1468 off = frag->page_offset;
1469 len = frag->size;
1470 while (len > 0) {
1471 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1472 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1473 KM_SKB_DATA_SOFTIRQ);
1474 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1475 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1476 off += clen;
1477 len -= clen;
1478 }
1479 }
1480 return crc;
1481}
Robert Love85b4aa42008-12-09 15:10:24 -08001482
1483/**
Robert Love1875f272009-11-03 11:47:50 -08001484 * fcoe_xmit() - Transmit a FCoE frame
1485 * @lport: The local port that the frame is to be transmitted for
1486 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001487 *
Robert Love1875f272009-11-03 11:47:50 -08001488 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001489 */
Robert Love1875f272009-11-03 11:47:50 -08001490int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001491{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001492 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001493 u32 crc;
1494 struct ethhdr *eh;
1495 struct fcoe_crc_eof *cp;
1496 struct sk_buff *skb;
1497 struct fcoe_dev_stats *stats;
1498 struct fc_frame_header *fh;
1499 unsigned int hlen; /* header length implies the version */
1500 unsigned int tlen; /* trailer length */
1501 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001502 struct fcoe_port *port = lport_priv(lport);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001503 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001504 u8 sof, eof;
1505 struct fcoe_hdr *hp;
1506
1507 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1508
Robert Love85b4aa42008-12-09 15:10:24 -08001509 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001510 skb = fp_skb(fp);
1511 wlen = skb->len / FCOE_WORD_TO_BYTE;
1512
Robert Love1875f272009-11-03 11:47:50 -08001513 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001514 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001515 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001516 }
1517
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001518 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001519 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001520 return 0;
1521
Robert Love85b4aa42008-12-09 15:10:24 -08001522 sof = fr_sof(fp);
1523 eof = fr_eof(fp);
1524
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001525 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001526 hlen = sizeof(struct fcoe_hdr);
1527 tlen = sizeof(struct fcoe_crc_eof);
1528 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1529
1530 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001531 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001532 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001533 skb->csum_start = skb_headroom(skb);
1534 skb->csum_offset = skb->len;
1535 crc = 0;
1536 } else {
1537 skb->ip_summed = CHECKSUM_NONE;
1538 crc = fcoe_fc_crc(fp);
1539 }
1540
Chris Leech014f5c32009-08-25 13:59:30 -07001541 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001542 if (skb_is_nonlinear(skb)) {
1543 skb_frag_t *frag;
1544 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001545 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001546 return -ENOMEM;
1547 }
1548 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1549 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1550 + frag->page_offset;
1551 } else {
1552 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1553 }
1554
1555 memset(cp, 0, sizeof(*cp));
1556 cp->fcoe_eof = eof;
1557 cp->fcoe_crc32 = cpu_to_le32(~crc);
1558
1559 if (skb_is_nonlinear(skb)) {
1560 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1561 cp = NULL;
1562 }
1563
Chris Leech014f5c32009-08-25 13:59:30 -07001564 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001565 skb_push(skb, elen + hlen);
1566 skb_reset_mac_header(skb);
1567 skb_reset_network_header(skb);
1568 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001569 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001570 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001571
1572 /* fill up mac and fcoe headers */
1573 eh = eth_hdr(skb);
1574 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001575 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001576 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001577 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001578
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001579 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1580 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001581 else
Chris Leech11b56182009-11-03 11:46:29 -08001582 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001583
1584 hp = (struct fcoe_hdr *)(eh + 1);
1585 memset(hp, 0, sizeof(*hp));
1586 if (FC_FCOE_VER)
1587 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1588 hp->fcoe_sof = sof;
1589
Yi Zou39ca9a02009-02-27 14:07:15 -08001590 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001591 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001592 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1593 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1594 } else {
1595 skb_shinfo(skb)->gso_type = 0;
1596 skb_shinfo(skb)->gso_size = 0;
1597 }
Robert Love85b4aa42008-12-09 15:10:24 -08001598 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001599 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001600 stats->TxFrames++;
1601 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001602 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001603
1604 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001605 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001606 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001607 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001608 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001609 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001610
1611 return 0;
1612}
Robert Love85b4aa42008-12-09 15:10:24 -08001613
Robert Love34f42a02009-02-27 10:55:45 -08001614/**
Robert Love1875f272009-11-03 11:47:50 -08001615 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1616 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001617 */
1618static void fcoe_percpu_flush_done(struct sk_buff *skb)
1619{
1620 complete(&fcoe_flush_completion);
1621}
1622
1623/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001624 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1625 * @lport: The local port the frame was received on
1626 * @fp: The received frame
1627 *
1628 * Return: 0 on passing filtering checks
1629 */
1630static inline int fcoe_filter_frames(struct fc_lport *lport,
1631 struct fc_frame *fp)
1632{
1633 struct fcoe_interface *fcoe;
1634 struct fc_frame_header *fh;
1635 struct sk_buff *skb = (struct sk_buff *)fp;
1636 struct fcoe_dev_stats *stats;
1637
1638 /*
1639 * We only check CRC if no offload is available and if it is
1640 * it's solicited data, in which case, the FCP layer would
1641 * check it during the copy.
1642 */
1643 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1644 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1645 else
1646 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1647
1648 fh = (struct fc_frame_header *) skb_transport_header(skb);
1649 fh = fc_frame_header_get(fp);
1650 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1651 return 0;
1652
1653 fcoe = ((struct fcoe_port *)lport_priv(lport))->fcoe;
1654 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1655 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1656 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1657 return -EINVAL;
1658 }
1659
1660 if (!fr_flags(fp) & FCPHF_CRC_UNCHECKED ||
1661 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1662 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1663 return 0;
1664 }
1665
1666 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1667 stats->InvalidCRCCount++;
1668 if (stats->InvalidCRCCount < 5)
1669 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1670 return -EINVAL;
1671}
1672
1673/**
Chris Leech859b7b62009-11-20 14:54:47 -08001674 * fcoe_recv_frame() - process a single received frame
1675 * @skb: frame to process
1676 */
1677static void fcoe_recv_frame(struct sk_buff *skb)
1678{
1679 u32 fr_len;
1680 struct fc_lport *lport;
1681 struct fcoe_rcv_info *fr;
1682 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001683 struct fcoe_crc_eof crc_eof;
1684 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001685 struct fcoe_port *port;
1686 struct fcoe_hdr *hp;
1687
1688 fr = fcoe_dev_from_skb(skb);
1689 lport = fr->fr_dev;
1690 if (unlikely(!lport)) {
1691 if (skb->destructor != fcoe_percpu_flush_done)
1692 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1693 kfree_skb(skb);
1694 return;
1695 }
1696
1697 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1698 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1699 skb->len, skb->data_len,
1700 skb->head, skb->data, skb_tail_pointer(skb),
1701 skb_end_pointer(skb), skb->csum,
1702 skb->dev ? skb->dev->name : "<NULL>");
1703
Chris Leech859b7b62009-11-20 14:54:47 -08001704 port = lport_priv(lport);
1705 if (skb_is_nonlinear(skb))
1706 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001707
1708 /*
1709 * Frame length checks and setting up the header pointers
1710 * was done in fcoe_rcv already.
1711 */
1712 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001713
Joe Eykholtf018b732010-03-12 16:08:55 -08001714 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001715 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1716 if (stats->ErrorFrames < 5)
1717 printk(KERN_WARNING "fcoe: FCoE version "
1718 "mismatch: The frame has "
1719 "version %x, but the "
1720 "initiator supports version "
1721 "%x\n", FC_FCOE_DECAPS_VER(hp),
1722 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001723 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001724 }
1725
1726 skb_pull(skb, sizeof(struct fcoe_hdr));
1727 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1728
1729 stats->RxFrames++;
1730 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1731
1732 fp = (struct fc_frame *)skb;
1733 fc_frame_init(fp);
1734 fr_dev(fp) = lport;
1735 fr_sof(fp) = hp->fcoe_sof;
1736
1737 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001738 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1739 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001740 fr_eof(fp) = crc_eof.fcoe_eof;
1741 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001742 if (pskb_trim(skb, fr_len))
1743 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001744
Vasu Dev52ee8322011-01-28 16:03:52 -08001745 if (!fcoe_filter_frames(lport, fp)) {
1746 put_cpu();
1747 fc_exch_recv(lport, fp);
1748 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001749 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001750drop:
1751 stats->ErrorFrames++;
1752 put_cpu();
1753 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001754}
1755
1756/**
Robert Love1875f272009-11-03 11:47:50 -08001757 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1758 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001759 *
1760 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001761 */
1762int fcoe_percpu_receive_thread(void *arg)
1763{
1764 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001765 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001766
Robert Love4469c192009-02-27 10:56:38 -08001767 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001768
1769 while (!kthread_should_stop()) {
1770
1771 spin_lock_bh(&p->fcoe_rx_list.lock);
1772 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1773 set_current_state(TASK_INTERRUPTIBLE);
1774 spin_unlock_bh(&p->fcoe_rx_list.lock);
1775 schedule();
1776 set_current_state(TASK_RUNNING);
1777 if (kthread_should_stop())
1778 return 0;
1779 spin_lock_bh(&p->fcoe_rx_list.lock);
1780 }
1781 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001782 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001783 }
1784 return 0;
1785}
1786
1787/**
Robert Love1875f272009-11-03 11:47:50 -08001788 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1789 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001790 *
Robert Love1875f272009-11-03 11:47:50 -08001791 * This empties the wait_queue, dequeues the head of the wait_queue queue
1792 * and calls fcoe_start_io() for each packet. If all skb have been
1793 * transmitted it returns the qlen. If an error occurs it restores
1794 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001795 *
Robert Love1875f272009-11-03 11:47:50 -08001796 * The wait_queue is used when the skb transmit fails. The failed skb
1797 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001798 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001799 */
Robert Love1875f272009-11-03 11:47:50 -08001800static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001801{
Robert Love1875f272009-11-03 11:47:50 -08001802 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001803 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001804
Chris Leech014f5c32009-08-25 13:59:30 -07001805 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001806
1807 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001808 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001809
Chris Leech014f5c32009-08-25 13:59:30 -07001810 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001811 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001812 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001813
Chris Leech014f5c32009-08-25 13:59:30 -07001814 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001815 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001816 port->fcoe_pending_queue.qlen++;
1817 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001818
Chris Leech014f5c32009-08-25 13:59:30 -07001819 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001820 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001821 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001822
1823 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001824 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001825 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001826 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001827 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001828 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001829 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001830 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001831 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001832
Chris Leech014f5c32009-08-25 13:59:30 -07001833 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001834 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001835 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1836 mod_timer(&port->timer, jiffies + 2);
1837 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001838out:
Chris Leech014f5c32009-08-25 13:59:30 -07001839 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001840 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001841 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001842 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001843}
1844
1845/**
Robert Love1875f272009-11-03 11:47:50 -08001846 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001847 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001848static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001849{
Robert Love85b4aa42008-12-09 15:10:24 -08001850 register_netdevice_notifier(&fcoe_notifier);
1851}
1852
1853/**
Robert Love1875f272009-11-03 11:47:50 -08001854 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001855 */
Robert Love85b4aa42008-12-09 15:10:24 -08001856static void fcoe_dev_cleanup(void)
1857{
1858 unregister_netdevice_notifier(&fcoe_notifier);
1859}
1860
1861/**
Robert Love1875f272009-11-03 11:47:50 -08001862 * fcoe_device_notification() - Handler for net device events
1863 * @notifier: The context of the notification
1864 * @event: The type of event
1865 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001866 *
Robert Love1875f272009-11-03 11:47:50 -08001867 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001868 *
1869 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001870 */
Robert Love85b4aa42008-12-09 15:10:24 -08001871static int fcoe_device_notification(struct notifier_block *notifier,
1872 ulong event, void *ptr)
1873{
Robert Love1875f272009-11-03 11:47:50 -08001874 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001875 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001876 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001877 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001878 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001879 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001880 u32 mfs;
1881 int rc = NOTIFY_OK;
1882
Chris Leech014f5c32009-08-25 13:59:30 -07001883 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001884 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001885 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001886 break;
1887 }
1888 }
Robert Love1875f272009-11-03 11:47:50 -08001889 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001890 rc = NOTIFY_DONE;
1891 goto out;
1892 }
1893
Robert Love85b4aa42008-12-09 15:10:24 -08001894 switch (event) {
1895 case NETDEV_DOWN:
1896 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001897 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001898 break;
1899 case NETDEV_UP:
1900 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001901 break;
1902 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001903 if (netdev->features & NETIF_F_FCOE_MTU)
1904 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001905 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1906 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001907 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001908 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001909 break;
1910 case NETDEV_REGISTER:
1911 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001912 case NETDEV_UNREGISTER:
1913 list_del(&fcoe->list);
1914 port = lport_priv(fcoe->ctlr.lp);
1915 fcoe_interface_cleanup(fcoe);
1916 schedule_work(&port->destroy_work);
1917 goto out;
1918 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001919 case NETDEV_FEAT_CHANGE:
1920 fcoe_netdev_features_change(lport, netdev);
1921 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001922 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001923 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001924 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001925 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001926
1927 fcoe_link_speed_update(lport);
1928
Robert Love1875f272009-11-03 11:47:50 -08001929 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001930 fcoe_ctlr_link_up(&fcoe->ctlr);
1931 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001932 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001933 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001934 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001935 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001936 }
1937out:
1938 return rc;
1939}
1940
1941/**
Robert Love1875f272009-11-03 11:47:50 -08001942 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
1943 * @buffer: The name of the net device
Robert Love85b4aa42008-12-09 15:10:24 -08001944 *
Robert Love1875f272009-11-03 11:47:50 -08001945 * Returns: NULL or a ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001946 */
Robert Love85b4aa42008-12-09 15:10:24 -08001947static struct net_device *fcoe_if_to_netdev(const char *buffer)
1948{
1949 char *cp;
1950 char ifname[IFNAMSIZ + 2];
1951
1952 if (buffer) {
1953 strlcpy(ifname, buffer, IFNAMSIZ);
1954 cp = ifname + strlen(ifname);
1955 while (--cp >= ifname && *cp == '\n')
1956 *cp = '\0';
1957 return dev_get_by_name(&init_net, ifname);
1958 }
1959 return NULL;
1960}
1961
1962/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001963 * fcoe_disable() - Disables a FCoE interface
1964 * @buffer: The name of the Ethernet interface to be disabled
1965 * @kp: The associated kernel parameter
1966 *
1967 * Called from sysfs.
1968 *
1969 * Returns: 0 for success
1970 */
1971static int fcoe_disable(const char *buffer, struct kernel_param *kp)
1972{
1973 struct fcoe_interface *fcoe;
1974 struct net_device *netdev;
1975 int rc = 0;
1976
1977 mutex_lock(&fcoe_config_mutex);
1978#ifdef CONFIG_FCOE_MODULE
1979 /*
1980 * Make sure the module has been initialized, and is not about to be
1981 * removed. Module paramter sysfs files are writable before the
1982 * module_init function is called and after module_exit.
1983 */
1984 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1985 rc = -ENODEV;
1986 goto out_nodev;
1987 }
1988#endif
1989
1990 netdev = fcoe_if_to_netdev(buffer);
1991 if (!netdev) {
1992 rc = -ENODEV;
1993 goto out_nodev;
1994 }
1995
Vasu Dev34ce27b2010-05-07 15:18:46 -07001996 if (!rtnl_trylock()) {
1997 dev_put(netdev);
1998 mutex_unlock(&fcoe_config_mutex);
1999 return restart_syscall();
2000 }
2001
Vasu Dev55a66d32009-12-10 09:59:31 -08002002 fcoe = fcoe_hostlist_lookup_port(netdev);
2003 rtnl_unlock();
2004
Chris Leech9ee50e42010-04-09 14:22:23 -07002005 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07002006 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002007 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002008 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002009 rc = -ENODEV;
2010
2011 dev_put(netdev);
2012out_nodev:
2013 mutex_unlock(&fcoe_config_mutex);
2014 return rc;
2015}
2016
2017/**
2018 * fcoe_enable() - Enables a FCoE interface
2019 * @buffer: The name of the Ethernet interface to be enabled
2020 * @kp: The associated kernel parameter
2021 *
2022 * Called from sysfs.
2023 *
2024 * Returns: 0 for success
2025 */
2026static int fcoe_enable(const char *buffer, struct kernel_param *kp)
2027{
2028 struct fcoe_interface *fcoe;
2029 struct net_device *netdev;
2030 int rc = 0;
2031
2032 mutex_lock(&fcoe_config_mutex);
2033#ifdef CONFIG_FCOE_MODULE
2034 /*
2035 * Make sure the module has been initialized, and is not about to be
2036 * removed. Module paramter sysfs files are writable before the
2037 * module_init function is called and after module_exit.
2038 */
2039 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2040 rc = -ENODEV;
2041 goto out_nodev;
2042 }
2043#endif
2044
2045 netdev = fcoe_if_to_netdev(buffer);
2046 if (!netdev) {
2047 rc = -ENODEV;
2048 goto out_nodev;
2049 }
2050
Vasu Dev34ce27b2010-05-07 15:18:46 -07002051 if (!rtnl_trylock()) {
2052 dev_put(netdev);
2053 mutex_unlock(&fcoe_config_mutex);
2054 return restart_syscall();
2055 }
2056
Vasu Dev55a66d32009-12-10 09:59:31 -08002057 fcoe = fcoe_hostlist_lookup_port(netdev);
2058 rtnl_unlock();
2059
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002060 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002061 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002062 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2063 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002064
2065 dev_put(netdev);
2066out_nodev:
2067 mutex_unlock(&fcoe_config_mutex);
2068 return rc;
2069}
2070
2071/**
Robert Love1875f272009-11-03 11:47:50 -08002072 * fcoe_destroy() - Destroy a FCoE interface
2073 * @buffer: The name of the Ethernet interface to be destroyed
2074 * @kp: The associated kernel parameter
2075 *
2076 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002077 *
2078 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002079 */
Robert Love85b4aa42008-12-09 15:10:24 -08002080static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
2081{
Chris Leech030f4e02009-08-25 14:00:02 -07002082 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002083 struct net_device *netdev;
Mike Christie8eca355f2009-10-21 16:27:44 -07002084 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002085
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002086 mutex_lock(&fcoe_config_mutex);
2087#ifdef CONFIG_FCOE_MODULE
2088 /*
2089 * Make sure the module has been initialized, and is not about to be
2090 * removed. Module paramter sysfs files are writable before the
2091 * module_init function is called and after module_exit.
2092 */
2093 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2094 rc = -ENODEV;
2095 goto out_nodev;
2096 }
2097#endif
2098
Robert Love85b4aa42008-12-09 15:10:24 -08002099 netdev = fcoe_if_to_netdev(buffer);
2100 if (!netdev) {
2101 rc = -ENODEV;
2102 goto out_nodev;
2103 }
Chris Leech2e70e242009-08-25 14:00:23 -07002104
Vasu Dev34ce27b2010-05-07 15:18:46 -07002105 if (!rtnl_trylock()) {
2106 dev_put(netdev);
2107 mutex_unlock(&fcoe_config_mutex);
2108 return restart_syscall();
2109 }
2110
Chris Leech2e70e242009-08-25 14:00:23 -07002111 fcoe = fcoe_hostlist_lookup_port(netdev);
2112 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07002113 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002114 rc = -ENODEV;
2115 goto out_putdev;
2116 }
Chris Leech2e70e242009-08-25 14:00:23 -07002117 fcoe_interface_cleanup(fcoe);
Yi Zou54a5b212010-07-20 15:21:17 -07002118 list_del(&fcoe->list);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002119 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002120 fcoe_if_destroy(fcoe->ctlr.lp);
Rob Love6409ea652010-01-21 10:16:05 -08002121
Robert Love85b4aa42008-12-09 15:10:24 -08002122out_putdev:
2123 dev_put(netdev);
2124out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002125 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002126 return rc;
2127}
2128
Robert Love1875f272009-11-03 11:47:50 -08002129/**
2130 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2131 * @work: Handle to the FCoE port to be destroyed
2132 */
Chris Leech2e70e242009-08-25 14:00:23 -07002133static void fcoe_destroy_work(struct work_struct *work)
2134{
2135 struct fcoe_port *port;
2136
2137 port = container_of(work, struct fcoe_port, destroy_work);
2138 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002139 rtnl_lock();
2140 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002141 fcoe_if_destroy(port->lport);
2142 mutex_unlock(&fcoe_config_mutex);
2143}
2144
Robert Love85b4aa42008-12-09 15:10:24 -08002145/**
Robert Love1875f272009-11-03 11:47:50 -08002146 * fcoe_create() - Create a fcoe interface
2147 * @buffer: The name of the Ethernet interface to create on
2148 * @kp: The associated kernel param
2149 *
2150 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002151 *
2152 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002153 */
Robert Love85b4aa42008-12-09 15:10:24 -08002154static int fcoe_create(const char *buffer, struct kernel_param *kp)
2155{
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002156 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
Robert Love85b4aa42008-12-09 15:10:24 -08002157 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07002158 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002159 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002160 struct net_device *netdev;
2161
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002162 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002163
2164 if (!rtnl_trylock()) {
2165 mutex_unlock(&fcoe_config_mutex);
2166 return restart_syscall();
2167 }
2168
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002169#ifdef CONFIG_FCOE_MODULE
2170 /*
2171 * Make sure the module has been initialized, and is not about to be
2172 * removed. Module paramter sysfs files are writable before the
2173 * module_init function is called and after module_exit.
2174 */
2175 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2176 rc = -ENODEV;
Robert Love7287fb92011-01-28 16:03:47 -08002177 goto out_nodev;
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002178 }
2179#endif
2180
Robert Love85b4aa42008-12-09 15:10:24 -08002181 netdev = fcoe_if_to_netdev(buffer);
2182 if (!netdev) {
2183 rc = -ENODEV;
2184 goto out_nodev;
2185 }
Chris Leech2e70e242009-08-25 14:00:23 -07002186
Robert Love85b4aa42008-12-09 15:10:24 -08002187 /* look for existing lport */
2188 if (fcoe_hostlist_lookup(netdev)) {
2189 rc = -EEXIST;
2190 goto out_putdev;
2191 }
Robert Love85b4aa42008-12-09 15:10:24 -08002192
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002193 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002194 if (IS_ERR(fcoe)) {
2195 rc = PTR_ERR(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002196 goto out_putdev;
2197 }
2198
Chris Leech9a057532009-11-03 11:46:40 -08002199 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002200 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002201 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002202 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002203 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002204 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002205 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002206 }
Chris Leech030f4e02009-08-25 14:00:02 -07002207
Chris Leech54b649f2009-08-25 14:00:07 -07002208 /* Make this the "master" N_Port */
2209 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002210
Chris Leechc863df32009-08-25 14:00:18 -07002211 /* add to lports list */
2212 fcoe_hostlist_add(lport);
2213
Chris Leech54b649f2009-08-25 14:00:07 -07002214 /* start FIP Discovery and FLOGI */
2215 lport->boot_time = jiffies;
2216 fc_fabric_login(lport);
2217 if (!fcoe_link_ok(lport))
2218 fcoe_ctlr_link_up(&fcoe->ctlr);
2219
Chris Leech54b649f2009-08-25 14:00:07 -07002220 /*
2221 * Release from init in fcoe_interface_create(), on success lport
2222 * should be holding a reference taken in fcoe_if_create().
2223 */
Chris Leech030f4e02009-08-25 14:00:02 -07002224 fcoe_interface_put(fcoe);
Rob Love6409ea652010-01-21 10:16:05 -08002225 dev_put(netdev);
2226 rtnl_unlock();
2227 mutex_unlock(&fcoe_config_mutex);
2228
2229 return 0;
2230out_free:
2231 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002232out_putdev:
2233 dev_put(netdev);
2234out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002235 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002236 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002237 return rc;
2238}
2239
Robert Love34f42a02009-02-27 10:55:45 -08002240/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002241 * fcoe_link_speed_update() - Update the supported and actual link speeds
2242 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002243 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002244 * Returns: 0 if the ethtool query was successful
2245 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002246 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002247int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002248{
Robert Love1875f272009-11-03 11:47:50 -08002249 struct fcoe_port *port = lport_priv(lport);
2250 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08002251 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002252
Robert Love5e4f8fe2010-05-07 15:18:35 -07002253 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002254 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002255 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2256 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2257 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002258 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002259 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002260 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002261 FC_PORTSPEED_10GBIT;
2262 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002263 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002264 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002265 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002266
Yi Zou2f718d62009-07-29 17:04:01 -07002267 return 0;
2268 }
2269 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002270}
Robert Love85b4aa42008-12-09 15:10:24 -08002271
Robert Love34f42a02009-02-27 10:55:45 -08002272/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002273 * fcoe_link_ok() - Check if the link is OK for a local port
2274 * @lport: The local port to check link on
2275 *
2276 * Returns: 0 if link is UP and OK, -1 if not
2277 *
2278 */
2279int fcoe_link_ok(struct fc_lport *lport)
2280{
2281 struct fcoe_port *port = lport_priv(lport);
2282 struct net_device *netdev = port->fcoe->netdev;
2283
2284 if (netif_oper_up(netdev))
2285 return 0;
2286 return -1;
2287}
2288
2289/**
Robert Love1875f272009-11-03 11:47:50 -08002290 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2291 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002292 *
2293 * Must be called with fcoe_create_mutex held to single-thread completion.
2294 *
2295 * This flushes the pending skbs by adding a new skb to each queue and
2296 * waiting until they are all freed. This assures us that not only are
2297 * there no packets that will be handled by the lport, but also that any
2298 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002299 */
Robert Love1875f272009-11-03 11:47:50 -08002300void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002301{
Robert Love85b4aa42008-12-09 15:10:24 -08002302 struct fcoe_percpu_s *pp;
2303 struct fcoe_rcv_info *fr;
2304 struct sk_buff_head *list;
2305 struct sk_buff *skb, *next;
2306 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002307 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002308
Robert Love5e5e92d2009-03-17 11:41:35 -07002309 for_each_possible_cpu(cpu) {
2310 pp = &per_cpu(fcoe_percpu, cpu);
2311 spin_lock_bh(&pp->fcoe_rx_list.lock);
2312 list = &pp->fcoe_rx_list;
2313 head = list->next;
2314 for (skb = head; skb != (struct sk_buff *)list;
2315 skb = next) {
2316 next = skb->next;
2317 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002318 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002319 __skb_unlink(skb, list);
2320 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002321 }
Robert Love85b4aa42008-12-09 15:10:24 -08002322 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002323
2324 if (!pp->thread || !cpu_online(cpu)) {
2325 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2326 continue;
2327 }
2328
2329 skb = dev_alloc_skb(0);
2330 if (!skb) {
2331 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2332 continue;
2333 }
2334 skb->destructor = fcoe_percpu_flush_done;
2335
2336 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2337 if (pp->fcoe_rx_list.qlen == 1)
2338 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002339 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002340
2341 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002342 }
2343}
Robert Love85b4aa42008-12-09 15:10:24 -08002344
2345/**
Robert Love34f42a02009-02-27 10:55:45 -08002346 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002347 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002348 */
Robert Love1875f272009-11-03 11:47:50 -08002349void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002350{
Robert Love1875f272009-11-03 11:47:50 -08002351 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002352 struct sk_buff *skb;
2353
Chris Leech014f5c32009-08-25 13:59:30 -07002354 spin_lock_bh(&port->fcoe_pending_queue.lock);
2355 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2356 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002357 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002358 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002359 }
Chris Leech014f5c32009-08-25 13:59:30 -07002360 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002361}
Robert Love85b4aa42008-12-09 15:10:24 -08002362
2363/**
Robert Love1875f272009-11-03 11:47:50 -08002364 * fcoe_reset() - Reset a local port
2365 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002366 *
Robert Love1875f272009-11-03 11:47:50 -08002367 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002368 */
2369int fcoe_reset(struct Scsi_Host *shost)
2370{
2371 struct fc_lport *lport = shost_priv(shost);
2372 fc_lport_reset(lport);
2373 return 0;
2374}
Robert Love85b4aa42008-12-09 15:10:24 -08002375
Robert Love34f42a02009-02-27 10:55:45 -08002376/**
Robert Love1875f272009-11-03 11:47:50 -08002377 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2378 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002379 *
Robert Love1875f272009-11-03 11:47:50 -08002380 * Locking: Must be called with the RNL mutex held.
2381 *
2382 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002383 */
Chris Leech014f5c32009-08-25 13:59:30 -07002384static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002385fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002386{
Chris Leech014f5c32009-08-25 13:59:30 -07002387 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002388
Chris Leech014f5c32009-08-25 13:59:30 -07002389 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002390 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002391 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002392 }
Robert Love85b4aa42008-12-09 15:10:24 -08002393 return NULL;
2394}
2395
Robert Love34f42a02009-02-27 10:55:45 -08002396/**
Robert Love1875f272009-11-03 11:47:50 -08002397 * fcoe_hostlist_lookup() - Find the local port associated with a
2398 * given net device
2399 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002400 *
Robert Love1875f272009-11-03 11:47:50 -08002401 * Locking: Must be called with the RTNL mutex held
2402 *
2403 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002404 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002405static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002406{
Chris Leech014f5c32009-08-25 13:59:30 -07002407 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002408
Chris Leech014f5c32009-08-25 13:59:30 -07002409 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002410 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002411}
Robert Love85b4aa42008-12-09 15:10:24 -08002412
Robert Love34f42a02009-02-27 10:55:45 -08002413/**
Robert Love1875f272009-11-03 11:47:50 -08002414 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2415 * port to the hostlist
2416 * @lport: The local port that identifies the FCoE interface to be added
2417 *
2418 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002419 *
2420 * Returns: 0 for success
2421 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002422static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002423{
Chris Leech014f5c32009-08-25 13:59:30 -07002424 struct fcoe_interface *fcoe;
2425 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002426
Chris Leech014f5c32009-08-25 13:59:30 -07002427 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2428 if (!fcoe) {
2429 port = lport_priv(lport);
2430 fcoe = port->fcoe;
2431 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002432 }
2433 return 0;
2434}
Robert Love85b4aa42008-12-09 15:10:24 -08002435
Robert Love34f42a02009-02-27 10:55:45 -08002436/**
Robert Love1875f272009-11-03 11:47:50 -08002437 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002438 *
Robert Love1875f272009-11-03 11:47:50 -08002439 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002440 */
Robert Love85b4aa42008-12-09 15:10:24 -08002441static int __init fcoe_init(void)
2442{
Robert Love1875f272009-11-03 11:47:50 -08002443 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002444 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002445 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002446
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002447 mutex_lock(&fcoe_config_mutex);
2448
Robert Love38eccab2009-03-17 11:41:30 -07002449 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002450 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002451 skb_queue_head_init(&p->fcoe_rx_list);
2452 }
2453
Robert Love8976f422009-03-17 11:41:46 -07002454 for_each_online_cpu(cpu)
2455 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002456
Robert Love8976f422009-03-17 11:41:46 -07002457 /* Initialize per CPU interrupt thread */
2458 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2459 if (rc)
2460 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002461
Robert Love8976f422009-03-17 11:41:46 -07002462 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002463 fcoe_dev_setup();
2464
Chris Leech5892c322009-08-25 13:59:14 -07002465 rc = fcoe_if_init();
2466 if (rc)
2467 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002468
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002469 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002470 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002471
2472out_free:
2473 for_each_online_cpu(cpu) {
2474 fcoe_percpu_thread_destroy(cpu);
2475 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002476 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002477 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002478}
2479module_init(fcoe_init);
2480
2481/**
Robert Love1875f272009-11-03 11:47:50 -08002482 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002483 *
Robert Love1875f272009-11-03 11:47:50 -08002484 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002485 */
Robert Love85b4aa42008-12-09 15:10:24 -08002486static void __exit fcoe_exit(void)
2487{
Chris Leech014f5c32009-08-25 13:59:30 -07002488 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002489 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002490 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002491
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002492 mutex_lock(&fcoe_config_mutex);
2493
Robert Love85b4aa42008-12-09 15:10:24 -08002494 fcoe_dev_cleanup();
2495
Vasu Dev5919a592009-03-27 09:03:29 -07002496 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002497 rtnl_lock();
2498 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002499 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002500 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002501 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002502 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002503 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002504 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002505
Robert Love8976f422009-03-17 11:41:46 -07002506 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2507
Chris Leech014f5c32009-08-25 13:59:30 -07002508 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002509 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002510
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002511 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002512
2513 /* flush any asyncronous interface destroys,
2514 * this should happen after the netdev notifier is unregistered */
2515 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002516 /* That will flush out all the N_Ports on the hostlist, but now we
2517 * may have NPIV VN_Ports scheduled for destruction */
2518 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002519
2520 /* detach from scsi transport
2521 * must happen after all destroys are done, therefor after the flush */
2522 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002523}
2524module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002525
2526/**
2527 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2528 * @seq: active sequence in the FLOGI or FDISC exchange
2529 * @fp: response frame, or error encoded in a pointer (timeout)
2530 * @arg: pointer the the fcoe_ctlr structure
2531 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002532 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002533 * the libfc FLOGI response handler.
2534 */
2535static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2536{
2537 struct fcoe_ctlr *fip = arg;
2538 struct fc_exch *exch = fc_seq_exch(seq);
2539 struct fc_lport *lport = exch->lp;
2540 u8 *mac;
2541
2542 if (IS_ERR(fp))
2543 goto done;
2544
2545 mac = fr_cb(fp)->granted_mac;
2546 if (is_zero_ether_addr(mac)) {
2547 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002548 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002549 fc_frame_free(fp);
2550 return;
2551 }
Chris Leech11b56182009-11-03 11:46:29 -08002552 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002553 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002554done:
2555 fc_lport_flogi_resp(seq, fp, lport);
2556}
2557
2558/**
2559 * fcoe_logo_resp() - FCoE specific LOGO response handler
2560 * @seq: active sequence in the LOGO exchange
2561 * @fp: response frame, or error encoded in a pointer (timeout)
2562 * @arg: pointer the the fcoe_ctlr structure
2563 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002564 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002565 * the libfc LOGO response handler.
2566 */
2567static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2568{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002569 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002570 static u8 zero_mac[ETH_ALEN] = { 0 };
2571
2572 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002573 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002574 fc_lport_logo_resp(seq, fp, lport);
2575}
2576
2577/**
2578 * fcoe_elsct_send - FCoE specific ELS handler
2579 *
2580 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2581 * using FCoE specific response handlers and passing the FIP controller as
2582 * the argument (the lport is still available from the exchange).
2583 *
2584 * Most of the work here is just handed off to the libfc routine.
2585 */
Robert Love1875f272009-11-03 11:47:50 -08002586static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2587 struct fc_frame *fp, unsigned int op,
2588 void (*resp)(struct fc_seq *,
2589 struct fc_frame *,
2590 void *),
2591 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002592{
2593 struct fcoe_port *port = lport_priv(lport);
2594 struct fcoe_interface *fcoe = port->fcoe;
2595 struct fcoe_ctlr *fip = &fcoe->ctlr;
2596 struct fc_frame_header *fh = fc_frame_header_get(fp);
2597
2598 switch (op) {
2599 case ELS_FLOGI:
2600 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002601 if (lport->point_to_multipoint)
2602 break;
Chris Leech11b56182009-11-03 11:46:29 -08002603 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2604 fip, timeout);
2605 case ELS_LOGO:
2606 /* only hook onto fabric logouts, not port logouts */
2607 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2608 break;
2609 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002610 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002611 }
2612 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2613}
2614
Chris Leech9a057532009-11-03 11:46:40 -08002615/**
2616 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2617 * @vport: fc_vport object to create a new fc_host for
2618 * @disabled: start the new fc_host in a disabled state by default?
2619 *
2620 * Returns: 0 for success
2621 */
2622static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2623{
2624 struct Scsi_Host *shost = vport_to_shost(vport);
2625 struct fc_lport *n_port = shost_priv(shost);
2626 struct fcoe_port *port = lport_priv(n_port);
2627 struct fcoe_interface *fcoe = port->fcoe;
2628 struct net_device *netdev = fcoe->netdev;
2629 struct fc_lport *vn_port;
2630
2631 mutex_lock(&fcoe_config_mutex);
2632 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2633 mutex_unlock(&fcoe_config_mutex);
2634
2635 if (IS_ERR(vn_port)) {
2636 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2637 netdev->name);
2638 return -EIO;
2639 }
2640
2641 if (disabled) {
2642 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2643 } else {
2644 vn_port->boot_time = jiffies;
2645 fc_fabric_login(vn_port);
2646 fc_vport_setlink(vn_port);
2647 }
2648 return 0;
2649}
2650
2651/**
2652 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2653 * @vport: fc_vport object that is being destroyed
2654 *
2655 * Returns: 0 for success
2656 */
2657static int fcoe_vport_destroy(struct fc_vport *vport)
2658{
2659 struct Scsi_Host *shost = vport_to_shost(vport);
2660 struct fc_lport *n_port = shost_priv(shost);
2661 struct fc_lport *vn_port = vport->dd_data;
2662 struct fcoe_port *port = lport_priv(vn_port);
2663
2664 mutex_lock(&n_port->lp_mutex);
2665 list_del(&vn_port->list);
2666 mutex_unlock(&n_port->lp_mutex);
2667 schedule_work(&port->destroy_work);
2668 return 0;
2669}
2670
2671/**
2672 * fcoe_vport_disable() - change vport state
2673 * @vport: vport to bring online/offline
2674 * @disable: should the vport be disabled?
2675 */
2676static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2677{
2678 struct fc_lport *lport = vport->dd_data;
2679
2680 if (disable) {
2681 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2682 fc_fabric_logoff(lport);
2683 } else {
2684 lport->boot_time = jiffies;
2685 fc_fabric_login(lport);
2686 fc_vport_setlink(lport);
2687 }
2688
2689 return 0;
2690}
2691
Chris Leechdc8596d2009-11-03 11:47:18 -08002692/**
2693 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2694 * @vport: fc_vport with a new symbolic name string
2695 *
2696 * After generating a new symbolic name string, a new RSPN_ID request is
2697 * sent to the name server. There is no response handler, so if it fails
2698 * for some reason it will not be retried.
2699 */
2700static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2701{
2702 struct fc_lport *lport = vport->dd_data;
2703 struct fc_frame *fp;
2704 size_t len;
2705
2706 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2707 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2708 fcoe_netdev(lport)->name, vport->symbolic_name);
2709
2710 if (lport->state != LPORT_ST_READY)
2711 return;
2712
2713 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2714 fp = fc_frame_alloc(lport,
2715 sizeof(struct fc_ct_hdr) +
2716 sizeof(struct fc_ns_rspn) + len);
2717 if (!fp)
2718 return;
2719 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002720 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002721}
Yi Zoub84056b2009-11-20 14:55:19 -08002722
2723/**
2724 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2725 * @lport: the local port
2726 * @fc_lesb: the link error status block
2727 */
2728static void fcoe_get_lesb(struct fc_lport *lport,
2729 struct fc_els_lesb *fc_lesb)
2730{
2731 unsigned int cpu;
2732 u32 lfc, vlfc, mdac;
2733 struct fcoe_dev_stats *devst;
2734 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002735 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002736 struct net_device *netdev = fcoe_netdev(lport);
2737
2738 lfc = 0;
2739 vlfc = 0;
2740 mdac = 0;
2741 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2742 memset(lesb, 0, sizeof(*lesb));
2743 for_each_possible_cpu(cpu) {
2744 devst = per_cpu_ptr(lport->dev_stats, cpu);
2745 lfc += devst->LinkFailureCount;
2746 vlfc += devst->VLinkFailureCount;
2747 mdac += devst->MissDiscAdvCount;
2748 }
2749 lesb->lesb_link_fail = htonl(lfc);
2750 lesb->lesb_vlink_fail = htonl(vlfc);
2751 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002752 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002753}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002754
2755/**
2756 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2757 * @lport: the local port
2758 * @port_id: the port ID
2759 * @fp: the received frame, if any, that caused the port_id to be set.
2760 *
2761 * This routine handles the case where we received a FLOGI and are
2762 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2763 * so it can set the non-mapped mode and gateway address.
2764 *
2765 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2766 */
2767static void fcoe_set_port_id(struct fc_lport *lport,
2768 u32 port_id, struct fc_frame *fp)
2769{
2770 struct fcoe_port *port = lport_priv(lport);
2771 struct fcoe_interface *fcoe = port->fcoe;
2772
2773 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2774 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2775}