GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / megaraid / megaraid_mbox.c
blobe537f5efb6ee24d800de3118f260c1daf0c7f87d
1 /*
3 * Linux MegaRAID device driver
5 * Copyright (c) 2003-2004 LSI Logic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * FILE : megaraid_mbox.c
13 * Version : v2.20.5.1 (Nov 16 2006)
15 * Authors:
16 * Atul Mukker <Atul.Mukker@lsi.com>
17 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
18 * Manoj Jose <Manoj.Jose@lsi.com>
19 * Seokmann Ju
21 * List of supported controllers
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
25 * Dell PERC3/QC 101E 1960 1028 0471
26 * Dell PERC3/DC 101E 1960 1028 0493
27 * Dell PERC3/SC 101E 1960 1028 0475
28 * Dell PERC3/Di 1028 1960 1028 0123
29 * Dell PERC4/SC 1000 1960 1028 0520
30 * Dell PERC4/DC 1000 1960 1028 0518
31 * Dell PERC4/QC 1000 0407 1028 0531
32 * Dell PERC4/Di 1028 000F 1028 014A
33 * Dell PERC 4e/Si 1028 0013 1028 016c
34 * Dell PERC 4e/Di 1028 0013 1028 016d
35 * Dell PERC 4e/Di 1028 0013 1028 016e
36 * Dell PERC 4e/Di 1028 0013 1028 016f
37 * Dell PERC 4e/Di 1028 0013 1028 0170
38 * Dell PERC 4e/DC 1000 0408 1028 0002
39 * Dell PERC 4e/SC 1000 0408 1028 0001
42 * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
43 * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
44 * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
45 * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
46 * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
47 * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
48 * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
49 * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
50 * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
51 * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
52 * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
53 * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
55 * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
56 * INTEL RAID Controller SRCS16 1000 1960 8086 0523
57 * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
58 * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
59 * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
60 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
61 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
62 * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
64 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
66 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
68 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
70 * For history of changes, see Documentation/ChangeLog.megaraid
73 #include <linux/slab.h>
74 #include "megaraid_mbox.h"
76 static int megaraid_init(void);
77 static void megaraid_exit(void);
79 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
80 static void megaraid_detach_one(struct pci_dev *);
81 static void megaraid_mbox_shutdown(struct pci_dev *);
83 static int megaraid_io_attach(adapter_t *);
84 static void megaraid_io_detach(adapter_t *);
86 static int megaraid_init_mbox(adapter_t *);
87 static void megaraid_fini_mbox(adapter_t *);
89 static int megaraid_alloc_cmd_packets(adapter_t *);
90 static void megaraid_free_cmd_packets(adapter_t *);
92 static int megaraid_mbox_setup_dma_pools(adapter_t *);
93 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
95 static int megaraid_sysfs_alloc_resources(adapter_t *);
96 static void megaraid_sysfs_free_resources(adapter_t *);
98 static int megaraid_abort_handler(struct scsi_cmnd *);
99 static int megaraid_reset_handler(struct scsi_cmnd *);
101 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
102 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
103 static int megaraid_busywait_mbox(mraid_device_t *);
104 static int megaraid_mbox_product_info(adapter_t *);
105 static int megaraid_mbox_extended_cdb(adapter_t *);
106 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
107 static int megaraid_mbox_support_random_del(adapter_t *);
108 static int megaraid_mbox_get_max_sg(adapter_t *);
109 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
110 static void megaraid_mbox_flush_cache(adapter_t *);
111 static int megaraid_mbox_fire_sync_cmd(adapter_t *);
113 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
114 static void megaraid_mbox_setup_device_map(adapter_t *);
116 static int megaraid_queue_command(struct scsi_cmnd *,
117 void (*)(struct scsi_cmnd *));
118 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
119 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
120 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
121 struct scsi_cmnd *);
122 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
123 struct scsi_cmnd *);
125 static irqreturn_t megaraid_isr(int, void *);
127 static void megaraid_mbox_dpc(unsigned long);
129 static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
130 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
132 static int megaraid_cmm_register(adapter_t *);
133 static int megaraid_cmm_unregister(adapter_t *);
134 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
135 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
136 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
137 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
138 static int wait_till_fw_empty(adapter_t *);
142 MODULE_AUTHOR("megaraidlinux@lsi.com");
143 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
144 MODULE_LICENSE("GPL");
145 MODULE_VERSION(MEGARAID_VERSION);
148 * ### modules parameters for driver ###
152 * Set to enable driver to expose unconfigured disk to kernel
154 static int megaraid_expose_unconf_disks = 0;
155 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
156 MODULE_PARM_DESC(unconf_disks,
157 "Set to expose unconfigured disks to kernel (default=0)");
160 * driver wait time if the adapter's mailbox is busy
162 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
163 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
164 MODULE_PARM_DESC(busy_wait,
165 "Max wait for mailbox in microseconds if busy (default=10)");
168 * number of sectors per IO command
170 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
171 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
172 MODULE_PARM_DESC(max_sectors,
173 "Maximum number of sectors per IO command (default=128)");
176 * number of commands per logical unit
178 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
179 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
180 MODULE_PARM_DESC(cmd_per_lun,
181 "Maximum number of commands per logical unit (default=64)");
185 * Fast driver load option, skip scanning for physical devices during load.
186 * This would result in non-disk devices being skipped during driver load
187 * time. These can be later added though, using /proc/scsi/scsi
189 static unsigned int megaraid_fast_load = 0;
190 module_param_named(fast_load, megaraid_fast_load, int, 0);
191 MODULE_PARM_DESC(fast_load,
192 "Faster loading of the driver, skips physical devices! (default=0)");
196 * mraid_debug level - threshold for amount of information to be displayed by
197 * the driver. This level can be changed through modules parameters, ioctl or
198 * sysfs/proc interface. By default, print the announcement messages only.
200 int mraid_debug_level = CL_ANN;
201 module_param_named(debug_level, mraid_debug_level, int, 0);
202 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
205 * ### global data ###
207 static uint8_t megaraid_mbox_version[8] =
208 { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
212 * PCI table for all supported controllers.
214 static struct pci_device_id pci_id_table_g[] = {
216 PCI_VENDOR_ID_DELL,
217 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
218 PCI_VENDOR_ID_DELL,
219 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
222 PCI_VENDOR_ID_LSI_LOGIC,
223 PCI_DEVICE_ID_PERC4_SC,
224 PCI_VENDOR_ID_DELL,
225 PCI_SUBSYS_ID_PERC4_SC,
228 PCI_VENDOR_ID_LSI_LOGIC,
229 PCI_DEVICE_ID_PERC4_DC,
230 PCI_VENDOR_ID_DELL,
231 PCI_SUBSYS_ID_PERC4_DC,
234 PCI_VENDOR_ID_LSI_LOGIC,
235 PCI_DEVICE_ID_VERDE,
236 PCI_ANY_ID,
237 PCI_ANY_ID,
240 PCI_VENDOR_ID_DELL,
241 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
242 PCI_VENDOR_ID_DELL,
243 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
246 PCI_VENDOR_ID_DELL,
247 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
248 PCI_VENDOR_ID_DELL,
249 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
252 PCI_VENDOR_ID_DELL,
253 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
254 PCI_VENDOR_ID_DELL,
255 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
258 PCI_VENDOR_ID_DELL,
259 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
260 PCI_VENDOR_ID_DELL,
261 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
264 PCI_VENDOR_ID_DELL,
265 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
266 PCI_VENDOR_ID_DELL,
267 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
270 PCI_VENDOR_ID_DELL,
271 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
272 PCI_VENDOR_ID_DELL,
273 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
276 PCI_VENDOR_ID_LSI_LOGIC,
277 PCI_DEVICE_ID_DOBSON,
278 PCI_ANY_ID,
279 PCI_ANY_ID,
282 PCI_VENDOR_ID_AMI,
283 PCI_DEVICE_ID_AMI_MEGARAID3,
284 PCI_ANY_ID,
285 PCI_ANY_ID,
288 PCI_VENDOR_ID_LSI_LOGIC,
289 PCI_DEVICE_ID_AMI_MEGARAID3,
290 PCI_ANY_ID,
291 PCI_ANY_ID,
294 PCI_VENDOR_ID_LSI_LOGIC,
295 PCI_DEVICE_ID_LINDSAY,
296 PCI_ANY_ID,
297 PCI_ANY_ID,
299 {0} /* Terminating entry */
301 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
304 static struct pci_driver megaraid_pci_driver = {
305 .name = "megaraid",
306 .id_table = pci_id_table_g,
307 .probe = megaraid_probe_one,
308 .remove = __devexit_p(megaraid_detach_one),
309 .shutdown = megaraid_mbox_shutdown,
314 // definitions for the device attributes for exporting logical drive number
315 // for a scsi address (Host, Channel, Id, Lun)
317 DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
318 NULL);
320 // Host template initializer for megaraid mbox sysfs device attributes
321 static struct device_attribute *megaraid_shost_attrs[] = {
322 &dev_attr_megaraid_mbox_app_hndl,
323 NULL,
327 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
329 // Host template initializer for megaraid mbox sysfs device attributes
330 static struct device_attribute *megaraid_sdev_attrs[] = {
331 &dev_attr_megaraid_mbox_ld,
332 NULL,
336 * megaraid_change_queue_depth - Change the device's queue depth
337 * @sdev: scsi device struct
338 * @qdepth: depth to set
339 * @reason: calling context
341 * Return value:
342 * actual depth set
344 static int megaraid_change_queue_depth(struct scsi_device *sdev, int qdepth,
345 int reason)
347 if (reason != SCSI_QDEPTH_DEFAULT)
348 return -EOPNOTSUPP;
350 if (qdepth > MBOX_MAX_SCSI_CMDS)
351 qdepth = MBOX_MAX_SCSI_CMDS;
352 scsi_adjust_queue_depth(sdev, 0, qdepth);
353 return sdev->queue_depth;
357 * Scsi host template for megaraid unified driver
359 static struct scsi_host_template megaraid_template_g = {
360 .module = THIS_MODULE,
361 .name = "LSI Logic MegaRAID driver",
362 .proc_name = "megaraid",
363 .queuecommand = megaraid_queue_command,
364 .eh_abort_handler = megaraid_abort_handler,
365 .eh_device_reset_handler = megaraid_reset_handler,
366 .eh_bus_reset_handler = megaraid_reset_handler,
367 .eh_host_reset_handler = megaraid_reset_handler,
368 .change_queue_depth = megaraid_change_queue_depth,
369 .use_clustering = ENABLE_CLUSTERING,
370 .sdev_attrs = megaraid_sdev_attrs,
371 .shost_attrs = megaraid_shost_attrs,
376 * megaraid_init - module load hook
378 * We register ourselves as hotplug enabled module and let PCI subsystem
379 * discover our adapters.
381 static int __init
382 megaraid_init(void)
384 int rval;
386 // Announce the driver version
387 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
388 MEGARAID_EXT_VERSION));
390 // check validity of module parameters
391 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
393 con_log(CL_ANN, (KERN_WARNING
394 "megaraid mailbox: max commands per lun reset to %d\n",
395 MBOX_MAX_SCSI_CMDS));
397 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
401 // register as a PCI hot-plug driver module
402 rval = pci_register_driver(&megaraid_pci_driver);
403 if (rval < 0) {
404 con_log(CL_ANN, (KERN_WARNING
405 "megaraid: could not register hotplug support.\n"));
408 return rval;
413 * megaraid_exit - driver unload entry point
415 * We simply unwrap the megaraid_init routine here.
417 static void __exit
418 megaraid_exit(void)
420 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
422 // unregister as PCI hotplug driver
423 pci_unregister_driver(&megaraid_pci_driver);
425 return;
430 * megaraid_probe_one - PCI hotplug entry point
431 * @pdev : handle to this controller's PCI configuration space
432 * @id : pci device id of the class of controllers
434 * This routine should be called whenever a new adapter is detected by the
435 * PCI hotplug susbsystem.
437 static int __devinit
438 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
440 adapter_t *adapter;
443 // detected a new controller
444 con_log(CL_ANN, (KERN_INFO
445 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
446 pdev->vendor, pdev->device, pdev->subsystem_vendor,
447 pdev->subsystem_device));
449 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
450 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
452 if (pci_enable_device(pdev)) {
453 con_log(CL_ANN, (KERN_WARNING
454 "megaraid: pci_enable_device failed\n"));
456 return -ENODEV;
459 // Enable bus-mastering on this controller
460 pci_set_master(pdev);
462 // Allocate the per driver initialization structure
463 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
465 if (adapter == NULL) {
466 con_log(CL_ANN, (KERN_WARNING
467 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
469 goto out_probe_one;
473 // set up PCI related soft state and other pre-known parameters
474 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
475 adapter->irq = pdev->irq;
476 adapter->pdev = pdev;
478 atomic_set(&adapter->being_detached, 0);
480 // Setup the default DMA mask. This would be changed later on
481 // depending on hardware capabilities
482 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
484 con_log(CL_ANN, (KERN_WARNING
485 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
487 goto out_free_adapter;
491 // Initialize the synchronization lock for kernel and LLD
492 spin_lock_init(&adapter->lock);
494 // Initialize the command queues: the list of free SCBs and the list
495 // of pending SCBs.
496 INIT_LIST_HEAD(&adapter->kscb_pool);
497 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
499 INIT_LIST_HEAD(&adapter->pend_list);
500 spin_lock_init(PENDING_LIST_LOCK(adapter));
502 INIT_LIST_HEAD(&adapter->completed_list);
503 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
506 // Start the mailbox based controller
507 if (megaraid_init_mbox(adapter) != 0) {
508 con_log(CL_ANN, (KERN_WARNING
509 "megaraid: maibox adapter did not initialize\n"));
511 goto out_free_adapter;
514 // Register with LSI Common Management Module
515 if (megaraid_cmm_register(adapter) != 0) {
517 con_log(CL_ANN, (KERN_WARNING
518 "megaraid: could not register with management module\n"));
520 goto out_fini_mbox;
523 // setup adapter handle in PCI soft state
524 pci_set_drvdata(pdev, adapter);
526 // attach with scsi mid-layer
527 if (megaraid_io_attach(adapter) != 0) {
529 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
531 goto out_cmm_unreg;
534 return 0;
536 out_cmm_unreg:
537 pci_set_drvdata(pdev, NULL);
538 megaraid_cmm_unregister(adapter);
539 out_fini_mbox:
540 megaraid_fini_mbox(adapter);
541 out_free_adapter:
542 kfree(adapter);
543 out_probe_one:
544 pci_disable_device(pdev);
546 return -ENODEV;
551 * megaraid_detach_one - release framework resources and call LLD release routine
552 * @pdev : handle for our PCI cofiguration space
554 * This routine is called during driver unload. We free all the allocated
555 * resources and call the corresponding LLD so that it can also release all
556 * its resources.
558 * This routine is also called from the PCI hotplug system.
560 static void
561 megaraid_detach_one(struct pci_dev *pdev)
563 adapter_t *adapter;
564 struct Scsi_Host *host;
567 // Start a rollback on this adapter
568 adapter = pci_get_drvdata(pdev);
570 if (!adapter) {
571 con_log(CL_ANN, (KERN_CRIT
572 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
573 pdev->vendor, pdev->device, pdev->subsystem_vendor,
574 pdev->subsystem_device));
576 return;
578 else {
579 con_log(CL_ANN, (KERN_NOTICE
580 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
581 pdev->vendor, pdev->device, pdev->subsystem_vendor,
582 pdev->subsystem_device));
586 host = adapter->host;
588 // do not allow any more requests from the management module for this
589 // adapter.
590 // pending with us?
591 atomic_set(&adapter->being_detached, 1);
593 // detach from the IO sub-system
594 megaraid_io_detach(adapter);
596 // reset the device state in the PCI structure. We check this
597 // condition when we enter here. If the device state is NULL,
598 // that would mean the device has already been removed
599 pci_set_drvdata(pdev, NULL);
601 // Unregister from common management module
603 // is a command pending with LLD or not.
604 megaraid_cmm_unregister(adapter);
606 // finalize the mailbox based controller and release all resources
607 megaraid_fini_mbox(adapter);
609 kfree(adapter);
611 scsi_host_put(host);
613 pci_disable_device(pdev);
615 return;
620 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
621 * @pdev : generic driver model device
623 * Shutdown notification, perform flush cache.
625 static void
626 megaraid_mbox_shutdown(struct pci_dev *pdev)
628 adapter_t *adapter = pci_get_drvdata(pdev);
629 static int counter;
631 if (!adapter) {
632 con_log(CL_ANN, (KERN_WARNING
633 "megaraid: null device in shutdown\n"));
634 return;
637 // flush caches now
638 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
639 counter++));
641 megaraid_mbox_flush_cache(adapter);
643 con_log(CL_ANN, ("done\n"));
648 * megaraid_io_attach - attach a device with the IO subsystem
649 * @adapter : controller's soft state
651 * Attach this device with the IO subsystem.
653 static int
654 megaraid_io_attach(adapter_t *adapter)
656 struct Scsi_Host *host;
658 // Initialize SCSI Host structure
659 host = scsi_host_alloc(&megaraid_template_g, 8);
660 if (!host) {
661 con_log(CL_ANN, (KERN_WARNING
662 "megaraid mbox: scsi_register failed\n"));
664 return -1;
667 SCSIHOST2ADAP(host) = (caddr_t)adapter;
668 adapter->host = host;
670 host->irq = adapter->irq;
671 host->unique_id = adapter->unique_id;
672 host->can_queue = adapter->max_cmds;
673 host->this_id = adapter->init_id;
674 host->sg_tablesize = adapter->sglen;
675 host->max_sectors = adapter->max_sectors;
676 host->cmd_per_lun = adapter->cmd_per_lun;
677 host->max_channel = adapter->max_channel;
678 host->max_id = adapter->max_target;
679 host->max_lun = adapter->max_lun;
682 // notify mid-layer about the new controller
683 if (scsi_add_host(host, &adapter->pdev->dev)) {
685 con_log(CL_ANN, (KERN_WARNING
686 "megaraid mbox: scsi_add_host failed\n"));
688 scsi_host_put(host);
690 return -1;
693 scsi_scan_host(host);
695 return 0;
700 * megaraid_io_detach - detach a device from the IO subsystem
701 * @adapter : controller's soft state
703 * Detach this device from the IO subsystem.
705 static void
706 megaraid_io_detach(adapter_t *adapter)
708 struct Scsi_Host *host;
710 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
712 host = adapter->host;
714 scsi_remove_host(host);
716 return;
721 * START: Mailbox Low Level Driver
723 * This is section specific to the single mailbox based controllers
727 * megaraid_init_mbox - initialize controller
728 * @adapter : our soft state
730 * - Allocate 16-byte aligned mailbox memory for firmware handshake
731 * - Allocate controller's memory resources
732 * - Find out all initialization data
733 * - Allocate memory required for all the commands
734 * - Use internal library of FW routines, build up complete soft state
736 static int __devinit
737 megaraid_init_mbox(adapter_t *adapter)
739 struct pci_dev *pdev;
740 mraid_device_t *raid_dev;
741 int i;
742 uint32_t magic64;
745 adapter->ito = MBOX_TIMEOUT;
746 pdev = adapter->pdev;
749 * Allocate and initialize the init data structure for mailbox
750 * controllers
752 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
753 if (raid_dev == NULL) return -1;
757 * Attach the adapter soft state to raid device soft state
759 adapter->raid_device = (caddr_t)raid_dev;
760 raid_dev->fast_load = megaraid_fast_load;
763 // our baseport
764 raid_dev->baseport = pci_resource_start(pdev, 0);
766 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
768 con_log(CL_ANN, (KERN_WARNING
769 "megaraid: mem region busy\n"));
771 goto out_free_raid_dev;
774 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
776 if (!raid_dev->baseaddr) {
778 con_log(CL_ANN, (KERN_WARNING
779 "megaraid: could not map hba memory\n") );
781 goto out_release_regions;
784 /* initialize the mutual exclusion lock for the mailbox */
785 spin_lock_init(&raid_dev->mailbox_lock);
787 /* allocate memory required for commands */
788 if (megaraid_alloc_cmd_packets(adapter) != 0)
789 goto out_iounmap;
792 * Issue SYNC cmd to flush the pending cmds in the adapter
793 * and initialize its internal state
796 if (megaraid_mbox_fire_sync_cmd(adapter))
797 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
800 * Setup the rest of the soft state using the library of
801 * FW routines
804 /* request IRQ and register the interrupt service routine */
805 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
806 adapter)) {
808 con_log(CL_ANN, (KERN_WARNING
809 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
810 goto out_alloc_cmds;
814 // Product info
815 if (megaraid_mbox_product_info(adapter) != 0)
816 goto out_free_irq;
818 // Do we support extended CDBs
819 adapter->max_cdb_sz = 10;
820 if (megaraid_mbox_extended_cdb(adapter) == 0) {
821 adapter->max_cdb_sz = 16;
825 * Do we support cluster environment, if we do, what is the initiator
826 * id.
827 * NOTE: In a non-cluster aware firmware environment, the LLD should
828 * return 7 as initiator id.
830 adapter->ha = 0;
831 adapter->init_id = -1;
832 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
833 adapter->ha = 1;
837 * Prepare the device ids array to have the mapping between the kernel
838 * device address and megaraid device address.
839 * We export the physical devices on their actual addresses. The
840 * logical drives are exported on a virtual SCSI channel
842 megaraid_mbox_setup_device_map(adapter);
844 // If the firmware supports random deletion, update the device id map
845 if (megaraid_mbox_support_random_del(adapter)) {
847 // Change the logical drives numbers in device_ids array one
848 // slot in device_ids is reserved for target id, that's why
849 // "<=" below
850 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
851 adapter->device_ids[adapter->max_channel][i] += 0x80;
853 adapter->device_ids[adapter->max_channel][adapter->init_id] =
854 0xFF;
856 raid_dev->random_del_supported = 1;
860 * find out the maximum number of scatter-gather elements supported by
861 * this firmware
863 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
865 // enumerate RAID and SCSI channels so that all devices on SCSI
866 // channels can later be exported, including disk devices
867 megaraid_mbox_enum_raid_scsi(adapter);
870 * Other parameters required by upper layer
872 * maximum number of sectors per IO command
874 adapter->max_sectors = megaraid_max_sectors;
877 * number of queued commands per LUN.
879 adapter->cmd_per_lun = megaraid_cmd_per_lun;
882 * Allocate resources required to issue FW calls, when sysfs is
883 * accessed
885 if (megaraid_sysfs_alloc_resources(adapter) != 0)
886 goto out_free_irq;
888 // Set the DMA mask to 64-bit. All supported controllers as capable of
889 // DMA in this range
890 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
892 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
893 ((adapter->pdev->subsystem_device !=
894 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
895 (adapter->pdev->subsystem_device !=
896 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
897 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
898 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
899 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
900 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
901 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
902 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
903 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
904 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
905 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
906 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
907 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
908 con_log(CL_ANN, (KERN_WARNING
909 "megaraid: DMA mask for 64-bit failed\n"));
911 if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
912 con_log(CL_ANN, (KERN_WARNING
913 "megaraid: 32-bit DMA mask failed\n"));
914 goto out_free_sysfs_res;
919 // setup tasklet for DPC
920 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
921 (unsigned long)adapter);
923 con_log(CL_DLEVEL1, (KERN_INFO
924 "megaraid mbox hba successfully initialized\n"));
926 return 0;
928 out_free_sysfs_res:
929 megaraid_sysfs_free_resources(adapter);
930 out_free_irq:
931 free_irq(adapter->irq, adapter);
932 out_alloc_cmds:
933 megaraid_free_cmd_packets(adapter);
934 out_iounmap:
935 iounmap(raid_dev->baseaddr);
936 out_release_regions:
937 pci_release_regions(pdev);
938 out_free_raid_dev:
939 kfree(raid_dev);
941 return -1;
946 * megaraid_fini_mbox - undo controller initialization
947 * @adapter : our soft state
949 static void
950 megaraid_fini_mbox(adapter_t *adapter)
952 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
954 // flush all caches
955 megaraid_mbox_flush_cache(adapter);
957 tasklet_kill(&adapter->dpc_h);
959 megaraid_sysfs_free_resources(adapter);
961 megaraid_free_cmd_packets(adapter);
963 free_irq(adapter->irq, adapter);
965 iounmap(raid_dev->baseaddr);
967 pci_release_regions(adapter->pdev);
969 kfree(raid_dev);
971 return;
976 * megaraid_alloc_cmd_packets - allocate shared mailbox
977 * @adapter : soft state of the raid controller
979 * Allocate and align the shared mailbox. This maibox is used to issue
980 * all the commands. For IO based controllers, the mailbox is also regsitered
981 * with the FW. Allocate memory for all commands as well.
982 * This is our big allocator.
984 static int
985 megaraid_alloc_cmd_packets(adapter_t *adapter)
987 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
988 struct pci_dev *pdev;
989 unsigned long align;
990 scb_t *scb;
991 mbox_ccb_t *ccb;
992 struct mraid_pci_blk *epthru_pci_blk;
993 struct mraid_pci_blk *sg_pci_blk;
994 struct mraid_pci_blk *mbox_pci_blk;
995 int i;
997 pdev = adapter->pdev;
1000 * Setup the mailbox
1001 * Allocate the common 16-byte aligned memory for the handshake
1002 * mailbox.
1004 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1005 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1007 if (!raid_dev->una_mbox64) {
1008 con_log(CL_ANN, (KERN_WARNING
1009 "megaraid: out of memory, %s %d\n", __func__,
1010 __LINE__));
1011 return -1;
1013 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1016 * Align the mailbox at 16-byte boundary
1018 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1020 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1021 (~0UL ^ 0xFUL));
1023 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1025 align = ((void *)raid_dev->mbox -
1026 ((void *)&raid_dev->una_mbox64->mbox32));
1028 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1029 align;
1031 // Allocate memory for commands issued internally
1032 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1033 &adapter->ibuf_dma_h);
1034 if (!adapter->ibuf) {
1036 con_log(CL_ANN, (KERN_WARNING
1037 "megaraid: out of memory, %s %d\n", __func__,
1038 __LINE__));
1040 goto out_free_common_mbox;
1042 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1044 // Allocate memory for our SCSI Command Blocks and their associated
1045 // memory
1048 * Allocate memory for the base list of scb. Later allocate memory for
1049 * CCBs and embedded components of each CCB and point the pointers in
1050 * scb to the allocated components
1051 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1052 * since the calling routine does not yet know the number of available
1053 * commands.
1055 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1057 if (adapter->kscb_list == NULL) {
1058 con_log(CL_ANN, (KERN_WARNING
1059 "megaraid: out of memory, %s %d\n", __func__,
1060 __LINE__));
1061 goto out_free_ibuf;
1064 // memory allocation for our command packets
1065 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1066 con_log(CL_ANN, (KERN_WARNING
1067 "megaraid: out of memory, %s %d\n", __func__,
1068 __LINE__));
1069 goto out_free_scb_list;
1072 // Adjust the scb pointers and link in the free pool
1073 epthru_pci_blk = raid_dev->epthru_pool;
1074 sg_pci_blk = raid_dev->sg_pool;
1075 mbox_pci_blk = raid_dev->mbox_pool;
1077 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1078 scb = adapter->kscb_list + i;
1079 ccb = raid_dev->ccb_list + i;
1081 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1082 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1083 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1084 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1086 // make sure the mailbox is aligned properly
1087 if (ccb->mbox_dma_h & 0x0F) {
1088 con_log(CL_ANN, (KERN_CRIT
1089 "megaraid mbox: not aligned on 16-bytes\n"));
1091 goto out_teardown_dma_pools;
1094 ccb->epthru = (mraid_epassthru_t *)
1095 epthru_pci_blk[i].vaddr;
1096 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1097 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1098 ccb->pthru_dma_h = ccb->epthru_dma_h;
1101 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1102 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1103 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1105 scb->ccb = (caddr_t)ccb;
1106 scb->gp = 0;
1108 scb->sno = i; // command index
1110 scb->scp = NULL;
1111 scb->state = SCB_FREE;
1112 scb->dma_direction = PCI_DMA_NONE;
1113 scb->dma_type = MRAID_DMA_NONE;
1114 scb->dev_channel = -1;
1115 scb->dev_target = -1;
1117 // put scb in the free pool
1118 list_add_tail(&scb->list, &adapter->kscb_pool);
1121 return 0;
1123 out_teardown_dma_pools:
1124 megaraid_mbox_teardown_dma_pools(adapter);
1125 out_free_scb_list:
1126 kfree(adapter->kscb_list);
1127 out_free_ibuf:
1128 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1129 adapter->ibuf_dma_h);
1130 out_free_common_mbox:
1131 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1132 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1134 return -1;
1139 * megaraid_free_cmd_packets - free memory
1140 * @adapter : soft state of the raid controller
1142 * Release memory resources allocated for commands.
1144 static void
1145 megaraid_free_cmd_packets(adapter_t *adapter)
1147 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1149 megaraid_mbox_teardown_dma_pools(adapter);
1151 kfree(adapter->kscb_list);
1153 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1154 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1156 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1157 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1158 return;
1163 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1164 * @adapter : HBA soft state
1166 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1167 * and scatter-gather lists.
1169 static int
1170 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1172 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1173 struct mraid_pci_blk *epthru_pci_blk;
1174 struct mraid_pci_blk *sg_pci_blk;
1175 struct mraid_pci_blk *mbox_pci_blk;
1176 int i;
1180 // Allocate memory for 16-bytes aligned mailboxes
1181 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1182 adapter->pdev,
1183 sizeof(mbox64_t) + 16,
1184 16, 0);
1186 if (raid_dev->mbox_pool_handle == NULL) {
1187 goto fail_setup_dma_pool;
1190 mbox_pci_blk = raid_dev->mbox_pool;
1191 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1192 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1193 raid_dev->mbox_pool_handle,
1194 GFP_KERNEL,
1195 &mbox_pci_blk[i].dma_addr);
1196 if (!mbox_pci_blk[i].vaddr) {
1197 goto fail_setup_dma_pool;
1202 * Allocate memory for each embedded passthru strucuture pointer
1203 * Request for a 128 bytes aligned structure for each passthru command
1204 * structure
1205 * Since passthru and extended passthru commands are exclusive, they
1206 * share common memory pool. Passthru structures piggyback on memory
1207 * allocted to extended passthru since passthru is smaller of the two
1209 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1210 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1212 if (raid_dev->epthru_pool_handle == NULL) {
1213 goto fail_setup_dma_pool;
1216 epthru_pci_blk = raid_dev->epthru_pool;
1217 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1218 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1219 raid_dev->epthru_pool_handle,
1220 GFP_KERNEL,
1221 &epthru_pci_blk[i].dma_addr);
1222 if (!epthru_pci_blk[i].vaddr) {
1223 goto fail_setup_dma_pool;
1228 // Allocate memory for each scatter-gather list. Request for 512 bytes
1229 // alignment for each sg list
1230 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1231 adapter->pdev,
1232 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1233 512, 0);
1235 if (raid_dev->sg_pool_handle == NULL) {
1236 goto fail_setup_dma_pool;
1239 sg_pci_blk = raid_dev->sg_pool;
1240 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1241 sg_pci_blk[i].vaddr = pci_pool_alloc(
1242 raid_dev->sg_pool_handle,
1243 GFP_KERNEL,
1244 &sg_pci_blk[i].dma_addr);
1245 if (!sg_pci_blk[i].vaddr) {
1246 goto fail_setup_dma_pool;
1250 return 0;
1252 fail_setup_dma_pool:
1253 megaraid_mbox_teardown_dma_pools(adapter);
1254 return -1;
1259 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1260 * @adapter : HBA soft state
1262 * Teardown the dma pool for mailbox, passthru and extended passthru
1263 * structures, and scatter-gather lists.
1265 static void
1266 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1268 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1269 struct mraid_pci_blk *epthru_pci_blk;
1270 struct mraid_pci_blk *sg_pci_blk;
1271 struct mraid_pci_blk *mbox_pci_blk;
1272 int i;
1275 sg_pci_blk = raid_dev->sg_pool;
1276 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1277 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1278 sg_pci_blk[i].dma_addr);
1280 if (raid_dev->sg_pool_handle)
1281 pci_pool_destroy(raid_dev->sg_pool_handle);
1284 epthru_pci_blk = raid_dev->epthru_pool;
1285 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1286 pci_pool_free(raid_dev->epthru_pool_handle,
1287 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1289 if (raid_dev->epthru_pool_handle)
1290 pci_pool_destroy(raid_dev->epthru_pool_handle);
1293 mbox_pci_blk = raid_dev->mbox_pool;
1294 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1295 pci_pool_free(raid_dev->mbox_pool_handle,
1296 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1298 if (raid_dev->mbox_pool_handle)
1299 pci_pool_destroy(raid_dev->mbox_pool_handle);
1301 return;
1306 * megaraid_alloc_scb - detach and return a scb from the free list
1307 * @adapter : controller's soft state
1308 * @scp : pointer to the scsi command to be executed
1310 * Return the scb from the head of the free list. %NULL if there are none
1311 * available.
1313 static scb_t *
1314 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1316 struct list_head *head = &adapter->kscb_pool;
1317 scb_t *scb = NULL;
1318 unsigned long flags;
1320 // detach scb from free pool
1321 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1323 if (list_empty(head)) {
1324 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1325 return NULL;
1328 scb = list_entry(head->next, scb_t, list);
1329 list_del_init(&scb->list);
1331 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1333 scb->state = SCB_ACTIVE;
1334 scb->scp = scp;
1335 scb->dma_type = MRAID_DMA_NONE;
1337 return scb;
1342 * megaraid_dealloc_scb - return the scb to the free pool
1343 * @adapter : controller's soft state
1344 * @scb : scb to be freed
1346 * Return the scb back to the free list of scbs. The caller must 'flush' the
1347 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1348 * NOTE NOTE: Make sure the scb is not on any list before calling this
1349 * routine.
1351 static inline void
1352 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1354 unsigned long flags;
1356 // put scb in the free pool
1357 scb->state = SCB_FREE;
1358 scb->scp = NULL;
1359 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1361 list_add(&scb->list, &adapter->kscb_pool);
1363 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1365 return;
1370 * megaraid_mbox_mksgl - make the scatter-gather list
1371 * @adapter : controller's soft state
1372 * @scb : scsi control block
1374 * Prepare the scatter-gather list.
1376 static int
1377 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1379 struct scatterlist *sgl;
1380 mbox_ccb_t *ccb;
1381 struct scsi_cmnd *scp;
1382 int sgcnt;
1383 int i;
1386 scp = scb->scp;
1387 ccb = (mbox_ccb_t *)scb->ccb;
1389 sgcnt = scsi_dma_map(scp);
1390 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1392 // no mapping required if no data to be transferred
1393 if (!sgcnt)
1394 return 0;
1396 scb->dma_type = MRAID_DMA_WSG;
1398 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1399 ccb->sgl64[i].address = sg_dma_address(sgl);
1400 ccb->sgl64[i].length = sg_dma_len(sgl);
1403 // Return count of SG nodes
1404 return sgcnt;
1409 * mbox_post_cmd - issue a mailbox command
1410 * @adapter : controller's soft state
1411 * @scb : command to be issued
1413 * Post the command to the controller if mailbox is available.
1415 static int
1416 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1418 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1419 mbox64_t *mbox64;
1420 mbox_t *mbox;
1421 mbox_ccb_t *ccb;
1422 unsigned long flags;
1423 unsigned int i = 0;
1426 ccb = (mbox_ccb_t *)scb->ccb;
1427 mbox = raid_dev->mbox;
1428 mbox64 = raid_dev->mbox64;
1431 * Check for busy mailbox. If it is, return failure - the caller
1432 * should retry later.
1434 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1436 if (unlikely(mbox->busy)) {
1437 do {
1438 udelay(1);
1439 i++;
1440 rmb();
1441 } while(mbox->busy && (i < max_mbox_busy_wait));
1443 if (mbox->busy) {
1445 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1447 return -1;
1452 // Copy this command's mailbox data into "adapter's" mailbox
1453 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1454 mbox->cmdid = scb->sno;
1456 adapter->outstanding_cmds++;
1458 if (scb->dma_direction == PCI_DMA_TODEVICE)
1459 pci_dma_sync_sg_for_device(adapter->pdev,
1460 scsi_sglist(scb->scp),
1461 scsi_sg_count(scb->scp),
1462 PCI_DMA_TODEVICE);
1464 mbox->busy = 1; // Set busy
1465 mbox->poll = 0;
1466 mbox->ack = 0;
1467 wmb();
1469 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1471 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1473 return 0;
1478 * megaraid_queue_command - generic queue entry point for all LLDs
1479 * @scp : pointer to the scsi command to be executed
1480 * @done : callback routine to be called after the cmd has be completed
1482 * Queue entry point for mailbox based controllers.
1484 static int
1485 megaraid_queue_command(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1487 adapter_t *adapter;
1488 scb_t *scb;
1489 int if_busy;
1491 adapter = SCP2ADAPTER(scp);
1492 scp->scsi_done = done;
1493 scp->result = 0;
1496 * Allocate and build a SCB request
1497 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1498 * not allocate scb. We will return non-zero status in that case.
1499 * NOTE: scb can be null even though certain commands completed
1500 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1501 * return 0 in that case, and we would do the callback right away.
1503 if_busy = 0;
1504 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1505 if (!scb) { // command already completed
1506 done(scp);
1507 return 0;
1510 megaraid_mbox_runpendq(adapter, scb);
1511 return if_busy;
1515 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1516 * @adapter : controller's soft state
1517 * @scp : mid-layer scsi command pointer
1518 * @busy : set if request could not be completed because of lack of
1519 * resources
1521 * Transform the mid-layer scsi command to megaraid firmware lingua.
1522 * Convert the command issued by mid-layer to format understood by megaraid
1523 * firmware. We also complete certain commands without sending them to firmware.
1525 static scb_t *
1526 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1528 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1529 int channel;
1530 int target;
1531 int islogical;
1532 mbox_ccb_t *ccb;
1533 mraid_passthru_t *pthru;
1534 mbox64_t *mbox64;
1535 mbox_t *mbox;
1536 scb_t *scb;
1537 char skip[] = "skipping";
1538 char scan[] = "scanning";
1539 char *ss;
1543 * Get the appropriate device map for the device this command is
1544 * intended for
1546 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1549 * Logical drive commands
1551 if (islogical) {
1552 switch (scp->cmnd[0]) {
1553 case TEST_UNIT_READY:
1555 * Do we support clustering and is the support enabled
1556 * If no, return success always
1558 if (!adapter->ha) {
1559 scp->result = (DID_OK << 16);
1560 return NULL;
1563 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1564 scp->result = (DID_ERROR << 16);
1565 *busy = 1;
1566 return NULL;
1569 scb->dma_direction = scp->sc_data_direction;
1570 scb->dev_channel = 0xFF;
1571 scb->dev_target = target;
1572 ccb = (mbox_ccb_t *)scb->ccb;
1575 * The command id will be provided by the command
1576 * issuance routine
1578 ccb->raw_mbox[0] = CLUSTER_CMD;
1579 ccb->raw_mbox[2] = RESERVATION_STATUS;
1580 ccb->raw_mbox[3] = target;
1582 return scb;
1584 case MODE_SENSE:
1586 struct scatterlist *sgl;
1587 caddr_t vaddr;
1589 sgl = scsi_sglist(scp);
1590 if (sg_page(sgl)) {
1591 vaddr = (caddr_t) sg_virt(&sgl[0]);
1593 memset(vaddr, 0, scp->cmnd[4]);
1595 else {
1596 con_log(CL_ANN, (KERN_WARNING
1597 "megaraid mailbox: invalid sg:%d\n",
1598 __LINE__));
1601 scp->result = (DID_OK << 16);
1602 return NULL;
1604 case INQUIRY:
1606 * Display the channel scan for logical drives
1607 * Do not display scan for a channel if already done.
1609 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1611 con_log(CL_ANN, (KERN_INFO
1612 "scsi[%d]: scanning scsi channel %d",
1613 adapter->host->host_no,
1614 SCP2CHANNEL(scp)));
1616 con_log(CL_ANN, (
1617 " [virtual] for logical drives\n"));
1619 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1622 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1623 scp->sense_buffer[0] = 0x70;
1624 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1625 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1626 scp->result = CHECK_CONDITION << 1;
1627 return NULL;
1630 /* Fall through */
1632 case READ_CAPACITY:
1634 * Do not allow LUN > 0 for logical drives and
1635 * requests for more than 40 logical drives
1637 if (SCP2LUN(scp)) {
1638 scp->result = (DID_BAD_TARGET << 16);
1639 return NULL;
1641 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1642 scp->result = (DID_BAD_TARGET << 16);
1643 return NULL;
1647 /* Allocate a SCB and initialize passthru */
1648 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1649 scp->result = (DID_ERROR << 16);
1650 *busy = 1;
1651 return NULL;
1654 ccb = (mbox_ccb_t *)scb->ccb;
1655 scb->dev_channel = 0xFF;
1656 scb->dev_target = target;
1657 pthru = ccb->pthru;
1658 mbox = ccb->mbox;
1659 mbox64 = ccb->mbox64;
1661 pthru->timeout = 0;
1662 pthru->ars = 1;
1663 pthru->reqsenselen = 14;
1664 pthru->islogical = 1;
1665 pthru->logdrv = target;
1666 pthru->cdblen = scp->cmd_len;
1667 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1669 mbox->cmd = MBOXCMD_PASSTHRU64;
1670 scb->dma_direction = scp->sc_data_direction;
1672 pthru->dataxferlen = scsi_bufflen(scp);
1673 pthru->dataxferaddr = ccb->sgl_dma_h;
1674 pthru->numsge = megaraid_mbox_mksgl(adapter,
1675 scb);
1677 mbox->xferaddr = 0xFFFFFFFF;
1678 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1679 mbox64->xferaddr_hi = 0;
1681 return scb;
1683 case READ_6:
1684 case WRITE_6:
1685 case READ_10:
1686 case WRITE_10:
1687 case READ_12:
1688 case WRITE_12:
1691 * Allocate a SCB and initialize mailbox
1693 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1694 scp->result = (DID_ERROR << 16);
1695 *busy = 1;
1696 return NULL;
1698 ccb = (mbox_ccb_t *)scb->ccb;
1699 scb->dev_channel = 0xFF;
1700 scb->dev_target = target;
1701 mbox = ccb->mbox;
1702 mbox64 = ccb->mbox64;
1703 mbox->logdrv = target;
1706 * A little HACK: 2nd bit is zero for all scsi read
1707 * commands and is set for all scsi write commands
1709 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1710 MBOXCMD_LREAD64 ;
1713 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1715 if (scp->cmd_len == 6) {
1716 mbox->numsectors = (uint32_t)scp->cmnd[4];
1717 mbox->lba =
1718 ((uint32_t)scp->cmnd[1] << 16) |
1719 ((uint32_t)scp->cmnd[2] << 8) |
1720 (uint32_t)scp->cmnd[3];
1722 mbox->lba &= 0x1FFFFF;
1726 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1728 else if (scp->cmd_len == 10) {
1729 mbox->numsectors =
1730 (uint32_t)scp->cmnd[8] |
1731 ((uint32_t)scp->cmnd[7] << 8);
1732 mbox->lba =
1733 ((uint32_t)scp->cmnd[2] << 24) |
1734 ((uint32_t)scp->cmnd[3] << 16) |
1735 ((uint32_t)scp->cmnd[4] << 8) |
1736 (uint32_t)scp->cmnd[5];
1740 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1742 else if (scp->cmd_len == 12) {
1743 mbox->lba =
1744 ((uint32_t)scp->cmnd[2] << 24) |
1745 ((uint32_t)scp->cmnd[3] << 16) |
1746 ((uint32_t)scp->cmnd[4] << 8) |
1747 (uint32_t)scp->cmnd[5];
1749 mbox->numsectors =
1750 ((uint32_t)scp->cmnd[6] << 24) |
1751 ((uint32_t)scp->cmnd[7] << 16) |
1752 ((uint32_t)scp->cmnd[8] << 8) |
1753 (uint32_t)scp->cmnd[9];
1755 else {
1756 con_log(CL_ANN, (KERN_WARNING
1757 "megaraid: unsupported CDB length\n"));
1759 megaraid_dealloc_scb(adapter, scb);
1761 scp->result = (DID_ERROR << 16);
1762 return NULL;
1765 scb->dma_direction = scp->sc_data_direction;
1767 // Calculate Scatter-Gather info
1768 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1769 mbox->numsge = megaraid_mbox_mksgl(adapter,
1770 scb);
1771 mbox->xferaddr = 0xFFFFFFFF;
1772 mbox64->xferaddr_hi = 0;
1774 return scb;
1776 case RESERVE:
1777 case RELEASE:
1779 * Do we support clustering and is the support enabled
1781 if (!adapter->ha) {
1782 scp->result = (DID_BAD_TARGET << 16);
1783 return NULL;
1787 * Allocate a SCB and initialize mailbox
1789 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1790 scp->result = (DID_ERROR << 16);
1791 *busy = 1;
1792 return NULL;
1795 ccb = (mbox_ccb_t *)scb->ccb;
1796 scb->dev_channel = 0xFF;
1797 scb->dev_target = target;
1798 ccb->raw_mbox[0] = CLUSTER_CMD;
1799 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1800 RESERVE_LD : RELEASE_LD;
1802 ccb->raw_mbox[3] = target;
1803 scb->dma_direction = scp->sc_data_direction;
1805 return scb;
1807 default:
1808 scp->result = (DID_BAD_TARGET << 16);
1809 return NULL;
1812 else { // Passthru device commands
1814 // Do not allow access to target id > 15 or LUN > 7
1815 if (target > 15 || SCP2LUN(scp) > 7) {
1816 scp->result = (DID_BAD_TARGET << 16);
1817 return NULL;
1820 // if fast load option was set and scan for last device is
1821 // over, reset the fast_load flag so that during a possible
1822 // next scan, devices can be made available
1823 if (rdev->fast_load && (target == 15) &&
1824 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1826 con_log(CL_ANN, (KERN_INFO
1827 "megaraid[%d]: physical device scan re-enabled\n",
1828 adapter->host->host_no));
1829 rdev->fast_load = 0;
1833 * Display the channel scan for physical devices
1835 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1837 ss = rdev->fast_load ? skip : scan;
1839 con_log(CL_ANN, (KERN_INFO
1840 "scsi[%d]: %s scsi channel %d [Phy %d]",
1841 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1842 channel));
1844 con_log(CL_ANN, (
1845 " for non-raid devices\n"));
1847 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1850 // disable channel sweep if fast load option given
1851 if (rdev->fast_load) {
1852 scp->result = (DID_BAD_TARGET << 16);
1853 return NULL;
1856 // Allocate a SCB and initialize passthru
1857 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1858 scp->result = (DID_ERROR << 16);
1859 *busy = 1;
1860 return NULL;
1863 ccb = (mbox_ccb_t *)scb->ccb;
1864 scb->dev_channel = channel;
1865 scb->dev_target = target;
1866 scb->dma_direction = scp->sc_data_direction;
1867 mbox = ccb->mbox;
1868 mbox64 = ccb->mbox64;
1870 // Does this firmware support extended CDBs
1871 if (adapter->max_cdb_sz == 16) {
1872 mbox->cmd = MBOXCMD_EXTPTHRU;
1874 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1876 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1877 mbox64->xferaddr_hi = 0;
1878 mbox->xferaddr = 0xFFFFFFFF;
1880 else {
1881 mbox->cmd = MBOXCMD_PASSTHRU64;
1883 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1885 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1886 mbox64->xferaddr_hi = 0;
1887 mbox->xferaddr = 0xFFFFFFFF;
1889 return scb;
1892 // NOT REACHED
1897 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1898 * @adapter : controller's soft state
1899 * @scb_q : SCB to be queued in the pending list
1901 * Scan the pending list for commands which are not yet issued and try to
1902 * post to the controller. The SCB can be a null pointer, which would indicate
1903 * no SCB to be queue, just try to execute the ones in the pending list.
1905 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1906 * out from the head of the pending list. If it is successfully issued, the
1907 * next SCB is at the head now.
1909 static void
1910 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1912 scb_t *scb;
1913 unsigned long flags;
1915 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1917 if (scb_q) {
1918 scb_q->state = SCB_PENDQ;
1919 list_add_tail(&scb_q->list, &adapter->pend_list);
1922 // if the adapter in not in quiescent mode, post the commands to FW
1923 if (adapter->quiescent) {
1924 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1925 return;
1928 while (!list_empty(&adapter->pend_list)) {
1930 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1932 scb = list_entry(adapter->pend_list.next, scb_t, list);
1934 // remove the scb from the pending list and try to
1935 // issue. If we are unable to issue it, put back in
1936 // the pending list and return
1938 list_del_init(&scb->list);
1940 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1942 // if mailbox was busy, return SCB back to pending
1943 // list. Make sure to add at the head, since that's
1944 // where it would have been removed from
1946 scb->state = SCB_ISSUED;
1948 if (mbox_post_cmd(adapter, scb) != 0) {
1950 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1952 scb->state = SCB_PENDQ;
1954 list_add(&scb->list, &adapter->pend_list);
1956 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1957 flags);
1959 return;
1962 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1965 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1968 return;
1973 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1974 * @adapter : pointer to controller's soft state
1975 * @scb : scsi control block
1976 * @scp : scsi command from the mid-layer
1978 * Prepare a command for the scsi physical devices.
1980 static void
1981 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1982 struct scsi_cmnd *scp)
1984 mbox_ccb_t *ccb;
1985 mraid_passthru_t *pthru;
1986 uint8_t channel;
1987 uint8_t target;
1989 ccb = (mbox_ccb_t *)scb->ccb;
1990 pthru = ccb->pthru;
1991 channel = scb->dev_channel;
1992 target = scb->dev_target;
1994 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1995 pthru->timeout = 4;
1996 pthru->ars = 1;
1997 pthru->islogical = 0;
1998 pthru->channel = 0;
1999 pthru->target = (channel << 4) | target;
2000 pthru->logdrv = SCP2LUN(scp);
2001 pthru->reqsenselen = 14;
2002 pthru->cdblen = scp->cmd_len;
2004 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2006 if (scsi_bufflen(scp)) {
2007 pthru->dataxferlen = scsi_bufflen(scp);
2008 pthru->dataxferaddr = ccb->sgl_dma_h;
2009 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2011 else {
2012 pthru->dataxferaddr = 0;
2013 pthru->dataxferlen = 0;
2014 pthru->numsge = 0;
2016 return;
2021 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2022 * @adapter : pointer to controller's soft state
2023 * @scb : scsi control block
2024 * @scp : scsi command from the mid-layer
2026 * Prepare a command for the scsi physical devices. This rountine prepares
2027 * commands for devices which can take extended CDBs (>10 bytes).
2029 static void
2030 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2031 struct scsi_cmnd *scp)
2033 mbox_ccb_t *ccb;
2034 mraid_epassthru_t *epthru;
2035 uint8_t channel;
2036 uint8_t target;
2038 ccb = (mbox_ccb_t *)scb->ccb;
2039 epthru = ccb->epthru;
2040 channel = scb->dev_channel;
2041 target = scb->dev_target;
2043 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2044 epthru->timeout = 4;
2045 epthru->ars = 1;
2046 epthru->islogical = 0;
2047 epthru->channel = 0;
2048 epthru->target = (channel << 4) | target;
2049 epthru->logdrv = SCP2LUN(scp);
2050 epthru->reqsenselen = 14;
2051 epthru->cdblen = scp->cmd_len;
2053 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2055 if (scsi_bufflen(scp)) {
2056 epthru->dataxferlen = scsi_bufflen(scp);
2057 epthru->dataxferaddr = ccb->sgl_dma_h;
2058 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2060 else {
2061 epthru->dataxferaddr = 0;
2062 epthru->dataxferlen = 0;
2063 epthru->numsge = 0;
2065 return;
2070 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2071 * @adapter : controller's soft state
2073 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2074 * completed command and put them on the completed list for later processing.
2076 * Returns: 1 if the interrupt is valid, 0 otherwise
2078 static int
2079 megaraid_ack_sequence(adapter_t *adapter)
2081 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2082 mbox_t *mbox;
2083 scb_t *scb;
2084 uint8_t nstatus;
2085 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2086 struct list_head clist;
2087 int handled;
2088 uint32_t dword;
2089 unsigned long flags;
2090 int i, j;
2093 mbox = raid_dev->mbox;
2095 // move the SCBs from the firmware completed array to our local list
2096 INIT_LIST_HEAD(&clist);
2098 // loop till F/W has more commands for us to complete
2099 handled = 0;
2100 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2101 do {
2103 * Check if a valid interrupt is pending. If found, force the
2104 * interrupt line low.
2106 dword = RDOUTDOOR(raid_dev);
2107 if (dword != 0x10001234) break;
2109 handled = 1;
2111 WROUTDOOR(raid_dev, 0x10001234);
2113 nstatus = 0;
2114 // wait for valid numstatus to post
2115 for (i = 0; i < 0xFFFFF; i++) {
2116 if (mbox->numstatus != 0xFF) {
2117 nstatus = mbox->numstatus;
2118 break;
2120 rmb();
2122 mbox->numstatus = 0xFF;
2124 adapter->outstanding_cmds -= nstatus;
2126 for (i = 0; i < nstatus; i++) {
2128 // wait for valid command index to post
2129 for (j = 0; j < 0xFFFFF; j++) {
2130 if (mbox->completed[i] != 0xFF) break;
2131 rmb();
2133 completed[i] = mbox->completed[i];
2134 mbox->completed[i] = 0xFF;
2136 if (completed[i] == 0xFF) {
2137 con_log(CL_ANN, (KERN_CRIT
2138 "megaraid: command posting timed out\n"));
2140 BUG();
2141 continue;
2144 // Get SCB associated with this command id
2145 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2146 // a cmm command
2147 scb = adapter->uscb_list + (completed[i] -
2148 MBOX_MAX_SCSI_CMDS);
2150 else {
2151 // an os command
2152 scb = adapter->kscb_list + completed[i];
2155 scb->status = mbox->status;
2156 list_add_tail(&scb->list, &clist);
2159 // Acknowledge interrupt
2160 WRINDOOR(raid_dev, 0x02);
2162 } while(1);
2164 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2167 // put the completed commands in the completed list. DPC would
2168 // complete these commands later
2169 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2171 list_splice(&clist, &adapter->completed_list);
2173 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2176 // schedule the DPC if there is some work for it
2177 if (handled)
2178 tasklet_schedule(&adapter->dpc_h);
2180 return handled;
2185 * megaraid_isr - isr for memory based mailbox based controllers
2186 * @irq : irq
2187 * @devp : pointer to our soft state
2189 * Interrupt service routine for memory-mapped mailbox controllers.
2191 static irqreturn_t
2192 megaraid_isr(int irq, void *devp)
2194 adapter_t *adapter = devp;
2195 int handled;
2197 handled = megaraid_ack_sequence(adapter);
2199 /* Loop through any pending requests */
2200 if (!adapter->quiescent) {
2201 megaraid_mbox_runpendq(adapter, NULL);
2204 return IRQ_RETVAL(handled);
2209 * megaraid_mbox_sync_scb - sync kernel buffers
2210 * @adapter : controller's soft state
2211 * @scb : pointer to the resource packet
2213 * DMA sync if required.
2215 static void
2216 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2218 mbox_ccb_t *ccb;
2220 ccb = (mbox_ccb_t *)scb->ccb;
2222 if (scb->dma_direction == PCI_DMA_FROMDEVICE)
2223 pci_dma_sync_sg_for_cpu(adapter->pdev,
2224 scsi_sglist(scb->scp),
2225 scsi_sg_count(scb->scp),
2226 PCI_DMA_FROMDEVICE);
2228 scsi_dma_unmap(scb->scp);
2229 return;
2234 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2235 * @devp : pointer to HBA soft state
2237 * Pick up the commands from the completed list and send back to the owners.
2238 * This is a reentrant function and does not assume any locks are held while
2239 * it is being called.
2241 static void
2242 megaraid_mbox_dpc(unsigned long devp)
2244 adapter_t *adapter = (adapter_t *)devp;
2245 mraid_device_t *raid_dev;
2246 struct list_head clist;
2247 struct scatterlist *sgl;
2248 scb_t *scb;
2249 scb_t *tmp;
2250 struct scsi_cmnd *scp;
2251 mraid_passthru_t *pthru;
2252 mraid_epassthru_t *epthru;
2253 mbox_ccb_t *ccb;
2254 int islogical;
2255 int pdev_index;
2256 int pdev_state;
2257 mbox_t *mbox;
2258 unsigned long flags;
2259 uint8_t c;
2260 int status;
2261 uioc_t *kioc;
2264 if (!adapter) return;
2266 raid_dev = ADAP2RAIDDEV(adapter);
2268 // move the SCBs from the completed list to our local list
2269 INIT_LIST_HEAD(&clist);
2271 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2273 list_splice_init(&adapter->completed_list, &clist);
2275 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2278 list_for_each_entry_safe(scb, tmp, &clist, list) {
2280 status = scb->status;
2281 scp = scb->scp;
2282 ccb = (mbox_ccb_t *)scb->ccb;
2283 pthru = ccb->pthru;
2284 epthru = ccb->epthru;
2285 mbox = ccb->mbox;
2287 // Make sure f/w has completed a valid command
2288 if (scb->state != SCB_ISSUED) {
2289 con_log(CL_ANN, (KERN_CRIT
2290 "megaraid critical err: invalid command %d:%d:%p\n",
2291 scb->sno, scb->state, scp));
2292 BUG();
2293 continue; // Must never happen!
2296 // check for the management command and complete it right away
2297 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2298 scb->state = SCB_FREE;
2299 scb->status = status;
2301 // remove from local clist
2302 list_del_init(&scb->list);
2304 kioc = (uioc_t *)scb->gp;
2305 kioc->status = 0;
2307 megaraid_mbox_mm_done(adapter, scb);
2309 continue;
2312 // Was an abort issued for this command earlier
2313 if (scb->state & SCB_ABORT) {
2314 con_log(CL_ANN, (KERN_NOTICE
2315 "megaraid: aborted cmd %lx[%x] completed\n",
2316 scp->serial_number, scb->sno));
2320 * If the inquiry came of a disk drive which is not part of
2321 * any RAID array, expose it to the kernel. For this to be
2322 * enabled, user must set the "megaraid_expose_unconf_disks"
2323 * flag to 1 by specifying it on module parameter list.
2324 * This would enable data migration off drives from other
2325 * configurations.
2327 islogical = MRAID_IS_LOGICAL(adapter, scp);
2328 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2329 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2331 sgl = scsi_sglist(scp);
2332 if (sg_page(sgl)) {
2333 c = *(unsigned char *) sg_virt(&sgl[0]);
2334 } else {
2335 con_log(CL_ANN, (KERN_WARNING
2336 "megaraid mailbox: invalid sg:%d\n",
2337 __LINE__));
2338 c = 0;
2341 if ((c & 0x1F ) == TYPE_DISK) {
2342 pdev_index = (scb->dev_channel * 16) +
2343 scb->dev_target;
2344 pdev_state =
2345 raid_dev->pdrv_state[pdev_index] & 0x0F;
2347 if (pdev_state == PDRV_ONLINE ||
2348 pdev_state == PDRV_FAILED ||
2349 pdev_state == PDRV_RBLD ||
2350 pdev_state == PDRV_HOTSPARE ||
2351 megaraid_expose_unconf_disks == 0) {
2353 status = 0xF0;
2358 // Convert MegaRAID status to Linux error code
2359 switch (status) {
2361 case 0x00:
2363 scp->result = (DID_OK << 16);
2364 break;
2366 case 0x02:
2368 /* set sense_buffer and result fields */
2369 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2370 mbox->cmd == MBOXCMD_PASSTHRU64) {
2372 memcpy(scp->sense_buffer, pthru->reqsensearea,
2373 14);
2375 scp->result = DRIVER_SENSE << 24 |
2376 DID_OK << 16 | CHECK_CONDITION << 1;
2378 else {
2379 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2381 memcpy(scp->sense_buffer,
2382 epthru->reqsensearea, 14);
2384 scp->result = DRIVER_SENSE << 24 |
2385 DID_OK << 16 |
2386 CHECK_CONDITION << 1;
2387 } else {
2388 scp->sense_buffer[0] = 0x70;
2389 scp->sense_buffer[2] = ABORTED_COMMAND;
2390 scp->result = CHECK_CONDITION << 1;
2393 break;
2395 case 0x08:
2397 scp->result = DID_BUS_BUSY << 16 | status;
2398 break;
2400 default:
2403 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2404 * failed
2406 if (scp->cmnd[0] == TEST_UNIT_READY) {
2407 scp->result = DID_ERROR << 16 |
2408 RESERVATION_CONFLICT << 1;
2410 else
2412 * Error code returned is 1 if Reserve or Release
2413 * failed or the input parameter is invalid
2415 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2416 scp->cmnd[0] == RELEASE)) {
2418 scp->result = DID_ERROR << 16 |
2419 RESERVATION_CONFLICT << 1;
2421 else {
2422 scp->result = DID_BAD_TARGET << 16 | status;
2426 // print a debug message for all failed commands
2427 if (status) {
2428 megaraid_mbox_display_scb(adapter, scb);
2431 // Free our internal resources and call the mid-layer callback
2432 // routine
2433 megaraid_mbox_sync_scb(adapter, scb);
2435 // remove from local clist
2436 list_del_init(&scb->list);
2438 // put back in free list
2439 megaraid_dealloc_scb(adapter, scb);
2441 // send the scsi packet back to kernel
2442 scp->scsi_done(scp);
2445 return;
2450 * megaraid_abort_handler - abort the scsi command
2451 * @scp : command to be aborted
2453 * Abort a previous SCSI request. Only commands on the pending list can be
2454 * aborted. All the commands issued to the F/W must complete.
2456 static int
2457 megaraid_abort_handler(struct scsi_cmnd *scp)
2459 adapter_t *adapter;
2460 mraid_device_t *raid_dev;
2461 scb_t *scb;
2462 scb_t *tmp;
2463 int found;
2464 unsigned long flags;
2465 int i;
2468 adapter = SCP2ADAPTER(scp);
2469 raid_dev = ADAP2RAIDDEV(adapter);
2471 con_log(CL_ANN, (KERN_WARNING
2472 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2473 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2474 SCP2TARGET(scp), SCP2LUN(scp)));
2476 // If FW has stopped responding, simply return failure
2477 if (raid_dev->hw_error) {
2478 con_log(CL_ANN, (KERN_NOTICE
2479 "megaraid: hw error, not aborting\n"));
2480 return FAILED;
2483 // There might a race here, where the command was completed by the
2484 // firmware and now it is on the completed list. Before we could
2485 // complete the command to the kernel in dpc, the abort came.
2486 // Find out if this is the case to avoid the race.
2487 scb = NULL;
2488 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2489 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2491 if (scb->scp == scp) { // Found command
2493 list_del_init(&scb->list); // from completed list
2495 con_log(CL_ANN, (KERN_WARNING
2496 "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2497 scp->serial_number, scb->sno,
2498 scb->dev_channel, scb->dev_target));
2500 scp->result = (DID_ABORT << 16);
2501 scp->scsi_done(scp);
2503 megaraid_dealloc_scb(adapter, scb);
2505 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2506 flags);
2508 return SUCCESS;
2511 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2514 // Find out if this command is still on the pending list. If it is and
2515 // was never issued, abort and return success. If the command is owned
2516 // by the firmware, we must wait for it to complete by the FW.
2517 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2518 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2520 if (scb->scp == scp) { // Found command
2522 list_del_init(&scb->list); // from pending list
2524 ASSERT(!(scb->state & SCB_ISSUED));
2526 con_log(CL_ANN, (KERN_WARNING
2527 "megaraid abort: %ld[%d:%d], driver owner\n",
2528 scp->serial_number, scb->dev_channel,
2529 scb->dev_target));
2531 scp->result = (DID_ABORT << 16);
2532 scp->scsi_done(scp);
2534 megaraid_dealloc_scb(adapter, scb);
2536 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2537 flags);
2539 return SUCCESS;
2542 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2545 // Check do we even own this command, in which case this would be
2546 // owned by the firmware. The only way to locate the FW scb is to
2547 // traverse through the list of all SCB, since driver does not
2548 // maintain these SCBs on any list
2549 found = 0;
2550 spin_lock_irq(&adapter->lock);
2551 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2552 scb = adapter->kscb_list + i;
2554 if (scb->scp == scp) {
2556 found = 1;
2558 if (!(scb->state & SCB_ISSUED)) {
2559 con_log(CL_ANN, (KERN_WARNING
2560 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2561 scp->serial_number, scb->sno, scb->dev_channel,
2562 scb->dev_target));
2563 BUG();
2565 else {
2566 con_log(CL_ANN, (KERN_WARNING
2567 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2568 scp->serial_number, scb->sno, scb->dev_channel,
2569 scb->dev_target));
2573 spin_unlock_irq(&adapter->lock);
2575 if (!found) {
2576 con_log(CL_ANN, (KERN_WARNING
2577 "megaraid abort: scsi cmd:%ld, do now own\n",
2578 scp->serial_number));
2580 return SUCCESS;
2583 // We cannot actually abort a command owned by firmware, return
2584 // failure and wait for reset. In host reset handler, we will find out
2585 // if the HBA is still live
2586 return FAILED;
2590 * megaraid_reset_handler - device reset hadler for mailbox based driver
2591 * @scp : reference command
2593 * Reset handler for the mailbox based controller. First try to find out if
2594 * the FW is still live, in which case the outstanding commands counter mut go
2595 * down to 0. If that happens, also issue the reservation reset command to
2596 * relinquish (possible) reservations on the logical drives connected to this
2597 * host.
2599 static int
2600 megaraid_reset_handler(struct scsi_cmnd *scp)
2602 adapter_t *adapter;
2603 scb_t *scb;
2604 scb_t *tmp;
2605 mraid_device_t *raid_dev;
2606 unsigned long flags;
2607 uint8_t raw_mbox[sizeof(mbox_t)];
2608 int rval;
2609 int recovery_window;
2610 int recovering;
2611 int i;
2612 uioc_t *kioc;
2614 adapter = SCP2ADAPTER(scp);
2615 raid_dev = ADAP2RAIDDEV(adapter);
2617 // return failure if adapter is not responding
2618 if (raid_dev->hw_error) {
2619 con_log(CL_ANN, (KERN_NOTICE
2620 "megaraid: hw error, cannot reset\n"));
2621 return FAILED;
2625 // Under exceptional conditions, FW can take up to 3 minutes to
2626 // complete command processing. Wait for additional 2 minutes for the
2627 // pending commands counter to go down to 0. If it doesn't, let the
2628 // controller be marked offline
2629 // Also, reset all the commands currently owned by the driver
2630 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2631 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2632 list_del_init(&scb->list); // from pending list
2634 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2635 con_log(CL_ANN, (KERN_WARNING
2636 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2637 scb->sno, scb->dev_channel, scb->dev_target));
2639 scb->status = -1;
2641 kioc = (uioc_t *)scb->gp;
2642 kioc->status = -EFAULT;
2644 megaraid_mbox_mm_done(adapter, scb);
2645 } else {
2646 if (scb->scp == scp) { // Found command
2647 con_log(CL_ANN, (KERN_WARNING
2648 "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2649 scp->serial_number, scb->sno,
2650 scb->dev_channel, scb->dev_target));
2651 } else {
2652 con_log(CL_ANN, (KERN_WARNING
2653 "megaraid: IO packet with %d[%d:%d] being reset\n",
2654 scb->sno, scb->dev_channel, scb->dev_target));
2657 scb->scp->result = (DID_RESET << 16);
2658 scb->scp->scsi_done(scb->scp);
2660 megaraid_dealloc_scb(adapter, scb);
2663 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2665 if (adapter->outstanding_cmds) {
2666 con_log(CL_ANN, (KERN_NOTICE
2667 "megaraid: %d outstanding commands. Max wait %d sec\n",
2668 adapter->outstanding_cmds,
2669 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2672 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2674 recovering = adapter->outstanding_cmds;
2676 for (i = 0; i < recovery_window; i++) {
2678 megaraid_ack_sequence(adapter);
2680 // print a message once every 5 seconds only
2681 if (!(i % 5)) {
2682 con_log(CL_ANN, (
2683 "megaraid mbox: Wait for %d commands to complete:%d\n",
2684 adapter->outstanding_cmds,
2685 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2688 // bailout if no recovery happended in reset time
2689 if (adapter->outstanding_cmds == 0) {
2690 break;
2693 msleep(1000);
2696 spin_lock(&adapter->lock);
2698 // If still outstanding commands, bail out
2699 if (adapter->outstanding_cmds) {
2700 con_log(CL_ANN, (KERN_WARNING
2701 "megaraid mbox: critical hardware error!\n"));
2703 raid_dev->hw_error = 1;
2705 rval = FAILED;
2706 goto out;
2708 else {
2709 con_log(CL_ANN, (KERN_NOTICE
2710 "megaraid mbox: reset sequence completed successfully\n"));
2714 // If the controller supports clustering, reset reservations
2715 if (!adapter->ha) {
2716 rval = SUCCESS;
2717 goto out;
2720 // clear reservations if any
2721 raw_mbox[0] = CLUSTER_CMD;
2722 raw_mbox[2] = RESET_RESERVATIONS;
2724 rval = SUCCESS;
2725 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2726 con_log(CL_ANN,
2727 (KERN_INFO "megaraid: reservation reset\n"));
2729 else {
2730 rval = FAILED;
2731 con_log(CL_ANN, (KERN_WARNING
2732 "megaraid: reservation reset failed\n"));
2735 out:
2736 spin_unlock_irq(&adapter->lock);
2737 return rval;
2741 * START: internal commands library
2743 * This section of the driver has the common routine used by the driver and
2744 * also has all the FW routines
2748 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2749 * @adapter : controller's soft state
2750 * @raw_mbox : the mailbox
2752 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2753 * controllers.
2755 static int
2756 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2758 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2759 mbox64_t *mbox64;
2760 mbox_t *mbox;
2761 uint8_t status;
2762 int i;
2765 mbox64 = raid_dev->mbox64;
2766 mbox = raid_dev->mbox;
2769 * Wait until mailbox is free
2771 if (megaraid_busywait_mbox(raid_dev) != 0)
2772 goto blocked_mailbox;
2775 * Copy mailbox data into host structure
2777 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2778 mbox->cmdid = 0xFE;
2779 mbox->busy = 1;
2780 mbox->poll = 0;
2781 mbox->ack = 0;
2782 mbox->numstatus = 0xFF;
2783 mbox->status = 0xFF;
2785 wmb();
2786 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2788 // wait for maximum 1 second for status to post. If the status is not
2789 // available within 1 second, assume FW is initializing and wait
2790 // for an extended amount of time
2791 if (mbox->numstatus == 0xFF) { // status not yet available
2792 udelay(25);
2794 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2795 rmb();
2796 msleep(1);
2800 if (i == 1000) {
2801 con_log(CL_ANN, (KERN_NOTICE
2802 "megaraid mailbox: wait for FW to boot "));
2804 for (i = 0; (mbox->numstatus == 0xFF) &&
2805 (i < MBOX_RESET_WAIT); i++) {
2806 rmb();
2807 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2808 MBOX_RESET_WAIT - i));
2809 msleep(1000);
2812 if (i == MBOX_RESET_WAIT) {
2814 con_log(CL_ANN, (
2815 "\nmegaraid mailbox: status not available\n"));
2817 return -1;
2819 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2823 // wait for maximum 1 second for poll semaphore
2824 if (mbox->poll != 0x77) {
2825 udelay(25);
2827 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2828 rmb();
2829 msleep(1);
2832 if (i == 1000) {
2833 con_log(CL_ANN, (KERN_WARNING
2834 "megaraid mailbox: could not get poll semaphore\n"));
2835 return -1;
2839 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2840 wmb();
2842 // wait for maximum 1 second for acknowledgement
2843 if (RDINDOOR(raid_dev) & 0x2) {
2844 udelay(25);
2846 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2847 rmb();
2848 msleep(1);
2851 if (i == 1000) {
2852 con_log(CL_ANN, (KERN_WARNING
2853 "megaraid mailbox: could not acknowledge\n"));
2854 return -1;
2857 mbox->poll = 0;
2858 mbox->ack = 0x77;
2860 status = mbox->status;
2862 // invalidate the completed command id array. After command
2863 // completion, firmware would write the valid id.
2864 mbox->numstatus = 0xFF;
2865 mbox->status = 0xFF;
2866 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2867 mbox->completed[i] = 0xFF;
2870 return status;
2872 blocked_mailbox:
2874 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2875 return -1;
2880 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2881 * @adapter : controller's soft state
2882 * @raw_mbox : the mailbox
2884 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2885 * controllers. This is a faster version of the synchronous command and
2886 * therefore can be called in interrupt-context as well.
2888 static int
2889 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2891 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2892 mbox_t *mbox;
2893 long i;
2896 mbox = raid_dev->mbox;
2898 // return immediately if the mailbox is busy
2899 if (mbox->busy) return -1;
2901 // Copy mailbox data into host structure
2902 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2903 mbox->cmdid = 0xFE;
2904 mbox->busy = 1;
2905 mbox->poll = 0;
2906 mbox->ack = 0;
2907 mbox->numstatus = 0xFF;
2908 mbox->status = 0xFF;
2910 wmb();
2911 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2913 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2914 if (mbox->numstatus != 0xFF) break;
2915 rmb();
2916 udelay(MBOX_SYNC_DELAY_200);
2919 if (i == MBOX_SYNC_WAIT_CNT) {
2920 // We may need to re-calibrate the counter
2921 con_log(CL_ANN, (KERN_CRIT
2922 "megaraid: fast sync command timed out\n"));
2925 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2926 wmb();
2928 return mbox->status;
2933 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2934 * @raid_dev : RAID device (HBA) soft state
2936 * Wait until the controller's mailbox is available to accept more commands.
2937 * Wait for at most 1 second.
2939 static int
2940 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2942 mbox_t *mbox = raid_dev->mbox;
2943 int i = 0;
2945 if (mbox->busy) {
2946 udelay(25);
2947 for (i = 0; mbox->busy && i < 1000; i++)
2948 msleep(1);
2951 if (i < 1000) return 0;
2952 else return -1;
2957 * megaraid_mbox_product_info - some static information about the controller
2958 * @adapter : our soft state
2960 * Issue commands to the controller to grab some parameters required by our
2961 * caller.
2963 static int
2964 megaraid_mbox_product_info(adapter_t *adapter)
2966 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2967 mbox_t *mbox;
2968 uint8_t raw_mbox[sizeof(mbox_t)];
2969 mraid_pinfo_t *pinfo;
2970 dma_addr_t pinfo_dma_h;
2971 mraid_inquiry3_t *mraid_inq3;
2972 int i;
2975 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2976 mbox = (mbox_t *)raw_mbox;
2979 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2980 * e.g., max channels, max commands etc.
2982 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
2983 &pinfo_dma_h);
2985 if (pinfo == NULL) {
2986 con_log(CL_ANN, (KERN_WARNING
2987 "megaraid: out of memory, %s %d\n", __func__,
2988 __LINE__));
2990 return -1;
2992 memset(pinfo, 0, sizeof(mraid_pinfo_t));
2994 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2995 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2997 raw_mbox[0] = FC_NEW_CONFIG;
2998 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2999 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3001 // Issue the command
3002 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3004 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3006 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3007 pinfo, pinfo_dma_h);
3009 return -1;
3013 * Collect information about state of each physical drive
3014 * attached to the controller. We will expose all the disks
3015 * which are not part of RAID
3017 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3018 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3019 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3023 * Get product info for information like number of channels,
3024 * maximum commands supported.
3026 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3027 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3029 raw_mbox[0] = FC_NEW_CONFIG;
3030 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3032 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3034 con_log(CL_ANN, (KERN_WARNING
3035 "megaraid: product info failed\n"));
3037 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3038 pinfo, pinfo_dma_h);
3040 return -1;
3044 * Setup some parameters for host, as required by our caller
3046 adapter->max_channel = pinfo->nchannels;
3049 * we will export all the logical drives on a single channel.
3050 * Add 1 since inquires do not come for inititor ID
3052 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3053 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3056 * These are the maximum outstanding commands for the scsi-layer
3058 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3060 memset(adapter->fw_version, 0, VERSION_SIZE);
3061 memset(adapter->bios_version, 0, VERSION_SIZE);
3063 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3064 adapter->fw_version[4] = 0;
3066 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3067 adapter->bios_version[4] = 0;
3069 con_log(CL_ANN, (KERN_NOTICE
3070 "megaraid: fw version:[%s] bios version:[%s]\n",
3071 adapter->fw_version, adapter->bios_version));
3073 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3074 pinfo_dma_h);
3076 return 0;
3082 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3083 * @adapter : soft state for the controller
3085 * This routine check whether the controller in question supports extended
3086 * ( > 10 bytes ) CDBs.
3088 static int
3089 megaraid_mbox_extended_cdb(adapter_t *adapter)
3091 mbox_t *mbox;
3092 uint8_t raw_mbox[sizeof(mbox_t)];
3093 int rval;
3095 mbox = (mbox_t *)raw_mbox;
3097 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3098 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3100 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3102 raw_mbox[0] = MAIN_MISC_OPCODE;
3103 raw_mbox[2] = SUPPORT_EXT_CDB;
3106 * Issue the command
3108 rval = 0;
3109 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3110 rval = -1;
3113 return rval;
3118 * megaraid_mbox_support_ha - Do we support clustering
3119 * @adapter : soft state for the controller
3120 * @init_id : ID of the initiator
3122 * Determine if the firmware supports clustering and the ID of the initiator.
3124 static int
3125 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3127 mbox_t *mbox;
3128 uint8_t raw_mbox[sizeof(mbox_t)];
3129 int rval;
3132 mbox = (mbox_t *)raw_mbox;
3134 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3136 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3138 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3140 raw_mbox[0] = GET_TARGET_ID;
3142 // Issue the command
3143 *init_id = 7;
3144 rval = -1;
3145 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3147 *init_id = *(uint8_t *)adapter->ibuf;
3149 con_log(CL_ANN, (KERN_INFO
3150 "megaraid: cluster firmware, initiator ID: %d\n",
3151 *init_id));
3153 rval = 0;
3156 return rval;
3161 * megaraid_mbox_support_random_del - Do we support random deletion
3162 * @adapter : soft state for the controller
3164 * Determine if the firmware supports random deletion.
3165 * Return: 1 is operation supported, 0 otherwise
3167 static int
3168 megaraid_mbox_support_random_del(adapter_t *adapter)
3170 mbox_t *mbox;
3171 uint8_t raw_mbox[sizeof(mbox_t)];
3172 int rval;
3175 * Newer firmware on Dell CERC expect a different
3176 * random deletion handling, so disable it.
3178 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3179 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3180 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3181 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3182 (adapter->fw_version[0] > '6' ||
3183 (adapter->fw_version[0] == '6' &&
3184 adapter->fw_version[2] > '6') ||
3185 (adapter->fw_version[0] == '6'
3186 && adapter->fw_version[2] == '6'
3187 && adapter->fw_version[3] > '1'))) {
3188 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3189 return 0;
3192 mbox = (mbox_t *)raw_mbox;
3194 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3196 raw_mbox[0] = FC_DEL_LOGDRV;
3197 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3199 // Issue the command
3200 rval = 0;
3201 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3203 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3205 rval = 1;
3208 return rval;
3213 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3214 * @adapter : soft state for the controller
3216 * Find out the maximum number of scatter-gather elements supported by the
3217 * firmware.
3219 static int
3220 megaraid_mbox_get_max_sg(adapter_t *adapter)
3222 mbox_t *mbox;
3223 uint8_t raw_mbox[sizeof(mbox_t)];
3224 int nsg;
3227 mbox = (mbox_t *)raw_mbox;
3229 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3231 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3233 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3235 raw_mbox[0] = MAIN_MISC_OPCODE;
3236 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3238 // Issue the command
3239 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3240 nsg = *(uint8_t *)adapter->ibuf;
3242 else {
3243 nsg = MBOX_DEFAULT_SG_SIZE;
3246 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3248 return nsg;
3253 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3254 * @adapter : soft state for the controller
3256 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3257 * can be exported as regular SCSI channels.
3259 static void
3260 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3262 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3263 mbox_t *mbox;
3264 uint8_t raw_mbox[sizeof(mbox_t)];
3267 mbox = (mbox_t *)raw_mbox;
3269 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3271 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3273 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3275 raw_mbox[0] = CHNL_CLASS;
3276 raw_mbox[2] = GET_CHNL_CLASS;
3278 // Issue the command. If the command fails, all channels are RAID
3279 // channels
3280 raid_dev->channel_class = 0xFF;
3281 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3282 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3285 return;
3290 * megaraid_mbox_flush_cache - flush adapter and disks cache
3291 * @adapter : soft state for the controller
3293 * Flush adapter cache followed by disks cache.
3295 static void
3296 megaraid_mbox_flush_cache(adapter_t *adapter)
3298 mbox_t *mbox;
3299 uint8_t raw_mbox[sizeof(mbox_t)];
3302 mbox = (mbox_t *)raw_mbox;
3304 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3306 raw_mbox[0] = FLUSH_ADAPTER;
3308 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3309 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3312 raw_mbox[0] = FLUSH_SYSTEM;
3314 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3315 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3318 return;
3323 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3324 * @adapter : soft state for the controller
3326 * Clears the pending cmds in FW and reinits its RAID structs.
3328 static int
3329 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3331 mbox_t *mbox;
3332 uint8_t raw_mbox[sizeof(mbox_t)];
3333 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3334 mbox64_t *mbox64;
3335 int status = 0;
3336 int i;
3337 uint32_t dword;
3339 mbox = (mbox_t *)raw_mbox;
3341 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3343 raw_mbox[0] = 0xFF;
3345 mbox64 = raid_dev->mbox64;
3346 mbox = raid_dev->mbox;
3348 /* Wait until mailbox is free */
3349 if (megaraid_busywait_mbox(raid_dev) != 0) {
3350 status = 1;
3351 goto blocked_mailbox;
3354 /* Copy mailbox data into host structure */
3355 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3356 mbox->cmdid = 0xFE;
3357 mbox->busy = 1;
3358 mbox->poll = 0;
3359 mbox->ack = 0;
3360 mbox->numstatus = 0;
3361 mbox->status = 0;
3363 wmb();
3364 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3366 /* Wait for maximum 1 min for status to post.
3367 * If the Firmware SUPPORTS the ABOVE COMMAND,
3368 * mbox->cmd will be set to 0
3369 * else
3370 * the firmware will reject the command with
3371 * mbox->numstatus set to 1
3374 i = 0;
3375 status = 0;
3376 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3377 rmb();
3378 msleep(1);
3379 i++;
3380 if (i > 1000 * 60) {
3381 status = 1;
3382 break;
3385 if (mbox->numstatus == 1)
3386 status = 1; /*cmd not supported*/
3388 /* Check for interrupt line */
3389 dword = RDOUTDOOR(raid_dev);
3390 WROUTDOOR(raid_dev, dword);
3391 WRINDOOR(raid_dev,2);
3393 return status;
3395 blocked_mailbox:
3396 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3397 return status;
3401 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3402 * @adapter : controller's soft state
3403 * @scb : SCB to be displayed
3404 * @level : debug level for console print
3406 * Diplay information about the given SCB iff the current debug level is
3407 * verbose.
3409 static void
3410 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3412 mbox_ccb_t *ccb;
3413 struct scsi_cmnd *scp;
3414 mbox_t *mbox;
3415 int level;
3416 int i;
3419 ccb = (mbox_ccb_t *)scb->ccb;
3420 scp = scb->scp;
3421 mbox = ccb->mbox;
3423 level = CL_DLEVEL3;
3425 con_log(level, (KERN_NOTICE
3426 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3427 mbox->cmd, scb->sno));
3429 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3430 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3431 mbox->numsge));
3433 if (!scp) return;
3435 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3437 for (i = 0; i < scp->cmd_len; i++) {
3438 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3441 con_log(level, ("\n"));
3443 return;
3448 * megaraid_mbox_setup_device_map - manage device ids
3449 * @adapter : Driver's soft state
3451 * Manange the device ids to have an appropraite mapping between the kernel
3452 * scsi addresses and megaraid scsi and logical drive addresses. We export
3453 * scsi devices on their actual addresses, whereas the logical drives are
3454 * exported on a virtual scsi channel.
3456 static void
3457 megaraid_mbox_setup_device_map(adapter_t *adapter)
3459 uint8_t c;
3460 uint8_t t;
3463 * First fill the values on the logical drive channel
3465 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3466 adapter->device_ids[adapter->max_channel][t] =
3467 (t < adapter->init_id) ? t : t - 1;
3469 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3472 * Fill the values on the physical devices channels
3474 for (c = 0; c < adapter->max_channel; c++)
3475 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3476 adapter->device_ids[c][t] = (c << 8) | t;
3481 * END: internal commands library
3485 * START: Interface for the common management module
3487 * This is the module, which interfaces with the common management module to
3488 * provide support for ioctl and sysfs
3492 * megaraid_cmm_register - register with the management module
3493 * @adapter : HBA soft state
3495 * Register with the management module, which allows applications to issue
3496 * ioctl calls to the drivers. This interface is used by the management module
3497 * to setup sysfs support as well.
3499 static int
3500 megaraid_cmm_register(adapter_t *adapter)
3502 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3503 mraid_mmadp_t adp;
3504 scb_t *scb;
3505 mbox_ccb_t *ccb;
3506 int rval;
3507 int i;
3509 // Allocate memory for the base list of scb for management module.
3510 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3512 if (adapter->uscb_list == NULL) {
3513 con_log(CL_ANN, (KERN_WARNING
3514 "megaraid: out of memory, %s %d\n", __func__,
3515 __LINE__));
3516 return -1;
3520 // Initialize the synchronization parameters for resources for
3521 // commands for management module
3522 INIT_LIST_HEAD(&adapter->uscb_pool);
3524 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3528 // link all the packets. Note, CCB for commands, coming from the
3529 // commom management module, mailbox physical address are already
3530 // setup by it. We just need placeholder for that in our local command
3531 // control blocks
3532 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3534 scb = adapter->uscb_list + i;
3535 ccb = raid_dev->uccb_list + i;
3537 scb->ccb = (caddr_t)ccb;
3538 ccb->mbox64 = raid_dev->umbox64 + i;
3539 ccb->mbox = &ccb->mbox64->mbox32;
3540 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3542 scb->gp = 0;
3544 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3545 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3546 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3548 scb->scp = NULL;
3549 scb->state = SCB_FREE;
3550 scb->dma_direction = PCI_DMA_NONE;
3551 scb->dma_type = MRAID_DMA_NONE;
3552 scb->dev_channel = -1;
3553 scb->dev_target = -1;
3555 // put scb in the free pool
3556 list_add_tail(&scb->list, &adapter->uscb_pool);
3559 adp.unique_id = adapter->unique_id;
3560 adp.drvr_type = DRVRTYPE_MBOX;
3561 adp.drvr_data = (unsigned long)adapter;
3562 adp.pdev = adapter->pdev;
3563 adp.issue_uioc = megaraid_mbox_mm_handler;
3564 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3565 adp.max_kioc = MBOX_MAX_USER_CMDS;
3567 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3569 con_log(CL_ANN, (KERN_WARNING
3570 "megaraid mbox: did not register with CMM\n"));
3572 kfree(adapter->uscb_list);
3575 return rval;
3579 static int
3580 megaraid_cmm_unregister(adapter_t *adapter)
3582 kfree(adapter->uscb_list);
3583 mraid_mm_unregister_adp(adapter->unique_id);
3584 return 0;
3589 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3590 * @drvr_data : LLD specific data
3591 * @kioc : CMM interface packet
3592 * @action : command action
3594 * This routine is invoked whenever the Common Management Module (CMM) has a
3595 * command for us. The 'action' parameter specifies if this is a new command
3596 * or otherwise.
3598 static int
3599 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3601 adapter_t *adapter;
3603 if (action != IOCTL_ISSUE) {
3604 con_log(CL_ANN, (KERN_WARNING
3605 "megaraid: unsupported management action:%#2x\n",
3606 action));
3607 return (-ENOTSUPP);
3610 adapter = (adapter_t *)drvr_data;
3612 // make sure this adapter is not being detached right now.
3613 if (atomic_read(&adapter->being_detached)) {
3614 con_log(CL_ANN, (KERN_WARNING
3615 "megaraid: reject management request, detaching\n"));
3616 return (-ENODEV);
3619 switch (kioc->opcode) {
3621 case GET_ADAP_INFO:
3623 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3624 (unsigned long)kioc->buf_vaddr);
3626 kioc->done(kioc);
3628 return kioc->status;
3630 case MBOX_CMD:
3632 return megaraid_mbox_mm_command(adapter, kioc);
3634 default:
3635 kioc->status = (-EINVAL);
3636 kioc->done(kioc);
3637 return (-EINVAL);
3640 return 0; // not reached
3644 * megaraid_mbox_mm_command - issues commands routed through CMM
3645 * @adapter : HBA soft state
3646 * @kioc : management command packet
3648 * Issues commands, which are routed through the management module.
3650 static int
3651 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3653 struct list_head *head = &adapter->uscb_pool;
3654 mbox64_t *mbox64;
3655 uint8_t *raw_mbox;
3656 scb_t *scb;
3657 mbox_ccb_t *ccb;
3658 unsigned long flags;
3660 // detach one scb from free pool
3661 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3663 if (list_empty(head)) { // should never happen because of CMM
3665 con_log(CL_ANN, (KERN_WARNING
3666 "megaraid mbox: bug in cmm handler, lost resources\n"));
3668 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3670 return (-EINVAL);
3673 scb = list_entry(head->next, scb_t, list);
3674 list_del_init(&scb->list);
3676 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3678 scb->state = SCB_ACTIVE;
3679 scb->dma_type = MRAID_DMA_NONE;
3680 scb->dma_direction = PCI_DMA_NONE;
3682 ccb = (mbox_ccb_t *)scb->ccb;
3683 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3684 raw_mbox = (uint8_t *)&mbox64->mbox32;
3686 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3688 scb->gp = (unsigned long)kioc;
3691 * If it is a logdrv random delete operation, we have to wait till
3692 * there are no outstanding cmds at the fw and then issue it directly
3694 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3696 if (wait_till_fw_empty(adapter)) {
3697 con_log(CL_ANN, (KERN_NOTICE
3698 "megaraid mbox: LD delete, timed out\n"));
3700 kioc->status = -ETIME;
3702 scb->status = -1;
3704 megaraid_mbox_mm_done(adapter, scb);
3706 return (-ETIME);
3709 INIT_LIST_HEAD(&scb->list);
3711 scb->state = SCB_ISSUED;
3712 if (mbox_post_cmd(adapter, scb) != 0) {
3714 con_log(CL_ANN, (KERN_NOTICE
3715 "megaraid mbox: LD delete, mailbox busy\n"));
3717 kioc->status = -EBUSY;
3719 scb->status = -1;
3721 megaraid_mbox_mm_done(adapter, scb);
3723 return (-EBUSY);
3726 return 0;
3729 // put the command on the pending list and execute
3730 megaraid_mbox_runpendq(adapter, scb);
3732 return 0;
3736 static int
3737 wait_till_fw_empty(adapter_t *adapter)
3739 unsigned long flags = 0;
3740 int i;
3744 * Set the quiescent flag to stop issuing cmds to FW.
3746 spin_lock_irqsave(&adapter->lock, flags);
3747 adapter->quiescent++;
3748 spin_unlock_irqrestore(&adapter->lock, flags);
3751 * Wait till there are no more cmds outstanding at FW. Try for at most
3752 * 60 seconds
3754 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3755 con_log(CL_DLEVEL1, (KERN_INFO
3756 "megaraid: FW has %d pending commands\n",
3757 adapter->outstanding_cmds));
3759 msleep(1000);
3762 return adapter->outstanding_cmds;
3767 * megaraid_mbox_mm_done - callback for CMM commands
3768 * @adapter : HBA soft state
3769 * @scb : completed command
3771 * Callback routine for internal commands originated from the management
3772 * module.
3774 static void
3775 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3777 uioc_t *kioc;
3778 mbox64_t *mbox64;
3779 uint8_t *raw_mbox;
3780 unsigned long flags;
3782 kioc = (uioc_t *)scb->gp;
3783 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3784 mbox64->mbox32.status = scb->status;
3785 raw_mbox = (uint8_t *)&mbox64->mbox32;
3788 // put scb in the free pool
3789 scb->state = SCB_FREE;
3790 scb->scp = NULL;
3792 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3794 list_add(&scb->list, &adapter->uscb_pool);
3796 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3798 // if a delete logical drive operation succeeded, restart the
3799 // controller
3800 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3802 adapter->quiescent--;
3804 megaraid_mbox_runpendq(adapter, NULL);
3807 kioc->done(kioc);
3809 return;
3814 * gather_hbainfo - HBA characteristics for the applications
3815 * @adapter : HBA soft state
3816 * @hinfo : pointer to the caller's host info strucuture
3818 static int
3819 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3821 uint8_t dmajor;
3823 dmajor = megaraid_mbox_version[0];
3825 hinfo->pci_vendor_id = adapter->pdev->vendor;
3826 hinfo->pci_device_id = adapter->pdev->device;
3827 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3828 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3830 hinfo->pci_bus = adapter->pdev->bus->number;
3831 hinfo->pci_dev_fn = adapter->pdev->devfn;
3832 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3833 hinfo->irq = adapter->host->irq;
3834 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3836 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3837 hinfo->host_no = adapter->host->host_no;
3839 return 0;
3843 * END: Interface for the common management module
3849 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3850 * @adapter : controller's soft state
3852 * Allocate packets required to issue FW calls whenever the sysfs attributes
3853 * are read. These attributes would require up-to-date information from the
3854 * FW. Also set up resources for mutual exclusion to share these resources and
3855 * the wait queue.
3857 * Return 0 on success.
3858 * Return -ERROR_CODE on failure.
3860 static int
3861 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3863 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3864 int rval = 0;
3866 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3868 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3870 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3871 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3873 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3874 !raid_dev->sysfs_buffer) {
3876 con_log(CL_ANN, (KERN_WARNING
3877 "megaraid: out of memory, %s %d\n", __func__,
3878 __LINE__));
3880 rval = -ENOMEM;
3882 megaraid_sysfs_free_resources(adapter);
3885 mutex_init(&raid_dev->sysfs_mtx);
3887 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3889 return rval;
3894 * megaraid_sysfs_free_resources - free sysfs related resources
3895 * @adapter : controller's soft state
3897 * Free packets allocated for sysfs FW commands
3899 static void
3900 megaraid_sysfs_free_resources(adapter_t *adapter)
3902 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3904 kfree(raid_dev->sysfs_uioc);
3905 kfree(raid_dev->sysfs_mbox64);
3907 if (raid_dev->sysfs_buffer) {
3908 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3909 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3915 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3916 * @uioc : completed packet
3918 * Callback routine called in the ISR/tasklet context for get ldmap call
3920 static void
3921 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3923 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3924 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3926 uioc->status = 0;
3928 wake_up(&raid_dev->sysfs_wait_q);
3933 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3934 * @data : timed out packet
3936 * Timeout routine to recover and return to application, in case the adapter
3937 * has stopped responding. A timeout of 60 seconds for this command seems like
3938 * a good value.
3940 static void
3941 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
3943 uioc_t *uioc = (uioc_t *)data;
3944 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3945 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3947 uioc->status = -ETIME;
3949 wake_up(&raid_dev->sysfs_wait_q);
3954 * megaraid_sysfs_get_ldmap - get update logical drive map
3955 * @adapter : controller's soft state
3957 * This routine will be called whenever user reads the logical drive
3958 * attributes, go get the current logical drive mapping table from the
3959 * firmware. We use the management API's to issue commands to the controller.
3961 * NOTE: The commands issuance functionality is not generalized and
3962 * implemented in context of "get ld map" command only. If required, the
3963 * command issuance logical can be trivially pulled out and implemented as a
3964 * standalone libary. For now, this should suffice since there is no other
3965 * user of this interface.
3967 * Return 0 on success.
3968 * Return -1 on failure.
3970 static int
3971 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3973 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3974 uioc_t *uioc;
3975 mbox64_t *mbox64;
3976 mbox_t *mbox;
3977 char *raw_mbox;
3978 struct timer_list sysfs_timer;
3979 struct timer_list *timerp;
3980 caddr_t ldmap;
3981 int rval = 0;
3984 * Allow only one read at a time to go through the sysfs attributes
3986 mutex_lock(&raid_dev->sysfs_mtx);
3988 uioc = raid_dev->sysfs_uioc;
3989 mbox64 = raid_dev->sysfs_mbox64;
3990 ldmap = raid_dev->sysfs_buffer;
3992 memset(uioc, 0, sizeof(uioc_t));
3993 memset(mbox64, 0, sizeof(mbox64_t));
3994 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3996 mbox = &mbox64->mbox32;
3997 raw_mbox = (char *)mbox;
3998 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3999 uioc->buf_vaddr = (caddr_t)adapter;
4000 uioc->status = -ENODATA;
4001 uioc->done = megaraid_sysfs_get_ldmap_done;
4004 * Prepare the mailbox packet to get the current logical drive mapping
4005 * table
4007 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
4009 raw_mbox[0] = FC_DEL_LOGDRV;
4010 raw_mbox[2] = OP_GET_LDID_MAP;
4013 * Setup a timer to recover from a non-responding controller
4015 timerp = &sysfs_timer;
4016 init_timer(timerp);
4018 timerp->function = megaraid_sysfs_get_ldmap_timeout;
4019 timerp->data = (unsigned long)uioc;
4020 timerp->expires = jiffies + 60 * HZ;
4022 add_timer(timerp);
4025 * Send the command to the firmware
4027 rval = megaraid_mbox_mm_command(adapter, uioc);
4029 if (rval == 0) { // command successfully issued
4030 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4033 * Check if the command timed out
4035 if (uioc->status == -ETIME) {
4036 con_log(CL_ANN, (KERN_NOTICE
4037 "megaraid: sysfs get ld map timed out\n"));
4039 rval = -ETIME;
4041 else {
4042 rval = mbox->status;
4045 if (rval == 0) {
4046 memcpy(raid_dev->curr_ldmap, ldmap,
4047 sizeof(raid_dev->curr_ldmap));
4049 else {
4050 con_log(CL_ANN, (KERN_NOTICE
4051 "megaraid: get ld map failed with %x\n", rval));
4054 else {
4055 con_log(CL_ANN, (KERN_NOTICE
4056 "megaraid: could not issue ldmap command:%x\n", rval));
4060 del_timer_sync(timerp);
4062 mutex_unlock(&raid_dev->sysfs_mtx);
4064 return rval;
4069 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4070 * @cdev : class device object representation for the host
4071 * @buf : buffer to send data to
4073 * Display the handle used by the applications while executing management
4074 * tasks on the adapter. We invoke a management module API to get the adapter
4075 * handle, since we do not interface with applications directly.
4077 static ssize_t
4078 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
4079 char *buf)
4081 struct Scsi_Host *shost = class_to_shost(dev);
4082 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4083 uint32_t app_hndl;
4085 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4087 return snprintf(buf, 8, "%u\n", app_hndl);
4092 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4093 * @dev : device object representation for the scsi device
4094 * @attr : device attribute to show
4095 * @buf : buffer to send data to
4097 * Display the logical drive number for the device in question, if it a valid
4098 * logical drive. For physical devices, "-1" is returned.
4100 * The logical drive number is displayed in following format:
4102 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4104 * <int> <int> <int> <int>
4106 static ssize_t
4107 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4109 struct scsi_device *sdev = to_scsi_device(dev);
4110 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4111 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4112 int scsi_id = -1;
4113 int logical_drv = -1;
4114 int ldid_map = -1;
4115 uint32_t app_hndl = 0;
4116 int mapped_sdev_id;
4117 int rval;
4118 int i;
4120 if (raid_dev->random_del_supported &&
4121 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4123 rval = megaraid_sysfs_get_ldmap(adapter);
4124 if (rval == 0) {
4126 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4128 mapped_sdev_id = sdev->id;
4130 if (sdev->id > adapter->init_id) {
4131 mapped_sdev_id -= 1;
4134 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4136 scsi_id = sdev->id;
4138 logical_drv = i;
4140 ldid_map = raid_dev->curr_ldmap[i];
4142 app_hndl = mraid_mm_adapter_app_handle(
4143 adapter->unique_id);
4145 break;
4149 else {
4150 con_log(CL_ANN, (KERN_NOTICE
4151 "megaraid: sysfs get ld map failed: %x\n",
4152 rval));
4156 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4157 ldid_map, app_hndl);
4162 * END: Mailbox Low Level Driver
4164 module_init(megaraid_init);
4165 module_exit(megaraid_exit);
4167 /* vim: set ts=8 sw=8 tw=78 ai si: */