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_TIMER(fcoe_timer
, NULL
, 0, 0);
58 DEFINE_PER_CPU(struct fcoe_percpu_s
, fcoe_percpu
);
60 /* Function Prototypes */
61 static int fcoe_reset(struct Scsi_Host
*shost
);
62 static int fcoe_xmit(struct fc_lport
*, struct fc_frame
*);
63 static int fcoe_rcv(struct sk_buff
*, struct net_device
*,
64 struct packet_type
*, struct net_device
*);
65 static int fcoe_percpu_receive_thread(void *arg
);
66 static void fcoe_clean_pending_queue(struct fc_lport
*lp
);
67 static void fcoe_percpu_clean(struct fc_lport
*lp
);
68 static int fcoe_link_ok(struct fc_lport
*lp
);
70 static struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*);
71 static int fcoe_hostlist_add(const struct fc_lport
*);
72 static int fcoe_hostlist_remove(const struct fc_lport
*);
74 static int fcoe_check_wait_queue(struct fc_lport
*);
75 static int fcoe_device_notification(struct notifier_block
*, ulong
, void *);
76 static void fcoe_dev_setup(void);
77 static void fcoe_dev_cleanup(void);
79 /* notification function from net device */
80 static struct notifier_block fcoe_notifier
= {
81 .notifier_call
= fcoe_device_notification
,
84 static struct scsi_transport_template
*scsi_transport_fcoe_sw
;
86 struct fc_function_template fcoe_transport_function
= {
87 .show_host_node_name
= 1,
88 .show_host_port_name
= 1,
89 .show_host_supported_classes
= 1,
90 .show_host_supported_fc4s
= 1,
91 .show_host_active_fc4s
= 1,
92 .show_host_maxframe_size
= 1,
94 .show_host_port_id
= 1,
95 .show_host_supported_speeds
= 1,
96 .get_host_speed
= fc_get_host_speed
,
98 .show_host_port_type
= 1,
99 .get_host_port_state
= fc_get_host_port_state
,
100 .show_host_port_state
= 1,
101 .show_host_symbolic_name
= 1,
103 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
104 .show_rport_maxframe_size
= 1,
105 .show_rport_supported_classes
= 1,
107 .show_host_fabric_name
= 1,
108 .show_starget_node_name
= 1,
109 .show_starget_port_name
= 1,
110 .show_starget_port_id
= 1,
111 .set_rport_dev_loss_tmo
= fc_set_rport_loss_tmo
,
112 .show_rport_dev_loss_tmo
= 1,
113 .get_fc_host_stats
= fc_get_host_stats
,
114 .issue_fc_host_lip
= fcoe_reset
,
116 .terminate_rport_io
= fc_rport_terminate_io
,
119 static struct scsi_host_template fcoe_shost_template
= {
120 .module
= THIS_MODULE
,
121 .name
= "FCoE Driver",
122 .proc_name
= FCOE_NAME
,
123 .queuecommand
= fc_queuecommand
,
124 .eh_abort_handler
= fc_eh_abort
,
125 .eh_device_reset_handler
= fc_eh_device_reset
,
126 .eh_host_reset_handler
= fc_eh_host_reset
,
127 .slave_alloc
= fc_slave_alloc
,
128 .change_queue_depth
= fc_change_queue_depth
,
129 .change_queue_type
= fc_change_queue_type
,
132 .can_queue
= FCOE_MAX_OUTSTANDING_COMMANDS
,
133 .use_clustering
= ENABLE_CLUSTERING
,
134 .sg_tablesize
= SG_ALL
,
135 .max_sectors
= 0xffff,
139 * fcoe_fip_recv - handle a received FIP frame.
140 * @skb: the receive skb
141 * @dev: associated &net_device
142 * @ptype: the &packet_type structure which was used to register this handler.
143 * @orig_dev: original receive &net_device, in case @dev is a bond.
145 * Returns: 0 for success
147 static int fcoe_fip_recv(struct sk_buff
*skb
, struct net_device
*dev
,
148 struct packet_type
*ptype
,
149 struct net_device
*orig_dev
)
151 struct fcoe_softc
*fc
;
153 fc
= container_of(ptype
, struct fcoe_softc
, fip_packet_type
);
154 fcoe_ctlr_recv(&fc
->ctlr
, skb
);
159 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
160 * @fip: FCoE controller.
163 static void fcoe_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
165 skb
->dev
= fcoe_from_ctlr(fip
)->real_dev
;
170 * fcoe_update_src_mac() - Update Ethernet MAC filters.
171 * @fip: FCoE controller.
172 * @old: Unicast MAC address to delete if the MAC is non-zero.
173 * @new: Unicast MAC address to add.
175 * Remove any previously-set unicast MAC filter.
176 * Add secondary FCoE MAC address filter for our OUI.
178 static void fcoe_update_src_mac(struct fcoe_ctlr
*fip
, u8
*old
, u8
*new)
180 struct fcoe_softc
*fc
;
182 fc
= fcoe_from_ctlr(fip
);
184 if (!is_zero_ether_addr(old
))
185 dev_unicast_delete(fc
->real_dev
, old
, ETH_ALEN
);
186 dev_unicast_add(fc
->real_dev
, new, ETH_ALEN
);
191 * fcoe_lport_config() - sets up the fc_lport
192 * @lp: ptr to the fc_lport
194 * Returns: 0 for success
196 static int fcoe_lport_config(struct fc_lport
*lp
)
200 lp
->max_retry_count
= 3;
201 lp
->e_d_tov
= 2 * 1000; /* FC-FS default */
202 lp
->r_a_tov
= 2 * 2 * 1000;
203 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
204 FCP_SPPF_RETRY
| FCP_SPPF_CONF_COMPL
);
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
, ETH_ALEN
);
237 if (!is_zero_ether_addr(fc
->ctlr
.data_src_addr
))
238 dev_unicast_delete(fc
->real_dev
,
239 fc
->ctlr
.data_src_addr
, ETH_ALEN
);
241 dev_unicast_delete(fc
->real_dev
,
242 fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
243 dev_mc_delete(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
248 * fcoe_netdev_config() - Set up netdev for SW FCoE
249 * @lp : ptr to the fc_lport
250 * @netdev : ptr to the associated netdevice struct
252 * Must be called after fcoe_lport_config() as it will use lport mutex
254 * Returns : 0 for success
256 static int fcoe_netdev_config(struct fc_lport
*lp
, struct net_device
*netdev
)
260 struct fcoe_softc
*fc
;
261 u8 flogi_maddr
[ETH_ALEN
];
262 struct netdev_hw_addr
*ha
;
264 /* Setup lport private data to point to fcoe softc */
267 fc
->real_dev
= netdev
;
268 fc
->phys_dev
= netdev
;
270 /* Require support for get_pauseparam ethtool op. */
271 if (netdev
->priv_flags
& IFF_802_1Q_VLAN
)
272 fc
->phys_dev
= vlan_dev_real_dev(netdev
);
274 /* Do not support for bonding device */
275 if ((fc
->real_dev
->priv_flags
& IFF_MASTER_ALB
) ||
276 (fc
->real_dev
->priv_flags
& IFF_SLAVE_INACTIVE
) ||
277 (fc
->real_dev
->priv_flags
& IFF_MASTER_8023AD
)) {
282 * Determine max frame size based on underlying device and optional
283 * user-configured limit. If the MFS is too low, fcoe_link_ok()
284 * will return 0, so do this first.
286 mfs
= fc
->real_dev
->mtu
- (sizeof(struct fcoe_hdr
) +
287 sizeof(struct fcoe_crc_eof
));
288 if (fc_set_mfs(lp
, mfs
))
291 /* offload features support */
292 if (fc
->real_dev
->features
& NETIF_F_SG
)
295 #ifdef NETIF_F_FCOE_CRC
296 if (netdev
->features
& NETIF_F_FCOE_CRC
) {
298 printk(KERN_DEBUG
"fcoe:%s supports FCCRC offload\n",
303 if (netdev
->features
& NETIF_F_FSO
) {
305 lp
->lso_max
= netdev
->gso_max_size
;
306 printk(KERN_DEBUG
"fcoe:%s supports LSO for max len 0x%x\n",
307 netdev
->name
, lp
->lso_max
);
310 if (netdev
->fcoe_ddp_xid
) {
312 lp
->lro_xid
= netdev
->fcoe_ddp_xid
;
313 printk(KERN_DEBUG
"fcoe:%s supports LRO for max xid 0x%x\n",
314 netdev
->name
, lp
->lro_xid
);
316 skb_queue_head_init(&fc
->fcoe_pending_queue
);
317 fc
->fcoe_pending_queue_active
= 0;
319 /* look for SAN MAC address, if multiple SAN MACs exist, only
320 * use the first one for SPMA */
322 for_each_dev_addr(netdev
, ha
) {
323 if ((ha
->type
== NETDEV_HW_ADDR_T_SAN
) &&
324 (is_valid_ether_addr(fc
->ctlr
.ctl_src_addr
))) {
325 memcpy(fc
->ctlr
.ctl_src_addr
, ha
->addr
, ETH_ALEN
);
332 /* setup Source Mac Address */
334 memcpy(fc
->ctlr
.ctl_src_addr
, fc
->real_dev
->dev_addr
,
335 fc
->real_dev
->addr_len
);
337 wwnn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 1, 0);
338 fc_set_wwnn(lp
, wwnn
);
339 /* XXX - 3rd arg needs to be vlan id */
340 wwpn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 2, 0);
341 fc_set_wwpn(lp
, wwpn
);
344 * Add FCoE MAC address as second unicast MAC address
345 * or enter promiscuous mode if not capable of listening
346 * for multiple unicast MACs.
349 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
350 dev_unicast_add(fc
->real_dev
, flogi_maddr
, ETH_ALEN
);
352 dev_unicast_add(fc
->real_dev
, fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
353 dev_mc_add(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
357 * setup the receive function from ethernet driver
358 * on the ethertype for the given device
360 fc
->fcoe_packet_type
.func
= fcoe_rcv
;
361 fc
->fcoe_packet_type
.type
= __constant_htons(ETH_P_FCOE
);
362 fc
->fcoe_packet_type
.dev
= fc
->real_dev
;
363 dev_add_pack(&fc
->fcoe_packet_type
);
365 fc
->fip_packet_type
.func
= fcoe_fip_recv
;
366 fc
->fip_packet_type
.type
= htons(ETH_P_FIP
);
367 fc
->fip_packet_type
.dev
= fc
->real_dev
;
368 dev_add_pack(&fc
->fip_packet_type
);
374 * fcoe_shost_config() - Sets up fc_lport->host
375 * @lp : ptr to the fc_lport
376 * @shost : ptr to the associated scsi host
377 * @dev : device associated to scsi host
379 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
381 * Returns : 0 for success
383 static int fcoe_shost_config(struct fc_lport
*lp
, struct Scsi_Host
*shost
,
388 /* lport scsi host config */
391 lp
->host
->max_lun
= FCOE_MAX_LUN
;
392 lp
->host
->max_id
= FCOE_MAX_FCP_TARGET
;
393 lp
->host
->max_channel
= 0;
394 lp
->host
->transportt
= scsi_transport_fcoe_sw
;
396 /* add the new host to the SCSI-ml */
397 rc
= scsi_add_host(lp
->host
, dev
);
399 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
402 sprintf(fc_host_symbolic_name(lp
->host
), "%s v%s over %s",
403 FCOE_NAME
, FCOE_VERSION
,
404 fcoe_netdev(lp
)->name
);
410 * fcoe_em_config() - allocates em for this lport
411 * @lp: the port that em is to allocated for
413 * Returns : 0 on success
415 static inline int fcoe_em_config(struct fc_lport
*lp
)
419 lp
->emp
= fc_exch_mgr_alloc(lp
, FC_CLASS_3
,
420 FCOE_MIN_XID
, FCOE_MAX_XID
);
428 * fcoe_if_destroy() - FCoE software HBA tear-down function
429 * @netdev: ptr to the associated net_device
431 * Returns: 0 if link is OK for use by FCoE.
433 static int fcoe_if_destroy(struct net_device
*netdev
)
435 struct fc_lport
*lp
= NULL
;
436 struct fcoe_softc
*fc
;
440 printk(KERN_DEBUG
"fcoe_if_destroy:interface on %s\n",
443 lp
= fcoe_hostlist_lookup(netdev
);
449 /* Logout of the fabric */
450 fc_fabric_logoff(lp
);
452 /* Remove the instance from fcoe's list */
453 fcoe_hostlist_remove(lp
);
455 /* clean up netdev configurations */
456 fcoe_netdev_cleanup(fc
);
458 /* tear-down the FCoE controller */
459 fcoe_ctlr_destroy(&fc
->ctlr
);
461 /* Cleanup the fc_lport */
462 fc_lport_destroy(lp
);
465 /* Detach from the scsi-ml */
466 fc_remove_host(lp
->host
);
467 scsi_remove_host(lp
->host
);
469 /* There are no more rports or I/O, free the EM */
471 fc_exch_mgr_free(lp
->emp
);
473 /* Free the per-CPU receive threads */
474 fcoe_percpu_clean(lp
);
476 /* Free existing skbs */
477 fcoe_clean_pending_queue(lp
);
479 /* Free memory used by statistical counters */
480 fc_lport_free_stats(lp
);
482 /* Release the net_device and Scsi_Host */
483 dev_put(fc
->real_dev
);
484 scsi_host_put(lp
->host
);
490 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
491 * @lp: the corresponding fc_lport
492 * @xid: the exchange id for this ddp transfer
493 * @sgl: the scatterlist describing this transfer
494 * @sgc: number of sg items
498 static int fcoe_ddp_setup(struct fc_lport
*lp
, u16 xid
,
499 struct scatterlist
*sgl
, unsigned int sgc
)
501 struct net_device
*n
= fcoe_netdev(lp
);
503 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_setup
)
504 return n
->netdev_ops
->ndo_fcoe_ddp_setup(n
, xid
, sgl
, sgc
);
510 * fcoe_ddp_done - calls LLD's ddp_done through net_device
511 * @lp: the corresponding fc_lport
512 * @xid: the exchange id for this ddp transfer
514 * Returns : the length of data that have been completed by ddp
516 static int fcoe_ddp_done(struct fc_lport
*lp
, u16 xid
)
518 struct net_device
*n
= fcoe_netdev(lp
);
520 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_done
)
521 return n
->netdev_ops
->ndo_fcoe_ddp_done(n
, xid
);
525 static struct libfc_function_template fcoe_libfc_fcn_templ
= {
526 .frame_send
= fcoe_xmit
,
527 .ddp_setup
= fcoe_ddp_setup
,
528 .ddp_done
= fcoe_ddp_done
,
532 * fcoe_if_create() - this function creates the fcoe interface
533 * @netdev: pointer the associated netdevice
535 * Creates fc_lport struct and scsi_host for lport, configures lport
536 * and starts fabric login.
538 * Returns : 0 on success
540 static int fcoe_if_create(struct net_device
*netdev
)
543 struct fc_lport
*lp
= NULL
;
544 struct fcoe_softc
*fc
;
545 struct Scsi_Host
*shost
;
549 printk(KERN_DEBUG
"fcoe_if_create:interface on %s\n",
552 lp
= fcoe_hostlist_lookup(netdev
);
556 shost
= libfc_host_alloc(&fcoe_shost_template
,
557 sizeof(struct fcoe_softc
));
559 FC_DBG("Could not allocate host structure\n");
562 lp
= shost_priv(shost
);
565 /* configure fc_lport, e.g., em */
566 rc
= fcoe_lport_config(lp
);
568 FC_DBG("Could not configure lport\n");
575 fcoe_ctlr_init(&fc
->ctlr
);
576 fc
->ctlr
.send
= fcoe_fip_send
;
577 fc
->ctlr
.update_mac
= fcoe_update_src_mac
;
579 /* configure lport network properties */
580 rc
= fcoe_netdev_config(lp
, netdev
);
582 FC_DBG("Could not configure netdev for the interface\n");
583 goto out_netdev_cleanup
;
586 /* configure lport scsi host properties */
587 rc
= fcoe_shost_config(lp
, shost
, &netdev
->dev
);
589 FC_DBG("Could not configure shost for lport\n");
590 goto out_netdev_cleanup
;
593 /* lport exch manager allocation */
594 rc
= fcoe_em_config(lp
);
596 FC_DBG("Could not configure em for lport\n");
597 goto out_netdev_cleanup
;
600 /* Initialize the library */
601 rc
= fcoe_libfc_config(lp
, &fcoe_libfc_fcn_templ
);
603 FC_DBG("Could not configure libfc for lport!\n");
607 /* add to lports list */
608 fcoe_hostlist_add(lp
);
610 lp
->boot_time
= jiffies
;
614 if (!fcoe_link_ok(lp
))
615 fcoe_ctlr_link_up(&fc
->ctlr
);
622 fc_exch_mgr_free(lp
->emp
); /* Free the EM */
624 fcoe_netdev_cleanup(fc
);
626 scsi_host_put(lp
->host
);
631 * fcoe_if_init() - attach to scsi transport
633 * Returns : 0 on success
635 static int __init
fcoe_if_init(void)
637 /* attach to scsi transport */
638 scsi_transport_fcoe_sw
=
639 fc_attach_transport(&fcoe_transport_function
);
641 if (!scsi_transport_fcoe_sw
) {
642 printk(KERN_ERR
"fcoe_init:fc_attach_transport() failed\n");
650 * fcoe_if_exit() - detach from scsi transport
652 * Returns : 0 on success
654 int __exit
fcoe_if_exit(void)
656 fc_release_transport(scsi_transport_fcoe_sw
);
661 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
662 * @cpu: cpu index for the online cpu
664 static void fcoe_percpu_thread_create(unsigned int cpu
)
666 struct fcoe_percpu_s
*p
;
667 struct task_struct
*thread
;
669 p
= &per_cpu(fcoe_percpu
, cpu
);
671 thread
= kthread_create(fcoe_percpu_receive_thread
,
672 (void *)p
, "fcoethread/%d", cpu
);
674 if (likely(!IS_ERR(p
->thread
))) {
675 kthread_bind(thread
, cpu
);
676 wake_up_process(thread
);
678 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
680 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
685 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
686 * @cpu: cpu index the rx thread is to be removed
688 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
689 * current CPU's Rx thread. If the thread being destroyed is bound to
690 * the CPU processing this context the skbs will be freed.
692 static void fcoe_percpu_thread_destroy(unsigned int cpu
)
694 struct fcoe_percpu_s
*p
;
695 struct task_struct
*thread
;
696 struct page
*crc_eof
;
699 struct fcoe_percpu_s
*p0
;
700 unsigned targ_cpu
= smp_processor_id();
701 #endif /* CONFIG_SMP */
703 printk(KERN_DEBUG
"fcoe: Destroying receive thread for CPU %d\n", cpu
);
705 /* Prevent any new skbs from being queued for this CPU. */
706 p
= &per_cpu(fcoe_percpu
, cpu
);
707 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
710 crc_eof
= p
->crc_eof_page
;
711 p
->crc_eof_page
= NULL
;
712 p
->crc_eof_offset
= 0;
713 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
717 * Don't bother moving the skb's if this context is running
718 * on the same CPU that is having its thread destroyed. This
719 * can easily happen when the module is removed.
721 if (cpu
!= targ_cpu
) {
722 p0
= &per_cpu(fcoe_percpu
, targ_cpu
);
723 spin_lock_bh(&p0
->fcoe_rx_list
.lock
);
725 FC_DBG("Moving frames from CPU %d to CPU %d\n",
728 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
729 __skb_queue_tail(&p0
->fcoe_rx_list
, skb
);
730 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
733 * The targeted CPU is not initialized and cannot accept
734 * new skbs. Unlock the targeted CPU and drop the skbs
735 * on the CPU that is going offline.
737 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
739 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
743 * This scenario occurs when the module is being removed
744 * and all threads are being destroyed. skbs will continue
745 * to be shifted from the CPU thread that is being removed
746 * to the CPU thread associated with the CPU that is processing
747 * the module removal. Once there is only one CPU Rx thread it
748 * will reach this case and we will drop all skbs and later
751 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
752 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
754 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
758 * This a non-SMP scenario where the singular Rx thread is
759 * being removed. Free all skbs and stop the thread.
761 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
762 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
764 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
768 kthread_stop(thread
);
775 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
776 * @nfb: callback data block
777 * @action: event triggering the callback
778 * @hcpu: index for the cpu of this event
780 * This creates or destroys per cpu data for fcoe
782 * Returns NOTIFY_OK always.
784 static int fcoe_cpu_callback(struct notifier_block
*nfb
,
785 unsigned long action
, void *hcpu
)
787 unsigned cpu
= (unsigned long)hcpu
;
791 case CPU_ONLINE_FROZEN
:
792 FC_DBG("CPU %x online: Create Rx thread\n", cpu
);
793 fcoe_percpu_thread_create(cpu
);
796 case CPU_DEAD_FROZEN
:
797 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu
);
798 fcoe_percpu_thread_destroy(cpu
);
806 static struct notifier_block fcoe_cpu_notifier
= {
807 .notifier_call
= fcoe_cpu_callback
,
811 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
812 * @skb: the receive skb
813 * @dev: associated net device
815 * @olddev: last device
817 * this function will receive the packet and build fc frame and pass it up
819 * Returns: 0 for success
821 int fcoe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
822 struct packet_type
*ptype
, struct net_device
*olddev
)
825 struct fcoe_rcv_info
*fr
;
826 struct fcoe_softc
*fc
;
827 struct fc_frame_header
*fh
;
828 struct fcoe_percpu_s
*fps
;
830 unsigned int cpu
= 0;
832 fc
= container_of(ptype
, struct fcoe_softc
, fcoe_packet_type
);
834 if (unlikely(lp
== NULL
)) {
835 FC_DBG("cannot find hba structure");
841 if (unlikely(debug_fcoe
)) {
842 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
843 "end:%p sum:%d dev:%s", skb
->len
, skb
->data_len
,
844 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
845 skb_end_pointer(skb
), skb
->csum
,
846 skb
->dev
? skb
->dev
->name
: "<NULL>");
850 /* check for FCOE packet type */
851 if (unlikely(eth_hdr(skb
)->h_proto
!= htons(ETH_P_FCOE
))) {
852 FC_DBG("wrong FC type frame");
857 * Check for minimum frame length, and make sure required FCoE
858 * and FC headers are pulled into the linear data area.
860 if (unlikely((skb
->len
< FCOE_MIN_FRAME
) ||
861 !pskb_may_pull(skb
, FCOE_HEADER_LEN
)))
864 skb_set_transport_header(skb
, sizeof(struct fcoe_hdr
));
865 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
867 oxid
= ntohs(fh
->fh_ox_id
);
869 fr
= fcoe_dev_from_skb(skb
);
875 * The incoming frame exchange id(oxid) is ANDed with num of online
876 * cpu bits to get cpu and then this cpu is used for selecting
877 * a per cpu kernel thread from fcoe_percpu.
879 cpu
= oxid
& (num_online_cpus() - 1);
882 fps
= &per_cpu(fcoe_percpu
, cpu
);
883 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
884 if (unlikely(!fps
->thread
)) {
886 * The targeted CPU is not ready, let's target
887 * the first CPU now. For non-SMP systems this
888 * will check the same CPU twice.
890 FC_DBG("CPU is online, but no receive thread ready "
891 "for incoming skb- using first online CPU.\n");
893 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
894 cpu
= first_cpu(cpu_online_map
);
895 fps
= &per_cpu(fcoe_percpu
, cpu
);
896 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
898 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
904 * We now have a valid CPU that we're targeting for
905 * this skb. We also have this receive thread locked,
906 * so we're free to queue skbs into it's queue.
908 __skb_queue_tail(&fps
->fcoe_rx_list
, skb
);
909 if (fps
->fcoe_rx_list
.qlen
== 1)
910 wake_up_process(fps
->thread
);
912 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
916 fc_lport_get_stats(lp
)->ErrorFrames
++;
924 * fcoe_start_io() - pass to netdev to start xmit for fcoe
925 * @skb: the skb to be xmitted
927 * Returns: 0 for success
929 static inline int fcoe_start_io(struct sk_buff
*skb
)
934 rc
= dev_queue_xmit(skb
);
942 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
943 * @skb: the skb to be xmitted
946 * Returns: 0 for success
948 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
950 struct fcoe_percpu_s
*fps
;
953 fps
= &get_cpu_var(fcoe_percpu
);
954 page
= fps
->crc_eof_page
;
956 page
= alloc_page(GFP_ATOMIC
);
958 put_cpu_var(fcoe_percpu
);
961 fps
->crc_eof_page
= page
;
962 fps
->crc_eof_offset
= 0;
966 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
967 fps
->crc_eof_offset
, tlen
);
969 skb
->data_len
+= tlen
;
970 skb
->truesize
+= tlen
;
971 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
973 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
974 fps
->crc_eof_page
= NULL
;
975 fps
->crc_eof_offset
= 0;
978 put_cpu_var(fcoe_percpu
);
983 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
984 * @fp: the fc_frame containing data to be checksummed
986 * This uses crc32() to calculate the crc for fc frame
987 * Return : 32 bit crc
989 u32
fcoe_fc_crc(struct fc_frame
*fp
)
991 struct sk_buff
*skb
= fp_skb(fp
);
992 struct skb_frag_struct
*frag
;
994 unsigned long off
, len
, clen
;
998 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
1000 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1001 frag
= &skb_shinfo(skb
)->frags
[i
];
1002 off
= frag
->page_offset
;
1005 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
1006 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
1007 KM_SKB_DATA_SOFTIRQ
);
1008 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
1009 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
1018 * fcoe_xmit() - FCoE frame transmit function
1019 * @lp: the associated local port
1020 * @fp: the fc_frame to be transmitted
1022 * Return : 0 for success
1024 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
1029 struct fcoe_crc_eof
*cp
;
1030 struct sk_buff
*skb
;
1031 struct fcoe_dev_stats
*stats
;
1032 struct fc_frame_header
*fh
;
1033 unsigned int hlen
; /* header length implies the version */
1034 unsigned int tlen
; /* trailer length */
1035 unsigned int elen
; /* eth header, may include vlan */
1036 struct fcoe_softc
*fc
;
1038 struct fcoe_hdr
*hp
;
1040 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
1042 fc
= lport_priv(lp
);
1043 fh
= fc_frame_header_get(fp
);
1045 wlen
= skb
->len
/ FCOE_WORD_TO_BYTE
;
1052 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
) &&
1053 fcoe_ctlr_els_send(&fc
->ctlr
, skb
))
1059 elen
= (fc
->real_dev
->priv_flags
& IFF_802_1Q_VLAN
) ?
1060 sizeof(struct vlan_ethhdr
) : sizeof(struct ethhdr
);
1061 hlen
= sizeof(struct fcoe_hdr
);
1062 tlen
= sizeof(struct fcoe_crc_eof
);
1063 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
1066 if (likely(lp
->crc_offload
)) {
1067 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1068 skb
->csum_start
= skb_headroom(skb
);
1069 skb
->csum_offset
= skb
->len
;
1072 skb
->ip_summed
= CHECKSUM_NONE
;
1073 crc
= fcoe_fc_crc(fp
);
1076 /* copy fc crc and eof to the skb buff */
1077 if (skb_is_nonlinear(skb
)) {
1079 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
1083 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
1084 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
1085 + frag
->page_offset
;
1087 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
1090 memset(cp
, 0, sizeof(*cp
));
1092 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
1094 if (skb_is_nonlinear(skb
)) {
1095 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
1099 /* adjust skb network/transport offsets to match mac/fcoe/fc */
1100 skb_push(skb
, elen
+ hlen
);
1101 skb_reset_mac_header(skb
);
1102 skb_reset_network_header(skb
);
1103 skb
->mac_len
= elen
;
1104 skb
->protocol
= htons(ETH_P_FCOE
);
1105 skb
->dev
= fc
->real_dev
;
1107 /* fill up mac and fcoe headers */
1109 eh
->h_proto
= htons(ETH_P_FCOE
);
1110 if (fc
->ctlr
.map_dest
)
1111 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
1113 /* insert GW address */
1114 memcpy(eh
->h_dest
, fc
->ctlr
.dest_addr
, ETH_ALEN
);
1116 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
))
1117 memcpy(eh
->h_source
, fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
1119 memcpy(eh
->h_source
, fc
->ctlr
.data_src_addr
, ETH_ALEN
);
1121 hp
= (struct fcoe_hdr
*)(eh
+ 1);
1122 memset(hp
, 0, sizeof(*hp
));
1124 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
1128 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1129 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
1130 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
1131 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
1133 skb_shinfo(skb
)->gso_type
= 0;
1134 skb_shinfo(skb
)->gso_size
= 0;
1137 /* update tx stats: regardless if LLD fails */
1138 stats
= fc_lport_get_stats(lp
);
1140 stats
->TxWords
+= wlen
;
1142 /* send down to lld */
1144 if (fc
->fcoe_pending_queue
.qlen
)
1145 rc
= fcoe_check_wait_queue(lp
);
1148 rc
= fcoe_start_io(skb
);
1151 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1152 __skb_queue_tail(&fc
->fcoe_pending_queue
, skb
);
1153 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1154 if (fc
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
1162 * fcoe_percpu_receive_thread() - recv thread per cpu
1163 * @arg: ptr to the fcoe per cpu struct
1165 * Return: 0 for success
1167 int fcoe_percpu_receive_thread(void *arg
)
1169 struct fcoe_percpu_s
*p
= arg
;
1171 struct fc_lport
*lp
;
1172 struct fcoe_rcv_info
*fr
;
1173 struct fcoe_dev_stats
*stats
;
1174 struct fc_frame_header
*fh
;
1175 struct sk_buff
*skb
;
1176 struct fcoe_crc_eof crc_eof
;
1177 struct fc_frame
*fp
;
1179 struct fcoe_softc
*fc
;
1180 struct fcoe_hdr
*hp
;
1182 set_user_nice(current
, -20);
1184 while (!kthread_should_stop()) {
1186 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1187 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
1188 set_current_state(TASK_INTERRUPTIBLE
);
1189 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1191 set_current_state(TASK_RUNNING
);
1192 if (kthread_should_stop())
1194 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1196 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1197 fr
= fcoe_dev_from_skb(skb
);
1199 if (unlikely(lp
== NULL
)) {
1200 FC_DBG("invalid HBA Structure");
1205 if (unlikely(debug_fcoe
)) {
1206 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1207 "tail:%p end:%p sum:%d dev:%s",
1208 skb
->len
, skb
->data_len
,
1209 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
1210 skb_end_pointer(skb
), skb
->csum
,
1211 skb
->dev
? skb
->dev
->name
: "<NULL>");
1215 * Save source MAC address before discarding header.
1217 fc
= lport_priv(lp
);
1218 if (skb_is_nonlinear(skb
))
1219 skb_linearize(skb
); /* not ideal */
1220 mac
= eth_hdr(skb
)->h_source
;
1223 * Frame length checks and setting up the header pointers
1224 * was done in fcoe_rcv already.
1226 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
1227 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1229 stats
= fc_lport_get_stats(lp
);
1230 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
1231 if (stats
->ErrorFrames
< 5)
1232 printk(KERN_WARNING
"FCoE version "
1233 "mismatch: The frame has "
1234 "version %x, but the "
1235 "initiator supports version "
1236 "%x\n", FC_FCOE_DECAPS_VER(hp
),
1238 stats
->ErrorFrames
++;
1243 skb_pull(skb
, sizeof(struct fcoe_hdr
));
1244 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
1247 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
1249 fp
= (struct fc_frame
*)skb
;
1252 fr_sof(fp
) = hp
->fcoe_sof
;
1254 /* Copy out the CRC and EOF trailer for access */
1255 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
1259 fr_eof(fp
) = crc_eof
.fcoe_eof
;
1260 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
1261 if (pskb_trim(skb
, fr_len
)) {
1267 * We only check CRC if no offload is available and if it is
1268 * it's solicited data, in which case, the FCP layer would
1269 * check it during the copy.
1271 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
1272 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1274 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
1276 fh
= fc_frame_header_get(fp
);
1277 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
1278 fh
->fh_type
== FC_TYPE_FCP
) {
1279 fc_exch_recv(lp
, lp
->emp
, fp
);
1282 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
1283 if (le32_to_cpu(fr_crc(fp
)) !=
1284 ~crc32(~0, skb
->data
, fr_len
)) {
1285 if (debug_fcoe
|| stats
->InvalidCRCCount
< 5)
1286 printk(KERN_WARNING
"fcoe: dropping "
1287 "frame with CRC error\n");
1288 stats
->InvalidCRCCount
++;
1289 stats
->ErrorFrames
++;
1293 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1295 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
) &&
1296 fcoe_ctlr_recv_flogi(&fc
->ctlr
, fp
, mac
)) {
1300 fc_exch_recv(lp
, lp
->emp
, fp
);
1306 * fcoe_watchdog() - fcoe timer callback
1309 * This checks the pending queue length for fcoe and set lport qfull
1310 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1313 * Returns: 0 for success
1315 void fcoe_watchdog(ulong vp
)
1317 struct fcoe_softc
*fc
;
1319 read_lock(&fcoe_hostlist_lock
);
1320 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1322 fcoe_check_wait_queue(fc
->ctlr
.lp
);
1324 read_unlock(&fcoe_hostlist_lock
);
1326 fcoe_timer
.expires
= jiffies
+ (1 * HZ
);
1327 add_timer(&fcoe_timer
);
1332 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1335 * This empties the wait_queue, dequeue the head of the wait_queue queue
1336 * and calls fcoe_start_io() for each packet, if all skb have been
1337 * transmitted, return qlen or -1 if a error occurs, then restore
1338 * wait_queue and try again later.
1340 * The wait_queue is used when the skb transmit fails. skb will go
1341 * in the wait_queue which will be emptied by the timer function or
1342 * by the next skb transmit.
1344 * Returns: 0 for success
1346 static int fcoe_check_wait_queue(struct fc_lport
*lp
)
1348 struct fcoe_softc
*fc
= lport_priv(lp
);
1349 struct sk_buff
*skb
;
1352 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1353 if (fc
->fcoe_pending_queue_active
)
1355 fc
->fcoe_pending_queue_active
= 1;
1357 while (fc
->fcoe_pending_queue
.qlen
) {
1358 /* keep qlen > 0 until fcoe_start_io succeeds */
1359 fc
->fcoe_pending_queue
.qlen
++;
1360 skb
= __skb_dequeue(&fc
->fcoe_pending_queue
);
1362 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1363 rc
= fcoe_start_io(skb
);
1364 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1367 __skb_queue_head(&fc
->fcoe_pending_queue
, skb
);
1368 /* undo temporary increment above */
1369 fc
->fcoe_pending_queue
.qlen
--;
1372 /* undo temporary increment above */
1373 fc
->fcoe_pending_queue
.qlen
--;
1376 if (fc
->fcoe_pending_queue
.qlen
< FCOE_LOW_QUEUE_DEPTH
)
1378 fc
->fcoe_pending_queue_active
= 0;
1379 rc
= fc
->fcoe_pending_queue
.qlen
;
1381 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1386 * fcoe_dev_setup() - setup link change notification interface
1388 static void fcoe_dev_setup()
1390 register_netdevice_notifier(&fcoe_notifier
);
1394 * fcoe_dev_setup() - cleanup link change notification interface
1396 static void fcoe_dev_cleanup(void)
1398 unregister_netdevice_notifier(&fcoe_notifier
);
1402 * fcoe_device_notification() - netdev event notification callback
1403 * @notifier: context of the notification
1404 * @event: type of event
1405 * @ptr: fixed array for output parsed ifname
1407 * This function is called by the ethernet driver in case of link change event
1409 * Returns: 0 for success
1411 static int fcoe_device_notification(struct notifier_block
*notifier
,
1412 ulong event
, void *ptr
)
1414 struct fc_lport
*lp
= NULL
;
1415 struct net_device
*real_dev
= ptr
;
1416 struct fcoe_softc
*fc
;
1417 struct fcoe_dev_stats
*stats
;
1418 u32 link_possible
= 1;
1422 read_lock(&fcoe_hostlist_lock
);
1423 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1424 if (fc
->real_dev
== real_dev
) {
1429 read_unlock(&fcoe_hostlist_lock
);
1437 case NETDEV_GOING_DOWN
:
1443 case NETDEV_CHANGEMTU
:
1444 mfs
= fc
->real_dev
->mtu
-
1445 (sizeof(struct fcoe_hdr
) +
1446 sizeof(struct fcoe_crc_eof
));
1447 if (mfs
>= FC_MIN_MAX_FRAME
)
1448 fc_set_mfs(lp
, mfs
);
1450 case NETDEV_REGISTER
:
1453 FC_DBG("Unknown event %ld from netdev netlink\n", event
);
1455 if (link_possible
&& !fcoe_link_ok(lp
))
1456 fcoe_ctlr_link_up(&fc
->ctlr
);
1457 else if (fcoe_ctlr_link_down(&fc
->ctlr
)) {
1458 stats
= fc_lport_get_stats(lp
);
1459 stats
->LinkFailureCount
++;
1460 fcoe_clean_pending_queue(lp
);
1467 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1468 * @buffer: incoming buffer to be copied
1470 * Returns: NULL or ptr to net_device
1472 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
1475 char ifname
[IFNAMSIZ
+ 2];
1478 strlcpy(ifname
, buffer
, IFNAMSIZ
);
1479 cp
= ifname
+ strlen(ifname
);
1480 while (--cp
>= ifname
&& *cp
== '\n')
1482 return dev_get_by_name(&init_net
, ifname
);
1488 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1489 * @netdev: the target netdev
1491 * Returns: ptr to the struct module, NULL for failure
1493 static struct module
*
1494 fcoe_netdev_to_module_owner(const struct net_device
*netdev
)
1501 dev
= netdev
->dev
.parent
;
1508 return dev
->driver
->owner
;
1512 * fcoe_ethdrv_get() - Hold the Ethernet driver
1513 * @netdev: the target netdev
1515 * Holds the Ethernet driver module by try_module_get() for
1516 * the corresponding netdev.
1518 * Returns: 0 for success
1520 static int fcoe_ethdrv_get(const struct net_device
*netdev
)
1522 struct module
*owner
;
1524 owner
= fcoe_netdev_to_module_owner(netdev
);
1526 printk(KERN_DEBUG
"fcoe:hold driver module %s for %s\n",
1527 module_name(owner
), netdev
->name
);
1528 return try_module_get(owner
);
1534 * fcoe_ethdrv_put() - Release the Ethernet driver
1535 * @netdev: the target netdev
1537 * Releases the Ethernet driver module by module_put for
1538 * the corresponding netdev.
1540 * Returns: 0 for success
1542 static int fcoe_ethdrv_put(const struct net_device
*netdev
)
1544 struct module
*owner
;
1546 owner
= fcoe_netdev_to_module_owner(netdev
);
1548 printk(KERN_DEBUG
"fcoe:release driver module %s for %s\n",
1549 module_name(owner
), netdev
->name
);
1557 * fcoe_destroy() - handles the destroy from sysfs
1558 * @buffer: expected to be an eth if name
1559 * @kp: associated kernel param
1561 * Returns: 0 for success
1563 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1566 struct net_device
*netdev
;
1568 netdev
= fcoe_if_to_netdev(buffer
);
1573 /* look for existing lport */
1574 if (!fcoe_hostlist_lookup(netdev
)) {
1578 rc
= fcoe_if_destroy(netdev
);
1580 printk(KERN_ERR
"fcoe: fcoe_if_destroy(%s) failed\n",
1585 fcoe_ethdrv_put(netdev
);
1594 * fcoe_create() - Handles the create call from sysfs
1595 * @buffer: expected to be an eth if name
1596 * @kp: associated kernel param
1598 * Returns: 0 for success
1600 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1603 struct net_device
*netdev
;
1605 netdev
= fcoe_if_to_netdev(buffer
);
1610 /* look for existing lport */
1611 if (fcoe_hostlist_lookup(netdev
)) {
1615 fcoe_ethdrv_get(netdev
);
1617 rc
= fcoe_if_create(netdev
);
1619 printk(KERN_ERR
"fcoe: fcoe_if_create(%s) failed\n",
1621 fcoe_ethdrv_put(netdev
);
1632 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1633 __MODULE_PARM_TYPE(create
, "string");
1634 MODULE_PARM_DESC(create
, "Create fcoe port using net device passed in.");
1635 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1636 __MODULE_PARM_TYPE(destroy
, "string");
1637 MODULE_PARM_DESC(destroy
, "Destroy fcoe port");
1640 * fcoe_link_ok() - Check if link is ok for the fc_lport
1641 * @lp: ptr to the fc_lport
1643 * Any permanently-disqualifying conditions have been previously checked.
1644 * This also updates the speed setting, which may change with link for 100/1000.
1646 * This function should probably be checking for PAUSE support at some point
1647 * in the future. Currently Per-priority-pause is not determinable using
1648 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1650 * Returns: 0 if link is OK for use by FCoE.
1653 int fcoe_link_ok(struct fc_lport
*lp
)
1655 struct fcoe_softc
*fc
= lport_priv(lp
);
1656 struct net_device
*dev
= fc
->real_dev
;
1657 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1660 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
)) {
1662 if (dev
->ethtool_ops
->get_settings
) {
1663 dev
->ethtool_ops
->get_settings(dev
, &ecmd
);
1664 lp
->link_supported_speeds
&=
1665 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1666 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1667 SUPPORTED_1000baseT_Full
))
1668 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1669 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1670 lp
->link_supported_speeds
|=
1671 FC_PORTSPEED_10GBIT
;
1672 if (ecmd
.speed
== SPEED_1000
)
1673 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1674 if (ecmd
.speed
== SPEED_10000
)
1675 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1684 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1687 void fcoe_percpu_clean(struct fc_lport
*lp
)
1689 struct fcoe_percpu_s
*pp
;
1690 struct fcoe_rcv_info
*fr
;
1691 struct sk_buff_head
*list
;
1692 struct sk_buff
*skb
, *next
;
1693 struct sk_buff
*head
;
1696 for_each_possible_cpu(cpu
) {
1697 pp
= &per_cpu(fcoe_percpu
, cpu
);
1698 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1699 list
= &pp
->fcoe_rx_list
;
1701 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1704 fr
= fcoe_dev_from_skb(skb
);
1705 if (fr
->fr_dev
== lp
) {
1706 __skb_unlink(skb
, list
);
1710 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1715 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1716 * @lp: the corresponding fc_lport
1720 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1722 struct fcoe_softc
*fc
= lport_priv(lp
);
1723 struct sk_buff
*skb
;
1725 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1726 while ((skb
= __skb_dequeue(&fc
->fcoe_pending_queue
)) != NULL
) {
1727 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1729 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1731 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1735 * fcoe_reset() - Resets the fcoe
1736 * @shost: shost the reset is from
1740 int fcoe_reset(struct Scsi_Host
*shost
)
1742 struct fc_lport
*lport
= shost_priv(shost
);
1743 fc_lport_reset(lport
);
1748 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1749 * @dev: this is currently ptr to net_device
1751 * Returns: NULL or the located fcoe_softc
1753 static struct fcoe_softc
*
1754 fcoe_hostlist_lookup_softc(const struct net_device
*dev
)
1756 struct fcoe_softc
*fc
;
1758 read_lock(&fcoe_hostlist_lock
);
1759 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1760 if (fc
->real_dev
== dev
) {
1761 read_unlock(&fcoe_hostlist_lock
);
1765 read_unlock(&fcoe_hostlist_lock
);
1770 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1771 * @netdev: ptr to net_device
1773 * Returns: 0 for success
1775 struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1777 struct fcoe_softc
*fc
;
1779 fc
= fcoe_hostlist_lookup_softc(netdev
);
1781 return (fc
) ? fc
->ctlr
.lp
: NULL
;
1785 * fcoe_hostlist_add() - Add a lport to lports list
1786 * @lp: ptr to the fc_lport to be added
1788 * Returns: 0 for success
1790 int fcoe_hostlist_add(const struct fc_lport
*lp
)
1792 struct fcoe_softc
*fc
;
1794 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1796 fc
= lport_priv(lp
);
1797 write_lock_bh(&fcoe_hostlist_lock
);
1798 list_add_tail(&fc
->list
, &fcoe_hostlist
);
1799 write_unlock_bh(&fcoe_hostlist_lock
);
1805 * fcoe_hostlist_remove() - remove a lport from lports list
1806 * @lp: ptr to the fc_lport to be removed
1808 * Returns: 0 for success
1810 int fcoe_hostlist_remove(const struct fc_lport
*lp
)
1812 struct fcoe_softc
*fc
;
1814 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1816 write_lock_bh(&fcoe_hostlist_lock
);
1817 list_del(&fc
->list
);
1818 write_unlock_bh(&fcoe_hostlist_lock
);
1824 * fcoe_init() - fcoe module loading initialization
1826 * Returns 0 on success, negative on failure
1828 static int __init
fcoe_init(void)
1832 struct fcoe_percpu_s
*p
;
1834 INIT_LIST_HEAD(&fcoe_hostlist
);
1835 rwlock_init(&fcoe_hostlist_lock
);
1837 for_each_possible_cpu(cpu
) {
1838 p
= &per_cpu(fcoe_percpu
, cpu
);
1839 skb_queue_head_init(&p
->fcoe_rx_list
);
1842 for_each_online_cpu(cpu
)
1843 fcoe_percpu_thread_create(cpu
);
1845 /* Initialize per CPU interrupt thread */
1846 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1850 /* Setup link change notification */
1853 setup_timer(&fcoe_timer
, fcoe_watchdog
, 0);
1855 mod_timer(&fcoe_timer
, jiffies
+ (10 * HZ
));
1862 for_each_online_cpu(cpu
) {
1863 fcoe_percpu_thread_destroy(cpu
);
1868 module_init(fcoe_init
);
1871 * fcoe_exit() - fcoe module unloading cleanup
1873 * Returns 0 on success, negative on failure
1875 static void __exit
fcoe_exit(void)
1878 struct fcoe_softc
*fc
, *tmp
;
1882 /* Stop the timer */
1883 del_timer_sync(&fcoe_timer
);
1885 /* releases the associated fcoe hosts */
1886 list_for_each_entry_safe(fc
, tmp
, &fcoe_hostlist
, list
)
1887 fcoe_if_destroy(fc
->real_dev
);
1889 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
1891 for_each_online_cpu(cpu
) {
1892 fcoe_percpu_thread_destroy(cpu
);
1895 /* detach from scsi transport */
1898 module_exit(fcoe_exit
);