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