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