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