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 * ### global data ###
207 static uint8_t megaraid_mbox_version[8] =
208 { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
212 * PCI table for all supported controllers.
214 static struct pci_device_id pci_id_table_g[] = {
217 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
219 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
222 PCI_VENDOR_ID_LSI_LOGIC,
223 PCI_DEVICE_ID_PERC4_SC,
225 PCI_SUBSYS_ID_PERC4_SC,
228 PCI_VENDOR_ID_LSI_LOGIC,
229 PCI_DEVICE_ID_PERC4_DC,
231 PCI_SUBSYS_ID_PERC4_DC,
234 PCI_VENDOR_ID_LSI_LOGIC,
241 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
243 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
247 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
249 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
253 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
255 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
259 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
261 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
265 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
267 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
271 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
273 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
276 PCI_VENDOR_ID_LSI_LOGIC,
277 PCI_DEVICE_ID_DOBSON,
283 PCI_DEVICE_ID_AMI_MEGARAID3,
288 PCI_VENDOR_ID_LSI_LOGIC,
289 PCI_DEVICE_ID_AMI_MEGARAID3,
294 PCI_VENDOR_ID_LSI_LOGIC,
295 PCI_DEVICE_ID_LINDSAY,
299 {0} /* Terminating entry */
301 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
304 static struct pci_driver megaraid_pci_driver = {
306 .id_table = pci_id_table_g,
307 .probe = megaraid_probe_one,
308 .remove = megaraid_detach_one,
309 .shutdown = megaraid_mbox_shutdown,
314 // definitions for the device attributes for exporting logical drive number
315 // for a scsi address (Host, Channel, Id, Lun)
317 DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
320 // Host template initializer for megaraid mbox sysfs device attributes
321 static struct device_attribute *megaraid_shost_attrs[] = {
322 &dev_attr_megaraid_mbox_app_hndl,
327 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
329 // Host template initializer for megaraid mbox sysfs device attributes
330 static struct device_attribute *megaraid_sdev_attrs[] = {
331 &dev_attr_megaraid_mbox_ld,
336 * megaraid_change_queue_depth - Change the device's queue depth
337 * @sdev: scsi device struct
338 * @qdepth: depth to set
339 * @reason: calling context
344 static int megaraid_change_queue_depth(struct scsi_device *sdev, int qdepth,
347 if (reason != SCSI_QDEPTH_DEFAULT)
350 if (qdepth > MBOX_MAX_SCSI_CMDS)
351 qdepth = MBOX_MAX_SCSI_CMDS;
352 scsi_adjust_queue_depth(sdev, 0, qdepth);
353 return sdev->queue_depth;
357 * Scsi host template for megaraid unified driver
359 static struct scsi_host_template megaraid_template_g = {
360 .module = THIS_MODULE,
361 .name = "LSI Logic MegaRAID driver",
362 .proc_name = "megaraid",
363 .queuecommand = megaraid_queue_command,
364 .eh_abort_handler = megaraid_abort_handler,
365 .eh_device_reset_handler = megaraid_reset_handler,
366 .eh_bus_reset_handler = megaraid_reset_handler,
367 .eh_host_reset_handler = megaraid_reset_handler,
368 .change_queue_depth = megaraid_change_queue_depth,
369 .use_clustering = ENABLE_CLUSTERING,
371 .sdev_attrs = megaraid_sdev_attrs,
372 .shost_attrs = megaraid_shost_attrs,
377 * megaraid_init - module load hook
379 * We register ourselves as hotplug enabled module and let PCI subsystem
380 * discover our adapters.
387 // Announce the driver version
388 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
389 MEGARAID_EXT_VERSION));
391 // check validity of module parameters
392 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
394 con_log(CL_ANN, (KERN_WARNING
395 "megaraid mailbox: max commands per lun reset to %d\n",
396 MBOX_MAX_SCSI_CMDS));
398 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
402 // register as a PCI hot-plug driver module
403 rval = pci_register_driver(&megaraid_pci_driver);
405 con_log(CL_ANN, (KERN_WARNING
406 "megaraid: could not register hotplug support.\n"));
414 * megaraid_exit - driver unload entry point
416 * We simply unwrap the megaraid_init routine here.
421 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
423 // unregister as PCI hotplug driver
424 pci_unregister_driver(&megaraid_pci_driver);
431 * megaraid_probe_one - PCI hotplug entry point
432 * @pdev : handle to this controller's PCI configuration space
433 * @id : pci device id of the class of controllers
435 * This routine should be called whenever a new adapter is detected by the
436 * PCI hotplug susbsystem.
439 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
444 // detected a new controller
445 con_log(CL_ANN, (KERN_INFO
446 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
447 pdev->vendor, pdev->device, pdev->subsystem_vendor,
448 pdev->subsystem_device));
450 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
451 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
453 if (pci_enable_device(pdev)) {
454 con_log(CL_ANN, (KERN_WARNING
455 "megaraid: pci_enable_device failed\n"));
460 // Enable bus-mastering on this controller
461 pci_set_master(pdev);
463 // Allocate the per driver initialization structure
464 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
466 if (adapter == NULL) {
467 con_log(CL_ANN, (KERN_WARNING
468 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
474 // set up PCI related soft state and other pre-known parameters
475 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
476 adapter->irq = pdev->irq;
477 adapter->pdev = pdev;
479 atomic_set(&adapter->being_detached, 0);
481 // Setup the default DMA mask. This would be changed later on
482 // depending on hardware capabilities
483 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
485 con_log(CL_ANN, (KERN_WARNING
486 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
488 goto out_free_adapter;
492 // Initialize the synchronization lock for kernel and LLD
493 spin_lock_init(&adapter->lock);
495 // Initialize the command queues: the list of free SCBs and the list
497 INIT_LIST_HEAD(&adapter->kscb_pool);
498 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
500 INIT_LIST_HEAD(&adapter->pend_list);
501 spin_lock_init(PENDING_LIST_LOCK(adapter));
503 INIT_LIST_HEAD(&adapter->completed_list);
504 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
507 // Start the mailbox based controller
508 if (megaraid_init_mbox(adapter) != 0) {
509 con_log(CL_ANN, (KERN_WARNING
510 "megaraid: maibox adapter did not initialize\n"));
512 goto out_free_adapter;
515 // Register with LSI Common Management Module
516 if (megaraid_cmm_register(adapter) != 0) {
518 con_log(CL_ANN, (KERN_WARNING
519 "megaraid: could not register with management module\n"));
524 // setup adapter handle in PCI soft state
525 pci_set_drvdata(pdev, adapter);
527 // attach with scsi mid-layer
528 if (megaraid_io_attach(adapter) != 0) {
530 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
538 pci_set_drvdata(pdev, NULL);
539 megaraid_cmm_unregister(adapter);
541 megaraid_fini_mbox(adapter);
545 pci_disable_device(pdev);
552 * megaraid_detach_one - release framework resources and call LLD release routine
553 * @pdev : handle for our PCI cofiguration space
555 * This routine is called during driver unload. We free all the allocated
556 * resources and call the corresponding LLD so that it can also release all
559 * This routine is also called from the PCI hotplug system.
562 megaraid_detach_one(struct pci_dev *pdev)
565 struct Scsi_Host *host;
568 // Start a rollback on this adapter
569 adapter = pci_get_drvdata(pdev);
572 con_log(CL_ANN, (KERN_CRIT
573 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
574 pdev->vendor, pdev->device, pdev->subsystem_vendor,
575 pdev->subsystem_device));
580 con_log(CL_ANN, (KERN_NOTICE
581 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
582 pdev->vendor, pdev->device, pdev->subsystem_vendor,
583 pdev->subsystem_device));
587 host = adapter->host;
589 // do not allow any more requests from the management module for this
591 // FIXME: How do we account for the request which might still be
593 atomic_set(&adapter->being_detached, 1);
595 // detach from the IO sub-system
596 megaraid_io_detach(adapter);
598 // reset the device state in the PCI structure. We check this
599 // condition when we enter here. If the device state is NULL,
600 // that would mean the device has already been removed
601 pci_set_drvdata(pdev, NULL);
603 // Unregister from common management module
605 // FIXME: this must return success or failure for conditions if there
606 // is a command pending with LLD or not.
607 megaraid_cmm_unregister(adapter);
609 // finalize the mailbox based controller and release all resources
610 megaraid_fini_mbox(adapter);
616 pci_disable_device(pdev);
623 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
624 * @pdev : generic driver model device
626 * Shutdown notification, perform flush cache.
629 megaraid_mbox_shutdown(struct pci_dev *pdev)
631 adapter_t *adapter = pci_get_drvdata(pdev);
635 con_log(CL_ANN, (KERN_WARNING
636 "megaraid: null device in shutdown\n"));
641 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
644 megaraid_mbox_flush_cache(adapter);
646 con_log(CL_ANN, ("done\n"));
651 * megaraid_io_attach - attach a device with the IO subsystem
652 * @adapter : controller's soft state
654 * Attach this device with the IO subsystem.
657 megaraid_io_attach(adapter_t *adapter)
659 struct Scsi_Host *host;
661 // Initialize SCSI Host structure
662 host = scsi_host_alloc(&megaraid_template_g, 8);
664 con_log(CL_ANN, (KERN_WARNING
665 "megaraid mbox: scsi_register failed\n"));
670 SCSIHOST2ADAP(host) = (caddr_t)adapter;
671 adapter->host = host;
673 host->irq = adapter->irq;
674 host->unique_id = adapter->unique_id;
675 host->can_queue = adapter->max_cmds;
676 host->this_id = adapter->init_id;
677 host->sg_tablesize = adapter->sglen;
678 host->max_sectors = adapter->max_sectors;
679 host->cmd_per_lun = adapter->cmd_per_lun;
680 host->max_channel = adapter->max_channel;
681 host->max_id = adapter->max_target;
682 host->max_lun = adapter->max_lun;
685 // notify mid-layer about the new controller
686 if (scsi_add_host(host, &adapter->pdev->dev)) {
688 con_log(CL_ANN, (KERN_WARNING
689 "megaraid mbox: scsi_add_host failed\n"));
696 scsi_scan_host(host);
703 * megaraid_io_detach - detach a device from the IO subsystem
704 * @adapter : controller's soft state
706 * Detach this device from the IO subsystem.
709 megaraid_io_detach(adapter_t *adapter)
711 struct Scsi_Host *host;
713 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
715 host = adapter->host;
717 scsi_remove_host(host);
724 * START: Mailbox Low Level Driver
726 * This is section specific to the single mailbox based controllers
730 * megaraid_init_mbox - initialize controller
731 * @adapter : our soft state
733 * - Allocate 16-byte aligned mailbox memory for firmware handshake
734 * - Allocate controller's memory resources
735 * - Find out all initialization data
736 * - Allocate memory required for all the commands
737 * - Use internal library of FW routines, build up complete soft state
740 megaraid_init_mbox(adapter_t *adapter)
742 struct pci_dev *pdev;
743 mraid_device_t *raid_dev;
748 adapter->ito = MBOX_TIMEOUT;
749 pdev = adapter->pdev;
752 * Allocate and initialize the init data structure for mailbox
755 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
756 if (raid_dev == NULL) return -1;
760 * Attach the adapter soft state to raid device soft state
762 adapter->raid_device = (caddr_t)raid_dev;
763 raid_dev->fast_load = megaraid_fast_load;
767 raid_dev->baseport = pci_resource_start(pdev, 0);
769 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
771 con_log(CL_ANN, (KERN_WARNING
772 "megaraid: mem region busy\n"));
774 goto out_free_raid_dev;
777 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
779 if (!raid_dev->baseaddr) {
781 con_log(CL_ANN, (KERN_WARNING
782 "megaraid: could not map hba memory\n") );
784 goto out_release_regions;
787 /* initialize the mutual exclusion lock for the mailbox */
788 spin_lock_init(&raid_dev->mailbox_lock);
790 /* allocate memory required for commands */
791 if (megaraid_alloc_cmd_packets(adapter) != 0)
795 * Issue SYNC cmd to flush the pending cmds in the adapter
796 * and initialize its internal state
799 if (megaraid_mbox_fire_sync_cmd(adapter))
800 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
803 * Setup the rest of the soft state using the library of
807 /* request IRQ and register the interrupt service routine */
808 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
811 con_log(CL_ANN, (KERN_WARNING
812 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
818 if (megaraid_mbox_product_info(adapter) != 0)
821 // Do we support extended CDBs
822 adapter->max_cdb_sz = 10;
823 if (megaraid_mbox_extended_cdb(adapter) == 0) {
824 adapter->max_cdb_sz = 16;
828 * Do we support cluster environment, if we do, what is the initiator
830 * NOTE: In a non-cluster aware firmware environment, the LLD should
831 * return 7 as initiator id.
834 adapter->init_id = -1;
835 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
840 * Prepare the device ids array to have the mapping between the kernel
841 * device address and megaraid device address.
842 * We export the physical devices on their actual addresses. The
843 * logical drives are exported on a virtual SCSI channel
845 megaraid_mbox_setup_device_map(adapter);
847 // If the firmware supports random deletion, update the device id map
848 if (megaraid_mbox_support_random_del(adapter)) {
850 // Change the logical drives numbers in device_ids array one
851 // slot in device_ids is reserved for target id, that's why
853 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
854 adapter->device_ids[adapter->max_channel][i] += 0x80;
856 adapter->device_ids[adapter->max_channel][adapter->init_id] =
859 raid_dev->random_del_supported = 1;
863 * find out the maximum number of scatter-gather elements supported by
866 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
868 // enumerate RAID and SCSI channels so that all devices on SCSI
869 // channels can later be exported, including disk devices
870 megaraid_mbox_enum_raid_scsi(adapter);
873 * Other parameters required by upper layer
875 * maximum number of sectors per IO command
877 adapter->max_sectors = megaraid_max_sectors;
880 * number of queued commands per LUN.
882 adapter->cmd_per_lun = megaraid_cmd_per_lun;
885 * Allocate resources required to issue FW calls, when sysfs is
888 if (megaraid_sysfs_alloc_resources(adapter) != 0)
891 // Set the DMA mask to 64-bit. All supported controllers as capable of
893 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
895 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
896 ((adapter->pdev->subsystem_device !=
897 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
898 (adapter->pdev->subsystem_device !=
899 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
900 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
901 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
902 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
903 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
904 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
905 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
906 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
907 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
908 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
909 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
910 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
911 con_log(CL_ANN, (KERN_WARNING
912 "megaraid: DMA mask for 64-bit failed\n"));
914 if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
915 con_log(CL_ANN, (KERN_WARNING
916 "megaraid: 32-bit DMA mask failed\n"));
917 goto out_free_sysfs_res;
922 // setup tasklet for DPC
923 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
924 (unsigned long)adapter);
926 con_log(CL_DLEVEL1, (KERN_INFO
927 "megaraid mbox hba successfully initialized\n"));
932 megaraid_sysfs_free_resources(adapter);
934 free_irq(adapter->irq, adapter);
936 megaraid_free_cmd_packets(adapter);
938 iounmap(raid_dev->baseaddr);
940 pci_release_regions(pdev);
949 * megaraid_fini_mbox - undo controller initialization
950 * @adapter : our soft state
953 megaraid_fini_mbox(adapter_t *adapter)
955 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
958 megaraid_mbox_flush_cache(adapter);
960 tasklet_kill(&adapter->dpc_h);
962 megaraid_sysfs_free_resources(adapter);
964 megaraid_free_cmd_packets(adapter);
966 free_irq(adapter->irq, adapter);
968 iounmap(raid_dev->baseaddr);
970 pci_release_regions(adapter->pdev);
979 * megaraid_alloc_cmd_packets - allocate shared mailbox
980 * @adapter : soft state of the raid controller
982 * Allocate and align the shared mailbox. This maibox is used to issue
983 * all the commands. For IO based controllers, the mailbox is also regsitered
984 * with the FW. Allocate memory for all commands as well.
985 * This is our big allocator.
988 megaraid_alloc_cmd_packets(adapter_t *adapter)
990 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
991 struct pci_dev *pdev;
995 struct mraid_pci_blk *epthru_pci_blk;
996 struct mraid_pci_blk *sg_pci_blk;
997 struct mraid_pci_blk *mbox_pci_blk;
1000 pdev = adapter->pdev;
1004 * Allocate the common 16-byte aligned memory for the handshake
1007 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1008 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1010 if (!raid_dev->una_mbox64) {
1011 con_log(CL_ANN, (KERN_WARNING
1012 "megaraid: out of memory, %s %d\n", __func__,
1016 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1019 * Align the mailbox at 16-byte boundary
1021 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1023 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1026 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1028 align = ((void *)raid_dev->mbox -
1029 ((void *)&raid_dev->una_mbox64->mbox32));
1031 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1034 // Allocate memory for commands issued internally
1035 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1036 &adapter->ibuf_dma_h);
1037 if (!adapter->ibuf) {
1039 con_log(CL_ANN, (KERN_WARNING
1040 "megaraid: out of memory, %s %d\n", __func__,
1043 goto out_free_common_mbox;
1045 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1047 // Allocate memory for our SCSI Command Blocks and their associated
1051 * Allocate memory for the base list of scb. Later allocate memory for
1052 * CCBs and embedded components of each CCB and point the pointers in
1053 * scb to the allocated components
1054 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1055 * since the calling routine does not yet know the number of available
1058 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1060 if (adapter->kscb_list == NULL) {
1061 con_log(CL_ANN, (KERN_WARNING
1062 "megaraid: out of memory, %s %d\n", __func__,
1067 // memory allocation for our command packets
1068 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1069 con_log(CL_ANN, (KERN_WARNING
1070 "megaraid: out of memory, %s %d\n", __func__,
1072 goto out_free_scb_list;
1075 // Adjust the scb pointers and link in the free pool
1076 epthru_pci_blk = raid_dev->epthru_pool;
1077 sg_pci_blk = raid_dev->sg_pool;
1078 mbox_pci_blk = raid_dev->mbox_pool;
1080 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1081 scb = adapter->kscb_list + i;
1082 ccb = raid_dev->ccb_list + i;
1084 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1085 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1086 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1087 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1089 // make sure the mailbox is aligned properly
1090 if (ccb->mbox_dma_h & 0x0F) {
1091 con_log(CL_ANN, (KERN_CRIT
1092 "megaraid mbox: not aligned on 16-bytes\n"));
1094 goto out_teardown_dma_pools;
1097 ccb->epthru = (mraid_epassthru_t *)
1098 epthru_pci_blk[i].vaddr;
1099 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1100 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1101 ccb->pthru_dma_h = ccb->epthru_dma_h;
1104 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1105 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1106 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1108 scb->ccb = (caddr_t)ccb;
1111 scb->sno = i; // command index
1114 scb->state = SCB_FREE;
1115 scb->dma_direction = PCI_DMA_NONE;
1116 scb->dma_type = MRAID_DMA_NONE;
1117 scb->dev_channel = -1;
1118 scb->dev_target = -1;
1120 // put scb in the free pool
1121 list_add_tail(&scb->list, &adapter->kscb_pool);
1126 out_teardown_dma_pools:
1127 megaraid_mbox_teardown_dma_pools(adapter);
1129 kfree(adapter->kscb_list);
1131 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1132 adapter->ibuf_dma_h);
1133 out_free_common_mbox:
1134 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1135 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1142 * megaraid_free_cmd_packets - free memory
1143 * @adapter : soft state of the raid controller
1145 * Release memory resources allocated for commands.
1148 megaraid_free_cmd_packets(adapter_t *adapter)
1150 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1152 megaraid_mbox_teardown_dma_pools(adapter);
1154 kfree(adapter->kscb_list);
1156 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1157 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1159 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1160 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1166 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1167 * @adapter : HBA soft state
1169 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1170 * and scatter-gather lists.
1173 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1175 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1176 struct mraid_pci_blk *epthru_pci_blk;
1177 struct mraid_pci_blk *sg_pci_blk;
1178 struct mraid_pci_blk *mbox_pci_blk;
1183 // Allocate memory for 16-bytes aligned mailboxes
1184 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1186 sizeof(mbox64_t) + 16,
1189 if (raid_dev->mbox_pool_handle == NULL) {
1190 goto fail_setup_dma_pool;
1193 mbox_pci_blk = raid_dev->mbox_pool;
1194 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1195 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1196 raid_dev->mbox_pool_handle,
1198 &mbox_pci_blk[i].dma_addr);
1199 if (!mbox_pci_blk[i].vaddr) {
1200 goto fail_setup_dma_pool;
1205 * Allocate memory for each embedded passthru strucuture pointer
1206 * Request for a 128 bytes aligned structure for each passthru command
1208 * Since passthru and extended passthru commands are exclusive, they
1209 * share common memory pool. Passthru structures piggyback on memory
1210 * allocted to extended passthru since passthru is smaller of the two
1212 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1213 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1215 if (raid_dev->epthru_pool_handle == NULL) {
1216 goto fail_setup_dma_pool;
1219 epthru_pci_blk = raid_dev->epthru_pool;
1220 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1221 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1222 raid_dev->epthru_pool_handle,
1224 &epthru_pci_blk[i].dma_addr);
1225 if (!epthru_pci_blk[i].vaddr) {
1226 goto fail_setup_dma_pool;
1231 // Allocate memory for each scatter-gather list. Request for 512 bytes
1232 // alignment for each sg list
1233 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1235 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1238 if (raid_dev->sg_pool_handle == NULL) {
1239 goto fail_setup_dma_pool;
1242 sg_pci_blk = raid_dev->sg_pool;
1243 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1244 sg_pci_blk[i].vaddr = pci_pool_alloc(
1245 raid_dev->sg_pool_handle,
1247 &sg_pci_blk[i].dma_addr);
1248 if (!sg_pci_blk[i].vaddr) {
1249 goto fail_setup_dma_pool;
1255 fail_setup_dma_pool:
1256 megaraid_mbox_teardown_dma_pools(adapter);
1262 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1263 * @adapter : HBA soft state
1265 * Teardown the dma pool for mailbox, passthru and extended passthru
1266 * structures, and scatter-gather lists.
1269 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1271 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1272 struct mraid_pci_blk *epthru_pci_blk;
1273 struct mraid_pci_blk *sg_pci_blk;
1274 struct mraid_pci_blk *mbox_pci_blk;
1278 sg_pci_blk = raid_dev->sg_pool;
1279 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1280 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1281 sg_pci_blk[i].dma_addr);
1283 if (raid_dev->sg_pool_handle)
1284 pci_pool_destroy(raid_dev->sg_pool_handle);
1287 epthru_pci_blk = raid_dev->epthru_pool;
1288 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1289 pci_pool_free(raid_dev->epthru_pool_handle,
1290 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1292 if (raid_dev->epthru_pool_handle)
1293 pci_pool_destroy(raid_dev->epthru_pool_handle);
1296 mbox_pci_blk = raid_dev->mbox_pool;
1297 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1298 pci_pool_free(raid_dev->mbox_pool_handle,
1299 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1301 if (raid_dev->mbox_pool_handle)
1302 pci_pool_destroy(raid_dev->mbox_pool_handle);
1309 * megaraid_alloc_scb - detach and return a scb from the free list
1310 * @adapter : controller's soft state
1311 * @scp : pointer to the scsi command to be executed
1313 * Return the scb from the head of the free list. %NULL if there are none
1317 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1319 struct list_head *head = &adapter->kscb_pool;
1321 unsigned long flags;
1323 // detach scb from free pool
1324 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1326 if (list_empty(head)) {
1327 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1331 scb = list_entry(head->next, scb_t, list);
1332 list_del_init(&scb->list);
1334 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1336 scb->state = SCB_ACTIVE;
1338 scb->dma_type = MRAID_DMA_NONE;
1345 * megaraid_dealloc_scb - return the scb to the free pool
1346 * @adapter : controller's soft state
1347 * @scb : scb to be freed
1349 * Return the scb back to the free list of scbs. The caller must 'flush' the
1350 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1351 * NOTE NOTE: Make sure the scb is not on any list before calling this
1355 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1357 unsigned long flags;
1359 // put scb in the free pool
1360 scb->state = SCB_FREE;
1362 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1364 list_add(&scb->list, &adapter->kscb_pool);
1366 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1373 * megaraid_mbox_mksgl - make the scatter-gather list
1374 * @adapter : controller's soft state
1375 * @scb : scsi control block
1377 * Prepare the scatter-gather list.
1380 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1382 struct scatterlist *sgl;
1384 struct scsi_cmnd *scp;
1390 ccb = (mbox_ccb_t *)scb->ccb;
1392 sgcnt = scsi_dma_map(scp);
1393 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1395 // no mapping required if no data to be transferred
1399 scb->dma_type = MRAID_DMA_WSG;
1401 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1402 ccb->sgl64[i].address = sg_dma_address(sgl);
1403 ccb->sgl64[i].length = sg_dma_len(sgl);
1406 // Return count of SG nodes
1412 * mbox_post_cmd - issue a mailbox command
1413 * @adapter : controller's soft state
1414 * @scb : command to be issued
1416 * Post the command to the controller if mailbox is available.
1419 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1421 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1425 unsigned long flags;
1429 ccb = (mbox_ccb_t *)scb->ccb;
1430 mbox = raid_dev->mbox;
1431 mbox64 = raid_dev->mbox64;
1434 * Check for busy mailbox. If it is, return failure - the caller
1435 * should retry later.
1437 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1439 if (unlikely(mbox->busy)) {
1444 } while(mbox->busy && (i < max_mbox_busy_wait));
1448 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1455 // Copy this command's mailbox data into "adapter's" mailbox
1456 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1457 mbox->cmdid = scb->sno;
1459 adapter->outstanding_cmds++;
1461 if (scb->dma_direction == PCI_DMA_TODEVICE)
1462 pci_dma_sync_sg_for_device(adapter->pdev,
1463 scsi_sglist(scb->scp),
1464 scsi_sg_count(scb->scp),
1467 mbox->busy = 1; // Set busy
1472 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1474 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1481 * megaraid_queue_command - generic queue entry point for all LLDs
1482 * @scp : pointer to the scsi command to be executed
1483 * @done : callback routine to be called after the cmd has be completed
1485 * Queue entry point for mailbox based controllers.
1488 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1494 adapter = SCP2ADAPTER(scp);
1495 scp->scsi_done = done;
1499 * Allocate and build a SCB request
1500 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1501 * not allocate scb. We will return non-zero status in that case.
1502 * NOTE: scb can be null even though certain commands completed
1503 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1504 * return 0 in that case, and we would do the callback right away.
1507 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1508 if (!scb) { // command already completed
1513 megaraid_mbox_runpendq(adapter, scb);
1517 static DEF_SCSI_QCMD(megaraid_queue_command)
1520 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1521 * @adapter : controller's soft state
1522 * @scp : mid-layer scsi command pointer
1523 * @busy : set if request could not be completed because of lack of
1526 * Transform the mid-layer scsi command to megaraid firmware lingua.
1527 * Convert the command issued by mid-layer to format understood by megaraid
1528 * firmware. We also complete certain commands without sending them to firmware.
1531 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1533 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1538 mraid_passthru_t *pthru;
1542 char skip[] = "skipping";
1543 char scan[] = "scanning";
1548 * Get the appropriate device map for the device this command is
1551 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1554 * Logical drive commands
1557 switch (scp->cmnd[0]) {
1558 case TEST_UNIT_READY:
1560 * Do we support clustering and is the support enabled
1561 * If no, return success always
1564 scp->result = (DID_OK << 16);
1568 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1569 scp->result = (DID_ERROR << 16);
1574 scb->dma_direction = scp->sc_data_direction;
1575 scb->dev_channel = 0xFF;
1576 scb->dev_target = target;
1577 ccb = (mbox_ccb_t *)scb->ccb;
1580 * The command id will be provided by the command
1583 ccb->raw_mbox[0] = CLUSTER_CMD;
1584 ccb->raw_mbox[2] = RESERVATION_STATUS;
1585 ccb->raw_mbox[3] = target;
1591 struct scatterlist *sgl;
1594 sgl = scsi_sglist(scp);
1596 vaddr = (caddr_t) sg_virt(&sgl[0]);
1598 memset(vaddr, 0, scp->cmnd[4]);
1601 con_log(CL_ANN, (KERN_WARNING
1602 "megaraid mailbox: invalid sg:%d\n",
1606 scp->result = (DID_OK << 16);
1611 * Display the channel scan for logical drives
1612 * Do not display scan for a channel if already done.
1614 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1616 con_log(CL_ANN, (KERN_INFO
1617 "scsi[%d]: scanning scsi channel %d",
1618 adapter->host->host_no,
1622 " [virtual] for logical drives\n"));
1624 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1627 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1628 scp->sense_buffer[0] = 0x70;
1629 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1630 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1631 scp->result = CHECK_CONDITION << 1;
1639 * Do not allow LUN > 0 for logical drives and
1640 * requests for more than 40 logical drives
1643 scp->result = (DID_BAD_TARGET << 16);
1646 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1647 scp->result = (DID_BAD_TARGET << 16);
1652 /* Allocate a SCB and initialize passthru */
1653 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1654 scp->result = (DID_ERROR << 16);
1659 ccb = (mbox_ccb_t *)scb->ccb;
1660 scb->dev_channel = 0xFF;
1661 scb->dev_target = target;
1664 mbox64 = ccb->mbox64;
1668 pthru->reqsenselen = 14;
1669 pthru->islogical = 1;
1670 pthru->logdrv = target;
1671 pthru->cdblen = scp->cmd_len;
1672 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1674 mbox->cmd = MBOXCMD_PASSTHRU64;
1675 scb->dma_direction = scp->sc_data_direction;
1677 pthru->dataxferlen = scsi_bufflen(scp);
1678 pthru->dataxferaddr = ccb->sgl_dma_h;
1679 pthru->numsge = megaraid_mbox_mksgl(adapter,
1682 mbox->xferaddr = 0xFFFFFFFF;
1683 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1684 mbox64->xferaddr_hi = 0;
1696 * Allocate a SCB and initialize mailbox
1698 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1699 scp->result = (DID_ERROR << 16);
1703 ccb = (mbox_ccb_t *)scb->ccb;
1704 scb->dev_channel = 0xFF;
1705 scb->dev_target = target;
1707 mbox64 = ccb->mbox64;
1708 mbox->logdrv = target;
1711 * A little HACK: 2nd bit is zero for all scsi read
1712 * commands and is set for all scsi write commands
1714 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1718 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1720 if (scp->cmd_len == 6) {
1721 mbox->numsectors = (uint32_t)scp->cmnd[4];
1723 ((uint32_t)scp->cmnd[1] << 16) |
1724 ((uint32_t)scp->cmnd[2] << 8) |
1725 (uint32_t)scp->cmnd[3];
1727 mbox->lba &= 0x1FFFFF;
1731 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1733 else if (scp->cmd_len == 10) {
1735 (uint32_t)scp->cmnd[8] |
1736 ((uint32_t)scp->cmnd[7] << 8);
1738 ((uint32_t)scp->cmnd[2] << 24) |
1739 ((uint32_t)scp->cmnd[3] << 16) |
1740 ((uint32_t)scp->cmnd[4] << 8) |
1741 (uint32_t)scp->cmnd[5];
1745 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1747 else if (scp->cmd_len == 12) {
1749 ((uint32_t)scp->cmnd[2] << 24) |
1750 ((uint32_t)scp->cmnd[3] << 16) |
1751 ((uint32_t)scp->cmnd[4] << 8) |
1752 (uint32_t)scp->cmnd[5];
1755 ((uint32_t)scp->cmnd[6] << 24) |
1756 ((uint32_t)scp->cmnd[7] << 16) |
1757 ((uint32_t)scp->cmnd[8] << 8) |
1758 (uint32_t)scp->cmnd[9];
1761 con_log(CL_ANN, (KERN_WARNING
1762 "megaraid: unsupported CDB length\n"));
1764 megaraid_dealloc_scb(adapter, scb);
1766 scp->result = (DID_ERROR << 16);
1770 scb->dma_direction = scp->sc_data_direction;
1772 // Calculate Scatter-Gather info
1773 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1774 mbox->numsge = megaraid_mbox_mksgl(adapter,
1776 mbox->xferaddr = 0xFFFFFFFF;
1777 mbox64->xferaddr_hi = 0;
1784 * Do we support clustering and is the support enabled
1787 scp->result = (DID_BAD_TARGET << 16);
1792 * Allocate a SCB and initialize mailbox
1794 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1795 scp->result = (DID_ERROR << 16);
1800 ccb = (mbox_ccb_t *)scb->ccb;
1801 scb->dev_channel = 0xFF;
1802 scb->dev_target = target;
1803 ccb->raw_mbox[0] = CLUSTER_CMD;
1804 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1805 RESERVE_LD : RELEASE_LD;
1807 ccb->raw_mbox[3] = target;
1808 scb->dma_direction = scp->sc_data_direction;
1813 scp->result = (DID_BAD_TARGET << 16);
1817 else { // Passthru device commands
1819 // Do not allow access to target id > 15 or LUN > 7
1820 if (target > 15 || SCP2LUN(scp) > 7) {
1821 scp->result = (DID_BAD_TARGET << 16);
1825 // if fast load option was set and scan for last device is
1826 // over, reset the fast_load flag so that during a possible
1827 // next scan, devices can be made available
1828 if (rdev->fast_load && (target == 15) &&
1829 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1831 con_log(CL_ANN, (KERN_INFO
1832 "megaraid[%d]: physical device scan re-enabled\n",
1833 adapter->host->host_no));
1834 rdev->fast_load = 0;
1838 * Display the channel scan for physical devices
1840 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1842 ss = rdev->fast_load ? skip : scan;
1844 con_log(CL_ANN, (KERN_INFO
1845 "scsi[%d]: %s scsi channel %d [Phy %d]",
1846 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1850 " for non-raid devices\n"));
1852 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1855 // disable channel sweep if fast load option given
1856 if (rdev->fast_load) {
1857 scp->result = (DID_BAD_TARGET << 16);
1861 // Allocate a SCB and initialize passthru
1862 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1863 scp->result = (DID_ERROR << 16);
1868 ccb = (mbox_ccb_t *)scb->ccb;
1869 scb->dev_channel = channel;
1870 scb->dev_target = target;
1871 scb->dma_direction = scp->sc_data_direction;
1873 mbox64 = ccb->mbox64;
1875 // Does this firmware support extended CDBs
1876 if (adapter->max_cdb_sz == 16) {
1877 mbox->cmd = MBOXCMD_EXTPTHRU;
1879 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1881 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1882 mbox64->xferaddr_hi = 0;
1883 mbox->xferaddr = 0xFFFFFFFF;
1886 mbox->cmd = MBOXCMD_PASSTHRU64;
1888 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1890 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1891 mbox64->xferaddr_hi = 0;
1892 mbox->xferaddr = 0xFFFFFFFF;
1902 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1903 * @adapter : controller's soft state
1904 * @scb_q : SCB to be queued in the pending list
1906 * Scan the pending list for commands which are not yet issued and try to
1907 * post to the controller. The SCB can be a null pointer, which would indicate
1908 * no SCB to be queue, just try to execute the ones in the pending list.
1910 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1911 * out from the head of the pending list. If it is successfully issued, the
1912 * next SCB is at the head now.
1915 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1918 unsigned long flags;
1920 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1923 scb_q->state = SCB_PENDQ;
1924 list_add_tail(&scb_q->list, &adapter->pend_list);
1927 // if the adapter in not in quiescent mode, post the commands to FW
1928 if (adapter->quiescent) {
1929 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1933 while (!list_empty(&adapter->pend_list)) {
1935 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1937 scb = list_entry(adapter->pend_list.next, scb_t, list);
1939 // remove the scb from the pending list and try to
1940 // issue. If we are unable to issue it, put back in
1941 // the pending list and return
1943 list_del_init(&scb->list);
1945 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1947 // if mailbox was busy, return SCB back to pending
1948 // list. Make sure to add at the head, since that's
1949 // where it would have been removed from
1951 scb->state = SCB_ISSUED;
1953 if (mbox_post_cmd(adapter, scb) != 0) {
1955 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1957 scb->state = SCB_PENDQ;
1959 list_add(&scb->list, &adapter->pend_list);
1961 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1967 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1970 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1978 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1979 * @adapter : pointer to controller's soft state
1980 * @scb : scsi control block
1981 * @scp : scsi command from the mid-layer
1983 * Prepare a command for the scsi physical devices.
1986 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1987 struct scsi_cmnd *scp)
1990 mraid_passthru_t *pthru;
1994 ccb = (mbox_ccb_t *)scb->ccb;
1996 channel = scb->dev_channel;
1997 target = scb->dev_target;
1999 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2002 pthru->islogical = 0;
2004 pthru->target = (channel << 4) | target;
2005 pthru->logdrv = SCP2LUN(scp);
2006 pthru->reqsenselen = 14;
2007 pthru->cdblen = scp->cmd_len;
2009 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2011 if (scsi_bufflen(scp)) {
2012 pthru->dataxferlen = scsi_bufflen(scp);
2013 pthru->dataxferaddr = ccb->sgl_dma_h;
2014 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2017 pthru->dataxferaddr = 0;
2018 pthru->dataxferlen = 0;
2026 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2027 * @adapter : pointer to controller's soft state
2028 * @scb : scsi control block
2029 * @scp : scsi command from the mid-layer
2031 * Prepare a command for the scsi physical devices. This rountine prepares
2032 * commands for devices which can take extended CDBs (>10 bytes).
2035 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2036 struct scsi_cmnd *scp)
2039 mraid_epassthru_t *epthru;
2043 ccb = (mbox_ccb_t *)scb->ccb;
2044 epthru = ccb->epthru;
2045 channel = scb->dev_channel;
2046 target = scb->dev_target;
2048 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2049 epthru->timeout = 4;
2051 epthru->islogical = 0;
2052 epthru->channel = 0;
2053 epthru->target = (channel << 4) | target;
2054 epthru->logdrv = SCP2LUN(scp);
2055 epthru->reqsenselen = 14;
2056 epthru->cdblen = scp->cmd_len;
2058 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2060 if (scsi_bufflen(scp)) {
2061 epthru->dataxferlen = scsi_bufflen(scp);
2062 epthru->dataxferaddr = ccb->sgl_dma_h;
2063 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2066 epthru->dataxferaddr = 0;
2067 epthru->dataxferlen = 0;
2075 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2076 * @adapter : controller's soft state
2078 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2079 * completed command and put them on the completed list for later processing.
2081 * Returns: 1 if the interrupt is valid, 0 otherwise
2084 megaraid_ack_sequence(adapter_t *adapter)
2086 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2090 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2091 struct list_head clist;
2094 unsigned long flags;
2098 mbox = raid_dev->mbox;
2100 // move the SCBs from the firmware completed array to our local list
2101 INIT_LIST_HEAD(&clist);
2103 // loop till F/W has more commands for us to complete
2105 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2108 * Check if a valid interrupt is pending. If found, force the
2109 * interrupt line low.
2111 dword = RDOUTDOOR(raid_dev);
2112 if (dword != 0x10001234) break;
2116 WROUTDOOR(raid_dev, 0x10001234);
2119 // wait for valid numstatus to post
2120 for (i = 0; i < 0xFFFFF; i++) {
2121 if (mbox->numstatus != 0xFF) {
2122 nstatus = mbox->numstatus;
2127 mbox->numstatus = 0xFF;
2129 adapter->outstanding_cmds -= nstatus;
2131 for (i = 0; i < nstatus; i++) {
2133 // wait for valid command index to post
2134 for (j = 0; j < 0xFFFFF; j++) {
2135 if (mbox->completed[i] != 0xFF) break;
2138 completed[i] = mbox->completed[i];
2139 mbox->completed[i] = 0xFF;
2141 if (completed[i] == 0xFF) {
2142 con_log(CL_ANN, (KERN_CRIT
2143 "megaraid: command posting timed out\n"));
2149 // Get SCB associated with this command id
2150 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2152 scb = adapter->uscb_list + (completed[i] -
2153 MBOX_MAX_SCSI_CMDS);
2157 scb = adapter->kscb_list + completed[i];
2160 scb->status = mbox->status;
2161 list_add_tail(&scb->list, &clist);
2164 // Acknowledge interrupt
2165 WRINDOOR(raid_dev, 0x02);
2169 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2172 // put the completed commands in the completed list. DPC would
2173 // complete these commands later
2174 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2176 list_splice(&clist, &adapter->completed_list);
2178 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2181 // schedule the DPC if there is some work for it
2183 tasklet_schedule(&adapter->dpc_h);
2190 * megaraid_isr - isr for memory based mailbox based controllers
2192 * @devp : pointer to our soft state
2194 * Interrupt service routine for memory-mapped mailbox controllers.
2197 megaraid_isr(int irq, void *devp)
2199 adapter_t *adapter = devp;
2202 handled = megaraid_ack_sequence(adapter);
2204 /* Loop through any pending requests */
2205 if (!adapter->quiescent) {
2206 megaraid_mbox_runpendq(adapter, NULL);
2209 return IRQ_RETVAL(handled);
2214 * megaraid_mbox_sync_scb - sync kernel buffers
2215 * @adapter : controller's soft state
2216 * @scb : pointer to the resource packet
2218 * DMA sync if required.
2221 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2225 ccb = (mbox_ccb_t *)scb->ccb;
2227 if (scb->dma_direction == PCI_DMA_FROMDEVICE)
2228 pci_dma_sync_sg_for_cpu(adapter->pdev,
2229 scsi_sglist(scb->scp),
2230 scsi_sg_count(scb->scp),
2231 PCI_DMA_FROMDEVICE);
2233 scsi_dma_unmap(scb->scp);
2239 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2240 * @devp : pointer to HBA soft state
2242 * Pick up the commands from the completed list and send back to the owners.
2243 * This is a reentrant function and does not assume any locks are held while
2244 * it is being called.
2247 megaraid_mbox_dpc(unsigned long devp)
2249 adapter_t *adapter = (adapter_t *)devp;
2250 mraid_device_t *raid_dev;
2251 struct list_head clist;
2252 struct scatterlist *sgl;
2255 struct scsi_cmnd *scp;
2256 mraid_passthru_t *pthru;
2257 mraid_epassthru_t *epthru;
2263 unsigned long flags;
2269 if (!adapter) return;
2271 raid_dev = ADAP2RAIDDEV(adapter);
2273 // move the SCBs from the completed list to our local list
2274 INIT_LIST_HEAD(&clist);
2276 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2278 list_splice_init(&adapter->completed_list, &clist);
2280 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2283 list_for_each_entry_safe(scb, tmp, &clist, list) {
2285 status = scb->status;
2287 ccb = (mbox_ccb_t *)scb->ccb;
2289 epthru = ccb->epthru;
2292 // Make sure f/w has completed a valid command
2293 if (scb->state != SCB_ISSUED) {
2294 con_log(CL_ANN, (KERN_CRIT
2295 "megaraid critical err: invalid command %d:%d:%p\n",
2296 scb->sno, scb->state, scp));
2298 continue; // Must never happen!
2301 // check for the management command and complete it right away
2302 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2303 scb->state = SCB_FREE;
2304 scb->status = status;
2306 // remove from local clist
2307 list_del_init(&scb->list);
2309 kioc = (uioc_t *)scb->gp;
2312 megaraid_mbox_mm_done(adapter, scb);
2317 // Was an abort issued for this command earlier
2318 if (scb->state & SCB_ABORT) {
2319 con_log(CL_ANN, (KERN_NOTICE
2320 "megaraid: aborted cmd [%x] completed\n",
2325 * If the inquiry came of a disk drive which is not part of
2326 * any RAID array, expose it to the kernel. For this to be
2327 * enabled, user must set the "megaraid_expose_unconf_disks"
2328 * flag to 1 by specifying it on module parameter list.
2329 * This would enable data migration off drives from other
2332 islogical = MRAID_IS_LOGICAL(adapter, scp);
2333 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2334 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2336 sgl = scsi_sglist(scp);
2338 c = *(unsigned char *) sg_virt(&sgl[0]);
2340 con_log(CL_ANN, (KERN_WARNING
2341 "megaraid mailbox: invalid sg:%d\n",
2346 if ((c & 0x1F ) == TYPE_DISK) {
2347 pdev_index = (scb->dev_channel * 16) +
2350 raid_dev->pdrv_state[pdev_index] & 0x0F;
2352 if (pdev_state == PDRV_ONLINE ||
2353 pdev_state == PDRV_FAILED ||
2354 pdev_state == PDRV_RBLD ||
2355 pdev_state == PDRV_HOTSPARE ||
2356 megaraid_expose_unconf_disks == 0) {
2363 // Convert MegaRAID status to Linux error code
2368 scp->result = (DID_OK << 16);
2373 /* set sense_buffer and result fields */
2374 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2375 mbox->cmd == MBOXCMD_PASSTHRU64) {
2377 memcpy(scp->sense_buffer, pthru->reqsensearea,
2380 scp->result = DRIVER_SENSE << 24 |
2381 DID_OK << 16 | CHECK_CONDITION << 1;
2384 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2386 memcpy(scp->sense_buffer,
2387 epthru->reqsensearea, 14);
2389 scp->result = DRIVER_SENSE << 24 |
2391 CHECK_CONDITION << 1;
2393 scp->sense_buffer[0] = 0x70;
2394 scp->sense_buffer[2] = ABORTED_COMMAND;
2395 scp->result = CHECK_CONDITION << 1;
2402 scp->result = DID_BUS_BUSY << 16 | status;
2408 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2411 if (scp->cmnd[0] == TEST_UNIT_READY) {
2412 scp->result = DID_ERROR << 16 |
2413 RESERVATION_CONFLICT << 1;
2417 * Error code returned is 1 if Reserve or Release
2418 * failed or the input parameter is invalid
2420 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2421 scp->cmnd[0] == RELEASE)) {
2423 scp->result = DID_ERROR << 16 |
2424 RESERVATION_CONFLICT << 1;
2427 scp->result = DID_BAD_TARGET << 16 | status;
2431 // print a debug message for all failed commands
2433 megaraid_mbox_display_scb(adapter, scb);
2436 // Free our internal resources and call the mid-layer callback
2438 megaraid_mbox_sync_scb(adapter, scb);
2440 // remove from local clist
2441 list_del_init(&scb->list);
2443 // put back in free list
2444 megaraid_dealloc_scb(adapter, scb);
2446 // send the scsi packet back to kernel
2447 scp->scsi_done(scp);
2455 * megaraid_abort_handler - abort the scsi command
2456 * @scp : command to be aborted
2458 * Abort a previous SCSI request. Only commands on the pending list can be
2459 * aborted. All the commands issued to the F/W must complete.
2462 megaraid_abort_handler(struct scsi_cmnd *scp)
2465 mraid_device_t *raid_dev;
2469 unsigned long flags;
2473 adapter = SCP2ADAPTER(scp);
2474 raid_dev = ADAP2RAIDDEV(adapter);
2476 con_log(CL_ANN, (KERN_WARNING
2477 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2478 scp->cmnd[0], SCP2CHANNEL(scp),
2479 SCP2TARGET(scp), SCP2LUN(scp)));
2481 // If FW has stopped responding, simply return failure
2482 if (raid_dev->hw_error) {
2483 con_log(CL_ANN, (KERN_NOTICE
2484 "megaraid: hw error, not aborting\n"));
2488 // There might a race here, where the command was completed by the
2489 // firmware and now it is on the completed list. Before we could
2490 // complete the command to the kernel in dpc, the abort came.
2491 // Find out if this is the case to avoid the race.
2493 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2494 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2496 if (scb->scp == scp) { // Found command
2498 list_del_init(&scb->list); // from completed list
2500 con_log(CL_ANN, (KERN_WARNING
2501 "megaraid: %d[%d:%d], abort from completed list\n",
2502 scb->sno, scb->dev_channel, scb->dev_target));
2504 scp->result = (DID_ABORT << 16);
2505 scp->scsi_done(scp);
2507 megaraid_dealloc_scb(adapter, scb);
2509 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2515 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2518 // Find out if this command is still on the pending list. If it is and
2519 // was never issued, abort and return success. If the command is owned
2520 // by the firmware, we must wait for it to complete by the FW.
2521 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2522 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2524 if (scb->scp == scp) { // Found command
2526 list_del_init(&scb->list); // from pending list
2528 ASSERT(!(scb->state & SCB_ISSUED));
2530 con_log(CL_ANN, (KERN_WARNING
2531 "megaraid abort: [%d:%d], driver owner\n",
2532 scb->dev_channel, scb->dev_target));
2534 scp->result = (DID_ABORT << 16);
2535 scp->scsi_done(scp);
2537 megaraid_dealloc_scb(adapter, scb);
2539 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2545 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2548 // Check do we even own this command, in which case this would be
2549 // owned by the firmware. The only way to locate the FW scb is to
2550 // traverse through the list of all SCB, since driver does not
2551 // maintain these SCBs on any list
2553 spin_lock_irq(&adapter->lock);
2554 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2555 scb = adapter->kscb_list + i;
2557 if (scb->scp == scp) {
2561 if (!(scb->state & SCB_ISSUED)) {
2562 con_log(CL_ANN, (KERN_WARNING
2563 "megaraid abort: %d[%d:%d], invalid state\n",
2564 scb->sno, scb->dev_channel, scb->dev_target));
2568 con_log(CL_ANN, (KERN_WARNING
2569 "megaraid abort: %d[%d:%d], fw owner\n",
2570 scb->sno, scb->dev_channel, scb->dev_target));
2574 spin_unlock_irq(&adapter->lock);
2577 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2579 // FIXME: Should there be a callback for this command?
2583 // We cannot actually abort a command owned by firmware, return
2584 // failure and wait for reset. In host reset handler, we will find out
2585 // if the HBA is still live
2590 * megaraid_reset_handler - device reset hadler for mailbox based driver
2591 * @scp : reference command
2593 * Reset handler for the mailbox based controller. First try to find out if
2594 * the FW is still live, in which case the outstanding commands counter mut go
2595 * down to 0. If that happens, also issue the reservation reset command to
2596 * relinquish (possible) reservations on the logical drives connected to this
2600 megaraid_reset_handler(struct scsi_cmnd *scp)
2605 mraid_device_t *raid_dev;
2606 unsigned long flags;
2607 uint8_t raw_mbox[sizeof(mbox_t)];
2609 int recovery_window;
2614 adapter = SCP2ADAPTER(scp);
2615 raid_dev = ADAP2RAIDDEV(adapter);
2617 // return failure if adapter is not responding
2618 if (raid_dev->hw_error) {
2619 con_log(CL_ANN, (KERN_NOTICE
2620 "megaraid: hw error, cannot reset\n"));
2625 // Under exceptional conditions, FW can take up to 3 minutes to
2626 // complete command processing. Wait for additional 2 minutes for the
2627 // pending commands counter to go down to 0. If it doesn't, let the
2628 // controller be marked offline
2629 // Also, reset all the commands currently owned by the driver
2630 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2631 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2632 list_del_init(&scb->list); // from pending list
2634 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2635 con_log(CL_ANN, (KERN_WARNING
2636 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2637 scb->sno, scb->dev_channel, scb->dev_target));
2641 kioc = (uioc_t *)scb->gp;
2642 kioc->status = -EFAULT;
2644 megaraid_mbox_mm_done(adapter, scb);
2646 if (scb->scp == scp) { // Found command
2647 con_log(CL_ANN, (KERN_WARNING
2648 "megaraid: %d[%d:%d], reset from pending list\n",
2649 scb->sno, scb->dev_channel, scb->dev_target));
2651 con_log(CL_ANN, (KERN_WARNING
2652 "megaraid: IO packet with %d[%d:%d] being reset\n",
2653 scb->sno, scb->dev_channel, scb->dev_target));
2656 scb->scp->result = (DID_RESET << 16);
2657 scb->scp->scsi_done(scb->scp);
2659 megaraid_dealloc_scb(adapter, scb);
2662 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2664 if (adapter->outstanding_cmds) {
2665 con_log(CL_ANN, (KERN_NOTICE
2666 "megaraid: %d outstanding commands. Max wait %d sec\n",
2667 adapter->outstanding_cmds,
2668 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2671 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2673 recovering = adapter->outstanding_cmds;
2675 for (i = 0; i < recovery_window; i++) {
2677 megaraid_ack_sequence(adapter);
2679 // print a message once every 5 seconds only
2682 "megaraid mbox: Wait for %d commands to complete:%d\n",
2683 adapter->outstanding_cmds,
2684 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2687 // bailout if no recovery happened in reset time
2688 if (adapter->outstanding_cmds == 0) {
2695 spin_lock(&adapter->lock);
2697 // If still outstanding commands, bail out
2698 if (adapter->outstanding_cmds) {
2699 con_log(CL_ANN, (KERN_WARNING
2700 "megaraid mbox: critical hardware error!\n"));
2702 raid_dev->hw_error = 1;
2708 con_log(CL_ANN, (KERN_NOTICE
2709 "megaraid mbox: reset sequence completed successfully\n"));
2713 // If the controller supports clustering, reset reservations
2719 // clear reservations if any
2720 raw_mbox[0] = CLUSTER_CMD;
2721 raw_mbox[2] = RESET_RESERVATIONS;
2724 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2726 (KERN_INFO "megaraid: reservation reset\n"));
2730 con_log(CL_ANN, (KERN_WARNING
2731 "megaraid: reservation reset failed\n"));
2735 spin_unlock(&adapter->lock);
2740 * START: internal commands library
2742 * This section of the driver has the common routine used by the driver and
2743 * also has all the FW routines
2747 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2748 * @adapter : controller's soft state
2749 * @raw_mbox : the mailbox
2751 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2755 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2757 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2764 mbox64 = raid_dev->mbox64;
2765 mbox = raid_dev->mbox;
2768 * Wait until mailbox is free
2770 if (megaraid_busywait_mbox(raid_dev) != 0)
2771 goto blocked_mailbox;
2774 * Copy mailbox data into host structure
2776 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2781 mbox->numstatus = 0xFF;
2782 mbox->status = 0xFF;
2785 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2787 // wait for maximum 1 second for status to post. If the status is not
2788 // available within 1 second, assume FW is initializing and wait
2789 // for an extended amount of time
2790 if (mbox->numstatus == 0xFF) { // status not yet available
2793 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2800 con_log(CL_ANN, (KERN_NOTICE
2801 "megaraid mailbox: wait for FW to boot "));
2803 for (i = 0; (mbox->numstatus == 0xFF) &&
2804 (i < MBOX_RESET_WAIT); i++) {
2806 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2807 MBOX_RESET_WAIT - i));
2811 if (i == MBOX_RESET_WAIT) {
2814 "\nmegaraid mailbox: status not available\n"));
2818 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2822 // wait for maximum 1 second for poll semaphore
2823 if (mbox->poll != 0x77) {
2826 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2832 con_log(CL_ANN, (KERN_WARNING
2833 "megaraid mailbox: could not get poll semaphore\n"));
2838 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2841 // wait for maximum 1 second for acknowledgement
2842 if (RDINDOOR(raid_dev) & 0x2) {
2845 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2851 con_log(CL_ANN, (KERN_WARNING
2852 "megaraid mailbox: could not acknowledge\n"));
2859 status = mbox->status;
2861 // invalidate the completed command id array. After command
2862 // completion, firmware would write the valid id.
2863 mbox->numstatus = 0xFF;
2864 mbox->status = 0xFF;
2865 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2866 mbox->completed[i] = 0xFF;
2873 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2879 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2880 * @adapter : controller's soft state
2881 * @raw_mbox : the mailbox
2883 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2884 * controllers. This is a faster version of the synchronous command and
2885 * therefore can be called in interrupt-context as well.
2888 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2890 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2895 mbox = raid_dev->mbox;
2897 // return immediately if the mailbox is busy
2898 if (mbox->busy) return -1;
2900 // Copy mailbox data into host structure
2901 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2906 mbox->numstatus = 0xFF;
2907 mbox->status = 0xFF;
2910 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2912 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2913 if (mbox->numstatus != 0xFF) break;
2915 udelay(MBOX_SYNC_DELAY_200);
2918 if (i == MBOX_SYNC_WAIT_CNT) {
2919 // We may need to re-calibrate the counter
2920 con_log(CL_ANN, (KERN_CRIT
2921 "megaraid: fast sync command timed out\n"));
2924 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2927 return mbox->status;
2932 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2933 * @raid_dev : RAID device (HBA) soft state
2935 * Wait until the controller's mailbox is available to accept more commands.
2936 * Wait for at most 1 second.
2939 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2941 mbox_t *mbox = raid_dev->mbox;
2946 for (i = 0; mbox->busy && i < 1000; i++)
2950 if (i < 1000) return 0;
2956 * megaraid_mbox_product_info - some static information about the controller
2957 * @adapter : our soft state
2959 * Issue commands to the controller to grab some parameters required by our
2963 megaraid_mbox_product_info(adapter_t *adapter)
2965 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2967 uint8_t raw_mbox[sizeof(mbox_t)];
2968 mraid_pinfo_t *pinfo;
2969 dma_addr_t pinfo_dma_h;
2970 mraid_inquiry3_t *mraid_inq3;
2974 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2975 mbox = (mbox_t *)raw_mbox;
2978 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2979 * e.g., max channels, max commands etc.
2981 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
2984 if (pinfo == NULL) {
2985 con_log(CL_ANN, (KERN_WARNING
2986 "megaraid: out of memory, %s %d\n", __func__,
2991 memset(pinfo, 0, sizeof(mraid_pinfo_t));
2993 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2994 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2996 raw_mbox[0] = FC_NEW_CONFIG;
2997 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2998 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3000 // Issue the command
3001 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3003 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3005 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3006 pinfo, pinfo_dma_h);
3012 * Collect information about state of each physical drive
3013 * attached to the controller. We will expose all the disks
3014 * which are not part of RAID
3016 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3017 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3018 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3022 * Get product info for information like number of channels,
3023 * maximum commands supported.
3025 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3026 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3028 raw_mbox[0] = FC_NEW_CONFIG;
3029 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3031 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3033 con_log(CL_ANN, (KERN_WARNING
3034 "megaraid: product info failed\n"));
3036 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3037 pinfo, pinfo_dma_h);
3043 * Setup some parameters for host, as required by our caller
3045 adapter->max_channel = pinfo->nchannels;
3048 * we will export all the logical drives on a single channel.
3049 * Add 1 since inquires do not come for inititor ID
3051 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3052 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3055 * These are the maximum outstanding commands for the scsi-layer
3057 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3059 memset(adapter->fw_version, 0, VERSION_SIZE);
3060 memset(adapter->bios_version, 0, VERSION_SIZE);
3062 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3063 adapter->fw_version[4] = 0;
3065 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3066 adapter->bios_version[4] = 0;
3068 con_log(CL_ANN, (KERN_NOTICE
3069 "megaraid: fw version:[%s] bios version:[%s]\n",
3070 adapter->fw_version, adapter->bios_version));
3072 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3081 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3082 * @adapter : soft state for the controller
3084 * This routine check whether the controller in question supports extended
3085 * ( > 10 bytes ) CDBs.
3088 megaraid_mbox_extended_cdb(adapter_t *adapter)
3091 uint8_t raw_mbox[sizeof(mbox_t)];
3094 mbox = (mbox_t *)raw_mbox;
3096 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3097 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3099 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3101 raw_mbox[0] = MAIN_MISC_OPCODE;
3102 raw_mbox[2] = SUPPORT_EXT_CDB;
3108 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3117 * megaraid_mbox_support_ha - Do we support clustering
3118 * @adapter : soft state for the controller
3119 * @init_id : ID of the initiator
3121 * Determine if the firmware supports clustering and the ID of the initiator.
3124 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3127 uint8_t raw_mbox[sizeof(mbox_t)];
3131 mbox = (mbox_t *)raw_mbox;
3133 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3135 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3137 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3139 raw_mbox[0] = GET_TARGET_ID;
3141 // Issue the command
3144 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3146 *init_id = *(uint8_t *)adapter->ibuf;
3148 con_log(CL_ANN, (KERN_INFO
3149 "megaraid: cluster firmware, initiator ID: %d\n",
3160 * megaraid_mbox_support_random_del - Do we support random deletion
3161 * @adapter : soft state for the controller
3163 * Determine if the firmware supports random deletion.
3164 * Return: 1 is operation supported, 0 otherwise
3167 megaraid_mbox_support_random_del(adapter_t *adapter)
3170 uint8_t raw_mbox[sizeof(mbox_t)];
3174 * Newer firmware on Dell CERC expect a different
3175 * random deletion handling, so disable it.
3177 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3178 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3179 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3180 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3181 (adapter->fw_version[0] > '6' ||
3182 (adapter->fw_version[0] == '6' &&
3183 adapter->fw_version[2] > '6') ||
3184 (adapter->fw_version[0] == '6'
3185 && adapter->fw_version[2] == '6'
3186 && adapter->fw_version[3] > '1'))) {
3187 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3191 mbox = (mbox_t *)raw_mbox;
3193 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3195 raw_mbox[0] = FC_DEL_LOGDRV;
3196 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3198 // Issue the command
3200 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3202 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3212 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3213 * @adapter : soft state for the controller
3215 * Find out the maximum number of scatter-gather elements supported by the
3219 megaraid_mbox_get_max_sg(adapter_t *adapter)
3222 uint8_t raw_mbox[sizeof(mbox_t)];
3226 mbox = (mbox_t *)raw_mbox;
3228 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3230 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3232 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3234 raw_mbox[0] = MAIN_MISC_OPCODE;
3235 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3237 // Issue the command
3238 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3239 nsg = *(uint8_t *)adapter->ibuf;
3242 nsg = MBOX_DEFAULT_SG_SIZE;
3245 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3252 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3253 * @adapter : soft state for the controller
3255 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3256 * can be exported as regular SCSI channels.
3259 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3261 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3263 uint8_t raw_mbox[sizeof(mbox_t)];
3266 mbox = (mbox_t *)raw_mbox;
3268 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3270 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3272 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3274 raw_mbox[0] = CHNL_CLASS;
3275 raw_mbox[2] = GET_CHNL_CLASS;
3277 // Issue the command. If the command fails, all channels are RAID
3279 raid_dev->channel_class = 0xFF;
3280 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3281 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3289 * megaraid_mbox_flush_cache - flush adapter and disks cache
3290 * @adapter : soft state for the controller
3292 * Flush adapter cache followed by disks cache.
3295 megaraid_mbox_flush_cache(adapter_t *adapter)
3298 uint8_t raw_mbox[sizeof(mbox_t)];
3301 mbox = (mbox_t *)raw_mbox;
3303 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3305 raw_mbox[0] = FLUSH_ADAPTER;
3307 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3308 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3311 raw_mbox[0] = FLUSH_SYSTEM;
3313 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3314 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3322 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3323 * @adapter : soft state for the controller
3325 * Clears the pending cmds in FW and reinits its RAID structs.
3328 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3331 uint8_t raw_mbox[sizeof(mbox_t)];
3332 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3338 mbox = (mbox_t *)raw_mbox;
3340 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3344 mbox64 = raid_dev->mbox64;
3345 mbox = raid_dev->mbox;
3347 /* Wait until mailbox is free */
3348 if (megaraid_busywait_mbox(raid_dev) != 0) {
3350 goto blocked_mailbox;
3353 /* Copy mailbox data into host structure */
3354 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3359 mbox->numstatus = 0;
3363 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3365 /* Wait for maximum 1 min for status to post.
3366 * If the Firmware SUPPORTS the ABOVE COMMAND,
3367 * mbox->cmd will be set to 0
3369 * the firmware will reject the command with
3370 * mbox->numstatus set to 1
3375 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3379 if (i > 1000 * 60) {
3384 if (mbox->numstatus == 1)
3385 status = 1; /*cmd not supported*/
3387 /* Check for interrupt line */
3388 dword = RDOUTDOOR(raid_dev);
3389 WROUTDOOR(raid_dev, dword);
3390 WRINDOOR(raid_dev,2);
3395 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3400 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3401 * @adapter : controller's soft state
3402 * @scb : SCB to be displayed
3403 * @level : debug level for console print
3405 * Diplay information about the given SCB iff the current debug level is
3409 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3412 struct scsi_cmnd *scp;
3418 ccb = (mbox_ccb_t *)scb->ccb;
3424 con_log(level, (KERN_NOTICE
3425 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3426 mbox->cmd, scb->sno));
3428 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3429 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3434 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3436 for (i = 0; i < scp->cmd_len; i++) {
3437 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3440 con_log(level, ("\n"));
3447 * megaraid_mbox_setup_device_map - manage device ids
3448 * @adapter : Driver's soft state
3450 * Manange the device ids to have an appropriate mapping between the kernel
3451 * scsi addresses and megaraid scsi and logical drive addresses. We export
3452 * scsi devices on their actual addresses, whereas the logical drives are
3453 * exported on a virtual scsi channel.
3456 megaraid_mbox_setup_device_map(adapter_t *adapter)
3462 * First fill the values on the logical drive channel
3464 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3465 adapter->device_ids[adapter->max_channel][t] =
3466 (t < adapter->init_id) ? t : t - 1;
3468 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3471 * Fill the values on the physical devices channels
3473 for (c = 0; c < adapter->max_channel; c++)
3474 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3475 adapter->device_ids[c][t] = (c << 8) | t;
3480 * END: internal commands library
3484 * START: Interface for the common management module
3486 * This is the module, which interfaces with the common management module to
3487 * provide support for ioctl and sysfs
3491 * megaraid_cmm_register - register with the management module
3492 * @adapter : HBA soft state
3494 * Register with the management module, which allows applications to issue
3495 * ioctl calls to the drivers. This interface is used by the management module
3496 * to setup sysfs support as well.
3499 megaraid_cmm_register(adapter_t *adapter)
3501 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3508 // Allocate memory for the base list of scb for management module.
3509 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3511 if (adapter->uscb_list == NULL) {
3512 con_log(CL_ANN, (KERN_WARNING
3513 "megaraid: out of memory, %s %d\n", __func__,
3519 // Initialize the synchronization parameters for resources for
3520 // commands for management module
3521 INIT_LIST_HEAD(&adapter->uscb_pool);
3523 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3527 // link all the packets. Note, CCB for commands, coming from the
3528 // commom management module, mailbox physical address are already
3529 // setup by it. We just need placeholder for that in our local command
3531 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3533 scb = adapter->uscb_list + i;
3534 ccb = raid_dev->uccb_list + i;
3536 scb->ccb = (caddr_t)ccb;
3537 ccb->mbox64 = raid_dev->umbox64 + i;
3538 ccb->mbox = &ccb->mbox64->mbox32;
3539 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3543 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3544 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3545 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3548 scb->state = SCB_FREE;
3549 scb->dma_direction = PCI_DMA_NONE;
3550 scb->dma_type = MRAID_DMA_NONE;
3551 scb->dev_channel = -1;
3552 scb->dev_target = -1;
3554 // put scb in the free pool
3555 list_add_tail(&scb->list, &adapter->uscb_pool);
3558 adp.unique_id = adapter->unique_id;
3559 adp.drvr_type = DRVRTYPE_MBOX;
3560 adp.drvr_data = (unsigned long)adapter;
3561 adp.pdev = adapter->pdev;
3562 adp.issue_uioc = megaraid_mbox_mm_handler;
3563 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3564 adp.max_kioc = MBOX_MAX_USER_CMDS;
3566 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3568 con_log(CL_ANN, (KERN_WARNING
3569 "megaraid mbox: did not register with CMM\n"));
3571 kfree(adapter->uscb_list);
3579 * megaraid_cmm_unregister - un-register with the management module
3580 * @adapter : HBA soft state
3582 * Un-register with the management module.
3583 * FIXME: mgmt module must return failure for unregister if it has pending
3587 megaraid_cmm_unregister(adapter_t *adapter)
3589 kfree(adapter->uscb_list);
3590 mraid_mm_unregister_adp(adapter->unique_id);
3596 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3597 * @drvr_data : LLD specific data
3598 * @kioc : CMM interface packet
3599 * @action : command action
3601 * This routine is invoked whenever the Common Management Module (CMM) has a
3602 * command for us. The 'action' parameter specifies if this is a new command
3606 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3610 if (action != IOCTL_ISSUE) {
3611 con_log(CL_ANN, (KERN_WARNING
3612 "megaraid: unsupported management action:%#2x\n",
3617 adapter = (adapter_t *)drvr_data;
3619 // make sure this adapter is not being detached right now.
3620 if (atomic_read(&adapter->being_detached)) {
3621 con_log(CL_ANN, (KERN_WARNING
3622 "megaraid: reject management request, detaching\n"));
3626 switch (kioc->opcode) {
3630 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3631 (unsigned long)kioc->buf_vaddr);
3635 return kioc->status;
3639 return megaraid_mbox_mm_command(adapter, kioc);
3642 kioc->status = (-EINVAL);
3647 return 0; // not reached
3651 * megaraid_mbox_mm_command - issues commands routed through CMM
3652 * @adapter : HBA soft state
3653 * @kioc : management command packet
3655 * Issues commands, which are routed through the management module.
3658 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3660 struct list_head *head = &adapter->uscb_pool;
3665 unsigned long flags;
3667 // detach one scb from free pool
3668 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3670 if (list_empty(head)) { // should never happen because of CMM
3672 con_log(CL_ANN, (KERN_WARNING
3673 "megaraid mbox: bug in cmm handler, lost resources\n"));
3675 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3680 scb = list_entry(head->next, scb_t, list);
3681 list_del_init(&scb->list);
3683 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3685 scb->state = SCB_ACTIVE;
3686 scb->dma_type = MRAID_DMA_NONE;
3687 scb->dma_direction = PCI_DMA_NONE;
3689 ccb = (mbox_ccb_t *)scb->ccb;
3690 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3691 raw_mbox = (uint8_t *)&mbox64->mbox32;
3693 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3695 scb->gp = (unsigned long)kioc;
3698 * If it is a logdrv random delete operation, we have to wait till
3699 * there are no outstanding cmds at the fw and then issue it directly
3701 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3703 if (wait_till_fw_empty(adapter)) {
3704 con_log(CL_ANN, (KERN_NOTICE
3705 "megaraid mbox: LD delete, timed out\n"));
3707 kioc->status = -ETIME;
3711 megaraid_mbox_mm_done(adapter, scb);
3716 INIT_LIST_HEAD(&scb->list);
3718 scb->state = SCB_ISSUED;
3719 if (mbox_post_cmd(adapter, scb) != 0) {
3721 con_log(CL_ANN, (KERN_NOTICE
3722 "megaraid mbox: LD delete, mailbox busy\n"));
3724 kioc->status = -EBUSY;
3728 megaraid_mbox_mm_done(adapter, scb);
3736 // put the command on the pending list and execute
3737 megaraid_mbox_runpendq(adapter, scb);
3744 wait_till_fw_empty(adapter_t *adapter)
3746 unsigned long flags = 0;
3751 * Set the quiescent flag to stop issuing cmds to FW.
3753 spin_lock_irqsave(&adapter->lock, flags);
3754 adapter->quiescent++;
3755 spin_unlock_irqrestore(&adapter->lock, flags);
3758 * Wait till there are no more cmds outstanding at FW. Try for at most
3761 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3762 con_log(CL_DLEVEL1, (KERN_INFO
3763 "megaraid: FW has %d pending commands\n",
3764 adapter->outstanding_cmds));
3769 return adapter->outstanding_cmds;
3774 * megaraid_mbox_mm_done - callback for CMM commands
3775 * @adapter : HBA soft state
3776 * @scb : completed command
3778 * Callback routine for internal commands originated from the management
3782 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3787 unsigned long flags;
3789 kioc = (uioc_t *)scb->gp;
3790 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3791 mbox64->mbox32.status = scb->status;
3792 raw_mbox = (uint8_t *)&mbox64->mbox32;
3795 // put scb in the free pool
3796 scb->state = SCB_FREE;
3799 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3801 list_add(&scb->list, &adapter->uscb_pool);
3803 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3805 // if a delete logical drive operation succeeded, restart the
3807 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3809 adapter->quiescent--;
3811 megaraid_mbox_runpendq(adapter, NULL);
3821 * gather_hbainfo - HBA characteristics for the applications
3822 * @adapter : HBA soft state
3823 * @hinfo : pointer to the caller's host info strucuture
3826 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3830 dmajor = megaraid_mbox_version[0];
3832 hinfo->pci_vendor_id = adapter->pdev->vendor;
3833 hinfo->pci_device_id = adapter->pdev->device;
3834 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3835 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3837 hinfo->pci_bus = adapter->pdev->bus->number;
3838 hinfo->pci_dev_fn = adapter->pdev->devfn;
3839 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3840 hinfo->irq = adapter->host->irq;
3841 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3843 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3844 hinfo->host_no = adapter->host->host_no;
3850 * END: Interface for the common management module
3856 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3857 * @adapter : controller's soft state
3859 * Allocate packets required to issue FW calls whenever the sysfs attributes
3860 * are read. These attributes would require up-to-date information from the
3861 * FW. Also set up resources for mutual exclusion to share these resources and
3864 * Return 0 on success.
3865 * Return -ERROR_CODE on failure.
3868 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3870 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3873 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3875 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3877 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3878 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3880 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3881 !raid_dev->sysfs_buffer) {
3883 con_log(CL_ANN, (KERN_WARNING
3884 "megaraid: out of memory, %s %d\n", __func__,
3889 megaraid_sysfs_free_resources(adapter);
3892 mutex_init(&raid_dev->sysfs_mtx);
3894 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3901 * megaraid_sysfs_free_resources - free sysfs related resources
3902 * @adapter : controller's soft state
3904 * Free packets allocated for sysfs FW commands
3907 megaraid_sysfs_free_resources(adapter_t *adapter)
3909 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3911 kfree(raid_dev->sysfs_uioc);
3912 kfree(raid_dev->sysfs_mbox64);
3914 if (raid_dev->sysfs_buffer) {
3915 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3916 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3922 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3923 * @uioc : completed packet
3925 * Callback routine called in the ISR/tasklet context for get ldmap call
3928 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3930 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3931 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3935 wake_up(&raid_dev->sysfs_wait_q);
3940 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3941 * @data : timed out packet
3943 * Timeout routine to recover and return to application, in case the adapter
3944 * has stopped responding. A timeout of 60 seconds for this command seems like
3948 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
3950 uioc_t *uioc = (uioc_t *)data;
3951 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3952 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3954 uioc->status = -ETIME;
3956 wake_up(&raid_dev->sysfs_wait_q);
3961 * megaraid_sysfs_get_ldmap - get update logical drive map
3962 * @adapter : controller's soft state
3964 * This routine will be called whenever user reads the logical drive
3965 * attributes, go get the current logical drive mapping table from the
3966 * firmware. We use the management API's to issue commands to the controller.
3968 * NOTE: The commands issuance functionality is not generalized and
3969 * implemented in context of "get ld map" command only. If required, the
3970 * command issuance logical can be trivially pulled out and implemented as a
3971 * standalone library. For now, this should suffice since there is no other
3972 * user of this interface.
3974 * Return 0 on success.
3975 * Return -1 on failure.
3978 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3980 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3985 struct timer_list sysfs_timer;
3986 struct timer_list *timerp;
3991 * Allow only one read at a time to go through the sysfs attributes
3993 mutex_lock(&raid_dev->sysfs_mtx);
3995 uioc = raid_dev->sysfs_uioc;
3996 mbox64 = raid_dev->sysfs_mbox64;
3997 ldmap = raid_dev->sysfs_buffer;
3999 memset(uioc, 0, sizeof(uioc_t));
4000 memset(mbox64, 0, sizeof(mbox64_t));
4001 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
4003 mbox = &mbox64->mbox32;
4004 raw_mbox = (char *)mbox;
4005 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
4006 uioc->buf_vaddr = (caddr_t)adapter;
4007 uioc->status = -ENODATA;
4008 uioc->done = megaraid_sysfs_get_ldmap_done;
4011 * Prepare the mailbox packet to get the current logical drive mapping
4014 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
4016 raw_mbox[0] = FC_DEL_LOGDRV;
4017 raw_mbox[2] = OP_GET_LDID_MAP;
4020 * Setup a timer to recover from a non-responding controller
4022 timerp = &sysfs_timer;
4025 timerp->function = megaraid_sysfs_get_ldmap_timeout;
4026 timerp->data = (unsigned long)uioc;
4027 timerp->expires = jiffies + 60 * HZ;
4032 * Send the command to the firmware
4034 rval = megaraid_mbox_mm_command(adapter, uioc);
4036 if (rval == 0) { // command successfully issued
4037 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4040 * Check if the command timed out
4042 if (uioc->status == -ETIME) {
4043 con_log(CL_ANN, (KERN_NOTICE
4044 "megaraid: sysfs get ld map timed out\n"));
4049 rval = mbox->status;
4053 memcpy(raid_dev->curr_ldmap, ldmap,
4054 sizeof(raid_dev->curr_ldmap));
4057 con_log(CL_ANN, (KERN_NOTICE
4058 "megaraid: get ld map failed with %x\n", rval));
4062 con_log(CL_ANN, (KERN_NOTICE
4063 "megaraid: could not issue ldmap command:%x\n", rval));
4067 del_timer_sync(timerp);
4069 mutex_unlock(&raid_dev->sysfs_mtx);
4076 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4077 * @cdev : class device object representation for the host
4078 * @buf : buffer to send data to
4080 * Display the handle used by the applications while executing management
4081 * tasks on the adapter. We invoke a management module API to get the adapter
4082 * handle, since we do not interface with applications directly.
4085 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
4088 struct Scsi_Host *shost = class_to_shost(dev);
4089 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4092 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4094 return snprintf(buf, 8, "%u\n", app_hndl);
4099 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4100 * @dev : device object representation for the scsi device
4101 * @attr : device attribute to show
4102 * @buf : buffer to send data to
4104 * Display the logical drive number for the device in question, if it a valid
4105 * logical drive. For physical devices, "-1" is returned.
4107 * The logical drive number is displayed in following format:
4109 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4111 * <int> <int> <int> <int>
4114 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4116 struct scsi_device *sdev = to_scsi_device(dev);
4117 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4118 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4120 int logical_drv = -1;
4122 uint32_t app_hndl = 0;
4127 if (raid_dev->random_del_supported &&
4128 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4130 rval = megaraid_sysfs_get_ldmap(adapter);
4133 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4135 mapped_sdev_id = sdev->id;
4137 if (sdev->id > adapter->init_id) {
4138 mapped_sdev_id -= 1;
4141 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4147 ldid_map = raid_dev->curr_ldmap[i];
4149 app_hndl = mraid_mm_adapter_app_handle(
4150 adapter->unique_id);
4157 con_log(CL_ANN, (KERN_NOTICE
4158 "megaraid: sysfs get ld map failed: %x\n",
4163 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4164 ldid_map, app_hndl);
4169 * END: Mailbox Low Level Driver
4171 module_init(megaraid_init);
4172 module_exit(megaraid_exit);
4174 /* vim: set ts=8 sw=8 tw=78 ai si: */