Pull 5571 into release branch
[linux-2.6/verdex.git] / drivers / scsi / megaraid / megaraid_mbox.c
blob4b5d420d2f4d36c017e01a3541d7d277c890f67b
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);
537 // Initialize the command queues: the list of free SCBs and the list
538 // of pending SCBs.
539 INIT_LIST_HEAD(&adapter->kscb_pool);
540 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
542 INIT_LIST_HEAD(&adapter->pend_list);
543 spin_lock_init(PENDING_LIST_LOCK(adapter));
545 INIT_LIST_HEAD(&adapter->completed_list);
546 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
549 // Start the mailbox based controller
550 if (megaraid_init_mbox(adapter) != 0) {
551 con_log(CL_ANN, (KERN_WARNING
552 "megaraid: maibox adapter did not initialize\n"));
554 goto out_free_adapter;
557 // Register with LSI Common Management Module
558 if (megaraid_cmm_register(adapter) != 0) {
560 con_log(CL_ANN, (KERN_WARNING
561 "megaraid: could not register with management module\n"));
563 goto out_fini_mbox;
566 // setup adapter handle in PCI soft state
567 pci_set_drvdata(pdev, adapter);
569 // attach with scsi mid-layer
570 if (megaraid_io_attach(adapter) != 0) {
572 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
574 goto out_cmm_unreg;
577 return 0;
579 out_cmm_unreg:
580 pci_set_drvdata(pdev, NULL);
581 megaraid_cmm_unregister(adapter);
582 out_fini_mbox:
583 megaraid_fini_mbox(adapter);
584 out_free_adapter:
585 kfree(adapter);
586 out_probe_one:
587 pci_disable_device(pdev);
589 return -ENODEV;
594 * megaraid_detach_one - release the framework resources and call LLD release
595 * routine
596 * @param pdev : handle for our PCI cofiguration space
598 * This routine is called during driver unload. We free all the allocated
599 * resources and call the corresponding LLD so that it can also release all
600 * its resources.
602 * This routine is also called from the PCI hotplug system
604 static void
605 megaraid_detach_one(struct pci_dev *pdev)
607 adapter_t *adapter;
608 struct Scsi_Host *host;
611 // Start a rollback on this adapter
612 adapter = pci_get_drvdata(pdev);
614 if (!adapter) {
615 con_log(CL_ANN, (KERN_CRIT
616 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
617 pdev->vendor, pdev->device, pdev->subsystem_vendor,
618 pdev->subsystem_device));
620 return;
622 else {
623 con_log(CL_ANN, (KERN_NOTICE
624 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
625 pdev->vendor, pdev->device, pdev->subsystem_vendor,
626 pdev->subsystem_device));
630 host = adapter->host;
632 // do not allow any more requests from the management module for this
633 // adapter.
634 // FIXME: How do we account for the request which might still be
635 // pending with us?
636 atomic_set(&adapter->being_detached, 1);
638 // detach from the IO sub-system
639 megaraid_io_detach(adapter);
641 // reset the device state in the PCI structure. We check this
642 // condition when we enter here. If the device state is NULL,
643 // that would mean the device has already been removed
644 pci_set_drvdata(pdev, NULL);
646 // Unregister from common management module
648 // FIXME: this must return success or failure for conditions if there
649 // is a command pending with LLD or not.
650 megaraid_cmm_unregister(adapter);
652 // finalize the mailbox based controller and release all resources
653 megaraid_fini_mbox(adapter);
655 kfree(adapter);
657 scsi_host_put(host);
659 pci_disable_device(pdev);
661 return;
666 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
667 * @param device : generice driver model device
669 * Shutdown notification, perform flush cache
671 static void
672 megaraid_mbox_shutdown(struct pci_dev *pdev)
674 adapter_t *adapter = pci_get_drvdata(pdev);
675 static int counter;
677 if (!adapter) {
678 con_log(CL_ANN, (KERN_WARNING
679 "megaraid: null device in shutdown\n"));
680 return;
683 // flush caches now
684 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
685 counter++));
687 megaraid_mbox_flush_cache(adapter);
689 con_log(CL_ANN, ("done\n"));
694 * megaraid_io_attach - attach a device with the IO subsystem
695 * @param adapter : controller's soft state
697 * Attach this device with the IO subsystem
699 static int
700 megaraid_io_attach(adapter_t *adapter)
702 struct Scsi_Host *host;
704 // Initialize SCSI Host structure
705 host = scsi_host_alloc(&megaraid_template_g, 8);
706 if (!host) {
707 con_log(CL_ANN, (KERN_WARNING
708 "megaraid mbox: scsi_register failed\n"));
710 return -1;
713 SCSIHOST2ADAP(host) = (caddr_t)adapter;
714 adapter->host = host;
716 host->irq = adapter->irq;
717 host->unique_id = adapter->unique_id;
718 host->can_queue = adapter->max_cmds;
719 host->this_id = adapter->init_id;
720 host->sg_tablesize = adapter->sglen;
721 host->max_sectors = adapter->max_sectors;
722 host->cmd_per_lun = adapter->cmd_per_lun;
723 host->max_channel = adapter->max_channel;
724 host->max_id = adapter->max_target;
725 host->max_lun = adapter->max_lun;
728 // notify mid-layer about the new controller
729 if (scsi_add_host(host, &adapter->pdev->dev)) {
731 con_log(CL_ANN, (KERN_WARNING
732 "megaraid mbox: scsi_add_host failed\n"));
734 scsi_host_put(host);
736 return -1;
739 scsi_scan_host(host);
741 return 0;
746 * megaraid_io_detach - detach a device from the IO subsystem
747 * @param adapter : controller's soft state
749 * Detach this device from the IO subsystem
751 static void
752 megaraid_io_detach(adapter_t *adapter)
754 struct Scsi_Host *host;
756 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
758 host = adapter->host;
760 scsi_remove_host(host);
762 return;
767 * START: Mailbox Low Level Driver
769 * This is section specific to the single mailbox based controllers
773 * megaraid_init_mbox - initialize controller
774 * @param adapter - our soft state
776 * . Allocate 16-byte aligned mailbox memory for firmware handshake
777 * . Allocate controller's memory resources
778 * . Find out all initialization data
779 * . Allocate memory required for all the commands
780 * . Use internal library of FW routines, build up complete soft state
782 static int __init
783 megaraid_init_mbox(adapter_t *adapter)
785 struct pci_dev *pdev;
786 mraid_device_t *raid_dev;
787 int i;
790 adapter->ito = MBOX_TIMEOUT;
791 pdev = adapter->pdev;
794 * Allocate and initialize the init data structure for mailbox
795 * controllers
797 raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
798 if (raid_dev == NULL) return -1;
800 memset(raid_dev, 0, sizeof(mraid_device_t));
803 * Attach the adapter soft state to raid device soft state
805 adapter->raid_device = (caddr_t)raid_dev;
806 raid_dev->fast_load = megaraid_fast_load;
809 // our baseport
810 raid_dev->baseport = pci_resource_start(pdev, 0);
812 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
814 con_log(CL_ANN, (KERN_WARNING
815 "megaraid: mem region busy\n"));
817 goto out_free_raid_dev;
820 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
822 if (!raid_dev->baseaddr) {
824 con_log(CL_ANN, (KERN_WARNING
825 "megaraid: could not map hba memory\n") );
827 goto out_release_regions;
831 // Setup the rest of the soft state using the library of FW routines
834 // request IRQ and register the interrupt service routine
835 if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
836 adapter)) {
838 con_log(CL_ANN, (KERN_WARNING
839 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
841 goto out_iounmap;
845 // initialize the mutual exclusion lock for the mailbox
846 spin_lock_init(&raid_dev->mailbox_lock);
848 // allocate memory required for commands
849 if (megaraid_alloc_cmd_packets(adapter) != 0) {
850 goto out_free_irq;
853 // Product info
854 if (megaraid_mbox_product_info(adapter) != 0) {
855 goto out_alloc_cmds;
858 // Do we support extended CDBs
859 adapter->max_cdb_sz = 10;
860 if (megaraid_mbox_extended_cdb(adapter) == 0) {
861 adapter->max_cdb_sz = 16;
865 * Do we support cluster environment, if we do, what is the initiator
866 * id.
867 * NOTE: In a non-cluster aware firmware environment, the LLD should
868 * return 7 as initiator id.
870 adapter->ha = 0;
871 adapter->init_id = -1;
872 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
873 adapter->ha = 1;
877 * Prepare the device ids array to have the mapping between the kernel
878 * device address and megaraid device address.
879 * We export the physical devices on their actual addresses. The
880 * logical drives are exported on a virtual SCSI channel
882 megaraid_mbox_setup_device_map(adapter);
884 // If the firmware supports random deletion, update the device id map
885 if (megaraid_mbox_support_random_del(adapter)) {
887 // Change the logical drives numbers in device_ids array one
888 // slot in device_ids is reserved for target id, that's why
889 // "<=" below
890 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
891 adapter->device_ids[adapter->max_channel][i] += 0x80;
893 adapter->device_ids[adapter->max_channel][adapter->init_id] =
894 0xFF;
896 raid_dev->random_del_supported = 1;
900 * find out the maximum number of scatter-gather elements supported by
901 * this firmware
903 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
905 // enumerate RAID and SCSI channels so that all devices on SCSI
906 // channels can later be exported, including disk devices
907 megaraid_mbox_enum_raid_scsi(adapter);
910 * Other parameters required by upper layer
912 * maximum number of sectors per IO command
914 adapter->max_sectors = megaraid_max_sectors;
917 * number of queued commands per LUN.
919 adapter->cmd_per_lun = megaraid_cmd_per_lun;
922 * Allocate resources required to issue FW calls, when sysfs is
923 * accessed
925 if (megaraid_sysfs_alloc_resources(adapter) != 0) {
926 goto out_alloc_cmds;
929 // Set the DMA mask to 64-bit. All supported controllers as capable of
930 // DMA in this range
931 if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
933 con_log(CL_ANN, (KERN_WARNING
934 "megaraid: could not set DMA mask for 64-bit.\n"));
936 goto out_free_sysfs_res;
939 // setup tasklet for DPC
940 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
941 (unsigned long)adapter);
943 con_log(CL_DLEVEL1, (KERN_INFO
944 "megaraid mbox hba successfully initialized\n"));
946 return 0;
948 out_free_sysfs_res:
949 megaraid_sysfs_free_resources(adapter);
950 out_alloc_cmds:
951 megaraid_free_cmd_packets(adapter);
952 out_free_irq:
953 free_irq(adapter->irq, adapter);
954 out_iounmap:
955 iounmap(raid_dev->baseaddr);
956 out_release_regions:
957 pci_release_regions(pdev);
958 out_free_raid_dev:
959 kfree(raid_dev);
961 return -1;
966 * megaraid_fini_mbox - undo controller initialization
967 * @param adapter : our soft state
969 static void
970 megaraid_fini_mbox(adapter_t *adapter)
972 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
974 // flush all caches
975 megaraid_mbox_flush_cache(adapter);
977 tasklet_kill(&adapter->dpc_h);
979 megaraid_sysfs_free_resources(adapter);
981 megaraid_free_cmd_packets(adapter);
983 free_irq(adapter->irq, adapter);
985 iounmap(raid_dev->baseaddr);
987 pci_release_regions(adapter->pdev);
989 kfree(raid_dev);
991 return;
996 * megaraid_alloc_cmd_packets - allocate shared mailbox
997 * @param adapter : soft state of the raid controller
999 * Allocate and align the shared mailbox. This maibox is used to issue
1000 * all the commands. For IO based controllers, the mailbox is also regsitered
1001 * with the FW. Allocate memory for all commands as well.
1002 * This is our big allocator
1004 static int
1005 megaraid_alloc_cmd_packets(adapter_t *adapter)
1007 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1008 struct pci_dev *pdev;
1009 unsigned long align;
1010 scb_t *scb;
1011 mbox_ccb_t *ccb;
1012 struct mraid_pci_blk *epthru_pci_blk;
1013 struct mraid_pci_blk *sg_pci_blk;
1014 struct mraid_pci_blk *mbox_pci_blk;
1015 int i;
1017 pdev = adapter->pdev;
1020 * Setup the mailbox
1021 * Allocate the common 16-byte aligned memory for the handshake
1022 * mailbox.
1024 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1025 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1027 if (!raid_dev->una_mbox64) {
1028 con_log(CL_ANN, (KERN_WARNING
1029 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1030 __LINE__));
1031 return -1;
1033 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1036 * Align the mailbox at 16-byte boundary
1038 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1040 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1041 (~0UL ^ 0xFUL));
1043 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1045 align = ((void *)raid_dev->mbox -
1046 ((void *)&raid_dev->una_mbox64->mbox32));
1048 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1049 align;
1051 // Allocate memory for commands issued internally
1052 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1053 &adapter->ibuf_dma_h);
1054 if (!adapter->ibuf) {
1056 con_log(CL_ANN, (KERN_WARNING
1057 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1058 __LINE__));
1060 goto out_free_common_mbox;
1062 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1064 // Allocate memory for our SCSI Command Blocks and their associated
1065 // memory
1068 * Allocate memory for the base list of scb. Later allocate memory for
1069 * CCBs and embedded components of each CCB and point the pointers in
1070 * scb to the allocated components
1071 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1072 * since the calling routine does not yet know the number of available
1073 * commands.
1075 adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
1076 GFP_KERNEL);
1078 if (adapter->kscb_list == NULL) {
1079 con_log(CL_ANN, (KERN_WARNING
1080 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1081 __LINE__));
1082 goto out_free_ibuf;
1084 memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
1086 // memory allocation for our command packets
1087 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1088 con_log(CL_ANN, (KERN_WARNING
1089 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1090 __LINE__));
1091 goto out_free_scb_list;
1094 // Adjust the scb pointers and link in the free pool
1095 epthru_pci_blk = raid_dev->epthru_pool;
1096 sg_pci_blk = raid_dev->sg_pool;
1097 mbox_pci_blk = raid_dev->mbox_pool;
1099 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1100 scb = adapter->kscb_list + i;
1101 ccb = raid_dev->ccb_list + i;
1103 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1104 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1105 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1106 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1108 // make sure the mailbox is aligned properly
1109 if (ccb->mbox_dma_h & 0x0F) {
1110 con_log(CL_ANN, (KERN_CRIT
1111 "megaraid mbox: not aligned on 16-bytes\n"));
1113 goto out_teardown_dma_pools;
1116 ccb->epthru = (mraid_epassthru_t *)
1117 epthru_pci_blk[i].vaddr;
1118 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1119 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1120 ccb->pthru_dma_h = ccb->epthru_dma_h;
1123 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1124 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1125 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1127 scb->ccb = (caddr_t)ccb;
1128 scb->gp = 0;
1130 scb->sno = i; // command index
1132 scb->scp = NULL;
1133 scb->state = SCB_FREE;
1134 scb->dma_direction = PCI_DMA_NONE;
1135 scb->dma_type = MRAID_DMA_NONE;
1136 scb->dev_channel = -1;
1137 scb->dev_target = -1;
1139 // put scb in the free pool
1140 list_add_tail(&scb->list, &adapter->kscb_pool);
1143 return 0;
1145 out_teardown_dma_pools:
1146 megaraid_mbox_teardown_dma_pools(adapter);
1147 out_free_scb_list:
1148 kfree(adapter->kscb_list);
1149 out_free_ibuf:
1150 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1151 adapter->ibuf_dma_h);
1152 out_free_common_mbox:
1153 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1154 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1156 return -1;
1161 * megaraid_free_cmd_packets - free memory
1162 * @param adapter : soft state of the raid controller
1164 * Release memory resources allocated for commands
1166 static void
1167 megaraid_free_cmd_packets(adapter_t *adapter)
1169 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1171 megaraid_mbox_teardown_dma_pools(adapter);
1173 kfree(adapter->kscb_list);
1175 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1176 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1178 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1179 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1180 return;
1185 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1186 * @param adapter : HBA soft state
1188 * setup the dma pools for mailbox, passthru and extended passthru structures,
1189 * and scatter-gather lists
1191 static int
1192 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1194 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1195 struct mraid_pci_blk *epthru_pci_blk;
1196 struct mraid_pci_blk *sg_pci_blk;
1197 struct mraid_pci_blk *mbox_pci_blk;
1198 int i;
1202 // Allocate memory for 16-bytes aligned mailboxes
1203 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1204 adapter->pdev,
1205 sizeof(mbox64_t) + 16,
1206 16, 0);
1208 if (raid_dev->mbox_pool_handle == NULL) {
1209 goto fail_setup_dma_pool;
1212 mbox_pci_blk = raid_dev->mbox_pool;
1213 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1214 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1215 raid_dev->mbox_pool_handle,
1216 GFP_KERNEL,
1217 &mbox_pci_blk[i].dma_addr);
1218 if (!mbox_pci_blk[i].vaddr) {
1219 goto fail_setup_dma_pool;
1224 * Allocate memory for each embedded passthru strucuture pointer
1225 * Request for a 128 bytes aligned structure for each passthru command
1226 * structure
1227 * Since passthru and extended passthru commands are exclusive, they
1228 * share common memory pool. Passthru structures piggyback on memory
1229 * allocted to extended passthru since passthru is smaller of the two
1231 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1232 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1234 if (raid_dev->epthru_pool_handle == NULL) {
1235 goto fail_setup_dma_pool;
1238 epthru_pci_blk = raid_dev->epthru_pool;
1239 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1240 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1241 raid_dev->epthru_pool_handle,
1242 GFP_KERNEL,
1243 &epthru_pci_blk[i].dma_addr);
1244 if (!epthru_pci_blk[i].vaddr) {
1245 goto fail_setup_dma_pool;
1250 // Allocate memory for each scatter-gather list. Request for 512 bytes
1251 // alignment for each sg list
1252 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1253 adapter->pdev,
1254 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1255 512, 0);
1257 if (raid_dev->sg_pool_handle == NULL) {
1258 goto fail_setup_dma_pool;
1261 sg_pci_blk = raid_dev->sg_pool;
1262 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1263 sg_pci_blk[i].vaddr = pci_pool_alloc(
1264 raid_dev->sg_pool_handle,
1265 GFP_KERNEL,
1266 &sg_pci_blk[i].dma_addr);
1267 if (!sg_pci_blk[i].vaddr) {
1268 goto fail_setup_dma_pool;
1272 return 0;
1274 fail_setup_dma_pool:
1275 megaraid_mbox_teardown_dma_pools(adapter);
1276 return -1;
1281 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1282 * @param adapter : HBA soft state
1284 * teardown the dma pool for mailbox, passthru and extended passthru
1285 * structures, and scatter-gather lists
1287 static void
1288 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1290 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1291 struct mraid_pci_blk *epthru_pci_blk;
1292 struct mraid_pci_blk *sg_pci_blk;
1293 struct mraid_pci_blk *mbox_pci_blk;
1294 int i;
1297 sg_pci_blk = raid_dev->sg_pool;
1298 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1299 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1300 sg_pci_blk[i].dma_addr);
1302 if (raid_dev->sg_pool_handle)
1303 pci_pool_destroy(raid_dev->sg_pool_handle);
1306 epthru_pci_blk = raid_dev->epthru_pool;
1307 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1308 pci_pool_free(raid_dev->epthru_pool_handle,
1309 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1311 if (raid_dev->epthru_pool_handle)
1312 pci_pool_destroy(raid_dev->epthru_pool_handle);
1315 mbox_pci_blk = raid_dev->mbox_pool;
1316 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1317 pci_pool_free(raid_dev->mbox_pool_handle,
1318 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1320 if (raid_dev->mbox_pool_handle)
1321 pci_pool_destroy(raid_dev->mbox_pool_handle);
1323 return;
1328 * megaraid_alloc_scb - detach and return a scb from the free list
1329 * @adapter : controller's soft state
1331 * return the scb from the head of the free list. NULL if there are none
1332 * available
1334 static inline scb_t *
1335 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1337 struct list_head *head = &adapter->kscb_pool;
1338 scb_t *scb = NULL;
1339 unsigned long flags;
1341 // detach scb from free pool
1342 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1344 if (list_empty(head)) {
1345 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1346 return NULL;
1349 scb = list_entry(head->next, scb_t, list);
1350 list_del_init(&scb->list);
1352 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1354 scb->state = SCB_ACTIVE;
1355 scb->scp = scp;
1356 scb->dma_type = MRAID_DMA_NONE;
1358 return scb;
1363 * megaraid_dealloc_scb - return the scb to the free pool
1364 * @adapter : controller's soft state
1365 * @scb : scb to be freed
1367 * return the scb back to the free list of scbs. The caller must 'flush' the
1368 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1369 * NOTE NOTE: Make sure the scb is not on any list before calling this
1370 * routine.
1372 static inline void
1373 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1375 unsigned long flags;
1377 // put scb in the free pool
1378 scb->state = SCB_FREE;
1379 scb->scp = NULL;
1380 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1382 list_add(&scb->list, &adapter->kscb_pool);
1384 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1386 return;
1391 * megaraid_mbox_mksgl - make the scatter-gather list
1392 * @adapter - controller's soft state
1393 * @scb - scsi control block
1395 * prepare the scatter-gather list
1397 static inline int
1398 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1400 struct scatterlist *sgl;
1401 mbox_ccb_t *ccb;
1402 struct page *page;
1403 unsigned long offset;
1404 struct scsi_cmnd *scp;
1405 int sgcnt;
1406 int i;
1409 scp = scb->scp;
1410 ccb = (mbox_ccb_t *)scb->ccb;
1412 // no mapping required if no data to be transferred
1413 if (!scp->request_buffer || !scp->request_bufflen)
1414 return 0;
1416 if (!scp->use_sg) { /* scatter-gather list not used */
1418 page = virt_to_page(scp->request_buffer);
1420 offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
1422 ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
1423 scp->request_bufflen,
1424 scb->dma_direction);
1425 scb->dma_type = MRAID_DMA_WBUF;
1428 * We need to handle special 64-bit commands that need a
1429 * minimum of 1 SG
1431 sgcnt = 1;
1432 ccb->sgl64[0].address = ccb->buf_dma_h;
1433 ccb->sgl64[0].length = scp->request_bufflen;
1435 return sgcnt;
1438 sgl = (struct scatterlist *)scp->request_buffer;
1440 // The number of sg elements returned must not exceed our limit
1441 sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
1442 scb->dma_direction);
1444 if (sgcnt > adapter->sglen) {
1445 con_log(CL_ANN, (KERN_CRIT
1446 "megaraid critical: too many sg elements:%d\n",
1447 sgcnt));
1448 BUG();
1451 scb->dma_type = MRAID_DMA_WSG;
1453 for (i = 0; i < sgcnt; i++, sgl++) {
1454 ccb->sgl64[i].address = sg_dma_address(sgl);
1455 ccb->sgl64[i].length = sg_dma_len(sgl);
1458 // Return count of SG nodes
1459 return sgcnt;
1464 * mbox_post_cmd - issue a mailbox command
1465 * @adapter - controller's soft state
1466 * @scb - command to be issued
1468 * post the command to the controller if mailbox is availble.
1470 static inline int
1471 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1473 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1474 mbox64_t *mbox64;
1475 mbox_t *mbox;
1476 mbox_ccb_t *ccb;
1477 unsigned long flags;
1478 unsigned int i = 0;
1481 ccb = (mbox_ccb_t *)scb->ccb;
1482 mbox = raid_dev->mbox;
1483 mbox64 = raid_dev->mbox64;
1486 * Check for busy mailbox. If it is, return failure - the caller
1487 * should retry later.
1489 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1491 if (unlikely(mbox->busy)) {
1492 do {
1493 udelay(1);
1494 i++;
1495 rmb();
1496 } while(mbox->busy && (i < max_mbox_busy_wait));
1498 if (mbox->busy) {
1500 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1502 return -1;
1507 // Copy this command's mailbox data into "adapter's" mailbox
1508 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1509 mbox->cmdid = scb->sno;
1511 adapter->outstanding_cmds++;
1513 if (scb->dma_direction == PCI_DMA_TODEVICE) {
1514 if (!scb->scp->use_sg) { // sg list not used
1515 pci_dma_sync_single_for_device(adapter->pdev,
1516 ccb->buf_dma_h,
1517 scb->scp->request_bufflen,
1518 PCI_DMA_TODEVICE);
1520 else {
1521 pci_dma_sync_sg_for_device(adapter->pdev,
1522 scb->scp->request_buffer,
1523 scb->scp->use_sg, PCI_DMA_TODEVICE);
1527 mbox->busy = 1; // Set busy
1528 mbox->poll = 0;
1529 mbox->ack = 0;
1530 wmb();
1532 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1534 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1536 return 0;
1541 * megaraid_queue_command - generic queue entry point for all LLDs
1542 * @scp : pointer to the scsi command to be executed
1543 * @done : callback routine to be called after the cmd has be completed
1545 * Queue entry point for mailbox based controllers.
1547 static int
1548 megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
1550 adapter_t *adapter;
1551 scb_t *scb;
1552 int if_busy;
1554 adapter = SCP2ADAPTER(scp);
1555 scp->scsi_done = done;
1556 scp->result = 0;
1559 * Allocate and build a SCB request
1560 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1561 * not allocate scb. We will return non-zero status in that case.
1562 * NOTE: scb can be null even though certain commands completed
1563 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1564 * return 0 in that case, and we would do the callback right away.
1566 if_busy = 0;
1567 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1568 if (!scb) { // command already completed
1569 done(scp);
1570 return 0;
1573 megaraid_mbox_runpendq(adapter, scb);
1574 return if_busy;
1578 * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
1579 * firmware lingua
1580 * @adapter - controller's soft state
1581 * @scp - mid-layer scsi command pointer
1582 * @busy - set if request could not be completed because of lack of
1583 * resources
1585 * convert the command issued by mid-layer to format understood by megaraid
1586 * firmware. We also complete certain command without sending them to firmware
1588 static scb_t *
1589 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1591 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1592 int channel;
1593 int target;
1594 int islogical;
1595 mbox_ccb_t *ccb;
1596 mraid_passthru_t *pthru;
1597 mbox64_t *mbox64;
1598 mbox_t *mbox;
1599 scb_t *scb;
1600 char skip[] = "skipping";
1601 char scan[] = "scanning";
1602 char *ss;
1606 * Get the appropriate device map for the device this command is
1607 * intended for
1609 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1612 * Logical drive commands
1614 if (islogical) {
1615 switch (scp->cmnd[0]) {
1616 case TEST_UNIT_READY:
1618 * Do we support clustering and is the support enabled
1619 * If no, return success always
1621 if (!adapter->ha) {
1622 scp->result = (DID_OK << 16);
1623 return NULL;
1626 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1627 scp->result = (DID_ERROR << 16);
1628 *busy = 1;
1629 return NULL;
1632 scb->dma_direction = scp->sc_data_direction;
1633 scb->dev_channel = 0xFF;
1634 scb->dev_target = target;
1635 ccb = (mbox_ccb_t *)scb->ccb;
1638 * The command id will be provided by the command
1639 * issuance routine
1641 ccb->raw_mbox[0] = CLUSTER_CMD;
1642 ccb->raw_mbox[2] = RESERVATION_STATUS;
1643 ccb->raw_mbox[3] = target;
1645 return scb;
1647 case MODE_SENSE:
1648 if (scp->use_sg) {
1649 struct scatterlist *sgl;
1650 caddr_t vaddr;
1652 sgl = (struct scatterlist *)scp->request_buffer;
1653 if (sgl->page) {
1654 vaddr = (caddr_t)
1655 (page_address((&sgl[0])->page)
1656 + (&sgl[0])->offset);
1658 memset(vaddr, 0, scp->cmnd[4]);
1660 else {
1661 con_log(CL_ANN, (KERN_WARNING
1662 "megaraid mailbox: invalid sg:%d\n",
1663 __LINE__));
1666 else {
1667 memset(scp->request_buffer, 0, scp->cmnd[4]);
1669 scp->result = (DID_OK << 16);
1670 return NULL;
1672 case INQUIRY:
1674 * Display the channel scan for logical drives
1675 * Do not display scan for a channel if already done.
1677 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1679 con_log(CL_ANN, (KERN_INFO
1680 "scsi[%d]: scanning scsi channel %d",
1681 adapter->host->host_no,
1682 SCP2CHANNEL(scp)));
1684 con_log(CL_ANN, (
1685 " [virtual] for logical drives\n"));
1687 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1690 /* Fall through */
1692 case READ_CAPACITY:
1694 * Do not allow LUN > 0 for logical drives and
1695 * requests for more than 40 logical drives
1697 if (SCP2LUN(scp)) {
1698 scp->result = (DID_BAD_TARGET << 16);
1699 return NULL;
1701 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1702 scp->result = (DID_BAD_TARGET << 16);
1703 return NULL;
1707 /* Allocate a SCB and initialize passthru */
1708 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1709 scp->result = (DID_ERROR << 16);
1710 *busy = 1;
1711 return NULL;
1714 ccb = (mbox_ccb_t *)scb->ccb;
1715 scb->dev_channel = 0xFF;
1716 scb->dev_target = target;
1717 pthru = ccb->pthru;
1718 mbox = ccb->mbox;
1719 mbox64 = ccb->mbox64;
1721 pthru->timeout = 0;
1722 pthru->ars = 1;
1723 pthru->reqsenselen = 14;
1724 pthru->islogical = 1;
1725 pthru->logdrv = target;
1726 pthru->cdblen = scp->cmd_len;
1727 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1729 mbox->cmd = MBOXCMD_PASSTHRU64;
1730 scb->dma_direction = scp->sc_data_direction;
1732 pthru->dataxferlen = scp->request_bufflen;
1733 pthru->dataxferaddr = ccb->sgl_dma_h;
1734 pthru->numsge = megaraid_mbox_mksgl(adapter,
1735 scb);
1737 mbox->xferaddr = 0xFFFFFFFF;
1738 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1739 mbox64->xferaddr_hi = 0;
1741 return scb;
1743 case READ_6:
1744 case WRITE_6:
1745 case READ_10:
1746 case WRITE_10:
1747 case READ_12:
1748 case WRITE_12:
1751 * Allocate a SCB and initialize mailbox
1753 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1754 scp->result = (DID_ERROR << 16);
1755 *busy = 1;
1756 return NULL;
1758 ccb = (mbox_ccb_t *)scb->ccb;
1759 scb->dev_channel = 0xFF;
1760 scb->dev_target = target;
1761 mbox = ccb->mbox;
1762 mbox64 = ccb->mbox64;
1763 mbox->logdrv = target;
1766 * A little HACK: 2nd bit is zero for all scsi read
1767 * commands and is set for all scsi write commands
1769 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1770 MBOXCMD_LREAD64 ;
1773 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1775 if (scp->cmd_len == 6) {
1776 mbox->numsectors = (uint32_t)scp->cmnd[4];
1777 mbox->lba =
1778 ((uint32_t)scp->cmnd[1] << 16) |
1779 ((uint32_t)scp->cmnd[2] << 8) |
1780 (uint32_t)scp->cmnd[3];
1782 mbox->lba &= 0x1FFFFF;
1786 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1788 else if (scp->cmd_len == 10) {
1789 mbox->numsectors =
1790 (uint32_t)scp->cmnd[8] |
1791 ((uint32_t)scp->cmnd[7] << 8);
1792 mbox->lba =
1793 ((uint32_t)scp->cmnd[2] << 24) |
1794 ((uint32_t)scp->cmnd[3] << 16) |
1795 ((uint32_t)scp->cmnd[4] << 8) |
1796 (uint32_t)scp->cmnd[5];
1800 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1802 else if (scp->cmd_len == 12) {
1803 mbox->lba =
1804 ((uint32_t)scp->cmnd[2] << 24) |
1805 ((uint32_t)scp->cmnd[3] << 16) |
1806 ((uint32_t)scp->cmnd[4] << 8) |
1807 (uint32_t)scp->cmnd[5];
1809 mbox->numsectors =
1810 ((uint32_t)scp->cmnd[6] << 24) |
1811 ((uint32_t)scp->cmnd[7] << 16) |
1812 ((uint32_t)scp->cmnd[8] << 8) |
1813 (uint32_t)scp->cmnd[9];
1815 else {
1816 con_log(CL_ANN, (KERN_WARNING
1817 "megaraid: unsupported CDB length\n"));
1819 megaraid_dealloc_scb(adapter, scb);
1821 scp->result = (DID_ERROR << 16);
1822 return NULL;
1825 scb->dma_direction = scp->sc_data_direction;
1827 // Calculate Scatter-Gather info
1828 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1829 mbox->numsge = megaraid_mbox_mksgl(adapter,
1830 scb);
1831 mbox->xferaddr = 0xFFFFFFFF;
1832 mbox64->xferaddr_hi = 0;
1834 return scb;
1836 case RESERVE:
1837 case RELEASE:
1839 * Do we support clustering and is the support enabled
1841 if (!adapter->ha) {
1842 scp->result = (DID_BAD_TARGET << 16);
1843 return NULL;
1847 * Allocate a SCB and initialize mailbox
1849 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1850 scp->result = (DID_ERROR << 16);
1851 *busy = 1;
1852 return NULL;
1855 ccb = (mbox_ccb_t *)scb->ccb;
1856 scb->dev_channel = 0xFF;
1857 scb->dev_target = target;
1858 ccb->raw_mbox[0] = CLUSTER_CMD;
1859 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1860 RESERVE_LD : RELEASE_LD;
1862 ccb->raw_mbox[3] = target;
1863 scb->dma_direction = scp->sc_data_direction;
1865 return scb;
1867 default:
1868 scp->result = (DID_BAD_TARGET << 16);
1869 return NULL;
1872 else { // Passthru device commands
1874 // Do not allow access to target id > 15 or LUN > 7
1875 if (target > 15 || SCP2LUN(scp) > 7) {
1876 scp->result = (DID_BAD_TARGET << 16);
1877 return NULL;
1880 // if fast load option was set and scan for last device is
1881 // over, reset the fast_load flag so that during a possible
1882 // next scan, devices can be made available
1883 if (rdev->fast_load && (target == 15) &&
1884 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1886 con_log(CL_ANN, (KERN_INFO
1887 "megaraid[%d]: physical device scan re-enabled\n",
1888 adapter->host->host_no));
1889 rdev->fast_load = 0;
1893 * Display the channel scan for physical devices
1895 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1897 ss = rdev->fast_load ? skip : scan;
1899 con_log(CL_ANN, (KERN_INFO
1900 "scsi[%d]: %s scsi channel %d [Phy %d]",
1901 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1902 channel));
1904 con_log(CL_ANN, (
1905 " for non-raid devices\n"));
1907 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1910 // disable channel sweep if fast load option given
1911 if (rdev->fast_load) {
1912 scp->result = (DID_BAD_TARGET << 16);
1913 return NULL;
1916 // Allocate a SCB and initialize passthru
1917 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1918 scp->result = (DID_ERROR << 16);
1919 *busy = 1;
1920 return NULL;
1923 ccb = (mbox_ccb_t *)scb->ccb;
1924 scb->dev_channel = channel;
1925 scb->dev_target = target;
1926 scb->dma_direction = scp->sc_data_direction;
1927 mbox = ccb->mbox;
1928 mbox64 = ccb->mbox64;
1930 // Does this firmware support extended CDBs
1931 if (adapter->max_cdb_sz == 16) {
1932 mbox->cmd = MBOXCMD_EXTPTHRU;
1934 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1936 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1937 mbox64->xferaddr_hi = 0;
1938 mbox->xferaddr = 0xFFFFFFFF;
1940 else {
1941 mbox->cmd = MBOXCMD_PASSTHRU64;
1943 megaraid_mbox_prepare_pthru(adapter, scb, scp);
1945 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
1946 mbox64->xferaddr_hi = 0;
1947 mbox->xferaddr = 0xFFFFFFFF;
1949 return scb;
1952 // NOT REACHED
1957 * megaraid_mbox_runpendq - execute commands queued in the pending queue
1958 * @adapter : controller's soft state
1959 * @scb : SCB to be queued in the pending list
1961 * scan the pending list for commands which are not yet issued and try to
1962 * post to the controller. The SCB can be a null pointer, which would indicate
1963 * no SCB to be queue, just try to execute the ones in the pending list.
1965 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1966 * out from the head of the pending list. If it is successfully issued, the
1967 * next SCB is at the head now.
1969 static void
1970 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1972 scb_t *scb;
1973 unsigned long flags;
1975 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1977 if (scb_q) {
1978 scb_q->state = SCB_PENDQ;
1979 list_add_tail(&scb_q->list, &adapter->pend_list);
1982 // if the adapter in not in quiescent mode, post the commands to FW
1983 if (adapter->quiescent) {
1984 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1985 return;
1988 while (!list_empty(&adapter->pend_list)) {
1990 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1992 scb = list_entry(adapter->pend_list.next, scb_t, list);
1994 // remove the scb from the pending list and try to
1995 // issue. If we are unable to issue it, put back in
1996 // the pending list and return
1998 list_del_init(&scb->list);
2000 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2002 // if mailbox was busy, return SCB back to pending
2003 // list. Make sure to add at the head, since that's
2004 // where it would have been removed from
2006 scb->state = SCB_ISSUED;
2008 if (mbox_post_cmd(adapter, scb) != 0) {
2010 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2012 scb->state = SCB_PENDQ;
2014 list_add(&scb->list, &adapter->pend_list);
2016 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2017 flags);
2019 return;
2022 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2025 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2028 return;
2033 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
2034 * @adapter - pointer to controller's soft state
2035 * @scb - scsi control block
2036 * @scp - scsi command from the mid-layer
2038 * prepare a command for the scsi physical devices
2040 static void
2041 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
2042 struct scsi_cmnd *scp)
2044 mbox_ccb_t *ccb;
2045 mraid_passthru_t *pthru;
2046 uint8_t channel;
2047 uint8_t target;
2049 ccb = (mbox_ccb_t *)scb->ccb;
2050 pthru = ccb->pthru;
2051 channel = scb->dev_channel;
2052 target = scb->dev_target;
2054 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2055 pthru->timeout = 4;
2056 pthru->ars = 1;
2057 pthru->islogical = 0;
2058 pthru->channel = 0;
2059 pthru->target = (channel << 4) | target;
2060 pthru->logdrv = SCP2LUN(scp);
2061 pthru->reqsenselen = 14;
2062 pthru->cdblen = scp->cmd_len;
2064 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2066 if (scp->request_bufflen) {
2067 pthru->dataxferlen = scp->request_bufflen;
2068 pthru->dataxferaddr = ccb->sgl_dma_h;
2069 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2071 else {
2072 pthru->dataxferaddr = 0;
2073 pthru->dataxferlen = 0;
2074 pthru->numsge = 0;
2076 return;
2081 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2082 * @adapter - pointer to controller's soft state
2083 * @scb - scsi control block
2084 * @scp - scsi command from the mid-layer
2086 * prepare a command for the scsi physical devices. This rountine prepares
2087 * commands for devices which can take extended CDBs (>10 bytes)
2089 static void
2090 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2091 struct scsi_cmnd *scp)
2093 mbox_ccb_t *ccb;
2094 mraid_epassthru_t *epthru;
2095 uint8_t channel;
2096 uint8_t target;
2098 ccb = (mbox_ccb_t *)scb->ccb;
2099 epthru = ccb->epthru;
2100 channel = scb->dev_channel;
2101 target = scb->dev_target;
2103 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2104 epthru->timeout = 4;
2105 epthru->ars = 1;
2106 epthru->islogical = 0;
2107 epthru->channel = 0;
2108 epthru->target = (channel << 4) | target;
2109 epthru->logdrv = SCP2LUN(scp);
2110 epthru->reqsenselen = 14;
2111 epthru->cdblen = scp->cmd_len;
2113 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2115 if (scp->request_bufflen) {
2116 epthru->dataxferlen = scp->request_bufflen;
2117 epthru->dataxferaddr = ccb->sgl_dma_h;
2118 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2120 else {
2121 epthru->dataxferaddr = 0;
2122 epthru->dataxferlen = 0;
2123 epthru->numsge = 0;
2125 return;
2130 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2131 * @adapter - controller's soft state
2133 * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
2134 * completed command and put them on the completed list for later processing.
2136 * Returns: 1 if the interrupt is valid, 0 otherwise
2138 static inline int
2139 megaraid_ack_sequence(adapter_t *adapter)
2141 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2142 mbox_t *mbox;
2143 scb_t *scb;
2144 uint8_t nstatus;
2145 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2146 struct list_head clist;
2147 int handled;
2148 uint32_t dword;
2149 unsigned long flags;
2150 int i, j;
2153 mbox = raid_dev->mbox;
2155 // move the SCBs from the firmware completed array to our local list
2156 INIT_LIST_HEAD(&clist);
2158 // loop till F/W has more commands for us to complete
2159 handled = 0;
2160 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2161 do {
2163 * Check if a valid interrupt is pending. If found, force the
2164 * interrupt line low.
2166 dword = RDOUTDOOR(raid_dev);
2167 if (dword != 0x10001234) break;
2169 handled = 1;
2171 WROUTDOOR(raid_dev, 0x10001234);
2173 nstatus = 0;
2174 // wait for valid numstatus to post
2175 for (i = 0; i < 0xFFFFF; i++) {
2176 if (mbox->numstatus != 0xFF) {
2177 nstatus = mbox->numstatus;
2178 break;
2180 rmb();
2182 mbox->numstatus = 0xFF;
2184 adapter->outstanding_cmds -= nstatus;
2186 for (i = 0; i < nstatus; i++) {
2188 // wait for valid command index to post
2189 for (j = 0; j < 0xFFFFF; j++) {
2190 if (mbox->completed[i] != 0xFF) break;
2191 rmb();
2193 completed[i] = mbox->completed[i];
2194 mbox->completed[i] = 0xFF;
2196 if (completed[i] == 0xFF) {
2197 con_log(CL_ANN, (KERN_CRIT
2198 "megaraid: command posting timed out\n"));
2200 BUG();
2201 continue;
2204 // Get SCB associated with this command id
2205 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2206 // a cmm command
2207 scb = adapter->uscb_list + (completed[i] -
2208 MBOX_MAX_SCSI_CMDS);
2210 else {
2211 // an os command
2212 scb = adapter->kscb_list + completed[i];
2215 scb->status = mbox->status;
2216 list_add_tail(&scb->list, &clist);
2219 // Acknowledge interrupt
2220 WRINDOOR(raid_dev, 0x02);
2222 } while(1);
2224 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2227 // put the completed commands in the completed list. DPC would
2228 // complete these commands later
2229 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2231 list_splice(&clist, &adapter->completed_list);
2233 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2236 // schedule the DPC if there is some work for it
2237 if (handled)
2238 tasklet_schedule(&adapter->dpc_h);
2240 return handled;
2245 * megaraid_isr - isr for memory based mailbox based controllers
2246 * @irq - irq
2247 * @devp - pointer to our soft state
2248 * @regs - unused
2250 * Interrupt service routine for memory-mapped mailbox controllers.
2252 static irqreturn_t
2253 megaraid_isr(int irq, void *devp, struct pt_regs *regs)
2255 adapter_t *adapter = devp;
2256 int handled;
2258 handled = megaraid_ack_sequence(adapter);
2260 /* Loop through any pending requests */
2261 if (!adapter->quiescent) {
2262 megaraid_mbox_runpendq(adapter, NULL);
2265 return IRQ_RETVAL(handled);
2270 * megaraid_mbox_sync_scb - sync kernel buffers
2271 * @adapter : controller's soft state
2272 * @scb : pointer to the resource packet
2274 * DMA sync if required.
2276 static inline void
2277 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2279 mbox_ccb_t *ccb;
2281 ccb = (mbox_ccb_t *)scb->ccb;
2283 switch (scb->dma_type) {
2285 case MRAID_DMA_WBUF:
2286 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2287 pci_dma_sync_single_for_cpu(adapter->pdev,
2288 ccb->buf_dma_h,
2289 scb->scp->request_bufflen,
2290 PCI_DMA_FROMDEVICE);
2293 pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
2294 scb->scp->request_bufflen, scb->dma_direction);
2296 break;
2298 case MRAID_DMA_WSG:
2299 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2300 pci_dma_sync_sg_for_cpu(adapter->pdev,
2301 scb->scp->request_buffer,
2302 scb->scp->use_sg, PCI_DMA_FROMDEVICE);
2305 pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
2306 scb->scp->use_sg, scb->dma_direction);
2308 break;
2310 default:
2311 break;
2314 return;
2319 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2320 * @devp : pointer to HBA soft state
2322 * Pick up the commands from the completed list and send back to the owners.
2323 * This is a reentrant function and does not assume any locks are held while
2324 * it is being called.
2326 static void
2327 megaraid_mbox_dpc(unsigned long devp)
2329 adapter_t *adapter = (adapter_t *)devp;
2330 mraid_device_t *raid_dev;
2331 struct list_head clist;
2332 struct scatterlist *sgl;
2333 scb_t *scb;
2334 scb_t *tmp;
2335 struct scsi_cmnd *scp;
2336 mraid_passthru_t *pthru;
2337 mraid_epassthru_t *epthru;
2338 mbox_ccb_t *ccb;
2339 int islogical;
2340 int pdev_index;
2341 int pdev_state;
2342 mbox_t *mbox;
2343 unsigned long flags;
2344 uint8_t c;
2345 int status;
2348 if (!adapter) return;
2350 raid_dev = ADAP2RAIDDEV(adapter);
2352 // move the SCBs from the completed list to our local list
2353 INIT_LIST_HEAD(&clist);
2355 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2357 list_splice_init(&adapter->completed_list, &clist);
2359 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2362 list_for_each_entry_safe(scb, tmp, &clist, list) {
2364 status = scb->status;
2365 scp = scb->scp;
2366 ccb = (mbox_ccb_t *)scb->ccb;
2367 pthru = ccb->pthru;
2368 epthru = ccb->epthru;
2369 mbox = ccb->mbox;
2371 // Make sure f/w has completed a valid command
2372 if (scb->state != SCB_ISSUED) {
2373 con_log(CL_ANN, (KERN_CRIT
2374 "megaraid critical err: invalid command %d:%d:%p\n",
2375 scb->sno, scb->state, scp));
2376 BUG();
2377 continue; // Must never happen!
2380 // check for the management command and complete it right away
2381 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2382 scb->state = SCB_FREE;
2383 scb->status = status;
2385 // remove from local clist
2386 list_del_init(&scb->list);
2388 megaraid_mbox_mm_done(adapter, scb);
2390 continue;
2393 // Was an abort issued for this command earlier
2394 if (scb->state & SCB_ABORT) {
2395 con_log(CL_ANN, (KERN_NOTICE
2396 "megaraid: aborted cmd %lx[%x] completed\n",
2397 scp->serial_number, scb->sno));
2401 * If the inquiry came of a disk drive which is not part of
2402 * any RAID array, expose it to the kernel. For this to be
2403 * enabled, user must set the "megaraid_expose_unconf_disks"
2404 * flag to 1 by specifying it on module parameter list.
2405 * This would enable data migration off drives from other
2406 * configurations.
2408 islogical = MRAID_IS_LOGICAL(adapter, scp);
2409 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2410 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2412 if (scp->use_sg) {
2413 sgl = (struct scatterlist *)
2414 scp->request_buffer;
2416 if (sgl->page) {
2417 c = *(unsigned char *)
2418 (page_address((&sgl[0])->page) +
2419 (&sgl[0])->offset);
2421 else {
2422 con_log(CL_ANN, (KERN_WARNING
2423 "megaraid mailbox: invalid sg:%d\n",
2424 __LINE__));
2425 c = 0;
2428 else {
2429 c = *(uint8_t *)scp->request_buffer;
2432 if ((c & 0x1F ) == TYPE_DISK) {
2433 pdev_index = (scb->dev_channel * 16) +
2434 scb->dev_target;
2435 pdev_state =
2436 raid_dev->pdrv_state[pdev_index] & 0x0F;
2438 if (pdev_state == PDRV_ONLINE ||
2439 pdev_state == PDRV_FAILED ||
2440 pdev_state == PDRV_RBLD ||
2441 pdev_state == PDRV_HOTSPARE ||
2442 megaraid_expose_unconf_disks == 0) {
2444 status = 0xF0;
2449 // Convert MegaRAID status to Linux error code
2450 switch (status) {
2452 case 0x00:
2454 scp->result = (DID_OK << 16);
2455 break;
2457 case 0x02:
2459 /* set sense_buffer and result fields */
2460 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2461 mbox->cmd == MBOXCMD_PASSTHRU64) {
2463 memcpy(scp->sense_buffer, pthru->reqsensearea,
2464 14);
2466 scp->result = DRIVER_SENSE << 24 |
2467 DID_OK << 16 | CHECK_CONDITION << 1;
2469 else {
2470 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2472 memcpy(scp->sense_buffer,
2473 epthru->reqsensearea, 14);
2475 scp->result = DRIVER_SENSE << 24 |
2476 DID_OK << 16 |
2477 CHECK_CONDITION << 1;
2478 } else {
2479 scp->sense_buffer[0] = 0x70;
2480 scp->sense_buffer[2] = ABORTED_COMMAND;
2481 scp->result = CHECK_CONDITION << 1;
2484 break;
2486 case 0x08:
2488 scp->result = DID_BUS_BUSY << 16 | status;
2489 break;
2491 default:
2494 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2495 * failed
2497 if (scp->cmnd[0] == TEST_UNIT_READY) {
2498 scp->result = DID_ERROR << 16 |
2499 RESERVATION_CONFLICT << 1;
2501 else
2503 * Error code returned is 1 if Reserve or Release
2504 * failed or the input parameter is invalid
2506 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2507 scp->cmnd[0] == RELEASE)) {
2509 scp->result = DID_ERROR << 16 |
2510 RESERVATION_CONFLICT << 1;
2512 else {
2513 scp->result = DID_BAD_TARGET << 16 | status;
2517 // print a debug message for all failed commands
2518 if (status) {
2519 megaraid_mbox_display_scb(adapter, scb);
2522 // Free our internal resources and call the mid-layer callback
2523 // routine
2524 megaraid_mbox_sync_scb(adapter, scb);
2526 // remove from local clist
2527 list_del_init(&scb->list);
2529 // put back in free list
2530 megaraid_dealloc_scb(adapter, scb);
2532 // send the scsi packet back to kernel
2533 scp->scsi_done(scp);
2536 return;
2541 * megaraid_abort_handler - abort the scsi command
2542 * @scp : command to be aborted
2544 * Abort a previous SCSI request. Only commands on the pending list can be
2545 * aborted. All the commands issued to the F/W must complete.
2547 static int
2548 megaraid_abort_handler(struct scsi_cmnd *scp)
2550 adapter_t *adapter;
2551 mraid_device_t *raid_dev;
2552 scb_t *scb;
2553 scb_t *tmp;
2554 int found;
2555 unsigned long flags;
2556 int i;
2559 adapter = SCP2ADAPTER(scp);
2560 raid_dev = ADAP2RAIDDEV(adapter);
2562 con_log(CL_ANN, (KERN_WARNING
2563 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2564 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2565 SCP2TARGET(scp), SCP2LUN(scp)));
2567 // If FW has stopped responding, simply return failure
2568 if (raid_dev->hw_error) {
2569 con_log(CL_ANN, (KERN_NOTICE
2570 "megaraid: hw error, not aborting\n"));
2571 return FAILED;
2574 // There might a race here, where the command was completed by the
2575 // firmware and now it is on the completed list. Before we could
2576 // complete the command to the kernel in dpc, the abort came.
2577 // Find out if this is the case to avoid the race.
2578 scb = NULL;
2579 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2580 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2582 if (scb->scp == scp) { // Found command
2584 list_del_init(&scb->list); // from completed list
2586 con_log(CL_ANN, (KERN_WARNING
2587 "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2588 scp->serial_number, scb->sno,
2589 scb->dev_channel, scb->dev_target));
2591 scp->result = (DID_ABORT << 16);
2592 scp->scsi_done(scp);
2594 megaraid_dealloc_scb(adapter, scb);
2596 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2597 flags);
2599 return SUCCESS;
2602 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2605 // Find out if this command is still on the pending list. If it is and
2606 // was never issued, abort and return success. If the command is owned
2607 // by the firmware, we must wait for it to complete by the FW.
2608 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2609 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2611 if (scb->scp == scp) { // Found command
2613 list_del_init(&scb->list); // from pending list
2615 ASSERT(!(scb->state & SCB_ISSUED));
2617 con_log(CL_ANN, (KERN_WARNING
2618 "megaraid abort: %ld[%d:%d], driver owner\n",
2619 scp->serial_number, scb->dev_channel,
2620 scb->dev_target));
2622 scp->result = (DID_ABORT << 16);
2623 scp->scsi_done(scp);
2625 megaraid_dealloc_scb(adapter, scb);
2627 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2628 flags);
2630 return SUCCESS;
2633 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2636 // Check do we even own this command, in which case this would be
2637 // owned by the firmware. The only way to locate the FW scb is to
2638 // traverse through the list of all SCB, since driver does not
2639 // maintain these SCBs on any list
2640 found = 0;
2641 spin_lock_irq(&adapter->lock);
2642 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2643 scb = adapter->kscb_list + i;
2645 if (scb->scp == scp) {
2647 found = 1;
2649 if (!(scb->state & SCB_ISSUED)) {
2650 con_log(CL_ANN, (KERN_WARNING
2651 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2652 scp->serial_number, scb->sno, scb->dev_channel,
2653 scb->dev_target));
2654 BUG();
2656 else {
2657 con_log(CL_ANN, (KERN_WARNING
2658 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2659 scp->serial_number, scb->sno, scb->dev_channel,
2660 scb->dev_target));
2664 spin_unlock_irq(&adapter->lock);
2666 if (!found) {
2667 con_log(CL_ANN, (KERN_WARNING
2668 "megaraid abort: scsi cmd:%ld, do now own\n",
2669 scp->serial_number));
2671 // FIXME: Should there be a callback for this command?
2672 return SUCCESS;
2675 // We cannot actually abort a command owned by firmware, return
2676 // failure and wait for reset. In host reset handler, we will find out
2677 // if the HBA is still live
2678 return FAILED;
2682 * megaraid_reset_handler - device reset hadler for mailbox based driver
2683 * @scp : reference command
2685 * Reset handler for the mailbox based controller. First try to find out if
2686 * the FW is still live, in which case the outstanding commands counter mut go
2687 * down to 0. If that happens, also issue the reservation reset command to
2688 * relinquish (possible) reservations on the logical drives connected to this
2689 * host
2691 static int
2692 megaraid_reset_handler(struct scsi_cmnd *scp)
2694 adapter_t *adapter;
2695 scb_t *scb;
2696 scb_t *tmp;
2697 mraid_device_t *raid_dev;
2698 unsigned long flags;
2699 uint8_t raw_mbox[sizeof(mbox_t)];
2700 int rval;
2701 int recovery_window;
2702 int recovering;
2703 int i;
2705 adapter = SCP2ADAPTER(scp);
2706 raid_dev = ADAP2RAIDDEV(adapter);
2708 // return failure if adapter is not responding
2709 if (raid_dev->hw_error) {
2710 con_log(CL_ANN, (KERN_NOTICE
2711 "megaraid: hw error, cannot reset\n"));
2712 return FAILED;
2716 // Under exceptional conditions, FW can take up to 3 minutes to
2717 // complete command processing. Wait for additional 2 minutes for the
2718 // pending commands counter to go down to 0. If it doesn't, let the
2719 // controller be marked offline
2720 // Also, reset all the commands currently owned by the driver
2721 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2722 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2724 list_del_init(&scb->list); // from pending list
2726 con_log(CL_ANN, (KERN_WARNING
2727 "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2728 scp->serial_number, scb->sno,
2729 scb->dev_channel, scb->dev_target));
2731 scp->result = (DID_RESET << 16);
2732 scp->scsi_done(scp);
2734 megaraid_dealloc_scb(adapter, scb);
2736 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2738 if (adapter->outstanding_cmds) {
2739 con_log(CL_ANN, (KERN_NOTICE
2740 "megaraid: %d outstanding commands. Max wait %d sec\n",
2741 adapter->outstanding_cmds, MBOX_RESET_WAIT));
2744 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2746 recovering = adapter->outstanding_cmds;
2748 for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
2750 megaraid_ack_sequence(adapter);
2752 // print a message once every 5 seconds only
2753 if (!(i % 5)) {
2754 con_log(CL_ANN, (
2755 "megaraid mbox: Wait for %d commands to complete:%d\n",
2756 adapter->outstanding_cmds,
2757 MBOX_RESET_WAIT - i));
2760 // bailout if no recovery happended in reset time
2761 if ((i == MBOX_RESET_WAIT) &&
2762 (recovering == adapter->outstanding_cmds)) {
2763 break;
2766 msleep(1000);
2769 spin_lock(&adapter->lock);
2771 // If still outstanding commands, bail out
2772 if (adapter->outstanding_cmds) {
2773 con_log(CL_ANN, (KERN_WARNING
2774 "megaraid mbox: critical hardware error!\n"));
2776 raid_dev->hw_error = 1;
2778 rval = FAILED;
2779 goto out;
2781 else {
2782 con_log(CL_ANN, (KERN_NOTICE
2783 "megaraid mbox: reset sequence completed sucessfully\n"));
2787 // If the controller supports clustering, reset reservations
2788 if (!adapter->ha) {
2789 rval = SUCCESS;
2790 goto out;
2793 // clear reservations if any
2794 raw_mbox[0] = CLUSTER_CMD;
2795 raw_mbox[2] = RESET_RESERVATIONS;
2797 rval = SUCCESS;
2798 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2799 con_log(CL_ANN,
2800 (KERN_INFO "megaraid: reservation reset\n"));
2802 else {
2803 rval = FAILED;
2804 con_log(CL_ANN, (KERN_WARNING
2805 "megaraid: reservation reset failed\n"));
2808 out:
2809 spin_unlock_irq(&adapter->lock);
2810 return rval;
2814 * START: internal commands library
2816 * This section of the driver has the common routine used by the driver and
2817 * also has all the FW routines
2821 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2822 * @adapter - controller's soft state
2823 * @raw_mbox - the mailbox
2825 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2826 * controllers
2828 static int
2829 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2831 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2832 mbox64_t *mbox64;
2833 mbox_t *mbox;
2834 uint8_t status;
2835 int i;
2838 mbox64 = raid_dev->mbox64;
2839 mbox = raid_dev->mbox;
2842 * Wait until mailbox is free
2844 if (megaraid_busywait_mbox(raid_dev) != 0)
2845 goto blocked_mailbox;
2848 * Copy mailbox data into host structure
2850 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2851 mbox->cmdid = 0xFE;
2852 mbox->busy = 1;
2853 mbox->poll = 0;
2854 mbox->ack = 0;
2855 mbox->numstatus = 0xFF;
2856 mbox->status = 0xFF;
2858 wmb();
2859 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2861 // wait for maximum 1 second for status to post. If the status is not
2862 // available within 1 second, assume FW is initializing and wait
2863 // for an extended amount of time
2864 if (mbox->numstatus == 0xFF) { // status not yet available
2865 udelay(25);;
2867 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2868 rmb();
2869 msleep(1);
2873 if (i == 1000) {
2874 con_log(CL_ANN, (KERN_NOTICE
2875 "megaraid mailbox: wait for FW to boot "));
2877 for (i = 0; (mbox->numstatus == 0xFF) &&
2878 (i < MBOX_RESET_WAIT); i++) {
2879 rmb();
2880 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2881 MBOX_RESET_WAIT - i));
2882 msleep(1000);
2885 if (i == MBOX_RESET_WAIT) {
2887 con_log(CL_ANN, (
2888 "\nmegaraid mailbox: status not available\n"));
2890 return -1;
2892 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2896 // wait for maximum 1 second for poll semaphore
2897 if (mbox->poll != 0x77) {
2898 udelay(25);
2900 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2901 rmb();
2902 msleep(1);
2905 if (i == 1000) {
2906 con_log(CL_ANN, (KERN_WARNING
2907 "megaraid mailbox: could not get poll semaphore\n"));
2908 return -1;
2912 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2913 wmb();
2915 // wait for maximum 1 second for acknowledgement
2916 if (RDINDOOR(raid_dev) & 0x2) {
2917 udelay(25);
2919 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2920 rmb();
2921 msleep(1);
2924 if (i == 1000) {
2925 con_log(CL_ANN, (KERN_WARNING
2926 "megaraid mailbox: could not acknowledge\n"));
2927 return -1;
2930 mbox->poll = 0;
2931 mbox->ack = 0x77;
2933 status = mbox->status;
2935 // invalidate the completed command id array. After command
2936 // completion, firmware would write the valid id.
2937 mbox->numstatus = 0xFF;
2938 mbox->status = 0xFF;
2939 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2940 mbox->completed[i] = 0xFF;
2943 return status;
2945 blocked_mailbox:
2947 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2948 return -1;
2953 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2954 * @adapter - controller's soft state
2955 * @raw_mbox - the mailbox
2957 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2958 * controllers. This is a faster version of the synchronous command and
2959 * therefore can be called in interrupt-context as well
2961 static int
2962 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2964 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2965 mbox_t *mbox;
2966 long i;
2969 mbox = raid_dev->mbox;
2971 // return immediately if the mailbox is busy
2972 if (mbox->busy) return -1;
2974 // Copy mailbox data into host structure
2975 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2976 mbox->cmdid = 0xFE;
2977 mbox->busy = 1;
2978 mbox->poll = 0;
2979 mbox->ack = 0;
2980 mbox->numstatus = 0xFF;
2981 mbox->status = 0xFF;
2983 wmb();
2984 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2986 for (i = 0; i < 0xFFFFF; i++) {
2987 if (mbox->numstatus != 0xFF) break;
2990 if (i == 0xFFFFF) {
2991 // We may need to re-calibrate the counter
2992 con_log(CL_ANN, (KERN_CRIT
2993 "megaraid: fast sync command timed out\n"));
2996 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2997 wmb();
2999 return mbox->status;
3004 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
3005 * @raid_dev - RAID device (HBA) soft state
3007 * wait until the controller's mailbox is available to accept more commands.
3008 * wait for at most 1 second
3010 static int
3011 megaraid_busywait_mbox(mraid_device_t *raid_dev)
3013 mbox_t *mbox = raid_dev->mbox;
3014 int i = 0;
3016 if (mbox->busy) {
3017 udelay(25);
3018 for (i = 0; mbox->busy && i < 1000; i++)
3019 msleep(1);
3022 if (i < 1000) return 0;
3023 else return -1;
3028 * megaraid_mbox_product_info - some static information about the controller
3029 * @adapter - our soft state
3031 * issue commands to the controller to grab some parameters required by our
3032 * caller.
3034 static int
3035 megaraid_mbox_product_info(adapter_t *adapter)
3037 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3038 mbox_t *mbox;
3039 uint8_t raw_mbox[sizeof(mbox_t)];
3040 mraid_pinfo_t *pinfo;
3041 dma_addr_t pinfo_dma_h;
3042 mraid_inquiry3_t *mraid_inq3;
3043 int i;
3046 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3047 mbox = (mbox_t *)raw_mbox;
3050 * Issue an ENQUIRY3 command to find out certain adapter parameters,
3051 * e.g., max channels, max commands etc.
3053 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3054 &pinfo_dma_h);
3056 if (pinfo == NULL) {
3057 con_log(CL_ANN, (KERN_WARNING
3058 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3059 __LINE__));
3061 return -1;
3063 memset(pinfo, 0, sizeof(mraid_pinfo_t));
3065 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3066 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3068 raw_mbox[0] = FC_NEW_CONFIG;
3069 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
3070 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3072 // Issue the command
3073 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3075 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3077 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3078 pinfo, pinfo_dma_h);
3080 return -1;
3084 * Collect information about state of each physical drive
3085 * attached to the controller. We will expose all the disks
3086 * which are not part of RAID
3088 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3089 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3090 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3094 * Get product info for information like number of channels,
3095 * maximum commands supported.
3097 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3098 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3100 raw_mbox[0] = FC_NEW_CONFIG;
3101 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3103 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3105 con_log(CL_ANN, (KERN_WARNING
3106 "megaraid: product info failed\n"));
3108 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3109 pinfo, pinfo_dma_h);
3111 return -1;
3115 * Setup some parameters for host, as required by our caller
3117 adapter->max_channel = pinfo->nchannels;
3120 * we will export all the logical drives on a single channel.
3121 * Add 1 since inquires do not come for inititor ID
3123 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3124 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3127 * These are the maximum outstanding commands for the scsi-layer
3129 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3131 memset(adapter->fw_version, 0, VERSION_SIZE);
3132 memset(adapter->bios_version, 0, VERSION_SIZE);
3134 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3135 adapter->fw_version[4] = 0;
3137 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3138 adapter->bios_version[4] = 0;
3140 con_log(CL_ANN, (KERN_NOTICE
3141 "megaraid: fw version:[%s] bios version:[%s]\n",
3142 adapter->fw_version, adapter->bios_version));
3144 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3145 pinfo_dma_h);
3147 return 0;
3153 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3154 * @adapter - soft state for the controller
3156 * this routine check whether the controller in question supports extended
3157 * ( > 10 bytes ) CDBs
3159 static int
3160 megaraid_mbox_extended_cdb(adapter_t *adapter)
3162 mbox_t *mbox;
3163 uint8_t raw_mbox[sizeof(mbox_t)];
3164 int rval;
3166 mbox = (mbox_t *)raw_mbox;
3168 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3169 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3171 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3173 raw_mbox[0] = MAIN_MISC_OPCODE;
3174 raw_mbox[2] = SUPPORT_EXT_CDB;
3177 * Issue the command
3179 rval = 0;
3180 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3181 rval = -1;
3184 return rval;
3189 * megaraid_mbox_support_ha - Do we support clustering
3190 * @adapter - soft state for the controller
3191 * @init_id - ID of the initiator
3193 * Determine if the firmware supports clustering and the ID of the initiator.
3195 static int
3196 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3198 mbox_t *mbox;
3199 uint8_t raw_mbox[sizeof(mbox_t)];
3200 int rval;
3203 mbox = (mbox_t *)raw_mbox;
3205 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3207 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3209 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3211 raw_mbox[0] = GET_TARGET_ID;
3213 // Issue the command
3214 *init_id = 7;
3215 rval = -1;
3216 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3218 *init_id = *(uint8_t *)adapter->ibuf;
3220 con_log(CL_ANN, (KERN_INFO
3221 "megaraid: cluster firmware, initiator ID: %d\n",
3222 *init_id));
3224 rval = 0;
3227 return rval;
3232 * megaraid_mbox_support_random_del - Do we support random deletion
3233 * @adapter - soft state for the controller
3235 * Determine if the firmware supports random deletion
3236 * Return: 1 is operation supported, 0 otherwise
3238 static int
3239 megaraid_mbox_support_random_del(adapter_t *adapter)
3241 mbox_t *mbox;
3242 uint8_t raw_mbox[sizeof(mbox_t)];
3243 int rval;
3246 mbox = (mbox_t *)raw_mbox;
3248 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3250 raw_mbox[0] = FC_DEL_LOGDRV;
3251 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3253 // Issue the command
3254 rval = 0;
3255 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3257 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3259 rval = 1;
3262 return rval;
3267 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3268 * @adapter - soft state for the controller
3270 * Find out the maximum number of scatter-gather elements supported by the
3271 * firmware
3273 static int
3274 megaraid_mbox_get_max_sg(adapter_t *adapter)
3276 mbox_t *mbox;
3277 uint8_t raw_mbox[sizeof(mbox_t)];
3278 int nsg;
3281 mbox = (mbox_t *)raw_mbox;
3283 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3285 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3287 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3289 raw_mbox[0] = MAIN_MISC_OPCODE;
3290 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3292 // Issue the command
3293 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3294 nsg = *(uint8_t *)adapter->ibuf;
3296 else {
3297 nsg = MBOX_DEFAULT_SG_SIZE;
3300 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3302 return nsg;
3307 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3308 * @adapter - soft state for the controller
3310 * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
3311 * can be exported as regular SCSI channels
3313 static void
3314 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3316 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3317 mbox_t *mbox;
3318 uint8_t raw_mbox[sizeof(mbox_t)];
3321 mbox = (mbox_t *)raw_mbox;
3323 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3325 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3327 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3329 raw_mbox[0] = CHNL_CLASS;
3330 raw_mbox[2] = GET_CHNL_CLASS;
3332 // Issue the command. If the command fails, all channels are RAID
3333 // channels
3334 raid_dev->channel_class = 0xFF;
3335 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3336 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3339 return;
3344 * megaraid_mbox_flush_cache - flush adapter and disks cache
3345 * @param adapter : soft state for the controller
3347 * Flush adapter cache followed by disks cache
3349 static void
3350 megaraid_mbox_flush_cache(adapter_t *adapter)
3352 mbox_t *mbox;
3353 uint8_t raw_mbox[sizeof(mbox_t)];
3356 mbox = (mbox_t *)raw_mbox;
3358 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3360 raw_mbox[0] = FLUSH_ADAPTER;
3362 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3363 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3366 raw_mbox[0] = FLUSH_SYSTEM;
3368 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3369 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3372 return;
3377 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3378 * @param adapter : controllers' soft state
3379 * @param scb : SCB to be displayed
3380 * @param level : debug level for console print
3382 * Diplay information about the given SCB iff the current debug level is
3383 * verbose
3385 static void
3386 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3388 mbox_ccb_t *ccb;
3389 struct scsi_cmnd *scp;
3390 mbox_t *mbox;
3391 int level;
3392 int i;
3395 ccb = (mbox_ccb_t *)scb->ccb;
3396 scp = scb->scp;
3397 mbox = ccb->mbox;
3399 level = CL_DLEVEL3;
3401 con_log(level, (KERN_NOTICE
3402 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3403 mbox->cmd, scb->sno));
3405 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3406 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3407 mbox->numsge));
3409 if (!scp) return;
3411 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3413 for (i = 0; i < scp->cmd_len; i++) {
3414 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3417 con_log(level, ("\n"));
3419 return;
3424 * megaraid_mbox_setup_device_map - manage device ids
3425 * @adapter : Driver's soft state
3427 * Manange the device ids to have an appropraite mapping between the kernel
3428 * scsi addresses and megaraid scsi and logical drive addresses. We export
3429 * scsi devices on their actual addresses, whereas the logical drives are
3430 * exported on a virtual scsi channel.
3432 static void
3433 megaraid_mbox_setup_device_map(adapter_t *adapter)
3435 uint8_t c;
3436 uint8_t t;
3439 * First fill the values on the logical drive channel
3441 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3442 adapter->device_ids[adapter->max_channel][t] =
3443 (t < adapter->init_id) ? t : t - 1;
3445 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3448 * Fill the values on the physical devices channels
3450 for (c = 0; c < adapter->max_channel; c++)
3451 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3452 adapter->device_ids[c][t] = (c << 8) | t;
3457 * END: internal commands library
3461 * START: Interface for the common management module
3463 * This is the module, which interfaces with the common mangement module to
3464 * provide support for ioctl and sysfs
3468 * megaraid_cmm_register - register with the mangement module
3469 * @param adapter : HBA soft state
3471 * Register with the management module, which allows applications to issue
3472 * ioctl calls to the drivers. This interface is used by the management module
3473 * to setup sysfs support as well.
3475 static int
3476 megaraid_cmm_register(adapter_t *adapter)
3478 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3479 mraid_mmadp_t adp;
3480 scb_t *scb;
3481 mbox_ccb_t *ccb;
3482 int rval;
3483 int i;
3485 // Allocate memory for the base list of scb for management module.
3486 adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
3487 GFP_KERNEL);
3489 if (adapter->uscb_list == NULL) {
3490 con_log(CL_ANN, (KERN_WARNING
3491 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3492 __LINE__));
3493 return -1;
3495 memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
3498 // Initialize the synchronization parameters for resources for
3499 // commands for management module
3500 INIT_LIST_HEAD(&adapter->uscb_pool);
3502 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3506 // link all the packets. Note, CCB for commands, coming from the
3507 // commom management module, mailbox physical address are already
3508 // setup by it. We just need placeholder for that in our local command
3509 // control blocks
3510 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3512 scb = adapter->uscb_list + i;
3513 ccb = raid_dev->uccb_list + i;
3515 scb->ccb = (caddr_t)ccb;
3516 ccb->mbox64 = raid_dev->umbox64 + i;
3517 ccb->mbox = &ccb->mbox64->mbox32;
3518 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3520 scb->gp = 0;
3522 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3523 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3524 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3526 scb->scp = NULL;
3527 scb->state = SCB_FREE;
3528 scb->dma_direction = PCI_DMA_NONE;
3529 scb->dma_type = MRAID_DMA_NONE;
3530 scb->dev_channel = -1;
3531 scb->dev_target = -1;
3533 // put scb in the free pool
3534 list_add_tail(&scb->list, &adapter->uscb_pool);
3537 adp.unique_id = adapter->unique_id;
3538 adp.drvr_type = DRVRTYPE_MBOX;
3539 adp.drvr_data = (unsigned long)adapter;
3540 adp.pdev = adapter->pdev;
3541 adp.issue_uioc = megaraid_mbox_mm_handler;
3542 adp.timeout = 300;
3543 adp.max_kioc = MBOX_MAX_USER_CMDS;
3545 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3547 con_log(CL_ANN, (KERN_WARNING
3548 "megaraid mbox: did not register with CMM\n"));
3550 kfree(adapter->uscb_list);
3553 return rval;
3558 * megaraid_cmm_unregister - un-register with the mangement module
3559 * @param adapter : HBA soft state
3561 * Un-register with the management module.
3562 * FIXME: mgmt module must return failure for unregister if it has pending
3563 * commands in LLD
3565 static int
3566 megaraid_cmm_unregister(adapter_t *adapter)
3568 kfree(adapter->uscb_list);
3569 mraid_mm_unregister_adp(adapter->unique_id);
3570 return 0;
3575 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3576 * @param drvr_data : LLD specific data
3577 * @param kioc : CMM interface packet
3578 * @param action : command action
3580 * This routine is invoked whenever the Common Mangement Module (CMM) has a
3581 * command for us. The 'action' parameter specifies if this is a new command
3582 * or otherwise.
3584 static int
3585 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3587 adapter_t *adapter;
3589 if (action != IOCTL_ISSUE) {
3590 con_log(CL_ANN, (KERN_WARNING
3591 "megaraid: unsupported management action:%#2x\n",
3592 action));
3593 return (-ENOTSUPP);
3596 adapter = (adapter_t *)drvr_data;
3598 // make sure this adapter is not being detached right now.
3599 if (atomic_read(&adapter->being_detached)) {
3600 con_log(CL_ANN, (KERN_WARNING
3601 "megaraid: reject management request, detaching\n"));
3602 return (-ENODEV);
3605 switch (kioc->opcode) {
3607 case GET_ADAP_INFO:
3609 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3610 (unsigned long)kioc->buf_vaddr);
3612 kioc->done(kioc);
3614 return kioc->status;
3616 case MBOX_CMD:
3618 return megaraid_mbox_mm_command(adapter, kioc);
3620 default:
3621 kioc->status = (-EINVAL);
3622 kioc->done(kioc);
3623 return (-EINVAL);
3626 return 0; // not reached
3630 * megaraid_mbox_mm_command - issues commands routed through CMM
3631 * @param adapter : HBA soft state
3632 * @param kioc : management command packet
3634 * Issues commands, which are routed through the management module.
3636 static int
3637 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3639 struct list_head *head = &adapter->uscb_pool;
3640 mbox64_t *mbox64;
3641 uint8_t *raw_mbox;
3642 scb_t *scb;
3643 mbox_ccb_t *ccb;
3644 unsigned long flags;
3646 // detach one scb from free pool
3647 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3649 if (list_empty(head)) { // should never happen because of CMM
3651 con_log(CL_ANN, (KERN_WARNING
3652 "megaraid mbox: bug in cmm handler, lost resources\n"));
3654 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3656 return (-EINVAL);
3659 scb = list_entry(head->next, scb_t, list);
3660 list_del_init(&scb->list);
3662 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3664 scb->state = SCB_ACTIVE;
3665 scb->dma_type = MRAID_DMA_NONE;
3666 scb->dma_direction = PCI_DMA_NONE;
3668 ccb = (mbox_ccb_t *)scb->ccb;
3669 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3670 raw_mbox = (uint8_t *)&mbox64->mbox32;
3672 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3674 scb->gp = (unsigned long)kioc;
3677 * If it is a logdrv random delete operation, we have to wait till
3678 * there are no outstanding cmds at the fw and then issue it directly
3680 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3682 if (wait_till_fw_empty(adapter)) {
3683 con_log(CL_ANN, (KERN_NOTICE
3684 "megaraid mbox: LD delete, timed out\n"));
3686 kioc->status = -ETIME;
3688 scb->status = -1;
3690 megaraid_mbox_mm_done(adapter, scb);
3692 return (-ETIME);
3695 INIT_LIST_HEAD(&scb->list);
3697 scb->state = SCB_ISSUED;
3698 if (mbox_post_cmd(adapter, scb) != 0) {
3700 con_log(CL_ANN, (KERN_NOTICE
3701 "megaraid mbox: LD delete, mailbox busy\n"));
3703 kioc->status = -EBUSY;
3705 scb->status = -1;
3707 megaraid_mbox_mm_done(adapter, scb);
3709 return (-EBUSY);
3712 return 0;
3715 // put the command on the pending list and execute
3716 megaraid_mbox_runpendq(adapter, scb);
3718 return 0;
3722 static int
3723 wait_till_fw_empty(adapter_t *adapter)
3725 unsigned long flags = 0;
3726 int i;
3730 * Set the quiescent flag to stop issuing cmds to FW.
3732 spin_lock_irqsave(&adapter->lock, flags);
3733 adapter->quiescent++;
3734 spin_unlock_irqrestore(&adapter->lock, flags);
3737 * Wait till there are no more cmds outstanding at FW. Try for at most
3738 * 60 seconds
3740 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3741 con_log(CL_DLEVEL1, (KERN_INFO
3742 "megaraid: FW has %d pending commands\n",
3743 adapter->outstanding_cmds));
3745 msleep(1000);
3748 return adapter->outstanding_cmds;
3753 * megaraid_mbox_mm_done - callback for CMM commands
3754 * @adapter : HBA soft state
3755 * @scb : completed command
3757 * Callback routine for internal commands originated from the management
3758 * module.
3760 static void
3761 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3763 uioc_t *kioc;
3764 mbox64_t *mbox64;
3765 uint8_t *raw_mbox;
3766 unsigned long flags;
3768 kioc = (uioc_t *)scb->gp;
3769 kioc->status = 0;
3770 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3771 mbox64->mbox32.status = scb->status;
3772 raw_mbox = (uint8_t *)&mbox64->mbox32;
3775 // put scb in the free pool
3776 scb->state = SCB_FREE;
3777 scb->scp = NULL;
3779 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3781 list_add(&scb->list, &adapter->uscb_pool);
3783 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3785 // if a delete logical drive operation succeeded, restart the
3786 // controller
3787 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3789 adapter->quiescent--;
3791 megaraid_mbox_runpendq(adapter, NULL);
3794 kioc->done(kioc);
3796 return;
3801 * gather_hbainfo - HBA characteristics for the applications
3802 * @param adapter : HBA soft state
3803 * @param hinfo : pointer to the caller's host info strucuture
3805 static int
3806 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3808 uint8_t dmajor;
3810 dmajor = megaraid_mbox_version[0];
3812 hinfo->pci_vendor_id = adapter->pdev->vendor;
3813 hinfo->pci_device_id = adapter->pdev->device;
3814 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3815 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3817 hinfo->pci_bus = adapter->pdev->bus->number;
3818 hinfo->pci_dev_fn = adapter->pdev->devfn;
3819 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3820 hinfo->irq = adapter->host->irq;
3821 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3823 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3824 hinfo->host_no = adapter->host->host_no;
3826 return 0;
3830 * END: Interface for the common management module
3836 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3838 * Allocate packets required to issue FW calls whenever the sysfs attributes
3839 * are read. These attributes would require up-to-date information from the
3840 * FW. Also set up resources for mutual exclusion to share these resources and
3841 * the wait queue.
3843 * @param adapter : controller's soft state
3845 * @return 0 on success
3846 * @return -ERROR_CODE on failure
3848 static int
3849 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3851 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3852 int rval = 0;
3854 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3856 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3858 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3859 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3861 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3862 !raid_dev->sysfs_buffer) {
3864 con_log(CL_ANN, (KERN_WARNING
3865 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3866 __LINE__));
3868 rval = -ENOMEM;
3870 megaraid_sysfs_free_resources(adapter);
3873 sema_init(&raid_dev->sysfs_sem, 1);
3875 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3877 return rval;
3882 * megaraid_sysfs_free_resources - free sysfs related resources
3884 * Free packets allocated for sysfs FW commands
3886 * @param adapter : controller's soft state
3888 static void
3889 megaraid_sysfs_free_resources(adapter_t *adapter)
3891 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3893 kfree(raid_dev->sysfs_uioc);
3894 kfree(raid_dev->sysfs_mbox64);
3896 if (raid_dev->sysfs_buffer) {
3897 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3898 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3904 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3906 * Callback routine called in the ISR/tasklet context for get ldmap call
3908 * @param uioc : completed packet
3910 static void
3911 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3913 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3914 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3916 uioc->status = 0;
3918 wake_up(&raid_dev->sysfs_wait_q);
3923 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3925 * Timeout routine to recover and return to application, in case the adapter
3926 * has stopped responding. A timeout of 60 seconds for this command seem like
3927 * a good value
3929 * @param uioc : timed out packet
3931 static void
3932 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
3934 uioc_t *uioc = (uioc_t *)data;
3935 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
3936 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3938 uioc->status = -ETIME;
3940 wake_up(&raid_dev->sysfs_wait_q);
3945 * megaraid_sysfs_get_ldmap - get update logical drive map
3947 * This routine will be called whenever user reads the logical drive
3948 * attributes, go get the current logical drive mapping table from the
3949 * firmware. We use the managment API's to issue commands to the controller.
3951 * NOTE: The commands issuance functionality is not generalized and
3952 * implemented in context of "get ld map" command only. If required, the
3953 * command issuance logical can be trivially pulled out and implemented as a
3954 * standalone libary. For now, this should suffice since there is no other
3955 * user of this interface.
3957 * @param adapter : controller's soft state
3959 * @return 0 on success
3960 * @return -1 on failure
3962 static int
3963 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3965 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3966 uioc_t *uioc;
3967 mbox64_t *mbox64;
3968 mbox_t *mbox;
3969 char *raw_mbox;
3970 struct timer_list sysfs_timer;
3971 struct timer_list *timerp;
3972 caddr_t ldmap;
3973 int rval = 0;
3976 * Allow only one read at a time to go through the sysfs attributes
3978 down(&raid_dev->sysfs_sem);
3980 uioc = raid_dev->sysfs_uioc;
3981 mbox64 = raid_dev->sysfs_mbox64;
3982 ldmap = raid_dev->sysfs_buffer;
3984 memset(uioc, 0, sizeof(uioc_t));
3985 memset(mbox64, 0, sizeof(mbox64_t));
3986 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3988 mbox = &mbox64->mbox32;
3989 raw_mbox = (char *)mbox;
3990 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
3991 uioc->buf_vaddr = (caddr_t)adapter;
3992 uioc->status = -ENODATA;
3993 uioc->done = megaraid_sysfs_get_ldmap_done;
3996 * Prepare the mailbox packet to get the current logical drive mapping
3997 * table
3999 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
4001 raw_mbox[0] = FC_DEL_LOGDRV;
4002 raw_mbox[2] = OP_GET_LDID_MAP;
4005 * Setup a timer to recover from a non-responding controller
4007 timerp = &sysfs_timer;
4008 init_timer(timerp);
4010 timerp->function = megaraid_sysfs_get_ldmap_timeout;
4011 timerp->data = (unsigned long)uioc;
4012 timerp->expires = jiffies + 60 * HZ;
4014 add_timer(timerp);
4017 * Send the command to the firmware
4019 rval = megaraid_mbox_mm_command(adapter, uioc);
4021 if (rval == 0) { // command successfully issued
4022 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4025 * Check if the command timed out
4027 if (uioc->status == -ETIME) {
4028 con_log(CL_ANN, (KERN_NOTICE
4029 "megaraid: sysfs get ld map timed out\n"));
4031 rval = -ETIME;
4033 else {
4034 rval = mbox->status;
4037 if (rval == 0) {
4038 memcpy(raid_dev->curr_ldmap, ldmap,
4039 sizeof(raid_dev->curr_ldmap));
4041 else {
4042 con_log(CL_ANN, (KERN_NOTICE
4043 "megaraid: get ld map failed with %x\n", rval));
4046 else {
4047 con_log(CL_ANN, (KERN_NOTICE
4048 "megaraid: could not issue ldmap command:%x\n", rval));
4052 del_timer_sync(timerp);
4054 up(&raid_dev->sysfs_sem);
4056 return rval;
4061 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4063 * Display the handle used by the applications while executing management
4064 * tasks on the adapter. We invoke a management module API to get the adapter
4065 * handle, since we do not interface with applications directly.
4067 * @param cdev : class device object representation for the host
4068 * @param buf : buffer to send data to
4070 static ssize_t
4071 megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
4073 struct Scsi_Host *shost = class_to_shost(cdev);
4074 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4075 uint32_t app_hndl;
4077 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4079 return snprintf(buf, 8, "%u\n", app_hndl);
4084 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4086 * Display the logical drive number for the device in question, if it a valid
4087 * logical drive. For physical devices, "-1" is returned
4088 * The logical drive number is displayed in following format
4090 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4091 * <int> <int> <int> <int>
4093 * @param dev : device object representation for the scsi device
4094 * @param buf : buffer to send data to
4096 static ssize_t
4097 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4099 struct scsi_device *sdev = to_scsi_device(dev);
4100 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4101 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4102 int scsi_id = -1;
4103 int logical_drv = -1;
4104 int ldid_map = -1;
4105 uint32_t app_hndl = 0;
4106 int mapped_sdev_id;
4107 int rval;
4108 int i;
4110 if (raid_dev->random_del_supported &&
4111 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4113 rval = megaraid_sysfs_get_ldmap(adapter);
4114 if (rval == 0) {
4116 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4118 mapped_sdev_id = sdev->id;
4120 if (sdev->id > adapter->init_id) {
4121 mapped_sdev_id -= 1;
4124 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4126 scsi_id = sdev->id;
4128 logical_drv = i;
4130 ldid_map = raid_dev->curr_ldmap[i];
4132 app_hndl = mraid_mm_adapter_app_handle(
4133 adapter->unique_id);
4135 break;
4139 else {
4140 con_log(CL_ANN, (KERN_NOTICE
4141 "megaraid: sysfs get ld map failed: %x\n",
4142 rval));
4146 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4147 ldid_map, app_hndl);
4152 * END: Mailbox Low Level Driver
4154 module_init(megaraid_init);
4155 module_exit(megaraid_exit);
4157 /* vim: set ts=8 sw=8 tw=78 ai si: */