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