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