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