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