[SCSI] fcoe, libfc: check offload features from LLD through netdev
[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         int i = 0;
117
118         lp->link_up = 0;
119         lp->qfull = 0;
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);
125
126         /*
127          * allocate per cpu stats block
128          */
129         for_each_online_cpu(i)
130                 lp->dev_stats[i] = kzalloc(sizeof(struct fcoe_dev_stats),
131                                            GFP_KERNEL);
132
133         /* lport fc_lport related configuration */
134         fc_lport_config(lp);
135
136         /* offload related configuration */
137         lp->crc_offload = 0;
138         lp->seq_offload = 0;
139         lp->lro_enabled = 0;
140         lp->lro_xid = 0;
141         lp->lso_max = 0;
142
143         return 0;
144 }
145
146 /**
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
150  *
151  * Must be called after fcoe_sw_lport_config() as it will use lport mutex
152  *
153  * Returns : 0 for success
154  */
155 static int fcoe_sw_netdev_config(struct fc_lport *lp, struct net_device *netdev)
156 {
157         u32 mfs;
158         u64 wwnn, wwpn;
159         struct fcoe_softc *fc;
160         u8 flogi_maddr[ETH_ALEN];
161
162         /* Setup lport private data to point to fcoe softc */
163         fc = lport_priv(lp);
164         fc->lp = lp;
165         fc->real_dev = netdev;
166         fc->phys_dev = netdev;
167
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);
171
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)) {
176                 return -EOPNOTSUPP;
177         }
178
179         /*
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.
183          */
184         mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
185                                    sizeof(struct fcoe_crc_eof));
186         if (fc_set_mfs(lp, mfs))
187                 return -EINVAL;
188
189         if (!fcoe_link_ok(lp))
190                 lp->link_up = 1;
191
192         /* offload features support */
193         if (fc->real_dev->features & NETIF_F_SG)
194                 lp->sg_supp = 1;
195
196 #ifdef NETIF_F_FCOE_CRC
197         if (netdev->features & NETIF_F_FCOE_CRC) {
198                 lp->crc_offload = 1;
199                 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
200                        netdev->name);
201         }
202 #endif
203 #ifdef NETIF_F_FSO
204         if (netdev->features & NETIF_F_FSO) {
205                 lp->seq_offload = 1;
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);
209         }
210 #endif
211         if (netdev->fcoe_ddp_xid) {
212                 lp->lro_enabled = 1;
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);
216         }
217         skb_queue_head_init(&fc->fcoe_pending_queue);
218         fc->fcoe_pending_queue_active = 0;
219
220         /* setup Source Mac Address */
221         memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
222                fc->real_dev->addr_len);
223
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);
229
230         /*
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.
234          */
235         rtnl_lock();
236         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
237         dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
238         rtnl_unlock();
239
240         /*
241          * setup the receive function from ethernet driver
242          * on the ethertype for the given device
243          */
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);
248
249         return 0;
250 }
251
252 /**
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
257  *
258  * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
259  *
260  * Returns : 0 for success
261  */
262 static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
263                                 struct device *dev)
264 {
265         int rc = 0;
266
267         /* lport scsi host config */
268         lp->host = shost;
269
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;
274
275         /* add the new host to the SCSI-ml */
276         rc = scsi_add_host(lp->host, dev);
277         if (rc) {
278                 FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
279                 return rc;
280         }
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);
284
285         return 0;
286 }
287
288 /**
289  * fcoe_sw_em_config() - allocates em for this lport
290  * @lp: the port that em is to allocated for
291  *
292  * Returns : 0 on success
293  */
294 static inline int fcoe_sw_em_config(struct fc_lport *lp)
295 {
296         BUG_ON(lp->emp);
297
298         lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
299                                     FCOE_MIN_XID, FCOE_MAX_XID);
300         if (!lp->emp)
301                 return -ENOMEM;
302
303         return 0;
304 }
305
306 /**
307  * fcoe_sw_destroy() - FCoE software HBA tear-down function
308  * @netdev: ptr to the associated net_device
309  *
310  * Returns: 0 if link is OK for use by FCoE.
311  */
312 static int fcoe_sw_destroy(struct net_device *netdev)
313 {
314         int cpu;
315         struct fc_lport *lp = NULL;
316         struct fcoe_softc *fc;
317         u8 flogi_maddr[ETH_ALEN];
318
319         BUG_ON(!netdev);
320
321         printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
322                netdev->name);
323
324         lp = fcoe_hostlist_lookup(netdev);
325         if (!lp)
326                 return -ENODEV;
327
328         fc = lport_priv(lp);
329
330         /* Logout of the fabric */
331         fc_fabric_logoff(lp);
332
333         /* Remove the instance from fcoe's list */
334         fcoe_hostlist_remove(lp);
335
336         /* Don't listen for Ethernet packets anymore */
337         dev_remove_pack(&fc->fcoe_packet_type);
338
339         /* Cleanup the fc_lport */
340         fc_lport_destroy(lp);
341         fc_fcp_destroy(lp);
342
343         /* Detach from the scsi-ml */
344         fc_remove_host(lp->host);
345         scsi_remove_host(lp->host);
346
347         /* There are no more rports or I/O, free the EM */
348         if (lp->emp)
349                 fc_exch_mgr_free(lp->emp);
350
351         /* Delete secondary MAC addresses */
352         rtnl_lock();
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);
357         rtnl_unlock();
358
359         /* Free the per-CPU revieve threads */
360         fcoe_percpu_clean(lp);
361
362         /* Free existing skbs */
363         fcoe_clean_pending_queue(lp);
364
365         /* Free memory used by statistical counters */
366         for_each_online_cpu(cpu)
367                 kfree(lp->dev_stats[cpu]);
368
369         /* Release the net_device and Scsi_Host */
370         dev_put(fc->real_dev);
371         scsi_host_put(lp->host);
372
373         return 0;
374 }
375
376 static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
377         .frame_send = fcoe_xmit,
378 };
379
380 /**
381  * fcoe_sw_create() - this function creates the fcoe interface
382  * @netdev: pointer the associated netdevice
383  *
384  * Creates fc_lport struct and scsi_host for lport, configures lport
385  * and starts fabric login.
386  *
387  * Returns : 0 on success
388  */
389 static int fcoe_sw_create(struct net_device *netdev)
390 {
391         int rc;
392         struct fc_lport *lp = NULL;
393         struct fcoe_softc *fc;
394         struct Scsi_Host *shost;
395
396         BUG_ON(!netdev);
397
398         printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
399                netdev->name);
400
401         lp = fcoe_hostlist_lookup(netdev);
402         if (lp)
403                 return -EEXIST;
404
405         shost = fcoe_host_alloc(&fcoe_sw_shost_template,
406                                 sizeof(struct fcoe_softc));
407         if (!shost) {
408                 FC_DBG("Could not allocate host structure\n");
409                 return -ENOMEM;
410         }
411         lp = shost_priv(shost);
412         fc = lport_priv(lp);
413
414         /* configure fc_lport, e.g., em */
415         rc = fcoe_sw_lport_config(lp);
416         if (rc) {
417                 FC_DBG("Could not configure lport\n");
418                 goto out_host_put;
419         }
420
421         /* configure lport network properties */
422         rc = fcoe_sw_netdev_config(lp, netdev);
423         if (rc) {
424                 FC_DBG("Could not configure netdev for lport\n");
425                 goto out_host_put;
426         }
427
428         /* configure lport scsi host properties */
429         rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
430         if (rc) {
431                 FC_DBG("Could not configure shost for lport\n");
432                 goto out_host_put;
433         }
434
435         /* lport exch manager allocation */
436         rc = fcoe_sw_em_config(lp);
437         if (rc) {
438                 FC_DBG("Could not configure em for lport\n");
439                 goto out_host_put;
440         }
441
442         /* Initialize the library */
443         rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
444         if (rc) {
445                 FC_DBG("Could not configure libfc for lport!\n");
446                 goto out_lp_destroy;
447         }
448
449         /* add to lports list */
450         fcoe_hostlist_add(lp);
451
452         lp->boot_time = jiffies;
453
454         fc_fabric_login(lp);
455
456         dev_hold(netdev);
457
458         return rc;
459
460 out_lp_destroy:
461         fc_exch_mgr_free(lp->emp); /* Free the EM */
462 out_host_put:
463         scsi_host_put(lp->host);
464         return rc;
465 }
466
467 /**
468  * fcoe_sw_match() - The FCoE SW transport match function
469  *
470  * Returns : false always
471  */
472 static bool fcoe_sw_match(struct net_device *netdev)
473 {
474         /* FIXME - for sw transport, always return false */
475         return false;
476 }
477
478 /* the sw hba fcoe transport */
479 struct fcoe_transport fcoe_sw_transport = {
480         .name = "fcoesw",
481         .create = fcoe_sw_create,
482         .destroy = fcoe_sw_destroy,
483         .match = fcoe_sw_match,
484         .vendor = 0x0,
485         .device = 0xffff,
486 };
487
488 /**
489  * fcoe_sw_init() - Registers fcoe_sw_transport
490  *
491  * Returns : 0 on success
492  */
493 int __init fcoe_sw_init(void)
494 {
495         /* attach to scsi transport */
496         scsi_transport_fcoe_sw =
497                 fc_attach_transport(&fcoe_sw_transport_function);
498
499         if (!scsi_transport_fcoe_sw) {
500                 printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
501                 return -ENODEV;
502         }
503
504         mutex_init(&fcoe_sw_transport.devlock);
505         INIT_LIST_HEAD(&fcoe_sw_transport.devlist);
506
507         /* register sw transport */
508         fcoe_transport_register(&fcoe_sw_transport);
509         return 0;
510 }
511
512 /**
513  * fcoe_sw_exit() - Unregisters fcoe_sw_transport
514  *
515  * Returns : 0 on success
516  */
517 int __exit fcoe_sw_exit(void)
518 {
519         /* dettach the transport */
520         fc_release_transport(scsi_transport_fcoe_sw);
521         fcoe_transport_unregister(&fcoe_sw_transport);
522         return 0;
523 }