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).");
58 DEFINE_MUTEX(fcoe_config_mutex
);
60 /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61 static DECLARE_COMPLETION(fcoe_flush_completion
);
64 /* must only by accessed under the RTNL mutex */
65 LIST_HEAD(fcoe_hostlist
);
66 DEFINE_PER_CPU(struct fcoe_percpu_s
, fcoe_percpu
);
68 /* Function Prototypes */
69 static int fcoe_reset(struct Scsi_Host
*shost
);
70 static int fcoe_xmit(struct fc_lport
*, struct fc_frame
*);
71 static int fcoe_rcv(struct sk_buff
*, struct net_device
*,
72 struct packet_type
*, struct net_device
*);
73 static int fcoe_percpu_receive_thread(void *arg
);
74 static void fcoe_clean_pending_queue(struct fc_lport
*lp
);
75 static void fcoe_percpu_clean(struct fc_lport
*lp
);
76 static int fcoe_link_ok(struct fc_lport
*lp
);
78 static struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*);
79 static int fcoe_hostlist_add(const struct fc_lport
*);
81 static void fcoe_check_wait_queue(struct fc_lport
*, struct sk_buff
*);
82 static int fcoe_device_notification(struct notifier_block
*, ulong
, void *);
83 static void fcoe_dev_setup(void);
84 static void fcoe_dev_cleanup(void);
85 static struct fcoe_interface
*
86 fcoe_hostlist_lookup_port(const struct net_device
*dev
);
88 /* notification function from net device */
89 static struct notifier_block fcoe_notifier
= {
90 .notifier_call
= fcoe_device_notification
,
93 static struct scsi_transport_template
*scsi_transport_fcoe_sw
;
95 struct fc_function_template fcoe_transport_function
= {
96 .show_host_node_name
= 1,
97 .show_host_port_name
= 1,
98 .show_host_supported_classes
= 1,
99 .show_host_supported_fc4s
= 1,
100 .show_host_active_fc4s
= 1,
101 .show_host_maxframe_size
= 1,
103 .show_host_port_id
= 1,
104 .show_host_supported_speeds
= 1,
105 .get_host_speed
= fc_get_host_speed
,
106 .show_host_speed
= 1,
107 .show_host_port_type
= 1,
108 .get_host_port_state
= fc_get_host_port_state
,
109 .show_host_port_state
= 1,
110 .show_host_symbolic_name
= 1,
112 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
113 .show_rport_maxframe_size
= 1,
114 .show_rport_supported_classes
= 1,
116 .show_host_fabric_name
= 1,
117 .show_starget_node_name
= 1,
118 .show_starget_port_name
= 1,
119 .show_starget_port_id
= 1,
120 .set_rport_dev_loss_tmo
= fc_set_rport_loss_tmo
,
121 .show_rport_dev_loss_tmo
= 1,
122 .get_fc_host_stats
= fc_get_host_stats
,
123 .issue_fc_host_lip
= fcoe_reset
,
125 .terminate_rport_io
= fc_rport_terminate_io
,
128 static struct scsi_host_template fcoe_shost_template
= {
129 .module
= THIS_MODULE
,
130 .name
= "FCoE Driver",
131 .proc_name
= FCOE_NAME
,
132 .queuecommand
= fc_queuecommand
,
133 .eh_abort_handler
= fc_eh_abort
,
134 .eh_device_reset_handler
= fc_eh_device_reset
,
135 .eh_host_reset_handler
= fc_eh_host_reset
,
136 .slave_alloc
= fc_slave_alloc
,
137 .change_queue_depth
= fc_change_queue_depth
,
138 .change_queue_type
= fc_change_queue_type
,
141 .can_queue
= FCOE_MAX_OUTSTANDING_COMMANDS
,
142 .use_clustering
= ENABLE_CLUSTERING
,
143 .sg_tablesize
= SG_ALL
,
144 .max_sectors
= 0xffff,
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 * fcoe_interface_setup()
152 * @fcoe: new fcoe_interface
153 * @netdev : ptr to the associated netdevice struct
155 * Returns : 0 for success
156 * Locking: must be called with the RTNL mutex held
158 static int fcoe_interface_setup(struct fcoe_interface
*fcoe
,
159 struct net_device
*netdev
)
161 struct fcoe_ctlr
*fip
= &fcoe
->ctlr
;
162 struct netdev_hw_addr
*ha
;
163 struct net_device
*real_dev
;
164 u8 flogi_maddr
[ETH_ALEN
];
166 fcoe
->netdev
= netdev
;
168 /* Do not support for bonding device */
169 if ((netdev
->priv_flags
& IFF_MASTER_ALB
) ||
170 (netdev
->priv_flags
& IFF_SLAVE_INACTIVE
) ||
171 (netdev
->priv_flags
& IFF_MASTER_8023AD
)) {
175 /* look for SAN MAC address, if multiple SAN MACs exist, only
176 * use the first one for SPMA */
177 real_dev
= (netdev
->priv_flags
& IFF_802_1Q_VLAN
) ?
178 vlan_dev_real_dev(netdev
) : netdev
;
180 for_each_dev_addr(real_dev
, ha
) {
181 if ((ha
->type
== NETDEV_HW_ADDR_T_SAN
) &&
182 (is_valid_ether_addr(ha
->addr
))) {
183 memcpy(fip
->ctl_src_addr
, ha
->addr
, ETH_ALEN
);
190 /* setup Source Mac Address */
192 memcpy(fip
->ctl_src_addr
, netdev
->dev_addr
, netdev
->addr_len
);
195 * Add FCoE MAC address as second unicast MAC address
196 * or enter promiscuous mode if not capable of listening
197 * for multiple unicast MACs.
199 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
200 dev_unicast_add(netdev
, flogi_maddr
);
202 dev_unicast_add(netdev
, fip
->ctl_src_addr
);
203 dev_mc_add(netdev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
206 * setup the receive function from ethernet driver
207 * on the ethertype for the given device
209 fcoe
->fcoe_packet_type
.func
= fcoe_rcv
;
210 fcoe
->fcoe_packet_type
.type
= __constant_htons(ETH_P_FCOE
);
211 fcoe
->fcoe_packet_type
.dev
= netdev
;
212 dev_add_pack(&fcoe
->fcoe_packet_type
);
214 fcoe
->fip_packet_type
.func
= fcoe_fip_recv
;
215 fcoe
->fip_packet_type
.type
= htons(ETH_P_FIP
);
216 fcoe
->fip_packet_type
.dev
= netdev
;
217 dev_add_pack(&fcoe
->fip_packet_type
);
222 static void fcoe_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
);
223 static void fcoe_update_src_mac(struct fcoe_ctlr
*fip
, u8
*old
, u8
*new);
224 static void fcoe_destroy_work(struct work_struct
*work
);
227 * fcoe_interface_create()
228 * @netdev: network interface
230 * Returns: pointer to a struct fcoe_interface or NULL on error
232 static struct fcoe_interface
*fcoe_interface_create(struct net_device
*netdev
)
234 struct fcoe_interface
*fcoe
;
236 fcoe
= kzalloc(sizeof(*fcoe
), GFP_KERNEL
);
238 FCOE_NETDEV_DBG(netdev
, "Could not allocate fcoe structure\n");
243 kref_init(&fcoe
->kref
);
248 fcoe_ctlr_init(&fcoe
->ctlr
);
249 fcoe
->ctlr
.send
= fcoe_fip_send
;
250 fcoe
->ctlr
.update_mac
= fcoe_update_src_mac
;
252 fcoe_interface_setup(fcoe
, netdev
);
258 * fcoe_interface_cleanup() - clean up netdev configurations
261 * Caller must be holding the RTNL mutex
263 void fcoe_interface_cleanup(struct fcoe_interface
*fcoe
)
265 struct net_device
*netdev
= fcoe
->netdev
;
266 struct fcoe_ctlr
*fip
= &fcoe
->ctlr
;
267 u8 flogi_maddr
[ETH_ALEN
];
270 * Don't listen for Ethernet packets anymore.
271 * synchronize_net() ensures that the packet handlers are not running
272 * on another CPU. dev_remove_pack() would do that, this calls the
273 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
275 __dev_remove_pack(&fcoe
->fcoe_packet_type
);
276 __dev_remove_pack(&fcoe
->fip_packet_type
);
279 /* Delete secondary MAC addresses */
280 memcpy(flogi_maddr
, (u8
[6]) FC_FCOE_FLOGI_MAC
, ETH_ALEN
);
281 dev_unicast_delete(netdev
, flogi_maddr
);
282 if (!is_zero_ether_addr(fip
->data_src_addr
))
283 dev_unicast_delete(netdev
, fip
->data_src_addr
);
285 dev_unicast_delete(netdev
, fip
->ctl_src_addr
);
286 dev_mc_delete(netdev
, FIP_ALL_ENODE_MACS
, ETH_ALEN
, 0);
290 * fcoe_interface_release() - fcoe_port kref release function
291 * @kref: embedded reference count in an fcoe_interface struct
293 static void fcoe_interface_release(struct kref
*kref
)
295 struct fcoe_interface
*fcoe
;
296 struct net_device
*netdev
;
298 fcoe
= container_of(kref
, struct fcoe_interface
, kref
);
299 netdev
= fcoe
->netdev
;
300 /* tear-down the FCoE controller */
301 fcoe_ctlr_destroy(&fcoe
->ctlr
);
307 * fcoe_interface_get()
310 static inline void fcoe_interface_get(struct fcoe_interface
*fcoe
)
312 kref_get(&fcoe
->kref
);
316 * fcoe_interface_put()
319 static inline void fcoe_interface_put(struct fcoe_interface
*fcoe
)
321 kref_put(&fcoe
->kref
, fcoe_interface_release
);
325 * fcoe_fip_recv - handle a received FIP frame.
326 * @skb: the receive skb
327 * @dev: associated &net_device
328 * @ptype: the &packet_type structure which was used to register this handler.
329 * @orig_dev: original receive &net_device, in case @dev is a bond.
331 * Returns: 0 for success
333 static int fcoe_fip_recv(struct sk_buff
*skb
, struct net_device
*dev
,
334 struct packet_type
*ptype
,
335 struct net_device
*orig_dev
)
337 struct fcoe_interface
*fcoe
;
339 fcoe
= container_of(ptype
, struct fcoe_interface
, fip_packet_type
);
340 fcoe_ctlr_recv(&fcoe
->ctlr
, skb
);
345 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
346 * @fip: FCoE controller.
349 static void fcoe_fip_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
351 skb
->dev
= fcoe_from_ctlr(fip
)->netdev
;
356 * fcoe_update_src_mac() - Update Ethernet MAC filters.
357 * @fip: FCoE controller.
358 * @old: Unicast MAC address to delete if the MAC is non-zero.
359 * @new: Unicast MAC address to add.
361 * Remove any previously-set unicast MAC filter.
362 * Add secondary FCoE MAC address filter for our OUI.
364 static void fcoe_update_src_mac(struct fcoe_ctlr
*fip
, u8
*old
, u8
*new)
366 struct fcoe_interface
*fcoe
;
368 fcoe
= fcoe_from_ctlr(fip
);
370 if (!is_zero_ether_addr(old
))
371 dev_unicast_delete(fcoe
->netdev
, old
);
372 dev_unicast_add(fcoe
->netdev
, new);
377 * fcoe_lport_config() - sets up the fc_lport
378 * @lp: ptr to the fc_lport
380 * Returns: 0 for success
382 static int fcoe_lport_config(struct fc_lport
*lp
)
386 lp
->max_retry_count
= 3;
387 lp
->max_rport_retry_count
= 3;
388 lp
->e_d_tov
= 2 * 1000; /* FC-FS default */
389 lp
->r_a_tov
= 2 * 2 * 1000;
390 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
391 FCP_SPPF_RETRY
| FCP_SPPF_CONF_COMPL
);
393 fc_lport_init_stats(lp
);
395 /* lport fc_lport related configuration */
398 /* offload related configuration */
409 * fcoe_queue_timer() - fcoe queue timer
410 * @lp: the fc_lport pointer
412 * Calls fcoe_check_wait_queue on timeout
415 static void fcoe_queue_timer(ulong lp
)
417 fcoe_check_wait_queue((struct fc_lport
*)lp
, NULL
);
421 * fcoe_netdev_config() - Set up netdev for SW FCoE
422 * @lp : ptr to the fc_lport
423 * @netdev : ptr to the associated netdevice struct
425 * Must be called after fcoe_lport_config() as it will use lport mutex
427 * Returns : 0 for success
429 static int fcoe_netdev_config(struct fc_lport
*lp
, struct net_device
*netdev
)
433 struct fcoe_interface
*fcoe
;
434 struct fcoe_port
*port
;
436 /* Setup lport private data to point to fcoe softc */
437 port
= lport_priv(lp
);
441 * Determine max frame size based on underlying device and optional
442 * user-configured limit. If the MFS is too low, fcoe_link_ok()
443 * will return 0, so do this first.
445 mfs
= netdev
->mtu
- (sizeof(struct fcoe_hdr
) +
446 sizeof(struct fcoe_crc_eof
));
447 if (fc_set_mfs(lp
, mfs
))
450 /* offload features support */
451 if (netdev
->features
& NETIF_F_SG
)
454 if (netdev
->features
& NETIF_F_FCOE_CRC
) {
456 FCOE_NETDEV_DBG(netdev
, "Supports FCCRC offload\n");
458 if (netdev
->features
& NETIF_F_FSO
) {
460 lp
->lso_max
= netdev
->gso_max_size
;
461 FCOE_NETDEV_DBG(netdev
, "Supports LSO for max len 0x%x\n",
464 if (netdev
->fcoe_ddp_xid
) {
466 lp
->lro_xid
= netdev
->fcoe_ddp_xid
;
467 FCOE_NETDEV_DBG(netdev
, "Supports LRO for max xid 0x%x\n",
470 skb_queue_head_init(&port
->fcoe_pending_queue
);
471 port
->fcoe_pending_queue_active
= 0;
472 setup_timer(&port
->timer
, fcoe_queue_timer
, (unsigned long)lp
);
474 wwnn
= fcoe_wwn_from_mac(netdev
->dev_addr
, 1, 0);
475 fc_set_wwnn(lp
, wwnn
);
476 /* XXX - 3rd arg needs to be vlan id */
477 wwpn
= fcoe_wwn_from_mac(netdev
->dev_addr
, 2, 0);
478 fc_set_wwpn(lp
, wwpn
);
484 * fcoe_shost_config() - Sets up fc_lport->host
485 * @lp : ptr to the fc_lport
486 * @shost : ptr to the associated scsi host
487 * @dev : device associated to scsi host
489 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
491 * Returns : 0 for success
493 static int fcoe_shost_config(struct fc_lport
*lp
, struct Scsi_Host
*shost
,
498 /* lport scsi host config */
501 lp
->host
->max_lun
= FCOE_MAX_LUN
;
502 lp
->host
->max_id
= FCOE_MAX_FCP_TARGET
;
503 lp
->host
->max_channel
= 0;
504 lp
->host
->transportt
= scsi_transport_fcoe_sw
;
506 /* add the new host to the SCSI-ml */
507 rc
= scsi_add_host(lp
->host
, dev
);
509 FCOE_NETDEV_DBG(fcoe_netdev(lp
), "fcoe_shost_config: "
510 "error on scsi_add_host\n");
513 sprintf(fc_host_symbolic_name(lp
->host
), "%s v%s over %s",
514 FCOE_NAME
, FCOE_VERSION
,
515 fcoe_netdev(lp
)->name
);
521 * fcoe_oem_match() - match for read types IO
522 * @fp: the fc_frame for new IO.
524 * Returns : true for read types IO, otherwise returns false.
526 bool fcoe_oem_match(struct fc_frame
*fp
)
528 return fc_fcp_is_read(fr_fsp(fp
)) &&
529 (fr_fsp(fp
)->data_len
> fcoe_ddp_min
);
533 * fcoe_em_config() - allocates em for this lport
534 * @lp: the fcoe that em is to allocated for
536 * Returns : 0 on success
538 static inline int fcoe_em_config(struct fc_lport
*lp
)
540 struct fcoe_port
*port
= lport_priv(lp
);
541 struct fcoe_interface
*fcoe
= port
->fcoe
;
542 struct fcoe_interface
*oldfcoe
= NULL
;
543 struct net_device
*old_real_dev
, *cur_real_dev
;
544 u16 min_xid
= FCOE_MIN_XID
;
545 u16 max_xid
= FCOE_MAX_XID
;
548 * Check if need to allocate an em instance for
549 * offload exchange ids to be shared across all VN_PORTs/lport.
551 if (!lp
->lro_enabled
|| !lp
->lro_xid
|| (lp
->lro_xid
>= max_xid
)) {
557 * Reuse existing offload em instance in case
558 * it is already allocated on real eth device
560 if (fcoe
->netdev
->priv_flags
& IFF_802_1Q_VLAN
)
561 cur_real_dev
= vlan_dev_real_dev(fcoe
->netdev
);
563 cur_real_dev
= fcoe
->netdev
;
565 list_for_each_entry(oldfcoe
, &fcoe_hostlist
, list
) {
566 if (oldfcoe
->netdev
->priv_flags
& IFF_802_1Q_VLAN
)
567 old_real_dev
= vlan_dev_real_dev(oldfcoe
->netdev
);
569 old_real_dev
= oldfcoe
->netdev
;
571 if (cur_real_dev
== old_real_dev
) {
572 fcoe
->oem
= oldfcoe
->oem
;
578 if (!fc_exch_mgr_add(lp
, fcoe
->oem
, fcoe_oem_match
)) {
579 printk(KERN_ERR
"fcoe_em_config: failed to add "
580 "offload em:%p on interface:%s\n",
581 fcoe
->oem
, fcoe
->netdev
->name
);
585 fcoe
->oem
= fc_exch_mgr_alloc(lp
, FC_CLASS_3
,
586 FCOE_MIN_XID
, lp
->lro_xid
,
589 printk(KERN_ERR
"fcoe_em_config: failed to allocate "
590 "em for offload exches on interface:%s\n",
597 * Exclude offload EM xid range from next EM xid range.
599 min_xid
+= lp
->lro_xid
+ 1;
602 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, min_xid
, max_xid
, NULL
)) {
603 printk(KERN_ERR
"fcoe_em_config: failed to "
604 "allocate em on interface %s\n", fcoe
->netdev
->name
);
612 * fcoe_if_destroy() - FCoE software HBA tear-down function
613 * @lport: fc_lport to destroy
615 static void fcoe_if_destroy(struct fc_lport
*lport
)
617 struct fcoe_port
*port
= lport_priv(lport
);
618 struct fcoe_interface
*fcoe
= port
->fcoe
;
619 struct net_device
*netdev
= fcoe
->netdev
;
621 FCOE_NETDEV_DBG(netdev
, "Destroying interface\n");
623 /* Logout of the fabric */
624 fc_fabric_logoff(lport
);
626 /* Cleanup the fc_lport */
627 fc_lport_destroy(lport
);
628 fc_fcp_destroy(lport
);
630 /* Stop the transmit retry timer */
631 del_timer_sync(&port
->timer
);
633 /* Free existing transmit skbs */
634 fcoe_clean_pending_queue(lport
);
636 /* receives may not be stopped until after this */
637 fcoe_interface_put(fcoe
);
639 /* Free queued packets for the per-CPU receive threads */
640 fcoe_percpu_clean(lport
);
642 /* Detach from the scsi-ml */
643 fc_remove_host(lport
->host
);
644 scsi_remove_host(lport
->host
);
646 /* There are no more rports or I/O, free the EM */
647 fc_exch_mgr_free(lport
);
649 /* Free memory used by statistical counters */
650 fc_lport_free_stats(lport
);
652 /* Release the Scsi_Host */
653 scsi_host_put(lport
->host
);
657 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
658 * @lp: the corresponding fc_lport
659 * @xid: the exchange id for this ddp transfer
660 * @sgl: the scatterlist describing this transfer
661 * @sgc: number of sg items
665 static int fcoe_ddp_setup(struct fc_lport
*lp
, u16 xid
,
666 struct scatterlist
*sgl
, unsigned int sgc
)
668 struct net_device
*n
= fcoe_netdev(lp
);
670 if (n
->netdev_ops
->ndo_fcoe_ddp_setup
)
671 return n
->netdev_ops
->ndo_fcoe_ddp_setup(n
, xid
, sgl
, sgc
);
677 * fcoe_ddp_done - calls LLD's ddp_done through net_device
678 * @lp: the corresponding fc_lport
679 * @xid: the exchange id for this ddp transfer
681 * Returns : the length of data that have been completed by ddp
683 static int fcoe_ddp_done(struct fc_lport
*lp
, u16 xid
)
685 struct net_device
*n
= fcoe_netdev(lp
);
687 if (n
->netdev_ops
->ndo_fcoe_ddp_done
)
688 return n
->netdev_ops
->ndo_fcoe_ddp_done(n
, xid
);
692 static struct libfc_function_template fcoe_libfc_fcn_templ
= {
693 .frame_send
= fcoe_xmit
,
694 .ddp_setup
= fcoe_ddp_setup
,
695 .ddp_done
= fcoe_ddp_done
,
699 * fcoe_if_create() - this function creates the fcoe port
700 * @fcoe: fcoe_interface structure to create an fc_lport instance on
701 * @parent: device pointer to be the parent in sysfs for the SCSI host
703 * Creates fc_lport struct and scsi_host for lport, configures lport.
705 * Returns : The allocated fc_lport or an error pointer
707 static struct fc_lport
*fcoe_if_create(struct fcoe_interface
*fcoe
,
708 struct device
*parent
)
711 struct fc_lport
*lport
= NULL
;
712 struct fcoe_port
*port
;
713 struct Scsi_Host
*shost
;
714 struct net_device
*netdev
= fcoe
->netdev
;
716 FCOE_NETDEV_DBG(netdev
, "Create Interface\n");
718 shost
= libfc_host_alloc(&fcoe_shost_template
,
719 sizeof(struct fcoe_port
));
721 FCOE_NETDEV_DBG(netdev
, "Could not allocate host structure\n");
725 lport
= shost_priv(shost
);
726 port
= lport_priv(lport
);
729 INIT_WORK(&port
->destroy_work
, fcoe_destroy_work
);
731 /* configure fc_lport, e.g., em */
732 rc
= fcoe_lport_config(lport
);
734 FCOE_NETDEV_DBG(netdev
, "Could not configure lport for the "
739 /* configure lport network properties */
740 rc
= fcoe_netdev_config(lport
, netdev
);
742 FCOE_NETDEV_DBG(netdev
, "Could not configure netdev for the "
747 /* configure lport scsi host properties */
748 rc
= fcoe_shost_config(lport
, shost
, parent
);
750 FCOE_NETDEV_DBG(netdev
, "Could not configure shost for the "
755 /* Initialize the library */
756 rc
= fcoe_libfc_config(lport
, &fcoe_libfc_fcn_templ
);
758 FCOE_NETDEV_DBG(netdev
, "Could not configure libfc for the "
764 * fcoe_em_alloc() and fcoe_hostlist_add() both
765 * need to be atomic with respect to other changes to the hostlist
766 * since fcoe_em_alloc() looks for an existing EM
767 * instance on host list updated by fcoe_hostlist_add().
769 * This is currently handled through the fcoe_config_mutex begin held.
772 /* lport exch manager allocation */
773 rc
= fcoe_em_config(lport
);
775 FCOE_NETDEV_DBG(netdev
, "Could not configure the EM for the "
780 fcoe_interface_get(fcoe
);
784 fc_exch_mgr_free(lport
);
786 scsi_host_put(lport
->host
);
792 * fcoe_if_init() - attach to scsi transport
794 * Returns : 0 on success
796 static int __init
fcoe_if_init(void)
798 /* attach to scsi transport */
799 scsi_transport_fcoe_sw
=
800 fc_attach_transport(&fcoe_transport_function
);
802 if (!scsi_transport_fcoe_sw
) {
803 printk(KERN_ERR
"fcoe: Failed to attach to the FC transport\n");
811 * fcoe_if_exit() - detach from scsi transport
813 * Returns : 0 on success
815 int __exit
fcoe_if_exit(void)
817 fc_release_transport(scsi_transport_fcoe_sw
);
818 scsi_transport_fcoe_sw
= NULL
;
823 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
824 * @cpu: cpu index for the online cpu
826 static void fcoe_percpu_thread_create(unsigned int cpu
)
828 struct fcoe_percpu_s
*p
;
829 struct task_struct
*thread
;
831 p
= &per_cpu(fcoe_percpu
, cpu
);
833 thread
= kthread_create(fcoe_percpu_receive_thread
,
834 (void *)p
, "fcoethread/%d", cpu
);
836 if (likely(!IS_ERR(thread
))) {
837 kthread_bind(thread
, cpu
);
838 wake_up_process(thread
);
840 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
842 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
847 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
848 * @cpu: cpu index the rx thread is to be removed
850 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
851 * current CPU's Rx thread. If the thread being destroyed is bound to
852 * the CPU processing this context the skbs will be freed.
854 static void fcoe_percpu_thread_destroy(unsigned int cpu
)
856 struct fcoe_percpu_s
*p
;
857 struct task_struct
*thread
;
858 struct page
*crc_eof
;
861 struct fcoe_percpu_s
*p0
;
862 unsigned targ_cpu
= smp_processor_id();
863 #endif /* CONFIG_SMP */
865 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu
);
867 /* Prevent any new skbs from being queued for this CPU. */
868 p
= &per_cpu(fcoe_percpu
, cpu
);
869 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
872 crc_eof
= p
->crc_eof_page
;
873 p
->crc_eof_page
= NULL
;
874 p
->crc_eof_offset
= 0;
875 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
879 * Don't bother moving the skb's if this context is running
880 * on the same CPU that is having its thread destroyed. This
881 * can easily happen when the module is removed.
883 if (cpu
!= targ_cpu
) {
884 p0
= &per_cpu(fcoe_percpu
, targ_cpu
);
885 spin_lock_bh(&p0
->fcoe_rx_list
.lock
);
887 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
890 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
891 __skb_queue_tail(&p0
->fcoe_rx_list
, skb
);
892 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
895 * The targeted CPU is not initialized and cannot accept
896 * new skbs. Unlock the targeted CPU and drop the skbs
897 * on the CPU that is going offline.
899 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
901 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
905 * This scenario occurs when the module is being removed
906 * and all threads are being destroyed. skbs will continue
907 * to be shifted from the CPU thread that is being removed
908 * to the CPU thread associated with the CPU that is processing
909 * the module removal. Once there is only one CPU Rx thread it
910 * will reach this case and we will drop all skbs and later
913 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
914 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
916 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
920 * This a non-SMP scenario where the singular Rx thread is
921 * being removed. Free all skbs and stop the thread.
923 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
924 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
926 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
930 kthread_stop(thread
);
937 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
938 * @nfb: callback data block
939 * @action: event triggering the callback
940 * @hcpu: index for the cpu of this event
942 * This creates or destroys per cpu data for fcoe
944 * Returns NOTIFY_OK always.
946 static int fcoe_cpu_callback(struct notifier_block
*nfb
,
947 unsigned long action
, void *hcpu
)
949 unsigned cpu
= (unsigned long)hcpu
;
953 case CPU_ONLINE_FROZEN
:
954 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu
);
955 fcoe_percpu_thread_create(cpu
);
958 case CPU_DEAD_FROZEN
:
959 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu
);
960 fcoe_percpu_thread_destroy(cpu
);
968 static struct notifier_block fcoe_cpu_notifier
= {
969 .notifier_call
= fcoe_cpu_callback
,
973 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
974 * @skb: the receive skb
975 * @dev: associated net device
977 * @olddev: last device
979 * this function will receive the packet and build fc frame and pass it up
981 * Returns: 0 for success
983 int fcoe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
984 struct packet_type
*ptype
, struct net_device
*olddev
)
987 struct fcoe_rcv_info
*fr
;
988 struct fcoe_interface
*fcoe
;
989 struct fc_frame_header
*fh
;
990 struct fcoe_percpu_s
*fps
;
993 fcoe
= container_of(ptype
, struct fcoe_interface
, fcoe_packet_type
);
995 if (unlikely(lp
== NULL
)) {
996 FCOE_NETDEV_DBG(dev
, "Cannot find hba structure");
1002 FCOE_NETDEV_DBG(dev
, "skb_info: len:%d data_len:%d head:%p "
1003 "data:%p tail:%p end:%p sum:%d dev:%s",
1004 skb
->len
, skb
->data_len
, skb
->head
, skb
->data
,
1005 skb_tail_pointer(skb
), skb_end_pointer(skb
),
1006 skb
->csum
, skb
->dev
? skb
->dev
->name
: "<NULL>");
1008 /* check for FCOE packet type */
1009 if (unlikely(eth_hdr(skb
)->h_proto
!= htons(ETH_P_FCOE
))) {
1010 FCOE_NETDEV_DBG(dev
, "Wrong FC type frame");
1015 * Check for minimum frame length, and make sure required FCoE
1016 * and FC headers are pulled into the linear data area.
1018 if (unlikely((skb
->len
< FCOE_MIN_FRAME
) ||
1019 !pskb_may_pull(skb
, FCOE_HEADER_LEN
)))
1022 skb_set_transport_header(skb
, sizeof(struct fcoe_hdr
));
1023 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1025 fr
= fcoe_dev_from_skb(skb
);
1030 * In case the incoming frame's exchange is originated from
1031 * the initiator, then received frame's exchange id is ANDed
1032 * with fc_cpu_mask bits to get the same cpu on which exchange
1033 * was originated, otherwise just use the current cpu.
1035 if (ntoh24(fh
->fh_f_ctl
) & FC_FC_EX_CTX
)
1036 cpu
= ntohs(fh
->fh_ox_id
) & fc_cpu_mask
;
1038 cpu
= smp_processor_id();
1040 fps
= &per_cpu(fcoe_percpu
, cpu
);
1041 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
1042 if (unlikely(!fps
->thread
)) {
1044 * The targeted CPU is not ready, let's target
1045 * the first CPU now. For non-SMP systems this
1046 * will check the same CPU twice.
1048 FCOE_NETDEV_DBG(dev
, "CPU is online, but no receive thread "
1049 "ready for incoming skb- using first online "
1052 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
1053 cpu
= first_cpu(cpu_online_map
);
1054 fps
= &per_cpu(fcoe_percpu
, cpu
);
1055 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
1057 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
1063 * We now have a valid CPU that we're targeting for
1064 * this skb. We also have this receive thread locked,
1065 * so we're free to queue skbs into it's queue.
1067 __skb_queue_tail(&fps
->fcoe_rx_list
, skb
);
1068 if (fps
->fcoe_rx_list
.qlen
== 1)
1069 wake_up_process(fps
->thread
);
1071 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
1075 fc_lport_get_stats(lp
)->ErrorFrames
++;
1083 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1084 * @skb: the skb to be xmitted
1086 * Returns: 0 for success
1088 static inline int fcoe_start_io(struct sk_buff
*skb
)
1093 rc
= dev_queue_xmit(skb
);
1101 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1102 * @skb: the skb to be xmitted
1105 * Returns: 0 for success
1107 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
1109 struct fcoe_percpu_s
*fps
;
1112 fps
= &get_cpu_var(fcoe_percpu
);
1113 page
= fps
->crc_eof_page
;
1115 page
= alloc_page(GFP_ATOMIC
);
1117 put_cpu_var(fcoe_percpu
);
1120 fps
->crc_eof_page
= page
;
1121 fps
->crc_eof_offset
= 0;
1125 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
1126 fps
->crc_eof_offset
, tlen
);
1128 skb
->data_len
+= tlen
;
1129 skb
->truesize
+= tlen
;
1130 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
1132 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
1133 fps
->crc_eof_page
= NULL
;
1134 fps
->crc_eof_offset
= 0;
1137 put_cpu_var(fcoe_percpu
);
1142 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1143 * @fp: the fc_frame containing data to be checksummed
1145 * This uses crc32() to calculate the crc for port frame
1146 * Return : 32 bit crc
1148 u32
fcoe_fc_crc(struct fc_frame
*fp
)
1150 struct sk_buff
*skb
= fp_skb(fp
);
1151 struct skb_frag_struct
*frag
;
1152 unsigned char *data
;
1153 unsigned long off
, len
, clen
;
1157 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
1159 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1160 frag
= &skb_shinfo(skb
)->frags
[i
];
1161 off
= frag
->page_offset
;
1164 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
1165 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
1166 KM_SKB_DATA_SOFTIRQ
);
1167 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
1168 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
1177 * fcoe_xmit() - FCoE frame transmit function
1178 * @lp: the associated local fcoe
1179 * @fp: the fc_frame to be transmitted
1181 * Return : 0 for success
1183 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
1188 struct fcoe_crc_eof
*cp
;
1189 struct sk_buff
*skb
;
1190 struct fcoe_dev_stats
*stats
;
1191 struct fc_frame_header
*fh
;
1192 unsigned int hlen
; /* header length implies the version */
1193 unsigned int tlen
; /* trailer length */
1194 unsigned int elen
; /* eth header, may include vlan */
1195 struct fcoe_port
*port
= lport_priv(lp
);
1196 struct fcoe_interface
*fcoe
= port
->fcoe
;
1198 struct fcoe_hdr
*hp
;
1200 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
1202 fh
= fc_frame_header_get(fp
);
1204 wlen
= skb
->len
/ FCOE_WORD_TO_BYTE
;
1211 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
) &&
1212 fcoe_ctlr_els_send(&fcoe
->ctlr
, skb
))
1218 elen
= sizeof(struct ethhdr
);
1219 hlen
= sizeof(struct fcoe_hdr
);
1220 tlen
= sizeof(struct fcoe_crc_eof
);
1221 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
1224 if (likely(lp
->crc_offload
)) {
1225 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1226 skb
->csum_start
= skb_headroom(skb
);
1227 skb
->csum_offset
= skb
->len
;
1230 skb
->ip_summed
= CHECKSUM_NONE
;
1231 crc
= fcoe_fc_crc(fp
);
1234 /* copy port crc and eof to the skb buff */
1235 if (skb_is_nonlinear(skb
)) {
1237 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
1241 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
1242 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
1243 + frag
->page_offset
;
1245 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
1248 memset(cp
, 0, sizeof(*cp
));
1250 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
1252 if (skb_is_nonlinear(skb
)) {
1253 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
1257 /* adjust skb network/transport offsets to match mac/fcoe/port */
1258 skb_push(skb
, elen
+ hlen
);
1259 skb_reset_mac_header(skb
);
1260 skb_reset_network_header(skb
);
1261 skb
->mac_len
= elen
;
1262 skb
->protocol
= htons(ETH_P_FCOE
);
1263 skb
->dev
= fcoe
->netdev
;
1265 /* fill up mac and fcoe headers */
1267 eh
->h_proto
= htons(ETH_P_FCOE
);
1268 if (fcoe
->ctlr
.map_dest
)
1269 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
1271 /* insert GW address */
1272 memcpy(eh
->h_dest
, fcoe
->ctlr
.dest_addr
, ETH_ALEN
);
1274 if (unlikely(fcoe
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
))
1275 memcpy(eh
->h_source
, fcoe
->ctlr
.ctl_src_addr
, ETH_ALEN
);
1277 memcpy(eh
->h_source
, fcoe
->ctlr
.data_src_addr
, ETH_ALEN
);
1279 hp
= (struct fcoe_hdr
*)(eh
+ 1);
1280 memset(hp
, 0, sizeof(*hp
));
1282 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
1285 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1286 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
1287 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
1288 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
1290 skb_shinfo(skb
)->gso_type
= 0;
1291 skb_shinfo(skb
)->gso_size
= 0;
1293 /* update tx stats: regardless if LLD fails */
1294 stats
= fc_lport_get_stats(lp
);
1296 stats
->TxWords
+= wlen
;
1298 /* send down to lld */
1300 if (port
->fcoe_pending_queue
.qlen
)
1301 fcoe_check_wait_queue(lp
, skb
);
1302 else if (fcoe_start_io(skb
))
1303 fcoe_check_wait_queue(lp
, skb
);
1309 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1310 * @skb: the skb being completed.
1312 static void fcoe_percpu_flush_done(struct sk_buff
*skb
)
1314 complete(&fcoe_flush_completion
);
1318 * fcoe_percpu_receive_thread() - recv thread per cpu
1319 * @arg: ptr to the fcoe per cpu struct
1321 * Return: 0 for success
1323 int fcoe_percpu_receive_thread(void *arg
)
1325 struct fcoe_percpu_s
*p
= arg
;
1327 struct fc_lport
*lp
;
1328 struct fcoe_rcv_info
*fr
;
1329 struct fcoe_dev_stats
*stats
;
1330 struct fc_frame_header
*fh
;
1331 struct sk_buff
*skb
;
1332 struct fcoe_crc_eof crc_eof
;
1333 struct fc_frame
*fp
;
1335 struct fcoe_port
*port
;
1336 struct fcoe_hdr
*hp
;
1338 set_user_nice(current
, -20);
1340 while (!kthread_should_stop()) {
1342 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1343 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
1344 set_current_state(TASK_INTERRUPTIBLE
);
1345 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1347 set_current_state(TASK_RUNNING
);
1348 if (kthread_should_stop())
1350 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
1352 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
1353 fr
= fcoe_dev_from_skb(skb
);
1355 if (unlikely(lp
== NULL
)) {
1356 if (skb
->destructor
!= fcoe_percpu_flush_done
)
1357 FCOE_NETDEV_DBG(skb
->dev
, "NULL lport in skb");
1362 FCOE_NETDEV_DBG(skb
->dev
, "skb_info: len:%d data_len:%d "
1363 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1364 skb
->len
, skb
->data_len
,
1365 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
1366 skb_end_pointer(skb
), skb
->csum
,
1367 skb
->dev
? skb
->dev
->name
: "<NULL>");
1370 * Save source MAC address before discarding header.
1372 port
= lport_priv(lp
);
1373 if (skb_is_nonlinear(skb
))
1374 skb_linearize(skb
); /* not ideal */
1375 mac
= eth_hdr(skb
)->h_source
;
1378 * Frame length checks and setting up the header pointers
1379 * was done in fcoe_rcv already.
1381 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
1382 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
1384 stats
= fc_lport_get_stats(lp
);
1385 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
1386 if (stats
->ErrorFrames
< 5)
1387 printk(KERN_WARNING
"fcoe: FCoE version "
1388 "mismatch: The frame has "
1389 "version %x, but the "
1390 "initiator supports version "
1391 "%x\n", FC_FCOE_DECAPS_VER(hp
),
1393 stats
->ErrorFrames
++;
1398 skb_pull(skb
, sizeof(struct fcoe_hdr
));
1399 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
1402 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
1404 fp
= (struct fc_frame
*)skb
;
1407 fr_sof(fp
) = hp
->fcoe_sof
;
1409 /* Copy out the CRC and EOF trailer for access */
1410 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
1414 fr_eof(fp
) = crc_eof
.fcoe_eof
;
1415 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
1416 if (pskb_trim(skb
, fr_len
)) {
1422 * We only check CRC if no offload is available and if it is
1423 * it's solicited data, in which case, the FCP layer would
1424 * check it during the copy.
1426 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
1427 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1429 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
1431 fh
= fc_frame_header_get(fp
);
1432 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
1433 fh
->fh_type
== FC_TYPE_FCP
) {
1434 fc_exch_recv(lp
, fp
);
1437 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
1438 if (le32_to_cpu(fr_crc(fp
)) !=
1439 ~crc32(~0, skb
->data
, fr_len
)) {
1440 if (stats
->InvalidCRCCount
< 5)
1441 printk(KERN_WARNING
"fcoe: dropping "
1442 "frame with CRC error\n");
1443 stats
->InvalidCRCCount
++;
1444 stats
->ErrorFrames
++;
1448 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
1450 if (unlikely(port
->fcoe
->ctlr
.flogi_oxid
!= FC_XID_UNKNOWN
) &&
1451 fcoe_ctlr_recv_flogi(&port
->fcoe
->ctlr
, fp
, mac
)) {
1455 fc_exch_recv(lp
, fp
);
1461 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1464 * This empties the wait_queue, dequeue the head of the wait_queue queue
1465 * and calls fcoe_start_io() for each packet, if all skb have been
1466 * transmitted, return qlen or -1 if a error occurs, then restore
1467 * wait_queue and try again later.
1469 * The wait_queue is used when the skb transmit fails. skb will go
1470 * in the wait_queue which will be emptied by the timer function or
1471 * by the next skb transmit.
1473 static void fcoe_check_wait_queue(struct fc_lport
*lp
, struct sk_buff
*skb
)
1475 struct fcoe_port
*port
= lport_priv(lp
);
1478 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1481 __skb_queue_tail(&port
->fcoe_pending_queue
, skb
);
1483 if (port
->fcoe_pending_queue_active
)
1485 port
->fcoe_pending_queue_active
= 1;
1487 while (port
->fcoe_pending_queue
.qlen
) {
1488 /* keep qlen > 0 until fcoe_start_io succeeds */
1489 port
->fcoe_pending_queue
.qlen
++;
1490 skb
= __skb_dequeue(&port
->fcoe_pending_queue
);
1492 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1493 rc
= fcoe_start_io(skb
);
1494 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1497 __skb_queue_head(&port
->fcoe_pending_queue
, skb
);
1498 /* undo temporary increment above */
1499 port
->fcoe_pending_queue
.qlen
--;
1502 /* undo temporary increment above */
1503 port
->fcoe_pending_queue
.qlen
--;
1506 if (port
->fcoe_pending_queue
.qlen
< FCOE_LOW_QUEUE_DEPTH
)
1508 if (port
->fcoe_pending_queue
.qlen
&& !timer_pending(&port
->timer
))
1509 mod_timer(&port
->timer
, jiffies
+ 2);
1510 port
->fcoe_pending_queue_active
= 0;
1512 if (port
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
1514 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1519 * fcoe_dev_setup() - setup link change notification interface
1521 static void fcoe_dev_setup(void)
1523 register_netdevice_notifier(&fcoe_notifier
);
1527 * fcoe_dev_cleanup() - cleanup link change notification interface
1529 static void fcoe_dev_cleanup(void)
1531 unregister_netdevice_notifier(&fcoe_notifier
);
1535 * fcoe_device_notification() - netdev event notification callback
1536 * @notifier: context of the notification
1537 * @event: type of event
1538 * @ptr: fixed array for output parsed ifname
1540 * This function is called by the ethernet driver in case of link change event
1542 * Returns: 0 for success
1544 static int fcoe_device_notification(struct notifier_block
*notifier
,
1545 ulong event
, void *ptr
)
1547 struct fc_lport
*lp
= NULL
;
1548 struct net_device
*netdev
= ptr
;
1549 struct fcoe_interface
*fcoe
;
1550 struct fcoe_port
*port
;
1551 struct fcoe_dev_stats
*stats
;
1552 u32 link_possible
= 1;
1556 list_for_each_entry(fcoe
, &fcoe_hostlist
, list
) {
1557 if (fcoe
->netdev
== netdev
) {
1569 case NETDEV_GOING_DOWN
:
1575 case NETDEV_CHANGEMTU
:
1576 mfs
= netdev
->mtu
- (sizeof(struct fcoe_hdr
) +
1577 sizeof(struct fcoe_crc_eof
));
1578 if (mfs
>= FC_MIN_MAX_FRAME
)
1579 fc_set_mfs(lp
, mfs
);
1581 case NETDEV_REGISTER
:
1583 case NETDEV_UNREGISTER
:
1584 list_del(&fcoe
->list
);
1585 port
= lport_priv(fcoe
->ctlr
.lp
);
1586 fcoe_interface_cleanup(fcoe
);
1587 schedule_work(&port
->destroy_work
);
1591 FCOE_NETDEV_DBG(netdev
, "Unknown event %ld "
1592 "from netdev netlink\n", event
);
1594 if (link_possible
&& !fcoe_link_ok(lp
))
1595 fcoe_ctlr_link_up(&fcoe
->ctlr
);
1596 else if (fcoe_ctlr_link_down(&fcoe
->ctlr
)) {
1597 stats
= fc_lport_get_stats(lp
);
1598 stats
->LinkFailureCount
++;
1599 fcoe_clean_pending_queue(lp
);
1606 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1607 * @buffer: incoming buffer to be copied
1609 * Returns: NULL or ptr to net_device
1611 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
1614 char ifname
[IFNAMSIZ
+ 2];
1617 strlcpy(ifname
, buffer
, IFNAMSIZ
);
1618 cp
= ifname
+ strlen(ifname
);
1619 while (--cp
>= ifname
&& *cp
== '\n')
1621 return dev_get_by_name(&init_net
, ifname
);
1627 * fcoe_destroy() - handles the destroy from sysfs
1628 * @buffer: expected to be an eth if name
1629 * @kp: associated kernel param
1631 * Returns: 0 for success
1633 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1635 struct fcoe_interface
*fcoe
;
1636 struct net_device
*netdev
;
1639 mutex_lock(&fcoe_config_mutex
);
1640 #ifdef CONFIG_FCOE_MODULE
1642 * Make sure the module has been initialized, and is not about to be
1643 * removed. Module paramter sysfs files are writable before the
1644 * module_init function is called and after module_exit.
1646 if (THIS_MODULE
->state
!= MODULE_STATE_LIVE
) {
1652 netdev
= fcoe_if_to_netdev(buffer
);
1659 fcoe
= fcoe_hostlist_lookup_port(netdev
);
1665 list_del(&fcoe
->list
);
1666 fcoe_interface_cleanup(fcoe
);
1668 fcoe_if_destroy(fcoe
->ctlr
.lp
);
1672 mutex_unlock(&fcoe_config_mutex
);
1676 static void fcoe_destroy_work(struct work_struct
*work
)
1678 struct fcoe_port
*port
;
1680 port
= container_of(work
, struct fcoe_port
, destroy_work
);
1681 mutex_lock(&fcoe_config_mutex
);
1682 fcoe_if_destroy(port
->lport
);
1683 mutex_unlock(&fcoe_config_mutex
);
1687 * fcoe_create() - Handles the create call from sysfs
1688 * @buffer: expected to be an eth if name
1689 * @kp: associated kernel param
1691 * Returns: 0 for success
1693 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1696 struct fcoe_interface
*fcoe
;
1697 struct fc_lport
*lport
;
1698 struct net_device
*netdev
;
1700 mutex_lock(&fcoe_config_mutex
);
1701 #ifdef CONFIG_FCOE_MODULE
1703 * Make sure the module has been initialized, and is not about to be
1704 * removed. Module paramter sysfs files are writable before the
1705 * module_init function is called and after module_exit.
1707 if (THIS_MODULE
->state
!= MODULE_STATE_LIVE
) {
1714 netdev
= fcoe_if_to_netdev(buffer
);
1720 /* look for existing lport */
1721 if (fcoe_hostlist_lookup(netdev
)) {
1726 fcoe
= fcoe_interface_create(netdev
);
1732 lport
= fcoe_if_create(fcoe
, &netdev
->dev
);
1733 if (IS_ERR(lport
)) {
1734 printk(KERN_ERR
"fcoe: Failed to create interface (%s)\n",
1737 fcoe_interface_cleanup(fcoe
);
1741 /* Make this the "master" N_Port */
1742 fcoe
->ctlr
.lp
= lport
;
1744 /* add to lports list */
1745 fcoe_hostlist_add(lport
);
1747 /* start FIP Discovery and FLOGI */
1748 lport
->boot_time
= jiffies
;
1749 fc_fabric_login(lport
);
1750 if (!fcoe_link_ok(lport
))
1751 fcoe_ctlr_link_up(&fcoe
->ctlr
);
1756 * Release from init in fcoe_interface_create(), on success lport
1757 * should be holding a reference taken in fcoe_if_create().
1759 fcoe_interface_put(fcoe
);
1764 mutex_unlock(&fcoe_config_mutex
);
1768 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1769 __MODULE_PARM_TYPE(create
, "string");
1770 MODULE_PARM_DESC(create
, "Create fcoe fcoe using net device passed in.");
1771 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1772 __MODULE_PARM_TYPE(destroy
, "string");
1773 MODULE_PARM_DESC(destroy
, "Destroy fcoe fcoe");
1776 * fcoe_link_ok() - Check if link is ok for the fc_lport
1777 * @lp: ptr to the fc_lport
1779 * Any permanently-disqualifying conditions have been previously checked.
1780 * This also updates the speed setting, which may change with link for 100/1000.
1782 * This function should probably be checking for PAUSE support at some point
1783 * in the future. Currently Per-priority-pause is not determinable using
1784 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1786 * Returns: 0 if link is OK for use by FCoE.
1789 int fcoe_link_ok(struct fc_lport
*lp
)
1791 struct fcoe_port
*port
= lport_priv(lp
);
1792 struct net_device
*dev
= port
->fcoe
->netdev
;
1793 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1795 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
) &&
1796 (!dev_ethtool_get_settings(dev
, &ecmd
))) {
1797 lp
->link_supported_speeds
&=
1798 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1799 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1800 SUPPORTED_1000baseT_Full
))
1801 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1802 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1803 lp
->link_supported_speeds
|=
1804 FC_PORTSPEED_10GBIT
;
1805 if (ecmd
.speed
== SPEED_1000
)
1806 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1807 if (ecmd
.speed
== SPEED_10000
)
1808 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1816 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1819 * Must be called with fcoe_create_mutex held to single-thread completion.
1821 * This flushes the pending skbs by adding a new skb to each queue and
1822 * waiting until they are all freed. This assures us that not only are
1823 * there no packets that will be handled by the lport, but also that any
1824 * threads already handling packet have returned.
1826 void fcoe_percpu_clean(struct fc_lport
*lp
)
1828 struct fcoe_percpu_s
*pp
;
1829 struct fcoe_rcv_info
*fr
;
1830 struct sk_buff_head
*list
;
1831 struct sk_buff
*skb
, *next
;
1832 struct sk_buff
*head
;
1835 for_each_possible_cpu(cpu
) {
1836 pp
= &per_cpu(fcoe_percpu
, cpu
);
1837 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1838 list
= &pp
->fcoe_rx_list
;
1840 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1843 fr
= fcoe_dev_from_skb(skb
);
1844 if (fr
->fr_dev
== lp
) {
1845 __skb_unlink(skb
, list
);
1850 if (!pp
->thread
|| !cpu_online(cpu
)) {
1851 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1855 skb
= dev_alloc_skb(0);
1857 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1860 skb
->destructor
= fcoe_percpu_flush_done
;
1862 __skb_queue_tail(&pp
->fcoe_rx_list
, skb
);
1863 if (pp
->fcoe_rx_list
.qlen
== 1)
1864 wake_up_process(pp
->thread
);
1865 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1867 wait_for_completion(&fcoe_flush_completion
);
1872 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1873 * @lp: the corresponding fc_lport
1877 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1879 struct fcoe_port
*port
= lport_priv(lp
);
1880 struct sk_buff
*skb
;
1882 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1883 while ((skb
= __skb_dequeue(&port
->fcoe_pending_queue
)) != NULL
) {
1884 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1886 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
1888 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
1892 * fcoe_reset() - Resets the fcoe
1893 * @shost: shost the reset is from
1897 int fcoe_reset(struct Scsi_Host
*shost
)
1899 struct fc_lport
*lport
= shost_priv(shost
);
1900 fc_lport_reset(lport
);
1905 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1906 * @dev: this is currently ptr to net_device
1908 * Returns: NULL or the located fcoe_port
1909 * Locking: must be called with the RNL mutex held
1911 static struct fcoe_interface
*
1912 fcoe_hostlist_lookup_port(const struct net_device
*dev
)
1914 struct fcoe_interface
*fcoe
;
1916 list_for_each_entry(fcoe
, &fcoe_hostlist
, list
) {
1917 if (fcoe
->netdev
== dev
)
1924 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1925 * @netdev: ptr to net_device
1927 * Returns: 0 for success
1928 * Locking: must be called with the RTNL mutex held
1930 static struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1932 struct fcoe_interface
*fcoe
;
1934 fcoe
= fcoe_hostlist_lookup_port(netdev
);
1935 return (fcoe
) ? fcoe
->ctlr
.lp
: NULL
;
1939 * fcoe_hostlist_add() - Add a lport to lports list
1940 * @lp: ptr to the fc_lport to be added
1942 * Returns: 0 for success
1943 * Locking: must be called with the RTNL mutex held
1945 static int fcoe_hostlist_add(const struct fc_lport
*lport
)
1947 struct fcoe_interface
*fcoe
;
1948 struct fcoe_port
*port
;
1950 fcoe
= fcoe_hostlist_lookup_port(fcoe_netdev(lport
));
1952 port
= lport_priv(lport
);
1954 list_add_tail(&fcoe
->list
, &fcoe_hostlist
);
1960 * fcoe_init() - fcoe module loading initialization
1962 * Returns 0 on success, negative on failure
1964 static int __init
fcoe_init(void)
1968 struct fcoe_percpu_s
*p
;
1970 mutex_lock(&fcoe_config_mutex
);
1972 for_each_possible_cpu(cpu
) {
1973 p
= &per_cpu(fcoe_percpu
, cpu
);
1974 skb_queue_head_init(&p
->fcoe_rx_list
);
1977 for_each_online_cpu(cpu
)
1978 fcoe_percpu_thread_create(cpu
);
1980 /* Initialize per CPU interrupt thread */
1981 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1985 /* Setup link change notification */
1988 rc
= fcoe_if_init();
1992 mutex_unlock(&fcoe_config_mutex
);
1996 for_each_online_cpu(cpu
) {
1997 fcoe_percpu_thread_destroy(cpu
);
1999 mutex_unlock(&fcoe_config_mutex
);
2002 module_init(fcoe_init
);
2005 * fcoe_exit() - fcoe module unloading cleanup
2007 * Returns 0 on success, negative on failure
2009 static void __exit
fcoe_exit(void)
2012 struct fcoe_interface
*fcoe
, *tmp
;
2013 struct fcoe_port
*port
;
2015 mutex_lock(&fcoe_config_mutex
);
2019 /* releases the associated fcoe hosts */
2021 list_for_each_entry_safe(fcoe
, tmp
, &fcoe_hostlist
, list
) {
2022 list_del(&fcoe
->list
);
2023 port
= lport_priv(fcoe
->ctlr
.lp
);
2024 fcoe_interface_cleanup(fcoe
);
2025 schedule_work(&port
->destroy_work
);
2029 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
2031 for_each_online_cpu(cpu
)
2032 fcoe_percpu_thread_destroy(cpu
);
2034 mutex_unlock(&fcoe_config_mutex
);
2036 /* flush any asyncronous interface destroys,
2037 * this should happen after the netdev notifier is unregistered */
2038 flush_scheduled_work();
2040 /* detach from scsi transport
2041 * must happen after all destroys are done, therefor after the flush */
2044 module_exit(fcoe_exit
);