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