1 /* 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> 21 #include <linux/errno.h> 22 #include <linux/init.h> 23 #include <linux/pci.h> 24 #include <linux/skbuff.h> 25 #include <linux/interrupt.h> 26 #include <linux/spinlock.h> 27 #include <linux/workqueue.h> 28 #include <scsi/scsi_host.h> 29 #include <scsi/scsi_transport.h> 30 #include <scsi/scsi_transport_fc.h> 31 #include <scsi/scsi_tcq.h> 32 #include <scsi/libfc.h> 33 #include <scsi/fc_frame.h> 34 35 #include "vnic_dev.h" 36 #include "vnic_intr.h" 37 #include "vnic_stats.h" 38 #include "fnic_io.h" 39 #include "fnic.h" 40 41 #define PCI_DEVICE_ID_CISCO_FNIC 0x0045 42 43 /* Timer to poll notification area for events. Used for MSI interrupts */ 44 #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ) 45 46 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES]; 47 static struct kmem_cache *fnic_io_req_cache; 48 LIST_HEAD(fnic_list); 49 DEFINE_SPINLOCK(fnic_list_lock); 50 51 /* Supported devices by fnic module */ 52 static struct pci_device_id fnic_id_table[] = { 53 { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) }, 54 { 0, } 55 }; 56 57 MODULE_DESCRIPTION(DRV_DESCRIPTION); 58 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, " 59 "Joseph R. Eykholt <jeykholt@cisco.com>"); 60 MODULE_LICENSE("GPL v2"); 61 MODULE_VERSION(DRV_VERSION); 62 MODULE_DEVICE_TABLE(pci, fnic_id_table); 63 64 unsigned int fnic_log_level; 65 module_param(fnic_log_level, int, S_IRUGO|S_IWUSR); 66 MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels"); 67 68 69 static struct libfc_function_template fnic_transport_template = { 70 .frame_send = fnic_send, 71 .fcp_abort_io = fnic_empty_scsi_cleanup, 72 .fcp_cleanup = fnic_empty_scsi_cleanup, 73 .exch_mgr_reset = fnic_exch_mgr_reset 74 }; 75 76 static int fnic_slave_alloc(struct scsi_device *sdev) 77 { 78 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 79 struct fc_lport *lp = shost_priv(sdev->host); 80 struct fnic *fnic = lport_priv(lp); 81 82 sdev->tagged_supported = 1; 83 84 if (!rport || fc_remote_port_chkready(rport)) 85 return -ENXIO; 86 87 scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH); 88 rport->dev_loss_tmo = fnic->config.port_down_timeout / 1000; 89 90 return 0; 91 } 92 93 static 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 112 static void fnic_get_host_speed(struct Scsi_Host *shost); 113 static struct scsi_transport_template *fnic_fc_transport; 114 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *); 115 116 static struct fc_function_template fnic_fc_functions = { 117 118 .show_host_node_name = 1, 119 .show_host_port_name = 1, 120 .show_host_supported_classes = 1, 121 .show_host_supported_fc4s = 1, 122 .show_host_active_fc4s = 1, 123 .show_host_maxframe_size = 1, 124 .show_host_port_id = 1, 125 .show_host_supported_speeds = 1, 126 .get_host_speed = fnic_get_host_speed, 127 .show_host_speed = 1, 128 .show_host_port_type = 1, 129 .get_host_port_state = fc_get_host_port_state, 130 .show_host_port_state = 1, 131 .show_host_symbolic_name = 1, 132 .show_rport_maxframe_size = 1, 133 .show_rport_supported_classes = 1, 134 .show_host_fabric_name = 1, 135 .show_starget_node_name = 1, 136 .show_starget_port_name = 1, 137 .show_starget_port_id = 1, 138 .show_rport_dev_loss_tmo = 1, 139 .issue_fc_host_lip = fnic_reset, 140 .get_fc_host_stats = fnic_get_stats, 141 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv), 142 .terminate_rport_io = fnic_terminate_rport_io, 143 }; 144 145 static void fnic_get_host_speed(struct Scsi_Host *shost) 146 { 147 struct fc_lport *lp = shost_priv(shost); 148 struct fnic *fnic = lport_priv(lp); 149 u32 port_speed = vnic_dev_port_speed(fnic->vdev); 150 151 /* Add in other values as they get defined in fw */ 152 switch (port_speed) { 153 case 10000: 154 fc_host_speed(shost) = FC_PORTSPEED_10GBIT; 155 break; 156 default: 157 fc_host_speed(shost) = FC_PORTSPEED_10GBIT; 158 break; 159 } 160 } 161 162 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host) 163 { 164 int ret; 165 struct fc_lport *lp = shost_priv(host); 166 struct fnic *fnic = lport_priv(lp); 167 struct fc_host_statistics *stats = &lp->host_stats; 168 struct vnic_stats *vs; 169 unsigned long flags; 170 171 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT)) 172 return stats; 173 fnic->stats_time = jiffies; 174 175 spin_lock_irqsave(&fnic->fnic_lock, flags); 176 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats); 177 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 178 179 if (ret) { 180 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, 181 "fnic: Get vnic stats failed" 182 " 0x%x", ret); 183 return stats; 184 } 185 vs = fnic->stats; 186 stats->tx_frames = vs->tx.tx_unicast_frames_ok; 187 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4; 188 stats->rx_frames = vs->rx.rx_unicast_frames_ok; 189 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4; 190 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors; 191 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop; 192 stats->invalid_crc_count = vs->rx.rx_crc_errors; 193 stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ; 194 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000); 195 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000); 196 197 return stats; 198 } 199 200 void fnic_log_q_error(struct fnic *fnic) 201 { 202 unsigned int i; 203 u32 error_status; 204 205 for (i = 0; i < fnic->raw_wq_count; i++) { 206 error_status = ioread32(&fnic->wq[i].ctrl->error_status); 207 if (error_status) 208 shost_printk(KERN_ERR, fnic->lport->host, 209 "WQ[%d] error_status" 210 " %d\n", i, error_status); 211 } 212 213 for (i = 0; i < fnic->rq_count; i++) { 214 error_status = ioread32(&fnic->rq[i].ctrl->error_status); 215 if (error_status) 216 shost_printk(KERN_ERR, fnic->lport->host, 217 "RQ[%d] error_status" 218 " %d\n", i, error_status); 219 } 220 221 for (i = 0; i < fnic->wq_copy_count; i++) { 222 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status); 223 if (error_status) 224 shost_printk(KERN_ERR, fnic->lport->host, 225 "CWQ[%d] error_status" 226 " %d\n", i, error_status); 227 } 228 } 229 230 void fnic_handle_link_event(struct fnic *fnic) 231 { 232 unsigned long flags; 233 234 spin_lock_irqsave(&fnic->fnic_lock, flags); 235 if (fnic->stop_rx_link_events) { 236 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 237 return; 238 } 239 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 240 241 queue_work(fnic_event_queue, &fnic->link_work); 242 243 } 244 245 static int fnic_notify_set(struct fnic *fnic) 246 { 247 int err; 248 249 switch (vnic_dev_get_intr_mode(fnic->vdev)) { 250 case VNIC_DEV_INTR_MODE_INTX: 251 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY); 252 break; 253 case VNIC_DEV_INTR_MODE_MSI: 254 err = vnic_dev_notify_set(fnic->vdev, -1); 255 break; 256 case VNIC_DEV_INTR_MODE_MSIX: 257 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY); 258 break; 259 default: 260 shost_printk(KERN_ERR, fnic->lport->host, 261 "Interrupt mode should be set up" 262 " before devcmd notify set %d\n", 263 vnic_dev_get_intr_mode(fnic->vdev)); 264 err = -1; 265 break; 266 } 267 268 return err; 269 } 270 271 static void fnic_notify_timer(unsigned long data) 272 { 273 struct fnic *fnic = (struct fnic *)data; 274 275 fnic_handle_link_event(fnic); 276 mod_timer(&fnic->notify_timer, 277 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD)); 278 } 279 280 static void fnic_notify_timer_start(struct fnic *fnic) 281 { 282 switch (vnic_dev_get_intr_mode(fnic->vdev)) { 283 case VNIC_DEV_INTR_MODE_MSI: 284 /* 285 * Schedule first timeout immediately. The driver is 286 * initiatialized and ready to look for link up notification 287 */ 288 mod_timer(&fnic->notify_timer, jiffies); 289 break; 290 default: 291 /* Using intr for notification for INTx/MSI-X */ 292 break; 293 }; 294 } 295 296 static int fnic_dev_wait(struct vnic_dev *vdev, 297 int (*start)(struct vnic_dev *, int), 298 int (*finished)(struct vnic_dev *, int *), 299 int arg) 300 { 301 unsigned long time; 302 int done; 303 int err; 304 305 err = start(vdev, arg); 306 if (err) 307 return err; 308 309 /* Wait for func to complete...2 seconds max */ 310 time = jiffies + (HZ * 2); 311 do { 312 err = finished(vdev, &done); 313 if (err) 314 return err; 315 if (done) 316 return 0; 317 schedule_timeout_uninterruptible(HZ / 10); 318 } while (time_after(time, jiffies)); 319 320 return -ETIMEDOUT; 321 } 322 323 static int fnic_cleanup(struct fnic *fnic) 324 { 325 unsigned int i; 326 int err; 327 unsigned long flags; 328 struct fc_frame *flogi = NULL; 329 struct fc_frame *flogi_resp = NULL; 330 331 vnic_dev_disable(fnic->vdev); 332 for (i = 0; i < fnic->intr_count; i++) 333 vnic_intr_mask(&fnic->intr[i]); 334 335 for (i = 0; i < fnic->rq_count; i++) { 336 err = vnic_rq_disable(&fnic->rq[i]); 337 if (err) 338 return err; 339 } 340 for (i = 0; i < fnic->raw_wq_count; i++) { 341 err = vnic_wq_disable(&fnic->wq[i]); 342 if (err) 343 return err; 344 } 345 for (i = 0; i < fnic->wq_copy_count; i++) { 346 err = vnic_wq_copy_disable(&fnic->wq_copy[i]); 347 if (err) 348 return err; 349 } 350 351 /* Clean up completed IOs and FCS frames */ 352 fnic_wq_copy_cmpl_handler(fnic, -1); 353 fnic_wq_cmpl_handler(fnic, -1); 354 fnic_rq_cmpl_handler(fnic, -1); 355 356 /* Clean up the IOs and FCS frames that have not completed */ 357 for (i = 0; i < fnic->raw_wq_count; i++) 358 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf); 359 for (i = 0; i < fnic->rq_count; i++) 360 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf); 361 for (i = 0; i < fnic->wq_copy_count; i++) 362 vnic_wq_copy_clean(&fnic->wq_copy[i], 363 fnic_wq_copy_cleanup_handler); 364 365 for (i = 0; i < fnic->cq_count; i++) 366 vnic_cq_clean(&fnic->cq[i]); 367 for (i = 0; i < fnic->intr_count; i++) 368 vnic_intr_clean(&fnic->intr[i]); 369 370 /* 371 * Remove cached flogi and flogi resp frames if any 372 * These frames are not in any queue, and therefore queue 373 * cleanup does not clean them. So clean them explicitly 374 */ 375 spin_lock_irqsave(&fnic->fnic_lock, flags); 376 flogi = fnic->flogi; 377 fnic->flogi = NULL; 378 flogi_resp = fnic->flogi_resp; 379 fnic->flogi_resp = NULL; 380 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 381 382 if (flogi) 383 dev_kfree_skb(fp_skb(flogi)); 384 385 if (flogi_resp) 386 dev_kfree_skb(fp_skb(flogi_resp)); 387 388 mempool_destroy(fnic->io_req_pool); 389 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++) 390 mempool_destroy(fnic->io_sgl_pool[i]); 391 392 return 0; 393 } 394 395 static void fnic_iounmap(struct fnic *fnic) 396 { 397 if (fnic->bar0.vaddr) 398 iounmap(fnic->bar0.vaddr); 399 } 400 401 /* 402 * Allocate element for mempools requiring GFP_DMA flag. 403 * Otherwise, checks in kmem_flagcheck() hit BUG_ON(). 404 */ 405 static void *fnic_alloc_slab_dma(gfp_t gfp_mask, void *pool_data) 406 { 407 struct kmem_cache *mem = pool_data; 408 409 return kmem_cache_alloc(mem, gfp_mask | GFP_ATOMIC | GFP_DMA); 410 } 411 412 static int __devinit fnic_probe(struct pci_dev *pdev, 413 const struct pci_device_id *ent) 414 { 415 struct Scsi_Host *host; 416 struct fc_lport *lp; 417 struct fnic *fnic; 418 mempool_t *pool; 419 int err; 420 int i; 421 unsigned long flags; 422 423 /* 424 * Allocate SCSI Host and set up association between host, 425 * local port, and fnic 426 */ 427 lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic)); 428 if (!lp) { 429 printk(KERN_ERR PFX "Unable to alloc libfc local port\n"); 430 err = -ENOMEM; 431 goto err_out; 432 } 433 host = lp->host; 434 fnic = lport_priv(lp); 435 fnic->lport = lp; 436 437 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME, 438 host->host_no); 439 440 host->transportt = fnic_fc_transport; 441 442 err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ); 443 if (err) { 444 shost_printk(KERN_ERR, fnic->lport->host, 445 "Unable to alloc shared tag map\n"); 446 goto err_out_free_hba; 447 } 448 449 /* Setup PCI resources */ 450 pci_set_drvdata(pdev, fnic); 451 452 fnic->pdev = pdev; 453 454 err = pci_enable_device(pdev); 455 if (err) { 456 shost_printk(KERN_ERR, fnic->lport->host, 457 "Cannot enable PCI device, aborting.\n"); 458 goto err_out_free_hba; 459 } 460 461 err = pci_request_regions(pdev, DRV_NAME); 462 if (err) { 463 shost_printk(KERN_ERR, fnic->lport->host, 464 "Cannot enable PCI resources, aborting\n"); 465 goto err_out_disable_device; 466 } 467 468 pci_set_master(pdev); 469 470 /* Query PCI controller on system for DMA addressing 471 * limitation for the device. Try 40-bit first, and 472 * fail to 32-bit. 473 */ 474 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40)); 475 if (err) { 476 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 477 if (err) { 478 shost_printk(KERN_ERR, fnic->lport->host, 479 "No usable DMA configuration " 480 "aborting\n"); 481 goto err_out_release_regions; 482 } 483 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 484 if (err) { 485 shost_printk(KERN_ERR, fnic->lport->host, 486 "Unable to obtain 32-bit DMA " 487 "for consistent allocations, aborting.\n"); 488 goto err_out_release_regions; 489 } 490 } else { 491 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40)); 492 if (err) { 493 shost_printk(KERN_ERR, fnic->lport->host, 494 "Unable to obtain 40-bit DMA " 495 "for consistent allocations, aborting.\n"); 496 goto err_out_release_regions; 497 } 498 } 499 500 /* Map vNIC resources from BAR0 */ 501 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 502 shost_printk(KERN_ERR, fnic->lport->host, 503 "BAR0 not memory-map'able, aborting.\n"); 504 err = -ENODEV; 505 goto err_out_release_regions; 506 } 507 508 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0); 509 fnic->bar0.bus_addr = pci_resource_start(pdev, 0); 510 fnic->bar0.len = pci_resource_len(pdev, 0); 511 512 if (!fnic->bar0.vaddr) { 513 shost_printk(KERN_ERR, fnic->lport->host, 514 "Cannot memory-map BAR0 res hdr, " 515 "aborting.\n"); 516 err = -ENODEV; 517 goto err_out_release_regions; 518 } 519 520 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0); 521 if (!fnic->vdev) { 522 shost_printk(KERN_ERR, fnic->lport->host, 523 "vNIC registration failed, " 524 "aborting.\n"); 525 err = -ENODEV; 526 goto err_out_iounmap; 527 } 528 529 err = fnic_dev_wait(fnic->vdev, vnic_dev_open, 530 vnic_dev_open_done, 0); 531 if (err) { 532 shost_printk(KERN_ERR, fnic->lport->host, 533 "vNIC dev open failed, aborting.\n"); 534 goto err_out_vnic_unregister; 535 } 536 537 err = vnic_dev_init(fnic->vdev, 0); 538 if (err) { 539 shost_printk(KERN_ERR, fnic->lport->host, 540 "vNIC dev init failed, aborting.\n"); 541 goto err_out_dev_close; 542 } 543 544 err = vnic_dev_mac_addr(fnic->vdev, fnic->mac_addr); 545 if (err) { 546 shost_printk(KERN_ERR, fnic->lport->host, 547 "vNIC get MAC addr failed \n"); 548 goto err_out_dev_close; 549 } 550 551 /* Get vNIC configuration */ 552 err = fnic_get_vnic_config(fnic); 553 if (err) { 554 shost_printk(KERN_ERR, fnic->lport->host, 555 "Get vNIC configuration failed, " 556 "aborting.\n"); 557 goto err_out_dev_close; 558 } 559 host->max_lun = fnic->config.luns_per_tgt; 560 host->max_id = FNIC_MAX_FCP_TARGET; 561 host->max_cmd_len = FNIC_MAX_CMD_LEN; 562 563 fnic_get_res_counts(fnic); 564 565 err = fnic_set_intr_mode(fnic); 566 if (err) { 567 shost_printk(KERN_ERR, fnic->lport->host, 568 "Failed to set intr mode, " 569 "aborting.\n"); 570 goto err_out_dev_close; 571 } 572 573 err = fnic_alloc_vnic_resources(fnic); 574 if (err) { 575 shost_printk(KERN_ERR, fnic->lport->host, 576 "Failed to alloc vNIC resources, " 577 "aborting.\n"); 578 goto err_out_clear_intr; 579 } 580 581 582 /* initialize all fnic locks */ 583 spin_lock_init(&fnic->fnic_lock); 584 585 for (i = 0; i < FNIC_WQ_MAX; i++) 586 spin_lock_init(&fnic->wq_lock[i]); 587 588 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) { 589 spin_lock_init(&fnic->wq_copy_lock[i]); 590 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK; 591 fnic->fw_ack_recd[i] = 0; 592 fnic->fw_ack_index[i] = -1; 593 } 594 595 for (i = 0; i < FNIC_IO_LOCKS; i++) 596 spin_lock_init(&fnic->io_req_lock[i]); 597 598 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache); 599 if (!fnic->io_req_pool) 600 goto err_out_free_resources; 601 602 pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab, 603 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]); 604 if (!pool) 605 goto err_out_free_ioreq_pool; 606 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool; 607 608 pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab, 609 fnic_sgl_cache[FNIC_SGL_CACHE_MAX]); 610 if (!pool) 611 goto err_out_free_dflt_pool; 612 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool; 613 614 /* setup vlan config, hw inserts vlan header */ 615 fnic->vlan_hw_insert = 1; 616 fnic->vlan_id = 0; 617 618 fnic->flogi_oxid = FC_XID_UNKNOWN; 619 fnic->flogi = NULL; 620 fnic->flogi_resp = NULL; 621 fnic->state = FNIC_IN_FC_MODE; 622 623 /* Enable hardware stripping of vlan header on ingress */ 624 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1); 625 626 /* Setup notification buffer area */ 627 err = fnic_notify_set(fnic); 628 if (err) { 629 shost_printk(KERN_ERR, fnic->lport->host, 630 "Failed to alloc notify buffer, aborting.\n"); 631 goto err_out_free_max_pool; 632 } 633 634 /* Setup notify timer when using MSI interrupts */ 635 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI) 636 setup_timer(&fnic->notify_timer, 637 fnic_notify_timer, (unsigned long)fnic); 638 639 /* allocate RQ buffers and post them to RQ*/ 640 for (i = 0; i < fnic->rq_count; i++) { 641 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame); 642 if (err) { 643 shost_printk(KERN_ERR, fnic->lport->host, 644 "fnic_alloc_rq_frame can't alloc " 645 "frame\n"); 646 goto err_out_free_rq_buf; 647 } 648 } 649 650 /* 651 * Initialization done with PCI system, hardware, firmware. 652 * Add host to SCSI 653 */ 654 err = scsi_add_host(lp->host, &pdev->dev); 655 if (err) { 656 shost_printk(KERN_ERR, fnic->lport->host, 657 "fnic: scsi_add_host failed...exiting\n"); 658 goto err_out_free_rq_buf; 659 } 660 661 /* Start local port initiatialization */ 662 663 lp->link_up = 0; 664 lp->tt = fnic_transport_template; 665 666 lp->max_retry_count = fnic->config.flogi_retries; 667 lp->max_rport_retry_count = fnic->config.plogi_retries; 668 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS | 669 FCP_SPPF_CONF_COMPL); 670 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) 671 lp->service_params |= FCP_SPPF_RETRY; 672 673 lp->boot_time = jiffies; 674 lp->e_d_tov = fnic->config.ed_tov; 675 lp->r_a_tov = fnic->config.ra_tov; 676 lp->link_supported_speeds = FC_PORTSPEED_10GBIT; 677 fc_set_wwnn(lp, fnic->config.node_wwn); 678 fc_set_wwpn(lp, fnic->config.port_wwn); 679 680 fc_lport_init(lp); 681 fc_exch_init(lp); 682 fc_elsct_init(lp); 683 fc_rport_init(lp); 684 fc_disc_init(lp); 685 686 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START, 687 FCPIO_HOST_EXCH_RANGE_END, NULL)) { 688 err = -ENOMEM; 689 goto err_out_remove_scsi_host; 690 } 691 692 fc_lport_config(lp); 693 694 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize + 695 sizeof(struct fc_frame_header))) { 696 err = -EINVAL; 697 goto err_out_free_exch_mgr; 698 } 699 fc_host_maxframe_size(lp->host) = lp->mfs; 700 701 sprintf(fc_host_symbolic_name(lp->host), 702 DRV_NAME " v" DRV_VERSION " over %s", fnic->name); 703 704 spin_lock_irqsave(&fnic_list_lock, flags); 705 list_add_tail(&fnic->list, &fnic_list); 706 spin_unlock_irqrestore(&fnic_list_lock, flags); 707 708 INIT_WORK(&fnic->link_work, fnic_handle_link); 709 INIT_WORK(&fnic->frame_work, fnic_handle_frame); 710 skb_queue_head_init(&fnic->frame_queue); 711 712 /* Enable all queues */ 713 for (i = 0; i < fnic->raw_wq_count; i++) 714 vnic_wq_enable(&fnic->wq[i]); 715 for (i = 0; i < fnic->rq_count; i++) 716 vnic_rq_enable(&fnic->rq[i]); 717 for (i = 0; i < fnic->wq_copy_count; i++) 718 vnic_wq_copy_enable(&fnic->wq_copy[i]); 719 720 fc_fabric_login(lp); 721 722 vnic_dev_enable(fnic->vdev); 723 724 err = fnic_request_intr(fnic); 725 if (err) { 726 shost_printk(KERN_ERR, fnic->lport->host, 727 "Unable to request irq.\n"); 728 goto err_out_free_exch_mgr; 729 } 730 731 for (i = 0; i < fnic->intr_count; i++) 732 vnic_intr_unmask(&fnic->intr[i]); 733 734 fnic_notify_timer_start(fnic); 735 736 return 0; 737 738 err_out_free_exch_mgr: 739 fc_exch_mgr_free(lp); 740 err_out_remove_scsi_host: 741 fc_remove_host(fnic->lport->host); 742 scsi_remove_host(fnic->lport->host); 743 err_out_free_rq_buf: 744 for (i = 0; i < fnic->rq_count; i++) 745 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf); 746 vnic_dev_notify_unset(fnic->vdev); 747 err_out_free_max_pool: 748 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]); 749 err_out_free_dflt_pool: 750 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]); 751 err_out_free_ioreq_pool: 752 mempool_destroy(fnic->io_req_pool); 753 err_out_free_resources: 754 fnic_free_vnic_resources(fnic); 755 err_out_clear_intr: 756 fnic_clear_intr_mode(fnic); 757 err_out_dev_close: 758 vnic_dev_close(fnic->vdev); 759 err_out_vnic_unregister: 760 vnic_dev_unregister(fnic->vdev); 761 err_out_iounmap: 762 fnic_iounmap(fnic); 763 err_out_release_regions: 764 pci_release_regions(pdev); 765 err_out_disable_device: 766 pci_disable_device(pdev); 767 err_out_free_hba: 768 scsi_host_put(lp->host); 769 err_out: 770 return err; 771 } 772 773 static void __devexit fnic_remove(struct pci_dev *pdev) 774 { 775 struct fnic *fnic = pci_get_drvdata(pdev); 776 unsigned long flags; 777 778 /* 779 * Mark state so that the workqueue thread stops forwarding 780 * received frames and link events to the local port. ISR and 781 * other threads that can queue work items will also stop 782 * creating work items on the fnic workqueue 783 */ 784 spin_lock_irqsave(&fnic->fnic_lock, flags); 785 fnic->stop_rx_link_events = 1; 786 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 787 788 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI) 789 del_timer_sync(&fnic->notify_timer); 790 791 /* 792 * Flush the fnic event queue. After this call, there should 793 * be no event queued for this fnic device in the workqueue 794 */ 795 flush_workqueue(fnic_event_queue); 796 skb_queue_purge(&fnic->frame_queue); 797 798 /* 799 * Log off the fabric. This stops all remote ports, dns port, 800 * logs off the fabric. This flushes all rport, disc, lport work 801 * before returning 802 */ 803 fc_fabric_logoff(fnic->lport); 804 805 spin_lock_irqsave(&fnic->fnic_lock, flags); 806 fnic->in_remove = 1; 807 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 808 809 fc_lport_destroy(fnic->lport); 810 811 /* 812 * This stops the fnic device, masks all interrupts. Completed 813 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are 814 * cleaned up 815 */ 816 fnic_cleanup(fnic); 817 818 BUG_ON(!skb_queue_empty(&fnic->frame_queue)); 819 820 spin_lock_irqsave(&fnic_list_lock, flags); 821 list_del(&fnic->list); 822 spin_unlock_irqrestore(&fnic_list_lock, flags); 823 824 fc_remove_host(fnic->lport->host); 825 scsi_remove_host(fnic->lport->host); 826 fc_exch_mgr_free(fnic->lport); 827 vnic_dev_notify_unset(fnic->vdev); 828 fnic_free_intr(fnic); 829 fnic_free_vnic_resources(fnic); 830 fnic_clear_intr_mode(fnic); 831 vnic_dev_close(fnic->vdev); 832 vnic_dev_unregister(fnic->vdev); 833 fnic_iounmap(fnic); 834 pci_release_regions(pdev); 835 pci_disable_device(pdev); 836 pci_set_drvdata(pdev, NULL); 837 scsi_host_put(fnic->lport->host); 838 } 839 840 static struct pci_driver fnic_driver = { 841 .name = DRV_NAME, 842 .id_table = fnic_id_table, 843 .probe = fnic_probe, 844 .remove = __devexit_p(fnic_remove), 845 }; 846 847 static int __init fnic_init_module(void) 848 { 849 size_t len; 850 int err = 0; 851 852 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION); 853 854 /* Create a cache for allocation of default size sgls */ 855 len = sizeof(struct fnic_dflt_sgl_list); 856 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create 857 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN, 858 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, 859 NULL); 860 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) { 861 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n"); 862 err = -ENOMEM; 863 goto err_create_fnic_sgl_slab_dflt; 864 } 865 866 /* Create a cache for allocation of max size sgls*/ 867 len = sizeof(struct fnic_sgl_list); 868 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create 869 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN, 870 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, 871 NULL); 872 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) { 873 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n"); 874 err = -ENOMEM; 875 goto err_create_fnic_sgl_slab_max; 876 } 877 878 /* Create a cache of io_req structs for use via mempool */ 879 fnic_io_req_cache = kmem_cache_create("fnic_io_req", 880 sizeof(struct fnic_io_req), 881 0, SLAB_HWCACHE_ALIGN, NULL); 882 if (!fnic_io_req_cache) { 883 printk(KERN_ERR PFX "failed to create fnic io_req slab\n"); 884 err = -ENOMEM; 885 goto err_create_fnic_ioreq_slab; 886 } 887 888 fnic_event_queue = create_singlethread_workqueue("fnic_event_wq"); 889 if (!fnic_event_queue) { 890 printk(KERN_ERR PFX "fnic work queue create failed\n"); 891 err = -ENOMEM; 892 goto err_create_fnic_workq; 893 } 894 895 spin_lock_init(&fnic_list_lock); 896 INIT_LIST_HEAD(&fnic_list); 897 898 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions); 899 if (!fnic_fc_transport) { 900 printk(KERN_ERR PFX "fc_attach_transport error\n"); 901 err = -ENOMEM; 902 goto err_fc_transport; 903 } 904 905 /* register the driver with PCI system */ 906 err = pci_register_driver(&fnic_driver); 907 if (err < 0) { 908 printk(KERN_ERR PFX "pci register error\n"); 909 goto err_pci_register; 910 } 911 return err; 912 913 err_pci_register: 914 fc_release_transport(fnic_fc_transport); 915 err_fc_transport: 916 destroy_workqueue(fnic_event_queue); 917 err_create_fnic_workq: 918 kmem_cache_destroy(fnic_io_req_cache); 919 err_create_fnic_ioreq_slab: 920 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]); 921 err_create_fnic_sgl_slab_max: 922 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]); 923 err_create_fnic_sgl_slab_dflt: 924 return err; 925 } 926 927 static void __exit fnic_cleanup_module(void) 928 { 929 pci_unregister_driver(&fnic_driver); 930 destroy_workqueue(fnic_event_queue); 931 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]); 932 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]); 933 kmem_cache_destroy(fnic_io_req_cache); 934 fc_release_transport(fnic_fc_transport); 935 } 936 937 module_init(fnic_init_module); 938 module_exit(fnic_cleanup_module); 939 940