blob: d1d6b3b8bf5bf928212eb52c28e14838340a1339 [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
Robert Love85b4aa42008-12-09 15:10:24 -080058/* fcoe host list */
59LIST_HEAD(fcoe_hostlist);
60DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070061DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080062
Chris Leechdd3fd722009-04-21 16:27:36 -070063/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070064static int fcoe_reset(struct Scsi_Host *shost);
65static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
66static int fcoe_rcv(struct sk_buff *, struct net_device *,
67 struct packet_type *, struct net_device *);
68static int fcoe_percpu_receive_thread(void *arg);
69static void fcoe_clean_pending_queue(struct fc_lport *lp);
70static void fcoe_percpu_clean(struct fc_lport *lp);
71static int fcoe_link_ok(struct fc_lport *lp);
72
73static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
74static int fcoe_hostlist_add(const struct fc_lport *);
75static int fcoe_hostlist_remove(const struct fc_lport *);
76
Vasu Dev4bb6b512009-05-06 10:52:34 -070077static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080078static int fcoe_device_notification(struct notifier_block *, ulong, void *);
79static void fcoe_dev_setup(void);
80static void fcoe_dev_cleanup(void);
81
82/* notification function from net device */
83static struct notifier_block fcoe_notifier = {
84 .notifier_call = fcoe_device_notification,
85};
86
Vasu Dev7f349142009-03-27 09:06:31 -070087static struct scsi_transport_template *scsi_transport_fcoe_sw;
88
89struct fc_function_template fcoe_transport_function = {
90 .show_host_node_name = 1,
91 .show_host_port_name = 1,
92 .show_host_supported_classes = 1,
93 .show_host_supported_fc4s = 1,
94 .show_host_active_fc4s = 1,
95 .show_host_maxframe_size = 1,
96
97 .show_host_port_id = 1,
98 .show_host_supported_speeds = 1,
99 .get_host_speed = fc_get_host_speed,
100 .show_host_speed = 1,
101 .show_host_port_type = 1,
102 .get_host_port_state = fc_get_host_port_state,
103 .show_host_port_state = 1,
104 .show_host_symbolic_name = 1,
105
106 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
107 .show_rport_maxframe_size = 1,
108 .show_rport_supported_classes = 1,
109
110 .show_host_fabric_name = 1,
111 .show_starget_node_name = 1,
112 .show_starget_port_name = 1,
113 .show_starget_port_id = 1,
114 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
115 .show_rport_dev_loss_tmo = 1,
116 .get_fc_host_stats = fc_get_host_stats,
117 .issue_fc_host_lip = fcoe_reset,
118
119 .terminate_rport_io = fc_rport_terminate_io,
120};
121
122static struct scsi_host_template fcoe_shost_template = {
123 .module = THIS_MODULE,
124 .name = "FCoE Driver",
125 .proc_name = FCOE_NAME,
126 .queuecommand = fc_queuecommand,
127 .eh_abort_handler = fc_eh_abort,
128 .eh_device_reset_handler = fc_eh_device_reset,
129 .eh_host_reset_handler = fc_eh_host_reset,
130 .slave_alloc = fc_slave_alloc,
131 .change_queue_depth = fc_change_queue_depth,
132 .change_queue_type = fc_change_queue_type,
133 .this_id = -1,
134 .cmd_per_lun = 32,
135 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
136 .use_clustering = ENABLE_CLUSTERING,
137 .sg_tablesize = SG_ALL,
138 .max_sectors = 0xffff,
139};
140
141/**
Chris Leech030f4e02009-08-25 14:00:02 -0700142 * fcoe_interface_create()
143 * @netdev: network interface
144 *
145 * Returns: pointer to a struct fcoe_interface or NULL on error
146 */
147static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
148{
149 struct fcoe_interface *fcoe;
150
151 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
152 if (!fcoe) {
153 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
154 return NULL;
155 }
156
157 kref_init(&fcoe->kref);
158 fcoe->netdev = netdev;
159
160 return fcoe;
161}
162
163/**
164 * fcoe_interface_release() - fcoe_port kref release function
165 * @kref: embedded reference count in an fcoe_interface struct
166 */
167static void fcoe_interface_release(struct kref *kref)
168{
169 struct fcoe_interface *fcoe;
170
171 fcoe = container_of(kref, struct fcoe_interface, kref);
172 kfree(fcoe);
173}
174
175/**
176 * fcoe_interface_get()
177 * @fcoe:
178 */
179static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
180{
181 kref_get(&fcoe->kref);
182}
183
184/**
185 * fcoe_interface_put()
186 * @fcoe:
187 */
188static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
189{
190 kref_put(&fcoe->kref, fcoe_interface_release);
191}
192
193/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000194 * fcoe_fip_recv - handle a received FIP frame.
195 * @skb: the receive skb
196 * @dev: associated &net_device
197 * @ptype: the &packet_type structure which was used to register this handler.
198 * @orig_dev: original receive &net_device, in case @dev is a bond.
199 *
200 * Returns: 0 for success
201 */
202static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
203 struct packet_type *ptype,
204 struct net_device *orig_dev)
205{
Chris Leech259ad852009-08-25 13:59:41 -0700206 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000207
Chris Leech259ad852009-08-25 13:59:41 -0700208 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700209 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000210 return 0;
211}
212
213/**
214 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
215 * @fip: FCoE controller.
216 * @skb: FIP Packet.
217 */
218static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
219{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700220 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000221 dev_queue_xmit(skb);
222}
223
224/**
225 * fcoe_update_src_mac() - Update Ethernet MAC filters.
226 * @fip: FCoE controller.
227 * @old: Unicast MAC address to delete if the MAC is non-zero.
228 * @new: Unicast MAC address to add.
229 *
230 * Remove any previously-set unicast MAC filter.
231 * Add secondary FCoE MAC address filter for our OUI.
232 */
233static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
234{
Chris Leech25024982009-08-25 13:59:35 -0700235 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000236
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700237 fcoe = fcoe_from_ctlr(fip);
Vasu Devab6b85c2009-05-17 12:33:08 +0000238 rtnl_lock();
239 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700240 dev_unicast_delete(fcoe->netdev, old);
241 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000242 rtnl_unlock();
243}
244
245/**
Vasu Dev7f349142009-03-27 09:06:31 -0700246 * fcoe_lport_config() - sets up the fc_lport
247 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700248 *
249 * Returns: 0 for success
250 */
251static int fcoe_lport_config(struct fc_lport *lp)
252{
253 lp->link_up = 0;
254 lp->qfull = 0;
255 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700256 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700257 lp->e_d_tov = 2 * 1000; /* FC-FS default */
258 lp->r_a_tov = 2 * 2 * 1000;
259 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
260 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
261
262 fc_lport_init_stats(lp);
263
264 /* lport fc_lport related configuration */
265 fc_lport_config(lp);
266
267 /* offload related configuration */
268 lp->crc_offload = 0;
269 lp->seq_offload = 0;
270 lp->lro_enabled = 0;
271 lp->lro_xid = 0;
272 lp->lso_max = 0;
273
274 return 0;
275}
276
277/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000278 * fcoe_netdev_cleanup() - clean up netdev configurations
Chris Leech014f5c32009-08-25 13:59:30 -0700279 * @port: ptr to the fcoe_port
Vasu Devab6b85c2009-05-17 12:33:08 +0000280 */
Chris Leech014f5c32009-08-25 13:59:30 -0700281void fcoe_netdev_cleanup(struct fcoe_port *port)
Vasu Devab6b85c2009-05-17 12:33:08 +0000282{
283 u8 flogi_maddr[ETH_ALEN];
Chris Leech25024982009-08-25 13:59:35 -0700284 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000285
286 /* Don't listen for Ethernet packets anymore */
Chris Leech259ad852009-08-25 13:59:41 -0700287 dev_remove_pack(&fcoe->fcoe_packet_type);
288 dev_remove_pack(&fcoe->fip_packet_type);
Vasu Devab6b85c2009-05-17 12:33:08 +0000289
290 /* Delete secondary MAC addresses */
291 rtnl_lock();
292 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Chris Leech25024982009-08-25 13:59:35 -0700293 dev_unicast_delete(fcoe->netdev, flogi_maddr);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700294 if (!is_zero_ether_addr(fcoe->ctlr.data_src_addr))
295 dev_unicast_delete(fcoe->netdev, fcoe->ctlr.data_src_addr);
296 if (fcoe->ctlr.spma)
297 dev_unicast_delete(fcoe->netdev, fcoe->ctlr.ctl_src_addr);
Chris Leech25024982009-08-25 13:59:35 -0700298 dev_mc_delete(fcoe->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Devab6b85c2009-05-17 12:33:08 +0000299 rtnl_unlock();
300}
301
302/**
Vasu Dev1047f222009-05-06 10:52:40 -0700303 * fcoe_queue_timer() - fcoe queue timer
304 * @lp: the fc_lport pointer
305 *
306 * Calls fcoe_check_wait_queue on timeout
307 *
308 */
309static void fcoe_queue_timer(ulong lp)
310{
311 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
312}
313
314/**
Vasu Dev7f349142009-03-27 09:06:31 -0700315 * fcoe_netdev_config() - Set up netdev for SW FCoE
316 * @lp : ptr to the fc_lport
317 * @netdev : ptr to the associated netdevice struct
318 *
319 * Must be called after fcoe_lport_config() as it will use lport mutex
320 *
321 * Returns : 0 for success
322 */
323static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
324{
325 u32 mfs;
326 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700327 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700328 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700329 u8 flogi_maddr[ETH_ALEN];
Vasu Dev184dd342009-05-17 12:33:28 +0000330 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700331
332 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700333 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700334 fcoe = port->fcoe;
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700335 fcoe->ctlr.lp = lp;
Chris Leech25024982009-08-25 13:59:35 -0700336 fcoe->netdev = netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700337
338 /* Do not support for bonding device */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700339 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
340 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
341 (netdev->priv_flags & IFF_MASTER_8023AD)) {
Vasu Dev7f349142009-03-27 09:06:31 -0700342 return -EOPNOTSUPP;
343 }
344
345 /*
346 * Determine max frame size based on underlying device and optional
347 * user-configured limit. If the MFS is too low, fcoe_link_ok()
348 * will return 0, so do this first.
349 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700350 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
351 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700352 if (fc_set_mfs(lp, mfs))
353 return -EINVAL;
354
Vasu Dev7f349142009-03-27 09:06:31 -0700355 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700356 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700357 lp->sg_supp = 1;
358
Vasu Dev7f349142009-03-27 09:06:31 -0700359 if (netdev->features & NETIF_F_FCOE_CRC) {
360 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700361 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700362 }
Vasu Dev7f349142009-03-27 09:06:31 -0700363 if (netdev->features & NETIF_F_FSO) {
364 lp->seq_offload = 1;
365 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700366 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
367 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700368 }
Vasu Dev7f349142009-03-27 09:06:31 -0700369 if (netdev->fcoe_ddp_xid) {
370 lp->lro_enabled = 1;
371 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700372 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
373 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700374 }
Chris Leech014f5c32009-08-25 13:59:30 -0700375 skb_queue_head_init(&port->fcoe_pending_queue);
376 port->fcoe_pending_queue_active = 0;
377 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700378
Vasu Dev184dd342009-05-17 12:33:28 +0000379 /* look for SAN MAC address, if multiple SAN MACs exist, only
380 * use the first one for SPMA */
381 rcu_read_lock();
382 for_each_dev_addr(netdev, ha) {
383 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700384 (is_valid_ether_addr(fcoe->ctlr.ctl_src_addr))) {
385 memcpy(fcoe->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
386 fcoe->ctlr.spma = 1;
Vasu Dev184dd342009-05-17 12:33:28 +0000387 break;
388 }
389 }
390 rcu_read_unlock();
391
Vasu Dev7f349142009-03-27 09:06:31 -0700392 /* setup Source Mac Address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700393 if (!fcoe->ctlr.spma)
394 memcpy(fcoe->ctlr.ctl_src_addr, netdev->dev_addr,
Chris Leech014f5c32009-08-25 13:59:30 -0700395 netdev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700396
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700397 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700398 fc_set_wwnn(lp, wwnn);
399 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700400 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700401 fc_set_wwpn(lp, wwpn);
402
403 /*
404 * Add FCoE MAC address as second unicast MAC address
405 * or enter promiscuous mode if not capable of listening
406 * for multiple unicast MACs.
407 */
408 rtnl_lock();
409 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700410 dev_unicast_add(netdev, flogi_maddr);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700411 if (fcoe->ctlr.spma)
412 dev_unicast_add(netdev, fcoe->ctlr.ctl_src_addr);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700413 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700414 rtnl_unlock();
415
416 /*
417 * setup the receive function from ethernet driver
418 * on the ethertype for the given device
419 */
Chris Leech259ad852009-08-25 13:59:41 -0700420 fcoe->fcoe_packet_type.func = fcoe_rcv;
421 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
422 fcoe->fcoe_packet_type.dev = netdev;
423 dev_add_pack(&fcoe->fcoe_packet_type);
Vasu Dev7f349142009-03-27 09:06:31 -0700424
Chris Leech259ad852009-08-25 13:59:41 -0700425 fcoe->fip_packet_type.func = fcoe_fip_recv;
426 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
427 fcoe->fip_packet_type.dev = netdev;
428 dev_add_pack(&fcoe->fip_packet_type);
Vasu Devab6b85c2009-05-17 12:33:08 +0000429
Vasu Dev7f349142009-03-27 09:06:31 -0700430 return 0;
431}
432
433/**
434 * fcoe_shost_config() - Sets up fc_lport->host
435 * @lp : ptr to the fc_lport
436 * @shost : ptr to the associated scsi host
437 * @dev : device associated to scsi host
438 *
439 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
440 *
441 * Returns : 0 for success
442 */
443static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
444 struct device *dev)
445{
446 int rc = 0;
447
448 /* lport scsi host config */
449 lp->host = shost;
450
451 lp->host->max_lun = FCOE_MAX_LUN;
452 lp->host->max_id = FCOE_MAX_FCP_TARGET;
453 lp->host->max_channel = 0;
454 lp->host->transportt = scsi_transport_fcoe_sw;
455
456 /* add the new host to the SCSI-ml */
457 rc = scsi_add_host(lp->host, dev);
458 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700459 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
460 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700461 return rc;
462 }
463 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
464 FCOE_NAME, FCOE_VERSION,
465 fcoe_netdev(lp)->name);
466
467 return 0;
468}
469
Vasu Devd7179682009-07-29 17:05:21 -0700470/*
471 * fcoe_oem_match() - match for read types IO
472 * @fp: the fc_frame for new IO.
473 *
474 * Returns : true for read types IO, otherwise returns false.
475 */
476bool fcoe_oem_match(struct fc_frame *fp)
477{
Yi Zou05cc7392009-08-25 13:59:03 -0700478 return fc_fcp_is_read(fr_fsp(fp)) &&
479 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700480}
481
Vasu Dev7f349142009-03-27 09:06:31 -0700482/**
483 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700484 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700485 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700486 * Called with write fcoe_hostlist_lock held.
487 *
Vasu Dev7f349142009-03-27 09:06:31 -0700488 * Returns : 0 on success
489 */
490static inline int fcoe_em_config(struct fc_lport *lp)
491{
Chris Leech014f5c32009-08-25 13:59:30 -0700492 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700493 struct fcoe_interface *fcoe = port->fcoe;
494 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700495 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700496 u16 min_xid = FCOE_MIN_XID;
497 u16 max_xid = FCOE_MAX_XID;
498
499 /*
500 * Check if need to allocate an em instance for
501 * offload exchange ids to be shared across all VN_PORTs/lport.
502 */
503 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
504 lp->lro_xid = 0;
505 goto skip_oem;
506 }
507
508 /*
509 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700510 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700511 */
Chris Leech25024982009-08-25 13:59:35 -0700512 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
513 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700514 else
Chris Leech25024982009-08-25 13:59:35 -0700515 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700516
Chris Leech25024982009-08-25 13:59:35 -0700517 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700518 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
519 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700520 else
Chris Leech25024982009-08-25 13:59:35 -0700521 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700522
523 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700524 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700525 break;
526 }
527 }
528
Chris Leech991cbb62009-08-25 13:59:51 -0700529 if (fcoe->oem) {
530 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700531 printk(KERN_ERR "fcoe_em_config: failed to add "
532 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700533 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700534 return -ENOMEM;
535 }
536 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700537 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700538 FCOE_MIN_XID, lp->lro_xid,
539 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700540 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700541 printk(KERN_ERR "fcoe_em_config: failed to allocate "
542 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700543 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700544 return -ENOMEM;
545 }
546 }
547
548 /*
549 * Exclude offload EM xid range from next EM xid range.
550 */
551 min_xid += lp->lro_xid + 1;
552
553skip_oem:
554 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
555 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700556 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700557 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700558 }
Vasu Dev7f349142009-03-27 09:06:31 -0700559
560 return 0;
561}
562
563/**
564 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700565 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700566 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700567static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700568{
Chris Leech014f5c32009-08-25 13:59:30 -0700569 struct fcoe_port *port = lport_priv(lport);
570 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700571 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700572
Robert Loved5488eb2009-06-10 15:30:59 -0700573 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700574
Vasu Dev7f349142009-03-27 09:06:31 -0700575 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700576 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700577
578 /* Remove the instance from fcoe's list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700579 fcoe_hostlist_remove(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700580
Vasu Devab6b85c2009-05-17 12:33:08 +0000581 /* clean up netdev configurations */
Chris Leech014f5c32009-08-25 13:59:30 -0700582 fcoe_netdev_cleanup(port);
Vasu Devab6b85c2009-05-17 12:33:08 +0000583
584 /* tear-down the FCoE controller */
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700585 fcoe_ctlr_destroy(&fcoe->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700586
Joe Eykholtf161fb72009-07-29 17:04:17 -0700587 /* Free queued packets for the per-CPU receive threads */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700588 fcoe_percpu_clean(lport);
Joe Eykholtf161fb72009-07-29 17:04:17 -0700589
Vasu Dev7f349142009-03-27 09:06:31 -0700590 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700591 fc_lport_destroy(lport);
592 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700593
594 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700595 fc_remove_host(lport->host);
596 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700597
598 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700599 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700600
Vasu Dev7f349142009-03-27 09:06:31 -0700601 /* Free existing skbs */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700602 fcoe_clean_pending_queue(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700603
Vasu Dev1047f222009-05-06 10:52:40 -0700604 /* Stop the timer */
Chris Leech014f5c32009-08-25 13:59:30 -0700605 del_timer_sync(&port->timer);
Vasu Dev1047f222009-05-06 10:52:40 -0700606
Vasu Dev7f349142009-03-27 09:06:31 -0700607 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700608 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700609
610 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700611 dev_put(netdev);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700612 scsi_host_put(lport->host);
Chris Leech030f4e02009-08-25 14:00:02 -0700613 fcoe_interface_put(fcoe);
Vasu Dev7f349142009-03-27 09:06:31 -0700614}
615
616/*
617 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
618 * @lp: the corresponding fc_lport
619 * @xid: the exchange id for this ddp transfer
620 * @sgl: the scatterlist describing this transfer
621 * @sgc: number of sg items
622 *
623 * Returns : 0 no ddp
624 */
625static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
626 struct scatterlist *sgl, unsigned int sgc)
627{
628 struct net_device *n = fcoe_netdev(lp);
629
630 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
631 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
632
633 return 0;
634}
635
636/*
637 * fcoe_ddp_done - calls LLD's ddp_done through net_device
638 * @lp: the corresponding fc_lport
639 * @xid: the exchange id for this ddp transfer
640 *
641 * Returns : the length of data that have been completed by ddp
642 */
643static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
644{
645 struct net_device *n = fcoe_netdev(lp);
646
647 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
648 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
649 return 0;
650}
651
652static struct libfc_function_template fcoe_libfc_fcn_templ = {
653 .frame_send = fcoe_xmit,
654 .ddp_setup = fcoe_ddp_setup,
655 .ddp_done = fcoe_ddp_done,
656};
657
658/**
Chris Leech030f4e02009-08-25 14:00:02 -0700659 * fcoe_if_create() - this function creates the fcoe port
660 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700661 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700662 *
663 * Creates fc_lport struct and scsi_host for lport, configures lport
664 * and starts fabric login.
665 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700666 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700667 */
Chris Leech030f4e02009-08-25 14:00:02 -0700668static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700669 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700670{
671 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700672 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700673 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700674 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700675 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700676
Robert Loved5488eb2009-06-10 15:30:59 -0700677 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700678
Chris Leech014f5c32009-08-25 13:59:30 -0700679 shost = libfc_host_alloc(&fcoe_shost_template,
680 sizeof(struct fcoe_port));
681 if (!shost) {
682 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
683 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700684 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700685 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700686 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700687 port = lport_priv(lport);
Chris Leech014f5c32009-08-25 13:59:30 -0700688 port->fcoe = fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700689
690 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700691 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700692 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700693 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
694 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700695 goto out_host_put;
696 }
697
Joe Eykholt97c83892009-03-17 11:42:40 -0700698 /*
699 * Initialize FIP.
700 */
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700701 fcoe_ctlr_init(&fcoe->ctlr);
702 fcoe->ctlr.send = fcoe_fip_send;
703 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Joe Eykholt97c83892009-03-17 11:42:40 -0700704
Vasu Devab6b85c2009-05-17 12:33:08 +0000705 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700706 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000707 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700708 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
709 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000710 goto out_netdev_cleanup;
711 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700712
Vasu Dev7f349142009-03-27 09:06:31 -0700713 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700714 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700715 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700716 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
717 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000718 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700719 }
720
Vasu Dev7f349142009-03-27 09:06:31 -0700721 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700722 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700723 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700724 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
725 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700726 goto out_lp_destroy;
727 }
728
Vasu Deve8af4d42009-07-29 17:05:15 -0700729 /*
730 * fcoe_em_alloc() and fcoe_hostlist_add() both
731 * need to be atomic under fcoe_hostlist_lock
732 * since fcoe_em_alloc() looks for an existing EM
733 * instance on host list updated by fcoe_hostlist_add().
734 */
735 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700736 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700737 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700738 if (rc) {
739 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
740 "interface\n");
741 goto out_lp_destroy;
742 }
743
Vasu Dev7f349142009-03-27 09:06:31 -0700744 /* add to lports list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700745 fcoe_hostlist_add(lport);
Vasu Deve8af4d42009-07-29 17:05:15 -0700746 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700747
Chris Leechaf7f85d2009-08-25 13:59:24 -0700748 lport->boot_time = jiffies;
Vasu Dev7f349142009-03-27 09:06:31 -0700749
Chris Leechaf7f85d2009-08-25 13:59:24 -0700750 fc_fabric_login(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700751
Chris Leechaf7f85d2009-08-25 13:59:24 -0700752 if (!fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700753 fcoe_ctlr_link_up(&fcoe->ctlr);
Joe Eykholt97c83892009-03-17 11:42:40 -0700754
Vasu Dev7f349142009-03-27 09:06:31 -0700755 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700756 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700757 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700758
759out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700760 fc_exch_mgr_free(lport);
Vasu Devab6b85c2009-05-17 12:33:08 +0000761out_netdev_cleanup:
Chris Leech014f5c32009-08-25 13:59:30 -0700762 fcoe_netdev_cleanup(port);
Vasu Dev7f349142009-03-27 09:06:31 -0700763out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700764 scsi_host_put(lport->host);
765out:
766 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700767}
768
769/**
770 * fcoe_if_init() - attach to scsi transport
771 *
772 * Returns : 0 on success
773 */
774static int __init fcoe_if_init(void)
775{
776 /* attach to scsi transport */
777 scsi_transport_fcoe_sw =
778 fc_attach_transport(&fcoe_transport_function);
779
780 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700781 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700782 return -ENODEV;
783 }
784
785 return 0;
786}
787
788/**
789 * fcoe_if_exit() - detach from scsi transport
790 *
791 * Returns : 0 on success
792 */
793int __exit fcoe_if_exit(void)
794{
795 fc_release_transport(scsi_transport_fcoe_sw);
796 return 0;
797}
798
Robert Love85b4aa42008-12-09 15:10:24 -0800799/**
Robert Love8976f422009-03-17 11:41:46 -0700800 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
801 * @cpu: cpu index for the online cpu
802 */
803static void fcoe_percpu_thread_create(unsigned int cpu)
804{
805 struct fcoe_percpu_s *p;
806 struct task_struct *thread;
807
808 p = &per_cpu(fcoe_percpu, cpu);
809
810 thread = kthread_create(fcoe_percpu_receive_thread,
811 (void *)p, "fcoethread/%d", cpu);
812
813 if (likely(!IS_ERR(p->thread))) {
814 kthread_bind(thread, cpu);
815 wake_up_process(thread);
816
817 spin_lock_bh(&p->fcoe_rx_list.lock);
818 p->thread = thread;
819 spin_unlock_bh(&p->fcoe_rx_list.lock);
820 }
821}
822
823/**
824 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
825 * @cpu: cpu index the rx thread is to be removed
826 *
827 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
828 * current CPU's Rx thread. If the thread being destroyed is bound to
829 * the CPU processing this context the skbs will be freed.
830 */
831static void fcoe_percpu_thread_destroy(unsigned int cpu)
832{
833 struct fcoe_percpu_s *p;
834 struct task_struct *thread;
835 struct page *crc_eof;
836 struct sk_buff *skb;
837#ifdef CONFIG_SMP
838 struct fcoe_percpu_s *p0;
839 unsigned targ_cpu = smp_processor_id();
840#endif /* CONFIG_SMP */
841
Robert Loved5488eb2009-06-10 15:30:59 -0700842 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700843
844 /* Prevent any new skbs from being queued for this CPU. */
845 p = &per_cpu(fcoe_percpu, cpu);
846 spin_lock_bh(&p->fcoe_rx_list.lock);
847 thread = p->thread;
848 p->thread = NULL;
849 crc_eof = p->crc_eof_page;
850 p->crc_eof_page = NULL;
851 p->crc_eof_offset = 0;
852 spin_unlock_bh(&p->fcoe_rx_list.lock);
853
854#ifdef CONFIG_SMP
855 /*
856 * Don't bother moving the skb's if this context is running
857 * on the same CPU that is having its thread destroyed. This
858 * can easily happen when the module is removed.
859 */
860 if (cpu != targ_cpu) {
861 p0 = &per_cpu(fcoe_percpu, targ_cpu);
862 spin_lock_bh(&p0->fcoe_rx_list.lock);
863 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700864 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
865 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700866
867 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
868 __skb_queue_tail(&p0->fcoe_rx_list, skb);
869 spin_unlock_bh(&p0->fcoe_rx_list.lock);
870 } else {
871 /*
872 * The targeted CPU is not initialized and cannot accept
873 * new skbs. Unlock the targeted CPU and drop the skbs
874 * on the CPU that is going offline.
875 */
876 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
877 kfree_skb(skb);
878 spin_unlock_bh(&p0->fcoe_rx_list.lock);
879 }
880 } else {
881 /*
882 * This scenario occurs when the module is being removed
883 * and all threads are being destroyed. skbs will continue
884 * to be shifted from the CPU thread that is being removed
885 * to the CPU thread associated with the CPU that is processing
886 * the module removal. Once there is only one CPU Rx thread it
887 * will reach this case and we will drop all skbs and later
888 * stop the thread.
889 */
890 spin_lock_bh(&p->fcoe_rx_list.lock);
891 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
892 kfree_skb(skb);
893 spin_unlock_bh(&p->fcoe_rx_list.lock);
894 }
895#else
896 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700897 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700898 * being removed. Free all skbs and stop the thread.
899 */
900 spin_lock_bh(&p->fcoe_rx_list.lock);
901 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
902 kfree_skb(skb);
903 spin_unlock_bh(&p->fcoe_rx_list.lock);
904#endif
905
906 if (thread)
907 kthread_stop(thread);
908
909 if (crc_eof)
910 put_page(crc_eof);
911}
912
913/**
914 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
915 * @nfb: callback data block
916 * @action: event triggering the callback
917 * @hcpu: index for the cpu of this event
918 *
919 * This creates or destroys per cpu data for fcoe
920 *
921 * Returns NOTIFY_OK always.
922 */
923static int fcoe_cpu_callback(struct notifier_block *nfb,
924 unsigned long action, void *hcpu)
925{
926 unsigned cpu = (unsigned long)hcpu;
927
928 switch (action) {
929 case CPU_ONLINE:
930 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700931 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700932 fcoe_percpu_thread_create(cpu);
933 break;
934 case CPU_DEAD:
935 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700936 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700937 fcoe_percpu_thread_destroy(cpu);
938 break;
939 default:
940 break;
941 }
942 return NOTIFY_OK;
943}
944
945static struct notifier_block fcoe_cpu_notifier = {
946 .notifier_call = fcoe_cpu_callback,
947};
948
949/**
Robert Love34f42a02009-02-27 10:55:45 -0800950 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800951 * @skb: the receive skb
952 * @dev: associated net device
953 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700954 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800955 *
956 * this function will receive the packet and build fc frame and pass it up
957 *
958 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800959 */
Robert Love85b4aa42008-12-09 15:10:24 -0800960int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
961 struct packet_type *ptype, struct net_device *olddev)
962{
963 struct fc_lport *lp;
964 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -0700965 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -0800966 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800967 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700968 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800969
Chris Leech259ad852009-08-25 13:59:41 -0700970 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700971 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800972 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700973 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800974 goto err2;
975 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700976 if (!lp->link_up)
977 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800978
Robert Loved5488eb2009-06-10 15:30:59 -0700979 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
980 "data:%p tail:%p end:%p sum:%d dev:%s",
981 skb->len, skb->data_len, skb->head, skb->data,
982 skb_tail_pointer(skb), skb_end_pointer(skb),
983 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800984
985 /* check for FCOE packet type */
986 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700987 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800988 goto err;
989 }
990
991 /*
992 * Check for minimum frame length, and make sure required FCoE
993 * and FC headers are pulled into the linear data area.
994 */
995 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
996 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
997 goto err;
998
999 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1000 fh = (struct fc_frame_header *) skb_transport_header(skb);
1001
Robert Love85b4aa42008-12-09 15:10:24 -08001002 fr = fcoe_dev_from_skb(skb);
1003 fr->fr_dev = lp;
1004 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001005
Robert Love85b4aa42008-12-09 15:10:24 -08001006 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001007 * In case the incoming frame's exchange is originated from
1008 * the initiator, then received frame's exchange id is ANDed
1009 * with fc_cpu_mask bits to get the same cpu on which exchange
1010 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001011 */
Vasu Devb2f00912009-08-25 13:58:53 -07001012 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1013 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1014 else
1015 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001016
Robert Love8976f422009-03-17 11:41:46 -07001017 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001018 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001019 if (unlikely(!fps->thread)) {
1020 /*
1021 * The targeted CPU is not ready, let's target
1022 * the first CPU now. For non-SMP systems this
1023 * will check the same CPU twice.
1024 */
Robert Loved5488eb2009-06-10 15:30:59 -07001025 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1026 "ready for incoming skb- using first online "
1027 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001028
1029 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1030 cpu = first_cpu(cpu_online_map);
1031 fps = &per_cpu(fcoe_percpu, cpu);
1032 spin_lock_bh(&fps->fcoe_rx_list.lock);
1033 if (!fps->thread) {
1034 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1035 goto err;
1036 }
1037 }
1038
1039 /*
1040 * We now have a valid CPU that we're targeting for
1041 * this skb. We also have this receive thread locked,
1042 * so we're free to queue skbs into it's queue.
1043 */
Robert Love85b4aa42008-12-09 15:10:24 -08001044 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1045 if (fps->fcoe_rx_list.qlen == 1)
1046 wake_up_process(fps->thread);
1047
1048 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1049
1050 return 0;
1051err:
Robert Love582b45b2009-03-31 15:51:50 -07001052 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001053
1054err2:
1055 kfree_skb(skb);
1056 return -1;
1057}
Robert Love85b4aa42008-12-09 15:10:24 -08001058
1059/**
Robert Love34f42a02009-02-27 10:55:45 -08001060 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001061 * @skb: the skb to be xmitted
1062 *
1063 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001064 */
Robert Love85b4aa42008-12-09 15:10:24 -08001065static inline int fcoe_start_io(struct sk_buff *skb)
1066{
1067 int rc;
1068
1069 skb_get(skb);
1070 rc = dev_queue_xmit(skb);
1071 if (rc != 0)
1072 return rc;
1073 kfree_skb(skb);
1074 return 0;
1075}
1076
1077/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001078 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001079 * @skb: the skb to be xmitted
1080 * @tlen: total len
1081 *
1082 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001083 */
Robert Love85b4aa42008-12-09 15:10:24 -08001084static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1085{
1086 struct fcoe_percpu_s *fps;
1087 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001088
Robert Love5e5e92d2009-03-17 11:41:35 -07001089 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001090 page = fps->crc_eof_page;
1091 if (!page) {
1092 page = alloc_page(GFP_ATOMIC);
1093 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001094 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001095 return -ENOMEM;
1096 }
1097 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001098 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001099 }
1100
1101 get_page(page);
1102 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1103 fps->crc_eof_offset, tlen);
1104 skb->len += tlen;
1105 skb->data_len += tlen;
1106 skb->truesize += tlen;
1107 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1108
1109 if (fps->crc_eof_offset >= PAGE_SIZE) {
1110 fps->crc_eof_page = NULL;
1111 fps->crc_eof_offset = 0;
1112 put_page(page);
1113 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001114 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001115 return 0;
1116}
1117
1118/**
Robert Love34f42a02009-02-27 10:55:45 -08001119 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001120 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001121 *
Chris Leech014f5c32009-08-25 13:59:30 -07001122 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001123 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001124 */
Robert Love85b4aa42008-12-09 15:10:24 -08001125u32 fcoe_fc_crc(struct fc_frame *fp)
1126{
1127 struct sk_buff *skb = fp_skb(fp);
1128 struct skb_frag_struct *frag;
1129 unsigned char *data;
1130 unsigned long off, len, clen;
1131 u32 crc;
1132 unsigned i;
1133
1134 crc = crc32(~0, skb->data, skb_headlen(skb));
1135
1136 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1137 frag = &skb_shinfo(skb)->frags[i];
1138 off = frag->page_offset;
1139 len = frag->size;
1140 while (len > 0) {
1141 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1142 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1143 KM_SKB_DATA_SOFTIRQ);
1144 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1145 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1146 off += clen;
1147 len -= clen;
1148 }
1149 }
1150 return crc;
1151}
Robert Love85b4aa42008-12-09 15:10:24 -08001152
1153/**
Robert Love34f42a02009-02-27 10:55:45 -08001154 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001155 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001156 * @fp: the fc_frame to be transmitted
1157 *
1158 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001159 */
Robert Love85b4aa42008-12-09 15:10:24 -08001160int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1161{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001162 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001163 u32 crc;
1164 struct ethhdr *eh;
1165 struct fcoe_crc_eof *cp;
1166 struct sk_buff *skb;
1167 struct fcoe_dev_stats *stats;
1168 struct fc_frame_header *fh;
1169 unsigned int hlen; /* header length implies the version */
1170 unsigned int tlen; /* trailer length */
1171 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001172 struct fcoe_port *port = lport_priv(lp);
1173 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001174 u8 sof, eof;
1175 struct fcoe_hdr *hp;
1176
1177 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1178
Robert Love85b4aa42008-12-09 15:10:24 -08001179 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001180 skb = fp_skb(fp);
1181 wlen = skb->len / FCOE_WORD_TO_BYTE;
1182
1183 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001184 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001185 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001186 }
1187
Joe Eykholt97c83892009-03-17 11:42:40 -07001188 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001189 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001190 return 0;
1191
Robert Love85b4aa42008-12-09 15:10:24 -08001192 sof = fr_sof(fp);
1193 eof = fr_eof(fp);
1194
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001195 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001196 hlen = sizeof(struct fcoe_hdr);
1197 tlen = sizeof(struct fcoe_crc_eof);
1198 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1199
1200 /* crc offload */
1201 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001202 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001203 skb->csum_start = skb_headroom(skb);
1204 skb->csum_offset = skb->len;
1205 crc = 0;
1206 } else {
1207 skb->ip_summed = CHECKSUM_NONE;
1208 crc = fcoe_fc_crc(fp);
1209 }
1210
Chris Leech014f5c32009-08-25 13:59:30 -07001211 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001212 if (skb_is_nonlinear(skb)) {
1213 skb_frag_t *frag;
1214 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001215 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001216 return -ENOMEM;
1217 }
1218 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1219 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1220 + frag->page_offset;
1221 } else {
1222 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1223 }
1224
1225 memset(cp, 0, sizeof(*cp));
1226 cp->fcoe_eof = eof;
1227 cp->fcoe_crc32 = cpu_to_le32(~crc);
1228
1229 if (skb_is_nonlinear(skb)) {
1230 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1231 cp = NULL;
1232 }
1233
Chris Leech014f5c32009-08-25 13:59:30 -07001234 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001235 skb_push(skb, elen + hlen);
1236 skb_reset_mac_header(skb);
1237 skb_reset_network_header(skb);
1238 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001239 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001240 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001241
1242 /* fill up mac and fcoe headers */
1243 eh = eth_hdr(skb);
1244 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001245 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001246 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1247 else
1248 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001249 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001250
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001251 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1252 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001253 else
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001254 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001255
1256 hp = (struct fcoe_hdr *)(eh + 1);
1257 memset(hp, 0, sizeof(*hp));
1258 if (FC_FCOE_VER)
1259 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1260 hp->fcoe_sof = sof;
1261
Yi Zou39ca9a02009-02-27 14:07:15 -08001262 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1263 if (lp->seq_offload && fr_max_payload(fp)) {
1264 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1265 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1266 } else {
1267 skb_shinfo(skb)->gso_type = 0;
1268 skb_shinfo(skb)->gso_size = 0;
1269 }
Robert Love85b4aa42008-12-09 15:10:24 -08001270 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001271 stats = fc_lport_get_stats(lp);
1272 stats->TxFrames++;
1273 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001274
1275 /* send down to lld */
1276 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001277 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001278 fcoe_check_wait_queue(lp, skb);
1279 else if (fcoe_start_io(skb))
1280 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001281
1282 return 0;
1283}
Robert Love85b4aa42008-12-09 15:10:24 -08001284
Robert Love34f42a02009-02-27 10:55:45 -08001285/**
1286 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001287 * @arg: ptr to the fcoe per cpu struct
1288 *
1289 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001290 */
1291int fcoe_percpu_receive_thread(void *arg)
1292{
1293 struct fcoe_percpu_s *p = arg;
1294 u32 fr_len;
1295 struct fc_lport *lp;
1296 struct fcoe_rcv_info *fr;
1297 struct fcoe_dev_stats *stats;
1298 struct fc_frame_header *fh;
1299 struct sk_buff *skb;
1300 struct fcoe_crc_eof crc_eof;
1301 struct fc_frame *fp;
1302 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001303 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001304 struct fcoe_hdr *hp;
1305
Robert Love4469c192009-02-27 10:56:38 -08001306 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001307
1308 while (!kthread_should_stop()) {
1309
1310 spin_lock_bh(&p->fcoe_rx_list.lock);
1311 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1312 set_current_state(TASK_INTERRUPTIBLE);
1313 spin_unlock_bh(&p->fcoe_rx_list.lock);
1314 schedule();
1315 set_current_state(TASK_RUNNING);
1316 if (kthread_should_stop())
1317 return 0;
1318 spin_lock_bh(&p->fcoe_rx_list.lock);
1319 }
1320 spin_unlock_bh(&p->fcoe_rx_list.lock);
1321 fr = fcoe_dev_from_skb(skb);
1322 lp = fr->fr_dev;
1323 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001324 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001325 kfree_skb(skb);
1326 continue;
1327 }
1328
Robert Loved5488eb2009-06-10 15:30:59 -07001329 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1330 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1331 skb->len, skb->data_len,
1332 skb->head, skb->data, skb_tail_pointer(skb),
1333 skb_end_pointer(skb), skb->csum,
1334 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001335
1336 /*
1337 * Save source MAC address before discarding header.
1338 */
Chris Leech014f5c32009-08-25 13:59:30 -07001339 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001340 if (skb_is_nonlinear(skb))
1341 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001342 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001343
1344 /*
1345 * Frame length checks and setting up the header pointers
1346 * was done in fcoe_rcv already.
1347 */
1348 hp = (struct fcoe_hdr *) skb_network_header(skb);
1349 fh = (struct fc_frame_header *) skb_transport_header(skb);
1350
Robert Love582b45b2009-03-31 15:51:50 -07001351 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001352 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001353 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001354 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001355 "mismatch: The frame has "
1356 "version %x, but the "
1357 "initiator supports version "
1358 "%x\n", FC_FCOE_DECAPS_VER(hp),
1359 FC_FCOE_VER);
1360 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001361 kfree_skb(skb);
1362 continue;
1363 }
1364
1365 skb_pull(skb, sizeof(struct fcoe_hdr));
1366 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1367
Robert Love582b45b2009-03-31 15:51:50 -07001368 stats->RxFrames++;
1369 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001370
1371 fp = (struct fc_frame *)skb;
1372 fc_frame_init(fp);
1373 fr_dev(fp) = lp;
1374 fr_sof(fp) = hp->fcoe_sof;
1375
1376 /* Copy out the CRC and EOF trailer for access */
1377 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1378 kfree_skb(skb);
1379 continue;
1380 }
1381 fr_eof(fp) = crc_eof.fcoe_eof;
1382 fr_crc(fp) = crc_eof.fcoe_crc32;
1383 if (pskb_trim(skb, fr_len)) {
1384 kfree_skb(skb);
1385 continue;
1386 }
1387
1388 /*
1389 * We only check CRC if no offload is available and if it is
1390 * it's solicited data, in which case, the FCP layer would
1391 * check it during the copy.
1392 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001393 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001394 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1395 else
1396 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1397
1398 fh = fc_frame_header_get(fp);
1399 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1400 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001401 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001402 continue;
1403 }
1404 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1405 if (le32_to_cpu(fr_crc(fp)) !=
1406 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001407 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001408 printk(KERN_WARNING "fcoe: dropping "
1409 "frame with CRC error\n");
1410 stats->InvalidCRCCount++;
1411 stats->ErrorFrames++;
1412 fc_frame_free(fp);
1413 continue;
1414 }
1415 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1416 }
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001417 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1418 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001419 fc_frame_free(fp);
1420 continue;
1421 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001422 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001423 }
1424 return 0;
1425}
1426
1427/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001428 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1429 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001430 *
1431 * This empties the wait_queue, dequeue the head of the wait_queue queue
1432 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001433 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001434 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001435 *
1436 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001437 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001438 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001439 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001440static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001441{
Chris Leech014f5c32009-08-25 13:59:30 -07001442 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001443 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001444
Chris Leech014f5c32009-08-25 13:59:30 -07001445 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001446
1447 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001448 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001449
Chris Leech014f5c32009-08-25 13:59:30 -07001450 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001451 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001452 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001453
Chris Leech014f5c32009-08-25 13:59:30 -07001454 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001455 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001456 port->fcoe_pending_queue.qlen++;
1457 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001458
Chris Leech014f5c32009-08-25 13:59:30 -07001459 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001460 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001461 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001462
1463 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001464 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001465 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001466 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001467 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001468 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001469 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001470 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001471 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001472
Chris Leech014f5c32009-08-25 13:59:30 -07001473 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001474 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001475 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1476 mod_timer(&port->timer, jiffies + 2);
1477 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001478out:
Chris Leech014f5c32009-08-25 13:59:30 -07001479 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001480 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001481 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001482 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001483}
1484
1485/**
Robert Love34f42a02009-02-27 10:55:45 -08001486 * fcoe_dev_setup() - setup link change notification interface
1487 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001488static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001489{
Robert Love85b4aa42008-12-09 15:10:24 -08001490 register_netdevice_notifier(&fcoe_notifier);
1491}
1492
1493/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001494 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001495 */
Robert Love85b4aa42008-12-09 15:10:24 -08001496static void fcoe_dev_cleanup(void)
1497{
1498 unregister_netdevice_notifier(&fcoe_notifier);
1499}
1500
1501/**
Robert Love34f42a02009-02-27 10:55:45 -08001502 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001503 * @notifier: context of the notification
1504 * @event: type of event
1505 * @ptr: fixed array for output parsed ifname
1506 *
1507 * This function is called by the ethernet driver in case of link change event
1508 *
1509 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001510 */
Robert Love85b4aa42008-12-09 15:10:24 -08001511static int fcoe_device_notification(struct notifier_block *notifier,
1512 ulong event, void *ptr)
1513{
1514 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001515 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001516 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001517 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001518 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001519 u32 mfs;
1520 int rc = NOTIFY_OK;
1521
1522 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001523 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001524 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001525 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001526 break;
1527 }
1528 }
1529 read_unlock(&fcoe_hostlist_lock);
1530 if (lp == NULL) {
1531 rc = NOTIFY_DONE;
1532 goto out;
1533 }
1534
Robert Love85b4aa42008-12-09 15:10:24 -08001535 switch (event) {
1536 case NETDEV_DOWN:
1537 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001538 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001539 break;
1540 case NETDEV_UP:
1541 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001542 break;
1543 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001544 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1545 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001546 if (mfs >= FC_MIN_MAX_FRAME)
1547 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001548 break;
1549 case NETDEV_REGISTER:
1550 break;
1551 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001552 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001553 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001554 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001555 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001556 fcoe_ctlr_link_up(&fcoe->ctlr);
1557 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001558 stats = fc_lport_get_stats(lp);
1559 stats->LinkFailureCount++;
1560 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001561 }
1562out:
1563 return rc;
1564}
1565
1566/**
Robert Love34f42a02009-02-27 10:55:45 -08001567 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001568 * @buffer: incoming buffer to be copied
1569 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001570 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001571 */
Robert Love85b4aa42008-12-09 15:10:24 -08001572static struct net_device *fcoe_if_to_netdev(const char *buffer)
1573{
1574 char *cp;
1575 char ifname[IFNAMSIZ + 2];
1576
1577 if (buffer) {
1578 strlcpy(ifname, buffer, IFNAMSIZ);
1579 cp = ifname + strlen(ifname);
1580 while (--cp >= ifname && *cp == '\n')
1581 *cp = '\0';
1582 return dev_get_by_name(&init_net, ifname);
1583 }
1584 return NULL;
1585}
1586
1587/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001588 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001589 * @netdev: the target netdev
1590 *
1591 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001592 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001593static struct module *
1594fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001595{
1596 struct device *dev;
1597
1598 if (!netdev)
1599 return NULL;
1600
1601 dev = netdev->dev.parent;
1602 if (!dev)
1603 return NULL;
1604
1605 if (!dev->driver)
1606 return NULL;
1607
1608 return dev->driver->owner;
1609}
1610
1611/**
Robert Love34f42a02009-02-27 10:55:45 -08001612 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001613 * @netdev: the target netdev
1614 *
Robert Love34f42a02009-02-27 10:55:45 -08001615 * Holds the Ethernet driver module by try_module_get() for
1616 * the corresponding netdev.
1617 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001618 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001619 */
Robert Love85b4aa42008-12-09 15:10:24 -08001620static int fcoe_ethdrv_get(const struct net_device *netdev)
1621{
1622 struct module *owner;
1623
1624 owner = fcoe_netdev_to_module_owner(netdev);
1625 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001626 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1627 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001628 return try_module_get(owner);
1629 }
1630 return -ENODEV;
1631}
1632
1633/**
Robert Love34f42a02009-02-27 10:55:45 -08001634 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001635 * @netdev: the target netdev
1636 *
Robert Love34f42a02009-02-27 10:55:45 -08001637 * Releases the Ethernet driver module by module_put for
1638 * the corresponding netdev.
1639 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001640 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001641 */
Robert Love85b4aa42008-12-09 15:10:24 -08001642static int fcoe_ethdrv_put(const struct net_device *netdev)
1643{
1644 struct module *owner;
1645
1646 owner = fcoe_netdev_to_module_owner(netdev);
1647 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001648 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1649 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001650 module_put(owner);
1651 return 0;
1652 }
1653 return -ENODEV;
1654}
1655
1656/**
Robert Love34f42a02009-02-27 10:55:45 -08001657 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001658 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001659 * @kp: associated kernel param
1660 *
1661 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001662 */
Robert Love85b4aa42008-12-09 15:10:24 -08001663static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1664{
Robert Love85b4aa42008-12-09 15:10:24 -08001665 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -07001666 struct fcoe_interface *fcoe;
1667 struct fcoe_port *port;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001668 struct fc_lport *lport;
1669 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001670
1671 netdev = fcoe_if_to_netdev(buffer);
1672 if (!netdev) {
1673 rc = -ENODEV;
1674 goto out_nodev;
1675 }
1676 /* look for existing lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001677 lport = fcoe_hostlist_lookup(netdev);
1678 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001679 rc = -ENODEV;
1680 goto out_putdev;
1681 }
Chris Leech030f4e02009-08-25 14:00:02 -07001682 port = lport_priv(lport);
1683 fcoe = port->fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001684 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001685 fcoe_ethdrv_put(netdev);
1686 rc = 0;
1687out_putdev:
1688 dev_put(netdev);
1689out_nodev:
1690 return rc;
1691}
1692
1693/**
Robert Love34f42a02009-02-27 10:55:45 -08001694 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001695 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001696 * @kp: associated kernel param
1697 *
1698 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001699 */
Robert Love85b4aa42008-12-09 15:10:24 -08001700static int fcoe_create(const char *buffer, struct kernel_param *kp)
1701{
1702 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001703 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001704 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001705 struct net_device *netdev;
1706
1707 netdev = fcoe_if_to_netdev(buffer);
1708 if (!netdev) {
1709 rc = -ENODEV;
1710 goto out_nodev;
1711 }
1712 /* look for existing lport */
1713 if (fcoe_hostlist_lookup(netdev)) {
1714 rc = -EEXIST;
1715 goto out_putdev;
1716 }
1717 fcoe_ethdrv_get(netdev);
1718
Chris Leech030f4e02009-08-25 14:00:02 -07001719 fcoe = fcoe_interface_create(netdev);
1720 if (!fcoe) {
1721 rc = -ENOMEM;
1722 goto out_putdev;
1723 }
1724
1725 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001726 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001727 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001728 netdev->name);
1729 fcoe_ethdrv_put(netdev);
1730 rc = -EIO;
Chris Leech030f4e02009-08-25 14:00:02 -07001731 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001732 }
Chris Leech030f4e02009-08-25 14:00:02 -07001733
1734 dev_put(netdev);
1735 return 0;
1736
1737out_free:
1738 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001739out_putdev:
1740 dev_put(netdev);
1741out_nodev:
1742 return rc;
1743}
1744
1745module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1746__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001747MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001748module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1749__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001750MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001751
Robert Love34f42a02009-02-27 10:55:45 -08001752/**
1753 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001754 * @lp: ptr to the fc_lport
1755 *
1756 * Any permanently-disqualifying conditions have been previously checked.
1757 * This also updates the speed setting, which may change with link for 100/1000.
1758 *
1759 * This function should probably be checking for PAUSE support at some point
1760 * in the future. Currently Per-priority-pause is not determinable using
1761 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1762 *
1763 * Returns: 0 if link is OK for use by FCoE.
1764 *
1765 */
1766int fcoe_link_ok(struct fc_lport *lp)
1767{
Chris Leech014f5c32009-08-25 13:59:30 -07001768 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001769 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001770 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001771
Yi Zou2f718d62009-07-29 17:04:01 -07001772 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1773 (!dev_ethtool_get_settings(dev, &ecmd))) {
1774 lp->link_supported_speeds &=
1775 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1776 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1777 SUPPORTED_1000baseT_Full))
1778 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1779 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1780 lp->link_supported_speeds |=
1781 FC_PORTSPEED_10GBIT;
1782 if (ecmd.speed == SPEED_1000)
1783 lp->link_speed = FC_PORTSPEED_1GBIT;
1784 if (ecmd.speed == SPEED_10000)
1785 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001786
Yi Zou2f718d62009-07-29 17:04:01 -07001787 return 0;
1788 }
1789 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001790}
Robert Love85b4aa42008-12-09 15:10:24 -08001791
Robert Love34f42a02009-02-27 10:55:45 -08001792/**
1793 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001794 * @lp: the fc_lport
1795 */
1796void fcoe_percpu_clean(struct fc_lport *lp)
1797{
Robert Love85b4aa42008-12-09 15:10:24 -08001798 struct fcoe_percpu_s *pp;
1799 struct fcoe_rcv_info *fr;
1800 struct sk_buff_head *list;
1801 struct sk_buff *skb, *next;
1802 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001803 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001804
Robert Love5e5e92d2009-03-17 11:41:35 -07001805 for_each_possible_cpu(cpu) {
1806 pp = &per_cpu(fcoe_percpu, cpu);
1807 spin_lock_bh(&pp->fcoe_rx_list.lock);
1808 list = &pp->fcoe_rx_list;
1809 head = list->next;
1810 for (skb = head; skb != (struct sk_buff *)list;
1811 skb = next) {
1812 next = skb->next;
1813 fr = fcoe_dev_from_skb(skb);
1814 if (fr->fr_dev == lp) {
1815 __skb_unlink(skb, list);
1816 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001817 }
Robert Love85b4aa42008-12-09 15:10:24 -08001818 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001819 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001820 }
1821}
Robert Love85b4aa42008-12-09 15:10:24 -08001822
1823/**
Robert Love34f42a02009-02-27 10:55:45 -08001824 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001825 * @lp: the corresponding fc_lport
1826 *
1827 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001828 */
Robert Love85b4aa42008-12-09 15:10:24 -08001829void fcoe_clean_pending_queue(struct fc_lport *lp)
1830{
Chris Leech014f5c32009-08-25 13:59:30 -07001831 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001832 struct sk_buff *skb;
1833
Chris Leech014f5c32009-08-25 13:59:30 -07001834 spin_lock_bh(&port->fcoe_pending_queue.lock);
1835 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1836 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001837 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001838 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001839 }
Chris Leech014f5c32009-08-25 13:59:30 -07001840 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001841}
Robert Love85b4aa42008-12-09 15:10:24 -08001842
1843/**
Robert Love34f42a02009-02-27 10:55:45 -08001844 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001845 * @shost: shost the reset is from
1846 *
1847 * Returns: always 0
1848 */
1849int fcoe_reset(struct Scsi_Host *shost)
1850{
1851 struct fc_lport *lport = shost_priv(shost);
1852 fc_lport_reset(lport);
1853 return 0;
1854}
Robert Love85b4aa42008-12-09 15:10:24 -08001855
Robert Love34f42a02009-02-27 10:55:45 -08001856/**
Chris Leech014f5c32009-08-25 13:59:30 -07001857 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001858 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001859 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001860 * Called with fcoe_hostlist_lock held.
1861 *
Chris Leech014f5c32009-08-25 13:59:30 -07001862 * Returns: NULL or the located fcoe_port
Robert Love85b4aa42008-12-09 15:10:24 -08001863 */
Chris Leech014f5c32009-08-25 13:59:30 -07001864static struct fcoe_interface *
1865fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001866{
Chris Leech014f5c32009-08-25 13:59:30 -07001867 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001868
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 == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001871 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001872 }
Robert Love85b4aa42008-12-09 15:10:24 -08001873 return NULL;
1874}
1875
Robert Love34f42a02009-02-27 10:55:45 -08001876/**
1877 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001878 * @netdev: ptr to net_device
1879 *
1880 * Returns: 0 for success
1881 */
1882struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1883{
Chris Leech014f5c32009-08-25 13:59:30 -07001884 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001885
Vasu Deve8af4d42009-07-29 17:05:15 -07001886 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001887 fcoe = fcoe_hostlist_lookup_port(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001888 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001889
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001890 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001891}
Robert Love85b4aa42008-12-09 15:10:24 -08001892
Robert Love34f42a02009-02-27 10:55:45 -08001893/**
1894 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001895 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001896 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001897 * Called with write fcoe_hostlist_lock held.
1898 *
Robert Love85b4aa42008-12-09 15:10:24 -08001899 * Returns: 0 for success
1900 */
Chris Leech014f5c32009-08-25 13:59:30 -07001901int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001902{
Chris Leech014f5c32009-08-25 13:59:30 -07001903 struct fcoe_interface *fcoe;
1904 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001905
Chris Leech014f5c32009-08-25 13:59:30 -07001906 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1907 if (!fcoe) {
1908 port = lport_priv(lport);
1909 fcoe = port->fcoe;
1910 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001911 }
1912 return 0;
1913}
Robert Love85b4aa42008-12-09 15:10:24 -08001914
Robert Love34f42a02009-02-27 10:55:45 -08001915/**
1916 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001917 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001918 *
1919 * Returns: 0 for success
1920 */
Chris Leech014f5c32009-08-25 13:59:30 -07001921int fcoe_hostlist_remove(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001922{
Chris Leech014f5c32009-08-25 13:59:30 -07001923 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001924
Vasu Deve8af4d42009-07-29 17:05:15 -07001925 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001926 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1927 BUG_ON(!fcoe);
1928 list_del(&fcoe->list);
Robert Love85b4aa42008-12-09 15:10:24 -08001929 write_unlock_bh(&fcoe_hostlist_lock);
1930
1931 return 0;
1932}
Robert Love85b4aa42008-12-09 15:10:24 -08001933
1934/**
Robert Love34f42a02009-02-27 10:55:45 -08001935 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001936 *
Robert Love85b4aa42008-12-09 15:10:24 -08001937 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001938 */
Robert Love85b4aa42008-12-09 15:10:24 -08001939static int __init fcoe_init(void)
1940{
Robert Love38eccab2009-03-17 11:41:30 -07001941 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001942 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001943 struct fcoe_percpu_s *p;
1944
Robert Love38eccab2009-03-17 11:41:30 -07001945 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001946 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001947 skb_queue_head_init(&p->fcoe_rx_list);
1948 }
1949
Robert Love8976f422009-03-17 11:41:46 -07001950 for_each_online_cpu(cpu)
1951 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001952
Robert Love8976f422009-03-17 11:41:46 -07001953 /* Initialize per CPU interrupt thread */
1954 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1955 if (rc)
1956 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001957
Robert Love8976f422009-03-17 11:41:46 -07001958 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001959 fcoe_dev_setup();
1960
Chris Leech5892c322009-08-25 13:59:14 -07001961 rc = fcoe_if_init();
1962 if (rc)
1963 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001964
1965 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001966
1967out_free:
1968 for_each_online_cpu(cpu) {
1969 fcoe_percpu_thread_destroy(cpu);
1970 }
1971
1972 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001973}
1974module_init(fcoe_init);
1975
1976/**
Robert Love34f42a02009-02-27 10:55:45 -08001977 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001978 *
1979 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001980 */
Robert Love85b4aa42008-12-09 15:10:24 -08001981static void __exit fcoe_exit(void)
1982{
Robert Love5e5e92d2009-03-17 11:41:35 -07001983 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07001984 struct fcoe_interface *fcoe, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001985
Robert Love85b4aa42008-12-09 15:10:24 -08001986 fcoe_dev_cleanup();
1987
Vasu Dev5919a592009-03-27 09:03:29 -07001988 /* releases the associated fcoe hosts */
Chris Leech014f5c32009-08-25 13:59:30 -07001989 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001990 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001991
Robert Love8976f422009-03-17 11:41:46 -07001992 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1993
Chris Leech014f5c32009-08-25 13:59:30 -07001994 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07001995 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001996
Vasu Dev7f349142009-03-27 09:06:31 -07001997 /* detach from scsi transport */
1998 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001999}
2000module_exit(fcoe_exit);