2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
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.
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
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.
17 * Maintained at www.Open-FCoE.org
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>
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>
42 #include <scsi/fc/fc_encaps.h>
44 #include <scsi/libfc.h>
45 #include <scsi/fc_frame.h>
46 #include <scsi/libfcoe.h>
48 static int debug_fcoe;
50 #define FCOE_MAX_QUEUE_DEPTH 256
51 #define FCOE_LOW_QUEUE_DEPTH 32
53 /* destination address mode */
54 #define FCOE_GW_ADDR_MODE 0x00
55 #define FCOE_FCOUI_ADDR_MODE 0x01
57 #define FCOE_WORD_TO_BYTE 4
59 #define FCOE_VERSION "0.1"
60 #define FCOE_NAME "fcoe"
61 #define FCOE_VENDOR "Open-FCoE.org"
63 #define FCOE_MAX_LUN 255
64 #define FCOE_MAX_FCP_TARGET 256
66 #define FCOE_MAX_OUTSTANDING_COMMANDS 1024
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 */
71 MODULE_AUTHOR("Open-FCoE.org");
72 MODULE_DESCRIPTION("FCoE");
73 MODULE_LICENSE("GPL v2");
76 LIST_HEAD(fcoe_hostlist);
77 DEFINE_RWLOCK(fcoe_hostlist_lock);
78 DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
79 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
82 /* Function Prototyes */
83 static int fcoe_check_wait_queue(struct fc_lport *);
84 static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
85 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86 static void fcoe_dev_setup(void);
87 static void fcoe_dev_cleanup(void);
89 /* notification function from net device */
90 static struct notifier_block fcoe_notifier = {
91 .notifier_call = fcoe_device_notification,
94 static struct scsi_transport_template *scsi_transport_fcoe_sw;
96 struct 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,
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,
113 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
114 .show_rport_maxframe_size = 1,
115 .show_rport_supported_classes = 1,
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,
126 .terminate_rport_io = fc_rport_terminate_io,
129 static 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,
142 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
143 .use_clustering = ENABLE_CLUSTERING,
144 .sg_tablesize = SG_ALL,
145 .max_sectors = 0xffff,
149 * fcoe_lport_config() - sets up the fc_lport
150 * @lp: ptr to the fc_lport
151 * @shost: ptr to the parent scsi host
153 * Returns: 0 for success
155 static int fcoe_lport_config(struct fc_lport *lp)
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);
165 fc_lport_init_stats(lp);
167 /* lport fc_lport related configuration */
170 /* offload related configuration */
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
185 * Must be called after fcoe_lport_config() as it will use lport mutex
187 * Returns : 0 for success
189 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
193 struct fcoe_softc *fc;
194 u8 flogi_maddr[ETH_ALEN];
196 /* Setup lport private data to point to fcoe softc */
199 fc->real_dev = netdev;
200 fc->phys_dev = netdev;
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);
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)) {
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.
218 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
219 sizeof(struct fcoe_crc_eof));
220 if (fc_set_mfs(lp, mfs))
223 if (!fcoe_link_ok(lp))
226 /* offload features support */
227 if (fc->real_dev->features & NETIF_F_SG)
230 #ifdef NETIF_F_FCOE_CRC
231 if (netdev->features & NETIF_F_FCOE_CRC) {
233 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
238 if (netdev->features & NETIF_F_FSO) {
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);
245 if (netdev->fcoe_ddp_xid) {
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);
251 skb_queue_head_init(&fc->fcoe_pending_queue);
252 fc->fcoe_pending_queue_active = 0;
254 /* setup Source Mac Address */
255 memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
256 fc->real_dev->addr_len);
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);
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.
270 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
271 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
275 * setup the receive function from ethernet driver
276 * on the ethertype for the given device
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);
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
292 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
294 * Returns : 0 for success
296 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
301 /* lport scsi host config */
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;
309 /* add the new host to the SCSI-ml */
310 rc = scsi_add_host(lp->host, dev);
312 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
315 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
316 FCOE_NAME, FCOE_VERSION,
317 fcoe_netdev(lp)->name);
323 * fcoe_em_config() - allocates em for this lport
324 * @lp: the port that em is to allocated for
326 * Returns : 0 on success
328 static inline int fcoe_em_config(struct fc_lport *lp)
332 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
333 FCOE_MIN_XID, FCOE_MAX_XID);
341 * fcoe_if_destroy() - FCoE software HBA tear-down function
342 * @netdev: ptr to the associated net_device
344 * Returns: 0 if link is OK for use by FCoE.
346 static int fcoe_if_destroy(struct net_device *netdev)
348 struct fc_lport *lp = NULL;
349 struct fcoe_softc *fc;
350 u8 flogi_maddr[ETH_ALEN];
354 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
357 lp = fcoe_hostlist_lookup(netdev);
363 /* Logout of the fabric */
364 fc_fabric_logoff(lp);
366 /* Remove the instance from fcoe's list */
367 fcoe_hostlist_remove(lp);
369 /* Don't listen for Ethernet packets anymore */
370 dev_remove_pack(&fc->fcoe_packet_type);
372 /* Cleanup the fc_lport */
373 fc_lport_destroy(lp);
376 /* Detach from the scsi-ml */
377 fc_remove_host(lp->host);
378 scsi_remove_host(lp->host);
380 /* There are no more rports or I/O, free the EM */
382 fc_exch_mgr_free(lp->emp);
384 /* Delete secondary MAC addresses */
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);
392 /* Free the per-CPU revieve threads */
393 fcoe_percpu_clean(lp);
395 /* Free existing skbs */
396 fcoe_clean_pending_queue(lp);
398 /* Free memory used by statistical counters */
399 fc_lport_free_stats(lp);
401 /* Release the net_device and Scsi_Host */
402 dev_put(fc->real_dev);
403 scsi_host_put(lp->host);
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
417 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
418 struct scatterlist *sgl, unsigned int sgc)
420 struct net_device *n = fcoe_netdev(lp);
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);
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
433 * Returns : the length of data that have been completed by ddp
435 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
437 struct net_device *n = fcoe_netdev(lp);
439 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
440 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
444 static 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,
451 * fcoe_if_create() - this function creates the fcoe interface
452 * @netdev: pointer the associated netdevice
454 * Creates fc_lport struct and scsi_host for lport, configures lport
455 * and starts fabric login.
457 * Returns : 0 on success
459 static int fcoe_if_create(struct net_device *netdev)
462 struct fc_lport *lp = NULL;
463 struct fcoe_softc *fc;
464 struct Scsi_Host *shost;
468 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
471 lp = fcoe_hostlist_lookup(netdev);
475 shost = fcoe_host_alloc(&fcoe_shost_template,
476 sizeof(struct fcoe_softc));
478 FC_DBG("Could not allocate host structure\n");
481 lp = shost_priv(shost);
484 /* configure fc_lport, e.g., em */
485 rc = fcoe_lport_config(lp);
487 FC_DBG("Could not configure lport\n");
491 /* configure lport network properties */
492 rc = fcoe_netdev_config(lp, netdev);
494 FC_DBG("Could not configure netdev for lport\n");
498 /* configure lport scsi host properties */
499 rc = fcoe_shost_config(lp, shost, &netdev->dev);
501 FC_DBG("Could not configure shost for lport\n");
505 /* lport exch manager allocation */
506 rc = fcoe_em_config(lp);
508 FC_DBG("Could not configure em for lport\n");
512 /* Initialize the library */
513 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
515 FC_DBG("Could not configure libfc for lport!\n");
519 /* add to lports list */
520 fcoe_hostlist_add(lp);
522 lp->boot_time = jiffies;
531 fc_exch_mgr_free(lp->emp); /* Free the EM */
533 scsi_host_put(lp->host);
538 * fcoe_if_init() - attach to scsi transport
540 * Returns : 0 on success
542 static int __init fcoe_if_init(void)
544 /* attach to scsi transport */
545 scsi_transport_fcoe_sw =
546 fc_attach_transport(&fcoe_transport_function);
548 if (!scsi_transport_fcoe_sw) {
549 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
557 * fcoe_if_exit() - detach from scsi transport
559 * Returns : 0 on success
561 int __exit fcoe_if_exit(void)
563 fc_release_transport(scsi_transport_fcoe_sw);
568 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
569 * @cpu: cpu index for the online cpu
571 static void fcoe_percpu_thread_create(unsigned int cpu)
573 struct fcoe_percpu_s *p;
574 struct task_struct *thread;
576 p = &per_cpu(fcoe_percpu, cpu);
578 thread = kthread_create(fcoe_percpu_receive_thread,
579 (void *)p, "fcoethread/%d", cpu);
581 if (likely(!IS_ERR(p->thread))) {
582 kthread_bind(thread, cpu);
583 wake_up_process(thread);
585 spin_lock_bh(&p->fcoe_rx_list.lock);
587 spin_unlock_bh(&p->fcoe_rx_list.lock);
592 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
593 * @cpu: cpu index the rx thread is to be removed
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.
599 static void fcoe_percpu_thread_destroy(unsigned int cpu)
601 struct fcoe_percpu_s *p;
602 struct task_struct *thread;
603 struct page *crc_eof;
606 struct fcoe_percpu_s *p0;
607 unsigned targ_cpu = smp_processor_id();
608 #endif /* CONFIG_SMP */
610 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
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);
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);
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.
628 if (cpu != targ_cpu) {
629 p0 = &per_cpu(fcoe_percpu, targ_cpu);
630 spin_lock_bh(&p0->fcoe_rx_list.lock);
632 FC_DBG("Moving frames from CPU %d to CPU %d\n",
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);
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.
644 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
646 spin_unlock_bh(&p0->fcoe_rx_list.lock);
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
658 spin_lock_bh(&p->fcoe_rx_list.lock);
659 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
661 spin_unlock_bh(&p->fcoe_rx_list.lock);
665 * This a non-SMP scenario where the singluar Rx thread is
666 * being removed. Free all skbs and stop the thread.
668 spin_lock_bh(&p->fcoe_rx_list.lock);
669 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
671 spin_unlock_bh(&p->fcoe_rx_list.lock);
675 kthread_stop(thread);
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
687 * This creates or destroys per cpu data for fcoe
689 * Returns NOTIFY_OK always.
691 static int fcoe_cpu_callback(struct notifier_block *nfb,
692 unsigned long action, void *hcpu)
694 unsigned cpu = (unsigned long)hcpu;
698 case CPU_ONLINE_FROZEN:
699 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
700 fcoe_percpu_thread_create(cpu);
703 case CPU_DEAD_FROZEN:
704 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
705 fcoe_percpu_thread_destroy(cpu);
713 static struct notifier_block fcoe_cpu_notifier = {
714 .notifier_call = fcoe_cpu_callback,
718 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
719 * @skb: the receive skb
720 * @dev: associated net device
722 * @odldev: last device
724 * this function will receive the packet and build fc frame and pass it up
726 * Returns: 0 for success
728 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
729 struct packet_type *ptype, struct net_device *olddev)
732 struct fcoe_rcv_info *fr;
733 struct fcoe_softc *fc;
734 struct fc_frame_header *fh;
735 struct fcoe_percpu_s *fps;
737 unsigned int cpu = 0;
739 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
741 if (unlikely(lp == NULL)) {
742 FC_DBG("cannot find hba structure");
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>");
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");
762 * Check for minimum frame length, and make sure required FCoE
763 * and FC headers are pulled into the linear data area.
765 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
766 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
769 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
770 fh = (struct fc_frame_header *) skb_transport_header(skb);
772 oxid = ntohs(fh->fh_ox_id);
774 fr = fcoe_dev_from_skb(skb);
780 * The incoming frame exchange id(oxid) is ANDed with num of online
781 * cpu bits to get cpu and then this cpu is used for selecting
782 * a per cpu kernel thread from fcoe_percpu.
784 cpu = oxid & (num_online_cpus() - 1);
787 fps = &per_cpu(fcoe_percpu, cpu);
788 spin_lock_bh(&fps->fcoe_rx_list.lock);
789 if (unlikely(!fps->thread)) {
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.
795 FC_DBG("CPU is online, but no receive thread ready "
796 "for incoming skb- using first online CPU.\n");
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);
803 spin_unlock_bh(&fps->fcoe_rx_list.lock);
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.
813 __skb_queue_tail(&fps->fcoe_rx_list, skb);
814 if (fps->fcoe_rx_list.qlen == 1)
815 wake_up_process(fps->thread);
817 spin_unlock_bh(&fps->fcoe_rx_list.lock);
821 fc_lport_get_stats(lp)->ErrorFrames++;
827 EXPORT_SYMBOL_GPL(fcoe_rcv);
830 * fcoe_start_io() - pass to netdev to start xmit for fcoe
831 * @skb: the skb to be xmitted
833 * Returns: 0 for success
835 static inline int fcoe_start_io(struct sk_buff *skb)
840 rc = dev_queue_xmit(skb);
848 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
849 * @skb: the skb to be xmitted
852 * Returns: 0 for success
854 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
856 struct fcoe_percpu_s *fps;
859 fps = &get_cpu_var(fcoe_percpu);
860 page = fps->crc_eof_page;
862 page = alloc_page(GFP_ATOMIC);
864 put_cpu_var(fcoe_percpu);
867 fps->crc_eof_page = page;
868 fps->crc_eof_offset = 0;
872 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
873 fps->crc_eof_offset, tlen);
875 skb->data_len += tlen;
876 skb->truesize += tlen;
877 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
879 if (fps->crc_eof_offset >= PAGE_SIZE) {
880 fps->crc_eof_page = NULL;
881 fps->crc_eof_offset = 0;
884 put_cpu_var(fcoe_percpu);
889 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
890 * @fp: the fc_frame containg data to be checksummed
892 * This uses crc32() to calculate the crc for fc frame
893 * Return : 32 bit crc
895 u32 fcoe_fc_crc(struct fc_frame *fp)
897 struct sk_buff *skb = fp_skb(fp);
898 struct skb_frag_struct *frag;
900 unsigned long off, len, clen;
904 crc = crc32(~0, skb->data, skb_headlen(skb));
906 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
907 frag = &skb_shinfo(skb)->frags[i];
908 off = frag->page_offset;
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);
922 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
925 * fcoe_xmit() - FCoE frame transmit function
926 * @lp: the associated local port
927 * @fp: the fc_frame to be transmitted
929 * Return : 0 for success
931 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
936 struct fcoe_crc_eof *cp;
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;
948 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
952 * if it is a flogi then we need to learn gw-addr
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) {
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
970 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
971 fc->flogi_progress = 0;
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;
986 if (likely(lp->crc_offload)) {
987 skb->ip_summed = CHECKSUM_PARTIAL;
988 skb->csum_start = skb_headroom(skb);
989 skb->csum_offset = skb->len;
992 skb->ip_summed = CHECKSUM_NONE;
993 crc = fcoe_fc_crc(fp);
996 /* copy fc crc and eof to the skb buff */
997 if (skb_is_nonlinear(skb)) {
999 if (fcoe_get_paged_crc_eof(skb, tlen)) {
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;
1007 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1010 memset(cp, 0, sizeof(*cp));
1012 cp->fcoe_crc32 = cpu_to_le32(~crc);
1014 if (skb_is_nonlinear(skb)) {
1015 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
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;
1024 skb->protocol = htons(ETH_P_FCOE);
1025 skb->dev = fc->real_dev;
1027 /* fill up mac and fcoe headers */
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);
1033 /* insert GW address */
1034 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
1036 if (unlikely(flogi_in_progress))
1037 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
1039 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
1041 hp = (struct fcoe_hdr *)(eh + 1);
1042 memset(hp, 0, sizeof(*hp));
1044 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
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);
1053 skb_shinfo(skb)->gso_type = 0;
1054 skb_shinfo(skb)->gso_size = 0;
1057 /* update tx stats: regardless if LLD fails */
1058 stats = fc_lport_get_stats(lp);
1060 stats->TxWords += wlen;
1062 /* send down to lld */
1064 if (fc->fcoe_pending_queue.qlen)
1065 rc = fcoe_check_wait_queue(lp);
1068 rc = fcoe_start_io(skb);
1071 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);
1074 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1080 EXPORT_SYMBOL_GPL(fcoe_xmit);
1083 * fcoe_percpu_receive_thread() - recv thread per cpu
1084 * @arg: ptr to the fcoe per cpu struct
1086 * Return: 0 for success
1088 int fcoe_percpu_receive_thread(void *arg)
1090 struct fcoe_percpu_s *p = arg;
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;
1100 struct fcoe_softc *fc;
1101 struct fcoe_hdr *hp;
1103 set_user_nice(current, -20);
1105 while (!kthread_should_stop()) {
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);
1112 set_current_state(TASK_RUNNING);
1113 if (kthread_should_stop())
1115 spin_lock_bh(&p->fcoe_rx_list.lock);
1117 spin_unlock_bh(&p->fcoe_rx_list.lock);
1118 fr = fcoe_dev_from_skb(skb);
1120 if (unlikely(lp == NULL)) {
1121 FC_DBG("invalid HBA Structure");
1126 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>");
1136 * Save source MAC address before discarding header.
1138 fc = lport_priv(lp);
1139 if (unlikely(fc->flogi_progress))
1140 mac = eth_hdr(skb)->h_source;
1142 if (skb_is_nonlinear(skb))
1143 skb_linearize(skb); /* not ideal */
1146 * Frame length checks and setting up the header pointers
1147 * was done in fcoe_rcv already.
1149 hp = (struct fcoe_hdr *) skb_network_header(skb);
1150 fh = (struct fc_frame_header *) skb_transport_header(skb);
1152 stats = fc_lport_get_stats(lp);
1153 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1154 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),
1161 stats->ErrorFrames++;
1166 skb_pull(skb, sizeof(struct fcoe_hdr));
1167 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1170 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1172 fp = (struct fc_frame *)skb;
1175 fr_sof(fp) = hp->fcoe_sof;
1177 /* Copy out the CRC and EOF trailer for access */
1178 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1182 fr_eof(fp) = crc_eof.fcoe_eof;
1183 fr_crc(fp) = crc_eof.fcoe_crc32;
1184 if (pskb_trim(skb, fr_len)) {
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.
1194 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1195 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1197 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
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);
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++;
1216 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
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);
1227 * fcoe_recv_flogi() - flogi receive function
1228 * @fc: associated fcoe_softc
1229 * @fp: the recieved frame
1230 * @sa: the source address of this flogi
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.
1237 static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
1239 struct fc_frame_header *fh;
1242 fh = fc_frame_header_get(fp);
1243 if (fh->fh_type != FC_TYPE_ELS)
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)) {
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
1254 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
1255 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
1257 memcpy(fc->dest_addr, sa, ETH_ALEN);
1258 fc->address_mode = FCOE_GW_ADDR_MODE;
1262 * Remove any previously-set unicast MAC filter.
1263 * Add secondary FCoE MAC address filter for our OUI.
1266 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
1267 dev_unicast_delete(fc->real_dev, fc->data_src_addr,
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);
1273 fc->flogi_progress = 0;
1274 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1276 * Save source MAC for point-to-point responses.
1278 memcpy(fc->dest_addr, sa, ETH_ALEN);
1279 fc->address_mode = FCOE_GW_ADDR_MODE;
1284 * fcoe_watchdog() - fcoe timer callback
1287 * This checks the pending queue length for fcoe and set lport qfull
1288 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1291 * Returns: 0 for success
1293 void fcoe_watchdog(ulong vp)
1295 struct fcoe_softc *fc;
1297 read_lock(&fcoe_hostlist_lock);
1298 list_for_each_entry(fc, &fcoe_hostlist, list) {
1300 fcoe_check_wait_queue(fc->lp);
1302 read_unlock(&fcoe_hostlist_lock);
1304 fcoe_timer.expires = jiffies + (1 * HZ);
1305 add_timer(&fcoe_timer);
1310 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
1311 * @lp: the fc_port for this skb
1312 * @skb: the associated skb to be xmitted
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
1316 * transmitted, return qlen or -1 if a error occurs, then restore
1317 * wait_queue and try again later.
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.
1323 * Returns: 0 for success
1325 static int fcoe_check_wait_queue(struct fc_lport *lp)
1327 struct fcoe_softc *fc = lport_priv(lp);
1328 struct sk_buff *skb;
1331 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1332 if (fc->fcoe_pending_queue_active)
1334 fc->fcoe_pending_queue_active = 1;
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);
1341 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1342 rc = fcoe_start_io(skb);
1343 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1346 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1347 /* undo temporary increment above */
1348 fc->fcoe_pending_queue.qlen--;
1351 /* undo temporary increment above */
1352 fc->fcoe_pending_queue.qlen--;
1355 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1357 fc->fcoe_pending_queue_active = 0;
1358 rc = fc->fcoe_pending_queue.qlen;
1360 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1365 * fcoe_dev_setup() - setup link change notification interface
1367 static void fcoe_dev_setup()
1370 * here setup a interface specific wd time to
1371 * monitor the link state
1373 register_netdevice_notifier(&fcoe_notifier);
1377 * fcoe_dev_setup() - cleanup link change notification interface
1379 static void fcoe_dev_cleanup(void)
1381 unregister_netdevice_notifier(&fcoe_notifier);
1385 * fcoe_device_notification() - netdev event notification callback
1386 * @notifier: context of the notification
1387 * @event: type of event
1388 * @ptr: fixed array for output parsed ifname
1390 * This function is called by the ethernet driver in case of link change event
1392 * Returns: 0 for success
1394 static int fcoe_device_notification(struct notifier_block *notifier,
1395 ulong event, void *ptr)
1397 struct fc_lport *lp = NULL;
1398 struct net_device *real_dev = ptr;
1399 struct fcoe_softc *fc;
1400 struct fcoe_dev_stats *stats;
1405 read_lock(&fcoe_hostlist_lock);
1406 list_for_each_entry(fc, &fcoe_hostlist, list) {
1407 if (fc->real_dev == real_dev) {
1412 read_unlock(&fcoe_hostlist_lock);
1418 new_link_up = lp->link_up;
1421 case NETDEV_GOING_DOWN:
1426 new_link_up = !fcoe_link_ok(lp);
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);
1434 new_link_up = !fcoe_link_ok(lp);
1436 case NETDEV_REGISTER:
1439 FC_DBG("unknown event %ld call", event);
1441 if (lp->link_up != new_link_up) {
1445 stats = fc_lport_get_stats(lp);
1446 stats->LinkFailureCount++;
1448 fcoe_clean_pending_queue(lp);
1456 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1457 * @ifname: fixed array for output parsed ifname
1458 * @buffer: incoming buffer to be copied
1460 * Returns: NULL or ptr to netdeive
1462 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1465 char ifname[IFNAMSIZ + 2];
1468 strlcpy(ifname, buffer, IFNAMSIZ);
1469 cp = ifname + strlen(ifname);
1470 while (--cp >= ifname && *cp == '\n')
1472 return dev_get_by_name(&init_net, ifname);
1478 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
1479 * @netdev: the target netdev
1481 * Returns: ptr to the struct module, NULL for failure
1483 static struct module *
1484 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1491 dev = netdev->dev.parent;
1498 return dev->driver->owner;
1502 * fcoe_ethdrv_get() - Hold the Ethernet driver
1503 * @netdev: the target netdev
1505 * Holds the Ethernet driver module by try_module_get() for
1506 * the corresponding netdev.
1508 * Returns: 0 for succsss
1510 static int fcoe_ethdrv_get(const struct net_device *netdev)
1512 struct module *owner;
1514 owner = fcoe_netdev_to_module_owner(netdev);
1516 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1517 module_name(owner), netdev->name);
1518 return try_module_get(owner);
1524 * fcoe_ethdrv_put() - Release the Ethernet driver
1525 * @netdev: the target netdev
1527 * Releases the Ethernet driver module by module_put for
1528 * the corresponding netdev.
1530 * Returns: 0 for succsss
1532 static int fcoe_ethdrv_put(const struct net_device *netdev)
1534 struct module *owner;
1536 owner = fcoe_netdev_to_module_owner(netdev);
1538 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1539 module_name(owner), netdev->name);
1547 * fcoe_destroy() - handles the destroy from sysfs
1548 * @buffer: expcted to be a eth if name
1549 * @kp: associated kernel param
1551 * Returns: 0 for success
1553 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1556 struct net_device *netdev;
1558 netdev = fcoe_if_to_netdev(buffer);
1563 /* look for existing lport */
1564 if (!fcoe_hostlist_lookup(netdev)) {
1568 rc = fcoe_if_destroy(netdev);
1570 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
1575 fcoe_ethdrv_put(netdev);
1584 * fcoe_create() - Handles the create call from sysfs
1585 * @buffer: expcted to be a eth if name
1586 * @kp: associated kernel param
1588 * Returns: 0 for success
1590 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1593 struct net_device *netdev;
1595 netdev = fcoe_if_to_netdev(buffer);
1600 /* look for existing lport */
1601 if (fcoe_hostlist_lookup(netdev)) {
1605 fcoe_ethdrv_get(netdev);
1607 rc = fcoe_if_create(netdev);
1609 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
1611 fcoe_ethdrv_put(netdev);
1622 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1623 __MODULE_PARM_TYPE(create, "string");
1624 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1625 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1626 __MODULE_PARM_TYPE(destroy, "string");
1627 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1630 * fcoe_link_ok() - Check if link is ok for the fc_lport
1631 * @lp: ptr to the fc_lport
1633 * Any permanently-disqualifying conditions have been previously checked.
1634 * This also updates the speed setting, which may change with link for 100/1000.
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.
1640 * Returns: 0 if link is OK for use by FCoE.
1643 int fcoe_link_ok(struct fc_lport *lp)
1645 struct fcoe_softc *fc = lport_priv(lp);
1646 struct net_device *dev = fc->real_dev;
1647 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1650 if ((dev->flags & IFF_UP) && netif_carrier_ok(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;
1672 EXPORT_SYMBOL_GPL(fcoe_link_ok);
1675 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1678 void fcoe_percpu_clean(struct fc_lport *lp)
1680 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;
1687 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;
1692 for (skb = head; skb != (struct sk_buff *)list;
1695 fr = fcoe_dev_from_skb(skb);
1696 if (fr->fr_dev == lp) {
1697 __skb_unlink(skb, list);
1701 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1704 EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1707 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1708 * @lp: the corresponding fc_lport
1712 void fcoe_clean_pending_queue(struct fc_lport *lp)
1714 struct fcoe_softc *fc = lport_priv(lp);
1715 struct sk_buff *skb;
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);
1721 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1723 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1725 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1728 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
1729 * @sht: ptr to the scsi host templ
1730 * @priv_size: size of private data after fc_lport
1732 * Returns: ptr to Scsi_Host
1735 static inline struct Scsi_Host *
1736 libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
1738 return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1742 * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
1743 * @sht: ptr to the scsi host templ
1744 * @priv_size: size of private data after fc_lport
1746 * Returns: ptr to Scsi_Host
1748 struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1750 return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1752 EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1755 * fcoe_reset() - Resets the fcoe
1756 * @shost: shost the reset is from
1760 int fcoe_reset(struct Scsi_Host *shost)
1762 struct fc_lport *lport = shost_priv(shost);
1763 fc_lport_reset(lport);
1766 EXPORT_SYMBOL_GPL(fcoe_reset);
1769 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1771 * @scheme: check port
1772 * @port: port indicator for converting
1774 * Returns: u64 fc world wide name
1776 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1777 unsigned int scheme, unsigned int port)
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) |
1790 WARN_ON(host_mac >= (1ULL << 48));
1791 wwn = host_mac | ((u64) scheme << 60);
1797 WARN_ON(port >= 0xfff);
1798 wwn |= (u64) port << 48;
1807 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1810 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1811 * @device: this is currently ptr to net_device
1813 * Returns: NULL or the located fcoe_softc
1815 static struct fcoe_softc *
1816 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1818 struct fcoe_softc *fc;
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);
1827 read_unlock(&fcoe_hostlist_lock);
1832 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1833 * @netdev: ptr to net_device
1835 * Returns: 0 for success
1837 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1839 struct fcoe_softc *fc;
1841 fc = fcoe_hostlist_lookup_softc(netdev);
1843 return (fc) ? fc->lp : NULL;
1845 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1848 * fcoe_hostlist_add() - Add a lport to lports list
1849 * @lp: ptr to the fc_lport to badded
1851 * Returns: 0 for success
1853 int fcoe_hostlist_add(const struct fc_lport *lp)
1855 struct fcoe_softc *fc;
1857 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1859 fc = lport_priv(lp);
1860 write_lock_bh(&fcoe_hostlist_lock);
1861 list_add_tail(&fc->list, &fcoe_hostlist);
1862 write_unlock_bh(&fcoe_hostlist_lock);
1866 EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1869 * fcoe_hostlist_remove() - remove a lport from lports list
1870 * @lp: ptr to the fc_lport to badded
1872 * Returns: 0 for success
1874 int fcoe_hostlist_remove(const struct fc_lport *lp)
1876 struct fcoe_softc *fc;
1878 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1880 write_lock_bh(&fcoe_hostlist_lock);
1881 list_del(&fc->list);
1882 write_unlock_bh(&fcoe_hostlist_lock);
1886 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1889 * fcoe_libfc_config() - sets up libfc related properties for lport
1890 * @lp: ptr to the fc_lport
1891 * @tt: libfc function template
1893 * Returns : 0 for success
1895 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1897 /* Set the function pointers set by the LLDD */
1898 memcpy(&lp->tt, tt, sizeof(*tt));
1899 if (fc_fcp_init(lp))
1909 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1912 * fcoe_init() - fcoe module loading initialization
1914 * Returns 0 on success, negative on failure
1916 static int __init fcoe_init(void)
1920 struct fcoe_percpu_s *p;
1922 INIT_LIST_HEAD(&fcoe_hostlist);
1923 rwlock_init(&fcoe_hostlist_lock);
1925 for_each_possible_cpu(cpu) {
1926 p = &per_cpu(fcoe_percpu, cpu);
1927 skb_queue_head_init(&p->fcoe_rx_list);
1930 for_each_online_cpu(cpu)
1931 fcoe_percpu_thread_create(cpu);
1933 /* Initialize per CPU interrupt thread */
1934 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1938 /* Setup link change notification */
1941 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1943 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
1950 for_each_online_cpu(cpu) {
1951 fcoe_percpu_thread_destroy(cpu);
1956 module_init(fcoe_init);
1959 * fcoe_exit() - fcoe module unloading cleanup
1961 * Returns 0 on success, negative on failure
1963 static void __exit fcoe_exit(void)
1966 struct fcoe_softc *fc, *tmp;
1970 /* Stop the timer */
1971 del_timer_sync(&fcoe_timer);
1973 /* releases the associated fcoe hosts */
1974 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1975 fcoe_if_destroy(fc->real_dev);
1977 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1979 for_each_online_cpu(cpu) {
1980 fcoe_percpu_thread_destroy(cpu);
1983 /* detach from scsi transport */
1986 module_exit(fcoe_exit);