RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / scsi / megaraid / megaraid_mbox.c
blob83b12d91657d6c2e06674de2c96d3dff60120c86
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 "megaraid_mbox.h"
75 static int megaraid_init(void);
76 static void megaraid_exit(void);
78 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
79 static void megaraid_detach_one(struct pci_dev *);
80 static void megaraid_mbox_shutdown(struct pci_dev *);
82 static int megaraid_io_attach(adapter_t *);
83 static void megaraid_io_detach(adapter_t *);
85 static int megaraid_init_mbox(adapter_t *);
86 static void megaraid_fini_mbox(adapter_t *);
88 static int megaraid_alloc_cmd_packets(adapter_t *);
89 static void megaraid_free_cmd_packets(adapter_t *);
91 static int megaraid_mbox_setup_dma_pools(adapter_t *);
92 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
94 static int megaraid_sysfs_alloc_resources(adapter_t *);
95 static void megaraid_sysfs_free_resources(adapter_t *);
97 static int megaraid_abort_handler(struct scsi_cmnd *);
98 static int megaraid_reset_handler(struct scsi_cmnd *);
100 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
101 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
102 static int megaraid_busywait_mbox(mraid_device_t *);
103 static int megaraid_mbox_product_info(adapter_t *);
104 static int megaraid_mbox_extended_cdb(adapter_t *);
105 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
106 static int megaraid_mbox_support_random_del(adapter_t *);
107 static int megaraid_mbox_get_max_sg(adapter_t *);
108 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
109 static void megaraid_mbox_flush_cache(adapter_t *);
110 static int megaraid_mbox_fire_sync_cmd(adapter_t *);
112 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
113 static void megaraid_mbox_setup_device_map(adapter_t *);
115 static int megaraid_queue_command(struct scsi_cmnd *,
116 void (*)(struct scsi_cmnd *));
117 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
118 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
119 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
120 struct scsi_cmnd *);
121 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
122 struct scsi_cmnd *);
124 static irqreturn_t megaraid_isr(int, void *);
126 static void megaraid_mbox_dpc(unsigned long);
128 static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
129 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
131 static int megaraid_cmm_register(adapter_t *);
132 static int megaraid_cmm_unregister(adapter_t *);
133 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
134 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
135 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
136 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
137 static int wait_till_fw_empty(adapter_t *);
141 MODULE_AUTHOR("megaraidlinux@lsi.com");
142 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
143 MODULE_LICENSE("GPL");
144 MODULE_VERSION(MEGARAID_VERSION);
147 * ### modules parameters for driver ###
151 * Set to enable driver to expose unconfigured disk to kernel
153 static int megaraid_expose_unconf_disks = 0;
154 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
155 MODULE_PARM_DESC(unconf_disks,
156 "Set to expose unconfigured disks to kernel (default=0)");
159 * driver wait time if the adapter's mailbox is busy
161 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
162 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
163 MODULE_PARM_DESC(busy_wait,
164 "Max wait for mailbox in microseconds if busy (default=10)");
167 * number of sectors per IO command
169 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
170 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
171 MODULE_PARM_DESC(max_sectors,
172 "Maximum number of sectors per IO command (default=128)");
175 * number of commands per logical unit
177 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
178 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
179 MODULE_PARM_DESC(cmd_per_lun,
180 "Maximum number of commands per logical unit (default=64)");
184 * Fast driver load option, skip scanning for physical devices during load.
185 * This would result in non-disk devices being skipped during driver load
186 * time. These can be later added though, using /proc/scsi/scsi
188 static unsigned int megaraid_fast_load = 0;
189 module_param_named(fast_load, megaraid_fast_load, int, 0);
190 MODULE_PARM_DESC(fast_load,
191 "Faster loading of the driver, skips physical devices! (default=0)");
195 * mraid_debug level - threshold for amount of information to be displayed by
196 * the driver. This level can be changed through modules parameters, ioctl or
197 * sysfs/proc interface. By default, print the announcement messages only.
199 int mraid_debug_level = CL_ANN;
200 module_param_named(debug_level, mraid_debug_level, int, 0);
201 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
204 * ### global data ###
206 static uint8_t megaraid_mbox_version[8] =
207 { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
211 * PCI table for all supported controllers.
213 static struct pci_device_id pci_id_table_g[] = {
215 PCI_VENDOR_ID_DELL,
216 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
217 PCI_VENDOR_ID_DELL,
218 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
221 PCI_VENDOR_ID_LSI_LOGIC,
222 PCI_DEVICE_ID_PERC4_SC,
223 PCI_VENDOR_ID_DELL,
224 PCI_SUBSYS_ID_PERC4_SC,
227 PCI_VENDOR_ID_LSI_LOGIC,
228 PCI_DEVICE_ID_PERC4_DC,
229 PCI_VENDOR_ID_DELL,
230 PCI_SUBSYS_ID_PERC4_DC,
233 PCI_VENDOR_ID_LSI_LOGIC,
234 PCI_DEVICE_ID_VERDE,
235 PCI_ANY_ID,
236 PCI_ANY_ID,
239 PCI_VENDOR_ID_DELL,
240 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
241 PCI_VENDOR_ID_DELL,
242 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
245 PCI_VENDOR_ID_DELL,
246 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
247 PCI_VENDOR_ID_DELL,
248 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
251 PCI_VENDOR_ID_DELL,
252 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
253 PCI_VENDOR_ID_DELL,
254 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
257 PCI_VENDOR_ID_DELL,
258 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
259 PCI_VENDOR_ID_DELL,
260 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
263 PCI_VENDOR_ID_DELL,
264 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
265 PCI_VENDOR_ID_DELL,
266 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
269 PCI_VENDOR_ID_DELL,
270 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
271 PCI_VENDOR_ID_DELL,
272 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
275 PCI_VENDOR_ID_LSI_LOGIC,
276 PCI_DEVICE_ID_DOBSON,
277 PCI_ANY_ID,
278 PCI_ANY_ID,
281 PCI_VENDOR_ID_AMI,
282 PCI_DEVICE_ID_AMI_MEGARAID3,
283 PCI_ANY_ID,
284 PCI_ANY_ID,
287 PCI_VENDOR_ID_LSI_LOGIC,
288 PCI_DEVICE_ID_AMI_MEGARAID3,
289 PCI_ANY_ID,
290 PCI_ANY_ID,
293 PCI_VENDOR_ID_LSI_LOGIC,
294 PCI_DEVICE_ID_LINDSAY,
295 PCI_ANY_ID,
296 PCI_ANY_ID,
298 {0} /* Terminating entry */
300 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
303 static struct pci_driver megaraid_pci_driver_g = {
304 .name = "megaraid",
305 .id_table = pci_id_table_g,
306 .probe = megaraid_probe_one,
307 .remove = __devexit_p(megaraid_detach_one),
308 .shutdown = megaraid_mbox_shutdown,
313 // definitions for the device attributes for exporting logical drive number
314 // for a scsi address (Host, Channel, Id, Lun)
316 CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
317 NULL);
319 // Host template initializer for megaraid mbox sysfs device attributes
320 static struct class_device_attribute *megaraid_shost_attrs[] = {
321 &class_device_attr_megaraid_mbox_app_hndl,
322 NULL,
326 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
328 // Host template initializer for megaraid mbox sysfs device attributes
329 static struct device_attribute *megaraid_sdev_attrs[] = {
330 &dev_attr_megaraid_mbox_ld,
331 NULL,
335 * megaraid_change_queue_depth - Change the device's queue depth
336 * @sdev: scsi device struct
337 * @qdepth: depth to set
339 * Return value:
340 * actual depth set
342 static int megaraid_change_queue_depth(struct scsi_device *sdev, int qdepth)
344 if (qdepth > MBOX_MAX_SCSI_CMDS)
345 qdepth = MBOX_MAX_SCSI_CMDS;
346 scsi_adjust_queue_depth(sdev, 0, qdepth);
347 return sdev->queue_depth;
351 * Scsi host template for megaraid unified driver
353 static struct scsi_host_template megaraid_template_g = {
354 .module = THIS_MODULE,
355 .name = "LSI Logic MegaRAID driver",
356 .proc_name = "megaraid",
357 .queuecommand = megaraid_queue_command,
358 .eh_abort_handler = megaraid_abort_handler,
359 .eh_device_reset_handler = megaraid_reset_handler,
360 .eh_bus_reset_handler = megaraid_reset_handler,
361 .eh_host_reset_handler = megaraid_reset_handler,
362 .change_queue_depth = megaraid_change_queue_depth,
363 .use_clustering = ENABLE_CLUSTERING,
364 .sdev_attrs = megaraid_sdev_attrs,
365 .shost_attrs = megaraid_shost_attrs,
370 * megaraid_init - module load hook
372 * We register ourselves as hotplug enabled module and let PCI subsystem
373 * discover our adapters.
375 static int __init
376 megaraid_init(void)
378 int rval;
380 // Announce the driver version
381 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
382 MEGARAID_EXT_VERSION));
384 // check validity of module parameters
385 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
387 con_log(CL_ANN, (KERN_WARNING
388 "megaraid mailbox: max commands per lun reset to %d\n",
389 MBOX_MAX_SCSI_CMDS));
391 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
395 // register as a PCI hot-plug driver module
396 rval = pci_register_driver(&megaraid_pci_driver_g);
397 if (rval < 0) {
398 con_log(CL_ANN, (KERN_WARNING
399 "megaraid: could not register hotplug support.\n"));
402 return rval;
407 * megaraid_exit - driver unload entry point
409 * We simply unwrap the megaraid_init routine here.
411 static void __exit
412 megaraid_exit(void)
414 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
416 // unregister as PCI hotplug driver
417 pci_unregister_driver(&megaraid_pci_driver_g);
419 return;
424 * megaraid_probe_one - PCI hotplug entry point
425 * @pdev : handle to this controller's PCI configuration space
426 * @id : pci device id of the class of controllers
428 * This routine should be called whenever a new adapter is detected by the
429 * PCI hotplug susbsytem.
431 static int __devinit
432 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
434 adapter_t *adapter;
437 // detected a new controller
438 con_log(CL_ANN, (KERN_INFO
439 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
440 pdev->vendor, pdev->device, pdev->subsystem_vendor,
441 pdev->subsystem_device));
443 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
444 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
446 if (pci_enable_device(pdev)) {
447 con_log(CL_ANN, (KERN_WARNING
448 "megaraid: pci_enable_device failed\n"));
450 return -ENODEV;
453 // Enable bus-mastering on this controller
454 pci_set_master(pdev);
456 // Allocate the per driver initialization structure
457 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
459 if (adapter == NULL) {
460 con_log(CL_ANN, (KERN_WARNING
461 "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
463 goto out_probe_one;
467 // set up PCI related soft state and other pre-known parameters
468 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
469 adapter->irq = pdev->irq;
470 adapter->pdev = pdev;
472 atomic_set(&adapter->being_detached, 0);
474 // Setup the default DMA mask. This would be changed later on
475 // depending on hardware capabilities
476 if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
478 con_log(CL_ANN, (KERN_WARNING
479 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
481 goto out_free_adapter;
485 // Initialize the synchronization lock for kernel and LLD
486 spin_lock_init(&adapter->lock);
488 // Initialize the command queues: the list of free SCBs and the list
489 // of pending SCBs.
490 INIT_LIST_HEAD(&adapter->kscb_pool);
491 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
493 INIT_LIST_HEAD(&adapter->pend_list);
494 spin_lock_init(PENDING_LIST_LOCK(adapter));
496 INIT_LIST_HEAD(&adapter->completed_list);
497 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
500 // Start the mailbox based controller
501 if (megaraid_init_mbox(adapter) != 0) {
502 con_log(CL_ANN, (KERN_WARNING
503 "megaraid: maibox adapter did not initialize\n"));
505 goto out_free_adapter;
508 // Register with LSI Common Management Module
509 if (megaraid_cmm_register(adapter) != 0) {
511 con_log(CL_ANN, (KERN_WARNING
512 "megaraid: could not register with management module\n"));
514 goto out_fini_mbox;
517 // setup adapter handle in PCI soft state
518 pci_set_drvdata(pdev, adapter);
520 // attach with scsi mid-layer
521 if (megaraid_io_attach(adapter) != 0) {
523 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
525 goto out_cmm_unreg;
528 return 0;
530 out_cmm_unreg:
531 pci_set_drvdata(pdev, NULL);
532 megaraid_cmm_unregister(adapter);
533 out_fini_mbox:
534 megaraid_fini_mbox(adapter);
535 out_free_adapter:
536 kfree(adapter);
537 out_probe_one:
538 pci_disable_device(pdev);
540 return -ENODEV;
545 * megaraid_detach_one - release framework resources and call LLD release routine
546 * @pdev : handle for our PCI cofiguration space
548 * This routine is called during driver unload. We free all the allocated
549 * resources and call the corresponding LLD so that it can also release all
550 * its resources.
552 * This routine is also called from the PCI hotplug system.
554 static void
555 megaraid_detach_one(struct pci_dev *pdev)
557 adapter_t *adapter;
558 struct Scsi_Host *host;
561 // Start a rollback on this adapter
562 adapter = pci_get_drvdata(pdev);
564 if (!adapter) {
565 con_log(CL_ANN, (KERN_CRIT
566 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
567 pdev->vendor, pdev->device, pdev->subsystem_vendor,
568 pdev->subsystem_device));
570 return;
572 else {
573 con_log(CL_ANN, (KERN_NOTICE
574 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
575 pdev->vendor, pdev->device, pdev->subsystem_vendor,
576 pdev->subsystem_device));
580 host = adapter->host;
582 // do not allow any more requests from the management module for this
583 // adapter.
584 // FIXME: How do we account for the request which might still be
585 // pending with us?
586 atomic_set(&adapter->being_detached, 1);
588 // detach from the IO sub-system
589 megaraid_io_detach(adapter);
591 // reset the device state in the PCI structure. We check this
592 // condition when we enter here. If the device state is NULL,
593 // that would mean the device has already been removed
594 pci_set_drvdata(pdev, NULL);
596 // Unregister from common management module
598 // FIXME: this must return success or failure for conditions if there
599 // is a command pending with LLD or not.
600 megaraid_cmm_unregister(adapter);
602 // finalize the mailbox based controller and release all resources
603 megaraid_fini_mbox(adapter);
605 kfree(adapter);
607 scsi_host_put(host);
609 pci_disable_device(pdev);
611 return;
616 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
617 * @pdev : generic driver model device
619 * Shutdown notification, perform flush cache.
621 static void
622 megaraid_mbox_shutdown(struct pci_dev *pdev)
624 adapter_t *adapter = pci_get_drvdata(pdev);
625 static int counter;
627 if (!adapter) {
628 con_log(CL_ANN, (KERN_WARNING
629 "megaraid: null device in shutdown\n"));
630 return;
633 // flush caches now
634 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
635 counter++));
637 megaraid_mbox_flush_cache(adapter);
639 con_log(CL_ANN, ("done\n"));
644 * megaraid_io_attach - attach a device with the IO subsystem
645 * @adapter : controller's soft state
647 * Attach this device with the IO subsystem.
649 static int
650 megaraid_io_attach(adapter_t *adapter)
652 struct Scsi_Host *host;
654 // Initialize SCSI Host structure
655 host = scsi_host_alloc(&megaraid_template_g, 8);
656 if (!host) {
657 con_log(CL_ANN, (KERN_WARNING
658 "megaraid mbox: scsi_register failed\n"));
660 return -1;
663 SCSIHOST2ADAP(host) = (caddr_t)adapter;
664 adapter->host = host;
666 host->irq = adapter->irq;
667 host->unique_id = adapter->unique_id;
668 host->can_queue = adapter->max_cmds;
669 host->this_id = adapter->init_id;
670 host->sg_tablesize = adapter->sglen;
671 host->max_sectors = adapter->max_sectors;
672 host->cmd_per_lun = adapter->cmd_per_lun;
673 host->max_channel = adapter->max_channel;
674 host->max_id = adapter->max_target;
675 host->max_lun = adapter->max_lun;
678 // notify mid-layer about the new controller
679 if (scsi_add_host(host, &adapter->pdev->dev)) {
681 con_log(CL_ANN, (KERN_WARNING
682 "megaraid mbox: scsi_add_host failed\n"));
684 scsi_host_put(host);
686 return -1;
689 scsi_scan_host(host);
691 return 0;
696 * megaraid_io_detach - detach a device from the IO subsystem
697 * @adapter : controller's soft state
699 * Detach this device from the IO subsystem.
701 static void
702 megaraid_io_detach(adapter_t *adapter)
704 struct Scsi_Host *host;
706 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
708 host = adapter->host;
710 scsi_remove_host(host);
712 return;
717 * START: Mailbox Low Level Driver
719 * This is section specific to the single mailbox based controllers
723 * megaraid_init_mbox - initialize controller
724 * @adapter : our soft state
726 * - Allocate 16-byte aligned mailbox memory for firmware handshake
727 * - Allocate controller's memory resources
728 * - Find out all initialization data
729 * - Allocate memory required for all the commands
730 * - Use internal library of FW routines, build up complete soft state
732 static int __devinit
733 megaraid_init_mbox(adapter_t *adapter)
735 struct pci_dev *pdev;
736 mraid_device_t *raid_dev;
737 int i;
738 uint32_t magic64;
741 adapter->ito = MBOX_TIMEOUT;
742 pdev = adapter->pdev;
745 * Allocate and initialize the init data structure for mailbox
746 * controllers
748 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
749 if (raid_dev == NULL) return -1;
753 * Attach the adapter soft state to raid device soft state
755 adapter->raid_device = (caddr_t)raid_dev;
756 raid_dev->fast_load = megaraid_fast_load;
759 // our baseport
760 raid_dev->baseport = pci_resource_start(pdev, 0);
762 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
764 con_log(CL_ANN, (KERN_WARNING
765 "megaraid: mem region busy\n"));
767 goto out_free_raid_dev;
770 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
772 if (!raid_dev->baseaddr) {
774 con_log(CL_ANN, (KERN_WARNING
775 "megaraid: could not map hba memory\n") );
777 goto out_release_regions;
780 /* initialize the mutual exclusion lock for the mailbox */
781 spin_lock_init(&raid_dev->mailbox_lock);
783 /* allocate memory required for commands */
784 if (megaraid_alloc_cmd_packets(adapter) != 0)
785 goto out_iounmap;
788 * Issue SYNC cmd to flush the pending cmds in the adapter
789 * and initialize its internal state
792 if (megaraid_mbox_fire_sync_cmd(adapter))
793 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
796 * Setup the rest of the soft state using the library of
797 * FW routines
800 /* request IRQ and register the interrupt service routine */
801 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
802 adapter)) {
804 con_log(CL_ANN, (KERN_WARNING
805 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
806 goto out_alloc_cmds;
810 // Product info
811 if (megaraid_mbox_product_info(adapter) != 0)
812 goto out_free_irq;
814 // Do we support extended CDBs
815 adapter->max_cdb_sz = 10;
816 if (megaraid_mbox_extended_cdb(adapter) == 0) {
817 adapter->max_cdb_sz = 16;
821 * Do we support cluster environment, if we do, what is the initiator
822 * id.
823 * NOTE: In a non-cluster aware firmware environment, the LLD should
824 * return 7 as initiator id.
826 adapter->ha = 0;
827 adapter->init_id = -1;
828 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
829 adapter->ha = 1;
833 * Prepare the device ids array to have the mapping between the kernel
834 * device address and megaraid device address.
835 * We export the physical devices on their actual addresses. The
836 * logical drives are exported on a virtual SCSI channel
838 megaraid_mbox_setup_device_map(adapter);
840 // If the firmware supports random deletion, update the device id map
841 if (megaraid_mbox_support_random_del(adapter)) {
843 // Change the logical drives numbers in device_ids array one
844 // slot in device_ids is reserved for target id, that's why
845 // "<=" below
846 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
847 adapter->device_ids[adapter->max_channel][i] += 0x80;
849 adapter->device_ids[adapter->max_channel][adapter->init_id] =
850 0xFF;
852 raid_dev->random_del_supported = 1;
856 * find out the maximum number of scatter-gather elements supported by
857 * this firmware
859 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
861 // enumerate RAID and SCSI channels so that all devices on SCSI
862 // channels can later be exported, including disk devices
863 megaraid_mbox_enum_raid_scsi(adapter);
866 * Other parameters required by upper layer
868 * maximum number of sectors per IO command
870 adapter->max_sectors = megaraid_max_sectors;
873 * number of queued commands per LUN.
875 adapter->cmd_per_lun = megaraid_cmd_per_lun;
878 * Allocate resources required to issue FW calls, when sysfs is
879 * accessed
881 if (megaraid_sysfs_alloc_resources(adapter) != 0)
882 goto out_free_irq;
884 // Set the DMA mask to 64-bit. All supported controllers as capable of
885 // DMA in this range
886 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
888 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
889 ((adapter->pdev->subsystem_device !=
890 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
891 (adapter->pdev->subsystem_device !=
892 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
893 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
894 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
895 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
896 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
897 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
898 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
899 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
900 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
901 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
902 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
903 if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK)) {
904 con_log(CL_ANN, (KERN_WARNING
905 "megaraid: DMA mask for 64-bit failed\n"));
907 if (pci_set_dma_mask (adapter->pdev, DMA_32BIT_MASK)) {
908 con_log(CL_ANN, (KERN_WARNING
909 "megaraid: 32-bit DMA mask failed\n"));
910 goto out_free_sysfs_res;
915 // setup tasklet for DPC
916 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
917 (unsigned long)adapter);
919 con_log(CL_DLEVEL1, (KERN_INFO
920 "megaraid mbox hba successfully initialized\n"));
922 return 0;
924 out_free_sysfs_res:
925 megaraid_sysfs_free_resources(adapter);
926 out_free_irq:
927 free_irq(adapter->irq, adapter);
928 out_alloc_cmds:
929 megaraid_free_cmd_packets(adapter);
930 out_iounmap:
931 iounmap(raid_dev->baseaddr);
932 out_release_regions:
933 pci_release_regions(pdev);
934 out_free_raid_dev:
935 kfree(raid_dev);
937 return -1;
942 * megaraid_fini_mbox - undo controller initialization
943 * @adapter : our soft state
945 static void
946 megaraid_fini_mbox(adapter_t *adapter)
948 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
950 // flush all caches
951 megaraid_mbox_flush_cache(adapter);
953 tasklet_kill(&adapter->dpc_h);
955 megaraid_sysfs_free_resources(adapter);
957 megaraid_free_cmd_packets(adapter);
959 free_irq(adapter->irq, adapter);
961 iounmap(raid_dev->baseaddr);
963 pci_release_regions(adapter->pdev);
965 kfree(raid_dev);
967 return;
972 * megaraid_alloc_cmd_packets - allocate shared mailbox
973 * @adapter : soft state of the raid controller
975 * Allocate and align the shared mailbox. This maibox is used to issue
976 * all the commands. For IO based controllers, the mailbox is also regsitered
977 * with the FW. Allocate memory for all commands as well.
978 * This is our big allocator.
980 static int
981 megaraid_alloc_cmd_packets(adapter_t *adapter)
983 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
984 struct pci_dev *pdev;
985 unsigned long align;
986 scb_t *scb;
987 mbox_ccb_t *ccb;
988 struct mraid_pci_blk *epthru_pci_blk;
989 struct mraid_pci_blk *sg_pci_blk;
990 struct mraid_pci_blk *mbox_pci_blk;
991 int i;
993 pdev = adapter->pdev;
996 * Setup the mailbox
997 * Allocate the common 16-byte aligned memory for the handshake
998 * mailbox.
1000 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1001 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1003 if (!raid_dev->una_mbox64) {
1004 con_log(CL_ANN, (KERN_WARNING
1005 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1006 __LINE__));
1007 return -1;
1009 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1012 * Align the mailbox at 16-byte boundary
1014 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1016 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1017 (~0UL ^ 0xFUL));
1019 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1021 align = ((void *)raid_dev->mbox -
1022 ((void *)&raid_dev->una_mbox64->mbox32));
1024 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1025 align;
1027 // Allocate memory for commands issued internally
1028 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1029 &adapter->ibuf_dma_h);
1030 if (!adapter->ibuf) {
1032 con_log(CL_ANN, (KERN_WARNING
1033 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1034 __LINE__));
1036 goto out_free_common_mbox;
1038 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1040 // Allocate memory for our SCSI Command Blocks and their associated
1041 // memory
1044 * Allocate memory for the base list of scb. Later allocate memory for
1045 * CCBs and embedded components of each CCB and point the pointers in
1046 * scb to the allocated components
1047 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1048 * since the calling routine does not yet know the number of available
1049 * commands.
1051 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1053 if (adapter->kscb_list == NULL) {
1054 con_log(CL_ANN, (KERN_WARNING
1055 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1056 __LINE__));
1057 goto out_free_ibuf;
1060 // memory allocation for our command packets
1061 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1062 con_log(CL_ANN, (KERN_WARNING
1063 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1064 __LINE__));
1065 goto out_free_scb_list;
1068 // Adjust the scb pointers and link in the free pool
1069 epthru_pci_blk = raid_dev->epthru_pool;
1070 sg_pci_blk = raid_dev->sg_pool;
1071 mbox_pci_blk = raid_dev->mbox_pool;
1073 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1074 scb = adapter->kscb_list + i;
1075 ccb = raid_dev->ccb_list + i;
1077 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1078 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1079 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1080 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1082 // make sure the mailbox is aligned properly
1083 if (ccb->mbox_dma_h & 0x0F) {
1084 con_log(CL_ANN, (KERN_CRIT
1085 "megaraid mbox: not aligned on 16-bytes\n"));
1087 goto out_teardown_dma_pools;
1090 ccb->epthru = (mraid_epassthru_t *)
1091 epthru_pci_blk[i].vaddr;
1092 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1093 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1094 ccb->pthru_dma_h = ccb->epthru_dma_h;
1097 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1098 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1099 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1101 scb->ccb = (caddr_t)ccb;
1102 scb->gp = 0;
1104 scb->sno = i; // command index
1106 scb->scp = NULL;
1107 scb->state = SCB_FREE;
1108 scb->dma_direction = PCI_DMA_NONE;
1109 scb->dma_type = MRAID_DMA_NONE;
1110 scb->dev_channel = -1;
1111 scb->dev_target = -1;
1113 // put scb in the free pool
1114 list_add_tail(&scb->list, &adapter->kscb_pool);
1117 return 0;
1119 out_teardown_dma_pools:
1120 megaraid_mbox_teardown_dma_pools(adapter);
1121 out_free_scb_list:
1122 kfree(adapter->kscb_list);
1123 out_free_ibuf:
1124 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1125 adapter->ibuf_dma_h);
1126 out_free_common_mbox:
1127 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1128 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1130 return -1;
1135 * megaraid_free_cmd_packets - free memory
1136 * @adapter : soft state of the raid controller
1138 * Release memory resources allocated for commands.
1140 static void
1141 megaraid_free_cmd_packets(adapter_t *adapter)
1143 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1145 megaraid_mbox_teardown_dma_pools(adapter);
1147 kfree(adapter->kscb_list);
1149 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1150 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1152 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1153 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1154 return;
1159 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1160 * @adapter : HBA soft state
1162 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1163 * and scatter-gather lists.
1165 static int
1166 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1168 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1169 struct mraid_pci_blk *epthru_pci_blk;
1170 struct mraid_pci_blk *sg_pci_blk;
1171 struct mraid_pci_blk *mbox_pci_blk;
1172 int i;
1176 // Allocate memory for 16-bytes aligned mailboxes
1177 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1178 adapter->pdev,
1179 sizeof(mbox64_t) + 16,
1180 16, 0);
1182 if (raid_dev->mbox_pool_handle == NULL) {
1183 goto fail_setup_dma_pool;
1186 mbox_pci_blk = raid_dev->mbox_pool;
1187 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1188 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1189 raid_dev->mbox_pool_handle,
1190 GFP_KERNEL,
1191 &mbox_pci_blk[i].dma_addr);
1192 if (!mbox_pci_blk[i].vaddr) {
1193 goto fail_setup_dma_pool;
1198 * Allocate memory for each embedded passthru strucuture pointer
1199 * Request for a 128 bytes aligned structure for each passthru command
1200 * structure
1201 * Since passthru and extended passthru commands are exclusive, they
1202 * share common memory pool. Passthru structures piggyback on memory
1203 * allocted to extended passthru since passthru is smaller of the two
1205 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1206 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1208 if (raid_dev->epthru_pool_handle == NULL) {
1209 goto fail_setup_dma_pool;
1212 epthru_pci_blk = raid_dev->epthru_pool;
1213 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1214 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1215 raid_dev->epthru_pool_handle,
1216 GFP_KERNEL,
1217 &epthru_pci_blk[i].dma_addr);
1218 if (!epthru_pci_blk[i].vaddr) {
1219 goto fail_setup_dma_pool;
1224 // Allocate memory for each scatter-gather list. Request for 512 bytes
1225 // alignment for each sg list
1226 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1227 adapter->pdev,
1228 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1229 512, 0);
1231 if (raid_dev->sg_pool_handle == NULL) {
1232 goto fail_setup_dma_pool;
1235 sg_pci_blk = raid_dev->sg_pool;
1236 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1237 sg_pci_blk[i].vaddr = pci_pool_alloc(
1238 raid_dev->sg_pool_handle,
1239 GFP_KERNEL,
1240 &sg_pci_blk[i].dma_addr);
1241 if (!sg_pci_blk[i].vaddr) {
1242 goto fail_setup_dma_pool;
1246 return 0;
1248 fail_setup_dma_pool:
1249 megaraid_mbox_teardown_dma_pools(adapter);
1250 return -1;
1255 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1256 * @adapter : HBA soft state
1258 * Teardown the dma pool for mailbox, passthru and extended passthru
1259 * structures, and scatter-gather lists.
1261 static void
1262 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1264 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1265 struct mraid_pci_blk *epthru_pci_blk;
1266 struct mraid_pci_blk *sg_pci_blk;
1267 struct mraid_pci_blk *mbox_pci_blk;
1268 int i;
1271 sg_pci_blk = raid_dev->sg_pool;
1272 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1273 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1274 sg_pci_blk[i].dma_addr);
1276 if (raid_dev->sg_pool_handle)
1277 pci_pool_destroy(raid_dev->sg_pool_handle);
1280 epthru_pci_blk = raid_dev->epthru_pool;
1281 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1282 pci_pool_free(raid_dev->epthru_pool_handle,
1283 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1285 if (raid_dev->epthru_pool_handle)
1286 pci_pool_destroy(raid_dev->epthru_pool_handle);
1289 mbox_pci_blk = raid_dev->mbox_pool;
1290 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1291 pci_pool_free(raid_dev->mbox_pool_handle,
1292 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1294 if (raid_dev->mbox_pool_handle)
1295 pci_pool_destroy(raid_dev->mbox_pool_handle);
1297 return;
1302 * megaraid_alloc_scb - detach and return a scb from the free list
1303 * @adapter : controller's soft state
1304 * @scp : pointer to the scsi command to be executed
1306 * Return the scb from the head of the free list. %NULL if there are none
1307 * available.
1309 static scb_t *
1310 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1312 struct list_head *head = &adapter->kscb_pool;
1313 scb_t *scb = NULL;
1314 unsigned long flags;
1316 // detach scb from free pool
1317 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1319 if (list_empty(head)) {
1320 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1321 return NULL;
1324 scb = list_entry(head->next, scb_t, list);
1325 list_del_init(&scb->list);
1327 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1329 scb->state = SCB_ACTIVE;
1330 scb->scp = scp;
1331 scb->dma_type = MRAID_DMA_NONE;
1333 return scb;
1338 * megaraid_dealloc_scb - return the scb to the free pool
1339 * @adapter : controller's soft state
1340 * @scb : scb to be freed
1342 * Return the scb back to the free list of scbs. The caller must 'flush' the
1343 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1344 * NOTE NOTE: Make sure the scb is not on any list before calling this
1345 * routine.
1347 static inline void
1348 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1350 unsigned long flags;
1352 // put scb in the free pool
1353 scb->state = SCB_FREE;
1354 scb->scp = NULL;
1355 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1357 list_add(&scb->list, &adapter->kscb_pool);
1359 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1361 return;
1366 * megaraid_mbox_mksgl - make the scatter-gather list
1367 * @adapter : controller's soft state
1368 * @scb : scsi control block
1370 * Prepare the scatter-gather list.
1372 static int
1373 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1375 struct scatterlist *sgl;
1376 mbox_ccb_t *ccb;
1377 struct page *page;
1378 unsigned long offset;
1379 struct scsi_cmnd *scp;
1380 int sgcnt;
1381 int i;
1384 scp = scb->scp;
1385 ccb = (mbox_ccb_t *)scb->ccb;
1387 // no mapping required if no data to be transferred
1388 if (!scp->request_buffer || !scp->request_bufflen)
1389 return 0;
1391 if (!scp->use_sg) { /* scatter-gather list not used */
1393 page = virt_to_page(scp->request_buffer);
1395 offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
1397 ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
1398 scp->request_bufflen,
1399 scb->dma_direction);
1400 scb->dma_type = MRAID_DMA_WBUF;
1403 * We need to handle special 64-bit commands that need a
1404 * minimum of 1 SG
1406 sgcnt = 1;
1407 ccb->sgl64[0].address = ccb->buf_dma_h;
1408 ccb->sgl64[0].length = scp->request_bufflen;
1410 return sgcnt;
1413 sgl = (struct scatterlist *)scp->request_buffer;
1415 // The number of sg elements returned must not exceed our limit
1416 sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
1417 scb->dma_direction);
1419 if (sgcnt > adapter->sglen) {
1420 con_log(CL_ANN, (KERN_CRIT
1421 "megaraid critical: too many sg elements:%d\n",
1422 sgcnt));
1423 BUG();
1426 scb->dma_type = MRAID_DMA_WSG;
1428 for (i = 0; i < sgcnt; i++, sgl++) {
1429 ccb->sgl64[i].address = sg_dma_address(sgl);
1430 ccb->sgl64[i].length = sg_dma_len(sgl);
1433 // Return count of SG nodes
1434 return sgcnt;
1439 * mbox_post_cmd - issue a mailbox command
1440 * @adapter : controller's soft state
1441 * @scb : command to be issued
1443 * Post the command to the controller if mailbox is available.
1445 static int
1446 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1448 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1449 mbox64_t *mbox64;
1450 mbox_t *mbox;
1451 mbox_ccb_t *ccb;
1452 unsigned long flags;
1453 unsigned int i = 0;
1456 ccb = (mbox_ccb_t *)scb->ccb;
1457 mbox = raid_dev->mbox;
1458 mbox64 = raid_dev->mbox64;
1461 * Check for busy mailbox. If it is, return failure - the caller
1462 * should retry later.
1464 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1466 if (unlikely(mbox->busy)) {
1467 do {
1468 udelay(1);
1469 i++;
1470 rmb();
1471 } while(mbox->busy && (i < max_mbox_busy_wait));
1473 if (mbox->busy) {
1475 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1477 return -1;
1482 // Copy this command's mailbox data into "adapter's" mailbox
1483 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1484 mbox->cmdid = scb->sno;
1486 adapter->outstanding_cmds++;
1488 if (scb->dma_direction == PCI_DMA_TODEVICE) {
1489 if (!scb->scp->use_sg) { // sg list not used
1490 pci_dma_sync_single_for_device(adapter->pdev,
1491 ccb->buf_dma_h,
1492 scb->scp->request_bufflen,
1493 PCI_DMA_TODEVICE);
1495 else {
1496 pci_dma_sync_sg_for_device(adapter->pdev,
1497 scb->scp->request_buffer,
1498 scb->scp->use_sg, PCI_DMA_TODEVICE);
1502 mbox->busy = 1; // Set busy
1503 mbox->poll = 0;
1504 mbox->ack = 0;
1505 wmb();
1507 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1509 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1511 return 0;
1516 * megaraid_queue_command - generic queue entry point for all LLDs
1517 * @scp : pointer to the scsi command to be executed
1518 * @done : callback routine to be called after the cmd has be completed
1520 * Queue entry point for mailbox based controllers.
1522 static int
1523 megaraid_queue_command(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1525 adapter_t *adapter;
1526 scb_t *scb;
1527 int if_busy;
1529 adapter = SCP2ADAPTER(scp);
1530 scp->scsi_done = done;
1531 scp->result = 0;
1534 * Allocate and build a SCB request
1535 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1536 * not allocate scb. We will return non-zero status in that case.
1537 * NOTE: scb can be null even though certain commands completed
1538 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1539 * return 0 in that case, and we would do the callback right away.
1541 if_busy = 0;
1542 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1543 if (!scb) { // command already completed
1544 done(scp);
1545 return 0;
1548 megaraid_mbox_runpendq(adapter, scb);
1549 return if_busy;
1553 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1554 * @adapter : controller's soft state
1555 * @scp : mid-layer scsi command pointer
1556 * @busy : set if request could not be completed because of lack of
1557 * resources
1559 * Transform the mid-layer scsi command to megaraid firmware lingua.
1560 * Convert the command issued by mid-layer to format understood by megaraid
1561 * firmware. We also complete certain commands without sending them to firmware.
1563 static scb_t *
1564 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1566 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1567 int channel;
1568 int target;
1569 int islogical;
1570 mbox_ccb_t *ccb;
1571 mraid_passthru_t *pthru;
1572 mbox64_t *mbox64;
1573 mbox_t *mbox;
1574 scb_t *scb;
1575 char skip[] = "skipping";
1576 char scan[] = "scanning";
1577 char *ss;
1581 * Get the appropriate device map for the device this command is
1582 * intended for
1584 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1587 * Logical drive commands
1589 if (islogical) {
1590 switch (scp->cmnd[0]) {
1591 case TEST_UNIT_READY:
1593 * Do we support clustering and is the support enabled
1594 * If no, return success always
1596 if (!adapter->ha) {
1597 scp->result = (DID_OK << 16);
1598 return NULL;
1601 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1602 scp->result = (DID_ERROR << 16);
1603 *busy = 1;
1604 return NULL;
1607 scb->dma_direction = scp->sc_data_direction;
1608 scb->dev_channel = 0xFF;
1609 scb->dev_target = target;
1610 ccb = (mbox_ccb_t *)scb->ccb;
1613 * The command id will be provided by the command
1614 * issuance routine
1616 ccb->raw_mbox[0] = CLUSTER_CMD;
1617 ccb->raw_mbox[2] = RESERVATION_STATUS;
1618 ccb->raw_mbox[3] = target;
1620 return scb;
1622 case MODE_SENSE:
1623 if (scp->use_sg) {
1624 struct scatterlist *sgl;
1625 caddr_t vaddr;
1627 sgl = (struct scatterlist *)scp->request_buffer;
1628 if (sgl->page) {
1629 vaddr = (caddr_t)
1630 (page_address((&sgl[0])->page)
1631 + (&sgl[0])->offset);
1633 memset(vaddr, 0, scp->cmnd[4]);
1635 else {
1636 con_log(CL_ANN, (KERN_WARNING
1637 "megaraid mailbox: invalid sg:%d\n",
1638 __LINE__));
1641 else {
1642 memset(scp->request_buffer, 0, scp->cmnd[4]);
1644 scp->result = (DID_OK << 16);
1645 return NULL;
1647 case INQUIRY:
1649 * Display the channel scan for logical drives
1650 * Do not display scan for a channel if already done.
1652 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1654 con_log(CL_ANN, (KERN_INFO
1655 "scsi[%d]: scanning scsi channel %d",
1656 adapter->host->host_no,
1657 SCP2CHANNEL(scp)));
1659 con_log(CL_ANN, (
1660 " [virtual] for logical drives\n"));
1662 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1665 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1666 scp->sense_buffer[0] = 0x70;
1667 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1668 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1669 scp->result = CHECK_CONDITION << 1;
1670 return NULL;
1673 /* Fall through */
1675 case READ_CAPACITY:
1677 * Do not allow LUN > 0 for logical drives and
1678 * requests for more than 40 logical drives
1680 if (SCP2LUN(scp)) {
1681 scp->result = (DID_BAD_TARGET << 16);
1682 return NULL;
1684 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1685 scp->result = (DID_BAD_TARGET << 16);
1686 return NULL;
1690 /* Allocate a SCB and initialize passthru */
1691 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1692 scp->result = (DID_ERROR << 16);
1693 *busy = 1;
1694 return NULL;
1697 ccb = (mbox_ccb_t *)scb->ccb;
1698 scb->dev_channel = 0xFF;
1699 scb->dev_target = target;
1700 pthru = ccb->pthru;
1701 mbox = ccb->mbox;
1702 mbox64 = ccb->mbox64;
1704 pthru->timeout = 0;
1705 pthru->ars = 1;
1706 pthru->reqsenselen = 14;
1707 pthru->islogical = 1;
1708 pthru->logdrv = target;
1709 pthru->cdblen = scp->cmd_len;
1710 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1712 mbox->cmd = MBOXCMD_PASSTHRU64;
1713 scb->dma_direction = scp->sc_data_direction;
1715 pthru->dataxferlen = scp->request_bufflen;
1716 pthru->dataxferaddr = ccb->sgl_dma_h;
1717 pthru->numsge = megaraid_mbox_mksgl(adapter,
1718 scb);
1720 mbox->xferaddr = 0xFFFFFFFF;
1721 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1722 mbox64->xferaddr_hi = 0;
1724 return scb;
1726 case READ_6:
1727 case WRITE_6:
1728 case READ_10:
1729 case WRITE_10:
1730 case READ_12:
1731 case WRITE_12:
1734 * Allocate a SCB and initialize mailbox
1736 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1737 scp->result = (DID_ERROR << 16);
1738 *busy = 1;
1739 return NULL;
1741 ccb = (mbox_ccb_t *)scb->ccb;
1742 scb->dev_channel = 0xFF;
1743 scb->dev_target = target;
1744 mbox = ccb->mbox;
1745 mbox64 = ccb->mbox64;
1746 mbox->logdrv = target;
1749 * A little HACK: 2nd bit is zero for all scsi read
1750 * commands and is set for all scsi write commands
1752 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1753 MBOXCMD_LREAD64 ;
1756 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1758 if (scp->cmd_len == 6) {
1759 mbox->numsectors = (uint32_t)scp->cmnd[4];
1760 mbox->lba =
1761 ((uint32_t)scp->cmnd[1] << 16) |
1762 ((uint32_t)scp->cmnd[2] << 8) |
1763 (uint32_t)scp->cmnd[3];
1765 mbox->lba &= 0x1FFFFF;
1769 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1771 else if (scp->cmd_len == 10) {
1772 mbox->numsectors =
1773 (uint32_t)scp->cmnd[8] |
1774 ((uint32_t)scp->cmnd[7] << 8);
1775 mbox->lba =
1776 ((uint32_t)scp->cmnd[2] << 24) |
1777 ((uint32_t)scp->cmnd[3] << 16) |
1778 ((uint32_t)scp->cmnd[4] << 8) |
1779 (uint32_t)scp->cmnd[5];
1783 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1785 else if (scp->cmd_len == 12) {
1786 mbox->lba =
1787 ((uint32_t)scp->cmnd[2] << 24) |
1788 ((uint32_t)scp->cmnd[3] << 16) |
1789 ((uint32_t)scp->cmnd[4] << 8) |
1790 (uint32_t)scp->cmnd[5];
1792 mbox->numsectors =
1793 ((uint32_t)scp->cmnd[6] << 24) |
1794 ((uint32_t)scp->cmnd[7] << 16) |
1795 ((uint32_t)scp->cmnd[8] << 8) |
1796 (uint32_t)scp->cmnd[9];
1798 else {
1799 con_log(CL_ANN, (KERN_WARNING
1800 "megaraid: unsupported CDB length\n"));
1802 megaraid_dealloc_scb(adapter, scb);
1804 scp->result = (DID_ERROR << 16);
1805 return NULL;
1808 scb->dma_direction = scp->sc_data_direction;
1810 // Calculate Scatter-Gather info
1811 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1812 mbox->numsge = megaraid_mbox_mksgl(adapter,
1813 scb);
1814 mbox->xferaddr = 0xFFFFFFFF;
1815 mbox64->xferaddr_hi = 0;
1817 return scb;
1819 case RESERVE:
1820 case RELEASE:
1822 * Do we support clustering and is the support enabled
1824 if (!adapter->ha) {
1825 scp->result = (DID_BAD_TARGET << 16);
1826 return NULL;
1830 * Allocate a SCB and initialize mailbox
1832 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1833 scp->result = (DID_ERROR << 16);
1834 *busy = 1;
1835 return NULL;
1838 ccb = (mbox_ccb_t *)scb->ccb;
1839 scb->dev_channel = 0xFF;
1840 scb->dev_target = target;
1841 ccb->raw_mbox[0] = CLUSTER_CMD;
1842 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1843 RESERVE_LD : RELEASE_LD;
1845 ccb->raw_mbox[3] = target;
1846 scb->dma_direction = scp->sc_data_direction;
1848 return scb;
1850 default:
1851 scp->result = (DID_BAD_TARGET << 16);
1852 return NULL;
1855 else { // Passthru device commands
1857 // Do not allow access to target id > 15 or LUN > 7
1858 if (target > 15 || SCP2LUN(scp) > 7) {
1859 scp->result = (DID_BAD_TARGET << 16);
1860 return NULL;
1863 // if fast load option was set and scan for last device is
1864 // over, reset the fast_load flag so that during a possible
1865 // next scan, devices can be made available
1866 if (rdev->fast_load && (target == 15) &&
1867 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1869 con_log(CL_ANN, (KERN_INFO
1870 "megaraid[%d]: physical device scan re-enabled\n",
1871 adapter->host->host_no));
1872 rdev->fast_load = 0;
1876 * Display the channel scan for physical devices
1878 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1880 ss = rdev->fast_load ? skip : scan;
1882 con_log(CL_ANN, (KERN_INFO
1883 "scsi[%d]: %s scsi channel %d [Phy %d]",
1884 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1885 channel));
1887 con_log(CL_ANN, (
1888 " for non-raid devices\n"));
1890 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1893 // disable channel sweep if fast load option given
1894 if (rdev->fast_load) {
1895 scp->result = (DID_BAD_TARGET << 16);
1896 return NULL;
1899 // Allocate a SCB and initialize passthru
1900 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1901 scp->result = (DID_ERROR << 16);
1902 *busy = 1;
1903 return NULL;
1906 ccb = (mbox_ccb_t *)scb->ccb;
1907 scb->dev_channel = channel;
1908 scb->dev_target = target;
1909 scb->dma_direction = scp->sc_data_direction;
1910 mbox = ccb->mbox;
1911 mbox64 = ccb->mbox64;
1913 // Does this firmware support extended CDBs
1914 if (adapter->max_cdb_sz == 16) {
1915 mbox->cmd = MBOXCMD_EXTPTHRU;
1917 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1919 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1920 mbox64->xferaddr_hi = 0;
1921 mbox->xferaddr = 0xFFFFFFFF;
1923 else {
1924 mbox->cmd = MBOXCMD_PASSTHRU64;
1926 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1928 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1929 mbox64->xferaddr_hi = 0;
1930 mbox->xferaddr = 0xFFFFFFFF;
1932 return scb;
1935 // NOT REACHED
1940 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1941 * @adapter : controller's soft state
1942 * @scb_q : SCB to be queued in the pending list
1944 * Scan the pending list for commands which are not yet issued and try to
1945 * post to the controller. The SCB can be a null pointer, which would indicate
1946 * no SCB to be queue, just try to execute the ones in the pending list.
1948 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1949 * out from the head of the pending list. If it is successfully issued, the
1950 * next SCB is at the head now.
1952 static void
1953 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1955 scb_t *scb;
1956 unsigned long flags;
1958 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1960 if (scb_q) {
1961 scb_q->state = SCB_PENDQ;
1962 list_add_tail(&scb_q->list, &adapter->pend_list);
1965 // if the adapter in not in quiescent mode, post the commands to FW
1966 if (adapter->quiescent) {
1967 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1968 return;
1971 while (!list_empty(&adapter->pend_list)) {
1973 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1975 scb = list_entry(adapter->pend_list.next, scb_t, list);
1977 // remove the scb from the pending list and try to
1978 // issue. If we are unable to issue it, put back in
1979 // the pending list and return
1981 list_del_init(&scb->list);
1983 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1985 // if mailbox was busy, return SCB back to pending
1986 // list. Make sure to add at the head, since that's
1987 // where it would have been removed from
1989 scb->state = SCB_ISSUED;
1991 if (mbox_post_cmd(adapter, scb) != 0) {
1993 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1995 scb->state = SCB_PENDQ;
1997 list_add(&scb->list, &adapter->pend_list);
1999 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2000 flags);
2002 return;
2005 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2008 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2011 return;
2016 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
2017 * @adapter : pointer to controller's soft state
2018 * @scb : scsi control block
2019 * @scp : scsi command from the mid-layer
2021 * Prepare a command for the scsi physical devices.
2023 static void
2024 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
2025 struct scsi_cmnd *scp)
2027 mbox_ccb_t *ccb;
2028 mraid_passthru_t *pthru;
2029 uint8_t channel;
2030 uint8_t target;
2032 ccb = (mbox_ccb_t *)scb->ccb;
2033 pthru = ccb->pthru;
2034 channel = scb->dev_channel;
2035 target = scb->dev_target;
2037 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2038 pthru->timeout = 4;
2039 pthru->ars = 1;
2040 pthru->islogical = 0;
2041 pthru->channel = 0;
2042 pthru->target = (channel << 4) | target;
2043 pthru->logdrv = SCP2LUN(scp);
2044 pthru->reqsenselen = 14;
2045 pthru->cdblen = scp->cmd_len;
2047 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2049 if (scp->request_bufflen) {
2050 pthru->dataxferlen = scp->request_bufflen;
2051 pthru->dataxferaddr = ccb->sgl_dma_h;
2052 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2054 else {
2055 pthru->dataxferaddr = 0;
2056 pthru->dataxferlen = 0;
2057 pthru->numsge = 0;
2059 return;
2064 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2065 * @adapter : pointer to controller's soft state
2066 * @scb : scsi control block
2067 * @scp : scsi command from the mid-layer
2069 * Prepare a command for the scsi physical devices. This rountine prepares
2070 * commands for devices which can take extended CDBs (>10 bytes).
2072 static void
2073 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2074 struct scsi_cmnd *scp)
2076 mbox_ccb_t *ccb;
2077 mraid_epassthru_t *epthru;
2078 uint8_t channel;
2079 uint8_t target;
2081 ccb = (mbox_ccb_t *)scb->ccb;
2082 epthru = ccb->epthru;
2083 channel = scb->dev_channel;
2084 target = scb->dev_target;
2086 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2087 epthru->timeout = 4;
2088 epthru->ars = 1;
2089 epthru->islogical = 0;
2090 epthru->channel = 0;
2091 epthru->target = (channel << 4) | target;
2092 epthru->logdrv = SCP2LUN(scp);
2093 epthru->reqsenselen = 14;
2094 epthru->cdblen = scp->cmd_len;
2096 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2098 if (scp->request_bufflen) {
2099 epthru->dataxferlen = scp->request_bufflen;
2100 epthru->dataxferaddr = ccb->sgl_dma_h;
2101 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2103 else {
2104 epthru->dataxferaddr = 0;
2105 epthru->dataxferlen = 0;
2106 epthru->numsge = 0;
2108 return;
2113 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2114 * @adapter : controller's soft state
2116 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2117 * completed command and put them on the completed list for later processing.
2119 * Returns: 1 if the interrupt is valid, 0 otherwise
2121 static int
2122 megaraid_ack_sequence(adapter_t *adapter)
2124 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2125 mbox_t *mbox;
2126 scb_t *scb;
2127 uint8_t nstatus;
2128 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2129 struct list_head clist;
2130 int handled;
2131 uint32_t dword;
2132 unsigned long flags;
2133 int i, j;
2136 mbox = raid_dev->mbox;
2138 // move the SCBs from the firmware completed array to our local list
2139 INIT_LIST_HEAD(&clist);
2141 // loop till F/W has more commands for us to complete
2142 handled = 0;
2143 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2144 do {
2146 * Check if a valid interrupt is pending. If found, force the
2147 * interrupt line low.
2149 dword = RDOUTDOOR(raid_dev);
2150 if (dword != 0x10001234) break;
2152 handled = 1;
2154 WROUTDOOR(raid_dev, 0x10001234);
2156 nstatus = 0;
2157 // wait for valid numstatus to post
2158 for (i = 0; i < 0xFFFFF; i++) {
2159 if (mbox->numstatus != 0xFF) {
2160 nstatus = mbox->numstatus;
2161 break;
2163 rmb();
2165 mbox->numstatus = 0xFF;
2167 adapter->outstanding_cmds -= nstatus;
2169 for (i = 0; i < nstatus; i++) {
2171 // wait for valid command index to post
2172 for (j = 0; j < 0xFFFFF; j++) {
2173 if (mbox->completed[i] != 0xFF) break;
2174 rmb();
2176 completed[i] = mbox->completed[i];
2177 mbox->completed[i] = 0xFF;
2179 if (completed[i] == 0xFF) {
2180 con_log(CL_ANN, (KERN_CRIT
2181 "megaraid: command posting timed out\n"));
2183 BUG();
2184 continue;
2187 // Get SCB associated with this command id
2188 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2189 // a cmm command
2190 scb = adapter->uscb_list + (completed[i] -
2191 MBOX_MAX_SCSI_CMDS);
2193 else {
2194 // an os command
2195 scb = adapter->kscb_list + completed[i];
2198 scb->status = mbox->status;
2199 list_add_tail(&scb->list, &clist);
2202 // Acknowledge interrupt
2203 WRINDOOR(raid_dev, 0x02);
2205 } while(1);
2207 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2210 // put the completed commands in the completed list. DPC would
2211 // complete these commands later
2212 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2214 list_splice(&clist, &adapter->completed_list);
2216 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2219 // schedule the DPC if there is some work for it
2220 if (handled)
2221 tasklet_schedule(&adapter->dpc_h);
2223 return handled;
2228 * megaraid_isr - isr for memory based mailbox based controllers
2229 * @irq : irq
2230 * @devp : pointer to our soft state
2232 * Interrupt service routine for memory-mapped mailbox controllers.
2234 static irqreturn_t
2235 megaraid_isr(int irq, void *devp)
2237 adapter_t *adapter = devp;
2238 int handled;
2240 handled = megaraid_ack_sequence(adapter);
2242 /* Loop through any pending requests */
2243 if (!adapter->quiescent) {
2244 megaraid_mbox_runpendq(adapter, NULL);
2247 return IRQ_RETVAL(handled);
2252 * megaraid_mbox_sync_scb - sync kernel buffers
2253 * @adapter : controller's soft state
2254 * @scb : pointer to the resource packet
2256 * DMA sync if required.
2258 static void
2259 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2261 mbox_ccb_t *ccb;
2263 ccb = (mbox_ccb_t *)scb->ccb;
2265 switch (scb->dma_type) {
2267 case MRAID_DMA_WBUF:
2268 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2269 pci_dma_sync_single_for_cpu(adapter->pdev,
2270 ccb->buf_dma_h,
2271 scb->scp->request_bufflen,
2272 PCI_DMA_FROMDEVICE);
2275 pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
2276 scb->scp->request_bufflen, scb->dma_direction);
2278 break;
2280 case MRAID_DMA_WSG:
2281 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2282 pci_dma_sync_sg_for_cpu(adapter->pdev,
2283 scb->scp->request_buffer,
2284 scb->scp->use_sg, PCI_DMA_FROMDEVICE);
2287 pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
2288 scb->scp->use_sg, scb->dma_direction);
2290 break;
2292 default:
2293 break;
2296 return;
2301 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2302 * @devp : pointer to HBA soft state
2304 * Pick up the commands from the completed list and send back to the owners.
2305 * This is a reentrant function and does not assume any locks are held while
2306 * it is being called.
2308 static void
2309 megaraid_mbox_dpc(unsigned long devp)
2311 adapter_t *adapter = (adapter_t *)devp;
2312 mraid_device_t *raid_dev;
2313 struct list_head clist;
2314 struct scatterlist *sgl;
2315 scb_t *scb;
2316 scb_t *tmp;
2317 struct scsi_cmnd *scp;
2318 mraid_passthru_t *pthru;
2319 mraid_epassthru_t *epthru;
2320 mbox_ccb_t *ccb;
2321 int islogical;
2322 int pdev_index;
2323 int pdev_state;
2324 mbox_t *mbox;
2325 unsigned long flags;
2326 uint8_t c;
2327 int status;
2328 uioc_t *kioc;
2331 if (!adapter) return;
2333 raid_dev = ADAP2RAIDDEV(adapter);
2335 // move the SCBs from the completed list to our local list
2336 INIT_LIST_HEAD(&clist);
2338 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2340 list_splice_init(&adapter->completed_list, &clist);
2342 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2345 list_for_each_entry_safe(scb, tmp, &clist, list) {
2347 status = scb->status;
2348 scp = scb->scp;
2349 ccb = (mbox_ccb_t *)scb->ccb;
2350 pthru = ccb->pthru;
2351 epthru = ccb->epthru;
2352 mbox = ccb->mbox;
2354 // Make sure f/w has completed a valid command
2355 if (scb->state != SCB_ISSUED) {
2356 con_log(CL_ANN, (KERN_CRIT
2357 "megaraid critical err: invalid command %d:%d:%p\n",
2358 scb->sno, scb->state, scp));
2359 BUG();
2360 continue; // Must never happen!
2363 // check for the management command and complete it right away
2364 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2365 scb->state = SCB_FREE;
2366 scb->status = status;
2368 // remove from local clist
2369 list_del_init(&scb->list);
2371 kioc = (uioc_t *)scb->gp;
2372 kioc->status = 0;
2374 megaraid_mbox_mm_done(adapter, scb);
2376 continue;
2379 // Was an abort issued for this command earlier
2380 if (scb->state & SCB_ABORT) {
2381 con_log(CL_ANN, (KERN_NOTICE
2382 "megaraid: aborted cmd %lx[%x] completed\n",
2383 scp->serial_number, scb->sno));
2387 * If the inquiry came of a disk drive which is not part of
2388 * any RAID array, expose it to the kernel. For this to be
2389 * enabled, user must set the "megaraid_expose_unconf_disks"
2390 * flag to 1 by specifying it on module parameter list.
2391 * This would enable data migration off drives from other
2392 * configurations.
2394 islogical = MRAID_IS_LOGICAL(adapter, scp);
2395 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2396 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2398 if (scp->use_sg) {
2399 sgl = (struct scatterlist *)
2400 scp->request_buffer;
2402 if (sgl->page) {
2403 c = *(unsigned char *)
2404 (page_address((&sgl[0])->page) +
2405 (&sgl[0])->offset);
2407 else {
2408 con_log(CL_ANN, (KERN_WARNING
2409 "megaraid mailbox: invalid sg:%d\n",
2410 __LINE__));
2411 c = 0;
2414 else {
2415 c = *(uint8_t *)scp->request_buffer;
2418 if ((c & 0x1F ) == TYPE_DISK) {
2419 pdev_index = (scb->dev_channel * 16) +
2420 scb->dev_target;
2421 pdev_state =
2422 raid_dev->pdrv_state[pdev_index] & 0x0F;
2424 if (pdev_state == PDRV_ONLINE ||
2425 pdev_state == PDRV_FAILED ||
2426 pdev_state == PDRV_RBLD ||
2427 pdev_state == PDRV_HOTSPARE ||
2428 megaraid_expose_unconf_disks == 0) {
2430 status = 0xF0;
2435 // Convert MegaRAID status to Linux error code
2436 switch (status) {
2438 case 0x00:
2440 scp->result = (DID_OK << 16);
2441 break;
2443 case 0x02:
2445 /* set sense_buffer and result fields */
2446 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2447 mbox->cmd == MBOXCMD_PASSTHRU64) {
2449 memcpy(scp->sense_buffer, pthru->reqsensearea,
2450 14);
2452 scp->result = DRIVER_SENSE << 24 |
2453 DID_OK << 16 | CHECK_CONDITION << 1;
2455 else {
2456 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2458 memcpy(scp->sense_buffer,
2459 epthru->reqsensearea, 14);
2461 scp->result = DRIVER_SENSE << 24 |
2462 DID_OK << 16 |
2463 CHECK_CONDITION << 1;
2464 } else {
2465 scp->sense_buffer[0] = 0x70;
2466 scp->sense_buffer[2] = ABORTED_COMMAND;
2467 scp->result = CHECK_CONDITION << 1;
2470 break;
2472 case 0x08:
2474 scp->result = DID_BUS_BUSY << 16 | status;
2475 break;
2477 default:
2480 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2481 * failed
2483 if (scp->cmnd[0] == TEST_UNIT_READY) {
2484 scp->result = DID_ERROR << 16 |
2485 RESERVATION_CONFLICT << 1;
2487 else
2489 * Error code returned is 1 if Reserve or Release
2490 * failed or the input parameter is invalid
2492 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2493 scp->cmnd[0] == RELEASE)) {
2495 scp->result = DID_ERROR << 16 |
2496 RESERVATION_CONFLICT << 1;
2498 else {
2499 scp->result = DID_BAD_TARGET << 16 | status;
2503 // print a debug message for all failed commands
2504 if (status) {
2505 megaraid_mbox_display_scb(adapter, scb);
2508 // Free our internal resources and call the mid-layer callback
2509 // routine
2510 megaraid_mbox_sync_scb(adapter, scb);
2512 // remove from local clist
2513 list_del_init(&scb->list);
2515 // put back in free list
2516 megaraid_dealloc_scb(adapter, scb);
2518 // send the scsi packet back to kernel
2519 scp->scsi_done(scp);
2522 return;
2527 * megaraid_abort_handler - abort the scsi command
2528 * @scp : command to be aborted
2530 * Abort a previous SCSI request. Only commands on the pending list can be
2531 * aborted. All the commands issued to the F/W must complete.
2533 static int
2534 megaraid_abort_handler(struct scsi_cmnd *scp)
2536 adapter_t *adapter;
2537 mraid_device_t *raid_dev;
2538 scb_t *scb;
2539 scb_t *tmp;
2540 int found;
2541 unsigned long flags;
2542 int i;
2545 adapter = SCP2ADAPTER(scp);
2546 raid_dev = ADAP2RAIDDEV(adapter);
2548 con_log(CL_ANN, (KERN_WARNING
2549 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2550 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2551 SCP2TARGET(scp), SCP2LUN(scp)));
2553 // If FW has stopped responding, simply return failure
2554 if (raid_dev->hw_error) {
2555 con_log(CL_ANN, (KERN_NOTICE
2556 "megaraid: hw error, not aborting\n"));
2557 return FAILED;
2560 // There might a race here, where the command was completed by the
2561 // firmware and now it is on the completed list. Before we could
2562 // complete the command to the kernel in dpc, the abort came.
2563 // Find out if this is the case to avoid the race.
2564 scb = NULL;
2565 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2566 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2568 if (scb->scp == scp) { // Found command
2570 list_del_init(&scb->list); // from completed list
2572 con_log(CL_ANN, (KERN_WARNING
2573 "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2574 scp->serial_number, scb->sno,
2575 scb->dev_channel, scb->dev_target));
2577 scp->result = (DID_ABORT << 16);
2578 scp->scsi_done(scp);
2580 megaraid_dealloc_scb(adapter, scb);
2582 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2583 flags);
2585 return SUCCESS;
2588 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2591 // Find out if this command is still on the pending list. If it is and
2592 // was never issued, abort and return success. If the command is owned
2593 // by the firmware, we must wait for it to complete by the FW.
2594 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2595 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2597 if (scb->scp == scp) { // Found command
2599 list_del_init(&scb->list); // from pending list
2601 ASSERT(!(scb->state & SCB_ISSUED));
2603 con_log(CL_ANN, (KERN_WARNING
2604 "megaraid abort: %ld[%d:%d], driver owner\n",
2605 scp->serial_number, scb->dev_channel,
2606 scb->dev_target));
2608 scp->result = (DID_ABORT << 16);
2609 scp->scsi_done(scp);
2611 megaraid_dealloc_scb(adapter, scb);
2613 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2614 flags);
2616 return SUCCESS;
2619 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2622 // Check do we even own this command, in which case this would be
2623 // owned by the firmware. The only way to locate the FW scb is to
2624 // traverse through the list of all SCB, since driver does not
2625 // maintain these SCBs on any list
2626 found = 0;
2627 spin_lock_irq(&adapter->lock);
2628 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2629 scb = adapter->kscb_list + i;
2631 if (scb->scp == scp) {
2633 found = 1;
2635 if (!(scb->state & SCB_ISSUED)) {
2636 con_log(CL_ANN, (KERN_WARNING
2637 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2638 scp->serial_number, scb->sno, scb->dev_channel,
2639 scb->dev_target));
2640 BUG();
2642 else {
2643 con_log(CL_ANN, (KERN_WARNING
2644 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2645 scp->serial_number, scb->sno, scb->dev_channel,
2646 scb->dev_target));
2650 spin_unlock_irq(&adapter->lock);
2652 if (!found) {
2653 con_log(CL_ANN, (KERN_WARNING
2654 "megaraid abort: scsi cmd:%ld, do now own\n",
2655 scp->serial_number));
2657 // FIXME: Should there be a callback for this command?
2658 return SUCCESS;
2661 // We cannot actually abort a command owned by firmware, return
2662 // failure and wait for reset. In host reset handler, we will find out
2663 // if the HBA is still live
2664 return FAILED;
2668 * megaraid_reset_handler - device reset hadler for mailbox based driver
2669 * @scp : reference command
2671 * Reset handler for the mailbox based controller. First try to find out if
2672 * the FW is still live, in which case the outstanding commands counter mut go
2673 * down to 0. If that happens, also issue the reservation reset command to
2674 * relinquish (possible) reservations on the logical drives connected to this
2675 * host.
2677 static int
2678 megaraid_reset_handler(struct scsi_cmnd *scp)
2680 adapter_t *adapter;
2681 scb_t *scb;
2682 scb_t *tmp;
2683 mraid_device_t *raid_dev;
2684 unsigned long flags;
2685 uint8_t raw_mbox[sizeof(mbox_t)];
2686 int rval;
2687 int recovery_window;
2688 int recovering;
2689 int i;
2690 uioc_t *kioc;
2692 adapter = SCP2ADAPTER(scp);
2693 raid_dev = ADAP2RAIDDEV(adapter);
2695 // return failure if adapter is not responding
2696 if (raid_dev->hw_error) {
2697 con_log(CL_ANN, (KERN_NOTICE
2698 "megaraid: hw error, cannot reset\n"));
2699 return FAILED;
2703 // Under exceptional conditions, FW can take up to 3 minutes to
2704 // complete command processing. Wait for additional 2 minutes for the
2705 // pending commands counter to go down to 0. If it doesn't, let the
2706 // controller be marked offline
2707 // Also, reset all the commands currently owned by the driver
2708 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2709 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2710 list_del_init(&scb->list); // from pending list
2712 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2713 con_log(CL_ANN, (KERN_WARNING
2714 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2715 scb->sno, scb->dev_channel, scb->dev_target));
2717 scb->status = -1;
2719 kioc = (uioc_t *)scb->gp;
2720 kioc->status = -EFAULT;
2722 megaraid_mbox_mm_done(adapter, scb);
2723 } else {
2724 if (scb->scp == scp) { // Found command
2725 con_log(CL_ANN, (KERN_WARNING
2726 "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2727 scp->serial_number, scb->sno,
2728 scb->dev_channel, scb->dev_target));
2729 } else {
2730 con_log(CL_ANN, (KERN_WARNING
2731 "megaraid: IO packet with %d[%d:%d] being reset\n",
2732 scb->sno, scb->dev_channel, scb->dev_target));
2735 scb->scp->result = (DID_RESET << 16);
2736 scb->scp->scsi_done(scb->scp);
2738 megaraid_dealloc_scb(adapter, scb);
2741 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2743 if (adapter->outstanding_cmds) {
2744 con_log(CL_ANN, (KERN_NOTICE
2745 "megaraid: %d outstanding commands. Max wait %d sec\n",
2746 adapter->outstanding_cmds,
2747 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2750 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2752 recovering = adapter->outstanding_cmds;
2754 for (i = 0; i < recovery_window; i++) {
2756 megaraid_ack_sequence(adapter);
2758 // print a message once every 5 seconds only
2759 if (!(i % 5)) {
2760 con_log(CL_ANN, (
2761 "megaraid mbox: Wait for %d commands to complete:%d\n",
2762 adapter->outstanding_cmds,
2763 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2766 // bailout if no recovery happended in reset time
2767 if (adapter->outstanding_cmds == 0) {
2768 break;
2771 msleep(1000);
2774 spin_lock(&adapter->lock);
2776 // If still outstanding commands, bail out
2777 if (adapter->outstanding_cmds) {
2778 con_log(CL_ANN, (KERN_WARNING
2779 "megaraid mbox: critical hardware error!\n"));
2781 raid_dev->hw_error = 1;
2783 rval = FAILED;
2784 goto out;
2786 else {
2787 con_log(CL_ANN, (KERN_NOTICE
2788 "megaraid mbox: reset sequence completed sucessfully\n"));
2792 // If the controller supports clustering, reset reservations
2793 if (!adapter->ha) {
2794 rval = SUCCESS;
2795 goto out;
2798 // clear reservations if any
2799 raw_mbox[0] = CLUSTER_CMD;
2800 raw_mbox[2] = RESET_RESERVATIONS;
2802 rval = SUCCESS;
2803 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2804 con_log(CL_ANN,
2805 (KERN_INFO "megaraid: reservation reset\n"));
2807 else {
2808 rval = FAILED;
2809 con_log(CL_ANN, (KERN_WARNING
2810 "megaraid: reservation reset failed\n"));
2813 out:
2814 spin_unlock_irq(&adapter->lock);
2815 return rval;
2819 * START: internal commands library
2821 * This section of the driver has the common routine used by the driver and
2822 * also has all the FW routines
2826 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2827 * @adapter : controller's soft state
2828 * @raw_mbox : the mailbox
2830 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2831 * controllers.
2833 static int
2834 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2836 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2837 mbox64_t *mbox64;
2838 mbox_t *mbox;
2839 uint8_t status;
2840 int i;
2843 mbox64 = raid_dev->mbox64;
2844 mbox = raid_dev->mbox;
2847 * Wait until mailbox is free
2849 if (megaraid_busywait_mbox(raid_dev) != 0)
2850 goto blocked_mailbox;
2853 * Copy mailbox data into host structure
2855 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2856 mbox->cmdid = 0xFE;
2857 mbox->busy = 1;
2858 mbox->poll = 0;
2859 mbox->ack = 0;
2860 mbox->numstatus = 0xFF;
2861 mbox->status = 0xFF;
2863 wmb();
2864 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2866 // wait for maximum 1 second for status to post. If the status is not
2867 // available within 1 second, assume FW is initializing and wait
2868 // for an extended amount of time
2869 if (mbox->numstatus == 0xFF) { // status not yet available
2870 udelay(25);
2872 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2873 rmb();
2874 msleep(1);
2878 if (i == 1000) {
2879 con_log(CL_ANN, (KERN_NOTICE
2880 "megaraid mailbox: wait for FW to boot "));
2882 for (i = 0; (mbox->numstatus == 0xFF) &&
2883 (i < MBOX_RESET_WAIT); i++) {
2884 rmb();
2885 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2886 MBOX_RESET_WAIT - i));
2887 msleep(1000);
2890 if (i == MBOX_RESET_WAIT) {
2892 con_log(CL_ANN, (
2893 "\nmegaraid mailbox: status not available\n"));
2895 return -1;
2897 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2901 // wait for maximum 1 second for poll semaphore
2902 if (mbox->poll != 0x77) {
2903 udelay(25);
2905 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2906 rmb();
2907 msleep(1);
2910 if (i == 1000) {
2911 con_log(CL_ANN, (KERN_WARNING
2912 "megaraid mailbox: could not get poll semaphore\n"));
2913 return -1;
2917 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2918 wmb();
2920 // wait for maximum 1 second for acknowledgement
2921 if (RDINDOOR(raid_dev) & 0x2) {
2922 udelay(25);
2924 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2925 rmb();
2926 msleep(1);
2929 if (i == 1000) {
2930 con_log(CL_ANN, (KERN_WARNING
2931 "megaraid mailbox: could not acknowledge\n"));
2932 return -1;
2935 mbox->poll = 0;
2936 mbox->ack = 0x77;
2938 status = mbox->status;
2940 // invalidate the completed command id array. After command
2941 // completion, firmware would write the valid id.
2942 mbox->numstatus = 0xFF;
2943 mbox->status = 0xFF;
2944 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2945 mbox->completed[i] = 0xFF;
2948 return status;
2950 blocked_mailbox:
2952 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2953 return -1;
2958 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2959 * @adapter : controller's soft state
2960 * @raw_mbox : the mailbox
2962 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2963 * controllers. This is a faster version of the synchronous command and
2964 * therefore can be called in interrupt-context as well.
2966 static int
2967 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2969 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2970 mbox_t *mbox;
2971 long i;
2974 mbox = raid_dev->mbox;
2976 // return immediately if the mailbox is busy
2977 if (mbox->busy) return -1;
2979 // Copy mailbox data into host structure
2980 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2981 mbox->cmdid = 0xFE;
2982 mbox->busy = 1;
2983 mbox->poll = 0;
2984 mbox->ack = 0;
2985 mbox->numstatus = 0xFF;
2986 mbox->status = 0xFF;
2988 wmb();
2989 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2991 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2992 if (mbox->numstatus != 0xFF) break;
2993 rmb();
2994 udelay(MBOX_SYNC_DELAY_200);
2997 if (i == MBOX_SYNC_WAIT_CNT) {
2998 // We may need to re-calibrate the counter
2999 con_log(CL_ANN, (KERN_CRIT
3000 "megaraid: fast sync command timed out\n"));
3003 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
3004 wmb();
3006 return mbox->status;
3011 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
3012 * @raid_dev : RAID device (HBA) soft state
3014 * Wait until the controller's mailbox is available to accept more commands.
3015 * Wait for at most 1 second.
3017 static int
3018 megaraid_busywait_mbox(mraid_device_t *raid_dev)
3020 mbox_t *mbox = raid_dev->mbox;
3021 int i = 0;
3023 if (mbox->busy) {
3024 udelay(25);
3025 for (i = 0; mbox->busy && i < 1000; i++)
3026 msleep(1);
3029 if (i < 1000) return 0;
3030 else return -1;
3035 * megaraid_mbox_product_info - some static information about the controller
3036 * @adapter : our soft state
3038 * Issue commands to the controller to grab some parameters required by our
3039 * caller.
3041 static int
3042 megaraid_mbox_product_info(adapter_t *adapter)
3044 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3045 mbox_t *mbox;
3046 uint8_t raw_mbox[sizeof(mbox_t)];
3047 mraid_pinfo_t *pinfo;
3048 dma_addr_t pinfo_dma_h;
3049 mraid_inquiry3_t *mraid_inq3;
3050 int i;
3053 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3054 mbox = (mbox_t *)raw_mbox;
3057 * Issue an ENQUIRY3 command to find out certain adapter parameters,
3058 * e.g., max channels, max commands etc.
3060 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3061 &pinfo_dma_h);
3063 if (pinfo == NULL) {
3064 con_log(CL_ANN, (KERN_WARNING
3065 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3066 __LINE__));
3068 return -1;
3070 memset(pinfo, 0, sizeof(mraid_pinfo_t));
3072 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3073 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3075 raw_mbox[0] = FC_NEW_CONFIG;
3076 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
3077 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3079 // Issue the command
3080 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3082 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3084 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3085 pinfo, pinfo_dma_h);
3087 return -1;
3091 * Collect information about state of each physical drive
3092 * attached to the controller. We will expose all the disks
3093 * which are not part of RAID
3095 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3096 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3097 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3101 * Get product info for information like number of channels,
3102 * maximum commands supported.
3104 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3105 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3107 raw_mbox[0] = FC_NEW_CONFIG;
3108 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3110 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3112 con_log(CL_ANN, (KERN_WARNING
3113 "megaraid: product info failed\n"));
3115 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3116 pinfo, pinfo_dma_h);
3118 return -1;
3122 * Setup some parameters for host, as required by our caller
3124 adapter->max_channel = pinfo->nchannels;
3127 * we will export all the logical drives on a single channel.
3128 * Add 1 since inquires do not come for inititor ID
3130 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3131 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3134 * These are the maximum outstanding commands for the scsi-layer
3136 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3138 memset(adapter->fw_version, 0, VERSION_SIZE);
3139 memset(adapter->bios_version, 0, VERSION_SIZE);
3141 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3142 adapter->fw_version[4] = 0;
3144 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3145 adapter->bios_version[4] = 0;
3147 con_log(CL_ANN, (KERN_NOTICE
3148 "megaraid: fw version:[%s] bios version:[%s]\n",
3149 adapter->fw_version, adapter->bios_version));
3151 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3152 pinfo_dma_h);
3154 return 0;
3160 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3161 * @adapter : soft state for the controller
3163 * This routine check whether the controller in question supports extended
3164 * ( > 10 bytes ) CDBs.
3166 static int
3167 megaraid_mbox_extended_cdb(adapter_t *adapter)
3169 mbox_t *mbox;
3170 uint8_t raw_mbox[sizeof(mbox_t)];
3171 int rval;
3173 mbox = (mbox_t *)raw_mbox;
3175 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3176 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3178 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3180 raw_mbox[0] = MAIN_MISC_OPCODE;
3181 raw_mbox[2] = SUPPORT_EXT_CDB;
3184 * Issue the command
3186 rval = 0;
3187 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3188 rval = -1;
3191 return rval;
3196 * megaraid_mbox_support_ha - Do we support clustering
3197 * @adapter : soft state for the controller
3198 * @init_id : ID of the initiator
3200 * Determine if the firmware supports clustering and the ID of the initiator.
3202 static int
3203 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3205 mbox_t *mbox;
3206 uint8_t raw_mbox[sizeof(mbox_t)];
3207 int rval;
3210 mbox = (mbox_t *)raw_mbox;
3212 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3214 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3216 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3218 raw_mbox[0] = GET_TARGET_ID;
3220 // Issue the command
3221 *init_id = 7;
3222 rval = -1;
3223 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3225 *init_id = *(uint8_t *)adapter->ibuf;
3227 con_log(CL_ANN, (KERN_INFO
3228 "megaraid: cluster firmware, initiator ID: %d\n",
3229 *init_id));
3231 rval = 0;
3234 return rval;
3239 * megaraid_mbox_support_random_del - Do we support random deletion
3240 * @adapter : soft state for the controller
3242 * Determine if the firmware supports random deletion.
3243 * Return: 1 is operation supported, 0 otherwise
3245 static int
3246 megaraid_mbox_support_random_del(adapter_t *adapter)
3248 mbox_t *mbox;
3249 uint8_t raw_mbox[sizeof(mbox_t)];
3250 int rval;
3253 mbox = (mbox_t *)raw_mbox;
3255 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3257 raw_mbox[0] = FC_DEL_LOGDRV;
3258 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3260 // Issue the command
3261 rval = 0;
3262 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3264 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3266 rval = 1;
3269 return rval;
3274 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3275 * @adapter : soft state for the controller
3277 * Find out the maximum number of scatter-gather elements supported by the
3278 * firmware.
3280 static int
3281 megaraid_mbox_get_max_sg(adapter_t *adapter)
3283 mbox_t *mbox;
3284 uint8_t raw_mbox[sizeof(mbox_t)];
3285 int nsg;
3288 mbox = (mbox_t *)raw_mbox;
3290 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3292 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3294 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3296 raw_mbox[0] = MAIN_MISC_OPCODE;
3297 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3299 // Issue the command
3300 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3301 nsg = *(uint8_t *)adapter->ibuf;
3303 else {
3304 nsg = MBOX_DEFAULT_SG_SIZE;
3307 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3309 return nsg;
3314 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3315 * @adapter : soft state for the controller
3317 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3318 * can be exported as regular SCSI channels.
3320 static void
3321 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3323 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3324 mbox_t *mbox;
3325 uint8_t raw_mbox[sizeof(mbox_t)];
3328 mbox = (mbox_t *)raw_mbox;
3330 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3332 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3334 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3336 raw_mbox[0] = CHNL_CLASS;
3337 raw_mbox[2] = GET_CHNL_CLASS;
3339 // Issue the command. If the command fails, all channels are RAID
3340 // channels
3341 raid_dev->channel_class = 0xFF;
3342 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3343 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3346 return;
3351 * megaraid_mbox_flush_cache - flush adapter and disks cache
3352 * @adapter : soft state for the controller
3354 * Flush adapter cache followed by disks cache.
3356 static void
3357 megaraid_mbox_flush_cache(adapter_t *adapter)
3359 mbox_t *mbox;
3360 uint8_t raw_mbox[sizeof(mbox_t)];
3363 mbox = (mbox_t *)raw_mbox;
3365 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3367 raw_mbox[0] = FLUSH_ADAPTER;
3369 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3370 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3373 raw_mbox[0] = FLUSH_SYSTEM;
3375 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3376 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3379 return;
3384 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3385 * @adapter : soft state for the controller
3387 * Clears the pending cmds in FW and reinits its RAID structs.
3389 static int
3390 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3392 mbox_t *mbox;
3393 uint8_t raw_mbox[sizeof(mbox_t)];
3394 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3395 mbox64_t *mbox64;
3396 int status = 0;
3397 int i;
3398 uint32_t dword;
3400 mbox = (mbox_t *)raw_mbox;
3402 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3404 raw_mbox[0] = 0xFF;
3406 mbox64 = raid_dev->mbox64;
3407 mbox = raid_dev->mbox;
3409 /* Wait until mailbox is free */
3410 if (megaraid_busywait_mbox(raid_dev) != 0) {
3411 status = 1;
3412 goto blocked_mailbox;
3415 /* Copy mailbox data into host structure */
3416 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3417 mbox->cmdid = 0xFE;
3418 mbox->busy = 1;
3419 mbox->poll = 0;
3420 mbox->ack = 0;
3421 mbox->numstatus = 0;
3422 mbox->status = 0;
3424 wmb();
3425 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3427 /* Wait for maximum 1 min for status to post.
3428 * If the Firmware SUPPORTS the ABOVE COMMAND,
3429 * mbox->cmd will be set to 0
3430 * else
3431 * the firmware will reject the command with
3432 * mbox->numstatus set to 1
3435 i = 0;
3436 status = 0;
3437 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3438 rmb();
3439 msleep(1);
3440 i++;
3441 if (i > 1000 * 60) {
3442 status = 1;
3443 break;
3446 if (mbox->numstatus == 1)
3447 status = 1; /*cmd not supported*/
3449 /* Check for interrupt line */
3450 dword = RDOUTDOOR(raid_dev);
3451 WROUTDOOR(raid_dev, dword);
3452 WRINDOOR(raid_dev,2);
3454 return status;
3456 blocked_mailbox:
3457 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3458 return status;
3462 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3463 * @adapter : controller's soft state
3464 * @scb : SCB to be displayed
3465 * @level : debug level for console print
3467 * Diplay information about the given SCB iff the current debug level is
3468 * verbose.
3470 static void
3471 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3473 mbox_ccb_t *ccb;
3474 struct scsi_cmnd *scp;
3475 mbox_t *mbox;
3476 int level;
3477 int i;
3480 ccb = (mbox_ccb_t *)scb->ccb;
3481 scp = scb->scp;
3482 mbox = ccb->mbox;
3484 level = CL_DLEVEL3;
3486 con_log(level, (KERN_NOTICE
3487 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3488 mbox->cmd, scb->sno));
3490 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3491 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3492 mbox->numsge));
3494 if (!scp) return;
3496 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3498 for (i = 0; i < scp->cmd_len; i++) {
3499 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3502 con_log(level, ("\n"));
3504 return;
3509 * megaraid_mbox_setup_device_map - manage device ids
3510 * @adapter : Driver's soft state
3512 * Manange the device ids to have an appropraite mapping between the kernel
3513 * scsi addresses and megaraid scsi and logical drive addresses. We export
3514 * scsi devices on their actual addresses, whereas the logical drives are
3515 * exported on a virtual scsi channel.
3517 static void
3518 megaraid_mbox_setup_device_map(adapter_t *adapter)
3520 uint8_t c;
3521 uint8_t t;
3524 * First fill the values on the logical drive channel
3526 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3527 adapter->device_ids[adapter->max_channel][t] =
3528 (t < adapter->init_id) ? t : t - 1;
3530 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3533 * Fill the values on the physical devices channels
3535 for (c = 0; c < adapter->max_channel; c++)
3536 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3537 adapter->device_ids[c][t] = (c << 8) | t;
3542 * END: internal commands library
3546 * START: Interface for the common management module
3548 * This is the module, which interfaces with the common mangement module to
3549 * provide support for ioctl and sysfs
3553 * megaraid_cmm_register - register with the mangement module
3554 * @adapter : HBA soft state
3556 * Register with the management module, which allows applications to issue
3557 * ioctl calls to the drivers. This interface is used by the management module
3558 * to setup sysfs support as well.
3560 static int
3561 megaraid_cmm_register(adapter_t *adapter)
3563 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3564 mraid_mmadp_t adp;
3565 scb_t *scb;
3566 mbox_ccb_t *ccb;
3567 int rval;
3568 int i;
3570 // Allocate memory for the base list of scb for management module.
3571 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3573 if (adapter->uscb_list == NULL) {
3574 con_log(CL_ANN, (KERN_WARNING
3575 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3576 __LINE__));
3577 return -1;
3581 // Initialize the synchronization parameters for resources for
3582 // commands for management module
3583 INIT_LIST_HEAD(&adapter->uscb_pool);
3585 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3589 // link all the packets. Note, CCB for commands, coming from the
3590 // commom management module, mailbox physical address are already
3591 // setup by it. We just need placeholder for that in our local command
3592 // control blocks
3593 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3595 scb = adapter->uscb_list + i;
3596 ccb = raid_dev->uccb_list + i;
3598 scb->ccb = (caddr_t)ccb;
3599 ccb->mbox64 = raid_dev->umbox64 + i;
3600 ccb->mbox = &ccb->mbox64->mbox32;
3601 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3603 scb->gp = 0;
3605 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3606 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3607 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3609 scb->scp = NULL;
3610 scb->state = SCB_FREE;
3611 scb->dma_direction = PCI_DMA_NONE;
3612 scb->dma_type = MRAID_DMA_NONE;
3613 scb->dev_channel = -1;
3614 scb->dev_target = -1;
3616 // put scb in the free pool
3617 list_add_tail(&scb->list, &adapter->uscb_pool);
3620 adp.unique_id = adapter->unique_id;
3621 adp.drvr_type = DRVRTYPE_MBOX;
3622 adp.drvr_data = (unsigned long)adapter;
3623 adp.pdev = adapter->pdev;
3624 adp.issue_uioc = megaraid_mbox_mm_handler;
3625 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3626 adp.max_kioc = MBOX_MAX_USER_CMDS;
3628 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3630 con_log(CL_ANN, (KERN_WARNING
3631 "megaraid mbox: did not register with CMM\n"));
3633 kfree(adapter->uscb_list);
3636 return rval;
3641 * megaraid_cmm_unregister - un-register with the mangement module
3642 * @adapter : HBA soft state
3644 * Un-register with the management module.
3645 * FIXME: mgmt module must return failure for unregister if it has pending
3646 * commands in LLD.
3648 static int
3649 megaraid_cmm_unregister(adapter_t *adapter)
3651 kfree(adapter->uscb_list);
3652 mraid_mm_unregister_adp(adapter->unique_id);
3653 return 0;
3658 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3659 * @drvr_data : LLD specific data
3660 * @kioc : CMM interface packet
3661 * @action : command action
3663 * This routine is invoked whenever the Common Mangement Module (CMM) has a
3664 * command for us. The 'action' parameter specifies if this is a new command
3665 * or otherwise.
3667 static int
3668 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3670 adapter_t *adapter;
3672 if (action != IOCTL_ISSUE) {
3673 con_log(CL_ANN, (KERN_WARNING
3674 "megaraid: unsupported management action:%#2x\n",
3675 action));
3676 return (-ENOTSUPP);
3679 adapter = (adapter_t *)drvr_data;
3681 // make sure this adapter is not being detached right now.
3682 if (atomic_read(&adapter->being_detached)) {
3683 con_log(CL_ANN, (KERN_WARNING
3684 "megaraid: reject management request, detaching\n"));
3685 return (-ENODEV);
3688 switch (kioc->opcode) {
3690 case GET_ADAP_INFO:
3692 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3693 (unsigned long)kioc->buf_vaddr);
3695 kioc->done(kioc);
3697 return kioc->status;
3699 case MBOX_CMD:
3701 return megaraid_mbox_mm_command(adapter, kioc);
3703 default:
3704 kioc->status = (-EINVAL);
3705 kioc->done(kioc);
3706 return (-EINVAL);
3709 return 0; // not reached
3713 * megaraid_mbox_mm_command - issues commands routed through CMM
3714 * @adapter : HBA soft state
3715 * @kioc : management command packet
3717 * Issues commands, which are routed through the management module.
3719 static int
3720 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3722 struct list_head *head = &adapter->uscb_pool;
3723 mbox64_t *mbox64;
3724 uint8_t *raw_mbox;
3725 scb_t *scb;
3726 mbox_ccb_t *ccb;
3727 unsigned long flags;
3729 // detach one scb from free pool
3730 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3732 if (list_empty(head)) { // should never happen because of CMM
3734 con_log(CL_ANN, (KERN_WARNING
3735 "megaraid mbox: bug in cmm handler, lost resources\n"));
3737 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3739 return (-EINVAL);
3742 scb = list_entry(head->next, scb_t, list);
3743 list_del_init(&scb->list);
3745 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3747 scb->state = SCB_ACTIVE;
3748 scb->dma_type = MRAID_DMA_NONE;
3749 scb->dma_direction = PCI_DMA_NONE;
3751 ccb = (mbox_ccb_t *)scb->ccb;
3752 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3753 raw_mbox = (uint8_t *)&mbox64->mbox32;
3755 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3757 scb->gp = (unsigned long)kioc;
3760 * If it is a logdrv random delete operation, we have to wait till
3761 * there are no outstanding cmds at the fw and then issue it directly
3763 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3765 if (wait_till_fw_empty(adapter)) {
3766 con_log(CL_ANN, (KERN_NOTICE
3767 "megaraid mbox: LD delete, timed out\n"));
3769 kioc->status = -ETIME;
3771 scb->status = -1;
3773 megaraid_mbox_mm_done(adapter, scb);
3775 return (-ETIME);
3778 INIT_LIST_HEAD(&scb->list);
3780 scb->state = SCB_ISSUED;
3781 if (mbox_post_cmd(adapter, scb) != 0) {
3783 con_log(CL_ANN, (KERN_NOTICE
3784 "megaraid mbox: LD delete, mailbox busy\n"));
3786 kioc->status = -EBUSY;
3788 scb->status = -1;
3790 megaraid_mbox_mm_done(adapter, scb);
3792 return (-EBUSY);
3795 return 0;
3798 // put the command on the pending list and execute
3799 megaraid_mbox_runpendq(adapter, scb);
3801 return 0;
3805 static int
3806 wait_till_fw_empty(adapter_t *adapter)
3808 unsigned long flags = 0;
3809 int i;
3813 * Set the quiescent flag to stop issuing cmds to FW.
3815 spin_lock_irqsave(&adapter->lock, flags);
3816 adapter->quiescent++;
3817 spin_unlock_irqrestore(&adapter->lock, flags);
3820 * Wait till there are no more cmds outstanding at FW. Try for at most
3821 * 60 seconds
3823 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3824 con_log(CL_DLEVEL1, (KERN_INFO
3825 "megaraid: FW has %d pending commands\n",
3826 adapter->outstanding_cmds));
3828 msleep(1000);
3831 return adapter->outstanding_cmds;
3836 * megaraid_mbox_mm_done - callback for CMM commands
3837 * @adapter : HBA soft state
3838 * @scb : completed command
3840 * Callback routine for internal commands originated from the management
3841 * module.
3843 static void
3844 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3846 uioc_t *kioc;
3847 mbox64_t *mbox64;
3848 uint8_t *raw_mbox;
3849 unsigned long flags;
3851 kioc = (uioc_t *)scb->gp;
3852 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3853 mbox64->mbox32.status = scb->status;
3854 raw_mbox = (uint8_t *)&mbox64->mbox32;
3857 // put scb in the free pool
3858 scb->state = SCB_FREE;
3859 scb->scp = NULL;
3861 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3863 list_add(&scb->list, &adapter->uscb_pool);
3865 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3867 // if a delete logical drive operation succeeded, restart the
3868 // controller
3869 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3871 adapter->quiescent--;
3873 megaraid_mbox_runpendq(adapter, NULL);
3876 kioc->done(kioc);
3878 return;
3883 * gather_hbainfo - HBA characteristics for the applications
3884 * @adapter : HBA soft state
3885 * @hinfo : pointer to the caller's host info strucuture
3887 static int
3888 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3890 uint8_t dmajor;
3892 dmajor = megaraid_mbox_version[0];
3894 hinfo->pci_vendor_id = adapter->pdev->vendor;
3895 hinfo->pci_device_id = adapter->pdev->device;
3896 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3897 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3899 hinfo->pci_bus = adapter->pdev->bus->number;
3900 hinfo->pci_dev_fn = adapter->pdev->devfn;
3901 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3902 hinfo->irq = adapter->host->irq;
3903 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3905 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3906 hinfo->host_no = adapter->host->host_no;
3908 return 0;
3912 * END: Interface for the common management module
3918 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3919 * @adapter : controller's soft state
3921 * Allocate packets required to issue FW calls whenever the sysfs attributes
3922 * are read. These attributes would require up-to-date information from the
3923 * FW. Also set up resources for mutual exclusion to share these resources and
3924 * the wait queue.
3926 * Return 0 on success.
3927 * Return -ERROR_CODE on failure.
3929 static int
3930 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3932 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3933 int rval = 0;
3935 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3937 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3939 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3940 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3942 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3943 !raid_dev->sysfs_buffer) {
3945 con_log(CL_ANN, (KERN_WARNING
3946 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3947 __LINE__));
3949 rval = -ENOMEM;
3951 megaraid_sysfs_free_resources(adapter);
3954 sema_init(&raid_dev->sysfs_sem, 1);
3956 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3958 return rval;
3963 * megaraid_sysfs_free_resources - free sysfs related resources
3964 * @adapter : controller's soft state
3966 * Free packets allocated for sysfs FW commands
3968 static void
3969 megaraid_sysfs_free_resources(adapter_t *adapter)
3971 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3973 kfree(raid_dev->sysfs_uioc);
3974 kfree(raid_dev->sysfs_mbox64);
3976 if (raid_dev->sysfs_buffer) {
3977 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3978 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3984 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3985 * @uioc : completed packet
3987 * Callback routine called in the ISR/tasklet context for get ldmap call
3989 static void
3990 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3992 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3993 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3995 uioc->status = 0;
3997 wake_up(&raid_dev->sysfs_wait_q);
4002 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
4003 * @data : timed out packet
4005 * Timeout routine to recover and return to application, in case the adapter
4006 * has stopped responding. A timeout of 60 seconds for this command seems like
4007 * a good value.
4009 static void
4010 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
4012 uioc_t *uioc = (uioc_t *)data;
4013 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
4014 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4016 uioc->status = -ETIME;
4018 wake_up(&raid_dev->sysfs_wait_q);
4023 * megaraid_sysfs_get_ldmap - get update logical drive map
4024 * @adapter : controller's soft state
4026 * This routine will be called whenever user reads the logical drive
4027 * attributes, go get the current logical drive mapping table from the
4028 * firmware. We use the managment API's to issue commands to the controller.
4030 * NOTE: The commands issuance functionality is not generalized and
4031 * implemented in context of "get ld map" command only. If required, the
4032 * command issuance logical can be trivially pulled out and implemented as a
4033 * standalone libary. For now, this should suffice since there is no other
4034 * user of this interface.
4036 * Return 0 on success.
4037 * Return -1 on failure.
4039 static int
4040 megaraid_sysfs_get_ldmap(adapter_t *adapter)
4042 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4043 uioc_t *uioc;
4044 mbox64_t *mbox64;
4045 mbox_t *mbox;
4046 char *raw_mbox;
4047 struct timer_list sysfs_timer;
4048 struct timer_list *timerp;
4049 caddr_t ldmap;
4050 int rval = 0;
4053 * Allow only one read at a time to go through the sysfs attributes
4055 down(&raid_dev->sysfs_sem);
4057 uioc = raid_dev->sysfs_uioc;
4058 mbox64 = raid_dev->sysfs_mbox64;
4059 ldmap = raid_dev->sysfs_buffer;
4061 memset(uioc, 0, sizeof(uioc_t));
4062 memset(mbox64, 0, sizeof(mbox64_t));
4063 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
4065 mbox = &mbox64->mbox32;
4066 raw_mbox = (char *)mbox;
4067 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
4068 uioc->buf_vaddr = (caddr_t)adapter;
4069 uioc->status = -ENODATA;
4070 uioc->done = megaraid_sysfs_get_ldmap_done;
4073 * Prepare the mailbox packet to get the current logical drive mapping
4074 * table
4076 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
4078 raw_mbox[0] = FC_DEL_LOGDRV;
4079 raw_mbox[2] = OP_GET_LDID_MAP;
4082 * Setup a timer to recover from a non-responding controller
4084 timerp = &sysfs_timer;
4085 init_timer(timerp);
4087 timerp->function = megaraid_sysfs_get_ldmap_timeout;
4088 timerp->data = (unsigned long)uioc;
4089 timerp->expires = jiffies + 60 * HZ;
4091 add_timer(timerp);
4094 * Send the command to the firmware
4096 rval = megaraid_mbox_mm_command(adapter, uioc);
4098 if (rval == 0) { // command successfully issued
4099 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4102 * Check if the command timed out
4104 if (uioc->status == -ETIME) {
4105 con_log(CL_ANN, (KERN_NOTICE
4106 "megaraid: sysfs get ld map timed out\n"));
4108 rval = -ETIME;
4110 else {
4111 rval = mbox->status;
4114 if (rval == 0) {
4115 memcpy(raid_dev->curr_ldmap, ldmap,
4116 sizeof(raid_dev->curr_ldmap));
4118 else {
4119 con_log(CL_ANN, (KERN_NOTICE
4120 "megaraid: get ld map failed with %x\n", rval));
4123 else {
4124 con_log(CL_ANN, (KERN_NOTICE
4125 "megaraid: could not issue ldmap command:%x\n", rval));
4129 del_timer_sync(timerp);
4131 up(&raid_dev->sysfs_sem);
4133 return rval;
4138 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4139 * @cdev : class device object representation for the host
4140 * @buf : buffer to send data to
4142 * Display the handle used by the applications while executing management
4143 * tasks on the adapter. We invoke a management module API to get the adapter
4144 * handle, since we do not interface with applications directly.
4146 static ssize_t
4147 megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
4149 struct Scsi_Host *shost = class_to_shost(cdev);
4150 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4151 uint32_t app_hndl;
4153 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4155 return snprintf(buf, 8, "%u\n", app_hndl);
4160 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4161 * @dev : device object representation for the scsi device
4162 * @attr : device attribute to show
4163 * @buf : buffer to send data to
4165 * Display the logical drive number for the device in question, if it a valid
4166 * logical drive. For physical devices, "-1" is returned.
4168 * The logical drive number is displayed in following format:
4170 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4172 * <int> <int> <int> <int>
4174 static ssize_t
4175 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4177 struct scsi_device *sdev = to_scsi_device(dev);
4178 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4179 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4180 int scsi_id = -1;
4181 int logical_drv = -1;
4182 int ldid_map = -1;
4183 uint32_t app_hndl = 0;
4184 int mapped_sdev_id;
4185 int rval;
4186 int i;
4188 if (raid_dev->random_del_supported &&
4189 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4191 rval = megaraid_sysfs_get_ldmap(adapter);
4192 if (rval == 0) {
4194 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4196 mapped_sdev_id = sdev->id;
4198 if (sdev->id > adapter->init_id) {
4199 mapped_sdev_id -= 1;
4202 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4204 scsi_id = sdev->id;
4206 logical_drv = i;
4208 ldid_map = raid_dev->curr_ldmap[i];
4210 app_hndl = mraid_mm_adapter_app_handle(
4211 adapter->unique_id);
4213 break;
4217 else {
4218 con_log(CL_ANN, (KERN_NOTICE
4219 "megaraid: sysfs get ld map failed: %x\n",
4220 rval));
4224 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4225 ldid_map, app_hndl);
4230 * END: Mailbox Low Level Driver
4232 module_init(megaraid_init);
4233 module_exit(megaraid_exit);
4235 /* vim: set ts=8 sw=8 tw=78 ai si: */