1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux MegaRAID device driver
6 * Copyright (c) 2003-2004 LSI Logic Corporation.
8 * FILE : megaraid_mbox.c
9 * Version : v2.20.5.1 (Nov 16 2006)
12 * Atul Mukker <Atul.Mukker@lsi.com>
13 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
14 * Manoj Jose <Manoj.Jose@lsi.com>
17 * List of supported controllers
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
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
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
59 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
61 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
63 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
65 * For history of changes, see Documentation/scsi/ChangeLog.megaraid
68 #include <linux/slab.h>
69 #include <linux/module.h>
70 #include "megaraid_mbox.h"
72 static int megaraid_init(void);
73 static void megaraid_exit(void);
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 *);
79 static int megaraid_io_attach(adapter_t *);
80 static void megaraid_io_detach(adapter_t *);
82 static int megaraid_init_mbox(adapter_t *);
83 static void megaraid_fini_mbox(adapter_t *);
85 static int megaraid_alloc_cmd_packets(adapter_t *);
86 static void megaraid_free_cmd_packets(adapter_t *);
88 static int megaraid_mbox_setup_dma_pools(adapter_t *);
89 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
91 static int megaraid_sysfs_alloc_resources(adapter_t *);
92 static void megaraid_sysfs_free_resources(adapter_t *);
94 static int megaraid_abort_handler(struct scsi_cmnd *);
95 static int megaraid_reset_handler(struct scsi_cmnd *);
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 *);
109 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
110 static void megaraid_mbox_setup_device_map(adapter_t *);
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 *,
117 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
120 static irqreturn_t megaraid_isr(int, void *);
122 static void megaraid_mbox_dpc(unsigned long);
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 *);
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 *);
137 MODULE_AUTHOR("megaraidlinux@lsi.com");
138 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
139 MODULE_LICENSE("GPL");
140 MODULE_VERSION(MEGARAID_VERSION);
143 * ### modules parameters for driver ###
147 * Set to enable driver to expose unconfigured disk to kernel
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)");
155 * driver wait time if the adapter's mailbox is busy
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)");
163 * number of sectors per IO command
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)");
171 * number of commands per logical unit
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)");
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
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)");
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.
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)");
200 * PCI table for all supported controllers.
202 static struct pci_device_id pci_id_table_g[] = {
205 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
207 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
210 PCI_VENDOR_ID_LSI_LOGIC,
211 PCI_DEVICE_ID_PERC4_SC,
213 PCI_SUBSYS_ID_PERC4_SC,
216 PCI_VENDOR_ID_LSI_LOGIC,
217 PCI_DEVICE_ID_PERC4_DC,
219 PCI_SUBSYS_ID_PERC4_DC,
222 PCI_VENDOR_ID_LSI_LOGIC,
229 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
231 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
235 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
237 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
241 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
243 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
247 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
249 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
253 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
255 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
259 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
261 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
264 PCI_VENDOR_ID_LSI_LOGIC,
265 PCI_DEVICE_ID_DOBSON,
271 PCI_DEVICE_ID_AMI_MEGARAID3,
276 PCI_VENDOR_ID_LSI_LOGIC,
277 PCI_DEVICE_ID_AMI_MEGARAID3,
282 PCI_VENDOR_ID_LSI_LOGIC,
283 PCI_DEVICE_ID_LINDSAY,
287 {0} /* Terminating entry */
289 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
292 static struct pci_driver megaraid_pci_driver = {
294 .id_table = pci_id_table_g,
295 .probe = megaraid_probe_one,
296 .remove = megaraid_detach_one,
297 .shutdown = megaraid_mbox_shutdown,
302 // definitions for the device attributes for exporting logical drive number
303 // for a scsi address (Host, Channel, Id, Lun)
305 static DEVICE_ATTR_ADMIN_RO(megaraid_mbox_app_hndl);
307 // Host template initializer for megaraid mbox sysfs device attributes
308 static struct device_attribute *megaraid_shost_attrs[] = {
309 &dev_attr_megaraid_mbox_app_hndl,
314 static DEVICE_ATTR_ADMIN_RO(megaraid_mbox_ld);
316 // Host template initializer for megaraid mbox sysfs device attributes
317 static struct device_attribute *megaraid_sdev_attrs[] = {
318 &dev_attr_megaraid_mbox_ld,
323 * Scsi host template for megaraid unified driver
325 static struct scsi_host_template megaraid_template_g = {
326 .module = THIS_MODULE,
327 .name = "LSI Logic MegaRAID driver",
328 .proc_name = "megaraid",
329 .queuecommand = megaraid_queue_command,
330 .eh_abort_handler = megaraid_abort_handler,
331 .eh_host_reset_handler = megaraid_reset_handler,
332 .change_queue_depth = scsi_change_queue_depth,
334 .sdev_attrs = megaraid_sdev_attrs,
335 .shost_attrs = megaraid_shost_attrs,
340 * megaraid_init - module load hook
342 * We register ourselves as hotplug enabled module and let PCI subsystem
343 * discover our adapters.
350 // Announce the driver version
351 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
352 MEGARAID_EXT_VERSION));
354 // check validity of module parameters
355 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
357 con_log(CL_ANN, (KERN_WARNING
358 "megaraid mailbox: max commands per lun reset to %d\n",
359 MBOX_MAX_SCSI_CMDS));
361 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
365 // register as a PCI hot-plug driver module
366 rval = pci_register_driver(&megaraid_pci_driver);
368 con_log(CL_ANN, (KERN_WARNING
369 "megaraid: could not register hotplug support.\n"));
377 * megaraid_exit - driver unload entry point
379 * We simply unwrap the megaraid_init routine here.
384 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
386 // unregister as PCI hotplug driver
387 pci_unregister_driver(&megaraid_pci_driver);
394 * megaraid_probe_one - PCI hotplug entry point
395 * @pdev : handle to this controller's PCI configuration space
396 * @id : pci device id of the class of controllers
398 * This routine should be called whenever a new adapter is detected by the
399 * PCI hotplug susbsystem.
402 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
407 // detected a new controller
408 con_log(CL_ANN, (KERN_INFO
409 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
410 pdev->vendor, pdev->device, pdev->subsystem_vendor,
411 pdev->subsystem_device));
413 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
414 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
416 if (pci_enable_device(pdev)) {
417 con_log(CL_ANN, (KERN_WARNING
418 "megaraid: pci_enable_device failed\n"));
423 // Enable bus-mastering on this controller
424 pci_set_master(pdev);
426 // Allocate the per driver initialization structure
427 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
429 if (adapter == NULL) {
430 con_log(CL_ANN, (KERN_WARNING
431 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
437 // set up PCI related soft state and other pre-known parameters
438 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
439 adapter->irq = pdev->irq;
440 adapter->pdev = pdev;
442 atomic_set(&adapter->being_detached, 0);
444 // Setup the default DMA mask. This would be changed later on
445 // depending on hardware capabilities
446 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) {
447 con_log(CL_ANN, (KERN_WARNING
448 "megaraid: dma_set_mask failed:%d\n", __LINE__));
450 goto out_free_adapter;
454 // Initialize the synchronization lock for kernel and LLD
455 spin_lock_init(&adapter->lock);
457 // Initialize the command queues: the list of free SCBs and the list
459 INIT_LIST_HEAD(&adapter->kscb_pool);
460 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
462 INIT_LIST_HEAD(&adapter->pend_list);
463 spin_lock_init(PENDING_LIST_LOCK(adapter));
465 INIT_LIST_HEAD(&adapter->completed_list);
466 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
469 // Start the mailbox based controller
470 if (megaraid_init_mbox(adapter) != 0) {
471 con_log(CL_ANN, (KERN_WARNING
472 "megaraid: mailbox adapter did not initialize\n"));
474 goto out_free_adapter;
477 // Register with LSI Common Management Module
478 if (megaraid_cmm_register(adapter) != 0) {
480 con_log(CL_ANN, (KERN_WARNING
481 "megaraid: could not register with management module\n"));
486 // setup adapter handle in PCI soft state
487 pci_set_drvdata(pdev, adapter);
489 // attach with scsi mid-layer
490 if (megaraid_io_attach(adapter) != 0) {
492 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
500 megaraid_cmm_unregister(adapter);
502 megaraid_fini_mbox(adapter);
506 pci_disable_device(pdev);
513 * megaraid_detach_one - release framework resources and call LLD release routine
514 * @pdev : handle for our PCI configuration space
516 * This routine is called during driver unload. We free all the allocated
517 * resources and call the corresponding LLD so that it can also release all
520 * This routine is also called from the PCI hotplug system.
523 megaraid_detach_one(struct pci_dev *pdev)
526 struct Scsi_Host *host;
529 // Start a rollback on this adapter
530 adapter = pci_get_drvdata(pdev);
533 con_log(CL_ANN, (KERN_CRIT
534 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
535 pdev->vendor, pdev->device, pdev->subsystem_vendor,
536 pdev->subsystem_device));
541 con_log(CL_ANN, (KERN_NOTICE
542 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
543 pdev->vendor, pdev->device, pdev->subsystem_vendor,
544 pdev->subsystem_device));
548 host = adapter->host;
550 // do not allow any more requests from the management module for this
552 // FIXME: How do we account for the request which might still be
554 atomic_set(&adapter->being_detached, 1);
556 // detach from the IO sub-system
557 megaraid_io_detach(adapter);
559 // Unregister from common management module
561 // FIXME: this must return success or failure for conditions if there
562 // is a command pending with LLD or not.
563 megaraid_cmm_unregister(adapter);
565 // finalize the mailbox based controller and release all resources
566 megaraid_fini_mbox(adapter);
572 pci_disable_device(pdev);
579 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
580 * @pdev : generic driver model device
582 * Shutdown notification, perform flush cache.
585 megaraid_mbox_shutdown(struct pci_dev *pdev)
587 adapter_t *adapter = pci_get_drvdata(pdev);
591 con_log(CL_ANN, (KERN_WARNING
592 "megaraid: null device in shutdown\n"));
597 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
600 megaraid_mbox_flush_cache(adapter);
602 con_log(CL_ANN, ("done\n"));
607 * megaraid_io_attach - attach a device with the IO subsystem
608 * @adapter : controller's soft state
610 * Attach this device with the IO subsystem.
613 megaraid_io_attach(adapter_t *adapter)
615 struct Scsi_Host *host;
617 // Initialize SCSI Host structure
618 host = scsi_host_alloc(&megaraid_template_g, 8);
620 con_log(CL_ANN, (KERN_WARNING
621 "megaraid mbox: scsi_register failed\n"));
626 SCSIHOST2ADAP(host) = (caddr_t)adapter;
627 adapter->host = host;
629 host->irq = adapter->irq;
630 host->unique_id = adapter->unique_id;
631 host->can_queue = adapter->max_cmds;
632 host->this_id = adapter->init_id;
633 host->sg_tablesize = adapter->sglen;
634 host->max_sectors = adapter->max_sectors;
635 host->cmd_per_lun = adapter->cmd_per_lun;
636 host->max_channel = adapter->max_channel;
637 host->max_id = adapter->max_target;
638 host->max_lun = adapter->max_lun;
641 // notify mid-layer about the new controller
642 if (scsi_add_host(host, &adapter->pdev->dev)) {
644 con_log(CL_ANN, (KERN_WARNING
645 "megaraid mbox: scsi_add_host failed\n"));
652 scsi_scan_host(host);
659 * megaraid_io_detach - detach a device from the IO subsystem
660 * @adapter : controller's soft state
662 * Detach this device from the IO subsystem.
665 megaraid_io_detach(adapter_t *adapter)
667 struct Scsi_Host *host;
669 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
671 host = adapter->host;
673 scsi_remove_host(host);
680 * START: Mailbox Low Level Driver
682 * This is section specific to the single mailbox based controllers
686 * megaraid_init_mbox - initialize controller
687 * @adapter : our soft state
689 * - Allocate 16-byte aligned mailbox memory for firmware handshake
690 * - Allocate controller's memory resources
691 * - Find out all initialization data
692 * - Allocate memory required for all the commands
693 * - Use internal library of FW routines, build up complete soft state
696 megaraid_init_mbox(adapter_t *adapter)
698 struct pci_dev *pdev;
699 mraid_device_t *raid_dev;
704 adapter->ito = MBOX_TIMEOUT;
705 pdev = adapter->pdev;
708 * Allocate and initialize the init data structure for mailbox
711 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
712 if (raid_dev == NULL) return -1;
716 * Attach the adapter soft state to raid device soft state
718 adapter->raid_device = (caddr_t)raid_dev;
719 raid_dev->fast_load = megaraid_fast_load;
723 raid_dev->baseport = pci_resource_start(pdev, 0);
725 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
727 con_log(CL_ANN, (KERN_WARNING
728 "megaraid: mem region busy\n"));
730 goto out_free_raid_dev;
733 raid_dev->baseaddr = ioremap(raid_dev->baseport, 128);
735 if (!raid_dev->baseaddr) {
737 con_log(CL_ANN, (KERN_WARNING
738 "megaraid: could not map hba memory\n") );
740 goto out_release_regions;
743 /* initialize the mutual exclusion lock for the mailbox */
744 spin_lock_init(&raid_dev->mailbox_lock);
746 /* allocate memory required for commands */
747 if (megaraid_alloc_cmd_packets(adapter) != 0)
751 * Issue SYNC cmd to flush the pending cmds in the adapter
752 * and initialize its internal state
755 if (megaraid_mbox_fire_sync_cmd(adapter))
756 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
759 * Setup the rest of the soft state using the library of
763 /* request IRQ and register the interrupt service routine */
764 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
767 con_log(CL_ANN, (KERN_WARNING
768 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
774 if (megaraid_mbox_product_info(adapter) != 0)
777 // Do we support extended CDBs
778 adapter->max_cdb_sz = 10;
779 if (megaraid_mbox_extended_cdb(adapter) == 0) {
780 adapter->max_cdb_sz = 16;
784 * Do we support cluster environment, if we do, what is the initiator
786 * NOTE: In a non-cluster aware firmware environment, the LLD should
787 * return 7 as initiator id.
790 adapter->init_id = -1;
791 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
796 * Prepare the device ids array to have the mapping between the kernel
797 * device address and megaraid device address.
798 * We export the physical devices on their actual addresses. The
799 * logical drives are exported on a virtual SCSI channel
801 megaraid_mbox_setup_device_map(adapter);
803 // If the firmware supports random deletion, update the device id map
804 if (megaraid_mbox_support_random_del(adapter)) {
806 // Change the logical drives numbers in device_ids array one
807 // slot in device_ids is reserved for target id, that's why
809 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
810 adapter->device_ids[adapter->max_channel][i] += 0x80;
812 adapter->device_ids[adapter->max_channel][adapter->init_id] =
815 raid_dev->random_del_supported = 1;
819 * find out the maximum number of scatter-gather elements supported by
822 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
824 // enumerate RAID and SCSI channels so that all devices on SCSI
825 // channels can later be exported, including disk devices
826 megaraid_mbox_enum_raid_scsi(adapter);
829 * Other parameters required by upper layer
831 * maximum number of sectors per IO command
833 adapter->max_sectors = megaraid_max_sectors;
836 * number of queued commands per LUN.
838 adapter->cmd_per_lun = megaraid_cmd_per_lun;
841 * Allocate resources required to issue FW calls, when sysfs is
844 if (megaraid_sysfs_alloc_resources(adapter) != 0)
847 // Set the DMA mask to 64-bit. All supported controllers as capable of
849 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
851 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
852 ((adapter->pdev->subsystem_device !=
853 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
854 (adapter->pdev->subsystem_device !=
855 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
856 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
857 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
858 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
859 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
860 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
861 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
862 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
863 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
864 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
865 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
866 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) {
867 con_log(CL_ANN, (KERN_WARNING
868 "megaraid: DMA mask for 64-bit failed\n"));
870 if (dma_set_mask(&adapter->pdev->dev,
872 con_log(CL_ANN, (KERN_WARNING
873 "megaraid: 32-bit DMA mask failed\n"));
874 goto out_free_sysfs_res;
879 // setup tasklet for DPC
880 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
881 (unsigned long)adapter);
883 con_log(CL_DLEVEL1, (KERN_INFO
884 "megaraid mbox hba successfully initialized\n"));
889 megaraid_sysfs_free_resources(adapter);
891 free_irq(adapter->irq, adapter);
893 megaraid_free_cmd_packets(adapter);
895 iounmap(raid_dev->baseaddr);
897 pci_release_regions(pdev);
906 * megaraid_fini_mbox - undo controller initialization
907 * @adapter : our soft state
910 megaraid_fini_mbox(adapter_t *adapter)
912 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
915 megaraid_mbox_flush_cache(adapter);
917 tasklet_kill(&adapter->dpc_h);
919 megaraid_sysfs_free_resources(adapter);
921 megaraid_free_cmd_packets(adapter);
923 free_irq(adapter->irq, adapter);
925 iounmap(raid_dev->baseaddr);
927 pci_release_regions(adapter->pdev);
936 * megaraid_alloc_cmd_packets - allocate shared mailbox
937 * @adapter : soft state of the raid controller
939 * Allocate and align the shared mailbox. This mailbox is used to issue
940 * all the commands. For IO based controllers, the mailbox is also registered
941 * with the FW. Allocate memory for all commands as well.
942 * This is our big allocator.
945 megaraid_alloc_cmd_packets(adapter_t *adapter)
947 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
948 struct pci_dev *pdev;
952 struct mraid_pci_blk *epthru_pci_blk;
953 struct mraid_pci_blk *sg_pci_blk;
954 struct mraid_pci_blk *mbox_pci_blk;
957 pdev = adapter->pdev;
961 * Allocate the common 16-byte aligned memory for the handshake
964 raid_dev->una_mbox64 = dma_alloc_coherent(&adapter->pdev->dev,
966 &raid_dev->una_mbox64_dma,
969 if (!raid_dev->una_mbox64) {
970 con_log(CL_ANN, (KERN_WARNING
971 "megaraid: out of memory, %s %d\n", __func__,
977 * Align the mailbox at 16-byte boundary
979 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
981 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
984 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
986 align = ((void *)raid_dev->mbox -
987 ((void *)&raid_dev->una_mbox64->mbox32));
989 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
992 // Allocate memory for commands issued internally
993 adapter->ibuf = dma_alloc_coherent(&pdev->dev, MBOX_IBUF_SIZE,
994 &adapter->ibuf_dma_h, GFP_KERNEL);
995 if (!adapter->ibuf) {
997 con_log(CL_ANN, (KERN_WARNING
998 "megaraid: out of memory, %s %d\n", __func__,
1001 goto out_free_common_mbox;
1004 // Allocate memory for our SCSI Command Blocks and their associated
1008 * Allocate memory for the base list of scb. Later allocate memory for
1009 * CCBs and embedded components of each CCB and point the pointers in
1010 * scb to the allocated components
1011 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1012 * since the calling routine does not yet know the number of available
1015 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1017 if (adapter->kscb_list == NULL) {
1018 con_log(CL_ANN, (KERN_WARNING
1019 "megaraid: out of memory, %s %d\n", __func__,
1024 // memory allocation for our command packets
1025 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1026 con_log(CL_ANN, (KERN_WARNING
1027 "megaraid: out of memory, %s %d\n", __func__,
1029 goto out_free_scb_list;
1032 // Adjust the scb pointers and link in the free pool
1033 epthru_pci_blk = raid_dev->epthru_pool;
1034 sg_pci_blk = raid_dev->sg_pool;
1035 mbox_pci_blk = raid_dev->mbox_pool;
1037 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1038 scb = adapter->kscb_list + i;
1039 ccb = raid_dev->ccb_list + i;
1041 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1042 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1043 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1044 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1046 // make sure the mailbox is aligned properly
1047 if (ccb->mbox_dma_h & 0x0F) {
1048 con_log(CL_ANN, (KERN_CRIT
1049 "megaraid mbox: not aligned on 16-bytes\n"));
1051 goto out_teardown_dma_pools;
1054 ccb->epthru = (mraid_epassthru_t *)
1055 epthru_pci_blk[i].vaddr;
1056 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1057 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1058 ccb->pthru_dma_h = ccb->epthru_dma_h;
1061 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1062 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1063 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1065 scb->ccb = (caddr_t)ccb;
1068 scb->sno = i; // command index
1071 scb->state = SCB_FREE;
1072 scb->dma_direction = DMA_NONE;
1073 scb->dma_type = MRAID_DMA_NONE;
1074 scb->dev_channel = -1;
1075 scb->dev_target = -1;
1077 // put scb in the free pool
1078 list_add_tail(&scb->list, &adapter->kscb_pool);
1083 out_teardown_dma_pools:
1084 megaraid_mbox_teardown_dma_pools(adapter);
1086 kfree(adapter->kscb_list);
1088 dma_free_coherent(&pdev->dev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1089 adapter->ibuf_dma_h);
1090 out_free_common_mbox:
1091 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1092 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1099 * megaraid_free_cmd_packets - free memory
1100 * @adapter : soft state of the raid controller
1102 * Release memory resources allocated for commands.
1105 megaraid_free_cmd_packets(adapter_t *adapter)
1107 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1109 megaraid_mbox_teardown_dma_pools(adapter);
1111 kfree(adapter->kscb_list);
1113 dma_free_coherent(&adapter->pdev->dev, MBOX_IBUF_SIZE,
1114 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1116 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1117 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1123 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1124 * @adapter : HBA soft state
1126 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1127 * and scatter-gather lists.
1130 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1132 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1133 struct mraid_pci_blk *epthru_pci_blk;
1134 struct mraid_pci_blk *sg_pci_blk;
1135 struct mraid_pci_blk *mbox_pci_blk;
1140 // Allocate memory for 16-bytes aligned mailboxes
1141 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
1142 &adapter->pdev->dev,
1143 sizeof(mbox64_t) + 16,
1146 if (raid_dev->mbox_pool_handle == NULL) {
1147 goto fail_setup_dma_pool;
1150 mbox_pci_blk = raid_dev->mbox_pool;
1151 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1152 mbox_pci_blk[i].vaddr = dma_pool_alloc(
1153 raid_dev->mbox_pool_handle,
1155 &mbox_pci_blk[i].dma_addr);
1156 if (!mbox_pci_blk[i].vaddr) {
1157 goto fail_setup_dma_pool;
1162 * Allocate memory for each embedded passthru strucuture pointer
1163 * Request for a 128 bytes aligned structure for each passthru command
1165 * Since passthru and extended passthru commands are exclusive, they
1166 * share common memory pool. Passthru structures piggyback on memory
1167 * allocated to extended passthru since passthru is smaller of the two
1169 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
1170 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
1172 if (raid_dev->epthru_pool_handle == NULL) {
1173 goto fail_setup_dma_pool;
1176 epthru_pci_blk = raid_dev->epthru_pool;
1177 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1178 epthru_pci_blk[i].vaddr = dma_pool_alloc(
1179 raid_dev->epthru_pool_handle,
1181 &epthru_pci_blk[i].dma_addr);
1182 if (!epthru_pci_blk[i].vaddr) {
1183 goto fail_setup_dma_pool;
1188 // Allocate memory for each scatter-gather list. Request for 512 bytes
1189 // alignment for each sg list
1190 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
1191 &adapter->pdev->dev,
1192 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1195 if (raid_dev->sg_pool_handle == NULL) {
1196 goto fail_setup_dma_pool;
1199 sg_pci_blk = raid_dev->sg_pool;
1200 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1201 sg_pci_blk[i].vaddr = dma_pool_alloc(
1202 raid_dev->sg_pool_handle,
1204 &sg_pci_blk[i].dma_addr);
1205 if (!sg_pci_blk[i].vaddr) {
1206 goto fail_setup_dma_pool;
1212 fail_setup_dma_pool:
1213 megaraid_mbox_teardown_dma_pools(adapter);
1219 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1220 * @adapter : HBA soft state
1222 * Teardown the dma pool for mailbox, passthru and extended passthru
1223 * structures, and scatter-gather lists.
1226 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1228 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1229 struct mraid_pci_blk *epthru_pci_blk;
1230 struct mraid_pci_blk *sg_pci_blk;
1231 struct mraid_pci_blk *mbox_pci_blk;
1235 sg_pci_blk = raid_dev->sg_pool;
1236 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1237 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1238 sg_pci_blk[i].dma_addr);
1240 dma_pool_destroy(raid_dev->sg_pool_handle);
1243 epthru_pci_blk = raid_dev->epthru_pool;
1244 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1245 dma_pool_free(raid_dev->epthru_pool_handle,
1246 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1248 dma_pool_destroy(raid_dev->epthru_pool_handle);
1251 mbox_pci_blk = raid_dev->mbox_pool;
1252 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1253 dma_pool_free(raid_dev->mbox_pool_handle,
1254 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1256 dma_pool_destroy(raid_dev->mbox_pool_handle);
1263 * megaraid_alloc_scb - detach and return a scb from the free list
1264 * @adapter : controller's soft state
1265 * @scp : pointer to the scsi command to be executed
1267 * Return the scb from the head of the free list. %NULL if there are none
1271 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1273 struct list_head *head = &adapter->kscb_pool;
1275 unsigned long flags;
1277 // detach scb from free pool
1278 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1280 if (list_empty(head)) {
1281 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1285 scb = list_entry(head->next, scb_t, list);
1286 list_del_init(&scb->list);
1288 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1290 scb->state = SCB_ACTIVE;
1292 scb->dma_type = MRAID_DMA_NONE;
1299 * megaraid_dealloc_scb - return the scb to the free pool
1300 * @adapter : controller's soft state
1301 * @scb : scb to be freed
1303 * Return the scb back to the free list of scbs. The caller must 'flush' the
1304 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1305 * NOTE NOTE: Make sure the scb is not on any list before calling this
1309 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1311 unsigned long flags;
1313 // put scb in the free pool
1314 scb->state = SCB_FREE;
1316 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1318 list_add(&scb->list, &adapter->kscb_pool);
1320 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1327 * megaraid_mbox_mksgl - make the scatter-gather list
1328 * @adapter : controller's soft state
1329 * @scb : scsi control block
1331 * Prepare the scatter-gather list.
1334 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1336 struct scatterlist *sgl;
1338 struct scsi_cmnd *scp;
1344 ccb = (mbox_ccb_t *)scb->ccb;
1346 sgcnt = scsi_dma_map(scp);
1347 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1349 // no mapping required if no data to be transferred
1353 scb->dma_type = MRAID_DMA_WSG;
1355 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1356 ccb->sgl64[i].address = sg_dma_address(sgl);
1357 ccb->sgl64[i].length = sg_dma_len(sgl);
1360 // Return count of SG nodes
1366 * mbox_post_cmd - issue a mailbox command
1367 * @adapter : controller's soft state
1368 * @scb : command to be issued
1370 * Post the command to the controller if mailbox is available.
1373 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1375 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1379 unsigned long flags;
1383 ccb = (mbox_ccb_t *)scb->ccb;
1384 mbox = raid_dev->mbox;
1385 mbox64 = raid_dev->mbox64;
1388 * Check for busy mailbox. If it is, return failure - the caller
1389 * should retry later.
1391 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1393 if (unlikely(mbox->busy)) {
1398 } while(mbox->busy && (i < max_mbox_busy_wait));
1402 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1409 // Copy this command's mailbox data into "adapter's" mailbox
1410 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1411 mbox->cmdid = scb->sno;
1413 adapter->outstanding_cmds++;
1415 mbox->busy = 1; // Set busy
1420 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1422 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1429 * megaraid_queue_command_lck - generic queue entry point for all LLDs
1430 * @scp : pointer to the scsi command to be executed
1431 * @done : callback routine to be called after the cmd has be completed
1433 * Queue entry point for mailbox based controllers.
1436 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1442 adapter = SCP2ADAPTER(scp);
1443 scp->scsi_done = done;
1447 * Allocate and build a SCB request
1448 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1449 * not allocate scb. We will return non-zero status in that case.
1450 * NOTE: scb can be null even though certain commands completed
1451 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1452 * return 0 in that case, and we would do the callback right away.
1455 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1456 if (!scb) { // command already completed
1461 megaraid_mbox_runpendq(adapter, scb);
1465 static DEF_SCSI_QCMD(megaraid_queue_command)
1468 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1469 * @adapter : controller's soft state
1470 * @scp : mid-layer scsi command pointer
1471 * @busy : set if request could not be completed because of lack of
1474 * Transform the mid-layer scsi command to megaraid firmware lingua.
1475 * Convert the command issued by mid-layer to format understood by megaraid
1476 * firmware. We also complete certain commands without sending them to firmware.
1479 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1481 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1486 mraid_passthru_t *pthru;
1490 char skip[] = "skipping";
1491 char scan[] = "scanning";
1496 * Get the appropriate device map for the device this command is
1499 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1502 * Logical drive commands
1505 switch (scp->cmnd[0]) {
1506 case TEST_UNIT_READY:
1508 * Do we support clustering and is the support enabled
1509 * If no, return success always
1512 scp->result = (DID_OK << 16);
1516 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1517 scp->result = (DID_ERROR << 16);
1522 scb->dma_direction = scp->sc_data_direction;
1523 scb->dev_channel = 0xFF;
1524 scb->dev_target = target;
1525 ccb = (mbox_ccb_t *)scb->ccb;
1528 * The command id will be provided by the command
1531 ccb->raw_mbox[0] = CLUSTER_CMD;
1532 ccb->raw_mbox[2] = RESERVATION_STATUS;
1533 ccb->raw_mbox[3] = target;
1539 struct scatterlist *sgl;
1542 sgl = scsi_sglist(scp);
1544 vaddr = (caddr_t) sg_virt(&sgl[0]);
1546 memset(vaddr, 0, scp->cmnd[4]);
1549 con_log(CL_ANN, (KERN_WARNING
1550 "megaraid mailbox: invalid sg:%d\n",
1554 scp->result = (DID_OK << 16);
1559 * Display the channel scan for logical drives
1560 * Do not display scan for a channel if already done.
1562 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1564 con_log(CL_ANN, (KERN_INFO
1565 "scsi[%d]: scanning scsi channel %d",
1566 adapter->host->host_no,
1570 " [virtual] for logical drives\n"));
1572 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1575 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1576 scsi_build_sense(scp, 0, ILLEGAL_REQUEST,
1577 MEGA_INVALID_FIELD_IN_CDB, 0);
1585 * Do not allow LUN > 0 for logical drives and
1586 * requests for more than 40 logical drives
1589 scp->result = (DID_BAD_TARGET << 16);
1592 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1593 scp->result = (DID_BAD_TARGET << 16);
1598 /* Allocate a SCB and initialize passthru */
1599 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1600 scp->result = (DID_ERROR << 16);
1605 ccb = (mbox_ccb_t *)scb->ccb;
1606 scb->dev_channel = 0xFF;
1607 scb->dev_target = target;
1610 mbox64 = ccb->mbox64;
1614 pthru->reqsenselen = 14;
1615 pthru->islogical = 1;
1616 pthru->logdrv = target;
1617 pthru->cdblen = scp->cmd_len;
1618 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1620 mbox->cmd = MBOXCMD_PASSTHRU64;
1621 scb->dma_direction = scp->sc_data_direction;
1623 pthru->dataxferlen = scsi_bufflen(scp);
1624 pthru->dataxferaddr = ccb->sgl_dma_h;
1625 pthru->numsge = megaraid_mbox_mksgl(adapter,
1628 mbox->xferaddr = 0xFFFFFFFF;
1629 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1630 mbox64->xferaddr_hi = 0;
1642 * Allocate a SCB and initialize mailbox
1644 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1645 scp->result = (DID_ERROR << 16);
1649 ccb = (mbox_ccb_t *)scb->ccb;
1650 scb->dev_channel = 0xFF;
1651 scb->dev_target = target;
1653 mbox64 = ccb->mbox64;
1654 mbox->logdrv = target;
1657 * A little HACK: 2nd bit is zero for all scsi read
1658 * commands and is set for all scsi write commands
1660 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1664 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1666 if (scp->cmd_len == 6) {
1667 mbox->numsectors = (uint32_t)scp->cmnd[4];
1669 ((uint32_t)scp->cmnd[1] << 16) |
1670 ((uint32_t)scp->cmnd[2] << 8) |
1671 (uint32_t)scp->cmnd[3];
1673 mbox->lba &= 0x1FFFFF;
1677 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1679 else if (scp->cmd_len == 10) {
1681 (uint32_t)scp->cmnd[8] |
1682 ((uint32_t)scp->cmnd[7] << 8);
1684 ((uint32_t)scp->cmnd[2] << 24) |
1685 ((uint32_t)scp->cmnd[3] << 16) |
1686 ((uint32_t)scp->cmnd[4] << 8) |
1687 (uint32_t)scp->cmnd[5];
1691 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1693 else if (scp->cmd_len == 12) {
1695 ((uint32_t)scp->cmnd[2] << 24) |
1696 ((uint32_t)scp->cmnd[3] << 16) |
1697 ((uint32_t)scp->cmnd[4] << 8) |
1698 (uint32_t)scp->cmnd[5];
1701 ((uint32_t)scp->cmnd[6] << 24) |
1702 ((uint32_t)scp->cmnd[7] << 16) |
1703 ((uint32_t)scp->cmnd[8] << 8) |
1704 (uint32_t)scp->cmnd[9];
1707 con_log(CL_ANN, (KERN_WARNING
1708 "megaraid: unsupported CDB length\n"));
1710 megaraid_dealloc_scb(adapter, scb);
1712 scp->result = (DID_ERROR << 16);
1716 scb->dma_direction = scp->sc_data_direction;
1718 // Calculate Scatter-Gather info
1719 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1720 mbox->numsge = megaraid_mbox_mksgl(adapter,
1722 mbox->xferaddr = 0xFFFFFFFF;
1723 mbox64->xferaddr_hi = 0;
1730 * Do we support clustering and is the support enabled
1733 scp->result = (DID_BAD_TARGET << 16);
1738 * Allocate a SCB and initialize mailbox
1740 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1741 scp->result = (DID_ERROR << 16);
1746 ccb = (mbox_ccb_t *)scb->ccb;
1747 scb->dev_channel = 0xFF;
1748 scb->dev_target = target;
1749 ccb->raw_mbox[0] = CLUSTER_CMD;
1750 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1751 RESERVE_LD : RELEASE_LD;
1753 ccb->raw_mbox[3] = target;
1754 scb->dma_direction = scp->sc_data_direction;
1759 scp->result = (DID_BAD_TARGET << 16);
1763 else { // Passthru device commands
1765 // Do not allow access to target id > 15 or LUN > 7
1766 if (target > 15 || SCP2LUN(scp) > 7) {
1767 scp->result = (DID_BAD_TARGET << 16);
1771 // if fast load option was set and scan for last device is
1772 // over, reset the fast_load flag so that during a possible
1773 // next scan, devices can be made available
1774 if (rdev->fast_load && (target == 15) &&
1775 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1777 con_log(CL_ANN, (KERN_INFO
1778 "megaraid[%d]: physical device scan re-enabled\n",
1779 adapter->host->host_no));
1780 rdev->fast_load = 0;
1784 * Display the channel scan for physical devices
1786 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1788 ss = rdev->fast_load ? skip : scan;
1790 con_log(CL_ANN, (KERN_INFO
1791 "scsi[%d]: %s scsi channel %d [Phy %d]",
1792 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1796 " for non-raid devices\n"));
1798 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1801 // disable channel sweep if fast load option given
1802 if (rdev->fast_load) {
1803 scp->result = (DID_BAD_TARGET << 16);
1807 // Allocate a SCB and initialize passthru
1808 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1809 scp->result = (DID_ERROR << 16);
1814 ccb = (mbox_ccb_t *)scb->ccb;
1815 scb->dev_channel = channel;
1816 scb->dev_target = target;
1817 scb->dma_direction = scp->sc_data_direction;
1819 mbox64 = ccb->mbox64;
1821 // Does this firmware support extended CDBs
1822 if (adapter->max_cdb_sz == 16) {
1823 mbox->cmd = MBOXCMD_EXTPTHRU;
1825 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1827 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1828 mbox64->xferaddr_hi = 0;
1829 mbox->xferaddr = 0xFFFFFFFF;
1832 mbox->cmd = MBOXCMD_PASSTHRU64;
1834 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1836 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1837 mbox64->xferaddr_hi = 0;
1838 mbox->xferaddr = 0xFFFFFFFF;
1848 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1849 * @adapter : controller's soft state
1850 * @scb_q : SCB to be queued in the pending list
1852 * Scan the pending list for commands which are not yet issued and try to
1853 * post to the controller. The SCB can be a null pointer, which would indicate
1854 * no SCB to be queue, just try to execute the ones in the pending list.
1856 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1857 * out from the head of the pending list. If it is successfully issued, the
1858 * next SCB is at the head now.
1861 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1864 unsigned long flags;
1866 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1869 scb_q->state = SCB_PENDQ;
1870 list_add_tail(&scb_q->list, &adapter->pend_list);
1873 // if the adapter in not in quiescent mode, post the commands to FW
1874 if (adapter->quiescent) {
1875 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1879 while (!list_empty(&adapter->pend_list)) {
1881 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1883 scb = list_entry(adapter->pend_list.next, scb_t, list);
1885 // remove the scb from the pending list and try to
1886 // issue. If we are unable to issue it, put back in
1887 // the pending list and return
1889 list_del_init(&scb->list);
1891 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1893 // if mailbox was busy, return SCB back to pending
1894 // list. Make sure to add at the head, since that's
1895 // where it would have been removed from
1897 scb->state = SCB_ISSUED;
1899 if (mbox_post_cmd(adapter, scb) != 0) {
1901 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1903 scb->state = SCB_PENDQ;
1905 list_add(&scb->list, &adapter->pend_list);
1907 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1913 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1916 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1924 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1925 * @adapter : pointer to controller's soft state
1926 * @scb : scsi control block
1927 * @scp : scsi command from the mid-layer
1929 * Prepare a command for the scsi physical devices.
1932 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1933 struct scsi_cmnd *scp)
1936 mraid_passthru_t *pthru;
1940 ccb = (mbox_ccb_t *)scb->ccb;
1942 channel = scb->dev_channel;
1943 target = scb->dev_target;
1945 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1948 pthru->islogical = 0;
1950 pthru->target = (channel << 4) | target;
1951 pthru->logdrv = SCP2LUN(scp);
1952 pthru->reqsenselen = 14;
1953 pthru->cdblen = scp->cmd_len;
1955 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1957 if (scsi_bufflen(scp)) {
1958 pthru->dataxferlen = scsi_bufflen(scp);
1959 pthru->dataxferaddr = ccb->sgl_dma_h;
1960 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1963 pthru->dataxferaddr = 0;
1964 pthru->dataxferlen = 0;
1972 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1973 * @adapter : pointer to controller's soft state
1974 * @scb : scsi control block
1975 * @scp : scsi command from the mid-layer
1977 * Prepare a command for the scsi physical devices. This routine prepares
1978 * commands for devices which can take extended CDBs (>10 bytes).
1981 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
1982 struct scsi_cmnd *scp)
1985 mraid_epassthru_t *epthru;
1989 ccb = (mbox_ccb_t *)scb->ccb;
1990 epthru = ccb->epthru;
1991 channel = scb->dev_channel;
1992 target = scb->dev_target;
1994 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1995 epthru->timeout = 4;
1997 epthru->islogical = 0;
1998 epthru->channel = 0;
1999 epthru->target = (channel << 4) | target;
2000 epthru->logdrv = SCP2LUN(scp);
2001 epthru->reqsenselen = 14;
2002 epthru->cdblen = scp->cmd_len;
2004 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2006 if (scsi_bufflen(scp)) {
2007 epthru->dataxferlen = scsi_bufflen(scp);
2008 epthru->dataxferaddr = ccb->sgl_dma_h;
2009 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2012 epthru->dataxferaddr = 0;
2013 epthru->dataxferlen = 0;
2021 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2022 * @adapter : controller's soft state
2024 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2025 * completed command and put them on the completed list for later processing.
2027 * Returns: 1 if the interrupt is valid, 0 otherwise
2030 megaraid_ack_sequence(adapter_t *adapter)
2032 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2036 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2037 struct list_head clist;
2040 unsigned long flags;
2044 mbox = raid_dev->mbox;
2046 // move the SCBs from the firmware completed array to our local list
2047 INIT_LIST_HEAD(&clist);
2049 // loop till F/W has more commands for us to complete
2051 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2054 * Check if a valid interrupt is pending. If found, force the
2055 * interrupt line low.
2057 dword = RDOUTDOOR(raid_dev);
2058 if (dword != 0x10001234) break;
2062 WROUTDOOR(raid_dev, 0x10001234);
2065 // wait for valid numstatus to post
2066 for (i = 0; i < 0xFFFFF; i++) {
2067 if (mbox->numstatus != 0xFF) {
2068 nstatus = mbox->numstatus;
2073 mbox->numstatus = 0xFF;
2075 adapter->outstanding_cmds -= nstatus;
2077 for (i = 0; i < nstatus; i++) {
2079 // wait for valid command index to post
2080 for (j = 0; j < 0xFFFFF; j++) {
2081 if (mbox->completed[i] != 0xFF) break;
2084 completed[i] = mbox->completed[i];
2085 mbox->completed[i] = 0xFF;
2087 if (completed[i] == 0xFF) {
2088 con_log(CL_ANN, (KERN_CRIT
2089 "megaraid: command posting timed out\n"));
2095 // Get SCB associated with this command id
2096 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2098 scb = adapter->uscb_list + (completed[i] -
2099 MBOX_MAX_SCSI_CMDS);
2103 scb = adapter->kscb_list + completed[i];
2106 scb->status = mbox->status;
2107 list_add_tail(&scb->list, &clist);
2110 // Acknowledge interrupt
2111 WRINDOOR(raid_dev, 0x02);
2115 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2118 // put the completed commands in the completed list. DPC would
2119 // complete these commands later
2120 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2122 list_splice(&clist, &adapter->completed_list);
2124 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2127 // schedule the DPC if there is some work for it
2129 tasklet_schedule(&adapter->dpc_h);
2136 * megaraid_isr - isr for memory based mailbox based controllers
2138 * @devp : pointer to our soft state
2140 * Interrupt service routine for memory-mapped mailbox controllers.
2143 megaraid_isr(int irq, void *devp)
2145 adapter_t *adapter = devp;
2148 handled = megaraid_ack_sequence(adapter);
2150 /* Loop through any pending requests */
2151 if (!adapter->quiescent) {
2152 megaraid_mbox_runpendq(adapter, NULL);
2155 return IRQ_RETVAL(handled);
2160 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2161 * @devp : pointer to HBA soft state
2163 * Pick up the commands from the completed list and send back to the owners.
2164 * This is a reentrant function and does not assume any locks are held while
2165 * it is being called.
2168 megaraid_mbox_dpc(unsigned long devp)
2170 adapter_t *adapter = (adapter_t *)devp;
2171 mraid_device_t *raid_dev;
2172 struct list_head clist;
2173 struct scatterlist *sgl;
2176 struct scsi_cmnd *scp;
2177 mraid_passthru_t *pthru;
2178 mraid_epassthru_t *epthru;
2184 unsigned long flags;
2190 if (!adapter) return;
2192 raid_dev = ADAP2RAIDDEV(adapter);
2194 // move the SCBs from the completed list to our local list
2195 INIT_LIST_HEAD(&clist);
2197 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2199 list_splice_init(&adapter->completed_list, &clist);
2201 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2204 list_for_each_entry_safe(scb, tmp, &clist, list) {
2206 status = scb->status;
2208 ccb = (mbox_ccb_t *)scb->ccb;
2210 epthru = ccb->epthru;
2213 // Make sure f/w has completed a valid command
2214 if (scb->state != SCB_ISSUED) {
2215 con_log(CL_ANN, (KERN_CRIT
2216 "megaraid critical err: invalid command %d:%d:%p\n",
2217 scb->sno, scb->state, scp));
2219 continue; // Must never happen!
2222 // check for the management command and complete it right away
2223 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2224 scb->state = SCB_FREE;
2225 scb->status = status;
2227 // remove from local clist
2228 list_del_init(&scb->list);
2230 kioc = (uioc_t *)scb->gp;
2233 megaraid_mbox_mm_done(adapter, scb);
2238 // Was an abort issued for this command earlier
2239 if (scb->state & SCB_ABORT) {
2240 con_log(CL_ANN, (KERN_NOTICE
2241 "megaraid: aborted cmd [%x] completed\n",
2246 * If the inquiry came of a disk drive which is not part of
2247 * any RAID array, expose it to the kernel. For this to be
2248 * enabled, user must set the "megaraid_expose_unconf_disks"
2249 * flag to 1 by specifying it on module parameter list.
2250 * This would enable data migration off drives from other
2253 islogical = MRAID_IS_LOGICAL(adapter, scp);
2254 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2255 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2257 sgl = scsi_sglist(scp);
2259 c = *(unsigned char *) sg_virt(&sgl[0]);
2261 con_log(CL_ANN, (KERN_WARNING
2262 "megaraid mailbox: invalid sg:%d\n",
2267 if ((c & 0x1F ) == TYPE_DISK) {
2268 pdev_index = (scb->dev_channel * 16) +
2271 raid_dev->pdrv_state[pdev_index] & 0x0F;
2273 if (pdev_state == PDRV_ONLINE ||
2274 pdev_state == PDRV_FAILED ||
2275 pdev_state == PDRV_RBLD ||
2276 pdev_state == PDRV_HOTSPARE ||
2277 megaraid_expose_unconf_disks == 0) {
2284 // Convert MegaRAID status to Linux error code
2289 scp->result = (DID_OK << 16);
2294 /* set sense_buffer and result fields */
2295 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2296 mbox->cmd == MBOXCMD_PASSTHRU64) {
2298 memcpy(scp->sense_buffer, pthru->reqsensearea,
2301 scp->result = SAM_STAT_CHECK_CONDITION;
2304 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2306 memcpy(scp->sense_buffer,
2307 epthru->reqsensearea, 14);
2309 scp->result = SAM_STAT_CHECK_CONDITION;
2311 scsi_build_sense(scp, 0,
2312 ABORTED_COMMAND, 0, 0);
2318 scp->result = DID_BUS_BUSY << 16 | status;
2324 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2327 if (scp->cmnd[0] == TEST_UNIT_READY) {
2328 scp->result = DID_ERROR << 16 |
2329 SAM_STAT_RESERVATION_CONFLICT;
2333 * Error code returned is 1 if Reserve or Release
2334 * failed or the input parameter is invalid
2336 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2337 scp->cmnd[0] == RELEASE)) {
2339 scp->result = DID_ERROR << 16 |
2340 SAM_STAT_RESERVATION_CONFLICT;
2343 scp->result = DID_BAD_TARGET << 16 | status;
2347 // print a debug message for all failed commands
2349 megaraid_mbox_display_scb(adapter, scb);
2352 scsi_dma_unmap(scp);
2354 // remove from local clist
2355 list_del_init(&scb->list);
2357 // put back in free list
2358 megaraid_dealloc_scb(adapter, scb);
2360 // send the scsi packet back to kernel
2361 scp->scsi_done(scp);
2369 * megaraid_abort_handler - abort the scsi command
2370 * @scp : command to be aborted
2372 * Abort a previous SCSI request. Only commands on the pending list can be
2373 * aborted. All the commands issued to the F/W must complete.
2376 megaraid_abort_handler(struct scsi_cmnd *scp)
2379 mraid_device_t *raid_dev;
2383 unsigned long flags;
2387 adapter = SCP2ADAPTER(scp);
2388 raid_dev = ADAP2RAIDDEV(adapter);
2390 con_log(CL_ANN, (KERN_WARNING
2391 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2392 scp->cmnd[0], SCP2CHANNEL(scp),
2393 SCP2TARGET(scp), SCP2LUN(scp)));
2395 // If FW has stopped responding, simply return failure
2396 if (raid_dev->hw_error) {
2397 con_log(CL_ANN, (KERN_NOTICE
2398 "megaraid: hw error, not aborting\n"));
2402 // There might a race here, where the command was completed by the
2403 // firmware and now it is on the completed list. Before we could
2404 // complete the command to the kernel in dpc, the abort came.
2405 // Find out if this is the case to avoid the race.
2407 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2408 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2410 if (scb->scp == scp) { // Found command
2412 list_del_init(&scb->list); // from completed list
2414 con_log(CL_ANN, (KERN_WARNING
2415 "megaraid: %d[%d:%d], abort from completed list\n",
2416 scb->sno, scb->dev_channel, scb->dev_target));
2418 scp->result = (DID_ABORT << 16);
2419 scp->scsi_done(scp);
2421 megaraid_dealloc_scb(adapter, scb);
2423 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2429 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2432 // Find out if this command is still on the pending list. If it is and
2433 // was never issued, abort and return success. If the command is owned
2434 // by the firmware, we must wait for it to complete by the FW.
2435 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2436 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2438 if (scb->scp == scp) { // Found command
2440 list_del_init(&scb->list); // from pending list
2442 ASSERT(!(scb->state & SCB_ISSUED));
2444 con_log(CL_ANN, (KERN_WARNING
2445 "megaraid abort: [%d:%d], driver owner\n",
2446 scb->dev_channel, scb->dev_target));
2448 scp->result = (DID_ABORT << 16);
2449 scp->scsi_done(scp);
2451 megaraid_dealloc_scb(adapter, scb);
2453 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2459 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2462 // Check do we even own this command, in which case this would be
2463 // owned by the firmware. The only way to locate the FW scb is to
2464 // traverse through the list of all SCB, since driver does not
2465 // maintain these SCBs on any list
2467 spin_lock_irq(&adapter->lock);
2468 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2469 scb = adapter->kscb_list + i;
2471 if (scb->scp == scp) {
2475 if (!(scb->state & SCB_ISSUED)) {
2476 con_log(CL_ANN, (KERN_WARNING
2477 "megaraid abort: %d[%d:%d], invalid state\n",
2478 scb->sno, scb->dev_channel, scb->dev_target));
2482 con_log(CL_ANN, (KERN_WARNING
2483 "megaraid abort: %d[%d:%d], fw owner\n",
2484 scb->sno, scb->dev_channel, scb->dev_target));
2488 spin_unlock_irq(&adapter->lock);
2491 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2493 // FIXME: Should there be a callback for this command?
2497 // We cannot actually abort a command owned by firmware, return
2498 // failure and wait for reset. In host reset handler, we will find out
2499 // if the HBA is still live
2504 * megaraid_reset_handler - device reset handler for mailbox based driver
2505 * @scp : reference command
2507 * Reset handler for the mailbox based controller. First try to find out if
2508 * the FW is still live, in which case the outstanding commands counter mut go
2509 * down to 0. If that happens, also issue the reservation reset command to
2510 * relinquish (possible) reservations on the logical drives connected to this
2514 megaraid_reset_handler(struct scsi_cmnd *scp)
2519 mraid_device_t *raid_dev;
2520 unsigned long flags;
2521 uint8_t raw_mbox[sizeof(mbox_t)];
2523 int recovery_window;
2527 adapter = SCP2ADAPTER(scp);
2528 raid_dev = ADAP2RAIDDEV(adapter);
2530 // return failure if adapter is not responding
2531 if (raid_dev->hw_error) {
2532 con_log(CL_ANN, (KERN_NOTICE
2533 "megaraid: hw error, cannot reset\n"));
2537 // Under exceptional conditions, FW can take up to 3 minutes to
2538 // complete command processing. Wait for additional 2 minutes for the
2539 // pending commands counter to go down to 0. If it doesn't, let the
2540 // controller be marked offline
2541 // Also, reset all the commands currently owned by the driver
2542 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2543 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2544 list_del_init(&scb->list); // from pending list
2546 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2547 con_log(CL_ANN, (KERN_WARNING
2548 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2549 scb->sno, scb->dev_channel, scb->dev_target));
2553 kioc = (uioc_t *)scb->gp;
2554 kioc->status = -EFAULT;
2556 megaraid_mbox_mm_done(adapter, scb);
2558 if (scb->scp == scp) { // Found command
2559 con_log(CL_ANN, (KERN_WARNING
2560 "megaraid: %d[%d:%d], reset from pending list\n",
2561 scb->sno, scb->dev_channel, scb->dev_target));
2563 con_log(CL_ANN, (KERN_WARNING
2564 "megaraid: IO packet with %d[%d:%d] being reset\n",
2565 scb->sno, scb->dev_channel, scb->dev_target));
2568 scb->scp->result = (DID_RESET << 16);
2569 scb->scp->scsi_done(scb->scp);
2571 megaraid_dealloc_scb(adapter, scb);
2574 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2576 if (adapter->outstanding_cmds) {
2577 con_log(CL_ANN, (KERN_NOTICE
2578 "megaraid: %d outstanding commands. Max wait %d sec\n",
2579 adapter->outstanding_cmds,
2580 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2583 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2585 for (i = 0; i < recovery_window; i++) {
2587 megaraid_ack_sequence(adapter);
2589 // print a message once every 5 seconds only
2592 "megaraid mbox: Wait for %d commands to complete:%d\n",
2593 adapter->outstanding_cmds,
2594 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2597 // bailout if no recovery happened in reset time
2598 if (adapter->outstanding_cmds == 0) {
2605 spin_lock(&adapter->lock);
2607 // If still outstanding commands, bail out
2608 if (adapter->outstanding_cmds) {
2609 con_log(CL_ANN, (KERN_WARNING
2610 "megaraid mbox: critical hardware error!\n"));
2612 raid_dev->hw_error = 1;
2618 con_log(CL_ANN, (KERN_NOTICE
2619 "megaraid mbox: reset sequence completed successfully\n"));
2623 // If the controller supports clustering, reset reservations
2629 // clear reservations if any
2630 raw_mbox[0] = CLUSTER_CMD;
2631 raw_mbox[2] = RESET_RESERVATIONS;
2634 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2636 (KERN_INFO "megaraid: reservation reset\n"));
2640 con_log(CL_ANN, (KERN_WARNING
2641 "megaraid: reservation reset failed\n"));
2645 spin_unlock(&adapter->lock);
2650 * START: internal commands library
2652 * This section of the driver has the common routine used by the driver and
2653 * also has all the FW routines
2657 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2658 * @adapter : controller's soft state
2659 * @raw_mbox : the mailbox
2661 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2665 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2667 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2672 mbox = raid_dev->mbox;
2675 * Wait until mailbox is free
2677 if (megaraid_busywait_mbox(raid_dev) != 0)
2678 goto blocked_mailbox;
2681 * Copy mailbox data into host structure
2683 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2688 mbox->numstatus = 0xFF;
2689 mbox->status = 0xFF;
2692 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2694 // wait for maximum 1 second for status to post. If the status is not
2695 // available within 1 second, assume FW is initializing and wait
2696 // for an extended amount of time
2697 if (mbox->numstatus == 0xFF) { // status not yet available
2700 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2707 con_log(CL_ANN, (KERN_NOTICE
2708 "megaraid mailbox: wait for FW to boot "));
2710 for (i = 0; (mbox->numstatus == 0xFF) &&
2711 (i < MBOX_RESET_WAIT); i++) {
2713 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2714 MBOX_RESET_WAIT - i));
2718 if (i == MBOX_RESET_WAIT) {
2721 "\nmegaraid mailbox: status not available\n"));
2725 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2729 // wait for maximum 1 second for poll semaphore
2730 if (mbox->poll != 0x77) {
2733 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2739 con_log(CL_ANN, (KERN_WARNING
2740 "megaraid mailbox: could not get poll semaphore\n"));
2745 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2748 // wait for maximum 1 second for acknowledgement
2749 if (RDINDOOR(raid_dev) & 0x2) {
2752 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2758 con_log(CL_ANN, (KERN_WARNING
2759 "megaraid mailbox: could not acknowledge\n"));
2766 status = mbox->status;
2768 // invalidate the completed command id array. After command
2769 // completion, firmware would write the valid id.
2770 mbox->numstatus = 0xFF;
2771 mbox->status = 0xFF;
2772 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2773 mbox->completed[i] = 0xFF;
2780 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2786 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2787 * @adapter : controller's soft state
2788 * @raw_mbox : the mailbox
2790 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2791 * controllers. This is a faster version of the synchronous command and
2792 * therefore can be called in interrupt-context as well.
2795 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2797 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2802 mbox = raid_dev->mbox;
2804 // return immediately if the mailbox is busy
2805 if (mbox->busy) return -1;
2807 // Copy mailbox data into host structure
2808 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2813 mbox->numstatus = 0xFF;
2814 mbox->status = 0xFF;
2817 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2819 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2820 if (mbox->numstatus != 0xFF) break;
2822 udelay(MBOX_SYNC_DELAY_200);
2825 if (i == MBOX_SYNC_WAIT_CNT) {
2826 // We may need to re-calibrate the counter
2827 con_log(CL_ANN, (KERN_CRIT
2828 "megaraid: fast sync command timed out\n"));
2831 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2834 return mbox->status;
2839 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2840 * @raid_dev : RAID device (HBA) soft state
2842 * Wait until the controller's mailbox is available to accept more commands.
2843 * Wait for at most 1 second.
2846 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2848 mbox_t *mbox = raid_dev->mbox;
2853 for (i = 0; mbox->busy && i < 1000; i++)
2857 if (i < 1000) return 0;
2863 * megaraid_mbox_product_info - some static information about the controller
2864 * @adapter : our soft state
2866 * Issue commands to the controller to grab some parameters required by our
2870 megaraid_mbox_product_info(adapter_t *adapter)
2872 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2874 uint8_t raw_mbox[sizeof(mbox_t)];
2875 mraid_pinfo_t *pinfo;
2876 dma_addr_t pinfo_dma_h;
2877 mraid_inquiry3_t *mraid_inq3;
2881 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2882 mbox = (mbox_t *)raw_mbox;
2885 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2886 * e.g., max channels, max commands etc.
2888 pinfo = dma_alloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2889 &pinfo_dma_h, GFP_KERNEL);
2890 if (pinfo == NULL) {
2891 con_log(CL_ANN, (KERN_WARNING
2892 "megaraid: out of memory, %s %d\n", __func__,
2898 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2899 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2901 raw_mbox[0] = FC_NEW_CONFIG;
2902 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2903 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2905 // Issue the command
2906 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2908 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2910 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2911 pinfo, pinfo_dma_h);
2917 * Collect information about state of each physical drive
2918 * attached to the controller. We will expose all the disks
2919 * which are not part of RAID
2921 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2922 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2923 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2927 * Get product info for information like number of channels,
2928 * maximum commands supported.
2930 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2931 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2933 raw_mbox[0] = FC_NEW_CONFIG;
2934 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
2936 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2938 con_log(CL_ANN, (KERN_WARNING
2939 "megaraid: product info failed\n"));
2941 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2942 pinfo, pinfo_dma_h);
2948 * Setup some parameters for host, as required by our caller
2950 adapter->max_channel = pinfo->nchannels;
2953 * we will export all the logical drives on a single channel.
2954 * Add 1 since inquires do not come for inititor ID
2956 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
2957 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
2960 * These are the maximum outstanding commands for the scsi-layer
2962 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
2964 memset(adapter->fw_version, 0, VERSION_SIZE);
2965 memset(adapter->bios_version, 0, VERSION_SIZE);
2967 memcpy(adapter->fw_version, pinfo->fw_version, 4);
2968 adapter->fw_version[4] = 0;
2970 memcpy(adapter->bios_version, pinfo->bios_version, 4);
2971 adapter->bios_version[4] = 0;
2973 con_log(CL_ANN, (KERN_NOTICE
2974 "megaraid: fw version:[%s] bios version:[%s]\n",
2975 adapter->fw_version, adapter->bios_version));
2977 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo,
2986 * megaraid_mbox_extended_cdb - check for support for extended CDBs
2987 * @adapter : soft state for the controller
2989 * This routine check whether the controller in question supports extended
2990 * ( > 10 bytes ) CDBs.
2993 megaraid_mbox_extended_cdb(adapter_t *adapter)
2996 uint8_t raw_mbox[sizeof(mbox_t)];
2999 mbox = (mbox_t *)raw_mbox;
3001 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3002 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3004 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3006 raw_mbox[0] = MAIN_MISC_OPCODE;
3007 raw_mbox[2] = SUPPORT_EXT_CDB;
3013 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3022 * megaraid_mbox_support_ha - Do we support clustering
3023 * @adapter : soft state for the controller
3024 * @init_id : ID of the initiator
3026 * Determine if the firmware supports clustering and the ID of the initiator.
3029 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3032 uint8_t raw_mbox[sizeof(mbox_t)];
3036 mbox = (mbox_t *)raw_mbox;
3038 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3040 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3042 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3044 raw_mbox[0] = GET_TARGET_ID;
3046 // Issue the command
3049 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3051 *init_id = *(uint8_t *)adapter->ibuf;
3053 con_log(CL_ANN, (KERN_INFO
3054 "megaraid: cluster firmware, initiator ID: %d\n",
3065 * megaraid_mbox_support_random_del - Do we support random deletion
3066 * @adapter : soft state for the controller
3068 * Determine if the firmware supports random deletion.
3069 * Return: 1 is operation supported, 0 otherwise
3072 megaraid_mbox_support_random_del(adapter_t *adapter)
3074 uint8_t raw_mbox[sizeof(mbox_t)];
3078 * Newer firmware on Dell CERC expect a different
3079 * random deletion handling, so disable it.
3081 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3082 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3083 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3084 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3085 (adapter->fw_version[0] > '6' ||
3086 (adapter->fw_version[0] == '6' &&
3087 adapter->fw_version[2] > '6') ||
3088 (adapter->fw_version[0] == '6'
3089 && adapter->fw_version[2] == '6'
3090 && adapter->fw_version[3] > '1'))) {
3091 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3095 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3097 raw_mbox[0] = FC_DEL_LOGDRV;
3098 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3100 // Issue the command
3102 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3104 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3114 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3115 * @adapter : soft state for the controller
3117 * Find out the maximum number of scatter-gather elements supported by the
3121 megaraid_mbox_get_max_sg(adapter_t *adapter)
3124 uint8_t raw_mbox[sizeof(mbox_t)];
3128 mbox = (mbox_t *)raw_mbox;
3130 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3132 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3134 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3136 raw_mbox[0] = MAIN_MISC_OPCODE;
3137 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3139 // Issue the command
3140 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3141 nsg = *(uint8_t *)adapter->ibuf;
3144 nsg = MBOX_DEFAULT_SG_SIZE;
3147 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3154 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3155 * @adapter : soft state for the controller
3157 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3158 * can be exported as regular SCSI channels.
3161 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3163 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3165 uint8_t raw_mbox[sizeof(mbox_t)];
3168 mbox = (mbox_t *)raw_mbox;
3170 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3172 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3174 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3176 raw_mbox[0] = CHNL_CLASS;
3177 raw_mbox[2] = GET_CHNL_CLASS;
3179 // Issue the command. If the command fails, all channels are RAID
3181 raid_dev->channel_class = 0xFF;
3182 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3183 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3191 * megaraid_mbox_flush_cache - flush adapter and disks cache
3192 * @adapter : soft state for the controller
3194 * Flush adapter cache followed by disks cache.
3197 megaraid_mbox_flush_cache(adapter_t *adapter)
3199 uint8_t raw_mbox[sizeof(mbox_t)];
3201 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3203 raw_mbox[0] = FLUSH_ADAPTER;
3205 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3206 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3209 raw_mbox[0] = FLUSH_SYSTEM;
3211 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3212 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3220 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3221 * @adapter : soft state for the controller
3223 * Clears the pending cmds in FW and reinits its RAID structs.
3226 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3229 uint8_t raw_mbox[sizeof(mbox_t)];
3230 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3235 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3239 mbox = raid_dev->mbox;
3241 /* Wait until mailbox is free */
3242 if (megaraid_busywait_mbox(raid_dev) != 0) {
3244 goto blocked_mailbox;
3247 /* Copy mailbox data into host structure */
3248 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3253 mbox->numstatus = 0;
3257 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3259 /* Wait for maximum 1 min for status to post.
3260 * If the Firmware SUPPORTS the ABOVE COMMAND,
3261 * mbox->cmd will be set to 0
3263 * the firmware will reject the command with
3264 * mbox->numstatus set to 1
3269 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3273 if (i > 1000 * 60) {
3278 if (mbox->numstatus == 1)
3279 status = 1; /*cmd not supported*/
3281 /* Check for interrupt line */
3282 dword = RDOUTDOOR(raid_dev);
3283 WROUTDOOR(raid_dev, dword);
3284 WRINDOOR(raid_dev,2);
3289 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3294 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3295 * @adapter : controller's soft state
3296 * @scb : SCB to be displayed
3298 * Diplay information about the given SCB iff the current debug level is
3302 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3305 struct scsi_cmnd *scp;
3311 ccb = (mbox_ccb_t *)scb->ccb;
3317 con_log(level, (KERN_NOTICE
3318 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3319 mbox->cmd, scb->sno));
3321 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3322 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3327 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3329 for (i = 0; i < scp->cmd_len; i++) {
3330 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3333 con_log(level, ("\n"));
3340 * megaraid_mbox_setup_device_map - manage device ids
3341 * @adapter : Driver's soft state
3343 * Manage the device ids to have an appropriate mapping between the kernel
3344 * scsi addresses and megaraid scsi and logical drive addresses. We export
3345 * scsi devices on their actual addresses, whereas the logical drives are
3346 * exported on a virtual scsi channel.
3349 megaraid_mbox_setup_device_map(adapter_t *adapter)
3355 * First fill the values on the logical drive channel
3357 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3358 adapter->device_ids[adapter->max_channel][t] =
3359 (t < adapter->init_id) ? t : t - 1;
3361 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3364 * Fill the values on the physical devices channels
3366 for (c = 0; c < adapter->max_channel; c++)
3367 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3368 adapter->device_ids[c][t] = (c << 8) | t;
3373 * END: internal commands library
3377 * START: Interface for the common management module
3379 * This is the module, which interfaces with the common management module to
3380 * provide support for ioctl and sysfs
3384 * megaraid_cmm_register - register with the management module
3385 * @adapter : HBA soft state
3387 * Register with the management module, which allows applications to issue
3388 * ioctl calls to the drivers. This interface is used by the management module
3389 * to setup sysfs support as well.
3392 megaraid_cmm_register(adapter_t *adapter)
3394 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3401 // Allocate memory for the base list of scb for management module.
3402 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3404 if (adapter->uscb_list == NULL) {
3405 con_log(CL_ANN, (KERN_WARNING
3406 "megaraid: out of memory, %s %d\n", __func__,
3412 // Initialize the synchronization parameters for resources for
3413 // commands for management module
3414 INIT_LIST_HEAD(&adapter->uscb_pool);
3416 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3420 // link all the packets. Note, CCB for commands, coming from the
3421 // commom management module, mailbox physical address are already
3422 // setup by it. We just need placeholder for that in our local command
3424 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3426 scb = adapter->uscb_list + i;
3427 ccb = raid_dev->uccb_list + i;
3429 scb->ccb = (caddr_t)ccb;
3430 ccb->mbox64 = raid_dev->umbox64 + i;
3431 ccb->mbox = &ccb->mbox64->mbox32;
3432 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3436 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3437 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3438 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3441 scb->state = SCB_FREE;
3442 scb->dma_direction = DMA_NONE;
3443 scb->dma_type = MRAID_DMA_NONE;
3444 scb->dev_channel = -1;
3445 scb->dev_target = -1;
3447 // put scb in the free pool
3448 list_add_tail(&scb->list, &adapter->uscb_pool);
3451 adp.unique_id = adapter->unique_id;
3452 adp.drvr_type = DRVRTYPE_MBOX;
3453 adp.drvr_data = (unsigned long)adapter;
3454 adp.pdev = adapter->pdev;
3455 adp.issue_uioc = megaraid_mbox_mm_handler;
3456 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3457 adp.max_kioc = MBOX_MAX_USER_CMDS;
3459 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3461 con_log(CL_ANN, (KERN_WARNING
3462 "megaraid mbox: did not register with CMM\n"));
3464 kfree(adapter->uscb_list);
3472 * megaraid_cmm_unregister - un-register with the management module
3473 * @adapter : HBA soft state
3475 * Un-register with the management module.
3476 * FIXME: mgmt module must return failure for unregister if it has pending
3480 megaraid_cmm_unregister(adapter_t *adapter)
3482 kfree(adapter->uscb_list);
3483 mraid_mm_unregister_adp(adapter->unique_id);
3489 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3490 * @drvr_data : LLD specific data
3491 * @kioc : CMM interface packet
3492 * @action : command action
3494 * This routine is invoked whenever the Common Management Module (CMM) has a
3495 * command for us. The 'action' parameter specifies if this is a new command
3499 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3503 if (action != IOCTL_ISSUE) {
3504 con_log(CL_ANN, (KERN_WARNING
3505 "megaraid: unsupported management action:%#2x\n",
3510 adapter = (adapter_t *)drvr_data;
3512 // make sure this adapter is not being detached right now.
3513 if (atomic_read(&adapter->being_detached)) {
3514 con_log(CL_ANN, (KERN_WARNING
3515 "megaraid: reject management request, detaching\n"));
3519 switch (kioc->opcode) {
3523 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3524 (unsigned long)kioc->buf_vaddr);
3528 return kioc->status;
3532 return megaraid_mbox_mm_command(adapter, kioc);
3535 kioc->status = (-EINVAL);
3540 return 0; // not reached
3544 * megaraid_mbox_mm_command - issues commands routed through CMM
3545 * @adapter : HBA soft state
3546 * @kioc : management command packet
3548 * Issues commands, which are routed through the management module.
3551 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3553 struct list_head *head = &adapter->uscb_pool;
3558 unsigned long flags;
3560 // detach one scb from free pool
3561 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3563 if (list_empty(head)) { // should never happen because of CMM
3565 con_log(CL_ANN, (KERN_WARNING
3566 "megaraid mbox: bug in cmm handler, lost resources\n"));
3568 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3573 scb = list_entry(head->next, scb_t, list);
3574 list_del_init(&scb->list);
3576 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3578 scb->state = SCB_ACTIVE;
3579 scb->dma_type = MRAID_DMA_NONE;
3580 scb->dma_direction = DMA_NONE;
3582 ccb = (mbox_ccb_t *)scb->ccb;
3583 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3584 raw_mbox = (uint8_t *)&mbox64->mbox32;
3586 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3588 scb->gp = (unsigned long)kioc;
3591 * If it is a logdrv random delete operation, we have to wait till
3592 * there are no outstanding cmds at the fw and then issue it directly
3594 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3596 if (wait_till_fw_empty(adapter)) {
3597 con_log(CL_ANN, (KERN_NOTICE
3598 "megaraid mbox: LD delete, timed out\n"));
3600 kioc->status = -ETIME;
3604 megaraid_mbox_mm_done(adapter, scb);
3609 INIT_LIST_HEAD(&scb->list);
3611 scb->state = SCB_ISSUED;
3612 if (mbox_post_cmd(adapter, scb) != 0) {
3614 con_log(CL_ANN, (KERN_NOTICE
3615 "megaraid mbox: LD delete, mailbox busy\n"));
3617 kioc->status = -EBUSY;
3621 megaraid_mbox_mm_done(adapter, scb);
3629 // put the command on the pending list and execute
3630 megaraid_mbox_runpendq(adapter, scb);
3637 wait_till_fw_empty(adapter_t *adapter)
3639 unsigned long flags = 0;
3644 * Set the quiescent flag to stop issuing cmds to FW.
3646 spin_lock_irqsave(&adapter->lock, flags);
3647 adapter->quiescent++;
3648 spin_unlock_irqrestore(&adapter->lock, flags);
3651 * Wait till there are no more cmds outstanding at FW. Try for at most
3654 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3655 con_log(CL_DLEVEL1, (KERN_INFO
3656 "megaraid: FW has %d pending commands\n",
3657 adapter->outstanding_cmds));
3662 return adapter->outstanding_cmds;
3667 * megaraid_mbox_mm_done - callback for CMM commands
3668 * @adapter : HBA soft state
3669 * @scb : completed command
3671 * Callback routine for internal commands originated from the management
3675 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3680 unsigned long flags;
3682 kioc = (uioc_t *)scb->gp;
3683 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3684 mbox64->mbox32.status = scb->status;
3685 raw_mbox = (uint8_t *)&mbox64->mbox32;
3688 // put scb in the free pool
3689 scb->state = SCB_FREE;
3692 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3694 list_add(&scb->list, &adapter->uscb_pool);
3696 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3698 // if a delete logical drive operation succeeded, restart the
3700 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3702 adapter->quiescent--;
3704 megaraid_mbox_runpendq(adapter, NULL);
3714 * gather_hbainfo - HBA characteristics for the applications
3715 * @adapter : HBA soft state
3716 * @hinfo : pointer to the caller's host info strucuture
3719 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3721 hinfo->pci_vendor_id = adapter->pdev->vendor;
3722 hinfo->pci_device_id = adapter->pdev->device;
3723 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3724 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3726 hinfo->pci_bus = adapter->pdev->bus->number;
3727 hinfo->pci_dev_fn = adapter->pdev->devfn;
3728 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3729 hinfo->irq = adapter->host->irq;
3730 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3732 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3733 hinfo->host_no = adapter->host->host_no;
3739 * END: Interface for the common management module
3745 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3746 * @adapter : controller's soft state
3748 * Allocate packets required to issue FW calls whenever the sysfs attributes
3749 * are read. These attributes would require up-to-date information from the
3750 * FW. Also set up resources for mutual exclusion to share these resources and
3753 * Return 0 on success.
3754 * Return -ERROR_CODE on failure.
3757 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3759 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3762 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3764 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3766 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev,
3767 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL);
3769 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3770 !raid_dev->sysfs_buffer) {
3772 con_log(CL_ANN, (KERN_WARNING
3773 "megaraid: out of memory, %s %d\n", __func__,
3778 megaraid_sysfs_free_resources(adapter);
3781 mutex_init(&raid_dev->sysfs_mtx);
3783 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3790 * megaraid_sysfs_free_resources - free sysfs related resources
3791 * @adapter : controller's soft state
3793 * Free packets allocated for sysfs FW commands
3796 megaraid_sysfs_free_resources(adapter_t *adapter)
3798 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3800 kfree(raid_dev->sysfs_uioc);
3801 kfree(raid_dev->sysfs_mbox64);
3803 if (raid_dev->sysfs_buffer) {
3804 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE,
3805 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3811 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3812 * @uioc : completed packet
3814 * Callback routine called in the ISR/tasklet context for get ldmap call
3817 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3819 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3820 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3824 wake_up(&raid_dev->sysfs_wait_q);
3828 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3829 * @t : timed out timer
3831 * Timeout routine to recover and return to application, in case the adapter
3832 * has stopped responding. A timeout of 60 seconds for this command seems like
3836 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
3838 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3839 uioc_t *uioc = timeout->uioc;
3840 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3841 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3843 uioc->status = -ETIME;
3845 wake_up(&raid_dev->sysfs_wait_q);
3850 * megaraid_sysfs_get_ldmap - get update logical drive map
3851 * @adapter : controller's soft state
3853 * This routine will be called whenever user reads the logical drive
3854 * attributes, go get the current logical drive mapping table from the
3855 * firmware. We use the management API's to issue commands to the controller.
3857 * NOTE: The commands issuance functionality is not generalized and
3858 * implemented in context of "get ld map" command only. If required, the
3859 * command issuance logical can be trivially pulled out and implemented as a
3860 * standalone library. For now, this should suffice since there is no other
3861 * user of this interface.
3863 * Return 0 on success.
3864 * Return -1 on failure.
3867 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3869 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3874 struct uioc_timeout timeout;
3879 * Allow only one read at a time to go through the sysfs attributes
3881 mutex_lock(&raid_dev->sysfs_mtx);
3883 uioc = raid_dev->sysfs_uioc;
3884 mbox64 = raid_dev->sysfs_mbox64;
3885 ldmap = raid_dev->sysfs_buffer;
3887 memset(uioc, 0, sizeof(uioc_t));
3888 memset(mbox64, 0, sizeof(mbox64_t));
3889 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3891 mbox = &mbox64->mbox32;
3892 raw_mbox = (char *)mbox;
3893 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3894 uioc->buf_vaddr = (caddr_t)adapter;
3895 uioc->status = -ENODATA;
3896 uioc->done = megaraid_sysfs_get_ldmap_done;
3899 * Prepare the mailbox packet to get the current logical drive mapping
3902 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3904 raw_mbox[0] = FC_DEL_LOGDRV;
3905 raw_mbox[2] = OP_GET_LDID_MAP;
3908 * Setup a timer to recover from a non-responding controller
3910 timeout.uioc = uioc;
3911 timer_setup_on_stack(&timeout.timer,
3912 megaraid_sysfs_get_ldmap_timeout, 0);
3914 timeout.timer.expires = jiffies + 60 * HZ;
3915 add_timer(&timeout.timer);
3918 * Send the command to the firmware
3920 rval = megaraid_mbox_mm_command(adapter, uioc);
3922 if (rval == 0) { // command successfully issued
3923 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3926 * Check if the command timed out
3928 if (uioc->status == -ETIME) {
3929 con_log(CL_ANN, (KERN_NOTICE
3930 "megaraid: sysfs get ld map timed out\n"));
3935 rval = mbox->status;
3939 memcpy(raid_dev->curr_ldmap, ldmap,
3940 sizeof(raid_dev->curr_ldmap));
3943 con_log(CL_ANN, (KERN_NOTICE
3944 "megaraid: get ld map failed with %x\n", rval));
3948 con_log(CL_ANN, (KERN_NOTICE
3949 "megaraid: could not issue ldmap command:%x\n", rval));
3953 del_timer_sync(&timeout.timer);
3954 destroy_timer_on_stack(&timeout.timer);
3956 mutex_unlock(&raid_dev->sysfs_mtx);
3963 * megaraid_mbox_app_hndl_show - display application handle for this adapter
3964 * @dev : class device object representation for the host
3965 * @attr : device attribute (unused)
3966 * @buf : buffer to send data to
3968 * Display the handle used by the applications while executing management
3969 * tasks on the adapter. We invoke a management module API to get the adapter
3970 * handle, since we do not interface with applications directly.
3973 megaraid_mbox_app_hndl_show(struct device *dev, struct device_attribute *attr, char *buf)
3975 struct Scsi_Host *shost = class_to_shost(dev);
3976 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
3979 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
3981 return snprintf(buf, 8, "%u\n", app_hndl);
3986 * megaraid_mbox_ld_show - display the logical drive number for this device
3987 * @dev : device object representation for the scsi device
3988 * @attr : device attribute to show
3989 * @buf : buffer to send data to
3991 * Display the logical drive number for the device in question, if it a valid
3992 * logical drive. For physical devices, "-1" is returned.
3994 * The logical drive number is displayed in following format:
3996 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
3998 * <int> <int> <int> <int>
4001 megaraid_mbox_ld_show(struct device *dev, struct device_attribute *attr, char *buf)
4003 struct scsi_device *sdev = to_scsi_device(dev);
4004 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4005 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4007 int logical_drv = -1;
4009 uint32_t app_hndl = 0;
4014 if (raid_dev->random_del_supported &&
4015 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4017 rval = megaraid_sysfs_get_ldmap(adapter);
4020 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4022 mapped_sdev_id = sdev->id;
4024 if (sdev->id > adapter->init_id) {
4025 mapped_sdev_id -= 1;
4028 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4034 ldid_map = raid_dev->curr_ldmap[i];
4036 app_hndl = mraid_mm_adapter_app_handle(
4037 adapter->unique_id);
4044 con_log(CL_ANN, (KERN_NOTICE
4045 "megaraid: sysfs get ld map failed: %x\n",
4050 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4051 ldid_map, app_hndl);
4056 * END: Mailbox Low Level Driver
4058 module_init(megaraid_init);
4059 module_exit(megaraid_exit);