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