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