Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[sfrench/cifs-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 #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         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
190         skb_queue_head_init(&fc->fcoe_pending_queue);
191         fc->fcoe_pending_queue_active = 0;
192
193         /* setup Source Mac Address */
194         memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
195                fc->real_dev->addr_len);
196
197         wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
198         fc_set_wwnn(lp, wwnn);
199         /* XXX - 3rd arg needs to be vlan id */
200         wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
201         fc_set_wwpn(lp, wwpn);
202
203         /*
204          * Add FCoE MAC address as second unicast MAC address
205          * or enter promiscuous mode if not capable of listening
206          * for multiple unicast MACs.
207          */
208         rtnl_lock();
209         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
210         dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
211         rtnl_unlock();
212
213         /*
214          * setup the receive function from ethernet driver
215          * on the ethertype for the given device
216          */
217         fc->fcoe_packet_type.func = fcoe_rcv;
218         fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
219         fc->fcoe_packet_type.dev = fc->real_dev;
220         dev_add_pack(&fc->fcoe_packet_type);
221
222         return 0;
223 }
224
225 /**
226  * fcoe_sw_shost_config() - Sets up fc_lport->host
227  * @lp : ptr to the fc_lport
228  * @shost : ptr to the associated scsi host
229  * @dev : device associated to scsi host
230  *
231  * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
232  *
233  * Returns : 0 for success
234  */
235 static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
236                                 struct device *dev)
237 {
238         int rc = 0;
239
240         /* lport scsi host config */
241         lp->host = shost;
242
243         lp->host->max_lun = FCOE_MAX_LUN;
244         lp->host->max_id = FCOE_MAX_FCP_TARGET;
245         lp->host->max_channel = 0;
246         lp->host->transportt = scsi_transport_fcoe_sw;
247
248         /* add the new host to the SCSI-ml */
249         rc = scsi_add_host(lp->host, dev);
250         if (rc) {
251                 FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
252                 return rc;
253         }
254         sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
255                 FCOE_SW_NAME, FCOE_SW_VERSION,
256                 fcoe_netdev(lp)->name);
257
258         return 0;
259 }
260
261 /**
262  * fcoe_sw_em_config() - allocates em for this lport
263  * @lp: the port that em is to allocated for
264  *
265  * Returns : 0 on success
266  */
267 static inline int fcoe_sw_em_config(struct fc_lport *lp)
268 {
269         BUG_ON(lp->emp);
270
271         lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
272                                     FCOE_MIN_XID, FCOE_MAX_XID);
273         if (!lp->emp)
274                 return -ENOMEM;
275
276         return 0;
277 }
278
279 /**
280  * fcoe_sw_destroy() - FCoE software HBA tear-down function
281  * @netdev: ptr to the associated net_device
282  *
283  * Returns: 0 if link is OK for use by FCoE.
284  */
285 static int fcoe_sw_destroy(struct net_device *netdev)
286 {
287         int cpu;
288         struct fc_lport *lp = NULL;
289         struct fcoe_softc *fc;
290         u8 flogi_maddr[ETH_ALEN];
291
292         BUG_ON(!netdev);
293
294         printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
295                netdev->name);
296
297         lp = fcoe_hostlist_lookup(netdev);
298         if (!lp)
299                 return -ENODEV;
300
301         fc = lport_priv(lp);
302
303         /* Logout of the fabric */
304         fc_fabric_logoff(lp);
305
306         /* Remove the instance from fcoe's list */
307         fcoe_hostlist_remove(lp);
308
309         /* Don't listen for Ethernet packets anymore */
310         dev_remove_pack(&fc->fcoe_packet_type);
311
312         /* Cleanup the fc_lport */
313         fc_lport_destroy(lp);
314         fc_fcp_destroy(lp);
315
316         /* Detach from the scsi-ml */
317         fc_remove_host(lp->host);
318         scsi_remove_host(lp->host);
319
320         /* There are no more rports or I/O, free the EM */
321         if (lp->emp)
322                 fc_exch_mgr_free(lp->emp);
323
324         /* Delete secondary MAC addresses */
325         rtnl_lock();
326         memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
327         dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
328         if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
329                 dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
330         rtnl_unlock();
331
332         /* Free the per-CPU revieve threads */
333         fcoe_percpu_clean(lp);
334
335         /* Free existing skbs */
336         fcoe_clean_pending_queue(lp);
337
338         /* Free memory used by statistical counters */
339         for_each_online_cpu(cpu)
340                 kfree(lp->dev_stats[cpu]);
341
342         /* Release the net_device and Scsi_Host */
343         dev_put(fc->real_dev);
344         scsi_host_put(lp->host);
345
346         return 0;
347 }
348
349 static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
350         .frame_send = fcoe_xmit,
351 };
352
353 /**
354  * fcoe_sw_create() - this function creates the fcoe interface
355  * @netdev: pointer the associated netdevice
356  *
357  * Creates fc_lport struct and scsi_host for lport, configures lport
358  * and starts fabric login.
359  *
360  * Returns : 0 on success
361  */
362 static int fcoe_sw_create(struct net_device *netdev)
363 {
364         int rc;
365         struct fc_lport *lp = NULL;
366         struct fcoe_softc *fc;
367         struct Scsi_Host *shost;
368
369         BUG_ON(!netdev);
370
371         printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
372                netdev->name);
373
374         lp = fcoe_hostlist_lookup(netdev);
375         if (lp)
376                 return -EEXIST;
377
378         shost = fcoe_host_alloc(&fcoe_sw_shost_template,
379                                 sizeof(struct fcoe_softc));
380         if (!shost) {
381                 FC_DBG("Could not allocate host structure\n");
382                 return -ENOMEM;
383         }
384         lp = shost_priv(shost);
385         fc = lport_priv(lp);
386
387         /* configure fc_lport, e.g., em */
388         rc = fcoe_sw_lport_config(lp);
389         if (rc) {
390                 FC_DBG("Could not configure lport\n");
391                 goto out_host_put;
392         }
393
394         /* configure lport network properties */
395         rc = fcoe_sw_netdev_config(lp, netdev);
396         if (rc) {
397                 FC_DBG("Could not configure netdev for lport\n");
398                 goto out_host_put;
399         }
400
401         /* configure lport scsi host properties */
402         rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
403         if (rc) {
404                 FC_DBG("Could not configure shost for lport\n");
405                 goto out_host_put;
406         }
407
408         /* lport exch manager allocation */
409         rc = fcoe_sw_em_config(lp);
410         if (rc) {
411                 FC_DBG("Could not configure em for lport\n");
412                 goto out_host_put;
413         }
414
415         /* Initialize the library */
416         rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
417         if (rc) {
418                 FC_DBG("Could not configure libfc for lport!\n");
419                 goto out_lp_destroy;
420         }
421
422         /* add to lports list */
423         fcoe_hostlist_add(lp);
424
425         lp->boot_time = jiffies;
426
427         fc_fabric_login(lp);
428
429         dev_hold(netdev);
430
431         return rc;
432
433 out_lp_destroy:
434         fc_exch_mgr_free(lp->emp); /* Free the EM */
435 out_host_put:
436         scsi_host_put(lp->host);
437         return rc;
438 }
439
440 /**
441  * fcoe_sw_match() - The FCoE SW transport match function
442  *
443  * Returns : false always
444  */
445 static bool fcoe_sw_match(struct net_device *netdev)
446 {
447         /* FIXME - for sw transport, always return false */
448         return false;
449 }
450
451 /* the sw hba fcoe transport */
452 struct fcoe_transport fcoe_sw_transport = {
453         .name = "fcoesw",
454         .create = fcoe_sw_create,
455         .destroy = fcoe_sw_destroy,
456         .match = fcoe_sw_match,
457         .vendor = 0x0,
458         .device = 0xffff,
459 };
460
461 /**
462  * fcoe_sw_init() - Registers fcoe_sw_transport
463  *
464  * Returns : 0 on success
465  */
466 int __init fcoe_sw_init(void)
467 {
468         /* attach to scsi transport */
469         scsi_transport_fcoe_sw =
470                 fc_attach_transport(&fcoe_sw_transport_function);
471
472         if (!scsi_transport_fcoe_sw) {
473                 printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
474                 return -ENODEV;
475         }
476
477         mutex_init(&fcoe_sw_transport.devlock);
478         INIT_LIST_HEAD(&fcoe_sw_transport.devlist);
479
480         /* register sw transport */
481         fcoe_transport_register(&fcoe_sw_transport);
482         return 0;
483 }
484
485 /**
486  * fcoe_sw_exit() - Unregisters fcoe_sw_transport
487  *
488  * Returns : 0 on success
489  */
490 int __exit fcoe_sw_exit(void)
491 {
492         /* dettach the transport */
493         fc_release_transport(scsi_transport_fcoe_sw);
494         fcoe_transport_unregister(&fcoe_sw_transport);
495         return 0;
496 }