blob: f791348871fc36d25eb750951fa3ec438e578ccc [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))
Jiri Pirkoccffad252009-05-22 23:22:17 +0000185 dev_unicast_delete(fc->real_dev, old);
186 dev_unicast_add(fc->real_dev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000187 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);
Jiri Pirkoccffad252009-05-22 23:22:17 +0000236 dev_unicast_delete(fc->real_dev, flogi_maddr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000237 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
Jiri Pirkoccffad252009-05-22 23:22:17 +0000238 dev_unicast_delete(fc->real_dev, fc->ctlr.data_src_addr);
Vasu Dev184dd342009-05-17 12:33:28 +0000239 if (fc->ctlr.spma)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000240 dev_unicast_delete(fc->real_dev, fc->ctlr.ctl_src_addr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000241 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];
Vasu Dev184dd342009-05-17 12:33:28 +0000260 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700261
262 /* Setup lport private data to point to fcoe softc */
263 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700264 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700265 fc->real_dev = netdev;
266 fc->phys_dev = netdev;
267
268 /* Require support for get_pauseparam ethtool op. */
269 if (netdev->priv_flags & IFF_802_1Q_VLAN)
270 fc->phys_dev = vlan_dev_real_dev(netdev);
271
272 /* Do not support for bonding device */
273 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
274 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
275 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
276 return -EOPNOTSUPP;
277 }
278
279 /*
280 * Determine max frame size based on underlying device and optional
281 * user-configured limit. If the MFS is too low, fcoe_link_ok()
282 * will return 0, so do this first.
283 */
284 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
285 sizeof(struct fcoe_crc_eof));
286 if (fc_set_mfs(lp, mfs))
287 return -EINVAL;
288
Vasu Dev7f349142009-03-27 09:06:31 -0700289 /* offload features support */
290 if (fc->real_dev->features & NETIF_F_SG)
291 lp->sg_supp = 1;
292
293#ifdef NETIF_F_FCOE_CRC
294 if (netdev->features & NETIF_F_FCOE_CRC) {
295 lp->crc_offload = 1;
296 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
297 netdev->name);
298 }
299#endif
300#ifdef NETIF_F_FSO
301 if (netdev->features & NETIF_F_FSO) {
302 lp->seq_offload = 1;
303 lp->lso_max = netdev->gso_max_size;
304 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
305 netdev->name, lp->lso_max);
306 }
307#endif
308 if (netdev->fcoe_ddp_xid) {
309 lp->lro_enabled = 1;
310 lp->lro_xid = netdev->fcoe_ddp_xid;
311 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
312 netdev->name, lp->lro_xid);
313 }
314 skb_queue_head_init(&fc->fcoe_pending_queue);
315 fc->fcoe_pending_queue_active = 0;
316
Vasu Dev184dd342009-05-17 12:33:28 +0000317 /* look for SAN MAC address, if multiple SAN MACs exist, only
318 * use the first one for SPMA */
319 rcu_read_lock();
320 for_each_dev_addr(netdev, ha) {
321 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
322 (is_valid_ether_addr(fc->ctlr.ctl_src_addr))) {
323 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
324 fc->ctlr.spma = 1;
325 break;
326 }
327 }
328 rcu_read_unlock();
329
Vasu Dev7f349142009-03-27 09:06:31 -0700330 /* setup Source Mac Address */
Vasu Dev184dd342009-05-17 12:33:28 +0000331 if (!fc->ctlr.spma)
332 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
333 fc->real_dev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700334
335 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
336 fc_set_wwnn(lp, wwnn);
337 /* XXX - 3rd arg needs to be vlan id */
338 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
339 fc_set_wwpn(lp, wwpn);
340
341 /*
342 * Add FCoE MAC address as second unicast MAC address
343 * or enter promiscuous mode if not capable of listening
344 * for multiple unicast MACs.
345 */
346 rtnl_lock();
347 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoccffad252009-05-22 23:22:17 +0000348 dev_unicast_add(fc->real_dev, flogi_maddr);
Vasu Dev184dd342009-05-17 12:33:28 +0000349 if (fc->ctlr.spma)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000350 dev_unicast_add(fc->real_dev, fc->ctlr.ctl_src_addr);
Joe Eykholt6401bdc2009-04-21 16:27:46 -0700351 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700352 rtnl_unlock();
353
354 /*
355 * setup the receive function from ethernet driver
356 * on the ethertype for the given device
357 */
358 fc->fcoe_packet_type.func = fcoe_rcv;
359 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
360 fc->fcoe_packet_type.dev = fc->real_dev;
361 dev_add_pack(&fc->fcoe_packet_type);
362
Vasu Devab6b85c2009-05-17 12:33:08 +0000363 fc->fip_packet_type.func = fcoe_fip_recv;
364 fc->fip_packet_type.type = htons(ETH_P_FIP);
365 fc->fip_packet_type.dev = fc->real_dev;
366 dev_add_pack(&fc->fip_packet_type);
367
Vasu Dev7f349142009-03-27 09:06:31 -0700368 return 0;
369}
370
371/**
372 * fcoe_shost_config() - Sets up fc_lport->host
373 * @lp : ptr to the fc_lport
374 * @shost : ptr to the associated scsi host
375 * @dev : device associated to scsi host
376 *
377 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
378 *
379 * Returns : 0 for success
380 */
381static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
382 struct device *dev)
383{
384 int rc = 0;
385
386 /* lport scsi host config */
387 lp->host = shost;
388
389 lp->host->max_lun = FCOE_MAX_LUN;
390 lp->host->max_id = FCOE_MAX_FCP_TARGET;
391 lp->host->max_channel = 0;
392 lp->host->transportt = scsi_transport_fcoe_sw;
393
394 /* add the new host to the SCSI-ml */
395 rc = scsi_add_host(lp->host, dev);
396 if (rc) {
397 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
398 return rc;
399 }
400 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
401 FCOE_NAME, FCOE_VERSION,
402 fcoe_netdev(lp)->name);
403
404 return 0;
405}
406
407/**
408 * fcoe_em_config() - allocates em for this lport
409 * @lp: the port that em is to allocated for
410 *
411 * Returns : 0 on success
412 */
413static inline int fcoe_em_config(struct fc_lport *lp)
414{
415 BUG_ON(lp->emp);
416
417 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
418 FCOE_MIN_XID, FCOE_MAX_XID);
419 if (!lp->emp)
420 return -ENOMEM;
421
422 return 0;
423}
424
425/**
426 * fcoe_if_destroy() - FCoE software HBA tear-down function
427 * @netdev: ptr to the associated net_device
428 *
429 * Returns: 0 if link is OK for use by FCoE.
430 */
431static int fcoe_if_destroy(struct net_device *netdev)
432{
433 struct fc_lport *lp = NULL;
434 struct fcoe_softc *fc;
Vasu Dev7f349142009-03-27 09:06:31 -0700435
436 BUG_ON(!netdev);
437
438 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
439 netdev->name);
440
441 lp = fcoe_hostlist_lookup(netdev);
442 if (!lp)
443 return -ENODEV;
444
445 fc = lport_priv(lp);
446
447 /* Logout of the fabric */
448 fc_fabric_logoff(lp);
449
450 /* Remove the instance from fcoe's list */
451 fcoe_hostlist_remove(lp);
452
Vasu Devab6b85c2009-05-17 12:33:08 +0000453 /* clean up netdev configurations */
454 fcoe_netdev_cleanup(fc);
455
456 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700457 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700458
459 /* Cleanup the fc_lport */
460 fc_lport_destroy(lp);
461 fc_fcp_destroy(lp);
462
463 /* Detach from the scsi-ml */
464 fc_remove_host(lp->host);
465 scsi_remove_host(lp->host);
466
467 /* There are no more rports or I/O, free the EM */
468 if (lp->emp)
469 fc_exch_mgr_free(lp->emp);
470
Chris Leechdd3fd722009-04-21 16:27:36 -0700471 /* Free the per-CPU receive threads */
Vasu Dev7f349142009-03-27 09:06:31 -0700472 fcoe_percpu_clean(lp);
473
474 /* Free existing skbs */
475 fcoe_clean_pending_queue(lp);
476
477 /* Free memory used by statistical counters */
478 fc_lport_free_stats(lp);
479
480 /* Release the net_device and Scsi_Host */
481 dev_put(fc->real_dev);
482 scsi_host_put(lp->host);
483
484 return 0;
485}
486
487/*
488 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
489 * @lp: the corresponding fc_lport
490 * @xid: the exchange id for this ddp transfer
491 * @sgl: the scatterlist describing this transfer
492 * @sgc: number of sg items
493 *
494 * Returns : 0 no ddp
495 */
496static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
497 struct scatterlist *sgl, unsigned int sgc)
498{
499 struct net_device *n = fcoe_netdev(lp);
500
501 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
502 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
503
504 return 0;
505}
506
507/*
508 * fcoe_ddp_done - calls LLD's ddp_done through net_device
509 * @lp: the corresponding fc_lport
510 * @xid: the exchange id for this ddp transfer
511 *
512 * Returns : the length of data that have been completed by ddp
513 */
514static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
515{
516 struct net_device *n = fcoe_netdev(lp);
517
518 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
519 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
520 return 0;
521}
522
523static struct libfc_function_template fcoe_libfc_fcn_templ = {
524 .frame_send = fcoe_xmit,
525 .ddp_setup = fcoe_ddp_setup,
526 .ddp_done = fcoe_ddp_done,
527};
528
529/**
530 * fcoe_if_create() - this function creates the fcoe interface
531 * @netdev: pointer the associated netdevice
532 *
533 * Creates fc_lport struct and scsi_host for lport, configures lport
534 * and starts fabric login.
535 *
536 * Returns : 0 on success
537 */
538static int fcoe_if_create(struct net_device *netdev)
539{
540 int rc;
541 struct fc_lport *lp = NULL;
542 struct fcoe_softc *fc;
543 struct Scsi_Host *shost;
544
545 BUG_ON(!netdev);
546
547 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
548 netdev->name);
549
550 lp = fcoe_hostlist_lookup(netdev);
551 if (lp)
552 return -EEXIST;
553
Vasu Deva0a25da2009-03-17 11:42:29 -0700554 shost = libfc_host_alloc(&fcoe_shost_template,
555 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700556 if (!shost) {
557 FC_DBG("Could not allocate host structure\n");
558 return -ENOMEM;
559 }
560 lp = shost_priv(shost);
561 fc = lport_priv(lp);
562
563 /* configure fc_lport, e.g., em */
564 rc = fcoe_lport_config(lp);
565 if (rc) {
566 FC_DBG("Could not configure lport\n");
567 goto out_host_put;
568 }
569
Joe Eykholt97c83892009-03-17 11:42:40 -0700570 /*
571 * Initialize FIP.
572 */
573 fcoe_ctlr_init(&fc->ctlr);
574 fc->ctlr.send = fcoe_fip_send;
575 fc->ctlr.update_mac = fcoe_update_src_mac;
576
Vasu Devab6b85c2009-05-17 12:33:08 +0000577 /* configure lport network properties */
578 rc = fcoe_netdev_config(lp, netdev);
579 if (rc) {
580 FC_DBG("Could not configure netdev for the interface\n");
581 goto out_netdev_cleanup;
582 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700583
Vasu Dev7f349142009-03-27 09:06:31 -0700584 /* configure lport scsi host properties */
585 rc = fcoe_shost_config(lp, shost, &netdev->dev);
586 if (rc) {
587 FC_DBG("Could not configure shost for lport\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000588 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700589 }
590
591 /* lport exch manager allocation */
592 rc = fcoe_em_config(lp);
593 if (rc) {
594 FC_DBG("Could not configure em for lport\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000595 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700596 }
597
598 /* Initialize the library */
599 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
600 if (rc) {
601 FC_DBG("Could not configure libfc for lport!\n");
602 goto out_lp_destroy;
603 }
604
605 /* add to lports list */
606 fcoe_hostlist_add(lp);
607
608 lp->boot_time = jiffies;
609
610 fc_fabric_login(lp);
611
Joe Eykholt97c83892009-03-17 11:42:40 -0700612 if (!fcoe_link_ok(lp))
613 fcoe_ctlr_link_up(&fc->ctlr);
614
Vasu Dev7f349142009-03-27 09:06:31 -0700615 dev_hold(netdev);
616
617 return rc;
618
619out_lp_destroy:
620 fc_exch_mgr_free(lp->emp); /* Free the EM */
Vasu Devab6b85c2009-05-17 12:33:08 +0000621out_netdev_cleanup:
622 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700623out_host_put:
624 scsi_host_put(lp->host);
625 return rc;
626}
627
628/**
629 * fcoe_if_init() - attach to scsi transport
630 *
631 * Returns : 0 on success
632 */
633static int __init fcoe_if_init(void)
634{
635 /* attach to scsi transport */
636 scsi_transport_fcoe_sw =
637 fc_attach_transport(&fcoe_transport_function);
638
639 if (!scsi_transport_fcoe_sw) {
640 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
641 return -ENODEV;
642 }
643
644 return 0;
645}
646
647/**
648 * fcoe_if_exit() - detach from scsi transport
649 *
650 * Returns : 0 on success
651 */
652int __exit fcoe_if_exit(void)
653{
654 fc_release_transport(scsi_transport_fcoe_sw);
655 return 0;
656}
657
Robert Love85b4aa42008-12-09 15:10:24 -0800658/**
Robert Love8976f422009-03-17 11:41:46 -0700659 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
660 * @cpu: cpu index for the online cpu
661 */
662static void fcoe_percpu_thread_create(unsigned int cpu)
663{
664 struct fcoe_percpu_s *p;
665 struct task_struct *thread;
666
667 p = &per_cpu(fcoe_percpu, cpu);
668
669 thread = kthread_create(fcoe_percpu_receive_thread,
670 (void *)p, "fcoethread/%d", cpu);
671
672 if (likely(!IS_ERR(p->thread))) {
673 kthread_bind(thread, cpu);
674 wake_up_process(thread);
675
676 spin_lock_bh(&p->fcoe_rx_list.lock);
677 p->thread = thread;
678 spin_unlock_bh(&p->fcoe_rx_list.lock);
679 }
680}
681
682/**
683 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
684 * @cpu: cpu index the rx thread is to be removed
685 *
686 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
687 * current CPU's Rx thread. If the thread being destroyed is bound to
688 * the CPU processing this context the skbs will be freed.
689 */
690static void fcoe_percpu_thread_destroy(unsigned int cpu)
691{
692 struct fcoe_percpu_s *p;
693 struct task_struct *thread;
694 struct page *crc_eof;
695 struct sk_buff *skb;
696#ifdef CONFIG_SMP
697 struct fcoe_percpu_s *p0;
698 unsigned targ_cpu = smp_processor_id();
699#endif /* CONFIG_SMP */
700
701 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
702
703 /* Prevent any new skbs from being queued for this CPU. */
704 p = &per_cpu(fcoe_percpu, cpu);
705 spin_lock_bh(&p->fcoe_rx_list.lock);
706 thread = p->thread;
707 p->thread = NULL;
708 crc_eof = p->crc_eof_page;
709 p->crc_eof_page = NULL;
710 p->crc_eof_offset = 0;
711 spin_unlock_bh(&p->fcoe_rx_list.lock);
712
713#ifdef CONFIG_SMP
714 /*
715 * Don't bother moving the skb's if this context is running
716 * on the same CPU that is having its thread destroyed. This
717 * can easily happen when the module is removed.
718 */
719 if (cpu != targ_cpu) {
720 p0 = &per_cpu(fcoe_percpu, targ_cpu);
721 spin_lock_bh(&p0->fcoe_rx_list.lock);
722 if (p0->thread) {
723 FC_DBG("Moving frames from CPU %d to CPU %d\n",
724 cpu, targ_cpu);
725
726 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
727 __skb_queue_tail(&p0->fcoe_rx_list, skb);
728 spin_unlock_bh(&p0->fcoe_rx_list.lock);
729 } else {
730 /*
731 * The targeted CPU is not initialized and cannot accept
732 * new skbs. Unlock the targeted CPU and drop the skbs
733 * on the CPU that is going offline.
734 */
735 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
736 kfree_skb(skb);
737 spin_unlock_bh(&p0->fcoe_rx_list.lock);
738 }
739 } else {
740 /*
741 * This scenario occurs when the module is being removed
742 * and all threads are being destroyed. skbs will continue
743 * to be shifted from the CPU thread that is being removed
744 * to the CPU thread associated with the CPU that is processing
745 * the module removal. Once there is only one CPU Rx thread it
746 * will reach this case and we will drop all skbs and later
747 * stop the thread.
748 */
749 spin_lock_bh(&p->fcoe_rx_list.lock);
750 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
751 kfree_skb(skb);
752 spin_unlock_bh(&p->fcoe_rx_list.lock);
753 }
754#else
755 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700756 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700757 * being removed. Free all skbs and stop the thread.
758 */
759 spin_lock_bh(&p->fcoe_rx_list.lock);
760 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
761 kfree_skb(skb);
762 spin_unlock_bh(&p->fcoe_rx_list.lock);
763#endif
764
765 if (thread)
766 kthread_stop(thread);
767
768 if (crc_eof)
769 put_page(crc_eof);
770}
771
772/**
773 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
774 * @nfb: callback data block
775 * @action: event triggering the callback
776 * @hcpu: index for the cpu of this event
777 *
778 * This creates or destroys per cpu data for fcoe
779 *
780 * Returns NOTIFY_OK always.
781 */
782static int fcoe_cpu_callback(struct notifier_block *nfb,
783 unsigned long action, void *hcpu)
784{
785 unsigned cpu = (unsigned long)hcpu;
786
787 switch (action) {
788 case CPU_ONLINE:
789 case CPU_ONLINE_FROZEN:
790 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
791 fcoe_percpu_thread_create(cpu);
792 break;
793 case CPU_DEAD:
794 case CPU_DEAD_FROZEN:
795 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
796 fcoe_percpu_thread_destroy(cpu);
797 break;
798 default:
799 break;
800 }
801 return NOTIFY_OK;
802}
803
804static struct notifier_block fcoe_cpu_notifier = {
805 .notifier_call = fcoe_cpu_callback,
806};
807
808/**
Robert Love34f42a02009-02-27 10:55:45 -0800809 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800810 * @skb: the receive skb
811 * @dev: associated net device
812 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700813 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800814 *
815 * this function will receive the packet and build fc frame and pass it up
816 *
817 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800818 */
Robert Love85b4aa42008-12-09 15:10:24 -0800819int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
820 struct packet_type *ptype, struct net_device *olddev)
821{
822 struct fc_lport *lp;
823 struct fcoe_rcv_info *fr;
824 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800825 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800826 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700827 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700828 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800829
830 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700831 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800832 if (unlikely(lp == NULL)) {
833 FC_DBG("cannot find hba structure");
834 goto err2;
835 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700836 if (!lp->link_up)
837 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800838
839 if (unlikely(debug_fcoe)) {
840 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
841 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
842 skb->head, skb->data, skb_tail_pointer(skb),
843 skb_end_pointer(skb), skb->csum,
844 skb->dev ? skb->dev->name : "<NULL>");
845
846 }
847
848 /* check for FCOE packet type */
849 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
850 FC_DBG("wrong FC type frame");
851 goto err;
852 }
853
854 /*
855 * Check for minimum frame length, and make sure required FCoE
856 * and FC headers are pulled into the linear data area.
857 */
858 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
859 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
860 goto err;
861
862 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
863 fh = (struct fc_frame_header *) skb_transport_header(skb);
864
865 oxid = ntohs(fh->fh_ox_id);
866
867 fr = fcoe_dev_from_skb(skb);
868 fr->fr_dev = lp;
869 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700870
Robert Love85b4aa42008-12-09 15:10:24 -0800871#ifdef CONFIG_SMP
872 /*
873 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700874 * cpu bits to get cpu and then this cpu is used for selecting
875 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800876 */
Robert Love8976f422009-03-17 11:41:46 -0700877 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800878#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700879
Robert Love8976f422009-03-17 11:41:46 -0700880 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800881 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700882 if (unlikely(!fps->thread)) {
883 /*
884 * The targeted CPU is not ready, let's target
885 * the first CPU now. For non-SMP systems this
886 * will check the same CPU twice.
887 */
888 FC_DBG("CPU is online, but no receive thread ready "
889 "for incoming skb- using first online CPU.\n");
890
891 spin_unlock_bh(&fps->fcoe_rx_list.lock);
892 cpu = first_cpu(cpu_online_map);
893 fps = &per_cpu(fcoe_percpu, cpu);
894 spin_lock_bh(&fps->fcoe_rx_list.lock);
895 if (!fps->thread) {
896 spin_unlock_bh(&fps->fcoe_rx_list.lock);
897 goto err;
898 }
899 }
900
901 /*
902 * We now have a valid CPU that we're targeting for
903 * this skb. We also have this receive thread locked,
904 * so we're free to queue skbs into it's queue.
905 */
Robert Love85b4aa42008-12-09 15:10:24 -0800906 __skb_queue_tail(&fps->fcoe_rx_list, skb);
907 if (fps->fcoe_rx_list.qlen == 1)
908 wake_up_process(fps->thread);
909
910 spin_unlock_bh(&fps->fcoe_rx_list.lock);
911
912 return 0;
913err:
Robert Love582b45b2009-03-31 15:51:50 -0700914 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800915
916err2:
917 kfree_skb(skb);
918 return -1;
919}
Robert Love85b4aa42008-12-09 15:10:24 -0800920
921/**
Robert Love34f42a02009-02-27 10:55:45 -0800922 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800923 * @skb: the skb to be xmitted
924 *
925 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800926 */
Robert Love85b4aa42008-12-09 15:10:24 -0800927static inline int fcoe_start_io(struct sk_buff *skb)
928{
929 int rc;
930
931 skb_get(skb);
932 rc = dev_queue_xmit(skb);
933 if (rc != 0)
934 return rc;
935 kfree_skb(skb);
936 return 0;
937}
938
939/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700940 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800941 * @skb: the skb to be xmitted
942 * @tlen: total len
943 *
944 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800945 */
Robert Love85b4aa42008-12-09 15:10:24 -0800946static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
947{
948 struct fcoe_percpu_s *fps;
949 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800950
Robert Love5e5e92d2009-03-17 11:41:35 -0700951 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800952 page = fps->crc_eof_page;
953 if (!page) {
954 page = alloc_page(GFP_ATOMIC);
955 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700956 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800957 return -ENOMEM;
958 }
959 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700960 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800961 }
962
963 get_page(page);
964 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
965 fps->crc_eof_offset, tlen);
966 skb->len += tlen;
967 skb->data_len += tlen;
968 skb->truesize += tlen;
969 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
970
971 if (fps->crc_eof_offset >= PAGE_SIZE) {
972 fps->crc_eof_page = NULL;
973 fps->crc_eof_offset = 0;
974 put_page(page);
975 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700976 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800977 return 0;
978}
979
980/**
Robert Love34f42a02009-02-27 10:55:45 -0800981 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -0700982 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -0800983 *
984 * This uses crc32() to calculate the crc for fc frame
985 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800986 */
Robert Love85b4aa42008-12-09 15:10:24 -0800987u32 fcoe_fc_crc(struct fc_frame *fp)
988{
989 struct sk_buff *skb = fp_skb(fp);
990 struct skb_frag_struct *frag;
991 unsigned char *data;
992 unsigned long off, len, clen;
993 u32 crc;
994 unsigned i;
995
996 crc = crc32(~0, skb->data, skb_headlen(skb));
997
998 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
999 frag = &skb_shinfo(skb)->frags[i];
1000 off = frag->page_offset;
1001 len = frag->size;
1002 while (len > 0) {
1003 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1004 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1005 KM_SKB_DATA_SOFTIRQ);
1006 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1007 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1008 off += clen;
1009 len -= clen;
1010 }
1011 }
1012 return crc;
1013}
Robert Love85b4aa42008-12-09 15:10:24 -08001014
1015/**
Robert Love34f42a02009-02-27 10:55:45 -08001016 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001017 * @lp: the associated local port
1018 * @fp: the fc_frame to be transmitted
1019 *
1020 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001021 */
Robert Love85b4aa42008-12-09 15:10:24 -08001022int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1023{
1024 int wlen, rc = 0;
1025 u32 crc;
1026 struct ethhdr *eh;
1027 struct fcoe_crc_eof *cp;
1028 struct sk_buff *skb;
1029 struct fcoe_dev_stats *stats;
1030 struct fc_frame_header *fh;
1031 unsigned int hlen; /* header length implies the version */
1032 unsigned int tlen; /* trailer length */
1033 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001034 struct fcoe_softc *fc;
1035 u8 sof, eof;
1036 struct fcoe_hdr *hp;
1037
1038 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1039
Robert Lovefc47ff62009-02-27 10:55:55 -08001040 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001041 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001042 skb = fp_skb(fp);
1043 wlen = skb->len / FCOE_WORD_TO_BYTE;
1044
1045 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001046 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001047 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001048 }
1049
Joe Eykholt97c83892009-03-17 11:42:40 -07001050 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1051 fcoe_ctlr_els_send(&fc->ctlr, skb))
1052 return 0;
1053
Robert Love85b4aa42008-12-09 15:10:24 -08001054 sof = fr_sof(fp);
1055 eof = fr_eof(fp);
1056
1057 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
1058 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
1059 hlen = sizeof(struct fcoe_hdr);
1060 tlen = sizeof(struct fcoe_crc_eof);
1061 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1062
1063 /* crc offload */
1064 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001065 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001066 skb->csum_start = skb_headroom(skb);
1067 skb->csum_offset = skb->len;
1068 crc = 0;
1069 } else {
1070 skb->ip_summed = CHECKSUM_NONE;
1071 crc = fcoe_fc_crc(fp);
1072 }
1073
1074 /* copy fc crc and eof to the skb buff */
1075 if (skb_is_nonlinear(skb)) {
1076 skb_frag_t *frag;
1077 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001078 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001079 return -ENOMEM;
1080 }
1081 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1082 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1083 + frag->page_offset;
1084 } else {
1085 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1086 }
1087
1088 memset(cp, 0, sizeof(*cp));
1089 cp->fcoe_eof = eof;
1090 cp->fcoe_crc32 = cpu_to_le32(~crc);
1091
1092 if (skb_is_nonlinear(skb)) {
1093 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1094 cp = NULL;
1095 }
1096
Chris Leechdd3fd722009-04-21 16:27:36 -07001097 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001098 skb_push(skb, elen + hlen);
1099 skb_reset_mac_header(skb);
1100 skb_reset_network_header(skb);
1101 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001102 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001103 skb->dev = fc->real_dev;
1104
1105 /* fill up mac and fcoe headers */
1106 eh = eth_hdr(skb);
1107 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001108 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001109 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1110 else
1111 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001112 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001113
Joe Eykholt97c83892009-03-17 11:42:40 -07001114 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1115 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001116 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001117 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001118
1119 hp = (struct fcoe_hdr *)(eh + 1);
1120 memset(hp, 0, sizeof(*hp));
1121 if (FC_FCOE_VER)
1122 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1123 hp->fcoe_sof = sof;
1124
Yi Zou39ca9a02009-02-27 14:07:15 -08001125#ifdef NETIF_F_FSO
1126 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1127 if (lp->seq_offload && fr_max_payload(fp)) {
1128 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1129 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1130 } else {
1131 skb_shinfo(skb)->gso_type = 0;
1132 skb_shinfo(skb)->gso_size = 0;
1133 }
1134#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001135 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001136 stats = fc_lport_get_stats(lp);
1137 stats->TxFrames++;
1138 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001139
1140 /* send down to lld */
1141 fr_dev(fp) = lp;
1142 if (fc->fcoe_pending_queue.qlen)
1143 rc = fcoe_check_wait_queue(lp);
1144
1145 if (rc == 0)
1146 rc = fcoe_start_io(skb);
1147
1148 if (rc) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001149 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1150 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1151 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001152 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -08001153 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001154 }
1155
1156 return 0;
1157}
Robert Love85b4aa42008-12-09 15:10:24 -08001158
Robert Love34f42a02009-02-27 10:55:45 -08001159/**
1160 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001161 * @arg: ptr to the fcoe per cpu struct
1162 *
1163 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001164 */
1165int fcoe_percpu_receive_thread(void *arg)
1166{
1167 struct fcoe_percpu_s *p = arg;
1168 u32 fr_len;
1169 struct fc_lport *lp;
1170 struct fcoe_rcv_info *fr;
1171 struct fcoe_dev_stats *stats;
1172 struct fc_frame_header *fh;
1173 struct sk_buff *skb;
1174 struct fcoe_crc_eof crc_eof;
1175 struct fc_frame *fp;
1176 u8 *mac = NULL;
1177 struct fcoe_softc *fc;
1178 struct fcoe_hdr *hp;
1179
Robert Love4469c192009-02-27 10:56:38 -08001180 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001181
1182 while (!kthread_should_stop()) {
1183
1184 spin_lock_bh(&p->fcoe_rx_list.lock);
1185 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1186 set_current_state(TASK_INTERRUPTIBLE);
1187 spin_unlock_bh(&p->fcoe_rx_list.lock);
1188 schedule();
1189 set_current_state(TASK_RUNNING);
1190 if (kthread_should_stop())
1191 return 0;
1192 spin_lock_bh(&p->fcoe_rx_list.lock);
1193 }
1194 spin_unlock_bh(&p->fcoe_rx_list.lock);
1195 fr = fcoe_dev_from_skb(skb);
1196 lp = fr->fr_dev;
1197 if (unlikely(lp == NULL)) {
1198 FC_DBG("invalid HBA Structure");
1199 kfree_skb(skb);
1200 continue;
1201 }
1202
Robert Love85b4aa42008-12-09 15:10:24 -08001203 if (unlikely(debug_fcoe)) {
1204 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1205 "tail:%p end:%p sum:%d dev:%s",
1206 skb->len, skb->data_len,
1207 skb->head, skb->data, skb_tail_pointer(skb),
1208 skb_end_pointer(skb), skb->csum,
1209 skb->dev ? skb->dev->name : "<NULL>");
1210 }
1211
1212 /*
1213 * Save source MAC address before discarding header.
1214 */
1215 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001216 if (skb_is_nonlinear(skb))
1217 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001218 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001219
1220 /*
1221 * Frame length checks and setting up the header pointers
1222 * was done in fcoe_rcv already.
1223 */
1224 hp = (struct fcoe_hdr *) skb_network_header(skb);
1225 fh = (struct fc_frame_header *) skb_transport_header(skb);
1226
Robert Love582b45b2009-03-31 15:51:50 -07001227 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001228 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001229 if (stats->ErrorFrames < 5)
1230 printk(KERN_WARNING "FCoE version "
1231 "mismatch: The frame has "
1232 "version %x, but the "
1233 "initiator supports version "
1234 "%x\n", FC_FCOE_DECAPS_VER(hp),
1235 FC_FCOE_VER);
1236 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001237 kfree_skb(skb);
1238 continue;
1239 }
1240
1241 skb_pull(skb, sizeof(struct fcoe_hdr));
1242 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1243
Robert Love582b45b2009-03-31 15:51:50 -07001244 stats->RxFrames++;
1245 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001246
1247 fp = (struct fc_frame *)skb;
1248 fc_frame_init(fp);
1249 fr_dev(fp) = lp;
1250 fr_sof(fp) = hp->fcoe_sof;
1251
1252 /* Copy out the CRC and EOF trailer for access */
1253 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1254 kfree_skb(skb);
1255 continue;
1256 }
1257 fr_eof(fp) = crc_eof.fcoe_eof;
1258 fr_crc(fp) = crc_eof.fcoe_crc32;
1259 if (pskb_trim(skb, fr_len)) {
1260 kfree_skb(skb);
1261 continue;
1262 }
1263
1264 /*
1265 * We only check CRC if no offload is available and if it is
1266 * it's solicited data, in which case, the FCP layer would
1267 * check it during the copy.
1268 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001269 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001270 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1271 else
1272 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1273
1274 fh = fc_frame_header_get(fp);
1275 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1276 fh->fh_type == FC_TYPE_FCP) {
1277 fc_exch_recv(lp, lp->emp, fp);
1278 continue;
1279 }
1280 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1281 if (le32_to_cpu(fr_crc(fp)) !=
1282 ~crc32(~0, skb->data, fr_len)) {
1283 if (debug_fcoe || stats->InvalidCRCCount < 5)
1284 printk(KERN_WARNING "fcoe: dropping "
1285 "frame with CRC error\n");
1286 stats->InvalidCRCCount++;
1287 stats->ErrorFrames++;
1288 fc_frame_free(fp);
1289 continue;
1290 }
1291 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1292 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001293 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1294 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1295 fc_frame_free(fp);
1296 continue;
1297 }
Robert Love85b4aa42008-12-09 15:10:24 -08001298 fc_exch_recv(lp, lp->emp, fp);
1299 }
1300 return 0;
1301}
1302
1303/**
Robert Love34f42a02009-02-27 10:55:45 -08001304 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -08001305 * @vp:
1306 *
Vasu Devbc0e17f2009-02-27 10:54:57 -08001307 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -08001308 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1309 * fcoe_hostlist.
1310 *
1311 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001312 */
Robert Love85b4aa42008-12-09 15:10:24 -08001313void fcoe_watchdog(ulong vp)
1314{
Robert Love85b4aa42008-12-09 15:10:24 -08001315 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001316
1317 read_lock(&fcoe_hostlist_lock);
1318 list_for_each_entry(fc, &fcoe_hostlist, list) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001319 if (fc->ctlr.lp)
1320 fcoe_check_wait_queue(fc->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001321 }
1322 read_unlock(&fcoe_hostlist_lock);
1323
1324 fcoe_timer.expires = jiffies + (1 * HZ);
1325 add_timer(&fcoe_timer);
1326}
1327
1328
1329/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001330 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1331 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001332 *
1333 * This empties the wait_queue, dequeue the head of the wait_queue queue
1334 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001335 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001336 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001337 *
1338 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001339 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001340 * by the next skb transmit.
1341 *
1342 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001343 */
Robert Love85b4aa42008-12-09 15:10:24 -08001344static int fcoe_check_wait_queue(struct fc_lport *lp)
1345{
Chris Leech55c8baf2009-02-27 10:56:32 -08001346 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001347 struct sk_buff *skb;
Vasu Devc826a312009-02-27 10:56:27 -08001348 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001349
Robert Love85b4aa42008-12-09 15:10:24 -08001350 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001351 if (fc->fcoe_pending_queue_active)
1352 goto out;
1353 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001354
1355 while (fc->fcoe_pending_queue.qlen) {
1356 /* keep qlen > 0 until fcoe_start_io succeeds */
1357 fc->fcoe_pending_queue.qlen++;
1358 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1359
1360 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1361 rc = fcoe_start_io(skb);
1362 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1363
1364 if (rc) {
1365 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1366 /* undo temporary increment above */
1367 fc->fcoe_pending_queue.qlen--;
1368 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001369 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001370 /* undo temporary increment above */
1371 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001372 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001373
1374 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1375 lp->qfull = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001376 fc->fcoe_pending_queue_active = 0;
1377 rc = fc->fcoe_pending_queue.qlen;
1378out:
Robert Love85b4aa42008-12-09 15:10:24 -08001379 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001380 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001381}
1382
1383/**
Robert Love34f42a02009-02-27 10:55:45 -08001384 * fcoe_dev_setup() - setup link change notification interface
1385 */
1386static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -08001387{
Robert Love85b4aa42008-12-09 15:10:24 -08001388 register_netdevice_notifier(&fcoe_notifier);
1389}
1390
1391/**
Robert Love34f42a02009-02-27 10:55:45 -08001392 * fcoe_dev_setup() - cleanup link change notification interface
1393 */
Robert Love85b4aa42008-12-09 15:10:24 -08001394static void fcoe_dev_cleanup(void)
1395{
1396 unregister_netdevice_notifier(&fcoe_notifier);
1397}
1398
1399/**
Robert Love34f42a02009-02-27 10:55:45 -08001400 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001401 * @notifier: context of the notification
1402 * @event: type of event
1403 * @ptr: fixed array for output parsed ifname
1404 *
1405 * This function is called by the ethernet driver in case of link change event
1406 *
1407 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001408 */
Robert Love85b4aa42008-12-09 15:10:24 -08001409static int fcoe_device_notification(struct notifier_block *notifier,
1410 ulong event, void *ptr)
1411{
1412 struct fc_lport *lp = NULL;
1413 struct net_device *real_dev = ptr;
1414 struct fcoe_softc *fc;
1415 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001416 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001417 u32 mfs;
1418 int rc = NOTIFY_OK;
1419
1420 read_lock(&fcoe_hostlist_lock);
1421 list_for_each_entry(fc, &fcoe_hostlist, list) {
1422 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001423 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001424 break;
1425 }
1426 }
1427 read_unlock(&fcoe_hostlist_lock);
1428 if (lp == NULL) {
1429 rc = NOTIFY_DONE;
1430 goto out;
1431 }
1432
Robert Love85b4aa42008-12-09 15:10:24 -08001433 switch (event) {
1434 case NETDEV_DOWN:
1435 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001436 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001437 break;
1438 case NETDEV_UP:
1439 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001440 break;
1441 case NETDEV_CHANGEMTU:
1442 mfs = fc->real_dev->mtu -
1443 (sizeof(struct fcoe_hdr) +
1444 sizeof(struct fcoe_crc_eof));
1445 if (mfs >= FC_MIN_MAX_FRAME)
1446 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001447 break;
1448 case NETDEV_REGISTER:
1449 break;
1450 default:
Joe Eykholt97c83892009-03-17 11:42:40 -07001451 FC_DBG("Unknown event %ld from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001452 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001453 if (link_possible && !fcoe_link_ok(lp))
1454 fcoe_ctlr_link_up(&fc->ctlr);
1455 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1456 stats = fc_lport_get_stats(lp);
1457 stats->LinkFailureCount++;
1458 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001459 }
1460out:
1461 return rc;
1462}
1463
1464/**
Robert Love34f42a02009-02-27 10:55:45 -08001465 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001466 * @buffer: incoming buffer to be copied
1467 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001468 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001469 */
Robert Love85b4aa42008-12-09 15:10:24 -08001470static struct net_device *fcoe_if_to_netdev(const char *buffer)
1471{
1472 char *cp;
1473 char ifname[IFNAMSIZ + 2];
1474
1475 if (buffer) {
1476 strlcpy(ifname, buffer, IFNAMSIZ);
1477 cp = ifname + strlen(ifname);
1478 while (--cp >= ifname && *cp == '\n')
1479 *cp = '\0';
1480 return dev_get_by_name(&init_net, ifname);
1481 }
1482 return NULL;
1483}
1484
1485/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001486 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001487 * @netdev: the target netdev
1488 *
1489 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001490 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001491static struct module *
1492fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001493{
1494 struct device *dev;
1495
1496 if (!netdev)
1497 return NULL;
1498
1499 dev = netdev->dev.parent;
1500 if (!dev)
1501 return NULL;
1502
1503 if (!dev->driver)
1504 return NULL;
1505
1506 return dev->driver->owner;
1507}
1508
1509/**
Robert Love34f42a02009-02-27 10:55:45 -08001510 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001511 * @netdev: the target netdev
1512 *
Robert Love34f42a02009-02-27 10:55:45 -08001513 * Holds the Ethernet driver module by try_module_get() for
1514 * the corresponding netdev.
1515 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001516 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001517 */
Robert Love85b4aa42008-12-09 15:10:24 -08001518static int fcoe_ethdrv_get(const struct net_device *netdev)
1519{
1520 struct module *owner;
1521
1522 owner = fcoe_netdev_to_module_owner(netdev);
1523 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001524 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1525 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001526 return try_module_get(owner);
1527 }
1528 return -ENODEV;
1529}
1530
1531/**
Robert Love34f42a02009-02-27 10:55:45 -08001532 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001533 * @netdev: the target netdev
1534 *
Robert Love34f42a02009-02-27 10:55:45 -08001535 * Releases the Ethernet driver module by module_put for
1536 * the corresponding netdev.
1537 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001538 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001539 */
Robert Love85b4aa42008-12-09 15:10:24 -08001540static int fcoe_ethdrv_put(const struct net_device *netdev)
1541{
1542 struct module *owner;
1543
1544 owner = fcoe_netdev_to_module_owner(netdev);
1545 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001546 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1547 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001548 module_put(owner);
1549 return 0;
1550 }
1551 return -ENODEV;
1552}
1553
1554/**
Robert Love34f42a02009-02-27 10:55:45 -08001555 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001556 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001557 * @kp: associated kernel param
1558 *
1559 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001560 */
Robert Love85b4aa42008-12-09 15:10:24 -08001561static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1562{
1563 int rc;
1564 struct net_device *netdev;
1565
1566 netdev = fcoe_if_to_netdev(buffer);
1567 if (!netdev) {
1568 rc = -ENODEV;
1569 goto out_nodev;
1570 }
1571 /* look for existing lport */
1572 if (!fcoe_hostlist_lookup(netdev)) {
1573 rc = -ENODEV;
1574 goto out_putdev;
1575 }
Vasu Dev7f349142009-03-27 09:06:31 -07001576 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001577 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001578 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001579 netdev->name);
1580 rc = -EIO;
1581 goto out_putdev;
1582 }
1583 fcoe_ethdrv_put(netdev);
1584 rc = 0;
1585out_putdev:
1586 dev_put(netdev);
1587out_nodev:
1588 return rc;
1589}
1590
1591/**
Robert Love34f42a02009-02-27 10:55:45 -08001592 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001593 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001594 * @kp: associated kernel param
1595 *
1596 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001597 */
Robert Love85b4aa42008-12-09 15:10:24 -08001598static int fcoe_create(const char *buffer, struct kernel_param *kp)
1599{
1600 int rc;
1601 struct net_device *netdev;
1602
1603 netdev = fcoe_if_to_netdev(buffer);
1604 if (!netdev) {
1605 rc = -ENODEV;
1606 goto out_nodev;
1607 }
1608 /* look for existing lport */
1609 if (fcoe_hostlist_lookup(netdev)) {
1610 rc = -EEXIST;
1611 goto out_putdev;
1612 }
1613 fcoe_ethdrv_get(netdev);
1614
Vasu Dev7f349142009-03-27 09:06:31 -07001615 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001616 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001617 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001618 netdev->name);
1619 fcoe_ethdrv_put(netdev);
1620 rc = -EIO;
1621 goto out_putdev;
1622 }
1623 rc = 0;
1624out_putdev:
1625 dev_put(netdev);
1626out_nodev:
1627 return rc;
1628}
1629
1630module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1631__MODULE_PARM_TYPE(create, "string");
1632MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1633module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1634__MODULE_PARM_TYPE(destroy, "string");
1635MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1636
Robert Love34f42a02009-02-27 10:55:45 -08001637/**
1638 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001639 * @lp: ptr to the fc_lport
1640 *
1641 * Any permanently-disqualifying conditions have been previously checked.
1642 * This also updates the speed setting, which may change with link for 100/1000.
1643 *
1644 * This function should probably be checking for PAUSE support at some point
1645 * in the future. Currently Per-priority-pause is not determinable using
1646 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1647 *
1648 * Returns: 0 if link is OK for use by FCoE.
1649 *
1650 */
1651int fcoe_link_ok(struct fc_lport *lp)
1652{
Robert Lovefc47ff62009-02-27 10:55:55 -08001653 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001654 struct net_device *dev = fc->real_dev;
1655 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1656 int rc = 0;
1657
1658 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1659 dev = fc->phys_dev;
1660 if (dev->ethtool_ops->get_settings) {
1661 dev->ethtool_ops->get_settings(dev, &ecmd);
1662 lp->link_supported_speeds &=
1663 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1664 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1665 SUPPORTED_1000baseT_Full))
1666 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1667 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1668 lp->link_supported_speeds |=
1669 FC_PORTSPEED_10GBIT;
1670 if (ecmd.speed == SPEED_1000)
1671 lp->link_speed = FC_PORTSPEED_1GBIT;
1672 if (ecmd.speed == SPEED_10000)
1673 lp->link_speed = FC_PORTSPEED_10GBIT;
1674 }
1675 } else
1676 rc = -1;
1677
1678 return rc;
1679}
Robert Love85b4aa42008-12-09 15:10:24 -08001680
Robert Love34f42a02009-02-27 10:55:45 -08001681/**
1682 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001683 * @lp: the fc_lport
1684 */
1685void fcoe_percpu_clean(struct fc_lport *lp)
1686{
Robert Love85b4aa42008-12-09 15:10:24 -08001687 struct fcoe_percpu_s *pp;
1688 struct fcoe_rcv_info *fr;
1689 struct sk_buff_head *list;
1690 struct sk_buff *skb, *next;
1691 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001692 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001693
Robert Love5e5e92d2009-03-17 11:41:35 -07001694 for_each_possible_cpu(cpu) {
1695 pp = &per_cpu(fcoe_percpu, cpu);
1696 spin_lock_bh(&pp->fcoe_rx_list.lock);
1697 list = &pp->fcoe_rx_list;
1698 head = list->next;
1699 for (skb = head; skb != (struct sk_buff *)list;
1700 skb = next) {
1701 next = skb->next;
1702 fr = fcoe_dev_from_skb(skb);
1703 if (fr->fr_dev == lp) {
1704 __skb_unlink(skb, list);
1705 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001706 }
Robert Love85b4aa42008-12-09 15:10:24 -08001707 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001708 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001709 }
1710}
Robert Love85b4aa42008-12-09 15:10:24 -08001711
1712/**
Robert Love34f42a02009-02-27 10:55:45 -08001713 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001714 * @lp: the corresponding fc_lport
1715 *
1716 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001717 */
Robert Love85b4aa42008-12-09 15:10:24 -08001718void fcoe_clean_pending_queue(struct fc_lport *lp)
1719{
1720 struct fcoe_softc *fc = lport_priv(lp);
1721 struct sk_buff *skb;
1722
1723 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1724 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1725 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1726 kfree_skb(skb);
1727 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1728 }
1729 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1730}
Robert Love85b4aa42008-12-09 15:10:24 -08001731
1732/**
Robert Love34f42a02009-02-27 10:55:45 -08001733 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001734 * @shost: shost the reset is from
1735 *
1736 * Returns: always 0
1737 */
1738int fcoe_reset(struct Scsi_Host *shost)
1739{
1740 struct fc_lport *lport = shost_priv(shost);
1741 fc_lport_reset(lport);
1742 return 0;
1743}
Robert Love85b4aa42008-12-09 15:10:24 -08001744
Robert Love34f42a02009-02-27 10:55:45 -08001745/**
Robert Love34f42a02009-02-27 10:55:45 -08001746 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001747 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001748 *
1749 * Returns: NULL or the located fcoe_softc
1750 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001751static struct fcoe_softc *
1752fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001753{
1754 struct fcoe_softc *fc;
1755
1756 read_lock(&fcoe_hostlist_lock);
1757 list_for_each_entry(fc, &fcoe_hostlist, list) {
1758 if (fc->real_dev == dev) {
1759 read_unlock(&fcoe_hostlist_lock);
1760 return fc;
1761 }
1762 }
1763 read_unlock(&fcoe_hostlist_lock);
1764 return NULL;
1765}
1766
Robert Love34f42a02009-02-27 10:55:45 -08001767/**
1768 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001769 * @netdev: ptr to net_device
1770 *
1771 * Returns: 0 for success
1772 */
1773struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1774{
1775 struct fcoe_softc *fc;
1776
1777 fc = fcoe_hostlist_lookup_softc(netdev);
1778
Joe Eykholt97c83892009-03-17 11:42:40 -07001779 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001780}
Robert Love85b4aa42008-12-09 15:10:24 -08001781
Robert Love34f42a02009-02-27 10:55:45 -08001782/**
1783 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001784 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001785 *
1786 * Returns: 0 for success
1787 */
1788int fcoe_hostlist_add(const struct fc_lport *lp)
1789{
1790 struct fcoe_softc *fc;
1791
1792 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1793 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001794 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001795 write_lock_bh(&fcoe_hostlist_lock);
1796 list_add_tail(&fc->list, &fcoe_hostlist);
1797 write_unlock_bh(&fcoe_hostlist_lock);
1798 }
1799 return 0;
1800}
Robert Love85b4aa42008-12-09 15:10:24 -08001801
Robert Love34f42a02009-02-27 10:55:45 -08001802/**
1803 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001804 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001805 *
1806 * Returns: 0 for success
1807 */
1808int fcoe_hostlist_remove(const struct fc_lport *lp)
1809{
1810 struct fcoe_softc *fc;
1811
1812 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1813 BUG_ON(!fc);
1814 write_lock_bh(&fcoe_hostlist_lock);
1815 list_del(&fc->list);
1816 write_unlock_bh(&fcoe_hostlist_lock);
1817
1818 return 0;
1819}
Robert Love85b4aa42008-12-09 15:10:24 -08001820
1821/**
Robert Love34f42a02009-02-27 10:55:45 -08001822 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001823 *
Robert Love85b4aa42008-12-09 15:10:24 -08001824 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001825 */
Robert Love85b4aa42008-12-09 15:10:24 -08001826static int __init fcoe_init(void)
1827{
Robert Love38eccab2009-03-17 11:41:30 -07001828 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001829 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001830 struct fcoe_percpu_s *p;
1831
Robert Love85b4aa42008-12-09 15:10:24 -08001832 INIT_LIST_HEAD(&fcoe_hostlist);
1833 rwlock_init(&fcoe_hostlist_lock);
1834
Robert Love38eccab2009-03-17 11:41:30 -07001835 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001836 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001837 skb_queue_head_init(&p->fcoe_rx_list);
1838 }
1839
Robert Love8976f422009-03-17 11:41:46 -07001840 for_each_online_cpu(cpu)
1841 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001842
Robert Love8976f422009-03-17 11:41:46 -07001843 /* Initialize per CPU interrupt thread */
1844 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1845 if (rc)
1846 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001847
Robert Love8976f422009-03-17 11:41:46 -07001848 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001849 fcoe_dev_setup();
1850
Robert Lovea468f322009-02-27 10:56:00 -08001851 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1852
1853 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001854
Vasu Dev7f349142009-03-27 09:06:31 -07001855 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001856
1857 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001858
1859out_free:
1860 for_each_online_cpu(cpu) {
1861 fcoe_percpu_thread_destroy(cpu);
1862 }
1863
1864 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001865}
1866module_init(fcoe_init);
1867
1868/**
Robert Love34f42a02009-02-27 10:55:45 -08001869 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001870 *
1871 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001872 */
Robert Love85b4aa42008-12-09 15:10:24 -08001873static void __exit fcoe_exit(void)
1874{
Robert Love5e5e92d2009-03-17 11:41:35 -07001875 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001876 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001877
Robert Love85b4aa42008-12-09 15:10:24 -08001878 fcoe_dev_cleanup();
1879
Robert Love582b45b2009-03-31 15:51:50 -07001880 /* Stop the timer */
Robert Love85b4aa42008-12-09 15:10:24 -08001881 del_timer_sync(&fcoe_timer);
1882
Vasu Dev5919a592009-03-27 09:03:29 -07001883 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001884 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001885 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001886
Robert Love8976f422009-03-17 11:41:46 -07001887 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1888
1889 for_each_online_cpu(cpu) {
1890 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001891 }
1892
Vasu Dev7f349142009-03-27 09:06:31 -07001893 /* detach from scsi transport */
1894 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001895}
1896module_exit(fcoe_exit);