2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/kernel.h>
23 #include <linux/pci.h>
24 #include <linux/init.h>
25 #include <linux/spinlock.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/if_vlan.h>
29 #include <net/rtnetlink.h>
31 #include <scsi/fc/fc_els.h>
32 #include <scsi/fc/fc_encaps.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <scsi/scsi_transport.h>
35 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/libfc.h>
38 #include <scsi/libfcoe.h>
39 #include <scsi/fc_transport_fcoe.h>
41 #define FCOE_SW_VERSION "0.1"
42 #define FCOE_SW_NAME "fcoesw"
43 #define FCOE_SW_VENDOR "Open-FCoE.org"
45 #define FCOE_MAX_LUN 255
46 #define FCOE_MAX_FCP_TARGET 256
48 #define FCOE_MAX_OUTSTANDING_COMMANDS 1024
50 #define FCOE_MIN_XID 0x0001 /* the min xid supported by fcoe_sw */
51 #define FCOE_MAX_XID 0x07ef /* the max xid supported by fcoe_sw */
53 static struct scsi_transport_template *scsi_transport_fcoe_sw;
55 struct fc_function_template fcoe_sw_transport_function = {
56 .show_host_node_name = 1,
57 .show_host_port_name = 1,
58 .show_host_supported_classes = 1,
59 .show_host_supported_fc4s = 1,
60 .show_host_active_fc4s = 1,
61 .show_host_maxframe_size = 1,
63 .show_host_port_id = 1,
64 .show_host_supported_speeds = 1,
65 .get_host_speed = fc_get_host_speed,
67 .show_host_port_type = 1,
68 .get_host_port_state = fc_get_host_port_state,
69 .show_host_port_state = 1,
70 .show_host_symbolic_name = 1,
72 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
73 .show_rport_maxframe_size = 1,
74 .show_rport_supported_classes = 1,
76 .show_host_fabric_name = 1,
77 .show_starget_node_name = 1,
78 .show_starget_port_name = 1,
79 .show_starget_port_id = 1,
80 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
81 .show_rport_dev_loss_tmo = 1,
82 .get_fc_host_stats = fc_get_host_stats,
83 .issue_fc_host_lip = fcoe_reset,
85 .terminate_rport_io = fc_rport_terminate_io,
88 static struct scsi_host_template fcoe_sw_shost_template = {
89 .module = THIS_MODULE,
90 .name = "FCoE Driver",
91 .proc_name = FCOE_SW_NAME,
92 .queuecommand = fc_queuecommand,
93 .eh_abort_handler = fc_eh_abort,
94 .eh_device_reset_handler = fc_eh_device_reset,
95 .eh_host_reset_handler = fc_eh_host_reset,
96 .slave_alloc = fc_slave_alloc,
97 .change_queue_depth = fc_change_queue_depth,
98 .change_queue_type = fc_change_queue_type,
101 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
102 .use_clustering = ENABLE_CLUSTERING,
103 .sg_tablesize = SG_ALL,
104 .max_sectors = 0xffff,
108 * fcoe_sw_lport_config() - sets up the fc_lport
109 * @lp: ptr to the fc_lport
110 * @shost: ptr to the parent scsi host
112 * Returns: 0 for success
114 static int fcoe_sw_lport_config(struct fc_lport *lp)
120 lp->max_retry_count = 3;
121 lp->e_d_tov = 2 * 1000; /* FC-FS default */
122 lp->r_a_tov = 2 * 2 * 1000;
123 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
124 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
127 * allocate per cpu stats block
129 for_each_online_cpu(i)
130 lp->dev_stats[i] = kzalloc(sizeof(struct fcoe_dev_stats),
133 /* lport fc_lport related configuration */
136 /* offload related configuration */
147 * fcoe_sw_netdev_config() - Set up netdev for SW FCoE
148 * @lp : ptr to the fc_lport
149 * @netdev : ptr to the associated netdevice struct
151 * Must be called after fcoe_sw_lport_config() as it will use lport mutex
153 * Returns : 0 for success
155 static int fcoe_sw_netdev_config(struct fc_lport *lp, struct net_device *netdev)
159 struct fcoe_softc *fc;
160 u8 flogi_maddr[ETH_ALEN];
162 /* Setup lport private data to point to fcoe softc */
165 fc->real_dev = netdev;
166 fc->phys_dev = netdev;
168 /* Require support for get_pauseparam ethtool op. */
169 if (netdev->priv_flags & IFF_802_1Q_VLAN)
170 fc->phys_dev = vlan_dev_real_dev(netdev);
172 /* Do not support for bonding device */
173 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
174 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
175 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
180 * Determine max frame size based on underlying device and optional
181 * user-configured limit. If the MFS is too low, fcoe_link_ok()
182 * will return 0, so do this first.
184 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
185 sizeof(struct fcoe_crc_eof));
186 if (fc_set_mfs(lp, mfs))
189 if (!fcoe_link_ok(lp))
192 /* offload features support */
193 if (fc->real_dev->features & NETIF_F_SG)
196 #ifdef NETIF_F_FCOE_CRC
197 if (netdev->features & NETIF_F_FCOE_CRC) {
199 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
204 if (netdev->features & NETIF_F_FSO) {
206 lp->lso_max = netdev->gso_max_size;
207 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
208 netdev->name, lp->lso_max);
211 if (netdev->fcoe_ddp_xid) {
213 lp->lro_xid = netdev->fcoe_ddp_xid;
214 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
215 netdev->name, lp->lro_xid);
217 skb_queue_head_init(&fc->fcoe_pending_queue);
218 fc->fcoe_pending_queue_active = 0;
220 /* setup Source Mac Address */
221 memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
222 fc->real_dev->addr_len);
224 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
225 fc_set_wwnn(lp, wwnn);
226 /* XXX - 3rd arg needs to be vlan id */
227 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
228 fc_set_wwpn(lp, wwpn);
231 * Add FCoE MAC address as second unicast MAC address
232 * or enter promiscuous mode if not capable of listening
233 * for multiple unicast MACs.
236 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
237 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
241 * setup the receive function from ethernet driver
242 * on the ethertype for the given device
244 fc->fcoe_packet_type.func = fcoe_rcv;
245 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
246 fc->fcoe_packet_type.dev = fc->real_dev;
247 dev_add_pack(&fc->fcoe_packet_type);
253 * fcoe_sw_shost_config() - Sets up fc_lport->host
254 * @lp : ptr to the fc_lport
255 * @shost : ptr to the associated scsi host
256 * @dev : device associated to scsi host
258 * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
260 * Returns : 0 for success
262 static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
267 /* lport scsi host config */
270 lp->host->max_lun = FCOE_MAX_LUN;
271 lp->host->max_id = FCOE_MAX_FCP_TARGET;
272 lp->host->max_channel = 0;
273 lp->host->transportt = scsi_transport_fcoe_sw;
275 /* add the new host to the SCSI-ml */
276 rc = scsi_add_host(lp->host, dev);
278 FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
281 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
282 FCOE_SW_NAME, FCOE_SW_VERSION,
283 fcoe_netdev(lp)->name);
289 * fcoe_sw_em_config() - allocates em for this lport
290 * @lp: the port that em is to allocated for
292 * Returns : 0 on success
294 static inline int fcoe_sw_em_config(struct fc_lport *lp)
298 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
299 FCOE_MIN_XID, FCOE_MAX_XID);
307 * fcoe_sw_destroy() - FCoE software HBA tear-down function
308 * @netdev: ptr to the associated net_device
310 * Returns: 0 if link is OK for use by FCoE.
312 static int fcoe_sw_destroy(struct net_device *netdev)
315 struct fc_lport *lp = NULL;
316 struct fcoe_softc *fc;
317 u8 flogi_maddr[ETH_ALEN];
321 printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
324 lp = fcoe_hostlist_lookup(netdev);
330 /* Logout of the fabric */
331 fc_fabric_logoff(lp);
333 /* Remove the instance from fcoe's list */
334 fcoe_hostlist_remove(lp);
336 /* Don't listen for Ethernet packets anymore */
337 dev_remove_pack(&fc->fcoe_packet_type);
339 /* Cleanup the fc_lport */
340 fc_lport_destroy(lp);
343 /* Detach from the scsi-ml */
344 fc_remove_host(lp->host);
345 scsi_remove_host(lp->host);
347 /* There are no more rports or I/O, free the EM */
349 fc_exch_mgr_free(lp->emp);
351 /* Delete secondary MAC addresses */
353 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
354 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
355 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
356 dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
359 /* Free the per-CPU revieve threads */
360 fcoe_percpu_clean(lp);
362 /* Free existing skbs */
363 fcoe_clean_pending_queue(lp);
365 /* Free memory used by statistical counters */
366 for_each_online_cpu(cpu)
367 kfree(lp->dev_stats[cpu]);
369 /* Release the net_device and Scsi_Host */
370 dev_put(fc->real_dev);
371 scsi_host_put(lp->host);
377 * fcoe_sw_ddp_setup - calls LLD's ddp_setup through net_device
378 * @lp: the corresponding fc_lport
379 * @xid: the exchange id for this ddp transfer
380 * @sgl: the scatterlist describing this transfer
381 * @sgc: number of sg items
385 static int fcoe_sw_ddp_setup(struct fc_lport *lp, u16 xid,
386 struct scatterlist *sgl, unsigned int sgc)
388 struct net_device *n = fcoe_netdev(lp);
390 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
391 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
397 * fcoe_sw_ddp_done - calls LLD's ddp_done through net_device
398 * @lp: the corresponding fc_lport
399 * @xid: the exchange id for this ddp transfer
401 * Returns : the length of data that have been completed by ddp
403 static int fcoe_sw_ddp_done(struct fc_lport *lp, u16 xid)
405 struct net_device *n = fcoe_netdev(lp);
407 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
408 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
412 static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
413 .frame_send = fcoe_xmit,
414 .ddp_setup = fcoe_sw_ddp_setup,
415 .ddp_done = fcoe_sw_ddp_done,
419 * fcoe_sw_create() - this function creates the fcoe interface
420 * @netdev: pointer the associated netdevice
422 * Creates fc_lport struct and scsi_host for lport, configures lport
423 * and starts fabric login.
425 * Returns : 0 on success
427 static int fcoe_sw_create(struct net_device *netdev)
430 struct fc_lport *lp = NULL;
431 struct fcoe_softc *fc;
432 struct Scsi_Host *shost;
436 printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
439 lp = fcoe_hostlist_lookup(netdev);
443 shost = fcoe_host_alloc(&fcoe_sw_shost_template,
444 sizeof(struct fcoe_softc));
446 FC_DBG("Could not allocate host structure\n");
449 lp = shost_priv(shost);
452 /* configure fc_lport, e.g., em */
453 rc = fcoe_sw_lport_config(lp);
455 FC_DBG("Could not configure lport\n");
459 /* configure lport network properties */
460 rc = fcoe_sw_netdev_config(lp, netdev);
462 FC_DBG("Could not configure netdev for lport\n");
466 /* configure lport scsi host properties */
467 rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
469 FC_DBG("Could not configure shost for lport\n");
473 /* lport exch manager allocation */
474 rc = fcoe_sw_em_config(lp);
476 FC_DBG("Could not configure em for lport\n");
480 /* Initialize the library */
481 rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
483 FC_DBG("Could not configure libfc for lport!\n");
487 /* add to lports list */
488 fcoe_hostlist_add(lp);
490 lp->boot_time = jiffies;
499 fc_exch_mgr_free(lp->emp); /* Free the EM */
501 scsi_host_put(lp->host);
506 * fcoe_sw_match() - The FCoE SW transport match function
508 * Returns : false always
510 static bool fcoe_sw_match(struct net_device *netdev)
512 /* FIXME - for sw transport, always return false */
516 /* the sw hba fcoe transport */
517 struct fcoe_transport fcoe_sw_transport = {
519 .create = fcoe_sw_create,
520 .destroy = fcoe_sw_destroy,
521 .match = fcoe_sw_match,
527 * fcoe_sw_init() - Registers fcoe_sw_transport
529 * Returns : 0 on success
531 int __init fcoe_sw_init(void)
533 /* attach to scsi transport */
534 scsi_transport_fcoe_sw =
535 fc_attach_transport(&fcoe_sw_transport_function);
537 if (!scsi_transport_fcoe_sw) {
538 printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
542 mutex_init(&fcoe_sw_transport.devlock);
543 INIT_LIST_HEAD(&fcoe_sw_transport.devlist);
545 /* register sw transport */
546 fcoe_transport_register(&fcoe_sw_transport);
551 * fcoe_sw_exit() - Unregisters fcoe_sw_transport
553 * Returns : 0 on success
555 int __exit fcoe_sw_exit(void)
557 /* dettach the transport */
558 fc_release_transport(scsi_transport_fcoe_sw);
559 fcoe_transport_unregister(&fcoe_sw_transport);