blob: df91a61591b2e3d57e03868927d1c9b433f730af [file] [log] [blame]
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18#include <linux/module.h>
19#include <linux/mempool.h>
20#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070022#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/pci.h>
25#include <linux/skbuff.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/workqueue.h>
Joe Eykholt78112e52009-11-03 11:49:22 -080029#include <linux/if_ether.h>
30#include <scsi/fc/fc_fip.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070031#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_fc.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/libfc.h>
36#include <scsi/fc_frame.h>
37
38#include "vnic_dev.h"
39#include "vnic_intr.h"
40#include "vnic_stats.h"
41#include "fnic_io.h"
42#include "fnic.h"
43
44#define PCI_DEVICE_ID_CISCO_FNIC 0x0045
45
46/* Timer to poll notification area for events. Used for MSI interrupts */
47#define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
48
49static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
50static struct kmem_cache *fnic_io_req_cache;
51LIST_HEAD(fnic_list);
52DEFINE_SPINLOCK(fnic_list_lock);
53
54/* Supported devices by fnic module */
55static struct pci_device_id fnic_id_table[] = {
56 { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
57 { 0, }
58};
59
60MODULE_DESCRIPTION(DRV_DESCRIPTION);
61MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
62 "Joseph R. Eykholt <jeykholt@cisco.com>");
63MODULE_LICENSE("GPL v2");
64MODULE_VERSION(DRV_VERSION);
65MODULE_DEVICE_TABLE(pci, fnic_id_table);
66
67unsigned int fnic_log_level;
68module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
69MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
70
71
72static struct libfc_function_template fnic_transport_template = {
73 .frame_send = fnic_send,
Joe Eykholt78112e52009-11-03 11:49:22 -080074 .lport_set_port_id = fnic_set_port_id,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070075 .fcp_abort_io = fnic_empty_scsi_cleanup,
76 .fcp_cleanup = fnic_empty_scsi_cleanup,
77 .exch_mgr_reset = fnic_exch_mgr_reset
78};
79
80static int fnic_slave_alloc(struct scsi_device *sdev)
81{
82 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070083
84 sdev->tagged_supported = 1;
85
86 if (!rport || fc_remote_port_chkready(rport))
87 return -ENXIO;
88
89 scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070090 return 0;
91}
92
93static struct scsi_host_template fnic_host_template = {
94 .module = THIS_MODULE,
95 .name = DRV_NAME,
96 .queuecommand = fnic_queuecommand,
97 .eh_abort_handler = fnic_abort_cmd,
98 .eh_device_reset_handler = fnic_device_reset,
99 .eh_host_reset_handler = fnic_host_reset,
100 .slave_alloc = fnic_slave_alloc,
101 .change_queue_depth = fc_change_queue_depth,
102 .change_queue_type = fc_change_queue_type,
103 .this_id = -1,
104 .cmd_per_lun = 3,
105 .can_queue = FNIC_MAX_IO_REQ,
106 .use_clustering = ENABLE_CLUSTERING,
107 .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
108 .max_sectors = 0xffff,
109 .shost_attrs = fnic_attrs,
110};
111
Mike Christie8196a932010-08-06 03:02:40 -0500112static void
113fnic_get_host_def_loss_tmo(struct Scsi_Host *shost)
114{
115 struct fc_lport *lp = shost_priv(shost);
116 struct fnic *fnic = lport_priv(lp);
117
118 fc_host_def_dev_loss_tmo(shost) = fnic->config.port_down_timeout / 1000;
119}
120
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700121static void fnic_get_host_speed(struct Scsi_Host *shost);
122static struct scsi_transport_template *fnic_fc_transport;
123static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
124
125static struct fc_function_template fnic_fc_functions = {
126
127 .show_host_node_name = 1,
128 .show_host_port_name = 1,
129 .show_host_supported_classes = 1,
130 .show_host_supported_fc4s = 1,
131 .show_host_active_fc4s = 1,
132 .show_host_maxframe_size = 1,
133 .show_host_port_id = 1,
134 .show_host_supported_speeds = 1,
135 .get_host_speed = fnic_get_host_speed,
136 .show_host_speed = 1,
137 .show_host_port_type = 1,
138 .get_host_port_state = fc_get_host_port_state,
139 .show_host_port_state = 1,
140 .show_host_symbolic_name = 1,
141 .show_rport_maxframe_size = 1,
142 .show_rport_supported_classes = 1,
143 .show_host_fabric_name = 1,
144 .show_starget_node_name = 1,
145 .show_starget_port_name = 1,
146 .show_starget_port_id = 1,
147 .show_rport_dev_loss_tmo = 1,
148 .issue_fc_host_lip = fnic_reset,
149 .get_fc_host_stats = fnic_get_stats,
Mike Christie8196a932010-08-06 03:02:40 -0500150 .get_host_def_dev_loss_tmo = fnic_get_host_def_loss_tmo,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700151 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
152 .terminate_rport_io = fnic_terminate_rport_io,
Joe Eykholt76d87372009-11-03 11:49:32 -0800153 .bsg_request = fc_lport_bsg_request,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700154};
155
156static void fnic_get_host_speed(struct Scsi_Host *shost)
157{
158 struct fc_lport *lp = shost_priv(shost);
159 struct fnic *fnic = lport_priv(lp);
160 u32 port_speed = vnic_dev_port_speed(fnic->vdev);
161
162 /* Add in other values as they get defined in fw */
163 switch (port_speed) {
164 case 10000:
165 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
166 break;
167 default:
168 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
169 break;
170 }
171}
172
173static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
174{
175 int ret;
176 struct fc_lport *lp = shost_priv(host);
177 struct fnic *fnic = lport_priv(lp);
178 struct fc_host_statistics *stats = &lp->host_stats;
179 struct vnic_stats *vs;
180 unsigned long flags;
181
182 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
183 return stats;
184 fnic->stats_time = jiffies;
185
186 spin_lock_irqsave(&fnic->fnic_lock, flags);
187 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
188 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
189
190 if (ret) {
191 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
192 "fnic: Get vnic stats failed"
193 " 0x%x", ret);
194 return stats;
195 }
196 vs = fnic->stats;
197 stats->tx_frames = vs->tx.tx_unicast_frames_ok;
198 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
199 stats->rx_frames = vs->rx.rx_unicast_frames_ok;
200 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
201 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
202 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
203 stats->invalid_crc_count = vs->rx.rx_crc_errors;
204 stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ;
205 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
206 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
207
208 return stats;
209}
210
211void fnic_log_q_error(struct fnic *fnic)
212{
213 unsigned int i;
214 u32 error_status;
215
216 for (i = 0; i < fnic->raw_wq_count; i++) {
217 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
218 if (error_status)
219 shost_printk(KERN_ERR, fnic->lport->host,
220 "WQ[%d] error_status"
221 " %d\n", i, error_status);
222 }
223
224 for (i = 0; i < fnic->rq_count; i++) {
225 error_status = ioread32(&fnic->rq[i].ctrl->error_status);
226 if (error_status)
227 shost_printk(KERN_ERR, fnic->lport->host,
228 "RQ[%d] error_status"
229 " %d\n", i, error_status);
230 }
231
232 for (i = 0; i < fnic->wq_copy_count; i++) {
233 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
234 if (error_status)
235 shost_printk(KERN_ERR, fnic->lport->host,
236 "CWQ[%d] error_status"
237 " %d\n", i, error_status);
238 }
239}
240
241void fnic_handle_link_event(struct fnic *fnic)
242{
243 unsigned long flags;
244
245 spin_lock_irqsave(&fnic->fnic_lock, flags);
246 if (fnic->stop_rx_link_events) {
247 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
248 return;
249 }
250 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
251
252 queue_work(fnic_event_queue, &fnic->link_work);
253
254}
255
256static int fnic_notify_set(struct fnic *fnic)
257{
258 int err;
259
260 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
261 case VNIC_DEV_INTR_MODE_INTX:
262 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
263 break;
264 case VNIC_DEV_INTR_MODE_MSI:
265 err = vnic_dev_notify_set(fnic->vdev, -1);
266 break;
267 case VNIC_DEV_INTR_MODE_MSIX:
268 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
269 break;
270 default:
271 shost_printk(KERN_ERR, fnic->lport->host,
272 "Interrupt mode should be set up"
273 " before devcmd notify set %d\n",
274 vnic_dev_get_intr_mode(fnic->vdev));
275 err = -1;
276 break;
277 }
278
279 return err;
280}
281
282static void fnic_notify_timer(unsigned long data)
283{
284 struct fnic *fnic = (struct fnic *)data;
285
286 fnic_handle_link_event(fnic);
287 mod_timer(&fnic->notify_timer,
288 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
289}
290
291static void fnic_notify_timer_start(struct fnic *fnic)
292{
293 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
294 case VNIC_DEV_INTR_MODE_MSI:
295 /*
296 * Schedule first timeout immediately. The driver is
297 * initiatialized and ready to look for link up notification
298 */
299 mod_timer(&fnic->notify_timer, jiffies);
300 break;
301 default:
302 /* Using intr for notification for INTx/MSI-X */
303 break;
304 };
305}
306
307static int fnic_dev_wait(struct vnic_dev *vdev,
308 int (*start)(struct vnic_dev *, int),
309 int (*finished)(struct vnic_dev *, int *),
310 int arg)
311{
312 unsigned long time;
313 int done;
314 int err;
315
316 err = start(vdev, arg);
317 if (err)
318 return err;
319
320 /* Wait for func to complete...2 seconds max */
321 time = jiffies + (HZ * 2);
322 do {
323 err = finished(vdev, &done);
324 if (err)
325 return err;
326 if (done)
327 return 0;
328 schedule_timeout_uninterruptible(HZ / 10);
329 } while (time_after(time, jiffies));
330
331 return -ETIMEDOUT;
332}
333
334static int fnic_cleanup(struct fnic *fnic)
335{
336 unsigned int i;
337 int err;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700338
339 vnic_dev_disable(fnic->vdev);
340 for (i = 0; i < fnic->intr_count; i++)
341 vnic_intr_mask(&fnic->intr[i]);
342
343 for (i = 0; i < fnic->rq_count; i++) {
344 err = vnic_rq_disable(&fnic->rq[i]);
345 if (err)
346 return err;
347 }
348 for (i = 0; i < fnic->raw_wq_count; i++) {
349 err = vnic_wq_disable(&fnic->wq[i]);
350 if (err)
351 return err;
352 }
353 for (i = 0; i < fnic->wq_copy_count; i++) {
354 err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
355 if (err)
356 return err;
357 }
358
359 /* Clean up completed IOs and FCS frames */
360 fnic_wq_copy_cmpl_handler(fnic, -1);
361 fnic_wq_cmpl_handler(fnic, -1);
362 fnic_rq_cmpl_handler(fnic, -1);
363
364 /* Clean up the IOs and FCS frames that have not completed */
365 for (i = 0; i < fnic->raw_wq_count; i++)
366 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
367 for (i = 0; i < fnic->rq_count; i++)
368 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
369 for (i = 0; i < fnic->wq_copy_count; i++)
370 vnic_wq_copy_clean(&fnic->wq_copy[i],
371 fnic_wq_copy_cleanup_handler);
372
373 for (i = 0; i < fnic->cq_count; i++)
374 vnic_cq_clean(&fnic->cq[i]);
375 for (i = 0; i < fnic->intr_count; i++)
376 vnic_intr_clean(&fnic->intr[i]);
377
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700378 mempool_destroy(fnic->io_req_pool);
379 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
380 mempool_destroy(fnic->io_sgl_pool[i]);
381
382 return 0;
383}
384
385static void fnic_iounmap(struct fnic *fnic)
386{
387 if (fnic->bar0.vaddr)
388 iounmap(fnic->bar0.vaddr);
389}
390
391/*
392 * Allocate element for mempools requiring GFP_DMA flag.
393 * Otherwise, checks in kmem_flagcheck() hit BUG_ON().
394 */
395static void *fnic_alloc_slab_dma(gfp_t gfp_mask, void *pool_data)
396{
397 struct kmem_cache *mem = pool_data;
398
399 return kmem_cache_alloc(mem, gfp_mask | GFP_ATOMIC | GFP_DMA);
400}
401
Joe Eykholt78112e52009-11-03 11:49:22 -0800402/**
403 * fnic_get_mac() - get assigned data MAC address for FIP code.
404 * @lport: local port.
405 */
406static u8 *fnic_get_mac(struct fc_lport *lport)
407{
408 struct fnic *fnic = lport_priv(lport);
409
410 return fnic->data_src_addr;
411}
412
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700413static int __devinit fnic_probe(struct pci_dev *pdev,
414 const struct pci_device_id *ent)
415{
416 struct Scsi_Host *host;
417 struct fc_lport *lp;
418 struct fnic *fnic;
419 mempool_t *pool;
420 int err;
421 int i;
422 unsigned long flags;
423
424 /*
425 * Allocate SCSI Host and set up association between host,
426 * local port, and fnic
427 */
Chris Leech86221962009-11-03 11:46:08 -0800428 lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
429 if (!lp) {
430 printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700431 err = -ENOMEM;
432 goto err_out;
433 }
Chris Leech86221962009-11-03 11:46:08 -0800434 host = lp->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700435 fnic = lport_priv(lp);
436 fnic->lport = lp;
Joe Eykholt78112e52009-11-03 11:49:22 -0800437 fnic->ctlr.lp = lp;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700438
439 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
440 host->host_no);
441
442 host->transportt = fnic_fc_transport;
443
444 err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ);
445 if (err) {
446 shost_printk(KERN_ERR, fnic->lport->host,
447 "Unable to alloc shared tag map\n");
448 goto err_out_free_hba;
449 }
450
451 /* Setup PCI resources */
452 pci_set_drvdata(pdev, fnic);
453
454 fnic->pdev = pdev;
455
456 err = pci_enable_device(pdev);
457 if (err) {
458 shost_printk(KERN_ERR, fnic->lport->host,
459 "Cannot enable PCI device, aborting.\n");
460 goto err_out_free_hba;
461 }
462
463 err = pci_request_regions(pdev, DRV_NAME);
464 if (err) {
465 shost_printk(KERN_ERR, fnic->lport->host,
466 "Cannot enable PCI resources, aborting\n");
467 goto err_out_disable_device;
468 }
469
470 pci_set_master(pdev);
471
472 /* Query PCI controller on system for DMA addressing
473 * limitation for the device. Try 40-bit first, and
474 * fail to 32-bit.
475 */
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700476 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700477 if (err) {
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700478 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700479 if (err) {
480 shost_printk(KERN_ERR, fnic->lport->host,
481 "No usable DMA configuration "
482 "aborting\n");
483 goto err_out_release_regions;
484 }
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700485 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700486 if (err) {
487 shost_printk(KERN_ERR, fnic->lport->host,
488 "Unable to obtain 32-bit DMA "
489 "for consistent allocations, aborting.\n");
490 goto err_out_release_regions;
491 }
492 } else {
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700493 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700494 if (err) {
495 shost_printk(KERN_ERR, fnic->lport->host,
496 "Unable to obtain 40-bit DMA "
497 "for consistent allocations, aborting.\n");
498 goto err_out_release_regions;
499 }
500 }
501
502 /* Map vNIC resources from BAR0 */
503 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
504 shost_printk(KERN_ERR, fnic->lport->host,
505 "BAR0 not memory-map'able, aborting.\n");
506 err = -ENODEV;
507 goto err_out_release_regions;
508 }
509
510 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
511 fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
512 fnic->bar0.len = pci_resource_len(pdev, 0);
513
514 if (!fnic->bar0.vaddr) {
515 shost_printk(KERN_ERR, fnic->lport->host,
516 "Cannot memory-map BAR0 res hdr, "
517 "aborting.\n");
518 err = -ENODEV;
519 goto err_out_release_regions;
520 }
521
522 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
523 if (!fnic->vdev) {
524 shost_printk(KERN_ERR, fnic->lport->host,
525 "vNIC registration failed, "
526 "aborting.\n");
527 err = -ENODEV;
528 goto err_out_iounmap;
529 }
530
531 err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
532 vnic_dev_open_done, 0);
533 if (err) {
534 shost_printk(KERN_ERR, fnic->lport->host,
535 "vNIC dev open failed, aborting.\n");
536 goto err_out_vnic_unregister;
537 }
538
539 err = vnic_dev_init(fnic->vdev, 0);
540 if (err) {
541 shost_printk(KERN_ERR, fnic->lport->host,
542 "vNIC dev init failed, aborting.\n");
543 goto err_out_dev_close;
544 }
545
Joe Eykholt78112e52009-11-03 11:49:22 -0800546 err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700547 if (err) {
548 shost_printk(KERN_ERR, fnic->lport->host,
549 "vNIC get MAC addr failed \n");
550 goto err_out_dev_close;
551 }
Joe Eykholt78112e52009-11-03 11:49:22 -0800552 /* set data_src for point-to-point mode and to keep it non-zero */
553 memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700554
555 /* Get vNIC configuration */
556 err = fnic_get_vnic_config(fnic);
557 if (err) {
558 shost_printk(KERN_ERR, fnic->lport->host,
559 "Get vNIC configuration failed, "
560 "aborting.\n");
561 goto err_out_dev_close;
562 }
563 host->max_lun = fnic->config.luns_per_tgt;
564 host->max_id = FNIC_MAX_FCP_TARGET;
Vasu Devda87bfa2010-04-09 14:22:59 -0700565 host->max_cmd_len = FCOE_MAX_CMD_LEN;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700566
567 fnic_get_res_counts(fnic);
568
569 err = fnic_set_intr_mode(fnic);
570 if (err) {
571 shost_printk(KERN_ERR, fnic->lport->host,
572 "Failed to set intr mode, "
573 "aborting.\n");
574 goto err_out_dev_close;
575 }
576
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700577 err = fnic_alloc_vnic_resources(fnic);
578 if (err) {
579 shost_printk(KERN_ERR, fnic->lport->host,
580 "Failed to alloc vNIC resources, "
581 "aborting.\n");
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800582 goto err_out_clear_intr;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700583 }
584
585
586 /* initialize all fnic locks */
587 spin_lock_init(&fnic->fnic_lock);
588
589 for (i = 0; i < FNIC_WQ_MAX; i++)
590 spin_lock_init(&fnic->wq_lock[i]);
591
592 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
593 spin_lock_init(&fnic->wq_copy_lock[i]);
594 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
595 fnic->fw_ack_recd[i] = 0;
596 fnic->fw_ack_index[i] = -1;
597 }
598
599 for (i = 0; i < FNIC_IO_LOCKS; i++)
600 spin_lock_init(&fnic->io_req_lock[i]);
601
602 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
603 if (!fnic->io_req_pool)
604 goto err_out_free_resources;
605
606 pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab,
607 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
608 if (!pool)
609 goto err_out_free_ioreq_pool;
610 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
611
612 pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab,
613 fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
614 if (!pool)
615 goto err_out_free_dflt_pool;
616 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
617
618 /* setup vlan config, hw inserts vlan header */
619 fnic->vlan_hw_insert = 1;
620 fnic->vlan_id = 0;
621
Joe Eykholt78112e52009-11-03 11:49:22 -0800622 /* Initialize the FIP fcoe_ctrl struct */
623 fnic->ctlr.send = fnic_eth_send;
624 fnic->ctlr.update_mac = fnic_update_mac;
625 fnic->ctlr.get_src_addr = fnic_get_mac;
Joe Eykholt78112e52009-11-03 11:49:22 -0800626 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
627 shost_printk(KERN_INFO, fnic->lport->host,
628 "firmware supports FIP\n");
Venkata Siva Vijayendra Bhamidipatiaaa5e562010-02-16 12:15:44 -0800629 /* enable directed and multicast */
630 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
Joe Eykholt78112e52009-11-03 11:49:22 -0800631 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
632 vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700633 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
Joe Eykholt78112e52009-11-03 11:49:22 -0800634 } else {
635 shost_printk(KERN_INFO, fnic->lport->host,
636 "firmware uses non-FIP mode\n");
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700637 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
Joe Eykholt78112e52009-11-03 11:49:22 -0800638 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700639 fnic->state = FNIC_IN_FC_MODE;
640
641 /* Enable hardware stripping of vlan header on ingress */
642 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
643
644 /* Setup notification buffer area */
645 err = fnic_notify_set(fnic);
646 if (err) {
647 shost_printk(KERN_ERR, fnic->lport->host,
648 "Failed to alloc notify buffer, aborting.\n");
649 goto err_out_free_max_pool;
650 }
651
652 /* Setup notify timer when using MSI interrupts */
653 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
654 setup_timer(&fnic->notify_timer,
655 fnic_notify_timer, (unsigned long)fnic);
656
657 /* allocate RQ buffers and post them to RQ*/
658 for (i = 0; i < fnic->rq_count; i++) {
659 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
660 if (err) {
661 shost_printk(KERN_ERR, fnic->lport->host,
662 "fnic_alloc_rq_frame can't alloc "
663 "frame\n");
664 goto err_out_free_rq_buf;
665 }
666 }
667
668 /*
669 * Initialization done with PCI system, hardware, firmware.
670 * Add host to SCSI
671 */
672 err = scsi_add_host(lp->host, &pdev->dev);
673 if (err) {
674 shost_printk(KERN_ERR, fnic->lport->host,
675 "fnic: scsi_add_host failed...exiting\n");
676 goto err_out_free_rq_buf;
677 }
678
679 /* Start local port initiatialization */
680
681 lp->link_up = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700682
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700683 lp->max_retry_count = fnic->config.flogi_retries;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700684 lp->max_rport_retry_count = fnic->config.plogi_retries;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700685 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
686 FCP_SPPF_CONF_COMPL);
687 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
688 lp->service_params |= FCP_SPPF_RETRY;
689
690 lp->boot_time = jiffies;
691 lp->e_d_tov = fnic->config.ed_tov;
692 lp->r_a_tov = fnic->config.ra_tov;
693 lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
694 fc_set_wwnn(lp, fnic->config.node_wwn);
695 fc_set_wwpn(lp, fnic->config.port_wwn);
696
Joe Eykholte10f8c62010-07-20 15:20:30 -0700697 fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700698
Vasu Dev52ff8782009-07-29 17:05:10 -0700699 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
700 FCPIO_HOST_EXCH_RANGE_END, NULL)) {
701 err = -ENOMEM;
702 goto err_out_remove_scsi_host;
703 }
704
Venkata Siva Vijayendra Bhamidipatic693a712010-02-16 12:15:50 -0800705 fc_lport_init_stats(lp);
706
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700707 fc_lport_config(lp);
708
709 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
710 sizeof(struct fc_frame_header))) {
711 err = -EINVAL;
712 goto err_out_free_exch_mgr;
713 }
714 fc_host_maxframe_size(lp->host) = lp->mfs;
715
716 sprintf(fc_host_symbolic_name(lp->host),
717 DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
718
719 spin_lock_irqsave(&fnic_list_lock, flags);
720 list_add_tail(&fnic->list, &fnic_list);
721 spin_unlock_irqrestore(&fnic_list_lock, flags);
722
723 INIT_WORK(&fnic->link_work, fnic_handle_link);
724 INIT_WORK(&fnic->frame_work, fnic_handle_frame);
725 skb_queue_head_init(&fnic->frame_queue);
Joe Eykholt78112e52009-11-03 11:49:22 -0800726 skb_queue_head_init(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700727
728 /* Enable all queues */
729 for (i = 0; i < fnic->raw_wq_count; i++)
730 vnic_wq_enable(&fnic->wq[i]);
731 for (i = 0; i < fnic->rq_count; i++)
732 vnic_rq_enable(&fnic->rq[i]);
733 for (i = 0; i < fnic->wq_copy_count; i++)
734 vnic_wq_copy_enable(&fnic->wq_copy[i]);
735
736 fc_fabric_login(lp);
737
738 vnic_dev_enable(fnic->vdev);
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800739
740 err = fnic_request_intr(fnic);
741 if (err) {
742 shost_printk(KERN_ERR, fnic->lport->host,
743 "Unable to request irq.\n");
744 goto err_out_free_exch_mgr;
745 }
746
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700747 for (i = 0; i < fnic->intr_count; i++)
748 vnic_intr_unmask(&fnic->intr[i]);
749
750 fnic_notify_timer_start(fnic);
751
752 return 0;
753
754err_out_free_exch_mgr:
Vasu Dev52ff8782009-07-29 17:05:10 -0700755 fc_exch_mgr_free(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700756err_out_remove_scsi_host:
Joe Eykholt78112e52009-11-03 11:49:22 -0800757 fc_remove_host(lp->host);
758 scsi_remove_host(lp->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700759err_out_free_rq_buf:
760 for (i = 0; i < fnic->rq_count; i++)
761 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
762 vnic_dev_notify_unset(fnic->vdev);
763err_out_free_max_pool:
764 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
765err_out_free_dflt_pool:
766 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
767err_out_free_ioreq_pool:
768 mempool_destroy(fnic->io_req_pool);
769err_out_free_resources:
770 fnic_free_vnic_resources(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700771err_out_clear_intr:
772 fnic_clear_intr_mode(fnic);
773err_out_dev_close:
774 vnic_dev_close(fnic->vdev);
775err_out_vnic_unregister:
776 vnic_dev_unregister(fnic->vdev);
777err_out_iounmap:
778 fnic_iounmap(fnic);
779err_out_release_regions:
780 pci_release_regions(pdev);
781err_out_disable_device:
782 pci_disable_device(pdev);
783err_out_free_hba:
784 scsi_host_put(lp->host);
785err_out:
786 return err;
787}
788
789static void __devexit fnic_remove(struct pci_dev *pdev)
790{
791 struct fnic *fnic = pci_get_drvdata(pdev);
Joe Eykholt78112e52009-11-03 11:49:22 -0800792 struct fc_lport *lp = fnic->lport;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700793 unsigned long flags;
794
795 /*
796 * Mark state so that the workqueue thread stops forwarding
797 * received frames and link events to the local port. ISR and
798 * other threads that can queue work items will also stop
799 * creating work items on the fnic workqueue
800 */
801 spin_lock_irqsave(&fnic->fnic_lock, flags);
802 fnic->stop_rx_link_events = 1;
803 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
804
805 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
806 del_timer_sync(&fnic->notify_timer);
807
808 /*
809 * Flush the fnic event queue. After this call, there should
810 * be no event queued for this fnic device in the workqueue
811 */
812 flush_workqueue(fnic_event_queue);
813 skb_queue_purge(&fnic->frame_queue);
Joe Eykholt78112e52009-11-03 11:49:22 -0800814 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700815
816 /*
817 * Log off the fabric. This stops all remote ports, dns port,
818 * logs off the fabric. This flushes all rport, disc, lport work
819 * before returning
820 */
821 fc_fabric_logoff(fnic->lport);
822
823 spin_lock_irqsave(&fnic->fnic_lock, flags);
824 fnic->in_remove = 1;
825 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
826
Joe Eykholt78112e52009-11-03 11:49:22 -0800827 fcoe_ctlr_destroy(&fnic->ctlr);
828 fc_lport_destroy(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700829
830 /*
831 * This stops the fnic device, masks all interrupts. Completed
832 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
833 * cleaned up
834 */
835 fnic_cleanup(fnic);
836
837 BUG_ON(!skb_queue_empty(&fnic->frame_queue));
Joe Eykholt78112e52009-11-03 11:49:22 -0800838 BUG_ON(!skb_queue_empty(&fnic->tx_queue));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700839
840 spin_lock_irqsave(&fnic_list_lock, flags);
841 list_del(&fnic->list);
842 spin_unlock_irqrestore(&fnic_list_lock, flags);
843
844 fc_remove_host(fnic->lport->host);
845 scsi_remove_host(fnic->lport->host);
Vasu Dev52ff8782009-07-29 17:05:10 -0700846 fc_exch_mgr_free(fnic->lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700847 vnic_dev_notify_unset(fnic->vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700848 fnic_free_intr(fnic);
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800849 fnic_free_vnic_resources(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700850 fnic_clear_intr_mode(fnic);
851 vnic_dev_close(fnic->vdev);
852 vnic_dev_unregister(fnic->vdev);
853 fnic_iounmap(fnic);
854 pci_release_regions(pdev);
855 pci_disable_device(pdev);
856 pci_set_drvdata(pdev, NULL);
Joe Eykholt78112e52009-11-03 11:49:22 -0800857 scsi_host_put(lp->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700858}
859
860static struct pci_driver fnic_driver = {
861 .name = DRV_NAME,
862 .id_table = fnic_id_table,
863 .probe = fnic_probe,
864 .remove = __devexit_p(fnic_remove),
865};
866
867static int __init fnic_init_module(void)
868{
869 size_t len;
870 int err = 0;
871
872 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
873
874 /* Create a cache for allocation of default size sgls */
875 len = sizeof(struct fnic_dflt_sgl_list);
876 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
877 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
878 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA,
879 NULL);
880 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
881 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
882 err = -ENOMEM;
883 goto err_create_fnic_sgl_slab_dflt;
884 }
885
886 /* Create a cache for allocation of max size sgls*/
887 len = sizeof(struct fnic_sgl_list);
888 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
889 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
890 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA,
891 NULL);
892 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
893 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
894 err = -ENOMEM;
895 goto err_create_fnic_sgl_slab_max;
896 }
897
898 /* Create a cache of io_req structs for use via mempool */
899 fnic_io_req_cache = kmem_cache_create("fnic_io_req",
900 sizeof(struct fnic_io_req),
901 0, SLAB_HWCACHE_ALIGN, NULL);
902 if (!fnic_io_req_cache) {
903 printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
904 err = -ENOMEM;
905 goto err_create_fnic_ioreq_slab;
906 }
907
908 fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
909 if (!fnic_event_queue) {
910 printk(KERN_ERR PFX "fnic work queue create failed\n");
911 err = -ENOMEM;
912 goto err_create_fnic_workq;
913 }
914
915 spin_lock_init(&fnic_list_lock);
916 INIT_LIST_HEAD(&fnic_list);
917
918 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
919 if (!fnic_fc_transport) {
920 printk(KERN_ERR PFX "fc_attach_transport error\n");
921 err = -ENOMEM;
922 goto err_fc_transport;
923 }
924
925 /* register the driver with PCI system */
926 err = pci_register_driver(&fnic_driver);
927 if (err < 0) {
928 printk(KERN_ERR PFX "pci register error\n");
929 goto err_pci_register;
930 }
931 return err;
932
933err_pci_register:
934 fc_release_transport(fnic_fc_transport);
935err_fc_transport:
936 destroy_workqueue(fnic_event_queue);
937err_create_fnic_workq:
938 kmem_cache_destroy(fnic_io_req_cache);
939err_create_fnic_ioreq_slab:
940 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
941err_create_fnic_sgl_slab_max:
942 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
943err_create_fnic_sgl_slab_dflt:
944 return err;
945}
946
947static void __exit fnic_cleanup_module(void)
948{
949 pci_unregister_driver(&fnic_driver);
950 destroy_workqueue(fnic_event_queue);
951 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
952 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
953 kmem_cache_destroy(fnic_io_req_cache);
954 fc_release_transport(fnic_fc_transport);
955}
956
957module_init(fnic_init_module);
958module_exit(fnic_cleanup_module);
959