Linux 4.16.11
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_mbox.c
blob530358cdcb3912e2c5c751b076905a348e32408e
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_host_reset_handler = megaraid_reset_handler,
345 .change_queue_depth = scsi_change_queue_depth,
346 .use_clustering = ENABLE_CLUSTERING,
347 .no_write_same = 1,
348 .sdev_attrs = megaraid_sdev_attrs,
349 .shost_attrs = megaraid_shost_attrs,
354 * megaraid_init - module load hook
356 * We register ourselves as hotplug enabled module and let PCI subsystem
357 * discover our adapters.
359 static int __init
360 megaraid_init(void)
362 int rval;
364 // Announce the driver version
365 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
366 MEGARAID_EXT_VERSION));
368 // check validity of module parameters
369 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
371 con_log(CL_ANN, (KERN_WARNING
372 "megaraid mailbox: max commands per lun reset to %d\n",
373 MBOX_MAX_SCSI_CMDS));
375 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
379 // register as a PCI hot-plug driver module
380 rval = pci_register_driver(&megaraid_pci_driver);
381 if (rval < 0) {
382 con_log(CL_ANN, (KERN_WARNING
383 "megaraid: could not register hotplug support.\n"));
386 return rval;
391 * megaraid_exit - driver unload entry point
393 * We simply unwrap the megaraid_init routine here.
395 static void __exit
396 megaraid_exit(void)
398 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
400 // unregister as PCI hotplug driver
401 pci_unregister_driver(&megaraid_pci_driver);
403 return;
408 * megaraid_probe_one - PCI hotplug entry point
409 * @pdev : handle to this controller's PCI configuration space
410 * @id : pci device id of the class of controllers
412 * This routine should be called whenever a new adapter is detected by the
413 * PCI hotplug susbsystem.
415 static int
416 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
418 adapter_t *adapter;
421 // detected a new controller
422 con_log(CL_ANN, (KERN_INFO
423 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
424 pdev->vendor, pdev->device, pdev->subsystem_vendor,
425 pdev->subsystem_device));
427 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
428 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
430 if (pci_enable_device(pdev)) {
431 con_log(CL_ANN, (KERN_WARNING
432 "megaraid: pci_enable_device failed\n"));
434 return -ENODEV;
437 // Enable bus-mastering on this controller
438 pci_set_master(pdev);
440 // Allocate the per driver initialization structure
441 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
443 if (adapter == NULL) {
444 con_log(CL_ANN, (KERN_WARNING
445 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
447 goto out_probe_one;
451 // set up PCI related soft state and other pre-known parameters
452 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
453 adapter->irq = pdev->irq;
454 adapter->pdev = pdev;
456 atomic_set(&adapter->being_detached, 0);
458 // Setup the default DMA mask. This would be changed later on
459 // depending on hardware capabilities
460 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
462 con_log(CL_ANN, (KERN_WARNING
463 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
465 goto out_free_adapter;
469 // Initialize the synchronization lock for kernel and LLD
470 spin_lock_init(&adapter->lock);
472 // Initialize the command queues: the list of free SCBs and the list
473 // of pending SCBs.
474 INIT_LIST_HEAD(&adapter->kscb_pool);
475 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
477 INIT_LIST_HEAD(&adapter->pend_list);
478 spin_lock_init(PENDING_LIST_LOCK(adapter));
480 INIT_LIST_HEAD(&adapter->completed_list);
481 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
484 // Start the mailbox based controller
485 if (megaraid_init_mbox(adapter) != 0) {
486 con_log(CL_ANN, (KERN_WARNING
487 "megaraid: maibox adapter did not initialize\n"));
489 goto out_free_adapter;
492 // Register with LSI Common Management Module
493 if (megaraid_cmm_register(adapter) != 0) {
495 con_log(CL_ANN, (KERN_WARNING
496 "megaraid: could not register with management module\n"));
498 goto out_fini_mbox;
501 // setup adapter handle in PCI soft state
502 pci_set_drvdata(pdev, adapter);
504 // attach with scsi mid-layer
505 if (megaraid_io_attach(adapter) != 0) {
507 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
509 goto out_cmm_unreg;
512 return 0;
514 out_cmm_unreg:
515 megaraid_cmm_unregister(adapter);
516 out_fini_mbox:
517 megaraid_fini_mbox(adapter);
518 out_free_adapter:
519 kfree(adapter);
520 out_probe_one:
521 pci_disable_device(pdev);
523 return -ENODEV;
528 * megaraid_detach_one - release framework resources and call LLD release routine
529 * @pdev : handle for our PCI configuration space
531 * This routine is called during driver unload. We free all the allocated
532 * resources and call the corresponding LLD so that it can also release all
533 * its resources.
535 * This routine is also called from the PCI hotplug system.
537 static void
538 megaraid_detach_one(struct pci_dev *pdev)
540 adapter_t *adapter;
541 struct Scsi_Host *host;
544 // Start a rollback on this adapter
545 adapter = pci_get_drvdata(pdev);
547 if (!adapter) {
548 con_log(CL_ANN, (KERN_CRIT
549 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
550 pdev->vendor, pdev->device, pdev->subsystem_vendor,
551 pdev->subsystem_device));
553 return;
555 else {
556 con_log(CL_ANN, (KERN_NOTICE
557 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
558 pdev->vendor, pdev->device, pdev->subsystem_vendor,
559 pdev->subsystem_device));
563 host = adapter->host;
565 // do not allow any more requests from the management module for this
566 // adapter.
567 // FIXME: How do we account for the request which might still be
568 // pending with us?
569 atomic_set(&adapter->being_detached, 1);
571 // detach from the IO sub-system
572 megaraid_io_detach(adapter);
574 // Unregister from common management module
576 // FIXME: this must return success or failure for conditions if there
577 // is a command pending with LLD or not.
578 megaraid_cmm_unregister(adapter);
580 // finalize the mailbox based controller and release all resources
581 megaraid_fini_mbox(adapter);
583 kfree(adapter);
585 scsi_host_put(host);
587 pci_disable_device(pdev);
589 return;
594 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
595 * @pdev : generic driver model device
597 * Shutdown notification, perform flush cache.
599 static void
600 megaraid_mbox_shutdown(struct pci_dev *pdev)
602 adapter_t *adapter = pci_get_drvdata(pdev);
603 static int counter;
605 if (!adapter) {
606 con_log(CL_ANN, (KERN_WARNING
607 "megaraid: null device in shutdown\n"));
608 return;
611 // flush caches now
612 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
613 counter++));
615 megaraid_mbox_flush_cache(adapter);
617 con_log(CL_ANN, ("done\n"));
622 * megaraid_io_attach - attach a device with the IO subsystem
623 * @adapter : controller's soft state
625 * Attach this device with the IO subsystem.
627 static int
628 megaraid_io_attach(adapter_t *adapter)
630 struct Scsi_Host *host;
632 // Initialize SCSI Host structure
633 host = scsi_host_alloc(&megaraid_template_g, 8);
634 if (!host) {
635 con_log(CL_ANN, (KERN_WARNING
636 "megaraid mbox: scsi_register failed\n"));
638 return -1;
641 SCSIHOST2ADAP(host) = (caddr_t)adapter;
642 adapter->host = host;
644 host->irq = adapter->irq;
645 host->unique_id = adapter->unique_id;
646 host->can_queue = adapter->max_cmds;
647 host->this_id = adapter->init_id;
648 host->sg_tablesize = adapter->sglen;
649 host->max_sectors = adapter->max_sectors;
650 host->cmd_per_lun = adapter->cmd_per_lun;
651 host->max_channel = adapter->max_channel;
652 host->max_id = adapter->max_target;
653 host->max_lun = adapter->max_lun;
656 // notify mid-layer about the new controller
657 if (scsi_add_host(host, &adapter->pdev->dev)) {
659 con_log(CL_ANN, (KERN_WARNING
660 "megaraid mbox: scsi_add_host failed\n"));
662 scsi_host_put(host);
664 return -1;
667 scsi_scan_host(host);
669 return 0;
674 * megaraid_io_detach - detach a device from the IO subsystem
675 * @adapter : controller's soft state
677 * Detach this device from the IO subsystem.
679 static void
680 megaraid_io_detach(adapter_t *adapter)
682 struct Scsi_Host *host;
684 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
686 host = adapter->host;
688 scsi_remove_host(host);
690 return;
695 * START: Mailbox Low Level Driver
697 * This is section specific to the single mailbox based controllers
701 * megaraid_init_mbox - initialize controller
702 * @adapter : our soft state
704 * - Allocate 16-byte aligned mailbox memory for firmware handshake
705 * - Allocate controller's memory resources
706 * - Find out all initialization data
707 * - Allocate memory required for all the commands
708 * - Use internal library of FW routines, build up complete soft state
710 static int
711 megaraid_init_mbox(adapter_t *adapter)
713 struct pci_dev *pdev;
714 mraid_device_t *raid_dev;
715 int i;
716 uint32_t magic64;
719 adapter->ito = MBOX_TIMEOUT;
720 pdev = adapter->pdev;
723 * Allocate and initialize the init data structure for mailbox
724 * controllers
726 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
727 if (raid_dev == NULL) return -1;
731 * Attach the adapter soft state to raid device soft state
733 adapter->raid_device = (caddr_t)raid_dev;
734 raid_dev->fast_load = megaraid_fast_load;
737 // our baseport
738 raid_dev->baseport = pci_resource_start(pdev, 0);
740 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
742 con_log(CL_ANN, (KERN_WARNING
743 "megaraid: mem region busy\n"));
745 goto out_free_raid_dev;
748 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
750 if (!raid_dev->baseaddr) {
752 con_log(CL_ANN, (KERN_WARNING
753 "megaraid: could not map hba memory\n") );
755 goto out_release_regions;
758 /* initialize the mutual exclusion lock for the mailbox */
759 spin_lock_init(&raid_dev->mailbox_lock);
761 /* allocate memory required for commands */
762 if (megaraid_alloc_cmd_packets(adapter) != 0)
763 goto out_iounmap;
766 * Issue SYNC cmd to flush the pending cmds in the adapter
767 * and initialize its internal state
770 if (megaraid_mbox_fire_sync_cmd(adapter))
771 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
774 * Setup the rest of the soft state using the library of
775 * FW routines
778 /* request IRQ and register the interrupt service routine */
779 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
780 adapter)) {
782 con_log(CL_ANN, (KERN_WARNING
783 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
784 goto out_alloc_cmds;
788 // Product info
789 if (megaraid_mbox_product_info(adapter) != 0)
790 goto out_free_irq;
792 // Do we support extended CDBs
793 adapter->max_cdb_sz = 10;
794 if (megaraid_mbox_extended_cdb(adapter) == 0) {
795 adapter->max_cdb_sz = 16;
799 * Do we support cluster environment, if we do, what is the initiator
800 * id.
801 * NOTE: In a non-cluster aware firmware environment, the LLD should
802 * return 7 as initiator id.
804 adapter->ha = 0;
805 adapter->init_id = -1;
806 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
807 adapter->ha = 1;
811 * Prepare the device ids array to have the mapping between the kernel
812 * device address and megaraid device address.
813 * We export the physical devices on their actual addresses. The
814 * logical drives are exported on a virtual SCSI channel
816 megaraid_mbox_setup_device_map(adapter);
818 // If the firmware supports random deletion, update the device id map
819 if (megaraid_mbox_support_random_del(adapter)) {
821 // Change the logical drives numbers in device_ids array one
822 // slot in device_ids is reserved for target id, that's why
823 // "<=" below
824 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
825 adapter->device_ids[adapter->max_channel][i] += 0x80;
827 adapter->device_ids[adapter->max_channel][adapter->init_id] =
828 0xFF;
830 raid_dev->random_del_supported = 1;
834 * find out the maximum number of scatter-gather elements supported by
835 * this firmware
837 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
839 // enumerate RAID and SCSI channels so that all devices on SCSI
840 // channels can later be exported, including disk devices
841 megaraid_mbox_enum_raid_scsi(adapter);
844 * Other parameters required by upper layer
846 * maximum number of sectors per IO command
848 adapter->max_sectors = megaraid_max_sectors;
851 * number of queued commands per LUN.
853 adapter->cmd_per_lun = megaraid_cmd_per_lun;
856 * Allocate resources required to issue FW calls, when sysfs is
857 * accessed
859 if (megaraid_sysfs_alloc_resources(adapter) != 0)
860 goto out_free_irq;
862 // Set the DMA mask to 64-bit. All supported controllers as capable of
863 // DMA in this range
864 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
866 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
867 ((adapter->pdev->subsystem_device !=
868 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
869 (adapter->pdev->subsystem_device !=
870 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
871 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
872 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
873 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
874 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
875 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
876 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
877 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
878 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
879 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
880 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
881 if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
882 con_log(CL_ANN, (KERN_WARNING
883 "megaraid: DMA mask for 64-bit failed\n"));
885 if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
886 con_log(CL_ANN, (KERN_WARNING
887 "megaraid: 32-bit DMA mask failed\n"));
888 goto out_free_sysfs_res;
893 // setup tasklet for DPC
894 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
895 (unsigned long)adapter);
897 con_log(CL_DLEVEL1, (KERN_INFO
898 "megaraid mbox hba successfully initialized\n"));
900 return 0;
902 out_free_sysfs_res:
903 megaraid_sysfs_free_resources(adapter);
904 out_free_irq:
905 free_irq(adapter->irq, adapter);
906 out_alloc_cmds:
907 megaraid_free_cmd_packets(adapter);
908 out_iounmap:
909 iounmap(raid_dev->baseaddr);
910 out_release_regions:
911 pci_release_regions(pdev);
912 out_free_raid_dev:
913 kfree(raid_dev);
915 return -1;
920 * megaraid_fini_mbox - undo controller initialization
921 * @adapter : our soft state
923 static void
924 megaraid_fini_mbox(adapter_t *adapter)
926 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
928 // flush all caches
929 megaraid_mbox_flush_cache(adapter);
931 tasklet_kill(&adapter->dpc_h);
933 megaraid_sysfs_free_resources(adapter);
935 megaraid_free_cmd_packets(adapter);
937 free_irq(adapter->irq, adapter);
939 iounmap(raid_dev->baseaddr);
941 pci_release_regions(adapter->pdev);
943 kfree(raid_dev);
945 return;
950 * megaraid_alloc_cmd_packets - allocate shared mailbox
951 * @adapter : soft state of the raid controller
953 * Allocate and align the shared mailbox. This maibox is used to issue
954 * all the commands. For IO based controllers, the mailbox is also registered
955 * with the FW. Allocate memory for all commands as well.
956 * This is our big allocator.
958 static int
959 megaraid_alloc_cmd_packets(adapter_t *adapter)
961 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
962 struct pci_dev *pdev;
963 unsigned long align;
964 scb_t *scb;
965 mbox_ccb_t *ccb;
966 struct mraid_pci_blk *epthru_pci_blk;
967 struct mraid_pci_blk *sg_pci_blk;
968 struct mraid_pci_blk *mbox_pci_blk;
969 int i;
971 pdev = adapter->pdev;
974 * Setup the mailbox
975 * Allocate the common 16-byte aligned memory for the handshake
976 * mailbox.
978 raid_dev->una_mbox64 = pci_zalloc_consistent(adapter->pdev,
979 sizeof(mbox64_t),
980 &raid_dev->una_mbox64_dma);
982 if (!raid_dev->una_mbox64) {
983 con_log(CL_ANN, (KERN_WARNING
984 "megaraid: out of memory, %s %d\n", __func__,
985 __LINE__));
986 return -1;
990 * Align the mailbox at 16-byte boundary
992 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
994 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
995 (~0UL ^ 0xFUL));
997 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
999 align = ((void *)raid_dev->mbox -
1000 ((void *)&raid_dev->una_mbox64->mbox32));
1002 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1003 align;
1005 // Allocate memory for commands issued internally
1006 adapter->ibuf = pci_zalloc_consistent(pdev, MBOX_IBUF_SIZE,
1007 &adapter->ibuf_dma_h);
1008 if (!adapter->ibuf) {
1010 con_log(CL_ANN, (KERN_WARNING
1011 "megaraid: out of memory, %s %d\n", __func__,
1012 __LINE__));
1014 goto out_free_common_mbox;
1017 // Allocate memory for our SCSI Command Blocks and their associated
1018 // memory
1021 * Allocate memory for the base list of scb. Later allocate memory for
1022 * CCBs and embedded components of each CCB and point the pointers in
1023 * scb to the allocated components
1024 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1025 * since the calling routine does not yet know the number of available
1026 * commands.
1028 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1030 if (adapter->kscb_list == NULL) {
1031 con_log(CL_ANN, (KERN_WARNING
1032 "megaraid: out of memory, %s %d\n", __func__,
1033 __LINE__));
1034 goto out_free_ibuf;
1037 // memory allocation for our command packets
1038 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1039 con_log(CL_ANN, (KERN_WARNING
1040 "megaraid: out of memory, %s %d\n", __func__,
1041 __LINE__));
1042 goto out_free_scb_list;
1045 // Adjust the scb pointers and link in the free pool
1046 epthru_pci_blk = raid_dev->epthru_pool;
1047 sg_pci_blk = raid_dev->sg_pool;
1048 mbox_pci_blk = raid_dev->mbox_pool;
1050 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1051 scb = adapter->kscb_list + i;
1052 ccb = raid_dev->ccb_list + i;
1054 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1055 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1056 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1057 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1059 // make sure the mailbox is aligned properly
1060 if (ccb->mbox_dma_h & 0x0F) {
1061 con_log(CL_ANN, (KERN_CRIT
1062 "megaraid mbox: not aligned on 16-bytes\n"));
1064 goto out_teardown_dma_pools;
1067 ccb->epthru = (mraid_epassthru_t *)
1068 epthru_pci_blk[i].vaddr;
1069 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1070 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1071 ccb->pthru_dma_h = ccb->epthru_dma_h;
1074 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1075 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1076 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1078 scb->ccb = (caddr_t)ccb;
1079 scb->gp = 0;
1081 scb->sno = i; // command index
1083 scb->scp = NULL;
1084 scb->state = SCB_FREE;
1085 scb->dma_direction = PCI_DMA_NONE;
1086 scb->dma_type = MRAID_DMA_NONE;
1087 scb->dev_channel = -1;
1088 scb->dev_target = -1;
1090 // put scb in the free pool
1091 list_add_tail(&scb->list, &adapter->kscb_pool);
1094 return 0;
1096 out_teardown_dma_pools:
1097 megaraid_mbox_teardown_dma_pools(adapter);
1098 out_free_scb_list:
1099 kfree(adapter->kscb_list);
1100 out_free_ibuf:
1101 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1102 adapter->ibuf_dma_h);
1103 out_free_common_mbox:
1104 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1105 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1107 return -1;
1112 * megaraid_free_cmd_packets - free memory
1113 * @adapter : soft state of the raid controller
1115 * Release memory resources allocated for commands.
1117 static void
1118 megaraid_free_cmd_packets(adapter_t *adapter)
1120 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1122 megaraid_mbox_teardown_dma_pools(adapter);
1124 kfree(adapter->kscb_list);
1126 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1127 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1129 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1130 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1131 return;
1136 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1137 * @adapter : HBA soft state
1139 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1140 * and scatter-gather lists.
1142 static int
1143 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1145 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1146 struct mraid_pci_blk *epthru_pci_blk;
1147 struct mraid_pci_blk *sg_pci_blk;
1148 struct mraid_pci_blk *mbox_pci_blk;
1149 int i;
1153 // Allocate memory for 16-bytes aligned mailboxes
1154 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
1155 &adapter->pdev->dev,
1156 sizeof(mbox64_t) + 16,
1157 16, 0);
1159 if (raid_dev->mbox_pool_handle == NULL) {
1160 goto fail_setup_dma_pool;
1163 mbox_pci_blk = raid_dev->mbox_pool;
1164 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1165 mbox_pci_blk[i].vaddr = dma_pool_alloc(
1166 raid_dev->mbox_pool_handle,
1167 GFP_KERNEL,
1168 &mbox_pci_blk[i].dma_addr);
1169 if (!mbox_pci_blk[i].vaddr) {
1170 goto fail_setup_dma_pool;
1175 * Allocate memory for each embedded passthru strucuture pointer
1176 * Request for a 128 bytes aligned structure for each passthru command
1177 * structure
1178 * Since passthru and extended passthru commands are exclusive, they
1179 * share common memory pool. Passthru structures piggyback on memory
1180 * allocted to extended passthru since passthru is smaller of the two
1182 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
1183 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
1185 if (raid_dev->epthru_pool_handle == NULL) {
1186 goto fail_setup_dma_pool;
1189 epthru_pci_blk = raid_dev->epthru_pool;
1190 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1191 epthru_pci_blk[i].vaddr = dma_pool_alloc(
1192 raid_dev->epthru_pool_handle,
1193 GFP_KERNEL,
1194 &epthru_pci_blk[i].dma_addr);
1195 if (!epthru_pci_blk[i].vaddr) {
1196 goto fail_setup_dma_pool;
1201 // Allocate memory for each scatter-gather list. Request for 512 bytes
1202 // alignment for each sg list
1203 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
1204 &adapter->pdev->dev,
1205 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1206 512, 0);
1208 if (raid_dev->sg_pool_handle == NULL) {
1209 goto fail_setup_dma_pool;
1212 sg_pci_blk = raid_dev->sg_pool;
1213 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1214 sg_pci_blk[i].vaddr = dma_pool_alloc(
1215 raid_dev->sg_pool_handle,
1216 GFP_KERNEL,
1217 &sg_pci_blk[i].dma_addr);
1218 if (!sg_pci_blk[i].vaddr) {
1219 goto fail_setup_dma_pool;
1223 return 0;
1225 fail_setup_dma_pool:
1226 megaraid_mbox_teardown_dma_pools(adapter);
1227 return -1;
1232 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1233 * @adapter : HBA soft state
1235 * Teardown the dma pool for mailbox, passthru and extended passthru
1236 * structures, and scatter-gather lists.
1238 static void
1239 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1241 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1242 struct mraid_pci_blk *epthru_pci_blk;
1243 struct mraid_pci_blk *sg_pci_blk;
1244 struct mraid_pci_blk *mbox_pci_blk;
1245 int i;
1248 sg_pci_blk = raid_dev->sg_pool;
1249 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1250 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1251 sg_pci_blk[i].dma_addr);
1253 if (raid_dev->sg_pool_handle)
1254 dma_pool_destroy(raid_dev->sg_pool_handle);
1257 epthru_pci_blk = raid_dev->epthru_pool;
1258 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1259 dma_pool_free(raid_dev->epthru_pool_handle,
1260 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1262 if (raid_dev->epthru_pool_handle)
1263 dma_pool_destroy(raid_dev->epthru_pool_handle);
1266 mbox_pci_blk = raid_dev->mbox_pool;
1267 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1268 dma_pool_free(raid_dev->mbox_pool_handle,
1269 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1271 if (raid_dev->mbox_pool_handle)
1272 dma_pool_destroy(raid_dev->mbox_pool_handle);
1274 return;
1279 * megaraid_alloc_scb - detach and return a scb from the free list
1280 * @adapter : controller's soft state
1281 * @scp : pointer to the scsi command to be executed
1283 * Return the scb from the head of the free list. %NULL if there are none
1284 * available.
1286 static scb_t *
1287 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1289 struct list_head *head = &adapter->kscb_pool;
1290 scb_t *scb = NULL;
1291 unsigned long flags;
1293 // detach scb from free pool
1294 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1296 if (list_empty(head)) {
1297 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1298 return NULL;
1301 scb = list_entry(head->next, scb_t, list);
1302 list_del_init(&scb->list);
1304 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1306 scb->state = SCB_ACTIVE;
1307 scb->scp = scp;
1308 scb->dma_type = MRAID_DMA_NONE;
1310 return scb;
1315 * megaraid_dealloc_scb - return the scb to the free pool
1316 * @adapter : controller's soft state
1317 * @scb : scb to be freed
1319 * Return the scb back to the free list of scbs. The caller must 'flush' the
1320 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1321 * NOTE NOTE: Make sure the scb is not on any list before calling this
1322 * routine.
1324 static inline void
1325 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1327 unsigned long flags;
1329 // put scb in the free pool
1330 scb->state = SCB_FREE;
1331 scb->scp = NULL;
1332 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1334 list_add(&scb->list, &adapter->kscb_pool);
1336 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1338 return;
1343 * megaraid_mbox_mksgl - make the scatter-gather list
1344 * @adapter : controller's soft state
1345 * @scb : scsi control block
1347 * Prepare the scatter-gather list.
1349 static int
1350 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1352 struct scatterlist *sgl;
1353 mbox_ccb_t *ccb;
1354 struct scsi_cmnd *scp;
1355 int sgcnt;
1356 int i;
1359 scp = scb->scp;
1360 ccb = (mbox_ccb_t *)scb->ccb;
1362 sgcnt = scsi_dma_map(scp);
1363 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1365 // no mapping required if no data to be transferred
1366 if (!sgcnt)
1367 return 0;
1369 scb->dma_type = MRAID_DMA_WSG;
1371 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1372 ccb->sgl64[i].address = sg_dma_address(sgl);
1373 ccb->sgl64[i].length = sg_dma_len(sgl);
1376 // Return count of SG nodes
1377 return sgcnt;
1382 * mbox_post_cmd - issue a mailbox command
1383 * @adapter : controller's soft state
1384 * @scb : command to be issued
1386 * Post the command to the controller if mailbox is available.
1388 static int
1389 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1391 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1392 mbox64_t *mbox64;
1393 mbox_t *mbox;
1394 mbox_ccb_t *ccb;
1395 unsigned long flags;
1396 unsigned int i = 0;
1399 ccb = (mbox_ccb_t *)scb->ccb;
1400 mbox = raid_dev->mbox;
1401 mbox64 = raid_dev->mbox64;
1404 * Check for busy mailbox. If it is, return failure - the caller
1405 * should retry later.
1407 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1409 if (unlikely(mbox->busy)) {
1410 do {
1411 udelay(1);
1412 i++;
1413 rmb();
1414 } while(mbox->busy && (i < max_mbox_busy_wait));
1416 if (mbox->busy) {
1418 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1420 return -1;
1425 // Copy this command's mailbox data into "adapter's" mailbox
1426 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1427 mbox->cmdid = scb->sno;
1429 adapter->outstanding_cmds++;
1431 if (scb->dma_direction == PCI_DMA_TODEVICE)
1432 pci_dma_sync_sg_for_device(adapter->pdev,
1433 scsi_sglist(scb->scp),
1434 scsi_sg_count(scb->scp),
1435 PCI_DMA_TODEVICE);
1437 mbox->busy = 1; // Set busy
1438 mbox->poll = 0;
1439 mbox->ack = 0;
1440 wmb();
1442 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1444 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1446 return 0;
1451 * megaraid_queue_command - generic queue entry point for all LLDs
1452 * @scp : pointer to the scsi command to be executed
1453 * @done : callback routine to be called after the cmd has be completed
1455 * Queue entry point for mailbox based controllers.
1457 static int
1458 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1460 adapter_t *adapter;
1461 scb_t *scb;
1462 int if_busy;
1464 adapter = SCP2ADAPTER(scp);
1465 scp->scsi_done = done;
1466 scp->result = 0;
1469 * Allocate and build a SCB request
1470 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1471 * not allocate scb. We will return non-zero status in that case.
1472 * NOTE: scb can be null even though certain commands completed
1473 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1474 * return 0 in that case, and we would do the callback right away.
1476 if_busy = 0;
1477 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1478 if (!scb) { // command already completed
1479 done(scp);
1480 return 0;
1483 megaraid_mbox_runpendq(adapter, scb);
1484 return if_busy;
1487 static DEF_SCSI_QCMD(megaraid_queue_command)
1490 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1491 * @adapter : controller's soft state
1492 * @scp : mid-layer scsi command pointer
1493 * @busy : set if request could not be completed because of lack of
1494 * resources
1496 * Transform the mid-layer scsi command to megaraid firmware lingua.
1497 * Convert the command issued by mid-layer to format understood by megaraid
1498 * firmware. We also complete certain commands without sending them to firmware.
1500 static scb_t *
1501 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1503 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1504 int channel;
1505 int target;
1506 int islogical;
1507 mbox_ccb_t *ccb;
1508 mraid_passthru_t *pthru;
1509 mbox64_t *mbox64;
1510 mbox_t *mbox;
1511 scb_t *scb;
1512 char skip[] = "skipping";
1513 char scan[] = "scanning";
1514 char *ss;
1518 * Get the appropriate device map for the device this command is
1519 * intended for
1521 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1524 * Logical drive commands
1526 if (islogical) {
1527 switch (scp->cmnd[0]) {
1528 case TEST_UNIT_READY:
1530 * Do we support clustering and is the support enabled
1531 * If no, return success always
1533 if (!adapter->ha) {
1534 scp->result = (DID_OK << 16);
1535 return NULL;
1538 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1539 scp->result = (DID_ERROR << 16);
1540 *busy = 1;
1541 return NULL;
1544 scb->dma_direction = scp->sc_data_direction;
1545 scb->dev_channel = 0xFF;
1546 scb->dev_target = target;
1547 ccb = (mbox_ccb_t *)scb->ccb;
1550 * The command id will be provided by the command
1551 * issuance routine
1553 ccb->raw_mbox[0] = CLUSTER_CMD;
1554 ccb->raw_mbox[2] = RESERVATION_STATUS;
1555 ccb->raw_mbox[3] = target;
1557 return scb;
1559 case MODE_SENSE:
1561 struct scatterlist *sgl;
1562 caddr_t vaddr;
1564 sgl = scsi_sglist(scp);
1565 if (sg_page(sgl)) {
1566 vaddr = (caddr_t) sg_virt(&sgl[0]);
1568 memset(vaddr, 0, scp->cmnd[4]);
1570 else {
1571 con_log(CL_ANN, (KERN_WARNING
1572 "megaraid mailbox: invalid sg:%d\n",
1573 __LINE__));
1576 scp->result = (DID_OK << 16);
1577 return NULL;
1579 case INQUIRY:
1581 * Display the channel scan for logical drives
1582 * Do not display scan for a channel if already done.
1584 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1586 con_log(CL_ANN, (KERN_INFO
1587 "scsi[%d]: scanning scsi channel %d",
1588 adapter->host->host_no,
1589 SCP2CHANNEL(scp)));
1591 con_log(CL_ANN, (
1592 " [virtual] for logical drives\n"));
1594 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1597 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1598 scp->sense_buffer[0] = 0x70;
1599 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1600 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1601 scp->result = CHECK_CONDITION << 1;
1602 return NULL;
1605 /* Fall through */
1607 case READ_CAPACITY:
1609 * Do not allow LUN > 0 for logical drives and
1610 * requests for more than 40 logical drives
1612 if (SCP2LUN(scp)) {
1613 scp->result = (DID_BAD_TARGET << 16);
1614 return NULL;
1616 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1617 scp->result = (DID_BAD_TARGET << 16);
1618 return NULL;
1622 /* Allocate a SCB and initialize passthru */
1623 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1624 scp->result = (DID_ERROR << 16);
1625 *busy = 1;
1626 return NULL;
1629 ccb = (mbox_ccb_t *)scb->ccb;
1630 scb->dev_channel = 0xFF;
1631 scb->dev_target = target;
1632 pthru = ccb->pthru;
1633 mbox = ccb->mbox;
1634 mbox64 = ccb->mbox64;
1636 pthru->timeout = 0;
1637 pthru->ars = 1;
1638 pthru->reqsenselen = 14;
1639 pthru->islogical = 1;
1640 pthru->logdrv = target;
1641 pthru->cdblen = scp->cmd_len;
1642 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1644 mbox->cmd = MBOXCMD_PASSTHRU64;
1645 scb->dma_direction = scp->sc_data_direction;
1647 pthru->dataxferlen = scsi_bufflen(scp);
1648 pthru->dataxferaddr = ccb->sgl_dma_h;
1649 pthru->numsge = megaraid_mbox_mksgl(adapter,
1650 scb);
1652 mbox->xferaddr = 0xFFFFFFFF;
1653 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1654 mbox64->xferaddr_hi = 0;
1656 return scb;
1658 case READ_6:
1659 case WRITE_6:
1660 case READ_10:
1661 case WRITE_10:
1662 case READ_12:
1663 case WRITE_12:
1666 * Allocate a SCB and initialize mailbox
1668 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1669 scp->result = (DID_ERROR << 16);
1670 *busy = 1;
1671 return NULL;
1673 ccb = (mbox_ccb_t *)scb->ccb;
1674 scb->dev_channel = 0xFF;
1675 scb->dev_target = target;
1676 mbox = ccb->mbox;
1677 mbox64 = ccb->mbox64;
1678 mbox->logdrv = target;
1681 * A little HACK: 2nd bit is zero for all scsi read
1682 * commands and is set for all scsi write commands
1684 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1685 MBOXCMD_LREAD64 ;
1688 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1690 if (scp->cmd_len == 6) {
1691 mbox->numsectors = (uint32_t)scp->cmnd[4];
1692 mbox->lba =
1693 ((uint32_t)scp->cmnd[1] << 16) |
1694 ((uint32_t)scp->cmnd[2] << 8) |
1695 (uint32_t)scp->cmnd[3];
1697 mbox->lba &= 0x1FFFFF;
1701 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1703 else if (scp->cmd_len == 10) {
1704 mbox->numsectors =
1705 (uint32_t)scp->cmnd[8] |
1706 ((uint32_t)scp->cmnd[7] << 8);
1707 mbox->lba =
1708 ((uint32_t)scp->cmnd[2] << 24) |
1709 ((uint32_t)scp->cmnd[3] << 16) |
1710 ((uint32_t)scp->cmnd[4] << 8) |
1711 (uint32_t)scp->cmnd[5];
1715 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1717 else if (scp->cmd_len == 12) {
1718 mbox->lba =
1719 ((uint32_t)scp->cmnd[2] << 24) |
1720 ((uint32_t)scp->cmnd[3] << 16) |
1721 ((uint32_t)scp->cmnd[4] << 8) |
1722 (uint32_t)scp->cmnd[5];
1724 mbox->numsectors =
1725 ((uint32_t)scp->cmnd[6] << 24) |
1726 ((uint32_t)scp->cmnd[7] << 16) |
1727 ((uint32_t)scp->cmnd[8] << 8) |
1728 (uint32_t)scp->cmnd[9];
1730 else {
1731 con_log(CL_ANN, (KERN_WARNING
1732 "megaraid: unsupported CDB length\n"));
1734 megaraid_dealloc_scb(adapter, scb);
1736 scp->result = (DID_ERROR << 16);
1737 return NULL;
1740 scb->dma_direction = scp->sc_data_direction;
1742 // Calculate Scatter-Gather info
1743 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1744 mbox->numsge = megaraid_mbox_mksgl(adapter,
1745 scb);
1746 mbox->xferaddr = 0xFFFFFFFF;
1747 mbox64->xferaddr_hi = 0;
1749 return scb;
1751 case RESERVE:
1752 case RELEASE:
1754 * Do we support clustering and is the support enabled
1756 if (!adapter->ha) {
1757 scp->result = (DID_BAD_TARGET << 16);
1758 return NULL;
1762 * Allocate a SCB and initialize mailbox
1764 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1765 scp->result = (DID_ERROR << 16);
1766 *busy = 1;
1767 return NULL;
1770 ccb = (mbox_ccb_t *)scb->ccb;
1771 scb->dev_channel = 0xFF;
1772 scb->dev_target = target;
1773 ccb->raw_mbox[0] = CLUSTER_CMD;
1774 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1775 RESERVE_LD : RELEASE_LD;
1777 ccb->raw_mbox[3] = target;
1778 scb->dma_direction = scp->sc_data_direction;
1780 return scb;
1782 default:
1783 scp->result = (DID_BAD_TARGET << 16);
1784 return NULL;
1787 else { // Passthru device commands
1789 // Do not allow access to target id > 15 or LUN > 7
1790 if (target > 15 || SCP2LUN(scp) > 7) {
1791 scp->result = (DID_BAD_TARGET << 16);
1792 return NULL;
1795 // if fast load option was set and scan for last device is
1796 // over, reset the fast_load flag so that during a possible
1797 // next scan, devices can be made available
1798 if (rdev->fast_load && (target == 15) &&
1799 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1801 con_log(CL_ANN, (KERN_INFO
1802 "megaraid[%d]: physical device scan re-enabled\n",
1803 adapter->host->host_no));
1804 rdev->fast_load = 0;
1808 * Display the channel scan for physical devices
1810 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1812 ss = rdev->fast_load ? skip : scan;
1814 con_log(CL_ANN, (KERN_INFO
1815 "scsi[%d]: %s scsi channel %d [Phy %d]",
1816 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1817 channel));
1819 con_log(CL_ANN, (
1820 " for non-raid devices\n"));
1822 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1825 // disable channel sweep if fast load option given
1826 if (rdev->fast_load) {
1827 scp->result = (DID_BAD_TARGET << 16);
1828 return NULL;
1831 // Allocate a SCB and initialize passthru
1832 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1833 scp->result = (DID_ERROR << 16);
1834 *busy = 1;
1835 return NULL;
1838 ccb = (mbox_ccb_t *)scb->ccb;
1839 scb->dev_channel = channel;
1840 scb->dev_target = target;
1841 scb->dma_direction = scp->sc_data_direction;
1842 mbox = ccb->mbox;
1843 mbox64 = ccb->mbox64;
1845 // Does this firmware support extended CDBs
1846 if (adapter->max_cdb_sz == 16) {
1847 mbox->cmd = MBOXCMD_EXTPTHRU;
1849 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1851 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1852 mbox64->xferaddr_hi = 0;
1853 mbox->xferaddr = 0xFFFFFFFF;
1855 else {
1856 mbox->cmd = MBOXCMD_PASSTHRU64;
1858 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1860 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1861 mbox64->xferaddr_hi = 0;
1862 mbox->xferaddr = 0xFFFFFFFF;
1864 return scb;
1867 // NOT REACHED
1872 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1873 * @adapter : controller's soft state
1874 * @scb_q : SCB to be queued in the pending list
1876 * Scan the pending list for commands which are not yet issued and try to
1877 * post to the controller. The SCB can be a null pointer, which would indicate
1878 * no SCB to be queue, just try to execute the ones in the pending list.
1880 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1881 * out from the head of the pending list. If it is successfully issued, the
1882 * next SCB is at the head now.
1884 static void
1885 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1887 scb_t *scb;
1888 unsigned long flags;
1890 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1892 if (scb_q) {
1893 scb_q->state = SCB_PENDQ;
1894 list_add_tail(&scb_q->list, &adapter->pend_list);
1897 // if the adapter in not in quiescent mode, post the commands to FW
1898 if (adapter->quiescent) {
1899 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1900 return;
1903 while (!list_empty(&adapter->pend_list)) {
1905 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1907 scb = list_entry(adapter->pend_list.next, scb_t, list);
1909 // remove the scb from the pending list and try to
1910 // issue. If we are unable to issue it, put back in
1911 // the pending list and return
1913 list_del_init(&scb->list);
1915 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1917 // if mailbox was busy, return SCB back to pending
1918 // list. Make sure to add at the head, since that's
1919 // where it would have been removed from
1921 scb->state = SCB_ISSUED;
1923 if (mbox_post_cmd(adapter, scb) != 0) {
1925 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1927 scb->state = SCB_PENDQ;
1929 list_add(&scb->list, &adapter->pend_list);
1931 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1932 flags);
1934 return;
1937 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1940 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1943 return;
1948 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1949 * @adapter : pointer to controller's soft state
1950 * @scb : scsi control block
1951 * @scp : scsi command from the mid-layer
1953 * Prepare a command for the scsi physical devices.
1955 static void
1956 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1957 struct scsi_cmnd *scp)
1959 mbox_ccb_t *ccb;
1960 mraid_passthru_t *pthru;
1961 uint8_t channel;
1962 uint8_t target;
1964 ccb = (mbox_ccb_t *)scb->ccb;
1965 pthru = ccb->pthru;
1966 channel = scb->dev_channel;
1967 target = scb->dev_target;
1969 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1970 pthru->timeout = 4;
1971 pthru->ars = 1;
1972 pthru->islogical = 0;
1973 pthru->channel = 0;
1974 pthru->target = (channel << 4) | target;
1975 pthru->logdrv = SCP2LUN(scp);
1976 pthru->reqsenselen = 14;
1977 pthru->cdblen = scp->cmd_len;
1979 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1981 if (scsi_bufflen(scp)) {
1982 pthru->dataxferlen = scsi_bufflen(scp);
1983 pthru->dataxferaddr = ccb->sgl_dma_h;
1984 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1986 else {
1987 pthru->dataxferaddr = 0;
1988 pthru->dataxferlen = 0;
1989 pthru->numsge = 0;
1991 return;
1996 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1997 * @adapter : pointer to controller's soft state
1998 * @scb : scsi control block
1999 * @scp : scsi command from the mid-layer
2001 * Prepare a command for the scsi physical devices. This routine prepares
2002 * commands for devices which can take extended CDBs (>10 bytes).
2004 static void
2005 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2006 struct scsi_cmnd *scp)
2008 mbox_ccb_t *ccb;
2009 mraid_epassthru_t *epthru;
2010 uint8_t channel;
2011 uint8_t target;
2013 ccb = (mbox_ccb_t *)scb->ccb;
2014 epthru = ccb->epthru;
2015 channel = scb->dev_channel;
2016 target = scb->dev_target;
2018 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2019 epthru->timeout = 4;
2020 epthru->ars = 1;
2021 epthru->islogical = 0;
2022 epthru->channel = 0;
2023 epthru->target = (channel << 4) | target;
2024 epthru->logdrv = SCP2LUN(scp);
2025 epthru->reqsenselen = 14;
2026 epthru->cdblen = scp->cmd_len;
2028 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2030 if (scsi_bufflen(scp)) {
2031 epthru->dataxferlen = scsi_bufflen(scp);
2032 epthru->dataxferaddr = ccb->sgl_dma_h;
2033 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2035 else {
2036 epthru->dataxferaddr = 0;
2037 epthru->dataxferlen = 0;
2038 epthru->numsge = 0;
2040 return;
2045 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2046 * @adapter : controller's soft state
2048 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2049 * completed command and put them on the completed list for later processing.
2051 * Returns: 1 if the interrupt is valid, 0 otherwise
2053 static int
2054 megaraid_ack_sequence(adapter_t *adapter)
2056 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2057 mbox_t *mbox;
2058 scb_t *scb;
2059 uint8_t nstatus;
2060 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2061 struct list_head clist;
2062 int handled;
2063 uint32_t dword;
2064 unsigned long flags;
2065 int i, j;
2068 mbox = raid_dev->mbox;
2070 // move the SCBs from the firmware completed array to our local list
2071 INIT_LIST_HEAD(&clist);
2073 // loop till F/W has more commands for us to complete
2074 handled = 0;
2075 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2076 do {
2078 * Check if a valid interrupt is pending. If found, force the
2079 * interrupt line low.
2081 dword = RDOUTDOOR(raid_dev);
2082 if (dword != 0x10001234) break;
2084 handled = 1;
2086 WROUTDOOR(raid_dev, 0x10001234);
2088 nstatus = 0;
2089 // wait for valid numstatus to post
2090 for (i = 0; i < 0xFFFFF; i++) {
2091 if (mbox->numstatus != 0xFF) {
2092 nstatus = mbox->numstatus;
2093 break;
2095 rmb();
2097 mbox->numstatus = 0xFF;
2099 adapter->outstanding_cmds -= nstatus;
2101 for (i = 0; i < nstatus; i++) {
2103 // wait for valid command index to post
2104 for (j = 0; j < 0xFFFFF; j++) {
2105 if (mbox->completed[i] != 0xFF) break;
2106 rmb();
2108 completed[i] = mbox->completed[i];
2109 mbox->completed[i] = 0xFF;
2111 if (completed[i] == 0xFF) {
2112 con_log(CL_ANN, (KERN_CRIT
2113 "megaraid: command posting timed out\n"));
2115 BUG();
2116 continue;
2119 // Get SCB associated with this command id
2120 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2121 // a cmm command
2122 scb = adapter->uscb_list + (completed[i] -
2123 MBOX_MAX_SCSI_CMDS);
2125 else {
2126 // an os command
2127 scb = adapter->kscb_list + completed[i];
2130 scb->status = mbox->status;
2131 list_add_tail(&scb->list, &clist);
2134 // Acknowledge interrupt
2135 WRINDOOR(raid_dev, 0x02);
2137 } while(1);
2139 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2142 // put the completed commands in the completed list. DPC would
2143 // complete these commands later
2144 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2146 list_splice(&clist, &adapter->completed_list);
2148 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2151 // schedule the DPC if there is some work for it
2152 if (handled)
2153 tasklet_schedule(&adapter->dpc_h);
2155 return handled;
2160 * megaraid_isr - isr for memory based mailbox based controllers
2161 * @irq : irq
2162 * @devp : pointer to our soft state
2164 * Interrupt service routine for memory-mapped mailbox controllers.
2166 static irqreturn_t
2167 megaraid_isr(int irq, void *devp)
2169 adapter_t *adapter = devp;
2170 int handled;
2172 handled = megaraid_ack_sequence(adapter);
2174 /* Loop through any pending requests */
2175 if (!adapter->quiescent) {
2176 megaraid_mbox_runpendq(adapter, NULL);
2179 return IRQ_RETVAL(handled);
2184 * megaraid_mbox_sync_scb - sync kernel buffers
2185 * @adapter : controller's soft state
2186 * @scb : pointer to the resource packet
2188 * DMA sync if required.
2190 static void
2191 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2193 mbox_ccb_t *ccb;
2195 ccb = (mbox_ccb_t *)scb->ccb;
2197 if (scb->dma_direction == PCI_DMA_FROMDEVICE)
2198 pci_dma_sync_sg_for_cpu(adapter->pdev,
2199 scsi_sglist(scb->scp),
2200 scsi_sg_count(scb->scp),
2201 PCI_DMA_FROMDEVICE);
2203 scsi_dma_unmap(scb->scp);
2204 return;
2209 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2210 * @devp : pointer to HBA soft state
2212 * Pick up the commands from the completed list and send back to the owners.
2213 * This is a reentrant function and does not assume any locks are held while
2214 * it is being called.
2216 static void
2217 megaraid_mbox_dpc(unsigned long devp)
2219 adapter_t *adapter = (adapter_t *)devp;
2220 mraid_device_t *raid_dev;
2221 struct list_head clist;
2222 struct scatterlist *sgl;
2223 scb_t *scb;
2224 scb_t *tmp;
2225 struct scsi_cmnd *scp;
2226 mraid_passthru_t *pthru;
2227 mraid_epassthru_t *epthru;
2228 mbox_ccb_t *ccb;
2229 int islogical;
2230 int pdev_index;
2231 int pdev_state;
2232 mbox_t *mbox;
2233 unsigned long flags;
2234 uint8_t c;
2235 int status;
2236 uioc_t *kioc;
2239 if (!adapter) return;
2241 raid_dev = ADAP2RAIDDEV(adapter);
2243 // move the SCBs from the completed list to our local list
2244 INIT_LIST_HEAD(&clist);
2246 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2248 list_splice_init(&adapter->completed_list, &clist);
2250 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2253 list_for_each_entry_safe(scb, tmp, &clist, list) {
2255 status = scb->status;
2256 scp = scb->scp;
2257 ccb = (mbox_ccb_t *)scb->ccb;
2258 pthru = ccb->pthru;
2259 epthru = ccb->epthru;
2260 mbox = ccb->mbox;
2262 // Make sure f/w has completed a valid command
2263 if (scb->state != SCB_ISSUED) {
2264 con_log(CL_ANN, (KERN_CRIT
2265 "megaraid critical err: invalid command %d:%d:%p\n",
2266 scb->sno, scb->state, scp));
2267 BUG();
2268 continue; // Must never happen!
2271 // check for the management command and complete it right away
2272 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2273 scb->state = SCB_FREE;
2274 scb->status = status;
2276 // remove from local clist
2277 list_del_init(&scb->list);
2279 kioc = (uioc_t *)scb->gp;
2280 kioc->status = 0;
2282 megaraid_mbox_mm_done(adapter, scb);
2284 continue;
2287 // Was an abort issued for this command earlier
2288 if (scb->state & SCB_ABORT) {
2289 con_log(CL_ANN, (KERN_NOTICE
2290 "megaraid: aborted cmd [%x] completed\n",
2291 scb->sno));
2295 * If the inquiry came of a disk drive which is not part of
2296 * any RAID array, expose it to the kernel. For this to be
2297 * enabled, user must set the "megaraid_expose_unconf_disks"
2298 * flag to 1 by specifying it on module parameter list.
2299 * This would enable data migration off drives from other
2300 * configurations.
2302 islogical = MRAID_IS_LOGICAL(adapter, scp);
2303 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2304 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2306 sgl = scsi_sglist(scp);
2307 if (sg_page(sgl)) {
2308 c = *(unsigned char *) sg_virt(&sgl[0]);
2309 } else {
2310 con_log(CL_ANN, (KERN_WARNING
2311 "megaraid mailbox: invalid sg:%d\n",
2312 __LINE__));
2313 c = 0;
2316 if ((c & 0x1F ) == TYPE_DISK) {
2317 pdev_index = (scb->dev_channel * 16) +
2318 scb->dev_target;
2319 pdev_state =
2320 raid_dev->pdrv_state[pdev_index] & 0x0F;
2322 if (pdev_state == PDRV_ONLINE ||
2323 pdev_state == PDRV_FAILED ||
2324 pdev_state == PDRV_RBLD ||
2325 pdev_state == PDRV_HOTSPARE ||
2326 megaraid_expose_unconf_disks == 0) {
2328 status = 0xF0;
2333 // Convert MegaRAID status to Linux error code
2334 switch (status) {
2336 case 0x00:
2338 scp->result = (DID_OK << 16);
2339 break;
2341 case 0x02:
2343 /* set sense_buffer and result fields */
2344 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2345 mbox->cmd == MBOXCMD_PASSTHRU64) {
2347 memcpy(scp->sense_buffer, pthru->reqsensearea,
2348 14);
2350 scp->result = DRIVER_SENSE << 24 |
2351 DID_OK << 16 | CHECK_CONDITION << 1;
2353 else {
2354 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2356 memcpy(scp->sense_buffer,
2357 epthru->reqsensearea, 14);
2359 scp->result = DRIVER_SENSE << 24 |
2360 DID_OK << 16 |
2361 CHECK_CONDITION << 1;
2362 } else {
2363 scp->sense_buffer[0] = 0x70;
2364 scp->sense_buffer[2] = ABORTED_COMMAND;
2365 scp->result = CHECK_CONDITION << 1;
2368 break;
2370 case 0x08:
2372 scp->result = DID_BUS_BUSY << 16 | status;
2373 break;
2375 default:
2378 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2379 * failed
2381 if (scp->cmnd[0] == TEST_UNIT_READY) {
2382 scp->result = DID_ERROR << 16 |
2383 RESERVATION_CONFLICT << 1;
2385 else
2387 * Error code returned is 1 if Reserve or Release
2388 * failed or the input parameter is invalid
2390 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2391 scp->cmnd[0] == RELEASE)) {
2393 scp->result = DID_ERROR << 16 |
2394 RESERVATION_CONFLICT << 1;
2396 else {
2397 scp->result = DID_BAD_TARGET << 16 | status;
2401 // print a debug message for all failed commands
2402 if (status) {
2403 megaraid_mbox_display_scb(adapter, scb);
2406 // Free our internal resources and call the mid-layer callback
2407 // routine
2408 megaraid_mbox_sync_scb(adapter, scb);
2410 // remove from local clist
2411 list_del_init(&scb->list);
2413 // put back in free list
2414 megaraid_dealloc_scb(adapter, scb);
2416 // send the scsi packet back to kernel
2417 scp->scsi_done(scp);
2420 return;
2425 * megaraid_abort_handler - abort the scsi command
2426 * @scp : command to be aborted
2428 * Abort a previous SCSI request. Only commands on the pending list can be
2429 * aborted. All the commands issued to the F/W must complete.
2431 static int
2432 megaraid_abort_handler(struct scsi_cmnd *scp)
2434 adapter_t *adapter;
2435 mraid_device_t *raid_dev;
2436 scb_t *scb;
2437 scb_t *tmp;
2438 int found;
2439 unsigned long flags;
2440 int i;
2443 adapter = SCP2ADAPTER(scp);
2444 raid_dev = ADAP2RAIDDEV(adapter);
2446 con_log(CL_ANN, (KERN_WARNING
2447 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2448 scp->cmnd[0], SCP2CHANNEL(scp),
2449 SCP2TARGET(scp), SCP2LUN(scp)));
2451 // If FW has stopped responding, simply return failure
2452 if (raid_dev->hw_error) {
2453 con_log(CL_ANN, (KERN_NOTICE
2454 "megaraid: hw error, not aborting\n"));
2455 return FAILED;
2458 // There might a race here, where the command was completed by the
2459 // firmware and now it is on the completed list. Before we could
2460 // complete the command to the kernel in dpc, the abort came.
2461 // Find out if this is the case to avoid the race.
2462 scb = NULL;
2463 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2464 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2466 if (scb->scp == scp) { // Found command
2468 list_del_init(&scb->list); // from completed list
2470 con_log(CL_ANN, (KERN_WARNING
2471 "megaraid: %d[%d:%d], abort from completed list\n",
2472 scb->sno, scb->dev_channel, scb->dev_target));
2474 scp->result = (DID_ABORT << 16);
2475 scp->scsi_done(scp);
2477 megaraid_dealloc_scb(adapter, scb);
2479 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2480 flags);
2482 return SUCCESS;
2485 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2488 // Find out if this command is still on the pending list. If it is and
2489 // was never issued, abort and return success. If the command is owned
2490 // by the firmware, we must wait for it to complete by the FW.
2491 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2492 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2494 if (scb->scp == scp) { // Found command
2496 list_del_init(&scb->list); // from pending list
2498 ASSERT(!(scb->state & SCB_ISSUED));
2500 con_log(CL_ANN, (KERN_WARNING
2501 "megaraid abort: [%d:%d], driver owner\n",
2502 scb->dev_channel, scb->dev_target));
2504 scp->result = (DID_ABORT << 16);
2505 scp->scsi_done(scp);
2507 megaraid_dealloc_scb(adapter, scb);
2509 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2510 flags);
2512 return SUCCESS;
2515 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2518 // Check do we even own this command, in which case this would be
2519 // owned by the firmware. The only way to locate the FW scb is to
2520 // traverse through the list of all SCB, since driver does not
2521 // maintain these SCBs on any list
2522 found = 0;
2523 spin_lock_irq(&adapter->lock);
2524 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2525 scb = adapter->kscb_list + i;
2527 if (scb->scp == scp) {
2529 found = 1;
2531 if (!(scb->state & SCB_ISSUED)) {
2532 con_log(CL_ANN, (KERN_WARNING
2533 "megaraid abort: %d[%d:%d], invalid state\n",
2534 scb->sno, scb->dev_channel, scb->dev_target));
2535 BUG();
2537 else {
2538 con_log(CL_ANN, (KERN_WARNING
2539 "megaraid abort: %d[%d:%d], fw owner\n",
2540 scb->sno, scb->dev_channel, scb->dev_target));
2544 spin_unlock_irq(&adapter->lock);
2546 if (!found) {
2547 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2549 // FIXME: Should there be a callback for this command?
2550 return SUCCESS;
2553 // We cannot actually abort a command owned by firmware, return
2554 // failure and wait for reset. In host reset handler, we will find out
2555 // if the HBA is still live
2556 return FAILED;
2560 * megaraid_reset_handler - device reset handler for mailbox based driver
2561 * @scp : reference command
2563 * Reset handler for the mailbox based controller. First try to find out if
2564 * the FW is still live, in which case the outstanding commands counter mut go
2565 * down to 0. If that happens, also issue the reservation reset command to
2566 * relinquish (possible) reservations on the logical drives connected to this
2567 * host.
2569 static int
2570 megaraid_reset_handler(struct scsi_cmnd *scp)
2572 adapter_t *adapter;
2573 scb_t *scb;
2574 scb_t *tmp;
2575 mraid_device_t *raid_dev;
2576 unsigned long flags;
2577 uint8_t raw_mbox[sizeof(mbox_t)];
2578 int rval;
2579 int recovery_window;
2580 int recovering;
2581 int i;
2582 uioc_t *kioc;
2584 adapter = SCP2ADAPTER(scp);
2585 raid_dev = ADAP2RAIDDEV(adapter);
2587 // return failure if adapter is not responding
2588 if (raid_dev->hw_error) {
2589 con_log(CL_ANN, (KERN_NOTICE
2590 "megaraid: hw error, cannot reset\n"));
2591 return FAILED;
2595 // Under exceptional conditions, FW can take up to 3 minutes to
2596 // complete command processing. Wait for additional 2 minutes for the
2597 // pending commands counter to go down to 0. If it doesn't, let the
2598 // controller be marked offline
2599 // Also, reset all the commands currently owned by the driver
2600 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2601 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2602 list_del_init(&scb->list); // from pending list
2604 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2605 con_log(CL_ANN, (KERN_WARNING
2606 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2607 scb->sno, scb->dev_channel, scb->dev_target));
2609 scb->status = -1;
2611 kioc = (uioc_t *)scb->gp;
2612 kioc->status = -EFAULT;
2614 megaraid_mbox_mm_done(adapter, scb);
2615 } else {
2616 if (scb->scp == scp) { // Found command
2617 con_log(CL_ANN, (KERN_WARNING
2618 "megaraid: %d[%d:%d], reset from pending list\n",
2619 scb->sno, scb->dev_channel, scb->dev_target));
2620 } else {
2621 con_log(CL_ANN, (KERN_WARNING
2622 "megaraid: IO packet with %d[%d:%d] being reset\n",
2623 scb->sno, scb->dev_channel, scb->dev_target));
2626 scb->scp->result = (DID_RESET << 16);
2627 scb->scp->scsi_done(scb->scp);
2629 megaraid_dealloc_scb(adapter, scb);
2632 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2634 if (adapter->outstanding_cmds) {
2635 con_log(CL_ANN, (KERN_NOTICE
2636 "megaraid: %d outstanding commands. Max wait %d sec\n",
2637 adapter->outstanding_cmds,
2638 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2641 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2643 recovering = adapter->outstanding_cmds;
2645 for (i = 0; i < recovery_window; i++) {
2647 megaraid_ack_sequence(adapter);
2649 // print a message once every 5 seconds only
2650 if (!(i % 5)) {
2651 con_log(CL_ANN, (
2652 "megaraid mbox: Wait for %d commands to complete:%d\n",
2653 adapter->outstanding_cmds,
2654 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2657 // bailout if no recovery happened in reset time
2658 if (adapter->outstanding_cmds == 0) {
2659 break;
2662 msleep(1000);
2665 spin_lock(&adapter->lock);
2667 // If still outstanding commands, bail out
2668 if (adapter->outstanding_cmds) {
2669 con_log(CL_ANN, (KERN_WARNING
2670 "megaraid mbox: critical hardware error!\n"));
2672 raid_dev->hw_error = 1;
2674 rval = FAILED;
2675 goto out;
2677 else {
2678 con_log(CL_ANN, (KERN_NOTICE
2679 "megaraid mbox: reset sequence completed successfully\n"));
2683 // If the controller supports clustering, reset reservations
2684 if (!adapter->ha) {
2685 rval = SUCCESS;
2686 goto out;
2689 // clear reservations if any
2690 raw_mbox[0] = CLUSTER_CMD;
2691 raw_mbox[2] = RESET_RESERVATIONS;
2693 rval = SUCCESS;
2694 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2695 con_log(CL_ANN,
2696 (KERN_INFO "megaraid: reservation reset\n"));
2698 else {
2699 rval = FAILED;
2700 con_log(CL_ANN, (KERN_WARNING
2701 "megaraid: reservation reset failed\n"));
2704 out:
2705 spin_unlock(&adapter->lock);
2706 return rval;
2710 * START: internal commands library
2712 * This section of the driver has the common routine used by the driver and
2713 * also has all the FW routines
2717 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2718 * @adapter : controller's soft state
2719 * @raw_mbox : the mailbox
2721 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2722 * controllers.
2724 static int
2725 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2727 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2728 mbox64_t *mbox64;
2729 mbox_t *mbox;
2730 uint8_t status;
2731 int i;
2734 mbox64 = raid_dev->mbox64;
2735 mbox = raid_dev->mbox;
2738 * Wait until mailbox is free
2740 if (megaraid_busywait_mbox(raid_dev) != 0)
2741 goto blocked_mailbox;
2744 * Copy mailbox data into host structure
2746 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2747 mbox->cmdid = 0xFE;
2748 mbox->busy = 1;
2749 mbox->poll = 0;
2750 mbox->ack = 0;
2751 mbox->numstatus = 0xFF;
2752 mbox->status = 0xFF;
2754 wmb();
2755 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2757 // wait for maximum 1 second for status to post. If the status is not
2758 // available within 1 second, assume FW is initializing and wait
2759 // for an extended amount of time
2760 if (mbox->numstatus == 0xFF) { // status not yet available
2761 udelay(25);
2763 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2764 rmb();
2765 msleep(1);
2769 if (i == 1000) {
2770 con_log(CL_ANN, (KERN_NOTICE
2771 "megaraid mailbox: wait for FW to boot "));
2773 for (i = 0; (mbox->numstatus == 0xFF) &&
2774 (i < MBOX_RESET_WAIT); i++) {
2775 rmb();
2776 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2777 MBOX_RESET_WAIT - i));
2778 msleep(1000);
2781 if (i == MBOX_RESET_WAIT) {
2783 con_log(CL_ANN, (
2784 "\nmegaraid mailbox: status not available\n"));
2786 return -1;
2788 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2792 // wait for maximum 1 second for poll semaphore
2793 if (mbox->poll != 0x77) {
2794 udelay(25);
2796 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2797 rmb();
2798 msleep(1);
2801 if (i == 1000) {
2802 con_log(CL_ANN, (KERN_WARNING
2803 "megaraid mailbox: could not get poll semaphore\n"));
2804 return -1;
2808 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2809 wmb();
2811 // wait for maximum 1 second for acknowledgement
2812 if (RDINDOOR(raid_dev) & 0x2) {
2813 udelay(25);
2815 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2816 rmb();
2817 msleep(1);
2820 if (i == 1000) {
2821 con_log(CL_ANN, (KERN_WARNING
2822 "megaraid mailbox: could not acknowledge\n"));
2823 return -1;
2826 mbox->poll = 0;
2827 mbox->ack = 0x77;
2829 status = mbox->status;
2831 // invalidate the completed command id array. After command
2832 // completion, firmware would write the valid id.
2833 mbox->numstatus = 0xFF;
2834 mbox->status = 0xFF;
2835 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2836 mbox->completed[i] = 0xFF;
2839 return status;
2841 blocked_mailbox:
2843 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2844 return -1;
2849 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2850 * @adapter : controller's soft state
2851 * @raw_mbox : the mailbox
2853 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2854 * controllers. This is a faster version of the synchronous command and
2855 * therefore can be called in interrupt-context as well.
2857 static int
2858 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2860 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2861 mbox_t *mbox;
2862 long i;
2865 mbox = raid_dev->mbox;
2867 // return immediately if the mailbox is busy
2868 if (mbox->busy) return -1;
2870 // Copy mailbox data into host structure
2871 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2872 mbox->cmdid = 0xFE;
2873 mbox->busy = 1;
2874 mbox->poll = 0;
2875 mbox->ack = 0;
2876 mbox->numstatus = 0xFF;
2877 mbox->status = 0xFF;
2879 wmb();
2880 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2882 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2883 if (mbox->numstatus != 0xFF) break;
2884 rmb();
2885 udelay(MBOX_SYNC_DELAY_200);
2888 if (i == MBOX_SYNC_WAIT_CNT) {
2889 // We may need to re-calibrate the counter
2890 con_log(CL_ANN, (KERN_CRIT
2891 "megaraid: fast sync command timed out\n"));
2894 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2895 wmb();
2897 return mbox->status;
2902 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2903 * @raid_dev : RAID device (HBA) soft state
2905 * Wait until the controller's mailbox is available to accept more commands.
2906 * Wait for at most 1 second.
2908 static int
2909 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2911 mbox_t *mbox = raid_dev->mbox;
2912 int i = 0;
2914 if (mbox->busy) {
2915 udelay(25);
2916 for (i = 0; mbox->busy && i < 1000; i++)
2917 msleep(1);
2920 if (i < 1000) return 0;
2921 else return -1;
2926 * megaraid_mbox_product_info - some static information about the controller
2927 * @adapter : our soft state
2929 * Issue commands to the controller to grab some parameters required by our
2930 * caller.
2932 static int
2933 megaraid_mbox_product_info(adapter_t *adapter)
2935 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2936 mbox_t *mbox;
2937 uint8_t raw_mbox[sizeof(mbox_t)];
2938 mraid_pinfo_t *pinfo;
2939 dma_addr_t pinfo_dma_h;
2940 mraid_inquiry3_t *mraid_inq3;
2941 int i;
2944 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2945 mbox = (mbox_t *)raw_mbox;
2948 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2949 * e.g., max channels, max commands etc.
2951 pinfo = pci_zalloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
2952 &pinfo_dma_h);
2954 if (pinfo == NULL) {
2955 con_log(CL_ANN, (KERN_WARNING
2956 "megaraid: out of memory, %s %d\n", __func__,
2957 __LINE__));
2959 return -1;
2962 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2963 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2965 raw_mbox[0] = FC_NEW_CONFIG;
2966 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2967 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2969 // Issue the command
2970 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2972 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2974 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
2975 pinfo, pinfo_dma_h);
2977 return -1;
2981 * Collect information about state of each physical drive
2982 * attached to the controller. We will expose all the disks
2983 * which are not part of RAID
2985 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2986 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2987 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2991 * Get product info for information like number of channels,
2992 * maximum commands supported.
2994 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2995 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2997 raw_mbox[0] = FC_NEW_CONFIG;
2998 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3000 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3002 con_log(CL_ANN, (KERN_WARNING
3003 "megaraid: product info failed\n"));
3005 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3006 pinfo, pinfo_dma_h);
3008 return -1;
3012 * Setup some parameters for host, as required by our caller
3014 adapter->max_channel = pinfo->nchannels;
3017 * we will export all the logical drives on a single channel.
3018 * Add 1 since inquires do not come for inititor ID
3020 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3021 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3024 * These are the maximum outstanding commands for the scsi-layer
3026 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3028 memset(adapter->fw_version, 0, VERSION_SIZE);
3029 memset(adapter->bios_version, 0, VERSION_SIZE);
3031 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3032 adapter->fw_version[4] = 0;
3034 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3035 adapter->bios_version[4] = 0;
3037 con_log(CL_ANN, (KERN_NOTICE
3038 "megaraid: fw version:[%s] bios version:[%s]\n",
3039 adapter->fw_version, adapter->bios_version));
3041 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3042 pinfo_dma_h);
3044 return 0;
3050 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3051 * @adapter : soft state for the controller
3053 * This routine check whether the controller in question supports extended
3054 * ( > 10 bytes ) CDBs.
3056 static int
3057 megaraid_mbox_extended_cdb(adapter_t *adapter)
3059 mbox_t *mbox;
3060 uint8_t raw_mbox[sizeof(mbox_t)];
3061 int rval;
3063 mbox = (mbox_t *)raw_mbox;
3065 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3066 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3068 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3070 raw_mbox[0] = MAIN_MISC_OPCODE;
3071 raw_mbox[2] = SUPPORT_EXT_CDB;
3074 * Issue the command
3076 rval = 0;
3077 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3078 rval = -1;
3081 return rval;
3086 * megaraid_mbox_support_ha - Do we support clustering
3087 * @adapter : soft state for the controller
3088 * @init_id : ID of the initiator
3090 * Determine if the firmware supports clustering and the ID of the initiator.
3092 static int
3093 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3095 mbox_t *mbox;
3096 uint8_t raw_mbox[sizeof(mbox_t)];
3097 int rval;
3100 mbox = (mbox_t *)raw_mbox;
3102 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3104 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3106 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3108 raw_mbox[0] = GET_TARGET_ID;
3110 // Issue the command
3111 *init_id = 7;
3112 rval = -1;
3113 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3115 *init_id = *(uint8_t *)adapter->ibuf;
3117 con_log(CL_ANN, (KERN_INFO
3118 "megaraid: cluster firmware, initiator ID: %d\n",
3119 *init_id));
3121 rval = 0;
3124 return rval;
3129 * megaraid_mbox_support_random_del - Do we support random deletion
3130 * @adapter : soft state for the controller
3132 * Determine if the firmware supports random deletion.
3133 * Return: 1 is operation supported, 0 otherwise
3135 static int
3136 megaraid_mbox_support_random_del(adapter_t *adapter)
3138 mbox_t *mbox;
3139 uint8_t raw_mbox[sizeof(mbox_t)];
3140 int rval;
3143 * Newer firmware on Dell CERC expect a different
3144 * random deletion handling, so disable it.
3146 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3147 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3148 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3149 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3150 (adapter->fw_version[0] > '6' ||
3151 (adapter->fw_version[0] == '6' &&
3152 adapter->fw_version[2] > '6') ||
3153 (adapter->fw_version[0] == '6'
3154 && adapter->fw_version[2] == '6'
3155 && adapter->fw_version[3] > '1'))) {
3156 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3157 return 0;
3160 mbox = (mbox_t *)raw_mbox;
3162 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3164 raw_mbox[0] = FC_DEL_LOGDRV;
3165 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3167 // Issue the command
3168 rval = 0;
3169 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3171 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3173 rval = 1;
3176 return rval;
3181 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3182 * @adapter : soft state for the controller
3184 * Find out the maximum number of scatter-gather elements supported by the
3185 * firmware.
3187 static int
3188 megaraid_mbox_get_max_sg(adapter_t *adapter)
3190 mbox_t *mbox;
3191 uint8_t raw_mbox[sizeof(mbox_t)];
3192 int nsg;
3195 mbox = (mbox_t *)raw_mbox;
3197 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3199 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3201 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3203 raw_mbox[0] = MAIN_MISC_OPCODE;
3204 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3206 // Issue the command
3207 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3208 nsg = *(uint8_t *)adapter->ibuf;
3210 else {
3211 nsg = MBOX_DEFAULT_SG_SIZE;
3214 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3216 return nsg;
3221 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3222 * @adapter : soft state for the controller
3224 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3225 * can be exported as regular SCSI channels.
3227 static void
3228 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3230 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3231 mbox_t *mbox;
3232 uint8_t raw_mbox[sizeof(mbox_t)];
3235 mbox = (mbox_t *)raw_mbox;
3237 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3239 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3241 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3243 raw_mbox[0] = CHNL_CLASS;
3244 raw_mbox[2] = GET_CHNL_CLASS;
3246 // Issue the command. If the command fails, all channels are RAID
3247 // channels
3248 raid_dev->channel_class = 0xFF;
3249 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3250 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3253 return;
3258 * megaraid_mbox_flush_cache - flush adapter and disks cache
3259 * @adapter : soft state for the controller
3261 * Flush adapter cache followed by disks cache.
3263 static void
3264 megaraid_mbox_flush_cache(adapter_t *adapter)
3266 mbox_t *mbox;
3267 uint8_t raw_mbox[sizeof(mbox_t)];
3270 mbox = (mbox_t *)raw_mbox;
3272 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3274 raw_mbox[0] = FLUSH_ADAPTER;
3276 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3277 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3280 raw_mbox[0] = FLUSH_SYSTEM;
3282 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3283 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3286 return;
3291 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3292 * @adapter : soft state for the controller
3294 * Clears the pending cmds in FW and reinits its RAID structs.
3296 static int
3297 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3299 mbox_t *mbox;
3300 uint8_t raw_mbox[sizeof(mbox_t)];
3301 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3302 mbox64_t *mbox64;
3303 int status = 0;
3304 int i;
3305 uint32_t dword;
3307 mbox = (mbox_t *)raw_mbox;
3309 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3311 raw_mbox[0] = 0xFF;
3313 mbox64 = raid_dev->mbox64;
3314 mbox = raid_dev->mbox;
3316 /* Wait until mailbox is free */
3317 if (megaraid_busywait_mbox(raid_dev) != 0) {
3318 status = 1;
3319 goto blocked_mailbox;
3322 /* Copy mailbox data into host structure */
3323 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3324 mbox->cmdid = 0xFE;
3325 mbox->busy = 1;
3326 mbox->poll = 0;
3327 mbox->ack = 0;
3328 mbox->numstatus = 0;
3329 mbox->status = 0;
3331 wmb();
3332 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3334 /* Wait for maximum 1 min for status to post.
3335 * If the Firmware SUPPORTS the ABOVE COMMAND,
3336 * mbox->cmd will be set to 0
3337 * else
3338 * the firmware will reject the command with
3339 * mbox->numstatus set to 1
3342 i = 0;
3343 status = 0;
3344 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3345 rmb();
3346 msleep(1);
3347 i++;
3348 if (i > 1000 * 60) {
3349 status = 1;
3350 break;
3353 if (mbox->numstatus == 1)
3354 status = 1; /*cmd not supported*/
3356 /* Check for interrupt line */
3357 dword = RDOUTDOOR(raid_dev);
3358 WROUTDOOR(raid_dev, dword);
3359 WRINDOOR(raid_dev,2);
3361 return status;
3363 blocked_mailbox:
3364 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3365 return status;
3369 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3370 * @adapter : controller's soft state
3371 * @scb : SCB to be displayed
3372 * @level : debug level for console print
3374 * Diplay information about the given SCB iff the current debug level is
3375 * verbose.
3377 static void
3378 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3380 mbox_ccb_t *ccb;
3381 struct scsi_cmnd *scp;
3382 mbox_t *mbox;
3383 int level;
3384 int i;
3387 ccb = (mbox_ccb_t *)scb->ccb;
3388 scp = scb->scp;
3389 mbox = ccb->mbox;
3391 level = CL_DLEVEL3;
3393 con_log(level, (KERN_NOTICE
3394 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3395 mbox->cmd, scb->sno));
3397 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3398 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3399 mbox->numsge));
3401 if (!scp) return;
3403 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3405 for (i = 0; i < scp->cmd_len; i++) {
3406 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3409 con_log(level, ("\n"));
3411 return;
3416 * megaraid_mbox_setup_device_map - manage device ids
3417 * @adapter : Driver's soft state
3419 * Manage the device ids to have an appropriate mapping between the kernel
3420 * scsi addresses and megaraid scsi and logical drive addresses. We export
3421 * scsi devices on their actual addresses, whereas the logical drives are
3422 * exported on a virtual scsi channel.
3424 static void
3425 megaraid_mbox_setup_device_map(adapter_t *adapter)
3427 uint8_t c;
3428 uint8_t t;
3431 * First fill the values on the logical drive channel
3433 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3434 adapter->device_ids[adapter->max_channel][t] =
3435 (t < adapter->init_id) ? t : t - 1;
3437 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3440 * Fill the values on the physical devices channels
3442 for (c = 0; c < adapter->max_channel; c++)
3443 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3444 adapter->device_ids[c][t] = (c << 8) | t;
3449 * END: internal commands library
3453 * START: Interface for the common management module
3455 * This is the module, which interfaces with the common management module to
3456 * provide support for ioctl and sysfs
3460 * megaraid_cmm_register - register with the management module
3461 * @adapter : HBA soft state
3463 * Register with the management module, which allows applications to issue
3464 * ioctl calls to the drivers. This interface is used by the management module
3465 * to setup sysfs support as well.
3467 static int
3468 megaraid_cmm_register(adapter_t *adapter)
3470 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3471 mraid_mmadp_t adp;
3472 scb_t *scb;
3473 mbox_ccb_t *ccb;
3474 int rval;
3475 int i;
3477 // Allocate memory for the base list of scb for management module.
3478 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3480 if (adapter->uscb_list == NULL) {
3481 con_log(CL_ANN, (KERN_WARNING
3482 "megaraid: out of memory, %s %d\n", __func__,
3483 __LINE__));
3484 return -1;
3488 // Initialize the synchronization parameters for resources for
3489 // commands for management module
3490 INIT_LIST_HEAD(&adapter->uscb_pool);
3492 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3496 // link all the packets. Note, CCB for commands, coming from the
3497 // commom management module, mailbox physical address are already
3498 // setup by it. We just need placeholder for that in our local command
3499 // control blocks
3500 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3502 scb = adapter->uscb_list + i;
3503 ccb = raid_dev->uccb_list + i;
3505 scb->ccb = (caddr_t)ccb;
3506 ccb->mbox64 = raid_dev->umbox64 + i;
3507 ccb->mbox = &ccb->mbox64->mbox32;
3508 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3510 scb->gp = 0;
3512 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3513 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3514 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3516 scb->scp = NULL;
3517 scb->state = SCB_FREE;
3518 scb->dma_direction = PCI_DMA_NONE;
3519 scb->dma_type = MRAID_DMA_NONE;
3520 scb->dev_channel = -1;
3521 scb->dev_target = -1;
3523 // put scb in the free pool
3524 list_add_tail(&scb->list, &adapter->uscb_pool);
3527 adp.unique_id = adapter->unique_id;
3528 adp.drvr_type = DRVRTYPE_MBOX;
3529 adp.drvr_data = (unsigned long)adapter;
3530 adp.pdev = adapter->pdev;
3531 adp.issue_uioc = megaraid_mbox_mm_handler;
3532 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3533 adp.max_kioc = MBOX_MAX_USER_CMDS;
3535 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3537 con_log(CL_ANN, (KERN_WARNING
3538 "megaraid mbox: did not register with CMM\n"));
3540 kfree(adapter->uscb_list);
3543 return rval;
3548 * megaraid_cmm_unregister - un-register with the management module
3549 * @adapter : HBA soft state
3551 * Un-register with the management module.
3552 * FIXME: mgmt module must return failure for unregister if it has pending
3553 * commands in LLD.
3555 static int
3556 megaraid_cmm_unregister(adapter_t *adapter)
3558 kfree(adapter->uscb_list);
3559 mraid_mm_unregister_adp(adapter->unique_id);
3560 return 0;
3565 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3566 * @drvr_data : LLD specific data
3567 * @kioc : CMM interface packet
3568 * @action : command action
3570 * This routine is invoked whenever the Common Management Module (CMM) has a
3571 * command for us. The 'action' parameter specifies if this is a new command
3572 * or otherwise.
3574 static int
3575 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3577 adapter_t *adapter;
3579 if (action != IOCTL_ISSUE) {
3580 con_log(CL_ANN, (KERN_WARNING
3581 "megaraid: unsupported management action:%#2x\n",
3582 action));
3583 return (-ENOTSUPP);
3586 adapter = (adapter_t *)drvr_data;
3588 // make sure this adapter is not being detached right now.
3589 if (atomic_read(&adapter->being_detached)) {
3590 con_log(CL_ANN, (KERN_WARNING
3591 "megaraid: reject management request, detaching\n"));
3592 return (-ENODEV);
3595 switch (kioc->opcode) {
3597 case GET_ADAP_INFO:
3599 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3600 (unsigned long)kioc->buf_vaddr);
3602 kioc->done(kioc);
3604 return kioc->status;
3606 case MBOX_CMD:
3608 return megaraid_mbox_mm_command(adapter, kioc);
3610 default:
3611 kioc->status = (-EINVAL);
3612 kioc->done(kioc);
3613 return (-EINVAL);
3616 return 0; // not reached
3620 * megaraid_mbox_mm_command - issues commands routed through CMM
3621 * @adapter : HBA soft state
3622 * @kioc : management command packet
3624 * Issues commands, which are routed through the management module.
3626 static int
3627 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3629 struct list_head *head = &adapter->uscb_pool;
3630 mbox64_t *mbox64;
3631 uint8_t *raw_mbox;
3632 scb_t *scb;
3633 mbox_ccb_t *ccb;
3634 unsigned long flags;
3636 // detach one scb from free pool
3637 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3639 if (list_empty(head)) { // should never happen because of CMM
3641 con_log(CL_ANN, (KERN_WARNING
3642 "megaraid mbox: bug in cmm handler, lost resources\n"));
3644 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3646 return (-EINVAL);
3649 scb = list_entry(head->next, scb_t, list);
3650 list_del_init(&scb->list);
3652 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3654 scb->state = SCB_ACTIVE;
3655 scb->dma_type = MRAID_DMA_NONE;
3656 scb->dma_direction = PCI_DMA_NONE;
3658 ccb = (mbox_ccb_t *)scb->ccb;
3659 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3660 raw_mbox = (uint8_t *)&mbox64->mbox32;
3662 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3664 scb->gp = (unsigned long)kioc;
3667 * If it is a logdrv random delete operation, we have to wait till
3668 * there are no outstanding cmds at the fw and then issue it directly
3670 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3672 if (wait_till_fw_empty(adapter)) {
3673 con_log(CL_ANN, (KERN_NOTICE
3674 "megaraid mbox: LD delete, timed out\n"));
3676 kioc->status = -ETIME;
3678 scb->status = -1;
3680 megaraid_mbox_mm_done(adapter, scb);
3682 return (-ETIME);
3685 INIT_LIST_HEAD(&scb->list);
3687 scb->state = SCB_ISSUED;
3688 if (mbox_post_cmd(adapter, scb) != 0) {
3690 con_log(CL_ANN, (KERN_NOTICE
3691 "megaraid mbox: LD delete, mailbox busy\n"));
3693 kioc->status = -EBUSY;
3695 scb->status = -1;
3697 megaraid_mbox_mm_done(adapter, scb);
3699 return (-EBUSY);
3702 return 0;
3705 // put the command on the pending list and execute
3706 megaraid_mbox_runpendq(adapter, scb);
3708 return 0;
3712 static int
3713 wait_till_fw_empty(adapter_t *adapter)
3715 unsigned long flags = 0;
3716 int i;
3720 * Set the quiescent flag to stop issuing cmds to FW.
3722 spin_lock_irqsave(&adapter->lock, flags);
3723 adapter->quiescent++;
3724 spin_unlock_irqrestore(&adapter->lock, flags);
3727 * Wait till there are no more cmds outstanding at FW. Try for at most
3728 * 60 seconds
3730 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3731 con_log(CL_DLEVEL1, (KERN_INFO
3732 "megaraid: FW has %d pending commands\n",
3733 adapter->outstanding_cmds));
3735 msleep(1000);
3738 return adapter->outstanding_cmds;
3743 * megaraid_mbox_mm_done - callback for CMM commands
3744 * @adapter : HBA soft state
3745 * @scb : completed command
3747 * Callback routine for internal commands originated from the management
3748 * module.
3750 static void
3751 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3753 uioc_t *kioc;
3754 mbox64_t *mbox64;
3755 uint8_t *raw_mbox;
3756 unsigned long flags;
3758 kioc = (uioc_t *)scb->gp;
3759 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3760 mbox64->mbox32.status = scb->status;
3761 raw_mbox = (uint8_t *)&mbox64->mbox32;
3764 // put scb in the free pool
3765 scb->state = SCB_FREE;
3766 scb->scp = NULL;
3768 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3770 list_add(&scb->list, &adapter->uscb_pool);
3772 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3774 // if a delete logical drive operation succeeded, restart the
3775 // controller
3776 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3778 adapter->quiescent--;
3780 megaraid_mbox_runpendq(adapter, NULL);
3783 kioc->done(kioc);
3785 return;
3790 * gather_hbainfo - HBA characteristics for the applications
3791 * @adapter : HBA soft state
3792 * @hinfo : pointer to the caller's host info strucuture
3794 static int
3795 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3797 uint8_t dmajor;
3799 dmajor = megaraid_mbox_version[0];
3801 hinfo->pci_vendor_id = adapter->pdev->vendor;
3802 hinfo->pci_device_id = adapter->pdev->device;
3803 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3804 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3806 hinfo->pci_bus = adapter->pdev->bus->number;
3807 hinfo->pci_dev_fn = adapter->pdev->devfn;
3808 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3809 hinfo->irq = adapter->host->irq;
3810 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3812 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3813 hinfo->host_no = adapter->host->host_no;
3815 return 0;
3819 * END: Interface for the common management module
3825 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3826 * @adapter : controller's soft state
3828 * Allocate packets required to issue FW calls whenever the sysfs attributes
3829 * are read. These attributes would require up-to-date information from the
3830 * FW. Also set up resources for mutual exclusion to share these resources and
3831 * the wait queue.
3833 * Return 0 on success.
3834 * Return -ERROR_CODE on failure.
3836 static int
3837 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3839 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3840 int rval = 0;
3842 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3844 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3846 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3847 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3849 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3850 !raid_dev->sysfs_buffer) {
3852 con_log(CL_ANN, (KERN_WARNING
3853 "megaraid: out of memory, %s %d\n", __func__,
3854 __LINE__));
3856 rval = -ENOMEM;
3858 megaraid_sysfs_free_resources(adapter);
3861 mutex_init(&raid_dev->sysfs_mtx);
3863 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3865 return rval;
3870 * megaraid_sysfs_free_resources - free sysfs related resources
3871 * @adapter : controller's soft state
3873 * Free packets allocated for sysfs FW commands
3875 static void
3876 megaraid_sysfs_free_resources(adapter_t *adapter)
3878 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3880 kfree(raid_dev->sysfs_uioc);
3881 kfree(raid_dev->sysfs_mbox64);
3883 if (raid_dev->sysfs_buffer) {
3884 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3885 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3891 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3892 * @uioc : completed packet
3894 * Callback routine called in the ISR/tasklet context for get ldmap call
3896 static void
3897 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3899 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3900 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3902 uioc->status = 0;
3904 wake_up(&raid_dev->sysfs_wait_q);
3908 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3909 * @t : timed out timer
3911 * Timeout routine to recover and return to application, in case the adapter
3912 * has stopped responding. A timeout of 60 seconds for this command seems like
3913 * a good value.
3915 static void
3916 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
3918 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3919 uioc_t *uioc = timeout->uioc;
3920 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3921 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3923 uioc->status = -ETIME;
3925 wake_up(&raid_dev->sysfs_wait_q);
3930 * megaraid_sysfs_get_ldmap - get update logical drive map
3931 * @adapter : controller's soft state
3933 * This routine will be called whenever user reads the logical drive
3934 * attributes, go get the current logical drive mapping table from the
3935 * firmware. We use the management API's to issue commands to the controller.
3937 * NOTE: The commands issuance functionality is not generalized and
3938 * implemented in context of "get ld map" command only. If required, the
3939 * command issuance logical can be trivially pulled out and implemented as a
3940 * standalone library. For now, this should suffice since there is no other
3941 * user of this interface.
3943 * Return 0 on success.
3944 * Return -1 on failure.
3946 static int
3947 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3949 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3950 uioc_t *uioc;
3951 mbox64_t *mbox64;
3952 mbox_t *mbox;
3953 char *raw_mbox;
3954 struct uioc_timeout timeout;
3955 caddr_t ldmap;
3956 int rval = 0;
3959 * Allow only one read at a time to go through the sysfs attributes
3961 mutex_lock(&raid_dev->sysfs_mtx);
3963 uioc = raid_dev->sysfs_uioc;
3964 mbox64 = raid_dev->sysfs_mbox64;
3965 ldmap = raid_dev->sysfs_buffer;
3967 memset(uioc, 0, sizeof(uioc_t));
3968 memset(mbox64, 0, sizeof(mbox64_t));
3969 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3971 mbox = &mbox64->mbox32;
3972 raw_mbox = (char *)mbox;
3973 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3974 uioc->buf_vaddr = (caddr_t)adapter;
3975 uioc->status = -ENODATA;
3976 uioc->done = megaraid_sysfs_get_ldmap_done;
3979 * Prepare the mailbox packet to get the current logical drive mapping
3980 * table
3982 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3984 raw_mbox[0] = FC_DEL_LOGDRV;
3985 raw_mbox[2] = OP_GET_LDID_MAP;
3988 * Setup a timer to recover from a non-responding controller
3990 timeout.uioc = uioc;
3991 timer_setup_on_stack(&timeout.timer,
3992 megaraid_sysfs_get_ldmap_timeout, 0);
3994 timeout.timer.expires = jiffies + 60 * HZ;
3995 add_timer(&timeout.timer);
3998 * Send the command to the firmware
4000 rval = megaraid_mbox_mm_command(adapter, uioc);
4002 if (rval == 0) { // command successfully issued
4003 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4006 * Check if the command timed out
4008 if (uioc->status == -ETIME) {
4009 con_log(CL_ANN, (KERN_NOTICE
4010 "megaraid: sysfs get ld map timed out\n"));
4012 rval = -ETIME;
4014 else {
4015 rval = mbox->status;
4018 if (rval == 0) {
4019 memcpy(raid_dev->curr_ldmap, ldmap,
4020 sizeof(raid_dev->curr_ldmap));
4022 else {
4023 con_log(CL_ANN, (KERN_NOTICE
4024 "megaraid: get ld map failed with %x\n", rval));
4027 else {
4028 con_log(CL_ANN, (KERN_NOTICE
4029 "megaraid: could not issue ldmap command:%x\n", rval));
4033 del_timer_sync(&timeout.timer);
4034 destroy_timer_on_stack(&timeout.timer);
4036 mutex_unlock(&raid_dev->sysfs_mtx);
4038 return rval;
4043 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4044 * @cdev : class device object representation for the host
4045 * @buf : buffer to send data to
4047 * Display the handle used by the applications while executing management
4048 * tasks on the adapter. We invoke a management module API to get the adapter
4049 * handle, since we do not interface with applications directly.
4051 static ssize_t
4052 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
4053 char *buf)
4055 struct Scsi_Host *shost = class_to_shost(dev);
4056 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4057 uint32_t app_hndl;
4059 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4061 return snprintf(buf, 8, "%u\n", app_hndl);
4066 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4067 * @dev : device object representation for the scsi device
4068 * @attr : device attribute to show
4069 * @buf : buffer to send data to
4071 * Display the logical drive number for the device in question, if it a valid
4072 * logical drive. For physical devices, "-1" is returned.
4074 * The logical drive number is displayed in following format:
4076 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4078 * <int> <int> <int> <int>
4080 static ssize_t
4081 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4083 struct scsi_device *sdev = to_scsi_device(dev);
4084 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4085 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4086 int scsi_id = -1;
4087 int logical_drv = -1;
4088 int ldid_map = -1;
4089 uint32_t app_hndl = 0;
4090 int mapped_sdev_id;
4091 int rval;
4092 int i;
4094 if (raid_dev->random_del_supported &&
4095 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4097 rval = megaraid_sysfs_get_ldmap(adapter);
4098 if (rval == 0) {
4100 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4102 mapped_sdev_id = sdev->id;
4104 if (sdev->id > adapter->init_id) {
4105 mapped_sdev_id -= 1;
4108 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4110 scsi_id = sdev->id;
4112 logical_drv = i;
4114 ldid_map = raid_dev->curr_ldmap[i];
4116 app_hndl = mraid_mm_adapter_app_handle(
4117 adapter->unique_id);
4119 break;
4123 else {
4124 con_log(CL_ANN, (KERN_NOTICE
4125 "megaraid: sysfs get ld map failed: %x\n",
4126 rval));
4130 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4131 ldid_map, app_hndl);
4136 * END: Mailbox Low Level Driver
4138 module_init(megaraid_init);
4139 module_exit(megaraid_exit);
4141 /* vim: set ts=8 sw=8 tw=78 ai si: */