blob: a81a8ec3908e429d022a1297862a56411225a101 [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>
22#include <linux/kernel.h>
23#include <linux/spinlock.h>
24#include <linux/skbuff.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/ethtool.h>
28#include <linux/if_ether.h>
29#include <linux/if_vlan.h>
30#include <linux/kthread.h>
31#include <linux/crc32.h>
32#include <linux/cpu.h>
33#include <linux/fs.h>
34#include <linux/sysfs.h>
35#include <linux/ctype.h>
36#include <scsi/scsi_tcq.h>
37#include <scsi/scsicam.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_fc.h>
40#include <net/rtnetlink.h>
41
42#include <scsi/fc/fc_encaps.h>
43
44#include <scsi/libfc.h>
45#include <scsi/fc_frame.h>
46#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080047
48static int debug_fcoe;
49
50#define FCOE_MAX_QUEUE_DEPTH 256
Vasu Devc826a312009-02-27 10:56:27 -080051#define FCOE_LOW_QUEUE_DEPTH 32
Robert Love85b4aa42008-12-09 15:10:24 -080052
53/* destination address mode */
54#define FCOE_GW_ADDR_MODE 0x00
55#define FCOE_FCOUI_ADDR_MODE 0x01
56
57#define FCOE_WORD_TO_BYTE 4
58
Vasu Dev7f349142009-03-27 09:06:31 -070059#define FCOE_VERSION "0.1"
60#define FCOE_NAME "fcoe"
61#define FCOE_VENDOR "Open-FCoE.org"
62
63#define FCOE_MAX_LUN 255
64#define FCOE_MAX_FCP_TARGET 256
65
66#define FCOE_MAX_OUTSTANDING_COMMANDS 1024
67
68#define FCOE_MIN_XID 0x0001 /* the min xid supported by fcoe_sw */
69#define FCOE_MAX_XID 0x07ef /* the max xid supported by fcoe_sw */
70
Robert Love85b4aa42008-12-09 15:10:24 -080071MODULE_AUTHOR("Open-FCoE.org");
72MODULE_DESCRIPTION("FCoE");
73MODULE_LICENSE("GPL");
74
75/* fcoe host list */
76LIST_HEAD(fcoe_hostlist);
77DEFINE_RWLOCK(fcoe_hostlist_lock);
78DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
Robert Love5e5e92d2009-03-17 11:41:35 -070079DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080080
81
82/* Function Prototyes */
83static int fcoe_check_wait_queue(struct fc_lport *);
Robert Love85b4aa42008-12-09 15:10:24 -080084static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
Robert Love85b4aa42008-12-09 15:10:24 -080085static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86static void fcoe_dev_setup(void);
87static void fcoe_dev_cleanup(void);
88
89/* notification function from net device */
90static struct notifier_block fcoe_notifier = {
91 .notifier_call = fcoe_device_notification,
92};
93
Vasu Dev7f349142009-03-27 09:06:31 -070094static struct scsi_transport_template *scsi_transport_fcoe_sw;
95
96struct fc_function_template fcoe_transport_function = {
97 .show_host_node_name = 1,
98 .show_host_port_name = 1,
99 .show_host_supported_classes = 1,
100 .show_host_supported_fc4s = 1,
101 .show_host_active_fc4s = 1,
102 .show_host_maxframe_size = 1,
103
104 .show_host_port_id = 1,
105 .show_host_supported_speeds = 1,
106 .get_host_speed = fc_get_host_speed,
107 .show_host_speed = 1,
108 .show_host_port_type = 1,
109 .get_host_port_state = fc_get_host_port_state,
110 .show_host_port_state = 1,
111 .show_host_symbolic_name = 1,
112
113 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
114 .show_rport_maxframe_size = 1,
115 .show_rport_supported_classes = 1,
116
117 .show_host_fabric_name = 1,
118 .show_starget_node_name = 1,
119 .show_starget_port_name = 1,
120 .show_starget_port_id = 1,
121 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
122 .show_rport_dev_loss_tmo = 1,
123 .get_fc_host_stats = fc_get_host_stats,
124 .issue_fc_host_lip = fcoe_reset,
125
126 .terminate_rport_io = fc_rport_terminate_io,
127};
128
129static struct scsi_host_template fcoe_shost_template = {
130 .module = THIS_MODULE,
131 .name = "FCoE Driver",
132 .proc_name = FCOE_NAME,
133 .queuecommand = fc_queuecommand,
134 .eh_abort_handler = fc_eh_abort,
135 .eh_device_reset_handler = fc_eh_device_reset,
136 .eh_host_reset_handler = fc_eh_host_reset,
137 .slave_alloc = fc_slave_alloc,
138 .change_queue_depth = fc_change_queue_depth,
139 .change_queue_type = fc_change_queue_type,
140 .this_id = -1,
141 .cmd_per_lun = 32,
142 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
143 .use_clustering = ENABLE_CLUSTERING,
144 .sg_tablesize = SG_ALL,
145 .max_sectors = 0xffff,
146};
147
148/**
149 * fcoe_lport_config() - sets up the fc_lport
150 * @lp: ptr to the fc_lport
151 * @shost: ptr to the parent scsi host
152 *
153 * Returns: 0 for success
154 */
155static int fcoe_lport_config(struct fc_lport *lp)
156{
157 lp->link_up = 0;
158 lp->qfull = 0;
159 lp->max_retry_count = 3;
160 lp->e_d_tov = 2 * 1000; /* FC-FS default */
161 lp->r_a_tov = 2 * 2 * 1000;
162 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
163 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
164
165 fc_lport_init_stats(lp);
166
167 /* lport fc_lport related configuration */
168 fc_lport_config(lp);
169
170 /* offload related configuration */
171 lp->crc_offload = 0;
172 lp->seq_offload = 0;
173 lp->lro_enabled = 0;
174 lp->lro_xid = 0;
175 lp->lso_max = 0;
176
177 return 0;
178}
179
180/**
181 * fcoe_netdev_config() - Set up netdev for SW FCoE
182 * @lp : ptr to the fc_lport
183 * @netdev : ptr to the associated netdevice struct
184 *
185 * Must be called after fcoe_lport_config() as it will use lport mutex
186 *
187 * Returns : 0 for success
188 */
189static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
190{
191 u32 mfs;
192 u64 wwnn, wwpn;
193 struct fcoe_softc *fc;
194 u8 flogi_maddr[ETH_ALEN];
195
196 /* Setup lport private data to point to fcoe softc */
197 fc = lport_priv(lp);
198 fc->lp = lp;
199 fc->real_dev = netdev;
200 fc->phys_dev = netdev;
201
202 /* Require support for get_pauseparam ethtool op. */
203 if (netdev->priv_flags & IFF_802_1Q_VLAN)
204 fc->phys_dev = vlan_dev_real_dev(netdev);
205
206 /* Do not support for bonding device */
207 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
208 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
209 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
210 return -EOPNOTSUPP;
211 }
212
213 /*
214 * Determine max frame size based on underlying device and optional
215 * user-configured limit. If the MFS is too low, fcoe_link_ok()
216 * will return 0, so do this first.
217 */
218 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
219 sizeof(struct fcoe_crc_eof));
220 if (fc_set_mfs(lp, mfs))
221 return -EINVAL;
222
223 if (!fcoe_link_ok(lp))
224 lp->link_up = 1;
225
226 /* offload features support */
227 if (fc->real_dev->features & NETIF_F_SG)
228 lp->sg_supp = 1;
229
230#ifdef NETIF_F_FCOE_CRC
231 if (netdev->features & NETIF_F_FCOE_CRC) {
232 lp->crc_offload = 1;
233 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
234 netdev->name);
235 }
236#endif
237#ifdef NETIF_F_FSO
238 if (netdev->features & NETIF_F_FSO) {
239 lp->seq_offload = 1;
240 lp->lso_max = netdev->gso_max_size;
241 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
242 netdev->name, lp->lso_max);
243 }
244#endif
245 if (netdev->fcoe_ddp_xid) {
246 lp->lro_enabled = 1;
247 lp->lro_xid = netdev->fcoe_ddp_xid;
248 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
249 netdev->name, lp->lro_xid);
250 }
251 skb_queue_head_init(&fc->fcoe_pending_queue);
252 fc->fcoe_pending_queue_active = 0;
253
254 /* setup Source Mac Address */
255 memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
256 fc->real_dev->addr_len);
257
258 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
259 fc_set_wwnn(lp, wwnn);
260 /* XXX - 3rd arg needs to be vlan id */
261 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
262 fc_set_wwpn(lp, wwpn);
263
264 /*
265 * Add FCoE MAC address as second unicast MAC address
266 * or enter promiscuous mode if not capable of listening
267 * for multiple unicast MACs.
268 */
269 rtnl_lock();
270 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
271 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
272 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);
371
372 /* Cleanup the fc_lport */
373 fc_lport_destroy(lp);
374 fc_fcp_destroy(lp);
375
376 /* Detach from the scsi-ml */
377 fc_remove_host(lp->host);
378 scsi_remove_host(lp->host);
379
380 /* There are no more rports or I/O, free the EM */
381 if (lp->emp)
382 fc_exch_mgr_free(lp->emp);
383
384 /* Delete secondary MAC addresses */
385 rtnl_lock();
386 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
387 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
388 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
389 dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
390 rtnl_unlock();
391
392 /* Free the per-CPU revieve threads */
393 fcoe_percpu_clean(lp);
394
395 /* Free existing skbs */
396 fcoe_clean_pending_queue(lp);
397
398 /* Free memory used by statistical counters */
399 fc_lport_free_stats(lp);
400
401 /* Release the net_device and Scsi_Host */
402 dev_put(fc->real_dev);
403 scsi_host_put(lp->host);
404
405 return 0;
406}
407
408/*
409 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
410 * @lp: the corresponding fc_lport
411 * @xid: the exchange id for this ddp transfer
412 * @sgl: the scatterlist describing this transfer
413 * @sgc: number of sg items
414 *
415 * Returns : 0 no ddp
416 */
417static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
418 struct scatterlist *sgl, unsigned int sgc)
419{
420 struct net_device *n = fcoe_netdev(lp);
421
422 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
423 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
424
425 return 0;
426}
427
428/*
429 * fcoe_ddp_done - calls LLD's ddp_done through net_device
430 * @lp: the corresponding fc_lport
431 * @xid: the exchange id for this ddp transfer
432 *
433 * Returns : the length of data that have been completed by ddp
434 */
435static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
436{
437 struct net_device *n = fcoe_netdev(lp);
438
439 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
440 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
441 return 0;
442}
443
444static struct libfc_function_template fcoe_libfc_fcn_templ = {
445 .frame_send = fcoe_xmit,
446 .ddp_setup = fcoe_ddp_setup,
447 .ddp_done = fcoe_ddp_done,
448};
449
450/**
451 * fcoe_if_create() - this function creates the fcoe interface
452 * @netdev: pointer the associated netdevice
453 *
454 * Creates fc_lport struct and scsi_host for lport, configures lport
455 * and starts fabric login.
456 *
457 * Returns : 0 on success
458 */
459static int fcoe_if_create(struct net_device *netdev)
460{
461 int rc;
462 struct fc_lport *lp = NULL;
463 struct fcoe_softc *fc;
464 struct Scsi_Host *shost;
465
466 BUG_ON(!netdev);
467
468 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
469 netdev->name);
470
471 lp = fcoe_hostlist_lookup(netdev);
472 if (lp)
473 return -EEXIST;
474
475 shost = fcoe_host_alloc(&fcoe_shost_template,
476 sizeof(struct fcoe_softc));
477 if (!shost) {
478 FC_DBG("Could not allocate host structure\n");
479 return -ENOMEM;
480 }
481 lp = shost_priv(shost);
482 fc = lport_priv(lp);
483
484 /* configure fc_lport, e.g., em */
485 rc = fcoe_lport_config(lp);
486 if (rc) {
487 FC_DBG("Could not configure lport\n");
488 goto out_host_put;
489 }
490
491 /* configure lport network properties */
492 rc = fcoe_netdev_config(lp, netdev);
493 if (rc) {
494 FC_DBG("Could not configure netdev for lport\n");
495 goto out_host_put;
496 }
497
498 /* configure lport scsi host properties */
499 rc = fcoe_shost_config(lp, shost, &netdev->dev);
500 if (rc) {
501 FC_DBG("Could not configure shost for lport\n");
502 goto out_host_put;
503 }
504
505 /* lport exch manager allocation */
506 rc = fcoe_em_config(lp);
507 if (rc) {
508 FC_DBG("Could not configure em for lport\n");
509 goto out_host_put;
510 }
511
512 /* Initialize the library */
513 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
514 if (rc) {
515 FC_DBG("Could not configure libfc for lport!\n");
516 goto out_lp_destroy;
517 }
518
519 /* add to lports list */
520 fcoe_hostlist_add(lp);
521
522 lp->boot_time = jiffies;
523
524 fc_fabric_login(lp);
525
526 dev_hold(netdev);
527
528 return rc;
529
530out_lp_destroy:
531 fc_exch_mgr_free(lp->emp); /* Free the EM */
532out_host_put:
533 scsi_host_put(lp->host);
534 return rc;
535}
536
537/**
538 * fcoe_if_init() - attach to scsi transport
539 *
540 * Returns : 0 on success
541 */
542static int __init fcoe_if_init(void)
543{
544 /* attach to scsi transport */
545 scsi_transport_fcoe_sw =
546 fc_attach_transport(&fcoe_transport_function);
547
548 if (!scsi_transport_fcoe_sw) {
549 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
550 return -ENODEV;
551 }
552
553 return 0;
554}
555
556/**
557 * fcoe_if_exit() - detach from scsi transport
558 *
559 * Returns : 0 on success
560 */
561int __exit fcoe_if_exit(void)
562{
563 fc_release_transport(scsi_transport_fcoe_sw);
564 return 0;
565}
566
Robert Love85b4aa42008-12-09 15:10:24 -0800567/**
Robert Love8976f422009-03-17 11:41:46 -0700568 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
569 * @cpu: cpu index for the online cpu
570 */
571static void fcoe_percpu_thread_create(unsigned int cpu)
572{
573 struct fcoe_percpu_s *p;
574 struct task_struct *thread;
575
576 p = &per_cpu(fcoe_percpu, cpu);
577
578 thread = kthread_create(fcoe_percpu_receive_thread,
579 (void *)p, "fcoethread/%d", cpu);
580
581 if (likely(!IS_ERR(p->thread))) {
582 kthread_bind(thread, cpu);
583 wake_up_process(thread);
584
585 spin_lock_bh(&p->fcoe_rx_list.lock);
586 p->thread = thread;
587 spin_unlock_bh(&p->fcoe_rx_list.lock);
588 }
589}
590
591/**
592 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
593 * @cpu: cpu index the rx thread is to be removed
594 *
595 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
596 * current CPU's Rx thread. If the thread being destroyed is bound to
597 * the CPU processing this context the skbs will be freed.
598 */
599static void fcoe_percpu_thread_destroy(unsigned int cpu)
600{
601 struct fcoe_percpu_s *p;
602 struct task_struct *thread;
603 struct page *crc_eof;
604 struct sk_buff *skb;
605#ifdef CONFIG_SMP
606 struct fcoe_percpu_s *p0;
607 unsigned targ_cpu = smp_processor_id();
608#endif /* CONFIG_SMP */
609
610 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
611
612 /* Prevent any new skbs from being queued for this CPU. */
613 p = &per_cpu(fcoe_percpu, cpu);
614 spin_lock_bh(&p->fcoe_rx_list.lock);
615 thread = p->thread;
616 p->thread = NULL;
617 crc_eof = p->crc_eof_page;
618 p->crc_eof_page = NULL;
619 p->crc_eof_offset = 0;
620 spin_unlock_bh(&p->fcoe_rx_list.lock);
621
622#ifdef CONFIG_SMP
623 /*
624 * Don't bother moving the skb's if this context is running
625 * on the same CPU that is having its thread destroyed. This
626 * can easily happen when the module is removed.
627 */
628 if (cpu != targ_cpu) {
629 p0 = &per_cpu(fcoe_percpu, targ_cpu);
630 spin_lock_bh(&p0->fcoe_rx_list.lock);
631 if (p0->thread) {
632 FC_DBG("Moving frames from CPU %d to CPU %d\n",
633 cpu, targ_cpu);
634
635 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
636 __skb_queue_tail(&p0->fcoe_rx_list, skb);
637 spin_unlock_bh(&p0->fcoe_rx_list.lock);
638 } else {
639 /*
640 * The targeted CPU is not initialized and cannot accept
641 * new skbs. Unlock the targeted CPU and drop the skbs
642 * on the CPU that is going offline.
643 */
644 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
645 kfree_skb(skb);
646 spin_unlock_bh(&p0->fcoe_rx_list.lock);
647 }
648 } else {
649 /*
650 * This scenario occurs when the module is being removed
651 * and all threads are being destroyed. skbs will continue
652 * to be shifted from the CPU thread that is being removed
653 * to the CPU thread associated with the CPU that is processing
654 * the module removal. Once there is only one CPU Rx thread it
655 * will reach this case and we will drop all skbs and later
656 * stop the thread.
657 */
658 spin_lock_bh(&p->fcoe_rx_list.lock);
659 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
660 kfree_skb(skb);
661 spin_unlock_bh(&p->fcoe_rx_list.lock);
662 }
663#else
664 /*
665 * This a non-SMP scenario where the singluar Rx thread is
666 * being removed. Free all skbs and stop the thread.
667 */
668 spin_lock_bh(&p->fcoe_rx_list.lock);
669 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
670 kfree_skb(skb);
671 spin_unlock_bh(&p->fcoe_rx_list.lock);
672#endif
673
674 if (thread)
675 kthread_stop(thread);
676
677 if (crc_eof)
678 put_page(crc_eof);
679}
680
681/**
682 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
683 * @nfb: callback data block
684 * @action: event triggering the callback
685 * @hcpu: index for the cpu of this event
686 *
687 * This creates or destroys per cpu data for fcoe
688 *
689 * Returns NOTIFY_OK always.
690 */
691static int fcoe_cpu_callback(struct notifier_block *nfb,
692 unsigned long action, void *hcpu)
693{
694 unsigned cpu = (unsigned long)hcpu;
695
696 switch (action) {
697 case CPU_ONLINE:
698 case CPU_ONLINE_FROZEN:
699 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
700 fcoe_percpu_thread_create(cpu);
701 break;
702 case CPU_DEAD:
703 case CPU_DEAD_FROZEN:
704 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
705 fcoe_percpu_thread_destroy(cpu);
706 break;
707 default:
708 break;
709 }
710 return NOTIFY_OK;
711}
712
713static struct notifier_block fcoe_cpu_notifier = {
714 .notifier_call = fcoe_cpu_callback,
715};
716
717/**
Robert Love34f42a02009-02-27 10:55:45 -0800718 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800719 * @skb: the receive skb
720 * @dev: associated net device
721 * @ptype: context
722 * @odldev: last device
723 *
724 * this function will receive the packet and build fc frame and pass it up
725 *
726 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800727 */
Robert Love85b4aa42008-12-09 15:10:24 -0800728int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
729 struct packet_type *ptype, struct net_device *olddev)
730{
731 struct fc_lport *lp;
732 struct fcoe_rcv_info *fr;
733 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800734 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800735 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700736 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700737 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800738
739 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
740 lp = fc->lp;
741 if (unlikely(lp == NULL)) {
742 FC_DBG("cannot find hba structure");
743 goto err2;
744 }
745
746 if (unlikely(debug_fcoe)) {
747 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
748 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
749 skb->head, skb->data, skb_tail_pointer(skb),
750 skb_end_pointer(skb), skb->csum,
751 skb->dev ? skb->dev->name : "<NULL>");
752
753 }
754
755 /* check for FCOE packet type */
756 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
757 FC_DBG("wrong FC type frame");
758 goto err;
759 }
760
761 /*
762 * Check for minimum frame length, and make sure required FCoE
763 * and FC headers are pulled into the linear data area.
764 */
765 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
766 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
767 goto err;
768
769 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
770 fh = (struct fc_frame_header *) skb_transport_header(skb);
771
772 oxid = ntohs(fh->fh_ox_id);
773
774 fr = fcoe_dev_from_skb(skb);
775 fr->fr_dev = lp;
776 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700777
Robert Love85b4aa42008-12-09 15:10:24 -0800778#ifdef CONFIG_SMP
779 /*
780 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700781 * cpu bits to get cpu and then this cpu is used for selecting
782 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800783 */
Robert Love8976f422009-03-17 11:41:46 -0700784 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800785#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700786
Robert Love8976f422009-03-17 11:41:46 -0700787 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800788 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700789 if (unlikely(!fps->thread)) {
790 /*
791 * The targeted CPU is not ready, let's target
792 * the first CPU now. For non-SMP systems this
793 * will check the same CPU twice.
794 */
795 FC_DBG("CPU is online, but no receive thread ready "
796 "for incoming skb- using first online CPU.\n");
797
798 spin_unlock_bh(&fps->fcoe_rx_list.lock);
799 cpu = first_cpu(cpu_online_map);
800 fps = &per_cpu(fcoe_percpu, cpu);
801 spin_lock_bh(&fps->fcoe_rx_list.lock);
802 if (!fps->thread) {
803 spin_unlock_bh(&fps->fcoe_rx_list.lock);
804 goto err;
805 }
806 }
807
808 /*
809 * We now have a valid CPU that we're targeting for
810 * this skb. We also have this receive thread locked,
811 * so we're free to queue skbs into it's queue.
812 */
Robert Love85b4aa42008-12-09 15:10:24 -0800813 __skb_queue_tail(&fps->fcoe_rx_list, skb);
814 if (fps->fcoe_rx_list.qlen == 1)
815 wake_up_process(fps->thread);
816
817 spin_unlock_bh(&fps->fcoe_rx_list.lock);
818
819 return 0;
820err:
Robert Love582b45b2009-03-31 15:51:50 -0700821 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800822
823err2:
824 kfree_skb(skb);
825 return -1;
826}
827EXPORT_SYMBOL_GPL(fcoe_rcv);
828
829/**
Robert Love34f42a02009-02-27 10:55:45 -0800830 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800831 * @skb: the skb to be xmitted
832 *
833 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800834 */
Robert Love85b4aa42008-12-09 15:10:24 -0800835static inline int fcoe_start_io(struct sk_buff *skb)
836{
837 int rc;
838
839 skb_get(skb);
840 rc = dev_queue_xmit(skb);
841 if (rc != 0)
842 return rc;
843 kfree_skb(skb);
844 return 0;
845}
846
847/**
Robert Love34f42a02009-02-27 10:55:45 -0800848 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800849 * @skb: the skb to be xmitted
850 * @tlen: total len
851 *
852 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800853 */
Robert Love85b4aa42008-12-09 15:10:24 -0800854static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
855{
856 struct fcoe_percpu_s *fps;
857 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800858
Robert Love5e5e92d2009-03-17 11:41:35 -0700859 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800860 page = fps->crc_eof_page;
861 if (!page) {
862 page = alloc_page(GFP_ATOMIC);
863 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700864 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800865 return -ENOMEM;
866 }
867 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700868 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800869 }
870
871 get_page(page);
872 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
873 fps->crc_eof_offset, tlen);
874 skb->len += tlen;
875 skb->data_len += tlen;
876 skb->truesize += tlen;
877 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
878
879 if (fps->crc_eof_offset >= PAGE_SIZE) {
880 fps->crc_eof_page = NULL;
881 fps->crc_eof_offset = 0;
882 put_page(page);
883 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700884 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800885 return 0;
886}
887
888/**
Robert Love34f42a02009-02-27 10:55:45 -0800889 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Robert Love85b4aa42008-12-09 15:10:24 -0800890 * @fp: the fc_frame containg data to be checksummed
891 *
892 * This uses crc32() to calculate the crc for fc frame
893 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800894 */
Robert Love85b4aa42008-12-09 15:10:24 -0800895u32 fcoe_fc_crc(struct fc_frame *fp)
896{
897 struct sk_buff *skb = fp_skb(fp);
898 struct skb_frag_struct *frag;
899 unsigned char *data;
900 unsigned long off, len, clen;
901 u32 crc;
902 unsigned i;
903
904 crc = crc32(~0, skb->data, skb_headlen(skb));
905
906 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
907 frag = &skb_shinfo(skb)->frags[i];
908 off = frag->page_offset;
909 len = frag->size;
910 while (len > 0) {
911 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
912 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
913 KM_SKB_DATA_SOFTIRQ);
914 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
915 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
916 off += clen;
917 len -= clen;
918 }
919 }
920 return crc;
921}
922EXPORT_SYMBOL_GPL(fcoe_fc_crc);
923
924/**
Robert Love34f42a02009-02-27 10:55:45 -0800925 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -0800926 * @lp: the associated local port
927 * @fp: the fc_frame to be transmitted
928 *
929 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800930 */
Robert Love85b4aa42008-12-09 15:10:24 -0800931int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
932{
933 int wlen, rc = 0;
934 u32 crc;
935 struct ethhdr *eh;
936 struct fcoe_crc_eof *cp;
937 struct sk_buff *skb;
938 struct fcoe_dev_stats *stats;
939 struct fc_frame_header *fh;
940 unsigned int hlen; /* header length implies the version */
941 unsigned int tlen; /* trailer length */
942 unsigned int elen; /* eth header, may include vlan */
943 int flogi_in_progress = 0;
944 struct fcoe_softc *fc;
945 u8 sof, eof;
946 struct fcoe_hdr *hp;
947
948 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
949
Robert Lovefc47ff62009-02-27 10:55:55 -0800950 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800951 /*
952 * if it is a flogi then we need to learn gw-addr
953 * and my own fcid
954 */
955 fh = fc_frame_header_get(fp);
956 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
957 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
958 fc->flogi_oxid = ntohs(fh->fh_ox_id);
959 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
960 fc->flogi_progress = 1;
961 flogi_in_progress = 1;
962 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
963 /*
964 * Here we must've gotten an SID by accepting an FLOGI
965 * from a point-to-point connection. Switch to using
966 * the source mac based on the SID. The destination
967 * MAC in this case would have been set by receving the
968 * FLOGI.
969 */
970 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
971 fc->flogi_progress = 0;
972 }
973 }
974
975 skb = fp_skb(fp);
976 sof = fr_sof(fp);
977 eof = fr_eof(fp);
978
979 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
980 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
981 hlen = sizeof(struct fcoe_hdr);
982 tlen = sizeof(struct fcoe_crc_eof);
983 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
984
985 /* crc offload */
986 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -0800987 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -0800988 skb->csum_start = skb_headroom(skb);
989 skb->csum_offset = skb->len;
990 crc = 0;
991 } else {
992 skb->ip_summed = CHECKSUM_NONE;
993 crc = fcoe_fc_crc(fp);
994 }
995
996 /* copy fc crc and eof to the skb buff */
997 if (skb_is_nonlinear(skb)) {
998 skb_frag_t *frag;
999 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001000 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001001 return -ENOMEM;
1002 }
1003 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1004 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1005 + frag->page_offset;
1006 } else {
1007 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1008 }
1009
1010 memset(cp, 0, sizeof(*cp));
1011 cp->fcoe_eof = eof;
1012 cp->fcoe_crc32 = cpu_to_le32(~crc);
1013
1014 if (skb_is_nonlinear(skb)) {
1015 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1016 cp = NULL;
1017 }
1018
1019 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
1020 skb_push(skb, elen + hlen);
1021 skb_reset_mac_header(skb);
1022 skb_reset_network_header(skb);
1023 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001024 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001025 skb->dev = fc->real_dev;
1026
1027 /* fill up mac and fcoe headers */
1028 eh = eth_hdr(skb);
1029 eh->h_proto = htons(ETH_P_FCOE);
1030 if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
1031 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1032 else
1033 /* insert GW address */
1034 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
1035
1036 if (unlikely(flogi_in_progress))
1037 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
1038 else
1039 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
1040
1041 hp = (struct fcoe_hdr *)(eh + 1);
1042 memset(hp, 0, sizeof(*hp));
1043 if (FC_FCOE_VER)
1044 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1045 hp->fcoe_sof = sof;
1046
Yi Zou39ca9a02009-02-27 14:07:15 -08001047#ifdef NETIF_F_FSO
1048 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1049 if (lp->seq_offload && fr_max_payload(fp)) {
1050 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1051 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1052 } else {
1053 skb_shinfo(skb)->gso_type = 0;
1054 skb_shinfo(skb)->gso_size = 0;
1055 }
1056#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001057 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001058 stats = fc_lport_get_stats(lp);
1059 stats->TxFrames++;
1060 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001061
1062 /* send down to lld */
1063 fr_dev(fp) = lp;
1064 if (fc->fcoe_pending_queue.qlen)
1065 rc = fcoe_check_wait_queue(lp);
1066
1067 if (rc == 0)
1068 rc = fcoe_start_io(skb);
1069
1070 if (rc) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001071 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1072 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1073 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001074 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -08001075 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001076 }
1077
1078 return 0;
1079}
1080EXPORT_SYMBOL_GPL(fcoe_xmit);
1081
Robert Love34f42a02009-02-27 10:55:45 -08001082/**
1083 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001084 * @arg: ptr to the fcoe per cpu struct
1085 *
1086 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001087 */
1088int fcoe_percpu_receive_thread(void *arg)
1089{
1090 struct fcoe_percpu_s *p = arg;
1091 u32 fr_len;
1092 struct fc_lport *lp;
1093 struct fcoe_rcv_info *fr;
1094 struct fcoe_dev_stats *stats;
1095 struct fc_frame_header *fh;
1096 struct sk_buff *skb;
1097 struct fcoe_crc_eof crc_eof;
1098 struct fc_frame *fp;
1099 u8 *mac = NULL;
1100 struct fcoe_softc *fc;
1101 struct fcoe_hdr *hp;
1102
Robert Love4469c192009-02-27 10:56:38 -08001103 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001104
1105 while (!kthread_should_stop()) {
1106
1107 spin_lock_bh(&p->fcoe_rx_list.lock);
1108 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1109 set_current_state(TASK_INTERRUPTIBLE);
1110 spin_unlock_bh(&p->fcoe_rx_list.lock);
1111 schedule();
1112 set_current_state(TASK_RUNNING);
1113 if (kthread_should_stop())
1114 return 0;
1115 spin_lock_bh(&p->fcoe_rx_list.lock);
1116 }
1117 spin_unlock_bh(&p->fcoe_rx_list.lock);
1118 fr = fcoe_dev_from_skb(skb);
1119 lp = fr->fr_dev;
1120 if (unlikely(lp == NULL)) {
1121 FC_DBG("invalid HBA Structure");
1122 kfree_skb(skb);
1123 continue;
1124 }
1125
Robert Love85b4aa42008-12-09 15:10:24 -08001126 if (unlikely(debug_fcoe)) {
1127 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1128 "tail:%p end:%p sum:%d dev:%s",
1129 skb->len, skb->data_len,
1130 skb->head, skb->data, skb_tail_pointer(skb),
1131 skb_end_pointer(skb), skb->csum,
1132 skb->dev ? skb->dev->name : "<NULL>");
1133 }
1134
1135 /*
1136 * Save source MAC address before discarding header.
1137 */
1138 fc = lport_priv(lp);
1139 if (unlikely(fc->flogi_progress))
1140 mac = eth_hdr(skb)->h_source;
1141
1142 if (skb_is_nonlinear(skb))
1143 skb_linearize(skb); /* not ideal */
1144
1145 /*
1146 * Frame length checks and setting up the header pointers
1147 * was done in fcoe_rcv already.
1148 */
1149 hp = (struct fcoe_hdr *) skb_network_header(skb);
1150 fh = (struct fc_frame_header *) skb_transport_header(skb);
1151
Robert Love582b45b2009-03-31 15:51:50 -07001152 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001153 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001154 if (stats->ErrorFrames < 5)
1155 printk(KERN_WARNING "FCoE version "
1156 "mismatch: The frame has "
1157 "version %x, but the "
1158 "initiator supports version "
1159 "%x\n", FC_FCOE_DECAPS_VER(hp),
1160 FC_FCOE_VER);
1161 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001162 kfree_skb(skb);
1163 continue;
1164 }
1165
1166 skb_pull(skb, sizeof(struct fcoe_hdr));
1167 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1168
Robert Love582b45b2009-03-31 15:51:50 -07001169 stats->RxFrames++;
1170 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001171
1172 fp = (struct fc_frame *)skb;
1173 fc_frame_init(fp);
1174 fr_dev(fp) = lp;
1175 fr_sof(fp) = hp->fcoe_sof;
1176
1177 /* Copy out the CRC and EOF trailer for access */
1178 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1179 kfree_skb(skb);
1180 continue;
1181 }
1182 fr_eof(fp) = crc_eof.fcoe_eof;
1183 fr_crc(fp) = crc_eof.fcoe_crc32;
1184 if (pskb_trim(skb, fr_len)) {
1185 kfree_skb(skb);
1186 continue;
1187 }
1188
1189 /*
1190 * We only check CRC if no offload is available and if it is
1191 * it's solicited data, in which case, the FCP layer would
1192 * check it during the copy.
1193 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001194 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001195 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1196 else
1197 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1198
1199 fh = fc_frame_header_get(fp);
1200 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1201 fh->fh_type == FC_TYPE_FCP) {
1202 fc_exch_recv(lp, lp->emp, fp);
1203 continue;
1204 }
1205 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1206 if (le32_to_cpu(fr_crc(fp)) !=
1207 ~crc32(~0, skb->data, fr_len)) {
1208 if (debug_fcoe || stats->InvalidCRCCount < 5)
1209 printk(KERN_WARNING "fcoe: dropping "
1210 "frame with CRC error\n");
1211 stats->InvalidCRCCount++;
1212 stats->ErrorFrames++;
1213 fc_frame_free(fp);
1214 continue;
1215 }
1216 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1217 }
1218 /* non flogi and non data exchanges are handled here */
1219 if (unlikely(fc->flogi_progress))
1220 fcoe_recv_flogi(fc, fp, mac);
1221 fc_exch_recv(lp, lp->emp, fp);
1222 }
1223 return 0;
1224}
1225
1226/**
Robert Love34f42a02009-02-27 10:55:45 -08001227 * fcoe_recv_flogi() - flogi receive function
Robert Love85b4aa42008-12-09 15:10:24 -08001228 * @fc: associated fcoe_softc
1229 * @fp: the recieved frame
1230 * @sa: the source address of this flogi
1231 *
1232 * This is responsible to parse the flogi response and sets the corresponding
1233 * mac address for the initiator, eitehr OUI based or GW based.
1234 *
1235 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001236 */
Robert Love85b4aa42008-12-09 15:10:24 -08001237static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
1238{
1239 struct fc_frame_header *fh;
1240 u8 op;
1241
1242 fh = fc_frame_header_get(fp);
1243 if (fh->fh_type != FC_TYPE_ELS)
1244 return;
1245 op = fc_frame_payload_op(fp);
1246 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1247 fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
1248 /*
1249 * FLOGI accepted.
1250 * If the src mac addr is FC_OUI-based, then we mark the
1251 * address_mode flag to use FC_OUI-based Ethernet DA.
1252 * Otherwise we use the FCoE gateway addr
1253 */
1254 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
1255 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
1256 } else {
1257 memcpy(fc->dest_addr, sa, ETH_ALEN);
1258 fc->address_mode = FCOE_GW_ADDR_MODE;
1259 }
1260
1261 /*
1262 * Remove any previously-set unicast MAC filter.
1263 * Add secondary FCoE MAC address filter for our OUI.
1264 */
1265 rtnl_lock();
1266 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
1267 dev_unicast_delete(fc->real_dev, fc->data_src_addr,
1268 ETH_ALEN);
1269 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
1270 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
1271 rtnl_unlock();
1272
1273 fc->flogi_progress = 0;
1274 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1275 /*
1276 * Save source MAC for point-to-point responses.
1277 */
1278 memcpy(fc->dest_addr, sa, ETH_ALEN);
1279 fc->address_mode = FCOE_GW_ADDR_MODE;
1280 }
1281}
1282
1283/**
Robert Love34f42a02009-02-27 10:55:45 -08001284 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -08001285 * @vp:
1286 *
Vasu Devbc0e17f2009-02-27 10:54:57 -08001287 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -08001288 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1289 * fcoe_hostlist.
1290 *
1291 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001292 */
Robert Love85b4aa42008-12-09 15:10:24 -08001293void fcoe_watchdog(ulong vp)
1294{
Robert Love85b4aa42008-12-09 15:10:24 -08001295 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001296
1297 read_lock(&fcoe_hostlist_lock);
1298 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Devc826a312009-02-27 10:56:27 -08001299 if (fc->lp)
1300 fcoe_check_wait_queue(fc->lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001301 }
1302 read_unlock(&fcoe_hostlist_lock);
1303
1304 fcoe_timer.expires = jiffies + (1 * HZ);
1305 add_timer(&fcoe_timer);
1306}
1307
1308
1309/**
Robert Love34f42a02009-02-27 10:55:45 -08001310 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
Robert Love85b4aa42008-12-09 15:10:24 -08001311 * @lp: the fc_port for this skb
1312 * @skb: the associated skb to be xmitted
1313 *
1314 * This empties the wait_queue, dequeue the head of the wait_queue queue
1315 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001316 * transmitted, return qlen or -1 if a error occurs, then restore
1317 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001318 *
1319 * The wait_queue is used when the skb transmit fails. skb will go
1320 * in the wait_queue which will be emptied by the time function OR
1321 * by the next skb transmit.
1322 *
1323 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001324 */
Robert Love85b4aa42008-12-09 15:10:24 -08001325static int fcoe_check_wait_queue(struct fc_lport *lp)
1326{
Chris Leech55c8baf2009-02-27 10:56:32 -08001327 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001328 struct sk_buff *skb;
Vasu Devc826a312009-02-27 10:56:27 -08001329 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001330
Robert Love85b4aa42008-12-09 15:10:24 -08001331 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001332 if (fc->fcoe_pending_queue_active)
1333 goto out;
1334 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001335
1336 while (fc->fcoe_pending_queue.qlen) {
1337 /* keep qlen > 0 until fcoe_start_io succeeds */
1338 fc->fcoe_pending_queue.qlen++;
1339 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1340
1341 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1342 rc = fcoe_start_io(skb);
1343 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1344
1345 if (rc) {
1346 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1347 /* undo temporary increment above */
1348 fc->fcoe_pending_queue.qlen--;
1349 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001350 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001351 /* undo temporary increment above */
1352 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001353 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001354
1355 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1356 lp->qfull = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001357 fc->fcoe_pending_queue_active = 0;
1358 rc = fc->fcoe_pending_queue.qlen;
1359out:
Robert Love85b4aa42008-12-09 15:10:24 -08001360 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -08001361 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001362}
1363
1364/**
Robert Love34f42a02009-02-27 10:55:45 -08001365 * fcoe_dev_setup() - setup link change notification interface
1366 */
1367static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -08001368{
1369 /*
1370 * here setup a interface specific wd time to
1371 * monitor the link state
1372 */
1373 register_netdevice_notifier(&fcoe_notifier);
1374}
1375
1376/**
Robert Love34f42a02009-02-27 10:55:45 -08001377 * fcoe_dev_setup() - cleanup link change notification interface
1378 */
Robert Love85b4aa42008-12-09 15:10:24 -08001379static void fcoe_dev_cleanup(void)
1380{
1381 unregister_netdevice_notifier(&fcoe_notifier);
1382}
1383
1384/**
Robert Love34f42a02009-02-27 10:55:45 -08001385 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001386 * @notifier: context of the notification
1387 * @event: type of event
1388 * @ptr: fixed array for output parsed ifname
1389 *
1390 * This function is called by the ethernet driver in case of link change event
1391 *
1392 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001393 */
Robert Love85b4aa42008-12-09 15:10:24 -08001394static int fcoe_device_notification(struct notifier_block *notifier,
1395 ulong event, void *ptr)
1396{
1397 struct fc_lport *lp = NULL;
1398 struct net_device *real_dev = ptr;
1399 struct fcoe_softc *fc;
1400 struct fcoe_dev_stats *stats;
Vasu Devbc0e17f2009-02-27 10:54:57 -08001401 u32 new_link_up;
Robert Love85b4aa42008-12-09 15:10:24 -08001402 u32 mfs;
1403 int rc = NOTIFY_OK;
1404
1405 read_lock(&fcoe_hostlist_lock);
1406 list_for_each_entry(fc, &fcoe_hostlist, list) {
1407 if (fc->real_dev == real_dev) {
1408 lp = fc->lp;
1409 break;
1410 }
1411 }
1412 read_unlock(&fcoe_hostlist_lock);
1413 if (lp == NULL) {
1414 rc = NOTIFY_DONE;
1415 goto out;
1416 }
1417
Vasu Devbc0e17f2009-02-27 10:54:57 -08001418 new_link_up = lp->link_up;
Robert Love85b4aa42008-12-09 15:10:24 -08001419 switch (event) {
1420 case NETDEV_DOWN:
1421 case NETDEV_GOING_DOWN:
Vasu Devbc0e17f2009-02-27 10:54:57 -08001422 new_link_up = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001423 break;
1424 case NETDEV_UP:
1425 case NETDEV_CHANGE:
Vasu Devbc0e17f2009-02-27 10:54:57 -08001426 new_link_up = !fcoe_link_ok(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001427 break;
1428 case NETDEV_CHANGEMTU:
1429 mfs = fc->real_dev->mtu -
1430 (sizeof(struct fcoe_hdr) +
1431 sizeof(struct fcoe_crc_eof));
1432 if (mfs >= FC_MIN_MAX_FRAME)
1433 fc_set_mfs(lp, mfs);
Vasu Devbc0e17f2009-02-27 10:54:57 -08001434 new_link_up = !fcoe_link_ok(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001435 break;
1436 case NETDEV_REGISTER:
1437 break;
1438 default:
1439 FC_DBG("unknown event %ld call", event);
1440 }
Vasu Devbc0e17f2009-02-27 10:54:57 -08001441 if (lp->link_up != new_link_up) {
1442 if (new_link_up)
Robert Love85b4aa42008-12-09 15:10:24 -08001443 fc_linkup(lp);
1444 else {
Robert Love582b45b2009-03-31 15:51:50 -07001445 stats = fc_lport_get_stats(lp);
1446 stats->LinkFailureCount++;
Robert Love85b4aa42008-12-09 15:10:24 -08001447 fc_linkdown(lp);
1448 fcoe_clean_pending_queue(lp);
1449 }
1450 }
1451out:
1452 return rc;
1453}
1454
1455/**
Robert Love34f42a02009-02-27 10:55:45 -08001456 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001457 * @ifname: fixed array for output parsed ifname
1458 * @buffer: incoming buffer to be copied
1459 *
1460 * Returns: NULL or ptr to netdeive
Robert Love34f42a02009-02-27 10:55:45 -08001461 */
Robert Love85b4aa42008-12-09 15:10:24 -08001462static struct net_device *fcoe_if_to_netdev(const char *buffer)
1463{
1464 char *cp;
1465 char ifname[IFNAMSIZ + 2];
1466
1467 if (buffer) {
1468 strlcpy(ifname, buffer, IFNAMSIZ);
1469 cp = ifname + strlen(ifname);
1470 while (--cp >= ifname && *cp == '\n')
1471 *cp = '\0';
1472 return dev_get_by_name(&init_net, ifname);
1473 }
1474 return NULL;
1475}
1476
1477/**
Robert Love34f42a02009-02-27 10:55:45 -08001478 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001479 * @netdev: the target netdev
1480 *
1481 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001482 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001483static struct module *
1484fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001485{
1486 struct device *dev;
1487
1488 if (!netdev)
1489 return NULL;
1490
1491 dev = netdev->dev.parent;
1492 if (!dev)
1493 return NULL;
1494
1495 if (!dev->driver)
1496 return NULL;
1497
1498 return dev->driver->owner;
1499}
1500
1501/**
Robert Love34f42a02009-02-27 10:55:45 -08001502 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001503 * @netdev: the target netdev
1504 *
Robert Love34f42a02009-02-27 10:55:45 -08001505 * Holds the Ethernet driver module by try_module_get() for
1506 * the corresponding netdev.
1507 *
Robert Love85b4aa42008-12-09 15:10:24 -08001508 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -08001509 */
Robert Love85b4aa42008-12-09 15:10:24 -08001510static int fcoe_ethdrv_get(const struct net_device *netdev)
1511{
1512 struct module *owner;
1513
1514 owner = fcoe_netdev_to_module_owner(netdev);
1515 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001516 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1517 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001518 return try_module_get(owner);
1519 }
1520 return -ENODEV;
1521}
1522
1523/**
Robert Love34f42a02009-02-27 10:55:45 -08001524 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001525 * @netdev: the target netdev
1526 *
Robert Love34f42a02009-02-27 10:55:45 -08001527 * Releases the Ethernet driver module by module_put for
1528 * the corresponding netdev.
1529 *
Robert Love85b4aa42008-12-09 15:10:24 -08001530 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -08001531 */
Robert Love85b4aa42008-12-09 15:10:24 -08001532static int fcoe_ethdrv_put(const struct net_device *netdev)
1533{
1534 struct module *owner;
1535
1536 owner = fcoe_netdev_to_module_owner(netdev);
1537 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001538 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1539 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001540 module_put(owner);
1541 return 0;
1542 }
1543 return -ENODEV;
1544}
1545
1546/**
Robert Love34f42a02009-02-27 10:55:45 -08001547 * fcoe_destroy() - handles the destroy from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -08001548 * @buffer: expcted to be a eth if name
1549 * @kp: associated kernel param
1550 *
1551 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001552 */
Robert Love85b4aa42008-12-09 15:10:24 -08001553static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1554{
1555 int rc;
1556 struct net_device *netdev;
1557
1558 netdev = fcoe_if_to_netdev(buffer);
1559 if (!netdev) {
1560 rc = -ENODEV;
1561 goto out_nodev;
1562 }
1563 /* look for existing lport */
1564 if (!fcoe_hostlist_lookup(netdev)) {
1565 rc = -ENODEV;
1566 goto out_putdev;
1567 }
Vasu Dev7f349142009-03-27 09:06:31 -07001568 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001569 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001570 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001571 netdev->name);
1572 rc = -EIO;
1573 goto out_putdev;
1574 }
1575 fcoe_ethdrv_put(netdev);
1576 rc = 0;
1577out_putdev:
1578 dev_put(netdev);
1579out_nodev:
1580 return rc;
1581}
1582
1583/**
Robert Love34f42a02009-02-27 10:55:45 -08001584 * fcoe_create() - Handles the create call from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -08001585 * @buffer: expcted to be a eth if name
1586 * @kp: associated kernel param
1587 *
1588 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001589 */
Robert Love85b4aa42008-12-09 15:10:24 -08001590static int fcoe_create(const char *buffer, struct kernel_param *kp)
1591{
1592 int rc;
1593 struct net_device *netdev;
1594
1595 netdev = fcoe_if_to_netdev(buffer);
1596 if (!netdev) {
1597 rc = -ENODEV;
1598 goto out_nodev;
1599 }
1600 /* look for existing lport */
1601 if (fcoe_hostlist_lookup(netdev)) {
1602 rc = -EEXIST;
1603 goto out_putdev;
1604 }
1605 fcoe_ethdrv_get(netdev);
1606
Vasu Dev7f349142009-03-27 09:06:31 -07001607 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001608 if (rc) {
Vasu Dev7f349142009-03-27 09:06:31 -07001609 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001610 netdev->name);
1611 fcoe_ethdrv_put(netdev);
1612 rc = -EIO;
1613 goto out_putdev;
1614 }
1615 rc = 0;
1616out_putdev:
1617 dev_put(netdev);
1618out_nodev:
1619 return rc;
1620}
1621
1622module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1623__MODULE_PARM_TYPE(create, "string");
1624MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1625module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1626__MODULE_PARM_TYPE(destroy, "string");
1627MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1628
Robert Love34f42a02009-02-27 10:55:45 -08001629/**
1630 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001631 * @lp: ptr to the fc_lport
1632 *
1633 * Any permanently-disqualifying conditions have been previously checked.
1634 * This also updates the speed setting, which may change with link for 100/1000.
1635 *
1636 * This function should probably be checking for PAUSE support at some point
1637 * in the future. Currently Per-priority-pause is not determinable using
1638 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1639 *
1640 * Returns: 0 if link is OK for use by FCoE.
1641 *
1642 */
1643int fcoe_link_ok(struct fc_lport *lp)
1644{
Robert Lovefc47ff62009-02-27 10:55:55 -08001645 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001646 struct net_device *dev = fc->real_dev;
1647 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1648 int rc = 0;
1649
1650 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1651 dev = fc->phys_dev;
1652 if (dev->ethtool_ops->get_settings) {
1653 dev->ethtool_ops->get_settings(dev, &ecmd);
1654 lp->link_supported_speeds &=
1655 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1656 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1657 SUPPORTED_1000baseT_Full))
1658 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1659 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1660 lp->link_supported_speeds |=
1661 FC_PORTSPEED_10GBIT;
1662 if (ecmd.speed == SPEED_1000)
1663 lp->link_speed = FC_PORTSPEED_1GBIT;
1664 if (ecmd.speed == SPEED_10000)
1665 lp->link_speed = FC_PORTSPEED_10GBIT;
1666 }
1667 } else
1668 rc = -1;
1669
1670 return rc;
1671}
1672EXPORT_SYMBOL_GPL(fcoe_link_ok);
1673
Robert Love34f42a02009-02-27 10:55:45 -08001674/**
1675 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001676 * @lp: the fc_lport
1677 */
1678void fcoe_percpu_clean(struct fc_lport *lp)
1679{
Robert Love85b4aa42008-12-09 15:10:24 -08001680 struct fcoe_percpu_s *pp;
1681 struct fcoe_rcv_info *fr;
1682 struct sk_buff_head *list;
1683 struct sk_buff *skb, *next;
1684 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001685 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001686
Robert Love5e5e92d2009-03-17 11:41:35 -07001687 for_each_possible_cpu(cpu) {
1688 pp = &per_cpu(fcoe_percpu, cpu);
1689 spin_lock_bh(&pp->fcoe_rx_list.lock);
1690 list = &pp->fcoe_rx_list;
1691 head = list->next;
1692 for (skb = head; skb != (struct sk_buff *)list;
1693 skb = next) {
1694 next = skb->next;
1695 fr = fcoe_dev_from_skb(skb);
1696 if (fr->fr_dev == lp) {
1697 __skb_unlink(skb, list);
1698 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001699 }
Robert Love85b4aa42008-12-09 15:10:24 -08001700 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001701 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001702 }
1703}
1704EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1705
1706/**
Robert Love34f42a02009-02-27 10:55:45 -08001707 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001708 * @lp: the corresponding fc_lport
1709 *
1710 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001711 */
Robert Love85b4aa42008-12-09 15:10:24 -08001712void fcoe_clean_pending_queue(struct fc_lport *lp)
1713{
1714 struct fcoe_softc *fc = lport_priv(lp);
1715 struct sk_buff *skb;
1716
1717 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1718 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1719 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1720 kfree_skb(skb);
1721 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1722 }
1723 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1724}
1725EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1726
1727/**
Robert Love34f42a02009-02-27 10:55:45 -08001728 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001729 * @sht: ptr to the scsi host templ
1730 * @priv_size: size of private data after fc_lport
1731 *
1732 * Returns: ptr to Scsi_Host
Robert Love34f42a02009-02-27 10:55:45 -08001733 * TODO: to libfc?
Robert Love85b4aa42008-12-09 15:10:24 -08001734 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001735static inline struct Scsi_Host *
1736libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
Robert Love85b4aa42008-12-09 15:10:24 -08001737{
1738 return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1739}
1740
1741/**
Robert Love34f42a02009-02-27 10:55:45 -08001742 * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
Robert Love85b4aa42008-12-09 15:10:24 -08001743 * @sht: ptr to the scsi host templ
1744 * @priv_size: size of private data after fc_lport
1745 *
1746 * Returns: ptr to Scsi_Host
1747 */
1748struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1749{
1750 return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1751}
1752EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1753
Robert Love34f42a02009-02-27 10:55:45 -08001754/**
1755 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001756 * @shost: shost the reset is from
1757 *
1758 * Returns: always 0
1759 */
1760int fcoe_reset(struct Scsi_Host *shost)
1761{
1762 struct fc_lport *lport = shost_priv(shost);
1763 fc_lport_reset(lport);
1764 return 0;
1765}
1766EXPORT_SYMBOL_GPL(fcoe_reset);
1767
Robert Love34f42a02009-02-27 10:55:45 -08001768/**
1769 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
Robert Love85b4aa42008-12-09 15:10:24 -08001770 * @mac: mac address
1771 * @scheme: check port
1772 * @port: port indicator for converting
1773 *
1774 * Returns: u64 fc world wide name
1775 */
1776u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1777 unsigned int scheme, unsigned int port)
1778{
1779 u64 wwn;
1780 u64 host_mac;
1781
1782 /* The MAC is in NO, so flip only the low 48 bits */
1783 host_mac = ((u64) mac[0] << 40) |
1784 ((u64) mac[1] << 32) |
1785 ((u64) mac[2] << 24) |
1786 ((u64) mac[3] << 16) |
1787 ((u64) mac[4] << 8) |
1788 (u64) mac[5];
1789
1790 WARN_ON(host_mac >= (1ULL << 48));
1791 wwn = host_mac | ((u64) scheme << 60);
1792 switch (scheme) {
1793 case 1:
1794 WARN_ON(port != 0);
1795 break;
1796 case 2:
1797 WARN_ON(port >= 0xfff);
1798 wwn |= (u64) port << 48;
1799 break;
1800 default:
1801 WARN_ON(1);
1802 break;
1803 }
1804
1805 return wwn;
1806}
1807EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
Robert Love34f42a02009-02-27 10:55:45 -08001808
1809/**
1810 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Robert Love85b4aa42008-12-09 15:10:24 -08001811 * @device: this is currently ptr to net_device
1812 *
1813 * Returns: NULL or the located fcoe_softc
1814 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001815static struct fcoe_softc *
1816fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001817{
1818 struct fcoe_softc *fc;
1819
1820 read_lock(&fcoe_hostlist_lock);
1821 list_for_each_entry(fc, &fcoe_hostlist, list) {
1822 if (fc->real_dev == dev) {
1823 read_unlock(&fcoe_hostlist_lock);
1824 return fc;
1825 }
1826 }
1827 read_unlock(&fcoe_hostlist_lock);
1828 return NULL;
1829}
1830
Robert Love34f42a02009-02-27 10:55:45 -08001831/**
1832 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001833 * @netdev: ptr to net_device
1834 *
1835 * Returns: 0 for success
1836 */
1837struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1838{
1839 struct fcoe_softc *fc;
1840
1841 fc = fcoe_hostlist_lookup_softc(netdev);
1842
1843 return (fc) ? fc->lp : NULL;
1844}
1845EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1846
Robert Love34f42a02009-02-27 10:55:45 -08001847/**
1848 * fcoe_hostlist_add() - Add a lport to lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001849 * @lp: ptr to the fc_lport to badded
1850 *
1851 * Returns: 0 for success
1852 */
1853int fcoe_hostlist_add(const struct fc_lport *lp)
1854{
1855 struct fcoe_softc *fc;
1856
1857 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1858 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001859 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001860 write_lock_bh(&fcoe_hostlist_lock);
1861 list_add_tail(&fc->list, &fcoe_hostlist);
1862 write_unlock_bh(&fcoe_hostlist_lock);
1863 }
1864 return 0;
1865}
1866EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1867
Robert Love34f42a02009-02-27 10:55:45 -08001868/**
1869 * fcoe_hostlist_remove() - remove a lport from lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001870 * @lp: ptr to the fc_lport to badded
1871 *
1872 * Returns: 0 for success
1873 */
1874int fcoe_hostlist_remove(const struct fc_lport *lp)
1875{
1876 struct fcoe_softc *fc;
1877
1878 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1879 BUG_ON(!fc);
1880 write_lock_bh(&fcoe_hostlist_lock);
1881 list_del(&fc->list);
1882 write_unlock_bh(&fcoe_hostlist_lock);
1883
1884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1887
1888/**
Robert Love34f42a02009-02-27 10:55:45 -08001889 * fcoe_libfc_config() - sets up libfc related properties for lport
Robert Love85b4aa42008-12-09 15:10:24 -08001890 * @lp: ptr to the fc_lport
1891 * @tt: libfc function template
1892 *
1893 * Returns : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001894 */
Robert Love85b4aa42008-12-09 15:10:24 -08001895int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1896{
1897 /* Set the function pointers set by the LLDD */
1898 memcpy(&lp->tt, tt, sizeof(*tt));
1899 if (fc_fcp_init(lp))
1900 return -ENOMEM;
1901 fc_exch_init(lp);
1902 fc_elsct_init(lp);
1903 fc_lport_init(lp);
1904 fc_rport_init(lp);
1905 fc_disc_init(lp);
1906
1907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1910
1911/**
Robert Love34f42a02009-02-27 10:55:45 -08001912 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001913 *
Robert Love85b4aa42008-12-09 15:10:24 -08001914 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001915 */
Robert Love85b4aa42008-12-09 15:10:24 -08001916static int __init fcoe_init(void)
1917{
Robert Love38eccab2009-03-17 11:41:30 -07001918 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001919 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001920 struct fcoe_percpu_s *p;
1921
Robert Love85b4aa42008-12-09 15:10:24 -08001922 INIT_LIST_HEAD(&fcoe_hostlist);
1923 rwlock_init(&fcoe_hostlist_lock);
1924
Robert Love38eccab2009-03-17 11:41:30 -07001925 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001926 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001927 skb_queue_head_init(&p->fcoe_rx_list);
1928 }
1929
Robert Love8976f422009-03-17 11:41:46 -07001930 for_each_online_cpu(cpu)
1931 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001932
Robert Love8976f422009-03-17 11:41:46 -07001933 /* Initialize per CPU interrupt thread */
1934 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1935 if (rc)
1936 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001937
Robert Love8976f422009-03-17 11:41:46 -07001938 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001939 fcoe_dev_setup();
1940
Robert Lovea468f322009-02-27 10:56:00 -08001941 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1942
1943 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001944
Vasu Dev7f349142009-03-27 09:06:31 -07001945 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001946
1947 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001948
1949out_free:
1950 for_each_online_cpu(cpu) {
1951 fcoe_percpu_thread_destroy(cpu);
1952 }
1953
1954 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001955}
1956module_init(fcoe_init);
1957
1958/**
Robert Love34f42a02009-02-27 10:55:45 -08001959 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001960 *
1961 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001962 */
Robert Love85b4aa42008-12-09 15:10:24 -08001963static void __exit fcoe_exit(void)
1964{
Robert Love5e5e92d2009-03-17 11:41:35 -07001965 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001966 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001967
Robert Love85b4aa42008-12-09 15:10:24 -08001968 fcoe_dev_cleanup();
1969
Robert Love582b45b2009-03-31 15:51:50 -07001970 /* Stop the timer */
Robert Love85b4aa42008-12-09 15:10:24 -08001971 del_timer_sync(&fcoe_timer);
1972
Vasu Dev5919a592009-03-27 09:03:29 -07001973 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001974 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001975 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001976
Robert Love8976f422009-03-17 11:41:46 -07001977 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1978
1979 for_each_online_cpu(cpu) {
1980 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001981 }
1982
Vasu Dev7f349142009-03-27 09:06:31 -07001983 /* detach from scsi transport */
1984 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001985}
1986module_exit(fcoe_exit);