blob: c9a0346e493a9ee798e4902ff28e634ccd99bd13 [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
Robert Love85b4aa42008-12-09 15:10:24 -080060/* fcoe host list */
61LIST_HEAD(fcoe_hostlist);
62DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070063DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080064
Chris Leechdd3fd722009-04-21 16:27:36 -070065/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070066static int fcoe_reset(struct Scsi_Host *shost);
67static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
68static int fcoe_rcv(struct sk_buff *, struct net_device *,
69 struct packet_type *, struct net_device *);
70static int fcoe_percpu_receive_thread(void *arg);
71static void fcoe_clean_pending_queue(struct fc_lport *lp);
72static void fcoe_percpu_clean(struct fc_lport *lp);
73static int fcoe_link_ok(struct fc_lport *lp);
74
75static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
76static int fcoe_hostlist_add(const struct fc_lport *);
77static int fcoe_hostlist_remove(const struct fc_lport *);
78
Vasu Dev4bb6b512009-05-06 10:52:34 -070079static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080080static int fcoe_device_notification(struct notifier_block *, ulong, void *);
81static void fcoe_dev_setup(void);
82static void fcoe_dev_cleanup(void);
83
84/* notification function from net device */
85static struct notifier_block fcoe_notifier = {
86 .notifier_call = fcoe_device_notification,
87};
88
Vasu Dev7f349142009-03-27 09:06:31 -070089static struct scsi_transport_template *scsi_transport_fcoe_sw;
90
91struct fc_function_template fcoe_transport_function = {
92 .show_host_node_name = 1,
93 .show_host_port_name = 1,
94 .show_host_supported_classes = 1,
95 .show_host_supported_fc4s = 1,
96 .show_host_active_fc4s = 1,
97 .show_host_maxframe_size = 1,
98
99 .show_host_port_id = 1,
100 .show_host_supported_speeds = 1,
101 .get_host_speed = fc_get_host_speed,
102 .show_host_speed = 1,
103 .show_host_port_type = 1,
104 .get_host_port_state = fc_get_host_port_state,
105 .show_host_port_state = 1,
106 .show_host_symbolic_name = 1,
107
108 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
109 .show_rport_maxframe_size = 1,
110 .show_rport_supported_classes = 1,
111
112 .show_host_fabric_name = 1,
113 .show_starget_node_name = 1,
114 .show_starget_port_name = 1,
115 .show_starget_port_id = 1,
116 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
117 .show_rport_dev_loss_tmo = 1,
118 .get_fc_host_stats = fc_get_host_stats,
119 .issue_fc_host_lip = fcoe_reset,
120
121 .terminate_rport_io = fc_rport_terminate_io,
122};
123
124static struct scsi_host_template fcoe_shost_template = {
125 .module = THIS_MODULE,
126 .name = "FCoE Driver",
127 .proc_name = FCOE_NAME,
128 .queuecommand = fc_queuecommand,
129 .eh_abort_handler = fc_eh_abort,
130 .eh_device_reset_handler = fc_eh_device_reset,
131 .eh_host_reset_handler = fc_eh_host_reset,
132 .slave_alloc = fc_slave_alloc,
133 .change_queue_depth = fc_change_queue_depth,
134 .change_queue_type = fc_change_queue_type,
135 .this_id = -1,
136 .cmd_per_lun = 32,
137 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
138 .use_clustering = ENABLE_CLUSTERING,
139 .sg_tablesize = SG_ALL,
140 .max_sectors = 0xffff,
141};
142
Chris Leech54b649f2009-08-25 14:00:07 -0700143static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
144 struct packet_type *ptype,
145 struct net_device *orig_dev);
146/**
147 * fcoe_interface_setup()
148 * @fcoe: new fcoe_interface
149 * @netdev : ptr to the associated netdevice struct
150 *
151 * Returns : 0 for success
152 */
153static int fcoe_interface_setup(struct fcoe_interface *fcoe,
154 struct net_device *netdev)
155{
156 struct fcoe_ctlr *fip = &fcoe->ctlr;
157 struct netdev_hw_addr *ha;
158 u8 flogi_maddr[ETH_ALEN];
159
160 fcoe->netdev = netdev;
161
162 /* Do not support for bonding device */
163 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
164 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
165 (netdev->priv_flags & IFF_MASTER_8023AD)) {
166 return -EOPNOTSUPP;
167 }
168
169 /* look for SAN MAC address, if multiple SAN MACs exist, only
170 * use the first one for SPMA */
171 rcu_read_lock();
172 for_each_dev_addr(netdev, ha) {
173 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
174 (is_valid_ether_addr(fip->ctl_src_addr))) {
175 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
176 fip->spma = 1;
177 break;
178 }
179 }
180 rcu_read_unlock();
181
182 /* setup Source Mac Address */
183 if (!fip->spma)
184 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
185
186 /*
187 * Add FCoE MAC address as second unicast MAC address
188 * or enter promiscuous mode if not capable of listening
189 * for multiple unicast MACs.
190 */
191 rtnl_lock();
192 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
193 dev_unicast_add(netdev, flogi_maddr);
194 if (fip->spma)
195 dev_unicast_add(netdev, fip->ctl_src_addr);
196 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
197 rtnl_unlock();
198
199 /*
200 * setup the receive function from ethernet driver
201 * on the ethertype for the given device
202 */
203 fcoe->fcoe_packet_type.func = fcoe_rcv;
204 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
205 fcoe->fcoe_packet_type.dev = netdev;
206 dev_add_pack(&fcoe->fcoe_packet_type);
207
208 fcoe->fip_packet_type.func = fcoe_fip_recv;
209 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
210 fcoe->fip_packet_type.dev = netdev;
211 dev_add_pack(&fcoe->fip_packet_type);
212
213 return 0;
214}
215
216static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
217static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
218
Vasu Dev7f349142009-03-27 09:06:31 -0700219/**
Chris Leech030f4e02009-08-25 14:00:02 -0700220 * fcoe_interface_create()
221 * @netdev: network interface
222 *
223 * Returns: pointer to a struct fcoe_interface or NULL on error
224 */
225static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
226{
227 struct fcoe_interface *fcoe;
228
229 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
230 if (!fcoe) {
231 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
232 return NULL;
233 }
234
235 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700236
237 /*
238 * Initialize FIP.
239 */
240 fcoe_ctlr_init(&fcoe->ctlr);
241 fcoe->ctlr.send = fcoe_fip_send;
242 fcoe->ctlr.update_mac = fcoe_update_src_mac;
243
244 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700245
246 return fcoe;
247}
248
249/**
Chris Leech54b649f2009-08-25 14:00:07 -0700250 * fcoe_interface_cleanup() - clean up netdev configurations
251 * @fcoe:
252 */
253void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
254{
255 struct net_device *netdev = fcoe->netdev;
256 struct fcoe_ctlr *fip = &fcoe->ctlr;
257 u8 flogi_maddr[ETH_ALEN];
258
259 /*
260 * Don't listen for Ethernet packets anymore.
261 * synchronize_net() ensures that the packet handlers are not running
262 * on another CPU. dev_remove_pack() would do that, this calls the
263 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
264 */
265 __dev_remove_pack(&fcoe->fcoe_packet_type);
266 __dev_remove_pack(&fcoe->fip_packet_type);
267 synchronize_net();
268
269 /* tear-down the FCoE controller */
270 fcoe_ctlr_destroy(&fcoe->ctlr);
271
272 /* Delete secondary MAC addresses */
273 rtnl_lock();
274 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
275 dev_unicast_delete(netdev, flogi_maddr);
276 if (!is_zero_ether_addr(fip->data_src_addr))
277 dev_unicast_delete(netdev, fip->data_src_addr);
278 if (fip->spma)
279 dev_unicast_delete(netdev, fip->ctl_src_addr);
280 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
281 rtnl_unlock();
282}
283
284/**
Chris Leech030f4e02009-08-25 14:00:02 -0700285 * fcoe_interface_release() - fcoe_port kref release function
286 * @kref: embedded reference count in an fcoe_interface struct
287 */
288static void fcoe_interface_release(struct kref *kref)
289{
290 struct fcoe_interface *fcoe;
291
292 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700293 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -0700294 kfree(fcoe);
295}
296
297/**
298 * fcoe_interface_get()
299 * @fcoe:
300 */
301static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
302{
303 kref_get(&fcoe->kref);
304}
305
306/**
307 * fcoe_interface_put()
308 * @fcoe:
309 */
310static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
311{
312 kref_put(&fcoe->kref, fcoe_interface_release);
313}
314
315/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000316 * fcoe_fip_recv - handle a received FIP frame.
317 * @skb: the receive skb
318 * @dev: associated &net_device
319 * @ptype: the &packet_type structure which was used to register this handler.
320 * @orig_dev: original receive &net_device, in case @dev is a bond.
321 *
322 * Returns: 0 for success
323 */
324static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
325 struct packet_type *ptype,
326 struct net_device *orig_dev)
327{
Chris Leech259ad852009-08-25 13:59:41 -0700328 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000329
Chris Leech259ad852009-08-25 13:59:41 -0700330 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700331 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000332 return 0;
333}
334
335/**
336 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
337 * @fip: FCoE controller.
338 * @skb: FIP Packet.
339 */
340static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
341{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700342 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000343 dev_queue_xmit(skb);
344}
345
346/**
347 * fcoe_update_src_mac() - Update Ethernet MAC filters.
348 * @fip: FCoE controller.
349 * @old: Unicast MAC address to delete if the MAC is non-zero.
350 * @new: Unicast MAC address to add.
351 *
352 * Remove any previously-set unicast MAC filter.
353 * Add secondary FCoE MAC address filter for our OUI.
354 */
355static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
356{
Chris Leech25024982009-08-25 13:59:35 -0700357 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000358
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700359 fcoe = fcoe_from_ctlr(fip);
Vasu Devab6b85c2009-05-17 12:33:08 +0000360 rtnl_lock();
361 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700362 dev_unicast_delete(fcoe->netdev, old);
363 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000364 rtnl_unlock();
365}
366
367/**
Vasu Dev7f349142009-03-27 09:06:31 -0700368 * fcoe_lport_config() - sets up the fc_lport
369 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700370 *
371 * Returns: 0 for success
372 */
373static int fcoe_lport_config(struct fc_lport *lp)
374{
375 lp->link_up = 0;
376 lp->qfull = 0;
377 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700378 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700379 lp->e_d_tov = 2 * 1000; /* FC-FS default */
380 lp->r_a_tov = 2 * 2 * 1000;
381 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
382 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
383
384 fc_lport_init_stats(lp);
385
386 /* lport fc_lport related configuration */
387 fc_lport_config(lp);
388
389 /* offload related configuration */
390 lp->crc_offload = 0;
391 lp->seq_offload = 0;
392 lp->lro_enabled = 0;
393 lp->lro_xid = 0;
394 lp->lso_max = 0;
395
396 return 0;
397}
398
399/**
Vasu Dev1047f222009-05-06 10:52:40 -0700400 * fcoe_queue_timer() - fcoe queue timer
401 * @lp: the fc_lport pointer
402 *
403 * Calls fcoe_check_wait_queue on timeout
404 *
405 */
406static void fcoe_queue_timer(ulong lp)
407{
408 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
409}
410
411/**
Vasu Dev7f349142009-03-27 09:06:31 -0700412 * fcoe_netdev_config() - Set up netdev for SW FCoE
413 * @lp : ptr to the fc_lport
414 * @netdev : ptr to the associated netdevice struct
415 *
416 * Must be called after fcoe_lport_config() as it will use lport mutex
417 *
418 * Returns : 0 for success
419 */
420static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
421{
422 u32 mfs;
423 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700424 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700425 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700426
427 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700428 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700429 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700430
431 /*
432 * Determine max frame size based on underlying device and optional
433 * user-configured limit. If the MFS is too low, fcoe_link_ok()
434 * will return 0, so do this first.
435 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700436 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
437 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700438 if (fc_set_mfs(lp, mfs))
439 return -EINVAL;
440
Vasu Dev7f349142009-03-27 09:06:31 -0700441 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700442 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700443 lp->sg_supp = 1;
444
Vasu Dev7f349142009-03-27 09:06:31 -0700445 if (netdev->features & NETIF_F_FCOE_CRC) {
446 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700447 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700448 }
Vasu Dev7f349142009-03-27 09:06:31 -0700449 if (netdev->features & NETIF_F_FSO) {
450 lp->seq_offload = 1;
451 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700452 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
453 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700454 }
Vasu Dev7f349142009-03-27 09:06:31 -0700455 if (netdev->fcoe_ddp_xid) {
456 lp->lro_enabled = 1;
457 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700458 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
459 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700460 }
Chris Leech014f5c32009-08-25 13:59:30 -0700461 skb_queue_head_init(&port->fcoe_pending_queue);
462 port->fcoe_pending_queue_active = 0;
463 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700464
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700465 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700466 fc_set_wwnn(lp, wwnn);
467 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700468 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700469 fc_set_wwpn(lp, wwpn);
470
Vasu Dev7f349142009-03-27 09:06:31 -0700471 return 0;
472}
473
474/**
475 * fcoe_shost_config() - Sets up fc_lport->host
476 * @lp : ptr to the fc_lport
477 * @shost : ptr to the associated scsi host
478 * @dev : device associated to scsi host
479 *
480 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
481 *
482 * Returns : 0 for success
483 */
484static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
485 struct device *dev)
486{
487 int rc = 0;
488
489 /* lport scsi host config */
490 lp->host = shost;
491
492 lp->host->max_lun = FCOE_MAX_LUN;
493 lp->host->max_id = FCOE_MAX_FCP_TARGET;
494 lp->host->max_channel = 0;
495 lp->host->transportt = scsi_transport_fcoe_sw;
496
497 /* add the new host to the SCSI-ml */
498 rc = scsi_add_host(lp->host, dev);
499 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700500 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
501 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700502 return rc;
503 }
504 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
505 FCOE_NAME, FCOE_VERSION,
506 fcoe_netdev(lp)->name);
507
508 return 0;
509}
510
Vasu Devd7179682009-07-29 17:05:21 -0700511/*
512 * fcoe_oem_match() - match for read types IO
513 * @fp: the fc_frame for new IO.
514 *
515 * Returns : true for read types IO, otherwise returns false.
516 */
517bool fcoe_oem_match(struct fc_frame *fp)
518{
Yi Zou05cc7392009-08-25 13:59:03 -0700519 return fc_fcp_is_read(fr_fsp(fp)) &&
520 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700521}
522
Vasu Dev7f349142009-03-27 09:06:31 -0700523/**
524 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700525 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700526 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700527 * Called with write fcoe_hostlist_lock held.
528 *
Vasu Dev7f349142009-03-27 09:06:31 -0700529 * Returns : 0 on success
530 */
531static inline int fcoe_em_config(struct fc_lport *lp)
532{
Chris Leech014f5c32009-08-25 13:59:30 -0700533 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700534 struct fcoe_interface *fcoe = port->fcoe;
535 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700536 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700537 u16 min_xid = FCOE_MIN_XID;
538 u16 max_xid = FCOE_MAX_XID;
539
540 /*
541 * Check if need to allocate an em instance for
542 * offload exchange ids to be shared across all VN_PORTs/lport.
543 */
544 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
545 lp->lro_xid = 0;
546 goto skip_oem;
547 }
548
549 /*
550 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700551 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700552 */
Chris Leech25024982009-08-25 13:59:35 -0700553 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
554 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700555 else
Chris Leech25024982009-08-25 13:59:35 -0700556 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700557
Chris Leech25024982009-08-25 13:59:35 -0700558 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700559 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
560 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700561 else
Chris Leech25024982009-08-25 13:59:35 -0700562 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700563
564 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700565 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700566 break;
567 }
568 }
569
Chris Leech991cbb62009-08-25 13:59:51 -0700570 if (fcoe->oem) {
571 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700572 printk(KERN_ERR "fcoe_em_config: failed to add "
573 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700574 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700575 return -ENOMEM;
576 }
577 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700578 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700579 FCOE_MIN_XID, lp->lro_xid,
580 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700581 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700582 printk(KERN_ERR "fcoe_em_config: failed to allocate "
583 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700584 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700585 return -ENOMEM;
586 }
587 }
588
589 /*
590 * Exclude offload EM xid range from next EM xid range.
591 */
592 min_xid += lp->lro_xid + 1;
593
594skip_oem:
595 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
596 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700597 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700598 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700599 }
Vasu Dev7f349142009-03-27 09:06:31 -0700600
601 return 0;
602}
603
604/**
605 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700606 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700607 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700608static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700609{
Chris Leech014f5c32009-08-25 13:59:30 -0700610 struct fcoe_port *port = lport_priv(lport);
611 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700612 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700613
Robert Loved5488eb2009-06-10 15:30:59 -0700614 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700615
Vasu Dev7f349142009-03-27 09:06:31 -0700616 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700617 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700618
Vasu Dev7f349142009-03-27 09:06:31 -0700619 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700620 fc_lport_destroy(lport);
621 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700622
Chris Leech54b649f2009-08-25 14:00:07 -0700623 /* Stop the transmit retry timer */
624 del_timer_sync(&port->timer);
625
626 /* Free existing transmit skbs */
627 fcoe_clean_pending_queue(lport);
628
629 /* receives may not be stopped until after this */
630 fcoe_interface_put(fcoe);
631
632 /* Free queued packets for the per-CPU receive threads */
633 fcoe_percpu_clean(lport);
634
Vasu Dev7f349142009-03-27 09:06:31 -0700635 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700636 fc_remove_host(lport->host);
637 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700638
639 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700640 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700641
Vasu Dev7f349142009-03-27 09:06:31 -0700642 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700643 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700644
645 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700646 dev_put(netdev);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700647 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700648}
649
650/*
651 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
652 * @lp: the corresponding fc_lport
653 * @xid: the exchange id for this ddp transfer
654 * @sgl: the scatterlist describing this transfer
655 * @sgc: number of sg items
656 *
657 * Returns : 0 no ddp
658 */
659static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
660 struct scatterlist *sgl, unsigned int sgc)
661{
662 struct net_device *n = fcoe_netdev(lp);
663
664 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
665 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
666
667 return 0;
668}
669
670/*
671 * fcoe_ddp_done - calls LLD's ddp_done through net_device
672 * @lp: the corresponding fc_lport
673 * @xid: the exchange id for this ddp transfer
674 *
675 * Returns : the length of data that have been completed by ddp
676 */
677static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
678{
679 struct net_device *n = fcoe_netdev(lp);
680
681 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
682 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
683 return 0;
684}
685
686static struct libfc_function_template fcoe_libfc_fcn_templ = {
687 .frame_send = fcoe_xmit,
688 .ddp_setup = fcoe_ddp_setup,
689 .ddp_done = fcoe_ddp_done,
690};
691
692/**
Chris Leech030f4e02009-08-25 14:00:02 -0700693 * fcoe_if_create() - this function creates the fcoe port
694 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700695 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700696 *
Chris Leech54b649f2009-08-25 14:00:07 -0700697 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700698 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700699 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700700 */
Chris Leech030f4e02009-08-25 14:00:02 -0700701static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700702 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700703{
704 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700705 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700706 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700707 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700708 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700709
Robert Loved5488eb2009-06-10 15:30:59 -0700710 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700711
Chris Leech014f5c32009-08-25 13:59:30 -0700712 shost = libfc_host_alloc(&fcoe_shost_template,
713 sizeof(struct fcoe_port));
714 if (!shost) {
715 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
716 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700717 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700718 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700719 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700720 port = lport_priv(lport);
Chris Leech014f5c32009-08-25 13:59:30 -0700721 port->fcoe = fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700722
723 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700724 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700725 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700726 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
727 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700728 goto out_host_put;
729 }
730
Vasu Devab6b85c2009-05-17 12:33:08 +0000731 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700732 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000733 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700734 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
735 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700736 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000737 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700738
Vasu Dev7f349142009-03-27 09:06:31 -0700739 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700740 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700741 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700742 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
743 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700744 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700745 }
746
Vasu Dev7f349142009-03-27 09:06:31 -0700747 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700748 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700749 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700750 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
751 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700752 goto out_lp_destroy;
753 }
754
Vasu Deve8af4d42009-07-29 17:05:15 -0700755 /*
756 * fcoe_em_alloc() and fcoe_hostlist_add() both
Chris Leechc863df32009-08-25 14:00:18 -0700757 * need to be atomic with respect to other changes to the hostlist
Vasu Deve8af4d42009-07-29 17:05:15 -0700758 * since fcoe_em_alloc() looks for an existing EM
759 * instance on host list updated by fcoe_hostlist_add().
Chris Leechc863df32009-08-25 14:00:18 -0700760 *
761 * This is currently handled through the fcoe_config_mutex begin held.
Vasu Deve8af4d42009-07-29 17:05:15 -0700762 */
Chris Leechc863df32009-08-25 14:00:18 -0700763
Vasu Dev96316092009-07-29 17:05:00 -0700764 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700765 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700766 if (rc) {
767 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
768 "interface\n");
769 goto out_lp_destroy;
770 }
771
Vasu Dev7f349142009-03-27 09:06:31 -0700772 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700773 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700774 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700775
776out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700777 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700778out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700779 scsi_host_put(lport->host);
780out:
781 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700782}
783
784/**
785 * fcoe_if_init() - attach to scsi transport
786 *
787 * Returns : 0 on success
788 */
789static int __init fcoe_if_init(void)
790{
791 /* attach to scsi transport */
792 scsi_transport_fcoe_sw =
793 fc_attach_transport(&fcoe_transport_function);
794
795 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700796 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700797 return -ENODEV;
798 }
799
800 return 0;
801}
802
803/**
804 * fcoe_if_exit() - detach from scsi transport
805 *
806 * Returns : 0 on success
807 */
808int __exit fcoe_if_exit(void)
809{
810 fc_release_transport(scsi_transport_fcoe_sw);
Chris Leechdfc1d0f2009-08-25 14:00:13 -0700811 scsi_transport_fcoe_sw = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700812 return 0;
813}
814
Robert Love85b4aa42008-12-09 15:10:24 -0800815/**
Robert Love8976f422009-03-17 11:41:46 -0700816 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
817 * @cpu: cpu index for the online cpu
818 */
819static void fcoe_percpu_thread_create(unsigned int cpu)
820{
821 struct fcoe_percpu_s *p;
822 struct task_struct *thread;
823
824 p = &per_cpu(fcoe_percpu, cpu);
825
826 thread = kthread_create(fcoe_percpu_receive_thread,
827 (void *)p, "fcoethread/%d", cpu);
828
829 if (likely(!IS_ERR(p->thread))) {
830 kthread_bind(thread, cpu);
831 wake_up_process(thread);
832
833 spin_lock_bh(&p->fcoe_rx_list.lock);
834 p->thread = thread;
835 spin_unlock_bh(&p->fcoe_rx_list.lock);
836 }
837}
838
839/**
840 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
841 * @cpu: cpu index the rx thread is to be removed
842 *
843 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
844 * current CPU's Rx thread. If the thread being destroyed is bound to
845 * the CPU processing this context the skbs will be freed.
846 */
847static void fcoe_percpu_thread_destroy(unsigned int cpu)
848{
849 struct fcoe_percpu_s *p;
850 struct task_struct *thread;
851 struct page *crc_eof;
852 struct sk_buff *skb;
853#ifdef CONFIG_SMP
854 struct fcoe_percpu_s *p0;
855 unsigned targ_cpu = smp_processor_id();
856#endif /* CONFIG_SMP */
857
Robert Loved5488eb2009-06-10 15:30:59 -0700858 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700859
860 /* Prevent any new skbs from being queued for this CPU. */
861 p = &per_cpu(fcoe_percpu, cpu);
862 spin_lock_bh(&p->fcoe_rx_list.lock);
863 thread = p->thread;
864 p->thread = NULL;
865 crc_eof = p->crc_eof_page;
866 p->crc_eof_page = NULL;
867 p->crc_eof_offset = 0;
868 spin_unlock_bh(&p->fcoe_rx_list.lock);
869
870#ifdef CONFIG_SMP
871 /*
872 * Don't bother moving the skb's if this context is running
873 * on the same CPU that is having its thread destroyed. This
874 * can easily happen when the module is removed.
875 */
876 if (cpu != targ_cpu) {
877 p0 = &per_cpu(fcoe_percpu, targ_cpu);
878 spin_lock_bh(&p0->fcoe_rx_list.lock);
879 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700880 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
881 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700882
883 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
884 __skb_queue_tail(&p0->fcoe_rx_list, skb);
885 spin_unlock_bh(&p0->fcoe_rx_list.lock);
886 } else {
887 /*
888 * The targeted CPU is not initialized and cannot accept
889 * new skbs. Unlock the targeted CPU and drop the skbs
890 * on the CPU that is going offline.
891 */
892 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
893 kfree_skb(skb);
894 spin_unlock_bh(&p0->fcoe_rx_list.lock);
895 }
896 } else {
897 /*
898 * This scenario occurs when the module is being removed
899 * and all threads are being destroyed. skbs will continue
900 * to be shifted from the CPU thread that is being removed
901 * to the CPU thread associated with the CPU that is processing
902 * the module removal. Once there is only one CPU Rx thread it
903 * will reach this case and we will drop all skbs and later
904 * stop the thread.
905 */
906 spin_lock_bh(&p->fcoe_rx_list.lock);
907 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
908 kfree_skb(skb);
909 spin_unlock_bh(&p->fcoe_rx_list.lock);
910 }
911#else
912 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700913 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700914 * being removed. Free all skbs and stop the thread.
915 */
916 spin_lock_bh(&p->fcoe_rx_list.lock);
917 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
918 kfree_skb(skb);
919 spin_unlock_bh(&p->fcoe_rx_list.lock);
920#endif
921
922 if (thread)
923 kthread_stop(thread);
924
925 if (crc_eof)
926 put_page(crc_eof);
927}
928
929/**
930 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
931 * @nfb: callback data block
932 * @action: event triggering the callback
933 * @hcpu: index for the cpu of this event
934 *
935 * This creates or destroys per cpu data for fcoe
936 *
937 * Returns NOTIFY_OK always.
938 */
939static int fcoe_cpu_callback(struct notifier_block *nfb,
940 unsigned long action, void *hcpu)
941{
942 unsigned cpu = (unsigned long)hcpu;
943
944 switch (action) {
945 case CPU_ONLINE:
946 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700947 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700948 fcoe_percpu_thread_create(cpu);
949 break;
950 case CPU_DEAD:
951 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700952 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700953 fcoe_percpu_thread_destroy(cpu);
954 break;
955 default:
956 break;
957 }
958 return NOTIFY_OK;
959}
960
961static struct notifier_block fcoe_cpu_notifier = {
962 .notifier_call = fcoe_cpu_callback,
963};
964
965/**
Robert Love34f42a02009-02-27 10:55:45 -0800966 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800967 * @skb: the receive skb
968 * @dev: associated net device
969 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700970 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800971 *
972 * this function will receive the packet and build fc frame and pass it up
973 *
974 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800975 */
Robert Love85b4aa42008-12-09 15:10:24 -0800976int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
977 struct packet_type *ptype, struct net_device *olddev)
978{
979 struct fc_lport *lp;
980 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -0700981 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -0800982 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800983 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700984 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800985
Chris Leech259ad852009-08-25 13:59:41 -0700986 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700987 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800988 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700989 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800990 goto err2;
991 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700992 if (!lp->link_up)
993 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800994
Robert Loved5488eb2009-06-10 15:30:59 -0700995 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
996 "data:%p tail:%p end:%p sum:%d dev:%s",
997 skb->len, skb->data_len, skb->head, skb->data,
998 skb_tail_pointer(skb), skb_end_pointer(skb),
999 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001000
1001 /* check for FCOE packet type */
1002 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001003 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001004 goto err;
1005 }
1006
1007 /*
1008 * Check for minimum frame length, and make sure required FCoE
1009 * and FC headers are pulled into the linear data area.
1010 */
1011 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1012 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1013 goto err;
1014
1015 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1016 fh = (struct fc_frame_header *) skb_transport_header(skb);
1017
Robert Love85b4aa42008-12-09 15:10:24 -08001018 fr = fcoe_dev_from_skb(skb);
1019 fr->fr_dev = lp;
1020 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001021
Robert Love85b4aa42008-12-09 15:10:24 -08001022 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001023 * In case the incoming frame's exchange is originated from
1024 * the initiator, then received frame's exchange id is ANDed
1025 * with fc_cpu_mask bits to get the same cpu on which exchange
1026 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001027 */
Vasu Devb2f00912009-08-25 13:58:53 -07001028 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1029 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1030 else
1031 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001032
Robert Love8976f422009-03-17 11:41:46 -07001033 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001034 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001035 if (unlikely(!fps->thread)) {
1036 /*
1037 * The targeted CPU is not ready, let's target
1038 * the first CPU now. For non-SMP systems this
1039 * will check the same CPU twice.
1040 */
Robert Loved5488eb2009-06-10 15:30:59 -07001041 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1042 "ready for incoming skb- using first online "
1043 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001044
1045 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1046 cpu = first_cpu(cpu_online_map);
1047 fps = &per_cpu(fcoe_percpu, cpu);
1048 spin_lock_bh(&fps->fcoe_rx_list.lock);
1049 if (!fps->thread) {
1050 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1051 goto err;
1052 }
1053 }
1054
1055 /*
1056 * We now have a valid CPU that we're targeting for
1057 * this skb. We also have this receive thread locked,
1058 * so we're free to queue skbs into it's queue.
1059 */
Robert Love85b4aa42008-12-09 15:10:24 -08001060 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1061 if (fps->fcoe_rx_list.qlen == 1)
1062 wake_up_process(fps->thread);
1063
1064 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1065
1066 return 0;
1067err:
Robert Love582b45b2009-03-31 15:51:50 -07001068 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001069
1070err2:
1071 kfree_skb(skb);
1072 return -1;
1073}
Robert Love85b4aa42008-12-09 15:10:24 -08001074
1075/**
Robert Love34f42a02009-02-27 10:55:45 -08001076 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001077 * @skb: the skb to be xmitted
1078 *
1079 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001080 */
Robert Love85b4aa42008-12-09 15:10:24 -08001081static inline int fcoe_start_io(struct sk_buff *skb)
1082{
1083 int rc;
1084
1085 skb_get(skb);
1086 rc = dev_queue_xmit(skb);
1087 if (rc != 0)
1088 return rc;
1089 kfree_skb(skb);
1090 return 0;
1091}
1092
1093/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001094 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001095 * @skb: the skb to be xmitted
1096 * @tlen: total len
1097 *
1098 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001099 */
Robert Love85b4aa42008-12-09 15:10:24 -08001100static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1101{
1102 struct fcoe_percpu_s *fps;
1103 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001104
Robert Love5e5e92d2009-03-17 11:41:35 -07001105 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001106 page = fps->crc_eof_page;
1107 if (!page) {
1108 page = alloc_page(GFP_ATOMIC);
1109 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001110 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001111 return -ENOMEM;
1112 }
1113 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001114 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001115 }
1116
1117 get_page(page);
1118 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1119 fps->crc_eof_offset, tlen);
1120 skb->len += tlen;
1121 skb->data_len += tlen;
1122 skb->truesize += tlen;
1123 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1124
1125 if (fps->crc_eof_offset >= PAGE_SIZE) {
1126 fps->crc_eof_page = NULL;
1127 fps->crc_eof_offset = 0;
1128 put_page(page);
1129 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001130 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001131 return 0;
1132}
1133
1134/**
Robert Love34f42a02009-02-27 10:55:45 -08001135 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001136 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001137 *
Chris Leech014f5c32009-08-25 13:59:30 -07001138 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001139 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001140 */
Robert Love85b4aa42008-12-09 15:10:24 -08001141u32 fcoe_fc_crc(struct fc_frame *fp)
1142{
1143 struct sk_buff *skb = fp_skb(fp);
1144 struct skb_frag_struct *frag;
1145 unsigned char *data;
1146 unsigned long off, len, clen;
1147 u32 crc;
1148 unsigned i;
1149
1150 crc = crc32(~0, skb->data, skb_headlen(skb));
1151
1152 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1153 frag = &skb_shinfo(skb)->frags[i];
1154 off = frag->page_offset;
1155 len = frag->size;
1156 while (len > 0) {
1157 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1158 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1159 KM_SKB_DATA_SOFTIRQ);
1160 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1161 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1162 off += clen;
1163 len -= clen;
1164 }
1165 }
1166 return crc;
1167}
Robert Love85b4aa42008-12-09 15:10:24 -08001168
1169/**
Robert Love34f42a02009-02-27 10:55:45 -08001170 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001171 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001172 * @fp: the fc_frame to be transmitted
1173 *
1174 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001175 */
Robert Love85b4aa42008-12-09 15:10:24 -08001176int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1177{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001178 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001179 u32 crc;
1180 struct ethhdr *eh;
1181 struct fcoe_crc_eof *cp;
1182 struct sk_buff *skb;
1183 struct fcoe_dev_stats *stats;
1184 struct fc_frame_header *fh;
1185 unsigned int hlen; /* header length implies the version */
1186 unsigned int tlen; /* trailer length */
1187 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001188 struct fcoe_port *port = lport_priv(lp);
1189 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001190 u8 sof, eof;
1191 struct fcoe_hdr *hp;
1192
1193 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1194
Robert Love85b4aa42008-12-09 15:10:24 -08001195 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001196 skb = fp_skb(fp);
1197 wlen = skb->len / FCOE_WORD_TO_BYTE;
1198
1199 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001200 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001201 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001202 }
1203
Joe Eykholt97c83892009-03-17 11:42:40 -07001204 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001205 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001206 return 0;
1207
Robert Love85b4aa42008-12-09 15:10:24 -08001208 sof = fr_sof(fp);
1209 eof = fr_eof(fp);
1210
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001211 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001212 hlen = sizeof(struct fcoe_hdr);
1213 tlen = sizeof(struct fcoe_crc_eof);
1214 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1215
1216 /* crc offload */
1217 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001218 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001219 skb->csum_start = skb_headroom(skb);
1220 skb->csum_offset = skb->len;
1221 crc = 0;
1222 } else {
1223 skb->ip_summed = CHECKSUM_NONE;
1224 crc = fcoe_fc_crc(fp);
1225 }
1226
Chris Leech014f5c32009-08-25 13:59:30 -07001227 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001228 if (skb_is_nonlinear(skb)) {
1229 skb_frag_t *frag;
1230 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001231 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001232 return -ENOMEM;
1233 }
1234 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1235 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1236 + frag->page_offset;
1237 } else {
1238 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1239 }
1240
1241 memset(cp, 0, sizeof(*cp));
1242 cp->fcoe_eof = eof;
1243 cp->fcoe_crc32 = cpu_to_le32(~crc);
1244
1245 if (skb_is_nonlinear(skb)) {
1246 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1247 cp = NULL;
1248 }
1249
Chris Leech014f5c32009-08-25 13:59:30 -07001250 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001251 skb_push(skb, elen + hlen);
1252 skb_reset_mac_header(skb);
1253 skb_reset_network_header(skb);
1254 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001255 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001256 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001257
1258 /* fill up mac and fcoe headers */
1259 eh = eth_hdr(skb);
1260 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001261 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001262 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1263 else
1264 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001265 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001266
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001267 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1268 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001269 else
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001270 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001271
1272 hp = (struct fcoe_hdr *)(eh + 1);
1273 memset(hp, 0, sizeof(*hp));
1274 if (FC_FCOE_VER)
1275 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1276 hp->fcoe_sof = sof;
1277
Yi Zou39ca9a02009-02-27 14:07:15 -08001278 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1279 if (lp->seq_offload && fr_max_payload(fp)) {
1280 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1281 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1282 } else {
1283 skb_shinfo(skb)->gso_type = 0;
1284 skb_shinfo(skb)->gso_size = 0;
1285 }
Robert Love85b4aa42008-12-09 15:10:24 -08001286 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001287 stats = fc_lport_get_stats(lp);
1288 stats->TxFrames++;
1289 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001290
1291 /* send down to lld */
1292 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001293 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001294 fcoe_check_wait_queue(lp, skb);
1295 else if (fcoe_start_io(skb))
1296 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001297
1298 return 0;
1299}
Robert Love85b4aa42008-12-09 15:10:24 -08001300
Robert Love34f42a02009-02-27 10:55:45 -08001301/**
1302 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001303 * @arg: ptr to the fcoe per cpu struct
1304 *
1305 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001306 */
1307int fcoe_percpu_receive_thread(void *arg)
1308{
1309 struct fcoe_percpu_s *p = arg;
1310 u32 fr_len;
1311 struct fc_lport *lp;
1312 struct fcoe_rcv_info *fr;
1313 struct fcoe_dev_stats *stats;
1314 struct fc_frame_header *fh;
1315 struct sk_buff *skb;
1316 struct fcoe_crc_eof crc_eof;
1317 struct fc_frame *fp;
1318 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001319 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001320 struct fcoe_hdr *hp;
1321
Robert Love4469c192009-02-27 10:56:38 -08001322 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001323
1324 while (!kthread_should_stop()) {
1325
1326 spin_lock_bh(&p->fcoe_rx_list.lock);
1327 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1328 set_current_state(TASK_INTERRUPTIBLE);
1329 spin_unlock_bh(&p->fcoe_rx_list.lock);
1330 schedule();
1331 set_current_state(TASK_RUNNING);
1332 if (kthread_should_stop())
1333 return 0;
1334 spin_lock_bh(&p->fcoe_rx_list.lock);
1335 }
1336 spin_unlock_bh(&p->fcoe_rx_list.lock);
1337 fr = fcoe_dev_from_skb(skb);
1338 lp = fr->fr_dev;
1339 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001340 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001341 kfree_skb(skb);
1342 continue;
1343 }
1344
Robert Loved5488eb2009-06-10 15:30:59 -07001345 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1346 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1347 skb->len, skb->data_len,
1348 skb->head, skb->data, skb_tail_pointer(skb),
1349 skb_end_pointer(skb), skb->csum,
1350 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001351
1352 /*
1353 * Save source MAC address before discarding header.
1354 */
Chris Leech014f5c32009-08-25 13:59:30 -07001355 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001356 if (skb_is_nonlinear(skb))
1357 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001358 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001359
1360 /*
1361 * Frame length checks and setting up the header pointers
1362 * was done in fcoe_rcv already.
1363 */
1364 hp = (struct fcoe_hdr *) skb_network_header(skb);
1365 fh = (struct fc_frame_header *) skb_transport_header(skb);
1366
Robert Love582b45b2009-03-31 15:51:50 -07001367 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001368 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001369 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001370 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001371 "mismatch: The frame has "
1372 "version %x, but the "
1373 "initiator supports version "
1374 "%x\n", FC_FCOE_DECAPS_VER(hp),
1375 FC_FCOE_VER);
1376 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001377 kfree_skb(skb);
1378 continue;
1379 }
1380
1381 skb_pull(skb, sizeof(struct fcoe_hdr));
1382 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1383
Robert Love582b45b2009-03-31 15:51:50 -07001384 stats->RxFrames++;
1385 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001386
1387 fp = (struct fc_frame *)skb;
1388 fc_frame_init(fp);
1389 fr_dev(fp) = lp;
1390 fr_sof(fp) = hp->fcoe_sof;
1391
1392 /* Copy out the CRC and EOF trailer for access */
1393 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1394 kfree_skb(skb);
1395 continue;
1396 }
1397 fr_eof(fp) = crc_eof.fcoe_eof;
1398 fr_crc(fp) = crc_eof.fcoe_crc32;
1399 if (pskb_trim(skb, fr_len)) {
1400 kfree_skb(skb);
1401 continue;
1402 }
1403
1404 /*
1405 * We only check CRC if no offload is available and if it is
1406 * it's solicited data, in which case, the FCP layer would
1407 * check it during the copy.
1408 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001409 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001410 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1411 else
1412 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1413
1414 fh = fc_frame_header_get(fp);
1415 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1416 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001417 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001418 continue;
1419 }
1420 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1421 if (le32_to_cpu(fr_crc(fp)) !=
1422 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001423 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001424 printk(KERN_WARNING "fcoe: dropping "
1425 "frame with CRC error\n");
1426 stats->InvalidCRCCount++;
1427 stats->ErrorFrames++;
1428 fc_frame_free(fp);
1429 continue;
1430 }
1431 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1432 }
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001433 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1434 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001435 fc_frame_free(fp);
1436 continue;
1437 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001438 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001439 }
1440 return 0;
1441}
1442
1443/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001444 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1445 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001446 *
1447 * This empties the wait_queue, dequeue the head of the wait_queue queue
1448 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001449 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001450 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001451 *
1452 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001453 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001454 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001455 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001456static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001457{
Chris Leech014f5c32009-08-25 13:59:30 -07001458 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001459 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001460
Chris Leech014f5c32009-08-25 13:59:30 -07001461 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001462
1463 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001464 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001465
Chris Leech014f5c32009-08-25 13:59:30 -07001466 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001467 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001468 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001469
Chris Leech014f5c32009-08-25 13:59:30 -07001470 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001471 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001472 port->fcoe_pending_queue.qlen++;
1473 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001474
Chris Leech014f5c32009-08-25 13:59:30 -07001475 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001476 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001477 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001478
1479 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001480 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001481 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001482 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001483 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001484 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001485 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001486 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001487 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001488
Chris Leech014f5c32009-08-25 13:59:30 -07001489 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001490 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001491 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1492 mod_timer(&port->timer, jiffies + 2);
1493 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001494out:
Chris Leech014f5c32009-08-25 13:59:30 -07001495 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001496 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001497 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001498 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001499}
1500
1501/**
Robert Love34f42a02009-02-27 10:55:45 -08001502 * fcoe_dev_setup() - setup link change notification interface
1503 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001504static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001505{
Robert Love85b4aa42008-12-09 15:10:24 -08001506 register_netdevice_notifier(&fcoe_notifier);
1507}
1508
1509/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001510 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001511 */
Robert Love85b4aa42008-12-09 15:10:24 -08001512static void fcoe_dev_cleanup(void)
1513{
1514 unregister_netdevice_notifier(&fcoe_notifier);
1515}
1516
1517/**
Robert Love34f42a02009-02-27 10:55:45 -08001518 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001519 * @notifier: context of the notification
1520 * @event: type of event
1521 * @ptr: fixed array for output parsed ifname
1522 *
1523 * This function is called by the ethernet driver in case of link change event
1524 *
1525 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001526 */
Robert Love85b4aa42008-12-09 15:10:24 -08001527static int fcoe_device_notification(struct notifier_block *notifier,
1528 ulong event, void *ptr)
1529{
1530 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001531 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001532 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001533 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001534 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001535 u32 mfs;
1536 int rc = NOTIFY_OK;
1537
1538 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001539 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001540 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001541 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001542 break;
1543 }
1544 }
1545 read_unlock(&fcoe_hostlist_lock);
1546 if (lp == NULL) {
1547 rc = NOTIFY_DONE;
1548 goto out;
1549 }
1550
Robert Love85b4aa42008-12-09 15:10:24 -08001551 switch (event) {
1552 case NETDEV_DOWN:
1553 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001554 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001555 break;
1556 case NETDEV_UP:
1557 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001558 break;
1559 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001560 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1561 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001562 if (mfs >= FC_MIN_MAX_FRAME)
1563 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001564 break;
1565 case NETDEV_REGISTER:
1566 break;
1567 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001568 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001569 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001570 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001571 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001572 fcoe_ctlr_link_up(&fcoe->ctlr);
1573 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001574 stats = fc_lport_get_stats(lp);
1575 stats->LinkFailureCount++;
1576 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001577 }
1578out:
1579 return rc;
1580}
1581
1582/**
Robert Love34f42a02009-02-27 10:55:45 -08001583 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001584 * @buffer: incoming buffer to be copied
1585 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001586 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001587 */
Robert Love85b4aa42008-12-09 15:10:24 -08001588static struct net_device *fcoe_if_to_netdev(const char *buffer)
1589{
1590 char *cp;
1591 char ifname[IFNAMSIZ + 2];
1592
1593 if (buffer) {
1594 strlcpy(ifname, buffer, IFNAMSIZ);
1595 cp = ifname + strlen(ifname);
1596 while (--cp >= ifname && *cp == '\n')
1597 *cp = '\0';
1598 return dev_get_by_name(&init_net, ifname);
1599 }
1600 return NULL;
1601}
1602
1603/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001604 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001605 * @netdev: the target netdev
1606 *
1607 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001608 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001609static struct module *
1610fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001611{
1612 struct device *dev;
1613
1614 if (!netdev)
1615 return NULL;
1616
1617 dev = netdev->dev.parent;
1618 if (!dev)
1619 return NULL;
1620
1621 if (!dev->driver)
1622 return NULL;
1623
1624 return dev->driver->owner;
1625}
1626
1627/**
Robert Love34f42a02009-02-27 10:55:45 -08001628 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001629 * @netdev: the target netdev
1630 *
Robert Love34f42a02009-02-27 10:55:45 -08001631 * Holds the Ethernet driver module by try_module_get() for
1632 * the corresponding netdev.
1633 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001634 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001635 */
Robert Love85b4aa42008-12-09 15:10:24 -08001636static int fcoe_ethdrv_get(const struct net_device *netdev)
1637{
1638 struct module *owner;
1639
1640 owner = fcoe_netdev_to_module_owner(netdev);
1641 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001642 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1643 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001644 return try_module_get(owner);
1645 }
1646 return -ENODEV;
1647}
1648
1649/**
Robert Love34f42a02009-02-27 10:55:45 -08001650 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001651 * @netdev: the target netdev
1652 *
Robert Love34f42a02009-02-27 10:55:45 -08001653 * Releases the Ethernet driver module by module_put for
1654 * the corresponding netdev.
1655 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001656 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001657 */
Robert Love85b4aa42008-12-09 15:10:24 -08001658static int fcoe_ethdrv_put(const struct net_device *netdev)
1659{
1660 struct module *owner;
1661
1662 owner = fcoe_netdev_to_module_owner(netdev);
1663 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001664 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1665 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001666 module_put(owner);
1667 return 0;
1668 }
1669 return -ENODEV;
1670}
1671
1672/**
Robert Love34f42a02009-02-27 10:55:45 -08001673 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001674 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001675 * @kp: associated kernel param
1676 *
1677 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001678 */
Robert Love85b4aa42008-12-09 15:10:24 -08001679static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1680{
Robert Love85b4aa42008-12-09 15:10:24 -08001681 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -07001682 struct fcoe_interface *fcoe;
1683 struct fcoe_port *port;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001684 struct fc_lport *lport;
1685 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001686
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001687 mutex_lock(&fcoe_config_mutex);
1688#ifdef CONFIG_FCOE_MODULE
1689 /*
1690 * Make sure the module has been initialized, and is not about to be
1691 * removed. Module paramter sysfs files are writable before the
1692 * module_init function is called and after module_exit.
1693 */
1694 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1695 rc = -ENODEV;
1696 goto out_nodev;
1697 }
1698#endif
1699
Robert Love85b4aa42008-12-09 15:10:24 -08001700 netdev = fcoe_if_to_netdev(buffer);
1701 if (!netdev) {
1702 rc = -ENODEV;
1703 goto out_nodev;
1704 }
1705 /* look for existing lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001706 lport = fcoe_hostlist_lookup(netdev);
1707 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001708 rc = -ENODEV;
1709 goto out_putdev;
1710 }
Chris Leechc863df32009-08-25 14:00:18 -07001711 /* Remove the instance from fcoe's list */
1712 fcoe_hostlist_remove(lport);
Chris Leech030f4e02009-08-25 14:00:02 -07001713 port = lport_priv(lport);
1714 fcoe = port->fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001715 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001716 fcoe_ethdrv_put(netdev);
1717 rc = 0;
1718out_putdev:
1719 dev_put(netdev);
1720out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001721 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001722 return rc;
1723}
1724
1725/**
Robert Love34f42a02009-02-27 10:55:45 -08001726 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001727 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001728 * @kp: associated kernel param
1729 *
1730 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001731 */
Robert Love85b4aa42008-12-09 15:10:24 -08001732static int fcoe_create(const char *buffer, struct kernel_param *kp)
1733{
1734 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001735 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001736 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001737 struct net_device *netdev;
1738
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001739 mutex_lock(&fcoe_config_mutex);
1740#ifdef CONFIG_FCOE_MODULE
1741 /*
1742 * Make sure the module has been initialized, and is not about to be
1743 * removed. Module paramter sysfs files are writable before the
1744 * module_init function is called and after module_exit.
1745 */
1746 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1747 rc = -ENODEV;
1748 goto out_nodev;
1749 }
1750#endif
1751
Robert Love85b4aa42008-12-09 15:10:24 -08001752 netdev = fcoe_if_to_netdev(buffer);
1753 if (!netdev) {
1754 rc = -ENODEV;
1755 goto out_nodev;
1756 }
1757 /* look for existing lport */
1758 if (fcoe_hostlist_lookup(netdev)) {
1759 rc = -EEXIST;
1760 goto out_putdev;
1761 }
1762 fcoe_ethdrv_get(netdev);
1763
Chris Leech030f4e02009-08-25 14:00:02 -07001764 fcoe = fcoe_interface_create(netdev);
1765 if (!fcoe) {
1766 rc = -ENOMEM;
1767 goto out_putdev;
1768 }
1769
1770 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001771 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001772 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001773 netdev->name);
1774 fcoe_ethdrv_put(netdev);
1775 rc = -EIO;
Chris Leech030f4e02009-08-25 14:00:02 -07001776 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001777 }
Chris Leech030f4e02009-08-25 14:00:02 -07001778
Chris Leech54b649f2009-08-25 14:00:07 -07001779 /* Make this the "master" N_Port */
1780 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001781
Chris Leechc863df32009-08-25 14:00:18 -07001782 /* add to lports list */
1783 fcoe_hostlist_add(lport);
1784
Chris Leech54b649f2009-08-25 14:00:07 -07001785 /* start FIP Discovery and FLOGI */
1786 lport->boot_time = jiffies;
1787 fc_fabric_login(lport);
1788 if (!fcoe_link_ok(lport))
1789 fcoe_ctlr_link_up(&fcoe->ctlr);
1790
1791 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001792out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001793 /*
1794 * Release from init in fcoe_interface_create(), on success lport
1795 * should be holding a reference taken in fcoe_if_create().
1796 */
Chris Leech030f4e02009-08-25 14:00:02 -07001797 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001798out_putdev:
1799 dev_put(netdev);
1800out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001801 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001802 return rc;
1803}
1804
1805module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1806__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001807MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001808module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1809__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001810MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001811
Robert Love34f42a02009-02-27 10:55:45 -08001812/**
1813 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001814 * @lp: ptr to the fc_lport
1815 *
1816 * Any permanently-disqualifying conditions have been previously checked.
1817 * This also updates the speed setting, which may change with link for 100/1000.
1818 *
1819 * This function should probably be checking for PAUSE support at some point
1820 * in the future. Currently Per-priority-pause is not determinable using
1821 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1822 *
1823 * Returns: 0 if link is OK for use by FCoE.
1824 *
1825 */
1826int fcoe_link_ok(struct fc_lport *lp)
1827{
Chris Leech014f5c32009-08-25 13:59:30 -07001828 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001829 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001830 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001831
Yi Zou2f718d62009-07-29 17:04:01 -07001832 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1833 (!dev_ethtool_get_settings(dev, &ecmd))) {
1834 lp->link_supported_speeds &=
1835 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1836 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1837 SUPPORTED_1000baseT_Full))
1838 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1839 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1840 lp->link_supported_speeds |=
1841 FC_PORTSPEED_10GBIT;
1842 if (ecmd.speed == SPEED_1000)
1843 lp->link_speed = FC_PORTSPEED_1GBIT;
1844 if (ecmd.speed == SPEED_10000)
1845 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001846
Yi Zou2f718d62009-07-29 17:04:01 -07001847 return 0;
1848 }
1849 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001850}
Robert Love85b4aa42008-12-09 15:10:24 -08001851
Robert Love34f42a02009-02-27 10:55:45 -08001852/**
1853 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001854 * @lp: the fc_lport
1855 */
1856void fcoe_percpu_clean(struct fc_lport *lp)
1857{
Robert Love85b4aa42008-12-09 15:10:24 -08001858 struct fcoe_percpu_s *pp;
1859 struct fcoe_rcv_info *fr;
1860 struct sk_buff_head *list;
1861 struct sk_buff *skb, *next;
1862 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001863 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001864
Robert Love5e5e92d2009-03-17 11:41:35 -07001865 for_each_possible_cpu(cpu) {
1866 pp = &per_cpu(fcoe_percpu, cpu);
1867 spin_lock_bh(&pp->fcoe_rx_list.lock);
1868 list = &pp->fcoe_rx_list;
1869 head = list->next;
1870 for (skb = head; skb != (struct sk_buff *)list;
1871 skb = next) {
1872 next = skb->next;
1873 fr = fcoe_dev_from_skb(skb);
1874 if (fr->fr_dev == lp) {
1875 __skb_unlink(skb, list);
1876 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001877 }
Robert Love85b4aa42008-12-09 15:10:24 -08001878 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001879 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001880 }
1881}
Robert Love85b4aa42008-12-09 15:10:24 -08001882
1883/**
Robert Love34f42a02009-02-27 10:55:45 -08001884 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001885 * @lp: the corresponding fc_lport
1886 *
1887 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001888 */
Robert Love85b4aa42008-12-09 15:10:24 -08001889void fcoe_clean_pending_queue(struct fc_lport *lp)
1890{
Chris Leech014f5c32009-08-25 13:59:30 -07001891 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001892 struct sk_buff *skb;
1893
Chris Leech014f5c32009-08-25 13:59:30 -07001894 spin_lock_bh(&port->fcoe_pending_queue.lock);
1895 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1896 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001897 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001898 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001899 }
Chris Leech014f5c32009-08-25 13:59:30 -07001900 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001901}
Robert Love85b4aa42008-12-09 15:10:24 -08001902
1903/**
Robert Love34f42a02009-02-27 10:55:45 -08001904 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001905 * @shost: shost the reset is from
1906 *
1907 * Returns: always 0
1908 */
1909int fcoe_reset(struct Scsi_Host *shost)
1910{
1911 struct fc_lport *lport = shost_priv(shost);
1912 fc_lport_reset(lport);
1913 return 0;
1914}
Robert Love85b4aa42008-12-09 15:10:24 -08001915
Robert Love34f42a02009-02-27 10:55:45 -08001916/**
Chris Leech014f5c32009-08-25 13:59:30 -07001917 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001918 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001919 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001920 * Called with fcoe_hostlist_lock held.
1921 *
Chris Leech014f5c32009-08-25 13:59:30 -07001922 * Returns: NULL or the located fcoe_port
Robert Love85b4aa42008-12-09 15:10:24 -08001923 */
Chris Leech014f5c32009-08-25 13:59:30 -07001924static struct fcoe_interface *
1925fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001926{
Chris Leech014f5c32009-08-25 13:59:30 -07001927 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001928
Chris Leech014f5c32009-08-25 13:59:30 -07001929 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001930 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001931 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001932 }
Robert Love85b4aa42008-12-09 15:10:24 -08001933 return NULL;
1934}
1935
Robert Love34f42a02009-02-27 10:55:45 -08001936/**
1937 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001938 * @netdev: ptr to net_device
1939 *
1940 * Returns: 0 for success
1941 */
1942struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1943{
Chris Leech014f5c32009-08-25 13:59:30 -07001944 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001945
Vasu Deve8af4d42009-07-29 17:05:15 -07001946 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001947 fcoe = fcoe_hostlist_lookup_port(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001948 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001949
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001950 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001951}
Robert Love85b4aa42008-12-09 15:10:24 -08001952
Robert Love34f42a02009-02-27 10:55:45 -08001953/**
1954 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001955 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001956 *
1957 * Returns: 0 for success
1958 */
Chris Leech014f5c32009-08-25 13:59:30 -07001959int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001960{
Chris Leech014f5c32009-08-25 13:59:30 -07001961 struct fcoe_interface *fcoe;
1962 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001963
Chris Leechc863df32009-08-25 14:00:18 -07001964 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001965 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1966 if (!fcoe) {
1967 port = lport_priv(lport);
1968 fcoe = port->fcoe;
1969 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001970 }
Chris Leechc863df32009-08-25 14:00:18 -07001971 write_unlock_bh(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001972 return 0;
1973}
Robert Love85b4aa42008-12-09 15:10:24 -08001974
Robert Love34f42a02009-02-27 10:55:45 -08001975/**
1976 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001977 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001978 *
1979 * Returns: 0 for success
1980 */
Chris Leech014f5c32009-08-25 13:59:30 -07001981int fcoe_hostlist_remove(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001982{
Chris Leech014f5c32009-08-25 13:59:30 -07001983 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001984
Vasu Deve8af4d42009-07-29 17:05:15 -07001985 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001986 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1987 BUG_ON(!fcoe);
1988 list_del(&fcoe->list);
Robert Love85b4aa42008-12-09 15:10:24 -08001989 write_unlock_bh(&fcoe_hostlist_lock);
1990
1991 return 0;
1992}
Robert Love85b4aa42008-12-09 15:10:24 -08001993
1994/**
Robert Love34f42a02009-02-27 10:55:45 -08001995 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001996 *
Robert Love85b4aa42008-12-09 15:10:24 -08001997 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001998 */
Robert Love85b4aa42008-12-09 15:10:24 -08001999static int __init fcoe_init(void)
2000{
Robert Love38eccab2009-03-17 11:41:30 -07002001 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002002 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002003 struct fcoe_percpu_s *p;
2004
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002005 mutex_lock(&fcoe_config_mutex);
2006
Robert Love38eccab2009-03-17 11:41:30 -07002007 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002008 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002009 skb_queue_head_init(&p->fcoe_rx_list);
2010 }
2011
Robert Love8976f422009-03-17 11:41:46 -07002012 for_each_online_cpu(cpu)
2013 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002014
Robert Love8976f422009-03-17 11:41:46 -07002015 /* Initialize per CPU interrupt thread */
2016 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2017 if (rc)
2018 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002019
Robert Love8976f422009-03-17 11:41:46 -07002020 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002021 fcoe_dev_setup();
2022
Chris Leech5892c322009-08-25 13:59:14 -07002023 rc = fcoe_if_init();
2024 if (rc)
2025 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002026
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002027 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002028 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002029
2030out_free:
2031 for_each_online_cpu(cpu) {
2032 fcoe_percpu_thread_destroy(cpu);
2033 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002034 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002035 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002036}
2037module_init(fcoe_init);
2038
2039/**
Robert Love34f42a02009-02-27 10:55:45 -08002040 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002041 *
2042 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002043 */
Robert Love85b4aa42008-12-09 15:10:24 -08002044static void __exit fcoe_exit(void)
2045{
Robert Love5e5e92d2009-03-17 11:41:35 -07002046 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002047 struct fcoe_interface *fcoe, *tmp;
Chris Leechc863df32009-08-25 14:00:18 -07002048 LIST_HEAD(local_list);
Robert Love85b4aa42008-12-09 15:10:24 -08002049
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002050 mutex_lock(&fcoe_config_mutex);
2051
Robert Love85b4aa42008-12-09 15:10:24 -08002052 fcoe_dev_cleanup();
2053
Vasu Dev5919a592009-03-27 09:03:29 -07002054 /* releases the associated fcoe hosts */
Chris Leechc863df32009-08-25 14:00:18 -07002055 write_lock_bh(&fcoe_hostlist_lock);
2056 list_splice_init(&fcoe_hostlist, &local_list);
2057 write_unlock_bh(&fcoe_hostlist_lock);
2058
2059 list_for_each_entry_safe(fcoe, tmp, &local_list, list) {
2060 list_del(&fcoe->list);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002061 fcoe_if_destroy(fcoe->ctlr.lp);
Chris Leechc863df32009-08-25 14:00:18 -07002062 }
Robert Love85b4aa42008-12-09 15:10:24 -08002063
Robert Love8976f422009-03-17 11:41:46 -07002064 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2065
Chris Leech014f5c32009-08-25 13:59:30 -07002066 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002067 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002068
Vasu Dev7f349142009-03-27 09:06:31 -07002069 /* detach from scsi transport */
2070 fcoe_if_exit();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002071
2072 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002073}
2074module_exit(fcoe_exit);