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