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