Use physical addresses at the interface level, letting drivers remap
[linux-2.6/mini2440.git] / drivers / scsi / megaraid / megaraid_mbox.c
blobc9e743ba09ec108d23129ae9d06d90092299aa8e
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.4.6 (Mar 07 2005)
15 * Authors:
16 * Atul Mukker <Atul.Mukker@lsil.com>
17 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
18 * Manoj Jose <Manoj.Jose@lsil.com>
20 * List of supported controllers
22 * OEM Product Name VID DID SSVID SSID
23 * --- ------------ --- --- ---- ----
24 * Dell PERC3/QC 101E 1960 1028 0471
25 * Dell PERC3/DC 101E 1960 1028 0493
26 * Dell PERC3/SC 101E 1960 1028 0475
27 * Dell PERC3/Di 1028 1960 1028 0123
28 * Dell PERC4/SC 1000 1960 1028 0520
29 * Dell PERC4/DC 1000 1960 1028 0518
30 * Dell PERC4/QC 1000 0407 1028 0531
31 * Dell PERC4/Di 1028 000F 1028 014A
32 * Dell PERC 4e/Si 1028 0013 1028 016c
33 * Dell PERC 4e/Di 1028 0013 1028 016d
34 * Dell PERC 4e/Di 1028 0013 1028 016e
35 * Dell PERC 4e/Di 1028 0013 1028 016f
36 * Dell PERC 4e/Di 1028 0013 1028 0170
37 * Dell PERC 4e/DC 1000 0408 1028 0002
38 * Dell PERC 4e/SC 1000 0408 1028 0001
41 * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
42 * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
43 * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
44 * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
45 * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
46 * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
47 * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
48 * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
49 * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
50 * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
51 * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
52 * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
54 * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
55 * INTEL RAID Controller SRCS16 1000 1960 8086 0523
56 * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
57 * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
58 * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
59 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
60 * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
61 * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
63 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
65 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
67 * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
69 * For history of changes, see Documentation/ChangeLog.megaraid
72 #include "megaraid_mbox.h"
74 static int megaraid_init(void);
75 static void megaraid_exit(void);
77 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
78 static void megaraid_detach_one(struct pci_dev *);
79 static void megaraid_mbox_shutdown(struct pci_dev *);
81 static int megaraid_io_attach(adapter_t *);
82 static void megaraid_io_detach(adapter_t *);
84 static int megaraid_init_mbox(adapter_t *);
85 static void megaraid_fini_mbox(adapter_t *);
87 static int megaraid_alloc_cmd_packets(adapter_t *);
88 static void megaraid_free_cmd_packets(adapter_t *);
90 static int megaraid_mbox_setup_dma_pools(adapter_t *);
91 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
93 static int megaraid_sysfs_alloc_resources(adapter_t *);
94 static void megaraid_sysfs_free_resources(adapter_t *);
96 static int megaraid_abort_handler(struct scsi_cmnd *);
97 static int megaraid_reset_handler(struct scsi_cmnd *);
99 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
100 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
101 static int megaraid_busywait_mbox(mraid_device_t *);
102 static int megaraid_mbox_product_info(adapter_t *);
103 static int megaraid_mbox_extended_cdb(adapter_t *);
104 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
105 static int megaraid_mbox_support_random_del(adapter_t *);
106 static int megaraid_mbox_get_max_sg(adapter_t *);
107 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
108 static void megaraid_mbox_flush_cache(adapter_t *);
110 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
111 static void megaraid_mbox_setup_device_map(adapter_t *);
113 static int megaraid_queue_command(struct scsi_cmnd *,
114 void (*)(struct scsi_cmnd *));
115 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
116 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
117 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
118 struct scsi_cmnd *);
119 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
120 struct scsi_cmnd *);
122 static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
124 static void megaraid_mbox_dpc(unsigned long);
126 static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
127 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
129 static int megaraid_cmm_register(adapter_t *);
130 static int megaraid_cmm_unregister(adapter_t *);
131 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
132 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
133 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
134 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
135 static int wait_till_fw_empty(adapter_t *);
139 MODULE_AUTHOR("LSI Logic Corporation");
140 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
141 MODULE_LICENSE("GPL");
142 MODULE_VERSION(MEGARAID_VERSION);
145 * ### modules parameters for driver ###
149 * Set to enable driver to expose unconfigured disk to kernel
151 static int megaraid_expose_unconf_disks = 0;
152 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
153 MODULE_PARM_DESC(unconf_disks,
154 "Set to expose unconfigured disks to kernel (default=0)");
157 * driver wait time if the adapter's mailbox is busy
159 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
160 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
161 MODULE_PARM_DESC(busy_wait,
162 "Max wait for mailbox in microseconds if busy (default=10)");
165 * number of sectors per IO command
167 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
168 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
169 MODULE_PARM_DESC(max_sectors,
170 "Maximum number of sectors per IO command (default=128)");
173 * number of commands per logical unit
175 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
176 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
177 MODULE_PARM_DESC(cmd_per_lun,
178 "Maximum number of commands per logical unit (default=64)");
182 * Fast driver load option, skip scanning for physical devices during load.
183 * This would result in non-disk devices being skipped during driver load
184 * time. These can be later added though, using /proc/scsi/scsi
186 static unsigned int megaraid_fast_load = 0;
187 module_param_named(fast_load, megaraid_fast_load, int, 0);
188 MODULE_PARM_DESC(fast_load,
189 "Faster loading of the driver, skips physical devices! (default=0)");
193 * mraid_debug level - threshold for amount of information to be displayed by
194 * the driver. This level can be changed through modules parameters, ioctl or
195 * sysfs/proc interface. By default, print the announcement messages only.
197 int mraid_debug_level = CL_ANN;
198 module_param_named(debug_level, mraid_debug_level, int, 0);
199 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
202 * ### global data ###
204 static uint8_t megaraid_mbox_version[8] =
205 { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
209 * PCI table for all supported controllers.
211 static struct pci_device_id pci_id_table_g[] = {
213 PCI_VENDOR_ID_DELL,
214 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
215 PCI_VENDOR_ID_DELL,
216 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
219 PCI_VENDOR_ID_LSI_LOGIC,
220 PCI_DEVICE_ID_PERC4_SC,
221 PCI_VENDOR_ID_DELL,
222 PCI_SUBSYS_ID_PERC4_SC,
225 PCI_VENDOR_ID_LSI_LOGIC,
226 PCI_DEVICE_ID_PERC4_DC,
227 PCI_VENDOR_ID_DELL,
228 PCI_SUBSYS_ID_PERC4_DC,
231 PCI_VENDOR_ID_LSI_LOGIC,
232 PCI_DEVICE_ID_VERDE,
233 PCI_ANY_ID,
234 PCI_ANY_ID,
237 PCI_VENDOR_ID_DELL,
238 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
239 PCI_VENDOR_ID_DELL,
240 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
243 PCI_VENDOR_ID_DELL,
244 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
245 PCI_VENDOR_ID_DELL,
246 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
249 PCI_VENDOR_ID_DELL,
250 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
251 PCI_VENDOR_ID_DELL,
252 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
255 PCI_VENDOR_ID_DELL,
256 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
257 PCI_VENDOR_ID_DELL,
258 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
261 PCI_VENDOR_ID_DELL,
262 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
263 PCI_VENDOR_ID_DELL,
264 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
267 PCI_VENDOR_ID_DELL,
268 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
269 PCI_VENDOR_ID_DELL,
270 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
273 PCI_VENDOR_ID_LSI_LOGIC,
274 PCI_DEVICE_ID_DOBSON,
275 PCI_ANY_ID,
276 PCI_ANY_ID,
279 PCI_VENDOR_ID_AMI,
280 PCI_DEVICE_ID_AMI_MEGARAID3,
281 PCI_VENDOR_ID_DELL,
282 PCI_SUBSYS_ID_PERC3_QC,
285 PCI_VENDOR_ID_AMI,
286 PCI_DEVICE_ID_AMI_MEGARAID3,
287 PCI_VENDOR_ID_DELL,
288 PCI_SUBSYS_ID_PERC3_DC,
291 PCI_VENDOR_ID_AMI,
292 PCI_DEVICE_ID_AMI_MEGARAID3,
293 PCI_VENDOR_ID_DELL,
294 PCI_SUBSYS_ID_PERC3_SC,
297 PCI_VENDOR_ID_AMI,
298 PCI_DEVICE_ID_AMI_MEGARAID3,
299 PCI_VENDOR_ID_AMI,
300 PCI_SUBSYS_ID_PERC3_SC,
303 PCI_VENDOR_ID_AMI,
304 PCI_DEVICE_ID_AMI_MEGARAID3,
305 PCI_VENDOR_ID_AMI,
306 PCI_SUBSYS_ID_PERC3_DC,
309 PCI_VENDOR_ID_LSI_LOGIC,
310 PCI_DEVICE_ID_MEGARAID_SCSI_320_0,
311 PCI_VENDOR_ID_LSI_LOGIC,
312 PCI_SUBSYS_ID_MEGARAID_SCSI_320_0,
315 PCI_VENDOR_ID_LSI_LOGIC,
316 PCI_DEVICE_ID_MEGARAID_SCSI_320_1,
317 PCI_VENDOR_ID_LSI_LOGIC,
318 PCI_SUBSYS_ID_MEGARAID_SCSI_320_1,
321 PCI_VENDOR_ID_LSI_LOGIC,
322 PCI_DEVICE_ID_MEGARAID_SCSI_320_2,
323 PCI_VENDOR_ID_LSI_LOGIC,
324 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2,
327 PCI_VENDOR_ID_LSI_LOGIC,
328 PCI_DEVICE_ID_MEGARAID_I4_133_RAID,
329 PCI_VENDOR_ID_LSI_LOGIC,
330 PCI_SUBSYS_ID_MEGARAID_I4_133_RAID,
333 PCI_VENDOR_ID_LSI_LOGIC,
334 PCI_DEVICE_ID_MEGARAID_SATA_150_4,
335 PCI_VENDOR_ID_LSI_LOGIC,
336 PCI_SUBSYS_ID_MEGARAID_SATA_150_4,
339 PCI_VENDOR_ID_LSI_LOGIC,
340 PCI_DEVICE_ID_MEGARAID_SATA_150_6,
341 PCI_VENDOR_ID_LSI_LOGIC,
342 PCI_SUBSYS_ID_MEGARAID_SATA_150_6,
345 PCI_VENDOR_ID_LSI_LOGIC,
346 PCI_DEVICE_ID_LINDSAY,
347 PCI_ANY_ID,
348 PCI_ANY_ID,
351 PCI_VENDOR_ID_LSI_LOGIC,
352 PCI_DEVICE_ID_INTEL_RAID_SRCS16,
353 PCI_VENDOR_ID_INTEL,
354 PCI_SUBSYS_ID_INTEL_RAID_SRCS16,
357 PCI_VENDOR_ID_LSI_LOGIC,
358 PCI_DEVICE_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
359 PCI_VENDOR_ID_INTEL,
360 PCI_SUBSYS_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
362 {0} /* Terminating entry */
364 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
367 static struct pci_driver megaraid_pci_driver_g = {
368 .name = "megaraid",
369 .id_table = pci_id_table_g,
370 .probe = megaraid_probe_one,
371 .remove = __devexit_p(megaraid_detach_one),
372 .shutdown = megaraid_mbox_shutdown,
377 // definitions for the device attributes for exporting logical drive number
378 // for a scsi address (Host, Channel, Id, Lun)
380 CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
381 NULL);
383 // Host template initializer for megaraid mbox sysfs device attributes
384 static struct class_device_attribute *megaraid_shost_attrs[] = {
385 &class_device_attr_megaraid_mbox_app_hndl,
386 NULL,
390 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
392 // Host template initializer for megaraid mbox sysfs device attributes
393 static struct device_attribute *megaraid_sdev_attrs[] = {
394 &dev_attr_megaraid_mbox_ld,
395 NULL,
400 * Scsi host template for megaraid unified driver
402 static struct scsi_host_template megaraid_template_g = {
403 .module = THIS_MODULE,
404 .name = "LSI Logic MegaRAID driver",
405 .proc_name = "megaraid",
406 .queuecommand = megaraid_queue_command,
407 .eh_abort_handler = megaraid_abort_handler,
408 .eh_device_reset_handler = megaraid_reset_handler,
409 .eh_bus_reset_handler = megaraid_reset_handler,
410 .eh_host_reset_handler = megaraid_reset_handler,
411 .use_clustering = ENABLE_CLUSTERING,
412 .sdev_attrs = megaraid_sdev_attrs,
413 .shost_attrs = megaraid_shost_attrs,
418 * megaraid_init - module load hook
420 * We register ourselves as hotplug enabled module and let PCI subsystem
421 * discover our adaters
423 static int __init
424 megaraid_init(void)
426 int rval;
428 // Announce the driver version
429 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
430 MEGARAID_EXT_VERSION));
432 // check validity of module parameters
433 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
435 con_log(CL_ANN, (KERN_WARNING
436 "megaraid mailbox: max commands per lun reset to %d\n",
437 MBOX_MAX_SCSI_CMDS));
439 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
443 // register as a PCI hot-plug driver module
444 rval = pci_register_driver(&megaraid_pci_driver_g);
445 if (rval < 0) {
446 con_log(CL_ANN, (KERN_WARNING
447 "megaraid: could not register hotplug support.\n"));
450 return rval;
455 * megaraid_exit - driver unload entry point
457 * We simply unwrap the megaraid_init routine here
459 static void __exit
460 megaraid_exit(void)
462 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
464 // unregister as PCI hotplug driver
465 pci_unregister_driver(&megaraid_pci_driver_g);
467 return;
472 * megaraid_probe_one - PCI hotplug entry point
473 * @param pdev : handle to this controller's PCI configuration space
474 * @param id : pci device id of the class of controllers
476 * This routine should be called whenever a new adapter is detected by the
477 * PCI hotplug susbsytem.
479 static int __devinit
480 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
482 adapter_t *adapter;
485 // detected a new controller
486 con_log(CL_ANN, (KERN_INFO
487 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
488 pdev->vendor, pdev->device, pdev->subsystem_vendor,
489 pdev->subsystem_device));
491 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
492 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
494 if (pci_enable_device(pdev)) {
495 con_log(CL_ANN, (KERN_WARNING
496 "megaraid: pci_enable_device failed\n"));
498 return -ENODEV;
501 // Enable bus-mastering on this controller
502 pci_set_master(pdev);
504 // Allocate the per driver initialization structure
505 adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
507 if (adapter == NULL) {
508 con_log(CL_ANN, (KERN_WARNING
509 "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
511 goto out_probe_one;
513 memset(adapter, 0, sizeof(adapter_t));
516 // set up PCI related soft state and other pre-known parameters
517 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
518 adapter->irq = pdev->irq;
519 adapter->pdev = pdev;
521 atomic_set(&adapter->being_detached, 0);
523 // Setup the default DMA mask. This would be changed later on
524 // depending on hardware capabilities
525 if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
527 con_log(CL_ANN, (KERN_WARNING
528 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
530 goto out_free_adapter;
534 // Initialize the synchronization lock for kernel and LLD
535 spin_lock_init(&adapter->lock);
536 adapter->host_lock = &adapter->lock;
539 // Initialize the command queues: the list of free SCBs and the list
540 // of pending SCBs.
541 INIT_LIST_HEAD(&adapter->kscb_pool);
542 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
544 INIT_LIST_HEAD(&adapter->pend_list);
545 spin_lock_init(PENDING_LIST_LOCK(adapter));
547 INIT_LIST_HEAD(&adapter->completed_list);
548 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
551 // Start the mailbox based controller
552 if (megaraid_init_mbox(adapter) != 0) {
553 con_log(CL_ANN, (KERN_WARNING
554 "megaraid: maibox adapter did not initialize\n"));
556 goto out_free_adapter;
559 // Register with LSI Common Management Module
560 if (megaraid_cmm_register(adapter) != 0) {
562 con_log(CL_ANN, (KERN_WARNING
563 "megaraid: could not register with management module\n"));
565 goto out_fini_mbox;
568 // setup adapter handle in PCI soft state
569 pci_set_drvdata(pdev, adapter);
571 // attach with scsi mid-layer
572 if (megaraid_io_attach(adapter) != 0) {
574 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
576 goto out_cmm_unreg;
579 return 0;
581 out_cmm_unreg:
582 pci_set_drvdata(pdev, NULL);
583 megaraid_cmm_unregister(adapter);
584 out_fini_mbox:
585 megaraid_fini_mbox(adapter);
586 out_free_adapter:
587 kfree(adapter);
588 out_probe_one:
589 pci_disable_device(pdev);
591 return -ENODEV;
596 * megaraid_detach_one - release the framework resources and call LLD release
597 * routine
598 * @param pdev : handle for our PCI cofiguration space
600 * This routine is called during driver unload. We free all the allocated
601 * resources and call the corresponding LLD so that it can also release all
602 * its resources.
604 * This routine is also called from the PCI hotplug system
606 static void
607 megaraid_detach_one(struct pci_dev *pdev)
609 adapter_t *adapter;
610 struct Scsi_Host *host;
613 // Start a rollback on this adapter
614 adapter = pci_get_drvdata(pdev);
616 if (!adapter) {
617 con_log(CL_ANN, (KERN_CRIT
618 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
619 pdev->vendor, pdev->device, pdev->subsystem_vendor,
620 pdev->subsystem_device));
622 return;
624 else {
625 con_log(CL_ANN, (KERN_NOTICE
626 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
627 pdev->vendor, pdev->device, pdev->subsystem_vendor,
628 pdev->subsystem_device));
632 host = adapter->host;
634 // do not allow any more requests from the management module for this
635 // adapter.
636 // FIXME: How do we account for the request which might still be
637 // pending with us?
638 atomic_set(&adapter->being_detached, 1);
640 // detach from the IO sub-system
641 megaraid_io_detach(adapter);
643 // reset the device state in the PCI structure. We check this
644 // condition when we enter here. If the device state is NULL,
645 // that would mean the device has already been removed
646 pci_set_drvdata(pdev, NULL);
648 // Unregister from common management module
650 // FIXME: this must return success or failure for conditions if there
651 // is a command pending with LLD or not.
652 megaraid_cmm_unregister(adapter);
654 // finalize the mailbox based controller and release all resources
655 megaraid_fini_mbox(adapter);
657 kfree(adapter);
659 scsi_host_put(host);
661 pci_disable_device(pdev);
663 return;
668 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
669 * @param device : generice driver model device
671 * Shutdown notification, perform flush cache
673 static void
674 megaraid_mbox_shutdown(struct pci_dev *pdev)
676 adapter_t *adapter = pci_get_drvdata(pdev);
677 static int counter;
679 if (!adapter) {
680 con_log(CL_ANN, (KERN_WARNING
681 "megaraid: null device in shutdown\n"));
682 return;
685 // flush caches now
686 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
687 counter++));
689 megaraid_mbox_flush_cache(adapter);
691 con_log(CL_ANN, ("done\n"));
696 * megaraid_io_attach - attach a device with the IO subsystem
697 * @param adapter : controller's soft state
699 * Attach this device with the IO subsystem
701 static int
702 megaraid_io_attach(adapter_t *adapter)
704 struct Scsi_Host *host;
706 // Initialize SCSI Host structure
707 host = scsi_host_alloc(&megaraid_template_g, 8);
708 if (!host) {
709 con_log(CL_ANN, (KERN_WARNING
710 "megaraid mbox: scsi_register failed\n"));
712 return -1;
715 SCSIHOST2ADAP(host) = (caddr_t)adapter;
716 adapter->host = host;
718 // export the parameters required by the mid-layer
719 scsi_assign_lock(host, adapter->host_lock);
721 host->irq = adapter->irq;
722 host->unique_id = adapter->unique_id;
723 host->can_queue = adapter->max_cmds;
724 host->this_id = adapter->init_id;
725 host->sg_tablesize = adapter->sglen;
726 host->max_sectors = adapter->max_sectors;
727 host->cmd_per_lun = adapter->cmd_per_lun;
728 host->max_channel = adapter->max_channel;
729 host->max_id = adapter->max_target;
730 host->max_lun = adapter->max_lun;
733 // notify mid-layer about the new controller
734 if (scsi_add_host(host, &adapter->pdev->dev)) {
736 con_log(CL_ANN, (KERN_WARNING
737 "megaraid mbox: scsi_add_host failed\n"));
739 scsi_host_put(host);
741 return -1;
744 scsi_scan_host(host);
746 return 0;
751 * megaraid_io_detach - detach a device from the IO subsystem
752 * @param adapter : controller's soft state
754 * Detach this device from the IO subsystem
756 static void
757 megaraid_io_detach(adapter_t *adapter)
759 struct Scsi_Host *host;
761 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
763 host = adapter->host;
765 scsi_remove_host(host);
767 return;
772 * START: Mailbox Low Level Driver
774 * This is section specific to the single mailbox based controllers
778 * megaraid_init_mbox - initialize controller
779 * @param adapter - our soft state
781 * . Allocate 16-byte aligned mailbox memory for firmware handshake
782 * . Allocate controller's memory resources
783 * . Find out all initialization data
784 * . Allocate memory required for all the commands
785 * . Use internal library of FW routines, build up complete soft state
787 static int __init
788 megaraid_init_mbox(adapter_t *adapter)
790 struct pci_dev *pdev;
791 mraid_device_t *raid_dev;
792 int i;
795 adapter->ito = MBOX_TIMEOUT;
796 pdev = adapter->pdev;
799 * Allocate and initialize the init data structure for mailbox
800 * controllers
802 raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
803 if (raid_dev == NULL) return -1;
805 memset(raid_dev, 0, sizeof(mraid_device_t));
808 * Attach the adapter soft state to raid device soft state
810 adapter->raid_device = (caddr_t)raid_dev;
811 raid_dev->fast_load = megaraid_fast_load;
814 // our baseport
815 raid_dev->baseport = pci_resource_start(pdev, 0);
817 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
819 con_log(CL_ANN, (KERN_WARNING
820 "megaraid: mem region busy\n"));
822 goto out_free_raid_dev;
825 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
827 if (!raid_dev->baseaddr) {
829 con_log(CL_ANN, (KERN_WARNING
830 "megaraid: could not map hba memory\n") );
832 goto out_release_regions;
836 // Setup the rest of the soft state using the library of FW routines
839 // request IRQ and register the interrupt service routine
840 if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
841 adapter)) {
843 con_log(CL_ANN, (KERN_WARNING
844 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
846 goto out_iounmap;
850 // initialize the mutual exclusion lock for the mailbox
851 spin_lock_init(&raid_dev->mailbox_lock);
853 // allocate memory required for commands
854 if (megaraid_alloc_cmd_packets(adapter) != 0) {
855 goto out_free_irq;
858 // Product info
859 if (megaraid_mbox_product_info(adapter) != 0) {
860 goto out_alloc_cmds;
863 // Do we support extended CDBs
864 adapter->max_cdb_sz = 10;
865 if (megaraid_mbox_extended_cdb(adapter) == 0) {
866 adapter->max_cdb_sz = 16;
870 * Do we support cluster environment, if we do, what is the initiator
871 * id.
872 * NOTE: In a non-cluster aware firmware environment, the LLD should
873 * return 7 as initiator id.
875 adapter->ha = 0;
876 adapter->init_id = -1;
877 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
878 adapter->ha = 1;
882 * Prepare the device ids array to have the mapping between the kernel
883 * device address and megaraid device address.
884 * We export the physical devices on their actual addresses. The
885 * logical drives are exported on a virtual SCSI channel
887 megaraid_mbox_setup_device_map(adapter);
889 // If the firmware supports random deletion, update the device id map
890 if (megaraid_mbox_support_random_del(adapter)) {
892 // Change the logical drives numbers in device_ids array one
893 // slot in device_ids is reserved for target id, that's why
894 // "<=" below
895 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
896 adapter->device_ids[adapter->max_channel][i] += 0x80;
898 adapter->device_ids[adapter->max_channel][adapter->init_id] =
899 0xFF;
901 raid_dev->random_del_supported = 1;
905 * find out the maximum number of scatter-gather elements supported by
906 * this firmware
908 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
910 // enumerate RAID and SCSI channels so that all devices on SCSI
911 // channels can later be exported, including disk devices
912 megaraid_mbox_enum_raid_scsi(adapter);
915 * Other parameters required by upper layer
917 * maximum number of sectors per IO command
919 adapter->max_sectors = megaraid_max_sectors;
922 * number of queued commands per LUN.
924 adapter->cmd_per_lun = megaraid_cmd_per_lun;
927 * Allocate resources required to issue FW calls, when sysfs is
928 * accessed
930 if (megaraid_sysfs_alloc_resources(adapter) != 0) {
931 goto out_alloc_cmds;
934 // Set the DMA mask to 64-bit. All supported controllers as capable of
935 // DMA in this range
936 if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
938 con_log(CL_ANN, (KERN_WARNING
939 "megaraid: could not set DMA mask for 64-bit.\n"));
941 goto out_free_sysfs_res;
944 // setup tasklet for DPC
945 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
946 (unsigned long)adapter);
948 con_log(CL_DLEVEL1, (KERN_INFO
949 "megaraid mbox hba successfully initialized\n"));
951 return 0;
953 out_free_sysfs_res:
954 megaraid_sysfs_free_resources(adapter);
955 out_alloc_cmds:
956 megaraid_free_cmd_packets(adapter);
957 out_free_irq:
958 free_irq(adapter->irq, adapter);
959 out_iounmap:
960 iounmap(raid_dev->baseaddr);
961 out_release_regions:
962 pci_release_regions(pdev);
963 out_free_raid_dev:
964 kfree(raid_dev);
966 return -1;
971 * megaraid_fini_mbox - undo controller initialization
972 * @param adapter : our soft state
974 static void
975 megaraid_fini_mbox(adapter_t *adapter)
977 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
979 // flush all caches
980 megaraid_mbox_flush_cache(adapter);
982 tasklet_kill(&adapter->dpc_h);
984 megaraid_sysfs_free_resources(adapter);
986 megaraid_free_cmd_packets(adapter);
988 free_irq(adapter->irq, adapter);
990 iounmap(raid_dev->baseaddr);
992 pci_release_regions(adapter->pdev);
994 kfree(raid_dev);
996 return;
1001 * megaraid_alloc_cmd_packets - allocate shared mailbox
1002 * @param adapter : soft state of the raid controller
1004 * Allocate and align the shared mailbox. This maibox is used to issue
1005 * all the commands. For IO based controllers, the mailbox is also regsitered
1006 * with the FW. Allocate memory for all commands as well.
1007 * This is our big allocator
1009 static int
1010 megaraid_alloc_cmd_packets(adapter_t *adapter)
1012 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1013 struct pci_dev *pdev;
1014 unsigned long align;
1015 scb_t *scb;
1016 mbox_ccb_t *ccb;
1017 struct mraid_pci_blk *epthru_pci_blk;
1018 struct mraid_pci_blk *sg_pci_blk;
1019 struct mraid_pci_blk *mbox_pci_blk;
1020 int i;
1022 pdev = adapter->pdev;
1025 * Setup the mailbox
1026 * Allocate the common 16-byte aligned memory for the handshake
1027 * mailbox.
1029 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1030 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1032 if (!raid_dev->una_mbox64) {
1033 con_log(CL_ANN, (KERN_WARNING
1034 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1035 __LINE__));
1036 return -1;
1038 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1041 * Align the mailbox at 16-byte boundary
1043 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1045 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1046 (~0UL ^ 0xFUL));
1048 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1050 align = ((void *)raid_dev->mbox -
1051 ((void *)&raid_dev->una_mbox64->mbox32));
1053 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1054 align;
1056 // Allocate memory for commands issued internally
1057 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1058 &adapter->ibuf_dma_h);
1059 if (!adapter->ibuf) {
1061 con_log(CL_ANN, (KERN_WARNING
1062 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1063 __LINE__));
1065 goto out_free_common_mbox;
1067 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1069 // Allocate memory for our SCSI Command Blocks and their associated
1070 // memory
1073 * Allocate memory for the base list of scb. Later allocate memory for
1074 * CCBs and embedded components of each CCB and point the pointers in
1075 * scb to the allocated components
1076 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1077 * since the calling routine does not yet know the number of available
1078 * commands.
1080 adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
1081 GFP_KERNEL);
1083 if (adapter->kscb_list == NULL) {
1084 con_log(CL_ANN, (KERN_WARNING
1085 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1086 __LINE__));
1087 goto out_free_ibuf;
1089 memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
1091 // memory allocation for our command packets
1092 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1093 con_log(CL_ANN, (KERN_WARNING
1094 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1095 __LINE__));
1096 goto out_free_scb_list;
1099 // Adjust the scb pointers and link in the free pool
1100 epthru_pci_blk = raid_dev->epthru_pool;
1101 sg_pci_blk = raid_dev->sg_pool;
1102 mbox_pci_blk = raid_dev->mbox_pool;
1104 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1105 scb = adapter->kscb_list + i;
1106 ccb = raid_dev->ccb_list + i;
1108 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1109 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1110 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1111 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1113 // make sure the mailbox is aligned properly
1114 if (ccb->mbox_dma_h & 0x0F) {
1115 con_log(CL_ANN, (KERN_CRIT
1116 "megaraid mbox: not aligned on 16-bytes\n"));
1118 goto out_teardown_dma_pools;
1121 ccb->epthru = (mraid_epassthru_t *)
1122 epthru_pci_blk[i].vaddr;
1123 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1124 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1125 ccb->pthru_dma_h = ccb->epthru_dma_h;
1128 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1129 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1130 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1132 scb->ccb = (caddr_t)ccb;
1133 scb->gp = 0;
1135 scb->sno = i; // command index
1137 scb->scp = NULL;
1138 scb->state = SCB_FREE;
1139 scb->dma_direction = PCI_DMA_NONE;
1140 scb->dma_type = MRAID_DMA_NONE;
1141 scb->dev_channel = -1;
1142 scb->dev_target = -1;
1144 // put scb in the free pool
1145 list_add_tail(&scb->list, &adapter->kscb_pool);
1148 return 0;
1150 out_teardown_dma_pools:
1151 megaraid_mbox_teardown_dma_pools(adapter);
1152 out_free_scb_list:
1153 kfree(adapter->kscb_list);
1154 out_free_ibuf:
1155 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1156 adapter->ibuf_dma_h);
1157 out_free_common_mbox:
1158 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1159 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1161 return -1;
1166 * megaraid_free_cmd_packets - free memory
1167 * @param adapter : soft state of the raid controller
1169 * Release memory resources allocated for commands
1171 static void
1172 megaraid_free_cmd_packets(adapter_t *adapter)
1174 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1176 megaraid_mbox_teardown_dma_pools(adapter);
1178 kfree(adapter->kscb_list);
1180 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1181 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1183 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1184 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1185 return;
1190 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1191 * @param adapter : HBA soft state
1193 * setup the dma pools for mailbox, passthru and extended passthru structures,
1194 * and scatter-gather lists
1196 static int
1197 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1199 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1200 struct mraid_pci_blk *epthru_pci_blk;
1201 struct mraid_pci_blk *sg_pci_blk;
1202 struct mraid_pci_blk *mbox_pci_blk;
1203 int i;
1207 // Allocate memory for 16-bytes aligned mailboxes
1208 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1209 adapter->pdev,
1210 sizeof(mbox64_t) + 16,
1211 16, 0);
1213 if (raid_dev->mbox_pool_handle == NULL) {
1214 goto fail_setup_dma_pool;
1217 mbox_pci_blk = raid_dev->mbox_pool;
1218 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1219 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1220 raid_dev->mbox_pool_handle,
1221 GFP_KERNEL,
1222 &mbox_pci_blk[i].dma_addr);
1223 if (!mbox_pci_blk[i].vaddr) {
1224 goto fail_setup_dma_pool;
1229 * Allocate memory for each embedded passthru strucuture pointer
1230 * Request for a 128 bytes aligned structure for each passthru command
1231 * structure
1232 * Since passthru and extended passthru commands are exclusive, they
1233 * share common memory pool. Passthru structures piggyback on memory
1234 * allocted to extended passthru since passthru is smaller of the two
1236 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1237 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1239 if (raid_dev->epthru_pool_handle == NULL) {
1240 goto fail_setup_dma_pool;
1243 epthru_pci_blk = raid_dev->epthru_pool;
1244 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1245 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1246 raid_dev->epthru_pool_handle,
1247 GFP_KERNEL,
1248 &epthru_pci_blk[i].dma_addr);
1249 if (!epthru_pci_blk[i].vaddr) {
1250 goto fail_setup_dma_pool;
1255 // Allocate memory for each scatter-gather list. Request for 512 bytes
1256 // alignment for each sg list
1257 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1258 adapter->pdev,
1259 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1260 512, 0);
1262 if (raid_dev->sg_pool_handle == NULL) {
1263 goto fail_setup_dma_pool;
1266 sg_pci_blk = raid_dev->sg_pool;
1267 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1268 sg_pci_blk[i].vaddr = pci_pool_alloc(
1269 raid_dev->sg_pool_handle,
1270 GFP_KERNEL,
1271 &sg_pci_blk[i].dma_addr);
1272 if (!sg_pci_blk[i].vaddr) {
1273 goto fail_setup_dma_pool;
1277 return 0;
1279 fail_setup_dma_pool:
1280 megaraid_mbox_teardown_dma_pools(adapter);
1281 return -1;
1286 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1287 * @param adapter : HBA soft state
1289 * teardown the dma pool for mailbox, passthru and extended passthru
1290 * structures, and scatter-gather lists
1292 static void
1293 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1295 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1296 struct mraid_pci_blk *epthru_pci_blk;
1297 struct mraid_pci_blk *sg_pci_blk;
1298 struct mraid_pci_blk *mbox_pci_blk;
1299 int i;
1302 sg_pci_blk = raid_dev->sg_pool;
1303 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1304 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1305 sg_pci_blk[i].dma_addr);
1307 if (raid_dev->sg_pool_handle)
1308 pci_pool_destroy(raid_dev->sg_pool_handle);
1311 epthru_pci_blk = raid_dev->epthru_pool;
1312 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1313 pci_pool_free(raid_dev->epthru_pool_handle,
1314 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1316 if (raid_dev->epthru_pool_handle)
1317 pci_pool_destroy(raid_dev->epthru_pool_handle);
1320 mbox_pci_blk = raid_dev->mbox_pool;
1321 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1322 pci_pool_free(raid_dev->mbox_pool_handle,
1323 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1325 if (raid_dev->mbox_pool_handle)
1326 pci_pool_destroy(raid_dev->mbox_pool_handle);
1328 return;
1333 * megaraid_alloc_scb - detach and return a scb from the free list
1334 * @adapter : controller's soft state
1336 * return the scb from the head of the free list. NULL if there are none
1337 * available
1339 static inline scb_t *
1340 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1342 struct list_head *head = &adapter->kscb_pool;
1343 scb_t *scb = NULL;
1344 unsigned long flags;
1346 // detach scb from free pool
1347 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1349 if (list_empty(head)) {
1350 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1351 return NULL;
1354 scb = list_entry(head->next, scb_t, list);
1355 list_del_init(&scb->list);
1357 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1359 scb->state = SCB_ACTIVE;
1360 scb->scp = scp;
1361 scb->dma_type = MRAID_DMA_NONE;
1363 return scb;
1368 * megaraid_dealloc_scb - return the scb to the free pool
1369 * @adapter : controller's soft state
1370 * @scb : scb to be freed
1372 * return the scb back to the free list of scbs. The caller must 'flush' the
1373 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1374 * NOTE NOTE: Make sure the scb is not on any list before calling this
1375 * routine.
1377 static inline void
1378 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1380 unsigned long flags;
1382 // put scb in the free pool
1383 scb->state = SCB_FREE;
1384 scb->scp = NULL;
1385 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1387 list_add(&scb->list, &adapter->kscb_pool);
1389 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1391 return;
1396 * megaraid_mbox_mksgl - make the scatter-gather list
1397 * @adapter - controller's soft state
1398 * @scb - scsi control block
1400 * prepare the scatter-gather list
1402 static inline int
1403 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1405 struct scatterlist *sgl;
1406 mbox_ccb_t *ccb;
1407 struct page *page;
1408 unsigned long offset;
1409 struct scsi_cmnd *scp;
1410 int sgcnt;
1411 int i;
1414 scp = scb->scp;
1415 ccb = (mbox_ccb_t *)scb->ccb;
1417 // no mapping required if no data to be transferred
1418 if (!scp->request_buffer || !scp->request_bufflen)
1419 return 0;
1421 if (!scp->use_sg) { /* scatter-gather list not used */
1423 page = virt_to_page(scp->request_buffer);
1425 offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
1427 ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
1428 scp->request_bufflen,
1429 scb->dma_direction);
1430 scb->dma_type = MRAID_DMA_WBUF;
1433 * We need to handle special 64-bit commands that need a
1434 * minimum of 1 SG
1436 sgcnt = 1;
1437 ccb->sgl64[0].address = ccb->buf_dma_h;
1438 ccb->sgl64[0].length = scp->request_bufflen;
1440 return sgcnt;
1443 sgl = (struct scatterlist *)scp->request_buffer;
1445 // The number of sg elements returned must not exceed our limit
1446 sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
1447 scb->dma_direction);
1449 if (sgcnt > adapter->sglen) {
1450 con_log(CL_ANN, (KERN_CRIT
1451 "megaraid critical: too many sg elements:%d\n",
1452 sgcnt));
1453 BUG();
1456 scb->dma_type = MRAID_DMA_WSG;
1458 for (i = 0; i < sgcnt; i++, sgl++) {
1459 ccb->sgl64[i].address = sg_dma_address(sgl);
1460 ccb->sgl64[i].length = sg_dma_len(sgl);
1463 // Return count of SG nodes
1464 return sgcnt;
1469 * mbox_post_cmd - issue a mailbox command
1470 * @adapter - controller's soft state
1471 * @scb - command to be issued
1473 * post the command to the controller if mailbox is availble.
1475 static inline int
1476 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1478 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1479 mbox64_t *mbox64;
1480 mbox_t *mbox;
1481 mbox_ccb_t *ccb;
1482 unsigned long flags;
1483 unsigned int i = 0;
1486 ccb = (mbox_ccb_t *)scb->ccb;
1487 mbox = raid_dev->mbox;
1488 mbox64 = raid_dev->mbox64;
1491 * Check for busy mailbox. If it is, return failure - the caller
1492 * should retry later.
1494 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1496 if (unlikely(mbox->busy)) {
1497 do {
1498 udelay(1);
1499 i++;
1500 rmb();
1501 } while(mbox->busy && (i < max_mbox_busy_wait));
1503 if (mbox->busy) {
1505 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1507 return -1;
1512 // Copy this command's mailbox data into "adapter's" mailbox
1513 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1514 mbox->cmdid = scb->sno;
1516 adapter->outstanding_cmds++;
1518 if (scb->dma_direction == PCI_DMA_TODEVICE) {
1519 if (!scb->scp->use_sg) { // sg list not used
1520 pci_dma_sync_single_for_device(adapter->pdev,
1521 ccb->buf_dma_h,
1522 scb->scp->request_bufflen,
1523 PCI_DMA_TODEVICE);
1525 else {
1526 pci_dma_sync_sg_for_device(adapter->pdev,
1527 scb->scp->request_buffer,
1528 scb->scp->use_sg, PCI_DMA_TODEVICE);
1532 mbox->busy = 1; // Set busy
1533 mbox->poll = 0;
1534 mbox->ack = 0;
1535 wmb();
1537 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1539 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1541 return 0;
1546 * megaraid_queue_command - generic queue entry point for all LLDs
1547 * @scp : pointer to the scsi command to be executed
1548 * @done : callback routine to be called after the cmd has be completed
1550 * Queue entry point for mailbox based controllers.
1552 static int
1553 megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
1555 adapter_t *adapter;
1556 scb_t *scb;
1557 int if_busy;
1559 adapter = SCP2ADAPTER(scp);
1560 scp->scsi_done = done;
1561 scp->result = 0;
1563 assert_spin_locked(adapter->host_lock);
1565 spin_unlock(adapter->host_lock);
1568 * Allocate and build a SCB request
1569 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1570 * not allocate scb. We will return non-zero status in that case.
1571 * NOTE: scb can be null even though certain commands completed
1572 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1573 * return 0 in that case, and we would do the callback right away.
1575 if_busy = 0;
1576 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1578 if (scb) {
1579 megaraid_mbox_runpendq(adapter, scb);
1582 spin_lock(adapter->host_lock);
1584 if (!scb) { // command already completed
1585 done(scp);
1586 return 0;
1589 return if_busy;
1594 * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
1595 * firmware lingua
1596 * @adapter - controller's soft state
1597 * @scp - mid-layer scsi command pointer
1598 * @busy - set if request could not be completed because of lack of
1599 * resources
1601 * convert the command issued by mid-layer to format understood by megaraid
1602 * firmware. We also complete certain command without sending them to firmware
1604 static scb_t *
1605 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1607 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1608 int channel;
1609 int target;
1610 int islogical;
1611 mbox_ccb_t *ccb;
1612 mraid_passthru_t *pthru;
1613 mbox64_t *mbox64;
1614 mbox_t *mbox;
1615 scb_t *scb;
1616 char skip[] = "skipping";
1617 char scan[] = "scanning";
1618 char *ss;
1622 * Get the appropriate device map for the device this command is
1623 * intended for
1625 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1628 * Logical drive commands
1630 if (islogical) {
1631 switch (scp->cmnd[0]) {
1632 case TEST_UNIT_READY:
1634 * Do we support clustering and is the support enabled
1635 * If no, return success always
1637 if (!adapter->ha) {
1638 scp->result = (DID_OK << 16);
1639 return NULL;
1642 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1643 scp->result = (DID_ERROR << 16);
1644 *busy = 1;
1645 return NULL;
1648 scb->dma_direction = scp->sc_data_direction;
1649 scb->dev_channel = 0xFF;
1650 scb->dev_target = target;
1651 ccb = (mbox_ccb_t *)scb->ccb;
1654 * The command id will be provided by the command
1655 * issuance routine
1657 ccb->raw_mbox[0] = CLUSTER_CMD;
1658 ccb->raw_mbox[2] = RESERVATION_STATUS;
1659 ccb->raw_mbox[3] = target;
1661 return scb;
1663 case MODE_SENSE:
1664 if (scp->use_sg) {
1665 struct scatterlist *sgl;
1666 caddr_t vaddr;
1668 sgl = (struct scatterlist *)scp->request_buffer;
1669 if (sgl->page) {
1670 vaddr = (caddr_t)
1671 (page_address((&sgl[0])->page)
1672 + (&sgl[0])->offset);
1674 memset(vaddr, 0, scp->cmnd[4]);
1676 else {
1677 con_log(CL_ANN, (KERN_WARNING
1678 "megaraid mailbox: invalid sg:%d\n",
1679 __LINE__));
1682 else {
1683 memset(scp->request_buffer, 0, scp->cmnd[4]);
1685 scp->result = (DID_OK << 16);
1686 return NULL;
1688 case INQUIRY:
1690 * Display the channel scan for logical drives
1691 * Do not display scan for a channel if already done.
1693 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1695 con_log(CL_ANN, (KERN_INFO
1696 "scsi[%d]: scanning scsi channel %d",
1697 adapter->host->host_no,
1698 SCP2CHANNEL(scp)));
1700 con_log(CL_ANN, (
1701 " [virtual] for logical drives\n"));
1703 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1706 /* Fall through */
1708 case READ_CAPACITY:
1710 * Do not allow LUN > 0 for logical drives and
1711 * requests for more than 40 logical drives
1713 if (SCP2LUN(scp)) {
1714 scp->result = (DID_BAD_TARGET << 16);
1715 return NULL;
1717 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1718 scp->result = (DID_BAD_TARGET << 16);
1719 return NULL;
1723 /* Allocate a SCB and initialize passthru */
1724 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1725 scp->result = (DID_ERROR << 16);
1726 *busy = 1;
1727 return NULL;
1730 ccb = (mbox_ccb_t *)scb->ccb;
1731 scb->dev_channel = 0xFF;
1732 scb->dev_target = target;
1733 pthru = ccb->pthru;
1734 mbox = ccb->mbox;
1735 mbox64 = ccb->mbox64;
1737 pthru->timeout = 0;
1738 pthru->ars = 1;
1739 pthru->reqsenselen = 14;
1740 pthru->islogical = 1;
1741 pthru->logdrv = target;
1742 pthru->cdblen = scp->cmd_len;
1743 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1745 mbox->cmd = MBOXCMD_PASSTHRU64;
1746 scb->dma_direction = scp->sc_data_direction;
1748 pthru->dataxferlen = scp->request_bufflen;
1749 pthru->dataxferaddr = ccb->sgl_dma_h;
1750 pthru->numsge = megaraid_mbox_mksgl(adapter,
1751 scb);
1753 mbox->xferaddr = 0xFFFFFFFF;
1754 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1755 mbox64->xferaddr_hi = 0;
1757 return scb;
1759 case READ_6:
1760 case WRITE_6:
1761 case READ_10:
1762 case WRITE_10:
1763 case READ_12:
1764 case WRITE_12:
1767 * Allocate a SCB and initialize mailbox
1769 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1770 scp->result = (DID_ERROR << 16);
1771 *busy = 1;
1772 return NULL;
1774 ccb = (mbox_ccb_t *)scb->ccb;
1775 scb->dev_channel = 0xFF;
1776 scb->dev_target = target;
1777 mbox = ccb->mbox;
1778 mbox64 = ccb->mbox64;
1779 mbox->logdrv = target;
1782 * A little HACK: 2nd bit is zero for all scsi read
1783 * commands and is set for all scsi write commands
1785 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1786 MBOXCMD_LREAD64 ;
1789 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1791 if (scp->cmd_len == 6) {
1792 mbox->numsectors = (uint32_t)scp->cmnd[4];
1793 mbox->lba =
1794 ((uint32_t)scp->cmnd[1] << 16) |
1795 ((uint32_t)scp->cmnd[2] << 8) |
1796 (uint32_t)scp->cmnd[3];
1798 mbox->lba &= 0x1FFFFF;
1802 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1804 else if (scp->cmd_len == 10) {
1805 mbox->numsectors =
1806 (uint32_t)scp->cmnd[8] |
1807 ((uint32_t)scp->cmnd[7] << 8);
1808 mbox->lba =
1809 ((uint32_t)scp->cmnd[2] << 24) |
1810 ((uint32_t)scp->cmnd[3] << 16) |
1811 ((uint32_t)scp->cmnd[4] << 8) |
1812 (uint32_t)scp->cmnd[5];
1816 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1818 else if (scp->cmd_len == 12) {
1819 mbox->lba =
1820 ((uint32_t)scp->cmnd[2] << 24) |
1821 ((uint32_t)scp->cmnd[3] << 16) |
1822 ((uint32_t)scp->cmnd[4] << 8) |
1823 (uint32_t)scp->cmnd[5];
1825 mbox->numsectors =
1826 ((uint32_t)scp->cmnd[6] << 24) |
1827 ((uint32_t)scp->cmnd[7] << 16) |
1828 ((uint32_t)scp->cmnd[8] << 8) |
1829 (uint32_t)scp->cmnd[9];
1831 else {
1832 con_log(CL_ANN, (KERN_WARNING
1833 "megaraid: unsupported CDB length\n"));
1835 megaraid_dealloc_scb(adapter, scb);
1837 scp->result = (DID_ERROR << 16);
1838 return NULL;
1841 scb->dma_direction = scp->sc_data_direction;
1843 // Calculate Scatter-Gather info
1844 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1845 mbox->numsge = megaraid_mbox_mksgl(adapter,
1846 scb);
1847 mbox->xferaddr = 0xFFFFFFFF;
1848 mbox64->xferaddr_hi = 0;
1850 return scb;
1852 case RESERVE:
1853 case RELEASE:
1855 * Do we support clustering and is the support enabled
1857 if (!adapter->ha) {
1858 scp->result = (DID_BAD_TARGET << 16);
1859 return NULL;
1863 * Allocate a SCB and initialize mailbox
1865 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1866 scp->result = (DID_ERROR << 16);
1867 *busy = 1;
1868 return NULL;
1871 ccb = (mbox_ccb_t *)scb->ccb;
1872 scb->dev_channel = 0xFF;
1873 scb->dev_target = target;
1874 ccb->raw_mbox[0] = CLUSTER_CMD;
1875 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1876 RESERVE_LD : RELEASE_LD;
1878 ccb->raw_mbox[3] = target;
1879 scb->dma_direction = scp->sc_data_direction;
1881 return scb;
1883 default:
1884 scp->result = (DID_BAD_TARGET << 16);
1885 return NULL;
1888 else { // Passthru device commands
1890 // Do not allow access to target id > 15 or LUN > 7
1891 if (target > 15 || SCP2LUN(scp) > 7) {
1892 scp->result = (DID_BAD_TARGET << 16);
1893 return NULL;
1896 // if fast load option was set and scan for last device is
1897 // over, reset the fast_load flag so that during a possible
1898 // next scan, devices can be made available
1899 if (rdev->fast_load && (target == 15) &&
1900 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1902 con_log(CL_ANN, (KERN_INFO
1903 "megaraid[%d]: physical device scan re-enabled\n",
1904 adapter->host->host_no));
1905 rdev->fast_load = 0;
1909 * Display the channel scan for physical devices
1911 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1913 ss = rdev->fast_load ? skip : scan;
1915 con_log(CL_ANN, (KERN_INFO
1916 "scsi[%d]: %s scsi channel %d [Phy %d]",
1917 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1918 channel));
1920 con_log(CL_ANN, (
1921 " for non-raid devices\n"));
1923 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1926 // disable channel sweep if fast load option given
1927 if (rdev->fast_load) {
1928 scp->result = (DID_BAD_TARGET << 16);
1929 return NULL;
1932 // Allocate a SCB and initialize passthru
1933 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1934 scp->result = (DID_ERROR << 16);
1935 *busy = 1;
1936 return NULL;
1939 ccb = (mbox_ccb_t *)scb->ccb;
1940 scb->dev_channel = channel;
1941 scb->dev_target = target;
1942 scb->dma_direction = scp->sc_data_direction;
1943 mbox = ccb->mbox;
1944 mbox64 = ccb->mbox64;
1946 // Does this firmware support extended CDBs
1947 if (adapter->max_cdb_sz == 16) {
1948 mbox->cmd = MBOXCMD_EXTPTHRU;
1950 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1952 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1953 mbox64->xferaddr_hi = 0;
1954 mbox->xferaddr = 0xFFFFFFFF;
1956 else {
1957 mbox->cmd = MBOXCMD_PASSTHRU64;
1959 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1961 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1962 mbox64->xferaddr_hi = 0;
1963 mbox->xferaddr = 0xFFFFFFFF;
1965 return scb;
1968 // NOT REACHED
1973 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1974 * @adapter : controller's soft state
1975 * @scb : SCB to be queued in the pending list
1977 * scan the pending list for commands which are not yet issued and try to
1978 * post to the controller. The SCB can be a null pointer, which would indicate
1979 * no SCB to be queue, just try to execute the ones in the pending list.
1981 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1982 * out from the head of the pending list. If it is successfully issued, the
1983 * next SCB is at the head now.
1985 static void
1986 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1988 scb_t *scb;
1989 unsigned long flags;
1991 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1993 if (scb_q) {
1994 scb_q->state = SCB_PENDQ;
1995 list_add_tail(&scb_q->list, &adapter->pend_list);
1998 // if the adapter in not in quiescent mode, post the commands to FW
1999 if (adapter->quiescent) {
2000 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2001 return;
2004 while (!list_empty(&adapter->pend_list)) {
2006 assert_spin_locked(PENDING_LIST_LOCK(adapter));
2008 scb = list_entry(adapter->pend_list.next, scb_t, list);
2010 // remove the scb from the pending list and try to
2011 // issue. If we are unable to issue it, put back in
2012 // the pending list and return
2014 list_del_init(&scb->list);
2016 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2018 // if mailbox was busy, return SCB back to pending
2019 // list. Make sure to add at the head, since that's
2020 // where it would have been removed from
2022 scb->state = SCB_ISSUED;
2024 if (mbox_post_cmd(adapter, scb) != 0) {
2026 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2028 scb->state = SCB_PENDQ;
2030 list_add(&scb->list, &adapter->pend_list);
2032 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2033 flags);
2035 return;
2038 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2041 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2044 return;
2049 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
2050 * @adapter - pointer to controller's soft state
2051 * @scb - scsi control block
2052 * @scp - scsi command from the mid-layer
2054 * prepare a command for the scsi physical devices
2056 static void
2057 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
2058 struct scsi_cmnd *scp)
2060 mbox_ccb_t *ccb;
2061 mraid_passthru_t *pthru;
2062 uint8_t channel;
2063 uint8_t target;
2065 ccb = (mbox_ccb_t *)scb->ccb;
2066 pthru = ccb->pthru;
2067 channel = scb->dev_channel;
2068 target = scb->dev_target;
2070 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2071 pthru->timeout = 4;
2072 pthru->ars = 1;
2073 pthru->islogical = 0;
2074 pthru->channel = 0;
2075 pthru->target = (channel << 4) | target;
2076 pthru->logdrv = SCP2LUN(scp);
2077 pthru->reqsenselen = 14;
2078 pthru->cdblen = scp->cmd_len;
2080 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2082 if (scp->request_bufflen) {
2083 pthru->dataxferlen = scp->request_bufflen;
2084 pthru->dataxferaddr = ccb->sgl_dma_h;
2085 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2087 else {
2088 pthru->dataxferaddr = 0;
2089 pthru->dataxferlen = 0;
2090 pthru->numsge = 0;
2092 return;
2097 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2098 * @adapter - pointer to controller's soft state
2099 * @scb - scsi control block
2100 * @scp - scsi command from the mid-layer
2102 * prepare a command for the scsi physical devices. This rountine prepares
2103 * commands for devices which can take extended CDBs (>10 bytes)
2105 static void
2106 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2107 struct scsi_cmnd *scp)
2109 mbox_ccb_t *ccb;
2110 mraid_epassthru_t *epthru;
2111 uint8_t channel;
2112 uint8_t target;
2114 ccb = (mbox_ccb_t *)scb->ccb;
2115 epthru = ccb->epthru;
2116 channel = scb->dev_channel;
2117 target = scb->dev_target;
2119 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2120 epthru->timeout = 4;
2121 epthru->ars = 1;
2122 epthru->islogical = 0;
2123 epthru->channel = 0;
2124 epthru->target = (channel << 4) | target;
2125 epthru->logdrv = SCP2LUN(scp);
2126 epthru->reqsenselen = 14;
2127 epthru->cdblen = scp->cmd_len;
2129 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2131 if (scp->request_bufflen) {
2132 epthru->dataxferlen = scp->request_bufflen;
2133 epthru->dataxferaddr = ccb->sgl_dma_h;
2134 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2136 else {
2137 epthru->dataxferaddr = 0;
2138 epthru->dataxferlen = 0;
2139 epthru->numsge = 0;
2141 return;
2146 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2147 * @adapter - controller's soft state
2149 * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
2150 * completed command and put them on the completed list for later processing.
2152 * Returns: 1 if the interrupt is valid, 0 otherwise
2154 static inline int
2155 megaraid_ack_sequence(adapter_t *adapter)
2157 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2158 mbox_t *mbox;
2159 scb_t *scb;
2160 uint8_t nstatus;
2161 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2162 struct list_head clist;
2163 int handled;
2164 uint32_t dword;
2165 unsigned long flags;
2166 int i, j;
2169 mbox = raid_dev->mbox;
2171 // move the SCBs from the firmware completed array to our local list
2172 INIT_LIST_HEAD(&clist);
2174 // loop till F/W has more commands for us to complete
2175 handled = 0;
2176 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2177 do {
2179 * Check if a valid interrupt is pending. If found, force the
2180 * interrupt line low.
2182 dword = RDOUTDOOR(raid_dev);
2183 if (dword != 0x10001234) break;
2185 handled = 1;
2187 WROUTDOOR(raid_dev, 0x10001234);
2189 nstatus = 0;
2190 // wait for valid numstatus to post
2191 for (i = 0; i < 0xFFFFF; i++) {
2192 if (mbox->numstatus != 0xFF) {
2193 nstatus = mbox->numstatus;
2194 break;
2196 rmb();
2198 mbox->numstatus = 0xFF;
2200 adapter->outstanding_cmds -= nstatus;
2202 for (i = 0; i < nstatus; i++) {
2204 // wait for valid command index to post
2205 for (j = 0; j < 0xFFFFF; j++) {
2206 if (mbox->completed[i] != 0xFF) break;
2207 rmb();
2209 completed[i] = mbox->completed[i];
2210 mbox->completed[i] = 0xFF;
2212 if (completed[i] == 0xFF) {
2213 con_log(CL_ANN, (KERN_CRIT
2214 "megaraid: command posting timed out\n"));
2216 BUG();
2217 continue;
2220 // Get SCB associated with this command id
2221 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2222 // a cmm command
2223 scb = adapter->uscb_list + (completed[i] -
2224 MBOX_MAX_SCSI_CMDS);
2226 else {
2227 // an os command
2228 scb = adapter->kscb_list + completed[i];
2231 scb->status = mbox->status;
2232 list_add_tail(&scb->list, &clist);
2235 // Acknowledge interrupt
2236 WRINDOOR(raid_dev, 0x02);
2238 } while(1);
2240 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2243 // put the completed commands in the completed list. DPC would
2244 // complete these commands later
2245 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2247 list_splice(&clist, &adapter->completed_list);
2249 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2252 // schedule the DPC if there is some work for it
2253 if (handled)
2254 tasklet_schedule(&adapter->dpc_h);
2256 return handled;
2261 * megaraid_isr - isr for memory based mailbox based controllers
2262 * @irq - irq
2263 * @devp - pointer to our soft state
2264 * @regs - unused
2266 * Interrupt service routine for memory-mapped mailbox controllers.
2268 static irqreturn_t
2269 megaraid_isr(int irq, void *devp, struct pt_regs *regs)
2271 adapter_t *adapter = devp;
2272 int handled;
2274 handled = megaraid_ack_sequence(adapter);
2276 /* Loop through any pending requests */
2277 if (!adapter->quiescent) {
2278 megaraid_mbox_runpendq(adapter, NULL);
2281 return IRQ_RETVAL(handled);
2286 * megaraid_mbox_sync_scb - sync kernel buffers
2287 * @adapter : controller's soft state
2288 * @scb : pointer to the resource packet
2290 * DMA sync if required.
2292 static inline void
2293 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2295 mbox_ccb_t *ccb;
2297 ccb = (mbox_ccb_t *)scb->ccb;
2299 switch (scb->dma_type) {
2301 case MRAID_DMA_WBUF:
2302 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2303 pci_dma_sync_single_for_cpu(adapter->pdev,
2304 ccb->buf_dma_h,
2305 scb->scp->request_bufflen,
2306 PCI_DMA_FROMDEVICE);
2309 pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
2310 scb->scp->request_bufflen, scb->dma_direction);
2312 break;
2314 case MRAID_DMA_WSG:
2315 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2316 pci_dma_sync_sg_for_cpu(adapter->pdev,
2317 scb->scp->request_buffer,
2318 scb->scp->use_sg, PCI_DMA_FROMDEVICE);
2321 pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
2322 scb->scp->use_sg, scb->dma_direction);
2324 break;
2326 default:
2327 break;
2330 return;
2335 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2336 * @devp : pointer to HBA soft state
2338 * Pick up the commands from the completed list and send back to the owners.
2339 * This is a reentrant function and does not assume any locks are held while
2340 * it is being called.
2342 static void
2343 megaraid_mbox_dpc(unsigned long devp)
2345 adapter_t *adapter = (adapter_t *)devp;
2346 mraid_device_t *raid_dev;
2347 struct list_head clist;
2348 struct scatterlist *sgl;
2349 scb_t *scb;
2350 scb_t *tmp;
2351 struct scsi_cmnd *scp;
2352 mraid_passthru_t *pthru;
2353 mraid_epassthru_t *epthru;
2354 mbox_ccb_t *ccb;
2355 int islogical;
2356 int pdev_index;
2357 int pdev_state;
2358 mbox_t *mbox;
2359 unsigned long flags;
2360 uint8_t c;
2361 int status;
2364 if (!adapter) return;
2366 raid_dev = ADAP2RAIDDEV(adapter);
2368 // move the SCBs from the completed list to our local list
2369 INIT_LIST_HEAD(&clist);
2371 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2373 list_splice_init(&adapter->completed_list, &clist);
2375 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2378 list_for_each_entry_safe(scb, tmp, &clist, list) {
2380 status = scb->status;
2381 scp = scb->scp;
2382 ccb = (mbox_ccb_t *)scb->ccb;
2383 pthru = ccb->pthru;
2384 epthru = ccb->epthru;
2385 mbox = ccb->mbox;
2387 // Make sure f/w has completed a valid command
2388 if (scb->state != SCB_ISSUED) {
2389 con_log(CL_ANN, (KERN_CRIT
2390 "megaraid critical err: invalid command %d:%d:%p\n",
2391 scb->sno, scb->state, scp));
2392 BUG();
2393 continue; // Must never happen!
2396 // check for the management command and complete it right away
2397 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2398 scb->state = SCB_FREE;
2399 scb->status = status;
2401 // remove from local clist
2402 list_del_init(&scb->list);
2404 megaraid_mbox_mm_done(adapter, scb);
2406 continue;
2409 // Was an abort issued for this command earlier
2410 if (scb->state & SCB_ABORT) {
2411 con_log(CL_ANN, (KERN_NOTICE
2412 "megaraid: aborted cmd %lx[%x] completed\n",
2413 scp->serial_number, scb->sno));
2417 * If the inquiry came of a disk drive which is not part of
2418 * any RAID array, expose it to the kernel. For this to be
2419 * enabled, user must set the "megaraid_expose_unconf_disks"
2420 * flag to 1 by specifying it on module parameter list.
2421 * This would enable data migration off drives from other
2422 * configurations.
2424 islogical = MRAID_IS_LOGICAL(adapter, scp);
2425 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2426 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2428 if (scp->use_sg) {
2429 sgl = (struct scatterlist *)
2430 scp->request_buffer;
2432 if (sgl->page) {
2433 c = *(unsigned char *)
2434 (page_address((&sgl[0])->page) +
2435 (&sgl[0])->offset);
2437 else {
2438 con_log(CL_ANN, (KERN_WARNING
2439 "megaraid mailbox: invalid sg:%d\n",
2440 __LINE__));
2441 c = 0;
2444 else {
2445 c = *(uint8_t *)scp->request_buffer;
2448 if ((c & 0x1F ) == TYPE_DISK) {
2449 pdev_index = (scb->dev_channel * 16) +
2450 scb->dev_target;
2451 pdev_state =
2452 raid_dev->pdrv_state[pdev_index] & 0x0F;
2454 if (pdev_state == PDRV_ONLINE ||
2455 pdev_state == PDRV_FAILED ||
2456 pdev_state == PDRV_RBLD ||
2457 pdev_state == PDRV_HOTSPARE ||
2458 megaraid_expose_unconf_disks == 0) {
2460 status = 0xF0;
2465 // Convert MegaRAID status to Linux error code
2466 switch (status) {
2468 case 0x00:
2470 scp->result = (DID_OK << 16);
2471 break;
2473 case 0x02:
2475 /* set sense_buffer and result fields */
2476 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2477 mbox->cmd == MBOXCMD_PASSTHRU64) {
2479 memcpy(scp->sense_buffer, pthru->reqsensearea,
2480 14);
2482 scp->result = DRIVER_SENSE << 24 |
2483 DID_OK << 16 | CHECK_CONDITION << 1;
2485 else {
2486 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2488 memcpy(scp->sense_buffer,
2489 epthru->reqsensearea, 14);
2491 scp->result = DRIVER_SENSE << 24 |
2492 DID_OK << 16 |
2493 CHECK_CONDITION << 1;
2494 } else {
2495 scp->sense_buffer[0] = 0x70;
2496 scp->sense_buffer[2] = ABORTED_COMMAND;
2497 scp->result = CHECK_CONDITION << 1;
2500 break;
2502 case 0x08:
2504 scp->result = DID_BUS_BUSY << 16 | status;
2505 break;
2507 default:
2510 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2511 * failed
2513 if (scp->cmnd[0] == TEST_UNIT_READY) {
2514 scp->result = DID_ERROR << 16 |
2515 RESERVATION_CONFLICT << 1;
2517 else
2519 * Error code returned is 1 if Reserve or Release
2520 * failed or the input parameter is invalid
2522 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2523 scp->cmnd[0] == RELEASE)) {
2525 scp->result = DID_ERROR << 16 |
2526 RESERVATION_CONFLICT << 1;
2528 else {
2529 scp->result = DID_BAD_TARGET << 16 | status;
2533 // print a debug message for all failed commands
2534 if (status) {
2535 megaraid_mbox_display_scb(adapter, scb);
2538 // Free our internal resources and call the mid-layer callback
2539 // routine
2540 megaraid_mbox_sync_scb(adapter, scb);
2542 // remove from local clist
2543 list_del_init(&scb->list);
2545 // put back in free list
2546 megaraid_dealloc_scb(adapter, scb);
2548 // send the scsi packet back to kernel
2549 spin_lock(adapter->host_lock);
2550 scp->scsi_done(scp);
2551 spin_unlock(adapter->host_lock);
2554 return;
2559 * megaraid_abort_handler - abort the scsi command
2560 * @scp : command to be aborted
2562 * Abort a previous SCSI request. Only commands on the pending list can be
2563 * aborted. All the commands issued to the F/W must complete.
2565 static int
2566 __megaraid_abort_handler(struct scsi_cmnd *scp)
2568 adapter_t *adapter;
2569 mraid_device_t *raid_dev;
2570 scb_t *scb;
2571 scb_t *tmp;
2572 int found;
2573 unsigned long flags;
2574 int i;
2577 adapter = SCP2ADAPTER(scp);
2578 raid_dev = ADAP2RAIDDEV(adapter);
2580 assert_spin_locked(adapter->host_lock);
2582 con_log(CL_ANN, (KERN_WARNING
2583 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2584 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2585 SCP2TARGET(scp), SCP2LUN(scp)));
2587 // If FW has stopped responding, simply return failure
2588 if (raid_dev->hw_error) {
2589 con_log(CL_ANN, (KERN_NOTICE
2590 "megaraid: hw error, not aborting\n"));
2591 return FAILED;
2594 // There might a race here, where the command was completed by the
2595 // firmware and now it is on the completed list. Before we could
2596 // complete the command to the kernel in dpc, the abort came.
2597 // Find out if this is the case to avoid the race.
2598 scb = NULL;
2599 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2600 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2602 if (scb->scp == scp) { // Found command
2604 list_del_init(&scb->list); // from completed list
2606 con_log(CL_ANN, (KERN_WARNING
2607 "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2608 scp->serial_number, scb->sno,
2609 scb->dev_channel, scb->dev_target));
2611 scp->result = (DID_ABORT << 16);
2612 scp->scsi_done(scp);
2614 megaraid_dealloc_scb(adapter, scb);
2616 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2617 flags);
2619 return SUCCESS;
2622 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2625 // Find out if this command is still on the pending list. If it is and
2626 // was never issued, abort and return success. If the command is owned
2627 // by the firmware, we must wait for it to complete by the FW.
2628 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2629 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2631 if (scb->scp == scp) { // Found command
2633 list_del_init(&scb->list); // from pending list
2635 ASSERT(!(scb->state & SCB_ISSUED));
2637 con_log(CL_ANN, (KERN_WARNING
2638 "megaraid abort: %ld[%d:%d], driver owner\n",
2639 scp->serial_number, scb->dev_channel,
2640 scb->dev_target));
2642 scp->result = (DID_ABORT << 16);
2643 scp->scsi_done(scp);
2645 megaraid_dealloc_scb(adapter, scb);
2647 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2648 flags);
2650 return SUCCESS;
2653 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2656 // Check do we even own this command, in which case this would be
2657 // owned by the firmware. The only way to locate the FW scb is to
2658 // traverse through the list of all SCB, since driver does not
2659 // maintain these SCBs on any list
2660 found = 0;
2661 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2662 scb = adapter->kscb_list + i;
2664 if (scb->scp == scp) {
2666 found = 1;
2668 if (!(scb->state & SCB_ISSUED)) {
2669 con_log(CL_ANN, (KERN_WARNING
2670 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2671 scp->serial_number, scb->sno, scb->dev_channel,
2672 scb->dev_target));
2673 BUG();
2675 else {
2676 con_log(CL_ANN, (KERN_WARNING
2677 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2678 scp->serial_number, scb->sno, scb->dev_channel,
2679 scb->dev_target));
2684 if (!found) {
2685 con_log(CL_ANN, (KERN_WARNING
2686 "megaraid abort: scsi cmd:%ld, do now own\n",
2687 scp->serial_number));
2689 // FIXME: Should there be a callback for this command?
2690 return SUCCESS;
2693 // We cannot actually abort a command owned by firmware, return
2694 // failure and wait for reset. In host reset handler, we will find out
2695 // if the HBA is still live
2696 return FAILED;
2699 static int
2700 megaraid_abort_handler(struct scsi_cmnd *scp)
2702 adapter_t *adapter;
2703 int rc;
2705 adapter = SCP2ADAPTER(scp);
2707 spin_lock_irq(adapter->host_lock);
2708 rc = __megaraid_abort_handler(scp);
2709 spin_unlock_irq(adapter->host_lock);
2711 return rc;
2716 * megaraid_reset_handler - device reset hadler for mailbox based driver
2717 * @scp : reference command
2719 * Reset handler for the mailbox based controller. First try to find out if
2720 * the FW is still live, in which case the outstanding commands counter mut go
2721 * down to 0. If that happens, also issue the reservation reset command to
2722 * relinquish (possible) reservations on the logical drives connected to this
2723 * host
2725 static int
2726 __megaraid_reset_handler(struct scsi_cmnd *scp)
2728 adapter_t *adapter;
2729 scb_t *scb;
2730 scb_t *tmp;
2731 mraid_device_t *raid_dev;
2732 unsigned long flags;
2733 uint8_t raw_mbox[sizeof(mbox_t)];
2734 int rval;
2735 int recovery_window;
2736 int recovering;
2737 int i;
2739 adapter = SCP2ADAPTER(scp);
2740 raid_dev = ADAP2RAIDDEV(adapter);
2742 assert_spin_locked(adapter->host_lock);
2744 con_log(CL_ANN, (KERN_WARNING "megaraid: reseting the host...\n"));
2746 // return failure if adapter is not responding
2747 if (raid_dev->hw_error) {
2748 con_log(CL_ANN, (KERN_NOTICE
2749 "megaraid: hw error, cannot reset\n"));
2750 return FAILED;
2754 // Under exceptional conditions, FW can take up to 3 minutes to
2755 // complete command processing. Wait for additional 2 minutes for the
2756 // pending commands counter to go down to 0. If it doesn't, let the
2757 // controller be marked offline
2758 // Also, reset all the commands currently owned by the driver
2759 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2760 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2762 list_del_init(&scb->list); // from pending list
2764 con_log(CL_ANN, (KERN_WARNING
2765 "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2766 scp->serial_number, scb->sno,
2767 scb->dev_channel, scb->dev_target));
2769 scp->result = (DID_RESET << 16);
2770 scp->scsi_done(scp);
2772 megaraid_dealloc_scb(adapter, scb);
2774 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2776 if (adapter->outstanding_cmds) {
2777 con_log(CL_ANN, (KERN_NOTICE
2778 "megaraid: %d outstanding commands. Max wait %d sec\n",
2779 adapter->outstanding_cmds, MBOX_RESET_WAIT));
2782 spin_unlock(adapter->host_lock);
2784 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2786 recovering = adapter->outstanding_cmds;
2788 for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
2790 megaraid_ack_sequence(adapter);
2792 // print a message once every 5 seconds only
2793 if (!(i % 5)) {
2794 con_log(CL_ANN, (
2795 "megaraid mbox: Wait for %d commands to complete:%d\n",
2796 adapter->outstanding_cmds,
2797 MBOX_RESET_WAIT - i));
2800 // bailout if no recovery happended in reset time
2801 if ((i == MBOX_RESET_WAIT) &&
2802 (recovering == adapter->outstanding_cmds)) {
2803 break;
2806 msleep(1000);
2809 spin_lock(adapter->host_lock);
2811 // If still outstanding commands, bail out
2812 if (adapter->outstanding_cmds) {
2813 con_log(CL_ANN, (KERN_WARNING
2814 "megaraid mbox: critical hardware error!\n"));
2816 raid_dev->hw_error = 1;
2818 return FAILED;
2820 else {
2821 con_log(CL_ANN, (KERN_NOTICE
2822 "megaraid mbox: reset sequence completed sucessfully\n"));
2826 // If the controller supports clustering, reset reservations
2827 if (!adapter->ha) return SUCCESS;
2829 // clear reservations if any
2830 raw_mbox[0] = CLUSTER_CMD;
2831 raw_mbox[2] = RESET_RESERVATIONS;
2833 rval = SUCCESS;
2834 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2835 con_log(CL_ANN,
2836 (KERN_INFO "megaraid: reservation reset\n"));
2838 else {
2839 rval = FAILED;
2840 con_log(CL_ANN, (KERN_WARNING
2841 "megaraid: reservation reset failed\n"));
2844 return rval;
2847 static int
2848 megaraid_reset_handler(struct scsi_cmnd *cmd)
2850 int rc;
2852 spin_lock_irq(cmd->device->host->host_lock);
2853 rc = __megaraid_reset_handler(cmd);
2854 spin_unlock_irq(cmd->device->host->host_lock);
2856 return rc;
2861 * START: internal commands library
2863 * This section of the driver has the common routine used by the driver and
2864 * also has all the FW routines
2868 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2869 * @adapter - controller's soft state
2870 * @raw_mbox - the mailbox
2872 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2873 * controllers
2875 static int
2876 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2878 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2879 mbox64_t *mbox64;
2880 mbox_t *mbox;
2881 uint8_t status;
2882 int i;
2885 mbox64 = raid_dev->mbox64;
2886 mbox = raid_dev->mbox;
2889 * Wait until mailbox is free
2891 if (megaraid_busywait_mbox(raid_dev) != 0)
2892 goto blocked_mailbox;
2895 * Copy mailbox data into host structure
2897 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2898 mbox->cmdid = 0xFE;
2899 mbox->busy = 1;
2900 mbox->poll = 0;
2901 mbox->ack = 0;
2902 mbox->numstatus = 0xFF;
2903 mbox->status = 0xFF;
2905 wmb();
2906 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2908 // wait for maximum 1 second for status to post. If the status is not
2909 // available within 1 second, assume FW is initializing and wait
2910 // for an extended amount of time
2911 if (mbox->numstatus == 0xFF) { // status not yet available
2912 udelay(25);;
2914 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2915 rmb();
2916 msleep(1);
2920 if (i == 1000) {
2921 con_log(CL_ANN, (KERN_NOTICE
2922 "megaraid mailbox: wait for FW to boot "));
2924 for (i = 0; (mbox->numstatus == 0xFF) &&
2925 (i < MBOX_RESET_WAIT); i++) {
2926 rmb();
2927 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2928 MBOX_RESET_WAIT - i));
2929 msleep(1000);
2932 if (i == MBOX_RESET_WAIT) {
2934 con_log(CL_ANN, (
2935 "\nmegaraid mailbox: status not available\n"));
2937 return -1;
2939 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2943 // wait for maximum 1 second for poll semaphore
2944 if (mbox->poll != 0x77) {
2945 udelay(25);
2947 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2948 rmb();
2949 msleep(1);
2952 if (i == 1000) {
2953 con_log(CL_ANN, (KERN_WARNING
2954 "megaraid mailbox: could not get poll semaphore\n"));
2955 return -1;
2959 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2960 wmb();
2962 // wait for maximum 1 second for acknowledgement
2963 if (RDINDOOR(raid_dev) & 0x2) {
2964 udelay(25);
2966 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2967 rmb();
2968 msleep(1);
2971 if (i == 1000) {
2972 con_log(CL_ANN, (KERN_WARNING
2973 "megaraid mailbox: could not acknowledge\n"));
2974 return -1;
2977 mbox->poll = 0;
2978 mbox->ack = 0x77;
2980 status = mbox->status;
2982 // invalidate the completed command id array. After command
2983 // completion, firmware would write the valid id.
2984 mbox->numstatus = 0xFF;
2985 mbox->status = 0xFF;
2986 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2987 mbox->completed[i] = 0xFF;
2990 return status;
2992 blocked_mailbox:
2994 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2995 return -1;
3000 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
3001 * @adapter - controller's soft state
3002 * @raw_mbox - the mailbox
3004 * Issue a scb in synchronous and non-interrupt mode for mailbox based
3005 * controllers. This is a faster version of the synchronous command and
3006 * therefore can be called in interrupt-context as well
3008 static int
3009 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
3011 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3012 mbox_t *mbox;
3013 long i;
3016 mbox = raid_dev->mbox;
3018 // return immediately if the mailbox is busy
3019 if (mbox->busy) return -1;
3021 // Copy mailbox data into host structure
3022 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
3023 mbox->cmdid = 0xFE;
3024 mbox->busy = 1;
3025 mbox->poll = 0;
3026 mbox->ack = 0;
3027 mbox->numstatus = 0xFF;
3028 mbox->status = 0xFF;
3030 wmb();
3031 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3033 for (i = 0; i < 0xFFFFF; i++) {
3034 if (mbox->numstatus != 0xFF) break;
3037 if (i == 0xFFFFF) {
3038 // We may need to re-calibrate the counter
3039 con_log(CL_ANN, (KERN_CRIT
3040 "megaraid: fast sync command timed out\n"));
3043 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
3044 wmb();
3046 return mbox->status;
3051 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
3052 * @raid_dev - RAID device (HBA) soft state
3054 * wait until the controller's mailbox is available to accept more commands.
3055 * wait for at most 1 second
3057 static int
3058 megaraid_busywait_mbox(mraid_device_t *raid_dev)
3060 mbox_t *mbox = raid_dev->mbox;
3061 int i = 0;
3063 if (mbox->busy) {
3064 udelay(25);
3065 for (i = 0; mbox->busy && i < 1000; i++)
3066 msleep(1);
3069 if (i < 1000) return 0;
3070 else return -1;
3075 * megaraid_mbox_product_info - some static information about the controller
3076 * @adapter - our soft state
3078 * issue commands to the controller to grab some parameters required by our
3079 * caller.
3081 static int
3082 megaraid_mbox_product_info(adapter_t *adapter)
3084 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3085 mbox_t *mbox;
3086 uint8_t raw_mbox[sizeof(mbox_t)];
3087 mraid_pinfo_t *pinfo;
3088 dma_addr_t pinfo_dma_h;
3089 mraid_inquiry3_t *mraid_inq3;
3090 int i;
3093 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3094 mbox = (mbox_t *)raw_mbox;
3097 * Issue an ENQUIRY3 command to find out certain adapter parameters,
3098 * e.g., max channels, max commands etc.
3100 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3101 &pinfo_dma_h);
3103 if (pinfo == NULL) {
3104 con_log(CL_ANN, (KERN_WARNING
3105 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3106 __LINE__));
3108 return -1;
3110 memset(pinfo, 0, sizeof(mraid_pinfo_t));
3112 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3113 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3115 raw_mbox[0] = FC_NEW_CONFIG;
3116 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
3117 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3119 // Issue the command
3120 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3122 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3124 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3125 pinfo, pinfo_dma_h);
3127 return -1;
3131 * Collect information about state of each physical drive
3132 * attached to the controller. We will expose all the disks
3133 * which are not part of RAID
3135 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3136 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3137 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3141 * Get product info for information like number of channels,
3142 * maximum commands supported.
3144 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3145 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3147 raw_mbox[0] = FC_NEW_CONFIG;
3148 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3150 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3152 con_log(CL_ANN, (KERN_WARNING
3153 "megaraid: product info failed\n"));
3155 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3156 pinfo, pinfo_dma_h);
3158 return -1;
3162 * Setup some parameters for host, as required by our caller
3164 adapter->max_channel = pinfo->nchannels;
3167 * we will export all the logical drives on a single channel.
3168 * Add 1 since inquires do not come for inititor ID
3170 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3171 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3174 * These are the maximum outstanding commands for the scsi-layer
3176 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3178 memset(adapter->fw_version, 0, VERSION_SIZE);
3179 memset(adapter->bios_version, 0, VERSION_SIZE);
3181 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3182 adapter->fw_version[4] = 0;
3184 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3185 adapter->bios_version[4] = 0;
3187 con_log(CL_ANN, (KERN_NOTICE
3188 "megaraid: fw version:[%s] bios version:[%s]\n",
3189 adapter->fw_version, adapter->bios_version));
3191 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3192 pinfo_dma_h);
3194 return 0;
3200 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3201 * @adapter - soft state for the controller
3203 * this routine check whether the controller in question supports extended
3204 * ( > 10 bytes ) CDBs
3206 static int
3207 megaraid_mbox_extended_cdb(adapter_t *adapter)
3209 mbox_t *mbox;
3210 uint8_t raw_mbox[sizeof(mbox_t)];
3211 int rval;
3213 mbox = (mbox_t *)raw_mbox;
3215 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3216 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3218 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3220 raw_mbox[0] = MAIN_MISC_OPCODE;
3221 raw_mbox[2] = SUPPORT_EXT_CDB;
3224 * Issue the command
3226 rval = 0;
3227 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3228 rval = -1;
3231 return rval;
3236 * megaraid_mbox_support_ha - Do we support clustering
3237 * @adapter - soft state for the controller
3238 * @init_id - ID of the initiator
3240 * Determine if the firmware supports clustering and the ID of the initiator.
3242 static int
3243 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3245 mbox_t *mbox;
3246 uint8_t raw_mbox[sizeof(mbox_t)];
3247 int rval;
3250 mbox = (mbox_t *)raw_mbox;
3252 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3254 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3256 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3258 raw_mbox[0] = GET_TARGET_ID;
3260 // Issue the command
3261 *init_id = 7;
3262 rval = -1;
3263 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3265 *init_id = *(uint8_t *)adapter->ibuf;
3267 con_log(CL_ANN, (KERN_INFO
3268 "megaraid: cluster firmware, initiator ID: %d\n",
3269 *init_id));
3271 rval = 0;
3274 return rval;
3279 * megaraid_mbox_support_random_del - Do we support random deletion
3280 * @adapter - soft state for the controller
3282 * Determine if the firmware supports random deletion
3283 * Return: 1 is operation supported, 0 otherwise
3285 static int
3286 megaraid_mbox_support_random_del(adapter_t *adapter)
3288 mbox_t *mbox;
3289 uint8_t raw_mbox[sizeof(mbox_t)];
3290 int rval;
3293 mbox = (mbox_t *)raw_mbox;
3295 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3297 raw_mbox[0] = FC_DEL_LOGDRV;
3298 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3300 // Issue the command
3301 rval = 0;
3302 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3304 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3306 rval = 1;
3309 return rval;
3314 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3315 * @adapter - soft state for the controller
3317 * Find out the maximum number of scatter-gather elements supported by the
3318 * firmware
3320 static int
3321 megaraid_mbox_get_max_sg(adapter_t *adapter)
3323 mbox_t *mbox;
3324 uint8_t raw_mbox[sizeof(mbox_t)];
3325 int nsg;
3328 mbox = (mbox_t *)raw_mbox;
3330 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3332 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3334 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3336 raw_mbox[0] = MAIN_MISC_OPCODE;
3337 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3339 // Issue the command
3340 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3341 nsg = *(uint8_t *)adapter->ibuf;
3343 else {
3344 nsg = MBOX_DEFAULT_SG_SIZE;
3347 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3349 return nsg;
3354 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3355 * @adapter - soft state for the controller
3357 * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
3358 * can be exported as regular SCSI channels
3360 static void
3361 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3363 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3364 mbox_t *mbox;
3365 uint8_t raw_mbox[sizeof(mbox_t)];
3368 mbox = (mbox_t *)raw_mbox;
3370 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3372 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3374 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3376 raw_mbox[0] = CHNL_CLASS;
3377 raw_mbox[2] = GET_CHNL_CLASS;
3379 // Issue the command. If the command fails, all channels are RAID
3380 // channels
3381 raid_dev->channel_class = 0xFF;
3382 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3383 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3386 return;
3391 * megaraid_mbox_flush_cache - flush adapter and disks cache
3392 * @param adapter : soft state for the controller
3394 * Flush adapter cache followed by disks cache
3396 static void
3397 megaraid_mbox_flush_cache(adapter_t *adapter)
3399 mbox_t *mbox;
3400 uint8_t raw_mbox[sizeof(mbox_t)];
3403 mbox = (mbox_t *)raw_mbox;
3405 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3407 raw_mbox[0] = FLUSH_ADAPTER;
3409 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3410 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3413 raw_mbox[0] = FLUSH_SYSTEM;
3415 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3416 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3419 return;
3424 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3425 * @param adapter : controllers' soft state
3426 * @param scb : SCB to be displayed
3427 * @param level : debug level for console print
3429 * Diplay information about the given SCB iff the current debug level is
3430 * verbose
3432 static void
3433 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3435 mbox_ccb_t *ccb;
3436 struct scsi_cmnd *scp;
3437 mbox_t *mbox;
3438 int level;
3439 int i;
3442 ccb = (mbox_ccb_t *)scb->ccb;
3443 scp = scb->scp;
3444 mbox = ccb->mbox;
3446 level = CL_DLEVEL3;
3448 con_log(level, (KERN_NOTICE
3449 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3450 mbox->cmd, scb->sno));
3452 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3453 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3454 mbox->numsge));
3456 if (!scp) return;
3458 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3460 for (i = 0; i < scp->cmd_len; i++) {
3461 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3464 con_log(level, ("\n"));
3466 return;
3471 * megaraid_mbox_setup_device_map - manage device ids
3472 * @adapter : Driver's soft state
3474 * Manange the device ids to have an appropraite mapping between the kernel
3475 * scsi addresses and megaraid scsi and logical drive addresses. We export
3476 * scsi devices on their actual addresses, whereas the logical drives are
3477 * exported on a virtual scsi channel.
3479 static void
3480 megaraid_mbox_setup_device_map(adapter_t *adapter)
3482 uint8_t c;
3483 uint8_t t;
3486 * First fill the values on the logical drive channel
3488 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3489 adapter->device_ids[adapter->max_channel][t] =
3490 (t < adapter->init_id) ? t : t - 1;
3492 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3495 * Fill the values on the physical devices channels
3497 for (c = 0; c < adapter->max_channel; c++)
3498 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3499 adapter->device_ids[c][t] = (c << 8) | t;
3504 * END: internal commands library
3508 * START: Interface for the common management module
3510 * This is the module, which interfaces with the common mangement module to
3511 * provide support for ioctl and sysfs
3515 * megaraid_cmm_register - register with the mangement module
3516 * @param adapter : HBA soft state
3518 * Register with the management module, which allows applications to issue
3519 * ioctl calls to the drivers. This interface is used by the management module
3520 * to setup sysfs support as well.
3522 static int
3523 megaraid_cmm_register(adapter_t *adapter)
3525 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3526 mraid_mmadp_t adp;
3527 scb_t *scb;
3528 mbox_ccb_t *ccb;
3529 int rval;
3530 int i;
3532 // Allocate memory for the base list of scb for management module.
3533 adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
3534 GFP_KERNEL);
3536 if (adapter->uscb_list == NULL) {
3537 con_log(CL_ANN, (KERN_WARNING
3538 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3539 __LINE__));
3540 return -1;
3542 memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
3545 // Initialize the synchronization parameters for resources for
3546 // commands for management module
3547 INIT_LIST_HEAD(&adapter->uscb_pool);
3549 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3553 // link all the packets. Note, CCB for commands, coming from the
3554 // commom management module, mailbox physical address are already
3555 // setup by it. We just need placeholder for that in our local command
3556 // control blocks
3557 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3559 scb = adapter->uscb_list + i;
3560 ccb = raid_dev->uccb_list + i;
3562 scb->ccb = (caddr_t)ccb;
3563 ccb->mbox64 = raid_dev->umbox64 + i;
3564 ccb->mbox = &ccb->mbox64->mbox32;
3565 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3567 scb->gp = 0;
3569 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3570 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3571 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3573 scb->scp = NULL;
3574 scb->state = SCB_FREE;
3575 scb->dma_direction = PCI_DMA_NONE;
3576 scb->dma_type = MRAID_DMA_NONE;
3577 scb->dev_channel = -1;
3578 scb->dev_target = -1;
3580 // put scb in the free pool
3581 list_add_tail(&scb->list, &adapter->uscb_pool);
3584 adp.unique_id = adapter->unique_id;
3585 adp.drvr_type = DRVRTYPE_MBOX;
3586 adp.drvr_data = (unsigned long)adapter;
3587 adp.pdev = adapter->pdev;
3588 adp.issue_uioc = megaraid_mbox_mm_handler;
3589 adp.timeout = 300;
3590 adp.max_kioc = MBOX_MAX_USER_CMDS;
3592 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3594 con_log(CL_ANN, (KERN_WARNING
3595 "megaraid mbox: did not register with CMM\n"));
3597 kfree(adapter->uscb_list);
3600 return rval;
3605 * megaraid_cmm_unregister - un-register with the mangement module
3606 * @param adapter : HBA soft state
3608 * Un-register with the management module.
3609 * FIXME: mgmt module must return failure for unregister if it has pending
3610 * commands in LLD
3612 static int
3613 megaraid_cmm_unregister(adapter_t *adapter)
3615 kfree(adapter->uscb_list);
3616 mraid_mm_unregister_adp(adapter->unique_id);
3617 return 0;
3622 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3623 * @param drvr_data : LLD specific data
3624 * @param kioc : CMM interface packet
3625 * @param action : command action
3627 * This routine is invoked whenever the Common Mangement Module (CMM) has a
3628 * command for us. The 'action' parameter specifies if this is a new command
3629 * or otherwise.
3631 static int
3632 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3634 adapter_t *adapter;
3636 if (action != IOCTL_ISSUE) {
3637 con_log(CL_ANN, (KERN_WARNING
3638 "megaraid: unsupported management action:%#2x\n",
3639 action));
3640 return (-ENOTSUPP);
3643 adapter = (adapter_t *)drvr_data;
3645 // make sure this adapter is not being detached right now.
3646 if (atomic_read(&adapter->being_detached)) {
3647 con_log(CL_ANN, (KERN_WARNING
3648 "megaraid: reject management request, detaching\n"));
3649 return (-ENODEV);
3652 switch (kioc->opcode) {
3654 case GET_ADAP_INFO:
3656 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3657 (unsigned long)kioc->buf_vaddr);
3659 kioc->done(kioc);
3661 return kioc->status;
3663 case MBOX_CMD:
3665 return megaraid_mbox_mm_command(adapter, kioc);
3667 default:
3668 kioc->status = (-EINVAL);
3669 kioc->done(kioc);
3670 return (-EINVAL);
3673 return 0; // not reached
3677 * megaraid_mbox_mm_command - issues commands routed through CMM
3678 * @param adapter : HBA soft state
3679 * @param kioc : management command packet
3681 * Issues commands, which are routed through the management module.
3683 static int
3684 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3686 struct list_head *head = &adapter->uscb_pool;
3687 mbox64_t *mbox64;
3688 uint8_t *raw_mbox;
3689 scb_t *scb;
3690 mbox_ccb_t *ccb;
3691 unsigned long flags;
3693 // detach one scb from free pool
3694 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3696 if (list_empty(head)) { // should never happen because of CMM
3698 con_log(CL_ANN, (KERN_WARNING
3699 "megaraid mbox: bug in cmm handler, lost resources\n"));
3701 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3703 return (-EINVAL);
3706 scb = list_entry(head->next, scb_t, list);
3707 list_del_init(&scb->list);
3709 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3711 scb->state = SCB_ACTIVE;
3712 scb->dma_type = MRAID_DMA_NONE;
3713 scb->dma_direction = PCI_DMA_NONE;
3715 ccb = (mbox_ccb_t *)scb->ccb;
3716 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3717 raw_mbox = (uint8_t *)&mbox64->mbox32;
3719 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3721 scb->gp = (unsigned long)kioc;
3724 * If it is a logdrv random delete operation, we have to wait till
3725 * there are no outstanding cmds at the fw and then issue it directly
3727 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3729 if (wait_till_fw_empty(adapter)) {
3730 con_log(CL_ANN, (KERN_NOTICE
3731 "megaraid mbox: LD delete, timed out\n"));
3733 kioc->status = -ETIME;
3735 scb->status = -1;
3737 megaraid_mbox_mm_done(adapter, scb);
3739 return (-ETIME);
3742 INIT_LIST_HEAD(&scb->list);
3744 scb->state = SCB_ISSUED;
3745 if (mbox_post_cmd(adapter, scb) != 0) {
3747 con_log(CL_ANN, (KERN_NOTICE
3748 "megaraid mbox: LD delete, mailbox busy\n"));
3750 kioc->status = -EBUSY;
3752 scb->status = -1;
3754 megaraid_mbox_mm_done(adapter, scb);
3756 return (-EBUSY);
3759 return 0;
3762 // put the command on the pending list and execute
3763 megaraid_mbox_runpendq(adapter, scb);
3765 return 0;
3769 static int
3770 wait_till_fw_empty(adapter_t *adapter)
3772 unsigned long flags = 0;
3773 int i;
3777 * Set the quiescent flag to stop issuing cmds to FW.
3779 spin_lock_irqsave(adapter->host_lock, flags);
3780 adapter->quiescent++;
3781 spin_unlock_irqrestore(adapter->host_lock, flags);
3784 * Wait till there are no more cmds outstanding at FW. Try for at most
3785 * 60 seconds
3787 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3788 con_log(CL_DLEVEL1, (KERN_INFO
3789 "megaraid: FW has %d pending commands\n",
3790 adapter->outstanding_cmds));
3792 msleep(1000);
3795 return adapter->outstanding_cmds;
3800 * megaraid_mbox_mm_done - callback for CMM commands
3801 * @adapter : HBA soft state
3802 * @scb : completed command
3804 * Callback routine for internal commands originated from the management
3805 * module.
3807 static void
3808 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3810 uioc_t *kioc;
3811 mbox64_t *mbox64;
3812 uint8_t *raw_mbox;
3813 unsigned long flags;
3815 kioc = (uioc_t *)scb->gp;
3816 kioc->status = 0;
3817 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3818 mbox64->mbox32.status = scb->status;
3819 raw_mbox = (uint8_t *)&mbox64->mbox32;
3822 // put scb in the free pool
3823 scb->state = SCB_FREE;
3824 scb->scp = NULL;
3826 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3828 list_add(&scb->list, &adapter->uscb_pool);
3830 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3832 // if a delete logical drive operation succeeded, restart the
3833 // controller
3834 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3836 adapter->quiescent--;
3838 megaraid_mbox_runpendq(adapter, NULL);
3841 kioc->done(kioc);
3843 return;
3848 * gather_hbainfo - HBA characteristics for the applications
3849 * @param adapter : HBA soft state
3850 * @param hinfo : pointer to the caller's host info strucuture
3852 static int
3853 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3855 uint8_t dmajor;
3857 dmajor = megaraid_mbox_version[0];
3859 hinfo->pci_vendor_id = adapter->pdev->vendor;
3860 hinfo->pci_device_id = adapter->pdev->device;
3861 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3862 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3864 hinfo->pci_bus = adapter->pdev->bus->number;
3865 hinfo->pci_dev_fn = adapter->pdev->devfn;
3866 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3867 hinfo->irq = adapter->host->irq;
3868 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3870 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3871 hinfo->host_no = adapter->host->host_no;
3873 return 0;
3877 * END: Interface for the common management module
3883 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3885 * Allocate packets required to issue FW calls whenever the sysfs attributes
3886 * are read. These attributes would require up-to-date information from the
3887 * FW. Also set up resources for mutual exclusion to share these resources and
3888 * the wait queue.
3890 * @param adapter : controller's soft state
3892 * @return 0 on success
3893 * @return -ERROR_CODE on failure
3895 static int
3896 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3898 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3899 int rval = 0;
3901 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3903 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3905 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3906 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3908 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3909 !raid_dev->sysfs_buffer) {
3911 con_log(CL_ANN, (KERN_WARNING
3912 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3913 __LINE__));
3915 rval = -ENOMEM;
3917 megaraid_sysfs_free_resources(adapter);
3920 sema_init(&raid_dev->sysfs_sem, 1);
3922 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3924 return rval;
3929 * megaraid_sysfs_free_resources - free sysfs related resources
3931 * Free packets allocated for sysfs FW commands
3933 * @param adapter : controller's soft state
3935 static void
3936 megaraid_sysfs_free_resources(adapter_t *adapter)
3938 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3940 if (raid_dev->sysfs_uioc) kfree(raid_dev->sysfs_uioc);
3942 if (raid_dev->sysfs_mbox64) kfree(raid_dev->sysfs_mbox64);
3944 if (raid_dev->sysfs_buffer) {
3945 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3946 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3952 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3954 * Callback routine called in the ISR/tasklet context for get ldmap call
3956 * @param uioc : completed packet
3958 static void
3959 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3961 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3962 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3964 uioc->status = 0;
3966 wake_up(&raid_dev->sysfs_wait_q);
3971 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3973 * Timeout routine to recover and return to application, in case the adapter
3974 * has stopped responding. A timeout of 60 seconds for this command seem like
3975 * a good value
3977 * @param uioc : timed out packet
3979 static void
3980 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
3982 uioc_t *uioc = (uioc_t *)data;
3983 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3984 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3986 uioc->status = -ETIME;
3988 wake_up(&raid_dev->sysfs_wait_q);
3993 * megaraid_sysfs_get_ldmap - get update logical drive map
3995 * This routine will be called whenever user reads the logical drive
3996 * attributes, go get the current logical drive mapping table from the
3997 * firmware. We use the managment API's to issue commands to the controller.
3999 * NOTE: The commands issuance functionality is not generalized and
4000 * implemented in context of "get ld map" command only. If required, the
4001 * command issuance logical can be trivially pulled out and implemented as a
4002 * standalone libary. For now, this should suffice since there is no other
4003 * user of this interface.
4005 * @param adapter : controller's soft state
4007 * @return 0 on success
4008 * @return -1 on failure
4010 static int
4011 megaraid_sysfs_get_ldmap(adapter_t *adapter)
4013 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4014 uioc_t *uioc;
4015 mbox64_t *mbox64;
4016 mbox_t *mbox;
4017 char *raw_mbox;
4018 struct timer_list sysfs_timer;
4019 struct timer_list *timerp;
4020 caddr_t ldmap;
4021 int rval = 0;
4024 * Allow only one read at a time to go through the sysfs attributes
4026 down(&raid_dev->sysfs_sem);
4028 uioc = raid_dev->sysfs_uioc;
4029 mbox64 = raid_dev->sysfs_mbox64;
4030 ldmap = raid_dev->sysfs_buffer;
4032 memset(uioc, 0, sizeof(uioc_t));
4033 memset(mbox64, 0, sizeof(mbox64_t));
4034 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
4036 mbox = &mbox64->mbox32;
4037 raw_mbox = (char *)mbox;
4038 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
4039 uioc->buf_vaddr = (caddr_t)adapter;
4040 uioc->status = -ENODATA;
4041 uioc->done = megaraid_sysfs_get_ldmap_done;
4044 * Prepare the mailbox packet to get the current logical drive mapping
4045 * table
4047 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
4049 raw_mbox[0] = FC_DEL_LOGDRV;
4050 raw_mbox[2] = OP_GET_LDID_MAP;
4053 * Setup a timer to recover from a non-responding controller
4055 timerp = &sysfs_timer;
4056 init_timer(timerp);
4058 timerp->function = megaraid_sysfs_get_ldmap_timeout;
4059 timerp->data = (unsigned long)uioc;
4060 timerp->expires = jiffies + 60 * HZ;
4062 add_timer(timerp);
4065 * Send the command to the firmware
4067 rval = megaraid_mbox_mm_command(adapter, uioc);
4069 if (rval == 0) { // command successfully issued
4070 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4073 * Check if the command timed out
4075 if (uioc->status == -ETIME) {
4076 con_log(CL_ANN, (KERN_NOTICE
4077 "megaraid: sysfs get ld map timed out\n"));
4079 rval = -ETIME;
4081 else {
4082 rval = mbox->status;
4085 if (rval == 0) {
4086 memcpy(raid_dev->curr_ldmap, ldmap,
4087 sizeof(raid_dev->curr_ldmap));
4089 else {
4090 con_log(CL_ANN, (KERN_NOTICE
4091 "megaraid: get ld map failed with %x\n", rval));
4094 else {
4095 con_log(CL_ANN, (KERN_NOTICE
4096 "megaraid: could not issue ldmap command:%x\n", rval));
4100 del_timer_sync(timerp);
4102 up(&raid_dev->sysfs_sem);
4104 return rval;
4109 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4111 * Display the handle used by the applications while executing management
4112 * tasks on the adapter. We invoke a management module API to get the adapter
4113 * handle, since we do not interface with applications directly.
4115 * @param cdev : class device object representation for the host
4116 * @param buf : buffer to send data to
4118 static ssize_t
4119 megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
4121 struct Scsi_Host *shost = class_to_shost(cdev);
4122 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4123 uint32_t app_hndl;
4125 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4127 return snprintf(buf, 8, "%u\n", app_hndl);
4132 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4134 * Display the logical drive number for the device in question, if it a valid
4135 * logical drive. For physical devices, "-1" is returned
4136 * The logical drive number is displayed in following format
4138 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4139 * <int> <int> <int> <int>
4141 * @param dev : device object representation for the scsi device
4142 * @param buf : buffer to send data to
4144 static ssize_t
4145 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4147 struct scsi_device *sdev = to_scsi_device(dev);
4148 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4149 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4150 int scsi_id = -1;
4151 int logical_drv = -1;
4152 int ldid_map = -1;
4153 uint32_t app_hndl = 0;
4154 int mapped_sdev_id;
4155 int rval;
4156 int i;
4158 if (raid_dev->random_del_supported &&
4159 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4161 rval = megaraid_sysfs_get_ldmap(adapter);
4162 if (rval == 0) {
4164 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4166 mapped_sdev_id = sdev->id;
4168 if (sdev->id > adapter->init_id) {
4169 mapped_sdev_id -= 1;
4172 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4174 scsi_id = sdev->id;
4176 logical_drv = i;
4178 ldid_map = raid_dev->curr_ldmap[i];
4180 app_hndl = mraid_mm_adapter_app_handle(
4181 adapter->unique_id);
4183 break;
4187 else {
4188 con_log(CL_ANN, (KERN_NOTICE
4189 "megaraid: sysfs get ld map failed: %x\n",
4190 rval));
4194 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4195 ldid_map, app_hndl);
4200 * END: Mailbox Low Level Driver
4202 module_init(megaraid_init);
4203 module_exit(megaraid_exit);
4205 /* vim: set ts=8 sw=8 tw=78 ai si: */