File: /usr/src/linux/drivers/scsi/aic7xxx/aic7xxx_osm.h

1     /*
2      * Adaptec AIC7xxx device driver for Linux.
3      *
4      * Copyright (c) 1994 John Aycock
5      *   The University of Calgary Department of Computer Science.
6      *
7      * This program is free software; you can redistribute it and/or modify
8      * it under the terms of the GNU General Public License as published by
9      * the Free Software Foundation; either version 2, or (at your option)
10      * any later version.
11      *
12      * This program is distributed in the hope that it will be useful,
13      * but WITHOUT ANY WARRANTY; without even the implied warranty of
14      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15      * GNU General Public License for more details.
16      *
17      * You should have received a copy of the GNU General Public License
18      * along with this program; see the file COPYING.  If not, write to
19      * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20      * 
21      * $Id: //depot/src/linux/drivers/scsi/aic7xxx/aic7xxx_linux.h#65 $
22      *
23      * Copyright (c) 2000, 2001 Adaptec Inc.
24      * All rights reserved.
25      *
26      * Redistribution and use in source and binary forms, with or without
27      * modification, are permitted provided that the following conditions
28      * are met:
29      * 1. Redistributions of source code must retain the above copyright
30      *    notice, this list of conditions, and the following disclaimer,
31      *    without modification.
32      * 2. The name of the author may not be used to endorse or promote products
33      *    derived from this software without specific prior written permission.
34      *
35      * Alternatively, this software may be distributed under the terms of the
36      * GNU Public License ("GPL").
37      *
38      * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
39      * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40      * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41      * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
42      * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43      * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44      * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45      * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46      * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47      * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48      * SUCH DAMAGE.
49      *
50      * $Id: //depot/src/linux/drivers/scsi/aic7xxx/aic7xxx_linux.h#65 $
51      *
52      */
53     #ifndef _AIC7XXX_LINUX_H_
54     #define _AIC7XXX_LINUX_H_
55     
56     #include <linux/types.h>
57     #include <linux/blk.h>
58     #include <linux/blkdev.h>
59     #include <linux/delay.h>
60     #include <linux/ioport.h>
61     #include <linux/slab.h>
62     #include <linux/pci.h>
63     #include <linux/version.h>
64     
65     #ifndef KERNEL_VERSION
66     #define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
67     #endif
68     
69     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
70     #include <linux/config.h>
71     #endif
72     
73     /* Core SCSI definitions */
74     #include "../scsi.h"
75     #include "../hosts.h"
76     
77     /* Name space conflict with BSD queue macros */
78     #ifdef LIST_HEAD
79     #undef LIST_HEAD
80     #endif
81     
82     #include "cam.h"
83     #include "queue.h"
84     #include "scsi_message.h"
85     
86     /************************* Forward Declarations *******************************/
87     struct ahc_softc;
88     typedef struct pci_dev *ahc_dev_softc_t;
89     typedef Scsi_Cmnd      *ahc_io_ctx_t;
90     
91     /******************************* Byte Order ***********************************/
92     #define ahc_htobe16(x)	cpu_to_be16(x)
93     #define ahc_htobe32(x)	cpu_to_be32(x)
94     #define ahc_htobe64(x)	cpu_to_be64(x)
95     #define ahc_htole16(x)	cpu_to_le16(x)
96     #define ahc_htole32(x)	cpu_to_le32(x)
97     #define ahc_htole64(x)	cpu_to_le64(x)
98     
99     #define ahc_be16toh(x)	be16_to_cpu(x)
100     #define ahc_be32toh(x)	be32_to_cpu(x)
101     #define ahc_be64toh(x)	be64_to_cpu(x)
102     #define ahc_le16toh(x)	le16_to_cpu(x)
103     #define ahc_le32toh(x)	le32_to_cpu(x)
104     #define ahc_le64toh(x)	le64_to_cpu(x)
105     
106     /************************* Configuration Data *********************************/
107     extern int aic7xxx_no_probe;
108     extern int aic7xxx_detect_complete;
109     extern Scsi_Host_Template* aic7xxx_driver_template;
110     
111     /***************************** Bus Space/DMA **********************************/
112     
113     #if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,17)
114     typedef dma_addr_t bus_addr_t;
115     #else
116     typedef uint32_t bus_addr_t;
117     #endif
118     typedef uint32_t bus_size_t;
119     
120     typedef enum {
121     	BUS_SPACE_MEMIO,
122     	BUS_SPACE_PIO
123     } bus_space_tag_t;
124     
125     typedef union {
126     	u_long		  ioport;
127     	volatile uint8_t *maddr;
128     } bus_space_handle_t;
129     
130     typedef struct bus_dma_segment
131     {
132     	bus_addr_t	ds_addr;
133     	bus_size_t	ds_len;
134     } bus_dma_segment_t;
135     
136     struct ahc_linux_dma_tag
137     {
138     	bus_size_t	alignment;
139     	bus_size_t	boundary;
140     	bus_size_t	maxsize;
141     };
142     typedef struct ahc_linux_dma_tag* bus_dma_tag_t;
143     
144     struct ahc_linux_dmamap
145     {
146     	bus_addr_t	bus_addr;
147     };
148     typedef struct ahc_linux_dmamap* bus_dmamap_t;
149     
150     typedef int bus_dma_filter_t(void*, bus_addr_t);
151     typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
152     
153     #define BUS_DMA_WAITOK		0x0
154     #define BUS_DMA_NOWAIT		0x1
155     #define BUS_DMA_ALLOCNOW	0x2
156     #define BUS_DMA_LOAD_SEGS	0x4	/*
157     					 * Argument is an S/G list not
158     					 * a single buffer.
159     					 */
160     
161     #define BUS_SPACE_MAXADDR	0xFFFFFFFF
162     #define BUS_SPACE_MAXADDR_32BIT	0xFFFFFFFF
163     #define BUS_SPACE_MAXSIZE_32BIT	0xFFFFFFFF
164     
165     int	ahc_dma_tag_create(struct ahc_softc *, bus_dma_tag_t /*parent*/,
166     			   bus_size_t /*alignment*/, bus_size_t /*boundary*/,
167     			   bus_addr_t /*lowaddr*/, bus_addr_t /*highaddr*/,
168     			   bus_dma_filter_t*/*filter*/, void */*filterarg*/,
169     			   bus_size_t /*maxsize*/, int /*nsegments*/,
170     			   bus_size_t /*maxsegsz*/, int /*flags*/,
171     			   bus_dma_tag_t */*dma_tagp*/);
172     
173     void	ahc_dma_tag_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/);
174     
175     int	ahc_dmamem_alloc(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
176     			 void** /*vaddr*/, int /*flags*/,
177     			 bus_dmamap_t* /*mapp*/);
178     
179     void	ahc_dmamem_free(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
180     			void* /*vaddr*/, bus_dmamap_t /*map*/);
181     
182     void	ahc_dmamap_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/,
183     			   bus_dmamap_t /*map*/);
184     
185     int	ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t /*dmat*/,
186     			bus_dmamap_t /*map*/, void * /*buf*/,
187     			bus_size_t /*buflen*/, bus_dmamap_callback_t *,
188     			void */*callback_arg*/, int /*flags*/);
189     
190     int	ahc_dmamap_unload(struct ahc_softc *, bus_dma_tag_t, bus_dmamap_t);
191     
192     /*
193      * Operations performed by ahc_dmamap_sync().
194      */
195     #define BUS_DMASYNC_PREREAD	0x01	/* pre-read synchronization */
196     #define BUS_DMASYNC_POSTREAD	0x02	/* post-read synchronization */
197     #define BUS_DMASYNC_PREWRITE	0x04	/* pre-write synchronization */
198     #define BUS_DMASYNC_POSTWRITE	0x08	/* post-write synchronization */
199     
200     /*
201      * XXX
202      * ahc_dmamap_sync is only used on buffers allocated with
203      * the pci_alloc_consistent() API.  Although I'm not sure how
204      * this works on architectures with a write buffer, Linux does
205      * not have an API to sync "coherent" memory.  Perhaps we need
206      * to do an mb()?
207      */
208     #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)
209     
210     /************************** SCSI Constants/Structures *************************/
211     #define SCSI_REV_2 2
212     #define	SCSI_STATUS_OK			0x00
213     #define	SCSI_STATUS_CHECK_COND		0x02
214     #define	SCSI_STATUS_COND_MET		0x04
215     #define	SCSI_STATUS_BUSY		0x08
216     #define SCSI_STATUS_INTERMED		0x10
217     #define SCSI_STATUS_INTERMED_COND_MET	0x14
218     #define SCSI_STATUS_RESERV_CONFLICT	0x18
219     #define SCSI_STATUS_CMD_TERMINATED	0x22
220     #define SCSI_STATUS_QUEUE_FULL		0x28
221     
222     /*
223      * 6 byte request sense CDB format.
224      */
225     struct scsi_sense
226     {
227     	uint8_t opcode;
228     	uint8_t byte2;
229     	uint8_t unused[2];
230     	uint8_t length;
231     	uint8_t control;
232     };
233     
234     struct scsi_sense_data
235     {
236     	uint8_t	error_code;
237     	uint8_t	segment;
238     	uint8_t	flags;
239     	uint8_t	info[4];
240     	uint8_t	extra_len;
241     	uint8_t	cmd_spec_info[4];
242     	uint8_t add_sense_code;
243     	uint8_t add_sense_code_qual;
244     	uint8_t	fru;
245     	uint8_t	sense_key_spec[3];
246     	uint8_t	extra_bytes[14];
247     };
248     
249     struct scsi_inquiry
250     { 
251     	u_int8_t opcode;
252     	u_int8_t byte2;
253     #define	SI_EVPD 0x01
254     	u_int8_t page_code;
255     	u_int8_t reserved;
256     	u_int8_t length;
257     	u_int8_t control;
258     };
259     
260     struct scsi_inquiry_data
261     {
262     	uint8_t device;
263     #define	SID_TYPE(inq_data) ((inq_data)->device & 0x1f)
264     #define	SID_QUAL(inq_data) (((inq_data)->device & 0xE0) >> 5)
265     #define	SID_QUAL_LU_CONNECTED	0x00	/*
266     					 * The specified peripheral device
267     					 * type is currently connected to
268     					 * logical unit.  If the target cannot
269     					 * determine whether or not a physical
270     					 * device is currently connected, it
271     					 * shall also use this peripheral
272     					 * qualifier when returning the INQUIRY
273     					 * data.  This peripheral qualifier
274     					 * does not mean that the device is
275     					 * ready for access by the initiator.
276     					 */
277     #define	SID_QUAL_LU_OFFLINE	0x01	/*
278     					 * The target is capable of supporting
279     					 * the specified peripheral device type
280     					 * on this logical unit; however, the
281     					 * physical device is not currently
282     					 * connected to this logical unit.
283     					 */
284     #define SID_QUAL_RSVD		0x02
285     #define	SID_QUAL_BAD_LU		0x03	/*
286     					 * The target is not capable of
287     					 * supporting a physical device on
288     					 * this logical unit. For this
289     					 * peripheral qualifier the peripheral
290     					 * device type shall be set to 1Fh to
291     					 * provide compatibility with previous
292     					 * versions of SCSI. All other
293     					 * peripheral device type values are
294     					 * reserved for this peripheral
295     					 * qualifier.
296     					 */
297     #define	SID_QUAL_IS_VENDOR_UNIQUE(inq_data) ((SID_QUAL(inq_data) & 0x08) != 0)
298     	uint8_t dev_qual2;
299     #define	SID_QUAL2	0x7F
300     #define	SID_IS_REMOVABLE(inq_data) (((inq_data)->dev_qual2 & 0x80) != 0)
301     	uint8_t version;
302     #define SID_ANSI_REV(inq_data) ((inq_data)->version & 0x07)
303     #define		SCSI_REV_0		0
304     #define		SCSI_REV_CCS		1
305     #define		SCSI_REV_2		2
306     #define		SCSI_REV_SPC		3
307     #define		SCSI_REV_SPC2		4
308     
309     #define SID_ECMA	0x38
310     #define SID_ISO		0xC0
311     	uint8_t response_format;
312     #define SID_AENC	0x80
313     #define SID_TrmIOP	0x40
314     	uint8_t additional_length;
315     	uint8_t reserved[2];
316     	uint8_t flags;
317     #define	SID_SftRe	0x01
318     #define	SID_CmdQue	0x02
319     #define	SID_Linked	0x08
320     #define	SID_Sync	0x10
321     #define	SID_WBus16	0x20
322     #define	SID_WBus32	0x40
323     #define	SID_RelAdr	0x80
324     #define SID_VENDOR_SIZE   8
325     	char	 vendor[SID_VENDOR_SIZE];
326     #define SID_PRODUCT_SIZE  16
327     	char	 product[SID_PRODUCT_SIZE];
328     #define SID_REVISION_SIZE 4
329     	char	 revision[SID_REVISION_SIZE];
330     	/*
331     	 * The following fields were taken from SCSI Primary Commands - 2
332     	 * (SPC-2) Revision 14, Dated 11 November 1999
333     	 */
334     #define	SID_VENDOR_SPECIFIC_0_SIZE	20
335     	u_int8_t vendor_specific0[SID_VENDOR_SPECIFIC_0_SIZE];
336     	/*
337     	 * An extension of SCSI Parallel Specific Values
338     	 */
339     #define	SID_SPI_IUS		0x01
340     #define	SID_SPI_QAS		0x02
341     #define	SID_SPI_CLOCK_ST	0x00
342     #define	SID_SPI_CLOCK_DT	0x04
343     #define	SID_SPI_CLOCK_DT_ST	0x0C
344     #define	SID_SPI_MASK		0x0F
345     	uint8_t spi3data;
346     	uint8_t reserved2;
347     	/*
348     	 * Version Descriptors, stored 2 byte values.
349     	 */
350     	uint8_t version1[2];
351     	uint8_t version2[2];
352     	uint8_t version3[2];
353     	uint8_t version4[2];
354     	uint8_t version5[2];
355     	uint8_t version6[2];
356     	uint8_t version7[2];
357     	uint8_t version8[2];
358     
359     	uint8_t reserved3[22];
360     
361     #define	SID_VENDOR_SPECIFIC_1_SIZE	160
362     	uint8_t vendor_specific1[SID_VENDOR_SPECIFIC_1_SIZE];
363     };
364     
365     /********************************** Includes **********************************/
366     /* Host template and function declarations referenced by the template. */
367     #include "aic7xxx_linux_host.h"
368     
369     /* Core driver definitions */
370     #include "aic7xxx.h"
371     
372     /* SMP support */
373     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,17)
374     #include <linux/spinlock.h>
375     #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,93)
376     #include <linux/smp.h>
377     #endif
378     
379     #define AIC7XXX_DRIVER_VERSION  "6.2.1"
380     
381     /**************************** Front End Queues ********************************/
382     /*
383      * Data structure used to cast the Linux struct scsi_cmnd to something
384      * that allows us to use the queue macros.  The linux structure has
385      * plenty of space to hold the links fields as required by the queue
386      * macros, but the queue macors require them to have the correct type.
387      */
388     struct ahc_cmd_internal {
389     	/* Area owned by the Linux scsi layer. */
390     	uint8_t	private[offsetof(struct scsi_cmnd, SCp.Status)];
391     	union {
392     		STAILQ_ENTRY(ahc_cmd)	ste;
393     		LIST_ENTRY(ahc_cmd)	le;
394     		TAILQ_ENTRY(ahc_cmd)	tqe;
395     	} links;
396     	uint32_t			end;
397     };
398     
399     struct ahc_cmd {
400     	union {
401     		struct ahc_cmd_internal	icmd;
402     		struct scsi_cmnd	scsi_cmd;
403     	} un;
404     };
405     
406     #define acmd_icmd(cmd) ((cmd)->un.icmd)
407     #define acmd_scsi_cmd(cmd) ((cmd)->un.scsi_cmd)
408     #define acmd_links un.icmd.links
409     
410     /*************************** Device Data Structures ***************************/
411     /*
412      * A per probed device structure used to deal with some error recovery
413      * scenarios that the Linux mid-layer code just doesn't know how to
414      * handle.  The structure allocated for a device only becomes persistant
415      * after a successfully completed inquiry command to the target when
416      * that inquiry data indicates a lun is present.
417      */
418     TAILQ_HEAD(ahc_busyq, ahc_cmd);
419     typedef enum {
420     	AHC_DEV_UNCONFIGURED	 = 0x01,
421     	AHC_DEV_FREEZE_TIL_EMPTY = 0x02, /* Freeze queue until active == 0 */
422     	AHC_DEV_TIMER_ACTIVE	 = 0x04, /* Our timer is active */
423     	AHC_DEV_ON_RUN_LIST	 = 0x08, /* Queued to be run later */
424     	AHC_DEV_Q_BASIC		 = 0x10, /* Allow basic device queuing */
425     	AHC_DEV_Q_TAGGED	 = 0x20  /* Allow full SCSI2 command queueing */
426     } ahc_dev_flags;
427     
428     struct ahc_linux_target;
429     struct ahc_linux_device {
430     	TAILQ_ENTRY(ahc_linux_device) links;
431     	struct		ahc_busyq busyq;
432     
433     	/*
434     	 * The number of transactions currently
435     	 * queued to the device.
436     	 */
437     	int		active;
438     
439     	/*
440     	 * The currently allowed number of 
441     	 * transactions that can be queued to
442     	 * the device.  Must be signed for
443     	 * conversion from tagged to untagged
444     	 * mode where the device may have more
445     	 * than one outstanding active transaction.
446     	 */
447     	int		openings;
448     
449     	/*
450     	 * A positive count indicates that this
451     	 * device's queue is halted.
452     	 */
453     	u_int		qfrozen;
454     	
455     	/*
456     	 * Cumulative command counter.
457     	 */
458     	u_long		commands_issued;
459     
460     	/*
461     	 * The number of tagged transactions when
462     	 * running at our current opening level
463     	 * that have been successfully received by
464     	 * this device since the last QUEUE FULL.
465     	 */
466     	u_int		tag_success_count;
467     #define AHC_TAG_SUCCESS_INTERVAL 50
468     
469     	ahc_dev_flags	flags;
470     
471     	/*
472     	 * The high limit for the tags variable.
473     	 */
474     	u_int		maxtags;
475     
476     	/*
477     	 * The computed number of tags outstanding
478     	 * at the time of the last QUEUE FULL event.
479     	 */
480     	u_int		tags_on_last_queuefull;
481     
482     	/*
483     	 * How many times we have seen a queue full
484     	 * with the same number of tags.  This is used
485     	 * to stop our adaptive queue depth algorithm
486     	 * on devices with a fixed number of tags.
487     	 */
488     	u_int		last_queuefull_same_count;
489     #define AHC_LOCK_TAGS_COUNT 50
490     
491     	/*
492     	 * How many transactions have been queued
493     	 * without the device going idle.  We use
494     	 * this statistic to 
495     	 */
496     	u_int		commands_since_idle_or_otag;
497     #define AHC_OTAG_THRESH	250
498     
499     	int		lun;
500     	struct		ahc_linux_target *target;
501     };
502     
503     struct ahc_linux_target {
504     	struct	ahc_linux_device *devices[AHC_NUM_LUNS];
505     	int	channel;
506     	int	target;
507     	int	refcount;
508     	struct	ahc_transinfo last_tinfo;
509     };
510     
511     /********************* Definitions Required by the Core ***********************/
512     /*
513      * Number of SG segments we require.  So long as the S/G segments for
514      * a particular transaction are allocated in a physically contiguous
515      * manner and are allocated below 4GB, the number of S/G segments is
516      * unrestricted.
517      */
518     #define        AHC_NSEG 128
519     
520     /*
521      * Per-SCB OSM storage.
522      */
523     struct scb_platform_data {
524     	struct ahc_linux_device	*dev;
525     	uint32_t		 xfer_len;
526     #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
527     	uint32_t		 resid;		/* Transfer residual */
528     #endif
529     };
530     
531     /*
532      * Define a structure used for each host adapter.  All members are
533      * aligned on a boundary >= the size of the member to honor the
534      * alignment restrictions of the various platforms supported by
535      * this driver.
536      */
537     TAILQ_HEAD(ahc_completeq, ahc_cmd);
538     struct ahc_platform_data {
539     	/*
540     	 * Fields accessed from interrupt context.
541     	 */
542     	struct ahc_linux_target *targets[AHC_NUM_TARGETS]; 
543     	TAILQ_HEAD(, ahc_linux_device) device_runq;
544     	struct ahc_completeq	 completeq;
545     
546     #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,0)
547     	spinlock_t		 spin_lock;
548     #endif
549     	u_int			 qfrozen;
550     	struct timer_list	 reset_timer;
551     	struct semaphore	 eh_sem;
552     	struct Scsi_Host        *host;		/* pointer to scsi host */
553     	uint32_t		 irq;		/* IRQ for this adapter */
554     	uint32_t		 bios_address;
555     	uint32_t		 mem_busaddr;	/* Mem Base Addr */
556     	bus_addr_t		 hw_dma_mask;
557     };
558     
559     /************************** OS Utility Wrappers *******************************/
560     #define printf printk
561     #define M_NOWAIT GFP_ATOMIC
562     #define M_WAITOK 0
563     #define malloc(size, type, flags) kmalloc(size, flags)
564     #define free(ptr, type) kfree(ptr)
565     
566     static __inline void ahc_delay(long);
567     static __inline void
568     ahc_delay(long usec)
569     {
570     	/*
571     	 * udelay on Linux can have problems for
572     	 * multi-millisecond waits.  Wait at most
573     	 * 1024us per call.
574     	 */
575     	while (usec > 0) {
576     		udelay(usec % 1024);
577     		usec -= 1024;
578     	}
579     }
580     
581     
582     /***************************** Low Level I/O **********************************/
583     #if defined(__powerpc__) || defined(__i386__) || defined(__ia64__) || defined(__x86_64__)
584     #define MMAPIO
585     #endif
586     
587     static __inline uint8_t ahc_inb(struct ahc_softc * ahc, long port);
588     static __inline void ahc_outb(struct ahc_softc * ahc, long port, uint8_t val);
589     static __inline void ahc_outsb(struct ahc_softc * ahc, long port,
590     			       uint8_t *, int count);
591     static __inline void ahc_insb(struct ahc_softc * ahc, long port,
592     			       uint8_t *, int count);
593     
594     static __inline uint8_t
595     ahc_inb(struct ahc_softc * ahc, long port)
596     {
597     #ifdef MMAPIO
598     	uint8_t x;
599     
600     	if (ahc->tag == BUS_SPACE_MEMIO) {
601     		x = readb(ahc->bsh.maddr + port);
602     	} else {
603     		x = inb(ahc->bsh.ioport + port);
604     	}
605     	mb();
606     	return (x);
607     #else
608     	return (inb(ahc->bsh.ioport + port));
609     #endif
610     }
611     
612     static __inline void
613     ahc_outb(struct ahc_softc * ahc, long port, uint8_t val)
614     {
615     #ifdef MMAPIO
616     	if (ahc->tag == BUS_SPACE_MEMIO) {
617     		writeb(val, ahc->bsh.maddr + port);
618     	} else {
619     		outb(val, ahc->bsh.ioport + port);
620     	}
621     	mb();
622     #else
623     	outb(val, ahc->bsh.ioport + port);
624     #endif
625     }
626     
627     static __inline void
628     ahc_outsb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
629     {
630     	int i;
631     
632     	/*
633     	 * There is probably a more efficient way to do this on Linux
634     	 * but we don't use this for anything speed critical and this
635     	 * should work.
636     	 */
637     	for (i = 0; i < count; i++)
638     		ahc_outb(ahc, port, *array++);
639     }
640     
641     static __inline void
642     ahc_insb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
643     {
644     	int i;
645     
646     	/*
647     	 * There is probably a more efficient way to do this on Linux
648     	 * but we don't use this for anything speed critical and this
649     	 * should work.
650     	 */
651     	for (i = 0; i < count; i++)
652     		*array++ = ahc_inb(ahc, port);
653     }
654     
655     /**************************** Initialization **********************************/
656     int		ahc_linux_register_host(struct ahc_softc *,
657     					Scsi_Host_Template *);
658     
659     uint64_t	ahc_linux_get_memsize(void);
660     
661     /*************************** Pretty Printing **********************************/
662     struct info_str {
663     	char *buffer;
664     	int length;
665     	off_t offset;
666     	int pos;
667     };
668     
669     void	ahc_format_transinfo(struct info_str *info,
670     			     struct ahc_transinfo *tinfo);
671     
672     /******************************** Locking *************************************/
673     /* Lock protecting internal data structures */
674     static __inline void ahc_lockinit(struct ahc_softc *);
675     static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
676     static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
677     
678     /* Lock held during command compeletion to the upper layer */
679     static __inline void ahc_done_lockinit(struct ahc_softc *);
680     static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
681     static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
682     
683     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,93)
684     static __inline void
685     ahc_lockinit(struct ahc_softc *ahc)
686     {
687     	spin_lock_init(&ahc->platform_data->spin_lock);
688     }
689     
690     static __inline void
691     ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
692     {
693     	*flags = 0;
694     	spin_lock_irqsave(&ahc->platform_data->spin_lock, *flags);
695     }
696     
697     static __inline void
698     ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
699     {
700     	spin_unlock_irqrestore(&ahc->platform_data->spin_lock, *flags);
701     }
702     
703     static __inline void
704     ahc_done_lockinit(struct ahc_softc *ahc)
705     {
706     	/* We don't own the iorequest lock, so we don't initialize it. */
707     }
708     
709     static __inline void
710     ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
711     {
712     	*flags = 0;
713     	spin_lock_irqsave(&io_request_lock, *flags);
714     }
715     
716     static __inline void
717     ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
718     {
719     	spin_unlock_irqrestore(&io_request_lock, *flags);
720     }
721     
722     #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0) */
723     
724     ahc_lockinit(struct ahc_softc *ahc)
725     {
726     }
727     
728     static __inline void
729     ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
730     {
731     	*flags = 0;
732     	save_flags(*flags);
733     	cli();
734     }
735     
736     static __inline void
737     ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
738     {
739     	restore_flags(*flags);
740     }
741     
742     ahc_done_lockinit(struct ahc_softc *ahc)
743     {
744     }
745     
746     static __inline void
747     ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
748     {
749     	/*
750     	 * The done lock is always held while
751     	 * the ahc lock is held so blocking
752     	 * interrupts again would have no effect.
753     	 */
754     }
755     
756     static __inline void
757     ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
758     {
759     }
760     #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0) */
761     
762     /******************************* PCI Definitions ******************************/
763     /*
764      * PCIM_xxx: mask to locate subfield in register
765      * PCIR_xxx: config register offset
766      * PCIC_xxx: device class
767      * PCIS_xxx: device subclass
768      * PCIP_xxx: device programming interface
769      * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
770      * PCID_xxx: device ID
771      */
772     #define PCIR_DEVVENDOR		0x00
773     #define PCIR_VENDOR		0x00
774     #define PCIR_DEVICE		0x02
775     #define PCIR_COMMAND		0x04
776     #define PCIM_CMD_PORTEN		0x0001
777     #define PCIM_CMD_MEMEN		0x0002
778     #define PCIM_CMD_BUSMASTEREN	0x0004
779     #define PCIM_CMD_MWRICEN	0x0010
780     #define PCIM_CMD_PERRESPEN	0x0040
781     #define PCIR_STATUS		0x06
782     #define PCIR_REVID		0x08
783     #define PCIR_PROGIF		0x09
784     #define PCIR_SUBCLASS		0x0a
785     #define PCIR_CLASS		0x0b
786     #define PCIR_CACHELNSZ		0x0c
787     #define PCIR_LATTIMER		0x0d
788     #define PCIR_HEADERTYPE		0x0e
789     #define PCIM_MFDEV		0x80
790     #define PCIR_BIST		0x0f
791     #define PCIR_CAP_PTR		0x34
792     
793     /* config registers for header type 0 devices */
794     #define PCIR_MAPS	0x10
795     #define PCIR_SUBVEND_0	0x2c
796     #define PCIR_SUBDEV_0	0x2e
797     
798     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
799     extern struct pci_driver aic7xxx_pci_driver;
800     #endif
801     
802     typedef enum
803     {
804     	AHC_POWER_STATE_D0,
805     	AHC_POWER_STATE_D1,
806     	AHC_POWER_STATE_D2,
807     	AHC_POWER_STATE_D3
808     } ahc_power_state;
809     
810     void ahc_power_state_change(struct ahc_softc *ahc,
811     			    ahc_power_state new_state);
812     /**************************** VL/EISA Routines ********************************/
813     int			 aic7770_linux_probe(Scsi_Host_Template *);
814     int			 aic7770_map_registers(struct ahc_softc *ahc);
815     int			 aic7770_map_int(struct ahc_softc *ahc, u_int irq);
816     
817     /******************************* PCI Routines *********************************/
818     /*
819      * We need to use the bios32.h routines if we are kernel version 2.1.92 or less.
820      */
821     #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,1,92)
822     #if defined(__sparc_v9__) || defined(__powerpc__)
823     #error "PPC and Sparc platforms are only support under 2.1.92 and above"
824     #endif
825     #include <linux/bios32.h>
826     #endif
827     
828     int			 ahc_linux_pci_probe(Scsi_Host_Template *);
829     int			 ahc_pci_map_registers(struct ahc_softc *ahc);
830     int			 ahc_pci_map_int(struct ahc_softc *ahc);
831     
832     static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
833     					     int reg, int width);
834     
835     static __inline uint32_t
836     ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
837     {
838     	switch (width) {
839     	case 1:
840     	{
841     		uint8_t retval;
842     
843     		pci_read_config_byte(pci, reg, &retval);
844     		return (retval);
845     	}
846     	case 2:
847     	{
848     		uint16_t retval;
849     		pci_read_config_word(pci, reg, &retval);
850     		return (retval);
851     	}
852     	case 4:
853     	{
854     		uint32_t retval;
855     		pci_read_config_dword(pci, reg, &retval);
856     		return (retval);
857     	}
858     	default:
859     		panic("ahc_pci_read_config: Read size too big");
860     		/* NOTREACHED */
861     		return (0);
862     	}
863     }
864     
865     static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
866     					  int reg, uint32_t value,
867     					  int width);
868     
869     static __inline void
870     ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
871     {
872     	switch (width) {
873     	case 1:
874     		pci_write_config_byte(pci, reg, value);
875     		break;
876     	case 2:
877     		pci_write_config_word(pci, reg, value);
878     		break;
879     	case 4:
880     		pci_write_config_dword(pci, reg, value);
881     		break;
882     	default:
883     		panic("ahc_pci_write_config: Write size too big");
884     		/* NOTREACHED */
885     	}
886     }
887     
888     static __inline int ahc_get_pci_function(ahc_dev_softc_t);
889     static __inline int
890     ahc_get_pci_function(ahc_dev_softc_t pci)
891     {
892     	return (PCI_FUNC(pci->devfn));
893     }
894     
895     static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
896     static __inline int
897     ahc_get_pci_slot(ahc_dev_softc_t pci)
898     {
899     	return (PCI_SLOT(pci->devfn));
900     }
901     
902     static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
903     static __inline int
904     ahc_get_pci_bus(ahc_dev_softc_t pci)
905     {
906     	return (pci->bus->number);
907     }
908     
909     static __inline void ahc_flush_device_writes(struct ahc_softc *);
910     static __inline void
911     ahc_flush_device_writes(struct ahc_softc *ahc)
912     {
913     	/* XXX Is this sufficient for all architectures??? */
914     	ahc_inb(ahc, INTSTAT);
915     }
916     
917     #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0)
918     #define pci_map_sg(pdev, sg_list, nseg, direction) (nseg)
919     #define pci_unmap_sg(pdev, sg_list, nseg, direction)
920     #define sg_dma_address(sg) (VIRT_TO_BUS((sg)->address))
921     #define sg_dma_len(sg) ((sg)->length)
922     #define pci_map_single(pdev, buffer, bufflen, direction) \
923     	(VIRT_TO_BUS(buffer))
924     #define pci_unmap_single(pdev, buffer, buflen, direction)
925     #endif
926     
927     /*********************** Transaction Access Wrappers **************************/
928     static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
929     static __inline
930     void ahc_set_transaction_status(struct scb *scb, uint32_t status)
931     {
932     	scb->io_ctx->result &= ~(CAM_STATUS_MASK << 16);
933     	scb->io_ctx->result |= status << 16;
934     }
935     
936     static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
937     static __inline
938     void ahc_set_scsi_status(struct scb *scb, uint32_t status)
939     {
940     	scb->io_ctx->result &= ~0xFFFF;
941     	scb->io_ctx->result |= status;
942     }
943     
944     static __inline uint32_t ahc_get_transaction_status(struct scb *);
945     static __inline
946     uint32_t ahc_get_transaction_status(struct scb *scb)
947     {
948     	return ((scb->io_ctx->result >> 16) & CAM_STATUS_MASK);
949     }
950     
951     static __inline uint32_t ahc_get_scsi_status(struct scb *);
952     static __inline
953     uint32_t ahc_get_scsi_status(struct scb *scb)
954     {
955     	return (scb->io_ctx->result & 0xFFFF);
956     }
957     
958     static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
959     static __inline
960     void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
961     {
962     	/*
963     	 * Nothing to do for linux as the incoming transaction
964     	 * has no concept of tag/non tagged, etc.
965     	 */
966     }
967     
968     static __inline u_long ahc_get_transfer_length(struct scb *);
969     static __inline
970     u_long ahc_get_transfer_length(struct scb *scb)
971     {
972     	return (scb->platform_data->xfer_len);
973     }
974     
975     static __inline int ahc_get_transfer_dir(struct scb *);
976     static __inline
977     int ahc_get_transfer_dir(struct scb *scb)
978     {
979     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,40)
980     	return (scb->io_ctx->sc_data_direction);
981     #else
982     	if (scb->io_ctx->bufflen == 0)
983     		return (CAM_DIR_NONE);
984     
985     	switch(scb->io_ctx->cmnd[0]) {
986     	case 0x08:  /* READ(6)  */
987     	case 0x28:  /* READ(10) */
988     	case 0xA8:  /* READ(12) */
989     		return (CAM_DIR_IN);
990             case 0x0A:  /* WRITE(6)  */
991             case 0x2A:  /* WRITE(10) */
992             case 0xAA:  /* WRITE(12) */
993     		return (CAM_DIR_OUT);
994             default:
995     		return (CAM_DIR_NONE);
996             }
997     #endif
998     }
999     
1000     static __inline void ahc_set_residual(struct scb *, u_long);
1001     static __inline
1002     void ahc_set_residual(struct scb *scb, u_long resid)
1003     {
1004     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1005     	scb->io_ctx->resid = resid;
1006     #else
1007     	scb->platform_data->resid = resid;
1008     #endif
1009     }
1010     
1011     static __inline void ahc_set_sense_residual(struct scb *, u_long);
1012     static __inline
1013     void ahc_set_sense_residual(struct scb *scb, u_long resid)
1014     {
1015     	/* This can't be reported in Linux */
1016     }
1017     
1018     static __inline u_long ahc_get_residual(struct scb *);
1019     static __inline
1020     u_long ahc_get_residual(struct scb *scb)
1021     {
1022     #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1023     	return (scb->io_ctx->resid);
1024     #else
1025     	return (scb->platform_data->resid);
1026     #endif
1027     }
1028     
1029     static __inline int ahc_perform_autosense(struct scb *);
1030     static __inline
1031     int ahc_perform_autosense(struct scb *scb)
1032     {
1033     	/*
1034     	 * We always perform autosense in Linux.
1035     	 * On other platforms this is set on a
1036     	 * per-transaction basis.
1037     	 */
1038     	return (1);
1039     }
1040     
1041     static __inline uint32_t
1042     ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
1043     {
1044     	return (sizeof(struct scsi_sense_data));
1045     }
1046     
1047     static __inline void ahc_notify_xfer_settings_change(struct ahc_softc *,
1048     						     struct ahc_devinfo *);
1049     static __inline void
1050     ahc_notify_xfer_settings_change(struct ahc_softc *ahc,
1051     				struct ahc_devinfo *devinfo)
1052     {
1053     	/* Nothing to do here for linux */
1054     }
1055     
1056     static __inline void ahc_platform_scb_free(struct ahc_softc *ahc,
1057     					   struct scb *scb);
1058     static __inline void
1059     ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
1060     {
1061     	ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
1062     }
1063     
1064     int	ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
1065     void	ahc_platform_free(struct ahc_softc *ahc);
1066     void	ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
1067     static __inline void	ahc_freeze_scb(struct scb *scb);
1068     static __inline void
1069     ahc_freeze_scb(struct scb *scb)
1070     {
1071     	/* Noting to do here for linux */
1072     }
1073     
1074     void	ahc_platform_set_tags(struct ahc_softc *ahc,
1075     			      struct ahc_devinfo *devinfo, ahc_queue_alg);
1076     int	ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
1077     				char channel, int lun, u_int tag,
1078     				role_t role, uint32_t status);
1079     void	ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
1080     void	ahc_platform_flushwork(struct ahc_softc *ahc);
1081     int	ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
1082     void	ahc_done(struct ahc_softc*, struct scb*);
1083     void	ahc_send_async(struct ahc_softc *, char channel,
1084     		       u_int target, u_int lun, ac_code, void *);
1085     void	ahc_print_path(struct ahc_softc *, struct scb *);
1086     void	ahc_platform_dump_card_state(struct ahc_softc *ahc);
1087     
1088     #ifdef CONFIG_PCI
1089     #define AHC_PCI_CONFIG 1
1090     #else
1091     #define AHC_PCI_CONFIG 0
1092     #endif
1093     #define bootverbose aic7xxx_verbose
1094     extern int aic7xxx_verbose;
1095     #endif /* _AIC7XXX_LINUX_H_ */
1096