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/spinlock.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/crc32.h>
29 #include <linux/cpu.h>
31 #include <linux/sysfs.h>
32 #include <linux/ctype.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsicam.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <net/rtnetlink.h>
39 #include <scsi/fc/fc_encaps.h>
40 #include <scsi/fc/fc_fip.h>
42 #include <scsi/libfc.h>
43 #include <scsi/fc_frame.h>
44 #include <scsi/libfcoe.h>
48 static int debug_fcoe
;
50 MODULE_AUTHOR("Open-FCoE.org");
51 MODULE_DESCRIPTION("FCoE");
52 MODULE_LICENSE("GPL v2");
55 LIST_HEAD(fcoe_hostlist
);
56 DEFINE_RWLOCK(fcoe_hostlist_lock
);
57 DEFINE_PER_CPU(struct fcoe_percpu_s
, fcoe_percpu
);
59 /* Function Prototypes */
60 static int fcoe_reset(struct Scsi_Host
*shost
);
61 static int fcoe_xmit(struct fc_lport
*, struct fc_frame
*);
62 static int fcoe_rcv(struct sk_buff
*, struct net_device
*,
63 struct packet_type
*, struct net_device
*);
64 static int fcoe_percpu_receive_thread(void *arg
);
65 static void fcoe_clean_pending_queue(struct fc_lport
*lp
);
66 static void fcoe_percpu_clean(struct fc_lport
*lp
);
67 static int fcoe_link_ok(struct fc_lport
*lp
);
69 static struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*);
70 static int fcoe_hostlist_add(const struct fc_lport
*);
71 static int fcoe_hostlist_remove(const struct fc_lport
*);
73 static void fcoe_check_wait_queue(struct fc_lport
*, struct sk_buff
*);
74 static int fcoe_device_notification(struct notifier_block
*, ulong
, void *);
75 static void fcoe_dev_setup(void);
76 static void fcoe_dev_cleanup(void);
78 /* notification function from net device */
79 static struct notifier_block fcoe_notifier
= {
80 .notifier_call
= fcoe_device_notification
,
83 static struct scsi_transport_template
*scsi_transport_fcoe_sw
;
85 struct fc_function_template fcoe_transport_function
= {
86 .show_host_node_name
= 1,
87 .show_host_port_name
= 1,
88 .show_host_supported_classes
= 1,
89 .show_host_supported_fc4s
= 1,
90 .show_host_active_fc4s
= 1,
91 .show_host_maxframe_size
= 1,
93 .show_host_port_id
= 1,
94 .show_host_supported_speeds
= 1,
95 .get_host_speed
= fc_get_host_speed
,
97 .show_host_port_type
= 1,
98 .get_host_port_state
= fc_get_host_port_state
,
99 .show_host_port_state
= 1,
100 .show_host_symbolic_name
= 1,
102 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
103 .show_rport_maxframe_size
= 1,
104 .show_rport_supported_classes
= 1,
106 .show_host_fabric_name
= 1,
107 .show_starget_node_name
= 1,
108 .show_starget_port_name
= 1,
109 .show_starget_port_id
= 1,
110 .set_rport_dev_loss_tmo
= fc_set_rport_loss_tmo
,
111 .show_rport_dev_loss_tmo
= 1,
112 .get_fc_host_stats
= fc_get_host_stats
,
113 .issue_fc_host_lip
= fcoe_reset
,
115 .terminate_rport_io
= fc_rport_terminate_io
,
118 static struct scsi_host_template fcoe_shost_template
= {
119 .module
= THIS_MODULE
,
120 .name
= "FCoE Driver",
121 .proc_name
= FCOE_NAME
,
122 .queuecommand
= fc_queuecommand
,
123 .eh_abort_handler
= fc_eh_abort
,
124 .eh_device_reset_handler
= fc_eh_device_reset
,
125 .eh_host_reset_handler
= fc_eh_host_reset
,
126 .slave_alloc
= fc_slave_alloc
,
127 .change_queue_depth
= fc_change_queue_depth
,
128 .change_queue_type
= fc_change_queue_type
,
131 .can_queue
= FCOE_MAX_OUTSTANDING_COMMANDS
,
132 .use_clustering
= ENABLE_CLUSTERING
,
133 .sg_tablesize
= SG_ALL
,
134 .max_sectors
= 0xffff,
138 * fcoe_fip_recv - handle a received FIP frame.
139 * @skb: the receive skb
140 * @dev: associated &net_device
141 * @ptype: the &packet_type structure which was used to register this handler.
142 * @orig_dev: original receive &net_device, in case @dev is a bond.
144 * Returns: 0 for success
146 static int fcoe_fip_recv(struct sk_buff
*skb
, struct net_device
*dev
,
147 struct packet_type
*ptype
,
148 struct net_device
*orig_dev
)
150 struct fcoe_softc
*fc
;
152 fc
= container_of(ptype
, struct fcoe_softc
, fip_packet_type
);
153 fcoe_ctlr_recv(&fc
->ctlr
, skb
);
158 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
159 * @fip: FCoE controller.
162 static void fcoe_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
164 skb
->dev
= fcoe_from_ctlr(fip
)->real_dev
;
169 * fcoe_update_src_mac() - Update Ethernet MAC filters.
170 * @fip: FCoE controller.
171 * @old: Unicast MAC address to delete if the MAC is non-zero.
172 * @new: Unicast MAC address to add.
174 * Remove any previously-set unicast MAC filter.
175 * Add secondary FCoE MAC address filter for our OUI.
177 static void fcoe_update_src_mac(struct fcoe_ctlr
*fip
, u8
*old
, u8
*new)
179 struct fcoe_softc
*fc
;
181 fc
= fcoe_from_ctlr(fip
);
183 if (!is_zero_ether_addr(old
))
184 dev_unicast_delete(fc
->real_dev
, old
);
185 dev_unicast_add(fc
->real_dev
, new);
190 * fcoe_lport_config() - sets up the fc_lport
191 * @lp: ptr to the fc_lport
193 * Returns: 0 for success
195 static int fcoe_lport_config(struct fc_lport
*lp
)
199 lp
->max_retry_count
= 3;
200 lp
->max_rport_retry_count
= 3;
201 lp
->e_d_tov
= 2 * 1000; /* FC-FS default */
202 lp
->r_a_tov
= 2 * 2 * 1000;
203 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
204 FCP_SPPF_RETRY
| FCP_SPPF_CONF_COMPL
);
206 fc_lport_init_stats(lp
);
208 /* lport fc_lport related configuration */
211 /* offload related configuration */
222 * fcoe_netdev_cleanup() - clean up netdev configurations
223 * @fc: ptr to the fcoe_softc
225 void fcoe_netdev_cleanup(struct fcoe_softc
*fc
)
227 u8 flogi_maddr
[ETH_ALEN
];
229 /* Don't listen for Ethernet packets anymore */
230 dev_remove_pack(&fc
->fcoe_packet_type
);
231 dev_remove_pack(&fc
->fip_packet_type
);
233 /* Delete secondary MAC addresses */
235 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
236 dev_unicast_delete(fc
->real_dev
, flogi_maddr
);
237 if (!is_zero_ether_addr(fc
->ctlr
.data_src_addr
))
238 dev_unicast_delete(fc
->real_dev
, fc
->ctlr
.data_src_addr
);
240 dev_unicast_delete(fc
->real_dev
, fc
->ctlr
.ctl_src_addr
);
241 dev_mc_delete(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
246 * fcoe_queue_timer() - fcoe queue timer
247 * @lp: the fc_lport pointer
249 * Calls fcoe_check_wait_queue on timeout
252 static void fcoe_queue_timer(ulong lp
)
254 fcoe_check_wait_queue((struct fc_lport
*)lp
, NULL
);
258 * fcoe_netdev_config() - Set up netdev for SW FCoE
259 * @lp : ptr to the fc_lport
260 * @netdev : ptr to the associated netdevice struct
262 * Must be called after fcoe_lport_config() as it will use lport mutex
264 * Returns : 0 for success
266 static int fcoe_netdev_config(struct fc_lport
*lp
, struct net_device
*netdev
)
270 struct fcoe_softc
*fc
;
271 u8 flogi_maddr
[ETH_ALEN
];
272 struct netdev_hw_addr
*ha
;
274 /* Setup lport private data to point to fcoe softc */
277 fc
->real_dev
= netdev
;
278 fc
->phys_dev
= netdev
;
280 /* Require support for get_pauseparam ethtool op. */
281 if (netdev
->priv_flags
& IFF_802_1Q_VLAN
)
282 fc
->phys_dev
= vlan_dev_real_dev(netdev
);
284 /* Do not support for bonding device */
285 if ((fc
->real_dev
->priv_flags
& IFF_MASTER_ALB
) ||
286 (fc
->real_dev
->priv_flags
& IFF_SLAVE_INACTIVE
) ||
287 (fc
->real_dev
->priv_flags
& IFF_MASTER_8023AD
)) {
292 * Determine max frame size based on underlying device and optional
293 * user-configured limit. If the MFS is too low, fcoe_link_ok()
294 * will return 0, so do this first.
296 mfs
= fc
->real_dev
->mtu
- (sizeof(struct fcoe_hdr
) +
297 sizeof(struct fcoe_crc_eof
));
298 if (fc_set_mfs(lp
, mfs
))
301 /* offload features support */
302 if (fc
->real_dev
->features
& NETIF_F_SG
)
305 #ifdef NETIF_F_FCOE_CRC
306 if (netdev
->features
& NETIF_F_FCOE_CRC
) {
308 printk(KERN_DEBUG
"fcoe:%s supports FCCRC offload\n",
313 if (netdev
->features
& NETIF_F_FSO
) {
315 lp
->lso_max
= netdev
->gso_max_size
;
316 printk(KERN_DEBUG
"fcoe:%s supports LSO for max len 0x%x\n",
317 netdev
->name
, lp
->lso_max
);
320 if (netdev
->fcoe_ddp_xid
) {
322 lp
->lro_xid
= netdev
->fcoe_ddp_xid
;
323 printk(KERN_DEBUG
"fcoe:%s supports LRO for max xid 0x%x\n",
324 netdev
->name
, lp
->lro_xid
);
326 skb_queue_head_init(&fc
->fcoe_pending_queue
);
327 fc
->fcoe_pending_queue_active
= 0;
328 setup_timer(&fc
->timer
, fcoe_queue_timer
, (unsigned long)lp
);
330 /* look for SAN MAC address, if multiple SAN MACs exist, only
331 * use the first one for SPMA */
333 for_each_dev_addr(netdev
, ha
) {
334 if ((ha
->type
== NETDEV_HW_ADDR_T_SAN
) &&
335 (is_valid_ether_addr(fc
->ctlr
.ctl_src_addr
))) {
336 memcpy(fc
->ctlr
.ctl_src_addr
, ha
->addr
, ETH_ALEN
);
343 /* setup Source Mac Address */
345 memcpy(fc
->ctlr
.ctl_src_addr
, fc
->real_dev
->dev_addr
,
346 fc
->real_dev
->addr_len
);
348 wwnn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 1, 0);
349 fc_set_wwnn(lp
, wwnn
);
350 /* XXX - 3rd arg needs to be vlan id */
351 wwpn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 2, 0);
352 fc_set_wwpn(lp
, wwpn
);
355 * Add FCoE MAC address as second unicast MAC address
356 * or enter promiscuous mode if not capable of listening
357 * for multiple unicast MACs.
360 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
361 dev_unicast_add(fc
->real_dev
, flogi_maddr
);
363 dev_unicast_add(fc
->real_dev
, fc
->ctlr
.ctl_src_addr
);
364 dev_mc_add(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
368 * setup the receive function from ethernet driver
369 * on the ethertype for the given device
371 fc
->fcoe_packet_type
.func
= fcoe_rcv
;
372 fc
->fcoe_packet_type
.type
= __constant_htons(ETH_P_FCOE
);
373 fc
->fcoe_packet_type
.dev
= fc
->real_dev
;
374 dev_add_pack(&fc
->fcoe_packet_type
);
376 fc
->fip_packet_type
.func
= fcoe_fip_recv
;
377 fc
->fip_packet_type
.type
= htons(ETH_P_FIP
);
378 fc
->fip_packet_type
.dev
= fc
->real_dev
;
379 dev_add_pack(&fc
->fip_packet_type
);
385 * fcoe_shost_config() - Sets up fc_lport->host
386 * @lp : ptr to the fc_lport
387 * @shost : ptr to the associated scsi host
388 * @dev : device associated to scsi host
390 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
392 * Returns : 0 for success
394 static int fcoe_shost_config(struct fc_lport
*lp
, struct Scsi_Host
*shost
,
399 /* lport scsi host config */
402 lp
->host
->max_lun
= FCOE_MAX_LUN
;
403 lp
->host
->max_id
= FCOE_MAX_FCP_TARGET
;
404 lp
->host
->max_channel
= 0;
405 lp
->host
->transportt
= scsi_transport_fcoe_sw
;
407 /* add the new host to the SCSI-ml */
408 rc
= scsi_add_host(lp
->host
, dev
);
410 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
413 sprintf(fc_host_symbolic_name(lp
->host
), "%s v%s over %s",
414 FCOE_NAME
, FCOE_VERSION
,
415 fcoe_netdev(lp
)->name
);
421 * fcoe_em_config() - allocates em for this lport
422 * @lp: the port that em is to allocated for
424 * Returns : 0 on success
426 static inline int fcoe_em_config(struct fc_lport
*lp
)
430 lp
->emp
= fc_exch_mgr_alloc(lp
, FC_CLASS_3
,
431 FCOE_MIN_XID
, FCOE_MAX_XID
);
439 * fcoe_if_destroy() - FCoE software HBA tear-down function
440 * @netdev: ptr to the associated net_device
442 * Returns: 0 if link is OK for use by FCoE.
444 static int fcoe_if_destroy(struct net_device
*netdev
)
446 struct fc_lport
*lp
= NULL
;
447 struct fcoe_softc
*fc
;
451 printk(KERN_DEBUG
"fcoe_if_destroy:interface on %s\n",
454 lp
= fcoe_hostlist_lookup(netdev
);
460 /* Logout of the fabric */
461 fc_fabric_logoff(lp
);
463 /* Remove the instance from fcoe's list */
464 fcoe_hostlist_remove(lp
);
466 /* clean up netdev configurations */
467 fcoe_netdev_cleanup(fc
);
469 /* tear-down the FCoE controller */
470 fcoe_ctlr_destroy(&fc
->ctlr
);
472 /* Cleanup the fc_lport */
473 fc_lport_destroy(lp
);
476 /* Detach from the scsi-ml */
477 fc_remove_host(lp
->host
);
478 scsi_remove_host(lp
->host
);
480 /* There are no more rports or I/O, free the EM */
482 fc_exch_mgr_free(lp
->emp
);
484 /* Free the per-CPU receive threads */
485 fcoe_percpu_clean(lp
);
487 /* Free existing skbs */
488 fcoe_clean_pending_queue(lp
);
491 del_timer_sync(&fc
->timer
);
493 /* Free memory used by statistical counters */
494 fc_lport_free_stats(lp
);
496 /* Release the net_device and Scsi_Host */
497 dev_put(fc
->real_dev
);
498 scsi_host_put(lp
->host
);
504 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
505 * @lp: the corresponding fc_lport
506 * @xid: the exchange id for this ddp transfer
507 * @sgl: the scatterlist describing this transfer
508 * @sgc: number of sg items
512 static int fcoe_ddp_setup(struct fc_lport
*lp
, u16 xid
,
513 struct scatterlist
*sgl
, unsigned int sgc
)
515 struct net_device
*n
= fcoe_netdev(lp
);
517 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_setup
)
518 return n
->netdev_ops
->ndo_fcoe_ddp_setup(n
, xid
, sgl
, sgc
);
524 * fcoe_ddp_done - calls LLD's ddp_done through net_device
525 * @lp: the corresponding fc_lport
526 * @xid: the exchange id for this ddp transfer
528 * Returns : the length of data that have been completed by ddp
530 static int fcoe_ddp_done(struct fc_lport
*lp
, u16 xid
)
532 struct net_device
*n
= fcoe_netdev(lp
);
534 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_done
)
535 return n
->netdev_ops
->ndo_fcoe_ddp_done(n
, xid
);
539 static struct libfc_function_template fcoe_libfc_fcn_templ
= {
540 .frame_send
= fcoe_xmit
,
541 .ddp_setup
= fcoe_ddp_setup
,
542 .ddp_done
= fcoe_ddp_done
,
546 * fcoe_if_create() - this function creates the fcoe interface
547 * @netdev: pointer the associated netdevice
549 * Creates fc_lport struct and scsi_host for lport, configures lport
550 * and starts fabric login.
552 * Returns : 0 on success
554 static int fcoe_if_create(struct net_device
*netdev
)
557 struct fc_lport
*lp
= NULL
;
558 struct fcoe_softc
*fc
;
559 struct Scsi_Host
*shost
;
563 printk(KERN_DEBUG
"fcoe_if_create:interface on %s\n",
566 lp
= fcoe_hostlist_lookup(netdev
);
570 shost
= libfc_host_alloc(&fcoe_shost_template
,
571 sizeof(struct fcoe_softc
));
573 FC_DBG("Could not allocate host structure\n");
576 lp
= shost_priv(shost
);
579 /* configure fc_lport, e.g., em */
580 rc
= fcoe_lport_config(lp
);
582 FC_DBG("Could not configure lport\n");
589 fcoe_ctlr_init(&fc
->ctlr
);
590 fc
->ctlr
.send
= fcoe_fip_send
;
591 fc
->ctlr
.update_mac
= fcoe_update_src_mac
;
593 /* configure lport network properties */
594 rc
= fcoe_netdev_config(lp
, netdev
);
596 FC_DBG("Could not configure netdev for the interface\n");
597 goto out_netdev_cleanup
;
600 /* configure lport scsi host properties */
601 rc
= fcoe_shost_config(lp
, shost
, &netdev
->dev
);
603 FC_DBG("Could not configure shost for lport\n");
604 goto out_netdev_cleanup
;
607 /* lport exch manager allocation */
608 rc
= fcoe_em_config(lp
);
610 FC_DBG("Could not configure em for lport\n");
611 goto out_netdev_cleanup
;
614 /* Initialize the library */
615 rc
= fcoe_libfc_config(lp
, &fcoe_libfc_fcn_templ
);
617 FC_DBG("Could not configure libfc for lport!\n");
621 /* add to lports list */
622 fcoe_hostlist_add(lp
);
624 lp
->boot_time
= jiffies
;
628 if (!fcoe_link_ok(lp
))
629 fcoe_ctlr_link_up(&fc
->ctlr
);
636 fc_exch_mgr_free(lp
->emp
); /* Free the EM */
638 fcoe_netdev_cleanup(fc
);
640 scsi_host_put(lp
->host
);
645 * fcoe_if_init() - attach to scsi transport
647 * Returns : 0 on success
649 static int __init
fcoe_if_init(void)
651 /* attach to scsi transport */
652 scsi_transport_fcoe_sw
=
653 fc_attach_transport(&fcoe_transport_function
);
655 if (!scsi_transport_fcoe_sw
) {
656 printk(KERN_ERR
"fcoe_init:fc_attach_transport() failed\n");
664 * fcoe_if_exit() - detach from scsi transport
666 * Returns : 0 on success
668 int __exit
fcoe_if_exit(void)
670 fc_release_transport(scsi_transport_fcoe_sw
);
675 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
676 * @cpu: cpu index for the online cpu
678 static void fcoe_percpu_thread_create(unsigned int cpu
)
680 struct fcoe_percpu_s
*p
;
681 struct task_struct
*thread
;
683 p
= &per_cpu(fcoe_percpu
, cpu
);
685 thread
= kthread_create(fcoe_percpu_receive_thread
,
686 (void *)p
, "fcoethread/%d", cpu
);
688 if (likely(!IS_ERR(p
->thread
))) {
689 kthread_bind(thread
, cpu
);
690 wake_up_process(thread
);
692 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
694 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
699 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
700 * @cpu: cpu index the rx thread is to be removed
702 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
703 * current CPU's Rx thread. If the thread being destroyed is bound to
704 * the CPU processing this context the skbs will be freed.
706 static void fcoe_percpu_thread_destroy(unsigned int cpu
)
708 struct fcoe_percpu_s
*p
;
709 struct task_struct
*thread
;
710 struct page
*crc_eof
;
713 struct fcoe_percpu_s
*p0
;
714 unsigned targ_cpu
= smp_processor_id();
715 #endif /* CONFIG_SMP */
717 printk(KERN_DEBUG
"fcoe: Destroying receive thread for CPU %d\n", cpu
);
719 /* Prevent any new skbs from being queued for this CPU. */
720 p
= &per_cpu(fcoe_percpu
, cpu
);
721 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
724 crc_eof
= p
->crc_eof_page
;
725 p
->crc_eof_page
= NULL
;
726 p
->crc_eof_offset
= 0;
727 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
731 * Don't bother moving the skb's if this context is running
732 * on the same CPU that is having its thread destroyed. This
733 * can easily happen when the module is removed.
735 if (cpu
!= targ_cpu
) {
736 p0
= &per_cpu(fcoe_percpu
, targ_cpu
);
737 spin_lock_bh(&p0
->fcoe_rx_list
.lock
);
739 FC_DBG("Moving frames from CPU %d to CPU %d\n",
742 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
743 __skb_queue_tail(&p0
->fcoe_rx_list
, skb
);
744 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
747 * The targeted CPU is not initialized and cannot accept
748 * new skbs. Unlock the targeted CPU and drop the skbs
749 * on the CPU that is going offline.
751 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
753 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
757 * This scenario occurs when the module is being removed
758 * and all threads are being destroyed. skbs will continue
759 * to be shifted from the CPU thread that is being removed
760 * to the CPU thread associated with the CPU that is processing
761 * the module removal. Once there is only one CPU Rx thread it
762 * will reach this case and we will drop all skbs and later
765 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
766 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
768 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
772 * This a non-SMP scenario where the singular Rx thread is
773 * being removed. Free all skbs and stop the thread.
775 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
776 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
778 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
782 kthread_stop(thread
);
789 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
790 * @nfb: callback data block
791 * @action: event triggering the callback
792 * @hcpu: index for the cpu of this event
794 * This creates or destroys per cpu data for fcoe
796 * Returns NOTIFY_OK always.
798 static int fcoe_cpu_callback(struct notifier_block
*nfb
,
799 unsigned long action
, void *hcpu
)
801 unsigned cpu
= (unsigned long)hcpu
;
805 case CPU_ONLINE_FROZEN
:
806 FC_DBG("CPU %x online: Create Rx thread\n", cpu
);
807 fcoe_percpu_thread_create(cpu
);
810 case CPU_DEAD_FROZEN
:
811 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu
);
812 fcoe_percpu_thread_destroy(cpu
);
820 static struct notifier_block fcoe_cpu_notifier
= {
821 .notifier_call
= fcoe_cpu_callback
,
825 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
826 * @skb: the receive skb
827 * @dev: associated net device
829 * @olddev: last device
831 * this function will receive the packet and build fc frame and pass it up
833 * Returns: 0 for success
835 int fcoe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
836 struct packet_type
*ptype
, struct net_device
*olddev
)
839 struct fcoe_rcv_info
*fr
;
840 struct fcoe_softc
*fc
;
841 struct fc_frame_header
*fh
;
842 struct fcoe_percpu_s
*fps
;
844 unsigned int cpu
= 0;
846 fc
= container_of(ptype
, struct fcoe_softc
, fcoe_packet_type
);
848 if (unlikely(lp
== NULL
)) {
849 FC_DBG("cannot find hba structure");
855 if (unlikely(debug_fcoe
)) {
856 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
857 "end:%p sum:%d dev:%s", skb
->len
, skb
->data_len
,
858 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
859 skb_end_pointer(skb
), skb
->csum
,
860 skb
->dev
? skb
->dev
->name
: "<NULL>");
864 /* check for FCOE packet type */
865 if (unlikely(eth_hdr(skb
)->h_proto
!= htons(ETH_P_FCOE
))) {
866 FC_DBG("wrong FC type frame");
871 * Check for minimum frame length, and make sure required FCoE
872 * and FC headers are pulled into the linear data area.
874 if (unlikely((skb
->len
< FCOE_MIN_FRAME
) ||
875 !pskb_may_pull(skb
, FCOE_HEADER_LEN
)))
878 skb_set_transport_header(skb
, sizeof(struct fcoe_hdr
));
879 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
881 oxid
= ntohs(fh
->fh_ox_id
);
883 fr
= fcoe_dev_from_skb(skb
);
889 * The incoming frame exchange id(oxid) is ANDed with num of online
890 * cpu bits to get cpu and then this cpu is used for selecting
891 * a per cpu kernel thread from fcoe_percpu.
893 cpu
= oxid
& (num_online_cpus() - 1);
896 fps
= &per_cpu(fcoe_percpu
, cpu
);
897 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
898 if (unlikely(!fps
->thread
)) {
900 * The targeted CPU is not ready, let's target
901 * the first CPU now. For non-SMP systems this
902 * will check the same CPU twice.
904 FC_DBG("CPU is online, but no receive thread ready "
905 "for incoming skb- using first online CPU.\n");
907 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
908 cpu
= first_cpu(cpu_online_map
);
909 fps
= &per_cpu(fcoe_percpu
, cpu
);
910 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
912 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
918 * We now have a valid CPU that we're targeting for
919 * this skb. We also have this receive thread locked,
920 * so we're free to queue skbs into it's queue.
922 __skb_queue_tail(&fps
->fcoe_rx_list
, skb
);
923 if (fps
->fcoe_rx_list
.qlen
== 1)
924 wake_up_process(fps
->thread
);
926 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
930 fc_lport_get_stats(lp
)->ErrorFrames
++;
938 * fcoe_start_io() - pass to netdev to start xmit for fcoe
939 * @skb: the skb to be xmitted
941 * Returns: 0 for success
943 static inline int fcoe_start_io(struct sk_buff
*skb
)
948 rc
= dev_queue_xmit(skb
);
956 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
957 * @skb: the skb to be xmitted
960 * Returns: 0 for success
962 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
964 struct fcoe_percpu_s
*fps
;
967 fps
= &get_cpu_var(fcoe_percpu
);
968 page
= fps
->crc_eof_page
;
970 page
= alloc_page(GFP_ATOMIC
);
972 put_cpu_var(fcoe_percpu
);
975 fps
->crc_eof_page
= page
;
976 fps
->crc_eof_offset
= 0;
980 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
981 fps
->crc_eof_offset
, tlen
);
983 skb
->data_len
+= tlen
;
984 skb
->truesize
+= tlen
;
985 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
987 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
988 fps
->crc_eof_page
= NULL
;
989 fps
->crc_eof_offset
= 0;
992 put_cpu_var(fcoe_percpu
);
997 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
998 * @fp: the fc_frame containing data to be checksummed
1000 * This uses crc32() to calculate the crc for fc frame
1001 * Return : 32 bit crc
1003 u32
fcoe_fc_crc(struct fc_frame
*fp
)
1005 struct sk_buff
*skb
= fp_skb(fp
);
1006 struct skb_frag_struct
*frag
;
1007 unsigned char *data
;
1008 unsigned long off
, len
, clen
;
1012 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
1014 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1015 frag
= &skb_shinfo(skb
)->frags
[i
];
1016 off
= frag
->page_offset
;
1019 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
1020 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
1021 KM_SKB_DATA_SOFTIRQ
);
1022 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
1023 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
1032 * fcoe_xmit() - FCoE frame transmit function
1033 * @lp: the associated local port
1034 * @fp: the fc_frame to be transmitted
1036 * Return : 0 for success
1038 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
1043 struct fcoe_crc_eof
*cp
;
1044 struct sk_buff
*skb
;
1045 struct fcoe_dev_stats
*stats
;
1046 struct fc_frame_header
*fh
;
1047 unsigned int hlen
; /* header length implies the version */
1048 unsigned int tlen
; /* trailer length */
1049 unsigned int elen
; /* eth header, may include vlan */
1050 struct fcoe_softc
*fc
;
1052 struct fcoe_hdr
*hp
;
1054 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
1056 fc
= lport_priv(lp
);
1057 fh
= fc_frame_header_get(fp
);
1059 wlen
= skb
->len
/ FCOE_WORD_TO_BYTE
;
1066 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
) &&
1067 fcoe_ctlr_els_send(&fc
->ctlr
, skb
))
1073 elen
= sizeof(struct ethhdr
);
1074 hlen
= sizeof(struct fcoe_hdr
);
1075 tlen
= sizeof(struct fcoe_crc_eof
);
1076 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
1079 if (likely(lp
->crc_offload
)) {
1080 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1081 skb
->csum_start
= skb_headroom(skb
);
1082 skb
->csum_offset
= skb
->len
;
1085 skb
->ip_summed
= CHECKSUM_NONE
;
1086 crc
= fcoe_fc_crc(fp
);
1089 /* copy fc crc and eof to the skb buff */
1090 if (skb_is_nonlinear(skb
)) {
1092 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
1096 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
1097 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
1098 + frag
->page_offset
;
1100 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
1103 memset(cp
, 0, sizeof(*cp
));
1105 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
1107 if (skb_is_nonlinear(skb
)) {
1108 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
1112 /* adjust skb network/transport offsets to match mac/fcoe/fc */
1113 skb_push(skb
, elen
+ hlen
);
1114 skb_reset_mac_header(skb
);
1115 skb_reset_network_header(skb
);
1116 skb
->mac_len
= elen
;
1117 skb
->protocol
= htons(ETH_P_FCOE
);
1118 skb
->dev
= fc
->real_dev
;
1120 /* fill up mac and fcoe headers */
1122 eh
->h_proto
= htons(ETH_P_FCOE
);
1123 if (fc
->ctlr
.map_dest
)
1124 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
1126 /* insert GW address */
1127 memcpy(eh
->h_dest
, fc
->ctlr
.dest_addr
, ETH_ALEN
);
1129 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
))
1130 memcpy(eh
->h_source
, fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
1132 memcpy(eh
->h_source
, fc
->ctlr
.data_src_addr
, ETH_ALEN
);
1134 hp
= (struct fcoe_hdr
*)(eh
+ 1);
1135 memset(hp
, 0, sizeof(*hp
));
1137 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
1141 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1142 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
1143 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
1144 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
1146 skb_shinfo(skb
)->gso_type
= 0;
1147 skb_shinfo(skb
)->gso_size
= 0;
1150 /* update tx stats: regardless if LLD fails */
1151 stats
= fc_lport_get_stats(lp
);
1153 stats
->TxWords
+= wlen
;
1155 /* send down to lld */
1157 if (fc
->fcoe_pending_queue
.qlen
)
1158 fcoe_check_wait_queue(lp
, skb
);
1159 else if (fcoe_start_io(skb
))
1160 fcoe_check_wait_queue(lp
, skb
);
1166 * fcoe_percpu_receive_thread() - recv thread per cpu
1167 * @arg: ptr to the fcoe per cpu struct
1169 * Return: 0 for success
1171 int fcoe_percpu_receive_thread(void *arg
)
1173 struct fcoe_percpu_s
*p
= arg
;
1175 struct fc_lport
*lp
;
1176 struct fcoe_rcv_info
*fr
;
1177 struct fcoe_dev_stats
*stats
;
1178 struct fc_frame_header
*fh
;
1179 struct sk_buff
*skb
;
1180 struct fcoe_crc_eof crc_eof
;
1181 struct fc_frame
*fp
;
1183 struct fcoe_softc
*fc
;
1184 struct fcoe_hdr
*hp
;
1186 set_user_nice(current
, -20);
1188 while (!kthread_should_stop()) {
1190 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1191 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
1192 set_current_state(TASK_INTERRUPTIBLE
);
1193 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1195 set_current_state(TASK_RUNNING
);
1196 if (kthread_should_stop())
1198 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1200 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1201 fr
= fcoe_dev_from_skb(skb
);
1203 if (unlikely(lp
== NULL
)) {
1204 FC_DBG("invalid HBA Structure");
1209 if (unlikely(debug_fcoe
)) {
1210 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1211 "tail:%p end:%p sum:%d dev:%s",
1212 skb
->len
, skb
->data_len
,
1213 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
1214 skb_end_pointer(skb
), skb
->csum
,
1215 skb
->dev
? skb
->dev
->name
: "<NULL>");
1219 * Save source MAC address before discarding header.
1221 fc
= lport_priv(lp
);
1222 if (skb_is_nonlinear(skb
))
1223 skb_linearize(skb
); /* not ideal */
1224 mac
= eth_hdr(skb
)->h_source
;
1227 * Frame length checks and setting up the header pointers
1228 * was done in fcoe_rcv already.
1230 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
1231 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1233 stats
= fc_lport_get_stats(lp
);
1234 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
1235 if (stats
->ErrorFrames
< 5)
1236 printk(KERN_WARNING
"FCoE version "
1237 "mismatch: The frame has "
1238 "version %x, but the "
1239 "initiator supports version "
1240 "%x\n", FC_FCOE_DECAPS_VER(hp
),
1242 stats
->ErrorFrames
++;
1247 skb_pull(skb
, sizeof(struct fcoe_hdr
));
1248 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
1251 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
1253 fp
= (struct fc_frame
*)skb
;
1256 fr_sof(fp
) = hp
->fcoe_sof
;
1258 /* Copy out the CRC and EOF trailer for access */
1259 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
1263 fr_eof(fp
) = crc_eof
.fcoe_eof
;
1264 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
1265 if (pskb_trim(skb
, fr_len
)) {
1271 * We only check CRC if no offload is available and if it is
1272 * it's solicited data, in which case, the FCP layer would
1273 * check it during the copy.
1275 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
1276 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1278 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
1280 fh
= fc_frame_header_get(fp
);
1281 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
1282 fh
->fh_type
== FC_TYPE_FCP
) {
1283 fc_exch_recv(lp
, lp
->emp
, fp
);
1286 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
1287 if (le32_to_cpu(fr_crc(fp
)) !=
1288 ~crc32(~0, skb
->data
, fr_len
)) {
1289 if (debug_fcoe
|| stats
->InvalidCRCCount
< 5)
1290 printk(KERN_WARNING
"fcoe: dropping "
1291 "frame with CRC error\n");
1292 stats
->InvalidCRCCount
++;
1293 stats
->ErrorFrames
++;
1297 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1299 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
) &&
1300 fcoe_ctlr_recv_flogi(&fc
->ctlr
, fp
, mac
)) {
1304 fc_exch_recv(lp
, lp
->emp
, fp
);
1310 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1313 * This empties the wait_queue, dequeue the head of the wait_queue queue
1314 * and calls fcoe_start_io() for each packet, if all skb have been
1315 * transmitted, return qlen or -1 if a error occurs, then restore
1316 * wait_queue and try again later.
1318 * The wait_queue is used when the skb transmit fails. skb will go
1319 * in the wait_queue which will be emptied by the timer function or
1320 * by the next skb transmit.
1322 static void fcoe_check_wait_queue(struct fc_lport
*lp
, struct sk_buff
*skb
)
1324 struct fcoe_softc
*fc
= lport_priv(lp
);
1327 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1330 __skb_queue_tail(&fc
->fcoe_pending_queue
, skb
);
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 if (fc
->fcoe_pending_queue
.qlen
&& !timer_pending(&fc
->timer
))
1358 mod_timer(&fc
->timer
, jiffies
+ 2);
1359 fc
->fcoe_pending_queue_active
= 0;
1361 if (fc
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
1363 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1368 * fcoe_dev_setup() - setup link change notification interface
1370 static void fcoe_dev_setup(void)
1372 register_netdevice_notifier(&fcoe_notifier
);
1376 * fcoe_dev_cleanup() - cleanup link change notification interface
1378 static void fcoe_dev_cleanup(void)
1380 unregister_netdevice_notifier(&fcoe_notifier
);
1384 * fcoe_device_notification() - netdev event notification callback
1385 * @notifier: context of the notification
1386 * @event: type of event
1387 * @ptr: fixed array for output parsed ifname
1389 * This function is called by the ethernet driver in case of link change event
1391 * Returns: 0 for success
1393 static int fcoe_device_notification(struct notifier_block
*notifier
,
1394 ulong event
, void *ptr
)
1396 struct fc_lport
*lp
= NULL
;
1397 struct net_device
*real_dev
= ptr
;
1398 struct fcoe_softc
*fc
;
1399 struct fcoe_dev_stats
*stats
;
1400 u32 link_possible
= 1;
1404 read_lock(&fcoe_hostlist_lock
);
1405 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1406 if (fc
->real_dev
== real_dev
) {
1411 read_unlock(&fcoe_hostlist_lock
);
1419 case NETDEV_GOING_DOWN
:
1425 case NETDEV_CHANGEMTU
:
1426 mfs
= fc
->real_dev
->mtu
-
1427 (sizeof(struct fcoe_hdr
) +
1428 sizeof(struct fcoe_crc_eof
));
1429 if (mfs
>= FC_MIN_MAX_FRAME
)
1430 fc_set_mfs(lp
, mfs
);
1432 case NETDEV_REGISTER
:
1435 FC_DBG("Unknown event %ld from netdev netlink\n", event
);
1437 if (link_possible
&& !fcoe_link_ok(lp
))
1438 fcoe_ctlr_link_up(&fc
->ctlr
);
1439 else if (fcoe_ctlr_link_down(&fc
->ctlr
)) {
1440 stats
= fc_lport_get_stats(lp
);
1441 stats
->LinkFailureCount
++;
1442 fcoe_clean_pending_queue(lp
);
1449 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1450 * @buffer: incoming buffer to be copied
1452 * Returns: NULL or ptr to net_device
1454 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
1457 char ifname
[IFNAMSIZ
+ 2];
1460 strlcpy(ifname
, buffer
, IFNAMSIZ
);
1461 cp
= ifname
+ strlen(ifname
);
1462 while (--cp
>= ifname
&& *cp
== '\n')
1464 return dev_get_by_name(&init_net
, ifname
);
1470 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1471 * @netdev: the target netdev
1473 * Returns: ptr to the struct module, NULL for failure
1475 static struct module
*
1476 fcoe_netdev_to_module_owner(const struct net_device
*netdev
)
1483 dev
= netdev
->dev
.parent
;
1490 return dev
->driver
->owner
;
1494 * fcoe_ethdrv_get() - Hold the Ethernet driver
1495 * @netdev: the target netdev
1497 * Holds the Ethernet driver module by try_module_get() for
1498 * the corresponding netdev.
1500 * Returns: 0 for success
1502 static int fcoe_ethdrv_get(const struct net_device
*netdev
)
1504 struct module
*owner
;
1506 owner
= fcoe_netdev_to_module_owner(netdev
);
1508 printk(KERN_DEBUG
"fcoe:hold driver module %s for %s\n",
1509 module_name(owner
), netdev
->name
);
1510 return try_module_get(owner
);
1516 * fcoe_ethdrv_put() - Release the Ethernet driver
1517 * @netdev: the target netdev
1519 * Releases the Ethernet driver module by module_put for
1520 * the corresponding netdev.
1522 * Returns: 0 for success
1524 static int fcoe_ethdrv_put(const struct net_device
*netdev
)
1526 struct module
*owner
;
1528 owner
= fcoe_netdev_to_module_owner(netdev
);
1530 printk(KERN_DEBUG
"fcoe:release driver module %s for %s\n",
1531 module_name(owner
), netdev
->name
);
1539 * fcoe_destroy() - handles the destroy from sysfs
1540 * @buffer: expected to be an eth if name
1541 * @kp: associated kernel param
1543 * Returns: 0 for success
1545 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1548 struct net_device
*netdev
;
1550 netdev
= fcoe_if_to_netdev(buffer
);
1555 /* look for existing lport */
1556 if (!fcoe_hostlist_lookup(netdev
)) {
1560 rc
= fcoe_if_destroy(netdev
);
1562 printk(KERN_ERR
"fcoe: fcoe_if_destroy(%s) failed\n",
1567 fcoe_ethdrv_put(netdev
);
1576 * fcoe_create() - Handles the create call from sysfs
1577 * @buffer: expected to be an eth if name
1578 * @kp: associated kernel param
1580 * Returns: 0 for success
1582 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1585 struct net_device
*netdev
;
1587 netdev
= fcoe_if_to_netdev(buffer
);
1592 /* look for existing lport */
1593 if (fcoe_hostlist_lookup(netdev
)) {
1597 fcoe_ethdrv_get(netdev
);
1599 rc
= fcoe_if_create(netdev
);
1601 printk(KERN_ERR
"fcoe: fcoe_if_create(%s) failed\n",
1603 fcoe_ethdrv_put(netdev
);
1614 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1615 __MODULE_PARM_TYPE(create
, "string");
1616 MODULE_PARM_DESC(create
, "Create fcoe port using net device passed in.");
1617 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1618 __MODULE_PARM_TYPE(destroy
, "string");
1619 MODULE_PARM_DESC(destroy
, "Destroy fcoe port");
1622 * fcoe_link_ok() - Check if link is ok for the fc_lport
1623 * @lp: ptr to the fc_lport
1625 * Any permanently-disqualifying conditions have been previously checked.
1626 * This also updates the speed setting, which may change with link for 100/1000.
1628 * This function should probably be checking for PAUSE support at some point
1629 * in the future. Currently Per-priority-pause is not determinable using
1630 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1632 * Returns: 0 if link is OK for use by FCoE.
1635 int fcoe_link_ok(struct fc_lport
*lp
)
1637 struct fcoe_softc
*fc
= lport_priv(lp
);
1638 struct net_device
*dev
= fc
->real_dev
;
1639 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1642 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
)) {
1644 if (dev
->ethtool_ops
->get_settings
) {
1645 dev
->ethtool_ops
->get_settings(dev
, &ecmd
);
1646 lp
->link_supported_speeds
&=
1647 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1648 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1649 SUPPORTED_1000baseT_Full
))
1650 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1651 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1652 lp
->link_supported_speeds
|=
1653 FC_PORTSPEED_10GBIT
;
1654 if (ecmd
.speed
== SPEED_1000
)
1655 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1656 if (ecmd
.speed
== SPEED_10000
)
1657 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1666 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1669 void fcoe_percpu_clean(struct fc_lport
*lp
)
1671 struct fcoe_percpu_s
*pp
;
1672 struct fcoe_rcv_info
*fr
;
1673 struct sk_buff_head
*list
;
1674 struct sk_buff
*skb
, *next
;
1675 struct sk_buff
*head
;
1678 for_each_possible_cpu(cpu
) {
1679 pp
= &per_cpu(fcoe_percpu
, cpu
);
1680 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1681 list
= &pp
->fcoe_rx_list
;
1683 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1686 fr
= fcoe_dev_from_skb(skb
);
1687 if (fr
->fr_dev
== lp
) {
1688 __skb_unlink(skb
, list
);
1692 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1697 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1698 * @lp: the corresponding fc_lport
1702 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1704 struct fcoe_softc
*fc
= lport_priv(lp
);
1705 struct sk_buff
*skb
;
1707 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1708 while ((skb
= __skb_dequeue(&fc
->fcoe_pending_queue
)) != NULL
) {
1709 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1711 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1713 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1717 * fcoe_reset() - Resets the fcoe
1718 * @shost: shost the reset is from
1722 int fcoe_reset(struct Scsi_Host
*shost
)
1724 struct fc_lport
*lport
= shost_priv(shost
);
1725 fc_lport_reset(lport
);
1730 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1731 * @dev: this is currently ptr to net_device
1733 * Returns: NULL or the located fcoe_softc
1735 static struct fcoe_softc
*
1736 fcoe_hostlist_lookup_softc(const struct net_device
*dev
)
1738 struct fcoe_softc
*fc
;
1740 read_lock(&fcoe_hostlist_lock
);
1741 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1742 if (fc
->real_dev
== dev
) {
1743 read_unlock(&fcoe_hostlist_lock
);
1747 read_unlock(&fcoe_hostlist_lock
);
1752 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1753 * @netdev: ptr to net_device
1755 * Returns: 0 for success
1757 struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1759 struct fcoe_softc
*fc
;
1761 fc
= fcoe_hostlist_lookup_softc(netdev
);
1763 return (fc
) ? fc
->ctlr
.lp
: NULL
;
1767 * fcoe_hostlist_add() - Add a lport to lports list
1768 * @lp: ptr to the fc_lport to be added
1770 * Returns: 0 for success
1772 int fcoe_hostlist_add(const struct fc_lport
*lp
)
1774 struct fcoe_softc
*fc
;
1776 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1778 fc
= lport_priv(lp
);
1779 write_lock_bh(&fcoe_hostlist_lock
);
1780 list_add_tail(&fc
->list
, &fcoe_hostlist
);
1781 write_unlock_bh(&fcoe_hostlist_lock
);
1787 * fcoe_hostlist_remove() - remove a lport from lports list
1788 * @lp: ptr to the fc_lport to be removed
1790 * Returns: 0 for success
1792 int fcoe_hostlist_remove(const struct fc_lport
*lp
)
1794 struct fcoe_softc
*fc
;
1796 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1798 write_lock_bh(&fcoe_hostlist_lock
);
1799 list_del(&fc
->list
);
1800 write_unlock_bh(&fcoe_hostlist_lock
);
1806 * fcoe_init() - fcoe module loading initialization
1808 * Returns 0 on success, negative on failure
1810 static int __init
fcoe_init(void)
1814 struct fcoe_percpu_s
*p
;
1816 INIT_LIST_HEAD(&fcoe_hostlist
);
1817 rwlock_init(&fcoe_hostlist_lock
);
1819 for_each_possible_cpu(cpu
) {
1820 p
= &per_cpu(fcoe_percpu
, cpu
);
1821 skb_queue_head_init(&p
->fcoe_rx_list
);
1824 for_each_online_cpu(cpu
)
1825 fcoe_percpu_thread_create(cpu
);
1827 /* Initialize per CPU interrupt thread */
1828 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1832 /* Setup link change notification */
1840 for_each_online_cpu(cpu
) {
1841 fcoe_percpu_thread_destroy(cpu
);
1846 module_init(fcoe_init
);
1849 * fcoe_exit() - fcoe module unloading cleanup
1851 * Returns 0 on success, negative on failure
1853 static void __exit
fcoe_exit(void)
1856 struct fcoe_softc
*fc
, *tmp
;
1860 /* releases the associated fcoe hosts */
1861 list_for_each_entry_safe(fc
, tmp
, &fcoe_hostlist
, list
)
1862 fcoe_if_destroy(fc
->real_dev
);
1864 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
1866 for_each_online_cpu(cpu
) {
1867 fcoe_percpu_thread_destroy(cpu
);
1870 /* detach from scsi transport */
1873 module_exit(fcoe_exit
);