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>
40 #define FCOE_SW_VERSION "0.1"
41 #define FCOE_SW_NAME "fcoesw"
42 #define FCOE_SW_VENDOR "Open-FCoE.org"
44 #define FCOE_MAX_LUN 255
45 #define FCOE_MAX_FCP_TARGET 256
47 #define FCOE_MAX_OUTSTANDING_COMMANDS 1024
49 #define FCOE_MIN_XID 0x0001 /* the min xid supported by fcoe_sw */
50 #define FCOE_MAX_XID 0x07ef /* the max xid supported by fcoe_sw */
52 static struct scsi_transport_template *scsi_transport_fcoe_sw;
54 struct fc_function_template fcoe_sw_transport_function = {
55 .show_host_node_name = 1,
56 .show_host_port_name = 1,
57 .show_host_supported_classes = 1,
58 .show_host_supported_fc4s = 1,
59 .show_host_active_fc4s = 1,
60 .show_host_maxframe_size = 1,
62 .show_host_port_id = 1,
63 .show_host_supported_speeds = 1,
64 .get_host_speed = fc_get_host_speed,
66 .show_host_port_type = 1,
67 .get_host_port_state = fc_get_host_port_state,
68 .show_host_port_state = 1,
69 .show_host_symbolic_name = 1,
71 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
72 .show_rport_maxframe_size = 1,
73 .show_rport_supported_classes = 1,
75 .show_host_fabric_name = 1,
76 .show_starget_node_name = 1,
77 .show_starget_port_name = 1,
78 .show_starget_port_id = 1,
79 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
80 .show_rport_dev_loss_tmo = 1,
81 .get_fc_host_stats = fc_get_host_stats,
82 .issue_fc_host_lip = fcoe_reset,
84 .terminate_rport_io = fc_rport_terminate_io,
87 static struct scsi_host_template fcoe_sw_shost_template = {
88 .module = THIS_MODULE,
89 .name = "FCoE Driver",
90 .proc_name = FCOE_SW_NAME,
91 .queuecommand = fc_queuecommand,
92 .eh_abort_handler = fc_eh_abort,
93 .eh_device_reset_handler = fc_eh_device_reset,
94 .eh_host_reset_handler = fc_eh_host_reset,
95 .slave_alloc = fc_slave_alloc,
96 .change_queue_depth = fc_change_queue_depth,
97 .change_queue_type = fc_change_queue_type,
100 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
101 .use_clustering = ENABLE_CLUSTERING,
102 .sg_tablesize = SG_ALL,
103 .max_sectors = 0xffff,
107 * fcoe_sw_lport_config() - sets up the fc_lport
108 * @lp: ptr to the fc_lport
109 * @shost: ptr to the parent scsi host
111 * Returns: 0 for success
113 static int fcoe_sw_lport_config(struct fc_lport *lp)
117 lp->max_retry_count = 3;
118 lp->e_d_tov = 2 * 1000; /* FC-FS default */
119 lp->r_a_tov = 2 * 2 * 1000;
120 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
121 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
123 fc_lport_init_stats(lp);
125 /* lport fc_lport related configuration */
128 /* offload related configuration */
139 * fcoe_sw_netdev_config() - Set up netdev for SW FCoE
140 * @lp : ptr to the fc_lport
141 * @netdev : ptr to the associated netdevice struct
143 * Must be called after fcoe_sw_lport_config() as it will use lport mutex
145 * Returns : 0 for success
147 static int fcoe_sw_netdev_config(struct fc_lport *lp, struct net_device *netdev)
151 struct fcoe_softc *fc;
152 u8 flogi_maddr[ETH_ALEN];
154 /* Setup lport private data to point to fcoe softc */
157 fc->real_dev = netdev;
158 fc->phys_dev = netdev;
160 /* Require support for get_pauseparam ethtool op. */
161 if (netdev->priv_flags & IFF_802_1Q_VLAN)
162 fc->phys_dev = vlan_dev_real_dev(netdev);
164 /* Do not support for bonding device */
165 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
166 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
167 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
172 * Determine max frame size based on underlying device and optional
173 * user-configured limit. If the MFS is too low, fcoe_link_ok()
174 * will return 0, so do this first.
176 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
177 sizeof(struct fcoe_crc_eof));
178 if (fc_set_mfs(lp, mfs))
181 if (!fcoe_link_ok(lp))
184 /* offload features support */
185 if (fc->real_dev->features & NETIF_F_SG)
188 #ifdef NETIF_F_FCOE_CRC
189 if (netdev->features & NETIF_F_FCOE_CRC) {
191 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
196 if (netdev->features & NETIF_F_FSO) {
198 lp->lso_max = netdev->gso_max_size;
199 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
200 netdev->name, lp->lso_max);
203 if (netdev->fcoe_ddp_xid) {
205 lp->lro_xid = netdev->fcoe_ddp_xid;
206 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
207 netdev->name, lp->lro_xid);
209 skb_queue_head_init(&fc->fcoe_pending_queue);
210 fc->fcoe_pending_queue_active = 0;
212 /* setup Source Mac Address */
213 memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
214 fc->real_dev->addr_len);
216 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
217 fc_set_wwnn(lp, wwnn);
218 /* XXX - 3rd arg needs to be vlan id */
219 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
220 fc_set_wwpn(lp, wwpn);
223 * Add FCoE MAC address as second unicast MAC address
224 * or enter promiscuous mode if not capable of listening
225 * for multiple unicast MACs.
228 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
229 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
233 * setup the receive function from ethernet driver
234 * on the ethertype for the given device
236 fc->fcoe_packet_type.func = fcoe_rcv;
237 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
238 fc->fcoe_packet_type.dev = fc->real_dev;
239 dev_add_pack(&fc->fcoe_packet_type);
245 * fcoe_sw_shost_config() - Sets up fc_lport->host
246 * @lp : ptr to the fc_lport
247 * @shost : ptr to the associated scsi host
248 * @dev : device associated to scsi host
250 * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
252 * Returns : 0 for success
254 static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
259 /* lport scsi host config */
262 lp->host->max_lun = FCOE_MAX_LUN;
263 lp->host->max_id = FCOE_MAX_FCP_TARGET;
264 lp->host->max_channel = 0;
265 lp->host->transportt = scsi_transport_fcoe_sw;
267 /* add the new host to the SCSI-ml */
268 rc = scsi_add_host(lp->host, dev);
270 FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
273 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
274 FCOE_SW_NAME, FCOE_SW_VERSION,
275 fcoe_netdev(lp)->name);
281 * fcoe_sw_em_config() - allocates em for this lport
282 * @lp: the port that em is to allocated for
284 * Returns : 0 on success
286 static inline int fcoe_sw_em_config(struct fc_lport *lp)
290 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
291 FCOE_MIN_XID, FCOE_MAX_XID);
299 * fcoe_sw_destroy() - FCoE software HBA tear-down function
300 * @netdev: ptr to the associated net_device
302 * Returns: 0 if link is OK for use by FCoE.
304 int fcoe_sw_destroy(struct net_device *netdev)
306 struct fc_lport *lp = NULL;
307 struct fcoe_softc *fc;
308 u8 flogi_maddr[ETH_ALEN];
312 printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
315 lp = fcoe_hostlist_lookup(netdev);
321 /* Logout of the fabric */
322 fc_fabric_logoff(lp);
324 /* Remove the instance from fcoe's list */
325 fcoe_hostlist_remove(lp);
327 /* Don't listen for Ethernet packets anymore */
328 dev_remove_pack(&fc->fcoe_packet_type);
330 /* Cleanup the fc_lport */
331 fc_lport_destroy(lp);
334 /* Detach from the scsi-ml */
335 fc_remove_host(lp->host);
336 scsi_remove_host(lp->host);
338 /* There are no more rports or I/O, free the EM */
340 fc_exch_mgr_free(lp->emp);
342 /* Delete secondary MAC addresses */
344 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
345 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
346 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
347 dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
350 /* Free the per-CPU revieve threads */
351 fcoe_percpu_clean(lp);
353 /* Free existing skbs */
354 fcoe_clean_pending_queue(lp);
356 /* Free memory used by statistical counters */
357 fc_lport_free_stats(lp);
359 /* Release the net_device and Scsi_Host */
360 dev_put(fc->real_dev);
361 scsi_host_put(lp->host);
367 * fcoe_sw_ddp_setup - calls LLD's ddp_setup through net_device
368 * @lp: the corresponding fc_lport
369 * @xid: the exchange id for this ddp transfer
370 * @sgl: the scatterlist describing this transfer
371 * @sgc: number of sg items
375 static int fcoe_sw_ddp_setup(struct fc_lport *lp, u16 xid,
376 struct scatterlist *sgl, unsigned int sgc)
378 struct net_device *n = fcoe_netdev(lp);
380 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
381 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
387 * fcoe_sw_ddp_done - calls LLD's ddp_done through net_device
388 * @lp: the corresponding fc_lport
389 * @xid: the exchange id for this ddp transfer
391 * Returns : the length of data that have been completed by ddp
393 static int fcoe_sw_ddp_done(struct fc_lport *lp, u16 xid)
395 struct net_device *n = fcoe_netdev(lp);
397 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
398 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
402 static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
403 .frame_send = fcoe_xmit,
404 .ddp_setup = fcoe_sw_ddp_setup,
405 .ddp_done = fcoe_sw_ddp_done,
409 * fcoe_sw_create() - this function creates the fcoe interface
410 * @netdev: pointer the associated netdevice
412 * Creates fc_lport struct and scsi_host for lport, configures lport
413 * and starts fabric login.
415 * Returns : 0 on success
417 int fcoe_sw_create(struct net_device *netdev)
420 struct fc_lport *lp = NULL;
421 struct fcoe_softc *fc;
422 struct Scsi_Host *shost;
426 printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
429 lp = fcoe_hostlist_lookup(netdev);
433 shost = fcoe_host_alloc(&fcoe_sw_shost_template,
434 sizeof(struct fcoe_softc));
436 FC_DBG("Could not allocate host structure\n");
439 lp = shost_priv(shost);
442 /* configure fc_lport, e.g., em */
443 rc = fcoe_sw_lport_config(lp);
445 FC_DBG("Could not configure lport\n");
449 /* configure lport network properties */
450 rc = fcoe_sw_netdev_config(lp, netdev);
452 FC_DBG("Could not configure netdev for lport\n");
456 /* configure lport scsi host properties */
457 rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
459 FC_DBG("Could not configure shost for lport\n");
463 /* lport exch manager allocation */
464 rc = fcoe_sw_em_config(lp);
466 FC_DBG("Could not configure em for lport\n");
470 /* Initialize the library */
471 rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
473 FC_DBG("Could not configure libfc for lport!\n");
477 /* add to lports list */
478 fcoe_hostlist_add(lp);
480 lp->boot_time = jiffies;
489 fc_exch_mgr_free(lp->emp); /* Free the EM */
491 scsi_host_put(lp->host);
496 * fcoe_sw_init() - attach to scsi transport
498 * Returns : 0 on success
500 int __init fcoe_sw_init(void)
502 /* attach to scsi transport */
503 scsi_transport_fcoe_sw =
504 fc_attach_transport(&fcoe_sw_transport_function);
506 if (!scsi_transport_fcoe_sw) {
507 printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
515 * fcoe_sw_exit() - detach from scsi transport
517 * Returns : 0 on success
519 int __exit fcoe_sw_exit(void)
521 fc_release_transport(scsi_transport_fcoe_sw);