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