blob: 04f500571d49e99d19283e9481afd787d055a9cb [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
Robert Love85b4aa42008-12-09 15:10:24 -080060/* Function Prototyes */
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/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000139 * fcoe_fip_recv - handle a received FIP frame.
140 * @skb: the receive skb
141 * @dev: associated &net_device
142 * @ptype: the &packet_type structure which was used to register this handler.
143 * @orig_dev: original receive &net_device, in case @dev is a bond.
144 *
145 * Returns: 0 for success
146 */
147static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
148 struct packet_type *ptype,
149 struct net_device *orig_dev)
150{
151 struct fcoe_softc *fc;
152
153 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
154 fcoe_ctlr_recv(&fc->ctlr, skb);
155 return 0;
156}
157
158/**
159 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
160 * @fip: FCoE controller.
161 * @skb: FIP Packet.
162 */
163static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
164{
165 skb->dev = fcoe_from_ctlr(fip)->real_dev;
166 dev_queue_xmit(skb);
167}
168
169/**
170 * fcoe_update_src_mac() - Update Ethernet MAC filters.
171 * @fip: FCoE controller.
172 * @old: Unicast MAC address to delete if the MAC is non-zero.
173 * @new: Unicast MAC address to add.
174 *
175 * Remove any previously-set unicast MAC filter.
176 * Add secondary FCoE MAC address filter for our OUI.
177 */
178static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
179{
180 struct fcoe_softc *fc;
181
182 fc = fcoe_from_ctlr(fip);
183 rtnl_lock();
184 if (!is_zero_ether_addr(old))
185 dev_unicast_delete(fc->real_dev, old, ETH_ALEN);
186 dev_unicast_add(fc->real_dev, new, ETH_ALEN);
187 rtnl_unlock();
188}
189
190/**
Vasu Dev7f349142009-03-27 09:06:31 -0700191 * fcoe_lport_config() - sets up the fc_lport
192 * @lp: ptr to the fc_lport
193 * @shost: ptr to the parent scsi host
194 *
195 * Returns: 0 for success
196 */
197static int fcoe_lport_config(struct fc_lport *lp)
198{
199 lp->link_up = 0;
200 lp->qfull = 0;
201 lp->max_retry_count = 3;
202 lp->e_d_tov = 2 * 1000; /* FC-FS default */
203 lp->r_a_tov = 2 * 2 * 1000;
204 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
205 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
206
207 fc_lport_init_stats(lp);
208
209 /* lport fc_lport related configuration */
210 fc_lport_config(lp);
211
212 /* offload related configuration */
213 lp->crc_offload = 0;
214 lp->seq_offload = 0;
215 lp->lro_enabled = 0;
216 lp->lro_xid = 0;
217 lp->lso_max = 0;
218
219 return 0;
220}
221
222/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000223 * fcoe_netdev_cleanup() - clean up netdev configurations
224 * @fc: ptr to the fcoe_softc
225 */
226void fcoe_netdev_cleanup(struct fcoe_softc *fc)
227{
228 u8 flogi_maddr[ETH_ALEN];
229
230 /* Don't listen for Ethernet packets anymore */
231 dev_remove_pack(&fc->fcoe_packet_type);
232 dev_remove_pack(&fc->fip_packet_type);
233
234 /* Delete secondary MAC addresses */
235 rtnl_lock();
236 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
237 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
238 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
239 dev_unicast_delete(fc->real_dev,
240 fc->ctlr.data_src_addr, ETH_ALEN);
241 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
242 rtnl_unlock();
243}
244
245/**
Vasu Dev7f349142009-03-27 09:06:31 -0700246 * fcoe_netdev_config() - Set up netdev for SW FCoE
247 * @lp : ptr to the fc_lport
248 * @netdev : ptr to the associated netdevice struct
249 *
250 * Must be called after fcoe_lport_config() as it will use lport mutex
251 *
252 * Returns : 0 for success
253 */
254static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
255{
256 u32 mfs;
257 u64 wwnn, wwpn;
258 struct fcoe_softc *fc;
259 u8 flogi_maddr[ETH_ALEN];
260
261 /* Setup lport private data to point to fcoe softc */
262 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700263 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700264 fc->real_dev = netdev;
265 fc->phys_dev = netdev;
266
267 /* Require support for get_pauseparam ethtool op. */
268 if (netdev->priv_flags & IFF_802_1Q_VLAN)
269 fc->phys_dev = vlan_dev_real_dev(netdev);
270
271 /* Do not support for bonding device */
272 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
273 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
274 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
275 return -EOPNOTSUPP;
276 }
277
278 /*
279 * Determine max frame size based on underlying device and optional
280 * user-configured limit. If the MFS is too low, fcoe_link_ok()
281 * will return 0, so do this first.
282 */
283 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
284 sizeof(struct fcoe_crc_eof));
285 if (fc_set_mfs(lp, mfs))
286 return -EINVAL;
287
Vasu Dev7f349142009-03-27 09:06:31 -0700288 /* offload features support */
289 if (fc->real_dev->features & NETIF_F_SG)
290 lp->sg_supp = 1;
291
292#ifdef NETIF_F_FCOE_CRC
293 if (netdev->features & NETIF_F_FCOE_CRC) {
294 lp->crc_offload = 1;
295 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
296 netdev->name);
297 }
298#endif
299#ifdef NETIF_F_FSO
300 if (netdev->features & NETIF_F_FSO) {
301 lp->seq_offload = 1;
302 lp->lso_max = netdev->gso_max_size;
303 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
304 netdev->name, lp->lso_max);
305 }
306#endif
307 if (netdev->fcoe_ddp_xid) {
308 lp->lro_enabled = 1;
309 lp->lro_xid = netdev->fcoe_ddp_xid;
310 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
311 netdev->name, lp->lro_xid);
312 }
313 skb_queue_head_init(&fc->fcoe_pending_queue);
314 fc->fcoe_pending_queue_active = 0;
315
316 /* setup Source Mac Address */
Joe Eykholt97c83892009-03-17 11:42:40 -0700317 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
Vasu Dev7f349142009-03-27 09:06:31 -0700318 fc->real_dev->addr_len);
319
320 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
321 fc_set_wwnn(lp, wwnn);
322 /* XXX - 3rd arg needs to be vlan id */
323 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
324 fc_set_wwpn(lp, wwpn);
325
326 /*
327 * Add FCoE MAC address as second unicast MAC address
328 * or enter promiscuous mode if not capable of listening
329 * for multiple unicast MACs.
330 */
331 rtnl_lock();
332 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
333 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
334 rtnl_unlock();
335
336 /*
337 * setup the receive function from ethernet driver
338 * on the ethertype for the given device
339 */
340 fc->fcoe_packet_type.func = fcoe_rcv;
341 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
342 fc->fcoe_packet_type.dev = fc->real_dev;
343 dev_add_pack(&fc->fcoe_packet_type);
344
Vasu Devab6b85c2009-05-17 12:33:08 +0000345 fc->fip_packet_type.func = fcoe_fip_recv;
346 fc->fip_packet_type.type = htons(ETH_P_FIP);
347 fc->fip_packet_type.dev = fc->real_dev;
348 dev_add_pack(&fc->fip_packet_type);
349
Vasu Dev7f349142009-03-27 09:06:31 -0700350 return 0;
351}
352
353/**
354 * fcoe_shost_config() - Sets up fc_lport->host
355 * @lp : ptr to the fc_lport
356 * @shost : ptr to the associated scsi host
357 * @dev : device associated to scsi host
358 *
359 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
360 *
361 * Returns : 0 for success
362 */
363static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
364 struct device *dev)
365{
366 int rc = 0;
367
368 /* lport scsi host config */
369 lp->host = shost;
370
371 lp->host->max_lun = FCOE_MAX_LUN;
372 lp->host->max_id = FCOE_MAX_FCP_TARGET;
373 lp->host->max_channel = 0;
374 lp->host->transportt = scsi_transport_fcoe_sw;
375
376 /* add the new host to the SCSI-ml */
377 rc = scsi_add_host(lp->host, dev);
378 if (rc) {
379 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
380 return rc;
381 }
382 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
383 FCOE_NAME, FCOE_VERSION,
384 fcoe_netdev(lp)->name);
385
386 return 0;
387}
388
389/**
390 * fcoe_em_config() - allocates em for this lport
391 * @lp: the port that em is to allocated for
392 *
393 * Returns : 0 on success
394 */
395static inline int fcoe_em_config(struct fc_lport *lp)
396{
397 BUG_ON(lp->emp);
398
399 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
400 FCOE_MIN_XID, FCOE_MAX_XID);
401 if (!lp->emp)
402 return -ENOMEM;
403
404 return 0;
405}
406
407/**
408 * fcoe_if_destroy() - FCoE software HBA tear-down function
409 * @netdev: ptr to the associated net_device
410 *
411 * Returns: 0 if link is OK for use by FCoE.
412 */
413static int fcoe_if_destroy(struct net_device *netdev)
414{
415 struct fc_lport *lp = NULL;
416 struct fcoe_softc *fc;
Vasu Dev7f349142009-03-27 09:06:31 -0700417
418 BUG_ON(!netdev);
419
420 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
421 netdev->name);
422
423 lp = fcoe_hostlist_lookup(netdev);
424 if (!lp)
425 return -ENODEV;
426
427 fc = lport_priv(lp);
428
429 /* Logout of the fabric */
430 fc_fabric_logoff(lp);
431
432 /* Remove the instance from fcoe's list */
433 fcoe_hostlist_remove(lp);
434
Vasu Devab6b85c2009-05-17 12:33:08 +0000435 /* clean up netdev configurations */
436 fcoe_netdev_cleanup(fc);
437
438 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700439 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700440
441 /* Cleanup the fc_lport */
442 fc_lport_destroy(lp);
443 fc_fcp_destroy(lp);
444
445 /* Detach from the scsi-ml */
446 fc_remove_host(lp->host);
447 scsi_remove_host(lp->host);
448
449 /* There are no more rports or I/O, free the EM */
450 if (lp->emp)
451 fc_exch_mgr_free(lp->emp);
452
Vasu Dev7f349142009-03-27 09:06:31 -0700453 /* Free the per-CPU revieve threads */
454 fcoe_percpu_clean(lp);
455
456 /* Free existing skbs */
457 fcoe_clean_pending_queue(lp);
458
459 /* Free memory used by statistical counters */
460 fc_lport_free_stats(lp);
461
462 /* Release the net_device and Scsi_Host */
463 dev_put(fc->real_dev);
464 scsi_host_put(lp->host);
465
466 return 0;
467}
468
469/*
470 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
471 * @lp: the corresponding fc_lport
472 * @xid: the exchange id for this ddp transfer
473 * @sgl: the scatterlist describing this transfer
474 * @sgc: number of sg items
475 *
476 * Returns : 0 no ddp
477 */
478static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
479 struct scatterlist *sgl, unsigned int sgc)
480{
481 struct net_device *n = fcoe_netdev(lp);
482
483 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
484 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
485
486 return 0;
487}
488
489/*
490 * fcoe_ddp_done - calls LLD's ddp_done through net_device
491 * @lp: the corresponding fc_lport
492 * @xid: the exchange id for this ddp transfer
493 *
494 * Returns : the length of data that have been completed by ddp
495 */
496static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
497{
498 struct net_device *n = fcoe_netdev(lp);
499
500 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
501 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
502 return 0;
503}
504
505static struct libfc_function_template fcoe_libfc_fcn_templ = {
506 .frame_send = fcoe_xmit,
507 .ddp_setup = fcoe_ddp_setup,
508 .ddp_done = fcoe_ddp_done,
509};
510
511/**
512 * fcoe_if_create() - this function creates the fcoe interface
513 * @netdev: pointer the associated netdevice
514 *
515 * Creates fc_lport struct and scsi_host for lport, configures lport
516 * and starts fabric login.
517 *
518 * Returns : 0 on success
519 */
520static int fcoe_if_create(struct net_device *netdev)
521{
522 int rc;
523 struct fc_lport *lp = NULL;
524 struct fcoe_softc *fc;
525 struct Scsi_Host *shost;
526
527 BUG_ON(!netdev);
528
529 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
530 netdev->name);
531
532 lp = fcoe_hostlist_lookup(netdev);
533 if (lp)
534 return -EEXIST;
535
Vasu Deva0a25da2009-03-17 11:42:29 -0700536 shost = libfc_host_alloc(&fcoe_shost_template,
537 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700538 if (!shost) {
539 FC_DBG("Could not allocate host structure\n");
540 return -ENOMEM;
541 }
542 lp = shost_priv(shost);
543 fc = lport_priv(lp);
544
545 /* configure fc_lport, e.g., em */
546 rc = fcoe_lport_config(lp);
547 if (rc) {
548 FC_DBG("Could not configure lport\n");
549 goto out_host_put;
550 }
551
Joe Eykholt97c83892009-03-17 11:42:40 -0700552 /*
553 * Initialize FIP.
554 */
555 fcoe_ctlr_init(&fc->ctlr);
556 fc->ctlr.send = fcoe_fip_send;
557 fc->ctlr.update_mac = fcoe_update_src_mac;
558
Vasu Devab6b85c2009-05-17 12:33:08 +0000559 /* configure lport network properties */
560 rc = fcoe_netdev_config(lp, netdev);
561 if (rc) {
562 FC_DBG("Could not configure netdev for the interface\n");
563 goto out_netdev_cleanup;
564 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700565
Vasu Dev7f349142009-03-27 09:06:31 -0700566 /* configure lport scsi host properties */
567 rc = fcoe_shost_config(lp, shost, &netdev->dev);
568 if (rc) {
569 FC_DBG("Could not configure shost for lport\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000570 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700571 }
572
573 /* lport exch manager allocation */
574 rc = fcoe_em_config(lp);
575 if (rc) {
576 FC_DBG("Could not configure em for lport\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000577 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700578 }
579
580 /* Initialize the library */
581 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
582 if (rc) {
583 FC_DBG("Could not configure libfc for lport!\n");
584 goto out_lp_destroy;
585 }
586
587 /* add to lports list */
588 fcoe_hostlist_add(lp);
589
590 lp->boot_time = jiffies;
591
592 fc_fabric_login(lp);
593
Joe Eykholt97c83892009-03-17 11:42:40 -0700594 if (!fcoe_link_ok(lp))
595 fcoe_ctlr_link_up(&fc->ctlr);
596
Vasu Dev7f349142009-03-27 09:06:31 -0700597 dev_hold(netdev);
598
599 return rc;
600
601out_lp_destroy:
602 fc_exch_mgr_free(lp->emp); /* Free the EM */
Vasu Devab6b85c2009-05-17 12:33:08 +0000603out_netdev_cleanup:
604 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700605out_host_put:
606 scsi_host_put(lp->host);
607 return rc;
608}
609
610/**
611 * fcoe_if_init() - attach to scsi transport
612 *
613 * Returns : 0 on success
614 */
615static int __init fcoe_if_init(void)
616{
617 /* attach to scsi transport */
618 scsi_transport_fcoe_sw =
619 fc_attach_transport(&fcoe_transport_function);
620
621 if (!scsi_transport_fcoe_sw) {
622 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
623 return -ENODEV;
624 }
625
626 return 0;
627}
628
629/**
630 * fcoe_if_exit() - detach from scsi transport
631 *
632 * Returns : 0 on success
633 */
634int __exit fcoe_if_exit(void)
635{
636 fc_release_transport(scsi_transport_fcoe_sw);
637 return 0;
638}
639
Robert Love85b4aa42008-12-09 15:10:24 -0800640/**
Robert Love8976f422009-03-17 11:41:46 -0700641 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
642 * @cpu: cpu index for the online cpu
643 */
644static void fcoe_percpu_thread_create(unsigned int cpu)
645{
646 struct fcoe_percpu_s *p;
647 struct task_struct *thread;
648
649 p = &per_cpu(fcoe_percpu, cpu);
650
651 thread = kthread_create(fcoe_percpu_receive_thread,
652 (void *)p, "fcoethread/%d", cpu);
653
654 if (likely(!IS_ERR(p->thread))) {
655 kthread_bind(thread, cpu);
656 wake_up_process(thread);
657
658 spin_lock_bh(&p->fcoe_rx_list.lock);
659 p->thread = thread;
660 spin_unlock_bh(&p->fcoe_rx_list.lock);
661 }
662}
663
664/**
665 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
666 * @cpu: cpu index the rx thread is to be removed
667 *
668 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
669 * current CPU's Rx thread. If the thread being destroyed is bound to
670 * the CPU processing this context the skbs will be freed.
671 */
672static void fcoe_percpu_thread_destroy(unsigned int cpu)
673{
674 struct fcoe_percpu_s *p;
675 struct task_struct *thread;
676 struct page *crc_eof;
677 struct sk_buff *skb;
678#ifdef CONFIG_SMP
679 struct fcoe_percpu_s *p0;
680 unsigned targ_cpu = smp_processor_id();
681#endif /* CONFIG_SMP */
682
683 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
684
685 /* Prevent any new skbs from being queued for this CPU. */
686 p = &per_cpu(fcoe_percpu, cpu);
687 spin_lock_bh(&p->fcoe_rx_list.lock);
688 thread = p->thread;
689 p->thread = NULL;
690 crc_eof = p->crc_eof_page;
691 p->crc_eof_page = NULL;
692 p->crc_eof_offset = 0;
693 spin_unlock_bh(&p->fcoe_rx_list.lock);
694
695#ifdef CONFIG_SMP
696 /*
697 * Don't bother moving the skb's if this context is running
698 * on the same CPU that is having its thread destroyed. This
699 * can easily happen when the module is removed.
700 */
701 if (cpu != targ_cpu) {
702 p0 = &per_cpu(fcoe_percpu, targ_cpu);
703 spin_lock_bh(&p0->fcoe_rx_list.lock);
704 if (p0->thread) {
705 FC_DBG("Moving frames from CPU %d to CPU %d\n",
706 cpu, targ_cpu);
707
708 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
709 __skb_queue_tail(&p0->fcoe_rx_list, skb);
710 spin_unlock_bh(&p0->fcoe_rx_list.lock);
711 } else {
712 /*
713 * The targeted CPU is not initialized and cannot accept
714 * new skbs. Unlock the targeted CPU and drop the skbs
715 * on the CPU that is going offline.
716 */
717 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
718 kfree_skb(skb);
719 spin_unlock_bh(&p0->fcoe_rx_list.lock);
720 }
721 } else {
722 /*
723 * This scenario occurs when the module is being removed
724 * and all threads are being destroyed. skbs will continue
725 * to be shifted from the CPU thread that is being removed
726 * to the CPU thread associated with the CPU that is processing
727 * the module removal. Once there is only one CPU Rx thread it
728 * will reach this case and we will drop all skbs and later
729 * stop the thread.
730 */
731 spin_lock_bh(&p->fcoe_rx_list.lock);
732 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
733 kfree_skb(skb);
734 spin_unlock_bh(&p->fcoe_rx_list.lock);
735 }
736#else
737 /*
738 * This a non-SMP scenario where the singluar Rx thread is
739 * being removed. Free all skbs and stop the thread.
740 */
741 spin_lock_bh(&p->fcoe_rx_list.lock);
742 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
743 kfree_skb(skb);
744 spin_unlock_bh(&p->fcoe_rx_list.lock);
745#endif
746
747 if (thread)
748 kthread_stop(thread);
749
750 if (crc_eof)
751 put_page(crc_eof);
752}
753
754/**
755 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
756 * @nfb: callback data block
757 * @action: event triggering the callback
758 * @hcpu: index for the cpu of this event
759 *
760 * This creates or destroys per cpu data for fcoe
761 *
762 * Returns NOTIFY_OK always.
763 */
764static int fcoe_cpu_callback(struct notifier_block *nfb,
765 unsigned long action, void *hcpu)
766{
767 unsigned cpu = (unsigned long)hcpu;
768
769 switch (action) {
770 case CPU_ONLINE:
771 case CPU_ONLINE_FROZEN:
772 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
773 fcoe_percpu_thread_create(cpu);
774 break;
775 case CPU_DEAD:
776 case CPU_DEAD_FROZEN:
777 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
778 fcoe_percpu_thread_destroy(cpu);
779 break;
780 default:
781 break;
782 }
783 return NOTIFY_OK;
784}
785
786static struct notifier_block fcoe_cpu_notifier = {
787 .notifier_call = fcoe_cpu_callback,
788};
789
790/**
Robert Love34f42a02009-02-27 10:55:45 -0800791 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800792 * @skb: the receive skb
793 * @dev: associated net device
794 * @ptype: context
795 * @odldev: last device
796 *
797 * this function will receive the packet and build fc frame and pass it up
798 *
799 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800800 */
Robert Love85b4aa42008-12-09 15:10:24 -0800801int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
802 struct packet_type *ptype, struct net_device *olddev)
803{
804 struct fc_lport *lp;
805 struct fcoe_rcv_info *fr;
806 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800807 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800808 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700809 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700810 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800811
812 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700813 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800814 if (unlikely(lp == NULL)) {
815 FC_DBG("cannot find hba structure");
816 goto err2;
817 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700818 if (!lp->link_up)
819 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800820
821 if (unlikely(debug_fcoe)) {
822 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
823 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
824 skb->head, skb->data, skb_tail_pointer(skb),
825 skb_end_pointer(skb), skb->csum,
826 skb->dev ? skb->dev->name : "<NULL>");
827
828 }
829
830 /* check for FCOE packet type */
831 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
832 FC_DBG("wrong FC type frame");
833 goto err;
834 }
835
836 /*
837 * Check for minimum frame length, and make sure required FCoE
838 * and FC headers are pulled into the linear data area.
839 */
840 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
841 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
842 goto err;
843
844 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
845 fh = (struct fc_frame_header *) skb_transport_header(skb);
846
847 oxid = ntohs(fh->fh_ox_id);
848
849 fr = fcoe_dev_from_skb(skb);
850 fr->fr_dev = lp;
851 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700852
Robert Love85b4aa42008-12-09 15:10:24 -0800853#ifdef CONFIG_SMP
854 /*
855 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700856 * cpu bits to get cpu and then this cpu is used for selecting
857 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800858 */
Robert Love8976f422009-03-17 11:41:46 -0700859 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800860#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700861
Robert Love8976f422009-03-17 11:41:46 -0700862 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800863 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700864 if (unlikely(!fps->thread)) {
865 /*
866 * The targeted CPU is not ready, let's target
867 * the first CPU now. For non-SMP systems this
868 * will check the same CPU twice.
869 */
870 FC_DBG("CPU is online, but no receive thread ready "
871 "for incoming skb- using first online CPU.\n");
872
873 spin_unlock_bh(&fps->fcoe_rx_list.lock);
874 cpu = first_cpu(cpu_online_map);
875 fps = &per_cpu(fcoe_percpu, cpu);
876 spin_lock_bh(&fps->fcoe_rx_list.lock);
877 if (!fps->thread) {
878 spin_unlock_bh(&fps->fcoe_rx_list.lock);
879 goto err;
880 }
881 }
882
883 /*
884 * We now have a valid CPU that we're targeting for
885 * this skb. We also have this receive thread locked,
886 * so we're free to queue skbs into it's queue.
887 */
Robert Love85b4aa42008-12-09 15:10:24 -0800888 __skb_queue_tail(&fps->fcoe_rx_list, skb);
889 if (fps->fcoe_rx_list.qlen == 1)
890 wake_up_process(fps->thread);
891
892 spin_unlock_bh(&fps->fcoe_rx_list.lock);
893
894 return 0;
895err:
Robert Love582b45b2009-03-31 15:51:50 -0700896 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800897
898err2:
899 kfree_skb(skb);
900 return -1;
901}
902EXPORT_SYMBOL_GPL(fcoe_rcv);
903
904/**
Robert Love34f42a02009-02-27 10:55:45 -0800905 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800906 * @skb: the skb to be xmitted
907 *
908 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800909 */
Robert Love85b4aa42008-12-09 15:10:24 -0800910static inline int fcoe_start_io(struct sk_buff *skb)
911{
912 int rc;
913
914 skb_get(skb);
915 rc = dev_queue_xmit(skb);
916 if (rc != 0)
917 return rc;
918 kfree_skb(skb);
919 return 0;
920}
921
922/**
Robert Love34f42a02009-02-27 10:55:45 -0800923 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800924 * @skb: the skb to be xmitted
925 * @tlen: total len
926 *
927 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800928 */
Robert Love85b4aa42008-12-09 15:10:24 -0800929static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
930{
931 struct fcoe_percpu_s *fps;
932 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800933
Robert Love5e5e92d2009-03-17 11:41:35 -0700934 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800935 page = fps->crc_eof_page;
936 if (!page) {
937 page = alloc_page(GFP_ATOMIC);
938 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700939 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800940 return -ENOMEM;
941 }
942 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700943 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800944 }
945
946 get_page(page);
947 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
948 fps->crc_eof_offset, tlen);
949 skb->len += tlen;
950 skb->data_len += tlen;
951 skb->truesize += tlen;
952 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
953
954 if (fps->crc_eof_offset >= PAGE_SIZE) {
955 fps->crc_eof_page = NULL;
956 fps->crc_eof_offset = 0;
957 put_page(page);
958 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700959 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800960 return 0;
961}
962
963/**
Robert Love34f42a02009-02-27 10:55:45 -0800964 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Robert Love85b4aa42008-12-09 15:10:24 -0800965 * @fp: the fc_frame containg data to be checksummed
966 *
967 * This uses crc32() to calculate the crc for fc frame
968 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800969 */
Robert Love85b4aa42008-12-09 15:10:24 -0800970u32 fcoe_fc_crc(struct fc_frame *fp)
971{
972 struct sk_buff *skb = fp_skb(fp);
973 struct skb_frag_struct *frag;
974 unsigned char *data;
975 unsigned long off, len, clen;
976 u32 crc;
977 unsigned i;
978
979 crc = crc32(~0, skb->data, skb_headlen(skb));
980
981 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
982 frag = &skb_shinfo(skb)->frags[i];
983 off = frag->page_offset;
984 len = frag->size;
985 while (len > 0) {
986 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
987 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
988 KM_SKB_DATA_SOFTIRQ);
989 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
990 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
991 off += clen;
992 len -= clen;
993 }
994 }
995 return crc;
996}
Robert Love85b4aa42008-12-09 15:10:24 -0800997
998/**
Robert Love34f42a02009-02-27 10:55:45 -0800999 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001000 * @lp: the associated local port
1001 * @fp: the fc_frame to be transmitted
1002 *
1003 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001004 */
Robert Love85b4aa42008-12-09 15:10:24 -08001005int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1006{
1007 int wlen, rc = 0;
1008 u32 crc;
1009 struct ethhdr *eh;
1010 struct fcoe_crc_eof *cp;
1011 struct sk_buff *skb;
1012 struct fcoe_dev_stats *stats;
1013 struct fc_frame_header *fh;
1014 unsigned int hlen; /* header length implies the version */
1015 unsigned int tlen; /* trailer length */
1016 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001017 struct fcoe_softc *fc;
1018 u8 sof, eof;
1019 struct fcoe_hdr *hp;
1020
1021 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1022
Robert Lovefc47ff62009-02-27 10:55:55 -08001023 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001024 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001025 skb = fp_skb(fp);
1026 wlen = skb->len / FCOE_WORD_TO_BYTE;
1027
1028 if (!lp->link_up) {
1029 kfree(skb);
1030 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001031 }
1032
Joe Eykholt97c83892009-03-17 11:42:40 -07001033 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1034 fcoe_ctlr_els_send(&fc->ctlr, skb))
1035 return 0;
1036
Robert Love85b4aa42008-12-09 15:10:24 -08001037 sof = fr_sof(fp);
1038 eof = fr_eof(fp);
1039
1040 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
1041 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
1042 hlen = sizeof(struct fcoe_hdr);
1043 tlen = sizeof(struct fcoe_crc_eof);
1044 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1045
1046 /* crc offload */
1047 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001048 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001049 skb->csum_start = skb_headroom(skb);
1050 skb->csum_offset = skb->len;
1051 crc = 0;
1052 } else {
1053 skb->ip_summed = CHECKSUM_NONE;
1054 crc = fcoe_fc_crc(fp);
1055 }
1056
1057 /* copy fc crc and eof to the skb buff */
1058 if (skb_is_nonlinear(skb)) {
1059 skb_frag_t *frag;
1060 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001061 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001062 return -ENOMEM;
1063 }
1064 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1065 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1066 + frag->page_offset;
1067 } else {
1068 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1069 }
1070
1071 memset(cp, 0, sizeof(*cp));
1072 cp->fcoe_eof = eof;
1073 cp->fcoe_crc32 = cpu_to_le32(~crc);
1074
1075 if (skb_is_nonlinear(skb)) {
1076 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1077 cp = NULL;
1078 }
1079
1080 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
1081 skb_push(skb, elen + hlen);
1082 skb_reset_mac_header(skb);
1083 skb_reset_network_header(skb);
1084 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001085 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001086 skb->dev = fc->real_dev;
1087
1088 /* fill up mac and fcoe headers */
1089 eh = eth_hdr(skb);
1090 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001091 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001092 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1093 else
1094 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001095 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001096
Joe Eykholt97c83892009-03-17 11:42:40 -07001097 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1098 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001099 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001100 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001101
1102 hp = (struct fcoe_hdr *)(eh + 1);
1103 memset(hp, 0, sizeof(*hp));
1104 if (FC_FCOE_VER)
1105 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1106 hp->fcoe_sof = sof;
1107
Yi Zou39ca9a02009-02-27 14:07:15 -08001108#ifdef NETIF_F_FSO
1109 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1110 if (lp->seq_offload && fr_max_payload(fp)) {
1111 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1112 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1113 } else {
1114 skb_shinfo(skb)->gso_type = 0;
1115 skb_shinfo(skb)->gso_size = 0;
1116 }
1117#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001118 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001119 stats = fc_lport_get_stats(lp);
1120 stats->TxFrames++;
1121 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001122
1123 /* send down to lld */
1124 fr_dev(fp) = lp;
1125 if (fc->fcoe_pending_queue.qlen)
1126 rc = fcoe_check_wait_queue(lp);
1127
1128 if (rc == 0)
1129 rc = fcoe_start_io(skb);
1130
1131 if (rc) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001132 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1133 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1134 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001135 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -08001136 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001137 }
1138
1139 return 0;
1140}
1141EXPORT_SYMBOL_GPL(fcoe_xmit);
1142
Robert Love34f42a02009-02-27 10:55:45 -08001143/**
1144 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001145 * @arg: ptr to the fcoe per cpu struct
1146 *
1147 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001148 */
1149int fcoe_percpu_receive_thread(void *arg)
1150{
1151 struct fcoe_percpu_s *p = arg;
1152 u32 fr_len;
1153 struct fc_lport *lp;
1154 struct fcoe_rcv_info *fr;
1155 struct fcoe_dev_stats *stats;
1156 struct fc_frame_header *fh;
1157 struct sk_buff *skb;
1158 struct fcoe_crc_eof crc_eof;
1159 struct fc_frame *fp;
1160 u8 *mac = NULL;
1161 struct fcoe_softc *fc;
1162 struct fcoe_hdr *hp;
1163
Robert Love4469c192009-02-27 10:56:38 -08001164 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001165
1166 while (!kthread_should_stop()) {
1167
1168 spin_lock_bh(&p->fcoe_rx_list.lock);
1169 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1170 set_current_state(TASK_INTERRUPTIBLE);
1171 spin_unlock_bh(&p->fcoe_rx_list.lock);
1172 schedule();
1173 set_current_state(TASK_RUNNING);
1174 if (kthread_should_stop())
1175 return 0;
1176 spin_lock_bh(&p->fcoe_rx_list.lock);
1177 }
1178 spin_unlock_bh(&p->fcoe_rx_list.lock);
1179 fr = fcoe_dev_from_skb(skb);
1180 lp = fr->fr_dev;
1181 if (unlikely(lp == NULL)) {
1182 FC_DBG("invalid HBA Structure");
1183 kfree_skb(skb);
1184 continue;
1185 }
1186
Robert Love85b4aa42008-12-09 15:10:24 -08001187 if (unlikely(debug_fcoe)) {
1188 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1189 "tail:%p end:%p sum:%d dev:%s",
1190 skb->len, skb->data_len,
1191 skb->head, skb->data, skb_tail_pointer(skb),
1192 skb_end_pointer(skb), skb->csum,
1193 skb->dev ? skb->dev->name : "<NULL>");
1194 }
1195
1196 /*
1197 * Save source MAC address before discarding header.
1198 */
1199 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001200 if (skb_is_nonlinear(skb))
1201 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001202 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001203
1204 /*
1205 * Frame length checks and setting up the header pointers
1206 * was done in fcoe_rcv already.
1207 */
1208 hp = (struct fcoe_hdr *) skb_network_header(skb);
1209 fh = (struct fc_frame_header *) skb_transport_header(skb);
1210
Robert Love582b45b2009-03-31 15:51:50 -07001211 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001212 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001213 if (stats->ErrorFrames < 5)
1214 printk(KERN_WARNING "FCoE version "
1215 "mismatch: The frame has "
1216 "version %x, but the "
1217 "initiator supports version "
1218 "%x\n", FC_FCOE_DECAPS_VER(hp),
1219 FC_FCOE_VER);
1220 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001221 kfree_skb(skb);
1222 continue;
1223 }
1224
1225 skb_pull(skb, sizeof(struct fcoe_hdr));
1226 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1227
Robert Love582b45b2009-03-31 15:51:50 -07001228 stats->RxFrames++;
1229 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001230
1231 fp = (struct fc_frame *)skb;
1232 fc_frame_init(fp);
1233 fr_dev(fp) = lp;
1234 fr_sof(fp) = hp->fcoe_sof;
1235
1236 /* Copy out the CRC and EOF trailer for access */
1237 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1238 kfree_skb(skb);
1239 continue;
1240 }
1241 fr_eof(fp) = crc_eof.fcoe_eof;
1242 fr_crc(fp) = crc_eof.fcoe_crc32;
1243 if (pskb_trim(skb, fr_len)) {
1244 kfree_skb(skb);
1245 continue;
1246 }
1247
1248 /*
1249 * We only check CRC if no offload is available and if it is
1250 * it's solicited data, in which case, the FCP layer would
1251 * check it during the copy.
1252 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001253 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001254 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1255 else
1256 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1257
1258 fh = fc_frame_header_get(fp);
1259 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1260 fh->fh_type == FC_TYPE_FCP) {
1261 fc_exch_recv(lp, lp->emp, fp);
1262 continue;
1263 }
1264 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1265 if (le32_to_cpu(fr_crc(fp)) !=
1266 ~crc32(~0, skb->data, fr_len)) {
1267 if (debug_fcoe || stats->InvalidCRCCount < 5)
1268 printk(KERN_WARNING "fcoe: dropping "
1269 "frame with CRC error\n");
1270 stats->InvalidCRCCount++;
1271 stats->ErrorFrames++;
1272 fc_frame_free(fp);
1273 continue;
1274 }
1275 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1276 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001277 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1278 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1279 fc_frame_free(fp);
1280 continue;
1281 }
Robert Love85b4aa42008-12-09 15:10:24 -08001282 fc_exch_recv(lp, lp->emp, fp);
1283 }
1284 return 0;
1285}
1286
1287/**
Robert Love34f42a02009-02-27 10:55:45 -08001288 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -08001289 * @vp:
1290 *
Vasu Devbc0e17f2009-02-27 10:54:57 -08001291 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -08001292 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1293 * fcoe_hostlist.
1294 *
1295 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001296 */
Robert Love85b4aa42008-12-09 15:10:24 -08001297void fcoe_watchdog(ulong vp)
1298{
Robert Love85b4aa42008-12-09 15:10:24 -08001299 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001300
1301 read_lock(&fcoe_hostlist_lock);
1302 list_for_each_entry(fc, &fcoe_hostlist, list) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001303 if (fc->ctlr.lp)
1304 fcoe_check_wait_queue(fc->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001305 }
1306 read_unlock(&fcoe_hostlist_lock);
1307
1308 fcoe_timer.expires = jiffies + (1 * HZ);
1309 add_timer(&fcoe_timer);
1310}
1311
1312
1313/**
Robert Love34f42a02009-02-27 10:55:45 -08001314 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
Robert Love85b4aa42008-12-09 15:10:24 -08001315 * @lp: the fc_port for this skb
1316 * @skb: the associated skb to be xmitted
1317 *
1318 * This empties the wait_queue, dequeue the head of the wait_queue queue
1319 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001320 * transmitted, return qlen or -1 if a error occurs, then restore
1321 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001322 *
1323 * The wait_queue is used when the skb transmit fails. skb will go
1324 * in the wait_queue which will be emptied by the time function OR
1325 * by the next skb transmit.
1326 *
1327 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001328 */
Robert Love85b4aa42008-12-09 15:10:24 -08001329static int fcoe_check_wait_queue(struct fc_lport *lp)
1330{
Chris Leech55c8baf2009-02-27 10:56:32 -08001331 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001332 struct sk_buff *skb;
Vasu Devc826a312009-02-27 10:56:27 -08001333 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001334
Robert Love85b4aa42008-12-09 15:10:24 -08001335 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001336 if (fc->fcoe_pending_queue_active)
1337 goto out;
1338 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001339
1340 while (fc->fcoe_pending_queue.qlen) {
1341 /* keep qlen > 0 until fcoe_start_io succeeds */
1342 fc->fcoe_pending_queue.qlen++;
1343 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1344
1345 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1346 rc = fcoe_start_io(skb);
1347 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1348
1349 if (rc) {
1350 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1351 /* undo temporary increment above */
1352 fc->fcoe_pending_queue.qlen--;
1353 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001354 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001355 /* undo temporary increment above */
1356 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001357 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001358
1359 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1360 lp->qfull = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001361 fc->fcoe_pending_queue_active = 0;
1362 rc = fc->fcoe_pending_queue.qlen;
1363out:
Robert Love85b4aa42008-12-09 15:10:24 -08001364 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001365 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001366}
1367
1368/**
Robert Love34f42a02009-02-27 10:55:45 -08001369 * fcoe_dev_setup() - setup link change notification interface
1370 */
1371static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -08001372{
1373 /*
1374 * here setup a interface specific wd time to
1375 * monitor the link state
1376 */
1377 register_netdevice_notifier(&fcoe_notifier);
1378}
1379
1380/**
Robert Love34f42a02009-02-27 10:55:45 -08001381 * fcoe_dev_setup() - cleanup link change notification interface
1382 */
Robert Love85b4aa42008-12-09 15:10:24 -08001383static void fcoe_dev_cleanup(void)
1384{
1385 unregister_netdevice_notifier(&fcoe_notifier);
1386}
1387
1388/**
Robert Love34f42a02009-02-27 10:55:45 -08001389 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001390 * @notifier: context of the notification
1391 * @event: type of event
1392 * @ptr: fixed array for output parsed ifname
1393 *
1394 * This function is called by the ethernet driver in case of link change event
1395 *
1396 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001397 */
Robert Love85b4aa42008-12-09 15:10:24 -08001398static int fcoe_device_notification(struct notifier_block *notifier,
1399 ulong event, void *ptr)
1400{
1401 struct fc_lport *lp = NULL;
1402 struct net_device *real_dev = ptr;
1403 struct fcoe_softc *fc;
1404 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001405 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001406 u32 mfs;
1407 int rc = NOTIFY_OK;
1408
1409 read_lock(&fcoe_hostlist_lock);
1410 list_for_each_entry(fc, &fcoe_hostlist, list) {
1411 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001412 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001413 break;
1414 }
1415 }
1416 read_unlock(&fcoe_hostlist_lock);
1417 if (lp == NULL) {
1418 rc = NOTIFY_DONE;
1419 goto out;
1420 }
1421
Robert Love85b4aa42008-12-09 15:10:24 -08001422 switch (event) {
1423 case NETDEV_DOWN:
1424 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001425 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001426 break;
1427 case NETDEV_UP:
1428 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001429 break;
1430 case NETDEV_CHANGEMTU:
1431 mfs = fc->real_dev->mtu -
1432 (sizeof(struct fcoe_hdr) +
1433 sizeof(struct fcoe_crc_eof));
1434 if (mfs >= FC_MIN_MAX_FRAME)
1435 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001436 break;
1437 case NETDEV_REGISTER:
1438 break;
1439 default:
Joe Eykholt97c83892009-03-17 11:42:40 -07001440 FC_DBG("Unknown event %ld from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001441 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001442 if (link_possible && !fcoe_link_ok(lp))
1443 fcoe_ctlr_link_up(&fc->ctlr);
1444 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1445 stats = fc_lport_get_stats(lp);
1446 stats->LinkFailureCount++;
1447 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001448 }
1449out:
1450 return rc;
1451}
1452
1453/**
Robert Love34f42a02009-02-27 10:55:45 -08001454 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001455 * @ifname: fixed array for output parsed ifname
1456 * @buffer: incoming buffer to be copied
1457 *
1458 * Returns: NULL or ptr to netdeive
Robert Love34f42a02009-02-27 10:55:45 -08001459 */
Robert Love85b4aa42008-12-09 15:10:24 -08001460static struct net_device *fcoe_if_to_netdev(const char *buffer)
1461{
1462 char *cp;
1463 char ifname[IFNAMSIZ + 2];
1464
1465 if (buffer) {
1466 strlcpy(ifname, buffer, IFNAMSIZ);
1467 cp = ifname + strlen(ifname);
1468 while (--cp >= ifname && *cp == '\n')
1469 *cp = '\0';
1470 return dev_get_by_name(&init_net, ifname);
1471 }
1472 return NULL;
1473}
1474
1475/**
Robert Love34f42a02009-02-27 10:55:45 -08001476 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001477 * @netdev: the target netdev
1478 *
1479 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001480 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001481static struct module *
1482fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001483{
1484 struct device *dev;
1485
1486 if (!netdev)
1487 return NULL;
1488
1489 dev = netdev->dev.parent;
1490 if (!dev)
1491 return NULL;
1492
1493 if (!dev->driver)
1494 return NULL;
1495
1496 return dev->driver->owner;
1497}
1498
1499/**
Robert Love34f42a02009-02-27 10:55:45 -08001500 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001501 * @netdev: the target netdev
1502 *
Robert Love34f42a02009-02-27 10:55:45 -08001503 * Holds the Ethernet driver module by try_module_get() for
1504 * the corresponding netdev.
1505 *
Robert Love85b4aa42008-12-09 15:10:24 -08001506 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -08001507 */
Robert Love85b4aa42008-12-09 15:10:24 -08001508static int fcoe_ethdrv_get(const struct net_device *netdev)
1509{
1510 struct module *owner;
1511
1512 owner = fcoe_netdev_to_module_owner(netdev);
1513 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001514 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1515 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001516 return try_module_get(owner);
1517 }
1518 return -ENODEV;
1519}
1520
1521/**
Robert Love34f42a02009-02-27 10:55:45 -08001522 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001523 * @netdev: the target netdev
1524 *
Robert Love34f42a02009-02-27 10:55:45 -08001525 * Releases the Ethernet driver module by module_put for
1526 * the corresponding netdev.
1527 *
Robert Love85b4aa42008-12-09 15:10:24 -08001528 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -08001529 */
Robert Love85b4aa42008-12-09 15:10:24 -08001530static int fcoe_ethdrv_put(const struct net_device *netdev)
1531{
1532 struct module *owner;
1533
1534 owner = fcoe_netdev_to_module_owner(netdev);
1535 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001536 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1537 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001538 module_put(owner);
1539 return 0;
1540 }
1541 return -ENODEV;
1542}
1543
1544/**
Robert Love34f42a02009-02-27 10:55:45 -08001545 * fcoe_destroy() - handles the destroy from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -08001546 * @buffer: expcted to be a eth if name
1547 * @kp: associated kernel param
1548 *
1549 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001550 */
Robert Love85b4aa42008-12-09 15:10:24 -08001551static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1552{
1553 int rc;
1554 struct net_device *netdev;
1555
1556 netdev = fcoe_if_to_netdev(buffer);
1557 if (!netdev) {
1558 rc = -ENODEV;
1559 goto out_nodev;
1560 }
1561 /* look for existing lport */
1562 if (!fcoe_hostlist_lookup(netdev)) {
1563 rc = -ENODEV;
1564 goto out_putdev;
1565 }
Vasu Dev7f349142009-03-27 09:06:31 -07001566 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001567 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001568 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001569 netdev->name);
1570 rc = -EIO;
1571 goto out_putdev;
1572 }
1573 fcoe_ethdrv_put(netdev);
1574 rc = 0;
1575out_putdev:
1576 dev_put(netdev);
1577out_nodev:
1578 return rc;
1579}
1580
1581/**
Robert Love34f42a02009-02-27 10:55:45 -08001582 * fcoe_create() - Handles the create call from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -08001583 * @buffer: expcted to be a eth if name
1584 * @kp: associated kernel param
1585 *
1586 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001587 */
Robert Love85b4aa42008-12-09 15:10:24 -08001588static int fcoe_create(const char *buffer, struct kernel_param *kp)
1589{
1590 int rc;
1591 struct net_device *netdev;
1592
1593 netdev = fcoe_if_to_netdev(buffer);
1594 if (!netdev) {
1595 rc = -ENODEV;
1596 goto out_nodev;
1597 }
1598 /* look for existing lport */
1599 if (fcoe_hostlist_lookup(netdev)) {
1600 rc = -EEXIST;
1601 goto out_putdev;
1602 }
1603 fcoe_ethdrv_get(netdev);
1604
Vasu Dev7f349142009-03-27 09:06:31 -07001605 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001606 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001607 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001608 netdev->name);
1609 fcoe_ethdrv_put(netdev);
1610 rc = -EIO;
1611 goto out_putdev;
1612 }
1613 rc = 0;
1614out_putdev:
1615 dev_put(netdev);
1616out_nodev:
1617 return rc;
1618}
1619
1620module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1621__MODULE_PARM_TYPE(create, "string");
1622MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1623module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1624__MODULE_PARM_TYPE(destroy, "string");
1625MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1626
Robert Love34f42a02009-02-27 10:55:45 -08001627/**
1628 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001629 * @lp: ptr to the fc_lport
1630 *
1631 * Any permanently-disqualifying conditions have been previously checked.
1632 * This also updates the speed setting, which may change with link for 100/1000.
1633 *
1634 * This function should probably be checking for PAUSE support at some point
1635 * in the future. Currently Per-priority-pause is not determinable using
1636 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1637 *
1638 * Returns: 0 if link is OK for use by FCoE.
1639 *
1640 */
1641int fcoe_link_ok(struct fc_lport *lp)
1642{
Robert Lovefc47ff62009-02-27 10:55:55 -08001643 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001644 struct net_device *dev = fc->real_dev;
1645 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1646 int rc = 0;
1647
1648 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1649 dev = fc->phys_dev;
1650 if (dev->ethtool_ops->get_settings) {
1651 dev->ethtool_ops->get_settings(dev, &ecmd);
1652 lp->link_supported_speeds &=
1653 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1654 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1655 SUPPORTED_1000baseT_Full))
1656 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1657 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1658 lp->link_supported_speeds |=
1659 FC_PORTSPEED_10GBIT;
1660 if (ecmd.speed == SPEED_1000)
1661 lp->link_speed = FC_PORTSPEED_1GBIT;
1662 if (ecmd.speed == SPEED_10000)
1663 lp->link_speed = FC_PORTSPEED_10GBIT;
1664 }
1665 } else
1666 rc = -1;
1667
1668 return rc;
1669}
1670EXPORT_SYMBOL_GPL(fcoe_link_ok);
1671
Robert Love34f42a02009-02-27 10:55:45 -08001672/**
1673 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001674 * @lp: the fc_lport
1675 */
1676void fcoe_percpu_clean(struct fc_lport *lp)
1677{
Robert Love85b4aa42008-12-09 15:10:24 -08001678 struct fcoe_percpu_s *pp;
1679 struct fcoe_rcv_info *fr;
1680 struct sk_buff_head *list;
1681 struct sk_buff *skb, *next;
1682 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001683 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001684
Robert Love5e5e92d2009-03-17 11:41:35 -07001685 for_each_possible_cpu(cpu) {
1686 pp = &per_cpu(fcoe_percpu, cpu);
1687 spin_lock_bh(&pp->fcoe_rx_list.lock);
1688 list = &pp->fcoe_rx_list;
1689 head = list->next;
1690 for (skb = head; skb != (struct sk_buff *)list;
1691 skb = next) {
1692 next = skb->next;
1693 fr = fcoe_dev_from_skb(skb);
1694 if (fr->fr_dev == lp) {
1695 __skb_unlink(skb, list);
1696 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001697 }
Robert Love85b4aa42008-12-09 15:10:24 -08001698 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001699 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001700 }
1701}
1702EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1703
1704/**
Robert Love34f42a02009-02-27 10:55:45 -08001705 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001706 * @lp: the corresponding fc_lport
1707 *
1708 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001709 */
Robert Love85b4aa42008-12-09 15:10:24 -08001710void fcoe_clean_pending_queue(struct fc_lport *lp)
1711{
1712 struct fcoe_softc *fc = lport_priv(lp);
1713 struct sk_buff *skb;
1714
1715 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1716 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1717 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1718 kfree_skb(skb);
1719 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1720 }
1721 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1722}
1723EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1724
1725/**
Robert Love34f42a02009-02-27 10:55:45 -08001726 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001727 * @shost: shost the reset is from
1728 *
1729 * Returns: always 0
1730 */
1731int fcoe_reset(struct Scsi_Host *shost)
1732{
1733 struct fc_lport *lport = shost_priv(shost);
1734 fc_lport_reset(lport);
1735 return 0;
1736}
1737EXPORT_SYMBOL_GPL(fcoe_reset);
1738
Robert Love34f42a02009-02-27 10:55:45 -08001739/**
Robert Love34f42a02009-02-27 10:55:45 -08001740 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Robert Love85b4aa42008-12-09 15:10:24 -08001741 * @device: this is currently ptr to net_device
1742 *
1743 * Returns: NULL or the located fcoe_softc
1744 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001745static struct fcoe_softc *
1746fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001747{
1748 struct fcoe_softc *fc;
1749
1750 read_lock(&fcoe_hostlist_lock);
1751 list_for_each_entry(fc, &fcoe_hostlist, list) {
1752 if (fc->real_dev == dev) {
1753 read_unlock(&fcoe_hostlist_lock);
1754 return fc;
1755 }
1756 }
1757 read_unlock(&fcoe_hostlist_lock);
1758 return NULL;
1759}
1760
Robert Love34f42a02009-02-27 10:55:45 -08001761/**
1762 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001763 * @netdev: ptr to net_device
1764 *
1765 * Returns: 0 for success
1766 */
1767struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1768{
1769 struct fcoe_softc *fc;
1770
1771 fc = fcoe_hostlist_lookup_softc(netdev);
1772
Joe Eykholt97c83892009-03-17 11:42:40 -07001773 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001774}
1775EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1776
Robert Love34f42a02009-02-27 10:55:45 -08001777/**
1778 * fcoe_hostlist_add() - Add a lport to lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001779 * @lp: ptr to the fc_lport to badded
1780 *
1781 * Returns: 0 for success
1782 */
1783int fcoe_hostlist_add(const struct fc_lport *lp)
1784{
1785 struct fcoe_softc *fc;
1786
1787 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1788 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001789 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001790 write_lock_bh(&fcoe_hostlist_lock);
1791 list_add_tail(&fc->list, &fcoe_hostlist);
1792 write_unlock_bh(&fcoe_hostlist_lock);
1793 }
1794 return 0;
1795}
1796EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1797
Robert Love34f42a02009-02-27 10:55:45 -08001798/**
1799 * fcoe_hostlist_remove() - remove a lport from lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001800 * @lp: ptr to the fc_lport to badded
1801 *
1802 * Returns: 0 for success
1803 */
1804int fcoe_hostlist_remove(const struct fc_lport *lp)
1805{
1806 struct fcoe_softc *fc;
1807
1808 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1809 BUG_ON(!fc);
1810 write_lock_bh(&fcoe_hostlist_lock);
1811 list_del(&fc->list);
1812 write_unlock_bh(&fcoe_hostlist_lock);
1813
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1817
1818/**
Robert Love34f42a02009-02-27 10:55:45 -08001819 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001820 *
Robert Love85b4aa42008-12-09 15:10:24 -08001821 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001822 */
Robert Love85b4aa42008-12-09 15:10:24 -08001823static int __init fcoe_init(void)
1824{
Robert Love38eccab2009-03-17 11:41:30 -07001825 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001826 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001827 struct fcoe_percpu_s *p;
1828
Robert Love85b4aa42008-12-09 15:10:24 -08001829 INIT_LIST_HEAD(&fcoe_hostlist);
1830 rwlock_init(&fcoe_hostlist_lock);
1831
Robert Love38eccab2009-03-17 11:41:30 -07001832 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001833 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001834 skb_queue_head_init(&p->fcoe_rx_list);
1835 }
1836
Robert Love8976f422009-03-17 11:41:46 -07001837 for_each_online_cpu(cpu)
1838 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001839
Robert Love8976f422009-03-17 11:41:46 -07001840 /* Initialize per CPU interrupt thread */
1841 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1842 if (rc)
1843 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001844
Robert Love8976f422009-03-17 11:41:46 -07001845 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001846 fcoe_dev_setup();
1847
Robert Lovea468f322009-02-27 10:56:00 -08001848 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1849
1850 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001851
Vasu Dev7f349142009-03-27 09:06:31 -07001852 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001853
1854 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001855
1856out_free:
1857 for_each_online_cpu(cpu) {
1858 fcoe_percpu_thread_destroy(cpu);
1859 }
1860
1861 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001862}
1863module_init(fcoe_init);
1864
1865/**
Robert Love34f42a02009-02-27 10:55:45 -08001866 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001867 *
1868 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001869 */
Robert Love85b4aa42008-12-09 15:10:24 -08001870static void __exit fcoe_exit(void)
1871{
Robert Love5e5e92d2009-03-17 11:41:35 -07001872 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001873 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001874
Robert Love85b4aa42008-12-09 15:10:24 -08001875 fcoe_dev_cleanup();
1876
Robert Love582b45b2009-03-31 15:51:50 -07001877 /* Stop the timer */
Robert Love85b4aa42008-12-09 15:10:24 -08001878 del_timer_sync(&fcoe_timer);
1879
Vasu Dev5919a592009-03-27 09:03:29 -07001880 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001881 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001882 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001883
Robert Love8976f422009-03-17 11:41:46 -07001884 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1885
1886 for_each_online_cpu(cpu) {
1887 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001888 }
1889
Vasu Dev7f349142009-03-27 09:06:31 -07001890 /* detach from scsi transport */
1891 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001892}
1893module_exit(fcoe_exit);