Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_mbox.c
blob4a27ac869f2e20dc86865079a5c687020c4de3fa
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 * Linux MegaRAID device driver
6 * Copyright (c) 2003-2004 LSI Logic Corporation.
8 * FILE : megaraid_mbox.c
9 * Version : v2.20.5.1 (Nov 16 2006)
11 * Authors:
12 * Atul Mukker <Atul.Mukker@lsi.com>
13 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
14 * Manoj Jose <Manoj.Jose@lsi.com>
15 * Seokmann Ju
17 * List of supported controllers
19 * OEM Product Name VID DID SSVID SSID
20 * --- ------------ --- --- ---- ----
21 * Dell PERC3/QC 101E 1960 1028 0471
22 * Dell PERC3/DC 101E 1960 1028 0493
23 * Dell PERC3/SC 101E 1960 1028 0475
24 * Dell PERC3/Di 1028 1960 1028 0123
25 * Dell PERC4/SC 1000 1960 1028 0520
26 * Dell PERC4/DC 1000 1960 1028 0518
27 * Dell PERC4/QC 1000 0407 1028 0531
28 * Dell PERC4/Di 1028 000F 1028 014A
29 * Dell PERC 4e/Si 1028 0013 1028 016c
30 * Dell PERC 4e/Di 1028 0013 1028 016d
31 * Dell PERC 4e/Di 1028 0013 1028 016e
32 * Dell PERC 4e/Di 1028 0013 1028 016f
33 * Dell PERC 4e/Di 1028 0013 1028 0170
34 * Dell PERC 4e/DC 1000 0408 1028 0002
35 * Dell PERC 4e/SC 1000 0408 1028 0001
37 * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
38 * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
39 * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
40 * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
41 * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
42 * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
43 * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
44 * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
45 * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
46 * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
47 * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
48 * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
50 * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
51 * INTEL RAID Controller SRCS16 1000 1960 8086 0523
52 * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
53 * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
54 * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
55 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
56 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
57 * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
59 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
61 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
63 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
65 * For history of changes, see Documentation/scsi/ChangeLog.megaraid
68 #include <linux/slab.h>
69 #include <linux/module.h>
70 #include "megaraid_mbox.h"
72 static int megaraid_init(void);
73 static void megaraid_exit(void);
75 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
76 static void megaraid_detach_one(struct pci_dev *);
77 static void megaraid_mbox_shutdown(struct pci_dev *);
79 static int megaraid_io_attach(adapter_t *);
80 static void megaraid_io_detach(adapter_t *);
82 static int megaraid_init_mbox(adapter_t *);
83 static void megaraid_fini_mbox(adapter_t *);
85 static int megaraid_alloc_cmd_packets(adapter_t *);
86 static void megaraid_free_cmd_packets(adapter_t *);
88 static int megaraid_mbox_setup_dma_pools(adapter_t *);
89 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
91 static int megaraid_sysfs_alloc_resources(adapter_t *);
92 static void megaraid_sysfs_free_resources(adapter_t *);
94 static int megaraid_abort_handler(struct scsi_cmnd *);
95 static int megaraid_reset_handler(struct scsi_cmnd *);
97 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
98 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
99 static int megaraid_busywait_mbox(mraid_device_t *);
100 static int megaraid_mbox_product_info(adapter_t *);
101 static int megaraid_mbox_extended_cdb(adapter_t *);
102 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
103 static int megaraid_mbox_support_random_del(adapter_t *);
104 static int megaraid_mbox_get_max_sg(adapter_t *);
105 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
106 static void megaraid_mbox_flush_cache(adapter_t *);
107 static int megaraid_mbox_fire_sync_cmd(adapter_t *);
109 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
110 static void megaraid_mbox_setup_device_map(adapter_t *);
112 static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
113 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
114 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
115 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
116 struct scsi_cmnd *);
117 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
118 struct scsi_cmnd *);
120 static irqreturn_t megaraid_isr(int, void *);
122 static void megaraid_mbox_dpc(unsigned long);
124 static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
125 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
127 static int megaraid_cmm_register(adapter_t *);
128 static int megaraid_cmm_unregister(adapter_t *);
129 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
130 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
131 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
132 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
133 static int wait_till_fw_empty(adapter_t *);
137 MODULE_AUTHOR("megaraidlinux@lsi.com");
138 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
139 MODULE_LICENSE("GPL");
140 MODULE_VERSION(MEGARAID_VERSION);
143 * ### modules parameters for driver ###
147 * Set to enable driver to expose unconfigured disk to kernel
149 static int megaraid_expose_unconf_disks = 0;
150 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
151 MODULE_PARM_DESC(unconf_disks,
152 "Set to expose unconfigured disks to kernel (default=0)");
155 * driver wait time if the adapter's mailbox is busy
157 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
158 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
159 MODULE_PARM_DESC(busy_wait,
160 "Max wait for mailbox in microseconds if busy (default=10)");
163 * number of sectors per IO command
165 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
166 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
167 MODULE_PARM_DESC(max_sectors,
168 "Maximum number of sectors per IO command (default=128)");
171 * number of commands per logical unit
173 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
174 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
175 MODULE_PARM_DESC(cmd_per_lun,
176 "Maximum number of commands per logical unit (default=64)");
180 * Fast driver load option, skip scanning for physical devices during load.
181 * This would result in non-disk devices being skipped during driver load
182 * time. These can be later added though, using /proc/scsi/scsi
184 static unsigned int megaraid_fast_load = 0;
185 module_param_named(fast_load, megaraid_fast_load, int, 0);
186 MODULE_PARM_DESC(fast_load,
187 "Faster loading of the driver, skips physical devices! (default=0)");
191 * mraid_debug level - threshold for amount of information to be displayed by
192 * the driver. This level can be changed through modules parameters, ioctl or
193 * sysfs/proc interface. By default, print the announcement messages only.
195 int mraid_debug_level = CL_ANN;
196 module_param_named(debug_level, mraid_debug_level, int, 0);
197 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
200 * PCI table for all supported controllers.
202 static struct pci_device_id pci_id_table_g[] = {
204 PCI_VENDOR_ID_DELL,
205 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
206 PCI_VENDOR_ID_DELL,
207 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
210 PCI_VENDOR_ID_LSI_LOGIC,
211 PCI_DEVICE_ID_PERC4_SC,
212 PCI_VENDOR_ID_DELL,
213 PCI_SUBSYS_ID_PERC4_SC,
216 PCI_VENDOR_ID_LSI_LOGIC,
217 PCI_DEVICE_ID_PERC4_DC,
218 PCI_VENDOR_ID_DELL,
219 PCI_SUBSYS_ID_PERC4_DC,
222 PCI_VENDOR_ID_LSI_LOGIC,
223 PCI_DEVICE_ID_VERDE,
224 PCI_ANY_ID,
225 PCI_ANY_ID,
228 PCI_VENDOR_ID_DELL,
229 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
230 PCI_VENDOR_ID_DELL,
231 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
234 PCI_VENDOR_ID_DELL,
235 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
236 PCI_VENDOR_ID_DELL,
237 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
240 PCI_VENDOR_ID_DELL,
241 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
242 PCI_VENDOR_ID_DELL,
243 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
246 PCI_VENDOR_ID_DELL,
247 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
248 PCI_VENDOR_ID_DELL,
249 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
252 PCI_VENDOR_ID_DELL,
253 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
254 PCI_VENDOR_ID_DELL,
255 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
258 PCI_VENDOR_ID_DELL,
259 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
260 PCI_VENDOR_ID_DELL,
261 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
264 PCI_VENDOR_ID_LSI_LOGIC,
265 PCI_DEVICE_ID_DOBSON,
266 PCI_ANY_ID,
267 PCI_ANY_ID,
270 PCI_VENDOR_ID_AMI,
271 PCI_DEVICE_ID_AMI_MEGARAID3,
272 PCI_ANY_ID,
273 PCI_ANY_ID,
276 PCI_VENDOR_ID_LSI_LOGIC,
277 PCI_DEVICE_ID_AMI_MEGARAID3,
278 PCI_ANY_ID,
279 PCI_ANY_ID,
282 PCI_VENDOR_ID_LSI_LOGIC,
283 PCI_DEVICE_ID_LINDSAY,
284 PCI_ANY_ID,
285 PCI_ANY_ID,
287 {0} /* Terminating entry */
289 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
292 static struct pci_driver megaraid_pci_driver = {
293 .name = "megaraid",
294 .id_table = pci_id_table_g,
295 .probe = megaraid_probe_one,
296 .remove = megaraid_detach_one,
297 .shutdown = megaraid_mbox_shutdown,
302 // definitions for the device attributes for exporting logical drive number
303 // for a scsi address (Host, Channel, Id, Lun)
305 static DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
306 NULL);
308 // Host template initializer for megaraid mbox sysfs device attributes
309 static struct device_attribute *megaraid_shost_attrs[] = {
310 &dev_attr_megaraid_mbox_app_hndl,
311 NULL,
315 static DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
317 // Host template initializer for megaraid mbox sysfs device attributes
318 static struct device_attribute *megaraid_sdev_attrs[] = {
319 &dev_attr_megaraid_mbox_ld,
320 NULL,
324 * Scsi host template for megaraid unified driver
326 static struct scsi_host_template megaraid_template_g = {
327 .module = THIS_MODULE,
328 .name = "LSI Logic MegaRAID driver",
329 .proc_name = "megaraid",
330 .queuecommand = megaraid_queue_command,
331 .eh_abort_handler = megaraid_abort_handler,
332 .eh_host_reset_handler = megaraid_reset_handler,
333 .change_queue_depth = scsi_change_queue_depth,
334 .no_write_same = 1,
335 .sdev_attrs = megaraid_sdev_attrs,
336 .shost_attrs = megaraid_shost_attrs,
341 * megaraid_init - module load hook
343 * We register ourselves as hotplug enabled module and let PCI subsystem
344 * discover our adapters.
346 static int __init
347 megaraid_init(void)
349 int rval;
351 // Announce the driver version
352 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
353 MEGARAID_EXT_VERSION));
355 // check validity of module parameters
356 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
358 con_log(CL_ANN, (KERN_WARNING
359 "megaraid mailbox: max commands per lun reset to %d\n",
360 MBOX_MAX_SCSI_CMDS));
362 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
366 // register as a PCI hot-plug driver module
367 rval = pci_register_driver(&megaraid_pci_driver);
368 if (rval < 0) {
369 con_log(CL_ANN, (KERN_WARNING
370 "megaraid: could not register hotplug support.\n"));
373 return rval;
378 * megaraid_exit - driver unload entry point
380 * We simply unwrap the megaraid_init routine here.
382 static void __exit
383 megaraid_exit(void)
385 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
387 // unregister as PCI hotplug driver
388 pci_unregister_driver(&megaraid_pci_driver);
390 return;
395 * megaraid_probe_one - PCI hotplug entry point
396 * @pdev : handle to this controller's PCI configuration space
397 * @id : pci device id of the class of controllers
399 * This routine should be called whenever a new adapter is detected by the
400 * PCI hotplug susbsystem.
402 static int
403 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
405 adapter_t *adapter;
408 // detected a new controller
409 con_log(CL_ANN, (KERN_INFO
410 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
411 pdev->vendor, pdev->device, pdev->subsystem_vendor,
412 pdev->subsystem_device));
414 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
415 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
417 if (pci_enable_device(pdev)) {
418 con_log(CL_ANN, (KERN_WARNING
419 "megaraid: pci_enable_device failed\n"));
421 return -ENODEV;
424 // Enable bus-mastering on this controller
425 pci_set_master(pdev);
427 // Allocate the per driver initialization structure
428 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
430 if (adapter == NULL) {
431 con_log(CL_ANN, (KERN_WARNING
432 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
434 goto out_probe_one;
438 // set up PCI related soft state and other pre-known parameters
439 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
440 adapter->irq = pdev->irq;
441 adapter->pdev = pdev;
443 atomic_set(&adapter->being_detached, 0);
445 // Setup the default DMA mask. This would be changed later on
446 // depending on hardware capabilities
447 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) {
448 con_log(CL_ANN, (KERN_WARNING
449 "megaraid: dma_set_mask failed:%d\n", __LINE__));
451 goto out_free_adapter;
455 // Initialize the synchronization lock for kernel and LLD
456 spin_lock_init(&adapter->lock);
458 // Initialize the command queues: the list of free SCBs and the list
459 // of pending SCBs.
460 INIT_LIST_HEAD(&adapter->kscb_pool);
461 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
463 INIT_LIST_HEAD(&adapter->pend_list);
464 spin_lock_init(PENDING_LIST_LOCK(adapter));
466 INIT_LIST_HEAD(&adapter->completed_list);
467 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
470 // Start the mailbox based controller
471 if (megaraid_init_mbox(adapter) != 0) {
472 con_log(CL_ANN, (KERN_WARNING
473 "megaraid: mailbox adapter did not initialize\n"));
475 goto out_free_adapter;
478 // Register with LSI Common Management Module
479 if (megaraid_cmm_register(adapter) != 0) {
481 con_log(CL_ANN, (KERN_WARNING
482 "megaraid: could not register with management module\n"));
484 goto out_fini_mbox;
487 // setup adapter handle in PCI soft state
488 pci_set_drvdata(pdev, adapter);
490 // attach with scsi mid-layer
491 if (megaraid_io_attach(adapter) != 0) {
493 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
495 goto out_cmm_unreg;
498 return 0;
500 out_cmm_unreg:
501 megaraid_cmm_unregister(adapter);
502 out_fini_mbox:
503 megaraid_fini_mbox(adapter);
504 out_free_adapter:
505 kfree(adapter);
506 out_probe_one:
507 pci_disable_device(pdev);
509 return -ENODEV;
514 * megaraid_detach_one - release framework resources and call LLD release routine
515 * @pdev : handle for our PCI configuration space
517 * This routine is called during driver unload. We free all the allocated
518 * resources and call the corresponding LLD so that it can also release all
519 * its resources.
521 * This routine is also called from the PCI hotplug system.
523 static void
524 megaraid_detach_one(struct pci_dev *pdev)
526 adapter_t *adapter;
527 struct Scsi_Host *host;
530 // Start a rollback on this adapter
531 adapter = pci_get_drvdata(pdev);
533 if (!adapter) {
534 con_log(CL_ANN, (KERN_CRIT
535 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
536 pdev->vendor, pdev->device, pdev->subsystem_vendor,
537 pdev->subsystem_device));
539 return;
541 else {
542 con_log(CL_ANN, (KERN_NOTICE
543 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
544 pdev->vendor, pdev->device, pdev->subsystem_vendor,
545 pdev->subsystem_device));
549 host = adapter->host;
551 // do not allow any more requests from the management module for this
552 // adapter.
553 // FIXME: How do we account for the request which might still be
554 // pending with us?
555 atomic_set(&adapter->being_detached, 1);
557 // detach from the IO sub-system
558 megaraid_io_detach(adapter);
560 // Unregister from common management module
562 // FIXME: this must return success or failure for conditions if there
563 // is a command pending with LLD or not.
564 megaraid_cmm_unregister(adapter);
566 // finalize the mailbox based controller and release all resources
567 megaraid_fini_mbox(adapter);
569 kfree(adapter);
571 scsi_host_put(host);
573 pci_disable_device(pdev);
575 return;
580 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
581 * @pdev : generic driver model device
583 * Shutdown notification, perform flush cache.
585 static void
586 megaraid_mbox_shutdown(struct pci_dev *pdev)
588 adapter_t *adapter = pci_get_drvdata(pdev);
589 static int counter;
591 if (!adapter) {
592 con_log(CL_ANN, (KERN_WARNING
593 "megaraid: null device in shutdown\n"));
594 return;
597 // flush caches now
598 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
599 counter++));
601 megaraid_mbox_flush_cache(adapter);
603 con_log(CL_ANN, ("done\n"));
608 * megaraid_io_attach - attach a device with the IO subsystem
609 * @adapter : controller's soft state
611 * Attach this device with the IO subsystem.
613 static int
614 megaraid_io_attach(adapter_t *adapter)
616 struct Scsi_Host *host;
618 // Initialize SCSI Host structure
619 host = scsi_host_alloc(&megaraid_template_g, 8);
620 if (!host) {
621 con_log(CL_ANN, (KERN_WARNING
622 "megaraid mbox: scsi_register failed\n"));
624 return -1;
627 SCSIHOST2ADAP(host) = (caddr_t)adapter;
628 adapter->host = host;
630 host->irq = adapter->irq;
631 host->unique_id = adapter->unique_id;
632 host->can_queue = adapter->max_cmds;
633 host->this_id = adapter->init_id;
634 host->sg_tablesize = adapter->sglen;
635 host->max_sectors = adapter->max_sectors;
636 host->cmd_per_lun = adapter->cmd_per_lun;
637 host->max_channel = adapter->max_channel;
638 host->max_id = adapter->max_target;
639 host->max_lun = adapter->max_lun;
642 // notify mid-layer about the new controller
643 if (scsi_add_host(host, &adapter->pdev->dev)) {
645 con_log(CL_ANN, (KERN_WARNING
646 "megaraid mbox: scsi_add_host failed\n"));
648 scsi_host_put(host);
650 return -1;
653 scsi_scan_host(host);
655 return 0;
660 * megaraid_io_detach - detach a device from the IO subsystem
661 * @adapter : controller's soft state
663 * Detach this device from the IO subsystem.
665 static void
666 megaraid_io_detach(adapter_t *adapter)
668 struct Scsi_Host *host;
670 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
672 host = adapter->host;
674 scsi_remove_host(host);
676 return;
681 * START: Mailbox Low Level Driver
683 * This is section specific to the single mailbox based controllers
687 * megaraid_init_mbox - initialize controller
688 * @adapter : our soft state
690 * - Allocate 16-byte aligned mailbox memory for firmware handshake
691 * - Allocate controller's memory resources
692 * - Find out all initialization data
693 * - Allocate memory required for all the commands
694 * - Use internal library of FW routines, build up complete soft state
696 static int
697 megaraid_init_mbox(adapter_t *adapter)
699 struct pci_dev *pdev;
700 mraid_device_t *raid_dev;
701 int i;
702 uint32_t magic64;
705 adapter->ito = MBOX_TIMEOUT;
706 pdev = adapter->pdev;
709 * Allocate and initialize the init data structure for mailbox
710 * controllers
712 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
713 if (raid_dev == NULL) return -1;
717 * Attach the adapter soft state to raid device soft state
719 adapter->raid_device = (caddr_t)raid_dev;
720 raid_dev->fast_load = megaraid_fast_load;
723 // our baseport
724 raid_dev->baseport = pci_resource_start(pdev, 0);
726 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
728 con_log(CL_ANN, (KERN_WARNING
729 "megaraid: mem region busy\n"));
731 goto out_free_raid_dev;
734 raid_dev->baseaddr = ioremap(raid_dev->baseport, 128);
736 if (!raid_dev->baseaddr) {
738 con_log(CL_ANN, (KERN_WARNING
739 "megaraid: could not map hba memory\n") );
741 goto out_release_regions;
744 /* initialize the mutual exclusion lock for the mailbox */
745 spin_lock_init(&raid_dev->mailbox_lock);
747 /* allocate memory required for commands */
748 if (megaraid_alloc_cmd_packets(adapter) != 0)
749 goto out_iounmap;
752 * Issue SYNC cmd to flush the pending cmds in the adapter
753 * and initialize its internal state
756 if (megaraid_mbox_fire_sync_cmd(adapter))
757 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
760 * Setup the rest of the soft state using the library of
761 * FW routines
764 /* request IRQ and register the interrupt service routine */
765 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
766 adapter)) {
768 con_log(CL_ANN, (KERN_WARNING
769 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
770 goto out_alloc_cmds;
774 // Product info
775 if (megaraid_mbox_product_info(adapter) != 0)
776 goto out_free_irq;
778 // Do we support extended CDBs
779 adapter->max_cdb_sz = 10;
780 if (megaraid_mbox_extended_cdb(adapter) == 0) {
781 adapter->max_cdb_sz = 16;
785 * Do we support cluster environment, if we do, what is the initiator
786 * id.
787 * NOTE: In a non-cluster aware firmware environment, the LLD should
788 * return 7 as initiator id.
790 adapter->ha = 0;
791 adapter->init_id = -1;
792 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
793 adapter->ha = 1;
797 * Prepare the device ids array to have the mapping between the kernel
798 * device address and megaraid device address.
799 * We export the physical devices on their actual addresses. The
800 * logical drives are exported on a virtual SCSI channel
802 megaraid_mbox_setup_device_map(adapter);
804 // If the firmware supports random deletion, update the device id map
805 if (megaraid_mbox_support_random_del(adapter)) {
807 // Change the logical drives numbers in device_ids array one
808 // slot in device_ids is reserved for target id, that's why
809 // "<=" below
810 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
811 adapter->device_ids[adapter->max_channel][i] += 0x80;
813 adapter->device_ids[adapter->max_channel][adapter->init_id] =
814 0xFF;
816 raid_dev->random_del_supported = 1;
820 * find out the maximum number of scatter-gather elements supported by
821 * this firmware
823 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
825 // enumerate RAID and SCSI channels so that all devices on SCSI
826 // channels can later be exported, including disk devices
827 megaraid_mbox_enum_raid_scsi(adapter);
830 * Other parameters required by upper layer
832 * maximum number of sectors per IO command
834 adapter->max_sectors = megaraid_max_sectors;
837 * number of queued commands per LUN.
839 adapter->cmd_per_lun = megaraid_cmd_per_lun;
842 * Allocate resources required to issue FW calls, when sysfs is
843 * accessed
845 if (megaraid_sysfs_alloc_resources(adapter) != 0)
846 goto out_free_irq;
848 // Set the DMA mask to 64-bit. All supported controllers as capable of
849 // DMA in this range
850 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
852 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
853 ((adapter->pdev->subsystem_device !=
854 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
855 (adapter->pdev->subsystem_device !=
856 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
857 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
858 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
859 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
860 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
861 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
862 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
863 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
864 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
865 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
866 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
867 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) {
868 con_log(CL_ANN, (KERN_WARNING
869 "megaraid: DMA mask for 64-bit failed\n"));
871 if (dma_set_mask(&adapter->pdev->dev,
872 DMA_BIT_MASK(32))) {
873 con_log(CL_ANN, (KERN_WARNING
874 "megaraid: 32-bit DMA mask failed\n"));
875 goto out_free_sysfs_res;
880 // setup tasklet for DPC
881 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
882 (unsigned long)adapter);
884 con_log(CL_DLEVEL1, (KERN_INFO
885 "megaraid mbox hba successfully initialized\n"));
887 return 0;
889 out_free_sysfs_res:
890 megaraid_sysfs_free_resources(adapter);
891 out_free_irq:
892 free_irq(adapter->irq, adapter);
893 out_alloc_cmds:
894 megaraid_free_cmd_packets(adapter);
895 out_iounmap:
896 iounmap(raid_dev->baseaddr);
897 out_release_regions:
898 pci_release_regions(pdev);
899 out_free_raid_dev:
900 kfree(raid_dev);
902 return -1;
907 * megaraid_fini_mbox - undo controller initialization
908 * @adapter : our soft state
910 static void
911 megaraid_fini_mbox(adapter_t *adapter)
913 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
915 // flush all caches
916 megaraid_mbox_flush_cache(adapter);
918 tasklet_kill(&adapter->dpc_h);
920 megaraid_sysfs_free_resources(adapter);
922 megaraid_free_cmd_packets(adapter);
924 free_irq(adapter->irq, adapter);
926 iounmap(raid_dev->baseaddr);
928 pci_release_regions(adapter->pdev);
930 kfree(raid_dev);
932 return;
937 * megaraid_alloc_cmd_packets - allocate shared mailbox
938 * @adapter : soft state of the raid controller
940 * Allocate and align the shared mailbox. This mailbox is used to issue
941 * all the commands. For IO based controllers, the mailbox is also registered
942 * with the FW. Allocate memory for all commands as well.
943 * This is our big allocator.
945 static int
946 megaraid_alloc_cmd_packets(adapter_t *adapter)
948 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
949 struct pci_dev *pdev;
950 unsigned long align;
951 scb_t *scb;
952 mbox_ccb_t *ccb;
953 struct mraid_pci_blk *epthru_pci_blk;
954 struct mraid_pci_blk *sg_pci_blk;
955 struct mraid_pci_blk *mbox_pci_blk;
956 int i;
958 pdev = adapter->pdev;
961 * Setup the mailbox
962 * Allocate the common 16-byte aligned memory for the handshake
963 * mailbox.
965 raid_dev->una_mbox64 = dma_alloc_coherent(&adapter->pdev->dev,
966 sizeof(mbox64_t),
967 &raid_dev->una_mbox64_dma,
968 GFP_KERNEL);
970 if (!raid_dev->una_mbox64) {
971 con_log(CL_ANN, (KERN_WARNING
972 "megaraid: out of memory, %s %d\n", __func__,
973 __LINE__));
974 return -1;
978 * Align the mailbox at 16-byte boundary
980 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
982 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
983 (~0UL ^ 0xFUL));
985 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
987 align = ((void *)raid_dev->mbox -
988 ((void *)&raid_dev->una_mbox64->mbox32));
990 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
991 align;
993 // Allocate memory for commands issued internally
994 adapter->ibuf = dma_alloc_coherent(&pdev->dev, MBOX_IBUF_SIZE,
995 &adapter->ibuf_dma_h, GFP_KERNEL);
996 if (!adapter->ibuf) {
998 con_log(CL_ANN, (KERN_WARNING
999 "megaraid: out of memory, %s %d\n", __func__,
1000 __LINE__));
1002 goto out_free_common_mbox;
1005 // Allocate memory for our SCSI Command Blocks and their associated
1006 // memory
1009 * Allocate memory for the base list of scb. Later allocate memory for
1010 * CCBs and embedded components of each CCB and point the pointers in
1011 * scb to the allocated components
1012 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1013 * since the calling routine does not yet know the number of available
1014 * commands.
1016 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1018 if (adapter->kscb_list == NULL) {
1019 con_log(CL_ANN, (KERN_WARNING
1020 "megaraid: out of memory, %s %d\n", __func__,
1021 __LINE__));
1022 goto out_free_ibuf;
1025 // memory allocation for our command packets
1026 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1027 con_log(CL_ANN, (KERN_WARNING
1028 "megaraid: out of memory, %s %d\n", __func__,
1029 __LINE__));
1030 goto out_free_scb_list;
1033 // Adjust the scb pointers and link in the free pool
1034 epthru_pci_blk = raid_dev->epthru_pool;
1035 sg_pci_blk = raid_dev->sg_pool;
1036 mbox_pci_blk = raid_dev->mbox_pool;
1038 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1039 scb = adapter->kscb_list + i;
1040 ccb = raid_dev->ccb_list + i;
1042 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1043 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1044 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1045 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1047 // make sure the mailbox is aligned properly
1048 if (ccb->mbox_dma_h & 0x0F) {
1049 con_log(CL_ANN, (KERN_CRIT
1050 "megaraid mbox: not aligned on 16-bytes\n"));
1052 goto out_teardown_dma_pools;
1055 ccb->epthru = (mraid_epassthru_t *)
1056 epthru_pci_blk[i].vaddr;
1057 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1058 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1059 ccb->pthru_dma_h = ccb->epthru_dma_h;
1062 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1063 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1064 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1066 scb->ccb = (caddr_t)ccb;
1067 scb->gp = 0;
1069 scb->sno = i; // command index
1071 scb->scp = NULL;
1072 scb->state = SCB_FREE;
1073 scb->dma_direction = DMA_NONE;
1074 scb->dma_type = MRAID_DMA_NONE;
1075 scb->dev_channel = -1;
1076 scb->dev_target = -1;
1078 // put scb in the free pool
1079 list_add_tail(&scb->list, &adapter->kscb_pool);
1082 return 0;
1084 out_teardown_dma_pools:
1085 megaraid_mbox_teardown_dma_pools(adapter);
1086 out_free_scb_list:
1087 kfree(adapter->kscb_list);
1088 out_free_ibuf:
1089 dma_free_coherent(&pdev->dev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1090 adapter->ibuf_dma_h);
1091 out_free_common_mbox:
1092 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1093 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1095 return -1;
1100 * megaraid_free_cmd_packets - free memory
1101 * @adapter : soft state of the raid controller
1103 * Release memory resources allocated for commands.
1105 static void
1106 megaraid_free_cmd_packets(adapter_t *adapter)
1108 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1110 megaraid_mbox_teardown_dma_pools(adapter);
1112 kfree(adapter->kscb_list);
1114 dma_free_coherent(&adapter->pdev->dev, MBOX_IBUF_SIZE,
1115 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1117 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1118 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1119 return;
1124 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1125 * @adapter : HBA soft state
1127 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1128 * and scatter-gather lists.
1130 static int
1131 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1133 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1134 struct mraid_pci_blk *epthru_pci_blk;
1135 struct mraid_pci_blk *sg_pci_blk;
1136 struct mraid_pci_blk *mbox_pci_blk;
1137 int i;
1141 // Allocate memory for 16-bytes aligned mailboxes
1142 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
1143 &adapter->pdev->dev,
1144 sizeof(mbox64_t) + 16,
1145 16, 0);
1147 if (raid_dev->mbox_pool_handle == NULL) {
1148 goto fail_setup_dma_pool;
1151 mbox_pci_blk = raid_dev->mbox_pool;
1152 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1153 mbox_pci_blk[i].vaddr = dma_pool_alloc(
1154 raid_dev->mbox_pool_handle,
1155 GFP_KERNEL,
1156 &mbox_pci_blk[i].dma_addr);
1157 if (!mbox_pci_blk[i].vaddr) {
1158 goto fail_setup_dma_pool;
1163 * Allocate memory for each embedded passthru strucuture pointer
1164 * Request for a 128 bytes aligned structure for each passthru command
1165 * structure
1166 * Since passthru and extended passthru commands are exclusive, they
1167 * share common memory pool. Passthru structures piggyback on memory
1168 * allocted to extended passthru since passthru is smaller of the two
1170 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
1171 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
1173 if (raid_dev->epthru_pool_handle == NULL) {
1174 goto fail_setup_dma_pool;
1177 epthru_pci_blk = raid_dev->epthru_pool;
1178 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1179 epthru_pci_blk[i].vaddr = dma_pool_alloc(
1180 raid_dev->epthru_pool_handle,
1181 GFP_KERNEL,
1182 &epthru_pci_blk[i].dma_addr);
1183 if (!epthru_pci_blk[i].vaddr) {
1184 goto fail_setup_dma_pool;
1189 // Allocate memory for each scatter-gather list. Request for 512 bytes
1190 // alignment for each sg list
1191 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
1192 &adapter->pdev->dev,
1193 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1194 512, 0);
1196 if (raid_dev->sg_pool_handle == NULL) {
1197 goto fail_setup_dma_pool;
1200 sg_pci_blk = raid_dev->sg_pool;
1201 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1202 sg_pci_blk[i].vaddr = dma_pool_alloc(
1203 raid_dev->sg_pool_handle,
1204 GFP_KERNEL,
1205 &sg_pci_blk[i].dma_addr);
1206 if (!sg_pci_blk[i].vaddr) {
1207 goto fail_setup_dma_pool;
1211 return 0;
1213 fail_setup_dma_pool:
1214 megaraid_mbox_teardown_dma_pools(adapter);
1215 return -1;
1220 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1221 * @adapter : HBA soft state
1223 * Teardown the dma pool for mailbox, passthru and extended passthru
1224 * structures, and scatter-gather lists.
1226 static void
1227 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1229 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1230 struct mraid_pci_blk *epthru_pci_blk;
1231 struct mraid_pci_blk *sg_pci_blk;
1232 struct mraid_pci_blk *mbox_pci_blk;
1233 int i;
1236 sg_pci_blk = raid_dev->sg_pool;
1237 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1238 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1239 sg_pci_blk[i].dma_addr);
1241 dma_pool_destroy(raid_dev->sg_pool_handle);
1244 epthru_pci_blk = raid_dev->epthru_pool;
1245 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1246 dma_pool_free(raid_dev->epthru_pool_handle,
1247 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1249 dma_pool_destroy(raid_dev->epthru_pool_handle);
1252 mbox_pci_blk = raid_dev->mbox_pool;
1253 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1254 dma_pool_free(raid_dev->mbox_pool_handle,
1255 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1257 dma_pool_destroy(raid_dev->mbox_pool_handle);
1259 return;
1264 * megaraid_alloc_scb - detach and return a scb from the free list
1265 * @adapter : controller's soft state
1266 * @scp : pointer to the scsi command to be executed
1268 * Return the scb from the head of the free list. %NULL if there are none
1269 * available.
1271 static scb_t *
1272 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1274 struct list_head *head = &adapter->kscb_pool;
1275 scb_t *scb = NULL;
1276 unsigned long flags;
1278 // detach scb from free pool
1279 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1281 if (list_empty(head)) {
1282 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1283 return NULL;
1286 scb = list_entry(head->next, scb_t, list);
1287 list_del_init(&scb->list);
1289 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1291 scb->state = SCB_ACTIVE;
1292 scb->scp = scp;
1293 scb->dma_type = MRAID_DMA_NONE;
1295 return scb;
1300 * megaraid_dealloc_scb - return the scb to the free pool
1301 * @adapter : controller's soft state
1302 * @scb : scb to be freed
1304 * Return the scb back to the free list of scbs. The caller must 'flush' the
1305 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1306 * NOTE NOTE: Make sure the scb is not on any list before calling this
1307 * routine.
1309 static inline void
1310 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1312 unsigned long flags;
1314 // put scb in the free pool
1315 scb->state = SCB_FREE;
1316 scb->scp = NULL;
1317 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1319 list_add(&scb->list, &adapter->kscb_pool);
1321 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1323 return;
1328 * megaraid_mbox_mksgl - make the scatter-gather list
1329 * @adapter : controller's soft state
1330 * @scb : scsi control block
1332 * Prepare the scatter-gather list.
1334 static int
1335 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1337 struct scatterlist *sgl;
1338 mbox_ccb_t *ccb;
1339 struct scsi_cmnd *scp;
1340 int sgcnt;
1341 int i;
1344 scp = scb->scp;
1345 ccb = (mbox_ccb_t *)scb->ccb;
1347 sgcnt = scsi_dma_map(scp);
1348 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1350 // no mapping required if no data to be transferred
1351 if (!sgcnt)
1352 return 0;
1354 scb->dma_type = MRAID_DMA_WSG;
1356 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1357 ccb->sgl64[i].address = sg_dma_address(sgl);
1358 ccb->sgl64[i].length = sg_dma_len(sgl);
1361 // Return count of SG nodes
1362 return sgcnt;
1367 * mbox_post_cmd - issue a mailbox command
1368 * @adapter : controller's soft state
1369 * @scb : command to be issued
1371 * Post the command to the controller if mailbox is available.
1373 static int
1374 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1376 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1377 mbox64_t *mbox64;
1378 mbox_t *mbox;
1379 mbox_ccb_t *ccb;
1380 unsigned long flags;
1381 unsigned int i = 0;
1384 ccb = (mbox_ccb_t *)scb->ccb;
1385 mbox = raid_dev->mbox;
1386 mbox64 = raid_dev->mbox64;
1389 * Check for busy mailbox. If it is, return failure - the caller
1390 * should retry later.
1392 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1394 if (unlikely(mbox->busy)) {
1395 do {
1396 udelay(1);
1397 i++;
1398 rmb();
1399 } while(mbox->busy && (i < max_mbox_busy_wait));
1401 if (mbox->busy) {
1403 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1405 return -1;
1410 // Copy this command's mailbox data into "adapter's" mailbox
1411 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1412 mbox->cmdid = scb->sno;
1414 adapter->outstanding_cmds++;
1416 mbox->busy = 1; // Set busy
1417 mbox->poll = 0;
1418 mbox->ack = 0;
1419 wmb();
1421 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1423 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1425 return 0;
1430 * megaraid_queue_command - generic queue entry point for all LLDs
1431 * @scp : pointer to the scsi command to be executed
1432 * @done : callback routine to be called after the cmd has be completed
1434 * Queue entry point for mailbox based controllers.
1436 static int
1437 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1439 adapter_t *adapter;
1440 scb_t *scb;
1441 int if_busy;
1443 adapter = SCP2ADAPTER(scp);
1444 scp->scsi_done = done;
1445 scp->result = 0;
1448 * Allocate and build a SCB request
1449 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1450 * not allocate scb. We will return non-zero status in that case.
1451 * NOTE: scb can be null even though certain commands completed
1452 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1453 * return 0 in that case, and we would do the callback right away.
1455 if_busy = 0;
1456 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1457 if (!scb) { // command already completed
1458 done(scp);
1459 return 0;
1462 megaraid_mbox_runpendq(adapter, scb);
1463 return if_busy;
1466 static DEF_SCSI_QCMD(megaraid_queue_command)
1469 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1470 * @adapter : controller's soft state
1471 * @scp : mid-layer scsi command pointer
1472 * @busy : set if request could not be completed because of lack of
1473 * resources
1475 * Transform the mid-layer scsi command to megaraid firmware lingua.
1476 * Convert the command issued by mid-layer to format understood by megaraid
1477 * firmware. We also complete certain commands without sending them to firmware.
1479 static scb_t *
1480 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1482 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1483 int channel;
1484 int target;
1485 int islogical;
1486 mbox_ccb_t *ccb;
1487 mraid_passthru_t *pthru;
1488 mbox64_t *mbox64;
1489 mbox_t *mbox;
1490 scb_t *scb;
1491 char skip[] = "skipping";
1492 char scan[] = "scanning";
1493 char *ss;
1497 * Get the appropriate device map for the device this command is
1498 * intended for
1500 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1503 * Logical drive commands
1505 if (islogical) {
1506 switch (scp->cmnd[0]) {
1507 case TEST_UNIT_READY:
1509 * Do we support clustering and is the support enabled
1510 * If no, return success always
1512 if (!adapter->ha) {
1513 scp->result = (DID_OK << 16);
1514 return NULL;
1517 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1518 scp->result = (DID_ERROR << 16);
1519 *busy = 1;
1520 return NULL;
1523 scb->dma_direction = scp->sc_data_direction;
1524 scb->dev_channel = 0xFF;
1525 scb->dev_target = target;
1526 ccb = (mbox_ccb_t *)scb->ccb;
1529 * The command id will be provided by the command
1530 * issuance routine
1532 ccb->raw_mbox[0] = CLUSTER_CMD;
1533 ccb->raw_mbox[2] = RESERVATION_STATUS;
1534 ccb->raw_mbox[3] = target;
1536 return scb;
1538 case MODE_SENSE:
1540 struct scatterlist *sgl;
1541 caddr_t vaddr;
1543 sgl = scsi_sglist(scp);
1544 if (sg_page(sgl)) {
1545 vaddr = (caddr_t) sg_virt(&sgl[0]);
1547 memset(vaddr, 0, scp->cmnd[4]);
1549 else {
1550 con_log(CL_ANN, (KERN_WARNING
1551 "megaraid mailbox: invalid sg:%d\n",
1552 __LINE__));
1555 scp->result = (DID_OK << 16);
1556 return NULL;
1558 case INQUIRY:
1560 * Display the channel scan for logical drives
1561 * Do not display scan for a channel if already done.
1563 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1565 con_log(CL_ANN, (KERN_INFO
1566 "scsi[%d]: scanning scsi channel %d",
1567 adapter->host->host_no,
1568 SCP2CHANNEL(scp)));
1570 con_log(CL_ANN, (
1571 " [virtual] for logical drives\n"));
1573 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1576 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1577 scp->sense_buffer[0] = 0x70;
1578 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1579 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1580 scp->result = CHECK_CONDITION << 1;
1581 return NULL;
1584 fallthrough;
1586 case READ_CAPACITY:
1588 * Do not allow LUN > 0 for logical drives and
1589 * requests for more than 40 logical drives
1591 if (SCP2LUN(scp)) {
1592 scp->result = (DID_BAD_TARGET << 16);
1593 return NULL;
1595 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1596 scp->result = (DID_BAD_TARGET << 16);
1597 return NULL;
1601 /* Allocate a SCB and initialize passthru */
1602 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1603 scp->result = (DID_ERROR << 16);
1604 *busy = 1;
1605 return NULL;
1608 ccb = (mbox_ccb_t *)scb->ccb;
1609 scb->dev_channel = 0xFF;
1610 scb->dev_target = target;
1611 pthru = ccb->pthru;
1612 mbox = ccb->mbox;
1613 mbox64 = ccb->mbox64;
1615 pthru->timeout = 0;
1616 pthru->ars = 1;
1617 pthru->reqsenselen = 14;
1618 pthru->islogical = 1;
1619 pthru->logdrv = target;
1620 pthru->cdblen = scp->cmd_len;
1621 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1623 mbox->cmd = MBOXCMD_PASSTHRU64;
1624 scb->dma_direction = scp->sc_data_direction;
1626 pthru->dataxferlen = scsi_bufflen(scp);
1627 pthru->dataxferaddr = ccb->sgl_dma_h;
1628 pthru->numsge = megaraid_mbox_mksgl(adapter,
1629 scb);
1631 mbox->xferaddr = 0xFFFFFFFF;
1632 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1633 mbox64->xferaddr_hi = 0;
1635 return scb;
1637 case READ_6:
1638 case WRITE_6:
1639 case READ_10:
1640 case WRITE_10:
1641 case READ_12:
1642 case WRITE_12:
1645 * Allocate a SCB and initialize mailbox
1647 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1648 scp->result = (DID_ERROR << 16);
1649 *busy = 1;
1650 return NULL;
1652 ccb = (mbox_ccb_t *)scb->ccb;
1653 scb->dev_channel = 0xFF;
1654 scb->dev_target = target;
1655 mbox = ccb->mbox;
1656 mbox64 = ccb->mbox64;
1657 mbox->logdrv = target;
1660 * A little HACK: 2nd bit is zero for all scsi read
1661 * commands and is set for all scsi write commands
1663 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1664 MBOXCMD_LREAD64 ;
1667 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1669 if (scp->cmd_len == 6) {
1670 mbox->numsectors = (uint32_t)scp->cmnd[4];
1671 mbox->lba =
1672 ((uint32_t)scp->cmnd[1] << 16) |
1673 ((uint32_t)scp->cmnd[2] << 8) |
1674 (uint32_t)scp->cmnd[3];
1676 mbox->lba &= 0x1FFFFF;
1680 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1682 else if (scp->cmd_len == 10) {
1683 mbox->numsectors =
1684 (uint32_t)scp->cmnd[8] |
1685 ((uint32_t)scp->cmnd[7] << 8);
1686 mbox->lba =
1687 ((uint32_t)scp->cmnd[2] << 24) |
1688 ((uint32_t)scp->cmnd[3] << 16) |
1689 ((uint32_t)scp->cmnd[4] << 8) |
1690 (uint32_t)scp->cmnd[5];
1694 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1696 else if (scp->cmd_len == 12) {
1697 mbox->lba =
1698 ((uint32_t)scp->cmnd[2] << 24) |
1699 ((uint32_t)scp->cmnd[3] << 16) |
1700 ((uint32_t)scp->cmnd[4] << 8) |
1701 (uint32_t)scp->cmnd[5];
1703 mbox->numsectors =
1704 ((uint32_t)scp->cmnd[6] << 24) |
1705 ((uint32_t)scp->cmnd[7] << 16) |
1706 ((uint32_t)scp->cmnd[8] << 8) |
1707 (uint32_t)scp->cmnd[9];
1709 else {
1710 con_log(CL_ANN, (KERN_WARNING
1711 "megaraid: unsupported CDB length\n"));
1713 megaraid_dealloc_scb(adapter, scb);
1715 scp->result = (DID_ERROR << 16);
1716 return NULL;
1719 scb->dma_direction = scp->sc_data_direction;
1721 // Calculate Scatter-Gather info
1722 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1723 mbox->numsge = megaraid_mbox_mksgl(adapter,
1724 scb);
1725 mbox->xferaddr = 0xFFFFFFFF;
1726 mbox64->xferaddr_hi = 0;
1728 return scb;
1730 case RESERVE:
1731 case RELEASE:
1733 * Do we support clustering and is the support enabled
1735 if (!adapter->ha) {
1736 scp->result = (DID_BAD_TARGET << 16);
1737 return NULL;
1741 * Allocate a SCB and initialize mailbox
1743 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1744 scp->result = (DID_ERROR << 16);
1745 *busy = 1;
1746 return NULL;
1749 ccb = (mbox_ccb_t *)scb->ccb;
1750 scb->dev_channel = 0xFF;
1751 scb->dev_target = target;
1752 ccb->raw_mbox[0] = CLUSTER_CMD;
1753 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1754 RESERVE_LD : RELEASE_LD;
1756 ccb->raw_mbox[3] = target;
1757 scb->dma_direction = scp->sc_data_direction;
1759 return scb;
1761 default:
1762 scp->result = (DID_BAD_TARGET << 16);
1763 return NULL;
1766 else { // Passthru device commands
1768 // Do not allow access to target id > 15 or LUN > 7
1769 if (target > 15 || SCP2LUN(scp) > 7) {
1770 scp->result = (DID_BAD_TARGET << 16);
1771 return NULL;
1774 // if fast load option was set and scan for last device is
1775 // over, reset the fast_load flag so that during a possible
1776 // next scan, devices can be made available
1777 if (rdev->fast_load && (target == 15) &&
1778 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1780 con_log(CL_ANN, (KERN_INFO
1781 "megaraid[%d]: physical device scan re-enabled\n",
1782 adapter->host->host_no));
1783 rdev->fast_load = 0;
1787 * Display the channel scan for physical devices
1789 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1791 ss = rdev->fast_load ? skip : scan;
1793 con_log(CL_ANN, (KERN_INFO
1794 "scsi[%d]: %s scsi channel %d [Phy %d]",
1795 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1796 channel));
1798 con_log(CL_ANN, (
1799 " for non-raid devices\n"));
1801 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1804 // disable channel sweep if fast load option given
1805 if (rdev->fast_load) {
1806 scp->result = (DID_BAD_TARGET << 16);
1807 return NULL;
1810 // Allocate a SCB and initialize passthru
1811 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1812 scp->result = (DID_ERROR << 16);
1813 *busy = 1;
1814 return NULL;
1817 ccb = (mbox_ccb_t *)scb->ccb;
1818 scb->dev_channel = channel;
1819 scb->dev_target = target;
1820 scb->dma_direction = scp->sc_data_direction;
1821 mbox = ccb->mbox;
1822 mbox64 = ccb->mbox64;
1824 // Does this firmware support extended CDBs
1825 if (adapter->max_cdb_sz == 16) {
1826 mbox->cmd = MBOXCMD_EXTPTHRU;
1828 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1830 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1831 mbox64->xferaddr_hi = 0;
1832 mbox->xferaddr = 0xFFFFFFFF;
1834 else {
1835 mbox->cmd = MBOXCMD_PASSTHRU64;
1837 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1839 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1840 mbox64->xferaddr_hi = 0;
1841 mbox->xferaddr = 0xFFFFFFFF;
1843 return scb;
1846 // NOT REACHED
1851 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1852 * @adapter : controller's soft state
1853 * @scb_q : SCB to be queued in the pending list
1855 * Scan the pending list for commands which are not yet issued and try to
1856 * post to the controller. The SCB can be a null pointer, which would indicate
1857 * no SCB to be queue, just try to execute the ones in the pending list.
1859 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1860 * out from the head of the pending list. If it is successfully issued, the
1861 * next SCB is at the head now.
1863 static void
1864 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1866 scb_t *scb;
1867 unsigned long flags;
1869 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1871 if (scb_q) {
1872 scb_q->state = SCB_PENDQ;
1873 list_add_tail(&scb_q->list, &adapter->pend_list);
1876 // if the adapter in not in quiescent mode, post the commands to FW
1877 if (adapter->quiescent) {
1878 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1879 return;
1882 while (!list_empty(&adapter->pend_list)) {
1884 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1886 scb = list_entry(adapter->pend_list.next, scb_t, list);
1888 // remove the scb from the pending list and try to
1889 // issue. If we are unable to issue it, put back in
1890 // the pending list and return
1892 list_del_init(&scb->list);
1894 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1896 // if mailbox was busy, return SCB back to pending
1897 // list. Make sure to add at the head, since that's
1898 // where it would have been removed from
1900 scb->state = SCB_ISSUED;
1902 if (mbox_post_cmd(adapter, scb) != 0) {
1904 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1906 scb->state = SCB_PENDQ;
1908 list_add(&scb->list, &adapter->pend_list);
1910 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1911 flags);
1913 return;
1916 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1919 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1922 return;
1927 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1928 * @adapter : pointer to controller's soft state
1929 * @scb : scsi control block
1930 * @scp : scsi command from the mid-layer
1932 * Prepare a command for the scsi physical devices.
1934 static void
1935 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1936 struct scsi_cmnd *scp)
1938 mbox_ccb_t *ccb;
1939 mraid_passthru_t *pthru;
1940 uint8_t channel;
1941 uint8_t target;
1943 ccb = (mbox_ccb_t *)scb->ccb;
1944 pthru = ccb->pthru;
1945 channel = scb->dev_channel;
1946 target = scb->dev_target;
1948 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1949 pthru->timeout = 4;
1950 pthru->ars = 1;
1951 pthru->islogical = 0;
1952 pthru->channel = 0;
1953 pthru->target = (channel << 4) | target;
1954 pthru->logdrv = SCP2LUN(scp);
1955 pthru->reqsenselen = 14;
1956 pthru->cdblen = scp->cmd_len;
1958 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1960 if (scsi_bufflen(scp)) {
1961 pthru->dataxferlen = scsi_bufflen(scp);
1962 pthru->dataxferaddr = ccb->sgl_dma_h;
1963 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1965 else {
1966 pthru->dataxferaddr = 0;
1967 pthru->dataxferlen = 0;
1968 pthru->numsge = 0;
1970 return;
1975 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1976 * @adapter : pointer to controller's soft state
1977 * @scb : scsi control block
1978 * @scp : scsi command from the mid-layer
1980 * Prepare a command for the scsi physical devices. This routine prepares
1981 * commands for devices which can take extended CDBs (>10 bytes).
1983 static void
1984 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
1985 struct scsi_cmnd *scp)
1987 mbox_ccb_t *ccb;
1988 mraid_epassthru_t *epthru;
1989 uint8_t channel;
1990 uint8_t target;
1992 ccb = (mbox_ccb_t *)scb->ccb;
1993 epthru = ccb->epthru;
1994 channel = scb->dev_channel;
1995 target = scb->dev_target;
1997 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1998 epthru->timeout = 4;
1999 epthru->ars = 1;
2000 epthru->islogical = 0;
2001 epthru->channel = 0;
2002 epthru->target = (channel << 4) | target;
2003 epthru->logdrv = SCP2LUN(scp);
2004 epthru->reqsenselen = 14;
2005 epthru->cdblen = scp->cmd_len;
2007 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2009 if (scsi_bufflen(scp)) {
2010 epthru->dataxferlen = scsi_bufflen(scp);
2011 epthru->dataxferaddr = ccb->sgl_dma_h;
2012 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2014 else {
2015 epthru->dataxferaddr = 0;
2016 epthru->dataxferlen = 0;
2017 epthru->numsge = 0;
2019 return;
2024 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2025 * @adapter : controller's soft state
2027 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2028 * completed command and put them on the completed list for later processing.
2030 * Returns: 1 if the interrupt is valid, 0 otherwise
2032 static int
2033 megaraid_ack_sequence(adapter_t *adapter)
2035 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2036 mbox_t *mbox;
2037 scb_t *scb;
2038 uint8_t nstatus;
2039 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2040 struct list_head clist;
2041 int handled;
2042 uint32_t dword;
2043 unsigned long flags;
2044 int i, j;
2047 mbox = raid_dev->mbox;
2049 // move the SCBs from the firmware completed array to our local list
2050 INIT_LIST_HEAD(&clist);
2052 // loop till F/W has more commands for us to complete
2053 handled = 0;
2054 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2055 do {
2057 * Check if a valid interrupt is pending. If found, force the
2058 * interrupt line low.
2060 dword = RDOUTDOOR(raid_dev);
2061 if (dword != 0x10001234) break;
2063 handled = 1;
2065 WROUTDOOR(raid_dev, 0x10001234);
2067 nstatus = 0;
2068 // wait for valid numstatus to post
2069 for (i = 0; i < 0xFFFFF; i++) {
2070 if (mbox->numstatus != 0xFF) {
2071 nstatus = mbox->numstatus;
2072 break;
2074 rmb();
2076 mbox->numstatus = 0xFF;
2078 adapter->outstanding_cmds -= nstatus;
2080 for (i = 0; i < nstatus; i++) {
2082 // wait for valid command index to post
2083 for (j = 0; j < 0xFFFFF; j++) {
2084 if (mbox->completed[i] != 0xFF) break;
2085 rmb();
2087 completed[i] = mbox->completed[i];
2088 mbox->completed[i] = 0xFF;
2090 if (completed[i] == 0xFF) {
2091 con_log(CL_ANN, (KERN_CRIT
2092 "megaraid: command posting timed out\n"));
2094 BUG();
2095 continue;
2098 // Get SCB associated with this command id
2099 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2100 // a cmm command
2101 scb = adapter->uscb_list + (completed[i] -
2102 MBOX_MAX_SCSI_CMDS);
2104 else {
2105 // an os command
2106 scb = adapter->kscb_list + completed[i];
2109 scb->status = mbox->status;
2110 list_add_tail(&scb->list, &clist);
2113 // Acknowledge interrupt
2114 WRINDOOR(raid_dev, 0x02);
2116 } while(1);
2118 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2121 // put the completed commands in the completed list. DPC would
2122 // complete these commands later
2123 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2125 list_splice(&clist, &adapter->completed_list);
2127 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2130 // schedule the DPC if there is some work for it
2131 if (handled)
2132 tasklet_schedule(&adapter->dpc_h);
2134 return handled;
2139 * megaraid_isr - isr for memory based mailbox based controllers
2140 * @irq : irq
2141 * @devp : pointer to our soft state
2143 * Interrupt service routine for memory-mapped mailbox controllers.
2145 static irqreturn_t
2146 megaraid_isr(int irq, void *devp)
2148 adapter_t *adapter = devp;
2149 int handled;
2151 handled = megaraid_ack_sequence(adapter);
2153 /* Loop through any pending requests */
2154 if (!adapter->quiescent) {
2155 megaraid_mbox_runpendq(adapter, NULL);
2158 return IRQ_RETVAL(handled);
2163 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2164 * @devp : pointer to HBA soft state
2166 * Pick up the commands from the completed list and send back to the owners.
2167 * This is a reentrant function and does not assume any locks are held while
2168 * it is being called.
2170 static void
2171 megaraid_mbox_dpc(unsigned long devp)
2173 adapter_t *adapter = (adapter_t *)devp;
2174 mraid_device_t *raid_dev;
2175 struct list_head clist;
2176 struct scatterlist *sgl;
2177 scb_t *scb;
2178 scb_t *tmp;
2179 struct scsi_cmnd *scp;
2180 mraid_passthru_t *pthru;
2181 mraid_epassthru_t *epthru;
2182 mbox_ccb_t *ccb;
2183 int islogical;
2184 int pdev_index;
2185 int pdev_state;
2186 mbox_t *mbox;
2187 unsigned long flags;
2188 uint8_t c;
2189 int status;
2190 uioc_t *kioc;
2193 if (!adapter) return;
2195 raid_dev = ADAP2RAIDDEV(adapter);
2197 // move the SCBs from the completed list to our local list
2198 INIT_LIST_HEAD(&clist);
2200 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2202 list_splice_init(&adapter->completed_list, &clist);
2204 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2207 list_for_each_entry_safe(scb, tmp, &clist, list) {
2209 status = scb->status;
2210 scp = scb->scp;
2211 ccb = (mbox_ccb_t *)scb->ccb;
2212 pthru = ccb->pthru;
2213 epthru = ccb->epthru;
2214 mbox = ccb->mbox;
2216 // Make sure f/w has completed a valid command
2217 if (scb->state != SCB_ISSUED) {
2218 con_log(CL_ANN, (KERN_CRIT
2219 "megaraid critical err: invalid command %d:%d:%p\n",
2220 scb->sno, scb->state, scp));
2221 BUG();
2222 continue; // Must never happen!
2225 // check for the management command and complete it right away
2226 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2227 scb->state = SCB_FREE;
2228 scb->status = status;
2230 // remove from local clist
2231 list_del_init(&scb->list);
2233 kioc = (uioc_t *)scb->gp;
2234 kioc->status = 0;
2236 megaraid_mbox_mm_done(adapter, scb);
2238 continue;
2241 // Was an abort issued for this command earlier
2242 if (scb->state & SCB_ABORT) {
2243 con_log(CL_ANN, (KERN_NOTICE
2244 "megaraid: aborted cmd [%x] completed\n",
2245 scb->sno));
2249 * If the inquiry came of a disk drive which is not part of
2250 * any RAID array, expose it to the kernel. For this to be
2251 * enabled, user must set the "megaraid_expose_unconf_disks"
2252 * flag to 1 by specifying it on module parameter list.
2253 * This would enable data migration off drives from other
2254 * configurations.
2256 islogical = MRAID_IS_LOGICAL(adapter, scp);
2257 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2258 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2260 sgl = scsi_sglist(scp);
2261 if (sg_page(sgl)) {
2262 c = *(unsigned char *) sg_virt(&sgl[0]);
2263 } else {
2264 con_log(CL_ANN, (KERN_WARNING
2265 "megaraid mailbox: invalid sg:%d\n",
2266 __LINE__));
2267 c = 0;
2270 if ((c & 0x1F ) == TYPE_DISK) {
2271 pdev_index = (scb->dev_channel * 16) +
2272 scb->dev_target;
2273 pdev_state =
2274 raid_dev->pdrv_state[pdev_index] & 0x0F;
2276 if (pdev_state == PDRV_ONLINE ||
2277 pdev_state == PDRV_FAILED ||
2278 pdev_state == PDRV_RBLD ||
2279 pdev_state == PDRV_HOTSPARE ||
2280 megaraid_expose_unconf_disks == 0) {
2282 status = 0xF0;
2287 // Convert MegaRAID status to Linux error code
2288 switch (status) {
2290 case 0x00:
2292 scp->result = (DID_OK << 16);
2293 break;
2295 case 0x02:
2297 /* set sense_buffer and result fields */
2298 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2299 mbox->cmd == MBOXCMD_PASSTHRU64) {
2301 memcpy(scp->sense_buffer, pthru->reqsensearea,
2302 14);
2304 scp->result = DRIVER_SENSE << 24 |
2305 DID_OK << 16 | CHECK_CONDITION << 1;
2307 else {
2308 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2310 memcpy(scp->sense_buffer,
2311 epthru->reqsensearea, 14);
2313 scp->result = DRIVER_SENSE << 24 |
2314 DID_OK << 16 |
2315 CHECK_CONDITION << 1;
2316 } else {
2317 scp->sense_buffer[0] = 0x70;
2318 scp->sense_buffer[2] = ABORTED_COMMAND;
2319 scp->result = CHECK_CONDITION << 1;
2322 break;
2324 case 0x08:
2326 scp->result = DID_BUS_BUSY << 16 | status;
2327 break;
2329 default:
2332 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2333 * failed
2335 if (scp->cmnd[0] == TEST_UNIT_READY) {
2336 scp->result = DID_ERROR << 16 |
2337 RESERVATION_CONFLICT << 1;
2339 else
2341 * Error code returned is 1 if Reserve or Release
2342 * failed or the input parameter is invalid
2344 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2345 scp->cmnd[0] == RELEASE)) {
2347 scp->result = DID_ERROR << 16 |
2348 RESERVATION_CONFLICT << 1;
2350 else {
2351 scp->result = DID_BAD_TARGET << 16 | status;
2355 // print a debug message for all failed commands
2356 if (status) {
2357 megaraid_mbox_display_scb(adapter, scb);
2360 scsi_dma_unmap(scp);
2362 // remove from local clist
2363 list_del_init(&scb->list);
2365 // put back in free list
2366 megaraid_dealloc_scb(adapter, scb);
2368 // send the scsi packet back to kernel
2369 scp->scsi_done(scp);
2372 return;
2377 * megaraid_abort_handler - abort the scsi command
2378 * @scp : command to be aborted
2380 * Abort a previous SCSI request. Only commands on the pending list can be
2381 * aborted. All the commands issued to the F/W must complete.
2383 static int
2384 megaraid_abort_handler(struct scsi_cmnd *scp)
2386 adapter_t *adapter;
2387 mraid_device_t *raid_dev;
2388 scb_t *scb;
2389 scb_t *tmp;
2390 int found;
2391 unsigned long flags;
2392 int i;
2395 adapter = SCP2ADAPTER(scp);
2396 raid_dev = ADAP2RAIDDEV(adapter);
2398 con_log(CL_ANN, (KERN_WARNING
2399 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2400 scp->cmnd[0], SCP2CHANNEL(scp),
2401 SCP2TARGET(scp), SCP2LUN(scp)));
2403 // If FW has stopped responding, simply return failure
2404 if (raid_dev->hw_error) {
2405 con_log(CL_ANN, (KERN_NOTICE
2406 "megaraid: hw error, not aborting\n"));
2407 return FAILED;
2410 // There might a race here, where the command was completed by the
2411 // firmware and now it is on the completed list. Before we could
2412 // complete the command to the kernel in dpc, the abort came.
2413 // Find out if this is the case to avoid the race.
2414 scb = NULL;
2415 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2416 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2418 if (scb->scp == scp) { // Found command
2420 list_del_init(&scb->list); // from completed list
2422 con_log(CL_ANN, (KERN_WARNING
2423 "megaraid: %d[%d:%d], abort from completed list\n",
2424 scb->sno, scb->dev_channel, scb->dev_target));
2426 scp->result = (DID_ABORT << 16);
2427 scp->scsi_done(scp);
2429 megaraid_dealloc_scb(adapter, scb);
2431 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2432 flags);
2434 return SUCCESS;
2437 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2440 // Find out if this command is still on the pending list. If it is and
2441 // was never issued, abort and return success. If the command is owned
2442 // by the firmware, we must wait for it to complete by the FW.
2443 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2444 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2446 if (scb->scp == scp) { // Found command
2448 list_del_init(&scb->list); // from pending list
2450 ASSERT(!(scb->state & SCB_ISSUED));
2452 con_log(CL_ANN, (KERN_WARNING
2453 "megaraid abort: [%d:%d], driver owner\n",
2454 scb->dev_channel, scb->dev_target));
2456 scp->result = (DID_ABORT << 16);
2457 scp->scsi_done(scp);
2459 megaraid_dealloc_scb(adapter, scb);
2461 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2462 flags);
2464 return SUCCESS;
2467 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2470 // Check do we even own this command, in which case this would be
2471 // owned by the firmware. The only way to locate the FW scb is to
2472 // traverse through the list of all SCB, since driver does not
2473 // maintain these SCBs on any list
2474 found = 0;
2475 spin_lock_irq(&adapter->lock);
2476 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2477 scb = adapter->kscb_list + i;
2479 if (scb->scp == scp) {
2481 found = 1;
2483 if (!(scb->state & SCB_ISSUED)) {
2484 con_log(CL_ANN, (KERN_WARNING
2485 "megaraid abort: %d[%d:%d], invalid state\n",
2486 scb->sno, scb->dev_channel, scb->dev_target));
2487 BUG();
2489 else {
2490 con_log(CL_ANN, (KERN_WARNING
2491 "megaraid abort: %d[%d:%d], fw owner\n",
2492 scb->sno, scb->dev_channel, scb->dev_target));
2496 spin_unlock_irq(&adapter->lock);
2498 if (!found) {
2499 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2501 // FIXME: Should there be a callback for this command?
2502 return SUCCESS;
2505 // We cannot actually abort a command owned by firmware, return
2506 // failure and wait for reset. In host reset handler, we will find out
2507 // if the HBA is still live
2508 return FAILED;
2512 * megaraid_reset_handler - device reset handler for mailbox based driver
2513 * @scp : reference command
2515 * Reset handler for the mailbox based controller. First try to find out if
2516 * the FW is still live, in which case the outstanding commands counter mut go
2517 * down to 0. If that happens, also issue the reservation reset command to
2518 * relinquish (possible) reservations on the logical drives connected to this
2519 * host.
2521 static int
2522 megaraid_reset_handler(struct scsi_cmnd *scp)
2524 adapter_t *adapter;
2525 scb_t *scb;
2526 scb_t *tmp;
2527 mraid_device_t *raid_dev;
2528 unsigned long flags;
2529 uint8_t raw_mbox[sizeof(mbox_t)];
2530 int rval;
2531 int recovery_window;
2532 int i;
2533 uioc_t *kioc;
2535 adapter = SCP2ADAPTER(scp);
2536 raid_dev = ADAP2RAIDDEV(adapter);
2538 // return failure if adapter is not responding
2539 if (raid_dev->hw_error) {
2540 con_log(CL_ANN, (KERN_NOTICE
2541 "megaraid: hw error, cannot reset\n"));
2542 return FAILED;
2545 // Under exceptional conditions, FW can take up to 3 minutes to
2546 // complete command processing. Wait for additional 2 minutes for the
2547 // pending commands counter to go down to 0. If it doesn't, let the
2548 // controller be marked offline
2549 // Also, reset all the commands currently owned by the driver
2550 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2551 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2552 list_del_init(&scb->list); // from pending list
2554 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2555 con_log(CL_ANN, (KERN_WARNING
2556 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2557 scb->sno, scb->dev_channel, scb->dev_target));
2559 scb->status = -1;
2561 kioc = (uioc_t *)scb->gp;
2562 kioc->status = -EFAULT;
2564 megaraid_mbox_mm_done(adapter, scb);
2565 } else {
2566 if (scb->scp == scp) { // Found command
2567 con_log(CL_ANN, (KERN_WARNING
2568 "megaraid: %d[%d:%d], reset from pending list\n",
2569 scb->sno, scb->dev_channel, scb->dev_target));
2570 } else {
2571 con_log(CL_ANN, (KERN_WARNING
2572 "megaraid: IO packet with %d[%d:%d] being reset\n",
2573 scb->sno, scb->dev_channel, scb->dev_target));
2576 scb->scp->result = (DID_RESET << 16);
2577 scb->scp->scsi_done(scb->scp);
2579 megaraid_dealloc_scb(adapter, scb);
2582 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2584 if (adapter->outstanding_cmds) {
2585 con_log(CL_ANN, (KERN_NOTICE
2586 "megaraid: %d outstanding commands. Max wait %d sec\n",
2587 adapter->outstanding_cmds,
2588 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2591 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2593 for (i = 0; i < recovery_window; i++) {
2595 megaraid_ack_sequence(adapter);
2597 // print a message once every 5 seconds only
2598 if (!(i % 5)) {
2599 con_log(CL_ANN, (
2600 "megaraid mbox: Wait for %d commands to complete:%d\n",
2601 adapter->outstanding_cmds,
2602 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2605 // bailout if no recovery happened in reset time
2606 if (adapter->outstanding_cmds == 0) {
2607 break;
2610 msleep(1000);
2613 spin_lock(&adapter->lock);
2615 // If still outstanding commands, bail out
2616 if (adapter->outstanding_cmds) {
2617 con_log(CL_ANN, (KERN_WARNING
2618 "megaraid mbox: critical hardware error!\n"));
2620 raid_dev->hw_error = 1;
2622 rval = FAILED;
2623 goto out;
2625 else {
2626 con_log(CL_ANN, (KERN_NOTICE
2627 "megaraid mbox: reset sequence completed successfully\n"));
2631 // If the controller supports clustering, reset reservations
2632 if (!adapter->ha) {
2633 rval = SUCCESS;
2634 goto out;
2637 // clear reservations if any
2638 raw_mbox[0] = CLUSTER_CMD;
2639 raw_mbox[2] = RESET_RESERVATIONS;
2641 rval = SUCCESS;
2642 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2643 con_log(CL_ANN,
2644 (KERN_INFO "megaraid: reservation reset\n"));
2646 else {
2647 rval = FAILED;
2648 con_log(CL_ANN, (KERN_WARNING
2649 "megaraid: reservation reset failed\n"));
2652 out:
2653 spin_unlock(&adapter->lock);
2654 return rval;
2658 * START: internal commands library
2660 * This section of the driver has the common routine used by the driver and
2661 * also has all the FW routines
2665 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2666 * @adapter : controller's soft state
2667 * @raw_mbox : the mailbox
2669 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2670 * controllers.
2672 static int
2673 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2675 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2676 mbox_t *mbox;
2677 uint8_t status;
2678 int i;
2680 mbox = raid_dev->mbox;
2683 * Wait until mailbox is free
2685 if (megaraid_busywait_mbox(raid_dev) != 0)
2686 goto blocked_mailbox;
2689 * Copy mailbox data into host structure
2691 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2692 mbox->cmdid = 0xFE;
2693 mbox->busy = 1;
2694 mbox->poll = 0;
2695 mbox->ack = 0;
2696 mbox->numstatus = 0xFF;
2697 mbox->status = 0xFF;
2699 wmb();
2700 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2702 // wait for maximum 1 second for status to post. If the status is not
2703 // available within 1 second, assume FW is initializing and wait
2704 // for an extended amount of time
2705 if (mbox->numstatus == 0xFF) { // status not yet available
2706 udelay(25);
2708 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2709 rmb();
2710 msleep(1);
2714 if (i == 1000) {
2715 con_log(CL_ANN, (KERN_NOTICE
2716 "megaraid mailbox: wait for FW to boot "));
2718 for (i = 0; (mbox->numstatus == 0xFF) &&
2719 (i < MBOX_RESET_WAIT); i++) {
2720 rmb();
2721 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2722 MBOX_RESET_WAIT - i));
2723 msleep(1000);
2726 if (i == MBOX_RESET_WAIT) {
2728 con_log(CL_ANN, (
2729 "\nmegaraid mailbox: status not available\n"));
2731 return -1;
2733 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2737 // wait for maximum 1 second for poll semaphore
2738 if (mbox->poll != 0x77) {
2739 udelay(25);
2741 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2742 rmb();
2743 msleep(1);
2746 if (i == 1000) {
2747 con_log(CL_ANN, (KERN_WARNING
2748 "megaraid mailbox: could not get poll semaphore\n"));
2749 return -1;
2753 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2754 wmb();
2756 // wait for maximum 1 second for acknowledgement
2757 if (RDINDOOR(raid_dev) & 0x2) {
2758 udelay(25);
2760 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2761 rmb();
2762 msleep(1);
2765 if (i == 1000) {
2766 con_log(CL_ANN, (KERN_WARNING
2767 "megaraid mailbox: could not acknowledge\n"));
2768 return -1;
2771 mbox->poll = 0;
2772 mbox->ack = 0x77;
2774 status = mbox->status;
2776 // invalidate the completed command id array. After command
2777 // completion, firmware would write the valid id.
2778 mbox->numstatus = 0xFF;
2779 mbox->status = 0xFF;
2780 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2781 mbox->completed[i] = 0xFF;
2784 return status;
2786 blocked_mailbox:
2788 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2789 return -1;
2794 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2795 * @adapter : controller's soft state
2796 * @raw_mbox : the mailbox
2798 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2799 * controllers. This is a faster version of the synchronous command and
2800 * therefore can be called in interrupt-context as well.
2802 static int
2803 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2805 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2806 mbox_t *mbox;
2807 long i;
2810 mbox = raid_dev->mbox;
2812 // return immediately if the mailbox is busy
2813 if (mbox->busy) return -1;
2815 // Copy mailbox data into host structure
2816 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2817 mbox->cmdid = 0xFE;
2818 mbox->busy = 1;
2819 mbox->poll = 0;
2820 mbox->ack = 0;
2821 mbox->numstatus = 0xFF;
2822 mbox->status = 0xFF;
2824 wmb();
2825 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2827 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2828 if (mbox->numstatus != 0xFF) break;
2829 rmb();
2830 udelay(MBOX_SYNC_DELAY_200);
2833 if (i == MBOX_SYNC_WAIT_CNT) {
2834 // We may need to re-calibrate the counter
2835 con_log(CL_ANN, (KERN_CRIT
2836 "megaraid: fast sync command timed out\n"));
2839 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2840 wmb();
2842 return mbox->status;
2847 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2848 * @raid_dev : RAID device (HBA) soft state
2850 * Wait until the controller's mailbox is available to accept more commands.
2851 * Wait for at most 1 second.
2853 static int
2854 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2856 mbox_t *mbox = raid_dev->mbox;
2857 int i = 0;
2859 if (mbox->busy) {
2860 udelay(25);
2861 for (i = 0; mbox->busy && i < 1000; i++)
2862 msleep(1);
2865 if (i < 1000) return 0;
2866 else return -1;
2871 * megaraid_mbox_product_info - some static information about the controller
2872 * @adapter : our soft state
2874 * Issue commands to the controller to grab some parameters required by our
2875 * caller.
2877 static int
2878 megaraid_mbox_product_info(adapter_t *adapter)
2880 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2881 mbox_t *mbox;
2882 uint8_t raw_mbox[sizeof(mbox_t)];
2883 mraid_pinfo_t *pinfo;
2884 dma_addr_t pinfo_dma_h;
2885 mraid_inquiry3_t *mraid_inq3;
2886 int i;
2889 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2890 mbox = (mbox_t *)raw_mbox;
2893 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2894 * e.g., max channels, max commands etc.
2896 pinfo = dma_alloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2897 &pinfo_dma_h, GFP_KERNEL);
2898 if (pinfo == NULL) {
2899 con_log(CL_ANN, (KERN_WARNING
2900 "megaraid: out of memory, %s %d\n", __func__,
2901 __LINE__));
2903 return -1;
2906 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2907 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2909 raw_mbox[0] = FC_NEW_CONFIG;
2910 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2911 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2913 // Issue the command
2914 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2916 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2918 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2919 pinfo, pinfo_dma_h);
2921 return -1;
2925 * Collect information about state of each physical drive
2926 * attached to the controller. We will expose all the disks
2927 * which are not part of RAID
2929 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2930 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2931 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2935 * Get product info for information like number of channels,
2936 * maximum commands supported.
2938 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2939 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2941 raw_mbox[0] = FC_NEW_CONFIG;
2942 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
2944 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2946 con_log(CL_ANN, (KERN_WARNING
2947 "megaraid: product info failed\n"));
2949 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2950 pinfo, pinfo_dma_h);
2952 return -1;
2956 * Setup some parameters for host, as required by our caller
2958 adapter->max_channel = pinfo->nchannels;
2961 * we will export all the logical drives on a single channel.
2962 * Add 1 since inquires do not come for inititor ID
2964 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
2965 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
2968 * These are the maximum outstanding commands for the scsi-layer
2970 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
2972 memset(adapter->fw_version, 0, VERSION_SIZE);
2973 memset(adapter->bios_version, 0, VERSION_SIZE);
2975 memcpy(adapter->fw_version, pinfo->fw_version, 4);
2976 adapter->fw_version[4] = 0;
2978 memcpy(adapter->bios_version, pinfo->bios_version, 4);
2979 adapter->bios_version[4] = 0;
2981 con_log(CL_ANN, (KERN_NOTICE
2982 "megaraid: fw version:[%s] bios version:[%s]\n",
2983 adapter->fw_version, adapter->bios_version));
2985 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo,
2986 pinfo_dma_h);
2988 return 0;
2994 * megaraid_mbox_extended_cdb - check for support for extended CDBs
2995 * @adapter : soft state for the controller
2997 * This routine check whether the controller in question supports extended
2998 * ( > 10 bytes ) CDBs.
3000 static int
3001 megaraid_mbox_extended_cdb(adapter_t *adapter)
3003 mbox_t *mbox;
3004 uint8_t raw_mbox[sizeof(mbox_t)];
3005 int rval;
3007 mbox = (mbox_t *)raw_mbox;
3009 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3010 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3012 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3014 raw_mbox[0] = MAIN_MISC_OPCODE;
3015 raw_mbox[2] = SUPPORT_EXT_CDB;
3018 * Issue the command
3020 rval = 0;
3021 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3022 rval = -1;
3025 return rval;
3030 * megaraid_mbox_support_ha - Do we support clustering
3031 * @adapter : soft state for the controller
3032 * @init_id : ID of the initiator
3034 * Determine if the firmware supports clustering and the ID of the initiator.
3036 static int
3037 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3039 mbox_t *mbox;
3040 uint8_t raw_mbox[sizeof(mbox_t)];
3041 int rval;
3044 mbox = (mbox_t *)raw_mbox;
3046 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3048 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3050 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3052 raw_mbox[0] = GET_TARGET_ID;
3054 // Issue the command
3055 *init_id = 7;
3056 rval = -1;
3057 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3059 *init_id = *(uint8_t *)adapter->ibuf;
3061 con_log(CL_ANN, (KERN_INFO
3062 "megaraid: cluster firmware, initiator ID: %d\n",
3063 *init_id));
3065 rval = 0;
3068 return rval;
3073 * megaraid_mbox_support_random_del - Do we support random deletion
3074 * @adapter : soft state for the controller
3076 * Determine if the firmware supports random deletion.
3077 * Return: 1 is operation supported, 0 otherwise
3079 static int
3080 megaraid_mbox_support_random_del(adapter_t *adapter)
3082 uint8_t raw_mbox[sizeof(mbox_t)];
3083 int rval;
3086 * Newer firmware on Dell CERC expect a different
3087 * random deletion handling, so disable it.
3089 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3090 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3091 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3092 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3093 (adapter->fw_version[0] > '6' ||
3094 (adapter->fw_version[0] == '6' &&
3095 adapter->fw_version[2] > '6') ||
3096 (adapter->fw_version[0] == '6'
3097 && adapter->fw_version[2] == '6'
3098 && adapter->fw_version[3] > '1'))) {
3099 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3100 return 0;
3103 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3105 raw_mbox[0] = FC_DEL_LOGDRV;
3106 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3108 // Issue the command
3109 rval = 0;
3110 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3112 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3114 rval = 1;
3117 return rval;
3122 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3123 * @adapter : soft state for the controller
3125 * Find out the maximum number of scatter-gather elements supported by the
3126 * firmware.
3128 static int
3129 megaraid_mbox_get_max_sg(adapter_t *adapter)
3131 mbox_t *mbox;
3132 uint8_t raw_mbox[sizeof(mbox_t)];
3133 int nsg;
3136 mbox = (mbox_t *)raw_mbox;
3138 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3140 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3142 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3144 raw_mbox[0] = MAIN_MISC_OPCODE;
3145 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3147 // Issue the command
3148 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3149 nsg = *(uint8_t *)adapter->ibuf;
3151 else {
3152 nsg = MBOX_DEFAULT_SG_SIZE;
3155 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3157 return nsg;
3162 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3163 * @adapter : soft state for the controller
3165 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3166 * can be exported as regular SCSI channels.
3168 static void
3169 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3171 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3172 mbox_t *mbox;
3173 uint8_t raw_mbox[sizeof(mbox_t)];
3176 mbox = (mbox_t *)raw_mbox;
3178 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3180 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3182 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3184 raw_mbox[0] = CHNL_CLASS;
3185 raw_mbox[2] = GET_CHNL_CLASS;
3187 // Issue the command. If the command fails, all channels are RAID
3188 // channels
3189 raid_dev->channel_class = 0xFF;
3190 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3191 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3194 return;
3199 * megaraid_mbox_flush_cache - flush adapter and disks cache
3200 * @adapter : soft state for the controller
3202 * Flush adapter cache followed by disks cache.
3204 static void
3205 megaraid_mbox_flush_cache(adapter_t *adapter)
3207 uint8_t raw_mbox[sizeof(mbox_t)];
3209 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3211 raw_mbox[0] = FLUSH_ADAPTER;
3213 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3214 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3217 raw_mbox[0] = FLUSH_SYSTEM;
3219 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3220 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3223 return;
3228 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3229 * @adapter : soft state for the controller
3231 * Clears the pending cmds in FW and reinits its RAID structs.
3233 static int
3234 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3236 mbox_t *mbox;
3237 uint8_t raw_mbox[sizeof(mbox_t)];
3238 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3239 int status = 0;
3240 int i;
3241 uint32_t dword;
3243 mbox = (mbox_t *)raw_mbox;
3245 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3247 raw_mbox[0] = 0xFF;
3249 mbox = raid_dev->mbox;
3251 /* Wait until mailbox is free */
3252 if (megaraid_busywait_mbox(raid_dev) != 0) {
3253 status = 1;
3254 goto blocked_mailbox;
3257 /* Copy mailbox data into host structure */
3258 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3259 mbox->cmdid = 0xFE;
3260 mbox->busy = 1;
3261 mbox->poll = 0;
3262 mbox->ack = 0;
3263 mbox->numstatus = 0;
3264 mbox->status = 0;
3266 wmb();
3267 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3269 /* Wait for maximum 1 min for status to post.
3270 * If the Firmware SUPPORTS the ABOVE COMMAND,
3271 * mbox->cmd will be set to 0
3272 * else
3273 * the firmware will reject the command with
3274 * mbox->numstatus set to 1
3277 i = 0;
3278 status = 0;
3279 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3280 rmb();
3281 msleep(1);
3282 i++;
3283 if (i > 1000 * 60) {
3284 status = 1;
3285 break;
3288 if (mbox->numstatus == 1)
3289 status = 1; /*cmd not supported*/
3291 /* Check for interrupt line */
3292 dword = RDOUTDOOR(raid_dev);
3293 WROUTDOOR(raid_dev, dword);
3294 WRINDOOR(raid_dev,2);
3296 return status;
3298 blocked_mailbox:
3299 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3300 return status;
3304 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3305 * @adapter : controller's soft state
3306 * @scb : SCB to be displayed
3308 * Diplay information about the given SCB iff the current debug level is
3309 * verbose.
3311 static void
3312 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3314 mbox_ccb_t *ccb;
3315 struct scsi_cmnd *scp;
3316 mbox_t *mbox;
3317 int level;
3318 int i;
3321 ccb = (mbox_ccb_t *)scb->ccb;
3322 scp = scb->scp;
3323 mbox = ccb->mbox;
3325 level = CL_DLEVEL3;
3327 con_log(level, (KERN_NOTICE
3328 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3329 mbox->cmd, scb->sno));
3331 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3332 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3333 mbox->numsge));
3335 if (!scp) return;
3337 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3339 for (i = 0; i < scp->cmd_len; i++) {
3340 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3343 con_log(level, ("\n"));
3345 return;
3350 * megaraid_mbox_setup_device_map - manage device ids
3351 * @adapter : Driver's soft state
3353 * Manage the device ids to have an appropriate mapping between the kernel
3354 * scsi addresses and megaraid scsi and logical drive addresses. We export
3355 * scsi devices on their actual addresses, whereas the logical drives are
3356 * exported on a virtual scsi channel.
3358 static void
3359 megaraid_mbox_setup_device_map(adapter_t *adapter)
3361 uint8_t c;
3362 uint8_t t;
3365 * First fill the values on the logical drive channel
3367 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3368 adapter->device_ids[adapter->max_channel][t] =
3369 (t < adapter->init_id) ? t : t - 1;
3371 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3374 * Fill the values on the physical devices channels
3376 for (c = 0; c < adapter->max_channel; c++)
3377 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3378 adapter->device_ids[c][t] = (c << 8) | t;
3383 * END: internal commands library
3387 * START: Interface for the common management module
3389 * This is the module, which interfaces with the common management module to
3390 * provide support for ioctl and sysfs
3394 * megaraid_cmm_register - register with the management module
3395 * @adapter : HBA soft state
3397 * Register with the management module, which allows applications to issue
3398 * ioctl calls to the drivers. This interface is used by the management module
3399 * to setup sysfs support as well.
3401 static int
3402 megaraid_cmm_register(adapter_t *adapter)
3404 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3405 mraid_mmadp_t adp;
3406 scb_t *scb;
3407 mbox_ccb_t *ccb;
3408 int rval;
3409 int i;
3411 // Allocate memory for the base list of scb for management module.
3412 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3414 if (adapter->uscb_list == NULL) {
3415 con_log(CL_ANN, (KERN_WARNING
3416 "megaraid: out of memory, %s %d\n", __func__,
3417 __LINE__));
3418 return -1;
3422 // Initialize the synchronization parameters for resources for
3423 // commands for management module
3424 INIT_LIST_HEAD(&adapter->uscb_pool);
3426 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3430 // link all the packets. Note, CCB for commands, coming from the
3431 // commom management module, mailbox physical address are already
3432 // setup by it. We just need placeholder for that in our local command
3433 // control blocks
3434 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3436 scb = adapter->uscb_list + i;
3437 ccb = raid_dev->uccb_list + i;
3439 scb->ccb = (caddr_t)ccb;
3440 ccb->mbox64 = raid_dev->umbox64 + i;
3441 ccb->mbox = &ccb->mbox64->mbox32;
3442 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3444 scb->gp = 0;
3446 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3447 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3448 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3450 scb->scp = NULL;
3451 scb->state = SCB_FREE;
3452 scb->dma_direction = DMA_NONE;
3453 scb->dma_type = MRAID_DMA_NONE;
3454 scb->dev_channel = -1;
3455 scb->dev_target = -1;
3457 // put scb in the free pool
3458 list_add_tail(&scb->list, &adapter->uscb_pool);
3461 adp.unique_id = adapter->unique_id;
3462 adp.drvr_type = DRVRTYPE_MBOX;
3463 adp.drvr_data = (unsigned long)adapter;
3464 adp.pdev = adapter->pdev;
3465 adp.issue_uioc = megaraid_mbox_mm_handler;
3466 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3467 adp.max_kioc = MBOX_MAX_USER_CMDS;
3469 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3471 con_log(CL_ANN, (KERN_WARNING
3472 "megaraid mbox: did not register with CMM\n"));
3474 kfree(adapter->uscb_list);
3477 return rval;
3482 * megaraid_cmm_unregister - un-register with the management module
3483 * @adapter : HBA soft state
3485 * Un-register with the management module.
3486 * FIXME: mgmt module must return failure for unregister if it has pending
3487 * commands in LLD.
3489 static int
3490 megaraid_cmm_unregister(adapter_t *adapter)
3492 kfree(adapter->uscb_list);
3493 mraid_mm_unregister_adp(adapter->unique_id);
3494 return 0;
3499 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3500 * @drvr_data : LLD specific data
3501 * @kioc : CMM interface packet
3502 * @action : command action
3504 * This routine is invoked whenever the Common Management Module (CMM) has a
3505 * command for us. The 'action' parameter specifies if this is a new command
3506 * or otherwise.
3508 static int
3509 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3511 adapter_t *adapter;
3513 if (action != IOCTL_ISSUE) {
3514 con_log(CL_ANN, (KERN_WARNING
3515 "megaraid: unsupported management action:%#2x\n",
3516 action));
3517 return (-ENOTSUPP);
3520 adapter = (adapter_t *)drvr_data;
3522 // make sure this adapter is not being detached right now.
3523 if (atomic_read(&adapter->being_detached)) {
3524 con_log(CL_ANN, (KERN_WARNING
3525 "megaraid: reject management request, detaching\n"));
3526 return (-ENODEV);
3529 switch (kioc->opcode) {
3531 case GET_ADAP_INFO:
3533 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3534 (unsigned long)kioc->buf_vaddr);
3536 kioc->done(kioc);
3538 return kioc->status;
3540 case MBOX_CMD:
3542 return megaraid_mbox_mm_command(adapter, kioc);
3544 default:
3545 kioc->status = (-EINVAL);
3546 kioc->done(kioc);
3547 return (-EINVAL);
3550 return 0; // not reached
3554 * megaraid_mbox_mm_command - issues commands routed through CMM
3555 * @adapter : HBA soft state
3556 * @kioc : management command packet
3558 * Issues commands, which are routed through the management module.
3560 static int
3561 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3563 struct list_head *head = &adapter->uscb_pool;
3564 mbox64_t *mbox64;
3565 uint8_t *raw_mbox;
3566 scb_t *scb;
3567 mbox_ccb_t *ccb;
3568 unsigned long flags;
3570 // detach one scb from free pool
3571 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3573 if (list_empty(head)) { // should never happen because of CMM
3575 con_log(CL_ANN, (KERN_WARNING
3576 "megaraid mbox: bug in cmm handler, lost resources\n"));
3578 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3580 return (-EINVAL);
3583 scb = list_entry(head->next, scb_t, list);
3584 list_del_init(&scb->list);
3586 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3588 scb->state = SCB_ACTIVE;
3589 scb->dma_type = MRAID_DMA_NONE;
3590 scb->dma_direction = DMA_NONE;
3592 ccb = (mbox_ccb_t *)scb->ccb;
3593 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3594 raw_mbox = (uint8_t *)&mbox64->mbox32;
3596 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3598 scb->gp = (unsigned long)kioc;
3601 * If it is a logdrv random delete operation, we have to wait till
3602 * there are no outstanding cmds at the fw and then issue it directly
3604 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3606 if (wait_till_fw_empty(adapter)) {
3607 con_log(CL_ANN, (KERN_NOTICE
3608 "megaraid mbox: LD delete, timed out\n"));
3610 kioc->status = -ETIME;
3612 scb->status = -1;
3614 megaraid_mbox_mm_done(adapter, scb);
3616 return (-ETIME);
3619 INIT_LIST_HEAD(&scb->list);
3621 scb->state = SCB_ISSUED;
3622 if (mbox_post_cmd(adapter, scb) != 0) {
3624 con_log(CL_ANN, (KERN_NOTICE
3625 "megaraid mbox: LD delete, mailbox busy\n"));
3627 kioc->status = -EBUSY;
3629 scb->status = -1;
3631 megaraid_mbox_mm_done(adapter, scb);
3633 return (-EBUSY);
3636 return 0;
3639 // put the command on the pending list and execute
3640 megaraid_mbox_runpendq(adapter, scb);
3642 return 0;
3646 static int
3647 wait_till_fw_empty(adapter_t *adapter)
3649 unsigned long flags = 0;
3650 int i;
3654 * Set the quiescent flag to stop issuing cmds to FW.
3656 spin_lock_irqsave(&adapter->lock, flags);
3657 adapter->quiescent++;
3658 spin_unlock_irqrestore(&adapter->lock, flags);
3661 * Wait till there are no more cmds outstanding at FW. Try for at most
3662 * 60 seconds
3664 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3665 con_log(CL_DLEVEL1, (KERN_INFO
3666 "megaraid: FW has %d pending commands\n",
3667 adapter->outstanding_cmds));
3669 msleep(1000);
3672 return adapter->outstanding_cmds;
3677 * megaraid_mbox_mm_done - callback for CMM commands
3678 * @adapter : HBA soft state
3679 * @scb : completed command
3681 * Callback routine for internal commands originated from the management
3682 * module.
3684 static void
3685 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3687 uioc_t *kioc;
3688 mbox64_t *mbox64;
3689 uint8_t *raw_mbox;
3690 unsigned long flags;
3692 kioc = (uioc_t *)scb->gp;
3693 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3694 mbox64->mbox32.status = scb->status;
3695 raw_mbox = (uint8_t *)&mbox64->mbox32;
3698 // put scb in the free pool
3699 scb->state = SCB_FREE;
3700 scb->scp = NULL;
3702 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3704 list_add(&scb->list, &adapter->uscb_pool);
3706 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3708 // if a delete logical drive operation succeeded, restart the
3709 // controller
3710 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3712 adapter->quiescent--;
3714 megaraid_mbox_runpendq(adapter, NULL);
3717 kioc->done(kioc);
3719 return;
3724 * gather_hbainfo - HBA characteristics for the applications
3725 * @adapter : HBA soft state
3726 * @hinfo : pointer to the caller's host info strucuture
3728 static int
3729 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3731 hinfo->pci_vendor_id = adapter->pdev->vendor;
3732 hinfo->pci_device_id = adapter->pdev->device;
3733 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3734 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3736 hinfo->pci_bus = adapter->pdev->bus->number;
3737 hinfo->pci_dev_fn = adapter->pdev->devfn;
3738 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3739 hinfo->irq = adapter->host->irq;
3740 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3742 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3743 hinfo->host_no = adapter->host->host_no;
3745 return 0;
3749 * END: Interface for the common management module
3755 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3756 * @adapter : controller's soft state
3758 * Allocate packets required to issue FW calls whenever the sysfs attributes
3759 * are read. These attributes would require up-to-date information from the
3760 * FW. Also set up resources for mutual exclusion to share these resources and
3761 * the wait queue.
3763 * Return 0 on success.
3764 * Return -ERROR_CODE on failure.
3766 static int
3767 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3769 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3770 int rval = 0;
3772 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3774 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3776 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev,
3777 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL);
3779 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3780 !raid_dev->sysfs_buffer) {
3782 con_log(CL_ANN, (KERN_WARNING
3783 "megaraid: out of memory, %s %d\n", __func__,
3784 __LINE__));
3786 rval = -ENOMEM;
3788 megaraid_sysfs_free_resources(adapter);
3791 mutex_init(&raid_dev->sysfs_mtx);
3793 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3795 return rval;
3800 * megaraid_sysfs_free_resources - free sysfs related resources
3801 * @adapter : controller's soft state
3803 * Free packets allocated for sysfs FW commands
3805 static void
3806 megaraid_sysfs_free_resources(adapter_t *adapter)
3808 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3810 kfree(raid_dev->sysfs_uioc);
3811 kfree(raid_dev->sysfs_mbox64);
3813 if (raid_dev->sysfs_buffer) {
3814 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE,
3815 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3821 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3822 * @uioc : completed packet
3824 * Callback routine called in the ISR/tasklet context for get ldmap call
3826 static void
3827 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3829 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3830 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3832 uioc->status = 0;
3834 wake_up(&raid_dev->sysfs_wait_q);
3838 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3839 * @t : timed out timer
3841 * Timeout routine to recover and return to application, in case the adapter
3842 * has stopped responding. A timeout of 60 seconds for this command seems like
3843 * a good value.
3845 static void
3846 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
3848 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3849 uioc_t *uioc = timeout->uioc;
3850 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3851 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3853 uioc->status = -ETIME;
3855 wake_up(&raid_dev->sysfs_wait_q);
3860 * megaraid_sysfs_get_ldmap - get update logical drive map
3861 * @adapter : controller's soft state
3863 * This routine will be called whenever user reads the logical drive
3864 * attributes, go get the current logical drive mapping table from the
3865 * firmware. We use the management API's to issue commands to the controller.
3867 * NOTE: The commands issuance functionality is not generalized and
3868 * implemented in context of "get ld map" command only. If required, the
3869 * command issuance logical can be trivially pulled out and implemented as a
3870 * standalone library. For now, this should suffice since there is no other
3871 * user of this interface.
3873 * Return 0 on success.
3874 * Return -1 on failure.
3876 static int
3877 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3879 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3880 uioc_t *uioc;
3881 mbox64_t *mbox64;
3882 mbox_t *mbox;
3883 char *raw_mbox;
3884 struct uioc_timeout timeout;
3885 caddr_t ldmap;
3886 int rval = 0;
3889 * Allow only one read at a time to go through the sysfs attributes
3891 mutex_lock(&raid_dev->sysfs_mtx);
3893 uioc = raid_dev->sysfs_uioc;
3894 mbox64 = raid_dev->sysfs_mbox64;
3895 ldmap = raid_dev->sysfs_buffer;
3897 memset(uioc, 0, sizeof(uioc_t));
3898 memset(mbox64, 0, sizeof(mbox64_t));
3899 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3901 mbox = &mbox64->mbox32;
3902 raw_mbox = (char *)mbox;
3903 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3904 uioc->buf_vaddr = (caddr_t)adapter;
3905 uioc->status = -ENODATA;
3906 uioc->done = megaraid_sysfs_get_ldmap_done;
3909 * Prepare the mailbox packet to get the current logical drive mapping
3910 * table
3912 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3914 raw_mbox[0] = FC_DEL_LOGDRV;
3915 raw_mbox[2] = OP_GET_LDID_MAP;
3918 * Setup a timer to recover from a non-responding controller
3920 timeout.uioc = uioc;
3921 timer_setup_on_stack(&timeout.timer,
3922 megaraid_sysfs_get_ldmap_timeout, 0);
3924 timeout.timer.expires = jiffies + 60 * HZ;
3925 add_timer(&timeout.timer);
3928 * Send the command to the firmware
3930 rval = megaraid_mbox_mm_command(adapter, uioc);
3932 if (rval == 0) { // command successfully issued
3933 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3936 * Check if the command timed out
3938 if (uioc->status == -ETIME) {
3939 con_log(CL_ANN, (KERN_NOTICE
3940 "megaraid: sysfs get ld map timed out\n"));
3942 rval = -ETIME;
3944 else {
3945 rval = mbox->status;
3948 if (rval == 0) {
3949 memcpy(raid_dev->curr_ldmap, ldmap,
3950 sizeof(raid_dev->curr_ldmap));
3952 else {
3953 con_log(CL_ANN, (KERN_NOTICE
3954 "megaraid: get ld map failed with %x\n", rval));
3957 else {
3958 con_log(CL_ANN, (KERN_NOTICE
3959 "megaraid: could not issue ldmap command:%x\n", rval));
3963 del_timer_sync(&timeout.timer);
3964 destroy_timer_on_stack(&timeout.timer);
3966 mutex_unlock(&raid_dev->sysfs_mtx);
3968 return rval;
3973 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
3974 * @dev : class device object representation for the host
3975 * @attr : device attribute (unused)
3976 * @buf : buffer to send data to
3978 * Display the handle used by the applications while executing management
3979 * tasks on the adapter. We invoke a management module API to get the adapter
3980 * handle, since we do not interface with applications directly.
3982 static ssize_t
3983 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
3984 char *buf)
3986 struct Scsi_Host *shost = class_to_shost(dev);
3987 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
3988 uint32_t app_hndl;
3990 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
3992 return snprintf(buf, 8, "%u\n", app_hndl);
3997 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
3998 * @dev : device object representation for the scsi device
3999 * @attr : device attribute to show
4000 * @buf : buffer to send data to
4002 * Display the logical drive number for the device in question, if it a valid
4003 * logical drive. For physical devices, "-1" is returned.
4005 * The logical drive number is displayed in following format:
4007 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4009 * <int> <int> <int> <int>
4011 static ssize_t
4012 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4014 struct scsi_device *sdev = to_scsi_device(dev);
4015 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4016 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4017 int scsi_id = -1;
4018 int logical_drv = -1;
4019 int ldid_map = -1;
4020 uint32_t app_hndl = 0;
4021 int mapped_sdev_id;
4022 int rval;
4023 int i;
4025 if (raid_dev->random_del_supported &&
4026 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4028 rval = megaraid_sysfs_get_ldmap(adapter);
4029 if (rval == 0) {
4031 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4033 mapped_sdev_id = sdev->id;
4035 if (sdev->id > adapter->init_id) {
4036 mapped_sdev_id -= 1;
4039 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4041 scsi_id = sdev->id;
4043 logical_drv = i;
4045 ldid_map = raid_dev->curr_ldmap[i];
4047 app_hndl = mraid_mm_adapter_app_handle(
4048 adapter->unique_id);
4050 break;
4054 else {
4055 con_log(CL_ANN, (KERN_NOTICE
4056 "megaraid: sysfs get ld map failed: %x\n",
4057 rval));
4061 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4062 ldid_map, app_hndl);
4067 * END: Mailbox Low Level Driver
4069 module_init(megaraid_init);
4070 module_exit(megaraid_exit);
4072 /* vim: set ts=8 sw=8 tw=78 ai si: */