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