xref: /netbsd-src/sys/dev/ic/osiopvar.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: osiopvar.h,v 1.10 2007/06/30 14:08:58 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1990 The Regents of the University of California.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Van Jacobson of Lawrence Berkeley Laboratory.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)siopvar.h	7.1 (Berkeley) 5/8/90
61  */
62 
63 #define osiop_read_1(sc, reg)					\
64     bus_space_read_1((sc)->sc_bst, (sc)->sc_reg, reg)
65 #define osiop_write_1(sc, reg, val)				\
66     bus_space_write_1((sc)->sc_bst, (sc)->sc_reg, reg, val)
67 
68 #define osiop_read_4(sc, reg)					\
69     bus_space_read_4((sc)->sc_bst, (sc)->sc_reg, reg)
70 #define osiop_write_4(sc, reg, val)				\
71     bus_space_write_4((sc)->sc_bst, (sc)->sc_reg, reg, val)
72 
73 /*
74  * The largest single request will be MAXPHYS bytes which will require
75  * at most MAXPHYS/NBPG+1 chain elements to describe, i.e. if none of
76  * the buffer pages are physically contiguous (MAXPHYS/NBPG) and the
77  * buffer is not page aligned (+1).
78  */
79 /* XXX This should be (MAXPHYS / NBPG + 1), but hardcoded in script */
80 #define OSIOP_NSG	(16 + 1)
81 #if MAXPHYS > (PAGE_SIZE * (OSIOP_NSG - 1))
82 #define OSIOP_MAX_XFER	(PAGE_SIZE * (OSIOP_NSG - 1))
83 #else
84 #define OSIOP_MAX_XFER	MAXPHYS
85 #endif
86 
87 #define OSIOP_NTGT 8
88 #define OSIOP_NACB 32	/* XXX (PAGE_SIZE / sizeof(osiop_ds)) is better? */
89 
90 /*
91  * Data Structure for SCRIPTS program
92  */
93 typedef struct buf_table {
94 	uint32_t count;
95 	uint32_t addr;
96 } buf_table_t;
97 
98 struct osiop_ds {
99 	uint32_t scsi_addr;		/*   0: SCSI ID & sync */
100 	uint32_t pad1;			/*   4: padding */
101 	buf_table_t id;			/*   8: Identify message */
102 	buf_table_t cmd;		/*  16: SCSI command */
103 	buf_table_t status;		/*  24: Status */
104 	buf_table_t msg;		/*  32: Message */
105 	buf_table_t msgin;		/*  40: Message in */
106 	buf_table_t extmsg;		/*  48: Extended message in */
107 	buf_table_t synmsg;		/*  56: Sync transfer request */
108 	buf_table_t data[OSIOP_NSG];	/*  64: DMA S/G buffers */
109 					/*      (8 * OSIOP_NSG == 136bytes) */
110 	uint8_t scsipi_cmd[16];		/* 200: cmd buf */
111 	uint8_t msgout[8];		/* 216: message out buf */
112 	uint8_t msgbuf[8];		/* 224: message in buf */
113 	uint8_t stat[8];		/* 232: stat buf */
114 	uint8_t pad2[16];		/* 240: padding to 256bytes */
115 } __attribute__((__packed__));
116 
117 /* status can hold the SCSI_* status values, and 2 additional values: */
118 #define SCSI_OSIOP_NOCHECK	0xfe	/* don't check the scsi status */
119 #define SCSI_OSIOP_NOSTATUS	0xff	/* device didn't report status */
120 
121 #define MSG_INVALID		0xff	/* dummy value for message buffer */
122 
123 #define OSIOP_DSOFF(x)		offsetof(struct osiop_ds, x)
124 #define OSIOP_DSCMDOFF		OSIOP_DSOFF(scsipi_cmd[0])
125 #define OSIOP_DSIDOFF		OSIOP_DSOFF(msgout[0])
126 #define OSIOP_DSMSGOFF		OSIOP_DSOFF(msgbuf[0])
127 #define OSIOP_DSMSGINOFF	OSIOP_DSOFF(msgbuf[1])
128 #define OSIOP_DSEXTMSGOFF	OSIOP_DSOFF(msgbuf[2])
129 #define OSIOP_DSSYNMSGOFF	OSIOP_DSOFF(msgbuf[3])
130 #define OSIOP_DSSTATOFF		OSIOP_DSOFF(stat[0])
131 
132 /*
133  * ACB. Holds additional information for each SCSI command Comments:
134  * Basicly, we refrain from fiddling with the scsi_xfer struct
135  * (except do the expected updating of return values).
136  * We'll generally update: xs->{flags,resid,error,status} and
137  * occasionally xs->retries.
138  */
139 struct osiop_acb {
140 	TAILQ_ENTRY(osiop_acb) chain;
141 	struct scsipi_xfer *xs;	/* SCSI xfer ctrl block from upper layer */
142 	struct osiop_softc *sc;	/* points back to our adapter */
143 
144 	bus_dmamap_t datadma;	/* DMA map for data transfer */
145 
146 	struct osiop_ds *ds;	/* data structure for this acb */
147 	bus_size_t dsoffset;	/* offset of data structure for this acb */
148 
149 	bus_size_t cmdlen;	/* command length */
150 	bus_size_t datalen;	/* transfer data length */
151 #ifdef OSIOP_DEBUG
152 	void *data;		/* transfer data buffer ptr */
153 #endif
154 
155 	bus_addr_t curaddr;	/* current transfer data buffer */
156 	bus_size_t curlen;	/* current transfer data length */
157 
158 	int status;		/* status of this acb */
159 /* status defs */
160 #define ACB_S_FREE	0	/* cmd slot is free */
161 #define ACB_S_READY	1	/* cmd slot is waiting for processing */
162 #define ACB_S_ACTIVE	2	/* cmd slot is being processed */
163 #define ACB_S_DONE	3	/* cmd slot has been processed */
164 
165 	int flags;		/* cmd slot flags */
166 #define ACB_F_TIMEOUT	0x01	/* command timeout */
167 
168 	uint8_t intstat;	/* buffer to save sc_flags on disconnect */
169 };
170 
171 /*
172  * Some info about each (possible) target on the SCSI bus.  This should
173  * probably have been a "per target+lunit" structure, but we'll leave it at
174  * this for now.  Is there a way to reliably hook it up to sc->fordriver??
175  */
176 struct osiop_tinfo {
177 	int cmds;		/* number of commands processed */
178 	int dconns;		/* number of disconnects */
179 	int touts;		/* number of timeouts */
180 	int perrs;		/* number of parity errors */
181 	int lubusy;		/* What local units/subr. are busy? */
182 	int period;		/* Period suggestion */
183 	int offset;		/* Offset suggestion */
184 	int flags;		/* misc flags per each target */
185 #define TI_NOSYNC	0x01	/* disable sync xfer on this target */
186 #define TI_NODISC	0x02	/* disable disconnect on this target */
187 	int state;		/* negotiation state */
188 	uint8_t sxfer;		/* value for SXFER reg */
189 	uint8_t sbcl;		/* value for SBCL reg */
190 };
191 
192 struct osiop_softc {
193 	struct device sc_dev;
194 
195 	bus_space_tag_t sc_bst;		/* bus space tag */
196 	bus_space_handle_t sc_reg;	/* register I/O handle */
197 
198 	bus_dma_tag_t sc_dmat;		/* bus dma tag */
199 	bus_dmamap_t sc_scrdma;		/* script dma map */
200 	bus_dmamap_t sc_dsdma;		/* script data dma map */
201 
202 	uint32_t *sc_script;		/* ptr to script memory */
203 	struct osiop_ds *sc_ds;		/* ptr to data structure memory */
204 
205 	int sc_id;			/* adapter SCSI id */
206 	int sc_active;			/* number of active I/O's */
207 
208 	struct osiop_acb *sc_nexus;	/* current command */
209 	struct osiop_acb *sc_acb;	/* the real command blocks */
210 
211 	/* Lists of command blocks */
212 	TAILQ_HEAD(acb_list, osiop_acb) free_list,
213 				        ready_list,
214 				        nexus_list;
215 
216 	struct scsipi_adapter sc_adapter;
217 	struct scsipi_channel sc_channel;
218 
219 	struct osiop_tinfo sc_tinfo[OSIOP_NTGT];
220 
221 	int sc_clock_freq;
222 	int sc_tcp[4];
223 	int sc_flags;
224 #define OSIOP_INTSOFF	0x80	/* Interrupts turned off */
225 #define OSIOP_INTDEFER	0x40	/* MD interrupt has been deferred */
226 #define OSIOP_NODMA	0x02	/* No DMA transfer */
227 #define OSIOP_ALIVE	0x01	/* controller initialized */
228 
229 	int sc_cfflags;			/* copy of config flags */
230 
231 	int sc_minsync;
232 
233 	uint8_t sc_dstat;
234 	uint8_t sc_sstat0;
235 	uint8_t sc_sstat1;
236 	uint8_t sc_istat;
237 	uint8_t sc_dcntl;
238 	uint8_t sc_ctest4;
239 	uint8_t sc_ctest7;
240 	uint8_t sc_sien;
241 	uint8_t sc_dien;
242 };
243 
244 /* negotiation states */
245 #define NEG_INIT	0	/* Initial negotiate state */
246 #define NEG_SYNC	NEG_INIT /* Negotiate synch transfers */
247 #define NEG_WAITS	1	/* Waiting for synch negotiation response */
248 #define NEG_DONE	2	/* Wide and/or sync negotiation done */
249 
250 void osiop_attach(struct osiop_softc *);
251 void osiop_intr(struct osiop_softc *);
252