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