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