xref: /openbmc/linux/drivers/scsi/megaraid/mega_common.h (revision 0c2cc4337968f7aab91a91b8d5889982e3a3bd0d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  *			Linux MegaRAID device driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2003-2004  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  * FILE		: mega_common.h
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Libaray of common routine used by all low-level megaraid drivers
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #ifndef _MEGA_COMMON_H_
181da177e4SLinus Torvalds #define _MEGA_COMMON_H_
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/pci.h>
231da177e4SLinus Torvalds #include <linux/spinlock.h>
24*0c2cc433SMatthias Kaehlcke #include <linux/mutex.h>
251da177e4SLinus Torvalds #include <linux/interrupt.h>
261da177e4SLinus Torvalds #include <linux/delay.h>
271da177e4SLinus Torvalds #include <linux/blkdev.h>
281da177e4SLinus Torvalds #include <linux/list.h>
291da177e4SLinus Torvalds #include <linux/moduleparam.h>
30672b2d38SJu, Seokmann  #include <linux/dma-mapping.h>
311da177e4SLinus Torvalds #include <asm/semaphore.h>
321da177e4SLinus Torvalds #include <scsi/scsi.h>
331da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
341da177e4SLinus Torvalds #include <scsi/scsi_device.h>
351da177e4SLinus Torvalds #include <scsi/scsi_host.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define LSI_MAX_CHANNELS		16
391da177e4SLinus Torvalds #define LSI_MAX_LOGICAL_DRIVES_64LD	(64+1)
401da177e4SLinus Torvalds 
41fbf60802SJu, Seokmann #define HBA_SIGNATURE_64_BIT		0x299
42fbf60802SJu, Seokmann #define PCI_CONF_AMISIG64		0xa4
43fbf60802SJu, Seokmann 
44aa677bc7SJu, Seokmann #define MEGA_SCSI_INQ_EVPD		1
45aa677bc7SJu, Seokmann #define MEGA_INVALID_FIELD_IN_CDB	0x24
46aa677bc7SJu, Seokmann 
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /**
491da177e4SLinus Torvalds  * scb_t - scsi command control block
50a69b74d3SRandy Dunlap  * @ccb			: command control block for individual driver
51a69b74d3SRandy Dunlap  * @list		: list of control blocks
52a69b74d3SRandy Dunlap  * @gp			: general purpose field for LLDs
53a69b74d3SRandy Dunlap  * @sno			: all SCBs have a serial number
54a69b74d3SRandy Dunlap  * @scp			: associated scsi command
55a69b74d3SRandy Dunlap  * @state		: current state of scb
56a69b74d3SRandy Dunlap  * @dma_dir		: direction of data transfer
57a69b74d3SRandy Dunlap  * @dma_type		: transfer with sg list, buffer, or no data transfer
58a69b74d3SRandy Dunlap  * @dev_channel		: actual channel on the device
59a69b74d3SRandy Dunlap  * @dev_target		: actual target on the device
60a69b74d3SRandy Dunlap  * @status		: completion status
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * This is our central data structure to issue commands the each driver.
631da177e4SLinus Torvalds  * Driver specific data structures are maintained in the ccb field.
641da177e4SLinus Torvalds  * scb provides a field 'gp', which can be used by LLD for its own purposes
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  * dev_channel and dev_target must be initialized with the actual channel and
671da177e4SLinus Torvalds  * target on the controller.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds typedef struct {
701da177e4SLinus Torvalds 	caddr_t			ccb;
711da177e4SLinus Torvalds 	struct list_head	list;
721da177e4SLinus Torvalds 	unsigned long		gp;
731da177e4SLinus Torvalds 	unsigned int		sno;
741da177e4SLinus Torvalds 	struct scsi_cmnd	*scp;
751da177e4SLinus Torvalds 	uint32_t		state;
761da177e4SLinus Torvalds 	uint32_t		dma_direction;
771da177e4SLinus Torvalds 	uint32_t		dma_type;
781da177e4SLinus Torvalds 	uint16_t		dev_channel;
791da177e4SLinus Torvalds 	uint16_t		dev_target;
801da177e4SLinus Torvalds 	uint32_t		status;
811da177e4SLinus Torvalds } scb_t;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * SCB states as it transitions from one state to another
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds #define SCB_FREE	0x0000	/* on the free list */
871da177e4SLinus Torvalds #define SCB_ACTIVE	0x0001	/* off the free list */
881da177e4SLinus Torvalds #define SCB_PENDQ	0x0002	/* on the pending queue */
891da177e4SLinus Torvalds #define SCB_ISSUED	0x0004	/* issued - owner f/w */
901da177e4SLinus Torvalds #define SCB_ABORT	0x0008	/* Got an abort for this one */
911da177e4SLinus Torvalds #define SCB_RESET	0x0010	/* Got a reset for this one */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds  * DMA types for scb
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds #define MRAID_DMA_NONE	0x0000	/* no data transfer for this command */
971da177e4SLinus Torvalds #define MRAID_DMA_WSG	0x0001	/* data transfer using a sg list */
981da177e4SLinus Torvalds #define MRAID_DMA_WBUF	0x0002	/* data transfer using a contiguous buffer */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /**
1021da177e4SLinus Torvalds  * struct adapter_t - driver's initialization structure
103a69b74d3SRandy Dunlap  * @aram dpc_h			: tasklet handle
104a69b74d3SRandy Dunlap  * @pdev			: pci configuration pointer for kernel
105a69b74d3SRandy Dunlap  * @host			: pointer to host structure of mid-layer
106a69b74d3SRandy Dunlap  * @lock			: synchronization lock for mid-layer and driver
107a69b74d3SRandy Dunlap  * @quiescent			: driver is quiescent for now.
108a69b74d3SRandy Dunlap  * @outstanding_cmds		: number of commands pending in the driver
109a69b74d3SRandy Dunlap  * @kscb_list			: pointer to the bulk of SCBs pointers for IO
110a69b74d3SRandy Dunlap  * @kscb_pool			: pool of free scbs for IO
111a69b74d3SRandy Dunlap  * @kscb_pool_lock		: lock for pool of free scbs
112a69b74d3SRandy Dunlap  * @pend_list			: pending commands list
113a69b74d3SRandy Dunlap  * @pend_list_lock		: exclusion lock for pending commands list
114a69b74d3SRandy Dunlap  * @completed_list		: list of completed commands
115a69b74d3SRandy Dunlap  * @completed_list_lock		: exclusion lock for list of completed commands
116a69b74d3SRandy Dunlap  * @sglen			: max sg elements supported
117a69b74d3SRandy Dunlap  * @device_ids			: to convert kernel device addr to our devices.
118a69b74d3SRandy Dunlap  * @raid_device			: raid adapter specific pointer
119a69b74d3SRandy Dunlap  * @max_channel			: maximum channel number supported - inclusive
120a69b74d3SRandy Dunlap  * @max_target			: max target supported - inclusive
121a69b74d3SRandy Dunlap  * @max_lun			: max lun supported - inclusive
122a69b74d3SRandy Dunlap  * @unique_id			: unique identifier for each adapter
123a69b74d3SRandy Dunlap  * @irq				: IRQ for this adapter
124a69b74d3SRandy Dunlap  * @ito				: internal timeout value, (-1) means no timeout
125a69b74d3SRandy Dunlap  * @ibuf			: buffer to issue internal commands
126a69b74d3SRandy Dunlap  * @ibuf_dma_h			: dma handle for the above buffer
127a69b74d3SRandy Dunlap  * @uscb_list			: SCB pointers for user cmds, common mgmt module
128a69b74d3SRandy Dunlap  * @uscb_pool			: pool of SCBs for user commands
129a69b74d3SRandy Dunlap  * @uscb_pool_lock		: exclusion lock for these SCBs
130a69b74d3SRandy Dunlap  * @max_cmds			: max outstanding commands
131a69b74d3SRandy Dunlap  * @fw_version			: firmware version
132a69b74d3SRandy Dunlap  * @bios_version		: bios version
133a69b74d3SRandy Dunlap  * @max_cdb_sz			: biggest CDB size supported.
134a69b74d3SRandy Dunlap  * @ha				: is high availability present - clustering
135a69b74d3SRandy Dunlap  * @init_id			: initiator ID, the default value should be 7
136a69b74d3SRandy Dunlap  * @max_sectors			: max sectors per request
137a69b74d3SRandy Dunlap  * @cmd_per_lun			: max outstanding commands per LUN
138a69b74d3SRandy Dunlap  * @being_detached		: set when unloading, no more mgmt calls
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  *
1411da177e4SLinus Torvalds  * mraid_setup_device_map() can be called anytime after the device map is
1421da177e4SLinus Torvalds  * available and MRAID_GET_DEVICE_MAP() can be called whenever the mapping is
1431da177e4SLinus Torvalds  * required, usually from LLD's queue entry point. The formar API sets up the
1441da177e4SLinus Torvalds  * MRAID_IS_LOGICAL(adapter_t *, struct scsi_cmnd *) to find out if the
1451da177e4SLinus Torvalds  * device in question is a logical drive.
1461da177e4SLinus Torvalds  *
1471da177e4SLinus Torvalds  * quiescent flag should be set by the driver if it is not accepting more
1481da177e4SLinus Torvalds  * commands
1491da177e4SLinus Torvalds  *
1501da177e4SLinus Torvalds  * NOTE: The fields of this structures are placed to minimize cache misses
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds // amount of space required to store the bios and firmware version strings
1541da177e4SLinus Torvalds #define VERSION_SIZE	16
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds typedef struct {
1571da177e4SLinus Torvalds 	struct tasklet_struct	dpc_h;
1581da177e4SLinus Torvalds 	struct pci_dev		*pdev;
1591da177e4SLinus Torvalds 	struct Scsi_Host	*host;
1601da177e4SLinus Torvalds 	spinlock_t		lock;
1611da177e4SLinus Torvalds 	uint8_t			quiescent;
1621da177e4SLinus Torvalds 	int			outstanding_cmds;
1631da177e4SLinus Torvalds 	scb_t			*kscb_list;
1641da177e4SLinus Torvalds 	struct list_head	kscb_pool;
1651da177e4SLinus Torvalds 	spinlock_t		kscb_pool_lock;
1661da177e4SLinus Torvalds 	struct list_head	pend_list;
1671da177e4SLinus Torvalds 	spinlock_t		pend_list_lock;
1681da177e4SLinus Torvalds 	struct list_head	completed_list;
1691da177e4SLinus Torvalds 	spinlock_t		completed_list_lock;
1701da177e4SLinus Torvalds 	uint16_t		sglen;
1711da177e4SLinus Torvalds 	int			device_ids[LSI_MAX_CHANNELS]
1721da177e4SLinus Torvalds 					[LSI_MAX_LOGICAL_DRIVES_64LD];
1731da177e4SLinus Torvalds 	caddr_t			raid_device;
1741da177e4SLinus Torvalds 	uint8_t			max_channel;
1751da177e4SLinus Torvalds 	uint16_t		max_target;
1761da177e4SLinus Torvalds 	uint8_t			max_lun;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	uint32_t		unique_id;
179f5ebbeb5SEric W. Biederman 	int			irq;
1801da177e4SLinus Torvalds 	uint8_t			ito;
1811da177e4SLinus Torvalds 	caddr_t			ibuf;
1821da177e4SLinus Torvalds 	dma_addr_t		ibuf_dma_h;
1831da177e4SLinus Torvalds 	scb_t			*uscb_list;
1841da177e4SLinus Torvalds 	struct list_head	uscb_pool;
1851da177e4SLinus Torvalds 	spinlock_t		uscb_pool_lock;
1861da177e4SLinus Torvalds 	int			max_cmds;
1871da177e4SLinus Torvalds 	uint8_t			fw_version[VERSION_SIZE];
1881da177e4SLinus Torvalds 	uint8_t			bios_version[VERSION_SIZE];
1891da177e4SLinus Torvalds 	uint8_t			max_cdb_sz;
1901da177e4SLinus Torvalds 	uint8_t			ha;
1911da177e4SLinus Torvalds 	uint16_t		init_id;
1921da177e4SLinus Torvalds 	uint16_t		max_sectors;
1931da177e4SLinus Torvalds 	uint16_t		cmd_per_lun;
1941da177e4SLinus Torvalds 	atomic_t		being_detached;
1951da177e4SLinus Torvalds } adapter_t;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds #define SCSI_FREE_LIST_LOCK(adapter)	(&adapter->kscb_pool_lock)
1981da177e4SLinus Torvalds #define USER_FREE_LIST_LOCK(adapter)	(&adapter->uscb_pool_lock)
1991da177e4SLinus Torvalds #define PENDING_LIST_LOCK(adapter)	(&adapter->pend_list_lock)
2001da177e4SLinus Torvalds #define COMPLETED_LIST_LOCK(adapter)	(&adapter->completed_list_lock)
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds // conversion from scsi command
2041da177e4SLinus Torvalds #define SCP2HOST(scp)			(scp)->device->host	// to host
2051da177e4SLinus Torvalds #define SCP2HOSTDATA(scp)		SCP2HOST(scp)->hostdata	// to soft state
2061da177e4SLinus Torvalds #define SCP2CHANNEL(scp)		(scp)->device->channel	// to channel
2071da177e4SLinus Torvalds #define SCP2TARGET(scp)			(scp)->device->id	// to target
2081da177e4SLinus Torvalds #define SCP2LUN(scp)			(scp)->device->lun	// to LUN
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds // generic macro to convert scsi command and host to controller's soft state
2111da177e4SLinus Torvalds #define SCSIHOST2ADAP(host)	(((caddr_t *)(host->hostdata))[0])
2121da177e4SLinus Torvalds #define SCP2ADAPTER(scp)	(adapter_t *)SCSIHOST2ADAP(SCP2HOST(scp))
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #define MRAID_IS_LOGICAL(adp, scp)	\
2161da177e4SLinus Torvalds 	(SCP2CHANNEL(scp) == (adp)->max_channel) ? 1 : 0
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds #define MRAID_IS_LOGICAL_SDEV(adp, sdev)	\
2191da177e4SLinus Torvalds 	(sdev->channel == (adp)->max_channel) ? 1 : 0
2201da177e4SLinus Torvalds 
221a69b74d3SRandy Dunlap /**
222a69b74d3SRandy Dunlap  * MRAID_GET_DEVICE_MAP - device ids
223a69b74d3SRandy Dunlap  * @adp			: adapter's soft state
224a69b74d3SRandy Dunlap  * @scp			: mid-layer scsi command pointer
225a69b74d3SRandy Dunlap  * @p_chan		: physical channel on the controller
226a69b74d3SRandy Dunlap  * @target		: target id of the device or logical drive number
227a69b74d3SRandy Dunlap  * @islogical		: set if the command is for the logical drive
228a69b74d3SRandy Dunlap  *
229a69b74d3SRandy Dunlap  * Macro to retrieve information about device class, logical or physical and
230a69b74d3SRandy Dunlap  * the corresponding physical channel and target or logical drive number
231a69b74d3SRandy Dunlap  */
2321da177e4SLinus Torvalds #define MRAID_GET_DEVICE_MAP(adp, scp, p_chan, target, islogical)	\
2331da177e4SLinus Torvalds 	/*								\
2341da177e4SLinus Torvalds 	 * Is the request coming for the virtual channel		\
2351da177e4SLinus Torvalds 	 */								\
2361da177e4SLinus Torvalds 	islogical = MRAID_IS_LOGICAL(adp, scp);				\
2371da177e4SLinus Torvalds 									\
2381da177e4SLinus Torvalds 	/*								\
2391da177e4SLinus Torvalds 	 * Get an index into our table of drive ids mapping		\
2401da177e4SLinus Torvalds 	 */								\
2411da177e4SLinus Torvalds 	if (islogical) {						\
2421da177e4SLinus Torvalds 		p_chan = 0xFF;						\
2431da177e4SLinus Torvalds 		target =						\
2441da177e4SLinus Torvalds 		(adp)->device_ids[(adp)->max_channel][SCP2TARGET(scp)];	\
2451da177e4SLinus Torvalds 	}								\
2461da177e4SLinus Torvalds 	else {								\
2471da177e4SLinus Torvalds 		p_chan = ((adp)->device_ids[SCP2CHANNEL(scp)]		\
2481da177e4SLinus Torvalds 					[SCP2TARGET(scp)] >> 8) & 0xFF;	\
2491da177e4SLinus Torvalds 		target = ((adp)->device_ids[SCP2CHANNEL(scp)]		\
2501da177e4SLinus Torvalds 					[SCP2TARGET(scp)] & 0xFF);	\
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /*
2541da177e4SLinus Torvalds  * ### Helper routines ###
2551da177e4SLinus Torvalds  */
2561da177e4SLinus Torvalds #define LSI_DBGLVL mraid_debug_level	// each LLD must define a global
2571da177e4SLinus Torvalds  					// mraid_debug_level
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds #ifdef DEBUG
2601da177e4SLinus Torvalds #if defined (_ASSERT_PANIC)
2611da177e4SLinus Torvalds #define ASSERT_ACTION	panic
2621da177e4SLinus Torvalds #else
2631da177e4SLinus Torvalds #define ASSERT_ACTION	printk
2641da177e4SLinus Torvalds #endif
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds #define ASSERT(expression)						\
2671da177e4SLinus Torvalds 	if (!(expression)) {						\
2681da177e4SLinus Torvalds 	ASSERT_ACTION("assertion failed:(%s), file: %s, line: %d:%s\n",	\
2691da177e4SLinus Torvalds 			#expression, __FILE__, __LINE__, __FUNCTION__);	\
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds #else
2721da177e4SLinus Torvalds #define ASSERT(expression)
2731da177e4SLinus Torvalds #endif
2741da177e4SLinus Torvalds 
27559f19a9eSRandy Dunlap /**
2761da177e4SLinus Torvalds  * struct mraid_pci_blk - structure holds DMA memory block info
277a69b74d3SRandy Dunlap  * @vaddr		: virtual address to a memory block
278a69b74d3SRandy Dunlap  * @dma_addr		: DMA handle to a memory block
2791da177e4SLinus Torvalds  *
2801da177e4SLinus Torvalds  * This structure is filled up for the caller. It is the responsibilty of the
2811da177e4SLinus Torvalds  * caller to allocate this array big enough to store addresses for all
2821da177e4SLinus Torvalds  * requested elements
2831da177e4SLinus Torvalds  */
2841da177e4SLinus Torvalds struct mraid_pci_blk {
2851da177e4SLinus Torvalds 	caddr_t		vaddr;
2861da177e4SLinus Torvalds 	dma_addr_t	dma_addr;
2871da177e4SLinus Torvalds };
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds #endif // _MEGA_COMMON_H_
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds // vim: set ts=8 sw=8 tw=78:
292