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