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