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