]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/scsi/fcoe/fcoe_sw.c
[SCSI] fcoe: prep work to completely remove fc_transport_fcoe code
[linux-2.6.git] / drivers / scsi / fcoe / fcoe_sw.c
1 /*
2  * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3  *
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.
7  *
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
11  * more details.
12  *
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.
16  *
17  * Maintained at www.Open-FCoE.org
18  */
19
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>
30
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>
36
37 #include <scsi/libfc.h>
38 #include <scsi/libfcoe.h>
39
40 #define FCOE_SW_VERSION "0.1"
41 #define FCOE_SW_NAME    "fcoesw"
42 #define FCOE_SW_VENDOR  "Open-FCoE.org"
43
44 #define FCOE_MAX_LUN            255
45 #define FCOE_MAX_FCP_TARGET     256
46
47 #define FCOE_MAX_OUTSTANDING_COMMANDS   1024
48
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 */
51
52 static struct scsi_transport_template *scsi_transport_fcoe_sw;
53
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,
61
62         .show_host_port_id = 1,
63         .show_host_supported_speeds = 1,
64         .get_host_speed = fc_get_host_speed,
65         .show_host_speed = 1,
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,
70
71         .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
72         .show_rport_maxframe_size = 1,
73         .show_rport_supported_classes = 1,
74
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,
83
84         .terminate_rport_io = fc_rport_terminate_io,
85 };
86
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,
98         .this_id = -1,
99         .cmd_per_lun = 32,
100         .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
101         .use_clustering = ENABLE_CLUSTERING,
102         .sg_tablesize = SG_ALL,
103         .max_sectors = 0xffff,
104 };
105
106 /**
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
110  *
111  * Returns: 0 for success
112  */
113 static int fcoe_sw_lport_config(struct fc_lport *lp)
114 {
115         lp->link_up = 0;
116         lp->qfull = 0;
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);
122
123         fc_lport_init_stats(lp);
124
125         /* lport fc_lport related configuration */
126         fc_lport_config(lp);
127
128         /* offload related configuration */
129         lp->crc_offload = 0;
130         lp->seq_offload = 0;
131         lp->lro_enabled = 0;
132         lp->lro_xid = 0;
133         lp->lso_max = 0;
134
135         return 0;
136 }
137
138 /**
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
142  *
143  * Must be called after fcoe_sw_lport_config() as it will use lport mutex
144  *
145  * Returns : 0 for success
146  */
147 static int fcoe_sw_netdev_config(struct fc_lport *lp, struct net_device *netdev)
148 {
149         u32 mfs;
150         u64 wwnn, wwpn;
151         struct fcoe_softc *fc;
152         u8 flogi_maddr[ETH_ALEN];
153
154         /* Setup lport private data to point to fcoe softc */
155         fc = lport_priv(lp);
156         fc->lp = lp;
157         fc->real_dev = netdev;
158         fc->phys_dev = netdev;
159
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);
163
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)) {
168                 return -EOPNOTSUPP;
169         }
170
171         /*
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.
175          */
176         mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
177                                    sizeof(struct fcoe_crc_eof));
178         if (fc_set_mfs(lp, mfs))
179                 return -EINVAL;
180
181         if (!fcoe_link_ok(lp))
182                 lp->link_up = 1;
183
184         /* offload features support */
185         if (fc->real_dev->features & NETIF_F_SG)
186                 lp->sg_supp = 1;
187
188 #ifdef NETIF_F_FCOE_CRC
189         if (netdev->features & NETIF_F_FCOE_CRC) {
190                 lp->crc_offload = 1;
191                 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
192                        netdev->name);
193         }
194 #endif
195 #ifdef NETIF_F_FSO
196         if (netdev->features & NETIF_F_FSO) {
197                 lp->seq_offload = 1;
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);
201         }
202 #endif
203         if (netdev->fcoe_ddp_xid) {
204                 lp->lro_enabled = 1;
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);
208         }
209         skb_queue_head_init(&fc->fcoe_pending_queue);
210         fc->fcoe_pending_queue_active = 0;
211
212         /* setup Source Mac Address */
213         memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
214                fc->real_dev->addr_len);
215
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);
221
222         /*
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.
226          */
227         rtnl_lock();
228         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
229         dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
230         rtnl_unlock();
231
232         /*
233          * setup the receive function from ethernet driver
234          * on the ethertype for the given device
235          */
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);
240
241         return 0;
242 }
243
244 /**
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
249  *
250  * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
251  *
252  * Returns : 0 for success
253  */
254 static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
255                                 struct device *dev)
256 {
257         int rc = 0;
258
259         /* lport scsi host config */
260         lp->host = shost;
261
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;
266
267         /* add the new host to the SCSI-ml */
268         rc = scsi_add_host(lp->host, dev);
269         if (rc) {
270                 FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
271                 return rc;
272         }
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);
276
277         return 0;
278 }
279
280 /**
281  * fcoe_sw_em_config() - allocates em for this lport
282  * @lp: the port that em is to allocated for
283  *
284  * Returns : 0 on success
285  */
286 static inline int fcoe_sw_em_config(struct fc_lport *lp)
287 {
288         BUG_ON(lp->emp);
289
290         lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
291                                     FCOE_MIN_XID, FCOE_MAX_XID);
292         if (!lp->emp)
293                 return -ENOMEM;
294
295         return 0;
296 }
297
298 /**
299  * fcoe_sw_destroy() - FCoE software HBA tear-down function
300  * @netdev: ptr to the associated net_device
301  *
302  * Returns: 0 if link is OK for use by FCoE.
303  */
304 int fcoe_sw_destroy(struct net_device *netdev)
305 {
306         struct fc_lport *lp = NULL;
307         struct fcoe_softc *fc;
308         u8 flogi_maddr[ETH_ALEN];
309
310         BUG_ON(!netdev);
311
312         printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
313                netdev->name);
314
315         lp = fcoe_hostlist_lookup(netdev);
316         if (!lp)
317                 return -ENODEV;
318
319         fc = lport_priv(lp);
320
321         /* Logout of the fabric */
322         fc_fabric_logoff(lp);
323
324         /* Remove the instance from fcoe's list */
325         fcoe_hostlist_remove(lp);
326
327         /* Don't listen for Ethernet packets anymore */
328         dev_remove_pack(&fc->fcoe_packet_type);
329
330         /* Cleanup the fc_lport */
331         fc_lport_destroy(lp);
332         fc_fcp_destroy(lp);
333
334         /* Detach from the scsi-ml */
335         fc_remove_host(lp->host);
336         scsi_remove_host(lp->host);
337
338         /* There are no more rports or I/O, free the EM */
339         if (lp->emp)
340                 fc_exch_mgr_free(lp->emp);
341
342         /* Delete secondary MAC addresses */
343         rtnl_lock();
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);
348         rtnl_unlock();
349
350         /* Free the per-CPU revieve threads */
351         fcoe_percpu_clean(lp);
352
353         /* Free existing skbs */
354         fcoe_clean_pending_queue(lp);
355
356         /* Free memory used by statistical counters */
357         fc_lport_free_stats(lp);
358
359         /* Release the net_device and Scsi_Host */
360         dev_put(fc->real_dev);
361         scsi_host_put(lp->host);
362
363         return 0;
364 }
365
366 /*
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
372  *
373  * Returns : 0 no ddp
374  */
375 static int fcoe_sw_ddp_setup(struct fc_lport *lp, u16 xid,
376                              struct scatterlist *sgl, unsigned int sgc)
377 {
378         struct net_device *n = fcoe_netdev(lp);
379
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);
382
383         return 0;
384 }
385
386 /*
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
390  *
391  * Returns : the length of data that have been completed by ddp
392  */
393 static int fcoe_sw_ddp_done(struct fc_lport *lp, u16 xid)
394 {
395         struct net_device *n = fcoe_netdev(lp);
396
397         if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
398                 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
399         return 0;
400 }
401
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,
406 };
407
408 /**
409  * fcoe_sw_create() - this function creates the fcoe interface
410  * @netdev: pointer the associated netdevice
411  *
412  * Creates fc_lport struct and scsi_host for lport, configures lport
413  * and starts fabric login.
414  *
415  * Returns : 0 on success
416  */
417 int fcoe_sw_create(struct net_device *netdev)
418 {
419         int rc;
420         struct fc_lport *lp = NULL;
421         struct fcoe_softc *fc;
422         struct Scsi_Host *shost;
423
424         BUG_ON(!netdev);
425
426         printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
427                netdev->name);
428
429         lp = fcoe_hostlist_lookup(netdev);
430         if (lp)
431                 return -EEXIST;
432
433         shost = fcoe_host_alloc(&fcoe_sw_shost_template,
434                                 sizeof(struct fcoe_softc));
435         if (!shost) {
436                 FC_DBG("Could not allocate host structure\n");
437                 return -ENOMEM;
438         }
439         lp = shost_priv(shost);
440         fc = lport_priv(lp);
441
442         /* configure fc_lport, e.g., em */
443         rc = fcoe_sw_lport_config(lp);
444         if (rc) {
445                 FC_DBG("Could not configure lport\n");
446                 goto out_host_put;
447         }
448
449         /* configure lport network properties */
450         rc = fcoe_sw_netdev_config(lp, netdev);
451         if (rc) {
452                 FC_DBG("Could not configure netdev for lport\n");
453                 goto out_host_put;
454         }
455
456         /* configure lport scsi host properties */
457         rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
458         if (rc) {
459                 FC_DBG("Could not configure shost for lport\n");
460                 goto out_host_put;
461         }
462
463         /* lport exch manager allocation */
464         rc = fcoe_sw_em_config(lp);
465         if (rc) {
466                 FC_DBG("Could not configure em for lport\n");
467                 goto out_host_put;
468         }
469
470         /* Initialize the library */
471         rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
472         if (rc) {
473                 FC_DBG("Could not configure libfc for lport!\n");
474                 goto out_lp_destroy;
475         }
476
477         /* add to lports list */
478         fcoe_hostlist_add(lp);
479
480         lp->boot_time = jiffies;
481
482         fc_fabric_login(lp);
483
484         dev_hold(netdev);
485
486         return rc;
487
488 out_lp_destroy:
489         fc_exch_mgr_free(lp->emp); /* Free the EM */
490 out_host_put:
491         scsi_host_put(lp->host);
492         return rc;
493 }
494
495 /**
496  * fcoe_sw_init() - attach to scsi transport
497  *
498  * Returns : 0 on success
499  */
500 int __init fcoe_sw_init(void)
501 {
502         /* attach to scsi transport */
503         scsi_transport_fcoe_sw =
504                 fc_attach_transport(&fcoe_sw_transport_function);
505
506         if (!scsi_transport_fcoe_sw) {
507                 printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
508                 return -ENODEV;
509         }
510
511         return 0;
512 }
513
514 /**
515  * fcoe_sw_exit() - detach from scsi transport
516  *
517  * Returns : 0 on success
518  */
519 int __exit fcoe_sw_exit(void)
520 {
521         fc_release_transport(scsi_transport_fcoe_sw);
522         return 0;
523 }