1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Linux MegaRAID device driver 5 * 6 * Copyright (c) 2003-2004 LSI Logic Corporation. 7 * 8 * FILE : megaraid_mbox.c 9 * Version : v2.20.5.1 (Nov 16 2006) 10 * 11 * Authors: 12 * Atul Mukker <Atul.Mukker@lsi.com> 13 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com> 14 * Manoj Jose <Manoj.Jose@lsi.com> 15 * Seokmann Ju 16 * 17 * List of supported controllers 18 * 19 * OEM Product Name VID DID SSVID SSID 20 * --- ------------ --- --- ---- ---- 21 * Dell PERC3/QC 101E 1960 1028 0471 22 * Dell PERC3/DC 101E 1960 1028 0493 23 * Dell PERC3/SC 101E 1960 1028 0475 24 * Dell PERC3/Di 1028 1960 1028 0123 25 * Dell PERC4/SC 1000 1960 1028 0520 26 * Dell PERC4/DC 1000 1960 1028 0518 27 * Dell PERC4/QC 1000 0407 1028 0531 28 * Dell PERC4/Di 1028 000F 1028 014A 29 * Dell PERC 4e/Si 1028 0013 1028 016c 30 * Dell PERC 4e/Di 1028 0013 1028 016d 31 * Dell PERC 4e/Di 1028 0013 1028 016e 32 * Dell PERC 4e/Di 1028 0013 1028 016f 33 * Dell PERC 4e/Di 1028 0013 1028 0170 34 * Dell PERC 4e/DC 1000 0408 1028 0002 35 * Dell PERC 4e/SC 1000 0408 1028 0001 36 * 37 * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520 38 * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520 39 * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518 40 * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530 41 * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532 42 * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531 43 * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001 44 * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002 45 * LSI MegaRAID SATA 150-4 1000 1960 1000 4523 46 * LSI MegaRAID SATA 150-6 1000 1960 1000 0523 47 * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004 48 * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008 49 * 50 * INTEL RAID Controller SRCU42X 1000 0407 8086 0532 51 * INTEL RAID Controller SRCS16 1000 1960 8086 0523 52 * INTEL RAID Controller SRCU42E 1000 0408 8086 0002 53 * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530 54 * INTEL RAID Controller SRCS28X 1000 0409 8086 3008 55 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431 56 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499 57 * INTEL RAID Controller SRCU51L 1000 1960 8086 0520 58 * 59 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065 60 * 61 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D 62 * 63 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287 64 * 65 * For history of changes, see Documentation/scsi/ChangeLog.megaraid 66 */ 67 68 #include <linux/slab.h> 69 #include <linux/module.h> 70 #include "megaraid_mbox.h" 71 72 static int megaraid_init(void); 73 static void megaraid_exit(void); 74 75 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *); 76 static void megaraid_detach_one(struct pci_dev *); 77 static void megaraid_mbox_shutdown(struct pci_dev *); 78 79 static int megaraid_io_attach(adapter_t *); 80 static void megaraid_io_detach(adapter_t *); 81 82 static int megaraid_init_mbox(adapter_t *); 83 static void megaraid_fini_mbox(adapter_t *); 84 85 static int megaraid_alloc_cmd_packets(adapter_t *); 86 static void megaraid_free_cmd_packets(adapter_t *); 87 88 static int megaraid_mbox_setup_dma_pools(adapter_t *); 89 static void megaraid_mbox_teardown_dma_pools(adapter_t *); 90 91 static int megaraid_sysfs_alloc_resources(adapter_t *); 92 static void megaraid_sysfs_free_resources(adapter_t *); 93 94 static int megaraid_abort_handler(struct scsi_cmnd *); 95 static int megaraid_reset_handler(struct scsi_cmnd *); 96 97 static int mbox_post_sync_cmd(adapter_t *, uint8_t []); 98 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []); 99 static int megaraid_busywait_mbox(mraid_device_t *); 100 static int megaraid_mbox_product_info(adapter_t *); 101 static int megaraid_mbox_extended_cdb(adapter_t *); 102 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *); 103 static int megaraid_mbox_support_random_del(adapter_t *); 104 static int megaraid_mbox_get_max_sg(adapter_t *); 105 static void megaraid_mbox_enum_raid_scsi(adapter_t *); 106 static void megaraid_mbox_flush_cache(adapter_t *); 107 static int megaraid_mbox_fire_sync_cmd(adapter_t *); 108 109 static void megaraid_mbox_display_scb(adapter_t *, scb_t *); 110 static void megaraid_mbox_setup_device_map(adapter_t *); 111 112 static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *); 113 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *); 114 static void megaraid_mbox_runpendq(adapter_t *, scb_t *); 115 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *, 116 struct scsi_cmnd *); 117 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *, 118 struct scsi_cmnd *); 119 120 static irqreturn_t megaraid_isr(int, void *); 121 122 static void megaraid_mbox_dpc(unsigned long); 123 124 static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *); 125 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *); 126 127 static int megaraid_cmm_register(adapter_t *); 128 static int megaraid_cmm_unregister(adapter_t *); 129 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t); 130 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *); 131 static void megaraid_mbox_mm_done(adapter_t *, scb_t *); 132 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *); 133 static int wait_till_fw_empty(adapter_t *); 134 135 136 137 MODULE_AUTHOR("megaraidlinux@lsi.com"); 138 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver"); 139 MODULE_LICENSE("GPL"); 140 MODULE_VERSION(MEGARAID_VERSION); 141 142 /* 143 * ### modules parameters for driver ### 144 */ 145 146 /* 147 * Set to enable driver to expose unconfigured disk to kernel 148 */ 149 static int megaraid_expose_unconf_disks = 0; 150 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0); 151 MODULE_PARM_DESC(unconf_disks, 152 "Set to expose unconfigured disks to kernel (default=0)"); 153 154 /* 155 * driver wait time if the adapter's mailbox is busy 156 */ 157 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT; 158 module_param_named(busy_wait, max_mbox_busy_wait, int, 0); 159 MODULE_PARM_DESC(busy_wait, 160 "Max wait for mailbox in microseconds if busy (default=10)"); 161 162 /* 163 * number of sectors per IO command 164 */ 165 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS; 166 module_param_named(max_sectors, megaraid_max_sectors, int, 0); 167 MODULE_PARM_DESC(max_sectors, 168 "Maximum number of sectors per IO command (default=128)"); 169 170 /* 171 * number of commands per logical unit 172 */ 173 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN; 174 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0); 175 MODULE_PARM_DESC(cmd_per_lun, 176 "Maximum number of commands per logical unit (default=64)"); 177 178 179 /* 180 * Fast driver load option, skip scanning for physical devices during load. 181 * This would result in non-disk devices being skipped during driver load 182 * time. These can be later added though, using /proc/scsi/scsi 183 */ 184 static unsigned int megaraid_fast_load = 0; 185 module_param_named(fast_load, megaraid_fast_load, int, 0); 186 MODULE_PARM_DESC(fast_load, 187 "Faster loading of the driver, skips physical devices! (default=0)"); 188 189 190 /* 191 * mraid_debug level - threshold for amount of information to be displayed by 192 * the driver. This level can be changed through modules parameters, ioctl or 193 * sysfs/proc interface. By default, print the announcement messages only. 194 */ 195 int mraid_debug_level = CL_ANN; 196 module_param_named(debug_level, mraid_debug_level, int, 0); 197 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)"); 198 199 /* 200 * PCI table for all supported controllers. 201 */ 202 static struct pci_device_id pci_id_table_g[] = { 203 { 204 PCI_VENDOR_ID_DELL, 205 PCI_DEVICE_ID_PERC4_DI_DISCOVERY, 206 PCI_VENDOR_ID_DELL, 207 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY, 208 }, 209 { 210 PCI_VENDOR_ID_LSI_LOGIC, 211 PCI_DEVICE_ID_PERC4_SC, 212 PCI_VENDOR_ID_DELL, 213 PCI_SUBSYS_ID_PERC4_SC, 214 }, 215 { 216 PCI_VENDOR_ID_LSI_LOGIC, 217 PCI_DEVICE_ID_PERC4_DC, 218 PCI_VENDOR_ID_DELL, 219 PCI_SUBSYS_ID_PERC4_DC, 220 }, 221 { 222 PCI_VENDOR_ID_LSI_LOGIC, 223 PCI_DEVICE_ID_VERDE, 224 PCI_ANY_ID, 225 PCI_ANY_ID, 226 }, 227 { 228 PCI_VENDOR_ID_DELL, 229 PCI_DEVICE_ID_PERC4_DI_EVERGLADES, 230 PCI_VENDOR_ID_DELL, 231 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES, 232 }, 233 { 234 PCI_VENDOR_ID_DELL, 235 PCI_DEVICE_ID_PERC4E_SI_BIGBEND, 236 PCI_VENDOR_ID_DELL, 237 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND, 238 }, 239 { 240 PCI_VENDOR_ID_DELL, 241 PCI_DEVICE_ID_PERC4E_DI_KOBUK, 242 PCI_VENDOR_ID_DELL, 243 PCI_SUBSYS_ID_PERC4E_DI_KOBUK, 244 }, 245 { 246 PCI_VENDOR_ID_DELL, 247 PCI_DEVICE_ID_PERC4E_DI_CORVETTE, 248 PCI_VENDOR_ID_DELL, 249 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE, 250 }, 251 { 252 PCI_VENDOR_ID_DELL, 253 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION, 254 PCI_VENDOR_ID_DELL, 255 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION, 256 }, 257 { 258 PCI_VENDOR_ID_DELL, 259 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE, 260 PCI_VENDOR_ID_DELL, 261 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE, 262 }, 263 { 264 PCI_VENDOR_ID_LSI_LOGIC, 265 PCI_DEVICE_ID_DOBSON, 266 PCI_ANY_ID, 267 PCI_ANY_ID, 268 }, 269 { 270 PCI_VENDOR_ID_AMI, 271 PCI_DEVICE_ID_AMI_MEGARAID3, 272 PCI_ANY_ID, 273 PCI_ANY_ID, 274 }, 275 { 276 PCI_VENDOR_ID_LSI_LOGIC, 277 PCI_DEVICE_ID_AMI_MEGARAID3, 278 PCI_ANY_ID, 279 PCI_ANY_ID, 280 }, 281 { 282 PCI_VENDOR_ID_LSI_LOGIC, 283 PCI_DEVICE_ID_LINDSAY, 284 PCI_ANY_ID, 285 PCI_ANY_ID, 286 }, 287 {0} /* Terminating entry */ 288 }; 289 MODULE_DEVICE_TABLE(pci, pci_id_table_g); 290 291 292 static struct pci_driver megaraid_pci_driver = { 293 .name = "megaraid", 294 .id_table = pci_id_table_g, 295 .probe = megaraid_probe_one, 296 .remove = megaraid_detach_one, 297 .shutdown = megaraid_mbox_shutdown, 298 }; 299 300 301 302 // definitions for the device attributes for exporting logical drive number 303 // for a scsi address (Host, Channel, Id, Lun) 304 305 static DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl, 306 NULL); 307 308 // Host template initializer for megaraid mbox sysfs device attributes 309 static struct device_attribute *megaraid_shost_attrs[] = { 310 &dev_attr_megaraid_mbox_app_hndl, 311 NULL, 312 }; 313 314 315 static DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL); 316 317 // Host template initializer for megaraid mbox sysfs device attributes 318 static struct device_attribute *megaraid_sdev_attrs[] = { 319 &dev_attr_megaraid_mbox_ld, 320 NULL, 321 }; 322 323 /* 324 * Scsi host template for megaraid unified driver 325 */ 326 static struct scsi_host_template megaraid_template_g = { 327 .module = THIS_MODULE, 328 .name = "LSI Logic MegaRAID driver", 329 .proc_name = "megaraid", 330 .queuecommand = megaraid_queue_command, 331 .eh_abort_handler = megaraid_abort_handler, 332 .eh_host_reset_handler = megaraid_reset_handler, 333 .change_queue_depth = scsi_change_queue_depth, 334 .no_write_same = 1, 335 .sdev_attrs = megaraid_sdev_attrs, 336 .shost_attrs = megaraid_shost_attrs, 337 }; 338 339 340 /** 341 * megaraid_init - module load hook 342 * 343 * We register ourselves as hotplug enabled module and let PCI subsystem 344 * discover our adapters. 345 */ 346 static int __init 347 megaraid_init(void) 348 { 349 int rval; 350 351 // Announce the driver version 352 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION, 353 MEGARAID_EXT_VERSION)); 354 355 // check validity of module parameters 356 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) { 357 358 con_log(CL_ANN, (KERN_WARNING 359 "megaraid mailbox: max commands per lun reset to %d\n", 360 MBOX_MAX_SCSI_CMDS)); 361 362 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS; 363 } 364 365 366 // register as a PCI hot-plug driver module 367 rval = pci_register_driver(&megaraid_pci_driver); 368 if (rval < 0) { 369 con_log(CL_ANN, (KERN_WARNING 370 "megaraid: could not register hotplug support.\n")); 371 } 372 373 return rval; 374 } 375 376 377 /** 378 * megaraid_exit - driver unload entry point 379 * 380 * We simply unwrap the megaraid_init routine here. 381 */ 382 static void __exit 383 megaraid_exit(void) 384 { 385 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n")); 386 387 // unregister as PCI hotplug driver 388 pci_unregister_driver(&megaraid_pci_driver); 389 390 return; 391 } 392 393 394 /** 395 * megaraid_probe_one - PCI hotplug entry point 396 * @pdev : handle to this controller's PCI configuration space 397 * @id : pci device id of the class of controllers 398 * 399 * This routine should be called whenever a new adapter is detected by the 400 * PCI hotplug susbsystem. 401 */ 402 static int 403 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id) 404 { 405 adapter_t *adapter; 406 407 408 // detected a new controller 409 con_log(CL_ANN, (KERN_INFO 410 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ", 411 pdev->vendor, pdev->device, pdev->subsystem_vendor, 412 pdev->subsystem_device)); 413 414 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number, 415 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn))); 416 417 if (pci_enable_device(pdev)) { 418 con_log(CL_ANN, (KERN_WARNING 419 "megaraid: pci_enable_device failed\n")); 420 421 return -ENODEV; 422 } 423 424 // Enable bus-mastering on this controller 425 pci_set_master(pdev); 426 427 // Allocate the per driver initialization structure 428 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL); 429 430 if (adapter == NULL) { 431 con_log(CL_ANN, (KERN_WARNING 432 "megaraid: out of memory, %s %d.\n", __func__, __LINE__)); 433 434 goto out_probe_one; 435 } 436 437 438 // set up PCI related soft state and other pre-known parameters 439 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn; 440 adapter->irq = pdev->irq; 441 adapter->pdev = pdev; 442 443 atomic_set(&adapter->being_detached, 0); 444 445 // Setup the default DMA mask. This would be changed later on 446 // depending on hardware capabilities 447 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) { 448 con_log(CL_ANN, (KERN_WARNING 449 "megaraid: dma_set_mask failed:%d\n", __LINE__)); 450 451 goto out_free_adapter; 452 } 453 454 455 // Initialize the synchronization lock for kernel and LLD 456 spin_lock_init(&adapter->lock); 457 458 // Initialize the command queues: the list of free SCBs and the list 459 // of pending SCBs. 460 INIT_LIST_HEAD(&adapter->kscb_pool); 461 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter)); 462 463 INIT_LIST_HEAD(&adapter->pend_list); 464 spin_lock_init(PENDING_LIST_LOCK(adapter)); 465 466 INIT_LIST_HEAD(&adapter->completed_list); 467 spin_lock_init(COMPLETED_LIST_LOCK(adapter)); 468 469 470 // Start the mailbox based controller 471 if (megaraid_init_mbox(adapter) != 0) { 472 con_log(CL_ANN, (KERN_WARNING 473 "megaraid: mailbox adapter did not initialize\n")); 474 475 goto out_free_adapter; 476 } 477 478 // Register with LSI Common Management Module 479 if (megaraid_cmm_register(adapter) != 0) { 480 481 con_log(CL_ANN, (KERN_WARNING 482 "megaraid: could not register with management module\n")); 483 484 goto out_fini_mbox; 485 } 486 487 // setup adapter handle in PCI soft state 488 pci_set_drvdata(pdev, adapter); 489 490 // attach with scsi mid-layer 491 if (megaraid_io_attach(adapter) != 0) { 492 493 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n")); 494 495 goto out_cmm_unreg; 496 } 497 498 return 0; 499 500 out_cmm_unreg: 501 megaraid_cmm_unregister(adapter); 502 out_fini_mbox: 503 megaraid_fini_mbox(adapter); 504 out_free_adapter: 505 kfree(adapter); 506 out_probe_one: 507 pci_disable_device(pdev); 508 509 return -ENODEV; 510 } 511 512 513 /** 514 * megaraid_detach_one - release framework resources and call LLD release routine 515 * @pdev : handle for our PCI configuration space 516 * 517 * This routine is called during driver unload. We free all the allocated 518 * resources and call the corresponding LLD so that it can also release all 519 * its resources. 520 * 521 * This routine is also called from the PCI hotplug system. 522 */ 523 static void 524 megaraid_detach_one(struct pci_dev *pdev) 525 { 526 adapter_t *adapter; 527 struct Scsi_Host *host; 528 529 530 // Start a rollback on this adapter 531 adapter = pci_get_drvdata(pdev); 532 533 if (!adapter) { 534 con_log(CL_ANN, (KERN_CRIT 535 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n", 536 pdev->vendor, pdev->device, pdev->subsystem_vendor, 537 pdev->subsystem_device)); 538 539 return; 540 } 541 else { 542 con_log(CL_ANN, (KERN_NOTICE 543 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n", 544 pdev->vendor, pdev->device, pdev->subsystem_vendor, 545 pdev->subsystem_device)); 546 } 547 548 549 host = adapter->host; 550 551 // do not allow any more requests from the management module for this 552 // adapter. 553 // FIXME: How do we account for the request which might still be 554 // pending with us? 555 atomic_set(&adapter->being_detached, 1); 556 557 // detach from the IO sub-system 558 megaraid_io_detach(adapter); 559 560 // Unregister from common management module 561 // 562 // FIXME: this must return success or failure for conditions if there 563 // is a command pending with LLD or not. 564 megaraid_cmm_unregister(adapter); 565 566 // finalize the mailbox based controller and release all resources 567 megaraid_fini_mbox(adapter); 568 569 kfree(adapter); 570 571 scsi_host_put(host); 572 573 pci_disable_device(pdev); 574 575 return; 576 } 577 578 579 /** 580 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA 581 * @pdev : generic driver model device 582 * 583 * Shutdown notification, perform flush cache. 584 */ 585 static void 586 megaraid_mbox_shutdown(struct pci_dev *pdev) 587 { 588 adapter_t *adapter = pci_get_drvdata(pdev); 589 static int counter; 590 591 if (!adapter) { 592 con_log(CL_ANN, (KERN_WARNING 593 "megaraid: null device in shutdown\n")); 594 return; 595 } 596 597 // flush caches now 598 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...", 599 counter++)); 600 601 megaraid_mbox_flush_cache(adapter); 602 603 con_log(CL_ANN, ("done\n")); 604 } 605 606 607 /** 608 * megaraid_io_attach - attach a device with the IO subsystem 609 * @adapter : controller's soft state 610 * 611 * Attach this device with the IO subsystem. 612 */ 613 static int 614 megaraid_io_attach(adapter_t *adapter) 615 { 616 struct Scsi_Host *host; 617 618 // Initialize SCSI Host structure 619 host = scsi_host_alloc(&megaraid_template_g, 8); 620 if (!host) { 621 con_log(CL_ANN, (KERN_WARNING 622 "megaraid mbox: scsi_register failed\n")); 623 624 return -1; 625 } 626 627 SCSIHOST2ADAP(host) = (caddr_t)adapter; 628 adapter->host = host; 629 630 host->irq = adapter->irq; 631 host->unique_id = adapter->unique_id; 632 host->can_queue = adapter->max_cmds; 633 host->this_id = adapter->init_id; 634 host->sg_tablesize = adapter->sglen; 635 host->max_sectors = adapter->max_sectors; 636 host->cmd_per_lun = adapter->cmd_per_lun; 637 host->max_channel = adapter->max_channel; 638 host->max_id = adapter->max_target; 639 host->max_lun = adapter->max_lun; 640 641 642 // notify mid-layer about the new controller 643 if (scsi_add_host(host, &adapter->pdev->dev)) { 644 645 con_log(CL_ANN, (KERN_WARNING 646 "megaraid mbox: scsi_add_host failed\n")); 647 648 scsi_host_put(host); 649 650 return -1; 651 } 652 653 scsi_scan_host(host); 654 655 return 0; 656 } 657 658 659 /** 660 * megaraid_io_detach - detach a device from the IO subsystem 661 * @adapter : controller's soft state 662 * 663 * Detach this device from the IO subsystem. 664 */ 665 static void 666 megaraid_io_detach(adapter_t *adapter) 667 { 668 struct Scsi_Host *host; 669 670 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n")); 671 672 host = adapter->host; 673 674 scsi_remove_host(host); 675 676 return; 677 } 678 679 680 /* 681 * START: Mailbox Low Level Driver 682 * 683 * This is section specific to the single mailbox based controllers 684 */ 685 686 /** 687 * megaraid_init_mbox - initialize controller 688 * @adapter : our soft state 689 * 690 * - Allocate 16-byte aligned mailbox memory for firmware handshake 691 * - Allocate controller's memory resources 692 * - Find out all initialization data 693 * - Allocate memory required for all the commands 694 * - Use internal library of FW routines, build up complete soft state 695 */ 696 static int 697 megaraid_init_mbox(adapter_t *adapter) 698 { 699 struct pci_dev *pdev; 700 mraid_device_t *raid_dev; 701 int i; 702 uint32_t magic64; 703 704 705 adapter->ito = MBOX_TIMEOUT; 706 pdev = adapter->pdev; 707 708 /* 709 * Allocate and initialize the init data structure for mailbox 710 * controllers 711 */ 712 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL); 713 if (raid_dev == NULL) return -1; 714 715 716 /* 717 * Attach the adapter soft state to raid device soft state 718 */ 719 adapter->raid_device = (caddr_t)raid_dev; 720 raid_dev->fast_load = megaraid_fast_load; 721 722 723 // our baseport 724 raid_dev->baseport = pci_resource_start(pdev, 0); 725 726 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) { 727 728 con_log(CL_ANN, (KERN_WARNING 729 "megaraid: mem region busy\n")); 730 731 goto out_free_raid_dev; 732 } 733 734 raid_dev->baseaddr = ioremap(raid_dev->baseport, 128); 735 736 if (!raid_dev->baseaddr) { 737 738 con_log(CL_ANN, (KERN_WARNING 739 "megaraid: could not map hba memory\n") ); 740 741 goto out_release_regions; 742 } 743 744 /* initialize the mutual exclusion lock for the mailbox */ 745 spin_lock_init(&raid_dev->mailbox_lock); 746 747 /* allocate memory required for commands */ 748 if (megaraid_alloc_cmd_packets(adapter) != 0) 749 goto out_iounmap; 750 751 /* 752 * Issue SYNC cmd to flush the pending cmds in the adapter 753 * and initialize its internal state 754 */ 755 756 if (megaraid_mbox_fire_sync_cmd(adapter)) 757 con_log(CL_ANN, ("megaraid: sync cmd failed\n")); 758 759 /* 760 * Setup the rest of the soft state using the library of 761 * FW routines 762 */ 763 764 /* request IRQ and register the interrupt service routine */ 765 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid", 766 adapter)) { 767 768 con_log(CL_ANN, (KERN_WARNING 769 "megaraid: Couldn't register IRQ %d!\n", adapter->irq)); 770 goto out_alloc_cmds; 771 772 } 773 774 // Product info 775 if (megaraid_mbox_product_info(adapter) != 0) 776 goto out_free_irq; 777 778 // Do we support extended CDBs 779 adapter->max_cdb_sz = 10; 780 if (megaraid_mbox_extended_cdb(adapter) == 0) { 781 adapter->max_cdb_sz = 16; 782 } 783 784 /* 785 * Do we support cluster environment, if we do, what is the initiator 786 * id. 787 * NOTE: In a non-cluster aware firmware environment, the LLD should 788 * return 7 as initiator id. 789 */ 790 adapter->ha = 0; 791 adapter->init_id = -1; 792 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) { 793 adapter->ha = 1; 794 } 795 796 /* 797 * Prepare the device ids array to have the mapping between the kernel 798 * device address and megaraid device address. 799 * We export the physical devices on their actual addresses. The 800 * logical drives are exported on a virtual SCSI channel 801 */ 802 megaraid_mbox_setup_device_map(adapter); 803 804 // If the firmware supports random deletion, update the device id map 805 if (megaraid_mbox_support_random_del(adapter)) { 806 807 // Change the logical drives numbers in device_ids array one 808 // slot in device_ids is reserved for target id, that's why 809 // "<=" below 810 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) { 811 adapter->device_ids[adapter->max_channel][i] += 0x80; 812 } 813 adapter->device_ids[adapter->max_channel][adapter->init_id] = 814 0xFF; 815 816 raid_dev->random_del_supported = 1; 817 } 818 819 /* 820 * find out the maximum number of scatter-gather elements supported by 821 * this firmware 822 */ 823 adapter->sglen = megaraid_mbox_get_max_sg(adapter); 824 825 // enumerate RAID and SCSI channels so that all devices on SCSI 826 // channels can later be exported, including disk devices 827 megaraid_mbox_enum_raid_scsi(adapter); 828 829 /* 830 * Other parameters required by upper layer 831 * 832 * maximum number of sectors per IO command 833 */ 834 adapter->max_sectors = megaraid_max_sectors; 835 836 /* 837 * number of queued commands per LUN. 838 */ 839 adapter->cmd_per_lun = megaraid_cmd_per_lun; 840 841 /* 842 * Allocate resources required to issue FW calls, when sysfs is 843 * accessed 844 */ 845 if (megaraid_sysfs_alloc_resources(adapter) != 0) 846 goto out_free_irq; 847 848 // Set the DMA mask to 64-bit. All supported controllers as capable of 849 // DMA in this range 850 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64); 851 852 if (((magic64 == HBA_SIGNATURE_64_BIT) && 853 ((adapter->pdev->subsystem_device != 854 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) && 855 (adapter->pdev->subsystem_device != 856 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) || 857 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC && 858 adapter->pdev->device == PCI_DEVICE_ID_VERDE) || 859 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC && 860 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) || 861 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC && 862 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) || 863 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL && 864 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) || 865 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL && 866 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) { 867 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) { 868 con_log(CL_ANN, (KERN_WARNING 869 "megaraid: DMA mask for 64-bit failed\n")); 870 871 if (dma_set_mask(&adapter->pdev->dev, 872 DMA_BIT_MASK(32))) { 873 con_log(CL_ANN, (KERN_WARNING 874 "megaraid: 32-bit DMA mask failed\n")); 875 goto out_free_sysfs_res; 876 } 877 } 878 } 879 880 // setup tasklet for DPC 881 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc, 882 (unsigned long)adapter); 883 884 con_log(CL_DLEVEL1, (KERN_INFO 885 "megaraid mbox hba successfully initialized\n")); 886 887 return 0; 888 889 out_free_sysfs_res: 890 megaraid_sysfs_free_resources(adapter); 891 out_free_irq: 892 free_irq(adapter->irq, adapter); 893 out_alloc_cmds: 894 megaraid_free_cmd_packets(adapter); 895 out_iounmap: 896 iounmap(raid_dev->baseaddr); 897 out_release_regions: 898 pci_release_regions(pdev); 899 out_free_raid_dev: 900 kfree(raid_dev); 901 902 return -1; 903 } 904 905 906 /** 907 * megaraid_fini_mbox - undo controller initialization 908 * @adapter : our soft state 909 */ 910 static void 911 megaraid_fini_mbox(adapter_t *adapter) 912 { 913 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 914 915 // flush all caches 916 megaraid_mbox_flush_cache(adapter); 917 918 tasklet_kill(&adapter->dpc_h); 919 920 megaraid_sysfs_free_resources(adapter); 921 922 megaraid_free_cmd_packets(adapter); 923 924 free_irq(adapter->irq, adapter); 925 926 iounmap(raid_dev->baseaddr); 927 928 pci_release_regions(adapter->pdev); 929 930 kfree(raid_dev); 931 932 return; 933 } 934 935 936 /** 937 * megaraid_alloc_cmd_packets - allocate shared mailbox 938 * @adapter : soft state of the raid controller 939 * 940 * Allocate and align the shared mailbox. This mailbox is used to issue 941 * all the commands. For IO based controllers, the mailbox is also registered 942 * with the FW. Allocate memory for all commands as well. 943 * This is our big allocator. 944 */ 945 static int 946 megaraid_alloc_cmd_packets(adapter_t *adapter) 947 { 948 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 949 struct pci_dev *pdev; 950 unsigned long align; 951 scb_t *scb; 952 mbox_ccb_t *ccb; 953 struct mraid_pci_blk *epthru_pci_blk; 954 struct mraid_pci_blk *sg_pci_blk; 955 struct mraid_pci_blk *mbox_pci_blk; 956 int i; 957 958 pdev = adapter->pdev; 959 960 /* 961 * Setup the mailbox 962 * Allocate the common 16-byte aligned memory for the handshake 963 * mailbox. 964 */ 965 raid_dev->una_mbox64 = dma_alloc_coherent(&adapter->pdev->dev, 966 sizeof(mbox64_t), 967 &raid_dev->una_mbox64_dma, 968 GFP_KERNEL); 969 970 if (!raid_dev->una_mbox64) { 971 con_log(CL_ANN, (KERN_WARNING 972 "megaraid: out of memory, %s %d\n", __func__, 973 __LINE__)); 974 return -1; 975 } 976 977 /* 978 * Align the mailbox at 16-byte boundary 979 */ 980 raid_dev->mbox = &raid_dev->una_mbox64->mbox32; 981 982 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) & 983 (~0UL ^ 0xFUL)); 984 985 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8); 986 987 align = ((void *)raid_dev->mbox - 988 ((void *)&raid_dev->una_mbox64->mbox32)); 989 990 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 + 991 align; 992 993 // Allocate memory for commands issued internally 994 adapter->ibuf = dma_alloc_coherent(&pdev->dev, MBOX_IBUF_SIZE, 995 &adapter->ibuf_dma_h, GFP_KERNEL); 996 if (!adapter->ibuf) { 997 998 con_log(CL_ANN, (KERN_WARNING 999 "megaraid: out of memory, %s %d\n", __func__, 1000 __LINE__)); 1001 1002 goto out_free_common_mbox; 1003 } 1004 1005 // Allocate memory for our SCSI Command Blocks and their associated 1006 // memory 1007 1008 /* 1009 * Allocate memory for the base list of scb. Later allocate memory for 1010 * CCBs and embedded components of each CCB and point the pointers in 1011 * scb to the allocated components 1012 * NOTE: The code to allocate SCB will be duplicated in all the LLD 1013 * since the calling routine does not yet know the number of available 1014 * commands. 1015 */ 1016 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL); 1017 1018 if (adapter->kscb_list == NULL) { 1019 con_log(CL_ANN, (KERN_WARNING 1020 "megaraid: out of memory, %s %d\n", __func__, 1021 __LINE__)); 1022 goto out_free_ibuf; 1023 } 1024 1025 // memory allocation for our command packets 1026 if (megaraid_mbox_setup_dma_pools(adapter) != 0) { 1027 con_log(CL_ANN, (KERN_WARNING 1028 "megaraid: out of memory, %s %d\n", __func__, 1029 __LINE__)); 1030 goto out_free_scb_list; 1031 } 1032 1033 // Adjust the scb pointers and link in the free pool 1034 epthru_pci_blk = raid_dev->epthru_pool; 1035 sg_pci_blk = raid_dev->sg_pool; 1036 mbox_pci_blk = raid_dev->mbox_pool; 1037 1038 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) { 1039 scb = adapter->kscb_list + i; 1040 ccb = raid_dev->ccb_list + i; 1041 1042 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16); 1043 ccb->raw_mbox = (uint8_t *)ccb->mbox; 1044 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8); 1045 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16; 1046 1047 // make sure the mailbox is aligned properly 1048 if (ccb->mbox_dma_h & 0x0F) { 1049 con_log(CL_ANN, (KERN_CRIT 1050 "megaraid mbox: not aligned on 16-bytes\n")); 1051 1052 goto out_teardown_dma_pools; 1053 } 1054 1055 ccb->epthru = (mraid_epassthru_t *) 1056 epthru_pci_blk[i].vaddr; 1057 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr; 1058 ccb->pthru = (mraid_passthru_t *)ccb->epthru; 1059 ccb->pthru_dma_h = ccb->epthru_dma_h; 1060 1061 1062 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr; 1063 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr; 1064 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64; 1065 1066 scb->ccb = (caddr_t)ccb; 1067 scb->gp = 0; 1068 1069 scb->sno = i; // command index 1070 1071 scb->scp = NULL; 1072 scb->state = SCB_FREE; 1073 scb->dma_direction = DMA_NONE; 1074 scb->dma_type = MRAID_DMA_NONE; 1075 scb->dev_channel = -1; 1076 scb->dev_target = -1; 1077 1078 // put scb in the free pool 1079 list_add_tail(&scb->list, &adapter->kscb_pool); 1080 } 1081 1082 return 0; 1083 1084 out_teardown_dma_pools: 1085 megaraid_mbox_teardown_dma_pools(adapter); 1086 out_free_scb_list: 1087 kfree(adapter->kscb_list); 1088 out_free_ibuf: 1089 dma_free_coherent(&pdev->dev, MBOX_IBUF_SIZE, (void *)adapter->ibuf, 1090 adapter->ibuf_dma_h); 1091 out_free_common_mbox: 1092 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t), 1093 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma); 1094 1095 return -1; 1096 } 1097 1098 1099 /** 1100 * megaraid_free_cmd_packets - free memory 1101 * @adapter : soft state of the raid controller 1102 * 1103 * Release memory resources allocated for commands. 1104 */ 1105 static void 1106 megaraid_free_cmd_packets(adapter_t *adapter) 1107 { 1108 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 1109 1110 megaraid_mbox_teardown_dma_pools(adapter); 1111 1112 kfree(adapter->kscb_list); 1113 1114 dma_free_coherent(&adapter->pdev->dev, MBOX_IBUF_SIZE, 1115 (void *)adapter->ibuf, adapter->ibuf_dma_h); 1116 1117 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t), 1118 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma); 1119 return; 1120 } 1121 1122 1123 /** 1124 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets 1125 * @adapter : HBA soft state 1126 * 1127 * Setup the dma pools for mailbox, passthru and extended passthru structures, 1128 * and scatter-gather lists. 1129 */ 1130 static int 1131 megaraid_mbox_setup_dma_pools(adapter_t *adapter) 1132 { 1133 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 1134 struct mraid_pci_blk *epthru_pci_blk; 1135 struct mraid_pci_blk *sg_pci_blk; 1136 struct mraid_pci_blk *mbox_pci_blk; 1137 int i; 1138 1139 1140 1141 // Allocate memory for 16-bytes aligned mailboxes 1142 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool", 1143 &adapter->pdev->dev, 1144 sizeof(mbox64_t) + 16, 1145 16, 0); 1146 1147 if (raid_dev->mbox_pool_handle == NULL) { 1148 goto fail_setup_dma_pool; 1149 } 1150 1151 mbox_pci_blk = raid_dev->mbox_pool; 1152 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) { 1153 mbox_pci_blk[i].vaddr = dma_pool_alloc( 1154 raid_dev->mbox_pool_handle, 1155 GFP_KERNEL, 1156 &mbox_pci_blk[i].dma_addr); 1157 if (!mbox_pci_blk[i].vaddr) { 1158 goto fail_setup_dma_pool; 1159 } 1160 } 1161 1162 /* 1163 * Allocate memory for each embedded passthru strucuture pointer 1164 * Request for a 128 bytes aligned structure for each passthru command 1165 * structure 1166 * Since passthru and extended passthru commands are exclusive, they 1167 * share common memory pool. Passthru structures piggyback on memory 1168 * allocated to extended passthru since passthru is smaller of the two 1169 */ 1170 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru", 1171 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0); 1172 1173 if (raid_dev->epthru_pool_handle == NULL) { 1174 goto fail_setup_dma_pool; 1175 } 1176 1177 epthru_pci_blk = raid_dev->epthru_pool; 1178 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) { 1179 epthru_pci_blk[i].vaddr = dma_pool_alloc( 1180 raid_dev->epthru_pool_handle, 1181 GFP_KERNEL, 1182 &epthru_pci_blk[i].dma_addr); 1183 if (!epthru_pci_blk[i].vaddr) { 1184 goto fail_setup_dma_pool; 1185 } 1186 } 1187 1188 1189 // Allocate memory for each scatter-gather list. Request for 512 bytes 1190 // alignment for each sg list 1191 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg", 1192 &adapter->pdev->dev, 1193 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE, 1194 512, 0); 1195 1196 if (raid_dev->sg_pool_handle == NULL) { 1197 goto fail_setup_dma_pool; 1198 } 1199 1200 sg_pci_blk = raid_dev->sg_pool; 1201 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) { 1202 sg_pci_blk[i].vaddr = dma_pool_alloc( 1203 raid_dev->sg_pool_handle, 1204 GFP_KERNEL, 1205 &sg_pci_blk[i].dma_addr); 1206 if (!sg_pci_blk[i].vaddr) { 1207 goto fail_setup_dma_pool; 1208 } 1209 } 1210 1211 return 0; 1212 1213 fail_setup_dma_pool: 1214 megaraid_mbox_teardown_dma_pools(adapter); 1215 return -1; 1216 } 1217 1218 1219 /** 1220 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets 1221 * @adapter : HBA soft state 1222 * 1223 * Teardown the dma pool for mailbox, passthru and extended passthru 1224 * structures, and scatter-gather lists. 1225 */ 1226 static void 1227 megaraid_mbox_teardown_dma_pools(adapter_t *adapter) 1228 { 1229 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 1230 struct mraid_pci_blk *epthru_pci_blk; 1231 struct mraid_pci_blk *sg_pci_blk; 1232 struct mraid_pci_blk *mbox_pci_blk; 1233 int i; 1234 1235 1236 sg_pci_blk = raid_dev->sg_pool; 1237 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) { 1238 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr, 1239 sg_pci_blk[i].dma_addr); 1240 } 1241 dma_pool_destroy(raid_dev->sg_pool_handle); 1242 1243 1244 epthru_pci_blk = raid_dev->epthru_pool; 1245 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) { 1246 dma_pool_free(raid_dev->epthru_pool_handle, 1247 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr); 1248 } 1249 dma_pool_destroy(raid_dev->epthru_pool_handle); 1250 1251 1252 mbox_pci_blk = raid_dev->mbox_pool; 1253 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) { 1254 dma_pool_free(raid_dev->mbox_pool_handle, 1255 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr); 1256 } 1257 dma_pool_destroy(raid_dev->mbox_pool_handle); 1258 1259 return; 1260 } 1261 1262 1263 /** 1264 * megaraid_alloc_scb - detach and return a scb from the free list 1265 * @adapter : controller's soft state 1266 * @scp : pointer to the scsi command to be executed 1267 * 1268 * Return the scb from the head of the free list. %NULL if there are none 1269 * available. 1270 */ 1271 static scb_t * 1272 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp) 1273 { 1274 struct list_head *head = &adapter->kscb_pool; 1275 scb_t *scb = NULL; 1276 unsigned long flags; 1277 1278 // detach scb from free pool 1279 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags); 1280 1281 if (list_empty(head)) { 1282 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags); 1283 return NULL; 1284 } 1285 1286 scb = list_entry(head->next, scb_t, list); 1287 list_del_init(&scb->list); 1288 1289 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags); 1290 1291 scb->state = SCB_ACTIVE; 1292 scb->scp = scp; 1293 scb->dma_type = MRAID_DMA_NONE; 1294 1295 return scb; 1296 } 1297 1298 1299 /** 1300 * megaraid_dealloc_scb - return the scb to the free pool 1301 * @adapter : controller's soft state 1302 * @scb : scb to be freed 1303 * 1304 * Return the scb back to the free list of scbs. The caller must 'flush' the 1305 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc. 1306 * NOTE NOTE: Make sure the scb is not on any list before calling this 1307 * routine. 1308 */ 1309 static inline void 1310 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb) 1311 { 1312 unsigned long flags; 1313 1314 // put scb in the free pool 1315 scb->state = SCB_FREE; 1316 scb->scp = NULL; 1317 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags); 1318 1319 list_add(&scb->list, &adapter->kscb_pool); 1320 1321 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags); 1322 1323 return; 1324 } 1325 1326 1327 /** 1328 * megaraid_mbox_mksgl - make the scatter-gather list 1329 * @adapter : controller's soft state 1330 * @scb : scsi control block 1331 * 1332 * Prepare the scatter-gather list. 1333 */ 1334 static int 1335 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb) 1336 { 1337 struct scatterlist *sgl; 1338 mbox_ccb_t *ccb; 1339 struct scsi_cmnd *scp; 1340 int sgcnt; 1341 int i; 1342 1343 1344 scp = scb->scp; 1345 ccb = (mbox_ccb_t *)scb->ccb; 1346 1347 sgcnt = scsi_dma_map(scp); 1348 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen); 1349 1350 // no mapping required if no data to be transferred 1351 if (!sgcnt) 1352 return 0; 1353 1354 scb->dma_type = MRAID_DMA_WSG; 1355 1356 scsi_for_each_sg(scp, sgl, sgcnt, i) { 1357 ccb->sgl64[i].address = sg_dma_address(sgl); 1358 ccb->sgl64[i].length = sg_dma_len(sgl); 1359 } 1360 1361 // Return count of SG nodes 1362 return sgcnt; 1363 } 1364 1365 1366 /** 1367 * mbox_post_cmd - issue a mailbox command 1368 * @adapter : controller's soft state 1369 * @scb : command to be issued 1370 * 1371 * Post the command to the controller if mailbox is available. 1372 */ 1373 static int 1374 mbox_post_cmd(adapter_t *adapter, scb_t *scb) 1375 { 1376 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 1377 mbox64_t *mbox64; 1378 mbox_t *mbox; 1379 mbox_ccb_t *ccb; 1380 unsigned long flags; 1381 unsigned int i = 0; 1382 1383 1384 ccb = (mbox_ccb_t *)scb->ccb; 1385 mbox = raid_dev->mbox; 1386 mbox64 = raid_dev->mbox64; 1387 1388 /* 1389 * Check for busy mailbox. If it is, return failure - the caller 1390 * should retry later. 1391 */ 1392 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags); 1393 1394 if (unlikely(mbox->busy)) { 1395 do { 1396 udelay(1); 1397 i++; 1398 rmb(); 1399 } while(mbox->busy && (i < max_mbox_busy_wait)); 1400 1401 if (mbox->busy) { 1402 1403 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags); 1404 1405 return -1; 1406 } 1407 } 1408 1409 1410 // Copy this command's mailbox data into "adapter's" mailbox 1411 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22); 1412 mbox->cmdid = scb->sno; 1413 1414 adapter->outstanding_cmds++; 1415 1416 mbox->busy = 1; // Set busy 1417 mbox->poll = 0; 1418 mbox->ack = 0; 1419 wmb(); 1420 1421 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1); 1422 1423 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags); 1424 1425 return 0; 1426 } 1427 1428 1429 /** 1430 * megaraid_queue_command_lck - generic queue entry point for all LLDs 1431 * @scp : pointer to the scsi command to be executed 1432 * @done : callback routine to be called after the cmd has be completed 1433 * 1434 * Queue entry point for mailbox based controllers. 1435 */ 1436 static int 1437 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *)) 1438 { 1439 adapter_t *adapter; 1440 scb_t *scb; 1441 int if_busy; 1442 1443 adapter = SCP2ADAPTER(scp); 1444 scp->scsi_done = done; 1445 scp->result = 0; 1446 1447 /* 1448 * Allocate and build a SCB request 1449 * if_busy flag will be set if megaraid_mbox_build_cmd() command could 1450 * not allocate scb. We will return non-zero status in that case. 1451 * NOTE: scb can be null even though certain commands completed 1452 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would 1453 * return 0 in that case, and we would do the callback right away. 1454 */ 1455 if_busy = 0; 1456 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy); 1457 if (!scb) { // command already completed 1458 done(scp); 1459 return 0; 1460 } 1461 1462 megaraid_mbox_runpendq(adapter, scb); 1463 return if_busy; 1464 } 1465 1466 static DEF_SCSI_QCMD(megaraid_queue_command) 1467 1468 /** 1469 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands 1470 * @adapter : controller's soft state 1471 * @scp : mid-layer scsi command pointer 1472 * @busy : set if request could not be completed because of lack of 1473 * resources 1474 * 1475 * Transform the mid-layer scsi command to megaraid firmware lingua. 1476 * Convert the command issued by mid-layer to format understood by megaraid 1477 * firmware. We also complete certain commands without sending them to firmware. 1478 */ 1479 static scb_t * 1480 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy) 1481 { 1482 mraid_device_t *rdev = ADAP2RAIDDEV(adapter); 1483 int channel; 1484 int target; 1485 int islogical; 1486 mbox_ccb_t *ccb; 1487 mraid_passthru_t *pthru; 1488 mbox64_t *mbox64; 1489 mbox_t *mbox; 1490 scb_t *scb; 1491 char skip[] = "skipping"; 1492 char scan[] = "scanning"; 1493 char *ss; 1494 1495 1496 /* 1497 * Get the appropriate device map for the device this command is 1498 * intended for 1499 */ 1500 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical); 1501 1502 /* 1503 * Logical drive commands 1504 */ 1505 if (islogical) { 1506 switch (scp->cmnd[0]) { 1507 case TEST_UNIT_READY: 1508 /* 1509 * Do we support clustering and is the support enabled 1510 * If no, return success always 1511 */ 1512 if (!adapter->ha) { 1513 scp->result = (DID_OK << 16); 1514 return NULL; 1515 } 1516 1517 if (!(scb = megaraid_alloc_scb(adapter, scp))) { 1518 scp->result = (DID_ERROR << 16); 1519 *busy = 1; 1520 return NULL; 1521 } 1522 1523 scb->dma_direction = scp->sc_data_direction; 1524 scb->dev_channel = 0xFF; 1525 scb->dev_target = target; 1526 ccb = (mbox_ccb_t *)scb->ccb; 1527 1528 /* 1529 * The command id will be provided by the command 1530 * issuance routine 1531 */ 1532 ccb->raw_mbox[0] = CLUSTER_CMD; 1533 ccb->raw_mbox[2] = RESERVATION_STATUS; 1534 ccb->raw_mbox[3] = target; 1535 1536 return scb; 1537 1538 case MODE_SENSE: 1539 { 1540 struct scatterlist *sgl; 1541 caddr_t vaddr; 1542 1543 sgl = scsi_sglist(scp); 1544 if (sg_page(sgl)) { 1545 vaddr = (caddr_t) sg_virt(&sgl[0]); 1546 1547 memset(vaddr, 0, scp->cmnd[4]); 1548 } 1549 else { 1550 con_log(CL_ANN, (KERN_WARNING 1551 "megaraid mailbox: invalid sg:%d\n", 1552 __LINE__)); 1553 } 1554 } 1555 scp->result = (DID_OK << 16); 1556 return NULL; 1557 1558 case INQUIRY: 1559 /* 1560 * Display the channel scan for logical drives 1561 * Do not display scan for a channel if already done. 1562 */ 1563 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) { 1564 1565 con_log(CL_ANN, (KERN_INFO 1566 "scsi[%d]: scanning scsi channel %d", 1567 adapter->host->host_no, 1568 SCP2CHANNEL(scp))); 1569 1570 con_log(CL_ANN, ( 1571 " [virtual] for logical drives\n")); 1572 1573 rdev->last_disp |= (1L << SCP2CHANNEL(scp)); 1574 } 1575 1576 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) { 1577 scsi_build_sense(scp, 0, ILLEGAL_REQUEST, 1578 MEGA_INVALID_FIELD_IN_CDB, 0); 1579 return NULL; 1580 } 1581 1582 fallthrough; 1583 1584 case READ_CAPACITY: 1585 /* 1586 * Do not allow LUN > 0 for logical drives and 1587 * requests for more than 40 logical drives 1588 */ 1589 if (SCP2LUN(scp)) { 1590 scp->result = (DID_BAD_TARGET << 16); 1591 return NULL; 1592 } 1593 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) { 1594 scp->result = (DID_BAD_TARGET << 16); 1595 return NULL; 1596 } 1597 1598 1599 /* Allocate a SCB and initialize passthru */ 1600 if (!(scb = megaraid_alloc_scb(adapter, scp))) { 1601 scp->result = (DID_ERROR << 16); 1602 *busy = 1; 1603 return NULL; 1604 } 1605 1606 ccb = (mbox_ccb_t *)scb->ccb; 1607 scb->dev_channel = 0xFF; 1608 scb->dev_target = target; 1609 pthru = ccb->pthru; 1610 mbox = ccb->mbox; 1611 mbox64 = ccb->mbox64; 1612 1613 pthru->timeout = 0; 1614 pthru->ars = 1; 1615 pthru->reqsenselen = 14; 1616 pthru->islogical = 1; 1617 pthru->logdrv = target; 1618 pthru->cdblen = scp->cmd_len; 1619 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len); 1620 1621 mbox->cmd = MBOXCMD_PASSTHRU64; 1622 scb->dma_direction = scp->sc_data_direction; 1623 1624 pthru->dataxferlen = scsi_bufflen(scp); 1625 pthru->dataxferaddr = ccb->sgl_dma_h; 1626 pthru->numsge = megaraid_mbox_mksgl(adapter, 1627 scb); 1628 1629 mbox->xferaddr = 0xFFFFFFFF; 1630 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h; 1631 mbox64->xferaddr_hi = 0; 1632 1633 return scb; 1634 1635 case READ_6: 1636 case WRITE_6: 1637 case READ_10: 1638 case WRITE_10: 1639 case READ_12: 1640 case WRITE_12: 1641 1642 /* 1643 * Allocate a SCB and initialize mailbox 1644 */ 1645 if (!(scb = megaraid_alloc_scb(adapter, scp))) { 1646 scp->result = (DID_ERROR << 16); 1647 *busy = 1; 1648 return NULL; 1649 } 1650 ccb = (mbox_ccb_t *)scb->ccb; 1651 scb->dev_channel = 0xFF; 1652 scb->dev_target = target; 1653 mbox = ccb->mbox; 1654 mbox64 = ccb->mbox64; 1655 mbox->logdrv = target; 1656 1657 /* 1658 * A little HACK: 2nd bit is zero for all scsi read 1659 * commands and is set for all scsi write commands 1660 */ 1661 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64: 1662 MBOXCMD_LREAD64 ; 1663 1664 /* 1665 * 6-byte READ(0x08) or WRITE(0x0A) cdb 1666 */ 1667 if (scp->cmd_len == 6) { 1668 mbox->numsectors = (uint32_t)scp->cmnd[4]; 1669 mbox->lba = 1670 ((uint32_t)scp->cmnd[1] << 16) | 1671 ((uint32_t)scp->cmnd[2] << 8) | 1672 (uint32_t)scp->cmnd[3]; 1673 1674 mbox->lba &= 0x1FFFFF; 1675 } 1676 1677 /* 1678 * 10-byte READ(0x28) or WRITE(0x2A) cdb 1679 */ 1680 else if (scp->cmd_len == 10) { 1681 mbox->numsectors = 1682 (uint32_t)scp->cmnd[8] | 1683 ((uint32_t)scp->cmnd[7] << 8); 1684 mbox->lba = 1685 ((uint32_t)scp->cmnd[2] << 24) | 1686 ((uint32_t)scp->cmnd[3] << 16) | 1687 ((uint32_t)scp->cmnd[4] << 8) | 1688 (uint32_t)scp->cmnd[5]; 1689 } 1690 1691 /* 1692 * 12-byte READ(0xA8) or WRITE(0xAA) cdb 1693 */ 1694 else if (scp->cmd_len == 12) { 1695 mbox->lba = 1696 ((uint32_t)scp->cmnd[2] << 24) | 1697 ((uint32_t)scp->cmnd[3] << 16) | 1698 ((uint32_t)scp->cmnd[4] << 8) | 1699 (uint32_t)scp->cmnd[5]; 1700 1701 mbox->numsectors = 1702 ((uint32_t)scp->cmnd[6] << 24) | 1703 ((uint32_t)scp->cmnd[7] << 16) | 1704 ((uint32_t)scp->cmnd[8] << 8) | 1705 (uint32_t)scp->cmnd[9]; 1706 } 1707 else { 1708 con_log(CL_ANN, (KERN_WARNING 1709 "megaraid: unsupported CDB length\n")); 1710 1711 megaraid_dealloc_scb(adapter, scb); 1712 1713 scp->result = (DID_ERROR << 16); 1714 return NULL; 1715 } 1716 1717 scb->dma_direction = scp->sc_data_direction; 1718 1719 // Calculate Scatter-Gather info 1720 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h; 1721 mbox->numsge = megaraid_mbox_mksgl(adapter, 1722 scb); 1723 mbox->xferaddr = 0xFFFFFFFF; 1724 mbox64->xferaddr_hi = 0; 1725 1726 return scb; 1727 1728 case RESERVE: 1729 case RELEASE: 1730 /* 1731 * Do we support clustering and is the support enabled 1732 */ 1733 if (!adapter->ha) { 1734 scp->result = (DID_BAD_TARGET << 16); 1735 return NULL; 1736 } 1737 1738 /* 1739 * Allocate a SCB and initialize mailbox 1740 */ 1741 if (!(scb = megaraid_alloc_scb(adapter, scp))) { 1742 scp->result = (DID_ERROR << 16); 1743 *busy = 1; 1744 return NULL; 1745 } 1746 1747 ccb = (mbox_ccb_t *)scb->ccb; 1748 scb->dev_channel = 0xFF; 1749 scb->dev_target = target; 1750 ccb->raw_mbox[0] = CLUSTER_CMD; 1751 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ? 1752 RESERVE_LD : RELEASE_LD; 1753 1754 ccb->raw_mbox[3] = target; 1755 scb->dma_direction = scp->sc_data_direction; 1756 1757 return scb; 1758 1759 default: 1760 scp->result = (DID_BAD_TARGET << 16); 1761 return NULL; 1762 } 1763 } 1764 else { // Passthru device commands 1765 1766 // Do not allow access to target id > 15 or LUN > 7 1767 if (target > 15 || SCP2LUN(scp) > 7) { 1768 scp->result = (DID_BAD_TARGET << 16); 1769 return NULL; 1770 } 1771 1772 // if fast load option was set and scan for last device is 1773 // over, reset the fast_load flag so that during a possible 1774 // next scan, devices can be made available 1775 if (rdev->fast_load && (target == 15) && 1776 (SCP2CHANNEL(scp) == adapter->max_channel -1)) { 1777 1778 con_log(CL_ANN, (KERN_INFO 1779 "megaraid[%d]: physical device scan re-enabled\n", 1780 adapter->host->host_no)); 1781 rdev->fast_load = 0; 1782 } 1783 1784 /* 1785 * Display the channel scan for physical devices 1786 */ 1787 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) { 1788 1789 ss = rdev->fast_load ? skip : scan; 1790 1791 con_log(CL_ANN, (KERN_INFO 1792 "scsi[%d]: %s scsi channel %d [Phy %d]", 1793 adapter->host->host_no, ss, SCP2CHANNEL(scp), 1794 channel)); 1795 1796 con_log(CL_ANN, ( 1797 " for non-raid devices\n")); 1798 1799 rdev->last_disp |= (1L << SCP2CHANNEL(scp)); 1800 } 1801 1802 // disable channel sweep if fast load option given 1803 if (rdev->fast_load) { 1804 scp->result = (DID_BAD_TARGET << 16); 1805 return NULL; 1806 } 1807 1808 // Allocate a SCB and initialize passthru 1809 if (!(scb = megaraid_alloc_scb(adapter, scp))) { 1810 scp->result = (DID_ERROR << 16); 1811 *busy = 1; 1812 return NULL; 1813 } 1814 1815 ccb = (mbox_ccb_t *)scb->ccb; 1816 scb->dev_channel = channel; 1817 scb->dev_target = target; 1818 scb->dma_direction = scp->sc_data_direction; 1819 mbox = ccb->mbox; 1820 mbox64 = ccb->mbox64; 1821 1822 // Does this firmware support extended CDBs 1823 if (adapter->max_cdb_sz == 16) { 1824 mbox->cmd = MBOXCMD_EXTPTHRU; 1825 1826 megaraid_mbox_prepare_epthru(adapter, scb, scp); 1827 1828 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h; 1829 mbox64->xferaddr_hi = 0; 1830 mbox->xferaddr = 0xFFFFFFFF; 1831 } 1832 else { 1833 mbox->cmd = MBOXCMD_PASSTHRU64; 1834 1835 megaraid_mbox_prepare_pthru(adapter, scb, scp); 1836 1837 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h; 1838 mbox64->xferaddr_hi = 0; 1839 mbox->xferaddr = 0xFFFFFFFF; 1840 } 1841 return scb; 1842 } 1843 1844 // NOT REACHED 1845 } 1846 1847 1848 /** 1849 * megaraid_mbox_runpendq - execute commands queued in the pending queue 1850 * @adapter : controller's soft state 1851 * @scb_q : SCB to be queued in the pending list 1852 * 1853 * Scan the pending list for commands which are not yet issued and try to 1854 * post to the controller. The SCB can be a null pointer, which would indicate 1855 * no SCB to be queue, just try to execute the ones in the pending list. 1856 * 1857 * NOTE: We do not actually traverse the pending list. The SCBs are plucked 1858 * out from the head of the pending list. If it is successfully issued, the 1859 * next SCB is at the head now. 1860 */ 1861 static void 1862 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q) 1863 { 1864 scb_t *scb; 1865 unsigned long flags; 1866 1867 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags); 1868 1869 if (scb_q) { 1870 scb_q->state = SCB_PENDQ; 1871 list_add_tail(&scb_q->list, &adapter->pend_list); 1872 } 1873 1874 // if the adapter in not in quiescent mode, post the commands to FW 1875 if (adapter->quiescent) { 1876 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags); 1877 return; 1878 } 1879 1880 while (!list_empty(&adapter->pend_list)) { 1881 1882 assert_spin_locked(PENDING_LIST_LOCK(adapter)); 1883 1884 scb = list_entry(adapter->pend_list.next, scb_t, list); 1885 1886 // remove the scb from the pending list and try to 1887 // issue. If we are unable to issue it, put back in 1888 // the pending list and return 1889 1890 list_del_init(&scb->list); 1891 1892 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags); 1893 1894 // if mailbox was busy, return SCB back to pending 1895 // list. Make sure to add at the head, since that's 1896 // where it would have been removed from 1897 1898 scb->state = SCB_ISSUED; 1899 1900 if (mbox_post_cmd(adapter, scb) != 0) { 1901 1902 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags); 1903 1904 scb->state = SCB_PENDQ; 1905 1906 list_add(&scb->list, &adapter->pend_list); 1907 1908 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), 1909 flags); 1910 1911 return; 1912 } 1913 1914 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags); 1915 } 1916 1917 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags); 1918 1919 1920 return; 1921 } 1922 1923 1924 /** 1925 * megaraid_mbox_prepare_pthru - prepare a command for physical devices 1926 * @adapter : pointer to controller's soft state 1927 * @scb : scsi control block 1928 * @scp : scsi command from the mid-layer 1929 * 1930 * Prepare a command for the scsi physical devices. 1931 */ 1932 static void 1933 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb, 1934 struct scsi_cmnd *scp) 1935 { 1936 mbox_ccb_t *ccb; 1937 mraid_passthru_t *pthru; 1938 uint8_t channel; 1939 uint8_t target; 1940 1941 ccb = (mbox_ccb_t *)scb->ccb; 1942 pthru = ccb->pthru; 1943 channel = scb->dev_channel; 1944 target = scb->dev_target; 1945 1946 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout 1947 pthru->timeout = 4; 1948 pthru->ars = 1; 1949 pthru->islogical = 0; 1950 pthru->channel = 0; 1951 pthru->target = (channel << 4) | target; 1952 pthru->logdrv = SCP2LUN(scp); 1953 pthru->reqsenselen = 14; 1954 pthru->cdblen = scp->cmd_len; 1955 1956 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len); 1957 1958 if (scsi_bufflen(scp)) { 1959 pthru->dataxferlen = scsi_bufflen(scp); 1960 pthru->dataxferaddr = ccb->sgl_dma_h; 1961 pthru->numsge = megaraid_mbox_mksgl(adapter, scb); 1962 } 1963 else { 1964 pthru->dataxferaddr = 0; 1965 pthru->dataxferlen = 0; 1966 pthru->numsge = 0; 1967 } 1968 return; 1969 } 1970 1971 1972 /** 1973 * megaraid_mbox_prepare_epthru - prepare a command for physical devices 1974 * @adapter : pointer to controller's soft state 1975 * @scb : scsi control block 1976 * @scp : scsi command from the mid-layer 1977 * 1978 * Prepare a command for the scsi physical devices. This routine prepares 1979 * commands for devices which can take extended CDBs (>10 bytes). 1980 */ 1981 static void 1982 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb, 1983 struct scsi_cmnd *scp) 1984 { 1985 mbox_ccb_t *ccb; 1986 mraid_epassthru_t *epthru; 1987 uint8_t channel; 1988 uint8_t target; 1989 1990 ccb = (mbox_ccb_t *)scb->ccb; 1991 epthru = ccb->epthru; 1992 channel = scb->dev_channel; 1993 target = scb->dev_target; 1994 1995 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout 1996 epthru->timeout = 4; 1997 epthru->ars = 1; 1998 epthru->islogical = 0; 1999 epthru->channel = 0; 2000 epthru->target = (channel << 4) | target; 2001 epthru->logdrv = SCP2LUN(scp); 2002 epthru->reqsenselen = 14; 2003 epthru->cdblen = scp->cmd_len; 2004 2005 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len); 2006 2007 if (scsi_bufflen(scp)) { 2008 epthru->dataxferlen = scsi_bufflen(scp); 2009 epthru->dataxferaddr = ccb->sgl_dma_h; 2010 epthru->numsge = megaraid_mbox_mksgl(adapter, scb); 2011 } 2012 else { 2013 epthru->dataxferaddr = 0; 2014 epthru->dataxferlen = 0; 2015 epthru->numsge = 0; 2016 } 2017 return; 2018 } 2019 2020 2021 /** 2022 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs 2023 * @adapter : controller's soft state 2024 * 2025 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the 2026 * completed command and put them on the completed list for later processing. 2027 * 2028 * Returns: 1 if the interrupt is valid, 0 otherwise 2029 */ 2030 static int 2031 megaraid_ack_sequence(adapter_t *adapter) 2032 { 2033 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 2034 mbox_t *mbox; 2035 scb_t *scb; 2036 uint8_t nstatus; 2037 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS]; 2038 struct list_head clist; 2039 int handled; 2040 uint32_t dword; 2041 unsigned long flags; 2042 int i, j; 2043 2044 2045 mbox = raid_dev->mbox; 2046 2047 // move the SCBs from the firmware completed array to our local list 2048 INIT_LIST_HEAD(&clist); 2049 2050 // loop till F/W has more commands for us to complete 2051 handled = 0; 2052 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags); 2053 do { 2054 /* 2055 * Check if a valid interrupt is pending. If found, force the 2056 * interrupt line low. 2057 */ 2058 dword = RDOUTDOOR(raid_dev); 2059 if (dword != 0x10001234) break; 2060 2061 handled = 1; 2062 2063 WROUTDOOR(raid_dev, 0x10001234); 2064 2065 nstatus = 0; 2066 // wait for valid numstatus to post 2067 for (i = 0; i < 0xFFFFF; i++) { 2068 if (mbox->numstatus != 0xFF) { 2069 nstatus = mbox->numstatus; 2070 break; 2071 } 2072 rmb(); 2073 } 2074 mbox->numstatus = 0xFF; 2075 2076 adapter->outstanding_cmds -= nstatus; 2077 2078 for (i = 0; i < nstatus; i++) { 2079 2080 // wait for valid command index to post 2081 for (j = 0; j < 0xFFFFF; j++) { 2082 if (mbox->completed[i] != 0xFF) break; 2083 rmb(); 2084 } 2085 completed[i] = mbox->completed[i]; 2086 mbox->completed[i] = 0xFF; 2087 2088 if (completed[i] == 0xFF) { 2089 con_log(CL_ANN, (KERN_CRIT 2090 "megaraid: command posting timed out\n")); 2091 2092 BUG(); 2093 continue; 2094 } 2095 2096 // Get SCB associated with this command id 2097 if (completed[i] >= MBOX_MAX_SCSI_CMDS) { 2098 // a cmm command 2099 scb = adapter->uscb_list + (completed[i] - 2100 MBOX_MAX_SCSI_CMDS); 2101 } 2102 else { 2103 // an os command 2104 scb = adapter->kscb_list + completed[i]; 2105 } 2106 2107 scb->status = mbox->status; 2108 list_add_tail(&scb->list, &clist); 2109 } 2110 2111 // Acknowledge interrupt 2112 WRINDOOR(raid_dev, 0x02); 2113 2114 } while(1); 2115 2116 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags); 2117 2118 2119 // put the completed commands in the completed list. DPC would 2120 // complete these commands later 2121 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags); 2122 2123 list_splice(&clist, &adapter->completed_list); 2124 2125 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags); 2126 2127 2128 // schedule the DPC if there is some work for it 2129 if (handled) 2130 tasklet_schedule(&adapter->dpc_h); 2131 2132 return handled; 2133 } 2134 2135 2136 /** 2137 * megaraid_isr - isr for memory based mailbox based controllers 2138 * @irq : irq 2139 * @devp : pointer to our soft state 2140 * 2141 * Interrupt service routine for memory-mapped mailbox controllers. 2142 */ 2143 static irqreturn_t 2144 megaraid_isr(int irq, void *devp) 2145 { 2146 adapter_t *adapter = devp; 2147 int handled; 2148 2149 handled = megaraid_ack_sequence(adapter); 2150 2151 /* Loop through any pending requests */ 2152 if (!adapter->quiescent) { 2153 megaraid_mbox_runpendq(adapter, NULL); 2154 } 2155 2156 return IRQ_RETVAL(handled); 2157 } 2158 2159 2160 /** 2161 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list 2162 * @devp : pointer to HBA soft state 2163 * 2164 * Pick up the commands from the completed list and send back to the owners. 2165 * This is a reentrant function and does not assume any locks are held while 2166 * it is being called. 2167 */ 2168 static void 2169 megaraid_mbox_dpc(unsigned long devp) 2170 { 2171 adapter_t *adapter = (adapter_t *)devp; 2172 mraid_device_t *raid_dev; 2173 struct list_head clist; 2174 struct scatterlist *sgl; 2175 scb_t *scb; 2176 scb_t *tmp; 2177 struct scsi_cmnd *scp; 2178 mraid_passthru_t *pthru; 2179 mraid_epassthru_t *epthru; 2180 mbox_ccb_t *ccb; 2181 int islogical; 2182 int pdev_index; 2183 int pdev_state; 2184 mbox_t *mbox; 2185 unsigned long flags; 2186 uint8_t c; 2187 int status; 2188 uioc_t *kioc; 2189 2190 2191 if (!adapter) return; 2192 2193 raid_dev = ADAP2RAIDDEV(adapter); 2194 2195 // move the SCBs from the completed list to our local list 2196 INIT_LIST_HEAD(&clist); 2197 2198 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags); 2199 2200 list_splice_init(&adapter->completed_list, &clist); 2201 2202 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags); 2203 2204 2205 list_for_each_entry_safe(scb, tmp, &clist, list) { 2206 2207 status = scb->status; 2208 scp = scb->scp; 2209 ccb = (mbox_ccb_t *)scb->ccb; 2210 pthru = ccb->pthru; 2211 epthru = ccb->epthru; 2212 mbox = ccb->mbox; 2213 2214 // Make sure f/w has completed a valid command 2215 if (scb->state != SCB_ISSUED) { 2216 con_log(CL_ANN, (KERN_CRIT 2217 "megaraid critical err: invalid command %d:%d:%p\n", 2218 scb->sno, scb->state, scp)); 2219 BUG(); 2220 continue; // Must never happen! 2221 } 2222 2223 // check for the management command and complete it right away 2224 if (scb->sno >= MBOX_MAX_SCSI_CMDS) { 2225 scb->state = SCB_FREE; 2226 scb->status = status; 2227 2228 // remove from local clist 2229 list_del_init(&scb->list); 2230 2231 kioc = (uioc_t *)scb->gp; 2232 kioc->status = 0; 2233 2234 megaraid_mbox_mm_done(adapter, scb); 2235 2236 continue; 2237 } 2238 2239 // Was an abort issued for this command earlier 2240 if (scb->state & SCB_ABORT) { 2241 con_log(CL_ANN, (KERN_NOTICE 2242 "megaraid: aborted cmd [%x] completed\n", 2243 scb->sno)); 2244 } 2245 2246 /* 2247 * If the inquiry came of a disk drive which is not part of 2248 * any RAID array, expose it to the kernel. For this to be 2249 * enabled, user must set the "megaraid_expose_unconf_disks" 2250 * flag to 1 by specifying it on module parameter list. 2251 * This would enable data migration off drives from other 2252 * configurations. 2253 */ 2254 islogical = MRAID_IS_LOGICAL(adapter, scp); 2255 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0 2256 && IS_RAID_CH(raid_dev, scb->dev_channel)) { 2257 2258 sgl = scsi_sglist(scp); 2259 if (sg_page(sgl)) { 2260 c = *(unsigned char *) sg_virt(&sgl[0]); 2261 } else { 2262 con_log(CL_ANN, (KERN_WARNING 2263 "megaraid mailbox: invalid sg:%d\n", 2264 __LINE__)); 2265 c = 0; 2266 } 2267 2268 if ((c & 0x1F ) == TYPE_DISK) { 2269 pdev_index = (scb->dev_channel * 16) + 2270 scb->dev_target; 2271 pdev_state = 2272 raid_dev->pdrv_state[pdev_index] & 0x0F; 2273 2274 if (pdev_state == PDRV_ONLINE || 2275 pdev_state == PDRV_FAILED || 2276 pdev_state == PDRV_RBLD || 2277 pdev_state == PDRV_HOTSPARE || 2278 megaraid_expose_unconf_disks == 0) { 2279 2280 status = 0xF0; 2281 } 2282 } 2283 } 2284 2285 // Convert MegaRAID status to Linux error code 2286 switch (status) { 2287 2288 case 0x00: 2289 2290 scp->result = (DID_OK << 16); 2291 break; 2292 2293 case 0x02: 2294 2295 /* set sense_buffer and result fields */ 2296 if (mbox->cmd == MBOXCMD_PASSTHRU || 2297 mbox->cmd == MBOXCMD_PASSTHRU64) { 2298 2299 memcpy(scp->sense_buffer, pthru->reqsensearea, 2300 14); 2301 2302 scp->result = SAM_STAT_CHECK_CONDITION; 2303 } 2304 else { 2305 if (mbox->cmd == MBOXCMD_EXTPTHRU) { 2306 2307 memcpy(scp->sense_buffer, 2308 epthru->reqsensearea, 14); 2309 2310 scp->result = SAM_STAT_CHECK_CONDITION; 2311 } else 2312 scsi_build_sense(scp, 0, 2313 ABORTED_COMMAND, 0, 0); 2314 } 2315 break; 2316 2317 case 0x08: 2318 2319 scp->result = DID_BUS_BUSY << 16 | status; 2320 break; 2321 2322 default: 2323 2324 /* 2325 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS 2326 * failed 2327 */ 2328 if (scp->cmnd[0] == TEST_UNIT_READY) { 2329 scp->result = DID_ERROR << 16 | 2330 SAM_STAT_RESERVATION_CONFLICT; 2331 } 2332 else 2333 /* 2334 * Error code returned is 1 if Reserve or Release 2335 * failed or the input parameter is invalid 2336 */ 2337 if (status == 1 && (scp->cmnd[0] == RESERVE || 2338 scp->cmnd[0] == RELEASE)) { 2339 2340 scp->result = DID_ERROR << 16 | 2341 SAM_STAT_RESERVATION_CONFLICT; 2342 } 2343 else { 2344 scp->result = DID_BAD_TARGET << 16 | status; 2345 } 2346 } 2347 2348 // print a debug message for all failed commands 2349 if (status) { 2350 megaraid_mbox_display_scb(adapter, scb); 2351 } 2352 2353 scsi_dma_unmap(scp); 2354 2355 // remove from local clist 2356 list_del_init(&scb->list); 2357 2358 // put back in free list 2359 megaraid_dealloc_scb(adapter, scb); 2360 2361 // send the scsi packet back to kernel 2362 scp->scsi_done(scp); 2363 } 2364 2365 return; 2366 } 2367 2368 2369 /** 2370 * megaraid_abort_handler - abort the scsi command 2371 * @scp : command to be aborted 2372 * 2373 * Abort a previous SCSI request. Only commands on the pending list can be 2374 * aborted. All the commands issued to the F/W must complete. 2375 **/ 2376 static int 2377 megaraid_abort_handler(struct scsi_cmnd *scp) 2378 { 2379 adapter_t *adapter; 2380 mraid_device_t *raid_dev; 2381 scb_t *scb; 2382 scb_t *tmp; 2383 int found; 2384 unsigned long flags; 2385 int i; 2386 2387 2388 adapter = SCP2ADAPTER(scp); 2389 raid_dev = ADAP2RAIDDEV(adapter); 2390 2391 con_log(CL_ANN, (KERN_WARNING 2392 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n", 2393 scp->cmnd[0], SCP2CHANNEL(scp), 2394 SCP2TARGET(scp), SCP2LUN(scp))); 2395 2396 // If FW has stopped responding, simply return failure 2397 if (raid_dev->hw_error) { 2398 con_log(CL_ANN, (KERN_NOTICE 2399 "megaraid: hw error, not aborting\n")); 2400 return FAILED; 2401 } 2402 2403 // There might a race here, where the command was completed by the 2404 // firmware and now it is on the completed list. Before we could 2405 // complete the command to the kernel in dpc, the abort came. 2406 // Find out if this is the case to avoid the race. 2407 scb = NULL; 2408 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags); 2409 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) { 2410 2411 if (scb->scp == scp) { // Found command 2412 2413 list_del_init(&scb->list); // from completed list 2414 2415 con_log(CL_ANN, (KERN_WARNING 2416 "megaraid: %d[%d:%d], abort from completed list\n", 2417 scb->sno, scb->dev_channel, scb->dev_target)); 2418 2419 scp->result = (DID_ABORT << 16); 2420 scp->scsi_done(scp); 2421 2422 megaraid_dealloc_scb(adapter, scb); 2423 2424 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), 2425 flags); 2426 2427 return SUCCESS; 2428 } 2429 } 2430 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags); 2431 2432 2433 // Find out if this command is still on the pending list. If it is and 2434 // was never issued, abort and return success. If the command is owned 2435 // by the firmware, we must wait for it to complete by the FW. 2436 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags); 2437 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) { 2438 2439 if (scb->scp == scp) { // Found command 2440 2441 list_del_init(&scb->list); // from pending list 2442 2443 ASSERT(!(scb->state & SCB_ISSUED)); 2444 2445 con_log(CL_ANN, (KERN_WARNING 2446 "megaraid abort: [%d:%d], driver owner\n", 2447 scb->dev_channel, scb->dev_target)); 2448 2449 scp->result = (DID_ABORT << 16); 2450 scp->scsi_done(scp); 2451 2452 megaraid_dealloc_scb(adapter, scb); 2453 2454 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), 2455 flags); 2456 2457 return SUCCESS; 2458 } 2459 } 2460 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags); 2461 2462 2463 // Check do we even own this command, in which case this would be 2464 // owned by the firmware. The only way to locate the FW scb is to 2465 // traverse through the list of all SCB, since driver does not 2466 // maintain these SCBs on any list 2467 found = 0; 2468 spin_lock_irq(&adapter->lock); 2469 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) { 2470 scb = adapter->kscb_list + i; 2471 2472 if (scb->scp == scp) { 2473 2474 found = 1; 2475 2476 if (!(scb->state & SCB_ISSUED)) { 2477 con_log(CL_ANN, (KERN_WARNING 2478 "megaraid abort: %d[%d:%d], invalid state\n", 2479 scb->sno, scb->dev_channel, scb->dev_target)); 2480 BUG(); 2481 } 2482 else { 2483 con_log(CL_ANN, (KERN_WARNING 2484 "megaraid abort: %d[%d:%d], fw owner\n", 2485 scb->sno, scb->dev_channel, scb->dev_target)); 2486 } 2487 } 2488 } 2489 spin_unlock_irq(&adapter->lock); 2490 2491 if (!found) { 2492 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n")); 2493 2494 // FIXME: Should there be a callback for this command? 2495 return SUCCESS; 2496 } 2497 2498 // We cannot actually abort a command owned by firmware, return 2499 // failure and wait for reset. In host reset handler, we will find out 2500 // if the HBA is still live 2501 return FAILED; 2502 } 2503 2504 /** 2505 * megaraid_reset_handler - device reset handler for mailbox based driver 2506 * @scp : reference command 2507 * 2508 * Reset handler for the mailbox based controller. First try to find out if 2509 * the FW is still live, in which case the outstanding commands counter mut go 2510 * down to 0. If that happens, also issue the reservation reset command to 2511 * relinquish (possible) reservations on the logical drives connected to this 2512 * host. 2513 **/ 2514 static int 2515 megaraid_reset_handler(struct scsi_cmnd *scp) 2516 { 2517 adapter_t *adapter; 2518 scb_t *scb; 2519 scb_t *tmp; 2520 mraid_device_t *raid_dev; 2521 unsigned long flags; 2522 uint8_t raw_mbox[sizeof(mbox_t)]; 2523 int rval; 2524 int recovery_window; 2525 int i; 2526 uioc_t *kioc; 2527 2528 adapter = SCP2ADAPTER(scp); 2529 raid_dev = ADAP2RAIDDEV(adapter); 2530 2531 // return failure if adapter is not responding 2532 if (raid_dev->hw_error) { 2533 con_log(CL_ANN, (KERN_NOTICE 2534 "megaraid: hw error, cannot reset\n")); 2535 return FAILED; 2536 } 2537 2538 // Under exceptional conditions, FW can take up to 3 minutes to 2539 // complete command processing. Wait for additional 2 minutes for the 2540 // pending commands counter to go down to 0. If it doesn't, let the 2541 // controller be marked offline 2542 // Also, reset all the commands currently owned by the driver 2543 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags); 2544 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) { 2545 list_del_init(&scb->list); // from pending list 2546 2547 if (scb->sno >= MBOX_MAX_SCSI_CMDS) { 2548 con_log(CL_ANN, (KERN_WARNING 2549 "megaraid: IOCTL packet with %d[%d:%d] being reset\n", 2550 scb->sno, scb->dev_channel, scb->dev_target)); 2551 2552 scb->status = -1; 2553 2554 kioc = (uioc_t *)scb->gp; 2555 kioc->status = -EFAULT; 2556 2557 megaraid_mbox_mm_done(adapter, scb); 2558 } else { 2559 if (scb->scp == scp) { // Found command 2560 con_log(CL_ANN, (KERN_WARNING 2561 "megaraid: %d[%d:%d], reset from pending list\n", 2562 scb->sno, scb->dev_channel, scb->dev_target)); 2563 } else { 2564 con_log(CL_ANN, (KERN_WARNING 2565 "megaraid: IO packet with %d[%d:%d] being reset\n", 2566 scb->sno, scb->dev_channel, scb->dev_target)); 2567 } 2568 2569 scb->scp->result = (DID_RESET << 16); 2570 scb->scp->scsi_done(scb->scp); 2571 2572 megaraid_dealloc_scb(adapter, scb); 2573 } 2574 } 2575 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags); 2576 2577 if (adapter->outstanding_cmds) { 2578 con_log(CL_ANN, (KERN_NOTICE 2579 "megaraid: %d outstanding commands. Max wait %d sec\n", 2580 adapter->outstanding_cmds, 2581 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT))); 2582 } 2583 2584 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT; 2585 2586 for (i = 0; i < recovery_window; i++) { 2587 2588 megaraid_ack_sequence(adapter); 2589 2590 // print a message once every 5 seconds only 2591 if (!(i % 5)) { 2592 con_log(CL_ANN, ( 2593 "megaraid mbox: Wait for %d commands to complete:%d\n", 2594 adapter->outstanding_cmds, 2595 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i)); 2596 } 2597 2598 // bailout if no recovery happened in reset time 2599 if (adapter->outstanding_cmds == 0) { 2600 break; 2601 } 2602 2603 msleep(1000); 2604 } 2605 2606 spin_lock(&adapter->lock); 2607 2608 // If still outstanding commands, bail out 2609 if (adapter->outstanding_cmds) { 2610 con_log(CL_ANN, (KERN_WARNING 2611 "megaraid mbox: critical hardware error!\n")); 2612 2613 raid_dev->hw_error = 1; 2614 2615 rval = FAILED; 2616 goto out; 2617 } 2618 else { 2619 con_log(CL_ANN, (KERN_NOTICE 2620 "megaraid mbox: reset sequence completed successfully\n")); 2621 } 2622 2623 2624 // If the controller supports clustering, reset reservations 2625 if (!adapter->ha) { 2626 rval = SUCCESS; 2627 goto out; 2628 } 2629 2630 // clear reservations if any 2631 raw_mbox[0] = CLUSTER_CMD; 2632 raw_mbox[2] = RESET_RESERVATIONS; 2633 2634 rval = SUCCESS; 2635 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) { 2636 con_log(CL_ANN, 2637 (KERN_INFO "megaraid: reservation reset\n")); 2638 } 2639 else { 2640 rval = FAILED; 2641 con_log(CL_ANN, (KERN_WARNING 2642 "megaraid: reservation reset failed\n")); 2643 } 2644 2645 out: 2646 spin_unlock(&adapter->lock); 2647 return rval; 2648 } 2649 2650 /* 2651 * START: internal commands library 2652 * 2653 * This section of the driver has the common routine used by the driver and 2654 * also has all the FW routines 2655 */ 2656 2657 /** 2658 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers 2659 * @adapter : controller's soft state 2660 * @raw_mbox : the mailbox 2661 * 2662 * Issue a scb in synchronous and non-interrupt mode for mailbox based 2663 * controllers. 2664 */ 2665 static int 2666 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[]) 2667 { 2668 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 2669 mbox_t *mbox; 2670 uint8_t status; 2671 int i; 2672 2673 mbox = raid_dev->mbox; 2674 2675 /* 2676 * Wait until mailbox is free 2677 */ 2678 if (megaraid_busywait_mbox(raid_dev) != 0) 2679 goto blocked_mailbox; 2680 2681 /* 2682 * Copy mailbox data into host structure 2683 */ 2684 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16); 2685 mbox->cmdid = 0xFE; 2686 mbox->busy = 1; 2687 mbox->poll = 0; 2688 mbox->ack = 0; 2689 mbox->numstatus = 0xFF; 2690 mbox->status = 0xFF; 2691 2692 wmb(); 2693 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1); 2694 2695 // wait for maximum 1 second for status to post. If the status is not 2696 // available within 1 second, assume FW is initializing and wait 2697 // for an extended amount of time 2698 if (mbox->numstatus == 0xFF) { // status not yet available 2699 udelay(25); 2700 2701 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) { 2702 rmb(); 2703 msleep(1); 2704 } 2705 2706 2707 if (i == 1000) { 2708 con_log(CL_ANN, (KERN_NOTICE 2709 "megaraid mailbox: wait for FW to boot ")); 2710 2711 for (i = 0; (mbox->numstatus == 0xFF) && 2712 (i < MBOX_RESET_WAIT); i++) { 2713 rmb(); 2714 con_log(CL_ANN, ("\b\b\b\b\b[%03d]", 2715 MBOX_RESET_WAIT - i)); 2716 msleep(1000); 2717 } 2718 2719 if (i == MBOX_RESET_WAIT) { 2720 2721 con_log(CL_ANN, ( 2722 "\nmegaraid mailbox: status not available\n")); 2723 2724 return -1; 2725 } 2726 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n")); 2727 } 2728 } 2729 2730 // wait for maximum 1 second for poll semaphore 2731 if (mbox->poll != 0x77) { 2732 udelay(25); 2733 2734 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) { 2735 rmb(); 2736 msleep(1); 2737 } 2738 2739 if (i == 1000) { 2740 con_log(CL_ANN, (KERN_WARNING 2741 "megaraid mailbox: could not get poll semaphore\n")); 2742 return -1; 2743 } 2744 } 2745 2746 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2); 2747 wmb(); 2748 2749 // wait for maximum 1 second for acknowledgement 2750 if (RDINDOOR(raid_dev) & 0x2) { 2751 udelay(25); 2752 2753 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) { 2754 rmb(); 2755 msleep(1); 2756 } 2757 2758 if (i == 1000) { 2759 con_log(CL_ANN, (KERN_WARNING 2760 "megaraid mailbox: could not acknowledge\n")); 2761 return -1; 2762 } 2763 } 2764 mbox->poll = 0; 2765 mbox->ack = 0x77; 2766 2767 status = mbox->status; 2768 2769 // invalidate the completed command id array. After command 2770 // completion, firmware would write the valid id. 2771 mbox->numstatus = 0xFF; 2772 mbox->status = 0xFF; 2773 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) { 2774 mbox->completed[i] = 0xFF; 2775 } 2776 2777 return status; 2778 2779 blocked_mailbox: 2780 2781 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") ); 2782 return -1; 2783 } 2784 2785 2786 /** 2787 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers 2788 * @adapter : controller's soft state 2789 * @raw_mbox : the mailbox 2790 * 2791 * Issue a scb in synchronous and non-interrupt mode for mailbox based 2792 * controllers. This is a faster version of the synchronous command and 2793 * therefore can be called in interrupt-context as well. 2794 */ 2795 static int 2796 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[]) 2797 { 2798 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 2799 mbox_t *mbox; 2800 long i; 2801 2802 2803 mbox = raid_dev->mbox; 2804 2805 // return immediately if the mailbox is busy 2806 if (mbox->busy) return -1; 2807 2808 // Copy mailbox data into host structure 2809 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14); 2810 mbox->cmdid = 0xFE; 2811 mbox->busy = 1; 2812 mbox->poll = 0; 2813 mbox->ack = 0; 2814 mbox->numstatus = 0xFF; 2815 mbox->status = 0xFF; 2816 2817 wmb(); 2818 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1); 2819 2820 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) { 2821 if (mbox->numstatus != 0xFF) break; 2822 rmb(); 2823 udelay(MBOX_SYNC_DELAY_200); 2824 } 2825 2826 if (i == MBOX_SYNC_WAIT_CNT) { 2827 // We may need to re-calibrate the counter 2828 con_log(CL_ANN, (KERN_CRIT 2829 "megaraid: fast sync command timed out\n")); 2830 } 2831 2832 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2); 2833 wmb(); 2834 2835 return mbox->status; 2836 } 2837 2838 2839 /** 2840 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available 2841 * @raid_dev : RAID device (HBA) soft state 2842 * 2843 * Wait until the controller's mailbox is available to accept more commands. 2844 * Wait for at most 1 second. 2845 */ 2846 static int 2847 megaraid_busywait_mbox(mraid_device_t *raid_dev) 2848 { 2849 mbox_t *mbox = raid_dev->mbox; 2850 int i = 0; 2851 2852 if (mbox->busy) { 2853 udelay(25); 2854 for (i = 0; mbox->busy && i < 1000; i++) 2855 msleep(1); 2856 } 2857 2858 if (i < 1000) return 0; 2859 else return -1; 2860 } 2861 2862 2863 /** 2864 * megaraid_mbox_product_info - some static information about the controller 2865 * @adapter : our soft state 2866 * 2867 * Issue commands to the controller to grab some parameters required by our 2868 * caller. 2869 */ 2870 static int 2871 megaraid_mbox_product_info(adapter_t *adapter) 2872 { 2873 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 2874 mbox_t *mbox; 2875 uint8_t raw_mbox[sizeof(mbox_t)]; 2876 mraid_pinfo_t *pinfo; 2877 dma_addr_t pinfo_dma_h; 2878 mraid_inquiry3_t *mraid_inq3; 2879 int i; 2880 2881 2882 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox)); 2883 mbox = (mbox_t *)raw_mbox; 2884 2885 /* 2886 * Issue an ENQUIRY3 command to find out certain adapter parameters, 2887 * e.g., max channels, max commands etc. 2888 */ 2889 pinfo = dma_alloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), 2890 &pinfo_dma_h, GFP_KERNEL); 2891 if (pinfo == NULL) { 2892 con_log(CL_ANN, (KERN_WARNING 2893 "megaraid: out of memory, %s %d\n", __func__, 2894 __LINE__)); 2895 2896 return -1; 2897 } 2898 2899 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h; 2900 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE); 2901 2902 raw_mbox[0] = FC_NEW_CONFIG; 2903 raw_mbox[2] = NC_SUBOP_ENQUIRY3; 2904 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; 2905 2906 // Issue the command 2907 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) { 2908 2909 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n")); 2910 2911 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), 2912 pinfo, pinfo_dma_h); 2913 2914 return -1; 2915 } 2916 2917 /* 2918 * Collect information about state of each physical drive 2919 * attached to the controller. We will expose all the disks 2920 * which are not part of RAID 2921 */ 2922 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf; 2923 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) { 2924 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i]; 2925 } 2926 2927 /* 2928 * Get product info for information like number of channels, 2929 * maximum commands supported. 2930 */ 2931 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox)); 2932 mbox->xferaddr = (uint32_t)pinfo_dma_h; 2933 2934 raw_mbox[0] = FC_NEW_CONFIG; 2935 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; 2936 2937 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) { 2938 2939 con_log(CL_ANN, (KERN_WARNING 2940 "megaraid: product info failed\n")); 2941 2942 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), 2943 pinfo, pinfo_dma_h); 2944 2945 return -1; 2946 } 2947 2948 /* 2949 * Setup some parameters for host, as required by our caller 2950 */ 2951 adapter->max_channel = pinfo->nchannels; 2952 2953 /* 2954 * we will export all the logical drives on a single channel. 2955 * Add 1 since inquires do not come for inititor ID 2956 */ 2957 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1; 2958 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices 2959 2960 /* 2961 * These are the maximum outstanding commands for the scsi-layer 2962 */ 2963 adapter->max_cmds = MBOX_MAX_SCSI_CMDS; 2964 2965 memset(adapter->fw_version, 0, VERSION_SIZE); 2966 memset(adapter->bios_version, 0, VERSION_SIZE); 2967 2968 memcpy(adapter->fw_version, pinfo->fw_version, 4); 2969 adapter->fw_version[4] = 0; 2970 2971 memcpy(adapter->bios_version, pinfo->bios_version, 4); 2972 adapter->bios_version[4] = 0; 2973 2974 con_log(CL_ANN, (KERN_NOTICE 2975 "megaraid: fw version:[%s] bios version:[%s]\n", 2976 adapter->fw_version, adapter->bios_version)); 2977 2978 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo, 2979 pinfo_dma_h); 2980 2981 return 0; 2982 } 2983 2984 2985 2986 /** 2987 * megaraid_mbox_extended_cdb - check for support for extended CDBs 2988 * @adapter : soft state for the controller 2989 * 2990 * This routine check whether the controller in question supports extended 2991 * ( > 10 bytes ) CDBs. 2992 */ 2993 static int 2994 megaraid_mbox_extended_cdb(adapter_t *adapter) 2995 { 2996 mbox_t *mbox; 2997 uint8_t raw_mbox[sizeof(mbox_t)]; 2998 int rval; 2999 3000 mbox = (mbox_t *)raw_mbox; 3001 3002 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox)); 3003 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h; 3004 3005 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE); 3006 3007 raw_mbox[0] = MAIN_MISC_OPCODE; 3008 raw_mbox[2] = SUPPORT_EXT_CDB; 3009 3010 /* 3011 * Issue the command 3012 */ 3013 rval = 0; 3014 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) { 3015 rval = -1; 3016 } 3017 3018 return rval; 3019 } 3020 3021 3022 /** 3023 * megaraid_mbox_support_ha - Do we support clustering 3024 * @adapter : soft state for the controller 3025 * @init_id : ID of the initiator 3026 * 3027 * Determine if the firmware supports clustering and the ID of the initiator. 3028 */ 3029 static int 3030 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id) 3031 { 3032 mbox_t *mbox; 3033 uint8_t raw_mbox[sizeof(mbox_t)]; 3034 int rval; 3035 3036 3037 mbox = (mbox_t *)raw_mbox; 3038 3039 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox)); 3040 3041 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h; 3042 3043 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE); 3044 3045 raw_mbox[0] = GET_TARGET_ID; 3046 3047 // Issue the command 3048 *init_id = 7; 3049 rval = -1; 3050 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) { 3051 3052 *init_id = *(uint8_t *)adapter->ibuf; 3053 3054 con_log(CL_ANN, (KERN_INFO 3055 "megaraid: cluster firmware, initiator ID: %d\n", 3056 *init_id)); 3057 3058 rval = 0; 3059 } 3060 3061 return rval; 3062 } 3063 3064 3065 /** 3066 * megaraid_mbox_support_random_del - Do we support random deletion 3067 * @adapter : soft state for the controller 3068 * 3069 * Determine if the firmware supports random deletion. 3070 * Return: 1 is operation supported, 0 otherwise 3071 */ 3072 static int 3073 megaraid_mbox_support_random_del(adapter_t *adapter) 3074 { 3075 uint8_t raw_mbox[sizeof(mbox_t)]; 3076 int rval; 3077 3078 /* 3079 * Newer firmware on Dell CERC expect a different 3080 * random deletion handling, so disable it. 3081 */ 3082 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI && 3083 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 && 3084 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL && 3085 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH && 3086 (adapter->fw_version[0] > '6' || 3087 (adapter->fw_version[0] == '6' && 3088 adapter->fw_version[2] > '6') || 3089 (adapter->fw_version[0] == '6' 3090 && adapter->fw_version[2] == '6' 3091 && adapter->fw_version[3] > '1'))) { 3092 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n")); 3093 return 0; 3094 } 3095 3096 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t)); 3097 3098 raw_mbox[0] = FC_DEL_LOGDRV; 3099 raw_mbox[2] = OP_SUP_DEL_LOGDRV; 3100 3101 // Issue the command 3102 rval = 0; 3103 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) { 3104 3105 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n")); 3106 3107 rval = 1; 3108 } 3109 3110 return rval; 3111 } 3112 3113 3114 /** 3115 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware 3116 * @adapter : soft state for the controller 3117 * 3118 * Find out the maximum number of scatter-gather elements supported by the 3119 * firmware. 3120 */ 3121 static int 3122 megaraid_mbox_get_max_sg(adapter_t *adapter) 3123 { 3124 mbox_t *mbox; 3125 uint8_t raw_mbox[sizeof(mbox_t)]; 3126 int nsg; 3127 3128 3129 mbox = (mbox_t *)raw_mbox; 3130 3131 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t)); 3132 3133 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h; 3134 3135 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE); 3136 3137 raw_mbox[0] = MAIN_MISC_OPCODE; 3138 raw_mbox[2] = GET_MAX_SG_SUPPORT; 3139 3140 // Issue the command 3141 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) { 3142 nsg = *(uint8_t *)adapter->ibuf; 3143 } 3144 else { 3145 nsg = MBOX_DEFAULT_SG_SIZE; 3146 } 3147 3148 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE; 3149 3150 return nsg; 3151 } 3152 3153 3154 /** 3155 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels 3156 * @adapter : soft state for the controller 3157 * 3158 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels 3159 * can be exported as regular SCSI channels. 3160 */ 3161 static void 3162 megaraid_mbox_enum_raid_scsi(adapter_t *adapter) 3163 { 3164 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3165 mbox_t *mbox; 3166 uint8_t raw_mbox[sizeof(mbox_t)]; 3167 3168 3169 mbox = (mbox_t *)raw_mbox; 3170 3171 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t)); 3172 3173 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h; 3174 3175 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE); 3176 3177 raw_mbox[0] = CHNL_CLASS; 3178 raw_mbox[2] = GET_CHNL_CLASS; 3179 3180 // Issue the command. If the command fails, all channels are RAID 3181 // channels 3182 raid_dev->channel_class = 0xFF; 3183 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) { 3184 raid_dev->channel_class = *(uint8_t *)adapter->ibuf; 3185 } 3186 3187 return; 3188 } 3189 3190 3191 /** 3192 * megaraid_mbox_flush_cache - flush adapter and disks cache 3193 * @adapter : soft state for the controller 3194 * 3195 * Flush adapter cache followed by disks cache. 3196 */ 3197 static void 3198 megaraid_mbox_flush_cache(adapter_t *adapter) 3199 { 3200 uint8_t raw_mbox[sizeof(mbox_t)]; 3201 3202 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t)); 3203 3204 raw_mbox[0] = FLUSH_ADAPTER; 3205 3206 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) { 3207 con_log(CL_ANN, ("megaraid: flush adapter failed\n")); 3208 } 3209 3210 raw_mbox[0] = FLUSH_SYSTEM; 3211 3212 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) { 3213 con_log(CL_ANN, ("megaraid: flush disks cache failed\n")); 3214 } 3215 3216 return; 3217 } 3218 3219 3220 /** 3221 * megaraid_mbox_fire_sync_cmd - fire the sync cmd 3222 * @adapter : soft state for the controller 3223 * 3224 * Clears the pending cmds in FW and reinits its RAID structs. 3225 */ 3226 static int 3227 megaraid_mbox_fire_sync_cmd(adapter_t *adapter) 3228 { 3229 mbox_t *mbox; 3230 uint8_t raw_mbox[sizeof(mbox_t)]; 3231 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3232 int status = 0; 3233 int i; 3234 uint32_t dword; 3235 3236 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t)); 3237 3238 raw_mbox[0] = 0xFF; 3239 3240 mbox = raid_dev->mbox; 3241 3242 /* Wait until mailbox is free */ 3243 if (megaraid_busywait_mbox(raid_dev) != 0) { 3244 status = 1; 3245 goto blocked_mailbox; 3246 } 3247 3248 /* Copy mailbox data into host structure */ 3249 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16); 3250 mbox->cmdid = 0xFE; 3251 mbox->busy = 1; 3252 mbox->poll = 0; 3253 mbox->ack = 0; 3254 mbox->numstatus = 0; 3255 mbox->status = 0; 3256 3257 wmb(); 3258 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1); 3259 3260 /* Wait for maximum 1 min for status to post. 3261 * If the Firmware SUPPORTS the ABOVE COMMAND, 3262 * mbox->cmd will be set to 0 3263 * else 3264 * the firmware will reject the command with 3265 * mbox->numstatus set to 1 3266 */ 3267 3268 i = 0; 3269 status = 0; 3270 while (!mbox->numstatus && mbox->cmd == 0xFF) { 3271 rmb(); 3272 msleep(1); 3273 i++; 3274 if (i > 1000 * 60) { 3275 status = 1; 3276 break; 3277 } 3278 } 3279 if (mbox->numstatus == 1) 3280 status = 1; /*cmd not supported*/ 3281 3282 /* Check for interrupt line */ 3283 dword = RDOUTDOOR(raid_dev); 3284 WROUTDOOR(raid_dev, dword); 3285 WRINDOOR(raid_dev,2); 3286 3287 return status; 3288 3289 blocked_mailbox: 3290 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n")); 3291 return status; 3292 } 3293 3294 /** 3295 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes 3296 * @adapter : controller's soft state 3297 * @scb : SCB to be displayed 3298 * 3299 * Diplay information about the given SCB iff the current debug level is 3300 * verbose. 3301 */ 3302 static void 3303 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb) 3304 { 3305 mbox_ccb_t *ccb; 3306 struct scsi_cmnd *scp; 3307 mbox_t *mbox; 3308 int level; 3309 int i; 3310 3311 3312 ccb = (mbox_ccb_t *)scb->ccb; 3313 scp = scb->scp; 3314 mbox = ccb->mbox; 3315 3316 level = CL_DLEVEL3; 3317 3318 con_log(level, (KERN_NOTICE 3319 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status, 3320 mbox->cmd, scb->sno)); 3321 3322 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n", 3323 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv, 3324 mbox->numsge)); 3325 3326 if (!scp) return; 3327 3328 con_log(level, (KERN_NOTICE "scsi cmnd: ")); 3329 3330 for (i = 0; i < scp->cmd_len; i++) { 3331 con_log(level, ("%#2.02x ", scp->cmnd[i])); 3332 } 3333 3334 con_log(level, ("\n")); 3335 3336 return; 3337 } 3338 3339 3340 /** 3341 * megaraid_mbox_setup_device_map - manage device ids 3342 * @adapter : Driver's soft state 3343 * 3344 * Manage the device ids to have an appropriate mapping between the kernel 3345 * scsi addresses and megaraid scsi and logical drive addresses. We export 3346 * scsi devices on their actual addresses, whereas the logical drives are 3347 * exported on a virtual scsi channel. 3348 */ 3349 static void 3350 megaraid_mbox_setup_device_map(adapter_t *adapter) 3351 { 3352 uint8_t c; 3353 uint8_t t; 3354 3355 /* 3356 * First fill the values on the logical drive channel 3357 */ 3358 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++) 3359 adapter->device_ids[adapter->max_channel][t] = 3360 (t < adapter->init_id) ? t : t - 1; 3361 3362 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF; 3363 3364 /* 3365 * Fill the values on the physical devices channels 3366 */ 3367 for (c = 0; c < adapter->max_channel; c++) 3368 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++) 3369 adapter->device_ids[c][t] = (c << 8) | t; 3370 } 3371 3372 3373 /* 3374 * END: internal commands library 3375 */ 3376 3377 /* 3378 * START: Interface for the common management module 3379 * 3380 * This is the module, which interfaces with the common management module to 3381 * provide support for ioctl and sysfs 3382 */ 3383 3384 /** 3385 * megaraid_cmm_register - register with the management module 3386 * @adapter : HBA soft state 3387 * 3388 * Register with the management module, which allows applications to issue 3389 * ioctl calls to the drivers. This interface is used by the management module 3390 * to setup sysfs support as well. 3391 */ 3392 static int 3393 megaraid_cmm_register(adapter_t *adapter) 3394 { 3395 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3396 mraid_mmadp_t adp; 3397 scb_t *scb; 3398 mbox_ccb_t *ccb; 3399 int rval; 3400 int i; 3401 3402 // Allocate memory for the base list of scb for management module. 3403 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL); 3404 3405 if (adapter->uscb_list == NULL) { 3406 con_log(CL_ANN, (KERN_WARNING 3407 "megaraid: out of memory, %s %d\n", __func__, 3408 __LINE__)); 3409 return -1; 3410 } 3411 3412 3413 // Initialize the synchronization parameters for resources for 3414 // commands for management module 3415 INIT_LIST_HEAD(&adapter->uscb_pool); 3416 3417 spin_lock_init(USER_FREE_LIST_LOCK(adapter)); 3418 3419 3420 3421 // link all the packets. Note, CCB for commands, coming from the 3422 // commom management module, mailbox physical address are already 3423 // setup by it. We just need placeholder for that in our local command 3424 // control blocks 3425 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) { 3426 3427 scb = adapter->uscb_list + i; 3428 ccb = raid_dev->uccb_list + i; 3429 3430 scb->ccb = (caddr_t)ccb; 3431 ccb->mbox64 = raid_dev->umbox64 + i; 3432 ccb->mbox = &ccb->mbox64->mbox32; 3433 ccb->raw_mbox = (uint8_t *)ccb->mbox; 3434 3435 scb->gp = 0; 3436 3437 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR 3438 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER) 3439 scb->sno = i + MBOX_MAX_SCSI_CMDS; 3440 3441 scb->scp = NULL; 3442 scb->state = SCB_FREE; 3443 scb->dma_direction = DMA_NONE; 3444 scb->dma_type = MRAID_DMA_NONE; 3445 scb->dev_channel = -1; 3446 scb->dev_target = -1; 3447 3448 // put scb in the free pool 3449 list_add_tail(&scb->list, &adapter->uscb_pool); 3450 } 3451 3452 adp.unique_id = adapter->unique_id; 3453 adp.drvr_type = DRVRTYPE_MBOX; 3454 adp.drvr_data = (unsigned long)adapter; 3455 adp.pdev = adapter->pdev; 3456 adp.issue_uioc = megaraid_mbox_mm_handler; 3457 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT; 3458 adp.max_kioc = MBOX_MAX_USER_CMDS; 3459 3460 if ((rval = mraid_mm_register_adp(&adp)) != 0) { 3461 3462 con_log(CL_ANN, (KERN_WARNING 3463 "megaraid mbox: did not register with CMM\n")); 3464 3465 kfree(adapter->uscb_list); 3466 } 3467 3468 return rval; 3469 } 3470 3471 3472 /** 3473 * megaraid_cmm_unregister - un-register with the management module 3474 * @adapter : HBA soft state 3475 * 3476 * Un-register with the management module. 3477 * FIXME: mgmt module must return failure for unregister if it has pending 3478 * commands in LLD. 3479 */ 3480 static int 3481 megaraid_cmm_unregister(adapter_t *adapter) 3482 { 3483 kfree(adapter->uscb_list); 3484 mraid_mm_unregister_adp(adapter->unique_id); 3485 return 0; 3486 } 3487 3488 3489 /** 3490 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD 3491 * @drvr_data : LLD specific data 3492 * @kioc : CMM interface packet 3493 * @action : command action 3494 * 3495 * This routine is invoked whenever the Common Management Module (CMM) has a 3496 * command for us. The 'action' parameter specifies if this is a new command 3497 * or otherwise. 3498 */ 3499 static int 3500 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action) 3501 { 3502 adapter_t *adapter; 3503 3504 if (action != IOCTL_ISSUE) { 3505 con_log(CL_ANN, (KERN_WARNING 3506 "megaraid: unsupported management action:%#2x\n", 3507 action)); 3508 return (-ENOTSUPP); 3509 } 3510 3511 adapter = (adapter_t *)drvr_data; 3512 3513 // make sure this adapter is not being detached right now. 3514 if (atomic_read(&adapter->being_detached)) { 3515 con_log(CL_ANN, (KERN_WARNING 3516 "megaraid: reject management request, detaching\n")); 3517 return (-ENODEV); 3518 } 3519 3520 switch (kioc->opcode) { 3521 3522 case GET_ADAP_INFO: 3523 3524 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *) 3525 (unsigned long)kioc->buf_vaddr); 3526 3527 kioc->done(kioc); 3528 3529 return kioc->status; 3530 3531 case MBOX_CMD: 3532 3533 return megaraid_mbox_mm_command(adapter, kioc); 3534 3535 default: 3536 kioc->status = (-EINVAL); 3537 kioc->done(kioc); 3538 return (-EINVAL); 3539 } 3540 3541 return 0; // not reached 3542 } 3543 3544 /** 3545 * megaraid_mbox_mm_command - issues commands routed through CMM 3546 * @adapter : HBA soft state 3547 * @kioc : management command packet 3548 * 3549 * Issues commands, which are routed through the management module. 3550 */ 3551 static int 3552 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc) 3553 { 3554 struct list_head *head = &adapter->uscb_pool; 3555 mbox64_t *mbox64; 3556 uint8_t *raw_mbox; 3557 scb_t *scb; 3558 mbox_ccb_t *ccb; 3559 unsigned long flags; 3560 3561 // detach one scb from free pool 3562 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags); 3563 3564 if (list_empty(head)) { // should never happen because of CMM 3565 3566 con_log(CL_ANN, (KERN_WARNING 3567 "megaraid mbox: bug in cmm handler, lost resources\n")); 3568 3569 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags); 3570 3571 return (-EINVAL); 3572 } 3573 3574 scb = list_entry(head->next, scb_t, list); 3575 list_del_init(&scb->list); 3576 3577 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags); 3578 3579 scb->state = SCB_ACTIVE; 3580 scb->dma_type = MRAID_DMA_NONE; 3581 scb->dma_direction = DMA_NONE; 3582 3583 ccb = (mbox_ccb_t *)scb->ccb; 3584 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf; 3585 raw_mbox = (uint8_t *)&mbox64->mbox32; 3586 3587 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t)); 3588 3589 scb->gp = (unsigned long)kioc; 3590 3591 /* 3592 * If it is a logdrv random delete operation, we have to wait till 3593 * there are no outstanding cmds at the fw and then issue it directly 3594 */ 3595 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) { 3596 3597 if (wait_till_fw_empty(adapter)) { 3598 con_log(CL_ANN, (KERN_NOTICE 3599 "megaraid mbox: LD delete, timed out\n")); 3600 3601 kioc->status = -ETIME; 3602 3603 scb->status = -1; 3604 3605 megaraid_mbox_mm_done(adapter, scb); 3606 3607 return (-ETIME); 3608 } 3609 3610 INIT_LIST_HEAD(&scb->list); 3611 3612 scb->state = SCB_ISSUED; 3613 if (mbox_post_cmd(adapter, scb) != 0) { 3614 3615 con_log(CL_ANN, (KERN_NOTICE 3616 "megaraid mbox: LD delete, mailbox busy\n")); 3617 3618 kioc->status = -EBUSY; 3619 3620 scb->status = -1; 3621 3622 megaraid_mbox_mm_done(adapter, scb); 3623 3624 return (-EBUSY); 3625 } 3626 3627 return 0; 3628 } 3629 3630 // put the command on the pending list and execute 3631 megaraid_mbox_runpendq(adapter, scb); 3632 3633 return 0; 3634 } 3635 3636 3637 static int 3638 wait_till_fw_empty(adapter_t *adapter) 3639 { 3640 unsigned long flags = 0; 3641 int i; 3642 3643 3644 /* 3645 * Set the quiescent flag to stop issuing cmds to FW. 3646 */ 3647 spin_lock_irqsave(&adapter->lock, flags); 3648 adapter->quiescent++; 3649 spin_unlock_irqrestore(&adapter->lock, flags); 3650 3651 /* 3652 * Wait till there are no more cmds outstanding at FW. Try for at most 3653 * 60 seconds 3654 */ 3655 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) { 3656 con_log(CL_DLEVEL1, (KERN_INFO 3657 "megaraid: FW has %d pending commands\n", 3658 adapter->outstanding_cmds)); 3659 3660 msleep(1000); 3661 } 3662 3663 return adapter->outstanding_cmds; 3664 } 3665 3666 3667 /** 3668 * megaraid_mbox_mm_done - callback for CMM commands 3669 * @adapter : HBA soft state 3670 * @scb : completed command 3671 * 3672 * Callback routine for internal commands originated from the management 3673 * module. 3674 */ 3675 static void 3676 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb) 3677 { 3678 uioc_t *kioc; 3679 mbox64_t *mbox64; 3680 uint8_t *raw_mbox; 3681 unsigned long flags; 3682 3683 kioc = (uioc_t *)scb->gp; 3684 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf; 3685 mbox64->mbox32.status = scb->status; 3686 raw_mbox = (uint8_t *)&mbox64->mbox32; 3687 3688 3689 // put scb in the free pool 3690 scb->state = SCB_FREE; 3691 scb->scp = NULL; 3692 3693 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags); 3694 3695 list_add(&scb->list, &adapter->uscb_pool); 3696 3697 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags); 3698 3699 // if a delete logical drive operation succeeded, restart the 3700 // controller 3701 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) { 3702 3703 adapter->quiescent--; 3704 3705 megaraid_mbox_runpendq(adapter, NULL); 3706 } 3707 3708 kioc->done(kioc); 3709 3710 return; 3711 } 3712 3713 3714 /** 3715 * gather_hbainfo - HBA characteristics for the applications 3716 * @adapter : HBA soft state 3717 * @hinfo : pointer to the caller's host info strucuture 3718 */ 3719 static int 3720 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo) 3721 { 3722 hinfo->pci_vendor_id = adapter->pdev->vendor; 3723 hinfo->pci_device_id = adapter->pdev->device; 3724 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor; 3725 hinfo->subsys_device_id = adapter->pdev->subsystem_device; 3726 3727 hinfo->pci_bus = adapter->pdev->bus->number; 3728 hinfo->pci_dev_fn = adapter->pdev->devfn; 3729 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn); 3730 hinfo->irq = adapter->host->irq; 3731 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport; 3732 3733 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn; 3734 hinfo->host_no = adapter->host->host_no; 3735 3736 return 0; 3737 } 3738 3739 /* 3740 * END: Interface for the common management module 3741 */ 3742 3743 3744 3745 /** 3746 * megaraid_sysfs_alloc_resources - allocate sysfs related resources 3747 * @adapter : controller's soft state 3748 * 3749 * Allocate packets required to issue FW calls whenever the sysfs attributes 3750 * are read. These attributes would require up-to-date information from the 3751 * FW. Also set up resources for mutual exclusion to share these resources and 3752 * the wait queue. 3753 * 3754 * Return 0 on success. 3755 * Return -ERROR_CODE on failure. 3756 */ 3757 static int 3758 megaraid_sysfs_alloc_resources(adapter_t *adapter) 3759 { 3760 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3761 int rval = 0; 3762 3763 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL); 3764 3765 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL); 3766 3767 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev, 3768 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL); 3769 3770 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 || 3771 !raid_dev->sysfs_buffer) { 3772 3773 con_log(CL_ANN, (KERN_WARNING 3774 "megaraid: out of memory, %s %d\n", __func__, 3775 __LINE__)); 3776 3777 rval = -ENOMEM; 3778 3779 megaraid_sysfs_free_resources(adapter); 3780 } 3781 3782 mutex_init(&raid_dev->sysfs_mtx); 3783 3784 init_waitqueue_head(&raid_dev->sysfs_wait_q); 3785 3786 return rval; 3787 } 3788 3789 3790 /** 3791 * megaraid_sysfs_free_resources - free sysfs related resources 3792 * @adapter : controller's soft state 3793 * 3794 * Free packets allocated for sysfs FW commands 3795 */ 3796 static void 3797 megaraid_sysfs_free_resources(adapter_t *adapter) 3798 { 3799 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3800 3801 kfree(raid_dev->sysfs_uioc); 3802 kfree(raid_dev->sysfs_mbox64); 3803 3804 if (raid_dev->sysfs_buffer) { 3805 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE, 3806 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma); 3807 } 3808 } 3809 3810 3811 /** 3812 * megaraid_sysfs_get_ldmap_done - callback for get ldmap 3813 * @uioc : completed packet 3814 * 3815 * Callback routine called in the ISR/tasklet context for get ldmap call 3816 */ 3817 static void 3818 megaraid_sysfs_get_ldmap_done(uioc_t *uioc) 3819 { 3820 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr; 3821 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3822 3823 uioc->status = 0; 3824 3825 wake_up(&raid_dev->sysfs_wait_q); 3826 } 3827 3828 /** 3829 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap 3830 * @t : timed out timer 3831 * 3832 * Timeout routine to recover and return to application, in case the adapter 3833 * has stopped responding. A timeout of 60 seconds for this command seems like 3834 * a good value. 3835 */ 3836 static void 3837 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t) 3838 { 3839 struct uioc_timeout *timeout = from_timer(timeout, t, timer); 3840 uioc_t *uioc = timeout->uioc; 3841 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr; 3842 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3843 3844 uioc->status = -ETIME; 3845 3846 wake_up(&raid_dev->sysfs_wait_q); 3847 } 3848 3849 3850 /** 3851 * megaraid_sysfs_get_ldmap - get update logical drive map 3852 * @adapter : controller's soft state 3853 * 3854 * This routine will be called whenever user reads the logical drive 3855 * attributes, go get the current logical drive mapping table from the 3856 * firmware. We use the management API's to issue commands to the controller. 3857 * 3858 * NOTE: The commands issuance functionality is not generalized and 3859 * implemented in context of "get ld map" command only. If required, the 3860 * command issuance logical can be trivially pulled out and implemented as a 3861 * standalone library. For now, this should suffice since there is no other 3862 * user of this interface. 3863 * 3864 * Return 0 on success. 3865 * Return -1 on failure. 3866 */ 3867 static int 3868 megaraid_sysfs_get_ldmap(adapter_t *adapter) 3869 { 3870 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 3871 uioc_t *uioc; 3872 mbox64_t *mbox64; 3873 mbox_t *mbox; 3874 char *raw_mbox; 3875 struct uioc_timeout timeout; 3876 caddr_t ldmap; 3877 int rval = 0; 3878 3879 /* 3880 * Allow only one read at a time to go through the sysfs attributes 3881 */ 3882 mutex_lock(&raid_dev->sysfs_mtx); 3883 3884 uioc = raid_dev->sysfs_uioc; 3885 mbox64 = raid_dev->sysfs_mbox64; 3886 ldmap = raid_dev->sysfs_buffer; 3887 3888 memset(uioc, 0, sizeof(uioc_t)); 3889 memset(mbox64, 0, sizeof(mbox64_t)); 3890 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap)); 3891 3892 mbox = &mbox64->mbox32; 3893 raw_mbox = (char *)mbox; 3894 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64; 3895 uioc->buf_vaddr = (caddr_t)adapter; 3896 uioc->status = -ENODATA; 3897 uioc->done = megaraid_sysfs_get_ldmap_done; 3898 3899 /* 3900 * Prepare the mailbox packet to get the current logical drive mapping 3901 * table 3902 */ 3903 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma; 3904 3905 raw_mbox[0] = FC_DEL_LOGDRV; 3906 raw_mbox[2] = OP_GET_LDID_MAP; 3907 3908 /* 3909 * Setup a timer to recover from a non-responding controller 3910 */ 3911 timeout.uioc = uioc; 3912 timer_setup_on_stack(&timeout.timer, 3913 megaraid_sysfs_get_ldmap_timeout, 0); 3914 3915 timeout.timer.expires = jiffies + 60 * HZ; 3916 add_timer(&timeout.timer); 3917 3918 /* 3919 * Send the command to the firmware 3920 */ 3921 rval = megaraid_mbox_mm_command(adapter, uioc); 3922 3923 if (rval == 0) { // command successfully issued 3924 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA)); 3925 3926 /* 3927 * Check if the command timed out 3928 */ 3929 if (uioc->status == -ETIME) { 3930 con_log(CL_ANN, (KERN_NOTICE 3931 "megaraid: sysfs get ld map timed out\n")); 3932 3933 rval = -ETIME; 3934 } 3935 else { 3936 rval = mbox->status; 3937 } 3938 3939 if (rval == 0) { 3940 memcpy(raid_dev->curr_ldmap, ldmap, 3941 sizeof(raid_dev->curr_ldmap)); 3942 } 3943 else { 3944 con_log(CL_ANN, (KERN_NOTICE 3945 "megaraid: get ld map failed with %x\n", rval)); 3946 } 3947 } 3948 else { 3949 con_log(CL_ANN, (KERN_NOTICE 3950 "megaraid: could not issue ldmap command:%x\n", rval)); 3951 } 3952 3953 3954 del_timer_sync(&timeout.timer); 3955 destroy_timer_on_stack(&timeout.timer); 3956 3957 mutex_unlock(&raid_dev->sysfs_mtx); 3958 3959 return rval; 3960 } 3961 3962 3963 /** 3964 * megaraid_sysfs_show_app_hndl - display application handle for this adapter 3965 * @dev : class device object representation for the host 3966 * @attr : device attribute (unused) 3967 * @buf : buffer to send data to 3968 * 3969 * Display the handle used by the applications while executing management 3970 * tasks on the adapter. We invoke a management module API to get the adapter 3971 * handle, since we do not interface with applications directly. 3972 */ 3973 static ssize_t 3974 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr, 3975 char *buf) 3976 { 3977 struct Scsi_Host *shost = class_to_shost(dev); 3978 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost); 3979 uint32_t app_hndl; 3980 3981 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id); 3982 3983 return snprintf(buf, 8, "%u\n", app_hndl); 3984 } 3985 3986 3987 /** 3988 * megaraid_sysfs_show_ldnum - display the logical drive number for this device 3989 * @dev : device object representation for the scsi device 3990 * @attr : device attribute to show 3991 * @buf : buffer to send data to 3992 * 3993 * Display the logical drive number for the device in question, if it a valid 3994 * logical drive. For physical devices, "-1" is returned. 3995 * 3996 * The logical drive number is displayed in following format: 3997 * 3998 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE> 3999 * 4000 * <int> <int> <int> <int> 4001 */ 4002 static ssize_t 4003 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf) 4004 { 4005 struct scsi_device *sdev = to_scsi_device(dev); 4006 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host); 4007 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter); 4008 int scsi_id = -1; 4009 int logical_drv = -1; 4010 int ldid_map = -1; 4011 uint32_t app_hndl = 0; 4012 int mapped_sdev_id; 4013 int rval; 4014 int i; 4015 4016 if (raid_dev->random_del_supported && 4017 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) { 4018 4019 rval = megaraid_sysfs_get_ldmap(adapter); 4020 if (rval == 0) { 4021 4022 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) { 4023 4024 mapped_sdev_id = sdev->id; 4025 4026 if (sdev->id > adapter->init_id) { 4027 mapped_sdev_id -= 1; 4028 } 4029 4030 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) { 4031 4032 scsi_id = sdev->id; 4033 4034 logical_drv = i; 4035 4036 ldid_map = raid_dev->curr_ldmap[i]; 4037 4038 app_hndl = mraid_mm_adapter_app_handle( 4039 adapter->unique_id); 4040 4041 break; 4042 } 4043 } 4044 } 4045 else { 4046 con_log(CL_ANN, (KERN_NOTICE 4047 "megaraid: sysfs get ld map failed: %x\n", 4048 rval)); 4049 } 4050 } 4051 4052 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv, 4053 ldid_map, app_hndl); 4054 } 4055 4056 4057 /* 4058 * END: Mailbox Low Level Driver 4059 */ 4060 module_init(megaraid_init); 4061 module_exit(megaraid_exit); 4062