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