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