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