blob: 4efbc17a7d7fd9274e218bd27804601d4d4eae09 [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>
29#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsicam.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_fc.h>
37#include <net/rtnetlink.h>
38
39#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070040#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080041
42#include <scsi/libfc.h>
43#include <scsi/fc_frame.h>
44#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080045
Vasu Devfdd78022009-03-17 11:42:24 -070046#include "fcoe.h"
47
Robert Love85b4aa42008-12-09 15:10:24 -080048MODULE_AUTHOR("Open-FCoE.org");
49MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070050MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080051
Yi Zou05cc7392009-08-25 13:59:03 -070052/* Performance tuning parameters for fcoe */
53static unsigned int fcoe_ddp_min;
54module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
56 "Direct Data Placement (DDP).");
57
Chris Leechdfc1d0f2009-08-25 14:00:13 -070058DEFINE_MUTEX(fcoe_config_mutex);
59
Joe Eykholte7a51992009-08-25 14:04:08 -070060/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61static DECLARE_COMPLETION(fcoe_flush_completion);
62
Robert Love85b4aa42008-12-09 15:10:24 -080063/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070064/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080065LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070066DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080067
Chris Leechdd3fd722009-04-21 16:27:36 -070068/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070069static int fcoe_reset(struct Scsi_Host *shost);
70static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71static int fcoe_rcv(struct sk_buff *, struct net_device *,
72 struct packet_type *, struct net_device *);
73static int fcoe_percpu_receive_thread(void *arg);
74static void fcoe_clean_pending_queue(struct fc_lport *lp);
75static void fcoe_percpu_clean(struct fc_lport *lp);
76static int fcoe_link_ok(struct fc_lport *lp);
77
78static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070080
Vasu Dev4bb6b512009-05-06 10:52:34 -070081static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080082static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83static void fcoe_dev_setup(void);
84static void fcoe_dev_cleanup(void);
Chris Leech2e70e242009-08-25 14:00:23 -070085static struct fcoe_interface *
86 fcoe_hostlist_lookup_port(const struct net_device *dev);
Robert Love85b4aa42008-12-09 15:10:24 -080087
88/* notification function from net device */
89static struct notifier_block fcoe_notifier = {
90 .notifier_call = fcoe_device_notification,
91};
92
Vasu Dev7f349142009-03-27 09:06:31 -070093static struct scsi_transport_template *scsi_transport_fcoe_sw;
94
95struct fc_function_template fcoe_transport_function = {
96 .show_host_node_name = 1,
97 .show_host_port_name = 1,
98 .show_host_supported_classes = 1,
99 .show_host_supported_fc4s = 1,
100 .show_host_active_fc4s = 1,
101 .show_host_maxframe_size = 1,
102
103 .show_host_port_id = 1,
104 .show_host_supported_speeds = 1,
105 .get_host_speed = fc_get_host_speed,
106 .show_host_speed = 1,
107 .show_host_port_type = 1,
108 .get_host_port_state = fc_get_host_port_state,
109 .show_host_port_state = 1,
110 .show_host_symbolic_name = 1,
111
112 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
113 .show_rport_maxframe_size = 1,
114 .show_rport_supported_classes = 1,
115
116 .show_host_fabric_name = 1,
117 .show_starget_node_name = 1,
118 .show_starget_port_name = 1,
119 .show_starget_port_id = 1,
120 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
121 .show_rport_dev_loss_tmo = 1,
122 .get_fc_host_stats = fc_get_host_stats,
123 .issue_fc_host_lip = fcoe_reset,
124
125 .terminate_rport_io = fc_rport_terminate_io,
126};
127
128static struct scsi_host_template fcoe_shost_template = {
129 .module = THIS_MODULE,
130 .name = "FCoE Driver",
131 .proc_name = FCOE_NAME,
132 .queuecommand = fc_queuecommand,
133 .eh_abort_handler = fc_eh_abort,
134 .eh_device_reset_handler = fc_eh_device_reset,
135 .eh_host_reset_handler = fc_eh_host_reset,
136 .slave_alloc = fc_slave_alloc,
137 .change_queue_depth = fc_change_queue_depth,
138 .change_queue_type = fc_change_queue_type,
139 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700140 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700141 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
142 .use_clustering = ENABLE_CLUSTERING,
143 .sg_tablesize = SG_ALL,
144 .max_sectors = 0xffff,
145};
146
Chris Leech54b649f2009-08-25 14:00:07 -0700147static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
148 struct packet_type *ptype,
149 struct net_device *orig_dev);
150/**
151 * fcoe_interface_setup()
152 * @fcoe: new fcoe_interface
153 * @netdev : ptr to the associated netdevice struct
154 *
155 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700156 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700157 */
158static int fcoe_interface_setup(struct fcoe_interface *fcoe,
159 struct net_device *netdev)
160{
161 struct fcoe_ctlr *fip = &fcoe->ctlr;
162 struct netdev_hw_addr *ha;
163 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700164 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700165
166 fcoe->netdev = netdev;
167
Yi Zoub7a727f2009-10-21 16:28:03 -0700168 /* Let LLD initialize for FCoE */
169 ops = netdev->netdev_ops;
170 if (ops->ndo_fcoe_enable) {
171 if (ops->ndo_fcoe_enable(netdev))
172 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
173 " specific feature for LLD.\n");
174 }
175
Chris Leech54b649f2009-08-25 14:00:07 -0700176 /* Do not support for bonding device */
177 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
178 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
179 (netdev->priv_flags & IFF_MASTER_8023AD)) {
180 return -EOPNOTSUPP;
181 }
182
183 /* look for SAN MAC address, if multiple SAN MACs exist, only
184 * use the first one for SPMA */
185 rcu_read_lock();
186 for_each_dev_addr(netdev, ha) {
187 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
188 (is_valid_ether_addr(fip->ctl_src_addr))) {
189 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
190 fip->spma = 1;
191 break;
192 }
193 }
194 rcu_read_unlock();
195
196 /* setup Source Mac Address */
197 if (!fip->spma)
198 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
199
200 /*
201 * Add FCoE MAC address as second unicast MAC address
202 * or enter promiscuous mode if not capable of listening
203 * for multiple unicast MACs.
204 */
Chris Leech54b649f2009-08-25 14:00:07 -0700205 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
206 dev_unicast_add(netdev, flogi_maddr);
207 if (fip->spma)
208 dev_unicast_add(netdev, fip->ctl_src_addr);
209 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700210
211 /*
212 * setup the receive function from ethernet driver
213 * on the ethertype for the given device
214 */
215 fcoe->fcoe_packet_type.func = fcoe_rcv;
216 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
217 fcoe->fcoe_packet_type.dev = netdev;
218 dev_add_pack(&fcoe->fcoe_packet_type);
219
220 fcoe->fip_packet_type.func = fcoe_fip_recv;
221 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
222 fcoe->fip_packet_type.dev = netdev;
223 dev_add_pack(&fcoe->fip_packet_type);
224
225 return 0;
226}
227
228static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
229static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
Chris Leech2e70e242009-08-25 14:00:23 -0700230static void fcoe_destroy_work(struct work_struct *work);
Chris Leech54b649f2009-08-25 14:00:07 -0700231
Vasu Dev7f349142009-03-27 09:06:31 -0700232/**
Chris Leech030f4e02009-08-25 14:00:02 -0700233 * fcoe_interface_create()
234 * @netdev: network interface
235 *
236 * Returns: pointer to a struct fcoe_interface or NULL on error
237 */
238static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
239{
240 struct fcoe_interface *fcoe;
241
242 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
243 if (!fcoe) {
244 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
245 return NULL;
246 }
247
Chris Leech2e70e242009-08-25 14:00:23 -0700248 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700249 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700250
251 /*
252 * Initialize FIP.
253 */
254 fcoe_ctlr_init(&fcoe->ctlr);
255 fcoe->ctlr.send = fcoe_fip_send;
256 fcoe->ctlr.update_mac = fcoe_update_src_mac;
257
258 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700259
260 return fcoe;
261}
262
263/**
Chris Leech54b649f2009-08-25 14:00:07 -0700264 * fcoe_interface_cleanup() - clean up netdev configurations
265 * @fcoe:
Chris Leech2e70e242009-08-25 14:00:23 -0700266 *
267 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700268 */
269void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
270{
271 struct net_device *netdev = fcoe->netdev;
272 struct fcoe_ctlr *fip = &fcoe->ctlr;
273 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700274 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700275
276 /*
277 * Don't listen for Ethernet packets anymore.
278 * synchronize_net() ensures that the packet handlers are not running
279 * on another CPU. dev_remove_pack() would do that, this calls the
280 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
281 */
282 __dev_remove_pack(&fcoe->fcoe_packet_type);
283 __dev_remove_pack(&fcoe->fip_packet_type);
284 synchronize_net();
285
Chris Leech54b649f2009-08-25 14:00:07 -0700286 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700287 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
288 dev_unicast_delete(netdev, flogi_maddr);
289 if (!is_zero_ether_addr(fip->data_src_addr))
290 dev_unicast_delete(netdev, fip->data_src_addr);
291 if (fip->spma)
292 dev_unicast_delete(netdev, fip->ctl_src_addr);
293 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700294
295 /* Tell the LLD we are done w/ FCoE */
296 ops = netdev->netdev_ops;
297 if (ops->ndo_fcoe_disable) {
298 if (ops->ndo_fcoe_disable(netdev))
299 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
300 " specific feature for LLD.\n");
301 }
Chris Leech54b649f2009-08-25 14:00:07 -0700302}
303
304/**
Chris Leech030f4e02009-08-25 14:00:02 -0700305 * fcoe_interface_release() - fcoe_port kref release function
306 * @kref: embedded reference count in an fcoe_interface struct
307 */
308static void fcoe_interface_release(struct kref *kref)
309{
310 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700311 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700312
313 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700314 netdev = fcoe->netdev;
315 /* tear-down the FCoE controller */
316 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700317 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700318 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700319}
320
321/**
322 * fcoe_interface_get()
323 * @fcoe:
324 */
325static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
326{
327 kref_get(&fcoe->kref);
328}
329
330/**
331 * fcoe_interface_put()
332 * @fcoe:
333 */
334static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
335{
336 kref_put(&fcoe->kref, fcoe_interface_release);
337}
338
339/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000340 * fcoe_fip_recv - handle a received FIP frame.
341 * @skb: the receive skb
342 * @dev: associated &net_device
343 * @ptype: the &packet_type structure which was used to register this handler.
344 * @orig_dev: original receive &net_device, in case @dev is a bond.
345 *
346 * Returns: 0 for success
347 */
348static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
349 struct packet_type *ptype,
350 struct net_device *orig_dev)
351{
Chris Leech259ad852009-08-25 13:59:41 -0700352 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000353
Chris Leech259ad852009-08-25 13:59:41 -0700354 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700355 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000356 return 0;
357}
358
359/**
360 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
361 * @fip: FCoE controller.
362 * @skb: FIP Packet.
363 */
364static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
365{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700366 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000367 dev_queue_xmit(skb);
368}
369
370/**
371 * fcoe_update_src_mac() - Update Ethernet MAC filters.
372 * @fip: FCoE controller.
373 * @old: Unicast MAC address to delete if the MAC is non-zero.
374 * @new: Unicast MAC address to add.
375 *
376 * Remove any previously-set unicast MAC filter.
377 * Add secondary FCoE MAC address filter for our OUI.
378 */
379static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
380{
Chris Leech25024982009-08-25 13:59:35 -0700381 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000382
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700383 fcoe = fcoe_from_ctlr(fip);
Vasu Devab6b85c2009-05-17 12:33:08 +0000384 rtnl_lock();
385 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700386 dev_unicast_delete(fcoe->netdev, old);
387 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000388 rtnl_unlock();
389}
390
391/**
Vasu Dev7f349142009-03-27 09:06:31 -0700392 * fcoe_lport_config() - sets up the fc_lport
393 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700394 *
395 * Returns: 0 for success
396 */
397static int fcoe_lport_config(struct fc_lport *lp)
398{
399 lp->link_up = 0;
400 lp->qfull = 0;
401 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700402 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700403 lp->e_d_tov = 2 * 1000; /* FC-FS default */
404 lp->r_a_tov = 2 * 2 * 1000;
405 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
406 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
407
408 fc_lport_init_stats(lp);
409
410 /* lport fc_lport related configuration */
411 fc_lport_config(lp);
412
413 /* offload related configuration */
414 lp->crc_offload = 0;
415 lp->seq_offload = 0;
416 lp->lro_enabled = 0;
417 lp->lro_xid = 0;
418 lp->lso_max = 0;
419
420 return 0;
421}
422
423/**
Vasu Dev1047f222009-05-06 10:52:40 -0700424 * fcoe_queue_timer() - fcoe queue timer
425 * @lp: the fc_lport pointer
426 *
427 * Calls fcoe_check_wait_queue on timeout
428 *
429 */
430static void fcoe_queue_timer(ulong lp)
431{
432 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
433}
434
435/**
Vasu Dev7f349142009-03-27 09:06:31 -0700436 * fcoe_netdev_config() - Set up netdev for SW FCoE
437 * @lp : ptr to the fc_lport
438 * @netdev : ptr to the associated netdevice struct
439 *
440 * Must be called after fcoe_lport_config() as it will use lport mutex
441 *
442 * Returns : 0 for success
443 */
444static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
445{
446 u32 mfs;
447 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700448 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700449 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700450
451 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700452 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700453 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700454
455 /*
456 * Determine max frame size based on underlying device and optional
457 * user-configured limit. If the MFS is too low, fcoe_link_ok()
458 * will return 0, so do this first.
459 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700460 mfs = netdev->mtu;
461 if (netdev->features & NETIF_F_FCOE_MTU) {
462 mfs = FCOE_MTU;
463 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
464 }
465 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700466 if (fc_set_mfs(lp, mfs))
467 return -EINVAL;
468
Vasu Dev7f349142009-03-27 09:06:31 -0700469 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700470 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700471 lp->sg_supp = 1;
472
Vasu Dev7f349142009-03-27 09:06:31 -0700473 if (netdev->features & NETIF_F_FCOE_CRC) {
474 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700475 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700476 }
Vasu Dev7f349142009-03-27 09:06:31 -0700477 if (netdev->features & NETIF_F_FSO) {
478 lp->seq_offload = 1;
479 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700480 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
481 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700482 }
Vasu Dev7f349142009-03-27 09:06:31 -0700483 if (netdev->fcoe_ddp_xid) {
484 lp->lro_enabled = 1;
485 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700486 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
487 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700488 }
Chris Leech014f5c32009-08-25 13:59:30 -0700489 skb_queue_head_init(&port->fcoe_pending_queue);
490 port->fcoe_pending_queue_active = 0;
491 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700492
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700493 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700494 fc_set_wwnn(lp, wwnn);
495 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700496 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700497 fc_set_wwpn(lp, wwpn);
498
Vasu Dev7f349142009-03-27 09:06:31 -0700499 return 0;
500}
501
502/**
503 * fcoe_shost_config() - Sets up fc_lport->host
504 * @lp : ptr to the fc_lport
505 * @shost : ptr to the associated scsi host
506 * @dev : device associated to scsi host
507 *
508 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
509 *
510 * Returns : 0 for success
511 */
512static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
513 struct device *dev)
514{
515 int rc = 0;
516
517 /* lport scsi host config */
518 lp->host = shost;
519
520 lp->host->max_lun = FCOE_MAX_LUN;
521 lp->host->max_id = FCOE_MAX_FCP_TARGET;
522 lp->host->max_channel = 0;
523 lp->host->transportt = scsi_transport_fcoe_sw;
524
525 /* add the new host to the SCSI-ml */
526 rc = scsi_add_host(lp->host, dev);
527 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700528 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
529 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700530 return rc;
531 }
532 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
533 FCOE_NAME, FCOE_VERSION,
534 fcoe_netdev(lp)->name);
535
536 return 0;
537}
538
Vasu Devd7179682009-07-29 17:05:21 -0700539/*
540 * fcoe_oem_match() - match for read types IO
541 * @fp: the fc_frame for new IO.
542 *
543 * Returns : true for read types IO, otherwise returns false.
544 */
545bool fcoe_oem_match(struct fc_frame *fp)
546{
Yi Zou05cc7392009-08-25 13:59:03 -0700547 return fc_fcp_is_read(fr_fsp(fp)) &&
548 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700549}
550
Vasu Dev7f349142009-03-27 09:06:31 -0700551/**
552 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700553 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700554 *
555 * Returns : 0 on success
556 */
557static inline int fcoe_em_config(struct fc_lport *lp)
558{
Chris Leech014f5c32009-08-25 13:59:30 -0700559 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700560 struct fcoe_interface *fcoe = port->fcoe;
561 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700562 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700563 u16 min_xid = FCOE_MIN_XID;
564 u16 max_xid = FCOE_MAX_XID;
565
566 /*
567 * Check if need to allocate an em instance for
568 * offload exchange ids to be shared across all VN_PORTs/lport.
569 */
570 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
571 lp->lro_xid = 0;
572 goto skip_oem;
573 }
574
575 /*
576 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700577 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700578 */
Chris Leech25024982009-08-25 13:59:35 -0700579 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
580 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700581 else
Chris Leech25024982009-08-25 13:59:35 -0700582 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700583
Chris Leech25024982009-08-25 13:59:35 -0700584 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700585 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
586 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700587 else
Chris Leech25024982009-08-25 13:59:35 -0700588 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700589
590 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700591 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700592 break;
593 }
594 }
595
Chris Leech991cbb62009-08-25 13:59:51 -0700596 if (fcoe->oem) {
597 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700598 printk(KERN_ERR "fcoe_em_config: failed to add "
599 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700600 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700601 return -ENOMEM;
602 }
603 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700604 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700605 FCOE_MIN_XID, lp->lro_xid,
606 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700607 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700608 printk(KERN_ERR "fcoe_em_config: failed to allocate "
609 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700610 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700611 return -ENOMEM;
612 }
613 }
614
615 /*
616 * Exclude offload EM xid range from next EM xid range.
617 */
618 min_xid += lp->lro_xid + 1;
619
620skip_oem:
621 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
622 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700623 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700624 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700625 }
Vasu Dev7f349142009-03-27 09:06:31 -0700626
627 return 0;
628}
629
630/**
631 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700632 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700633 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700634static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700635{
Chris Leech014f5c32009-08-25 13:59:30 -0700636 struct fcoe_port *port = lport_priv(lport);
637 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700638 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700639
Robert Loved5488eb2009-06-10 15:30:59 -0700640 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700641
Vasu Dev7f349142009-03-27 09:06:31 -0700642 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700643 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700644
Vasu Dev7f349142009-03-27 09:06:31 -0700645 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700646 fc_lport_destroy(lport);
647 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700648
Chris Leech54b649f2009-08-25 14:00:07 -0700649 /* Stop the transmit retry timer */
650 del_timer_sync(&port->timer);
651
652 /* Free existing transmit skbs */
653 fcoe_clean_pending_queue(lport);
654
655 /* receives may not be stopped until after this */
656 fcoe_interface_put(fcoe);
657
658 /* Free queued packets for the per-CPU receive threads */
659 fcoe_percpu_clean(lport);
660
Vasu Dev7f349142009-03-27 09:06:31 -0700661 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700662 fc_remove_host(lport->host);
663 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700664
665 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700666 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700667
Vasu Dev7f349142009-03-27 09:06:31 -0700668 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700669 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700670
Chris Leech2e70e242009-08-25 14:00:23 -0700671 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700672 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700673}
674
675/*
676 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
677 * @lp: the corresponding fc_lport
678 * @xid: the exchange id for this ddp transfer
679 * @sgl: the scatterlist describing this transfer
680 * @sgc: number of sg items
681 *
682 * Returns : 0 no ddp
683 */
684static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
685 struct scatterlist *sgl, unsigned int sgc)
686{
687 struct net_device *n = fcoe_netdev(lp);
688
Yi Zoub04d0232009-10-21 16:26:55 -0700689 if (n->netdev_ops->ndo_fcoe_ddp_setup)
Vasu Dev7f349142009-03-27 09:06:31 -0700690 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
691
692 return 0;
693}
694
695/*
696 * fcoe_ddp_done - calls LLD's ddp_done through net_device
697 * @lp: the corresponding fc_lport
698 * @xid: the exchange id for this ddp transfer
699 *
700 * Returns : the length of data that have been completed by ddp
701 */
702static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
703{
704 struct net_device *n = fcoe_netdev(lp);
705
Yi Zoub04d0232009-10-21 16:26:55 -0700706 if (n->netdev_ops->ndo_fcoe_ddp_done)
Vasu Dev7f349142009-03-27 09:06:31 -0700707 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
708 return 0;
709}
710
711static struct libfc_function_template fcoe_libfc_fcn_templ = {
712 .frame_send = fcoe_xmit,
713 .ddp_setup = fcoe_ddp_setup,
714 .ddp_done = fcoe_ddp_done,
715};
716
717/**
Chris Leech030f4e02009-08-25 14:00:02 -0700718 * fcoe_if_create() - this function creates the fcoe port
719 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700720 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700721 *
Chris Leech54b649f2009-08-25 14:00:07 -0700722 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700723 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700724 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700725 */
Chris Leech030f4e02009-08-25 14:00:02 -0700726static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700727 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700728{
729 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700730 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700731 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700732 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700733 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700734
Robert Loved5488eb2009-06-10 15:30:59 -0700735 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700736
Chris Leech014f5c32009-08-25 13:59:30 -0700737 shost = libfc_host_alloc(&fcoe_shost_template,
738 sizeof(struct fcoe_port));
739 if (!shost) {
740 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
741 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700742 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700743 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700744 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700745 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700746 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700747 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700748 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700749
750 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700751 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700752 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700753 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
754 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700755 goto out_host_put;
756 }
757
Vasu Devab6b85c2009-05-17 12:33:08 +0000758 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700759 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000760 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700761 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
762 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700763 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000764 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700765
Vasu Dev7f349142009-03-27 09:06:31 -0700766 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700767 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700768 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700769 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
770 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700771 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700772 }
773
Vasu Dev7f349142009-03-27 09:06:31 -0700774 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700775 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700776 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700777 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
778 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700779 goto out_lp_destroy;
780 }
781
Vasu Deve8af4d42009-07-29 17:05:15 -0700782 /*
783 * fcoe_em_alloc() and fcoe_hostlist_add() both
Chris Leechc863df32009-08-25 14:00:18 -0700784 * need to be atomic with respect to other changes to the hostlist
Vasu Deve8af4d42009-07-29 17:05:15 -0700785 * since fcoe_em_alloc() looks for an existing EM
786 * instance on host list updated by fcoe_hostlist_add().
Chris Leechc863df32009-08-25 14:00:18 -0700787 *
788 * This is currently handled through the fcoe_config_mutex begin held.
Vasu Deve8af4d42009-07-29 17:05:15 -0700789 */
Chris Leechc863df32009-08-25 14:00:18 -0700790
Vasu Dev96316092009-07-29 17:05:00 -0700791 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700792 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700793 if (rc) {
794 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
795 "interface\n");
796 goto out_lp_destroy;
797 }
798
Chris Leech030f4e02009-08-25 14:00:02 -0700799 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700800 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700801
802out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700803 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700804out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700805 scsi_host_put(lport->host);
806out:
807 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700808}
809
810/**
811 * fcoe_if_init() - attach to scsi transport
812 *
813 * Returns : 0 on success
814 */
815static int __init fcoe_if_init(void)
816{
817 /* attach to scsi transport */
818 scsi_transport_fcoe_sw =
819 fc_attach_transport(&fcoe_transport_function);
820
821 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700822 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700823 return -ENODEV;
824 }
825
826 return 0;
827}
828
829/**
830 * fcoe_if_exit() - detach from scsi transport
831 *
832 * Returns : 0 on success
833 */
834int __exit fcoe_if_exit(void)
835{
836 fc_release_transport(scsi_transport_fcoe_sw);
Chris Leechdfc1d0f2009-08-25 14:00:13 -0700837 scsi_transport_fcoe_sw = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700838 return 0;
839}
840
Robert Love85b4aa42008-12-09 15:10:24 -0800841/**
Robert Love8976f422009-03-17 11:41:46 -0700842 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
843 * @cpu: cpu index for the online cpu
844 */
845static void fcoe_percpu_thread_create(unsigned int cpu)
846{
847 struct fcoe_percpu_s *p;
848 struct task_struct *thread;
849
850 p = &per_cpu(fcoe_percpu, cpu);
851
852 thread = kthread_create(fcoe_percpu_receive_thread,
853 (void *)p, "fcoethread/%d", cpu);
854
Joe Eykholte7a51992009-08-25 14:04:08 -0700855 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -0700856 kthread_bind(thread, cpu);
857 wake_up_process(thread);
858
859 spin_lock_bh(&p->fcoe_rx_list.lock);
860 p->thread = thread;
861 spin_unlock_bh(&p->fcoe_rx_list.lock);
862 }
863}
864
865/**
866 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
867 * @cpu: cpu index the rx thread is to be removed
868 *
869 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
870 * current CPU's Rx thread. If the thread being destroyed is bound to
871 * the CPU processing this context the skbs will be freed.
872 */
873static void fcoe_percpu_thread_destroy(unsigned int cpu)
874{
875 struct fcoe_percpu_s *p;
876 struct task_struct *thread;
877 struct page *crc_eof;
878 struct sk_buff *skb;
879#ifdef CONFIG_SMP
880 struct fcoe_percpu_s *p0;
881 unsigned targ_cpu = smp_processor_id();
882#endif /* CONFIG_SMP */
883
Robert Loved5488eb2009-06-10 15:30:59 -0700884 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700885
886 /* Prevent any new skbs from being queued for this CPU. */
887 p = &per_cpu(fcoe_percpu, cpu);
888 spin_lock_bh(&p->fcoe_rx_list.lock);
889 thread = p->thread;
890 p->thread = NULL;
891 crc_eof = p->crc_eof_page;
892 p->crc_eof_page = NULL;
893 p->crc_eof_offset = 0;
894 spin_unlock_bh(&p->fcoe_rx_list.lock);
895
896#ifdef CONFIG_SMP
897 /*
898 * Don't bother moving the skb's if this context is running
899 * on the same CPU that is having its thread destroyed. This
900 * can easily happen when the module is removed.
901 */
902 if (cpu != targ_cpu) {
903 p0 = &per_cpu(fcoe_percpu, targ_cpu);
904 spin_lock_bh(&p0->fcoe_rx_list.lock);
905 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700906 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
907 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700908
909 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
910 __skb_queue_tail(&p0->fcoe_rx_list, skb);
911 spin_unlock_bh(&p0->fcoe_rx_list.lock);
912 } else {
913 /*
914 * The targeted CPU is not initialized and cannot accept
915 * new skbs. Unlock the targeted CPU and drop the skbs
916 * on the CPU that is going offline.
917 */
918 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
919 kfree_skb(skb);
920 spin_unlock_bh(&p0->fcoe_rx_list.lock);
921 }
922 } else {
923 /*
924 * This scenario occurs when the module is being removed
925 * and all threads are being destroyed. skbs will continue
926 * to be shifted from the CPU thread that is being removed
927 * to the CPU thread associated with the CPU that is processing
928 * the module removal. Once there is only one CPU Rx thread it
929 * will reach this case and we will drop all skbs and later
930 * stop the thread.
931 */
932 spin_lock_bh(&p->fcoe_rx_list.lock);
933 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
934 kfree_skb(skb);
935 spin_unlock_bh(&p->fcoe_rx_list.lock);
936 }
937#else
938 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700939 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700940 * being removed. Free all skbs and stop the thread.
941 */
942 spin_lock_bh(&p->fcoe_rx_list.lock);
943 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
944 kfree_skb(skb);
945 spin_unlock_bh(&p->fcoe_rx_list.lock);
946#endif
947
948 if (thread)
949 kthread_stop(thread);
950
951 if (crc_eof)
952 put_page(crc_eof);
953}
954
955/**
956 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
957 * @nfb: callback data block
958 * @action: event triggering the callback
959 * @hcpu: index for the cpu of this event
960 *
961 * This creates or destroys per cpu data for fcoe
962 *
963 * Returns NOTIFY_OK always.
964 */
965static int fcoe_cpu_callback(struct notifier_block *nfb,
966 unsigned long action, void *hcpu)
967{
968 unsigned cpu = (unsigned long)hcpu;
969
970 switch (action) {
971 case CPU_ONLINE:
972 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700973 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700974 fcoe_percpu_thread_create(cpu);
975 break;
976 case CPU_DEAD:
977 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700978 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700979 fcoe_percpu_thread_destroy(cpu);
980 break;
981 default:
982 break;
983 }
984 return NOTIFY_OK;
985}
986
987static struct notifier_block fcoe_cpu_notifier = {
988 .notifier_call = fcoe_cpu_callback,
989};
990
991/**
Robert Love34f42a02009-02-27 10:55:45 -0800992 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800993 * @skb: the receive skb
994 * @dev: associated net device
995 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700996 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800997 *
998 * this function will receive the packet and build fc frame and pass it up
999 *
1000 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001001 */
Robert Love85b4aa42008-12-09 15:10:24 -08001002int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1003 struct packet_type *ptype, struct net_device *olddev)
1004{
1005 struct fc_lport *lp;
1006 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001007 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001008 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001009 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001010 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001011
Chris Leech259ad852009-08-25 13:59:41 -07001012 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001013 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001014 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001015 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001016 goto err2;
1017 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001018 if (!lp->link_up)
1019 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001020
Robert Loved5488eb2009-06-10 15:30:59 -07001021 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1022 "data:%p tail:%p end:%p sum:%d dev:%s",
1023 skb->len, skb->data_len, skb->head, skb->data,
1024 skb_tail_pointer(skb), skb_end_pointer(skb),
1025 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001026
1027 /* check for FCOE packet type */
1028 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001029 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001030 goto err;
1031 }
1032
1033 /*
1034 * Check for minimum frame length, and make sure required FCoE
1035 * and FC headers are pulled into the linear data area.
1036 */
1037 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1038 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1039 goto err;
1040
1041 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1042 fh = (struct fc_frame_header *) skb_transport_header(skb);
1043
Robert Love85b4aa42008-12-09 15:10:24 -08001044 fr = fcoe_dev_from_skb(skb);
1045 fr->fr_dev = lp;
1046 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001047
Robert Love85b4aa42008-12-09 15:10:24 -08001048 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001049 * In case the incoming frame's exchange is originated from
1050 * the initiator, then received frame's exchange id is ANDed
1051 * with fc_cpu_mask bits to get the same cpu on which exchange
1052 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001053 */
Vasu Devb2f00912009-08-25 13:58:53 -07001054 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1055 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1056 else
1057 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001058
Robert Love8976f422009-03-17 11:41:46 -07001059 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001060 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001061 if (unlikely(!fps->thread)) {
1062 /*
1063 * The targeted CPU is not ready, let's target
1064 * the first CPU now. For non-SMP systems this
1065 * will check the same CPU twice.
1066 */
Robert Loved5488eb2009-06-10 15:30:59 -07001067 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1068 "ready for incoming skb- using first online "
1069 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001070
1071 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1072 cpu = first_cpu(cpu_online_map);
1073 fps = &per_cpu(fcoe_percpu, cpu);
1074 spin_lock_bh(&fps->fcoe_rx_list.lock);
1075 if (!fps->thread) {
1076 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1077 goto err;
1078 }
1079 }
1080
1081 /*
1082 * We now have a valid CPU that we're targeting for
1083 * this skb. We also have this receive thread locked,
1084 * so we're free to queue skbs into it's queue.
1085 */
Robert Love85b4aa42008-12-09 15:10:24 -08001086 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1087 if (fps->fcoe_rx_list.qlen == 1)
1088 wake_up_process(fps->thread);
1089
1090 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1091
1092 return 0;
1093err:
Robert Love582b45b2009-03-31 15:51:50 -07001094 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001095
1096err2:
1097 kfree_skb(skb);
1098 return -1;
1099}
Robert Love85b4aa42008-12-09 15:10:24 -08001100
1101/**
Robert Love34f42a02009-02-27 10:55:45 -08001102 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001103 * @skb: the skb to be xmitted
1104 *
1105 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001106 */
Robert Love85b4aa42008-12-09 15:10:24 -08001107static inline int fcoe_start_io(struct sk_buff *skb)
1108{
1109 int rc;
1110
1111 skb_get(skb);
1112 rc = dev_queue_xmit(skb);
1113 if (rc != 0)
1114 return rc;
1115 kfree_skb(skb);
1116 return 0;
1117}
1118
1119/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001120 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001121 * @skb: the skb to be xmitted
1122 * @tlen: total len
1123 *
1124 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001125 */
Robert Love85b4aa42008-12-09 15:10:24 -08001126static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1127{
1128 struct fcoe_percpu_s *fps;
1129 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001130
Robert Love5e5e92d2009-03-17 11:41:35 -07001131 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001132 page = fps->crc_eof_page;
1133 if (!page) {
1134 page = alloc_page(GFP_ATOMIC);
1135 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001136 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001137 return -ENOMEM;
1138 }
1139 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001140 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001141 }
1142
1143 get_page(page);
1144 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1145 fps->crc_eof_offset, tlen);
1146 skb->len += tlen;
1147 skb->data_len += tlen;
1148 skb->truesize += tlen;
1149 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1150
1151 if (fps->crc_eof_offset >= PAGE_SIZE) {
1152 fps->crc_eof_page = NULL;
1153 fps->crc_eof_offset = 0;
1154 put_page(page);
1155 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001156 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001157 return 0;
1158}
1159
1160/**
Robert Love34f42a02009-02-27 10:55:45 -08001161 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001162 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001163 *
Chris Leech014f5c32009-08-25 13:59:30 -07001164 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001165 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001166 */
Robert Love85b4aa42008-12-09 15:10:24 -08001167u32 fcoe_fc_crc(struct fc_frame *fp)
1168{
1169 struct sk_buff *skb = fp_skb(fp);
1170 struct skb_frag_struct *frag;
1171 unsigned char *data;
1172 unsigned long off, len, clen;
1173 u32 crc;
1174 unsigned i;
1175
1176 crc = crc32(~0, skb->data, skb_headlen(skb));
1177
1178 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1179 frag = &skb_shinfo(skb)->frags[i];
1180 off = frag->page_offset;
1181 len = frag->size;
1182 while (len > 0) {
1183 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1184 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1185 KM_SKB_DATA_SOFTIRQ);
1186 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1187 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1188 off += clen;
1189 len -= clen;
1190 }
1191 }
1192 return crc;
1193}
Robert Love85b4aa42008-12-09 15:10:24 -08001194
1195/**
Robert Love34f42a02009-02-27 10:55:45 -08001196 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001197 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001198 * @fp: the fc_frame to be transmitted
1199 *
1200 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001201 */
Robert Love85b4aa42008-12-09 15:10:24 -08001202int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1203{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001204 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001205 u32 crc;
1206 struct ethhdr *eh;
1207 struct fcoe_crc_eof *cp;
1208 struct sk_buff *skb;
1209 struct fcoe_dev_stats *stats;
1210 struct fc_frame_header *fh;
1211 unsigned int hlen; /* header length implies the version */
1212 unsigned int tlen; /* trailer length */
1213 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001214 struct fcoe_port *port = lport_priv(lp);
1215 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001216 u8 sof, eof;
1217 struct fcoe_hdr *hp;
1218
1219 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1220
Robert Love85b4aa42008-12-09 15:10:24 -08001221 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001222 skb = fp_skb(fp);
1223 wlen = skb->len / FCOE_WORD_TO_BYTE;
1224
1225 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001226 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001227 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001228 }
1229
Joe Eykholt97c83892009-03-17 11:42:40 -07001230 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001231 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001232 return 0;
1233
Robert Love85b4aa42008-12-09 15:10:24 -08001234 sof = fr_sof(fp);
1235 eof = fr_eof(fp);
1236
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001237 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001238 hlen = sizeof(struct fcoe_hdr);
1239 tlen = sizeof(struct fcoe_crc_eof);
1240 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1241
1242 /* crc offload */
1243 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001244 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001245 skb->csum_start = skb_headroom(skb);
1246 skb->csum_offset = skb->len;
1247 crc = 0;
1248 } else {
1249 skb->ip_summed = CHECKSUM_NONE;
1250 crc = fcoe_fc_crc(fp);
1251 }
1252
Chris Leech014f5c32009-08-25 13:59:30 -07001253 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001254 if (skb_is_nonlinear(skb)) {
1255 skb_frag_t *frag;
1256 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001257 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001258 return -ENOMEM;
1259 }
1260 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1261 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1262 + frag->page_offset;
1263 } else {
1264 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1265 }
1266
1267 memset(cp, 0, sizeof(*cp));
1268 cp->fcoe_eof = eof;
1269 cp->fcoe_crc32 = cpu_to_le32(~crc);
1270
1271 if (skb_is_nonlinear(skb)) {
1272 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1273 cp = NULL;
1274 }
1275
Chris Leech014f5c32009-08-25 13:59:30 -07001276 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001277 skb_push(skb, elen + hlen);
1278 skb_reset_mac_header(skb);
1279 skb_reset_network_header(skb);
1280 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001281 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001282 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001283
1284 /* fill up mac and fcoe headers */
1285 eh = eth_hdr(skb);
1286 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001287 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001288 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1289 else
1290 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001291 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001292
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001293 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1294 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001295 else
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001296 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001297
1298 hp = (struct fcoe_hdr *)(eh + 1);
1299 memset(hp, 0, sizeof(*hp));
1300 if (FC_FCOE_VER)
1301 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1302 hp->fcoe_sof = sof;
1303
Yi Zou39ca9a02009-02-27 14:07:15 -08001304 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1305 if (lp->seq_offload && fr_max_payload(fp)) {
1306 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1307 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1308 } else {
1309 skb_shinfo(skb)->gso_type = 0;
1310 skb_shinfo(skb)->gso_size = 0;
1311 }
Robert Love85b4aa42008-12-09 15:10:24 -08001312 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001313 stats = fc_lport_get_stats(lp);
1314 stats->TxFrames++;
1315 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001316
1317 /* send down to lld */
1318 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001319 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001320 fcoe_check_wait_queue(lp, skb);
1321 else if (fcoe_start_io(skb))
1322 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001323
1324 return 0;
1325}
Robert Love85b4aa42008-12-09 15:10:24 -08001326
Robert Love34f42a02009-02-27 10:55:45 -08001327/**
Joe Eykholte7a51992009-08-25 14:04:08 -07001328 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1329 * @skb: the skb being completed.
1330 */
1331static void fcoe_percpu_flush_done(struct sk_buff *skb)
1332{
1333 complete(&fcoe_flush_completion);
1334}
1335
1336/**
Robert Love34f42a02009-02-27 10:55:45 -08001337 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001338 * @arg: ptr to the fcoe per cpu struct
1339 *
1340 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001341 */
1342int fcoe_percpu_receive_thread(void *arg)
1343{
1344 struct fcoe_percpu_s *p = arg;
1345 u32 fr_len;
1346 struct fc_lport *lp;
1347 struct fcoe_rcv_info *fr;
1348 struct fcoe_dev_stats *stats;
1349 struct fc_frame_header *fh;
1350 struct sk_buff *skb;
1351 struct fcoe_crc_eof crc_eof;
1352 struct fc_frame *fp;
1353 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001354 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001355 struct fcoe_hdr *hp;
1356
Robert Love4469c192009-02-27 10:56:38 -08001357 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001358
1359 while (!kthread_should_stop()) {
1360
1361 spin_lock_bh(&p->fcoe_rx_list.lock);
1362 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1363 set_current_state(TASK_INTERRUPTIBLE);
1364 spin_unlock_bh(&p->fcoe_rx_list.lock);
1365 schedule();
1366 set_current_state(TASK_RUNNING);
1367 if (kthread_should_stop())
1368 return 0;
1369 spin_lock_bh(&p->fcoe_rx_list.lock);
1370 }
1371 spin_unlock_bh(&p->fcoe_rx_list.lock);
1372 fr = fcoe_dev_from_skb(skb);
1373 lp = fr->fr_dev;
1374 if (unlikely(lp == NULL)) {
Joe Eykholte7a51992009-08-25 14:04:08 -07001375 if (skb->destructor != fcoe_percpu_flush_done)
1376 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
Robert Love85b4aa42008-12-09 15:10:24 -08001377 kfree_skb(skb);
1378 continue;
1379 }
1380
Robert Loved5488eb2009-06-10 15:30:59 -07001381 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1382 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1383 skb->len, skb->data_len,
1384 skb->head, skb->data, skb_tail_pointer(skb),
1385 skb_end_pointer(skb), skb->csum,
1386 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001387
1388 /*
1389 * Save source MAC address before discarding header.
1390 */
Chris Leech014f5c32009-08-25 13:59:30 -07001391 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001392 if (skb_is_nonlinear(skb))
1393 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001394 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001395
1396 /*
1397 * Frame length checks and setting up the header pointers
1398 * was done in fcoe_rcv already.
1399 */
1400 hp = (struct fcoe_hdr *) skb_network_header(skb);
1401 fh = (struct fc_frame_header *) skb_transport_header(skb);
1402
Robert Love582b45b2009-03-31 15:51:50 -07001403 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001404 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001405 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001406 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001407 "mismatch: The frame has "
1408 "version %x, but the "
1409 "initiator supports version "
1410 "%x\n", FC_FCOE_DECAPS_VER(hp),
1411 FC_FCOE_VER);
1412 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001413 kfree_skb(skb);
1414 continue;
1415 }
1416
1417 skb_pull(skb, sizeof(struct fcoe_hdr));
1418 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1419
Robert Love582b45b2009-03-31 15:51:50 -07001420 stats->RxFrames++;
1421 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001422
1423 fp = (struct fc_frame *)skb;
1424 fc_frame_init(fp);
1425 fr_dev(fp) = lp;
1426 fr_sof(fp) = hp->fcoe_sof;
1427
1428 /* Copy out the CRC and EOF trailer for access */
1429 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1430 kfree_skb(skb);
1431 continue;
1432 }
1433 fr_eof(fp) = crc_eof.fcoe_eof;
1434 fr_crc(fp) = crc_eof.fcoe_crc32;
1435 if (pskb_trim(skb, fr_len)) {
1436 kfree_skb(skb);
1437 continue;
1438 }
1439
1440 /*
1441 * We only check CRC if no offload is available and if it is
1442 * it's solicited data, in which case, the FCP layer would
1443 * check it during the copy.
1444 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001445 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001446 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1447 else
1448 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1449
1450 fh = fc_frame_header_get(fp);
1451 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1452 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001453 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001454 continue;
1455 }
1456 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1457 if (le32_to_cpu(fr_crc(fp)) !=
1458 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001459 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001460 printk(KERN_WARNING "fcoe: dropping "
1461 "frame with CRC error\n");
1462 stats->InvalidCRCCount++;
1463 stats->ErrorFrames++;
1464 fc_frame_free(fp);
1465 continue;
1466 }
1467 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1468 }
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001469 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1470 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001471 fc_frame_free(fp);
1472 continue;
1473 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001474 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001475 }
1476 return 0;
1477}
1478
1479/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001480 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1481 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001482 *
1483 * This empties the wait_queue, dequeue the head of the wait_queue queue
1484 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001485 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001486 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001487 *
1488 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001489 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001490 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001491 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001492static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001493{
Chris Leech014f5c32009-08-25 13:59:30 -07001494 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001495 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001496
Chris Leech014f5c32009-08-25 13:59:30 -07001497 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001498
1499 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001500 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001501
Chris Leech014f5c32009-08-25 13:59:30 -07001502 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001503 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001504 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001505
Chris Leech014f5c32009-08-25 13:59:30 -07001506 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001507 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001508 port->fcoe_pending_queue.qlen++;
1509 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001510
Chris Leech014f5c32009-08-25 13:59:30 -07001511 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001512 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001513 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001514
1515 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001516 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001517 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001518 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001519 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001520 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001521 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001522 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001523 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001524
Chris Leech014f5c32009-08-25 13:59:30 -07001525 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001526 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001527 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1528 mod_timer(&port->timer, jiffies + 2);
1529 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001530out:
Chris Leech014f5c32009-08-25 13:59:30 -07001531 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001532 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001533 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001534 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001535}
1536
1537/**
Robert Love34f42a02009-02-27 10:55:45 -08001538 * fcoe_dev_setup() - setup link change notification interface
1539 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001540static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001541{
Robert Love85b4aa42008-12-09 15:10:24 -08001542 register_netdevice_notifier(&fcoe_notifier);
1543}
1544
1545/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001546 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001547 */
Robert Love85b4aa42008-12-09 15:10:24 -08001548static void fcoe_dev_cleanup(void)
1549{
1550 unregister_netdevice_notifier(&fcoe_notifier);
1551}
1552
1553/**
Robert Love34f42a02009-02-27 10:55:45 -08001554 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001555 * @notifier: context of the notification
1556 * @event: type of event
1557 * @ptr: fixed array for output parsed ifname
1558 *
1559 * This function is called by the ethernet driver in case of link change event
1560 *
1561 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001562 */
Robert Love85b4aa42008-12-09 15:10:24 -08001563static int fcoe_device_notification(struct notifier_block *notifier,
1564 ulong event, void *ptr)
1565{
1566 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001567 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001568 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001569 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001570 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001571 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001572 u32 mfs;
1573 int rc = NOTIFY_OK;
1574
Chris Leech014f5c32009-08-25 13:59:30 -07001575 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001576 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001577 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001578 break;
1579 }
1580 }
Robert Love85b4aa42008-12-09 15:10:24 -08001581 if (lp == NULL) {
1582 rc = NOTIFY_DONE;
1583 goto out;
1584 }
1585
Robert Love85b4aa42008-12-09 15:10:24 -08001586 switch (event) {
1587 case NETDEV_DOWN:
1588 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001589 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001590 break;
1591 case NETDEV_UP:
1592 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001593 break;
1594 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001595 if (netdev->features & NETIF_F_FCOE_MTU)
1596 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001597 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1598 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001599 if (mfs >= FC_MIN_MAX_FRAME)
1600 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001601 break;
1602 case NETDEV_REGISTER:
1603 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001604 case NETDEV_UNREGISTER:
1605 list_del(&fcoe->list);
1606 port = lport_priv(fcoe->ctlr.lp);
1607 fcoe_interface_cleanup(fcoe);
1608 schedule_work(&port->destroy_work);
1609 goto out;
1610 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001611 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001612 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001613 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001614 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001615 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001616 fcoe_ctlr_link_up(&fcoe->ctlr);
1617 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001618 stats = fc_lport_get_stats(lp);
1619 stats->LinkFailureCount++;
1620 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001621 }
1622out:
1623 return rc;
1624}
1625
1626/**
Robert Love34f42a02009-02-27 10:55:45 -08001627 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001628 * @buffer: incoming buffer to be copied
1629 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001630 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001631 */
Robert Love85b4aa42008-12-09 15:10:24 -08001632static struct net_device *fcoe_if_to_netdev(const char *buffer)
1633{
1634 char *cp;
1635 char ifname[IFNAMSIZ + 2];
1636
1637 if (buffer) {
1638 strlcpy(ifname, buffer, IFNAMSIZ);
1639 cp = ifname + strlen(ifname);
1640 while (--cp >= ifname && *cp == '\n')
1641 *cp = '\0';
1642 return dev_get_by_name(&init_net, ifname);
1643 }
1644 return NULL;
1645}
1646
1647/**
Robert Love34f42a02009-02-27 10:55:45 -08001648 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001649 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001650 * @kp: associated kernel param
1651 *
1652 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001653 */
Robert Love85b4aa42008-12-09 15:10:24 -08001654static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1655{
Chris Leech030f4e02009-08-25 14:00:02 -07001656 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001657 struct net_device *netdev;
Mike Christie8eca3552009-10-21 16:27:44 -07001658 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001659
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001660 mutex_lock(&fcoe_config_mutex);
1661#ifdef CONFIG_FCOE_MODULE
1662 /*
1663 * Make sure the module has been initialized, and is not about to be
1664 * removed. Module paramter sysfs files are writable before the
1665 * module_init function is called and after module_exit.
1666 */
1667 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1668 rc = -ENODEV;
1669 goto out_nodev;
1670 }
1671#endif
1672
Robert Love85b4aa42008-12-09 15:10:24 -08001673 netdev = fcoe_if_to_netdev(buffer);
1674 if (!netdev) {
1675 rc = -ENODEV;
1676 goto out_nodev;
1677 }
Chris Leech2e70e242009-08-25 14:00:23 -07001678
Chris Leech090eb6c2009-08-25 14:00:28 -07001679 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001680 fcoe = fcoe_hostlist_lookup_port(netdev);
1681 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001682 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001683 rc = -ENODEV;
1684 goto out_putdev;
1685 }
Chris Leech2e70e242009-08-25 14:00:23 -07001686 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001687 fcoe_interface_cleanup(fcoe);
1688 rtnl_unlock();
1689 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001690out_putdev:
1691 dev_put(netdev);
1692out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001693 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001694 return rc;
1695}
1696
Chris Leech2e70e242009-08-25 14:00:23 -07001697static void fcoe_destroy_work(struct work_struct *work)
1698{
1699 struct fcoe_port *port;
1700
1701 port = container_of(work, struct fcoe_port, destroy_work);
1702 mutex_lock(&fcoe_config_mutex);
1703 fcoe_if_destroy(port->lport);
1704 mutex_unlock(&fcoe_config_mutex);
1705}
1706
Robert Love85b4aa42008-12-09 15:10:24 -08001707/**
Robert Love34f42a02009-02-27 10:55:45 -08001708 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001709 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001710 * @kp: associated kernel param
1711 *
1712 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001713 */
Robert Love85b4aa42008-12-09 15:10:24 -08001714static int fcoe_create(const char *buffer, struct kernel_param *kp)
1715{
1716 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001717 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001718 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001719 struct net_device *netdev;
1720
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001721 mutex_lock(&fcoe_config_mutex);
1722#ifdef CONFIG_FCOE_MODULE
1723 /*
1724 * Make sure the module has been initialized, and is not about to be
1725 * removed. Module paramter sysfs files are writable before the
1726 * module_init function is called and after module_exit.
1727 */
1728 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1729 rc = -ENODEV;
1730 goto out_nodev;
1731 }
1732#endif
1733
Chris Leech2e70e242009-08-25 14:00:23 -07001734 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001735 netdev = fcoe_if_to_netdev(buffer);
1736 if (!netdev) {
1737 rc = -ENODEV;
1738 goto out_nodev;
1739 }
Chris Leech2e70e242009-08-25 14:00:23 -07001740
Robert Love85b4aa42008-12-09 15:10:24 -08001741 /* look for existing lport */
1742 if (fcoe_hostlist_lookup(netdev)) {
1743 rc = -EEXIST;
1744 goto out_putdev;
1745 }
Robert Love85b4aa42008-12-09 15:10:24 -08001746
Chris Leech030f4e02009-08-25 14:00:02 -07001747 fcoe = fcoe_interface_create(netdev);
1748 if (!fcoe) {
1749 rc = -ENOMEM;
1750 goto out_putdev;
1751 }
1752
1753 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001754 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001755 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001756 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001757 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001758 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001759 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001760 }
Chris Leech030f4e02009-08-25 14:00:02 -07001761
Chris Leech54b649f2009-08-25 14:00:07 -07001762 /* Make this the "master" N_Port */
1763 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001764
Chris Leechc863df32009-08-25 14:00:18 -07001765 /* add to lports list */
1766 fcoe_hostlist_add(lport);
1767
Chris Leech54b649f2009-08-25 14:00:07 -07001768 /* start FIP Discovery and FLOGI */
1769 lport->boot_time = jiffies;
1770 fc_fabric_login(lport);
1771 if (!fcoe_link_ok(lport))
1772 fcoe_ctlr_link_up(&fcoe->ctlr);
1773
1774 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001775out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001776 /*
1777 * Release from init in fcoe_interface_create(), on success lport
1778 * should be holding a reference taken in fcoe_if_create().
1779 */
Chris Leech030f4e02009-08-25 14:00:02 -07001780 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001781out_putdev:
1782 dev_put(netdev);
1783out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001784 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001785 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001786 return rc;
1787}
1788
1789module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1790__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001791MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001792module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1793__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001794MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001795
Robert Love34f42a02009-02-27 10:55:45 -08001796/**
1797 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001798 * @lp: ptr to the fc_lport
1799 *
1800 * Any permanently-disqualifying conditions have been previously checked.
1801 * This also updates the speed setting, which may change with link for 100/1000.
1802 *
1803 * This function should probably be checking for PAUSE support at some point
1804 * in the future. Currently Per-priority-pause is not determinable using
1805 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1806 *
1807 * Returns: 0 if link is OK for use by FCoE.
1808 *
1809 */
1810int fcoe_link_ok(struct fc_lport *lp)
1811{
Chris Leech014f5c32009-08-25 13:59:30 -07001812 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001813 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001814 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001815
Yi Zou2f718d62009-07-29 17:04:01 -07001816 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1817 (!dev_ethtool_get_settings(dev, &ecmd))) {
1818 lp->link_supported_speeds &=
1819 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1820 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1821 SUPPORTED_1000baseT_Full))
1822 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1823 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1824 lp->link_supported_speeds |=
1825 FC_PORTSPEED_10GBIT;
1826 if (ecmd.speed == SPEED_1000)
1827 lp->link_speed = FC_PORTSPEED_1GBIT;
1828 if (ecmd.speed == SPEED_10000)
1829 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001830
Yi Zou2f718d62009-07-29 17:04:01 -07001831 return 0;
1832 }
1833 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001834}
Robert Love85b4aa42008-12-09 15:10:24 -08001835
Robert Love34f42a02009-02-27 10:55:45 -08001836/**
1837 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001838 * @lp: the fc_lport
Joe Eykholte7a51992009-08-25 14:04:08 -07001839 *
1840 * Must be called with fcoe_create_mutex held to single-thread completion.
1841 *
1842 * This flushes the pending skbs by adding a new skb to each queue and
1843 * waiting until they are all freed. This assures us that not only are
1844 * there no packets that will be handled by the lport, but also that any
1845 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08001846 */
1847void fcoe_percpu_clean(struct fc_lport *lp)
1848{
Robert Love85b4aa42008-12-09 15:10:24 -08001849 struct fcoe_percpu_s *pp;
1850 struct fcoe_rcv_info *fr;
1851 struct sk_buff_head *list;
1852 struct sk_buff *skb, *next;
1853 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001854 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001855
Robert Love5e5e92d2009-03-17 11:41:35 -07001856 for_each_possible_cpu(cpu) {
1857 pp = &per_cpu(fcoe_percpu, cpu);
1858 spin_lock_bh(&pp->fcoe_rx_list.lock);
1859 list = &pp->fcoe_rx_list;
1860 head = list->next;
1861 for (skb = head; skb != (struct sk_buff *)list;
1862 skb = next) {
1863 next = skb->next;
1864 fr = fcoe_dev_from_skb(skb);
1865 if (fr->fr_dev == lp) {
1866 __skb_unlink(skb, list);
1867 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001868 }
Robert Love85b4aa42008-12-09 15:10:24 -08001869 }
Joe Eykholte7a51992009-08-25 14:04:08 -07001870
1871 if (!pp->thread || !cpu_online(cpu)) {
1872 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1873 continue;
1874 }
1875
1876 skb = dev_alloc_skb(0);
1877 if (!skb) {
1878 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1879 continue;
1880 }
1881 skb->destructor = fcoe_percpu_flush_done;
1882
1883 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1884 if (pp->fcoe_rx_list.qlen == 1)
1885 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07001886 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07001887
1888 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08001889 }
1890}
Robert Love85b4aa42008-12-09 15:10:24 -08001891
1892/**
Robert Love34f42a02009-02-27 10:55:45 -08001893 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001894 * @lp: the corresponding fc_lport
1895 *
1896 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001897 */
Robert Love85b4aa42008-12-09 15:10:24 -08001898void fcoe_clean_pending_queue(struct fc_lport *lp)
1899{
Chris Leech014f5c32009-08-25 13:59:30 -07001900 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001901 struct sk_buff *skb;
1902
Chris Leech014f5c32009-08-25 13:59:30 -07001903 spin_lock_bh(&port->fcoe_pending_queue.lock);
1904 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1905 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001906 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001907 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001908 }
Chris Leech014f5c32009-08-25 13:59:30 -07001909 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001910}
Robert Love85b4aa42008-12-09 15:10:24 -08001911
1912/**
Robert Love34f42a02009-02-27 10:55:45 -08001913 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001914 * @shost: shost the reset is from
1915 *
1916 * Returns: always 0
1917 */
1918int fcoe_reset(struct Scsi_Host *shost)
1919{
1920 struct fc_lport *lport = shost_priv(shost);
1921 fc_lport_reset(lport);
1922 return 0;
1923}
Robert Love85b4aa42008-12-09 15:10:24 -08001924
Robert Love34f42a02009-02-27 10:55:45 -08001925/**
Chris Leech014f5c32009-08-25 13:59:30 -07001926 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001927 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001928 *
Chris Leech014f5c32009-08-25 13:59:30 -07001929 * Returns: NULL or the located fcoe_port
Chris Leech090eb6c2009-08-25 14:00:28 -07001930 * Locking: must be called with the RNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001931 */
Chris Leech014f5c32009-08-25 13:59:30 -07001932static struct fcoe_interface *
1933fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001934{
Chris Leech014f5c32009-08-25 13:59:30 -07001935 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001936
Chris Leech014f5c32009-08-25 13:59:30 -07001937 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001938 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001939 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001940 }
Robert Love85b4aa42008-12-09 15:10:24 -08001941 return NULL;
1942}
1943
Robert Love34f42a02009-02-27 10:55:45 -08001944/**
1945 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001946 * @netdev: ptr to net_device
1947 *
1948 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07001949 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001950 */
Chris Leech090eb6c2009-08-25 14:00:28 -07001951static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001952{
Chris Leech014f5c32009-08-25 13:59:30 -07001953 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001954
Chris Leech014f5c32009-08-25 13:59:30 -07001955 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001956 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001957}
Robert Love85b4aa42008-12-09 15:10:24 -08001958
Robert Love34f42a02009-02-27 10:55:45 -08001959/**
1960 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001961 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001962 *
1963 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07001964 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001965 */
Chris Leech090eb6c2009-08-25 14:00:28 -07001966static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001967{
Chris Leech014f5c32009-08-25 13:59:30 -07001968 struct fcoe_interface *fcoe;
1969 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001970
Chris Leech014f5c32009-08-25 13:59:30 -07001971 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1972 if (!fcoe) {
1973 port = lport_priv(lport);
1974 fcoe = port->fcoe;
1975 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001976 }
1977 return 0;
1978}
Robert Love85b4aa42008-12-09 15:10:24 -08001979
Robert Love34f42a02009-02-27 10:55:45 -08001980/**
Robert Love34f42a02009-02-27 10:55:45 -08001981 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001982 *
Robert Love85b4aa42008-12-09 15:10:24 -08001983 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001984 */
Robert Love85b4aa42008-12-09 15:10:24 -08001985static int __init fcoe_init(void)
1986{
Robert Love38eccab2009-03-17 11:41:30 -07001987 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001988 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001989 struct fcoe_percpu_s *p;
1990
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001991 mutex_lock(&fcoe_config_mutex);
1992
Robert Love38eccab2009-03-17 11:41:30 -07001993 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001994 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001995 skb_queue_head_init(&p->fcoe_rx_list);
1996 }
1997
Robert Love8976f422009-03-17 11:41:46 -07001998 for_each_online_cpu(cpu)
1999 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002000
Robert Love8976f422009-03-17 11:41:46 -07002001 /* Initialize per CPU interrupt thread */
2002 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2003 if (rc)
2004 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002005
Robert Love8976f422009-03-17 11:41:46 -07002006 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002007 fcoe_dev_setup();
2008
Chris Leech5892c322009-08-25 13:59:14 -07002009 rc = fcoe_if_init();
2010 if (rc)
2011 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002012
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002013 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002014 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002015
2016out_free:
2017 for_each_online_cpu(cpu) {
2018 fcoe_percpu_thread_destroy(cpu);
2019 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002020 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002021 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002022}
2023module_init(fcoe_init);
2024
2025/**
Robert Love34f42a02009-02-27 10:55:45 -08002026 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002027 *
2028 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002029 */
Robert Love85b4aa42008-12-09 15:10:24 -08002030static void __exit fcoe_exit(void)
2031{
Robert Love5e5e92d2009-03-17 11:41:35 -07002032 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002033 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002034 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002035
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002036 mutex_lock(&fcoe_config_mutex);
2037
Robert Love85b4aa42008-12-09 15:10:24 -08002038 fcoe_dev_cleanup();
2039
Vasu Dev5919a592009-03-27 09:03:29 -07002040 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002041 rtnl_lock();
2042 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002043 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002044 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002045 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002046 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002047 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002048 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002049
Robert Love8976f422009-03-17 11:41:46 -07002050 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2051
Chris Leech014f5c32009-08-25 13:59:30 -07002052 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002053 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002054
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002055 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002056
2057 /* flush any asyncronous interface destroys,
2058 * this should happen after the netdev notifier is unregistered */
2059 flush_scheduled_work();
2060
2061 /* detach from scsi transport
2062 * must happen after all destroys are done, therefor after the flush */
2063 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002064}
2065module_exit(fcoe_exit);