2 * Copyright(c) 2007 - 2009 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 MODULE_AUTHOR("Open-FCoE.org");
49 MODULE_DESCRIPTION("FCoE");
50 MODULE_LICENSE("GPL v2");
52 /* Performance tuning parameters for fcoe */
53 static unsigned int fcoe_ddp_min
;
54 module_param_named(ddp_min
, fcoe_ddp_min
, uint
, S_IRUGO
| S_IWUSR
);
55 MODULE_PARM_DESC(ddp_min
, "Minimum I/O size in bytes for " \
56 "Direct Data Placement (DDP).");
59 LIST_HEAD(fcoe_hostlist
);
60 DEFINE_RWLOCK(fcoe_hostlist_lock
);
61 DEFINE_PER_CPU(struct fcoe_percpu_s
, fcoe_percpu
);
63 /* Function Prototypes */
64 static int fcoe_reset(struct Scsi_Host
*shost
);
65 static int fcoe_xmit(struct fc_lport
*, struct fc_frame
*);
66 static int fcoe_rcv(struct sk_buff
*, struct net_device
*,
67 struct packet_type
*, struct net_device
*);
68 static int fcoe_percpu_receive_thread(void *arg
);
69 static void fcoe_clean_pending_queue(struct fc_lport
*lp
);
70 static void fcoe_percpu_clean(struct fc_lport
*lp
);
71 static int fcoe_link_ok(struct fc_lport
*lp
);
73 static struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*);
74 static int fcoe_hostlist_add(const struct fc_lport
*);
75 static int fcoe_hostlist_remove(const struct fc_lport
*);
77 static void fcoe_check_wait_queue(struct fc_lport
*, struct sk_buff
*);
78 static int fcoe_device_notification(struct notifier_block
*, ulong
, void *);
79 static void fcoe_dev_setup(void);
80 static void fcoe_dev_cleanup(void);
82 /* notification function from net device */
83 static struct notifier_block fcoe_notifier
= {
84 .notifier_call
= fcoe_device_notification
,
87 static struct scsi_transport_template
*scsi_transport_fcoe_sw
;
89 struct fc_function_template fcoe_transport_function
= {
90 .show_host_node_name
= 1,
91 .show_host_port_name
= 1,
92 .show_host_supported_classes
= 1,
93 .show_host_supported_fc4s
= 1,
94 .show_host_active_fc4s
= 1,
95 .show_host_maxframe_size
= 1,
97 .show_host_port_id
= 1,
98 .show_host_supported_speeds
= 1,
99 .get_host_speed
= fc_get_host_speed
,
100 .show_host_speed
= 1,
101 .show_host_port_type
= 1,
102 .get_host_port_state
= fc_get_host_port_state
,
103 .show_host_port_state
= 1,
104 .show_host_symbolic_name
= 1,
106 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
107 .show_rport_maxframe_size
= 1,
108 .show_rport_supported_classes
= 1,
110 .show_host_fabric_name
= 1,
111 .show_starget_node_name
= 1,
112 .show_starget_port_name
= 1,
113 .show_starget_port_id
= 1,
114 .set_rport_dev_loss_tmo
= fc_set_rport_loss_tmo
,
115 .show_rport_dev_loss_tmo
= 1,
116 .get_fc_host_stats
= fc_get_host_stats
,
117 .issue_fc_host_lip
= fcoe_reset
,
119 .terminate_rport_io
= fc_rport_terminate_io
,
122 static struct scsi_host_template fcoe_shost_template
= {
123 .module
= THIS_MODULE
,
124 .name
= "FCoE Driver",
125 .proc_name
= FCOE_NAME
,
126 .queuecommand
= fc_queuecommand
,
127 .eh_abort_handler
= fc_eh_abort
,
128 .eh_device_reset_handler
= fc_eh_device_reset
,
129 .eh_host_reset_handler
= fc_eh_host_reset
,
130 .slave_alloc
= fc_slave_alloc
,
131 .change_queue_depth
= fc_change_queue_depth
,
132 .change_queue_type
= fc_change_queue_type
,
135 .can_queue
= FCOE_MAX_OUTSTANDING_COMMANDS
,
136 .use_clustering
= ENABLE_CLUSTERING
,
137 .sg_tablesize
= SG_ALL
,
138 .max_sectors
= 0xffff,
142 * fcoe_interface_create()
143 * @netdev: network interface
145 * Returns: pointer to a struct fcoe_interface or NULL on error
147 static struct fcoe_interface
*fcoe_interface_create(struct net_device
*netdev
)
149 struct fcoe_interface
*fcoe
;
151 fcoe
= kzalloc(sizeof(*fcoe
), GFP_KERNEL
);
153 FCOE_NETDEV_DBG(netdev
, "Could not allocate fcoe structure\n");
157 kref_init(&fcoe
->kref
);
158 fcoe
->netdev
= netdev
;
164 * fcoe_interface_release() - fcoe_port kref release function
165 * @kref: embedded reference count in an fcoe_interface struct
167 static void fcoe_interface_release(struct kref
*kref
)
169 struct fcoe_interface
*fcoe
;
171 fcoe
= container_of(kref
, struct fcoe_interface
, kref
);
176 * fcoe_interface_get()
179 static inline void fcoe_interface_get(struct fcoe_interface
*fcoe
)
181 kref_get(&fcoe
->kref
);
185 * fcoe_interface_put()
188 static inline void fcoe_interface_put(struct fcoe_interface
*fcoe
)
190 kref_put(&fcoe
->kref
, fcoe_interface_release
);
194 * fcoe_fip_recv - handle a received FIP frame.
195 * @skb: the receive skb
196 * @dev: associated &net_device
197 * @ptype: the &packet_type structure which was used to register this handler.
198 * @orig_dev: original receive &net_device, in case @dev is a bond.
200 * Returns: 0 for success
202 static int fcoe_fip_recv(struct sk_buff
*skb
, struct net_device
*dev
,
203 struct packet_type
*ptype
,
204 struct net_device
*orig_dev
)
206 struct fcoe_interface
*fcoe
;
208 fcoe
= container_of(ptype
, struct fcoe_interface
, fip_packet_type
);
209 fcoe_ctlr_recv(&fcoe
->ctlr
, skb
);
214 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
215 * @fip: FCoE controller.
218 static void fcoe_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
220 skb
->dev
= fcoe_from_ctlr(fip
)->netdev
;
225 * fcoe_update_src_mac() - Update Ethernet MAC filters.
226 * @fip: FCoE controller.
227 * @old: Unicast MAC address to delete if the MAC is non-zero.
228 * @new: Unicast MAC address to add.
230 * Remove any previously-set unicast MAC filter.
231 * Add secondary FCoE MAC address filter for our OUI.
233 static void fcoe_update_src_mac(struct fcoe_ctlr
*fip
, u8
*old
, u8
*new)
235 struct fcoe_interface
*fcoe
;
237 fcoe
= fcoe_from_ctlr(fip
);
239 if (!is_zero_ether_addr(old
))
240 dev_unicast_delete(fcoe
->netdev
, old
);
241 dev_unicast_add(fcoe
->netdev
, new);
246 * fcoe_lport_config() - sets up the fc_lport
247 * @lp: ptr to the fc_lport
249 * Returns: 0 for success
251 static int fcoe_lport_config(struct fc_lport
*lp
)
255 lp
->max_retry_count
= 3;
256 lp
->max_rport_retry_count
= 3;
257 lp
->e_d_tov
= 2 * 1000; /* FC-FS default */
258 lp
->r_a_tov
= 2 * 2 * 1000;
259 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
260 FCP_SPPF_RETRY
| FCP_SPPF_CONF_COMPL
);
262 fc_lport_init_stats(lp
);
264 /* lport fc_lport related configuration */
267 /* offload related configuration */
278 * fcoe_netdev_cleanup() - clean up netdev configurations
279 * @port: ptr to the fcoe_port
281 void fcoe_netdev_cleanup(struct fcoe_port
*port
)
283 u8 flogi_maddr
[ETH_ALEN
];
284 struct fcoe_interface
*fcoe
= port
->fcoe
;
286 /* Don't listen for Ethernet packets anymore */
287 dev_remove_pack(&fcoe
->fcoe_packet_type
);
288 dev_remove_pack(&fcoe
->fip_packet_type
);
290 /* Delete secondary MAC addresses */
292 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
293 dev_unicast_delete(fcoe
->netdev
, flogi_maddr
);
294 if (!is_zero_ether_addr(fcoe
->ctlr
.data_src_addr
))
295 dev_unicast_delete(fcoe
->netdev
, fcoe
->ctlr
.data_src_addr
);
297 dev_unicast_delete(fcoe
->netdev
, fcoe
->ctlr
.ctl_src_addr
);
298 dev_mc_delete(fcoe
->netdev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
303 * fcoe_queue_timer() - fcoe queue timer
304 * @lp: the fc_lport pointer
306 * Calls fcoe_check_wait_queue on timeout
309 static void fcoe_queue_timer(ulong lp
)
311 fcoe_check_wait_queue((struct fc_lport
*)lp
, NULL
);
315 * fcoe_netdev_config() - Set up netdev for SW FCoE
316 * @lp : ptr to the fc_lport
317 * @netdev : ptr to the associated netdevice struct
319 * Must be called after fcoe_lport_config() as it will use lport mutex
321 * Returns : 0 for success
323 static int fcoe_netdev_config(struct fc_lport
*lp
, struct net_device
*netdev
)
327 struct fcoe_interface
*fcoe
;
328 struct fcoe_port
*port
;
329 u8 flogi_maddr
[ETH_ALEN
];
330 struct netdev_hw_addr
*ha
;
332 /* Setup lport private data to point to fcoe softc */
333 port
= lport_priv(lp
);
336 fcoe
->netdev
= netdev
;
338 /* Do not support for bonding device */
339 if ((netdev
->priv_flags
& IFF_MASTER_ALB
) ||
340 (netdev
->priv_flags
& IFF_SLAVE_INACTIVE
) ||
341 (netdev
->priv_flags
& IFF_MASTER_8023AD
)) {
346 * Determine max frame size based on underlying device and optional
347 * user-configured limit. If the MFS is too low, fcoe_link_ok()
348 * will return 0, so do this first.
350 mfs
= netdev
->mtu
- (sizeof(struct fcoe_hdr
) +
351 sizeof(struct fcoe_crc_eof
));
352 if (fc_set_mfs(lp
, mfs
))
355 /* offload features support */
356 if (netdev
->features
& NETIF_F_SG
)
359 if (netdev
->features
& NETIF_F_FCOE_CRC
) {
361 FCOE_NETDEV_DBG(netdev
, "Supports FCCRC offload\n");
363 if (netdev
->features
& NETIF_F_FSO
) {
365 lp
->lso_max
= netdev
->gso_max_size
;
366 FCOE_NETDEV_DBG(netdev
, "Supports LSO for max len 0x%x\n",
369 if (netdev
->fcoe_ddp_xid
) {
371 lp
->lro_xid
= netdev
->fcoe_ddp_xid
;
372 FCOE_NETDEV_DBG(netdev
, "Supports LRO for max xid 0x%x\n",
375 skb_queue_head_init(&port
->fcoe_pending_queue
);
376 port
->fcoe_pending_queue_active
= 0;
377 setup_timer(&port
->timer
, fcoe_queue_timer
, (unsigned long)lp
);
379 /* look for SAN MAC address, if multiple SAN MACs exist, only
380 * use the first one for SPMA */
382 for_each_dev_addr(netdev
, ha
) {
383 if ((ha
->type
== NETDEV_HW_ADDR_T_SAN
) &&
384 (is_valid_ether_addr(fcoe
->ctlr
.ctl_src_addr
))) {
385 memcpy(fcoe
->ctlr
.ctl_src_addr
, ha
->addr
, ETH_ALEN
);
392 /* setup Source Mac Address */
393 if (!fcoe
->ctlr
.spma
)
394 memcpy(fcoe
->ctlr
.ctl_src_addr
, netdev
->dev_addr
,
397 wwnn
= fcoe_wwn_from_mac(netdev
->dev_addr
, 1, 0);
398 fc_set_wwnn(lp
, wwnn
);
399 /* XXX - 3rd arg needs to be vlan id */
400 wwpn
= fcoe_wwn_from_mac(netdev
->dev_addr
, 2, 0);
401 fc_set_wwpn(lp
, wwpn
);
404 * Add FCoE MAC address as second unicast MAC address
405 * or enter promiscuous mode if not capable of listening
406 * for multiple unicast MACs.
409 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
410 dev_unicast_add(netdev
, flogi_maddr
);
412 dev_unicast_add(netdev
, fcoe
->ctlr
.ctl_src_addr
);
413 dev_mc_add(netdev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
417 * setup the receive function from ethernet driver
418 * on the ethertype for the given device
420 fcoe
->fcoe_packet_type
.func
= fcoe_rcv
;
421 fcoe
->fcoe_packet_type
.type
= __constant_htons(ETH_P_FCOE
);
422 fcoe
->fcoe_packet_type
.dev
= netdev
;
423 dev_add_pack(&fcoe
->fcoe_packet_type
);
425 fcoe
->fip_packet_type
.func
= fcoe_fip_recv
;
426 fcoe
->fip_packet_type
.type
= htons(ETH_P_FIP
);
427 fcoe
->fip_packet_type
.dev
= netdev
;
428 dev_add_pack(&fcoe
->fip_packet_type
);
434 * fcoe_shost_config() - Sets up fc_lport->host
435 * @lp : ptr to the fc_lport
436 * @shost : ptr to the associated scsi host
437 * @dev : device associated to scsi host
439 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
441 * Returns : 0 for success
443 static int fcoe_shost_config(struct fc_lport
*lp
, struct Scsi_Host
*shost
,
448 /* lport scsi host config */
451 lp
->host
->max_lun
= FCOE_MAX_LUN
;
452 lp
->host
->max_id
= FCOE_MAX_FCP_TARGET
;
453 lp
->host
->max_channel
= 0;
454 lp
->host
->transportt
= scsi_transport_fcoe_sw
;
456 /* add the new host to the SCSI-ml */
457 rc
= scsi_add_host(lp
->host
, dev
);
459 FCOE_NETDEV_DBG(fcoe_netdev(lp
), "fcoe_shost_config: "
460 "error on scsi_add_host\n");
463 sprintf(fc_host_symbolic_name(lp
->host
), "%s v%s over %s",
464 FCOE_NAME
, FCOE_VERSION
,
465 fcoe_netdev(lp
)->name
);
471 * fcoe_oem_match() - match for read types IO
472 * @fp: the fc_frame for new IO.
474 * Returns : true for read types IO, otherwise returns false.
476 bool fcoe_oem_match(struct fc_frame
*fp
)
478 return fc_fcp_is_read(fr_fsp(fp
)) &&
479 (fr_fsp(fp
)->data_len
> fcoe_ddp_min
);
483 * fcoe_em_config() - allocates em for this lport
484 * @lp: the fcoe that em is to allocated for
486 * Called with write fcoe_hostlist_lock held.
488 * Returns : 0 on success
490 static inline int fcoe_em_config(struct fc_lport
*lp
)
492 struct fcoe_port
*port
= lport_priv(lp
);
493 struct fcoe_interface
*fcoe
= port
->fcoe
;
494 struct fcoe_interface
*oldfcoe
= NULL
;
495 struct net_device
*old_real_dev
, *cur_real_dev
;
496 u16 min_xid
= FCOE_MIN_XID
;
497 u16 max_xid
= FCOE_MAX_XID
;
500 * Check if need to allocate an em instance for
501 * offload exchange ids to be shared across all VN_PORTs/lport.
503 if (!lp
->lro_enabled
|| !lp
->lro_xid
|| (lp
->lro_xid
>= max_xid
)) {
509 * Reuse existing offload em instance in case
510 * it is already allocated on real eth device
512 if (fcoe
->netdev
->priv_flags
& IFF_802_1Q_VLAN
)
513 cur_real_dev
= vlan_dev_real_dev(fcoe
->netdev
);
515 cur_real_dev
= fcoe
->netdev
;
517 list_for_each_entry(oldfcoe
, &fcoe_hostlist
, list
) {
518 if (oldfcoe
->netdev
->priv_flags
& IFF_802_1Q_VLAN
)
519 old_real_dev
= vlan_dev_real_dev(oldfcoe
->netdev
);
521 old_real_dev
= oldfcoe
->netdev
;
523 if (cur_real_dev
== old_real_dev
) {
524 fcoe
->oem
= oldfcoe
->oem
;
530 if (!fc_exch_mgr_add(lp
, fcoe
->oem
, fcoe_oem_match
)) {
531 printk(KERN_ERR
"fcoe_em_config: failed to add "
532 "offload em:%p on interface:%s\n",
533 fcoe
->oem
, fcoe
->netdev
->name
);
537 fcoe
->oem
= fc_exch_mgr_alloc(lp
, FC_CLASS_3
,
538 FCOE_MIN_XID
, lp
->lro_xid
,
541 printk(KERN_ERR
"fcoe_em_config: failed to allocate "
542 "em for offload exches on interface:%s\n",
549 * Exclude offload EM xid range from next EM xid range.
551 min_xid
+= lp
->lro_xid
+ 1;
554 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, min_xid
, max_xid
, NULL
)) {
555 printk(KERN_ERR
"fcoe_em_config: failed to "
556 "allocate em on interface %s\n", fcoe
->netdev
->name
);
564 * fcoe_if_destroy() - FCoE software HBA tear-down function
565 * @lport: fc_lport to destroy
567 static void fcoe_if_destroy(struct fc_lport
*lport
)
569 struct fcoe_port
*port
= lport_priv(lport
);
570 struct fcoe_interface
*fcoe
= port
->fcoe
;
571 struct net_device
*netdev
= fcoe
->netdev
;
573 FCOE_NETDEV_DBG(netdev
, "Destroying interface\n");
575 /* Logout of the fabric */
576 fc_fabric_logoff(lport
);
578 /* Remove the instance from fcoe's list */
579 fcoe_hostlist_remove(lport
);
581 /* clean up netdev configurations */
582 fcoe_netdev_cleanup(port
);
584 /* tear-down the FCoE controller */
585 fcoe_ctlr_destroy(&fcoe
->ctlr
);
587 /* Free queued packets for the per-CPU receive threads */
588 fcoe_percpu_clean(lport
);
590 /* Cleanup the fc_lport */
591 fc_lport_destroy(lport
);
592 fc_fcp_destroy(lport
);
594 /* Detach from the scsi-ml */
595 fc_remove_host(lport
->host
);
596 scsi_remove_host(lport
->host
);
598 /* There are no more rports or I/O, free the EM */
599 fc_exch_mgr_free(lport
);
601 /* Free existing skbs */
602 fcoe_clean_pending_queue(lport
);
605 del_timer_sync(&port
->timer
);
607 /* Free memory used by statistical counters */
608 fc_lport_free_stats(lport
);
610 /* Release the net_device and Scsi_Host */
612 scsi_host_put(lport
->host
);
613 fcoe_interface_put(fcoe
);
617 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
618 * @lp: the corresponding fc_lport
619 * @xid: the exchange id for this ddp transfer
620 * @sgl: the scatterlist describing this transfer
621 * @sgc: number of sg items
625 static int fcoe_ddp_setup(struct fc_lport
*lp
, u16 xid
,
626 struct scatterlist
*sgl
, unsigned int sgc
)
628 struct net_device
*n
= fcoe_netdev(lp
);
630 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_setup
)
631 return n
->netdev_ops
->ndo_fcoe_ddp_setup(n
, xid
, sgl
, sgc
);
637 * fcoe_ddp_done - calls LLD's ddp_done through net_device
638 * @lp: the corresponding fc_lport
639 * @xid: the exchange id for this ddp transfer
641 * Returns : the length of data that have been completed by ddp
643 static int fcoe_ddp_done(struct fc_lport
*lp
, u16 xid
)
645 struct net_device
*n
= fcoe_netdev(lp
);
647 if (n
->netdev_ops
&& n
->netdev_ops
->ndo_fcoe_ddp_done
)
648 return n
->netdev_ops
->ndo_fcoe_ddp_done(n
, xid
);
652 static struct libfc_function_template fcoe_libfc_fcn_templ
= {
653 .frame_send
= fcoe_xmit
,
654 .ddp_setup
= fcoe_ddp_setup
,
655 .ddp_done
= fcoe_ddp_done
,
659 * fcoe_if_create() - this function creates the fcoe port
660 * @fcoe: fcoe_interface structure to create an fc_lport instance on
661 * @parent: device pointer to be the parent in sysfs for the SCSI host
663 * Creates fc_lport struct and scsi_host for lport, configures lport
664 * and starts fabric login.
666 * Returns : The allocated fc_lport or an error pointer
668 static struct fc_lport
*fcoe_if_create(struct fcoe_interface
*fcoe
,
669 struct device
*parent
)
672 struct fc_lport
*lport
= NULL
;
673 struct fcoe_port
*port
;
674 struct Scsi_Host
*shost
;
675 struct net_device
*netdev
= fcoe
->netdev
;
677 FCOE_NETDEV_DBG(netdev
, "Create Interface\n");
679 shost
= libfc_host_alloc(&fcoe_shost_template
,
680 sizeof(struct fcoe_port
));
682 FCOE_NETDEV_DBG(netdev
, "Could not allocate host structure\n");
686 lport
= shost_priv(shost
);
687 port
= lport_priv(lport
);
690 /* configure fc_lport, e.g., em */
691 rc
= fcoe_lport_config(lport
);
693 FCOE_NETDEV_DBG(netdev
, "Could not configure lport for the "
701 fcoe_ctlr_init(&fcoe
->ctlr
);
702 fcoe
->ctlr
.send
= fcoe_fip_send
;
703 fcoe
->ctlr
.update_mac
= fcoe_update_src_mac
;
705 /* configure lport network properties */
706 rc
= fcoe_netdev_config(lport
, netdev
);
708 FCOE_NETDEV_DBG(netdev
, "Could not configure netdev for the "
710 goto out_netdev_cleanup
;
713 /* configure lport scsi host properties */
714 rc
= fcoe_shost_config(lport
, shost
, parent
);
716 FCOE_NETDEV_DBG(netdev
, "Could not configure shost for the "
718 goto out_netdev_cleanup
;
721 /* Initialize the library */
722 rc
= fcoe_libfc_config(lport
, &fcoe_libfc_fcn_templ
);
724 FCOE_NETDEV_DBG(netdev
, "Could not configure libfc for the "
730 * fcoe_em_alloc() and fcoe_hostlist_add() both
731 * need to be atomic under fcoe_hostlist_lock
732 * since fcoe_em_alloc() looks for an existing EM
733 * instance on host list updated by fcoe_hostlist_add().
735 write_lock(&fcoe_hostlist_lock
);
736 /* lport exch manager allocation */
737 rc
= fcoe_em_config(lport
);
739 FCOE_NETDEV_DBG(netdev
, "Could not configure the EM for the "
744 /* add to lports list */
745 fcoe_hostlist_add(lport
);
746 write_unlock(&fcoe_hostlist_lock
);
748 lport
->boot_time
= jiffies
;
750 fc_fabric_login(lport
);
752 if (!fcoe_link_ok(lport
))
753 fcoe_ctlr_link_up(&fcoe
->ctlr
);
756 fcoe_interface_get(fcoe
);
760 fc_exch_mgr_free(lport
);
762 fcoe_netdev_cleanup(port
);
764 scsi_host_put(lport
->host
);
770 * fcoe_if_init() - attach to scsi transport
772 * Returns : 0 on success
774 static int __init
fcoe_if_init(void)
776 /* attach to scsi transport */
777 scsi_transport_fcoe_sw
=
778 fc_attach_transport(&fcoe_transport_function
);
780 if (!scsi_transport_fcoe_sw
) {
781 printk(KERN_ERR
"fcoe: Failed to attach to the FC transport\n");
789 * fcoe_if_exit() - detach from scsi transport
791 * Returns : 0 on success
793 int __exit
fcoe_if_exit(void)
795 fc_release_transport(scsi_transport_fcoe_sw
);
800 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
801 * @cpu: cpu index for the online cpu
803 static void fcoe_percpu_thread_create(unsigned int cpu
)
805 struct fcoe_percpu_s
*p
;
806 struct task_struct
*thread
;
808 p
= &per_cpu(fcoe_percpu
, cpu
);
810 thread
= kthread_create(fcoe_percpu_receive_thread
,
811 (void *)p
, "fcoethread/%d", cpu
);
813 if (likely(!IS_ERR(p
->thread
))) {
814 kthread_bind(thread
, cpu
);
815 wake_up_process(thread
);
817 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
819 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
824 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
825 * @cpu: cpu index the rx thread is to be removed
827 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
828 * current CPU's Rx thread. If the thread being destroyed is bound to
829 * the CPU processing this context the skbs will be freed.
831 static void fcoe_percpu_thread_destroy(unsigned int cpu
)
833 struct fcoe_percpu_s
*p
;
834 struct task_struct
*thread
;
835 struct page
*crc_eof
;
838 struct fcoe_percpu_s
*p0
;
839 unsigned targ_cpu
= smp_processor_id();
840 #endif /* CONFIG_SMP */
842 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu
);
844 /* Prevent any new skbs from being queued for this CPU. */
845 p
= &per_cpu(fcoe_percpu
, cpu
);
846 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
849 crc_eof
= p
->crc_eof_page
;
850 p
->crc_eof_page
= NULL
;
851 p
->crc_eof_offset
= 0;
852 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
856 * Don't bother moving the skb's if this context is running
857 * on the same CPU that is having its thread destroyed. This
858 * can easily happen when the module is removed.
860 if (cpu
!= targ_cpu
) {
861 p0
= &per_cpu(fcoe_percpu
, targ_cpu
);
862 spin_lock_bh(&p0
->fcoe_rx_list
.lock
);
864 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
867 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
868 __skb_queue_tail(&p0
->fcoe_rx_list
, skb
);
869 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
872 * The targeted CPU is not initialized and cannot accept
873 * new skbs. Unlock the targeted CPU and drop the skbs
874 * on the CPU that is going offline.
876 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
878 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
882 * This scenario occurs when the module is being removed
883 * and all threads are being destroyed. skbs will continue
884 * to be shifted from the CPU thread that is being removed
885 * to the CPU thread associated with the CPU that is processing
886 * the module removal. Once there is only one CPU Rx thread it
887 * will reach this case and we will drop all skbs and later
890 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
891 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
893 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
897 * This a non-SMP scenario where the singular Rx thread is
898 * being removed. Free all skbs and stop the thread.
900 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
901 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
903 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
907 kthread_stop(thread
);
914 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
915 * @nfb: callback data block
916 * @action: event triggering the callback
917 * @hcpu: index for the cpu of this event
919 * This creates or destroys per cpu data for fcoe
921 * Returns NOTIFY_OK always.
923 static int fcoe_cpu_callback(struct notifier_block
*nfb
,
924 unsigned long action
, void *hcpu
)
926 unsigned cpu
= (unsigned long)hcpu
;
930 case CPU_ONLINE_FROZEN
:
931 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu
);
932 fcoe_percpu_thread_create(cpu
);
935 case CPU_DEAD_FROZEN
:
936 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu
);
937 fcoe_percpu_thread_destroy(cpu
);
945 static struct notifier_block fcoe_cpu_notifier
= {
946 .notifier_call
= fcoe_cpu_callback
,
950 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
951 * @skb: the receive skb
952 * @dev: associated net device
954 * @olddev: last device
956 * this function will receive the packet and build fc frame and pass it up
958 * Returns: 0 for success
960 int fcoe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
961 struct packet_type
*ptype
, struct net_device
*olddev
)
964 struct fcoe_rcv_info
*fr
;
965 struct fcoe_interface
*fcoe
;
966 struct fc_frame_header
*fh
;
967 struct fcoe_percpu_s
*fps
;
970 fcoe
= container_of(ptype
, struct fcoe_interface
, fcoe_packet_type
);
972 if (unlikely(lp
== NULL
)) {
973 FCOE_NETDEV_DBG(dev
, "Cannot find hba structure");
979 FCOE_NETDEV_DBG(dev
, "skb_info: len:%d data_len:%d head:%p "
980 "data:%p tail:%p end:%p sum:%d dev:%s",
981 skb
->len
, skb
->data_len
, skb
->head
, skb
->data
,
982 skb_tail_pointer(skb
), skb_end_pointer(skb
),
983 skb
->csum
, skb
->dev
? skb
->dev
->name
: "<NULL>");
985 /* check for FCOE packet type */
986 if (unlikely(eth_hdr(skb
)->h_proto
!= htons(ETH_P_FCOE
))) {
987 FCOE_NETDEV_DBG(dev
, "Wrong FC type frame");
992 * Check for minimum frame length, and make sure required FCoE
993 * and FC headers are pulled into the linear data area.
995 if (unlikely((skb
->len
< FCOE_MIN_FRAME
) ||
996 !pskb_may_pull(skb
, FCOE_HEADER_LEN
)))
999 skb_set_transport_header(skb
, sizeof(struct fcoe_hdr
));
1000 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1002 fr
= fcoe_dev_from_skb(skb
);
1007 * In case the incoming frame's exchange is originated from
1008 * the initiator, then received frame's exchange id is ANDed
1009 * with fc_cpu_mask bits to get the same cpu on which exchange
1010 * was originated, otherwise just use the current cpu.
1012 if (ntoh24(fh
->fh_f_ctl
) & FC_FC_EX_CTX
)
1013 cpu
= ntohs(fh
->fh_ox_id
) & fc_cpu_mask
;
1015 cpu
= smp_processor_id();
1017 fps
= &per_cpu(fcoe_percpu
, cpu
);
1018 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
1019 if (unlikely(!fps
->thread
)) {
1021 * The targeted CPU is not ready, let's target
1022 * the first CPU now. For non-SMP systems this
1023 * will check the same CPU twice.
1025 FCOE_NETDEV_DBG(dev
, "CPU is online, but no receive thread "
1026 "ready for incoming skb- using first online "
1029 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
1030 cpu
= first_cpu(cpu_online_map
);
1031 fps
= &per_cpu(fcoe_percpu
, cpu
);
1032 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
1034 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
1040 * We now have a valid CPU that we're targeting for
1041 * this skb. We also have this receive thread locked,
1042 * so we're free to queue skbs into it's queue.
1044 __skb_queue_tail(&fps
->fcoe_rx_list
, skb
);
1045 if (fps
->fcoe_rx_list
.qlen
== 1)
1046 wake_up_process(fps
->thread
);
1048 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
1052 fc_lport_get_stats(lp
)->ErrorFrames
++;
1060 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1061 * @skb: the skb to be xmitted
1063 * Returns: 0 for success
1065 static inline int fcoe_start_io(struct sk_buff
*skb
)
1070 rc
= dev_queue_xmit(skb
);
1078 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1079 * @skb: the skb to be xmitted
1082 * Returns: 0 for success
1084 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
1086 struct fcoe_percpu_s
*fps
;
1089 fps
= &get_cpu_var(fcoe_percpu
);
1090 page
= fps
->crc_eof_page
;
1092 page
= alloc_page(GFP_ATOMIC
);
1094 put_cpu_var(fcoe_percpu
);
1097 fps
->crc_eof_page
= page
;
1098 fps
->crc_eof_offset
= 0;
1102 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
1103 fps
->crc_eof_offset
, tlen
);
1105 skb
->data_len
+= tlen
;
1106 skb
->truesize
+= tlen
;
1107 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
1109 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
1110 fps
->crc_eof_page
= NULL
;
1111 fps
->crc_eof_offset
= 0;
1114 put_cpu_var(fcoe_percpu
);
1119 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1120 * @fp: the fc_frame containing data to be checksummed
1122 * This uses crc32() to calculate the crc for port frame
1123 * Return : 32 bit crc
1125 u32
fcoe_fc_crc(struct fc_frame
*fp
)
1127 struct sk_buff
*skb
= fp_skb(fp
);
1128 struct skb_frag_struct
*frag
;
1129 unsigned char *data
;
1130 unsigned long off
, len
, clen
;
1134 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
1136 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1137 frag
= &skb_shinfo(skb
)->frags
[i
];
1138 off
= frag
->page_offset
;
1141 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
1142 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
1143 KM_SKB_DATA_SOFTIRQ
);
1144 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
1145 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
1154 * fcoe_xmit() - FCoE frame transmit function
1155 * @lp: the associated local fcoe
1156 * @fp: the fc_frame to be transmitted
1158 * Return : 0 for success
1160 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
1165 struct fcoe_crc_eof
*cp
;
1166 struct sk_buff
*skb
;
1167 struct fcoe_dev_stats
*stats
;
1168 struct fc_frame_header
*fh
;
1169 unsigned int hlen
; /* header length implies the version */
1170 unsigned int tlen
; /* trailer length */
1171 unsigned int elen
; /* eth header, may include vlan */
1172 struct fcoe_port
*port
= lport_priv(lp
);
1173 struct fcoe_interface
*fcoe
= port
->fcoe
;
1175 struct fcoe_hdr
*hp
;
1177 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
1179 fh
= fc_frame_header_get(fp
);
1181 wlen
= skb
->len
/ FCOE_WORD_TO_BYTE
;
1188 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
) &&
1189 fcoe_ctlr_els_send(&fcoe
->ctlr
, skb
))
1195 elen
= sizeof(struct ethhdr
);
1196 hlen
= sizeof(struct fcoe_hdr
);
1197 tlen
= sizeof(struct fcoe_crc_eof
);
1198 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
1201 if (likely(lp
->crc_offload
)) {
1202 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1203 skb
->csum_start
= skb_headroom(skb
);
1204 skb
->csum_offset
= skb
->len
;
1207 skb
->ip_summed
= CHECKSUM_NONE
;
1208 crc
= fcoe_fc_crc(fp
);
1211 /* copy port crc and eof to the skb buff */
1212 if (skb_is_nonlinear(skb
)) {
1214 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
1218 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
1219 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
1220 + frag
->page_offset
;
1222 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
1225 memset(cp
, 0, sizeof(*cp
));
1227 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
1229 if (skb_is_nonlinear(skb
)) {
1230 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
1234 /* adjust skb network/transport offsets to match mac/fcoe/port */
1235 skb_push(skb
, elen
+ hlen
);
1236 skb_reset_mac_header(skb
);
1237 skb_reset_network_header(skb
);
1238 skb
->mac_len
= elen
;
1239 skb
->protocol
= htons(ETH_P_FCOE
);
1240 skb
->dev
= fcoe
->netdev
;
1242 /* fill up mac and fcoe headers */
1244 eh
->h_proto
= htons(ETH_P_FCOE
);
1245 if (fcoe
->ctlr
.map_dest
)
1246 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
1248 /* insert GW address */
1249 memcpy(eh
->h_dest
, fcoe
->ctlr
.dest_addr
, ETH_ALEN
);
1251 if (unlikely(fcoe
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
))
1252 memcpy(eh
->h_source
, fcoe
->ctlr
.ctl_src_addr
, ETH_ALEN
);
1254 memcpy(eh
->h_source
, fcoe
->ctlr
.data_src_addr
, ETH_ALEN
);
1256 hp
= (struct fcoe_hdr
*)(eh
+ 1);
1257 memset(hp
, 0, sizeof(*hp
));
1259 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
1262 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1263 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
1264 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
1265 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
1267 skb_shinfo(skb
)->gso_type
= 0;
1268 skb_shinfo(skb
)->gso_size
= 0;
1270 /* update tx stats: regardless if LLD fails */
1271 stats
= fc_lport_get_stats(lp
);
1273 stats
->TxWords
+= wlen
;
1275 /* send down to lld */
1277 if (port
->fcoe_pending_queue
.qlen
)
1278 fcoe_check_wait_queue(lp
, skb
);
1279 else if (fcoe_start_io(skb
))
1280 fcoe_check_wait_queue(lp
, skb
);
1286 * fcoe_percpu_receive_thread() - recv thread per cpu
1287 * @arg: ptr to the fcoe per cpu struct
1289 * Return: 0 for success
1291 int fcoe_percpu_receive_thread(void *arg
)
1293 struct fcoe_percpu_s
*p
= arg
;
1295 struct fc_lport
*lp
;
1296 struct fcoe_rcv_info
*fr
;
1297 struct fcoe_dev_stats
*stats
;
1298 struct fc_frame_header
*fh
;
1299 struct sk_buff
*skb
;
1300 struct fcoe_crc_eof crc_eof
;
1301 struct fc_frame
*fp
;
1303 struct fcoe_port
*port
;
1304 struct fcoe_hdr
*hp
;
1306 set_user_nice(current
, -20);
1308 while (!kthread_should_stop()) {
1310 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1311 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
1312 set_current_state(TASK_INTERRUPTIBLE
);
1313 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1315 set_current_state(TASK_RUNNING
);
1316 if (kthread_should_stop())
1318 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1320 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1321 fr
= fcoe_dev_from_skb(skb
);
1323 if (unlikely(lp
== NULL
)) {
1324 FCOE_NETDEV_DBG(skb
->dev
, "Invalid HBA Structure");
1329 FCOE_NETDEV_DBG(skb
->dev
, "skb_info: len:%d data_len:%d "
1330 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1331 skb
->len
, skb
->data_len
,
1332 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
1333 skb_end_pointer(skb
), skb
->csum
,
1334 skb
->dev
? skb
->dev
->name
: "<NULL>");
1337 * Save source MAC address before discarding header.
1339 port
= lport_priv(lp
);
1340 if (skb_is_nonlinear(skb
))
1341 skb_linearize(skb
); /* not ideal */
1342 mac
= eth_hdr(skb
)->h_source
;
1345 * Frame length checks and setting up the header pointers
1346 * was done in fcoe_rcv already.
1348 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
1349 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1351 stats
= fc_lport_get_stats(lp
);
1352 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
1353 if (stats
->ErrorFrames
< 5)
1354 printk(KERN_WARNING
"fcoe: FCoE version "
1355 "mismatch: The frame has "
1356 "version %x, but the "
1357 "initiator supports version "
1358 "%x\n", FC_FCOE_DECAPS_VER(hp
),
1360 stats
->ErrorFrames
++;
1365 skb_pull(skb
, sizeof(struct fcoe_hdr
));
1366 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
1369 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
1371 fp
= (struct fc_frame
*)skb
;
1374 fr_sof(fp
) = hp
->fcoe_sof
;
1376 /* Copy out the CRC and EOF trailer for access */
1377 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
1381 fr_eof(fp
) = crc_eof
.fcoe_eof
;
1382 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
1383 if (pskb_trim(skb
, fr_len
)) {
1389 * We only check CRC if no offload is available and if it is
1390 * it's solicited data, in which case, the FCP layer would
1391 * check it during the copy.
1393 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
1394 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1396 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
1398 fh
= fc_frame_header_get(fp
);
1399 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
1400 fh
->fh_type
== FC_TYPE_FCP
) {
1401 fc_exch_recv(lp
, fp
);
1404 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
1405 if (le32_to_cpu(fr_crc(fp
)) !=
1406 ~crc32(~0, skb
->data
, fr_len
)) {
1407 if (stats
->InvalidCRCCount
< 5)
1408 printk(KERN_WARNING
"fcoe: dropping "
1409 "frame with CRC error\n");
1410 stats
->InvalidCRCCount
++;
1411 stats
->ErrorFrames
++;
1415 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1417 if (unlikely(port
->fcoe
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
) &&
1418 fcoe_ctlr_recv_flogi(&port
->fcoe
->ctlr
, fp
, mac
)) {
1422 fc_exch_recv(lp
, fp
);
1428 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1431 * This empties the wait_queue, dequeue the head of the wait_queue queue
1432 * and calls fcoe_start_io() for each packet, if all skb have been
1433 * transmitted, return qlen or -1 if a error occurs, then restore
1434 * wait_queue and try again later.
1436 * The wait_queue is used when the skb transmit fails. skb will go
1437 * in the wait_queue which will be emptied by the timer function or
1438 * by the next skb transmit.
1440 static void fcoe_check_wait_queue(struct fc_lport
*lp
, struct sk_buff
*skb
)
1442 struct fcoe_port
*port
= lport_priv(lp
);
1445 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1448 __skb_queue_tail(&port
->fcoe_pending_queue
, skb
);
1450 if (port
->fcoe_pending_queue_active
)
1452 port
->fcoe_pending_queue_active
= 1;
1454 while (port
->fcoe_pending_queue
.qlen
) {
1455 /* keep qlen > 0 until fcoe_start_io succeeds */
1456 port
->fcoe_pending_queue
.qlen
++;
1457 skb
= __skb_dequeue(&port
->fcoe_pending_queue
);
1459 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1460 rc
= fcoe_start_io(skb
);
1461 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1464 __skb_queue_head(&port
->fcoe_pending_queue
, skb
);
1465 /* undo temporary increment above */
1466 port
->fcoe_pending_queue
.qlen
--;
1469 /* undo temporary increment above */
1470 port
->fcoe_pending_queue
.qlen
--;
1473 if (port
->fcoe_pending_queue
.qlen
< FCOE_LOW_QUEUE_DEPTH
)
1475 if (port
->fcoe_pending_queue
.qlen
&& !timer_pending(&port
->timer
))
1476 mod_timer(&port
->timer
, jiffies
+ 2);
1477 port
->fcoe_pending_queue_active
= 0;
1479 if (port
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
1481 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1486 * fcoe_dev_setup() - setup link change notification interface
1488 static void fcoe_dev_setup(void)
1490 register_netdevice_notifier(&fcoe_notifier
);
1494 * fcoe_dev_cleanup() - cleanup link change notification interface
1496 static void fcoe_dev_cleanup(void)
1498 unregister_netdevice_notifier(&fcoe_notifier
);
1502 * fcoe_device_notification() - netdev event notification callback
1503 * @notifier: context of the notification
1504 * @event: type of event
1505 * @ptr: fixed array for output parsed ifname
1507 * This function is called by the ethernet driver in case of link change event
1509 * Returns: 0 for success
1511 static int fcoe_device_notification(struct notifier_block
*notifier
,
1512 ulong event
, void *ptr
)
1514 struct fc_lport
*lp
= NULL
;
1515 struct net_device
*netdev
= ptr
;
1516 struct fcoe_interface
*fcoe
;
1517 struct fcoe_dev_stats
*stats
;
1518 u32 link_possible
= 1;
1522 read_lock(&fcoe_hostlist_lock
);
1523 list_for_each_entry(fcoe
, &fcoe_hostlist
, list
) {
1524 if (fcoe
->netdev
== netdev
) {
1529 read_unlock(&fcoe_hostlist_lock
);
1537 case NETDEV_GOING_DOWN
:
1543 case NETDEV_CHANGEMTU
:
1544 mfs
= netdev
->mtu
- (sizeof(struct fcoe_hdr
) +
1545 sizeof(struct fcoe_crc_eof
));
1546 if (mfs
>= FC_MIN_MAX_FRAME
)
1547 fc_set_mfs(lp
, mfs
);
1549 case NETDEV_REGISTER
:
1552 FCOE_NETDEV_DBG(netdev
, "Unknown event %ld "
1553 "from netdev netlink\n", event
);
1555 if (link_possible
&& !fcoe_link_ok(lp
))
1556 fcoe_ctlr_link_up(&fcoe
->ctlr
);
1557 else if (fcoe_ctlr_link_down(&fcoe
->ctlr
)) {
1558 stats
= fc_lport_get_stats(lp
);
1559 stats
->LinkFailureCount
++;
1560 fcoe_clean_pending_queue(lp
);
1567 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1568 * @buffer: incoming buffer to be copied
1570 * Returns: NULL or ptr to net_device
1572 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
1575 char ifname
[IFNAMSIZ
+ 2];
1578 strlcpy(ifname
, buffer
, IFNAMSIZ
);
1579 cp
= ifname
+ strlen(ifname
);
1580 while (--cp
>= ifname
&& *cp
== '\n')
1582 return dev_get_by_name(&init_net
, ifname
);
1588 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1589 * @netdev: the target netdev
1591 * Returns: ptr to the struct module, NULL for failure
1593 static struct module
*
1594 fcoe_netdev_to_module_owner(const struct net_device
*netdev
)
1601 dev
= netdev
->dev
.parent
;
1608 return dev
->driver
->owner
;
1612 * fcoe_ethdrv_get() - Hold the Ethernet driver
1613 * @netdev: the target netdev
1615 * Holds the Ethernet driver module by try_module_get() for
1616 * the corresponding netdev.
1618 * Returns: 0 for success
1620 static int fcoe_ethdrv_get(const struct net_device
*netdev
)
1622 struct module
*owner
;
1624 owner
= fcoe_netdev_to_module_owner(netdev
);
1626 FCOE_NETDEV_DBG(netdev
, "Hold driver module %s\n",
1627 module_name(owner
));
1628 return try_module_get(owner
);
1634 * fcoe_ethdrv_put() - Release the Ethernet driver
1635 * @netdev: the target netdev
1637 * Releases the Ethernet driver module by module_put for
1638 * the corresponding netdev.
1640 * Returns: 0 for success
1642 static int fcoe_ethdrv_put(const struct net_device
*netdev
)
1644 struct module
*owner
;
1646 owner
= fcoe_netdev_to_module_owner(netdev
);
1648 FCOE_NETDEV_DBG(netdev
, "Release driver module %s\n",
1649 module_name(owner
));
1657 * fcoe_destroy() - handles the destroy from sysfs
1658 * @buffer: expected to be an eth if name
1659 * @kp: associated kernel param
1661 * Returns: 0 for success
1663 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1665 struct net_device
*netdev
;
1666 struct fcoe_interface
*fcoe
;
1667 struct fcoe_port
*port
;
1668 struct fc_lport
*lport
;
1671 netdev
= fcoe_if_to_netdev(buffer
);
1676 /* look for existing lport */
1677 lport
= fcoe_hostlist_lookup(netdev
);
1682 port
= lport_priv(lport
);
1684 fcoe_if_destroy(lport
);
1685 fcoe_ethdrv_put(netdev
);
1694 * fcoe_create() - Handles the create call from sysfs
1695 * @buffer: expected to be an eth if name
1696 * @kp: associated kernel param
1698 * Returns: 0 for success
1700 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1703 struct fcoe_interface
*fcoe
;
1704 struct fc_lport
*lport
;
1705 struct net_device
*netdev
;
1707 netdev
= fcoe_if_to_netdev(buffer
);
1712 /* look for existing lport */
1713 if (fcoe_hostlist_lookup(netdev
)) {
1717 fcoe_ethdrv_get(netdev
);
1719 fcoe
= fcoe_interface_create(netdev
);
1725 lport
= fcoe_if_create(fcoe
, &netdev
->dev
);
1726 if (IS_ERR(lport
)) {
1727 printk(KERN_ERR
"fcoe: Failed to create interface (%s)\n",
1729 fcoe_ethdrv_put(netdev
);
1738 fcoe_interface_put(fcoe
);
1745 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1746 __MODULE_PARM_TYPE(create
, "string");
1747 MODULE_PARM_DESC(create
, "Create fcoe fcoe using net device passed in.");
1748 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1749 __MODULE_PARM_TYPE(destroy
, "string");
1750 MODULE_PARM_DESC(destroy
, "Destroy fcoe fcoe");
1753 * fcoe_link_ok() - Check if link is ok for the fc_lport
1754 * @lp: ptr to the fc_lport
1756 * Any permanently-disqualifying conditions have been previously checked.
1757 * This also updates the speed setting, which may change with link for 100/1000.
1759 * This function should probably be checking for PAUSE support at some point
1760 * in the future. Currently Per-priority-pause is not determinable using
1761 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1763 * Returns: 0 if link is OK for use by FCoE.
1766 int fcoe_link_ok(struct fc_lport
*lp
)
1768 struct fcoe_port
*port
= lport_priv(lp
);
1769 struct net_device
*dev
= port
->fcoe
->netdev
;
1770 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1772 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
) &&
1773 (!dev_ethtool_get_settings(dev
, &ecmd
))) {
1774 lp
->link_supported_speeds
&=
1775 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1776 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1777 SUPPORTED_1000baseT_Full
))
1778 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1779 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1780 lp
->link_supported_speeds
|=
1781 FC_PORTSPEED_10GBIT
;
1782 if (ecmd
.speed
== SPEED_1000
)
1783 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1784 if (ecmd
.speed
== SPEED_10000
)
1785 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1793 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1796 void fcoe_percpu_clean(struct fc_lport
*lp
)
1798 struct fcoe_percpu_s
*pp
;
1799 struct fcoe_rcv_info
*fr
;
1800 struct sk_buff_head
*list
;
1801 struct sk_buff
*skb
, *next
;
1802 struct sk_buff
*head
;
1805 for_each_possible_cpu(cpu
) {
1806 pp
= &per_cpu(fcoe_percpu
, cpu
);
1807 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1808 list
= &pp
->fcoe_rx_list
;
1810 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1813 fr
= fcoe_dev_from_skb(skb
);
1814 if (fr
->fr_dev
== lp
) {
1815 __skb_unlink(skb
, list
);
1819 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1824 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1825 * @lp: the corresponding fc_lport
1829 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1831 struct fcoe_port
*port
= lport_priv(lp
);
1832 struct sk_buff
*skb
;
1834 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1835 while ((skb
= __skb_dequeue(&port
->fcoe_pending_queue
)) != NULL
) {
1836 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1838 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1840 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1844 * fcoe_reset() - Resets the fcoe
1845 * @shost: shost the reset is from
1849 int fcoe_reset(struct Scsi_Host
*shost
)
1851 struct fc_lport
*lport
= shost_priv(shost
);
1852 fc_lport_reset(lport
);
1857 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1858 * @dev: this is currently ptr to net_device
1860 * Called with fcoe_hostlist_lock held.
1862 * Returns: NULL or the located fcoe_port
1864 static struct fcoe_interface
*
1865 fcoe_hostlist_lookup_port(const struct net_device
*dev
)
1867 struct fcoe_interface
*fcoe
;
1869 list_for_each_entry(fcoe
, &fcoe_hostlist
, list
) {
1870 if (fcoe
->netdev
== dev
)
1877 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1878 * @netdev: ptr to net_device
1880 * Returns: 0 for success
1882 struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1884 struct fcoe_interface
*fcoe
;
1886 read_lock(&fcoe_hostlist_lock
);
1887 fcoe
= fcoe_hostlist_lookup_port(netdev
);
1888 read_unlock(&fcoe_hostlist_lock
);
1890 return (fcoe
) ? fcoe
->ctlr
.lp
: NULL
;
1894 * fcoe_hostlist_add() - Add a lport to lports list
1895 * @lp: ptr to the fc_lport to be added
1897 * Called with write fcoe_hostlist_lock held.
1899 * Returns: 0 for success
1901 int fcoe_hostlist_add(const struct fc_lport
*lport
)
1903 struct fcoe_interface
*fcoe
;
1904 struct fcoe_port
*port
;
1906 fcoe
= fcoe_hostlist_lookup_port(fcoe_netdev(lport
));
1908 port
= lport_priv(lport
);
1910 list_add_tail(&fcoe
->list
, &fcoe_hostlist
);
1916 * fcoe_hostlist_remove() - remove a lport from lports list
1917 * @lp: ptr to the fc_lport to be removed
1919 * Returns: 0 for success
1921 int fcoe_hostlist_remove(const struct fc_lport
*lport
)
1923 struct fcoe_interface
*fcoe
;
1925 write_lock_bh(&fcoe_hostlist_lock
);
1926 fcoe
= fcoe_hostlist_lookup_port(fcoe_netdev(lport
));
1928 list_del(&fcoe
->list
);
1929 write_unlock_bh(&fcoe_hostlist_lock
);
1935 * fcoe_init() - fcoe module loading initialization
1937 * Returns 0 on success, negative on failure
1939 static int __init
fcoe_init(void)
1943 struct fcoe_percpu_s
*p
;
1945 for_each_possible_cpu(cpu
) {
1946 p
= &per_cpu(fcoe_percpu
, cpu
);
1947 skb_queue_head_init(&p
->fcoe_rx_list
);
1950 for_each_online_cpu(cpu
)
1951 fcoe_percpu_thread_create(cpu
);
1953 /* Initialize per CPU interrupt thread */
1954 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1958 /* Setup link change notification */
1961 rc
= fcoe_if_init();
1968 for_each_online_cpu(cpu
) {
1969 fcoe_percpu_thread_destroy(cpu
);
1974 module_init(fcoe_init
);
1977 * fcoe_exit() - fcoe module unloading cleanup
1979 * Returns 0 on success, negative on failure
1981 static void __exit
fcoe_exit(void)
1984 struct fcoe_interface
*fcoe
, *tmp
;
1988 /* releases the associated fcoe hosts */
1989 list_for_each_entry_safe(fcoe
, tmp
, &fcoe_hostlist
, list
)
1990 fcoe_if_destroy(fcoe
->ctlr
.lp
);
1992 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
1994 for_each_online_cpu(cpu
)
1995 fcoe_percpu_thread_destroy(cpu
);
1997 /* detach from scsi transport */
2000 module_exit(fcoe_exit
);