perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_mbox.c
blob3b7abe5ca7f5d56e417ce9b25528ce7eb9a11cb9
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 .use_clustering = ENABLE_CLUSTERING,
340 .no_write_same = 1,
341 .sdev_attrs = megaraid_sdev_attrs,
342 .shost_attrs = megaraid_shost_attrs,
347 * megaraid_init - module load hook
349 * We register ourselves as hotplug enabled module and let PCI subsystem
350 * discover our adapters.
352 static int __init
353 megaraid_init(void)
355 int rval;
357 // Announce the driver version
358 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
359 MEGARAID_EXT_VERSION));
361 // check validity of module parameters
362 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
364 con_log(CL_ANN, (KERN_WARNING
365 "megaraid mailbox: max commands per lun reset to %d\n",
366 MBOX_MAX_SCSI_CMDS));
368 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
372 // register as a PCI hot-plug driver module
373 rval = pci_register_driver(&megaraid_pci_driver);
374 if (rval < 0) {
375 con_log(CL_ANN, (KERN_WARNING
376 "megaraid: could not register hotplug support.\n"));
379 return rval;
384 * megaraid_exit - driver unload entry point
386 * We simply unwrap the megaraid_init routine here.
388 static void __exit
389 megaraid_exit(void)
391 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
393 // unregister as PCI hotplug driver
394 pci_unregister_driver(&megaraid_pci_driver);
396 return;
401 * megaraid_probe_one - PCI hotplug entry point
402 * @pdev : handle to this controller's PCI configuration space
403 * @id : pci device id of the class of controllers
405 * This routine should be called whenever a new adapter is detected by the
406 * PCI hotplug susbsystem.
408 static int
409 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
411 adapter_t *adapter;
414 // detected a new controller
415 con_log(CL_ANN, (KERN_INFO
416 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
417 pdev->vendor, pdev->device, pdev->subsystem_vendor,
418 pdev->subsystem_device));
420 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
421 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
423 if (pci_enable_device(pdev)) {
424 con_log(CL_ANN, (KERN_WARNING
425 "megaraid: pci_enable_device failed\n"));
427 return -ENODEV;
430 // Enable bus-mastering on this controller
431 pci_set_master(pdev);
433 // Allocate the per driver initialization structure
434 adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
436 if (adapter == NULL) {
437 con_log(CL_ANN, (KERN_WARNING
438 "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
440 goto out_probe_one;
444 // set up PCI related soft state and other pre-known parameters
445 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
446 adapter->irq = pdev->irq;
447 adapter->pdev = pdev;
449 atomic_set(&adapter->being_detached, 0);
451 // Setup the default DMA mask. This would be changed later on
452 // depending on hardware capabilities
453 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(32))) {
454 con_log(CL_ANN, (KERN_WARNING
455 "megaraid: dma_set_mask failed:%d\n", __LINE__));
457 goto out_free_adapter;
461 // Initialize the synchronization lock for kernel and LLD
462 spin_lock_init(&adapter->lock);
464 // Initialize the command queues: the list of free SCBs and the list
465 // of pending SCBs.
466 INIT_LIST_HEAD(&adapter->kscb_pool);
467 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
469 INIT_LIST_HEAD(&adapter->pend_list);
470 spin_lock_init(PENDING_LIST_LOCK(adapter));
472 INIT_LIST_HEAD(&adapter->completed_list);
473 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
476 // Start the mailbox based controller
477 if (megaraid_init_mbox(adapter) != 0) {
478 con_log(CL_ANN, (KERN_WARNING
479 "megaraid: mailbox adapter did not initialize\n"));
481 goto out_free_adapter;
484 // Register with LSI Common Management Module
485 if (megaraid_cmm_register(adapter) != 0) {
487 con_log(CL_ANN, (KERN_WARNING
488 "megaraid: could not register with management module\n"));
490 goto out_fini_mbox;
493 // setup adapter handle in PCI soft state
494 pci_set_drvdata(pdev, adapter);
496 // attach with scsi mid-layer
497 if (megaraid_io_attach(adapter) != 0) {
499 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
501 goto out_cmm_unreg;
504 return 0;
506 out_cmm_unreg:
507 megaraid_cmm_unregister(adapter);
508 out_fini_mbox:
509 megaraid_fini_mbox(adapter);
510 out_free_adapter:
511 kfree(adapter);
512 out_probe_one:
513 pci_disable_device(pdev);
515 return -ENODEV;
520 * megaraid_detach_one - release framework resources and call LLD release routine
521 * @pdev : handle for our PCI configuration space
523 * This routine is called during driver unload. We free all the allocated
524 * resources and call the corresponding LLD so that it can also release all
525 * its resources.
527 * This routine is also called from the PCI hotplug system.
529 static void
530 megaraid_detach_one(struct pci_dev *pdev)
532 adapter_t *adapter;
533 struct Scsi_Host *host;
536 // Start a rollback on this adapter
537 adapter = pci_get_drvdata(pdev);
539 if (!adapter) {
540 con_log(CL_ANN, (KERN_CRIT
541 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
542 pdev->vendor, pdev->device, pdev->subsystem_vendor,
543 pdev->subsystem_device));
545 return;
547 else {
548 con_log(CL_ANN, (KERN_NOTICE
549 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
550 pdev->vendor, pdev->device, pdev->subsystem_vendor,
551 pdev->subsystem_device));
555 host = adapter->host;
557 // do not allow any more requests from the management module for this
558 // adapter.
559 // FIXME: How do we account for the request which might still be
560 // pending with us?
561 atomic_set(&adapter->being_detached, 1);
563 // detach from the IO sub-system
564 megaraid_io_detach(adapter);
566 // Unregister from common management module
568 // FIXME: this must return success or failure for conditions if there
569 // is a command pending with LLD or not.
570 megaraid_cmm_unregister(adapter);
572 // finalize the mailbox based controller and release all resources
573 megaraid_fini_mbox(adapter);
575 kfree(adapter);
577 scsi_host_put(host);
579 pci_disable_device(pdev);
581 return;
586 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
587 * @pdev : generic driver model device
589 * Shutdown notification, perform flush cache.
591 static void
592 megaraid_mbox_shutdown(struct pci_dev *pdev)
594 adapter_t *adapter = pci_get_drvdata(pdev);
595 static int counter;
597 if (!adapter) {
598 con_log(CL_ANN, (KERN_WARNING
599 "megaraid: null device in shutdown\n"));
600 return;
603 // flush caches now
604 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
605 counter++));
607 megaraid_mbox_flush_cache(adapter);
609 con_log(CL_ANN, ("done\n"));
614 * megaraid_io_attach - attach a device with the IO subsystem
615 * @adapter : controller's soft state
617 * Attach this device with the IO subsystem.
619 static int
620 megaraid_io_attach(adapter_t *adapter)
622 struct Scsi_Host *host;
624 // Initialize SCSI Host structure
625 host = scsi_host_alloc(&megaraid_template_g, 8);
626 if (!host) {
627 con_log(CL_ANN, (KERN_WARNING
628 "megaraid mbox: scsi_register failed\n"));
630 return -1;
633 SCSIHOST2ADAP(host) = (caddr_t)adapter;
634 adapter->host = host;
636 host->irq = adapter->irq;
637 host->unique_id = adapter->unique_id;
638 host->can_queue = adapter->max_cmds;
639 host->this_id = adapter->init_id;
640 host->sg_tablesize = adapter->sglen;
641 host->max_sectors = adapter->max_sectors;
642 host->cmd_per_lun = adapter->cmd_per_lun;
643 host->max_channel = adapter->max_channel;
644 host->max_id = adapter->max_target;
645 host->max_lun = adapter->max_lun;
648 // notify mid-layer about the new controller
649 if (scsi_add_host(host, &adapter->pdev->dev)) {
651 con_log(CL_ANN, (KERN_WARNING
652 "megaraid mbox: scsi_add_host failed\n"));
654 scsi_host_put(host);
656 return -1;
659 scsi_scan_host(host);
661 return 0;
666 * megaraid_io_detach - detach a device from the IO subsystem
667 * @adapter : controller's soft state
669 * Detach this device from the IO subsystem.
671 static void
672 megaraid_io_detach(adapter_t *adapter)
674 struct Scsi_Host *host;
676 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
678 host = adapter->host;
680 scsi_remove_host(host);
682 return;
687 * START: Mailbox Low Level Driver
689 * This is section specific to the single mailbox based controllers
693 * megaraid_init_mbox - initialize controller
694 * @adapter : our soft state
696 * - Allocate 16-byte aligned mailbox memory for firmware handshake
697 * - Allocate controller's memory resources
698 * - Find out all initialization data
699 * - Allocate memory required for all the commands
700 * - Use internal library of FW routines, build up complete soft state
702 static int
703 megaraid_init_mbox(adapter_t *adapter)
705 struct pci_dev *pdev;
706 mraid_device_t *raid_dev;
707 int i;
708 uint32_t magic64;
711 adapter->ito = MBOX_TIMEOUT;
712 pdev = adapter->pdev;
715 * Allocate and initialize the init data structure for mailbox
716 * controllers
718 raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
719 if (raid_dev == NULL) return -1;
723 * Attach the adapter soft state to raid device soft state
725 adapter->raid_device = (caddr_t)raid_dev;
726 raid_dev->fast_load = megaraid_fast_load;
729 // our baseport
730 raid_dev->baseport = pci_resource_start(pdev, 0);
732 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
734 con_log(CL_ANN, (KERN_WARNING
735 "megaraid: mem region busy\n"));
737 goto out_free_raid_dev;
740 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
742 if (!raid_dev->baseaddr) {
744 con_log(CL_ANN, (KERN_WARNING
745 "megaraid: could not map hba memory\n") );
747 goto out_release_regions;
750 /* initialize the mutual exclusion lock for the mailbox */
751 spin_lock_init(&raid_dev->mailbox_lock);
753 /* allocate memory required for commands */
754 if (megaraid_alloc_cmd_packets(adapter) != 0)
755 goto out_iounmap;
758 * Issue SYNC cmd to flush the pending cmds in the adapter
759 * and initialize its internal state
762 if (megaraid_mbox_fire_sync_cmd(adapter))
763 con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
766 * Setup the rest of the soft state using the library of
767 * FW routines
770 /* request IRQ and register the interrupt service routine */
771 if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
772 adapter)) {
774 con_log(CL_ANN, (KERN_WARNING
775 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
776 goto out_alloc_cmds;
780 // Product info
781 if (megaraid_mbox_product_info(adapter) != 0)
782 goto out_free_irq;
784 // Do we support extended CDBs
785 adapter->max_cdb_sz = 10;
786 if (megaraid_mbox_extended_cdb(adapter) == 0) {
787 adapter->max_cdb_sz = 16;
791 * Do we support cluster environment, if we do, what is the initiator
792 * id.
793 * NOTE: In a non-cluster aware firmware environment, the LLD should
794 * return 7 as initiator id.
796 adapter->ha = 0;
797 adapter->init_id = -1;
798 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
799 adapter->ha = 1;
803 * Prepare the device ids array to have the mapping between the kernel
804 * device address and megaraid device address.
805 * We export the physical devices on their actual addresses. The
806 * logical drives are exported on a virtual SCSI channel
808 megaraid_mbox_setup_device_map(adapter);
810 // If the firmware supports random deletion, update the device id map
811 if (megaraid_mbox_support_random_del(adapter)) {
813 // Change the logical drives numbers in device_ids array one
814 // slot in device_ids is reserved for target id, that's why
815 // "<=" below
816 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
817 adapter->device_ids[adapter->max_channel][i] += 0x80;
819 adapter->device_ids[adapter->max_channel][adapter->init_id] =
820 0xFF;
822 raid_dev->random_del_supported = 1;
826 * find out the maximum number of scatter-gather elements supported by
827 * this firmware
829 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
831 // enumerate RAID and SCSI channels so that all devices on SCSI
832 // channels can later be exported, including disk devices
833 megaraid_mbox_enum_raid_scsi(adapter);
836 * Other parameters required by upper layer
838 * maximum number of sectors per IO command
840 adapter->max_sectors = megaraid_max_sectors;
843 * number of queued commands per LUN.
845 adapter->cmd_per_lun = megaraid_cmd_per_lun;
848 * Allocate resources required to issue FW calls, when sysfs is
849 * accessed
851 if (megaraid_sysfs_alloc_resources(adapter) != 0)
852 goto out_free_irq;
854 // Set the DMA mask to 64-bit. All supported controllers as capable of
855 // DMA in this range
856 pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
858 if (((magic64 == HBA_SIGNATURE_64_BIT) &&
859 ((adapter->pdev->subsystem_device !=
860 PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
861 (adapter->pdev->subsystem_device !=
862 PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
863 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
864 adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
865 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
866 adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
867 (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
868 adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
869 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
870 adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
871 (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
872 adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
873 if (dma_set_mask(&adapter->pdev->dev, DMA_BIT_MASK(64))) {
874 con_log(CL_ANN, (KERN_WARNING
875 "megaraid: DMA mask for 64-bit failed\n"));
877 if (dma_set_mask(&adapter->pdev->dev,
878 DMA_BIT_MASK(32))) {
879 con_log(CL_ANN, (KERN_WARNING
880 "megaraid: 32-bit DMA mask failed\n"));
881 goto out_free_sysfs_res;
886 // setup tasklet for DPC
887 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
888 (unsigned long)adapter);
890 con_log(CL_DLEVEL1, (KERN_INFO
891 "megaraid mbox hba successfully initialized\n"));
893 return 0;
895 out_free_sysfs_res:
896 megaraid_sysfs_free_resources(adapter);
897 out_free_irq:
898 free_irq(adapter->irq, adapter);
899 out_alloc_cmds:
900 megaraid_free_cmd_packets(adapter);
901 out_iounmap:
902 iounmap(raid_dev->baseaddr);
903 out_release_regions:
904 pci_release_regions(pdev);
905 out_free_raid_dev:
906 kfree(raid_dev);
908 return -1;
913 * megaraid_fini_mbox - undo controller initialization
914 * @adapter : our soft state
916 static void
917 megaraid_fini_mbox(adapter_t *adapter)
919 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
921 // flush all caches
922 megaraid_mbox_flush_cache(adapter);
924 tasklet_kill(&adapter->dpc_h);
926 megaraid_sysfs_free_resources(adapter);
928 megaraid_free_cmd_packets(adapter);
930 free_irq(adapter->irq, adapter);
932 iounmap(raid_dev->baseaddr);
934 pci_release_regions(adapter->pdev);
936 kfree(raid_dev);
938 return;
943 * megaraid_alloc_cmd_packets - allocate shared mailbox
944 * @adapter : soft state of the raid controller
946 * Allocate and align the shared mailbox. This mailbox is used to issue
947 * all the commands. For IO based controllers, the mailbox is also registered
948 * with the FW. Allocate memory for all commands as well.
949 * This is our big allocator.
951 static int
952 megaraid_alloc_cmd_packets(adapter_t *adapter)
954 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
955 struct pci_dev *pdev;
956 unsigned long align;
957 scb_t *scb;
958 mbox_ccb_t *ccb;
959 struct mraid_pci_blk *epthru_pci_blk;
960 struct mraid_pci_blk *sg_pci_blk;
961 struct mraid_pci_blk *mbox_pci_blk;
962 int i;
964 pdev = adapter->pdev;
967 * Setup the mailbox
968 * Allocate the common 16-byte aligned memory for the handshake
969 * mailbox.
971 raid_dev->una_mbox64 = dma_zalloc_coherent(&adapter->pdev->dev,
972 sizeof(mbox64_t), &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_zalloc_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 if (raid_dev->sg_pool_handle)
1247 dma_pool_destroy(raid_dev->sg_pool_handle);
1250 epthru_pci_blk = raid_dev->epthru_pool;
1251 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1252 dma_pool_free(raid_dev->epthru_pool_handle,
1253 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1255 if (raid_dev->epthru_pool_handle)
1256 dma_pool_destroy(raid_dev->epthru_pool_handle);
1259 mbox_pci_blk = raid_dev->mbox_pool;
1260 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1261 dma_pool_free(raid_dev->mbox_pool_handle,
1262 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1264 if (raid_dev->mbox_pool_handle)
1265 dma_pool_destroy(raid_dev->mbox_pool_handle);
1267 return;
1272 * megaraid_alloc_scb - detach and return a scb from the free list
1273 * @adapter : controller's soft state
1274 * @scp : pointer to the scsi command to be executed
1276 * Return the scb from the head of the free list. %NULL if there are none
1277 * available.
1279 static scb_t *
1280 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1282 struct list_head *head = &adapter->kscb_pool;
1283 scb_t *scb = NULL;
1284 unsigned long flags;
1286 // detach scb from free pool
1287 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1289 if (list_empty(head)) {
1290 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1291 return NULL;
1294 scb = list_entry(head->next, scb_t, list);
1295 list_del_init(&scb->list);
1297 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1299 scb->state = SCB_ACTIVE;
1300 scb->scp = scp;
1301 scb->dma_type = MRAID_DMA_NONE;
1303 return scb;
1308 * megaraid_dealloc_scb - return the scb to the free pool
1309 * @adapter : controller's soft state
1310 * @scb : scb to be freed
1312 * Return the scb back to the free list of scbs. The caller must 'flush' the
1313 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1314 * NOTE NOTE: Make sure the scb is not on any list before calling this
1315 * routine.
1317 static inline void
1318 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1320 unsigned long flags;
1322 // put scb in the free pool
1323 scb->state = SCB_FREE;
1324 scb->scp = NULL;
1325 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1327 list_add(&scb->list, &adapter->kscb_pool);
1329 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1331 return;
1336 * megaraid_mbox_mksgl - make the scatter-gather list
1337 * @adapter : controller's soft state
1338 * @scb : scsi control block
1340 * Prepare the scatter-gather list.
1342 static int
1343 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1345 struct scatterlist *sgl;
1346 mbox_ccb_t *ccb;
1347 struct scsi_cmnd *scp;
1348 int sgcnt;
1349 int i;
1352 scp = scb->scp;
1353 ccb = (mbox_ccb_t *)scb->ccb;
1355 sgcnt = scsi_dma_map(scp);
1356 BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
1358 // no mapping required if no data to be transferred
1359 if (!sgcnt)
1360 return 0;
1362 scb->dma_type = MRAID_DMA_WSG;
1364 scsi_for_each_sg(scp, sgl, sgcnt, i) {
1365 ccb->sgl64[i].address = sg_dma_address(sgl);
1366 ccb->sgl64[i].length = sg_dma_len(sgl);
1369 // Return count of SG nodes
1370 return sgcnt;
1375 * mbox_post_cmd - issue a mailbox command
1376 * @adapter : controller's soft state
1377 * @scb : command to be issued
1379 * Post the command to the controller if mailbox is available.
1381 static int
1382 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1384 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1385 mbox64_t *mbox64;
1386 mbox_t *mbox;
1387 mbox_ccb_t *ccb;
1388 unsigned long flags;
1389 unsigned int i = 0;
1392 ccb = (mbox_ccb_t *)scb->ccb;
1393 mbox = raid_dev->mbox;
1394 mbox64 = raid_dev->mbox64;
1397 * Check for busy mailbox. If it is, return failure - the caller
1398 * should retry later.
1400 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1402 if (unlikely(mbox->busy)) {
1403 do {
1404 udelay(1);
1405 i++;
1406 rmb();
1407 } while(mbox->busy && (i < max_mbox_busy_wait));
1409 if (mbox->busy) {
1411 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1413 return -1;
1418 // Copy this command's mailbox data into "adapter's" mailbox
1419 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1420 mbox->cmdid = scb->sno;
1422 adapter->outstanding_cmds++;
1424 mbox->busy = 1; // Set busy
1425 mbox->poll = 0;
1426 mbox->ack = 0;
1427 wmb();
1429 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1431 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1433 return 0;
1438 * megaraid_queue_command - generic queue entry point for all LLDs
1439 * @scp : pointer to the scsi command to be executed
1440 * @done : callback routine to be called after the cmd has be completed
1442 * Queue entry point for mailbox based controllers.
1444 static int
1445 megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
1447 adapter_t *adapter;
1448 scb_t *scb;
1449 int if_busy;
1451 adapter = SCP2ADAPTER(scp);
1452 scp->scsi_done = done;
1453 scp->result = 0;
1456 * Allocate and build a SCB request
1457 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1458 * not allocate scb. We will return non-zero status in that case.
1459 * NOTE: scb can be null even though certain commands completed
1460 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1461 * return 0 in that case, and we would do the callback right away.
1463 if_busy = 0;
1464 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1465 if (!scb) { // command already completed
1466 done(scp);
1467 return 0;
1470 megaraid_mbox_runpendq(adapter, scb);
1471 return if_busy;
1474 static DEF_SCSI_QCMD(megaraid_queue_command)
1477 * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
1478 * @adapter : controller's soft state
1479 * @scp : mid-layer scsi command pointer
1480 * @busy : set if request could not be completed because of lack of
1481 * resources
1483 * Transform the mid-layer scsi command to megaraid firmware lingua.
1484 * Convert the command issued by mid-layer to format understood by megaraid
1485 * firmware. We also complete certain commands without sending them to firmware.
1487 static scb_t *
1488 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1490 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1491 int channel;
1492 int target;
1493 int islogical;
1494 mbox_ccb_t *ccb;
1495 mraid_passthru_t *pthru;
1496 mbox64_t *mbox64;
1497 mbox_t *mbox;
1498 scb_t *scb;
1499 char skip[] = "skipping";
1500 char scan[] = "scanning";
1501 char *ss;
1505 * Get the appropriate device map for the device this command is
1506 * intended for
1508 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1511 * Logical drive commands
1513 if (islogical) {
1514 switch (scp->cmnd[0]) {
1515 case TEST_UNIT_READY:
1517 * Do we support clustering and is the support enabled
1518 * If no, return success always
1520 if (!adapter->ha) {
1521 scp->result = (DID_OK << 16);
1522 return NULL;
1525 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1526 scp->result = (DID_ERROR << 16);
1527 *busy = 1;
1528 return NULL;
1531 scb->dma_direction = scp->sc_data_direction;
1532 scb->dev_channel = 0xFF;
1533 scb->dev_target = target;
1534 ccb = (mbox_ccb_t *)scb->ccb;
1537 * The command id will be provided by the command
1538 * issuance routine
1540 ccb->raw_mbox[0] = CLUSTER_CMD;
1541 ccb->raw_mbox[2] = RESERVATION_STATUS;
1542 ccb->raw_mbox[3] = target;
1544 return scb;
1546 case MODE_SENSE:
1548 struct scatterlist *sgl;
1549 caddr_t vaddr;
1551 sgl = scsi_sglist(scp);
1552 if (sg_page(sgl)) {
1553 vaddr = (caddr_t) sg_virt(&sgl[0]);
1555 memset(vaddr, 0, scp->cmnd[4]);
1557 else {
1558 con_log(CL_ANN, (KERN_WARNING
1559 "megaraid mailbox: invalid sg:%d\n",
1560 __LINE__));
1563 scp->result = (DID_OK << 16);
1564 return NULL;
1566 case INQUIRY:
1568 * Display the channel scan for logical drives
1569 * Do not display scan for a channel if already done.
1571 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1573 con_log(CL_ANN, (KERN_INFO
1574 "scsi[%d]: scanning scsi channel %d",
1575 adapter->host->host_no,
1576 SCP2CHANNEL(scp)));
1578 con_log(CL_ANN, (
1579 " [virtual] for logical drives\n"));
1581 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1584 if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
1585 scp->sense_buffer[0] = 0x70;
1586 scp->sense_buffer[2] = ILLEGAL_REQUEST;
1587 scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
1588 scp->result = CHECK_CONDITION << 1;
1589 return NULL;
1592 /* Fall through */
1594 case READ_CAPACITY:
1596 * Do not allow LUN > 0 for logical drives and
1597 * requests for more than 40 logical drives
1599 if (SCP2LUN(scp)) {
1600 scp->result = (DID_BAD_TARGET << 16);
1601 return NULL;
1603 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1604 scp->result = (DID_BAD_TARGET << 16);
1605 return NULL;
1609 /* Allocate a SCB and initialize passthru */
1610 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1611 scp->result = (DID_ERROR << 16);
1612 *busy = 1;
1613 return NULL;
1616 ccb = (mbox_ccb_t *)scb->ccb;
1617 scb->dev_channel = 0xFF;
1618 scb->dev_target = target;
1619 pthru = ccb->pthru;
1620 mbox = ccb->mbox;
1621 mbox64 = ccb->mbox64;
1623 pthru->timeout = 0;
1624 pthru->ars = 1;
1625 pthru->reqsenselen = 14;
1626 pthru->islogical = 1;
1627 pthru->logdrv = target;
1628 pthru->cdblen = scp->cmd_len;
1629 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1631 mbox->cmd = MBOXCMD_PASSTHRU64;
1632 scb->dma_direction = scp->sc_data_direction;
1634 pthru->dataxferlen = scsi_bufflen(scp);
1635 pthru->dataxferaddr = ccb->sgl_dma_h;
1636 pthru->numsge = megaraid_mbox_mksgl(adapter,
1637 scb);
1639 mbox->xferaddr = 0xFFFFFFFF;
1640 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1641 mbox64->xferaddr_hi = 0;
1643 return scb;
1645 case READ_6:
1646 case WRITE_6:
1647 case READ_10:
1648 case WRITE_10:
1649 case READ_12:
1650 case WRITE_12:
1653 * Allocate a SCB and initialize mailbox
1655 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1656 scp->result = (DID_ERROR << 16);
1657 *busy = 1;
1658 return NULL;
1660 ccb = (mbox_ccb_t *)scb->ccb;
1661 scb->dev_channel = 0xFF;
1662 scb->dev_target = target;
1663 mbox = ccb->mbox;
1664 mbox64 = ccb->mbox64;
1665 mbox->logdrv = target;
1668 * A little HACK: 2nd bit is zero for all scsi read
1669 * commands and is set for all scsi write commands
1671 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1672 MBOXCMD_LREAD64 ;
1675 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1677 if (scp->cmd_len == 6) {
1678 mbox->numsectors = (uint32_t)scp->cmnd[4];
1679 mbox->lba =
1680 ((uint32_t)scp->cmnd[1] << 16) |
1681 ((uint32_t)scp->cmnd[2] << 8) |
1682 (uint32_t)scp->cmnd[3];
1684 mbox->lba &= 0x1FFFFF;
1688 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1690 else if (scp->cmd_len == 10) {
1691 mbox->numsectors =
1692 (uint32_t)scp->cmnd[8] |
1693 ((uint32_t)scp->cmnd[7] << 8);
1694 mbox->lba =
1695 ((uint32_t)scp->cmnd[2] << 24) |
1696 ((uint32_t)scp->cmnd[3] << 16) |
1697 ((uint32_t)scp->cmnd[4] << 8) |
1698 (uint32_t)scp->cmnd[5];
1702 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1704 else if (scp->cmd_len == 12) {
1705 mbox->lba =
1706 ((uint32_t)scp->cmnd[2] << 24) |
1707 ((uint32_t)scp->cmnd[3] << 16) |
1708 ((uint32_t)scp->cmnd[4] << 8) |
1709 (uint32_t)scp->cmnd[5];
1711 mbox->numsectors =
1712 ((uint32_t)scp->cmnd[6] << 24) |
1713 ((uint32_t)scp->cmnd[7] << 16) |
1714 ((uint32_t)scp->cmnd[8] << 8) |
1715 (uint32_t)scp->cmnd[9];
1717 else {
1718 con_log(CL_ANN, (KERN_WARNING
1719 "megaraid: unsupported CDB length\n"));
1721 megaraid_dealloc_scb(adapter, scb);
1723 scp->result = (DID_ERROR << 16);
1724 return NULL;
1727 scb->dma_direction = scp->sc_data_direction;
1729 // Calculate Scatter-Gather info
1730 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1731 mbox->numsge = megaraid_mbox_mksgl(adapter,
1732 scb);
1733 mbox->xferaddr = 0xFFFFFFFF;
1734 mbox64->xferaddr_hi = 0;
1736 return scb;
1738 case RESERVE:
1739 case RELEASE:
1741 * Do we support clustering and is the support enabled
1743 if (!adapter->ha) {
1744 scp->result = (DID_BAD_TARGET << 16);
1745 return NULL;
1749 * Allocate a SCB and initialize mailbox
1751 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1752 scp->result = (DID_ERROR << 16);
1753 *busy = 1;
1754 return NULL;
1757 ccb = (mbox_ccb_t *)scb->ccb;
1758 scb->dev_channel = 0xFF;
1759 scb->dev_target = target;
1760 ccb->raw_mbox[0] = CLUSTER_CMD;
1761 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1762 RESERVE_LD : RELEASE_LD;
1764 ccb->raw_mbox[3] = target;
1765 scb->dma_direction = scp->sc_data_direction;
1767 return scb;
1769 default:
1770 scp->result = (DID_BAD_TARGET << 16);
1771 return NULL;
1774 else { // Passthru device commands
1776 // Do not allow access to target id > 15 or LUN > 7
1777 if (target > 15 || SCP2LUN(scp) > 7) {
1778 scp->result = (DID_BAD_TARGET << 16);
1779 return NULL;
1782 // if fast load option was set and scan for last device is
1783 // over, reset the fast_load flag so that during a possible
1784 // next scan, devices can be made available
1785 if (rdev->fast_load && (target == 15) &&
1786 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1788 con_log(CL_ANN, (KERN_INFO
1789 "megaraid[%d]: physical device scan re-enabled\n",
1790 adapter->host->host_no));
1791 rdev->fast_load = 0;
1795 * Display the channel scan for physical devices
1797 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1799 ss = rdev->fast_load ? skip : scan;
1801 con_log(CL_ANN, (KERN_INFO
1802 "scsi[%d]: %s scsi channel %d [Phy %d]",
1803 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1804 channel));
1806 con_log(CL_ANN, (
1807 " for non-raid devices\n"));
1809 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1812 // disable channel sweep if fast load option given
1813 if (rdev->fast_load) {
1814 scp->result = (DID_BAD_TARGET << 16);
1815 return NULL;
1818 // Allocate a SCB and initialize passthru
1819 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1820 scp->result = (DID_ERROR << 16);
1821 *busy = 1;
1822 return NULL;
1825 ccb = (mbox_ccb_t *)scb->ccb;
1826 scb->dev_channel = channel;
1827 scb->dev_target = target;
1828 scb->dma_direction = scp->sc_data_direction;
1829 mbox = ccb->mbox;
1830 mbox64 = ccb->mbox64;
1832 // Does this firmware support extended CDBs
1833 if (adapter->max_cdb_sz == 16) {
1834 mbox->cmd = MBOXCMD_EXTPTHRU;
1836 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1838 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1839 mbox64->xferaddr_hi = 0;
1840 mbox->xferaddr = 0xFFFFFFFF;
1842 else {
1843 mbox->cmd = MBOXCMD_PASSTHRU64;
1845 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1847 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1848 mbox64->xferaddr_hi = 0;
1849 mbox->xferaddr = 0xFFFFFFFF;
1851 return scb;
1854 // NOT REACHED
1859 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1860 * @adapter : controller's soft state
1861 * @scb_q : SCB to be queued in the pending list
1863 * Scan the pending list for commands which are not yet issued and try to
1864 * post to the controller. The SCB can be a null pointer, which would indicate
1865 * no SCB to be queue, just try to execute the ones in the pending list.
1867 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1868 * out from the head of the pending list. If it is successfully issued, the
1869 * next SCB is at the head now.
1871 static void
1872 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1874 scb_t *scb;
1875 unsigned long flags;
1877 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1879 if (scb_q) {
1880 scb_q->state = SCB_PENDQ;
1881 list_add_tail(&scb_q->list, &adapter->pend_list);
1884 // if the adapter in not in quiescent mode, post the commands to FW
1885 if (adapter->quiescent) {
1886 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1887 return;
1890 while (!list_empty(&adapter->pend_list)) {
1892 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1894 scb = list_entry(adapter->pend_list.next, scb_t, list);
1896 // remove the scb from the pending list and try to
1897 // issue. If we are unable to issue it, put back in
1898 // the pending list and return
1900 list_del_init(&scb->list);
1902 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1904 // if mailbox was busy, return SCB back to pending
1905 // list. Make sure to add at the head, since that's
1906 // where it would have been removed from
1908 scb->state = SCB_ISSUED;
1910 if (mbox_post_cmd(adapter, scb) != 0) {
1912 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1914 scb->state = SCB_PENDQ;
1916 list_add(&scb->list, &adapter->pend_list);
1918 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1919 flags);
1921 return;
1924 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1927 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1930 return;
1935 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1936 * @adapter : pointer to controller's soft state
1937 * @scb : scsi control block
1938 * @scp : scsi command from the mid-layer
1940 * Prepare a command for the scsi physical devices.
1942 static void
1943 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1944 struct scsi_cmnd *scp)
1946 mbox_ccb_t *ccb;
1947 mraid_passthru_t *pthru;
1948 uint8_t channel;
1949 uint8_t target;
1951 ccb = (mbox_ccb_t *)scb->ccb;
1952 pthru = ccb->pthru;
1953 channel = scb->dev_channel;
1954 target = scb->dev_target;
1956 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
1957 pthru->timeout = 4;
1958 pthru->ars = 1;
1959 pthru->islogical = 0;
1960 pthru->channel = 0;
1961 pthru->target = (channel << 4) | target;
1962 pthru->logdrv = SCP2LUN(scp);
1963 pthru->reqsenselen = 14;
1964 pthru->cdblen = scp->cmd_len;
1966 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1968 if (scsi_bufflen(scp)) {
1969 pthru->dataxferlen = scsi_bufflen(scp);
1970 pthru->dataxferaddr = ccb->sgl_dma_h;
1971 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
1973 else {
1974 pthru->dataxferaddr = 0;
1975 pthru->dataxferlen = 0;
1976 pthru->numsge = 0;
1978 return;
1983 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
1984 * @adapter : pointer to controller's soft state
1985 * @scb : scsi control block
1986 * @scp : scsi command from the mid-layer
1988 * Prepare a command for the scsi physical devices. This routine prepares
1989 * commands for devices which can take extended CDBs (>10 bytes).
1991 static void
1992 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
1993 struct scsi_cmnd *scp)
1995 mbox_ccb_t *ccb;
1996 mraid_epassthru_t *epthru;
1997 uint8_t channel;
1998 uint8_t target;
2000 ccb = (mbox_ccb_t *)scb->ccb;
2001 epthru = ccb->epthru;
2002 channel = scb->dev_channel;
2003 target = scb->dev_target;
2005 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2006 epthru->timeout = 4;
2007 epthru->ars = 1;
2008 epthru->islogical = 0;
2009 epthru->channel = 0;
2010 epthru->target = (channel << 4) | target;
2011 epthru->logdrv = SCP2LUN(scp);
2012 epthru->reqsenselen = 14;
2013 epthru->cdblen = scp->cmd_len;
2015 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2017 if (scsi_bufflen(scp)) {
2018 epthru->dataxferlen = scsi_bufflen(scp);
2019 epthru->dataxferaddr = ccb->sgl_dma_h;
2020 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2022 else {
2023 epthru->dataxferaddr = 0;
2024 epthru->dataxferlen = 0;
2025 epthru->numsge = 0;
2027 return;
2032 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2033 * @adapter : controller's soft state
2035 * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
2036 * completed command and put them on the completed list for later processing.
2038 * Returns: 1 if the interrupt is valid, 0 otherwise
2040 static int
2041 megaraid_ack_sequence(adapter_t *adapter)
2043 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2044 mbox_t *mbox;
2045 scb_t *scb;
2046 uint8_t nstatus;
2047 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2048 struct list_head clist;
2049 int handled;
2050 uint32_t dword;
2051 unsigned long flags;
2052 int i, j;
2055 mbox = raid_dev->mbox;
2057 // move the SCBs from the firmware completed array to our local list
2058 INIT_LIST_HEAD(&clist);
2060 // loop till F/W has more commands for us to complete
2061 handled = 0;
2062 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2063 do {
2065 * Check if a valid interrupt is pending. If found, force the
2066 * interrupt line low.
2068 dword = RDOUTDOOR(raid_dev);
2069 if (dword != 0x10001234) break;
2071 handled = 1;
2073 WROUTDOOR(raid_dev, 0x10001234);
2075 nstatus = 0;
2076 // wait for valid numstatus to post
2077 for (i = 0; i < 0xFFFFF; i++) {
2078 if (mbox->numstatus != 0xFF) {
2079 nstatus = mbox->numstatus;
2080 break;
2082 rmb();
2084 mbox->numstatus = 0xFF;
2086 adapter->outstanding_cmds -= nstatus;
2088 for (i = 0; i < nstatus; i++) {
2090 // wait for valid command index to post
2091 for (j = 0; j < 0xFFFFF; j++) {
2092 if (mbox->completed[i] != 0xFF) break;
2093 rmb();
2095 completed[i] = mbox->completed[i];
2096 mbox->completed[i] = 0xFF;
2098 if (completed[i] == 0xFF) {
2099 con_log(CL_ANN, (KERN_CRIT
2100 "megaraid: command posting timed out\n"));
2102 BUG();
2103 continue;
2106 // Get SCB associated with this command id
2107 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2108 // a cmm command
2109 scb = adapter->uscb_list + (completed[i] -
2110 MBOX_MAX_SCSI_CMDS);
2112 else {
2113 // an os command
2114 scb = adapter->kscb_list + completed[i];
2117 scb->status = mbox->status;
2118 list_add_tail(&scb->list, &clist);
2121 // Acknowledge interrupt
2122 WRINDOOR(raid_dev, 0x02);
2124 } while(1);
2126 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2129 // put the completed commands in the completed list. DPC would
2130 // complete these commands later
2131 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2133 list_splice(&clist, &adapter->completed_list);
2135 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2138 // schedule the DPC if there is some work for it
2139 if (handled)
2140 tasklet_schedule(&adapter->dpc_h);
2142 return handled;
2147 * megaraid_isr - isr for memory based mailbox based controllers
2148 * @irq : irq
2149 * @devp : pointer to our soft state
2151 * Interrupt service routine for memory-mapped mailbox controllers.
2153 static irqreturn_t
2154 megaraid_isr(int irq, void *devp)
2156 adapter_t *adapter = devp;
2157 int handled;
2159 handled = megaraid_ack_sequence(adapter);
2161 /* Loop through any pending requests */
2162 if (!adapter->quiescent) {
2163 megaraid_mbox_runpendq(adapter, NULL);
2166 return IRQ_RETVAL(handled);
2171 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2172 * @devp : pointer to HBA soft state
2174 * Pick up the commands from the completed list and send back to the owners.
2175 * This is a reentrant function and does not assume any locks are held while
2176 * it is being called.
2178 static void
2179 megaraid_mbox_dpc(unsigned long devp)
2181 adapter_t *adapter = (adapter_t *)devp;
2182 mraid_device_t *raid_dev;
2183 struct list_head clist;
2184 struct scatterlist *sgl;
2185 scb_t *scb;
2186 scb_t *tmp;
2187 struct scsi_cmnd *scp;
2188 mraid_passthru_t *pthru;
2189 mraid_epassthru_t *epthru;
2190 mbox_ccb_t *ccb;
2191 int islogical;
2192 int pdev_index;
2193 int pdev_state;
2194 mbox_t *mbox;
2195 unsigned long flags;
2196 uint8_t c;
2197 int status;
2198 uioc_t *kioc;
2201 if (!adapter) return;
2203 raid_dev = ADAP2RAIDDEV(adapter);
2205 // move the SCBs from the completed list to our local list
2206 INIT_LIST_HEAD(&clist);
2208 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2210 list_splice_init(&adapter->completed_list, &clist);
2212 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2215 list_for_each_entry_safe(scb, tmp, &clist, list) {
2217 status = scb->status;
2218 scp = scb->scp;
2219 ccb = (mbox_ccb_t *)scb->ccb;
2220 pthru = ccb->pthru;
2221 epthru = ccb->epthru;
2222 mbox = ccb->mbox;
2224 // Make sure f/w has completed a valid command
2225 if (scb->state != SCB_ISSUED) {
2226 con_log(CL_ANN, (KERN_CRIT
2227 "megaraid critical err: invalid command %d:%d:%p\n",
2228 scb->sno, scb->state, scp));
2229 BUG();
2230 continue; // Must never happen!
2233 // check for the management command and complete it right away
2234 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2235 scb->state = SCB_FREE;
2236 scb->status = status;
2238 // remove from local clist
2239 list_del_init(&scb->list);
2241 kioc = (uioc_t *)scb->gp;
2242 kioc->status = 0;
2244 megaraid_mbox_mm_done(adapter, scb);
2246 continue;
2249 // Was an abort issued for this command earlier
2250 if (scb->state & SCB_ABORT) {
2251 con_log(CL_ANN, (KERN_NOTICE
2252 "megaraid: aborted cmd [%x] completed\n",
2253 scb->sno));
2257 * If the inquiry came of a disk drive which is not part of
2258 * any RAID array, expose it to the kernel. For this to be
2259 * enabled, user must set the "megaraid_expose_unconf_disks"
2260 * flag to 1 by specifying it on module parameter list.
2261 * This would enable data migration off drives from other
2262 * configurations.
2264 islogical = MRAID_IS_LOGICAL(adapter, scp);
2265 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2266 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2268 sgl = scsi_sglist(scp);
2269 if (sg_page(sgl)) {
2270 c = *(unsigned char *) sg_virt(&sgl[0]);
2271 } else {
2272 con_log(CL_ANN, (KERN_WARNING
2273 "megaraid mailbox: invalid sg:%d\n",
2274 __LINE__));
2275 c = 0;
2278 if ((c & 0x1F ) == TYPE_DISK) {
2279 pdev_index = (scb->dev_channel * 16) +
2280 scb->dev_target;
2281 pdev_state =
2282 raid_dev->pdrv_state[pdev_index] & 0x0F;
2284 if (pdev_state == PDRV_ONLINE ||
2285 pdev_state == PDRV_FAILED ||
2286 pdev_state == PDRV_RBLD ||
2287 pdev_state == PDRV_HOTSPARE ||
2288 megaraid_expose_unconf_disks == 0) {
2290 status = 0xF0;
2295 // Convert MegaRAID status to Linux error code
2296 switch (status) {
2298 case 0x00:
2300 scp->result = (DID_OK << 16);
2301 break;
2303 case 0x02:
2305 /* set sense_buffer and result fields */
2306 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2307 mbox->cmd == MBOXCMD_PASSTHRU64) {
2309 memcpy(scp->sense_buffer, pthru->reqsensearea,
2310 14);
2312 scp->result = DRIVER_SENSE << 24 |
2313 DID_OK << 16 | CHECK_CONDITION << 1;
2315 else {
2316 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2318 memcpy(scp->sense_buffer,
2319 epthru->reqsensearea, 14);
2321 scp->result = DRIVER_SENSE << 24 |
2322 DID_OK << 16 |
2323 CHECK_CONDITION << 1;
2324 } else {
2325 scp->sense_buffer[0] = 0x70;
2326 scp->sense_buffer[2] = ABORTED_COMMAND;
2327 scp->result = CHECK_CONDITION << 1;
2330 break;
2332 case 0x08:
2334 scp->result = DID_BUS_BUSY << 16 | status;
2335 break;
2337 default:
2340 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2341 * failed
2343 if (scp->cmnd[0] == TEST_UNIT_READY) {
2344 scp->result = DID_ERROR << 16 |
2345 RESERVATION_CONFLICT << 1;
2347 else
2349 * Error code returned is 1 if Reserve or Release
2350 * failed or the input parameter is invalid
2352 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2353 scp->cmnd[0] == RELEASE)) {
2355 scp->result = DID_ERROR << 16 |
2356 RESERVATION_CONFLICT << 1;
2358 else {
2359 scp->result = DID_BAD_TARGET << 16 | status;
2363 // print a debug message for all failed commands
2364 if (status) {
2365 megaraid_mbox_display_scb(adapter, scb);
2368 scsi_dma_unmap(scp);
2370 // remove from local clist
2371 list_del_init(&scb->list);
2373 // put back in free list
2374 megaraid_dealloc_scb(adapter, scb);
2376 // send the scsi packet back to kernel
2377 scp->scsi_done(scp);
2380 return;
2385 * megaraid_abort_handler - abort the scsi command
2386 * @scp : command to be aborted
2388 * Abort a previous SCSI request. Only commands on the pending list can be
2389 * aborted. All the commands issued to the F/W must complete.
2391 static int
2392 megaraid_abort_handler(struct scsi_cmnd *scp)
2394 adapter_t *adapter;
2395 mraid_device_t *raid_dev;
2396 scb_t *scb;
2397 scb_t *tmp;
2398 int found;
2399 unsigned long flags;
2400 int i;
2403 adapter = SCP2ADAPTER(scp);
2404 raid_dev = ADAP2RAIDDEV(adapter);
2406 con_log(CL_ANN, (KERN_WARNING
2407 "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
2408 scp->cmnd[0], SCP2CHANNEL(scp),
2409 SCP2TARGET(scp), SCP2LUN(scp)));
2411 // If FW has stopped responding, simply return failure
2412 if (raid_dev->hw_error) {
2413 con_log(CL_ANN, (KERN_NOTICE
2414 "megaraid: hw error, not aborting\n"));
2415 return FAILED;
2418 // There might a race here, where the command was completed by the
2419 // firmware and now it is on the completed list. Before we could
2420 // complete the command to the kernel in dpc, the abort came.
2421 // Find out if this is the case to avoid the race.
2422 scb = NULL;
2423 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2424 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2426 if (scb->scp == scp) { // Found command
2428 list_del_init(&scb->list); // from completed list
2430 con_log(CL_ANN, (KERN_WARNING
2431 "megaraid: %d[%d:%d], abort from completed list\n",
2432 scb->sno, scb->dev_channel, scb->dev_target));
2434 scp->result = (DID_ABORT << 16);
2435 scp->scsi_done(scp);
2437 megaraid_dealloc_scb(adapter, scb);
2439 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2440 flags);
2442 return SUCCESS;
2445 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2448 // Find out if this command is still on the pending list. If it is and
2449 // was never issued, abort and return success. If the command is owned
2450 // by the firmware, we must wait for it to complete by the FW.
2451 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2452 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2454 if (scb->scp == scp) { // Found command
2456 list_del_init(&scb->list); // from pending list
2458 ASSERT(!(scb->state & SCB_ISSUED));
2460 con_log(CL_ANN, (KERN_WARNING
2461 "megaraid abort: [%d:%d], driver owner\n",
2462 scb->dev_channel, scb->dev_target));
2464 scp->result = (DID_ABORT << 16);
2465 scp->scsi_done(scp);
2467 megaraid_dealloc_scb(adapter, scb);
2469 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2470 flags);
2472 return SUCCESS;
2475 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2478 // Check do we even own this command, in which case this would be
2479 // owned by the firmware. The only way to locate the FW scb is to
2480 // traverse through the list of all SCB, since driver does not
2481 // maintain these SCBs on any list
2482 found = 0;
2483 spin_lock_irq(&adapter->lock);
2484 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2485 scb = adapter->kscb_list + i;
2487 if (scb->scp == scp) {
2489 found = 1;
2491 if (!(scb->state & SCB_ISSUED)) {
2492 con_log(CL_ANN, (KERN_WARNING
2493 "megaraid abort: %d[%d:%d], invalid state\n",
2494 scb->sno, scb->dev_channel, scb->dev_target));
2495 BUG();
2497 else {
2498 con_log(CL_ANN, (KERN_WARNING
2499 "megaraid abort: %d[%d:%d], fw owner\n",
2500 scb->sno, scb->dev_channel, scb->dev_target));
2504 spin_unlock_irq(&adapter->lock);
2506 if (!found) {
2507 con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
2509 // FIXME: Should there be a callback for this command?
2510 return SUCCESS;
2513 // We cannot actually abort a command owned by firmware, return
2514 // failure and wait for reset. In host reset handler, we will find out
2515 // if the HBA is still live
2516 return FAILED;
2520 * megaraid_reset_handler - device reset handler for mailbox based driver
2521 * @scp : reference command
2523 * Reset handler for the mailbox based controller. First try to find out if
2524 * the FW is still live, in which case the outstanding commands counter mut go
2525 * down to 0. If that happens, also issue the reservation reset command to
2526 * relinquish (possible) reservations on the logical drives connected to this
2527 * host.
2529 static int
2530 megaraid_reset_handler(struct scsi_cmnd *scp)
2532 adapter_t *adapter;
2533 scb_t *scb;
2534 scb_t *tmp;
2535 mraid_device_t *raid_dev;
2536 unsigned long flags;
2537 uint8_t raw_mbox[sizeof(mbox_t)];
2538 int rval;
2539 int recovery_window;
2540 int i;
2541 uioc_t *kioc;
2543 adapter = SCP2ADAPTER(scp);
2544 raid_dev = ADAP2RAIDDEV(adapter);
2546 // return failure if adapter is not responding
2547 if (raid_dev->hw_error) {
2548 con_log(CL_ANN, (KERN_NOTICE
2549 "megaraid: hw error, cannot reset\n"));
2550 return FAILED;
2553 // Under exceptional conditions, FW can take up to 3 minutes to
2554 // complete command processing. Wait for additional 2 minutes for the
2555 // pending commands counter to go down to 0. If it doesn't, let the
2556 // controller be marked offline
2557 // Also, reset all the commands currently owned by the driver
2558 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2559 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2560 list_del_init(&scb->list); // from pending list
2562 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2563 con_log(CL_ANN, (KERN_WARNING
2564 "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2565 scb->sno, scb->dev_channel, scb->dev_target));
2567 scb->status = -1;
2569 kioc = (uioc_t *)scb->gp;
2570 kioc->status = -EFAULT;
2572 megaraid_mbox_mm_done(adapter, scb);
2573 } else {
2574 if (scb->scp == scp) { // Found command
2575 con_log(CL_ANN, (KERN_WARNING
2576 "megaraid: %d[%d:%d], reset from pending list\n",
2577 scb->sno, scb->dev_channel, scb->dev_target));
2578 } else {
2579 con_log(CL_ANN, (KERN_WARNING
2580 "megaraid: IO packet with %d[%d:%d] being reset\n",
2581 scb->sno, scb->dev_channel, scb->dev_target));
2584 scb->scp->result = (DID_RESET << 16);
2585 scb->scp->scsi_done(scb->scp);
2587 megaraid_dealloc_scb(adapter, scb);
2590 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2592 if (adapter->outstanding_cmds) {
2593 con_log(CL_ANN, (KERN_NOTICE
2594 "megaraid: %d outstanding commands. Max wait %d sec\n",
2595 adapter->outstanding_cmds,
2596 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2599 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2601 for (i = 0; i < recovery_window; i++) {
2603 megaraid_ack_sequence(adapter);
2605 // print a message once every 5 seconds only
2606 if (!(i % 5)) {
2607 con_log(CL_ANN, (
2608 "megaraid mbox: Wait for %d commands to complete:%d\n",
2609 adapter->outstanding_cmds,
2610 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2613 // bailout if no recovery happened in reset time
2614 if (adapter->outstanding_cmds == 0) {
2615 break;
2618 msleep(1000);
2621 spin_lock(&adapter->lock);
2623 // If still outstanding commands, bail out
2624 if (adapter->outstanding_cmds) {
2625 con_log(CL_ANN, (KERN_WARNING
2626 "megaraid mbox: critical hardware error!\n"));
2628 raid_dev->hw_error = 1;
2630 rval = FAILED;
2631 goto out;
2633 else {
2634 con_log(CL_ANN, (KERN_NOTICE
2635 "megaraid mbox: reset sequence completed successfully\n"));
2639 // If the controller supports clustering, reset reservations
2640 if (!adapter->ha) {
2641 rval = SUCCESS;
2642 goto out;
2645 // clear reservations if any
2646 raw_mbox[0] = CLUSTER_CMD;
2647 raw_mbox[2] = RESET_RESERVATIONS;
2649 rval = SUCCESS;
2650 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2651 con_log(CL_ANN,
2652 (KERN_INFO "megaraid: reservation reset\n"));
2654 else {
2655 rval = FAILED;
2656 con_log(CL_ANN, (KERN_WARNING
2657 "megaraid: reservation reset failed\n"));
2660 out:
2661 spin_unlock(&adapter->lock);
2662 return rval;
2666 * START: internal commands library
2668 * This section of the driver has the common routine used by the driver and
2669 * also has all the FW routines
2673 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2674 * @adapter : controller's soft state
2675 * @raw_mbox : the mailbox
2677 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2678 * controllers.
2680 static int
2681 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2683 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2684 mbox_t *mbox;
2685 uint8_t status;
2686 int i;
2688 mbox = raid_dev->mbox;
2691 * Wait until mailbox is free
2693 if (megaraid_busywait_mbox(raid_dev) != 0)
2694 goto blocked_mailbox;
2697 * Copy mailbox data into host structure
2699 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2700 mbox->cmdid = 0xFE;
2701 mbox->busy = 1;
2702 mbox->poll = 0;
2703 mbox->ack = 0;
2704 mbox->numstatus = 0xFF;
2705 mbox->status = 0xFF;
2707 wmb();
2708 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2710 // wait for maximum 1 second for status to post. If the status is not
2711 // available within 1 second, assume FW is initializing and wait
2712 // for an extended amount of time
2713 if (mbox->numstatus == 0xFF) { // status not yet available
2714 udelay(25);
2716 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2717 rmb();
2718 msleep(1);
2722 if (i == 1000) {
2723 con_log(CL_ANN, (KERN_NOTICE
2724 "megaraid mailbox: wait for FW to boot "));
2726 for (i = 0; (mbox->numstatus == 0xFF) &&
2727 (i < MBOX_RESET_WAIT); i++) {
2728 rmb();
2729 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2730 MBOX_RESET_WAIT - i));
2731 msleep(1000);
2734 if (i == MBOX_RESET_WAIT) {
2736 con_log(CL_ANN, (
2737 "\nmegaraid mailbox: status not available\n"));
2739 return -1;
2741 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2745 // wait for maximum 1 second for poll semaphore
2746 if (mbox->poll != 0x77) {
2747 udelay(25);
2749 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2750 rmb();
2751 msleep(1);
2754 if (i == 1000) {
2755 con_log(CL_ANN, (KERN_WARNING
2756 "megaraid mailbox: could not get poll semaphore\n"));
2757 return -1;
2761 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2762 wmb();
2764 // wait for maximum 1 second for acknowledgement
2765 if (RDINDOOR(raid_dev) & 0x2) {
2766 udelay(25);
2768 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2769 rmb();
2770 msleep(1);
2773 if (i == 1000) {
2774 con_log(CL_ANN, (KERN_WARNING
2775 "megaraid mailbox: could not acknowledge\n"));
2776 return -1;
2779 mbox->poll = 0;
2780 mbox->ack = 0x77;
2782 status = mbox->status;
2784 // invalidate the completed command id array. After command
2785 // completion, firmware would write the valid id.
2786 mbox->numstatus = 0xFF;
2787 mbox->status = 0xFF;
2788 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2789 mbox->completed[i] = 0xFF;
2792 return status;
2794 blocked_mailbox:
2796 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2797 return -1;
2802 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2803 * @adapter : controller's soft state
2804 * @raw_mbox : the mailbox
2806 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2807 * controllers. This is a faster version of the synchronous command and
2808 * therefore can be called in interrupt-context as well.
2810 static int
2811 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2813 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2814 mbox_t *mbox;
2815 long i;
2818 mbox = raid_dev->mbox;
2820 // return immediately if the mailbox is busy
2821 if (mbox->busy) return -1;
2823 // Copy mailbox data into host structure
2824 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2825 mbox->cmdid = 0xFE;
2826 mbox->busy = 1;
2827 mbox->poll = 0;
2828 mbox->ack = 0;
2829 mbox->numstatus = 0xFF;
2830 mbox->status = 0xFF;
2832 wmb();
2833 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2835 for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2836 if (mbox->numstatus != 0xFF) break;
2837 rmb();
2838 udelay(MBOX_SYNC_DELAY_200);
2841 if (i == MBOX_SYNC_WAIT_CNT) {
2842 // We may need to re-calibrate the counter
2843 con_log(CL_ANN, (KERN_CRIT
2844 "megaraid: fast sync command timed out\n"));
2847 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2848 wmb();
2850 return mbox->status;
2855 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2856 * @raid_dev : RAID device (HBA) soft state
2858 * Wait until the controller's mailbox is available to accept more commands.
2859 * Wait for at most 1 second.
2861 static int
2862 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2864 mbox_t *mbox = raid_dev->mbox;
2865 int i = 0;
2867 if (mbox->busy) {
2868 udelay(25);
2869 for (i = 0; mbox->busy && i < 1000; i++)
2870 msleep(1);
2873 if (i < 1000) return 0;
2874 else return -1;
2879 * megaraid_mbox_product_info - some static information about the controller
2880 * @adapter : our soft state
2882 * Issue commands to the controller to grab some parameters required by our
2883 * caller.
2885 static int
2886 megaraid_mbox_product_info(adapter_t *adapter)
2888 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2889 mbox_t *mbox;
2890 uint8_t raw_mbox[sizeof(mbox_t)];
2891 mraid_pinfo_t *pinfo;
2892 dma_addr_t pinfo_dma_h;
2893 mraid_inquiry3_t *mraid_inq3;
2894 int i;
2897 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2898 mbox = (mbox_t *)raw_mbox;
2901 * Issue an ENQUIRY3 command to find out certain adapter parameters,
2902 * e.g., max channels, max commands etc.
2904 pinfo = dma_zalloc_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2905 &pinfo_dma_h, GFP_KERNEL);
2906 if (pinfo == NULL) {
2907 con_log(CL_ANN, (KERN_WARNING
2908 "megaraid: out of memory, %s %d\n", __func__,
2909 __LINE__));
2911 return -1;
2914 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
2915 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
2917 raw_mbox[0] = FC_NEW_CONFIG;
2918 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
2919 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
2921 // Issue the command
2922 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2924 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
2926 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2927 pinfo, pinfo_dma_h);
2929 return -1;
2933 * Collect information about state of each physical drive
2934 * attached to the controller. We will expose all the disks
2935 * which are not part of RAID
2937 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
2938 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
2939 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
2943 * Get product info for information like number of channels,
2944 * maximum commands supported.
2946 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
2947 mbox->xferaddr = (uint32_t)pinfo_dma_h;
2949 raw_mbox[0] = FC_NEW_CONFIG;
2950 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
2952 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
2954 con_log(CL_ANN, (KERN_WARNING
2955 "megaraid: product info failed\n"));
2957 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t),
2958 pinfo, pinfo_dma_h);
2960 return -1;
2964 * Setup some parameters for host, as required by our caller
2966 adapter->max_channel = pinfo->nchannels;
2969 * we will export all the logical drives on a single channel.
2970 * Add 1 since inquires do not come for inititor ID
2972 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
2973 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
2976 * These are the maximum outstanding commands for the scsi-layer
2978 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
2980 memset(adapter->fw_version, 0, VERSION_SIZE);
2981 memset(adapter->bios_version, 0, VERSION_SIZE);
2983 memcpy(adapter->fw_version, pinfo->fw_version, 4);
2984 adapter->fw_version[4] = 0;
2986 memcpy(adapter->bios_version, pinfo->bios_version, 4);
2987 adapter->bios_version[4] = 0;
2989 con_log(CL_ANN, (KERN_NOTICE
2990 "megaraid: fw version:[%s] bios version:[%s]\n",
2991 adapter->fw_version, adapter->bios_version));
2993 dma_free_coherent(&adapter->pdev->dev, sizeof(mraid_pinfo_t), pinfo,
2994 pinfo_dma_h);
2996 return 0;
3002 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3003 * @adapter : soft state for the controller
3005 * This routine check whether the controller in question supports extended
3006 * ( > 10 bytes ) CDBs.
3008 static int
3009 megaraid_mbox_extended_cdb(adapter_t *adapter)
3011 mbox_t *mbox;
3012 uint8_t raw_mbox[sizeof(mbox_t)];
3013 int rval;
3015 mbox = (mbox_t *)raw_mbox;
3017 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3018 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3020 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3022 raw_mbox[0] = MAIN_MISC_OPCODE;
3023 raw_mbox[2] = SUPPORT_EXT_CDB;
3026 * Issue the command
3028 rval = 0;
3029 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3030 rval = -1;
3033 return rval;
3038 * megaraid_mbox_support_ha - Do we support clustering
3039 * @adapter : soft state for the controller
3040 * @init_id : ID of the initiator
3042 * Determine if the firmware supports clustering and the ID of the initiator.
3044 static int
3045 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3047 mbox_t *mbox;
3048 uint8_t raw_mbox[sizeof(mbox_t)];
3049 int rval;
3052 mbox = (mbox_t *)raw_mbox;
3054 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3056 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3058 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3060 raw_mbox[0] = GET_TARGET_ID;
3062 // Issue the command
3063 *init_id = 7;
3064 rval = -1;
3065 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3067 *init_id = *(uint8_t *)adapter->ibuf;
3069 con_log(CL_ANN, (KERN_INFO
3070 "megaraid: cluster firmware, initiator ID: %d\n",
3071 *init_id));
3073 rval = 0;
3076 return rval;
3081 * megaraid_mbox_support_random_del - Do we support random deletion
3082 * @adapter : soft state for the controller
3084 * Determine if the firmware supports random deletion.
3085 * Return: 1 is operation supported, 0 otherwise
3087 static int
3088 megaraid_mbox_support_random_del(adapter_t *adapter)
3090 uint8_t raw_mbox[sizeof(mbox_t)];
3091 int rval;
3094 * Newer firmware on Dell CERC expect a different
3095 * random deletion handling, so disable it.
3097 if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
3098 adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
3099 adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
3100 adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
3101 (adapter->fw_version[0] > '6' ||
3102 (adapter->fw_version[0] == '6' &&
3103 adapter->fw_version[2] > '6') ||
3104 (adapter->fw_version[0] == '6'
3105 && adapter->fw_version[2] == '6'
3106 && adapter->fw_version[3] > '1'))) {
3107 con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
3108 return 0;
3111 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3113 raw_mbox[0] = FC_DEL_LOGDRV;
3114 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3116 // Issue the command
3117 rval = 0;
3118 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3120 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3122 rval = 1;
3125 return rval;
3130 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3131 * @adapter : soft state for the controller
3133 * Find out the maximum number of scatter-gather elements supported by the
3134 * firmware.
3136 static int
3137 megaraid_mbox_get_max_sg(adapter_t *adapter)
3139 mbox_t *mbox;
3140 uint8_t raw_mbox[sizeof(mbox_t)];
3141 int nsg;
3144 mbox = (mbox_t *)raw_mbox;
3146 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3148 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3150 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3152 raw_mbox[0] = MAIN_MISC_OPCODE;
3153 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3155 // Issue the command
3156 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3157 nsg = *(uint8_t *)adapter->ibuf;
3159 else {
3160 nsg = MBOX_DEFAULT_SG_SIZE;
3163 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3165 return nsg;
3170 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3171 * @adapter : soft state for the controller
3173 * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
3174 * can be exported as regular SCSI channels.
3176 static void
3177 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3179 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3180 mbox_t *mbox;
3181 uint8_t raw_mbox[sizeof(mbox_t)];
3184 mbox = (mbox_t *)raw_mbox;
3186 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3188 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3190 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3192 raw_mbox[0] = CHNL_CLASS;
3193 raw_mbox[2] = GET_CHNL_CLASS;
3195 // Issue the command. If the command fails, all channels are RAID
3196 // channels
3197 raid_dev->channel_class = 0xFF;
3198 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3199 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3202 return;
3207 * megaraid_mbox_flush_cache - flush adapter and disks cache
3208 * @adapter : soft state for the controller
3210 * Flush adapter cache followed by disks cache.
3212 static void
3213 megaraid_mbox_flush_cache(adapter_t *adapter)
3215 uint8_t raw_mbox[sizeof(mbox_t)];
3217 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3219 raw_mbox[0] = FLUSH_ADAPTER;
3221 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3222 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3225 raw_mbox[0] = FLUSH_SYSTEM;
3227 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3228 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3231 return;
3236 * megaraid_mbox_fire_sync_cmd - fire the sync cmd
3237 * @adapter : soft state for the controller
3239 * Clears the pending cmds in FW and reinits its RAID structs.
3241 static int
3242 megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
3244 mbox_t *mbox;
3245 uint8_t raw_mbox[sizeof(mbox_t)];
3246 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3247 int status = 0;
3248 int i;
3249 uint32_t dword;
3251 mbox = (mbox_t *)raw_mbox;
3253 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3255 raw_mbox[0] = 0xFF;
3257 mbox = raid_dev->mbox;
3259 /* Wait until mailbox is free */
3260 if (megaraid_busywait_mbox(raid_dev) != 0) {
3261 status = 1;
3262 goto blocked_mailbox;
3265 /* Copy mailbox data into host structure */
3266 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
3267 mbox->cmdid = 0xFE;
3268 mbox->busy = 1;
3269 mbox->poll = 0;
3270 mbox->ack = 0;
3271 mbox->numstatus = 0;
3272 mbox->status = 0;
3274 wmb();
3275 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3277 /* Wait for maximum 1 min for status to post.
3278 * If the Firmware SUPPORTS the ABOVE COMMAND,
3279 * mbox->cmd will be set to 0
3280 * else
3281 * the firmware will reject the command with
3282 * mbox->numstatus set to 1
3285 i = 0;
3286 status = 0;
3287 while (!mbox->numstatus && mbox->cmd == 0xFF) {
3288 rmb();
3289 msleep(1);
3290 i++;
3291 if (i > 1000 * 60) {
3292 status = 1;
3293 break;
3296 if (mbox->numstatus == 1)
3297 status = 1; /*cmd not supported*/
3299 /* Check for interrupt line */
3300 dword = RDOUTDOOR(raid_dev);
3301 WROUTDOOR(raid_dev, dword);
3302 WRINDOOR(raid_dev,2);
3304 return status;
3306 blocked_mailbox:
3307 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
3308 return status;
3312 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3313 * @adapter : controller's soft state
3314 * @scb : SCB to be displayed
3315 * @level : debug level for console print
3317 * Diplay information about the given SCB iff the current debug level is
3318 * verbose.
3320 static void
3321 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3323 mbox_ccb_t *ccb;
3324 struct scsi_cmnd *scp;
3325 mbox_t *mbox;
3326 int level;
3327 int i;
3330 ccb = (mbox_ccb_t *)scb->ccb;
3331 scp = scb->scp;
3332 mbox = ccb->mbox;
3334 level = CL_DLEVEL3;
3336 con_log(level, (KERN_NOTICE
3337 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3338 mbox->cmd, scb->sno));
3340 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3341 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3342 mbox->numsge));
3344 if (!scp) return;
3346 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3348 for (i = 0; i < scp->cmd_len; i++) {
3349 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3352 con_log(level, ("\n"));
3354 return;
3359 * megaraid_mbox_setup_device_map - manage device ids
3360 * @adapter : Driver's soft state
3362 * Manage the device ids to have an appropriate mapping between the kernel
3363 * scsi addresses and megaraid scsi and logical drive addresses. We export
3364 * scsi devices on their actual addresses, whereas the logical drives are
3365 * exported on a virtual scsi channel.
3367 static void
3368 megaraid_mbox_setup_device_map(adapter_t *adapter)
3370 uint8_t c;
3371 uint8_t t;
3374 * First fill the values on the logical drive channel
3376 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3377 adapter->device_ids[adapter->max_channel][t] =
3378 (t < adapter->init_id) ? t : t - 1;
3380 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3383 * Fill the values on the physical devices channels
3385 for (c = 0; c < adapter->max_channel; c++)
3386 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3387 adapter->device_ids[c][t] = (c << 8) | t;
3392 * END: internal commands library
3396 * START: Interface for the common management module
3398 * This is the module, which interfaces with the common management module to
3399 * provide support for ioctl and sysfs
3403 * megaraid_cmm_register - register with the management module
3404 * @adapter : HBA soft state
3406 * Register with the management module, which allows applications to issue
3407 * ioctl calls to the drivers. This interface is used by the management module
3408 * to setup sysfs support as well.
3410 static int
3411 megaraid_cmm_register(adapter_t *adapter)
3413 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3414 mraid_mmadp_t adp;
3415 scb_t *scb;
3416 mbox_ccb_t *ccb;
3417 int rval;
3418 int i;
3420 // Allocate memory for the base list of scb for management module.
3421 adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
3423 if (adapter->uscb_list == NULL) {
3424 con_log(CL_ANN, (KERN_WARNING
3425 "megaraid: out of memory, %s %d\n", __func__,
3426 __LINE__));
3427 return -1;
3431 // Initialize the synchronization parameters for resources for
3432 // commands for management module
3433 INIT_LIST_HEAD(&adapter->uscb_pool);
3435 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3439 // link all the packets. Note, CCB for commands, coming from the
3440 // commom management module, mailbox physical address are already
3441 // setup by it. We just need placeholder for that in our local command
3442 // control blocks
3443 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3445 scb = adapter->uscb_list + i;
3446 ccb = raid_dev->uccb_list + i;
3448 scb->ccb = (caddr_t)ccb;
3449 ccb->mbox64 = raid_dev->umbox64 + i;
3450 ccb->mbox = &ccb->mbox64->mbox32;
3451 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3453 scb->gp = 0;
3455 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3456 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3457 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3459 scb->scp = NULL;
3460 scb->state = SCB_FREE;
3461 scb->dma_direction = DMA_NONE;
3462 scb->dma_type = MRAID_DMA_NONE;
3463 scb->dev_channel = -1;
3464 scb->dev_target = -1;
3466 // put scb in the free pool
3467 list_add_tail(&scb->list, &adapter->uscb_pool);
3470 adp.unique_id = adapter->unique_id;
3471 adp.drvr_type = DRVRTYPE_MBOX;
3472 adp.drvr_data = (unsigned long)adapter;
3473 adp.pdev = adapter->pdev;
3474 adp.issue_uioc = megaraid_mbox_mm_handler;
3475 adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3476 adp.max_kioc = MBOX_MAX_USER_CMDS;
3478 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3480 con_log(CL_ANN, (KERN_WARNING
3481 "megaraid mbox: did not register with CMM\n"));
3483 kfree(adapter->uscb_list);
3486 return rval;
3491 * megaraid_cmm_unregister - un-register with the management module
3492 * @adapter : HBA soft state
3494 * Un-register with the management module.
3495 * FIXME: mgmt module must return failure for unregister if it has pending
3496 * commands in LLD.
3498 static int
3499 megaraid_cmm_unregister(adapter_t *adapter)
3501 kfree(adapter->uscb_list);
3502 mraid_mm_unregister_adp(adapter->unique_id);
3503 return 0;
3508 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3509 * @drvr_data : LLD specific data
3510 * @kioc : CMM interface packet
3511 * @action : command action
3513 * This routine is invoked whenever the Common Management Module (CMM) has a
3514 * command for us. The 'action' parameter specifies if this is a new command
3515 * or otherwise.
3517 static int
3518 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3520 adapter_t *adapter;
3522 if (action != IOCTL_ISSUE) {
3523 con_log(CL_ANN, (KERN_WARNING
3524 "megaraid: unsupported management action:%#2x\n",
3525 action));
3526 return (-ENOTSUPP);
3529 adapter = (adapter_t *)drvr_data;
3531 // make sure this adapter is not being detached right now.
3532 if (atomic_read(&adapter->being_detached)) {
3533 con_log(CL_ANN, (KERN_WARNING
3534 "megaraid: reject management request, detaching\n"));
3535 return (-ENODEV);
3538 switch (kioc->opcode) {
3540 case GET_ADAP_INFO:
3542 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3543 (unsigned long)kioc->buf_vaddr);
3545 kioc->done(kioc);
3547 return kioc->status;
3549 case MBOX_CMD:
3551 return megaraid_mbox_mm_command(adapter, kioc);
3553 default:
3554 kioc->status = (-EINVAL);
3555 kioc->done(kioc);
3556 return (-EINVAL);
3559 return 0; // not reached
3563 * megaraid_mbox_mm_command - issues commands routed through CMM
3564 * @adapter : HBA soft state
3565 * @kioc : management command packet
3567 * Issues commands, which are routed through the management module.
3569 static int
3570 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3572 struct list_head *head = &adapter->uscb_pool;
3573 mbox64_t *mbox64;
3574 uint8_t *raw_mbox;
3575 scb_t *scb;
3576 mbox_ccb_t *ccb;
3577 unsigned long flags;
3579 // detach one scb from free pool
3580 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3582 if (list_empty(head)) { // should never happen because of CMM
3584 con_log(CL_ANN, (KERN_WARNING
3585 "megaraid mbox: bug in cmm handler, lost resources\n"));
3587 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3589 return (-EINVAL);
3592 scb = list_entry(head->next, scb_t, list);
3593 list_del_init(&scb->list);
3595 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3597 scb->state = SCB_ACTIVE;
3598 scb->dma_type = MRAID_DMA_NONE;
3599 scb->dma_direction = DMA_NONE;
3601 ccb = (mbox_ccb_t *)scb->ccb;
3602 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3603 raw_mbox = (uint8_t *)&mbox64->mbox32;
3605 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3607 scb->gp = (unsigned long)kioc;
3610 * If it is a logdrv random delete operation, we have to wait till
3611 * there are no outstanding cmds at the fw and then issue it directly
3613 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3615 if (wait_till_fw_empty(adapter)) {
3616 con_log(CL_ANN, (KERN_NOTICE
3617 "megaraid mbox: LD delete, timed out\n"));
3619 kioc->status = -ETIME;
3621 scb->status = -1;
3623 megaraid_mbox_mm_done(adapter, scb);
3625 return (-ETIME);
3628 INIT_LIST_HEAD(&scb->list);
3630 scb->state = SCB_ISSUED;
3631 if (mbox_post_cmd(adapter, scb) != 0) {
3633 con_log(CL_ANN, (KERN_NOTICE
3634 "megaraid mbox: LD delete, mailbox busy\n"));
3636 kioc->status = -EBUSY;
3638 scb->status = -1;
3640 megaraid_mbox_mm_done(adapter, scb);
3642 return (-EBUSY);
3645 return 0;
3648 // put the command on the pending list and execute
3649 megaraid_mbox_runpendq(adapter, scb);
3651 return 0;
3655 static int
3656 wait_till_fw_empty(adapter_t *adapter)
3658 unsigned long flags = 0;
3659 int i;
3663 * Set the quiescent flag to stop issuing cmds to FW.
3665 spin_lock_irqsave(&adapter->lock, flags);
3666 adapter->quiescent++;
3667 spin_unlock_irqrestore(&adapter->lock, flags);
3670 * Wait till there are no more cmds outstanding at FW. Try for at most
3671 * 60 seconds
3673 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3674 con_log(CL_DLEVEL1, (KERN_INFO
3675 "megaraid: FW has %d pending commands\n",
3676 adapter->outstanding_cmds));
3678 msleep(1000);
3681 return adapter->outstanding_cmds;
3686 * megaraid_mbox_mm_done - callback for CMM commands
3687 * @adapter : HBA soft state
3688 * @scb : completed command
3690 * Callback routine for internal commands originated from the management
3691 * module.
3693 static void
3694 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3696 uioc_t *kioc;
3697 mbox64_t *mbox64;
3698 uint8_t *raw_mbox;
3699 unsigned long flags;
3701 kioc = (uioc_t *)scb->gp;
3702 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3703 mbox64->mbox32.status = scb->status;
3704 raw_mbox = (uint8_t *)&mbox64->mbox32;
3707 // put scb in the free pool
3708 scb->state = SCB_FREE;
3709 scb->scp = NULL;
3711 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3713 list_add(&scb->list, &adapter->uscb_pool);
3715 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3717 // if a delete logical drive operation succeeded, restart the
3718 // controller
3719 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3721 adapter->quiescent--;
3723 megaraid_mbox_runpendq(adapter, NULL);
3726 kioc->done(kioc);
3728 return;
3733 * gather_hbainfo - HBA characteristics for the applications
3734 * @adapter : HBA soft state
3735 * @hinfo : pointer to the caller's host info strucuture
3737 static int
3738 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3740 hinfo->pci_vendor_id = adapter->pdev->vendor;
3741 hinfo->pci_device_id = adapter->pdev->device;
3742 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3743 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3745 hinfo->pci_bus = adapter->pdev->bus->number;
3746 hinfo->pci_dev_fn = adapter->pdev->devfn;
3747 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3748 hinfo->irq = adapter->host->irq;
3749 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3751 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3752 hinfo->host_no = adapter->host->host_no;
3754 return 0;
3758 * END: Interface for the common management module
3764 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3765 * @adapter : controller's soft state
3767 * Allocate packets required to issue FW calls whenever the sysfs attributes
3768 * are read. These attributes would require up-to-date information from the
3769 * FW. Also set up resources for mutual exclusion to share these resources and
3770 * the wait queue.
3772 * Return 0 on success.
3773 * Return -ERROR_CODE on failure.
3775 static int
3776 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3778 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3779 int rval = 0;
3781 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3783 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3785 raid_dev->sysfs_buffer = dma_alloc_coherent(&adapter->pdev->dev,
3786 PAGE_SIZE, &raid_dev->sysfs_buffer_dma, GFP_KERNEL);
3788 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3789 !raid_dev->sysfs_buffer) {
3791 con_log(CL_ANN, (KERN_WARNING
3792 "megaraid: out of memory, %s %d\n", __func__,
3793 __LINE__));
3795 rval = -ENOMEM;
3797 megaraid_sysfs_free_resources(adapter);
3800 mutex_init(&raid_dev->sysfs_mtx);
3802 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3804 return rval;
3809 * megaraid_sysfs_free_resources - free sysfs related resources
3810 * @adapter : controller's soft state
3812 * Free packets allocated for sysfs FW commands
3814 static void
3815 megaraid_sysfs_free_resources(adapter_t *adapter)
3817 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3819 kfree(raid_dev->sysfs_uioc);
3820 kfree(raid_dev->sysfs_mbox64);
3822 if (raid_dev->sysfs_buffer) {
3823 dma_free_coherent(&adapter->pdev->dev, PAGE_SIZE,
3824 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3830 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3831 * @uioc : completed packet
3833 * Callback routine called in the ISR/tasklet context for get ldmap call
3835 static void
3836 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3838 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3839 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3841 uioc->status = 0;
3843 wake_up(&raid_dev->sysfs_wait_q);
3847 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3848 * @t : timed out timer
3850 * Timeout routine to recover and return to application, in case the adapter
3851 * has stopped responding. A timeout of 60 seconds for this command seems like
3852 * a good value.
3854 static void
3855 megaraid_sysfs_get_ldmap_timeout(struct timer_list *t)
3857 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
3858 uioc_t *uioc = timeout->uioc;
3859 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3860 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3862 uioc->status = -ETIME;
3864 wake_up(&raid_dev->sysfs_wait_q);
3869 * megaraid_sysfs_get_ldmap - get update logical drive map
3870 * @adapter : controller's soft state
3872 * This routine will be called whenever user reads the logical drive
3873 * attributes, go get the current logical drive mapping table from the
3874 * firmware. We use the management API's to issue commands to the controller.
3876 * NOTE: The commands issuance functionality is not generalized and
3877 * implemented in context of "get ld map" command only. If required, the
3878 * command issuance logical can be trivially pulled out and implemented as a
3879 * standalone library. For now, this should suffice since there is no other
3880 * user of this interface.
3882 * Return 0 on success.
3883 * Return -1 on failure.
3885 static int
3886 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3888 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3889 uioc_t *uioc;
3890 mbox64_t *mbox64;
3891 mbox_t *mbox;
3892 char *raw_mbox;
3893 struct uioc_timeout timeout;
3894 caddr_t ldmap;
3895 int rval = 0;
3898 * Allow only one read at a time to go through the sysfs attributes
3900 mutex_lock(&raid_dev->sysfs_mtx);
3902 uioc = raid_dev->sysfs_uioc;
3903 mbox64 = raid_dev->sysfs_mbox64;
3904 ldmap = raid_dev->sysfs_buffer;
3906 memset(uioc, 0, sizeof(uioc_t));
3907 memset(mbox64, 0, sizeof(mbox64_t));
3908 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3910 mbox = &mbox64->mbox32;
3911 raw_mbox = (char *)mbox;
3912 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3913 uioc->buf_vaddr = (caddr_t)adapter;
3914 uioc->status = -ENODATA;
3915 uioc->done = megaraid_sysfs_get_ldmap_done;
3918 * Prepare the mailbox packet to get the current logical drive mapping
3919 * table
3921 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3923 raw_mbox[0] = FC_DEL_LOGDRV;
3924 raw_mbox[2] = OP_GET_LDID_MAP;
3927 * Setup a timer to recover from a non-responding controller
3929 timeout.uioc = uioc;
3930 timer_setup_on_stack(&timeout.timer,
3931 megaraid_sysfs_get_ldmap_timeout, 0);
3933 timeout.timer.expires = jiffies + 60 * HZ;
3934 add_timer(&timeout.timer);
3937 * Send the command to the firmware
3939 rval = megaraid_mbox_mm_command(adapter, uioc);
3941 if (rval == 0) { // command successfully issued
3942 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3945 * Check if the command timed out
3947 if (uioc->status == -ETIME) {
3948 con_log(CL_ANN, (KERN_NOTICE
3949 "megaraid: sysfs get ld map timed out\n"));
3951 rval = -ETIME;
3953 else {
3954 rval = mbox->status;
3957 if (rval == 0) {
3958 memcpy(raid_dev->curr_ldmap, ldmap,
3959 sizeof(raid_dev->curr_ldmap));
3961 else {
3962 con_log(CL_ANN, (KERN_NOTICE
3963 "megaraid: get ld map failed with %x\n", rval));
3966 else {
3967 con_log(CL_ANN, (KERN_NOTICE
3968 "megaraid: could not issue ldmap command:%x\n", rval));
3972 del_timer_sync(&timeout.timer);
3973 destroy_timer_on_stack(&timeout.timer);
3975 mutex_unlock(&raid_dev->sysfs_mtx);
3977 return rval;
3982 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
3983 * @cdev : class device object representation for the host
3984 * @buf : buffer to send data to
3986 * Display the handle used by the applications while executing management
3987 * tasks on the adapter. We invoke a management module API to get the adapter
3988 * handle, since we do not interface with applications directly.
3990 static ssize_t
3991 megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
3992 char *buf)
3994 struct Scsi_Host *shost = class_to_shost(dev);
3995 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
3996 uint32_t app_hndl;
3998 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4000 return snprintf(buf, 8, "%u\n", app_hndl);
4005 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4006 * @dev : device object representation for the scsi device
4007 * @attr : device attribute to show
4008 * @buf : buffer to send data to
4010 * Display the logical drive number for the device in question, if it a valid
4011 * logical drive. For physical devices, "-1" is returned.
4013 * The logical drive number is displayed in following format:
4015 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4017 * <int> <int> <int> <int>
4019 static ssize_t
4020 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4022 struct scsi_device *sdev = to_scsi_device(dev);
4023 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4024 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4025 int scsi_id = -1;
4026 int logical_drv = -1;
4027 int ldid_map = -1;
4028 uint32_t app_hndl = 0;
4029 int mapped_sdev_id;
4030 int rval;
4031 int i;
4033 if (raid_dev->random_del_supported &&
4034 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4036 rval = megaraid_sysfs_get_ldmap(adapter);
4037 if (rval == 0) {
4039 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4041 mapped_sdev_id = sdev->id;
4043 if (sdev->id > adapter->init_id) {
4044 mapped_sdev_id -= 1;
4047 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4049 scsi_id = sdev->id;
4051 logical_drv = i;
4053 ldid_map = raid_dev->curr_ldmap[i];
4055 app_hndl = mraid_mm_adapter_app_handle(
4056 adapter->unique_id);
4058 break;
4062 else {
4063 con_log(CL_ANN, (KERN_NOTICE
4064 "megaraid: sysfs get ld map failed: %x\n",
4065 rval));
4069 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4070 ldid_map, app_hndl);
4075 * END: Mailbox Low Level Driver
4077 module_init(megaraid_init);
4078 module_exit(megaraid_exit);
4080 /* vim: set ts=8 sw=8 tw=78 ai si: */