blob: 8ad126556249a11c6f89eb764e209fcb007b5107 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3 *
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 -080048static int debug_fcoe;
49
Robert Love85b4aa42008-12-09 15:10:24 -080050MODULE_AUTHOR("Open-FCoE.org");
51MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070052MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080053
54/* fcoe host list */
55LIST_HEAD(fcoe_hostlist);
56DEFINE_RWLOCK(fcoe_hostlist_lock);
57DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
Robert Love5e5e92d2009-03-17 11:41:35 -070058DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080059
Chris Leechdd3fd722009-04-21 16:27:36 -070060/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070061static int fcoe_reset(struct Scsi_Host *shost);
62static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
63static int fcoe_rcv(struct sk_buff *, struct net_device *,
64 struct packet_type *, struct net_device *);
65static int fcoe_percpu_receive_thread(void *arg);
66static void fcoe_clean_pending_queue(struct fc_lport *lp);
67static void fcoe_percpu_clean(struct fc_lport *lp);
68static int fcoe_link_ok(struct fc_lport *lp);
69
70static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
71static int fcoe_hostlist_add(const struct fc_lport *);
72static int fcoe_hostlist_remove(const struct fc_lport *);
73
Robert Love85b4aa42008-12-09 15:10:24 -080074static int fcoe_check_wait_queue(struct fc_lport *);
Robert Love85b4aa42008-12-09 15:10:24 -080075static int fcoe_device_notification(struct notifier_block *, ulong, void *);
76static void fcoe_dev_setup(void);
77static void fcoe_dev_cleanup(void);
78
79/* notification function from net device */
80static struct notifier_block fcoe_notifier = {
81 .notifier_call = fcoe_device_notification,
82};
83
Vasu Dev7f349142009-03-27 09:06:31 -070084static struct scsi_transport_template *scsi_transport_fcoe_sw;
85
86struct fc_function_template fcoe_transport_function = {
87 .show_host_node_name = 1,
88 .show_host_port_name = 1,
89 .show_host_supported_classes = 1,
90 .show_host_supported_fc4s = 1,
91 .show_host_active_fc4s = 1,
92 .show_host_maxframe_size = 1,
93
94 .show_host_port_id = 1,
95 .show_host_supported_speeds = 1,
96 .get_host_speed = fc_get_host_speed,
97 .show_host_speed = 1,
98 .show_host_port_type = 1,
99 .get_host_port_state = fc_get_host_port_state,
100 .show_host_port_state = 1,
101 .show_host_symbolic_name = 1,
102
103 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
104 .show_rport_maxframe_size = 1,
105 .show_rport_supported_classes = 1,
106
107 .show_host_fabric_name = 1,
108 .show_starget_node_name = 1,
109 .show_starget_port_name = 1,
110 .show_starget_port_id = 1,
111 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
112 .show_rport_dev_loss_tmo = 1,
113 .get_fc_host_stats = fc_get_host_stats,
114 .issue_fc_host_lip = fcoe_reset,
115
116 .terminate_rport_io = fc_rport_terminate_io,
117};
118
119static struct scsi_host_template fcoe_shost_template = {
120 .module = THIS_MODULE,
121 .name = "FCoE Driver",
122 .proc_name = FCOE_NAME,
123 .queuecommand = fc_queuecommand,
124 .eh_abort_handler = fc_eh_abort,
125 .eh_device_reset_handler = fc_eh_device_reset,
126 .eh_host_reset_handler = fc_eh_host_reset,
127 .slave_alloc = fc_slave_alloc,
128 .change_queue_depth = fc_change_queue_depth,
129 .change_queue_type = fc_change_queue_type,
130 .this_id = -1,
131 .cmd_per_lun = 32,
132 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
133 .use_clustering = ENABLE_CLUSTERING,
134 .sg_tablesize = SG_ALL,
135 .max_sectors = 0xffff,
136};
137
138/**
139 * fcoe_lport_config() - sets up the fc_lport
140 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700141 *
142 * Returns: 0 for success
143 */
144static int fcoe_lport_config(struct fc_lport *lp)
145{
146 lp->link_up = 0;
147 lp->qfull = 0;
148 lp->max_retry_count = 3;
149 lp->e_d_tov = 2 * 1000; /* FC-FS default */
150 lp->r_a_tov = 2 * 2 * 1000;
151 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
152 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
153
154 fc_lport_init_stats(lp);
155
156 /* lport fc_lport related configuration */
157 fc_lport_config(lp);
158
159 /* offload related configuration */
160 lp->crc_offload = 0;
161 lp->seq_offload = 0;
162 lp->lro_enabled = 0;
163 lp->lro_xid = 0;
164 lp->lso_max = 0;
165
166 return 0;
167}
168
169/**
170 * fcoe_netdev_config() - Set up netdev for SW FCoE
171 * @lp : ptr to the fc_lport
172 * @netdev : ptr to the associated netdevice struct
173 *
174 * Must be called after fcoe_lport_config() as it will use lport mutex
175 *
176 * Returns : 0 for success
177 */
178static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
179{
180 u32 mfs;
181 u64 wwnn, wwpn;
182 struct fcoe_softc *fc;
183 u8 flogi_maddr[ETH_ALEN];
184
185 /* Setup lport private data to point to fcoe softc */
186 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700187 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700188 fc->real_dev = netdev;
189 fc->phys_dev = netdev;
190
191 /* Require support for get_pauseparam ethtool op. */
192 if (netdev->priv_flags & IFF_802_1Q_VLAN)
193 fc->phys_dev = vlan_dev_real_dev(netdev);
194
195 /* Do not support for bonding device */
196 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
197 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
198 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
199 return -EOPNOTSUPP;
200 }
201
202 /*
203 * Determine max frame size based on underlying device and optional
204 * user-configured limit. If the MFS is too low, fcoe_link_ok()
205 * will return 0, so do this first.
206 */
207 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
208 sizeof(struct fcoe_crc_eof));
209 if (fc_set_mfs(lp, mfs))
210 return -EINVAL;
211
Vasu Dev7f349142009-03-27 09:06:31 -0700212 /* offload features support */
213 if (fc->real_dev->features & NETIF_F_SG)
214 lp->sg_supp = 1;
215
216#ifdef NETIF_F_FCOE_CRC
217 if (netdev->features & NETIF_F_FCOE_CRC) {
218 lp->crc_offload = 1;
219 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
220 netdev->name);
221 }
222#endif
223#ifdef NETIF_F_FSO
224 if (netdev->features & NETIF_F_FSO) {
225 lp->seq_offload = 1;
226 lp->lso_max = netdev->gso_max_size;
227 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
228 netdev->name, lp->lso_max);
229 }
230#endif
231 if (netdev->fcoe_ddp_xid) {
232 lp->lro_enabled = 1;
233 lp->lro_xid = netdev->fcoe_ddp_xid;
234 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
235 netdev->name, lp->lro_xid);
236 }
237 skb_queue_head_init(&fc->fcoe_pending_queue);
238 fc->fcoe_pending_queue_active = 0;
239
240 /* setup Source Mac Address */
Joe Eykholt97c83892009-03-17 11:42:40 -0700241 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
Vasu Dev7f349142009-03-27 09:06:31 -0700242 fc->real_dev->addr_len);
243
244 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
245 fc_set_wwnn(lp, wwnn);
246 /* XXX - 3rd arg needs to be vlan id */
247 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
248 fc_set_wwpn(lp, wwpn);
249
250 /*
251 * Add FCoE MAC address as second unicast MAC address
252 * or enter promiscuous mode if not capable of listening
253 * for multiple unicast MACs.
254 */
255 rtnl_lock();
256 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
257 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
258 rtnl_unlock();
259
260 /*
261 * setup the receive function from ethernet driver
262 * on the ethertype for the given device
263 */
264 fc->fcoe_packet_type.func = fcoe_rcv;
265 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
266 fc->fcoe_packet_type.dev = fc->real_dev;
267 dev_add_pack(&fc->fcoe_packet_type);
268
269 return 0;
270}
271
272/**
273 * fcoe_shost_config() - Sets up fc_lport->host
274 * @lp : ptr to the fc_lport
275 * @shost : ptr to the associated scsi host
276 * @dev : device associated to scsi host
277 *
278 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
279 *
280 * Returns : 0 for success
281 */
282static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
283 struct device *dev)
284{
285 int rc = 0;
286
287 /* lport scsi host config */
288 lp->host = shost;
289
290 lp->host->max_lun = FCOE_MAX_LUN;
291 lp->host->max_id = FCOE_MAX_FCP_TARGET;
292 lp->host->max_channel = 0;
293 lp->host->transportt = scsi_transport_fcoe_sw;
294
295 /* add the new host to the SCSI-ml */
296 rc = scsi_add_host(lp->host, dev);
297 if (rc) {
298 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
299 return rc;
300 }
301 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
302 FCOE_NAME, FCOE_VERSION,
303 fcoe_netdev(lp)->name);
304
305 return 0;
306}
307
308/**
309 * fcoe_em_config() - allocates em for this lport
310 * @lp: the port that em is to allocated for
311 *
312 * Returns : 0 on success
313 */
314static inline int fcoe_em_config(struct fc_lport *lp)
315{
316 BUG_ON(lp->emp);
317
318 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
319 FCOE_MIN_XID, FCOE_MAX_XID);
320 if (!lp->emp)
321 return -ENOMEM;
322
323 return 0;
324}
325
326/**
327 * fcoe_if_destroy() - FCoE software HBA tear-down function
328 * @netdev: ptr to the associated net_device
329 *
330 * Returns: 0 if link is OK for use by FCoE.
331 */
332static int fcoe_if_destroy(struct net_device *netdev)
333{
334 struct fc_lport *lp = NULL;
335 struct fcoe_softc *fc;
336 u8 flogi_maddr[ETH_ALEN];
337
338 BUG_ON(!netdev);
339
340 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
341 netdev->name);
342
343 lp = fcoe_hostlist_lookup(netdev);
344 if (!lp)
345 return -ENODEV;
346
347 fc = lport_priv(lp);
348
349 /* Logout of the fabric */
350 fc_fabric_logoff(lp);
351
352 /* Remove the instance from fcoe's list */
353 fcoe_hostlist_remove(lp);
354
355 /* Don't listen for Ethernet packets anymore */
356 dev_remove_pack(&fc->fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700357 dev_remove_pack(&fc->fip_packet_type);
358 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700359
360 /* Cleanup the fc_lport */
361 fc_lport_destroy(lp);
362 fc_fcp_destroy(lp);
363
364 /* Detach from the scsi-ml */
365 fc_remove_host(lp->host);
366 scsi_remove_host(lp->host);
367
368 /* There are no more rports or I/O, free the EM */
369 if (lp->emp)
370 fc_exch_mgr_free(lp->emp);
371
372 /* Delete secondary MAC addresses */
373 rtnl_lock();
374 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
375 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700376 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
377 dev_unicast_delete(fc->real_dev,
378 fc->ctlr.data_src_addr, ETH_ALEN);
379 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700380 rtnl_unlock();
381
Chris Leechdd3fd722009-04-21 16:27:36 -0700382 /* Free the per-CPU receive threads */
Vasu Dev7f349142009-03-27 09:06:31 -0700383 fcoe_percpu_clean(lp);
384
385 /* Free existing skbs */
386 fcoe_clean_pending_queue(lp);
387
388 /* Free memory used by statistical counters */
389 fc_lport_free_stats(lp);
390
391 /* Release the net_device and Scsi_Host */
392 dev_put(fc->real_dev);
393 scsi_host_put(lp->host);
394
395 return 0;
396}
397
398/*
399 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
400 * @lp: the corresponding fc_lport
401 * @xid: the exchange id for this ddp transfer
402 * @sgl: the scatterlist describing this transfer
403 * @sgc: number of sg items
404 *
405 * Returns : 0 no ddp
406 */
407static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
408 struct scatterlist *sgl, unsigned int sgc)
409{
410 struct net_device *n = fcoe_netdev(lp);
411
412 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
413 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
414
415 return 0;
416}
417
418/*
419 * fcoe_ddp_done - calls LLD's ddp_done through net_device
420 * @lp: the corresponding fc_lport
421 * @xid: the exchange id for this ddp transfer
422 *
423 * Returns : the length of data that have been completed by ddp
424 */
425static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
426{
427 struct net_device *n = fcoe_netdev(lp);
428
429 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
430 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
431 return 0;
432}
433
434static struct libfc_function_template fcoe_libfc_fcn_templ = {
435 .frame_send = fcoe_xmit,
436 .ddp_setup = fcoe_ddp_setup,
437 .ddp_done = fcoe_ddp_done,
438};
439
440/**
Joe Eykholt97c83892009-03-17 11:42:40 -0700441 * fcoe_fip_recv - handle a received FIP frame.
442 * @skb: the receive skb
443 * @dev: associated &net_device
444 * @ptype: the &packet_type structure which was used to register this handler.
445 * @orig_dev: original receive &net_device, in case @dev is a bond.
446 *
447 * Returns: 0 for success
448 */
449static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
450 struct packet_type *ptype,
451 struct net_device *orig_dev)
452{
453 struct fcoe_softc *fc;
454
455 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
456 fcoe_ctlr_recv(&fc->ctlr, skb);
457 return 0;
458}
459
460/**
461 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
462 * @fip: FCoE controller.
463 * @skb: FIP Packet.
464 */
465static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
466{
467 skb->dev = fcoe_from_ctlr(fip)->real_dev;
468 dev_queue_xmit(skb);
469}
470
471/**
472 * fcoe_update_src_mac() - Update Ethernet MAC filters.
473 * @fip: FCoE controller.
474 * @old: Unicast MAC address to delete if the MAC is non-zero.
475 * @new: Unicast MAC address to add.
476 *
477 * Remove any previously-set unicast MAC filter.
478 * Add secondary FCoE MAC address filter for our OUI.
479 */
480static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
481{
482 struct fcoe_softc *fc;
483
484 fc = fcoe_from_ctlr(fip);
485 rtnl_lock();
486 if (!is_zero_ether_addr(old))
487 dev_unicast_delete(fc->real_dev, old, ETH_ALEN);
488 dev_unicast_add(fc->real_dev, new, ETH_ALEN);
489 rtnl_unlock();
490}
491
492/**
Vasu Dev7f349142009-03-27 09:06:31 -0700493 * fcoe_if_create() - this function creates the fcoe interface
494 * @netdev: pointer the associated netdevice
495 *
496 * Creates fc_lport struct and scsi_host for lport, configures lport
497 * and starts fabric login.
498 *
499 * Returns : 0 on success
500 */
501static int fcoe_if_create(struct net_device *netdev)
502{
503 int rc;
504 struct fc_lport *lp = NULL;
505 struct fcoe_softc *fc;
506 struct Scsi_Host *shost;
507
508 BUG_ON(!netdev);
509
510 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
511 netdev->name);
512
513 lp = fcoe_hostlist_lookup(netdev);
514 if (lp)
515 return -EEXIST;
516
Vasu Deva0a25da2009-03-17 11:42:29 -0700517 shost = libfc_host_alloc(&fcoe_shost_template,
518 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700519 if (!shost) {
520 FC_DBG("Could not allocate host structure\n");
521 return -ENOMEM;
522 }
523 lp = shost_priv(shost);
524 fc = lport_priv(lp);
525
526 /* configure fc_lport, e.g., em */
527 rc = fcoe_lport_config(lp);
528 if (rc) {
529 FC_DBG("Could not configure lport\n");
530 goto out_host_put;
531 }
532
533 /* configure lport network properties */
534 rc = fcoe_netdev_config(lp, netdev);
535 if (rc) {
536 FC_DBG("Could not configure netdev for lport\n");
537 goto out_host_put;
538 }
539
Joe Eykholt97c83892009-03-17 11:42:40 -0700540 /*
541 * Initialize FIP.
542 */
543 fcoe_ctlr_init(&fc->ctlr);
544 fc->ctlr.send = fcoe_fip_send;
545 fc->ctlr.update_mac = fcoe_update_src_mac;
546
547 fc->fip_packet_type.func = fcoe_fip_recv;
548 fc->fip_packet_type.type = htons(ETH_P_FIP);
549 fc->fip_packet_type.dev = fc->real_dev;
550 dev_add_pack(&fc->fip_packet_type);
551
Vasu Dev7f349142009-03-27 09:06:31 -0700552 /* configure lport scsi host properties */
553 rc = fcoe_shost_config(lp, shost, &netdev->dev);
554 if (rc) {
555 FC_DBG("Could not configure shost for lport\n");
556 goto out_host_put;
557 }
558
559 /* lport exch manager allocation */
560 rc = fcoe_em_config(lp);
561 if (rc) {
562 FC_DBG("Could not configure em for lport\n");
563 goto out_host_put;
564 }
565
566 /* Initialize the library */
567 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
568 if (rc) {
569 FC_DBG("Could not configure libfc for lport!\n");
570 goto out_lp_destroy;
571 }
572
573 /* add to lports list */
574 fcoe_hostlist_add(lp);
575
576 lp->boot_time = jiffies;
577
578 fc_fabric_login(lp);
579
Joe Eykholt97c83892009-03-17 11:42:40 -0700580 if (!fcoe_link_ok(lp))
581 fcoe_ctlr_link_up(&fc->ctlr);
582
Vasu Dev7f349142009-03-27 09:06:31 -0700583 dev_hold(netdev);
584
585 return rc;
586
587out_lp_destroy:
588 fc_exch_mgr_free(lp->emp); /* Free the EM */
589out_host_put:
590 scsi_host_put(lp->host);
591 return rc;
592}
593
594/**
595 * fcoe_if_init() - attach to scsi transport
596 *
597 * Returns : 0 on success
598 */
599static int __init fcoe_if_init(void)
600{
601 /* attach to scsi transport */
602 scsi_transport_fcoe_sw =
603 fc_attach_transport(&fcoe_transport_function);
604
605 if (!scsi_transport_fcoe_sw) {
606 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
607 return -ENODEV;
608 }
609
610 return 0;
611}
612
613/**
614 * fcoe_if_exit() - detach from scsi transport
615 *
616 * Returns : 0 on success
617 */
618int __exit fcoe_if_exit(void)
619{
620 fc_release_transport(scsi_transport_fcoe_sw);
621 return 0;
622}
623
Robert Love85b4aa42008-12-09 15:10:24 -0800624/**
Robert Love8976f422009-03-17 11:41:46 -0700625 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
626 * @cpu: cpu index for the online cpu
627 */
628static void fcoe_percpu_thread_create(unsigned int cpu)
629{
630 struct fcoe_percpu_s *p;
631 struct task_struct *thread;
632
633 p = &per_cpu(fcoe_percpu, cpu);
634
635 thread = kthread_create(fcoe_percpu_receive_thread,
636 (void *)p, "fcoethread/%d", cpu);
637
638 if (likely(!IS_ERR(p->thread))) {
639 kthread_bind(thread, cpu);
640 wake_up_process(thread);
641
642 spin_lock_bh(&p->fcoe_rx_list.lock);
643 p->thread = thread;
644 spin_unlock_bh(&p->fcoe_rx_list.lock);
645 }
646}
647
648/**
649 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
650 * @cpu: cpu index the rx thread is to be removed
651 *
652 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
653 * current CPU's Rx thread. If the thread being destroyed is bound to
654 * the CPU processing this context the skbs will be freed.
655 */
656static void fcoe_percpu_thread_destroy(unsigned int cpu)
657{
658 struct fcoe_percpu_s *p;
659 struct task_struct *thread;
660 struct page *crc_eof;
661 struct sk_buff *skb;
662#ifdef CONFIG_SMP
663 struct fcoe_percpu_s *p0;
664 unsigned targ_cpu = smp_processor_id();
665#endif /* CONFIG_SMP */
666
667 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
668
669 /* Prevent any new skbs from being queued for this CPU. */
670 p = &per_cpu(fcoe_percpu, cpu);
671 spin_lock_bh(&p->fcoe_rx_list.lock);
672 thread = p->thread;
673 p->thread = NULL;
674 crc_eof = p->crc_eof_page;
675 p->crc_eof_page = NULL;
676 p->crc_eof_offset = 0;
677 spin_unlock_bh(&p->fcoe_rx_list.lock);
678
679#ifdef CONFIG_SMP
680 /*
681 * Don't bother moving the skb's if this context is running
682 * on the same CPU that is having its thread destroyed. This
683 * can easily happen when the module is removed.
684 */
685 if (cpu != targ_cpu) {
686 p0 = &per_cpu(fcoe_percpu, targ_cpu);
687 spin_lock_bh(&p0->fcoe_rx_list.lock);
688 if (p0->thread) {
689 FC_DBG("Moving frames from CPU %d to CPU %d\n",
690 cpu, targ_cpu);
691
692 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
693 __skb_queue_tail(&p0->fcoe_rx_list, skb);
694 spin_unlock_bh(&p0->fcoe_rx_list.lock);
695 } else {
696 /*
697 * The targeted CPU is not initialized and cannot accept
698 * new skbs. Unlock the targeted CPU and drop the skbs
699 * on the CPU that is going offline.
700 */
701 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
702 kfree_skb(skb);
703 spin_unlock_bh(&p0->fcoe_rx_list.lock);
704 }
705 } else {
706 /*
707 * This scenario occurs when the module is being removed
708 * and all threads are being destroyed. skbs will continue
709 * to be shifted from the CPU thread that is being removed
710 * to the CPU thread associated with the CPU that is processing
711 * the module removal. Once there is only one CPU Rx thread it
712 * will reach this case and we will drop all skbs and later
713 * stop the thread.
714 */
715 spin_lock_bh(&p->fcoe_rx_list.lock);
716 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
717 kfree_skb(skb);
718 spin_unlock_bh(&p->fcoe_rx_list.lock);
719 }
720#else
721 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700722 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700723 * being removed. Free all skbs and stop the thread.
724 */
725 spin_lock_bh(&p->fcoe_rx_list.lock);
726 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
727 kfree_skb(skb);
728 spin_unlock_bh(&p->fcoe_rx_list.lock);
729#endif
730
731 if (thread)
732 kthread_stop(thread);
733
734 if (crc_eof)
735 put_page(crc_eof);
736}
737
738/**
739 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
740 * @nfb: callback data block
741 * @action: event triggering the callback
742 * @hcpu: index for the cpu of this event
743 *
744 * This creates or destroys per cpu data for fcoe
745 *
746 * Returns NOTIFY_OK always.
747 */
748static int fcoe_cpu_callback(struct notifier_block *nfb,
749 unsigned long action, void *hcpu)
750{
751 unsigned cpu = (unsigned long)hcpu;
752
753 switch (action) {
754 case CPU_ONLINE:
755 case CPU_ONLINE_FROZEN:
756 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
757 fcoe_percpu_thread_create(cpu);
758 break;
759 case CPU_DEAD:
760 case CPU_DEAD_FROZEN:
761 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
762 fcoe_percpu_thread_destroy(cpu);
763 break;
764 default:
765 break;
766 }
767 return NOTIFY_OK;
768}
769
770static struct notifier_block fcoe_cpu_notifier = {
771 .notifier_call = fcoe_cpu_callback,
772};
773
774/**
Robert Love34f42a02009-02-27 10:55:45 -0800775 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800776 * @skb: the receive skb
777 * @dev: associated net device
778 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700779 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800780 *
781 * this function will receive the packet and build fc frame and pass it up
782 *
783 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800784 */
Robert Love85b4aa42008-12-09 15:10:24 -0800785int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
786 struct packet_type *ptype, struct net_device *olddev)
787{
788 struct fc_lport *lp;
789 struct fcoe_rcv_info *fr;
790 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800791 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800792 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700793 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700794 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800795
796 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700797 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800798 if (unlikely(lp == NULL)) {
799 FC_DBG("cannot find hba structure");
800 goto err2;
801 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700802 if (!lp->link_up)
803 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800804
805 if (unlikely(debug_fcoe)) {
806 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
807 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
808 skb->head, skb->data, skb_tail_pointer(skb),
809 skb_end_pointer(skb), skb->csum,
810 skb->dev ? skb->dev->name : "<NULL>");
811
812 }
813
814 /* check for FCOE packet type */
815 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
816 FC_DBG("wrong FC type frame");
817 goto err;
818 }
819
820 /*
821 * Check for minimum frame length, and make sure required FCoE
822 * and FC headers are pulled into the linear data area.
823 */
824 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
825 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
826 goto err;
827
828 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
829 fh = (struct fc_frame_header *) skb_transport_header(skb);
830
831 oxid = ntohs(fh->fh_ox_id);
832
833 fr = fcoe_dev_from_skb(skb);
834 fr->fr_dev = lp;
835 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700836
Robert Love85b4aa42008-12-09 15:10:24 -0800837#ifdef CONFIG_SMP
838 /*
839 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700840 * cpu bits to get cpu and then this cpu is used for selecting
841 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800842 */
Robert Love8976f422009-03-17 11:41:46 -0700843 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800844#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700845
Robert Love8976f422009-03-17 11:41:46 -0700846 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800847 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700848 if (unlikely(!fps->thread)) {
849 /*
850 * The targeted CPU is not ready, let's target
851 * the first CPU now. For non-SMP systems this
852 * will check the same CPU twice.
853 */
854 FC_DBG("CPU is online, but no receive thread ready "
855 "for incoming skb- using first online CPU.\n");
856
857 spin_unlock_bh(&fps->fcoe_rx_list.lock);
858 cpu = first_cpu(cpu_online_map);
859 fps = &per_cpu(fcoe_percpu, cpu);
860 spin_lock_bh(&fps->fcoe_rx_list.lock);
861 if (!fps->thread) {
862 spin_unlock_bh(&fps->fcoe_rx_list.lock);
863 goto err;
864 }
865 }
866
867 /*
868 * We now have a valid CPU that we're targeting for
869 * this skb. We also have this receive thread locked,
870 * so we're free to queue skbs into it's queue.
871 */
Robert Love85b4aa42008-12-09 15:10:24 -0800872 __skb_queue_tail(&fps->fcoe_rx_list, skb);
873 if (fps->fcoe_rx_list.qlen == 1)
874 wake_up_process(fps->thread);
875
876 spin_unlock_bh(&fps->fcoe_rx_list.lock);
877
878 return 0;
879err:
Robert Love582b45b2009-03-31 15:51:50 -0700880 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800881
882err2:
883 kfree_skb(skb);
884 return -1;
885}
Robert Love85b4aa42008-12-09 15:10:24 -0800886
887/**
Robert Love34f42a02009-02-27 10:55:45 -0800888 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800889 * @skb: the skb to be xmitted
890 *
891 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800892 */
Robert Love85b4aa42008-12-09 15:10:24 -0800893static inline int fcoe_start_io(struct sk_buff *skb)
894{
895 int rc;
896
897 skb_get(skb);
898 rc = dev_queue_xmit(skb);
899 if (rc != 0)
900 return rc;
901 kfree_skb(skb);
902 return 0;
903}
904
905/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700906 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800907 * @skb: the skb to be xmitted
908 * @tlen: total len
909 *
910 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800911 */
Robert Love85b4aa42008-12-09 15:10:24 -0800912static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
913{
914 struct fcoe_percpu_s *fps;
915 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800916
Robert Love5e5e92d2009-03-17 11:41:35 -0700917 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800918 page = fps->crc_eof_page;
919 if (!page) {
920 page = alloc_page(GFP_ATOMIC);
921 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700922 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800923 return -ENOMEM;
924 }
925 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700926 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800927 }
928
929 get_page(page);
930 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
931 fps->crc_eof_offset, tlen);
932 skb->len += tlen;
933 skb->data_len += tlen;
934 skb->truesize += tlen;
935 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
936
937 if (fps->crc_eof_offset >= PAGE_SIZE) {
938 fps->crc_eof_page = NULL;
939 fps->crc_eof_offset = 0;
940 put_page(page);
941 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700942 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800943 return 0;
944}
945
946/**
Robert Love34f42a02009-02-27 10:55:45 -0800947 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -0700948 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -0800949 *
950 * This uses crc32() to calculate the crc for fc frame
951 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800952 */
Robert Love85b4aa42008-12-09 15:10:24 -0800953u32 fcoe_fc_crc(struct fc_frame *fp)
954{
955 struct sk_buff *skb = fp_skb(fp);
956 struct skb_frag_struct *frag;
957 unsigned char *data;
958 unsigned long off, len, clen;
959 u32 crc;
960 unsigned i;
961
962 crc = crc32(~0, skb->data, skb_headlen(skb));
963
964 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
965 frag = &skb_shinfo(skb)->frags[i];
966 off = frag->page_offset;
967 len = frag->size;
968 while (len > 0) {
969 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
970 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
971 KM_SKB_DATA_SOFTIRQ);
972 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
973 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
974 off += clen;
975 len -= clen;
976 }
977 }
978 return crc;
979}
Robert Love85b4aa42008-12-09 15:10:24 -0800980
981/**
Robert Love34f42a02009-02-27 10:55:45 -0800982 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -0800983 * @lp: the associated local port
984 * @fp: the fc_frame to be transmitted
985 *
986 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800987 */
Robert Love85b4aa42008-12-09 15:10:24 -0800988int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
989{
990 int wlen, rc = 0;
991 u32 crc;
992 struct ethhdr *eh;
993 struct fcoe_crc_eof *cp;
994 struct sk_buff *skb;
995 struct fcoe_dev_stats *stats;
996 struct fc_frame_header *fh;
997 unsigned int hlen; /* header length implies the version */
998 unsigned int tlen; /* trailer length */
999 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001000 struct fcoe_softc *fc;
1001 u8 sof, eof;
1002 struct fcoe_hdr *hp;
1003
1004 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1005
Robert Lovefc47ff62009-02-27 10:55:55 -08001006 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001007 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001008 skb = fp_skb(fp);
1009 wlen = skb->len / FCOE_WORD_TO_BYTE;
1010
1011 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001012 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001013 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001014 }
1015
Joe Eykholt97c83892009-03-17 11:42:40 -07001016 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1017 fcoe_ctlr_els_send(&fc->ctlr, skb))
1018 return 0;
1019
Robert Love85b4aa42008-12-09 15:10:24 -08001020 sof = fr_sof(fp);
1021 eof = fr_eof(fp);
1022
1023 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
1024 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
1025 hlen = sizeof(struct fcoe_hdr);
1026 tlen = sizeof(struct fcoe_crc_eof);
1027 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1028
1029 /* crc offload */
1030 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001031 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001032 skb->csum_start = skb_headroom(skb);
1033 skb->csum_offset = skb->len;
1034 crc = 0;
1035 } else {
1036 skb->ip_summed = CHECKSUM_NONE;
1037 crc = fcoe_fc_crc(fp);
1038 }
1039
1040 /* copy fc crc and eof to the skb buff */
1041 if (skb_is_nonlinear(skb)) {
1042 skb_frag_t *frag;
1043 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001044 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001045 return -ENOMEM;
1046 }
1047 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1048 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1049 + frag->page_offset;
1050 } else {
1051 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1052 }
1053
1054 memset(cp, 0, sizeof(*cp));
1055 cp->fcoe_eof = eof;
1056 cp->fcoe_crc32 = cpu_to_le32(~crc);
1057
1058 if (skb_is_nonlinear(skb)) {
1059 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1060 cp = NULL;
1061 }
1062
Chris Leechdd3fd722009-04-21 16:27:36 -07001063 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001064 skb_push(skb, elen + hlen);
1065 skb_reset_mac_header(skb);
1066 skb_reset_network_header(skb);
1067 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001068 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001069 skb->dev = fc->real_dev;
1070
1071 /* fill up mac and fcoe headers */
1072 eh = eth_hdr(skb);
1073 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001074 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001075 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1076 else
1077 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001078 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001079
Joe Eykholt97c83892009-03-17 11:42:40 -07001080 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1081 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001082 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001083 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001084
1085 hp = (struct fcoe_hdr *)(eh + 1);
1086 memset(hp, 0, sizeof(*hp));
1087 if (FC_FCOE_VER)
1088 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1089 hp->fcoe_sof = sof;
1090
Yi Zou39ca9a02009-02-27 14:07:15 -08001091#ifdef NETIF_F_FSO
1092 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1093 if (lp->seq_offload && fr_max_payload(fp)) {
1094 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1095 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1096 } else {
1097 skb_shinfo(skb)->gso_type = 0;
1098 skb_shinfo(skb)->gso_size = 0;
1099 }
1100#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001101 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001102 stats = fc_lport_get_stats(lp);
1103 stats->TxFrames++;
1104 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001105
1106 /* send down to lld */
1107 fr_dev(fp) = lp;
1108 if (fc->fcoe_pending_queue.qlen)
1109 rc = fcoe_check_wait_queue(lp);
1110
1111 if (rc == 0)
1112 rc = fcoe_start_io(skb);
1113
1114 if (rc) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001115 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1116 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1117 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001118 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -08001119 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001120 }
1121
1122 return 0;
1123}
Robert Love85b4aa42008-12-09 15:10:24 -08001124
Robert Love34f42a02009-02-27 10:55:45 -08001125/**
1126 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001127 * @arg: ptr to the fcoe per cpu struct
1128 *
1129 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001130 */
1131int fcoe_percpu_receive_thread(void *arg)
1132{
1133 struct fcoe_percpu_s *p = arg;
1134 u32 fr_len;
1135 struct fc_lport *lp;
1136 struct fcoe_rcv_info *fr;
1137 struct fcoe_dev_stats *stats;
1138 struct fc_frame_header *fh;
1139 struct sk_buff *skb;
1140 struct fcoe_crc_eof crc_eof;
1141 struct fc_frame *fp;
1142 u8 *mac = NULL;
1143 struct fcoe_softc *fc;
1144 struct fcoe_hdr *hp;
1145
Robert Love4469c192009-02-27 10:56:38 -08001146 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001147
1148 while (!kthread_should_stop()) {
1149
1150 spin_lock_bh(&p->fcoe_rx_list.lock);
1151 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1152 set_current_state(TASK_INTERRUPTIBLE);
1153 spin_unlock_bh(&p->fcoe_rx_list.lock);
1154 schedule();
1155 set_current_state(TASK_RUNNING);
1156 if (kthread_should_stop())
1157 return 0;
1158 spin_lock_bh(&p->fcoe_rx_list.lock);
1159 }
1160 spin_unlock_bh(&p->fcoe_rx_list.lock);
1161 fr = fcoe_dev_from_skb(skb);
1162 lp = fr->fr_dev;
1163 if (unlikely(lp == NULL)) {
1164 FC_DBG("invalid HBA Structure");
1165 kfree_skb(skb);
1166 continue;
1167 }
1168
Robert Love85b4aa42008-12-09 15:10:24 -08001169 if (unlikely(debug_fcoe)) {
1170 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1171 "tail:%p end:%p sum:%d dev:%s",
1172 skb->len, skb->data_len,
1173 skb->head, skb->data, skb_tail_pointer(skb),
1174 skb_end_pointer(skb), skb->csum,
1175 skb->dev ? skb->dev->name : "<NULL>");
1176 }
1177
1178 /*
1179 * Save source MAC address before discarding header.
1180 */
1181 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001182 if (skb_is_nonlinear(skb))
1183 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001184 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001185
1186 /*
1187 * Frame length checks and setting up the header pointers
1188 * was done in fcoe_rcv already.
1189 */
1190 hp = (struct fcoe_hdr *) skb_network_header(skb);
1191 fh = (struct fc_frame_header *) skb_transport_header(skb);
1192
Robert Love582b45b2009-03-31 15:51:50 -07001193 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001194 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001195 if (stats->ErrorFrames < 5)
1196 printk(KERN_WARNING "FCoE version "
1197 "mismatch: The frame has "
1198 "version %x, but the "
1199 "initiator supports version "
1200 "%x\n", FC_FCOE_DECAPS_VER(hp),
1201 FC_FCOE_VER);
1202 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001203 kfree_skb(skb);
1204 continue;
1205 }
1206
1207 skb_pull(skb, sizeof(struct fcoe_hdr));
1208 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1209
Robert Love582b45b2009-03-31 15:51:50 -07001210 stats->RxFrames++;
1211 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001212
1213 fp = (struct fc_frame *)skb;
1214 fc_frame_init(fp);
1215 fr_dev(fp) = lp;
1216 fr_sof(fp) = hp->fcoe_sof;
1217
1218 /* Copy out the CRC and EOF trailer for access */
1219 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1220 kfree_skb(skb);
1221 continue;
1222 }
1223 fr_eof(fp) = crc_eof.fcoe_eof;
1224 fr_crc(fp) = crc_eof.fcoe_crc32;
1225 if (pskb_trim(skb, fr_len)) {
1226 kfree_skb(skb);
1227 continue;
1228 }
1229
1230 /*
1231 * We only check CRC if no offload is available and if it is
1232 * it's solicited data, in which case, the FCP layer would
1233 * check it during the copy.
1234 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001235 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001236 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1237 else
1238 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1239
1240 fh = fc_frame_header_get(fp);
1241 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1242 fh->fh_type == FC_TYPE_FCP) {
1243 fc_exch_recv(lp, lp->emp, fp);
1244 continue;
1245 }
1246 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1247 if (le32_to_cpu(fr_crc(fp)) !=
1248 ~crc32(~0, skb->data, fr_len)) {
1249 if (debug_fcoe || stats->InvalidCRCCount < 5)
1250 printk(KERN_WARNING "fcoe: dropping "
1251 "frame with CRC error\n");
1252 stats->InvalidCRCCount++;
1253 stats->ErrorFrames++;
1254 fc_frame_free(fp);
1255 continue;
1256 }
1257 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1258 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001259 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1260 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1261 fc_frame_free(fp);
1262 continue;
1263 }
Robert Love85b4aa42008-12-09 15:10:24 -08001264 fc_exch_recv(lp, lp->emp, fp);
1265 }
1266 return 0;
1267}
1268
1269/**
Robert Love34f42a02009-02-27 10:55:45 -08001270 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -08001271 * @vp:
1272 *
Vasu Devbc0e17f2009-02-27 10:54:57 -08001273 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -08001274 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1275 * fcoe_hostlist.
1276 *
1277 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001278 */
Robert Love85b4aa42008-12-09 15:10:24 -08001279void fcoe_watchdog(ulong vp)
1280{
Robert Love85b4aa42008-12-09 15:10:24 -08001281 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001282
1283 read_lock(&fcoe_hostlist_lock);
1284 list_for_each_entry(fc, &fcoe_hostlist, list) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001285 if (fc->ctlr.lp)
1286 fcoe_check_wait_queue(fc->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001287 }
1288 read_unlock(&fcoe_hostlist_lock);
1289
1290 fcoe_timer.expires = jiffies + (1 * HZ);
1291 add_timer(&fcoe_timer);
1292}
1293
1294
1295/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001296 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1297 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001298 *
1299 * This empties the wait_queue, dequeue the head of the wait_queue queue
1300 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001301 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001302 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001303 *
1304 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001305 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001306 * by the next skb transmit.
1307 *
1308 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001309 */
Robert Love85b4aa42008-12-09 15:10:24 -08001310static int fcoe_check_wait_queue(struct fc_lport *lp)
1311{
Chris Leech55c8baf2009-02-27 10:56:32 -08001312 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001313 struct sk_buff *skb;
Vasu Devc826a312009-02-27 10:56:27 -08001314 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001315
Robert Love85b4aa42008-12-09 15:10:24 -08001316 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001317 if (fc->fcoe_pending_queue_active)
1318 goto out;
1319 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001320
1321 while (fc->fcoe_pending_queue.qlen) {
1322 /* keep qlen > 0 until fcoe_start_io succeeds */
1323 fc->fcoe_pending_queue.qlen++;
1324 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1325
1326 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1327 rc = fcoe_start_io(skb);
1328 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1329
1330 if (rc) {
1331 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1332 /* undo temporary increment above */
1333 fc->fcoe_pending_queue.qlen--;
1334 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001335 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001336 /* undo temporary increment above */
1337 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001338 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001339
1340 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1341 lp->qfull = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001342 fc->fcoe_pending_queue_active = 0;
1343 rc = fc->fcoe_pending_queue.qlen;
1344out:
Robert Love85b4aa42008-12-09 15:10:24 -08001345 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001346 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001347}
1348
1349/**
Robert Love34f42a02009-02-27 10:55:45 -08001350 * fcoe_dev_setup() - setup link change notification interface
1351 */
1352static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -08001353{
Robert Love85b4aa42008-12-09 15:10:24 -08001354 register_netdevice_notifier(&fcoe_notifier);
1355}
1356
1357/**
Robert Love34f42a02009-02-27 10:55:45 -08001358 * fcoe_dev_setup() - cleanup link change notification interface
1359 */
Robert Love85b4aa42008-12-09 15:10:24 -08001360static void fcoe_dev_cleanup(void)
1361{
1362 unregister_netdevice_notifier(&fcoe_notifier);
1363}
1364
1365/**
Robert Love34f42a02009-02-27 10:55:45 -08001366 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001367 * @notifier: context of the notification
1368 * @event: type of event
1369 * @ptr: fixed array for output parsed ifname
1370 *
1371 * This function is called by the ethernet driver in case of link change event
1372 *
1373 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001374 */
Robert Love85b4aa42008-12-09 15:10:24 -08001375static int fcoe_device_notification(struct notifier_block *notifier,
1376 ulong event, void *ptr)
1377{
1378 struct fc_lport *lp = NULL;
1379 struct net_device *real_dev = ptr;
1380 struct fcoe_softc *fc;
1381 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001382 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001383 u32 mfs;
1384 int rc = NOTIFY_OK;
1385
1386 read_lock(&fcoe_hostlist_lock);
1387 list_for_each_entry(fc, &fcoe_hostlist, list) {
1388 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001389 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001390 break;
1391 }
1392 }
1393 read_unlock(&fcoe_hostlist_lock);
1394 if (lp == NULL) {
1395 rc = NOTIFY_DONE;
1396 goto out;
1397 }
1398
Robert Love85b4aa42008-12-09 15:10:24 -08001399 switch (event) {
1400 case NETDEV_DOWN:
1401 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001402 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001403 break;
1404 case NETDEV_UP:
1405 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001406 break;
1407 case NETDEV_CHANGEMTU:
1408 mfs = fc->real_dev->mtu -
1409 (sizeof(struct fcoe_hdr) +
1410 sizeof(struct fcoe_crc_eof));
1411 if (mfs >= FC_MIN_MAX_FRAME)
1412 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001413 break;
1414 case NETDEV_REGISTER:
1415 break;
1416 default:
Joe Eykholt97c83892009-03-17 11:42:40 -07001417 FC_DBG("Unknown event %ld from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001418 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001419 if (link_possible && !fcoe_link_ok(lp))
1420 fcoe_ctlr_link_up(&fc->ctlr);
1421 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1422 stats = fc_lport_get_stats(lp);
1423 stats->LinkFailureCount++;
1424 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001425 }
1426out:
1427 return rc;
1428}
1429
1430/**
Robert Love34f42a02009-02-27 10:55:45 -08001431 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001432 * @buffer: incoming buffer to be copied
1433 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001434 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001435 */
Robert Love85b4aa42008-12-09 15:10:24 -08001436static struct net_device *fcoe_if_to_netdev(const char *buffer)
1437{
1438 char *cp;
1439 char ifname[IFNAMSIZ + 2];
1440
1441 if (buffer) {
1442 strlcpy(ifname, buffer, IFNAMSIZ);
1443 cp = ifname + strlen(ifname);
1444 while (--cp >= ifname && *cp == '\n')
1445 *cp = '\0';
1446 return dev_get_by_name(&init_net, ifname);
1447 }
1448 return NULL;
1449}
1450
1451/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001452 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001453 * @netdev: the target netdev
1454 *
1455 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001456 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001457static struct module *
1458fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001459{
1460 struct device *dev;
1461
1462 if (!netdev)
1463 return NULL;
1464
1465 dev = netdev->dev.parent;
1466 if (!dev)
1467 return NULL;
1468
1469 if (!dev->driver)
1470 return NULL;
1471
1472 return dev->driver->owner;
1473}
1474
1475/**
Robert Love34f42a02009-02-27 10:55:45 -08001476 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001477 * @netdev: the target netdev
1478 *
Robert Love34f42a02009-02-27 10:55:45 -08001479 * Holds the Ethernet driver module by try_module_get() for
1480 * the corresponding netdev.
1481 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001482 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001483 */
Robert Love85b4aa42008-12-09 15:10:24 -08001484static int fcoe_ethdrv_get(const struct net_device *netdev)
1485{
1486 struct module *owner;
1487
1488 owner = fcoe_netdev_to_module_owner(netdev);
1489 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001490 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1491 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001492 return try_module_get(owner);
1493 }
1494 return -ENODEV;
1495}
1496
1497/**
Robert Love34f42a02009-02-27 10:55:45 -08001498 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001499 * @netdev: the target netdev
1500 *
Robert Love34f42a02009-02-27 10:55:45 -08001501 * Releases the Ethernet driver module by module_put for
1502 * the corresponding netdev.
1503 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001504 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001505 */
Robert Love85b4aa42008-12-09 15:10:24 -08001506static int fcoe_ethdrv_put(const struct net_device *netdev)
1507{
1508 struct module *owner;
1509
1510 owner = fcoe_netdev_to_module_owner(netdev);
1511 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001512 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1513 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001514 module_put(owner);
1515 return 0;
1516 }
1517 return -ENODEV;
1518}
1519
1520/**
Robert Love34f42a02009-02-27 10:55:45 -08001521 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001522 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001523 * @kp: associated kernel param
1524 *
1525 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001526 */
Robert Love85b4aa42008-12-09 15:10:24 -08001527static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1528{
1529 int rc;
1530 struct net_device *netdev;
1531
1532 netdev = fcoe_if_to_netdev(buffer);
1533 if (!netdev) {
1534 rc = -ENODEV;
1535 goto out_nodev;
1536 }
1537 /* look for existing lport */
1538 if (!fcoe_hostlist_lookup(netdev)) {
1539 rc = -ENODEV;
1540 goto out_putdev;
1541 }
Vasu Dev7f349142009-03-27 09:06:31 -07001542 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001543 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001544 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001545 netdev->name);
1546 rc = -EIO;
1547 goto out_putdev;
1548 }
1549 fcoe_ethdrv_put(netdev);
1550 rc = 0;
1551out_putdev:
1552 dev_put(netdev);
1553out_nodev:
1554 return rc;
1555}
1556
1557/**
Robert Love34f42a02009-02-27 10:55:45 -08001558 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001559 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001560 * @kp: associated kernel param
1561 *
1562 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001563 */
Robert Love85b4aa42008-12-09 15:10:24 -08001564static int fcoe_create(const char *buffer, struct kernel_param *kp)
1565{
1566 int rc;
1567 struct net_device *netdev;
1568
1569 netdev = fcoe_if_to_netdev(buffer);
1570 if (!netdev) {
1571 rc = -ENODEV;
1572 goto out_nodev;
1573 }
1574 /* look for existing lport */
1575 if (fcoe_hostlist_lookup(netdev)) {
1576 rc = -EEXIST;
1577 goto out_putdev;
1578 }
1579 fcoe_ethdrv_get(netdev);
1580
Vasu Dev7f349142009-03-27 09:06:31 -07001581 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001582 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001583 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001584 netdev->name);
1585 fcoe_ethdrv_put(netdev);
1586 rc = -EIO;
1587 goto out_putdev;
1588 }
1589 rc = 0;
1590out_putdev:
1591 dev_put(netdev);
1592out_nodev:
1593 return rc;
1594}
1595
1596module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1597__MODULE_PARM_TYPE(create, "string");
1598MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1599module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1600__MODULE_PARM_TYPE(destroy, "string");
1601MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1602
Robert Love34f42a02009-02-27 10:55:45 -08001603/**
1604 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001605 * @lp: ptr to the fc_lport
1606 *
1607 * Any permanently-disqualifying conditions have been previously checked.
1608 * This also updates the speed setting, which may change with link for 100/1000.
1609 *
1610 * This function should probably be checking for PAUSE support at some point
1611 * in the future. Currently Per-priority-pause is not determinable using
1612 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1613 *
1614 * Returns: 0 if link is OK for use by FCoE.
1615 *
1616 */
1617int fcoe_link_ok(struct fc_lport *lp)
1618{
Robert Lovefc47ff62009-02-27 10:55:55 -08001619 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001620 struct net_device *dev = fc->real_dev;
1621 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1622 int rc = 0;
1623
1624 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1625 dev = fc->phys_dev;
1626 if (dev->ethtool_ops->get_settings) {
1627 dev->ethtool_ops->get_settings(dev, &ecmd);
1628 lp->link_supported_speeds &=
1629 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1630 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1631 SUPPORTED_1000baseT_Full))
1632 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1633 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1634 lp->link_supported_speeds |=
1635 FC_PORTSPEED_10GBIT;
1636 if (ecmd.speed == SPEED_1000)
1637 lp->link_speed = FC_PORTSPEED_1GBIT;
1638 if (ecmd.speed == SPEED_10000)
1639 lp->link_speed = FC_PORTSPEED_10GBIT;
1640 }
1641 } else
1642 rc = -1;
1643
1644 return rc;
1645}
Robert Love85b4aa42008-12-09 15:10:24 -08001646
Robert Love34f42a02009-02-27 10:55:45 -08001647/**
1648 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001649 * @lp: the fc_lport
1650 */
1651void fcoe_percpu_clean(struct fc_lport *lp)
1652{
Robert Love85b4aa42008-12-09 15:10:24 -08001653 struct fcoe_percpu_s *pp;
1654 struct fcoe_rcv_info *fr;
1655 struct sk_buff_head *list;
1656 struct sk_buff *skb, *next;
1657 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001658 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001659
Robert Love5e5e92d2009-03-17 11:41:35 -07001660 for_each_possible_cpu(cpu) {
1661 pp = &per_cpu(fcoe_percpu, cpu);
1662 spin_lock_bh(&pp->fcoe_rx_list.lock);
1663 list = &pp->fcoe_rx_list;
1664 head = list->next;
1665 for (skb = head; skb != (struct sk_buff *)list;
1666 skb = next) {
1667 next = skb->next;
1668 fr = fcoe_dev_from_skb(skb);
1669 if (fr->fr_dev == lp) {
1670 __skb_unlink(skb, list);
1671 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001672 }
Robert Love85b4aa42008-12-09 15:10:24 -08001673 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001674 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001675 }
1676}
Robert Love85b4aa42008-12-09 15:10:24 -08001677
1678/**
Robert Love34f42a02009-02-27 10:55:45 -08001679 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001680 * @lp: the corresponding fc_lport
1681 *
1682 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001683 */
Robert Love85b4aa42008-12-09 15:10:24 -08001684void fcoe_clean_pending_queue(struct fc_lport *lp)
1685{
1686 struct fcoe_softc *fc = lport_priv(lp);
1687 struct sk_buff *skb;
1688
1689 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1690 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1691 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1692 kfree_skb(skb);
1693 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1694 }
1695 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1696}
Robert Love85b4aa42008-12-09 15:10:24 -08001697
1698/**
Robert Love34f42a02009-02-27 10:55:45 -08001699 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001700 * @shost: shost the reset is from
1701 *
1702 * Returns: always 0
1703 */
1704int fcoe_reset(struct Scsi_Host *shost)
1705{
1706 struct fc_lport *lport = shost_priv(shost);
1707 fc_lport_reset(lport);
1708 return 0;
1709}
Robert Love85b4aa42008-12-09 15:10:24 -08001710
Robert Love34f42a02009-02-27 10:55:45 -08001711/**
Robert Love34f42a02009-02-27 10:55:45 -08001712 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001713 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001714 *
1715 * Returns: NULL or the located fcoe_softc
1716 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001717static struct fcoe_softc *
1718fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001719{
1720 struct fcoe_softc *fc;
1721
1722 read_lock(&fcoe_hostlist_lock);
1723 list_for_each_entry(fc, &fcoe_hostlist, list) {
1724 if (fc->real_dev == dev) {
1725 read_unlock(&fcoe_hostlist_lock);
1726 return fc;
1727 }
1728 }
1729 read_unlock(&fcoe_hostlist_lock);
1730 return NULL;
1731}
1732
Robert Love34f42a02009-02-27 10:55:45 -08001733/**
1734 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001735 * @netdev: ptr to net_device
1736 *
1737 * Returns: 0 for success
1738 */
1739struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1740{
1741 struct fcoe_softc *fc;
1742
1743 fc = fcoe_hostlist_lookup_softc(netdev);
1744
Joe Eykholt97c83892009-03-17 11:42:40 -07001745 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001746}
Robert Love85b4aa42008-12-09 15:10:24 -08001747
Robert Love34f42a02009-02-27 10:55:45 -08001748/**
1749 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001750 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001751 *
1752 * Returns: 0 for success
1753 */
1754int fcoe_hostlist_add(const struct fc_lport *lp)
1755{
1756 struct fcoe_softc *fc;
1757
1758 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1759 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001760 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001761 write_lock_bh(&fcoe_hostlist_lock);
1762 list_add_tail(&fc->list, &fcoe_hostlist);
1763 write_unlock_bh(&fcoe_hostlist_lock);
1764 }
1765 return 0;
1766}
Robert Love85b4aa42008-12-09 15:10:24 -08001767
Robert Love34f42a02009-02-27 10:55:45 -08001768/**
1769 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001770 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001771 *
1772 * Returns: 0 for success
1773 */
1774int fcoe_hostlist_remove(const struct fc_lport *lp)
1775{
1776 struct fcoe_softc *fc;
1777
1778 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1779 BUG_ON(!fc);
1780 write_lock_bh(&fcoe_hostlist_lock);
1781 list_del(&fc->list);
1782 write_unlock_bh(&fcoe_hostlist_lock);
1783
1784 return 0;
1785}
Robert Love85b4aa42008-12-09 15:10:24 -08001786
1787/**
Robert Love34f42a02009-02-27 10:55:45 -08001788 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001789 *
Robert Love85b4aa42008-12-09 15:10:24 -08001790 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001791 */
Robert Love85b4aa42008-12-09 15:10:24 -08001792static int __init fcoe_init(void)
1793{
Robert Love38eccab2009-03-17 11:41:30 -07001794 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001795 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001796 struct fcoe_percpu_s *p;
1797
Robert Love85b4aa42008-12-09 15:10:24 -08001798 INIT_LIST_HEAD(&fcoe_hostlist);
1799 rwlock_init(&fcoe_hostlist_lock);
1800
Robert Love38eccab2009-03-17 11:41:30 -07001801 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001802 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001803 skb_queue_head_init(&p->fcoe_rx_list);
1804 }
1805
Robert Love8976f422009-03-17 11:41:46 -07001806 for_each_online_cpu(cpu)
1807 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001808
Robert Love8976f422009-03-17 11:41:46 -07001809 /* Initialize per CPU interrupt thread */
1810 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1811 if (rc)
1812 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001813
Robert Love8976f422009-03-17 11:41:46 -07001814 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001815 fcoe_dev_setup();
1816
Robert Lovea468f322009-02-27 10:56:00 -08001817 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1818
1819 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001820
Vasu Dev7f349142009-03-27 09:06:31 -07001821 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001822
1823 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001824
1825out_free:
1826 for_each_online_cpu(cpu) {
1827 fcoe_percpu_thread_destroy(cpu);
1828 }
1829
1830 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001831}
1832module_init(fcoe_init);
1833
1834/**
Robert Love34f42a02009-02-27 10:55:45 -08001835 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001836 *
1837 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001838 */
Robert Love85b4aa42008-12-09 15:10:24 -08001839static void __exit fcoe_exit(void)
1840{
Robert Love5e5e92d2009-03-17 11:41:35 -07001841 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001842 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001843
Robert Love85b4aa42008-12-09 15:10:24 -08001844 fcoe_dev_cleanup();
1845
Robert Love582b45b2009-03-31 15:51:50 -07001846 /* Stop the timer */
Robert Love85b4aa42008-12-09 15:10:24 -08001847 del_timer_sync(&fcoe_timer);
1848
Vasu Dev5919a592009-03-27 09:03:29 -07001849 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001850 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001851 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001852
Robert Love8976f422009-03-17 11:41:46 -07001853 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1854
1855 for_each_online_cpu(cpu) {
1856 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001857 }
1858
Vasu Dev7f349142009-03-27 09:06:31 -07001859 /* detach from scsi transport */
1860 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001861}
1862module_exit(fcoe_exit);