blob: 6e7a700f5d5429d22c28a57214e52f37a0d03524 [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);
Robert Love5e5e92d2009-03-17 11:41:35 -070057DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080058
Chris Leechdd3fd722009-04-21 16:27:36 -070059/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070060static int fcoe_reset(struct Scsi_Host *shost);
61static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
62static int fcoe_rcv(struct sk_buff *, struct net_device *,
63 struct packet_type *, struct net_device *);
64static int fcoe_percpu_receive_thread(void *arg);
65static void fcoe_clean_pending_queue(struct fc_lport *lp);
66static void fcoe_percpu_clean(struct fc_lport *lp);
67static int fcoe_link_ok(struct fc_lport *lp);
68
69static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
70static int fcoe_hostlist_add(const struct fc_lport *);
71static int fcoe_hostlist_remove(const struct fc_lport *);
72
Vasu Dev4bb6b512009-05-06 10:52:34 -070073static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080074static int fcoe_device_notification(struct notifier_block *, ulong, void *);
75static void fcoe_dev_setup(void);
76static void fcoe_dev_cleanup(void);
77
78/* notification function from net device */
79static struct notifier_block fcoe_notifier = {
80 .notifier_call = fcoe_device_notification,
81};
82
Vasu Dev7f349142009-03-27 09:06:31 -070083static struct scsi_transport_template *scsi_transport_fcoe_sw;
84
85struct fc_function_template fcoe_transport_function = {
86 .show_host_node_name = 1,
87 .show_host_port_name = 1,
88 .show_host_supported_classes = 1,
89 .show_host_supported_fc4s = 1,
90 .show_host_active_fc4s = 1,
91 .show_host_maxframe_size = 1,
92
93 .show_host_port_id = 1,
94 .show_host_supported_speeds = 1,
95 .get_host_speed = fc_get_host_speed,
96 .show_host_speed = 1,
97 .show_host_port_type = 1,
98 .get_host_port_state = fc_get_host_port_state,
99 .show_host_port_state = 1,
100 .show_host_symbolic_name = 1,
101
102 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
103 .show_rport_maxframe_size = 1,
104 .show_rport_supported_classes = 1,
105
106 .show_host_fabric_name = 1,
107 .show_starget_node_name = 1,
108 .show_starget_port_name = 1,
109 .show_starget_port_id = 1,
110 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
111 .show_rport_dev_loss_tmo = 1,
112 .get_fc_host_stats = fc_get_host_stats,
113 .issue_fc_host_lip = fcoe_reset,
114
115 .terminate_rport_io = fc_rport_terminate_io,
116};
117
118static struct scsi_host_template fcoe_shost_template = {
119 .module = THIS_MODULE,
120 .name = "FCoE Driver",
121 .proc_name = FCOE_NAME,
122 .queuecommand = fc_queuecommand,
123 .eh_abort_handler = fc_eh_abort,
124 .eh_device_reset_handler = fc_eh_device_reset,
125 .eh_host_reset_handler = fc_eh_host_reset,
126 .slave_alloc = fc_slave_alloc,
127 .change_queue_depth = fc_change_queue_depth,
128 .change_queue_type = fc_change_queue_type,
129 .this_id = -1,
130 .cmd_per_lun = 32,
131 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
132 .use_clustering = ENABLE_CLUSTERING,
133 .sg_tablesize = SG_ALL,
134 .max_sectors = 0xffff,
135};
136
137/**
138 * fcoe_lport_config() - sets up the fc_lport
139 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700140 *
141 * Returns: 0 for success
142 */
143static int fcoe_lport_config(struct fc_lport *lp)
144{
145 lp->link_up = 0;
146 lp->qfull = 0;
147 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700148 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700149 lp->e_d_tov = 2 * 1000; /* FC-FS default */
150 lp->r_a_tov = 2 * 2 * 1000;
151 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
152 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
153
154 fc_lport_init_stats(lp);
155
156 /* lport fc_lport related configuration */
157 fc_lport_config(lp);
158
159 /* offload related configuration */
160 lp->crc_offload = 0;
161 lp->seq_offload = 0;
162 lp->lro_enabled = 0;
163 lp->lro_xid = 0;
164 lp->lso_max = 0;
165
166 return 0;
167}
168
169/**
Vasu Dev1047f222009-05-06 10:52:40 -0700170 * fcoe_queue_timer() - fcoe queue timer
171 * @lp: the fc_lport pointer
172 *
173 * Calls fcoe_check_wait_queue on timeout
174 *
175 */
176static void fcoe_queue_timer(ulong lp)
177{
178 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
179}
180
181/**
Vasu Dev7f349142009-03-27 09:06:31 -0700182 * fcoe_netdev_config() - Set up netdev for SW FCoE
183 * @lp : ptr to the fc_lport
184 * @netdev : ptr to the associated netdevice struct
185 *
186 * Must be called after fcoe_lport_config() as it will use lport mutex
187 *
188 * Returns : 0 for success
189 */
190static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
191{
192 u32 mfs;
193 u64 wwnn, wwpn;
194 struct fcoe_softc *fc;
195 u8 flogi_maddr[ETH_ALEN];
196
197 /* Setup lport private data to point to fcoe softc */
198 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700199 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700200 fc->real_dev = netdev;
201 fc->phys_dev = netdev;
202
203 /* Require support for get_pauseparam ethtool op. */
204 if (netdev->priv_flags & IFF_802_1Q_VLAN)
205 fc->phys_dev = vlan_dev_real_dev(netdev);
206
207 /* Do not support for bonding device */
208 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
209 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
210 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
211 return -EOPNOTSUPP;
212 }
213
214 /*
215 * Determine max frame size based on underlying device and optional
216 * user-configured limit. If the MFS is too low, fcoe_link_ok()
217 * will return 0, so do this first.
218 */
219 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
220 sizeof(struct fcoe_crc_eof));
221 if (fc_set_mfs(lp, mfs))
222 return -EINVAL;
223
Vasu Dev7f349142009-03-27 09:06:31 -0700224 /* offload features support */
225 if (fc->real_dev->features & NETIF_F_SG)
226 lp->sg_supp = 1;
227
228#ifdef NETIF_F_FCOE_CRC
229 if (netdev->features & NETIF_F_FCOE_CRC) {
230 lp->crc_offload = 1;
231 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
232 netdev->name);
233 }
234#endif
235#ifdef NETIF_F_FSO
236 if (netdev->features & NETIF_F_FSO) {
237 lp->seq_offload = 1;
238 lp->lso_max = netdev->gso_max_size;
239 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
240 netdev->name, lp->lso_max);
241 }
242#endif
243 if (netdev->fcoe_ddp_xid) {
244 lp->lro_enabled = 1;
245 lp->lro_xid = netdev->fcoe_ddp_xid;
246 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
247 netdev->name, lp->lro_xid);
248 }
249 skb_queue_head_init(&fc->fcoe_pending_queue);
250 fc->fcoe_pending_queue_active = 0;
Vasu Dev1047f222009-05-06 10:52:40 -0700251 setup_timer(&fc->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700252
253 /* setup Source Mac Address */
Joe Eykholt97c83892009-03-17 11:42:40 -0700254 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
Vasu Dev7f349142009-03-27 09:06:31 -0700255 fc->real_dev->addr_len);
256
257 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
258 fc_set_wwnn(lp, wwnn);
259 /* XXX - 3rd arg needs to be vlan id */
260 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
261 fc_set_wwpn(lp, wwpn);
262
263 /*
264 * Add FCoE MAC address as second unicast MAC address
265 * or enter promiscuous mode if not capable of listening
266 * for multiple unicast MACs.
267 */
268 rtnl_lock();
269 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
270 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
Joe Eykholt6401bdc2009-04-21 16:27:46 -0700271 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700272 rtnl_unlock();
273
274 /*
275 * setup the receive function from ethernet driver
276 * on the ethertype for the given device
277 */
278 fc->fcoe_packet_type.func = fcoe_rcv;
279 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
280 fc->fcoe_packet_type.dev = fc->real_dev;
281 dev_add_pack(&fc->fcoe_packet_type);
282
283 return 0;
284}
285
286/**
287 * fcoe_shost_config() - Sets up fc_lport->host
288 * @lp : ptr to the fc_lport
289 * @shost : ptr to the associated scsi host
290 * @dev : device associated to scsi host
291 *
292 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
293 *
294 * Returns : 0 for success
295 */
296static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
297 struct device *dev)
298{
299 int rc = 0;
300
301 /* lport scsi host config */
302 lp->host = shost;
303
304 lp->host->max_lun = FCOE_MAX_LUN;
305 lp->host->max_id = FCOE_MAX_FCP_TARGET;
306 lp->host->max_channel = 0;
307 lp->host->transportt = scsi_transport_fcoe_sw;
308
309 /* add the new host to the SCSI-ml */
310 rc = scsi_add_host(lp->host, dev);
311 if (rc) {
312 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
313 return rc;
314 }
315 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
316 FCOE_NAME, FCOE_VERSION,
317 fcoe_netdev(lp)->name);
318
319 return 0;
320}
321
322/**
323 * fcoe_em_config() - allocates em for this lport
324 * @lp: the port that em is to allocated for
325 *
326 * Returns : 0 on success
327 */
328static inline int fcoe_em_config(struct fc_lport *lp)
329{
330 BUG_ON(lp->emp);
331
332 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
333 FCOE_MIN_XID, FCOE_MAX_XID);
334 if (!lp->emp)
335 return -ENOMEM;
336
337 return 0;
338}
339
340/**
341 * fcoe_if_destroy() - FCoE software HBA tear-down function
342 * @netdev: ptr to the associated net_device
343 *
344 * Returns: 0 if link is OK for use by FCoE.
345 */
346static int fcoe_if_destroy(struct net_device *netdev)
347{
348 struct fc_lport *lp = NULL;
349 struct fcoe_softc *fc;
350 u8 flogi_maddr[ETH_ALEN];
351
352 BUG_ON(!netdev);
353
354 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
355 netdev->name);
356
357 lp = fcoe_hostlist_lookup(netdev);
358 if (!lp)
359 return -ENODEV;
360
361 fc = lport_priv(lp);
362
363 /* Logout of the fabric */
364 fc_fabric_logoff(lp);
365
366 /* Remove the instance from fcoe's list */
367 fcoe_hostlist_remove(lp);
368
369 /* Don't listen for Ethernet packets anymore */
370 dev_remove_pack(&fc->fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700371 dev_remove_pack(&fc->fip_packet_type);
372 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700373
374 /* Cleanup the fc_lport */
375 fc_lport_destroy(lp);
376 fc_fcp_destroy(lp);
377
378 /* Detach from the scsi-ml */
379 fc_remove_host(lp->host);
380 scsi_remove_host(lp->host);
381
382 /* There are no more rports or I/O, free the EM */
383 if (lp->emp)
384 fc_exch_mgr_free(lp->emp);
385
386 /* Delete secondary MAC addresses */
387 rtnl_lock();
388 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
389 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700390 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
391 dev_unicast_delete(fc->real_dev,
392 fc->ctlr.data_src_addr, ETH_ALEN);
393 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700394 rtnl_unlock();
395
Chris Leechdd3fd722009-04-21 16:27:36 -0700396 /* Free the per-CPU receive threads */
Vasu Dev7f349142009-03-27 09:06:31 -0700397 fcoe_percpu_clean(lp);
398
399 /* Free existing skbs */
400 fcoe_clean_pending_queue(lp);
401
Vasu Dev1047f222009-05-06 10:52:40 -0700402 /* Stop the timer */
403 del_timer_sync(&fc->timer);
404
Vasu Dev7f349142009-03-27 09:06:31 -0700405 /* Free memory used by statistical counters */
406 fc_lport_free_stats(lp);
407
408 /* Release the net_device and Scsi_Host */
409 dev_put(fc->real_dev);
410 scsi_host_put(lp->host);
411
412 return 0;
413}
414
415/*
416 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
417 * @lp: the corresponding fc_lport
418 * @xid: the exchange id for this ddp transfer
419 * @sgl: the scatterlist describing this transfer
420 * @sgc: number of sg items
421 *
422 * Returns : 0 no ddp
423 */
424static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
425 struct scatterlist *sgl, unsigned int sgc)
426{
427 struct net_device *n = fcoe_netdev(lp);
428
429 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
430 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
431
432 return 0;
433}
434
435/*
436 * fcoe_ddp_done - calls LLD's ddp_done through net_device
437 * @lp: the corresponding fc_lport
438 * @xid: the exchange id for this ddp transfer
439 *
440 * Returns : the length of data that have been completed by ddp
441 */
442static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
443{
444 struct net_device *n = fcoe_netdev(lp);
445
446 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
447 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
448 return 0;
449}
450
451static struct libfc_function_template fcoe_libfc_fcn_templ = {
452 .frame_send = fcoe_xmit,
453 .ddp_setup = fcoe_ddp_setup,
454 .ddp_done = fcoe_ddp_done,
455};
456
457/**
Joe Eykholt97c83892009-03-17 11:42:40 -0700458 * fcoe_fip_recv - handle a received FIP frame.
459 * @skb: the receive skb
460 * @dev: associated &net_device
461 * @ptype: the &packet_type structure which was used to register this handler.
462 * @orig_dev: original receive &net_device, in case @dev is a bond.
463 *
464 * Returns: 0 for success
465 */
466static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
467 struct packet_type *ptype,
468 struct net_device *orig_dev)
469{
470 struct fcoe_softc *fc;
471
472 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
473 fcoe_ctlr_recv(&fc->ctlr, skb);
474 return 0;
475}
476
477/**
478 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
479 * @fip: FCoE controller.
480 * @skb: FIP Packet.
481 */
482static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
483{
484 skb->dev = fcoe_from_ctlr(fip)->real_dev;
485 dev_queue_xmit(skb);
486}
487
488/**
489 * fcoe_update_src_mac() - Update Ethernet MAC filters.
490 * @fip: FCoE controller.
491 * @old: Unicast MAC address to delete if the MAC is non-zero.
492 * @new: Unicast MAC address to add.
493 *
494 * Remove any previously-set unicast MAC filter.
495 * Add secondary FCoE MAC address filter for our OUI.
496 */
497static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
498{
499 struct fcoe_softc *fc;
500
501 fc = fcoe_from_ctlr(fip);
502 rtnl_lock();
503 if (!is_zero_ether_addr(old))
504 dev_unicast_delete(fc->real_dev, old, ETH_ALEN);
505 dev_unicast_add(fc->real_dev, new, ETH_ALEN);
506 rtnl_unlock();
507}
508
509/**
Vasu Dev7f349142009-03-27 09:06:31 -0700510 * fcoe_if_create() - this function creates the fcoe interface
511 * @netdev: pointer the associated netdevice
512 *
513 * Creates fc_lport struct and scsi_host for lport, configures lport
514 * and starts fabric login.
515 *
516 * Returns : 0 on success
517 */
518static int fcoe_if_create(struct net_device *netdev)
519{
520 int rc;
521 struct fc_lport *lp = NULL;
522 struct fcoe_softc *fc;
523 struct Scsi_Host *shost;
524
525 BUG_ON(!netdev);
526
527 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
528 netdev->name);
529
530 lp = fcoe_hostlist_lookup(netdev);
531 if (lp)
532 return -EEXIST;
533
Vasu Deva0a25da2009-03-17 11:42:29 -0700534 shost = libfc_host_alloc(&fcoe_shost_template,
535 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700536 if (!shost) {
537 FC_DBG("Could not allocate host structure\n");
538 return -ENOMEM;
539 }
540 lp = shost_priv(shost);
541 fc = lport_priv(lp);
542
543 /* configure fc_lport, e.g., em */
544 rc = fcoe_lport_config(lp);
545 if (rc) {
546 FC_DBG("Could not configure lport\n");
547 goto out_host_put;
548 }
549
550 /* configure lport network properties */
551 rc = fcoe_netdev_config(lp, netdev);
552 if (rc) {
553 FC_DBG("Could not configure netdev for lport\n");
554 goto out_host_put;
555 }
556
Joe Eykholt97c83892009-03-17 11:42:40 -0700557 /*
558 * Initialize FIP.
559 */
560 fcoe_ctlr_init(&fc->ctlr);
561 fc->ctlr.send = fcoe_fip_send;
562 fc->ctlr.update_mac = fcoe_update_src_mac;
563
564 fc->fip_packet_type.func = fcoe_fip_recv;
565 fc->fip_packet_type.type = htons(ETH_P_FIP);
566 fc->fip_packet_type.dev = fc->real_dev;
567 dev_add_pack(&fc->fip_packet_type);
568
Vasu Dev7f349142009-03-27 09:06:31 -0700569 /* configure lport scsi host properties */
570 rc = fcoe_shost_config(lp, shost, &netdev->dev);
571 if (rc) {
572 FC_DBG("Could not configure shost for lport\n");
573 goto out_host_put;
574 }
575
576 /* lport exch manager allocation */
577 rc = fcoe_em_config(lp);
578 if (rc) {
579 FC_DBG("Could not configure em for lport\n");
580 goto out_host_put;
581 }
582
583 /* Initialize the library */
584 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
585 if (rc) {
586 FC_DBG("Could not configure libfc for lport!\n");
587 goto out_lp_destroy;
588 }
589
590 /* add to lports list */
591 fcoe_hostlist_add(lp);
592
593 lp->boot_time = jiffies;
594
595 fc_fabric_login(lp);
596
Joe Eykholt97c83892009-03-17 11:42:40 -0700597 if (!fcoe_link_ok(lp))
598 fcoe_ctlr_link_up(&fc->ctlr);
599
Vasu Dev7f349142009-03-27 09:06:31 -0700600 dev_hold(netdev);
601
602 return rc;
603
604out_lp_destroy:
605 fc_exch_mgr_free(lp->emp); /* Free the EM */
606out_host_put:
607 scsi_host_put(lp->host);
608 return rc;
609}
610
611/**
612 * fcoe_if_init() - attach to scsi transport
613 *
614 * Returns : 0 on success
615 */
616static int __init fcoe_if_init(void)
617{
618 /* attach to scsi transport */
619 scsi_transport_fcoe_sw =
620 fc_attach_transport(&fcoe_transport_function);
621
622 if (!scsi_transport_fcoe_sw) {
623 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
624 return -ENODEV;
625 }
626
627 return 0;
628}
629
630/**
631 * fcoe_if_exit() - detach from scsi transport
632 *
633 * Returns : 0 on success
634 */
635int __exit fcoe_if_exit(void)
636{
637 fc_release_transport(scsi_transport_fcoe_sw);
638 return 0;
639}
640
Robert Love85b4aa42008-12-09 15:10:24 -0800641/**
Robert Love8976f422009-03-17 11:41:46 -0700642 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
643 * @cpu: cpu index for the online cpu
644 */
645static void fcoe_percpu_thread_create(unsigned int cpu)
646{
647 struct fcoe_percpu_s *p;
648 struct task_struct *thread;
649
650 p = &per_cpu(fcoe_percpu, cpu);
651
652 thread = kthread_create(fcoe_percpu_receive_thread,
653 (void *)p, "fcoethread/%d", cpu);
654
655 if (likely(!IS_ERR(p->thread))) {
656 kthread_bind(thread, cpu);
657 wake_up_process(thread);
658
659 spin_lock_bh(&p->fcoe_rx_list.lock);
660 p->thread = thread;
661 spin_unlock_bh(&p->fcoe_rx_list.lock);
662 }
663}
664
665/**
666 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
667 * @cpu: cpu index the rx thread is to be removed
668 *
669 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
670 * current CPU's Rx thread. If the thread being destroyed is bound to
671 * the CPU processing this context the skbs will be freed.
672 */
673static void fcoe_percpu_thread_destroy(unsigned int cpu)
674{
675 struct fcoe_percpu_s *p;
676 struct task_struct *thread;
677 struct page *crc_eof;
678 struct sk_buff *skb;
679#ifdef CONFIG_SMP
680 struct fcoe_percpu_s *p0;
681 unsigned targ_cpu = smp_processor_id();
682#endif /* CONFIG_SMP */
683
684 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
685
686 /* Prevent any new skbs from being queued for this CPU. */
687 p = &per_cpu(fcoe_percpu, cpu);
688 spin_lock_bh(&p->fcoe_rx_list.lock);
689 thread = p->thread;
690 p->thread = NULL;
691 crc_eof = p->crc_eof_page;
692 p->crc_eof_page = NULL;
693 p->crc_eof_offset = 0;
694 spin_unlock_bh(&p->fcoe_rx_list.lock);
695
696#ifdef CONFIG_SMP
697 /*
698 * Don't bother moving the skb's if this context is running
699 * on the same CPU that is having its thread destroyed. This
700 * can easily happen when the module is removed.
701 */
702 if (cpu != targ_cpu) {
703 p0 = &per_cpu(fcoe_percpu, targ_cpu);
704 spin_lock_bh(&p0->fcoe_rx_list.lock);
705 if (p0->thread) {
706 FC_DBG("Moving frames from CPU %d to CPU %d\n",
707 cpu, targ_cpu);
708
709 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
710 __skb_queue_tail(&p0->fcoe_rx_list, skb);
711 spin_unlock_bh(&p0->fcoe_rx_list.lock);
712 } else {
713 /*
714 * The targeted CPU is not initialized and cannot accept
715 * new skbs. Unlock the targeted CPU and drop the skbs
716 * on the CPU that is going offline.
717 */
718 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
719 kfree_skb(skb);
720 spin_unlock_bh(&p0->fcoe_rx_list.lock);
721 }
722 } else {
723 /*
724 * This scenario occurs when the module is being removed
725 * and all threads are being destroyed. skbs will continue
726 * to be shifted from the CPU thread that is being removed
727 * to the CPU thread associated with the CPU that is processing
728 * the module removal. Once there is only one CPU Rx thread it
729 * will reach this case and we will drop all skbs and later
730 * stop the thread.
731 */
732 spin_lock_bh(&p->fcoe_rx_list.lock);
733 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
734 kfree_skb(skb);
735 spin_unlock_bh(&p->fcoe_rx_list.lock);
736 }
737#else
738 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700739 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700740 * being removed. Free all skbs and stop the thread.
741 */
742 spin_lock_bh(&p->fcoe_rx_list.lock);
743 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
744 kfree_skb(skb);
745 spin_unlock_bh(&p->fcoe_rx_list.lock);
746#endif
747
748 if (thread)
749 kthread_stop(thread);
750
751 if (crc_eof)
752 put_page(crc_eof);
753}
754
755/**
756 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
757 * @nfb: callback data block
758 * @action: event triggering the callback
759 * @hcpu: index for the cpu of this event
760 *
761 * This creates or destroys per cpu data for fcoe
762 *
763 * Returns NOTIFY_OK always.
764 */
765static int fcoe_cpu_callback(struct notifier_block *nfb,
766 unsigned long action, void *hcpu)
767{
768 unsigned cpu = (unsigned long)hcpu;
769
770 switch (action) {
771 case CPU_ONLINE:
772 case CPU_ONLINE_FROZEN:
773 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
774 fcoe_percpu_thread_create(cpu);
775 break;
776 case CPU_DEAD:
777 case CPU_DEAD_FROZEN:
778 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
779 fcoe_percpu_thread_destroy(cpu);
780 break;
781 default:
782 break;
783 }
784 return NOTIFY_OK;
785}
786
787static struct notifier_block fcoe_cpu_notifier = {
788 .notifier_call = fcoe_cpu_callback,
789};
790
791/**
Robert Love34f42a02009-02-27 10:55:45 -0800792 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800793 * @skb: the receive skb
794 * @dev: associated net device
795 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700796 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800797 *
798 * this function will receive the packet and build fc frame and pass it up
799 *
800 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800801 */
Robert Love85b4aa42008-12-09 15:10:24 -0800802int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
803 struct packet_type *ptype, struct net_device *olddev)
804{
805 struct fc_lport *lp;
806 struct fcoe_rcv_info *fr;
807 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800808 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800809 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700810 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700811 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800812
813 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700814 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800815 if (unlikely(lp == NULL)) {
816 FC_DBG("cannot find hba structure");
817 goto err2;
818 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700819 if (!lp->link_up)
820 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800821
822 if (unlikely(debug_fcoe)) {
823 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
824 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
825 skb->head, skb->data, skb_tail_pointer(skb),
826 skb_end_pointer(skb), skb->csum,
827 skb->dev ? skb->dev->name : "<NULL>");
828
829 }
830
831 /* check for FCOE packet type */
832 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
833 FC_DBG("wrong FC type frame");
834 goto err;
835 }
836
837 /*
838 * Check for minimum frame length, and make sure required FCoE
839 * and FC headers are pulled into the linear data area.
840 */
841 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
842 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
843 goto err;
844
845 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
846 fh = (struct fc_frame_header *) skb_transport_header(skb);
847
848 oxid = ntohs(fh->fh_ox_id);
849
850 fr = fcoe_dev_from_skb(skb);
851 fr->fr_dev = lp;
852 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700853
Robert Love85b4aa42008-12-09 15:10:24 -0800854#ifdef CONFIG_SMP
855 /*
856 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700857 * cpu bits to get cpu and then this cpu is used for selecting
858 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800859 */
Robert Love8976f422009-03-17 11:41:46 -0700860 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800861#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700862
Robert Love8976f422009-03-17 11:41:46 -0700863 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800864 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700865 if (unlikely(!fps->thread)) {
866 /*
867 * The targeted CPU is not ready, let's target
868 * the first CPU now. For non-SMP systems this
869 * will check the same CPU twice.
870 */
871 FC_DBG("CPU is online, but no receive thread ready "
872 "for incoming skb- using first online CPU.\n");
873
874 spin_unlock_bh(&fps->fcoe_rx_list.lock);
875 cpu = first_cpu(cpu_online_map);
876 fps = &per_cpu(fcoe_percpu, cpu);
877 spin_lock_bh(&fps->fcoe_rx_list.lock);
878 if (!fps->thread) {
879 spin_unlock_bh(&fps->fcoe_rx_list.lock);
880 goto err;
881 }
882 }
883
884 /*
885 * We now have a valid CPU that we're targeting for
886 * this skb. We also have this receive thread locked,
887 * so we're free to queue skbs into it's queue.
888 */
Robert Love85b4aa42008-12-09 15:10:24 -0800889 __skb_queue_tail(&fps->fcoe_rx_list, skb);
890 if (fps->fcoe_rx_list.qlen == 1)
891 wake_up_process(fps->thread);
892
893 spin_unlock_bh(&fps->fcoe_rx_list.lock);
894
895 return 0;
896err:
Robert Love582b45b2009-03-31 15:51:50 -0700897 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800898
899err2:
900 kfree_skb(skb);
901 return -1;
902}
Robert Love85b4aa42008-12-09 15:10:24 -0800903
904/**
Robert Love34f42a02009-02-27 10:55:45 -0800905 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800906 * @skb: the skb to be xmitted
907 *
908 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800909 */
Robert Love85b4aa42008-12-09 15:10:24 -0800910static inline int fcoe_start_io(struct sk_buff *skb)
911{
912 int rc;
913
914 skb_get(skb);
915 rc = dev_queue_xmit(skb);
916 if (rc != 0)
917 return rc;
918 kfree_skb(skb);
919 return 0;
920}
921
922/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700923 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800924 * @skb: the skb to be xmitted
925 * @tlen: total len
926 *
927 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800928 */
Robert Love85b4aa42008-12-09 15:10:24 -0800929static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
930{
931 struct fcoe_percpu_s *fps;
932 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800933
Robert Love5e5e92d2009-03-17 11:41:35 -0700934 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800935 page = fps->crc_eof_page;
936 if (!page) {
937 page = alloc_page(GFP_ATOMIC);
938 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700939 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800940 return -ENOMEM;
941 }
942 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700943 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800944 }
945
946 get_page(page);
947 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
948 fps->crc_eof_offset, tlen);
949 skb->len += tlen;
950 skb->data_len += tlen;
951 skb->truesize += tlen;
952 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
953
954 if (fps->crc_eof_offset >= PAGE_SIZE) {
955 fps->crc_eof_page = NULL;
956 fps->crc_eof_offset = 0;
957 put_page(page);
958 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700959 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800960 return 0;
961}
962
963/**
Robert Love34f42a02009-02-27 10:55:45 -0800964 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -0700965 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -0800966 *
967 * This uses crc32() to calculate the crc for fc frame
968 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800969 */
Robert Love85b4aa42008-12-09 15:10:24 -0800970u32 fcoe_fc_crc(struct fc_frame *fp)
971{
972 struct sk_buff *skb = fp_skb(fp);
973 struct skb_frag_struct *frag;
974 unsigned char *data;
975 unsigned long off, len, clen;
976 u32 crc;
977 unsigned i;
978
979 crc = crc32(~0, skb->data, skb_headlen(skb));
980
981 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
982 frag = &skb_shinfo(skb)->frags[i];
983 off = frag->page_offset;
984 len = frag->size;
985 while (len > 0) {
986 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
987 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
988 KM_SKB_DATA_SOFTIRQ);
989 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
990 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
991 off += clen;
992 len -= clen;
993 }
994 }
995 return crc;
996}
Robert Love85b4aa42008-12-09 15:10:24 -0800997
998/**
Robert Love34f42a02009-02-27 10:55:45 -0800999 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001000 * @lp: the associated local port
1001 * @fp: the fc_frame to be transmitted
1002 *
1003 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001004 */
Robert Love85b4aa42008-12-09 15:10:24 -08001005int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1006{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001007 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001008 u32 crc;
1009 struct ethhdr *eh;
1010 struct fcoe_crc_eof *cp;
1011 struct sk_buff *skb;
1012 struct fcoe_dev_stats *stats;
1013 struct fc_frame_header *fh;
1014 unsigned int hlen; /* header length implies the version */
1015 unsigned int tlen; /* trailer length */
1016 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001017 struct fcoe_softc *fc;
1018 u8 sof, eof;
1019 struct fcoe_hdr *hp;
1020
1021 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1022
Robert Lovefc47ff62009-02-27 10:55:55 -08001023 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001024 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001025 skb = fp_skb(fp);
1026 wlen = skb->len / FCOE_WORD_TO_BYTE;
1027
1028 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001029 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001030 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001031 }
1032
Joe Eykholt97c83892009-03-17 11:42:40 -07001033 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1034 fcoe_ctlr_els_send(&fc->ctlr, skb))
1035 return 0;
1036
Robert Love85b4aa42008-12-09 15:10:24 -08001037 sof = fr_sof(fp);
1038 eof = fr_eof(fp);
1039
1040 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
1041 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
1042 hlen = sizeof(struct fcoe_hdr);
1043 tlen = sizeof(struct fcoe_crc_eof);
1044 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1045
1046 /* crc offload */
1047 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001048 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001049 skb->csum_start = skb_headroom(skb);
1050 skb->csum_offset = skb->len;
1051 crc = 0;
1052 } else {
1053 skb->ip_summed = CHECKSUM_NONE;
1054 crc = fcoe_fc_crc(fp);
1055 }
1056
1057 /* copy fc crc and eof to the skb buff */
1058 if (skb_is_nonlinear(skb)) {
1059 skb_frag_t *frag;
1060 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001061 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001062 return -ENOMEM;
1063 }
1064 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1065 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1066 + frag->page_offset;
1067 } else {
1068 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1069 }
1070
1071 memset(cp, 0, sizeof(*cp));
1072 cp->fcoe_eof = eof;
1073 cp->fcoe_crc32 = cpu_to_le32(~crc);
1074
1075 if (skb_is_nonlinear(skb)) {
1076 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1077 cp = NULL;
1078 }
1079
Chris Leechdd3fd722009-04-21 16:27:36 -07001080 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001081 skb_push(skb, elen + hlen);
1082 skb_reset_mac_header(skb);
1083 skb_reset_network_header(skb);
1084 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001085 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001086 skb->dev = fc->real_dev;
1087
1088 /* fill up mac and fcoe headers */
1089 eh = eth_hdr(skb);
1090 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001091 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001092 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1093 else
1094 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001095 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001096
Joe Eykholt97c83892009-03-17 11:42:40 -07001097 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1098 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001099 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001100 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001101
1102 hp = (struct fcoe_hdr *)(eh + 1);
1103 memset(hp, 0, sizeof(*hp));
1104 if (FC_FCOE_VER)
1105 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1106 hp->fcoe_sof = sof;
1107
Yi Zou39ca9a02009-02-27 14:07:15 -08001108#ifdef NETIF_F_FSO
1109 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1110 if (lp->seq_offload && fr_max_payload(fp)) {
1111 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1112 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1113 } else {
1114 skb_shinfo(skb)->gso_type = 0;
1115 skb_shinfo(skb)->gso_size = 0;
1116 }
1117#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001118 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001119 stats = fc_lport_get_stats(lp);
1120 stats->TxFrames++;
1121 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001122
1123 /* send down to lld */
1124 fr_dev(fp) = lp;
1125 if (fc->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001126 fcoe_check_wait_queue(lp, skb);
1127 else if (fcoe_start_io(skb))
1128 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001129
1130 return 0;
1131}
Robert Love85b4aa42008-12-09 15:10:24 -08001132
Robert Love34f42a02009-02-27 10:55:45 -08001133/**
1134 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001135 * @arg: ptr to the fcoe per cpu struct
1136 *
1137 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001138 */
1139int fcoe_percpu_receive_thread(void *arg)
1140{
1141 struct fcoe_percpu_s *p = arg;
1142 u32 fr_len;
1143 struct fc_lport *lp;
1144 struct fcoe_rcv_info *fr;
1145 struct fcoe_dev_stats *stats;
1146 struct fc_frame_header *fh;
1147 struct sk_buff *skb;
1148 struct fcoe_crc_eof crc_eof;
1149 struct fc_frame *fp;
1150 u8 *mac = NULL;
1151 struct fcoe_softc *fc;
1152 struct fcoe_hdr *hp;
1153
Robert Love4469c192009-02-27 10:56:38 -08001154 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001155
1156 while (!kthread_should_stop()) {
1157
1158 spin_lock_bh(&p->fcoe_rx_list.lock);
1159 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1160 set_current_state(TASK_INTERRUPTIBLE);
1161 spin_unlock_bh(&p->fcoe_rx_list.lock);
1162 schedule();
1163 set_current_state(TASK_RUNNING);
1164 if (kthread_should_stop())
1165 return 0;
1166 spin_lock_bh(&p->fcoe_rx_list.lock);
1167 }
1168 spin_unlock_bh(&p->fcoe_rx_list.lock);
1169 fr = fcoe_dev_from_skb(skb);
1170 lp = fr->fr_dev;
1171 if (unlikely(lp == NULL)) {
1172 FC_DBG("invalid HBA Structure");
1173 kfree_skb(skb);
1174 continue;
1175 }
1176
Robert Love85b4aa42008-12-09 15:10:24 -08001177 if (unlikely(debug_fcoe)) {
1178 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1179 "tail:%p end:%p sum:%d dev:%s",
1180 skb->len, skb->data_len,
1181 skb->head, skb->data, skb_tail_pointer(skb),
1182 skb_end_pointer(skb), skb->csum,
1183 skb->dev ? skb->dev->name : "<NULL>");
1184 }
1185
1186 /*
1187 * Save source MAC address before discarding header.
1188 */
1189 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001190 if (skb_is_nonlinear(skb))
1191 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001192 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001193
1194 /*
1195 * Frame length checks and setting up the header pointers
1196 * was done in fcoe_rcv already.
1197 */
1198 hp = (struct fcoe_hdr *) skb_network_header(skb);
1199 fh = (struct fc_frame_header *) skb_transport_header(skb);
1200
Robert Love582b45b2009-03-31 15:51:50 -07001201 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001202 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001203 if (stats->ErrorFrames < 5)
1204 printk(KERN_WARNING "FCoE version "
1205 "mismatch: The frame has "
1206 "version %x, but the "
1207 "initiator supports version "
1208 "%x\n", FC_FCOE_DECAPS_VER(hp),
1209 FC_FCOE_VER);
1210 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001211 kfree_skb(skb);
1212 continue;
1213 }
1214
1215 skb_pull(skb, sizeof(struct fcoe_hdr));
1216 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1217
Robert Love582b45b2009-03-31 15:51:50 -07001218 stats->RxFrames++;
1219 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001220
1221 fp = (struct fc_frame *)skb;
1222 fc_frame_init(fp);
1223 fr_dev(fp) = lp;
1224 fr_sof(fp) = hp->fcoe_sof;
1225
1226 /* Copy out the CRC and EOF trailer for access */
1227 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1228 kfree_skb(skb);
1229 continue;
1230 }
1231 fr_eof(fp) = crc_eof.fcoe_eof;
1232 fr_crc(fp) = crc_eof.fcoe_crc32;
1233 if (pskb_trim(skb, fr_len)) {
1234 kfree_skb(skb);
1235 continue;
1236 }
1237
1238 /*
1239 * We only check CRC if no offload is available and if it is
1240 * it's solicited data, in which case, the FCP layer would
1241 * check it during the copy.
1242 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001243 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001244 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1245 else
1246 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1247
1248 fh = fc_frame_header_get(fp);
1249 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1250 fh->fh_type == FC_TYPE_FCP) {
1251 fc_exch_recv(lp, lp->emp, fp);
1252 continue;
1253 }
1254 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1255 if (le32_to_cpu(fr_crc(fp)) !=
1256 ~crc32(~0, skb->data, fr_len)) {
1257 if (debug_fcoe || stats->InvalidCRCCount < 5)
1258 printk(KERN_WARNING "fcoe: dropping "
1259 "frame with CRC error\n");
1260 stats->InvalidCRCCount++;
1261 stats->ErrorFrames++;
1262 fc_frame_free(fp);
1263 continue;
1264 }
1265 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1266 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001267 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1268 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1269 fc_frame_free(fp);
1270 continue;
1271 }
Robert Love85b4aa42008-12-09 15:10:24 -08001272 fc_exch_recv(lp, lp->emp, fp);
1273 }
1274 return 0;
1275}
1276
1277/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001278 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1279 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001280 *
1281 * This empties the wait_queue, dequeue the head of the wait_queue queue
1282 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001283 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001284 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001285 *
1286 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001287 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001288 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001289 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001290static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001291{
Chris Leech55c8baf2009-02-27 10:56:32 -08001292 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001293 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001294
Robert Love85b4aa42008-12-09 15:10:24 -08001295 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001296
1297 if (skb)
1298 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1299
Vasu Devc826a312009-02-27 10:56:27 -08001300 if (fc->fcoe_pending_queue_active)
1301 goto out;
1302 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001303
1304 while (fc->fcoe_pending_queue.qlen) {
1305 /* keep qlen > 0 until fcoe_start_io succeeds */
1306 fc->fcoe_pending_queue.qlen++;
1307 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1308
1309 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1310 rc = fcoe_start_io(skb);
1311 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1312
1313 if (rc) {
1314 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1315 /* undo temporary increment above */
1316 fc->fcoe_pending_queue.qlen--;
1317 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001318 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001319 /* undo temporary increment above */
1320 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001321 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001322
1323 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1324 lp->qfull = 0;
Vasu Dev1047f222009-05-06 10:52:40 -07001325 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1326 mod_timer(&fc->timer, jiffies + 2);
Vasu Devc826a312009-02-27 10:56:27 -08001327 fc->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001328out:
Vasu Dev4bb6b512009-05-06 10:52:34 -07001329 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1330 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001331 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001332 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001333}
1334
1335/**
Robert Love34f42a02009-02-27 10:55:45 -08001336 * fcoe_dev_setup() - setup link change notification interface
1337 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001338static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001339{
Robert Love85b4aa42008-12-09 15:10:24 -08001340 register_netdevice_notifier(&fcoe_notifier);
1341}
1342
1343/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001344 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001345 */
Robert Love85b4aa42008-12-09 15:10:24 -08001346static void fcoe_dev_cleanup(void)
1347{
1348 unregister_netdevice_notifier(&fcoe_notifier);
1349}
1350
1351/**
Robert Love34f42a02009-02-27 10:55:45 -08001352 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001353 * @notifier: context of the notification
1354 * @event: type of event
1355 * @ptr: fixed array for output parsed ifname
1356 *
1357 * This function is called by the ethernet driver in case of link change event
1358 *
1359 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001360 */
Robert Love85b4aa42008-12-09 15:10:24 -08001361static int fcoe_device_notification(struct notifier_block *notifier,
1362 ulong event, void *ptr)
1363{
1364 struct fc_lport *lp = NULL;
1365 struct net_device *real_dev = ptr;
1366 struct fcoe_softc *fc;
1367 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001368 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001369 u32 mfs;
1370 int rc = NOTIFY_OK;
1371
1372 read_lock(&fcoe_hostlist_lock);
1373 list_for_each_entry(fc, &fcoe_hostlist, list) {
1374 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001375 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001376 break;
1377 }
1378 }
1379 read_unlock(&fcoe_hostlist_lock);
1380 if (lp == NULL) {
1381 rc = NOTIFY_DONE;
1382 goto out;
1383 }
1384
Robert Love85b4aa42008-12-09 15:10:24 -08001385 switch (event) {
1386 case NETDEV_DOWN:
1387 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001388 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001389 break;
1390 case NETDEV_UP:
1391 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001392 break;
1393 case NETDEV_CHANGEMTU:
1394 mfs = fc->real_dev->mtu -
1395 (sizeof(struct fcoe_hdr) +
1396 sizeof(struct fcoe_crc_eof));
1397 if (mfs >= FC_MIN_MAX_FRAME)
1398 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001399 break;
1400 case NETDEV_REGISTER:
1401 break;
1402 default:
Joe Eykholt97c83892009-03-17 11:42:40 -07001403 FC_DBG("Unknown event %ld from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001404 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001405 if (link_possible && !fcoe_link_ok(lp))
1406 fcoe_ctlr_link_up(&fc->ctlr);
1407 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1408 stats = fc_lport_get_stats(lp);
1409 stats->LinkFailureCount++;
1410 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001411 }
1412out:
1413 return rc;
1414}
1415
1416/**
Robert Love34f42a02009-02-27 10:55:45 -08001417 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001418 * @buffer: incoming buffer to be copied
1419 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001420 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001421 */
Robert Love85b4aa42008-12-09 15:10:24 -08001422static struct net_device *fcoe_if_to_netdev(const char *buffer)
1423{
1424 char *cp;
1425 char ifname[IFNAMSIZ + 2];
1426
1427 if (buffer) {
1428 strlcpy(ifname, buffer, IFNAMSIZ);
1429 cp = ifname + strlen(ifname);
1430 while (--cp >= ifname && *cp == '\n')
1431 *cp = '\0';
1432 return dev_get_by_name(&init_net, ifname);
1433 }
1434 return NULL;
1435}
1436
1437/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001438 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001439 * @netdev: the target netdev
1440 *
1441 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001442 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001443static struct module *
1444fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001445{
1446 struct device *dev;
1447
1448 if (!netdev)
1449 return NULL;
1450
1451 dev = netdev->dev.parent;
1452 if (!dev)
1453 return NULL;
1454
1455 if (!dev->driver)
1456 return NULL;
1457
1458 return dev->driver->owner;
1459}
1460
1461/**
Robert Love34f42a02009-02-27 10:55:45 -08001462 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001463 * @netdev: the target netdev
1464 *
Robert Love34f42a02009-02-27 10:55:45 -08001465 * Holds the Ethernet driver module by try_module_get() for
1466 * the corresponding netdev.
1467 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001468 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001469 */
Robert Love85b4aa42008-12-09 15:10:24 -08001470static int fcoe_ethdrv_get(const struct net_device *netdev)
1471{
1472 struct module *owner;
1473
1474 owner = fcoe_netdev_to_module_owner(netdev);
1475 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001476 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1477 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001478 return try_module_get(owner);
1479 }
1480 return -ENODEV;
1481}
1482
1483/**
Robert Love34f42a02009-02-27 10:55:45 -08001484 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001485 * @netdev: the target netdev
1486 *
Robert Love34f42a02009-02-27 10:55:45 -08001487 * Releases the Ethernet driver module by module_put for
1488 * the corresponding netdev.
1489 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001490 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001491 */
Robert Love85b4aa42008-12-09 15:10:24 -08001492static int fcoe_ethdrv_put(const struct net_device *netdev)
1493{
1494 struct module *owner;
1495
1496 owner = fcoe_netdev_to_module_owner(netdev);
1497 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001498 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1499 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001500 module_put(owner);
1501 return 0;
1502 }
1503 return -ENODEV;
1504}
1505
1506/**
Robert Love34f42a02009-02-27 10:55:45 -08001507 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001508 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001509 * @kp: associated kernel param
1510 *
1511 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001512 */
Robert Love85b4aa42008-12-09 15:10:24 -08001513static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1514{
1515 int rc;
1516 struct net_device *netdev;
1517
1518 netdev = fcoe_if_to_netdev(buffer);
1519 if (!netdev) {
1520 rc = -ENODEV;
1521 goto out_nodev;
1522 }
1523 /* look for existing lport */
1524 if (!fcoe_hostlist_lookup(netdev)) {
1525 rc = -ENODEV;
1526 goto out_putdev;
1527 }
Vasu Dev7f349142009-03-27 09:06:31 -07001528 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001529 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001530 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001531 netdev->name);
1532 rc = -EIO;
1533 goto out_putdev;
1534 }
1535 fcoe_ethdrv_put(netdev);
1536 rc = 0;
1537out_putdev:
1538 dev_put(netdev);
1539out_nodev:
1540 return rc;
1541}
1542
1543/**
Robert Love34f42a02009-02-27 10:55:45 -08001544 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001545 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001546 * @kp: associated kernel param
1547 *
1548 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001549 */
Robert Love85b4aa42008-12-09 15:10:24 -08001550static int fcoe_create(const char *buffer, struct kernel_param *kp)
1551{
1552 int rc;
1553 struct net_device *netdev;
1554
1555 netdev = fcoe_if_to_netdev(buffer);
1556 if (!netdev) {
1557 rc = -ENODEV;
1558 goto out_nodev;
1559 }
1560 /* look for existing lport */
1561 if (fcoe_hostlist_lookup(netdev)) {
1562 rc = -EEXIST;
1563 goto out_putdev;
1564 }
1565 fcoe_ethdrv_get(netdev);
1566
Vasu Dev7f349142009-03-27 09:06:31 -07001567 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001568 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001569 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001570 netdev->name);
1571 fcoe_ethdrv_put(netdev);
1572 rc = -EIO;
1573 goto out_putdev;
1574 }
1575 rc = 0;
1576out_putdev:
1577 dev_put(netdev);
1578out_nodev:
1579 return rc;
1580}
1581
1582module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1583__MODULE_PARM_TYPE(create, "string");
1584MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1585module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1586__MODULE_PARM_TYPE(destroy, "string");
1587MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1588
Robert Love34f42a02009-02-27 10:55:45 -08001589/**
1590 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001591 * @lp: ptr to the fc_lport
1592 *
1593 * Any permanently-disqualifying conditions have been previously checked.
1594 * This also updates the speed setting, which may change with link for 100/1000.
1595 *
1596 * This function should probably be checking for PAUSE support at some point
1597 * in the future. Currently Per-priority-pause is not determinable using
1598 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1599 *
1600 * Returns: 0 if link is OK for use by FCoE.
1601 *
1602 */
1603int fcoe_link_ok(struct fc_lport *lp)
1604{
Robert Lovefc47ff62009-02-27 10:55:55 -08001605 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001606 struct net_device *dev = fc->real_dev;
1607 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1608 int rc = 0;
1609
1610 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1611 dev = fc->phys_dev;
1612 if (dev->ethtool_ops->get_settings) {
1613 dev->ethtool_ops->get_settings(dev, &ecmd);
1614 lp->link_supported_speeds &=
1615 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1616 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1617 SUPPORTED_1000baseT_Full))
1618 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1619 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1620 lp->link_supported_speeds |=
1621 FC_PORTSPEED_10GBIT;
1622 if (ecmd.speed == SPEED_1000)
1623 lp->link_speed = FC_PORTSPEED_1GBIT;
1624 if (ecmd.speed == SPEED_10000)
1625 lp->link_speed = FC_PORTSPEED_10GBIT;
1626 }
1627 } else
1628 rc = -1;
1629
1630 return rc;
1631}
Robert Love85b4aa42008-12-09 15:10:24 -08001632
Robert Love34f42a02009-02-27 10:55:45 -08001633/**
1634 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001635 * @lp: the fc_lport
1636 */
1637void fcoe_percpu_clean(struct fc_lport *lp)
1638{
Robert Love85b4aa42008-12-09 15:10:24 -08001639 struct fcoe_percpu_s *pp;
1640 struct fcoe_rcv_info *fr;
1641 struct sk_buff_head *list;
1642 struct sk_buff *skb, *next;
1643 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001644 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001645
Robert Love5e5e92d2009-03-17 11:41:35 -07001646 for_each_possible_cpu(cpu) {
1647 pp = &per_cpu(fcoe_percpu, cpu);
1648 spin_lock_bh(&pp->fcoe_rx_list.lock);
1649 list = &pp->fcoe_rx_list;
1650 head = list->next;
1651 for (skb = head; skb != (struct sk_buff *)list;
1652 skb = next) {
1653 next = skb->next;
1654 fr = fcoe_dev_from_skb(skb);
1655 if (fr->fr_dev == lp) {
1656 __skb_unlink(skb, list);
1657 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001658 }
Robert Love85b4aa42008-12-09 15:10:24 -08001659 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001660 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001661 }
1662}
Robert Love85b4aa42008-12-09 15:10:24 -08001663
1664/**
Robert Love34f42a02009-02-27 10:55:45 -08001665 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001666 * @lp: the corresponding fc_lport
1667 *
1668 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001669 */
Robert Love85b4aa42008-12-09 15:10:24 -08001670void fcoe_clean_pending_queue(struct fc_lport *lp)
1671{
1672 struct fcoe_softc *fc = lport_priv(lp);
1673 struct sk_buff *skb;
1674
1675 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1676 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1677 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1678 kfree_skb(skb);
1679 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1680 }
1681 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1682}
Robert Love85b4aa42008-12-09 15:10:24 -08001683
1684/**
Robert Love34f42a02009-02-27 10:55:45 -08001685 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001686 * @shost: shost the reset is from
1687 *
1688 * Returns: always 0
1689 */
1690int fcoe_reset(struct Scsi_Host *shost)
1691{
1692 struct fc_lport *lport = shost_priv(shost);
1693 fc_lport_reset(lport);
1694 return 0;
1695}
Robert Love85b4aa42008-12-09 15:10:24 -08001696
Robert Love34f42a02009-02-27 10:55:45 -08001697/**
Robert Love34f42a02009-02-27 10:55:45 -08001698 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001699 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001700 *
1701 * Returns: NULL or the located fcoe_softc
1702 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001703static struct fcoe_softc *
1704fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001705{
1706 struct fcoe_softc *fc;
1707
1708 read_lock(&fcoe_hostlist_lock);
1709 list_for_each_entry(fc, &fcoe_hostlist, list) {
1710 if (fc->real_dev == dev) {
1711 read_unlock(&fcoe_hostlist_lock);
1712 return fc;
1713 }
1714 }
1715 read_unlock(&fcoe_hostlist_lock);
1716 return NULL;
1717}
1718
Robert Love34f42a02009-02-27 10:55:45 -08001719/**
1720 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001721 * @netdev: ptr to net_device
1722 *
1723 * Returns: 0 for success
1724 */
1725struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1726{
1727 struct fcoe_softc *fc;
1728
1729 fc = fcoe_hostlist_lookup_softc(netdev);
1730
Joe Eykholt97c83892009-03-17 11:42:40 -07001731 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001732}
Robert Love85b4aa42008-12-09 15:10:24 -08001733
Robert Love34f42a02009-02-27 10:55:45 -08001734/**
1735 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001736 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001737 *
1738 * Returns: 0 for success
1739 */
1740int fcoe_hostlist_add(const struct fc_lport *lp)
1741{
1742 struct fcoe_softc *fc;
1743
1744 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1745 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001746 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001747 write_lock_bh(&fcoe_hostlist_lock);
1748 list_add_tail(&fc->list, &fcoe_hostlist);
1749 write_unlock_bh(&fcoe_hostlist_lock);
1750 }
1751 return 0;
1752}
Robert Love85b4aa42008-12-09 15:10:24 -08001753
Robert Love34f42a02009-02-27 10:55:45 -08001754/**
1755 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001756 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001757 *
1758 * Returns: 0 for success
1759 */
1760int fcoe_hostlist_remove(const struct fc_lport *lp)
1761{
1762 struct fcoe_softc *fc;
1763
1764 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1765 BUG_ON(!fc);
1766 write_lock_bh(&fcoe_hostlist_lock);
1767 list_del(&fc->list);
1768 write_unlock_bh(&fcoe_hostlist_lock);
1769
1770 return 0;
1771}
Robert Love85b4aa42008-12-09 15:10:24 -08001772
1773/**
Robert Love34f42a02009-02-27 10:55:45 -08001774 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001775 *
Robert Love85b4aa42008-12-09 15:10:24 -08001776 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001777 */
Robert Love85b4aa42008-12-09 15:10:24 -08001778static int __init fcoe_init(void)
1779{
Robert Love38eccab2009-03-17 11:41:30 -07001780 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001781 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001782 struct fcoe_percpu_s *p;
1783
Robert Love85b4aa42008-12-09 15:10:24 -08001784 INIT_LIST_HEAD(&fcoe_hostlist);
1785 rwlock_init(&fcoe_hostlist_lock);
1786
Robert Love38eccab2009-03-17 11:41:30 -07001787 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001788 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001789 skb_queue_head_init(&p->fcoe_rx_list);
1790 }
1791
Robert Love8976f422009-03-17 11:41:46 -07001792 for_each_online_cpu(cpu)
1793 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001794
Robert Love8976f422009-03-17 11:41:46 -07001795 /* Initialize per CPU interrupt thread */
1796 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1797 if (rc)
1798 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001799
Robert Love8976f422009-03-17 11:41:46 -07001800 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001801 fcoe_dev_setup();
1802
Vasu Dev7f349142009-03-27 09:06:31 -07001803 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001804
1805 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001806
1807out_free:
1808 for_each_online_cpu(cpu) {
1809 fcoe_percpu_thread_destroy(cpu);
1810 }
1811
1812 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001813}
1814module_init(fcoe_init);
1815
1816/**
Robert Love34f42a02009-02-27 10:55:45 -08001817 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001818 *
1819 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001820 */
Robert Love85b4aa42008-12-09 15:10:24 -08001821static void __exit fcoe_exit(void)
1822{
Robert Love5e5e92d2009-03-17 11:41:35 -07001823 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001824 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001825
Robert Love85b4aa42008-12-09 15:10:24 -08001826 fcoe_dev_cleanup();
1827
Vasu Dev5919a592009-03-27 09:03:29 -07001828 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001829 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001830 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001831
Robert Love8976f422009-03-17 11:41:46 -07001832 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1833
1834 for_each_online_cpu(cpu) {
1835 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001836 }
1837
Vasu Dev7f349142009-03-27 09:06:31 -07001838 /* detach from scsi transport */
1839 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001840}
1841module_exit(fcoe_exit);