]> err.no Git - linux-2.6/blob - drivers/scsi/megaraid/megaraid_mbox.c
[SCSI] megaraid: Add support for change_queue_depth
[linux-2.6] / drivers / scsi / megaraid / megaraid_mbox.c
1 /*
2  *
3  *                      Linux MegaRAID device driver
4  *
5  * Copyright (c) 2003-2004  LSI Logic Corporation.
6  *
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.
11  *
12  * FILE         : megaraid_mbox.c
13  * Version      : v2.20.4.8 (Apr 11 2006)
14  *
15  * Authors:
16  *      Atul Mukker             <Atul.Mukker@lsil.com>
17  *      Sreenivas Bagalkote     <Sreenivas.Bagalkote@lsil.com>
18  *      Manoj Jose              <Manoj.Jose@lsil.com>
19  *      Seokmann Ju             <Seokmann.Ju@lsil.com>
20  *
21  * List of supported controllers
22  *
23  * OEM  Product Name                    VID     DID     SSVID   SSID
24  * ---  ------------                    ---     ---     ----    ----
25  * Dell PERC3/QC                        101E    1960    1028    0471
26  * Dell PERC3/DC                        101E    1960    1028    0493
27  * Dell PERC3/SC                        101E    1960    1028    0475
28  * Dell PERC3/Di                        1028    1960    1028    0123
29  * Dell PERC4/SC                        1000    1960    1028    0520
30  * Dell PERC4/DC                        1000    1960    1028    0518
31  * Dell PERC4/QC                        1000    0407    1028    0531
32  * Dell PERC4/Di                        1028    000F    1028    014A
33  * Dell PERC 4e/Si                      1028    0013    1028    016c
34  * Dell PERC 4e/Di                      1028    0013    1028    016d
35  * Dell PERC 4e/Di                      1028    0013    1028    016e
36  * Dell PERC 4e/Di                      1028    0013    1028    016f
37  * Dell PERC 4e/Di                      1028    0013    1028    0170
38  * Dell PERC 4e/DC                      1000    0408    1028    0002
39  * Dell PERC 4e/SC                      1000    0408    1028    0001
40  *
41  *
42  * LSI MegaRAID SCSI 320-0              1000    1960    1000    A520
43  * LSI MegaRAID SCSI 320-1              1000    1960    1000    0520
44  * LSI MegaRAID SCSI 320-2              1000    1960    1000    0518
45  * LSI MegaRAID SCSI 320-0X             1000    0407    1000    0530
46  * LSI MegaRAID SCSI 320-2X             1000    0407    1000    0532
47  * LSI MegaRAID SCSI 320-4X             1000    0407    1000    0531
48  * LSI MegaRAID SCSI 320-1E             1000    0408    1000    0001
49  * LSI MegaRAID SCSI 320-2E             1000    0408    1000    0002
50  * LSI MegaRAID SATA 150-4              1000    1960    1000    4523
51  * LSI MegaRAID SATA 150-6              1000    1960    1000    0523
52  * LSI MegaRAID SATA 300-4X             1000    0409    1000    3004
53  * LSI MegaRAID SATA 300-8X             1000    0409    1000    3008
54  *
55  * INTEL RAID Controller SRCU42X        1000    0407    8086    0532
56  * INTEL RAID Controller SRCS16         1000    1960    8086    0523
57  * INTEL RAID Controller SRCU42E        1000    0408    8086    0002
58  * INTEL RAID Controller SRCZCRX        1000    0407    8086    0530
59  * INTEL RAID Controller SRCS28X        1000    0409    8086    3008
60  * INTEL RAID Controller SROMBU42E      1000    0408    8086    3431
61  * INTEL RAID Controller SROMBU42E      1000    0408    8086    3499
62  * INTEL RAID Controller SRCU51L        1000    1960    8086    0520
63  *
64  * FSC  MegaRAID PCI Express ROMB       1000    0408    1734    1065
65  *
66  * ACER MegaRAID ROMB-2E                1000    0408    1025    004D
67  *
68  * NEC  MegaRAID PCI Express ROMB       1000    0408    1033    8287
69  *
70  * For history of changes, see Documentation/ChangeLog.megaraid
71  */
72
73 #include "megaraid_mbox.h"
74
75 static int megaraid_init(void);
76 static void megaraid_exit(void);
77
78 static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
79 static void megaraid_detach_one(struct pci_dev *);
80 static void megaraid_mbox_shutdown(struct pci_dev *);
81
82 static int megaraid_io_attach(adapter_t *);
83 static void megaraid_io_detach(adapter_t *);
84
85 static int megaraid_init_mbox(adapter_t *);
86 static void megaraid_fini_mbox(adapter_t *);
87
88 static int megaraid_alloc_cmd_packets(adapter_t *);
89 static void megaraid_free_cmd_packets(adapter_t *);
90
91 static int megaraid_mbox_setup_dma_pools(adapter_t *);
92 static void megaraid_mbox_teardown_dma_pools(adapter_t *);
93
94 static int megaraid_sysfs_alloc_resources(adapter_t *);
95 static void megaraid_sysfs_free_resources(adapter_t *);
96
97 static int megaraid_abort_handler(struct scsi_cmnd *);
98 static int megaraid_reset_handler(struct scsi_cmnd *);
99
100 static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
101 static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
102 static int megaraid_busywait_mbox(mraid_device_t *);
103 static int megaraid_mbox_product_info(adapter_t *);
104 static int megaraid_mbox_extended_cdb(adapter_t *);
105 static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
106 static int megaraid_mbox_support_random_del(adapter_t *);
107 static int megaraid_mbox_get_max_sg(adapter_t *);
108 static void megaraid_mbox_enum_raid_scsi(adapter_t *);
109 static void megaraid_mbox_flush_cache(adapter_t *);
110
111 static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
112 static void megaraid_mbox_setup_device_map(adapter_t *);
113
114 static int megaraid_queue_command(struct scsi_cmnd *,
115                 void (*)(struct scsi_cmnd *));
116 static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
117 static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
118 static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
119                 struct scsi_cmnd *);
120 static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
121                 struct scsi_cmnd *);
122
123 static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
124
125 static void megaraid_mbox_dpc(unsigned long);
126
127 static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
128 static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
129
130 static int megaraid_cmm_register(adapter_t *);
131 static int megaraid_cmm_unregister(adapter_t *);
132 static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
133 static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
134 static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
135 static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
136 static int wait_till_fw_empty(adapter_t *);
137
138
139
140 MODULE_AUTHOR("sju@lsil.com");
141 MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
142 MODULE_LICENSE("GPL");
143 MODULE_VERSION(MEGARAID_VERSION);
144
145 /*
146  * ### modules parameters for driver ###
147  */
148
149 /**
150  * Set to enable driver to expose unconfigured disk to kernel
151  */
152 static int megaraid_expose_unconf_disks = 0;
153 module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
154 MODULE_PARM_DESC(unconf_disks,
155         "Set to expose unconfigured disks to kernel (default=0)");
156
157 /**
158  * driver wait time if the adapter's mailbox is busy
159  */
160 static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
161 module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
162 MODULE_PARM_DESC(busy_wait,
163         "Max wait for mailbox in microseconds if busy (default=10)");
164
165 /**
166  * number of sectors per IO command
167  */
168 static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
169 module_param_named(max_sectors, megaraid_max_sectors, int, 0);
170 MODULE_PARM_DESC(max_sectors,
171         "Maximum number of sectors per IO command (default=128)");
172
173 /**
174  * number of commands per logical unit
175  */
176 static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
177 module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
178 MODULE_PARM_DESC(cmd_per_lun,
179         "Maximum number of commands per logical unit (default=64)");
180
181
182 /**
183  * Fast driver load option, skip scanning for physical devices during load.
184  * This would result in non-disk devices being skipped during driver load
185  * time. These can be later added though, using /proc/scsi/scsi
186  */
187 static unsigned int megaraid_fast_load = 0;
188 module_param_named(fast_load, megaraid_fast_load, int, 0);
189 MODULE_PARM_DESC(fast_load,
190         "Faster loading of the driver, skips physical devices! (default=0)");
191
192
193 /**
194  * mraid_debug level - threshold for amount of information to be displayed by
195  * the driver. This level can be changed through modules parameters, ioctl or
196  * sysfs/proc interface. By default, print the announcement messages only.
197  */
198 int mraid_debug_level = CL_ANN;
199 module_param_named(debug_level, mraid_debug_level, int, 0);
200 MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
201
202 /*
203  * ### global data ###
204  */
205 static uint8_t megaraid_mbox_version[8] =
206         { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
207
208
209 /*
210  * PCI table for all supported controllers.
211  */
212 static struct pci_device_id pci_id_table_g[] =  {
213         {
214                 PCI_VENDOR_ID_DELL,
215                 PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
216                 PCI_VENDOR_ID_DELL,
217                 PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
218         },
219         {
220                 PCI_VENDOR_ID_LSI_LOGIC,
221                 PCI_DEVICE_ID_PERC4_SC,
222                 PCI_VENDOR_ID_DELL,
223                 PCI_SUBSYS_ID_PERC4_SC,
224         },
225         {
226                 PCI_VENDOR_ID_LSI_LOGIC,
227                 PCI_DEVICE_ID_PERC4_DC,
228                 PCI_VENDOR_ID_DELL,
229                 PCI_SUBSYS_ID_PERC4_DC,
230         },
231         {
232                 PCI_VENDOR_ID_LSI_LOGIC,
233                 PCI_DEVICE_ID_VERDE,
234                 PCI_ANY_ID,
235                 PCI_ANY_ID,
236         },
237         {
238                 PCI_VENDOR_ID_DELL,
239                 PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
240                 PCI_VENDOR_ID_DELL,
241                 PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
242         },
243         {
244                 PCI_VENDOR_ID_DELL,
245                 PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
246                 PCI_VENDOR_ID_DELL,
247                 PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
248         },
249         {
250                 PCI_VENDOR_ID_DELL,
251                 PCI_DEVICE_ID_PERC4E_DI_KOBUK,
252                 PCI_VENDOR_ID_DELL,
253                 PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
254         },
255         {
256                 PCI_VENDOR_ID_DELL,
257                 PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
258                 PCI_VENDOR_ID_DELL,
259                 PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
260         },
261         {
262                 PCI_VENDOR_ID_DELL,
263                 PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
264                 PCI_VENDOR_ID_DELL,
265                 PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
266         },
267         {
268                 PCI_VENDOR_ID_DELL,
269                 PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
270                 PCI_VENDOR_ID_DELL,
271                 PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
272         },
273         {
274                 PCI_VENDOR_ID_LSI_LOGIC,
275                 PCI_DEVICE_ID_DOBSON,
276                 PCI_ANY_ID,
277                 PCI_ANY_ID,
278         },
279         {
280                 PCI_VENDOR_ID_AMI,
281                 PCI_DEVICE_ID_AMI_MEGARAID3,
282                 PCI_ANY_ID,
283                 PCI_ANY_ID,
284         },
285         {
286                 PCI_VENDOR_ID_LSI_LOGIC,
287                 PCI_DEVICE_ID_AMI_MEGARAID3,
288                 PCI_ANY_ID,
289                 PCI_ANY_ID,
290         },
291         {
292                 PCI_VENDOR_ID_LSI_LOGIC,
293                 PCI_DEVICE_ID_LINDSAY,
294                 PCI_ANY_ID,
295                 PCI_ANY_ID,
296         },
297         {0}     /* Terminating entry */
298 };
299 MODULE_DEVICE_TABLE(pci, pci_id_table_g);
300
301
302 static struct pci_driver megaraid_pci_driver_g = {
303         .name           = "megaraid",
304         .id_table       = pci_id_table_g,
305         .probe          = megaraid_probe_one,
306         .remove         = __devexit_p(megaraid_detach_one),
307         .shutdown       = megaraid_mbox_shutdown,
308 };
309
310
311
312 // definitions for the device attributes for exporting logical drive number
313 // for a scsi address (Host, Channel, Id, Lun)
314
315 CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
316                 NULL);
317
318 // Host template initializer for megaraid mbox sysfs device attributes
319 static struct class_device_attribute *megaraid_shost_attrs[] = {
320         &class_device_attr_megaraid_mbox_app_hndl,
321         NULL,
322 };
323
324
325 DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
326
327 // Host template initializer for megaraid mbox sysfs device attributes
328 static struct device_attribute *megaraid_sdev_attrs[] = {
329         &dev_attr_megaraid_mbox_ld,
330         NULL,
331 };
332
333 /**
334  * megaraid_change_queue_depth - Change the device's queue depth
335  * @sdev:       scsi device struct
336  * @qdepth:     depth to set
337  *
338  * Return value:
339  *      actual depth set
340  **/
341 static int megaraid_change_queue_depth(struct scsi_device *sdev, int qdepth)
342 {
343         if (qdepth > MBOX_MAX_SCSI_CMDS)
344                 qdepth = MBOX_MAX_SCSI_CMDS;
345         scsi_adjust_queue_depth(sdev, 0, qdepth);
346         return sdev->queue_depth;
347 }
348
349 /*
350  * Scsi host template for megaraid unified driver
351  */
352 static struct scsi_host_template megaraid_template_g = {
353         .module                         = THIS_MODULE,
354         .name                           = "LSI Logic MegaRAID driver",
355         .proc_name                      = "megaraid",
356         .queuecommand                   = megaraid_queue_command,
357         .eh_abort_handler               = megaraid_abort_handler,
358         .eh_device_reset_handler        = megaraid_reset_handler,
359         .eh_bus_reset_handler           = megaraid_reset_handler,
360         .eh_host_reset_handler          = megaraid_reset_handler,
361         .change_queue_depth             = megaraid_change_queue_depth,
362         .use_clustering                 = ENABLE_CLUSTERING,
363         .sdev_attrs                     = megaraid_sdev_attrs,
364         .shost_attrs                    = megaraid_shost_attrs,
365 };
366
367
368 /**
369  * megaraid_init - module load hook
370  *
371  * We register ourselves as hotplug enabled module and let PCI subsystem
372  * discover our adaters
373  **/
374 static int __init
375 megaraid_init(void)
376 {
377         int     rval;
378
379         // Announce the driver version
380         con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
381                 MEGARAID_EXT_VERSION));
382
383         // check validity of module parameters
384         if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
385
386                 con_log(CL_ANN, (KERN_WARNING
387                         "megaraid mailbox: max commands per lun reset to %d\n",
388                         MBOX_MAX_SCSI_CMDS));
389
390                 megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
391         }
392
393
394         // register as a PCI hot-plug driver module
395         rval = pci_register_driver(&megaraid_pci_driver_g);
396         if (rval < 0) {
397                 con_log(CL_ANN, (KERN_WARNING
398                         "megaraid: could not register hotplug support.\n"));
399         }
400
401         return rval;
402 }
403
404
405 /**
406  * megaraid_exit - driver unload entry point
407  *
408  * We simply unwrap the megaraid_init routine here
409  */
410 static void __exit
411 megaraid_exit(void)
412 {
413         con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
414
415         // unregister as PCI hotplug driver
416         pci_unregister_driver(&megaraid_pci_driver_g);
417
418         return;
419 }
420
421
422 /**
423  * megaraid_probe_one - PCI hotplug entry point
424  * @param pdev  : handle to this controller's PCI configuration space
425  * @param id    : pci device id of the class of controllers
426  *
427  * This routine should be called whenever a new adapter is detected by the
428  * PCI hotplug susbsytem.
429  **/
430 static int __devinit
431 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
432 {
433         adapter_t       *adapter;
434
435
436         // detected a new controller
437         con_log(CL_ANN, (KERN_INFO
438                 "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
439                 pdev->vendor, pdev->device, pdev->subsystem_vendor,
440                 pdev->subsystem_device));
441
442         con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
443                 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
444
445         if (pci_enable_device(pdev)) {
446                 con_log(CL_ANN, (KERN_WARNING
447                                 "megaraid: pci_enable_device failed\n"));
448
449                 return -ENODEV;
450         }
451
452         // Enable bus-mastering on this controller
453         pci_set_master(pdev);
454
455         // Allocate the per driver initialization structure
456         adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
457
458         if (adapter == NULL) {
459                 con_log(CL_ANN, (KERN_WARNING
460                 "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
461
462                 goto out_probe_one;
463         }
464         memset(adapter, 0, sizeof(adapter_t));
465
466
467         // set up PCI related soft state and other pre-known parameters
468         adapter->unique_id      = pdev->bus->number << 8 | pdev->devfn;
469         adapter->irq            = pdev->irq;
470         adapter->pdev           = pdev;
471
472         atomic_set(&adapter->being_detached, 0);
473
474         // Setup the default DMA mask. This would be changed later on
475         // depending on hardware capabilities
476         if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
477
478                 con_log(CL_ANN, (KERN_WARNING
479                         "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
480
481                 goto out_free_adapter;
482         }
483
484
485         // Initialize the synchronization lock for kernel and LLD
486         spin_lock_init(&adapter->lock);
487
488         // Initialize the command queues: the list of free SCBs and the list
489         // of pending SCBs.
490         INIT_LIST_HEAD(&adapter->kscb_pool);
491         spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
492
493         INIT_LIST_HEAD(&adapter->pend_list);
494         spin_lock_init(PENDING_LIST_LOCK(adapter));
495
496         INIT_LIST_HEAD(&adapter->completed_list);
497         spin_lock_init(COMPLETED_LIST_LOCK(adapter));
498
499
500         // Start the mailbox based controller
501         if (megaraid_init_mbox(adapter) != 0) {
502                 con_log(CL_ANN, (KERN_WARNING
503                         "megaraid: maibox adapter did not initialize\n"));
504
505                 goto out_free_adapter;
506         }
507
508         // Register with LSI Common Management Module
509         if (megaraid_cmm_register(adapter) != 0) {
510
511                 con_log(CL_ANN, (KERN_WARNING
512                 "megaraid: could not register with management module\n"));
513
514                 goto out_fini_mbox;
515         }
516
517         // setup adapter handle in PCI soft state
518         pci_set_drvdata(pdev, adapter);
519
520         // attach with scsi mid-layer
521         if (megaraid_io_attach(adapter) != 0) {
522
523                 con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
524
525                 goto out_cmm_unreg;
526         }
527
528         return 0;
529
530 out_cmm_unreg:
531         pci_set_drvdata(pdev, NULL);
532         megaraid_cmm_unregister(adapter);
533 out_fini_mbox:
534         megaraid_fini_mbox(adapter);
535 out_free_adapter:
536         kfree(adapter);
537 out_probe_one:
538         pci_disable_device(pdev);
539
540         return -ENODEV;
541 }
542
543
544 /**
545  * megaraid_detach_one - release the framework resources and call LLD release
546  * routine
547  * @param pdev  : handle for our PCI cofiguration space
548  *
549  * This routine is called during driver unload. We free all the allocated
550  * resources and call the corresponding LLD so that it can also release all
551  * its resources.
552  *
553  * This routine is also called from the PCI hotplug system
554  **/
555 static void
556 megaraid_detach_one(struct pci_dev *pdev)
557 {
558         adapter_t               *adapter;
559         struct Scsi_Host        *host;
560
561
562         // Start a rollback on this adapter
563         adapter = pci_get_drvdata(pdev);
564
565         if (!adapter) {
566                 con_log(CL_ANN, (KERN_CRIT
567                 "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
568                         pdev->vendor, pdev->device, pdev->subsystem_vendor,
569                         pdev->subsystem_device));
570
571                 return;
572         }
573         else {
574                 con_log(CL_ANN, (KERN_NOTICE
575                 "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
576                         pdev->vendor, pdev->device, pdev->subsystem_vendor,
577                         pdev->subsystem_device));
578         }
579
580
581         host = adapter->host;
582
583         // do not allow any more requests from the management module for this
584         // adapter.
585         // FIXME: How do we account for the request which might still be
586         // pending with us?
587         atomic_set(&adapter->being_detached, 1);
588
589         // detach from the IO sub-system
590         megaraid_io_detach(adapter);
591
592         // reset the device state in the PCI structure. We check this
593         // condition when we enter here. If the device state is NULL,
594         // that would mean the device has already been removed
595         pci_set_drvdata(pdev, NULL);
596
597         // Unregister from common management module
598         //
599         // FIXME: this must return success or failure for conditions if there
600         // is a command pending with LLD or not.
601         megaraid_cmm_unregister(adapter);
602
603         // finalize the mailbox based controller and release all resources
604         megaraid_fini_mbox(adapter);
605
606         kfree(adapter);
607
608         scsi_host_put(host);
609
610         pci_disable_device(pdev);
611
612         return;
613 }
614
615
616 /**
617  * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
618  * @param device        : generice driver model device
619  *
620  * Shutdown notification, perform flush cache
621  */
622 static void
623 megaraid_mbox_shutdown(struct pci_dev *pdev)
624 {
625         adapter_t               *adapter = pci_get_drvdata(pdev);
626         static int              counter;
627
628         if (!adapter) {
629                 con_log(CL_ANN, (KERN_WARNING
630                         "megaraid: null device in shutdown\n"));
631                 return;
632         }
633
634         // flush caches now
635         con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
636                 counter++));
637
638         megaraid_mbox_flush_cache(adapter);
639
640         con_log(CL_ANN, ("done\n"));
641 }
642
643
644 /**
645  * megaraid_io_attach - attach a device with the IO subsystem
646  * @param adapter       : controller's soft state
647  *
648  * Attach this device with the IO subsystem
649  **/
650 static int
651 megaraid_io_attach(adapter_t *adapter)
652 {
653         struct Scsi_Host        *host;
654
655         // Initialize SCSI Host structure
656         host = scsi_host_alloc(&megaraid_template_g, 8);
657         if (!host) {
658                 con_log(CL_ANN, (KERN_WARNING
659                         "megaraid mbox: scsi_register failed\n"));
660
661                 return -1;
662         }
663
664         SCSIHOST2ADAP(host)     = (caddr_t)adapter;
665         adapter->host           = host;
666
667         host->irq               = adapter->irq;
668         host->unique_id         = adapter->unique_id;
669         host->can_queue         = adapter->max_cmds;
670         host->this_id           = adapter->init_id;
671         host->sg_tablesize      = adapter->sglen;
672         host->max_sectors       = adapter->max_sectors;
673         host->cmd_per_lun       = adapter->cmd_per_lun;
674         host->max_channel       = adapter->max_channel;
675         host->max_id            = adapter->max_target;
676         host->max_lun           = adapter->max_lun;
677
678
679         // notify mid-layer about the new controller
680         if (scsi_add_host(host, &adapter->pdev->dev)) {
681
682                 con_log(CL_ANN, (KERN_WARNING
683                         "megaraid mbox: scsi_add_host failed\n"));
684
685                 scsi_host_put(host);
686
687                 return -1;
688         }
689
690         scsi_scan_host(host);
691
692         return 0;
693 }
694
695
696 /**
697  * megaraid_io_detach - detach a device from the IO subsystem
698  * @param adapter       : controller's soft state
699  *
700  * Detach this device from the IO subsystem
701  **/
702 static void
703 megaraid_io_detach(adapter_t *adapter)
704 {
705         struct Scsi_Host        *host;
706
707         con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
708
709         host = adapter->host;
710
711         scsi_remove_host(host);
712
713         return;
714 }
715
716
717 /*
718  * START: Mailbox Low Level Driver
719  *
720  * This is section specific to the single mailbox based controllers
721  */
722
723 /**
724  * megaraid_init_mbox - initialize controller
725  * @param adapter       - our soft state
726  *
727  * . Allocate 16-byte aligned mailbox memory for firmware handshake
728  * . Allocate controller's memory resources
729  * . Find out all initialization data
730  * . Allocate memory required for all the commands
731  * . Use internal library of FW routines, build up complete soft state
732  */
733 static int __devinit
734 megaraid_init_mbox(adapter_t *adapter)
735 {
736         struct pci_dev          *pdev;
737         mraid_device_t          *raid_dev;
738         int                     i;
739
740
741         adapter->ito    = MBOX_TIMEOUT;
742         pdev            = adapter->pdev;
743
744         /*
745          * Allocate and initialize the init data structure for mailbox
746          * controllers
747          */
748         raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
749         if (raid_dev == NULL) return -1;
750
751         memset(raid_dev, 0, sizeof(mraid_device_t));
752
753         /*
754          * Attach the adapter soft state to raid device soft state
755          */
756         adapter->raid_device    = (caddr_t)raid_dev;
757         raid_dev->fast_load     = megaraid_fast_load;
758
759
760         // our baseport
761         raid_dev->baseport = pci_resource_start(pdev, 0);
762
763         if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
764
765                 con_log(CL_ANN, (KERN_WARNING
766                                 "megaraid: mem region busy\n"));
767
768                 goto out_free_raid_dev;
769         }
770
771         raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
772
773         if (!raid_dev->baseaddr) {
774
775                 con_log(CL_ANN, (KERN_WARNING
776                         "megaraid: could not map hba memory\n") );
777
778                 goto out_release_regions;
779         }
780
781         //
782         // Setup the rest of the soft state using the library of FW routines
783         //
784
785         // request IRQ and register the interrupt service routine
786         if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
787                 adapter)) {
788
789                 con_log(CL_ANN, (KERN_WARNING
790                         "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
791
792                 goto out_iounmap;
793         }
794
795
796         // initialize the mutual exclusion lock for the mailbox
797         spin_lock_init(&raid_dev->mailbox_lock);
798
799         // allocate memory required for commands
800         if (megaraid_alloc_cmd_packets(adapter) != 0) {
801                 goto out_free_irq;
802         }
803
804         // Product info
805         if (megaraid_mbox_product_info(adapter) != 0) {
806                 goto out_alloc_cmds;
807         }
808
809         // Do we support extended CDBs
810         adapter->max_cdb_sz = 10;
811         if (megaraid_mbox_extended_cdb(adapter) == 0) {
812                 adapter->max_cdb_sz = 16;
813         }
814
815         /*
816          * Do we support cluster environment, if we do, what is the initiator
817          * id.
818          * NOTE: In a non-cluster aware firmware environment, the LLD should
819          * return 7 as initiator id.
820          */
821         adapter->ha             = 0;
822         adapter->init_id        = -1;
823         if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
824                 adapter->ha = 1;
825         }
826
827         /*
828          * Prepare the device ids array to have the mapping between the kernel
829          * device address and megaraid device address.
830          * We export the physical devices on their actual addresses. The
831          * logical drives are exported on a virtual SCSI channel
832          */
833         megaraid_mbox_setup_device_map(adapter);
834
835         // If the firmware supports random deletion, update the device id map
836         if (megaraid_mbox_support_random_del(adapter)) {
837
838                 // Change the logical drives numbers in device_ids array one
839                 // slot in device_ids is reserved for target id, that's why
840                 // "<=" below
841                 for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
842                         adapter->device_ids[adapter->max_channel][i] += 0x80;
843                 }
844                 adapter->device_ids[adapter->max_channel][adapter->init_id] =
845                         0xFF;
846
847                 raid_dev->random_del_supported = 1;
848         }
849
850         /*
851          * find out the maximum number of scatter-gather elements supported by
852          * this firmware
853          */
854         adapter->sglen = megaraid_mbox_get_max_sg(adapter);
855
856         // enumerate RAID and SCSI channels so that all devices on SCSI
857         // channels can later be exported, including disk devices
858         megaraid_mbox_enum_raid_scsi(adapter);
859
860         /*
861          * Other parameters required by upper layer
862          *
863          * maximum number of sectors per IO command
864          */
865         adapter->max_sectors = megaraid_max_sectors;
866
867         /*
868          * number of queued commands per LUN.
869          */
870         adapter->cmd_per_lun = megaraid_cmd_per_lun;
871
872         /*
873          * Allocate resources required to issue FW calls, when sysfs is
874          * accessed
875          */
876         if (megaraid_sysfs_alloc_resources(adapter) != 0) {
877                 goto out_alloc_cmds;
878         }
879
880         // Set the DMA mask to 64-bit. All supported controllers as capable of
881         // DMA in this range
882         if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
883
884                 con_log(CL_ANN, (KERN_WARNING
885                         "megaraid: could not set DMA mask for 64-bit.\n"));
886
887                 goto out_free_sysfs_res;
888         }
889
890         // setup tasklet for DPC
891         tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
892                         (unsigned long)adapter);
893
894         con_log(CL_DLEVEL1, (KERN_INFO
895                 "megaraid mbox hba successfully initialized\n"));
896
897         return 0;
898
899 out_free_sysfs_res:
900         megaraid_sysfs_free_resources(adapter);
901 out_alloc_cmds:
902         megaraid_free_cmd_packets(adapter);
903 out_free_irq:
904         free_irq(adapter->irq, adapter);
905 out_iounmap:
906         iounmap(raid_dev->baseaddr);
907 out_release_regions:
908         pci_release_regions(pdev);
909 out_free_raid_dev:
910         kfree(raid_dev);
911
912         return -1;
913 }
914
915
916 /**
917  * megaraid_fini_mbox - undo controller initialization
918  * @param adapter       : our soft state
919  */
920 static void
921 megaraid_fini_mbox(adapter_t *adapter)
922 {
923         mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
924
925         // flush all caches
926         megaraid_mbox_flush_cache(adapter);
927
928         tasklet_kill(&adapter->dpc_h);
929
930         megaraid_sysfs_free_resources(adapter);
931
932         megaraid_free_cmd_packets(adapter);
933
934         free_irq(adapter->irq, adapter);
935
936         iounmap(raid_dev->baseaddr);
937
938         pci_release_regions(adapter->pdev);
939
940         kfree(raid_dev);
941
942         return;
943 }
944
945
946 /**
947  * megaraid_alloc_cmd_packets - allocate shared mailbox
948  * @param adapter       : soft state of the raid controller
949  *
950  * Allocate and align the shared mailbox. This maibox is used to issue
951  * all the commands. For IO based controllers, the mailbox is also regsitered
952  * with the FW. Allocate memory for all commands as well.
953  * This is our big allocator
954  */
955 static int
956 megaraid_alloc_cmd_packets(adapter_t *adapter)
957 {
958         mraid_device_t          *raid_dev = ADAP2RAIDDEV(adapter);
959         struct pci_dev          *pdev;
960         unsigned long           align;
961         scb_t                   *scb;
962         mbox_ccb_t              *ccb;
963         struct mraid_pci_blk    *epthru_pci_blk;
964         struct mraid_pci_blk    *sg_pci_blk;
965         struct mraid_pci_blk    *mbox_pci_blk;
966         int                     i;
967
968         pdev = adapter->pdev;
969
970         /*
971          * Setup the mailbox
972          * Allocate the common 16-byte aligned memory for the handshake
973          * mailbox.
974          */
975         raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
976                         sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
977
978         if (!raid_dev->una_mbox64) {
979                 con_log(CL_ANN, (KERN_WARNING
980                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
981                         __LINE__));
982                 return -1;
983         }
984         memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
985
986         /*
987          * Align the mailbox at 16-byte boundary
988          */
989         raid_dev->mbox  = &raid_dev->una_mbox64->mbox32;
990
991         raid_dev->mbox  = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
992                                 (~0UL ^ 0xFUL));
993
994         raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
995
996         align = ((void *)raid_dev->mbox -
997                         ((void *)&raid_dev->una_mbox64->mbox32));
998
999         raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
1000                         align;
1001
1002         // Allocate memory for commands issued internally
1003         adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
1004                                 &adapter->ibuf_dma_h);
1005         if (!adapter->ibuf) {
1006
1007                 con_log(CL_ANN, (KERN_WARNING
1008                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
1009                         __LINE__));
1010
1011                 goto out_free_common_mbox;
1012         }
1013         memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
1014
1015         // Allocate memory for our SCSI Command Blocks and their associated
1016         // memory
1017
1018         /*
1019          * Allocate memory for the base list of scb. Later allocate memory for
1020          * CCBs and embedded components of each CCB and point the pointers in
1021          * scb to the allocated components
1022          * NOTE: The code to allocate SCB will be duplicated in all the LLD
1023          * since the calling routine does not yet know the number of available
1024          * commands.
1025          */
1026         adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
1027                         GFP_KERNEL);
1028
1029         if (adapter->kscb_list == NULL) {
1030                 con_log(CL_ANN, (KERN_WARNING
1031                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
1032                         __LINE__));
1033                 goto out_free_ibuf;
1034         }
1035         memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
1036
1037         // memory allocation for our command packets
1038         if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
1039                 con_log(CL_ANN, (KERN_WARNING
1040                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
1041                         __LINE__));
1042                 goto out_free_scb_list;
1043         }
1044
1045         // Adjust the scb pointers and link in the free pool
1046         epthru_pci_blk  = raid_dev->epthru_pool;
1047         sg_pci_blk      = raid_dev->sg_pool;
1048         mbox_pci_blk    = raid_dev->mbox_pool;
1049
1050         for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1051                 scb                     = adapter->kscb_list + i;
1052                 ccb                     = raid_dev->ccb_list + i;
1053
1054                 ccb->mbox       = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
1055                 ccb->raw_mbox   = (uint8_t *)ccb->mbox;
1056                 ccb->mbox64     = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
1057                 ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
1058
1059                 // make sure the mailbox is aligned properly
1060                 if (ccb->mbox_dma_h & 0x0F) {
1061                         con_log(CL_ANN, (KERN_CRIT
1062                                 "megaraid mbox: not aligned on 16-bytes\n"));
1063
1064                         goto out_teardown_dma_pools;
1065                 }
1066
1067                 ccb->epthru             = (mraid_epassthru_t *)
1068                                                 epthru_pci_blk[i].vaddr;
1069                 ccb->epthru_dma_h       = epthru_pci_blk[i].dma_addr;
1070                 ccb->pthru              = (mraid_passthru_t *)ccb->epthru;
1071                 ccb->pthru_dma_h        = ccb->epthru_dma_h;
1072
1073
1074                 ccb->sgl64              = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
1075                 ccb->sgl_dma_h          = sg_pci_blk[i].dma_addr;
1076                 ccb->sgl32              = (mbox_sgl32 *)ccb->sgl64;
1077
1078                 scb->ccb                = (caddr_t)ccb;
1079                 scb->gp                 = 0;
1080
1081                 scb->sno                = i;    // command index
1082
1083                 scb->scp                = NULL;
1084                 scb->state              = SCB_FREE;
1085                 scb->dma_direction      = PCI_DMA_NONE;
1086                 scb->dma_type           = MRAID_DMA_NONE;
1087                 scb->dev_channel        = -1;
1088                 scb->dev_target         = -1;
1089
1090                 // put scb in the free pool
1091                 list_add_tail(&scb->list, &adapter->kscb_pool);
1092         }
1093
1094         return 0;
1095
1096 out_teardown_dma_pools:
1097         megaraid_mbox_teardown_dma_pools(adapter);
1098 out_free_scb_list:
1099         kfree(adapter->kscb_list);
1100 out_free_ibuf:
1101         pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
1102                 adapter->ibuf_dma_h);
1103 out_free_common_mbox:
1104         pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1105                 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1106
1107         return -1;
1108 }
1109
1110
1111 /**
1112  * megaraid_free_cmd_packets - free memory
1113  * @param adapter       : soft state of the raid controller
1114  *
1115  * Release memory resources allocated for commands
1116  */
1117 static void
1118 megaraid_free_cmd_packets(adapter_t *adapter)
1119 {
1120         mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
1121
1122         megaraid_mbox_teardown_dma_pools(adapter);
1123
1124         kfree(adapter->kscb_list);
1125
1126         pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
1127                 (void *)adapter->ibuf, adapter->ibuf_dma_h);
1128
1129         pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
1130                 (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
1131         return;
1132 }
1133
1134
1135 /**
1136  * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
1137  * @param adapter       : HBA soft state
1138  *
1139  * setup the dma pools for mailbox, passthru and extended passthru structures,
1140  * and scatter-gather lists
1141  */
1142 static int
1143 megaraid_mbox_setup_dma_pools(adapter_t *adapter)
1144 {
1145         mraid_device_t          *raid_dev = ADAP2RAIDDEV(adapter);
1146         struct mraid_pci_blk    *epthru_pci_blk;
1147         struct mraid_pci_blk    *sg_pci_blk;
1148         struct mraid_pci_blk    *mbox_pci_blk;
1149         int                     i;
1150
1151
1152
1153         // Allocate memory for 16-bytes aligned mailboxes
1154         raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
1155                                                 adapter->pdev,
1156                                                 sizeof(mbox64_t) + 16,
1157                                                 16, 0);
1158
1159         if (raid_dev->mbox_pool_handle == NULL) {
1160                 goto fail_setup_dma_pool;
1161         }
1162
1163         mbox_pci_blk = raid_dev->mbox_pool;
1164         for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1165                 mbox_pci_blk[i].vaddr = pci_pool_alloc(
1166                                                 raid_dev->mbox_pool_handle,
1167                                                 GFP_KERNEL,
1168                                                 &mbox_pci_blk[i].dma_addr);
1169                 if (!mbox_pci_blk[i].vaddr) {
1170                         goto fail_setup_dma_pool;
1171                 }
1172         }
1173
1174         /*
1175          * Allocate memory for each embedded passthru strucuture pointer
1176          * Request for a 128 bytes aligned structure for each passthru command
1177          * structure
1178          * Since passthru and extended passthru commands are exclusive, they
1179          * share common memory pool. Passthru structures piggyback on memory
1180          * allocted to extended passthru since passthru is smaller of the two
1181          */
1182         raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
1183                         adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
1184
1185         if (raid_dev->epthru_pool_handle == NULL) {
1186                 goto fail_setup_dma_pool;
1187         }
1188
1189         epthru_pci_blk = raid_dev->epthru_pool;
1190         for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1191                 epthru_pci_blk[i].vaddr = pci_pool_alloc(
1192                                                 raid_dev->epthru_pool_handle,
1193                                                 GFP_KERNEL,
1194                                                 &epthru_pci_blk[i].dma_addr);
1195                 if (!epthru_pci_blk[i].vaddr) {
1196                         goto fail_setup_dma_pool;
1197                 }
1198         }
1199
1200
1201         // Allocate memory for each scatter-gather list. Request for 512 bytes
1202         // alignment for each sg list
1203         raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
1204                                         adapter->pdev,
1205                                         sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
1206                                         512, 0);
1207
1208         if (raid_dev->sg_pool_handle == NULL) {
1209                 goto fail_setup_dma_pool;
1210         }
1211
1212         sg_pci_blk = raid_dev->sg_pool;
1213         for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
1214                 sg_pci_blk[i].vaddr = pci_pool_alloc(
1215                                                 raid_dev->sg_pool_handle,
1216                                                 GFP_KERNEL,
1217                                                 &sg_pci_blk[i].dma_addr);
1218                 if (!sg_pci_blk[i].vaddr) {
1219                         goto fail_setup_dma_pool;
1220                 }
1221         }
1222
1223         return 0;
1224
1225 fail_setup_dma_pool:
1226         megaraid_mbox_teardown_dma_pools(adapter);
1227         return -1;
1228 }
1229
1230
1231 /**
1232  * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
1233  * @param adapter       : HBA soft state
1234  *
1235  * teardown the dma pool for mailbox, passthru and extended passthru
1236  * structures, and scatter-gather lists
1237  */
1238 static void
1239 megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
1240 {
1241         mraid_device_t          *raid_dev = ADAP2RAIDDEV(adapter);
1242         struct mraid_pci_blk    *epthru_pci_blk;
1243         struct mraid_pci_blk    *sg_pci_blk;
1244         struct mraid_pci_blk    *mbox_pci_blk;
1245         int                     i;
1246
1247
1248         sg_pci_blk = raid_dev->sg_pool;
1249         for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
1250                 pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
1251                         sg_pci_blk[i].dma_addr);
1252         }
1253         if (raid_dev->sg_pool_handle)
1254                 pci_pool_destroy(raid_dev->sg_pool_handle);
1255
1256
1257         epthru_pci_blk = raid_dev->epthru_pool;
1258         for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
1259                 pci_pool_free(raid_dev->epthru_pool_handle,
1260                         epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
1261         }
1262         if (raid_dev->epthru_pool_handle)
1263                 pci_pool_destroy(raid_dev->epthru_pool_handle);
1264
1265
1266         mbox_pci_blk = raid_dev->mbox_pool;
1267         for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
1268                 pci_pool_free(raid_dev->mbox_pool_handle,
1269                         mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
1270         }
1271         if (raid_dev->mbox_pool_handle)
1272                 pci_pool_destroy(raid_dev->mbox_pool_handle);
1273
1274         return;
1275 }
1276
1277
1278 /**
1279  * megaraid_alloc_scb - detach and return a scb from the free list
1280  * @adapter     : controller's soft state
1281  *
1282  * return the scb from the head of the free list. NULL if there are none
1283  * available
1284  **/
1285 static scb_t *
1286 megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
1287 {
1288         struct list_head        *head = &adapter->kscb_pool;
1289         scb_t                   *scb = NULL;
1290         unsigned long           flags;
1291
1292         // detach scb from free pool
1293         spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1294
1295         if (list_empty(head)) {
1296                 spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1297                 return NULL;
1298         }
1299
1300         scb = list_entry(head->next, scb_t, list);
1301         list_del_init(&scb->list);
1302
1303         spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1304
1305         scb->state      = SCB_ACTIVE;
1306         scb->scp        = scp;
1307         scb->dma_type   = MRAID_DMA_NONE;
1308
1309         return scb;
1310 }
1311
1312
1313 /**
1314  * megaraid_dealloc_scb - return the scb to the free pool
1315  * @adapter     : controller's soft state
1316  * @scb         : scb to be freed
1317  *
1318  * return the scb back to the free list of scbs. The caller must 'flush' the
1319  * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
1320  * NOTE NOTE: Make sure the scb is not on any list before calling this
1321  * routine.
1322  **/
1323 static inline void
1324 megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
1325 {
1326         unsigned long           flags;
1327
1328         // put scb in the free pool
1329         scb->state      = SCB_FREE;
1330         scb->scp        = NULL;
1331         spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
1332
1333         list_add(&scb->list, &adapter->kscb_pool);
1334
1335         spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
1336
1337         return;
1338 }
1339
1340
1341 /**
1342  * megaraid_mbox_mksgl - make the scatter-gather list
1343  * @adapter     - controller's soft state
1344  * @scb         - scsi control block
1345  *
1346  * prepare the scatter-gather list
1347  */
1348 static int
1349 megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
1350 {
1351         struct scatterlist      *sgl;
1352         mbox_ccb_t              *ccb;
1353         struct page             *page;
1354         unsigned long           offset;
1355         struct scsi_cmnd        *scp;
1356         int                     sgcnt;
1357         int                     i;
1358
1359
1360         scp     = scb->scp;
1361         ccb     = (mbox_ccb_t *)scb->ccb;
1362
1363         // no mapping required if no data to be transferred
1364         if (!scp->request_buffer || !scp->request_bufflen)
1365                 return 0;
1366
1367         if (!scp->use_sg) {     /* scatter-gather list not used */
1368
1369                 page = virt_to_page(scp->request_buffer);
1370
1371                 offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
1372
1373                 ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
1374                                                   scp->request_bufflen,
1375                                                   scb->dma_direction);
1376                 scb->dma_type = MRAID_DMA_WBUF;
1377
1378                 /*
1379                  * We need to handle special 64-bit commands that need a
1380                  * minimum of 1 SG
1381                  */
1382                 sgcnt = 1;
1383                 ccb->sgl64[0].address   = ccb->buf_dma_h;
1384                 ccb->sgl64[0].length    = scp->request_bufflen;
1385
1386                 return sgcnt;
1387         }
1388
1389         sgl = (struct scatterlist *)scp->request_buffer;
1390
1391         // The number of sg elements returned must not exceed our limit
1392         sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
1393                         scb->dma_direction);
1394
1395         if (sgcnt > adapter->sglen) {
1396                 con_log(CL_ANN, (KERN_CRIT
1397                         "megaraid critical: too many sg elements:%d\n",
1398                         sgcnt));
1399                 BUG();
1400         }
1401
1402         scb->dma_type = MRAID_DMA_WSG;
1403
1404         for (i = 0; i < sgcnt; i++, sgl++) {
1405                 ccb->sgl64[i].address   = sg_dma_address(sgl);
1406                 ccb->sgl64[i].length    = sg_dma_len(sgl);
1407         }
1408
1409         // Return count of SG nodes
1410         return sgcnt;
1411 }
1412
1413
1414 /**
1415  * mbox_post_cmd - issue a mailbox command
1416  * @adapter     - controller's soft state
1417  * @scb         - command to be issued
1418  *
1419  * post the command to the controller if mailbox is availble.
1420  */
1421 static int
1422 mbox_post_cmd(adapter_t *adapter, scb_t *scb)
1423 {
1424         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
1425         mbox64_t        *mbox64;
1426         mbox_t          *mbox;
1427         mbox_ccb_t      *ccb;
1428         unsigned long   flags;
1429         unsigned int    i = 0;
1430
1431
1432         ccb     = (mbox_ccb_t *)scb->ccb;
1433         mbox    = raid_dev->mbox;
1434         mbox64  = raid_dev->mbox64;
1435
1436         /*
1437          * Check for busy mailbox. If it is, return failure - the caller
1438          * should retry later.
1439          */
1440         spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
1441
1442         if (unlikely(mbox->busy)) {
1443                 do {
1444                         udelay(1);
1445                         i++;
1446                         rmb();
1447                 } while(mbox->busy && (i < max_mbox_busy_wait));
1448
1449                 if (mbox->busy) {
1450
1451                         spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1452
1453                         return -1;
1454                 }
1455         }
1456
1457
1458         // Copy this command's mailbox data into "adapter's" mailbox
1459         memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
1460         mbox->cmdid = scb->sno;
1461
1462         adapter->outstanding_cmds++;
1463
1464         if (scb->dma_direction == PCI_DMA_TODEVICE) {
1465                 if (!scb->scp->use_sg) {        // sg list not used
1466                         pci_dma_sync_single_for_device(adapter->pdev,
1467                                         ccb->buf_dma_h,
1468                                         scb->scp->request_bufflen,
1469                                         PCI_DMA_TODEVICE);
1470                 }
1471                 else {
1472                         pci_dma_sync_sg_for_device(adapter->pdev,
1473                                 scb->scp->request_buffer,
1474                                 scb->scp->use_sg, PCI_DMA_TODEVICE);
1475                 }
1476         }
1477
1478         mbox->busy      = 1;    // Set busy
1479         mbox->poll      = 0;
1480         mbox->ack       = 0;
1481         wmb();
1482
1483         WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
1484
1485         spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
1486
1487         return 0;
1488 }
1489
1490
1491 /**
1492  * megaraid_queue_command - generic queue entry point for all LLDs
1493  * @scp         : pointer to the scsi command to be executed
1494  * @done        : callback routine to be called after the cmd has be completed
1495  *
1496  * Queue entry point for mailbox based controllers.
1497  */
1498 static int
1499 megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
1500 {
1501         adapter_t       *adapter;
1502         scb_t           *scb;
1503         int             if_busy;
1504
1505         adapter         = SCP2ADAPTER(scp);
1506         scp->scsi_done  = done;
1507         scp->result     = 0;
1508
1509         /*
1510          * Allocate and build a SCB request
1511          * if_busy flag will be set if megaraid_mbox_build_cmd() command could
1512          * not allocate scb. We will return non-zero status in that case.
1513          * NOTE: scb can be null even though certain commands completed
1514          * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
1515          * return 0 in that case, and we would do the callback right away.
1516          */
1517         if_busy = 0;
1518         scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
1519         if (!scb) {     // command already completed
1520                 done(scp);
1521                 return 0;
1522         }
1523
1524         megaraid_mbox_runpendq(adapter, scb);
1525         return if_busy;
1526 }
1527
1528 /**
1529  * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
1530  * firmware lingua
1531  * @adapter     - controller's soft state
1532  * @scp         - mid-layer scsi command pointer
1533  * @busy        - set if request could not be completed because of lack of
1534  *              resources
1535  *
1536  * convert the command issued by mid-layer to format understood by megaraid
1537  * firmware. We also complete certain command without sending them to firmware
1538  */
1539 static scb_t *
1540 megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
1541 {
1542         mraid_device_t          *rdev = ADAP2RAIDDEV(adapter);
1543         int                     channel;
1544         int                     target;
1545         int                     islogical;
1546         mbox_ccb_t              *ccb;
1547         mraid_passthru_t        *pthru;
1548         mbox64_t                *mbox64;
1549         mbox_t                  *mbox;
1550         scb_t                   *scb;
1551         char                    skip[] = "skipping";
1552         char                    scan[] = "scanning";
1553         char                    *ss;
1554
1555
1556         /*
1557          * Get the appropriate device map for the device this command is
1558          * intended for
1559          */
1560         MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
1561
1562         /*
1563          * Logical drive commands
1564          */
1565         if (islogical) {
1566                 switch (scp->cmnd[0]) {
1567                 case TEST_UNIT_READY:
1568                         /*
1569                          * Do we support clustering and is the support enabled
1570                          * If no, return success always
1571                          */
1572                         if (!adapter->ha) {
1573                                 scp->result = (DID_OK << 16);
1574                                 return NULL;
1575                         }
1576
1577                         if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1578                                 scp->result = (DID_ERROR << 16);
1579                                 *busy = 1;
1580                                 return NULL;
1581                         }
1582
1583                         scb->dma_direction      = scp->sc_data_direction;
1584                         scb->dev_channel        = 0xFF;
1585                         scb->dev_target         = target;
1586                         ccb                     = (mbox_ccb_t *)scb->ccb;
1587
1588                         /*
1589                          * The command id will be provided by the command
1590                          * issuance routine
1591                          */
1592                         ccb->raw_mbox[0]        = CLUSTER_CMD;
1593                         ccb->raw_mbox[2]        = RESERVATION_STATUS;
1594                         ccb->raw_mbox[3]        = target;
1595
1596                         return scb;
1597
1598                 case MODE_SENSE:
1599                         if (scp->use_sg) {
1600                                 struct scatterlist      *sgl;
1601                                 caddr_t                 vaddr;
1602
1603                                 sgl = (struct scatterlist *)scp->request_buffer;
1604                                 if (sgl->page) {
1605                                         vaddr = (caddr_t)
1606                                                 (page_address((&sgl[0])->page)
1607                                                 + (&sgl[0])->offset);
1608
1609                                         memset(vaddr, 0, scp->cmnd[4]);
1610                                 }
1611                                 else {
1612                                         con_log(CL_ANN, (KERN_WARNING
1613                                         "megaraid mailbox: invalid sg:%d\n",
1614                                         __LINE__));
1615                                 }
1616                         }
1617                         else {
1618                                 memset(scp->request_buffer, 0, scp->cmnd[4]);
1619                         }
1620                         scp->result = (DID_OK << 16);
1621                         return NULL;
1622
1623                 case INQUIRY:
1624                         /*
1625                          * Display the channel scan for logical drives
1626                          * Do not display scan for a channel if already done.
1627                          */
1628                         if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1629
1630                                 con_log(CL_ANN, (KERN_INFO
1631                                         "scsi[%d]: scanning scsi channel %d",
1632                                         adapter->host->host_no,
1633                                         SCP2CHANNEL(scp)));
1634
1635                                 con_log(CL_ANN, (
1636                                         " [virtual] for logical drives\n"));
1637
1638                                 rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1639                         }
1640
1641                         /* Fall through */
1642
1643                 case READ_CAPACITY:
1644                         /*
1645                          * Do not allow LUN > 0 for logical drives and
1646                          * requests for more than 40 logical drives
1647                          */
1648                         if (SCP2LUN(scp)) {
1649                                 scp->result = (DID_BAD_TARGET << 16);
1650                                 return NULL;
1651                         }
1652                         if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
1653                                 scp->result = (DID_BAD_TARGET << 16);
1654                                 return NULL;
1655                         }
1656
1657
1658                         /* Allocate a SCB and initialize passthru */
1659                         if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1660                                 scp->result = (DID_ERROR << 16);
1661                                 *busy = 1;
1662                                 return NULL;
1663                         }
1664
1665                         ccb                     = (mbox_ccb_t *)scb->ccb;
1666                         scb->dev_channel        = 0xFF;
1667                         scb->dev_target         = target;
1668                         pthru                   = ccb->pthru;
1669                         mbox                    = ccb->mbox;
1670                         mbox64                  = ccb->mbox64;
1671
1672                         pthru->timeout          = 0;
1673                         pthru->ars              = 1;
1674                         pthru->reqsenselen      = 14;
1675                         pthru->islogical        = 1;
1676                         pthru->logdrv           = target;
1677                         pthru->cdblen           = scp->cmd_len;
1678                         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1679
1680                         mbox->cmd               = MBOXCMD_PASSTHRU64;
1681                         scb->dma_direction      = scp->sc_data_direction;
1682
1683                         pthru->dataxferlen      = scp->request_bufflen;
1684                         pthru->dataxferaddr     = ccb->sgl_dma_h;
1685                         pthru->numsge           = megaraid_mbox_mksgl(adapter,
1686                                                         scb);
1687
1688                         mbox->xferaddr          = 0xFFFFFFFF;
1689                         mbox64->xferaddr_lo     = (uint32_t )ccb->pthru_dma_h;
1690                         mbox64->xferaddr_hi     = 0;
1691
1692                         return scb;
1693
1694                 case READ_6:
1695                 case WRITE_6:
1696                 case READ_10:
1697                 case WRITE_10:
1698                 case READ_12:
1699                 case WRITE_12:
1700
1701                         /*
1702                          * Allocate a SCB and initialize mailbox
1703                          */
1704                         if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1705                                 scp->result = (DID_ERROR << 16);
1706                                 *busy = 1;
1707                                 return NULL;
1708                         }
1709                         ccb                     = (mbox_ccb_t *)scb->ccb;
1710                         scb->dev_channel        = 0xFF;
1711                         scb->dev_target         = target;
1712                         mbox                    = ccb->mbox;
1713                         mbox64                  = ccb->mbox64;
1714                         mbox->logdrv            = target;
1715
1716                         /*
1717                          * A little HACK: 2nd bit is zero for all scsi read
1718                          * commands and is set for all scsi write commands
1719                          */
1720                         mbox->cmd = (scp->cmnd[0] & 0x02) ?  MBOXCMD_LWRITE64:
1721                                         MBOXCMD_LREAD64 ;
1722
1723                         /*
1724                          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1725                          */
1726                         if (scp->cmd_len == 6) {
1727                                 mbox->numsectors = (uint32_t)scp->cmnd[4];
1728                                 mbox->lba =
1729                                         ((uint32_t)scp->cmnd[1] << 16)  |
1730                                         ((uint32_t)scp->cmnd[2] << 8)   |
1731                                         (uint32_t)scp->cmnd[3];
1732
1733                                 mbox->lba &= 0x1FFFFF;
1734                         }
1735
1736                         /*
1737                          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1738                          */
1739                         else if (scp->cmd_len == 10) {
1740                                 mbox->numsectors =
1741                                         (uint32_t)scp->cmnd[8] |
1742                                         ((uint32_t)scp->cmnd[7] << 8);
1743                                 mbox->lba =
1744                                         ((uint32_t)scp->cmnd[2] << 24) |
1745                                         ((uint32_t)scp->cmnd[3] << 16) |
1746                                         ((uint32_t)scp->cmnd[4] << 8) |
1747                                         (uint32_t)scp->cmnd[5];
1748                         }
1749
1750                         /*
1751                          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1752                          */
1753                         else if (scp->cmd_len == 12) {
1754                                 mbox->lba =
1755                                         ((uint32_t)scp->cmnd[2] << 24) |
1756                                         ((uint32_t)scp->cmnd[3] << 16) |
1757                                         ((uint32_t)scp->cmnd[4] << 8) |
1758                                         (uint32_t)scp->cmnd[5];
1759
1760                                 mbox->numsectors =
1761                                         ((uint32_t)scp->cmnd[6] << 24) |
1762                                         ((uint32_t)scp->cmnd[7] << 16) |
1763                                         ((uint32_t)scp->cmnd[8] << 8) |
1764                                         (uint32_t)scp->cmnd[9];
1765                         }
1766                         else {
1767                                 con_log(CL_ANN, (KERN_WARNING
1768                                         "megaraid: unsupported CDB length\n"));
1769
1770                                 megaraid_dealloc_scb(adapter, scb);
1771
1772                                 scp->result = (DID_ERROR << 16);
1773                                 return NULL;
1774                         }
1775
1776                         scb->dma_direction = scp->sc_data_direction;
1777
1778                         // Calculate Scatter-Gather info
1779                         mbox64->xferaddr_lo     = (uint32_t )ccb->sgl_dma_h;
1780                         mbox->numsge            = megaraid_mbox_mksgl(adapter,
1781                                                         scb);
1782                         mbox->xferaddr          = 0xFFFFFFFF;
1783                         mbox64->xferaddr_hi     = 0;
1784
1785                         return scb;
1786
1787                 case RESERVE:
1788                 case RELEASE:
1789                         /*
1790                          * Do we support clustering and is the support enabled
1791                          */
1792                         if (!adapter->ha) {
1793                                 scp->result = (DID_BAD_TARGET << 16);
1794                                 return NULL;
1795                         }
1796
1797                         /*
1798                          * Allocate a SCB and initialize mailbox
1799                          */
1800                         if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1801                                 scp->result = (DID_ERROR << 16);
1802                                 *busy = 1;
1803                                 return NULL;
1804                         }
1805
1806                         ccb                     = (mbox_ccb_t *)scb->ccb;
1807                         scb->dev_channel        = 0xFF;
1808                         scb->dev_target         = target;
1809                         ccb->raw_mbox[0]        = CLUSTER_CMD;
1810                         ccb->raw_mbox[2]        =  (scp->cmnd[0] == RESERVE) ?
1811                                                 RESERVE_LD : RELEASE_LD;
1812
1813                         ccb->raw_mbox[3]        = target;
1814                         scb->dma_direction      = scp->sc_data_direction;
1815
1816                         return scb;
1817
1818                 default:
1819                         scp->result = (DID_BAD_TARGET << 16);
1820                         return NULL;
1821                 }
1822         }
1823         else { // Passthru device commands
1824
1825                 // Do not allow access to target id > 15 or LUN > 7
1826                 if (target > 15 || SCP2LUN(scp) > 7) {
1827                         scp->result = (DID_BAD_TARGET << 16);
1828                         return NULL;
1829                 }
1830
1831                 // if fast load option was set and scan for last device is
1832                 // over, reset the fast_load flag so that during a possible
1833                 // next scan, devices can be made available
1834                 if (rdev->fast_load && (target == 15) &&
1835                         (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
1836
1837                         con_log(CL_ANN, (KERN_INFO
1838                         "megaraid[%d]: physical device scan re-enabled\n",
1839                                 adapter->host->host_no));
1840                         rdev->fast_load = 0;
1841                 }
1842
1843                 /*
1844                  * Display the channel scan for physical devices
1845                  */
1846                 if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
1847
1848                         ss = rdev->fast_load ? skip : scan;
1849
1850                         con_log(CL_ANN, (KERN_INFO
1851                                 "scsi[%d]: %s scsi channel %d [Phy %d]",
1852                                 adapter->host->host_no, ss, SCP2CHANNEL(scp),
1853                                 channel));
1854
1855                         con_log(CL_ANN, (
1856                                 " for non-raid devices\n"));
1857
1858                         rdev->last_disp |= (1L << SCP2CHANNEL(scp));
1859                 }
1860
1861                 // disable channel sweep if fast load option given
1862                 if (rdev->fast_load) {
1863                         scp->result = (DID_BAD_TARGET << 16);
1864                         return NULL;
1865                 }
1866
1867                 // Allocate a SCB and initialize passthru
1868                 if (!(scb = megaraid_alloc_scb(adapter, scp))) {
1869                         scp->result = (DID_ERROR << 16);
1870                         *busy = 1;
1871                         return NULL;
1872                 }
1873
1874                 ccb                     = (mbox_ccb_t *)scb->ccb;
1875                 scb->dev_channel        = channel;
1876                 scb->dev_target         = target;
1877                 scb->dma_direction      = scp->sc_data_direction;
1878                 mbox                    = ccb->mbox;
1879                 mbox64                  = ccb->mbox64;
1880
1881                 // Does this firmware support extended CDBs
1882                 if (adapter->max_cdb_sz == 16) {
1883                         mbox->cmd               = MBOXCMD_EXTPTHRU;
1884
1885                         megaraid_mbox_prepare_epthru(adapter, scb, scp);
1886
1887                         mbox64->xferaddr_lo     = (uint32_t)ccb->epthru_dma_h;
1888                         mbox64->xferaddr_hi     = 0;
1889                         mbox->xferaddr          = 0xFFFFFFFF;
1890                 }
1891                 else {
1892                         mbox->cmd = MBOXCMD_PASSTHRU64;
1893
1894                         megaraid_mbox_prepare_pthru(adapter, scb, scp);
1895
1896                         mbox64->xferaddr_lo     = (uint32_t)ccb->pthru_dma_h;
1897                         mbox64->xferaddr_hi     = 0;
1898                         mbox->xferaddr          = 0xFFFFFFFF;
1899                 }
1900                 return scb;
1901         }
1902
1903         // NOT REACHED
1904 }
1905
1906
1907 /**
1908  * megaraid_mbox_runpendq - execute commands queued in the pending queue
1909  * @adapter     : controller's soft state
1910  * @scb         : SCB to be queued in the pending list
1911  *
1912  * scan the pending list for commands which are not yet issued and try to
1913  * post to the controller. The SCB can be a null pointer, which would indicate
1914  * no SCB to be queue, just try to execute the ones in the pending list.
1915  *
1916  * NOTE: We do not actually traverse the pending list. The SCBs are plucked
1917  * out from the head of the pending list. If it is successfully issued, the
1918  * next SCB is at the head now.
1919  */
1920 static void
1921 megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
1922 {
1923         scb_t                   *scb;
1924         unsigned long           flags;
1925
1926         spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1927
1928         if (scb_q) {
1929                 scb_q->state = SCB_PENDQ;
1930                 list_add_tail(&scb_q->list, &adapter->pend_list);
1931         }
1932
1933         // if the adapter in not in quiescent mode, post the commands to FW
1934         if (adapter->quiescent) {
1935                 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1936                 return;
1937         }
1938
1939         while (!list_empty(&adapter->pend_list)) {
1940
1941                 assert_spin_locked(PENDING_LIST_LOCK(adapter));
1942
1943                 scb = list_entry(adapter->pend_list.next, scb_t, list);
1944
1945                 // remove the scb from the pending list and try to
1946                 // issue. If we are unable to issue it, put back in
1947                 // the pending list and return
1948
1949                 list_del_init(&scb->list);
1950
1951                 spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1952
1953                 // if mailbox was busy, return SCB back to pending
1954                 // list. Make sure to add at the head, since that's
1955                 // where it would have been removed from
1956
1957                 scb->state = SCB_ISSUED;
1958
1959                 if (mbox_post_cmd(adapter, scb) != 0) {
1960
1961                         spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1962
1963                         scb->state = SCB_PENDQ;
1964
1965                         list_add(&scb->list, &adapter->pend_list);
1966
1967                         spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
1968                                 flags);
1969
1970                         return;
1971                 }
1972
1973                 spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
1974         }
1975
1976         spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
1977
1978
1979         return;
1980 }
1981
1982
1983 /**
1984  * megaraid_mbox_prepare_pthru - prepare a command for physical devices
1985  * @adapter     - pointer to controller's soft state
1986  * @scb         - scsi control block
1987  * @scp         - scsi command from the mid-layer
1988  *
1989  * prepare a command for the scsi physical devices
1990  */
1991 static void
1992 megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
1993                 struct scsi_cmnd *scp)
1994 {
1995         mbox_ccb_t              *ccb;
1996         mraid_passthru_t        *pthru;
1997         uint8_t                 channel;
1998         uint8_t                 target;
1999
2000         ccb     = (mbox_ccb_t *)scb->ccb;
2001         pthru   = ccb->pthru;
2002         channel = scb->dev_channel;
2003         target  = scb->dev_target;
2004
2005         // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2006         pthru->timeout          = 4;    
2007         pthru->ars              = 1;
2008         pthru->islogical        = 0;
2009         pthru->channel          = 0;
2010         pthru->target           = (channel << 4) | target;
2011         pthru->logdrv           = SCP2LUN(scp);
2012         pthru->reqsenselen      = 14;
2013         pthru->cdblen           = scp->cmd_len;
2014
2015         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
2016
2017         if (scp->request_bufflen) {
2018                 pthru->dataxferlen      = scp->request_bufflen;
2019                 pthru->dataxferaddr     = ccb->sgl_dma_h;
2020                 pthru->numsge           = megaraid_mbox_mksgl(adapter, scb);
2021         }
2022         else {
2023                 pthru->dataxferaddr     = 0;
2024                 pthru->dataxferlen      = 0;
2025                 pthru->numsge           = 0;
2026         }
2027         return;
2028 }
2029
2030
2031 /**
2032  * megaraid_mbox_prepare_epthru - prepare a command for physical devices
2033  * @adapter     - pointer to controller's soft state
2034  * @scb         - scsi control block
2035  * @scp         - scsi command from the mid-layer
2036  *
2037  * prepare a command for the scsi physical devices. This rountine prepares
2038  * commands for devices which can take extended CDBs (>10 bytes)
2039  */
2040 static void
2041 megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
2042                 struct scsi_cmnd *scp)
2043 {
2044         mbox_ccb_t              *ccb;
2045         mraid_epassthru_t       *epthru;
2046         uint8_t                 channel;
2047         uint8_t                 target;
2048
2049         ccb     = (mbox_ccb_t *)scb->ccb;
2050         epthru  = ccb->epthru;
2051         channel = scb->dev_channel;
2052         target  = scb->dev_target;
2053
2054         // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
2055         epthru->timeout         = 4;    
2056         epthru->ars             = 1;
2057         epthru->islogical       = 0;
2058         epthru->channel         = 0;
2059         epthru->target          = (channel << 4) | target;
2060         epthru->logdrv          = SCP2LUN(scp);
2061         epthru->reqsenselen     = 14;
2062         epthru->cdblen          = scp->cmd_len;
2063
2064         memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
2065
2066         if (scp->request_bufflen) {
2067                 epthru->dataxferlen     = scp->request_bufflen;
2068                 epthru->dataxferaddr    = ccb->sgl_dma_h;
2069                 epthru->numsge          = megaraid_mbox_mksgl(adapter, scb);
2070         }
2071         else {
2072                 epthru->dataxferaddr    = 0;
2073                 epthru->dataxferlen     = 0;
2074                 epthru->numsge          = 0;
2075         }
2076         return;
2077 }
2078
2079
2080 /**
2081  * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
2082  * @adapter     - controller's soft state
2083  *
2084  * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
2085  * completed command and put them on the completed list for later processing.
2086  *
2087  * Returns:     1 if the interrupt is valid, 0 otherwise
2088  */
2089 static int
2090 megaraid_ack_sequence(adapter_t *adapter)
2091 {
2092         mraid_device_t          *raid_dev = ADAP2RAIDDEV(adapter);
2093         mbox_t                  *mbox;
2094         scb_t                   *scb;
2095         uint8_t                 nstatus;
2096         uint8_t                 completed[MBOX_MAX_FIRMWARE_STATUS];
2097         struct list_head        clist;
2098         int                     handled;
2099         uint32_t                dword;
2100         unsigned long           flags;
2101         int                     i, j;
2102
2103
2104         mbox    = raid_dev->mbox;
2105
2106         // move the SCBs from the firmware completed array to our local list
2107         INIT_LIST_HEAD(&clist);
2108
2109         // loop till F/W has more commands for us to complete
2110         handled = 0;
2111         spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
2112         do {
2113                 /*
2114                  * Check if a valid interrupt is pending. If found, force the
2115                  * interrupt line low.
2116                  */
2117                 dword = RDOUTDOOR(raid_dev);
2118                 if (dword != 0x10001234) break;
2119
2120                 handled = 1;
2121
2122                 WROUTDOOR(raid_dev, 0x10001234);
2123
2124                 nstatus = 0;
2125                 // wait for valid numstatus to post
2126                 for (i = 0; i < 0xFFFFF; i++) {
2127                         if (mbox->numstatus != 0xFF) {
2128                                 nstatus = mbox->numstatus;
2129                                 break;
2130                         }
2131                         rmb();
2132                 }
2133                 mbox->numstatus = 0xFF;
2134
2135                 adapter->outstanding_cmds -= nstatus;
2136
2137                 for (i = 0; i < nstatus; i++) {
2138
2139                         // wait for valid command index to post
2140                         for (j = 0; j < 0xFFFFF; j++) {
2141                                 if (mbox->completed[i] != 0xFF) break;
2142                                 rmb();
2143                         }
2144                         completed[i]            = mbox->completed[i];
2145                         mbox->completed[i]      = 0xFF;
2146
2147                         if (completed[i] == 0xFF) {
2148                                 con_log(CL_ANN, (KERN_CRIT
2149                                 "megaraid: command posting timed out\n"));
2150
2151                                 BUG();
2152                                 continue;
2153                         }
2154
2155                         // Get SCB associated with this command id
2156                         if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
2157                                 // a cmm command
2158                                 scb = adapter->uscb_list + (completed[i] -
2159                                                 MBOX_MAX_SCSI_CMDS);
2160                         }
2161                         else {
2162                                 // an os command
2163                                 scb = adapter->kscb_list + completed[i];
2164                         }
2165
2166                         scb->status = mbox->status;
2167                         list_add_tail(&scb->list, &clist);
2168                 }
2169
2170                 // Acknowledge interrupt
2171                 WRINDOOR(raid_dev, 0x02);
2172
2173         } while(1);
2174
2175         spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
2176
2177
2178         // put the completed commands in the completed list. DPC would
2179         // complete these commands later
2180         spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2181
2182         list_splice(&clist, &adapter->completed_list);
2183
2184         spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2185
2186
2187         // schedule the DPC if there is some work for it
2188         if (handled)
2189                 tasklet_schedule(&adapter->dpc_h);
2190
2191         return handled;
2192 }
2193
2194
2195 /**
2196  * megaraid_isr - isr for memory based mailbox based controllers
2197  * @irq         - irq
2198  * @devp        - pointer to our soft state
2199  * @regs        - unused
2200  *
2201  * Interrupt service routine for memory-mapped mailbox controllers.
2202  */
2203 static irqreturn_t
2204 megaraid_isr(int irq, void *devp, struct pt_regs *regs)
2205 {
2206         adapter_t       *adapter = devp;
2207         int             handled;
2208
2209         handled = megaraid_ack_sequence(adapter);
2210
2211         /* Loop through any pending requests */
2212         if (!adapter->quiescent) {
2213                 megaraid_mbox_runpendq(adapter, NULL);
2214         }
2215
2216         return IRQ_RETVAL(handled);
2217 }
2218
2219
2220 /**
2221  * megaraid_mbox_sync_scb - sync kernel buffers
2222  * @adapter     : controller's soft state
2223  * @scb         : pointer to the resource packet
2224  *
2225  * DMA sync if required.
2226  */
2227 static void
2228 megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
2229 {
2230         mbox_ccb_t      *ccb;
2231
2232         ccb     = (mbox_ccb_t *)scb->ccb;
2233
2234         switch (scb->dma_type) {
2235
2236         case MRAID_DMA_WBUF:
2237                 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2238                         pci_dma_sync_single_for_cpu(adapter->pdev,
2239                                         ccb->buf_dma_h,
2240                                         scb->scp->request_bufflen,
2241                                         PCI_DMA_FROMDEVICE);
2242                 }
2243
2244                 pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
2245                         scb->scp->request_bufflen, scb->dma_direction);
2246
2247                 break;
2248
2249         case MRAID_DMA_WSG:
2250                 if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
2251                         pci_dma_sync_sg_for_cpu(adapter->pdev,
2252                                         scb->scp->request_buffer,
2253                                         scb->scp->use_sg, PCI_DMA_FROMDEVICE);
2254                 }
2255
2256                 pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
2257                         scb->scp->use_sg, scb->dma_direction);
2258
2259                 break;
2260
2261         default:
2262                 break;
2263         }
2264
2265         return;
2266 }
2267
2268
2269 /**
2270  * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
2271  * @devp        : pointer to HBA soft state
2272  *
2273  * Pick up the commands from the completed list and send back to the owners.
2274  * This is a reentrant function and does not assume any locks are held while
2275  * it is being called.
2276  */
2277 static void
2278 megaraid_mbox_dpc(unsigned long devp)
2279 {
2280         adapter_t               *adapter = (adapter_t *)devp;
2281         mraid_device_t          *raid_dev;
2282         struct list_head        clist;
2283         struct scatterlist      *sgl;
2284         scb_t                   *scb;
2285         scb_t                   *tmp;
2286         struct scsi_cmnd        *scp;
2287         mraid_passthru_t        *pthru;
2288         mraid_epassthru_t       *epthru;
2289         mbox_ccb_t              *ccb;
2290         int                     islogical;
2291         int                     pdev_index;
2292         int                     pdev_state;
2293         mbox_t                  *mbox;
2294         unsigned long           flags;
2295         uint8_t                 c;
2296         int                     status;
2297         uioc_t                  *kioc;
2298
2299
2300         if (!adapter) return;
2301
2302         raid_dev = ADAP2RAIDDEV(adapter);
2303
2304         // move the SCBs from the completed list to our local list
2305         INIT_LIST_HEAD(&clist);
2306
2307         spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2308
2309         list_splice_init(&adapter->completed_list, &clist);
2310
2311         spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2312
2313
2314         list_for_each_entry_safe(scb, tmp, &clist, list) {
2315
2316                 status          = scb->status;
2317                 scp             = scb->scp;
2318                 ccb             = (mbox_ccb_t *)scb->ccb;
2319                 pthru           = ccb->pthru;
2320                 epthru          = ccb->epthru;
2321                 mbox            = ccb->mbox;
2322
2323                 // Make sure f/w has completed a valid command
2324                 if (scb->state != SCB_ISSUED) {
2325                         con_log(CL_ANN, (KERN_CRIT
2326                         "megaraid critical err: invalid command %d:%d:%p\n",
2327                                 scb->sno, scb->state, scp));
2328                         BUG();
2329                         continue;       // Must never happen!
2330                 }
2331
2332                 // check for the management command and complete it right away
2333                 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2334                         scb->state      = SCB_FREE;
2335                         scb->status     = status;
2336
2337                         // remove from local clist
2338                         list_del_init(&scb->list);
2339
2340                         kioc                    = (uioc_t *)scb->gp;
2341                         kioc->status            = 0;
2342
2343                         megaraid_mbox_mm_done(adapter, scb);
2344
2345                         continue;
2346                 }
2347
2348                 // Was an abort issued for this command earlier
2349                 if (scb->state & SCB_ABORT) {
2350                         con_log(CL_ANN, (KERN_NOTICE
2351                         "megaraid: aborted cmd %lx[%x] completed\n",
2352                                 scp->serial_number, scb->sno));
2353                 }
2354
2355                 /*
2356                  * If the inquiry came of a disk drive which is not part of
2357                  * any RAID array, expose it to the kernel. For this to be
2358                  * enabled, user must set the "megaraid_expose_unconf_disks"
2359                  * flag to 1 by specifying it on module parameter list.
2360                  * This would enable data migration off drives from other
2361                  * configurations.
2362                  */
2363                 islogical = MRAID_IS_LOGICAL(adapter, scp);
2364                 if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
2365                                 && IS_RAID_CH(raid_dev, scb->dev_channel)) {
2366
2367                         if (scp->use_sg) {
2368                                 sgl = (struct scatterlist *)
2369                                         scp->request_buffer;
2370
2371                                 if (sgl->page) {
2372                                         c = *(unsigned char *)
2373                                         (page_address((&sgl[0])->page) +
2374                                                 (&sgl[0])->offset);
2375                                 }
2376                                 else {
2377                                         con_log(CL_ANN, (KERN_WARNING
2378                                         "megaraid mailbox: invalid sg:%d\n",
2379                                         __LINE__));
2380                                         c = 0;
2381                                 }
2382                         }
2383                         else {
2384                                 c = *(uint8_t *)scp->request_buffer;
2385                         }
2386
2387                         if ((c & 0x1F ) == TYPE_DISK) {
2388                                 pdev_index = (scb->dev_channel * 16) +
2389                                         scb->dev_target;
2390                                 pdev_state =
2391                                         raid_dev->pdrv_state[pdev_index] & 0x0F;
2392
2393                                 if (pdev_state == PDRV_ONLINE           ||
2394                                         pdev_state == PDRV_FAILED       ||
2395                                         pdev_state == PDRV_RBLD         ||
2396                                         pdev_state == PDRV_HOTSPARE     ||
2397                                         megaraid_expose_unconf_disks == 0) {
2398
2399                                         status = 0xF0;
2400                                 }
2401                         }
2402                 }
2403
2404                 // Convert MegaRAID status to Linux error code
2405                 switch (status) {
2406
2407                 case 0x00:
2408
2409                         scp->result = (DID_OK << 16);
2410                         break;
2411
2412                 case 0x02:
2413
2414                         /* set sense_buffer and result fields */
2415                         if (mbox->cmd == MBOXCMD_PASSTHRU ||
2416                                 mbox->cmd == MBOXCMD_PASSTHRU64) {
2417
2418                                 memcpy(scp->sense_buffer, pthru->reqsensearea,
2419                                                 14);
2420
2421                                 scp->result = DRIVER_SENSE << 24 |
2422                                         DID_OK << 16 | CHECK_CONDITION << 1;
2423                         }
2424                         else {
2425                                 if (mbox->cmd == MBOXCMD_EXTPTHRU) {
2426
2427                                         memcpy(scp->sense_buffer,
2428                                                 epthru->reqsensearea, 14);
2429
2430                                         scp->result = DRIVER_SENSE << 24 |
2431                                                 DID_OK << 16 |
2432                                                 CHECK_CONDITION << 1;
2433                                 } else {
2434                                         scp->sense_buffer[0] = 0x70;
2435                                         scp->sense_buffer[2] = ABORTED_COMMAND;
2436                                         scp->result = CHECK_CONDITION << 1;
2437                                 }
2438                         }
2439                         break;
2440
2441                 case 0x08:
2442
2443                         scp->result = DID_BUS_BUSY << 16 | status;
2444                         break;
2445
2446                 default:
2447
2448                         /*
2449                          * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
2450                          * failed
2451                          */
2452                         if (scp->cmnd[0] == TEST_UNIT_READY) {
2453                                 scp->result = DID_ERROR << 16 |
2454                                         RESERVATION_CONFLICT << 1;
2455                         }
2456                         else
2457                         /*
2458                          * Error code returned is 1 if Reserve or Release
2459                          * failed or the input parameter is invalid
2460                          */
2461                         if (status == 1 && (scp->cmnd[0] == RESERVE ||
2462                                          scp->cmnd[0] == RELEASE)) {
2463
2464                                 scp->result = DID_ERROR << 16 |
2465                                         RESERVATION_CONFLICT << 1;
2466                         }
2467                         else {
2468                                 scp->result = DID_BAD_TARGET << 16 | status;
2469                         }
2470                 }
2471
2472                 // print a debug message for all failed commands
2473                 if (status) {
2474                         megaraid_mbox_display_scb(adapter, scb);
2475                 }
2476
2477                 // Free our internal resources and call the mid-layer callback
2478                 // routine
2479                 megaraid_mbox_sync_scb(adapter, scb);
2480
2481                 // remove from local clist
2482                 list_del_init(&scb->list);
2483
2484                 // put back in free list
2485                 megaraid_dealloc_scb(adapter, scb);
2486
2487                 // send the scsi packet back to kernel
2488                 scp->scsi_done(scp);
2489         }
2490
2491         return;
2492 }
2493
2494
2495 /**
2496  * megaraid_abort_handler - abort the scsi command
2497  * @scp         : command to be aborted
2498  *
2499  * Abort a previous SCSI request. Only commands on the pending list can be
2500  * aborted. All the commands issued to the F/W must complete.
2501  **/
2502 static int
2503 megaraid_abort_handler(struct scsi_cmnd *scp)
2504 {
2505         adapter_t               *adapter;
2506         mraid_device_t          *raid_dev;
2507         scb_t                   *scb;
2508         scb_t                   *tmp;
2509         int                     found;
2510         unsigned long           flags;
2511         int                     i;
2512
2513
2514         adapter         = SCP2ADAPTER(scp);
2515         raid_dev        = ADAP2RAIDDEV(adapter);
2516
2517         con_log(CL_ANN, (KERN_WARNING
2518                 "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2519                 scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
2520                 SCP2TARGET(scp), SCP2LUN(scp)));
2521
2522         // If FW has stopped responding, simply return failure
2523         if (raid_dev->hw_error) {
2524                 con_log(CL_ANN, (KERN_NOTICE
2525                         "megaraid: hw error, not aborting\n"));
2526                 return FAILED;
2527         }
2528
2529         // There might a race here, where the command was completed by the
2530         // firmware and now it is on the completed list. Before we could
2531         // complete the command to the kernel in dpc, the abort came.
2532         // Find out if this is the case to avoid the race.
2533         scb = NULL;
2534         spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
2535         list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
2536
2537                 if (scb->scp == scp) {  // Found command
2538
2539                         list_del_init(&scb->list);      // from completed list
2540
2541                         con_log(CL_ANN, (KERN_WARNING
2542                         "megaraid: %ld:%d[%d:%d], abort from completed list\n",
2543                                 scp->serial_number, scb->sno,
2544                                 scb->dev_channel, scb->dev_target));
2545
2546                         scp->result = (DID_ABORT << 16);
2547                         scp->scsi_done(scp);
2548
2549                         megaraid_dealloc_scb(adapter, scb);
2550
2551                         spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
2552                                 flags);
2553
2554                         return SUCCESS;
2555                 }
2556         }
2557         spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
2558
2559
2560         // Find out if this command is still on the pending list. If it is and
2561         // was never issued, abort and return success. If the command is owned
2562         // by the firmware, we must wait for it to complete by the FW.
2563         spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2564         list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2565
2566                 if (scb->scp == scp) {  // Found command
2567
2568                         list_del_init(&scb->list);      // from pending list
2569
2570                         ASSERT(!(scb->state & SCB_ISSUED));
2571
2572                         con_log(CL_ANN, (KERN_WARNING
2573                                 "megaraid abort: %ld[%d:%d], driver owner\n",
2574                                 scp->serial_number, scb->dev_channel,
2575                                 scb->dev_target));
2576
2577                         scp->result = (DID_ABORT << 16);
2578                         scp->scsi_done(scp);
2579
2580                         megaraid_dealloc_scb(adapter, scb);
2581
2582                         spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
2583                                 flags);
2584
2585                         return SUCCESS;
2586                 }
2587         }
2588         spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2589
2590
2591         // Check do we even own this command, in which case this would be
2592         // owned by the firmware. The only way to locate the FW scb is to
2593         // traverse through the list of all SCB, since driver does not
2594         // maintain these SCBs on any list
2595         found = 0;
2596         spin_lock_irq(&adapter->lock);
2597         for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
2598                 scb = adapter->kscb_list + i;
2599
2600                 if (scb->scp == scp) {
2601
2602                         found = 1;
2603
2604                         if (!(scb->state & SCB_ISSUED)) {
2605                                 con_log(CL_ANN, (KERN_WARNING
2606                                 "megaraid abort: %ld%d[%d:%d], invalid state\n",
2607                                 scp->serial_number, scb->sno, scb->dev_channel,
2608                                 scb->dev_target));
2609                                 BUG();
2610                         }
2611                         else {
2612                                 con_log(CL_ANN, (KERN_WARNING
2613                                 "megaraid abort: %ld:%d[%d:%d], fw owner\n",
2614                                 scp->serial_number, scb->sno, scb->dev_channel,
2615                                 scb->dev_target));
2616                         }
2617                 }
2618         }
2619         spin_unlock_irq(&adapter->lock);
2620
2621         if (!found) {
2622                 con_log(CL_ANN, (KERN_WARNING
2623                         "megaraid abort: scsi cmd:%ld, do now own\n",
2624                         scp->serial_number));
2625
2626                 // FIXME: Should there be a callback for this command?
2627                 return SUCCESS;
2628         }
2629
2630         // We cannot actually abort a command owned by firmware, return
2631         // failure and wait for reset. In host reset handler, we will find out
2632         // if the HBA is still live
2633         return FAILED;
2634 }
2635
2636 /**
2637  * megaraid_reset_handler - device reset hadler for mailbox based driver
2638  * @scp         : reference command
2639  *
2640  * Reset handler for the mailbox based controller. First try to find out if
2641  * the FW is still live, in which case the outstanding commands counter mut go
2642  * down to 0. If that happens, also issue the reservation reset command to
2643  * relinquish (possible) reservations on the logical drives connected to this
2644  * host
2645  **/
2646 static int
2647 megaraid_reset_handler(struct scsi_cmnd *scp)
2648 {
2649         adapter_t       *adapter;
2650         scb_t           *scb;
2651         scb_t           *tmp;
2652         mraid_device_t  *raid_dev;
2653         unsigned long   flags;
2654         uint8_t         raw_mbox[sizeof(mbox_t)];
2655         int             rval;
2656         int             recovery_window;
2657         int             recovering;
2658         int             i;
2659         uioc_t          *kioc;
2660
2661         adapter         = SCP2ADAPTER(scp);
2662         raid_dev        = ADAP2RAIDDEV(adapter);
2663
2664         // return failure if adapter is not responding
2665         if (raid_dev->hw_error) {
2666                 con_log(CL_ANN, (KERN_NOTICE
2667                         "megaraid: hw error, cannot reset\n"));
2668                 return FAILED;
2669         }
2670
2671
2672         // Under exceptional conditions, FW can take up to 3 minutes to
2673         // complete command processing. Wait for additional 2 minutes for the
2674         // pending commands counter to go down to 0. If it doesn't, let the
2675         // controller be marked offline
2676         // Also, reset all the commands currently owned by the driver
2677         spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
2678         list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
2679                 list_del_init(&scb->list);      // from pending list
2680
2681                 if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
2682                         con_log(CL_ANN, (KERN_WARNING
2683                         "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
2684                         scb->sno, scb->dev_channel, scb->dev_target));
2685
2686                         scb->status = -1;
2687
2688                         kioc                    = (uioc_t *)scb->gp;
2689                         kioc->status            = -EFAULT;
2690
2691                         megaraid_mbox_mm_done(adapter, scb);
2692                 } else {
2693                         if (scb->scp == scp) {  // Found command
2694                                 con_log(CL_ANN, (KERN_WARNING
2695                                         "megaraid: %ld:%d[%d:%d], reset from pending list\n",
2696                                         scp->serial_number, scb->sno,
2697                                         scb->dev_channel, scb->dev_target));
2698                         } else {
2699                                 con_log(CL_ANN, (KERN_WARNING
2700                                 "megaraid: IO packet with %d[%d:%d] being reset\n",
2701                                 scb->sno, scb->dev_channel, scb->dev_target));
2702                         }
2703
2704                         scb->scp->result = (DID_RESET << 16);
2705                         scb->scp->scsi_done(scb->scp);
2706
2707                         megaraid_dealloc_scb(adapter, scb);
2708                 }
2709         }
2710         spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
2711
2712         if (adapter->outstanding_cmds) {
2713                 con_log(CL_ANN, (KERN_NOTICE
2714                         "megaraid: %d outstanding commands. Max wait %d sec\n",
2715                         adapter->outstanding_cmds,
2716                         (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
2717         }
2718
2719         recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
2720
2721         recovering = adapter->outstanding_cmds;
2722
2723         for (i = 0; i < recovery_window; i++) {
2724
2725                 megaraid_ack_sequence(adapter);
2726
2727                 // print a message once every 5 seconds only
2728                 if (!(i % 5)) {
2729                         con_log(CL_ANN, (
2730                         "megaraid mbox: Wait for %d commands to complete:%d\n",
2731                                 adapter->outstanding_cmds,
2732                                 (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
2733                 }
2734
2735                 // bailout if no recovery happended in reset time
2736                 if (adapter->outstanding_cmds == 0) {
2737                         break;
2738                 }
2739
2740                 msleep(1000);
2741         }
2742
2743         spin_lock(&adapter->lock);
2744
2745         // If still outstanding commands, bail out
2746         if (adapter->outstanding_cmds) {
2747                 con_log(CL_ANN, (KERN_WARNING
2748                         "megaraid mbox: critical hardware error!\n"));
2749
2750                 raid_dev->hw_error = 1;
2751
2752                 rval = FAILED;
2753                 goto out;
2754         }
2755         else {
2756                 con_log(CL_ANN, (KERN_NOTICE
2757                 "megaraid mbox: reset sequence completed sucessfully\n"));
2758         }
2759
2760
2761         // If the controller supports clustering, reset reservations
2762         if (!adapter->ha) {
2763                 rval = SUCCESS;
2764                 goto out;
2765         }
2766
2767         // clear reservations if any
2768         raw_mbox[0] = CLUSTER_CMD;
2769         raw_mbox[2] = RESET_RESERVATIONS;
2770
2771         rval = SUCCESS;
2772         if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
2773                 con_log(CL_ANN,
2774                         (KERN_INFO "megaraid: reservation reset\n"));
2775         }
2776         else {
2777                 rval = FAILED;
2778                 con_log(CL_ANN, (KERN_WARNING
2779                                 "megaraid: reservation reset failed\n"));
2780         }
2781
2782  out:
2783         spin_unlock_irq(&adapter->lock);
2784         return rval;
2785 }
2786
2787 /*
2788  * START: internal commands library
2789  *
2790  * This section of the driver has the common routine used by the driver and
2791  * also has all the FW routines
2792  */
2793
2794 /**
2795  * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
2796  * @adapter     - controller's soft state
2797  * @raw_mbox    - the mailbox
2798  *
2799  * Issue a scb in synchronous and non-interrupt mode for mailbox based
2800  * controllers
2801  */
2802 static int
2803 mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
2804 {
2805         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
2806         mbox64_t        *mbox64;
2807         mbox_t          *mbox;
2808         uint8_t         status;
2809         int             i;
2810
2811
2812         mbox64  = raid_dev->mbox64;
2813         mbox    = raid_dev->mbox;
2814
2815         /*
2816          * Wait until mailbox is free
2817          */
2818         if (megaraid_busywait_mbox(raid_dev) != 0)
2819                 goto blocked_mailbox;
2820
2821         /*
2822          * Copy mailbox data into host structure
2823          */
2824         memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
2825         mbox->cmdid             = 0xFE;
2826         mbox->busy              = 1;
2827         mbox->poll              = 0;
2828         mbox->ack               = 0;
2829         mbox->numstatus         = 0xFF;
2830         mbox->status            = 0xFF;
2831
2832         wmb();
2833         WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2834
2835         // wait for maximum 1 second for status to post. If the status is not
2836         // available within 1 second, assume FW is initializing and wait
2837         // for an extended amount of time
2838         if (mbox->numstatus == 0xFF) {  // status not yet available
2839                 udelay(25);
2840
2841                 for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
2842                         rmb();
2843                         msleep(1);
2844                 }
2845
2846
2847                 if (i == 1000) {
2848                         con_log(CL_ANN, (KERN_NOTICE
2849                                 "megaraid mailbox: wait for FW to boot      "));
2850
2851                         for (i = 0; (mbox->numstatus == 0xFF) &&
2852                                         (i < MBOX_RESET_WAIT); i++) {
2853                                 rmb();
2854                                 con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
2855                                                         MBOX_RESET_WAIT - i));
2856                                 msleep(1000);
2857                         }
2858
2859                         if (i == MBOX_RESET_WAIT) {
2860
2861                                 con_log(CL_ANN, (
2862                                 "\nmegaraid mailbox: status not available\n"));
2863
2864                                 return -1;
2865                         }
2866                         con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
2867                 }
2868         }
2869
2870         // wait for maximum 1 second for poll semaphore
2871         if (mbox->poll != 0x77) {
2872                 udelay(25);
2873
2874                 for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
2875                         rmb();
2876                         msleep(1);
2877                 }
2878
2879                 if (i == 1000) {
2880                         con_log(CL_ANN, (KERN_WARNING
2881                         "megaraid mailbox: could not get poll semaphore\n"));
2882                         return -1;
2883                 }
2884         }
2885
2886         WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2887         wmb();
2888
2889         // wait for maximum 1 second for acknowledgement
2890         if (RDINDOOR(raid_dev) & 0x2) {
2891                 udelay(25);
2892
2893                 for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
2894                         rmb();
2895                         msleep(1);
2896                 }
2897
2898                 if (i == 1000) {
2899                         con_log(CL_ANN, (KERN_WARNING
2900                                 "megaraid mailbox: could not acknowledge\n"));
2901                         return -1;
2902                 }
2903         }
2904         mbox->poll      = 0;
2905         mbox->ack       = 0x77;
2906
2907         status = mbox->status;
2908
2909         // invalidate the completed command id array. After command
2910         // completion, firmware would write the valid id.
2911         mbox->numstatus = 0xFF;
2912         mbox->status    = 0xFF;
2913         for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
2914                 mbox->completed[i] = 0xFF;
2915         }
2916
2917         return status;
2918
2919 blocked_mailbox:
2920
2921         con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
2922         return -1;
2923 }
2924
2925
2926 /**
2927  * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
2928  * @adapter     - controller's soft state
2929  * @raw_mbox    - the mailbox
2930  *
2931  * Issue a scb in synchronous and non-interrupt mode for mailbox based
2932  * controllers. This is a faster version of the synchronous command and
2933  * therefore can be called in interrupt-context as well
2934  */
2935 static int
2936 mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
2937 {
2938         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
2939         mbox_t          *mbox;
2940         long            i;
2941
2942
2943         mbox    = raid_dev->mbox;
2944
2945         // return immediately if the mailbox is busy
2946         if (mbox->busy) return -1;
2947
2948         // Copy mailbox data into host structure
2949         memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
2950         mbox->cmdid             = 0xFE;
2951         mbox->busy              = 1;
2952         mbox->poll              = 0;
2953         mbox->ack               = 0;
2954         mbox->numstatus         = 0xFF;
2955         mbox->status            = 0xFF;
2956
2957         wmb();
2958         WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
2959
2960         for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
2961                 if (mbox->numstatus != 0xFF) break;
2962                 rmb();
2963                 udelay(MBOX_SYNC_DELAY_200);
2964         }
2965
2966         if (i == MBOX_SYNC_WAIT_CNT) {
2967                 // We may need to re-calibrate the counter
2968                 con_log(CL_ANN, (KERN_CRIT
2969                         "megaraid: fast sync command timed out\n"));
2970         }
2971
2972         WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
2973         wmb();
2974
2975         return mbox->status;
2976 }
2977
2978
2979 /**
2980  * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
2981  * @raid_dev    - RAID device (HBA) soft state
2982  *
2983  * wait until the controller's mailbox is available to accept more commands.
2984  * wait for at most 1 second
2985  */
2986 static int
2987 megaraid_busywait_mbox(mraid_device_t *raid_dev)
2988 {
2989         mbox_t  *mbox = raid_dev->mbox;
2990         int     i = 0;
2991
2992         if (mbox->busy) {
2993                 udelay(25);
2994                 for (i = 0; mbox->busy && i < 1000; i++)
2995                         msleep(1);
2996         }
2997
2998         if (i < 1000) return 0;
2999         else return -1;
3000 }
3001
3002
3003 /**
3004  * megaraid_mbox_product_info - some static information about the controller
3005  * @adapter     - our soft state
3006  *
3007  * issue commands to the controller to grab some parameters required by our
3008  * caller.
3009  */
3010 static int
3011 megaraid_mbox_product_info(adapter_t *adapter)
3012 {
3013         mraid_device_t          *raid_dev = ADAP2RAIDDEV(adapter);
3014         mbox_t                  *mbox;
3015         uint8_t                 raw_mbox[sizeof(mbox_t)];
3016         mraid_pinfo_t           *pinfo;
3017         dma_addr_t              pinfo_dma_h;
3018         mraid_inquiry3_t        *mraid_inq3;
3019         int                     i;
3020
3021
3022         memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3023         mbox = (mbox_t *)raw_mbox;
3024
3025         /*
3026          * Issue an ENQUIRY3 command to find out certain adapter parameters,
3027          * e.g., max channels, max commands etc.
3028          */
3029         pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3030                         &pinfo_dma_h);
3031
3032         if (pinfo == NULL) {
3033                 con_log(CL_ANN, (KERN_WARNING
3034                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
3035                         __LINE__));
3036
3037                 return -1;
3038         }
3039         memset(pinfo, 0, sizeof(mraid_pinfo_t));
3040
3041         mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3042         memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3043
3044         raw_mbox[0] = FC_NEW_CONFIG;
3045         raw_mbox[2] = NC_SUBOP_ENQUIRY3;
3046         raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
3047
3048         // Issue the command
3049         if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3050
3051                 con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
3052
3053                 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3054                         pinfo, pinfo_dma_h);
3055
3056                 return -1;
3057         }
3058
3059         /*
3060          * Collect information about state of each physical drive
3061          * attached to the controller. We will expose all the disks
3062          * which are not part of RAID
3063          */
3064         mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
3065         for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
3066                 raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
3067         }
3068
3069         /*
3070          * Get product info for information like number of channels,
3071          * maximum commands supported.
3072          */
3073         memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3074         mbox->xferaddr = (uint32_t)pinfo_dma_h;
3075
3076         raw_mbox[0] = FC_NEW_CONFIG;
3077         raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
3078
3079         if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3080
3081                 con_log(CL_ANN, (KERN_WARNING
3082                         "megaraid: product info failed\n"));
3083
3084                 pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
3085                         pinfo, pinfo_dma_h);
3086
3087                 return -1;
3088         }
3089
3090         /*
3091          * Setup some parameters for host, as required by our caller
3092          */
3093         adapter->max_channel = pinfo->nchannels;
3094
3095         /*
3096          * we will export all the logical drives on a single channel.
3097          * Add 1 since inquires do not come for inititor ID
3098          */
3099         adapter->max_target     = MAX_LOGICAL_DRIVES_40LD + 1;
3100         adapter->max_lun        = 8;    // up to 8 LUNs for non-disk devices
3101
3102         /*
3103          * These are the maximum outstanding commands for the scsi-layer
3104          */
3105         adapter->max_cmds       = MBOX_MAX_SCSI_CMDS;
3106
3107         memset(adapter->fw_version, 0, VERSION_SIZE);
3108         memset(adapter->bios_version, 0, VERSION_SIZE);
3109
3110         memcpy(adapter->fw_version, pinfo->fw_version, 4);
3111         adapter->fw_version[4] = 0;
3112
3113         memcpy(adapter->bios_version, pinfo->bios_version, 4);
3114         adapter->bios_version[4] = 0;
3115
3116         con_log(CL_ANN, (KERN_NOTICE
3117                 "megaraid: fw version:[%s] bios version:[%s]\n",
3118                 adapter->fw_version, adapter->bios_version));
3119
3120         pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
3121                         pinfo_dma_h);
3122
3123         return 0;
3124 }
3125
3126
3127
3128 /**
3129  * megaraid_mbox_extended_cdb - check for support for extended CDBs
3130  * @adapter     - soft state for the controller
3131  *
3132  * this routine check whether the controller in question supports extended
3133  * ( > 10 bytes ) CDBs
3134  */
3135 static int
3136 megaraid_mbox_extended_cdb(adapter_t *adapter)
3137 {
3138         mbox_t          *mbox;
3139         uint8_t         raw_mbox[sizeof(mbox_t)];
3140         int             rval;
3141
3142         mbox = (mbox_t *)raw_mbox;
3143
3144         memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3145         mbox->xferaddr  = (uint32_t)adapter->ibuf_dma_h;
3146
3147         memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3148
3149         raw_mbox[0] = MAIN_MISC_OPCODE;
3150         raw_mbox[2] = SUPPORT_EXT_CDB;
3151
3152         /*
3153          * Issue the command
3154          */
3155         rval = 0;
3156         if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3157                 rval = -1;
3158         }
3159
3160         return rval;
3161 }
3162
3163
3164 /**
3165  * megaraid_mbox_support_ha - Do we support clustering
3166  * @adapter     - soft state for the controller
3167  * @init_id     - ID of the initiator
3168  *
3169  * Determine if the firmware supports clustering and the ID of the initiator.
3170  */
3171 static int
3172 megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
3173 {
3174         mbox_t          *mbox;
3175         uint8_t         raw_mbox[sizeof(mbox_t)];
3176         int             rval;
3177
3178
3179         mbox = (mbox_t *)raw_mbox;
3180
3181         memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
3182
3183         mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3184
3185         memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3186
3187         raw_mbox[0] = GET_TARGET_ID;
3188
3189         // Issue the command
3190         *init_id = 7;
3191         rval =  -1;
3192         if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3193
3194                 *init_id = *(uint8_t *)adapter->ibuf;
3195
3196                 con_log(CL_ANN, (KERN_INFO
3197                         "megaraid: cluster firmware, initiator ID: %d\n",
3198                         *init_id));
3199
3200                 rval =  0;
3201         }
3202
3203         return rval;
3204 }
3205
3206
3207 /**
3208  * megaraid_mbox_support_random_del - Do we support random deletion
3209  * @adapter     - soft state for the controller
3210  *
3211  * Determine if the firmware supports random deletion
3212  * Return:      1 is operation supported, 0 otherwise
3213  */
3214 static int
3215 megaraid_mbox_support_random_del(adapter_t *adapter)
3216 {
3217         mbox_t          *mbox;
3218         uint8_t         raw_mbox[sizeof(mbox_t)];
3219         int             rval;
3220
3221
3222         mbox = (mbox_t *)raw_mbox;
3223
3224         memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3225
3226         raw_mbox[0] = FC_DEL_LOGDRV;
3227         raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3228
3229         // Issue the command
3230         rval = 0;
3231         if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3232
3233                 con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
3234
3235                 rval =  1;
3236         }
3237
3238         return rval;
3239 }
3240
3241
3242 /**
3243  * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
3244  * @adapter     - soft state for the controller
3245  *
3246  * Find out the maximum number of scatter-gather elements supported by the
3247  * firmware
3248  */
3249 static int
3250 megaraid_mbox_get_max_sg(adapter_t *adapter)
3251 {
3252         mbox_t          *mbox;
3253         uint8_t         raw_mbox[sizeof(mbox_t)];
3254         int             nsg;
3255
3256
3257         mbox = (mbox_t *)raw_mbox;
3258
3259         memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3260
3261         mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3262
3263         memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3264
3265         raw_mbox[0] = MAIN_MISC_OPCODE;
3266         raw_mbox[2] = GET_MAX_SG_SUPPORT;
3267
3268         // Issue the command
3269         if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3270                 nsg =  *(uint8_t *)adapter->ibuf;
3271         }
3272         else {
3273                 nsg =  MBOX_DEFAULT_SG_SIZE;
3274         }
3275
3276         if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
3277
3278         return nsg;
3279 }
3280
3281
3282 /**
3283  * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
3284  * @adapter     - soft state for the controller
3285  *
3286  * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
3287  * can be exported as regular SCSI channels
3288  */
3289 static void
3290 megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
3291 {
3292         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
3293         mbox_t          *mbox;
3294         uint8_t         raw_mbox[sizeof(mbox_t)];
3295
3296
3297         mbox = (mbox_t *)raw_mbox;
3298
3299         memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3300
3301         mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
3302
3303         memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
3304
3305         raw_mbox[0] = CHNL_CLASS;
3306         raw_mbox[2] = GET_CHNL_CLASS;
3307
3308         // Issue the command. If the command fails, all channels are RAID
3309         // channels
3310         raid_dev->channel_class = 0xFF;
3311         if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
3312                 raid_dev->channel_class =  *(uint8_t *)adapter->ibuf;
3313         }
3314
3315         return;
3316 }
3317
3318
3319 /**
3320  * megaraid_mbox_flush_cache - flush adapter and disks cache
3321  * @param adapter       : soft state for the controller
3322  *
3323  * Flush adapter cache followed by disks cache
3324  */
3325 static void
3326 megaraid_mbox_flush_cache(adapter_t *adapter)
3327 {
3328         mbox_t  *mbox;
3329         uint8_t raw_mbox[sizeof(mbox_t)];
3330
3331
3332         mbox = (mbox_t *)raw_mbox;
3333
3334         memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
3335
3336         raw_mbox[0] = FLUSH_ADAPTER;
3337
3338         if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3339                 con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
3340         }
3341
3342         raw_mbox[0] = FLUSH_SYSTEM;
3343
3344         if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
3345                 con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
3346         }
3347
3348         return;
3349 }
3350
3351
3352 /**
3353  * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
3354  * @param adapter       : controllers' soft state
3355  * @param scb           : SCB to be displayed
3356  * @param level : debug level for console print
3357  *
3358  * Diplay information about the given SCB iff the current debug level is
3359  * verbose
3360  */
3361 static void
3362 megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
3363 {
3364         mbox_ccb_t              *ccb;
3365         struct scsi_cmnd        *scp;
3366         mbox_t                  *mbox;
3367         int                     level;
3368         int                     i;
3369
3370
3371         ccb     = (mbox_ccb_t *)scb->ccb;
3372         scp     = scb->scp;
3373         mbox    = ccb->mbox;
3374
3375         level = CL_DLEVEL3;
3376
3377         con_log(level, (KERN_NOTICE
3378                 "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
3379                 mbox->cmd, scb->sno));
3380
3381         con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
3382                 mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
3383                 mbox->numsge));
3384
3385         if (!scp) return;
3386
3387         con_log(level, (KERN_NOTICE "scsi cmnd: "));
3388
3389         for (i = 0; i < scp->cmd_len; i++) {
3390                 con_log(level, ("%#2.02x ", scp->cmnd[i]));
3391         }
3392
3393         con_log(level, ("\n"));
3394
3395         return;
3396 }
3397
3398
3399 /**
3400  * megaraid_mbox_setup_device_map - manage device ids
3401  * @adapter     : Driver's soft state
3402  *
3403  * Manange the device ids to have an appropraite mapping between the kernel
3404  * scsi addresses and megaraid scsi and logical drive addresses. We export
3405  * scsi devices on their actual addresses, whereas the logical drives are
3406  * exported on a virtual scsi channel.
3407  **/
3408 static void
3409 megaraid_mbox_setup_device_map(adapter_t *adapter)
3410 {
3411         uint8_t         c;
3412         uint8_t         t;
3413
3414         /*
3415          * First fill the values on the logical drive channel
3416          */
3417         for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3418                 adapter->device_ids[adapter->max_channel][t] =
3419                         (t < adapter->init_id) ?  t : t - 1;
3420
3421         adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
3422
3423         /*
3424          * Fill the values on the physical devices channels
3425          */
3426         for (c = 0; c < adapter->max_channel; c++)
3427                 for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
3428                         adapter->device_ids[c][t] = (c << 8) | t;
3429 }
3430
3431
3432 /*
3433  * END: internal commands library
3434  */
3435
3436 /*
3437  * START: Interface for the common management module
3438  *
3439  * This is the module, which interfaces with the common mangement module to
3440  * provide support for ioctl and sysfs
3441  */
3442
3443 /**
3444  * megaraid_cmm_register - register with the mangement module
3445  * @param adapter       : HBA soft state
3446  *
3447  * Register with the management module, which allows applications to issue
3448  * ioctl calls to the drivers. This interface is used by the management module
3449  * to setup sysfs support as well.
3450  */
3451 static int
3452 megaraid_cmm_register(adapter_t *adapter)
3453 {
3454         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
3455         mraid_mmadp_t   adp;
3456         scb_t           *scb;
3457         mbox_ccb_t      *ccb;
3458         int             rval;
3459         int             i;
3460
3461         // Allocate memory for the base list of scb for management module.
3462         adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
3463                         GFP_KERNEL);
3464
3465         if (adapter->uscb_list == NULL) {
3466                 con_log(CL_ANN, (KERN_WARNING
3467                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
3468                         __LINE__));
3469                 return -1;
3470         }
3471         memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
3472
3473
3474         // Initialize the synchronization parameters for resources for
3475         // commands for management module
3476         INIT_LIST_HEAD(&adapter->uscb_pool);
3477
3478         spin_lock_init(USER_FREE_LIST_LOCK(adapter));
3479
3480
3481
3482         // link all the packets. Note, CCB for commands, coming from the
3483         // commom management module, mailbox physical address are already
3484         // setup by it. We just need placeholder for that in our local command
3485         // control blocks
3486         for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
3487
3488                 scb                     = adapter->uscb_list + i;
3489                 ccb                     = raid_dev->uccb_list + i;
3490
3491                 scb->ccb                = (caddr_t)ccb;
3492                 ccb->mbox64             = raid_dev->umbox64 + i;
3493                 ccb->mbox               = &ccb->mbox64->mbox32;
3494                 ccb->raw_mbox           = (uint8_t *)ccb->mbox;
3495
3496                 scb->gp                 = 0;
3497
3498                 // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
3499                 // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
3500                 scb->sno                = i + MBOX_MAX_SCSI_CMDS;
3501
3502                 scb->scp                = NULL;
3503                 scb->state              = SCB_FREE;
3504                 scb->dma_direction      = PCI_DMA_NONE;
3505                 scb->dma_type           = MRAID_DMA_NONE;
3506                 scb->dev_channel        = -1;
3507                 scb->dev_target         = -1;
3508
3509                 // put scb in the free pool
3510                 list_add_tail(&scb->list, &adapter->uscb_pool);
3511         }
3512
3513         adp.unique_id           = adapter->unique_id;
3514         adp.drvr_type           = DRVRTYPE_MBOX;
3515         adp.drvr_data           = (unsigned long)adapter;
3516         adp.pdev                = adapter->pdev;
3517         adp.issue_uioc          = megaraid_mbox_mm_handler;
3518         adp.timeout             = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
3519         adp.max_kioc            = MBOX_MAX_USER_CMDS;
3520
3521         if ((rval = mraid_mm_register_adp(&adp)) != 0) {
3522
3523                 con_log(CL_ANN, (KERN_WARNING
3524                         "megaraid mbox: did not register with CMM\n"));
3525
3526                 kfree(adapter->uscb_list);
3527         }
3528
3529         return rval;
3530 }
3531
3532
3533 /**
3534  * megaraid_cmm_unregister - un-register with the mangement module
3535  * @param adapter       : HBA soft state
3536  *
3537  * Un-register with the management module.
3538  * FIXME: mgmt module must return failure for unregister if it has pending
3539  * commands in LLD
3540  */
3541 static int
3542 megaraid_cmm_unregister(adapter_t *adapter)
3543 {
3544         kfree(adapter->uscb_list);
3545         mraid_mm_unregister_adp(adapter->unique_id);
3546         return 0;
3547 }
3548
3549
3550 /**
3551  * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
3552  * @param drvr_data     : LLD specific data
3553  * @param kioc          : CMM interface packet
3554  * @param action        : command action
3555  *
3556  * This routine is invoked whenever the Common Mangement Module (CMM) has a
3557  * command for us. The 'action' parameter specifies if this is a new command
3558  * or otherwise.
3559  */
3560 static int
3561 megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
3562 {
3563         adapter_t *adapter;
3564
3565         if (action != IOCTL_ISSUE) {
3566                 con_log(CL_ANN, (KERN_WARNING
3567                         "megaraid: unsupported management action:%#2x\n",
3568                         action));
3569                 return (-ENOTSUPP);
3570         }
3571
3572         adapter = (adapter_t *)drvr_data;
3573
3574         // make sure this adapter is not being detached right now.
3575         if (atomic_read(&adapter->being_detached)) {
3576                 con_log(CL_ANN, (KERN_WARNING
3577                         "megaraid: reject management request, detaching\n"));
3578                 return (-ENODEV);
3579         }
3580
3581         switch (kioc->opcode) {
3582
3583         case GET_ADAP_INFO:
3584
3585                 kioc->status =  gather_hbainfo(adapter, (mraid_hba_info_t *)
3586                                         (unsigned long)kioc->buf_vaddr);
3587
3588                 kioc->done(kioc);
3589
3590                 return kioc->status;
3591
3592         case MBOX_CMD:
3593
3594                 return megaraid_mbox_mm_command(adapter, kioc);
3595
3596         default:
3597                 kioc->status = (-EINVAL);
3598                 kioc->done(kioc);
3599                 return (-EINVAL);
3600         }
3601
3602         return 0;       // not reached
3603 }
3604
3605 /**
3606  * megaraid_mbox_mm_command - issues commands routed through CMM
3607  * @param adapter       : HBA soft state
3608  * @param kioc          : management command packet
3609  *
3610  * Issues commands, which are routed through the management module.
3611  */
3612 static int
3613 megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
3614 {
3615         struct list_head        *head = &adapter->uscb_pool;
3616         mbox64_t                *mbox64;
3617         uint8_t                 *raw_mbox;
3618         scb_t                   *scb;
3619         mbox_ccb_t              *ccb;
3620         unsigned long           flags;
3621
3622         // detach one scb from free pool
3623         spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3624
3625         if (list_empty(head)) { // should never happen because of CMM
3626
3627                 con_log(CL_ANN, (KERN_WARNING
3628                         "megaraid mbox: bug in cmm handler, lost resources\n"));
3629
3630                 spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3631
3632                 return (-EINVAL);
3633         }
3634
3635         scb = list_entry(head->next, scb_t, list);
3636         list_del_init(&scb->list);
3637
3638         spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3639
3640         scb->state              = SCB_ACTIVE;
3641         scb->dma_type           = MRAID_DMA_NONE;
3642         scb->dma_direction      = PCI_DMA_NONE;
3643
3644         ccb             = (mbox_ccb_t *)scb->ccb;
3645         mbox64          = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3646         raw_mbox        = (uint8_t *)&mbox64->mbox32;
3647
3648         memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
3649
3650         scb->gp         = (unsigned long)kioc;
3651
3652         /*
3653          * If it is a logdrv random delete operation, we have to wait till
3654          * there are no outstanding cmds at the fw and then issue it directly
3655          */
3656         if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3657
3658                 if (wait_till_fw_empty(adapter)) {
3659                         con_log(CL_ANN, (KERN_NOTICE
3660                                 "megaraid mbox: LD delete, timed out\n"));
3661
3662                         kioc->status = -ETIME;
3663
3664                         scb->status = -1;
3665
3666                         megaraid_mbox_mm_done(adapter, scb);
3667
3668                         return (-ETIME);
3669                 }
3670
3671                 INIT_LIST_HEAD(&scb->list);
3672
3673                 scb->state = SCB_ISSUED;
3674                 if (mbox_post_cmd(adapter, scb) != 0) {
3675
3676                         con_log(CL_ANN, (KERN_NOTICE
3677                                 "megaraid mbox: LD delete, mailbox busy\n"));
3678
3679                         kioc->status = -EBUSY;
3680
3681                         scb->status = -1;
3682
3683                         megaraid_mbox_mm_done(adapter, scb);
3684
3685                         return (-EBUSY);
3686                 }
3687
3688                 return 0;
3689         }
3690
3691         // put the command on the pending list and execute
3692         megaraid_mbox_runpendq(adapter, scb);
3693
3694         return 0;
3695 }
3696
3697
3698 static int
3699 wait_till_fw_empty(adapter_t *adapter)
3700 {
3701         unsigned long   flags = 0;
3702         int             i;
3703
3704
3705         /*
3706          * Set the quiescent flag to stop issuing cmds to FW.
3707          */
3708         spin_lock_irqsave(&adapter->lock, flags);
3709         adapter->quiescent++;
3710         spin_unlock_irqrestore(&adapter->lock, flags);
3711
3712         /*
3713          * Wait till there are no more cmds outstanding at FW. Try for at most
3714          * 60 seconds
3715          */
3716         for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
3717                 con_log(CL_DLEVEL1, (KERN_INFO
3718                         "megaraid: FW has %d pending commands\n",
3719                         adapter->outstanding_cmds));
3720
3721                 msleep(1000);
3722         }
3723
3724         return adapter->outstanding_cmds;
3725 }
3726
3727
3728 /**
3729  * megaraid_mbox_mm_done - callback for CMM commands
3730  * @adapter     : HBA soft state
3731  * @scb         : completed command
3732  *
3733  * Callback routine for internal commands originated from the management
3734  * module.
3735  */
3736 static void
3737 megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
3738 {
3739         uioc_t                  *kioc;
3740         mbox64_t                *mbox64;
3741         uint8_t                 *raw_mbox;
3742         unsigned long           flags;
3743
3744         kioc                    = (uioc_t *)scb->gp;
3745         mbox64                  = (mbox64_t *)(unsigned long)kioc->cmdbuf;
3746         mbox64->mbox32.status   = scb->status;
3747         raw_mbox                = (uint8_t *)&mbox64->mbox32;
3748
3749
3750         // put scb in the free pool
3751         scb->state      = SCB_FREE;
3752         scb->scp        = NULL;
3753
3754         spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
3755
3756         list_add(&scb->list, &adapter->uscb_pool);
3757
3758         spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
3759
3760         // if a delete logical drive operation succeeded, restart the
3761         // controller
3762         if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
3763
3764                 adapter->quiescent--;
3765
3766                 megaraid_mbox_runpendq(adapter, NULL);
3767         }
3768
3769         kioc->done(kioc);
3770
3771         return;
3772 }
3773
3774
3775 /**
3776  * gather_hbainfo - HBA characteristics for the applications
3777  * @param adapter       : HBA soft state
3778  * @param hinfo         : pointer to the caller's host info strucuture
3779  */
3780 static int
3781 gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
3782 {
3783         uint8_t dmajor;
3784
3785         dmajor                  = megaraid_mbox_version[0];
3786
3787         hinfo->pci_vendor_id    = adapter->pdev->vendor;
3788         hinfo->pci_device_id    = adapter->pdev->device;
3789         hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
3790         hinfo->subsys_device_id = adapter->pdev->subsystem_device;
3791
3792         hinfo->pci_bus          = adapter->pdev->bus->number;
3793         hinfo->pci_dev_fn       = adapter->pdev->devfn;
3794         hinfo->pci_slot         = PCI_SLOT(adapter->pdev->devfn);
3795         hinfo->irq              = adapter->host->irq;
3796         hinfo->baseport         = ADAP2RAIDDEV(adapter)->baseport;
3797
3798         hinfo->unique_id        = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
3799         hinfo->host_no          = adapter->host->host_no;
3800
3801         return 0;
3802 }
3803
3804 /*
3805  * END: Interface for the common management module
3806  */
3807
3808
3809
3810 /**
3811  * megaraid_sysfs_alloc_resources - allocate sysfs related resources
3812  *
3813  * Allocate packets required to issue FW calls whenever the sysfs attributes
3814  * are read. These attributes would require up-to-date information from the
3815  * FW. Also set up resources for mutual exclusion to share these resources and
3816  * the wait queue.
3817  *
3818  * @param adapter : controller's soft state
3819  *
3820  * @return 0 on success
3821  * @return -ERROR_CODE on failure
3822  */
3823 static int
3824 megaraid_sysfs_alloc_resources(adapter_t *adapter)
3825 {
3826         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
3827         int             rval = 0;
3828
3829         raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
3830
3831         raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
3832
3833         raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
3834                         PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
3835
3836         if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
3837                 !raid_dev->sysfs_buffer) {
3838
3839                 con_log(CL_ANN, (KERN_WARNING
3840                         "megaraid: out of memory, %s %d\n", __FUNCTION__,
3841                         __LINE__));
3842
3843                 rval = -ENOMEM;
3844
3845                 megaraid_sysfs_free_resources(adapter);
3846         }
3847
3848         sema_init(&raid_dev->sysfs_sem, 1);
3849
3850         init_waitqueue_head(&raid_dev->sysfs_wait_q);
3851
3852         return rval;
3853 }
3854
3855
3856 /**
3857  * megaraid_sysfs_free_resources - free sysfs related resources
3858  *
3859  * Free packets allocated for sysfs FW commands
3860  *
3861  * @param adapter : controller's soft state
3862  */
3863 static void
3864 megaraid_sysfs_free_resources(adapter_t *adapter)
3865 {
3866         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
3867
3868         kfree(raid_dev->sysfs_uioc);
3869         kfree(raid_dev->sysfs_mbox64);
3870
3871         if (raid_dev->sysfs_buffer) {
3872                 pci_free_consistent(adapter->pdev, PAGE_SIZE,
3873                         raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
3874         }
3875 }
3876
3877
3878 /**
3879  * megaraid_sysfs_get_ldmap_done - callback for get ldmap
3880  *
3881  * Callback routine called in the ISR/tasklet context for get ldmap call
3882  *
3883  * @param uioc : completed packet
3884  */
3885 static void
3886 megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
3887 {
3888         adapter_t       *adapter = (adapter_t *)uioc->buf_vaddr;
3889         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
3890
3891         uioc->status = 0;
3892
3893         wake_up(&raid_dev->sysfs_wait_q);
3894 }
3895
3896
3897 /**
3898  * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
3899  *
3900  * Timeout routine to recover and return to application, in case the adapter
3901  * has stopped responding. A timeout of 60 seconds for this command seem like
3902  * a good value
3903  *
3904  * @param uioc : timed out packet
3905  */
3906 static void
3907 megaraid_sysfs_get_ldmap_timeout(unsigned long data)
3908 {
3909         uioc_t          *uioc = (uioc_t *)data;
3910         adapter_t       *adapter = (adapter_t *)uioc->buf_vaddr;
3911         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
3912
3913         uioc->status = -ETIME;
3914
3915         wake_up(&raid_dev->sysfs_wait_q);
3916 }
3917
3918
3919 /**
3920  * megaraid_sysfs_get_ldmap - get update logical drive map
3921  *
3922  * This routine will be called whenever user reads the logical drive
3923  * attributes, go get the current logical drive mapping table from the
3924  * firmware. We use the managment API's to issue commands to the controller.
3925  *
3926  * NOTE: The commands issuance functionality is not generalized and
3927  * implemented in context of "get ld map" command only. If required, the
3928  * command issuance logical can be trivially pulled out and implemented as a
3929  * standalone libary. For now, this should suffice since there is no other
3930  * user of this interface.
3931  *
3932  * @param adapter : controller's soft state
3933  *
3934  * @return 0 on success
3935  * @return -1 on failure
3936  */
3937 static int
3938 megaraid_sysfs_get_ldmap(adapter_t *adapter)
3939 {
3940         mraid_device_t          *raid_dev = ADAP2RAIDDEV(adapter);
3941         uioc_t                  *uioc;
3942         mbox64_t                *mbox64;
3943         mbox_t                  *mbox;
3944         char                    *raw_mbox;
3945         struct timer_list       sysfs_timer;
3946         struct timer_list       *timerp;
3947         caddr_t                 ldmap;
3948         int                     rval = 0;
3949
3950         /*
3951          * Allow only one read at a time to go through the sysfs attributes
3952          */
3953         down(&raid_dev->sysfs_sem);
3954
3955         uioc    = raid_dev->sysfs_uioc;
3956         mbox64  = raid_dev->sysfs_mbox64;
3957         ldmap   = raid_dev->sysfs_buffer;
3958
3959         memset(uioc, 0, sizeof(uioc_t));
3960         memset(mbox64, 0, sizeof(mbox64_t));
3961         memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
3962
3963         mbox            = &mbox64->mbox32;
3964         raw_mbox        = (char *)mbox;
3965         uioc->cmdbuf    = (uint64_t)(unsigned long)mbox64;
3966         uioc->buf_vaddr = (caddr_t)adapter;
3967         uioc->status    = -ENODATA;
3968         uioc->done      = megaraid_sysfs_get_ldmap_done;
3969
3970         /*
3971          * Prepare the mailbox packet to get the current logical drive mapping
3972          * table
3973          */
3974         mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
3975
3976         raw_mbox[0] = FC_DEL_LOGDRV;
3977         raw_mbox[2] = OP_GET_LDID_MAP;
3978
3979         /*
3980          * Setup a timer to recover from a non-responding controller
3981          */
3982         timerp  = &sysfs_timer;
3983         init_timer(timerp);
3984
3985         timerp->function        = megaraid_sysfs_get_ldmap_timeout;
3986         timerp->data            = (unsigned long)uioc;
3987         timerp->expires         = jiffies + 60 * HZ;
3988
3989         add_timer(timerp);
3990
3991         /*
3992          * Send the command to the firmware
3993          */
3994         rval = megaraid_mbox_mm_command(adapter, uioc);
3995
3996         if (rval == 0) {        // command successfully issued
3997                 wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
3998
3999                 /*
4000                  * Check if the command timed out
4001                  */
4002                 if (uioc->status == -ETIME) {
4003                         con_log(CL_ANN, (KERN_NOTICE
4004                                 "megaraid: sysfs get ld map timed out\n"));
4005
4006                         rval = -ETIME;
4007                 }
4008                 else {
4009                         rval = mbox->status;
4010                 }
4011
4012                 if (rval == 0) {
4013                         memcpy(raid_dev->curr_ldmap, ldmap,
4014                                 sizeof(raid_dev->curr_ldmap));
4015                 }
4016                 else {
4017                         con_log(CL_ANN, (KERN_NOTICE
4018                                 "megaraid: get ld map failed with %x\n", rval));
4019                 }
4020         }
4021         else {
4022                 con_log(CL_ANN, (KERN_NOTICE
4023                         "megaraid: could not issue ldmap command:%x\n", rval));
4024         }
4025
4026
4027         del_timer_sync(timerp);
4028
4029         up(&raid_dev->sysfs_sem);
4030
4031         return rval;
4032 }
4033
4034
4035 /**
4036  * megaraid_sysfs_show_app_hndl - display application handle for this adapter
4037  *
4038  * Display the handle used by the applications while executing management
4039  * tasks on the adapter. We invoke a management module API to get the adapter
4040  * handle, since we do not interface with applications directly.
4041  *
4042  * @param cdev  : class device object representation for the host
4043  * @param buf   : buffer to send data to
4044  */
4045 static ssize_t
4046 megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
4047 {
4048         struct Scsi_Host *shost = class_to_shost(cdev);
4049         adapter_t       *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
4050         uint32_t        app_hndl;
4051
4052         app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
4053
4054         return snprintf(buf, 8, "%u\n", app_hndl);
4055 }
4056
4057
4058 /**
4059  * megaraid_sysfs_show_ldnum - display the logical drive number for this device
4060  *
4061  * Display the logical drive number for the device in question, if it a valid
4062  * logical drive. For physical devices, "-1" is returned
4063  * The logical drive number is displayed in following format
4064  *
4065  * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
4066  *   <int>     <int>       <int>            <int>
4067  *
4068  * @param dev   : device object representation for the scsi device
4069  * @param buf   : buffer to send data to
4070  */
4071 static ssize_t
4072 megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
4073 {
4074         struct scsi_device *sdev = to_scsi_device(dev);
4075         adapter_t       *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
4076         mraid_device_t  *raid_dev = ADAP2RAIDDEV(adapter);
4077         int             scsi_id = -1;
4078         int             logical_drv = -1;
4079         int             ldid_map = -1;
4080         uint32_t        app_hndl = 0;
4081         int             mapped_sdev_id;
4082         int             rval;
4083         int             i;
4084
4085         if (raid_dev->random_del_supported &&
4086                         MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
4087
4088                 rval = megaraid_sysfs_get_ldmap(adapter);
4089                 if (rval == 0) {
4090
4091                         for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
4092
4093                                 mapped_sdev_id = sdev->id;
4094
4095                                 if (sdev->id > adapter->init_id) {
4096                                         mapped_sdev_id -= 1;
4097                                 }
4098
4099                                 if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
4100
4101                                         scsi_id = sdev->id;
4102
4103                                         logical_drv = i;
4104
4105                                         ldid_map = raid_dev->curr_ldmap[i];
4106
4107                                         app_hndl = mraid_mm_adapter_app_handle(
4108                                                         adapter->unique_id);
4109
4110                                         break;
4111                                 }
4112                         }
4113                 }
4114                 else {
4115                         con_log(CL_ANN, (KERN_NOTICE
4116                                 "megaraid: sysfs get ld map failed: %x\n",
4117                                 rval));
4118                 }
4119         }
4120
4121         return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
4122                         ldid_map, app_hndl);
4123 }
4124
4125
4126 /*
4127  * END: Mailbox Low Level Driver
4128  */
4129 module_init(megaraid_init);
4130 module_exit(megaraid_exit);
4131
4132 /* vim: set ts=8 sw=8 tw=78 ai si: */