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