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