Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / megaraid / megaraid_mbox.c
blob138fa481583371623124c419e1d2367cfb7da612
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.5 (Feb 03 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 device *);
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 *, 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, 0x05, 2, 3, 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_PERC4_QC,
233 PCI_VENDOR_ID_DELL,
234 PCI_SUBSYS_ID_PERC4_QC,
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_PERC4E_DC_320_2E,
275 PCI_VENDOR_ID_DELL,
276 PCI_SUBSYS_ID_PERC4E_DC_320_2E,
279 PCI_VENDOR_ID_LSI_LOGIC,
280 PCI_DEVICE_ID_PERC4E_SC_320_1E,
281 PCI_VENDOR_ID_DELL,
282 PCI_SUBSYS_ID_PERC4E_SC_320_1E,
285 PCI_VENDOR_ID_AMI,
286 PCI_DEVICE_ID_AMI_MEGARAID3,
287 PCI_VENDOR_ID_DELL,
288 PCI_SUBSYS_ID_PERC3_QC,
291 PCI_VENDOR_ID_AMI,
292 PCI_DEVICE_ID_AMI_MEGARAID3,
293 PCI_VENDOR_ID_DELL,
294 PCI_SUBSYS_ID_PERC3_DC,
297 PCI_VENDOR_ID_AMI,
298 PCI_DEVICE_ID_AMI_MEGARAID3,
299 PCI_VENDOR_ID_DELL,
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_SC,
309 PCI_VENDOR_ID_AMI,
310 PCI_DEVICE_ID_AMI_MEGARAID3,
311 PCI_VENDOR_ID_AMI,
312 PCI_SUBSYS_ID_PERC3_DC,
315 PCI_VENDOR_ID_LSI_LOGIC,
316 PCI_DEVICE_ID_MEGARAID_SCSI_320_0,
317 PCI_VENDOR_ID_LSI_LOGIC,
318 PCI_SUBSYS_ID_MEGARAID_SCSI_320_0,
321 PCI_VENDOR_ID_LSI_LOGIC,
322 PCI_DEVICE_ID_MEGARAID_SCSI_320_1,
323 PCI_VENDOR_ID_LSI_LOGIC,
324 PCI_SUBSYS_ID_MEGARAID_SCSI_320_1,
327 PCI_VENDOR_ID_LSI_LOGIC,
328 PCI_DEVICE_ID_MEGARAID_SCSI_320_2,
329 PCI_VENDOR_ID_LSI_LOGIC,
330 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2,
333 PCI_VENDOR_ID_LSI_LOGIC,
334 PCI_DEVICE_ID_MEGARAID_SCSI_320_0x,
335 PCI_VENDOR_ID_LSI_LOGIC,
336 PCI_SUBSYS_ID_MEGARAID_SCSI_320_0x,
339 PCI_VENDOR_ID_LSI_LOGIC,
340 PCI_DEVICE_ID_MEGARAID_SCSI_320_2x,
341 PCI_VENDOR_ID_LSI_LOGIC,
342 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2x,
345 PCI_VENDOR_ID_LSI_LOGIC,
346 PCI_DEVICE_ID_MEGARAID_SCSI_320_4x,
347 PCI_VENDOR_ID_LSI_LOGIC,
348 PCI_SUBSYS_ID_MEGARAID_SCSI_320_4x,
351 PCI_VENDOR_ID_LSI_LOGIC,
352 PCI_DEVICE_ID_MEGARAID_SCSI_320_1E,
353 PCI_VENDOR_ID_LSI_LOGIC,
354 PCI_SUBSYS_ID_MEGARAID_SCSI_320_1E,
357 PCI_VENDOR_ID_LSI_LOGIC,
358 PCI_DEVICE_ID_MEGARAID_SCSI_320_2E,
359 PCI_VENDOR_ID_LSI_LOGIC,
360 PCI_SUBSYS_ID_MEGARAID_SCSI_320_2E,
363 PCI_VENDOR_ID_LSI_LOGIC,
364 PCI_DEVICE_ID_MEGARAID_I4_133_RAID,
365 PCI_VENDOR_ID_LSI_LOGIC,
366 PCI_SUBSYS_ID_MEGARAID_I4_133_RAID,
369 PCI_VENDOR_ID_LSI_LOGIC,
370 PCI_DEVICE_ID_MEGARAID_SATA_150_4,
371 PCI_VENDOR_ID_LSI_LOGIC,
372 PCI_SUBSYS_ID_MEGARAID_SATA_150_4,
375 PCI_VENDOR_ID_LSI_LOGIC,
376 PCI_DEVICE_ID_MEGARAID_SATA_150_6,
377 PCI_VENDOR_ID_LSI_LOGIC,
378 PCI_SUBSYS_ID_MEGARAID_SATA_150_6,
381 PCI_VENDOR_ID_LSI_LOGIC,
382 PCI_DEVICE_ID_MEGARAID_SATA_300_4x,
383 PCI_VENDOR_ID_LSI_LOGIC,
384 PCI_SUBSYS_ID_MEGARAID_SATA_300_4x,
387 PCI_VENDOR_ID_LSI_LOGIC,
388 PCI_DEVICE_ID_MEGARAID_SATA_300_8x,
389 PCI_VENDOR_ID_LSI_LOGIC,
390 PCI_SUBSYS_ID_MEGARAID_SATA_300_8x,
393 PCI_VENDOR_ID_LSI_LOGIC,
394 PCI_DEVICE_ID_INTEL_RAID_SRCU42X,
395 PCI_VENDOR_ID_INTEL,
396 PCI_SUBSYS_ID_INTEL_RAID_SRCU42X,
399 PCI_VENDOR_ID_LSI_LOGIC,
400 PCI_DEVICE_ID_INTEL_RAID_SRCS16,
401 PCI_VENDOR_ID_INTEL,
402 PCI_SUBSYS_ID_INTEL_RAID_SRCS16,
405 PCI_VENDOR_ID_LSI_LOGIC,
406 PCI_DEVICE_ID_INTEL_RAID_SRCU42E,
407 PCI_VENDOR_ID_INTEL,
408 PCI_SUBSYS_ID_INTEL_RAID_SRCU42E,
411 PCI_VENDOR_ID_LSI_LOGIC,
412 PCI_DEVICE_ID_INTEL_RAID_SRCZCRX,
413 PCI_VENDOR_ID_INTEL,
414 PCI_SUBSYS_ID_INTEL_RAID_SRCZCRX,
417 PCI_VENDOR_ID_LSI_LOGIC,
418 PCI_DEVICE_ID_INTEL_RAID_SRCS28X,
419 PCI_VENDOR_ID_INTEL,
420 PCI_SUBSYS_ID_INTEL_RAID_SRCS28X,
423 PCI_VENDOR_ID_LSI_LOGIC,
424 PCI_DEVICE_ID_INTEL_RAID_SROMBU42E_ALIEF,
425 PCI_VENDOR_ID_INTEL,
426 PCI_SUBSYS_ID_INTEL_RAID_SROMBU42E_ALIEF,
429 PCI_VENDOR_ID_LSI_LOGIC,
430 PCI_DEVICE_ID_INTEL_RAID_SROMBU42E_HARWICH,
431 PCI_VENDOR_ID_INTEL,
432 PCI_SUBSYS_ID_INTEL_RAID_SROMBU42E_HARWICH,
435 PCI_VENDOR_ID_LSI_LOGIC,
436 PCI_DEVICE_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
437 PCI_VENDOR_ID_INTEL,
438 PCI_SUBSYS_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
441 PCI_VENDOR_ID_LSI_LOGIC,
442 PCI_DEVICE_ID_FSC_MEGARAID_PCI_EXPRESS_ROMB,
443 PCI_SUBSYS_ID_FSC,
444 PCI_SUBSYS_ID_FSC_MEGARAID_PCI_EXPRESS_ROMB,
447 PCI_VENDOR_ID_LSI_LOGIC,
448 PCI_DEVICE_ID_MEGARAID_ACER_ROMB_2E,
449 PCI_VENDOR_ID_AI,
450 PCI_SUBSYS_ID_MEGARAID_ACER_ROMB_2E,
453 PCI_VENDOR_ID_LSI_LOGIC,
454 PCI_DEVICE_ID_MEGARAID_NEC_ROMB_2E,
455 PCI_VENDOR_ID_NEC,
456 PCI_SUBSYS_ID_MEGARAID_NEC_ROMB_2E,
458 {0} /* Terminating entry */
460 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
463 static struct pci_driver megaraid_pci_driver_g = {
464 .name = "megaraid",
465 .id_table = pci_id_table_g,
466 .probe = megaraid_probe_one,
467 .remove = __devexit_p(megaraid_detach_one),
468 .driver = {
469 .shutdown = megaraid_mbox_shutdown,
475 // definitions for the device attributes for exporting logical drive number
476 // for a scsi address (Host, Channel, Id, Lun)
478 CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
479 NULL);
481 // Host template initializer for megaraid mbox sysfs device attributes
482 static struct class_device_attribute *megaraid_shost_attrs[] = {
483 &class_device_attr_megaraid_mbox_app_hndl,
484 NULL,
488 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
490 // Host template initializer for megaraid mbox sysfs device attributes
491 static struct device_attribute *megaraid_sdev_attrs[] = {
492 &dev_attr_megaraid_mbox_ld,
493 NULL,
498 * Scsi host template for megaraid unified driver
500 static struct scsi_host_template megaraid_template_g = {
501 .module = THIS_MODULE,
502 .name = "LSI Logic MegaRAID driver",
503 .proc_name = "megaraid",
504 .queuecommand = megaraid_queue_command,
505 .eh_abort_handler = megaraid_abort_handler,
506 .eh_device_reset_handler = megaraid_reset_handler,
507 .eh_bus_reset_handler = megaraid_reset_handler,
508 .eh_host_reset_handler = megaraid_reset_handler,
509 .use_clustering = ENABLE_CLUSTERING,
510 .sdev_attrs = megaraid_sdev_attrs,
511 .shost_attrs = megaraid_shost_attrs,
516 * megaraid_init - module load hook
518 * We register ourselves as hotplug enabled module and let PCI subsystem
519 * discover our adaters
521 static int __init
522 megaraid_init(void)
524 int rval;
526 // Announce the driver version
527 con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
528 MEGARAID_EXT_VERSION));
530 // check validity of module parameters
531 if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
533 con_log(CL_ANN, (KERN_WARNING
534 "megaraid mailbox: max commands per lun reset to %d\n",
535 MBOX_MAX_SCSI_CMDS));
537 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
541 // register as a PCI hot-plug driver module
542 if ((rval = pci_module_init(&megaraid_pci_driver_g))) {
543 con_log(CL_ANN, (KERN_WARNING
544 "megaraid: could not register hotplug support.\n"));
547 return rval;
552 * megaraid_exit - driver unload entry point
554 * We simply unwrap the megaraid_init routine here
556 static void __exit
557 megaraid_exit(void)
559 con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
561 // unregister as PCI hotplug driver
562 pci_unregister_driver(&megaraid_pci_driver_g);
564 return;
569 * megaraid_probe_one - PCI hotplug entry point
570 * @param pdev : handle to this controller's PCI configuration space
571 * @param id : pci device id of the class of controllers
573 * This routine should be called whenever a new adapter is detected by the
574 * PCI hotplug susbsytem.
576 static int __devinit
577 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
579 adapter_t *adapter;
582 // detected a new controller
583 con_log(CL_ANN, (KERN_INFO
584 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
585 pdev->vendor, pdev->device, pdev->subsystem_vendor,
586 pdev->subsystem_device));
588 con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
589 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
591 if (pci_enable_device(pdev)) {
592 con_log(CL_ANN, (KERN_WARNING
593 "megaraid: pci_enable_device failed\n"));
595 return -ENODEV;
598 // Enable bus-mastering on this controller
599 pci_set_master(pdev);
601 // Allocate the per driver initialization structure
602 adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
604 if (adapter == NULL) {
605 con_log(CL_ANN, (KERN_WARNING
606 "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
608 goto out_probe_one;
610 memset(adapter, 0, sizeof(adapter_t));
613 // set up PCI related soft state and other pre-known parameters
614 adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
615 adapter->irq = pdev->irq;
616 adapter->pdev = pdev;
618 atomic_set(&adapter->being_detached, 0);
620 // Setup the default DMA mask. This would be changed later on
621 // depending on hardware capabilities
622 if (pci_set_dma_mask(adapter->pdev, 0xFFFFFFFF) != 0) {
624 con_log(CL_ANN, (KERN_WARNING
625 "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
627 goto out_free_adapter;
631 // Initialize the synchronization lock for kernel and LLD
632 spin_lock_init(&adapter->lock);
633 adapter->host_lock = &adapter->lock;
636 // Initialize the command queues: the list of free SCBs and the list
637 // of pending SCBs.
638 INIT_LIST_HEAD(&adapter->kscb_pool);
639 spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
641 INIT_LIST_HEAD(&adapter->pend_list);
642 spin_lock_init(PENDING_LIST_LOCK(adapter));
644 INIT_LIST_HEAD(&adapter->completed_list);
645 spin_lock_init(COMPLETED_LIST_LOCK(adapter));
648 // Start the mailbox based controller
649 if (megaraid_init_mbox(adapter) != 0) {
650 con_log(CL_ANN, (KERN_WARNING
651 "megaraid: maibox adapter did not initialize\n"));
653 goto out_free_adapter;
656 // Register with LSI Common Management Module
657 if (megaraid_cmm_register(adapter) != 0) {
659 con_log(CL_ANN, (KERN_WARNING
660 "megaraid: could not register with management module\n"));
662 goto out_fini_mbox;
665 // setup adapter handle in PCI soft state
666 pci_set_drvdata(pdev, adapter);
668 // attach with scsi mid-layer
669 if (megaraid_io_attach(adapter) != 0) {
671 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
673 goto out_cmm_unreg;
676 return 0;
678 out_cmm_unreg:
679 pci_set_drvdata(pdev, NULL);
680 megaraid_cmm_unregister(adapter);
681 out_fini_mbox:
682 megaraid_fini_mbox(adapter);
683 out_free_adapter:
684 kfree(adapter);
685 out_probe_one:
686 pci_disable_device(pdev);
688 return -ENODEV;
693 * megaraid_detach_one - release the framework resources and call LLD release
694 * routine
695 * @param pdev : handle for our PCI cofiguration space
697 * This routine is called during driver unload. We free all the allocated
698 * resources and call the corresponding LLD so that it can also release all
699 * its resources.
701 * This routine is also called from the PCI hotplug system
703 static void
704 megaraid_detach_one(struct pci_dev *pdev)
706 adapter_t *adapter;
707 struct Scsi_Host *host;
710 // Start a rollback on this adapter
711 adapter = pci_get_drvdata(pdev);
713 if (!adapter) {
714 con_log(CL_ANN, (KERN_CRIT
715 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
716 pdev->vendor, pdev->device, pdev->subsystem_vendor,
717 pdev->subsystem_device));
719 return;
721 else {
722 con_log(CL_ANN, (KERN_NOTICE
723 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
724 pdev->vendor, pdev->device, pdev->subsystem_vendor,
725 pdev->subsystem_device));
729 host = adapter->host;
731 // do not allow any more requests from the management module for this
732 // adapter.
733 // FIXME: How do we account for the request which might still be
734 // pending with us?
735 atomic_set(&adapter->being_detached, 1);
737 // detach from the IO sub-system
738 megaraid_io_detach(adapter);
740 // reset the device state in the PCI structure. We check this
741 // condition when we enter here. If the device state is NULL,
742 // that would mean the device has already been removed
743 pci_set_drvdata(pdev, NULL);
745 // Unregister from common management module
747 // FIXME: this must return success or failure for conditions if there
748 // is a command pending with LLD or not.
749 megaraid_cmm_unregister(adapter);
751 // finalize the mailbox based controller and release all resources
752 megaraid_fini_mbox(adapter);
754 kfree(adapter);
756 scsi_host_put(host);
758 pci_disable_device(pdev);
760 return;
765 * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
766 * @param device : generice driver model device
768 * Shutdown notification, perform flush cache
770 static void
771 megaraid_mbox_shutdown(struct device *device)
773 adapter_t *adapter = pci_get_drvdata(to_pci_dev(device));
774 static int counter;
776 if (!adapter) {
777 con_log(CL_ANN, (KERN_WARNING
778 "megaraid: null device in shutdown\n"));
779 return;
782 // flush caches now
783 con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
784 counter++));
786 megaraid_mbox_flush_cache(adapter);
788 con_log(CL_ANN, ("done\n"));
793 * megaraid_io_attach - attach a device with the IO subsystem
794 * @param adapter : controller's soft state
796 * Attach this device with the IO subsystem
798 static int
799 megaraid_io_attach(adapter_t *adapter)
801 struct Scsi_Host *host;
803 // Initialize SCSI Host structure
804 host = scsi_host_alloc(&megaraid_template_g, 8);
805 if (!host) {
806 con_log(CL_ANN, (KERN_WARNING
807 "megaraid mbox: scsi_register failed\n"));
809 return -1;
812 SCSIHOST2ADAP(host) = (caddr_t)adapter;
813 adapter->host = host;
815 // export the parameters required by the mid-layer
816 scsi_assign_lock(host, adapter->host_lock);
817 scsi_set_device(host, &adapter->pdev->dev);
819 host->irq = adapter->irq;
820 host->unique_id = adapter->unique_id;
821 host->can_queue = adapter->max_cmds;
822 host->this_id = adapter->init_id;
823 host->sg_tablesize = adapter->sglen;
824 host->max_sectors = adapter->max_sectors;
825 host->cmd_per_lun = adapter->cmd_per_lun;
826 host->max_channel = adapter->max_channel;
827 host->max_id = adapter->max_target;
828 host->max_lun = adapter->max_lun;
831 // notify mid-layer about the new controller
832 if (scsi_add_host(host, &adapter->pdev->dev)) {
834 con_log(CL_ANN, (KERN_WARNING
835 "megaraid mbox: scsi_add_host failed\n"));
837 scsi_host_put(host);
839 return -1;
842 scsi_scan_host(host);
844 return 0;
849 * megaraid_io_detach - detach a device from the IO subsystem
850 * @param adapter : controller's soft state
852 * Detach this device from the IO subsystem
854 static void
855 megaraid_io_detach(adapter_t *adapter)
857 struct Scsi_Host *host;
859 con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
861 host = adapter->host;
863 scsi_remove_host(host);
865 return;
870 * START: Mailbox Low Level Driver
872 * This is section specific to the single mailbox based controllers
876 * megaraid_init_mbox - initialize controller
877 * @param adapter - our soft state
879 * . Allocate 16-byte aligned mailbox memory for firmware handshake
880 * . Allocate controller's memory resources
881 * . Find out all initialization data
882 * . Allocate memory required for all the commands
883 * . Use internal library of FW routines, build up complete soft state
885 static int __init
886 megaraid_init_mbox(adapter_t *adapter)
888 struct pci_dev *pdev;
889 mraid_device_t *raid_dev;
890 int i;
893 adapter->ito = MBOX_TIMEOUT;
894 pdev = adapter->pdev;
897 * Allocate and initialize the init data structure for mailbox
898 * controllers
900 raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
901 if (raid_dev == NULL) return -1;
903 memset(raid_dev, 0, sizeof(mraid_device_t));
906 * Attach the adapter soft state to raid device soft state
908 adapter->raid_device = (caddr_t)raid_dev;
909 raid_dev->fast_load = megaraid_fast_load;
912 // our baseport
913 raid_dev->baseport = pci_resource_start(pdev, 0);
915 if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
917 con_log(CL_ANN, (KERN_WARNING
918 "megaraid: mem region busy\n"));
920 goto out_free_raid_dev;
923 raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
925 if (!raid_dev->baseaddr) {
927 con_log(CL_ANN, (KERN_WARNING
928 "megaraid: could not map hba memory\n") );
930 goto out_release_regions;
934 // Setup the rest of the soft state using the library of FW routines
937 // request IRQ and register the interrupt service routine
938 if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
939 adapter)) {
941 con_log(CL_ANN, (KERN_WARNING
942 "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
944 goto out_iounmap;
948 // initialize the mutual exclusion lock for the mailbox
949 spin_lock_init(&raid_dev->mailbox_lock);
951 // allocate memory required for commands
952 if (megaraid_alloc_cmd_packets(adapter) != 0) {
953 goto out_free_irq;
956 // Product info
957 if (megaraid_mbox_product_info(adapter) != 0) {
958 goto out_alloc_cmds;
961 // Do we support extended CDBs
962 adapter->max_cdb_sz = 10;
963 if (megaraid_mbox_extended_cdb(adapter) == 0) {
964 adapter->max_cdb_sz = 16;
968 * Do we support cluster environment, if we do, what is the initiator
969 * id.
970 * NOTE: In a non-cluster aware firmware environment, the LLD should
971 * return 7 as initiator id.
973 adapter->ha = 0;
974 adapter->init_id = -1;
975 if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
976 adapter->ha = 1;
980 * Prepare the device ids array to have the mapping between the kernel
981 * device address and megaraid device address.
982 * We export the physical devices on their actual addresses. The
983 * logical drives are exported on a virtual SCSI channel
985 megaraid_mbox_setup_device_map(adapter);
987 // If the firmware supports random deletion, update the device id map
988 if (megaraid_mbox_support_random_del(adapter)) {
990 // Change the logical drives numbers in device_ids array one
991 // slot in device_ids is reserved for target id, that's why
992 // "<=" below
993 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
994 adapter->device_ids[adapter->max_channel][i] += 0x80;
996 adapter->device_ids[adapter->max_channel][adapter->init_id] =
997 0xFF;
999 raid_dev->random_del_supported = 1;
1003 * find out the maximum number of scatter-gather elements supported by
1004 * this firmware
1006 adapter->sglen = megaraid_mbox_get_max_sg(adapter);
1008 // enumerate RAID and SCSI channels so that all devices on SCSI
1009 // channels can later be exported, including disk devices
1010 megaraid_mbox_enum_raid_scsi(adapter);
1013 * Other parameters required by upper layer
1015 * maximum number of sectors per IO command
1017 adapter->max_sectors = megaraid_max_sectors;
1020 * number of queued commands per LUN.
1022 adapter->cmd_per_lun = megaraid_cmd_per_lun;
1025 * Allocate resources required to issue FW calls, when sysfs is
1026 * accessed
1028 if (megaraid_sysfs_alloc_resources(adapter) != 0) {
1029 goto out_alloc_cmds;
1032 // Set the DMA mask to 64-bit. All supported controllers as capable of
1033 // DMA in this range
1034 if (pci_set_dma_mask(adapter->pdev, 0xFFFFFFFFFFFFFFFFULL) != 0) {
1036 con_log(CL_ANN, (KERN_WARNING
1037 "megaraid: could not set DMA mask for 64-bit.\n"));
1039 goto out_free_sysfs_res;
1042 // setup tasklet for DPC
1043 tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
1044 (unsigned long)adapter);
1046 con_log(CL_DLEVEL1, (KERN_INFO
1047 "megaraid mbox hba successfully initialized\n"));
1049 return 0;
1051 out_free_sysfs_res:
1052 megaraid_sysfs_free_resources(adapter);
1053 out_alloc_cmds:
1054 megaraid_free_cmd_packets(adapter);
1055 out_free_irq:
1056 free_irq(adapter->irq, adapter);
1057 out_iounmap:
1058 iounmap(raid_dev->baseaddr);
1059 out_release_regions:
1060 pci_release_regions(pdev);
1061 out_free_raid_dev:
1062 kfree(raid_dev);
1064 return -1;
1069 * megaraid_fini_mbox - undo controller initialization
1070 * @param adapter : our soft state
1072 static void
1073 megaraid_fini_mbox(adapter_t *adapter)
1075 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1077 // flush all caches
1078 megaraid_mbox_flush_cache(adapter);
1080 tasklet_kill(&adapter->dpc_h);
1082 megaraid_sysfs_free_resources(adapter);
1084 megaraid_free_cmd_packets(adapter);
1086 free_irq(adapter->irq, adapter);
1088 iounmap(raid_dev->baseaddr);
1090 pci_release_regions(adapter->pdev);
1092 kfree(raid_dev);
1094 return;
1099 * megaraid_alloc_cmd_packets - allocate shared mailbox
1100 * @param adapter : soft state of the raid controller
1102 * Allocate and align the shared mailbox. This maibox is used to issue
1103 * all the commands. For IO based controllers, the mailbox is also regsitered
1104 * with the FW. Allocate memory for all commands as well.
1105 * This is our big allocator
1107 static int
1108 megaraid_alloc_cmd_packets(adapter_t *adapter)
1110 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1111 struct pci_dev *pdev;
1112 unsigned long align;
1113 scb_t *scb;
1114 mbox_ccb_t *ccb;
1115 struct mraid_pci_blk *epthru_pci_blk;
1116 struct mraid_pci_blk *sg_pci_blk;
1117 struct mraid_pci_blk *mbox_pci_blk;
1118 int i;
1120 pdev = adapter->pdev;
1123 * Setup the mailbox
1124 * Allocate the common 16-byte aligned memory for the handshake
1125 * mailbox.
1127 raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
1128 sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
1130 if (!raid_dev->una_mbox64) {
1131 con_log(CL_ANN, (KERN_WARNING
1132 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1133 __LINE__));
1134 return -1;
1136 memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
1139 * Align the mailbox at 16-byte boundary
1141 raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
1143 raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
1144 (~0UL ^ 0xFUL));
1146 raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
1148 align = ((void *)raid_dev->mbox -
1149 ((void *)&raid_dev->una_mbox64->mbox32));
1151 raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1152 align;
1154 // Allocate memory for commands issued internally
1155 adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1156 &adapter->ibuf_dma_h);
1157 if (!adapter->ibuf) {
1159 con_log(CL_ANN, (KERN_WARNING
1160 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1161 __LINE__));
1163 goto out_free_common_mbox;
1165 memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1167 // Allocate memory for our SCSI Command Blocks and their associated
1168 // memory
1171 * Allocate memory for the base list of scb. Later allocate memory for
1172 * CCBs and embedded components of each CCB and point the pointers in
1173 * scb to the allocated components
1174 * NOTE: The code to allocate SCB will be duplicated in all the LLD
1175 * since the calling routine does not yet know the number of available
1176 * commands.
1178 adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
1179 GFP_KERNEL);
1181 if (adapter->kscb_list == NULL) {
1182 con_log(CL_ANN, (KERN_WARNING
1183 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1184 __LINE__));
1185 goto out_free_ibuf;
1187 memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
1189 // memory allocation for our command packets
1190 if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1191 con_log(CL_ANN, (KERN_WARNING
1192 "megaraid: out of memory, %s %d\n", __FUNCTION__,
1193 __LINE__));
1194 goto out_free_scb_list;
1197 // Adjust the scb pointers and link in the free pool
1198 epthru_pci_blk = raid_dev->epthru_pool;
1199 sg_pci_blk = raid_dev->sg_pool;
1200 mbox_pci_blk = raid_dev->mbox_pool;
1202 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1203 scb = adapter->kscb_list + i;
1204 ccb = raid_dev->ccb_list + i;
1206 ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1207 ccb->raw_mbox = (uint8_t *)ccb->mbox;
1208 ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1209 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1211 // make sure the mailbox is aligned properly
1212 if (ccb->mbox_dma_h & 0x0F) {
1213 con_log(CL_ANN, (KERN_CRIT
1214 "megaraid mbox: not aligned on 16-bytes\n"));
1216 goto out_teardown_dma_pools;
1219 ccb->epthru = (mraid_epassthru_t *)
1220 epthru_pci_blk[i].vaddr;
1221 ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
1222 ccb->pthru = (mraid_passthru_t *)ccb->epthru;
1223 ccb->pthru_dma_h = ccb->epthru_dma_h;
1226 ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1227 ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
1228 ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
1230 scb->ccb = (caddr_t)ccb;
1231 scb->gp = 0;
1233 scb->sno = i; // command index
1235 scb->scp = NULL;
1236 scb->state = SCB_FREE;
1237 scb->dma_direction = PCI_DMA_NONE;
1238 scb->dma_type = MRAID_DMA_NONE;
1239 scb->dev_channel = -1;
1240 scb->dev_target = -1;
1242 // put scb in the free pool
1243 list_add_tail(&scb->list, &adapter->kscb_pool);
1246 return 0;
1248 out_teardown_dma_pools:
1249 megaraid_mbox_teardown_dma_pools(adapter);
1250 out_free_scb_list:
1251 kfree(adapter->kscb_list);
1252 out_free_ibuf:
1253 pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1254 adapter->ibuf_dma_h);
1255 out_free_common_mbox:
1256 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1257 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1259 return -1;
1264 * megaraid_free_cmd_packets - free memory
1265 * @param adapter : soft state of the raid controller
1267 * Release memory resources allocated for commands
1269 static void
1270 megaraid_free_cmd_packets(adapter_t *adapter)
1272 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1274 megaraid_mbox_teardown_dma_pools(adapter);
1276 kfree(adapter->kscb_list);
1278 pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1279 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1281 pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1282 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1283 return;
1288 * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1289 * @param adapter : HBA soft state
1291 * setup the dma pools for mailbox, passthru and extended passthru structures,
1292 * and scatter-gather lists
1294 static int
1295 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1297 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1298 struct mraid_pci_blk *epthru_pci_blk;
1299 struct mraid_pci_blk *sg_pci_blk;
1300 struct mraid_pci_blk *mbox_pci_blk;
1301 int i;
1305 // Allocate memory for 16-bytes aligned mailboxes
1306 raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1307 adapter->pdev,
1308 sizeof(mbox64_t) + 16,
1309 16, 0);
1311 if (raid_dev->mbox_pool_handle == NULL) {
1312 goto fail_setup_dma_pool;
1315 mbox_pci_blk = raid_dev->mbox_pool;
1316 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1317 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1318 raid_dev->mbox_pool_handle,
1319 GFP_KERNEL,
1320 &mbox_pci_blk[i].dma_addr);
1321 if (!mbox_pci_blk[i].vaddr) {
1322 goto fail_setup_dma_pool;
1327 * Allocate memory for each embedded passthru strucuture pointer
1328 * Request for a 128 bytes aligned structure for each passthru command
1329 * structure
1330 * Since passthru and extended passthru commands are exclusive, they
1331 * share common memory pool. Passthru structures piggyback on memory
1332 * allocted to extended passthru since passthru is smaller of the two
1334 raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1335 adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1337 if (raid_dev->epthru_pool_handle == NULL) {
1338 goto fail_setup_dma_pool;
1341 epthru_pci_blk = raid_dev->epthru_pool;
1342 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1343 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1344 raid_dev->epthru_pool_handle,
1345 GFP_KERNEL,
1346 &epthru_pci_blk[i].dma_addr);
1347 if (!epthru_pci_blk[i].vaddr) {
1348 goto fail_setup_dma_pool;
1353 // Allocate memory for each scatter-gather list. Request for 512 bytes
1354 // alignment for each sg list
1355 raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1356 adapter->pdev,
1357 sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1358 512, 0);
1360 if (raid_dev->sg_pool_handle == NULL) {
1361 goto fail_setup_dma_pool;
1364 sg_pci_blk = raid_dev->sg_pool;
1365 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1366 sg_pci_blk[i].vaddr = pci_pool_alloc(
1367 raid_dev->sg_pool_handle,
1368 GFP_KERNEL,
1369 &sg_pci_blk[i].dma_addr);
1370 if (!sg_pci_blk[i].vaddr) {
1371 goto fail_setup_dma_pool;
1375 return 0;
1377 fail_setup_dma_pool:
1378 megaraid_mbox_teardown_dma_pools(adapter);
1379 return -1;
1384 * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1385 * @param adapter : HBA soft state
1387 * teardown the dma pool for mailbox, passthru and extended passthru
1388 * structures, and scatter-gather lists
1390 static void
1391 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1393 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1394 struct mraid_pci_blk *epthru_pci_blk;
1395 struct mraid_pci_blk *sg_pci_blk;
1396 struct mraid_pci_blk *mbox_pci_blk;
1397 int i;
1400 sg_pci_blk = raid_dev->sg_pool;
1401 for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1402 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1403 sg_pci_blk[i].dma_addr);
1405 if (raid_dev->sg_pool_handle)
1406 pci_pool_destroy(raid_dev->sg_pool_handle);
1409 epthru_pci_blk = raid_dev->epthru_pool;
1410 for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1411 pci_pool_free(raid_dev->epthru_pool_handle,
1412 epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1414 if (raid_dev->epthru_pool_handle)
1415 pci_pool_destroy(raid_dev->epthru_pool_handle);
1418 mbox_pci_blk = raid_dev->mbox_pool;
1419 for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1420 pci_pool_free(raid_dev->mbox_pool_handle,
1421 mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1423 if (raid_dev->mbox_pool_handle)
1424 pci_pool_destroy(raid_dev->mbox_pool_handle);
1426 return;
1431 * megaraid_alloc_scb - detach and return a scb from the free list
1432 * @adapter : controller's soft state
1434 * return the scb from the head of the free list. NULL if there are none
1435 * available
1437 static inline scb_t *
1438 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1440 struct list_head *head = &adapter->kscb_pool;
1441 scb_t *scb = NULL;
1442 unsigned long flags;
1444 // detach scb from free pool
1445 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1447 if (list_empty(head)) {
1448 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1449 return NULL;
1452 scb = list_entry(head->next, scb_t, list);
1453 list_del_init(&scb->list);
1455 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1457 scb->state = SCB_ACTIVE;
1458 scb->scp = scp;
1459 scb->dma_type = MRAID_DMA_NONE;
1461 return scb;
1466 * megaraid_dealloc_scb - return the scb to the free pool
1467 * @adapter : controller's soft state
1468 * @scb : scb to be freed
1470 * return the scb back to the free list of scbs. The caller must 'flush' the
1471 * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1472 * NOTE NOTE: Make sure the scb is not on any list before calling this
1473 * routine.
1475 static inline void
1476 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1478 unsigned long flags;
1480 // put scb in the free pool
1481 scb->state = SCB_FREE;
1482 scb->scp = NULL;
1483 spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1485 list_add(&scb->list, &adapter->kscb_pool);
1487 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1489 return;
1494 * megaraid_mbox_mksgl - make the scatter-gather list
1495 * @adapter - controller's soft state
1496 * @scb - scsi control block
1498 * prepare the scatter-gather list
1500 static inline int
1501 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1503 struct scatterlist *sgl;
1504 mbox_ccb_t *ccb;
1505 struct page *page;
1506 unsigned long offset;
1507 struct scsi_cmnd *scp;
1508 int sgcnt;
1509 int i;
1512 scp = scb->scp;
1513 ccb = (mbox_ccb_t *)scb->ccb;
1515 // no mapping required if no data to be transferred
1516 if (!scp->request_buffer || !scp->request_bufflen)
1517 return 0;
1519 if (!scp->use_sg) { /* scatter-gather list not used */
1521 page = virt_to_page(scp->request_buffer);
1523 offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
1525 ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
1526 scp->request_bufflen,
1527 scb->dma_direction);
1528 scb->dma_type = MRAID_DMA_WBUF;
1531 * We need to handle special 64-bit commands that need a
1532 * minimum of 1 SG
1534 sgcnt = 1;
1535 ccb->sgl64[0].address = ccb->buf_dma_h;
1536 ccb->sgl64[0].length = scp->request_bufflen;
1538 return sgcnt;
1541 sgl = (struct scatterlist *)scp->request_buffer;
1543 // The number of sg elements returned must not exceed our limit
1544 sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
1545 scb->dma_direction);
1547 if (sgcnt > adapter->sglen) {
1548 con_log(CL_ANN, (KERN_CRIT
1549 "megaraid critical: too many sg elements:%d\n",
1550 sgcnt));
1551 BUG();
1554 scb->dma_type = MRAID_DMA_WSG;
1556 for (i = 0; i < sgcnt; i++, sgl++) {
1557 ccb->sgl64[i].address = sg_dma_address(sgl);
1558 ccb->sgl64[i].length = sg_dma_len(sgl);
1561 // Return count of SG nodes
1562 return sgcnt;
1567 * mbox_post_cmd - issue a mailbox command
1568 * @adapter - controller's soft state
1569 * @scb - command to be issued
1571 * post the command to the controller if mailbox is availble.
1573 static inline int
1574 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1576 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1577 mbox64_t *mbox64;
1578 mbox_t *mbox;
1579 mbox_ccb_t *ccb;
1580 unsigned long flags;
1581 unsigned int i = 0;
1584 ccb = (mbox_ccb_t *)scb->ccb;
1585 mbox = raid_dev->mbox;
1586 mbox64 = raid_dev->mbox64;
1589 * Check for busy mailbox. If it is, return failure - the caller
1590 * should retry later.
1592 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1594 if (unlikely(mbox->busy)) {
1595 do {
1596 udelay(1);
1597 i++;
1598 rmb();
1599 } while(mbox->busy && (i < max_mbox_busy_wait));
1601 if (mbox->busy) {
1603 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1605 return -1;
1610 // Copy this command's mailbox data into "adapter's" mailbox
1611 memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1612 mbox->cmdid = scb->sno;
1614 adapter->outstanding_cmds++;
1616 if (scb->dma_direction == PCI_DMA_TODEVICE) {
1617 if (!scb->scp->use_sg) { // sg list not used
1618 pci_dma_sync_single_for_device(adapter->pdev,
1619 ccb->buf_dma_h,
1620 scb->scp->request_bufflen,
1621 PCI_DMA_TODEVICE);
1623 else {
1624 pci_dma_sync_sg_for_device(adapter->pdev,
1625 scb->scp->request_buffer,
1626 scb->scp->use_sg, PCI_DMA_TODEVICE);
1630 mbox->busy = 1; // Set busy
1631 mbox->poll = 0;
1632 mbox->ack = 0;
1633 wmb();
1635 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1637 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1639 return 0;
1644 * megaraid_queue_command - generic queue entry point for all LLDs
1645 * @scp : pointer to the scsi command to be executed
1646 * @done : callback routine to be called after the cmd has be completed
1648 * Queue entry point for mailbox based controllers.
1650 static int
1651 megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
1653 adapter_t *adapter;
1654 scb_t *scb;
1655 int if_busy;
1657 adapter = SCP2ADAPTER(scp);
1658 scp->scsi_done = done;
1659 scp->result = 0;
1661 assert_spin_locked(adapter->host_lock);
1663 spin_unlock(adapter->host_lock);
1666 * Allocate and build a SCB request
1667 * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1668 * not allocate scb. We will return non-zero status in that case.
1669 * NOTE: scb can be null even though certain commands completed
1670 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1671 * return 0 in that case, and we would do the callback right away.
1673 if_busy = 0;
1674 scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1676 if (scb) {
1677 megaraid_mbox_runpendq(adapter, scb);
1680 spin_lock(adapter->host_lock);
1682 if (!scb) { // command already completed
1683 done(scp);
1684 return 0;
1687 return if_busy;
1692 * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
1693 * firmware lingua
1694 * @adapter - controller's soft state
1695 * @scp - mid-layer scsi command pointer
1696 * @busy - set if request could not be completed because of lack of
1697 * resources
1699 * convert the command issued by mid-layer to format understood by megaraid
1700 * firmware. We also complete certain command without sending them to firmware
1702 static scb_t *
1703 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1705 mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
1706 int channel;
1707 int target;
1708 int islogical;
1709 mbox_ccb_t *ccb;
1710 mraid_passthru_t *pthru;
1711 mbox64_t *mbox64;
1712 mbox_t *mbox;
1713 scb_t *scb;
1714 char skip[] = "skipping";
1715 char scan[] = "scanning";
1716 char *ss;
1720 * Get the appropriate device map for the device this command is
1721 * intended for
1723 MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1726 * Logical drive commands
1728 if (islogical) {
1729 switch (scp->cmnd[0]) {
1730 case TEST_UNIT_READY:
1732 * Do we support clustering and is the support enabled
1733 * If no, return success always
1735 if (!adapter->ha) {
1736 scp->result = (DID_OK << 16);
1737 return NULL;
1740 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1741 scp->result = (DID_ERROR << 16);
1742 *busy = 1;
1743 return NULL;
1746 scb->dma_direction = scp->sc_data_direction;
1747 scb->dev_channel = 0xFF;
1748 scb->dev_target = target;
1749 ccb = (mbox_ccb_t *)scb->ccb;
1752 * The command id will be provided by the command
1753 * issuance routine
1755 ccb->raw_mbox[0] = CLUSTER_CMD;
1756 ccb->raw_mbox[2] = RESERVATION_STATUS;
1757 ccb->raw_mbox[3] = target;
1759 return scb;
1761 case MODE_SENSE:
1762 if (scp->use_sg) {
1763 struct scatterlist *sgl;
1764 caddr_t vaddr;
1766 sgl = (struct scatterlist *)scp->request_buffer;
1767 if (sgl->page) {
1768 vaddr = (caddr_t)
1769 (page_address((&sgl[0])->page)
1770 + (&sgl[0])->offset);
1772 memset(vaddr, 0, scp->cmnd[4]);
1774 else {
1775 con_log(CL_ANN, (KERN_WARNING
1776 "megaraid mailbox: invalid sg:%d\n",
1777 __LINE__));
1780 else {
1781 memset(scp->request_buffer, 0, scp->cmnd[4]);
1783 scp->result = (DID_OK << 16);
1784 return NULL;
1786 case INQUIRY:
1788 * Display the channel scan for logical drives
1789 * Do not display scan for a channel if already done.
1791 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1793 con_log(CL_ANN, (KERN_INFO
1794 "scsi[%d]: scanning scsi channel %d",
1795 adapter->host->host_no,
1796 SCP2CHANNEL(scp)));
1798 con_log(CL_ANN, (
1799 " [virtual] for logical drives\n"));
1801 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1804 /* Fall through */
1806 case READ_CAPACITY:
1808 * Do not allow LUN > 0 for logical drives and
1809 * requests for more than 40 logical drives
1811 if (SCP2LUN(scp)) {
1812 scp->result = (DID_BAD_TARGET << 16);
1813 return NULL;
1815 if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1816 scp->result = (DID_BAD_TARGET << 16);
1817 return NULL;
1821 /* Allocate a SCB and initialize passthru */
1822 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1823 scp->result = (DID_ERROR << 16);
1824 *busy = 1;
1825 return NULL;
1828 ccb = (mbox_ccb_t *)scb->ccb;
1829 scb->dev_channel = 0xFF;
1830 scb->dev_target = target;
1831 pthru = ccb->pthru;
1832 mbox = ccb->mbox;
1833 mbox64 = ccb->mbox64;
1835 pthru->timeout = 0;
1836 pthru->ars = 1;
1837 pthru->reqsenselen = 14;
1838 pthru->islogical = 1;
1839 pthru->logdrv = target;
1840 pthru->cdblen = scp->cmd_len;
1841 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1843 mbox->cmd = MBOXCMD_PASSTHRU64;
1844 scb->dma_direction = scp->sc_data_direction;
1846 pthru->dataxferlen = scp->request_bufflen;
1847 pthru->dataxferaddr = ccb->sgl_dma_h;
1848 pthru->numsge = megaraid_mbox_mksgl(adapter,
1849 scb);
1851 mbox->xferaddr = 0xFFFFFFFF;
1852 mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
1853 mbox64->xferaddr_hi = 0;
1855 return scb;
1857 case READ_6:
1858 case WRITE_6:
1859 case READ_10:
1860 case WRITE_10:
1861 case READ_12:
1862 case WRITE_12:
1865 * Allocate a SCB and initialize mailbox
1867 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1868 scp->result = (DID_ERROR << 16);
1869 *busy = 1;
1870 return NULL;
1872 ccb = (mbox_ccb_t *)scb->ccb;
1873 scb->dev_channel = 0xFF;
1874 scb->dev_target = target;
1875 mbox = ccb->mbox;
1876 mbox64 = ccb->mbox64;
1877 mbox->logdrv = target;
1880 * A little HACK: 2nd bit is zero for all scsi read
1881 * commands and is set for all scsi write commands
1883 mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
1884 MBOXCMD_LREAD64 ;
1887 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1889 if (scp->cmd_len == 6) {
1890 mbox->numsectors = (uint32_t)scp->cmnd[4];
1891 mbox->lba =
1892 ((uint32_t)scp->cmnd[1] << 16) |
1893 ((uint32_t)scp->cmnd[2] << 8) |
1894 (uint32_t)scp->cmnd[3];
1896 mbox->lba &= 0x1FFFFF;
1900 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1902 else if (scp->cmd_len == 10) {
1903 mbox->numsectors =
1904 (uint32_t)scp->cmnd[8] |
1905 ((uint32_t)scp->cmnd[7] << 8);
1906 mbox->lba =
1907 ((uint32_t)scp->cmnd[2] << 24) |
1908 ((uint32_t)scp->cmnd[3] << 16) |
1909 ((uint32_t)scp->cmnd[4] << 8) |
1910 (uint32_t)scp->cmnd[5];
1914 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1916 else if (scp->cmd_len == 12) {
1917 mbox->lba =
1918 ((uint32_t)scp->cmnd[2] << 24) |
1919 ((uint32_t)scp->cmnd[3] << 16) |
1920 ((uint32_t)scp->cmnd[4] << 8) |
1921 (uint32_t)scp->cmnd[5];
1923 mbox->numsectors =
1924 ((uint32_t)scp->cmnd[6] << 24) |
1925 ((uint32_t)scp->cmnd[7] << 16) |
1926 ((uint32_t)scp->cmnd[8] << 8) |
1927 (uint32_t)scp->cmnd[9];
1929 else {
1930 con_log(CL_ANN, (KERN_WARNING
1931 "megaraid: unsupported CDB length\n"));
1933 megaraid_dealloc_scb(adapter, scb);
1935 scp->result = (DID_ERROR << 16);
1936 return NULL;
1939 scb->dma_direction = scp->sc_data_direction;
1941 // Calculate Scatter-Gather info
1942 mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
1943 mbox->numsge = megaraid_mbox_mksgl(adapter,
1944 scb);
1945 mbox->xferaddr = 0xFFFFFFFF;
1946 mbox64->xferaddr_hi = 0;
1948 return scb;
1950 case RESERVE:
1951 case RELEASE:
1953 * Do we support clustering and is the support enabled
1955 if (!adapter->ha) {
1956 scp->result = (DID_BAD_TARGET << 16);
1957 return NULL;
1961 * Allocate a SCB and initialize mailbox
1963 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1964 scp->result = (DID_ERROR << 16);
1965 *busy = 1;
1966 return NULL;
1969 ccb = (mbox_ccb_t *)scb->ccb;
1970 scb->dev_channel = 0xFF;
1971 scb->dev_target = target;
1972 ccb->raw_mbox[0] = CLUSTER_CMD;
1973 ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
1974 RESERVE_LD : RELEASE_LD;
1976 ccb->raw_mbox[3] = target;
1977 scb->dma_direction = scp->sc_data_direction;
1979 return scb;
1981 default:
1982 scp->result = (DID_BAD_TARGET << 16);
1983 return NULL;
1986 else { // Passthru device commands
1988 // Do not allow access to target id > 15 or LUN > 7
1989 if (target > 15 || SCP2LUN(scp) > 7) {
1990 scp->result = (DID_BAD_TARGET << 16);
1991 return NULL;
1994 // if fast load option was set and scan for last device is
1995 // over, reset the fast_load flag so that during a possible
1996 // next scan, devices can be made available
1997 if (rdev->fast_load && (target == 15) &&
1998 (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
2000 con_log(CL_ANN, (KERN_INFO
2001 "megaraid[%d]: physical device scan re-enabled\n",
2002 adapter->host->host_no));
2003 rdev->fast_load = 0;
2007 * Display the channel scan for physical devices
2009 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
2011 ss = rdev->fast_load ? skip : scan;
2013 con_log(CL_ANN, (KERN_INFO
2014 "scsi[%d]: %s scsi channel %d [Phy %d]",
2015 adapter->host->host_no, ss, SCP2CHANNEL(scp),
2016 channel));
2018 con_log(CL_ANN, (
2019 " for non-raid devices\n"));
2021 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
2024 // disable channel sweep if fast load option given
2025 if (rdev->fast_load) {
2026 scp->result = (DID_BAD_TARGET << 16);
2027 return NULL;
2030 // Allocate a SCB and initialize passthru
2031 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
2032 scp->result = (DID_ERROR << 16);
2033 *busy = 1;
2034 return NULL;
2037 ccb = (mbox_ccb_t *)scb->ccb;
2038 scb->dev_channel = channel;
2039 scb->dev_target = target;
2040 scb->dma_direction = scp->sc_data_direction;
2041 mbox = ccb->mbox;
2042 mbox64 = ccb->mbox64;
2044 // Does this firmware support extended CDBs
2045 if (adapter->max_cdb_sz == 16) {
2046 mbox->cmd = MBOXCMD_EXTPTHRU;
2048 megaraid_mbox_prepare_epthru(adapter, scb, scp);
2050 mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
2051 mbox64->xferaddr_hi = 0;
2052 mbox->xferaddr = 0xFFFFFFFF;
2054 else {
2055 mbox->cmd = MBOXCMD_PASSTHRU64;
2057 megaraid_mbox_prepare_pthru(adapter, scb, scp);
2059 mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
2060 mbox64->xferaddr_hi = 0;
2061 mbox->xferaddr = 0xFFFFFFFF;
2063 return scb;
2066 // NOT REACHED
2071 * megaraid_mbox_runpendq - execute commands queued in the pending queue
2072 * @adapter : controller's soft state
2073 * @scb : SCB to be queued in the pending list
2075 * scan the pending list for commands which are not yet issued and try to
2076 * post to the controller. The SCB can be a null pointer, which would indicate
2077 * no SCB to be queue, just try to execute the ones in the pending list.
2079 * NOTE: We do not actually traverse the pending list. The SCBs are plucked
2080 * out from the head of the pending list. If it is successfully issued, the
2081 * next SCB is at the head now.
2083 static void
2084 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
2086 scb_t *scb;
2087 unsigned long flags;
2089 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2091 if (scb_q) {
2092 scb_q->state = SCB_PENDQ;
2093 list_add_tail(&scb_q->list, &adapter->pend_list);
2096 // if the adapter in not in quiescent mode, post the commands to FW
2097 if (adapter->quiescent) {
2098 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2099 return;
2102 while (!list_empty(&adapter->pend_list)) {
2104 assert_spin_locked(PENDING_LIST_LOCK(adapter));
2106 scb = list_entry(adapter->pend_list.next, scb_t, list);
2108 // remove the scb from the pending list and try to
2109 // issue. If we are unable to issue it, put back in
2110 // the pending list and return
2112 list_del_init(&scb->list);
2114 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2116 // if mailbox was busy, return SCB back to pending
2117 // list. Make sure to add at the head, since that's
2118 // where it would have been removed from
2120 scb->state = SCB_ISSUED;
2122 if (mbox_post_cmd(adapter, scb) != 0) {
2124 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2126 scb->state = SCB_PENDQ;
2128 list_add(&scb->list, &adapter->pend_list);
2130 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2131 flags);
2133 return;
2136 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2139 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2142 return;
2147 * megaraid_mbox_prepare_pthru - prepare a command for physical devices
2148 * @adapter - pointer to controller's soft state
2149 * @scb - scsi control block
2150 * @scp - scsi command from the mid-layer
2152 * prepare a command for the scsi physical devices
2154 static void
2155 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
2156 struct scsi_cmnd *scp)
2158 mbox_ccb_t *ccb;
2159 mraid_passthru_t *pthru;
2160 uint8_t channel;
2161 uint8_t target;
2163 ccb = (mbox_ccb_t *)scb->ccb;
2164 pthru = ccb->pthru;
2165 channel = scb->dev_channel;
2166 target = scb->dev_target;
2168 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2169 pthru->timeout = 4;
2170 pthru->ars = 1;
2171 pthru->islogical = 0;
2172 pthru->channel = 0;
2173 pthru->target = (channel << 4) | target;
2174 pthru->logdrv = SCP2LUN(scp);
2175 pthru->reqsenselen = 14;
2176 pthru->cdblen = scp->cmd_len;
2178 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2180 if (scp->request_bufflen) {
2181 pthru->dataxferlen = scp->request_bufflen;
2182 pthru->dataxferaddr = ccb->sgl_dma_h;
2183 pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2185 else {
2186 pthru->dataxferaddr = 0;
2187 pthru->dataxferlen = 0;
2188 pthru->numsge = 0;
2190 return;
2195 * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2196 * @adapter - pointer to controller's soft state
2197 * @scb - scsi control block
2198 * @scp - scsi command from the mid-layer
2200 * prepare a command for the scsi physical devices. This rountine prepares
2201 * commands for devices which can take extended CDBs (>10 bytes)
2203 static void
2204 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2205 struct scsi_cmnd *scp)
2207 mbox_ccb_t *ccb;
2208 mraid_epassthru_t *epthru;
2209 uint8_t channel;
2210 uint8_t target;
2212 ccb = (mbox_ccb_t *)scb->ccb;
2213 epthru = ccb->epthru;
2214 channel = scb->dev_channel;
2215 target = scb->dev_target;
2217 // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2218 epthru->timeout = 4;
2219 epthru->ars = 1;
2220 epthru->islogical = 0;
2221 epthru->channel = 0;
2222 epthru->target = (channel << 4) | target;
2223 epthru->logdrv = SCP2LUN(scp);
2224 epthru->reqsenselen = 14;
2225 epthru->cdblen = scp->cmd_len;
2227 memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2229 if (scp->request_bufflen) {
2230 epthru->dataxferlen = scp->request_bufflen;
2231 epthru->dataxferaddr = ccb->sgl_dma_h;
2232 epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
2234 else {
2235 epthru->dataxferaddr = 0;
2236 epthru->dataxferlen = 0;
2237 epthru->numsge = 0;
2239 return;
2244 * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2245 * @adapter - controller's soft state
2247 * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
2248 * completed command and put them on the completed list for later processing.
2250 * Returns: 1 if the interrupt is valid, 0 otherwise
2252 static inline int
2253 megaraid_ack_sequence(adapter_t *adapter)
2255 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2256 mbox_t *mbox;
2257 scb_t *scb;
2258 uint8_t nstatus;
2259 uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
2260 struct list_head clist;
2261 int handled;
2262 uint32_t dword;
2263 unsigned long flags;
2264 int i, j;
2267 mbox = raid_dev->mbox;
2269 // move the SCBs from the firmware completed array to our local list
2270 INIT_LIST_HEAD(&clist);
2272 // loop till F/W has more commands for us to complete
2273 handled = 0;
2274 spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2275 do {
2277 * Check if a valid interrupt is pending. If found, force the
2278 * interrupt line low.
2280 dword = RDOUTDOOR(raid_dev);
2281 if (dword != 0x10001234) break;
2283 handled = 1;
2285 WROUTDOOR(raid_dev, 0x10001234);
2287 nstatus = 0;
2288 // wait for valid numstatus to post
2289 for (i = 0; i < 0xFFFFF; i++) {
2290 if (mbox->numstatus != 0xFF) {
2291 nstatus = mbox->numstatus;
2292 break;
2294 rmb();
2296 mbox->numstatus = 0xFF;
2298 adapter->outstanding_cmds -= nstatus;
2300 for (i = 0; i < nstatus; i++) {
2302 // wait for valid command index to post
2303 for (j = 0; j < 0xFFFFF; j++) {
2304 if (mbox->completed[i] != 0xFF) break;
2305 rmb();
2307 completed[i] = mbox->completed[i];
2308 mbox->completed[i] = 0xFF;
2310 if (completed[i] == 0xFF) {
2311 con_log(CL_ANN, (KERN_CRIT
2312 "megaraid: command posting timed out\n"));
2314 BUG();
2315 continue;
2318 // Get SCB associated with this command id
2319 if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2320 // a cmm command
2321 scb = adapter->uscb_list + (completed[i] -
2322 MBOX_MAX_SCSI_CMDS);
2324 else {
2325 // an os command
2326 scb = adapter->kscb_list + completed[i];
2329 scb->status = mbox->status;
2330 list_add_tail(&scb->list, &clist);
2333 // Acknowledge interrupt
2334 WRINDOOR(raid_dev, 0x02);
2336 } while(1);
2338 spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2341 // put the completed commands in the completed list. DPC would
2342 // complete these commands later
2343 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2345 list_splice(&clist, &adapter->completed_list);
2347 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2350 // schedule the DPC if there is some work for it
2351 if (handled)
2352 tasklet_schedule(&adapter->dpc_h);
2354 return handled;
2359 * megaraid_isr - isr for memory based mailbox based controllers
2360 * @irq - irq
2361 * @devp - pointer to our soft state
2362 * @regs - unused
2364 * Interrupt service routine for memory-mapped mailbox controllers.
2366 static irqreturn_t
2367 megaraid_isr(int irq, void *devp, struct pt_regs *regs)
2369 adapter_t *adapter = devp;
2370 int handled;
2372 handled = megaraid_ack_sequence(adapter);
2374 /* Loop through any pending requests */
2375 if (!adapter->quiescent) {
2376 megaraid_mbox_runpendq(adapter, NULL);
2379 return IRQ_RETVAL(handled);
2384 * megaraid_mbox_sync_scb - sync kernel buffers
2385 * @adapter : controller's soft state
2386 * @scb : pointer to the resource packet
2388 * DMA sync if required.
2390 static inline void
2391 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2393 mbox_ccb_t *ccb;
2395 ccb = (mbox_ccb_t *)scb->ccb;
2397 switch (scb->dma_type) {
2399 case MRAID_DMA_WBUF:
2400 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2401 pci_dma_sync_single_for_cpu(adapter->pdev,
2402 ccb->buf_dma_h,
2403 scb->scp->request_bufflen,
2404 PCI_DMA_FROMDEVICE);
2407 pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
2408 scb->scp->request_bufflen, scb->dma_direction);
2410 break;
2412 case MRAID_DMA_WSG:
2413 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2414 pci_dma_sync_sg_for_cpu(adapter->pdev,
2415 scb->scp->request_buffer,
2416 scb->scp->use_sg, PCI_DMA_FROMDEVICE);
2419 pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
2420 scb->scp->use_sg, scb->dma_direction);
2422 break;
2424 default:
2425 break;
2428 return;
2433 * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2434 * @devp : pointer to HBA soft state
2436 * Pick up the commands from the completed list and send back to the owners.
2437 * This is a reentrant function and does not assume any locks are held while
2438 * it is being called.
2440 static void
2441 megaraid_mbox_dpc(unsigned long devp)
2443 adapter_t *adapter = (adapter_t *)devp;
2444 mraid_device_t *raid_dev;
2445 struct list_head clist;
2446 struct scatterlist *sgl;
2447 scb_t *scb;
2448 scb_t *tmp;
2449 struct scsi_cmnd *scp;
2450 mraid_passthru_t *pthru;
2451 mraid_epassthru_t *epthru;
2452 mbox_ccb_t *ccb;
2453 int islogical;
2454 int pdev_index;
2455 int pdev_state;
2456 mbox_t *mbox;
2457 unsigned long flags;
2458 uint8_t c;
2459 int status;
2462 if (!adapter) return;
2464 raid_dev = ADAP2RAIDDEV(adapter);
2466 // move the SCBs from the completed list to our local list
2467 INIT_LIST_HEAD(&clist);
2469 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2471 list_splice_init(&adapter->completed_list, &clist);
2473 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2476 list_for_each_entry_safe(scb, tmp, &clist, list) {
2478 status = scb->status;
2479 scp = scb->scp;
2480 ccb = (mbox_ccb_t *)scb->ccb;
2481 pthru = ccb->pthru;
2482 epthru = ccb->epthru;
2483 mbox = ccb->mbox;
2485 // Make sure f/w has completed a valid command
2486 if (scb->state != SCB_ISSUED) {
2487 con_log(CL_ANN, (KERN_CRIT
2488 "megaraid critical err: invalid command %d:%d:%p\n",
2489 scb->sno, scb->state, scp));
2490 BUG();
2491 continue; // Must never happen!
2494 // check for the management command and complete it right away
2495 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2496 scb->state = SCB_FREE;
2497 scb->status = status;
2499 // remove from local clist
2500 list_del_init(&scb->list);
2502 megaraid_mbox_mm_done(adapter, scb);
2504 continue;
2507 // Was an abort issued for this command earlier
2508 if (scb->state & SCB_ABORT) {
2509 con_log(CL_ANN, (KERN_NOTICE
2510 "megaraid: aborted cmd %lx[%x] completed\n",
2511 scp->serial_number, scb->sno));
2515 * If the inquiry came of a disk drive which is not part of
2516 * any RAID array, expose it to the kernel. For this to be
2517 * enabled, user must set the "megaraid_expose_unconf_disks"
2518 * flag to 1 by specifying it on module parameter list.
2519 * This would enable data migration off drives from other
2520 * configurations.
2522 islogical = MRAID_IS_LOGICAL(adapter, scp);
2523 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2524 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2526 if (scp->use_sg) {
2527 sgl = (struct scatterlist *)
2528 scp->request_buffer;
2530 if (sgl->page) {
2531 c = *(unsigned char *)
2532 (page_address((&sgl[0])->page) +
2533 (&sgl[0])->offset);
2535 else {
2536 con_log(CL_ANN, (KERN_WARNING
2537 "megaraid mailbox: invalid sg:%d\n",
2538 __LINE__));
2539 c = 0;
2542 else {
2543 c = *(uint8_t *)scp->request_buffer;
2546 if ((c & 0x1F ) == TYPE_DISK) {
2547 pdev_index = (scb->dev_channel * 16) +
2548 scb->dev_target;
2549 pdev_state =
2550 raid_dev->pdrv_state[pdev_index] & 0x0F;
2552 if (pdev_state == PDRV_ONLINE ||
2553 pdev_state == PDRV_FAILED ||
2554 pdev_state == PDRV_RBLD ||
2555 pdev_state == PDRV_HOTSPARE ||
2556 megaraid_expose_unconf_disks == 0) {
2558 status = 0xF0;
2563 // Convert MegaRAID status to Linux error code
2564 switch (status) {
2566 case 0x00:
2568 scp->result = (DID_OK << 16);
2569 break;
2571 case 0x02:
2573 /* set sense_buffer and result fields */
2574 if (mbox->cmd == MBOXCMD_PASSTHRU ||
2575 mbox->cmd == MBOXCMD_PASSTHRU64) {
2577 memcpy(scp->sense_buffer, pthru->reqsensearea,
2578 14);
2580 scp->result = DRIVER_SENSE << 24 |
2581 DID_OK << 16 | CHECK_CONDITION << 1;
2583 else {
2584 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2586 memcpy(scp->sense_buffer,
2587 epthru->reqsensearea, 14);
2589 scp->result = DRIVER_SENSE << 24 |
2590 DID_OK << 16 |
2591 CHECK_CONDITION << 1;
2592 } else {
2593 scp->sense_buffer[0] = 0x70;
2594 scp->sense_buffer[2] = ABORTED_COMMAND;
2595 scp->result = CHECK_CONDITION << 1;
2598 break;
2600 case 0x08:
2602 scp->result = DID_BUS_BUSY << 16 | status;
2603 break;
2605 default:
2608 * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2609 * failed
2611 if (scp->cmnd[0] == TEST_UNIT_READY) {
2612 scp->result = DID_ERROR << 16 |
2613 RESERVATION_CONFLICT << 1;
2615 else
2617 * Error code returned is 1 if Reserve or Release
2618 * failed or the input parameter is invalid
2620 if (status == 1 && (scp->cmnd[0] == RESERVE ||
2621 scp->cmnd[0] == RELEASE)) {
2623 scp->result = DID_ERROR << 16 |
2624 RESERVATION_CONFLICT << 1;
2626 else {
2627 scp->result = DID_BAD_TARGET << 16 | status;
2631 // print a debug message for all failed commands
2632 if (status) {
2633 megaraid_mbox_display_scb(adapter, scb);
2636 // Free our internal resources and call the mid-layer callback
2637 // routine
2638 megaraid_mbox_sync_scb(adapter, scb);
2640 // remove from local clist
2641 list_del_init(&scb->list);
2643 // put back in free list
2644 megaraid_dealloc_scb(adapter, scb);
2646 // send the scsi packet back to kernel
2647 spin_lock(adapter->host_lock);
2648 scp->scsi_done(scp);
2649 spin_unlock(adapter->host_lock);
2652 return;
2657 * megaraid_abort_handler - abort the scsi command
2658 * @scp : command to be aborted
2660 * Abort a previous SCSI request. Only commands on the pending list can be
2661 * aborted. All the commands issued to the F/W must complete.
2663 static int
2664 megaraid_abort_handler(struct scsi_cmnd *scp)
2666 adapter_t *adapter;
2667 mraid_device_t *raid_dev;
2668 scb_t *scb;
2669 scb_t *tmp;
2670 int found;
2671 unsigned long flags;
2672 int i;
2675 adapter = SCP2ADAPTER(scp);
2676 raid_dev = ADAP2RAIDDEV(adapter);
2678 assert_spin_locked(adapter->host_lock);
2680 con_log(CL_ANN, (KERN_WARNING
2681 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2682 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2683 SCP2TARGET(scp), SCP2LUN(scp)));
2685 // If FW has stopped responding, simply return failure
2686 if (raid_dev->hw_error) {
2687 con_log(CL_ANN, (KERN_NOTICE
2688 "megaraid: hw error, not aborting\n"));
2689 return FAILED;
2692 // There might a race here, where the command was completed by the
2693 // firmware and now it is on the completed list. Before we could
2694 // complete the command to the kernel in dpc, the abort came.
2695 // Find out if this is the case to avoid the race.
2696 scb = NULL;
2697 spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2698 list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2700 if (scb->scp == scp) { // Found command
2702 list_del_init(&scb->list); // from completed list
2704 con_log(CL_ANN, (KERN_WARNING
2705 "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2706 scp->serial_number, scb->sno,
2707 scb->dev_channel, scb->dev_target));
2709 scp->result = (DID_ABORT << 16);
2710 scp->scsi_done(scp);
2712 megaraid_dealloc_scb(adapter, scb);
2714 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2715 flags);
2717 return SUCCESS;
2720 spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2723 // Find out if this command is still on the pending list. If it is and
2724 // was never issued, abort and return success. If the command is owned
2725 // by the firmware, we must wait for it to complete by the FW.
2726 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2727 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2729 if (scb->scp == scp) { // Found command
2731 list_del_init(&scb->list); // from pending list
2733 ASSERT(!(scb->state & SCB_ISSUED));
2735 con_log(CL_ANN, (KERN_WARNING
2736 "megaraid abort: %ld[%d:%d], driver owner\n",
2737 scp->serial_number, scb->dev_channel,
2738 scb->dev_target));
2740 scp->result = (DID_ABORT << 16);
2741 scp->scsi_done(scp);
2743 megaraid_dealloc_scb(adapter, scb);
2745 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2746 flags);
2748 return SUCCESS;
2751 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2754 // Check do we even own this command, in which case this would be
2755 // owned by the firmware. The only way to locate the FW scb is to
2756 // traverse through the list of all SCB, since driver does not
2757 // maintain these SCBs on any list
2758 found = 0;
2759 for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2760 scb = adapter->kscb_list + i;
2762 if (scb->scp == scp) {
2764 found = 1;
2766 if (!(scb->state & SCB_ISSUED)) {
2767 con_log(CL_ANN, (KERN_WARNING
2768 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2769 scp->serial_number, scb->sno, scb->dev_channel,
2770 scb->dev_target));
2771 BUG();
2773 else {
2774 con_log(CL_ANN, (KERN_WARNING
2775 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2776 scp->serial_number, scb->sno, scb->dev_channel,
2777 scb->dev_target));
2782 if (!found) {
2783 con_log(CL_ANN, (KERN_WARNING
2784 "megaraid abort: scsi cmd:%ld, do now own\n",
2785 scp->serial_number));
2787 // FIXME: Should there be a callback for this command?
2788 return SUCCESS;
2791 // We cannot actually abort a command owned by firmware, return
2792 // failure and wait for reset. In host reset handler, we will find out
2793 // if the HBA is still live
2794 return FAILED;
2799 * megaraid_reset_handler - device reset hadler for mailbox based driver
2800 * @scp : reference command
2802 * Reset handler for the mailbox based controller. First try to find out if
2803 * the FW is still live, in which case the outstanding commands counter mut go
2804 * down to 0. If that happens, also issue the reservation reset command to
2805 * relinquish (possible) reservations on the logical drives connected to this
2806 * host
2808 static int
2809 megaraid_reset_handler(struct scsi_cmnd *scp)
2811 adapter_t *adapter;
2812 scb_t *scb;
2813 scb_t *tmp;
2814 mraid_device_t *raid_dev;
2815 unsigned long flags;
2816 uint8_t raw_mbox[sizeof(mbox_t)];
2817 int rval;
2818 int recovery_window;
2819 int recovering;
2820 int i;
2822 adapter = SCP2ADAPTER(scp);
2823 raid_dev = ADAP2RAIDDEV(adapter);
2825 assert_spin_locked(adapter->host_lock);
2827 con_log(CL_ANN, (KERN_WARNING "megaraid: reseting the host...\n"));
2829 // return failure if adapter is not responding
2830 if (raid_dev->hw_error) {
2831 con_log(CL_ANN, (KERN_NOTICE
2832 "megaraid: hw error, cannot reset\n"));
2833 return FAILED;
2837 // Under exceptional conditions, FW can take up to 3 minutes to
2838 // complete command processing. Wait for additional 2 minutes for the
2839 // pending commands counter to go down to 0. If it doesn't, let the
2840 // controller be marked offline
2841 // Also, reset all the commands currently owned by the driver
2842 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2843 list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2845 list_del_init(&scb->list); // from pending list
2847 con_log(CL_ANN, (KERN_WARNING
2848 "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2849 scp->serial_number, scb->sno,
2850 scb->dev_channel, scb->dev_target));
2852 scp->result = (DID_RESET << 16);
2853 scp->scsi_done(scp);
2855 megaraid_dealloc_scb(adapter, scb);
2857 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2859 if (adapter->outstanding_cmds) {
2860 con_log(CL_ANN, (KERN_NOTICE
2861 "megaraid: %d outstanding commands. Max wait %d sec\n",
2862 adapter->outstanding_cmds, MBOX_RESET_WAIT));
2865 spin_unlock(adapter->host_lock);
2867 recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2869 recovering = adapter->outstanding_cmds;
2871 for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
2873 megaraid_ack_sequence(adapter);
2875 // print a message once every 5 seconds only
2876 if (!(i % 5)) {
2877 con_log(CL_ANN, (
2878 "megaraid mbox: Wait for %d commands to complete:%d\n",
2879 adapter->outstanding_cmds,
2880 MBOX_RESET_WAIT - i));
2883 // bailout if no recovery happended in reset time
2884 if ((i == MBOX_RESET_WAIT) &&
2885 (recovering == adapter->outstanding_cmds)) {
2886 break;
2889 msleep(1000);
2892 spin_lock(adapter->host_lock);
2894 // If still outstanding commands, bail out
2895 if (adapter->outstanding_cmds) {
2896 con_log(CL_ANN, (KERN_WARNING
2897 "megaraid mbox: critical hardware error!\n"));
2899 raid_dev->hw_error = 1;
2901 return FAILED;
2903 else {
2904 con_log(CL_ANN, (KERN_NOTICE
2905 "megaraid mbox: reset sequence completed sucessfully\n"));
2909 // If the controller supports clustering, reset reservations
2910 if (!adapter->ha) return SUCCESS;
2912 // clear reservations if any
2913 raw_mbox[0] = CLUSTER_CMD;
2914 raw_mbox[2] = RESET_RESERVATIONS;
2916 rval = SUCCESS;
2917 if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2918 con_log(CL_ANN,
2919 (KERN_INFO "megaraid: reservation reset\n"));
2921 else {
2922 rval = FAILED;
2923 con_log(CL_ANN, (KERN_WARNING
2924 "megaraid: reservation reset failed\n"));
2927 return rval;
2932 * START: internal commands library
2934 * This section of the driver has the common routine used by the driver and
2935 * also has all the FW routines
2939 * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2940 * @adapter - controller's soft state
2941 * @raw_mbox - the mailbox
2943 * Issue a scb in synchronous and non-interrupt mode for mailbox based
2944 * controllers
2946 static int
2947 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2949 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
2950 mbox64_t *mbox64;
2951 mbox_t *mbox;
2952 uint8_t status;
2953 int i;
2956 mbox64 = raid_dev->mbox64;
2957 mbox = raid_dev->mbox;
2960 * Wait until mailbox is free
2962 if (megaraid_busywait_mbox(raid_dev) != 0)
2963 goto blocked_mailbox;
2966 * Copy mailbox data into host structure
2968 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2969 mbox->cmdid = 0xFE;
2970 mbox->busy = 1;
2971 mbox->poll = 0;
2972 mbox->ack = 0;
2973 mbox->numstatus = 0xFF;
2974 mbox->status = 0xFF;
2976 wmb();
2977 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2979 // wait for maximum 1 second for status to post. If the status is not
2980 // available within 1 second, assume FW is initializing and wait
2981 // for an extended amount of time
2982 if (mbox->numstatus == 0xFF) { // status not yet available
2983 udelay(25);;
2985 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2986 rmb();
2987 msleep(1);
2991 if (i == 1000) {
2992 con_log(CL_ANN, (KERN_NOTICE
2993 "megaraid mailbox: wait for FW to boot "));
2995 for (i = 0; (mbox->numstatus == 0xFF) &&
2996 (i < MBOX_RESET_WAIT); i++) {
2997 rmb();
2998 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2999 MBOX_RESET_WAIT - i));
3000 msleep(1000);
3003 if (i == MBOX_RESET_WAIT) {
3005 con_log(CL_ANN, (
3006 "\nmegaraid mailbox: status not available\n"));
3008 return -1;
3010 con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
3014 // wait for maximum 1 second for poll semaphore
3015 if (mbox->poll != 0x77) {
3016 udelay(25);
3018 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
3019 rmb();
3020 msleep(1);
3023 if (i == 1000) {
3024 con_log(CL_ANN, (KERN_WARNING
3025 "megaraid mailbox: could not get poll semaphore\n"));
3026 return -1;
3030 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
3031 wmb();
3033 // wait for maximum 1 second for acknowledgement
3034 if (RDINDOOR(raid_dev) & 0x2) {
3035 udelay(25);
3037 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
3038 rmb();
3039 msleep(1);
3042 if (i == 1000) {
3043 con_log(CL_ANN, (KERN_WARNING
3044 "megaraid mailbox: could not acknowledge\n"));
3045 return -1;
3048 mbox->poll = 0;
3049 mbox->ack = 0x77;
3051 status = mbox->status;
3053 // invalidate the completed command id array. After command
3054 // completion, firmware would write the valid id.
3055 mbox->numstatus = 0xFF;
3056 mbox->status = 0xFF;
3057 for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
3058 mbox->completed[i] = 0xFF;
3061 return status;
3063 blocked_mailbox:
3065 con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
3066 return -1;
3071 * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
3072 * @adapter - controller's soft state
3073 * @raw_mbox - the mailbox
3075 * Issue a scb in synchronous and non-interrupt mode for mailbox based
3076 * controllers. This is a faster version of the synchronous command and
3077 * therefore can be called in interrupt-context as well
3079 static int
3080 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
3082 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3083 mbox_t *mbox;
3084 long i;
3087 mbox = raid_dev->mbox;
3089 // return immediately if the mailbox is busy
3090 if (mbox->busy) return -1;
3092 // Copy mailbox data into host structure
3093 memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
3094 mbox->cmdid = 0xFE;
3095 mbox->busy = 1;
3096 mbox->poll = 0;
3097 mbox->ack = 0;
3098 mbox->numstatus = 0xFF;
3099 mbox->status = 0xFF;
3101 wmb();
3102 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
3104 for (i = 0; i < 0xFFFFF; i++) {
3105 if (mbox->numstatus != 0xFF) break;
3108 if (i == 0xFFFFF) {
3109 // We may need to re-calibrate the counter
3110 con_log(CL_ANN, (KERN_CRIT
3111 "megaraid: fast sync command timed out\n"));
3114 WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
3115 wmb();
3117 return mbox->status;
3122 * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
3123 * @raid_dev - RAID device (HBA) soft state
3125 * wait until the controller's mailbox is available to accept more commands.
3126 * wait for at most 1 second
3128 static int
3129 megaraid_busywait_mbox(mraid_device_t *raid_dev)
3131 mbox_t *mbox = raid_dev->mbox;
3132 int i = 0;
3134 if (mbox->busy) {
3135 udelay(25);
3136 for (i = 0; mbox->busy && i < 1000; i++)
3137 msleep(1);
3140 if (i < 1000) return 0;
3141 else return -1;
3146 * megaraid_mbox_product_info - some static information about the controller
3147 * @adapter - our soft state
3149 * issue commands to the controller to grab some parameters required by our
3150 * caller.
3152 static int
3153 megaraid_mbox_product_info(adapter_t *adapter)
3155 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3156 mbox_t *mbox;
3157 uint8_t raw_mbox[sizeof(mbox_t)];
3158 mraid_pinfo_t *pinfo;
3159 dma_addr_t pinfo_dma_h;
3160 mraid_inquiry3_t *mraid_inq3;
3161 int i;
3164 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3165 mbox = (mbox_t *)raw_mbox;
3168 * Issue an ENQUIRY3 command to find out certain adapter parameters,
3169 * e.g., max channels, max commands etc.
3171 pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3172 &pinfo_dma_h);
3174 if (pinfo == NULL) {
3175 con_log(CL_ANN, (KERN_WARNING
3176 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3177 __LINE__));
3179 return -1;
3181 memset(pinfo, 0, sizeof(mraid_pinfo_t));
3183 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3184 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3186 raw_mbox[0] = FC_NEW_CONFIG;
3187 raw_mbox[2] = NC_SUBOP_ENQUIRY3;
3188 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3190 // Issue the command
3191 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3193 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3195 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3196 pinfo, pinfo_dma_h);
3198 return -1;
3202 * Collect information about state of each physical drive
3203 * attached to the controller. We will expose all the disks
3204 * which are not part of RAID
3206 mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3207 for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3208 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3212 * Get product info for information like number of channels,
3213 * maximum commands supported.
3215 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3216 mbox->xferaddr = (uint32_t)pinfo_dma_h;
3218 raw_mbox[0] = FC_NEW_CONFIG;
3219 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3221 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3223 con_log(CL_ANN, (KERN_WARNING
3224 "megaraid: product info failed\n"));
3226 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3227 pinfo, pinfo_dma_h);
3229 return -1;
3233 * Setup some parameters for host, as required by our caller
3235 adapter->max_channel = pinfo->nchannels;
3238 * we will export all the logical drives on a single channel.
3239 * Add 1 since inquires do not come for inititor ID
3241 adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
3242 adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
3245 * These are the maximum outstanding commands for the scsi-layer
3247 adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
3249 memset(adapter->fw_version, 0, VERSION_SIZE);
3250 memset(adapter->bios_version, 0, VERSION_SIZE);
3252 memcpy(adapter->fw_version, pinfo->fw_version, 4);
3253 adapter->fw_version[4] = 0;
3255 memcpy(adapter->bios_version, pinfo->bios_version, 4);
3256 adapter->bios_version[4] = 0;
3258 con_log(CL_ANN, (KERN_NOTICE
3259 "megaraid: fw version:[%s] bios version:[%s]\n",
3260 adapter->fw_version, adapter->bios_version));
3262 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3263 pinfo_dma_h);
3265 return 0;
3271 * megaraid_mbox_extended_cdb - check for support for extended CDBs
3272 * @adapter - soft state for the controller
3274 * this routine check whether the controller in question supports extended
3275 * ( > 10 bytes ) CDBs
3277 static int
3278 megaraid_mbox_extended_cdb(adapter_t *adapter)
3280 mbox_t *mbox;
3281 uint8_t raw_mbox[sizeof(mbox_t)];
3282 int rval;
3284 mbox = (mbox_t *)raw_mbox;
3286 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3287 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3289 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3291 raw_mbox[0] = MAIN_MISC_OPCODE;
3292 raw_mbox[2] = SUPPORT_EXT_CDB;
3295 * Issue the command
3297 rval = 0;
3298 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3299 rval = -1;
3302 return rval;
3307 * megaraid_mbox_support_ha - Do we support clustering
3308 * @adapter - soft state for the controller
3309 * @init_id - ID of the initiator
3311 * Determine if the firmware supports clustering and the ID of the initiator.
3313 static int
3314 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3316 mbox_t *mbox;
3317 uint8_t raw_mbox[sizeof(mbox_t)];
3318 int rval;
3321 mbox = (mbox_t *)raw_mbox;
3323 memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3325 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3327 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3329 raw_mbox[0] = GET_TARGET_ID;
3331 // Issue the command
3332 *init_id = 7;
3333 rval = -1;
3334 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3336 *init_id = *(uint8_t *)adapter->ibuf;
3338 con_log(CL_ANN, (KERN_INFO
3339 "megaraid: cluster firmware, initiator ID: %d\n",
3340 *init_id));
3342 rval = 0;
3345 return rval;
3350 * megaraid_mbox_support_random_del - Do we support random deletion
3351 * @adapter - soft state for the controller
3353 * Determine if the firmware supports random deletion
3354 * Return: 1 is operation supported, 0 otherwise
3356 static int
3357 megaraid_mbox_support_random_del(adapter_t *adapter)
3359 mbox_t *mbox;
3360 uint8_t raw_mbox[sizeof(mbox_t)];
3361 int rval;
3364 mbox = (mbox_t *)raw_mbox;
3366 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3368 raw_mbox[0] = FC_DEL_LOGDRV;
3369 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3371 // Issue the command
3372 rval = 0;
3373 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3375 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3377 rval = 1;
3380 return rval;
3385 * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3386 * @adapter - soft state for the controller
3388 * Find out the maximum number of scatter-gather elements supported by the
3389 * firmware
3391 static int
3392 megaraid_mbox_get_max_sg(adapter_t *adapter)
3394 mbox_t *mbox;
3395 uint8_t raw_mbox[sizeof(mbox_t)];
3396 int nsg;
3399 mbox = (mbox_t *)raw_mbox;
3401 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3403 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3405 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3407 raw_mbox[0] = MAIN_MISC_OPCODE;
3408 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3410 // Issue the command
3411 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3412 nsg = *(uint8_t *)adapter->ibuf;
3414 else {
3415 nsg = MBOX_DEFAULT_SG_SIZE;
3418 if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3420 return nsg;
3425 * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3426 * @adapter - soft state for the controller
3428 * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
3429 * can be exported as regular SCSI channels
3431 static void
3432 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3434 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3435 mbox_t *mbox;
3436 uint8_t raw_mbox[sizeof(mbox_t)];
3439 mbox = (mbox_t *)raw_mbox;
3441 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3443 mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3445 memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3447 raw_mbox[0] = CHNL_CLASS;
3448 raw_mbox[2] = GET_CHNL_CLASS;
3450 // Issue the command. If the command fails, all channels are RAID
3451 // channels
3452 raid_dev->channel_class = 0xFF;
3453 if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3454 raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
3457 return;
3462 * megaraid_mbox_flush_cache - flush adapter and disks cache
3463 * @param adapter : soft state for the controller
3465 * Flush adapter cache followed by disks cache
3467 static void
3468 megaraid_mbox_flush_cache(adapter_t *adapter)
3470 mbox_t *mbox;
3471 uint8_t raw_mbox[sizeof(mbox_t)];
3474 mbox = (mbox_t *)raw_mbox;
3476 memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3478 raw_mbox[0] = FLUSH_ADAPTER;
3480 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3481 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3484 raw_mbox[0] = FLUSH_SYSTEM;
3486 if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3487 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3490 return;
3495 * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3496 * @param adapter : controllers' soft state
3497 * @param scb : SCB to be displayed
3498 * @param level : debug level for console print
3500 * Diplay information about the given SCB iff the current debug level is
3501 * verbose
3503 static void
3504 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3506 mbox_ccb_t *ccb;
3507 struct scsi_cmnd *scp;
3508 mbox_t *mbox;
3509 int level;
3510 int i;
3513 ccb = (mbox_ccb_t *)scb->ccb;
3514 scp = scb->scp;
3515 mbox = ccb->mbox;
3517 level = CL_DLEVEL3;
3519 con_log(level, (KERN_NOTICE
3520 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3521 mbox->cmd, scb->sno));
3523 con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3524 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3525 mbox->numsge));
3527 if (!scp) return;
3529 con_log(level, (KERN_NOTICE "scsi cmnd: "));
3531 for (i = 0; i < scp->cmd_len; i++) {
3532 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3535 con_log(level, ("\n"));
3537 return;
3542 * megaraid_mbox_setup_device_map - manage device ids
3543 * @adapter : Driver's soft state
3545 * Manange the device ids to have an appropraite mapping between the kernel
3546 * scsi addresses and megaraid scsi and logical drive addresses. We export
3547 * scsi devices on their actual addresses, whereas the logical drives are
3548 * exported on a virtual scsi channel.
3550 static void
3551 megaraid_mbox_setup_device_map(adapter_t *adapter)
3553 uint8_t c;
3554 uint8_t t;
3557 * First fill the values on the logical drive channel
3559 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3560 adapter->device_ids[adapter->max_channel][t] =
3561 (t < adapter->init_id) ? t : t - 1;
3563 adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3566 * Fill the values on the physical devices channels
3568 for (c = 0; c < adapter->max_channel; c++)
3569 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3570 adapter->device_ids[c][t] = (c << 8) | t;
3575 * END: internal commands library
3579 * START: Interface for the common management module
3581 * This is the module, which interfaces with the common mangement module to
3582 * provide support for ioctl and sysfs
3586 * megaraid_cmm_register - register with the mangement module
3587 * @param adapter : HBA soft state
3589 * Register with the management module, which allows applications to issue
3590 * ioctl calls to the drivers. This interface is used by the management module
3591 * to setup sysfs support as well.
3593 static int
3594 megaraid_cmm_register(adapter_t *adapter)
3596 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3597 mraid_mmadp_t adp;
3598 scb_t *scb;
3599 mbox_ccb_t *ccb;
3600 int rval;
3601 int i;
3603 // Allocate memory for the base list of scb for management module.
3604 adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
3605 GFP_KERNEL);
3607 if (adapter->uscb_list == NULL) {
3608 con_log(CL_ANN, (KERN_WARNING
3609 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3610 __LINE__));
3611 return -1;
3613 memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
3616 // Initialize the synchronization parameters for resources for
3617 // commands for management module
3618 INIT_LIST_HEAD(&adapter->uscb_pool);
3620 spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3624 // link all the packets. Note, CCB for commands, coming from the
3625 // commom management module, mailbox physical address are already
3626 // setup by it. We just need placeholder for that in our local command
3627 // control blocks
3628 for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3630 scb = adapter->uscb_list + i;
3631 ccb = raid_dev->uccb_list + i;
3633 scb->ccb = (caddr_t)ccb;
3634 ccb->mbox64 = raid_dev->umbox64 + i;
3635 ccb->mbox = &ccb->mbox64->mbox32;
3636 ccb->raw_mbox = (uint8_t *)ccb->mbox;
3638 scb->gp = 0;
3640 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3641 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3642 scb->sno = i + MBOX_MAX_SCSI_CMDS;
3644 scb->scp = NULL;
3645 scb->state = SCB_FREE;
3646 scb->dma_direction = PCI_DMA_NONE;
3647 scb->dma_type = MRAID_DMA_NONE;
3648 scb->dev_channel = -1;
3649 scb->dev_target = -1;
3651 // put scb in the free pool
3652 list_add_tail(&scb->list, &adapter->uscb_pool);
3655 adp.unique_id = adapter->unique_id;
3656 adp.drvr_type = DRVRTYPE_MBOX;
3657 adp.drvr_data = (unsigned long)adapter;
3658 adp.pdev = adapter->pdev;
3659 adp.issue_uioc = megaraid_mbox_mm_handler;
3660 adp.timeout = 300;
3661 adp.max_kioc = MBOX_MAX_USER_CMDS;
3663 if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3665 con_log(CL_ANN, (KERN_WARNING
3666 "megaraid mbox: did not register with CMM\n"));
3668 kfree(adapter->uscb_list);
3671 return rval;
3676 * megaraid_cmm_unregister - un-register with the mangement module
3677 * @param adapter : HBA soft state
3679 * Un-register with the management module.
3680 * FIXME: mgmt module must return failure for unregister if it has pending
3681 * commands in LLD
3683 static int
3684 megaraid_cmm_unregister(adapter_t *adapter)
3686 kfree(adapter->uscb_list);
3687 mraid_mm_unregister_adp(adapter->unique_id);
3688 return 0;
3693 * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3694 * @param drvr_data : LLD specific data
3695 * @param kioc : CMM interface packet
3696 * @param action : command action
3698 * This routine is invoked whenever the Common Mangement Module (CMM) has a
3699 * command for us. The 'action' parameter specifies if this is a new command
3700 * or otherwise.
3702 static int
3703 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3705 adapter_t *adapter;
3707 if (action != IOCTL_ISSUE) {
3708 con_log(CL_ANN, (KERN_WARNING
3709 "megaraid: unsupported management action:%#2x\n",
3710 action));
3711 return (-ENOTSUPP);
3714 adapter = (adapter_t *)drvr_data;
3716 // make sure this adapter is not being detached right now.
3717 if (atomic_read(&adapter->being_detached)) {
3718 con_log(CL_ANN, (KERN_WARNING
3719 "megaraid: reject management request, detaching\n"));
3720 return (-ENODEV);
3723 switch (kioc->opcode) {
3725 case GET_ADAP_INFO:
3727 kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
3728 (unsigned long)kioc->buf_vaddr);
3730 kioc->done(kioc);
3732 return kioc->status;
3734 case MBOX_CMD:
3736 return megaraid_mbox_mm_command(adapter, kioc);
3738 default:
3739 kioc->status = (-EINVAL);
3740 kioc->done(kioc);
3741 return (-EINVAL);
3744 return 0; // not reached
3748 * megaraid_mbox_mm_command - issues commands routed through CMM
3749 * @param adapter : HBA soft state
3750 * @param kioc : management command packet
3752 * Issues commands, which are routed through the management module.
3754 static int
3755 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3757 struct list_head *head = &adapter->uscb_pool;
3758 mbox64_t *mbox64;
3759 uint8_t *raw_mbox;
3760 scb_t *scb;
3761 mbox_ccb_t *ccb;
3762 unsigned long flags;
3764 // detach one scb from free pool
3765 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3767 if (list_empty(head)) { // should never happen because of CMM
3769 con_log(CL_ANN, (KERN_WARNING
3770 "megaraid mbox: bug in cmm handler, lost resources\n"));
3772 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3774 return (-EINVAL);
3777 scb = list_entry(head->next, scb_t, list);
3778 list_del_init(&scb->list);
3780 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3782 scb->state = SCB_ACTIVE;
3783 scb->dma_type = MRAID_DMA_NONE;
3784 scb->dma_direction = PCI_DMA_NONE;
3786 ccb = (mbox_ccb_t *)scb->ccb;
3787 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3788 raw_mbox = (uint8_t *)&mbox64->mbox32;
3790 memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3792 scb->gp = (unsigned long)kioc;
3795 * If it is a logdrv random delete operation, we have to wait till
3796 * there are no outstanding cmds at the fw and then issue it directly
3798 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3800 if (wait_till_fw_empty(adapter)) {
3801 con_log(CL_ANN, (KERN_NOTICE
3802 "megaraid mbox: LD delete, timed out\n"));
3804 kioc->status = -ETIME;
3806 scb->status = -1;
3808 megaraid_mbox_mm_done(adapter, scb);
3810 return (-ETIME);
3813 INIT_LIST_HEAD(&scb->list);
3815 scb->state = SCB_ISSUED;
3816 if (mbox_post_cmd(adapter, scb) != 0) {
3818 con_log(CL_ANN, (KERN_NOTICE
3819 "megaraid mbox: LD delete, mailbox busy\n"));
3821 kioc->status = -EBUSY;
3823 scb->status = -1;
3825 megaraid_mbox_mm_done(adapter, scb);
3827 return (-EBUSY);
3830 return 0;
3833 // put the command on the pending list and execute
3834 megaraid_mbox_runpendq(adapter, scb);
3836 return 0;
3840 static int
3841 wait_till_fw_empty(adapter_t *adapter)
3843 unsigned long flags = 0;
3844 int i;
3848 * Set the quiescent flag to stop issuing cmds to FW.
3850 spin_lock_irqsave(adapter->host_lock, flags);
3851 adapter->quiescent++;
3852 spin_unlock_irqrestore(adapter->host_lock, flags);
3855 * Wait till there are no more cmds outstanding at FW. Try for at most
3856 * 60 seconds
3858 for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3859 con_log(CL_DLEVEL1, (KERN_INFO
3860 "megaraid: FW has %d pending commands\n",
3861 adapter->outstanding_cmds));
3863 msleep(1000);
3866 return adapter->outstanding_cmds;
3871 * megaraid_mbox_mm_done - callback for CMM commands
3872 * @adapter : HBA soft state
3873 * @scb : completed command
3875 * Callback routine for internal commands originated from the management
3876 * module.
3878 static void
3879 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3881 uioc_t *kioc;
3882 mbox64_t *mbox64;
3883 uint8_t *raw_mbox;
3884 unsigned long flags;
3886 kioc = (uioc_t *)scb->gp;
3887 kioc->status = 0;
3888 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3889 mbox64->mbox32.status = scb->status;
3890 raw_mbox = (uint8_t *)&mbox64->mbox32;
3893 // put scb in the free pool
3894 scb->state = SCB_FREE;
3895 scb->scp = NULL;
3897 spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3899 list_add(&scb->list, &adapter->uscb_pool);
3901 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3903 // if a delete logical drive operation succeeded, restart the
3904 // controller
3905 if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3907 adapter->quiescent--;
3909 megaraid_mbox_runpendq(adapter, NULL);
3912 kioc->done(kioc);
3914 return;
3919 * gather_hbainfo - HBA characteristics for the applications
3920 * @param adapter : HBA soft state
3921 * @param hinfo : pointer to the caller's host info strucuture
3923 static int
3924 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3926 uint8_t dmajor;
3928 dmajor = megaraid_mbox_version[0];
3930 hinfo->pci_vendor_id = adapter->pdev->vendor;
3931 hinfo->pci_device_id = adapter->pdev->device;
3932 hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3933 hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3935 hinfo->pci_bus = adapter->pdev->bus->number;
3936 hinfo->pci_dev_fn = adapter->pdev->devfn;
3937 hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
3938 hinfo->irq = adapter->host->irq;
3939 hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
3941 hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3942 hinfo->host_no = adapter->host->host_no;
3944 return 0;
3948 * END: Interface for the common management module
3954 * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3956 * Allocate packets required to issue FW calls whenever the sysfs attributes
3957 * are read. These attributes would require up-to-date information from the
3958 * FW. Also set up resources for mutual exclusion to share these resources and
3959 * the wait queue.
3961 * @param adapter : controller's soft state
3963 * @return 0 on success
3964 * @return -ERROR_CODE on failure
3966 static int
3967 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3969 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
3970 int rval = 0;
3972 raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3974 raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3976 raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3977 PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3979 if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3980 !raid_dev->sysfs_buffer) {
3982 con_log(CL_ANN, (KERN_WARNING
3983 "megaraid: out of memory, %s %d\n", __FUNCTION__,
3984 __LINE__));
3986 rval = -ENOMEM;
3988 megaraid_sysfs_free_resources(adapter);
3991 sema_init(&raid_dev->sysfs_sem, 1);
3993 init_waitqueue_head(&raid_dev->sysfs_wait_q);
3995 return rval;
4000 * megaraid_sysfs_free_resources - free sysfs related resources
4002 * Free packets allocated for sysfs FW commands
4004 * @param adapter : controller's soft state
4006 static void
4007 megaraid_sysfs_free_resources(adapter_t *adapter)
4009 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4011 if (raid_dev->sysfs_uioc) kfree(raid_dev->sysfs_uioc);
4013 if (raid_dev->sysfs_mbox64) kfree(raid_dev->sysfs_mbox64);
4015 if (raid_dev->sysfs_buffer) {
4016 pci_free_consistent(adapter->pdev, PAGE_SIZE,
4017 raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
4023 * megaraid_sysfs_get_ldmap_done - callback for get ldmap
4025 * Callback routine called in the ISR/tasklet context for get ldmap call
4027 * @param uioc : completed packet
4029 static void
4030 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
4032 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
4033 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4035 uioc->status = 0;
4037 wake_up(&raid_dev->sysfs_wait_q);
4042 * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
4044 * Timeout routine to recover and return to application, in case the adapter
4045 * has stopped responding. A timeout of 60 seconds for this command seem like
4046 * a good value
4048 * @param uioc : timed out packet
4050 static void
4051 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
4053 uioc_t *uioc = (uioc_t *)data;
4054 adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
4055 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4057 uioc->status = -ETIME;
4059 wake_up(&raid_dev->sysfs_wait_q);
4064 * megaraid_sysfs_get_ldmap - get update logical drive map
4066 * This routine will be called whenever user reads the logical drive
4067 * attributes, go get the current logical drive mapping table from the
4068 * firmware. We use the managment API's to issue commands to the controller.
4070 * NOTE: The commands issuance functionality is not generalized and
4071 * implemented in context of "get ld map" command only. If required, the
4072 * command issuance logical can be trivially pulled out and implemented as a
4073 * standalone libary. For now, this should suffice since there is no other
4074 * user of this interface.
4076 * @param adapter : controller's soft state
4078 * @return 0 on success
4079 * @return -1 on failure
4081 static int
4082 megaraid_sysfs_get_ldmap(adapter_t *adapter)
4084 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4085 uioc_t *uioc;
4086 mbox64_t *mbox64;
4087 mbox_t *mbox;
4088 char *raw_mbox;
4089 struct timer_list sysfs_timer;
4090 struct timer_list *timerp;
4091 caddr_t ldmap;
4092 int rval = 0;
4095 * Allow only one read at a time to go through the sysfs attributes
4097 down(&raid_dev->sysfs_sem);
4099 uioc = raid_dev->sysfs_uioc;
4100 mbox64 = raid_dev->sysfs_mbox64;
4101 ldmap = raid_dev->sysfs_buffer;
4103 memset(uioc, 0, sizeof(uioc_t));
4104 memset(mbox64, 0, sizeof(mbox64_t));
4105 memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
4107 mbox = &mbox64->mbox32;
4108 raw_mbox = (char *)mbox;
4109 uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
4110 uioc->buf_vaddr = (caddr_t)adapter;
4111 uioc->status = -ENODATA;
4112 uioc->done = megaraid_sysfs_get_ldmap_done;
4115 * Prepare the mailbox packet to get the current logical drive mapping
4116 * table
4118 mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
4120 raw_mbox[0] = FC_DEL_LOGDRV;
4121 raw_mbox[2] = OP_GET_LDID_MAP;
4124 * Setup a timer to recover from a non-responding controller
4126 timerp = &sysfs_timer;
4127 init_timer(timerp);
4129 timerp->function = megaraid_sysfs_get_ldmap_timeout;
4130 timerp->data = (unsigned long)uioc;
4131 timerp->expires = jiffies + 60 * HZ;
4133 add_timer(timerp);
4136 * Send the command to the firmware
4138 rval = megaraid_mbox_mm_command(adapter, uioc);
4140 if (rval == 0) { // command successfully issued
4141 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
4144 * Check if the command timed out
4146 if (uioc->status == -ETIME) {
4147 con_log(CL_ANN, (KERN_NOTICE
4148 "megaraid: sysfs get ld map timed out\n"));
4150 rval = -ETIME;
4152 else {
4153 rval = mbox->status;
4156 if (rval == 0) {
4157 memcpy(raid_dev->curr_ldmap, ldmap,
4158 sizeof(raid_dev->curr_ldmap));
4160 else {
4161 con_log(CL_ANN, (KERN_NOTICE
4162 "megaraid: get ld map failed with %x\n", rval));
4165 else {
4166 con_log(CL_ANN, (KERN_NOTICE
4167 "megaraid: could not issue ldmap command:%x\n", rval));
4171 del_timer_sync(timerp);
4173 up(&raid_dev->sysfs_sem);
4175 return rval;
4180 * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4182 * Display the handle used by the applications while executing management
4183 * tasks on the adapter. We invoke a management module API to get the adapter
4184 * handle, since we do not interface with applications directly.
4186 * @param cdev : class device object representation for the host
4187 * @param buf : buffer to send data to
4189 static ssize_t
4190 megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
4192 struct Scsi_Host *shost = class_to_shost(cdev);
4193 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4194 uint32_t app_hndl;
4196 app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4198 return snprintf(buf, 8, "%u\n", app_hndl);
4203 * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4205 * Display the logical drive number for the device in question, if it a valid
4206 * logical drive. For physical devices, "-1" is returned
4207 * The logical drive number is displayed in following format
4209 * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4210 * <int> <int> <int> <int>
4212 * @param dev : device object representation for the scsi device
4213 * @param buf : buffer to send data to
4215 static ssize_t
4216 megaraid_sysfs_show_ldnum(struct device *dev, char *buf)
4218 struct scsi_device *sdev = to_scsi_device(dev);
4219 adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4220 mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
4221 int scsi_id = -1;
4222 int logical_drv = -1;
4223 int ldid_map = -1;
4224 uint32_t app_hndl = 0;
4225 int mapped_sdev_id;
4226 int rval;
4227 int i;
4229 if (raid_dev->random_del_supported &&
4230 MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4232 rval = megaraid_sysfs_get_ldmap(adapter);
4233 if (rval == 0) {
4235 for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4237 mapped_sdev_id = sdev->id;
4239 if (sdev->id > adapter->init_id) {
4240 mapped_sdev_id -= 1;
4243 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4245 scsi_id = sdev->id;
4247 logical_drv = i;
4249 ldid_map = raid_dev->curr_ldmap[i];
4251 app_hndl = mraid_mm_adapter_app_handle(
4252 adapter->unique_id);
4254 break;
4258 else {
4259 con_log(CL_ANN, (KERN_NOTICE
4260 "megaraid: sysfs get ld map failed: %x\n",
4261 rval));
4265 return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4266 ldid_map, app_hndl);
4271 * END: Mailbox Low Level Driver
4273 module_init(megaraid_init);
4274 module_exit(megaraid_exit);
4276 /* vim: set ts=8 sw=8 tw=78 ai si: */