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 *fcoe_transport_template;
94 static struct scsi_transport_template *fcoe_vport_transport_template;
96 struct fc_function_template fcoe_transport_function = {
97 .show_host_node_name = 1,
98 .show_host_port_name = 1,
99 .show_host_supported_classes = 1,
100 .show_host_supported_fc4s = 1,
101 .show_host_active_fc4s = 1,
102 .show_host_maxframe_size = 1,
104 .show_host_port_id = 1,
105 .show_host_supported_speeds = 1,
106 .get_host_speed = fc_get_host_speed,
107 .show_host_speed = 1,
108 .show_host_port_type = 1,
109 .get_host_port_state = fc_get_host_port_state,
110 .show_host_port_state = 1,
111 .show_host_symbolic_name = 1,
113 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
114 .show_rport_maxframe_size = 1,
115 .show_rport_supported_classes = 1,
117 .show_host_fabric_name = 1,
118 .show_starget_node_name = 1,
119 .show_starget_port_name = 1,
120 .show_starget_port_id = 1,
121 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
122 .show_rport_dev_loss_tmo = 1,
123 .get_fc_host_stats = fc_get_host_stats,
124 .issue_fc_host_lip = fcoe_reset,
126 .terminate_rport_io = fc_rport_terminate_io,
129 struct fc_function_template fcoe_vport_transport_function = {
130 .show_host_node_name = 1,
131 .show_host_port_name = 1,
132 .show_host_supported_classes = 1,
133 .show_host_supported_fc4s = 1,
134 .show_host_active_fc4s = 1,
135 .show_host_maxframe_size = 1,
137 .show_host_port_id = 1,
138 .show_host_supported_speeds = 1,
139 .get_host_speed = fc_get_host_speed,
140 .show_host_speed = 1,
141 .show_host_port_type = 1,
142 .get_host_port_state = fc_get_host_port_state,
143 .show_host_port_state = 1,
144 .show_host_symbolic_name = 1,
146 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
147 .show_rport_maxframe_size = 1,
148 .show_rport_supported_classes = 1,
150 .show_host_fabric_name = 1,
151 .show_starget_node_name = 1,
152 .show_starget_port_name = 1,
153 .show_starget_port_id = 1,
154 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
155 .show_rport_dev_loss_tmo = 1,
156 .get_fc_host_stats = fc_get_host_stats,
157 .issue_fc_host_lip = fcoe_reset,
159 .terminate_rport_io = fc_rport_terminate_io,
162 static struct scsi_host_template fcoe_shost_template = {
163 .module = THIS_MODULE,
164 .name = "FCoE Driver",
165 .proc_name = FCOE_NAME,
166 .queuecommand = fc_queuecommand,
167 .eh_abort_handler = fc_eh_abort,
168 .eh_device_reset_handler = fc_eh_device_reset,
169 .eh_host_reset_handler = fc_eh_host_reset,
170 .slave_alloc = fc_slave_alloc,
171 .change_queue_depth = fc_change_queue_depth,
172 .change_queue_type = fc_change_queue_type,
175 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
176 .use_clustering = ENABLE_CLUSTERING,
177 .sg_tablesize = SG_ALL,
178 .max_sectors = 0xffff,
181 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
182 struct packet_type *ptype,
183 struct net_device *orig_dev);
185 * fcoe_interface_setup()
186 * @fcoe: new fcoe_interface
187 * @netdev : ptr to the associated netdevice struct
189 * Returns : 0 for success
190 * Locking: must be called with the RTNL mutex held
192 static int fcoe_interface_setup(struct fcoe_interface *fcoe,
193 struct net_device *netdev)
195 struct fcoe_ctlr *fip = &fcoe->ctlr;
196 struct netdev_hw_addr *ha;
197 u8 flogi_maddr[ETH_ALEN];
198 const struct net_device_ops *ops;
200 fcoe->netdev = netdev;
202 /* Let LLD initialize for FCoE */
203 ops = netdev->netdev_ops;
204 if (ops->ndo_fcoe_enable) {
205 if (ops->ndo_fcoe_enable(netdev))
206 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
207 " specific feature for LLD.\n");
210 /* Do not support for bonding device */
211 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
212 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
213 (netdev->priv_flags & IFF_MASTER_8023AD)) {
217 /* look for SAN MAC address, if multiple SAN MACs exist, only
218 * use the first one for SPMA */
220 for_each_dev_addr(netdev, ha) {
221 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
222 (is_valid_ether_addr(fip->ctl_src_addr))) {
223 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
230 /* setup Source Mac Address */
232 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
235 * Add FCoE MAC address as second unicast MAC address
236 * or enter promiscuous mode if not capable of listening
237 * for multiple unicast MACs.
239 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
240 dev_unicast_add(netdev, flogi_maddr);
242 dev_unicast_add(netdev, fip->ctl_src_addr);
243 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
246 * setup the receive function from ethernet driver
247 * on the ethertype for the given device
249 fcoe->fcoe_packet_type.func = fcoe_rcv;
250 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
251 fcoe->fcoe_packet_type.dev = netdev;
252 dev_add_pack(&fcoe->fcoe_packet_type);
254 fcoe->fip_packet_type.func = fcoe_fip_recv;
255 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
256 fcoe->fip_packet_type.dev = netdev;
257 dev_add_pack(&fcoe->fip_packet_type);
262 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
263 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
264 static u8 *fcoe_get_src_mac(struct fc_lport *lport);
265 static void fcoe_destroy_work(struct work_struct *work);
268 * fcoe_interface_create()
269 * @netdev: network interface
271 * Returns: pointer to a struct fcoe_interface or NULL on error
273 static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
275 struct fcoe_interface *fcoe;
277 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
279 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
284 kref_init(&fcoe->kref);
289 fcoe_ctlr_init(&fcoe->ctlr);
290 fcoe->ctlr.send = fcoe_fip_send;
291 fcoe->ctlr.update_mac = fcoe_update_src_mac;
292 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
294 fcoe_interface_setup(fcoe, netdev);
300 * fcoe_interface_cleanup() - clean up netdev configurations
303 * Caller must be holding the RTNL mutex
305 void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
307 struct net_device *netdev = fcoe->netdev;
308 struct fcoe_ctlr *fip = &fcoe->ctlr;
309 u8 flogi_maddr[ETH_ALEN];
310 const struct net_device_ops *ops;
313 * Don't listen for Ethernet packets anymore.
314 * synchronize_net() ensures that the packet handlers are not running
315 * on another CPU. dev_remove_pack() would do that, this calls the
316 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
318 __dev_remove_pack(&fcoe->fcoe_packet_type);
319 __dev_remove_pack(&fcoe->fip_packet_type);
322 /* Delete secondary MAC addresses */
323 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
324 dev_unicast_delete(netdev, flogi_maddr);
326 dev_unicast_delete(netdev, fip->ctl_src_addr);
327 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
329 /* Tell the LLD we are done w/ FCoE */
330 ops = netdev->netdev_ops;
331 if (ops->ndo_fcoe_disable) {
332 if (ops->ndo_fcoe_disable(netdev))
333 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
334 " specific feature for LLD.\n");
339 * fcoe_interface_release() - fcoe_port kref release function
340 * @kref: embedded reference count in an fcoe_interface struct
342 static void fcoe_interface_release(struct kref *kref)
344 struct fcoe_interface *fcoe;
345 struct net_device *netdev;
347 fcoe = container_of(kref, struct fcoe_interface, kref);
348 netdev = fcoe->netdev;
349 /* tear-down the FCoE controller */
350 fcoe_ctlr_destroy(&fcoe->ctlr);
356 * fcoe_interface_get()
359 static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
361 kref_get(&fcoe->kref);
365 * fcoe_interface_put()
368 static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
370 kref_put(&fcoe->kref, fcoe_interface_release);
374 * fcoe_fip_recv - handle a received FIP frame.
375 * @skb: the receive skb
376 * @dev: associated &net_device
377 * @ptype: the &packet_type structure which was used to register this handler.
378 * @orig_dev: original receive &net_device, in case @dev is a bond.
380 * Returns: 0 for success
382 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
383 struct packet_type *ptype,
384 struct net_device *orig_dev)
386 struct fcoe_interface *fcoe;
388 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
389 fcoe_ctlr_recv(&fcoe->ctlr, skb);
394 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
395 * @fip: FCoE controller.
398 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
400 skb->dev = fcoe_from_ctlr(fip)->netdev;
405 * fcoe_update_src_mac() - Update Ethernet MAC filters.
406 * @lport: libfc lport
407 * @addr: Unicast MAC address to add.
409 * Remove any previously-set unicast MAC filter.
410 * Add secondary FCoE MAC address filter for our OUI.
412 static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
414 struct fcoe_port *port = lport_priv(lport);
415 struct fcoe_interface *fcoe = port->fcoe;
418 if (!is_zero_ether_addr(port->data_src_addr))
419 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
420 if (!is_zero_ether_addr(addr))
421 dev_unicast_add(fcoe->netdev, addr);
422 memcpy(port->data_src_addr, addr, ETH_ALEN);
427 * fcoe_get_src_mac() - return the Ethernet source address for an lport
428 * @lport: libfc lport
430 static u8 *fcoe_get_src_mac(struct fc_lport *lport)
432 struct fcoe_port *port = lport_priv(lport);
434 return port->data_src_addr;
438 * fcoe_lport_config() - sets up the fc_lport
439 * @lp: ptr to the fc_lport
441 * Returns: 0 for success
443 static int fcoe_lport_config(struct fc_lport *lp)
447 lp->max_retry_count = 3;
448 lp->max_rport_retry_count = 3;
449 lp->e_d_tov = 2 * 1000; /* FC-FS default */
450 lp->r_a_tov = 2 * 2 * 1000;
451 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
452 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
454 fc_lport_init_stats(lp);
456 /* lport fc_lport related configuration */
459 /* offload related configuration */
470 * fcoe_queue_timer() - fcoe queue timer
471 * @lp: the fc_lport pointer
473 * Calls fcoe_check_wait_queue on timeout
476 static void fcoe_queue_timer(ulong lp)
478 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
482 * fcoe_netdev_config() - Set up netdev for SW FCoE
483 * @lp : ptr to the fc_lport
484 * @netdev : ptr to the associated netdevice struct
486 * Must be called after fcoe_lport_config() as it will use lport mutex
488 * Returns : 0 for success
490 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
494 struct fcoe_interface *fcoe;
495 struct fcoe_port *port;
497 /* Setup lport private data to point to fcoe softc */
498 port = lport_priv(lp);
502 * Determine max frame size based on underlying device and optional
503 * user-configured limit. If the MFS is too low, fcoe_link_ok()
504 * will return 0, so do this first.
507 if (netdev->features & NETIF_F_FCOE_MTU) {
509 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
511 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
512 if (fc_set_mfs(lp, mfs))
515 /* offload features support */
516 if (netdev->features & NETIF_F_SG)
519 if (netdev->features & NETIF_F_FCOE_CRC) {
521 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
523 if (netdev->features & NETIF_F_FSO) {
525 lp->lso_max = netdev->gso_max_size;
526 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
529 if (netdev->fcoe_ddp_xid) {
531 lp->lro_xid = netdev->fcoe_ddp_xid;
532 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
535 skb_queue_head_init(&port->fcoe_pending_queue);
536 port->fcoe_pending_queue_active = 0;
537 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
539 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
540 fc_set_wwnn(lp, wwnn);
541 /* XXX - 3rd arg needs to be vlan id */
542 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
543 fc_set_wwpn(lp, wwpn);
549 * fcoe_shost_config() - Sets up fc_lport->host
550 * @lp : ptr to the fc_lport
551 * @shost : ptr to the associated scsi host
552 * @dev : device associated to scsi host
554 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
556 * Returns : 0 for success
558 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
563 /* lport scsi host config */
564 lp->host->max_lun = FCOE_MAX_LUN;
565 lp->host->max_id = FCOE_MAX_FCP_TARGET;
566 lp->host->max_channel = 0;
568 lp->host->transportt = fcoe_vport_transport_template;
570 lp->host->transportt = fcoe_transport_template;
572 /* add the new host to the SCSI-ml */
573 rc = scsi_add_host(lp->host, dev);
575 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
576 "error on scsi_add_host\n");
579 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
580 FCOE_NAME, FCOE_VERSION,
581 fcoe_netdev(lp)->name);
587 * fcoe_oem_match() - match for read types IO
588 * @fp: the fc_frame for new IO.
590 * Returns : true for read types IO, otherwise returns false.
592 bool fcoe_oem_match(struct fc_frame *fp)
594 return fc_fcp_is_read(fr_fsp(fp)) &&
595 (fr_fsp(fp)->data_len > fcoe_ddp_min);
599 * fcoe_em_config() - allocates em for this lport
600 * @lp: the fcoe that em is to allocated for
602 * Returns : 0 on success
604 static inline int fcoe_em_config(struct fc_lport *lp)
606 struct fcoe_port *port = lport_priv(lp);
607 struct fcoe_interface *fcoe = port->fcoe;
608 struct fcoe_interface *oldfcoe = NULL;
609 struct net_device *old_real_dev, *cur_real_dev;
610 u16 min_xid = FCOE_MIN_XID;
611 u16 max_xid = FCOE_MAX_XID;
614 * Check if need to allocate an em instance for
615 * offload exchange ids to be shared across all VN_PORTs/lport.
617 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
623 * Reuse existing offload em instance in case
624 * it is already allocated on real eth device
626 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
627 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
629 cur_real_dev = fcoe->netdev;
631 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
632 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
633 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
635 old_real_dev = oldfcoe->netdev;
637 if (cur_real_dev == old_real_dev) {
638 fcoe->oem = oldfcoe->oem;
644 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
645 printk(KERN_ERR "fcoe_em_config: failed to add "
646 "offload em:%p on interface:%s\n",
647 fcoe->oem, fcoe->netdev->name);
651 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
652 FCOE_MIN_XID, lp->lro_xid,
655 printk(KERN_ERR "fcoe_em_config: failed to allocate "
656 "em for offload exches on interface:%s\n",
663 * Exclude offload EM xid range from next EM xid range.
665 min_xid += lp->lro_xid + 1;
668 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
669 printk(KERN_ERR "fcoe_em_config: failed to "
670 "allocate em on interface %s\n", fcoe->netdev->name);
678 * fcoe_if_destroy() - FCoE software HBA tear-down function
679 * @lport: fc_lport to destroy
681 static void fcoe_if_destroy(struct fc_lport *lport)
683 struct fcoe_port *port = lport_priv(lport);
684 struct fcoe_interface *fcoe = port->fcoe;
685 struct net_device *netdev = fcoe->netdev;
687 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
689 /* Logout of the fabric */
690 fc_fabric_logoff(lport);
692 /* Cleanup the fc_lport */
693 fc_lport_destroy(lport);
694 fc_fcp_destroy(lport);
696 /* Stop the transmit retry timer */
697 del_timer_sync(&port->timer);
699 /* Free existing transmit skbs */
700 fcoe_clean_pending_queue(lport);
703 if (!is_zero_ether_addr(port->data_src_addr))
704 dev_unicast_delete(netdev, port->data_src_addr);
707 /* receives may not be stopped until after this */
708 fcoe_interface_put(fcoe);
710 /* Free queued packets for the per-CPU receive threads */
711 fcoe_percpu_clean(lport);
713 /* Detach from the scsi-ml */
714 fc_remove_host(lport->host);
715 scsi_remove_host(lport->host);
717 /* There are no more rports or I/O, free the EM */
718 fc_exch_mgr_free(lport);
720 /* Free memory used by statistical counters */
721 fc_lport_free_stats(lport);
723 /* Release the Scsi_Host */
724 scsi_host_put(lport->host);
728 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
729 * @lp: the corresponding fc_lport
730 * @xid: the exchange id for this ddp transfer
731 * @sgl: the scatterlist describing this transfer
732 * @sgc: number of sg items
736 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
737 struct scatterlist *sgl, unsigned int sgc)
739 struct net_device *n = fcoe_netdev(lp);
741 if (n->netdev_ops->ndo_fcoe_ddp_setup)
742 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
748 * fcoe_ddp_done - calls LLD's ddp_done through net_device
749 * @lp: the corresponding fc_lport
750 * @xid: the exchange id for this ddp transfer
752 * Returns : the length of data that have been completed by ddp
754 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
756 struct net_device *n = fcoe_netdev(lp);
758 if (n->netdev_ops->ndo_fcoe_ddp_done)
759 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
763 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
764 u32 did, struct fc_frame *fp, unsigned int op,
765 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
766 void *arg, u32 timeout);
768 static struct libfc_function_template fcoe_libfc_fcn_templ = {
769 .frame_send = fcoe_xmit,
770 .ddp_setup = fcoe_ddp_setup,
771 .ddp_done = fcoe_ddp_done,
772 .elsct_send = fcoe_elsct_send,
776 * fcoe_if_create() - this function creates the fcoe port
777 * @fcoe: fcoe_interface structure to create an fc_lport instance on
778 * @parent: device pointer to be the parent in sysfs for the SCSI host
780 * Creates fc_lport struct and scsi_host for lport, configures lport.
782 * Returns : The allocated fc_lport or an error pointer
784 static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
785 struct device *parent)
788 struct fc_lport *lport = NULL;
789 struct fcoe_port *port;
790 struct Scsi_Host *shost;
791 struct net_device *netdev = fcoe->netdev;
793 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
795 lport = libfc_host_alloc(&fcoe_shost_template,
796 sizeof(struct fcoe_port));
798 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
803 port = lport_priv(lport);
806 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
808 /* configure fc_lport, e.g., em */
809 rc = fcoe_lport_config(lport);
811 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
816 /* configure lport network properties */
817 rc = fcoe_netdev_config(lport, netdev);
819 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
824 /* configure lport scsi host properties */
825 rc = fcoe_shost_config(lport, shost, parent);
827 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
832 /* Initialize the library */
833 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
835 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
841 * fcoe_em_alloc() and fcoe_hostlist_add() both
842 * need to be atomic with respect to other changes to the hostlist
843 * since fcoe_em_alloc() looks for an existing EM
844 * instance on host list updated by fcoe_hostlist_add().
846 * This is currently handled through the fcoe_config_mutex begin held.
849 /* lport exch manager allocation */
850 rc = fcoe_em_config(lport);
852 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
857 fcoe_interface_get(fcoe);
861 fc_exch_mgr_free(lport);
863 scsi_host_put(lport->host);
869 * fcoe_if_init() - attach to scsi transport
871 * Returns : 0 on success
873 static int __init fcoe_if_init(void)
875 /* attach to scsi transport */
876 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
877 fcoe_vport_transport_template =
878 fc_attach_transport(&fcoe_vport_transport_function);
880 if (!fcoe_transport_template) {
881 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
889 * fcoe_if_exit() - detach from scsi transport
891 * Returns : 0 on success
893 int __exit fcoe_if_exit(void)
895 fc_release_transport(fcoe_transport_template);
896 fc_release_transport(fcoe_vport_transport_template);
897 fcoe_transport_template = NULL;
898 fcoe_vport_transport_template = NULL;
903 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
904 * @cpu: cpu index for the online cpu
906 static void fcoe_percpu_thread_create(unsigned int cpu)
908 struct fcoe_percpu_s *p;
909 struct task_struct *thread;
911 p = &per_cpu(fcoe_percpu, cpu);
913 thread = kthread_create(fcoe_percpu_receive_thread,
914 (void *)p, "fcoethread/%d", cpu);
916 if (likely(!IS_ERR(thread))) {
917 kthread_bind(thread, cpu);
918 wake_up_process(thread);
920 spin_lock_bh(&p->fcoe_rx_list.lock);
922 spin_unlock_bh(&p->fcoe_rx_list.lock);
927 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
928 * @cpu: cpu index the rx thread is to be removed
930 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
931 * current CPU's Rx thread. If the thread being destroyed is bound to
932 * the CPU processing this context the skbs will be freed.
934 static void fcoe_percpu_thread_destroy(unsigned int cpu)
936 struct fcoe_percpu_s *p;
937 struct task_struct *thread;
938 struct page *crc_eof;
941 struct fcoe_percpu_s *p0;
942 unsigned targ_cpu = smp_processor_id();
943 #endif /* CONFIG_SMP */
945 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
947 /* Prevent any new skbs from being queued for this CPU. */
948 p = &per_cpu(fcoe_percpu, cpu);
949 spin_lock_bh(&p->fcoe_rx_list.lock);
952 crc_eof = p->crc_eof_page;
953 p->crc_eof_page = NULL;
954 p->crc_eof_offset = 0;
955 spin_unlock_bh(&p->fcoe_rx_list.lock);
959 * Don't bother moving the skb's if this context is running
960 * on the same CPU that is having its thread destroyed. This
961 * can easily happen when the module is removed.
963 if (cpu != targ_cpu) {
964 p0 = &per_cpu(fcoe_percpu, targ_cpu);
965 spin_lock_bh(&p0->fcoe_rx_list.lock);
967 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
970 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
971 __skb_queue_tail(&p0->fcoe_rx_list, skb);
972 spin_unlock_bh(&p0->fcoe_rx_list.lock);
975 * The targeted CPU is not initialized and cannot accept
976 * new skbs. Unlock the targeted CPU and drop the skbs
977 * on the CPU that is going offline.
979 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
981 spin_unlock_bh(&p0->fcoe_rx_list.lock);
985 * This scenario occurs when the module is being removed
986 * and all threads are being destroyed. skbs will continue
987 * to be shifted from the CPU thread that is being removed
988 * to the CPU thread associated with the CPU that is processing
989 * the module removal. Once there is only one CPU Rx thread it
990 * will reach this case and we will drop all skbs and later
993 spin_lock_bh(&p->fcoe_rx_list.lock);
994 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
996 spin_unlock_bh(&p->fcoe_rx_list.lock);
1000 * This a non-SMP scenario where the singular Rx thread is
1001 * being removed. Free all skbs and stop the thread.
1003 spin_lock_bh(&p->fcoe_rx_list.lock);
1004 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1006 spin_unlock_bh(&p->fcoe_rx_list.lock);
1010 kthread_stop(thread);
1017 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
1018 * @nfb: callback data block
1019 * @action: event triggering the callback
1020 * @hcpu: index for the cpu of this event
1022 * This creates or destroys per cpu data for fcoe
1024 * Returns NOTIFY_OK always.
1026 static int fcoe_cpu_callback(struct notifier_block *nfb,
1027 unsigned long action, void *hcpu)
1029 unsigned cpu = (unsigned long)hcpu;
1033 case CPU_ONLINE_FROZEN:
1034 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
1035 fcoe_percpu_thread_create(cpu);
1038 case CPU_DEAD_FROZEN:
1039 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
1040 fcoe_percpu_thread_destroy(cpu);
1048 static struct notifier_block fcoe_cpu_notifier = {
1049 .notifier_call = fcoe_cpu_callback,
1053 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
1054 * @skb: the receive skb
1055 * @dev: associated net device
1057 * @olddev: last device
1059 * this function will receive the packet and build fc frame and pass it up
1061 * Returns: 0 for success
1063 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1064 struct packet_type *ptype, struct net_device *olddev)
1066 struct fc_lport *lp;
1067 struct fcoe_rcv_info *fr;
1068 struct fcoe_interface *fcoe;
1069 struct fc_frame_header *fh;
1070 struct fcoe_percpu_s *fps;
1073 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1075 if (unlikely(lp == NULL)) {
1076 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
1082 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1083 "data:%p tail:%p end:%p sum:%d dev:%s",
1084 skb->len, skb->data_len, skb->head, skb->data,
1085 skb_tail_pointer(skb), skb_end_pointer(skb),
1086 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1088 /* check for FCOE packet type */
1089 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
1090 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
1095 * Check for minimum frame length, and make sure required FCoE
1096 * and FC headers are pulled into the linear data area.
1098 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1099 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1102 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1103 fh = (struct fc_frame_header *) skb_transport_header(skb);
1105 fr = fcoe_dev_from_skb(skb);
1110 * In case the incoming frame's exchange is originated from
1111 * the initiator, then received frame's exchange id is ANDed
1112 * with fc_cpu_mask bits to get the same cpu on which exchange
1113 * was originated, otherwise just use the current cpu.
1115 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1116 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1118 cpu = smp_processor_id();
1120 fps = &per_cpu(fcoe_percpu, cpu);
1121 spin_lock_bh(&fps->fcoe_rx_list.lock);
1122 if (unlikely(!fps->thread)) {
1124 * The targeted CPU is not ready, let's target
1125 * the first CPU now. For non-SMP systems this
1126 * will check the same CPU twice.
1128 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1129 "ready for incoming skb- using first online "
1132 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1133 cpu = first_cpu(cpu_online_map);
1134 fps = &per_cpu(fcoe_percpu, cpu);
1135 spin_lock_bh(&fps->fcoe_rx_list.lock);
1137 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1143 * We now have a valid CPU that we're targeting for
1144 * this skb. We also have this receive thread locked,
1145 * so we're free to queue skbs into it's queue.
1147 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1148 if (fps->fcoe_rx_list.qlen == 1)
1149 wake_up_process(fps->thread);
1151 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1155 fc_lport_get_stats(lp)->ErrorFrames++;
1163 * fcoe_start_io() - pass to netdev to start xmit for fcoe
1164 * @skb: the skb to be xmitted
1166 * Returns: 0 for success
1168 static inline int fcoe_start_io(struct sk_buff *skb)
1173 rc = dev_queue_xmit(skb);
1181 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
1182 * @skb: the skb to be xmitted
1185 * Returns: 0 for success
1187 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1189 struct fcoe_percpu_s *fps;
1192 fps = &get_cpu_var(fcoe_percpu);
1193 page = fps->crc_eof_page;
1195 page = alloc_page(GFP_ATOMIC);
1197 put_cpu_var(fcoe_percpu);
1200 fps->crc_eof_page = page;
1201 fps->crc_eof_offset = 0;
1205 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1206 fps->crc_eof_offset, tlen);
1208 skb->data_len += tlen;
1209 skb->truesize += tlen;
1210 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1212 if (fps->crc_eof_offset >= PAGE_SIZE) {
1213 fps->crc_eof_page = NULL;
1214 fps->crc_eof_offset = 0;
1217 put_cpu_var(fcoe_percpu);
1222 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
1223 * @fp: the fc_frame containing data to be checksummed
1225 * This uses crc32() to calculate the crc for port frame
1226 * Return : 32 bit crc
1228 u32 fcoe_fc_crc(struct fc_frame *fp)
1230 struct sk_buff *skb = fp_skb(fp);
1231 struct skb_frag_struct *frag;
1232 unsigned char *data;
1233 unsigned long off, len, clen;
1237 crc = crc32(~0, skb->data, skb_headlen(skb));
1239 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1240 frag = &skb_shinfo(skb)->frags[i];
1241 off = frag->page_offset;
1244 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1245 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1246 KM_SKB_DATA_SOFTIRQ);
1247 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1248 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1257 * fcoe_xmit() - FCoE frame transmit function
1258 * @lp: the associated local fcoe
1259 * @fp: the fc_frame to be transmitted
1261 * Return : 0 for success
1263 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1268 struct fcoe_crc_eof *cp;
1269 struct sk_buff *skb;
1270 struct fcoe_dev_stats *stats;
1271 struct fc_frame_header *fh;
1272 unsigned int hlen; /* header length implies the version */
1273 unsigned int tlen; /* trailer length */
1274 unsigned int elen; /* eth header, may include vlan */
1275 struct fcoe_port *port = lport_priv(lp);
1276 struct fcoe_interface *fcoe = port->fcoe;
1278 struct fcoe_hdr *hp;
1280 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1282 fh = fc_frame_header_get(fp);
1284 wlen = skb->len / FCOE_WORD_TO_BYTE;
1291 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1292 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
1298 elen = sizeof(struct ethhdr);
1299 hlen = sizeof(struct fcoe_hdr);
1300 tlen = sizeof(struct fcoe_crc_eof);
1301 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1304 if (likely(lp->crc_offload)) {
1305 skb->ip_summed = CHECKSUM_PARTIAL;
1306 skb->csum_start = skb_headroom(skb);
1307 skb->csum_offset = skb->len;
1310 skb->ip_summed = CHECKSUM_NONE;
1311 crc = fcoe_fc_crc(fp);
1314 /* copy port crc and eof to the skb buff */
1315 if (skb_is_nonlinear(skb)) {
1317 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1321 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1322 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1323 + frag->page_offset;
1325 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1328 memset(cp, 0, sizeof(*cp));
1330 cp->fcoe_crc32 = cpu_to_le32(~crc);
1332 if (skb_is_nonlinear(skb)) {
1333 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1337 /* adjust skb network/transport offsets to match mac/fcoe/port */
1338 skb_push(skb, elen + hlen);
1339 skb_reset_mac_header(skb);
1340 skb_reset_network_header(skb);
1341 skb->mac_len = elen;
1342 skb->protocol = htons(ETH_P_FCOE);
1343 skb->dev = fcoe->netdev;
1345 /* fill up mac and fcoe headers */
1347 eh->h_proto = htons(ETH_P_FCOE);
1348 if (fcoe->ctlr.map_dest)
1349 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1351 /* insert GW address */
1352 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1354 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1355 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
1357 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1359 hp = (struct fcoe_hdr *)(eh + 1);
1360 memset(hp, 0, sizeof(*hp));
1362 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1365 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1366 if (lp->seq_offload && fr_max_payload(fp)) {
1367 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1368 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1370 skb_shinfo(skb)->gso_type = 0;
1371 skb_shinfo(skb)->gso_size = 0;
1373 /* update tx stats: regardless if LLD fails */
1374 stats = fc_lport_get_stats(lp);
1376 stats->TxWords += wlen;
1378 /* send down to lld */
1380 if (port->fcoe_pending_queue.qlen)
1381 fcoe_check_wait_queue(lp, skb);
1382 else if (fcoe_start_io(skb))
1383 fcoe_check_wait_queue(lp, skb);
1389 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1390 * @skb: the skb being completed.
1392 static void fcoe_percpu_flush_done(struct sk_buff *skb)
1394 complete(&fcoe_flush_completion);
1398 * fcoe_percpu_receive_thread() - recv thread per cpu
1399 * @arg: ptr to the fcoe per cpu struct
1401 * Return: 0 for success
1403 int fcoe_percpu_receive_thread(void *arg)
1405 struct fcoe_percpu_s *p = arg;
1407 struct fc_lport *lp;
1408 struct fcoe_rcv_info *fr;
1409 struct fcoe_dev_stats *stats;
1410 struct fc_frame_header *fh;
1411 struct sk_buff *skb;
1412 struct fcoe_crc_eof crc_eof;
1413 struct fc_frame *fp;
1415 struct fcoe_port *port;
1416 struct fcoe_hdr *hp;
1418 set_user_nice(current, -20);
1420 while (!kthread_should_stop()) {
1422 spin_lock_bh(&p->fcoe_rx_list.lock);
1423 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1424 set_current_state(TASK_INTERRUPTIBLE);
1425 spin_unlock_bh(&p->fcoe_rx_list.lock);
1427 set_current_state(TASK_RUNNING);
1428 if (kthread_should_stop())
1430 spin_lock_bh(&p->fcoe_rx_list.lock);
1432 spin_unlock_bh(&p->fcoe_rx_list.lock);
1433 fr = fcoe_dev_from_skb(skb);
1435 if (unlikely(lp == NULL)) {
1436 if (skb->destructor != fcoe_percpu_flush_done)
1437 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1442 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1443 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1444 skb->len, skb->data_len,
1445 skb->head, skb->data, skb_tail_pointer(skb),
1446 skb_end_pointer(skb), skb->csum,
1447 skb->dev ? skb->dev->name : "<NULL>");
1450 * Save source MAC address before discarding header.
1452 port = lport_priv(lp);
1453 if (skb_is_nonlinear(skb))
1454 skb_linearize(skb); /* not ideal */
1455 mac = eth_hdr(skb)->h_source;
1458 * Frame length checks and setting up the header pointers
1459 * was done in fcoe_rcv already.
1461 hp = (struct fcoe_hdr *) skb_network_header(skb);
1462 fh = (struct fc_frame_header *) skb_transport_header(skb);
1464 stats = fc_lport_get_stats(lp);
1465 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1466 if (stats->ErrorFrames < 5)
1467 printk(KERN_WARNING "fcoe: FCoE version "
1468 "mismatch: The frame has "
1469 "version %x, but the "
1470 "initiator supports version "
1471 "%x\n", FC_FCOE_DECAPS_VER(hp),
1473 stats->ErrorFrames++;
1478 skb_pull(skb, sizeof(struct fcoe_hdr));
1479 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1482 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1484 fp = (struct fc_frame *)skb;
1487 fr_sof(fp) = hp->fcoe_sof;
1489 /* Copy out the CRC and EOF trailer for access */
1490 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1494 fr_eof(fp) = crc_eof.fcoe_eof;
1495 fr_crc(fp) = crc_eof.fcoe_crc32;
1496 if (pskb_trim(skb, fr_len)) {
1502 * We only check CRC if no offload is available and if it is
1503 * it's solicited data, in which case, the FCP layer would
1504 * check it during the copy.
1506 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1507 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1509 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1511 fh = fc_frame_header_get(fp);
1512 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1513 fh->fh_type == FC_TYPE_FCP) {
1514 fc_exch_recv(lp, fp);
1517 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1518 if (le32_to_cpu(fr_crc(fp)) !=
1519 ~crc32(~0, skb->data, fr_len)) {
1520 if (stats->InvalidCRCCount < 5)
1521 printk(KERN_WARNING "fcoe: dropping "
1522 "frame with CRC error\n");
1523 stats->InvalidCRCCount++;
1524 stats->ErrorFrames++;
1528 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1530 fc_exch_recv(lp, fp);
1536 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1539 * This empties the wait_queue, dequeue the head of the wait_queue queue
1540 * and calls fcoe_start_io() for each packet, if all skb have been
1541 * transmitted, return qlen or -1 if a error occurs, then restore
1542 * wait_queue and try again later.
1544 * The wait_queue is used when the skb transmit fails. skb will go
1545 * in the wait_queue which will be emptied by the timer function or
1546 * by the next skb transmit.
1548 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1550 struct fcoe_port *port = lport_priv(lp);
1553 spin_lock_bh(&port->fcoe_pending_queue.lock);
1556 __skb_queue_tail(&port->fcoe_pending_queue, skb);
1558 if (port->fcoe_pending_queue_active)
1560 port->fcoe_pending_queue_active = 1;
1562 while (port->fcoe_pending_queue.qlen) {
1563 /* keep qlen > 0 until fcoe_start_io succeeds */
1564 port->fcoe_pending_queue.qlen++;
1565 skb = __skb_dequeue(&port->fcoe_pending_queue);
1567 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1568 rc = fcoe_start_io(skb);
1569 spin_lock_bh(&port->fcoe_pending_queue.lock);
1572 __skb_queue_head(&port->fcoe_pending_queue, skb);
1573 /* undo temporary increment above */
1574 port->fcoe_pending_queue.qlen--;
1577 /* undo temporary increment above */
1578 port->fcoe_pending_queue.qlen--;
1581 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1583 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1584 mod_timer(&port->timer, jiffies + 2);
1585 port->fcoe_pending_queue_active = 0;
1587 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1589 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1594 * fcoe_dev_setup() - setup link change notification interface
1596 static void fcoe_dev_setup(void)
1598 register_netdevice_notifier(&fcoe_notifier);
1602 * fcoe_dev_cleanup() - cleanup link change notification interface
1604 static void fcoe_dev_cleanup(void)
1606 unregister_netdevice_notifier(&fcoe_notifier);
1610 * fcoe_device_notification() - netdev event notification callback
1611 * @notifier: context of the notification
1612 * @event: type of event
1613 * @ptr: fixed array for output parsed ifname
1615 * This function is called by the ethernet driver in case of link change event
1617 * Returns: 0 for success
1619 static int fcoe_device_notification(struct notifier_block *notifier,
1620 ulong event, void *ptr)
1622 struct fc_lport *lp = NULL;
1623 struct net_device *netdev = ptr;
1624 struct fcoe_interface *fcoe;
1625 struct fcoe_port *port;
1626 struct fcoe_dev_stats *stats;
1627 u32 link_possible = 1;
1631 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1632 if (fcoe->netdev == netdev) {
1644 case NETDEV_GOING_DOWN:
1650 case NETDEV_CHANGEMTU:
1651 if (netdev->features & NETIF_F_FCOE_MTU)
1653 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1654 sizeof(struct fcoe_crc_eof));
1655 if (mfs >= FC_MIN_MAX_FRAME)
1656 fc_set_mfs(lp, mfs);
1658 case NETDEV_REGISTER:
1660 case NETDEV_UNREGISTER:
1661 list_del(&fcoe->list);
1662 port = lport_priv(fcoe->ctlr.lp);
1663 fcoe_interface_cleanup(fcoe);
1664 schedule_work(&port->destroy_work);
1668 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1669 "from netdev netlink\n", event);
1671 if (link_possible && !fcoe_link_ok(lp))
1672 fcoe_ctlr_link_up(&fcoe->ctlr);
1673 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1674 stats = fc_lport_get_stats(lp);
1675 stats->LinkFailureCount++;
1676 fcoe_clean_pending_queue(lp);
1683 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1684 * @buffer: incoming buffer to be copied
1686 * Returns: NULL or ptr to net_device
1688 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1691 char ifname[IFNAMSIZ + 2];
1694 strlcpy(ifname, buffer, IFNAMSIZ);
1695 cp = ifname + strlen(ifname);
1696 while (--cp >= ifname && *cp == '\n')
1698 return dev_get_by_name(&init_net, ifname);
1704 * fcoe_destroy() - handles the destroy from sysfs
1705 * @buffer: expected to be an eth if name
1706 * @kp: associated kernel param
1708 * Returns: 0 for success
1710 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1712 struct fcoe_interface *fcoe;
1713 struct net_device *netdev;
1716 mutex_lock(&fcoe_config_mutex);
1717 #ifdef CONFIG_FCOE_MODULE
1719 * Make sure the module has been initialized, and is not about to be
1720 * removed. Module paramter sysfs files are writable before the
1721 * module_init function is called and after module_exit.
1723 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1729 netdev = fcoe_if_to_netdev(buffer);
1736 fcoe = fcoe_hostlist_lookup_port(netdev);
1742 list_del(&fcoe->list);
1743 fcoe_interface_cleanup(fcoe);
1745 fcoe_if_destroy(fcoe->ctlr.lp);
1749 mutex_unlock(&fcoe_config_mutex);
1753 static void fcoe_destroy_work(struct work_struct *work)
1755 struct fcoe_port *port;
1757 port = container_of(work, struct fcoe_port, destroy_work);
1758 mutex_lock(&fcoe_config_mutex);
1759 fcoe_if_destroy(port->lport);
1760 mutex_unlock(&fcoe_config_mutex);
1764 * fcoe_create() - Handles the create call from sysfs
1765 * @buffer: expected to be an eth if name
1766 * @kp: associated kernel param
1768 * Returns: 0 for success
1770 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1773 struct fcoe_interface *fcoe;
1774 struct fc_lport *lport;
1775 struct net_device *netdev;
1777 mutex_lock(&fcoe_config_mutex);
1778 #ifdef CONFIG_FCOE_MODULE
1780 * Make sure the module has been initialized, and is not about to be
1781 * removed. Module paramter sysfs files are writable before the
1782 * module_init function is called and after module_exit.
1784 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1791 netdev = fcoe_if_to_netdev(buffer);
1797 /* look for existing lport */
1798 if (fcoe_hostlist_lookup(netdev)) {
1803 fcoe = fcoe_interface_create(netdev);
1809 lport = fcoe_if_create(fcoe, &netdev->dev);
1810 if (IS_ERR(lport)) {
1811 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
1814 fcoe_interface_cleanup(fcoe);
1818 /* Make this the "master" N_Port */
1819 fcoe->ctlr.lp = lport;
1821 /* add to lports list */
1822 fcoe_hostlist_add(lport);
1824 /* start FIP Discovery and FLOGI */
1825 lport->boot_time = jiffies;
1826 fc_fabric_login(lport);
1827 if (!fcoe_link_ok(lport))
1828 fcoe_ctlr_link_up(&fcoe->ctlr);
1833 * Release from init in fcoe_interface_create(), on success lport
1834 * should be holding a reference taken in fcoe_if_create().
1836 fcoe_interface_put(fcoe);
1841 mutex_unlock(&fcoe_config_mutex);
1845 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1846 __MODULE_PARM_TYPE(create, "string");
1847 MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
1848 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1849 __MODULE_PARM_TYPE(destroy, "string");
1850 MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
1853 * fcoe_link_ok() - Check if link is ok for the fc_lport
1854 * @lp: ptr to the fc_lport
1856 * Any permanently-disqualifying conditions have been previously checked.
1857 * This also updates the speed setting, which may change with link for 100/1000.
1859 * This function should probably be checking for PAUSE support at some point
1860 * in the future. Currently Per-priority-pause is not determinable using
1861 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1863 * Returns: 0 if link is OK for use by FCoE.
1866 int fcoe_link_ok(struct fc_lport *lp)
1868 struct fcoe_port *port = lport_priv(lp);
1869 struct net_device *dev = port->fcoe->netdev;
1870 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1872 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1873 (!dev_ethtool_get_settings(dev, &ecmd))) {
1874 lp->link_supported_speeds &=
1875 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1876 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1877 SUPPORTED_1000baseT_Full))
1878 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1879 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1880 lp->link_supported_speeds |=
1881 FC_PORTSPEED_10GBIT;
1882 if (ecmd.speed == SPEED_1000)
1883 lp->link_speed = FC_PORTSPEED_1GBIT;
1884 if (ecmd.speed == SPEED_10000)
1885 lp->link_speed = FC_PORTSPEED_10GBIT;
1893 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1896 * Must be called with fcoe_create_mutex held to single-thread completion.
1898 * This flushes the pending skbs by adding a new skb to each queue and
1899 * waiting until they are all freed. This assures us that not only are
1900 * there no packets that will be handled by the lport, but also that any
1901 * threads already handling packet have returned.
1903 void fcoe_percpu_clean(struct fc_lport *lp)
1905 struct fcoe_percpu_s *pp;
1906 struct fcoe_rcv_info *fr;
1907 struct sk_buff_head *list;
1908 struct sk_buff *skb, *next;
1909 struct sk_buff *head;
1912 for_each_possible_cpu(cpu) {
1913 pp = &per_cpu(fcoe_percpu, cpu);
1914 spin_lock_bh(&pp->fcoe_rx_list.lock);
1915 list = &pp->fcoe_rx_list;
1917 for (skb = head; skb != (struct sk_buff *)list;
1920 fr = fcoe_dev_from_skb(skb);
1921 if (fr->fr_dev == lp) {
1922 __skb_unlink(skb, list);
1927 if (!pp->thread || !cpu_online(cpu)) {
1928 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1932 skb = dev_alloc_skb(0);
1934 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1937 skb->destructor = fcoe_percpu_flush_done;
1939 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1940 if (pp->fcoe_rx_list.qlen == 1)
1941 wake_up_process(pp->thread);
1942 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1944 wait_for_completion(&fcoe_flush_completion);
1949 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1950 * @lp: the corresponding fc_lport
1954 void fcoe_clean_pending_queue(struct fc_lport *lp)
1956 struct fcoe_port *port = lport_priv(lp);
1957 struct sk_buff *skb;
1959 spin_lock_bh(&port->fcoe_pending_queue.lock);
1960 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1961 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1963 spin_lock_bh(&port->fcoe_pending_queue.lock);
1965 spin_unlock_bh(&port->fcoe_pending_queue.lock);
1969 * fcoe_reset() - Resets the fcoe
1970 * @shost: shost the reset is from
1974 int fcoe_reset(struct Scsi_Host *shost)
1976 struct fc_lport *lport = shost_priv(shost);
1977 fc_lport_reset(lport);
1982 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
1983 * @dev: this is currently ptr to net_device
1985 * Returns: NULL or the located fcoe_port
1986 * Locking: must be called with the RNL mutex held
1988 static struct fcoe_interface *
1989 fcoe_hostlist_lookup_port(const struct net_device *dev)
1991 struct fcoe_interface *fcoe;
1993 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1994 if (fcoe->netdev == dev)
2001 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
2002 * @netdev: ptr to net_device
2004 * Returns: 0 for success
2005 * Locking: must be called with the RTNL mutex held
2007 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2009 struct fcoe_interface *fcoe;
2011 fcoe = fcoe_hostlist_lookup_port(netdev);
2012 return (fcoe) ? fcoe->ctlr.lp : NULL;
2016 * fcoe_hostlist_add() - Add a lport to lports list
2017 * @lp: ptr to the fc_lport to be added
2019 * Returns: 0 for success
2020 * Locking: must be called with the RTNL mutex held
2022 static int fcoe_hostlist_add(const struct fc_lport *lport)
2024 struct fcoe_interface *fcoe;
2025 struct fcoe_port *port;
2027 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2029 port = lport_priv(lport);
2031 list_add_tail(&fcoe->list, &fcoe_hostlist);
2037 * fcoe_init() - fcoe module loading initialization
2039 * Returns 0 on success, negative on failure
2041 static int __init fcoe_init(void)
2045 struct fcoe_percpu_s *p;
2047 mutex_lock(&fcoe_config_mutex);
2049 for_each_possible_cpu(cpu) {
2050 p = &per_cpu(fcoe_percpu, cpu);
2051 skb_queue_head_init(&p->fcoe_rx_list);
2054 for_each_online_cpu(cpu)
2055 fcoe_percpu_thread_create(cpu);
2057 /* Initialize per CPU interrupt thread */
2058 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2062 /* Setup link change notification */
2065 rc = fcoe_if_init();
2069 mutex_unlock(&fcoe_config_mutex);
2073 for_each_online_cpu(cpu) {
2074 fcoe_percpu_thread_destroy(cpu);
2076 mutex_unlock(&fcoe_config_mutex);
2079 module_init(fcoe_init);
2082 * fcoe_exit() - fcoe module unloading cleanup
2084 * Returns 0 on success, negative on failure
2086 static void __exit fcoe_exit(void)
2089 struct fcoe_interface *fcoe, *tmp;
2090 struct fcoe_port *port;
2092 mutex_lock(&fcoe_config_mutex);
2096 /* releases the associated fcoe hosts */
2098 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2099 list_del(&fcoe->list);
2100 port = lport_priv(fcoe->ctlr.lp);
2101 fcoe_interface_cleanup(fcoe);
2102 schedule_work(&port->destroy_work);
2106 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2108 for_each_online_cpu(cpu)
2109 fcoe_percpu_thread_destroy(cpu);
2111 mutex_unlock(&fcoe_config_mutex);
2113 /* flush any asyncronous interface destroys,
2114 * this should happen after the netdev notifier is unregistered */
2115 flush_scheduled_work();
2117 /* detach from scsi transport
2118 * must happen after all destroys are done, therefor after the flush */
2121 module_exit(fcoe_exit);
2124 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2125 * @seq: active sequence in the FLOGI or FDISC exchange
2126 * @fp: response frame, or error encoded in a pointer (timeout)
2127 * @arg: pointer the the fcoe_ctlr structure
2129 * This handles MAC address managment for FCoE, then passes control on to
2130 * the libfc FLOGI response handler.
2132 static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2134 struct fcoe_ctlr *fip = arg;
2135 struct fc_exch *exch = fc_seq_exch(seq);
2136 struct fc_lport *lport = exch->lp;
2142 mac = fr_cb(fp)->granted_mac;
2143 if (is_zero_ether_addr(mac)) {
2145 mac = eth_hdr(&fp->skb)->h_source;
2146 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2151 /* FIP, libfcoe has already seen it */
2152 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2155 fc_lport_flogi_resp(seq, fp, lport);
2159 * fcoe_logo_resp() - FCoE specific LOGO response handler
2160 * @seq: active sequence in the LOGO exchange
2161 * @fp: response frame, or error encoded in a pointer (timeout)
2162 * @arg: pointer the the fcoe_ctlr structure
2164 * This handles MAC address managment for FCoE, then passes control on to
2165 * the libfc LOGO response handler.
2167 static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2169 struct fcoe_ctlr *fip = arg;
2170 struct fc_exch *exch = fc_seq_exch(seq);
2171 struct fc_lport *lport = exch->lp;
2172 static u8 zero_mac[ETH_ALEN] = { 0 };
2175 fip->update_mac(lport, zero_mac);
2176 fc_lport_logo_resp(seq, fp, lport);
2180 * fcoe_elsct_send - FCoE specific ELS handler
2182 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2183 * using FCoE specific response handlers and passing the FIP controller as
2184 * the argument (the lport is still available from the exchange).
2186 * Most of the work here is just handed off to the libfc routine.
2188 static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2189 u32 did, struct fc_frame *fp, unsigned int op,
2190 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2191 void *arg, u32 timeout)
2193 struct fcoe_port *port = lport_priv(lport);
2194 struct fcoe_interface *fcoe = port->fcoe;
2195 struct fcoe_ctlr *fip = &fcoe->ctlr;
2196 struct fc_frame_header *fh = fc_frame_header_get(fp);
2201 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2204 /* only hook onto fabric logouts, not port logouts */
2205 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2207 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2210 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);