[SCSI] fcoe: Use per-CPU kernel function for dev_stats instead of an array
[safe/jmp/linux-2.6] / 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 #include <scsi/fc_transport_fcoe.h>
40
41 #define FCOE_SW_VERSION "0.1"
42 #define FCOE_SW_NAME    "fcoesw"
43 #define FCOE_SW_VENDOR  "Open-FCoE.org"
44
45 #define FCOE_MAX_LUN            255
46 #define FCOE_MAX_FCP_TARGET     256
47
48 #define FCOE_MAX_OUTSTANDING_COMMANDS   1024
49
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 */
52
53 static struct scsi_transport_template *scsi_transport_fcoe_sw;
54
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,
62
63         .show_host_port_id = 1,
64         .show_host_supported_speeds = 1,
65         .get_host_speed = fc_get_host_speed,
66         .show_host_speed = 1,
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,
71
72         .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
73         .show_rport_maxframe_size = 1,
74         .show_rport_supported_classes = 1,
75
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,
84
85         .terminate_rport_io = fc_rport_terminate_io,
86 };
87
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,
99         .this_id = -1,
100         .cmd_per_lun = 32,
101         .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
102         .use_clustering = ENABLE_CLUSTERING,
103         .sg_tablesize = SG_ALL,
104         .max_sectors = 0xffff,
105 };
106
107 /**
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
111  *
112  * Returns: 0 for success
113  */
114 static int fcoe_sw_lport_config(struct fc_lport *lp)
115 {
116         lp->link_up = 0;
117         lp->qfull = 0;
118         lp->max_retry_count = 3;
119         lp->e_d_tov = 2 * 1000; /* FC-FS default */
120         lp->r_a_tov = 2 * 2 * 1000;
121         lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
122                               FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
123
124         fc_lport_init_stats(lp);
125
126         /* lport fc_lport related configuration */
127         fc_lport_config(lp);
128
129         /* offload related configuration */
130         lp->crc_offload = 0;
131         lp->seq_offload = 0;
132         lp->lro_enabled = 0;
133         lp->lro_xid = 0;
134         lp->lso_max = 0;
135
136         return 0;
137 }
138
139 /**
140  * fcoe_sw_netdev_config() - Set up netdev for SW FCoE
141  * @lp : ptr to the fc_lport
142  * @netdev : ptr to the associated netdevice struct
143  *
144  * Must be called after fcoe_sw_lport_config() as it will use lport mutex
145  *
146  * Returns : 0 for success
147  */
148 static int fcoe_sw_netdev_config(struct fc_lport *lp, struct net_device *netdev)
149 {
150         u32 mfs;
151         u64 wwnn, wwpn;
152         struct fcoe_softc *fc;
153         u8 flogi_maddr[ETH_ALEN];
154
155         /* Setup lport private data to point to fcoe softc */
156         fc = lport_priv(lp);
157         fc->lp = lp;
158         fc->real_dev = netdev;
159         fc->phys_dev = netdev;
160
161         /* Require support for get_pauseparam ethtool op. */
162         if (netdev->priv_flags & IFF_802_1Q_VLAN)
163                 fc->phys_dev = vlan_dev_real_dev(netdev);
164
165         /* Do not support for bonding device */
166         if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
167             (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
168             (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
169                 return -EOPNOTSUPP;
170         }
171
172         /*
173          * Determine max frame size based on underlying device and optional
174          * user-configured limit.  If the MFS is too low, fcoe_link_ok()
175          * will return 0, so do this first.
176          */
177         mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
178                                    sizeof(struct fcoe_crc_eof));
179         if (fc_set_mfs(lp, mfs))
180                 return -EINVAL;
181
182         if (!fcoe_link_ok(lp))
183                 lp->link_up = 1;
184
185         /* offload features support */
186         if (fc->real_dev->features & NETIF_F_SG)
187                 lp->sg_supp = 1;
188
189 #ifdef NETIF_F_FCOE_CRC
190         if (netdev->features & NETIF_F_FCOE_CRC) {
191                 lp->crc_offload = 1;
192                 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
193                        netdev->name);
194         }
195 #endif
196 #ifdef NETIF_F_FSO
197         if (netdev->features & NETIF_F_FSO) {
198                 lp->seq_offload = 1;
199                 lp->lso_max = netdev->gso_max_size;
200                 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
201                        netdev->name, lp->lso_max);
202         }
203 #endif
204         if (netdev->fcoe_ddp_xid) {
205                 lp->lro_enabled = 1;
206                 lp->lro_xid = netdev->fcoe_ddp_xid;
207                 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
208                        netdev->name, lp->lro_xid);
209         }
210         skb_queue_head_init(&fc->fcoe_pending_queue);
211         fc->fcoe_pending_queue_active = 0;
212
213         /* setup Source Mac Address */
214         memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
215                fc->real_dev->addr_len);
216
217         wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
218         fc_set_wwnn(lp, wwnn);
219         /* XXX - 3rd arg needs to be vlan id */
220         wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
221         fc_set_wwpn(lp, wwpn);
222
223         /*
224          * Add FCoE MAC address as second unicast MAC address
225          * or enter promiscuous mode if not capable of listening
226          * for multiple unicast MACs.
227          */
228         rtnl_lock();
229         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
230         dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
231         rtnl_unlock();
232
233         /*
234          * setup the receive function from ethernet driver
235          * on the ethertype for the given device
236          */
237         fc->fcoe_packet_type.func = fcoe_rcv;
238         fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
239         fc->fcoe_packet_type.dev = fc->real_dev;
240         dev_add_pack(&fc->fcoe_packet_type);
241
242         return 0;
243 }
244
245 /**
246  * fcoe_sw_shost_config() - Sets up fc_lport->host
247  * @lp : ptr to the fc_lport
248  * @shost : ptr to the associated scsi host
249  * @dev : device associated to scsi host
250  *
251  * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
252  *
253  * Returns : 0 for success
254  */
255 static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
256                                 struct device *dev)
257 {
258         int rc = 0;
259
260         /* lport scsi host config */
261         lp->host = shost;
262
263         lp->host->max_lun = FCOE_MAX_LUN;
264         lp->host->max_id = FCOE_MAX_FCP_TARGET;
265         lp->host->max_channel = 0;
266         lp->host->transportt = scsi_transport_fcoe_sw;
267
268         /* add the new host to the SCSI-ml */
269         rc = scsi_add_host(lp->host, dev);
270         if (rc) {
271                 FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
272                 return rc;
273         }
274         sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
275                 FCOE_SW_NAME, FCOE_SW_VERSION,
276                 fcoe_netdev(lp)->name);
277
278         return 0;
279 }
280
281 /**
282  * fcoe_sw_em_config() - allocates em for this lport
283  * @lp: the port that em is to allocated for
284  *
285  * Returns : 0 on success
286  */
287 static inline int fcoe_sw_em_config(struct fc_lport *lp)
288 {
289         BUG_ON(lp->emp);
290
291         lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
292                                     FCOE_MIN_XID, FCOE_MAX_XID);
293         if (!lp->emp)
294                 return -ENOMEM;
295
296         return 0;
297 }
298
299 /**
300  * fcoe_sw_destroy() - FCoE software HBA tear-down function
301  * @netdev: ptr to the associated net_device
302  *
303  * Returns: 0 if link is OK for use by FCoE.
304  */
305 static int fcoe_sw_destroy(struct net_device *netdev)
306 {
307         struct fc_lport *lp = NULL;
308         struct fcoe_softc *fc;
309         u8 flogi_maddr[ETH_ALEN];
310
311         BUG_ON(!netdev);
312
313         printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
314                netdev->name);
315
316         lp = fcoe_hostlist_lookup(netdev);
317         if (!lp)
318                 return -ENODEV;
319
320         fc = lport_priv(lp);
321
322         /* Logout of the fabric */
323         fc_fabric_logoff(lp);
324
325         /* Remove the instance from fcoe's list */
326         fcoe_hostlist_remove(lp);
327
328         /* Don't listen for Ethernet packets anymore */
329         dev_remove_pack(&fc->fcoe_packet_type);
330
331         /* Cleanup the fc_lport */
332         fc_lport_destroy(lp);
333         fc_fcp_destroy(lp);
334
335         /* Detach from the scsi-ml */
336         fc_remove_host(lp->host);
337         scsi_remove_host(lp->host);
338
339         /* There are no more rports or I/O, free the EM */
340         if (lp->emp)
341                 fc_exch_mgr_free(lp->emp);
342
343         /* Delete secondary MAC addresses */
344         rtnl_lock();
345         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
346         dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
347         if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
348                 dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
349         rtnl_unlock();
350
351         /* Free the per-CPU revieve threads */
352         fcoe_percpu_clean(lp);
353
354         /* Free existing skbs */
355         fcoe_clean_pending_queue(lp);
356
357         /* Free memory used by statistical counters */
358         fc_lport_free_stats(lp);
359
360         /* Release the net_device and Scsi_Host */
361         dev_put(fc->real_dev);
362         scsi_host_put(lp->host);
363
364         return 0;
365 }
366
367 /*
368  * fcoe_sw_ddp_setup - calls LLD's ddp_setup through net_device
369  * @lp: the corresponding fc_lport
370  * @xid: the exchange id for this ddp transfer
371  * @sgl: the scatterlist describing this transfer
372  * @sgc: number of sg items
373  *
374  * Returns : 0 no ddp
375  */
376 static int fcoe_sw_ddp_setup(struct fc_lport *lp, u16 xid,
377                              struct scatterlist *sgl, unsigned int sgc)
378 {
379         struct net_device *n = fcoe_netdev(lp);
380
381         if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
382                 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
383
384         return 0;
385 }
386
387 /*
388  * fcoe_sw_ddp_done - calls LLD's ddp_done through net_device
389  * @lp: the corresponding fc_lport
390  * @xid: the exchange id for this ddp transfer
391  *
392  * Returns : the length of data that have been completed by ddp
393  */
394 static int fcoe_sw_ddp_done(struct fc_lport *lp, u16 xid)
395 {
396         struct net_device *n = fcoe_netdev(lp);
397
398         if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
399                 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
400         return 0;
401 }
402
403 static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
404         .frame_send = fcoe_xmit,
405         .ddp_setup = fcoe_sw_ddp_setup,
406         .ddp_done = fcoe_sw_ddp_done,
407 };
408
409 /**
410  * fcoe_sw_create() - this function creates the fcoe interface
411  * @netdev: pointer the associated netdevice
412  *
413  * Creates fc_lport struct and scsi_host for lport, configures lport
414  * and starts fabric login.
415  *
416  * Returns : 0 on success
417  */
418 static int fcoe_sw_create(struct net_device *netdev)
419 {
420         int rc;
421         struct fc_lport *lp = NULL;
422         struct fcoe_softc *fc;
423         struct Scsi_Host *shost;
424
425         BUG_ON(!netdev);
426
427         printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
428                netdev->name);
429
430         lp = fcoe_hostlist_lookup(netdev);
431         if (lp)
432                 return -EEXIST;
433
434         shost = fcoe_host_alloc(&fcoe_sw_shost_template,
435                                 sizeof(struct fcoe_softc));
436         if (!shost) {
437                 FC_DBG("Could not allocate host structure\n");
438                 return -ENOMEM;
439         }
440         lp = shost_priv(shost);
441         fc = lport_priv(lp);
442
443         /* configure fc_lport, e.g., em */
444         rc = fcoe_sw_lport_config(lp);
445         if (rc) {
446                 FC_DBG("Could not configure lport\n");
447                 goto out_host_put;
448         }
449
450         /* configure lport network properties */
451         rc = fcoe_sw_netdev_config(lp, netdev);
452         if (rc) {
453                 FC_DBG("Could not configure netdev for lport\n");
454                 goto out_host_put;
455         }
456
457         /* configure lport scsi host properties */
458         rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
459         if (rc) {
460                 FC_DBG("Could not configure shost for lport\n");
461                 goto out_host_put;
462         }
463
464         /* lport exch manager allocation */
465         rc = fcoe_sw_em_config(lp);
466         if (rc) {
467                 FC_DBG("Could not configure em for lport\n");
468                 goto out_host_put;
469         }
470
471         /* Initialize the library */
472         rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
473         if (rc) {
474                 FC_DBG("Could not configure libfc for lport!\n");
475                 goto out_lp_destroy;
476         }
477
478         /* add to lports list */
479         fcoe_hostlist_add(lp);
480
481         lp->boot_time = jiffies;
482
483         fc_fabric_login(lp);
484
485         dev_hold(netdev);
486
487         return rc;
488
489 out_lp_destroy:
490         fc_exch_mgr_free(lp->emp); /* Free the EM */
491 out_host_put:
492         scsi_host_put(lp->host);
493         return rc;
494 }
495
496 /**
497  * fcoe_sw_match() - The FCoE SW transport match function
498  *
499  * Returns : false always
500  */
501 static bool fcoe_sw_match(struct net_device *netdev)
502 {
503         /* FIXME - for sw transport, always return false */
504         return false;
505 }
506
507 /* the sw hba fcoe transport */
508 struct fcoe_transport fcoe_sw_transport = {
509         .name = "fcoesw",
510         .create = fcoe_sw_create,
511         .destroy = fcoe_sw_destroy,
512         .match = fcoe_sw_match,
513         .vendor = 0x0,
514         .device = 0xffff,
515 };
516
517 /**
518  * fcoe_sw_init() - Registers fcoe_sw_transport
519  *
520  * Returns : 0 on success
521  */
522 int __init fcoe_sw_init(void)
523 {
524         /* attach to scsi transport */
525         scsi_transport_fcoe_sw =
526                 fc_attach_transport(&fcoe_sw_transport_function);
527
528         if (!scsi_transport_fcoe_sw) {
529                 printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
530                 return -ENODEV;
531         }
532
533         mutex_init(&fcoe_sw_transport.devlock);
534         INIT_LIST_HEAD(&fcoe_sw_transport.devlist);
535
536         /* register sw transport */
537         fcoe_transport_register(&fcoe_sw_transport);
538         return 0;
539 }
540
541 /**
542  * fcoe_sw_exit() - Unregisters fcoe_sw_transport
543  *
544  * Returns : 0 on success
545  */
546 int __exit fcoe_sw_exit(void)
547 {
548         /* dettach the transport */
549         fc_release_transport(scsi_transport_fcoe_sw);
550         fcoe_transport_unregister(&fcoe_sw_transport);
551         return 0;
552 }