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