blob: ce33f107b0a00d4b562fde4ced6dd15ea193e359 [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/**
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
Vasu Dev7f349142009-03-27 09:06:31 -0700193 *
194 * Returns: 0 for success
195 */
196static int fcoe_lport_config(struct fc_lport *lp)
197{
198 lp->link_up = 0;
199 lp->qfull = 0;
200 lp->max_retry_count = 3;
201 lp->e_d_tov = 2 * 1000; /* FC-FS default */
202 lp->r_a_tov = 2 * 2 * 1000;
203 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
204 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
205
206 fc_lport_init_stats(lp);
207
208 /* lport fc_lport related configuration */
209 fc_lport_config(lp);
210
211 /* offload related configuration */
212 lp->crc_offload = 0;
213 lp->seq_offload = 0;
214 lp->lro_enabled = 0;
215 lp->lro_xid = 0;
216 lp->lso_max = 0;
217
218 return 0;
219}
220
221/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000222 * fcoe_netdev_cleanup() - clean up netdev configurations
223 * @fc: ptr to the fcoe_softc
224 */
225void fcoe_netdev_cleanup(struct fcoe_softc *fc)
226{
227 u8 flogi_maddr[ETH_ALEN];
228
229 /* Don't listen for Ethernet packets anymore */
230 dev_remove_pack(&fc->fcoe_packet_type);
231 dev_remove_pack(&fc->fip_packet_type);
232
233 /* Delete secondary MAC addresses */
234 rtnl_lock();
235 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
236 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
237 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
238 dev_unicast_delete(fc->real_dev,
239 fc->ctlr.data_src_addr, ETH_ALEN);
Vasu Dev184dd342009-05-17 12:33:28 +0000240 if (fc->ctlr.spma)
241 dev_unicast_delete(fc->real_dev,
242 fc->ctlr.ctl_src_addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000243 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
244 rtnl_unlock();
245}
246
247/**
Vasu Dev7f349142009-03-27 09:06:31 -0700248 * fcoe_netdev_config() - Set up netdev for SW FCoE
249 * @lp : ptr to the fc_lport
250 * @netdev : ptr to the associated netdevice struct
251 *
252 * Must be called after fcoe_lport_config() as it will use lport mutex
253 *
254 * Returns : 0 for success
255 */
256static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
257{
258 u32 mfs;
259 u64 wwnn, wwpn;
260 struct fcoe_softc *fc;
261 u8 flogi_maddr[ETH_ALEN];
Vasu Dev184dd342009-05-17 12:33:28 +0000262 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700263
264 /* Setup lport private data to point to fcoe softc */
265 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700266 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700267 fc->real_dev = netdev;
268 fc->phys_dev = netdev;
269
270 /* Require support for get_pauseparam ethtool op. */
271 if (netdev->priv_flags & IFF_802_1Q_VLAN)
272 fc->phys_dev = vlan_dev_real_dev(netdev);
273
274 /* Do not support for bonding device */
275 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
276 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
277 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
278 return -EOPNOTSUPP;
279 }
280
281 /*
282 * Determine max frame size based on underlying device and optional
283 * user-configured limit. If the MFS is too low, fcoe_link_ok()
284 * will return 0, so do this first.
285 */
286 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
287 sizeof(struct fcoe_crc_eof));
288 if (fc_set_mfs(lp, mfs))
289 return -EINVAL;
290
Vasu Dev7f349142009-03-27 09:06:31 -0700291 /* offload features support */
292 if (fc->real_dev->features & NETIF_F_SG)
293 lp->sg_supp = 1;
294
295#ifdef NETIF_F_FCOE_CRC
296 if (netdev->features & NETIF_F_FCOE_CRC) {
297 lp->crc_offload = 1;
298 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
299 netdev->name);
300 }
301#endif
302#ifdef NETIF_F_FSO
303 if (netdev->features & NETIF_F_FSO) {
304 lp->seq_offload = 1;
305 lp->lso_max = netdev->gso_max_size;
306 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
307 netdev->name, lp->lso_max);
308 }
309#endif
310 if (netdev->fcoe_ddp_xid) {
311 lp->lro_enabled = 1;
312 lp->lro_xid = netdev->fcoe_ddp_xid;
313 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
314 netdev->name, lp->lro_xid);
315 }
316 skb_queue_head_init(&fc->fcoe_pending_queue);
317 fc->fcoe_pending_queue_active = 0;
318
Vasu Dev184dd342009-05-17 12:33:28 +0000319 /* look for SAN MAC address, if multiple SAN MACs exist, only
320 * use the first one for SPMA */
321 rcu_read_lock();
322 for_each_dev_addr(netdev, ha) {
323 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
324 (is_valid_ether_addr(fc->ctlr.ctl_src_addr))) {
325 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
326 fc->ctlr.spma = 1;
327 break;
328 }
329 }
330 rcu_read_unlock();
331
Vasu Dev7f349142009-03-27 09:06:31 -0700332 /* setup Source Mac Address */
Vasu Dev184dd342009-05-17 12:33:28 +0000333 if (!fc->ctlr.spma)
334 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
335 fc->real_dev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700336
337 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
338 fc_set_wwnn(lp, wwnn);
339 /* XXX - 3rd arg needs to be vlan id */
340 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
341 fc_set_wwpn(lp, wwpn);
342
343 /*
344 * Add FCoE MAC address as second unicast MAC address
345 * or enter promiscuous mode if not capable of listening
346 * for multiple unicast MACs.
347 */
348 rtnl_lock();
349 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
350 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
Vasu Dev184dd342009-05-17 12:33:28 +0000351 if (fc->ctlr.spma)
352 dev_unicast_add(fc->real_dev, fc->ctlr.ctl_src_addr, ETH_ALEN);
Joe Eykholt6401bdc2009-04-21 16:27:46 -0700353 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700354 rtnl_unlock();
355
356 /*
357 * setup the receive function from ethernet driver
358 * on the ethertype for the given device
359 */
360 fc->fcoe_packet_type.func = fcoe_rcv;
361 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
362 fc->fcoe_packet_type.dev = fc->real_dev;
363 dev_add_pack(&fc->fcoe_packet_type);
364
Vasu Devab6b85c2009-05-17 12:33:08 +0000365 fc->fip_packet_type.func = fcoe_fip_recv;
366 fc->fip_packet_type.type = htons(ETH_P_FIP);
367 fc->fip_packet_type.dev = fc->real_dev;
368 dev_add_pack(&fc->fip_packet_type);
369
Vasu Dev7f349142009-03-27 09:06:31 -0700370 return 0;
371}
372
373/**
374 * fcoe_shost_config() - Sets up fc_lport->host
375 * @lp : ptr to the fc_lport
376 * @shost : ptr to the associated scsi host
377 * @dev : device associated to scsi host
378 *
379 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
380 *
381 * Returns : 0 for success
382 */
383static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
384 struct device *dev)
385{
386 int rc = 0;
387
388 /* lport scsi host config */
389 lp->host = shost;
390
391 lp->host->max_lun = FCOE_MAX_LUN;
392 lp->host->max_id = FCOE_MAX_FCP_TARGET;
393 lp->host->max_channel = 0;
394 lp->host->transportt = scsi_transport_fcoe_sw;
395
396 /* add the new host to the SCSI-ml */
397 rc = scsi_add_host(lp->host, dev);
398 if (rc) {
399 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
400 return rc;
401 }
402 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
403 FCOE_NAME, FCOE_VERSION,
404 fcoe_netdev(lp)->name);
405
406 return 0;
407}
408
409/**
410 * fcoe_em_config() - allocates em for this lport
411 * @lp: the port that em is to allocated for
412 *
413 * Returns : 0 on success
414 */
415static inline int fcoe_em_config(struct fc_lport *lp)
416{
417 BUG_ON(lp->emp);
418
419 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
420 FCOE_MIN_XID, FCOE_MAX_XID);
421 if (!lp->emp)
422 return -ENOMEM;
423
424 return 0;
425}
426
427/**
428 * fcoe_if_destroy() - FCoE software HBA tear-down function
429 * @netdev: ptr to the associated net_device
430 *
431 * Returns: 0 if link is OK for use by FCoE.
432 */
433static int fcoe_if_destroy(struct net_device *netdev)
434{
435 struct fc_lport *lp = NULL;
436 struct fcoe_softc *fc;
Vasu Dev7f349142009-03-27 09:06:31 -0700437
438 BUG_ON(!netdev);
439
440 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
441 netdev->name);
442
443 lp = fcoe_hostlist_lookup(netdev);
444 if (!lp)
445 return -ENODEV;
446
447 fc = lport_priv(lp);
448
449 /* Logout of the fabric */
450 fc_fabric_logoff(lp);
451
452 /* Remove the instance from fcoe's list */
453 fcoe_hostlist_remove(lp);
454
Vasu Devab6b85c2009-05-17 12:33:08 +0000455 /* clean up netdev configurations */
456 fcoe_netdev_cleanup(fc);
457
458 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700459 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700460
461 /* Cleanup the fc_lport */
462 fc_lport_destroy(lp);
463 fc_fcp_destroy(lp);
464
465 /* Detach from the scsi-ml */
466 fc_remove_host(lp->host);
467 scsi_remove_host(lp->host);
468
469 /* There are no more rports or I/O, free the EM */
470 if (lp->emp)
471 fc_exch_mgr_free(lp->emp);
472
Chris Leechdd3fd722009-04-21 16:27:36 -0700473 /* Free the per-CPU receive threads */
Vasu Dev7f349142009-03-27 09:06:31 -0700474 fcoe_percpu_clean(lp);
475
476 /* Free existing skbs */
477 fcoe_clean_pending_queue(lp);
478
479 /* Free memory used by statistical counters */
480 fc_lport_free_stats(lp);
481
482 /* Release the net_device and Scsi_Host */
483 dev_put(fc->real_dev);
484 scsi_host_put(lp->host);
485
486 return 0;
487}
488
489/*
490 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
491 * @lp: the corresponding fc_lport
492 * @xid: the exchange id for this ddp transfer
493 * @sgl: the scatterlist describing this transfer
494 * @sgc: number of sg items
495 *
496 * Returns : 0 no ddp
497 */
498static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
499 struct scatterlist *sgl, unsigned int sgc)
500{
501 struct net_device *n = fcoe_netdev(lp);
502
503 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
504 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
505
506 return 0;
507}
508
509/*
510 * fcoe_ddp_done - calls LLD's ddp_done through net_device
511 * @lp: the corresponding fc_lport
512 * @xid: the exchange id for this ddp transfer
513 *
514 * Returns : the length of data that have been completed by ddp
515 */
516static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
517{
518 struct net_device *n = fcoe_netdev(lp);
519
520 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
521 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
522 return 0;
523}
524
525static struct libfc_function_template fcoe_libfc_fcn_templ = {
526 .frame_send = fcoe_xmit,
527 .ddp_setup = fcoe_ddp_setup,
528 .ddp_done = fcoe_ddp_done,
529};
530
531/**
532 * fcoe_if_create() - this function creates the fcoe interface
533 * @netdev: pointer the associated netdevice
534 *
535 * Creates fc_lport struct and scsi_host for lport, configures lport
536 * and starts fabric login.
537 *
538 * Returns : 0 on success
539 */
540static int fcoe_if_create(struct net_device *netdev)
541{
542 int rc;
543 struct fc_lport *lp = NULL;
544 struct fcoe_softc *fc;
545 struct Scsi_Host *shost;
546
547 BUG_ON(!netdev);
548
549 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
550 netdev->name);
551
552 lp = fcoe_hostlist_lookup(netdev);
553 if (lp)
554 return -EEXIST;
555
Vasu Deva0a25da2009-03-17 11:42:29 -0700556 shost = libfc_host_alloc(&fcoe_shost_template,
557 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700558 if (!shost) {
559 FC_DBG("Could not allocate host structure\n");
560 return -ENOMEM;
561 }
562 lp = shost_priv(shost);
563 fc = lport_priv(lp);
564
565 /* configure fc_lport, e.g., em */
566 rc = fcoe_lport_config(lp);
567 if (rc) {
568 FC_DBG("Could not configure lport\n");
569 goto out_host_put;
570 }
571
Joe Eykholt97c83892009-03-17 11:42:40 -0700572 /*
573 * Initialize FIP.
574 */
575 fcoe_ctlr_init(&fc->ctlr);
576 fc->ctlr.send = fcoe_fip_send;
577 fc->ctlr.update_mac = fcoe_update_src_mac;
578
Vasu Devab6b85c2009-05-17 12:33:08 +0000579 /* configure lport network properties */
580 rc = fcoe_netdev_config(lp, netdev);
581 if (rc) {
582 FC_DBG("Could not configure netdev for the interface\n");
583 goto out_netdev_cleanup;
584 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700585
Vasu Dev7f349142009-03-27 09:06:31 -0700586 /* configure lport scsi host properties */
587 rc = fcoe_shost_config(lp, shost, &netdev->dev);
588 if (rc) {
589 FC_DBG("Could not configure shost for lport\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000590 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700591 }
592
593 /* lport exch manager allocation */
594 rc = fcoe_em_config(lp);
595 if (rc) {
596 FC_DBG("Could not configure em for lport\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000597 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700598 }
599
600 /* Initialize the library */
601 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
602 if (rc) {
603 FC_DBG("Could not configure libfc for lport!\n");
604 goto out_lp_destroy;
605 }
606
607 /* add to lports list */
608 fcoe_hostlist_add(lp);
609
610 lp->boot_time = jiffies;
611
612 fc_fabric_login(lp);
613
Joe Eykholt97c83892009-03-17 11:42:40 -0700614 if (!fcoe_link_ok(lp))
615 fcoe_ctlr_link_up(&fc->ctlr);
616
Vasu Dev7f349142009-03-27 09:06:31 -0700617 dev_hold(netdev);
618
619 return rc;
620
621out_lp_destroy:
622 fc_exch_mgr_free(lp->emp); /* Free the EM */
Vasu Devab6b85c2009-05-17 12:33:08 +0000623out_netdev_cleanup:
624 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700625out_host_put:
626 scsi_host_put(lp->host);
627 return rc;
628}
629
630/**
631 * fcoe_if_init() - attach to scsi transport
632 *
633 * Returns : 0 on success
634 */
635static int __init fcoe_if_init(void)
636{
637 /* attach to scsi transport */
638 scsi_transport_fcoe_sw =
639 fc_attach_transport(&fcoe_transport_function);
640
641 if (!scsi_transport_fcoe_sw) {
642 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
643 return -ENODEV;
644 }
645
646 return 0;
647}
648
649/**
650 * fcoe_if_exit() - detach from scsi transport
651 *
652 * Returns : 0 on success
653 */
654int __exit fcoe_if_exit(void)
655{
656 fc_release_transport(scsi_transport_fcoe_sw);
657 return 0;
658}
659
Robert Love85b4aa42008-12-09 15:10:24 -0800660/**
Robert Love8976f422009-03-17 11:41:46 -0700661 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
662 * @cpu: cpu index for the online cpu
663 */
664static void fcoe_percpu_thread_create(unsigned int cpu)
665{
666 struct fcoe_percpu_s *p;
667 struct task_struct *thread;
668
669 p = &per_cpu(fcoe_percpu, cpu);
670
671 thread = kthread_create(fcoe_percpu_receive_thread,
672 (void *)p, "fcoethread/%d", cpu);
673
674 if (likely(!IS_ERR(p->thread))) {
675 kthread_bind(thread, cpu);
676 wake_up_process(thread);
677
678 spin_lock_bh(&p->fcoe_rx_list.lock);
679 p->thread = thread;
680 spin_unlock_bh(&p->fcoe_rx_list.lock);
681 }
682}
683
684/**
685 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
686 * @cpu: cpu index the rx thread is to be removed
687 *
688 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
689 * current CPU's Rx thread. If the thread being destroyed is bound to
690 * the CPU processing this context the skbs will be freed.
691 */
692static void fcoe_percpu_thread_destroy(unsigned int cpu)
693{
694 struct fcoe_percpu_s *p;
695 struct task_struct *thread;
696 struct page *crc_eof;
697 struct sk_buff *skb;
698#ifdef CONFIG_SMP
699 struct fcoe_percpu_s *p0;
700 unsigned targ_cpu = smp_processor_id();
701#endif /* CONFIG_SMP */
702
703 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
704
705 /* Prevent any new skbs from being queued for this CPU. */
706 p = &per_cpu(fcoe_percpu, cpu);
707 spin_lock_bh(&p->fcoe_rx_list.lock);
708 thread = p->thread;
709 p->thread = NULL;
710 crc_eof = p->crc_eof_page;
711 p->crc_eof_page = NULL;
712 p->crc_eof_offset = 0;
713 spin_unlock_bh(&p->fcoe_rx_list.lock);
714
715#ifdef CONFIG_SMP
716 /*
717 * Don't bother moving the skb's if this context is running
718 * on the same CPU that is having its thread destroyed. This
719 * can easily happen when the module is removed.
720 */
721 if (cpu != targ_cpu) {
722 p0 = &per_cpu(fcoe_percpu, targ_cpu);
723 spin_lock_bh(&p0->fcoe_rx_list.lock);
724 if (p0->thread) {
725 FC_DBG("Moving frames from CPU %d to CPU %d\n",
726 cpu, targ_cpu);
727
728 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
729 __skb_queue_tail(&p0->fcoe_rx_list, skb);
730 spin_unlock_bh(&p0->fcoe_rx_list.lock);
731 } else {
732 /*
733 * The targeted CPU is not initialized and cannot accept
734 * new skbs. Unlock the targeted CPU and drop the skbs
735 * on the CPU that is going offline.
736 */
737 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
738 kfree_skb(skb);
739 spin_unlock_bh(&p0->fcoe_rx_list.lock);
740 }
741 } else {
742 /*
743 * This scenario occurs when the module is being removed
744 * and all threads are being destroyed. skbs will continue
745 * to be shifted from the CPU thread that is being removed
746 * to the CPU thread associated with the CPU that is processing
747 * the module removal. Once there is only one CPU Rx thread it
748 * will reach this case and we will drop all skbs and later
749 * stop the thread.
750 */
751 spin_lock_bh(&p->fcoe_rx_list.lock);
752 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
753 kfree_skb(skb);
754 spin_unlock_bh(&p->fcoe_rx_list.lock);
755 }
756#else
757 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700758 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700759 * being removed. Free all skbs and stop the thread.
760 */
761 spin_lock_bh(&p->fcoe_rx_list.lock);
762 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
763 kfree_skb(skb);
764 spin_unlock_bh(&p->fcoe_rx_list.lock);
765#endif
766
767 if (thread)
768 kthread_stop(thread);
769
770 if (crc_eof)
771 put_page(crc_eof);
772}
773
774/**
775 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
776 * @nfb: callback data block
777 * @action: event triggering the callback
778 * @hcpu: index for the cpu of this event
779 *
780 * This creates or destroys per cpu data for fcoe
781 *
782 * Returns NOTIFY_OK always.
783 */
784static int fcoe_cpu_callback(struct notifier_block *nfb,
785 unsigned long action, void *hcpu)
786{
787 unsigned cpu = (unsigned long)hcpu;
788
789 switch (action) {
790 case CPU_ONLINE:
791 case CPU_ONLINE_FROZEN:
792 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
793 fcoe_percpu_thread_create(cpu);
794 break;
795 case CPU_DEAD:
796 case CPU_DEAD_FROZEN:
797 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
798 fcoe_percpu_thread_destroy(cpu);
799 break;
800 default:
801 break;
802 }
803 return NOTIFY_OK;
804}
805
806static struct notifier_block fcoe_cpu_notifier = {
807 .notifier_call = fcoe_cpu_callback,
808};
809
810/**
Robert Love34f42a02009-02-27 10:55:45 -0800811 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800812 * @skb: the receive skb
813 * @dev: associated net device
814 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700815 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800816 *
817 * this function will receive the packet and build fc frame and pass it up
818 *
819 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800820 */
Robert Love85b4aa42008-12-09 15:10:24 -0800821int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
822 struct packet_type *ptype, struct net_device *olddev)
823{
824 struct fc_lport *lp;
825 struct fcoe_rcv_info *fr;
826 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800827 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800828 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700829 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700830 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800831
832 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700833 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800834 if (unlikely(lp == NULL)) {
835 FC_DBG("cannot find hba structure");
836 goto err2;
837 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700838 if (!lp->link_up)
839 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800840
841 if (unlikely(debug_fcoe)) {
842 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
843 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
844 skb->head, skb->data, skb_tail_pointer(skb),
845 skb_end_pointer(skb), skb->csum,
846 skb->dev ? skb->dev->name : "<NULL>");
847
848 }
849
850 /* check for FCOE packet type */
851 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
852 FC_DBG("wrong FC type frame");
853 goto err;
854 }
855
856 /*
857 * Check for minimum frame length, and make sure required FCoE
858 * and FC headers are pulled into the linear data area.
859 */
860 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
861 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
862 goto err;
863
864 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
865 fh = (struct fc_frame_header *) skb_transport_header(skb);
866
867 oxid = ntohs(fh->fh_ox_id);
868
869 fr = fcoe_dev_from_skb(skb);
870 fr->fr_dev = lp;
871 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700872
Robert Love85b4aa42008-12-09 15:10:24 -0800873#ifdef CONFIG_SMP
874 /*
875 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700876 * cpu bits to get cpu and then this cpu is used for selecting
877 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800878 */
Robert Love8976f422009-03-17 11:41:46 -0700879 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800880#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700881
Robert Love8976f422009-03-17 11:41:46 -0700882 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800883 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700884 if (unlikely(!fps->thread)) {
885 /*
886 * The targeted CPU is not ready, let's target
887 * the first CPU now. For non-SMP systems this
888 * will check the same CPU twice.
889 */
890 FC_DBG("CPU is online, but no receive thread ready "
891 "for incoming skb- using first online CPU.\n");
892
893 spin_unlock_bh(&fps->fcoe_rx_list.lock);
894 cpu = first_cpu(cpu_online_map);
895 fps = &per_cpu(fcoe_percpu, cpu);
896 spin_lock_bh(&fps->fcoe_rx_list.lock);
897 if (!fps->thread) {
898 spin_unlock_bh(&fps->fcoe_rx_list.lock);
899 goto err;
900 }
901 }
902
903 /*
904 * We now have a valid CPU that we're targeting for
905 * this skb. We also have this receive thread locked,
906 * so we're free to queue skbs into it's queue.
907 */
Robert Love85b4aa42008-12-09 15:10:24 -0800908 __skb_queue_tail(&fps->fcoe_rx_list, skb);
909 if (fps->fcoe_rx_list.qlen == 1)
910 wake_up_process(fps->thread);
911
912 spin_unlock_bh(&fps->fcoe_rx_list.lock);
913
914 return 0;
915err:
Robert Love582b45b2009-03-31 15:51:50 -0700916 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800917
918err2:
919 kfree_skb(skb);
920 return -1;
921}
Robert Love85b4aa42008-12-09 15:10:24 -0800922
923/**
Robert Love34f42a02009-02-27 10:55:45 -0800924 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800925 * @skb: the skb to be xmitted
926 *
927 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800928 */
Robert Love85b4aa42008-12-09 15:10:24 -0800929static inline int fcoe_start_io(struct sk_buff *skb)
930{
931 int rc;
932
933 skb_get(skb);
934 rc = dev_queue_xmit(skb);
935 if (rc != 0)
936 return rc;
937 kfree_skb(skb);
938 return 0;
939}
940
941/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700942 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800943 * @skb: the skb to be xmitted
944 * @tlen: total len
945 *
946 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800947 */
Robert Love85b4aa42008-12-09 15:10:24 -0800948static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
949{
950 struct fcoe_percpu_s *fps;
951 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800952
Robert Love5e5e92d2009-03-17 11:41:35 -0700953 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800954 page = fps->crc_eof_page;
955 if (!page) {
956 page = alloc_page(GFP_ATOMIC);
957 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700958 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800959 return -ENOMEM;
960 }
961 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700962 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800963 }
964
965 get_page(page);
966 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
967 fps->crc_eof_offset, tlen);
968 skb->len += tlen;
969 skb->data_len += tlen;
970 skb->truesize += tlen;
971 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
972
973 if (fps->crc_eof_offset >= PAGE_SIZE) {
974 fps->crc_eof_page = NULL;
975 fps->crc_eof_offset = 0;
976 put_page(page);
977 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700978 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800979 return 0;
980}
981
982/**
Robert Love34f42a02009-02-27 10:55:45 -0800983 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -0700984 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -0800985 *
986 * This uses crc32() to calculate the crc for fc frame
987 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800988 */
Robert Love85b4aa42008-12-09 15:10:24 -0800989u32 fcoe_fc_crc(struct fc_frame *fp)
990{
991 struct sk_buff *skb = fp_skb(fp);
992 struct skb_frag_struct *frag;
993 unsigned char *data;
994 unsigned long off, len, clen;
995 u32 crc;
996 unsigned i;
997
998 crc = crc32(~0, skb->data, skb_headlen(skb));
999
1000 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1001 frag = &skb_shinfo(skb)->frags[i];
1002 off = frag->page_offset;
1003 len = frag->size;
1004 while (len > 0) {
1005 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1006 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1007 KM_SKB_DATA_SOFTIRQ);
1008 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1009 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1010 off += clen;
1011 len -= clen;
1012 }
1013 }
1014 return crc;
1015}
Robert Love85b4aa42008-12-09 15:10:24 -08001016
1017/**
Robert Love34f42a02009-02-27 10:55:45 -08001018 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001019 * @lp: the associated local port
1020 * @fp: the fc_frame to be transmitted
1021 *
1022 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001023 */
Robert Love85b4aa42008-12-09 15:10:24 -08001024int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1025{
1026 int wlen, rc = 0;
1027 u32 crc;
1028 struct ethhdr *eh;
1029 struct fcoe_crc_eof *cp;
1030 struct sk_buff *skb;
1031 struct fcoe_dev_stats *stats;
1032 struct fc_frame_header *fh;
1033 unsigned int hlen; /* header length implies the version */
1034 unsigned int tlen; /* trailer length */
1035 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001036 struct fcoe_softc *fc;
1037 u8 sof, eof;
1038 struct fcoe_hdr *hp;
1039
1040 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1041
Robert Lovefc47ff62009-02-27 10:55:55 -08001042 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001043 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001044 skb = fp_skb(fp);
1045 wlen = skb->len / FCOE_WORD_TO_BYTE;
1046
1047 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001048 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001049 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001050 }
1051
Joe Eykholt97c83892009-03-17 11:42:40 -07001052 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1053 fcoe_ctlr_els_send(&fc->ctlr, skb))
1054 return 0;
1055
Robert Love85b4aa42008-12-09 15:10:24 -08001056 sof = fr_sof(fp);
1057 eof = fr_eof(fp);
1058
1059 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
1060 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
1061 hlen = sizeof(struct fcoe_hdr);
1062 tlen = sizeof(struct fcoe_crc_eof);
1063 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1064
1065 /* crc offload */
1066 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001067 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001068 skb->csum_start = skb_headroom(skb);
1069 skb->csum_offset = skb->len;
1070 crc = 0;
1071 } else {
1072 skb->ip_summed = CHECKSUM_NONE;
1073 crc = fcoe_fc_crc(fp);
1074 }
1075
1076 /* copy fc crc and eof to the skb buff */
1077 if (skb_is_nonlinear(skb)) {
1078 skb_frag_t *frag;
1079 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001080 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001081 return -ENOMEM;
1082 }
1083 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1084 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1085 + frag->page_offset;
1086 } else {
1087 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1088 }
1089
1090 memset(cp, 0, sizeof(*cp));
1091 cp->fcoe_eof = eof;
1092 cp->fcoe_crc32 = cpu_to_le32(~crc);
1093
1094 if (skb_is_nonlinear(skb)) {
1095 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1096 cp = NULL;
1097 }
1098
Chris Leechdd3fd722009-04-21 16:27:36 -07001099 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001100 skb_push(skb, elen + hlen);
1101 skb_reset_mac_header(skb);
1102 skb_reset_network_header(skb);
1103 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001104 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001105 skb->dev = fc->real_dev;
1106
1107 /* fill up mac and fcoe headers */
1108 eh = eth_hdr(skb);
1109 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001110 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001111 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1112 else
1113 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001114 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001115
Joe Eykholt97c83892009-03-17 11:42:40 -07001116 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1117 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001118 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001119 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001120
1121 hp = (struct fcoe_hdr *)(eh + 1);
1122 memset(hp, 0, sizeof(*hp));
1123 if (FC_FCOE_VER)
1124 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1125 hp->fcoe_sof = sof;
1126
Yi Zou39ca9a02009-02-27 14:07:15 -08001127#ifdef NETIF_F_FSO
1128 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1129 if (lp->seq_offload && fr_max_payload(fp)) {
1130 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1131 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1132 } else {
1133 skb_shinfo(skb)->gso_type = 0;
1134 skb_shinfo(skb)->gso_size = 0;
1135 }
1136#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001137 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001138 stats = fc_lport_get_stats(lp);
1139 stats->TxFrames++;
1140 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001141
1142 /* send down to lld */
1143 fr_dev(fp) = lp;
1144 if (fc->fcoe_pending_queue.qlen)
1145 rc = fcoe_check_wait_queue(lp);
1146
1147 if (rc == 0)
1148 rc = fcoe_start_io(skb);
1149
1150 if (rc) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001151 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1152 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1153 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001154 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -08001155 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001156 }
1157
1158 return 0;
1159}
Robert Love85b4aa42008-12-09 15:10:24 -08001160
Robert Love34f42a02009-02-27 10:55:45 -08001161/**
1162 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001163 * @arg: ptr to the fcoe per cpu struct
1164 *
1165 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001166 */
1167int fcoe_percpu_receive_thread(void *arg)
1168{
1169 struct fcoe_percpu_s *p = arg;
1170 u32 fr_len;
1171 struct fc_lport *lp;
1172 struct fcoe_rcv_info *fr;
1173 struct fcoe_dev_stats *stats;
1174 struct fc_frame_header *fh;
1175 struct sk_buff *skb;
1176 struct fcoe_crc_eof crc_eof;
1177 struct fc_frame *fp;
1178 u8 *mac = NULL;
1179 struct fcoe_softc *fc;
1180 struct fcoe_hdr *hp;
1181
Robert Love4469c192009-02-27 10:56:38 -08001182 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001183
1184 while (!kthread_should_stop()) {
1185
1186 spin_lock_bh(&p->fcoe_rx_list.lock);
1187 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1188 set_current_state(TASK_INTERRUPTIBLE);
1189 spin_unlock_bh(&p->fcoe_rx_list.lock);
1190 schedule();
1191 set_current_state(TASK_RUNNING);
1192 if (kthread_should_stop())
1193 return 0;
1194 spin_lock_bh(&p->fcoe_rx_list.lock);
1195 }
1196 spin_unlock_bh(&p->fcoe_rx_list.lock);
1197 fr = fcoe_dev_from_skb(skb);
1198 lp = fr->fr_dev;
1199 if (unlikely(lp == NULL)) {
1200 FC_DBG("invalid HBA Structure");
1201 kfree_skb(skb);
1202 continue;
1203 }
1204
Robert Love85b4aa42008-12-09 15:10:24 -08001205 if (unlikely(debug_fcoe)) {
1206 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1207 "tail:%p end:%p sum:%d dev:%s",
1208 skb->len, skb->data_len,
1209 skb->head, skb->data, skb_tail_pointer(skb),
1210 skb_end_pointer(skb), skb->csum,
1211 skb->dev ? skb->dev->name : "<NULL>");
1212 }
1213
1214 /*
1215 * Save source MAC address before discarding header.
1216 */
1217 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001218 if (skb_is_nonlinear(skb))
1219 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001220 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001221
1222 /*
1223 * Frame length checks and setting up the header pointers
1224 * was done in fcoe_rcv already.
1225 */
1226 hp = (struct fcoe_hdr *) skb_network_header(skb);
1227 fh = (struct fc_frame_header *) skb_transport_header(skb);
1228
Robert Love582b45b2009-03-31 15:51:50 -07001229 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001230 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001231 if (stats->ErrorFrames < 5)
1232 printk(KERN_WARNING "FCoE version "
1233 "mismatch: The frame has "
1234 "version %x, but the "
1235 "initiator supports version "
1236 "%x\n", FC_FCOE_DECAPS_VER(hp),
1237 FC_FCOE_VER);
1238 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001239 kfree_skb(skb);
1240 continue;
1241 }
1242
1243 skb_pull(skb, sizeof(struct fcoe_hdr));
1244 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1245
Robert Love582b45b2009-03-31 15:51:50 -07001246 stats->RxFrames++;
1247 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001248
1249 fp = (struct fc_frame *)skb;
1250 fc_frame_init(fp);
1251 fr_dev(fp) = lp;
1252 fr_sof(fp) = hp->fcoe_sof;
1253
1254 /* Copy out the CRC and EOF trailer for access */
1255 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1256 kfree_skb(skb);
1257 continue;
1258 }
1259 fr_eof(fp) = crc_eof.fcoe_eof;
1260 fr_crc(fp) = crc_eof.fcoe_crc32;
1261 if (pskb_trim(skb, fr_len)) {
1262 kfree_skb(skb);
1263 continue;
1264 }
1265
1266 /*
1267 * We only check CRC if no offload is available and if it is
1268 * it's solicited data, in which case, the FCP layer would
1269 * check it during the copy.
1270 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001271 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001272 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1273 else
1274 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1275
1276 fh = fc_frame_header_get(fp);
1277 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1278 fh->fh_type == FC_TYPE_FCP) {
1279 fc_exch_recv(lp, lp->emp, fp);
1280 continue;
1281 }
1282 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1283 if (le32_to_cpu(fr_crc(fp)) !=
1284 ~crc32(~0, skb->data, fr_len)) {
1285 if (debug_fcoe || stats->InvalidCRCCount < 5)
1286 printk(KERN_WARNING "fcoe: dropping "
1287 "frame with CRC error\n");
1288 stats->InvalidCRCCount++;
1289 stats->ErrorFrames++;
1290 fc_frame_free(fp);
1291 continue;
1292 }
1293 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1294 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001295 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1296 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1297 fc_frame_free(fp);
1298 continue;
1299 }
Robert Love85b4aa42008-12-09 15:10:24 -08001300 fc_exch_recv(lp, lp->emp, fp);
1301 }
1302 return 0;
1303}
1304
1305/**
Robert Love34f42a02009-02-27 10:55:45 -08001306 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -08001307 * @vp:
1308 *
Vasu Devbc0e17f2009-02-27 10:54:57 -08001309 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -08001310 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1311 * fcoe_hostlist.
1312 *
1313 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001314 */
Robert Love85b4aa42008-12-09 15:10:24 -08001315void fcoe_watchdog(ulong vp)
1316{
Robert Love85b4aa42008-12-09 15:10:24 -08001317 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001318
1319 read_lock(&fcoe_hostlist_lock);
1320 list_for_each_entry(fc, &fcoe_hostlist, list) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001321 if (fc->ctlr.lp)
1322 fcoe_check_wait_queue(fc->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001323 }
1324 read_unlock(&fcoe_hostlist_lock);
1325
1326 fcoe_timer.expires = jiffies + (1 * HZ);
1327 add_timer(&fcoe_timer);
1328}
1329
1330
1331/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001332 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1333 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001334 *
1335 * This empties the wait_queue, dequeue the head of the wait_queue queue
1336 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001337 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001338 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001339 *
1340 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001341 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001342 * by the next skb transmit.
1343 *
1344 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001345 */
Robert Love85b4aa42008-12-09 15:10:24 -08001346static int fcoe_check_wait_queue(struct fc_lport *lp)
1347{
Chris Leech55c8baf2009-02-27 10:56:32 -08001348 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001349 struct sk_buff *skb;
Vasu Devc826a312009-02-27 10:56:27 -08001350 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001351
Robert Love85b4aa42008-12-09 15:10:24 -08001352 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001353 if (fc->fcoe_pending_queue_active)
1354 goto out;
1355 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001356
1357 while (fc->fcoe_pending_queue.qlen) {
1358 /* keep qlen > 0 until fcoe_start_io succeeds */
1359 fc->fcoe_pending_queue.qlen++;
1360 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1361
1362 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1363 rc = fcoe_start_io(skb);
1364 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1365
1366 if (rc) {
1367 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1368 /* undo temporary increment above */
1369 fc->fcoe_pending_queue.qlen--;
1370 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001371 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001372 /* undo temporary increment above */
1373 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001374 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001375
1376 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1377 lp->qfull = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001378 fc->fcoe_pending_queue_active = 0;
1379 rc = fc->fcoe_pending_queue.qlen;
1380out:
Robert Love85b4aa42008-12-09 15:10:24 -08001381 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001382 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001383}
1384
1385/**
Robert Love34f42a02009-02-27 10:55:45 -08001386 * fcoe_dev_setup() - setup link change notification interface
1387 */
1388static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -08001389{
Robert Love85b4aa42008-12-09 15:10:24 -08001390 register_netdevice_notifier(&fcoe_notifier);
1391}
1392
1393/**
Robert Love34f42a02009-02-27 10:55:45 -08001394 * fcoe_dev_setup() - cleanup link change notification interface
1395 */
Robert Love85b4aa42008-12-09 15:10:24 -08001396static void fcoe_dev_cleanup(void)
1397{
1398 unregister_netdevice_notifier(&fcoe_notifier);
1399}
1400
1401/**
Robert Love34f42a02009-02-27 10:55:45 -08001402 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001403 * @notifier: context of the notification
1404 * @event: type of event
1405 * @ptr: fixed array for output parsed ifname
1406 *
1407 * This function is called by the ethernet driver in case of link change event
1408 *
1409 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001410 */
Robert Love85b4aa42008-12-09 15:10:24 -08001411static int fcoe_device_notification(struct notifier_block *notifier,
1412 ulong event, void *ptr)
1413{
1414 struct fc_lport *lp = NULL;
1415 struct net_device *real_dev = ptr;
1416 struct fcoe_softc *fc;
1417 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001418 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001419 u32 mfs;
1420 int rc = NOTIFY_OK;
1421
1422 read_lock(&fcoe_hostlist_lock);
1423 list_for_each_entry(fc, &fcoe_hostlist, list) {
1424 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001425 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001426 break;
1427 }
1428 }
1429 read_unlock(&fcoe_hostlist_lock);
1430 if (lp == NULL) {
1431 rc = NOTIFY_DONE;
1432 goto out;
1433 }
1434
Robert Love85b4aa42008-12-09 15:10:24 -08001435 switch (event) {
1436 case NETDEV_DOWN:
1437 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001438 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001439 break;
1440 case NETDEV_UP:
1441 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001442 break;
1443 case NETDEV_CHANGEMTU:
1444 mfs = fc->real_dev->mtu -
1445 (sizeof(struct fcoe_hdr) +
1446 sizeof(struct fcoe_crc_eof));
1447 if (mfs >= FC_MIN_MAX_FRAME)
1448 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001449 break;
1450 case NETDEV_REGISTER:
1451 break;
1452 default:
Joe Eykholt97c83892009-03-17 11:42:40 -07001453 FC_DBG("Unknown event %ld from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001454 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001455 if (link_possible && !fcoe_link_ok(lp))
1456 fcoe_ctlr_link_up(&fc->ctlr);
1457 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1458 stats = fc_lport_get_stats(lp);
1459 stats->LinkFailureCount++;
1460 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001461 }
1462out:
1463 return rc;
1464}
1465
1466/**
Robert Love34f42a02009-02-27 10:55:45 -08001467 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001468 * @buffer: incoming buffer to be copied
1469 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001470 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001471 */
Robert Love85b4aa42008-12-09 15:10:24 -08001472static struct net_device *fcoe_if_to_netdev(const char *buffer)
1473{
1474 char *cp;
1475 char ifname[IFNAMSIZ + 2];
1476
1477 if (buffer) {
1478 strlcpy(ifname, buffer, IFNAMSIZ);
1479 cp = ifname + strlen(ifname);
1480 while (--cp >= ifname && *cp == '\n')
1481 *cp = '\0';
1482 return dev_get_by_name(&init_net, ifname);
1483 }
1484 return NULL;
1485}
1486
1487/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001488 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001489 * @netdev: the target netdev
1490 *
1491 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001492 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001493static struct module *
1494fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001495{
1496 struct device *dev;
1497
1498 if (!netdev)
1499 return NULL;
1500
1501 dev = netdev->dev.parent;
1502 if (!dev)
1503 return NULL;
1504
1505 if (!dev->driver)
1506 return NULL;
1507
1508 return dev->driver->owner;
1509}
1510
1511/**
Robert Love34f42a02009-02-27 10:55:45 -08001512 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001513 * @netdev: the target netdev
1514 *
Robert Love34f42a02009-02-27 10:55:45 -08001515 * Holds the Ethernet driver module by try_module_get() for
1516 * the corresponding netdev.
1517 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001518 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001519 */
Robert Love85b4aa42008-12-09 15:10:24 -08001520static int fcoe_ethdrv_get(const struct net_device *netdev)
1521{
1522 struct module *owner;
1523
1524 owner = fcoe_netdev_to_module_owner(netdev);
1525 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001526 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1527 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001528 return try_module_get(owner);
1529 }
1530 return -ENODEV;
1531}
1532
1533/**
Robert Love34f42a02009-02-27 10:55:45 -08001534 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001535 * @netdev: the target netdev
1536 *
Robert Love34f42a02009-02-27 10:55:45 -08001537 * Releases the Ethernet driver module by module_put for
1538 * the corresponding netdev.
1539 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001540 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001541 */
Robert Love85b4aa42008-12-09 15:10:24 -08001542static int fcoe_ethdrv_put(const struct net_device *netdev)
1543{
1544 struct module *owner;
1545
1546 owner = fcoe_netdev_to_module_owner(netdev);
1547 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001548 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1549 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001550 module_put(owner);
1551 return 0;
1552 }
1553 return -ENODEV;
1554}
1555
1556/**
Robert Love34f42a02009-02-27 10:55:45 -08001557 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001558 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001559 * @kp: associated kernel param
1560 *
1561 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001562 */
Robert Love85b4aa42008-12-09 15:10:24 -08001563static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1564{
1565 int rc;
1566 struct net_device *netdev;
1567
1568 netdev = fcoe_if_to_netdev(buffer);
1569 if (!netdev) {
1570 rc = -ENODEV;
1571 goto out_nodev;
1572 }
1573 /* look for existing lport */
1574 if (!fcoe_hostlist_lookup(netdev)) {
1575 rc = -ENODEV;
1576 goto out_putdev;
1577 }
Vasu Dev7f349142009-03-27 09:06:31 -07001578 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001579 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001580 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001581 netdev->name);
1582 rc = -EIO;
1583 goto out_putdev;
1584 }
1585 fcoe_ethdrv_put(netdev);
1586 rc = 0;
1587out_putdev:
1588 dev_put(netdev);
1589out_nodev:
1590 return rc;
1591}
1592
1593/**
Robert Love34f42a02009-02-27 10:55:45 -08001594 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001595 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001596 * @kp: associated kernel param
1597 *
1598 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001599 */
Robert Love85b4aa42008-12-09 15:10:24 -08001600static int fcoe_create(const char *buffer, struct kernel_param *kp)
1601{
1602 int rc;
1603 struct net_device *netdev;
1604
1605 netdev = fcoe_if_to_netdev(buffer);
1606 if (!netdev) {
1607 rc = -ENODEV;
1608 goto out_nodev;
1609 }
1610 /* look for existing lport */
1611 if (fcoe_hostlist_lookup(netdev)) {
1612 rc = -EEXIST;
1613 goto out_putdev;
1614 }
1615 fcoe_ethdrv_get(netdev);
1616
Vasu Dev7f349142009-03-27 09:06:31 -07001617 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001618 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001619 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001620 netdev->name);
1621 fcoe_ethdrv_put(netdev);
1622 rc = -EIO;
1623 goto out_putdev;
1624 }
1625 rc = 0;
1626out_putdev:
1627 dev_put(netdev);
1628out_nodev:
1629 return rc;
1630}
1631
1632module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1633__MODULE_PARM_TYPE(create, "string");
1634MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1635module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1636__MODULE_PARM_TYPE(destroy, "string");
1637MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1638
Robert Love34f42a02009-02-27 10:55:45 -08001639/**
1640 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001641 * @lp: ptr to the fc_lport
1642 *
1643 * Any permanently-disqualifying conditions have been previously checked.
1644 * This also updates the speed setting, which may change with link for 100/1000.
1645 *
1646 * This function should probably be checking for PAUSE support at some point
1647 * in the future. Currently Per-priority-pause is not determinable using
1648 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1649 *
1650 * Returns: 0 if link is OK for use by FCoE.
1651 *
1652 */
1653int fcoe_link_ok(struct fc_lport *lp)
1654{
Robert Lovefc47ff62009-02-27 10:55:55 -08001655 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001656 struct net_device *dev = fc->real_dev;
1657 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1658 int rc = 0;
1659
1660 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1661 dev = fc->phys_dev;
1662 if (dev->ethtool_ops->get_settings) {
1663 dev->ethtool_ops->get_settings(dev, &ecmd);
1664 lp->link_supported_speeds &=
1665 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1666 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1667 SUPPORTED_1000baseT_Full))
1668 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1669 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1670 lp->link_supported_speeds |=
1671 FC_PORTSPEED_10GBIT;
1672 if (ecmd.speed == SPEED_1000)
1673 lp->link_speed = FC_PORTSPEED_1GBIT;
1674 if (ecmd.speed == SPEED_10000)
1675 lp->link_speed = FC_PORTSPEED_10GBIT;
1676 }
1677 } else
1678 rc = -1;
1679
1680 return rc;
1681}
Robert Love85b4aa42008-12-09 15:10:24 -08001682
Robert Love34f42a02009-02-27 10:55:45 -08001683/**
1684 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001685 * @lp: the fc_lport
1686 */
1687void fcoe_percpu_clean(struct fc_lport *lp)
1688{
Robert Love85b4aa42008-12-09 15:10:24 -08001689 struct fcoe_percpu_s *pp;
1690 struct fcoe_rcv_info *fr;
1691 struct sk_buff_head *list;
1692 struct sk_buff *skb, *next;
1693 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001694 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001695
Robert Love5e5e92d2009-03-17 11:41:35 -07001696 for_each_possible_cpu(cpu) {
1697 pp = &per_cpu(fcoe_percpu, cpu);
1698 spin_lock_bh(&pp->fcoe_rx_list.lock);
1699 list = &pp->fcoe_rx_list;
1700 head = list->next;
1701 for (skb = head; skb != (struct sk_buff *)list;
1702 skb = next) {
1703 next = skb->next;
1704 fr = fcoe_dev_from_skb(skb);
1705 if (fr->fr_dev == lp) {
1706 __skb_unlink(skb, list);
1707 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001708 }
Robert Love85b4aa42008-12-09 15:10:24 -08001709 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001710 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001711 }
1712}
Robert Love85b4aa42008-12-09 15:10:24 -08001713
1714/**
Robert Love34f42a02009-02-27 10:55:45 -08001715 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001716 * @lp: the corresponding fc_lport
1717 *
1718 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001719 */
Robert Love85b4aa42008-12-09 15:10:24 -08001720void fcoe_clean_pending_queue(struct fc_lport *lp)
1721{
1722 struct fcoe_softc *fc = lport_priv(lp);
1723 struct sk_buff *skb;
1724
1725 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1726 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1727 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1728 kfree_skb(skb);
1729 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1730 }
1731 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1732}
Robert Love85b4aa42008-12-09 15:10:24 -08001733
1734/**
Robert Love34f42a02009-02-27 10:55:45 -08001735 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001736 * @shost: shost the reset is from
1737 *
1738 * Returns: always 0
1739 */
1740int fcoe_reset(struct Scsi_Host *shost)
1741{
1742 struct fc_lport *lport = shost_priv(shost);
1743 fc_lport_reset(lport);
1744 return 0;
1745}
Robert Love85b4aa42008-12-09 15:10:24 -08001746
Robert Love34f42a02009-02-27 10:55:45 -08001747/**
Robert Love34f42a02009-02-27 10:55:45 -08001748 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001749 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001750 *
1751 * Returns: NULL or the located fcoe_softc
1752 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001753static struct fcoe_softc *
1754fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001755{
1756 struct fcoe_softc *fc;
1757
1758 read_lock(&fcoe_hostlist_lock);
1759 list_for_each_entry(fc, &fcoe_hostlist, list) {
1760 if (fc->real_dev == dev) {
1761 read_unlock(&fcoe_hostlist_lock);
1762 return fc;
1763 }
1764 }
1765 read_unlock(&fcoe_hostlist_lock);
1766 return NULL;
1767}
1768
Robert Love34f42a02009-02-27 10:55:45 -08001769/**
1770 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001771 * @netdev: ptr to net_device
1772 *
1773 * Returns: 0 for success
1774 */
1775struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1776{
1777 struct fcoe_softc *fc;
1778
1779 fc = fcoe_hostlist_lookup_softc(netdev);
1780
Joe Eykholt97c83892009-03-17 11:42:40 -07001781 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001782}
Robert Love85b4aa42008-12-09 15:10:24 -08001783
Robert Love34f42a02009-02-27 10:55:45 -08001784/**
1785 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001786 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001787 *
1788 * Returns: 0 for success
1789 */
1790int fcoe_hostlist_add(const struct fc_lport *lp)
1791{
1792 struct fcoe_softc *fc;
1793
1794 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1795 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001796 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001797 write_lock_bh(&fcoe_hostlist_lock);
1798 list_add_tail(&fc->list, &fcoe_hostlist);
1799 write_unlock_bh(&fcoe_hostlist_lock);
1800 }
1801 return 0;
1802}
Robert Love85b4aa42008-12-09 15:10:24 -08001803
Robert Love34f42a02009-02-27 10:55:45 -08001804/**
1805 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001806 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001807 *
1808 * Returns: 0 for success
1809 */
1810int fcoe_hostlist_remove(const struct fc_lport *lp)
1811{
1812 struct fcoe_softc *fc;
1813
1814 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1815 BUG_ON(!fc);
1816 write_lock_bh(&fcoe_hostlist_lock);
1817 list_del(&fc->list);
1818 write_unlock_bh(&fcoe_hostlist_lock);
1819
1820 return 0;
1821}
Robert Love85b4aa42008-12-09 15:10:24 -08001822
1823/**
Robert Love34f42a02009-02-27 10:55:45 -08001824 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001825 *
Robert Love85b4aa42008-12-09 15:10:24 -08001826 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001827 */
Robert Love85b4aa42008-12-09 15:10:24 -08001828static int __init fcoe_init(void)
1829{
Robert Love38eccab2009-03-17 11:41:30 -07001830 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001831 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001832 struct fcoe_percpu_s *p;
1833
Robert Love85b4aa42008-12-09 15:10:24 -08001834 INIT_LIST_HEAD(&fcoe_hostlist);
1835 rwlock_init(&fcoe_hostlist_lock);
1836
Robert Love38eccab2009-03-17 11:41:30 -07001837 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001838 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001839 skb_queue_head_init(&p->fcoe_rx_list);
1840 }
1841
Robert Love8976f422009-03-17 11:41:46 -07001842 for_each_online_cpu(cpu)
1843 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001844
Robert Love8976f422009-03-17 11:41:46 -07001845 /* Initialize per CPU interrupt thread */
1846 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1847 if (rc)
1848 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001849
Robert Love8976f422009-03-17 11:41:46 -07001850 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001851 fcoe_dev_setup();
1852
Robert Lovea468f322009-02-27 10:56:00 -08001853 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1854
1855 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001856
Vasu Dev7f349142009-03-27 09:06:31 -07001857 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001858
1859 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001860
1861out_free:
1862 for_each_online_cpu(cpu) {
1863 fcoe_percpu_thread_destroy(cpu);
1864 }
1865
1866 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001867}
1868module_init(fcoe_init);
1869
1870/**
Robert Love34f42a02009-02-27 10:55:45 -08001871 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001872 *
1873 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001874 */
Robert Love85b4aa42008-12-09 15:10:24 -08001875static void __exit fcoe_exit(void)
1876{
Robert Love5e5e92d2009-03-17 11:41:35 -07001877 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001878 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001879
Robert Love85b4aa42008-12-09 15:10:24 -08001880 fcoe_dev_cleanup();
1881
Robert Love582b45b2009-03-31 15:51:50 -07001882 /* Stop the timer */
Robert Love85b4aa42008-12-09 15:10:24 -08001883 del_timer_sync(&fcoe_timer);
1884
Vasu Dev5919a592009-03-27 09:03:29 -07001885 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001886 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001887 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001888
Robert Love8976f422009-03-17 11:41:46 -07001889 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1890
1891 for_each_online_cpu(cpu) {
1892 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001893 }
1894
Vasu Dev7f349142009-03-27 09:06:31 -07001895 /* detach from scsi transport */
1896 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001897}
1898module_exit(fcoe_exit);