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