xref: /linux/drivers/scsi/megaraid.c (revision bab41e9be75121c473b00df2ffa33af3c44066a7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  *			Linux MegaRAID device driver
41da177e4SLinus Torvalds  *
53492b328SJu, Seokmann  * Copyright (c) 2002  LSI Logic Corporation.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	   This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	   modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  *	   as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  *	   2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
131da177e4SLinus Torvalds  *	  - fixes
141da177e4SLinus Torvalds  *	  - speed-ups (list handling fixes, issued_list, optimizations.)
151da177e4SLinus Torvalds  *	  - lots of cleanups.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Copyright (c) 2003  Christoph Hellwig  <hch@lst.de>
181da177e4SLinus Torvalds  *	  - new-style, hotplug-aware pci probing and scsi registration
191da177e4SLinus Torvalds  *
203492b328SJu, Seokmann  * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
213492b328SJu, Seokmann  * 						<Seokmann.Ju@lsil.com>
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Description: Linux device driver for LSI Logic MegaRAID controller
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
261da177e4SLinus Torvalds  *					518, 520, 531, 532
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
291da177e4SLinus Torvalds  * and others. Please send updates to the mailing list
301da177e4SLinus Torvalds  * linux-scsi@vger.kernel.org .
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <linux/mm.h>
351da177e4SLinus Torvalds #include <linux/fs.h>
361da177e4SLinus Torvalds #include <linux/blkdev.h>
371da177e4SLinus Torvalds #include <asm/uaccess.h>
381da177e4SLinus Torvalds #include <asm/io.h>
398d115f84SChristoph Hellwig #include <linux/completion.h>
401da177e4SLinus Torvalds #include <linux/delay.h>
411da177e4SLinus Torvalds #include <linux/proc_fs.h>
421da177e4SLinus Torvalds #include <linux/reboot.h>
431da177e4SLinus Torvalds #include <linux/module.h>
441da177e4SLinus Torvalds #include <linux/list.h>
451da177e4SLinus Torvalds #include <linux/interrupt.h>
461da177e4SLinus Torvalds #include <linux/pci.h>
471da177e4SLinus Torvalds #include <linux/init.h>
48910638aeSMatthias Gehre #include <linux/dma-mapping.h>
491da177e4SLinus Torvalds #include <scsi/scsicam.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include "scsi.h"
521da177e4SLinus Torvalds #include <scsi/scsi_host.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include "megaraid.h"
551da177e4SLinus Torvalds 
563492b328SJu, Seokmann #define MEGARAID_MODULE_VERSION "2.00.4"
571da177e4SLinus Torvalds 
583492b328SJu, Seokmann MODULE_AUTHOR ("sju@lsil.com");
593492b328SJu, Seokmann MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
601da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
611da177e4SLinus Torvalds MODULE_VERSION(MEGARAID_MODULE_VERSION);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
641da177e4SLinus Torvalds module_param(max_cmd_per_lun, uint, 0);
651da177e4SLinus 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)");
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
681da177e4SLinus Torvalds module_param(max_sectors_per_io, ushort, 0);
691da177e4SLinus Torvalds MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
731da177e4SLinus Torvalds module_param(max_mbox_busy_wait, ushort, 0);
741da177e4SLinus Torvalds MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
751da177e4SLinus Torvalds 
7600769ec4SJeff Garzik #define RDINDOOR(adapter)	readl((adapter)->mmio_base + 0x20)
7700769ec4SJeff Garzik #define RDOUTDOOR(adapter)	readl((adapter)->mmio_base + 0x2C)
7800769ec4SJeff Garzik #define WRINDOOR(adapter,value)	 writel(value, (adapter)->mmio_base + 0x20)
7900769ec4SJeff Garzik #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * Global variables
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static int hba_count;
861da177e4SLinus Torvalds static adapter_t *hba_soft_state[MAX_CONTROLLERS];
871da177e4SLinus Torvalds static struct proc_dir_entry *mega_proc_dir_entry;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* For controller re-ordering */
901da177e4SLinus Torvalds static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /*
931da177e4SLinus Torvalds  * The File Operations structure for the serial/ioctl interface of the driver
941da177e4SLinus Torvalds  */
9500977a59SArjan van de Ven static const struct file_operations megadev_fops = {
961da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
971da177e4SLinus Torvalds 	.ioctl		= megadev_ioctl,
981da177e4SLinus Torvalds 	.open		= megadev_open,
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds  * Array to structures for storing the information about the controllers. This
1031da177e4SLinus Torvalds  * information is sent to the user level applications, when they do an ioctl
1041da177e4SLinus Torvalds  * for this information.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static struct mcontroller mcontroller[MAX_CONTROLLERS];
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /* The current driver version */
1091da177e4SLinus Torvalds static u32 driver_ver = 0x02000000;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* major number used by the device for character interface */
1121da177e4SLinus Torvalds static int major;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #define IS_RAID_CH(hba, ch)	(((hba)->mega_ch_class >> (ch)) & 0x01)
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /*
1181da177e4SLinus Torvalds  * Debug variable to print some diagnostic messages
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds static int trace_level;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /**
1231da177e4SLinus Torvalds  * mega_setup_mailbox()
1241da177e4SLinus Torvalds  * @adapter - pointer to our soft state
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  * Allocates a 8 byte aligned memory for the handshake mailbox.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds static int
1291da177e4SLinus Torvalds mega_setup_mailbox(adapter_t *adapter)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	unsigned long	align;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
1341da177e4SLinus Torvalds 			sizeof(mbox64_t), &adapter->una_mbox64_dma);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	if( !adapter->una_mbox64 ) return -1;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	adapter->mbox = &adapter->una_mbox64->mbox;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
1411da177e4SLinus Torvalds 			(~0UL ^ 0xFUL));
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	/*
1501da177e4SLinus Torvalds 	 * Register the mailbox if the controller is an io-mapped controller
1511da177e4SLinus Torvalds 	 */
1521da177e4SLinus Torvalds 	if( adapter->flag & BOARD_IOMAP ) {
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 		outb_p(adapter->mbox_dma & 0xFF,
1551da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT0);
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 		outb_p((adapter->mbox_dma >> 8) & 0xFF,
1581da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT1);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 		outb_p((adapter->mbox_dma >> 16) & 0xFF,
1611da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT2);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 		outb_p((adapter->mbox_dma >> 24) & 0xFF,
1641da177e4SLinus Torvalds 				adapter->host->io_port + MBOX_PORT3);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 		outb_p(ENABLE_MBOX_BYTE,
1671da177e4SLinus Torvalds 				adapter->host->io_port + ENABLE_MBOX_REGION);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 		irq_ack(adapter);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 		irq_enable(adapter);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	return 0;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /*
1791da177e4SLinus Torvalds  * mega_query_adapter()
1801da177e4SLinus Torvalds  * @adapter - pointer to our soft state
1811da177e4SLinus Torvalds  *
1821da177e4SLinus Torvalds  * Issue the adapter inquiry commands to the controller and find out
1831da177e4SLinus Torvalds  * information and parameter about the devices attached
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds static int
1861da177e4SLinus Torvalds mega_query_adapter(adapter_t *adapter)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	dma_addr_t	prod_info_dma_handle;
1891da177e4SLinus Torvalds 	mega_inquiry3	*inquiry3;
1901da177e4SLinus Torvalds 	u8	raw_mbox[sizeof(struct mbox_out)];
1911da177e4SLinus Torvalds 	mbox_t	*mbox;
1921da177e4SLinus Torvalds 	int	retval;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* Initialize adapter inquiry mailbox */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
1991da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/*
2021da177e4SLinus Torvalds 	 * Try to issue Inquiry3 command
2031da177e4SLinus Torvalds 	 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
2041da177e4SLinus Torvalds 	 * update enquiry3 structure
2051da177e4SLinus Torvalds 	 */
2061da177e4SLinus Torvalds 	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	raw_mbox[0] = FC_NEW_CONFIG;		/* i.e. mbox->cmd=0xA1 */
2111da177e4SLinus Torvalds 	raw_mbox[2] = NC_SUBOP_ENQUIRY3;	/* i.e. 0x0F */
2121da177e4SLinus Torvalds 	raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;	/* i.e. 0x02 */
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	/* Issue a blocking command to the card */
2151da177e4SLinus Torvalds 	if ((retval = issue_scb_block(adapter, raw_mbox))) {
2161da177e4SLinus Torvalds 		/* the adapter does not support 40ld */
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 		mraid_ext_inquiry	*ext_inq;
2191da177e4SLinus Torvalds 		mraid_inquiry		*inq;
2201da177e4SLinus Torvalds 		dma_addr_t		dma_handle;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 		ext_inq = pci_alloc_consistent(adapter->dev,
2231da177e4SLinus Torvalds 				sizeof(mraid_ext_inquiry), &dma_handle);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 		if( ext_inq == NULL ) return -1;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 		inq = &ext_inq->raid_inq;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 		mbox->m_out.xferaddr = (u32)dma_handle;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		/*issue old 0x04 command to adapter */
2321da177e4SLinus Torvalds 		mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 		issue_scb_block(adapter, raw_mbox);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 		/*
2371da177e4SLinus Torvalds 		 * update Enquiry3 and ProductInfo structures with
2381da177e4SLinus Torvalds 		 * mraid_inquiry structure
2391da177e4SLinus Torvalds 		 */
2401da177e4SLinus Torvalds 		mega_8_to_40ld(inq, inquiry3,
2411da177e4SLinus Torvalds 				(mega_product_info *)&adapter->product_info);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 		pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
2441da177e4SLinus Torvalds 				ext_inq, dma_handle);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	} else {		/*adapter supports 40ld */
2471da177e4SLinus Torvalds 		adapter->flag |= BOARD_40LD;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 		/*
2501da177e4SLinus Torvalds 		 * get product_info, which is static information and will be
2511da177e4SLinus Torvalds 		 * unchanged
2521da177e4SLinus Torvalds 		 */
2531da177e4SLinus Torvalds 		prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
2541da177e4SLinus Torvalds 				&adapter->product_info,
2551da177e4SLinus Torvalds 				sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		mbox->m_out.xferaddr = prod_info_dma_handle;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 		raw_mbox[0] = FC_NEW_CONFIG;	/* i.e. mbox->cmd=0xA1 */
2601da177e4SLinus Torvalds 		raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;	/* i.e. 0x0E */
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 		if ((retval = issue_scb_block(adapter, raw_mbox)))
2631da177e4SLinus Torvalds 			printk(KERN_WARNING
2641da177e4SLinus Torvalds 			"megaraid: Product_info cmd failed with error: %d\n",
2651da177e4SLinus Torvalds 				retval);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		pci_unmap_single(adapter->dev, prod_info_dma_handle,
2681da177e4SLinus Torvalds 				sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/*
2731da177e4SLinus Torvalds 	 * kernel scans the channels from 0 to <= max_channel
2741da177e4SLinus Torvalds 	 */
2751da177e4SLinus Torvalds 	adapter->host->max_channel =
2761da177e4SLinus Torvalds 		adapter->product_info.nchannels + NVIRT_CHAN -1;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	adapter->host->max_id = 16;	/* max targets per channel */
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	adapter->host->max_lun = 7;	/* Upto 7 luns for non disk devices */
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	adapter->host->cmd_per_lun = max_cmd_per_lun;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	adapter->numldrv = inquiry3->num_ldrv;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	adapter->max_cmds = adapter->product_info.max_commands;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if(adapter->max_cmds > MAX_COMMANDS)
2891da177e4SLinus Torvalds 		adapter->max_cmds = MAX_COMMANDS;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	adapter->host->can_queue = adapter->max_cmds - 1;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	/*
2941da177e4SLinus Torvalds 	 * Get the maximum number of scatter-gather elements supported by this
2951da177e4SLinus Torvalds 	 * firmware
2961da177e4SLinus Torvalds 	 */
2971da177e4SLinus Torvalds 	mega_get_max_sgl(adapter);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	adapter->host->sg_tablesize = adapter->sglen;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	/* use HP firmware and bios version encoding */
3031da177e4SLinus Torvalds 	if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
3041da177e4SLinus Torvalds 		sprintf (adapter->fw_version, "%c%d%d.%d%d",
3051da177e4SLinus Torvalds 			 adapter->product_info.fw_version[2],
3061da177e4SLinus Torvalds 			 adapter->product_info.fw_version[1] >> 8,
3071da177e4SLinus Torvalds 			 adapter->product_info.fw_version[1] & 0x0f,
3081da177e4SLinus Torvalds 			 adapter->product_info.fw_version[0] >> 8,
3091da177e4SLinus Torvalds 			 adapter->product_info.fw_version[0] & 0x0f);
3101da177e4SLinus Torvalds 		sprintf (adapter->bios_version, "%c%d%d.%d%d",
3111da177e4SLinus Torvalds 			 adapter->product_info.bios_version[2],
3121da177e4SLinus Torvalds 			 adapter->product_info.bios_version[1] >> 8,
3131da177e4SLinus Torvalds 			 adapter->product_info.bios_version[1] & 0x0f,
3141da177e4SLinus Torvalds 			 adapter->product_info.bios_version[0] >> 8,
3151da177e4SLinus Torvalds 			 adapter->product_info.bios_version[0] & 0x0f);
3161da177e4SLinus Torvalds 	} else {
3171da177e4SLinus Torvalds 		memcpy(adapter->fw_version,
3181da177e4SLinus Torvalds 				(char *)adapter->product_info.fw_version, 4);
3191da177e4SLinus Torvalds 		adapter->fw_version[4] = 0;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		memcpy(adapter->bios_version,
3221da177e4SLinus Torvalds 				(char *)adapter->product_info.bios_version, 4);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 		adapter->bios_version[4] = 0;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
3281da177e4SLinus Torvalds 		adapter->fw_version, adapter->bios_version, adapter->numldrv);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	/*
3311da177e4SLinus Torvalds 	 * Do we support extended (>10 bytes) cdbs
3321da177e4SLinus Torvalds 	 */
3331da177e4SLinus Torvalds 	adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
3341da177e4SLinus Torvalds 	if (adapter->support_ext_cdb)
3351da177e4SLinus Torvalds 		printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	return 0;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /**
3421da177e4SLinus Torvalds  * mega_runpendq()
3431da177e4SLinus Torvalds  * @adapter - pointer to our soft state
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  * Runs through the list of pending requests.
3461da177e4SLinus Torvalds  */
3471da177e4SLinus Torvalds static inline void
3481da177e4SLinus Torvalds mega_runpendq(adapter_t *adapter)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	if(!list_empty(&adapter->pending_list))
3511da177e4SLinus Torvalds 		__mega_runpendq(adapter);
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /*
3551da177e4SLinus Torvalds  * megaraid_queue()
3561da177e4SLinus Torvalds  * @scmd - Issue this scsi command
3571da177e4SLinus Torvalds  * @done - the callback hook into the scsi mid-layer
3581da177e4SLinus Torvalds  *
3591da177e4SLinus Torvalds  * The command queuing entry point for the mid-layer.
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds static int
3621da177e4SLinus Torvalds megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	adapter_t	*adapter;
3651da177e4SLinus Torvalds 	scb_t	*scb;
3661da177e4SLinus Torvalds 	int	busy=0;
367cb0258a2SChristoph Hellwig 	unsigned long flags;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	adapter = (adapter_t *)scmd->device->host->hostdata;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	scmd->scsi_done = done;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/*
3751da177e4SLinus Torvalds 	 * Allocate and build a SCB request
3761da177e4SLinus Torvalds 	 * busy flag will be set if mega_build_cmd() command could not
3771da177e4SLinus Torvalds 	 * allocate scb. We will return non-zero status in that case.
3781da177e4SLinus Torvalds 	 * NOTE: scb can be null even though certain commands completed
3791da177e4SLinus Torvalds 	 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
3801da177e4SLinus Torvalds 	 * return 0 in that case.
3811da177e4SLinus Torvalds 	 */
3821da177e4SLinus Torvalds 
383cb0258a2SChristoph Hellwig 	spin_lock_irqsave(&adapter->lock, flags);
3841da177e4SLinus Torvalds 	scb = mega_build_cmd(adapter, scmd, &busy);
385238f9b06SChristoph Hellwig 	if (!scb)
386238f9b06SChristoph Hellwig 		goto out;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	scb->state |= SCB_PENDQ;
3891da177e4SLinus Torvalds 	list_add_tail(&scb->list, &adapter->pending_list);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	/*
3921da177e4SLinus Torvalds 	 * Check if the HBA is in quiescent state, e.g., during a
3931da177e4SLinus Torvalds 	 * delete logical drive opertion. If it is, don't run
3941da177e4SLinus Torvalds 	 * the pending_list.
3951da177e4SLinus Torvalds 	 */
396238f9b06SChristoph Hellwig 	if (atomic_read(&adapter->quiescent) == 0)
3971da177e4SLinus Torvalds 		mega_runpendq(adapter);
3981da177e4SLinus Torvalds 
399238f9b06SChristoph Hellwig 	busy = 0;
400238f9b06SChristoph Hellwig  out:
401238f9b06SChristoph Hellwig 	spin_unlock_irqrestore(&adapter->lock, flags);
4021da177e4SLinus Torvalds 	return busy;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /**
4061da177e4SLinus Torvalds  * mega_allocate_scb()
4071da177e4SLinus Torvalds  * @adapter - pointer to our soft state
4081da177e4SLinus Torvalds  * @cmd - scsi command from the mid-layer
4091da177e4SLinus Torvalds  *
4101da177e4SLinus Torvalds  * Allocate a SCB structure. This is the central structure for controller
4111da177e4SLinus Torvalds  * commands.
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds static inline scb_t *
4141da177e4SLinus Torvalds mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct list_head *head = &adapter->free_list;
4171da177e4SLinus Torvalds 	scb_t	*scb;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Unlink command from Free List */
4201da177e4SLinus Torvalds 	if( !list_empty(head) ) {
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		scb = list_entry(head->next, scb_t, list);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		list_del_init(head->next);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		scb->state = SCB_ACTIVE;
4271da177e4SLinus Torvalds 		scb->cmd = cmd;
4281da177e4SLinus Torvalds 		scb->dma_type = MEGA_DMA_TYPE_NONE;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 		return scb;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	return NULL;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /**
4371da177e4SLinus Torvalds  * mega_get_ldrv_num()
4381da177e4SLinus Torvalds  * @adapter - pointer to our soft state
4391da177e4SLinus Torvalds  * @cmd - scsi mid layer command
4401da177e4SLinus Torvalds  * @channel - channel on the controller
4411da177e4SLinus Torvalds  *
4421da177e4SLinus Torvalds  * Calculate the logical drive number based on the information in scsi command
4431da177e4SLinus Torvalds  * and the channel number.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds static inline int
4461da177e4SLinus Torvalds mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 	int		tgt;
4491da177e4SLinus Torvalds 	int		ldrv_num;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	tgt = cmd->device->id;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	if ( tgt > adapter->this_id )
4541da177e4SLinus Torvalds 		tgt--;	/* we do not get inquires for initiator id */
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	ldrv_num = (channel * 15) + tgt;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/*
4601da177e4SLinus Torvalds 	 * If we have a logical drive with boot enabled, project it first
4611da177e4SLinus Torvalds 	 */
4621da177e4SLinus Torvalds 	if( adapter->boot_ldrv_enabled ) {
4631da177e4SLinus Torvalds 		if( ldrv_num == 0 ) {
4641da177e4SLinus Torvalds 			ldrv_num = adapter->boot_ldrv;
4651da177e4SLinus Torvalds 		}
4661da177e4SLinus Torvalds 		else {
4671da177e4SLinus Torvalds 			if( ldrv_num <= adapter->boot_ldrv ) {
4681da177e4SLinus Torvalds 				ldrv_num--;
4691da177e4SLinus Torvalds 			}
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/*
4741da177e4SLinus Torvalds 	 * If "delete logical drive" feature is enabled on this controller.
4751da177e4SLinus Torvalds 	 * Do only if at least one delete logical drive operation was done.
4761da177e4SLinus Torvalds 	 *
4771da177e4SLinus Torvalds 	 * Also, after logical drive deletion, instead of logical drive number,
4781da177e4SLinus Torvalds 	 * the value returned should be 0x80+logical drive id.
4791da177e4SLinus Torvalds 	 *
4801da177e4SLinus Torvalds 	 * These is valid only for IO commands.
4811da177e4SLinus Torvalds 	 */
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (adapter->support_random_del && adapter->read_ldidmap )
4841da177e4SLinus Torvalds 		switch (cmd->cmnd[0]) {
4851da177e4SLinus Torvalds 		case READ_6:	/* fall through */
4861da177e4SLinus Torvalds 		case WRITE_6:	/* fall through */
4871da177e4SLinus Torvalds 		case READ_10:	/* fall through */
4881da177e4SLinus Torvalds 		case WRITE_10:
4891da177e4SLinus Torvalds 			ldrv_num += 0x80;
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	return ldrv_num;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /**
4961da177e4SLinus Torvalds  * mega_build_cmd()
4971da177e4SLinus Torvalds  * @adapter - pointer to our soft state
4981da177e4SLinus Torvalds  * @cmd - Prepare using this scsi command
4991da177e4SLinus Torvalds  * @busy - busy flag if no resources
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  * Prepares a command and scatter gather list for the controller. This routine
5021da177e4SLinus Torvalds  * also finds out if the commands is intended for a logical drive or a
5031da177e4SLinus Torvalds  * physical device and prepares the controller command accordingly.
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  * We also re-order the logical drives and physical devices based on their
5061da177e4SLinus Torvalds  * boot settings.
5071da177e4SLinus Torvalds  */
5081da177e4SLinus Torvalds static scb_t *
5091da177e4SLinus Torvalds mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	mega_ext_passthru	*epthru;
5121da177e4SLinus Torvalds 	mega_passthru	*pthru;
5131da177e4SLinus Torvalds 	scb_t	*scb;
5141da177e4SLinus Torvalds 	mbox_t	*mbox;
5151da177e4SLinus Torvalds 	long	seg;
5161da177e4SLinus Torvalds 	char	islogical;
5171da177e4SLinus Torvalds 	int	max_ldrv_num;
5181da177e4SLinus Torvalds 	int	channel = 0;
5191da177e4SLinus Torvalds 	int	target = 0;
5201da177e4SLinus Torvalds 	int	ldrv_num = 0;   /* logical drive number */
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	/*
5241da177e4SLinus Torvalds 	 * filter the internal and ioctl commands
5251da177e4SLinus Torvalds 	 */
5261da177e4SLinus Torvalds 	if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
5275d5ff44fSChristoph Hellwig 		return cmd->request_buffer;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	/*
5321da177e4SLinus Torvalds 	 * We know what channels our logical drives are on - mega_find_card()
5331da177e4SLinus Torvalds 	 */
5341da177e4SLinus Torvalds 	islogical = adapter->logdrv_chan[cmd->device->channel];
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	/*
5371da177e4SLinus Torvalds 	 * The theory: If physical drive is chosen for boot, all the physical
5381da177e4SLinus Torvalds 	 * devices are exported before the logical drives, otherwise physical
5391da177e4SLinus Torvalds 	 * devices are pushed after logical drives, in which case - Kernel sees
5401da177e4SLinus Torvalds 	 * the physical devices on virtual channel which is obviously converted
5411da177e4SLinus Torvalds 	 * to actual channel on the HBA.
5421da177e4SLinus Torvalds 	 */
5431da177e4SLinus Torvalds 	if( adapter->boot_pdrv_enabled ) {
5441da177e4SLinus Torvalds 		if( islogical ) {
5451da177e4SLinus Torvalds 			/* logical channel */
5461da177e4SLinus Torvalds 			channel = cmd->device->channel -
5471da177e4SLinus Torvalds 				adapter->product_info.nchannels;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		else {
5501da177e4SLinus Torvalds 			/* this is physical channel */
5511da177e4SLinus Torvalds 			channel = cmd->device->channel;
5521da177e4SLinus Torvalds 			target = cmd->device->id;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 			/*
5551da177e4SLinus Torvalds 			 * boot from a physical disk, that disk needs to be
5561da177e4SLinus Torvalds 			 * exposed first IF both the channels are SCSI, then
5571da177e4SLinus Torvalds 			 * booting from the second channel is not allowed.
5581da177e4SLinus Torvalds 			 */
5591da177e4SLinus Torvalds 			if( target == 0 ) {
5601da177e4SLinus Torvalds 				target = adapter->boot_pdrv_tgt;
5611da177e4SLinus Torvalds 			}
5621da177e4SLinus Torvalds 			else if( target == adapter->boot_pdrv_tgt ) {
5631da177e4SLinus Torvalds 				target = 0;
5641da177e4SLinus Torvalds 			}
5651da177e4SLinus Torvalds 		}
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 	else {
5681da177e4SLinus Torvalds 		if( islogical ) {
5691da177e4SLinus Torvalds 			/* this is the logical channel */
5701da177e4SLinus Torvalds 			channel = cmd->device->channel;
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 		else {
5731da177e4SLinus Torvalds 			/* physical channel */
5741da177e4SLinus Torvalds 			channel = cmd->device->channel - NVIRT_CHAN;
5751da177e4SLinus Torvalds 			target = cmd->device->id;
5761da177e4SLinus Torvalds 		}
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	if(islogical) {
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 		/* have just LUN 0 for each target on virtual channels */
5831da177e4SLinus Torvalds 		if (cmd->device->lun) {
5841da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
5851da177e4SLinus Torvalds 			cmd->scsi_done(cmd);
5861da177e4SLinus Torvalds 			return NULL;
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 		ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 		max_ldrv_num = (adapter->flag & BOARD_40LD) ?
5931da177e4SLinus Torvalds 			MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 		/*
5961da177e4SLinus Torvalds 		 * max_ldrv_num increases by 0x80 if some logical drive was
5971da177e4SLinus Torvalds 		 * deleted.
5981da177e4SLinus Torvalds 		 */
5991da177e4SLinus Torvalds 		if(adapter->read_ldidmap)
6001da177e4SLinus Torvalds 			max_ldrv_num += 0x80;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 		if(ldrv_num > max_ldrv_num ) {
6031da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
6041da177e4SLinus Torvalds 			cmd->scsi_done(cmd);
6051da177e4SLinus Torvalds 			return NULL;
6061da177e4SLinus Torvalds 		}
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 	else {
6101da177e4SLinus Torvalds 		if( cmd->device->lun > 7) {
6111da177e4SLinus Torvalds 			/*
6121da177e4SLinus Torvalds 			 * Do not support lun >7 for physically accessed
6131da177e4SLinus Torvalds 			 * devices
6141da177e4SLinus Torvalds 			 */
6151da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
6161da177e4SLinus Torvalds 			cmd->scsi_done(cmd);
6171da177e4SLinus Torvalds 			return NULL;
6181da177e4SLinus Torvalds 		}
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	/*
6221da177e4SLinus Torvalds 	 *
6231da177e4SLinus Torvalds 	 * Logical drive commands
6241da177e4SLinus Torvalds 	 *
6251da177e4SLinus Torvalds 	 */
6261da177e4SLinus Torvalds 	if(islogical) {
6271da177e4SLinus Torvalds 		switch (cmd->cmnd[0]) {
6281da177e4SLinus Torvalds 		case TEST_UNIT_READY:
6291da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
6301da177e4SLinus Torvalds 			/*
6311da177e4SLinus Torvalds 			 * Do we support clustering and is the support enabled
6321da177e4SLinus Torvalds 			 * If no, return success always
6331da177e4SLinus Torvalds 			 */
6341da177e4SLinus Torvalds 			if( !adapter->has_cluster ) {
6351da177e4SLinus Torvalds 				cmd->result = (DID_OK << 16);
6361da177e4SLinus Torvalds 				cmd->scsi_done(cmd);
6371da177e4SLinus Torvalds 				return NULL;
6381da177e4SLinus Torvalds 			}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
6411da177e4SLinus Torvalds 				*busy = 1;
6421da177e4SLinus Torvalds 				return NULL;
6431da177e4SLinus Torvalds 			}
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
6461da177e4SLinus Torvalds 			scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
6471da177e4SLinus Torvalds 			scb->raw_mbox[3] = ldrv_num;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 			scb->dma_direction = PCI_DMA_NONE;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 			return scb;
6521da177e4SLinus Torvalds #else
6531da177e4SLinus Torvalds 			cmd->result = (DID_OK << 16);
6541da177e4SLinus Torvalds 			cmd->scsi_done(cmd);
6551da177e4SLinus Torvalds 			return NULL;
6561da177e4SLinus Torvalds #endif
6571da177e4SLinus Torvalds 
65851c928c3SJames Bottomley 		case MODE_SENSE: {
65951c928c3SJames Bottomley 			char *buf;
66051c928c3SJames Bottomley 
66151c928c3SJames Bottomley 			if (cmd->use_sg) {
66251c928c3SJames Bottomley 				struct scatterlist *sg;
66351c928c3SJames Bottomley 
66451c928c3SJames Bottomley 				sg = (struct scatterlist *)cmd->request_buffer;
66551c928c3SJames Bottomley 				buf = kmap_atomic(sg->page, KM_IRQ0) +
66651c928c3SJames Bottomley 					sg->offset;
66751c928c3SJames Bottomley 			} else
66851c928c3SJames Bottomley 				buf = cmd->request_buffer;
669f0353301SMark Lord 			memset(buf, 0, cmd->cmnd[4]);
67051c928c3SJames Bottomley 			if (cmd->use_sg) {
67151c928c3SJames Bottomley 				struct scatterlist *sg;
67251c928c3SJames Bottomley 
67351c928c3SJames Bottomley 				sg = (struct scatterlist *)cmd->request_buffer;
67451c928c3SJames Bottomley 				kunmap_atomic(buf - sg->offset, KM_IRQ0);
67551c928c3SJames Bottomley 			}
6761da177e4SLinus Torvalds 			cmd->result = (DID_OK << 16);
6771da177e4SLinus Torvalds 			cmd->scsi_done(cmd);
6781da177e4SLinus Torvalds 			return NULL;
67951c928c3SJames Bottomley 		}
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 		case READ_CAPACITY:
6821da177e4SLinus Torvalds 		case INQUIRY:
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 			if(!(adapter->flag & (1L << cmd->device->channel))) {
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 				printk(KERN_NOTICE
6871da177e4SLinus Torvalds 					"scsi%d: scanning scsi channel %d ",
6881da177e4SLinus Torvalds 						adapter->host->host_no,
6891da177e4SLinus Torvalds 						cmd->device->channel);
6901da177e4SLinus Torvalds 				printk("for logical drives.\n");
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 				adapter->flag |= (1L << cmd->device->channel);
6931da177e4SLinus Torvalds 			}
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 			/* Allocate a SCB and initialize passthru */
6961da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
6971da177e4SLinus Torvalds 				*busy = 1;
6981da177e4SLinus Torvalds 				return NULL;
6991da177e4SLinus Torvalds 			}
7001da177e4SLinus Torvalds 			pthru = scb->pthru;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
7031da177e4SLinus Torvalds 			memset(mbox, 0, sizeof(scb->raw_mbox));
7041da177e4SLinus Torvalds 			memset(pthru, 0, sizeof(mega_passthru));
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 			pthru->timeout = 0;
7071da177e4SLinus Torvalds 			pthru->ars = 1;
7081da177e4SLinus Torvalds 			pthru->reqsenselen = 14;
7091da177e4SLinus Torvalds 			pthru->islogical = 1;
7101da177e4SLinus Torvalds 			pthru->logdrv = ldrv_num;
7111da177e4SLinus Torvalds 			pthru->cdblen = cmd->cmd_len;
7121da177e4SLinus Torvalds 			memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 			if( adapter->has_64bit_addr ) {
7151da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
7161da177e4SLinus Torvalds 			}
7171da177e4SLinus Torvalds 			else {
7181da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
7191da177e4SLinus Torvalds 			}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 			scb->dma_direction = PCI_DMA_FROMDEVICE;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 			pthru->numsgelements = mega_build_sglist(adapter, scb,
7241da177e4SLinus Torvalds 				&pthru->dataxferaddr, &pthru->dataxferlen);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 			mbox->m_out.xferaddr = scb->pthru_dma_addr;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 			return scb;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		case READ_6:
7311da177e4SLinus Torvalds 		case WRITE_6:
7321da177e4SLinus Torvalds 		case READ_10:
7331da177e4SLinus Torvalds 		case WRITE_10:
7341da177e4SLinus Torvalds 		case READ_12:
7351da177e4SLinus Torvalds 		case WRITE_12:
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 			/* Allocate a SCB and initialize mailbox */
7381da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
7391da177e4SLinus Torvalds 				*busy = 1;
7401da177e4SLinus Torvalds 				return NULL;
7411da177e4SLinus Torvalds 			}
7421da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 			memset(mbox, 0, sizeof(scb->raw_mbox));
7451da177e4SLinus Torvalds 			mbox->m_out.logdrv = ldrv_num;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 			/*
7481da177e4SLinus Torvalds 			 * A little hack: 2nd bit is zero for all scsi read
7491da177e4SLinus Torvalds 			 * commands and is set for all scsi write commands
7501da177e4SLinus Torvalds 			 */
7511da177e4SLinus Torvalds 			if( adapter->has_64bit_addr ) {
7521da177e4SLinus Torvalds 				mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
7531da177e4SLinus Torvalds 					MEGA_MBOXCMD_LWRITE64:
7541da177e4SLinus Torvalds 					MEGA_MBOXCMD_LREAD64 ;
7551da177e4SLinus Torvalds 			}
7561da177e4SLinus Torvalds 			else {
7571da177e4SLinus Torvalds 				mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
7581da177e4SLinus Torvalds 					MEGA_MBOXCMD_LWRITE:
7591da177e4SLinus Torvalds 					MEGA_MBOXCMD_LREAD ;
7601da177e4SLinus Torvalds 			}
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 			/*
7631da177e4SLinus Torvalds 			 * 6-byte READ(0x08) or WRITE(0x0A) cdb
7641da177e4SLinus Torvalds 			 */
7651da177e4SLinus Torvalds 			if( cmd->cmd_len == 6 ) {
7661da177e4SLinus Torvalds 				mbox->m_out.numsectors = (u32) cmd->cmnd[4];
7671da177e4SLinus Torvalds 				mbox->m_out.lba =
7681da177e4SLinus Torvalds 					((u32)cmd->cmnd[1] << 16) |
7691da177e4SLinus Torvalds 					((u32)cmd->cmnd[2] << 8) |
7701da177e4SLinus Torvalds 					(u32)cmd->cmnd[3];
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 				mbox->m_out.lba &= 0x1FFFFF;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds #if MEGA_HAVE_STATS
7751da177e4SLinus Torvalds 				/*
7761da177e4SLinus Torvalds 				 * Take modulo 0x80, since the logical drive
7771da177e4SLinus Torvalds 				 * number increases by 0x80 when a logical
7781da177e4SLinus Torvalds 				 * drive was deleted
7791da177e4SLinus Torvalds 				 */
7801da177e4SLinus Torvalds 				if (*cmd->cmnd == READ_6) {
7811da177e4SLinus Torvalds 					adapter->nreads[ldrv_num%0x80]++;
7821da177e4SLinus Torvalds 					adapter->nreadblocks[ldrv_num%0x80] +=
7831da177e4SLinus Torvalds 						mbox->m_out.numsectors;
7841da177e4SLinus Torvalds 				} else {
7851da177e4SLinus Torvalds 					adapter->nwrites[ldrv_num%0x80]++;
7861da177e4SLinus Torvalds 					adapter->nwriteblocks[ldrv_num%0x80] +=
7871da177e4SLinus Torvalds 						mbox->m_out.numsectors;
7881da177e4SLinus Torvalds 				}
7891da177e4SLinus Torvalds #endif
7901da177e4SLinus Torvalds 			}
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 			/*
7931da177e4SLinus Torvalds 			 * 10-byte READ(0x28) or WRITE(0x2A) cdb
7941da177e4SLinus Torvalds 			 */
7951da177e4SLinus Torvalds 			if( cmd->cmd_len == 10 ) {
7961da177e4SLinus Torvalds 				mbox->m_out.numsectors =
7971da177e4SLinus Torvalds 					(u32)cmd->cmnd[8] |
7981da177e4SLinus Torvalds 					((u32)cmd->cmnd[7] << 8);
7991da177e4SLinus Torvalds 				mbox->m_out.lba =
8001da177e4SLinus Torvalds 					((u32)cmd->cmnd[2] << 24) |
8011da177e4SLinus Torvalds 					((u32)cmd->cmnd[3] << 16) |
8021da177e4SLinus Torvalds 					((u32)cmd->cmnd[4] << 8) |
8031da177e4SLinus Torvalds 					(u32)cmd->cmnd[5];
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds #if MEGA_HAVE_STATS
8061da177e4SLinus Torvalds 				if (*cmd->cmnd == READ_10) {
8071da177e4SLinus Torvalds 					adapter->nreads[ldrv_num%0x80]++;
8081da177e4SLinus Torvalds 					adapter->nreadblocks[ldrv_num%0x80] +=
8091da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8101da177e4SLinus Torvalds 				} else {
8111da177e4SLinus Torvalds 					adapter->nwrites[ldrv_num%0x80]++;
8121da177e4SLinus Torvalds 					adapter->nwriteblocks[ldrv_num%0x80] +=
8131da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8141da177e4SLinus Torvalds 				}
8151da177e4SLinus Torvalds #endif
8161da177e4SLinus Torvalds 			}
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 			/*
8191da177e4SLinus Torvalds 			 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
8201da177e4SLinus Torvalds 			 */
8211da177e4SLinus Torvalds 			if( cmd->cmd_len == 12 ) {
8221da177e4SLinus Torvalds 				mbox->m_out.lba =
8231da177e4SLinus Torvalds 					((u32)cmd->cmnd[2] << 24) |
8241da177e4SLinus Torvalds 					((u32)cmd->cmnd[3] << 16) |
8251da177e4SLinus Torvalds 					((u32)cmd->cmnd[4] << 8) |
8261da177e4SLinus Torvalds 					(u32)cmd->cmnd[5];
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 				mbox->m_out.numsectors =
8291da177e4SLinus Torvalds 					((u32)cmd->cmnd[6] << 24) |
8301da177e4SLinus Torvalds 					((u32)cmd->cmnd[7] << 16) |
8311da177e4SLinus Torvalds 					((u32)cmd->cmnd[8] << 8) |
8321da177e4SLinus Torvalds 					(u32)cmd->cmnd[9];
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds #if MEGA_HAVE_STATS
8351da177e4SLinus Torvalds 				if (*cmd->cmnd == READ_12) {
8361da177e4SLinus Torvalds 					adapter->nreads[ldrv_num%0x80]++;
8371da177e4SLinus Torvalds 					adapter->nreadblocks[ldrv_num%0x80] +=
8381da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8391da177e4SLinus Torvalds 				} else {
8401da177e4SLinus Torvalds 					adapter->nwrites[ldrv_num%0x80]++;
8411da177e4SLinus Torvalds 					adapter->nwriteblocks[ldrv_num%0x80] +=
8421da177e4SLinus Torvalds 						mbox->m_out.numsectors;
8431da177e4SLinus Torvalds 				}
8441da177e4SLinus Torvalds #endif
8451da177e4SLinus Torvalds 			}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 			/*
8481da177e4SLinus Torvalds 			 * If it is a read command
8491da177e4SLinus Torvalds 			 */
8501da177e4SLinus Torvalds 			if( (*cmd->cmnd & 0x0F) == 0x08 ) {
8511da177e4SLinus Torvalds 				scb->dma_direction = PCI_DMA_FROMDEVICE;
8521da177e4SLinus Torvalds 			}
8531da177e4SLinus Torvalds 			else {
8541da177e4SLinus Torvalds 				scb->dma_direction = PCI_DMA_TODEVICE;
8551da177e4SLinus Torvalds 			}
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 			/* Calculate Scatter-Gather info */
8581da177e4SLinus Torvalds 			mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
8591da177e4SLinus Torvalds 					(u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 			return scb;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
8641da177e4SLinus Torvalds 		case RESERVE:	/* Fall through */
8651da177e4SLinus Torvalds 		case RELEASE:
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 			/*
8681da177e4SLinus Torvalds 			 * Do we support clustering and is the support enabled
8691da177e4SLinus Torvalds 			 */
8701da177e4SLinus Torvalds 			if( ! adapter->has_cluster ) {
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 				cmd->result = (DID_BAD_TARGET << 16);
8731da177e4SLinus Torvalds 				cmd->scsi_done(cmd);
8741da177e4SLinus Torvalds 				return NULL;
8751da177e4SLinus Torvalds 			}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 			/* Allocate a SCB and initialize mailbox */
8781da177e4SLinus Torvalds 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
8791da177e4SLinus Torvalds 				*busy = 1;
8801da177e4SLinus Torvalds 				return NULL;
8811da177e4SLinus Torvalds 			}
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
8841da177e4SLinus Torvalds 			scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
8851da177e4SLinus Torvalds 				MEGA_RESERVE_LD : MEGA_RELEASE_LD;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 			scb->raw_mbox[3] = ldrv_num;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 			scb->dma_direction = PCI_DMA_NONE;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 			return scb;
8921da177e4SLinus Torvalds #endif
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		default:
8951da177e4SLinus Torvalds 			cmd->result = (DID_BAD_TARGET << 16);
8961da177e4SLinus Torvalds 			cmd->scsi_done(cmd);
8971da177e4SLinus Torvalds 			return NULL;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	/*
9021da177e4SLinus Torvalds 	 * Passthru drive commands
9031da177e4SLinus Torvalds 	 */
9041da177e4SLinus Torvalds 	else {
9051da177e4SLinus Torvalds 		/* Allocate a SCB and initialize passthru */
9061da177e4SLinus Torvalds 		if(!(scb = mega_allocate_scb(adapter, cmd))) {
9071da177e4SLinus Torvalds 			*busy = 1;
9081da177e4SLinus Torvalds 			return NULL;
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 		mbox = (mbox_t *)scb->raw_mbox;
9121da177e4SLinus Torvalds 		memset(mbox, 0, sizeof(scb->raw_mbox));
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		if( adapter->support_ext_cdb ) {
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 			epthru = mega_prepare_extpassthru(adapter, scb, cmd,
9171da177e4SLinus Torvalds 					channel, target);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 			mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 			mbox->m_out.xferaddr = scb->epthru_dma_addr;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 		}
9241da177e4SLinus Torvalds 		else {
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 			pthru = mega_prepare_passthru(adapter, scb, cmd,
9271da177e4SLinus Torvalds 					channel, target);
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 			/* Initialize mailbox */
9301da177e4SLinus Torvalds 			if( adapter->has_64bit_addr ) {
9311da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
9321da177e4SLinus Torvalds 			}
9331da177e4SLinus Torvalds 			else {
9341da177e4SLinus Torvalds 				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
9351da177e4SLinus Torvalds 			}
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 			mbox->m_out.xferaddr = scb->pthru_dma_addr;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 		return scb;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 	return NULL;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds /**
9471da177e4SLinus Torvalds  * mega_prepare_passthru()
9481da177e4SLinus Torvalds  * @adapter - pointer to our soft state
9491da177e4SLinus Torvalds  * @scb - our scsi control block
9501da177e4SLinus Torvalds  * @cmd - scsi command from the mid-layer
9511da177e4SLinus Torvalds  * @channel - actual channel on the controller
9521da177e4SLinus Torvalds  * @target - actual id on the controller.
9531da177e4SLinus Torvalds  *
9541da177e4SLinus Torvalds  * prepare a command for the scsi physical devices.
9551da177e4SLinus Torvalds  */
9561da177e4SLinus Torvalds static mega_passthru *
9571da177e4SLinus Torvalds mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
9581da177e4SLinus Torvalds 		int channel, int target)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	mega_passthru *pthru;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	pthru = scb->pthru;
9631da177e4SLinus Torvalds 	memset(pthru, 0, sizeof (mega_passthru));
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	/* 0=6sec/1=60sec/2=10min/3=3hrs */
9661da177e4SLinus Torvalds 	pthru->timeout = 2;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	pthru->ars = 1;
9691da177e4SLinus Torvalds 	pthru->reqsenselen = 14;
9701da177e4SLinus Torvalds 	pthru->islogical = 0;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	pthru->target = (adapter->flag & BOARD_40LD) ?
9751da177e4SLinus Torvalds 		(channel << 4) | target : target;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	pthru->cdblen = cmd->cmd_len;
9781da177e4SLinus Torvalds 	pthru->logdrv = cmd->device->lun;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/* Not sure about the direction */
9831da177e4SLinus Torvalds 	scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
9861da177e4SLinus Torvalds 	switch (cmd->cmnd[0]) {
9871da177e4SLinus Torvalds 	case INQUIRY:
9881da177e4SLinus Torvalds 	case READ_CAPACITY:
9891da177e4SLinus Torvalds 		if(!(adapter->flag & (1L << cmd->device->channel))) {
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 			printk(KERN_NOTICE
9921da177e4SLinus Torvalds 				"scsi%d: scanning scsi channel %d [P%d] ",
9931da177e4SLinus Torvalds 					adapter->host->host_no,
9941da177e4SLinus Torvalds 					cmd->device->channel, channel);
9951da177e4SLinus Torvalds 			printk("for physical devices.\n");
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 			adapter->flag |= (1L << cmd->device->channel);
9981da177e4SLinus Torvalds 		}
9991da177e4SLinus Torvalds 		/* Fall through */
10001da177e4SLinus Torvalds 	default:
10011da177e4SLinus Torvalds 		pthru->numsgelements = mega_build_sglist(adapter, scb,
10021da177e4SLinus Torvalds 				&pthru->dataxferaddr, &pthru->dataxferlen);
10031da177e4SLinus Torvalds 		break;
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds 	return pthru;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds /**
10101da177e4SLinus Torvalds  * mega_prepare_extpassthru()
10111da177e4SLinus Torvalds  * @adapter - pointer to our soft state
10121da177e4SLinus Torvalds  * @scb - our scsi control block
10131da177e4SLinus Torvalds  * @cmd - scsi command from the mid-layer
10141da177e4SLinus Torvalds  * @channel - actual channel on the controller
10151da177e4SLinus Torvalds  * @target - actual id on the controller.
10161da177e4SLinus Torvalds  *
10171da177e4SLinus Torvalds  * prepare a command for the scsi physical devices. This rountine prepares
10181da177e4SLinus Torvalds  * commands for devices which can take extended CDBs (>10 bytes)
10191da177e4SLinus Torvalds  */
10201da177e4SLinus Torvalds static mega_ext_passthru *
10211da177e4SLinus Torvalds mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
10221da177e4SLinus Torvalds 		int channel, int target)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	mega_ext_passthru	*epthru;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	epthru = scb->epthru;
10271da177e4SLinus Torvalds 	memset(epthru, 0, sizeof(mega_ext_passthru));
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	/* 0=6sec/1=60sec/2=10min/3=3hrs */
10301da177e4SLinus Torvalds 	epthru->timeout = 2;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	epthru->ars = 1;
10331da177e4SLinus Torvalds 	epthru->reqsenselen = 14;
10341da177e4SLinus Torvalds 	epthru->islogical = 0;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
10371da177e4SLinus Torvalds 	epthru->target = (adapter->flag & BOARD_40LD) ?
10381da177e4SLinus Torvalds 		(channel << 4) | target : target;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	epthru->cdblen = cmd->cmd_len;
10411da177e4SLinus Torvalds 	epthru->logdrv = cmd->device->lun;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	/* Not sure about the direction */
10461da177e4SLinus Torvalds 	scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	switch(cmd->cmnd[0]) {
10491da177e4SLinus Torvalds 	case INQUIRY:
10501da177e4SLinus Torvalds 	case READ_CAPACITY:
10511da177e4SLinus Torvalds 		if(!(adapter->flag & (1L << cmd->device->channel))) {
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 			printk(KERN_NOTICE
10541da177e4SLinus Torvalds 				"scsi%d: scanning scsi channel %d [P%d] ",
10551da177e4SLinus Torvalds 					adapter->host->host_no,
10561da177e4SLinus Torvalds 					cmd->device->channel, channel);
10571da177e4SLinus Torvalds 			printk("for physical devices.\n");
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 			adapter->flag |= (1L << cmd->device->channel);
10601da177e4SLinus Torvalds 		}
10611da177e4SLinus Torvalds 		/* Fall through */
10621da177e4SLinus Torvalds 	default:
10631da177e4SLinus Torvalds 		epthru->numsgelements = mega_build_sglist(adapter, scb,
10641da177e4SLinus Torvalds 				&epthru->dataxferaddr, &epthru->dataxferlen);
10651da177e4SLinus Torvalds 		break;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	return epthru;
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds static void
10721da177e4SLinus Torvalds __mega_runpendq(adapter_t *adapter)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	scb_t *scb;
10751da177e4SLinus Torvalds 	struct list_head *pos, *next;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	/* Issue any pending commands to the card */
10781da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &adapter->pending_list) {
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 		scb = list_entry(pos, scb_t, list);
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 		if( !(scb->state & SCB_ISSUED) ) {
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 			if( issue_scb(adapter, scb) != 0 )
10851da177e4SLinus Torvalds 				return;
10861da177e4SLinus Torvalds 		}
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	return;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds /**
10941da177e4SLinus Torvalds  * issue_scb()
10951da177e4SLinus Torvalds  * @adapter - pointer to our soft state
10961da177e4SLinus Torvalds  * @scb - scsi control block
10971da177e4SLinus Torvalds  *
10981da177e4SLinus Torvalds  * Post a command to the card if the mailbox is available, otherwise return
10991da177e4SLinus Torvalds  * busy. We also take the scb from the pending list if the mailbox is
11001da177e4SLinus Torvalds  * available.
11011da177e4SLinus Torvalds  */
11021da177e4SLinus Torvalds static int
11031da177e4SLinus Torvalds issue_scb(adapter_t *adapter, scb_t *scb)
11041da177e4SLinus Torvalds {
11051da177e4SLinus Torvalds 	volatile mbox64_t	*mbox64 = adapter->mbox64;
11061da177e4SLinus Torvalds 	volatile mbox_t		*mbox = adapter->mbox;
11071da177e4SLinus Torvalds 	unsigned int	i = 0;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	if(unlikely(mbox->m_in.busy)) {
11101da177e4SLinus Torvalds 		do {
11111da177e4SLinus Torvalds 			udelay(1);
11121da177e4SLinus Torvalds 			i++;
11131da177e4SLinus Torvalds 		} while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 		if(mbox->m_in.busy) return -1;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	/* Copy mailbox data into host structure */
11191da177e4SLinus Torvalds 	memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
11201da177e4SLinus Torvalds 			sizeof(struct mbox_out));
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	mbox->m_out.cmdid = scb->idx;	/* Set cmdid */
11231da177e4SLinus Torvalds 	mbox->m_in.busy = 1;		/* Set busy */
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	/*
11271da177e4SLinus Torvalds 	 * Increment the pending queue counter
11281da177e4SLinus Torvalds 	 */
11291da177e4SLinus Torvalds 	atomic_inc(&adapter->pend_cmds);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	switch (mbox->m_out.cmd) {
11321da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LREAD64:
11331da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LWRITE64:
11341da177e4SLinus Torvalds 	case MEGA_MBOXCMD_PASSTHRU64:
11351da177e4SLinus Torvalds 	case MEGA_MBOXCMD_EXTPTHRU:
11361da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
11371da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
11381da177e4SLinus Torvalds 		mbox->m_out.xferaddr = 0xFFFFFFFF;
11391da177e4SLinus Torvalds 		break;
11401da177e4SLinus Torvalds 	default:
11411da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = 0;
11421da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	/*
11461da177e4SLinus Torvalds 	 * post the command
11471da177e4SLinus Torvalds 	 */
11481da177e4SLinus Torvalds 	scb->state |= SCB_ISSUED;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	if( likely(adapter->flag & BOARD_MEMMAP) ) {
11511da177e4SLinus Torvalds 		mbox->m_in.poll = 0;
11521da177e4SLinus Torvalds 		mbox->m_in.ack = 0;
11531da177e4SLinus Torvalds 		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 	else {
11561da177e4SLinus Torvalds 		irq_enable(adapter);
11571da177e4SLinus Torvalds 		issue_command(adapter);
11581da177e4SLinus Torvalds 	}
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	return 0;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds /*
11641da177e4SLinus Torvalds  * Wait until the controller's mailbox is available
11651da177e4SLinus Torvalds  */
11661da177e4SLinus Torvalds static inline int
11671da177e4SLinus Torvalds mega_busywait_mbox (adapter_t *adapter)
11681da177e4SLinus Torvalds {
11691da177e4SLinus Torvalds 	if (adapter->mbox->m_in.busy)
11701da177e4SLinus Torvalds 		return __mega_busywait_mbox(adapter);
11711da177e4SLinus Torvalds 	return 0;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds /**
11751da177e4SLinus Torvalds  * issue_scb_block()
11761da177e4SLinus Torvalds  * @adapter - pointer to our soft state
11771da177e4SLinus Torvalds  * @raw_mbox - the mailbox
11781da177e4SLinus Torvalds  *
11791da177e4SLinus Torvalds  * Issue a scb in synchronous and non-interrupt mode
11801da177e4SLinus Torvalds  */
11811da177e4SLinus Torvalds static int
11821da177e4SLinus Torvalds issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	volatile mbox64_t *mbox64 = adapter->mbox64;
11851da177e4SLinus Torvalds 	volatile mbox_t *mbox = adapter->mbox;
11861da177e4SLinus Torvalds 	u8	byte;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	/* Wait until mailbox is free */
11891da177e4SLinus Torvalds 	if(mega_busywait_mbox (adapter))
11901da177e4SLinus Torvalds 		goto bug_blocked_mailbox;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	/* Copy mailbox data into host structure */
11931da177e4SLinus Torvalds 	memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
11941da177e4SLinus Torvalds 	mbox->m_out.cmdid = 0xFE;
11951da177e4SLinus Torvalds 	mbox->m_in.busy = 1;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	switch (raw_mbox[0]) {
11981da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LREAD64:
11991da177e4SLinus Torvalds 	case MEGA_MBOXCMD_LWRITE64:
12001da177e4SLinus Torvalds 	case MEGA_MBOXCMD_PASSTHRU64:
12011da177e4SLinus Torvalds 	case MEGA_MBOXCMD_EXTPTHRU:
12021da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
12031da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
12041da177e4SLinus Torvalds 		mbox->m_out.xferaddr = 0xFFFFFFFF;
12051da177e4SLinus Torvalds 		break;
12061da177e4SLinus Torvalds 	default:
12071da177e4SLinus Torvalds 		mbox64->xfer_segment_lo = 0;
12081da177e4SLinus Torvalds 		mbox64->xfer_segment_hi = 0;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	if( likely(adapter->flag & BOARD_MEMMAP) ) {
12121da177e4SLinus Torvalds 		mbox->m_in.poll = 0;
12131da177e4SLinus Torvalds 		mbox->m_in.ack = 0;
12141da177e4SLinus Torvalds 		mbox->m_in.numstatus = 0xFF;
12151da177e4SLinus Torvalds 		mbox->m_in.status = 0xFF;
12161da177e4SLinus Torvalds 		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		while((volatile u8)mbox->m_in.numstatus == 0xFF)
12191da177e4SLinus Torvalds 			cpu_relax();
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 		mbox->m_in.numstatus = 0xFF;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 		while( (volatile u8)mbox->m_in.poll != 0x77 )
12241da177e4SLinus Torvalds 			cpu_relax();
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		mbox->m_in.poll = 0;
12271da177e4SLinus Torvalds 		mbox->m_in.ack = 0x77;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 		WRINDOOR(adapter, adapter->mbox_dma | 0x2);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		while(RDINDOOR(adapter) & 0x2)
12321da177e4SLinus Torvalds 			cpu_relax();
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds 	else {
12351da177e4SLinus Torvalds 		irq_disable(adapter);
12361da177e4SLinus Torvalds 		issue_command(adapter);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		while (!((byte = irq_state(adapter)) & INTR_VALID))
12391da177e4SLinus Torvalds 			cpu_relax();
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 		set_irq_state(adapter, byte);
12421da177e4SLinus Torvalds 		irq_enable(adapter);
12431da177e4SLinus Torvalds 		irq_ack(adapter);
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	return mbox->m_in.status;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds bug_blocked_mailbox:
12491da177e4SLinus Torvalds 	printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
12501da177e4SLinus Torvalds 	udelay (1000);
12511da177e4SLinus Torvalds 	return -1;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds /**
12561da177e4SLinus Torvalds  * megaraid_isr_iomapped()
12571da177e4SLinus Torvalds  * @irq - irq
12581da177e4SLinus Torvalds  * @devp - pointer to our soft state
12591da177e4SLinus Torvalds  *
12601da177e4SLinus Torvalds  * Interrupt service routine for io-mapped controllers.
12611da177e4SLinus Torvalds  * Find out if our device is interrupting. If yes, acknowledge the interrupt
12621da177e4SLinus Torvalds  * and service the completed commands.
12631da177e4SLinus Torvalds  */
12641da177e4SLinus Torvalds static irqreturn_t
12657d12e780SDavid Howells megaraid_isr_iomapped(int irq, void *devp)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	adapter_t	*adapter = devp;
12681da177e4SLinus Torvalds 	unsigned long	flags;
12691da177e4SLinus Torvalds 	u8	status;
12701da177e4SLinus Torvalds 	u8	nstatus;
12711da177e4SLinus Torvalds 	u8	completed[MAX_FIRMWARE_STATUS];
12721da177e4SLinus Torvalds 	u8	byte;
12731da177e4SLinus Torvalds 	int	handled = 0;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	/*
12771da177e4SLinus Torvalds 	 * loop till F/W has more commands for us to complete.
12781da177e4SLinus Torvalds 	 */
12791da177e4SLinus Torvalds 	spin_lock_irqsave(&adapter->lock, flags);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	do {
12821da177e4SLinus Torvalds 		/* Check if a valid interrupt is pending */
12831da177e4SLinus Torvalds 		byte = irq_state(adapter);
12841da177e4SLinus Torvalds 		if( (byte & VALID_INTR_BYTE) == 0 ) {
12851da177e4SLinus Torvalds 			/*
12861da177e4SLinus Torvalds 			 * No more pending commands
12871da177e4SLinus Torvalds 			 */
12881da177e4SLinus Torvalds 			goto out_unlock;
12891da177e4SLinus Torvalds 		}
12901da177e4SLinus Torvalds 		set_irq_state(adapter, byte);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
12931da177e4SLinus Torvalds 				== 0xFF)
12941da177e4SLinus Torvalds 			cpu_relax();
12951da177e4SLinus Torvalds 		adapter->mbox->m_in.numstatus = 0xFF;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 		status = adapter->mbox->m_in.status;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 		/*
13001da177e4SLinus Torvalds 		 * decrement the pending queue counter
13011da177e4SLinus Torvalds 		 */
13021da177e4SLinus Torvalds 		atomic_sub(nstatus, &adapter->pend_cmds);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		memcpy(completed, (void *)adapter->mbox->m_in.completed,
13051da177e4SLinus Torvalds 				nstatus);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 		/* Acknowledge interrupt */
13081da177e4SLinus Torvalds 		irq_ack(adapter);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 		mega_cmd_done(adapter, completed, nstatus, status);
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		mega_rundoneq(adapter);
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 		handled = 1;
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 		/* Loop through any pending requests */
13171da177e4SLinus Torvalds 		if(atomic_read(&adapter->quiescent) == 0) {
13181da177e4SLinus Torvalds 			mega_runpendq(adapter);
13191da177e4SLinus Torvalds 		}
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	} while(1);
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds  out_unlock:
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adapter->lock, flags);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	return IRQ_RETVAL(handled);
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds /**
13321da177e4SLinus Torvalds  * megaraid_isr_memmapped()
13331da177e4SLinus Torvalds  * @irq - irq
13341da177e4SLinus Torvalds  * @devp - pointer to our soft state
13351da177e4SLinus Torvalds  *
13361da177e4SLinus Torvalds  * Interrupt service routine for memory-mapped controllers.
13371da177e4SLinus Torvalds  * Find out if our device is interrupting. If yes, acknowledge the interrupt
13381da177e4SLinus Torvalds  * and service the completed commands.
13391da177e4SLinus Torvalds  */
13401da177e4SLinus Torvalds static irqreturn_t
13417d12e780SDavid Howells megaraid_isr_memmapped(int irq, void *devp)
13421da177e4SLinus Torvalds {
13431da177e4SLinus Torvalds 	adapter_t	*adapter = devp;
13441da177e4SLinus Torvalds 	unsigned long	flags;
13451da177e4SLinus Torvalds 	u8	status;
13461da177e4SLinus Torvalds 	u32	dword = 0;
13471da177e4SLinus Torvalds 	u8	nstatus;
13481da177e4SLinus Torvalds 	u8	completed[MAX_FIRMWARE_STATUS];
13491da177e4SLinus Torvalds 	int	handled = 0;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	/*
13531da177e4SLinus Torvalds 	 * loop till F/W has more commands for us to complete.
13541da177e4SLinus Torvalds 	 */
13551da177e4SLinus Torvalds 	spin_lock_irqsave(&adapter->lock, flags);
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	do {
13581da177e4SLinus Torvalds 		/* Check if a valid interrupt is pending */
13591da177e4SLinus Torvalds 		dword = RDOUTDOOR(adapter);
13601da177e4SLinus Torvalds 		if(dword != 0x10001234) {
13611da177e4SLinus Torvalds 			/*
13621da177e4SLinus Torvalds 			 * No more pending commands
13631da177e4SLinus Torvalds 			 */
13641da177e4SLinus Torvalds 			goto out_unlock;
13651da177e4SLinus Torvalds 		}
13661da177e4SLinus Torvalds 		WROUTDOOR(adapter, 0x10001234);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 		while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
13691da177e4SLinus Torvalds 				== 0xFF) {
13701da177e4SLinus Torvalds 			cpu_relax();
13711da177e4SLinus Torvalds 		}
13721da177e4SLinus Torvalds 		adapter->mbox->m_in.numstatus = 0xFF;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 		status = adapter->mbox->m_in.status;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 		/*
13771da177e4SLinus Torvalds 		 * decrement the pending queue counter
13781da177e4SLinus Torvalds 		 */
13791da177e4SLinus Torvalds 		atomic_sub(nstatus, &adapter->pend_cmds);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		memcpy(completed, (void *)adapter->mbox->m_in.completed,
13821da177e4SLinus Torvalds 				nstatus);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 		/* Acknowledge interrupt */
13851da177e4SLinus Torvalds 		WRINDOOR(adapter, 0x2);
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 		handled = 1;
13881da177e4SLinus Torvalds 
138900769ec4SJeff Garzik 		while( RDINDOOR(adapter) & 0x02 )
139000769ec4SJeff Garzik 			cpu_relax();
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 		mega_cmd_done(adapter, completed, nstatus, status);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 		mega_rundoneq(adapter);
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 		/* Loop through any pending requests */
13971da177e4SLinus Torvalds 		if(atomic_read(&adapter->quiescent) == 0) {
13981da177e4SLinus Torvalds 			mega_runpendq(adapter);
13991da177e4SLinus Torvalds 		}
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	} while(1);
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds  out_unlock:
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adapter->lock, flags);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	return IRQ_RETVAL(handled);
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds /**
14101da177e4SLinus Torvalds  * mega_cmd_done()
14111da177e4SLinus Torvalds  * @adapter - pointer to our soft state
14121da177e4SLinus Torvalds  * @completed - array of ids of completed commands
14131da177e4SLinus Torvalds  * @nstatus - number of completed commands
14141da177e4SLinus Torvalds  * @status - status of the last command completed
14151da177e4SLinus Torvalds  *
14161da177e4SLinus Torvalds  * Complete the comamnds and call the scsi mid-layer callback hooks.
14171da177e4SLinus Torvalds  */
14181da177e4SLinus Torvalds static void
14191da177e4SLinus Torvalds mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	mega_ext_passthru	*epthru = NULL;
14221da177e4SLinus Torvalds 	struct scatterlist	*sgl;
14231da177e4SLinus Torvalds 	Scsi_Cmnd	*cmd = NULL;
14241da177e4SLinus Torvalds 	mega_passthru	*pthru = NULL;
14251da177e4SLinus Torvalds 	mbox_t	*mbox = NULL;
14261da177e4SLinus Torvalds 	u8	c;
14271da177e4SLinus Torvalds 	scb_t	*scb;
14281da177e4SLinus Torvalds 	int	islogical;
14291da177e4SLinus Torvalds 	int	cmdid;
14301da177e4SLinus Torvalds 	int	i;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	/*
14331da177e4SLinus Torvalds 	 * for all the commands completed, call the mid-layer callback routine
14341da177e4SLinus Torvalds 	 * and free the scb.
14351da177e4SLinus Torvalds 	 */
14361da177e4SLinus Torvalds 	for( i = 0; i < nstatus; i++ ) {
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 		cmdid = completed[i];
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 		if( cmdid == CMDID_INT_CMDS ) { /* internal command */
14411da177e4SLinus Torvalds 			scb = &adapter->int_scb;
14421da177e4SLinus Torvalds 			cmd = scb->cmd;
14431da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 			/*
14461da177e4SLinus Torvalds 			 * Internal command interface do not fire the extended
14471da177e4SLinus Torvalds 			 * passthru or 64-bit passthru
14481da177e4SLinus Torvalds 			 */
14491da177e4SLinus Torvalds 			pthru = scb->pthru;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 		}
14521da177e4SLinus Torvalds 		else {
14531da177e4SLinus Torvalds 			scb = &adapter->scb_list[cmdid];
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 			/*
14561da177e4SLinus Torvalds 			 * Make sure f/w has completed a valid command
14571da177e4SLinus Torvalds 			 */
14581da177e4SLinus Torvalds 			if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
14591da177e4SLinus Torvalds 				printk(KERN_CRIT
14601da177e4SLinus Torvalds 					"megaraid: invalid command ");
14611da177e4SLinus Torvalds 				printk("Id %d, scb->state:%x, scsi cmd:%p\n",
14621da177e4SLinus Torvalds 					cmdid, scb->state, scb->cmd);
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 				continue;
14651da177e4SLinus Torvalds 			}
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 			/*
14681da177e4SLinus Torvalds 			 * Was a abort issued for this command
14691da177e4SLinus Torvalds 			 */
14701da177e4SLinus Torvalds 			if( scb->state & SCB_ABORT ) {
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 				printk(KERN_WARNING
14731da177e4SLinus Torvalds 				"megaraid: aborted cmd %lx[%x] complete.\n",
14741da177e4SLinus Torvalds 					scb->cmd->serial_number, scb->idx);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 				scb->cmd->result = (DID_ABORT << 16);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 				list_add_tail(SCSI_LIST(scb->cmd),
14791da177e4SLinus Torvalds 						&adapter->completed_list);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 				mega_free_scb(adapter, scb);
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 				continue;
14841da177e4SLinus Torvalds 			}
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 			/*
14871da177e4SLinus Torvalds 			 * Was a reset issued for this command
14881da177e4SLinus Torvalds 			 */
14891da177e4SLinus Torvalds 			if( scb->state & SCB_RESET ) {
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 				printk(KERN_WARNING
14921da177e4SLinus Torvalds 				"megaraid: reset cmd %lx[%x] complete.\n",
14931da177e4SLinus Torvalds 					scb->cmd->serial_number, scb->idx);
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 				scb->cmd->result = (DID_RESET << 16);
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 				list_add_tail(SCSI_LIST(scb->cmd),
14981da177e4SLinus Torvalds 						&adapter->completed_list);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 				mega_free_scb (adapter, scb);
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 				continue;
15031da177e4SLinus Torvalds 			}
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 			cmd = scb->cmd;
15061da177e4SLinus Torvalds 			pthru = scb->pthru;
15071da177e4SLinus Torvalds 			epthru = scb->epthru;
15081da177e4SLinus Torvalds 			mbox = (mbox_t *)scb->raw_mbox;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds #if MEGA_HAVE_STATS
15111da177e4SLinus Torvalds 			{
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 			int	logdrv = mbox->m_out.logdrv;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 			islogical = adapter->logdrv_chan[cmd->channel];
15161da177e4SLinus Torvalds 			/*
15171da177e4SLinus Torvalds 			 * Maintain an error counter for the logical drive.
15181da177e4SLinus Torvalds 			 * Some application like SNMP agent need such
15191da177e4SLinus Torvalds 			 * statistics
15201da177e4SLinus Torvalds 			 */
15211da177e4SLinus Torvalds 			if( status && islogical && (cmd->cmnd[0] == READ_6 ||
15221da177e4SLinus Torvalds 						cmd->cmnd[0] == READ_10 ||
15231da177e4SLinus Torvalds 						cmd->cmnd[0] == READ_12)) {
15241da177e4SLinus Torvalds 				/*
15251da177e4SLinus Torvalds 				 * Logical drive number increases by 0x80 when
15261da177e4SLinus Torvalds 				 * a logical drive is deleted
15271da177e4SLinus Torvalds 				 */
15281da177e4SLinus Torvalds 				adapter->rd_errors[logdrv%0x80]++;
15291da177e4SLinus Torvalds 			}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 			if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
15321da177e4SLinus Torvalds 						cmd->cmnd[0] == WRITE_10 ||
15331da177e4SLinus Torvalds 						cmd->cmnd[0] == WRITE_12)) {
15341da177e4SLinus Torvalds 				/*
15351da177e4SLinus Torvalds 				 * Logical drive number increases by 0x80 when
15361da177e4SLinus Torvalds 				 * a logical drive is deleted
15371da177e4SLinus Torvalds 				 */
15381da177e4SLinus Torvalds 				adapter->wr_errors[logdrv%0x80]++;
15391da177e4SLinus Torvalds 			}
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 			}
15421da177e4SLinus Torvalds #endif
15431da177e4SLinus Torvalds 		}
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 		/*
15461da177e4SLinus Torvalds 		 * Do not return the presence of hard disk on the channel so,
15471da177e4SLinus Torvalds 		 * inquiry sent, and returned data==hard disk or removable
15481da177e4SLinus Torvalds 		 * hard disk and not logical, request should return failure! -
15491da177e4SLinus Torvalds 		 * PJ
15501da177e4SLinus Torvalds 		 */
15511da177e4SLinus Torvalds 		islogical = adapter->logdrv_chan[cmd->device->channel];
15521da177e4SLinus Torvalds 		if( cmd->cmnd[0] == INQUIRY && !islogical ) {
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 			if( cmd->use_sg ) {
15551da177e4SLinus Torvalds 				sgl = (struct scatterlist *)
15561da177e4SLinus Torvalds 					cmd->request_buffer;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 				if( sgl->page ) {
15591da177e4SLinus Torvalds 					c = *(unsigned char *)
15601da177e4SLinus Torvalds 					page_address((&sgl[0])->page) +
15611da177e4SLinus Torvalds 					(&sgl[0])->offset;
15621da177e4SLinus Torvalds 				}
15631da177e4SLinus Torvalds 				else {
15641da177e4SLinus Torvalds 					printk(KERN_WARNING
15651da177e4SLinus Torvalds 						"megaraid: invalid sg.\n");
15661da177e4SLinus Torvalds 					c = 0;
15671da177e4SLinus Torvalds 				}
15681da177e4SLinus Torvalds 			}
15691da177e4SLinus Torvalds 			else {
15701da177e4SLinus Torvalds 				c = *(u8 *)cmd->request_buffer;
15711da177e4SLinus Torvalds 			}
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 			if(IS_RAID_CH(adapter, cmd->device->channel) &&
15741da177e4SLinus Torvalds 					((c & 0x1F ) == TYPE_DISK)) {
15751da177e4SLinus Torvalds 				status = 0xF0;
15761da177e4SLinus Torvalds 			}
15771da177e4SLinus Torvalds 		}
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 		/* clear result; otherwise, success returns corrupt value */
15801da177e4SLinus Torvalds 		cmd->result = 0;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 		/* Convert MegaRAID status to Linux error code */
15831da177e4SLinus Torvalds 		switch (status) {
15841da177e4SLinus Torvalds 		case 0x00:	/* SUCCESS , i.e. SCSI_STATUS_GOOD */
15851da177e4SLinus Torvalds 			cmd->result |= (DID_OK << 16);
15861da177e4SLinus Torvalds 			break;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 		case 0x02:	/* ERROR_ABORTED, i.e.
15891da177e4SLinus Torvalds 				   SCSI_STATUS_CHECK_CONDITION */
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 			/* set sense_buffer and result fields */
15921da177e4SLinus Torvalds 			if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
15931da177e4SLinus Torvalds 				mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 				memcpy(cmd->sense_buffer, pthru->reqsensearea,
15961da177e4SLinus Torvalds 						14);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 				cmd->result = (DRIVER_SENSE << 24) |
15991da177e4SLinus Torvalds 					(DID_OK << 16) |
16001da177e4SLinus Torvalds 					(CHECK_CONDITION << 1);
16011da177e4SLinus Torvalds 			}
16021da177e4SLinus Torvalds 			else {
16031da177e4SLinus Torvalds 				if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 					memcpy(cmd->sense_buffer,
16061da177e4SLinus Torvalds 						epthru->reqsensearea, 14);
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 					cmd->result = (DRIVER_SENSE << 24) |
16091da177e4SLinus Torvalds 						(DID_OK << 16) |
16101da177e4SLinus Torvalds 						(CHECK_CONDITION << 1);
16111da177e4SLinus Torvalds 				} else {
16121da177e4SLinus Torvalds 					cmd->sense_buffer[0] = 0x70;
16131da177e4SLinus Torvalds 					cmd->sense_buffer[2] = ABORTED_COMMAND;
16141da177e4SLinus Torvalds 					cmd->result |= (CHECK_CONDITION << 1);
16151da177e4SLinus Torvalds 				}
16161da177e4SLinus Torvalds 			}
16171da177e4SLinus Torvalds 			break;
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 		case 0x08:	/* ERR_DEST_DRIVE_FAILED, i.e.
16201da177e4SLinus Torvalds 				   SCSI_STATUS_BUSY */
16211da177e4SLinus Torvalds 			cmd->result |= (DID_BUS_BUSY << 16) | status;
16221da177e4SLinus Torvalds 			break;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 		default:
16251da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
16261da177e4SLinus Torvalds 			/*
16271da177e4SLinus Torvalds 			 * If TEST_UNIT_READY fails, we know
16281da177e4SLinus Torvalds 			 * MEGA_RESERVATION_STATUS failed
16291da177e4SLinus Torvalds 			 */
16301da177e4SLinus Torvalds 			if( cmd->cmnd[0] == TEST_UNIT_READY ) {
16311da177e4SLinus Torvalds 				cmd->result |= (DID_ERROR << 16) |
16321da177e4SLinus Torvalds 					(RESERVATION_CONFLICT << 1);
16331da177e4SLinus Torvalds 			}
16341da177e4SLinus Torvalds 			else
16351da177e4SLinus Torvalds 			/*
16361da177e4SLinus Torvalds 			 * Error code returned is 1 if Reserve or Release
16371da177e4SLinus Torvalds 			 * failed or the input parameter is invalid
16381da177e4SLinus Torvalds 			 */
16391da177e4SLinus Torvalds 			if( status == 1 &&
16401da177e4SLinus Torvalds 				(cmd->cmnd[0] == RESERVE ||
16411da177e4SLinus Torvalds 					 cmd->cmnd[0] == RELEASE) ) {
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 				cmd->result |= (DID_ERROR << 16) |
16441da177e4SLinus Torvalds 					(RESERVATION_CONFLICT << 1);
16451da177e4SLinus Torvalds 			}
16461da177e4SLinus Torvalds 			else
16471da177e4SLinus Torvalds #endif
16481da177e4SLinus Torvalds 				cmd->result |= (DID_BAD_TARGET << 16)|status;
16491da177e4SLinus Torvalds 		}
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 		/*
16521da177e4SLinus Torvalds 		 * Only free SCBs for the commands coming down from the
16531da177e4SLinus Torvalds 		 * mid-layer, not for which were issued internally
16541da177e4SLinus Torvalds 		 *
16551da177e4SLinus Torvalds 		 * For internal command, restore the status returned by the
16561da177e4SLinus Torvalds 		 * firmware so that user can interpret it.
16571da177e4SLinus Torvalds 		 */
16581da177e4SLinus Torvalds 		if( cmdid == CMDID_INT_CMDS ) { /* internal command */
16591da177e4SLinus Torvalds 			cmd->result = status;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 			/*
16621da177e4SLinus Torvalds 			 * Remove the internal command from the pending list
16631da177e4SLinus Torvalds 			 */
16641da177e4SLinus Torvalds 			list_del_init(&scb->list);
16651da177e4SLinus Torvalds 			scb->state = SCB_FREE;
16661da177e4SLinus Torvalds 		}
16671da177e4SLinus Torvalds 		else {
16681da177e4SLinus Torvalds 			mega_free_scb(adapter, scb);
16691da177e4SLinus Torvalds 		}
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 		/* Add Scsi_Command to end of completed queue */
16721da177e4SLinus Torvalds 		list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
16731da177e4SLinus Torvalds 	}
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds /*
16781da177e4SLinus Torvalds  * mega_runpendq()
16791da177e4SLinus Torvalds  *
16801da177e4SLinus Torvalds  * Run through the list of completed requests and finish it
16811da177e4SLinus Torvalds  */
16821da177e4SLinus Torvalds static void
16831da177e4SLinus Torvalds mega_rundoneq (adapter_t *adapter)
16841da177e4SLinus Torvalds {
16851da177e4SLinus Torvalds 	Scsi_Cmnd *cmd;
16861da177e4SLinus Torvalds 	struct list_head *pos;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	list_for_each(pos, &adapter->completed_list) {
16891da177e4SLinus Torvalds 
16900a04137eSChristoph Hellwig 		struct scsi_pointer* spos = (struct scsi_pointer *)pos;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 		cmd = list_entry(spos, Scsi_Cmnd, SCp);
16931da177e4SLinus Torvalds 		cmd->scsi_done(cmd);
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->completed_list);
16971da177e4SLinus Torvalds }
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds /*
17011da177e4SLinus Torvalds  * Free a SCB structure
17021da177e4SLinus Torvalds  * Note: We assume the scsi commands associated with this scb is not free yet.
17031da177e4SLinus Torvalds  */
17041da177e4SLinus Torvalds static void
17051da177e4SLinus Torvalds mega_free_scb(adapter_t *adapter, scb_t *scb)
17061da177e4SLinus Torvalds {
170751c928c3SJames Bottomley 	unsigned long length;
170851c928c3SJames Bottomley 
17091da177e4SLinus Torvalds 	switch( scb->dma_type ) {
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	case MEGA_DMA_TYPE_NONE:
17121da177e4SLinus Torvalds 		break;
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	case MEGA_BULK_DATA:
171551c928c3SJames Bottomley 		if (scb->cmd->use_sg == 0)
171651c928c3SJames Bottomley 			length = scb->cmd->request_bufflen;
171751c928c3SJames Bottomley 		else {
171851c928c3SJames Bottomley 			struct scatterlist *sgl =
171951c928c3SJames Bottomley 				(struct scatterlist *)scb->cmd->request_buffer;
172051c928c3SJames Bottomley 			length = sgl->length;
172151c928c3SJames Bottomley 		}
17221da177e4SLinus Torvalds 		pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
172351c928c3SJames Bottomley 			       length, scb->dma_direction);
17241da177e4SLinus Torvalds 		break;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	case MEGA_SGLIST:
17271da177e4SLinus Torvalds 		pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
17281da177e4SLinus Torvalds 			scb->cmd->use_sg, scb->dma_direction);
17291da177e4SLinus Torvalds 		break;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	default:
17321da177e4SLinus Torvalds 		break;
17331da177e4SLinus Torvalds 	}
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	/*
17361da177e4SLinus Torvalds 	 * Remove from the pending list
17371da177e4SLinus Torvalds 	 */
17381da177e4SLinus Torvalds 	list_del_init(&scb->list);
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds 	/* Link the scb back into free list */
17411da177e4SLinus Torvalds 	scb->state = SCB_FREE;
17421da177e4SLinus Torvalds 	scb->cmd = NULL;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	list_add(&scb->list, &adapter->free_list);
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds static int
17491da177e4SLinus Torvalds __mega_busywait_mbox (adapter_t *adapter)
17501da177e4SLinus Torvalds {
17511da177e4SLinus Torvalds 	volatile mbox_t *mbox = adapter->mbox;
17521da177e4SLinus Torvalds 	long counter;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	for (counter = 0; counter < 10000; counter++) {
17551da177e4SLinus Torvalds 		if (!mbox->m_in.busy)
17561da177e4SLinus Torvalds 			return 0;
17571da177e4SLinus Torvalds 		udelay(100); yield();
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds 	return -1;		/* give up after 1 second */
17601da177e4SLinus Torvalds }
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds /*
17631da177e4SLinus Torvalds  * Copies data to SGLIST
17641da177e4SLinus Torvalds  * Note: For 64 bit cards, we need a minimum of one SG element for read/write
17651da177e4SLinus Torvalds  */
17661da177e4SLinus Torvalds static int
17671da177e4SLinus Torvalds mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	struct scatterlist	*sgl;
17701da177e4SLinus Torvalds 	struct page	*page;
17711da177e4SLinus Torvalds 	unsigned long	offset;
177251c928c3SJames Bottomley 	unsigned int	length;
17731da177e4SLinus Torvalds 	Scsi_Cmnd	*cmd;
17741da177e4SLinus Torvalds 	int	sgcnt;
17751da177e4SLinus Torvalds 	int	idx;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	cmd = scb->cmd;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	/* Scatter-gather not used */
178051c928c3SJames Bottomley 	if( cmd->use_sg == 0 || (cmd->use_sg == 1 &&
178151c928c3SJames Bottomley 				 !adapter->has_64bit_addr)) {
17821da177e4SLinus Torvalds 
178351c928c3SJames Bottomley 		if (cmd->use_sg == 0) {
17841da177e4SLinus Torvalds 			page = virt_to_page(cmd->request_buffer);
17851da177e4SLinus Torvalds 			offset = offset_in_page(cmd->request_buffer);
178651c928c3SJames Bottomley 			length = cmd->request_bufflen;
178751c928c3SJames Bottomley 		} else {
178851c928c3SJames Bottomley 			sgl = (struct scatterlist *)cmd->request_buffer;
178951c928c3SJames Bottomley 			page = sgl->page;
179051c928c3SJames Bottomley 			offset = sgl->offset;
179151c928c3SJames Bottomley 			length = sgl->length;
179251c928c3SJames Bottomley 		}
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 		scb->dma_h_bulkdata = pci_map_page(adapter->dev,
17951da177e4SLinus Torvalds 						  page, offset,
179651c928c3SJames Bottomley 						  length,
17971da177e4SLinus Torvalds 						  scb->dma_direction);
17981da177e4SLinus Torvalds 		scb->dma_type = MEGA_BULK_DATA;
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 		/*
18011da177e4SLinus Torvalds 		 * We need to handle special 64-bit commands that need a
18021da177e4SLinus Torvalds 		 * minimum of 1 SG
18031da177e4SLinus Torvalds 		 */
18041da177e4SLinus Torvalds 		if( adapter->has_64bit_addr ) {
18051da177e4SLinus Torvalds 			scb->sgl64[0].address = scb->dma_h_bulkdata;
180651c928c3SJames Bottomley 			scb->sgl64[0].length = length;
18071da177e4SLinus Torvalds 			*buf = (u32)scb->sgl_dma_addr;
180851c928c3SJames Bottomley 			*len = (u32)length;
18091da177e4SLinus Torvalds 			return 1;
18101da177e4SLinus Torvalds 		}
18111da177e4SLinus Torvalds 		else {
18121da177e4SLinus Torvalds 			*buf = (u32)scb->dma_h_bulkdata;
181351c928c3SJames Bottomley 			*len = (u32)length;
18141da177e4SLinus Torvalds 		}
18151da177e4SLinus Torvalds 		return 0;
18161da177e4SLinus Torvalds 	}
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	sgl = (struct scatterlist *)cmd->request_buffer;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	/*
18211da177e4SLinus Torvalds 	 * Copy Scatter-Gather list info into controller structure.
18221da177e4SLinus Torvalds 	 *
18231da177e4SLinus Torvalds 	 * The number of sg elements returned must not exceed our limit
18241da177e4SLinus Torvalds 	 */
18251da177e4SLinus Torvalds 	sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
18261da177e4SLinus Torvalds 			scb->dma_direction);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	scb->dma_type = MEGA_SGLIST;
18291da177e4SLinus Torvalds 
1830125e1874SEric Sesterhenn 	BUG_ON(sgcnt > adapter->sglen);
18311da177e4SLinus Torvalds 
183251c928c3SJames Bottomley 	*len = 0;
183351c928c3SJames Bottomley 
18341da177e4SLinus Torvalds 	for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 		if( adapter->has_64bit_addr ) {
18371da177e4SLinus Torvalds 			scb->sgl64[idx].address = sg_dma_address(sgl);
183851c928c3SJames Bottomley 			*len += scb->sgl64[idx].length = sg_dma_len(sgl);
18391da177e4SLinus Torvalds 		}
18401da177e4SLinus Torvalds 		else {
18411da177e4SLinus Torvalds 			scb->sgl[idx].address = sg_dma_address(sgl);
184251c928c3SJames Bottomley 			*len += scb->sgl[idx].length = sg_dma_len(sgl);
18431da177e4SLinus Torvalds 		}
18441da177e4SLinus Torvalds 	}
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	/* Reset pointer and length fields */
18471da177e4SLinus Torvalds 	*buf = scb->sgl_dma_addr;
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	/* Return count of SG requests */
18501da177e4SLinus Torvalds 	return sgcnt;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*
18551da177e4SLinus Torvalds  * mega_8_to_40ld()
18561da177e4SLinus Torvalds  *
18571da177e4SLinus Torvalds  * takes all info in AdapterInquiry structure and puts it into ProductInfo and
18581da177e4SLinus Torvalds  * Enquiry3 structures for later use
18591da177e4SLinus Torvalds  */
18601da177e4SLinus Torvalds static void
18611da177e4SLinus Torvalds mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
18621da177e4SLinus Torvalds 		mega_product_info *product_info)
18631da177e4SLinus Torvalds {
18641da177e4SLinus Torvalds 	int i;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	product_info->max_commands = inquiry->adapter_info.max_commands;
18671da177e4SLinus Torvalds 	enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
18681da177e4SLinus Torvalds 	product_info->nchannels = inquiry->adapter_info.nchannels;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	for (i = 0; i < 4; i++) {
18711da177e4SLinus Torvalds 		product_info->fw_version[i] =
18721da177e4SLinus Torvalds 			inquiry->adapter_info.fw_version[i];
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 		product_info->bios_version[i] =
18751da177e4SLinus Torvalds 			inquiry->adapter_info.bios_version[i];
18761da177e4SLinus Torvalds 	}
18771da177e4SLinus Torvalds 	enquiry3->cache_flush_interval =
18781da177e4SLinus Torvalds 		inquiry->adapter_info.cache_flush_interval;
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	product_info->dram_size = inquiry->adapter_info.dram_size;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
18851da177e4SLinus Torvalds 		enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
18861da177e4SLinus Torvalds 		enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
18871da177e4SLinus Torvalds 		enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
18911da177e4SLinus Torvalds 		enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds static inline void
18951da177e4SLinus Torvalds mega_free_sgl(adapter_t *adapter)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	scb_t	*scb;
18981da177e4SLinus Torvalds 	int	i;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	for(i = 0; i < adapter->max_cmds; i++) {
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 		scb = &adapter->scb_list[i];
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 		if( scb->sgl64 ) {
19051da177e4SLinus Torvalds 			pci_free_consistent(adapter->dev,
19061da177e4SLinus Torvalds 				sizeof(mega_sgl64) * adapter->sglen,
19071da177e4SLinus Torvalds 				scb->sgl64,
19081da177e4SLinus Torvalds 				scb->sgl_dma_addr);
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 			scb->sgl64 = NULL;
19111da177e4SLinus Torvalds 		}
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 		if( scb->pthru ) {
19141da177e4SLinus Torvalds 			pci_free_consistent(adapter->dev, sizeof(mega_passthru),
19151da177e4SLinus Torvalds 				scb->pthru, scb->pthru_dma_addr);
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 			scb->pthru = NULL;
19181da177e4SLinus Torvalds 		}
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 		if( scb->epthru ) {
19211da177e4SLinus Torvalds 			pci_free_consistent(adapter->dev,
19221da177e4SLinus Torvalds 				sizeof(mega_ext_passthru),
19231da177e4SLinus Torvalds 				scb->epthru, scb->epthru_dma_addr);
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 			scb->epthru = NULL;
19261da177e4SLinus Torvalds 		}
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	}
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds /*
19331da177e4SLinus Torvalds  * Get information about the card/driver
19341da177e4SLinus Torvalds  */
19351da177e4SLinus Torvalds const char *
19361da177e4SLinus Torvalds megaraid_info(struct Scsi_Host *host)
19371da177e4SLinus Torvalds {
19381da177e4SLinus Torvalds 	static char buffer[512];
19391da177e4SLinus Torvalds 	adapter_t *adapter;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	adapter = (adapter_t *)host->hostdata;
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	sprintf (buffer,
19441da177e4SLinus Torvalds 		 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
19451da177e4SLinus Torvalds 		 adapter->fw_version, adapter->product_info.max_commands,
19461da177e4SLinus Torvalds 		 adapter->host->max_id, adapter->host->max_channel,
19471da177e4SLinus Torvalds 		 adapter->host->max_lun);
19481da177e4SLinus Torvalds 	return buffer;
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds /*
19521da177e4SLinus Torvalds  * Abort a previous SCSI request. Only commands on the pending list can be
19531da177e4SLinus Torvalds  * aborted. All the commands issued to the F/W must complete.
19541da177e4SLinus Torvalds  */
19551da177e4SLinus Torvalds static int
19561da177e4SLinus Torvalds megaraid_abort(Scsi_Cmnd *cmd)
19571da177e4SLinus Torvalds {
19581da177e4SLinus Torvalds 	adapter_t	*adapter;
19591da177e4SLinus Torvalds 	int		rval;
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	adapter = (adapter_t *)cmd->device->host->hostdata;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	rval =  megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	/*
19661da177e4SLinus Torvalds 	 * This is required here to complete any completed requests
19671da177e4SLinus Torvalds 	 * to be communicated over to the mid layer.
19681da177e4SLinus Torvalds 	 */
19691da177e4SLinus Torvalds 	mega_rundoneq(adapter);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	return rval;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds static int
1976fa4c4966SJames Bottomley megaraid_reset(struct scsi_cmnd *cmd)
19771da177e4SLinus Torvalds {
19781da177e4SLinus Torvalds 	adapter_t	*adapter;
19791da177e4SLinus Torvalds 	megacmd_t	mc;
19801da177e4SLinus Torvalds 	int		rval;
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	adapter = (adapter_t *)cmd->device->host->hostdata;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
19851da177e4SLinus Torvalds 	mc.cmd = MEGA_CLUSTER_CMD;
19861da177e4SLinus Torvalds 	mc.opcode = MEGA_RESET_RESERVATIONS;
19871da177e4SLinus Torvalds 
1988cb0258a2SChristoph Hellwig 	if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
19891da177e4SLinus Torvalds 		printk(KERN_WARNING
19901da177e4SLinus Torvalds 				"megaraid: reservation reset failed.\n");
19911da177e4SLinus Torvalds 	}
19921da177e4SLinus Torvalds 	else {
19931da177e4SLinus Torvalds 		printk(KERN_INFO "megaraid: reservation reset.\n");
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds #endif
19961da177e4SLinus Torvalds 
1997fa4c4966SJames Bottomley 	spin_lock_irq(&adapter->lock);
1998fa4c4966SJames Bottomley 
19991da177e4SLinus Torvalds 	rval =  megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	/*
20021da177e4SLinus Torvalds 	 * This is required here to complete any completed requests
20031da177e4SLinus Torvalds 	 * to be communicated over to the mid layer.
20041da177e4SLinus Torvalds 	 */
20051da177e4SLinus Torvalds 	mega_rundoneq(adapter);
2006fa4c4966SJames Bottomley 	spin_unlock_irq(&adapter->lock);
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	return rval;
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds /**
20121da177e4SLinus Torvalds  * megaraid_abort_and_reset()
20131da177e4SLinus Torvalds  * @adapter - megaraid soft state
20141da177e4SLinus Torvalds  * @cmd - scsi command to be aborted or reset
20151da177e4SLinus Torvalds  * @aor - abort or reset flag
20161da177e4SLinus Torvalds  *
20171da177e4SLinus Torvalds  * Try to locate the scsi command in the pending queue. If found and is not
20181da177e4SLinus Torvalds  * issued to the controller, abort/reset it. Otherwise return failure
20191da177e4SLinus Torvalds  */
20201da177e4SLinus Torvalds static int
20211da177e4SLinus Torvalds megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
20221da177e4SLinus Torvalds {
20231da177e4SLinus Torvalds 	struct list_head	*pos, *next;
20241da177e4SLinus Torvalds 	scb_t			*scb;
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
20271da177e4SLinus Torvalds 	     (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
20281da177e4SLinus Torvalds 	     cmd->cmnd[0], cmd->device->channel,
20291da177e4SLinus Torvalds 	     cmd->device->id, cmd->device->lun);
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	if(list_empty(&adapter->pending_list))
20321da177e4SLinus Torvalds 		return FALSE;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &adapter->pending_list) {
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 		scb = list_entry(pos, scb_t, list);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 		if (scb->cmd == cmd) { /* Found command */
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 			scb->state |= aor;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 			/*
20431da177e4SLinus Torvalds 			 * Check if this command has firmare owenership. If
20441da177e4SLinus Torvalds 			 * yes, we cannot reset this command. Whenever, f/w
20451da177e4SLinus Torvalds 			 * completes this command, we will return appropriate
20461da177e4SLinus Torvalds 			 * status from ISR.
20471da177e4SLinus Torvalds 			 */
20481da177e4SLinus Torvalds 			if( scb->state & SCB_ISSUED ) {
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 				printk(KERN_WARNING
20511da177e4SLinus Torvalds 					"megaraid: %s-%lx[%x], fw owner.\n",
20521da177e4SLinus Torvalds 					(aor==SCB_ABORT) ? "ABORTING":"RESET",
20531da177e4SLinus Torvalds 					cmd->serial_number, scb->idx);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 				return FALSE;
20561da177e4SLinus Torvalds 			}
20571da177e4SLinus Torvalds 			else {
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 				/*
20601da177e4SLinus Torvalds 				 * Not yet issued! Remove from the pending
20611da177e4SLinus Torvalds 				 * list
20621da177e4SLinus Torvalds 				 */
20631da177e4SLinus Torvalds 				printk(KERN_WARNING
20641da177e4SLinus Torvalds 					"megaraid: %s-%lx[%x], driver owner.\n",
20651da177e4SLinus Torvalds 					(aor==SCB_ABORT) ? "ABORTING":"RESET",
20661da177e4SLinus Torvalds 					cmd->serial_number, scb->idx);
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 				mega_free_scb(adapter, scb);
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 				if( aor == SCB_ABORT ) {
20711da177e4SLinus Torvalds 					cmd->result = (DID_ABORT << 16);
20721da177e4SLinus Torvalds 				}
20731da177e4SLinus Torvalds 				else {
20741da177e4SLinus Torvalds 					cmd->result = (DID_RESET << 16);
20751da177e4SLinus Torvalds 				}
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 				list_add_tail(SCSI_LIST(cmd),
20781da177e4SLinus Torvalds 						&adapter->completed_list);
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 				return TRUE;
20811da177e4SLinus Torvalds 			}
20821da177e4SLinus Torvalds 		}
20831da177e4SLinus Torvalds 	}
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	return FALSE;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds static inline int
20891da177e4SLinus Torvalds make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
20901da177e4SLinus Torvalds {
2091*bab41e9bSMichael Ellerman 	*pdev = alloc_pci_dev();
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	if( *pdev == NULL ) return -1;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
20961da177e4SLinus Torvalds 
2097910638aeSMatthias Gehre 	if( pci_set_dma_mask(*pdev, DMA_32BIT_MASK) != 0 ) {
20981da177e4SLinus Torvalds 		kfree(*pdev);
20991da177e4SLinus Torvalds 		return -1;
21001da177e4SLinus Torvalds 	}
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	return 0;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds static inline void
21061da177e4SLinus Torvalds free_local_pdev(struct pci_dev *pdev)
21071da177e4SLinus Torvalds {
21081da177e4SLinus Torvalds 	kfree(pdev);
21091da177e4SLinus Torvalds }
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds /**
21121da177e4SLinus Torvalds  * mega_allocate_inquiry()
21131da177e4SLinus Torvalds  * @dma_handle - handle returned for dma address
21141da177e4SLinus Torvalds  * @pdev - handle to pci device
21151da177e4SLinus Torvalds  *
21161da177e4SLinus Torvalds  * allocates memory for inquiry structure
21171da177e4SLinus Torvalds  */
21181da177e4SLinus Torvalds static inline void *
21191da177e4SLinus Torvalds mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
21201da177e4SLinus Torvalds {
21211da177e4SLinus Torvalds 	return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds static inline void
21261da177e4SLinus Torvalds mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
21271da177e4SLinus Torvalds {
21281da177e4SLinus Torvalds 	pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
21331da177e4SLinus Torvalds /* Following code handles /proc fs  */
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds #define CREATE_READ_PROC(string, func)	create_proc_read_entry(string,	\
21361da177e4SLinus Torvalds 					S_IRUSR | S_IFREG,		\
21371da177e4SLinus Torvalds 					controller_proc_dir_entry,	\
21381da177e4SLinus Torvalds 					func, adapter)
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds /**
21411da177e4SLinus Torvalds  * mega_create_proc_entry()
21421da177e4SLinus Torvalds  * @index - index in soft state array
21431da177e4SLinus Torvalds  * @parent - parent node for this /proc entry
21441da177e4SLinus Torvalds  *
21451da177e4SLinus Torvalds  * Creates /proc entries for our controllers.
21461da177e4SLinus Torvalds  */
21471da177e4SLinus Torvalds static void
21481da177e4SLinus Torvalds mega_create_proc_entry(int index, struct proc_dir_entry *parent)
21491da177e4SLinus Torvalds {
21501da177e4SLinus Torvalds 	struct proc_dir_entry	*controller_proc_dir_entry = NULL;
21511da177e4SLinus Torvalds 	u8		string[64] = { 0 };
21521da177e4SLinus Torvalds 	adapter_t	*adapter = hba_soft_state[index];
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	sprintf(string, "hba%d", adapter->host->host_no);
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	controller_proc_dir_entry =
21571da177e4SLinus Torvalds 		adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	if(!controller_proc_dir_entry) {
21601da177e4SLinus Torvalds 		printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
21611da177e4SLinus Torvalds 		return;
21621da177e4SLinus Torvalds 	}
21631da177e4SLinus Torvalds 	adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
21641da177e4SLinus Torvalds 	adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
21651da177e4SLinus Torvalds 	adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
21661da177e4SLinus Torvalds #if MEGA_HAVE_ENH_PROC
21671da177e4SLinus Torvalds 	adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
21681da177e4SLinus Torvalds 	adapter->proc_battery = CREATE_READ_PROC("battery-status",
21691da177e4SLinus Torvalds 			proc_battery);
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	/*
21721da177e4SLinus Torvalds 	 * Display each physical drive on its channel
21731da177e4SLinus Torvalds 	 */
21741da177e4SLinus Torvalds 	adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
21751da177e4SLinus Torvalds 					proc_pdrv_ch0);
21761da177e4SLinus Torvalds 	adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
21771da177e4SLinus Torvalds 					proc_pdrv_ch1);
21781da177e4SLinus Torvalds 	adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
21791da177e4SLinus Torvalds 					proc_pdrv_ch2);
21801da177e4SLinus Torvalds 	adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
21811da177e4SLinus Torvalds 					proc_pdrv_ch3);
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	/*
21841da177e4SLinus Torvalds 	 * Display a set of up to 10 logical drive through each of following
21851da177e4SLinus Torvalds 	 * /proc entries
21861da177e4SLinus Torvalds 	 */
21871da177e4SLinus Torvalds 	adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
21881da177e4SLinus Torvalds 					proc_rdrv_10);
21891da177e4SLinus Torvalds 	adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
21901da177e4SLinus Torvalds 					proc_rdrv_20);
21911da177e4SLinus Torvalds 	adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
21921da177e4SLinus Torvalds 					proc_rdrv_30);
21931da177e4SLinus Torvalds 	adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
21941da177e4SLinus Torvalds 					proc_rdrv_40);
21951da177e4SLinus Torvalds #endif
21961da177e4SLinus Torvalds }
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds /**
22001da177e4SLinus Torvalds  * proc_read_config()
22011da177e4SLinus Torvalds  * @page - buffer to write the data in
22021da177e4SLinus Torvalds  * @start - where the actual data has been written in page
22031da177e4SLinus Torvalds  * @offset - same meaning as the read system call
22041da177e4SLinus Torvalds  * @count - same meaning as the read system call
22051da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
22061da177e4SLinus Torvalds  * @data - pointer to our soft state
22071da177e4SLinus Torvalds  *
22081da177e4SLinus Torvalds  * Display configuration information about the controller.
22091da177e4SLinus Torvalds  */
22101da177e4SLinus Torvalds static int
22111da177e4SLinus Torvalds proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
22121da177e4SLinus Torvalds 		void *data)
22131da177e4SLinus Torvalds {
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
22161da177e4SLinus Torvalds 	int len = 0;
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	len += sprintf(page+len, "%s", MEGARAID_VERSION);
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	if(adapter->product_info.product_name[0])
22211da177e4SLinus Torvalds 		len += sprintf(page+len, "%s\n",
22221da177e4SLinus Torvalds 				adapter->product_info.product_name);
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	len += sprintf(page+len, "Controller Type: ");
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	if( adapter->flag & BOARD_MEMMAP ) {
22271da177e4SLinus Torvalds 		len += sprintf(page+len,
22281da177e4SLinus Torvalds 			"438/466/467/471/493/518/520/531/532\n");
22291da177e4SLinus Torvalds 	}
22301da177e4SLinus Torvalds 	else {
22311da177e4SLinus Torvalds 		len += sprintf(page+len,
22321da177e4SLinus Torvalds 			"418/428/434\n");
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds 	if(adapter->flag & BOARD_40LD) {
22361da177e4SLinus Torvalds 		len += sprintf(page+len,
22371da177e4SLinus Torvalds 				"Controller Supports 40 Logical Drives\n");
22381da177e4SLinus Torvalds 	}
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	if(adapter->flag & BOARD_64BIT) {
22411da177e4SLinus Torvalds 		len += sprintf(page+len,
22421da177e4SLinus Torvalds 		"Controller capable of 64-bit memory addressing\n");
22431da177e4SLinus Torvalds 	}
22441da177e4SLinus Torvalds 	if( adapter->has_64bit_addr ) {
22451da177e4SLinus Torvalds 		len += sprintf(page+len,
22461da177e4SLinus Torvalds 			"Controller using 64-bit memory addressing\n");
22471da177e4SLinus Torvalds 	}
22481da177e4SLinus Torvalds 	else {
22491da177e4SLinus Torvalds 		len += sprintf(page+len,
22501da177e4SLinus Torvalds 			"Controller is not using 64-bit memory addressing\n");
22511da177e4SLinus Torvalds 	}
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
22541da177e4SLinus Torvalds 			adapter->host->irq);
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
22571da177e4SLinus Torvalds 			adapter->numldrv, adapter->product_info.nchannels);
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
22601da177e4SLinus Torvalds 			adapter->fw_version, adapter->bios_version,
22611da177e4SLinus Torvalds 			adapter->product_info.dram_size);
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	len += sprintf(page+len,
22641da177e4SLinus Torvalds 		"Controller Queue Depth = %d, Driver Queue Depth = %d\n",
22651da177e4SLinus Torvalds 		adapter->product_info.max_commands, adapter->max_cmds);
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	len += sprintf(page+len, "support_ext_cdb    = %d\n",
22681da177e4SLinus Torvalds 			adapter->support_ext_cdb);
22691da177e4SLinus Torvalds 	len += sprintf(page+len, "support_random_del = %d\n",
22701da177e4SLinus Torvalds 			adapter->support_random_del);
22711da177e4SLinus Torvalds 	len += sprintf(page+len, "boot_ldrv_enabled  = %d\n",
22721da177e4SLinus Torvalds 			adapter->boot_ldrv_enabled);
22731da177e4SLinus Torvalds 	len += sprintf(page+len, "boot_ldrv          = %d\n",
22741da177e4SLinus Torvalds 			adapter->boot_ldrv);
22751da177e4SLinus Torvalds 	len += sprintf(page+len, "boot_pdrv_enabled  = %d\n",
22761da177e4SLinus Torvalds 			adapter->boot_pdrv_enabled);
22771da177e4SLinus Torvalds 	len += sprintf(page+len, "boot_pdrv_ch       = %d\n",
22781da177e4SLinus Torvalds 			adapter->boot_pdrv_ch);
22791da177e4SLinus Torvalds 	len += sprintf(page+len, "boot_pdrv_tgt      = %d\n",
22801da177e4SLinus Torvalds 			adapter->boot_pdrv_tgt);
22811da177e4SLinus Torvalds 	len += sprintf(page+len, "quiescent          = %d\n",
22821da177e4SLinus Torvalds 			atomic_read(&adapter->quiescent));
22831da177e4SLinus Torvalds 	len += sprintf(page+len, "has_cluster        = %d\n",
22841da177e4SLinus Torvalds 			adapter->has_cluster);
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	len += sprintf(page+len, "\nModule Parameters:\n");
22871da177e4SLinus Torvalds 	len += sprintf(page+len, "max_cmd_per_lun    = %d\n",
22881da177e4SLinus Torvalds 			max_cmd_per_lun);
22891da177e4SLinus Torvalds 	len += sprintf(page+len, "max_sectors_per_io = %d\n",
22901da177e4SLinus Torvalds 			max_sectors_per_io);
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 	*eof = 1;
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	return len;
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds /**
23001da177e4SLinus Torvalds  * proc_read_stat()
23011da177e4SLinus Torvalds  * @page - buffer to write the data in
23021da177e4SLinus Torvalds  * @start - where the actual data has been written in page
23031da177e4SLinus Torvalds  * @offset - same meaning as the read system call
23041da177e4SLinus Torvalds  * @count - same meaning as the read system call
23051da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
23061da177e4SLinus Torvalds  * @data - pointer to our soft state
23071da177e4SLinus Torvalds  *
23081da177e4SLinus Torvalds  * Diaplay statistical information about the I/O activity.
23091da177e4SLinus Torvalds  */
23101da177e4SLinus Torvalds static int
23111da177e4SLinus Torvalds proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
23121da177e4SLinus Torvalds 		void *data)
23131da177e4SLinus Torvalds {
23141da177e4SLinus Torvalds 	adapter_t	*adapter;
23151da177e4SLinus Torvalds 	int	len;
23161da177e4SLinus Torvalds 	int	i;
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds 	i = 0;	/* avoid compilation warnings */
23191da177e4SLinus Torvalds 	len = 0;
23201da177e4SLinus Torvalds 	adapter = (adapter_t *)data;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	len = sprintf(page, "Statistical Information for this controller\n");
23231da177e4SLinus Torvalds 	len += sprintf(page+len, "pend_cmds = %d\n",
23241da177e4SLinus Torvalds 			atomic_read(&adapter->pend_cmds));
23251da177e4SLinus Torvalds #if MEGA_HAVE_STATS
23261da177e4SLinus Torvalds 	for(i = 0; i < adapter->numldrv; i++) {
23271da177e4SLinus Torvalds 		len += sprintf(page+len, "Logical Drive %d:\n", i);
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 		len += sprintf(page+len,
23301da177e4SLinus Torvalds 			"\tReads Issued = %lu, Writes Issued = %lu\n",
23311da177e4SLinus Torvalds 			adapter->nreads[i], adapter->nwrites[i]);
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 		len += sprintf(page+len,
23341da177e4SLinus Torvalds 			"\tSectors Read = %lu, Sectors Written = %lu\n",
23351da177e4SLinus Torvalds 			adapter->nreadblocks[i], adapter->nwriteblocks[i]);
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 		len += sprintf(page+len,
23381da177e4SLinus Torvalds 			"\tRead errors = %lu, Write errors = %lu\n\n",
23391da177e4SLinus Torvalds 			adapter->rd_errors[i], adapter->wr_errors[i]);
23401da177e4SLinus Torvalds 	}
23411da177e4SLinus Torvalds #else
23421da177e4SLinus Torvalds 	len += sprintf(page+len,
23431da177e4SLinus Torvalds 			"IO and error counters not compiled in driver.\n");
23441da177e4SLinus Torvalds #endif
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	*eof = 1;
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 	return len;
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds /**
23531da177e4SLinus Torvalds  * proc_read_mbox()
23541da177e4SLinus Torvalds  * @page - buffer to write the data in
23551da177e4SLinus Torvalds  * @start - where the actual data has been written in page
23561da177e4SLinus Torvalds  * @offset - same meaning as the read system call
23571da177e4SLinus Torvalds  * @count - same meaning as the read system call
23581da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
23591da177e4SLinus Torvalds  * @data - pointer to our soft state
23601da177e4SLinus Torvalds  *
23611da177e4SLinus Torvalds  * Display mailbox information for the last command issued. This information
23621da177e4SLinus Torvalds  * is good for debugging.
23631da177e4SLinus Torvalds  */
23641da177e4SLinus Torvalds static int
23651da177e4SLinus Torvalds proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
23661da177e4SLinus Torvalds 		void *data)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	adapter_t	*adapter = (adapter_t *)data;
23701da177e4SLinus Torvalds 	volatile mbox_t	*mbox = adapter->mbox;
23711da177e4SLinus Torvalds 	int	len = 0;
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	len = sprintf(page, "Contents of Mail Box Structure\n");
23741da177e4SLinus Torvalds 	len += sprintf(page+len, "  Fw Command   = 0x%02x\n",
23751da177e4SLinus Torvalds 			mbox->m_out.cmd);
23761da177e4SLinus Torvalds 	len += sprintf(page+len, "  Cmd Sequence = 0x%02x\n",
23771da177e4SLinus Torvalds 			mbox->m_out.cmdid);
23781da177e4SLinus Torvalds 	len += sprintf(page+len, "  No of Sectors= %04d\n",
23791da177e4SLinus Torvalds 			mbox->m_out.numsectors);
23801da177e4SLinus Torvalds 	len += sprintf(page+len, "  LBA          = 0x%02x\n",
23811da177e4SLinus Torvalds 			mbox->m_out.lba);
23821da177e4SLinus Torvalds 	len += sprintf(page+len, "  DTA          = 0x%08x\n",
23831da177e4SLinus Torvalds 			mbox->m_out.xferaddr);
23841da177e4SLinus Torvalds 	len += sprintf(page+len, "  Logical Drive= 0x%02x\n",
23851da177e4SLinus Torvalds 			mbox->m_out.logdrv);
23861da177e4SLinus Torvalds 	len += sprintf(page+len, "  No of SG Elmt= 0x%02x\n",
23871da177e4SLinus Torvalds 			mbox->m_out.numsgelements);
23881da177e4SLinus Torvalds 	len += sprintf(page+len, "  Busy         = %01x\n",
23891da177e4SLinus Torvalds 			mbox->m_in.busy);
23901da177e4SLinus Torvalds 	len += sprintf(page+len, "  Status       = 0x%02x\n",
23911da177e4SLinus Torvalds 			mbox->m_in.status);
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	*eof = 1;
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	return len;
23961da177e4SLinus Torvalds }
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds /**
24001da177e4SLinus Torvalds  * proc_rebuild_rate()
24011da177e4SLinus Torvalds  * @page - buffer to write the data in
24021da177e4SLinus Torvalds  * @start - where the actual data has been written in page
24031da177e4SLinus Torvalds  * @offset - same meaning as the read system call
24041da177e4SLinus Torvalds  * @count - same meaning as the read system call
24051da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
24061da177e4SLinus Torvalds  * @data - pointer to our soft state
24071da177e4SLinus Torvalds  *
24081da177e4SLinus Torvalds  * Display current rebuild rate
24091da177e4SLinus Torvalds  */
24101da177e4SLinus Torvalds static int
24111da177e4SLinus Torvalds proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
24121da177e4SLinus Torvalds 		void *data)
24131da177e4SLinus Torvalds {
24141da177e4SLinus Torvalds 	adapter_t	*adapter = (adapter_t *)data;
24151da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
24161da177e4SLinus Torvalds 	caddr_t		inquiry;
24171da177e4SLinus Torvalds 	struct pci_dev	*pdev;
24181da177e4SLinus Torvalds 	int	len = 0;
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	if( make_local_pdev(adapter, &pdev) != 0 ) {
24211da177e4SLinus Torvalds 		*eof = 1;
24221da177e4SLinus Torvalds 		return len;
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
24261da177e4SLinus Torvalds 		free_local_pdev(pdev);
24271da177e4SLinus Torvalds 		*eof = 1;
24281da177e4SLinus Torvalds 		return len;
24291da177e4SLinus Torvalds 	}
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 		len = sprintf(page, "Adapter inquiry failed.\n");
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 		mega_free_inquiry(inquiry, dma_handle, pdev);
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds 		free_local_pdev(pdev);
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 		*eof = 1;
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 		return len;
24441da177e4SLinus Torvalds 	}
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
24471da177e4SLinus Torvalds 		len = sprintf(page, "Rebuild Rate: [%d%%]\n",
24481da177e4SLinus Torvalds 			((mega_inquiry3 *)inquiry)->rebuild_rate);
24491da177e4SLinus Torvalds 	}
24501da177e4SLinus Torvalds 	else {
24511da177e4SLinus Torvalds 		len = sprintf(page, "Rebuild Rate: [%d%%]\n",
24521da177e4SLinus Torvalds 			((mraid_ext_inquiry *)
24531da177e4SLinus Torvalds 			inquiry)->raid_inq.adapter_info.rebuild_rate);
24541da177e4SLinus Torvalds 	}
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 	free_local_pdev(pdev);
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	*eof = 1;
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 	return len;
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds /**
24681da177e4SLinus Torvalds  * proc_battery()
24691da177e4SLinus Torvalds  * @page - buffer to write the data in
24701da177e4SLinus Torvalds  * @start - where the actual data has been written in page
24711da177e4SLinus Torvalds  * @offset - same meaning as the read system call
24721da177e4SLinus Torvalds  * @count - same meaning as the read system call
24731da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
24741da177e4SLinus Torvalds  * @data - pointer to our soft state
24751da177e4SLinus Torvalds  *
24761da177e4SLinus Torvalds  * Display information about the battery module on the controller.
24771da177e4SLinus Torvalds  */
24781da177e4SLinus Torvalds static int
24791da177e4SLinus Torvalds proc_battery(char *page, char **start, off_t offset, int count, int *eof,
24801da177e4SLinus Torvalds 		void *data)
24811da177e4SLinus Torvalds {
24821da177e4SLinus Torvalds 	adapter_t	*adapter = (adapter_t *)data;
24831da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
24841da177e4SLinus Torvalds 	caddr_t		inquiry;
24851da177e4SLinus Torvalds 	struct pci_dev	*pdev;
24861da177e4SLinus Torvalds 	u8	battery_status = 0;
24871da177e4SLinus Torvalds 	char	str[256];
24881da177e4SLinus Torvalds 	int	len = 0;
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 	if( make_local_pdev(adapter, &pdev) != 0 ) {
24911da177e4SLinus Torvalds 		*eof = 1;
24921da177e4SLinus Torvalds 		return len;
24931da177e4SLinus Torvalds 	}
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
24961da177e4SLinus Torvalds 		free_local_pdev(pdev);
24971da177e4SLinus Torvalds 		*eof = 1;
24981da177e4SLinus Torvalds 		return len;
24991da177e4SLinus Torvalds 	}
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 		len = sprintf(page, "Adapter inquiry failed.\n");
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 		mega_free_inquiry(inquiry, dma_handle, pdev);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 		free_local_pdev(pdev);
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 		*eof = 1;
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 		return len;
25141da177e4SLinus Torvalds 	}
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
25171da177e4SLinus Torvalds 		battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
25181da177e4SLinus Torvalds 	}
25191da177e4SLinus Torvalds 	else {
25201da177e4SLinus Torvalds 		battery_status = ((mraid_ext_inquiry *)inquiry)->
25211da177e4SLinus Torvalds 			raid_inq.adapter_info.battery_status;
25221da177e4SLinus Torvalds 	}
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	/*
25251da177e4SLinus Torvalds 	 * Decode the battery status
25261da177e4SLinus Torvalds 	 */
25271da177e4SLinus Torvalds 	sprintf(str, "Battery Status:[%d]", battery_status);
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	if(battery_status == MEGA_BATT_CHARGE_DONE)
25301da177e4SLinus Torvalds 		strcat(str, " Charge Done");
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_MODULE_MISSING)
25331da177e4SLinus Torvalds 		strcat(str, " Module Missing");
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_LOW_VOLTAGE)
25361da177e4SLinus Torvalds 		strcat(str, " Low Voltage");
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_TEMP_HIGH)
25391da177e4SLinus Torvalds 		strcat(str, " Temperature High");
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_PACK_MISSING)
25421da177e4SLinus Torvalds 		strcat(str, " Pack Missing");
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_CHARGE_INPROG)
25451da177e4SLinus Torvalds 		strcat(str, " Charge In-progress");
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_CHARGE_FAIL)
25481da177e4SLinus Torvalds 		strcat(str, " Charge Fail");
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 	if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
25511da177e4SLinus Torvalds 		strcat(str, " Cycles Exceeded");
25521da177e4SLinus Torvalds 
25531da177e4SLinus Torvalds 	len = sprintf(page, "%s\n", str);
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	free_local_pdev(pdev);
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	*eof = 1;
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	return len;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds /**
25671da177e4SLinus Torvalds  * proc_pdrv_ch0()
25681da177e4SLinus Torvalds  * @page - buffer to write the data in
25691da177e4SLinus Torvalds  * @start - where the actual data has been written in page
25701da177e4SLinus Torvalds  * @offset - same meaning as the read system call
25711da177e4SLinus Torvalds  * @count - same meaning as the read system call
25721da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
25731da177e4SLinus Torvalds  * @data - pointer to our soft state
25741da177e4SLinus Torvalds  *
25751da177e4SLinus Torvalds  * Display information about the physical drives on physical channel 0.
25761da177e4SLinus Torvalds  */
25771da177e4SLinus Torvalds static int
25781da177e4SLinus Torvalds proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
25791da177e4SLinus Torvalds 		void *data)
25801da177e4SLinus Torvalds {
25811da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 	*eof = 1;
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 	return (proc_pdrv(adapter, page, 0));
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds /**
25901da177e4SLinus Torvalds  * proc_pdrv_ch1()
25911da177e4SLinus Torvalds  * @page - buffer to write the data in
25921da177e4SLinus Torvalds  * @start - where the actual data has been written in page
25931da177e4SLinus Torvalds  * @offset - same meaning as the read system call
25941da177e4SLinus Torvalds  * @count - same meaning as the read system call
25951da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
25961da177e4SLinus Torvalds  * @data - pointer to our soft state
25971da177e4SLinus Torvalds  *
25981da177e4SLinus Torvalds  * Display information about the physical drives on physical channel 1.
25991da177e4SLinus Torvalds  */
26001da177e4SLinus Torvalds static int
26011da177e4SLinus Torvalds proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
26021da177e4SLinus Torvalds 		void *data)
26031da177e4SLinus Torvalds {
26041da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	*eof = 1;
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	return (proc_pdrv(adapter, page, 1));
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds /**
26131da177e4SLinus Torvalds  * proc_pdrv_ch2()
26141da177e4SLinus Torvalds  * @page - buffer to write the data in
26151da177e4SLinus Torvalds  * @start - where the actual data has been written in page
26161da177e4SLinus Torvalds  * @offset - same meaning as the read system call
26171da177e4SLinus Torvalds  * @count - same meaning as the read system call
26181da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
26191da177e4SLinus Torvalds  * @data - pointer to our soft state
26201da177e4SLinus Torvalds  *
26211da177e4SLinus Torvalds  * Display information about the physical drives on physical channel 2.
26221da177e4SLinus Torvalds  */
26231da177e4SLinus Torvalds static int
26241da177e4SLinus Torvalds proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
26251da177e4SLinus Torvalds 		void *data)
26261da177e4SLinus Torvalds {
26271da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds 	*eof = 1;
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	return (proc_pdrv(adapter, page, 2));
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds /**
26361da177e4SLinus Torvalds  * proc_pdrv_ch3()
26371da177e4SLinus Torvalds  * @page - buffer to write the data in
26381da177e4SLinus Torvalds  * @start - where the actual data has been written in page
26391da177e4SLinus Torvalds  * @offset - same meaning as the read system call
26401da177e4SLinus Torvalds  * @count - same meaning as the read system call
26411da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
26421da177e4SLinus Torvalds  * @data - pointer to our soft state
26431da177e4SLinus Torvalds  *
26441da177e4SLinus Torvalds  * Display information about the physical drives on physical channel 3.
26451da177e4SLinus Torvalds  */
26461da177e4SLinus Torvalds static int
26471da177e4SLinus Torvalds proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
26481da177e4SLinus Torvalds 		void *data)
26491da177e4SLinus Torvalds {
26501da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	*eof = 1;
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	return (proc_pdrv(adapter, page, 3));
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds /**
26591da177e4SLinus Torvalds  * proc_pdrv()
26601da177e4SLinus Torvalds  * @page - buffer to write the data in
26611da177e4SLinus Torvalds  * @adapter - pointer to our soft state
26621da177e4SLinus Torvalds  *
26631da177e4SLinus Torvalds  * Display information about the physical drives.
26641da177e4SLinus Torvalds  */
26651da177e4SLinus Torvalds static int
26661da177e4SLinus Torvalds proc_pdrv(adapter_t *adapter, char *page, int channel)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
26691da177e4SLinus Torvalds 	char		*scsi_inq;
26701da177e4SLinus Torvalds 	dma_addr_t	scsi_inq_dma_handle;
26711da177e4SLinus Torvalds 	caddr_t		inquiry;
26721da177e4SLinus Torvalds 	struct pci_dev	*pdev;
26731da177e4SLinus Torvalds 	u8	*pdrv_state;
26741da177e4SLinus Torvalds 	u8	state;
26751da177e4SLinus Torvalds 	int	tgt;
26761da177e4SLinus Torvalds 	int	max_channels;
26771da177e4SLinus Torvalds 	int	len = 0;
26781da177e4SLinus Torvalds 	char	str[80];
26791da177e4SLinus Torvalds 	int	i;
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 	if( make_local_pdev(adapter, &pdev) != 0 ) {
26821da177e4SLinus Torvalds 		return len;
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
26861da177e4SLinus Torvalds 		goto free_pdev;
26871da177e4SLinus Torvalds 	}
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
26901da177e4SLinus Torvalds 		len = sprintf(page, "Adapter inquiry failed.\n");
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 		goto free_inquiry;
26951da177e4SLinus Torvalds 	}
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds 	scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
26991da177e4SLinus Torvalds 
27001da177e4SLinus Torvalds 	if( scsi_inq == NULL ) {
27011da177e4SLinus Torvalds 		len = sprintf(page, "memory not available for scsi inq.\n");
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 		goto free_inquiry;
27041da177e4SLinus Torvalds 	}
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
27071da177e4SLinus Torvalds 		pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
27081da177e4SLinus Torvalds 	}
27091da177e4SLinus Torvalds 	else {
27101da177e4SLinus Torvalds 		pdrv_state = ((mraid_ext_inquiry *)inquiry)->
27111da177e4SLinus Torvalds 			raid_inq.pdrv_info.pdrv_state;
27121da177e4SLinus Torvalds 	}
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	max_channels = adapter->product_info.nchannels;
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 	if( channel >= max_channels ) {
27171da177e4SLinus Torvalds 		goto free_pci;
27181da177e4SLinus Torvalds 	}
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 	for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 		i = channel*16 + tgt;
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 		state = *(pdrv_state + i);
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds 		switch( state & 0x0F ) {
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 		case PDRV_ONLINE:
27291da177e4SLinus Torvalds 			sprintf(str,
27301da177e4SLinus Torvalds 			"Channel:%2d Id:%2d State: Online",
27311da177e4SLinus Torvalds 				channel, tgt);
27321da177e4SLinus Torvalds 			break;
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 		case PDRV_FAILED:
27351da177e4SLinus Torvalds 			sprintf(str,
27361da177e4SLinus Torvalds 			"Channel:%2d Id:%2d State: Failed",
27371da177e4SLinus Torvalds 				channel, tgt);
27381da177e4SLinus Torvalds 			break;
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 		case PDRV_RBLD:
27411da177e4SLinus Torvalds 			sprintf(str,
27421da177e4SLinus Torvalds 			"Channel:%2d Id:%2d State: Rebuild",
27431da177e4SLinus Torvalds 				channel, tgt);
27441da177e4SLinus Torvalds 			break;
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 		case PDRV_HOTSPARE:
27471da177e4SLinus Torvalds 			sprintf(str,
27481da177e4SLinus Torvalds 			"Channel:%2d Id:%2d State: Hot spare",
27491da177e4SLinus Torvalds 				channel, tgt);
27501da177e4SLinus Torvalds 			break;
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 		default:
27531da177e4SLinus Torvalds 			sprintf(str,
27541da177e4SLinus Torvalds 			"Channel:%2d Id:%2d State: Un-configured",
27551da177e4SLinus Torvalds 				channel, tgt);
27561da177e4SLinus Torvalds 			break;
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 		}
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds 		/*
27611da177e4SLinus Torvalds 		 * This interface displays inquiries for disk drives
27621da177e4SLinus Torvalds 		 * only. Inquries for logical drives and non-disk
27631da177e4SLinus Torvalds 		 * devices are available through /proc/scsi/scsi
27641da177e4SLinus Torvalds 		 */
27651da177e4SLinus Torvalds 		memset(scsi_inq, 0, 256);
27661da177e4SLinus Torvalds 		if( mega_internal_dev_inquiry(adapter, channel, tgt,
27671da177e4SLinus Torvalds 				scsi_inq_dma_handle) ||
27681da177e4SLinus Torvalds 				(scsi_inq[0] & 0x1F) != TYPE_DISK ) {
27691da177e4SLinus Torvalds 			continue;
27701da177e4SLinus Torvalds 		}
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds 		/*
27731da177e4SLinus Torvalds 		 * Check for overflow. We print less than 240
27741da177e4SLinus Torvalds 		 * characters for inquiry
27751da177e4SLinus Torvalds 		 */
27761da177e4SLinus Torvalds 		if( (len + 240) >= PAGE_SIZE ) break;
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 		len += sprintf(page+len, "%s.\n", str);
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 		len += mega_print_inquiry(page+len, scsi_inq);
27811da177e4SLinus Torvalds 	}
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds free_pci:
27841da177e4SLinus Torvalds 	pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
27851da177e4SLinus Torvalds free_inquiry:
27861da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
27871da177e4SLinus Torvalds free_pdev:
27881da177e4SLinus Torvalds 	free_local_pdev(pdev);
27891da177e4SLinus Torvalds 
27901da177e4SLinus Torvalds 	return len;
27911da177e4SLinus Torvalds }
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds /*
27951da177e4SLinus Torvalds  * Display scsi inquiry
27961da177e4SLinus Torvalds  */
27971da177e4SLinus Torvalds static int
27981da177e4SLinus Torvalds mega_print_inquiry(char *page, char *scsi_inq)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	int	len = 0;
28011da177e4SLinus Torvalds 	int	i;
28021da177e4SLinus Torvalds 
28031da177e4SLinus Torvalds 	len = sprintf(page, "  Vendor: ");
28041da177e4SLinus Torvalds 	for( i = 8; i < 16; i++ ) {
28051da177e4SLinus Torvalds 		len += sprintf(page+len, "%c", scsi_inq[i]);
28061da177e4SLinus Torvalds 	}
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	len += sprintf(page+len, "  Model: ");
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	for( i = 16; i < 32; i++ ) {
28111da177e4SLinus Torvalds 		len += sprintf(page+len, "%c", scsi_inq[i]);
28121da177e4SLinus Torvalds 	}
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 	len += sprintf(page+len, "  Rev: ");
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 	for( i = 32; i < 36; i++ ) {
28171da177e4SLinus Torvalds 		len += sprintf(page+len, "%c", scsi_inq[i]);
28181da177e4SLinus Torvalds 	}
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 	len += sprintf(page+len, "\n");
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 	i = scsi_inq[0] & 0x1f;
28231da177e4SLinus Torvalds 
28244ff36718SMatthew Wilcox 	len += sprintf(page+len, "  Type:   %s ", scsi_device_type(i));
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds 	len += sprintf(page+len,
28271da177e4SLinus Torvalds 	"                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds 	if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
28301da177e4SLinus Torvalds 		len += sprintf(page+len, " CCS\n");
28311da177e4SLinus Torvalds 	else
28321da177e4SLinus Torvalds 		len += sprintf(page+len, "\n");
28331da177e4SLinus Torvalds 
28341da177e4SLinus Torvalds 	return len;
28351da177e4SLinus Torvalds }
28361da177e4SLinus Torvalds 
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds /**
28391da177e4SLinus Torvalds  * proc_rdrv_10()
28401da177e4SLinus Torvalds  * @page - buffer to write the data in
28411da177e4SLinus Torvalds  * @start - where the actual data has been written in page
28421da177e4SLinus Torvalds  * @offset - same meaning as the read system call
28431da177e4SLinus Torvalds  * @count - same meaning as the read system call
28441da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
28451da177e4SLinus Torvalds  * @data - pointer to our soft state
28461da177e4SLinus Torvalds  *
28471da177e4SLinus Torvalds  * Display real time information about the logical drives 0 through 9.
28481da177e4SLinus Torvalds  */
28491da177e4SLinus Torvalds static int
28501da177e4SLinus Torvalds proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
28511da177e4SLinus Torvalds 		void *data)
28521da177e4SLinus Torvalds {
28531da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds 	*eof = 1;
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	return (proc_rdrv(adapter, page, 0, 9));
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds /**
28621da177e4SLinus Torvalds  * proc_rdrv_20()
28631da177e4SLinus Torvalds  * @page - buffer to write the data in
28641da177e4SLinus Torvalds  * @start - where the actual data has been written in page
28651da177e4SLinus Torvalds  * @offset - same meaning as the read system call
28661da177e4SLinus Torvalds  * @count - same meaning as the read system call
28671da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
28681da177e4SLinus Torvalds  * @data - pointer to our soft state
28691da177e4SLinus Torvalds  *
28701da177e4SLinus Torvalds  * Display real time information about the logical drives 0 through 9.
28711da177e4SLinus Torvalds  */
28721da177e4SLinus Torvalds static int
28731da177e4SLinus Torvalds proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
28741da177e4SLinus Torvalds 		void *data)
28751da177e4SLinus Torvalds {
28761da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	*eof = 1;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 	return (proc_rdrv(adapter, page, 10, 19));
28811da177e4SLinus Torvalds }
28821da177e4SLinus Torvalds 
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds /**
28851da177e4SLinus Torvalds  * proc_rdrv_30()
28861da177e4SLinus Torvalds  * @page - buffer to write the data in
28871da177e4SLinus Torvalds  * @start - where the actual data has been written in page
28881da177e4SLinus Torvalds  * @offset - same meaning as the read system call
28891da177e4SLinus Torvalds  * @count - same meaning as the read system call
28901da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
28911da177e4SLinus Torvalds  * @data - pointer to our soft state
28921da177e4SLinus Torvalds  *
28931da177e4SLinus Torvalds  * Display real time information about the logical drives 0 through 9.
28941da177e4SLinus Torvalds  */
28951da177e4SLinus Torvalds static int
28961da177e4SLinus Torvalds proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
28971da177e4SLinus Torvalds 		void *data)
28981da177e4SLinus Torvalds {
28991da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	*eof = 1;
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	return (proc_rdrv(adapter, page, 20, 29));
29041da177e4SLinus Torvalds }
29051da177e4SLinus Torvalds 
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds /**
29081da177e4SLinus Torvalds  * proc_rdrv_40()
29091da177e4SLinus Torvalds  * @page - buffer to write the data in
29101da177e4SLinus Torvalds  * @start - where the actual data has been written in page
29111da177e4SLinus Torvalds  * @offset - same meaning as the read system call
29121da177e4SLinus Torvalds  * @count - same meaning as the read system call
29131da177e4SLinus Torvalds  * @eof - set if no more data needs to be returned
29141da177e4SLinus Torvalds  * @data - pointer to our soft state
29151da177e4SLinus Torvalds  *
29161da177e4SLinus Torvalds  * Display real time information about the logical drives 0 through 9.
29171da177e4SLinus Torvalds  */
29181da177e4SLinus Torvalds static int
29191da177e4SLinus Torvalds proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
29201da177e4SLinus Torvalds 		void *data)
29211da177e4SLinus Torvalds {
29221da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)data;
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 	*eof = 1;
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 	return (proc_rdrv(adapter, page, 30, 39));
29271da177e4SLinus Torvalds }
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds /**
29311da177e4SLinus Torvalds  * proc_rdrv()
29321da177e4SLinus Torvalds  * @page - buffer to write the data in
29331da177e4SLinus Torvalds  * @adapter - pointer to our soft state
29341da177e4SLinus Torvalds  * @start - starting logical drive to display
29351da177e4SLinus Torvalds  * @end - ending logical drive to display
29361da177e4SLinus Torvalds  *
29371da177e4SLinus Torvalds  * We do not print the inquiry information since its already available through
29381da177e4SLinus Torvalds  * /proc/scsi/scsi interface
29391da177e4SLinus Torvalds  */
29401da177e4SLinus Torvalds static int
29411da177e4SLinus Torvalds proc_rdrv(adapter_t *adapter, char *page, int start, int end )
29421da177e4SLinus Torvalds {
29431da177e4SLinus Torvalds 	dma_addr_t	dma_handle;
29441da177e4SLinus Torvalds 	logdrv_param	*lparam;
29451da177e4SLinus Torvalds 	megacmd_t	mc;
29461da177e4SLinus Torvalds 	char		*disk_array;
29471da177e4SLinus Torvalds 	dma_addr_t	disk_array_dma_handle;
29481da177e4SLinus Torvalds 	caddr_t		inquiry;
29491da177e4SLinus Torvalds 	struct pci_dev	*pdev;
29501da177e4SLinus Torvalds 	u8	*rdrv_state;
29511da177e4SLinus Torvalds 	int	num_ldrv;
29521da177e4SLinus Torvalds 	u32	array_sz;
29531da177e4SLinus Torvalds 	int	len = 0;
29541da177e4SLinus Torvalds 	int	i;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	if( make_local_pdev(adapter, &pdev) != 0 ) {
29571da177e4SLinus Torvalds 		return len;
29581da177e4SLinus Torvalds 	}
29591da177e4SLinus Torvalds 
29601da177e4SLinus Torvalds 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
29611da177e4SLinus Torvalds 		free_local_pdev(pdev);
29621da177e4SLinus Torvalds 		return len;
29631da177e4SLinus Torvalds 	}
29641da177e4SLinus Torvalds 
29651da177e4SLinus Torvalds 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
29661da177e4SLinus Torvalds 
29671da177e4SLinus Torvalds 		len = sprintf(page, "Adapter inquiry failed.\n");
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds 		mega_free_inquiry(inquiry, dma_handle, pdev);
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 		free_local_pdev(pdev);
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 		return len;
29761da177e4SLinus Torvalds 	}
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	memset(&mc, 0, sizeof(megacmd_t));
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
29811da177e4SLinus Torvalds 		array_sz = sizeof(disk_array_40ld);
29821da177e4SLinus Torvalds 
29831da177e4SLinus Torvalds 		rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds 		num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
29861da177e4SLinus Torvalds 	}
29871da177e4SLinus Torvalds 	else {
29881da177e4SLinus Torvalds 		array_sz = sizeof(disk_array_8ld);
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 		rdrv_state = ((mraid_ext_inquiry *)inquiry)->
29911da177e4SLinus Torvalds 			raid_inq.logdrv_info.ldrv_state;
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds 		num_ldrv = ((mraid_ext_inquiry *)inquiry)->
29941da177e4SLinus Torvalds 			raid_inq.logdrv_info.num_ldrv;
29951da177e4SLinus Torvalds 	}
29961da177e4SLinus Torvalds 
29971da177e4SLinus Torvalds 	disk_array = pci_alloc_consistent(pdev, array_sz,
29981da177e4SLinus Torvalds 			&disk_array_dma_handle);
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	if( disk_array == NULL ) {
30011da177e4SLinus Torvalds 		len = sprintf(page, "memory not available.\n");
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 		mega_free_inquiry(inquiry, dma_handle, pdev);
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds 		free_local_pdev(pdev);
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 		return len;
30081da177e4SLinus Torvalds 	}
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	mc.xferaddr = (u32)disk_array_dma_handle;
30111da177e4SLinus Torvalds 
30121da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
30131da177e4SLinus Torvalds 		mc.cmd = FC_NEW_CONFIG;
30141da177e4SLinus Torvalds 		mc.opcode = OP_DCMD_READ_CONFIG;
30151da177e4SLinus Torvalds 
3016cb0258a2SChristoph Hellwig 		if( mega_internal_command(adapter, &mc, NULL) ) {
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 			len = sprintf(page, "40LD read config failed.\n");
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 			mega_free_inquiry(inquiry, dma_handle, pdev);
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 			pci_free_consistent(pdev, array_sz, disk_array,
30231da177e4SLinus Torvalds 					disk_array_dma_handle);
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds 			free_local_pdev(pdev);
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 			return len;
30281da177e4SLinus Torvalds 		}
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	}
30311da177e4SLinus Torvalds 	else {
30321da177e4SLinus Torvalds 		mc.cmd = NEW_READ_CONFIG_8LD;
30331da177e4SLinus Torvalds 
3034cb0258a2SChristoph Hellwig 		if( mega_internal_command(adapter, &mc, NULL) ) {
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 			mc.cmd = READ_CONFIG_8LD;
30371da177e4SLinus Torvalds 
3038cb0258a2SChristoph Hellwig 			if( mega_internal_command(adapter, &mc,
30391da177e4SLinus Torvalds 						NULL) ){
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 				len = sprintf(page,
30421da177e4SLinus Torvalds 					"8LD read config failed.\n");
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 				mega_free_inquiry(inquiry, dma_handle, pdev);
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds 				pci_free_consistent(pdev, array_sz,
30471da177e4SLinus Torvalds 						disk_array,
30481da177e4SLinus Torvalds 						disk_array_dma_handle);
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds 				free_local_pdev(pdev);
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 				return len;
30531da177e4SLinus Torvalds 			}
30541da177e4SLinus Torvalds 		}
30551da177e4SLinus Torvalds 	}
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds 	for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 		if( adapter->flag & BOARD_40LD ) {
30601da177e4SLinus Torvalds 			lparam =
30611da177e4SLinus Torvalds 			&((disk_array_40ld *)disk_array)->ldrv[i].lparam;
30621da177e4SLinus Torvalds 		}
30631da177e4SLinus Torvalds 		else {
30641da177e4SLinus Torvalds 			lparam =
30651da177e4SLinus Torvalds 			&((disk_array_8ld *)disk_array)->ldrv[i].lparam;
30661da177e4SLinus Torvalds 		}
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 		/*
30691da177e4SLinus Torvalds 		 * Check for overflow. We print less than 240 characters for
30701da177e4SLinus Torvalds 		 * information about each logical drive.
30711da177e4SLinus Torvalds 		 */
30721da177e4SLinus Torvalds 		if( (len + 240) >= PAGE_SIZE ) break;
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 		len += sprintf(page+len, "Logical drive:%2d:, ", i);
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 		switch( rdrv_state[i] & 0x0F ) {
30771da177e4SLinus Torvalds 		case RDRV_OFFLINE:
30781da177e4SLinus Torvalds 			len += sprintf(page+len, "state: offline");
30791da177e4SLinus Torvalds 			break;
30801da177e4SLinus Torvalds 
30811da177e4SLinus Torvalds 		case RDRV_DEGRADED:
30821da177e4SLinus Torvalds 			len += sprintf(page+len, "state: degraded");
30831da177e4SLinus Torvalds 			break;
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds 		case RDRV_OPTIMAL:
30861da177e4SLinus Torvalds 			len += sprintf(page+len, "state: optimal");
30871da177e4SLinus Torvalds 			break;
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 		case RDRV_DELETED:
30901da177e4SLinus Torvalds 			len += sprintf(page+len, "state: deleted");
30911da177e4SLinus Torvalds 			break;
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds 		default:
30941da177e4SLinus Torvalds 			len += sprintf(page+len, "state: unknown");
30951da177e4SLinus Torvalds 			break;
30961da177e4SLinus Torvalds 		}
30971da177e4SLinus Torvalds 
30981da177e4SLinus Torvalds 		/*
30991da177e4SLinus Torvalds 		 * Check if check consistency or initialization is going on
31001da177e4SLinus Torvalds 		 * for this logical drive.
31011da177e4SLinus Torvalds 		 */
31021da177e4SLinus Torvalds 		if( (rdrv_state[i] & 0xF0) == 0x20 ) {
31031da177e4SLinus Torvalds 			len += sprintf(page+len,
31041da177e4SLinus Torvalds 					", check-consistency in progress");
31051da177e4SLinus Torvalds 		}
31061da177e4SLinus Torvalds 		else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
31071da177e4SLinus Torvalds 			len += sprintf(page+len,
31081da177e4SLinus Torvalds 					", initialization in progress");
31091da177e4SLinus Torvalds 		}
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds 		len += sprintf(page+len, "\n");
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 		len += sprintf(page+len, "Span depth:%3d, ",
31141da177e4SLinus Torvalds 				lparam->span_depth);
31151da177e4SLinus Torvalds 
31161da177e4SLinus Torvalds 		len += sprintf(page+len, "RAID level:%3d, ",
31171da177e4SLinus Torvalds 				lparam->level);
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds 		len += sprintf(page+len, "Stripe size:%3d, ",
31201da177e4SLinus Torvalds 				lparam->stripe_sz ? lparam->stripe_sz/2: 128);
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds 		len += sprintf(page+len, "Row size:%3d\n",
31231da177e4SLinus Torvalds 				lparam->row_size);
31241da177e4SLinus Torvalds 
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds 		len += sprintf(page+len, "Read Policy: ");
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 		switch(lparam->read_ahead) {
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 		case NO_READ_AHEAD:
31311da177e4SLinus Torvalds 			len += sprintf(page+len, "No read ahead, ");
31321da177e4SLinus Torvalds 			break;
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 		case READ_AHEAD:
31351da177e4SLinus Torvalds 			len += sprintf(page+len, "Read ahead, ");
31361da177e4SLinus Torvalds 			break;
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 		case ADAP_READ_AHEAD:
31391da177e4SLinus Torvalds 			len += sprintf(page+len, "Adaptive, ");
31401da177e4SLinus Torvalds 			break;
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds 		}
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds 		len += sprintf(page+len, "Write Policy: ");
31451da177e4SLinus Torvalds 
31461da177e4SLinus Torvalds 		switch(lparam->write_mode) {
31471da177e4SLinus Torvalds 
31481da177e4SLinus Torvalds 		case WRMODE_WRITE_THRU:
31491da177e4SLinus Torvalds 			len += sprintf(page+len, "Write thru, ");
31501da177e4SLinus Torvalds 			break;
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 		case WRMODE_WRITE_BACK:
31531da177e4SLinus Torvalds 			len += sprintf(page+len, "Write back, ");
31541da177e4SLinus Torvalds 			break;
31551da177e4SLinus Torvalds 		}
31561da177e4SLinus Torvalds 
31571da177e4SLinus Torvalds 		len += sprintf(page+len, "Cache Policy: ");
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds 		switch(lparam->direct_io) {
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 		case CACHED_IO:
31621da177e4SLinus Torvalds 			len += sprintf(page+len, "Cached IO\n\n");
31631da177e4SLinus Torvalds 			break;
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 		case DIRECT_IO:
31661da177e4SLinus Torvalds 			len += sprintf(page+len, "Direct IO\n\n");
31671da177e4SLinus Torvalds 			break;
31681da177e4SLinus Torvalds 		}
31691da177e4SLinus Torvalds 	}
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds 	mega_free_inquiry(inquiry, dma_handle, pdev);
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 	pci_free_consistent(pdev, array_sz, disk_array,
31741da177e4SLinus Torvalds 			disk_array_dma_handle);
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds 	free_local_pdev(pdev);
31771da177e4SLinus Torvalds 
31781da177e4SLinus Torvalds 	return len;
31791da177e4SLinus Torvalds }
31801da177e4SLinus Torvalds 
31811da177e4SLinus Torvalds #endif
31821da177e4SLinus Torvalds 
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds /**
31851da177e4SLinus Torvalds  * megaraid_biosparam()
31861da177e4SLinus Torvalds  *
31871da177e4SLinus Torvalds  * Return the disk geometry for a particular disk
31881da177e4SLinus Torvalds  */
31891da177e4SLinus Torvalds static int
31901da177e4SLinus Torvalds megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
31911da177e4SLinus Torvalds 		    sector_t capacity, int geom[])
31921da177e4SLinus Torvalds {
31931da177e4SLinus Torvalds 	adapter_t	*adapter;
31941da177e4SLinus Torvalds 	unsigned char	*bh;
31951da177e4SLinus Torvalds 	int	heads;
31961da177e4SLinus Torvalds 	int	sectors;
31971da177e4SLinus Torvalds 	int	cylinders;
31981da177e4SLinus Torvalds 	int	rval;
31991da177e4SLinus Torvalds 
32001da177e4SLinus Torvalds 	/* Get pointer to host config structure */
32011da177e4SLinus Torvalds 	adapter = (adapter_t *)sdev->host->hostdata;
32021da177e4SLinus Torvalds 
32031da177e4SLinus Torvalds 	if (IS_RAID_CH(adapter, sdev->channel)) {
32041da177e4SLinus Torvalds 			/* Default heads (64) & sectors (32) */
32051da177e4SLinus Torvalds 			heads = 64;
32061da177e4SLinus Torvalds 			sectors = 32;
32071da177e4SLinus Torvalds 			cylinders = (ulong)capacity / (heads * sectors);
32081da177e4SLinus Torvalds 
32091da177e4SLinus Torvalds 			/*
32101da177e4SLinus Torvalds 			 * Handle extended translation size for logical drives
32111da177e4SLinus Torvalds 			 * > 1Gb
32121da177e4SLinus Torvalds 			 */
32131da177e4SLinus Torvalds 			if ((ulong)capacity >= 0x200000) {
32141da177e4SLinus Torvalds 				heads = 255;
32151da177e4SLinus Torvalds 				sectors = 63;
32161da177e4SLinus Torvalds 				cylinders = (ulong)capacity / (heads * sectors);
32171da177e4SLinus Torvalds 			}
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds 			/* return result */
32201da177e4SLinus Torvalds 			geom[0] = heads;
32211da177e4SLinus Torvalds 			geom[1] = sectors;
32221da177e4SLinus Torvalds 			geom[2] = cylinders;
32231da177e4SLinus Torvalds 	}
32241da177e4SLinus Torvalds 	else {
32251da177e4SLinus Torvalds 		bh = scsi_bios_ptable(bdev);
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 		if( bh ) {
32281da177e4SLinus Torvalds 			rval = scsi_partsize(bh, capacity,
32291da177e4SLinus Torvalds 					    &geom[2], &geom[0], &geom[1]);
32301da177e4SLinus Torvalds 			kfree(bh);
32311da177e4SLinus Torvalds 			if( rval != -1 )
32321da177e4SLinus Torvalds 				return rval;
32331da177e4SLinus Torvalds 		}
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 		printk(KERN_INFO
32361da177e4SLinus Torvalds 		"megaraid: invalid partition on this disk on channel %d\n",
32371da177e4SLinus Torvalds 				sdev->channel);
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 		/* Default heads (64) & sectors (32) */
32401da177e4SLinus Torvalds 		heads = 64;
32411da177e4SLinus Torvalds 		sectors = 32;
32421da177e4SLinus Torvalds 		cylinders = (ulong)capacity / (heads * sectors);
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 		/* Handle extended translation size for logical drives > 1Gb */
32451da177e4SLinus Torvalds 		if ((ulong)capacity >= 0x200000) {
32461da177e4SLinus Torvalds 			heads = 255;
32471da177e4SLinus Torvalds 			sectors = 63;
32481da177e4SLinus Torvalds 			cylinders = (ulong)capacity / (heads * sectors);
32491da177e4SLinus Torvalds 		}
32501da177e4SLinus Torvalds 
32511da177e4SLinus Torvalds 		/* return result */
32521da177e4SLinus Torvalds 		geom[0] = heads;
32531da177e4SLinus Torvalds 		geom[1] = sectors;
32541da177e4SLinus Torvalds 		geom[2] = cylinders;
32551da177e4SLinus Torvalds 	}
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds 	return 0;
32581da177e4SLinus Torvalds }
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds /**
32611da177e4SLinus Torvalds  * mega_init_scb()
32621da177e4SLinus Torvalds  * @adapter - pointer to our soft state
32631da177e4SLinus Torvalds  *
32641da177e4SLinus Torvalds  * Allocate memory for the various pointers in the scb structures:
32651da177e4SLinus Torvalds  * scatter-gather list pointer, passthru and extended passthru structure
32661da177e4SLinus Torvalds  * pointers.
32671da177e4SLinus Torvalds  */
32681da177e4SLinus Torvalds static int
32691da177e4SLinus Torvalds mega_init_scb(adapter_t *adapter)
32701da177e4SLinus Torvalds {
32711da177e4SLinus Torvalds 	scb_t	*scb;
32721da177e4SLinus Torvalds 	int	i;
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	for( i = 0; i < adapter->max_cmds; i++ ) {
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds 		scb = &adapter->scb_list[i];
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 		scb->sgl64 = NULL;
32791da177e4SLinus Torvalds 		scb->sgl = NULL;
32801da177e4SLinus Torvalds 		scb->pthru = NULL;
32811da177e4SLinus Torvalds 		scb->epthru = NULL;
32821da177e4SLinus Torvalds 	}
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds 	for( i = 0; i < adapter->max_cmds; i++ ) {
32851da177e4SLinus Torvalds 
32861da177e4SLinus Torvalds 		scb = &adapter->scb_list[i];
32871da177e4SLinus Torvalds 
32881da177e4SLinus Torvalds 		scb->idx = i;
32891da177e4SLinus Torvalds 
32901da177e4SLinus Torvalds 		scb->sgl64 = pci_alloc_consistent(adapter->dev,
32911da177e4SLinus Torvalds 				sizeof(mega_sgl64) * adapter->sglen,
32921da177e4SLinus Torvalds 				&scb->sgl_dma_addr);
32931da177e4SLinus Torvalds 
32941da177e4SLinus Torvalds 		scb->sgl = (mega_sglist *)scb->sgl64;
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 		if( !scb->sgl ) {
32971da177e4SLinus Torvalds 			printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
32981da177e4SLinus Torvalds 			mega_free_sgl(adapter);
32991da177e4SLinus Torvalds 			return -1;
33001da177e4SLinus Torvalds 		}
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds 		scb->pthru = pci_alloc_consistent(adapter->dev,
33031da177e4SLinus Torvalds 				sizeof(mega_passthru),
33041da177e4SLinus Torvalds 				&scb->pthru_dma_addr);
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 		if( !scb->pthru ) {
33071da177e4SLinus Torvalds 			printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
33081da177e4SLinus Torvalds 			mega_free_sgl(adapter);
33091da177e4SLinus Torvalds 			return -1;
33101da177e4SLinus Torvalds 		}
33111da177e4SLinus Torvalds 
33121da177e4SLinus Torvalds 		scb->epthru = pci_alloc_consistent(adapter->dev,
33131da177e4SLinus Torvalds 				sizeof(mega_ext_passthru),
33141da177e4SLinus Torvalds 				&scb->epthru_dma_addr);
33151da177e4SLinus Torvalds 
33161da177e4SLinus Torvalds 		if( !scb->epthru ) {
33171da177e4SLinus Torvalds 			printk(KERN_WARNING
33181da177e4SLinus Torvalds 				"Can't allocate extended passthru.\n");
33191da177e4SLinus Torvalds 			mega_free_sgl(adapter);
33201da177e4SLinus Torvalds 			return -1;
33211da177e4SLinus Torvalds 		}
33221da177e4SLinus Torvalds 
33231da177e4SLinus Torvalds 
33241da177e4SLinus Torvalds 		scb->dma_type = MEGA_DMA_TYPE_NONE;
33251da177e4SLinus Torvalds 
33261da177e4SLinus Torvalds 		/*
33271da177e4SLinus Torvalds 		 * Link to free list
33281da177e4SLinus Torvalds 		 * lock not required since we are loading the driver, so no
33291da177e4SLinus Torvalds 		 * commands possible right now.
33301da177e4SLinus Torvalds 		 */
33311da177e4SLinus Torvalds 		scb->state = SCB_FREE;
33321da177e4SLinus Torvalds 		scb->cmd = NULL;
33331da177e4SLinus Torvalds 		list_add(&scb->list, &adapter->free_list);
33341da177e4SLinus Torvalds 	}
33351da177e4SLinus Torvalds 
33361da177e4SLinus Torvalds 	return 0;
33371da177e4SLinus Torvalds }
33381da177e4SLinus Torvalds 
33391da177e4SLinus Torvalds 
33401da177e4SLinus Torvalds /**
33411da177e4SLinus Torvalds  * megadev_open()
33421da177e4SLinus Torvalds  * @inode - unused
33431da177e4SLinus Torvalds  * @filep - unused
33441da177e4SLinus Torvalds  *
33451da177e4SLinus Torvalds  * Routines for the character/ioctl interface to the driver. Find out if this
33461da177e4SLinus Torvalds  * is a valid open. If yes, increment the module use count so that it cannot
33471da177e4SLinus Torvalds  * be unloaded.
33481da177e4SLinus Torvalds  */
33491da177e4SLinus Torvalds static int
33501da177e4SLinus Torvalds megadev_open (struct inode *inode, struct file *filep)
33511da177e4SLinus Torvalds {
33521da177e4SLinus Torvalds 	/*
33531da177e4SLinus Torvalds 	 * Only allow superuser to access private ioctl interface
33541da177e4SLinus Torvalds 	 */
33551da177e4SLinus Torvalds 	if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
33561da177e4SLinus Torvalds 
33571da177e4SLinus Torvalds 	return 0;
33581da177e4SLinus Torvalds }
33591da177e4SLinus Torvalds 
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds /**
33621da177e4SLinus Torvalds  * megadev_ioctl()
33631da177e4SLinus Torvalds  * @inode - Our device inode
33641da177e4SLinus Torvalds  * @filep - unused
33651da177e4SLinus Torvalds  * @cmd - ioctl command
33661da177e4SLinus Torvalds  * @arg - user buffer
33671da177e4SLinus Torvalds  *
33681da177e4SLinus Torvalds  * ioctl entry point for our private ioctl interface. We move the data in from
33691da177e4SLinus Torvalds  * the user space, prepare the command (if necessary, convert the old MIMD
33701da177e4SLinus Torvalds  * ioctl to new ioctl command), and issue a synchronous command to the
33711da177e4SLinus Torvalds  * controller.
33721da177e4SLinus Torvalds  */
33731da177e4SLinus Torvalds static int
33741da177e4SLinus Torvalds megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
33751da177e4SLinus Torvalds 		unsigned long arg)
33761da177e4SLinus Torvalds {
33771da177e4SLinus Torvalds 	adapter_t	*adapter;
33781da177e4SLinus Torvalds 	nitioctl_t	uioc;
33791da177e4SLinus Torvalds 	int		adapno;
33801da177e4SLinus Torvalds 	int		rval;
33811da177e4SLinus Torvalds 	mega_passthru	__user *upthru;	/* user address for passthru */
33821da177e4SLinus Torvalds 	mega_passthru	*pthru;		/* copy user passthru here */
33831da177e4SLinus Torvalds 	dma_addr_t	pthru_dma_hndl;
33841da177e4SLinus Torvalds 	void		*data = NULL;	/* data to be transferred */
33851da177e4SLinus Torvalds 	dma_addr_t	data_dma_hndl;	/* dma handle for data xfer area */
33861da177e4SLinus Torvalds 	megacmd_t	mc;
33871da177e4SLinus Torvalds 	megastat_t	__user *ustats;
33881da177e4SLinus Torvalds 	int		num_ldrv;
33891da177e4SLinus Torvalds 	u32		uxferaddr = 0;
33901da177e4SLinus Torvalds 	struct pci_dev	*pdev;
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 	ustats = NULL; /* avoid compilation warnings */
33931da177e4SLinus Torvalds 	num_ldrv = 0;
33941da177e4SLinus Torvalds 
33951da177e4SLinus Torvalds 	/*
33961da177e4SLinus Torvalds 	 * Make sure only USCSICMD are issued through this interface.
33971da177e4SLinus Torvalds 	 * MIMD application would still fire different command.
33981da177e4SLinus Torvalds 	 */
33991da177e4SLinus Torvalds 	if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
34001da177e4SLinus Torvalds 		return -EINVAL;
34011da177e4SLinus Torvalds 	}
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	/*
34041da177e4SLinus Torvalds 	 * Check and convert a possible MIMD command to NIT command.
34051da177e4SLinus Torvalds 	 * mega_m_to_n() copies the data from the user space, so we do not
34061da177e4SLinus Torvalds 	 * have to do it here.
34071da177e4SLinus Torvalds 	 * NOTE: We will need some user address to copyout the data, therefore
34081da177e4SLinus Torvalds 	 * the inteface layer will also provide us with the required user
34091da177e4SLinus Torvalds 	 * addresses.
34101da177e4SLinus Torvalds 	 */
34111da177e4SLinus Torvalds 	memset(&uioc, 0, sizeof(nitioctl_t));
34121da177e4SLinus Torvalds 	if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
34131da177e4SLinus Torvalds 		return rval;
34141da177e4SLinus Torvalds 
34151da177e4SLinus Torvalds 
34161da177e4SLinus Torvalds 	switch( uioc.opcode ) {
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds 	case GET_DRIVER_VER:
34191da177e4SLinus Torvalds 		if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
34201da177e4SLinus Torvalds 			return (-EFAULT);
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 		break;
34231da177e4SLinus Torvalds 
34241da177e4SLinus Torvalds 	case GET_N_ADAP:
34251da177e4SLinus Torvalds 		if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
34261da177e4SLinus Torvalds 			return (-EFAULT);
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds 		/*
34291da177e4SLinus Torvalds 		 * Shucks. MIMD interface returns a positive value for number
34301da177e4SLinus Torvalds 		 * of adapters. TODO: Change it to return 0 when there is no
34311da177e4SLinus Torvalds 		 * applicatio using mimd interface.
34321da177e4SLinus Torvalds 		 */
34331da177e4SLinus Torvalds 		return hba_count;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 	case GET_ADAP_INFO:
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds 		/*
34381da177e4SLinus Torvalds 		 * Which adapter
34391da177e4SLinus Torvalds 		 */
34401da177e4SLinus Torvalds 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
34411da177e4SLinus Torvalds 			return (-ENODEV);
34421da177e4SLinus Torvalds 
34431da177e4SLinus Torvalds 		if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
34441da177e4SLinus Torvalds 				sizeof(struct mcontroller)) )
34451da177e4SLinus Torvalds 			return (-EFAULT);
34461da177e4SLinus Torvalds 		break;
34471da177e4SLinus Torvalds 
34481da177e4SLinus Torvalds #if MEGA_HAVE_STATS
34491da177e4SLinus Torvalds 
34501da177e4SLinus Torvalds 	case GET_STATS:
34511da177e4SLinus Torvalds 		/*
34521da177e4SLinus Torvalds 		 * Which adapter
34531da177e4SLinus Torvalds 		 */
34541da177e4SLinus Torvalds 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
34551da177e4SLinus Torvalds 			return (-ENODEV);
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds 		adapter = hba_soft_state[adapno];
34581da177e4SLinus Torvalds 
34591da177e4SLinus Torvalds 		ustats = uioc.uioc_uaddr;
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds 		if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
34621da177e4SLinus Torvalds 			return (-EFAULT);
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds 		/*
34651da177e4SLinus Torvalds 		 * Check for the validity of the logical drive number
34661da177e4SLinus Torvalds 		 */
34671da177e4SLinus Torvalds 		if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
34681da177e4SLinus Torvalds 
34691da177e4SLinus Torvalds 		if( copy_to_user(ustats->nreads, adapter->nreads,
34701da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
34711da177e4SLinus Torvalds 			return -EFAULT;
34721da177e4SLinus Torvalds 
34731da177e4SLinus Torvalds 		if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
34741da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
34751da177e4SLinus Torvalds 			return -EFAULT;
34761da177e4SLinus Torvalds 
34771da177e4SLinus Torvalds 		if( copy_to_user(ustats->nwrites, adapter->nwrites,
34781da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
34791da177e4SLinus Torvalds 			return -EFAULT;
34801da177e4SLinus Torvalds 
34811da177e4SLinus Torvalds 		if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
34821da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
34831da177e4SLinus Torvalds 			return -EFAULT;
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 		if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
34861da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
34871da177e4SLinus Torvalds 			return -EFAULT;
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds 		if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
34901da177e4SLinus Torvalds 					num_ldrv*sizeof(u32)) )
34911da177e4SLinus Torvalds 			return -EFAULT;
34921da177e4SLinus Torvalds 
34931da177e4SLinus Torvalds 		return 0;
34941da177e4SLinus Torvalds 
34951da177e4SLinus Torvalds #endif
34961da177e4SLinus Torvalds 	case MBOX_CMD:
34971da177e4SLinus Torvalds 
34981da177e4SLinus Torvalds 		/*
34991da177e4SLinus Torvalds 		 * Which adapter
35001da177e4SLinus Torvalds 		 */
35011da177e4SLinus Torvalds 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
35021da177e4SLinus Torvalds 			return (-ENODEV);
35031da177e4SLinus Torvalds 
35041da177e4SLinus Torvalds 		adapter = hba_soft_state[adapno];
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds 		/*
35071da177e4SLinus Torvalds 		 * Deletion of logical drive is a special case. The adapter
35081da177e4SLinus Torvalds 		 * should be quiescent before this command is issued.
35091da177e4SLinus Torvalds 		 */
35101da177e4SLinus Torvalds 		if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
35111da177e4SLinus Torvalds 				uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
35121da177e4SLinus Torvalds 
35131da177e4SLinus Torvalds 			/*
35141da177e4SLinus Torvalds 			 * Do we support this feature
35151da177e4SLinus Torvalds 			 */
35161da177e4SLinus Torvalds 			if( !adapter->support_random_del ) {
35171da177e4SLinus Torvalds 				printk(KERN_WARNING "megaraid: logdrv ");
35181da177e4SLinus Torvalds 				printk("delete on non-supporting F/W.\n");
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds 				return (-EINVAL);
35211da177e4SLinus Torvalds 			}
35221da177e4SLinus Torvalds 
35231da177e4SLinus Torvalds 			rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
35241da177e4SLinus Torvalds 
35251da177e4SLinus Torvalds 			if( rval == 0 ) {
35261da177e4SLinus Torvalds 				memset(&mc, 0, sizeof(megacmd_t));
35271da177e4SLinus Torvalds 
35281da177e4SLinus Torvalds 				mc.status = rval;
35291da177e4SLinus Torvalds 
35301da177e4SLinus Torvalds 				rval = mega_n_to_m((void __user *)arg, &mc);
35311da177e4SLinus Torvalds 			}
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds 			return rval;
35341da177e4SLinus Torvalds 		}
35351da177e4SLinus Torvalds 		/*
35361da177e4SLinus Torvalds 		 * This interface only support the regular passthru commands.
35371da177e4SLinus Torvalds 		 * Reject extended passthru and 64-bit passthru
35381da177e4SLinus Torvalds 		 */
35391da177e4SLinus Torvalds 		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
35401da177e4SLinus Torvalds 			uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 			printk(KERN_WARNING "megaraid: rejected passthru.\n");
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 			return (-EINVAL);
35451da177e4SLinus Torvalds 		}
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 		/*
35481da177e4SLinus Torvalds 		 * For all internal commands, the buffer must be allocated in
35491da177e4SLinus Torvalds 		 * <4GB address range
35501da177e4SLinus Torvalds 		 */
35511da177e4SLinus Torvalds 		if( make_local_pdev(adapter, &pdev) != 0 )
35521da177e4SLinus Torvalds 			return -EIO;
35531da177e4SLinus Torvalds 
35541da177e4SLinus Torvalds 		/* Is it a passthru command or a DCMD */
35551da177e4SLinus Torvalds 		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
35561da177e4SLinus Torvalds 			/* Passthru commands */
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 			pthru = pci_alloc_consistent(pdev,
35591da177e4SLinus Torvalds 					sizeof(mega_passthru),
35601da177e4SLinus Torvalds 					&pthru_dma_hndl);
35611da177e4SLinus Torvalds 
35621da177e4SLinus Torvalds 			if( pthru == NULL ) {
35631da177e4SLinus Torvalds 				free_local_pdev(pdev);
35641da177e4SLinus Torvalds 				return (-ENOMEM);
35651da177e4SLinus Torvalds 			}
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds 			/*
35681da177e4SLinus Torvalds 			 * The user passthru structure
35691da177e4SLinus Torvalds 			 */
35701da177e4SLinus Torvalds 			upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
35711da177e4SLinus Torvalds 
35721da177e4SLinus Torvalds 			/*
35731da177e4SLinus Torvalds 			 * Copy in the user passthru here.
35741da177e4SLinus Torvalds 			 */
35751da177e4SLinus Torvalds 			if( copy_from_user(pthru, upthru,
35761da177e4SLinus Torvalds 						sizeof(mega_passthru)) ) {
35771da177e4SLinus Torvalds 
35781da177e4SLinus Torvalds 				pci_free_consistent(pdev,
35791da177e4SLinus Torvalds 						sizeof(mega_passthru), pthru,
35801da177e4SLinus Torvalds 						pthru_dma_hndl);
35811da177e4SLinus Torvalds 
35821da177e4SLinus Torvalds 				free_local_pdev(pdev);
35831da177e4SLinus Torvalds 
35841da177e4SLinus Torvalds 				return (-EFAULT);
35851da177e4SLinus Torvalds 			}
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds 			/*
35881da177e4SLinus Torvalds 			 * Is there a data transfer
35891da177e4SLinus Torvalds 			 */
35901da177e4SLinus Torvalds 			if( pthru->dataxferlen ) {
35911da177e4SLinus Torvalds 				data = pci_alloc_consistent(pdev,
35921da177e4SLinus Torvalds 						pthru->dataxferlen,
35931da177e4SLinus Torvalds 						&data_dma_hndl);
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 				if( data == NULL ) {
35961da177e4SLinus Torvalds 					pci_free_consistent(pdev,
35971da177e4SLinus Torvalds 							sizeof(mega_passthru),
35981da177e4SLinus Torvalds 							pthru,
35991da177e4SLinus Torvalds 							pthru_dma_hndl);
36001da177e4SLinus Torvalds 
36011da177e4SLinus Torvalds 					free_local_pdev(pdev);
36021da177e4SLinus Torvalds 
36031da177e4SLinus Torvalds 					return (-ENOMEM);
36041da177e4SLinus Torvalds 				}
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds 				/*
36071da177e4SLinus Torvalds 				 * Save the user address and point the kernel
36081da177e4SLinus Torvalds 				 * address at just allocated memory
36091da177e4SLinus Torvalds 				 */
36101da177e4SLinus Torvalds 				uxferaddr = pthru->dataxferaddr;
36111da177e4SLinus Torvalds 				pthru->dataxferaddr = data_dma_hndl;
36121da177e4SLinus Torvalds 			}
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 
36151da177e4SLinus Torvalds 			/*
36161da177e4SLinus Torvalds 			 * Is data coming down-stream
36171da177e4SLinus Torvalds 			 */
36181da177e4SLinus Torvalds 			if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
36191da177e4SLinus Torvalds 				/*
36201da177e4SLinus Torvalds 				 * Get the user data
36211da177e4SLinus Torvalds 				 */
36221da177e4SLinus Torvalds 				if( copy_from_user(data, (char __user *)uxferaddr,
36231da177e4SLinus Torvalds 							pthru->dataxferlen) ) {
36241da177e4SLinus Torvalds 					rval = (-EFAULT);
36251da177e4SLinus Torvalds 					goto freemem_and_return;
36261da177e4SLinus Torvalds 				}
36271da177e4SLinus Torvalds 			}
36281da177e4SLinus Torvalds 
36291da177e4SLinus Torvalds 			memset(&mc, 0, sizeof(megacmd_t));
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds 			mc.cmd = MEGA_MBOXCMD_PASSTHRU;
36321da177e4SLinus Torvalds 			mc.xferaddr = (u32)pthru_dma_hndl;
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds 			/*
36351da177e4SLinus Torvalds 			 * Issue the command
36361da177e4SLinus Torvalds 			 */
3637cb0258a2SChristoph Hellwig 			mega_internal_command(adapter, &mc, pthru);
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds 			rval = mega_n_to_m((void __user *)arg, &mc);
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds 			if( rval ) goto freemem_and_return;
36421da177e4SLinus Torvalds 
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds 			/*
36451da177e4SLinus Torvalds 			 * Is data going up-stream
36461da177e4SLinus Torvalds 			 */
36471da177e4SLinus Torvalds 			if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
36481da177e4SLinus Torvalds 				if( copy_to_user((char __user *)uxferaddr, data,
36491da177e4SLinus Torvalds 							pthru->dataxferlen) ) {
36501da177e4SLinus Torvalds 					rval = (-EFAULT);
36511da177e4SLinus Torvalds 				}
36521da177e4SLinus Torvalds 			}
36531da177e4SLinus Torvalds 
36541da177e4SLinus Torvalds 			/*
36551da177e4SLinus Torvalds 			 * Send the request sense data also, irrespective of
36561da177e4SLinus Torvalds 			 * whether the user has asked for it or not.
36571da177e4SLinus Torvalds 			 */
36582d2f8d59SJesper Juhl 			if (copy_to_user(upthru->reqsensearea,
36592d2f8d59SJesper Juhl 					pthru->reqsensearea, 14))
36602d2f8d59SJesper Juhl 				rval = -EFAULT;
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds freemem_and_return:
36631da177e4SLinus Torvalds 			if( pthru->dataxferlen ) {
36641da177e4SLinus Torvalds 				pci_free_consistent(pdev,
36651da177e4SLinus Torvalds 						pthru->dataxferlen, data,
36661da177e4SLinus Torvalds 						data_dma_hndl);
36671da177e4SLinus Torvalds 			}
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 			pci_free_consistent(pdev, sizeof(mega_passthru),
36701da177e4SLinus Torvalds 					pthru, pthru_dma_hndl);
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds 			free_local_pdev(pdev);
36731da177e4SLinus Torvalds 
36741da177e4SLinus Torvalds 			return rval;
36751da177e4SLinus Torvalds 		}
36761da177e4SLinus Torvalds 		else {
36771da177e4SLinus Torvalds 			/* DCMD commands */
36781da177e4SLinus Torvalds 
36791da177e4SLinus Torvalds 			/*
36801da177e4SLinus Torvalds 			 * Is there a data transfer
36811da177e4SLinus Torvalds 			 */
36821da177e4SLinus Torvalds 			if( uioc.xferlen ) {
36831da177e4SLinus Torvalds 				data = pci_alloc_consistent(pdev,
36841da177e4SLinus Torvalds 						uioc.xferlen, &data_dma_hndl);
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds 				if( data == NULL ) {
36871da177e4SLinus Torvalds 					free_local_pdev(pdev);
36881da177e4SLinus Torvalds 					return (-ENOMEM);
36891da177e4SLinus Torvalds 				}
36901da177e4SLinus Torvalds 
36911da177e4SLinus Torvalds 				uxferaddr = MBOX(uioc)->xferaddr;
36921da177e4SLinus Torvalds 			}
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds 			/*
36951da177e4SLinus Torvalds 			 * Is data coming down-stream
36961da177e4SLinus Torvalds 			 */
36971da177e4SLinus Torvalds 			if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
36981da177e4SLinus Torvalds 				/*
36991da177e4SLinus Torvalds 				 * Get the user data
37001da177e4SLinus Torvalds 				 */
37011da177e4SLinus Torvalds 				if( copy_from_user(data, (char __user *)uxferaddr,
37021da177e4SLinus Torvalds 							uioc.xferlen) ) {
37031da177e4SLinus Torvalds 
37041da177e4SLinus Torvalds 					pci_free_consistent(pdev,
37051da177e4SLinus Torvalds 							uioc.xferlen,
37061da177e4SLinus Torvalds 							data, data_dma_hndl);
37071da177e4SLinus Torvalds 
37081da177e4SLinus Torvalds 					free_local_pdev(pdev);
37091da177e4SLinus Torvalds 
37101da177e4SLinus Torvalds 					return (-EFAULT);
37111da177e4SLinus Torvalds 				}
37121da177e4SLinus Torvalds 			}
37131da177e4SLinus Torvalds 
37141da177e4SLinus Torvalds 			memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
37151da177e4SLinus Torvalds 
37161da177e4SLinus Torvalds 			mc.xferaddr = (u32)data_dma_hndl;
37171da177e4SLinus Torvalds 
37181da177e4SLinus Torvalds 			/*
37191da177e4SLinus Torvalds 			 * Issue the command
37201da177e4SLinus Torvalds 			 */
3721cb0258a2SChristoph Hellwig 			mega_internal_command(adapter, &mc, NULL);
37221da177e4SLinus Torvalds 
37231da177e4SLinus Torvalds 			rval = mega_n_to_m((void __user *)arg, &mc);
37241da177e4SLinus Torvalds 
37251da177e4SLinus Torvalds 			if( rval ) {
37261da177e4SLinus Torvalds 				if( uioc.xferlen ) {
37271da177e4SLinus Torvalds 					pci_free_consistent(pdev,
37281da177e4SLinus Torvalds 							uioc.xferlen, data,
37291da177e4SLinus Torvalds 							data_dma_hndl);
37301da177e4SLinus Torvalds 				}
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds 				free_local_pdev(pdev);
37331da177e4SLinus Torvalds 
37341da177e4SLinus Torvalds 				return rval;
37351da177e4SLinus Torvalds 			}
37361da177e4SLinus Torvalds 
37371da177e4SLinus Torvalds 			/*
37381da177e4SLinus Torvalds 			 * Is data going up-stream
37391da177e4SLinus Torvalds 			 */
37401da177e4SLinus Torvalds 			if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
37411da177e4SLinus Torvalds 				if( copy_to_user((char __user *)uxferaddr, data,
37421da177e4SLinus Torvalds 							uioc.xferlen) ) {
37431da177e4SLinus Torvalds 
37441da177e4SLinus Torvalds 					rval = (-EFAULT);
37451da177e4SLinus Torvalds 				}
37461da177e4SLinus Torvalds 			}
37471da177e4SLinus Torvalds 
37481da177e4SLinus Torvalds 			if( uioc.xferlen ) {
37491da177e4SLinus Torvalds 				pci_free_consistent(pdev,
37501da177e4SLinus Torvalds 						uioc.xferlen, data,
37511da177e4SLinus Torvalds 						data_dma_hndl);
37521da177e4SLinus Torvalds 			}
37531da177e4SLinus Torvalds 
37541da177e4SLinus Torvalds 			free_local_pdev(pdev);
37551da177e4SLinus Torvalds 
37561da177e4SLinus Torvalds 			return rval;
37571da177e4SLinus Torvalds 		}
37581da177e4SLinus Torvalds 
37591da177e4SLinus Torvalds 	default:
37601da177e4SLinus Torvalds 		return (-EINVAL);
37611da177e4SLinus Torvalds 	}
37621da177e4SLinus Torvalds 
37631da177e4SLinus Torvalds 	return 0;
37641da177e4SLinus Torvalds }
37651da177e4SLinus Torvalds 
37661da177e4SLinus Torvalds /**
37671da177e4SLinus Torvalds  * mega_m_to_n()
37681da177e4SLinus Torvalds  * @arg - user address
37691da177e4SLinus Torvalds  * @uioc - new ioctl structure
37701da177e4SLinus Torvalds  *
37711da177e4SLinus Torvalds  * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
37721da177e4SLinus Torvalds  * structure
37731da177e4SLinus Torvalds  *
37741da177e4SLinus Torvalds  * Converts the older mimd ioctl structure to newer NIT structure
37751da177e4SLinus Torvalds  */
37761da177e4SLinus Torvalds static int
37771da177e4SLinus Torvalds mega_m_to_n(void __user *arg, nitioctl_t *uioc)
37781da177e4SLinus Torvalds {
37791da177e4SLinus Torvalds 	struct uioctl_t	uioc_mimd;
37801da177e4SLinus Torvalds 	char	signature[8] = {0};
37811da177e4SLinus Torvalds 	u8	opcode;
37821da177e4SLinus Torvalds 	u8	subopcode;
37831da177e4SLinus Torvalds 
37841da177e4SLinus Torvalds 
37851da177e4SLinus Torvalds 	/*
37861da177e4SLinus Torvalds 	 * check is the application conforms to NIT. We do not have to do much
37871da177e4SLinus Torvalds 	 * in that case.
37881da177e4SLinus Torvalds 	 * We exploit the fact that the signature is stored in the very
37891da177e4SLinus Torvalds 	 * begining of the structure.
37901da177e4SLinus Torvalds 	 */
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds 	if( copy_from_user(signature, arg, 7) )
37931da177e4SLinus Torvalds 		return (-EFAULT);
37941da177e4SLinus Torvalds 
37951da177e4SLinus Torvalds 	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 		/*
37981da177e4SLinus Torvalds 		 * NOTE NOTE: The nit ioctl is still under flux because of
37991da177e4SLinus Torvalds 		 * change of mailbox definition, in HPE. No applications yet
38001da177e4SLinus Torvalds 		 * use this interface and let's not have applications use this
38011da177e4SLinus Torvalds 		 * interface till the new specifitions are in place.
38021da177e4SLinus Torvalds 		 */
38031da177e4SLinus Torvalds 		return -EINVAL;
38041da177e4SLinus Torvalds #if 0
38051da177e4SLinus Torvalds 		if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
38061da177e4SLinus Torvalds 			return (-EFAULT);
38071da177e4SLinus Torvalds 		return 0;
38081da177e4SLinus Torvalds #endif
38091da177e4SLinus Torvalds 	}
38101da177e4SLinus Torvalds 
38111da177e4SLinus Torvalds 	/*
38121da177e4SLinus Torvalds 	 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
38131da177e4SLinus Torvalds 	 *
38141da177e4SLinus Torvalds 	 * Get the user ioctl structure
38151da177e4SLinus Torvalds 	 */
38161da177e4SLinus Torvalds 	if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
38171da177e4SLinus Torvalds 		return (-EFAULT);
38181da177e4SLinus Torvalds 
38191da177e4SLinus Torvalds 
38201da177e4SLinus Torvalds 	/*
38211da177e4SLinus Torvalds 	 * Get the opcode and subopcode for the commands
38221da177e4SLinus Torvalds 	 */
38231da177e4SLinus Torvalds 	opcode = uioc_mimd.ui.fcs.opcode;
38241da177e4SLinus Torvalds 	subopcode = uioc_mimd.ui.fcs.subopcode;
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds 	switch (opcode) {
38271da177e4SLinus Torvalds 	case 0x82:
38281da177e4SLinus Torvalds 
38291da177e4SLinus Torvalds 		switch (subopcode) {
38301da177e4SLinus Torvalds 
38311da177e4SLinus Torvalds 		case MEGAIOC_QDRVRVER:	/* Query driver version */
38321da177e4SLinus Torvalds 			uioc->opcode = GET_DRIVER_VER;
38331da177e4SLinus Torvalds 			uioc->uioc_uaddr = uioc_mimd.data;
38341da177e4SLinus Torvalds 			break;
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 		case MEGAIOC_QNADAP:	/* Get # of adapters */
38371da177e4SLinus Torvalds 			uioc->opcode = GET_N_ADAP;
38381da177e4SLinus Torvalds 			uioc->uioc_uaddr = uioc_mimd.data;
38391da177e4SLinus Torvalds 			break;
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds 		case MEGAIOC_QADAPINFO:	/* Get adapter information */
38421da177e4SLinus Torvalds 			uioc->opcode = GET_ADAP_INFO;
38431da177e4SLinus Torvalds 			uioc->adapno = uioc_mimd.ui.fcs.adapno;
38441da177e4SLinus Torvalds 			uioc->uioc_uaddr = uioc_mimd.data;
38451da177e4SLinus Torvalds 			break;
38461da177e4SLinus Torvalds 
38471da177e4SLinus Torvalds 		default:
38481da177e4SLinus Torvalds 			return(-EINVAL);
38491da177e4SLinus Torvalds 		}
38501da177e4SLinus Torvalds 
38511da177e4SLinus Torvalds 		break;
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds 
38541da177e4SLinus Torvalds 	case 0x81:
38551da177e4SLinus Torvalds 
38561da177e4SLinus Torvalds 		uioc->opcode = MBOX_CMD;
38571da177e4SLinus Torvalds 		uioc->adapno = uioc_mimd.ui.fcs.adapno;
38581da177e4SLinus Torvalds 
38591da177e4SLinus Torvalds 		memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
38601da177e4SLinus Torvalds 
38611da177e4SLinus Torvalds 		uioc->xferlen = uioc_mimd.ui.fcs.length;
38621da177e4SLinus Torvalds 
38631da177e4SLinus Torvalds 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
38641da177e4SLinus Torvalds 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds 		break;
38671da177e4SLinus Torvalds 
38681da177e4SLinus Torvalds 	case 0x80:
38691da177e4SLinus Torvalds 
38701da177e4SLinus Torvalds 		uioc->opcode = MBOX_CMD;
38711da177e4SLinus Torvalds 		uioc->adapno = uioc_mimd.ui.fcs.adapno;
38721da177e4SLinus Torvalds 
38731da177e4SLinus Torvalds 		memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds 		/*
38761da177e4SLinus Torvalds 		 * Choose the xferlen bigger of input and output data
38771da177e4SLinus Torvalds 		 */
38781da177e4SLinus Torvalds 		uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
38791da177e4SLinus Torvalds 			uioc_mimd.outlen : uioc_mimd.inlen;
38801da177e4SLinus Torvalds 
38811da177e4SLinus Torvalds 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
38821da177e4SLinus Torvalds 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
38831da177e4SLinus Torvalds 
38841da177e4SLinus Torvalds 		break;
38851da177e4SLinus Torvalds 
38861da177e4SLinus Torvalds 	default:
38871da177e4SLinus Torvalds 		return (-EINVAL);
38881da177e4SLinus Torvalds 
38891da177e4SLinus Torvalds 	}
38901da177e4SLinus Torvalds 
38911da177e4SLinus Torvalds 	return 0;
38921da177e4SLinus Torvalds }
38931da177e4SLinus Torvalds 
38941da177e4SLinus Torvalds /*
38951da177e4SLinus Torvalds  * mega_n_to_m()
38961da177e4SLinus Torvalds  * @arg - user address
38971da177e4SLinus Torvalds  * @mc - mailbox command
38981da177e4SLinus Torvalds  *
38991da177e4SLinus Torvalds  * Updates the status information to the application, depending on application
39001da177e4SLinus Torvalds  * conforms to older mimd ioctl interface or newer NIT ioctl interface
39011da177e4SLinus Torvalds  */
39021da177e4SLinus Torvalds static int
39031da177e4SLinus Torvalds mega_n_to_m(void __user *arg, megacmd_t *mc)
39041da177e4SLinus Torvalds {
39051da177e4SLinus Torvalds 	nitioctl_t	__user *uiocp;
39061da177e4SLinus Torvalds 	megacmd_t	__user *umc;
39071da177e4SLinus Torvalds 	mega_passthru	__user *upthru;
39081da177e4SLinus Torvalds 	struct uioctl_t	__user *uioc_mimd;
39091da177e4SLinus Torvalds 	char	signature[8] = {0};
39101da177e4SLinus Torvalds 
39111da177e4SLinus Torvalds 	/*
39121da177e4SLinus Torvalds 	 * check is the application conforms to NIT.
39131da177e4SLinus Torvalds 	 */
39141da177e4SLinus Torvalds 	if( copy_from_user(signature, arg, 7) )
39151da177e4SLinus Torvalds 		return -EFAULT;
39161da177e4SLinus Torvalds 
39171da177e4SLinus Torvalds 	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
39181da177e4SLinus Torvalds 
39191da177e4SLinus Torvalds 		uiocp = arg;
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds 		if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
39221da177e4SLinus Torvalds 			return (-EFAULT);
39231da177e4SLinus Torvalds 
39241da177e4SLinus Torvalds 		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
39251da177e4SLinus Torvalds 
39261da177e4SLinus Torvalds 			umc = MBOX_P(uiocp);
39271da177e4SLinus Torvalds 
39281da177e4SLinus Torvalds 			if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
39291da177e4SLinus Torvalds 				return -EFAULT;
39301da177e4SLinus Torvalds 
39311da177e4SLinus Torvalds 			if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
39321da177e4SLinus Torvalds 				return (-EFAULT);
39331da177e4SLinus Torvalds 		}
39341da177e4SLinus Torvalds 	}
39351da177e4SLinus Torvalds 	else {
39361da177e4SLinus Torvalds 		uioc_mimd = arg;
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds 		if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
39391da177e4SLinus Torvalds 			return (-EFAULT);
39401da177e4SLinus Torvalds 
39411da177e4SLinus Torvalds 		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
39421da177e4SLinus Torvalds 
39431da177e4SLinus Torvalds 			umc = (megacmd_t __user *)uioc_mimd->mbox;
39441da177e4SLinus Torvalds 
39451da177e4SLinus Torvalds 			if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
39461da177e4SLinus Torvalds 				return (-EFAULT);
39471da177e4SLinus Torvalds 
39481da177e4SLinus Torvalds 			if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
39491da177e4SLinus Torvalds 				return (-EFAULT);
39501da177e4SLinus Torvalds 		}
39511da177e4SLinus Torvalds 	}
39521da177e4SLinus Torvalds 
39531da177e4SLinus Torvalds 	return 0;
39541da177e4SLinus Torvalds }
39551da177e4SLinus Torvalds 
39561da177e4SLinus Torvalds 
39571da177e4SLinus Torvalds /*
39581da177e4SLinus Torvalds  * MEGARAID 'FW' commands.
39591da177e4SLinus Torvalds  */
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds /**
39621da177e4SLinus Torvalds  * mega_is_bios_enabled()
39631da177e4SLinus Torvalds  * @adapter - pointer to our soft state
39641da177e4SLinus Torvalds  *
39651da177e4SLinus Torvalds  * issue command to find out if the BIOS is enabled for this controller
39661da177e4SLinus Torvalds  */
39671da177e4SLinus Torvalds static int
39681da177e4SLinus Torvalds mega_is_bios_enabled(adapter_t *adapter)
39691da177e4SLinus Torvalds {
39701da177e4SLinus Torvalds 	unsigned char	raw_mbox[sizeof(struct mbox_out)];
39711da177e4SLinus Torvalds 	mbox_t	*mbox;
39721da177e4SLinus Torvalds 	int	ret;
39731da177e4SLinus Torvalds 
39741da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
39771da177e4SLinus Torvalds 
39781da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
39791da177e4SLinus Torvalds 
39801da177e4SLinus Torvalds 	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
39811da177e4SLinus Torvalds 
39821da177e4SLinus Torvalds 	raw_mbox[0] = IS_BIOS_ENABLED;
39831da177e4SLinus Torvalds 	raw_mbox[2] = GET_BIOS;
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds 
39861da177e4SLinus Torvalds 	ret = issue_scb_block(adapter, raw_mbox);
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds 	return *(char *)adapter->mega_buffer;
39891da177e4SLinus Torvalds }
39901da177e4SLinus Torvalds 
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds /**
39931da177e4SLinus Torvalds  * mega_enum_raid_scsi()
39941da177e4SLinus Torvalds  * @adapter - pointer to our soft state
39951da177e4SLinus Torvalds  *
39961da177e4SLinus Torvalds  * Find out what channels are RAID/SCSI. This information is used to
39971da177e4SLinus Torvalds  * differentiate the virtual channels and physical channels and to support
39981da177e4SLinus Torvalds  * ROMB feature and non-disk devices.
39991da177e4SLinus Torvalds  */
40001da177e4SLinus Torvalds static void
40011da177e4SLinus Torvalds mega_enum_raid_scsi(adapter_t *adapter)
40021da177e4SLinus Torvalds {
40031da177e4SLinus Torvalds 	unsigned char raw_mbox[sizeof(struct mbox_out)];
40041da177e4SLinus Torvalds 	mbox_t *mbox;
40051da177e4SLinus Torvalds 	int i;
40061da177e4SLinus Torvalds 
40071da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
40081da177e4SLinus Torvalds 
40091da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 	/*
40121da177e4SLinus Torvalds 	 * issue command to find out what channels are raid/scsi
40131da177e4SLinus Torvalds 	 */
40141da177e4SLinus Torvalds 	raw_mbox[0] = CHNL_CLASS;
40151da177e4SLinus Torvalds 	raw_mbox[2] = GET_CHNL_CLASS;
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
40181da177e4SLinus Torvalds 
40191da177e4SLinus Torvalds 	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
40201da177e4SLinus Torvalds 
40211da177e4SLinus Torvalds 	/*
40221da177e4SLinus Torvalds 	 * Non-ROMB firmware fail this command, so all channels
40231da177e4SLinus Torvalds 	 * must be shown RAID
40241da177e4SLinus Torvalds 	 */
40251da177e4SLinus Torvalds 	adapter->mega_ch_class = 0xFF;
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds 	if(!issue_scb_block(adapter, raw_mbox)) {
40281da177e4SLinus Torvalds 		adapter->mega_ch_class = *((char *)adapter->mega_buffer);
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds 	}
40311da177e4SLinus Torvalds 
40321da177e4SLinus Torvalds 	for( i = 0; i < adapter->product_info.nchannels; i++ ) {
40331da177e4SLinus Torvalds 		if( (adapter->mega_ch_class >> i) & 0x01 ) {
40341da177e4SLinus Torvalds 			printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
40351da177e4SLinus Torvalds 					i);
40361da177e4SLinus Torvalds 		}
40371da177e4SLinus Torvalds 		else {
40381da177e4SLinus Torvalds 			printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
40391da177e4SLinus Torvalds 					i);
40401da177e4SLinus Torvalds 		}
40411da177e4SLinus Torvalds 	}
40421da177e4SLinus Torvalds 
40431da177e4SLinus Torvalds 	return;
40441da177e4SLinus Torvalds }
40451da177e4SLinus Torvalds 
40461da177e4SLinus Torvalds 
40471da177e4SLinus Torvalds /**
40481da177e4SLinus Torvalds  * mega_get_boot_drv()
40491da177e4SLinus Torvalds  * @adapter - pointer to our soft state
40501da177e4SLinus Torvalds  *
40511da177e4SLinus Torvalds  * Find out which device is the boot device. Note, any logical drive or any
40521da177e4SLinus Torvalds  * phyical device (e.g., a CDROM) can be designated as a boot device.
40531da177e4SLinus Torvalds  */
40541da177e4SLinus Torvalds static void
40551da177e4SLinus Torvalds mega_get_boot_drv(adapter_t *adapter)
40561da177e4SLinus Torvalds {
40571da177e4SLinus Torvalds 	struct private_bios_data	*prv_bios_data;
40581da177e4SLinus Torvalds 	unsigned char	raw_mbox[sizeof(struct mbox_out)];
40591da177e4SLinus Torvalds 	mbox_t	*mbox;
40601da177e4SLinus Torvalds 	u16	cksum = 0;
40611da177e4SLinus Torvalds 	u8	*cksum_p;
40621da177e4SLinus Torvalds 	u8	boot_pdrv;
40631da177e4SLinus Torvalds 	int	i;
40641da177e4SLinus Torvalds 
40651da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
40661da177e4SLinus Torvalds 
40671da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
40681da177e4SLinus Torvalds 
40691da177e4SLinus Torvalds 	raw_mbox[0] = BIOS_PVT_DATA;
40701da177e4SLinus Torvalds 	raw_mbox[2] = GET_BIOS_PVT_DATA;
40711da177e4SLinus Torvalds 
40721da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
40731da177e4SLinus Torvalds 
40741da177e4SLinus Torvalds 	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
40751da177e4SLinus Torvalds 
40761da177e4SLinus Torvalds 	adapter->boot_ldrv_enabled = 0;
40771da177e4SLinus Torvalds 	adapter->boot_ldrv = 0;
40781da177e4SLinus Torvalds 
40791da177e4SLinus Torvalds 	adapter->boot_pdrv_enabled = 0;
40801da177e4SLinus Torvalds 	adapter->boot_pdrv_ch = 0;
40811da177e4SLinus Torvalds 	adapter->boot_pdrv_tgt = 0;
40821da177e4SLinus Torvalds 
40831da177e4SLinus Torvalds 	if(issue_scb_block(adapter, raw_mbox) == 0) {
40841da177e4SLinus Torvalds 		prv_bios_data =
40851da177e4SLinus Torvalds 			(struct private_bios_data *)adapter->mega_buffer;
40861da177e4SLinus Torvalds 
40871da177e4SLinus Torvalds 		cksum = 0;
40881da177e4SLinus Torvalds 		cksum_p = (char *)prv_bios_data;
40891da177e4SLinus Torvalds 		for (i = 0; i < 14; i++ ) {
40901da177e4SLinus Torvalds 			cksum += (u16)(*cksum_p++);
40911da177e4SLinus Torvalds 		}
40921da177e4SLinus Torvalds 
40931da177e4SLinus Torvalds 		if (prv_bios_data->cksum == (u16)(0-cksum) ) {
40941da177e4SLinus Torvalds 
40951da177e4SLinus Torvalds 			/*
40961da177e4SLinus Torvalds 			 * If MSB is set, a physical drive is set as boot
40971da177e4SLinus Torvalds 			 * device
40981da177e4SLinus Torvalds 			 */
40991da177e4SLinus Torvalds 			if( prv_bios_data->boot_drv & 0x80 ) {
41001da177e4SLinus Torvalds 				adapter->boot_pdrv_enabled = 1;
41011da177e4SLinus Torvalds 				boot_pdrv = prv_bios_data->boot_drv & 0x7F;
41021da177e4SLinus Torvalds 				adapter->boot_pdrv_ch = boot_pdrv / 16;
41031da177e4SLinus Torvalds 				adapter->boot_pdrv_tgt = boot_pdrv % 16;
41041da177e4SLinus Torvalds 			}
41051da177e4SLinus Torvalds 			else {
41061da177e4SLinus Torvalds 				adapter->boot_ldrv_enabled = 1;
41071da177e4SLinus Torvalds 				adapter->boot_ldrv = prv_bios_data->boot_drv;
41081da177e4SLinus Torvalds 			}
41091da177e4SLinus Torvalds 		}
41101da177e4SLinus Torvalds 	}
41111da177e4SLinus Torvalds 
41121da177e4SLinus Torvalds }
41131da177e4SLinus Torvalds 
41141da177e4SLinus Torvalds /**
41151da177e4SLinus Torvalds  * mega_support_random_del()
41161da177e4SLinus Torvalds  * @adapter - pointer to our soft state
41171da177e4SLinus Torvalds  *
41181da177e4SLinus Torvalds  * Find out if this controller supports random deletion and addition of
41191da177e4SLinus Torvalds  * logical drives
41201da177e4SLinus Torvalds  */
41211da177e4SLinus Torvalds static int
41221da177e4SLinus Torvalds mega_support_random_del(adapter_t *adapter)
41231da177e4SLinus Torvalds {
41241da177e4SLinus Torvalds 	unsigned char raw_mbox[sizeof(struct mbox_out)];
41251da177e4SLinus Torvalds 	mbox_t *mbox;
41261da177e4SLinus Torvalds 	int rval;
41271da177e4SLinus Torvalds 
41281da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
41291da177e4SLinus Torvalds 
41301da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
41311da177e4SLinus Torvalds 
41321da177e4SLinus Torvalds 	/*
41331da177e4SLinus Torvalds 	 * issue command
41341da177e4SLinus Torvalds 	 */
41351da177e4SLinus Torvalds 	raw_mbox[0] = FC_DEL_LOGDRV;
41361da177e4SLinus Torvalds 	raw_mbox[2] = OP_SUP_DEL_LOGDRV;
41371da177e4SLinus Torvalds 
41381da177e4SLinus Torvalds 	rval = issue_scb_block(adapter, raw_mbox);
41391da177e4SLinus Torvalds 
41401da177e4SLinus Torvalds 	return !rval;
41411da177e4SLinus Torvalds }
41421da177e4SLinus Torvalds 
41431da177e4SLinus Torvalds 
41441da177e4SLinus Torvalds /**
41451da177e4SLinus Torvalds  * mega_support_ext_cdb()
41461da177e4SLinus Torvalds  * @adapter - pointer to our soft state
41471da177e4SLinus Torvalds  *
41481da177e4SLinus Torvalds  * Find out if this firmware support cdblen > 10
41491da177e4SLinus Torvalds  */
41501da177e4SLinus Torvalds static int
41511da177e4SLinus Torvalds mega_support_ext_cdb(adapter_t *adapter)
41521da177e4SLinus Torvalds {
41531da177e4SLinus Torvalds 	unsigned char raw_mbox[sizeof(struct mbox_out)];
41541da177e4SLinus Torvalds 	mbox_t *mbox;
41551da177e4SLinus Torvalds 	int rval;
41561da177e4SLinus Torvalds 
41571da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
41581da177e4SLinus Torvalds 
41591da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
41601da177e4SLinus Torvalds 	/*
41611da177e4SLinus Torvalds 	 * issue command to find out if controller supports extended CDBs.
41621da177e4SLinus Torvalds 	 */
41631da177e4SLinus Torvalds 	raw_mbox[0] = 0xA4;
41641da177e4SLinus Torvalds 	raw_mbox[2] = 0x16;
41651da177e4SLinus Torvalds 
41661da177e4SLinus Torvalds 	rval = issue_scb_block(adapter, raw_mbox);
41671da177e4SLinus Torvalds 
41681da177e4SLinus Torvalds 	return !rval;
41691da177e4SLinus Torvalds }
41701da177e4SLinus Torvalds 
41711da177e4SLinus Torvalds 
41721da177e4SLinus Torvalds /**
41731da177e4SLinus Torvalds  * mega_del_logdrv()
41741da177e4SLinus Torvalds  * @adapter - pointer to our soft state
41751da177e4SLinus Torvalds  * @logdrv - logical drive to be deleted
41761da177e4SLinus Torvalds  *
41771da177e4SLinus Torvalds  * Delete the specified logical drive. It is the responsibility of the user
41781da177e4SLinus Torvalds  * app to let the OS know about this operation.
41791da177e4SLinus Torvalds  */
41801da177e4SLinus Torvalds static int
41811da177e4SLinus Torvalds mega_del_logdrv(adapter_t *adapter, int logdrv)
41821da177e4SLinus Torvalds {
41831da177e4SLinus Torvalds 	unsigned long flags;
41841da177e4SLinus Torvalds 	scb_t *scb;
41851da177e4SLinus Torvalds 	int rval;
41861da177e4SLinus Torvalds 
41871da177e4SLinus Torvalds 	/*
41881da177e4SLinus Torvalds 	 * Stop sending commands to the controller, queue them internally.
41891da177e4SLinus Torvalds 	 * When deletion is complete, ISR will flush the queue.
41901da177e4SLinus Torvalds 	 */
41911da177e4SLinus Torvalds 	atomic_set(&adapter->quiescent, 1);
41921da177e4SLinus Torvalds 
41931da177e4SLinus Torvalds 	/*
41941da177e4SLinus Torvalds 	 * Wait till all the issued commands are complete and there are no
41951da177e4SLinus Torvalds 	 * commands in the pending queue
41961da177e4SLinus Torvalds 	 */
41971da177e4SLinus Torvalds 	while (atomic_read(&adapter->pend_cmds) > 0 ||
41981da177e4SLinus Torvalds 	       !list_empty(&adapter->pending_list))
41991da177e4SLinus Torvalds 		msleep(1000);	/* sleep for 1s */
42001da177e4SLinus Torvalds 
42011da177e4SLinus Torvalds 	rval = mega_do_del_logdrv(adapter, logdrv);
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds 	spin_lock_irqsave(&adapter->lock, flags);
42041da177e4SLinus Torvalds 
42051da177e4SLinus Torvalds 	/*
42061da177e4SLinus Torvalds 	 * If delete operation was successful, add 0x80 to the logical drive
42071da177e4SLinus Torvalds 	 * ids for commands in the pending queue.
42081da177e4SLinus Torvalds 	 */
42091da177e4SLinus Torvalds 	if (adapter->read_ldidmap) {
42101da177e4SLinus Torvalds 		struct list_head *pos;
42111da177e4SLinus Torvalds 		list_for_each(pos, &adapter->pending_list) {
42121da177e4SLinus Torvalds 			scb = list_entry(pos, scb_t, list);
42131da177e4SLinus Torvalds 			if (scb->pthru->logdrv < 0x80 )
42141da177e4SLinus Torvalds 				scb->pthru->logdrv += 0x80;
42151da177e4SLinus Torvalds 		}
42161da177e4SLinus Torvalds 	}
42171da177e4SLinus Torvalds 
42181da177e4SLinus Torvalds 	atomic_set(&adapter->quiescent, 0);
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds 	mega_runpendq(adapter);
42211da177e4SLinus Torvalds 
42221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&adapter->lock, flags);
42231da177e4SLinus Torvalds 
42241da177e4SLinus Torvalds 	return rval;
42251da177e4SLinus Torvalds }
42261da177e4SLinus Torvalds 
42271da177e4SLinus Torvalds 
42281da177e4SLinus Torvalds static int
42291da177e4SLinus Torvalds mega_do_del_logdrv(adapter_t *adapter, int logdrv)
42301da177e4SLinus Torvalds {
42311da177e4SLinus Torvalds 	megacmd_t	mc;
42321da177e4SLinus Torvalds 	int	rval;
42331da177e4SLinus Torvalds 
42341da177e4SLinus Torvalds 	memset( &mc, 0, sizeof(megacmd_t));
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds 	mc.cmd = FC_DEL_LOGDRV;
42371da177e4SLinus Torvalds 	mc.opcode = OP_DEL_LOGDRV;
42381da177e4SLinus Torvalds 	mc.subopcode = logdrv;
42391da177e4SLinus Torvalds 
4240cb0258a2SChristoph Hellwig 	rval = mega_internal_command(adapter, &mc, NULL);
42411da177e4SLinus Torvalds 
42421da177e4SLinus Torvalds 	/* log this event */
42431da177e4SLinus Torvalds 	if(rval) {
42441da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
42451da177e4SLinus Torvalds 		return rval;
42461da177e4SLinus Torvalds 	}
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds 	/*
42491da177e4SLinus Torvalds 	 * After deleting first logical drive, the logical drives must be
42501da177e4SLinus Torvalds 	 * addressed by adding 0x80 to the logical drive id.
42511da177e4SLinus Torvalds 	 */
42521da177e4SLinus Torvalds 	adapter->read_ldidmap = 1;
42531da177e4SLinus Torvalds 
42541da177e4SLinus Torvalds 	return rval;
42551da177e4SLinus Torvalds }
42561da177e4SLinus Torvalds 
42571da177e4SLinus Torvalds 
42581da177e4SLinus Torvalds /**
42591da177e4SLinus Torvalds  * mega_get_max_sgl()
42601da177e4SLinus Torvalds  * @adapter - pointer to our soft state
42611da177e4SLinus Torvalds  *
42621da177e4SLinus Torvalds  * Find out the maximum number of scatter-gather elements supported by this
42631da177e4SLinus Torvalds  * version of the firmware
42641da177e4SLinus Torvalds  */
42651da177e4SLinus Torvalds static void
42661da177e4SLinus Torvalds mega_get_max_sgl(adapter_t *adapter)
42671da177e4SLinus Torvalds {
42681da177e4SLinus Torvalds 	unsigned char	raw_mbox[sizeof(struct mbox_out)];
42691da177e4SLinus Torvalds 	mbox_t	*mbox;
42701da177e4SLinus Torvalds 
42711da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
42721da177e4SLinus Torvalds 
42731da177e4SLinus Torvalds 	memset(mbox, 0, sizeof(raw_mbox));
42741da177e4SLinus Torvalds 
42751da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
42761da177e4SLinus Torvalds 
42771da177e4SLinus Torvalds 	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
42781da177e4SLinus Torvalds 
42791da177e4SLinus Torvalds 	raw_mbox[0] = MAIN_MISC_OPCODE;
42801da177e4SLinus Torvalds 	raw_mbox[2] = GET_MAX_SG_SUPPORT;
42811da177e4SLinus Torvalds 
42821da177e4SLinus Torvalds 
42831da177e4SLinus Torvalds 	if( issue_scb_block(adapter, raw_mbox) ) {
42841da177e4SLinus Torvalds 		/*
42851da177e4SLinus Torvalds 		 * f/w does not support this command. Choose the default value
42861da177e4SLinus Torvalds 		 */
42871da177e4SLinus Torvalds 		adapter->sglen = MIN_SGLIST;
42881da177e4SLinus Torvalds 	}
42891da177e4SLinus Torvalds 	else {
42901da177e4SLinus Torvalds 		adapter->sglen = *((char *)adapter->mega_buffer);
42911da177e4SLinus Torvalds 
42921da177e4SLinus Torvalds 		/*
42931da177e4SLinus Torvalds 		 * Make sure this is not more than the resources we are
42941da177e4SLinus Torvalds 		 * planning to allocate
42951da177e4SLinus Torvalds 		 */
42961da177e4SLinus Torvalds 		if ( adapter->sglen > MAX_SGLIST )
42971da177e4SLinus Torvalds 			adapter->sglen = MAX_SGLIST;
42981da177e4SLinus Torvalds 	}
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds 	return;
43011da177e4SLinus Torvalds }
43021da177e4SLinus Torvalds 
43031da177e4SLinus Torvalds 
43041da177e4SLinus Torvalds /**
43051da177e4SLinus Torvalds  * mega_support_cluster()
43061da177e4SLinus Torvalds  * @adapter - pointer to our soft state
43071da177e4SLinus Torvalds  *
43081da177e4SLinus Torvalds  * Find out if this firmware support cluster calls.
43091da177e4SLinus Torvalds  */
43101da177e4SLinus Torvalds static int
43111da177e4SLinus Torvalds mega_support_cluster(adapter_t *adapter)
43121da177e4SLinus Torvalds {
43131da177e4SLinus Torvalds 	unsigned char	raw_mbox[sizeof(struct mbox_out)];
43141da177e4SLinus Torvalds 	mbox_t	*mbox;
43151da177e4SLinus Torvalds 
43161da177e4SLinus Torvalds 	mbox = (mbox_t *)raw_mbox;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds 	memset(mbox, 0, sizeof(raw_mbox));
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
43211da177e4SLinus Torvalds 
43221da177e4SLinus Torvalds 	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
43231da177e4SLinus Torvalds 
43241da177e4SLinus Torvalds 	/*
43251da177e4SLinus Torvalds 	 * Try to get the initiator id. This command will succeed iff the
43261da177e4SLinus Torvalds 	 * clustering is available on this HBA.
43271da177e4SLinus Torvalds 	 */
43281da177e4SLinus Torvalds 	raw_mbox[0] = MEGA_GET_TARGET_ID;
43291da177e4SLinus Torvalds 
43301da177e4SLinus Torvalds 	if( issue_scb_block(adapter, raw_mbox) == 0 ) {
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 		/*
43331da177e4SLinus Torvalds 		 * Cluster support available. Get the initiator target id.
43341da177e4SLinus Torvalds 		 * Tell our id to mid-layer too.
43351da177e4SLinus Torvalds 		 */
43361da177e4SLinus Torvalds 		adapter->this_id = *(u32 *)adapter->mega_buffer;
43371da177e4SLinus Torvalds 		adapter->host->this_id = adapter->this_id;
43381da177e4SLinus Torvalds 
43391da177e4SLinus Torvalds 		return 1;
43401da177e4SLinus Torvalds 	}
43411da177e4SLinus Torvalds 
43421da177e4SLinus Torvalds 	return 0;
43431da177e4SLinus Torvalds }
43441da177e4SLinus Torvalds 
43451da177e4SLinus Torvalds 
43461da177e4SLinus Torvalds /**
43471da177e4SLinus Torvalds  * mega_adapinq()
43481da177e4SLinus Torvalds  * @adapter - pointer to our soft state
43491da177e4SLinus Torvalds  * @dma_handle - DMA address of the buffer
43501da177e4SLinus Torvalds  *
43511da177e4SLinus Torvalds  * Issue internal comamnds while interrupts are available.
43521da177e4SLinus Torvalds  * We only issue direct mailbox commands from within the driver. ioctl()
43531da177e4SLinus Torvalds  * interface using these routines can issue passthru commands.
43541da177e4SLinus Torvalds  */
43551da177e4SLinus Torvalds static int
43561da177e4SLinus Torvalds mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
43571da177e4SLinus Torvalds {
43581da177e4SLinus Torvalds 	megacmd_t	mc;
43591da177e4SLinus Torvalds 
43601da177e4SLinus Torvalds 	memset(&mc, 0, sizeof(megacmd_t));
43611da177e4SLinus Torvalds 
43621da177e4SLinus Torvalds 	if( adapter->flag & BOARD_40LD ) {
43631da177e4SLinus Torvalds 		mc.cmd = FC_NEW_CONFIG;
43641da177e4SLinus Torvalds 		mc.opcode = NC_SUBOP_ENQUIRY3;
43651da177e4SLinus Torvalds 		mc.subopcode = ENQ3_GET_SOLICITED_FULL;
43661da177e4SLinus Torvalds 	}
43671da177e4SLinus Torvalds 	else {
43681da177e4SLinus Torvalds 		mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
43691da177e4SLinus Torvalds 	}
43701da177e4SLinus Torvalds 
43711da177e4SLinus Torvalds 	mc.xferaddr = (u32)dma_handle;
43721da177e4SLinus Torvalds 
4373cb0258a2SChristoph Hellwig 	if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
43741da177e4SLinus Torvalds 		return -1;
43751da177e4SLinus Torvalds 	}
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds 	return 0;
43781da177e4SLinus Torvalds }
43791da177e4SLinus Torvalds 
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds /** mega_internal_dev_inquiry()
43821da177e4SLinus Torvalds  * @adapter - pointer to our soft state
43831da177e4SLinus Torvalds  * @ch - channel for this device
43841da177e4SLinus Torvalds  * @tgt - ID of this device
43851da177e4SLinus Torvalds  * @buf_dma_handle - DMA address of the buffer
43861da177e4SLinus Torvalds  *
43871da177e4SLinus Torvalds  * Issue the scsi inquiry for the specified device.
43881da177e4SLinus Torvalds  */
43891da177e4SLinus Torvalds static int
43901da177e4SLinus Torvalds mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
43911da177e4SLinus Torvalds 		dma_addr_t buf_dma_handle)
43921da177e4SLinus Torvalds {
43931da177e4SLinus Torvalds 	mega_passthru	*pthru;
43941da177e4SLinus Torvalds 	dma_addr_t	pthru_dma_handle;
43951da177e4SLinus Torvalds 	megacmd_t	mc;
43961da177e4SLinus Torvalds 	int		rval;
43971da177e4SLinus Torvalds 	struct pci_dev	*pdev;
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds 
44001da177e4SLinus Torvalds 	/*
44011da177e4SLinus Torvalds 	 * For all internal commands, the buffer must be allocated in <4GB
44021da177e4SLinus Torvalds 	 * address range
44031da177e4SLinus Torvalds 	 */
44041da177e4SLinus Torvalds 	if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
44051da177e4SLinus Torvalds 
44061da177e4SLinus Torvalds 	pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
44071da177e4SLinus Torvalds 			&pthru_dma_handle);
44081da177e4SLinus Torvalds 
44091da177e4SLinus Torvalds 	if( pthru == NULL ) {
44101da177e4SLinus Torvalds 		free_local_pdev(pdev);
44111da177e4SLinus Torvalds 		return -1;
44121da177e4SLinus Torvalds 	}
44131da177e4SLinus Torvalds 
44141da177e4SLinus Torvalds 	pthru->timeout = 2;
44151da177e4SLinus Torvalds 	pthru->ars = 1;
44161da177e4SLinus Torvalds 	pthru->reqsenselen = 14;
44171da177e4SLinus Torvalds 	pthru->islogical = 0;
44181da177e4SLinus Torvalds 
44191da177e4SLinus Torvalds 	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
44201da177e4SLinus Torvalds 
44211da177e4SLinus Torvalds 	pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
44221da177e4SLinus Torvalds 
44231da177e4SLinus Torvalds 	pthru->cdblen = 6;
44241da177e4SLinus Torvalds 
44251da177e4SLinus Torvalds 	pthru->cdb[0] = INQUIRY;
44261da177e4SLinus Torvalds 	pthru->cdb[1] = 0;
44271da177e4SLinus Torvalds 	pthru->cdb[2] = 0;
44281da177e4SLinus Torvalds 	pthru->cdb[3] = 0;
44291da177e4SLinus Torvalds 	pthru->cdb[4] = 255;
44301da177e4SLinus Torvalds 	pthru->cdb[5] = 0;
44311da177e4SLinus Torvalds 
44321da177e4SLinus Torvalds 
44331da177e4SLinus Torvalds 	pthru->dataxferaddr = (u32)buf_dma_handle;
44341da177e4SLinus Torvalds 	pthru->dataxferlen = 256;
44351da177e4SLinus Torvalds 
44361da177e4SLinus Torvalds 	memset(&mc, 0, sizeof(megacmd_t));
44371da177e4SLinus Torvalds 
44381da177e4SLinus Torvalds 	mc.cmd = MEGA_MBOXCMD_PASSTHRU;
44391da177e4SLinus Torvalds 	mc.xferaddr = (u32)pthru_dma_handle;
44401da177e4SLinus Torvalds 
4441cb0258a2SChristoph Hellwig 	rval = mega_internal_command(adapter, &mc, pthru);
44421da177e4SLinus Torvalds 
44431da177e4SLinus Torvalds 	pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
44441da177e4SLinus Torvalds 			pthru_dma_handle);
44451da177e4SLinus Torvalds 
44461da177e4SLinus Torvalds 	free_local_pdev(pdev);
44471da177e4SLinus Torvalds 
44481da177e4SLinus Torvalds 	return rval;
44491da177e4SLinus Torvalds }
44501da177e4SLinus Torvalds 
44511da177e4SLinus Torvalds 
44521da177e4SLinus Torvalds /**
44531da177e4SLinus Torvalds  * mega_internal_command()
44541da177e4SLinus Torvalds  * @adapter - pointer to our soft state
44551da177e4SLinus Torvalds  * @mc - the mailbox command
44561da177e4SLinus Torvalds  * @pthru - Passthru structure for DCDB commands
44571da177e4SLinus Torvalds  *
44581da177e4SLinus Torvalds  * Issue the internal commands in interrupt mode.
44591da177e4SLinus Torvalds  * The last argument is the address of the passthru structure if the command
44601da177e4SLinus Torvalds  * to be fired is a passthru command
44611da177e4SLinus Torvalds  *
44621da177e4SLinus Torvalds  * lockscope specifies whether the caller has already acquired the lock. Of
44631da177e4SLinus Torvalds  * course, the caller must know which lock we are talking about.
44641da177e4SLinus Torvalds  *
44651da177e4SLinus Torvalds  * Note: parameter 'pthru' is null for non-passthru commands.
44661da177e4SLinus Torvalds  */
44671da177e4SLinus Torvalds static int
4468cb0258a2SChristoph Hellwig mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
44691da177e4SLinus Torvalds {
44701da177e4SLinus Torvalds 	Scsi_Cmnd	*scmd;
44711da177e4SLinus Torvalds 	struct	scsi_device *sdev;
44721da177e4SLinus Torvalds 	scb_t	*scb;
44731da177e4SLinus Torvalds 	int	rval;
44741da177e4SLinus Torvalds 
44751da177e4SLinus Torvalds 	/*
44761da177e4SLinus Torvalds 	 * The internal commands share one command id and hence are
44771da177e4SLinus Torvalds 	 * serialized. This is so because we want to reserve maximum number of
44781da177e4SLinus Torvalds 	 * available command ids for the I/O commands.
44791da177e4SLinus Torvalds 	 */
44800b950672SArjan van de Ven 	mutex_lock(&adapter->int_mtx);
44811da177e4SLinus Torvalds 
44821da177e4SLinus Torvalds 	scb = &adapter->int_scb;
44831da177e4SLinus Torvalds 	memset(scb, 0, sizeof(scb_t));
44841da177e4SLinus Torvalds 
44851da177e4SLinus Torvalds 	scmd = &adapter->int_scmd;
44861da177e4SLinus Torvalds 	memset(scmd, 0, sizeof(Scsi_Cmnd));
44871da177e4SLinus Torvalds 
44881da177e4SLinus Torvalds 	sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
44891da177e4SLinus Torvalds 	memset(sdev, 0, sizeof(struct scsi_device));
44901da177e4SLinus Torvalds 	scmd->device = sdev;
44911da177e4SLinus Torvalds 
44921da177e4SLinus Torvalds 	scmd->device->host = adapter->host;
44935d5ff44fSChristoph Hellwig 	scmd->request_buffer = (void *)scb;
44941da177e4SLinus Torvalds 	scmd->cmnd[0] = MEGA_INTERNAL_CMD;
44951da177e4SLinus Torvalds 
44961da177e4SLinus Torvalds 	scb->state |= SCB_ACTIVE;
44971da177e4SLinus Torvalds 	scb->cmd = scmd;
44981da177e4SLinus Torvalds 
44991da177e4SLinus Torvalds 	memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
45001da177e4SLinus Torvalds 
45011da177e4SLinus Torvalds 	/*
45021da177e4SLinus Torvalds 	 * Is it a passthru command
45031da177e4SLinus Torvalds 	 */
45041da177e4SLinus Torvalds 	if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
45051da177e4SLinus Torvalds 
45061da177e4SLinus Torvalds 		scb->pthru = pthru;
45071da177e4SLinus Torvalds 	}
45081da177e4SLinus Torvalds 
45091da177e4SLinus Torvalds 	scb->idx = CMDID_INT_CMDS;
45101da177e4SLinus Torvalds 
45111da177e4SLinus Torvalds 	megaraid_queue(scmd, mega_internal_done);
45121da177e4SLinus Torvalds 
45138d115f84SChristoph Hellwig 	wait_for_completion(&adapter->int_waitq);
45141da177e4SLinus Torvalds 
45151da177e4SLinus Torvalds 	rval = scmd->result;
45161da177e4SLinus Torvalds 	mc->status = scmd->result;
45171da177e4SLinus Torvalds 	kfree(sdev);
45181da177e4SLinus Torvalds 
45191da177e4SLinus Torvalds 	/*
45201da177e4SLinus Torvalds 	 * Print a debug message for all failed commands. Applications can use
45211da177e4SLinus Torvalds 	 * this information.
45221da177e4SLinus Torvalds 	 */
45231da177e4SLinus Torvalds 	if( scmd->result && trace_level ) {
45241da177e4SLinus Torvalds 		printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
45251da177e4SLinus Torvalds 			mc->cmd, mc->opcode, mc->subopcode, scmd->result);
45261da177e4SLinus Torvalds 	}
45271da177e4SLinus Torvalds 
45280b950672SArjan van de Ven 	mutex_unlock(&adapter->int_mtx);
45291da177e4SLinus Torvalds 
45301da177e4SLinus Torvalds 	return rval;
45311da177e4SLinus Torvalds }
45321da177e4SLinus Torvalds 
45331da177e4SLinus Torvalds 
45341da177e4SLinus Torvalds /**
45351da177e4SLinus Torvalds  * mega_internal_done()
45361da177e4SLinus Torvalds  * @scmd - internal scsi command
45371da177e4SLinus Torvalds  *
45381da177e4SLinus Torvalds  * Callback routine for internal commands.
45391da177e4SLinus Torvalds  */
45401da177e4SLinus Torvalds static void
45411da177e4SLinus Torvalds mega_internal_done(Scsi_Cmnd *scmd)
45421da177e4SLinus Torvalds {
45431da177e4SLinus Torvalds 	adapter_t	*adapter;
45441da177e4SLinus Torvalds 
45451da177e4SLinus Torvalds 	adapter = (adapter_t *)scmd->device->host->hostdata;
45461da177e4SLinus Torvalds 
45478d115f84SChristoph Hellwig 	complete(&adapter->int_waitq);
45481da177e4SLinus Torvalds 
45491da177e4SLinus Torvalds }
45501da177e4SLinus Torvalds 
45511da177e4SLinus Torvalds 
45521da177e4SLinus Torvalds static struct scsi_host_template megaraid_template = {
45531da177e4SLinus Torvalds 	.module				= THIS_MODULE,
45541da177e4SLinus Torvalds 	.name				= "MegaRAID",
45553492b328SJu, Seokmann 	.proc_name			= "megaraid_legacy",
45561da177e4SLinus Torvalds 	.info				= megaraid_info,
45571da177e4SLinus Torvalds 	.queuecommand			= megaraid_queue,
45581da177e4SLinus Torvalds 	.bios_param			= megaraid_biosparam,
45591da177e4SLinus Torvalds 	.max_sectors			= MAX_SECTORS_PER_IO,
45601da177e4SLinus Torvalds 	.can_queue			= MAX_COMMANDS,
45611da177e4SLinus Torvalds 	.this_id			= DEFAULT_INITIATOR_ID,
45621da177e4SLinus Torvalds 	.sg_tablesize			= MAX_SGLIST,
45631da177e4SLinus Torvalds 	.cmd_per_lun			= DEF_CMD_PER_LUN,
45641da177e4SLinus Torvalds 	.use_clustering			= ENABLE_CLUSTERING,
45651da177e4SLinus Torvalds 	.eh_abort_handler		= megaraid_abort,
45661da177e4SLinus Torvalds 	.eh_device_reset_handler	= megaraid_reset,
45671da177e4SLinus Torvalds 	.eh_bus_reset_handler		= megaraid_reset,
45681da177e4SLinus Torvalds 	.eh_host_reset_handler		= megaraid_reset,
45691da177e4SLinus Torvalds };
45701da177e4SLinus Torvalds 
45711da177e4SLinus Torvalds static int __devinit
45721da177e4SLinus Torvalds megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
45731da177e4SLinus Torvalds {
45741da177e4SLinus Torvalds 	struct Scsi_Host *host;
45751da177e4SLinus Torvalds 	adapter_t *adapter;
45761da177e4SLinus Torvalds 	unsigned long mega_baseport, tbase, flag = 0;
45771da177e4SLinus Torvalds 	u16 subsysid, subsysvid;
45781da177e4SLinus Torvalds 	u8 pci_bus, pci_dev_func;
45791da177e4SLinus Torvalds 	int irq, i, j;
45801da177e4SLinus Torvalds 	int error = -ENODEV;
45811da177e4SLinus Torvalds 
45821da177e4SLinus Torvalds 	if (pci_enable_device(pdev))
45831da177e4SLinus Torvalds 		goto out;
45841da177e4SLinus Torvalds 	pci_set_master(pdev);
45851da177e4SLinus Torvalds 
45861da177e4SLinus Torvalds 	pci_bus = pdev->bus->number;
45871da177e4SLinus Torvalds 	pci_dev_func = pdev->devfn;
45881da177e4SLinus Torvalds 
45891da177e4SLinus Torvalds 	/*
45901da177e4SLinus Torvalds 	 * The megaraid3 stuff reports the ID of the Intel part which is not
45911da177e4SLinus Torvalds 	 * remotely specific to the megaraid
45921da177e4SLinus Torvalds 	 */
45931da177e4SLinus Torvalds 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
45941da177e4SLinus Torvalds 		u16 magic;
45951da177e4SLinus Torvalds 		/*
45961da177e4SLinus Torvalds 		 * Don't fall over the Compaq management cards using the same
45971da177e4SLinus Torvalds 		 * PCI identifier
45981da177e4SLinus Torvalds 		 */
45991da177e4SLinus Torvalds 		if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
46001da177e4SLinus Torvalds 		    pdev->subsystem_device == 0xC000)
46011da177e4SLinus Torvalds 		   	return -ENODEV;
46021da177e4SLinus Torvalds 		/* Now check the magic signature byte */
46031da177e4SLinus Torvalds 		pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
46041da177e4SLinus Torvalds 		if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
46051da177e4SLinus Torvalds 			return -ENODEV;
46061da177e4SLinus Torvalds 		/* Ok it is probably a megaraid */
46071da177e4SLinus Torvalds 	}
46081da177e4SLinus Torvalds 
46091da177e4SLinus Torvalds 	/*
46101da177e4SLinus Torvalds 	 * For these vendor and device ids, signature offsets are not
46111da177e4SLinus Torvalds 	 * valid and 64 bit is implicit
46121da177e4SLinus Torvalds 	 */
46131da177e4SLinus Torvalds 	if (id->driver_data & BOARD_64BIT)
46141da177e4SLinus Torvalds 		flag |= BOARD_64BIT;
46151da177e4SLinus Torvalds 	else {
46161da177e4SLinus Torvalds 		u32 magic64;
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds 		pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
46191da177e4SLinus Torvalds 		if (magic64 == HBA_SIGNATURE_64BIT)
46201da177e4SLinus Torvalds 			flag |= BOARD_64BIT;
46211da177e4SLinus Torvalds 	}
46221da177e4SLinus Torvalds 
46231da177e4SLinus Torvalds 	subsysvid = pdev->subsystem_vendor;
46241da177e4SLinus Torvalds 	subsysid = pdev->subsystem_device;
46251da177e4SLinus Torvalds 
46261da177e4SLinus Torvalds 	printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
46271da177e4SLinus Torvalds 		id->vendor, id->device, pci_bus);
46281da177e4SLinus Torvalds 
46291da177e4SLinus Torvalds 	printk("slot %d:func %d\n",
46301da177e4SLinus Torvalds 		PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
46311da177e4SLinus Torvalds 
46321da177e4SLinus Torvalds 	/* Read the base port and IRQ from PCI */
46331da177e4SLinus Torvalds 	mega_baseport = pci_resource_start(pdev, 0);
46341da177e4SLinus Torvalds 	irq = pdev->irq;
46351da177e4SLinus Torvalds 
46361da177e4SLinus Torvalds 	tbase = mega_baseport;
46371da177e4SLinus Torvalds 	if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
46381da177e4SLinus Torvalds 		flag |= BOARD_MEMMAP;
46391da177e4SLinus Torvalds 
46401da177e4SLinus Torvalds 		if (!request_mem_region(mega_baseport, 128, "megaraid")) {
46411da177e4SLinus Torvalds 			printk(KERN_WARNING "megaraid: mem region busy!\n");
46421da177e4SLinus Torvalds 			goto out_disable_device;
46431da177e4SLinus Torvalds 		}
46441da177e4SLinus Torvalds 
46451da177e4SLinus Torvalds 		mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
46461da177e4SLinus Torvalds 		if (!mega_baseport) {
46471da177e4SLinus Torvalds 			printk(KERN_WARNING
46481da177e4SLinus Torvalds 			       "megaraid: could not map hba memory\n");
46491da177e4SLinus Torvalds 			goto out_release_region;
46501da177e4SLinus Torvalds 		}
46511da177e4SLinus Torvalds 	} else {
46521da177e4SLinus Torvalds 		flag |= BOARD_IOMAP;
46531da177e4SLinus Torvalds 		mega_baseport += 0x10;
46541da177e4SLinus Torvalds 
46551da177e4SLinus Torvalds 		if (!request_region(mega_baseport, 16, "megaraid"))
46561da177e4SLinus Torvalds 			goto out_disable_device;
46571da177e4SLinus Torvalds 	}
46581da177e4SLinus Torvalds 
46591da177e4SLinus Torvalds 	/* Initialize SCSI Host structure */
46601da177e4SLinus Torvalds 	host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
46611da177e4SLinus Torvalds 	if (!host)
46621da177e4SLinus Torvalds 		goto out_iounmap;
46631da177e4SLinus Torvalds 
46641da177e4SLinus Torvalds 	adapter = (adapter_t *)host->hostdata;
46651da177e4SLinus Torvalds 	memset(adapter, 0, sizeof(adapter_t));
46661da177e4SLinus Torvalds 
46671da177e4SLinus Torvalds 	printk(KERN_NOTICE
46681da177e4SLinus Torvalds 		"scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
46691da177e4SLinus Torvalds 		host->host_no, mega_baseport, irq);
46701da177e4SLinus Torvalds 
46711da177e4SLinus Torvalds 	adapter->base = mega_baseport;
467200769ec4SJeff Garzik 	if (flag & BOARD_MEMMAP)
467300769ec4SJeff Garzik 		adapter->mmio_base = (void __iomem *) mega_baseport;
46741da177e4SLinus Torvalds 
46751da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->free_list);
46761da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->pending_list);
46771da177e4SLinus Torvalds 	INIT_LIST_HEAD(&adapter->completed_list);
46781da177e4SLinus Torvalds 
46791da177e4SLinus Torvalds 	adapter->flag = flag;
46801da177e4SLinus Torvalds 	spin_lock_init(&adapter->lock);
46811da177e4SLinus Torvalds 
46821da177e4SLinus Torvalds 	host->cmd_per_lun = max_cmd_per_lun;
46831da177e4SLinus Torvalds 	host->max_sectors = max_sectors_per_io;
46841da177e4SLinus Torvalds 
46851da177e4SLinus Torvalds 	adapter->dev = pdev;
46861da177e4SLinus Torvalds 	adapter->host = host;
46871da177e4SLinus Torvalds 
46881da177e4SLinus Torvalds 	adapter->host->irq = irq;
46891da177e4SLinus Torvalds 
46901da177e4SLinus Torvalds 	if (flag & BOARD_MEMMAP)
46911da177e4SLinus Torvalds 		adapter->host->base = tbase;
46921da177e4SLinus Torvalds 	else {
46931da177e4SLinus Torvalds 		adapter->host->io_port = tbase;
46941da177e4SLinus Torvalds 		adapter->host->n_io_port = 16;
46951da177e4SLinus Torvalds 	}
46961da177e4SLinus Torvalds 
46971da177e4SLinus Torvalds 	adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
46981da177e4SLinus Torvalds 
46991da177e4SLinus Torvalds 	/*
47001da177e4SLinus Torvalds 	 * Allocate buffer to issue internal commands.
47011da177e4SLinus Torvalds 	 */
47021da177e4SLinus Torvalds 	adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
47031da177e4SLinus Torvalds 		MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
47041da177e4SLinus Torvalds 	if (!adapter->mega_buffer) {
47051da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: out of RAM.\n");
47061da177e4SLinus Torvalds 		goto out_host_put;
47071da177e4SLinus Torvalds 	}
47081da177e4SLinus Torvalds 
47091da177e4SLinus Torvalds 	adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
47101da177e4SLinus Torvalds 	if (!adapter->scb_list) {
47111da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: out of RAM.\n");
47121da177e4SLinus Torvalds 		goto out_free_cmd_buffer;
47131da177e4SLinus Torvalds 	}
47141da177e4SLinus Torvalds 
47151da177e4SLinus Torvalds 	if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
47161da177e4SLinus Torvalds 				megaraid_isr_memmapped : megaraid_isr_iomapped,
47171d6f359aSThomas Gleixner 					IRQF_SHARED, "megaraid", adapter)) {
47181da177e4SLinus Torvalds 		printk(KERN_WARNING
47191da177e4SLinus Torvalds 			"megaraid: Couldn't register IRQ %d!\n", irq);
47201da177e4SLinus Torvalds 		goto out_free_scb_list;
47211da177e4SLinus Torvalds 	}
47221da177e4SLinus Torvalds 
47231da177e4SLinus Torvalds 	if (mega_setup_mailbox(adapter))
47241da177e4SLinus Torvalds 		goto out_free_irq;
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds 	if (mega_query_adapter(adapter))
47271da177e4SLinus Torvalds 		goto out_free_mbox;
47281da177e4SLinus Torvalds 
47291da177e4SLinus Torvalds 	/*
47301da177e4SLinus Torvalds 	 * Have checks for some buggy f/w
47311da177e4SLinus Torvalds 	 */
47321da177e4SLinus Torvalds 	if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
47331da177e4SLinus Torvalds 		/*
47341da177e4SLinus Torvalds 		 * Which firmware
47351da177e4SLinus Torvalds 		 */
47361da177e4SLinus Torvalds 		if (!strcmp(adapter->fw_version, "3.00") ||
47371da177e4SLinus Torvalds 				!strcmp(adapter->fw_version, "3.01")) {
47381da177e4SLinus Torvalds 
47391da177e4SLinus Torvalds 			printk( KERN_WARNING
47401da177e4SLinus Torvalds 				"megaraid: Your  card is a Dell PERC "
47411da177e4SLinus Torvalds 				"2/SC RAID controller with  "
47421da177e4SLinus Torvalds 				"firmware\nmegaraid: 3.00 or 3.01.  "
47431da177e4SLinus Torvalds 				"This driver is known to have "
47441da177e4SLinus Torvalds 				"corruption issues\nmegaraid: with "
47451da177e4SLinus Torvalds 				"those firmware versions on this "
47461da177e4SLinus Torvalds 				"specific card.  In order\nmegaraid: "
47471da177e4SLinus Torvalds 				"to protect your data, please upgrade "
47481da177e4SLinus Torvalds 				"your firmware to version\nmegaraid: "
47491da177e4SLinus Torvalds 				"3.10 or later, available from the "
47501da177e4SLinus Torvalds 				"Dell Technical Support web\n"
47511da177e4SLinus Torvalds 				"megaraid: site at\nhttp://support."
47521da177e4SLinus Torvalds 				"dell.com/us/en/filelib/download/"
47531da177e4SLinus Torvalds 				"index.asp?fileid=2940\n"
47541da177e4SLinus Torvalds 			);
47551da177e4SLinus Torvalds 		}
47561da177e4SLinus Torvalds 	}
47571da177e4SLinus Torvalds 
47581da177e4SLinus Torvalds 	/*
47591da177e4SLinus Torvalds 	 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
47601da177e4SLinus Torvalds 	 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
47611da177e4SLinus Torvalds 	 * support, since this firmware cannot handle 64 bit
47621da177e4SLinus Torvalds 	 * addressing
47631da177e4SLinus Torvalds 	 */
47641da177e4SLinus Torvalds 	if ((subsysvid == HP_SUBSYS_VID) &&
47651da177e4SLinus Torvalds 	    ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
47661da177e4SLinus Torvalds 		/*
47671da177e4SLinus Torvalds 		 * which firmware
47681da177e4SLinus Torvalds 		 */
47691da177e4SLinus Torvalds 		if (!strcmp(adapter->fw_version, "H01.07") ||
47701da177e4SLinus Torvalds 		    !strcmp(adapter->fw_version, "H01.08") ||
47711da177e4SLinus Torvalds 		    !strcmp(adapter->fw_version, "H01.09") ) {
47721da177e4SLinus Torvalds 			printk(KERN_WARNING
47731da177e4SLinus Torvalds 				"megaraid: Firmware H.01.07, "
47741da177e4SLinus Torvalds 				"H.01.08, and H.01.09 on 1M/2M "
47751da177e4SLinus Torvalds 				"controllers\n"
47761da177e4SLinus Torvalds 				"megaraid: do not support 64 bit "
47771da177e4SLinus Torvalds 				"addressing.\nmegaraid: DISABLING "
47781da177e4SLinus Torvalds 				"64 bit support.\n");
47791da177e4SLinus Torvalds 			adapter->flag &= ~BOARD_64BIT;
47801da177e4SLinus Torvalds 		}
47811da177e4SLinus Torvalds 	}
47821da177e4SLinus Torvalds 
47831da177e4SLinus Torvalds 	if (mega_is_bios_enabled(adapter))
47841da177e4SLinus Torvalds 		mega_hbas[hba_count].is_bios_enabled = 1;
47851da177e4SLinus Torvalds 	mega_hbas[hba_count].hostdata_addr = adapter;
47861da177e4SLinus Torvalds 
47871da177e4SLinus Torvalds 	/*
47881da177e4SLinus Torvalds 	 * Find out which channel is raid and which is scsi. This is
47891da177e4SLinus Torvalds 	 * for ROMB support.
47901da177e4SLinus Torvalds 	 */
47911da177e4SLinus Torvalds 	mega_enum_raid_scsi(adapter);
47921da177e4SLinus Torvalds 
47931da177e4SLinus Torvalds 	/*
47941da177e4SLinus Torvalds 	 * Find out if a logical drive is set as the boot drive. If
47951da177e4SLinus Torvalds 	 * there is one, will make that as the first logical drive.
47961da177e4SLinus Torvalds 	 * ROMB: Do we have to boot from a physical drive. Then all
47971da177e4SLinus Torvalds 	 * the physical drives would appear before the logical disks.
47981da177e4SLinus Torvalds 	 * Else, all the physical drives would be exported to the mid
47991da177e4SLinus Torvalds 	 * layer after logical drives.
48001da177e4SLinus Torvalds 	 */
48011da177e4SLinus Torvalds 	mega_get_boot_drv(adapter);
48021da177e4SLinus Torvalds 
48031da177e4SLinus Torvalds 	if (adapter->boot_pdrv_enabled) {
48041da177e4SLinus Torvalds 		j = adapter->product_info.nchannels;
48051da177e4SLinus Torvalds 		for( i = 0; i < j; i++ )
48061da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 0;
48071da177e4SLinus Torvalds 		for( i = j; i < NVIRT_CHAN + j; i++ )
48081da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 1;
48091da177e4SLinus Torvalds 	} else {
48101da177e4SLinus Torvalds 		for (i = 0; i < NVIRT_CHAN; i++)
48111da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 1;
48121da177e4SLinus Torvalds 		for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
48131da177e4SLinus Torvalds 			adapter->logdrv_chan[i] = 0;
48141da177e4SLinus Torvalds 		adapter->mega_ch_class <<= NVIRT_CHAN;
48151da177e4SLinus Torvalds 	}
48161da177e4SLinus Torvalds 
48171da177e4SLinus Torvalds 	/*
48181da177e4SLinus Torvalds 	 * Do we support random deletion and addition of logical
48191da177e4SLinus Torvalds 	 * drives
48201da177e4SLinus Torvalds 	 */
48211da177e4SLinus Torvalds 	adapter->read_ldidmap = 0;	/* set it after first logdrv
48221da177e4SLinus Torvalds 						   delete cmd */
48231da177e4SLinus Torvalds 	adapter->support_random_del = mega_support_random_del(adapter);
48241da177e4SLinus Torvalds 
48251da177e4SLinus Torvalds 	/* Initialize SCBs */
48261da177e4SLinus Torvalds 	if (mega_init_scb(adapter))
48271da177e4SLinus Torvalds 		goto out_free_mbox;
48281da177e4SLinus Torvalds 
48291da177e4SLinus Torvalds 	/*
48301da177e4SLinus Torvalds 	 * Reset the pending commands counter
48311da177e4SLinus Torvalds 	 */
48321da177e4SLinus Torvalds 	atomic_set(&adapter->pend_cmds, 0);
48331da177e4SLinus Torvalds 
48341da177e4SLinus Torvalds 	/*
48351da177e4SLinus Torvalds 	 * Reset the adapter quiescent flag
48361da177e4SLinus Torvalds 	 */
48371da177e4SLinus Torvalds 	atomic_set(&adapter->quiescent, 0);
48381da177e4SLinus Torvalds 
48391da177e4SLinus Torvalds 	hba_soft_state[hba_count] = adapter;
48401da177e4SLinus Torvalds 
48411da177e4SLinus Torvalds 	/*
48421da177e4SLinus Torvalds 	 * Fill in the structure which needs to be passed back to the
48431da177e4SLinus Torvalds 	 * application when it does an ioctl() for controller related
48441da177e4SLinus Torvalds 	 * information.
48451da177e4SLinus Torvalds 	 */
48461da177e4SLinus Torvalds 	i = hba_count;
48471da177e4SLinus Torvalds 
48481da177e4SLinus Torvalds 	mcontroller[i].base = mega_baseport;
48491da177e4SLinus Torvalds 	mcontroller[i].irq = irq;
48501da177e4SLinus Torvalds 	mcontroller[i].numldrv = adapter->numldrv;
48511da177e4SLinus Torvalds 	mcontroller[i].pcibus = pci_bus;
48521da177e4SLinus Torvalds 	mcontroller[i].pcidev = id->device;
48531da177e4SLinus Torvalds 	mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
48541da177e4SLinus Torvalds 	mcontroller[i].pciid = -1;
48551da177e4SLinus Torvalds 	mcontroller[i].pcivendor = id->vendor;
48561da177e4SLinus Torvalds 	mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
48571da177e4SLinus Torvalds 	mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
48581da177e4SLinus Torvalds 
48591da177e4SLinus Torvalds 
48601da177e4SLinus Torvalds 	/* Set the Mode of addressing to 64 bit if we can */
48611da177e4SLinus Torvalds 	if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4862910638aeSMatthias Gehre 		pci_set_dma_mask(pdev, DMA_64BIT_MASK);
48631da177e4SLinus Torvalds 		adapter->has_64bit_addr = 1;
48641da177e4SLinus Torvalds 	} else  {
4865910638aeSMatthias Gehre 		pci_set_dma_mask(pdev, DMA_32BIT_MASK);
48661da177e4SLinus Torvalds 		adapter->has_64bit_addr = 0;
48671da177e4SLinus Torvalds 	}
48681da177e4SLinus Torvalds 
48690b950672SArjan van de Ven 	mutex_init(&adapter->int_mtx);
48708d115f84SChristoph Hellwig 	init_completion(&adapter->int_waitq);
48711da177e4SLinus Torvalds 
48721da177e4SLinus Torvalds 	adapter->this_id = DEFAULT_INITIATOR_ID;
48731da177e4SLinus Torvalds 	adapter->host->this_id = DEFAULT_INITIATOR_ID;
48741da177e4SLinus Torvalds 
48751da177e4SLinus Torvalds #if MEGA_HAVE_CLUSTERING
48761da177e4SLinus Torvalds 	/*
48771da177e4SLinus Torvalds 	 * Is cluster support enabled on this controller
48781da177e4SLinus Torvalds 	 * Note: In a cluster the HBAs ( the initiators ) will have
48791da177e4SLinus Torvalds 	 * different target IDs and we cannot assume it to be 7. Call
48801da177e4SLinus Torvalds 	 * to mega_support_cluster() will get the target ids also if
48811da177e4SLinus Torvalds 	 * the cluster support is available
48821da177e4SLinus Torvalds 	 */
48831da177e4SLinus Torvalds 	adapter->has_cluster = mega_support_cluster(adapter);
48841da177e4SLinus Torvalds 	if (adapter->has_cluster) {
48851da177e4SLinus Torvalds 		printk(KERN_NOTICE
48861da177e4SLinus Torvalds 			"megaraid: Cluster driver, initiator id:%d\n",
48871da177e4SLinus Torvalds 			adapter->this_id);
48881da177e4SLinus Torvalds 	}
48891da177e4SLinus Torvalds #endif
48901da177e4SLinus Torvalds 
48911da177e4SLinus Torvalds 	pci_set_drvdata(pdev, host);
48921da177e4SLinus Torvalds 
48931da177e4SLinus Torvalds 	mega_create_proc_entry(hba_count, mega_proc_dir_entry);
48941da177e4SLinus Torvalds 
48951da177e4SLinus Torvalds 	error = scsi_add_host(host, &pdev->dev);
48961da177e4SLinus Torvalds 	if (error)
48971da177e4SLinus Torvalds 		goto out_free_mbox;
48981da177e4SLinus Torvalds 
48991da177e4SLinus Torvalds 	scsi_scan_host(host);
49001da177e4SLinus Torvalds 	hba_count++;
49011da177e4SLinus Torvalds 	return 0;
49021da177e4SLinus Torvalds 
49031da177e4SLinus Torvalds  out_free_mbox:
49041da177e4SLinus Torvalds 	pci_free_consistent(adapter->dev, sizeof(mbox64_t),
49051da177e4SLinus Torvalds 			adapter->una_mbox64, adapter->una_mbox64_dma);
49061da177e4SLinus Torvalds  out_free_irq:
49071da177e4SLinus Torvalds 	free_irq(adapter->host->irq, adapter);
49081da177e4SLinus Torvalds  out_free_scb_list:
49091da177e4SLinus Torvalds 	kfree(adapter->scb_list);
49101da177e4SLinus Torvalds  out_free_cmd_buffer:
49111da177e4SLinus Torvalds 	pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
49121da177e4SLinus Torvalds 			adapter->mega_buffer, adapter->buf_dma_handle);
49131da177e4SLinus Torvalds  out_host_put:
49141da177e4SLinus Torvalds 	scsi_host_put(host);
49151da177e4SLinus Torvalds  out_iounmap:
49161da177e4SLinus Torvalds 	if (flag & BOARD_MEMMAP)
49171da177e4SLinus Torvalds 		iounmap((void *)mega_baseport);
49181da177e4SLinus Torvalds  out_release_region:
49191da177e4SLinus Torvalds 	if (flag & BOARD_MEMMAP)
49201da177e4SLinus Torvalds 		release_mem_region(tbase, 128);
49211da177e4SLinus Torvalds 	else
49221da177e4SLinus Torvalds 		release_region(mega_baseport, 16);
49231da177e4SLinus Torvalds  out_disable_device:
49241da177e4SLinus Torvalds 	pci_disable_device(pdev);
49251da177e4SLinus Torvalds  out:
49261da177e4SLinus Torvalds 	return error;
49271da177e4SLinus Torvalds }
49281da177e4SLinus Torvalds 
49291da177e4SLinus Torvalds static void
49301da177e4SLinus Torvalds __megaraid_shutdown(adapter_t *adapter)
49311da177e4SLinus Torvalds {
49321da177e4SLinus Torvalds 	u_char	raw_mbox[sizeof(struct mbox_out)];
49331da177e4SLinus Torvalds 	mbox_t	*mbox = (mbox_t *)raw_mbox;
49341da177e4SLinus Torvalds 	int	i;
49351da177e4SLinus Torvalds 
49361da177e4SLinus Torvalds 	/* Flush adapter cache */
49371da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
49381da177e4SLinus Torvalds 	raw_mbox[0] = FLUSH_ADAPTER;
49391da177e4SLinus Torvalds 
49401da177e4SLinus Torvalds 	free_irq(adapter->host->irq, adapter);
49411da177e4SLinus Torvalds 
49421da177e4SLinus Torvalds 	/* Issue a blocking (interrupts disabled) command to the card */
49431da177e4SLinus Torvalds 	issue_scb_block(adapter, raw_mbox);
49441da177e4SLinus Torvalds 
49451da177e4SLinus Torvalds 	/* Flush disks cache */
49461da177e4SLinus Torvalds 	memset(&mbox->m_out, 0, sizeof(raw_mbox));
49471da177e4SLinus Torvalds 	raw_mbox[0] = FLUSH_SYSTEM;
49481da177e4SLinus Torvalds 
49491da177e4SLinus Torvalds 	/* Issue a blocking (interrupts disabled) command to the card */
49501da177e4SLinus Torvalds 	issue_scb_block(adapter, raw_mbox);
49511da177e4SLinus Torvalds 
49521da177e4SLinus Torvalds 	if (atomic_read(&adapter->pend_cmds) > 0)
49531da177e4SLinus Torvalds 		printk(KERN_WARNING "megaraid: pending commands!!\n");
49541da177e4SLinus Torvalds 
49551da177e4SLinus Torvalds 	/*
49561da177e4SLinus Torvalds 	 * Have a delibrate delay to make sure all the caches are
49571da177e4SLinus Torvalds 	 * actually flushed.
49581da177e4SLinus Torvalds 	 */
49591da177e4SLinus Torvalds 	for (i = 0; i <= 10; i++)
49601da177e4SLinus Torvalds 		mdelay(1000);
49611da177e4SLinus Torvalds }
49621da177e4SLinus Torvalds 
49631da177e4SLinus Torvalds static void
49641da177e4SLinus Torvalds megaraid_remove_one(struct pci_dev *pdev)
49651da177e4SLinus Torvalds {
49661da177e4SLinus Torvalds 	struct Scsi_Host *host = pci_get_drvdata(pdev);
49671da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)host->hostdata;
49681da177e4SLinus Torvalds 	char	buf[12] = { 0 };
49691da177e4SLinus Torvalds 
49701da177e4SLinus Torvalds 	scsi_remove_host(host);
49711da177e4SLinus Torvalds 
49721da177e4SLinus Torvalds 	__megaraid_shutdown(adapter);
49731da177e4SLinus Torvalds 
49741da177e4SLinus Torvalds 	/* Free our resources */
49751da177e4SLinus Torvalds 	if (adapter->flag & BOARD_MEMMAP) {
49761da177e4SLinus Torvalds 		iounmap((void *)adapter->base);
49771da177e4SLinus Torvalds 		release_mem_region(adapter->host->base, 128);
49781da177e4SLinus Torvalds 	} else
49791da177e4SLinus Torvalds 		release_region(adapter->base, 16);
49801da177e4SLinus Torvalds 
49811da177e4SLinus Torvalds 	mega_free_sgl(adapter);
49821da177e4SLinus Torvalds 
49831da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
49841da177e4SLinus Torvalds 	if (adapter->controller_proc_dir_entry) {
49851da177e4SLinus Torvalds 		remove_proc_entry("stat", adapter->controller_proc_dir_entry);
49861da177e4SLinus Torvalds 		remove_proc_entry("config",
49871da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
49881da177e4SLinus Torvalds 		remove_proc_entry("mailbox",
49891da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
49901da177e4SLinus Torvalds #if MEGA_HAVE_ENH_PROC
49911da177e4SLinus Torvalds 		remove_proc_entry("rebuild-rate",
49921da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
49931da177e4SLinus Torvalds 		remove_proc_entry("battery-status",
49941da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
49951da177e4SLinus Torvalds 
49961da177e4SLinus Torvalds 		remove_proc_entry("diskdrives-ch0",
49971da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
49981da177e4SLinus Torvalds 		remove_proc_entry("diskdrives-ch1",
49991da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50001da177e4SLinus Torvalds 		remove_proc_entry("diskdrives-ch2",
50011da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50021da177e4SLinus Torvalds 		remove_proc_entry("diskdrives-ch3",
50031da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50041da177e4SLinus Torvalds 
50051da177e4SLinus Torvalds 		remove_proc_entry("raiddrives-0-9",
50061da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50071da177e4SLinus Torvalds 		remove_proc_entry("raiddrives-10-19",
50081da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50091da177e4SLinus Torvalds 		remove_proc_entry("raiddrives-20-29",
50101da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50111da177e4SLinus Torvalds 		remove_proc_entry("raiddrives-30-39",
50121da177e4SLinus Torvalds 				adapter->controller_proc_dir_entry);
50131da177e4SLinus Torvalds #endif
50141da177e4SLinus Torvalds 		sprintf(buf, "hba%d", adapter->host->host_no);
50151da177e4SLinus Torvalds 		remove_proc_entry(buf, mega_proc_dir_entry);
50161da177e4SLinus Torvalds 	}
50171da177e4SLinus Torvalds #endif
50181da177e4SLinus Torvalds 
50191da177e4SLinus Torvalds 	pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
50201da177e4SLinus Torvalds 			adapter->mega_buffer, adapter->buf_dma_handle);
50211da177e4SLinus Torvalds 	kfree(adapter->scb_list);
50221da177e4SLinus Torvalds 	pci_free_consistent(adapter->dev, sizeof(mbox64_t),
50231da177e4SLinus Torvalds 			adapter->una_mbox64, adapter->una_mbox64_dma);
50241da177e4SLinus Torvalds 
50251da177e4SLinus Torvalds 	scsi_host_put(host);
50261da177e4SLinus Torvalds 	pci_disable_device(pdev);
50271da177e4SLinus Torvalds 
50281da177e4SLinus Torvalds 	hba_count--;
50291da177e4SLinus Torvalds }
50301da177e4SLinus Torvalds 
50311da177e4SLinus Torvalds static void
5032d18c3db5SGreg Kroah-Hartman megaraid_shutdown(struct pci_dev *pdev)
50331da177e4SLinus Torvalds {
5034d18c3db5SGreg Kroah-Hartman 	struct Scsi_Host *host = pci_get_drvdata(pdev);
50351da177e4SLinus Torvalds 	adapter_t *adapter = (adapter_t *)host->hostdata;
50361da177e4SLinus Torvalds 
50371da177e4SLinus Torvalds 	__megaraid_shutdown(adapter);
50381da177e4SLinus Torvalds }
50391da177e4SLinus Torvalds 
50401da177e4SLinus Torvalds static struct pci_device_id megaraid_pci_tbl[] = {
50411da177e4SLinus Torvalds 	{PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
50421da177e4SLinus Torvalds 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
50431da177e4SLinus Torvalds 	{PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
50441da177e4SLinus Torvalds 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
50451da177e4SLinus Torvalds 	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
50461da177e4SLinus Torvalds 		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
50471da177e4SLinus Torvalds 	{0,}
50481da177e4SLinus Torvalds };
50491da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
50501da177e4SLinus Torvalds 
50511da177e4SLinus Torvalds static struct pci_driver megaraid_pci_driver = {
50523542adcbSJu, Seokmann 	.name		= "megaraid_legacy",
50531da177e4SLinus Torvalds 	.id_table	= megaraid_pci_tbl,
50541da177e4SLinus Torvalds 	.probe		= megaraid_probe_one,
50551da177e4SLinus Torvalds 	.remove		= __devexit_p(megaraid_remove_one),
50561da177e4SLinus Torvalds 	.shutdown	= megaraid_shutdown,
50571da177e4SLinus Torvalds };
50581da177e4SLinus Torvalds 
50591da177e4SLinus Torvalds static int __init megaraid_init(void)
50601da177e4SLinus Torvalds {
50611da177e4SLinus Torvalds 	int error;
50621da177e4SLinus Torvalds 
50631da177e4SLinus Torvalds 	if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
50641da177e4SLinus Torvalds 		max_cmd_per_lun = MAX_CMD_PER_LUN;
50651da177e4SLinus Torvalds 	if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
50661da177e4SLinus Torvalds 		max_mbox_busy_wait = MBOX_BUSY_WAIT;
50671da177e4SLinus Torvalds 
50681da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
50691da177e4SLinus Torvalds 	mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
50701da177e4SLinus Torvalds 	if (!mega_proc_dir_entry) {
50711da177e4SLinus Torvalds 		printk(KERN_WARNING
50721da177e4SLinus Torvalds 				"megaraid: failed to create megaraid root\n");
50731da177e4SLinus Torvalds 	}
50741da177e4SLinus Torvalds #endif
50754520b008SRichard Knutsson 	error = pci_register_driver(&megaraid_pci_driver);
50761da177e4SLinus Torvalds 	if (error) {
50771da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
50781da177e4SLinus Torvalds 		remove_proc_entry("megaraid", &proc_root);
50791da177e4SLinus Torvalds #endif
50801da177e4SLinus Torvalds 		return error;
50811da177e4SLinus Torvalds 	}
50821da177e4SLinus Torvalds 
50831da177e4SLinus Torvalds 	/*
50841da177e4SLinus Torvalds 	 * Register the driver as a character device, for applications
50851da177e4SLinus Torvalds 	 * to access it for ioctls.
50861da177e4SLinus Torvalds 	 * First argument (major) to register_chrdev implies a dynamic
50871da177e4SLinus Torvalds 	 * major number allocation.
50881da177e4SLinus Torvalds 	 */
50893492b328SJu, Seokmann 	major = register_chrdev(0, "megadev_legacy", &megadev_fops);
50901da177e4SLinus Torvalds 	if (!major) {
50911da177e4SLinus Torvalds 		printk(KERN_WARNING
50921da177e4SLinus Torvalds 				"megaraid: failed to register char device\n");
50931da177e4SLinus Torvalds 	}
50941da177e4SLinus Torvalds 
50951da177e4SLinus Torvalds 	return 0;
50961da177e4SLinus Torvalds }
50971da177e4SLinus Torvalds 
50981da177e4SLinus Torvalds static void __exit megaraid_exit(void)
50991da177e4SLinus Torvalds {
51001da177e4SLinus Torvalds 	/*
51011da177e4SLinus Torvalds 	 * Unregister the character device interface to the driver.
51021da177e4SLinus Torvalds 	 */
51033492b328SJu, Seokmann 	unregister_chrdev(major, "megadev_legacy");
51041da177e4SLinus Torvalds 
51051da177e4SLinus Torvalds 	pci_unregister_driver(&megaraid_pci_driver);
51061da177e4SLinus Torvalds 
51071da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
51081da177e4SLinus Torvalds 	remove_proc_entry("megaraid", &proc_root);
51091da177e4SLinus Torvalds #endif
51101da177e4SLinus Torvalds }
51111da177e4SLinus Torvalds 
51121da177e4SLinus Torvalds module_init(megaraid_init);
51131da177e4SLinus Torvalds module_exit(megaraid_exit);
51141da177e4SLinus Torvalds 
51151da177e4SLinus Torvalds /* vi: set ts=8 sw=8 tw=78: */
5116