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_lport_config() - sets up the fc_lport
139 * @lp: ptr to the fc_lport
141 * Returns: 0 for success
143 static int fcoe_lport_config(struct fc_lport
*lp
)
147 lp
->max_retry_count
= 3;
148 lp
->max_rport_retry_count
= 3;
149 lp
->e_d_tov
= 2 * 1000; /* FC-FS default */
150 lp
->r_a_tov
= 2 * 2 * 1000;
151 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
152 FCP_SPPF_RETRY
| FCP_SPPF_CONF_COMPL
);
154 fc_lport_init_stats(lp
);
156 /* lport fc_lport related configuration */
159 /* offload related configuration */
170 * fcoe_queue_timer() - fcoe queue timer
171 * @lp: the fc_lport pointer
173 * Calls fcoe_check_wait_queue on timeout
176 static void fcoe_queue_timer(ulong lp
)
178 fcoe_check_wait_queue((struct fc_lport
*)lp
, NULL
);
182 * fcoe_netdev_config() - Set up netdev for SW FCoE
183 * @lp : ptr to the fc_lport
184 * @netdev : ptr to the associated netdevice struct
186 * Must be called after fcoe_lport_config() as it will use lport mutex
188 * Returns : 0 for success
190 static int fcoe_netdev_config(struct fc_lport
*lp
, struct net_device
*netdev
)
194 struct fcoe_softc
*fc
;
195 u8 flogi_maddr
[ETH_ALEN
];
197 /* Setup lport private data to point to fcoe softc */
200 fc
->real_dev
= netdev
;
201 fc
->phys_dev
= netdev
;
203 /* Require support for get_pauseparam ethtool op. */
204 if (netdev
->priv_flags
& IFF_802_1Q_VLAN
)
205 fc
->phys_dev
= vlan_dev_real_dev(netdev
);
207 /* Do not support for bonding device */
208 if ((fc
->real_dev
->priv_flags
& IFF_MASTER_ALB
) ||
209 (fc
->real_dev
->priv_flags
& IFF_SLAVE_INACTIVE
) ||
210 (fc
->real_dev
->priv_flags
& IFF_MASTER_8023AD
)) {
215 * Determine max frame size based on underlying device and optional
216 * user-configured limit. If the MFS is too low, fcoe_link_ok()
217 * will return 0, so do this first.
219 mfs
= fc
->real_dev
->mtu
- (sizeof(struct fcoe_hdr
) +
220 sizeof(struct fcoe_crc_eof
));
221 if (fc_set_mfs(lp
, mfs
))
224 /* offload features support */
225 if (fc
->real_dev
->features
& NETIF_F_SG
)
228 #ifdef NETIF_F_FCOE_CRC
229 if (netdev
->features
& NETIF_F_FCOE_CRC
) {
231 printk(KERN_DEBUG
"fcoe:%s supports FCCRC offload\n",
236 if (netdev
->features
& NETIF_F_FSO
) {
238 lp
->lso_max
= netdev
->gso_max_size
;
239 printk(KERN_DEBUG
"fcoe:%s supports LSO for max len 0x%x\n",
240 netdev
->name
, lp
->lso_max
);
243 if (netdev
->fcoe_ddp_xid
) {
245 lp
->lro_xid
= netdev
->fcoe_ddp_xid
;
246 printk(KERN_DEBUG
"fcoe:%s supports LRO for max xid 0x%x\n",
247 netdev
->name
, lp
->lro_xid
);
249 skb_queue_head_init(&fc
->fcoe_pending_queue
);
250 fc
->fcoe_pending_queue_active
= 0;
251 setup_timer(&fc
->timer
, fcoe_queue_timer
, (unsigned long)lp
);
253 /* setup Source Mac Address */
254 memcpy(fc
->ctlr
.ctl_src_addr
, fc
->real_dev
->dev_addr
,
255 fc
->real_dev
->addr_len
);
257 wwnn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 1, 0);
258 fc_set_wwnn(lp
, wwnn
);
259 /* XXX - 3rd arg needs to be vlan id */
260 wwpn
= fcoe_wwn_from_mac(fc
->real_dev
->dev_addr
, 2, 0);
261 fc_set_wwpn(lp
, wwpn
);
264 * Add FCoE MAC address as second unicast MAC address
265 * or enter promiscuous mode if not capable of listening
266 * for multiple unicast MACs.
269 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
270 dev_unicast_add(fc
->real_dev
, flogi_maddr
, ETH_ALEN
);
271 dev_mc_add(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
275 * setup the receive function from ethernet driver
276 * on the ethertype for the given device
278 fc
->fcoe_packet_type
.func
= fcoe_rcv
;
279 fc
->fcoe_packet_type
.type
= __constant_htons(ETH_P_FCOE
);
280 fc
->fcoe_packet_type
.dev
= fc
->real_dev
;
281 dev_add_pack(&fc
->fcoe_packet_type
);
287 * fcoe_shost_config() - Sets up fc_lport->host
288 * @lp : ptr to the fc_lport
289 * @shost : ptr to the associated scsi host
290 * @dev : device associated to scsi host
292 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
294 * Returns : 0 for success
296 static int fcoe_shost_config(struct fc_lport
*lp
, struct Scsi_Host
*shost
,
301 /* lport scsi host config */
304 lp
->host
->max_lun
= FCOE_MAX_LUN
;
305 lp
->host
->max_id
= FCOE_MAX_FCP_TARGET
;
306 lp
->host
->max_channel
= 0;
307 lp
->host
->transportt
= scsi_transport_fcoe_sw
;
309 /* add the new host to the SCSI-ml */
310 rc
= scsi_add_host(lp
->host
, dev
);
312 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
315 sprintf(fc_host_symbolic_name(lp
->host
), "%s v%s over %s",
316 FCOE_NAME
, FCOE_VERSION
,
317 fcoe_netdev(lp
)->name
);
323 * fcoe_em_config() - allocates em for this lport
324 * @lp: the port that em is to allocated for
326 * Returns : 0 on success
328 static inline int fcoe_em_config(struct fc_lport
*lp
)
332 lp
->emp
= fc_exch_mgr_alloc(lp
, FC_CLASS_3
,
333 FCOE_MIN_XID
, FCOE_MAX_XID
);
341 * fcoe_if_destroy() - FCoE software HBA tear-down function
342 * @netdev: ptr to the associated net_device
344 * Returns: 0 if link is OK for use by FCoE.
346 static int fcoe_if_destroy(struct net_device
*netdev
)
348 struct fc_lport
*lp
= NULL
;
349 struct fcoe_softc
*fc
;
350 u8 flogi_maddr
[ETH_ALEN
];
354 printk(KERN_DEBUG
"fcoe_if_destroy:interface on %s\n",
357 lp
= fcoe_hostlist_lookup(netdev
);
363 /* Logout of the fabric */
364 fc_fabric_logoff(lp
);
366 /* Remove the instance from fcoe's list */
367 fcoe_hostlist_remove(lp
);
369 /* Don't listen for Ethernet packets anymore */
370 dev_remove_pack(&fc
->fcoe_packet_type
);
371 dev_remove_pack(&fc
->fip_packet_type
);
372 fcoe_ctlr_destroy(&fc
->ctlr
);
374 /* Cleanup the fc_lport */
375 fc_lport_destroy(lp
);
378 /* Detach from the scsi-ml */
379 fc_remove_host(lp
->host
);
380 scsi_remove_host(lp
->host
);
382 /* There are no more rports or I/O, free the EM */
384 fc_exch_mgr_free(lp
->emp
);
386 /* Delete secondary MAC addresses */
388 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
389 dev_unicast_delete(fc
->real_dev
, flogi_maddr
, ETH_ALEN
);
390 if (!is_zero_ether_addr(fc
->ctlr
.data_src_addr
))
391 dev_unicast_delete(fc
->real_dev
,
392 fc
->ctlr
.data_src_addr
, ETH_ALEN
);
393 dev_mc_delete(fc
->real_dev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
396 /* Free the per-CPU receive threads */
397 fcoe_percpu_clean(lp
);
399 /* Free existing skbs */
400 fcoe_clean_pending_queue(lp
);
403 del_timer_sync(&fc
->timer
);
405 /* Free memory used by statistical counters */
406 fc_lport_free_stats(lp
);
408 /* Release the net_device and Scsi_Host */
409 dev_put(fc
->real_dev
);
410 scsi_host_put(lp
->host
);
416 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
417 * @lp: the corresponding fc_lport
418 * @xid: the exchange id for this ddp transfer
419 * @sgl: the scatterlist describing this transfer
420 * @sgc: number of sg items
424 static int fcoe_ddp_setup(struct fc_lport
*lp
, u16 xid
,
425 struct scatterlist
*sgl
, unsigned int sgc
)
427 struct net_device
*n
= fcoe_netdev(lp
);
429 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_setup
)
430 return n
->netdev_ops
->ndo_fcoe_ddp_setup(n
, xid
, sgl
, sgc
);
436 * fcoe_ddp_done - calls LLD's ddp_done through net_device
437 * @lp: the corresponding fc_lport
438 * @xid: the exchange id for this ddp transfer
440 * Returns : the length of data that have been completed by ddp
442 static int fcoe_ddp_done(struct fc_lport
*lp
, u16 xid
)
444 struct net_device
*n
= fcoe_netdev(lp
);
446 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_done
)
447 return n
->netdev_ops
->ndo_fcoe_ddp_done(n
, xid
);
451 static struct libfc_function_template fcoe_libfc_fcn_templ
= {
452 .frame_send
= fcoe_xmit
,
453 .ddp_setup
= fcoe_ddp_setup
,
454 .ddp_done
= fcoe_ddp_done
,
458 * fcoe_fip_recv - handle a received FIP frame.
459 * @skb: the receive skb
460 * @dev: associated &net_device
461 * @ptype: the &packet_type structure which was used to register this handler.
462 * @orig_dev: original receive &net_device, in case @dev is a bond.
464 * Returns: 0 for success
466 static int fcoe_fip_recv(struct sk_buff
*skb
, struct net_device
*dev
,
467 struct packet_type
*ptype
,
468 struct net_device
*orig_dev
)
470 struct fcoe_softc
*fc
;
472 fc
= container_of(ptype
, struct fcoe_softc
, fip_packet_type
);
473 fcoe_ctlr_recv(&fc
->ctlr
, skb
);
478 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
479 * @fip: FCoE controller.
482 static void fcoe_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
484 skb
->dev
= fcoe_from_ctlr(fip
)->real_dev
;
489 * fcoe_update_src_mac() - Update Ethernet MAC filters.
490 * @fip: FCoE controller.
491 * @old: Unicast MAC address to delete if the MAC is non-zero.
492 * @new: Unicast MAC address to add.
494 * Remove any previously-set unicast MAC filter.
495 * Add secondary FCoE MAC address filter for our OUI.
497 static void fcoe_update_src_mac(struct fcoe_ctlr
*fip
, u8
*old
, u8
*new)
499 struct fcoe_softc
*fc
;
501 fc
= fcoe_from_ctlr(fip
);
503 if (!is_zero_ether_addr(old
))
504 dev_unicast_delete(fc
->real_dev
, old
, ETH_ALEN
);
505 dev_unicast_add(fc
->real_dev
, new, ETH_ALEN
);
510 * fcoe_if_create() - this function creates the fcoe interface
511 * @netdev: pointer the associated netdevice
513 * Creates fc_lport struct and scsi_host for lport, configures lport
514 * and starts fabric login.
516 * Returns : 0 on success
518 static int fcoe_if_create(struct net_device
*netdev
)
521 struct fc_lport
*lp
= NULL
;
522 struct fcoe_softc
*fc
;
523 struct Scsi_Host
*shost
;
527 printk(KERN_DEBUG
"fcoe_if_create:interface on %s\n",
530 lp
= fcoe_hostlist_lookup(netdev
);
534 shost
= libfc_host_alloc(&fcoe_shost_template
,
535 sizeof(struct fcoe_softc
));
537 FC_DBG("Could not allocate host structure\n");
540 lp
= shost_priv(shost
);
543 /* configure fc_lport, e.g., em */
544 rc
= fcoe_lport_config(lp
);
546 FC_DBG("Could not configure lport\n");
550 /* configure lport network properties */
551 rc
= fcoe_netdev_config(lp
, netdev
);
553 FC_DBG("Could not configure netdev for lport\n");
560 fcoe_ctlr_init(&fc
->ctlr
);
561 fc
->ctlr
.send
= fcoe_fip_send
;
562 fc
->ctlr
.update_mac
= fcoe_update_src_mac
;
564 fc
->fip_packet_type
.func
= fcoe_fip_recv
;
565 fc
->fip_packet_type
.type
= htons(ETH_P_FIP
);
566 fc
->fip_packet_type
.dev
= fc
->real_dev
;
567 dev_add_pack(&fc
->fip_packet_type
);
569 /* configure lport scsi host properties */
570 rc
= fcoe_shost_config(lp
, shost
, &netdev
->dev
);
572 FC_DBG("Could not configure shost for lport\n");
576 /* lport exch manager allocation */
577 rc
= fcoe_em_config(lp
);
579 FC_DBG("Could not configure em for lport\n");
583 /* Initialize the library */
584 rc
= fcoe_libfc_config(lp
, &fcoe_libfc_fcn_templ
);
586 FC_DBG("Could not configure libfc for lport!\n");
590 /* add to lports list */
591 fcoe_hostlist_add(lp
);
593 lp
->boot_time
= jiffies
;
597 if (!fcoe_link_ok(lp
))
598 fcoe_ctlr_link_up(&fc
->ctlr
);
605 fc_exch_mgr_free(lp
->emp
); /* Free the EM */
607 scsi_host_put(lp
->host
);
612 * fcoe_if_init() - attach to scsi transport
614 * Returns : 0 on success
616 static int __init
fcoe_if_init(void)
618 /* attach to scsi transport */
619 scsi_transport_fcoe_sw
=
620 fc_attach_transport(&fcoe_transport_function
);
622 if (!scsi_transport_fcoe_sw
) {
623 printk(KERN_ERR
"fcoe_init:fc_attach_transport() failed\n");
631 * fcoe_if_exit() - detach from scsi transport
633 * Returns : 0 on success
635 int __exit
fcoe_if_exit(void)
637 fc_release_transport(scsi_transport_fcoe_sw
);
642 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
643 * @cpu: cpu index for the online cpu
645 static void fcoe_percpu_thread_create(unsigned int cpu
)
647 struct fcoe_percpu_s
*p
;
648 struct task_struct
*thread
;
650 p
= &per_cpu(fcoe_percpu
, cpu
);
652 thread
= kthread_create(fcoe_percpu_receive_thread
,
653 (void *)p
, "fcoethread/%d", cpu
);
655 if (likely(!IS_ERR(p
->thread
))) {
656 kthread_bind(thread
, cpu
);
657 wake_up_process(thread
);
659 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
661 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
666 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
667 * @cpu: cpu index the rx thread is to be removed
669 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
670 * current CPU's Rx thread. If the thread being destroyed is bound to
671 * the CPU processing this context the skbs will be freed.
673 static void fcoe_percpu_thread_destroy(unsigned int cpu
)
675 struct fcoe_percpu_s
*p
;
676 struct task_struct
*thread
;
677 struct page
*crc_eof
;
680 struct fcoe_percpu_s
*p0
;
681 unsigned targ_cpu
= smp_processor_id();
682 #endif /* CONFIG_SMP */
684 printk(KERN_DEBUG
"fcoe: Destroying receive thread for CPU %d\n", cpu
);
686 /* Prevent any new skbs from being queued for this CPU. */
687 p
= &per_cpu(fcoe_percpu
, cpu
);
688 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
691 crc_eof
= p
->crc_eof_page
;
692 p
->crc_eof_page
= NULL
;
693 p
->crc_eof_offset
= 0;
694 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
698 * Don't bother moving the skb's if this context is running
699 * on the same CPU that is having its thread destroyed. This
700 * can easily happen when the module is removed.
702 if (cpu
!= targ_cpu
) {
703 p0
= &per_cpu(fcoe_percpu
, targ_cpu
);
704 spin_lock_bh(&p0
->fcoe_rx_list
.lock
);
706 FC_DBG("Moving frames from CPU %d to CPU %d\n",
709 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
710 __skb_queue_tail(&p0
->fcoe_rx_list
, skb
);
711 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
714 * The targeted CPU is not initialized and cannot accept
715 * new skbs. Unlock the targeted CPU and drop the skbs
716 * on the CPU that is going offline.
718 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
720 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
724 * This scenario occurs when the module is being removed
725 * and all threads are being destroyed. skbs will continue
726 * to be shifted from the CPU thread that is being removed
727 * to the CPU thread associated with the CPU that is processing
728 * the module removal. Once there is only one CPU Rx thread it
729 * will reach this case and we will drop all skbs and later
732 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
733 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
735 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
739 * This a non-SMP scenario where the singular Rx thread is
740 * being removed. Free all skbs and stop the thread.
742 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
743 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
745 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
749 kthread_stop(thread
);
756 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
757 * @nfb: callback data block
758 * @action: event triggering the callback
759 * @hcpu: index for the cpu of this event
761 * This creates or destroys per cpu data for fcoe
763 * Returns NOTIFY_OK always.
765 static int fcoe_cpu_callback(struct notifier_block
*nfb
,
766 unsigned long action
, void *hcpu
)
768 unsigned cpu
= (unsigned long)hcpu
;
772 case CPU_ONLINE_FROZEN
:
773 FC_DBG("CPU %x online: Create Rx thread\n", cpu
);
774 fcoe_percpu_thread_create(cpu
);
777 case CPU_DEAD_FROZEN
:
778 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu
);
779 fcoe_percpu_thread_destroy(cpu
);
787 static struct notifier_block fcoe_cpu_notifier
= {
788 .notifier_call
= fcoe_cpu_callback
,
792 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
793 * @skb: the receive skb
794 * @dev: associated net device
796 * @olddev: last device
798 * this function will receive the packet and build fc frame and pass it up
800 * Returns: 0 for success
802 int fcoe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
803 struct packet_type
*ptype
, struct net_device
*olddev
)
806 struct fcoe_rcv_info
*fr
;
807 struct fcoe_softc
*fc
;
808 struct fc_frame_header
*fh
;
809 struct fcoe_percpu_s
*fps
;
811 unsigned int cpu
= 0;
813 fc
= container_of(ptype
, struct fcoe_softc
, fcoe_packet_type
);
815 if (unlikely(lp
== NULL
)) {
816 FC_DBG("cannot find hba structure");
822 if (unlikely(debug_fcoe
)) {
823 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
824 "end:%p sum:%d dev:%s", skb
->len
, skb
->data_len
,
825 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
826 skb_end_pointer(skb
), skb
->csum
,
827 skb
->dev
? skb
->dev
->name
: "<NULL>");
831 /* check for FCOE packet type */
832 if (unlikely(eth_hdr(skb
)->h_proto
!= htons(ETH_P_FCOE
))) {
833 FC_DBG("wrong FC type frame");
838 * Check for minimum frame length, and make sure required FCoE
839 * and FC headers are pulled into the linear data area.
841 if (unlikely((skb
->len
< FCOE_MIN_FRAME
) ||
842 !pskb_may_pull(skb
, FCOE_HEADER_LEN
)))
845 skb_set_transport_header(skb
, sizeof(struct fcoe_hdr
));
846 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
848 oxid
= ntohs(fh
->fh_ox_id
);
850 fr
= fcoe_dev_from_skb(skb
);
856 * The incoming frame exchange id(oxid) is ANDed with num of online
857 * cpu bits to get cpu and then this cpu is used for selecting
858 * a per cpu kernel thread from fcoe_percpu.
860 cpu
= oxid
& (num_online_cpus() - 1);
863 fps
= &per_cpu(fcoe_percpu
, cpu
);
864 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
865 if (unlikely(!fps
->thread
)) {
867 * The targeted CPU is not ready, let's target
868 * the first CPU now. For non-SMP systems this
869 * will check the same CPU twice.
871 FC_DBG("CPU is online, but no receive thread ready "
872 "for incoming skb- using first online CPU.\n");
874 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
875 cpu
= first_cpu(cpu_online_map
);
876 fps
= &per_cpu(fcoe_percpu
, cpu
);
877 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
879 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
885 * We now have a valid CPU that we're targeting for
886 * this skb. We also have this receive thread locked,
887 * so we're free to queue skbs into it's queue.
889 __skb_queue_tail(&fps
->fcoe_rx_list
, skb
);
890 if (fps
->fcoe_rx_list
.qlen
== 1)
891 wake_up_process(fps
->thread
);
893 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
897 fc_lport_get_stats(lp
)->ErrorFrames
++;
905 * fcoe_start_io() - pass to netdev to start xmit for fcoe
906 * @skb: the skb to be xmitted
908 * Returns: 0 for success
910 static inline int fcoe_start_io(struct sk_buff
*skb
)
915 rc
= dev_queue_xmit(skb
);
923 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
924 * @skb: the skb to be xmitted
927 * Returns: 0 for success
929 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
931 struct fcoe_percpu_s
*fps
;
934 fps
= &get_cpu_var(fcoe_percpu
);
935 page
= fps
->crc_eof_page
;
937 page
= alloc_page(GFP_ATOMIC
);
939 put_cpu_var(fcoe_percpu
);
942 fps
->crc_eof_page
= page
;
943 fps
->crc_eof_offset
= 0;
947 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
948 fps
->crc_eof_offset
, tlen
);
950 skb
->data_len
+= tlen
;
951 skb
->truesize
+= tlen
;
952 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
954 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
955 fps
->crc_eof_page
= NULL
;
956 fps
->crc_eof_offset
= 0;
959 put_cpu_var(fcoe_percpu
);
964 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
965 * @fp: the fc_frame containing data to be checksummed
967 * This uses crc32() to calculate the crc for fc frame
968 * Return : 32 bit crc
970 u32
fcoe_fc_crc(struct fc_frame
*fp
)
972 struct sk_buff
*skb
= fp_skb(fp
);
973 struct skb_frag_struct
*frag
;
975 unsigned long off
, len
, clen
;
979 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
981 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
982 frag
= &skb_shinfo(skb
)->frags
[i
];
983 off
= frag
->page_offset
;
986 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
987 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
988 KM_SKB_DATA_SOFTIRQ
);
989 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
990 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
999 * fcoe_xmit() - FCoE frame transmit function
1000 * @lp: the associated local port
1001 * @fp: the fc_frame to be transmitted
1003 * Return : 0 for success
1005 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
1010 struct fcoe_crc_eof
*cp
;
1011 struct sk_buff
*skb
;
1012 struct fcoe_dev_stats
*stats
;
1013 struct fc_frame_header
*fh
;
1014 unsigned int hlen
; /* header length implies the version */
1015 unsigned int tlen
; /* trailer length */
1016 unsigned int elen
; /* eth header, may include vlan */
1017 struct fcoe_softc
*fc
;
1019 struct fcoe_hdr
*hp
;
1021 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
1023 fc
= lport_priv(lp
);
1024 fh
= fc_frame_header_get(fp
);
1026 wlen
= skb
->len
/ FCOE_WORD_TO_BYTE
;
1033 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
) &&
1034 fcoe_ctlr_els_send(&fc
->ctlr
, skb
))
1040 elen
= sizeof(struct ethhdr
);
1041 hlen
= sizeof(struct fcoe_hdr
);
1042 tlen
= sizeof(struct fcoe_crc_eof
);
1043 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
1046 if (likely(lp
->crc_offload
)) {
1047 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1048 skb
->csum_start
= skb_headroom(skb
);
1049 skb
->csum_offset
= skb
->len
;
1052 skb
->ip_summed
= CHECKSUM_NONE
;
1053 crc
= fcoe_fc_crc(fp
);
1056 /* copy fc crc and eof to the skb buff */
1057 if (skb_is_nonlinear(skb
)) {
1059 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
1063 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
1064 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
1065 + frag
->page_offset
;
1067 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
1070 memset(cp
, 0, sizeof(*cp
));
1072 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
1074 if (skb_is_nonlinear(skb
)) {
1075 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
1079 /* adjust skb network/transport offsets to match mac/fcoe/fc */
1080 skb_push(skb
, elen
+ hlen
);
1081 skb_reset_mac_header(skb
);
1082 skb_reset_network_header(skb
);
1083 skb
->mac_len
= elen
;
1084 skb
->protocol
= htons(ETH_P_FCOE
);
1085 skb
->dev
= fc
->real_dev
;
1087 /* fill up mac and fcoe headers */
1089 eh
->h_proto
= htons(ETH_P_FCOE
);
1090 if (fc
->ctlr
.map_dest
)
1091 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
1093 /* insert GW address */
1094 memcpy(eh
->h_dest
, fc
->ctlr
.dest_addr
, ETH_ALEN
);
1096 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
))
1097 memcpy(eh
->h_source
, fc
->ctlr
.ctl_src_addr
, ETH_ALEN
);
1099 memcpy(eh
->h_source
, fc
->ctlr
.data_src_addr
, ETH_ALEN
);
1101 hp
= (struct fcoe_hdr
*)(eh
+ 1);
1102 memset(hp
, 0, sizeof(*hp
));
1104 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
1108 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1109 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
1110 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
1111 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
1113 skb_shinfo(skb
)->gso_type
= 0;
1114 skb_shinfo(skb
)->gso_size
= 0;
1117 /* update tx stats: regardless if LLD fails */
1118 stats
= fc_lport_get_stats(lp
);
1120 stats
->TxWords
+= wlen
;
1122 /* send down to lld */
1124 if (fc
->fcoe_pending_queue
.qlen
)
1125 fcoe_check_wait_queue(lp
, skb
);
1126 else if (fcoe_start_io(skb
))
1127 fcoe_check_wait_queue(lp
, skb
);
1133 * fcoe_percpu_receive_thread() - recv thread per cpu
1134 * @arg: ptr to the fcoe per cpu struct
1136 * Return: 0 for success
1138 int fcoe_percpu_receive_thread(void *arg
)
1140 struct fcoe_percpu_s
*p
= arg
;
1142 struct fc_lport
*lp
;
1143 struct fcoe_rcv_info
*fr
;
1144 struct fcoe_dev_stats
*stats
;
1145 struct fc_frame_header
*fh
;
1146 struct sk_buff
*skb
;
1147 struct fcoe_crc_eof crc_eof
;
1148 struct fc_frame
*fp
;
1150 struct fcoe_softc
*fc
;
1151 struct fcoe_hdr
*hp
;
1153 set_user_nice(current
, -20);
1155 while (!kthread_should_stop()) {
1157 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1158 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
1159 set_current_state(TASK_INTERRUPTIBLE
);
1160 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1162 set_current_state(TASK_RUNNING
);
1163 if (kthread_should_stop())
1165 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1167 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1168 fr
= fcoe_dev_from_skb(skb
);
1170 if (unlikely(lp
== NULL
)) {
1171 FC_DBG("invalid HBA Structure");
1176 if (unlikely(debug_fcoe
)) {
1177 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1178 "tail:%p end:%p sum:%d dev:%s",
1179 skb
->len
, skb
->data_len
,
1180 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
1181 skb_end_pointer(skb
), skb
->csum
,
1182 skb
->dev
? skb
->dev
->name
: "<NULL>");
1186 * Save source MAC address before discarding header.
1188 fc
= lport_priv(lp
);
1189 if (skb_is_nonlinear(skb
))
1190 skb_linearize(skb
); /* not ideal */
1191 mac
= eth_hdr(skb
)->h_source
;
1194 * Frame length checks and setting up the header pointers
1195 * was done in fcoe_rcv already.
1197 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
1198 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1200 stats
= fc_lport_get_stats(lp
);
1201 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
1202 if (stats
->ErrorFrames
< 5)
1203 printk(KERN_WARNING
"FCoE version "
1204 "mismatch: The frame has "
1205 "version %x, but the "
1206 "initiator supports version "
1207 "%x\n", FC_FCOE_DECAPS_VER(hp
),
1209 stats
->ErrorFrames
++;
1214 skb_pull(skb
, sizeof(struct fcoe_hdr
));
1215 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
1218 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
1220 fp
= (struct fc_frame
*)skb
;
1223 fr_sof(fp
) = hp
->fcoe_sof
;
1225 /* Copy out the CRC and EOF trailer for access */
1226 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
1230 fr_eof(fp
) = crc_eof
.fcoe_eof
;
1231 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
1232 if (pskb_trim(skb
, fr_len
)) {
1238 * We only check CRC if no offload is available and if it is
1239 * it's solicited data, in which case, the FCP layer would
1240 * check it during the copy.
1242 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
1243 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1245 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
1247 fh
= fc_frame_header_get(fp
);
1248 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
1249 fh
->fh_type
== FC_TYPE_FCP
) {
1250 fc_exch_recv(lp
, lp
->emp
, fp
);
1253 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
1254 if (le32_to_cpu(fr_crc(fp
)) !=
1255 ~crc32(~0, skb
->data
, fr_len
)) {
1256 if (debug_fcoe
|| stats
->InvalidCRCCount
< 5)
1257 printk(KERN_WARNING
"fcoe: dropping "
1258 "frame with CRC error\n");
1259 stats
->InvalidCRCCount
++;
1260 stats
->ErrorFrames
++;
1264 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1266 if (unlikely(fc
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
) &&
1267 fcoe_ctlr_recv_flogi(&fc
->ctlr
, fp
, mac
)) {
1271 fc_exch_recv(lp
, lp
->emp
, fp
);
1277 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1280 * This empties the wait_queue, dequeue the head of the wait_queue queue
1281 * and calls fcoe_start_io() for each packet, if all skb have been
1282 * transmitted, return qlen or -1 if a error occurs, then restore
1283 * wait_queue and try again later.
1285 * The wait_queue is used when the skb transmit fails. skb will go
1286 * in the wait_queue which will be emptied by the timer function or
1287 * by the next skb transmit.
1289 static void fcoe_check_wait_queue(struct fc_lport
*lp
, struct sk_buff
*skb
)
1291 struct fcoe_softc
*fc
= lport_priv(lp
);
1294 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1297 __skb_queue_tail(&fc
->fcoe_pending_queue
, skb
);
1299 if (fc
->fcoe_pending_queue_active
)
1301 fc
->fcoe_pending_queue_active
= 1;
1303 while (fc
->fcoe_pending_queue
.qlen
) {
1304 /* keep qlen > 0 until fcoe_start_io succeeds */
1305 fc
->fcoe_pending_queue
.qlen
++;
1306 skb
= __skb_dequeue(&fc
->fcoe_pending_queue
);
1308 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1309 rc
= fcoe_start_io(skb
);
1310 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1313 __skb_queue_head(&fc
->fcoe_pending_queue
, skb
);
1314 /* undo temporary increment above */
1315 fc
->fcoe_pending_queue
.qlen
--;
1318 /* undo temporary increment above */
1319 fc
->fcoe_pending_queue
.qlen
--;
1322 if (fc
->fcoe_pending_queue
.qlen
< FCOE_LOW_QUEUE_DEPTH
)
1324 if (fc
->fcoe_pending_queue
.qlen
&& !timer_pending(&fc
->timer
))
1325 mod_timer(&fc
->timer
, jiffies
+ 2);
1326 fc
->fcoe_pending_queue_active
= 0;
1328 if (fc
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
1330 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1335 * fcoe_dev_setup() - setup link change notification interface
1337 static void fcoe_dev_setup(void)
1339 register_netdevice_notifier(&fcoe_notifier
);
1343 * fcoe_dev_cleanup() - cleanup link change notification interface
1345 static void fcoe_dev_cleanup(void)
1347 unregister_netdevice_notifier(&fcoe_notifier
);
1351 * fcoe_device_notification() - netdev event notification callback
1352 * @notifier: context of the notification
1353 * @event: type of event
1354 * @ptr: fixed array for output parsed ifname
1356 * This function is called by the ethernet driver in case of link change event
1358 * Returns: 0 for success
1360 static int fcoe_device_notification(struct notifier_block
*notifier
,
1361 ulong event
, void *ptr
)
1363 struct fc_lport
*lp
= NULL
;
1364 struct net_device
*real_dev
= ptr
;
1365 struct fcoe_softc
*fc
;
1366 struct fcoe_dev_stats
*stats
;
1367 u32 link_possible
= 1;
1371 read_lock(&fcoe_hostlist_lock
);
1372 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1373 if (fc
->real_dev
== real_dev
) {
1378 read_unlock(&fcoe_hostlist_lock
);
1386 case NETDEV_GOING_DOWN
:
1392 case NETDEV_CHANGEMTU
:
1393 mfs
= fc
->real_dev
->mtu
-
1394 (sizeof(struct fcoe_hdr
) +
1395 sizeof(struct fcoe_crc_eof
));
1396 if (mfs
>= FC_MIN_MAX_FRAME
)
1397 fc_set_mfs(lp
, mfs
);
1399 case NETDEV_REGISTER
:
1402 FC_DBG("Unknown event %ld from netdev netlink\n", event
);
1404 if (link_possible
&& !fcoe_link_ok(lp
))
1405 fcoe_ctlr_link_up(&fc
->ctlr
);
1406 else if (fcoe_ctlr_link_down(&fc
->ctlr
)) {
1407 stats
= fc_lport_get_stats(lp
);
1408 stats
->LinkFailureCount
++;
1409 fcoe_clean_pending_queue(lp
);
1416 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1417 * @buffer: incoming buffer to be copied
1419 * Returns: NULL or ptr to net_device
1421 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
1424 char ifname
[IFNAMSIZ
+ 2];
1427 strlcpy(ifname
, buffer
, IFNAMSIZ
);
1428 cp
= ifname
+ strlen(ifname
);
1429 while (--cp
>= ifname
&& *cp
== '\n')
1431 return dev_get_by_name(&init_net
, ifname
);
1437 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1438 * @netdev: the target netdev
1440 * Returns: ptr to the struct module, NULL for failure
1442 static struct module
*
1443 fcoe_netdev_to_module_owner(const struct net_device
*netdev
)
1450 dev
= netdev
->dev
.parent
;
1457 return dev
->driver
->owner
;
1461 * fcoe_ethdrv_get() - Hold the Ethernet driver
1462 * @netdev: the target netdev
1464 * Holds the Ethernet driver module by try_module_get() for
1465 * the corresponding netdev.
1467 * Returns: 0 for success
1469 static int fcoe_ethdrv_get(const struct net_device
*netdev
)
1471 struct module
*owner
;
1473 owner
= fcoe_netdev_to_module_owner(netdev
);
1475 printk(KERN_DEBUG
"fcoe:hold driver module %s for %s\n",
1476 module_name(owner
), netdev
->name
);
1477 return try_module_get(owner
);
1483 * fcoe_ethdrv_put() - Release the Ethernet driver
1484 * @netdev: the target netdev
1486 * Releases the Ethernet driver module by module_put for
1487 * the corresponding netdev.
1489 * Returns: 0 for success
1491 static int fcoe_ethdrv_put(const struct net_device
*netdev
)
1493 struct module
*owner
;
1495 owner
= fcoe_netdev_to_module_owner(netdev
);
1497 printk(KERN_DEBUG
"fcoe:release driver module %s for %s\n",
1498 module_name(owner
), netdev
->name
);
1506 * fcoe_destroy() - handles the destroy from sysfs
1507 * @buffer: expected to be an eth if name
1508 * @kp: associated kernel param
1510 * Returns: 0 for success
1512 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1515 struct net_device
*netdev
;
1517 netdev
= fcoe_if_to_netdev(buffer
);
1522 /* look for existing lport */
1523 if (!fcoe_hostlist_lookup(netdev
)) {
1527 rc
= fcoe_if_destroy(netdev
);
1529 printk(KERN_ERR
"fcoe: fcoe_if_destroy(%s) failed\n",
1534 fcoe_ethdrv_put(netdev
);
1543 * fcoe_create() - Handles the create call from sysfs
1544 * @buffer: expected to be an eth if name
1545 * @kp: associated kernel param
1547 * Returns: 0 for success
1549 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1552 struct net_device
*netdev
;
1554 netdev
= fcoe_if_to_netdev(buffer
);
1559 /* look for existing lport */
1560 if (fcoe_hostlist_lookup(netdev
)) {
1564 fcoe_ethdrv_get(netdev
);
1566 rc
= fcoe_if_create(netdev
);
1568 printk(KERN_ERR
"fcoe: fcoe_if_create(%s) failed\n",
1570 fcoe_ethdrv_put(netdev
);
1581 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1582 __MODULE_PARM_TYPE(create
, "string");
1583 MODULE_PARM_DESC(create
, "Create fcoe port using net device passed in.");
1584 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1585 __MODULE_PARM_TYPE(destroy
, "string");
1586 MODULE_PARM_DESC(destroy
, "Destroy fcoe port");
1589 * fcoe_link_ok() - Check if link is ok for the fc_lport
1590 * @lp: ptr to the fc_lport
1592 * Any permanently-disqualifying conditions have been previously checked.
1593 * This also updates the speed setting, which may change with link for 100/1000.
1595 * This function should probably be checking for PAUSE support at some point
1596 * in the future. Currently Per-priority-pause is not determinable using
1597 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1599 * Returns: 0 if link is OK for use by FCoE.
1602 int fcoe_link_ok(struct fc_lport
*lp
)
1604 struct fcoe_softc
*fc
= lport_priv(lp
);
1605 struct net_device
*dev
= fc
->real_dev
;
1606 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1609 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
)) {
1611 if (dev
->ethtool_ops
->get_settings
) {
1612 dev
->ethtool_ops
->get_settings(dev
, &ecmd
);
1613 lp
->link_supported_speeds
&=
1614 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1615 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1616 SUPPORTED_1000baseT_Full
))
1617 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1618 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1619 lp
->link_supported_speeds
|=
1620 FC_PORTSPEED_10GBIT
;
1621 if (ecmd
.speed
== SPEED_1000
)
1622 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1623 if (ecmd
.speed
== SPEED_10000
)
1624 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1633 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1636 void fcoe_percpu_clean(struct fc_lport
*lp
)
1638 struct fcoe_percpu_s
*pp
;
1639 struct fcoe_rcv_info
*fr
;
1640 struct sk_buff_head
*list
;
1641 struct sk_buff
*skb
, *next
;
1642 struct sk_buff
*head
;
1645 for_each_possible_cpu(cpu
) {
1646 pp
= &per_cpu(fcoe_percpu
, cpu
);
1647 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1648 list
= &pp
->fcoe_rx_list
;
1650 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1653 fr
= fcoe_dev_from_skb(skb
);
1654 if (fr
->fr_dev
== lp
) {
1655 __skb_unlink(skb
, list
);
1659 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1664 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1665 * @lp: the corresponding fc_lport
1669 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1671 struct fcoe_softc
*fc
= lport_priv(lp
);
1672 struct sk_buff
*skb
;
1674 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1675 while ((skb
= __skb_dequeue(&fc
->fcoe_pending_queue
)) != NULL
) {
1676 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1678 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1680 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1684 * fcoe_reset() - Resets the fcoe
1685 * @shost: shost the reset is from
1689 int fcoe_reset(struct Scsi_Host
*shost
)
1691 struct fc_lport
*lport
= shost_priv(shost
);
1692 fc_lport_reset(lport
);
1697 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1698 * @dev: this is currently ptr to net_device
1700 * Returns: NULL or the located fcoe_softc
1702 static struct fcoe_softc
*
1703 fcoe_hostlist_lookup_softc(const struct net_device
*dev
)
1705 struct fcoe_softc
*fc
;
1707 read_lock(&fcoe_hostlist_lock
);
1708 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1709 if (fc
->real_dev
== dev
) {
1710 read_unlock(&fcoe_hostlist_lock
);
1714 read_unlock(&fcoe_hostlist_lock
);
1719 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1720 * @netdev: ptr to net_device
1722 * Returns: 0 for success
1724 struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1726 struct fcoe_softc
*fc
;
1728 fc
= fcoe_hostlist_lookup_softc(netdev
);
1730 return (fc
) ? fc
->ctlr
.lp
: NULL
;
1734 * fcoe_hostlist_add() - Add a lport to lports list
1735 * @lp: ptr to the fc_lport to be added
1737 * Returns: 0 for success
1739 int fcoe_hostlist_add(const struct fc_lport
*lp
)
1741 struct fcoe_softc
*fc
;
1743 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1745 fc
= lport_priv(lp
);
1746 write_lock_bh(&fcoe_hostlist_lock
);
1747 list_add_tail(&fc
->list
, &fcoe_hostlist
);
1748 write_unlock_bh(&fcoe_hostlist_lock
);
1754 * fcoe_hostlist_remove() - remove a lport from lports list
1755 * @lp: ptr to the fc_lport to be removed
1757 * Returns: 0 for success
1759 int fcoe_hostlist_remove(const struct fc_lport
*lp
)
1761 struct fcoe_softc
*fc
;
1763 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1765 write_lock_bh(&fcoe_hostlist_lock
);
1766 list_del(&fc
->list
);
1767 write_unlock_bh(&fcoe_hostlist_lock
);
1773 * fcoe_init() - fcoe module loading initialization
1775 * Returns 0 on success, negative on failure
1777 static int __init
fcoe_init(void)
1781 struct fcoe_percpu_s
*p
;
1783 INIT_LIST_HEAD(&fcoe_hostlist
);
1784 rwlock_init(&fcoe_hostlist_lock
);
1786 for_each_possible_cpu(cpu
) {
1787 p
= &per_cpu(fcoe_percpu
, cpu
);
1788 skb_queue_head_init(&p
->fcoe_rx_list
);
1791 for_each_online_cpu(cpu
)
1792 fcoe_percpu_thread_create(cpu
);
1794 /* Initialize per CPU interrupt thread */
1795 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1799 /* Setup link change notification */
1807 for_each_online_cpu(cpu
) {
1808 fcoe_percpu_thread_destroy(cpu
);
1813 module_init(fcoe_init
);
1816 * fcoe_exit() - fcoe module unloading cleanup
1818 * Returns 0 on success, negative on failure
1820 static void __exit
fcoe_exit(void)
1823 struct fcoe_softc
*fc
, *tmp
;
1827 /* releases the associated fcoe hosts */
1828 list_for_each_entry_safe(fc
, tmp
, &fcoe_hostlist
, list
)
1829 fcoe_if_destroy(fc
->real_dev
);
1831 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
1833 for_each_online_cpu(cpu
) {
1834 fcoe_percpu_thread_destroy(cpu
);
1837 /* detach from scsi transport */
1840 module_exit(fcoe_exit
);