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/slab.h> 22 #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> 29 #include <linux/if_ether.h> 30 #include <scsi/fc/fc_fip.h> 31 #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_fip.h" 43 #include "fnic.h" 44 45 #define PCI_DEVICE_ID_CISCO_FNIC 0x0045 46 47 /* Timer to poll notification area for events. Used for MSI interrupts */ 48 #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ) 49 50 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES]; 51 static struct kmem_cache *fnic_io_req_cache; 52 LIST_HEAD(fnic_list); 53 DEFINE_SPINLOCK(fnic_list_lock); 54 55 /* Supported devices by fnic module */ 56 static struct pci_device_id fnic_id_table[] = { 57 { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) }, 58 { 0, } 59 }; 60 61 MODULE_DESCRIPTION(DRV_DESCRIPTION); 62 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, " 63 "Joseph R. Eykholt <jeykholt@cisco.com>"); 64 MODULE_LICENSE("GPL v2"); 65 MODULE_VERSION(DRV_VERSION); 66 MODULE_DEVICE_TABLE(pci, fnic_id_table); 67 68 unsigned int fnic_log_level; 69 module_param(fnic_log_level, int, S_IRUGO|S_IWUSR); 70 MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels"); 71 72 unsigned int fnic_trace_max_pages = 16; 73 module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR); 74 MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages " 75 "for fnic trace buffer"); 76 77 unsigned int fnic_fc_trace_max_pages = 64; 78 module_param(fnic_fc_trace_max_pages, uint, S_IRUGO|S_IWUSR); 79 MODULE_PARM_DESC(fnic_fc_trace_max_pages, 80 "Total allocated memory pages for fc trace buffer"); 81 82 static unsigned int fnic_max_qdepth = FNIC_DFLT_QUEUE_DEPTH; 83 module_param(fnic_max_qdepth, uint, S_IRUGO|S_IWUSR); 84 MODULE_PARM_DESC(fnic_max_qdepth, "Queue depth to report for each LUN"); 85 86 static struct libfc_function_template fnic_transport_template = { 87 .frame_send = fnic_send, 88 .lport_set_port_id = fnic_set_port_id, 89 .fcp_abort_io = fnic_empty_scsi_cleanup, 90 .fcp_cleanup = fnic_empty_scsi_cleanup, 91 .exch_mgr_reset = fnic_exch_mgr_reset 92 }; 93 94 static int fnic_slave_alloc(struct scsi_device *sdev) 95 { 96 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 97 98 if (!rport || fc_remote_port_chkready(rport)) 99 return -ENXIO; 100 101 scsi_change_queue_depth(sdev, fnic_max_qdepth); 102 return 0; 103 } 104 105 static struct scsi_host_template fnic_host_template = { 106 .module = THIS_MODULE, 107 .name = DRV_NAME, 108 .queuecommand = fnic_queuecommand, 109 .eh_timed_out = fc_eh_timed_out, 110 .eh_abort_handler = fnic_abort_cmd, 111 .eh_device_reset_handler = fnic_device_reset, 112 .eh_host_reset_handler = fnic_host_reset, 113 .slave_alloc = fnic_slave_alloc, 114 .change_queue_depth = scsi_change_queue_depth, 115 .this_id = -1, 116 .cmd_per_lun = 3, 117 .can_queue = FNIC_DFLT_IO_REQ, 118 .sg_tablesize = FNIC_MAX_SG_DESC_CNT, 119 .max_sectors = 0xffff, 120 .shost_attrs = fnic_attrs, 121 .track_queue_depth = 1, 122 }; 123 124 static void 125 fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout) 126 { 127 if (timeout) 128 rport->dev_loss_tmo = timeout; 129 else 130 rport->dev_loss_tmo = 1; 131 } 132 133 static void fnic_get_host_speed(struct Scsi_Host *shost); 134 static struct scsi_transport_template *fnic_fc_transport; 135 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *); 136 static void fnic_reset_host_stats(struct Scsi_Host *); 137 138 static struct fc_function_template fnic_fc_functions = { 139 140 .show_host_node_name = 1, 141 .show_host_port_name = 1, 142 .show_host_supported_classes = 1, 143 .show_host_supported_fc4s = 1, 144 .show_host_active_fc4s = 1, 145 .show_host_maxframe_size = 1, 146 .show_host_port_id = 1, 147 .show_host_supported_speeds = 1, 148 .get_host_speed = fnic_get_host_speed, 149 .show_host_speed = 1, 150 .show_host_port_type = 1, 151 .get_host_port_state = fc_get_host_port_state, 152 .show_host_port_state = 1, 153 .show_host_symbolic_name = 1, 154 .show_rport_maxframe_size = 1, 155 .show_rport_supported_classes = 1, 156 .show_host_fabric_name = 1, 157 .show_starget_node_name = 1, 158 .show_starget_port_name = 1, 159 .show_starget_port_id = 1, 160 .show_rport_dev_loss_tmo = 1, 161 .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo, 162 .issue_fc_host_lip = fnic_reset, 163 .get_fc_host_stats = fnic_get_stats, 164 .reset_fc_host_stats = fnic_reset_host_stats, 165 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv), 166 .terminate_rport_io = fnic_terminate_rport_io, 167 .bsg_request = fc_lport_bsg_request, 168 }; 169 170 static void fnic_get_host_speed(struct Scsi_Host *shost) 171 { 172 struct fc_lport *lp = shost_priv(shost); 173 struct fnic *fnic = lport_priv(lp); 174 u32 port_speed = vnic_dev_port_speed(fnic->vdev); 175 176 /* Add in other values as they get defined in fw */ 177 switch (port_speed) { 178 case DCEM_PORTSPEED_10G: 179 fc_host_speed(shost) = FC_PORTSPEED_10GBIT; 180 break; 181 case DCEM_PORTSPEED_25G: 182 fc_host_speed(shost) = FC_PORTSPEED_25GBIT; 183 break; 184 case DCEM_PORTSPEED_40G: 185 case DCEM_PORTSPEED_4x10G: 186 fc_host_speed(shost) = FC_PORTSPEED_40GBIT; 187 break; 188 case DCEM_PORTSPEED_100G: 189 fc_host_speed(shost) = FC_PORTSPEED_100GBIT; 190 break; 191 default: 192 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN; 193 break; 194 } 195 } 196 197 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host) 198 { 199 int ret; 200 struct fc_lport *lp = shost_priv(host); 201 struct fnic *fnic = lport_priv(lp); 202 struct fc_host_statistics *stats = &lp->host_stats; 203 struct vnic_stats *vs; 204 unsigned long flags; 205 206 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT)) 207 return stats; 208 fnic->stats_time = jiffies; 209 210 spin_lock_irqsave(&fnic->fnic_lock, flags); 211 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats); 212 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 213 214 if (ret) { 215 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, 216 "fnic: Get vnic stats failed" 217 " 0x%x", ret); 218 return stats; 219 } 220 vs = fnic->stats; 221 stats->tx_frames = vs->tx.tx_unicast_frames_ok; 222 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4; 223 stats->rx_frames = vs->rx.rx_unicast_frames_ok; 224 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4; 225 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors; 226 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop; 227 stats->invalid_crc_count = vs->rx.rx_crc_errors; 228 stats->seconds_since_last_reset = 229 (jiffies - fnic->stats_reset_time) / HZ; 230 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000); 231 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000); 232 233 return stats; 234 } 235 236 /* 237 * fnic_dump_fchost_stats 238 * note : dumps fc_statistics into system logs 239 */ 240 void fnic_dump_fchost_stats(struct Scsi_Host *host, 241 struct fc_host_statistics *stats) 242 { 243 FNIC_MAIN_NOTE(KERN_NOTICE, host, 244 "fnic: seconds since last reset = %llu\n", 245 stats->seconds_since_last_reset); 246 FNIC_MAIN_NOTE(KERN_NOTICE, host, 247 "fnic: tx frames = %llu\n", 248 stats->tx_frames); 249 FNIC_MAIN_NOTE(KERN_NOTICE, host, 250 "fnic: tx words = %llu\n", 251 stats->tx_words); 252 FNIC_MAIN_NOTE(KERN_NOTICE, host, 253 "fnic: rx frames = %llu\n", 254 stats->rx_frames); 255 FNIC_MAIN_NOTE(KERN_NOTICE, host, 256 "fnic: rx words = %llu\n", 257 stats->rx_words); 258 FNIC_MAIN_NOTE(KERN_NOTICE, host, 259 "fnic: lip count = %llu\n", 260 stats->lip_count); 261 FNIC_MAIN_NOTE(KERN_NOTICE, host, 262 "fnic: nos count = %llu\n", 263 stats->nos_count); 264 FNIC_MAIN_NOTE(KERN_NOTICE, host, 265 "fnic: error frames = %llu\n", 266 stats->error_frames); 267 FNIC_MAIN_NOTE(KERN_NOTICE, host, 268 "fnic: dumped frames = %llu\n", 269 stats->dumped_frames); 270 FNIC_MAIN_NOTE(KERN_NOTICE, host, 271 "fnic: link failure count = %llu\n", 272 stats->link_failure_count); 273 FNIC_MAIN_NOTE(KERN_NOTICE, host, 274 "fnic: loss of sync count = %llu\n", 275 stats->loss_of_sync_count); 276 FNIC_MAIN_NOTE(KERN_NOTICE, host, 277 "fnic: loss of signal count = %llu\n", 278 stats->loss_of_signal_count); 279 FNIC_MAIN_NOTE(KERN_NOTICE, host, 280 "fnic: prim seq protocol err count = %llu\n", 281 stats->prim_seq_protocol_err_count); 282 FNIC_MAIN_NOTE(KERN_NOTICE, host, 283 "fnic: invalid tx word count= %llu\n", 284 stats->invalid_tx_word_count); 285 FNIC_MAIN_NOTE(KERN_NOTICE, host, 286 "fnic: invalid crc count = %llu\n", 287 stats->invalid_crc_count); 288 FNIC_MAIN_NOTE(KERN_NOTICE, host, 289 "fnic: fcp input requests = %llu\n", 290 stats->fcp_input_requests); 291 FNIC_MAIN_NOTE(KERN_NOTICE, host, 292 "fnic: fcp output requests = %llu\n", 293 stats->fcp_output_requests); 294 FNIC_MAIN_NOTE(KERN_NOTICE, host, 295 "fnic: fcp control requests = %llu\n", 296 stats->fcp_control_requests); 297 FNIC_MAIN_NOTE(KERN_NOTICE, host, 298 "fnic: fcp input megabytes = %llu\n", 299 stats->fcp_input_megabytes); 300 FNIC_MAIN_NOTE(KERN_NOTICE, host, 301 "fnic: fcp output megabytes = %llu\n", 302 stats->fcp_output_megabytes); 303 return; 304 } 305 306 /* 307 * fnic_reset_host_stats : clears host stats 308 * note : called when reset_statistics set under sysfs dir 309 */ 310 static void fnic_reset_host_stats(struct Scsi_Host *host) 311 { 312 int ret; 313 struct fc_lport *lp = shost_priv(host); 314 struct fnic *fnic = lport_priv(lp); 315 struct fc_host_statistics *stats; 316 unsigned long flags; 317 318 /* dump current stats, before clearing them */ 319 stats = fnic_get_stats(host); 320 fnic_dump_fchost_stats(host, stats); 321 322 spin_lock_irqsave(&fnic->fnic_lock, flags); 323 ret = vnic_dev_stats_clear(fnic->vdev); 324 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 325 326 if (ret) { 327 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, 328 "fnic: Reset vnic stats failed" 329 " 0x%x", ret); 330 return; 331 } 332 fnic->stats_reset_time = jiffies; 333 memset(stats, 0, sizeof(*stats)); 334 335 return; 336 } 337 338 void fnic_log_q_error(struct fnic *fnic) 339 { 340 unsigned int i; 341 u32 error_status; 342 343 for (i = 0; i < fnic->raw_wq_count; i++) { 344 error_status = ioread32(&fnic->wq[i].ctrl->error_status); 345 if (error_status) 346 shost_printk(KERN_ERR, fnic->lport->host, 347 "WQ[%d] error_status" 348 " %d\n", i, error_status); 349 } 350 351 for (i = 0; i < fnic->rq_count; i++) { 352 error_status = ioread32(&fnic->rq[i].ctrl->error_status); 353 if (error_status) 354 shost_printk(KERN_ERR, fnic->lport->host, 355 "RQ[%d] error_status" 356 " %d\n", i, error_status); 357 } 358 359 for (i = 0; i < fnic->wq_copy_count; i++) { 360 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status); 361 if (error_status) 362 shost_printk(KERN_ERR, fnic->lport->host, 363 "CWQ[%d] error_status" 364 " %d\n", i, error_status); 365 } 366 } 367 368 void fnic_handle_link_event(struct fnic *fnic) 369 { 370 unsigned long flags; 371 372 spin_lock_irqsave(&fnic->fnic_lock, flags); 373 if (fnic->stop_rx_link_events) { 374 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 375 return; 376 } 377 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 378 379 queue_work(fnic_event_queue, &fnic->link_work); 380 381 } 382 383 static int fnic_notify_set(struct fnic *fnic) 384 { 385 int err; 386 387 switch (vnic_dev_get_intr_mode(fnic->vdev)) { 388 case VNIC_DEV_INTR_MODE_INTX: 389 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY); 390 break; 391 case VNIC_DEV_INTR_MODE_MSI: 392 err = vnic_dev_notify_set(fnic->vdev, -1); 393 break; 394 case VNIC_DEV_INTR_MODE_MSIX: 395 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY); 396 break; 397 default: 398 shost_printk(KERN_ERR, fnic->lport->host, 399 "Interrupt mode should be set up" 400 " before devcmd notify set %d\n", 401 vnic_dev_get_intr_mode(fnic->vdev)); 402 err = -1; 403 break; 404 } 405 406 return err; 407 } 408 409 static void fnic_notify_timer(struct timer_list *t) 410 { 411 struct fnic *fnic = from_timer(fnic, t, notify_timer); 412 413 fnic_handle_link_event(fnic); 414 mod_timer(&fnic->notify_timer, 415 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD)); 416 } 417 418 static void fnic_fip_notify_timer(struct timer_list *t) 419 { 420 struct fnic *fnic = from_timer(fnic, t, fip_timer); 421 422 fnic_handle_fip_timer(fnic); 423 } 424 425 static void fnic_notify_timer_start(struct fnic *fnic) 426 { 427 switch (vnic_dev_get_intr_mode(fnic->vdev)) { 428 case VNIC_DEV_INTR_MODE_MSI: 429 /* 430 * Schedule first timeout immediately. The driver is 431 * initiatialized and ready to look for link up notification 432 */ 433 mod_timer(&fnic->notify_timer, jiffies); 434 break; 435 default: 436 /* Using intr for notification for INTx/MSI-X */ 437 break; 438 }; 439 } 440 441 static int fnic_dev_wait(struct vnic_dev *vdev, 442 int (*start)(struct vnic_dev *, int), 443 int (*finished)(struct vnic_dev *, int *), 444 int arg) 445 { 446 unsigned long time; 447 int done; 448 int err; 449 int count; 450 451 count = 0; 452 453 err = start(vdev, arg); 454 if (err) 455 return err; 456 457 /* Wait for func to complete. 458 * Sometime schedule_timeout_uninterruptible take long time 459 * to wake up so we do not retry as we are only waiting for 460 * 2 seconds in while loop. By adding count, we make sure 461 * we try atleast three times before returning -ETIMEDOUT 462 */ 463 time = jiffies + (HZ * 2); 464 do { 465 err = finished(vdev, &done); 466 count++; 467 if (err) 468 return err; 469 if (done) 470 return 0; 471 schedule_timeout_uninterruptible(HZ / 10); 472 } while (time_after(time, jiffies) || (count < 3)); 473 474 return -ETIMEDOUT; 475 } 476 477 static int fnic_cleanup(struct fnic *fnic) 478 { 479 unsigned int i; 480 int err; 481 482 vnic_dev_disable(fnic->vdev); 483 for (i = 0; i < fnic->intr_count; i++) 484 vnic_intr_mask(&fnic->intr[i]); 485 486 for (i = 0; i < fnic->rq_count; i++) { 487 err = vnic_rq_disable(&fnic->rq[i]); 488 if (err) 489 return err; 490 } 491 for (i = 0; i < fnic->raw_wq_count; i++) { 492 err = vnic_wq_disable(&fnic->wq[i]); 493 if (err) 494 return err; 495 } 496 for (i = 0; i < fnic->wq_copy_count; i++) { 497 err = vnic_wq_copy_disable(&fnic->wq_copy[i]); 498 if (err) 499 return err; 500 } 501 502 /* Clean up completed IOs and FCS frames */ 503 fnic_wq_copy_cmpl_handler(fnic, -1); 504 fnic_wq_cmpl_handler(fnic, -1); 505 fnic_rq_cmpl_handler(fnic, -1); 506 507 /* Clean up the IOs and FCS frames that have not completed */ 508 for (i = 0; i < fnic->raw_wq_count; i++) 509 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf); 510 for (i = 0; i < fnic->rq_count; i++) 511 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf); 512 for (i = 0; i < fnic->wq_copy_count; i++) 513 vnic_wq_copy_clean(&fnic->wq_copy[i], 514 fnic_wq_copy_cleanup_handler); 515 516 for (i = 0; i < fnic->cq_count; i++) 517 vnic_cq_clean(&fnic->cq[i]); 518 for (i = 0; i < fnic->intr_count; i++) 519 vnic_intr_clean(&fnic->intr[i]); 520 521 mempool_destroy(fnic->io_req_pool); 522 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++) 523 mempool_destroy(fnic->io_sgl_pool[i]); 524 525 return 0; 526 } 527 528 static void fnic_iounmap(struct fnic *fnic) 529 { 530 if (fnic->bar0.vaddr) 531 iounmap(fnic->bar0.vaddr); 532 } 533 534 /** 535 * fnic_get_mac() - get assigned data MAC address for FIP code. 536 * @lport: local port. 537 */ 538 static u8 *fnic_get_mac(struct fc_lport *lport) 539 { 540 struct fnic *fnic = lport_priv(lport); 541 542 return fnic->data_src_addr; 543 } 544 545 static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id) 546 { 547 u16 old_vlan; 548 old_vlan = vnic_dev_set_default_vlan(fnic->vdev, vlan_id); 549 } 550 551 static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 552 { 553 struct Scsi_Host *host; 554 struct fc_lport *lp; 555 struct fnic *fnic; 556 mempool_t *pool; 557 int err; 558 int i; 559 unsigned long flags; 560 561 /* 562 * Allocate SCSI Host and set up association between host, 563 * local port, and fnic 564 */ 565 lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic)); 566 if (!lp) { 567 printk(KERN_ERR PFX "Unable to alloc libfc local port\n"); 568 err = -ENOMEM; 569 goto err_out; 570 } 571 host = lp->host; 572 fnic = lport_priv(lp); 573 fnic->lport = lp; 574 fnic->ctlr.lp = lp; 575 576 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME, 577 host->host_no); 578 579 host->transportt = fnic_fc_transport; 580 581 err = fnic_stats_debugfs_init(fnic); 582 if (err) { 583 shost_printk(KERN_ERR, fnic->lport->host, 584 "Failed to initialize debugfs for stats\n"); 585 fnic_stats_debugfs_remove(fnic); 586 } 587 588 /* Setup PCI resources */ 589 pci_set_drvdata(pdev, fnic); 590 591 fnic->pdev = pdev; 592 593 err = pci_enable_device(pdev); 594 if (err) { 595 shost_printk(KERN_ERR, fnic->lport->host, 596 "Cannot enable PCI device, aborting.\n"); 597 goto err_out_free_hba; 598 } 599 600 err = pci_request_regions(pdev, DRV_NAME); 601 if (err) { 602 shost_printk(KERN_ERR, fnic->lport->host, 603 "Cannot enable PCI resources, aborting\n"); 604 goto err_out_disable_device; 605 } 606 607 pci_set_master(pdev); 608 609 /* Query PCI controller on system for DMA addressing 610 * limitation for the device. Try 64-bit first, and 611 * fail to 32-bit. 612 */ 613 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 614 if (err) { 615 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 616 if (err) { 617 shost_printk(KERN_ERR, fnic->lport->host, 618 "No usable DMA configuration " 619 "aborting\n"); 620 goto err_out_release_regions; 621 } 622 } 623 624 /* Map vNIC resources from BAR0 */ 625 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 626 shost_printk(KERN_ERR, fnic->lport->host, 627 "BAR0 not memory-map'able, aborting.\n"); 628 err = -ENODEV; 629 goto err_out_release_regions; 630 } 631 632 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0); 633 fnic->bar0.bus_addr = pci_resource_start(pdev, 0); 634 fnic->bar0.len = pci_resource_len(pdev, 0); 635 636 if (!fnic->bar0.vaddr) { 637 shost_printk(KERN_ERR, fnic->lport->host, 638 "Cannot memory-map BAR0 res hdr, " 639 "aborting.\n"); 640 err = -ENODEV; 641 goto err_out_release_regions; 642 } 643 644 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0); 645 if (!fnic->vdev) { 646 shost_printk(KERN_ERR, fnic->lport->host, 647 "vNIC registration failed, " 648 "aborting.\n"); 649 err = -ENODEV; 650 goto err_out_iounmap; 651 } 652 653 err = fnic_dev_wait(fnic->vdev, vnic_dev_open, 654 vnic_dev_open_done, 0); 655 if (err) { 656 shost_printk(KERN_ERR, fnic->lport->host, 657 "vNIC dev open failed, aborting.\n"); 658 goto err_out_vnic_unregister; 659 } 660 661 err = vnic_dev_init(fnic->vdev, 0); 662 if (err) { 663 shost_printk(KERN_ERR, fnic->lport->host, 664 "vNIC dev init failed, aborting.\n"); 665 goto err_out_dev_close; 666 } 667 668 err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr); 669 if (err) { 670 shost_printk(KERN_ERR, fnic->lport->host, 671 "vNIC get MAC addr failed \n"); 672 goto err_out_dev_close; 673 } 674 /* set data_src for point-to-point mode and to keep it non-zero */ 675 memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN); 676 677 /* Get vNIC configuration */ 678 err = fnic_get_vnic_config(fnic); 679 if (err) { 680 shost_printk(KERN_ERR, fnic->lport->host, 681 "Get vNIC configuration failed, " 682 "aborting.\n"); 683 goto err_out_dev_close; 684 } 685 686 /* Configure Maximum Outstanding IO reqs*/ 687 if (fnic->config.io_throttle_count != FNIC_UCSM_DFLT_THROTTLE_CNT_BLD) { 688 host->can_queue = min_t(u32, FNIC_MAX_IO_REQ, 689 max_t(u32, FNIC_MIN_IO_REQ, 690 fnic->config.io_throttle_count)); 691 } 692 fnic->fnic_max_tag_id = host->can_queue; 693 694 host->max_lun = fnic->config.luns_per_tgt; 695 host->max_id = FNIC_MAX_FCP_TARGET; 696 host->max_cmd_len = FCOE_MAX_CMD_LEN; 697 698 fnic_get_res_counts(fnic); 699 700 err = fnic_set_intr_mode(fnic); 701 if (err) { 702 shost_printk(KERN_ERR, fnic->lport->host, 703 "Failed to set intr mode, " 704 "aborting.\n"); 705 goto err_out_dev_close; 706 } 707 708 err = fnic_alloc_vnic_resources(fnic); 709 if (err) { 710 shost_printk(KERN_ERR, fnic->lport->host, 711 "Failed to alloc vNIC resources, " 712 "aborting.\n"); 713 goto err_out_clear_intr; 714 } 715 716 717 /* initialize all fnic locks */ 718 spin_lock_init(&fnic->fnic_lock); 719 720 for (i = 0; i < FNIC_WQ_MAX; i++) 721 spin_lock_init(&fnic->wq_lock[i]); 722 723 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) { 724 spin_lock_init(&fnic->wq_copy_lock[i]); 725 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK; 726 fnic->fw_ack_recd[i] = 0; 727 fnic->fw_ack_index[i] = -1; 728 } 729 730 for (i = 0; i < FNIC_IO_LOCKS; i++) 731 spin_lock_init(&fnic->io_req_lock[i]); 732 733 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache); 734 if (!fnic->io_req_pool) 735 goto err_out_free_resources; 736 737 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]); 738 if (!pool) 739 goto err_out_free_ioreq_pool; 740 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool; 741 742 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]); 743 if (!pool) 744 goto err_out_free_dflt_pool; 745 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool; 746 747 /* setup vlan config, hw inserts vlan header */ 748 fnic->vlan_hw_insert = 1; 749 fnic->vlan_id = 0; 750 751 /* Initialize the FIP fcoe_ctrl struct */ 752 fnic->ctlr.send = fnic_eth_send; 753 fnic->ctlr.update_mac = fnic_update_mac; 754 fnic->ctlr.get_src_addr = fnic_get_mac; 755 if (fnic->config.flags & VFCF_FIP_CAPABLE) { 756 shost_printk(KERN_INFO, fnic->lport->host, 757 "firmware supports FIP\n"); 758 /* enable directed and multicast */ 759 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0); 760 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS); 761 vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr); 762 fnic->set_vlan = fnic_set_vlan; 763 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO); 764 timer_setup(&fnic->fip_timer, fnic_fip_notify_timer, 0); 765 spin_lock_init(&fnic->vlans_lock); 766 INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame); 767 INIT_WORK(&fnic->event_work, fnic_handle_event); 768 skb_queue_head_init(&fnic->fip_frame_queue); 769 INIT_LIST_HEAD(&fnic->evlist); 770 INIT_LIST_HEAD(&fnic->vlans); 771 } else { 772 shost_printk(KERN_INFO, fnic->lport->host, 773 "firmware uses non-FIP mode\n"); 774 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP); 775 fnic->ctlr.state = FIP_ST_NON_FIP; 776 } 777 fnic->state = FNIC_IN_FC_MODE; 778 779 atomic_set(&fnic->in_flight, 0); 780 fnic->state_flags = FNIC_FLAGS_NONE; 781 782 /* Enable hardware stripping of vlan header on ingress */ 783 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1); 784 785 /* Setup notification buffer area */ 786 err = fnic_notify_set(fnic); 787 if (err) { 788 shost_printk(KERN_ERR, fnic->lport->host, 789 "Failed to alloc notify buffer, aborting.\n"); 790 goto err_out_free_max_pool; 791 } 792 793 /* Setup notify timer when using MSI interrupts */ 794 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI) 795 timer_setup(&fnic->notify_timer, fnic_notify_timer, 0); 796 797 /* allocate RQ buffers and post them to RQ*/ 798 for (i = 0; i < fnic->rq_count; i++) { 799 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame); 800 if (err) { 801 shost_printk(KERN_ERR, fnic->lport->host, 802 "fnic_alloc_rq_frame can't alloc " 803 "frame\n"); 804 goto err_out_free_rq_buf; 805 } 806 } 807 808 /* 809 * Initialization done with PCI system, hardware, firmware. 810 * Add host to SCSI 811 */ 812 err = scsi_add_host(lp->host, &pdev->dev); 813 if (err) { 814 shost_printk(KERN_ERR, fnic->lport->host, 815 "fnic: scsi_add_host failed...exiting\n"); 816 goto err_out_free_rq_buf; 817 } 818 819 /* Start local port initiatialization */ 820 821 lp->link_up = 0; 822 823 lp->max_retry_count = fnic->config.flogi_retries; 824 lp->max_rport_retry_count = fnic->config.plogi_retries; 825 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS | 826 FCP_SPPF_CONF_COMPL); 827 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) 828 lp->service_params |= FCP_SPPF_RETRY; 829 830 lp->boot_time = jiffies; 831 lp->e_d_tov = fnic->config.ed_tov; 832 lp->r_a_tov = fnic->config.ra_tov; 833 lp->link_supported_speeds = FC_PORTSPEED_10GBIT; 834 fc_set_wwnn(lp, fnic->config.node_wwn); 835 fc_set_wwpn(lp, fnic->config.port_wwn); 836 837 fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0); 838 839 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START, 840 FCPIO_HOST_EXCH_RANGE_END, NULL)) { 841 err = -ENOMEM; 842 goto err_out_remove_scsi_host; 843 } 844 845 fc_lport_init_stats(lp); 846 fnic->stats_reset_time = jiffies; 847 848 fc_lport_config(lp); 849 850 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize + 851 sizeof(struct fc_frame_header))) { 852 err = -EINVAL; 853 goto err_out_free_exch_mgr; 854 } 855 fc_host_maxframe_size(lp->host) = lp->mfs; 856 fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000; 857 858 sprintf(fc_host_symbolic_name(lp->host), 859 DRV_NAME " v" DRV_VERSION " over %s", fnic->name); 860 861 spin_lock_irqsave(&fnic_list_lock, flags); 862 list_add_tail(&fnic->list, &fnic_list); 863 spin_unlock_irqrestore(&fnic_list_lock, flags); 864 865 INIT_WORK(&fnic->link_work, fnic_handle_link); 866 INIT_WORK(&fnic->frame_work, fnic_handle_frame); 867 skb_queue_head_init(&fnic->frame_queue); 868 skb_queue_head_init(&fnic->tx_queue); 869 870 /* Enable all queues */ 871 for (i = 0; i < fnic->raw_wq_count; i++) 872 vnic_wq_enable(&fnic->wq[i]); 873 for (i = 0; i < fnic->rq_count; i++) 874 vnic_rq_enable(&fnic->rq[i]); 875 for (i = 0; i < fnic->wq_copy_count; i++) 876 vnic_wq_copy_enable(&fnic->wq_copy[i]); 877 878 fc_fabric_login(lp); 879 880 vnic_dev_enable(fnic->vdev); 881 882 err = fnic_request_intr(fnic); 883 if (err) { 884 shost_printk(KERN_ERR, fnic->lport->host, 885 "Unable to request irq.\n"); 886 goto err_out_free_exch_mgr; 887 } 888 889 for (i = 0; i < fnic->intr_count; i++) 890 vnic_intr_unmask(&fnic->intr[i]); 891 892 fnic_notify_timer_start(fnic); 893 894 return 0; 895 896 err_out_free_exch_mgr: 897 fc_exch_mgr_free(lp); 898 err_out_remove_scsi_host: 899 fc_remove_host(lp->host); 900 scsi_remove_host(lp->host); 901 err_out_free_rq_buf: 902 for (i = 0; i < fnic->rq_count; i++) 903 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf); 904 vnic_dev_notify_unset(fnic->vdev); 905 err_out_free_max_pool: 906 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]); 907 err_out_free_dflt_pool: 908 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]); 909 err_out_free_ioreq_pool: 910 mempool_destroy(fnic->io_req_pool); 911 err_out_free_resources: 912 fnic_free_vnic_resources(fnic); 913 err_out_clear_intr: 914 fnic_clear_intr_mode(fnic); 915 err_out_dev_close: 916 vnic_dev_close(fnic->vdev); 917 err_out_vnic_unregister: 918 vnic_dev_unregister(fnic->vdev); 919 err_out_iounmap: 920 fnic_iounmap(fnic); 921 err_out_release_regions: 922 pci_release_regions(pdev); 923 err_out_disable_device: 924 pci_disable_device(pdev); 925 err_out_free_hba: 926 fnic_stats_debugfs_remove(fnic); 927 scsi_host_put(lp->host); 928 err_out: 929 return err; 930 } 931 932 static void fnic_remove(struct pci_dev *pdev) 933 { 934 struct fnic *fnic = pci_get_drvdata(pdev); 935 struct fc_lport *lp = fnic->lport; 936 unsigned long flags; 937 938 /* 939 * Mark state so that the workqueue thread stops forwarding 940 * received frames and link events to the local port. ISR and 941 * other threads that can queue work items will also stop 942 * creating work items on the fnic workqueue 943 */ 944 spin_lock_irqsave(&fnic->fnic_lock, flags); 945 fnic->stop_rx_link_events = 1; 946 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 947 948 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI) 949 del_timer_sync(&fnic->notify_timer); 950 951 /* 952 * Flush the fnic event queue. After this call, there should 953 * be no event queued for this fnic device in the workqueue 954 */ 955 flush_workqueue(fnic_event_queue); 956 skb_queue_purge(&fnic->frame_queue); 957 skb_queue_purge(&fnic->tx_queue); 958 959 if (fnic->config.flags & VFCF_FIP_CAPABLE) { 960 del_timer_sync(&fnic->fip_timer); 961 skb_queue_purge(&fnic->fip_frame_queue); 962 fnic_fcoe_reset_vlans(fnic); 963 fnic_fcoe_evlist_free(fnic); 964 } 965 966 /* 967 * Log off the fabric. This stops all remote ports, dns port, 968 * logs off the fabric. This flushes all rport, disc, lport work 969 * before returning 970 */ 971 fc_fabric_logoff(fnic->lport); 972 973 spin_lock_irqsave(&fnic->fnic_lock, flags); 974 fnic->in_remove = 1; 975 spin_unlock_irqrestore(&fnic->fnic_lock, flags); 976 977 fcoe_ctlr_destroy(&fnic->ctlr); 978 fc_lport_destroy(lp); 979 fnic_stats_debugfs_remove(fnic); 980 981 /* 982 * This stops the fnic device, masks all interrupts. Completed 983 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are 984 * cleaned up 985 */ 986 fnic_cleanup(fnic); 987 988 BUG_ON(!skb_queue_empty(&fnic->frame_queue)); 989 BUG_ON(!skb_queue_empty(&fnic->tx_queue)); 990 991 spin_lock_irqsave(&fnic_list_lock, flags); 992 list_del(&fnic->list); 993 spin_unlock_irqrestore(&fnic_list_lock, flags); 994 995 fc_remove_host(fnic->lport->host); 996 scsi_remove_host(fnic->lport->host); 997 fc_exch_mgr_free(fnic->lport); 998 vnic_dev_notify_unset(fnic->vdev); 999 fnic_free_intr(fnic); 1000 fnic_free_vnic_resources(fnic); 1001 fnic_clear_intr_mode(fnic); 1002 vnic_dev_close(fnic->vdev); 1003 vnic_dev_unregister(fnic->vdev); 1004 fnic_iounmap(fnic); 1005 pci_release_regions(pdev); 1006 pci_disable_device(pdev); 1007 scsi_host_put(lp->host); 1008 } 1009 1010 static struct pci_driver fnic_driver = { 1011 .name = DRV_NAME, 1012 .id_table = fnic_id_table, 1013 .probe = fnic_probe, 1014 .remove = fnic_remove, 1015 }; 1016 1017 static int __init fnic_init_module(void) 1018 { 1019 size_t len; 1020 int err = 0; 1021 1022 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION); 1023 1024 /* Create debugfs entries for fnic */ 1025 err = fnic_debugfs_init(); 1026 if (err < 0) { 1027 printk(KERN_ERR PFX "Failed to create fnic directory " 1028 "for tracing and stats logging\n"); 1029 fnic_debugfs_terminate(); 1030 } 1031 1032 /* Allocate memory for trace buffer */ 1033 err = fnic_trace_buf_init(); 1034 if (err < 0) { 1035 printk(KERN_ERR PFX 1036 "Trace buffer initialization Failed. " 1037 "Fnic Tracing utility is disabled\n"); 1038 fnic_trace_free(); 1039 } 1040 1041 /* Allocate memory for fc trace buffer */ 1042 err = fnic_fc_trace_init(); 1043 if (err < 0) { 1044 printk(KERN_ERR PFX "FC trace buffer initialization Failed " 1045 "FC frame tracing utility is disabled\n"); 1046 fnic_fc_trace_free(); 1047 } 1048 1049 /* Create a cache for allocation of default size sgls */ 1050 len = sizeof(struct fnic_dflt_sgl_list); 1051 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create 1052 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN, 1053 SLAB_HWCACHE_ALIGN, 1054 NULL); 1055 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) { 1056 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n"); 1057 err = -ENOMEM; 1058 goto err_create_fnic_sgl_slab_dflt; 1059 } 1060 1061 /* Create a cache for allocation of max size sgls*/ 1062 len = sizeof(struct fnic_sgl_list); 1063 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create 1064 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN, 1065 SLAB_HWCACHE_ALIGN, 1066 NULL); 1067 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) { 1068 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n"); 1069 err = -ENOMEM; 1070 goto err_create_fnic_sgl_slab_max; 1071 } 1072 1073 /* Create a cache of io_req structs for use via mempool */ 1074 fnic_io_req_cache = kmem_cache_create("fnic_io_req", 1075 sizeof(struct fnic_io_req), 1076 0, SLAB_HWCACHE_ALIGN, NULL); 1077 if (!fnic_io_req_cache) { 1078 printk(KERN_ERR PFX "failed to create fnic io_req slab\n"); 1079 err = -ENOMEM; 1080 goto err_create_fnic_ioreq_slab; 1081 } 1082 1083 fnic_event_queue = create_singlethread_workqueue("fnic_event_wq"); 1084 if (!fnic_event_queue) { 1085 printk(KERN_ERR PFX "fnic work queue create failed\n"); 1086 err = -ENOMEM; 1087 goto err_create_fnic_workq; 1088 } 1089 1090 spin_lock_init(&fnic_list_lock); 1091 INIT_LIST_HEAD(&fnic_list); 1092 1093 fnic_fip_queue = create_singlethread_workqueue("fnic_fip_q"); 1094 if (!fnic_fip_queue) { 1095 printk(KERN_ERR PFX "fnic FIP work queue create failed\n"); 1096 err = -ENOMEM; 1097 goto err_create_fip_workq; 1098 } 1099 1100 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions); 1101 if (!fnic_fc_transport) { 1102 printk(KERN_ERR PFX "fc_attach_transport error\n"); 1103 err = -ENOMEM; 1104 goto err_fc_transport; 1105 } 1106 1107 /* register the driver with PCI system */ 1108 err = pci_register_driver(&fnic_driver); 1109 if (err < 0) { 1110 printk(KERN_ERR PFX "pci register error\n"); 1111 goto err_pci_register; 1112 } 1113 return err; 1114 1115 err_pci_register: 1116 fc_release_transport(fnic_fc_transport); 1117 err_fc_transport: 1118 destroy_workqueue(fnic_fip_queue); 1119 err_create_fip_workq: 1120 destroy_workqueue(fnic_event_queue); 1121 err_create_fnic_workq: 1122 kmem_cache_destroy(fnic_io_req_cache); 1123 err_create_fnic_ioreq_slab: 1124 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]); 1125 err_create_fnic_sgl_slab_max: 1126 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]); 1127 err_create_fnic_sgl_slab_dflt: 1128 fnic_trace_free(); 1129 fnic_fc_trace_free(); 1130 fnic_debugfs_terminate(); 1131 return err; 1132 } 1133 1134 static void __exit fnic_cleanup_module(void) 1135 { 1136 pci_unregister_driver(&fnic_driver); 1137 destroy_workqueue(fnic_event_queue); 1138 if (fnic_fip_queue) { 1139 flush_workqueue(fnic_fip_queue); 1140 destroy_workqueue(fnic_fip_queue); 1141 } 1142 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]); 1143 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]); 1144 kmem_cache_destroy(fnic_io_req_cache); 1145 fc_release_transport(fnic_fc_transport); 1146 fnic_trace_free(); 1147 fnic_fc_trace_free(); 1148 fnic_debugfs_terminate(); 1149 } 1150 1151 module_init(fnic_init_module); 1152 module_exit(fnic_cleanup_module); 1153 1154