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 Prototyes */
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
193 * @shost: ptr to the parent scsi host
195 * Returns: 0 for success
197 static int fcoe_lport_config(struct fc_lport
*lp
)
201 lp
->max_retry_count
= 3;
202 lp
->e_d_tov
= 2 * 1000; /* FC-FS default */
203 lp
->r_a_tov
= 2 * 2 * 1000;
204 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
205 FCP_SPPF_RETRY
| FCP_SPPF_CONF_COMPL
);
207 fc_lport_init_stats(lp
);
209 /* lport fc_lport related configuration */
212 /* offload related configuration */
223 * fcoe_netdev_cleanup() - clean up netdev configurations
224 * @fc: ptr to the fcoe_softc
226 void fcoe_netdev_cleanup(struct fcoe_softc
*fc
)
228 u8 flogi_maddr
[ETH_ALEN
];
230 /* Don't listen for Ethernet packets anymore */
231 dev_remove_pack(&fc
->fcoe_packet_type
);
232 dev_remove_pack(&fc
->fip_packet_type
);
234 /* Delete secondary MAC addresses */
236 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
237 dev_unicast_delete(fc
->real_dev
, flogi_maddr
, ETH_ALEN
);
238 if (!is_zero_ether_addr(fc
->ctlr
.data_src_addr
))
239 dev_unicast_delete(fc
->real_dev
,
240 fc
->ctlr
.data_src_addr
, ETH_ALEN
);
242 dev_unicast_delete(fc
->real_dev
,
243 fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
244 dev_mc_delete(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
249 * fcoe_netdev_config() - Set up netdev for SW FCoE
250 * @lp : ptr to the fc_lport
251 * @netdev : ptr to the associated netdevice struct
253 * Must be called after fcoe_lport_config() as it will use lport mutex
255 * Returns : 0 for success
257 static int fcoe_netdev_config(struct fc_lport
*lp
, struct net_device
*netdev
)
261 struct fcoe_softc
*fc
;
262 u8 flogi_maddr
[ETH_ALEN
];
263 struct netdev_hw_addr
*ha
;
265 /* Setup lport private data to point to fcoe softc */
268 fc
->real_dev
= netdev
;
269 fc
->phys_dev
= netdev
;
271 /* Require support for get_pauseparam ethtool op. */
272 if (netdev
->priv_flags
& IFF_802_1Q_VLAN
)
273 fc
->phys_dev
= vlan_dev_real_dev(netdev
);
275 /* Do not support for bonding device */
276 if ((fc
->real_dev
->priv_flags
& IFF_MASTER_ALB
) ||
277 (fc
->real_dev
->priv_flags
& IFF_SLAVE_INACTIVE
) ||
278 (fc
->real_dev
->priv_flags
& IFF_MASTER_8023AD
)) {
283 * Determine max frame size based on underlying device and optional
284 * user-configured limit. If the MFS is too low, fcoe_link_ok()
285 * will return 0, so do this first.
287 mfs
= fc
->real_dev
->mtu
- (sizeof(struct fcoe_hdr
) +
288 sizeof(struct fcoe_crc_eof
));
289 if (fc_set_mfs(lp
, mfs
))
292 /* offload features support */
293 if (fc
->real_dev
->features
& NETIF_F_SG
)
296 #ifdef NETIF_F_FCOE_CRC
297 if (netdev
->features
& NETIF_F_FCOE_CRC
) {
299 printk(KERN_DEBUG
"fcoe:%s supports FCCRC offload\n",
304 if (netdev
->features
& NETIF_F_FSO
) {
306 lp
->lso_max
= netdev
->gso_max_size
;
307 printk(KERN_DEBUG
"fcoe:%s supports LSO for max len 0x%x\n",
308 netdev
->name
, lp
->lso_max
);
311 if (netdev
->fcoe_ddp_xid
) {
313 lp
->lro_xid
= netdev
->fcoe_ddp_xid
;
314 printk(KERN_DEBUG
"fcoe:%s supports LRO for max xid 0x%x\n",
315 netdev
->name
, lp
->lro_xid
);
317 skb_queue_head_init(&fc
->fcoe_pending_queue
);
318 fc
->fcoe_pending_queue_active
= 0;
320 /* look for SAN MAC address, if multiple SAN MACs exist, only
321 * use the first one for SPMA */
323 for_each_dev_addr(netdev
, ha
) {
324 if ((ha
->type
== NETDEV_HW_ADDR_T_SAN
) &&
325 (is_valid_ether_addr(fc
->ctlr
.ctl_src_addr
))) {
326 memcpy(fc
->ctlr
.ctl_src_addr
, ha
->addr
, ETH_ALEN
);
333 /* setup Source Mac Address */
335 memcpy(fc
->ctlr
.ctl_src_addr
, fc
->real_dev
->dev_addr
,
336 fc
->real_dev
->addr_len
);
338 wwnn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 1, 0);
339 fc_set_wwnn(lp
, wwnn
);
340 /* XXX - 3rd arg needs to be vlan id */
341 wwpn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 2, 0);
342 fc_set_wwpn(lp
, wwpn
);
345 * Add FCoE MAC address as second unicast MAC address
346 * or enter promiscuous mode if not capable of listening
347 * for multiple unicast MACs.
350 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
351 dev_unicast_add(fc
->real_dev
, flogi_maddr
, ETH_ALEN
);
353 dev_unicast_add(fc
->real_dev
, fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
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 revieve 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 singluar 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 * @odldev: 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
++;
922 EXPORT_SYMBOL_GPL(fcoe_rcv
);
925 * fcoe_start_io() - pass to netdev to start xmit for fcoe
926 * @skb: the skb to be xmitted
928 * Returns: 0 for success
930 static inline int fcoe_start_io(struct sk_buff
*skb
)
935 rc
= dev_queue_xmit(skb
);
943 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
944 * @skb: the skb to be xmitted
947 * Returns: 0 for success
949 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
951 struct fcoe_percpu_s
*fps
;
954 fps
= &get_cpu_var(fcoe_percpu
);
955 page
= fps
->crc_eof_page
;
957 page
= alloc_page(GFP_ATOMIC
);
959 put_cpu_var(fcoe_percpu
);
962 fps
->crc_eof_page
= page
;
963 fps
->crc_eof_offset
= 0;
967 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
968 fps
->crc_eof_offset
, tlen
);
970 skb
->data_len
+= tlen
;
971 skb
->truesize
+= tlen
;
972 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
974 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
975 fps
->crc_eof_page
= NULL
;
976 fps
->crc_eof_offset
= 0;
979 put_cpu_var(fcoe_percpu
);
984 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
985 * @fp: the fc_frame containg data to be checksummed
987 * This uses crc32() to calculate the crc for fc frame
988 * Return : 32 bit crc
990 u32
fcoe_fc_crc(struct fc_frame
*fp
)
992 struct sk_buff
*skb
= fp_skb(fp
);
993 struct skb_frag_struct
*frag
;
995 unsigned long off
, len
, clen
;
999 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
1001 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1002 frag
= &skb_shinfo(skb
)->frags
[i
];
1003 off
= frag
->page_offset
;
1006 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
1007 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
1008 KM_SKB_DATA_SOFTIRQ
);
1009 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
1010 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
1019 * fcoe_xmit() - FCoE frame transmit function
1020 * @lp: the associated local port
1021 * @fp: the fc_frame to be transmitted
1023 * Return : 0 for success
1025 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
1030 struct fcoe_crc_eof
*cp
;
1031 struct sk_buff
*skb
;
1032 struct fcoe_dev_stats
*stats
;
1033 struct fc_frame_header
*fh
;
1034 unsigned int hlen
; /* header length implies the version */
1035 unsigned int tlen
; /* trailer length */
1036 unsigned int elen
; /* eth header, may include vlan */
1037 struct fcoe_softc
*fc
;
1039 struct fcoe_hdr
*hp
;
1041 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
1043 fc
= lport_priv(lp
);
1044 fh
= fc_frame_header_get(fp
);
1046 wlen
= skb
->len
/ FCOE_WORD_TO_BYTE
;
1053 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
) &&
1054 fcoe_ctlr_els_send(&fc
->ctlr
, skb
))
1060 elen
= (fc
->real_dev
->priv_flags
& IFF_802_1Q_VLAN
) ?
1061 sizeof(struct vlan_ethhdr
) : sizeof(struct ethhdr
);
1062 hlen
= sizeof(struct fcoe_hdr
);
1063 tlen
= sizeof(struct fcoe_crc_eof
);
1064 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
1067 if (likely(lp
->crc_offload
)) {
1068 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1069 skb
->csum_start
= skb_headroom(skb
);
1070 skb
->csum_offset
= skb
->len
;
1073 skb
->ip_summed
= CHECKSUM_NONE
;
1074 crc
= fcoe_fc_crc(fp
);
1077 /* copy fc crc and eof to the skb buff */
1078 if (skb_is_nonlinear(skb
)) {
1080 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
1084 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
1085 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
1086 + frag
->page_offset
;
1088 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
1091 memset(cp
, 0, sizeof(*cp
));
1093 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
1095 if (skb_is_nonlinear(skb
)) {
1096 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
1100 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
1101 skb_push(skb
, elen
+ hlen
);
1102 skb_reset_mac_header(skb
);
1103 skb_reset_network_header(skb
);
1104 skb
->mac_len
= elen
;
1105 skb
->protocol
= htons(ETH_P_FCOE
);
1106 skb
->dev
= fc
->real_dev
;
1108 /* fill up mac and fcoe headers */
1110 eh
->h_proto
= htons(ETH_P_FCOE
);
1111 if (fc
->ctlr
.map_dest
)
1112 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
1114 /* insert GW address */
1115 memcpy(eh
->h_dest
, fc
->ctlr
.dest_addr
, ETH_ALEN
);
1117 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
))
1118 memcpy(eh
->h_source
, fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
1120 memcpy(eh
->h_source
, fc
->ctlr
.data_src_addr
, ETH_ALEN
);
1122 hp
= (struct fcoe_hdr
*)(eh
+ 1);
1123 memset(hp
, 0, sizeof(*hp
));
1125 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
1129 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1130 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
1131 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
1132 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
1134 skb_shinfo(skb
)->gso_type
= 0;
1135 skb_shinfo(skb
)->gso_size
= 0;
1138 /* update tx stats: regardless if LLD fails */
1139 stats
= fc_lport_get_stats(lp
);
1141 stats
->TxWords
+= wlen
;
1143 /* send down to lld */
1145 if (fc
->fcoe_pending_queue
.qlen
)
1146 rc
= fcoe_check_wait_queue(lp
);
1149 rc
= fcoe_start_io(skb
);
1152 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1153 __skb_queue_tail(&fc
->fcoe_pending_queue
, skb
);
1154 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1155 if (fc
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
1161 EXPORT_SYMBOL_GPL(fcoe_xmit
);
1164 * fcoe_percpu_receive_thread() - recv thread per cpu
1165 * @arg: ptr to the fcoe per cpu struct
1167 * Return: 0 for success
1169 int fcoe_percpu_receive_thread(void *arg
)
1171 struct fcoe_percpu_s
*p
= arg
;
1173 struct fc_lport
*lp
;
1174 struct fcoe_rcv_info
*fr
;
1175 struct fcoe_dev_stats
*stats
;
1176 struct fc_frame_header
*fh
;
1177 struct sk_buff
*skb
;
1178 struct fcoe_crc_eof crc_eof
;
1179 struct fc_frame
*fp
;
1181 struct fcoe_softc
*fc
;
1182 struct fcoe_hdr
*hp
;
1184 set_user_nice(current
, -20);
1186 while (!kthread_should_stop()) {
1188 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1189 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
1190 set_current_state(TASK_INTERRUPTIBLE
);
1191 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1193 set_current_state(TASK_RUNNING
);
1194 if (kthread_should_stop())
1196 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1198 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1199 fr
= fcoe_dev_from_skb(skb
);
1201 if (unlikely(lp
== NULL
)) {
1202 FC_DBG("invalid HBA Structure");
1207 if (unlikely(debug_fcoe
)) {
1208 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1209 "tail:%p end:%p sum:%d dev:%s",
1210 skb
->len
, skb
->data_len
,
1211 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
1212 skb_end_pointer(skb
), skb
->csum
,
1213 skb
->dev
? skb
->dev
->name
: "<NULL>");
1217 * Save source MAC address before discarding header.
1219 fc
= lport_priv(lp
);
1220 if (skb_is_nonlinear(skb
))
1221 skb_linearize(skb
); /* not ideal */
1222 mac
= eth_hdr(skb
)->h_source
;
1225 * Frame length checks and setting up the header pointers
1226 * was done in fcoe_rcv already.
1228 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
1229 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1231 stats
= fc_lport_get_stats(lp
);
1232 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
1233 if (stats
->ErrorFrames
< 5)
1234 printk(KERN_WARNING
"FCoE version "
1235 "mismatch: The frame has "
1236 "version %x, but the "
1237 "initiator supports version "
1238 "%x\n", FC_FCOE_DECAPS_VER(hp
),
1240 stats
->ErrorFrames
++;
1245 skb_pull(skb
, sizeof(struct fcoe_hdr
));
1246 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
1249 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
1251 fp
= (struct fc_frame
*)skb
;
1254 fr_sof(fp
) = hp
->fcoe_sof
;
1256 /* Copy out the CRC and EOF trailer for access */
1257 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
1261 fr_eof(fp
) = crc_eof
.fcoe_eof
;
1262 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
1263 if (pskb_trim(skb
, fr_len
)) {
1269 * We only check CRC if no offload is available and if it is
1270 * it's solicited data, in which case, the FCP layer would
1271 * check it during the copy.
1273 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
1274 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1276 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
1278 fh
= fc_frame_header_get(fp
);
1279 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
1280 fh
->fh_type
== FC_TYPE_FCP
) {
1281 fc_exch_recv(lp
, lp
->emp
, fp
);
1284 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
1285 if (le32_to_cpu(fr_crc(fp
)) !=
1286 ~crc32(~0, skb
->data
, fr_len
)) {
1287 if (debug_fcoe
|| stats
->InvalidCRCCount
< 5)
1288 printk(KERN_WARNING
"fcoe: dropping "
1289 "frame with CRC error\n");
1290 stats
->InvalidCRCCount
++;
1291 stats
->ErrorFrames
++;
1295 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1297 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
) &&
1298 fcoe_ctlr_recv_flogi(&fc
->ctlr
, fp
, mac
)) {
1302 fc_exch_recv(lp
, lp
->emp
, fp
);
1308 * fcoe_watchdog() - fcoe timer callback
1311 * This checks the pending queue length for fcoe and set lport qfull
1312 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
1315 * Returns: 0 for success
1317 void fcoe_watchdog(ulong vp
)
1319 struct fcoe_softc
*fc
;
1321 read_lock(&fcoe_hostlist_lock
);
1322 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1324 fcoe_check_wait_queue(fc
->ctlr
.lp
);
1326 read_unlock(&fcoe_hostlist_lock
);
1328 fcoe_timer
.expires
= jiffies
+ (1 * HZ
);
1329 add_timer(&fcoe_timer
);
1334 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
1335 * @lp: the fc_port for this skb
1336 * @skb: the associated skb to be xmitted
1338 * This empties the wait_queue, dequeue the head of the wait_queue queue
1339 * and calls fcoe_start_io() for each packet, if all skb have been
1340 * transmitted, return qlen or -1 if a error occurs, then restore
1341 * wait_queue and try again later.
1343 * The wait_queue is used when the skb transmit fails. skb will go
1344 * in the wait_queue which will be emptied by the time function OR
1345 * by the next skb transmit.
1347 * Returns: 0 for success
1349 static int fcoe_check_wait_queue(struct fc_lport
*lp
)
1351 struct fcoe_softc
*fc
= lport_priv(lp
);
1352 struct sk_buff
*skb
;
1355 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1356 if (fc
->fcoe_pending_queue_active
)
1358 fc
->fcoe_pending_queue_active
= 1;
1360 while (fc
->fcoe_pending_queue
.qlen
) {
1361 /* keep qlen > 0 until fcoe_start_io succeeds */
1362 fc
->fcoe_pending_queue
.qlen
++;
1363 skb
= __skb_dequeue(&fc
->fcoe_pending_queue
);
1365 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1366 rc
= fcoe_start_io(skb
);
1367 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1370 __skb_queue_head(&fc
->fcoe_pending_queue
, skb
);
1371 /* undo temporary increment above */
1372 fc
->fcoe_pending_queue
.qlen
--;
1375 /* undo temporary increment above */
1376 fc
->fcoe_pending_queue
.qlen
--;
1379 if (fc
->fcoe_pending_queue
.qlen
< FCOE_LOW_QUEUE_DEPTH
)
1381 fc
->fcoe_pending_queue_active
= 0;
1382 rc
= fc
->fcoe_pending_queue
.qlen
;
1384 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1389 * fcoe_dev_setup() - setup link change notification interface
1391 static void fcoe_dev_setup()
1394 * here setup a interface specific wd time to
1395 * monitor the link state
1397 register_netdevice_notifier(&fcoe_notifier
);
1401 * fcoe_dev_setup() - cleanup link change notification interface
1403 static void fcoe_dev_cleanup(void)
1405 unregister_netdevice_notifier(&fcoe_notifier
);
1409 * fcoe_device_notification() - netdev event notification callback
1410 * @notifier: context of the notification
1411 * @event: type of event
1412 * @ptr: fixed array for output parsed ifname
1414 * This function is called by the ethernet driver in case of link change event
1416 * Returns: 0 for success
1418 static int fcoe_device_notification(struct notifier_block
*notifier
,
1419 ulong event
, void *ptr
)
1421 struct fc_lport
*lp
= NULL
;
1422 struct net_device
*real_dev
= ptr
;
1423 struct fcoe_softc
*fc
;
1424 struct fcoe_dev_stats
*stats
;
1425 u32 link_possible
= 1;
1429 read_lock(&fcoe_hostlist_lock
);
1430 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1431 if (fc
->real_dev
== real_dev
) {
1436 read_unlock(&fcoe_hostlist_lock
);
1444 case NETDEV_GOING_DOWN
:
1450 case NETDEV_CHANGEMTU
:
1451 mfs
= fc
->real_dev
->mtu
-
1452 (sizeof(struct fcoe_hdr
) +
1453 sizeof(struct fcoe_crc_eof
));
1454 if (mfs
>= FC_MIN_MAX_FRAME
)
1455 fc_set_mfs(lp
, mfs
);
1457 case NETDEV_REGISTER
:
1460 FC_DBG("Unknown event %ld from netdev netlink\n", event
);
1462 if (link_possible
&& !fcoe_link_ok(lp
))
1463 fcoe_ctlr_link_up(&fc
->ctlr
);
1464 else if (fcoe_ctlr_link_down(&fc
->ctlr
)) {
1465 stats
= fc_lport_get_stats(lp
);
1466 stats
->LinkFailureCount
++;
1467 fcoe_clean_pending_queue(lp
);
1474 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1475 * @ifname: fixed array for output parsed ifname
1476 * @buffer: incoming buffer to be copied
1478 * Returns: NULL or ptr to netdeive
1480 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
1483 char ifname
[IFNAMSIZ
+ 2];
1486 strlcpy(ifname
, buffer
, IFNAMSIZ
);
1487 cp
= ifname
+ strlen(ifname
);
1488 while (--cp
>= ifname
&& *cp
== '\n')
1490 return dev_get_by_name(&init_net
, ifname
);
1496 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
1497 * @netdev: the target netdev
1499 * Returns: ptr to the struct module, NULL for failure
1501 static struct module
*
1502 fcoe_netdev_to_module_owner(const struct net_device
*netdev
)
1509 dev
= netdev
->dev
.parent
;
1516 return dev
->driver
->owner
;
1520 * fcoe_ethdrv_get() - Hold the Ethernet driver
1521 * @netdev: the target netdev
1523 * Holds the Ethernet driver module by try_module_get() for
1524 * the corresponding netdev.
1526 * Returns: 0 for succsss
1528 static int fcoe_ethdrv_get(const struct net_device
*netdev
)
1530 struct module
*owner
;
1532 owner
= fcoe_netdev_to_module_owner(netdev
);
1534 printk(KERN_DEBUG
"fcoe:hold driver module %s for %s\n",
1535 module_name(owner
), netdev
->name
);
1536 return try_module_get(owner
);
1542 * fcoe_ethdrv_put() - Release the Ethernet driver
1543 * @netdev: the target netdev
1545 * Releases the Ethernet driver module by module_put for
1546 * the corresponding netdev.
1548 * Returns: 0 for succsss
1550 static int fcoe_ethdrv_put(const struct net_device
*netdev
)
1552 struct module
*owner
;
1554 owner
= fcoe_netdev_to_module_owner(netdev
);
1556 printk(KERN_DEBUG
"fcoe:release driver module %s for %s\n",
1557 module_name(owner
), netdev
->name
);
1565 * fcoe_destroy() - handles the destroy from sysfs
1566 * @buffer: expcted to be a eth if name
1567 * @kp: associated kernel param
1569 * Returns: 0 for success
1571 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1574 struct net_device
*netdev
;
1576 netdev
= fcoe_if_to_netdev(buffer
);
1581 /* look for existing lport */
1582 if (!fcoe_hostlist_lookup(netdev
)) {
1586 rc
= fcoe_if_destroy(netdev
);
1588 printk(KERN_ERR
"fcoe: fcoe_if_destroy(%s) failed\n",
1593 fcoe_ethdrv_put(netdev
);
1602 * fcoe_create() - Handles the create call from sysfs
1603 * @buffer: expcted to be a eth if name
1604 * @kp: associated kernel param
1606 * Returns: 0 for success
1608 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1611 struct net_device
*netdev
;
1613 netdev
= fcoe_if_to_netdev(buffer
);
1618 /* look for existing lport */
1619 if (fcoe_hostlist_lookup(netdev
)) {
1623 fcoe_ethdrv_get(netdev
);
1625 rc
= fcoe_if_create(netdev
);
1627 printk(KERN_ERR
"fcoe: fcoe_if_create(%s) failed\n",
1629 fcoe_ethdrv_put(netdev
);
1640 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1641 __MODULE_PARM_TYPE(create
, "string");
1642 MODULE_PARM_DESC(create
, "Create fcoe port using net device passed in.");
1643 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1644 __MODULE_PARM_TYPE(destroy
, "string");
1645 MODULE_PARM_DESC(destroy
, "Destroy fcoe port");
1648 * fcoe_link_ok() - Check if link is ok for the fc_lport
1649 * @lp: ptr to the fc_lport
1651 * Any permanently-disqualifying conditions have been previously checked.
1652 * This also updates the speed setting, which may change with link for 100/1000.
1654 * This function should probably be checking for PAUSE support at some point
1655 * in the future. Currently Per-priority-pause is not determinable using
1656 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1658 * Returns: 0 if link is OK for use by FCoE.
1661 int fcoe_link_ok(struct fc_lport
*lp
)
1663 struct fcoe_softc
*fc
= lport_priv(lp
);
1664 struct net_device
*dev
= fc
->real_dev
;
1665 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1668 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
)) {
1670 if (dev
->ethtool_ops
->get_settings
) {
1671 dev
->ethtool_ops
->get_settings(dev
, &ecmd
);
1672 lp
->link_supported_speeds
&=
1673 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1674 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1675 SUPPORTED_1000baseT_Full
))
1676 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1677 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1678 lp
->link_supported_speeds
|=
1679 FC_PORTSPEED_10GBIT
;
1680 if (ecmd
.speed
== SPEED_1000
)
1681 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1682 if (ecmd
.speed
== SPEED_10000
)
1683 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1690 EXPORT_SYMBOL_GPL(fcoe_link_ok
);
1693 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1696 void fcoe_percpu_clean(struct fc_lport
*lp
)
1698 struct fcoe_percpu_s
*pp
;
1699 struct fcoe_rcv_info
*fr
;
1700 struct sk_buff_head
*list
;
1701 struct sk_buff
*skb
, *next
;
1702 struct sk_buff
*head
;
1705 for_each_possible_cpu(cpu
) {
1706 pp
= &per_cpu(fcoe_percpu
, cpu
);
1707 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1708 list
= &pp
->fcoe_rx_list
;
1710 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1713 fr
= fcoe_dev_from_skb(skb
);
1714 if (fr
->fr_dev
== lp
) {
1715 __skb_unlink(skb
, list
);
1719 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1722 EXPORT_SYMBOL_GPL(fcoe_percpu_clean
);
1725 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1726 * @lp: the corresponding fc_lport
1730 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1732 struct fcoe_softc
*fc
= lport_priv(lp
);
1733 struct sk_buff
*skb
;
1735 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1736 while ((skb
= __skb_dequeue(&fc
->fcoe_pending_queue
)) != NULL
) {
1737 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1739 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1741 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1743 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue
);
1746 * fcoe_reset() - Resets the fcoe
1747 * @shost: shost the reset is from
1751 int fcoe_reset(struct Scsi_Host
*shost
)
1753 struct fc_lport
*lport
= shost_priv(shost
);
1754 fc_lport_reset(lport
);
1757 EXPORT_SYMBOL_GPL(fcoe_reset
);
1760 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1761 * @device: this is currently ptr to net_device
1763 * Returns: NULL or the located fcoe_softc
1765 static struct fcoe_softc
*
1766 fcoe_hostlist_lookup_softc(const struct net_device
*dev
)
1768 struct fcoe_softc
*fc
;
1770 read_lock(&fcoe_hostlist_lock
);
1771 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1772 if (fc
->real_dev
== dev
) {
1773 read_unlock(&fcoe_hostlist_lock
);
1777 read_unlock(&fcoe_hostlist_lock
);
1782 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1783 * @netdev: ptr to net_device
1785 * Returns: 0 for success
1787 struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1789 struct fcoe_softc
*fc
;
1791 fc
= fcoe_hostlist_lookup_softc(netdev
);
1793 return (fc
) ? fc
->ctlr
.lp
: NULL
;
1795 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup
);
1798 * fcoe_hostlist_add() - Add a lport to lports list
1799 * @lp: ptr to the fc_lport to badded
1801 * Returns: 0 for success
1803 int fcoe_hostlist_add(const struct fc_lport
*lp
)
1805 struct fcoe_softc
*fc
;
1807 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1809 fc
= lport_priv(lp
);
1810 write_lock_bh(&fcoe_hostlist_lock
);
1811 list_add_tail(&fc
->list
, &fcoe_hostlist
);
1812 write_unlock_bh(&fcoe_hostlist_lock
);
1816 EXPORT_SYMBOL_GPL(fcoe_hostlist_add
);
1819 * fcoe_hostlist_remove() - remove a lport from lports list
1820 * @lp: ptr to the fc_lport to badded
1822 * Returns: 0 for success
1824 int fcoe_hostlist_remove(const struct fc_lport
*lp
)
1826 struct fcoe_softc
*fc
;
1828 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1830 write_lock_bh(&fcoe_hostlist_lock
);
1831 list_del(&fc
->list
);
1832 write_unlock_bh(&fcoe_hostlist_lock
);
1836 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove
);
1839 * fcoe_init() - fcoe module loading initialization
1841 * Returns 0 on success, negative on failure
1843 static int __init
fcoe_init(void)
1847 struct fcoe_percpu_s
*p
;
1849 INIT_LIST_HEAD(&fcoe_hostlist
);
1850 rwlock_init(&fcoe_hostlist_lock
);
1852 for_each_possible_cpu(cpu
) {
1853 p
= &per_cpu(fcoe_percpu
, cpu
);
1854 skb_queue_head_init(&p
->fcoe_rx_list
);
1857 for_each_online_cpu(cpu
)
1858 fcoe_percpu_thread_create(cpu
);
1860 /* Initialize per CPU interrupt thread */
1861 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1865 /* Setup link change notification */
1868 setup_timer(&fcoe_timer
, fcoe_watchdog
, 0);
1870 mod_timer(&fcoe_timer
, jiffies
+ (10 * HZ
));
1877 for_each_online_cpu(cpu
) {
1878 fcoe_percpu_thread_destroy(cpu
);
1883 module_init(fcoe_init
);
1886 * fcoe_exit() - fcoe module unloading cleanup
1888 * Returns 0 on success, negative on failure
1890 static void __exit
fcoe_exit(void)
1893 struct fcoe_softc
*fc
, *tmp
;
1897 /* Stop the timer */
1898 del_timer_sync(&fcoe_timer
);
1900 /* releases the associated fcoe hosts */
1901 list_for_each_entry_safe(fc
, tmp
, &fcoe_hostlist
, list
)
1902 fcoe_if_destroy(fc
->real_dev
);
1904 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
1906 for_each_online_cpu(cpu
) {
1907 fcoe_percpu_thread_destroy(cpu
);
1910 /* detach from scsi transport */
1913 module_exit(fcoe_exit
);