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