xref: /linux/drivers/scsi/megaraid.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *			Linux MegaRAID device driver
51da177e4SLinus Torvalds  *
63492b328SJu, Seokmann  * Copyright (c) 2002  LSI Logic Corporation.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
91da177e4SLinus Torvalds  *	  - fixes
101da177e4SLinus Torvalds  *	  - speed-ups (list handling fixes, issued_list, optimizations.)
111da177e4SLinus Torvalds  *	  - lots of cleanups.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Copyright (c) 2003  Christoph Hellwig  <hch@lst.de>
141da177e4SLinus Torvalds  *	  - new-style, hotplug-aware pci probing and scsi registration
151da177e4SLinus Torvalds  *
163492b328SJu, Seokmann  * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
173492b328SJu, Seokmann  * 						<Seokmann.Ju@lsil.com>
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Description: Linux device driver for LSI Logic MegaRAID controller
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
221da177e4SLinus Torvalds  *					518, 520, 531, 532
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
251da177e4SLinus Torvalds  * and others. Please send updates to the mailing list
261da177e4SLinus Torvalds  * linux-scsi@vger.kernel.org .
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/fs.h>
311da177e4SLinus Torvalds #include <linux/blkdev.h>
327c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
331da177e4SLinus Torvalds #include <asm/io.h>
348d115f84SChristoph Hellwig #include <linux/completion.h>
351da177e4SLinus Torvalds #include <linux/delay.h>
361da177e4SLinus Torvalds #include <linux/proc_fs.h>
37c7f079caSDavid Howells #include <linux/seq_file.h>
381da177e4SLinus Torvalds #include <linux/reboot.h>
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/list.h>
411da177e4SLinus Torvalds #include <linux/interrupt.h>
421da177e4SLinus Torvalds #include <linux/pci.h>
431da177e4SLinus Torvalds #include <linux/init.h>
44910638aeSMatthias Gehre #include <linux/dma-mapping.h>
45c45d15d2SArnd Bergmann #include <linux/mutex.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
471da177e4SLinus Torvalds 
4853555fb7SBart Van Assche #include <scsi/scsi.h>
4953555fb7SBart Van Assche #include <scsi/scsi_cmnd.h>
5053555fb7SBart Van Assche #include <scsi/scsi_device.h>
5153555fb7SBart Van Assche #include <scsi/scsi_eh.h>
521da177e4SLinus Torvalds #include <scsi/scsi_host.h>
5353555fb7SBart Van Assche #include <scsi/scsi_tcq.h>
5453555fb7SBart Van Assche #include <scsi/scsicam.h>
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include "megaraid.h"
571da177e4SLinus Torvalds 
583492b328SJu, Seokmann #define MEGARAID_MODULE_VERSION "2.00.4"
591da177e4SLinus Torvalds 
603492b328SJu, Seokmann MODULE_AUTHOR ("sju@lsil.com");
613492b328SJu, Seokmann MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
621da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
631da177e4SLinus Torvalds MODULE_VERSION(MEGARAID_MODULE_VERSION);
641da177e4SLinus Torvalds 
65c45d15d2SArnd Bergmann static DEFINE_MUTEX(megadev_mutex);
661da177e4SLinus Torvalds static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
671da177e4SLinus Torvalds module_param(max_cmd_per_lun, uint, 0);
681da177e4SLinus Torvalds MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
711da177e4SLinus Torvalds module_param(max_sectors_per_io, ushort, 0);
721da177e4SLinus Torvalds MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
761da177e4SLinus Torvalds module_param(max_mbox_busy_wait, ushort, 0);
771da177e4SLinus Torvalds MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
781da177e4SLinus Torvalds 
7900769ec4SJeff Garzik #define RDINDOOR(adapter)	readl((adapter)->mmio_base + 0x20)
8000769ec4SJeff Garzik #define RDOUTDOOR(adapter)	readl((adapter)->mmio_base + 0x2C)
8100769ec4SJeff Garzik #define WRINDOOR(adapter,value)	 writel(value, (adapter)->mmio_base + 0x20)
8200769ec4SJeff Garzik #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /*
851da177e4SLinus Torvalds  * Global variables
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static int hba_count;
891da177e4SLinus Torvalds static adapter_t *hba_soft_state[MAX_CONTROLLERS];
901da177e4SLinus Torvalds static struct proc_dir_entry *mega_proc_dir_entry;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* For controller re-ordering */
931da177e4SLinus Torvalds static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
941da177e4SLinus Torvalds 
95f4927c45SArnd Bergmann static long
96f4927c45SArnd Bergmann megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
97f4927c45SArnd Bergmann 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * The File Operations structure for the serial/ioctl interface of the driver
1001da177e4SLinus Torvalds  */
10100977a59SArjan van de Ven static const struct file_operations megadev_fops = {
1021da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
103f4927c45SArnd Bergmann 	.unlocked_ioctl	= megadev_unlocked_ioctl,
1041da177e4SLinus Torvalds 	.open		= megadev_open,
1056038f373SArnd Bergmann 	.llseek		= noop_llseek,
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * Array to structures for storing the information about the controllers. This
1101da177e4SLinus Torvalds  * information is sent to the user level applications, when they do an ioctl
1111da177e4SLinus Torvalds  * for this information.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds static struct mcontroller mcontroller[MAX_CONTROLLERS];
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* The current driver version */
1161da177e4SLinus Torvalds static u32 driver_ver = 0x02000000;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /* major number used by the device for character interface */
1191da177e4SLinus Torvalds static int major;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define IS_RAID_CH(hba, ch)	(((hba)->mega_ch_class >> (ch)) & 0x01)
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /*
1251da177e4SLinus Torvalds  * Debug variable to print some diagnostic messages
1261da177e4SLinus Torvalds  */
1271da177e4SLinus Torvalds static int trace_level;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /**
1301da177e4SLinus Torvalds  * mega_setup_mailbox()
1312b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  * Allocates a 8 byte aligned memory for the handshake mailbox.
1341da177e4SLinus Torvalds  */
1351da177e4SLinus Torvalds static int
mega_setup_mailbox(adapter_t * adapter)1361da177e4SLinus Torvalds mega_setup_mailbox(adapter_t *adapter)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	unsigned long	align;
1391da177e4SLinus Torvalds 
140ec090ef8SSuraj Upadhyay 	adapter->una_mbox64 = dma_alloc_coherent(&adapter->dev->dev,
141ec090ef8SSuraj Upadhyay 						 sizeof(mbox64_t),
142ec090ef8SSuraj Upadhyay 						 &adapter->una_mbox64_dma,
143ec090ef8SSuraj Upadhyay 						 GFP_KERNEL);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	if( !adapter->una_mbox64 ) return -1;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	adapter->mbox = &adapter->una_mbox64->mbox;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
1501da177e4SLinus Torvalds 			(~0UL ^ 0xFUL));
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/*
1591da177e4SLinus Torvalds 	 * Register the mailbox if the controller is an io-mapped controller
1601da177e4SLinus Torvalds 	 */
1611da177e4SLinus Torvalds 	if( adapter->flag & BOARD_IOMAP ) {
1621da177e4SLinus Torvalds 
1637d1abbe8SAlan Cox 		outb(adapter->mbox_dma & 0xFF,
1641da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT0);
1651da177e4SLinus Torvalds 
1667d1abbe8SAlan Cox 		outb((adapter->mbox_dma >> 8) & 0xFF,
1671da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT1);
1681da177e4SLinus Torvalds 
1697d1abbe8SAlan Cox 		outb((adapter->mbox_dma >> 16) & 0xFF,
1701da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT2);
1711da177e4SLinus Torvalds 
1727d1abbe8SAlan Cox 		outb((adapter->mbox_dma >> 24) & 0xFF,
1731da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT3);
1741da177e4SLinus Torvalds 
1757d1abbe8SAlan Cox 		outb(ENABLE_MBOX_BYTE,
1761da177e4SLinus Torvalds 				adapter->host->io_port + ENABLE_MBOX_REGION);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 		irq_ack(adapter);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		irq_enable(adapter);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * mega_query_adapter()
1891da177e4SLinus Torvalds  * @adapter - pointer to our soft state
1901da177e4SLinus Torvalds  *
1911da177e4SLinus Torvalds  * Issue the adapter inquiry commands to the controller and find out
1921da177e4SLinus Torvalds  * information and parameter about the devices attached
1931da177e4SLinus Torvalds  */
1941da177e4SLinus Torvalds static int
mega_query_adapter(adapter_t * adapter)1951da177e4SLinus Torvalds mega_query_adapter(adapter_t *adapter)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	dma_addr_t	prod_info_dma_handle;
1981da177e4SLinus Torvalds 	mega_inquiry3	*inquiry3;
199315d049aSKees Cook 	struct mbox_out	mbox;
200315d049aSKees Cook 	u8	*raw_mbox = (u8 *)&mbox;
2011da177e4SLinus Torvalds 	int	retval;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	/* Initialize adapter inquiry mailbox */
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
206315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/*
2091da177e4SLinus Torvalds 	 * Try to issue Inquiry3 command
2101da177e4SLinus Torvalds 	 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
2111da177e4SLinus Torvalds 	 * update enquiry3 structure
2121da177e4SLinus Torvalds 	 */
213315d049aSKees Cook 	mbox.xferaddr = (u32)adapter->buf_dma_handle;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	raw_mbox[0] = FC_NEW_CONFIG;		/* i.e. mbox->cmd=0xA1 */
2181da177e4SLinus Torvalds 	raw_mbox[2] = NC_SUBOP_ENQUIRY3;	/* i.e. 0x0F */
2191da177e4SLinus Torvalds 	raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;	/* i.e. 0x02 */
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/* Issue a blocking command to the card */
222*9759cdc1SColin Ian King 	if (issue_scb_block(adapter, raw_mbox)) {
2231da177e4SLinus Torvalds 		/* the adapter does not support 40ld */
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 		mraid_ext_inquiry	*ext_inq;
2261da177e4SLinus Torvalds 		mraid_inquiry		*inq;
2271da177e4SLinus Torvalds 		dma_addr_t		dma_handle;
2281da177e4SLinus Torvalds 
229ec090ef8SSuraj Upadhyay 		ext_inq = dma_alloc_coherent(&adapter->dev->dev,
230ec090ef8SSuraj Upadhyay 					     sizeof(mraid_ext_inquiry),
231ec090ef8SSuraj Upadhyay 					     &dma_handle, GFP_KERNEL);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 		if( ext_inq == NULL ) return -1;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 		inq = &ext_inq->raid_inq;
2361da177e4SLinus Torvalds 
237315d049aSKees Cook 		mbox.xferaddr = (u32)dma_handle;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 		/*issue old 0x04 command to adapter */
240315d049aSKees Cook 		mbox.cmd = MEGA_MBOXCMD_ADPEXTINQ;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 		issue_scb_block(adapter, raw_mbox);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 		/*
2451da177e4SLinus Torvalds 		 * update Enquiry3 and ProductInfo structures with
2461da177e4SLinus Torvalds 		 * mraid_inquiry structure
2471da177e4SLinus Torvalds 		 */
2481da177e4SLinus Torvalds 		mega_8_to_40ld(inq, inquiry3,
2491da177e4SLinus Torvalds 				(mega_product_info *)&adapter->product_info);
2501da177e4SLinus Torvalds 
251ec090ef8SSuraj Upadhyay 		dma_free_coherent(&adapter->dev->dev,
252ec090ef8SSuraj Upadhyay 				  sizeof(mraid_ext_inquiry), ext_inq,
253ec090ef8SSuraj Upadhyay 				  dma_handle);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	} else {		/*adapter supports 40ld */
2561da177e4SLinus Torvalds 		adapter->flag |= BOARD_40LD;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 		/*
2591da177e4SLinus Torvalds 		 * get product_info, which is static information and will be
2601da177e4SLinus Torvalds 		 * unchanged
2611da177e4SLinus Torvalds 		 */
262ec090ef8SSuraj Upadhyay 		prod_info_dma_handle = dma_map_single(&adapter->dev->dev,
263ec090ef8SSuraj Upadhyay 						      (void *)&adapter->product_info,
264ec090ef8SSuraj Upadhyay 						      sizeof(mega_product_info),
265ec090ef8SSuraj Upadhyay 						      DMA_FROM_DEVICE);
2661da177e4SLinus Torvalds 
267315d049aSKees Cook 		mbox.xferaddr = prod_info_dma_handle;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		raw_mbox[0] = FC_NEW_CONFIG;	/* i.e. mbox->cmd=0xA1 */
2701da177e4SLinus Torvalds 		raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;	/* i.e. 0x0E */
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 		if ((retval = issue_scb_block(adapter, raw_mbox)))
2733b8a1ba3SBjorn Helgaas 			dev_warn(&adapter->dev->dev,
2743b8a1ba3SBjorn Helgaas 				"Product_info cmd failed with error: %d\n",
2751da177e4SLinus Torvalds 				retval);
2761da177e4SLinus Torvalds 
277ec090ef8SSuraj Upadhyay 		dma_unmap_single(&adapter->dev->dev, prod_info_dma_handle,
278ec090ef8SSuraj Upadhyay 				 sizeof(mega_product_info), DMA_FROM_DEVICE);
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/*
2831da177e4SLinus Torvalds 	 * kernel scans the channels from 0 to <= max_channel
2841da177e4SLinus Torvalds 	 */
2851da177e4SLinus Torvalds 	adapter->host->max_channel =
2861da177e4SLinus Torvalds 		adapter->product_info.nchannels + NVIRT_CHAN -1;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	adapter->host->max_id = 16;	/* max targets per channel */
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	adapter->host->max_lun = 7;	/* Up to 7 luns for non disk devices */
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	adapter->host->cmd_per_lun = max_cmd_per_lun;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	adapter->numldrv = inquiry3->num_ldrv;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	adapter->max_cmds = adapter->product_info.max_commands;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if(adapter->max_cmds > MAX_COMMANDS)
2991da177e4SLinus Torvalds 		adapter->max_cmds = MAX_COMMANDS;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	adapter->host->can_queue = adapter->max_cmds - 1;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/*
3041da177e4SLinus Torvalds 	 * Get the maximum number of scatter-gather elements supported by this
3051da177e4SLinus Torvalds 	 * firmware
3061da177e4SLinus Torvalds 	 */
3071da177e4SLinus Torvalds 	mega_get_max_sgl(adapter);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	adapter->host->sg_tablesize = adapter->sglen;
3101da177e4SLinus Torvalds 
311124dd90fSadam radford 	/* use HP firmware and bios version encoding
312124dd90fSadam radford 	   Note: fw_version[0|1] and bios_version[0|1] were originally shifted
313124dd90fSadam radford 	   right 8 bits making them zero. This 0 value was hardcoded to fix
314124dd90fSadam radford 	   sparse warnings. */
31554ebfd57SJon Mason 	if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
316875826a7SArnd Bergmann 		snprintf(adapter->fw_version, sizeof(adapter->fw_version),
317875826a7SArnd Bergmann 			 "%c%d%d.%d%d",
3181da177e4SLinus Torvalds 			 adapter->product_info.fw_version[2],
319124dd90fSadam radford 			 0,
3201da177e4SLinus Torvalds 			 adapter->product_info.fw_version[1] & 0x0f,
321124dd90fSadam radford 			 0,
3221da177e4SLinus Torvalds 			 adapter->product_info.fw_version[0] & 0x0f);
323875826a7SArnd Bergmann 		snprintf(adapter->bios_version, sizeof(adapter->fw_version),
324875826a7SArnd Bergmann 			 "%c%d%d.%d%d",
3251da177e4SLinus Torvalds 			 adapter->product_info.bios_version[2],
326124dd90fSadam radford 			 0,
3271da177e4SLinus Torvalds 			 adapter->product_info.bios_version[1] & 0x0f,
328124dd90fSadam radford 			 0,
3291da177e4SLinus Torvalds 			 adapter->product_info.bios_version[0] & 0x0f);
3301da177e4SLinus Torvalds 	} else {
3311da177e4SLinus Torvalds 		memcpy(adapter->fw_version,
3321da177e4SLinus Torvalds 				(char *)adapter->product_info.fw_version, 4);
3331da177e4SLinus Torvalds 		adapter->fw_version[4] = 0;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 		memcpy(adapter->bios_version,
3361da177e4SLinus Torvalds 				(char *)adapter->product_info.bios_version, 4);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		adapter->bios_version[4] = 0;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
3413b8a1ba3SBjorn Helgaas 	dev_notice(&adapter->dev->dev, "[%s:%s] detected %d logical drives\n",
3421da177e4SLinus Torvalds 		adapter->fw_version, adapter->bios_version, adapter->numldrv);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/*
3451da177e4SLinus Torvalds 	 * Do we support extended (>10 bytes) cdbs
3461da177e4SLinus Torvalds 	 */
3471da177e4SLinus Torvalds 	adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
3481da177e4SLinus Torvalds 	if (adapter->support_ext_cdb)
3493b8a1ba3SBjorn Helgaas 		dev_notice(&adapter->dev->dev, "supports extended CDBs\n");
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	return 0;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds /**
3561da177e4SLinus Torvalds  * mega_runpendq()
3572b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
3581da177e4SLinus Torvalds  *
3591da177e4SLinus Torvalds  * Runs through the list of pending requests.
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds static inline void
mega_runpendq(adapter_t * adapter)3621da177e4SLinus Torvalds mega_runpendq(adapter_t *adapter)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	if(!list_empty(&adapter->pending_list))
3651da177e4SLinus Torvalds 		__mega_runpendq(adapter);
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /*
3691da177e4SLinus Torvalds  * megaraid_queue()
3701da177e4SLinus Torvalds  * @scmd - Issue this scsi command
3711da177e4SLinus Torvalds  * @done - the callback hook into the scsi mid-layer
3721da177e4SLinus Torvalds  *
3731da177e4SLinus Torvalds  * The command queuing entry point for the mid-layer.
3741da177e4SLinus Torvalds  */
megaraid_queue_lck(struct scsi_cmnd * scmd)375af049dfdSBart Van Assche static int megaraid_queue_lck(struct scsi_cmnd *scmd)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	adapter_t	*adapter;
3781da177e4SLinus Torvalds 	scb_t	*scb;
3791da177e4SLinus Torvalds 	int	busy=0;
380cb0258a2SChristoph Hellwig 	unsigned long flags;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	adapter = (adapter_t *)scmd->device->host->hostdata;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/*
3851da177e4SLinus Torvalds 	 * Allocate and build a SCB request
3861da177e4SLinus Torvalds 	 * busy flag will be set if mega_build_cmd() command could not
3871da177e4SLinus Torvalds 	 * allocate scb. We will return non-zero status in that case.
3881da177e4SLinus Torvalds 	 * NOTE: scb can be null even though certain commands completed
3891da177e4SLinus Torvalds 	 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
3901da177e4SLinus Torvalds 	 * return 0 in that case.
3911da177e4SLinus Torvalds 	 */
3921da177e4SLinus Torvalds 
393cb0258a2SChristoph Hellwig 	spin_lock_irqsave(&adapter->lock, flags);
3941da177e4SLinus Torvalds 	scb = mega_build_cmd(adapter, scmd, &busy);
395238f9b06SChristoph Hellwig 	if (!scb)
396238f9b06SChristoph Hellwig 		goto out;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	scb->state |= SCB_PENDQ;
3991da177e4SLinus Torvalds 	list_add_tail(&scb->list, &adapter->pending_list);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/*
4021da177e4SLinus Torvalds 	 * Check if the HBA is in quiescent state, e.g., during a
4031da177e4SLinus Torvalds 	 * delete logical drive opertion. If it is, don't run
4041da177e4SLinus Torvalds 	 * the pending_list.
4051da177e4SLinus Torvalds 	 */
406238f9b06SChristoph Hellwig 	if (atomic_read(&adapter->quiescent) == 0)
4071da177e4SLinus Torvalds 		mega_runpendq(adapter);
4081da177e4SLinus Torvalds 
409238f9b06SChristoph Hellwig 	busy = 0;
410238f9b06SChristoph Hellwig  out:
411238f9b06SChristoph Hellwig 	spin_unlock_irqrestore(&adapter->lock, flags);
4121da177e4SLinus Torvalds 	return busy;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
DEF_SCSI_QCMD(megaraid_queue)415f281233dSJeff Garzik static DEF_SCSI_QCMD(megaraid_queue)
416f281233dSJeff Garzik 
4171da177e4SLinus Torvalds /**
4181da177e4SLinus Torvalds  * mega_allocate_scb()
4192b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
4202b46e5c1SDamien Le Moal  * @cmd: scsi command from the mid-layer
4211da177e4SLinus Torvalds  *
4221da177e4SLinus Torvalds  * Allocate a SCB structure. This is the central structure for controller
4231da177e4SLinus Torvalds  * commands.
4241da177e4SLinus Torvalds  */
4251da177e4SLinus Torvalds static inline scb_t *
42691ebc1faSJohannes Thumshirn mega_allocate_scb(adapter_t *adapter, struct scsi_cmnd *cmd)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct list_head *head = &adapter->free_list;
4291da177e4SLinus Torvalds 	scb_t	*scb;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/* Unlink command from Free List */
4321da177e4SLinus Torvalds 	if( !list_empty(head) ) {
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 		scb = list_entry(head->next, scb_t, list);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 		list_del_init(head->next);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 		scb->state = SCB_ACTIVE;
4391da177e4SLinus Torvalds 		scb->cmd = cmd;
4401da177e4SLinus Torvalds 		scb->dma_type = MEGA_DMA_TYPE_NONE;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 		return scb;
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	return NULL;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /**
4491da177e4SLinus Torvalds  * mega_get_ldrv_num()
4502b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
4512b46e5c1SDamien Le Moal  * @cmd: scsi mid layer command
4522b46e5c1SDamien Le Moal  * @channel: channel on the controller
4531da177e4SLinus Torvalds  *
4541da177e4SLinus Torvalds  * Calculate the logical drive number based on the information in scsi command
4551da177e4SLinus Torvalds  * and the channel number.
4561da177e4SLinus Torvalds  */
4571da177e4SLinus Torvalds static inline int
mega_get_ldrv_num(adapter_t * adapter,struct scsi_cmnd * cmd,int channel)45891ebc1faSJohannes Thumshirn mega_get_ldrv_num(adapter_t *adapter, struct scsi_cmnd *cmd, int channel)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	int		tgt;
4611da177e4SLinus Torvalds 	int		ldrv_num;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	tgt = cmd->device->id;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	if ( tgt > adapter->this_id )
4661da177e4SLinus Torvalds 		tgt--;	/* we do not get inquires for initiator id */
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	ldrv_num = (channel * 15) + tgt;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	/*
4721da177e4SLinus Torvalds 	 * If we have a logical drive with boot enabled, project it first
4731da177e4SLinus Torvalds 	 */
4741da177e4SLinus Torvalds 	if( adapter->boot_ldrv_enabled ) {
4751da177e4SLinus Torvalds 		if( ldrv_num == 0 ) {
4761da177e4SLinus Torvalds 			ldrv_num = adapter->boot_ldrv;
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 		else {
4791da177e4SLinus Torvalds 			if( ldrv_num <= adapter->boot_ldrv ) {
4801da177e4SLinus Torvalds 				ldrv_num--;
4811da177e4SLinus Torvalds 			}
4821da177e4SLinus Torvalds 		}
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/*
4861da177e4SLinus Torvalds 	 * If "delete logical drive" feature is enabled on this controller.
4871da177e4SLinus Torvalds 	 * Do only if at least one delete logical drive operation was done.
4881da177e4SLinus Torvalds 	 *
4891da177e4SLinus Torvalds 	 * Also, after logical drive deletion, instead of logical drive number,
4901da177e4SLinus Torvalds 	 * the value returned should be 0x80+logical drive id.
4911da177e4SLinus Torvalds 	 *
4921da177e4SLinus Torvalds 	 * These is valid only for IO commands.
4931da177e4SLinus Torvalds 	 */
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (adapter->support_random_del && adapter->read_ldidmap )
4961da177e4SLinus Torvalds 		switch (cmd->cmnd[0]) {
497df561f66SGustavo A. R. Silva 		case READ_6:
498df561f66SGustavo A. R. Silva 		case WRITE_6:
499df561f66SGustavo A. R. Silva 		case READ_10:
5001da177e4SLinus Torvalds 		case WRITE_10:
5011da177e4SLinus Torvalds 			ldrv_num += 0x80;
5021da177e4SLinus Torvalds 		}
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	return ldrv_num;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /**
5081da177e4SLinus Torvalds  * mega_build_cmd()
5092b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
5102b46e5c1SDamien Le Moal  * @cmd: Prepare using this scsi command
5112b46e5c1SDamien Le Moal  * @busy: busy flag if no resources
5121da177e4SLinus Torvalds  *
5131da177e4SLinus Torvalds  * Prepares a command and scatter gather list for the controller. This routine
5141da177e4SLinus Torvalds  * also finds out if the commands is intended for a logical drive or a
5151da177e4SLinus Torvalds  * physical device and prepares the controller command accordingly.
5161da177e4SLinus Torvalds  *
5171da177e4SLinus Torvalds  * We also re-order the logical drives and physical devices based on their
5181da177e4SLinus Torvalds  * boot settings.
5191da177e4SLinus Torvalds  */
5201da177e4SLinus Torvalds static scb_t *
mega_build_cmd(adapter_t * adapter,struct scsi_cmnd * cmd,int * busy)52191ebc1faSJohannes Thumshirn mega_build_cmd(adapter_t *adapter, struct scsi_cmnd *cmd, int *busy)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	mega_passthru	*pthru;
5241da177e4SLinus Torvalds 	scb_t	*scb;
5251da177e4SLinus Torvalds 	mbox_t	*mbox;
5269d5d93e3SDan Carpenter 	u32	seg;
5271da177e4SLinus Torvalds 	char	islogical;
5281da177e4SLinus Torvalds 	int	max_ldrv_num;
5291da177e4SLinus Torvalds 	int	channel = 0;
5301da177e4SLinus Torvalds 	int	target = 0;
5311da177e4SLinus Torvalds 	int	ldrv_num = 0;   /* logical drive number */
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/*
5341da177e4SLinus Torvalds 	 * We know what channels our logical drives are on - mega_find_card()
5351da177e4SLinus Torvalds 	 */
5361da177e4SLinus Torvalds 	islogical = adapter->logdrv_chan[cmd->device->channel];
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	/*
5391da177e4SLinus Torvalds 	 * The theory: If physical drive is chosen for boot, all the physical
5401da177e4SLinus Torvalds 	 * devices are exported before the logical drives, otherwise physical
5411da177e4SLinus Torvalds 	 * devices are pushed after logical drives, in which case - Kernel sees
5421da177e4SLinus Torvalds 	 * the physical devices on virtual channel which is obviously converted
5431da177e4SLinus Torvalds 	 * to actual channel on the HBA.
5441da177e4SLinus Torvalds 	 */
5451da177e4SLinus Torvalds 	if( adapter->boot_pdrv_enabled ) {
5461da177e4SLinus Torvalds 		if( islogical ) {
5471da177e4SLinus Torvalds 			/* logical channel */
5481da177e4SLinus Torvalds 			channel = cmd->device->channel -
5491da177e4SLinus Torvalds 				adapter->product_info.nchannels;
5501da177e4SLinus Torvalds 		}
5511da177e4SLinus Torvalds 		else {
5521da177e4SLinus Torvalds 			/* this is physical channel */
5531da177e4SLinus Torvalds 			channel = cmd->device->channel;
5541da177e4SLinus Torvalds 			target = cmd->device->id;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 			/*
5571da177e4SLinus Torvalds 			 * boot from a physical disk, that disk needs to be
5581da177e4SLinus Torvalds 			 * exposed first IF both the channels are SCSI, then
5591da177e4SLinus Torvalds 			 * booting from the second channel is not allowed.
5601da177e4SLinus Torvalds 			 */
5611da177e4SLinus Torvalds 			if( target == 0 ) {
5621da177e4SLinus Torvalds 				target = adapter->boot_pdrv_tgt;
5631da177e4SLinus Torvalds 			}
5641da177e4SLinus Torvalds 			else if( target == adapter->boot_pdrv_tgt ) {
5651da177e4SLinus Torvalds 				target = 0;
5661da177e4SLinus Torvalds 			}
5671da177e4SLinus Torvalds 		}
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	else {
5701da177e4SLinus Torvalds 		if( islogical ) {
5711da177e4SLinus Torvalds 			/* this is the logical channel */
5721da177e4SLinus Torvalds 			channel = cmd->device->channel;
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 		else {
5751da177e4SLinus Torvalds 			/* physical channel */
5761da177e4SLinus Torvalds 			channel = cmd->device->channel - NVIRT_CHAN;
5771da177e4SLinus Torvalds 			target = cmd->device->id;
5781da177e4SLinus Torvalds 		}
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	if(islogical) {
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 		/* have just LUN 0 for each target on virtual channels */
5851da177e4SLinus Torvalds 		if (cmd->device->lun) {
5861da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
5879e060365SBart Van Assche 			scsi_done(cmd);
5881da177e4SLinus Torvalds 			return NULL;
5891da177e4SLinus Torvalds 		}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		max_ldrv_num = (adapter->flag & BOARD_40LD) ?
5951da177e4SLinus Torvalds 			MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 		/*
5981da177e4SLinus Torvalds 		 * max_ldrv_num increases by 0x80 if some logical drive was
5991da177e4SLinus Torvalds 		 * deleted.
6001da177e4SLinus Torvalds 		 */
6011da177e4SLinus Torvalds 		if(adapter->read_ldidmap)
6021da177e4SLinus Torvalds 			max_ldrv_num += 0x80;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 		if(ldrv_num > max_ldrv_num ) {
6051da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
6069e060365SBart Van Assche 			scsi_done(cmd);
6071da177e4SLinus Torvalds 			return NULL;
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 	else {
6121da177e4SLinus Torvalds 		if( cmd->device->lun > 7) {
6131da177e4SLinus Torvalds 			/*
6141da177e4SLinus Torvalds 			 * Do not support lun >7 for physically accessed
6151da177e4SLinus Torvalds 			 * devices
6161da177e4SLinus Torvalds 			 */
6171da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
6189e060365SBart Van Assche 			scsi_done(cmd);
6191da177e4SLinus Torvalds 			return NULL;
6201da177e4SLinus Torvalds 		}
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/*
6241da177e4SLinus Torvalds 	 *
6251da177e4SLinus Torvalds 	 * Logical drive commands
6261da177e4SLinus Torvalds 	 *
6271da177e4SLinus Torvalds 	 */
6281da177e4SLinus Torvalds 	if(islogical) {
6291da177e4SLinus Torvalds 		switch (cmd->cmnd[0]) {
6301da177e4SLinus Torvalds 		case TEST_UNIT_READY:
6311da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
6321da177e4SLinus Torvalds 			/*
6331da177e4SLinus Torvalds 			 * Do we support clustering and is the support enabled
6341da177e4SLinus Torvalds 			 * If no, return success always
6351da177e4SLinus Torvalds 			 */
6361da177e4SLinus Torvalds 			if( !adapter->has_cluster ) {
6371da177e4SLinus Torvalds 				cmd->result = (DID_OK << 16);
6389e060365SBart Van Assche 				scsi_done(cmd);
6391da177e4SLinus Torvalds 				return NULL;
6401da177e4SLinus Torvalds 			}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
6431da177e4SLinus Torvalds 				*busy = 1;
6441da177e4SLinus Torvalds 				return NULL;
6451da177e4SLinus Torvalds 			}
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
6481da177e4SLinus Torvalds 			scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
6491da177e4SLinus Torvalds 			scb->raw_mbox[3] = ldrv_num;
6501da177e4SLinus Torvalds 
651ec090ef8SSuraj Upadhyay 			scb->dma_direction = DMA_NONE;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 			return scb;
6541da177e4SLinus Torvalds #else
6551da177e4SLinus Torvalds 			cmd->result = (DID_OK << 16);
6569e060365SBart Van Assche 			scsi_done(cmd);
6571da177e4SLinus Torvalds 			return NULL;
6581da177e4SLinus Torvalds #endif
6591da177e4SLinus Torvalds 
66051c928c3SJames Bottomley 		case MODE_SENSE: {
66151c928c3SJames Bottomley 			char *buf;
66251c928c3SJames Bottomley 			struct scatterlist *sg;
66351c928c3SJames Bottomley 
6643f6270efSFUJITA Tomonori 			sg = scsi_sglist(cmd);
66577dfce07SCong Wang 			buf = kmap_atomic(sg_page(sg)) + sg->offset;
6663f6270efSFUJITA Tomonori 
667f0353301SMark Lord 			memset(buf, 0, cmd->cmnd[4]);
66877dfce07SCong Wang 			kunmap_atomic(buf - sg->offset);
6693f6270efSFUJITA Tomonori 
6701da177e4SLinus Torvalds 			cmd->result = (DID_OK << 16);
6719e060365SBart Van Assche 			scsi_done(cmd);
6721da177e4SLinus Torvalds 			return NULL;
67351c928c3SJames Bottomley 		}
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 		case READ_CAPACITY:
6761da177e4SLinus Torvalds 		case INQUIRY:
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 			if(!(adapter->flag & (1L << cmd->device->channel))) {
6791da177e4SLinus Torvalds 
6803b8a1ba3SBjorn Helgaas 				dev_notice(&adapter->dev->dev,
6813b8a1ba3SBjorn Helgaas 					"scsi%d: scanning scsi channel %d "
6823b8a1ba3SBjorn Helgaas 					"for logical drives\n",
6831da177e4SLinus Torvalds 						adapter->host->host_no,
6841da177e4SLinus Torvalds 						cmd->device->channel);
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 				adapter->flag |= (1L << cmd->device->channel);
6871da177e4SLinus Torvalds 			}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 			/* Allocate a SCB and initialize passthru */
6901da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
6911da177e4SLinus Torvalds 				*busy = 1;
6921da177e4SLinus Torvalds 				return NULL;
6931da177e4SLinus Torvalds 			}
6941da177e4SLinus Torvalds 			pthru = scb->pthru;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
6971da177e4SLinus Torvalds 			memset(mbox, 0, sizeof(scb->raw_mbox));
6981da177e4SLinus Torvalds 			memset(pthru, 0, sizeof(mega_passthru));
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 			pthru->timeout = 0;
7011da177e4SLinus Torvalds 			pthru->ars = 1;
7021da177e4SLinus Torvalds 			pthru->reqsenselen = 14;
7031da177e4SLinus Torvalds 			pthru->islogical = 1;
7041da177e4SLinus Torvalds 			pthru->logdrv = ldrv_num;
7051da177e4SLinus Torvalds 			pthru->cdblen = cmd->cmd_len;
7061da177e4SLinus Torvalds 			memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 			if( adapter->has_64bit_addr ) {
7091da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
7101da177e4SLinus Torvalds 			}
7111da177e4SLinus Torvalds 			else {
7121da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
7131da177e4SLinus Torvalds 			}
7141da177e4SLinus Torvalds 
715ec090ef8SSuraj Upadhyay 			scb->dma_direction = DMA_FROM_DEVICE;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 			pthru->numsgelements = mega_build_sglist(adapter, scb,
7181da177e4SLinus Torvalds 				&pthru->dataxferaddr, &pthru->dataxferlen);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 			mbox->m_out.xferaddr = scb->pthru_dma_addr;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 			return scb;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 		case READ_6:
7251da177e4SLinus Torvalds 		case WRITE_6:
7261da177e4SLinus Torvalds 		case READ_10:
7271da177e4SLinus Torvalds 		case WRITE_10:
7281da177e4SLinus Torvalds 		case READ_12:
7291da177e4SLinus Torvalds 		case WRITE_12:
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 			/* Allocate a SCB and initialize mailbox */
7321da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
7331da177e4SLinus Torvalds 				*busy = 1;
7341da177e4SLinus Torvalds 				return NULL;
7351da177e4SLinus Torvalds 			}
7361da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 			memset(mbox, 0, sizeof(scb->raw_mbox));
7391da177e4SLinus Torvalds 			mbox->m_out.logdrv = ldrv_num;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 			/*
7421da177e4SLinus Torvalds 			 * A little hack: 2nd bit is zero for all scsi read
7431da177e4SLinus Torvalds 			 * commands and is set for all scsi write commands
7441da177e4SLinus Torvalds 			 */
7451da177e4SLinus Torvalds 			if( adapter->has_64bit_addr ) {
7461da177e4SLinus Torvalds 				mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
7471da177e4SLinus Torvalds 					MEGA_MBOXCMD_LWRITE64:
7481da177e4SLinus Torvalds 					MEGA_MBOXCMD_LREAD64 ;
7491da177e4SLinus Torvalds 			}
7501da177e4SLinus Torvalds 			else {
7511da177e4SLinus Torvalds 				mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
7521da177e4SLinus Torvalds 					MEGA_MBOXCMD_LWRITE:
7531da177e4SLinus Torvalds 					MEGA_MBOXCMD_LREAD ;
7541da177e4SLinus Torvalds 			}
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 			/*
7571da177e4SLinus Torvalds 			 * 6-byte READ(0x08) or WRITE(0x0A) cdb
7581da177e4SLinus Torvalds 			 */
7591da177e4SLinus Torvalds 			if( cmd->cmd_len == 6 ) {
7601da177e4SLinus Torvalds 				mbox->m_out.numsectors = (u32) cmd->cmnd[4];
7611da177e4SLinus Torvalds 				mbox->m_out.lba =
7621da177e4SLinus Torvalds 					((u32)cmd->cmnd[1] << 16) |
7631da177e4SLinus Torvalds 					((u32)cmd->cmnd[2] << 8) |
7641da177e4SLinus Torvalds 					(u32)cmd->cmnd[3];
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 				mbox->m_out.lba &= 0x1FFFFF;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds #if MEGA_HAVE_STATS
7691da177e4SLinus Torvalds 				/*
7701da177e4SLinus Torvalds 				 * Take modulo 0x80, since the logical drive
7711da177e4SLinus Torvalds 				 * number increases by 0x80 when a logical
7721da177e4SLinus Torvalds 				 * drive was deleted
7731da177e4SLinus Torvalds 				 */
7741da177e4SLinus Torvalds 				if (*cmd->cmnd == READ_6) {
7751da177e4SLinus Torvalds 					adapter->nreads[ldrv_num%0x80]++;
7761da177e4SLinus Torvalds 					adapter->nreadblocks[ldrv_num%0x80] +=
7771da177e4SLinus Torvalds 						mbox->m_out.numsectors;
7781da177e4SLinus Torvalds 				} else {
7791da177e4SLinus Torvalds 					adapter->nwrites[ldrv_num%0x80]++;
7801da177e4SLinus Torvalds 					adapter->nwriteblocks[ldrv_num%0x80] +=
7811da177e4SLinus Torvalds 						mbox->m_out.numsectors;
7821da177e4SLinus Torvalds 				}
7831da177e4SLinus Torvalds #endif
7841da177e4SLinus Torvalds 			}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 			/*
7871da177e4SLinus Torvalds 			 * 10-byte READ(0x28) or WRITE(0x2A) cdb
7881da177e4SLinus Torvalds 			 */
7891da177e4SLinus Torvalds 			if( cmd->cmd_len == 10 ) {
7901da177e4SLinus Torvalds 				mbox->m_out.numsectors =
7911da177e4SLinus Torvalds 					(u32)cmd->cmnd[8] |
7921da177e4SLinus Torvalds 					((u32)cmd->cmnd[7] << 8);
7931da177e4SLinus Torvalds 				mbox->m_out.lba =
7941da177e4SLinus Torvalds 					((u32)cmd->cmnd[2] << 24) |
7951da177e4SLinus Torvalds 					((u32)cmd->cmnd[3] << 16) |
7961da177e4SLinus Torvalds 					((u32)cmd->cmnd[4] << 8) |
7971da177e4SLinus Torvalds 					(u32)cmd->cmnd[5];
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds #if MEGA_HAVE_STATS
8001da177e4SLinus Torvalds 				if (*cmd->cmnd == READ_10) {
8011da177e4SLinus Torvalds 					adapter->nreads[ldrv_num%0x80]++;
8021da177e4SLinus Torvalds 					adapter->nreadblocks[ldrv_num%0x80] +=
8031da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8041da177e4SLinus Torvalds 				} else {
8051da177e4SLinus Torvalds 					adapter->nwrites[ldrv_num%0x80]++;
8061da177e4SLinus Torvalds 					adapter->nwriteblocks[ldrv_num%0x80] +=
8071da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8081da177e4SLinus Torvalds 				}
8091da177e4SLinus Torvalds #endif
8101da177e4SLinus Torvalds 			}
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 			/*
8131da177e4SLinus Torvalds 			 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
8141da177e4SLinus Torvalds 			 */
8151da177e4SLinus Torvalds 			if( cmd->cmd_len == 12 ) {
8161da177e4SLinus Torvalds 				mbox->m_out.lba =
8171da177e4SLinus Torvalds 					((u32)cmd->cmnd[2] << 24) |
8181da177e4SLinus Torvalds 					((u32)cmd->cmnd[3] << 16) |
8191da177e4SLinus Torvalds 					((u32)cmd->cmnd[4] << 8) |
8201da177e4SLinus Torvalds 					(u32)cmd->cmnd[5];
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 				mbox->m_out.numsectors =
8231da177e4SLinus Torvalds 					((u32)cmd->cmnd[6] << 24) |
8241da177e4SLinus Torvalds 					((u32)cmd->cmnd[7] << 16) |
8251da177e4SLinus Torvalds 					((u32)cmd->cmnd[8] << 8) |
8261da177e4SLinus Torvalds 					(u32)cmd->cmnd[9];
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds #if MEGA_HAVE_STATS
8291da177e4SLinus Torvalds 				if (*cmd->cmnd == READ_12) {
8301da177e4SLinus Torvalds 					adapter->nreads[ldrv_num%0x80]++;
8311da177e4SLinus Torvalds 					adapter->nreadblocks[ldrv_num%0x80] +=
8321da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8331da177e4SLinus Torvalds 				} else {
8341da177e4SLinus Torvalds 					adapter->nwrites[ldrv_num%0x80]++;
8351da177e4SLinus Torvalds 					adapter->nwriteblocks[ldrv_num%0x80] +=
8361da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8371da177e4SLinus Torvalds 				}
8381da177e4SLinus Torvalds #endif
8391da177e4SLinus Torvalds 			}
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 			/*
8421da177e4SLinus Torvalds 			 * If it is a read command
8431da177e4SLinus Torvalds 			 */
8441da177e4SLinus Torvalds 			if( (*cmd->cmnd & 0x0F) == 0x08 ) {
845ec090ef8SSuraj Upadhyay 				scb->dma_direction = DMA_FROM_DEVICE;
8461da177e4SLinus Torvalds 			}
8471da177e4SLinus Torvalds 			else {
848ec090ef8SSuraj Upadhyay 				scb->dma_direction = DMA_TO_DEVICE;
8491da177e4SLinus Torvalds 			}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 			/* Calculate Scatter-Gather info */
8521da177e4SLinus Torvalds 			mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
8539d5d93e3SDan Carpenter 					(u32 *)&mbox->m_out.xferaddr, &seg);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 			return scb;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
858df561f66SGustavo A. R. Silva 		case RESERVE:
8591da177e4SLinus Torvalds 		case RELEASE:
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 			/*
8621da177e4SLinus Torvalds 			 * Do we support clustering and is the support enabled
8631da177e4SLinus Torvalds 			 */
8641da177e4SLinus Torvalds 			if( ! adapter->has_cluster ) {
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 				cmd->result = (DID_BAD_TARGET << 16);
8679e060365SBart Van Assche 				scsi_done(cmd);
8681da177e4SLinus Torvalds 				return NULL;
8691da177e4SLinus Torvalds 			}
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 			/* Allocate a SCB and initialize mailbox */
8721da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
8731da177e4SLinus Torvalds 				*busy = 1;
8741da177e4SLinus Torvalds 				return NULL;
8751da177e4SLinus Torvalds 			}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
8781da177e4SLinus Torvalds 			scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
8791da177e4SLinus Torvalds 				MEGA_RESERVE_LD : MEGA_RELEASE_LD;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 			scb->raw_mbox[3] = ldrv_num;
8821da177e4SLinus Torvalds 
883ec090ef8SSuraj Upadhyay 			scb->dma_direction = DMA_NONE;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 			return scb;
8861da177e4SLinus Torvalds #endif
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		default:
8891da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
8909e060365SBart Van Assche 			scsi_done(cmd);
8911da177e4SLinus Torvalds 			return NULL;
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	/*
8961da177e4SLinus Torvalds 	 * Passthru drive commands
8971da177e4SLinus Torvalds 	 */
8981da177e4SLinus Torvalds 	else {
8991da177e4SLinus Torvalds 		/* Allocate a SCB and initialize passthru */
9001da177e4SLinus Torvalds 		if(!(scb = mega_allocate_scb(adapter, cmd))) {
9011da177e4SLinus Torvalds 			*busy = 1;
9021da177e4SLinus Torvalds 			return NULL;
9031da177e4SLinus Torvalds 		}
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 		mbox = (mbox_t *)scb->raw_mbox;
9061da177e4SLinus Torvalds 		memset(mbox, 0, sizeof(scb->raw_mbox));
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 		if( adapter->support_ext_cdb ) {
9091da177e4SLinus Torvalds 
9102a6576d2SDamien Le Moal 			mega_prepare_extpassthru(adapter, scb, cmd,
9111da177e4SLinus Torvalds 					channel, target);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 			mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 			mbox->m_out.xferaddr = scb->epthru_dma_addr;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		}
9181da177e4SLinus Torvalds 		else {
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 			pthru = mega_prepare_passthru(adapter, scb, cmd,
9211da177e4SLinus Torvalds 					channel, target);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 			/* Initialize mailbox */
9241da177e4SLinus Torvalds 			if( adapter->has_64bit_addr ) {
9251da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
9261da177e4SLinus Torvalds 			}
9271da177e4SLinus Torvalds 			else {
9281da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
9291da177e4SLinus Torvalds 			}
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 			mbox->m_out.xferaddr = scb->pthru_dma_addr;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 		}
9341da177e4SLinus Torvalds 		return scb;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 	return NULL;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds /**
9411da177e4SLinus Torvalds  * mega_prepare_passthru()
9422b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
9432b46e5c1SDamien Le Moal  * @scb: our scsi control block
9442b46e5c1SDamien Le Moal  * @cmd: scsi command from the mid-layer
9452b46e5c1SDamien Le Moal  * @channel: actual channel on the controller
9462b46e5c1SDamien Le Moal  * @target: actual id on the controller.
9471da177e4SLinus Torvalds  *
9481da177e4SLinus Torvalds  * prepare a command for the scsi physical devices.
9491da177e4SLinus Torvalds  */
9501da177e4SLinus Torvalds static mega_passthru *
mega_prepare_passthru(adapter_t * adapter,scb_t * scb,struct scsi_cmnd * cmd,int channel,int target)95191ebc1faSJohannes Thumshirn mega_prepare_passthru(adapter_t *adapter, scb_t *scb, struct scsi_cmnd *cmd,
9521da177e4SLinus Torvalds 		      int channel, int target)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	mega_passthru *pthru;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	pthru = scb->pthru;
9571da177e4SLinus Torvalds 	memset(pthru, 0, sizeof (mega_passthru));
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/* 0=6sec/1=60sec/2=10min/3=3hrs */
9601da177e4SLinus Torvalds 	pthru->timeout = 2;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	pthru->ars = 1;
9631da177e4SLinus Torvalds 	pthru->reqsenselen = 14;
9641da177e4SLinus Torvalds 	pthru->islogical = 0;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	pthru->target = (adapter->flag & BOARD_40LD) ?
9691da177e4SLinus Torvalds 		(channel << 4) | target : target;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	pthru->cdblen = cmd->cmd_len;
9721da177e4SLinus Torvalds 	pthru->logdrv = cmd->device->lun;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	/* Not sure about the direction */
977ec090ef8SSuraj Upadhyay 	scb->dma_direction = DMA_BIDIRECTIONAL;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	/* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
9801da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
9811da177e4SLinus Torvalds 	case INQUIRY:
9821da177e4SLinus Torvalds 	case READ_CAPACITY:
9831da177e4SLinus Torvalds 		if(!(adapter->flag & (1L << cmd->device->channel))) {
9841da177e4SLinus Torvalds 
9853b8a1ba3SBjorn Helgaas 			dev_notice(&adapter->dev->dev,
9863b8a1ba3SBjorn Helgaas 				"scsi%d: scanning scsi channel %d [P%d] "
9873b8a1ba3SBjorn Helgaas 				"for physical devices\n",
9881da177e4SLinus Torvalds 					adapter->host->host_no,
9891da177e4SLinus Torvalds 					cmd->device->channel, channel);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 			adapter->flag |= (1L << cmd->device->channel);
9921da177e4SLinus Torvalds 		}
993df561f66SGustavo A. R. Silva 		fallthrough;
9941da177e4SLinus Torvalds 	default:
9951da177e4SLinus Torvalds 		pthru->numsgelements = mega_build_sglist(adapter, scb,
9961da177e4SLinus Torvalds 				&pthru->dataxferaddr, &pthru->dataxferlen);
9971da177e4SLinus Torvalds 		break;
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 	return pthru;
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds /**
10041da177e4SLinus Torvalds  * mega_prepare_extpassthru()
10052b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
10062b46e5c1SDamien Le Moal  * @scb: our scsi control block
10072b46e5c1SDamien Le Moal  * @cmd: scsi command from the mid-layer
10082b46e5c1SDamien Le Moal  * @channel: actual channel on the controller
10092b46e5c1SDamien Le Moal  * @target: actual id on the controller.
10101da177e4SLinus Torvalds  *
10111da177e4SLinus Torvalds  * prepare a command for the scsi physical devices. This rountine prepares
10121da177e4SLinus Torvalds  * commands for devices which can take extended CDBs (>10 bytes)
10131da177e4SLinus Torvalds  */
10141da177e4SLinus Torvalds static mega_ext_passthru *
mega_prepare_extpassthru(adapter_t * adapter,scb_t * scb,struct scsi_cmnd * cmd,int channel,int target)101591ebc1faSJohannes Thumshirn mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb,
101691ebc1faSJohannes Thumshirn 			 struct scsi_cmnd *cmd,
10171da177e4SLinus Torvalds 			 int channel, int target)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	mega_ext_passthru	*epthru;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	epthru = scb->epthru;
10221da177e4SLinus Torvalds 	memset(epthru, 0, sizeof(mega_ext_passthru));
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	/* 0=6sec/1=60sec/2=10min/3=3hrs */
10251da177e4SLinus Torvalds 	epthru->timeout = 2;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	epthru->ars = 1;
10281da177e4SLinus Torvalds 	epthru->reqsenselen = 14;
10291da177e4SLinus Torvalds 	epthru->islogical = 0;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
10321da177e4SLinus Torvalds 	epthru->target = (adapter->flag & BOARD_40LD) ?
10331da177e4SLinus Torvalds 		(channel << 4) | target : target;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	epthru->cdblen = cmd->cmd_len;
10361da177e4SLinus Torvalds 	epthru->logdrv = cmd->device->lun;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	/* Not sure about the direction */
1041ec090ef8SSuraj Upadhyay 	scb->dma_direction = DMA_BIDIRECTIONAL;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	switch(cmd->cmnd[0]) {
10441da177e4SLinus Torvalds 	case INQUIRY:
10451da177e4SLinus Torvalds 	case READ_CAPACITY:
10461da177e4SLinus Torvalds 		if(!(adapter->flag & (1L << cmd->device->channel))) {
10471da177e4SLinus Torvalds 
10483b8a1ba3SBjorn Helgaas 			dev_notice(&adapter->dev->dev,
10493b8a1ba3SBjorn Helgaas 				"scsi%d: scanning scsi channel %d [P%d] "
10503b8a1ba3SBjorn Helgaas 				"for physical devices\n",
10511da177e4SLinus Torvalds 					adapter->host->host_no,
10521da177e4SLinus Torvalds 					cmd->device->channel, channel);
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 			adapter->flag |= (1L << cmd->device->channel);
10551da177e4SLinus Torvalds 		}
1056df561f66SGustavo A. R. Silva 		fallthrough;
10571da177e4SLinus Torvalds 	default:
10581da177e4SLinus Torvalds 		epthru->numsgelements = mega_build_sglist(adapter, scb,
10591da177e4SLinus Torvalds 				&epthru->dataxferaddr, &epthru->dataxferlen);
10601da177e4SLinus Torvalds 		break;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	return epthru;
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds static void
__mega_runpendq(adapter_t * adapter)10671da177e4SLinus Torvalds __mega_runpendq(adapter_t *adapter)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds 	scb_t *scb;
10701da177e4SLinus Torvalds 	struct list_head *pos, *next;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	/* Issue any pending commands to the card */
10731da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &adapter->pending_list) {
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 		scb = list_entry(pos, scb_t, list);
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 		if( !(scb->state & SCB_ISSUED) ) {
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 			if( issue_scb(adapter, scb) != 0 )
10801da177e4SLinus Torvalds 				return;
10811da177e4SLinus Torvalds 		}
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	return;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds /**
10891da177e4SLinus Torvalds  * issue_scb()
10902b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
10912b46e5c1SDamien Le Moal  * @scb: scsi control block
10921da177e4SLinus Torvalds  *
10931da177e4SLinus Torvalds  * Post a command to the card if the mailbox is available, otherwise return
10941da177e4SLinus Torvalds  * busy. We also take the scb from the pending list if the mailbox is
10951da177e4SLinus Torvalds  * available.
10961da177e4SLinus Torvalds  */
10971da177e4SLinus Torvalds static int
issue_scb(adapter_t * adapter,scb_t * scb)10981da177e4SLinus Torvalds issue_scb(adapter_t *adapter, scb_t *scb)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	volatile mbox64_t	*mbox64 = adapter->mbox64;
11011da177e4SLinus Torvalds 	volatile mbox_t		*mbox = adapter->mbox;
11021da177e4SLinus Torvalds 	unsigned int	i = 0;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	if(unlikely(mbox->m_in.busy)) {
11051da177e4SLinus Torvalds 		do {
11061da177e4SLinus Torvalds 			udelay(1);
11071da177e4SLinus Torvalds 			i++;
11081da177e4SLinus Torvalds 		} while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 		if(mbox->m_in.busy) return -1;
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	/* Copy mailbox data into host structure */
11141da177e4SLinus Torvalds 	memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
11151da177e4SLinus Torvalds 			sizeof(struct mbox_out));
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	mbox->m_out.cmdid = scb->idx;	/* Set cmdid */
11181da177e4SLinus Torvalds 	mbox->m_in.busy = 1;		/* Set busy */
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	/*
11221da177e4SLinus Torvalds 	 * Increment the pending queue counter
11231da177e4SLinus Torvalds 	 */
11241da177e4SLinus Torvalds 	atomic_inc(&adapter->pend_cmds);
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	switch (mbox->m_out.cmd) {
11271da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LREAD64:
11281da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LWRITE64:
11291da177e4SLinus Torvalds 	case MEGA_MBOXCMD_PASSTHRU64:
11301da177e4SLinus Torvalds 	case MEGA_MBOXCMD_EXTPTHRU:
11311da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
11321da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
11331da177e4SLinus Torvalds 		mbox->m_out.xferaddr = 0xFFFFFFFF;
11341da177e4SLinus Torvalds 		break;
11351da177e4SLinus Torvalds 	default:
11361da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = 0;
11371da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	/*
11411da177e4SLinus Torvalds 	 * post the command
11421da177e4SLinus Torvalds 	 */
11431da177e4SLinus Torvalds 	scb->state |= SCB_ISSUED;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	if( likely(adapter->flag & BOARD_MEMMAP) ) {
11461da177e4SLinus Torvalds 		mbox->m_in.poll = 0;
11471da177e4SLinus Torvalds 		mbox->m_in.ack = 0;
11481da177e4SLinus Torvalds 		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 	else {
11511da177e4SLinus Torvalds 		irq_enable(adapter);
11521da177e4SLinus Torvalds 		issue_command(adapter);
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	return 0;
11561da177e4SLinus Torvalds }
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds /*
11591da177e4SLinus Torvalds  * Wait until the controller's mailbox is available
11601da177e4SLinus Torvalds  */
11611da177e4SLinus Torvalds static inline int
mega_busywait_mbox(adapter_t * adapter)11621da177e4SLinus Torvalds mega_busywait_mbox (adapter_t *adapter)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	if (adapter->mbox->m_in.busy)
11651da177e4SLinus Torvalds 		return __mega_busywait_mbox(adapter);
11661da177e4SLinus Torvalds 	return 0;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds /**
11701da177e4SLinus Torvalds  * issue_scb_block()
11712b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
11722b46e5c1SDamien Le Moal  * @raw_mbox: the mailbox
11731da177e4SLinus Torvalds  *
11741da177e4SLinus Torvalds  * Issue a scb in synchronous and non-interrupt mode
11751da177e4SLinus Torvalds  */
11761da177e4SLinus Torvalds static int
issue_scb_block(adapter_t * adapter,u_char * raw_mbox)11771da177e4SLinus Torvalds issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
11781da177e4SLinus Torvalds {
11791da177e4SLinus Torvalds 	volatile mbox64_t *mbox64 = adapter->mbox64;
11801da177e4SLinus Torvalds 	volatile mbox_t *mbox = adapter->mbox;
11811da177e4SLinus Torvalds 	u8	byte;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	/* Wait until mailbox is free */
11841da177e4SLinus Torvalds 	if(mega_busywait_mbox (adapter))
11851da177e4SLinus Torvalds 		goto bug_blocked_mailbox;
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	/* Copy mailbox data into host structure */
11881da177e4SLinus Torvalds 	memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
11891da177e4SLinus Torvalds 	mbox->m_out.cmdid = 0xFE;
11901da177e4SLinus Torvalds 	mbox->m_in.busy = 1;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	switch (raw_mbox[0]) {
11931da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LREAD64:
11941da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LWRITE64:
11951da177e4SLinus Torvalds 	case MEGA_MBOXCMD_PASSTHRU64:
11961da177e4SLinus Torvalds 	case MEGA_MBOXCMD_EXTPTHRU:
11971da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
11981da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
11991da177e4SLinus Torvalds 		mbox->m_out.xferaddr = 0xFFFFFFFF;
12001da177e4SLinus Torvalds 		break;
12011da177e4SLinus Torvalds 	default:
12021da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = 0;
12031da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
12041da177e4SLinus Torvalds 	}
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	if( likely(adapter->flag & BOARD_MEMMAP) ) {
12071da177e4SLinus Torvalds 		mbox->m_in.poll = 0;
12081da177e4SLinus Torvalds 		mbox->m_in.ack = 0;
12091da177e4SLinus Torvalds 		mbox->m_in.numstatus = 0xFF;
12101da177e4SLinus Torvalds 		mbox->m_in.status = 0xFF;
12111da177e4SLinus Torvalds 		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 		while((volatile u8)mbox->m_in.numstatus == 0xFF)
12141da177e4SLinus Torvalds 			cpu_relax();
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 		mbox->m_in.numstatus = 0xFF;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		while( (volatile u8)mbox->m_in.poll != 0x77 )
12191da177e4SLinus Torvalds 			cpu_relax();
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 		mbox->m_in.poll = 0;
12221da177e4SLinus Torvalds 		mbox->m_in.ack = 0x77;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 		WRINDOOR(adapter, adapter->mbox_dma | 0x2);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		while(RDINDOOR(adapter) & 0x2)
12271da177e4SLinus Torvalds 			cpu_relax();
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 	else {
12301da177e4SLinus Torvalds 		irq_disable(adapter);
12311da177e4SLinus Torvalds 		issue_command(adapter);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		while (!((byte = irq_state(adapter)) & INTR_VALID))
12341da177e4SLinus Torvalds 			cpu_relax();
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		set_irq_state(adapter, byte);
12371da177e4SLinus Torvalds 		irq_enable(adapter);
12381da177e4SLinus Torvalds 		irq_ack(adapter);
12391da177e4SLinus Torvalds 	}
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	return mbox->m_in.status;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds bug_blocked_mailbox:
12443b8a1ba3SBjorn Helgaas 	dev_warn(&adapter->dev->dev, "Blocked mailbox......!!\n");
12451da177e4SLinus Torvalds 	udelay (1000);
12461da177e4SLinus Torvalds 	return -1;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds /**
12511da177e4SLinus Torvalds  * megaraid_isr_iomapped()
12522b46e5c1SDamien Le Moal  * @irq: irq
12532b46e5c1SDamien Le Moal  * @devp: pointer to our soft state
12541da177e4SLinus Torvalds  *
12551da177e4SLinus Torvalds  * Interrupt service routine for io-mapped controllers.
12561da177e4SLinus Torvalds  * Find out if our device is interrupting. If yes, acknowledge the interrupt
12571da177e4SLinus Torvalds  * and service the completed commands.
12581da177e4SLinus Torvalds  */
12591da177e4SLinus Torvalds static irqreturn_t
megaraid_isr_iomapped(int irq,void * devp)12607d12e780SDavid Howells megaraid_isr_iomapped(int irq, void *devp)
12611da177e4SLinus Torvalds {
12621da177e4SLinus Torvalds 	adapter_t	*adapter = devp;
12631da177e4SLinus Torvalds 	unsigned long	flags;
12641da177e4SLinus Torvalds 	u8	status;
12651da177e4SLinus Torvalds 	u8	nstatus;
12661da177e4SLinus Torvalds 	u8	completed[MAX_FIRMWARE_STATUS];
12671da177e4SLinus Torvalds 	u8	byte;
12681da177e4SLinus Torvalds 	int	handled = 0;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	/*
12721da177e4SLinus Torvalds 	 * loop till F/W has more commands for us to complete.
12731da177e4SLinus Torvalds 	 */
12741da177e4SLinus Torvalds 	spin_lock_irqsave(&adapter->lock, flags);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	do {
12771da177e4SLinus Torvalds 		/* Check if a valid interrupt is pending */
12781da177e4SLinus Torvalds 		byte = irq_state(adapter);
12791da177e4SLinus Torvalds 		if( (byte & VALID_INTR_BYTE) == 0 ) {
12801da177e4SLinus Torvalds 			/*
12811da177e4SLinus Torvalds 			 * No more pending commands
12821da177e4SLinus Torvalds 			 */
12831da177e4SLinus Torvalds 			goto out_unlock;
12841da177e4SLinus Torvalds 		}
12851da177e4SLinus Torvalds 		set_irq_state(adapter, byte);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 		while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
12881da177e4SLinus Torvalds 				== 0xFF)
12891da177e4SLinus Torvalds 			cpu_relax();
12901da177e4SLinus Torvalds 		adapter->mbox->m_in.numstatus = 0xFF;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		status = adapter->mbox->m_in.status;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 		/*
12951da177e4SLinus Torvalds 		 * decrement the pending queue counter
12961da177e4SLinus Torvalds 		 */
12971da177e4SLinus Torvalds 		atomic_sub(nstatus, &adapter->pend_cmds);
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		memcpy(completed, (void *)adapter->mbox->m_in.completed,
13001da177e4SLinus Torvalds 				nstatus);
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 		/* Acknowledge interrupt */
13031da177e4SLinus Torvalds 		irq_ack(adapter);
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 		mega_cmd_done(adapter, completed, nstatus, status);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 		mega_rundoneq(adapter);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 		handled = 1;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		/* Loop through any pending requests */
13121da177e4SLinus Torvalds 		if(atomic_read(&adapter->quiescent) == 0) {
13131da177e4SLinus Torvalds 			mega_runpendq(adapter);
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	} while(1);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds  out_unlock:
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adapter->lock, flags);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	return IRQ_RETVAL(handled);
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds /**
13271da177e4SLinus Torvalds  * megaraid_isr_memmapped()
13282b46e5c1SDamien Le Moal  * @irq: irq
13292b46e5c1SDamien Le Moal  * @devp: pointer to our soft state
13301da177e4SLinus Torvalds  *
13311da177e4SLinus Torvalds  * Interrupt service routine for memory-mapped controllers.
13321da177e4SLinus Torvalds  * Find out if our device is interrupting. If yes, acknowledge the interrupt
13331da177e4SLinus Torvalds  * and service the completed commands.
13341da177e4SLinus Torvalds  */
13351da177e4SLinus Torvalds static irqreturn_t
megaraid_isr_memmapped(int irq,void * devp)13367d12e780SDavid Howells megaraid_isr_memmapped(int irq, void *devp)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	adapter_t	*adapter = devp;
13391da177e4SLinus Torvalds 	unsigned long	flags;
13401da177e4SLinus Torvalds 	u8	status;
13411da177e4SLinus Torvalds 	u32	dword = 0;
13421da177e4SLinus Torvalds 	u8	nstatus;
13431da177e4SLinus Torvalds 	u8	completed[MAX_FIRMWARE_STATUS];
13441da177e4SLinus Torvalds 	int	handled = 0;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	/*
13481da177e4SLinus Torvalds 	 * loop till F/W has more commands for us to complete.
13491da177e4SLinus Torvalds 	 */
13501da177e4SLinus Torvalds 	spin_lock_irqsave(&adapter->lock, flags);
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	do {
13531da177e4SLinus Torvalds 		/* Check if a valid interrupt is pending */
13541da177e4SLinus Torvalds 		dword = RDOUTDOOR(adapter);
13551da177e4SLinus Torvalds 		if(dword != 0x10001234) {
13561da177e4SLinus Torvalds 			/*
13571da177e4SLinus Torvalds 			 * No more pending commands
13581da177e4SLinus Torvalds 			 */
13591da177e4SLinus Torvalds 			goto out_unlock;
13601da177e4SLinus Torvalds 		}
13611da177e4SLinus Torvalds 		WROUTDOOR(adapter, 0x10001234);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
13641da177e4SLinus Torvalds 				== 0xFF) {
13651da177e4SLinus Torvalds 			cpu_relax();
13661da177e4SLinus Torvalds 		}
13671da177e4SLinus Torvalds 		adapter->mbox->m_in.numstatus = 0xFF;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 		status = adapter->mbox->m_in.status;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 		/*
13721da177e4SLinus Torvalds 		 * decrement the pending queue counter
13731da177e4SLinus Torvalds 		 */
13741da177e4SLinus Torvalds 		atomic_sub(nstatus, &adapter->pend_cmds);
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 		memcpy(completed, (void *)adapter->mbox->m_in.completed,
13771da177e4SLinus Torvalds 				nstatus);
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 		/* Acknowledge interrupt */
13801da177e4SLinus Torvalds 		WRINDOOR(adapter, 0x2);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 		handled = 1;
13831da177e4SLinus Torvalds 
138400769ec4SJeff Garzik 		while( RDINDOOR(adapter) & 0x02 )
138500769ec4SJeff Garzik 			cpu_relax();
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 		mega_cmd_done(adapter, completed, nstatus, status);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 		mega_rundoneq(adapter);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 		/* Loop through any pending requests */
13921da177e4SLinus Torvalds 		if(atomic_read(&adapter->quiescent) == 0) {
13931da177e4SLinus Torvalds 			mega_runpendq(adapter);
13941da177e4SLinus Torvalds 		}
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	} while(1);
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds  out_unlock:
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adapter->lock, flags);
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	return IRQ_RETVAL(handled);
14031da177e4SLinus Torvalds }
14041da177e4SLinus Torvalds /**
14051da177e4SLinus Torvalds  * mega_cmd_done()
14062b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
14072b46e5c1SDamien Le Moal  * @completed: array of ids of completed commands
14082b46e5c1SDamien Le Moal  * @nstatus: number of completed commands
14092b46e5c1SDamien Le Moal  * @status: status of the last command completed
14101da177e4SLinus Torvalds  *
14118e572babSJustin P. Mattock  * Complete the commands and call the scsi mid-layer callback hooks.
14121da177e4SLinus Torvalds  */
14131da177e4SLinus Torvalds static void
mega_cmd_done(adapter_t * adapter,u8 completed[],int nstatus,int status)14141da177e4SLinus Torvalds mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
14151da177e4SLinus Torvalds {
14161da177e4SLinus Torvalds 	mega_ext_passthru	*epthru = NULL;
14171da177e4SLinus Torvalds 	struct scatterlist	*sgl;
141891ebc1faSJohannes Thumshirn 	struct scsi_cmnd	*cmd = NULL;
14191da177e4SLinus Torvalds 	mega_passthru	*pthru = NULL;
14201da177e4SLinus Torvalds 	mbox_t	*mbox = NULL;
14211da177e4SLinus Torvalds 	u8	c;
14221da177e4SLinus Torvalds 	scb_t	*scb;
14231da177e4SLinus Torvalds 	int	islogical;
14241da177e4SLinus Torvalds 	int	cmdid;
14251da177e4SLinus Torvalds 	int	i;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	/*
14281da177e4SLinus Torvalds 	 * for all the commands completed, call the mid-layer callback routine
14291da177e4SLinus Torvalds 	 * and free the scb.
14301da177e4SLinus Torvalds 	 */
14311da177e4SLinus Torvalds 	for( i = 0; i < nstatus; i++ ) {
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 		cmdid = completed[i];
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 		/*
14360f2bb84dSChristoph Hellwig 		 * Only free SCBs for the commands coming down from the
14370f2bb84dSChristoph Hellwig 		 * mid-layer, not for which were issued internally
14380f2bb84dSChristoph Hellwig 		 *
14390f2bb84dSChristoph Hellwig 		 * For internal command, restore the status returned by the
14400f2bb84dSChristoph Hellwig 		 * firmware so that user can interpret it.
14411da177e4SLinus Torvalds 		 */
14420f2bb84dSChristoph Hellwig 		if (cmdid == CMDID_INT_CMDS) {
14430f2bb84dSChristoph Hellwig 			scb = &adapter->int_scb;
144475cb113cSDanila Chernetsov 			cmd = scb->cmd;
14451da177e4SLinus Torvalds 
14460f2bb84dSChristoph Hellwig 			list_del_init(&scb->list);
14470f2bb84dSChristoph Hellwig 			scb->state = SCB_FREE;
14480f2bb84dSChristoph Hellwig 
14490f2bb84dSChristoph Hellwig 			adapter->int_status = status;
14500f2bb84dSChristoph Hellwig 			complete(&adapter->int_waitq);
14510f2bb84dSChristoph Hellwig 		} else {
14521da177e4SLinus Torvalds 			scb = &adapter->scb_list[cmdid];
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 			/*
14551da177e4SLinus Torvalds 			 * Make sure f/w has completed a valid command
14561da177e4SLinus Torvalds 			 */
14571da177e4SLinus Torvalds 			if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
14583b8a1ba3SBjorn Helgaas 				dev_crit(&adapter->dev->dev, "invalid command "
14593b8a1ba3SBjorn Helgaas 					"Id %d, scb->state:%x, scsi cmd:%p\n",
14601da177e4SLinus Torvalds 					cmdid, scb->state, scb->cmd);
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 				continue;
14631da177e4SLinus Torvalds 			}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 			/*
14661da177e4SLinus Torvalds 			 * Was a abort issued for this command
14671da177e4SLinus Torvalds 			 */
14681da177e4SLinus Torvalds 			if( scb->state & SCB_ABORT ) {
14691da177e4SLinus Torvalds 
14703b8a1ba3SBjorn Helgaas 				dev_warn(&adapter->dev->dev,
14713b8a1ba3SBjorn Helgaas 					"aborted cmd [%x] complete\n",
14725cd049a5SChristoph Hellwig 					scb->idx);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 				scb->cmd->result = (DID_ABORT << 16);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 				list_add_tail(SCSI_LIST(scb->cmd),
14771da177e4SLinus Torvalds 						&adapter->completed_list);
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 				mega_free_scb(adapter, scb);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 				continue;
14821da177e4SLinus Torvalds 			}
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 			/*
14851da177e4SLinus Torvalds 			 * Was a reset issued for this command
14861da177e4SLinus Torvalds 			 */
14871da177e4SLinus Torvalds 			if( scb->state & SCB_RESET ) {
14881da177e4SLinus Torvalds 
14893b8a1ba3SBjorn Helgaas 				dev_warn(&adapter->dev->dev,
14903b8a1ba3SBjorn Helgaas 					"reset cmd [%x] complete\n",
14915cd049a5SChristoph Hellwig 					scb->idx);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 				scb->cmd->result = (DID_RESET << 16);
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 				list_add_tail(SCSI_LIST(scb->cmd),
14961da177e4SLinus Torvalds 						&adapter->completed_list);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 				mega_free_scb (adapter, scb);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 				continue;
15011da177e4SLinus Torvalds 			}
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 			cmd = scb->cmd;
15041da177e4SLinus Torvalds 			pthru = scb->pthru;
15051da177e4SLinus Torvalds 			epthru = scb->epthru;
15061da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds #if MEGA_HAVE_STATS
15091da177e4SLinus Torvalds 			{
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 			int	logdrv = mbox->m_out.logdrv;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 			islogical = adapter->logdrv_chan[cmd->channel];
15141da177e4SLinus Torvalds 			/*
15151da177e4SLinus Torvalds 			 * Maintain an error counter for the logical drive.
15161da177e4SLinus Torvalds 			 * Some application like SNMP agent need such
15171da177e4SLinus Torvalds 			 * statistics
15181da177e4SLinus Torvalds 			 */
15191da177e4SLinus Torvalds 			if( status && islogical && (cmd->cmnd[0] == READ_6 ||
15201da177e4SLinus Torvalds 						cmd->cmnd[0] == READ_10 ||
15211da177e4SLinus Torvalds 						cmd->cmnd[0] == READ_12)) {
15221da177e4SLinus Torvalds 				/*
15231da177e4SLinus Torvalds 				 * Logical drive number increases by 0x80 when
15241da177e4SLinus Torvalds 				 * a logical drive is deleted
15251da177e4SLinus Torvalds 				 */
15261da177e4SLinus Torvalds 				adapter->rd_errors[logdrv%0x80]++;
15271da177e4SLinus Torvalds 			}
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 			if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
15301da177e4SLinus Torvalds 						cmd->cmnd[0] == WRITE_10 ||
15311da177e4SLinus Torvalds 						cmd->cmnd[0] == WRITE_12)) {
15321da177e4SLinus Torvalds 				/*
15331da177e4SLinus Torvalds 				 * Logical drive number increases by 0x80 when
15341da177e4SLinus Torvalds 				 * a logical drive is deleted
15351da177e4SLinus Torvalds 				 */
15361da177e4SLinus Torvalds 				adapter->wr_errors[logdrv%0x80]++;
15371da177e4SLinus Torvalds 			}
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 			}
15401da177e4SLinus Torvalds #endif
15411da177e4SLinus Torvalds 		}
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 		/*
15441da177e4SLinus Torvalds 		 * Do not return the presence of hard disk on the channel so,
15451da177e4SLinus Torvalds 		 * inquiry sent, and returned data==hard disk or removable
15461da177e4SLinus Torvalds 		 * hard disk and not logical, request should return failure! -
15471da177e4SLinus Torvalds 		 * PJ
15481da177e4SLinus Torvalds 		 */
15491da177e4SLinus Torvalds 		islogical = adapter->logdrv_chan[cmd->device->channel];
15501da177e4SLinus Torvalds 		if( cmd->cmnd[0] == INQUIRY && !islogical ) {
15511da177e4SLinus Torvalds 
15523f6270efSFUJITA Tomonori 			sgl = scsi_sglist(cmd);
155345711f1aSJens Axboe 			if( sg_page(sgl) ) {
155445711f1aSJens Axboe 				c = *(unsigned char *) sg_virt(&sgl[0]);
15553f6270efSFUJITA Tomonori 			} else {
15563b8a1ba3SBjorn Helgaas 				dev_warn(&adapter->dev->dev, "invalid sg\n");
15571da177e4SLinus Torvalds 				c = 0;
15581da177e4SLinus Torvalds 			}
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 			if(IS_RAID_CH(adapter, cmd->device->channel) &&
15611da177e4SLinus Torvalds 					((c & 0x1F ) == TYPE_DISK)) {
15621da177e4SLinus Torvalds 				status = 0xF0;
15631da177e4SLinus Torvalds 			}
15641da177e4SLinus Torvalds 		}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 		/* clear result; otherwise, success returns corrupt value */
15671da177e4SLinus Torvalds 		cmd->result = 0;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 		/* Convert MegaRAID status to Linux error code */
15701da177e4SLinus Torvalds 		switch (status) {
15711da177e4SLinus Torvalds 		case 0x00:	/* SUCCESS , i.e. SCSI_STATUS_GOOD */
15721da177e4SLinus Torvalds 			cmd->result |= (DID_OK << 16);
15731da177e4SLinus Torvalds 			break;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 		case 0x02:	/* ERROR_ABORTED, i.e.
15761da177e4SLinus Torvalds 				   SCSI_STATUS_CHECK_CONDITION */
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 			/* set sense_buffer and result fields */
15791da177e4SLinus Torvalds 			if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
15801da177e4SLinus Torvalds 				mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 				memcpy(cmd->sense_buffer, pthru->reqsensearea,
15831da177e4SLinus Torvalds 						14);
15841da177e4SLinus Torvalds 
1585464a00c9SHannes Reinecke 				cmd->result = SAM_STAT_CHECK_CONDITION;
15861da177e4SLinus Torvalds 			}
15871da177e4SLinus Torvalds 			else {
15881da177e4SLinus Torvalds 				if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 					memcpy(cmd->sense_buffer,
15911da177e4SLinus Torvalds 						epthru->reqsensearea, 14);
15921da177e4SLinus Torvalds 
1593464a00c9SHannes Reinecke 					cmd->result = SAM_STAT_CHECK_CONDITION;
1594f2b1e9c6SHannes Reinecke 				} else
1595f2b1e9c6SHannes Reinecke 					scsi_build_sense(cmd, 0,
1596f2b1e9c6SHannes Reinecke 							 ABORTED_COMMAND, 0, 0);
15971da177e4SLinus Torvalds 			}
15981da177e4SLinus Torvalds 			break;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 		case 0x08:	/* ERR_DEST_DRIVE_FAILED, i.e.
16011da177e4SLinus Torvalds 				   SCSI_STATUS_BUSY */
16021da177e4SLinus Torvalds 			cmd->result |= (DID_BUS_BUSY << 16) | status;
16031da177e4SLinus Torvalds 			break;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 		default:
16061da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
16071da177e4SLinus Torvalds 			/*
16081da177e4SLinus Torvalds 			 * If TEST_UNIT_READY fails, we know
16091da177e4SLinus Torvalds 			 * MEGA_RESERVATION_STATUS failed
16101da177e4SLinus Torvalds 			 */
16111da177e4SLinus Torvalds 			if( cmd->cmnd[0] == TEST_UNIT_READY ) {
16121da177e4SLinus Torvalds 				cmd->result |= (DID_ERROR << 16) |
16133d45cefcSHannes Reinecke 					SAM_STAT_RESERVATION_CONFLICT;
16141da177e4SLinus Torvalds 			}
16151da177e4SLinus Torvalds 			else
16161da177e4SLinus Torvalds 			/*
16171da177e4SLinus Torvalds 			 * Error code returned is 1 if Reserve or Release
16181da177e4SLinus Torvalds 			 * failed or the input parameter is invalid
16191da177e4SLinus Torvalds 			 */
16201da177e4SLinus Torvalds 			if( status == 1 &&
16211da177e4SLinus Torvalds 				(cmd->cmnd[0] == RESERVE ||
16221da177e4SLinus Torvalds 					 cmd->cmnd[0] == RELEASE) ) {
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 				cmd->result |= (DID_ERROR << 16) |
16253d45cefcSHannes Reinecke 					SAM_STAT_RESERVATION_CONFLICT;
16261da177e4SLinus Torvalds 			}
16271da177e4SLinus Torvalds 			else
16281da177e4SLinus Torvalds #endif
16291da177e4SLinus Torvalds 				cmd->result |= (DID_BAD_TARGET << 16)|status;
16301da177e4SLinus Torvalds 		}
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		mega_free_scb(adapter, scb);
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 		/* Add Scsi_Command to end of completed queue */
16351da177e4SLinus Torvalds 		list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds /*
16411da177e4SLinus Torvalds  * mega_runpendq()
16421da177e4SLinus Torvalds  *
16431da177e4SLinus Torvalds  * Run through the list of completed requests and finish it
16441da177e4SLinus Torvalds  */
16451da177e4SLinus Torvalds static void
mega_rundoneq(adapter_t * adapter)16461da177e4SLinus Torvalds mega_rundoneq (adapter_t *adapter)
16471da177e4SLinus Torvalds {
1648fb597392SBart Van Assche 	struct megaraid_cmd_priv *cmd_priv;
16491da177e4SLinus Torvalds 
1650fb597392SBart Van Assche 	list_for_each_entry(cmd_priv, &adapter->completed_list, entry)
1651fb597392SBart Van Assche 		scsi_done(megaraid_to_scsi_cmd(cmd_priv));
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->completed_list);
16541da177e4SLinus Torvalds }
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /*
16581da177e4SLinus Torvalds  * Free a SCB structure
16591da177e4SLinus Torvalds  * Note: We assume the scsi commands associated with this scb is not free yet.
16601da177e4SLinus Torvalds  */
16611da177e4SLinus Torvalds static void
mega_free_scb(adapter_t * adapter,scb_t * scb)16621da177e4SLinus Torvalds mega_free_scb(adapter_t *adapter, scb_t *scb)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	switch( scb->dma_type ) {
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	case MEGA_DMA_TYPE_NONE:
16671da177e4SLinus Torvalds 		break;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	case MEGA_SGLIST:
16703f6270efSFUJITA Tomonori 		scsi_dma_unmap(scb->cmd);
16711da177e4SLinus Torvalds 		break;
16721da177e4SLinus Torvalds 	default:
16731da177e4SLinus Torvalds 		break;
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	/*
16771da177e4SLinus Torvalds 	 * Remove from the pending list
16781da177e4SLinus Torvalds 	 */
16791da177e4SLinus Torvalds 	list_del_init(&scb->list);
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	/* Link the scb back into free list */
16821da177e4SLinus Torvalds 	scb->state = SCB_FREE;
16831da177e4SLinus Torvalds 	scb->cmd = NULL;
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	list_add(&scb->list, &adapter->free_list);
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds static int
__mega_busywait_mbox(adapter_t * adapter)16901da177e4SLinus Torvalds __mega_busywait_mbox (adapter_t *adapter)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	volatile mbox_t *mbox = adapter->mbox;
16931da177e4SLinus Torvalds 	long counter;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	for (counter = 0; counter < 10000; counter++) {
16961da177e4SLinus Torvalds 		if (!mbox->m_in.busy)
16971da177e4SLinus Torvalds 			return 0;
1698e1fa0ceaSAmol Lad 		udelay(100);
1699e1fa0ceaSAmol Lad 		cond_resched();
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 	return -1;		/* give up after 1 second */
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds /*
17051da177e4SLinus Torvalds  * Copies data to SGLIST
17061da177e4SLinus Torvalds  * Note: For 64 bit cards, we need a minimum of one SG element for read/write
17071da177e4SLinus Torvalds  */
17081da177e4SLinus Torvalds static int
mega_build_sglist(adapter_t * adapter,scb_t * scb,u32 * buf,u32 * len)17091da177e4SLinus Torvalds mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
17101da177e4SLinus Torvalds {
17113f6270efSFUJITA Tomonori 	struct scatterlist *sg;
171291ebc1faSJohannes Thumshirn 	struct scsi_cmnd	*cmd;
17131da177e4SLinus Torvalds 	int	sgcnt;
17141da177e4SLinus Torvalds 	int	idx;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	cmd = scb->cmd;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	/*
17191da177e4SLinus Torvalds 	 * Copy Scatter-Gather list info into controller structure.
17201da177e4SLinus Torvalds 	 *
17211da177e4SLinus Torvalds 	 * The number of sg elements returned must not exceed our limit
17221da177e4SLinus Torvalds 	 */
17233f6270efSFUJITA Tomonori 	sgcnt = scsi_dma_map(cmd);
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	scb->dma_type = MEGA_SGLIST;
17261da177e4SLinus Torvalds 
17273f6270efSFUJITA Tomonori 	BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
17281da177e4SLinus Torvalds 
172951c928c3SJames Bottomley 	*len = 0;
173051c928c3SJames Bottomley 
1731d5e89385SFUJITA Tomonori 	if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1732d5e89385SFUJITA Tomonori 		sg = scsi_sglist(cmd);
1733d5e89385SFUJITA Tomonori 		scb->dma_h_bulkdata = sg_dma_address(sg);
1734d5e89385SFUJITA Tomonori 		*buf = (u32)scb->dma_h_bulkdata;
1735d5e89385SFUJITA Tomonori 		*len = sg_dma_len(sg);
1736d5e89385SFUJITA Tomonori 		return 0;
1737d5e89385SFUJITA Tomonori 	}
1738d5e89385SFUJITA Tomonori 
17393f6270efSFUJITA Tomonori 	scsi_for_each_sg(cmd, sg, sgcnt, idx) {
17401da177e4SLinus Torvalds 		if (adapter->has_64bit_addr) {
17413f6270efSFUJITA Tomonori 			scb->sgl64[idx].address = sg_dma_address(sg);
17423f6270efSFUJITA Tomonori 			*len += scb->sgl64[idx].length = sg_dma_len(sg);
17433f6270efSFUJITA Tomonori 		} else {
17443f6270efSFUJITA Tomonori 			scb->sgl[idx].address = sg_dma_address(sg);
17453f6270efSFUJITA Tomonori 			*len += scb->sgl[idx].length = sg_dma_len(sg);
17461da177e4SLinus Torvalds 		}
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	/* Reset pointer and length fields */
17501da177e4SLinus Torvalds 	*buf = scb->sgl_dma_addr;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	/* Return count of SG requests */
17531da177e4SLinus Torvalds 	return sgcnt;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds /*
17581da177e4SLinus Torvalds  * mega_8_to_40ld()
17591da177e4SLinus Torvalds  *
17601da177e4SLinus Torvalds  * takes all info in AdapterInquiry structure and puts it into ProductInfo and
17611da177e4SLinus Torvalds  * Enquiry3 structures for later use
17621da177e4SLinus Torvalds  */
17631da177e4SLinus Torvalds static void
mega_8_to_40ld(mraid_inquiry * inquiry,mega_inquiry3 * enquiry3,mega_product_info * product_info)17641da177e4SLinus Torvalds mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
17651da177e4SLinus Torvalds 		mega_product_info *product_info)
17661da177e4SLinus Torvalds {
17671da177e4SLinus Torvalds 	int i;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	product_info->max_commands = inquiry->adapter_info.max_commands;
17701da177e4SLinus Torvalds 	enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
17711da177e4SLinus Torvalds 	product_info->nchannels = inquiry->adapter_info.nchannels;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	for (i = 0; i < 4; i++) {
17741da177e4SLinus Torvalds 		product_info->fw_version[i] =
17751da177e4SLinus Torvalds 			inquiry->adapter_info.fw_version[i];
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 		product_info->bios_version[i] =
17781da177e4SLinus Torvalds 			inquiry->adapter_info.bios_version[i];
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 	enquiry3->cache_flush_interval =
17811da177e4SLinus Torvalds 		inquiry->adapter_info.cache_flush_interval;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	product_info->dram_size = inquiry->adapter_info.dram_size;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
17881da177e4SLinus Torvalds 		enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
17891da177e4SLinus Torvalds 		enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
17901da177e4SLinus Torvalds 		enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
17911da177e4SLinus Torvalds 	}
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
17941da177e4SLinus Torvalds 		enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
17951da177e4SLinus Torvalds }
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds static inline void
mega_free_sgl(adapter_t * adapter)17981da177e4SLinus Torvalds mega_free_sgl(adapter_t *adapter)
17991da177e4SLinus Torvalds {
18001da177e4SLinus Torvalds 	scb_t	*scb;
18011da177e4SLinus Torvalds 	int	i;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	for(i = 0; i < adapter->max_cmds; i++) {
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 		scb = &adapter->scb_list[i];
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 		if( scb->sgl64 ) {
1808ec090ef8SSuraj Upadhyay 			dma_free_coherent(&adapter->dev->dev,
18091da177e4SLinus Torvalds 					  sizeof(mega_sgl64) * adapter->sglen,
1810ec090ef8SSuraj Upadhyay 					  scb->sgl64, scb->sgl_dma_addr);
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 			scb->sgl64 = NULL;
18131da177e4SLinus Torvalds 		}
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 		if( scb->pthru ) {
1816ec090ef8SSuraj Upadhyay 			dma_free_coherent(&adapter->dev->dev,
1817ec090ef8SSuraj Upadhyay 					  sizeof(mega_passthru), scb->pthru,
1818ec090ef8SSuraj Upadhyay 					  scb->pthru_dma_addr);
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 			scb->pthru = NULL;
18211da177e4SLinus Torvalds 		}
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 		if( scb->epthru ) {
1824ec090ef8SSuraj Upadhyay 			dma_free_coherent(&adapter->dev->dev,
18251da177e4SLinus Torvalds 					  sizeof(mega_ext_passthru),
18261da177e4SLinus Torvalds 					  scb->epthru, scb->epthru_dma_addr);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 			scb->epthru = NULL;
18291da177e4SLinus Torvalds 		}
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	}
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /*
18361da177e4SLinus Torvalds  * Get information about the card/driver
18371da177e4SLinus Torvalds  */
18381da177e4SLinus Torvalds const char *
megaraid_info(struct Scsi_Host * host)18391da177e4SLinus Torvalds megaraid_info(struct Scsi_Host *host)
18401da177e4SLinus Torvalds {
18411da177e4SLinus Torvalds 	static char buffer[512];
18421da177e4SLinus Torvalds 	adapter_t *adapter;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	adapter = (adapter_t *)host->hostdata;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	sprintf (buffer,
18471da177e4SLinus Torvalds 		 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
18481da177e4SLinus Torvalds 		 adapter->fw_version, adapter->product_info.max_commands,
18491da177e4SLinus Torvalds 		 adapter->host->max_id, adapter->host->max_channel,
18501abf635dSHannes Reinecke 		 (u32)adapter->host->max_lun);
18511da177e4SLinus Torvalds 	return buffer;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*
18551da177e4SLinus Torvalds  * Abort a previous SCSI request. Only commands on the pending list can be
18561da177e4SLinus Torvalds  * aborted. All the commands issued to the F/W must complete.
18571da177e4SLinus Torvalds  */
18581da177e4SLinus Torvalds static int
megaraid_abort(struct scsi_cmnd * cmd)185991ebc1faSJohannes Thumshirn megaraid_abort(struct scsi_cmnd *cmd)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	adapter_t	*adapter;
18621da177e4SLinus Torvalds 	int		rval;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	adapter = (adapter_t *)cmd->device->host->hostdata;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	rval =  megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/*
18691da177e4SLinus Torvalds 	 * This is required here to complete any completed requests
18701da177e4SLinus Torvalds 	 * to be communicated over to the mid layer.
18711da177e4SLinus Torvalds 	 */
18721da177e4SLinus Torvalds 	mega_rundoneq(adapter);
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	return rval;
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds static int
megaraid_reset(struct scsi_cmnd * cmd)1879fa4c4966SJames Bottomley megaraid_reset(struct scsi_cmnd *cmd)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	adapter_t	*adapter;
18821da177e4SLinus Torvalds 	megacmd_t	mc;
18831da177e4SLinus Torvalds 	int		rval;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	adapter = (adapter_t *)cmd->device->host->hostdata;
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
18881da177e4SLinus Torvalds 	mc.cmd = MEGA_CLUSTER_CMD;
18891da177e4SLinus Torvalds 	mc.opcode = MEGA_RESET_RESERVATIONS;
18901da177e4SLinus Torvalds 
1891cb0258a2SChristoph Hellwig 	if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
18923b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "reservation reset failed\n");
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 	else {
18953b8a1ba3SBjorn Helgaas 		dev_info(&adapter->dev->dev, "reservation reset\n");
18961da177e4SLinus Torvalds 	}
18971da177e4SLinus Torvalds #endif
18981da177e4SLinus Torvalds 
1899fa4c4966SJames Bottomley 	spin_lock_irq(&adapter->lock);
1900fa4c4966SJames Bottomley 
19015bcd3bfbSHannes Reinecke 	rval =  megaraid_abort_and_reset(adapter, NULL, SCB_RESET);
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	/*
19041da177e4SLinus Torvalds 	 * This is required here to complete any completed requests
19051da177e4SLinus Torvalds 	 * to be communicated over to the mid layer.
19061da177e4SLinus Torvalds 	 */
19071da177e4SLinus Torvalds 	mega_rundoneq(adapter);
1908fa4c4966SJames Bottomley 	spin_unlock_irq(&adapter->lock);
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	return rval;
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds /**
19141da177e4SLinus Torvalds  * megaraid_abort_and_reset()
19152b46e5c1SDamien Le Moal  * @adapter: megaraid soft state
19162b46e5c1SDamien Le Moal  * @cmd: scsi command to be aborted or reset
19172b46e5c1SDamien Le Moal  * @aor: abort or reset flag
19181da177e4SLinus Torvalds  *
19191da177e4SLinus Torvalds  * Try to locate the scsi command in the pending queue. If found and is not
19201da177e4SLinus Torvalds  * issued to the controller, abort/reset it. Otherwise return failure
19211da177e4SLinus Torvalds  */
19221da177e4SLinus Torvalds static int
megaraid_abort_and_reset(adapter_t * adapter,struct scsi_cmnd * cmd,int aor)192391ebc1faSJohannes Thumshirn megaraid_abort_and_reset(adapter_t *adapter, struct scsi_cmnd *cmd, int aor)
19241da177e4SLinus Torvalds {
19251da177e4SLinus Torvalds 	struct list_head	*pos, *next;
19261da177e4SLinus Torvalds 	scb_t			*scb;
19271da177e4SLinus Torvalds 
1928f2d79aa1SHannes Reinecke 	if (aor == SCB_ABORT)
1929f2d79aa1SHannes Reinecke 		dev_warn(&adapter->dev->dev,
1930f2d79aa1SHannes Reinecke 			 "ABORTING cmd=%x <c=%d t=%d l=%d>\n",
19311da177e4SLinus Torvalds 			 cmd->cmnd[0], cmd->device->channel,
19329cb78c16SHannes Reinecke 			 cmd->device->id, (u32)cmd->device->lun);
1933f2d79aa1SHannes Reinecke 	else
1934f2d79aa1SHannes Reinecke 		dev_warn(&adapter->dev->dev, "RESETTING\n");
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	if(list_empty(&adapter->pending_list))
1937b6c92b7eSHannes Reinecke 		return FAILED;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &adapter->pending_list) {
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 		scb = list_entry(pos, scb_t, list);
19421da177e4SLinus Torvalds 
19435bcd3bfbSHannes Reinecke 		if (!cmd || scb->cmd == cmd) { /* Found command */
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 			scb->state |= aor;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 			/*
1948d41ad938SNick Andrew 			 * Check if this command has firmware ownership. If
1949d41ad938SNick Andrew 			 * yes, we cannot reset this command. Whenever f/w
19501da177e4SLinus Torvalds 			 * completes this command, we will return appropriate
19511da177e4SLinus Torvalds 			 * status from ISR.
19521da177e4SLinus Torvalds 			 */
19531da177e4SLinus Torvalds 			if( scb->state & SCB_ISSUED ) {
19541da177e4SLinus Torvalds 
19553b8a1ba3SBjorn Helgaas 				dev_warn(&adapter->dev->dev,
19563b8a1ba3SBjorn Helgaas 					"%s[%x], fw owner\n",
19571da177e4SLinus Torvalds 					(aor==SCB_ABORT) ? "ABORTING":"RESET",
19585cd049a5SChristoph Hellwig 					scb->idx);
19591da177e4SLinus Torvalds 
1960b6c92b7eSHannes Reinecke 				return FAILED;
19611da177e4SLinus Torvalds 			}
19621da177e4SLinus Torvalds 			/*
19631da177e4SLinus Torvalds 			 * Not yet issued! Remove from the pending
19641da177e4SLinus Torvalds 			 * list
19651da177e4SLinus Torvalds 			 */
19663b8a1ba3SBjorn Helgaas 			dev_warn(&adapter->dev->dev,
19673b8a1ba3SBjorn Helgaas 				 "%s-[%x], driver owner\n",
19685bcd3bfbSHannes Reinecke 				 (cmd) ? "ABORTING":"RESET",
19695cd049a5SChristoph Hellwig 				 scb->idx);
19701da177e4SLinus Torvalds 			mega_free_scb(adapter, scb);
19711da177e4SLinus Torvalds 
19725bcd3bfbSHannes Reinecke 			if (cmd) {
19731da177e4SLinus Torvalds 				cmd->result = (DID_ABORT << 16);
19741da177e4SLinus Torvalds 				list_add_tail(SCSI_LIST(cmd),
19751da177e4SLinus Torvalds 					      &adapter->completed_list);
19765bcd3bfbSHannes Reinecke 			}
19771da177e4SLinus Torvalds 
1978b6c92b7eSHannes Reinecke 			return SUCCESS;
19791da177e4SLinus Torvalds 		}
19801da177e4SLinus Torvalds 	}
19811da177e4SLinus Torvalds 
1982b6c92b7eSHannes Reinecke 	return FAILED;
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds static inline int
make_local_pdev(adapter_t * adapter,struct pci_dev ** pdev)19861da177e4SLinus Torvalds make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
19871da177e4SLinus Torvalds {
19888b1fce04SGu Zheng 	*pdev = pci_alloc_dev(NULL);
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	if( *pdev == NULL ) return -1;
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
19931da177e4SLinus Torvalds 
1994ec090ef8SSuraj Upadhyay 	if (dma_set_mask(&(*pdev)->dev, DMA_BIT_MASK(32)) != 0) {
19951da177e4SLinus Torvalds 		kfree(*pdev);
19961da177e4SLinus Torvalds 		return -1;
19971da177e4SLinus Torvalds 	}
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	return 0;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds static inline void
free_local_pdev(struct pci_dev * pdev)20031da177e4SLinus Torvalds free_local_pdev(struct pci_dev *pdev)
20041da177e4SLinus Torvalds {
20051da177e4SLinus Torvalds 	kfree(pdev);
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds /**
20091da177e4SLinus Torvalds  * mega_allocate_inquiry()
20102b46e5c1SDamien Le Moal  * @dma_handle: handle returned for dma address
20112b46e5c1SDamien Le Moal  * @pdev: handle to pci device
20121da177e4SLinus Torvalds  *
20131da177e4SLinus Torvalds  * allocates memory for inquiry structure
20141da177e4SLinus Torvalds  */
20151da177e4SLinus Torvalds static inline void *
mega_allocate_inquiry(dma_addr_t * dma_handle,struct pci_dev * pdev)20161da177e4SLinus Torvalds mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
20171da177e4SLinus Torvalds {
2018ec090ef8SSuraj Upadhyay 	return dma_alloc_coherent(&pdev->dev, sizeof(mega_inquiry3),
2019ec090ef8SSuraj Upadhyay 				  dma_handle, GFP_KERNEL);
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds static inline void
mega_free_inquiry(void * inquiry,dma_addr_t dma_handle,struct pci_dev * pdev)20241da177e4SLinus Torvalds mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
20251da177e4SLinus Torvalds {
2026ec090ef8SSuraj Upadhyay 	dma_free_coherent(&pdev->dev, sizeof(mega_inquiry3), inquiry,
2027ec090ef8SSuraj Upadhyay 			  dma_handle);
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
20321da177e4SLinus Torvalds /* Following code handles /proc fs  */
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds /**
2035c7f079caSDavid Howells  * proc_show_config()
20362b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
20372b46e5c1SDamien Le Moal  * @v: File iterator
20381da177e4SLinus Torvalds  *
20391da177e4SLinus Torvalds  * Display configuration information about the controller.
20401da177e4SLinus Torvalds  */
20411da177e4SLinus Torvalds static int
proc_show_config(struct seq_file * m,void * v)2042c7f079caSDavid Howells proc_show_config(struct seq_file *m, void *v)
20431da177e4SLinus Torvalds {
20441da177e4SLinus Torvalds 
2045c7f079caSDavid Howells 	adapter_t *adapter = m->private;
20461da177e4SLinus Torvalds 
2047c7f079caSDavid Howells 	seq_puts(m, MEGARAID_VERSION);
20481da177e4SLinus Torvalds 	if(adapter->product_info.product_name[0])
2049c7f079caSDavid Howells 		seq_printf(m, "%s\n", adapter->product_info.product_name);
20501da177e4SLinus Torvalds 
2051c7f079caSDavid Howells 	seq_puts(m, "Controller Type: ");
20521da177e4SLinus Torvalds 
2053c7f079caSDavid Howells 	if( adapter->flag & BOARD_MEMMAP )
2054c7f079caSDavid Howells 		seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2055c7f079caSDavid Howells 	else
2056c7f079caSDavid Howells 		seq_puts(m, "418/428/434\n");
20571da177e4SLinus Torvalds 
2058c7f079caSDavid Howells 	if(adapter->flag & BOARD_40LD)
2059c7f079caSDavid Howells 		seq_puts(m, "Controller Supports 40 Logical Drives\n");
20601da177e4SLinus Torvalds 
2061c7f079caSDavid Howells 	if(adapter->flag & BOARD_64BIT)
2062c7f079caSDavid Howells 		seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2063c7f079caSDavid Howells 	if( adapter->has_64bit_addr )
2064c7f079caSDavid Howells 		seq_puts(m, "Controller using 64-bit memory addressing\n");
2065c7f079caSDavid Howells 	else
2066c7f079caSDavid Howells 		seq_puts(m, "Controller is not using 64-bit memory addressing\n");
20671da177e4SLinus Torvalds 
2068c7f079caSDavid Howells 	seq_printf(m, "Base = %08lx, Irq = %d, ",
2069c7f079caSDavid Howells 		   adapter->base, adapter->host->irq);
20701da177e4SLinus Torvalds 
2071c7f079caSDavid Howells 	seq_printf(m, "Logical Drives = %d, Channels = %d\n",
20721da177e4SLinus Torvalds 		   adapter->numldrv, adapter->product_info.nchannels);
20731da177e4SLinus Torvalds 
2074c7f079caSDavid Howells 	seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
20751da177e4SLinus Torvalds 		   adapter->fw_version, adapter->bios_version,
20761da177e4SLinus Torvalds 		   adapter->product_info.dram_size);
20771da177e4SLinus Torvalds 
2078c7f079caSDavid Howells 	seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
20791da177e4SLinus Torvalds 		   adapter->product_info.max_commands, adapter->max_cmds);
20801da177e4SLinus Torvalds 
2081c7f079caSDavid Howells 	seq_printf(m, "support_ext_cdb    = %d\n", adapter->support_ext_cdb);
2082c7f079caSDavid Howells 	seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2083c7f079caSDavid Howells 	seq_printf(m, "boot_ldrv_enabled  = %d\n", adapter->boot_ldrv_enabled);
2084c7f079caSDavid Howells 	seq_printf(m, "boot_ldrv          = %d\n", adapter->boot_ldrv);
2085c7f079caSDavid Howells 	seq_printf(m, "boot_pdrv_enabled  = %d\n", adapter->boot_pdrv_enabled);
2086c7f079caSDavid Howells 	seq_printf(m, "boot_pdrv_ch       = %d\n", adapter->boot_pdrv_ch);
2087c7f079caSDavid Howells 	seq_printf(m, "boot_pdrv_tgt      = %d\n", adapter->boot_pdrv_tgt);
2088c7f079caSDavid Howells 	seq_printf(m, "quiescent          = %d\n",
20891da177e4SLinus Torvalds 		   atomic_read(&adapter->quiescent));
2090c7f079caSDavid Howells 	seq_printf(m, "has_cluster        = %d\n", adapter->has_cluster);
20911da177e4SLinus Torvalds 
2092c7f079caSDavid Howells 	seq_puts(m, "\nModule Parameters:\n");
2093c7f079caSDavid Howells 	seq_printf(m, "max_cmd_per_lun    = %d\n", max_cmd_per_lun);
2094c7f079caSDavid Howells 	seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2095c7f079caSDavid Howells 	return 0;
20961da177e4SLinus Torvalds }
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds /**
2099c7f079caSDavid Howells  * proc_show_stat()
21002b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
21012b46e5c1SDamien Le Moal  * @v: File iterator
21021da177e4SLinus Torvalds  *
2103c7f079caSDavid Howells  * Display statistical information about the I/O activity.
21041da177e4SLinus Torvalds  */
21051da177e4SLinus Torvalds static int
proc_show_stat(struct seq_file * m,void * v)2106c7f079caSDavid Howells proc_show_stat(struct seq_file *m, void *v)
21071da177e4SLinus Torvalds {
2108c7f079caSDavid Howells 	adapter_t *adapter = m->private;
2109c7f079caSDavid Howells #if MEGA_HAVE_STATS
21101da177e4SLinus Torvalds 	int	i;
2111c7f079caSDavid Howells #endif
21121da177e4SLinus Torvalds 
2113c7f079caSDavid Howells 	seq_puts(m, "Statistical Information for this controller\n");
2114c7f079caSDavid Howells 	seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
21151da177e4SLinus Torvalds #if MEGA_HAVE_STATS
21161da177e4SLinus Torvalds 	for(i = 0; i < adapter->numldrv; i++) {
2117c7f079caSDavid Howells 		seq_printf(m, "Logical Drive %d:\n", i);
2118c7f079caSDavid Howells 		seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
21191da177e4SLinus Torvalds 			   adapter->nreads[i], adapter->nwrites[i]);
2120c7f079caSDavid Howells 		seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
21211da177e4SLinus Torvalds 			   adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2122c7f079caSDavid Howells 		seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
21231da177e4SLinus Torvalds 			   adapter->rd_errors[i], adapter->wr_errors[i]);
21241da177e4SLinus Torvalds 	}
21251da177e4SLinus Torvalds #else
2126c7f079caSDavid Howells 	seq_puts(m, "IO and error counters not compiled in driver.\n");
21271da177e4SLinus Torvalds #endif
2128c7f079caSDavid Howells 	return 0;
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds /**
2133c7f079caSDavid Howells  * proc_show_mbox()
21342b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
21352b46e5c1SDamien Le Moal  * @v: File iterator
21361da177e4SLinus Torvalds  *
21371da177e4SLinus Torvalds  * Display mailbox information for the last command issued. This information
21381da177e4SLinus Torvalds  * is good for debugging.
21391da177e4SLinus Torvalds  */
21401da177e4SLinus Torvalds static int
proc_show_mbox(struct seq_file * m,void * v)2141c7f079caSDavid Howells proc_show_mbox(struct seq_file *m, void *v)
21421da177e4SLinus Torvalds {
2143c7f079caSDavid Howells 	adapter_t	*adapter = m->private;
21441da177e4SLinus Torvalds 	volatile mbox_t	*mbox = adapter->mbox;
21451da177e4SLinus Torvalds 
2146c7f079caSDavid Howells 	seq_puts(m, "Contents of Mail Box Structure\n");
2147c7f079caSDavid Howells 	seq_printf(m, "  Fw Command   = 0x%02x\n", mbox->m_out.cmd);
2148c7f079caSDavid Howells 	seq_printf(m, "  Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2149c7f079caSDavid Howells 	seq_printf(m, "  No of Sectors= %04d\n", mbox->m_out.numsectors);
2150c7f079caSDavid Howells 	seq_printf(m, "  LBA          = 0x%02x\n", mbox->m_out.lba);
2151c7f079caSDavid Howells 	seq_printf(m, "  DTA          = 0x%08x\n", mbox->m_out.xferaddr);
2152c7f079caSDavid Howells 	seq_printf(m, "  Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2153c7f079caSDavid Howells 	seq_printf(m, "  No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2154c7f079caSDavid Howells 	seq_printf(m, "  Busy         = %01x\n", mbox->m_in.busy);
2155c7f079caSDavid Howells 	seq_printf(m, "  Status       = 0x%02x\n", mbox->m_in.status);
2156c7f079caSDavid Howells 	return 0;
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds /**
2161c7f079caSDavid Howells  * proc_show_rebuild_rate()
21622b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
21632b46e5c1SDamien Le Moal  * @v: File iterator
21641da177e4SLinus Torvalds  *
21651da177e4SLinus Torvalds  * Display current rebuild rate
21661da177e4SLinus Torvalds  */
21671da177e4SLinus Torvalds static int
proc_show_rebuild_rate(struct seq_file * m,void * v)2168c7f079caSDavid Howells proc_show_rebuild_rate(struct seq_file *m, void *v)
21691da177e4SLinus Torvalds {
2170c7f079caSDavid Howells 	adapter_t	*adapter = m->private;
21711da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
21721da177e4SLinus Torvalds 	caddr_t		inquiry;
21731da177e4SLinus Torvalds 	struct pci_dev	*pdev;
21741da177e4SLinus Torvalds 
2175c7f079caSDavid Howells 	if( make_local_pdev(adapter, &pdev) != 0 )
2176c7f079caSDavid Howells 		return 0;
21771da177e4SLinus Torvalds 
2178c7f079caSDavid Howells 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2179c7f079caSDavid Howells 		goto free_pdev;
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
2182c7f079caSDavid Howells 		seq_puts(m, "Adapter inquiry failed.\n");
21833b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "inquiry failed\n");
2184c7f079caSDavid Howells 		goto free_inquiry;
21851da177e4SLinus Torvalds 	}
21861da177e4SLinus Torvalds 
2187c7f079caSDavid Howells 	if( adapter->flag & BOARD_40LD )
2188c7f079caSDavid Howells 		seq_printf(m, "Rebuild Rate: [%d%%]\n",
21891da177e4SLinus Torvalds 			   ((mega_inquiry3 *)inquiry)->rebuild_rate);
2190c7f079caSDavid Howells 	else
2191c7f079caSDavid Howells 		seq_printf(m, "Rebuild Rate: [%d%%]\n",
21921da177e4SLinus Torvalds 			((mraid_ext_inquiry *)
21931da177e4SLinus Torvalds 			 inquiry)->raid_inq.adapter_info.rebuild_rate);
21941da177e4SLinus Torvalds 
2195c7f079caSDavid Howells free_inquiry:
21961da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
2197c7f079caSDavid Howells free_pdev:
21981da177e4SLinus Torvalds 	free_local_pdev(pdev);
2199c7f079caSDavid Howells 	return 0;
22001da177e4SLinus Torvalds }
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds /**
2204c7f079caSDavid Howells  * proc_show_battery()
22052b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
22062b46e5c1SDamien Le Moal  * @v: File iterator
22071da177e4SLinus Torvalds  *
22081da177e4SLinus Torvalds  * Display information about the battery module on the controller.
22091da177e4SLinus Torvalds  */
22101da177e4SLinus Torvalds static int
proc_show_battery(struct seq_file * m,void * v)2211c7f079caSDavid Howells proc_show_battery(struct seq_file *m, void *v)
22121da177e4SLinus Torvalds {
2213c7f079caSDavid Howells 	adapter_t	*adapter = m->private;
22141da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
22151da177e4SLinus Torvalds 	caddr_t		inquiry;
22161da177e4SLinus Torvalds 	struct pci_dev	*pdev;
2217c7f079caSDavid Howells 	u8	battery_status;
22181da177e4SLinus Torvalds 
2219c7f079caSDavid Howells 	if( make_local_pdev(adapter, &pdev) != 0 )
2220c7f079caSDavid Howells 		return 0;
22211da177e4SLinus Torvalds 
2222c7f079caSDavid Howells 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2223c7f079caSDavid Howells 		goto free_pdev;
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
222691c40f24SRasmus Villemoes 		seq_puts(m, "Adapter inquiry failed.\n");
22273b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "inquiry failed\n");
2228c7f079caSDavid Howells 		goto free_inquiry;
22291da177e4SLinus Torvalds 	}
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
22321da177e4SLinus Torvalds 		battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 	else {
22351da177e4SLinus Torvalds 		battery_status = ((mraid_ext_inquiry *)inquiry)->
22361da177e4SLinus Torvalds 			raid_inq.adapter_info.battery_status;
22371da177e4SLinus Torvalds 	}
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	/*
22401da177e4SLinus Torvalds 	 * Decode the battery status
22411da177e4SLinus Torvalds 	 */
2242c7f079caSDavid Howells 	seq_printf(m, "Battery Status:[%d]", battery_status);
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 	if(battery_status == MEGA_BATT_CHARGE_DONE)
2245c7f079caSDavid Howells 		seq_puts(m, " Charge Done");
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_MODULE_MISSING)
2248c7f079caSDavid Howells 		seq_puts(m, " Module Missing");
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2251c7f079caSDavid Howells 		seq_puts(m, " Low Voltage");
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_TEMP_HIGH)
2254c7f079caSDavid Howells 		seq_puts(m, " Temperature High");
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_PACK_MISSING)
2257c7f079caSDavid Howells 		seq_puts(m, " Pack Missing");
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_CHARGE_INPROG)
2260c7f079caSDavid Howells 		seq_puts(m, " Charge In-progress");
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_CHARGE_FAIL)
2263c7f079caSDavid Howells 		seq_puts(m, " Charge Fail");
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2266c7f079caSDavid Howells 		seq_puts(m, " Cycles Exceeded");
22671da177e4SLinus Torvalds 
2268c7f079caSDavid Howells 	seq_putc(m, '\n');
22691da177e4SLinus Torvalds 
2270c7f079caSDavid Howells free_inquiry:
22711da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
2272c7f079caSDavid Howells free_pdev:
22731da177e4SLinus Torvalds 	free_local_pdev(pdev);
2274c7f079caSDavid Howells 	return 0;
22751da177e4SLinus Torvalds }
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 
2278c7f079caSDavid Howells /*
2279c7f079caSDavid Howells  * Display scsi inquiry
22801da177e4SLinus Torvalds  */
2281c7f079caSDavid Howells static void
mega_print_inquiry(struct seq_file * m,char * scsi_inq)2282c7f079caSDavid Howells mega_print_inquiry(struct seq_file *m, char *scsi_inq)
22831da177e4SLinus Torvalds {
2284c7f079caSDavid Howells 	int	i;
22851da177e4SLinus Torvalds 
2286c7f079caSDavid Howells 	seq_puts(m, "  Vendor: ");
2287c7f079caSDavid Howells 	seq_write(m, scsi_inq + 8, 8);
2288c7f079caSDavid Howells 	seq_puts(m, "  Model: ");
2289c7f079caSDavid Howells 	seq_write(m, scsi_inq + 16, 16);
2290c7f079caSDavid Howells 	seq_puts(m, "  Rev: ");
2291c7f079caSDavid Howells 	seq_write(m, scsi_inq + 32, 4);
2292c7f079caSDavid Howells 	seq_putc(m, '\n');
22931da177e4SLinus Torvalds 
2294c7f079caSDavid Howells 	i = scsi_inq[0] & 0x1f;
2295c7f079caSDavid Howells 	seq_printf(m, "  Type:   %s ", scsi_device_type(i));
2296c7f079caSDavid Howells 
2297c7f079caSDavid Howells 	seq_printf(m, "                 ANSI SCSI revision: %02x",
2298c7f079caSDavid Howells 		   scsi_inq[2] & 0x07);
2299c7f079caSDavid Howells 
2300c7f079caSDavid Howells 	if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2301c7f079caSDavid Howells 		seq_puts(m, " CCS\n");
2302c7f079caSDavid Howells 	else
2303c7f079caSDavid Howells 		seq_putc(m, '\n');
23041da177e4SLinus Torvalds }
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds /**
2307c7f079caSDavid Howells  * proc_show_pdrv()
23082b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
23092b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
23102b46e5c1SDamien Le Moal  * @channel: channel
23111da177e4SLinus Torvalds  *
23121da177e4SLinus Torvalds  * Display information about the physical drives.
23131da177e4SLinus Torvalds  */
23141da177e4SLinus Torvalds static int
proc_show_pdrv(struct seq_file * m,adapter_t * adapter,int channel)2315c7f079caSDavid Howells proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
23181da177e4SLinus Torvalds 	char		*scsi_inq;
23191da177e4SLinus Torvalds 	dma_addr_t	scsi_inq_dma_handle;
23201da177e4SLinus Torvalds 	caddr_t		inquiry;
23211da177e4SLinus Torvalds 	struct pci_dev	*pdev;
23221da177e4SLinus Torvalds 	u8	*pdrv_state;
23231da177e4SLinus Torvalds 	u8	state;
23241da177e4SLinus Torvalds 	int	tgt;
23251da177e4SLinus Torvalds 	int	max_channels;
23261da177e4SLinus Torvalds 	int	i;
23271da177e4SLinus Torvalds 
2328c7f079caSDavid Howells 	if( make_local_pdev(adapter, &pdev) != 0 )
2329c7f079caSDavid Howells 		return 0;
23301da177e4SLinus Torvalds 
2331c7f079caSDavid Howells 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
23321da177e4SLinus Torvalds 		goto free_pdev;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
2335c7f079caSDavid Howells 		seq_puts(m, "Adapter inquiry failed.\n");
23363b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "inquiry failed\n");
23371da177e4SLinus Torvalds 		goto free_inquiry;
23381da177e4SLinus Torvalds 	}
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 
2341ec090ef8SSuraj Upadhyay 	scsi_inq = dma_alloc_coherent(&pdev->dev, 256, &scsi_inq_dma_handle,
2342ec090ef8SSuraj Upadhyay 				      GFP_KERNEL);
23431da177e4SLinus Torvalds 	if( scsi_inq == NULL ) {
2344c7f079caSDavid Howells 		seq_puts(m, "memory not available for scsi inq.\n");
23451da177e4SLinus Torvalds 		goto free_inquiry;
23461da177e4SLinus Torvalds 	}
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
23491da177e4SLinus Torvalds 		pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
23501da177e4SLinus Torvalds 	}
23511da177e4SLinus Torvalds 	else {
23521da177e4SLinus Torvalds 		pdrv_state = ((mraid_ext_inquiry *)inquiry)->
23531da177e4SLinus Torvalds 			raid_inq.pdrv_info.pdrv_state;
23541da177e4SLinus Torvalds 	}
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 	max_channels = adapter->product_info.nchannels;
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	if( channel >= max_channels ) {
23591da177e4SLinus Torvalds 		goto free_pci;
23601da177e4SLinus Torvalds 	}
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 		i = channel*16 + tgt;
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 		state = *(pdrv_state + i);
23671da177e4SLinus Torvalds 		switch( state & 0x0F ) {
23681da177e4SLinus Torvalds 		case PDRV_ONLINE:
2369c7f079caSDavid Howells 			seq_printf(m, "Channel:%2d Id:%2d State: Online",
23701da177e4SLinus Torvalds 				   channel, tgt);
23711da177e4SLinus Torvalds 			break;
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 		case PDRV_FAILED:
2374c7f079caSDavid Howells 			seq_printf(m, "Channel:%2d Id:%2d State: Failed",
23751da177e4SLinus Torvalds 				   channel, tgt);
23761da177e4SLinus Torvalds 			break;
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 		case PDRV_RBLD:
2379c7f079caSDavid Howells 			seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
23801da177e4SLinus Torvalds 				   channel, tgt);
23811da177e4SLinus Torvalds 			break;
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 		case PDRV_HOTSPARE:
2384c7f079caSDavid Howells 			seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
23851da177e4SLinus Torvalds 				   channel, tgt);
23861da177e4SLinus Torvalds 			break;
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 		default:
2389c7f079caSDavid Howells 			seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
23901da177e4SLinus Torvalds 				   channel, tgt);
23911da177e4SLinus Torvalds 			break;
23921da177e4SLinus Torvalds 		}
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 		/*
23951da177e4SLinus Torvalds 		 * This interface displays inquiries for disk drives
23961da177e4SLinus Torvalds 		 * only. Inquries for logical drives and non-disk
23971da177e4SLinus Torvalds 		 * devices are available through /proc/scsi/scsi
23981da177e4SLinus Torvalds 		 */
23991da177e4SLinus Torvalds 		memset(scsi_inq, 0, 256);
24001da177e4SLinus Torvalds 		if( mega_internal_dev_inquiry(adapter, channel, tgt,
24011da177e4SLinus Torvalds 				scsi_inq_dma_handle) ||
24021da177e4SLinus Torvalds 				(scsi_inq[0] & 0x1F) != TYPE_DISK ) {
24031da177e4SLinus Torvalds 			continue;
24041da177e4SLinus Torvalds 		}
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds 		/*
24071da177e4SLinus Torvalds 		 * Check for overflow. We print less than 240
24081da177e4SLinus Torvalds 		 * characters for inquiry
24091da177e4SLinus Torvalds 		 */
2410c7f079caSDavid Howells 		seq_puts(m, ".\n");
2411c7f079caSDavid Howells 		mega_print_inquiry(m, scsi_inq);
24121da177e4SLinus Torvalds 	}
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds free_pci:
2415ec090ef8SSuraj Upadhyay 	dma_free_coherent(&pdev->dev, 256, scsi_inq, scsi_inq_dma_handle);
24161da177e4SLinus Torvalds free_inquiry:
24171da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
24181da177e4SLinus Torvalds free_pdev:
24191da177e4SLinus Torvalds 	free_local_pdev(pdev);
2420c7f079caSDavid Howells 	return 0;
24211da177e4SLinus Torvalds }
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds /**
2424c7f079caSDavid Howells  * proc_show_pdrv_ch0()
24252b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
24262b46e5c1SDamien Le Moal  * @v: File iterator
24271da177e4SLinus Torvalds  *
2428c7f079caSDavid Howells  * Display information about the physical drives on physical channel 0.
24291da177e4SLinus Torvalds  */
24301da177e4SLinus Torvalds static int
proc_show_pdrv_ch0(struct seq_file * m,void * v)2431c7f079caSDavid Howells proc_show_pdrv_ch0(struct seq_file *m, void *v)
24321da177e4SLinus Torvalds {
2433c7f079caSDavid Howells 	return proc_show_pdrv(m, m->private, 0);
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds /**
2438c7f079caSDavid Howells  * proc_show_pdrv_ch1()
24392b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
24402b46e5c1SDamien Le Moal  * @v: File iterator
24411da177e4SLinus Torvalds  *
2442c7f079caSDavid Howells  * Display information about the physical drives on physical channel 1.
24431da177e4SLinus Torvalds  */
24441da177e4SLinus Torvalds static int
proc_show_pdrv_ch1(struct seq_file * m,void * v)2445c7f079caSDavid Howells proc_show_pdrv_ch1(struct seq_file *m, void *v)
24461da177e4SLinus Torvalds {
2447c7f079caSDavid Howells 	return proc_show_pdrv(m, m->private, 1);
24481da177e4SLinus Torvalds }
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds /**
2452c7f079caSDavid Howells  * proc_show_pdrv_ch2()
24532b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
24542b46e5c1SDamien Le Moal  * @v: File iterator
24551da177e4SLinus Torvalds  *
2456c7f079caSDavid Howells  * Display information about the physical drives on physical channel 2.
24571da177e4SLinus Torvalds  */
24581da177e4SLinus Torvalds static int
proc_show_pdrv_ch2(struct seq_file * m,void * v)2459c7f079caSDavid Howells proc_show_pdrv_ch2(struct seq_file *m, void *v)
24601da177e4SLinus Torvalds {
2461c7f079caSDavid Howells 	return proc_show_pdrv(m, m->private, 2);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds /**
2466c7f079caSDavid Howells  * proc_show_pdrv_ch3()
24672b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
24682b46e5c1SDamien Le Moal  * @v: File iterator
24691da177e4SLinus Torvalds  *
2470c7f079caSDavid Howells  * Display information about the physical drives on physical channel 3.
24711da177e4SLinus Torvalds  */
24721da177e4SLinus Torvalds static int
proc_show_pdrv_ch3(struct seq_file * m,void * v)2473c7f079caSDavid Howells proc_show_pdrv_ch3(struct seq_file *m, void *v)
24741da177e4SLinus Torvalds {
2475c7f079caSDavid Howells 	return proc_show_pdrv(m, m->private, 3);
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds /**
2480c7f079caSDavid Howells  * proc_show_rdrv()
24812b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
24822b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
24832b46e5c1SDamien Le Moal  * @start: starting logical drive to display
24842b46e5c1SDamien Le Moal  * @end: ending logical drive to display
24851da177e4SLinus Torvalds  *
24861da177e4SLinus Torvalds  * We do not print the inquiry information since its already available through
24871da177e4SLinus Torvalds  * /proc/scsi/scsi interface
24881da177e4SLinus Torvalds  */
24891da177e4SLinus Torvalds static int
proc_show_rdrv(struct seq_file * m,adapter_t * adapter,int start,int end)2490c7f079caSDavid Howells proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
24911da177e4SLinus Torvalds {
24921da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
24931da177e4SLinus Torvalds 	logdrv_param	*lparam;
24941da177e4SLinus Torvalds 	megacmd_t	mc;
24951da177e4SLinus Torvalds 	char		*disk_array;
24961da177e4SLinus Torvalds 	dma_addr_t	disk_array_dma_handle;
24971da177e4SLinus Torvalds 	caddr_t		inquiry;
24981da177e4SLinus Torvalds 	struct pci_dev	*pdev;
24991da177e4SLinus Torvalds 	u8	*rdrv_state;
25001da177e4SLinus Torvalds 	int	num_ldrv;
25011da177e4SLinus Torvalds 	u32	array_sz;
25021da177e4SLinus Torvalds 	int	i;
25031da177e4SLinus Torvalds 
2504c7f079caSDavid Howells 	if( make_local_pdev(adapter, &pdev) != 0 )
2505c7f079caSDavid Howells 		return 0;
25061da177e4SLinus Torvalds 
2507c7f079caSDavid Howells 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2508c7f079caSDavid Howells 		goto free_pdev;
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
2511c7f079caSDavid Howells 		seq_puts(m, "Adapter inquiry failed.\n");
25123b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "inquiry failed\n");
2513c7f079caSDavid Howells 		goto free_inquiry;
25141da177e4SLinus Torvalds 	}
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	memset(&mc, 0, sizeof(megacmd_t));
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
25191da177e4SLinus Torvalds 		array_sz = sizeof(disk_array_40ld);
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 		rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 		num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
25241da177e4SLinus Torvalds 	}
25251da177e4SLinus Torvalds 	else {
25261da177e4SLinus Torvalds 		array_sz = sizeof(disk_array_8ld);
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 		rdrv_state = ((mraid_ext_inquiry *)inquiry)->
25291da177e4SLinus Torvalds 			raid_inq.logdrv_info.ldrv_state;
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 		num_ldrv = ((mraid_ext_inquiry *)inquiry)->
25321da177e4SLinus Torvalds 			raid_inq.logdrv_info.num_ldrv;
25331da177e4SLinus Torvalds 	}
25341da177e4SLinus Torvalds 
2535ec090ef8SSuraj Upadhyay 	disk_array = dma_alloc_coherent(&pdev->dev, array_sz,
2536ec090ef8SSuraj Upadhyay 					&disk_array_dma_handle, GFP_KERNEL);
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	if( disk_array == NULL ) {
2539c7f079caSDavid Howells 		seq_puts(m, "memory not available.\n");
2540c7f079caSDavid Howells 		goto free_inquiry;
25411da177e4SLinus Torvalds 	}
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 	mc.xferaddr = (u32)disk_array_dma_handle;
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
25461da177e4SLinus Torvalds 		mc.cmd = FC_NEW_CONFIG;
25471da177e4SLinus Torvalds 		mc.opcode = OP_DCMD_READ_CONFIG;
25481da177e4SLinus Torvalds 
2549cb0258a2SChristoph Hellwig 		if( mega_internal_command(adapter, &mc, NULL) ) {
2550c7f079caSDavid Howells 			seq_puts(m, "40LD read config failed.\n");
2551c7f079caSDavid Howells 			goto free_pci;
25521da177e4SLinus Torvalds 		}
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	}
25551da177e4SLinus Torvalds 	else {
25561da177e4SLinus Torvalds 		mc.cmd = NEW_READ_CONFIG_8LD;
25571da177e4SLinus Torvalds 
2558cb0258a2SChristoph Hellwig 		if( mega_internal_command(adapter, &mc, NULL) ) {
25591da177e4SLinus Torvalds 			mc.cmd = READ_CONFIG_8LD;
2560c7f079caSDavid Howells 			if( mega_internal_command(adapter, &mc, NULL) ) {
2561c7f079caSDavid Howells 				seq_puts(m, "8LD read config failed.\n");
2562c7f079caSDavid Howells 				goto free_pci;
25631da177e4SLinus Torvalds 			}
25641da177e4SLinus Torvalds 		}
25651da177e4SLinus Torvalds 	}
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 	for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 		if( adapter->flag & BOARD_40LD ) {
25701da177e4SLinus Torvalds 			lparam =
25711da177e4SLinus Torvalds 			&((disk_array_40ld *)disk_array)->ldrv[i].lparam;
25721da177e4SLinus Torvalds 		}
25731da177e4SLinus Torvalds 		else {
25741da177e4SLinus Torvalds 			lparam =
25751da177e4SLinus Torvalds 			&((disk_array_8ld *)disk_array)->ldrv[i].lparam;
25761da177e4SLinus Torvalds 		}
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 		/*
25791da177e4SLinus Torvalds 		 * Check for overflow. We print less than 240 characters for
25801da177e4SLinus Torvalds 		 * information about each logical drive.
25811da177e4SLinus Torvalds 		 */
2582c7f079caSDavid Howells 		seq_printf(m, "Logical drive:%2d:, ", i);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 		switch( rdrv_state[i] & 0x0F ) {
25851da177e4SLinus Torvalds 		case RDRV_OFFLINE:
2586c7f079caSDavid Howells 			seq_puts(m, "state: offline");
25871da177e4SLinus Torvalds 			break;
25881da177e4SLinus Torvalds 		case RDRV_DEGRADED:
2589c7f079caSDavid Howells 			seq_puts(m, "state: degraded");
25901da177e4SLinus Torvalds 			break;
25911da177e4SLinus Torvalds 		case RDRV_OPTIMAL:
2592c7f079caSDavid Howells 			seq_puts(m, "state: optimal");
25931da177e4SLinus Torvalds 			break;
25941da177e4SLinus Torvalds 		case RDRV_DELETED:
2595c7f079caSDavid Howells 			seq_puts(m, "state: deleted");
25961da177e4SLinus Torvalds 			break;
25971da177e4SLinus Torvalds 		default:
2598c7f079caSDavid Howells 			seq_puts(m, "state: unknown");
25991da177e4SLinus Torvalds 			break;
26001da177e4SLinus Torvalds 		}
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 		/*
26031da177e4SLinus Torvalds 		 * Check if check consistency or initialization is going on
26041da177e4SLinus Torvalds 		 * for this logical drive.
26051da177e4SLinus Torvalds 		 */
2606c7f079caSDavid Howells 		if( (rdrv_state[i] & 0xF0) == 0x20 )
2607c7f079caSDavid Howells 			seq_puts(m, ", check-consistency in progress");
2608c7f079caSDavid Howells 		else if( (rdrv_state[i] & 0xF0) == 0x10 )
2609c7f079caSDavid Howells 			seq_puts(m, ", initialization in progress");
26101da177e4SLinus Torvalds 
2611c7f079caSDavid Howells 		seq_putc(m, '\n');
26121da177e4SLinus Torvalds 
2613c7f079caSDavid Howells 		seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2614c7f079caSDavid Howells 		seq_printf(m, "RAID level:%3d, ", lparam->level);
2615c7f079caSDavid Howells 		seq_printf(m, "Stripe size:%3d, ",
26161da177e4SLinus Torvalds 			   lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2617c7f079caSDavid Howells 		seq_printf(m, "Row size:%3d\n", lparam->row_size);
26181da177e4SLinus Torvalds 
2619c7f079caSDavid Howells 		seq_puts(m, "Read Policy: ");
26201da177e4SLinus Torvalds 		switch(lparam->read_ahead) {
26211da177e4SLinus Torvalds 		case NO_READ_AHEAD:
2622c7f079caSDavid Howells 			seq_puts(m, "No read ahead, ");
26231da177e4SLinus Torvalds 			break;
26241da177e4SLinus Torvalds 		case READ_AHEAD:
2625c7f079caSDavid Howells 			seq_puts(m, "Read ahead, ");
26261da177e4SLinus Torvalds 			break;
26271da177e4SLinus Torvalds 		case ADAP_READ_AHEAD:
2628c7f079caSDavid Howells 			seq_puts(m, "Adaptive, ");
26291da177e4SLinus Torvalds 			break;
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 		}
26321da177e4SLinus Torvalds 
2633c7f079caSDavid Howells 		seq_puts(m, "Write Policy: ");
26341da177e4SLinus Torvalds 		switch(lparam->write_mode) {
26351da177e4SLinus Torvalds 		case WRMODE_WRITE_THRU:
2636c7f079caSDavid Howells 			seq_puts(m, "Write thru, ");
26371da177e4SLinus Torvalds 			break;
26381da177e4SLinus Torvalds 		case WRMODE_WRITE_BACK:
2639c7f079caSDavid Howells 			seq_puts(m, "Write back, ");
26401da177e4SLinus Torvalds 			break;
26411da177e4SLinus Torvalds 		}
26421da177e4SLinus Torvalds 
2643c7f079caSDavid Howells 		seq_puts(m, "Cache Policy: ");
26441da177e4SLinus Torvalds 		switch(lparam->direct_io) {
26451da177e4SLinus Torvalds 		case CACHED_IO:
2646c7f079caSDavid Howells 			seq_puts(m, "Cached IO\n\n");
26471da177e4SLinus Torvalds 			break;
26481da177e4SLinus Torvalds 		case DIRECT_IO:
2649c7f079caSDavid Howells 			seq_puts(m, "Direct IO\n\n");
26501da177e4SLinus Torvalds 			break;
26511da177e4SLinus Torvalds 		}
26521da177e4SLinus Torvalds 	}
26531da177e4SLinus Torvalds 
2654c7f079caSDavid Howells free_pci:
2655ec090ef8SSuraj Upadhyay 	dma_free_coherent(&pdev->dev, array_sz, disk_array,
26561da177e4SLinus Torvalds 			  disk_array_dma_handle);
2657c7f079caSDavid Howells free_inquiry:
2658c7f079caSDavid Howells 	mega_free_inquiry(inquiry, dma_handle, pdev);
2659c7f079caSDavid Howells free_pdev:
26601da177e4SLinus Torvalds 	free_local_pdev(pdev);
2661c7f079caSDavid Howells 	return 0;
26621da177e4SLinus Torvalds }
2663c7f079caSDavid Howells 
2664c7f079caSDavid Howells /**
2665c7f079caSDavid Howells  * proc_show_rdrv_10()
26662b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
26672b46e5c1SDamien Le Moal  * @v: File iterator
2668c7f079caSDavid Howells  *
2669c7f079caSDavid Howells  * Display real time information about the logical drives 0 through 9.
2670c7f079caSDavid Howells  */
2671c7f079caSDavid Howells static int
proc_show_rdrv_10(struct seq_file * m,void * v)2672c7f079caSDavid Howells proc_show_rdrv_10(struct seq_file *m, void *v)
2673c7f079caSDavid Howells {
2674c7f079caSDavid Howells 	return proc_show_rdrv(m, m->private, 0, 9);
2675c7f079caSDavid Howells }
2676c7f079caSDavid Howells 
2677c7f079caSDavid Howells 
2678c7f079caSDavid Howells /**
2679c7f079caSDavid Howells  * proc_show_rdrv_20()
26802b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
26812b46e5c1SDamien Le Moal  * @v: File iterator
2682c7f079caSDavid Howells  *
2683c7f079caSDavid Howells  * Display real time information about the logical drives 0 through 9.
2684c7f079caSDavid Howells  */
2685c7f079caSDavid Howells static int
proc_show_rdrv_20(struct seq_file * m,void * v)2686c7f079caSDavid Howells proc_show_rdrv_20(struct seq_file *m, void *v)
2687c7f079caSDavid Howells {
2688c7f079caSDavid Howells 	return proc_show_rdrv(m, m->private, 10, 19);
2689c7f079caSDavid Howells }
2690c7f079caSDavid Howells 
2691c7f079caSDavid Howells 
2692c7f079caSDavid Howells /**
2693c7f079caSDavid Howells  * proc_show_rdrv_30()
26942b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
26952b46e5c1SDamien Le Moal  * @v: File iterator
2696c7f079caSDavid Howells  *
2697c7f079caSDavid Howells  * Display real time information about the logical drives 0 through 9.
2698c7f079caSDavid Howells  */
2699c7f079caSDavid Howells static int
proc_show_rdrv_30(struct seq_file * m,void * v)2700c7f079caSDavid Howells proc_show_rdrv_30(struct seq_file *m, void *v)
2701c7f079caSDavid Howells {
2702c7f079caSDavid Howells 	return proc_show_rdrv(m, m->private, 20, 29);
2703c7f079caSDavid Howells }
2704c7f079caSDavid Howells 
2705c7f079caSDavid Howells 
2706c7f079caSDavid Howells /**
2707c7f079caSDavid Howells  * proc_show_rdrv_40()
27082b46e5c1SDamien Le Moal  * @m: Synthetic file construction data
27092b46e5c1SDamien Le Moal  * @v: File iterator
2710c7f079caSDavid Howells  *
2711c7f079caSDavid Howells  * Display real time information about the logical drives 0 through 9.
2712c7f079caSDavid Howells  */
2713c7f079caSDavid Howells static int
proc_show_rdrv_40(struct seq_file * m,void * v)2714c7f079caSDavid Howells proc_show_rdrv_40(struct seq_file *m, void *v)
2715c7f079caSDavid Howells {
2716c7f079caSDavid Howells 	return proc_show_rdrv(m, m->private, 30, 39);
2717c7f079caSDavid Howells }
2718c7f079caSDavid Howells 
2719c7f079caSDavid Howells /**
2720c7f079caSDavid Howells  * mega_create_proc_entry()
27212b46e5c1SDamien Le Moal  * @index: index in soft state array
27222b46e5c1SDamien Le Moal  * @parent: parent node for this /proc entry
2723c7f079caSDavid Howells  *
2724c7f079caSDavid Howells  * Creates /proc entries for our controllers.
2725c7f079caSDavid Howells  */
2726c7f079caSDavid Howells static void
mega_create_proc_entry(int index,struct proc_dir_entry * parent)2727c7f079caSDavid Howells mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2728c7f079caSDavid Howells {
2729c7f079caSDavid Howells 	adapter_t *adapter = hba_soft_state[index];
2730f7680becSChristoph Hellwig 	struct proc_dir_entry *dir;
2731c7f079caSDavid Howells 	u8 string[16];
2732c7f079caSDavid Howells 
2733c7f079caSDavid Howells 	sprintf(string, "hba%d", adapter->host->host_no);
2734f7680becSChristoph Hellwig 	dir = proc_mkdir_data(string, 0, parent, adapter);
2735c7f079caSDavid Howells 	if (!dir) {
27363b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "proc_mkdir failed\n");
2737c7f079caSDavid Howells 		return;
2738c7f079caSDavid Howells 	}
2739c7f079caSDavid Howells 
2740f7680becSChristoph Hellwig 	proc_create_single_data("config", S_IRUSR, dir,
2741f7680becSChristoph Hellwig 			proc_show_config, adapter);
2742f7680becSChristoph Hellwig 	proc_create_single_data("stat", S_IRUSR, dir,
2743f7680becSChristoph Hellwig 			proc_show_stat, adapter);
2744f7680becSChristoph Hellwig 	proc_create_single_data("mailbox", S_IRUSR, dir,
2745f7680becSChristoph Hellwig 			proc_show_mbox, adapter);
2746f7680becSChristoph Hellwig #if MEGA_HAVE_ENH_PROC
2747f7680becSChristoph Hellwig 	proc_create_single_data("rebuild-rate", S_IRUSR, dir,
2748f7680becSChristoph Hellwig 			proc_show_rebuild_rate, adapter);
2749f7680becSChristoph Hellwig 	proc_create_single_data("battery-status", S_IRUSR, dir,
2750f7680becSChristoph Hellwig 			proc_show_battery, adapter);
2751f7680becSChristoph Hellwig 	proc_create_single_data("diskdrives-ch0", S_IRUSR, dir,
2752f7680becSChristoph Hellwig 			proc_show_pdrv_ch0, adapter);
2753f7680becSChristoph Hellwig 	proc_create_single_data("diskdrives-ch1", S_IRUSR, dir,
2754f7680becSChristoph Hellwig 			proc_show_pdrv_ch1, adapter);
2755f7680becSChristoph Hellwig 	proc_create_single_data("diskdrives-ch2", S_IRUSR, dir,
2756f7680becSChristoph Hellwig 			proc_show_pdrv_ch2, adapter);
2757f7680becSChristoph Hellwig 	proc_create_single_data("diskdrives-ch3", S_IRUSR, dir,
2758f7680becSChristoph Hellwig 			proc_show_pdrv_ch3, adapter);
2759f7680becSChristoph Hellwig 	proc_create_single_data("raiddrives-0-9", S_IRUSR, dir,
2760f7680becSChristoph Hellwig 			proc_show_rdrv_10, adapter);
2761f7680becSChristoph Hellwig 	proc_create_single_data("raiddrives-10-19", S_IRUSR, dir,
2762f7680becSChristoph Hellwig 			proc_show_rdrv_20, adapter);
2763f7680becSChristoph Hellwig 	proc_create_single_data("raiddrives-20-29", S_IRUSR, dir,
2764f7680becSChristoph Hellwig 			proc_show_rdrv_30, adapter);
2765f7680becSChristoph Hellwig 	proc_create_single_data("raiddrives-30-39", S_IRUSR, dir,
2766f7680becSChristoph Hellwig 			proc_show_rdrv_40, adapter);
2767f7680becSChristoph Hellwig #endif
2768c7f079caSDavid Howells }
2769c7f079caSDavid Howells 
277084a3c97bSwalter harms #else
mega_create_proc_entry(int index,struct proc_dir_entry * parent)277184a3c97bSwalter harms static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
277284a3c97bSwalter harms {
277384a3c97bSwalter harms }
27741da177e4SLinus Torvalds #endif
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 
27772b46e5c1SDamien Le Moal /*
27781da177e4SLinus Torvalds  * megaraid_biosparam()
27791da177e4SLinus Torvalds  *
27801da177e4SLinus Torvalds  * Return the disk geometry for a particular disk
27811da177e4SLinus Torvalds  */
27821da177e4SLinus Torvalds static int
megaraid_biosparam(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int geom[])27831da177e4SLinus Torvalds megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
27841da177e4SLinus Torvalds 		    sector_t capacity, int geom[])
27851da177e4SLinus Torvalds {
27861da177e4SLinus Torvalds 	adapter_t	*adapter;
27871da177e4SLinus Torvalds 	int	heads;
27881da177e4SLinus Torvalds 	int	sectors;
27891da177e4SLinus Torvalds 	int	cylinders;
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 	/* Get pointer to host config structure */
27921da177e4SLinus Torvalds 	adapter = (adapter_t *)sdev->host->hostdata;
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds 	if (IS_RAID_CH(adapter, sdev->channel)) {
27951da177e4SLinus Torvalds 			/* Default heads (64) & sectors (32) */
27961da177e4SLinus Torvalds 			heads = 64;
27971da177e4SLinus Torvalds 			sectors = 32;
27981da177e4SLinus Torvalds 			cylinders = (ulong)capacity / (heads * sectors);
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds 			/*
28011da177e4SLinus Torvalds 			 * Handle extended translation size for logical drives
28021da177e4SLinus Torvalds 			 * > 1Gb
28031da177e4SLinus Torvalds 			 */
28041da177e4SLinus Torvalds 			if ((ulong)capacity >= 0x200000) {
28051da177e4SLinus Torvalds 				heads = 255;
28061da177e4SLinus Torvalds 				sectors = 63;
28071da177e4SLinus Torvalds 				cylinders = (ulong)capacity / (heads * sectors);
28081da177e4SLinus Torvalds 			}
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 			/* return result */
28111da177e4SLinus Torvalds 			geom[0] = heads;
28121da177e4SLinus Torvalds 			geom[1] = sectors;
28131da177e4SLinus Torvalds 			geom[2] = cylinders;
28141da177e4SLinus Torvalds 	}
28151da177e4SLinus Torvalds 	else {
2816a10183d7SChristoph Hellwig 		if (scsi_partsize(bdev, capacity, geom))
2817a10183d7SChristoph Hellwig 			return 0;
28181da177e4SLinus Torvalds 
28193b8a1ba3SBjorn Helgaas 		dev_info(&adapter->dev->dev,
28203b8a1ba3SBjorn Helgaas 			 "invalid partition on this disk on channel %d\n",
28211da177e4SLinus Torvalds 			 sdev->channel);
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 		/* Default heads (64) & sectors (32) */
28241da177e4SLinus Torvalds 		heads = 64;
28251da177e4SLinus Torvalds 		sectors = 32;
28261da177e4SLinus Torvalds 		cylinders = (ulong)capacity / (heads * sectors);
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds 		/* Handle extended translation size for logical drives > 1Gb */
28291da177e4SLinus Torvalds 		if ((ulong)capacity >= 0x200000) {
28301da177e4SLinus Torvalds 			heads = 255;
28311da177e4SLinus Torvalds 			sectors = 63;
28321da177e4SLinus Torvalds 			cylinders = (ulong)capacity / (heads * sectors);
28331da177e4SLinus Torvalds 		}
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds 		/* return result */
28361da177e4SLinus Torvalds 		geom[0] = heads;
28371da177e4SLinus Torvalds 		geom[1] = sectors;
28381da177e4SLinus Torvalds 		geom[2] = cylinders;
28391da177e4SLinus Torvalds 	}
28401da177e4SLinus Torvalds 
28411da177e4SLinus Torvalds 	return 0;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds /**
28451da177e4SLinus Torvalds  * mega_init_scb()
28462b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
28471da177e4SLinus Torvalds  *
28481da177e4SLinus Torvalds  * Allocate memory for the various pointers in the scb structures:
28491da177e4SLinus Torvalds  * scatter-gather list pointer, passthru and extended passthru structure
28501da177e4SLinus Torvalds  * pointers.
28511da177e4SLinus Torvalds  */
28521da177e4SLinus Torvalds static int
mega_init_scb(adapter_t * adapter)28531da177e4SLinus Torvalds mega_init_scb(adapter_t *adapter)
28541da177e4SLinus Torvalds {
28551da177e4SLinus Torvalds 	scb_t	*scb;
28561da177e4SLinus Torvalds 	int	i;
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 	for( i = 0; i < adapter->max_cmds; i++ ) {
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 		scb = &adapter->scb_list[i];
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds 		scb->sgl64 = NULL;
28631da177e4SLinus Torvalds 		scb->sgl = NULL;
28641da177e4SLinus Torvalds 		scb->pthru = NULL;
28651da177e4SLinus Torvalds 		scb->epthru = NULL;
28661da177e4SLinus Torvalds 	}
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	for( i = 0; i < adapter->max_cmds; i++ ) {
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 		scb = &adapter->scb_list[i];
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 		scb->idx = i;
28731da177e4SLinus Torvalds 
2874ec090ef8SSuraj Upadhyay 		scb->sgl64 = dma_alloc_coherent(&adapter->dev->dev,
28751da177e4SLinus Torvalds 						sizeof(mega_sgl64) * adapter->sglen,
2876ec090ef8SSuraj Upadhyay 						&scb->sgl_dma_addr, GFP_KERNEL);
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 		scb->sgl = (mega_sglist *)scb->sgl64;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 		if( !scb->sgl ) {
28813b8a1ba3SBjorn Helgaas 			dev_warn(&adapter->dev->dev, "RAID: Can't allocate sglist\n");
28821da177e4SLinus Torvalds 			mega_free_sgl(adapter);
28831da177e4SLinus Torvalds 			return -1;
28841da177e4SLinus Torvalds 		}
28851da177e4SLinus Torvalds 
2886ec090ef8SSuraj Upadhyay 		scb->pthru = dma_alloc_coherent(&adapter->dev->dev,
28871da177e4SLinus Torvalds 						sizeof(mega_passthru),
2888ec090ef8SSuraj Upadhyay 						&scb->pthru_dma_addr, GFP_KERNEL);
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds 		if( !scb->pthru ) {
28913b8a1ba3SBjorn Helgaas 			dev_warn(&adapter->dev->dev, "RAID: Can't allocate passthru\n");
28921da177e4SLinus Torvalds 			mega_free_sgl(adapter);
28931da177e4SLinus Torvalds 			return -1;
28941da177e4SLinus Torvalds 		}
28951da177e4SLinus Torvalds 
2896ec090ef8SSuraj Upadhyay 		scb->epthru = dma_alloc_coherent(&adapter->dev->dev,
28971da177e4SLinus Torvalds 						 sizeof(mega_ext_passthru),
2898ec090ef8SSuraj Upadhyay 						 &scb->epthru_dma_addr, GFP_KERNEL);
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 		if( !scb->epthru ) {
29013b8a1ba3SBjorn Helgaas 			dev_warn(&adapter->dev->dev,
29023b8a1ba3SBjorn Helgaas 				"Can't allocate extended passthru\n");
29031da177e4SLinus Torvalds 			mega_free_sgl(adapter);
29041da177e4SLinus Torvalds 			return -1;
29051da177e4SLinus Torvalds 		}
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 		scb->dma_type = MEGA_DMA_TYPE_NONE;
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 		/*
29111da177e4SLinus Torvalds 		 * Link to free list
29121da177e4SLinus Torvalds 		 * lock not required since we are loading the driver, so no
29131da177e4SLinus Torvalds 		 * commands possible right now.
29141da177e4SLinus Torvalds 		 */
29151da177e4SLinus Torvalds 		scb->state = SCB_FREE;
29161da177e4SLinus Torvalds 		scb->cmd = NULL;
29171da177e4SLinus Torvalds 		list_add(&scb->list, &adapter->free_list);
29181da177e4SLinus Torvalds 	}
29191da177e4SLinus Torvalds 
29201da177e4SLinus Torvalds 	return 0;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds /**
29251da177e4SLinus Torvalds  * megadev_open()
29262b46e5c1SDamien Le Moal  * @inode: unused
29272b46e5c1SDamien Le Moal  * @filep: unused
29281da177e4SLinus Torvalds  *
29291da177e4SLinus Torvalds  * Routines for the character/ioctl interface to the driver. Find out if this
2930d21c95c5SJonathan Corbet  * is a valid open.
29311da177e4SLinus Torvalds  */
29321da177e4SLinus Torvalds static int
megadev_open(struct inode * inode,struct file * filep)29331da177e4SLinus Torvalds megadev_open (struct inode *inode, struct file *filep)
29341da177e4SLinus Torvalds {
29351da177e4SLinus Torvalds 	/*
29361da177e4SLinus Torvalds 	 * Only allow superuser to access private ioctl interface
29371da177e4SLinus Torvalds 	 */
29381da177e4SLinus Torvalds 	if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	return 0;
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds /**
29451da177e4SLinus Torvalds  * megadev_ioctl()
29462b46e5c1SDamien Le Moal  * @filep: Our device file
29472b46e5c1SDamien Le Moal  * @cmd: ioctl command
29482b46e5c1SDamien Le Moal  * @arg: user buffer
29491da177e4SLinus Torvalds  *
29501da177e4SLinus Torvalds  * ioctl entry point for our private ioctl interface. We move the data in from
29511da177e4SLinus Torvalds  * the user space, prepare the command (if necessary, convert the old MIMD
29521da177e4SLinus Torvalds  * ioctl to new ioctl command), and issue a synchronous command to the
29531da177e4SLinus Torvalds  * controller.
29541da177e4SLinus Torvalds  */
29551da177e4SLinus Torvalds static int
megadev_ioctl(struct file * filep,unsigned int cmd,unsigned long arg)2956f4927c45SArnd Bergmann megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
29571da177e4SLinus Torvalds {
29581da177e4SLinus Torvalds 	adapter_t	*adapter;
29591da177e4SLinus Torvalds 	nitioctl_t	uioc;
29601da177e4SLinus Torvalds 	int		adapno;
29611da177e4SLinus Torvalds 	int		rval;
29621da177e4SLinus Torvalds 	mega_passthru	__user *upthru;	/* user address for passthru */
29631da177e4SLinus Torvalds 	mega_passthru	*pthru;		/* copy user passthru here */
29641da177e4SLinus Torvalds 	dma_addr_t	pthru_dma_hndl;
29651da177e4SLinus Torvalds 	void		*data = NULL;	/* data to be transferred */
29661da177e4SLinus Torvalds 	dma_addr_t	data_dma_hndl;	/* dma handle for data xfer area */
29671da177e4SLinus Torvalds 	megacmd_t	mc;
2968aa055885SDamien Le Moal #if MEGA_HAVE_STATS
2969aa055885SDamien Le Moal 	megastat_t	__user *ustats = NULL;
2970aa055885SDamien Le Moal 	int		num_ldrv = 0;
2971aa055885SDamien Le Moal #endif
29721da177e4SLinus Torvalds 	u32		uxferaddr = 0;
29731da177e4SLinus Torvalds 	struct pci_dev	*pdev;
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 	/*
29761da177e4SLinus Torvalds 	 * Make sure only USCSICMD are issued through this interface.
29771da177e4SLinus Torvalds 	 * MIMD application would still fire different command.
29781da177e4SLinus Torvalds 	 */
29791da177e4SLinus Torvalds 	if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
29801da177e4SLinus Torvalds 		return -EINVAL;
29811da177e4SLinus Torvalds 	}
29821da177e4SLinus Torvalds 
29831da177e4SLinus Torvalds 	/*
29841da177e4SLinus Torvalds 	 * Check and convert a possible MIMD command to NIT command.
29851da177e4SLinus Torvalds 	 * mega_m_to_n() copies the data from the user space, so we do not
29861da177e4SLinus Torvalds 	 * have to do it here.
29871da177e4SLinus Torvalds 	 * NOTE: We will need some user address to copyout the data, therefore
29881da177e4SLinus Torvalds 	 * the inteface layer will also provide us with the required user
29891da177e4SLinus Torvalds 	 * addresses.
29901da177e4SLinus Torvalds 	 */
29911da177e4SLinus Torvalds 	memset(&uioc, 0, sizeof(nitioctl_t));
29921da177e4SLinus Torvalds 	if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
29931da177e4SLinus Torvalds 		return rval;
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	switch( uioc.opcode ) {
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 	case GET_DRIVER_VER:
29991da177e4SLinus Torvalds 		if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
30001da177e4SLinus Torvalds 			return (-EFAULT);
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 		break;
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	case GET_N_ADAP:
30051da177e4SLinus Torvalds 		if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
30061da177e4SLinus Torvalds 			return (-EFAULT);
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds 		/*
30091da177e4SLinus Torvalds 		 * Shucks. MIMD interface returns a positive value for number
30101da177e4SLinus Torvalds 		 * of adapters. TODO: Change it to return 0 when there is no
30111da177e4SLinus Torvalds 		 * applicatio using mimd interface.
30121da177e4SLinus Torvalds 		 */
30131da177e4SLinus Torvalds 		return hba_count;
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	case GET_ADAP_INFO:
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds 		/*
30181da177e4SLinus Torvalds 		 * Which adapter
30191da177e4SLinus Torvalds 		 */
30201da177e4SLinus Torvalds 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
30211da177e4SLinus Torvalds 			return (-ENODEV);
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 		if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
30241da177e4SLinus Torvalds 				sizeof(struct mcontroller)) )
30251da177e4SLinus Torvalds 			return (-EFAULT);
30261da177e4SLinus Torvalds 		break;
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds #if MEGA_HAVE_STATS
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	case GET_STATS:
30311da177e4SLinus Torvalds 		/*
30321da177e4SLinus Torvalds 		 * Which adapter
30331da177e4SLinus Torvalds 		 */
30341da177e4SLinus Torvalds 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
30351da177e4SLinus Torvalds 			return (-ENODEV);
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds 		adapter = hba_soft_state[adapno];
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 		ustats = uioc.uioc_uaddr;
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 		if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
30421da177e4SLinus Torvalds 			return (-EFAULT);
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 		/*
30451da177e4SLinus Torvalds 		 * Check for the validity of the logical drive number
30461da177e4SLinus Torvalds 		 */
30471da177e4SLinus Torvalds 		if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 		if( copy_to_user(ustats->nreads, adapter->nreads,
30501da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
30511da177e4SLinus Torvalds 			return -EFAULT;
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 		if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
30541da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
30551da177e4SLinus Torvalds 			return -EFAULT;
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds 		if( copy_to_user(ustats->nwrites, adapter->nwrites,
30581da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
30591da177e4SLinus Torvalds 			return -EFAULT;
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds 		if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
30621da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
30631da177e4SLinus Torvalds 			return -EFAULT;
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 		if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
30661da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
30671da177e4SLinus Torvalds 			return -EFAULT;
30681da177e4SLinus Torvalds 
30691da177e4SLinus Torvalds 		if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
30701da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
30711da177e4SLinus Torvalds 			return -EFAULT;
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 		return 0;
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds #endif
30761da177e4SLinus Torvalds 	case MBOX_CMD:
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 		/*
30791da177e4SLinus Torvalds 		 * Which adapter
30801da177e4SLinus Torvalds 		 */
30811da177e4SLinus Torvalds 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
30821da177e4SLinus Torvalds 			return (-ENODEV);
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 		adapter = hba_soft_state[adapno];
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 		/*
30871da177e4SLinus Torvalds 		 * Deletion of logical drive is a special case. The adapter
30881da177e4SLinus Torvalds 		 * should be quiescent before this command is issued.
30891da177e4SLinus Torvalds 		 */
30901da177e4SLinus Torvalds 		if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
30911da177e4SLinus Torvalds 				uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds 			/*
30941da177e4SLinus Torvalds 			 * Do we support this feature
30951da177e4SLinus Torvalds 			 */
30961da177e4SLinus Torvalds 			if( !adapter->support_random_del ) {
30973b8a1ba3SBjorn Helgaas 				dev_warn(&adapter->dev->dev, "logdrv "
30983b8a1ba3SBjorn Helgaas 					"delete on non-supporting F/W\n");
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 				return (-EINVAL);
31011da177e4SLinus Torvalds 			}
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds 			rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
31041da177e4SLinus Torvalds 
31051da177e4SLinus Torvalds 			if( rval == 0 ) {
31061da177e4SLinus Torvalds 				memset(&mc, 0, sizeof(megacmd_t));
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds 				mc.status = rval;
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds 				rval = mega_n_to_m((void __user *)arg, &mc);
31111da177e4SLinus Torvalds 			}
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 			return rval;
31141da177e4SLinus Torvalds 		}
31151da177e4SLinus Torvalds 		/*
31161da177e4SLinus Torvalds 		 * This interface only support the regular passthru commands.
31171da177e4SLinus Torvalds 		 * Reject extended passthru and 64-bit passthru
31181da177e4SLinus Torvalds 		 */
31191da177e4SLinus Torvalds 		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
31201da177e4SLinus Torvalds 			uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
31211da177e4SLinus Torvalds 
31223b8a1ba3SBjorn Helgaas 			dev_warn(&adapter->dev->dev, "rejected passthru\n");
31231da177e4SLinus Torvalds 
31241da177e4SLinus Torvalds 			return (-EINVAL);
31251da177e4SLinus Torvalds 		}
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds 		/*
31281da177e4SLinus Torvalds 		 * For all internal commands, the buffer must be allocated in
31291da177e4SLinus Torvalds 		 * <4GB address range
31301da177e4SLinus Torvalds 		 */
31311da177e4SLinus Torvalds 		if( make_local_pdev(adapter, &pdev) != 0 )
31321da177e4SLinus Torvalds 			return -EIO;
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 		/* Is it a passthru command or a DCMD */
31351da177e4SLinus Torvalds 		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
31361da177e4SLinus Torvalds 			/* Passthru commands */
31371da177e4SLinus Torvalds 
3138ec090ef8SSuraj Upadhyay 			pthru = dma_alloc_coherent(&pdev->dev,
31391da177e4SLinus Torvalds 						   sizeof(mega_passthru),
3140ec090ef8SSuraj Upadhyay 						   &pthru_dma_hndl, GFP_KERNEL);
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds 			if( pthru == NULL ) {
31431da177e4SLinus Torvalds 				free_local_pdev(pdev);
31441da177e4SLinus Torvalds 				return (-ENOMEM);
31451da177e4SLinus Torvalds 			}
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 			/*
31481da177e4SLinus Torvalds 			 * The user passthru structure
31491da177e4SLinus Torvalds 			 */
3150de5952e9SMartin Bligh 			upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 			/*
31531da177e4SLinus Torvalds 			 * Copy in the user passthru here.
31541da177e4SLinus Torvalds 			 */
31551da177e4SLinus Torvalds 			if( copy_from_user(pthru, upthru,
31561da177e4SLinus Torvalds 						sizeof(mega_passthru)) ) {
31571da177e4SLinus Torvalds 
3158ec090ef8SSuraj Upadhyay 				dma_free_coherent(&pdev->dev,
3159ec090ef8SSuraj Upadhyay 						  sizeof(mega_passthru),
3160ec090ef8SSuraj Upadhyay 						  pthru, pthru_dma_hndl);
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 				free_local_pdev(pdev);
31631da177e4SLinus Torvalds 
31641da177e4SLinus Torvalds 				return (-EFAULT);
31651da177e4SLinus Torvalds 			}
31661da177e4SLinus Torvalds 
31671da177e4SLinus Torvalds 			/*
31681da177e4SLinus Torvalds 			 * Is there a data transfer
31691da177e4SLinus Torvalds 			 */
31701da177e4SLinus Torvalds 			if( pthru->dataxferlen ) {
3171ec090ef8SSuraj Upadhyay 				data = dma_alloc_coherent(&pdev->dev,
31721da177e4SLinus Torvalds 							  pthru->dataxferlen,
3173ec090ef8SSuraj Upadhyay 							  &data_dma_hndl,
3174ec090ef8SSuraj Upadhyay 							  GFP_KERNEL);
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds 				if( data == NULL ) {
3177ec090ef8SSuraj Upadhyay 					dma_free_coherent(&pdev->dev,
31781da177e4SLinus Torvalds 							  sizeof(mega_passthru),
31791da177e4SLinus Torvalds 							  pthru,
31801da177e4SLinus Torvalds 							  pthru_dma_hndl);
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds 					free_local_pdev(pdev);
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds 					return (-ENOMEM);
31851da177e4SLinus Torvalds 				}
31861da177e4SLinus Torvalds 
31871da177e4SLinus Torvalds 				/*
31881da177e4SLinus Torvalds 				 * Save the user address and point the kernel
31891da177e4SLinus Torvalds 				 * address at just allocated memory
31901da177e4SLinus Torvalds 				 */
31911da177e4SLinus Torvalds 				uxferaddr = pthru->dataxferaddr;
31921da177e4SLinus Torvalds 				pthru->dataxferaddr = data_dma_hndl;
31931da177e4SLinus Torvalds 			}
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 
31961da177e4SLinus Torvalds 			/*
31971da177e4SLinus Torvalds 			 * Is data coming down-stream
31981da177e4SLinus Torvalds 			 */
31991da177e4SLinus Torvalds 			if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
32001da177e4SLinus Torvalds 				/*
32011da177e4SLinus Torvalds 				 * Get the user data
32021da177e4SLinus Torvalds 				 */
3203de5952e9SMartin Bligh 				if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
32041da177e4SLinus Torvalds 							pthru->dataxferlen) ) {
32051da177e4SLinus Torvalds 					rval = (-EFAULT);
32061da177e4SLinus Torvalds 					goto freemem_and_return;
32071da177e4SLinus Torvalds 				}
32081da177e4SLinus Torvalds 			}
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds 			memset(&mc, 0, sizeof(megacmd_t));
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds 			mc.cmd = MEGA_MBOXCMD_PASSTHRU;
32131da177e4SLinus Torvalds 			mc.xferaddr = (u32)pthru_dma_hndl;
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds 			/*
32161da177e4SLinus Torvalds 			 * Issue the command
32171da177e4SLinus Torvalds 			 */
3218cb0258a2SChristoph Hellwig 			mega_internal_command(adapter, &mc, pthru);
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds 			rval = mega_n_to_m((void __user *)arg, &mc);
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds 			if( rval ) goto freemem_and_return;
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds 			/*
32261da177e4SLinus Torvalds 			 * Is data going up-stream
32271da177e4SLinus Torvalds 			 */
32281da177e4SLinus Torvalds 			if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3229de5952e9SMartin Bligh 				if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
32301da177e4SLinus Torvalds 							pthru->dataxferlen) ) {
32311da177e4SLinus Torvalds 					rval = (-EFAULT);
32321da177e4SLinus Torvalds 				}
32331da177e4SLinus Torvalds 			}
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 			/*
32361da177e4SLinus Torvalds 			 * Send the request sense data also, irrespective of
32371da177e4SLinus Torvalds 			 * whether the user has asked for it or not.
32381da177e4SLinus Torvalds 			 */
32392d2f8d59SJesper Juhl 			if (copy_to_user(upthru->reqsensearea,
32402d2f8d59SJesper Juhl 					pthru->reqsensearea, 14))
32412d2f8d59SJesper Juhl 				rval = -EFAULT;
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds freemem_and_return:
32441da177e4SLinus Torvalds 			if( pthru->dataxferlen ) {
3245ec090ef8SSuraj Upadhyay 				dma_free_coherent(&pdev->dev,
32461da177e4SLinus Torvalds 						  pthru->dataxferlen, data,
32471da177e4SLinus Torvalds 						  data_dma_hndl);
32481da177e4SLinus Torvalds 			}
32491da177e4SLinus Torvalds 
3250ec090ef8SSuraj Upadhyay 			dma_free_coherent(&pdev->dev, sizeof(mega_passthru),
32511da177e4SLinus Torvalds 					  pthru, pthru_dma_hndl);
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds 			free_local_pdev(pdev);
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds 			return rval;
32561da177e4SLinus Torvalds 		}
32571da177e4SLinus Torvalds 		else {
32581da177e4SLinus Torvalds 			/* DCMD commands */
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 			/*
32611da177e4SLinus Torvalds 			 * Is there a data transfer
32621da177e4SLinus Torvalds 			 */
32631da177e4SLinus Torvalds 			if( uioc.xferlen ) {
3264ec090ef8SSuraj Upadhyay 				data = dma_alloc_coherent(&pdev->dev,
3265ec090ef8SSuraj Upadhyay 							  uioc.xferlen,
3266ec090ef8SSuraj Upadhyay 							  &data_dma_hndl,
3267ec090ef8SSuraj Upadhyay 							  GFP_KERNEL);
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds 				if( data == NULL ) {
32701da177e4SLinus Torvalds 					free_local_pdev(pdev);
32711da177e4SLinus Torvalds 					return (-ENOMEM);
32721da177e4SLinus Torvalds 				}
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 				uxferaddr = MBOX(uioc)->xferaddr;
32751da177e4SLinus Torvalds 			}
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds 			/*
32781da177e4SLinus Torvalds 			 * Is data coming down-stream
32791da177e4SLinus Torvalds 			 */
32801da177e4SLinus Torvalds 			if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
32811da177e4SLinus Torvalds 				/*
32821da177e4SLinus Torvalds 				 * Get the user data
32831da177e4SLinus Torvalds 				 */
3284de5952e9SMartin Bligh 				if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
32851da177e4SLinus Torvalds 							uioc.xferlen) ) {
32861da177e4SLinus Torvalds 
3287ec090ef8SSuraj Upadhyay 					dma_free_coherent(&pdev->dev,
3288ec090ef8SSuraj Upadhyay 							  uioc.xferlen, data,
3289ec090ef8SSuraj Upadhyay 							  data_dma_hndl);
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 					free_local_pdev(pdev);
32921da177e4SLinus Torvalds 
32931da177e4SLinus Torvalds 					return (-EFAULT);
32941da177e4SLinus Torvalds 				}
32951da177e4SLinus Torvalds 			}
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 			memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
32981da177e4SLinus Torvalds 
32991da177e4SLinus Torvalds 			mc.xferaddr = (u32)data_dma_hndl;
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds 			/*
33021da177e4SLinus Torvalds 			 * Issue the command
33031da177e4SLinus Torvalds 			 */
3304cb0258a2SChristoph Hellwig 			mega_internal_command(adapter, &mc, NULL);
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 			rval = mega_n_to_m((void __user *)arg, &mc);
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 			if( rval ) {
33091da177e4SLinus Torvalds 				if( uioc.xferlen ) {
3310ec090ef8SSuraj Upadhyay 					dma_free_coherent(&pdev->dev,
33111da177e4SLinus Torvalds 							  uioc.xferlen, data,
33121da177e4SLinus Torvalds 							  data_dma_hndl);
33131da177e4SLinus Torvalds 				}
33141da177e4SLinus Torvalds 
33151da177e4SLinus Torvalds 				free_local_pdev(pdev);
33161da177e4SLinus Torvalds 
33171da177e4SLinus Torvalds 				return rval;
33181da177e4SLinus Torvalds 			}
33191da177e4SLinus Torvalds 
33201da177e4SLinus Torvalds 			/*
33211da177e4SLinus Torvalds 			 * Is data going up-stream
33221da177e4SLinus Torvalds 			 */
33231da177e4SLinus Torvalds 			if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3324de5952e9SMartin Bligh 				if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
33251da177e4SLinus Torvalds 							uioc.xferlen) ) {
33261da177e4SLinus Torvalds 
33271da177e4SLinus Torvalds 					rval = (-EFAULT);
33281da177e4SLinus Torvalds 				}
33291da177e4SLinus Torvalds 			}
33301da177e4SLinus Torvalds 
33311da177e4SLinus Torvalds 			if( uioc.xferlen ) {
3332ec090ef8SSuraj Upadhyay 				dma_free_coherent(&pdev->dev, uioc.xferlen,
3333ec090ef8SSuraj Upadhyay 						  data, data_dma_hndl);
33341da177e4SLinus Torvalds 			}
33351da177e4SLinus Torvalds 
33361da177e4SLinus Torvalds 			free_local_pdev(pdev);
33371da177e4SLinus Torvalds 
33381da177e4SLinus Torvalds 			return rval;
33391da177e4SLinus Torvalds 		}
33401da177e4SLinus Torvalds 
33411da177e4SLinus Torvalds 	default:
33421da177e4SLinus Torvalds 		return (-EINVAL);
33431da177e4SLinus Torvalds 	}
33441da177e4SLinus Torvalds 
33451da177e4SLinus Torvalds 	return 0;
33461da177e4SLinus Torvalds }
33471da177e4SLinus Torvalds 
3348f4927c45SArnd Bergmann static long
megadev_unlocked_ioctl(struct file * filep,unsigned int cmd,unsigned long arg)3349f4927c45SArnd Bergmann megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3350f4927c45SArnd Bergmann {
3351f4927c45SArnd Bergmann 	int ret;
3352f4927c45SArnd Bergmann 
3353c45d15d2SArnd Bergmann 	mutex_lock(&megadev_mutex);
3354f4927c45SArnd Bergmann 	ret = megadev_ioctl(filep, cmd, arg);
3355c45d15d2SArnd Bergmann 	mutex_unlock(&megadev_mutex);
3356f4927c45SArnd Bergmann 
3357f4927c45SArnd Bergmann 	return ret;
3358f4927c45SArnd Bergmann }
3359f4927c45SArnd Bergmann 
33601da177e4SLinus Torvalds /**
33611da177e4SLinus Torvalds  * mega_m_to_n()
33622b46e5c1SDamien Le Moal  * @arg: user address
33632b46e5c1SDamien Le Moal  * @uioc: new ioctl structure
33641da177e4SLinus Torvalds  *
33651da177e4SLinus Torvalds  * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
33661da177e4SLinus Torvalds  * structure
33671da177e4SLinus Torvalds  *
33681da177e4SLinus Torvalds  * Converts the older mimd ioctl structure to newer NIT structure
33691da177e4SLinus Torvalds  */
33701da177e4SLinus Torvalds static int
mega_m_to_n(void __user * arg,nitioctl_t * uioc)33711da177e4SLinus Torvalds mega_m_to_n(void __user *arg, nitioctl_t *uioc)
33721da177e4SLinus Torvalds {
33731da177e4SLinus Torvalds 	struct uioctl_t	uioc_mimd;
33741da177e4SLinus Torvalds 	char	signature[8] = {0};
33751da177e4SLinus Torvalds 	u8	opcode;
33761da177e4SLinus Torvalds 	u8	subopcode;
33771da177e4SLinus Torvalds 
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds 	/*
33801da177e4SLinus Torvalds 	 * check is the application conforms to NIT. We do not have to do much
33811da177e4SLinus Torvalds 	 * in that case.
33821da177e4SLinus Torvalds 	 * We exploit the fact that the signature is stored in the very
338325985edcSLucas De Marchi 	 * beginning of the structure.
33841da177e4SLinus Torvalds 	 */
33851da177e4SLinus Torvalds 
33861da177e4SLinus Torvalds 	if( copy_from_user(signature, arg, 7) )
33871da177e4SLinus Torvalds 		return (-EFAULT);
33881da177e4SLinus Torvalds 
33891da177e4SLinus Torvalds 	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds 		/*
33921da177e4SLinus Torvalds 		 * NOTE NOTE: The nit ioctl is still under flux because of
33931da177e4SLinus Torvalds 		 * change of mailbox definition, in HPE. No applications yet
33941da177e4SLinus Torvalds 		 * use this interface and let's not have applications use this
33951da177e4SLinus Torvalds 		 * interface till the new specifitions are in place.
33961da177e4SLinus Torvalds 		 */
33971da177e4SLinus Torvalds 		return -EINVAL;
33981da177e4SLinus Torvalds #if 0
33991da177e4SLinus Torvalds 		if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
34001da177e4SLinus Torvalds 			return (-EFAULT);
34011da177e4SLinus Torvalds 		return 0;
34021da177e4SLinus Torvalds #endif
34031da177e4SLinus Torvalds 	}
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	/*
34061da177e4SLinus Torvalds 	 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
34071da177e4SLinus Torvalds 	 *
34081da177e4SLinus Torvalds 	 * Get the user ioctl structure
34091da177e4SLinus Torvalds 	 */
34101da177e4SLinus Torvalds 	if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
34111da177e4SLinus Torvalds 		return (-EFAULT);
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds 	/*
34151da177e4SLinus Torvalds 	 * Get the opcode and subopcode for the commands
34161da177e4SLinus Torvalds 	 */
34171da177e4SLinus Torvalds 	opcode = uioc_mimd.ui.fcs.opcode;
34181da177e4SLinus Torvalds 	subopcode = uioc_mimd.ui.fcs.subopcode;
34191da177e4SLinus Torvalds 
34201da177e4SLinus Torvalds 	switch (opcode) {
34211da177e4SLinus Torvalds 	case 0x82:
34221da177e4SLinus Torvalds 
34231da177e4SLinus Torvalds 		switch (subopcode) {
34241da177e4SLinus Torvalds 
34251da177e4SLinus Torvalds 		case MEGAIOC_QDRVRVER:	/* Query driver version */
34261da177e4SLinus Torvalds 			uioc->opcode = GET_DRIVER_VER;
34271da177e4SLinus Torvalds 			uioc->uioc_uaddr = uioc_mimd.data;
34281da177e4SLinus Torvalds 			break;
34291da177e4SLinus Torvalds 
34301da177e4SLinus Torvalds 		case MEGAIOC_QNADAP:	/* Get # of adapters */
34311da177e4SLinus Torvalds 			uioc->opcode = GET_N_ADAP;
34321da177e4SLinus Torvalds 			uioc->uioc_uaddr = uioc_mimd.data;
34331da177e4SLinus Torvalds 			break;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 		case MEGAIOC_QADAPINFO:	/* Get adapter information */
34361da177e4SLinus Torvalds 			uioc->opcode = GET_ADAP_INFO;
34371da177e4SLinus Torvalds 			uioc->adapno = uioc_mimd.ui.fcs.adapno;
34381da177e4SLinus Torvalds 			uioc->uioc_uaddr = uioc_mimd.data;
34391da177e4SLinus Torvalds 			break;
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds 		default:
34421da177e4SLinus Torvalds 			return(-EINVAL);
34431da177e4SLinus Torvalds 		}
34441da177e4SLinus Torvalds 
34451da177e4SLinus Torvalds 		break;
34461da177e4SLinus Torvalds 
34471da177e4SLinus Torvalds 
34481da177e4SLinus Torvalds 	case 0x81:
34491da177e4SLinus Torvalds 
34501da177e4SLinus Torvalds 		uioc->opcode = MBOX_CMD;
34511da177e4SLinus Torvalds 		uioc->adapno = uioc_mimd.ui.fcs.adapno;
34521da177e4SLinus Torvalds 
34531da177e4SLinus Torvalds 		memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
34541da177e4SLinus Torvalds 
34551da177e4SLinus Torvalds 		uioc->xferlen = uioc_mimd.ui.fcs.length;
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
34581da177e4SLinus Torvalds 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds 		break;
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds 	case 0x80:
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds 		uioc->opcode = MBOX_CMD;
34651da177e4SLinus Torvalds 		uioc->adapno = uioc_mimd.ui.fcs.adapno;
34661da177e4SLinus Torvalds 
34671da177e4SLinus Torvalds 		memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
34681da177e4SLinus Torvalds 
34691da177e4SLinus Torvalds 		/*
34701da177e4SLinus Torvalds 		 * Choose the xferlen bigger of input and output data
34711da177e4SLinus Torvalds 		 */
34721da177e4SLinus Torvalds 		uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
34731da177e4SLinus Torvalds 			uioc_mimd.outlen : uioc_mimd.inlen;
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
34761da177e4SLinus Torvalds 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
34771da177e4SLinus Torvalds 
34781da177e4SLinus Torvalds 		break;
34791da177e4SLinus Torvalds 
34801da177e4SLinus Torvalds 	default:
34811da177e4SLinus Torvalds 		return (-EINVAL);
34821da177e4SLinus Torvalds 
34831da177e4SLinus Torvalds 	}
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 	return 0;
34861da177e4SLinus Torvalds }
34871da177e4SLinus Torvalds 
34881da177e4SLinus Torvalds /*
34891da177e4SLinus Torvalds  * mega_n_to_m()
34902b46e5c1SDamien Le Moal  * @arg: user address
34912b46e5c1SDamien Le Moal  * @mc: mailbox command
34921da177e4SLinus Torvalds  *
34931da177e4SLinus Torvalds  * Updates the status information to the application, depending on application
34941da177e4SLinus Torvalds  * conforms to older mimd ioctl interface or newer NIT ioctl interface
34951da177e4SLinus Torvalds  */
34961da177e4SLinus Torvalds static int
mega_n_to_m(void __user * arg,megacmd_t * mc)34971da177e4SLinus Torvalds mega_n_to_m(void __user *arg, megacmd_t *mc)
34981da177e4SLinus Torvalds {
34991da177e4SLinus Torvalds 	nitioctl_t	__user *uiocp;
35001da177e4SLinus Torvalds 	megacmd_t	__user *umc;
35011da177e4SLinus Torvalds 	mega_passthru	__user *upthru;
35021da177e4SLinus Torvalds 	struct uioctl_t	__user *uioc_mimd;
35031da177e4SLinus Torvalds 	char	signature[8] = {0};
35041da177e4SLinus Torvalds 
35051da177e4SLinus Torvalds 	/*
35061da177e4SLinus Torvalds 	 * check is the application conforms to NIT.
35071da177e4SLinus Torvalds 	 */
35081da177e4SLinus Torvalds 	if( copy_from_user(signature, arg, 7) )
35091da177e4SLinus Torvalds 		return -EFAULT;
35101da177e4SLinus Torvalds 
35111da177e4SLinus Torvalds 	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
35121da177e4SLinus Torvalds 
35131da177e4SLinus Torvalds 		uiocp = arg;
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds 		if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
35161da177e4SLinus Torvalds 			return (-EFAULT);
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds 			umc = MBOX_P(uiocp);
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 			if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
35231da177e4SLinus Torvalds 				return -EFAULT;
35241da177e4SLinus Torvalds 
35251da177e4SLinus Torvalds 			if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
35261da177e4SLinus Torvalds 				return (-EFAULT);
35271da177e4SLinus Torvalds 		}
35281da177e4SLinus Torvalds 	}
35291da177e4SLinus Torvalds 	else {
35301da177e4SLinus Torvalds 		uioc_mimd = arg;
35311da177e4SLinus Torvalds 
35321da177e4SLinus Torvalds 		if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
35331da177e4SLinus Torvalds 			return (-EFAULT);
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds 		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
35361da177e4SLinus Torvalds 
35371da177e4SLinus Torvalds 			umc = (megacmd_t __user *)uioc_mimd->mbox;
35381da177e4SLinus Torvalds 
35391da177e4SLinus Torvalds 			if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
35401da177e4SLinus Torvalds 				return (-EFAULT);
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 			if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
35431da177e4SLinus Torvalds 				return (-EFAULT);
35441da177e4SLinus Torvalds 		}
35451da177e4SLinus Torvalds 	}
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 	return 0;
35481da177e4SLinus Torvalds }
35491da177e4SLinus Torvalds 
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds /*
35521da177e4SLinus Torvalds  * MEGARAID 'FW' commands.
35531da177e4SLinus Torvalds  */
35541da177e4SLinus Torvalds 
35551da177e4SLinus Torvalds /**
35561da177e4SLinus Torvalds  * mega_is_bios_enabled()
35572b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
35581da177e4SLinus Torvalds  *
35591da177e4SLinus Torvalds  * issue command to find out if the BIOS is enabled for this controller
35601da177e4SLinus Torvalds  */
35611da177e4SLinus Torvalds static int
mega_is_bios_enabled(adapter_t * adapter)35621da177e4SLinus Torvalds mega_is_bios_enabled(adapter_t *adapter)
35631da177e4SLinus Torvalds {
3564315d049aSKees Cook 	struct mbox_out mbox;
3565315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
35661da177e4SLinus Torvalds 
3567315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
35681da177e4SLinus Torvalds 
35691da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
35701da177e4SLinus Torvalds 
3571315d049aSKees Cook 	mbox.xferaddr = (u32)adapter->buf_dma_handle;
35721da177e4SLinus Torvalds 
35731da177e4SLinus Torvalds 	raw_mbox[0] = IS_BIOS_ENABLED;
35741da177e4SLinus Torvalds 	raw_mbox[2] = GET_BIOS;
35751da177e4SLinus Torvalds 
3576b1a557c2SDamien Le Moal 	issue_scb_block(adapter, raw_mbox);
35771da177e4SLinus Torvalds 
35781da177e4SLinus Torvalds 	return *(char *)adapter->mega_buffer;
35791da177e4SLinus Torvalds }
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds 
35821da177e4SLinus Torvalds /**
35831da177e4SLinus Torvalds  * mega_enum_raid_scsi()
35842b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
35851da177e4SLinus Torvalds  *
35861da177e4SLinus Torvalds  * Find out what channels are RAID/SCSI. This information is used to
35871da177e4SLinus Torvalds  * differentiate the virtual channels and physical channels and to support
35881da177e4SLinus Torvalds  * ROMB feature and non-disk devices.
35891da177e4SLinus Torvalds  */
35901da177e4SLinus Torvalds static void
mega_enum_raid_scsi(adapter_t * adapter)35911da177e4SLinus Torvalds mega_enum_raid_scsi(adapter_t *adapter)
35921da177e4SLinus Torvalds {
3593315d049aSKees Cook 	struct mbox_out mbox;
3594315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
35951da177e4SLinus Torvalds 	int i;
35961da177e4SLinus Torvalds 
3597315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
35981da177e4SLinus Torvalds 
35991da177e4SLinus Torvalds 	/*
36001da177e4SLinus Torvalds 	 * issue command to find out what channels are raid/scsi
36011da177e4SLinus Torvalds 	 */
36021da177e4SLinus Torvalds 	raw_mbox[0] = CHNL_CLASS;
36031da177e4SLinus Torvalds 	raw_mbox[2] = GET_CHNL_CLASS;
36041da177e4SLinus Torvalds 
36051da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
36061da177e4SLinus Torvalds 
3607315d049aSKees Cook 	mbox.xferaddr = (u32)adapter->buf_dma_handle;
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds 	/*
36101da177e4SLinus Torvalds 	 * Non-ROMB firmware fail this command, so all channels
36111da177e4SLinus Torvalds 	 * must be shown RAID
36121da177e4SLinus Torvalds 	 */
36131da177e4SLinus Torvalds 	adapter->mega_ch_class = 0xFF;
36141da177e4SLinus Torvalds 
36151da177e4SLinus Torvalds 	if(!issue_scb_block(adapter, raw_mbox)) {
36161da177e4SLinus Torvalds 		adapter->mega_ch_class = *((char *)adapter->mega_buffer);
36171da177e4SLinus Torvalds 
36181da177e4SLinus Torvalds 	}
36191da177e4SLinus Torvalds 
36201da177e4SLinus Torvalds 	for( i = 0; i < adapter->product_info.nchannels; i++ ) {
36211da177e4SLinus Torvalds 		if( (adapter->mega_ch_class >> i) & 0x01 ) {
36223b8a1ba3SBjorn Helgaas 			dev_info(&adapter->dev->dev, "channel[%d] is raid\n",
36231da177e4SLinus Torvalds 					i);
36241da177e4SLinus Torvalds 		}
36251da177e4SLinus Torvalds 		else {
36263b8a1ba3SBjorn Helgaas 			dev_info(&adapter->dev->dev, "channel[%d] is scsi\n",
36271da177e4SLinus Torvalds 					i);
36281da177e4SLinus Torvalds 		}
36291da177e4SLinus Torvalds 	}
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds 	return;
36321da177e4SLinus Torvalds }
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds 
36351da177e4SLinus Torvalds /**
36361da177e4SLinus Torvalds  * mega_get_boot_drv()
36372b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
36381da177e4SLinus Torvalds  *
36391da177e4SLinus Torvalds  * Find out which device is the boot device. Note, any logical drive or any
36401da177e4SLinus Torvalds  * phyical device (e.g., a CDROM) can be designated as a boot device.
36411da177e4SLinus Torvalds  */
36421da177e4SLinus Torvalds static void
mega_get_boot_drv(adapter_t * adapter)36431da177e4SLinus Torvalds mega_get_boot_drv(adapter_t *adapter)
36441da177e4SLinus Torvalds {
36451da177e4SLinus Torvalds 	struct private_bios_data	*prv_bios_data;
3646315d049aSKees Cook 	struct mbox_out mbox;
3647315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
36481da177e4SLinus Torvalds 	u16	cksum = 0;
36491da177e4SLinus Torvalds 	u8	*cksum_p;
36501da177e4SLinus Torvalds 	u8	boot_pdrv;
36511da177e4SLinus Torvalds 	int	i;
36521da177e4SLinus Torvalds 
3653315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
36541da177e4SLinus Torvalds 
36551da177e4SLinus Torvalds 	raw_mbox[0] = BIOS_PVT_DATA;
36561da177e4SLinus Torvalds 	raw_mbox[2] = GET_BIOS_PVT_DATA;
36571da177e4SLinus Torvalds 
36581da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
36591da177e4SLinus Torvalds 
3660315d049aSKees Cook 	mbox.xferaddr = (u32)adapter->buf_dma_handle;
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds 	adapter->boot_ldrv_enabled = 0;
36631da177e4SLinus Torvalds 	adapter->boot_ldrv = 0;
36641da177e4SLinus Torvalds 
36651da177e4SLinus Torvalds 	adapter->boot_pdrv_enabled = 0;
36661da177e4SLinus Torvalds 	adapter->boot_pdrv_ch = 0;
36671da177e4SLinus Torvalds 	adapter->boot_pdrv_tgt = 0;
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	if(issue_scb_block(adapter, raw_mbox) == 0) {
36701da177e4SLinus Torvalds 		prv_bios_data =
36711da177e4SLinus Torvalds 			(struct private_bios_data *)adapter->mega_buffer;
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 		cksum = 0;
36741da177e4SLinus Torvalds 		cksum_p = (char *)prv_bios_data;
36751da177e4SLinus Torvalds 		for (i = 0; i < 14; i++ ) {
36761da177e4SLinus Torvalds 			cksum += (u16)(*cksum_p++);
36771da177e4SLinus Torvalds 		}
36781da177e4SLinus Torvalds 
36791da177e4SLinus Torvalds 		if (prv_bios_data->cksum == (u16)(0-cksum) ) {
36801da177e4SLinus Torvalds 
36811da177e4SLinus Torvalds 			/*
36821da177e4SLinus Torvalds 			 * If MSB is set, a physical drive is set as boot
36831da177e4SLinus Torvalds 			 * device
36841da177e4SLinus Torvalds 			 */
36851da177e4SLinus Torvalds 			if( prv_bios_data->boot_drv & 0x80 ) {
36861da177e4SLinus Torvalds 				adapter->boot_pdrv_enabled = 1;
36871da177e4SLinus Torvalds 				boot_pdrv = prv_bios_data->boot_drv & 0x7F;
36881da177e4SLinus Torvalds 				adapter->boot_pdrv_ch = boot_pdrv / 16;
36891da177e4SLinus Torvalds 				adapter->boot_pdrv_tgt = boot_pdrv % 16;
36901da177e4SLinus Torvalds 			}
36911da177e4SLinus Torvalds 			else {
36921da177e4SLinus Torvalds 				adapter->boot_ldrv_enabled = 1;
36931da177e4SLinus Torvalds 				adapter->boot_ldrv = prv_bios_data->boot_drv;
36941da177e4SLinus Torvalds 			}
36951da177e4SLinus Torvalds 		}
36961da177e4SLinus Torvalds 	}
36971da177e4SLinus Torvalds 
36981da177e4SLinus Torvalds }
36991da177e4SLinus Torvalds 
37001da177e4SLinus Torvalds /**
37011da177e4SLinus Torvalds  * mega_support_random_del()
37022b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
37031da177e4SLinus Torvalds  *
37041da177e4SLinus Torvalds  * Find out if this controller supports random deletion and addition of
37051da177e4SLinus Torvalds  * logical drives
37061da177e4SLinus Torvalds  */
37071da177e4SLinus Torvalds static int
mega_support_random_del(adapter_t * adapter)37081da177e4SLinus Torvalds mega_support_random_del(adapter_t *adapter)
37091da177e4SLinus Torvalds {
3710315d049aSKees Cook 	struct mbox_out mbox;
3711315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
37121da177e4SLinus Torvalds 	int rval;
37131da177e4SLinus Torvalds 
3714315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
37151da177e4SLinus Torvalds 
37161da177e4SLinus Torvalds 	/*
37171da177e4SLinus Torvalds 	 * issue command
37181da177e4SLinus Torvalds 	 */
37191da177e4SLinus Torvalds 	raw_mbox[0] = FC_DEL_LOGDRV;
37201da177e4SLinus Torvalds 	raw_mbox[2] = OP_SUP_DEL_LOGDRV;
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds 	rval = issue_scb_block(adapter, raw_mbox);
37231da177e4SLinus Torvalds 
37241da177e4SLinus Torvalds 	return !rval;
37251da177e4SLinus Torvalds }
37261da177e4SLinus Torvalds 
37271da177e4SLinus Torvalds 
37281da177e4SLinus Torvalds /**
37291da177e4SLinus Torvalds  * mega_support_ext_cdb()
37302b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
37311da177e4SLinus Torvalds  *
37321da177e4SLinus Torvalds  * Find out if this firmware support cdblen > 10
37331da177e4SLinus Torvalds  */
37341da177e4SLinus Torvalds static int
mega_support_ext_cdb(adapter_t * adapter)37351da177e4SLinus Torvalds mega_support_ext_cdb(adapter_t *adapter)
37361da177e4SLinus Torvalds {
3737315d049aSKees Cook 	struct mbox_out mbox;
3738315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
37391da177e4SLinus Torvalds 	int rval;
37401da177e4SLinus Torvalds 
3741315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
37421da177e4SLinus Torvalds 	/*
37431da177e4SLinus Torvalds 	 * issue command to find out if controller supports extended CDBs.
37441da177e4SLinus Torvalds 	 */
37451da177e4SLinus Torvalds 	raw_mbox[0] = 0xA4;
37461da177e4SLinus Torvalds 	raw_mbox[2] = 0x16;
37471da177e4SLinus Torvalds 
37481da177e4SLinus Torvalds 	rval = issue_scb_block(adapter, raw_mbox);
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds 	return !rval;
37511da177e4SLinus Torvalds }
37521da177e4SLinus Torvalds 
37531da177e4SLinus Torvalds 
37541da177e4SLinus Torvalds /**
37551da177e4SLinus Torvalds  * mega_del_logdrv()
37562b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
37572b46e5c1SDamien Le Moal  * @logdrv: logical drive to be deleted
37581da177e4SLinus Torvalds  *
37591da177e4SLinus Torvalds  * Delete the specified logical drive. It is the responsibility of the user
37601da177e4SLinus Torvalds  * app to let the OS know about this operation.
37611da177e4SLinus Torvalds  */
37621da177e4SLinus Torvalds static int
mega_del_logdrv(adapter_t * adapter,int logdrv)37631da177e4SLinus Torvalds mega_del_logdrv(adapter_t *adapter, int logdrv)
37641da177e4SLinus Torvalds {
37651da177e4SLinus Torvalds 	unsigned long flags;
37661da177e4SLinus Torvalds 	scb_t *scb;
37671da177e4SLinus Torvalds 	int rval;
37681da177e4SLinus Torvalds 
37691da177e4SLinus Torvalds 	/*
37701da177e4SLinus Torvalds 	 * Stop sending commands to the controller, queue them internally.
37711da177e4SLinus Torvalds 	 * When deletion is complete, ISR will flush the queue.
37721da177e4SLinus Torvalds 	 */
37731da177e4SLinus Torvalds 	atomic_set(&adapter->quiescent, 1);
37741da177e4SLinus Torvalds 
37751da177e4SLinus Torvalds 	/*
37761da177e4SLinus Torvalds 	 * Wait till all the issued commands are complete and there are no
37771da177e4SLinus Torvalds 	 * commands in the pending queue
37781da177e4SLinus Torvalds 	 */
37791da177e4SLinus Torvalds 	while (atomic_read(&adapter->pend_cmds) > 0 ||
37801da177e4SLinus Torvalds 	       !list_empty(&adapter->pending_list))
37811da177e4SLinus Torvalds 		msleep(1000);	/* sleep for 1s */
37821da177e4SLinus Torvalds 
37831da177e4SLinus Torvalds 	rval = mega_do_del_logdrv(adapter, logdrv);
37841da177e4SLinus Torvalds 
37851da177e4SLinus Torvalds 	spin_lock_irqsave(&adapter->lock, flags);
37861da177e4SLinus Torvalds 
37871da177e4SLinus Torvalds 	/*
37881da177e4SLinus Torvalds 	 * If delete operation was successful, add 0x80 to the logical drive
37891da177e4SLinus Torvalds 	 * ids for commands in the pending queue.
37901da177e4SLinus Torvalds 	 */
37911da177e4SLinus Torvalds 	if (adapter->read_ldidmap) {
37921da177e4SLinus Torvalds 		struct list_head *pos;
37931da177e4SLinus Torvalds 		list_for_each(pos, &adapter->pending_list) {
37941da177e4SLinus Torvalds 			scb = list_entry(pos, scb_t, list);
37951da177e4SLinus Torvalds 			if (scb->pthru->logdrv < 0x80 )
37961da177e4SLinus Torvalds 				scb->pthru->logdrv += 0x80;
37971da177e4SLinus Torvalds 		}
37981da177e4SLinus Torvalds 	}
37991da177e4SLinus Torvalds 
38001da177e4SLinus Torvalds 	atomic_set(&adapter->quiescent, 0);
38011da177e4SLinus Torvalds 
38021da177e4SLinus Torvalds 	mega_runpendq(adapter);
38031da177e4SLinus Torvalds 
38041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adapter->lock, flags);
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds 	return rval;
38071da177e4SLinus Torvalds }
38081da177e4SLinus Torvalds 
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds static int
mega_do_del_logdrv(adapter_t * adapter,int logdrv)38111da177e4SLinus Torvalds mega_do_del_logdrv(adapter_t *adapter, int logdrv)
38121da177e4SLinus Torvalds {
38131da177e4SLinus Torvalds 	megacmd_t	mc;
38141da177e4SLinus Torvalds 	int	rval;
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds 	memset( &mc, 0, sizeof(megacmd_t));
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds 	mc.cmd = FC_DEL_LOGDRV;
38191da177e4SLinus Torvalds 	mc.opcode = OP_DEL_LOGDRV;
38201da177e4SLinus Torvalds 	mc.subopcode = logdrv;
38211da177e4SLinus Torvalds 
3822cb0258a2SChristoph Hellwig 	rval = mega_internal_command(adapter, &mc, NULL);
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds 	/* log this event */
38251da177e4SLinus Torvalds 	if(rval) {
38263b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "Delete LD-%d failed", logdrv);
38271da177e4SLinus Torvalds 		return rval;
38281da177e4SLinus Torvalds 	}
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds 	/*
38311da177e4SLinus Torvalds 	 * After deleting first logical drive, the logical drives must be
38321da177e4SLinus Torvalds 	 * addressed by adding 0x80 to the logical drive id.
38331da177e4SLinus Torvalds 	 */
38341da177e4SLinus Torvalds 	adapter->read_ldidmap = 1;
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	return rval;
38371da177e4SLinus Torvalds }
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds 
38401da177e4SLinus Torvalds /**
38411da177e4SLinus Torvalds  * mega_get_max_sgl()
38422b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
38431da177e4SLinus Torvalds  *
38441da177e4SLinus Torvalds  * Find out the maximum number of scatter-gather elements supported by this
38451da177e4SLinus Torvalds  * version of the firmware
38461da177e4SLinus Torvalds  */
38471da177e4SLinus Torvalds static void
mega_get_max_sgl(adapter_t * adapter)38481da177e4SLinus Torvalds mega_get_max_sgl(adapter_t *adapter)
38491da177e4SLinus Torvalds {
3850315d049aSKees Cook 	struct mbox_out	mbox;
3851315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
38521da177e4SLinus Torvalds 
3853315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
38541da177e4SLinus Torvalds 
38551da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
38561da177e4SLinus Torvalds 
3857315d049aSKees Cook 	mbox.xferaddr = (u32)adapter->buf_dma_handle;
38581da177e4SLinus Torvalds 
38591da177e4SLinus Torvalds 	raw_mbox[0] = MAIN_MISC_OPCODE;
38601da177e4SLinus Torvalds 	raw_mbox[2] = GET_MAX_SG_SUPPORT;
38611da177e4SLinus Torvalds 
38621da177e4SLinus Torvalds 
38631da177e4SLinus Torvalds 	if( issue_scb_block(adapter, raw_mbox) ) {
38641da177e4SLinus Torvalds 		/*
38651da177e4SLinus Torvalds 		 * f/w does not support this command. Choose the default value
38661da177e4SLinus Torvalds 		 */
38671da177e4SLinus Torvalds 		adapter->sglen = MIN_SGLIST;
38681da177e4SLinus Torvalds 	}
38691da177e4SLinus Torvalds 	else {
38701da177e4SLinus Torvalds 		adapter->sglen = *((char *)adapter->mega_buffer);
38711da177e4SLinus Torvalds 
38721da177e4SLinus Torvalds 		/*
38731da177e4SLinus Torvalds 		 * Make sure this is not more than the resources we are
38741da177e4SLinus Torvalds 		 * planning to allocate
38751da177e4SLinus Torvalds 		 */
38761da177e4SLinus Torvalds 		if ( adapter->sglen > MAX_SGLIST )
38771da177e4SLinus Torvalds 			adapter->sglen = MAX_SGLIST;
38781da177e4SLinus Torvalds 	}
38791da177e4SLinus Torvalds 
38801da177e4SLinus Torvalds 	return;
38811da177e4SLinus Torvalds }
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 
38841da177e4SLinus Torvalds /**
38851da177e4SLinus Torvalds  * mega_support_cluster()
38862b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
38871da177e4SLinus Torvalds  *
38881da177e4SLinus Torvalds  * Find out if this firmware support cluster calls.
38891da177e4SLinus Torvalds  */
38901da177e4SLinus Torvalds static int
mega_support_cluster(adapter_t * adapter)38911da177e4SLinus Torvalds mega_support_cluster(adapter_t *adapter)
38921da177e4SLinus Torvalds {
3893315d049aSKees Cook 	struct mbox_out	mbox;
3894315d049aSKees Cook 	unsigned char	*raw_mbox = (u8 *)&mbox;
38951da177e4SLinus Torvalds 
3896315d049aSKees Cook 	memset(&mbox, 0, sizeof(mbox));
38971da177e4SLinus Torvalds 
38981da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
38991da177e4SLinus Torvalds 
3900315d049aSKees Cook 	mbox.xferaddr = (u32)adapter->buf_dma_handle;
39011da177e4SLinus Torvalds 
39021da177e4SLinus Torvalds 	/*
39031da177e4SLinus Torvalds 	 * Try to get the initiator id. This command will succeed iff the
39041da177e4SLinus Torvalds 	 * clustering is available on this HBA.
39051da177e4SLinus Torvalds 	 */
39061da177e4SLinus Torvalds 	raw_mbox[0] = MEGA_GET_TARGET_ID;
39071da177e4SLinus Torvalds 
39081da177e4SLinus Torvalds 	if( issue_scb_block(adapter, raw_mbox) == 0 ) {
39091da177e4SLinus Torvalds 
39101da177e4SLinus Torvalds 		/*
39111da177e4SLinus Torvalds 		 * Cluster support available. Get the initiator target id.
39121da177e4SLinus Torvalds 		 * Tell our id to mid-layer too.
39131da177e4SLinus Torvalds 		 */
39141da177e4SLinus Torvalds 		adapter->this_id = *(u32 *)adapter->mega_buffer;
39151da177e4SLinus Torvalds 		adapter->host->this_id = adapter->this_id;
39161da177e4SLinus Torvalds 
39171da177e4SLinus Torvalds 		return 1;
39181da177e4SLinus Torvalds 	}
39191da177e4SLinus Torvalds 
39201da177e4SLinus Torvalds 	return 0;
39211da177e4SLinus Torvalds }
39221da177e4SLinus Torvalds 
392384a3c97bSwalter harms #ifdef CONFIG_PROC_FS
39241da177e4SLinus Torvalds /**
39251da177e4SLinus Torvalds  * mega_adapinq()
39262b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
39272b46e5c1SDamien Le Moal  * @dma_handle: DMA address of the buffer
39281da177e4SLinus Torvalds  *
39298e572babSJustin P. Mattock  * Issue internal commands while interrupts are available.
39301da177e4SLinus Torvalds  * We only issue direct mailbox commands from within the driver. ioctl()
39311da177e4SLinus Torvalds  * interface using these routines can issue passthru commands.
39321da177e4SLinus Torvalds  */
39331da177e4SLinus Torvalds static int
mega_adapinq(adapter_t * adapter,dma_addr_t dma_handle)39341da177e4SLinus Torvalds mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
39351da177e4SLinus Torvalds {
39361da177e4SLinus Torvalds 	megacmd_t	mc;
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds 	memset(&mc, 0, sizeof(megacmd_t));
39391da177e4SLinus Torvalds 
39401da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
39411da177e4SLinus Torvalds 		mc.cmd = FC_NEW_CONFIG;
39421da177e4SLinus Torvalds 		mc.opcode = NC_SUBOP_ENQUIRY3;
39431da177e4SLinus Torvalds 		mc.subopcode = ENQ3_GET_SOLICITED_FULL;
39441da177e4SLinus Torvalds 	}
39451da177e4SLinus Torvalds 	else {
39461da177e4SLinus Torvalds 		mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
39471da177e4SLinus Torvalds 	}
39481da177e4SLinus Torvalds 
39491da177e4SLinus Torvalds 	mc.xferaddr = (u32)dma_handle;
39501da177e4SLinus Torvalds 
3951cb0258a2SChristoph Hellwig 	if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
39521da177e4SLinus Torvalds 		return -1;
39531da177e4SLinus Torvalds 	}
39541da177e4SLinus Torvalds 
39551da177e4SLinus Torvalds 	return 0;
39561da177e4SLinus Torvalds }
39571da177e4SLinus Torvalds 
39581da177e4SLinus Torvalds 
39592b46e5c1SDamien Le Moal /**
39602b46e5c1SDamien Le Moal  * mega_internal_dev_inquiry()
39612b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
39622b46e5c1SDamien Le Moal  * @ch: channel for this device
39632b46e5c1SDamien Le Moal  * @tgt: ID of this device
39642b46e5c1SDamien Le Moal  * @buf_dma_handle: DMA address of the buffer
39651da177e4SLinus Torvalds  *
39661da177e4SLinus Torvalds  * Issue the scsi inquiry for the specified device.
39671da177e4SLinus Torvalds  */
39681da177e4SLinus Torvalds static int
mega_internal_dev_inquiry(adapter_t * adapter,u8 ch,u8 tgt,dma_addr_t buf_dma_handle)39691da177e4SLinus Torvalds mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
39701da177e4SLinus Torvalds 		dma_addr_t buf_dma_handle)
39711da177e4SLinus Torvalds {
39721da177e4SLinus Torvalds 	mega_passthru	*pthru;
39731da177e4SLinus Torvalds 	dma_addr_t	pthru_dma_handle;
39741da177e4SLinus Torvalds 	megacmd_t	mc;
39751da177e4SLinus Torvalds 	int		rval;
39761da177e4SLinus Torvalds 	struct pci_dev	*pdev;
39771da177e4SLinus Torvalds 
39781da177e4SLinus Torvalds 
39791da177e4SLinus Torvalds 	/*
39801da177e4SLinus Torvalds 	 * For all internal commands, the buffer must be allocated in <4GB
39811da177e4SLinus Torvalds 	 * address range
39821da177e4SLinus Torvalds 	 */
39831da177e4SLinus Torvalds 	if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
39841da177e4SLinus Torvalds 
3985ec090ef8SSuraj Upadhyay 	pthru = dma_alloc_coherent(&pdev->dev, sizeof(mega_passthru),
3986ec090ef8SSuraj Upadhyay 				   &pthru_dma_handle, GFP_KERNEL);
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds 	if( pthru == NULL ) {
39891da177e4SLinus Torvalds 		free_local_pdev(pdev);
39901da177e4SLinus Torvalds 		return -1;
39911da177e4SLinus Torvalds 	}
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds 	pthru->timeout = 2;
39941da177e4SLinus Torvalds 	pthru->ars = 1;
39951da177e4SLinus Torvalds 	pthru->reqsenselen = 14;
39961da177e4SLinus Torvalds 	pthru->islogical = 0;
39971da177e4SLinus Torvalds 
39981da177e4SLinus Torvalds 	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
39991da177e4SLinus Torvalds 
40001da177e4SLinus Torvalds 	pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
40011da177e4SLinus Torvalds 
40021da177e4SLinus Torvalds 	pthru->cdblen = 6;
40031da177e4SLinus Torvalds 
40041da177e4SLinus Torvalds 	pthru->cdb[0] = INQUIRY;
40051da177e4SLinus Torvalds 	pthru->cdb[1] = 0;
40061da177e4SLinus Torvalds 	pthru->cdb[2] = 0;
40071da177e4SLinus Torvalds 	pthru->cdb[3] = 0;
40081da177e4SLinus Torvalds 	pthru->cdb[4] = 255;
40091da177e4SLinus Torvalds 	pthru->cdb[5] = 0;
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 
40121da177e4SLinus Torvalds 	pthru->dataxferaddr = (u32)buf_dma_handle;
40131da177e4SLinus Torvalds 	pthru->dataxferlen = 256;
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds 	memset(&mc, 0, sizeof(megacmd_t));
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds 	mc.cmd = MEGA_MBOXCMD_PASSTHRU;
40181da177e4SLinus Torvalds 	mc.xferaddr = (u32)pthru_dma_handle;
40191da177e4SLinus Torvalds 
4020cb0258a2SChristoph Hellwig 	rval = mega_internal_command(adapter, &mc, pthru);
40211da177e4SLinus Torvalds 
4022ec090ef8SSuraj Upadhyay 	dma_free_coherent(&pdev->dev, sizeof(mega_passthru), pthru,
40231da177e4SLinus Torvalds 			  pthru_dma_handle);
40241da177e4SLinus Torvalds 
40251da177e4SLinus Torvalds 	free_local_pdev(pdev);
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds 	return rval;
40281da177e4SLinus Torvalds }
402984a3c97bSwalter harms #endif
40301da177e4SLinus Torvalds 
40311da177e4SLinus Torvalds /**
40321da177e4SLinus Torvalds  * mega_internal_command()
40332b46e5c1SDamien Le Moal  * @adapter: pointer to our soft state
40342b46e5c1SDamien Le Moal  * @mc: the mailbox command
40352b46e5c1SDamien Le Moal  * @pthru: Passthru structure for DCDB commands
40361da177e4SLinus Torvalds  *
40371da177e4SLinus Torvalds  * Issue the internal commands in interrupt mode.
40381da177e4SLinus Torvalds  * The last argument is the address of the passthru structure if the command
40391da177e4SLinus Torvalds  * to be fired is a passthru command
40401da177e4SLinus Torvalds  *
40411da177e4SLinus Torvalds  * Note: parameter 'pthru' is null for non-passthru commands.
40421da177e4SLinus Torvalds  */
40431da177e4SLinus Torvalds static int
mega_internal_command(adapter_t * adapter,megacmd_t * mc,mega_passthru * pthru)4044cb0258a2SChristoph Hellwig mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
40451da177e4SLinus Torvalds {
40460f2bb84dSChristoph Hellwig 	unsigned long flags;
40471da177e4SLinus Torvalds 	scb_t	*scb;
40481da177e4SLinus Torvalds 	int	rval;
40491da177e4SLinus Torvalds 
40501da177e4SLinus Torvalds 	/*
40511da177e4SLinus Torvalds 	 * The internal commands share one command id and hence are
40521da177e4SLinus Torvalds 	 * serialized. This is so because we want to reserve maximum number of
40531da177e4SLinus Torvalds 	 * available command ids for the I/O commands.
40541da177e4SLinus Torvalds 	 */
40550b950672SArjan van de Ven 	mutex_lock(&adapter->int_mtx);
40561da177e4SLinus Torvalds 
40571da177e4SLinus Torvalds 	scb = &adapter->int_scb;
40581da177e4SLinus Torvalds 	memset(scb, 0, sizeof(scb_t));
40591da177e4SLinus Torvalds 
40600f2bb84dSChristoph Hellwig 	scb->idx = CMDID_INT_CMDS;
40610f2bb84dSChristoph Hellwig 	scb->state |= SCB_ACTIVE | SCB_PENDQ;
40621da177e4SLinus Torvalds 
40631da177e4SLinus Torvalds 	memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
40641da177e4SLinus Torvalds 
40651da177e4SLinus Torvalds 	/*
40661da177e4SLinus Torvalds 	 * Is it a passthru command
40671da177e4SLinus Torvalds 	 */
40680f2bb84dSChristoph Hellwig 	if (mc->cmd == MEGA_MBOXCMD_PASSTHRU)
40691da177e4SLinus Torvalds 		scb->pthru = pthru;
40701da177e4SLinus Torvalds 
40710f2bb84dSChristoph Hellwig 	spin_lock_irqsave(&adapter->lock, flags);
40720f2bb84dSChristoph Hellwig 	list_add_tail(&scb->list, &adapter->pending_list);
40730f2bb84dSChristoph Hellwig 	/*
40740f2bb84dSChristoph Hellwig 	 * Check if the HBA is in quiescent state, e.g., during a
40750f2bb84dSChristoph Hellwig 	 * delete logical drive opertion. If it is, don't run
40760f2bb84dSChristoph Hellwig 	 * the pending_list.
40770f2bb84dSChristoph Hellwig 	 */
40780f2bb84dSChristoph Hellwig 	if (atomic_read(&adapter->quiescent) == 0)
40790f2bb84dSChristoph Hellwig 		mega_runpendq(adapter);
40800f2bb84dSChristoph Hellwig 	spin_unlock_irqrestore(&adapter->lock, flags);
40811da177e4SLinus Torvalds 
40828d115f84SChristoph Hellwig 	wait_for_completion(&adapter->int_waitq);
40831da177e4SLinus Torvalds 
40840f2bb84dSChristoph Hellwig 	mc->status = rval = adapter->int_status;
40851da177e4SLinus Torvalds 
40861da177e4SLinus Torvalds 	/*
40871da177e4SLinus Torvalds 	 * Print a debug message for all failed commands. Applications can use
40881da177e4SLinus Torvalds 	 * this information.
40891da177e4SLinus Torvalds 	 */
40900f2bb84dSChristoph Hellwig 	if (rval && trace_level) {
40913b8a1ba3SBjorn Helgaas 		dev_info(&adapter->dev->dev, "cmd [%x, %x, %x] status:[%x]\n",
40920f2bb84dSChristoph Hellwig 			mc->cmd, mc->opcode, mc->subopcode, rval);
40931da177e4SLinus Torvalds 	}
40941da177e4SLinus Torvalds 
40950b950672SArjan van de Ven 	mutex_unlock(&adapter->int_mtx);
40961da177e4SLinus Torvalds 	return rval;
40971da177e4SLinus Torvalds }
40981da177e4SLinus Torvalds 
4099264e222bSBart Van Assche static const struct scsi_host_template megaraid_template = {
41001da177e4SLinus Torvalds 	.module				= THIS_MODULE,
41011da177e4SLinus Torvalds 	.name				= "MegaRAID",
41023492b328SJu, Seokmann 	.proc_name			= "megaraid_legacy",
41031da177e4SLinus Torvalds 	.info				= megaraid_info,
41041da177e4SLinus Torvalds 	.queuecommand			= megaraid_queue,
41051da177e4SLinus Torvalds 	.bios_param			= megaraid_biosparam,
41061da177e4SLinus Torvalds 	.max_sectors			= MAX_SECTORS_PER_IO,
41071da177e4SLinus Torvalds 	.can_queue			= MAX_COMMANDS,
41081da177e4SLinus Torvalds 	.this_id			= DEFAULT_INITIATOR_ID,
41091da177e4SLinus Torvalds 	.sg_tablesize			= MAX_SGLIST,
41101da177e4SLinus Torvalds 	.cmd_per_lun			= DEF_CMD_PER_LUN,
41111da177e4SLinus Torvalds 	.eh_abort_handler		= megaraid_abort,
41121da177e4SLinus Torvalds 	.eh_host_reset_handler		= megaraid_reset,
411354b2b50cSMartin K. Petersen 	.no_write_same			= 1,
4114fb597392SBart Van Assche 	.cmd_size			= sizeof(struct megaraid_cmd_priv),
41151da177e4SLinus Torvalds };
41161da177e4SLinus Torvalds 
41176f039790SGreg Kroah-Hartman static int
megaraid_probe_one(struct pci_dev * pdev,const struct pci_device_id * id)41181da177e4SLinus Torvalds megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
41191da177e4SLinus Torvalds {
41201da177e4SLinus Torvalds 	struct Scsi_Host *host;
41211da177e4SLinus Torvalds 	adapter_t *adapter;
41221da177e4SLinus Torvalds 	unsigned long mega_baseport, tbase, flag = 0;
41231da177e4SLinus Torvalds 	u16 subsysid, subsysvid;
41241da177e4SLinus Torvalds 	u8 pci_bus, pci_dev_func;
41251da177e4SLinus Torvalds 	int irq, i, j;
41261da177e4SLinus Torvalds 	int error = -ENODEV;
41271da177e4SLinus Torvalds 
412827e833daSDan Carpenter 	if (hba_count >= MAX_CONTROLLERS)
412927e833daSDan Carpenter 		goto out;
413027e833daSDan Carpenter 
41311da177e4SLinus Torvalds 	if (pci_enable_device(pdev))
41321da177e4SLinus Torvalds 		goto out;
41331da177e4SLinus Torvalds 	pci_set_master(pdev);
41341da177e4SLinus Torvalds 
41351da177e4SLinus Torvalds 	pci_bus = pdev->bus->number;
41361da177e4SLinus Torvalds 	pci_dev_func = pdev->devfn;
41371da177e4SLinus Torvalds 
41381da177e4SLinus Torvalds 	/*
41391da177e4SLinus Torvalds 	 * The megaraid3 stuff reports the ID of the Intel part which is not
41401da177e4SLinus Torvalds 	 * remotely specific to the megaraid
41411da177e4SLinus Torvalds 	 */
41421da177e4SLinus Torvalds 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
41431da177e4SLinus Torvalds 		u16 magic;
41441da177e4SLinus Torvalds 		/*
41451da177e4SLinus Torvalds 		 * Don't fall over the Compaq management cards using the same
41461da177e4SLinus Torvalds 		 * PCI identifier
41471da177e4SLinus Torvalds 		 */
41481da177e4SLinus Torvalds 		if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
41491da177e4SLinus Torvalds 		    pdev->subsystem_device == 0xC000)
415070054aa3SXiang Chen 			goto out_disable_device;
41511da177e4SLinus Torvalds 		/* Now check the magic signature byte */
41521da177e4SLinus Torvalds 		pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
41531da177e4SLinus Torvalds 		if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
415470054aa3SXiang Chen 			goto out_disable_device;
41551da177e4SLinus Torvalds 		/* Ok it is probably a megaraid */
41561da177e4SLinus Torvalds 	}
41571da177e4SLinus Torvalds 
41581da177e4SLinus Torvalds 	/*
41591da177e4SLinus Torvalds 	 * For these vendor and device ids, signature offsets are not
41601da177e4SLinus Torvalds 	 * valid and 64 bit is implicit
41611da177e4SLinus Torvalds 	 */
41621da177e4SLinus Torvalds 	if (id->driver_data & BOARD_64BIT)
41631da177e4SLinus Torvalds 		flag |= BOARD_64BIT;
41641da177e4SLinus Torvalds 	else {
41651da177e4SLinus Torvalds 		u32 magic64;
41661da177e4SLinus Torvalds 
41671da177e4SLinus Torvalds 		pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
41681da177e4SLinus Torvalds 		if (magic64 == HBA_SIGNATURE_64BIT)
41691da177e4SLinus Torvalds 			flag |= BOARD_64BIT;
41701da177e4SLinus Torvalds 	}
41711da177e4SLinus Torvalds 
41721da177e4SLinus Torvalds 	subsysvid = pdev->subsystem_vendor;
41731da177e4SLinus Torvalds 	subsysid = pdev->subsystem_device;
41741da177e4SLinus Torvalds 
41753b8a1ba3SBjorn Helgaas 	dev_notice(&pdev->dev, "found 0x%4.04x:0x%4.04x\n",
41763b8a1ba3SBjorn Helgaas 		id->vendor, id->device);
41771da177e4SLinus Torvalds 
41781da177e4SLinus Torvalds 	/* Read the base port and IRQ from PCI */
41791da177e4SLinus Torvalds 	mega_baseport = pci_resource_start(pdev, 0);
41801da177e4SLinus Torvalds 	irq = pdev->irq;
41811da177e4SLinus Torvalds 
41821da177e4SLinus Torvalds 	tbase = mega_baseport;
41831da177e4SLinus Torvalds 	if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
41841da177e4SLinus Torvalds 		flag |= BOARD_MEMMAP;
41851da177e4SLinus Torvalds 
41861da177e4SLinus Torvalds 		if (!request_mem_region(mega_baseport, 128, "megaraid")) {
41873b8a1ba3SBjorn Helgaas 			dev_warn(&pdev->dev, "mem region busy!\n");
41881da177e4SLinus Torvalds 			goto out_disable_device;
41891da177e4SLinus Torvalds 		}
41901da177e4SLinus Torvalds 
41911da177e4SLinus Torvalds 		mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
41921da177e4SLinus Torvalds 		if (!mega_baseport) {
41933b8a1ba3SBjorn Helgaas 			dev_warn(&pdev->dev, "could not map hba memory\n");
41941da177e4SLinus Torvalds 			goto out_release_region;
41951da177e4SLinus Torvalds 		}
41961da177e4SLinus Torvalds 	} else {
41971da177e4SLinus Torvalds 		flag |= BOARD_IOMAP;
41981da177e4SLinus Torvalds 		mega_baseport += 0x10;
41991da177e4SLinus Torvalds 
42001da177e4SLinus Torvalds 		if (!request_region(mega_baseport, 16, "megaraid"))
42011da177e4SLinus Torvalds 			goto out_disable_device;
42021da177e4SLinus Torvalds 	}
42031da177e4SLinus Torvalds 
42041da177e4SLinus Torvalds 	/* Initialize SCSI Host structure */
42051da177e4SLinus Torvalds 	host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
42061da177e4SLinus Torvalds 	if (!host)
42071da177e4SLinus Torvalds 		goto out_iounmap;
42081da177e4SLinus Torvalds 
42091da177e4SLinus Torvalds 	adapter = (adapter_t *)host->hostdata;
42101da177e4SLinus Torvalds 	memset(adapter, 0, sizeof(adapter_t));
42111da177e4SLinus Torvalds 
42123b8a1ba3SBjorn Helgaas 	dev_notice(&pdev->dev,
42131da177e4SLinus Torvalds 		"scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
42141da177e4SLinus Torvalds 		host->host_no, mega_baseport, irq);
42151da177e4SLinus Torvalds 
42161da177e4SLinus Torvalds 	adapter->base = mega_baseport;
421700769ec4SJeff Garzik 	if (flag & BOARD_MEMMAP)
421800769ec4SJeff Garzik 		adapter->mmio_base = (void __iomem *) mega_baseport;
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->free_list);
42211da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->pending_list);
42221da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->completed_list);
42231da177e4SLinus Torvalds 
42241da177e4SLinus Torvalds 	adapter->flag = flag;
42251da177e4SLinus Torvalds 	spin_lock_init(&adapter->lock);
42261da177e4SLinus Torvalds 
42271da177e4SLinus Torvalds 	host->cmd_per_lun = max_cmd_per_lun;
42281da177e4SLinus Torvalds 	host->max_sectors = max_sectors_per_io;
42291da177e4SLinus Torvalds 
42301da177e4SLinus Torvalds 	adapter->dev = pdev;
42311da177e4SLinus Torvalds 	adapter->host = host;
42321da177e4SLinus Torvalds 
42331da177e4SLinus Torvalds 	adapter->host->irq = irq;
42341da177e4SLinus Torvalds 
42351da177e4SLinus Torvalds 	if (flag & BOARD_MEMMAP)
42361da177e4SLinus Torvalds 		adapter->host->base = tbase;
42371da177e4SLinus Torvalds 	else {
42381da177e4SLinus Torvalds 		adapter->host->io_port = tbase;
42391da177e4SLinus Torvalds 		adapter->host->n_io_port = 16;
42401da177e4SLinus Torvalds 	}
42411da177e4SLinus Torvalds 
42421da177e4SLinus Torvalds 	adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
42431da177e4SLinus Torvalds 
42441da177e4SLinus Torvalds 	/*
42451da177e4SLinus Torvalds 	 * Allocate buffer to issue internal commands.
42461da177e4SLinus Torvalds 	 */
4247ec090ef8SSuraj Upadhyay 	adapter->mega_buffer = dma_alloc_coherent(&adapter->dev->dev,
4248ec090ef8SSuraj Upadhyay 						  MEGA_BUFFER_SIZE,
4249ec090ef8SSuraj Upadhyay 						  &adapter->buf_dma_handle,
4250ec090ef8SSuraj Upadhyay 						  GFP_KERNEL);
42511da177e4SLinus Torvalds 	if (!adapter->mega_buffer) {
42523b8a1ba3SBjorn Helgaas 		dev_warn(&pdev->dev, "out of RAM\n");
42531da177e4SLinus Torvalds 		goto out_host_put;
42541da177e4SLinus Torvalds 	}
42551da177e4SLinus Torvalds 
42566da2ec56SKees Cook 	adapter->scb_list = kmalloc_array(MAX_COMMANDS, sizeof(scb_t),
42576da2ec56SKees Cook 					  GFP_KERNEL);
42581da177e4SLinus Torvalds 	if (!adapter->scb_list) {
42593b8a1ba3SBjorn Helgaas 		dev_warn(&pdev->dev, "out of RAM\n");
42601da177e4SLinus Torvalds 		goto out_free_cmd_buffer;
42611da177e4SLinus Torvalds 	}
42621da177e4SLinus Torvalds 
42631da177e4SLinus Torvalds 	if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
42641da177e4SLinus Torvalds 				megaraid_isr_memmapped : megaraid_isr_iomapped,
42651d6f359aSThomas Gleixner 					IRQF_SHARED, "megaraid", adapter)) {
42663b8a1ba3SBjorn Helgaas 		dev_warn(&pdev->dev, "Couldn't register IRQ %d!\n", irq);
42671da177e4SLinus Torvalds 		goto out_free_scb_list;
42681da177e4SLinus Torvalds 	}
42691da177e4SLinus Torvalds 
42701da177e4SLinus Torvalds 	if (mega_setup_mailbox(adapter))
42711da177e4SLinus Torvalds 		goto out_free_irq;
42721da177e4SLinus Torvalds 
42731da177e4SLinus Torvalds 	if (mega_query_adapter(adapter))
42741da177e4SLinus Torvalds 		goto out_free_mbox;
42751da177e4SLinus Torvalds 
42761da177e4SLinus Torvalds 	/*
42771da177e4SLinus Torvalds 	 * Have checks for some buggy f/w
42781da177e4SLinus Torvalds 	 */
42791da177e4SLinus Torvalds 	if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
42801da177e4SLinus Torvalds 		/*
42811da177e4SLinus Torvalds 		 * Which firmware
42821da177e4SLinus Torvalds 		 */
42831da177e4SLinus Torvalds 		if (!strcmp(adapter->fw_version, "3.00") ||
42841da177e4SLinus Torvalds 				!strcmp(adapter->fw_version, "3.01")) {
42851da177e4SLinus Torvalds 
42863b8a1ba3SBjorn Helgaas 			dev_warn(&pdev->dev,
42873b8a1ba3SBjorn Helgaas 				"Your card is a Dell PERC "
42881da177e4SLinus Torvalds 				"2/SC RAID controller with "
42891da177e4SLinus Torvalds 				"firmware\nmegaraid: 3.00 or 3.01.  "
42901da177e4SLinus Torvalds 				"This driver is known to have "
42911da177e4SLinus Torvalds 				"corruption issues\nmegaraid: with "
42921da177e4SLinus Torvalds 				"those firmware versions on this "
42931da177e4SLinus Torvalds 				"specific card.  In order\nmegaraid: "
42941da177e4SLinus Torvalds 				"to protect your data, please upgrade "
42951da177e4SLinus Torvalds 				"your firmware to version\nmegaraid: "
42961da177e4SLinus Torvalds 				"3.10 or later, available from the "
42971da177e4SLinus Torvalds 				"Dell Technical Support web\n"
42981da177e4SLinus Torvalds 				"megaraid: site at\nhttp://support."
42991da177e4SLinus Torvalds 				"dell.com/us/en/filelib/download/"
43001da177e4SLinus Torvalds 				"index.asp?fileid=2940\n"
43011da177e4SLinus Torvalds 			);
43021da177e4SLinus Torvalds 		}
43031da177e4SLinus Torvalds 	}
43041da177e4SLinus Torvalds 
43051da177e4SLinus Torvalds 	/*
43061da177e4SLinus Torvalds 	 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
43071da177e4SLinus Torvalds 	 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
43081da177e4SLinus Torvalds 	 * support, since this firmware cannot handle 64 bit
43091da177e4SLinus Torvalds 	 * addressing
43101da177e4SLinus Torvalds 	 */
431154ebfd57SJon Mason 	if ((subsysvid == PCI_VENDOR_ID_HP) &&
43121da177e4SLinus Torvalds 	    ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
43131da177e4SLinus Torvalds 		/*
43141da177e4SLinus Torvalds 		 * which firmware
43151da177e4SLinus Torvalds 		 */
43161da177e4SLinus Torvalds 		if (!strcmp(adapter->fw_version, "H01.07") ||
43171da177e4SLinus Torvalds 		    !strcmp(adapter->fw_version, "H01.08") ||
43181da177e4SLinus Torvalds 		    !strcmp(adapter->fw_version, "H01.09") ) {
43193b8a1ba3SBjorn Helgaas 			dev_warn(&pdev->dev,
43203b8a1ba3SBjorn Helgaas 				"Firmware H.01.07, "
43211da177e4SLinus Torvalds 				"H.01.08, and H.01.09 on 1M/2M "
43221da177e4SLinus Torvalds 				"controllers\n"
43233b8a1ba3SBjorn Helgaas 				"do not support 64 bit "
43243b8a1ba3SBjorn Helgaas 				"addressing.\nDISABLING "
43251da177e4SLinus Torvalds 				"64 bit support.\n");
43261da177e4SLinus Torvalds 			adapter->flag &= ~BOARD_64BIT;
43271da177e4SLinus Torvalds 		}
43281da177e4SLinus Torvalds 	}
43291da177e4SLinus Torvalds 
43301da177e4SLinus Torvalds 	if (mega_is_bios_enabled(adapter))
43311da177e4SLinus Torvalds 		mega_hbas[hba_count].is_bios_enabled = 1;
43321da177e4SLinus Torvalds 	mega_hbas[hba_count].hostdata_addr = adapter;
43331da177e4SLinus Torvalds 
43341da177e4SLinus Torvalds 	/*
43351da177e4SLinus Torvalds 	 * Find out which channel is raid and which is scsi. This is
43361da177e4SLinus Torvalds 	 * for ROMB support.
43371da177e4SLinus Torvalds 	 */
43381da177e4SLinus Torvalds 	mega_enum_raid_scsi(adapter);
43391da177e4SLinus Torvalds 
43401da177e4SLinus Torvalds 	/*
43411da177e4SLinus Torvalds 	 * Find out if a logical drive is set as the boot drive. If
43421da177e4SLinus Torvalds 	 * there is one, will make that as the first logical drive.
43431da177e4SLinus Torvalds 	 * ROMB: Do we have to boot from a physical drive. Then all
43441da177e4SLinus Torvalds 	 * the physical drives would appear before the logical disks.
43451da177e4SLinus Torvalds 	 * Else, all the physical drives would be exported to the mid
43461da177e4SLinus Torvalds 	 * layer after logical drives.
43471da177e4SLinus Torvalds 	 */
43481da177e4SLinus Torvalds 	mega_get_boot_drv(adapter);
43491da177e4SLinus Torvalds 
43501da177e4SLinus Torvalds 	if (adapter->boot_pdrv_enabled) {
43511da177e4SLinus Torvalds 		j = adapter->product_info.nchannels;
43521da177e4SLinus Torvalds 		for( i = 0; i < j; i++ )
43531da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 0;
43541da177e4SLinus Torvalds 		for( i = j; i < NVIRT_CHAN + j; i++ )
43551da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 1;
43561da177e4SLinus Torvalds 	} else {
43571da177e4SLinus Torvalds 		for (i = 0; i < NVIRT_CHAN; i++)
43581da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 1;
43591da177e4SLinus Torvalds 		for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
43601da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 0;
43611da177e4SLinus Torvalds 		adapter->mega_ch_class <<= NVIRT_CHAN;
43621da177e4SLinus Torvalds 	}
43631da177e4SLinus Torvalds 
43641da177e4SLinus Torvalds 	/*
43651da177e4SLinus Torvalds 	 * Do we support random deletion and addition of logical
43661da177e4SLinus Torvalds 	 * drives
43671da177e4SLinus Torvalds 	 */
43681da177e4SLinus Torvalds 	adapter->read_ldidmap = 0;	/* set it after first logdrv
43691da177e4SLinus Torvalds 						   delete cmd */
43701da177e4SLinus Torvalds 	adapter->support_random_del = mega_support_random_del(adapter);
43711da177e4SLinus Torvalds 
43721da177e4SLinus Torvalds 	/* Initialize SCBs */
43731da177e4SLinus Torvalds 	if (mega_init_scb(adapter))
43741da177e4SLinus Torvalds 		goto out_free_mbox;
43751da177e4SLinus Torvalds 
43761da177e4SLinus Torvalds 	/*
43771da177e4SLinus Torvalds 	 * Reset the pending commands counter
43781da177e4SLinus Torvalds 	 */
43791da177e4SLinus Torvalds 	atomic_set(&adapter->pend_cmds, 0);
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds 	/*
43821da177e4SLinus Torvalds 	 * Reset the adapter quiescent flag
43831da177e4SLinus Torvalds 	 */
43841da177e4SLinus Torvalds 	atomic_set(&adapter->quiescent, 0);
43851da177e4SLinus Torvalds 
43861da177e4SLinus Torvalds 	hba_soft_state[hba_count] = adapter;
43871da177e4SLinus Torvalds 
43881da177e4SLinus Torvalds 	/*
43891da177e4SLinus Torvalds 	 * Fill in the structure which needs to be passed back to the
43901da177e4SLinus Torvalds 	 * application when it does an ioctl() for controller related
43911da177e4SLinus Torvalds 	 * information.
43921da177e4SLinus Torvalds 	 */
43931da177e4SLinus Torvalds 	i = hba_count;
43941da177e4SLinus Torvalds 
43951da177e4SLinus Torvalds 	mcontroller[i].base = mega_baseport;
43961da177e4SLinus Torvalds 	mcontroller[i].irq = irq;
43971da177e4SLinus Torvalds 	mcontroller[i].numldrv = adapter->numldrv;
43981da177e4SLinus Torvalds 	mcontroller[i].pcibus = pci_bus;
43991da177e4SLinus Torvalds 	mcontroller[i].pcidev = id->device;
44001da177e4SLinus Torvalds 	mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
44011da177e4SLinus Torvalds 	mcontroller[i].pciid = -1;
44021da177e4SLinus Torvalds 	mcontroller[i].pcivendor = id->vendor;
44031da177e4SLinus Torvalds 	mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
44041da177e4SLinus Torvalds 	mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
44051da177e4SLinus Torvalds 
44061da177e4SLinus Torvalds 
44071da177e4SLinus Torvalds 	/* Set the Mode of addressing to 64 bit if we can */
44081da177e4SLinus Torvalds 	if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4409ec090ef8SSuraj Upadhyay 		dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
44101da177e4SLinus Torvalds 		adapter->has_64bit_addr = 1;
44111da177e4SLinus Torvalds 	} else  {
4412ec090ef8SSuraj Upadhyay 		dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
44131da177e4SLinus Torvalds 		adapter->has_64bit_addr = 0;
44141da177e4SLinus Torvalds 	}
44151da177e4SLinus Torvalds 
44160b950672SArjan van de Ven 	mutex_init(&adapter->int_mtx);
44178d115f84SChristoph Hellwig 	init_completion(&adapter->int_waitq);
44181da177e4SLinus Torvalds 
44191da177e4SLinus Torvalds 	adapter->this_id = DEFAULT_INITIATOR_ID;
44201da177e4SLinus Torvalds 	adapter->host->this_id = DEFAULT_INITIATOR_ID;
44211da177e4SLinus Torvalds 
44221da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
44231da177e4SLinus Torvalds 	/*
44241da177e4SLinus Torvalds 	 * Is cluster support enabled on this controller
44251da177e4SLinus Torvalds 	 * Note: In a cluster the HBAs ( the initiators ) will have
44261da177e4SLinus Torvalds 	 * different target IDs and we cannot assume it to be 7. Call
44271da177e4SLinus Torvalds 	 * to mega_support_cluster() will get the target ids also if
44281da177e4SLinus Torvalds 	 * the cluster support is available
44291da177e4SLinus Torvalds 	 */
44301da177e4SLinus Torvalds 	adapter->has_cluster = mega_support_cluster(adapter);
44311da177e4SLinus Torvalds 	if (adapter->has_cluster) {
44323b8a1ba3SBjorn Helgaas 		dev_notice(&pdev->dev,
44333b8a1ba3SBjorn Helgaas 			"Cluster driver, initiator id:%d\n",
44341da177e4SLinus Torvalds 			adapter->this_id);
44351da177e4SLinus Torvalds 	}
44361da177e4SLinus Torvalds #endif
44371da177e4SLinus Torvalds 
44381da177e4SLinus Torvalds 	pci_set_drvdata(pdev, host);
44391da177e4SLinus Torvalds 
44401da177e4SLinus Torvalds 	mega_create_proc_entry(hba_count, mega_proc_dir_entry);
44411da177e4SLinus Torvalds 
44421da177e4SLinus Torvalds 	error = scsi_add_host(host, &pdev->dev);
44431da177e4SLinus Torvalds 	if (error)
44441da177e4SLinus Torvalds 		goto out_free_mbox;
44451da177e4SLinus Torvalds 
44461da177e4SLinus Torvalds 	scsi_scan_host(host);
44471da177e4SLinus Torvalds 	hba_count++;
44481da177e4SLinus Torvalds 	return 0;
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds  out_free_mbox:
4451ec090ef8SSuraj Upadhyay 	dma_free_coherent(&adapter->dev->dev, sizeof(mbox64_t),
44521da177e4SLinus Torvalds 			  adapter->una_mbox64, adapter->una_mbox64_dma);
44531da177e4SLinus Torvalds  out_free_irq:
44541da177e4SLinus Torvalds 	free_irq(adapter->host->irq, adapter);
44551da177e4SLinus Torvalds  out_free_scb_list:
44561da177e4SLinus Torvalds 	kfree(adapter->scb_list);
44571da177e4SLinus Torvalds  out_free_cmd_buffer:
4458ec090ef8SSuraj Upadhyay 	dma_free_coherent(&adapter->dev->dev, MEGA_BUFFER_SIZE,
44591da177e4SLinus Torvalds 			  adapter->mega_buffer, adapter->buf_dma_handle);
44601da177e4SLinus Torvalds  out_host_put:
44611da177e4SLinus Torvalds 	scsi_host_put(host);
44621da177e4SLinus Torvalds  out_iounmap:
44631da177e4SLinus Torvalds 	if (flag & BOARD_MEMMAP)
44641da177e4SLinus Torvalds 		iounmap((void *)mega_baseport);
44651da177e4SLinus Torvalds  out_release_region:
44661da177e4SLinus Torvalds 	if (flag & BOARD_MEMMAP)
44671da177e4SLinus Torvalds 		release_mem_region(tbase, 128);
44681da177e4SLinus Torvalds 	else
44691da177e4SLinus Torvalds 		release_region(mega_baseport, 16);
44701da177e4SLinus Torvalds  out_disable_device:
44711da177e4SLinus Torvalds 	pci_disable_device(pdev);
44721da177e4SLinus Torvalds  out:
44731da177e4SLinus Torvalds 	return error;
44741da177e4SLinus Torvalds }
44751da177e4SLinus Torvalds 
44761da177e4SLinus Torvalds static void
__megaraid_shutdown(adapter_t * adapter)44771da177e4SLinus Torvalds __megaraid_shutdown(adapter_t *adapter)
44781da177e4SLinus Torvalds {
44791da177e4SLinus Torvalds 	u_char	raw_mbox[sizeof(struct mbox_out)];
44801da177e4SLinus Torvalds 	mbox_t	*mbox = (mbox_t *)raw_mbox;
44811da177e4SLinus Torvalds 	int	i;
44821da177e4SLinus Torvalds 
44831da177e4SLinus Torvalds 	/* Flush adapter cache */
44841da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
44851da177e4SLinus Torvalds 	raw_mbox[0] = FLUSH_ADAPTER;
44861da177e4SLinus Torvalds 
44871da177e4SLinus Torvalds 	free_irq(adapter->host->irq, adapter);
44881da177e4SLinus Torvalds 
44891da177e4SLinus Torvalds 	/* Issue a blocking (interrupts disabled) command to the card */
44901da177e4SLinus Torvalds 	issue_scb_block(adapter, raw_mbox);
44911da177e4SLinus Torvalds 
44921da177e4SLinus Torvalds 	/* Flush disks cache */
44931da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
44941da177e4SLinus Torvalds 	raw_mbox[0] = FLUSH_SYSTEM;
44951da177e4SLinus Torvalds 
44961da177e4SLinus Torvalds 	/* Issue a blocking (interrupts disabled) command to the card */
44971da177e4SLinus Torvalds 	issue_scb_block(adapter, raw_mbox);
44981da177e4SLinus Torvalds 
44991da177e4SLinus Torvalds 	if (atomic_read(&adapter->pend_cmds) > 0)
45003b8a1ba3SBjorn Helgaas 		dev_warn(&adapter->dev->dev, "pending commands!!\n");
45011da177e4SLinus Torvalds 
45021da177e4SLinus Torvalds 	/*
45031da177e4SLinus Torvalds 	 * Have a delibrate delay to make sure all the caches are
45041da177e4SLinus Torvalds 	 * actually flushed.
45051da177e4SLinus Torvalds 	 */
45061da177e4SLinus Torvalds 	for (i = 0; i <= 10; i++)
45071da177e4SLinus Torvalds 		mdelay(1000);
45081da177e4SLinus Torvalds }
45091da177e4SLinus Torvalds 
45106f039790SGreg Kroah-Hartman static void
megaraid_remove_one(struct pci_dev * pdev)45111da177e4SLinus Torvalds megaraid_remove_one(struct pci_dev *pdev)
45121da177e4SLinus Torvalds {
45131da177e4SLinus Torvalds 	struct Scsi_Host *host = pci_get_drvdata(pdev);
45141da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)host->hostdata;
4515f7680becSChristoph Hellwig 	char buf[12] = { 0 };
45161da177e4SLinus Torvalds 
45171da177e4SLinus Torvalds 	scsi_remove_host(host);
45181da177e4SLinus Torvalds 
45191da177e4SLinus Torvalds 	__megaraid_shutdown(adapter);
45201da177e4SLinus Torvalds 
45211da177e4SLinus Torvalds 	/* Free our resources */
45221da177e4SLinus Torvalds 	if (adapter->flag & BOARD_MEMMAP) {
45231da177e4SLinus Torvalds 		iounmap((void *)adapter->base);
45241da177e4SLinus Torvalds 		release_mem_region(adapter->host->base, 128);
45251da177e4SLinus Torvalds 	} else
45261da177e4SLinus Torvalds 		release_region(adapter->base, 16);
45271da177e4SLinus Torvalds 
45281da177e4SLinus Torvalds 	mega_free_sgl(adapter);
45291da177e4SLinus Torvalds 
45301da177e4SLinus Torvalds 	sprintf(buf, "hba%d", adapter->host->host_no);
4531f7680becSChristoph Hellwig 	remove_proc_subtree(buf, mega_proc_dir_entry);
45321da177e4SLinus Torvalds 
4533ec090ef8SSuraj Upadhyay 	dma_free_coherent(&adapter->dev->dev, MEGA_BUFFER_SIZE,
45341da177e4SLinus Torvalds 			  adapter->mega_buffer, adapter->buf_dma_handle);
45351da177e4SLinus Torvalds 	kfree(adapter->scb_list);
4536ec090ef8SSuraj Upadhyay 	dma_free_coherent(&adapter->dev->dev, sizeof(mbox64_t),
45371da177e4SLinus Torvalds 			  adapter->una_mbox64, adapter->una_mbox64_dma);
45381da177e4SLinus Torvalds 
45391da177e4SLinus Torvalds 	scsi_host_put(host);
45401da177e4SLinus Torvalds 	pci_disable_device(pdev);
45411da177e4SLinus Torvalds 
45421da177e4SLinus Torvalds 	hba_count--;
45431da177e4SLinus Torvalds }
45441da177e4SLinus Torvalds 
45451da177e4SLinus Torvalds static void
megaraid_shutdown(struct pci_dev * pdev)4546d18c3db5SGreg Kroah-Hartman megaraid_shutdown(struct pci_dev *pdev)
45471da177e4SLinus Torvalds {
4548d18c3db5SGreg Kroah-Hartman 	struct Scsi_Host *host = pci_get_drvdata(pdev);
45491da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)host->hostdata;
45501da177e4SLinus Torvalds 
45511da177e4SLinus Torvalds 	__megaraid_shutdown(adapter);
45521da177e4SLinus Torvalds }
45531da177e4SLinus Torvalds 
45541da177e4SLinus Torvalds static struct pci_device_id megaraid_pci_tbl[] = {
45551da177e4SLinus Torvalds 	{PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
45561da177e4SLinus Torvalds 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
45571da177e4SLinus Torvalds 	{PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
45581da177e4SLinus Torvalds 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
45591da177e4SLinus Torvalds 	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
45601da177e4SLinus Torvalds 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
45611da177e4SLinus Torvalds 	{0,}
45621da177e4SLinus Torvalds };
45631da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
45641da177e4SLinus Torvalds 
45651da177e4SLinus Torvalds static struct pci_driver megaraid_pci_driver = {
45663542adcbSJu, Seokmann 	.name		= "megaraid_legacy",
45671da177e4SLinus Torvalds 	.id_table	= megaraid_pci_tbl,
45681da177e4SLinus Torvalds 	.probe		= megaraid_probe_one,
45696f039790SGreg Kroah-Hartman 	.remove		= megaraid_remove_one,
45701da177e4SLinus Torvalds 	.shutdown	= megaraid_shutdown,
45711da177e4SLinus Torvalds };
45721da177e4SLinus Torvalds 
megaraid_init(void)45731da177e4SLinus Torvalds static int __init megaraid_init(void)
45741da177e4SLinus Torvalds {
45751da177e4SLinus Torvalds 	int error;
45761da177e4SLinus Torvalds 
45771da177e4SLinus Torvalds 	if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
45781da177e4SLinus Torvalds 		max_cmd_per_lun = MAX_CMD_PER_LUN;
45791da177e4SLinus Torvalds 	if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
45801da177e4SLinus Torvalds 		max_mbox_busy_wait = MBOX_BUSY_WAIT;
45811da177e4SLinus Torvalds 
45821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
4583c74c120aSAlexey Dobriyan 	mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
45841da177e4SLinus Torvalds 	if (!mega_proc_dir_entry) {
45851da177e4SLinus Torvalds 		printk(KERN_WARNING
45861da177e4SLinus Torvalds 				"megaraid: failed to create megaraid root\n");
45871da177e4SLinus Torvalds 	}
45881da177e4SLinus Torvalds #endif
45894520b008SRichard Knutsson 	error = pci_register_driver(&megaraid_pci_driver);
45901da177e4SLinus Torvalds 	if (error) {
45911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
4592c74c120aSAlexey Dobriyan 		remove_proc_entry("megaraid", NULL);
45931da177e4SLinus Torvalds #endif
45941da177e4SLinus Torvalds 		return error;
45951da177e4SLinus Torvalds 	}
45961da177e4SLinus Torvalds 
45971da177e4SLinus Torvalds 	/*
45981da177e4SLinus Torvalds 	 * Register the driver as a character device, for applications
45991da177e4SLinus Torvalds 	 * to access it for ioctls.
46001da177e4SLinus Torvalds 	 * First argument (major) to register_chrdev implies a dynamic
46011da177e4SLinus Torvalds 	 * major number allocation.
46021da177e4SLinus Torvalds 	 */
46033492b328SJu, Seokmann 	major = register_chrdev(0, "megadev_legacy", &megadev_fops);
4604c5acd61dSLv Ruyi 	if (major < 0) {
46051da177e4SLinus Torvalds 		printk(KERN_WARNING
46061da177e4SLinus Torvalds 				"megaraid: failed to register char device\n");
46071da177e4SLinus Torvalds 	}
46081da177e4SLinus Torvalds 
46091da177e4SLinus Torvalds 	return 0;
46101da177e4SLinus Torvalds }
46111da177e4SLinus Torvalds 
megaraid_exit(void)46121da177e4SLinus Torvalds static void __exit megaraid_exit(void)
46131da177e4SLinus Torvalds {
46141da177e4SLinus Torvalds 	/*
46151da177e4SLinus Torvalds 	 * Unregister the character device interface to the driver.
46161da177e4SLinus Torvalds 	 */
46173492b328SJu, Seokmann 	unregister_chrdev(major, "megadev_legacy");
46181da177e4SLinus Torvalds 
46191da177e4SLinus Torvalds 	pci_unregister_driver(&megaraid_pci_driver);
46201da177e4SLinus Torvalds 
46211da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
4622c74c120aSAlexey Dobriyan 	remove_proc_entry("megaraid", NULL);
46231da177e4SLinus Torvalds #endif
46241da177e4SLinus Torvalds }
46251da177e4SLinus Torvalds 
46261da177e4SLinus Torvalds module_init(megaraid_init);
46271da177e4SLinus Torvalds module_exit(megaraid_exit);
46281da177e4SLinus Torvalds 
46291da177e4SLinus Torvalds /* vi: set ts=8 sw=8 tw=78: */
4630