blob: 46c57e5755aef42280cb10714310f46b32308c19 [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>
Tejun Heo2ca32b42011-01-28 16:05:32 -080034#include <linux/workqueue.h>
Robert Love85b4aa42008-12-09 15:10:24 -080035#include <scsi/scsi_tcq.h>
36#include <scsi/scsicam.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_fc.h>
39#include <net/rtnetlink.h>
40
41#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070042#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080043
44#include <scsi/libfc.h>
45#include <scsi/fc_frame.h>
46#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080047
Vasu Devfdd78022009-03-17 11:42:24 -070048#include "fcoe.h"
49
Robert Love85b4aa42008-12-09 15:10:24 -080050MODULE_AUTHOR("Open-FCoE.org");
51MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070052MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080053
Yi Zou05cc7392009-08-25 13:59:03 -070054/* Performance tuning parameters for fcoe */
55static unsigned int fcoe_ddp_min;
56module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
58 "Direct Data Placement (DDP).");
59
Chris Leechdfc1d0f2009-08-25 14:00:13 -070060DEFINE_MUTEX(fcoe_config_mutex);
61
Tejun Heo2ca32b42011-01-28 16:05:32 -080062static struct workqueue_struct *fcoe_wq;
63
Joe Eykholte7a51992009-08-25 14:04:08 -070064/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
65static DECLARE_COMPLETION(fcoe_flush_completion);
66
Robert Love85b4aa42008-12-09 15:10:24 -080067/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070068/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080069LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070070DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080071
Chris Leechdd3fd722009-04-21 16:27:36 -070072/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080073static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070074static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
75static int fcoe_rcv(struct sk_buff *, struct net_device *,
76 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080077static int fcoe_percpu_receive_thread(void *);
78static void fcoe_clean_pending_queue(struct fc_lport *);
79static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070080static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080081static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070082
83static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
84static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070085
Vasu Dev4bb6b512009-05-06 10:52:34 -070086static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080087static int fcoe_device_notification(struct notifier_block *, ulong, void *);
88static void fcoe_dev_setup(void);
89static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080090static struct fcoe_interface
91*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080092
Robert Love1875f272009-11-03 11:47:50 -080093static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
94 struct packet_type *, struct net_device *);
95
96static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
97static void fcoe_update_src_mac(struct fc_lport *, u8 *);
98static u8 *fcoe_get_src_mac(struct fc_lport *);
99static void fcoe_destroy_work(struct work_struct *);
100
101static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
102 unsigned int);
103static int fcoe_ddp_done(struct fc_lport *, u16);
104
105static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
106
Yi Zou78a58242011-01-28 16:05:16 -0800107static bool fcoe_match(struct net_device *netdev);
108static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
109static int fcoe_destroy(struct net_device *netdev);
110static int fcoe_enable(struct net_device *netdev);
111static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800112
Robert Love1875f272009-11-03 11:47:50 -0800113static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
114 u32 did, struct fc_frame *,
115 unsigned int op,
116 void (*resp)(struct fc_seq *,
117 struct fc_frame *,
118 void *),
119 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800120static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800121
Yi Zoub84056b2009-11-20 14:55:19 -0800122static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
123
Robert Love1875f272009-11-03 11:47:50 -0800124/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800125static struct notifier_block fcoe_notifier = {
126 .notifier_call = fcoe_device_notification,
127};
128
Robert Love1875f272009-11-03 11:47:50 -0800129/* notification function for CPU hotplug events */
130static struct notifier_block fcoe_cpu_notifier = {
131 .notifier_call = fcoe_cpu_callback,
132};
133
Yi Zou8ca86f82011-01-28 16:05:11 -0800134static struct scsi_transport_template *fcoe_nport_scsi_transport;
135static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700136
Robert Love1875f272009-11-03 11:47:50 -0800137static int fcoe_vport_destroy(struct fc_vport *);
138static int fcoe_vport_create(struct fc_vport *, bool disabled);
139static int fcoe_vport_disable(struct fc_vport *, bool disable);
140static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800141static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love1875f272009-11-03 11:47:50 -0800142
143static struct libfc_function_template fcoe_libfc_fcn_templ = {
144 .frame_send = fcoe_xmit,
145 .ddp_setup = fcoe_ddp_setup,
146 .ddp_done = fcoe_ddp_done,
147 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800148 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800149 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800150};
Chris Leech9a057532009-11-03 11:46:40 -0800151
Yi Zou8ca86f82011-01-28 16:05:11 -0800152struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700153 .show_host_node_name = 1,
154 .show_host_port_name = 1,
155 .show_host_supported_classes = 1,
156 .show_host_supported_fc4s = 1,
157 .show_host_active_fc4s = 1,
158 .show_host_maxframe_size = 1,
159
160 .show_host_port_id = 1,
161 .show_host_supported_speeds = 1,
162 .get_host_speed = fc_get_host_speed,
163 .show_host_speed = 1,
164 .show_host_port_type = 1,
165 .get_host_port_state = fc_get_host_port_state,
166 .show_host_port_state = 1,
167 .show_host_symbolic_name = 1,
168
169 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
170 .show_rport_maxframe_size = 1,
171 .show_rport_supported_classes = 1,
172
173 .show_host_fabric_name = 1,
174 .show_starget_node_name = 1,
175 .show_starget_port_name = 1,
176 .show_starget_port_id = 1,
177 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
178 .show_rport_dev_loss_tmo = 1,
179 .get_fc_host_stats = fc_get_host_stats,
180 .issue_fc_host_lip = fcoe_reset,
181
182 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800183
184 .vport_create = fcoe_vport_create,
185 .vport_delete = fcoe_vport_destroy,
186 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800187 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800188
189 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700190};
191
Yi Zou8ca86f82011-01-28 16:05:11 -0800192struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800193 .show_host_node_name = 1,
194 .show_host_port_name = 1,
195 .show_host_supported_classes = 1,
196 .show_host_supported_fc4s = 1,
197 .show_host_active_fc4s = 1,
198 .show_host_maxframe_size = 1,
199
200 .show_host_port_id = 1,
201 .show_host_supported_speeds = 1,
202 .get_host_speed = fc_get_host_speed,
203 .show_host_speed = 1,
204 .show_host_port_type = 1,
205 .get_host_port_state = fc_get_host_port_state,
206 .show_host_port_state = 1,
207 .show_host_symbolic_name = 1,
208
209 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
210 .show_rport_maxframe_size = 1,
211 .show_rport_supported_classes = 1,
212
213 .show_host_fabric_name = 1,
214 .show_starget_node_name = 1,
215 .show_starget_port_name = 1,
216 .show_starget_port_id = 1,
217 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
218 .show_rport_dev_loss_tmo = 1,
219 .get_fc_host_stats = fc_get_host_stats,
220 .issue_fc_host_lip = fcoe_reset,
221
222 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800223
224 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800225};
226
Vasu Dev7f349142009-03-27 09:06:31 -0700227static struct scsi_host_template fcoe_shost_template = {
228 .module = THIS_MODULE,
229 .name = "FCoE Driver",
230 .proc_name = FCOE_NAME,
231 .queuecommand = fc_queuecommand,
232 .eh_abort_handler = fc_eh_abort,
233 .eh_device_reset_handler = fc_eh_device_reset,
234 .eh_host_reset_handler = fc_eh_host_reset,
235 .slave_alloc = fc_slave_alloc,
236 .change_queue_depth = fc_change_queue_depth,
237 .change_queue_type = fc_change_queue_type,
238 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700239 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700240 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
241 .use_clustering = ENABLE_CLUSTERING,
242 .sg_tablesize = SG_ALL,
243 .max_sectors = 0xffff,
244};
245
Chris Leech54b649f2009-08-25 14:00:07 -0700246/**
Robert Love1875f272009-11-03 11:47:50 -0800247 * fcoe_interface_setup() - Setup a FCoE interface
248 * @fcoe: The new FCoE interface
249 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700250 *
251 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700252 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700253 */
254static int fcoe_interface_setup(struct fcoe_interface *fcoe,
255 struct net_device *netdev)
256{
257 struct fcoe_ctlr *fip = &fcoe->ctlr;
258 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800259 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700260 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700261 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700262
263 fcoe->netdev = netdev;
264
Yi Zoub7a727f2009-10-21 16:28:03 -0700265 /* Let LLD initialize for FCoE */
266 ops = netdev->netdev_ops;
267 if (ops->ndo_fcoe_enable) {
268 if (ops->ndo_fcoe_enable(netdev))
269 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
270 " specific feature for LLD.\n");
271 }
272
Chris Leech54b649f2009-08-25 14:00:07 -0700273 /* Do not support for bonding device */
274 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
275 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
276 (netdev->priv_flags & IFF_MASTER_8023AD)) {
john fastabend59d92512009-11-03 11:48:44 -0800277 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700278 return -EOPNOTSUPP;
279 }
280
281 /* look for SAN MAC address, if multiple SAN MACs exist, only
282 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800283 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
284 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700285 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800286 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700287 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800288 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700289 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
290 fip->spma = 1;
291 break;
292 }
293 }
294 rcu_read_unlock();
295
296 /* setup Source Mac Address */
297 if (!fip->spma)
298 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
299
300 /*
301 * Add FCoE MAC address as second unicast MAC address
302 * or enter promiscuous mode if not capable of listening
303 * for multiple unicast MACs.
304 */
Chris Leech54b649f2009-08-25 14:00:07 -0700305 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000306 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700307 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000308 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700309 if (fip->mode == FIP_MODE_VN2VN) {
310 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
311 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
312 } else
313 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700314
315 /*
316 * setup the receive function from ethernet driver
317 * on the ethertype for the given device
318 */
319 fcoe->fcoe_packet_type.func = fcoe_rcv;
320 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
321 fcoe->fcoe_packet_type.dev = netdev;
322 dev_add_pack(&fcoe->fcoe_packet_type);
323
324 fcoe->fip_packet_type.func = fcoe_fip_recv;
325 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
326 fcoe->fip_packet_type.dev = netdev;
327 dev_add_pack(&fcoe->fip_packet_type);
328
329 return 0;
330}
331
Vasu Dev7f349142009-03-27 09:06:31 -0700332/**
Robert Love1875f272009-11-03 11:47:50 -0800333 * fcoe_interface_create() - Create a FCoE interface on a net device
334 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700335 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700336 *
337 * Returns: pointer to a struct fcoe_interface or NULL on error
338 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700339static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
340 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700341{
342 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800343 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700344
Robert Love7287fb92011-01-28 16:03:47 -0800345 if (!try_module_get(THIS_MODULE)) {
346 FCOE_NETDEV_DBG(netdev,
347 "Could not get a reference to the module\n");
348 fcoe = ERR_PTR(-EBUSY);
349 goto out;
350 }
351
Chris Leech030f4e02009-08-25 14:00:02 -0700352 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
353 if (!fcoe) {
354 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800355 fcoe = ERR_PTR(-ENOMEM);
356 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700357 }
358
Chris Leech2e70e242009-08-25 14:00:23 -0700359 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700360 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700361
362 /*
363 * Initialize FIP.
364 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700365 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700366 fcoe->ctlr.send = fcoe_fip_send;
367 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800368 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700369
john fastabend59d92512009-11-03 11:48:44 -0800370 err = fcoe_interface_setup(fcoe, netdev);
371 if (err) {
372 fcoe_ctlr_destroy(&fcoe->ctlr);
373 kfree(fcoe);
374 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800375 fcoe = ERR_PTR(err);
376 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800377 }
Chris Leech030f4e02009-08-25 14:00:02 -0700378
Robert Love7287fb92011-01-28 16:03:47 -0800379 goto out;
380
381out_nomod:
382 module_put(THIS_MODULE);
383out:
Chris Leech030f4e02009-08-25 14:00:02 -0700384 return fcoe;
385}
386
387/**
Robert Love1875f272009-11-03 11:47:50 -0800388 * fcoe_interface_cleanup() - Clean up a FCoE interface
389 * @fcoe: The FCoE interface to be cleaned up
Chris Leech2e70e242009-08-25 14:00:23 -0700390 *
391 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700392 */
393void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
394{
395 struct net_device *netdev = fcoe->netdev;
396 struct fcoe_ctlr *fip = &fcoe->ctlr;
397 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700398 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700399
400 /*
401 * Don't listen for Ethernet packets anymore.
402 * synchronize_net() ensures that the packet handlers are not running
403 * on another CPU. dev_remove_pack() would do that, this calls the
404 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
405 */
406 __dev_remove_pack(&fcoe->fcoe_packet_type);
407 __dev_remove_pack(&fcoe->fip_packet_type);
408 synchronize_net();
409
Chris Leech54b649f2009-08-25 14:00:07 -0700410 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700411 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000412 dev_uc_del(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700413 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000414 dev_uc_del(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700415 if (fip->mode == FIP_MODE_VN2VN) {
416 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
417 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
418 } else
419 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
Yi Zoub7a727f2009-10-21 16:28:03 -0700420
421 /* Tell the LLD we are done w/ FCoE */
422 ops = netdev->netdev_ops;
423 if (ops->ndo_fcoe_disable) {
424 if (ops->ndo_fcoe_disable(netdev))
425 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
426 " specific feature for LLD.\n");
427 }
Chris Leech54b649f2009-08-25 14:00:07 -0700428}
429
430/**
Chris Leech030f4e02009-08-25 14:00:02 -0700431 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800432 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700433 */
434static void fcoe_interface_release(struct kref *kref)
435{
436 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700437 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700438
439 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700440 netdev = fcoe->netdev;
441 /* tear-down the FCoE controller */
442 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700443 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700444 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800445 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700446}
447
448/**
Robert Love1875f272009-11-03 11:47:50 -0800449 * fcoe_interface_get() - Get a reference to a FCoE interface
450 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700451 */
452static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
453{
454 kref_get(&fcoe->kref);
455}
456
457/**
Robert Love1875f272009-11-03 11:47:50 -0800458 * fcoe_interface_put() - Put a reference to a FCoE interface
459 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700460 */
461static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
462{
463 kref_put(&fcoe->kref, fcoe_interface_release);
464}
465
466/**
Robert Love1875f272009-11-03 11:47:50 -0800467 * fcoe_fip_recv() - Handler for received FIP frames
468 * @skb: The receive skb
469 * @netdev: The associated net device
470 * @ptype: The packet_type structure which was used to register this handler
471 * @orig_dev: The original net_device the the skb was received on.
472 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000473 *
474 * Returns: 0 for success
475 */
Robert Love1875f272009-11-03 11:47:50 -0800476static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000477 struct packet_type *ptype,
478 struct net_device *orig_dev)
479{
Chris Leech259ad852009-08-25 13:59:41 -0700480 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000481
Chris Leech259ad852009-08-25 13:59:41 -0700482 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700483 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000484 return 0;
485}
486
487/**
Robert Love1875f272009-11-03 11:47:50 -0800488 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
489 * @fip: The FCoE controller
490 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000491 */
492static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
493{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700494 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000495 dev_queue_xmit(skb);
496}
497
498/**
Robert Love1875f272009-11-03 11:47:50 -0800499 * fcoe_update_src_mac() - Update the Ethernet MAC filters
500 * @lport: The local port to update the source MAC on
501 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000502 *
503 * Remove any previously-set unicast MAC filter.
504 * Add secondary FCoE MAC address filter for our OUI.
505 */
Chris Leech11b56182009-11-03 11:46:29 -0800506static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000507{
Chris Leech11b56182009-11-03 11:46:29 -0800508 struct fcoe_port *port = lport_priv(lport);
509 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000510
Vasu Devab6b85c2009-05-17 12:33:08 +0000511 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800512 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000513 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800514 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000515 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800516 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000517 rtnl_unlock();
518}
519
520/**
Chris Leech11b56182009-11-03 11:46:29 -0800521 * fcoe_get_src_mac() - return the Ethernet source address for an lport
522 * @lport: libfc lport
523 */
524static u8 *fcoe_get_src_mac(struct fc_lport *lport)
525{
526 struct fcoe_port *port = lport_priv(lport);
527
528 return port->data_src_addr;
529}
530
531/**
Robert Love1875f272009-11-03 11:47:50 -0800532 * fcoe_lport_config() - Set up a local port
533 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700534 *
535 * Returns: 0 for success
536 */
Robert Love1875f272009-11-03 11:47:50 -0800537static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700538{
Robert Love1875f272009-11-03 11:47:50 -0800539 lport->link_up = 0;
540 lport->qfull = 0;
541 lport->max_retry_count = 3;
542 lport->max_rport_retry_count = 3;
543 lport->e_d_tov = 2 * 1000; /* FC-FS default */
544 lport->r_a_tov = 2 * 2 * 1000;
545 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
546 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
547 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700548
Robert Love1875f272009-11-03 11:47:50 -0800549 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700550
551 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800552 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700553
554 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800555 lport->crc_offload = 0;
556 lport->seq_offload = 0;
557 lport->lro_enabled = 0;
558 lport->lro_xid = 0;
559 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700560
561 return 0;
562}
563
564/**
Robert Love1875f272009-11-03 11:47:50 -0800565 * fcoe_queue_timer() - The fcoe queue timer
566 * @lport: The local port
Vasu Dev1047f222009-05-06 10:52:40 -0700567 *
568 * Calls fcoe_check_wait_queue on timeout
Vasu Dev1047f222009-05-06 10:52:40 -0700569 */
Robert Love1875f272009-11-03 11:47:50 -0800570static void fcoe_queue_timer(ulong lport)
Vasu Dev1047f222009-05-06 10:52:40 -0700571{
Robert Love1875f272009-11-03 11:47:50 -0800572 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
Vasu Dev1047f222009-05-06 10:52:40 -0700573}
574
575/**
Yi Zoudcece412009-11-20 15:22:21 -0800576 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
577 * @netdev: the associated net device
578 * @wwn: the output WWN
579 * @type: the type of WWN (WWPN or WWNN)
580 *
581 * Returns: 0 for success
582 */
583static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
584{
585 const struct net_device_ops *ops = netdev->netdev_ops;
586
587 if (ops->ndo_fcoe_get_wwn)
588 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
589 return -EINVAL;
590}
591
592/**
Yi Zou54a5b212010-07-20 15:21:17 -0700593 * fcoe_netdev_features_change - Updates the lport's offload flags based
594 * on the LLD netdev's FCoE feature flags
595 */
596static void fcoe_netdev_features_change(struct fc_lport *lport,
597 struct net_device *netdev)
598{
599 mutex_lock(&lport->lp_mutex);
600
601 if (netdev->features & NETIF_F_SG)
602 lport->sg_supp = 1;
603 else
604 lport->sg_supp = 0;
605
606 if (netdev->features & NETIF_F_FCOE_CRC) {
607 lport->crc_offload = 1;
608 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
609 } else {
610 lport->crc_offload = 0;
611 }
612
613 if (netdev->features & NETIF_F_FSO) {
614 lport->seq_offload = 1;
615 lport->lso_max = netdev->gso_max_size;
616 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
617 lport->lso_max);
618 } else {
619 lport->seq_offload = 0;
620 lport->lso_max = 0;
621 }
622
623 if (netdev->fcoe_ddp_xid) {
624 lport->lro_enabled = 1;
625 lport->lro_xid = netdev->fcoe_ddp_xid;
626 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
627 lport->lro_xid);
628 } else {
629 lport->lro_enabled = 0;
630 lport->lro_xid = 0;
631 }
632
633 mutex_unlock(&lport->lp_mutex);
634}
635
636/**
Robert Love1875f272009-11-03 11:47:50 -0800637 * fcoe_netdev_config() - Set up net devive for SW FCoE
638 * @lport: The local port that is associated with the net device
639 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700640 *
Robert Love1875f272009-11-03 11:47:50 -0800641 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700642 *
Robert Love1875f272009-11-03 11:47:50 -0800643 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700644 */
Robert Love1875f272009-11-03 11:47:50 -0800645static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700646{
647 u32 mfs;
648 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700649 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700650 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700651
652 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800653 port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700654 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700655
656 /*
657 * Determine max frame size based on underlying device and optional
658 * user-configured limit. If the MFS is too low, fcoe_link_ok()
659 * will return 0, so do this first.
660 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700661 mfs = netdev->mtu;
662 if (netdev->features & NETIF_F_FCOE_MTU) {
663 mfs = FCOE_MTU;
664 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
665 }
666 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800667 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700668 return -EINVAL;
669
Vasu Dev7f349142009-03-27 09:06:31 -0700670 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700671 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700672
Chris Leech014f5c32009-08-25 13:59:30 -0700673 skb_queue_head_init(&port->fcoe_pending_queue);
674 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800675 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700676
Robert Love5e4f8fe2010-05-07 15:18:35 -0700677 fcoe_link_speed_update(lport);
678
Robert Love1875f272009-11-03 11:47:50 -0800679 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800680 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
681 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800682 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800683 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
684 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700685 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800686 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800687 }
Vasu Dev7f349142009-03-27 09:06:31 -0700688
Vasu Dev7f349142009-03-27 09:06:31 -0700689 return 0;
690}
691
692/**
Robert Love1875f272009-11-03 11:47:50 -0800693 * fcoe_shost_config() - Set up the SCSI host associated with a local port
694 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800695 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700696 *
697 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
698 *
Robert Love1875f272009-11-03 11:47:50 -0800699 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700700 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700701static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700702{
703 int rc = 0;
704
705 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800706 lport->host->max_lun = FCOE_MAX_LUN;
707 lport->host->max_id = FCOE_MAX_FCP_TARGET;
708 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700709 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
710
Robert Love1875f272009-11-03 11:47:50 -0800711 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800712 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800713 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800714 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700715
716 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800717 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700718 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800719 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700720 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700721 return rc;
722 }
Chris Leech9a057532009-11-03 11:46:40 -0800723
Robert Love1875f272009-11-03 11:47:50 -0800724 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700725 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800726
Robert Love1875f272009-11-03 11:47:50 -0800727 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800728 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800729 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700730
731 return 0;
732}
733
Robert Love1875f272009-11-03 11:47:50 -0800734/**
735 * fcoe_oem_match() - The match routine for the offloaded exchange manager
736 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700737 *
Robert Love1875f272009-11-03 11:47:50 -0800738 * This routine will be associated with an exchange manager (EM). When
739 * the libfc exchange handling code is looking for an EM to use it will
740 * call this routine and pass it the frame that it wishes to send. This
741 * routine will return True if the associated EM is to be used and False
742 * if the echange code should continue looking for an EM.
743 *
744 * The offload EM that this routine is associated with will handle any
745 * packets that are for SCSI read requests.
746 *
747 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700748 */
749bool fcoe_oem_match(struct fc_frame *fp)
750{
Yi Zou05cc7392009-08-25 13:59:03 -0700751 return fc_fcp_is_read(fr_fsp(fp)) &&
752 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700753}
754
Vasu Dev7f349142009-03-27 09:06:31 -0700755/**
Robert Love1875f272009-11-03 11:47:50 -0800756 * fcoe_em_config() - Allocate and configure an exchange manager
757 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700758 *
Robert Love1875f272009-11-03 11:47:50 -0800759 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700760 */
Robert Love1875f272009-11-03 11:47:50 -0800761static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700762{
Robert Love1875f272009-11-03 11:47:50 -0800763 struct fcoe_port *port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700764 struct fcoe_interface *fcoe = port->fcoe;
765 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700766 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700767 u16 min_xid = FCOE_MIN_XID;
768 u16 max_xid = FCOE_MAX_XID;
769
770 /*
771 * Check if need to allocate an em instance for
772 * offload exchange ids to be shared across all VN_PORTs/lport.
773 */
Robert Love1875f272009-11-03 11:47:50 -0800774 if (!lport->lro_enabled || !lport->lro_xid ||
775 (lport->lro_xid >= max_xid)) {
776 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700777 goto skip_oem;
778 }
779
780 /*
781 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700782 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700783 */
Chris Leech25024982009-08-25 13:59:35 -0700784 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
785 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700786 else
Chris Leech25024982009-08-25 13:59:35 -0700787 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700788
Chris Leech25024982009-08-25 13:59:35 -0700789 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700790 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
791 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700792 else
Chris Leech25024982009-08-25 13:59:35 -0700793 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700794
795 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700796 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700797 break;
798 }
799 }
800
Chris Leech991cbb62009-08-25 13:59:51 -0700801 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800802 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700803 printk(KERN_ERR "fcoe_em_config: failed to add "
804 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700805 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700806 return -ENOMEM;
807 }
808 } else {
Robert Love1875f272009-11-03 11:47:50 -0800809 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
810 FCOE_MIN_XID, lport->lro_xid,
811 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700812 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700813 printk(KERN_ERR "fcoe_em_config: failed to allocate "
814 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700815 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700816 return -ENOMEM;
817 }
818 }
819
820 /*
821 * Exclude offload EM xid range from next EM xid range.
822 */
Robert Love1875f272009-11-03 11:47:50 -0800823 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700824
825skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800826 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700827 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700828 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700829 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700830 }
Vasu Dev7f349142009-03-27 09:06:31 -0700831
832 return 0;
833}
834
835/**
Robert Love1875f272009-11-03 11:47:50 -0800836 * fcoe_if_destroy() - Tear down a SW FCoE instance
837 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700838 *
839 * Locking: must be called with the RTNL mutex held and RTNL mutex
840 * needed to be dropped by this function since not dropping RTNL
841 * would cause circular locking warning on synchronous fip worker
842 * cancelling thru fcoe_interface_put invoked by this function.
843 *
Vasu Dev7f349142009-03-27 09:06:31 -0700844 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700845static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700846{
Chris Leech014f5c32009-08-25 13:59:30 -0700847 struct fcoe_port *port = lport_priv(lport);
848 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700849 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700850
Robert Loved5488eb2009-06-10 15:30:59 -0700851 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700852
Vasu Dev7f349142009-03-27 09:06:31 -0700853 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700854 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700855
Vasu Dev7f349142009-03-27 09:06:31 -0700856 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700857 fc_lport_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700858
Chris Leech54b649f2009-08-25 14:00:07 -0700859 /* Stop the transmit retry timer */
860 del_timer_sync(&port->timer);
861
862 /* Free existing transmit skbs */
863 fcoe_clean_pending_queue(lport);
864
Chris Leech11b56182009-11-03 11:46:29 -0800865 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000866 dev_uc_del(netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800867 rtnl_unlock();
868
Chris Leech54b649f2009-08-25 14:00:07 -0700869 /* receives may not be stopped until after this */
870 fcoe_interface_put(fcoe);
871
872 /* Free queued packets for the per-CPU receive threads */
873 fcoe_percpu_clean(lport);
874
Vasu Dev7f349142009-03-27 09:06:31 -0700875 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700876 fc_remove_host(lport->host);
877 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700878
Yi Zou80e736f2010-11-30 16:18:07 -0800879 /* Destroy lport scsi_priv */
880 fc_fcp_destroy(lport);
881
Vasu Dev7f349142009-03-27 09:06:31 -0700882 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700883 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700884
Vasu Dev7f349142009-03-27 09:06:31 -0700885 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700886 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700887
Chris Leech2e70e242009-08-25 14:00:23 -0700888 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700889 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700890}
891
Robert Love1875f272009-11-03 11:47:50 -0800892/**
893 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
894 * @lport: The local port to setup DDP for
895 * @xid: The exchange ID for this DDP transfer
896 * @sgl: The scatterlist describing this transfer
897 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700898 *
Robert Love1875f272009-11-03 11:47:50 -0800899 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700900 */
Robert Love1875f272009-11-03 11:47:50 -0800901static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
902 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700903{
Robert Love1875f272009-11-03 11:47:50 -0800904 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700905
Robert Love1875f272009-11-03 11:47:50 -0800906 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
907 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
908 xid, sgl,
909 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700910
911 return 0;
912}
913
Vasu Dev7f349142009-03-27 09:06:31 -0700914/**
Robert Love1875f272009-11-03 11:47:50 -0800915 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
916 * @lport: The local port to complete DDP on
917 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700918 *
Robert Love1875f272009-11-03 11:47:50 -0800919 * Returns: the length of data that have been completed by DDP
920 */
921static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
922{
923 struct net_device *netdev = fcoe_netdev(lport);
924
925 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
926 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
927 return 0;
928}
929
930/**
931 * fcoe_if_create() - Create a FCoE instance on an interface
932 * @fcoe: The FCoE interface to create a local port on
933 * @parent: The device pointer to be the parent in sysfs for the SCSI host
934 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700935 *
Robert Love1875f272009-11-03 11:47:50 -0800936 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
937 *
938 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700939 */
Chris Leech030f4e02009-08-25 14:00:02 -0700940static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800941 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700942{
Robert Love1875f272009-11-03 11:47:50 -0800943 struct net_device *netdev = fcoe->netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700944 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700945 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -0800946 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800947 /*
948 * parent is only a vport if npiv is 1,
949 * but we'll only use vport in that case so go ahead and set it
950 */
951 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700952
Robert Loved5488eb2009-06-10 15:30:59 -0700953 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700954
Chris Leech9a057532009-11-03 11:46:40 -0800955 if (!npiv) {
956 lport = libfc_host_alloc(&fcoe_shost_template,
957 sizeof(struct fcoe_port));
958 } else {
959 lport = libfc_vport_create(vport,
960 sizeof(struct fcoe_port));
961 }
Chris Leech86221962009-11-03 11:46:08 -0800962 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700963 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
964 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700965 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700966 }
Chris Leech014f5c32009-08-25 13:59:30 -0700967 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700968 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700969 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700970 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700971
Robert Love1875f272009-11-03 11:47:50 -0800972 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700973 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700974 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700975 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
976 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700977 goto out_host_put;
978 }
979
Chris Leech9a057532009-11-03 11:46:40 -0800980 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700981 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
982 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -0800983 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800984 fc_set_wwnn(lport, vport->node_name);
985 fc_set_wwpn(lport, vport->port_name);
986 }
987
Vasu Devab6b85c2009-05-17 12:33:08 +0000988 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700989 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000990 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700991 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
992 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700993 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000994 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700995
Vasu Dev7f349142009-03-27 09:06:31 -0700996 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700997 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700998 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700999 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1000 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001001 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001002 }
1003
Vasu Dev7f349142009-03-27 09:06:31 -07001004 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001005 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001006 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001007 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1008 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001009 goto out_lp_destroy;
1010 }
1011
Chris Leech9a057532009-11-03 11:46:40 -08001012 if (!npiv) {
1013 /*
1014 * fcoe_em_alloc() and fcoe_hostlist_add() both
1015 * need to be atomic with respect to other changes to the
1016 * hostlist since fcoe_em_alloc() looks for an existing EM
1017 * instance on host list updated by fcoe_hostlist_add().
1018 *
1019 * This is currently handled through the fcoe_config_mutex
1020 * begin held.
1021 */
Chris Leechc863df32009-08-25 14:00:18 -07001022
Chris Leech9a057532009-11-03 11:46:40 -08001023 /* lport exch manager allocation */
1024 rc = fcoe_em_config(lport);
1025 if (rc) {
1026 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
Robert Love1875f272009-11-03 11:47:50 -08001027 "for the interface\n");
Chris Leech9a057532009-11-03 11:46:40 -08001028 goto out_lp_destroy;
1029 }
Vasu Dev96316092009-07-29 17:05:00 -07001030 }
1031
Chris Leech030f4e02009-08-25 14:00:02 -07001032 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001033 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001034
1035out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001036 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001037out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001038 scsi_host_put(lport->host);
1039out:
1040 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001041}
1042
1043/**
Robert Love1875f272009-11-03 11:47:50 -08001044 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001045 *
Robert Love1875f272009-11-03 11:47:50 -08001046 * Attaches the SW FCoE transport to the FC transport
1047 *
1048 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001049 */
1050static int __init fcoe_if_init(void)
1051{
1052 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001053 fcoe_nport_scsi_transport =
1054 fc_attach_transport(&fcoe_nport_fc_functions);
1055 fcoe_vport_scsi_transport =
1056 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001057
Yi Zou8ca86f82011-01-28 16:05:11 -08001058 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001059 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001060 return -ENODEV;
1061 }
1062
1063 return 0;
1064}
1065
1066/**
Robert Love1875f272009-11-03 11:47:50 -08001067 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001068 *
Robert Love1875f272009-11-03 11:47:50 -08001069 * Detaches the SW FCoE transport from the FC transport
1070 *
1071 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001072 */
1073int __exit fcoe_if_exit(void)
1074{
Yi Zou8ca86f82011-01-28 16:05:11 -08001075 fc_release_transport(fcoe_nport_scsi_transport);
1076 fc_release_transport(fcoe_vport_scsi_transport);
1077 fcoe_nport_scsi_transport = NULL;
1078 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001079 return 0;
1080}
1081
Robert Love85b4aa42008-12-09 15:10:24 -08001082/**
Robert Love1875f272009-11-03 11:47:50 -08001083 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1084 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001085 */
1086static void fcoe_percpu_thread_create(unsigned int cpu)
1087{
1088 struct fcoe_percpu_s *p;
1089 struct task_struct *thread;
1090
1091 p = &per_cpu(fcoe_percpu, cpu);
1092
1093 thread = kthread_create(fcoe_percpu_receive_thread,
1094 (void *)p, "fcoethread/%d", cpu);
1095
Joe Eykholte7a51992009-08-25 14:04:08 -07001096 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001097 kthread_bind(thread, cpu);
1098 wake_up_process(thread);
1099
1100 spin_lock_bh(&p->fcoe_rx_list.lock);
1101 p->thread = thread;
1102 spin_unlock_bh(&p->fcoe_rx_list.lock);
1103 }
1104}
1105
1106/**
Robert Love1875f272009-11-03 11:47:50 -08001107 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1108 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001109 *
1110 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1111 * current CPU's Rx thread. If the thread being destroyed is bound to
1112 * the CPU processing this context the skbs will be freed.
1113 */
1114static void fcoe_percpu_thread_destroy(unsigned int cpu)
1115{
1116 struct fcoe_percpu_s *p;
1117 struct task_struct *thread;
1118 struct page *crc_eof;
1119 struct sk_buff *skb;
1120#ifdef CONFIG_SMP
1121 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001122 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001123#endif /* CONFIG_SMP */
1124
Robert Loved5488eb2009-06-10 15:30:59 -07001125 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001126
1127 /* Prevent any new skbs from being queued for this CPU. */
1128 p = &per_cpu(fcoe_percpu, cpu);
1129 spin_lock_bh(&p->fcoe_rx_list.lock);
1130 thread = p->thread;
1131 p->thread = NULL;
1132 crc_eof = p->crc_eof_page;
1133 p->crc_eof_page = NULL;
1134 p->crc_eof_offset = 0;
1135 spin_unlock_bh(&p->fcoe_rx_list.lock);
1136
1137#ifdef CONFIG_SMP
1138 /*
1139 * Don't bother moving the skb's if this context is running
1140 * on the same CPU that is having its thread destroyed. This
1141 * can easily happen when the module is removed.
1142 */
1143 if (cpu != targ_cpu) {
1144 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1145 spin_lock_bh(&p0->fcoe_rx_list.lock);
1146 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001147 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1148 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001149
1150 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1151 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1152 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1153 } else {
1154 /*
1155 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001156 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001157 * on the CPU that is going offline.
1158 */
1159 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1160 kfree_skb(skb);
1161 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1162 }
1163 } else {
1164 /*
1165 * This scenario occurs when the module is being removed
1166 * and all threads are being destroyed. skbs will continue
1167 * to be shifted from the CPU thread that is being removed
1168 * to the CPU thread associated with the CPU that is processing
1169 * the module removal. Once there is only one CPU Rx thread it
1170 * will reach this case and we will drop all skbs and later
1171 * stop the thread.
1172 */
1173 spin_lock_bh(&p->fcoe_rx_list.lock);
1174 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1175 kfree_skb(skb);
1176 spin_unlock_bh(&p->fcoe_rx_list.lock);
1177 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001178 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001179#else
1180 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001181 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001182 * being removed. Free all skbs and stop the thread.
1183 */
1184 spin_lock_bh(&p->fcoe_rx_list.lock);
1185 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1186 kfree_skb(skb);
1187 spin_unlock_bh(&p->fcoe_rx_list.lock);
1188#endif
1189
1190 if (thread)
1191 kthread_stop(thread);
1192
1193 if (crc_eof)
1194 put_page(crc_eof);
1195}
1196
1197/**
Robert Love1875f272009-11-03 11:47:50 -08001198 * fcoe_cpu_callback() - Handler for CPU hotplug events
1199 * @nfb: The callback data block
1200 * @action: The event triggering the callback
1201 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001202 *
Robert Love1875f272009-11-03 11:47:50 -08001203 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001204 *
1205 * Returns NOTIFY_OK always.
1206 */
1207static int fcoe_cpu_callback(struct notifier_block *nfb,
1208 unsigned long action, void *hcpu)
1209{
1210 unsigned cpu = (unsigned long)hcpu;
1211
1212 switch (action) {
1213 case CPU_ONLINE:
1214 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001215 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001216 fcoe_percpu_thread_create(cpu);
1217 break;
1218 case CPU_DEAD:
1219 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001220 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001221 fcoe_percpu_thread_destroy(cpu);
1222 break;
1223 default:
1224 break;
1225 }
1226 return NOTIFY_OK;
1227}
1228
Robert Love8976f422009-03-17 11:41:46 -07001229/**
Robert Love1875f272009-11-03 11:47:50 -08001230 * fcoe_rcv() - Receive packets from a net device
1231 * @skb: The received packet
1232 * @netdev: The net device that the packet was received on
1233 * @ptype: The packet type context
1234 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001235 *
Robert Love1875f272009-11-03 11:47:50 -08001236 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1237 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001238 *
1239 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001240 */
Robert Love1875f272009-11-03 11:47:50 -08001241int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001242 struct packet_type *ptype, struct net_device *olddev)
1243{
Robert Love1875f272009-11-03 11:47:50 -08001244 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001245 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001246 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001247 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001248 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001249 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001250 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001251
Chris Leech259ad852009-08-25 13:59:41 -07001252 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001253 lport = fcoe->ctlr.lp;
1254 if (unlikely(!lport)) {
1255 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001256 goto err2;
1257 }
Robert Love1875f272009-11-03 11:47:50 -08001258 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001259 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001260
Robert Love1875f272009-11-03 11:47:50 -08001261 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001262 "data:%p tail:%p end:%p sum:%d dev:%s",
1263 skb->len, skb->data_len, skb->head, skb->data,
1264 skb_tail_pointer(skb), skb_end_pointer(skb),
1265 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001266
Vasu Dev519e5132010-07-20 15:19:32 -07001267 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001268
1269 if (is_fip_mode(&fcoe->ctlr) &&
1270 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1271 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1272 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001273 goto err;
1274 }
1275
1276 /*
1277 * Check for minimum frame length, and make sure required FCoE
1278 * and FC headers are pulled into the linear data area.
1279 */
1280 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001281 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001282 goto err;
1283
1284 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1285 fh = (struct fc_frame_header *) skb_transport_header(skb);
1286
Robert Love0ee31cb2010-10-08 17:12:46 -07001287 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1288 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1289 eh->h_dest);
1290 goto err;
1291 }
1292
Robert Love85b4aa42008-12-09 15:10:24 -08001293 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001294 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001295 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001296
Robert Love85b4aa42008-12-09 15:10:24 -08001297 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001298 * In case the incoming frame's exchange is originated from
1299 * the initiator, then received frame's exchange id is ANDed
1300 * with fc_cpu_mask bits to get the same cpu on which exchange
1301 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001302 */
Vasu Devb2f00912009-08-25 13:58:53 -07001303 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1304 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1305 else
1306 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001307
Robert Love8976f422009-03-17 11:41:46 -07001308 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001309 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001310 if (unlikely(!fps->thread)) {
1311 /*
1312 * The targeted CPU is not ready, let's target
1313 * the first CPU now. For non-SMP systems this
1314 * will check the same CPU twice.
1315 */
Robert Love1875f272009-11-03 11:47:50 -08001316 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001317 "ready for incoming skb- using first online "
1318 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001319
1320 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001321 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001322 fps = &per_cpu(fcoe_percpu, cpu);
1323 spin_lock_bh(&fps->fcoe_rx_list.lock);
1324 if (!fps->thread) {
1325 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1326 goto err;
1327 }
1328 }
1329
1330 /*
1331 * We now have a valid CPU that we're targeting for
1332 * this skb. We also have this receive thread locked,
1333 * so we're free to queue skbs into it's queue.
1334 */
Robert Love85b4aa42008-12-09 15:10:24 -08001335
Chris Leech859b7b62009-11-20 14:54:47 -08001336 /* If this is a SCSI-FCP frame, and this is already executing on the
1337 * correct CPU, and the queue for this CPU is empty, then go ahead
1338 * and process the frame directly in the softirq context.
1339 * This lets us process completions without context switching from the
1340 * NET_RX softirq, to our receive processing thread, and then back to
1341 * BLOCK softirq context.
1342 */
1343 if (fh->fh_type == FC_TYPE_FCP &&
1344 cpu == smp_processor_id() &&
1345 skb_queue_empty(&fps->fcoe_rx_list)) {
1346 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1347 fcoe_recv_frame(skb);
1348 } else {
1349 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1350 if (fps->fcoe_rx_list.qlen == 1)
1351 wake_up_process(fps->thread);
1352 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1353 }
Robert Love85b4aa42008-12-09 15:10:24 -08001354
1355 return 0;
1356err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001357 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1358 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001359err2:
1360 kfree_skb(skb);
1361 return -1;
1362}
Robert Love85b4aa42008-12-09 15:10:24 -08001363
1364/**
Robert Love1875f272009-11-03 11:47:50 -08001365 * fcoe_start_io() - Start FCoE I/O
1366 * @skb: The packet to be transmitted
1367 *
1368 * This routine is called from the net device to start transmitting
1369 * FCoE packets.
Robert Love85b4aa42008-12-09 15:10:24 -08001370 *
1371 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001372 */
Robert Love85b4aa42008-12-09 15:10:24 -08001373static inline int fcoe_start_io(struct sk_buff *skb)
1374{
Chris Leech18fa11e2009-11-03 11:50:05 -08001375 struct sk_buff *nskb;
Robert Love85b4aa42008-12-09 15:10:24 -08001376 int rc;
1377
Chris Leech18fa11e2009-11-03 11:50:05 -08001378 nskb = skb_clone(skb, GFP_ATOMIC);
1379 rc = dev_queue_xmit(nskb);
Robert Love85b4aa42008-12-09 15:10:24 -08001380 if (rc != 0)
1381 return rc;
1382 kfree_skb(skb);
1383 return 0;
1384}
1385
1386/**
Robert Love1875f272009-11-03 11:47:50 -08001387 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1388 * @skb: The packet to be transmitted
1389 * @tlen: The total length of the trailer
1390 *
1391 * This routine allocates a page for frame trailers. The page is re-used if
1392 * there is enough room left on it for the current trailer. If there isn't
1393 * enough buffer left a new page is allocated for the trailer. Reference to
1394 * the page from this function as well as the skbs using the page fragments
1395 * ensure that the page is freed at the appropriate time.
Robert Love85b4aa42008-12-09 15:10:24 -08001396 *
1397 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001398 */
Robert Love85b4aa42008-12-09 15:10:24 -08001399static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1400{
1401 struct fcoe_percpu_s *fps;
1402 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001403
Robert Love5e5e92d2009-03-17 11:41:35 -07001404 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001405 page = fps->crc_eof_page;
1406 if (!page) {
1407 page = alloc_page(GFP_ATOMIC);
1408 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001409 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001410 return -ENOMEM;
1411 }
1412 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001413 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001414 }
1415
1416 get_page(page);
1417 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1418 fps->crc_eof_offset, tlen);
1419 skb->len += tlen;
1420 skb->data_len += tlen;
1421 skb->truesize += tlen;
1422 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1423
1424 if (fps->crc_eof_offset >= PAGE_SIZE) {
1425 fps->crc_eof_page = NULL;
1426 fps->crc_eof_offset = 0;
1427 put_page(page);
1428 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001429 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001430 return 0;
1431}
1432
1433/**
Robert Love1875f272009-11-03 11:47:50 -08001434 * fcoe_fc_crc() - Calculates the CRC for a given frame
1435 * @fp: The frame to be checksumed
Robert Love85b4aa42008-12-09 15:10:24 -08001436 *
Robert Love1875f272009-11-03 11:47:50 -08001437 * This uses crc32() routine to calculate the CRC for a frame
1438 *
1439 * Return: The 32 bit CRC value
Robert Love34f42a02009-02-27 10:55:45 -08001440 */
Robert Love85b4aa42008-12-09 15:10:24 -08001441u32 fcoe_fc_crc(struct fc_frame *fp)
1442{
1443 struct sk_buff *skb = fp_skb(fp);
1444 struct skb_frag_struct *frag;
1445 unsigned char *data;
1446 unsigned long off, len, clen;
1447 u32 crc;
1448 unsigned i;
1449
1450 crc = crc32(~0, skb->data, skb_headlen(skb));
1451
1452 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1453 frag = &skb_shinfo(skb)->frags[i];
1454 off = frag->page_offset;
1455 len = frag->size;
1456 while (len > 0) {
1457 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1458 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1459 KM_SKB_DATA_SOFTIRQ);
1460 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1461 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1462 off += clen;
1463 len -= clen;
1464 }
1465 }
1466 return crc;
1467}
Robert Love85b4aa42008-12-09 15:10:24 -08001468
1469/**
Robert Love1875f272009-11-03 11:47:50 -08001470 * fcoe_xmit() - Transmit a FCoE frame
1471 * @lport: The local port that the frame is to be transmitted for
1472 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001473 *
Robert Love1875f272009-11-03 11:47:50 -08001474 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001475 */
Robert Love1875f272009-11-03 11:47:50 -08001476int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001477{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001478 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001479 u32 crc;
1480 struct ethhdr *eh;
1481 struct fcoe_crc_eof *cp;
1482 struct sk_buff *skb;
1483 struct fcoe_dev_stats *stats;
1484 struct fc_frame_header *fh;
1485 unsigned int hlen; /* header length implies the version */
1486 unsigned int tlen; /* trailer length */
1487 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001488 struct fcoe_port *port = lport_priv(lport);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001489 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001490 u8 sof, eof;
1491 struct fcoe_hdr *hp;
1492
1493 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1494
Robert Love85b4aa42008-12-09 15:10:24 -08001495 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001496 skb = fp_skb(fp);
1497 wlen = skb->len / FCOE_WORD_TO_BYTE;
1498
Robert Love1875f272009-11-03 11:47:50 -08001499 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001500 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001501 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001502 }
1503
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001504 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001505 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001506 return 0;
1507
Robert Love85b4aa42008-12-09 15:10:24 -08001508 sof = fr_sof(fp);
1509 eof = fr_eof(fp);
1510
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001511 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001512 hlen = sizeof(struct fcoe_hdr);
1513 tlen = sizeof(struct fcoe_crc_eof);
1514 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1515
1516 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001517 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001518 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001519 skb->csum_start = skb_headroom(skb);
1520 skb->csum_offset = skb->len;
1521 crc = 0;
1522 } else {
1523 skb->ip_summed = CHECKSUM_NONE;
1524 crc = fcoe_fc_crc(fp);
1525 }
1526
Chris Leech014f5c32009-08-25 13:59:30 -07001527 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001528 if (skb_is_nonlinear(skb)) {
1529 skb_frag_t *frag;
1530 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001531 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001532 return -ENOMEM;
1533 }
1534 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1535 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1536 + frag->page_offset;
1537 } else {
1538 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1539 }
1540
1541 memset(cp, 0, sizeof(*cp));
1542 cp->fcoe_eof = eof;
1543 cp->fcoe_crc32 = cpu_to_le32(~crc);
1544
1545 if (skb_is_nonlinear(skb)) {
1546 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1547 cp = NULL;
1548 }
1549
Chris Leech014f5c32009-08-25 13:59:30 -07001550 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001551 skb_push(skb, elen + hlen);
1552 skb_reset_mac_header(skb);
1553 skb_reset_network_header(skb);
1554 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001555 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001556 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001557
1558 /* fill up mac and fcoe headers */
1559 eh = eth_hdr(skb);
1560 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001561 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001562 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001563 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001564
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001565 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1566 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001567 else
Chris Leech11b56182009-11-03 11:46:29 -08001568 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001569
1570 hp = (struct fcoe_hdr *)(eh + 1);
1571 memset(hp, 0, sizeof(*hp));
1572 if (FC_FCOE_VER)
1573 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1574 hp->fcoe_sof = sof;
1575
Yi Zou39ca9a02009-02-27 14:07:15 -08001576 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001577 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001578 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1579 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1580 } else {
1581 skb_shinfo(skb)->gso_type = 0;
1582 skb_shinfo(skb)->gso_size = 0;
1583 }
Robert Love85b4aa42008-12-09 15:10:24 -08001584 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001585 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001586 stats->TxFrames++;
1587 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001588 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001589
1590 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001591 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001592 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001593 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001594 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001595 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001596
1597 return 0;
1598}
Robert Love85b4aa42008-12-09 15:10:24 -08001599
Robert Love34f42a02009-02-27 10:55:45 -08001600/**
Robert Love1875f272009-11-03 11:47:50 -08001601 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1602 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001603 */
1604static void fcoe_percpu_flush_done(struct sk_buff *skb)
1605{
1606 complete(&fcoe_flush_completion);
1607}
1608
1609/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001610 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1611 * @lport: The local port the frame was received on
1612 * @fp: The received frame
1613 *
1614 * Return: 0 on passing filtering checks
1615 */
1616static inline int fcoe_filter_frames(struct fc_lport *lport,
1617 struct fc_frame *fp)
1618{
1619 struct fcoe_interface *fcoe;
1620 struct fc_frame_header *fh;
1621 struct sk_buff *skb = (struct sk_buff *)fp;
1622 struct fcoe_dev_stats *stats;
1623
1624 /*
1625 * We only check CRC if no offload is available and if it is
1626 * it's solicited data, in which case, the FCP layer would
1627 * check it during the copy.
1628 */
1629 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1630 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1631 else
1632 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1633
1634 fh = (struct fc_frame_header *) skb_transport_header(skb);
1635 fh = fc_frame_header_get(fp);
1636 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1637 return 0;
1638
1639 fcoe = ((struct fcoe_port *)lport_priv(lport))->fcoe;
1640 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1641 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1642 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1643 return -EINVAL;
1644 }
1645
1646 if (!fr_flags(fp) & FCPHF_CRC_UNCHECKED ||
1647 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1648 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1649 return 0;
1650 }
1651
1652 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1653 stats->InvalidCRCCount++;
1654 if (stats->InvalidCRCCount < 5)
1655 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1656 return -EINVAL;
1657}
1658
1659/**
Chris Leech859b7b62009-11-20 14:54:47 -08001660 * fcoe_recv_frame() - process a single received frame
1661 * @skb: frame to process
1662 */
1663static void fcoe_recv_frame(struct sk_buff *skb)
1664{
1665 u32 fr_len;
1666 struct fc_lport *lport;
1667 struct fcoe_rcv_info *fr;
1668 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001669 struct fcoe_crc_eof crc_eof;
1670 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001671 struct fcoe_port *port;
1672 struct fcoe_hdr *hp;
1673
1674 fr = fcoe_dev_from_skb(skb);
1675 lport = fr->fr_dev;
1676 if (unlikely(!lport)) {
1677 if (skb->destructor != fcoe_percpu_flush_done)
1678 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1679 kfree_skb(skb);
1680 return;
1681 }
1682
1683 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1684 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1685 skb->len, skb->data_len,
1686 skb->head, skb->data, skb_tail_pointer(skb),
1687 skb_end_pointer(skb), skb->csum,
1688 skb->dev ? skb->dev->name : "<NULL>");
1689
Chris Leech859b7b62009-11-20 14:54:47 -08001690 port = lport_priv(lport);
1691 if (skb_is_nonlinear(skb))
1692 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001693
1694 /*
1695 * Frame length checks and setting up the header pointers
1696 * was done in fcoe_rcv already.
1697 */
1698 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001699
Joe Eykholtf018b732010-03-12 16:08:55 -08001700 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001701 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1702 if (stats->ErrorFrames < 5)
1703 printk(KERN_WARNING "fcoe: FCoE version "
1704 "mismatch: The frame has "
1705 "version %x, but the "
1706 "initiator supports version "
1707 "%x\n", FC_FCOE_DECAPS_VER(hp),
1708 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001709 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001710 }
1711
1712 skb_pull(skb, sizeof(struct fcoe_hdr));
1713 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1714
1715 stats->RxFrames++;
1716 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1717
1718 fp = (struct fc_frame *)skb;
1719 fc_frame_init(fp);
1720 fr_dev(fp) = lport;
1721 fr_sof(fp) = hp->fcoe_sof;
1722
1723 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001724 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1725 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001726 fr_eof(fp) = crc_eof.fcoe_eof;
1727 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001728 if (pskb_trim(skb, fr_len))
1729 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001730
Vasu Dev52ee8322011-01-28 16:03:52 -08001731 if (!fcoe_filter_frames(lport, fp)) {
1732 put_cpu();
1733 fc_exch_recv(lport, fp);
1734 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001735 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001736drop:
1737 stats->ErrorFrames++;
1738 put_cpu();
1739 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001740}
1741
1742/**
Robert Love1875f272009-11-03 11:47:50 -08001743 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1744 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001745 *
1746 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001747 */
1748int fcoe_percpu_receive_thread(void *arg)
1749{
1750 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001751 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001752
Robert Love4469c192009-02-27 10:56:38 -08001753 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001754
1755 while (!kthread_should_stop()) {
1756
1757 spin_lock_bh(&p->fcoe_rx_list.lock);
1758 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1759 set_current_state(TASK_INTERRUPTIBLE);
1760 spin_unlock_bh(&p->fcoe_rx_list.lock);
1761 schedule();
1762 set_current_state(TASK_RUNNING);
1763 if (kthread_should_stop())
1764 return 0;
1765 spin_lock_bh(&p->fcoe_rx_list.lock);
1766 }
1767 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001768 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001769 }
1770 return 0;
1771}
1772
1773/**
Robert Love1875f272009-11-03 11:47:50 -08001774 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1775 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001776 *
Robert Love1875f272009-11-03 11:47:50 -08001777 * This empties the wait_queue, dequeues the head of the wait_queue queue
1778 * and calls fcoe_start_io() for each packet. If all skb have been
1779 * transmitted it returns the qlen. If an error occurs it restores
1780 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001781 *
Robert Love1875f272009-11-03 11:47:50 -08001782 * The wait_queue is used when the skb transmit fails. The failed skb
1783 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001784 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001785 */
Robert Love1875f272009-11-03 11:47:50 -08001786static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001787{
Robert Love1875f272009-11-03 11:47:50 -08001788 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001789 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001790
Chris Leech014f5c32009-08-25 13:59:30 -07001791 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001792
1793 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001794 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001795
Chris Leech014f5c32009-08-25 13:59:30 -07001796 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001797 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001798 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001799
Chris Leech014f5c32009-08-25 13:59:30 -07001800 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001801 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001802 port->fcoe_pending_queue.qlen++;
1803 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001804
Chris Leech014f5c32009-08-25 13:59:30 -07001805 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001806 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001807 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001808
1809 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001810 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001811 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001812 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001813 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001814 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001815 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001816 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001817 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001818
Chris Leech014f5c32009-08-25 13:59:30 -07001819 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001820 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001821 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1822 mod_timer(&port->timer, jiffies + 2);
1823 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001824out:
Chris Leech014f5c32009-08-25 13:59:30 -07001825 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001826 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001827 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001828 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001829}
1830
1831/**
Robert Love1875f272009-11-03 11:47:50 -08001832 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001833 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001834static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001835{
Robert Love85b4aa42008-12-09 15:10:24 -08001836 register_netdevice_notifier(&fcoe_notifier);
1837}
1838
1839/**
Robert Love1875f272009-11-03 11:47:50 -08001840 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001841 */
Robert Love85b4aa42008-12-09 15:10:24 -08001842static void fcoe_dev_cleanup(void)
1843{
1844 unregister_netdevice_notifier(&fcoe_notifier);
1845}
1846
1847/**
Robert Love1875f272009-11-03 11:47:50 -08001848 * fcoe_device_notification() - Handler for net device events
1849 * @notifier: The context of the notification
1850 * @event: The type of event
1851 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001852 *
Robert Love1875f272009-11-03 11:47:50 -08001853 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001854 *
1855 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001856 */
Robert Love85b4aa42008-12-09 15:10:24 -08001857static int fcoe_device_notification(struct notifier_block *notifier,
1858 ulong event, void *ptr)
1859{
Robert Love1875f272009-11-03 11:47:50 -08001860 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001861 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001862 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001863 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001864 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001865 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001866 u32 mfs;
1867 int rc = NOTIFY_OK;
1868
Chris Leech014f5c32009-08-25 13:59:30 -07001869 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001870 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001871 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001872 break;
1873 }
1874 }
Robert Love1875f272009-11-03 11:47:50 -08001875 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001876 rc = NOTIFY_DONE;
1877 goto out;
1878 }
1879
Robert Love85b4aa42008-12-09 15:10:24 -08001880 switch (event) {
1881 case NETDEV_DOWN:
1882 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001883 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001884 break;
1885 case NETDEV_UP:
1886 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001887 break;
1888 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001889 if (netdev->features & NETIF_F_FCOE_MTU)
1890 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001891 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1892 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001893 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001894 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001895 break;
1896 case NETDEV_REGISTER:
1897 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001898 case NETDEV_UNREGISTER:
1899 list_del(&fcoe->list);
1900 port = lport_priv(fcoe->ctlr.lp);
1901 fcoe_interface_cleanup(fcoe);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001902 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001903 goto out;
1904 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001905 case NETDEV_FEAT_CHANGE:
1906 fcoe_netdev_features_change(lport, netdev);
1907 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001908 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001909 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001910 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001911 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001912
1913 fcoe_link_speed_update(lport);
1914
Robert Love1875f272009-11-03 11:47:50 -08001915 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001916 fcoe_ctlr_link_up(&fcoe->ctlr);
1917 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001918 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001919 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001920 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001921 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001922 }
1923out:
1924 return rc;
1925}
1926
1927/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001928 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001929 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001930 *
Yi Zou78a58242011-01-28 16:05:16 -08001931 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001932 *
1933 * Returns: 0 for success
1934 */
Yi Zou78a58242011-01-28 16:05:16 -08001935static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001936{
1937 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001938 int rc = 0;
1939
1940 mutex_lock(&fcoe_config_mutex);
1941#ifdef CONFIG_FCOE_MODULE
1942 /*
1943 * Make sure the module has been initialized, and is not about to be
1944 * removed. Module paramter sysfs files are writable before the
1945 * module_init function is called and after module_exit.
1946 */
1947 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1948 rc = -ENODEV;
1949 goto out_nodev;
1950 }
1951#endif
1952
Vasu Dev34ce27b2010-05-07 15:18:46 -07001953 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001954 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001955 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001956 }
1957
Vasu Dev55a66d32009-12-10 09:59:31 -08001958 fcoe = fcoe_hostlist_lookup_port(netdev);
1959 rtnl_unlock();
1960
Chris Leech9ee50e42010-04-09 14:22:23 -07001961 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001962 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001963 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001964 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001965 rc = -ENODEV;
1966
Vasu Dev55a66d32009-12-10 09:59:31 -08001967out_nodev:
1968 mutex_unlock(&fcoe_config_mutex);
1969 return rc;
1970}
1971
1972/**
1973 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001974 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001975 *
Yi Zou78a58242011-01-28 16:05:16 -08001976 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001977 *
1978 * Returns: 0 for success
1979 */
Yi Zou78a58242011-01-28 16:05:16 -08001980static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001981{
1982 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001983 int rc = 0;
1984
1985 mutex_lock(&fcoe_config_mutex);
1986#ifdef CONFIG_FCOE_MODULE
1987 /*
1988 * Make sure the module has been initialized, and is not about to be
1989 * removed. Module paramter sysfs files are writable before the
1990 * module_init function is called and after module_exit.
1991 */
1992 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1993 rc = -ENODEV;
1994 goto out_nodev;
1995 }
1996#endif
Vasu Dev34ce27b2010-05-07 15:18:46 -07001997 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001998 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001999 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07002000 }
2001
Vasu Dev55a66d32009-12-10 09:59:31 -08002002 fcoe = fcoe_hostlist_lookup_port(netdev);
2003 rtnl_unlock();
2004
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002005 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002006 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002007 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2008 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002009
Vasu Dev55a66d32009-12-10 09:59:31 -08002010out_nodev:
2011 mutex_unlock(&fcoe_config_mutex);
2012 return rc;
2013}
2014
2015/**
Robert Love1875f272009-11-03 11:47:50 -08002016 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002017 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002018 *
Yi Zou78a58242011-01-28 16:05:16 -08002019 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002020 *
2021 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002022 */
Yi Zou78a58242011-01-28 16:05:16 -08002023static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002024{
Chris Leech030f4e02009-08-25 14:00:02 -07002025 struct fcoe_interface *fcoe;
Mike Christie8eca3552009-10-21 16:27:44 -07002026 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002027
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002028 mutex_lock(&fcoe_config_mutex);
2029#ifdef CONFIG_FCOE_MODULE
2030 /*
2031 * Make sure the module has been initialized, and is not about to be
2032 * removed. Module paramter sysfs files are writable before the
2033 * module_init function is called and after module_exit.
2034 */
2035 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2036 rc = -ENODEV;
2037 goto out_nodev;
2038 }
2039#endif
Vasu Dev34ce27b2010-05-07 15:18:46 -07002040 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07002041 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08002042 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07002043 }
2044
Chris Leech2e70e242009-08-25 14:00:23 -07002045 fcoe = fcoe_hostlist_lookup_port(netdev);
2046 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07002047 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002048 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002049 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002050 }
Chris Leech2e70e242009-08-25 14:00:23 -07002051 fcoe_interface_cleanup(fcoe);
Yi Zou54a5b212010-07-20 15:21:17 -07002052 list_del(&fcoe->list);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002053 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002054 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08002055out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002056 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002057 return rc;
2058}
2059
Robert Love1875f272009-11-03 11:47:50 -08002060/**
2061 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2062 * @work: Handle to the FCoE port to be destroyed
2063 */
Chris Leech2e70e242009-08-25 14:00:23 -07002064static void fcoe_destroy_work(struct work_struct *work)
2065{
2066 struct fcoe_port *port;
2067
2068 port = container_of(work, struct fcoe_port, destroy_work);
2069 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002070 rtnl_lock();
2071 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002072 fcoe_if_destroy(port->lport);
2073 mutex_unlock(&fcoe_config_mutex);
2074}
2075
Robert Love85b4aa42008-12-09 15:10:24 -08002076/**
Yi Zou78a58242011-01-28 16:05:16 -08002077 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2078 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002079 *
Yi Zou78a58242011-01-28 16:05:16 -08002080 * Called from fcoe transport.
2081 *
2082 * Returns: always returns true as this is the default FCoE transport,
2083 * i.e., support all netdevs.
2084 */
2085static bool fcoe_match(struct net_device *netdev)
2086{
2087 return true;
2088}
2089
2090/**
2091 * fcoe_create() - Create a fcoe interface
2092 * @netdev : The net_device object the Ethernet interface to create on
2093 * @fip_mode: The FIP mode for this creation
2094 *
2095 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002096 *
2097 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002098 */
Yi Zou78a58242011-01-28 16:05:16 -08002099static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002100{
2101 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07002102 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002103 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002104
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002105 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002106
2107 if (!rtnl_trylock()) {
2108 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08002109 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07002110 }
2111
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002112#ifdef CONFIG_FCOE_MODULE
2113 /*
2114 * Make sure the module has been initialized, and is not about to be
2115 * removed. Module paramter sysfs files are writable before the
2116 * module_init function is called and after module_exit.
2117 */
2118 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2119 rc = -ENODEV;
Robert Love7287fb92011-01-28 16:03:47 -08002120 goto out_nodev;
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002121 }
2122#endif
2123
Robert Love85b4aa42008-12-09 15:10:24 -08002124 /* look for existing lport */
2125 if (fcoe_hostlist_lookup(netdev)) {
2126 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002127 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002128 }
Robert Love85b4aa42008-12-09 15:10:24 -08002129
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002130 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002131 if (IS_ERR(fcoe)) {
2132 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002133 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002134 }
2135
Chris Leech9a057532009-11-03 11:46:40 -08002136 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002137 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002138 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002139 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002140 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002141 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002142 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002143 }
Chris Leech030f4e02009-08-25 14:00:02 -07002144
Chris Leech54b649f2009-08-25 14:00:07 -07002145 /* Make this the "master" N_Port */
2146 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002147
Chris Leechc863df32009-08-25 14:00:18 -07002148 /* add to lports list */
2149 fcoe_hostlist_add(lport);
2150
Chris Leech54b649f2009-08-25 14:00:07 -07002151 /* start FIP Discovery and FLOGI */
2152 lport->boot_time = jiffies;
2153 fc_fabric_login(lport);
2154 if (!fcoe_link_ok(lport))
2155 fcoe_ctlr_link_up(&fcoe->ctlr);
2156
Chris Leech54b649f2009-08-25 14:00:07 -07002157 /*
2158 * Release from init in fcoe_interface_create(), on success lport
2159 * should be holding a reference taken in fcoe_if_create().
2160 */
Chris Leech030f4e02009-08-25 14:00:02 -07002161 fcoe_interface_put(fcoe);
Rob Love6409ea62010-01-21 10:16:05 -08002162 rtnl_unlock();
2163 mutex_unlock(&fcoe_config_mutex);
2164
2165 return 0;
2166out_free:
2167 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002168out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002169 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002170 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002171 return rc;
2172}
2173
Robert Love34f42a02009-02-27 10:55:45 -08002174/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002175 * fcoe_link_speed_update() - Update the supported and actual link speeds
2176 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002177 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002178 * Returns: 0 if the ethtool query was successful
2179 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002180 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002181int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002182{
Robert Love1875f272009-11-03 11:47:50 -08002183 struct fcoe_port *port = lport_priv(lport);
2184 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08002185 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002186
Robert Love5e4f8fe2010-05-07 15:18:35 -07002187 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002188 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002189 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2190 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2191 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002192 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002193 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002194 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002195 FC_PORTSPEED_10GBIT;
2196 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002197 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002198 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002199 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002200
Yi Zou2f718d62009-07-29 17:04:01 -07002201 return 0;
2202 }
2203 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002204}
Robert Love85b4aa42008-12-09 15:10:24 -08002205
Robert Love34f42a02009-02-27 10:55:45 -08002206/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002207 * fcoe_link_ok() - Check if the link is OK for a local port
2208 * @lport: The local port to check link on
2209 *
2210 * Returns: 0 if link is UP and OK, -1 if not
2211 *
2212 */
2213int fcoe_link_ok(struct fc_lport *lport)
2214{
2215 struct fcoe_port *port = lport_priv(lport);
2216 struct net_device *netdev = port->fcoe->netdev;
2217
2218 if (netif_oper_up(netdev))
2219 return 0;
2220 return -1;
2221}
2222
2223/**
Robert Love1875f272009-11-03 11:47:50 -08002224 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2225 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002226 *
2227 * Must be called with fcoe_create_mutex held to single-thread completion.
2228 *
2229 * This flushes the pending skbs by adding a new skb to each queue and
2230 * waiting until they are all freed. This assures us that not only are
2231 * there no packets that will be handled by the lport, but also that any
2232 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002233 */
Robert Love1875f272009-11-03 11:47:50 -08002234void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002235{
Robert Love85b4aa42008-12-09 15:10:24 -08002236 struct fcoe_percpu_s *pp;
2237 struct fcoe_rcv_info *fr;
2238 struct sk_buff_head *list;
2239 struct sk_buff *skb, *next;
2240 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002241 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002242
Robert Love5e5e92d2009-03-17 11:41:35 -07002243 for_each_possible_cpu(cpu) {
2244 pp = &per_cpu(fcoe_percpu, cpu);
2245 spin_lock_bh(&pp->fcoe_rx_list.lock);
2246 list = &pp->fcoe_rx_list;
2247 head = list->next;
2248 for (skb = head; skb != (struct sk_buff *)list;
2249 skb = next) {
2250 next = skb->next;
2251 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002252 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002253 __skb_unlink(skb, list);
2254 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002255 }
Robert Love85b4aa42008-12-09 15:10:24 -08002256 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002257
2258 if (!pp->thread || !cpu_online(cpu)) {
2259 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2260 continue;
2261 }
2262
2263 skb = dev_alloc_skb(0);
2264 if (!skb) {
2265 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2266 continue;
2267 }
2268 skb->destructor = fcoe_percpu_flush_done;
2269
2270 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2271 if (pp->fcoe_rx_list.qlen == 1)
2272 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002273 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002274
2275 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002276 }
2277}
Robert Love85b4aa42008-12-09 15:10:24 -08002278
2279/**
Robert Love34f42a02009-02-27 10:55:45 -08002280 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002281 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002282 */
Robert Love1875f272009-11-03 11:47:50 -08002283void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002284{
Robert Love1875f272009-11-03 11:47:50 -08002285 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002286 struct sk_buff *skb;
2287
Chris Leech014f5c32009-08-25 13:59:30 -07002288 spin_lock_bh(&port->fcoe_pending_queue.lock);
2289 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2290 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002291 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002292 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002293 }
Chris Leech014f5c32009-08-25 13:59:30 -07002294 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002295}
Robert Love85b4aa42008-12-09 15:10:24 -08002296
2297/**
Robert Love1875f272009-11-03 11:47:50 -08002298 * fcoe_reset() - Reset a local port
2299 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002300 *
Robert Love1875f272009-11-03 11:47:50 -08002301 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002302 */
2303int fcoe_reset(struct Scsi_Host *shost)
2304{
2305 struct fc_lport *lport = shost_priv(shost);
2306 fc_lport_reset(lport);
2307 return 0;
2308}
Robert Love85b4aa42008-12-09 15:10:24 -08002309
Robert Love34f42a02009-02-27 10:55:45 -08002310/**
Robert Love1875f272009-11-03 11:47:50 -08002311 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2312 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002313 *
Robert Love1875f272009-11-03 11:47:50 -08002314 * Locking: Must be called with the RNL mutex held.
2315 *
2316 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002317 */
Chris Leech014f5c32009-08-25 13:59:30 -07002318static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002319fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002320{
Chris Leech014f5c32009-08-25 13:59:30 -07002321 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002322
Chris Leech014f5c32009-08-25 13:59:30 -07002323 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002324 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002325 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002326 }
Robert Love85b4aa42008-12-09 15:10:24 -08002327 return NULL;
2328}
2329
Robert Love34f42a02009-02-27 10:55:45 -08002330/**
Robert Love1875f272009-11-03 11:47:50 -08002331 * fcoe_hostlist_lookup() - Find the local port associated with a
2332 * given net device
2333 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002334 *
Robert Love1875f272009-11-03 11:47:50 -08002335 * Locking: Must be called with the RTNL mutex held
2336 *
2337 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002338 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002339static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002340{
Chris Leech014f5c32009-08-25 13:59:30 -07002341 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002342
Chris Leech014f5c32009-08-25 13:59:30 -07002343 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002344 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002345}
Robert Love85b4aa42008-12-09 15:10:24 -08002346
Robert Love34f42a02009-02-27 10:55:45 -08002347/**
Robert Love1875f272009-11-03 11:47:50 -08002348 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2349 * port to the hostlist
2350 * @lport: The local port that identifies the FCoE interface to be added
2351 *
2352 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002353 *
2354 * Returns: 0 for success
2355 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002356static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002357{
Chris Leech014f5c32009-08-25 13:59:30 -07002358 struct fcoe_interface *fcoe;
2359 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002360
Chris Leech014f5c32009-08-25 13:59:30 -07002361 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2362 if (!fcoe) {
2363 port = lport_priv(lport);
2364 fcoe = port->fcoe;
2365 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002366 }
2367 return 0;
2368}
Robert Love85b4aa42008-12-09 15:10:24 -08002369
Yi Zou78a58242011-01-28 16:05:16 -08002370
2371static struct fcoe_transport fcoe_sw_transport = {
2372 .name = {FCOE_TRANSPORT_DEFAULT},
2373 .attached = false,
2374 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2375 .match = fcoe_match,
2376 .create = fcoe_create,
2377 .destroy = fcoe_destroy,
2378 .enable = fcoe_enable,
2379 .disable = fcoe_disable,
2380};
2381
Robert Love34f42a02009-02-27 10:55:45 -08002382/**
Robert Love1875f272009-11-03 11:47:50 -08002383 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002384 *
Robert Love1875f272009-11-03 11:47:50 -08002385 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002386 */
Robert Love85b4aa42008-12-09 15:10:24 -08002387static int __init fcoe_init(void)
2388{
Robert Love1875f272009-11-03 11:47:50 -08002389 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002390 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002391 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002392
Tejun Heo2ca32b42011-01-28 16:05:32 -08002393 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2394 if (!fcoe_wq)
2395 return -ENOMEM;
2396
Yi Zou78a58242011-01-28 16:05:16 -08002397 /* register as a fcoe transport */
2398 rc = fcoe_transport_attach(&fcoe_sw_transport);
2399 if (rc) {
2400 printk(KERN_ERR "failed to register an fcoe transport, check "
2401 "if libfcoe is loaded\n");
2402 return rc;
2403 }
2404
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002405 mutex_lock(&fcoe_config_mutex);
2406
Robert Love38eccab2009-03-17 11:41:30 -07002407 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002408 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002409 skb_queue_head_init(&p->fcoe_rx_list);
2410 }
2411
Robert Love8976f422009-03-17 11:41:46 -07002412 for_each_online_cpu(cpu)
2413 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002414
Robert Love8976f422009-03-17 11:41:46 -07002415 /* Initialize per CPU interrupt thread */
2416 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2417 if (rc)
2418 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002419
Robert Love8976f422009-03-17 11:41:46 -07002420 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002421 fcoe_dev_setup();
2422
Chris Leech5892c322009-08-25 13:59:14 -07002423 rc = fcoe_if_init();
2424 if (rc)
2425 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002426
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002427 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002428 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002429
2430out_free:
2431 for_each_online_cpu(cpu) {
2432 fcoe_percpu_thread_destroy(cpu);
2433 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002434 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002435 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002436 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002437}
2438module_init(fcoe_init);
2439
2440/**
Robert Love1875f272009-11-03 11:47:50 -08002441 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002442 *
Robert Love1875f272009-11-03 11:47:50 -08002443 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002444 */
Robert Love85b4aa42008-12-09 15:10:24 -08002445static void __exit fcoe_exit(void)
2446{
Chris Leech014f5c32009-08-25 13:59:30 -07002447 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002448 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002449 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002450
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002451 mutex_lock(&fcoe_config_mutex);
2452
Robert Love85b4aa42008-12-09 15:10:24 -08002453 fcoe_dev_cleanup();
2454
Vasu Dev5919a592009-03-27 09:03:29 -07002455 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002456 rtnl_lock();
2457 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002458 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002459 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002460 fcoe_interface_cleanup(fcoe);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002461 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002462 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002463 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002464
Robert Love8976f422009-03-17 11:41:46 -07002465 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2466
Chris Leech014f5c32009-08-25 13:59:30 -07002467 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002468 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002469
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002470 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002471
Tejun Heo2ca32b42011-01-28 16:05:32 -08002472 /*
2473 * destroy_work's may be chained but destroy_workqueue()
2474 * can take care of them. Just kill the fcoe_wq.
2475 */
2476 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002477
Tejun Heo2ca32b42011-01-28 16:05:32 -08002478 /*
2479 * Detaching from the scsi transport must happen after all
2480 * destroys are done on the fcoe_wq. destroy_workqueue will
2481 * enusre the fcoe_wq is flushed.
2482 */
Chris Leech2e70e242009-08-25 14:00:23 -07002483 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002484
2485 /* detach from fcoe transport */
2486 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002487}
2488module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002489
2490/**
2491 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2492 * @seq: active sequence in the FLOGI or FDISC exchange
2493 * @fp: response frame, or error encoded in a pointer (timeout)
2494 * @arg: pointer the the fcoe_ctlr structure
2495 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002496 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002497 * the libfc FLOGI response handler.
2498 */
2499static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2500{
2501 struct fcoe_ctlr *fip = arg;
2502 struct fc_exch *exch = fc_seq_exch(seq);
2503 struct fc_lport *lport = exch->lp;
2504 u8 *mac;
2505
2506 if (IS_ERR(fp))
2507 goto done;
2508
2509 mac = fr_cb(fp)->granted_mac;
2510 if (is_zero_ether_addr(mac)) {
2511 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002512 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002513 fc_frame_free(fp);
2514 return;
2515 }
Chris Leech11b56182009-11-03 11:46:29 -08002516 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002517 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002518done:
2519 fc_lport_flogi_resp(seq, fp, lport);
2520}
2521
2522/**
2523 * fcoe_logo_resp() - FCoE specific LOGO response handler
2524 * @seq: active sequence in the LOGO exchange
2525 * @fp: response frame, or error encoded in a pointer (timeout)
2526 * @arg: pointer the the fcoe_ctlr structure
2527 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002528 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002529 * the libfc LOGO response handler.
2530 */
2531static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2532{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002533 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002534 static u8 zero_mac[ETH_ALEN] = { 0 };
2535
2536 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002537 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002538 fc_lport_logo_resp(seq, fp, lport);
2539}
2540
2541/**
2542 * fcoe_elsct_send - FCoE specific ELS handler
2543 *
2544 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2545 * using FCoE specific response handlers and passing the FIP controller as
2546 * the argument (the lport is still available from the exchange).
2547 *
2548 * Most of the work here is just handed off to the libfc routine.
2549 */
Robert Love1875f272009-11-03 11:47:50 -08002550static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2551 struct fc_frame *fp, unsigned int op,
2552 void (*resp)(struct fc_seq *,
2553 struct fc_frame *,
2554 void *),
2555 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002556{
2557 struct fcoe_port *port = lport_priv(lport);
2558 struct fcoe_interface *fcoe = port->fcoe;
2559 struct fcoe_ctlr *fip = &fcoe->ctlr;
2560 struct fc_frame_header *fh = fc_frame_header_get(fp);
2561
2562 switch (op) {
2563 case ELS_FLOGI:
2564 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002565 if (lport->point_to_multipoint)
2566 break;
Chris Leech11b56182009-11-03 11:46:29 -08002567 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2568 fip, timeout);
2569 case ELS_LOGO:
2570 /* only hook onto fabric logouts, not port logouts */
2571 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2572 break;
2573 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002574 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002575 }
2576 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2577}
2578
Chris Leech9a057532009-11-03 11:46:40 -08002579/**
2580 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2581 * @vport: fc_vport object to create a new fc_host for
2582 * @disabled: start the new fc_host in a disabled state by default?
2583 *
2584 * Returns: 0 for success
2585 */
2586static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2587{
2588 struct Scsi_Host *shost = vport_to_shost(vport);
2589 struct fc_lport *n_port = shost_priv(shost);
2590 struct fcoe_port *port = lport_priv(n_port);
2591 struct fcoe_interface *fcoe = port->fcoe;
2592 struct net_device *netdev = fcoe->netdev;
2593 struct fc_lport *vn_port;
2594
2595 mutex_lock(&fcoe_config_mutex);
2596 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2597 mutex_unlock(&fcoe_config_mutex);
2598
2599 if (IS_ERR(vn_port)) {
2600 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2601 netdev->name);
2602 return -EIO;
2603 }
2604
2605 if (disabled) {
2606 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2607 } else {
2608 vn_port->boot_time = jiffies;
2609 fc_fabric_login(vn_port);
2610 fc_vport_setlink(vn_port);
2611 }
2612 return 0;
2613}
2614
2615/**
2616 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2617 * @vport: fc_vport object that is being destroyed
2618 *
2619 * Returns: 0 for success
2620 */
2621static int fcoe_vport_destroy(struct fc_vport *vport)
2622{
2623 struct Scsi_Host *shost = vport_to_shost(vport);
2624 struct fc_lport *n_port = shost_priv(shost);
2625 struct fc_lport *vn_port = vport->dd_data;
2626 struct fcoe_port *port = lport_priv(vn_port);
2627
2628 mutex_lock(&n_port->lp_mutex);
2629 list_del(&vn_port->list);
2630 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002631 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002632 return 0;
2633}
2634
2635/**
2636 * fcoe_vport_disable() - change vport state
2637 * @vport: vport to bring online/offline
2638 * @disable: should the vport be disabled?
2639 */
2640static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2641{
2642 struct fc_lport *lport = vport->dd_data;
2643
2644 if (disable) {
2645 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2646 fc_fabric_logoff(lport);
2647 } else {
2648 lport->boot_time = jiffies;
2649 fc_fabric_login(lport);
2650 fc_vport_setlink(lport);
2651 }
2652
2653 return 0;
2654}
2655
Chris Leechdc8596d2009-11-03 11:47:18 -08002656/**
2657 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2658 * @vport: fc_vport with a new symbolic name string
2659 *
2660 * After generating a new symbolic name string, a new RSPN_ID request is
2661 * sent to the name server. There is no response handler, so if it fails
2662 * for some reason it will not be retried.
2663 */
2664static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2665{
2666 struct fc_lport *lport = vport->dd_data;
2667 struct fc_frame *fp;
2668 size_t len;
2669
2670 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2671 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2672 fcoe_netdev(lport)->name, vport->symbolic_name);
2673
2674 if (lport->state != LPORT_ST_READY)
2675 return;
2676
2677 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2678 fp = fc_frame_alloc(lport,
2679 sizeof(struct fc_ct_hdr) +
2680 sizeof(struct fc_ns_rspn) + len);
2681 if (!fp)
2682 return;
2683 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002684 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002685}
Yi Zoub84056b2009-11-20 14:55:19 -08002686
2687/**
2688 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2689 * @lport: the local port
2690 * @fc_lesb: the link error status block
2691 */
2692static void fcoe_get_lesb(struct fc_lport *lport,
2693 struct fc_els_lesb *fc_lesb)
2694{
2695 unsigned int cpu;
2696 u32 lfc, vlfc, mdac;
2697 struct fcoe_dev_stats *devst;
2698 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002699 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002700 struct net_device *netdev = fcoe_netdev(lport);
2701
2702 lfc = 0;
2703 vlfc = 0;
2704 mdac = 0;
2705 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2706 memset(lesb, 0, sizeof(*lesb));
2707 for_each_possible_cpu(cpu) {
2708 devst = per_cpu_ptr(lport->dev_stats, cpu);
2709 lfc += devst->LinkFailureCount;
2710 vlfc += devst->VLinkFailureCount;
2711 mdac += devst->MissDiscAdvCount;
2712 }
2713 lesb->lesb_link_fail = htonl(lfc);
2714 lesb->lesb_vlink_fail = htonl(vlfc);
2715 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002716 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002717}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002718
2719/**
2720 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2721 * @lport: the local port
2722 * @port_id: the port ID
2723 * @fp: the received frame, if any, that caused the port_id to be set.
2724 *
2725 * This routine handles the case where we received a FLOGI and are
2726 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2727 * so it can set the non-mapped mode and gateway address.
2728 *
2729 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2730 */
2731static void fcoe_set_port_id(struct fc_lport *lport,
2732 u32 port_id, struct fc_frame *fp)
2733{
2734 struct fcoe_port *port = lport_priv(lport);
2735 struct fcoe_interface *fcoe = port->fcoe;
2736
2737 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2738 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2739}