3 * Linux MegaRAID device driver
5 * Copyright (c) 2003-2004 LSI Logic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * FILE : megaraid_mbox.c
13 * Version : v2.20.5.1 (Nov 16 2006)
16 * Atul Mukker <Atul.Mukker@lsi.com>
17 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
18 * Manoj Jose <Manoj.Jose@lsi.com>
21 * List of supported controllers
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
25 * Dell PERC3/QC 101E 1960 1028 0471
26 * Dell PERC3/DC 101E 1960 1028 0493
27 * Dell PERC3/SC 101E 1960 1028 0475
28 * Dell PERC3/Di 1028 1960 1028 0123
29 * Dell PERC4/SC 1000 1960 1028 0520
30 * Dell PERC4/DC 1000 1960 1028 0518
31 * Dell PERC4/QC 1000 0407 1028 0531
32 * Dell PERC4/Di 1028 000F 1028 014A
33 * Dell PERC 4e/Si 1028 0013 1028 016c
34 * Dell PERC 4e/Di 1028 0013 1028 016d
35 * Dell PERC 4e/Di 1028 0013 1028 016e
36 * Dell PERC 4e/Di 1028 0013 1028 016f
37 * Dell PERC 4e/Di 1028 0013 1028 0170
38 * Dell PERC 4e/DC 1000 0408 1028 0002
39 * Dell PERC 4e/SC 1000 0408 1028 0001
42 * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
43 * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
44 * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
45 * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
46 * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
47 * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
48 * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
49 * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
50 * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
51 * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
52 * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
53 * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
55 * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
56 * INTEL RAID Controller SRCS16 1000 1960 8086 0523
57 * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
58 * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
59 * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
60 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
61 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
62 * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
64 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
66 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
68 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
70 * For history of changes, see Documentation/scsi/ChangeLog.megaraid
73 #include <linux/slab.h>
74 #include <linux/module.h>
75 #include "megaraid_mbox.h"
77 static int megaraid_init(void);
78 static void megaraid_exit(void);
80 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
81 static void megaraid_detach_one(struct pci_dev *);
82 static void megaraid_mbox_shutdown(struct pci_dev *);
84 static int megaraid_io_attach(adapter_t *);
85 static void megaraid_io_detach(adapter_t *);
87 static int megaraid_init_mbox(adapter_t *);
88 static void megaraid_fini_mbox(adapter_t *);
90 static int megaraid_alloc_cmd_packets(adapter_t *);
91 static void megaraid_free_cmd_packets(adapter_t *);
93 static int megaraid_mbox_setup_dma_pools(adapter_t *);
94 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
96 static int megaraid_sysfs_alloc_resources(adapter_t *);
97 static void megaraid_sysfs_free_resources(adapter_t *);
99 static int megaraid_abort_handler(struct scsi_cmnd *);
100 static int megaraid_reset_handler(struct scsi_cmnd *);
102 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
103 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
104 static int megaraid_busywait_mbox(mraid_device_t *);
105 static int megaraid_mbox_product_info(adapter_t *);
106 static int megaraid_mbox_extended_cdb(adapter_t *);
107 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
108 static int megaraid_mbox_support_random_del(adapter_t *);
109 static int megaraid_mbox_get_max_sg(adapter_t *);
110 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
111 static void megaraid_mbox_flush_cache(adapter_t *);
112 static int megaraid_mbox_fire_sync_cmd(adapter_t *);
114 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
115 static void megaraid_mbox_setup_device_map(adapter_t *);
117 static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
118 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
119 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
120 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
122 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
125 static irqreturn_t megaraid_isr(int, void *);
127 static void megaraid_mbox_dpc(unsigned long);
129 static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
130 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
132 static int megaraid_cmm_register(adapter_t *);
133 static int megaraid_cmm_unregister(adapter_t *);
134 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
135 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
136 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
137 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
138 static int wait_till_fw_empty(adapter_t *);
142 MODULE_AUTHOR("megaraidlinux@lsi.com");
143 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
144 MODULE_LICENSE("GPL");
145 MODULE_VERSION(MEGARAID_VERSION);
148 * ### modules parameters for driver ###
152 * Set to enable driver to expose unconfigured disk to kernel
154 static int megaraid_expose_unconf_disks = 0;
155 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
156 MODULE_PARM_DESC(unconf_disks,
157 "Set to expose unconfigured disks to kernel (default=0)");
160 * driver wait time if the adapter's mailbox is busy
162 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
163 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
164 MODULE_PARM_DESC(busy_wait,
165 "Max wait for mailbox in microseconds if busy (default=10)");
168 * number of sectors per IO command
170 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
171 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
172 MODULE_PARM_DESC(max_sectors,
173 "Maximum number of sectors per IO command (default=128)");
176 * number of commands per logical unit
178 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
179 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
180 MODULE_PARM_DESC(cmd_per_lun,
181 "Maximum number of commands per logical unit (default=64)");
185 * Fast driver load option, skip scanning for physical devices during load.
186 * This would result in non-disk devices being skipped during driver load
187 * time. These can be later added though, using /proc/scsi/scsi
189 static unsigned int megaraid_fast_load = 0;
190 module_param_named(fast_load, megaraid_fast_load, int, 0);
191 MODULE_PARM_DESC(fast_load,
192 "Faster loading of the driver, skips physical devices! (default=0)");
196 * mraid_debug level - threshold for amount of information to be displayed by
197 * the driver. This level can be changed through modules parameters, ioctl or
198 * sysfs/proc interface. By default, print the announcement messages only.
200 int mraid_debug_level = CL_ANN;
201 module_param_named(debug_level, mraid_debug_level, int, 0);
202 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
205 * PCI table for all supported controllers.
207 static struct pci_device_id pci_id_table_g[] = {
210 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
212 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
215 PCI_VENDOR_ID_LSI_LOGIC,
216 PCI_DEVICE_ID_PERC4_SC,
218 PCI_SUBSYS_ID_PERC4_SC,
221 PCI_VENDOR_ID_LSI_LOGIC,
222 PCI_DEVICE_ID_PERC4_DC,
224 PCI_SUBSYS_ID_PERC4_DC,
227 PCI_VENDOR_ID_LSI_LOGIC,
234 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
236 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
240 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
242 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
246 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
248 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
252 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
254 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
258 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
260 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
264 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
266 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
269 PCI_VENDOR_ID_LSI_LOGIC,
270 PCI_DEVICE_ID_DOBSON,
276 PCI_DEVICE_ID_AMI_MEGARAID3,
281 PCI_VENDOR_ID_LSI_LOGIC,
282 PCI_DEVICE_ID_AMI_MEGARAID3,
287 PCI_VENDOR_ID_LSI_LOGIC,
288 PCI_DEVICE_ID_LINDSAY,
292 {0} /* Terminating entry */
294 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
297 static struct pci_driver megaraid_pci_driver = {
299 .id_table = pci_id_table_g,
300 .probe = megaraid_probe_one,
301 .remove = megaraid_detach_one,
302 .shutdown = megaraid_mbox_shutdown,
307 // definitions for the device attributes for exporting logical drive number
308 // for a scsi address (Host, Channel, Id, Lun)
310 DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
313 // Host template initializer for megaraid mbox sysfs device attributes
314 static struct device_attribute *megaraid_shost_attrs[] = {
315 &dev_attr_megaraid_mbox_app_hndl,
320 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
322 // Host template initializer for megaraid mbox sysfs device attributes
323 static struct device_attribute *megaraid_sdev_attrs[] = {
324 &dev_attr_megaraid_mbox_ld,
329 * Scsi host template for megaraid unified driver
331 static struct scsi_host_template megaraid_template_g = {
332 .module = THIS_MODULE,
333 .name = "LSI Logic MegaRAID driver",
334 .proc_name = "megaraid",
335 .queuecommand = megaraid_queue_command,
336 .eh_abort_handler = megaraid_abort_handler,
337 .eh_host_reset_handler = megaraid_reset_handler,
338 .change_queue_depth = scsi_change_queue_depth,
340 .sdev_attrs = megaraid_sdev_attrs,
341 .shost_attrs = megaraid_shost_attrs,
346 * megaraid_init - module load hook
348 * We register ourselves as hotplug enabled module and let PCI subsystem
349 * discover our adapters.
356 // Announce the driver version
357 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
358 MEGARAID_EXT_VERSION));
360 // check validity of module parameters
361 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
363 con_log(CL_ANN, (KERN_WARNING
364 "megaraid mailbox: max commands per lun reset to %d\n",
365 MBOX_MAX_SCSI_CMDS));
367 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
371 // register as a PCI hot-plug driver module
372 rval = pci_register_driver(&megaraid_pci_driver);
374 con_log(CL_ANN, (KERN_WARNING
375 "megaraid: could not register hotplug support.\n"));
383 * megaraid_exit - driver unload entry point
385 * We simply unwrap the megaraid_init routine here.
390 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
392 // unregister as PCI hotplug driver
393 pci_unregister_driver(&megaraid_pci_driver);
400 * megaraid_probe_one - PCI hotplug entry point
401 * @pdev : handle to this controller's PCI configuration space
402 * @id : pci device id of the class of controllers
404 * This routine should be called whenever a new adapter is detected by the
405 * PCI hotplug susbsystem.
408 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
413 // detected a new controller
414 con_log(CL_ANN, (KERN_INFO
415 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
416 pdev->vendor, pdev->device, pdev->subsystem_vendor,
417 pdev->subsystem_device));
419 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
420 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
422 if (pci_enable_device(pdev)) {
423 con_log(CL_ANN, (KERN_WARNING
424 "megaraid: pci_enable_device failed\n"));
429 // Enable bus-mastering on this controller
430 pci_set_master(pdev);
432 // Allocate the per driver initialization structure
433 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
435 if (adapter == NULL) {
436 con_log(CL_ANN, (KERN_WARNING
437 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
443 // set up PCI related soft state and other pre-known parameters
444 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
445 adapter->irq = pdev->irq;
446 adapter->pdev = pdev;
448 atomic_set(&adapter->being_detached, 0);
450 // Setup the default DMA mask. This would be changed later on
451 // depending on hardware capabilities
452 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) {
453 con_log(CL_ANN, (KERN_WARNING
454 "megaraid: dma_set_mask failed:%d\n", __LINE__));
456 goto out_free_adapter;
460 // Initialize the synchronization lock for kernel and LLD
461 spin_lock_init(&adapter->lock);
463 // Initialize the command queues: the list of free SCBs and the list
465 INIT_LIST_HEAD(&adapter->kscb_pool);
466 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
468 INIT_LIST_HEAD(&adapter->pend_list);
469 spin_lock_init(PENDING_LIST_LOCK(adapter));
471 INIT_LIST_HEAD(&adapter->completed_list);
472 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
475 // Start the mailbox based controller
476 if (megaraid_init_mbox(adapter) != 0) {
477 con_log(CL_ANN, (KERN_WARNING
478 "megaraid: mailbox adapter did not initialize\n"));
480 goto out_free_adapter;
483 // Register with LSI Common Management Module
484 if (megaraid_cmm_register(adapter) != 0) {
486 con_log(CL_ANN, (KERN_WARNING
487 "megaraid: could not register with management module\n"));
492 // setup adapter handle in PCI soft state
493 pci_set_drvdata(pdev, adapter);
495 // attach with scsi mid-layer
496 if (megaraid_io_attach(adapter) != 0) {
498 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
506 megaraid_cmm_unregister(adapter);
508 megaraid_fini_mbox(adapter);
512 pci_disable_device(pdev);
519 * megaraid_detach_one - release framework resources and call LLD release routine
520 * @pdev : handle for our PCI configuration space
522 * This routine is called during driver unload. We free all the allocated
523 * resources and call the corresponding LLD so that it can also release all
526 * This routine is also called from the PCI hotplug system.
529 megaraid_detach_one(struct pci_dev *pdev)
532 struct Scsi_Host *host;
535 // Start a rollback on this adapter
536 adapter = pci_get_drvdata(pdev);
539 con_log(CL_ANN, (KERN_CRIT
540 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
541 pdev->vendor, pdev->device, pdev->subsystem_vendor,
542 pdev->subsystem_device));
547 con_log(CL_ANN, (KERN_NOTICE
548 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
549 pdev->vendor, pdev->device, pdev->subsystem_vendor,
550 pdev->subsystem_device));
554 host = adapter->host;
556 // do not allow any more requests from the management module for this
558 // FIXME: How do we account for the request which might still be
560 atomic_set(&adapter->being_detached, 1);
562 // detach from the IO sub-system
563 megaraid_io_detach(adapter);
565 // Unregister from common management module
567 // FIXME: this must return success or failure for conditions if there
568 // is a command pending with LLD or not.
569 megaraid_cmm_unregister(adapter);
571 // finalize the mailbox based controller and release all resources
572 megaraid_fini_mbox(adapter);
578 pci_disable_device(pdev);
585 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
586 * @pdev : generic driver model device
588 * Shutdown notification, perform flush cache.
591 megaraid_mbox_shutdown(struct pci_dev *pdev)
593 adapter_t *adapter = pci_get_drvdata(pdev);
597 con_log(CL_ANN, (KERN_WARNING
598 "megaraid: null device in shutdown\n"));
603 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
606 megaraid_mbox_flush_cache(adapter);
608 con_log(CL_ANN, ("done\n"));
613 * megaraid_io_attach - attach a device with the IO subsystem
614 * @adapter : controller's soft state
616 * Attach this device with the IO subsystem.
619 megaraid_io_attach(adapter_t *adapter)
621 struct Scsi_Host *host;
623 // Initialize SCSI Host structure
624 host = scsi_host_alloc(&megaraid_template_g, 8);
626 con_log(CL_ANN, (KERN_WARNING
627 "megaraid mbox: scsi_register failed\n"));
632 SCSIHOST2ADAP(host) = (caddr_t)adapter;
633 adapter->host = host;
635 host->irq = adapter->irq;
636 host->unique_id = adapter->unique_id;
637 host->can_queue = adapter->max_cmds;
638 host->this_id = adapter->init_id;
639 host->sg_tablesize = adapter->sglen;
640 host->max_sectors = adapter->max_sectors;
641 host->cmd_per_lun = adapter->cmd_per_lun;
642 host->max_channel = adapter->max_channel;
643 host->max_id = adapter->max_target;
644 host->max_lun = adapter->max_lun;
647 // notify mid-layer about the new controller
648 if (scsi_add_host(host, &adapter->pdev->dev)) {
650 con_log(CL_ANN, (KERN_WARNING
651 "megaraid mbox: scsi_add_host failed\n"));
658 scsi_scan_host(host);
665 * megaraid_io_detach - detach a device from the IO subsystem
666 * @adapter : controller's soft state
668 * Detach this device from the IO subsystem.
671 megaraid_io_detach(adapter_t *adapter)
673 struct Scsi_Host *host;
675 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
677 host = adapter->host;
679 scsi_remove_host(host);
686 * START: Mailbox Low Level Driver
688 * This is section specific to the single mailbox based controllers
692 * megaraid_init_mbox - initialize controller
693 * @adapter : our soft state
695 * - Allocate 16-byte aligned mailbox memory for firmware handshake
696 * - Allocate controller's memory resources
697 * - Find out all initialization data
698 * - Allocate memory required for all the commands
699 * - Use internal library of FW routines, build up complete soft state
702 megaraid_init_mbox(adapter_t *adapter)
704 struct pci_dev *pdev;
705 mraid_device_t *raid_dev;
710 adapter->ito = MBOX_TIMEOUT;
711 pdev = adapter->pdev;
714 * Allocate and initialize the init data structure for mailbox
717 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
718 if (raid_dev == NULL) return -1;
722 * Attach the adapter soft state to raid device soft state
724 adapter->raid_device = (caddr_t)raid_dev;
725 raid_dev->fast_load = megaraid_fast_load;
729 raid_dev->baseport = pci_resource_start(pdev, 0);
731 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
733 con_log(CL_ANN, (KERN_WARNING
734 "megaraid: mem region busy\n"));
736 goto out_free_raid_dev;
739 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
741 if (!raid_dev->baseaddr) {
743 con_log(CL_ANN, (KERN_WARNING
744 "megaraid: could not map hba memory\n") );
746 goto out_release_regions;
749 /* initialize the mutual exclusion lock for the mailbox */
750 spin_lock_init(&raid_dev->mailbox_lock);
752 /* allocate memory required for commands */
753 if (megaraid_alloc_cmd_packets(adapter) != 0)
757 * Issue SYNC cmd to flush the pending cmds in the adapter
758 * and initialize its internal state
761 if (megaraid_mbox_fire_sync_cmd(adapter))
762 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
765 * Setup the rest of the soft state using the library of
769 /* request IRQ and register the interrupt service routine */
770 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
773 con_log(CL_ANN, (KERN_WARNING
774 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
780 if (megaraid_mbox_product_info(adapter) != 0)
783 // Do we support extended CDBs
784 adapter->max_cdb_sz = 10;
785 if (megaraid_mbox_extended_cdb(adapter) == 0) {
786 adapter->max_cdb_sz = 16;
790 * Do we support cluster environment, if we do, what is the initiator
792 * NOTE: In a non-cluster aware firmware environment, the LLD should
793 * return 7 as initiator id.
796 adapter->init_id = -1;
797 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
802 * Prepare the device ids array to have the mapping between the kernel
803 * device address and megaraid device address.
804 * We export the physical devices on their actual addresses. The
805 * logical drives are exported on a virtual SCSI channel
807 megaraid_mbox_setup_device_map(adapter);
809 // If the firmware supports random deletion, update the device id map
810 if (megaraid_mbox_support_random_del(adapter)) {
812 // Change the logical drives numbers in device_ids array one
813 // slot in device_ids is reserved for target id, that's why
815 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
816 adapter->device_ids[adapter->max_channel][i] += 0x80;
818 adapter->device_ids[adapter->max_channel][adapter->init_id] =
821 raid_dev->random_del_supported = 1;
825 * find out the maximum number of scatter-gather elements supported by
828 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
830 // enumerate RAID and SCSI channels so that all devices on SCSI
831 // channels can later be exported, including disk devices
832 megaraid_mbox_enum_raid_scsi(adapter);
835 * Other parameters required by upper layer
837 * maximum number of sectors per IO command
839 adapter->max_sectors = megaraid_max_sectors;
842 * number of queued commands per LUN.
844 adapter->cmd_per_lun = megaraid_cmd_per_lun;
847 * Allocate resources required to issue FW calls, when sysfs is
850 if (megaraid_sysfs_alloc_resources(adapter) != 0)
853 // Set the DMA mask to 64-bit. All supported controllers as capable of
855 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
857 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
858 ((adapter->pdev->subsystem_device !=
859 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
860 (adapter->pdev->subsystem_device !=
861 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
862 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
863 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
864 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
865 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
866 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
867 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
868 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
869 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
870 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
871 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
872 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) {
873 con_log(CL_ANN, (KERN_WARNING
874 "megaraid: DMA mask for 64-bit failed\n"));
876 if (dma_set_mask(&adapter->pdev->dev,
878 con_log(CL_ANN, (KERN_WARNING
879 "megaraid: 32-bit DMA mask failed\n"));
880 goto out_free_sysfs_res;
885 // setup tasklet for DPC
886 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
887 (unsigned long)adapter);
889 con_log(CL_DLEVEL1, (KERN_INFO
890 "megaraid mbox hba successfully initialized\n"));
895 megaraid_sysfs_free_resources(adapter);
897 free_irq(adapter->irq, adapter);
899 megaraid_free_cmd_packets(adapter);
901 iounmap(raid_dev->baseaddr);
903 pci_release_regions(pdev);
912 * megaraid_fini_mbox - undo controller initialization
913 * @adapter : our soft state
916 megaraid_fini_mbox(adapter_t *adapter)
918 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
921 megaraid_mbox_flush_cache(adapter);
923 tasklet_kill(&adapter->dpc_h);
925 megaraid_sysfs_free_resources(adapter);
927 megaraid_free_cmd_packets(adapter);
929 free_irq(adapter->irq, adapter);
931 iounmap(raid_dev->baseaddr);
933 pci_release_regions(adapter->pdev);
942 * megaraid_alloc_cmd_packets - allocate shared mailbox
943 * @adapter : soft state of the raid controller
945 * Allocate and align the shared mailbox. This mailbox is used to issue
946 * all the commands. For IO based controllers, the mailbox is also registered
947 * with the FW. Allocate memory for all commands as well.
948 * This is our big allocator.
951 megaraid_alloc_cmd_packets(adapter_t *adapter)
953 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
954 struct pci_dev *pdev;
958 struct mraid_pci_blk *epthru_pci_blk;
959 struct mraid_pci_blk *sg_pci_blk;
960 struct mraid_pci_blk *mbox_pci_blk;
963 pdev = adapter->pdev;
967 * Allocate the common 16-byte aligned memory for the handshake
970 raid_dev->una_mbox64 = dma_zalloc_coherent(&adapter->pdev->dev,
971 sizeof(mbox64_t), &raid_dev->una_mbox64_dma,
974 if (!raid_dev->una_mbox64) {
975 con_log(CL_ANN, (KERN_WARNING
976 "megaraid: out of memory, %s %d\n", __func__,
982 * Align the mailbox at 16-byte boundary
984 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
986 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
989 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
991 align = ((void *)raid_dev->mbox -
992 ((void *)&raid_dev->una_mbox64->mbox32));
994 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
997 // Allocate memory for commands issued internally
998 adapter->ibuf = dma_zalloc_coherent(&pdev->dev, MBOX_IBUF_SIZE,
999 &adapter->ibuf_dma_h, GFP_KERNEL);
1000 if (!adapter->ibuf) {
1002 con_log(CL_ANN, (KERN_WARNING
1003 "megaraid: out of memory, %s %d\n", __func__,
1006 goto out_free_common_mbox;
1009 // Allocate memory for our SCSI Command Blocks and their associated
1013 * Allocate memory for the base list of scb. Later allocate memory for
1014 * CCBs and embedded components of each CCB and point the pointers in
1015 * scb to the allocated components
1016 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1017 * since the calling routine does not yet know the number of available
1020 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1022 if (adapter->kscb_list == NULL) {
1023 con_log(CL_ANN, (KERN_WARNING
1024 "megaraid: out of memory, %s %d\n", __func__,
1029 // memory allocation for our command packets
1030 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1031 con_log(CL_ANN, (KERN_WARNING
1032 "megaraid: out of memory, %s %d\n", __func__,
1034 goto out_free_scb_list;
1037 // Adjust the scb pointers and link in the free pool
1038 epthru_pci_blk = raid_dev->epthru_pool;
1039 sg_pci_blk = raid_dev->sg_pool;
1040 mbox_pci_blk = raid_dev->mbox_pool;
1042 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1043 scb = adapter->kscb_list + i;
1044 ccb = raid_dev->ccb_list + i;
1046 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1047 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1048 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1049 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1051 // make sure the mailbox is aligned properly
1052 if (ccb->mbox_dma_h & 0x0F) {
1053 con_log(CL_ANN, (KERN_CRIT
1054 "megaraid mbox: not aligned on 16-bytes\n"));
1056 goto out_teardown_dma_pools;
1059 ccb->epthru = (mraid_epassthru_t *)
1060 epthru_pci_blk[i].vaddr;
1061 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1062 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1063 ccb->pthru_dma_h = ccb->epthru_dma_h;
1066 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1067 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1068 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1070 scb->ccb = (caddr_t)ccb;
1073 scb->sno = i; // command index
1076 scb->state = SCB_FREE;
1077 scb->dma_direction = DMA_NONE;
1078 scb->dma_type = MRAID_DMA_NONE;
1079 scb->dev_channel = -1;
1080 scb->dev_target = -1;
1082 // put scb in the free pool
1083 list_add_tail(&scb->list, &adapter->kscb_pool);
1088 out_teardown_dma_pools:
1089 megaraid_mbox_teardown_dma_pools(adapter);
1091 kfree(adapter->kscb_list);
1093 dma_free_coherent(&pdev->dev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1094 adapter->ibuf_dma_h);
1095 out_free_common_mbox:
1096 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1097 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1104 * megaraid_free_cmd_packets - free memory
1105 * @adapter : soft state of the raid controller
1107 * Release memory resources allocated for commands.
1110 megaraid_free_cmd_packets(adapter_t *adapter)
1112 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1114 megaraid_mbox_teardown_dma_pools(adapter);
1116 kfree(adapter->kscb_list);
1118 dma_free_coherent(&adapter->pdev->dev, MBOX_IBUF_SIZE,
1119 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1121 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1122 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1128 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1129 * @adapter : HBA soft state
1131 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1132 * and scatter-gather lists.
1135 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1137 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1138 struct mraid_pci_blk *epthru_pci_blk;
1139 struct mraid_pci_blk *sg_pci_blk;
1140 struct mraid_pci_blk *mbox_pci_blk;
1145 // Allocate memory for 16-bytes aligned mailboxes
1146 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
1147 &adapter->pdev->dev,
1148 sizeof(mbox64_t) + 16,
1151 if (raid_dev->mbox_pool_handle == NULL) {
1152 goto fail_setup_dma_pool;
1155 mbox_pci_blk = raid_dev->mbox_pool;
1156 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1157 mbox_pci_blk[i].vaddr = dma_pool_alloc(
1158 raid_dev->mbox_pool_handle,
1160 &mbox_pci_blk[i].dma_addr);
1161 if (!mbox_pci_blk[i].vaddr) {
1162 goto fail_setup_dma_pool;
1167 * Allocate memory for each embedded passthru strucuture pointer
1168 * Request for a 128 bytes aligned structure for each passthru command
1170 * Since passthru and extended passthru commands are exclusive, they
1171 * share common memory pool. Passthru structures piggyback on memory
1172 * allocted to extended passthru since passthru is smaller of the two
1174 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
1175 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
1177 if (raid_dev->epthru_pool_handle == NULL) {
1178 goto fail_setup_dma_pool;
1181 epthru_pci_blk = raid_dev->epthru_pool;
1182 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1183 epthru_pci_blk[i].vaddr = dma_pool_alloc(
1184 raid_dev->epthru_pool_handle,
1186 &epthru_pci_blk[i].dma_addr);
1187 if (!epthru_pci_blk[i].vaddr) {
1188 goto fail_setup_dma_pool;
1193 // Allocate memory for each scatter-gather list. Request for 512 bytes
1194 // alignment for each sg list
1195 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
1196 &adapter->pdev->dev,
1197 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1200 if (raid_dev->sg_pool_handle == NULL) {
1201 goto fail_setup_dma_pool;
1204 sg_pci_blk = raid_dev->sg_pool;
1205 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1206 sg_pci_blk[i].vaddr = dma_pool_alloc(
1207 raid_dev->sg_pool_handle,
1209 &sg_pci_blk[i].dma_addr);
1210 if (!sg_pci_blk[i].vaddr) {
1211 goto fail_setup_dma_pool;
1217 fail_setup_dma_pool:
1218 megaraid_mbox_teardown_dma_pools(adapter);
1224 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1225 * @adapter : HBA soft state
1227 * Teardown the dma pool for mailbox, passthru and extended passthru
1228 * structures, and scatter-gather lists.
1231 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1233 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1234 struct mraid_pci_blk *epthru_pci_blk;
1235 struct mraid_pci_blk *sg_pci_blk;
1236 struct mraid_pci_blk *mbox_pci_blk;
1240 sg_pci_blk = raid_dev->sg_pool;
1241 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1242 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1243 sg_pci_blk[i].dma_addr);
1245 dma_pool_destroy(raid_dev->sg_pool_handle);
1248 epthru_pci_blk = raid_dev->epthru_pool;
1249 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1250 dma_pool_free(raid_dev->epthru_pool_handle,
1251 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1253 dma_pool_destroy(raid_dev->epthru_pool_handle);
1256 mbox_pci_blk = raid_dev->mbox_pool;
1257 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1258 dma_pool_free(raid_dev->mbox_pool_handle,
1259 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1261 dma_pool_destroy(raid_dev->mbox_pool_handle);
1268 * megaraid_alloc_scb - detach and return a scb from the free list
1269 * @adapter : controller's soft state
1270 * @scp : pointer to the scsi command to be executed
1272 * Return the scb from the head of the free list. %NULL if there are none
1276 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1278 struct list_head *head = &adapter->kscb_pool;
1280 unsigned long flags;
1282 // detach scb from free pool
1283 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1285 if (list_empty(head)) {
1286 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1290 scb = list_entry(head->next, scb_t, list);
1291 list_del_init(&scb->list);
1293 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1295 scb->state = SCB_ACTIVE;
1297 scb->dma_type = MRAID_DMA_NONE;
1304 * megaraid_dealloc_scb - return the scb to the free pool
1305 * @adapter : controller's soft state
1306 * @scb : scb to be freed
1308 * Return the scb back to the free list of scbs. The caller must 'flush' the
1309 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1310 * NOTE NOTE: Make sure the scb is not on any list before calling this
1314 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1316 unsigned long flags;
1318 // put scb in the free pool
1319 scb->state = SCB_FREE;
1321 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1323 list_add(&scb->list, &adapter->kscb_pool);
1325 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1332 * megaraid_mbox_mksgl - make the scatter-gather list
1333 * @adapter : controller's soft state
1334 * @scb : scsi control block
1336 * Prepare the scatter-gather list.
1339 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1341 struct scatterlist *sgl;
1343 struct scsi_cmnd *scp;
1349 ccb = (mbox_ccb_t *)scb->ccb;
1351 sgcnt = scsi_dma_map(scp);
1352 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1354 // no mapping required if no data to be transferred
1358 scb->dma_type = MRAID_DMA_WSG;
1360 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1361 ccb->sgl64[i].address = sg_dma_address(sgl);
1362 ccb->sgl64[i].length = sg_dma_len(sgl);
1365 // Return count of SG nodes
1371 * mbox_post_cmd - issue a mailbox command
1372 * @adapter : controller's soft state
1373 * @scb : command to be issued
1375 * Post the command to the controller if mailbox is available.
1378 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1380 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1384 unsigned long flags;
1388 ccb = (mbox_ccb_t *)scb->ccb;
1389 mbox = raid_dev->mbox;
1390 mbox64 = raid_dev->mbox64;
1393 * Check for busy mailbox. If it is, return failure - the caller
1394 * should retry later.
1396 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1398 if (unlikely(mbox->busy)) {
1403 } while(mbox->busy && (i < max_mbox_busy_wait));
1407 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1414 // Copy this command's mailbox data into "adapter's" mailbox
1415 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1416 mbox->cmdid = scb->sno;
1418 adapter->outstanding_cmds++;
1420 mbox->busy = 1; // Set busy
1425 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1427 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1434 * megaraid_queue_command - generic queue entry point for all LLDs
1435 * @scp : pointer to the scsi command to be executed
1436 * @done : callback routine to be called after the cmd has be completed
1438 * Queue entry point for mailbox based controllers.
1441 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1447 adapter = SCP2ADAPTER(scp);
1448 scp->scsi_done = done;
1452 * Allocate and build a SCB request
1453 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1454 * not allocate scb. We will return non-zero status in that case.
1455 * NOTE: scb can be null even though certain commands completed
1456 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1457 * return 0 in that case, and we would do the callback right away.
1460 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1461 if (!scb) { // command already completed
1466 megaraid_mbox_runpendq(adapter, scb);
1470 static DEF_SCSI_QCMD(megaraid_queue_command)
1473 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1474 * @adapter : controller's soft state
1475 * @scp : mid-layer scsi command pointer
1476 * @busy : set if request could not be completed because of lack of
1479 * Transform the mid-layer scsi command to megaraid firmware lingua.
1480 * Convert the command issued by mid-layer to format understood by megaraid
1481 * firmware. We also complete certain commands without sending them to firmware.
1484 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1486 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1491 mraid_passthru_t *pthru;
1495 char skip[] = "skipping";
1496 char scan[] = "scanning";
1501 * Get the appropriate device map for the device this command is
1504 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1507 * Logical drive commands
1510 switch (scp->cmnd[0]) {
1511 case TEST_UNIT_READY:
1513 * Do we support clustering and is the support enabled
1514 * If no, return success always
1517 scp->result = (DID_OK << 16);
1521 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1522 scp->result = (DID_ERROR << 16);
1527 scb->dma_direction = scp->sc_data_direction;
1528 scb->dev_channel = 0xFF;
1529 scb->dev_target = target;
1530 ccb = (mbox_ccb_t *)scb->ccb;
1533 * The command id will be provided by the command
1536 ccb->raw_mbox[0] = CLUSTER_CMD;
1537 ccb->raw_mbox[2] = RESERVATION_STATUS;
1538 ccb->raw_mbox[3] = target;
1544 struct scatterlist *sgl;
1547 sgl = scsi_sglist(scp);
1549 vaddr = (caddr_t) sg_virt(&sgl[0]);
1551 memset(vaddr, 0, scp->cmnd[4]);
1554 con_log(CL_ANN, (KERN_WARNING
1555 "megaraid mailbox: invalid sg:%d\n",
1559 scp->result = (DID_OK << 16);
1564 * Display the channel scan for logical drives
1565 * Do not display scan for a channel if already done.
1567 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1569 con_log(CL_ANN, (KERN_INFO
1570 "scsi[%d]: scanning scsi channel %d",
1571 adapter->host->host_no,
1575 " [virtual] for logical drives\n"));
1577 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1580 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1581 scp->sense_buffer[0] = 0x70;
1582 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1583 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1584 scp->result = CHECK_CONDITION << 1;
1592 * Do not allow LUN > 0 for logical drives and
1593 * requests for more than 40 logical drives
1596 scp->result = (DID_BAD_TARGET << 16);
1599 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1600 scp->result = (DID_BAD_TARGET << 16);
1605 /* Allocate a SCB and initialize passthru */
1606 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1607 scp->result = (DID_ERROR << 16);
1612 ccb = (mbox_ccb_t *)scb->ccb;
1613 scb->dev_channel = 0xFF;
1614 scb->dev_target = target;
1617 mbox64 = ccb->mbox64;
1621 pthru->reqsenselen = 14;
1622 pthru->islogical = 1;
1623 pthru->logdrv = target;
1624 pthru->cdblen = scp->cmd_len;
1625 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1627 mbox->cmd = MBOXCMD_PASSTHRU64;
1628 scb->dma_direction = scp->sc_data_direction;
1630 pthru->dataxferlen = scsi_bufflen(scp);
1631 pthru->dataxferaddr = ccb->sgl_dma_h;
1632 pthru->numsge = megaraid_mbox_mksgl(adapter,
1635 mbox->xferaddr = 0xFFFFFFFF;
1636 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1637 mbox64->xferaddr_hi = 0;
1649 * Allocate a SCB and initialize mailbox
1651 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1652 scp->result = (DID_ERROR << 16);
1656 ccb = (mbox_ccb_t *)scb->ccb;
1657 scb->dev_channel = 0xFF;
1658 scb->dev_target = target;
1660 mbox64 = ccb->mbox64;
1661 mbox->logdrv = target;
1664 * A little HACK: 2nd bit is zero for all scsi read
1665 * commands and is set for all scsi write commands
1667 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1671 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1673 if (scp->cmd_len == 6) {
1674 mbox->numsectors = (uint32_t)scp->cmnd[4];
1676 ((uint32_t)scp->cmnd[1] << 16) |
1677 ((uint32_t)scp->cmnd[2] << 8) |
1678 (uint32_t)scp->cmnd[3];
1680 mbox->lba &= 0x1FFFFF;
1684 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1686 else if (scp->cmd_len == 10) {
1688 (uint32_t)scp->cmnd[8] |
1689 ((uint32_t)scp->cmnd[7] << 8);
1691 ((uint32_t)scp->cmnd[2] << 24) |
1692 ((uint32_t)scp->cmnd[3] << 16) |
1693 ((uint32_t)scp->cmnd[4] << 8) |
1694 (uint32_t)scp->cmnd[5];
1698 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1700 else if (scp->cmd_len == 12) {
1702 ((uint32_t)scp->cmnd[2] << 24) |
1703 ((uint32_t)scp->cmnd[3] << 16) |
1704 ((uint32_t)scp->cmnd[4] << 8) |
1705 (uint32_t)scp->cmnd[5];
1708 ((uint32_t)scp->cmnd[6] << 24) |
1709 ((uint32_t)scp->cmnd[7] << 16) |
1710 ((uint32_t)scp->cmnd[8] << 8) |
1711 (uint32_t)scp->cmnd[9];
1714 con_log(CL_ANN, (KERN_WARNING
1715 "megaraid: unsupported CDB length\n"));
1717 megaraid_dealloc_scb(adapter, scb);
1719 scp->result = (DID_ERROR << 16);
1723 scb->dma_direction = scp->sc_data_direction;
1725 // Calculate Scatter-Gather info
1726 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1727 mbox->numsge = megaraid_mbox_mksgl(adapter,
1729 mbox->xferaddr = 0xFFFFFFFF;
1730 mbox64->xferaddr_hi = 0;
1737 * Do we support clustering and is the support enabled
1740 scp->result = (DID_BAD_TARGET << 16);
1745 * Allocate a SCB and initialize mailbox
1747 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1748 scp->result = (DID_ERROR << 16);
1753 ccb = (mbox_ccb_t *)scb->ccb;
1754 scb->dev_channel = 0xFF;
1755 scb->dev_target = target;
1756 ccb->raw_mbox[0] = CLUSTER_CMD;
1757 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1758 RESERVE_LD : RELEASE_LD;
1760 ccb->raw_mbox[3] = target;
1761 scb->dma_direction = scp->sc_data_direction;
1766 scp->result = (DID_BAD_TARGET << 16);
1770 else { // Passthru device commands
1772 // Do not allow access to target id > 15 or LUN > 7
1773 if (target > 15 || SCP2LUN(scp) > 7) {
1774 scp->result = (DID_BAD_TARGET << 16);
1778 // if fast load option was set and scan for last device is
1779 // over, reset the fast_load flag so that during a possible
1780 // next scan, devices can be made available
1781 if (rdev->fast_load && (target == 15) &&
1782 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1784 con_log(CL_ANN, (KERN_INFO
1785 "megaraid[%d]: physical device scan re-enabled\n",
1786 adapter->host->host_no));
1787 rdev->fast_load = 0;
1791 * Display the channel scan for physical devices
1793 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1795 ss = rdev->fast_load ? skip : scan;
1797 con_log(CL_ANN, (KERN_INFO
1798 "scsi[%d]: %s scsi channel %d [Phy %d]",
1799 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1803 " for non-raid devices\n"));
1805 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1808 // disable channel sweep if fast load option given
1809 if (rdev->fast_load) {
1810 scp->result = (DID_BAD_TARGET << 16);
1814 // Allocate a SCB and initialize passthru
1815 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1816 scp->result = (DID_ERROR << 16);
1821 ccb = (mbox_ccb_t *)scb->ccb;
1822 scb->dev_channel = channel;
1823 scb->dev_target = target;
1824 scb->dma_direction = scp->sc_data_direction;
1826 mbox64 = ccb->mbox64;
1828 // Does this firmware support extended CDBs
1829 if (adapter->max_cdb_sz == 16) {
1830 mbox->cmd = MBOXCMD_EXTPTHRU;
1832 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1834 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1835 mbox64->xferaddr_hi = 0;
1836 mbox->xferaddr = 0xFFFFFFFF;
1839 mbox->cmd = MBOXCMD_PASSTHRU64;
1841 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1843 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1844 mbox64->xferaddr_hi = 0;
1845 mbox->xferaddr = 0xFFFFFFFF;
1855 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1856 * @adapter : controller's soft state
1857 * @scb_q : SCB to be queued in the pending list
1859 * Scan the pending list for commands which are not yet issued and try to
1860 * post to the controller. The SCB can be a null pointer, which would indicate
1861 * no SCB to be queue, just try to execute the ones in the pending list.
1863 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1864 * out from the head of the pending list. If it is successfully issued, the
1865 * next SCB is at the head now.
1868 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1871 unsigned long flags;
1873 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1876 scb_q->state = SCB_PENDQ;
1877 list_add_tail(&scb_q->list, &adapter->pend_list);
1880 // if the adapter in not in quiescent mode, post the commands to FW
1881 if (adapter->quiescent) {
1882 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1886 while (!list_empty(&adapter->pend_list)) {
1888 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1890 scb = list_entry(adapter->pend_list.next, scb_t, list);
1892 // remove the scb from the pending list and try to
1893 // issue. If we are unable to issue it, put back in
1894 // the pending list and return
1896 list_del_init(&scb->list);
1898 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1900 // if mailbox was busy, return SCB back to pending
1901 // list. Make sure to add at the head, since that's
1902 // where it would have been removed from
1904 scb->state = SCB_ISSUED;
1906 if (mbox_post_cmd(adapter, scb) != 0) {
1908 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1910 scb->state = SCB_PENDQ;
1912 list_add(&scb->list, &adapter->pend_list);
1914 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1920 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1923 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1931 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1932 * @adapter : pointer to controller's soft state
1933 * @scb : scsi control block
1934 * @scp : scsi command from the mid-layer
1936 * Prepare a command for the scsi physical devices.
1939 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1940 struct scsi_cmnd *scp)
1943 mraid_passthru_t *pthru;
1947 ccb = (mbox_ccb_t *)scb->ccb;
1949 channel = scb->dev_channel;
1950 target = scb->dev_target;
1952 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1955 pthru->islogical = 0;
1957 pthru->target = (channel << 4) | target;
1958 pthru->logdrv = SCP2LUN(scp);
1959 pthru->reqsenselen = 14;
1960 pthru->cdblen = scp->cmd_len;
1962 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1964 if (scsi_bufflen(scp)) {
1965 pthru->dataxferlen = scsi_bufflen(scp);
1966 pthru->dataxferaddr = ccb->sgl_dma_h;
1967 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1970 pthru->dataxferaddr = 0;
1971 pthru->dataxferlen = 0;
1979 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1980 * @adapter : pointer to controller's soft state
1981 * @scb : scsi control block
1982 * @scp : scsi command from the mid-layer
1984 * Prepare a command for the scsi physical devices. This routine prepares
1985 * commands for devices which can take extended CDBs (>10 bytes).
1988 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
1989 struct scsi_cmnd *scp)
1992 mraid_epassthru_t *epthru;
1996 ccb = (mbox_ccb_t *)scb->ccb;
1997 epthru = ccb->epthru;
1998 channel = scb->dev_channel;
1999 target = scb->dev_target;
2001 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2002 epthru->timeout = 4;
2004 epthru->islogical = 0;
2005 epthru->channel = 0;
2006 epthru->target = (channel << 4) | target;
2007 epthru->logdrv = SCP2LUN(scp);
2008 epthru->reqsenselen = 14;
2009 epthru->cdblen = scp->cmd_len;
2011 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2013 if (scsi_bufflen(scp)) {
2014 epthru->dataxferlen = scsi_bufflen(scp);
2015 epthru->dataxferaddr = ccb->sgl_dma_h;
2016 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2019 epthru->dataxferaddr = 0;
2020 epthru->dataxferlen = 0;
2028 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2029 * @adapter : controller's soft state
2031 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2032 * completed command and put them on the completed list for later processing.
2034 * Returns: 1 if the interrupt is valid, 0 otherwise
2037 megaraid_ack_sequence(adapter_t *adapter)
2039 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2043 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2044 struct list_head clist;
2047 unsigned long flags;
2051 mbox = raid_dev->mbox;
2053 // move the SCBs from the firmware completed array to our local list
2054 INIT_LIST_HEAD(&clist);
2056 // loop till F/W has more commands for us to complete
2058 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2061 * Check if a valid interrupt is pending. If found, force the
2062 * interrupt line low.
2064 dword = RDOUTDOOR(raid_dev);
2065 if (dword != 0x10001234) break;
2069 WROUTDOOR(raid_dev, 0x10001234);
2072 // wait for valid numstatus to post
2073 for (i = 0; i < 0xFFFFF; i++) {
2074 if (mbox->numstatus != 0xFF) {
2075 nstatus = mbox->numstatus;
2080 mbox->numstatus = 0xFF;
2082 adapter->outstanding_cmds -= nstatus;
2084 for (i = 0; i < nstatus; i++) {
2086 // wait for valid command index to post
2087 for (j = 0; j < 0xFFFFF; j++) {
2088 if (mbox->completed[i] != 0xFF) break;
2091 completed[i] = mbox->completed[i];
2092 mbox->completed[i] = 0xFF;
2094 if (completed[i] == 0xFF) {
2095 con_log(CL_ANN, (KERN_CRIT
2096 "megaraid: command posting timed out\n"));
2102 // Get SCB associated with this command id
2103 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2105 scb = adapter->uscb_list + (completed[i] -
2106 MBOX_MAX_SCSI_CMDS);
2110 scb = adapter->kscb_list + completed[i];
2113 scb->status = mbox->status;
2114 list_add_tail(&scb->list, &clist);
2117 // Acknowledge interrupt
2118 WRINDOOR(raid_dev, 0x02);
2122 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2125 // put the completed commands in the completed list. DPC would
2126 // complete these commands later
2127 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2129 list_splice(&clist, &adapter->completed_list);
2131 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2134 // schedule the DPC if there is some work for it
2136 tasklet_schedule(&adapter->dpc_h);
2143 * megaraid_isr - isr for memory based mailbox based controllers
2145 * @devp : pointer to our soft state
2147 * Interrupt service routine for memory-mapped mailbox controllers.
2150 megaraid_isr(int irq, void *devp)
2152 adapter_t *adapter = devp;
2155 handled = megaraid_ack_sequence(adapter);
2157 /* Loop through any pending requests */
2158 if (!adapter->quiescent) {
2159 megaraid_mbox_runpendq(adapter, NULL);
2162 return IRQ_RETVAL(handled);
2167 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2168 * @devp : pointer to HBA soft state
2170 * Pick up the commands from the completed list and send back to the owners.
2171 * This is a reentrant function and does not assume any locks are held while
2172 * it is being called.
2175 megaraid_mbox_dpc(unsigned long devp)
2177 adapter_t *adapter = (adapter_t *)devp;
2178 mraid_device_t *raid_dev;
2179 struct list_head clist;
2180 struct scatterlist *sgl;
2183 struct scsi_cmnd *scp;
2184 mraid_passthru_t *pthru;
2185 mraid_epassthru_t *epthru;
2191 unsigned long flags;
2197 if (!adapter) return;
2199 raid_dev = ADAP2RAIDDEV(adapter);
2201 // move the SCBs from the completed list to our local list
2202 INIT_LIST_HEAD(&clist);
2204 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2206 list_splice_init(&adapter->completed_list, &clist);
2208 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2211 list_for_each_entry_safe(scb, tmp, &clist, list) {
2213 status = scb->status;
2215 ccb = (mbox_ccb_t *)scb->ccb;
2217 epthru = ccb->epthru;
2220 // Make sure f/w has completed a valid command
2221 if (scb->state != SCB_ISSUED) {
2222 con_log(CL_ANN, (KERN_CRIT
2223 "megaraid critical err: invalid command %d:%d:%p\n",
2224 scb->sno, scb->state, scp));
2226 continue; // Must never happen!
2229 // check for the management command and complete it right away
2230 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2231 scb->state = SCB_FREE;
2232 scb->status = status;
2234 // remove from local clist
2235 list_del_init(&scb->list);
2237 kioc = (uioc_t *)scb->gp;
2240 megaraid_mbox_mm_done(adapter, scb);
2245 // Was an abort issued for this command earlier
2246 if (scb->state & SCB_ABORT) {
2247 con_log(CL_ANN, (KERN_NOTICE
2248 "megaraid: aborted cmd [%x] completed\n",
2253 * If the inquiry came of a disk drive which is not part of
2254 * any RAID array, expose it to the kernel. For this to be
2255 * enabled, user must set the "megaraid_expose_unconf_disks"
2256 * flag to 1 by specifying it on module parameter list.
2257 * This would enable data migration off drives from other
2260 islogical = MRAID_IS_LOGICAL(adapter, scp);
2261 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2262 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2264 sgl = scsi_sglist(scp);
2266 c = *(unsigned char *) sg_virt(&sgl[0]);
2268 con_log(CL_ANN, (KERN_WARNING
2269 "megaraid mailbox: invalid sg:%d\n",
2274 if ((c & 0x1F ) == TYPE_DISK) {
2275 pdev_index = (scb->dev_channel * 16) +
2278 raid_dev->pdrv_state[pdev_index] & 0x0F;
2280 if (pdev_state == PDRV_ONLINE ||
2281 pdev_state == PDRV_FAILED ||
2282 pdev_state == PDRV_RBLD ||
2283 pdev_state == PDRV_HOTSPARE ||
2284 megaraid_expose_unconf_disks == 0) {
2291 // Convert MegaRAID status to Linux error code
2296 scp->result = (DID_OK << 16);
2301 /* set sense_buffer and result fields */
2302 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2303 mbox->cmd == MBOXCMD_PASSTHRU64) {
2305 memcpy(scp->sense_buffer, pthru->reqsensearea,
2308 scp->result = DRIVER_SENSE << 24 |
2309 DID_OK << 16 | CHECK_CONDITION << 1;
2312 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2314 memcpy(scp->sense_buffer,
2315 epthru->reqsensearea, 14);
2317 scp->result = DRIVER_SENSE << 24 |
2319 CHECK_CONDITION << 1;
2321 scp->sense_buffer[0] = 0x70;
2322 scp->sense_buffer[2] = ABORTED_COMMAND;
2323 scp->result = CHECK_CONDITION << 1;
2330 scp->result = DID_BUS_BUSY << 16 | status;
2336 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2339 if (scp->cmnd[0] == TEST_UNIT_READY) {
2340 scp->result = DID_ERROR << 16 |
2341 RESERVATION_CONFLICT << 1;
2345 * Error code returned is 1 if Reserve or Release
2346 * failed or the input parameter is invalid
2348 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2349 scp->cmnd[0] == RELEASE)) {
2351 scp->result = DID_ERROR << 16 |
2352 RESERVATION_CONFLICT << 1;
2355 scp->result = DID_BAD_TARGET << 16 | status;
2359 // print a debug message for all failed commands
2361 megaraid_mbox_display_scb(adapter, scb);
2364 scsi_dma_unmap(scp);
2366 // remove from local clist
2367 list_del_init(&scb->list);
2369 // put back in free list
2370 megaraid_dealloc_scb(adapter, scb);
2372 // send the scsi packet back to kernel
2373 scp->scsi_done(scp);
2381 * megaraid_abort_handler - abort the scsi command
2382 * @scp : command to be aborted
2384 * Abort a previous SCSI request. Only commands on the pending list can be
2385 * aborted. All the commands issued to the F/W must complete.
2388 megaraid_abort_handler(struct scsi_cmnd *scp)
2391 mraid_device_t *raid_dev;
2395 unsigned long flags;
2399 adapter = SCP2ADAPTER(scp);
2400 raid_dev = ADAP2RAIDDEV(adapter);
2402 con_log(CL_ANN, (KERN_WARNING
2403 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2404 scp->cmnd[0], SCP2CHANNEL(scp),
2405 SCP2TARGET(scp), SCP2LUN(scp)));
2407 // If FW has stopped responding, simply return failure
2408 if (raid_dev->hw_error) {
2409 con_log(CL_ANN, (KERN_NOTICE
2410 "megaraid: hw error, not aborting\n"));
2414 // There might a race here, where the command was completed by the
2415 // firmware and now it is on the completed list. Before we could
2416 // complete the command to the kernel in dpc, the abort came.
2417 // Find out if this is the case to avoid the race.
2419 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2420 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2422 if (scb->scp == scp) { // Found command
2424 list_del_init(&scb->list); // from completed list
2426 con_log(CL_ANN, (KERN_WARNING
2427 "megaraid: %d[%d:%d], abort from completed list\n",
2428 scb->sno, scb->dev_channel, scb->dev_target));
2430 scp->result = (DID_ABORT << 16);
2431 scp->scsi_done(scp);
2433 megaraid_dealloc_scb(adapter, scb);
2435 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2441 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2444 // Find out if this command is still on the pending list. If it is and
2445 // was never issued, abort and return success. If the command is owned
2446 // by the firmware, we must wait for it to complete by the FW.
2447 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2448 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2450 if (scb->scp == scp) { // Found command
2452 list_del_init(&scb->list); // from pending list
2454 ASSERT(!(scb->state & SCB_ISSUED));
2456 con_log(CL_ANN, (KERN_WARNING
2457 "megaraid abort: [%d:%d], driver owner\n",
2458 scb->dev_channel, scb->dev_target));
2460 scp->result = (DID_ABORT << 16);
2461 scp->scsi_done(scp);
2463 megaraid_dealloc_scb(adapter, scb);
2465 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2471 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2474 // Check do we even own this command, in which case this would be
2475 // owned by the firmware. The only way to locate the FW scb is to
2476 // traverse through the list of all SCB, since driver does not
2477 // maintain these SCBs on any list
2479 spin_lock_irq(&adapter->lock);
2480 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2481 scb = adapter->kscb_list + i;
2483 if (scb->scp == scp) {
2487 if (!(scb->state & SCB_ISSUED)) {
2488 con_log(CL_ANN, (KERN_WARNING
2489 "megaraid abort: %d[%d:%d], invalid state\n",
2490 scb->sno, scb->dev_channel, scb->dev_target));
2494 con_log(CL_ANN, (KERN_WARNING
2495 "megaraid abort: %d[%d:%d], fw owner\n",
2496 scb->sno, scb->dev_channel, scb->dev_target));
2500 spin_unlock_irq(&adapter->lock);
2503 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2505 // FIXME: Should there be a callback for this command?
2509 // We cannot actually abort a command owned by firmware, return
2510 // failure and wait for reset. In host reset handler, we will find out
2511 // if the HBA is still live
2516 * megaraid_reset_handler - device reset handler for mailbox based driver
2517 * @scp : reference command
2519 * Reset handler for the mailbox based controller. First try to find out if
2520 * the FW is still live, in which case the outstanding commands counter mut go
2521 * down to 0. If that happens, also issue the reservation reset command to
2522 * relinquish (possible) reservations on the logical drives connected to this
2526 megaraid_reset_handler(struct scsi_cmnd *scp)
2531 mraid_device_t *raid_dev;
2532 unsigned long flags;
2533 uint8_t raw_mbox[sizeof(mbox_t)];
2535 int recovery_window;
2539 adapter = SCP2ADAPTER(scp);
2540 raid_dev = ADAP2RAIDDEV(adapter);
2542 // return failure if adapter is not responding
2543 if (raid_dev->hw_error) {
2544 con_log(CL_ANN, (KERN_NOTICE
2545 "megaraid: hw error, cannot reset\n"));
2549 // Under exceptional conditions, FW can take up to 3 minutes to
2550 // complete command processing. Wait for additional 2 minutes for the
2551 // pending commands counter to go down to 0. If it doesn't, let the
2552 // controller be marked offline
2553 // Also, reset all the commands currently owned by the driver
2554 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2555 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2556 list_del_init(&scb->list); // from pending list
2558 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2559 con_log(CL_ANN, (KERN_WARNING
2560 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2561 scb->sno, scb->dev_channel, scb->dev_target));
2565 kioc = (uioc_t *)scb->gp;
2566 kioc->status = -EFAULT;
2568 megaraid_mbox_mm_done(adapter, scb);
2570 if (scb->scp == scp) { // Found command
2571 con_log(CL_ANN, (KERN_WARNING
2572 "megaraid: %d[%d:%d], reset from pending list\n",
2573 scb->sno, scb->dev_channel, scb->dev_target));
2575 con_log(CL_ANN, (KERN_WARNING
2576 "megaraid: IO packet with %d[%d:%d] being reset\n",
2577 scb->sno, scb->dev_channel, scb->dev_target));
2580 scb->scp->result = (DID_RESET << 16);
2581 scb->scp->scsi_done(scb->scp);
2583 megaraid_dealloc_scb(adapter, scb);
2586 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2588 if (adapter->outstanding_cmds) {
2589 con_log(CL_ANN, (KERN_NOTICE
2590 "megaraid: %d outstanding commands. Max wait %d sec\n",
2591 adapter->outstanding_cmds,
2592 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2595 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2597 for (i = 0; i < recovery_window; i++) {
2599 megaraid_ack_sequence(adapter);
2601 // print a message once every 5 seconds only
2604 "megaraid mbox: Wait for %d commands to complete:%d\n",
2605 adapter->outstanding_cmds,
2606 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2609 // bailout if no recovery happened in reset time
2610 if (adapter->outstanding_cmds == 0) {
2617 spin_lock(&adapter->lock);
2619 // If still outstanding commands, bail out
2620 if (adapter->outstanding_cmds) {
2621 con_log(CL_ANN, (KERN_WARNING
2622 "megaraid mbox: critical hardware error!\n"));
2624 raid_dev->hw_error = 1;
2630 con_log(CL_ANN, (KERN_NOTICE
2631 "megaraid mbox: reset sequence completed successfully\n"));
2635 // If the controller supports clustering, reset reservations
2641 // clear reservations if any
2642 raw_mbox[0] = CLUSTER_CMD;
2643 raw_mbox[2] = RESET_RESERVATIONS;
2646 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2648 (KERN_INFO "megaraid: reservation reset\n"));
2652 con_log(CL_ANN, (KERN_WARNING
2653 "megaraid: reservation reset failed\n"));
2657 spin_unlock(&adapter->lock);
2662 * START: internal commands library
2664 * This section of the driver has the common routine used by the driver and
2665 * also has all the FW routines
2669 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2670 * @adapter : controller's soft state
2671 * @raw_mbox : the mailbox
2673 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2677 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2679 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2684 mbox = raid_dev->mbox;
2687 * Wait until mailbox is free
2689 if (megaraid_busywait_mbox(raid_dev) != 0)
2690 goto blocked_mailbox;
2693 * Copy mailbox data into host structure
2695 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2700 mbox->numstatus = 0xFF;
2701 mbox->status = 0xFF;
2704 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2706 // wait for maximum 1 second for status to post. If the status is not
2707 // available within 1 second, assume FW is initializing and wait
2708 // for an extended amount of time
2709 if (mbox->numstatus == 0xFF) { // status not yet available
2712 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2719 con_log(CL_ANN, (KERN_NOTICE
2720 "megaraid mailbox: wait for FW to boot "));
2722 for (i = 0; (mbox->numstatus == 0xFF) &&
2723 (i < MBOX_RESET_WAIT); i++) {
2725 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2726 MBOX_RESET_WAIT - i));
2730 if (i == MBOX_RESET_WAIT) {
2733 "\nmegaraid mailbox: status not available\n"));
2737 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2741 // wait for maximum 1 second for poll semaphore
2742 if (mbox->poll != 0x77) {
2745 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2751 con_log(CL_ANN, (KERN_WARNING
2752 "megaraid mailbox: could not get poll semaphore\n"));
2757 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2760 // wait for maximum 1 second for acknowledgement
2761 if (RDINDOOR(raid_dev) & 0x2) {
2764 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2770 con_log(CL_ANN, (KERN_WARNING
2771 "megaraid mailbox: could not acknowledge\n"));
2778 status = mbox->status;
2780 // invalidate the completed command id array. After command
2781 // completion, firmware would write the valid id.
2782 mbox->numstatus = 0xFF;
2783 mbox->status = 0xFF;
2784 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2785 mbox->completed[i] = 0xFF;
2792 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2798 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2799 * @adapter : controller's soft state
2800 * @raw_mbox : the mailbox
2802 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2803 * controllers. This is a faster version of the synchronous command and
2804 * therefore can be called in interrupt-context as well.
2807 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2809 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2814 mbox = raid_dev->mbox;
2816 // return immediately if the mailbox is busy
2817 if (mbox->busy) return -1;
2819 // Copy mailbox data into host structure
2820 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2825 mbox->numstatus = 0xFF;
2826 mbox->status = 0xFF;
2829 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2831 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2832 if (mbox->numstatus != 0xFF) break;
2834 udelay(MBOX_SYNC_DELAY_200);
2837 if (i == MBOX_SYNC_WAIT_CNT) {
2838 // We may need to re-calibrate the counter
2839 con_log(CL_ANN, (KERN_CRIT
2840 "megaraid: fast sync command timed out\n"));
2843 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2846 return mbox->status;
2851 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2852 * @raid_dev : RAID device (HBA) soft state
2854 * Wait until the controller's mailbox is available to accept more commands.
2855 * Wait for at most 1 second.
2858 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2860 mbox_t *mbox = raid_dev->mbox;
2865 for (i = 0; mbox->busy && i < 1000; i++)
2869 if (i < 1000) return 0;
2875 * megaraid_mbox_product_info - some static information about the controller
2876 * @adapter : our soft state
2878 * Issue commands to the controller to grab some parameters required by our
2882 megaraid_mbox_product_info(adapter_t *adapter)
2884 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2886 uint8_t raw_mbox[sizeof(mbox_t)];
2887 mraid_pinfo_t *pinfo;
2888 dma_addr_t pinfo_dma_h;
2889 mraid_inquiry3_t *mraid_inq3;
2893 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2894 mbox = (mbox_t *)raw_mbox;
2897 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2898 * e.g., max channels, max commands etc.
2900 pinfo = dma_zalloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2901 &pinfo_dma_h, GFP_KERNEL);
2902 if (pinfo == NULL) {
2903 con_log(CL_ANN, (KERN_WARNING
2904 "megaraid: out of memory, %s %d\n", __func__,
2910 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2911 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2913 raw_mbox[0] = FC_NEW_CONFIG;
2914 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2915 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2917 // Issue the command
2918 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2920 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2922 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2923 pinfo, pinfo_dma_h);
2929 * Collect information about state of each physical drive
2930 * attached to the controller. We will expose all the disks
2931 * which are not part of RAID
2933 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2934 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2935 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2939 * Get product info for information like number of channels,
2940 * maximum commands supported.
2942 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2943 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2945 raw_mbox[0] = FC_NEW_CONFIG;
2946 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
2948 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2950 con_log(CL_ANN, (KERN_WARNING
2951 "megaraid: product info failed\n"));
2953 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2954 pinfo, pinfo_dma_h);
2960 * Setup some parameters for host, as required by our caller
2962 adapter->max_channel = pinfo->nchannels;
2965 * we will export all the logical drives on a single channel.
2966 * Add 1 since inquires do not come for inititor ID
2968 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
2969 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
2972 * These are the maximum outstanding commands for the scsi-layer
2974 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
2976 memset(adapter->fw_version, 0, VERSION_SIZE);
2977 memset(adapter->bios_version, 0, VERSION_SIZE);
2979 memcpy(adapter->fw_version, pinfo->fw_version, 4);
2980 adapter->fw_version[4] = 0;
2982 memcpy(adapter->bios_version, pinfo->bios_version, 4);
2983 adapter->bios_version[4] = 0;
2985 con_log(CL_ANN, (KERN_NOTICE
2986 "megaraid: fw version:[%s] bios version:[%s]\n",
2987 adapter->fw_version, adapter->bios_version));
2989 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo,
2998 * megaraid_mbox_extended_cdb - check for support for extended CDBs
2999 * @adapter : soft state for the controller
3001 * This routine check whether the controller in question supports extended
3002 * ( > 10 bytes ) CDBs.
3005 megaraid_mbox_extended_cdb(adapter_t *adapter)
3008 uint8_t raw_mbox[sizeof(mbox_t)];
3011 mbox = (mbox_t *)raw_mbox;
3013 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3014 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3016 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3018 raw_mbox[0] = MAIN_MISC_OPCODE;
3019 raw_mbox[2] = SUPPORT_EXT_CDB;
3025 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3034 * megaraid_mbox_support_ha - Do we support clustering
3035 * @adapter : soft state for the controller
3036 * @init_id : ID of the initiator
3038 * Determine if the firmware supports clustering and the ID of the initiator.
3041 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3044 uint8_t raw_mbox[sizeof(mbox_t)];
3048 mbox = (mbox_t *)raw_mbox;
3050 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3052 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3054 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3056 raw_mbox[0] = GET_TARGET_ID;
3058 // Issue the command
3061 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3063 *init_id = *(uint8_t *)adapter->ibuf;
3065 con_log(CL_ANN, (KERN_INFO
3066 "megaraid: cluster firmware, initiator ID: %d\n",
3077 * megaraid_mbox_support_random_del - Do we support random deletion
3078 * @adapter : soft state for the controller
3080 * Determine if the firmware supports random deletion.
3081 * Return: 1 is operation supported, 0 otherwise
3084 megaraid_mbox_support_random_del(adapter_t *adapter)
3086 uint8_t raw_mbox[sizeof(mbox_t)];
3090 * Newer firmware on Dell CERC expect a different
3091 * random deletion handling, so disable it.
3093 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3094 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3095 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3096 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3097 (adapter->fw_version[0] > '6' ||
3098 (adapter->fw_version[0] == '6' &&
3099 adapter->fw_version[2] > '6') ||
3100 (adapter->fw_version[0] == '6'
3101 && adapter->fw_version[2] == '6'
3102 && adapter->fw_version[3] > '1'))) {
3103 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3107 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3109 raw_mbox[0] = FC_DEL_LOGDRV;
3110 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3112 // Issue the command
3114 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3116 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3126 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3127 * @adapter : soft state for the controller
3129 * Find out the maximum number of scatter-gather elements supported by the
3133 megaraid_mbox_get_max_sg(adapter_t *adapter)
3136 uint8_t raw_mbox[sizeof(mbox_t)];
3140 mbox = (mbox_t *)raw_mbox;
3142 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3144 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3146 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3148 raw_mbox[0] = MAIN_MISC_OPCODE;
3149 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3151 // Issue the command
3152 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3153 nsg = *(uint8_t *)adapter->ibuf;
3156 nsg = MBOX_DEFAULT_SG_SIZE;
3159 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3166 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3167 * @adapter : soft state for the controller
3169 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3170 * can be exported as regular SCSI channels.
3173 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3175 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3177 uint8_t raw_mbox[sizeof(mbox_t)];
3180 mbox = (mbox_t *)raw_mbox;
3182 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3184 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3186 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3188 raw_mbox[0] = CHNL_CLASS;
3189 raw_mbox[2] = GET_CHNL_CLASS;
3191 // Issue the command. If the command fails, all channels are RAID
3193 raid_dev->channel_class = 0xFF;
3194 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3195 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3203 * megaraid_mbox_flush_cache - flush adapter and disks cache
3204 * @adapter : soft state for the controller
3206 * Flush adapter cache followed by disks cache.
3209 megaraid_mbox_flush_cache(adapter_t *adapter)
3211 uint8_t raw_mbox[sizeof(mbox_t)];
3213 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3215 raw_mbox[0] = FLUSH_ADAPTER;
3217 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3218 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3221 raw_mbox[0] = FLUSH_SYSTEM;
3223 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3224 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3232 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3233 * @adapter : soft state for the controller
3235 * Clears the pending cmds in FW and reinits its RAID structs.
3238 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3241 uint8_t raw_mbox[sizeof(mbox_t)];
3242 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3247 mbox = (mbox_t *)raw_mbox;
3249 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3253 mbox = raid_dev->mbox;
3255 /* Wait until mailbox is free */
3256 if (megaraid_busywait_mbox(raid_dev) != 0) {
3258 goto blocked_mailbox;
3261 /* Copy mailbox data into host structure */
3262 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3267 mbox->numstatus = 0;
3271 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3273 /* Wait for maximum 1 min for status to post.
3274 * If the Firmware SUPPORTS the ABOVE COMMAND,
3275 * mbox->cmd will be set to 0
3277 * the firmware will reject the command with
3278 * mbox->numstatus set to 1
3283 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3287 if (i > 1000 * 60) {
3292 if (mbox->numstatus == 1)
3293 status = 1; /*cmd not supported*/
3295 /* Check for interrupt line */
3296 dword = RDOUTDOOR(raid_dev);
3297 WROUTDOOR(raid_dev, dword);
3298 WRINDOOR(raid_dev,2);
3303 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3308 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3309 * @adapter : controller's soft state
3310 * @scb : SCB to be displayed
3311 * @level : debug level for console print
3313 * Diplay information about the given SCB iff the current debug level is
3317 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3320 struct scsi_cmnd *scp;
3326 ccb = (mbox_ccb_t *)scb->ccb;
3332 con_log(level, (KERN_NOTICE
3333 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3334 mbox->cmd, scb->sno));
3336 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3337 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3342 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3344 for (i = 0; i < scp->cmd_len; i++) {
3345 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3348 con_log(level, ("\n"));
3355 * megaraid_mbox_setup_device_map - manage device ids
3356 * @adapter : Driver's soft state
3358 * Manage the device ids to have an appropriate mapping between the kernel
3359 * scsi addresses and megaraid scsi and logical drive addresses. We export
3360 * scsi devices on their actual addresses, whereas the logical drives are
3361 * exported on a virtual scsi channel.
3364 megaraid_mbox_setup_device_map(adapter_t *adapter)
3370 * First fill the values on the logical drive channel
3372 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3373 adapter->device_ids[adapter->max_channel][t] =
3374 (t < adapter->init_id) ? t : t - 1;
3376 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3379 * Fill the values on the physical devices channels
3381 for (c = 0; c < adapter->max_channel; c++)
3382 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3383 adapter->device_ids[c][t] = (c << 8) | t;
3388 * END: internal commands library
3392 * START: Interface for the common management module
3394 * This is the module, which interfaces with the common management module to
3395 * provide support for ioctl and sysfs
3399 * megaraid_cmm_register - register with the management module
3400 * @adapter : HBA soft state
3402 * Register with the management module, which allows applications to issue
3403 * ioctl calls to the drivers. This interface is used by the management module
3404 * to setup sysfs support as well.
3407 megaraid_cmm_register(adapter_t *adapter)
3409 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3416 // Allocate memory for the base list of scb for management module.
3417 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3419 if (adapter->uscb_list == NULL) {
3420 con_log(CL_ANN, (KERN_WARNING
3421 "megaraid: out of memory, %s %d\n", __func__,
3427 // Initialize the synchronization parameters for resources for
3428 // commands for management module
3429 INIT_LIST_HEAD(&adapter->uscb_pool);
3431 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3435 // link all the packets. Note, CCB for commands, coming from the
3436 // commom management module, mailbox physical address are already
3437 // setup by it. We just need placeholder for that in our local command
3439 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3441 scb = adapter->uscb_list + i;
3442 ccb = raid_dev->uccb_list + i;
3444 scb->ccb = (caddr_t)ccb;
3445 ccb->mbox64 = raid_dev->umbox64 + i;
3446 ccb->mbox = &ccb->mbox64->mbox32;
3447 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3451 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3452 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3453 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3456 scb->state = SCB_FREE;
3457 scb->dma_direction = DMA_NONE;
3458 scb->dma_type = MRAID_DMA_NONE;
3459 scb->dev_channel = -1;
3460 scb->dev_target = -1;
3462 // put scb in the free pool
3463 list_add_tail(&scb->list, &adapter->uscb_pool);
3466 adp.unique_id = adapter->unique_id;
3467 adp.drvr_type = DRVRTYPE_MBOX;
3468 adp.drvr_data = (unsigned long)adapter;
3469 adp.pdev = adapter->pdev;
3470 adp.issue_uioc = megaraid_mbox_mm_handler;
3471 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3472 adp.max_kioc = MBOX_MAX_USER_CMDS;
3474 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3476 con_log(CL_ANN, (KERN_WARNING
3477 "megaraid mbox: did not register with CMM\n"));
3479 kfree(adapter->uscb_list);
3487 * megaraid_cmm_unregister - un-register with the management module
3488 * @adapter : HBA soft state
3490 * Un-register with the management module.
3491 * FIXME: mgmt module must return failure for unregister if it has pending
3495 megaraid_cmm_unregister(adapter_t *adapter)
3497 kfree(adapter->uscb_list);
3498 mraid_mm_unregister_adp(adapter->unique_id);
3504 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3505 * @drvr_data : LLD specific data
3506 * @kioc : CMM interface packet
3507 * @action : command action
3509 * This routine is invoked whenever the Common Management Module (CMM) has a
3510 * command for us. The 'action' parameter specifies if this is a new command
3514 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3518 if (action != IOCTL_ISSUE) {
3519 con_log(CL_ANN, (KERN_WARNING
3520 "megaraid: unsupported management action:%#2x\n",
3525 adapter = (adapter_t *)drvr_data;
3527 // make sure this adapter is not being detached right now.
3528 if (atomic_read(&adapter->being_detached)) {
3529 con_log(CL_ANN, (KERN_WARNING
3530 "megaraid: reject management request, detaching\n"));
3534 switch (kioc->opcode) {
3538 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3539 (unsigned long)kioc->buf_vaddr);
3543 return kioc->status;
3547 return megaraid_mbox_mm_command(adapter, kioc);
3550 kioc->status = (-EINVAL);
3555 return 0; // not reached
3559 * megaraid_mbox_mm_command - issues commands routed through CMM
3560 * @adapter : HBA soft state
3561 * @kioc : management command packet
3563 * Issues commands, which are routed through the management module.
3566 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3568 struct list_head *head = &adapter->uscb_pool;
3573 unsigned long flags;
3575 // detach one scb from free pool
3576 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3578 if (list_empty(head)) { // should never happen because of CMM
3580 con_log(CL_ANN, (KERN_WARNING
3581 "megaraid mbox: bug in cmm handler, lost resources\n"));
3583 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3588 scb = list_entry(head->next, scb_t, list);
3589 list_del_init(&scb->list);
3591 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3593 scb->state = SCB_ACTIVE;
3594 scb->dma_type = MRAID_DMA_NONE;
3595 scb->dma_direction = DMA_NONE;
3597 ccb = (mbox_ccb_t *)scb->ccb;
3598 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3599 raw_mbox = (uint8_t *)&mbox64->mbox32;
3601 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3603 scb->gp = (unsigned long)kioc;
3606 * If it is a logdrv random delete operation, we have to wait till
3607 * there are no outstanding cmds at the fw and then issue it directly
3609 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3611 if (wait_till_fw_empty(adapter)) {
3612 con_log(CL_ANN, (KERN_NOTICE
3613 "megaraid mbox: LD delete, timed out\n"));
3615 kioc->status = -ETIME;
3619 megaraid_mbox_mm_done(adapter, scb);
3624 INIT_LIST_HEAD(&scb->list);
3626 scb->state = SCB_ISSUED;
3627 if (mbox_post_cmd(adapter, scb) != 0) {
3629 con_log(CL_ANN, (KERN_NOTICE
3630 "megaraid mbox: LD delete, mailbox busy\n"));
3632 kioc->status = -EBUSY;
3636 megaraid_mbox_mm_done(adapter, scb);
3644 // put the command on the pending list and execute
3645 megaraid_mbox_runpendq(adapter, scb);
3652 wait_till_fw_empty(adapter_t *adapter)
3654 unsigned long flags = 0;
3659 * Set the quiescent flag to stop issuing cmds to FW.
3661 spin_lock_irqsave(&adapter->lock, flags);
3662 adapter->quiescent++;
3663 spin_unlock_irqrestore(&adapter->lock, flags);
3666 * Wait till there are no more cmds outstanding at FW. Try for at most
3669 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3670 con_log(CL_DLEVEL1, (KERN_INFO
3671 "megaraid: FW has %d pending commands\n",
3672 adapter->outstanding_cmds));
3677 return adapter->outstanding_cmds;
3682 * megaraid_mbox_mm_done - callback for CMM commands
3683 * @adapter : HBA soft state
3684 * @scb : completed command
3686 * Callback routine for internal commands originated from the management
3690 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3695 unsigned long flags;
3697 kioc = (uioc_t *)scb->gp;
3698 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3699 mbox64->mbox32.status = scb->status;
3700 raw_mbox = (uint8_t *)&mbox64->mbox32;
3703 // put scb in the free pool
3704 scb->state = SCB_FREE;
3707 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3709 list_add(&scb->list, &adapter->uscb_pool);
3711 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3713 // if a delete logical drive operation succeeded, restart the
3715 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3717 adapter->quiescent--;
3719 megaraid_mbox_runpendq(adapter, NULL);
3729 * gather_hbainfo - HBA characteristics for the applications
3730 * @adapter : HBA soft state
3731 * @hinfo : pointer to the caller's host info strucuture
3734 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3736 hinfo->pci_vendor_id = adapter->pdev->vendor;
3737 hinfo->pci_device_id = adapter->pdev->device;
3738 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3739 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3741 hinfo->pci_bus = adapter->pdev->bus->number;
3742 hinfo->pci_dev_fn = adapter->pdev->devfn;
3743 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3744 hinfo->irq = adapter->host->irq;
3745 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3747 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3748 hinfo->host_no = adapter->host->host_no;
3754 * END: Interface for the common management module
3760 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3761 * @adapter : controller's soft state
3763 * Allocate packets required to issue FW calls whenever the sysfs attributes
3764 * are read. These attributes would require up-to-date information from the
3765 * FW. Also set up resources for mutual exclusion to share these resources and
3768 * Return 0 on success.
3769 * Return -ERROR_CODE on failure.
3772 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3774 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3777 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3779 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3781 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev,
3782 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL);
3784 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3785 !raid_dev->sysfs_buffer) {
3787 con_log(CL_ANN, (KERN_WARNING
3788 "megaraid: out of memory, %s %d\n", __func__,
3793 megaraid_sysfs_free_resources(adapter);
3796 mutex_init(&raid_dev->sysfs_mtx);
3798 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3805 * megaraid_sysfs_free_resources - free sysfs related resources
3806 * @adapter : controller's soft state
3808 * Free packets allocated for sysfs FW commands
3811 megaraid_sysfs_free_resources(adapter_t *adapter)
3813 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3815 kfree(raid_dev->sysfs_uioc);
3816 kfree(raid_dev->sysfs_mbox64);
3818 if (raid_dev->sysfs_buffer) {
3819 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE,
3820 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3826 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3827 * @uioc : completed packet
3829 * Callback routine called in the ISR/tasklet context for get ldmap call
3832 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3834 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3835 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3839 wake_up(&raid_dev->sysfs_wait_q);
3843 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3844 * @t : timed out timer
3846 * Timeout routine to recover and return to application, in case the adapter
3847 * has stopped responding. A timeout of 60 seconds for this command seems like
3851 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
3853 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3854 uioc_t *uioc = timeout->uioc;
3855 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3856 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3858 uioc->status = -ETIME;
3860 wake_up(&raid_dev->sysfs_wait_q);
3865 * megaraid_sysfs_get_ldmap - get update logical drive map
3866 * @adapter : controller's soft state
3868 * This routine will be called whenever user reads the logical drive
3869 * attributes, go get the current logical drive mapping table from the
3870 * firmware. We use the management API's to issue commands to the controller.
3872 * NOTE: The commands issuance functionality is not generalized and
3873 * implemented in context of "get ld map" command only. If required, the
3874 * command issuance logical can be trivially pulled out and implemented as a
3875 * standalone library. For now, this should suffice since there is no other
3876 * user of this interface.
3878 * Return 0 on success.
3879 * Return -1 on failure.
3882 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3884 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3889 struct uioc_timeout timeout;
3894 * Allow only one read at a time to go through the sysfs attributes
3896 mutex_lock(&raid_dev->sysfs_mtx);
3898 uioc = raid_dev->sysfs_uioc;
3899 mbox64 = raid_dev->sysfs_mbox64;
3900 ldmap = raid_dev->sysfs_buffer;
3902 memset(uioc, 0, sizeof(uioc_t));
3903 memset(mbox64, 0, sizeof(mbox64_t));
3904 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3906 mbox = &mbox64->mbox32;
3907 raw_mbox = (char *)mbox;
3908 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3909 uioc->buf_vaddr = (caddr_t)adapter;
3910 uioc->status = -ENODATA;
3911 uioc->done = megaraid_sysfs_get_ldmap_done;
3914 * Prepare the mailbox packet to get the current logical drive mapping
3917 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3919 raw_mbox[0] = FC_DEL_LOGDRV;
3920 raw_mbox[2] = OP_GET_LDID_MAP;
3923 * Setup a timer to recover from a non-responding controller
3925 timeout.uioc = uioc;
3926 timer_setup_on_stack(&timeout.timer,
3927 megaraid_sysfs_get_ldmap_timeout, 0);
3929 timeout.timer.expires = jiffies + 60 * HZ;
3930 add_timer(&timeout.timer);
3933 * Send the command to the firmware
3935 rval = megaraid_mbox_mm_command(adapter, uioc);
3937 if (rval == 0) { // command successfully issued
3938 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3941 * Check if the command timed out
3943 if (uioc->status == -ETIME) {
3944 con_log(CL_ANN, (KERN_NOTICE
3945 "megaraid: sysfs get ld map timed out\n"));
3950 rval = mbox->status;
3954 memcpy(raid_dev->curr_ldmap, ldmap,
3955 sizeof(raid_dev->curr_ldmap));
3958 con_log(CL_ANN, (KERN_NOTICE
3959 "megaraid: get ld map failed with %x\n", rval));
3963 con_log(CL_ANN, (KERN_NOTICE
3964 "megaraid: could not issue ldmap command:%x\n", rval));
3968 del_timer_sync(&timeout.timer);
3969 destroy_timer_on_stack(&timeout.timer);
3971 mutex_unlock(&raid_dev->sysfs_mtx);
3978 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
3979 * @cdev : class device object representation for the host
3980 * @buf : buffer to send data to
3982 * Display the handle used by the applications while executing management
3983 * tasks on the adapter. We invoke a management module API to get the adapter
3984 * handle, since we do not interface with applications directly.
3987 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
3990 struct Scsi_Host *shost = class_to_shost(dev);
3991 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
3994 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
3996 return snprintf(buf, 8, "%u\n", app_hndl);
4001 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4002 * @dev : device object representation for the scsi device
4003 * @attr : device attribute to show
4004 * @buf : buffer to send data to
4006 * Display the logical drive number for the device in question, if it a valid
4007 * logical drive. For physical devices, "-1" is returned.
4009 * The logical drive number is displayed in following format:
4011 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4013 * <int> <int> <int> <int>
4016 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4018 struct scsi_device *sdev = to_scsi_device(dev);
4019 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4020 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4022 int logical_drv = -1;
4024 uint32_t app_hndl = 0;
4029 if (raid_dev->random_del_supported &&
4030 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4032 rval = megaraid_sysfs_get_ldmap(adapter);
4035 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4037 mapped_sdev_id = sdev->id;
4039 if (sdev->id > adapter->init_id) {
4040 mapped_sdev_id -= 1;
4043 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4049 ldid_map = raid_dev->curr_ldmap[i];
4051 app_hndl = mraid_mm_adapter_app_handle(
4052 adapter->unique_id);
4059 con_log(CL_ANN, (KERN_NOTICE
4060 "megaraid: sysfs get ld map failed: %x\n",
4065 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4066 ldid_map, app_hndl);
4071 * END: Mailbox Low Level Driver
4073 module_init(megaraid_init);
4074 module_exit(megaraid_exit);
4076 /* vim: set ts=8 sw=8 tw=78 ai si: */