x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_mbox.c
blobf0987f22ea70be2f13cf434120155112fcbcc9dc
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/scsi/ChangeLog.megaraid
73 #include <linux/slab.h>
74 #include <linux/module.h>
75 #include "megaraid_mbox.h"
77 static int megaraid_init(void);
78 static void megaraid_exit(void);
80 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
81 static void megaraid_detach_one(struct pci_dev *);
82 static void megaraid_mbox_shutdown(struct pci_dev *);
84 static int megaraid_io_attach(adapter_t *);
85 static void megaraid_io_detach(adapter_t *);
87 static int megaraid_init_mbox(adapter_t *);
88 static void megaraid_fini_mbox(adapter_t *);
90 static int megaraid_alloc_cmd_packets(adapter_t *);
91 static void megaraid_free_cmd_packets(adapter_t *);
93 static int megaraid_mbox_setup_dma_pools(adapter_t *);
94 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
96 static int megaraid_sysfs_alloc_resources(adapter_t *);
97 static void megaraid_sysfs_free_resources(adapter_t *);
99 static int megaraid_abort_handler(struct scsi_cmnd *);
100 static int megaraid_reset_handler(struct scsi_cmnd *);
102 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
103 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
104 static int megaraid_busywait_mbox(mraid_device_t *);
105 static int megaraid_mbox_product_info(adapter_t *);
106 static int megaraid_mbox_extended_cdb(adapter_t *);
107 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
108 static int megaraid_mbox_support_random_del(adapter_t *);
109 static int megaraid_mbox_get_max_sg(adapter_t *);
110 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
111 static void megaraid_mbox_flush_cache(adapter_t *);
112 static int megaraid_mbox_fire_sync_cmd(adapter_t *);
114 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
115 static void megaraid_mbox_setup_device_map(adapter_t *);
117 static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
118 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
119 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
120 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
121 struct scsi_cmnd *);
122 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
123 struct scsi_cmnd *);
125 static irqreturn_t megaraid_isr(int, void *);
127 static void megaraid_mbox_dpc(unsigned long);
129 static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
130 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
132 static int megaraid_cmm_register(adapter_t *);
133 static int megaraid_cmm_unregister(adapter_t *);
134 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
135 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
136 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
137 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
138 static int wait_till_fw_empty(adapter_t *);
142 MODULE_AUTHOR("megaraidlinux@lsi.com");
143 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
144 MODULE_LICENSE("GPL");
145 MODULE_VERSION(MEGARAID_VERSION);
148 * ### modules parameters for driver ###
152 * Set to enable driver to expose unconfigured disk to kernel
154 static int megaraid_expose_unconf_disks = 0;
155 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
156 MODULE_PARM_DESC(unconf_disks,
157 "Set to expose unconfigured disks to kernel (default=0)");
160 * driver wait time if the adapter's mailbox is busy
162 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
163 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
164 MODULE_PARM_DESC(busy_wait,
165 "Max wait for mailbox in microseconds if busy (default=10)");
168 * number of sectors per IO command
170 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
171 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
172 MODULE_PARM_DESC(max_sectors,
173 "Maximum number of sectors per IO command (default=128)");
176 * number of commands per logical unit
178 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
179 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
180 MODULE_PARM_DESC(cmd_per_lun,
181 "Maximum number of commands per logical unit (default=64)");
185 * Fast driver load option, skip scanning for physical devices during load.
186 * This would result in non-disk devices being skipped during driver load
187 * time. These can be later added though, using /proc/scsi/scsi
189 static unsigned int megaraid_fast_load = 0;
190 module_param_named(fast_load, megaraid_fast_load, int, 0);
191 MODULE_PARM_DESC(fast_load,
192 "Faster loading of the driver, skips physical devices! (default=0)");
196 * mraid_debug level - threshold for amount of information to be displayed by
197 * the driver. This level can be changed through modules parameters, ioctl or
198 * sysfs/proc interface. By default, print the announcement messages only.
200 int mraid_debug_level = CL_ANN;
201 module_param_named(debug_level, mraid_debug_level, int, 0);
202 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
205 * ### global data ###
207 static uint8_t megaraid_mbox_version[8] =
208 { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
212 * PCI table for all supported controllers.
214 static struct pci_device_id pci_id_table_g[] = {
216 PCI_VENDOR_ID_DELL,
217 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
218 PCI_VENDOR_ID_DELL,
219 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
222 PCI_VENDOR_ID_LSI_LOGIC,
223 PCI_DEVICE_ID_PERC4_SC,
224 PCI_VENDOR_ID_DELL,
225 PCI_SUBSYS_ID_PERC4_SC,
228 PCI_VENDOR_ID_LSI_LOGIC,
229 PCI_DEVICE_ID_PERC4_DC,
230 PCI_VENDOR_ID_DELL,
231 PCI_SUBSYS_ID_PERC4_DC,
234 PCI_VENDOR_ID_LSI_LOGIC,
235 PCI_DEVICE_ID_VERDE,
236 PCI_ANY_ID,
237 PCI_ANY_ID,
240 PCI_VENDOR_ID_DELL,
241 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
242 PCI_VENDOR_ID_DELL,
243 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
246 PCI_VENDOR_ID_DELL,
247 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
248 PCI_VENDOR_ID_DELL,
249 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
252 PCI_VENDOR_ID_DELL,
253 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
254 PCI_VENDOR_ID_DELL,
255 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
258 PCI_VENDOR_ID_DELL,
259 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
260 PCI_VENDOR_ID_DELL,
261 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
264 PCI_VENDOR_ID_DELL,
265 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
266 PCI_VENDOR_ID_DELL,
267 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
270 PCI_VENDOR_ID_DELL,
271 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
272 PCI_VENDOR_ID_DELL,
273 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
276 PCI_VENDOR_ID_LSI_LOGIC,
277 PCI_DEVICE_ID_DOBSON,
278 PCI_ANY_ID,
279 PCI_ANY_ID,
282 PCI_VENDOR_ID_AMI,
283 PCI_DEVICE_ID_AMI_MEGARAID3,
284 PCI_ANY_ID,
285 PCI_ANY_ID,
288 PCI_VENDOR_ID_LSI_LOGIC,
289 PCI_DEVICE_ID_AMI_MEGARAID3,
290 PCI_ANY_ID,
291 PCI_ANY_ID,
294 PCI_VENDOR_ID_LSI_LOGIC,
295 PCI_DEVICE_ID_LINDSAY,
296 PCI_ANY_ID,
297 PCI_ANY_ID,
299 {0} /* Terminating entry */
301 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
304 static struct pci_driver megaraid_pci_driver = {
305 .name = "megaraid",
306 .id_table = pci_id_table_g,
307 .probe = megaraid_probe_one,
308 .remove = megaraid_detach_one,
309 .shutdown = megaraid_mbox_shutdown,
314 // definitions for the device attributes for exporting logical drive number
315 // for a scsi address (Host, Channel, Id, Lun)
317 DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
318 NULL);
320 // Host template initializer for megaraid mbox sysfs device attributes
321 static struct device_attribute *megaraid_shost_attrs[] = {
322 &dev_attr_megaraid_mbox_app_hndl,
323 NULL,
327 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
329 // Host template initializer for megaraid mbox sysfs device attributes
330 static struct device_attribute *megaraid_sdev_attrs[] = {
331 &dev_attr_megaraid_mbox_ld,
332 NULL,
336 * Scsi host template for megaraid unified driver
338 static struct scsi_host_template megaraid_template_g = {
339 .module = THIS_MODULE,
340 .name = "LSI Logic MegaRAID driver",
341 .proc_name = "megaraid",
342 .queuecommand = megaraid_queue_command,
343 .eh_abort_handler = megaraid_abort_handler,
344 .eh_device_reset_handler = megaraid_reset_handler,
345 .eh_bus_reset_handler = megaraid_reset_handler,
346 .eh_host_reset_handler = megaraid_reset_handler,
347 .change_queue_depth = scsi_change_queue_depth,
348 .use_clustering = ENABLE_CLUSTERING,
349 .no_write_same = 1,
350 .sdev_attrs = megaraid_sdev_attrs,
351 .shost_attrs = megaraid_shost_attrs,
356 * megaraid_init - module load hook
358 * We register ourselves as hotplug enabled module and let PCI subsystem
359 * discover our adapters.
361 static int __init
362 megaraid_init(void)
364 int rval;
366 // Announce the driver version
367 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
368 MEGARAID_EXT_VERSION));
370 // check validity of module parameters
371 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
373 con_log(CL_ANN, (KERN_WARNING
374 "megaraid mailbox: max commands per lun reset to %d\n",
375 MBOX_MAX_SCSI_CMDS));
377 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
381 // register as a PCI hot-plug driver module
382 rval = pci_register_driver(&megaraid_pci_driver);
383 if (rval < 0) {
384 con_log(CL_ANN, (KERN_WARNING
385 "megaraid: could not register hotplug support.\n"));
388 return rval;
393 * megaraid_exit - driver unload entry point
395 * We simply unwrap the megaraid_init routine here.
397 static void __exit
398 megaraid_exit(void)
400 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
402 // unregister as PCI hotplug driver
403 pci_unregister_driver(&megaraid_pci_driver);
405 return;
410 * megaraid_probe_one - PCI hotplug entry point
411 * @pdev : handle to this controller's PCI configuration space
412 * @id : pci device id of the class of controllers
414 * This routine should be called whenever a new adapter is detected by the
415 * PCI hotplug susbsystem.
417 static int
418 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
420 adapter_t *adapter;
423 // detected a new controller
424 con_log(CL_ANN, (KERN_INFO
425 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
426 pdev->vendor, pdev->device, pdev->subsystem_vendor,
427 pdev->subsystem_device));
429 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
430 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
432 if (pci_enable_device(pdev)) {
433 con_log(CL_ANN, (KERN_WARNING
434 "megaraid: pci_enable_device failed\n"));
436 return -ENODEV;
439 // Enable bus-mastering on this controller
440 pci_set_master(pdev);
442 // Allocate the per driver initialization structure
443 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
445 if (adapter == NULL) {
446 con_log(CL_ANN, (KERN_WARNING
447 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
449 goto out_probe_one;
453 // set up PCI related soft state and other pre-known parameters
454 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
455 adapter->irq = pdev->irq;
456 adapter->pdev = pdev;
458 atomic_set(&adapter->being_detached, 0);
460 // Setup the default DMA mask. This would be changed later on
461 // depending on hardware capabilities
462 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
464 con_log(CL_ANN, (KERN_WARNING
465 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
467 goto out_free_adapter;
471 // Initialize the synchronization lock for kernel and LLD
472 spin_lock_init(&adapter->lock);
474 // Initialize the command queues: the list of free SCBs and the list
475 // of pending SCBs.
476 INIT_LIST_HEAD(&adapter->kscb_pool);
477 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
479 INIT_LIST_HEAD(&adapter->pend_list);
480 spin_lock_init(PENDING_LIST_LOCK(adapter));
482 INIT_LIST_HEAD(&adapter->completed_list);
483 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
486 // Start the mailbox based controller
487 if (megaraid_init_mbox(adapter) != 0) {
488 con_log(CL_ANN, (KERN_WARNING
489 "megaraid: maibox adapter did not initialize\n"));
491 goto out_free_adapter;
494 // Register with LSI Common Management Module
495 if (megaraid_cmm_register(adapter) != 0) {
497 con_log(CL_ANN, (KERN_WARNING
498 "megaraid: could not register with management module\n"));
500 goto out_fini_mbox;
503 // setup adapter handle in PCI soft state
504 pci_set_drvdata(pdev, adapter);
506 // attach with scsi mid-layer
507 if (megaraid_io_attach(adapter) != 0) {
509 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
511 goto out_cmm_unreg;
514 return 0;
516 out_cmm_unreg:
517 megaraid_cmm_unregister(adapter);
518 out_fini_mbox:
519 megaraid_fini_mbox(adapter);
520 out_free_adapter:
521 kfree(adapter);
522 out_probe_one:
523 pci_disable_device(pdev);
525 return -ENODEV;
530 * megaraid_detach_one - release framework resources and call LLD release routine
531 * @pdev : handle for our PCI configuration space
533 * This routine is called during driver unload. We free all the allocated
534 * resources and call the corresponding LLD so that it can also release all
535 * its resources.
537 * This routine is also called from the PCI hotplug system.
539 static void
540 megaraid_detach_one(struct pci_dev *pdev)
542 adapter_t *adapter;
543 struct Scsi_Host *host;
546 // Start a rollback on this adapter
547 adapter = pci_get_drvdata(pdev);
549 if (!adapter) {
550 con_log(CL_ANN, (KERN_CRIT
551 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
552 pdev->vendor, pdev->device, pdev->subsystem_vendor,
553 pdev->subsystem_device));
555 return;
557 else {
558 con_log(CL_ANN, (KERN_NOTICE
559 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
560 pdev->vendor, pdev->device, pdev->subsystem_vendor,
561 pdev->subsystem_device));
565 host = adapter->host;
567 // do not allow any more requests from the management module for this
568 // adapter.
569 // FIXME: How do we account for the request which might still be
570 // pending with us?
571 atomic_set(&adapter->being_detached, 1);
573 // detach from the IO sub-system
574 megaraid_io_detach(adapter);
576 // Unregister from common management module
578 // FIXME: this must return success or failure for conditions if there
579 // is a command pending with LLD or not.
580 megaraid_cmm_unregister(adapter);
582 // finalize the mailbox based controller and release all resources
583 megaraid_fini_mbox(adapter);
585 kfree(adapter);
587 scsi_host_put(host);
589 pci_disable_device(pdev);
591 return;
596 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
597 * @pdev : generic driver model device
599 * Shutdown notification, perform flush cache.
601 static void
602 megaraid_mbox_shutdown(struct pci_dev *pdev)
604 adapter_t *adapter = pci_get_drvdata(pdev);
605 static int counter;
607 if (!adapter) {
608 con_log(CL_ANN, (KERN_WARNING
609 "megaraid: null device in shutdown\n"));
610 return;
613 // flush caches now
614 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
615 counter++));
617 megaraid_mbox_flush_cache(adapter);
619 con_log(CL_ANN, ("done\n"));
624 * megaraid_io_attach - attach a device with the IO subsystem
625 * @adapter : controller's soft state
627 * Attach this device with the IO subsystem.
629 static int
630 megaraid_io_attach(adapter_t *adapter)
632 struct Scsi_Host *host;
634 // Initialize SCSI Host structure
635 host = scsi_host_alloc(&megaraid_template_g, 8);
636 if (!host) {
637 con_log(CL_ANN, (KERN_WARNING
638 "megaraid mbox: scsi_register failed\n"));
640 return -1;
643 SCSIHOST2ADAP(host) = (caddr_t)adapter;
644 adapter->host = host;
646 host->irq = adapter->irq;
647 host->unique_id = adapter->unique_id;
648 host->can_queue = adapter->max_cmds;
649 host->this_id = adapter->init_id;
650 host->sg_tablesize = adapter->sglen;
651 host->max_sectors = adapter->max_sectors;
652 host->cmd_per_lun = adapter->cmd_per_lun;
653 host->max_channel = adapter->max_channel;
654 host->max_id = adapter->max_target;
655 host->max_lun = adapter->max_lun;
658 // notify mid-layer about the new controller
659 if (scsi_add_host(host, &adapter->pdev->dev)) {
661 con_log(CL_ANN, (KERN_WARNING
662 "megaraid mbox: scsi_add_host failed\n"));
664 scsi_host_put(host);
666 return -1;
669 scsi_scan_host(host);
671 return 0;
676 * megaraid_io_detach - detach a device from the IO subsystem
677 * @adapter : controller's soft state
679 * Detach this device from the IO subsystem.
681 static void
682 megaraid_io_detach(adapter_t *adapter)
684 struct Scsi_Host *host;
686 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
688 host = adapter->host;
690 scsi_remove_host(host);
692 return;
697 * START: Mailbox Low Level Driver
699 * This is section specific to the single mailbox based controllers
703 * megaraid_init_mbox - initialize controller
704 * @adapter : our soft state
706 * - Allocate 16-byte aligned mailbox memory for firmware handshake
707 * - Allocate controller's memory resources
708 * - Find out all initialization data
709 * - Allocate memory required for all the commands
710 * - Use internal library of FW routines, build up complete soft state
712 static int
713 megaraid_init_mbox(adapter_t *adapter)
715 struct pci_dev *pdev;
716 mraid_device_t *raid_dev;
717 int i;
718 uint32_t magic64;
721 adapter->ito = MBOX_TIMEOUT;
722 pdev = adapter->pdev;
725 * Allocate and initialize the init data structure for mailbox
726 * controllers
728 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
729 if (raid_dev == NULL) return -1;
733 * Attach the adapter soft state to raid device soft state
735 adapter->raid_device = (caddr_t)raid_dev;
736 raid_dev->fast_load = megaraid_fast_load;
739 // our baseport
740 raid_dev->baseport = pci_resource_start(pdev, 0);
742 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
744 con_log(CL_ANN, (KERN_WARNING
745 "megaraid: mem region busy\n"));
747 goto out_free_raid_dev;
750 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
752 if (!raid_dev->baseaddr) {
754 con_log(CL_ANN, (KERN_WARNING
755 "megaraid: could not map hba memory\n") );
757 goto out_release_regions;
760 /* initialize the mutual exclusion lock for the mailbox */
761 spin_lock_init(&raid_dev->mailbox_lock);
763 /* allocate memory required for commands */
764 if (megaraid_alloc_cmd_packets(adapter) != 0)
765 goto out_iounmap;
768 * Issue SYNC cmd to flush the pending cmds in the adapter
769 * and initialize its internal state
772 if (megaraid_mbox_fire_sync_cmd(adapter))
773 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
776 * Setup the rest of the soft state using the library of
777 * FW routines
780 /* request IRQ and register the interrupt service routine */
781 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
782 adapter)) {
784 con_log(CL_ANN, (KERN_WARNING
785 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
786 goto out_alloc_cmds;
790 // Product info
791 if (megaraid_mbox_product_info(adapter) != 0)
792 goto out_free_irq;
794 // Do we support extended CDBs
795 adapter->max_cdb_sz = 10;
796 if (megaraid_mbox_extended_cdb(adapter) == 0) {
797 adapter->max_cdb_sz = 16;
801 * Do we support cluster environment, if we do, what is the initiator
802 * id.
803 * NOTE: In a non-cluster aware firmware environment, the LLD should
804 * return 7 as initiator id.
806 adapter->ha = 0;
807 adapter->init_id = -1;
808 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
809 adapter->ha = 1;
813 * Prepare the device ids array to have the mapping between the kernel
814 * device address and megaraid device address.
815 * We export the physical devices on their actual addresses. The
816 * logical drives are exported on a virtual SCSI channel
818 megaraid_mbox_setup_device_map(adapter);
820 // If the firmware supports random deletion, update the device id map
821 if (megaraid_mbox_support_random_del(adapter)) {
823 // Change the logical drives numbers in device_ids array one
824 // slot in device_ids is reserved for target id, that's why
825 // "<=" below
826 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
827 adapter->device_ids[adapter->max_channel][i] += 0x80;
829 adapter->device_ids[adapter->max_channel][adapter->init_id] =
830 0xFF;
832 raid_dev->random_del_supported = 1;
836 * find out the maximum number of scatter-gather elements supported by
837 * this firmware
839 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
841 // enumerate RAID and SCSI channels so that all devices on SCSI
842 // channels can later be exported, including disk devices
843 megaraid_mbox_enum_raid_scsi(adapter);
846 * Other parameters required by upper layer
848 * maximum number of sectors per IO command
850 adapter->max_sectors = megaraid_max_sectors;
853 * number of queued commands per LUN.
855 adapter->cmd_per_lun = megaraid_cmd_per_lun;
858 * Allocate resources required to issue FW calls, when sysfs is
859 * accessed
861 if (megaraid_sysfs_alloc_resources(adapter) != 0)
862 goto out_free_irq;
864 // Set the DMA mask to 64-bit. All supported controllers as capable of
865 // DMA in this range
866 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
868 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
869 ((adapter->pdev->subsystem_device !=
870 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
871 (adapter->pdev->subsystem_device !=
872 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
873 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
874 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
875 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
876 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
877 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
878 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
879 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
880 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
881 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
882 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
883 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
884 con_log(CL_ANN, (KERN_WARNING
885 "megaraid: DMA mask for 64-bit failed\n"));
887 if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
888 con_log(CL_ANN, (KERN_WARNING
889 "megaraid: 32-bit DMA mask failed\n"));
890 goto out_free_sysfs_res;
895 // setup tasklet for DPC
896 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
897 (unsigned long)adapter);
899 con_log(CL_DLEVEL1, (KERN_INFO
900 "megaraid mbox hba successfully initialized\n"));
902 return 0;
904 out_free_sysfs_res:
905 megaraid_sysfs_free_resources(adapter);
906 out_free_irq:
907 free_irq(adapter->irq, adapter);
908 out_alloc_cmds:
909 megaraid_free_cmd_packets(adapter);
910 out_iounmap:
911 iounmap(raid_dev->baseaddr);
912 out_release_regions:
913 pci_release_regions(pdev);
914 out_free_raid_dev:
915 kfree(raid_dev);
917 return -1;
922 * megaraid_fini_mbox - undo controller initialization
923 * @adapter : our soft state
925 static void
926 megaraid_fini_mbox(adapter_t *adapter)
928 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
930 // flush all caches
931 megaraid_mbox_flush_cache(adapter);
933 tasklet_kill(&adapter->dpc_h);
935 megaraid_sysfs_free_resources(adapter);
937 megaraid_free_cmd_packets(adapter);
939 free_irq(adapter->irq, adapter);
941 iounmap(raid_dev->baseaddr);
943 pci_release_regions(adapter->pdev);
945 kfree(raid_dev);
947 return;
952 * megaraid_alloc_cmd_packets - allocate shared mailbox
953 * @adapter : soft state of the raid controller
955 * Allocate and align the shared mailbox. This maibox is used to issue
956 * all the commands. For IO based controllers, the mailbox is also registered
957 * with the FW. Allocate memory for all commands as well.
958 * This is our big allocator.
960 static int
961 megaraid_alloc_cmd_packets(adapter_t *adapter)
963 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
964 struct pci_dev *pdev;
965 unsigned long align;
966 scb_t *scb;
967 mbox_ccb_t *ccb;
968 struct mraid_pci_blk *epthru_pci_blk;
969 struct mraid_pci_blk *sg_pci_blk;
970 struct mraid_pci_blk *mbox_pci_blk;
971 int i;
973 pdev = adapter->pdev;
976 * Setup the mailbox
977 * Allocate the common 16-byte aligned memory for the handshake
978 * mailbox.
980 raid_dev->una_mbox64 = pci_zalloc_consistent(adapter->pdev,
981 sizeof(mbox64_t),
982 &raid_dev->una_mbox64_dma);
984 if (!raid_dev->una_mbox64) {
985 con_log(CL_ANN, (KERN_WARNING
986 "megaraid: out of memory, %s %d\n", __func__,
987 __LINE__));
988 return -1;
992 * Align the mailbox at 16-byte boundary
994 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
996 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
997 (~0UL ^ 0xFUL));
999 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1001 align = ((void *)raid_dev->mbox -
1002 ((void *)&raid_dev->una_mbox64->mbox32));
1004 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1005 align;
1007 // Allocate memory for commands issued internally
1008 adapter->ibuf = pci_zalloc_consistent(pdev, MBOX_IBUF_SIZE,
1009 &adapter->ibuf_dma_h);
1010 if (!adapter->ibuf) {
1012 con_log(CL_ANN, (KERN_WARNING
1013 "megaraid: out of memory, %s %d\n", __func__,
1014 __LINE__));
1016 goto out_free_common_mbox;
1019 // Allocate memory for our SCSI Command Blocks and their associated
1020 // memory
1023 * Allocate memory for the base list of scb. Later allocate memory for
1024 * CCBs and embedded components of each CCB and point the pointers in
1025 * scb to the allocated components
1026 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1027 * since the calling routine does not yet know the number of available
1028 * commands.
1030 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1032 if (adapter->kscb_list == NULL) {
1033 con_log(CL_ANN, (KERN_WARNING
1034 "megaraid: out of memory, %s %d\n", __func__,
1035 __LINE__));
1036 goto out_free_ibuf;
1039 // memory allocation for our command packets
1040 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1041 con_log(CL_ANN, (KERN_WARNING
1042 "megaraid: out of memory, %s %d\n", __func__,
1043 __LINE__));
1044 goto out_free_scb_list;
1047 // Adjust the scb pointers and link in the free pool
1048 epthru_pci_blk = raid_dev->epthru_pool;
1049 sg_pci_blk = raid_dev->sg_pool;
1050 mbox_pci_blk = raid_dev->mbox_pool;
1052 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1053 scb = adapter->kscb_list + i;
1054 ccb = raid_dev->ccb_list + i;
1056 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1057 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1058 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1059 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1061 // make sure the mailbox is aligned properly
1062 if (ccb->mbox_dma_h & 0x0F) {
1063 con_log(CL_ANN, (KERN_CRIT
1064 "megaraid mbox: not aligned on 16-bytes\n"));
1066 goto out_teardown_dma_pools;
1069 ccb->epthru = (mraid_epassthru_t *)
1070 epthru_pci_blk[i].vaddr;
1071 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1072 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1073 ccb->pthru_dma_h = ccb->epthru_dma_h;
1076 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1077 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1078 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1080 scb->ccb = (caddr_t)ccb;
1081 scb->gp = 0;
1083 scb->sno = i; // command index
1085 scb->scp = NULL;
1086 scb->state = SCB_FREE;
1087 scb->dma_direction = PCI_DMA_NONE;
1088 scb->dma_type = MRAID_DMA_NONE;
1089 scb->dev_channel = -1;
1090 scb->dev_target = -1;
1092 // put scb in the free pool
1093 list_add_tail(&scb->list, &adapter->kscb_pool);
1096 return 0;
1098 out_teardown_dma_pools:
1099 megaraid_mbox_teardown_dma_pools(adapter);
1100 out_free_scb_list:
1101 kfree(adapter->kscb_list);
1102 out_free_ibuf:
1103 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1104 adapter->ibuf_dma_h);
1105 out_free_common_mbox:
1106 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1107 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1109 return -1;
1114 * megaraid_free_cmd_packets - free memory
1115 * @adapter : soft state of the raid controller
1117 * Release memory resources allocated for commands.
1119 static void
1120 megaraid_free_cmd_packets(adapter_t *adapter)
1122 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1124 megaraid_mbox_teardown_dma_pools(adapter);
1126 kfree(adapter->kscb_list);
1128 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1129 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1131 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1132 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1133 return;
1138 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1139 * @adapter : HBA soft state
1141 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1142 * and scatter-gather lists.
1144 static int
1145 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1147 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1148 struct mraid_pci_blk *epthru_pci_blk;
1149 struct mraid_pci_blk *sg_pci_blk;
1150 struct mraid_pci_blk *mbox_pci_blk;
1151 int i;
1155 // Allocate memory for 16-bytes aligned mailboxes
1156 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1157 adapter->pdev,
1158 sizeof(mbox64_t) + 16,
1159 16, 0);
1161 if (raid_dev->mbox_pool_handle == NULL) {
1162 goto fail_setup_dma_pool;
1165 mbox_pci_blk = raid_dev->mbox_pool;
1166 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1167 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1168 raid_dev->mbox_pool_handle,
1169 GFP_KERNEL,
1170 &mbox_pci_blk[i].dma_addr);
1171 if (!mbox_pci_blk[i].vaddr) {
1172 goto fail_setup_dma_pool;
1177 * Allocate memory for each embedded passthru strucuture pointer
1178 * Request for a 128 bytes aligned structure for each passthru command
1179 * structure
1180 * Since passthru and extended passthru commands are exclusive, they
1181 * share common memory pool. Passthru structures piggyback on memory
1182 * allocted to extended passthru since passthru is smaller of the two
1184 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1185 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1187 if (raid_dev->epthru_pool_handle == NULL) {
1188 goto fail_setup_dma_pool;
1191 epthru_pci_blk = raid_dev->epthru_pool;
1192 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1193 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1194 raid_dev->epthru_pool_handle,
1195 GFP_KERNEL,
1196 &epthru_pci_blk[i].dma_addr);
1197 if (!epthru_pci_blk[i].vaddr) {
1198 goto fail_setup_dma_pool;
1203 // Allocate memory for each scatter-gather list. Request for 512 bytes
1204 // alignment for each sg list
1205 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1206 adapter->pdev,
1207 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1208 512, 0);
1210 if (raid_dev->sg_pool_handle == NULL) {
1211 goto fail_setup_dma_pool;
1214 sg_pci_blk = raid_dev->sg_pool;
1215 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1216 sg_pci_blk[i].vaddr = pci_pool_alloc(
1217 raid_dev->sg_pool_handle,
1218 GFP_KERNEL,
1219 &sg_pci_blk[i].dma_addr);
1220 if (!sg_pci_blk[i].vaddr) {
1221 goto fail_setup_dma_pool;
1225 return 0;
1227 fail_setup_dma_pool:
1228 megaraid_mbox_teardown_dma_pools(adapter);
1229 return -1;
1234 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1235 * @adapter : HBA soft state
1237 * Teardown the dma pool for mailbox, passthru and extended passthru
1238 * structures, and scatter-gather lists.
1240 static void
1241 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1243 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1244 struct mraid_pci_blk *epthru_pci_blk;
1245 struct mraid_pci_blk *sg_pci_blk;
1246 struct mraid_pci_blk *mbox_pci_blk;
1247 int i;
1250 sg_pci_blk = raid_dev->sg_pool;
1251 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1252 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1253 sg_pci_blk[i].dma_addr);
1255 if (raid_dev->sg_pool_handle)
1256 pci_pool_destroy(raid_dev->sg_pool_handle);
1259 epthru_pci_blk = raid_dev->epthru_pool;
1260 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1261 pci_pool_free(raid_dev->epthru_pool_handle,
1262 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1264 if (raid_dev->epthru_pool_handle)
1265 pci_pool_destroy(raid_dev->epthru_pool_handle);
1268 mbox_pci_blk = raid_dev->mbox_pool;
1269 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1270 pci_pool_free(raid_dev->mbox_pool_handle,
1271 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1273 if (raid_dev->mbox_pool_handle)
1274 pci_pool_destroy(raid_dev->mbox_pool_handle);
1276 return;
1281 * megaraid_alloc_scb - detach and return a scb from the free list
1282 * @adapter : controller's soft state
1283 * @scp : pointer to the scsi command to be executed
1285 * Return the scb from the head of the free list. %NULL if there are none
1286 * available.
1288 static scb_t *
1289 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1291 struct list_head *head = &adapter->kscb_pool;
1292 scb_t *scb = NULL;
1293 unsigned long flags;
1295 // detach scb from free pool
1296 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1298 if (list_empty(head)) {
1299 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1300 return NULL;
1303 scb = list_entry(head->next, scb_t, list);
1304 list_del_init(&scb->list);
1306 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1308 scb->state = SCB_ACTIVE;
1309 scb->scp = scp;
1310 scb->dma_type = MRAID_DMA_NONE;
1312 return scb;
1317 * megaraid_dealloc_scb - return the scb to the free pool
1318 * @adapter : controller's soft state
1319 * @scb : scb to be freed
1321 * Return the scb back to the free list of scbs. The caller must 'flush' the
1322 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1323 * NOTE NOTE: Make sure the scb is not on any list before calling this
1324 * routine.
1326 static inline void
1327 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1329 unsigned long flags;
1331 // put scb in the free pool
1332 scb->state = SCB_FREE;
1333 scb->scp = NULL;
1334 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1336 list_add(&scb->list, &adapter->kscb_pool);
1338 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1340 return;
1345 * megaraid_mbox_mksgl - make the scatter-gather list
1346 * @adapter : controller's soft state
1347 * @scb : scsi control block
1349 * Prepare the scatter-gather list.
1351 static int
1352 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1354 struct scatterlist *sgl;
1355 mbox_ccb_t *ccb;
1356 struct scsi_cmnd *scp;
1357 int sgcnt;
1358 int i;
1361 scp = scb->scp;
1362 ccb = (mbox_ccb_t *)scb->ccb;
1364 sgcnt = scsi_dma_map(scp);
1365 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1367 // no mapping required if no data to be transferred
1368 if (!sgcnt)
1369 return 0;
1371 scb->dma_type = MRAID_DMA_WSG;
1373 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1374 ccb->sgl64[i].address = sg_dma_address(sgl);
1375 ccb->sgl64[i].length = sg_dma_len(sgl);
1378 // Return count of SG nodes
1379 return sgcnt;
1384 * mbox_post_cmd - issue a mailbox command
1385 * @adapter : controller's soft state
1386 * @scb : command to be issued
1388 * Post the command to the controller if mailbox is available.
1390 static int
1391 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1393 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1394 mbox64_t *mbox64;
1395 mbox_t *mbox;
1396 mbox_ccb_t *ccb;
1397 unsigned long flags;
1398 unsigned int i = 0;
1401 ccb = (mbox_ccb_t *)scb->ccb;
1402 mbox = raid_dev->mbox;
1403 mbox64 = raid_dev->mbox64;
1406 * Check for busy mailbox. If it is, return failure - the caller
1407 * should retry later.
1409 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1411 if (unlikely(mbox->busy)) {
1412 do {
1413 udelay(1);
1414 i++;
1415 rmb();
1416 } while(mbox->busy && (i < max_mbox_busy_wait));
1418 if (mbox->busy) {
1420 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1422 return -1;
1427 // Copy this command's mailbox data into "adapter's" mailbox
1428 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1429 mbox->cmdid = scb->sno;
1431 adapter->outstanding_cmds++;
1433 if (scb->dma_direction == PCI_DMA_TODEVICE)
1434 pci_dma_sync_sg_for_device(adapter->pdev,
1435 scsi_sglist(scb->scp),
1436 scsi_sg_count(scb->scp),
1437 PCI_DMA_TODEVICE);
1439 mbox->busy = 1; // Set busy
1440 mbox->poll = 0;
1441 mbox->ack = 0;
1442 wmb();
1444 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1446 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1448 return 0;
1453 * megaraid_queue_command - generic queue entry point for all LLDs
1454 * @scp : pointer to the scsi command to be executed
1455 * @done : callback routine to be called after the cmd has be completed
1457 * Queue entry point for mailbox based controllers.
1459 static int
1460 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1462 adapter_t *adapter;
1463 scb_t *scb;
1464 int if_busy;
1466 adapter = SCP2ADAPTER(scp);
1467 scp->scsi_done = done;
1468 scp->result = 0;
1471 * Allocate and build a SCB request
1472 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1473 * not allocate scb. We will return non-zero status in that case.
1474 * NOTE: scb can be null even though certain commands completed
1475 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1476 * return 0 in that case, and we would do the callback right away.
1478 if_busy = 0;
1479 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1480 if (!scb) { // command already completed
1481 done(scp);
1482 return 0;
1485 megaraid_mbox_runpendq(adapter, scb);
1486 return if_busy;
1489 static DEF_SCSI_QCMD(megaraid_queue_command)
1492 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1493 * @adapter : controller's soft state
1494 * @scp : mid-layer scsi command pointer
1495 * @busy : set if request could not be completed because of lack of
1496 * resources
1498 * Transform the mid-layer scsi command to megaraid firmware lingua.
1499 * Convert the command issued by mid-layer to format understood by megaraid
1500 * firmware. We also complete certain commands without sending them to firmware.
1502 static scb_t *
1503 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1505 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1506 int channel;
1507 int target;
1508 int islogical;
1509 mbox_ccb_t *ccb;
1510 mraid_passthru_t *pthru;
1511 mbox64_t *mbox64;
1512 mbox_t *mbox;
1513 scb_t *scb;
1514 char skip[] = "skipping";
1515 char scan[] = "scanning";
1516 char *ss;
1520 * Get the appropriate device map for the device this command is
1521 * intended for
1523 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1526 * Logical drive commands
1528 if (islogical) {
1529 switch (scp->cmnd[0]) {
1530 case TEST_UNIT_READY:
1532 * Do we support clustering and is the support enabled
1533 * If no, return success always
1535 if (!adapter->ha) {
1536 scp->result = (DID_OK << 16);
1537 return NULL;
1540 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1541 scp->result = (DID_ERROR << 16);
1542 *busy = 1;
1543 return NULL;
1546 scb->dma_direction = scp->sc_data_direction;
1547 scb->dev_channel = 0xFF;
1548 scb->dev_target = target;
1549 ccb = (mbox_ccb_t *)scb->ccb;
1552 * The command id will be provided by the command
1553 * issuance routine
1555 ccb->raw_mbox[0] = CLUSTER_CMD;
1556 ccb->raw_mbox[2] = RESERVATION_STATUS;
1557 ccb->raw_mbox[3] = target;
1559 return scb;
1561 case MODE_SENSE:
1563 struct scatterlist *sgl;
1564 caddr_t vaddr;
1566 sgl = scsi_sglist(scp);
1567 if (sg_page(sgl)) {
1568 vaddr = (caddr_t) sg_virt(&sgl[0]);
1570 memset(vaddr, 0, scp->cmnd[4]);
1572 else {
1573 con_log(CL_ANN, (KERN_WARNING
1574 "megaraid mailbox: invalid sg:%d\n",
1575 __LINE__));
1578 scp->result = (DID_OK << 16);
1579 return NULL;
1581 case INQUIRY:
1583 * Display the channel scan for logical drives
1584 * Do not display scan for a channel if already done.
1586 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1588 con_log(CL_ANN, (KERN_INFO
1589 "scsi[%d]: scanning scsi channel %d",
1590 adapter->host->host_no,
1591 SCP2CHANNEL(scp)));
1593 con_log(CL_ANN, (
1594 " [virtual] for logical drives\n"));
1596 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1599 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1600 scp->sense_buffer[0] = 0x70;
1601 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1602 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1603 scp->result = CHECK_CONDITION << 1;
1604 return NULL;
1607 /* Fall through */
1609 case READ_CAPACITY:
1611 * Do not allow LUN > 0 for logical drives and
1612 * requests for more than 40 logical drives
1614 if (SCP2LUN(scp)) {
1615 scp->result = (DID_BAD_TARGET << 16);
1616 return NULL;
1618 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1619 scp->result = (DID_BAD_TARGET << 16);
1620 return NULL;
1624 /* Allocate a SCB and initialize passthru */
1625 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1626 scp->result = (DID_ERROR << 16);
1627 *busy = 1;
1628 return NULL;
1631 ccb = (mbox_ccb_t *)scb->ccb;
1632 scb->dev_channel = 0xFF;
1633 scb->dev_target = target;
1634 pthru = ccb->pthru;
1635 mbox = ccb->mbox;
1636 mbox64 = ccb->mbox64;
1638 pthru->timeout = 0;
1639 pthru->ars = 1;
1640 pthru->reqsenselen = 14;
1641 pthru->islogical = 1;
1642 pthru->logdrv = target;
1643 pthru->cdblen = scp->cmd_len;
1644 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1646 mbox->cmd = MBOXCMD_PASSTHRU64;
1647 scb->dma_direction = scp->sc_data_direction;
1649 pthru->dataxferlen = scsi_bufflen(scp);
1650 pthru->dataxferaddr = ccb->sgl_dma_h;
1651 pthru->numsge = megaraid_mbox_mksgl(adapter,
1652 scb);
1654 mbox->xferaddr = 0xFFFFFFFF;
1655 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1656 mbox64->xferaddr_hi = 0;
1658 return scb;
1660 case READ_6:
1661 case WRITE_6:
1662 case READ_10:
1663 case WRITE_10:
1664 case READ_12:
1665 case WRITE_12:
1668 * Allocate a SCB and initialize mailbox
1670 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1671 scp->result = (DID_ERROR << 16);
1672 *busy = 1;
1673 return NULL;
1675 ccb = (mbox_ccb_t *)scb->ccb;
1676 scb->dev_channel = 0xFF;
1677 scb->dev_target = target;
1678 mbox = ccb->mbox;
1679 mbox64 = ccb->mbox64;
1680 mbox->logdrv = target;
1683 * A little HACK: 2nd bit is zero for all scsi read
1684 * commands and is set for all scsi write commands
1686 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1687 MBOXCMD_LREAD64 ;
1690 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1692 if (scp->cmd_len == 6) {
1693 mbox->numsectors = (uint32_t)scp->cmnd[4];
1694 mbox->lba =
1695 ((uint32_t)scp->cmnd[1] << 16) |
1696 ((uint32_t)scp->cmnd[2] << 8) |
1697 (uint32_t)scp->cmnd[3];
1699 mbox->lba &= 0x1FFFFF;
1703 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1705 else if (scp->cmd_len == 10) {
1706 mbox->numsectors =
1707 (uint32_t)scp->cmnd[8] |
1708 ((uint32_t)scp->cmnd[7] << 8);
1709 mbox->lba =
1710 ((uint32_t)scp->cmnd[2] << 24) |
1711 ((uint32_t)scp->cmnd[3] << 16) |
1712 ((uint32_t)scp->cmnd[4] << 8) |
1713 (uint32_t)scp->cmnd[5];
1717 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1719 else if (scp->cmd_len == 12) {
1720 mbox->lba =
1721 ((uint32_t)scp->cmnd[2] << 24) |
1722 ((uint32_t)scp->cmnd[3] << 16) |
1723 ((uint32_t)scp->cmnd[4] << 8) |
1724 (uint32_t)scp->cmnd[5];
1726 mbox->numsectors =
1727 ((uint32_t)scp->cmnd[6] << 24) |
1728 ((uint32_t)scp->cmnd[7] << 16) |
1729 ((uint32_t)scp->cmnd[8] << 8) |
1730 (uint32_t)scp->cmnd[9];
1732 else {
1733 con_log(CL_ANN, (KERN_WARNING
1734 "megaraid: unsupported CDB length\n"));
1736 megaraid_dealloc_scb(adapter, scb);
1738 scp->result = (DID_ERROR << 16);
1739 return NULL;
1742 scb->dma_direction = scp->sc_data_direction;
1744 // Calculate Scatter-Gather info
1745 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1746 mbox->numsge = megaraid_mbox_mksgl(adapter,
1747 scb);
1748 mbox->xferaddr = 0xFFFFFFFF;
1749 mbox64->xferaddr_hi = 0;
1751 return scb;
1753 case RESERVE:
1754 case RELEASE:
1756 * Do we support clustering and is the support enabled
1758 if (!adapter->ha) {
1759 scp->result = (DID_BAD_TARGET << 16);
1760 return NULL;
1764 * Allocate a SCB and initialize mailbox
1766 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1767 scp->result = (DID_ERROR << 16);
1768 *busy = 1;
1769 return NULL;
1772 ccb = (mbox_ccb_t *)scb->ccb;
1773 scb->dev_channel = 0xFF;
1774 scb->dev_target = target;
1775 ccb->raw_mbox[0] = CLUSTER_CMD;
1776 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1777 RESERVE_LD : RELEASE_LD;
1779 ccb->raw_mbox[3] = target;
1780 scb->dma_direction = scp->sc_data_direction;
1782 return scb;
1784 default:
1785 scp->result = (DID_BAD_TARGET << 16);
1786 return NULL;
1789 else { // Passthru device commands
1791 // Do not allow access to target id > 15 or LUN > 7
1792 if (target > 15 || SCP2LUN(scp) > 7) {
1793 scp->result = (DID_BAD_TARGET << 16);
1794 return NULL;
1797 // if fast load option was set and scan for last device is
1798 // over, reset the fast_load flag so that during a possible
1799 // next scan, devices can be made available
1800 if (rdev->fast_load && (target == 15) &&
1801 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1803 con_log(CL_ANN, (KERN_INFO
1804 "megaraid[%d]: physical device scan re-enabled\n",
1805 adapter->host->host_no));
1806 rdev->fast_load = 0;
1810 * Display the channel scan for physical devices
1812 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1814 ss = rdev->fast_load ? skip : scan;
1816 con_log(CL_ANN, (KERN_INFO
1817 "scsi[%d]: %s scsi channel %d [Phy %d]",
1818 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1819 channel));
1821 con_log(CL_ANN, (
1822 " for non-raid devices\n"));
1824 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1827 // disable channel sweep if fast load option given
1828 if (rdev->fast_load) {
1829 scp->result = (DID_BAD_TARGET << 16);
1830 return NULL;
1833 // Allocate a SCB and initialize passthru
1834 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1835 scp->result = (DID_ERROR << 16);
1836 *busy = 1;
1837 return NULL;
1840 ccb = (mbox_ccb_t *)scb->ccb;
1841 scb->dev_channel = channel;
1842 scb->dev_target = target;
1843 scb->dma_direction = scp->sc_data_direction;
1844 mbox = ccb->mbox;
1845 mbox64 = ccb->mbox64;
1847 // Does this firmware support extended CDBs
1848 if (adapter->max_cdb_sz == 16) {
1849 mbox->cmd = MBOXCMD_EXTPTHRU;
1851 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1853 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1854 mbox64->xferaddr_hi = 0;
1855 mbox->xferaddr = 0xFFFFFFFF;
1857 else {
1858 mbox->cmd = MBOXCMD_PASSTHRU64;
1860 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1862 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1863 mbox64->xferaddr_hi = 0;
1864 mbox->xferaddr = 0xFFFFFFFF;
1866 return scb;
1869 // NOT REACHED
1874 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1875 * @adapter : controller's soft state
1876 * @scb_q : SCB to be queued in the pending list
1878 * Scan the pending list for commands which are not yet issued and try to
1879 * post to the controller. The SCB can be a null pointer, which would indicate
1880 * no SCB to be queue, just try to execute the ones in the pending list.
1882 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1883 * out from the head of the pending list. If it is successfully issued, the
1884 * next SCB is at the head now.
1886 static void
1887 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1889 scb_t *scb;
1890 unsigned long flags;
1892 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1894 if (scb_q) {
1895 scb_q->state = SCB_PENDQ;
1896 list_add_tail(&scb_q->list, &adapter->pend_list);
1899 // if the adapter in not in quiescent mode, post the commands to FW
1900 if (adapter->quiescent) {
1901 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1902 return;
1905 while (!list_empty(&adapter->pend_list)) {
1907 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1909 scb = list_entry(adapter->pend_list.next, scb_t, list);
1911 // remove the scb from the pending list and try to
1912 // issue. If we are unable to issue it, put back in
1913 // the pending list and return
1915 list_del_init(&scb->list);
1917 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1919 // if mailbox was busy, return SCB back to pending
1920 // list. Make sure to add at the head, since that's
1921 // where it would have been removed from
1923 scb->state = SCB_ISSUED;
1925 if (mbox_post_cmd(adapter, scb) != 0) {
1927 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1929 scb->state = SCB_PENDQ;
1931 list_add(&scb->list, &adapter->pend_list);
1933 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1934 flags);
1936 return;
1939 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1942 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1945 return;
1950 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1951 * @adapter : pointer to controller's soft state
1952 * @scb : scsi control block
1953 * @scp : scsi command from the mid-layer
1955 * Prepare a command for the scsi physical devices.
1957 static void
1958 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1959 struct scsi_cmnd *scp)
1961 mbox_ccb_t *ccb;
1962 mraid_passthru_t *pthru;
1963 uint8_t channel;
1964 uint8_t target;
1966 ccb = (mbox_ccb_t *)scb->ccb;
1967 pthru = ccb->pthru;
1968 channel = scb->dev_channel;
1969 target = scb->dev_target;
1971 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1972 pthru->timeout = 4;
1973 pthru->ars = 1;
1974 pthru->islogical = 0;
1975 pthru->channel = 0;
1976 pthru->target = (channel << 4) | target;
1977 pthru->logdrv = SCP2LUN(scp);
1978 pthru->reqsenselen = 14;
1979 pthru->cdblen = scp->cmd_len;
1981 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1983 if (scsi_bufflen(scp)) {
1984 pthru->dataxferlen = scsi_bufflen(scp);
1985 pthru->dataxferaddr = ccb->sgl_dma_h;
1986 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1988 else {
1989 pthru->dataxferaddr = 0;
1990 pthru->dataxferlen = 0;
1991 pthru->numsge = 0;
1993 return;
1998 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1999 * @adapter : pointer to controller's soft state
2000 * @scb : scsi control block
2001 * @scp : scsi command from the mid-layer
2003 * Prepare a command for the scsi physical devices. This routine prepares
2004 * commands for devices which can take extended CDBs (>10 bytes).
2006 static void
2007 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2008 struct scsi_cmnd *scp)
2010 mbox_ccb_t *ccb;
2011 mraid_epassthru_t *epthru;
2012 uint8_t channel;
2013 uint8_t target;
2015 ccb = (mbox_ccb_t *)scb->ccb;
2016 epthru = ccb->epthru;
2017 channel = scb->dev_channel;
2018 target = scb->dev_target;
2020 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2021 epthru->timeout = 4;
2022 epthru->ars = 1;
2023 epthru->islogical = 0;
2024 epthru->channel = 0;
2025 epthru->target = (channel << 4) | target;
2026 epthru->logdrv = SCP2LUN(scp);
2027 epthru->reqsenselen = 14;
2028 epthru->cdblen = scp->cmd_len;
2030 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2032 if (scsi_bufflen(scp)) {
2033 epthru->dataxferlen = scsi_bufflen(scp);
2034 epthru->dataxferaddr = ccb->sgl_dma_h;
2035 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2037 else {
2038 epthru->dataxferaddr = 0;
2039 epthru->dataxferlen = 0;
2040 epthru->numsge = 0;
2042 return;
2047 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2048 * @adapter : controller's soft state
2050 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2051 * completed command and put them on the completed list for later processing.
2053 * Returns: 1 if the interrupt is valid, 0 otherwise
2055 static int
2056 megaraid_ack_sequence(adapter_t *adapter)
2058 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2059 mbox_t *mbox;
2060 scb_t *scb;
2061 uint8_t nstatus;
2062 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2063 struct list_head clist;
2064 int handled;
2065 uint32_t dword;
2066 unsigned long flags;
2067 int i, j;
2070 mbox = raid_dev->mbox;
2072 // move the SCBs from the firmware completed array to our local list
2073 INIT_LIST_HEAD(&clist);
2075 // loop till F/W has more commands for us to complete
2076 handled = 0;
2077 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2078 do {
2080 * Check if a valid interrupt is pending. If found, force the
2081 * interrupt line low.
2083 dword = RDOUTDOOR(raid_dev);
2084 if (dword != 0x10001234) break;
2086 handled = 1;
2088 WROUTDOOR(raid_dev, 0x10001234);
2090 nstatus = 0;
2091 // wait for valid numstatus to post
2092 for (i = 0; i < 0xFFFFF; i++) {
2093 if (mbox->numstatus != 0xFF) {
2094 nstatus = mbox->numstatus;
2095 break;
2097 rmb();
2099 mbox->numstatus = 0xFF;
2101 adapter->outstanding_cmds -= nstatus;
2103 for (i = 0; i < nstatus; i++) {
2105 // wait for valid command index to post
2106 for (j = 0; j < 0xFFFFF; j++) {
2107 if (mbox->completed[i] != 0xFF) break;
2108 rmb();
2110 completed[i] = mbox->completed[i];
2111 mbox->completed[i] = 0xFF;
2113 if (completed[i] == 0xFF) {
2114 con_log(CL_ANN, (KERN_CRIT
2115 "megaraid: command posting timed out\n"));
2117 BUG();
2118 continue;
2121 // Get SCB associated with this command id
2122 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2123 // a cmm command
2124 scb = adapter->uscb_list + (completed[i] -
2125 MBOX_MAX_SCSI_CMDS);
2127 else {
2128 // an os command
2129 scb = adapter->kscb_list + completed[i];
2132 scb->status = mbox->status;
2133 list_add_tail(&scb->list, &clist);
2136 // Acknowledge interrupt
2137 WRINDOOR(raid_dev, 0x02);
2139 } while(1);
2141 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2144 // put the completed commands in the completed list. DPC would
2145 // complete these commands later
2146 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2148 list_splice(&clist, &adapter->completed_list);
2150 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2153 // schedule the DPC if there is some work for it
2154 if (handled)
2155 tasklet_schedule(&adapter->dpc_h);
2157 return handled;
2162 * megaraid_isr - isr for memory based mailbox based controllers
2163 * @irq : irq
2164 * @devp : pointer to our soft state
2166 * Interrupt service routine for memory-mapped mailbox controllers.
2168 static irqreturn_t
2169 megaraid_isr(int irq, void *devp)
2171 adapter_t *adapter = devp;
2172 int handled;
2174 handled = megaraid_ack_sequence(adapter);
2176 /* Loop through any pending requests */
2177 if (!adapter->quiescent) {
2178 megaraid_mbox_runpendq(adapter, NULL);
2181 return IRQ_RETVAL(handled);
2186 * megaraid_mbox_sync_scb - sync kernel buffers
2187 * @adapter : controller's soft state
2188 * @scb : pointer to the resource packet
2190 * DMA sync if required.
2192 static void
2193 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2195 mbox_ccb_t *ccb;
2197 ccb = (mbox_ccb_t *)scb->ccb;
2199 if (scb->dma_direction == PCI_DMA_FROMDEVICE)
2200 pci_dma_sync_sg_for_cpu(adapter->pdev,
2201 scsi_sglist(scb->scp),
2202 scsi_sg_count(scb->scp),
2203 PCI_DMA_FROMDEVICE);
2205 scsi_dma_unmap(scb->scp);
2206 return;
2211 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2212 * @devp : pointer to HBA soft state
2214 * Pick up the commands from the completed list and send back to the owners.
2215 * This is a reentrant function and does not assume any locks are held while
2216 * it is being called.
2218 static void
2219 megaraid_mbox_dpc(unsigned long devp)
2221 adapter_t *adapter = (adapter_t *)devp;
2222 mraid_device_t *raid_dev;
2223 struct list_head clist;
2224 struct scatterlist *sgl;
2225 scb_t *scb;
2226 scb_t *tmp;
2227 struct scsi_cmnd *scp;
2228 mraid_passthru_t *pthru;
2229 mraid_epassthru_t *epthru;
2230 mbox_ccb_t *ccb;
2231 int islogical;
2232 int pdev_index;
2233 int pdev_state;
2234 mbox_t *mbox;
2235 unsigned long flags;
2236 uint8_t c;
2237 int status;
2238 uioc_t *kioc;
2241 if (!adapter) return;
2243 raid_dev = ADAP2RAIDDEV(adapter);
2245 // move the SCBs from the completed list to our local list
2246 INIT_LIST_HEAD(&clist);
2248 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2250 list_splice_init(&adapter->completed_list, &clist);
2252 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2255 list_for_each_entry_safe(scb, tmp, &clist, list) {
2257 status = scb->status;
2258 scp = scb->scp;
2259 ccb = (mbox_ccb_t *)scb->ccb;
2260 pthru = ccb->pthru;
2261 epthru = ccb->epthru;
2262 mbox = ccb->mbox;
2264 // Make sure f/w has completed a valid command
2265 if (scb->state != SCB_ISSUED) {
2266 con_log(CL_ANN, (KERN_CRIT
2267 "megaraid critical err: invalid command %d:%d:%p\n",
2268 scb->sno, scb->state, scp));
2269 BUG();
2270 continue; // Must never happen!
2273 // check for the management command and complete it right away
2274 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2275 scb->state = SCB_FREE;
2276 scb->status = status;
2278 // remove from local clist
2279 list_del_init(&scb->list);
2281 kioc = (uioc_t *)scb->gp;
2282 kioc->status = 0;
2284 megaraid_mbox_mm_done(adapter, scb);
2286 continue;
2289 // Was an abort issued for this command earlier
2290 if (scb->state & SCB_ABORT) {
2291 con_log(CL_ANN, (KERN_NOTICE
2292 "megaraid: aborted cmd [%x] completed\n",
2293 scb->sno));
2297 * If the inquiry came of a disk drive which is not part of
2298 * any RAID array, expose it to the kernel. For this to be
2299 * enabled, user must set the "megaraid_expose_unconf_disks"
2300 * flag to 1 by specifying it on module parameter list.
2301 * This would enable data migration off drives from other
2302 * configurations.
2304 islogical = MRAID_IS_LOGICAL(adapter, scp);
2305 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2306 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2308 sgl = scsi_sglist(scp);
2309 if (sg_page(sgl)) {
2310 c = *(unsigned char *) sg_virt(&sgl[0]);
2311 } else {
2312 con_log(CL_ANN, (KERN_WARNING
2313 "megaraid mailbox: invalid sg:%d\n",
2314 __LINE__));
2315 c = 0;
2318 if ((c & 0x1F ) == TYPE_DISK) {
2319 pdev_index = (scb->dev_channel * 16) +
2320 scb->dev_target;
2321 pdev_state =
2322 raid_dev->pdrv_state[pdev_index] & 0x0F;
2324 if (pdev_state == PDRV_ONLINE ||
2325 pdev_state == PDRV_FAILED ||
2326 pdev_state == PDRV_RBLD ||
2327 pdev_state == PDRV_HOTSPARE ||
2328 megaraid_expose_unconf_disks == 0) {
2330 status = 0xF0;
2335 // Convert MegaRAID status to Linux error code
2336 switch (status) {
2338 case 0x00:
2340 scp->result = (DID_OK << 16);
2341 break;
2343 case 0x02:
2345 /* set sense_buffer and result fields */
2346 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2347 mbox->cmd == MBOXCMD_PASSTHRU64) {
2349 memcpy(scp->sense_buffer, pthru->reqsensearea,
2350 14);
2352 scp->result = DRIVER_SENSE << 24 |
2353 DID_OK << 16 | CHECK_CONDITION << 1;
2355 else {
2356 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2358 memcpy(scp->sense_buffer,
2359 epthru->reqsensearea, 14);
2361 scp->result = DRIVER_SENSE << 24 |
2362 DID_OK << 16 |
2363 CHECK_CONDITION << 1;
2364 } else {
2365 scp->sense_buffer[0] = 0x70;
2366 scp->sense_buffer[2] = ABORTED_COMMAND;
2367 scp->result = CHECK_CONDITION << 1;
2370 break;
2372 case 0x08:
2374 scp->result = DID_BUS_BUSY << 16 | status;
2375 break;
2377 default:
2380 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2381 * failed
2383 if (scp->cmnd[0] == TEST_UNIT_READY) {
2384 scp->result = DID_ERROR << 16 |
2385 RESERVATION_CONFLICT << 1;
2387 else
2389 * Error code returned is 1 if Reserve or Release
2390 * failed or the input parameter is invalid
2392 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2393 scp->cmnd[0] == RELEASE)) {
2395 scp->result = DID_ERROR << 16 |
2396 RESERVATION_CONFLICT << 1;
2398 else {
2399 scp->result = DID_BAD_TARGET << 16 | status;
2403 // print a debug message for all failed commands
2404 if (status) {
2405 megaraid_mbox_display_scb(adapter, scb);
2408 // Free our internal resources and call the mid-layer callback
2409 // routine
2410 megaraid_mbox_sync_scb(adapter, scb);
2412 // remove from local clist
2413 list_del_init(&scb->list);
2415 // put back in free list
2416 megaraid_dealloc_scb(adapter, scb);
2418 // send the scsi packet back to kernel
2419 scp->scsi_done(scp);
2422 return;
2427 * megaraid_abort_handler - abort the scsi command
2428 * @scp : command to be aborted
2430 * Abort a previous SCSI request. Only commands on the pending list can be
2431 * aborted. All the commands issued to the F/W must complete.
2433 static int
2434 megaraid_abort_handler(struct scsi_cmnd *scp)
2436 adapter_t *adapter;
2437 mraid_device_t *raid_dev;
2438 scb_t *scb;
2439 scb_t *tmp;
2440 int found;
2441 unsigned long flags;
2442 int i;
2445 adapter = SCP2ADAPTER(scp);
2446 raid_dev = ADAP2RAIDDEV(adapter);
2448 con_log(CL_ANN, (KERN_WARNING
2449 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2450 scp->cmnd[0], SCP2CHANNEL(scp),
2451 SCP2TARGET(scp), SCP2LUN(scp)));
2453 // If FW has stopped responding, simply return failure
2454 if (raid_dev->hw_error) {
2455 con_log(CL_ANN, (KERN_NOTICE
2456 "megaraid: hw error, not aborting\n"));
2457 return FAILED;
2460 // There might a race here, where the command was completed by the
2461 // firmware and now it is on the completed list. Before we could
2462 // complete the command to the kernel in dpc, the abort came.
2463 // Find out if this is the case to avoid the race.
2464 scb = NULL;
2465 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2466 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2468 if (scb->scp == scp) { // Found command
2470 list_del_init(&scb->list); // from completed list
2472 con_log(CL_ANN, (KERN_WARNING
2473 "megaraid: %d[%d:%d], abort from completed list\n",
2474 scb->sno, scb->dev_channel, scb->dev_target));
2476 scp->result = (DID_ABORT << 16);
2477 scp->scsi_done(scp);
2479 megaraid_dealloc_scb(adapter, scb);
2481 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2482 flags);
2484 return SUCCESS;
2487 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2490 // Find out if this command is still on the pending list. If it is and
2491 // was never issued, abort and return success. If the command is owned
2492 // by the firmware, we must wait for it to complete by the FW.
2493 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2494 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2496 if (scb->scp == scp) { // Found command
2498 list_del_init(&scb->list); // from pending list
2500 ASSERT(!(scb->state & SCB_ISSUED));
2502 con_log(CL_ANN, (KERN_WARNING
2503 "megaraid abort: [%d:%d], driver owner\n",
2504 scb->dev_channel, scb->dev_target));
2506 scp->result = (DID_ABORT << 16);
2507 scp->scsi_done(scp);
2509 megaraid_dealloc_scb(adapter, scb);
2511 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2512 flags);
2514 return SUCCESS;
2517 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2520 // Check do we even own this command, in which case this would be
2521 // owned by the firmware. The only way to locate the FW scb is to
2522 // traverse through the list of all SCB, since driver does not
2523 // maintain these SCBs on any list
2524 found = 0;
2525 spin_lock_irq(&adapter->lock);
2526 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2527 scb = adapter->kscb_list + i;
2529 if (scb->scp == scp) {
2531 found = 1;
2533 if (!(scb->state & SCB_ISSUED)) {
2534 con_log(CL_ANN, (KERN_WARNING
2535 "megaraid abort: %d[%d:%d], invalid state\n",
2536 scb->sno, scb->dev_channel, scb->dev_target));
2537 BUG();
2539 else {
2540 con_log(CL_ANN, (KERN_WARNING
2541 "megaraid abort: %d[%d:%d], fw owner\n",
2542 scb->sno, scb->dev_channel, scb->dev_target));
2546 spin_unlock_irq(&adapter->lock);
2548 if (!found) {
2549 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2551 // FIXME: Should there be a callback for this command?
2552 return SUCCESS;
2555 // We cannot actually abort a command owned by firmware, return
2556 // failure and wait for reset. In host reset handler, we will find out
2557 // if the HBA is still live
2558 return FAILED;
2562 * megaraid_reset_handler - device reset handler for mailbox based driver
2563 * @scp : reference command
2565 * Reset handler for the mailbox based controller. First try to find out if
2566 * the FW is still live, in which case the outstanding commands counter mut go
2567 * down to 0. If that happens, also issue the reservation reset command to
2568 * relinquish (possible) reservations on the logical drives connected to this
2569 * host.
2571 static int
2572 megaraid_reset_handler(struct scsi_cmnd *scp)
2574 adapter_t *adapter;
2575 scb_t *scb;
2576 scb_t *tmp;
2577 mraid_device_t *raid_dev;
2578 unsigned long flags;
2579 uint8_t raw_mbox[sizeof(mbox_t)];
2580 int rval;
2581 int recovery_window;
2582 int recovering;
2583 int i;
2584 uioc_t *kioc;
2586 adapter = SCP2ADAPTER(scp);
2587 raid_dev = ADAP2RAIDDEV(adapter);
2589 // return failure if adapter is not responding
2590 if (raid_dev->hw_error) {
2591 con_log(CL_ANN, (KERN_NOTICE
2592 "megaraid: hw error, cannot reset\n"));
2593 return FAILED;
2597 // Under exceptional conditions, FW can take up to 3 minutes to
2598 // complete command processing. Wait for additional 2 minutes for the
2599 // pending commands counter to go down to 0. If it doesn't, let the
2600 // controller be marked offline
2601 // Also, reset all the commands currently owned by the driver
2602 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2603 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2604 list_del_init(&scb->list); // from pending list
2606 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2607 con_log(CL_ANN, (KERN_WARNING
2608 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2609 scb->sno, scb->dev_channel, scb->dev_target));
2611 scb->status = -1;
2613 kioc = (uioc_t *)scb->gp;
2614 kioc->status = -EFAULT;
2616 megaraid_mbox_mm_done(adapter, scb);
2617 } else {
2618 if (scb->scp == scp) { // Found command
2619 con_log(CL_ANN, (KERN_WARNING
2620 "megaraid: %d[%d:%d], reset from pending list\n",
2621 scb->sno, scb->dev_channel, scb->dev_target));
2622 } else {
2623 con_log(CL_ANN, (KERN_WARNING
2624 "megaraid: IO packet with %d[%d:%d] being reset\n",
2625 scb->sno, scb->dev_channel, scb->dev_target));
2628 scb->scp->result = (DID_RESET << 16);
2629 scb->scp->scsi_done(scb->scp);
2631 megaraid_dealloc_scb(adapter, scb);
2634 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2636 if (adapter->outstanding_cmds) {
2637 con_log(CL_ANN, (KERN_NOTICE
2638 "megaraid: %d outstanding commands. Max wait %d sec\n",
2639 adapter->outstanding_cmds,
2640 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2643 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2645 recovering = adapter->outstanding_cmds;
2647 for (i = 0; i < recovery_window; i++) {
2649 megaraid_ack_sequence(adapter);
2651 // print a message once every 5 seconds only
2652 if (!(i % 5)) {
2653 con_log(CL_ANN, (
2654 "megaraid mbox: Wait for %d commands to complete:%d\n",
2655 adapter->outstanding_cmds,
2656 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2659 // bailout if no recovery happened in reset time
2660 if (adapter->outstanding_cmds == 0) {
2661 break;
2664 msleep(1000);
2667 spin_lock(&adapter->lock);
2669 // If still outstanding commands, bail out
2670 if (adapter->outstanding_cmds) {
2671 con_log(CL_ANN, (KERN_WARNING
2672 "megaraid mbox: critical hardware error!\n"));
2674 raid_dev->hw_error = 1;
2676 rval = FAILED;
2677 goto out;
2679 else {
2680 con_log(CL_ANN, (KERN_NOTICE
2681 "megaraid mbox: reset sequence completed successfully\n"));
2685 // If the controller supports clustering, reset reservations
2686 if (!adapter->ha) {
2687 rval = SUCCESS;
2688 goto out;
2691 // clear reservations if any
2692 raw_mbox[0] = CLUSTER_CMD;
2693 raw_mbox[2] = RESET_RESERVATIONS;
2695 rval = SUCCESS;
2696 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2697 con_log(CL_ANN,
2698 (KERN_INFO "megaraid: reservation reset\n"));
2700 else {
2701 rval = FAILED;
2702 con_log(CL_ANN, (KERN_WARNING
2703 "megaraid: reservation reset failed\n"));
2706 out:
2707 spin_unlock(&adapter->lock);
2708 return rval;
2712 * START: internal commands library
2714 * This section of the driver has the common routine used by the driver and
2715 * also has all the FW routines
2719 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2720 * @adapter : controller's soft state
2721 * @raw_mbox : the mailbox
2723 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2724 * controllers.
2726 static int
2727 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2729 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2730 mbox64_t *mbox64;
2731 mbox_t *mbox;
2732 uint8_t status;
2733 int i;
2736 mbox64 = raid_dev->mbox64;
2737 mbox = raid_dev->mbox;
2740 * Wait until mailbox is free
2742 if (megaraid_busywait_mbox(raid_dev) != 0)
2743 goto blocked_mailbox;
2746 * Copy mailbox data into host structure
2748 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2749 mbox->cmdid = 0xFE;
2750 mbox->busy = 1;
2751 mbox->poll = 0;
2752 mbox->ack = 0;
2753 mbox->numstatus = 0xFF;
2754 mbox->status = 0xFF;
2756 wmb();
2757 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2759 // wait for maximum 1 second for status to post. If the status is not
2760 // available within 1 second, assume FW is initializing and wait
2761 // for an extended amount of time
2762 if (mbox->numstatus == 0xFF) { // status not yet available
2763 udelay(25);
2765 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2766 rmb();
2767 msleep(1);
2771 if (i == 1000) {
2772 con_log(CL_ANN, (KERN_NOTICE
2773 "megaraid mailbox: wait for FW to boot "));
2775 for (i = 0; (mbox->numstatus == 0xFF) &&
2776 (i < MBOX_RESET_WAIT); i++) {
2777 rmb();
2778 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2779 MBOX_RESET_WAIT - i));
2780 msleep(1000);
2783 if (i == MBOX_RESET_WAIT) {
2785 con_log(CL_ANN, (
2786 "\nmegaraid mailbox: status not available\n"));
2788 return -1;
2790 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2794 // wait for maximum 1 second for poll semaphore
2795 if (mbox->poll != 0x77) {
2796 udelay(25);
2798 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2799 rmb();
2800 msleep(1);
2803 if (i == 1000) {
2804 con_log(CL_ANN, (KERN_WARNING
2805 "megaraid mailbox: could not get poll semaphore\n"));
2806 return -1;
2810 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2811 wmb();
2813 // wait for maximum 1 second for acknowledgement
2814 if (RDINDOOR(raid_dev) & 0x2) {
2815 udelay(25);
2817 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2818 rmb();
2819 msleep(1);
2822 if (i == 1000) {
2823 con_log(CL_ANN, (KERN_WARNING
2824 "megaraid mailbox: could not acknowledge\n"));
2825 return -1;
2828 mbox->poll = 0;
2829 mbox->ack = 0x77;
2831 status = mbox->status;
2833 // invalidate the completed command id array. After command
2834 // completion, firmware would write the valid id.
2835 mbox->numstatus = 0xFF;
2836 mbox->status = 0xFF;
2837 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2838 mbox->completed[i] = 0xFF;
2841 return status;
2843 blocked_mailbox:
2845 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2846 return -1;
2851 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2852 * @adapter : controller's soft state
2853 * @raw_mbox : the mailbox
2855 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2856 * controllers. This is a faster version of the synchronous command and
2857 * therefore can be called in interrupt-context as well.
2859 static int
2860 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2862 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2863 mbox_t *mbox;
2864 long i;
2867 mbox = raid_dev->mbox;
2869 // return immediately if the mailbox is busy
2870 if (mbox->busy) return -1;
2872 // Copy mailbox data into host structure
2873 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2874 mbox->cmdid = 0xFE;
2875 mbox->busy = 1;
2876 mbox->poll = 0;
2877 mbox->ack = 0;
2878 mbox->numstatus = 0xFF;
2879 mbox->status = 0xFF;
2881 wmb();
2882 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2884 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2885 if (mbox->numstatus != 0xFF) break;
2886 rmb();
2887 udelay(MBOX_SYNC_DELAY_200);
2890 if (i == MBOX_SYNC_WAIT_CNT) {
2891 // We may need to re-calibrate the counter
2892 con_log(CL_ANN, (KERN_CRIT
2893 "megaraid: fast sync command timed out\n"));
2896 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2897 wmb();
2899 return mbox->status;
2904 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2905 * @raid_dev : RAID device (HBA) soft state
2907 * Wait until the controller's mailbox is available to accept more commands.
2908 * Wait for at most 1 second.
2910 static int
2911 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2913 mbox_t *mbox = raid_dev->mbox;
2914 int i = 0;
2916 if (mbox->busy) {
2917 udelay(25);
2918 for (i = 0; mbox->busy && i < 1000; i++)
2919 msleep(1);
2922 if (i < 1000) return 0;
2923 else return -1;
2928 * megaraid_mbox_product_info - some static information about the controller
2929 * @adapter : our soft state
2931 * Issue commands to the controller to grab some parameters required by our
2932 * caller.
2934 static int
2935 megaraid_mbox_product_info(adapter_t *adapter)
2937 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2938 mbox_t *mbox;
2939 uint8_t raw_mbox[sizeof(mbox_t)];
2940 mraid_pinfo_t *pinfo;
2941 dma_addr_t pinfo_dma_h;
2942 mraid_inquiry3_t *mraid_inq3;
2943 int i;
2946 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2947 mbox = (mbox_t *)raw_mbox;
2950 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2951 * e.g., max channels, max commands etc.
2953 pinfo = pci_zalloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
2954 &pinfo_dma_h);
2956 if (pinfo == NULL) {
2957 con_log(CL_ANN, (KERN_WARNING
2958 "megaraid: out of memory, %s %d\n", __func__,
2959 __LINE__));
2961 return -1;
2964 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2965 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2967 raw_mbox[0] = FC_NEW_CONFIG;
2968 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2969 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2971 // Issue the command
2972 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2974 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2976 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
2977 pinfo, pinfo_dma_h);
2979 return -1;
2983 * Collect information about state of each physical drive
2984 * attached to the controller. We will expose all the disks
2985 * which are not part of RAID
2987 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2988 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2989 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2993 * Get product info for information like number of channels,
2994 * maximum commands supported.
2996 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2997 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2999 raw_mbox[0] = FC_NEW_CONFIG;
3000 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3002 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3004 con_log(CL_ANN, (KERN_WARNING
3005 "megaraid: product info failed\n"));
3007 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3008 pinfo, pinfo_dma_h);
3010 return -1;
3014 * Setup some parameters for host, as required by our caller
3016 adapter->max_channel = pinfo->nchannels;
3019 * we will export all the logical drives on a single channel.
3020 * Add 1 since inquires do not come for inititor ID
3022 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3023 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3026 * These are the maximum outstanding commands for the scsi-layer
3028 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3030 memset(adapter->fw_version, 0, VERSION_SIZE);
3031 memset(adapter->bios_version, 0, VERSION_SIZE);
3033 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3034 adapter->fw_version[4] = 0;
3036 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3037 adapter->bios_version[4] = 0;
3039 con_log(CL_ANN, (KERN_NOTICE
3040 "megaraid: fw version:[%s] bios version:[%s]\n",
3041 adapter->fw_version, adapter->bios_version));
3043 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3044 pinfo_dma_h);
3046 return 0;
3052 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3053 * @adapter : soft state for the controller
3055 * This routine check whether the controller in question supports extended
3056 * ( > 10 bytes ) CDBs.
3058 static int
3059 megaraid_mbox_extended_cdb(adapter_t *adapter)
3061 mbox_t *mbox;
3062 uint8_t raw_mbox[sizeof(mbox_t)];
3063 int rval;
3065 mbox = (mbox_t *)raw_mbox;
3067 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3068 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3070 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3072 raw_mbox[0] = MAIN_MISC_OPCODE;
3073 raw_mbox[2] = SUPPORT_EXT_CDB;
3076 * Issue the command
3078 rval = 0;
3079 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3080 rval = -1;
3083 return rval;
3088 * megaraid_mbox_support_ha - Do we support clustering
3089 * @adapter : soft state for the controller
3090 * @init_id : ID of the initiator
3092 * Determine if the firmware supports clustering and the ID of the initiator.
3094 static int
3095 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3097 mbox_t *mbox;
3098 uint8_t raw_mbox[sizeof(mbox_t)];
3099 int rval;
3102 mbox = (mbox_t *)raw_mbox;
3104 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3106 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3108 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3110 raw_mbox[0] = GET_TARGET_ID;
3112 // Issue the command
3113 *init_id = 7;
3114 rval = -1;
3115 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3117 *init_id = *(uint8_t *)adapter->ibuf;
3119 con_log(CL_ANN, (KERN_INFO
3120 "megaraid: cluster firmware, initiator ID: %d\n",
3121 *init_id));
3123 rval = 0;
3126 return rval;
3131 * megaraid_mbox_support_random_del - Do we support random deletion
3132 * @adapter : soft state for the controller
3134 * Determine if the firmware supports random deletion.
3135 * Return: 1 is operation supported, 0 otherwise
3137 static int
3138 megaraid_mbox_support_random_del(adapter_t *adapter)
3140 mbox_t *mbox;
3141 uint8_t raw_mbox[sizeof(mbox_t)];
3142 int rval;
3145 * Newer firmware on Dell CERC expect a different
3146 * random deletion handling, so disable it.
3148 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3149 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3150 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3151 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3152 (adapter->fw_version[0] > '6' ||
3153 (adapter->fw_version[0] == '6' &&
3154 adapter->fw_version[2] > '6') ||
3155 (adapter->fw_version[0] == '6'
3156 && adapter->fw_version[2] == '6'
3157 && adapter->fw_version[3] > '1'))) {
3158 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3159 return 0;
3162 mbox = (mbox_t *)raw_mbox;
3164 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3166 raw_mbox[0] = FC_DEL_LOGDRV;
3167 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3169 // Issue the command
3170 rval = 0;
3171 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3173 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3175 rval = 1;
3178 return rval;
3183 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3184 * @adapter : soft state for the controller
3186 * Find out the maximum number of scatter-gather elements supported by the
3187 * firmware.
3189 static int
3190 megaraid_mbox_get_max_sg(adapter_t *adapter)
3192 mbox_t *mbox;
3193 uint8_t raw_mbox[sizeof(mbox_t)];
3194 int nsg;
3197 mbox = (mbox_t *)raw_mbox;
3199 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3201 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3203 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3205 raw_mbox[0] = MAIN_MISC_OPCODE;
3206 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3208 // Issue the command
3209 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3210 nsg = *(uint8_t *)adapter->ibuf;
3212 else {
3213 nsg = MBOX_DEFAULT_SG_SIZE;
3216 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3218 return nsg;
3223 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3224 * @adapter : soft state for the controller
3226 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3227 * can be exported as regular SCSI channels.
3229 static void
3230 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3232 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3233 mbox_t *mbox;
3234 uint8_t raw_mbox[sizeof(mbox_t)];
3237 mbox = (mbox_t *)raw_mbox;
3239 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3241 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3243 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3245 raw_mbox[0] = CHNL_CLASS;
3246 raw_mbox[2] = GET_CHNL_CLASS;
3248 // Issue the command. If the command fails, all channels are RAID
3249 // channels
3250 raid_dev->channel_class = 0xFF;
3251 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3252 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3255 return;
3260 * megaraid_mbox_flush_cache - flush adapter and disks cache
3261 * @adapter : soft state for the controller
3263 * Flush adapter cache followed by disks cache.
3265 static void
3266 megaraid_mbox_flush_cache(adapter_t *adapter)
3268 mbox_t *mbox;
3269 uint8_t raw_mbox[sizeof(mbox_t)];
3272 mbox = (mbox_t *)raw_mbox;
3274 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3276 raw_mbox[0] = FLUSH_ADAPTER;
3278 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3279 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3282 raw_mbox[0] = FLUSH_SYSTEM;
3284 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3285 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3288 return;
3293 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3294 * @adapter : soft state for the controller
3296 * Clears the pending cmds in FW and reinits its RAID structs.
3298 static int
3299 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3301 mbox_t *mbox;
3302 uint8_t raw_mbox[sizeof(mbox_t)];
3303 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3304 mbox64_t *mbox64;
3305 int status = 0;
3306 int i;
3307 uint32_t dword;
3309 mbox = (mbox_t *)raw_mbox;
3311 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3313 raw_mbox[0] = 0xFF;
3315 mbox64 = raid_dev->mbox64;
3316 mbox = raid_dev->mbox;
3318 /* Wait until mailbox is free */
3319 if (megaraid_busywait_mbox(raid_dev) != 0) {
3320 status = 1;
3321 goto blocked_mailbox;
3324 /* Copy mailbox data into host structure */
3325 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3326 mbox->cmdid = 0xFE;
3327 mbox->busy = 1;
3328 mbox->poll = 0;
3329 mbox->ack = 0;
3330 mbox->numstatus = 0;
3331 mbox->status = 0;
3333 wmb();
3334 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3336 /* Wait for maximum 1 min for status to post.
3337 * If the Firmware SUPPORTS the ABOVE COMMAND,
3338 * mbox->cmd will be set to 0
3339 * else
3340 * the firmware will reject the command with
3341 * mbox->numstatus set to 1
3344 i = 0;
3345 status = 0;
3346 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3347 rmb();
3348 msleep(1);
3349 i++;
3350 if (i > 1000 * 60) {
3351 status = 1;
3352 break;
3355 if (mbox->numstatus == 1)
3356 status = 1; /*cmd not supported*/
3358 /* Check for interrupt line */
3359 dword = RDOUTDOOR(raid_dev);
3360 WROUTDOOR(raid_dev, dword);
3361 WRINDOOR(raid_dev,2);
3363 return status;
3365 blocked_mailbox:
3366 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3367 return status;
3371 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3372 * @adapter : controller's soft state
3373 * @scb : SCB to be displayed
3374 * @level : debug level for console print
3376 * Diplay information about the given SCB iff the current debug level is
3377 * verbose.
3379 static void
3380 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3382 mbox_ccb_t *ccb;
3383 struct scsi_cmnd *scp;
3384 mbox_t *mbox;
3385 int level;
3386 int i;
3389 ccb = (mbox_ccb_t *)scb->ccb;
3390 scp = scb->scp;
3391 mbox = ccb->mbox;
3393 level = CL_DLEVEL3;
3395 con_log(level, (KERN_NOTICE
3396 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3397 mbox->cmd, scb->sno));
3399 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3400 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3401 mbox->numsge));
3403 if (!scp) return;
3405 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3407 for (i = 0; i < scp->cmd_len; i++) {
3408 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3411 con_log(level, ("\n"));
3413 return;
3418 * megaraid_mbox_setup_device_map - manage device ids
3419 * @adapter : Driver's soft state
3421 * Manage the device ids to have an appropriate mapping between the kernel
3422 * scsi addresses and megaraid scsi and logical drive addresses. We export
3423 * scsi devices on their actual addresses, whereas the logical drives are
3424 * exported on a virtual scsi channel.
3426 static void
3427 megaraid_mbox_setup_device_map(adapter_t *adapter)
3429 uint8_t c;
3430 uint8_t t;
3433 * First fill the values on the logical drive channel
3435 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3436 adapter->device_ids[adapter->max_channel][t] =
3437 (t < adapter->init_id) ? t : t - 1;
3439 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3442 * Fill the values on the physical devices channels
3444 for (c = 0; c < adapter->max_channel; c++)
3445 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3446 adapter->device_ids[c][t] = (c << 8) | t;
3451 * END: internal commands library
3455 * START: Interface for the common management module
3457 * This is the module, which interfaces with the common management module to
3458 * provide support for ioctl and sysfs
3462 * megaraid_cmm_register - register with the management module
3463 * @adapter : HBA soft state
3465 * Register with the management module, which allows applications to issue
3466 * ioctl calls to the drivers. This interface is used by the management module
3467 * to setup sysfs support as well.
3469 static int
3470 megaraid_cmm_register(adapter_t *adapter)
3472 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3473 mraid_mmadp_t adp;
3474 scb_t *scb;
3475 mbox_ccb_t *ccb;
3476 int rval;
3477 int i;
3479 // Allocate memory for the base list of scb for management module.
3480 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3482 if (adapter->uscb_list == NULL) {
3483 con_log(CL_ANN, (KERN_WARNING
3484 "megaraid: out of memory, %s %d\n", __func__,
3485 __LINE__));
3486 return -1;
3490 // Initialize the synchronization parameters for resources for
3491 // commands for management module
3492 INIT_LIST_HEAD(&adapter->uscb_pool);
3494 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3498 // link all the packets. Note, CCB for commands, coming from the
3499 // commom management module, mailbox physical address are already
3500 // setup by it. We just need placeholder for that in our local command
3501 // control blocks
3502 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3504 scb = adapter->uscb_list + i;
3505 ccb = raid_dev->uccb_list + i;
3507 scb->ccb = (caddr_t)ccb;
3508 ccb->mbox64 = raid_dev->umbox64 + i;
3509 ccb->mbox = &ccb->mbox64->mbox32;
3510 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3512 scb->gp = 0;
3514 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3515 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3516 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3518 scb->scp = NULL;
3519 scb->state = SCB_FREE;
3520 scb->dma_direction = PCI_DMA_NONE;
3521 scb->dma_type = MRAID_DMA_NONE;
3522 scb->dev_channel = -1;
3523 scb->dev_target = -1;
3525 // put scb in the free pool
3526 list_add_tail(&scb->list, &adapter->uscb_pool);
3529 adp.unique_id = adapter->unique_id;
3530 adp.drvr_type = DRVRTYPE_MBOX;
3531 adp.drvr_data = (unsigned long)adapter;
3532 adp.pdev = adapter->pdev;
3533 adp.issue_uioc = megaraid_mbox_mm_handler;
3534 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3535 adp.max_kioc = MBOX_MAX_USER_CMDS;
3537 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3539 con_log(CL_ANN, (KERN_WARNING
3540 "megaraid mbox: did not register with CMM\n"));
3542 kfree(adapter->uscb_list);
3545 return rval;
3550 * megaraid_cmm_unregister - un-register with the management module
3551 * @adapter : HBA soft state
3553 * Un-register with the management module.
3554 * FIXME: mgmt module must return failure for unregister if it has pending
3555 * commands in LLD.
3557 static int
3558 megaraid_cmm_unregister(adapter_t *adapter)
3560 kfree(adapter->uscb_list);
3561 mraid_mm_unregister_adp(adapter->unique_id);
3562 return 0;
3567 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3568 * @drvr_data : LLD specific data
3569 * @kioc : CMM interface packet
3570 * @action : command action
3572 * This routine is invoked whenever the Common Management Module (CMM) has a
3573 * command for us. The 'action' parameter specifies if this is a new command
3574 * or otherwise.
3576 static int
3577 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3579 adapter_t *adapter;
3581 if (action != IOCTL_ISSUE) {
3582 con_log(CL_ANN, (KERN_WARNING
3583 "megaraid: unsupported management action:%#2x\n",
3584 action));
3585 return (-ENOTSUPP);
3588 adapter = (adapter_t *)drvr_data;
3590 // make sure this adapter is not being detached right now.
3591 if (atomic_read(&adapter->being_detached)) {
3592 con_log(CL_ANN, (KERN_WARNING
3593 "megaraid: reject management request, detaching\n"));
3594 return (-ENODEV);
3597 switch (kioc->opcode) {
3599 case GET_ADAP_INFO:
3601 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3602 (unsigned long)kioc->buf_vaddr);
3604 kioc->done(kioc);
3606 return kioc->status;
3608 case MBOX_CMD:
3610 return megaraid_mbox_mm_command(adapter, kioc);
3612 default:
3613 kioc->status = (-EINVAL);
3614 kioc->done(kioc);
3615 return (-EINVAL);
3618 return 0; // not reached
3622 * megaraid_mbox_mm_command - issues commands routed through CMM
3623 * @adapter : HBA soft state
3624 * @kioc : management command packet
3626 * Issues commands, which are routed through the management module.
3628 static int
3629 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3631 struct list_head *head = &adapter->uscb_pool;
3632 mbox64_t *mbox64;
3633 uint8_t *raw_mbox;
3634 scb_t *scb;
3635 mbox_ccb_t *ccb;
3636 unsigned long flags;
3638 // detach one scb from free pool
3639 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3641 if (list_empty(head)) { // should never happen because of CMM
3643 con_log(CL_ANN, (KERN_WARNING
3644 "megaraid mbox: bug in cmm handler, lost resources\n"));
3646 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3648 return (-EINVAL);
3651 scb = list_entry(head->next, scb_t, list);
3652 list_del_init(&scb->list);
3654 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3656 scb->state = SCB_ACTIVE;
3657 scb->dma_type = MRAID_DMA_NONE;
3658 scb->dma_direction = PCI_DMA_NONE;
3660 ccb = (mbox_ccb_t *)scb->ccb;
3661 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3662 raw_mbox = (uint8_t *)&mbox64->mbox32;
3664 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3666 scb->gp = (unsigned long)kioc;
3669 * If it is a logdrv random delete operation, we have to wait till
3670 * there are no outstanding cmds at the fw and then issue it directly
3672 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3674 if (wait_till_fw_empty(adapter)) {
3675 con_log(CL_ANN, (KERN_NOTICE
3676 "megaraid mbox: LD delete, timed out\n"));
3678 kioc->status = -ETIME;
3680 scb->status = -1;
3682 megaraid_mbox_mm_done(adapter, scb);
3684 return (-ETIME);
3687 INIT_LIST_HEAD(&scb->list);
3689 scb->state = SCB_ISSUED;
3690 if (mbox_post_cmd(adapter, scb) != 0) {
3692 con_log(CL_ANN, (KERN_NOTICE
3693 "megaraid mbox: LD delete, mailbox busy\n"));
3695 kioc->status = -EBUSY;
3697 scb->status = -1;
3699 megaraid_mbox_mm_done(adapter, scb);
3701 return (-EBUSY);
3704 return 0;
3707 // put the command on the pending list and execute
3708 megaraid_mbox_runpendq(adapter, scb);
3710 return 0;
3714 static int
3715 wait_till_fw_empty(adapter_t *adapter)
3717 unsigned long flags = 0;
3718 int i;
3722 * Set the quiescent flag to stop issuing cmds to FW.
3724 spin_lock_irqsave(&adapter->lock, flags);
3725 adapter->quiescent++;
3726 spin_unlock_irqrestore(&adapter->lock, flags);
3729 * Wait till there are no more cmds outstanding at FW. Try for at most
3730 * 60 seconds
3732 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3733 con_log(CL_DLEVEL1, (KERN_INFO
3734 "megaraid: FW has %d pending commands\n",
3735 adapter->outstanding_cmds));
3737 msleep(1000);
3740 return adapter->outstanding_cmds;
3745 * megaraid_mbox_mm_done - callback for CMM commands
3746 * @adapter : HBA soft state
3747 * @scb : completed command
3749 * Callback routine for internal commands originated from the management
3750 * module.
3752 static void
3753 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3755 uioc_t *kioc;
3756 mbox64_t *mbox64;
3757 uint8_t *raw_mbox;
3758 unsigned long flags;
3760 kioc = (uioc_t *)scb->gp;
3761 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3762 mbox64->mbox32.status = scb->status;
3763 raw_mbox = (uint8_t *)&mbox64->mbox32;
3766 // put scb in the free pool
3767 scb->state = SCB_FREE;
3768 scb->scp = NULL;
3770 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3772 list_add(&scb->list, &adapter->uscb_pool);
3774 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3776 // if a delete logical drive operation succeeded, restart the
3777 // controller
3778 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3780 adapter->quiescent--;
3782 megaraid_mbox_runpendq(adapter, NULL);
3785 kioc->done(kioc);
3787 return;
3792 * gather_hbainfo - HBA characteristics for the applications
3793 * @adapter : HBA soft state
3794 * @hinfo : pointer to the caller's host info strucuture
3796 static int
3797 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3799 uint8_t dmajor;
3801 dmajor = megaraid_mbox_version[0];
3803 hinfo->pci_vendor_id = adapter->pdev->vendor;
3804 hinfo->pci_device_id = adapter->pdev->device;
3805 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3806 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3808 hinfo->pci_bus = adapter->pdev->bus->number;
3809 hinfo->pci_dev_fn = adapter->pdev->devfn;
3810 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3811 hinfo->irq = adapter->host->irq;
3812 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3814 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3815 hinfo->host_no = adapter->host->host_no;
3817 return 0;
3821 * END: Interface for the common management module
3827 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3828 * @adapter : controller's soft state
3830 * Allocate packets required to issue FW calls whenever the sysfs attributes
3831 * are read. These attributes would require up-to-date information from the
3832 * FW. Also set up resources for mutual exclusion to share these resources and
3833 * the wait queue.
3835 * Return 0 on success.
3836 * Return -ERROR_CODE on failure.
3838 static int
3839 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3841 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3842 int rval = 0;
3844 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3846 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3848 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3849 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3851 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3852 !raid_dev->sysfs_buffer) {
3854 con_log(CL_ANN, (KERN_WARNING
3855 "megaraid: out of memory, %s %d\n", __func__,
3856 __LINE__));
3858 rval = -ENOMEM;
3860 megaraid_sysfs_free_resources(adapter);
3863 mutex_init(&raid_dev->sysfs_mtx);
3865 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3867 return rval;
3872 * megaraid_sysfs_free_resources - free sysfs related resources
3873 * @adapter : controller's soft state
3875 * Free packets allocated for sysfs FW commands
3877 static void
3878 megaraid_sysfs_free_resources(adapter_t *adapter)
3880 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3882 kfree(raid_dev->sysfs_uioc);
3883 kfree(raid_dev->sysfs_mbox64);
3885 if (raid_dev->sysfs_buffer) {
3886 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3887 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3893 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3894 * @uioc : completed packet
3896 * Callback routine called in the ISR/tasklet context for get ldmap call
3898 static void
3899 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3901 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3902 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3904 uioc->status = 0;
3906 wake_up(&raid_dev->sysfs_wait_q);
3911 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3912 * @data : timed out packet
3914 * Timeout routine to recover and return to application, in case the adapter
3915 * has stopped responding. A timeout of 60 seconds for this command seems like
3916 * a good value.
3918 static void
3919 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
3921 uioc_t *uioc = (uioc_t *)data;
3922 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3923 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3925 uioc->status = -ETIME;
3927 wake_up(&raid_dev->sysfs_wait_q);
3932 * megaraid_sysfs_get_ldmap - get update logical drive map
3933 * @adapter : controller's soft state
3935 * This routine will be called whenever user reads the logical drive
3936 * attributes, go get the current logical drive mapping table from the
3937 * firmware. We use the management API's to issue commands to the controller.
3939 * NOTE: The commands issuance functionality is not generalized and
3940 * implemented in context of "get ld map" command only. If required, the
3941 * command issuance logical can be trivially pulled out and implemented as a
3942 * standalone library. For now, this should suffice since there is no other
3943 * user of this interface.
3945 * Return 0 on success.
3946 * Return -1 on failure.
3948 static int
3949 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3951 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3952 uioc_t *uioc;
3953 mbox64_t *mbox64;
3954 mbox_t *mbox;
3955 char *raw_mbox;
3956 struct timer_list sysfs_timer;
3957 struct timer_list *timerp;
3958 caddr_t ldmap;
3959 int rval = 0;
3962 * Allow only one read at a time to go through the sysfs attributes
3964 mutex_lock(&raid_dev->sysfs_mtx);
3966 uioc = raid_dev->sysfs_uioc;
3967 mbox64 = raid_dev->sysfs_mbox64;
3968 ldmap = raid_dev->sysfs_buffer;
3970 memset(uioc, 0, sizeof(uioc_t));
3971 memset(mbox64, 0, sizeof(mbox64_t));
3972 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3974 mbox = &mbox64->mbox32;
3975 raw_mbox = (char *)mbox;
3976 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3977 uioc->buf_vaddr = (caddr_t)adapter;
3978 uioc->status = -ENODATA;
3979 uioc->done = megaraid_sysfs_get_ldmap_done;
3982 * Prepare the mailbox packet to get the current logical drive mapping
3983 * table
3985 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3987 raw_mbox[0] = FC_DEL_LOGDRV;
3988 raw_mbox[2] = OP_GET_LDID_MAP;
3991 * Setup a timer to recover from a non-responding controller
3993 timerp = &sysfs_timer;
3994 init_timer(timerp);
3996 timerp->function = megaraid_sysfs_get_ldmap_timeout;
3997 timerp->data = (unsigned long)uioc;
3998 timerp->expires = jiffies + 60 * HZ;
4000 add_timer(timerp);
4003 * Send the command to the firmware
4005 rval = megaraid_mbox_mm_command(adapter, uioc);
4007 if (rval == 0) { // command successfully issued
4008 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4011 * Check if the command timed out
4013 if (uioc->status == -ETIME) {
4014 con_log(CL_ANN, (KERN_NOTICE
4015 "megaraid: sysfs get ld map timed out\n"));
4017 rval = -ETIME;
4019 else {
4020 rval = mbox->status;
4023 if (rval == 0) {
4024 memcpy(raid_dev->curr_ldmap, ldmap,
4025 sizeof(raid_dev->curr_ldmap));
4027 else {
4028 con_log(CL_ANN, (KERN_NOTICE
4029 "megaraid: get ld map failed with %x\n", rval));
4032 else {
4033 con_log(CL_ANN, (KERN_NOTICE
4034 "megaraid: could not issue ldmap command:%x\n", rval));
4038 del_timer_sync(timerp);
4040 mutex_unlock(&raid_dev->sysfs_mtx);
4042 return rval;
4047 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4048 * @cdev : class device object representation for the host
4049 * @buf : buffer to send data to
4051 * Display the handle used by the applications while executing management
4052 * tasks on the adapter. We invoke a management module API to get the adapter
4053 * handle, since we do not interface with applications directly.
4055 static ssize_t
4056 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
4057 char *buf)
4059 struct Scsi_Host *shost = class_to_shost(dev);
4060 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4061 uint32_t app_hndl;
4063 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4065 return snprintf(buf, 8, "%u\n", app_hndl);
4070 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4071 * @dev : device object representation for the scsi device
4072 * @attr : device attribute to show
4073 * @buf : buffer to send data to
4075 * Display the logical drive number for the device in question, if it a valid
4076 * logical drive. For physical devices, "-1" is returned.
4078 * The logical drive number is displayed in following format:
4080 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4082 * <int> <int> <int> <int>
4084 static ssize_t
4085 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4087 struct scsi_device *sdev = to_scsi_device(dev);
4088 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4089 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4090 int scsi_id = -1;
4091 int logical_drv = -1;
4092 int ldid_map = -1;
4093 uint32_t app_hndl = 0;
4094 int mapped_sdev_id;
4095 int rval;
4096 int i;
4098 if (raid_dev->random_del_supported &&
4099 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4101 rval = megaraid_sysfs_get_ldmap(adapter);
4102 if (rval == 0) {
4104 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4106 mapped_sdev_id = sdev->id;
4108 if (sdev->id > adapter->init_id) {
4109 mapped_sdev_id -= 1;
4112 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4114 scsi_id = sdev->id;
4116 logical_drv = i;
4118 ldid_map = raid_dev->curr_ldmap[i];
4120 app_hndl = mraid_mm_adapter_app_handle(
4121 adapter->unique_id);
4123 break;
4127 else {
4128 con_log(CL_ANN, (KERN_NOTICE
4129 "megaraid: sysfs get ld map failed: %x\n",
4130 rval));
4134 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4135 ldid_map, app_hndl);
4140 * END: Mailbox Low Level Driver
4142 module_init(megaraid_init);
4143 module_exit(megaraid_exit);
4145 /* vim: set ts=8 sw=8 tw=78 ai si: */