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