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.1 (Nov 04 2004)
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
64 * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
67 * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
70 * For history of changes, see Documentation/ChangeLog.megaraid
73 #include "megaraid_mbox.h"
75 static int megaraid_init(void);
76 static void megaraid_exit(void);
78 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
79 static void megaraid_detach_one(struct pci_dev *);
80 static void megaraid_mbox_shutdown(struct device *);
82 static int megaraid_io_attach(adapter_t *);
83 static void megaraid_io_detach(adapter_t *);
85 static int megaraid_init_mbox(adapter_t *);
86 static void megaraid_fini_mbox(adapter_t *);
88 static int megaraid_alloc_cmd_packets(adapter_t *);
89 static void megaraid_free_cmd_packets(adapter_t *);
91 static int megaraid_mbox_setup_dma_pools(adapter_t *);
92 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
94 static int megaraid_abort_handler(struct scsi_cmnd *);
95 static int megaraid_reset_handler(struct scsi_cmnd *);
97 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
98 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
99 static int megaraid_busywait_mbox(mraid_device_t *);
100 static int megaraid_mbox_product_info(adapter_t *);
101 static int megaraid_mbox_extended_cdb(adapter_t *);
102 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
103 static int megaraid_mbox_support_random_del(adapter_t *);
104 static int megaraid_mbox_get_max_sg(adapter_t *);
105 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
106 static void megaraid_mbox_flush_cache(adapter_t *);
108 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
109 static void megaraid_mbox_setup_device_map(adapter_t *);
111 static int megaraid_queue_command(struct scsi_cmnd *,
112 void (*)(struct scsi_cmnd *));
113 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
114 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
115 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
117 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
120 static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
122 static void megaraid_mbox_dpc(unsigned long);
124 static int megaraid_cmm_register(adapter_t *);
125 static int megaraid_cmm_unregister(adapter_t *);
126 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
127 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
128 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
129 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
130 static int wait_till_fw_empty(adapter_t *);
134 MODULE_AUTHOR("LSI Logic Corporation");
135 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
136 MODULE_LICENSE("GPL");
137 MODULE_VERSION(MEGARAID_VERSION);
140 * ### modules parameters for driver ###
144 * Set to enable driver to expose unconfigured disk to kernel
146 static int megaraid_expose_unconf_disks = 0;
147 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
148 MODULE_PARM_DESC(unconf_disks,
149 "Set to expose unconfigured disks to kernel (default=0)");
152 * driver wait time if the adapter's mailbox is busy
154 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
155 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
156 MODULE_PARM_DESC(busy_wait,
157 "Max wait for mailbox in microseconds if busy (default=10)");
160 * number of sectors per IO command
162 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
163 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
164 MODULE_PARM_DESC(max_sectors,
165 "Maximum number of sectors per IO command (default=128)");
168 * number of commands per logical unit
170 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
171 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
172 MODULE_PARM_DESC(cmd_per_lun,
173 "Maximum number of commands per logical unit (default=64)");
177 * Fast driver load option, skip scanning for physical devices during load.
178 * This would result in non-disk devices being skipped during driver load
179 * time. These can be later added though, using /proc/scsi/scsi
181 static unsigned int megaraid_fast_load = 0;
182 module_param_named(fast_load, megaraid_fast_load, int, 0);
183 MODULE_PARM_DESC(fast_load,
184 "Faster loading of the driver, skips physical devices! (default=0)");
188 * mraid_debug level - threshold for amount of information to be displayed by
189 * the driver. This level can be changed through modules parameters, ioctl or
190 * sysfs/proc interface. By default, print the announcement messages only.
192 int mraid_debug_level = CL_ANN;
193 module_param_named(debug_level, mraid_debug_level, int, 0);
194 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
197 * ### global data ###
199 static uint8_t megaraid_mbox_version[8] =
200 { 0x02, 0x20, 0x04, 0x00, 9, 27, 20, 4 };
204 * PCI table for all supported controllers.
206 static struct pci_device_id pci_id_table_g[] = {
209 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
211 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
214 PCI_VENDOR_ID_LSI_LOGIC,
215 PCI_DEVICE_ID_PERC4_SC,
217 PCI_SUBSYS_ID_PERC4_SC,
220 PCI_VENDOR_ID_LSI_LOGIC,
221 PCI_DEVICE_ID_PERC4_DC,
223 PCI_SUBSYS_ID_PERC4_DC,
226 PCI_VENDOR_ID_LSI_LOGIC,
227 PCI_DEVICE_ID_PERC4_QC,
229 PCI_SUBSYS_ID_PERC4_QC,
233 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
235 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
239 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
241 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
245 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
247 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
251 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
253 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
257 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
259 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
263 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
265 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
268 PCI_VENDOR_ID_LSI_LOGIC,
269 PCI_DEVICE_ID_PERC4E_DC_320_2E,
271 PCI_SUBSYS_ID_PERC4E_DC_320_2E,
274 PCI_VENDOR_ID_LSI_LOGIC,
275 PCI_DEVICE_ID_PERC4E_SC_320_1E,
277 PCI_SUBSYS_ID_PERC4E_SC_320_1E,
281 PCI_DEVICE_ID_AMI_MEGARAID3,
283 PCI_SUBSYS_ID_PERC3_QC,
287 PCI_DEVICE_ID_AMI_MEGARAID3,
289 PCI_SUBSYS_ID_PERC3_DC,
293 PCI_DEVICE_ID_AMI_MEGARAID3,
295 PCI_SUBSYS_ID_PERC3_SC,
299 PCI_DEVICE_ID_AMI_MEGARAID3,
301 PCI_SUBSYS_ID_PERC3_SC,
304 PCI_VENDOR_ID_LSI_LOGIC,
305 PCI_DEVICE_ID_MEGARAID_SCSI_320_0,
306 PCI_VENDOR_ID_LSI_LOGIC,
307 PCI_SUBSYS_ID_MEGARAID_SCSI_320_0,
310 PCI_VENDOR_ID_LSI_LOGIC,
311 PCI_DEVICE_ID_MEGARAID_SCSI_320_1,
312 PCI_VENDOR_ID_LSI_LOGIC,
313 PCI_SUBSYS_ID_MEGARAID_SCSI_320_1,
316 PCI_VENDOR_ID_LSI_LOGIC,
317 PCI_DEVICE_ID_MEGARAID_SCSI_320_2,
318 PCI_VENDOR_ID_LSI_LOGIC,
319 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2,
322 PCI_VENDOR_ID_LSI_LOGIC,
323 PCI_DEVICE_ID_MEGARAID_SCSI_320_0x,
324 PCI_VENDOR_ID_LSI_LOGIC,
325 PCI_SUBSYS_ID_MEGARAID_SCSI_320_0x,
328 PCI_VENDOR_ID_LSI_LOGIC,
329 PCI_DEVICE_ID_MEGARAID_SCSI_320_2x,
330 PCI_VENDOR_ID_LSI_LOGIC,
331 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2x,
334 PCI_VENDOR_ID_LSI_LOGIC,
335 PCI_DEVICE_ID_MEGARAID_SCSI_320_4x,
336 PCI_VENDOR_ID_LSI_LOGIC,
337 PCI_SUBSYS_ID_MEGARAID_SCSI_320_4x,
340 PCI_VENDOR_ID_LSI_LOGIC,
341 PCI_DEVICE_ID_MEGARAID_SCSI_320_1E,
342 PCI_VENDOR_ID_LSI_LOGIC,
343 PCI_SUBSYS_ID_MEGARAID_SCSI_320_1E,
346 PCI_VENDOR_ID_LSI_LOGIC,
347 PCI_DEVICE_ID_MEGARAID_SCSI_320_2E,
348 PCI_VENDOR_ID_LSI_LOGIC,
349 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2E,
352 PCI_VENDOR_ID_LSI_LOGIC,
353 PCI_DEVICE_ID_MEGARAID_I4_133_RAID,
354 PCI_VENDOR_ID_LSI_LOGIC,
355 PCI_SUBSYS_ID_MEGARAID_I4_133_RAID,
358 PCI_VENDOR_ID_LSI_LOGIC,
359 PCI_DEVICE_ID_MEGARAID_SATA_150_4,
360 PCI_VENDOR_ID_LSI_LOGIC,
361 PCI_SUBSYS_ID_MEGARAID_SATA_150_4,
364 PCI_VENDOR_ID_LSI_LOGIC,
365 PCI_DEVICE_ID_MEGARAID_SATA_150_6,
366 PCI_VENDOR_ID_LSI_LOGIC,
367 PCI_SUBSYS_ID_MEGARAID_SATA_150_6,
370 PCI_VENDOR_ID_LSI_LOGIC,
371 PCI_DEVICE_ID_MEGARAID_SATA_300_4x,
372 PCI_VENDOR_ID_LSI_LOGIC,
373 PCI_SUBSYS_ID_MEGARAID_SATA_300_4x,
376 PCI_VENDOR_ID_LSI_LOGIC,
377 PCI_DEVICE_ID_MEGARAID_SATA_300_8x,
378 PCI_VENDOR_ID_LSI_LOGIC,
379 PCI_SUBSYS_ID_MEGARAID_SATA_300_8x,
382 PCI_VENDOR_ID_LSI_LOGIC,
383 PCI_DEVICE_ID_INTEL_RAID_SRCU42X,
385 PCI_SUBSYS_ID_INTEL_RAID_SRCU42X,
388 PCI_VENDOR_ID_LSI_LOGIC,
389 PCI_DEVICE_ID_INTEL_RAID_SRCS16,
391 PCI_SUBSYS_ID_INTEL_RAID_SRCS16,
394 PCI_VENDOR_ID_LSI_LOGIC,
395 PCI_DEVICE_ID_INTEL_RAID_SRCU42E,
397 PCI_SUBSYS_ID_INTEL_RAID_SRCU42E,
400 PCI_VENDOR_ID_LSI_LOGIC,
401 PCI_DEVICE_ID_INTEL_RAID_SRCZCRX,
403 PCI_SUBSYS_ID_INTEL_RAID_SRCZCRX,
406 PCI_VENDOR_ID_LSI_LOGIC,
407 PCI_DEVICE_ID_INTEL_RAID_SRCS28X,
409 PCI_SUBSYS_ID_INTEL_RAID_SRCS28X,
412 PCI_VENDOR_ID_LSI_LOGIC,
413 PCI_DEVICE_ID_INTEL_RAID_SROMBU42E_ALIEF,
415 PCI_SUBSYS_ID_INTEL_RAID_SROMBU42E_ALIEF,
418 PCI_VENDOR_ID_LSI_LOGIC,
419 PCI_DEVICE_ID_INTEL_RAID_SROMBU42E_HARWICH,
421 PCI_SUBSYS_ID_INTEL_RAID_SROMBU42E_HARWICH,
424 PCI_VENDOR_ID_LSI_LOGIC,
425 PCI_DEVICE_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
427 PCI_SUBSYS_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
430 PCI_VENDOR_ID_LSI_LOGIC,
431 PCI_DEVICE_ID_FSC_MEGARAID_PCI_EXPRESS_ROMB,
433 PCI_SUBSYS_ID_FSC_MEGARAID_PCI_EXPRESS_ROMB,
436 PCI_VENDOR_ID_LSI_LOGIC,
437 PCI_DEVICE_ID_MEGARAID_ACER_ROMB_2E,
439 PCI_SUBSYS_ID_MEGARAID_ACER_ROMB_2E,
441 {0} /* Terminating entry */
443 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
446 static struct pci_driver megaraid_pci_driver_g = {
448 .id_table = pci_id_table_g,
449 .probe = megaraid_probe_one,
450 .remove = __devexit_p(megaraid_detach_one),
452 .shutdown = megaraid_mbox_shutdown,
458 * Scsi host template for megaraid unified driver
460 static struct scsi_host_template megaraid_template_g = {
461 .module = THIS_MODULE,
462 .name = "LSI Logic MegaRAID driver",
463 .proc_name = "megaraid",
464 .queuecommand = megaraid_queue_command,
465 .eh_abort_handler = megaraid_abort_handler,
466 .eh_device_reset_handler = megaraid_reset_handler,
467 .eh_bus_reset_handler = megaraid_reset_handler,
468 .eh_host_reset_handler = megaraid_reset_handler,
469 .use_clustering = ENABLE_CLUSTERING,
474 * megaraid_init - module load hook
476 * We register ourselves as hotplug enabled module and let PCI subsystem
477 * discover our adaters
484 // Announce the driver version
485 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
486 MEGARAID_EXT_VERSION));
488 // check validity of module parameters
489 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
491 con_log(CL_ANN, (KERN_WARNING
492 "megaraid mailbox: max commands per lun reset to %d\n",
493 MBOX_MAX_SCSI_CMDS));
495 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
499 // register as a PCI hot-plug driver module
500 if ((rval = pci_module_init(&megaraid_pci_driver_g))) {
501 con_log(CL_ANN, (KERN_WARNING
502 "megaraid: could not register hotplug support.\n"));
510 * megaraid_exit - driver unload entry point
512 * We simply unwrap the megaraid_init routine here
517 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
519 // unregister as PCI hotplug driver
520 pci_unregister_driver(&megaraid_pci_driver_g);
527 * megaraid_probe_one - PCI hotplug entry point
528 * @param pdev : handle to this controller's PCI configuration space
529 * @param id : pci device id of the class of controllers
531 * This routine should be called whenever a new adapter is detected by the
532 * PCI hotplug susbsytem.
535 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
540 // detected a new controller
541 con_log(CL_ANN, (KERN_INFO
542 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
543 pdev->vendor, pdev->device, pdev->subsystem_vendor,
544 pdev->subsystem_device));
546 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
547 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
549 if (pci_enable_device(pdev)) {
550 con_log(CL_ANN, (KERN_WARNING
551 "megaraid: pci_enable_device failed\n"));
556 // Enable bus-mastering on this controller
557 pci_set_master(pdev);
559 // Allocate the per driver initialization structure
560 adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
562 if (adapter == NULL) {
563 con_log(CL_ANN, (KERN_WARNING
564 "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
568 memset(adapter, 0, sizeof(adapter_t));
571 // set up PCI related soft state and other pre-known parameters
572 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
573 adapter->irq = pdev->irq;
574 adapter->pdev = pdev;
576 atomic_set(&adapter->being_detached, 0);
578 // Setup the default DMA mask. This would be changed later on
579 // depending on hardware capabilities
580 if (pci_set_dma_mask(adapter->pdev, 0xFFFFFFFF) != 0) {
582 con_log(CL_ANN, (KERN_WARNING
583 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
585 goto out_free_adapter;
589 // Initialize the synchronization lock for kernel and LLD
590 spin_lock_init(&adapter->lock);
591 adapter->host_lock = &adapter->lock;
594 // Initialize the command queues: the list of free SCBs and the list
596 INIT_LIST_HEAD(&adapter->kscb_pool);
597 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
599 INIT_LIST_HEAD(&adapter->pend_list);
600 spin_lock_init(PENDING_LIST_LOCK(adapter));
602 INIT_LIST_HEAD(&adapter->completed_list);
603 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
606 // Start the mailbox based controller
607 if (megaraid_init_mbox(adapter) != 0) {
608 con_log(CL_ANN, (KERN_WARNING
609 "megaraid: maibox adapter did not initialize\n"));
611 goto out_free_adapter;
614 // Register with LSI Common Management Module
615 if (megaraid_cmm_register(adapter) != 0) {
617 con_log(CL_ANN, (KERN_WARNING
618 "megaraid: could not register with management module\n"));
623 // setup adapter handle in PCI soft state
624 pci_set_drvdata(pdev, adapter);
626 // attach with scsi mid-layer
627 if (megaraid_io_attach(adapter) != 0) {
629 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
637 pci_set_drvdata(pdev, NULL);
638 megaraid_cmm_unregister(adapter);
640 megaraid_fini_mbox(adapter);
644 pci_disable_device(pdev);
651 * megaraid_detach_one - release the framework resources and call LLD release
653 * @param pdev : handle for our PCI cofiguration space
655 * This routine is called during driver unload. We free all the allocated
656 * resources and call the corresponding LLD so that it can also release all
659 * This routine is also called from the PCI hotplug system
662 megaraid_detach_one(struct pci_dev *pdev)
665 struct Scsi_Host *host;
668 // Start a rollback on this adapter
669 adapter = pci_get_drvdata(pdev);
672 con_log(CL_ANN, (KERN_CRIT
673 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
674 pdev->vendor, pdev->device, pdev->subsystem_vendor,
675 pdev->subsystem_device));
680 con_log(CL_ANN, (KERN_NOTICE
681 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
682 pdev->vendor, pdev->device, pdev->subsystem_vendor,
683 pdev->subsystem_device));
687 host = adapter->host;
689 // do not allow any more requests from the management module for this
691 // FIXME: How do we account for the request which might still be
693 atomic_set(&adapter->being_detached, 1);
695 // detach from the IO sub-system
696 megaraid_io_detach(adapter);
698 // reset the device state in the PCI structure. We check this
699 // condition when we enter here. If the device state is NULL,
700 // that would mean the device has already been removed
701 pci_set_drvdata(pdev, NULL);
703 // Unregister from common management module
705 // FIXME: this must return success or failure for conditions if there
706 // is a command pending with LLD or not.
707 megaraid_cmm_unregister(adapter);
709 // finalize the mailbox based controller and release all resources
710 megaraid_fini_mbox(adapter);
716 pci_disable_device(pdev);
723 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
724 * @param device : generice driver model device
726 * Shutdown notification, perform flush cache
729 megaraid_mbox_shutdown(struct device *device)
731 adapter_t *adapter = pci_get_drvdata(to_pci_dev(device));
735 con_log(CL_ANN, (KERN_WARNING
736 "megaraid: null device in shutdown\n"));
741 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
744 megaraid_mbox_flush_cache(adapter);
746 con_log(CL_ANN, ("done\n"));
751 * megaraid_io_attach - attach a device with the IO subsystem
752 * @param adapter : controller's soft state
754 * Attach this device with the IO subsystem
757 megaraid_io_attach(adapter_t *adapter)
759 struct Scsi_Host *host;
761 // Initialize SCSI Host structure
762 host = scsi_host_alloc(&megaraid_template_g, 8);
764 con_log(CL_ANN, (KERN_WARNING
765 "megaraid mbox: scsi_register failed\n"));
770 SCSIHOST2ADAP(host) = (caddr_t)adapter;
771 adapter->host = host;
773 // export the parameters required by the mid-layer
774 scsi_assign_lock(host, adapter->host_lock);
775 scsi_set_device(host, &adapter->pdev->dev);
777 host->irq = adapter->irq;
778 host->unique_id = adapter->unique_id;
779 host->can_queue = adapter->max_cmds;
780 host->this_id = adapter->init_id;
781 host->sg_tablesize = adapter->sglen;
782 host->max_sectors = adapter->max_sectors;
783 host->cmd_per_lun = adapter->cmd_per_lun;
784 host->max_channel = adapter->max_channel;
785 host->max_id = adapter->max_target;
786 host->max_lun = adapter->max_lun;
789 // notify mid-layer about the new controller
790 if (scsi_add_host(host, &adapter->pdev->dev)) {
792 con_log(CL_ANN, (KERN_WARNING
793 "megaraid mbox: scsi_add_host failed\n"));
800 scsi_scan_host(host);
807 * megaraid_io_detach - detach a device from the IO subsystem
808 * @param adapter : controller's soft state
810 * Detach this device from the IO subsystem
813 megaraid_io_detach(adapter_t *adapter)
815 struct Scsi_Host *host;
817 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
819 host = adapter->host;
821 scsi_remove_host(host);
828 * START: Mailbox Low Level Driver
830 * This is section specific to the single mailbox based controllers
834 * megaraid_init_mbox - initialize controller
835 * @param adapter - our soft state
837 * . Allocate 16-byte aligned mailbox memory for firmware handshake
838 * . Allocate controller's memory resources
839 * . Find out all initialization data
840 * . Allocate memory required for all the commands
841 * . Use internal library of FW routines, build up complete soft state
844 megaraid_init_mbox(adapter_t *adapter)
846 struct pci_dev *pdev;
847 mraid_device_t *raid_dev;
851 adapter->ito = MBOX_TIMEOUT;
852 pdev = adapter->pdev;
855 * Allocate and initialize the init data structure for mailbox
858 raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
859 if (raid_dev == NULL) return -1;
861 memset(raid_dev, 0, sizeof(mraid_device_t));
864 * Attach the adapter soft state to raid device soft state
866 adapter->raid_device = (caddr_t)raid_dev;
867 raid_dev->fast_load = megaraid_fast_load;
871 raid_dev->baseport = pci_resource_start(pdev, 0);
873 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
875 con_log(CL_ANN, (KERN_WARNING
876 "megaraid: mem region busy\n"));
878 goto out_free_raid_dev;
881 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
883 if (!raid_dev->baseaddr) {
885 con_log(CL_ANN, (KERN_WARNING
886 "megaraid: could not map hba memory\n") );
888 goto out_release_regions;
892 // Setup the rest of the soft state using the library of FW routines
895 // request IRQ and register the interrupt service routine
896 if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
899 con_log(CL_ANN, (KERN_WARNING
900 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
906 // initialize the mutual exclusion lock for the mailbox
907 spin_lock_init(&raid_dev->mailbox_lock);
909 // allocate memory required for commands
910 if (megaraid_alloc_cmd_packets(adapter) != 0) {
915 if (megaraid_mbox_product_info(adapter) != 0) {
919 // Do we support extended CDBs
920 adapter->max_cdb_sz = 10;
921 if (megaraid_mbox_extended_cdb(adapter) == 0) {
922 adapter->max_cdb_sz = 16;
926 * Do we support cluster environment, if we do, what is the initiator
928 * NOTE: In a non-cluster aware firmware environment, the LLD should
929 * return 7 as initiator id.
932 adapter->init_id = -1;
933 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
938 * Prepare the device ids array to have the mapping between the kernel
939 * device address and megaraid device address.
940 * We export the physical devices on their actual addresses. The
941 * logical drives are exported on a virtual SCSI channel
943 megaraid_mbox_setup_device_map(adapter);
945 // If the firmware supports random deletion, update the device id map
946 if (megaraid_mbox_support_random_del(adapter)) {
948 // Change the logical drives numbers in device_ids array one
949 // slot in device_ids is reserved for target id, that's why
951 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
952 adapter->device_ids[adapter->max_channel][i] += 0x80;
954 adapter->device_ids[adapter->max_channel][adapter->init_id] =
959 * find out the maximum number of scatter-gather elements supported by
962 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
964 // enumerate RAID and SCSI channels so that all devices on SCSI
965 // channels can later be exported, including disk devices
966 megaraid_mbox_enum_raid_scsi(adapter);
969 * Other parameters required by upper layer
971 * maximum number of sectors per IO command
973 adapter->max_sectors = megaraid_max_sectors;
976 * number of queued commands per LUN.
978 adapter->cmd_per_lun = megaraid_cmd_per_lun;
980 // Set the DMA mask to 64-bit. All supported controllers as capable of
982 if (pci_set_dma_mask(adapter->pdev, 0xFFFFFFFFFFFFFFFFULL) != 0) {
984 con_log(CL_ANN, (KERN_WARNING
985 "megaraid: could not set DMA mask for 64-bit.\n"));
990 // setup tasklet for DPC
991 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
992 (unsigned long)adapter);
994 con_log(CL_DLEVEL1, (KERN_INFO
995 "megaraid mbox hba successfully initialized\n"));
1000 megaraid_free_cmd_packets(adapter);
1002 free_irq(adapter->irq, adapter);
1004 iounmap(raid_dev->baseaddr);
1005 out_release_regions:
1006 pci_release_regions(pdev);
1015 * megaraid_fini_mbox - undo controller initialization
1016 * @param adapter : our soft state
1019 megaraid_fini_mbox(adapter_t *adapter)
1021 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1024 megaraid_mbox_flush_cache(adapter);
1026 tasklet_kill(&adapter->dpc_h);
1028 megaraid_free_cmd_packets(adapter);
1030 free_irq(adapter->irq, adapter);
1032 iounmap(raid_dev->baseaddr);
1034 pci_release_regions(adapter->pdev);
1043 * megaraid_alloc_cmd_packets - allocate shared mailbox
1044 * @param adapter : soft state of the raid controller
1046 * Allocate and align the shared mailbox. This maibox is used to issue
1047 * all the commands. For IO based controllers, the mailbox is also regsitered
1048 * with the FW. Allocate memory for all commands as well.
1049 * This is our big allocator
1052 megaraid_alloc_cmd_packets(adapter_t *adapter)
1054 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1055 struct pci_dev *pdev;
1056 unsigned long align;
1059 struct mraid_pci_blk *epthru_pci_blk;
1060 struct mraid_pci_blk *sg_pci_blk;
1061 struct mraid_pci_blk *mbox_pci_blk;
1064 pdev = adapter->pdev;
1068 * Allocate the common 16-byte aligned memory for the handshake
1071 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1072 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1074 if (!raid_dev->una_mbox64) {
1075 con_log(CL_ANN, (KERN_WARNING
1076 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1080 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1083 * Align the mailbox at 16-byte boundary
1085 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1087 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1090 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1092 align = ((void *)raid_dev->mbox -
1093 ((void *)&raid_dev->una_mbox64->mbox32));
1095 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1098 // Allocate memory for commands issued internally
1099 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1100 &adapter->ibuf_dma_h);
1101 if (!adapter->ibuf) {
1103 con_log(CL_ANN, (KERN_WARNING
1104 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1107 goto out_free_common_mbox;
1109 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1111 // Allocate memory for our SCSI Command Blocks and their associated
1115 * Allocate memory for the base list of scb. Later allocate memory for
1116 * CCBs and embedded components of each CCB and point the pointers in
1117 * scb to the allocated components
1118 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1119 * since the calling routine does not yet know the number of available
1122 adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
1125 if (adapter->kscb_list == NULL) {
1126 con_log(CL_ANN, (KERN_WARNING
1127 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1131 memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
1133 // memory allocation for our command packets
1134 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1135 con_log(CL_ANN, (KERN_WARNING
1136 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1138 goto out_free_scb_list;
1141 // Adjust the scb pointers and link in the free pool
1142 epthru_pci_blk = raid_dev->epthru_pool;
1143 sg_pci_blk = raid_dev->sg_pool;
1144 mbox_pci_blk = raid_dev->mbox_pool;
1146 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1147 scb = adapter->kscb_list + i;
1148 ccb = raid_dev->ccb_list + i;
1150 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1151 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1152 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1153 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1155 // make sure the mailbox is aligned properly
1156 if (ccb->mbox_dma_h & 0x0F) {
1157 con_log(CL_ANN, (KERN_CRIT
1158 "megaraid mbox: not aligned on 16-bytes\n"));
1160 goto out_teardown_dma_pools;
1163 ccb->epthru = (mraid_epassthru_t *)
1164 epthru_pci_blk[i].vaddr;
1165 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1166 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1167 ccb->pthru_dma_h = ccb->epthru_dma_h;
1170 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1171 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1172 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1174 scb->ccb = (caddr_t)ccb;
1177 scb->sno = i; // command index
1180 scb->state = SCB_FREE;
1181 scb->dma_direction = PCI_DMA_NONE;
1182 scb->dma_type = MRAID_DMA_NONE;
1183 scb->dev_channel = -1;
1184 scb->dev_target = -1;
1186 // put scb in the free pool
1187 list_add_tail(&scb->list, &adapter->kscb_pool);
1192 out_teardown_dma_pools:
1193 megaraid_mbox_teardown_dma_pools(adapter);
1195 kfree(adapter->kscb_list);
1197 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1198 adapter->ibuf_dma_h);
1199 out_free_common_mbox:
1200 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1201 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1208 * megaraid_free_cmd_packets - free memory
1209 * @param adapter : soft state of the raid controller
1211 * Release memory resources allocated for commands
1214 megaraid_free_cmd_packets(adapter_t *adapter)
1216 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1218 megaraid_mbox_teardown_dma_pools(adapter);
1220 kfree(adapter->kscb_list);
1222 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1223 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1225 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1226 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1232 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1233 * @param adapter : HBA soft state
1235 * setup the dma pools for mailbox, passthru and extended passthru structures,
1236 * and scatter-gather lists
1239 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1241 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1242 struct mraid_pci_blk *epthru_pci_blk;
1243 struct mraid_pci_blk *sg_pci_blk;
1244 struct mraid_pci_blk *mbox_pci_blk;
1249 // Allocate memory for 16-bytes aligned mailboxes
1250 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1252 sizeof(mbox64_t) + 16,
1255 if (raid_dev->mbox_pool_handle == NULL) {
1256 goto fail_setup_dma_pool;
1259 mbox_pci_blk = raid_dev->mbox_pool;
1260 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1261 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1262 raid_dev->mbox_pool_handle,
1264 &mbox_pci_blk[i].dma_addr);
1265 if (!mbox_pci_blk[i].vaddr) {
1266 goto fail_setup_dma_pool;
1271 * Allocate memory for each embedded passthru strucuture pointer
1272 * Request for a 128 bytes aligned structure for each passthru command
1274 * Since passthru and extended passthru commands are exclusive, they
1275 * share common memory pool. Passthru structures piggyback on memory
1276 * allocted to extended passthru since passthru is smaller of the two
1278 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1279 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1281 if (raid_dev->epthru_pool_handle == NULL) {
1282 goto fail_setup_dma_pool;
1285 epthru_pci_blk = raid_dev->epthru_pool;
1286 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1287 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1288 raid_dev->epthru_pool_handle,
1290 &epthru_pci_blk[i].dma_addr);
1291 if (!epthru_pci_blk[i].vaddr) {
1292 goto fail_setup_dma_pool;
1297 // Allocate memory for each scatter-gather list. Request for 512 bytes
1298 // alignment for each sg list
1299 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1301 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1304 if (raid_dev->sg_pool_handle == NULL) {
1305 goto fail_setup_dma_pool;
1308 sg_pci_blk = raid_dev->sg_pool;
1309 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1310 sg_pci_blk[i].vaddr = pci_pool_alloc(
1311 raid_dev->sg_pool_handle,
1313 &sg_pci_blk[i].dma_addr);
1314 if (!sg_pci_blk[i].vaddr) {
1315 goto fail_setup_dma_pool;
1321 fail_setup_dma_pool:
1322 megaraid_mbox_teardown_dma_pools(adapter);
1328 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1329 * @param adapter : HBA soft state
1331 * teardown the dma pool for mailbox, passthru and extended passthru
1332 * structures, and scatter-gather lists
1335 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1337 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1338 struct mraid_pci_blk *epthru_pci_blk;
1339 struct mraid_pci_blk *sg_pci_blk;
1340 struct mraid_pci_blk *mbox_pci_blk;
1344 sg_pci_blk = raid_dev->sg_pool;
1345 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1346 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1347 sg_pci_blk[i].dma_addr);
1349 if (raid_dev->sg_pool_handle)
1350 pci_pool_destroy(raid_dev->sg_pool_handle);
1353 epthru_pci_blk = raid_dev->epthru_pool;
1354 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1355 pci_pool_free(raid_dev->epthru_pool_handle,
1356 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1358 if (raid_dev->epthru_pool_handle)
1359 pci_pool_destroy(raid_dev->epthru_pool_handle);
1362 mbox_pci_blk = raid_dev->mbox_pool;
1363 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1364 pci_pool_free(raid_dev->mbox_pool_handle,
1365 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1367 if (raid_dev->mbox_pool_handle)
1368 pci_pool_destroy(raid_dev->mbox_pool_handle);
1375 * megaraid_alloc_scb - detach and return a scb from the free list
1376 * @adapter : controller's soft state
1378 * return the scb from the head of the free list. NULL if there are none
1381 static inline scb_t *
1382 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1384 struct list_head *head = &adapter->kscb_pool;
1386 unsigned long flags;
1388 // detach scb from free pool
1389 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1391 if (list_empty(head)) {
1392 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1396 scb = list_entry(head->next, scb_t, list);
1397 list_del_init(&scb->list);
1399 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1401 scb->state = SCB_ACTIVE;
1403 scb->dma_type = MRAID_DMA_NONE;
1410 * megaraid_dealloc_scb - return the scb to the free pool
1411 * @adapter : controller's soft state
1412 * @scb : scb to be freed
1414 * return the scb back to the free list of scbs. The caller must 'flush' the
1415 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1416 * NOTE NOTE: Make sure the scb is not on any list before calling this
1420 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1422 unsigned long flags;
1424 // put scb in the free pool
1425 scb->state = SCB_FREE;
1427 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1429 list_add(&scb->list, &adapter->kscb_pool);
1431 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1438 * megaraid_mbox_mksgl - make the scatter-gather list
1439 * @adapter - controller's soft state
1440 * @scb - scsi control block
1442 * prepare the scatter-gather list
1445 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1447 struct scatterlist *sgl;
1450 unsigned long offset;
1451 struct scsi_cmnd *scp;
1457 ccb = (mbox_ccb_t *)scb->ccb;
1459 // no mapping required if no data to be transferred
1460 if (!scp->request_buffer || !scp->request_bufflen)
1463 if (!scp->use_sg) { /* scatter-gather list not used */
1465 page = virt_to_page(scp->request_buffer);
1467 offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
1469 ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
1470 scp->request_bufflen,
1471 scb->dma_direction);
1472 scb->dma_type = MRAID_DMA_WBUF;
1475 * We need to handle special 64-bit commands that need a
1479 ccb->sgl64[0].address = ccb->buf_dma_h;
1480 ccb->sgl64[0].length = scp->request_bufflen;
1485 sgl = (struct scatterlist *)scp->request_buffer;
1487 // The number of sg elements returned must not exceed our limit
1488 sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
1489 scb->dma_direction);
1491 if (sgcnt > adapter->sglen) {
1492 con_log(CL_ANN, (KERN_CRIT
1493 "megaraid critical: too many sg elements:%d\n",
1498 scb->dma_type = MRAID_DMA_WSG;
1500 for (i = 0; i < sgcnt; i++, sgl++) {
1501 ccb->sgl64[i].address = sg_dma_address(sgl);
1502 ccb->sgl64[i].length = sg_dma_len(sgl);
1505 // Return count of SG nodes
1511 * mbox_post_cmd - issue a mailbox command
1512 * @adapter - controller's soft state
1513 * @scb - command to be issued
1515 * post the command to the controller if mailbox is availble.
1518 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1520 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1524 unsigned long flags;
1528 ccb = (mbox_ccb_t *)scb->ccb;
1529 mbox = raid_dev->mbox;
1530 mbox64 = raid_dev->mbox64;
1533 * Check for busy mailbox. If it is, return failure - the caller
1534 * should retry later.
1536 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1538 if (unlikely(mbox->busy)) {
1543 } while(mbox->busy && (i < max_mbox_busy_wait));
1547 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1554 // Copy this command's mailbox data into "adapter's" mailbox
1555 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1556 mbox->cmdid = scb->sno;
1558 adapter->outstanding_cmds++;
1560 if (scb->dma_direction == PCI_DMA_TODEVICE) {
1561 if (!scb->scp->use_sg) { // sg list not used
1562 pci_dma_sync_single_for_device(adapter->pdev, ccb->buf_dma_h,
1563 scb->scp->request_bufflen,
1567 pci_dma_sync_sg_for_device(adapter->pdev, scb->scp->request_buffer,
1568 scb->scp->use_sg, PCI_DMA_TODEVICE);
1572 mbox->busy = 1; // Set busy
1577 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1579 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1586 * megaraid_queue_command - generic queue entry point for all LLDs
1587 * @scp : pointer to the scsi command to be executed
1588 * @done : callback routine to be called after the cmd has be completed
1590 * Queue entry point for mailbox based controllers.
1593 megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
1599 adapter = SCP2ADAPTER(scp);
1600 scp->scsi_done = done;
1603 ASSERT(spin_is_locked(adapter->host_lock));
1605 spin_unlock(adapter->host_lock);
1608 * Allocate and build a SCB request
1609 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1610 * not allocate scb. We will return non-zero status in that case.
1611 * NOTE: scb can be null even though certain commands completed
1612 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1613 * return 0 in that case, and we would do the callback right away.
1616 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1619 megaraid_mbox_runpendq(adapter, scb);
1622 spin_lock(adapter->host_lock);
1624 if (!scb) { // command already completed
1634 * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
1636 * @adapter - controller's soft state
1637 * @scp - mid-layer scsi command pointer
1638 * @busy - set if request could not be completed because of lack of
1641 * convert the command issued by mid-layer to format understood by megaraid
1642 * firmware. We also complete certain command without sending them to firmware
1645 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1647 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1652 mraid_passthru_t *pthru;
1656 char skip[] = "skipping";
1657 char scan[] = "scanning";
1662 * Get the appropriate device map for the device this command is
1665 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1668 * Logical drive commands
1671 switch (scp->cmnd[0]) {
1672 case TEST_UNIT_READY:
1674 * Do we support clustering and is the support enabled
1675 * If no, return success always
1678 scp->result = (DID_OK << 16);
1682 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1683 scp->result = (DID_ERROR << 16);
1688 scb->dma_direction = scp->sc_data_direction;
1689 scb->dev_channel = 0xFF;
1690 scb->dev_target = target;
1691 ccb = (mbox_ccb_t *)scb->ccb;
1694 * The command id will be provided by the command
1697 ccb->raw_mbox[0] = CLUSTER_CMD;
1698 ccb->raw_mbox[2] = RESERVATION_STATUS;
1699 ccb->raw_mbox[3] = target;
1705 struct scatterlist *sgl;
1708 sgl = (struct scatterlist *)scp->request_buffer;
1711 (page_address((&sgl[0])->page)
1712 + (&sgl[0])->offset);
1714 memset(vaddr, 0, scp->cmnd[4]);
1717 con_log(CL_ANN, (KERN_WARNING
1718 "megaraid mailbox: invalid sg:%d\n",
1723 memset(scp->request_buffer, 0, scp->cmnd[4]);
1725 scp->result = (DID_OK << 16);
1730 * Display the channel scan for logical drives
1731 * Do not display scan for a channel if already done.
1733 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1735 con_log(CL_ANN, (KERN_INFO
1736 "scsi[%d]: scanning scsi channel %d",
1737 adapter->host->host_no,
1741 " [virtual] for logical drives\n"));
1743 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1750 * Do not allow LUN > 0 for logical drives and
1751 * requests for more than 40 logical drives
1754 scp->result = (DID_BAD_TARGET << 16);
1757 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1758 scp->result = (DID_BAD_TARGET << 16);
1763 /* Allocate a SCB and initialize passthru */
1764 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1765 scp->result = (DID_ERROR << 16);
1770 ccb = (mbox_ccb_t *)scb->ccb;
1771 scb->dev_channel = 0xFF;
1772 scb->dev_target = target;
1775 mbox64 = ccb->mbox64;
1779 pthru->reqsenselen = 14;
1780 pthru->islogical = 1;
1781 pthru->logdrv = target;
1782 pthru->cdblen = scp->cmd_len;
1783 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1785 mbox->cmd = MBOXCMD_PASSTHRU64;
1786 scb->dma_direction = scp->sc_data_direction;
1788 pthru->dataxferlen = scp->request_bufflen;
1789 pthru->dataxferaddr = ccb->sgl_dma_h;
1790 pthru->numsge = megaraid_mbox_mksgl(adapter,
1793 mbox->xferaddr = 0xFFFFFFFF;
1794 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1795 mbox64->xferaddr_hi = 0;
1807 * Allocate a SCB and initialize mailbox
1809 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1810 scp->result = (DID_ERROR << 16);
1814 ccb = (mbox_ccb_t *)scb->ccb;
1815 scb->dev_channel = 0xFF;
1816 scb->dev_target = target;
1818 mbox64 = ccb->mbox64;
1819 mbox->logdrv = target;
1822 * A little HACK: 2nd bit is zero for all scsi read
1823 * commands and is set for all scsi write commands
1825 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1829 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1831 if (scp->cmd_len == 6) {
1832 mbox->numsectors = (uint32_t)scp->cmnd[4];
1834 ((uint32_t)scp->cmnd[1] << 16) |
1835 ((uint32_t)scp->cmnd[2] << 8) |
1836 (uint32_t)scp->cmnd[3];
1838 mbox->lba &= 0x1FFFFF;
1842 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1844 else if (scp->cmd_len == 10) {
1846 (uint32_t)scp->cmnd[8] |
1847 ((uint32_t)scp->cmnd[7] << 8);
1849 ((uint32_t)scp->cmnd[2] << 24) |
1850 ((uint32_t)scp->cmnd[3] << 16) |
1851 ((uint32_t)scp->cmnd[4] << 8) |
1852 (uint32_t)scp->cmnd[5];
1856 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1858 else if (scp->cmd_len == 12) {
1860 ((uint32_t)scp->cmnd[2] << 24) |
1861 ((uint32_t)scp->cmnd[3] << 16) |
1862 ((uint32_t)scp->cmnd[4] << 8) |
1863 (uint32_t)scp->cmnd[5];
1866 ((uint32_t)scp->cmnd[6] << 24) |
1867 ((uint32_t)scp->cmnd[7] << 16) |
1868 ((uint32_t)scp->cmnd[8] << 8) |
1869 (uint32_t)scp->cmnd[9];
1872 con_log(CL_ANN, (KERN_WARNING
1873 "megaraid: unsupported CDB length\n"));
1875 megaraid_dealloc_scb(adapter, scb);
1877 scp->result = (DID_ERROR << 16);
1881 scb->dma_direction = scp->sc_data_direction;
1883 // Calculate Scatter-Gather info
1884 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1885 mbox->numsge = megaraid_mbox_mksgl(adapter,
1887 mbox->xferaddr = 0xFFFFFFFF;
1888 mbox64->xferaddr_hi = 0;
1895 * Do we support clustering and is the support enabled
1898 scp->result = (DID_BAD_TARGET << 16);
1903 * Allocate a SCB and initialize mailbox
1905 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1906 scp->result = (DID_ERROR << 16);
1911 ccb = (mbox_ccb_t *)scb->ccb;
1912 scb->dev_channel = 0xFF;
1913 scb->dev_target = target;
1914 ccb->raw_mbox[0] = CLUSTER_CMD;
1915 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1916 RESERVE_LD : RELEASE_LD;
1918 ccb->raw_mbox[3] = target;
1919 scb->dma_direction = scp->sc_data_direction;
1924 scp->result = (DID_BAD_TARGET << 16);
1928 else { // Passthru device commands
1930 // Do not allow access to target id > 15 or LUN > 7
1931 if (target > 15 || SCP2LUN(scp) > 7) {
1932 scp->result = (DID_BAD_TARGET << 16);
1936 // if fast load option was set and scan for last device is
1937 // over, reset the fast_load flag so that during a possible
1938 // next scan, devices can be made available
1939 if (rdev->fast_load && (target == 15) &&
1940 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1942 con_log(CL_ANN, (KERN_INFO
1943 "megaraid[%d]: physical device scan re-enabled\n",
1944 adapter->host->host_no));
1945 rdev->fast_load = 0;
1949 * Display the channel scan for physical devices
1951 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1953 ss = rdev->fast_load ? skip : scan;
1955 con_log(CL_ANN, (KERN_INFO
1956 "scsi[%d]: %s scsi channel %d [Phy %d]",
1957 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1961 " for non-raid devices\n"));
1963 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1966 // disable channel sweep if fast load option given
1967 if (rdev->fast_load) {
1968 scp->result = (DID_BAD_TARGET << 16);
1972 // Allocate a SCB and initialize passthru
1973 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1974 scp->result = (DID_ERROR << 16);
1979 ccb = (mbox_ccb_t *)scb->ccb;
1980 scb->dev_channel = channel;
1981 scb->dev_target = target;
1982 scb->dma_direction = scp->sc_data_direction;
1984 mbox64 = ccb->mbox64;
1986 // Does this firmware support extended CDBs
1987 if (adapter->max_cdb_sz == 16) {
1988 mbox->cmd = MBOXCMD_EXTPTHRU;
1990 megaraid_mbox_prepare_epthru(adapter, scb, scp);
1992 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
1993 mbox64->xferaddr_hi = 0;
1994 mbox->xferaddr = 0xFFFFFFFF;
1997 mbox->cmd = MBOXCMD_PASSTHRU64;
1999 megaraid_mbox_prepare_pthru(adapter, scb, scp);
2001 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
2002 mbox64->xferaddr_hi = 0;
2003 mbox->xferaddr = 0xFFFFFFFF;
2013 * megaraid_mbox_runpendq - execute commands queued in the pending queue
2014 * @adapter : controller's soft state
2015 * @scb : SCB to be queued in the pending list
2017 * scan the pending list for commands which are not yet issued and try to
2018 * post to the controller. The SCB can be a null pointer, which would indicate
2019 * no SCB to be queue, just try to execute the ones in the pending list.
2021 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
2022 * out from the head of the pending list. If it is successfully issued, the
2023 * next SCB is at the head now.
2026 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
2029 unsigned long flags;
2031 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2034 scb_q->state = SCB_PENDQ;
2035 list_add_tail(&scb_q->list, &adapter->pend_list);
2038 // if the adapter in not in quiescent mode, post the commands to FW
2039 if (adapter->quiescent) {
2040 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2044 while (!list_empty(&adapter->pend_list)) {
2046 ASSERT(spin_is_locked(PENDING_LIST_LOCK(adapter)));
2048 scb = list_entry(adapter->pend_list.next, scb_t, list);
2050 // remove the scb from the pending list and try to
2051 // issue. If we are unable to issue it, put back in
2052 // the pending list and return
2054 list_del_init(&scb->list);
2056 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2058 // if mailbox was busy, return SCB back to pending
2059 // list. Make sure to add at the head, since that's
2060 // where it would have been removed from
2062 scb->state = SCB_ISSUED;
2064 if (mbox_post_cmd(adapter, scb) != 0) {
2066 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2068 scb->state = SCB_PENDQ;
2070 list_add(&scb->list, &adapter->pend_list);
2072 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2078 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2081 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2089 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
2090 * @adapter - pointer to controller's soft state
2091 * @scb - scsi control block
2092 * @scp - scsi command from the mid-layer
2094 * prepare a command for the scsi physical devices
2097 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
2098 struct scsi_cmnd *scp)
2101 mraid_passthru_t *pthru;
2105 ccb = (mbox_ccb_t *)scb->ccb;
2107 channel = scb->dev_channel;
2108 target = scb->dev_target;
2110 pthru->timeout = 1; // 0=6sec, 1=60sec, 2=10min, 3=3hrs
2112 pthru->islogical = 0;
2114 pthru->target = (channel << 4) | target;
2115 pthru->logdrv = SCP2LUN(scp);
2116 pthru->reqsenselen = 14;
2117 pthru->cdblen = scp->cmd_len;
2119 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2121 if (scp->request_bufflen) {
2122 pthru->dataxferlen = scp->request_bufflen;
2123 pthru->dataxferaddr = ccb->sgl_dma_h;
2124 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2127 pthru->dataxferaddr = 0;
2128 pthru->dataxferlen = 0;
2136 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2137 * @adapter - pointer to controller's soft state
2138 * @scb - scsi control block
2139 * @scp - scsi command from the mid-layer
2141 * prepare a command for the scsi physical devices. This rountine prepares
2142 * commands for devices which can take extended CDBs (>10 bytes)
2145 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2146 struct scsi_cmnd *scp)
2149 mraid_epassthru_t *epthru;
2153 ccb = (mbox_ccb_t *)scb->ccb;
2154 epthru = ccb->epthru;
2155 channel = scb->dev_channel;
2156 target = scb->dev_target;
2158 epthru->timeout = 1; // 0=6sec, 1=60sec, 2=10min, 3=3hrs
2160 epthru->islogical = 0;
2161 epthru->channel = 0;
2162 epthru->target = (channel << 4) | target;
2163 epthru->logdrv = SCP2LUN(scp);
2164 epthru->reqsenselen = 14;
2165 epthru->cdblen = scp->cmd_len;
2167 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2169 if (scp->request_bufflen) {
2170 epthru->dataxferlen = scp->request_bufflen;
2171 epthru->dataxferaddr = ccb->sgl_dma_h;
2172 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2175 epthru->dataxferaddr = 0;
2176 epthru->dataxferlen = 0;
2184 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2185 * @adapter - controller's soft state
2187 * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
2188 * completed command and put them on the completed list for later processing.
2190 * Returns: 1 if the interrupt is valid, 0 otherwise
2193 megaraid_ack_sequence(adapter_t *adapter)
2195 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2199 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2200 struct list_head clist;
2203 unsigned long flags;
2207 mbox = raid_dev->mbox;
2209 // move the SCBs from the firmware completed array to our local list
2210 INIT_LIST_HEAD(&clist);
2212 // loop till F/W has more commands for us to complete
2214 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2217 * Check if a valid interrupt is pending. If found, force the
2218 * interrupt line low.
2220 dword = RDOUTDOOR(raid_dev);
2221 if (dword != 0x10001234) break;
2225 WROUTDOOR(raid_dev, 0x10001234);
2228 // wait for valid numstatus to post
2229 for (i = 0; i < 0xFFFFF; i++) {
2230 if (mbox->numstatus != 0xFF) {
2231 nstatus = mbox->numstatus;
2236 mbox->numstatus = 0xFF;
2238 adapter->outstanding_cmds -= nstatus;
2240 for (i = 0; i < nstatus; i++) {
2242 // wait for valid command index to post
2243 for (j = 0; j < 0xFFFFF; j++) {
2244 if (mbox->completed[i] != 0xFF) break;
2247 completed[i] = mbox->completed[i];
2248 mbox->completed[i] = 0xFF;
2250 if (completed[i] == 0xFF) {
2251 con_log(CL_ANN, (KERN_CRIT
2252 "megaraid: command posting timed out\n"));
2258 // Get SCB associated with this command id
2259 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2261 scb = adapter->uscb_list + (completed[i] -
2262 MBOX_MAX_SCSI_CMDS);
2266 scb = adapter->kscb_list + completed[i];
2269 scb->status = mbox->status;
2270 list_add_tail(&scb->list, &clist);
2273 // Acknowledge interrupt
2274 WRINDOOR(raid_dev, 0x02);
2278 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2281 // put the completed commands in the completed list. DPC would
2282 // complete these commands later
2283 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2285 list_splice(&clist, &adapter->completed_list);
2287 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2290 // schedule the DPC if there is some work for it
2292 tasklet_schedule(&adapter->dpc_h);
2299 * megaraid_isr - isr for memory based mailbox based controllers
2301 * @devp - pointer to our soft state
2304 * Interrupt service routine for memory-mapped mailbox controllers.
2307 megaraid_isr(int irq, void *devp, struct pt_regs *regs)
2309 adapter_t *adapter = devp;
2312 handled = megaraid_ack_sequence(adapter);
2314 /* Loop through any pending requests */
2315 if (!adapter->quiescent) {
2316 megaraid_mbox_runpendq(adapter, NULL);
2319 return IRQ_RETVAL(handled);
2324 * megaraid_mbox_sync_scb - sync kernel buffers
2325 * @adapter : controller's soft state
2326 * @scb : pointer to the resource packet
2328 * DMA sync if required.
2331 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2335 ccb = (mbox_ccb_t *)scb->ccb;
2337 switch (scb->dma_type) {
2339 case MRAID_DMA_WBUF:
2340 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2341 pci_dma_sync_single_for_cpu(adapter->pdev,
2343 scb->scp->request_bufflen,
2344 PCI_DMA_FROMDEVICE);
2347 pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
2348 scb->scp->request_bufflen, scb->dma_direction);
2353 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2354 pci_dma_sync_sg_for_cpu(adapter->pdev,
2355 scb->scp->request_buffer,
2356 scb->scp->use_sg, PCI_DMA_FROMDEVICE);
2359 pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
2360 scb->scp->use_sg, scb->dma_direction);
2373 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2374 * @devp : pointer to HBA soft state
2376 * Pick up the commands from the completed list and send back to the owners.
2377 * This is a reentrant function and does not assume any locks are held while
2378 * it is being called.
2381 megaraid_mbox_dpc(unsigned long devp)
2383 adapter_t *adapter = (adapter_t *)devp;
2384 mraid_device_t *raid_dev;
2385 struct list_head clist;
2386 struct scatterlist *sgl;
2389 struct scsi_cmnd *scp;
2390 mraid_passthru_t *pthru;
2391 mraid_epassthru_t *epthru;
2397 unsigned long flags;
2402 if (!adapter) return;
2404 raid_dev = ADAP2RAIDDEV(adapter);
2406 // move the SCBs from the completed list to our local list
2407 INIT_LIST_HEAD(&clist);
2409 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2411 list_splice_init(&adapter->completed_list, &clist);
2413 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2416 list_for_each_entry_safe(scb, tmp, &clist, list) {
2418 status = scb->status;
2420 ccb = (mbox_ccb_t *)scb->ccb;
2422 epthru = ccb->epthru;
2425 // Make sure f/w has completed a valid command
2426 if (scb->state != SCB_ISSUED) {
2427 con_log(CL_ANN, (KERN_CRIT
2428 "megaraid critical err: invalid command %d:%d:%p\n",
2429 scb->sno, scb->state, scp));
2431 continue; // Must never happen!
2434 // check for the management command and complete it right away
2435 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2436 scb->state = SCB_FREE;
2437 scb->status = status;
2439 // remove from local clist
2440 list_del_init(&scb->list);
2442 megaraid_mbox_mm_done(adapter, scb);
2447 // Was an abort issued for this command earlier
2448 if (scb->state & SCB_ABORT) {
2449 con_log(CL_ANN, (KERN_NOTICE
2450 "megaraid: aborted cmd %lx[%x] completed\n",
2451 scp->serial_number, scb->sno));
2455 * If the inquiry came of a disk drive which is not part of
2456 * any RAID array, expose it to the kernel. For this to be
2457 * enabled, user must set the "megaraid_expose_unconf_disks"
2458 * flag to 1 by specifying it on module parameter list.
2459 * This would enable data migration off drives from other
2462 islogical = MRAID_IS_LOGICAL(adapter, scp);
2463 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2464 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2467 sgl = (struct scatterlist *)
2468 scp->request_buffer;
2471 c = *(unsigned char *)
2472 (page_address((&sgl[0])->page) +
2476 con_log(CL_ANN, (KERN_WARNING
2477 "megaraid mailbox: invalid sg:%d\n",
2483 c = *(uint8_t *)scp->request_buffer;
2486 if ((c & 0x1F ) == TYPE_DISK) {
2487 pdev_index = (scb->dev_channel * 16) +
2490 raid_dev->pdrv_state[pdev_index] & 0x0F;
2492 if (pdev_state == PDRV_ONLINE ||
2493 pdev_state == PDRV_FAILED ||
2494 pdev_state == PDRV_RBLD ||
2495 pdev_state == PDRV_HOTSPARE ||
2496 megaraid_expose_unconf_disks == 0) {
2503 // Convert MegaRAID status to Linux error code
2508 scp->result = (DID_OK << 16);
2513 /* set sense_buffer and result fields */
2514 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2515 mbox->cmd == MBOXCMD_PASSTHRU64) {
2517 memcpy(scp->sense_buffer, pthru->reqsensearea,
2520 scp->result = DRIVER_SENSE << 24 |
2521 DID_OK << 16 | CHECK_CONDITION << 1;
2524 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2526 memcpy(scp->sense_buffer,
2527 epthru->reqsensearea, 14);
2529 scp->result = DRIVER_SENSE << 24 |
2531 CHECK_CONDITION << 1;
2533 scp->sense_buffer[0] = 0x70;
2534 scp->sense_buffer[2] = ABORTED_COMMAND;
2535 scp->result = CHECK_CONDITION << 1;
2542 scp->result = DID_BUS_BUSY << 16 | status;
2548 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2551 if (scp->cmnd[0] == TEST_UNIT_READY) {
2552 scp->result = DID_ERROR << 16 |
2553 RESERVATION_CONFLICT << 1;
2557 * Error code returned is 1 if Reserve or Release
2558 * failed or the input parameter is invalid
2560 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2561 scp->cmnd[0] == RELEASE)) {
2563 scp->result = DID_ERROR << 16 |
2564 RESERVATION_CONFLICT << 1;
2567 scp->result = DID_BAD_TARGET << 16 | status;
2571 // print a debug message for all failed commands
2573 megaraid_mbox_display_scb(adapter, scb);
2576 // Free our internal resources and call the mid-layer callback
2578 megaraid_mbox_sync_scb(adapter, scb);
2580 // remove from local clist
2581 list_del_init(&scb->list);
2583 // put back in free list
2584 megaraid_dealloc_scb(adapter, scb);
2586 // send the scsi packet back to kernel
2587 spin_lock(adapter->host_lock);
2588 scp->scsi_done(scp);
2589 spin_unlock(adapter->host_lock);
2597 * megaraid_abort_handler - abort the scsi command
2598 * @scp : command to be aborted
2600 * Abort a previous SCSI request. Only commands on the pending list can be
2601 * aborted. All the commands issued to the F/W must complete.
2604 megaraid_abort_handler(struct scsi_cmnd *scp)
2607 mraid_device_t *raid_dev;
2611 unsigned long flags;
2615 adapter = SCP2ADAPTER(scp);
2616 raid_dev = ADAP2RAIDDEV(adapter);
2618 ASSERT(spin_is_locked(adapter->host_lock));
2620 con_log(CL_ANN, (KERN_WARNING
2621 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2622 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2623 SCP2TARGET(scp), SCP2LUN(scp)));
2625 // If FW has stopped responding, simply return failure
2626 if (raid_dev->hw_error) {
2627 con_log(CL_ANN, (KERN_NOTICE
2628 "megaraid: hw error, not aborting\n"));
2632 // There might a race here, where the command was completed by the
2633 // firmware and now it is on the completed list. Before we could
2634 // complete the command to the kernel in dpc, the abort came.
2635 // Find out if this is the case to avoid the race.
2637 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2638 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2640 if (scb->scp == scp) { // Found command
2642 list_del_init(&scb->list); // from completed list
2644 con_log(CL_ANN, (KERN_WARNING
2645 "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2646 scp->serial_number, scb->sno,
2647 scb->dev_channel, scb->dev_target));
2649 scp->result = (DID_ABORT << 16);
2650 scp->scsi_done(scp);
2652 megaraid_dealloc_scb(adapter, scb);
2654 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2660 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2663 // Find out if this command is still on the pending list. If it is and
2664 // was never issued, abort and return success. If the command is owned
2665 // by the firmware, we must wait for it to complete by the FW.
2666 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2667 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2669 if (scb->scp == scp) { // Found command
2671 list_del_init(&scb->list); // from pending list
2673 ASSERT(!(scb->state & SCB_ISSUED));
2675 con_log(CL_ANN, (KERN_WARNING
2676 "megaraid abort: %ld[%d:%d], driver owner\n",
2677 scp->serial_number, scb->dev_channel,
2680 scp->result = (DID_ABORT << 16);
2681 scp->scsi_done(scp);
2683 megaraid_dealloc_scb(adapter, scb);
2685 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2691 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2694 // Check do we even own this command, in which case this would be
2695 // owned by the firmware. The only way to locate the FW scb is to
2696 // traverse through the list of all SCB, since driver does not
2697 // maintain these SCBs on any list
2699 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2700 scb = adapter->kscb_list + i;
2702 if (scb->scp == scp) {
2706 if (!(scb->state & SCB_ISSUED)) {
2707 con_log(CL_ANN, (KERN_WARNING
2708 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2709 scp->serial_number, scb->sno, scb->dev_channel,
2714 con_log(CL_ANN, (KERN_WARNING
2715 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2716 scp->serial_number, scb->sno, scb->dev_channel,
2723 con_log(CL_ANN, (KERN_WARNING
2724 "megaraid abort: scsi cmd:%ld, do now own\n",
2725 scp->serial_number));
2727 // FIXME: Should there be a callback for this command?
2731 // We cannot actually abort a command owned by firmware, return
2732 // failure and wait for reset. In host reset handler, we will find out
2733 // if the HBA is still live
2739 * megaraid_reset_handler - device reset hadler for mailbox based driver
2740 * @scp : reference command
2742 * Reset handler for the mailbox based controller. First try to find out if
2743 * the FW is still live, in which case the outstanding commands counter mut go
2744 * down to 0. If that happens, also issue the reservation reset command to
2745 * relinquish (possible) reservations on the logical drives connected to this
2749 megaraid_reset_handler(struct scsi_cmnd *scp)
2754 mraid_device_t *raid_dev;
2755 unsigned long flags;
2756 uint8_t raw_mbox[sizeof(mbox_t)];
2758 int recovery_window;
2762 adapter = SCP2ADAPTER(scp);
2763 raid_dev = ADAP2RAIDDEV(adapter);
2765 ASSERT(spin_is_locked(adapter->host_lock));
2767 con_log(CL_ANN, (KERN_WARNING "megaraid: reseting the host...\n"));
2769 // return failure if adapter is not responding
2770 if (raid_dev->hw_error) {
2771 con_log(CL_ANN, (KERN_NOTICE
2772 "megaraid: hw error, cannot reset\n"));
2777 // Under exceptional conditions, FW can take up to 3 minutes to
2778 // complete command processing. Wait for additional 2 minutes for the
2779 // pending commands counter to go down to 0. If it doesn't, let the
2780 // controller be marked offline
2781 // Also, reset all the commands currently owned by the driver
2782 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2783 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2785 list_del_init(&scb->list); // from pending list
2787 con_log(CL_ANN, (KERN_WARNING
2788 "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2789 scp->serial_number, scb->sno,
2790 scb->dev_channel, scb->dev_target));
2792 scp->result = (DID_RESET << 16);
2793 scp->scsi_done(scp);
2795 megaraid_dealloc_scb(adapter, scb);
2797 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2799 if (adapter->outstanding_cmds) {
2800 con_log(CL_ANN, (KERN_NOTICE
2801 "megaraid: %d outstanding commands. Max wait %d sec\n",
2802 adapter->outstanding_cmds, MBOX_RESET_WAIT));
2805 spin_unlock(adapter->host_lock);
2807 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2809 recovering = adapter->outstanding_cmds;
2811 for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
2813 megaraid_ack_sequence(adapter);
2815 // print a message once every 5 seconds only
2818 "megaraid mbox: Wait for %d commands to complete:%d\n",
2819 adapter->outstanding_cmds,
2820 MBOX_RESET_WAIT - i));
2823 // bailout if no recovery happended in reset time
2824 if ((i == MBOX_RESET_WAIT) &&
2825 (recovering == adapter->outstanding_cmds)) {
2832 spin_lock(adapter->host_lock);
2834 // If still outstanding commands, bail out
2835 if (adapter->outstanding_cmds) {
2836 con_log(CL_ANN, (KERN_WARNING
2837 "megaraid mbox: critical hardware error!\n"));
2839 raid_dev->hw_error = 1;
2844 con_log(CL_ANN, (KERN_NOTICE
2845 "megaraid mbox: reset sequence completed sucessfully\n"));
2849 // If the controller supports clustering, reset reservations
2850 if (!adapter->ha) return SUCCESS;
2852 // clear reservations if any
2853 raw_mbox[0] = CLUSTER_CMD;
2854 raw_mbox[2] = RESET_RESERVATIONS;
2857 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2859 (KERN_INFO "megaraid: reservation reset\n"));
2863 con_log(CL_ANN, (KERN_WARNING
2864 "megaraid: reservation reset failed\n"));
2872 * START: internal commands library
2874 * This section of the driver has the common routine used by the driver and
2875 * also has all the FW routines
2879 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2880 * @adapter - controller's soft state
2881 * @raw_mbox - the mailbox
2883 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2887 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2889 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2896 mbox64 = raid_dev->mbox64;
2897 mbox = raid_dev->mbox;
2900 * Wait until mailbox is free
2902 if (megaraid_busywait_mbox(raid_dev) != 0)
2903 goto blocked_mailbox;
2906 * Copy mailbox data into host structure
2908 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2913 mbox->numstatus = 0xFF;
2914 mbox->status = 0xFF;
2917 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2919 // wait for maximum 1 second for status to post. If the status is not
2920 // available within 1 second, assume FW is initializing and wait
2921 // for an extended amount of time
2922 if (mbox->numstatus == 0xFF) { // status not yet available
2925 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2932 con_log(CL_ANN, (KERN_NOTICE
2933 "megaraid mailbox: wait for FW to boot "));
2935 for (i = 0; (mbox->numstatus == 0xFF) &&
2936 (i < MBOX_RESET_WAIT); i++) {
2938 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2939 MBOX_RESET_WAIT - i));
2943 if (i == MBOX_RESET_WAIT) {
2946 "\nmegaraid mailbox: status not available\n"));
2950 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2954 // wait for maximum 1 second for poll semaphore
2955 if (mbox->poll != 0x77) {
2958 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2964 con_log(CL_ANN, (KERN_WARNING
2965 "megaraid mailbox: could not get poll semaphore\n"));
2970 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2973 // wait for maximum 1 second for acknowledgement
2974 if (RDINDOOR(raid_dev) & 0x2) {
2977 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2983 con_log(CL_ANN, (KERN_WARNING
2984 "megaraid mailbox: could not acknowledge\n"));
2991 status = mbox->status;
2993 // invalidate the completed command id array. After command
2994 // completion, firmware would write the valid id.
2995 mbox->numstatus = 0xFF;
2996 mbox->status = 0xFF;
2997 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2998 mbox->completed[i] = 0xFF;
3005 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
3011 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
3012 * @adapter - controller's soft state
3013 * @raw_mbox - the mailbox
3015 * Issue a scb in synchronous and non-interrupt mode for mailbox based
3016 * controllers. This is a faster version of the synchronous command and
3017 * therefore can be called in interrupt-context as well
3020 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
3022 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3027 mbox = raid_dev->mbox;
3029 // return immediately if the mailbox is busy
3030 if (mbox->busy) return -1;
3032 // Copy mailbox data into host structure
3033 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
3038 mbox->numstatus = 0xFF;
3039 mbox->status = 0xFF;
3042 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3044 for (i = 0; i < 0xFFFFF; i++) {
3045 if (mbox->numstatus != 0xFF) break;
3049 // We may need to re-calibrate the counter
3050 con_log(CL_ANN, (KERN_CRIT
3051 "megaraid: fast sync command timed out\n"));
3054 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
3057 return mbox->status;
3062 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
3063 * @raid_dev - RAID device (HBA) soft state
3065 * wait until the controller's mailbox is available to accept more commands.
3066 * wait for at most 1 second
3069 megaraid_busywait_mbox(mraid_device_t *raid_dev)
3071 mbox_t *mbox = raid_dev->mbox;
3076 for (i = 0; mbox->busy && i < 1000; i++)
3080 if (i < 1000) return 0;
3086 * megaraid_mbox_product_info - some static information about the controller
3087 * @adapter - our soft state
3089 * issue commands to the controller to grab some parameters required by our
3093 megaraid_mbox_product_info(adapter_t *adapter)
3095 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3097 uint8_t raw_mbox[sizeof(mbox_t)];
3098 mraid_pinfo_t *pinfo;
3099 dma_addr_t pinfo_dma_h;
3100 mraid_inquiry3_t *mraid_inq3;
3104 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3105 mbox = (mbox_t *)raw_mbox;
3108 * Issue an ENQUIRY3 command to find out certain adapter parameters,
3109 * e.g., max channels, max commands etc.
3111 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3114 if (pinfo == NULL) {
3115 con_log(CL_ANN, (KERN_WARNING
3116 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3121 memset(pinfo, 0, sizeof(mraid_pinfo_t));
3123 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3124 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3126 raw_mbox[0] = FC_NEW_CONFIG;
3127 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
3128 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3130 // Issue the command
3131 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3133 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3135 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3136 pinfo, pinfo_dma_h);
3142 * Collect information about state of each physical drive
3143 * attached to the controller. We will expose all the disks
3144 * which are not part of RAID
3146 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3147 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3148 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3152 * Get product info for information like number of channels,
3153 * maximum commands supported.
3155 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3156 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3158 raw_mbox[0] = FC_NEW_CONFIG;
3159 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3161 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3163 con_log(CL_ANN, (KERN_WARNING
3164 "megaraid: product info failed\n"));
3166 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3167 pinfo, pinfo_dma_h);
3173 * Setup some parameters for host, as required by our caller
3175 adapter->max_channel = pinfo->nchannels;
3178 * we will export all the logical drives on a single channel.
3179 * Add 1 since inquires do not come for inititor ID
3181 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3182 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3185 * These are the maximum outstanding commands for the scsi-layer
3187 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3189 memset(adapter->fw_version, 0, VERSION_SIZE);
3190 memset(adapter->bios_version, 0, VERSION_SIZE);
3192 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3193 adapter->fw_version[4] = 0;
3195 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3196 adapter->bios_version[4] = 0;
3198 con_log(CL_ANN, (KERN_NOTICE
3199 "megaraid: fw version:[%s] bios version:[%s]\n",
3200 adapter->fw_version, adapter->bios_version));
3202 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3211 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3212 * @adapter - soft state for the controller
3214 * this routine check whether the controller in question supports extended
3215 * ( > 10 bytes ) CDBs
3218 megaraid_mbox_extended_cdb(adapter_t *adapter)
3221 uint8_t raw_mbox[sizeof(mbox_t)];
3224 mbox = (mbox_t *)raw_mbox;
3226 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3227 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3229 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3231 raw_mbox[0] = MAIN_MISC_OPCODE;
3232 raw_mbox[2] = SUPPORT_EXT_CDB;
3238 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3247 * megaraid_mbox_support_ha - Do we support clustering
3248 * @adapter - soft state for the controller
3249 * @init_id - ID of the initiator
3251 * Determine if the firmware supports clustering and the ID of the initiator.
3254 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3257 uint8_t raw_mbox[sizeof(mbox_t)];
3261 mbox = (mbox_t *)raw_mbox;
3263 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3265 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3267 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3269 raw_mbox[0] = GET_TARGET_ID;
3271 // Issue the command
3274 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3276 *init_id = *(uint8_t *)adapter->ibuf;
3278 con_log(CL_ANN, (KERN_INFO
3279 "megaraid: cluster firmware, initiator ID: %d\n",
3290 * megaraid_mbox_support_random_del - Do we support random deletion
3291 * @adapter - soft state for the controller
3293 * Determine if the firmware supports random deletion
3294 * Return: 1 is operation supported, 0 otherwise
3297 megaraid_mbox_support_random_del(adapter_t *adapter)
3300 uint8_t raw_mbox[sizeof(mbox_t)];
3304 mbox = (mbox_t *)raw_mbox;
3306 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3308 raw_mbox[0] = FC_DEL_LOGDRV;
3309 raw_mbox[0] = OP_SUP_DEL_LOGDRV;
3311 // Issue the command
3313 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3315 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3325 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3326 * @adapter - soft state for the controller
3328 * Find out the maximum number of scatter-gather elements supported by the
3332 megaraid_mbox_get_max_sg(adapter_t *adapter)
3335 uint8_t raw_mbox[sizeof(mbox_t)];
3339 mbox = (mbox_t *)raw_mbox;
3341 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3343 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3345 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3347 raw_mbox[0] = MAIN_MISC_OPCODE;
3348 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3350 // Issue the command
3351 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3352 nsg = *(uint8_t *)adapter->ibuf;
3355 nsg = MBOX_DEFAULT_SG_SIZE;
3358 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3365 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3366 * @adapter - soft state for the controller
3368 * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
3369 * can be exported as regular SCSI channels
3372 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3374 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3376 uint8_t raw_mbox[sizeof(mbox_t)];
3379 mbox = (mbox_t *)raw_mbox;
3381 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3383 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3385 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3387 raw_mbox[0] = CHNL_CLASS;
3388 raw_mbox[2] = GET_CHNL_CLASS;
3390 // Issue the command. If the command fails, all channels are RAID
3392 raid_dev->channel_class = 0xFF;
3393 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3394 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3402 * megaraid_mbox_flush_cache - flush adapter and disks cache
3403 * @param adapter : soft state for the controller
3405 * Flush adapter cache followed by disks cache
3408 megaraid_mbox_flush_cache(adapter_t *adapter)
3411 uint8_t raw_mbox[sizeof(mbox_t)];
3414 mbox = (mbox_t *)raw_mbox;
3416 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3418 raw_mbox[0] = FLUSH_ADAPTER;
3420 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3421 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3424 raw_mbox[0] = FLUSH_SYSTEM;
3426 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3427 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3435 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3436 * @param adapter : controllers' soft state
3437 * @param scb : SCB to be displayed
3438 * @param level : debug level for console print
3440 * Diplay information about the given SCB iff the current debug level is
3444 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3447 struct scsi_cmnd *scp;
3453 ccb = (mbox_ccb_t *)scb->ccb;
3459 con_log(level, (KERN_NOTICE
3460 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3461 mbox->cmd, scb->sno));
3463 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3464 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3469 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3471 for (i = 0; i < scp->cmd_len; i++) {
3472 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3475 con_log(level, ("\n"));
3482 * megaraid_mbox_setup_device_map - manage device ids
3483 * @adapter : Driver's soft state
3485 * Manange the device ids to have an appropraite mapping between the kernel
3486 * scsi addresses and megaraid scsi and logical drive addresses. We export
3487 * scsi devices on their actual addresses, whereas the logical drives are
3488 * exported on a virtual scsi channel.
3491 megaraid_mbox_setup_device_map(adapter_t *adapter)
3497 * First fill the values on the logical drive channel
3499 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3500 adapter->device_ids[adapter->max_channel][t] =
3501 (t < adapter->init_id) ? t : t - 1;
3503 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3506 * Fill the values on the physical devices channels
3508 for (c = 0; c < adapter->max_channel; c++)
3509 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3510 adapter->device_ids[c][t] = (c << 8) | t;
3515 * END: internal commands library
3519 * START: Interface for the common management module
3521 * This is the module, which interfaces with the common mangement module to
3522 * provide support for ioctl and sysfs
3526 * megaraid_cmm_register - register with the mangement module
3527 * @param adapter : HBA soft state
3529 * Register with the management module, which allows applications to issue
3530 * ioctl calls to the drivers. This interface is used by the management module
3531 * to setup sysfs support as well.
3534 megaraid_cmm_register(adapter_t *adapter)
3536 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3543 // Allocate memory for the base list of scb for management module.
3544 adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
3547 if (adapter->uscb_list == NULL) {
3548 con_log(CL_ANN, (KERN_WARNING
3549 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3553 memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
3556 // Initialize the synchronization parameters for resources for
3557 // commands for management module
3558 INIT_LIST_HEAD(&adapter->uscb_pool);
3560 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3564 // link all the packets. Note, CCB for commands, coming from the
3565 // commom management module, mailbox physical address are already
3566 // setup by it. We just need placeholder for that in our local command
3568 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3570 scb = adapter->uscb_list + i;
3571 ccb = raid_dev->uccb_list + i;
3573 scb->ccb = (caddr_t)ccb;
3574 ccb->mbox64 = raid_dev->umbox64 + i;
3575 ccb->mbox = &ccb->mbox64->mbox32;
3576 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3580 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3581 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3582 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3585 scb->state = SCB_FREE;
3586 scb->dma_direction = PCI_DMA_NONE;
3587 scb->dma_type = MRAID_DMA_NONE;
3588 scb->dev_channel = -1;
3589 scb->dev_target = -1;
3591 // put scb in the free pool
3592 list_add_tail(&scb->list, &adapter->uscb_pool);
3595 adp.unique_id = adapter->unique_id;
3596 adp.drvr_type = DRVRTYPE_MBOX;
3597 adp.drvr_data = (unsigned long)adapter;
3598 adp.pdev = adapter->pdev;
3599 adp.issue_uioc = megaraid_mbox_mm_handler;
3601 adp.max_kioc = MBOX_MAX_USER_CMDS;
3603 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3605 con_log(CL_ANN, (KERN_WARNING
3606 "megaraid mbox: did not register with CMM\n"));
3608 kfree(adapter->uscb_list);
3616 * megaraid_cmm_unregister - un-register with the mangement module
3617 * @param adapter : HBA soft state
3619 * Un-register with the management module.
3620 * FIXME: mgmt module must return failure for unregister if it has pending
3624 megaraid_cmm_unregister(adapter_t *adapter)
3626 kfree(adapter->uscb_list);
3627 mraid_mm_unregister_adp(adapter->unique_id);
3633 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3634 * @param drvr_data : LLD specific data
3635 * @param kioc : CMM interface packet
3636 * @param action : command action
3638 * This routine is invoked whenever the Common Mangement Module (CMM) has a
3639 * command for us. The 'action' parameter specifies if this is a new command
3643 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3647 if (action != IOCTL_ISSUE) {
3648 con_log(CL_ANN, (KERN_WARNING
3649 "megaraid: unsupported management action:%#2x\n",
3654 adapter = (adapter_t *)drvr_data;
3656 // make sure this adapter is not being detached right now.
3657 if (atomic_read(&adapter->being_detached)) {
3658 con_log(CL_ANN, (KERN_WARNING
3659 "megaraid: reject management request, detaching\n"));
3663 switch (kioc->opcode) {
3667 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3668 (unsigned long)kioc->buf_vaddr);
3672 return kioc->status;
3676 return megaraid_mbox_mm_command(adapter, kioc);
3679 kioc->status = (-EINVAL);
3684 return 0; // not reached
3688 * megaraid_mbox_mm_command - issues commands routed through CMM
3689 * @param adapter : HBA soft state
3690 * @param kioc : management command packet
3692 * Issues commands, which are routed through the management module.
3695 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3697 struct list_head *head = &adapter->uscb_pool;
3702 unsigned long flags;
3704 // detach one scb from free pool
3705 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3707 if (list_empty(head)) { // should never happen because of CMM
3709 con_log(CL_ANN, (KERN_WARNING
3710 "megaraid mbox: bug in cmm handler, lost resources\n"));
3712 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3717 scb = list_entry(head->next, scb_t, list);
3718 list_del_init(&scb->list);
3720 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3722 scb->state = SCB_ACTIVE;
3723 scb->dma_type = MRAID_DMA_NONE;
3725 ccb = (mbox_ccb_t *)scb->ccb;
3726 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3727 raw_mbox = (uint8_t *)&mbox64->mbox32;
3729 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3731 scb->gp = (unsigned long)kioc;
3734 * If it is a logdrv random delete operation, we have to wait till
3735 * there are no outstanding cmds at the fw and then issue it directly
3737 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3739 if (wait_till_fw_empty(adapter)) {
3740 con_log(CL_ANN, (KERN_NOTICE
3741 "megaraid mbox: LD delete, timed out\n"));
3743 kioc->status = -ETIME;
3747 megaraid_mbox_mm_done(adapter, scb);
3752 INIT_LIST_HEAD(&scb->list);
3754 scb->state = SCB_ISSUED;
3755 if (mbox_post_cmd(adapter, scb) != 0) {
3757 con_log(CL_ANN, (KERN_NOTICE
3758 "megaraid mbox: LD delete, mailbox busy\n"));
3760 kioc->status = -EBUSY;
3764 megaraid_mbox_mm_done(adapter, scb);
3772 // put the command on the pending list and execute
3773 megaraid_mbox_runpendq(adapter, scb);
3780 wait_till_fw_empty(adapter_t *adapter)
3782 unsigned long flags = 0;
3787 * Set the quiescent flag to stop issuing cmds to FW.
3789 spin_lock_irqsave(adapter->host_lock, flags);
3790 adapter->quiescent++;
3791 spin_unlock_irqrestore(adapter->host_lock, flags);
3794 * Wait till there are no more cmds outstanding at FW. Try for at most
3797 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3798 con_log(CL_DLEVEL1, (KERN_INFO
3799 "megaraid: FW has %d pending commands\n",
3800 adapter->outstanding_cmds));
3805 return adapter->outstanding_cmds;
3810 * megaraid_mbox_mm_done - callback for CMM commands
3811 * @adapter : HBA soft state
3812 * @scb : completed command
3814 * Callback routine for internal commands originated from the management
3818 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3823 unsigned long flags;
3825 kioc = (uioc_t *)scb->gp;
3827 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3828 mbox64->mbox32.status = scb->status;
3829 raw_mbox = (uint8_t *)&mbox64->mbox32;
3832 // put scb in the free pool
3833 scb->state = SCB_FREE;
3836 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3838 list_add(&scb->list, &adapter->uscb_pool);
3840 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3842 // if a delete logical drive operation succeeded, restart the
3844 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3846 adapter->quiescent--;
3848 megaraid_mbox_runpendq(adapter, NULL);
3858 * gather_hbainfo - HBA characteristics for the applications
3859 * @param adapter : HBA soft state
3860 * @param hinfo : pointer to the caller's host info strucuture
3863 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3867 dmajor = megaraid_mbox_version[0];
3869 hinfo->pci_vendor_id = adapter->pdev->vendor;
3870 hinfo->pci_device_id = adapter->pdev->device;
3871 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3872 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3874 hinfo->pci_bus = adapter->pdev->bus->number;
3875 hinfo->pci_dev_fn = adapter->pdev->devfn;
3876 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3877 hinfo->irq = adapter->host->irq;
3878 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3880 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3881 hinfo->host_no = adapter->host->host_no;
3887 * END: Interface for the common management module
3892 * END: Mailbox Low Level Driver
3894 module_init(megaraid_init);
3895 module_exit(megaraid_exit);
3897 /* vim: set ts=8 sw=8 tw=78 ai si: */