mic: vop: Fix use-after-free on remove
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_mbox.c
blobf112458023ff4f5d0c101bb1353f2c9815b9f8a7
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 * PCI table for all supported controllers.
207 static struct pci_device_id pci_id_table_g[] = {
209 PCI_VENDOR_ID_DELL,
210 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
211 PCI_VENDOR_ID_DELL,
212 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
215 PCI_VENDOR_ID_LSI_LOGIC,
216 PCI_DEVICE_ID_PERC4_SC,
217 PCI_VENDOR_ID_DELL,
218 PCI_SUBSYS_ID_PERC4_SC,
221 PCI_VENDOR_ID_LSI_LOGIC,
222 PCI_DEVICE_ID_PERC4_DC,
223 PCI_VENDOR_ID_DELL,
224 PCI_SUBSYS_ID_PERC4_DC,
227 PCI_VENDOR_ID_LSI_LOGIC,
228 PCI_DEVICE_ID_VERDE,
229 PCI_ANY_ID,
230 PCI_ANY_ID,
233 PCI_VENDOR_ID_DELL,
234 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
235 PCI_VENDOR_ID_DELL,
236 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
239 PCI_VENDOR_ID_DELL,
240 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
241 PCI_VENDOR_ID_DELL,
242 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
245 PCI_VENDOR_ID_DELL,
246 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
247 PCI_VENDOR_ID_DELL,
248 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
251 PCI_VENDOR_ID_DELL,
252 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
253 PCI_VENDOR_ID_DELL,
254 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
257 PCI_VENDOR_ID_DELL,
258 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
259 PCI_VENDOR_ID_DELL,
260 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
263 PCI_VENDOR_ID_DELL,
264 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
265 PCI_VENDOR_ID_DELL,
266 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
269 PCI_VENDOR_ID_LSI_LOGIC,
270 PCI_DEVICE_ID_DOBSON,
271 PCI_ANY_ID,
272 PCI_ANY_ID,
275 PCI_VENDOR_ID_AMI,
276 PCI_DEVICE_ID_AMI_MEGARAID3,
277 PCI_ANY_ID,
278 PCI_ANY_ID,
281 PCI_VENDOR_ID_LSI_LOGIC,
282 PCI_DEVICE_ID_AMI_MEGARAID3,
283 PCI_ANY_ID,
284 PCI_ANY_ID,
287 PCI_VENDOR_ID_LSI_LOGIC,
288 PCI_DEVICE_ID_LINDSAY,
289 PCI_ANY_ID,
290 PCI_ANY_ID,
292 {0} /* Terminating entry */
294 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
297 static struct pci_driver megaraid_pci_driver = {
298 .name = "megaraid",
299 .id_table = pci_id_table_g,
300 .probe = megaraid_probe_one,
301 .remove = megaraid_detach_one,
302 .shutdown = megaraid_mbox_shutdown,
307 // definitions for the device attributes for exporting logical drive number
308 // for a scsi address (Host, Channel, Id, Lun)
310 DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
311 NULL);
313 // Host template initializer for megaraid mbox sysfs device attributes
314 static struct device_attribute *megaraid_shost_attrs[] = {
315 &dev_attr_megaraid_mbox_app_hndl,
316 NULL,
320 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
322 // Host template initializer for megaraid mbox sysfs device attributes
323 static struct device_attribute *megaraid_sdev_attrs[] = {
324 &dev_attr_megaraid_mbox_ld,
325 NULL,
329 * Scsi host template for megaraid unified driver
331 static struct scsi_host_template megaraid_template_g = {
332 .module = THIS_MODULE,
333 .name = "LSI Logic MegaRAID driver",
334 .proc_name = "megaraid",
335 .queuecommand = megaraid_queue_command,
336 .eh_abort_handler = megaraid_abort_handler,
337 .eh_host_reset_handler = megaraid_reset_handler,
338 .change_queue_depth = scsi_change_queue_depth,
339 .no_write_same = 1,
340 .sdev_attrs = megaraid_sdev_attrs,
341 .shost_attrs = megaraid_shost_attrs,
346 * megaraid_init - module load hook
348 * We register ourselves as hotplug enabled module and let PCI subsystem
349 * discover our adapters.
351 static int __init
352 megaraid_init(void)
354 int rval;
356 // Announce the driver version
357 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
358 MEGARAID_EXT_VERSION));
360 // check validity of module parameters
361 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
363 con_log(CL_ANN, (KERN_WARNING
364 "megaraid mailbox: max commands per lun reset to %d\n",
365 MBOX_MAX_SCSI_CMDS));
367 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
371 // register as a PCI hot-plug driver module
372 rval = pci_register_driver(&megaraid_pci_driver);
373 if (rval < 0) {
374 con_log(CL_ANN, (KERN_WARNING
375 "megaraid: could not register hotplug support.\n"));
378 return rval;
383 * megaraid_exit - driver unload entry point
385 * We simply unwrap the megaraid_init routine here.
387 static void __exit
388 megaraid_exit(void)
390 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
392 // unregister as PCI hotplug driver
393 pci_unregister_driver(&megaraid_pci_driver);
395 return;
400 * megaraid_probe_one - PCI hotplug entry point
401 * @pdev : handle to this controller's PCI configuration space
402 * @id : pci device id of the class of controllers
404 * This routine should be called whenever a new adapter is detected by the
405 * PCI hotplug susbsystem.
407 static int
408 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
410 adapter_t *adapter;
413 // detected a new controller
414 con_log(CL_ANN, (KERN_INFO
415 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
416 pdev->vendor, pdev->device, pdev->subsystem_vendor,
417 pdev->subsystem_device));
419 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
420 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
422 if (pci_enable_device(pdev)) {
423 con_log(CL_ANN, (KERN_WARNING
424 "megaraid: pci_enable_device failed\n"));
426 return -ENODEV;
429 // Enable bus-mastering on this controller
430 pci_set_master(pdev);
432 // Allocate the per driver initialization structure
433 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
435 if (adapter == NULL) {
436 con_log(CL_ANN, (KERN_WARNING
437 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
439 goto out_probe_one;
443 // set up PCI related soft state and other pre-known parameters
444 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
445 adapter->irq = pdev->irq;
446 adapter->pdev = pdev;
448 atomic_set(&adapter->being_detached, 0);
450 // Setup the default DMA mask. This would be changed later on
451 // depending on hardware capabilities
452 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) {
453 con_log(CL_ANN, (KERN_WARNING
454 "megaraid: dma_set_mask failed:%d\n", __LINE__));
456 goto out_free_adapter;
460 // Initialize the synchronization lock for kernel and LLD
461 spin_lock_init(&adapter->lock);
463 // Initialize the command queues: the list of free SCBs and the list
464 // of pending SCBs.
465 INIT_LIST_HEAD(&adapter->kscb_pool);
466 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
468 INIT_LIST_HEAD(&adapter->pend_list);
469 spin_lock_init(PENDING_LIST_LOCK(adapter));
471 INIT_LIST_HEAD(&adapter->completed_list);
472 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
475 // Start the mailbox based controller
476 if (megaraid_init_mbox(adapter) != 0) {
477 con_log(CL_ANN, (KERN_WARNING
478 "megaraid: mailbox adapter did not initialize\n"));
480 goto out_free_adapter;
483 // Register with LSI Common Management Module
484 if (megaraid_cmm_register(adapter) != 0) {
486 con_log(CL_ANN, (KERN_WARNING
487 "megaraid: could not register with management module\n"));
489 goto out_fini_mbox;
492 // setup adapter handle in PCI soft state
493 pci_set_drvdata(pdev, adapter);
495 // attach with scsi mid-layer
496 if (megaraid_io_attach(adapter) != 0) {
498 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
500 goto out_cmm_unreg;
503 return 0;
505 out_cmm_unreg:
506 megaraid_cmm_unregister(adapter);
507 out_fini_mbox:
508 megaraid_fini_mbox(adapter);
509 out_free_adapter:
510 kfree(adapter);
511 out_probe_one:
512 pci_disable_device(pdev);
514 return -ENODEV;
519 * megaraid_detach_one - release framework resources and call LLD release routine
520 * @pdev : handle for our PCI configuration space
522 * This routine is called during driver unload. We free all the allocated
523 * resources and call the corresponding LLD so that it can also release all
524 * its resources.
526 * This routine is also called from the PCI hotplug system.
528 static void
529 megaraid_detach_one(struct pci_dev *pdev)
531 adapter_t *adapter;
532 struct Scsi_Host *host;
535 // Start a rollback on this adapter
536 adapter = pci_get_drvdata(pdev);
538 if (!adapter) {
539 con_log(CL_ANN, (KERN_CRIT
540 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
541 pdev->vendor, pdev->device, pdev->subsystem_vendor,
542 pdev->subsystem_device));
544 return;
546 else {
547 con_log(CL_ANN, (KERN_NOTICE
548 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
549 pdev->vendor, pdev->device, pdev->subsystem_vendor,
550 pdev->subsystem_device));
554 host = adapter->host;
556 // do not allow any more requests from the management module for this
557 // adapter.
558 // FIXME: How do we account for the request which might still be
559 // pending with us?
560 atomic_set(&adapter->being_detached, 1);
562 // detach from the IO sub-system
563 megaraid_io_detach(adapter);
565 // Unregister from common management module
567 // FIXME: this must return success or failure for conditions if there
568 // is a command pending with LLD or not.
569 megaraid_cmm_unregister(adapter);
571 // finalize the mailbox based controller and release all resources
572 megaraid_fini_mbox(adapter);
574 kfree(adapter);
576 scsi_host_put(host);
578 pci_disable_device(pdev);
580 return;
585 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
586 * @pdev : generic driver model device
588 * Shutdown notification, perform flush cache.
590 static void
591 megaraid_mbox_shutdown(struct pci_dev *pdev)
593 adapter_t *adapter = pci_get_drvdata(pdev);
594 static int counter;
596 if (!adapter) {
597 con_log(CL_ANN, (KERN_WARNING
598 "megaraid: null device in shutdown\n"));
599 return;
602 // flush caches now
603 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
604 counter++));
606 megaraid_mbox_flush_cache(adapter);
608 con_log(CL_ANN, ("done\n"));
613 * megaraid_io_attach - attach a device with the IO subsystem
614 * @adapter : controller's soft state
616 * Attach this device with the IO subsystem.
618 static int
619 megaraid_io_attach(adapter_t *adapter)
621 struct Scsi_Host *host;
623 // Initialize SCSI Host structure
624 host = scsi_host_alloc(&megaraid_template_g, 8);
625 if (!host) {
626 con_log(CL_ANN, (KERN_WARNING
627 "megaraid mbox: scsi_register failed\n"));
629 return -1;
632 SCSIHOST2ADAP(host) = (caddr_t)adapter;
633 adapter->host = host;
635 host->irq = adapter->irq;
636 host->unique_id = adapter->unique_id;
637 host->can_queue = adapter->max_cmds;
638 host->this_id = adapter->init_id;
639 host->sg_tablesize = adapter->sglen;
640 host->max_sectors = adapter->max_sectors;
641 host->cmd_per_lun = adapter->cmd_per_lun;
642 host->max_channel = adapter->max_channel;
643 host->max_id = adapter->max_target;
644 host->max_lun = adapter->max_lun;
647 // notify mid-layer about the new controller
648 if (scsi_add_host(host, &adapter->pdev->dev)) {
650 con_log(CL_ANN, (KERN_WARNING
651 "megaraid mbox: scsi_add_host failed\n"));
653 scsi_host_put(host);
655 return -1;
658 scsi_scan_host(host);
660 return 0;
665 * megaraid_io_detach - detach a device from the IO subsystem
666 * @adapter : controller's soft state
668 * Detach this device from the IO subsystem.
670 static void
671 megaraid_io_detach(adapter_t *adapter)
673 struct Scsi_Host *host;
675 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
677 host = adapter->host;
679 scsi_remove_host(host);
681 return;
686 * START: Mailbox Low Level Driver
688 * This is section specific to the single mailbox based controllers
692 * megaraid_init_mbox - initialize controller
693 * @adapter : our soft state
695 * - Allocate 16-byte aligned mailbox memory for firmware handshake
696 * - Allocate controller's memory resources
697 * - Find out all initialization data
698 * - Allocate memory required for all the commands
699 * - Use internal library of FW routines, build up complete soft state
701 static int
702 megaraid_init_mbox(adapter_t *adapter)
704 struct pci_dev *pdev;
705 mraid_device_t *raid_dev;
706 int i;
707 uint32_t magic64;
710 adapter->ito = MBOX_TIMEOUT;
711 pdev = adapter->pdev;
714 * Allocate and initialize the init data structure for mailbox
715 * controllers
717 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
718 if (raid_dev == NULL) return -1;
722 * Attach the adapter soft state to raid device soft state
724 adapter->raid_device = (caddr_t)raid_dev;
725 raid_dev->fast_load = megaraid_fast_load;
728 // our baseport
729 raid_dev->baseport = pci_resource_start(pdev, 0);
731 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
733 con_log(CL_ANN, (KERN_WARNING
734 "megaraid: mem region busy\n"));
736 goto out_free_raid_dev;
739 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
741 if (!raid_dev->baseaddr) {
743 con_log(CL_ANN, (KERN_WARNING
744 "megaraid: could not map hba memory\n") );
746 goto out_release_regions;
749 /* initialize the mutual exclusion lock for the mailbox */
750 spin_lock_init(&raid_dev->mailbox_lock);
752 /* allocate memory required for commands */
753 if (megaraid_alloc_cmd_packets(adapter) != 0)
754 goto out_iounmap;
757 * Issue SYNC cmd to flush the pending cmds in the adapter
758 * and initialize its internal state
761 if (megaraid_mbox_fire_sync_cmd(adapter))
762 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
765 * Setup the rest of the soft state using the library of
766 * FW routines
769 /* request IRQ and register the interrupt service routine */
770 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
771 adapter)) {
773 con_log(CL_ANN, (KERN_WARNING
774 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
775 goto out_alloc_cmds;
779 // Product info
780 if (megaraid_mbox_product_info(adapter) != 0)
781 goto out_free_irq;
783 // Do we support extended CDBs
784 adapter->max_cdb_sz = 10;
785 if (megaraid_mbox_extended_cdb(adapter) == 0) {
786 adapter->max_cdb_sz = 16;
790 * Do we support cluster environment, if we do, what is the initiator
791 * id.
792 * NOTE: In a non-cluster aware firmware environment, the LLD should
793 * return 7 as initiator id.
795 adapter->ha = 0;
796 adapter->init_id = -1;
797 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
798 adapter->ha = 1;
802 * Prepare the device ids array to have the mapping between the kernel
803 * device address and megaraid device address.
804 * We export the physical devices on their actual addresses. The
805 * logical drives are exported on a virtual SCSI channel
807 megaraid_mbox_setup_device_map(adapter);
809 // If the firmware supports random deletion, update the device id map
810 if (megaraid_mbox_support_random_del(adapter)) {
812 // Change the logical drives numbers in device_ids array one
813 // slot in device_ids is reserved for target id, that's why
814 // "<=" below
815 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
816 adapter->device_ids[adapter->max_channel][i] += 0x80;
818 adapter->device_ids[adapter->max_channel][adapter->init_id] =
819 0xFF;
821 raid_dev->random_del_supported = 1;
825 * find out the maximum number of scatter-gather elements supported by
826 * this firmware
828 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
830 // enumerate RAID and SCSI channels so that all devices on SCSI
831 // channels can later be exported, including disk devices
832 megaraid_mbox_enum_raid_scsi(adapter);
835 * Other parameters required by upper layer
837 * maximum number of sectors per IO command
839 adapter->max_sectors = megaraid_max_sectors;
842 * number of queued commands per LUN.
844 adapter->cmd_per_lun = megaraid_cmd_per_lun;
847 * Allocate resources required to issue FW calls, when sysfs is
848 * accessed
850 if (megaraid_sysfs_alloc_resources(adapter) != 0)
851 goto out_free_irq;
853 // Set the DMA mask to 64-bit. All supported controllers as capable of
854 // DMA in this range
855 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
857 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
858 ((adapter->pdev->subsystem_device !=
859 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
860 (adapter->pdev->subsystem_device !=
861 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
862 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
863 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
864 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
865 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
866 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
867 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
868 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
869 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
870 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
871 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
872 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) {
873 con_log(CL_ANN, (KERN_WARNING
874 "megaraid: DMA mask for 64-bit failed\n"));
876 if (dma_set_mask(&adapter->pdev->dev,
877 DMA_BIT_MASK(32))) {
878 con_log(CL_ANN, (KERN_WARNING
879 "megaraid: 32-bit DMA mask failed\n"));
880 goto out_free_sysfs_res;
885 // setup tasklet for DPC
886 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
887 (unsigned long)adapter);
889 con_log(CL_DLEVEL1, (KERN_INFO
890 "megaraid mbox hba successfully initialized\n"));
892 return 0;
894 out_free_sysfs_res:
895 megaraid_sysfs_free_resources(adapter);
896 out_free_irq:
897 free_irq(adapter->irq, adapter);
898 out_alloc_cmds:
899 megaraid_free_cmd_packets(adapter);
900 out_iounmap:
901 iounmap(raid_dev->baseaddr);
902 out_release_regions:
903 pci_release_regions(pdev);
904 out_free_raid_dev:
905 kfree(raid_dev);
907 return -1;
912 * megaraid_fini_mbox - undo controller initialization
913 * @adapter : our soft state
915 static void
916 megaraid_fini_mbox(adapter_t *adapter)
918 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
920 // flush all caches
921 megaraid_mbox_flush_cache(adapter);
923 tasklet_kill(&adapter->dpc_h);
925 megaraid_sysfs_free_resources(adapter);
927 megaraid_free_cmd_packets(adapter);
929 free_irq(adapter->irq, adapter);
931 iounmap(raid_dev->baseaddr);
933 pci_release_regions(adapter->pdev);
935 kfree(raid_dev);
937 return;
942 * megaraid_alloc_cmd_packets - allocate shared mailbox
943 * @adapter : soft state of the raid controller
945 * Allocate and align the shared mailbox. This mailbox is used to issue
946 * all the commands. For IO based controllers, the mailbox is also registered
947 * with the FW. Allocate memory for all commands as well.
948 * This is our big allocator.
950 static int
951 megaraid_alloc_cmd_packets(adapter_t *adapter)
953 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
954 struct pci_dev *pdev;
955 unsigned long align;
956 scb_t *scb;
957 mbox_ccb_t *ccb;
958 struct mraid_pci_blk *epthru_pci_blk;
959 struct mraid_pci_blk *sg_pci_blk;
960 struct mraid_pci_blk *mbox_pci_blk;
961 int i;
963 pdev = adapter->pdev;
966 * Setup the mailbox
967 * Allocate the common 16-byte aligned memory for the handshake
968 * mailbox.
970 raid_dev->una_mbox64 = dma_alloc_coherent(&adapter->pdev->dev,
971 sizeof(mbox64_t),
972 &raid_dev->una_mbox64_dma,
973 GFP_KERNEL);
975 if (!raid_dev->una_mbox64) {
976 con_log(CL_ANN, (KERN_WARNING
977 "megaraid: out of memory, %s %d\n", __func__,
978 __LINE__));
979 return -1;
983 * Align the mailbox at 16-byte boundary
985 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
987 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
988 (~0UL ^ 0xFUL));
990 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
992 align = ((void *)raid_dev->mbox -
993 ((void *)&raid_dev->una_mbox64->mbox32));
995 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
996 align;
998 // Allocate memory for commands issued internally
999 adapter->ibuf = dma_alloc_coherent(&pdev->dev, MBOX_IBUF_SIZE,
1000 &adapter->ibuf_dma_h, GFP_KERNEL);
1001 if (!adapter->ibuf) {
1003 con_log(CL_ANN, (KERN_WARNING
1004 "megaraid: out of memory, %s %d\n", __func__,
1005 __LINE__));
1007 goto out_free_common_mbox;
1010 // Allocate memory for our SCSI Command Blocks and their associated
1011 // memory
1014 * Allocate memory for the base list of scb. Later allocate memory for
1015 * CCBs and embedded components of each CCB and point the pointers in
1016 * scb to the allocated components
1017 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1018 * since the calling routine does not yet know the number of available
1019 * commands.
1021 adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
1023 if (adapter->kscb_list == NULL) {
1024 con_log(CL_ANN, (KERN_WARNING
1025 "megaraid: out of memory, %s %d\n", __func__,
1026 __LINE__));
1027 goto out_free_ibuf;
1030 // memory allocation for our command packets
1031 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1032 con_log(CL_ANN, (KERN_WARNING
1033 "megaraid: out of memory, %s %d\n", __func__,
1034 __LINE__));
1035 goto out_free_scb_list;
1038 // Adjust the scb pointers and link in the free pool
1039 epthru_pci_blk = raid_dev->epthru_pool;
1040 sg_pci_blk = raid_dev->sg_pool;
1041 mbox_pci_blk = raid_dev->mbox_pool;
1043 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1044 scb = adapter->kscb_list + i;
1045 ccb = raid_dev->ccb_list + i;
1047 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1048 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1049 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1050 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1052 // make sure the mailbox is aligned properly
1053 if (ccb->mbox_dma_h & 0x0F) {
1054 con_log(CL_ANN, (KERN_CRIT
1055 "megaraid mbox: not aligned on 16-bytes\n"));
1057 goto out_teardown_dma_pools;
1060 ccb->epthru = (mraid_epassthru_t *)
1061 epthru_pci_blk[i].vaddr;
1062 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1063 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1064 ccb->pthru_dma_h = ccb->epthru_dma_h;
1067 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1068 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1069 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1071 scb->ccb = (caddr_t)ccb;
1072 scb->gp = 0;
1074 scb->sno = i; // command index
1076 scb->scp = NULL;
1077 scb->state = SCB_FREE;
1078 scb->dma_direction = DMA_NONE;
1079 scb->dma_type = MRAID_DMA_NONE;
1080 scb->dev_channel = -1;
1081 scb->dev_target = -1;
1083 // put scb in the free pool
1084 list_add_tail(&scb->list, &adapter->kscb_pool);
1087 return 0;
1089 out_teardown_dma_pools:
1090 megaraid_mbox_teardown_dma_pools(adapter);
1091 out_free_scb_list:
1092 kfree(adapter->kscb_list);
1093 out_free_ibuf:
1094 dma_free_coherent(&pdev->dev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1095 adapter->ibuf_dma_h);
1096 out_free_common_mbox:
1097 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1098 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1100 return -1;
1105 * megaraid_free_cmd_packets - free memory
1106 * @adapter : soft state of the raid controller
1108 * Release memory resources allocated for commands.
1110 static void
1111 megaraid_free_cmd_packets(adapter_t *adapter)
1113 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1115 megaraid_mbox_teardown_dma_pools(adapter);
1117 kfree(adapter->kscb_list);
1119 dma_free_coherent(&adapter->pdev->dev, MBOX_IBUF_SIZE,
1120 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1122 dma_free_coherent(&adapter->pdev->dev, sizeof(mbox64_t),
1123 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1124 return;
1129 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1130 * @adapter : HBA soft state
1132 * Setup the dma pools for mailbox, passthru and extended passthru structures,
1133 * and scatter-gather lists.
1135 static int
1136 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1138 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1139 struct mraid_pci_blk *epthru_pci_blk;
1140 struct mraid_pci_blk *sg_pci_blk;
1141 struct mraid_pci_blk *mbox_pci_blk;
1142 int i;
1146 // Allocate memory for 16-bytes aligned mailboxes
1147 raid_dev->mbox_pool_handle = dma_pool_create("megaraid mbox pool",
1148 &adapter->pdev->dev,
1149 sizeof(mbox64_t) + 16,
1150 16, 0);
1152 if (raid_dev->mbox_pool_handle == NULL) {
1153 goto fail_setup_dma_pool;
1156 mbox_pci_blk = raid_dev->mbox_pool;
1157 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1158 mbox_pci_blk[i].vaddr = dma_pool_alloc(
1159 raid_dev->mbox_pool_handle,
1160 GFP_KERNEL,
1161 &mbox_pci_blk[i].dma_addr);
1162 if (!mbox_pci_blk[i].vaddr) {
1163 goto fail_setup_dma_pool;
1168 * Allocate memory for each embedded passthru strucuture pointer
1169 * Request for a 128 bytes aligned structure for each passthru command
1170 * structure
1171 * Since passthru and extended passthru commands are exclusive, they
1172 * share common memory pool. Passthru structures piggyback on memory
1173 * allocted to extended passthru since passthru is smaller of the two
1175 raid_dev->epthru_pool_handle = dma_pool_create("megaraid mbox pthru",
1176 &adapter->pdev->dev, sizeof(mraid_epassthru_t), 128, 0);
1178 if (raid_dev->epthru_pool_handle == NULL) {
1179 goto fail_setup_dma_pool;
1182 epthru_pci_blk = raid_dev->epthru_pool;
1183 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1184 epthru_pci_blk[i].vaddr = dma_pool_alloc(
1185 raid_dev->epthru_pool_handle,
1186 GFP_KERNEL,
1187 &epthru_pci_blk[i].dma_addr);
1188 if (!epthru_pci_blk[i].vaddr) {
1189 goto fail_setup_dma_pool;
1194 // Allocate memory for each scatter-gather list. Request for 512 bytes
1195 // alignment for each sg list
1196 raid_dev->sg_pool_handle = dma_pool_create("megaraid mbox sg",
1197 &adapter->pdev->dev,
1198 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1199 512, 0);
1201 if (raid_dev->sg_pool_handle == NULL) {
1202 goto fail_setup_dma_pool;
1205 sg_pci_blk = raid_dev->sg_pool;
1206 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1207 sg_pci_blk[i].vaddr = dma_pool_alloc(
1208 raid_dev->sg_pool_handle,
1209 GFP_KERNEL,
1210 &sg_pci_blk[i].dma_addr);
1211 if (!sg_pci_blk[i].vaddr) {
1212 goto fail_setup_dma_pool;
1216 return 0;
1218 fail_setup_dma_pool:
1219 megaraid_mbox_teardown_dma_pools(adapter);
1220 return -1;
1225 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1226 * @adapter : HBA soft state
1228 * Teardown the dma pool for mailbox, passthru and extended passthru
1229 * structures, and scatter-gather lists.
1231 static void
1232 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1234 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1235 struct mraid_pci_blk *epthru_pci_blk;
1236 struct mraid_pci_blk *sg_pci_blk;
1237 struct mraid_pci_blk *mbox_pci_blk;
1238 int i;
1241 sg_pci_blk = raid_dev->sg_pool;
1242 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1243 dma_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1244 sg_pci_blk[i].dma_addr);
1246 dma_pool_destroy(raid_dev->sg_pool_handle);
1249 epthru_pci_blk = raid_dev->epthru_pool;
1250 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1251 dma_pool_free(raid_dev->epthru_pool_handle,
1252 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1254 dma_pool_destroy(raid_dev->epthru_pool_handle);
1257 mbox_pci_blk = raid_dev->mbox_pool;
1258 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1259 dma_pool_free(raid_dev->mbox_pool_handle,
1260 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1262 dma_pool_destroy(raid_dev->mbox_pool_handle);
1264 return;
1269 * megaraid_alloc_scb - detach and return a scb from the free list
1270 * @adapter : controller's soft state
1271 * @scp : pointer to the scsi command to be executed
1273 * Return the scb from the head of the free list. %NULL if there are none
1274 * available.
1276 static scb_t *
1277 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1279 struct list_head *head = &adapter->kscb_pool;
1280 scb_t *scb = NULL;
1281 unsigned long flags;
1283 // detach scb from free pool
1284 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1286 if (list_empty(head)) {
1287 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1288 return NULL;
1291 scb = list_entry(head->next, scb_t, list);
1292 list_del_init(&scb->list);
1294 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1296 scb->state = SCB_ACTIVE;
1297 scb->scp = scp;
1298 scb->dma_type = MRAID_DMA_NONE;
1300 return scb;
1305 * megaraid_dealloc_scb - return the scb to the free pool
1306 * @adapter : controller's soft state
1307 * @scb : scb to be freed
1309 * Return the scb back to the free list of scbs. The caller must 'flush' the
1310 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1311 * NOTE NOTE: Make sure the scb is not on any list before calling this
1312 * routine.
1314 static inline void
1315 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1317 unsigned long flags;
1319 // put scb in the free pool
1320 scb->state = SCB_FREE;
1321 scb->scp = NULL;
1322 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1324 list_add(&scb->list, &adapter->kscb_pool);
1326 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1328 return;
1333 * megaraid_mbox_mksgl - make the scatter-gather list
1334 * @adapter : controller's soft state
1335 * @scb : scsi control block
1337 * Prepare the scatter-gather list.
1339 static int
1340 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1342 struct scatterlist *sgl;
1343 mbox_ccb_t *ccb;
1344 struct scsi_cmnd *scp;
1345 int sgcnt;
1346 int i;
1349 scp = scb->scp;
1350 ccb = (mbox_ccb_t *)scb->ccb;
1352 sgcnt = scsi_dma_map(scp);
1353 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1355 // no mapping required if no data to be transferred
1356 if (!sgcnt)
1357 return 0;
1359 scb->dma_type = MRAID_DMA_WSG;
1361 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1362 ccb->sgl64[i].address = sg_dma_address(sgl);
1363 ccb->sgl64[i].length = sg_dma_len(sgl);
1366 // Return count of SG nodes
1367 return sgcnt;
1372 * mbox_post_cmd - issue a mailbox command
1373 * @adapter : controller's soft state
1374 * @scb : command to be issued
1376 * Post the command to the controller if mailbox is available.
1378 static int
1379 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1381 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1382 mbox64_t *mbox64;
1383 mbox_t *mbox;
1384 mbox_ccb_t *ccb;
1385 unsigned long flags;
1386 unsigned int i = 0;
1389 ccb = (mbox_ccb_t *)scb->ccb;
1390 mbox = raid_dev->mbox;
1391 mbox64 = raid_dev->mbox64;
1394 * Check for busy mailbox. If it is, return failure - the caller
1395 * should retry later.
1397 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1399 if (unlikely(mbox->busy)) {
1400 do {
1401 udelay(1);
1402 i++;
1403 rmb();
1404 } while(mbox->busy && (i < max_mbox_busy_wait));
1406 if (mbox->busy) {
1408 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1410 return -1;
1415 // Copy this command's mailbox data into "adapter's" mailbox
1416 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1417 mbox->cmdid = scb->sno;
1419 adapter->outstanding_cmds++;
1421 mbox->busy = 1; // Set busy
1422 mbox->poll = 0;
1423 mbox->ack = 0;
1424 wmb();
1426 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1428 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1430 return 0;
1435 * megaraid_queue_command - generic queue entry point for all LLDs
1436 * @scp : pointer to the scsi command to be executed
1437 * @done : callback routine to be called after the cmd has be completed
1439 * Queue entry point for mailbox based controllers.
1441 static int
1442 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1444 adapter_t *adapter;
1445 scb_t *scb;
1446 int if_busy;
1448 adapter = SCP2ADAPTER(scp);
1449 scp->scsi_done = done;
1450 scp->result = 0;
1453 * Allocate and build a SCB request
1454 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1455 * not allocate scb. We will return non-zero status in that case.
1456 * NOTE: scb can be null even though certain commands completed
1457 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1458 * return 0 in that case, and we would do the callback right away.
1460 if_busy = 0;
1461 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1462 if (!scb) { // command already completed
1463 done(scp);
1464 return 0;
1467 megaraid_mbox_runpendq(adapter, scb);
1468 return if_busy;
1471 static DEF_SCSI_QCMD(megaraid_queue_command)
1474 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1475 * @adapter : controller's soft state
1476 * @scp : mid-layer scsi command pointer
1477 * @busy : set if request could not be completed because of lack of
1478 * resources
1480 * Transform the mid-layer scsi command to megaraid firmware lingua.
1481 * Convert the command issued by mid-layer to format understood by megaraid
1482 * firmware. We also complete certain commands without sending them to firmware.
1484 static scb_t *
1485 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1487 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1488 int channel;
1489 int target;
1490 int islogical;
1491 mbox_ccb_t *ccb;
1492 mraid_passthru_t *pthru;
1493 mbox64_t *mbox64;
1494 mbox_t *mbox;
1495 scb_t *scb;
1496 char skip[] = "skipping";
1497 char scan[] = "scanning";
1498 char *ss;
1502 * Get the appropriate device map for the device this command is
1503 * intended for
1505 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1508 * Logical drive commands
1510 if (islogical) {
1511 switch (scp->cmnd[0]) {
1512 case TEST_UNIT_READY:
1514 * Do we support clustering and is the support enabled
1515 * If no, return success always
1517 if (!adapter->ha) {
1518 scp->result = (DID_OK << 16);
1519 return NULL;
1522 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1523 scp->result = (DID_ERROR << 16);
1524 *busy = 1;
1525 return NULL;
1528 scb->dma_direction = scp->sc_data_direction;
1529 scb->dev_channel = 0xFF;
1530 scb->dev_target = target;
1531 ccb = (mbox_ccb_t *)scb->ccb;
1534 * The command id will be provided by the command
1535 * issuance routine
1537 ccb->raw_mbox[0] = CLUSTER_CMD;
1538 ccb->raw_mbox[2] = RESERVATION_STATUS;
1539 ccb->raw_mbox[3] = target;
1541 return scb;
1543 case MODE_SENSE:
1545 struct scatterlist *sgl;
1546 caddr_t vaddr;
1548 sgl = scsi_sglist(scp);
1549 if (sg_page(sgl)) {
1550 vaddr = (caddr_t) sg_virt(&sgl[0]);
1552 memset(vaddr, 0, scp->cmnd[4]);
1554 else {
1555 con_log(CL_ANN, (KERN_WARNING
1556 "megaraid mailbox: invalid sg:%d\n",
1557 __LINE__));
1560 scp->result = (DID_OK << 16);
1561 return NULL;
1563 case INQUIRY:
1565 * Display the channel scan for logical drives
1566 * Do not display scan for a channel if already done.
1568 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1570 con_log(CL_ANN, (KERN_INFO
1571 "scsi[%d]: scanning scsi channel %d",
1572 adapter->host->host_no,
1573 SCP2CHANNEL(scp)));
1575 con_log(CL_ANN, (
1576 " [virtual] for logical drives\n"));
1578 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1581 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1582 scp->sense_buffer[0] = 0x70;
1583 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1584 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1585 scp->result = CHECK_CONDITION << 1;
1586 return NULL;
1589 /* Fall through */
1591 case READ_CAPACITY:
1593 * Do not allow LUN > 0 for logical drives and
1594 * requests for more than 40 logical drives
1596 if (SCP2LUN(scp)) {
1597 scp->result = (DID_BAD_TARGET << 16);
1598 return NULL;
1600 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1601 scp->result = (DID_BAD_TARGET << 16);
1602 return NULL;
1606 /* Allocate a SCB and initialize passthru */
1607 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1608 scp->result = (DID_ERROR << 16);
1609 *busy = 1;
1610 return NULL;
1613 ccb = (mbox_ccb_t *)scb->ccb;
1614 scb->dev_channel = 0xFF;
1615 scb->dev_target = target;
1616 pthru = ccb->pthru;
1617 mbox = ccb->mbox;
1618 mbox64 = ccb->mbox64;
1620 pthru->timeout = 0;
1621 pthru->ars = 1;
1622 pthru->reqsenselen = 14;
1623 pthru->islogical = 1;
1624 pthru->logdrv = target;
1625 pthru->cdblen = scp->cmd_len;
1626 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1628 mbox->cmd = MBOXCMD_PASSTHRU64;
1629 scb->dma_direction = scp->sc_data_direction;
1631 pthru->dataxferlen = scsi_bufflen(scp);
1632 pthru->dataxferaddr = ccb->sgl_dma_h;
1633 pthru->numsge = megaraid_mbox_mksgl(adapter,
1634 scb);
1636 mbox->xferaddr = 0xFFFFFFFF;
1637 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1638 mbox64->xferaddr_hi = 0;
1640 return scb;
1642 case READ_6:
1643 case WRITE_6:
1644 case READ_10:
1645 case WRITE_10:
1646 case READ_12:
1647 case WRITE_12:
1650 * Allocate a SCB and initialize mailbox
1652 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1653 scp->result = (DID_ERROR << 16);
1654 *busy = 1;
1655 return NULL;
1657 ccb = (mbox_ccb_t *)scb->ccb;
1658 scb->dev_channel = 0xFF;
1659 scb->dev_target = target;
1660 mbox = ccb->mbox;
1661 mbox64 = ccb->mbox64;
1662 mbox->logdrv = target;
1665 * A little HACK: 2nd bit is zero for all scsi read
1666 * commands and is set for all scsi write commands
1668 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1669 MBOXCMD_LREAD64 ;
1672 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1674 if (scp->cmd_len == 6) {
1675 mbox->numsectors = (uint32_t)scp->cmnd[4];
1676 mbox->lba =
1677 ((uint32_t)scp->cmnd[1] << 16) |
1678 ((uint32_t)scp->cmnd[2] << 8) |
1679 (uint32_t)scp->cmnd[3];
1681 mbox->lba &= 0x1FFFFF;
1685 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1687 else if (scp->cmd_len == 10) {
1688 mbox->numsectors =
1689 (uint32_t)scp->cmnd[8] |
1690 ((uint32_t)scp->cmnd[7] << 8);
1691 mbox->lba =
1692 ((uint32_t)scp->cmnd[2] << 24) |
1693 ((uint32_t)scp->cmnd[3] << 16) |
1694 ((uint32_t)scp->cmnd[4] << 8) |
1695 (uint32_t)scp->cmnd[5];
1699 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1701 else if (scp->cmd_len == 12) {
1702 mbox->lba =
1703 ((uint32_t)scp->cmnd[2] << 24) |
1704 ((uint32_t)scp->cmnd[3] << 16) |
1705 ((uint32_t)scp->cmnd[4] << 8) |
1706 (uint32_t)scp->cmnd[5];
1708 mbox->numsectors =
1709 ((uint32_t)scp->cmnd[6] << 24) |
1710 ((uint32_t)scp->cmnd[7] << 16) |
1711 ((uint32_t)scp->cmnd[8] << 8) |
1712 (uint32_t)scp->cmnd[9];
1714 else {
1715 con_log(CL_ANN, (KERN_WARNING
1716 "megaraid: unsupported CDB length\n"));
1718 megaraid_dealloc_scb(adapter, scb);
1720 scp->result = (DID_ERROR << 16);
1721 return NULL;
1724 scb->dma_direction = scp->sc_data_direction;
1726 // Calculate Scatter-Gather info
1727 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1728 mbox->numsge = megaraid_mbox_mksgl(adapter,
1729 scb);
1730 mbox->xferaddr = 0xFFFFFFFF;
1731 mbox64->xferaddr_hi = 0;
1733 return scb;
1735 case RESERVE:
1736 case RELEASE:
1738 * Do we support clustering and is the support enabled
1740 if (!adapter->ha) {
1741 scp->result = (DID_BAD_TARGET << 16);
1742 return NULL;
1746 * Allocate a SCB and initialize mailbox
1748 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1749 scp->result = (DID_ERROR << 16);
1750 *busy = 1;
1751 return NULL;
1754 ccb = (mbox_ccb_t *)scb->ccb;
1755 scb->dev_channel = 0xFF;
1756 scb->dev_target = target;
1757 ccb->raw_mbox[0] = CLUSTER_CMD;
1758 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1759 RESERVE_LD : RELEASE_LD;
1761 ccb->raw_mbox[3] = target;
1762 scb->dma_direction = scp->sc_data_direction;
1764 return scb;
1766 default:
1767 scp->result = (DID_BAD_TARGET << 16);
1768 return NULL;
1771 else { // Passthru device commands
1773 // Do not allow access to target id > 15 or LUN > 7
1774 if (target > 15 || SCP2LUN(scp) > 7) {
1775 scp->result = (DID_BAD_TARGET << 16);
1776 return NULL;
1779 // if fast load option was set and scan for last device is
1780 // over, reset the fast_load flag so that during a possible
1781 // next scan, devices can be made available
1782 if (rdev->fast_load && (target == 15) &&
1783 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1785 con_log(CL_ANN, (KERN_INFO
1786 "megaraid[%d]: physical device scan re-enabled\n",
1787 adapter->host->host_no));
1788 rdev->fast_load = 0;
1792 * Display the channel scan for physical devices
1794 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1796 ss = rdev->fast_load ? skip : scan;
1798 con_log(CL_ANN, (KERN_INFO
1799 "scsi[%d]: %s scsi channel %d [Phy %d]",
1800 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1801 channel));
1803 con_log(CL_ANN, (
1804 " for non-raid devices\n"));
1806 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1809 // disable channel sweep if fast load option given
1810 if (rdev->fast_load) {
1811 scp->result = (DID_BAD_TARGET << 16);
1812 return NULL;
1815 // Allocate a SCB and initialize passthru
1816 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1817 scp->result = (DID_ERROR << 16);
1818 *busy = 1;
1819 return NULL;
1822 ccb = (mbox_ccb_t *)scb->ccb;
1823 scb->dev_channel = channel;
1824 scb->dev_target = target;
1825 scb->dma_direction = scp->sc_data_direction;
1826 mbox = ccb->mbox;
1827 mbox64 = ccb->mbox64;
1829 // Does this firmware support extended CDBs
1830 if (adapter->max_cdb_sz == 16) {
1831 mbox->cmd = MBOXCMD_EXTPTHRU;
1833 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1835 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1836 mbox64->xferaddr_hi = 0;
1837 mbox->xferaddr = 0xFFFFFFFF;
1839 else {
1840 mbox->cmd = MBOXCMD_PASSTHRU64;
1842 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1844 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1845 mbox64->xferaddr_hi = 0;
1846 mbox->xferaddr = 0xFFFFFFFF;
1848 return scb;
1851 // NOT REACHED
1856 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1857 * @adapter : controller's soft state
1858 * @scb_q : SCB to be queued in the pending list
1860 * Scan the pending list for commands which are not yet issued and try to
1861 * post to the controller. The SCB can be a null pointer, which would indicate
1862 * no SCB to be queue, just try to execute the ones in the pending list.
1864 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1865 * out from the head of the pending list. If it is successfully issued, the
1866 * next SCB is at the head now.
1868 static void
1869 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1871 scb_t *scb;
1872 unsigned long flags;
1874 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1876 if (scb_q) {
1877 scb_q->state = SCB_PENDQ;
1878 list_add_tail(&scb_q->list, &adapter->pend_list);
1881 // if the adapter in not in quiescent mode, post the commands to FW
1882 if (adapter->quiescent) {
1883 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1884 return;
1887 while (!list_empty(&adapter->pend_list)) {
1889 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1891 scb = list_entry(adapter->pend_list.next, scb_t, list);
1893 // remove the scb from the pending list and try to
1894 // issue. If we are unable to issue it, put back in
1895 // the pending list and return
1897 list_del_init(&scb->list);
1899 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1901 // if mailbox was busy, return SCB back to pending
1902 // list. Make sure to add at the head, since that's
1903 // where it would have been removed from
1905 scb->state = SCB_ISSUED;
1907 if (mbox_post_cmd(adapter, scb) != 0) {
1909 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1911 scb->state = SCB_PENDQ;
1913 list_add(&scb->list, &adapter->pend_list);
1915 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1916 flags);
1918 return;
1921 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1924 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1927 return;
1932 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1933 * @adapter : pointer to controller's soft state
1934 * @scb : scsi control block
1935 * @scp : scsi command from the mid-layer
1937 * Prepare a command for the scsi physical devices.
1939 static void
1940 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1941 struct scsi_cmnd *scp)
1943 mbox_ccb_t *ccb;
1944 mraid_passthru_t *pthru;
1945 uint8_t channel;
1946 uint8_t target;
1948 ccb = (mbox_ccb_t *)scb->ccb;
1949 pthru = ccb->pthru;
1950 channel = scb->dev_channel;
1951 target = scb->dev_target;
1953 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1954 pthru->timeout = 4;
1955 pthru->ars = 1;
1956 pthru->islogical = 0;
1957 pthru->channel = 0;
1958 pthru->target = (channel << 4) | target;
1959 pthru->logdrv = SCP2LUN(scp);
1960 pthru->reqsenselen = 14;
1961 pthru->cdblen = scp->cmd_len;
1963 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1965 if (scsi_bufflen(scp)) {
1966 pthru->dataxferlen = scsi_bufflen(scp);
1967 pthru->dataxferaddr = ccb->sgl_dma_h;
1968 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1970 else {
1971 pthru->dataxferaddr = 0;
1972 pthru->dataxferlen = 0;
1973 pthru->numsge = 0;
1975 return;
1980 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1981 * @adapter : pointer to controller's soft state
1982 * @scb : scsi control block
1983 * @scp : scsi command from the mid-layer
1985 * Prepare a command for the scsi physical devices. This routine prepares
1986 * commands for devices which can take extended CDBs (>10 bytes).
1988 static void
1989 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
1990 struct scsi_cmnd *scp)
1992 mbox_ccb_t *ccb;
1993 mraid_epassthru_t *epthru;
1994 uint8_t channel;
1995 uint8_t target;
1997 ccb = (mbox_ccb_t *)scb->ccb;
1998 epthru = ccb->epthru;
1999 channel = scb->dev_channel;
2000 target = scb->dev_target;
2002 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2003 epthru->timeout = 4;
2004 epthru->ars = 1;
2005 epthru->islogical = 0;
2006 epthru->channel = 0;
2007 epthru->target = (channel << 4) | target;
2008 epthru->logdrv = SCP2LUN(scp);
2009 epthru->reqsenselen = 14;
2010 epthru->cdblen = scp->cmd_len;
2012 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2014 if (scsi_bufflen(scp)) {
2015 epthru->dataxferlen = scsi_bufflen(scp);
2016 epthru->dataxferaddr = ccb->sgl_dma_h;
2017 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2019 else {
2020 epthru->dataxferaddr = 0;
2021 epthru->dataxferlen = 0;
2022 epthru->numsge = 0;
2024 return;
2029 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2030 * @adapter : controller's soft state
2032 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2033 * completed command and put them on the completed list for later processing.
2035 * Returns: 1 if the interrupt is valid, 0 otherwise
2037 static int
2038 megaraid_ack_sequence(adapter_t *adapter)
2040 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2041 mbox_t *mbox;
2042 scb_t *scb;
2043 uint8_t nstatus;
2044 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2045 struct list_head clist;
2046 int handled;
2047 uint32_t dword;
2048 unsigned long flags;
2049 int i, j;
2052 mbox = raid_dev->mbox;
2054 // move the SCBs from the firmware completed array to our local list
2055 INIT_LIST_HEAD(&clist);
2057 // loop till F/W has more commands for us to complete
2058 handled = 0;
2059 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2060 do {
2062 * Check if a valid interrupt is pending. If found, force the
2063 * interrupt line low.
2065 dword = RDOUTDOOR(raid_dev);
2066 if (dword != 0x10001234) break;
2068 handled = 1;
2070 WROUTDOOR(raid_dev, 0x10001234);
2072 nstatus = 0;
2073 // wait for valid numstatus to post
2074 for (i = 0; i < 0xFFFFF; i++) {
2075 if (mbox->numstatus != 0xFF) {
2076 nstatus = mbox->numstatus;
2077 break;
2079 rmb();
2081 mbox->numstatus = 0xFF;
2083 adapter->outstanding_cmds -= nstatus;
2085 for (i = 0; i < nstatus; i++) {
2087 // wait for valid command index to post
2088 for (j = 0; j < 0xFFFFF; j++) {
2089 if (mbox->completed[i] != 0xFF) break;
2090 rmb();
2092 completed[i] = mbox->completed[i];
2093 mbox->completed[i] = 0xFF;
2095 if (completed[i] == 0xFF) {
2096 con_log(CL_ANN, (KERN_CRIT
2097 "megaraid: command posting timed out\n"));
2099 BUG();
2100 continue;
2103 // Get SCB associated with this command id
2104 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2105 // a cmm command
2106 scb = adapter->uscb_list + (completed[i] -
2107 MBOX_MAX_SCSI_CMDS);
2109 else {
2110 // an os command
2111 scb = adapter->kscb_list + completed[i];
2114 scb->status = mbox->status;
2115 list_add_tail(&scb->list, &clist);
2118 // Acknowledge interrupt
2119 WRINDOOR(raid_dev, 0x02);
2121 } while(1);
2123 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2126 // put the completed commands in the completed list. DPC would
2127 // complete these commands later
2128 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2130 list_splice(&clist, &adapter->completed_list);
2132 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2135 // schedule the DPC if there is some work for it
2136 if (handled)
2137 tasklet_schedule(&adapter->dpc_h);
2139 return handled;
2144 * megaraid_isr - isr for memory based mailbox based controllers
2145 * @irq : irq
2146 * @devp : pointer to our soft state
2148 * Interrupt service routine for memory-mapped mailbox controllers.
2150 static irqreturn_t
2151 megaraid_isr(int irq, void *devp)
2153 adapter_t *adapter = devp;
2154 int handled;
2156 handled = megaraid_ack_sequence(adapter);
2158 /* Loop through any pending requests */
2159 if (!adapter->quiescent) {
2160 megaraid_mbox_runpendq(adapter, NULL);
2163 return IRQ_RETVAL(handled);
2168 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2169 * @devp : pointer to HBA soft state
2171 * Pick up the commands from the completed list and send back to the owners.
2172 * This is a reentrant function and does not assume any locks are held while
2173 * it is being called.
2175 static void
2176 megaraid_mbox_dpc(unsigned long devp)
2178 adapter_t *adapter = (adapter_t *)devp;
2179 mraid_device_t *raid_dev;
2180 struct list_head clist;
2181 struct scatterlist *sgl;
2182 scb_t *scb;
2183 scb_t *tmp;
2184 struct scsi_cmnd *scp;
2185 mraid_passthru_t *pthru;
2186 mraid_epassthru_t *epthru;
2187 mbox_ccb_t *ccb;
2188 int islogical;
2189 int pdev_index;
2190 int pdev_state;
2191 mbox_t *mbox;
2192 unsigned long flags;
2193 uint8_t c;
2194 int status;
2195 uioc_t *kioc;
2198 if (!adapter) return;
2200 raid_dev = ADAP2RAIDDEV(adapter);
2202 // move the SCBs from the completed list to our local list
2203 INIT_LIST_HEAD(&clist);
2205 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2207 list_splice_init(&adapter->completed_list, &clist);
2209 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2212 list_for_each_entry_safe(scb, tmp, &clist, list) {
2214 status = scb->status;
2215 scp = scb->scp;
2216 ccb = (mbox_ccb_t *)scb->ccb;
2217 pthru = ccb->pthru;
2218 epthru = ccb->epthru;
2219 mbox = ccb->mbox;
2221 // Make sure f/w has completed a valid command
2222 if (scb->state != SCB_ISSUED) {
2223 con_log(CL_ANN, (KERN_CRIT
2224 "megaraid critical err: invalid command %d:%d:%p\n",
2225 scb->sno, scb->state, scp));
2226 BUG();
2227 continue; // Must never happen!
2230 // check for the management command and complete it right away
2231 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2232 scb->state = SCB_FREE;
2233 scb->status = status;
2235 // remove from local clist
2236 list_del_init(&scb->list);
2238 kioc = (uioc_t *)scb->gp;
2239 kioc->status = 0;
2241 megaraid_mbox_mm_done(adapter, scb);
2243 continue;
2246 // Was an abort issued for this command earlier
2247 if (scb->state & SCB_ABORT) {
2248 con_log(CL_ANN, (KERN_NOTICE
2249 "megaraid: aborted cmd [%x] completed\n",
2250 scb->sno));
2254 * If the inquiry came of a disk drive which is not part of
2255 * any RAID array, expose it to the kernel. For this to be
2256 * enabled, user must set the "megaraid_expose_unconf_disks"
2257 * flag to 1 by specifying it on module parameter list.
2258 * This would enable data migration off drives from other
2259 * configurations.
2261 islogical = MRAID_IS_LOGICAL(adapter, scp);
2262 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2263 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2265 sgl = scsi_sglist(scp);
2266 if (sg_page(sgl)) {
2267 c = *(unsigned char *) sg_virt(&sgl[0]);
2268 } else {
2269 con_log(CL_ANN, (KERN_WARNING
2270 "megaraid mailbox: invalid sg:%d\n",
2271 __LINE__));
2272 c = 0;
2275 if ((c & 0x1F ) == TYPE_DISK) {
2276 pdev_index = (scb->dev_channel * 16) +
2277 scb->dev_target;
2278 pdev_state =
2279 raid_dev->pdrv_state[pdev_index] & 0x0F;
2281 if (pdev_state == PDRV_ONLINE ||
2282 pdev_state == PDRV_FAILED ||
2283 pdev_state == PDRV_RBLD ||
2284 pdev_state == PDRV_HOTSPARE ||
2285 megaraid_expose_unconf_disks == 0) {
2287 status = 0xF0;
2292 // Convert MegaRAID status to Linux error code
2293 switch (status) {
2295 case 0x00:
2297 scp->result = (DID_OK << 16);
2298 break;
2300 case 0x02:
2302 /* set sense_buffer and result fields */
2303 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2304 mbox->cmd == MBOXCMD_PASSTHRU64) {
2306 memcpy(scp->sense_buffer, pthru->reqsensearea,
2307 14);
2309 scp->result = DRIVER_SENSE << 24 |
2310 DID_OK << 16 | CHECK_CONDITION << 1;
2312 else {
2313 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2315 memcpy(scp->sense_buffer,
2316 epthru->reqsensearea, 14);
2318 scp->result = DRIVER_SENSE << 24 |
2319 DID_OK << 16 |
2320 CHECK_CONDITION << 1;
2321 } else {
2322 scp->sense_buffer[0] = 0x70;
2323 scp->sense_buffer[2] = ABORTED_COMMAND;
2324 scp->result = CHECK_CONDITION << 1;
2327 break;
2329 case 0x08:
2331 scp->result = DID_BUS_BUSY << 16 | status;
2332 break;
2334 default:
2337 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2338 * failed
2340 if (scp->cmnd[0] == TEST_UNIT_READY) {
2341 scp->result = DID_ERROR << 16 |
2342 RESERVATION_CONFLICT << 1;
2344 else
2346 * Error code returned is 1 if Reserve or Release
2347 * failed or the input parameter is invalid
2349 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2350 scp->cmnd[0] == RELEASE)) {
2352 scp->result = DID_ERROR << 16 |
2353 RESERVATION_CONFLICT << 1;
2355 else {
2356 scp->result = DID_BAD_TARGET << 16 | status;
2360 // print a debug message for all failed commands
2361 if (status) {
2362 megaraid_mbox_display_scb(adapter, scb);
2365 scsi_dma_unmap(scp);
2367 // remove from local clist
2368 list_del_init(&scb->list);
2370 // put back in free list
2371 megaraid_dealloc_scb(adapter, scb);
2373 // send the scsi packet back to kernel
2374 scp->scsi_done(scp);
2377 return;
2382 * megaraid_abort_handler - abort the scsi command
2383 * @scp : command to be aborted
2385 * Abort a previous SCSI request. Only commands on the pending list can be
2386 * aborted. All the commands issued to the F/W must complete.
2388 static int
2389 megaraid_abort_handler(struct scsi_cmnd *scp)
2391 adapter_t *adapter;
2392 mraid_device_t *raid_dev;
2393 scb_t *scb;
2394 scb_t *tmp;
2395 int found;
2396 unsigned long flags;
2397 int i;
2400 adapter = SCP2ADAPTER(scp);
2401 raid_dev = ADAP2RAIDDEV(adapter);
2403 con_log(CL_ANN, (KERN_WARNING
2404 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2405 scp->cmnd[0], SCP2CHANNEL(scp),
2406 SCP2TARGET(scp), SCP2LUN(scp)));
2408 // If FW has stopped responding, simply return failure
2409 if (raid_dev->hw_error) {
2410 con_log(CL_ANN, (KERN_NOTICE
2411 "megaraid: hw error, not aborting\n"));
2412 return FAILED;
2415 // There might a race here, where the command was completed by the
2416 // firmware and now it is on the completed list. Before we could
2417 // complete the command to the kernel in dpc, the abort came.
2418 // Find out if this is the case to avoid the race.
2419 scb = NULL;
2420 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2421 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2423 if (scb->scp == scp) { // Found command
2425 list_del_init(&scb->list); // from completed list
2427 con_log(CL_ANN, (KERN_WARNING
2428 "megaraid: %d[%d:%d], abort from completed list\n",
2429 scb->sno, scb->dev_channel, scb->dev_target));
2431 scp->result = (DID_ABORT << 16);
2432 scp->scsi_done(scp);
2434 megaraid_dealloc_scb(adapter, scb);
2436 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2437 flags);
2439 return SUCCESS;
2442 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2445 // Find out if this command is still on the pending list. If it is and
2446 // was never issued, abort and return success. If the command is owned
2447 // by the firmware, we must wait for it to complete by the FW.
2448 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2449 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2451 if (scb->scp == scp) { // Found command
2453 list_del_init(&scb->list); // from pending list
2455 ASSERT(!(scb->state & SCB_ISSUED));
2457 con_log(CL_ANN, (KERN_WARNING
2458 "megaraid abort: [%d:%d], driver owner\n",
2459 scb->dev_channel, scb->dev_target));
2461 scp->result = (DID_ABORT << 16);
2462 scp->scsi_done(scp);
2464 megaraid_dealloc_scb(adapter, scb);
2466 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2467 flags);
2469 return SUCCESS;
2472 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2475 // Check do we even own this command, in which case this would be
2476 // owned by the firmware. The only way to locate the FW scb is to
2477 // traverse through the list of all SCB, since driver does not
2478 // maintain these SCBs on any list
2479 found = 0;
2480 spin_lock_irq(&adapter->lock);
2481 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2482 scb = adapter->kscb_list + i;
2484 if (scb->scp == scp) {
2486 found = 1;
2488 if (!(scb->state & SCB_ISSUED)) {
2489 con_log(CL_ANN, (KERN_WARNING
2490 "megaraid abort: %d[%d:%d], invalid state\n",
2491 scb->sno, scb->dev_channel, scb->dev_target));
2492 BUG();
2494 else {
2495 con_log(CL_ANN, (KERN_WARNING
2496 "megaraid abort: %d[%d:%d], fw owner\n",
2497 scb->sno, scb->dev_channel, scb->dev_target));
2501 spin_unlock_irq(&adapter->lock);
2503 if (!found) {
2504 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2506 // FIXME: Should there be a callback for this command?
2507 return SUCCESS;
2510 // We cannot actually abort a command owned by firmware, return
2511 // failure and wait for reset. In host reset handler, we will find out
2512 // if the HBA is still live
2513 return FAILED;
2517 * megaraid_reset_handler - device reset handler for mailbox based driver
2518 * @scp : reference command
2520 * Reset handler for the mailbox based controller. First try to find out if
2521 * the FW is still live, in which case the outstanding commands counter mut go
2522 * down to 0. If that happens, also issue the reservation reset command to
2523 * relinquish (possible) reservations on the logical drives connected to this
2524 * host.
2526 static int
2527 megaraid_reset_handler(struct scsi_cmnd *scp)
2529 adapter_t *adapter;
2530 scb_t *scb;
2531 scb_t *tmp;
2532 mraid_device_t *raid_dev;
2533 unsigned long flags;
2534 uint8_t raw_mbox[sizeof(mbox_t)];
2535 int rval;
2536 int recovery_window;
2537 int i;
2538 uioc_t *kioc;
2540 adapter = SCP2ADAPTER(scp);
2541 raid_dev = ADAP2RAIDDEV(adapter);
2543 // return failure if adapter is not responding
2544 if (raid_dev->hw_error) {
2545 con_log(CL_ANN, (KERN_NOTICE
2546 "megaraid: hw error, cannot reset\n"));
2547 return FAILED;
2550 // Under exceptional conditions, FW can take up to 3 minutes to
2551 // complete command processing. Wait for additional 2 minutes for the
2552 // pending commands counter to go down to 0. If it doesn't, let the
2553 // controller be marked offline
2554 // Also, reset all the commands currently owned by the driver
2555 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2556 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2557 list_del_init(&scb->list); // from pending list
2559 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2560 con_log(CL_ANN, (KERN_WARNING
2561 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2562 scb->sno, scb->dev_channel, scb->dev_target));
2564 scb->status = -1;
2566 kioc = (uioc_t *)scb->gp;
2567 kioc->status = -EFAULT;
2569 megaraid_mbox_mm_done(adapter, scb);
2570 } else {
2571 if (scb->scp == scp) { // Found command
2572 con_log(CL_ANN, (KERN_WARNING
2573 "megaraid: %d[%d:%d], reset from pending list\n",
2574 scb->sno, scb->dev_channel, scb->dev_target));
2575 } else {
2576 con_log(CL_ANN, (KERN_WARNING
2577 "megaraid: IO packet with %d[%d:%d] being reset\n",
2578 scb->sno, scb->dev_channel, scb->dev_target));
2581 scb->scp->result = (DID_RESET << 16);
2582 scb->scp->scsi_done(scb->scp);
2584 megaraid_dealloc_scb(adapter, scb);
2587 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2589 if (adapter->outstanding_cmds) {
2590 con_log(CL_ANN, (KERN_NOTICE
2591 "megaraid: %d outstanding commands. Max wait %d sec\n",
2592 adapter->outstanding_cmds,
2593 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2596 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2598 for (i = 0; i < recovery_window; i++) {
2600 megaraid_ack_sequence(adapter);
2602 // print a message once every 5 seconds only
2603 if (!(i % 5)) {
2604 con_log(CL_ANN, (
2605 "megaraid mbox: Wait for %d commands to complete:%d\n",
2606 adapter->outstanding_cmds,
2607 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2610 // bailout if no recovery happened in reset time
2611 if (adapter->outstanding_cmds == 0) {
2612 break;
2615 msleep(1000);
2618 spin_lock(&adapter->lock);
2620 // If still outstanding commands, bail out
2621 if (adapter->outstanding_cmds) {
2622 con_log(CL_ANN, (KERN_WARNING
2623 "megaraid mbox: critical hardware error!\n"));
2625 raid_dev->hw_error = 1;
2627 rval = FAILED;
2628 goto out;
2630 else {
2631 con_log(CL_ANN, (KERN_NOTICE
2632 "megaraid mbox: reset sequence completed successfully\n"));
2636 // If the controller supports clustering, reset reservations
2637 if (!adapter->ha) {
2638 rval = SUCCESS;
2639 goto out;
2642 // clear reservations if any
2643 raw_mbox[0] = CLUSTER_CMD;
2644 raw_mbox[2] = RESET_RESERVATIONS;
2646 rval = SUCCESS;
2647 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2648 con_log(CL_ANN,
2649 (KERN_INFO "megaraid: reservation reset\n"));
2651 else {
2652 rval = FAILED;
2653 con_log(CL_ANN, (KERN_WARNING
2654 "megaraid: reservation reset failed\n"));
2657 out:
2658 spin_unlock(&adapter->lock);
2659 return rval;
2663 * START: internal commands library
2665 * This section of the driver has the common routine used by the driver and
2666 * also has all the FW routines
2670 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2671 * @adapter : controller's soft state
2672 * @raw_mbox : the mailbox
2674 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2675 * controllers.
2677 static int
2678 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2680 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2681 mbox_t *mbox;
2682 uint8_t status;
2683 int i;
2685 mbox = raid_dev->mbox;
2688 * Wait until mailbox is free
2690 if (megaraid_busywait_mbox(raid_dev) != 0)
2691 goto blocked_mailbox;
2694 * Copy mailbox data into host structure
2696 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2697 mbox->cmdid = 0xFE;
2698 mbox->busy = 1;
2699 mbox->poll = 0;
2700 mbox->ack = 0;
2701 mbox->numstatus = 0xFF;
2702 mbox->status = 0xFF;
2704 wmb();
2705 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2707 // wait for maximum 1 second for status to post. If the status is not
2708 // available within 1 second, assume FW is initializing and wait
2709 // for an extended amount of time
2710 if (mbox->numstatus == 0xFF) { // status not yet available
2711 udelay(25);
2713 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2714 rmb();
2715 msleep(1);
2719 if (i == 1000) {
2720 con_log(CL_ANN, (KERN_NOTICE
2721 "megaraid mailbox: wait for FW to boot "));
2723 for (i = 0; (mbox->numstatus == 0xFF) &&
2724 (i < MBOX_RESET_WAIT); i++) {
2725 rmb();
2726 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2727 MBOX_RESET_WAIT - i));
2728 msleep(1000);
2731 if (i == MBOX_RESET_WAIT) {
2733 con_log(CL_ANN, (
2734 "\nmegaraid mailbox: status not available\n"));
2736 return -1;
2738 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2742 // wait for maximum 1 second for poll semaphore
2743 if (mbox->poll != 0x77) {
2744 udelay(25);
2746 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2747 rmb();
2748 msleep(1);
2751 if (i == 1000) {
2752 con_log(CL_ANN, (KERN_WARNING
2753 "megaraid mailbox: could not get poll semaphore\n"));
2754 return -1;
2758 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2759 wmb();
2761 // wait for maximum 1 second for acknowledgement
2762 if (RDINDOOR(raid_dev) & 0x2) {
2763 udelay(25);
2765 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2766 rmb();
2767 msleep(1);
2770 if (i == 1000) {
2771 con_log(CL_ANN, (KERN_WARNING
2772 "megaraid mailbox: could not acknowledge\n"));
2773 return -1;
2776 mbox->poll = 0;
2777 mbox->ack = 0x77;
2779 status = mbox->status;
2781 // invalidate the completed command id array. After command
2782 // completion, firmware would write the valid id.
2783 mbox->numstatus = 0xFF;
2784 mbox->status = 0xFF;
2785 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2786 mbox->completed[i] = 0xFF;
2789 return status;
2791 blocked_mailbox:
2793 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2794 return -1;
2799 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2800 * @adapter : controller's soft state
2801 * @raw_mbox : the mailbox
2803 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2804 * controllers. This is a faster version of the synchronous command and
2805 * therefore can be called in interrupt-context as well.
2807 static int
2808 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2810 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2811 mbox_t *mbox;
2812 long i;
2815 mbox = raid_dev->mbox;
2817 // return immediately if the mailbox is busy
2818 if (mbox->busy) return -1;
2820 // Copy mailbox data into host structure
2821 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2822 mbox->cmdid = 0xFE;
2823 mbox->busy = 1;
2824 mbox->poll = 0;
2825 mbox->ack = 0;
2826 mbox->numstatus = 0xFF;
2827 mbox->status = 0xFF;
2829 wmb();
2830 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2832 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2833 if (mbox->numstatus != 0xFF) break;
2834 rmb();
2835 udelay(MBOX_SYNC_DELAY_200);
2838 if (i == MBOX_SYNC_WAIT_CNT) {
2839 // We may need to re-calibrate the counter
2840 con_log(CL_ANN, (KERN_CRIT
2841 "megaraid: fast sync command timed out\n"));
2844 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2845 wmb();
2847 return mbox->status;
2852 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2853 * @raid_dev : RAID device (HBA) soft state
2855 * Wait until the controller's mailbox is available to accept more commands.
2856 * Wait for at most 1 second.
2858 static int
2859 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2861 mbox_t *mbox = raid_dev->mbox;
2862 int i = 0;
2864 if (mbox->busy) {
2865 udelay(25);
2866 for (i = 0; mbox->busy && i < 1000; i++)
2867 msleep(1);
2870 if (i < 1000) return 0;
2871 else return -1;
2876 * megaraid_mbox_product_info - some static information about the controller
2877 * @adapter : our soft state
2879 * Issue commands to the controller to grab some parameters required by our
2880 * caller.
2882 static int
2883 megaraid_mbox_product_info(adapter_t *adapter)
2885 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2886 mbox_t *mbox;
2887 uint8_t raw_mbox[sizeof(mbox_t)];
2888 mraid_pinfo_t *pinfo;
2889 dma_addr_t pinfo_dma_h;
2890 mraid_inquiry3_t *mraid_inq3;
2891 int i;
2894 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2895 mbox = (mbox_t *)raw_mbox;
2898 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2899 * e.g., max channels, max commands etc.
2901 pinfo = dma_alloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2902 &pinfo_dma_h, GFP_KERNEL);
2903 if (pinfo == NULL) {
2904 con_log(CL_ANN, (KERN_WARNING
2905 "megaraid: out of memory, %s %d\n", __func__,
2906 __LINE__));
2908 return -1;
2911 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2912 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2914 raw_mbox[0] = FC_NEW_CONFIG;
2915 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2916 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2918 // Issue the command
2919 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2921 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2923 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2924 pinfo, pinfo_dma_h);
2926 return -1;
2930 * Collect information about state of each physical drive
2931 * attached to the controller. We will expose all the disks
2932 * which are not part of RAID
2934 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2935 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2936 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2940 * Get product info for information like number of channels,
2941 * maximum commands supported.
2943 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2944 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2946 raw_mbox[0] = FC_NEW_CONFIG;
2947 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
2949 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2951 con_log(CL_ANN, (KERN_WARNING
2952 "megaraid: product info failed\n"));
2954 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2955 pinfo, pinfo_dma_h);
2957 return -1;
2961 * Setup some parameters for host, as required by our caller
2963 adapter->max_channel = pinfo->nchannels;
2966 * we will export all the logical drives on a single channel.
2967 * Add 1 since inquires do not come for inititor ID
2969 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
2970 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
2973 * These are the maximum outstanding commands for the scsi-layer
2975 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
2977 memset(adapter->fw_version, 0, VERSION_SIZE);
2978 memset(adapter->bios_version, 0, VERSION_SIZE);
2980 memcpy(adapter->fw_version, pinfo->fw_version, 4);
2981 adapter->fw_version[4] = 0;
2983 memcpy(adapter->bios_version, pinfo->bios_version, 4);
2984 adapter->bios_version[4] = 0;
2986 con_log(CL_ANN, (KERN_NOTICE
2987 "megaraid: fw version:[%s] bios version:[%s]\n",
2988 adapter->fw_version, adapter->bios_version));
2990 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo,
2991 pinfo_dma_h);
2993 return 0;
2999 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3000 * @adapter : soft state for the controller
3002 * This routine check whether the controller in question supports extended
3003 * ( > 10 bytes ) CDBs.
3005 static int
3006 megaraid_mbox_extended_cdb(adapter_t *adapter)
3008 mbox_t *mbox;
3009 uint8_t raw_mbox[sizeof(mbox_t)];
3010 int rval;
3012 mbox = (mbox_t *)raw_mbox;
3014 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3015 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3017 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3019 raw_mbox[0] = MAIN_MISC_OPCODE;
3020 raw_mbox[2] = SUPPORT_EXT_CDB;
3023 * Issue the command
3025 rval = 0;
3026 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3027 rval = -1;
3030 return rval;
3035 * megaraid_mbox_support_ha - Do we support clustering
3036 * @adapter : soft state for the controller
3037 * @init_id : ID of the initiator
3039 * Determine if the firmware supports clustering and the ID of the initiator.
3041 static int
3042 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3044 mbox_t *mbox;
3045 uint8_t raw_mbox[sizeof(mbox_t)];
3046 int rval;
3049 mbox = (mbox_t *)raw_mbox;
3051 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3053 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3055 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3057 raw_mbox[0] = GET_TARGET_ID;
3059 // Issue the command
3060 *init_id = 7;
3061 rval = -1;
3062 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3064 *init_id = *(uint8_t *)adapter->ibuf;
3066 con_log(CL_ANN, (KERN_INFO
3067 "megaraid: cluster firmware, initiator ID: %d\n",
3068 *init_id));
3070 rval = 0;
3073 return rval;
3078 * megaraid_mbox_support_random_del - Do we support random deletion
3079 * @adapter : soft state for the controller
3081 * Determine if the firmware supports random deletion.
3082 * Return: 1 is operation supported, 0 otherwise
3084 static int
3085 megaraid_mbox_support_random_del(adapter_t *adapter)
3087 uint8_t raw_mbox[sizeof(mbox_t)];
3088 int rval;
3091 * Newer firmware on Dell CERC expect a different
3092 * random deletion handling, so disable it.
3094 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3095 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3096 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3097 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3098 (adapter->fw_version[0] > '6' ||
3099 (adapter->fw_version[0] == '6' &&
3100 adapter->fw_version[2] > '6') ||
3101 (adapter->fw_version[0] == '6'
3102 && adapter->fw_version[2] == '6'
3103 && adapter->fw_version[3] > '1'))) {
3104 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3105 return 0;
3108 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3110 raw_mbox[0] = FC_DEL_LOGDRV;
3111 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3113 // Issue the command
3114 rval = 0;
3115 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3117 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3119 rval = 1;
3122 return rval;
3127 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3128 * @adapter : soft state for the controller
3130 * Find out the maximum number of scatter-gather elements supported by the
3131 * firmware.
3133 static int
3134 megaraid_mbox_get_max_sg(adapter_t *adapter)
3136 mbox_t *mbox;
3137 uint8_t raw_mbox[sizeof(mbox_t)];
3138 int nsg;
3141 mbox = (mbox_t *)raw_mbox;
3143 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3145 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3147 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3149 raw_mbox[0] = MAIN_MISC_OPCODE;
3150 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3152 // Issue the command
3153 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3154 nsg = *(uint8_t *)adapter->ibuf;
3156 else {
3157 nsg = MBOX_DEFAULT_SG_SIZE;
3160 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3162 return nsg;
3167 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3168 * @adapter : soft state for the controller
3170 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3171 * can be exported as regular SCSI channels.
3173 static void
3174 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3176 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3177 mbox_t *mbox;
3178 uint8_t raw_mbox[sizeof(mbox_t)];
3181 mbox = (mbox_t *)raw_mbox;
3183 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3185 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3187 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3189 raw_mbox[0] = CHNL_CLASS;
3190 raw_mbox[2] = GET_CHNL_CLASS;
3192 // Issue the command. If the command fails, all channels are RAID
3193 // channels
3194 raid_dev->channel_class = 0xFF;
3195 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3196 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3199 return;
3204 * megaraid_mbox_flush_cache - flush adapter and disks cache
3205 * @adapter : soft state for the controller
3207 * Flush adapter cache followed by disks cache.
3209 static void
3210 megaraid_mbox_flush_cache(adapter_t *adapter)
3212 uint8_t raw_mbox[sizeof(mbox_t)];
3214 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3216 raw_mbox[0] = FLUSH_ADAPTER;
3218 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3219 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3222 raw_mbox[0] = FLUSH_SYSTEM;
3224 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3225 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3228 return;
3233 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3234 * @adapter : soft state for the controller
3236 * Clears the pending cmds in FW and reinits its RAID structs.
3238 static int
3239 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3241 mbox_t *mbox;
3242 uint8_t raw_mbox[sizeof(mbox_t)];
3243 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3244 int status = 0;
3245 int i;
3246 uint32_t dword;
3248 mbox = (mbox_t *)raw_mbox;
3250 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3252 raw_mbox[0] = 0xFF;
3254 mbox = raid_dev->mbox;
3256 /* Wait until mailbox is free */
3257 if (megaraid_busywait_mbox(raid_dev) != 0) {
3258 status = 1;
3259 goto blocked_mailbox;
3262 /* Copy mailbox data into host structure */
3263 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3264 mbox->cmdid = 0xFE;
3265 mbox->busy = 1;
3266 mbox->poll = 0;
3267 mbox->ack = 0;
3268 mbox->numstatus = 0;
3269 mbox->status = 0;
3271 wmb();
3272 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3274 /* Wait for maximum 1 min for status to post.
3275 * If the Firmware SUPPORTS the ABOVE COMMAND,
3276 * mbox->cmd will be set to 0
3277 * else
3278 * the firmware will reject the command with
3279 * mbox->numstatus set to 1
3282 i = 0;
3283 status = 0;
3284 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3285 rmb();
3286 msleep(1);
3287 i++;
3288 if (i > 1000 * 60) {
3289 status = 1;
3290 break;
3293 if (mbox->numstatus == 1)
3294 status = 1; /*cmd not supported*/
3296 /* Check for interrupt line */
3297 dword = RDOUTDOOR(raid_dev);
3298 WROUTDOOR(raid_dev, dword);
3299 WRINDOOR(raid_dev,2);
3301 return status;
3303 blocked_mailbox:
3304 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3305 return status;
3309 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3310 * @adapter : controller's soft state
3311 * @scb : SCB to be displayed
3312 * @level : debug level for console print
3314 * Diplay information about the given SCB iff the current debug level is
3315 * verbose.
3317 static void
3318 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3320 mbox_ccb_t *ccb;
3321 struct scsi_cmnd *scp;
3322 mbox_t *mbox;
3323 int level;
3324 int i;
3327 ccb = (mbox_ccb_t *)scb->ccb;
3328 scp = scb->scp;
3329 mbox = ccb->mbox;
3331 level = CL_DLEVEL3;
3333 con_log(level, (KERN_NOTICE
3334 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3335 mbox->cmd, scb->sno));
3337 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3338 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3339 mbox->numsge));
3341 if (!scp) return;
3343 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3345 for (i = 0; i < scp->cmd_len; i++) {
3346 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3349 con_log(level, ("\n"));
3351 return;
3356 * megaraid_mbox_setup_device_map - manage device ids
3357 * @adapter : Driver's soft state
3359 * Manage the device ids to have an appropriate mapping between the kernel
3360 * scsi addresses and megaraid scsi and logical drive addresses. We export
3361 * scsi devices on their actual addresses, whereas the logical drives are
3362 * exported on a virtual scsi channel.
3364 static void
3365 megaraid_mbox_setup_device_map(adapter_t *adapter)
3367 uint8_t c;
3368 uint8_t t;
3371 * First fill the values on the logical drive channel
3373 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3374 adapter->device_ids[adapter->max_channel][t] =
3375 (t < adapter->init_id) ? t : t - 1;
3377 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3380 * Fill the values on the physical devices channels
3382 for (c = 0; c < adapter->max_channel; c++)
3383 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3384 adapter->device_ids[c][t] = (c << 8) | t;
3389 * END: internal commands library
3393 * START: Interface for the common management module
3395 * This is the module, which interfaces with the common management module to
3396 * provide support for ioctl and sysfs
3400 * megaraid_cmm_register - register with the management module
3401 * @adapter : HBA soft state
3403 * Register with the management module, which allows applications to issue
3404 * ioctl calls to the drivers. This interface is used by the management module
3405 * to setup sysfs support as well.
3407 static int
3408 megaraid_cmm_register(adapter_t *adapter)
3410 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3411 mraid_mmadp_t adp;
3412 scb_t *scb;
3413 mbox_ccb_t *ccb;
3414 int rval;
3415 int i;
3417 // Allocate memory for the base list of scb for management module.
3418 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3420 if (adapter->uscb_list == NULL) {
3421 con_log(CL_ANN, (KERN_WARNING
3422 "megaraid: out of memory, %s %d\n", __func__,
3423 __LINE__));
3424 return -1;
3428 // Initialize the synchronization parameters for resources for
3429 // commands for management module
3430 INIT_LIST_HEAD(&adapter->uscb_pool);
3432 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3436 // link all the packets. Note, CCB for commands, coming from the
3437 // commom management module, mailbox physical address are already
3438 // setup by it. We just need placeholder for that in our local command
3439 // control blocks
3440 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3442 scb = adapter->uscb_list + i;
3443 ccb = raid_dev->uccb_list + i;
3445 scb->ccb = (caddr_t)ccb;
3446 ccb->mbox64 = raid_dev->umbox64 + i;
3447 ccb->mbox = &ccb->mbox64->mbox32;
3448 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3450 scb->gp = 0;
3452 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3453 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3454 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3456 scb->scp = NULL;
3457 scb->state = SCB_FREE;
3458 scb->dma_direction = DMA_NONE;
3459 scb->dma_type = MRAID_DMA_NONE;
3460 scb->dev_channel = -1;
3461 scb->dev_target = -1;
3463 // put scb in the free pool
3464 list_add_tail(&scb->list, &adapter->uscb_pool);
3467 adp.unique_id = adapter->unique_id;
3468 adp.drvr_type = DRVRTYPE_MBOX;
3469 adp.drvr_data = (unsigned long)adapter;
3470 adp.pdev = adapter->pdev;
3471 adp.issue_uioc = megaraid_mbox_mm_handler;
3472 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3473 adp.max_kioc = MBOX_MAX_USER_CMDS;
3475 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3477 con_log(CL_ANN, (KERN_WARNING
3478 "megaraid mbox: did not register with CMM\n"));
3480 kfree(adapter->uscb_list);
3483 return rval;
3488 * megaraid_cmm_unregister - un-register with the management module
3489 * @adapter : HBA soft state
3491 * Un-register with the management module.
3492 * FIXME: mgmt module must return failure for unregister if it has pending
3493 * commands in LLD.
3495 static int
3496 megaraid_cmm_unregister(adapter_t *adapter)
3498 kfree(adapter->uscb_list);
3499 mraid_mm_unregister_adp(adapter->unique_id);
3500 return 0;
3505 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3506 * @drvr_data : LLD specific data
3507 * @kioc : CMM interface packet
3508 * @action : command action
3510 * This routine is invoked whenever the Common Management Module (CMM) has a
3511 * command for us. The 'action' parameter specifies if this is a new command
3512 * or otherwise.
3514 static int
3515 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3517 adapter_t *adapter;
3519 if (action != IOCTL_ISSUE) {
3520 con_log(CL_ANN, (KERN_WARNING
3521 "megaraid: unsupported management action:%#2x\n",
3522 action));
3523 return (-ENOTSUPP);
3526 adapter = (adapter_t *)drvr_data;
3528 // make sure this adapter is not being detached right now.
3529 if (atomic_read(&adapter->being_detached)) {
3530 con_log(CL_ANN, (KERN_WARNING
3531 "megaraid: reject management request, detaching\n"));
3532 return (-ENODEV);
3535 switch (kioc->opcode) {
3537 case GET_ADAP_INFO:
3539 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3540 (unsigned long)kioc->buf_vaddr);
3542 kioc->done(kioc);
3544 return kioc->status;
3546 case MBOX_CMD:
3548 return megaraid_mbox_mm_command(adapter, kioc);
3550 default:
3551 kioc->status = (-EINVAL);
3552 kioc->done(kioc);
3553 return (-EINVAL);
3556 return 0; // not reached
3560 * megaraid_mbox_mm_command - issues commands routed through CMM
3561 * @adapter : HBA soft state
3562 * @kioc : management command packet
3564 * Issues commands, which are routed through the management module.
3566 static int
3567 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3569 struct list_head *head = &adapter->uscb_pool;
3570 mbox64_t *mbox64;
3571 uint8_t *raw_mbox;
3572 scb_t *scb;
3573 mbox_ccb_t *ccb;
3574 unsigned long flags;
3576 // detach one scb from free pool
3577 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3579 if (list_empty(head)) { // should never happen because of CMM
3581 con_log(CL_ANN, (KERN_WARNING
3582 "megaraid mbox: bug in cmm handler, lost resources\n"));
3584 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3586 return (-EINVAL);
3589 scb = list_entry(head->next, scb_t, list);
3590 list_del_init(&scb->list);
3592 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3594 scb->state = SCB_ACTIVE;
3595 scb->dma_type = MRAID_DMA_NONE;
3596 scb->dma_direction = DMA_NONE;
3598 ccb = (mbox_ccb_t *)scb->ccb;
3599 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3600 raw_mbox = (uint8_t *)&mbox64->mbox32;
3602 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3604 scb->gp = (unsigned long)kioc;
3607 * If it is a logdrv random delete operation, we have to wait till
3608 * there are no outstanding cmds at the fw and then issue it directly
3610 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3612 if (wait_till_fw_empty(adapter)) {
3613 con_log(CL_ANN, (KERN_NOTICE
3614 "megaraid mbox: LD delete, timed out\n"));
3616 kioc->status = -ETIME;
3618 scb->status = -1;
3620 megaraid_mbox_mm_done(adapter, scb);
3622 return (-ETIME);
3625 INIT_LIST_HEAD(&scb->list);
3627 scb->state = SCB_ISSUED;
3628 if (mbox_post_cmd(adapter, scb) != 0) {
3630 con_log(CL_ANN, (KERN_NOTICE
3631 "megaraid mbox: LD delete, mailbox busy\n"));
3633 kioc->status = -EBUSY;
3635 scb->status = -1;
3637 megaraid_mbox_mm_done(adapter, scb);
3639 return (-EBUSY);
3642 return 0;
3645 // put the command on the pending list and execute
3646 megaraid_mbox_runpendq(adapter, scb);
3648 return 0;
3652 static int
3653 wait_till_fw_empty(adapter_t *adapter)
3655 unsigned long flags = 0;
3656 int i;
3660 * Set the quiescent flag to stop issuing cmds to FW.
3662 spin_lock_irqsave(&adapter->lock, flags);
3663 adapter->quiescent++;
3664 spin_unlock_irqrestore(&adapter->lock, flags);
3667 * Wait till there are no more cmds outstanding at FW. Try for at most
3668 * 60 seconds
3670 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3671 con_log(CL_DLEVEL1, (KERN_INFO
3672 "megaraid: FW has %d pending commands\n",
3673 adapter->outstanding_cmds));
3675 msleep(1000);
3678 return adapter->outstanding_cmds;
3683 * megaraid_mbox_mm_done - callback for CMM commands
3684 * @adapter : HBA soft state
3685 * @scb : completed command
3687 * Callback routine for internal commands originated from the management
3688 * module.
3690 static void
3691 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3693 uioc_t *kioc;
3694 mbox64_t *mbox64;
3695 uint8_t *raw_mbox;
3696 unsigned long flags;
3698 kioc = (uioc_t *)scb->gp;
3699 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3700 mbox64->mbox32.status = scb->status;
3701 raw_mbox = (uint8_t *)&mbox64->mbox32;
3704 // put scb in the free pool
3705 scb->state = SCB_FREE;
3706 scb->scp = NULL;
3708 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3710 list_add(&scb->list, &adapter->uscb_pool);
3712 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3714 // if a delete logical drive operation succeeded, restart the
3715 // controller
3716 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3718 adapter->quiescent--;
3720 megaraid_mbox_runpendq(adapter, NULL);
3723 kioc->done(kioc);
3725 return;
3730 * gather_hbainfo - HBA characteristics for the applications
3731 * @adapter : HBA soft state
3732 * @hinfo : pointer to the caller's host info strucuture
3734 static int
3735 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3737 hinfo->pci_vendor_id = adapter->pdev->vendor;
3738 hinfo->pci_device_id = adapter->pdev->device;
3739 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3740 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3742 hinfo->pci_bus = adapter->pdev->bus->number;
3743 hinfo->pci_dev_fn = adapter->pdev->devfn;
3744 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3745 hinfo->irq = adapter->host->irq;
3746 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3748 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3749 hinfo->host_no = adapter->host->host_no;
3751 return 0;
3755 * END: Interface for the common management module
3761 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3762 * @adapter : controller's soft state
3764 * Allocate packets required to issue FW calls whenever the sysfs attributes
3765 * are read. These attributes would require up-to-date information from the
3766 * FW. Also set up resources for mutual exclusion to share these resources and
3767 * the wait queue.
3769 * Return 0 on success.
3770 * Return -ERROR_CODE on failure.
3772 static int
3773 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3775 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3776 int rval = 0;
3778 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3780 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3782 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev,
3783 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL);
3785 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3786 !raid_dev->sysfs_buffer) {
3788 con_log(CL_ANN, (KERN_WARNING
3789 "megaraid: out of memory, %s %d\n", __func__,
3790 __LINE__));
3792 rval = -ENOMEM;
3794 megaraid_sysfs_free_resources(adapter);
3797 mutex_init(&raid_dev->sysfs_mtx);
3799 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3801 return rval;
3806 * megaraid_sysfs_free_resources - free sysfs related resources
3807 * @adapter : controller's soft state
3809 * Free packets allocated for sysfs FW commands
3811 static void
3812 megaraid_sysfs_free_resources(adapter_t *adapter)
3814 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3816 kfree(raid_dev->sysfs_uioc);
3817 kfree(raid_dev->sysfs_mbox64);
3819 if (raid_dev->sysfs_buffer) {
3820 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE,
3821 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3827 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3828 * @uioc : completed packet
3830 * Callback routine called in the ISR/tasklet context for get ldmap call
3832 static void
3833 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3835 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3836 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3838 uioc->status = 0;
3840 wake_up(&raid_dev->sysfs_wait_q);
3844 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3845 * @t : timed out timer
3847 * Timeout routine to recover and return to application, in case the adapter
3848 * has stopped responding. A timeout of 60 seconds for this command seems like
3849 * a good value.
3851 static void
3852 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
3854 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3855 uioc_t *uioc = timeout->uioc;
3856 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3857 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3859 uioc->status = -ETIME;
3861 wake_up(&raid_dev->sysfs_wait_q);
3866 * megaraid_sysfs_get_ldmap - get update logical drive map
3867 * @adapter : controller's soft state
3869 * This routine will be called whenever user reads the logical drive
3870 * attributes, go get the current logical drive mapping table from the
3871 * firmware. We use the management API's to issue commands to the controller.
3873 * NOTE: The commands issuance functionality is not generalized and
3874 * implemented in context of "get ld map" command only. If required, the
3875 * command issuance logical can be trivially pulled out and implemented as a
3876 * standalone library. For now, this should suffice since there is no other
3877 * user of this interface.
3879 * Return 0 on success.
3880 * Return -1 on failure.
3882 static int
3883 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3885 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3886 uioc_t *uioc;
3887 mbox64_t *mbox64;
3888 mbox_t *mbox;
3889 char *raw_mbox;
3890 struct uioc_timeout timeout;
3891 caddr_t ldmap;
3892 int rval = 0;
3895 * Allow only one read at a time to go through the sysfs attributes
3897 mutex_lock(&raid_dev->sysfs_mtx);
3899 uioc = raid_dev->sysfs_uioc;
3900 mbox64 = raid_dev->sysfs_mbox64;
3901 ldmap = raid_dev->sysfs_buffer;
3903 memset(uioc, 0, sizeof(uioc_t));
3904 memset(mbox64, 0, sizeof(mbox64_t));
3905 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3907 mbox = &mbox64->mbox32;
3908 raw_mbox = (char *)mbox;
3909 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3910 uioc->buf_vaddr = (caddr_t)adapter;
3911 uioc->status = -ENODATA;
3912 uioc->done = megaraid_sysfs_get_ldmap_done;
3915 * Prepare the mailbox packet to get the current logical drive mapping
3916 * table
3918 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3920 raw_mbox[0] = FC_DEL_LOGDRV;
3921 raw_mbox[2] = OP_GET_LDID_MAP;
3924 * Setup a timer to recover from a non-responding controller
3926 timeout.uioc = uioc;
3927 timer_setup_on_stack(&timeout.timer,
3928 megaraid_sysfs_get_ldmap_timeout, 0);
3930 timeout.timer.expires = jiffies + 60 * HZ;
3931 add_timer(&timeout.timer);
3934 * Send the command to the firmware
3936 rval = megaraid_mbox_mm_command(adapter, uioc);
3938 if (rval == 0) { // command successfully issued
3939 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3942 * Check if the command timed out
3944 if (uioc->status == -ETIME) {
3945 con_log(CL_ANN, (KERN_NOTICE
3946 "megaraid: sysfs get ld map timed out\n"));
3948 rval = -ETIME;
3950 else {
3951 rval = mbox->status;
3954 if (rval == 0) {
3955 memcpy(raid_dev->curr_ldmap, ldmap,
3956 sizeof(raid_dev->curr_ldmap));
3958 else {
3959 con_log(CL_ANN, (KERN_NOTICE
3960 "megaraid: get ld map failed with %x\n", rval));
3963 else {
3964 con_log(CL_ANN, (KERN_NOTICE
3965 "megaraid: could not issue ldmap command:%x\n", rval));
3969 del_timer_sync(&timeout.timer);
3970 destroy_timer_on_stack(&timeout.timer);
3972 mutex_unlock(&raid_dev->sysfs_mtx);
3974 return rval;
3979 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
3980 * @cdev : class device object representation for the host
3981 * @buf : buffer to send data to
3983 * Display the handle used by the applications while executing management
3984 * tasks on the adapter. We invoke a management module API to get the adapter
3985 * handle, since we do not interface with applications directly.
3987 static ssize_t
3988 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
3989 char *buf)
3991 struct Scsi_Host *shost = class_to_shost(dev);
3992 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
3993 uint32_t app_hndl;
3995 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
3997 return snprintf(buf, 8, "%u\n", app_hndl);
4002 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4003 * @dev : device object representation for the scsi device
4004 * @attr : device attribute to show
4005 * @buf : buffer to send data to
4007 * Display the logical drive number for the device in question, if it a valid
4008 * logical drive. For physical devices, "-1" is returned.
4010 * The logical drive number is displayed in following format:
4012 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4014 * <int> <int> <int> <int>
4016 static ssize_t
4017 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4019 struct scsi_device *sdev = to_scsi_device(dev);
4020 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4021 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4022 int scsi_id = -1;
4023 int logical_drv = -1;
4024 int ldid_map = -1;
4025 uint32_t app_hndl = 0;
4026 int mapped_sdev_id;
4027 int rval;
4028 int i;
4030 if (raid_dev->random_del_supported &&
4031 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4033 rval = megaraid_sysfs_get_ldmap(adapter);
4034 if (rval == 0) {
4036 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4038 mapped_sdev_id = sdev->id;
4040 if (sdev->id > adapter->init_id) {
4041 mapped_sdev_id -= 1;
4044 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4046 scsi_id = sdev->id;
4048 logical_drv = i;
4050 ldid_map = raid_dev->curr_ldmap[i];
4052 app_hndl = mraid_mm_adapter_app_handle(
4053 adapter->unique_id);
4055 break;
4059 else {
4060 con_log(CL_ANN, (KERN_NOTICE
4061 "megaraid: sysfs get ld map failed: %x\n",
4062 rval));
4066 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4067 ldid_map, app_hndl);
4072 * END: Mailbox Low Level Driver
4074 module_init(megaraid_init);
4075 module_exit(megaraid_exit);
4077 /* vim: set ts=8 sw=8 tw=78 ai si: */