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