xref: /linux/drivers/scsi/aic7xxx/aic7xxx_osm.h (revision 013791ee)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Adaptec AIC7xxx device driver for Linux.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 1994 John Aycock
51da177e4SLinus Torvalds  *   The University of Calgary Department of Computer Science.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  * the Free Software Foundation; either version 2, or (at your option)
101da177e4SLinus Torvalds  * any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
131da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
141da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151da177e4SLinus Torvalds  * GNU General Public License for more details.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
181da177e4SLinus Torvalds  * along with this program; see the file COPYING.  If not, write to
191da177e4SLinus Torvalds  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Copyright (c) 2000-2003 Adaptec Inc.
221da177e4SLinus Torvalds  * All rights reserved.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Redistribution and use in source and binary forms, with or without
251da177e4SLinus Torvalds  * modification, are permitted provided that the following conditions
261da177e4SLinus Torvalds  * are met:
271da177e4SLinus Torvalds  * 1. Redistributions of source code must retain the above copyright
281da177e4SLinus Torvalds  *    notice, this list of conditions, and the following disclaimer,
291da177e4SLinus Torvalds  *    without modification.
301da177e4SLinus Torvalds  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
311da177e4SLinus Torvalds  *    substantially similar to the "NO WARRANTY" disclaimer below
321da177e4SLinus Torvalds  *    ("Disclaimer") and any redistribution must be conditioned upon
331da177e4SLinus Torvalds  *    including a substantially similar Disclaimer requirement for further
341da177e4SLinus Torvalds  *    binary redistribution.
351da177e4SLinus Torvalds  * 3. Neither the names of the above-listed copyright holders nor the names
361da177e4SLinus Torvalds  *    of any contributors may be used to endorse or promote products derived
371da177e4SLinus Torvalds  *    from this software without specific prior written permission.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * Alternatively, this software may be distributed under the terms of the
401da177e4SLinus Torvalds  * GNU General Public License ("GPL") version 2 as published by the Free
411da177e4SLinus Torvalds  * Software Foundation.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * NO WARRANTY
441da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
451da177e4SLinus Torvalds  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
461da177e4SLinus Torvalds  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
471da177e4SLinus Torvalds  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
481da177e4SLinus Torvalds  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
491da177e4SLinus Torvalds  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
501da177e4SLinus Torvalds  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
511da177e4SLinus Torvalds  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
521da177e4SLinus Torvalds  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
531da177e4SLinus Torvalds  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
541da177e4SLinus Torvalds  * POSSIBILITY OF SUCH DAMAGES.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.h#151 $
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds #ifndef _AIC7XXX_LINUX_H_
601da177e4SLinus Torvalds #define _AIC7XXX_LINUX_H_
611da177e4SLinus Torvalds 
62*013791eeSChristoph Hellwig  #include <linux/config.h>
631da177e4SLinus Torvalds #include <linux/types.h>
641da177e4SLinus Torvalds #include <linux/blkdev.h>
651da177e4SLinus Torvalds #include <linux/delay.h>
661da177e4SLinus Torvalds #include <linux/ioport.h>
671da177e4SLinus Torvalds #include <linux/pci.h>
681da177e4SLinus Torvalds #include <linux/smp_lock.h>
691da177e4SLinus Torvalds #include <linux/version.h>
70e4e360c3SJames Bottomley  #include <linux/interrupt.h>
711da177e4SLinus Torvalds #include <linux/module.h>
72*013791eeSChristoph Hellwig  #include <linux/slab.h>
731da177e4SLinus Torvalds #include <asm/byteorder.h>
741da177e4SLinus Torvalds #include <asm/io.h>
751da177e4SLinus Torvalds 
76*013791eeSChristoph Hellwig  #include <scsi/scsi.h>
77*013791eeSChristoph Hellwig  #include <scsi/scsi_cmnd.h>
78*013791eeSChristoph Hellwig  #include <scsi/scsi_eh.h>
79*013791eeSChristoph Hellwig  #include <scsi/scsi_device.h>
80*013791eeSChristoph Hellwig  #include <scsi/scsi_host.h>
81*013791eeSChristoph Hellwig  #include <scsi/scsi_tcq.h>
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* Core SCSI definitions */
841da177e4SLinus Torvalds #define AIC_LIB_PREFIX ahc
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* Name space conflict with BSD queue macros */
871da177e4SLinus Torvalds #ifdef LIST_HEAD
881da177e4SLinus Torvalds #undef LIST_HEAD
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #include "cam.h"
921da177e4SLinus Torvalds #include "queue.h"
931da177e4SLinus Torvalds #include "scsi_message.h"
941da177e4SLinus Torvalds #include "aiclib.h"
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /*********************************** Debugging ********************************/
971da177e4SLinus Torvalds #ifdef CONFIG_AIC7XXX_DEBUG_ENABLE
981da177e4SLinus Torvalds #ifdef CONFIG_AIC7XXX_DEBUG_MASK
991da177e4SLinus Torvalds #define AHC_DEBUG 1
1001da177e4SLinus Torvalds #define AHC_DEBUG_OPTS CONFIG_AIC7XXX_DEBUG_MASK
1011da177e4SLinus Torvalds #else
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * Compile in debugging code, but do not enable any printfs.
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds #define AHC_DEBUG 1
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds /* No debugging code. */
1081da177e4SLinus Torvalds #endif
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /************************* Forward Declarations *******************************/
1111da177e4SLinus Torvalds struct ahc_softc;
1121da177e4SLinus Torvalds typedef struct pci_dev *ahc_dev_softc_t;
113*013791eeSChristoph Hellwig  typedef struct scsi_cmnd      *ahc_io_ctx_t;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /******************************* Byte Order ***********************************/
1161da177e4SLinus Torvalds #define ahc_htobe16(x)	cpu_to_be16(x)
1171da177e4SLinus Torvalds #define ahc_htobe32(x)	cpu_to_be32(x)
1181da177e4SLinus Torvalds #define ahc_htobe64(x)	cpu_to_be64(x)
1191da177e4SLinus Torvalds #define ahc_htole16(x)	cpu_to_le16(x)
1201da177e4SLinus Torvalds #define ahc_htole32(x)	cpu_to_le32(x)
1211da177e4SLinus Torvalds #define ahc_htole64(x)	cpu_to_le64(x)
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define ahc_be16toh(x)	be16_to_cpu(x)
1241da177e4SLinus Torvalds #define ahc_be32toh(x)	be32_to_cpu(x)
1251da177e4SLinus Torvalds #define ahc_be64toh(x)	be64_to_cpu(x)
1261da177e4SLinus Torvalds #define ahc_le16toh(x)	le16_to_cpu(x)
1271da177e4SLinus Torvalds #define ahc_le32toh(x)	le32_to_cpu(x)
1281da177e4SLinus Torvalds #define ahc_le64toh(x)	le64_to_cpu(x)
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #ifndef LITTLE_ENDIAN
1311da177e4SLinus Torvalds #define LITTLE_ENDIAN 1234
1321da177e4SLinus Torvalds #endif
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #ifndef BIG_ENDIAN
1351da177e4SLinus Torvalds #define BIG_ENDIAN 4321
1361da177e4SLinus Torvalds #endif
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #ifndef BYTE_ORDER
1391da177e4SLinus Torvalds #if defined(__BIG_ENDIAN)
1401da177e4SLinus Torvalds #define BYTE_ORDER BIG_ENDIAN
1411da177e4SLinus Torvalds #endif
1421da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN)
1431da177e4SLinus Torvalds #define BYTE_ORDER LITTLE_ENDIAN
1441da177e4SLinus Torvalds #endif
1451da177e4SLinus Torvalds #endif /* BYTE_ORDER */
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /************************* Configuration Data *********************************/
1481da177e4SLinus Torvalds extern u_int aic7xxx_no_probe;
1491da177e4SLinus Torvalds extern u_int aic7xxx_allow_memio;
1501da177e4SLinus Torvalds extern int aic7xxx_detect_complete;
151*013791eeSChristoph Hellwig  extern struct scsi_host_template aic7xxx_driver_template;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /***************************** Bus Space/DMA **********************************/
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds typedef uint32_t bus_size_t;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds typedef enum {
1581da177e4SLinus Torvalds 	BUS_SPACE_MEMIO,
1591da177e4SLinus Torvalds 	BUS_SPACE_PIO
1601da177e4SLinus Torvalds } bus_space_tag_t;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds typedef union {
1631da177e4SLinus Torvalds 	u_long		  ioport;
1641da177e4SLinus Torvalds 	volatile uint8_t __iomem *maddr;
1651da177e4SLinus Torvalds } bus_space_handle_t;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds typedef struct bus_dma_segment
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	dma_addr_t	ds_addr;
1701da177e4SLinus Torvalds 	bus_size_t	ds_len;
1711da177e4SLinus Torvalds } bus_dma_segment_t;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds struct ahc_linux_dma_tag
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	bus_size_t	alignment;
1761da177e4SLinus Torvalds 	bus_size_t	boundary;
1771da177e4SLinus Torvalds 	bus_size_t	maxsize;
1781da177e4SLinus Torvalds };
1791da177e4SLinus Torvalds typedef struct ahc_linux_dma_tag* bus_dma_tag_t;
1801da177e4SLinus Torvalds 
1817dfa0f26SChristoph Hellwig  typedef dma_addr_t bus_dmamap_t;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds typedef int bus_dma_filter_t(void*, dma_addr_t);
1841da177e4SLinus Torvalds typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds #define BUS_DMA_WAITOK		0x0
1871da177e4SLinus Torvalds #define BUS_DMA_NOWAIT		0x1
1881da177e4SLinus Torvalds #define BUS_DMA_ALLOCNOW	0x2
1891da177e4SLinus Torvalds #define BUS_DMA_LOAD_SEGS	0x4	/*
1901da177e4SLinus Torvalds 					 * Argument is an S/G list not
1911da177e4SLinus Torvalds 					 * a single buffer.
1921da177e4SLinus Torvalds 					 */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds #define BUS_SPACE_MAXADDR	0xFFFFFFFF
1951da177e4SLinus Torvalds #define BUS_SPACE_MAXADDR_32BIT	0xFFFFFFFF
1961da177e4SLinus Torvalds #define BUS_SPACE_MAXSIZE_32BIT	0xFFFFFFFF
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds int	ahc_dma_tag_create(struct ahc_softc *, bus_dma_tag_t /*parent*/,
1991da177e4SLinus Torvalds 			   bus_size_t /*alignment*/, bus_size_t /*boundary*/,
2001da177e4SLinus Torvalds 			   dma_addr_t /*lowaddr*/, dma_addr_t /*highaddr*/,
2011da177e4SLinus Torvalds 			   bus_dma_filter_t*/*filter*/, void */*filterarg*/,
2021da177e4SLinus Torvalds 			   bus_size_t /*maxsize*/, int /*nsegments*/,
2031da177e4SLinus Torvalds 			   bus_size_t /*maxsegsz*/, int /*flags*/,
2041da177e4SLinus Torvalds 			   bus_dma_tag_t */*dma_tagp*/);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds void	ahc_dma_tag_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds int	ahc_dmamem_alloc(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
2091da177e4SLinus Torvalds 			 void** /*vaddr*/, int /*flags*/,
2101da177e4SLinus Torvalds 			 bus_dmamap_t* /*mapp*/);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds void	ahc_dmamem_free(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
2131da177e4SLinus Torvalds 			void* /*vaddr*/, bus_dmamap_t /*map*/);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds void	ahc_dmamap_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/,
2161da177e4SLinus Torvalds 			   bus_dmamap_t /*map*/);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds int	ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t /*dmat*/,
2191da177e4SLinus Torvalds 			bus_dmamap_t /*map*/, void * /*buf*/,
2201da177e4SLinus Torvalds 			bus_size_t /*buflen*/, bus_dmamap_callback_t *,
2211da177e4SLinus Torvalds 			void */*callback_arg*/, int /*flags*/);
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds int	ahc_dmamap_unload(struct ahc_softc *, bus_dma_tag_t, bus_dmamap_t);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /*
2261da177e4SLinus Torvalds  * Operations performed by ahc_dmamap_sync().
2271da177e4SLinus Torvalds  */
2281da177e4SLinus Torvalds #define BUS_DMASYNC_PREREAD	0x01	/* pre-read synchronization */
2291da177e4SLinus Torvalds #define BUS_DMASYNC_POSTREAD	0x02	/* post-read synchronization */
2301da177e4SLinus Torvalds #define BUS_DMASYNC_PREWRITE	0x04	/* pre-write synchronization */
2311da177e4SLinus Torvalds #define BUS_DMASYNC_POSTWRITE	0x08	/* post-write synchronization */
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /*
2341da177e4SLinus Torvalds  * XXX
2351da177e4SLinus Torvalds  * ahc_dmamap_sync is only used on buffers allocated with
2361da177e4SLinus Torvalds  * the pci_alloc_consistent() API.  Although I'm not sure how
2371da177e4SLinus Torvalds  * this works on architectures with a write buffer, Linux does
2381da177e4SLinus Torvalds  * not have an API to sync "coherent" memory.  Perhaps we need
2391da177e4SLinus Torvalds  * to do an mb()?
2401da177e4SLinus Torvalds  */
2411da177e4SLinus Torvalds #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /************************** Timer DataStructures ******************************/
2441da177e4SLinus Torvalds typedef struct timer_list ahc_timer_t;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /********************************** Includes **********************************/
2471da177e4SLinus Torvalds #ifdef CONFIG_AIC7XXX_REG_PRETTY_PRINT
2481da177e4SLinus Torvalds #define AIC_DEBUG_REGISTERS 1
2491da177e4SLinus Torvalds #else
2501da177e4SLinus Torvalds #define AIC_DEBUG_REGISTERS 0
2511da177e4SLinus Torvalds #endif
2521da177e4SLinus Torvalds #include "aic7xxx.h"
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /***************************** Timer Facilities *******************************/
2551da177e4SLinus Torvalds #define ahc_timer_init init_timer
2561da177e4SLinus Torvalds #define ahc_timer_stop del_timer_sync
2571da177e4SLinus Torvalds typedef void ahc_linux_callback_t (u_long);
2581da177e4SLinus Torvalds static __inline void ahc_timer_reset(ahc_timer_t *timer, int usec,
2591da177e4SLinus Torvalds 				     ahc_callback_t *func, void *arg);
2601da177e4SLinus Torvalds static __inline void ahc_scb_timer_reset(struct scb *scb, u_int usec);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static __inline void
2631da177e4SLinus Torvalds ahc_timer_reset(ahc_timer_t *timer, int usec, ahc_callback_t *func, void *arg)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	struct ahc_softc *ahc;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	ahc = (struct ahc_softc *)arg;
2681da177e4SLinus Torvalds 	del_timer(timer);
2691da177e4SLinus Torvalds 	timer->data = (u_long)arg;
2701da177e4SLinus Torvalds 	timer->expires = jiffies + (usec * HZ)/1000000;
2711da177e4SLinus Torvalds 	timer->function = (ahc_linux_callback_t*)func;
2721da177e4SLinus Torvalds 	add_timer(timer);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds static __inline void
2761da177e4SLinus Torvalds ahc_scb_timer_reset(struct scb *scb, u_int usec)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	mod_timer(&scb->io_ctx->eh_timeout, jiffies + (usec * HZ)/1000000);
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /***************************** SMP support ************************************/
2821da177e4SLinus Torvalds #include <linux/spinlock.h>
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds #define AIC7XXX_DRIVER_VERSION "6.2.36"
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /**************************** Front End Queues ********************************/
2871da177e4SLinus Torvalds /*
2881da177e4SLinus Torvalds  * Data structure used to cast the Linux struct scsi_cmnd to something
2891da177e4SLinus Torvalds  * that allows us to use the queue macros.  The linux structure has
2901da177e4SLinus Torvalds  * plenty of space to hold the links fields as required by the queue
2911da177e4SLinus Torvalds  * macros, but the queue macors require them to have the correct type.
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds struct ahc_cmd_internal {
2941da177e4SLinus Torvalds 	/* Area owned by the Linux scsi layer. */
2951da177e4SLinus Torvalds 	uint8_t	private[offsetof(struct scsi_cmnd, SCp.Status)];
2961da177e4SLinus Torvalds 	union {
2971da177e4SLinus Torvalds 		STAILQ_ENTRY(ahc_cmd)	ste;
2981da177e4SLinus Torvalds 		LIST_ENTRY(ahc_cmd)	le;
2991da177e4SLinus Torvalds 		TAILQ_ENTRY(ahc_cmd)	tqe;
3001da177e4SLinus Torvalds 	} links;
3011da177e4SLinus Torvalds 	uint32_t			end;
3021da177e4SLinus Torvalds };
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds struct ahc_cmd {
3051da177e4SLinus Torvalds 	union {
3061da177e4SLinus Torvalds 		struct ahc_cmd_internal	icmd;
3071da177e4SLinus Torvalds 		struct scsi_cmnd	scsi_cmd;
3081da177e4SLinus Torvalds 	} un;
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds #define acmd_icmd(cmd) ((cmd)->un.icmd)
3121da177e4SLinus Torvalds #define acmd_scsi_cmd(cmd) ((cmd)->un.scsi_cmd)
3131da177e4SLinus Torvalds #define acmd_links un.icmd.links
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds /*************************** Device Data Structures ***************************/
3161da177e4SLinus Torvalds /*
3171da177e4SLinus Torvalds  * A per probed device structure used to deal with some error recovery
3181da177e4SLinus Torvalds  * scenarios that the Linux mid-layer code just doesn't know how to
3191da177e4SLinus Torvalds  * handle.  The structure allocated for a device only becomes persistent
3201da177e4SLinus Torvalds  * after a successfully completed inquiry command to the target when
3211da177e4SLinus Torvalds  * that inquiry data indicates a lun is present.
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds TAILQ_HEAD(ahc_busyq, ahc_cmd);
3241da177e4SLinus Torvalds typedef enum {
3251da177e4SLinus Torvalds 	AHC_DEV_UNCONFIGURED	 = 0x01,
3261da177e4SLinus Torvalds 	AHC_DEV_FREEZE_TIL_EMPTY = 0x02, /* Freeze queue until active == 0 */
3271da177e4SLinus Torvalds 	AHC_DEV_TIMER_ACTIVE	 = 0x04, /* Our timer is active */
3281da177e4SLinus Torvalds 	AHC_DEV_ON_RUN_LIST	 = 0x08, /* Queued to be run later */
3291da177e4SLinus Torvalds 	AHC_DEV_Q_BASIC		 = 0x10, /* Allow basic device queuing */
3301da177e4SLinus Torvalds 	AHC_DEV_Q_TAGGED	 = 0x20, /* Allow full SCSI2 command queueing */
3311da177e4SLinus Torvalds 	AHC_DEV_PERIODIC_OTAG	 = 0x40, /* Send OTAG to prevent starvation */
3321da177e4SLinus Torvalds 	AHC_DEV_SLAVE_CONFIGURED = 0x80	 /* slave_configure() has been called */
3331da177e4SLinus Torvalds } ahc_linux_dev_flags;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds struct ahc_linux_target;
3361da177e4SLinus Torvalds struct ahc_linux_device {
3371da177e4SLinus Torvalds 	TAILQ_ENTRY(ahc_linux_device) links;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/*
3401da177e4SLinus Torvalds 	 * The number of transactions currently
3411da177e4SLinus Torvalds 	 * queued to the device.
3421da177e4SLinus Torvalds 	 */
3431da177e4SLinus Torvalds 	int			active;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	/*
3461da177e4SLinus Torvalds 	 * The currently allowed number of
3471da177e4SLinus Torvalds 	 * transactions that can be queued to
3481da177e4SLinus Torvalds 	 * the device.  Must be signed for
3491da177e4SLinus Torvalds 	 * conversion from tagged to untagged
3501da177e4SLinus Torvalds 	 * mode where the device may have more
3511da177e4SLinus Torvalds 	 * than one outstanding active transaction.
3521da177e4SLinus Torvalds 	 */
3531da177e4SLinus Torvalds 	int			openings;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	/*
3561da177e4SLinus Torvalds 	 * A positive count indicates that this
3571da177e4SLinus Torvalds 	 * device's queue is halted.
3581da177e4SLinus Torvalds 	 */
3591da177e4SLinus Torvalds 	u_int			qfrozen;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/*
3621da177e4SLinus Torvalds 	 * Cumulative command counter.
3631da177e4SLinus Torvalds 	 */
3641da177e4SLinus Torvalds 	u_long			commands_issued;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	/*
3671da177e4SLinus Torvalds 	 * The number of tagged transactions when
3681da177e4SLinus Torvalds 	 * running at our current opening level
3691da177e4SLinus Torvalds 	 * that have been successfully received by
3701da177e4SLinus Torvalds 	 * this device since the last QUEUE FULL.
3711da177e4SLinus Torvalds 	 */
3721da177e4SLinus Torvalds 	u_int			tag_success_count;
3731da177e4SLinus Torvalds #define AHC_TAG_SUCCESS_INTERVAL 50
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	ahc_linux_dev_flags	flags;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/*
3781da177e4SLinus Torvalds 	 * Per device timer.
3791da177e4SLinus Torvalds 	 */
3801da177e4SLinus Torvalds 	struct timer_list	timer;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	/*
3831da177e4SLinus Torvalds 	 * The high limit for the tags variable.
3841da177e4SLinus Torvalds 	 */
3851da177e4SLinus Torvalds 	u_int			maxtags;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/*
3881da177e4SLinus Torvalds 	 * The computed number of tags outstanding
3891da177e4SLinus Torvalds 	 * at the time of the last QUEUE FULL event.
3901da177e4SLinus Torvalds 	 */
3911da177e4SLinus Torvalds 	u_int			tags_on_last_queuefull;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/*
3941da177e4SLinus Torvalds 	 * How many times we have seen a queue full
3951da177e4SLinus Torvalds 	 * with the same number of tags.  This is used
3961da177e4SLinus Torvalds 	 * to stop our adaptive queue depth algorithm
3971da177e4SLinus Torvalds 	 * on devices with a fixed number of tags.
3981da177e4SLinus Torvalds 	 */
3991da177e4SLinus Torvalds 	u_int			last_queuefull_same_count;
4001da177e4SLinus Torvalds #define AHC_LOCK_TAGS_COUNT 50
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	/*
4031da177e4SLinus Torvalds 	 * How many transactions have been queued
4041da177e4SLinus Torvalds 	 * without the device going idle.  We use
4051da177e4SLinus Torvalds 	 * this statistic to determine when to issue
4061da177e4SLinus Torvalds 	 * an ordered tag to prevent transaction
4071da177e4SLinus Torvalds 	 * starvation.  This statistic is only updated
4081da177e4SLinus Torvalds 	 * if the AHC_DEV_PERIODIC_OTAG flag is set
4091da177e4SLinus Torvalds 	 * on this device.
4101da177e4SLinus Torvalds 	 */
4111da177e4SLinus Torvalds 	u_int			commands_since_idle_or_otag;
4121da177e4SLinus Torvalds #define AHC_OTAG_THRESH	500
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	int			lun;
415*013791eeSChristoph Hellwig  	struct scsi_device       *scsi_device;
4161da177e4SLinus Torvalds 	struct			ahc_linux_target *target;
4171da177e4SLinus Torvalds };
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds struct ahc_linux_target {
4201da177e4SLinus Torvalds 	struct ahc_linux_device	 *devices[AHC_NUM_LUNS];
4211da177e4SLinus Torvalds 	int			  channel;
4221da177e4SLinus Torvalds 	int			  target;
4231da177e4SLinus Torvalds 	int			  refcount;
4241da177e4SLinus Torvalds 	struct ahc_transinfo	  last_tinfo;
4251da177e4SLinus Torvalds 	struct ahc_softc	 *ahc;
4261da177e4SLinus Torvalds };
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds /********************* Definitions Required by the Core ***********************/
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds  * Number of SG segments we require.  So long as the S/G segments for
4311da177e4SLinus Torvalds  * a particular transaction are allocated in a physically contiguous
4321da177e4SLinus Torvalds  * manner and are allocated below 4GB, the number of S/G segments is
4331da177e4SLinus Torvalds  * unrestricted.
4341da177e4SLinus Torvalds  */
4351da177e4SLinus Torvalds #define	AHC_NSEG 128
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /*
4381da177e4SLinus Torvalds  * Per-SCB OSM storage.
4391da177e4SLinus Torvalds  */
4401da177e4SLinus Torvalds typedef enum {
4411da177e4SLinus Torvalds 	AHC_UP_EH_SEMAPHORE = 0x1
4421da177e4SLinus Torvalds } ahc_linux_scb_flags;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds struct scb_platform_data {
4451da177e4SLinus Torvalds 	struct ahc_linux_device	*dev;
4461da177e4SLinus Torvalds 	dma_addr_t		 buf_busaddr;
4471da177e4SLinus Torvalds 	uint32_t		 xfer_len;
4481da177e4SLinus Torvalds 	uint32_t		 sense_resid;	/* Auto-Sense residual */
4491da177e4SLinus Torvalds 	ahc_linux_scb_flags	 flags;
4501da177e4SLinus Torvalds };
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * Define a structure used for each host adapter.  All members are
4541da177e4SLinus Torvalds  * aligned on a boundary >= the size of the member to honor the
4551da177e4SLinus Torvalds  * alignment restrictions of the various platforms supported by
4561da177e4SLinus Torvalds  * this driver.
4571da177e4SLinus Torvalds  */
4581da177e4SLinus Torvalds typedef enum {
4591da177e4SLinus Torvalds 	AHC_RUN_CMPLT_Q_TIMER	 = 0x10
4601da177e4SLinus Torvalds } ahc_linux_softc_flags;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds TAILQ_HEAD(ahc_completeq, ahc_cmd);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds struct ahc_platform_data {
4651da177e4SLinus Torvalds 	/*
4661da177e4SLinus Torvalds 	 * Fields accessed from interrupt context.
4671da177e4SLinus Torvalds 	 */
4681da177e4SLinus Torvalds 	struct ahc_linux_target *targets[AHC_NUM_TARGETS];
4691da177e4SLinus Torvalds 	TAILQ_HEAD(, ahc_linux_device) device_runq;
4701da177e4SLinus Torvalds 	struct ahc_completeq	 completeq;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	spinlock_t		 spin_lock;
4731da177e4SLinus Torvalds 	u_int			 qfrozen;
4741da177e4SLinus Torvalds 	struct timer_list	 completeq_timer;
4751da177e4SLinus Torvalds 	struct timer_list	 reset_timer;
4761da177e4SLinus Torvalds 	struct semaphore	 eh_sem;
4771da177e4SLinus Torvalds 	struct Scsi_Host        *host;		/* pointer to scsi host */
4781da177e4SLinus Torvalds #define AHC_LINUX_NOIRQ	((uint32_t)~0)
4791da177e4SLinus Torvalds 	uint32_t		 irq;		/* IRQ for this adapter */
4801da177e4SLinus Torvalds 	uint32_t		 bios_address;
4811da177e4SLinus Torvalds 	uint32_t		 mem_busaddr;	/* Mem Base Addr */
4821da177e4SLinus Torvalds 	ahc_linux_softc_flags	 flags;
4831da177e4SLinus Torvalds };
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /************************** OS Utility Wrappers *******************************/
4861da177e4SLinus Torvalds #define printf printk
4871da177e4SLinus Torvalds #define M_NOWAIT GFP_ATOMIC
4881da177e4SLinus Torvalds #define M_WAITOK 0
4891da177e4SLinus Torvalds #define malloc(size, type, flags) kmalloc(size, flags)
4901da177e4SLinus Torvalds #define free(ptr, type) kfree(ptr)
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds static __inline void ahc_delay(long);
4931da177e4SLinus Torvalds static __inline void
4941da177e4SLinus Torvalds ahc_delay(long usec)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	/*
4971da177e4SLinus Torvalds 	 * udelay on Linux can have problems for
4981da177e4SLinus Torvalds 	 * multi-millisecond waits.  Wait at most
4991da177e4SLinus Torvalds 	 * 1024us per call.
5001da177e4SLinus Torvalds 	 */
5011da177e4SLinus Torvalds 	while (usec > 0) {
5021da177e4SLinus Torvalds 		udelay(usec % 1024);
5031da177e4SLinus Torvalds 		usec -= 1024;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds /***************************** Low Level I/O **********************************/
5091da177e4SLinus Torvalds static __inline uint8_t ahc_inb(struct ahc_softc * ahc, long port);
5101da177e4SLinus Torvalds static __inline void ahc_outb(struct ahc_softc * ahc, long port, uint8_t val);
5111da177e4SLinus Torvalds static __inline void ahc_outsb(struct ahc_softc * ahc, long port,
5121da177e4SLinus Torvalds 			       uint8_t *, int count);
5131da177e4SLinus Torvalds static __inline void ahc_insb(struct ahc_softc * ahc, long port,
5141da177e4SLinus Torvalds 			       uint8_t *, int count);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static __inline uint8_t
5171da177e4SLinus Torvalds ahc_inb(struct ahc_softc * ahc, long port)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	uint8_t x;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (ahc->tag == BUS_SPACE_MEMIO) {
5221da177e4SLinus Torvalds 		x = readb(ahc->bsh.maddr + port);
5231da177e4SLinus Torvalds 	} else {
5241da177e4SLinus Torvalds 		x = inb(ahc->bsh.ioport + port);
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 	mb();
5271da177e4SLinus Torvalds 	return (x);
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds static __inline void
5311da177e4SLinus Torvalds ahc_outb(struct ahc_softc * ahc, long port, uint8_t val)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	if (ahc->tag == BUS_SPACE_MEMIO) {
5341da177e4SLinus Torvalds 		writeb(val, ahc->bsh.maddr + port);
5351da177e4SLinus Torvalds 	} else {
5361da177e4SLinus Torvalds 		outb(val, ahc->bsh.ioport + port);
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 	mb();
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static __inline void
5421da177e4SLinus Torvalds ahc_outsb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	int i;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/*
5471da177e4SLinus Torvalds 	 * There is probably a more efficient way to do this on Linux
5481da177e4SLinus Torvalds 	 * but we don't use this for anything speed critical and this
5491da177e4SLinus Torvalds 	 * should work.
5501da177e4SLinus Torvalds 	 */
5511da177e4SLinus Torvalds 	for (i = 0; i < count; i++)
5521da177e4SLinus Torvalds 		ahc_outb(ahc, port, *array++);
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds static __inline void
5561da177e4SLinus Torvalds ahc_insb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
5571da177e4SLinus Torvalds {
5581da177e4SLinus Torvalds 	int i;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	/*
5611da177e4SLinus Torvalds 	 * There is probably a more efficient way to do this on Linux
5621da177e4SLinus Torvalds 	 * but we don't use this for anything speed critical and this
5631da177e4SLinus Torvalds 	 * should work.
5641da177e4SLinus Torvalds 	 */
5651da177e4SLinus Torvalds 	for (i = 0; i < count; i++)
5661da177e4SLinus Torvalds 		*array++ = ahc_inb(ahc, port);
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /**************************** Initialization **********************************/
5701da177e4SLinus Torvalds int		ahc_linux_register_host(struct ahc_softc *,
571*013791eeSChristoph Hellwig  					struct scsi_host_template *);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds uint64_t	ahc_linux_get_memsize(void);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds /*************************** Pretty Printing **********************************/
5761da177e4SLinus Torvalds struct info_str {
5771da177e4SLinus Torvalds 	char *buffer;
5781da177e4SLinus Torvalds 	int length;
5791da177e4SLinus Torvalds 	off_t offset;
5801da177e4SLinus Torvalds 	int pos;
5811da177e4SLinus Torvalds };
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds void	ahc_format_transinfo(struct info_str *info,
5841da177e4SLinus Torvalds 			     struct ahc_transinfo *tinfo);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /******************************** Locking *************************************/
5871da177e4SLinus Torvalds /* Lock protecting internal data structures */
5881da177e4SLinus Torvalds static __inline void ahc_lockinit(struct ahc_softc *);
5891da177e4SLinus Torvalds static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
5901da177e4SLinus Torvalds static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds /* Lock held during ahc_list manipulation and ahc softc frees */
5931da177e4SLinus Torvalds extern spinlock_t ahc_list_spinlock;
5941da177e4SLinus Torvalds static __inline void ahc_list_lockinit(void);
5951da177e4SLinus Torvalds static __inline void ahc_list_lock(unsigned long *flags);
5961da177e4SLinus Torvalds static __inline void ahc_list_unlock(unsigned long *flags);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds static __inline void
5991da177e4SLinus Torvalds ahc_lockinit(struct ahc_softc *ahc)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	spin_lock_init(&ahc->platform_data->spin_lock);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds static __inline void
6051da177e4SLinus Torvalds ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	spin_lock_irqsave(&ahc->platform_data->spin_lock, *flags);
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds static __inline void
6111da177e4SLinus Torvalds ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	spin_unlock_irqrestore(&ahc->platform_data->spin_lock, *flags);
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds static __inline void
6171da177e4SLinus Torvalds ahc_list_lockinit(void)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	spin_lock_init(&ahc_list_spinlock);
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds static __inline void
6231da177e4SLinus Torvalds ahc_list_lock(unsigned long *flags)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	spin_lock_irqsave(&ahc_list_spinlock, *flags);
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds static __inline void
6291da177e4SLinus Torvalds ahc_list_unlock(unsigned long *flags)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	spin_unlock_irqrestore(&ahc_list_spinlock, *flags);
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds /******************************* PCI Definitions ******************************/
6351da177e4SLinus Torvalds /*
6361da177e4SLinus Torvalds  * PCIM_xxx: mask to locate subfield in register
6371da177e4SLinus Torvalds  * PCIR_xxx: config register offset
6381da177e4SLinus Torvalds  * PCIC_xxx: device class
6391da177e4SLinus Torvalds  * PCIS_xxx: device subclass
6401da177e4SLinus Torvalds  * PCIP_xxx: device programming interface
6411da177e4SLinus Torvalds  * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
6421da177e4SLinus Torvalds  * PCID_xxx: device ID
6431da177e4SLinus Torvalds  */
6441da177e4SLinus Torvalds #define PCIR_DEVVENDOR		0x00
6451da177e4SLinus Torvalds #define PCIR_VENDOR		0x00
6461da177e4SLinus Torvalds #define PCIR_DEVICE		0x02
6471da177e4SLinus Torvalds #define PCIR_COMMAND		0x04
6481da177e4SLinus Torvalds #define PCIM_CMD_PORTEN		0x0001
6491da177e4SLinus Torvalds #define PCIM_CMD_MEMEN		0x0002
6501da177e4SLinus Torvalds #define PCIM_CMD_BUSMASTEREN	0x0004
6511da177e4SLinus Torvalds #define PCIM_CMD_MWRICEN	0x0010
6521da177e4SLinus Torvalds #define PCIM_CMD_PERRESPEN	0x0040
6531da177e4SLinus Torvalds #define	PCIM_CMD_SERRESPEN	0x0100
6541da177e4SLinus Torvalds #define PCIR_STATUS		0x06
6551da177e4SLinus Torvalds #define PCIR_REVID		0x08
6561da177e4SLinus Torvalds #define PCIR_PROGIF		0x09
6571da177e4SLinus Torvalds #define PCIR_SUBCLASS		0x0a
6581da177e4SLinus Torvalds #define PCIR_CLASS		0x0b
6591da177e4SLinus Torvalds #define PCIR_CACHELNSZ		0x0c
6601da177e4SLinus Torvalds #define PCIR_LATTIMER		0x0d
6611da177e4SLinus Torvalds #define PCIR_HEADERTYPE		0x0e
6621da177e4SLinus Torvalds #define PCIM_MFDEV		0x80
6631da177e4SLinus Torvalds #define PCIR_BIST		0x0f
6641da177e4SLinus Torvalds #define PCIR_CAP_PTR		0x34
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds /* config registers for header type 0 devices */
6671da177e4SLinus Torvalds #define PCIR_MAPS	0x10
6681da177e4SLinus Torvalds #define PCIR_SUBVEND_0	0x2c
6691da177e4SLinus Torvalds #define PCIR_SUBDEV_0	0x2e
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds extern struct pci_driver aic7xxx_pci_driver;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds typedef enum
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	AHC_POWER_STATE_D0,
6761da177e4SLinus Torvalds 	AHC_POWER_STATE_D1,
6771da177e4SLinus Torvalds 	AHC_POWER_STATE_D2,
6781da177e4SLinus Torvalds 	AHC_POWER_STATE_D3
6791da177e4SLinus Torvalds } ahc_power_state;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds /**************************** VL/EISA Routines ********************************/
6821da177e4SLinus Torvalds #ifdef CONFIG_EISA
6831da177e4SLinus Torvalds extern uint32_t aic7xxx_probe_eisa_vl;
6841da177e4SLinus Torvalds int			 ahc_linux_eisa_init(void);
6851da177e4SLinus Torvalds void			 ahc_linux_eisa_exit(void);
6861da177e4SLinus Torvalds int			 aic7770_map_registers(struct ahc_softc *ahc,
6871da177e4SLinus Torvalds 					       u_int port);
6881da177e4SLinus Torvalds int			 aic7770_map_int(struct ahc_softc *ahc, u_int irq);
6891da177e4SLinus Torvalds #else
6901da177e4SLinus Torvalds static inline int	ahc_linux_eisa_init(void) {
6911da177e4SLinus Torvalds 	return -ENODEV;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds static inline void	ahc_linux_eisa_exit(void) {
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds #endif
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds /******************************* PCI Routines *********************************/
6981da177e4SLinus Torvalds #ifdef CONFIG_PCI
6991da177e4SLinus Torvalds int			 ahc_linux_pci_init(void);
7001da177e4SLinus Torvalds void			 ahc_linux_pci_exit(void);
7011da177e4SLinus Torvalds int			 ahc_pci_map_registers(struct ahc_softc *ahc);
7021da177e4SLinus Torvalds int			 ahc_pci_map_int(struct ahc_softc *ahc);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
7051da177e4SLinus Torvalds 					     int reg, int width);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static __inline uint32_t
7081da177e4SLinus Torvalds ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	switch (width) {
7111da177e4SLinus Torvalds 	case 1:
7121da177e4SLinus Torvalds 	{
7131da177e4SLinus Torvalds 		uint8_t retval;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 		pci_read_config_byte(pci, reg, &retval);
7161da177e4SLinus Torvalds 		return (retval);
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 	case 2:
7191da177e4SLinus Torvalds 	{
7201da177e4SLinus Torvalds 		uint16_t retval;
7211da177e4SLinus Torvalds 		pci_read_config_word(pci, reg, &retval);
7221da177e4SLinus Torvalds 		return (retval);
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 	case 4:
7251da177e4SLinus Torvalds 	{
7261da177e4SLinus Torvalds 		uint32_t retval;
7271da177e4SLinus Torvalds 		pci_read_config_dword(pci, reg, &retval);
7281da177e4SLinus Torvalds 		return (retval);
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 	default:
7311da177e4SLinus Torvalds 		panic("ahc_pci_read_config: Read size too big");
7321da177e4SLinus Torvalds 		/* NOTREACHED */
7331da177e4SLinus Torvalds 		return (0);
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
7381da177e4SLinus Torvalds 					  int reg, uint32_t value,
7391da177e4SLinus Torvalds 					  int width);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds static __inline void
7421da177e4SLinus Torvalds ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	switch (width) {
7451da177e4SLinus Torvalds 	case 1:
7461da177e4SLinus Torvalds 		pci_write_config_byte(pci, reg, value);
7471da177e4SLinus Torvalds 		break;
7481da177e4SLinus Torvalds 	case 2:
7491da177e4SLinus Torvalds 		pci_write_config_word(pci, reg, value);
7501da177e4SLinus Torvalds 		break;
7511da177e4SLinus Torvalds 	case 4:
7521da177e4SLinus Torvalds 		pci_write_config_dword(pci, reg, value);
7531da177e4SLinus Torvalds 		break;
7541da177e4SLinus Torvalds 	default:
7551da177e4SLinus Torvalds 		panic("ahc_pci_write_config: Write size too big");
7561da177e4SLinus Torvalds 		/* NOTREACHED */
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds static __inline int ahc_get_pci_function(ahc_dev_softc_t);
7611da177e4SLinus Torvalds static __inline int
7621da177e4SLinus Torvalds ahc_get_pci_function(ahc_dev_softc_t pci)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	return (PCI_FUNC(pci->devfn));
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
7681da177e4SLinus Torvalds static __inline int
7691da177e4SLinus Torvalds ahc_get_pci_slot(ahc_dev_softc_t pci)
7701da177e4SLinus Torvalds {
7711da177e4SLinus Torvalds 	return (PCI_SLOT(pci->devfn));
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
7751da177e4SLinus Torvalds static __inline int
7761da177e4SLinus Torvalds ahc_get_pci_bus(ahc_dev_softc_t pci)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	return (pci->bus->number);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds #else
7811da177e4SLinus Torvalds static inline int ahc_linux_pci_init(void) {
7821da177e4SLinus Torvalds 	return 0;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds static inline void ahc_linux_pci_exit(void) {
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds #endif
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static __inline void ahc_flush_device_writes(struct ahc_softc *);
7891da177e4SLinus Torvalds static __inline void
7901da177e4SLinus Torvalds ahc_flush_device_writes(struct ahc_softc *ahc)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	/* XXX Is this sufficient for all architectures??? */
7931da177e4SLinus Torvalds 	ahc_inb(ahc, INTSTAT);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /**************************** Proc FS Support *********************************/
7971da177e4SLinus Torvalds int	ahc_linux_proc_info(struct Scsi_Host *, char *, char **,
7981da177e4SLinus Torvalds 			    off_t, int, int);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /*************************** Domain Validation ********************************/
8011da177e4SLinus Torvalds /*********************** Transaction Access Wrappers *************************/
802*013791eeSChristoph Hellwig  static __inline void ahc_cmd_set_transaction_status(struct scsi_cmnd *, uint32_t);
8031da177e4SLinus Torvalds static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
804*013791eeSChristoph Hellwig  static __inline void ahc_cmd_set_scsi_status(struct scsi_cmnd *, uint32_t);
8051da177e4SLinus Torvalds static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
806*013791eeSChristoph Hellwig  static __inline uint32_t ahc_cmd_get_transaction_status(struct scsi_cmnd *cmd);
8071da177e4SLinus Torvalds static __inline uint32_t ahc_get_transaction_status(struct scb *);
808*013791eeSChristoph Hellwig  static __inline uint32_t ahc_cmd_get_scsi_status(struct scsi_cmnd *cmd);
8091da177e4SLinus Torvalds static __inline uint32_t ahc_get_scsi_status(struct scb *);
8101da177e4SLinus Torvalds static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
8111da177e4SLinus Torvalds static __inline u_long ahc_get_transfer_length(struct scb *);
8121da177e4SLinus Torvalds static __inline int ahc_get_transfer_dir(struct scb *);
8131da177e4SLinus Torvalds static __inline void ahc_set_residual(struct scb *, u_long);
8141da177e4SLinus Torvalds static __inline void ahc_set_sense_residual(struct scb *scb, u_long resid);
8151da177e4SLinus Torvalds static __inline u_long ahc_get_residual(struct scb *);
8161da177e4SLinus Torvalds static __inline u_long ahc_get_sense_residual(struct scb *);
8171da177e4SLinus Torvalds static __inline int ahc_perform_autosense(struct scb *);
8181da177e4SLinus Torvalds static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
8191da177e4SLinus Torvalds 					       struct scb *);
8201da177e4SLinus Torvalds static __inline void ahc_notify_xfer_settings_change(struct ahc_softc *,
8211da177e4SLinus Torvalds 						     struct ahc_devinfo *);
8221da177e4SLinus Torvalds static __inline void ahc_platform_scb_free(struct ahc_softc *ahc,
8231da177e4SLinus Torvalds 					   struct scb *scb);
8241da177e4SLinus Torvalds static __inline void ahc_freeze_scb(struct scb *scb);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds static __inline
827*013791eeSChristoph Hellwig  void ahc_cmd_set_transaction_status(struct scsi_cmnd *cmd, uint32_t status)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	cmd->result &= ~(CAM_STATUS_MASK << 16);
8301da177e4SLinus Torvalds 	cmd->result |= status << 16;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds static __inline
8341da177e4SLinus Torvalds void ahc_set_transaction_status(struct scb *scb, uint32_t status)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	ahc_cmd_set_transaction_status(scb->io_ctx,status);
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds static __inline
840*013791eeSChristoph Hellwig  void ahc_cmd_set_scsi_status(struct scsi_cmnd *cmd, uint32_t status)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	cmd->result &= ~0xFFFF;
8431da177e4SLinus Torvalds 	cmd->result |= status;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds static __inline
8471da177e4SLinus Torvalds void ahc_set_scsi_status(struct scb *scb, uint32_t status)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	ahc_cmd_set_scsi_status(scb->io_ctx, status);
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds static __inline
853*013791eeSChristoph Hellwig  uint32_t ahc_cmd_get_transaction_status(struct scsi_cmnd *cmd)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	return ((cmd->result >> 16) & CAM_STATUS_MASK);
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static __inline
8591da177e4SLinus Torvalds uint32_t ahc_get_transaction_status(struct scb *scb)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	return (ahc_cmd_get_transaction_status(scb->io_ctx));
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds static __inline
865*013791eeSChristoph Hellwig  uint32_t ahc_cmd_get_scsi_status(struct scsi_cmnd *cmd)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	return (cmd->result & 0xFFFF);
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds static __inline
8711da177e4SLinus Torvalds uint32_t ahc_get_scsi_status(struct scb *scb)
8721da177e4SLinus Torvalds {
8731da177e4SLinus Torvalds 	return (ahc_cmd_get_scsi_status(scb->io_ctx));
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds static __inline
8771da177e4SLinus Torvalds void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	/*
8801da177e4SLinus Torvalds 	 * Nothing to do for linux as the incoming transaction
8811da177e4SLinus Torvalds 	 * has no concept of tag/non tagged, etc.
8821da177e4SLinus Torvalds 	 */
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds static __inline
8861da177e4SLinus Torvalds u_long ahc_get_transfer_length(struct scb *scb)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	return (scb->platform_data->xfer_len);
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds static __inline
8921da177e4SLinus Torvalds int ahc_get_transfer_dir(struct scb *scb)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	return (scb->io_ctx->sc_data_direction);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static __inline
8981da177e4SLinus Torvalds void ahc_set_residual(struct scb *scb, u_long resid)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	scb->io_ctx->resid = resid;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds static __inline
9041da177e4SLinus Torvalds void ahc_set_sense_residual(struct scb *scb, u_long resid)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	scb->platform_data->sense_resid = resid;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds static __inline
9101da177e4SLinus Torvalds u_long ahc_get_residual(struct scb *scb)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	return (scb->io_ctx->resid);
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds static __inline
9161da177e4SLinus Torvalds u_long ahc_get_sense_residual(struct scb *scb)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	return (scb->platform_data->sense_resid);
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds static __inline
9221da177e4SLinus Torvalds int ahc_perform_autosense(struct scb *scb)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds 	/*
9251da177e4SLinus Torvalds 	 * We always perform autosense in Linux.
9261da177e4SLinus Torvalds 	 * On other platforms this is set on a
9271da177e4SLinus Torvalds 	 * per-transaction basis.
9281da177e4SLinus Torvalds 	 */
9291da177e4SLinus Torvalds 	return (1);
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds static __inline uint32_t
9331da177e4SLinus Torvalds ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	return (sizeof(struct scsi_sense_data));
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds static __inline void
9391da177e4SLinus Torvalds ahc_notify_xfer_settings_change(struct ahc_softc *ahc,
9401da177e4SLinus Torvalds 				struct ahc_devinfo *devinfo)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	/* Nothing to do here for linux */
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds static __inline void
9461da177e4SLinus Torvalds ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds int	ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
9521da177e4SLinus Torvalds void	ahc_platform_free(struct ahc_softc *ahc);
9531da177e4SLinus Torvalds void	ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds static __inline void
9561da177e4SLinus Torvalds ahc_freeze_scb(struct scb *scb)
9571da177e4SLinus Torvalds {
9581da177e4SLinus Torvalds 	if ((scb->io_ctx->result & (CAM_DEV_QFRZN << 16)) == 0) {
9591da177e4SLinus Torvalds                 scb->io_ctx->result |= CAM_DEV_QFRZN << 16;
9601da177e4SLinus Torvalds                 scb->platform_data->dev->qfrozen++;
9611da177e4SLinus Torvalds         }
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds void	ahc_platform_set_tags(struct ahc_softc *ahc,
9651da177e4SLinus Torvalds 			      struct ahc_devinfo *devinfo, ahc_queue_alg);
9661da177e4SLinus Torvalds int	ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
9671da177e4SLinus Torvalds 				char channel, int lun, u_int tag,
9681da177e4SLinus Torvalds 				role_t role, uint32_t status);
9691da177e4SLinus Torvalds irqreturn_t
9701da177e4SLinus Torvalds 	ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
9711da177e4SLinus Torvalds void	ahc_platform_flushwork(struct ahc_softc *ahc);
9721da177e4SLinus Torvalds int	ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
9731da177e4SLinus Torvalds void	ahc_done(struct ahc_softc*, struct scb*);
9741da177e4SLinus Torvalds void	ahc_send_async(struct ahc_softc *, char channel,
9751da177e4SLinus Torvalds 		       u_int target, u_int lun, ac_code, void *);
9761da177e4SLinus Torvalds void	ahc_print_path(struct ahc_softc *, struct scb *);
9771da177e4SLinus Torvalds void	ahc_platform_dump_card_state(struct ahc_softc *ahc);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds #ifdef CONFIG_PCI
9801da177e4SLinus Torvalds #define AHC_PCI_CONFIG 1
9811da177e4SLinus Torvalds #else
9821da177e4SLinus Torvalds #define AHC_PCI_CONFIG 0
9831da177e4SLinus Torvalds #endif
9841da177e4SLinus Torvalds #define bootverbose aic7xxx_verbose
9851da177e4SLinus Torvalds extern u_int aic7xxx_verbose;
9861da177e4SLinus Torvalds #endif /* _AIC7XXX_LINUX_H_ */
987