aic6360.c revision 1.7 1 1.1 mycroft /*
2 1.7 mycroft * Copyright (c) 1994 Charles Hannum.
3 1.1 mycroft * Copyright (c) 1994 Jarle Greipsland
4 1.1 mycroft * All rights reserved.
5 1.1 mycroft *
6 1.1 mycroft * Redistribution and use in source and binary forms, with or without
7 1.1 mycroft * modification, are permitted provided that the following conditions
8 1.1 mycroft * are met:
9 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
10 1.1 mycroft * notice, this list of conditions and the following disclaimer.
11 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.1 mycroft * documentation and/or other materials provided with the distribution.
14 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.1 mycroft * must display the following acknowledgement:
16 1.1 mycroft * This product includes software developed by Jarle Greipsland
17 1.1 mycroft * 4. The name of the author may not be used to endorse or promote products
18 1.1 mycroft * derived from this software without specific prior written permission.
19 1.1 mycroft *
20 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 1.1 mycroft * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 1.1 mycroft * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 1.1 mycroft * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1 mycroft * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 1.1 mycroft * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 1.1 mycroft * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 1.1 mycroft * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 mycroft * POSSIBILITY OF SUCH DAMAGE.
31 1.1 mycroft */
32 1.1 mycroft
33 1.1 mycroft /*
34 1.7 mycroft * $Id: aic6360.c,v 1.7 1994/08/08 07:09:31 mycroft Exp $
35 1.1 mycroft *
36 1.1 mycroft * Acknowledgements: Many of the algorithms used in this driver are
37 1.1 mycroft * inspired by the work of Julian Elischer (julian (at) tfs.com) and
38 1.1 mycroft * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu). Thanks a million!
39 1.1 mycroft */
40 1.1 mycroft
41 1.1 mycroft /* TODO list:
42 1.1 mycroft * 1) Get the DMA stuff working.
43 1.1 mycroft * 2) Get the iov/uio stuff working. Is this a good thing ???
44 1.1 mycroft * 3) Get the synch stuff working.
45 1.1 mycroft * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
46 1.1 mycroft */
47 1.1 mycroft
48 1.1 mycroft /*
49 1.1 mycroft * A few customizable items:
50 1.1 mycroft */
51 1.1 mycroft
52 1.1 mycroft /* The SCSI ID of the host adapter/computer */
53 1.1 mycroft #define AIC_SCSI_HOSTID 7
54 1.1 mycroft
55 1.1 mycroft /* Use doubleword transfers to/from SCSI chip. Note: This requires
56 1.1 mycroft * motherboard support. Basicly, some motherboard chipsets are able to
57 1.1 mycroft * split a 32 bit I/O operation into two 16 bit I/O operations,
58 1.1 mycroft * transparently to the processor. This speeds up some things, notably long
59 1.1 mycroft * data transfers.
60 1.1 mycroft */
61 1.6 mycroft #define AIC_USE_DWORDS 0
62 1.1 mycroft
63 1.1 mycroft /* Allow disconnects? Was mainly used in an early phase of the driver when
64 1.1 mycroft * the message system was very flaky. Should go away soon.
65 1.1 mycroft */
66 1.1 mycroft #define AIC_ALLOW_DISCONNECT 1
67 1.1 mycroft
68 1.1 mycroft /* Synchronous data transfers? (does not work yet!) XXX */
69 1.1 mycroft #define AIC_USE_SYNCHRONOUS 0 /* Enable/disable (1/0) */
70 1.1 mycroft #define AIC_SYNC_PERIOD 200
71 1.1 mycroft #define AIC_SYNC_REQ_ACK_OFS 8
72 1.1 mycroft
73 1.1 mycroft /* Max attempts made to transmit a message */
74 1.1 mycroft #define AIC_MSG_MAX_ATTEMPT 3 /* Not used now XXX */
75 1.1 mycroft
76 1.1 mycroft /* Use DMA (else we do programmed I/O using string instructions) (not yet!)*/
77 1.1 mycroft #define AIC_USE_EISA_DMA 0
78 1.1 mycroft #define AIC_USE_ISA_DMA 0
79 1.1 mycroft
80 1.1 mycroft /* How to behave on the (E)ISA bus when/if DMAing (on<<4) + off in us */
81 1.1 mycroft #define EISA_BRST_TIM ((15<<4) + 1) /* 15us on, 1us off */
82 1.1 mycroft
83 1.1 mycroft /* Some spin loop parameters (essentially how long to wait some places)
84 1.1 mycroft * The problem(?) is that sometimes we expect either to be able to transmit a
85 1.1 mycroft * byte or to get a new one from the SCSI bus pretty soon. In order to avoid
86 1.1 mycroft * returning from the interrupt just to get yanked back for the next byte we
87 1.1 mycroft * may spin in the interrupt routine waiting for this byte to come. How long?
88 1.1 mycroft * This is really (SCSI) device and processor dependent. Tuneable, I guess.
89 1.1 mycroft */
90 1.1 mycroft #define AIC_MSGI_SPIN 1 /* Will spinwait upto ?ms for a new msg byte */
91 1.1 mycroft #define AIC_MSGO_SPIN 1
92 1.1 mycroft
93 1.1 mycroft /* Include debug functions? At the end of this file there are a bunch of
94 1.1 mycroft * functions that will print out various information regarding queued SCSI
95 1.1 mycroft * commands, driver state and chip contents. You can call them from the
96 1.1 mycroft * kernel debugger. If you set AIC_DEBUG to 0 they are not included (the
97 1.1 mycroft * kernel uses less memory) but you lose the debugging facilities.
98 1.1 mycroft */
99 1.1 mycroft #define AIC_DEBUG 1
100 1.1 mycroft
101 1.1 mycroft /* End of customizable parameters */
102 1.1 mycroft
103 1.1 mycroft #if AIC_USE_EISA_DMA || AIC_USE_ISA_DMA
104 1.1 mycroft #error "I said not yet! Start paying attention... grumble"
105 1.1 mycroft #endif
106 1.1 mycroft
107 1.1 mycroft #include <sys/types.h>
108 1.1 mycroft #include <sys/param.h>
109 1.1 mycroft #include <sys/systm.h>
110 1.1 mycroft #include <sys/kernel.h>
111 1.1 mycroft #include <sys/errno.h>
112 1.1 mycroft #include <sys/ioctl.h>
113 1.1 mycroft #include <sys/device.h>
114 1.1 mycroft #include <sys/buf.h>
115 1.1 mycroft #include <sys/proc.h>
116 1.1 mycroft #include <sys/user.h>
117 1.1 mycroft #include <sys/queue.h>
118 1.1 mycroft
119 1.1 mycroft #include <machine/pio.h>
120 1.1 mycroft
121 1.1 mycroft #include <scsi/scsi_all.h>
122 1.1 mycroft #include <scsi/scsiconf.h>
123 1.1 mycroft
124 1.1 mycroft #include <i386/isa/isavar.h>
125 1.1 mycroft #include <i386/isa/icu.h>
126 1.1 mycroft
127 1.1 mycroft /* Definitions, most of them has turned out to be unneccesary, but here they
128 1.1 mycroft * are anyway.
129 1.1 mycroft */
130 1.1 mycroft
131 1.1 mycroft /*
132 1.1 mycroft * Generic SCSI messages. For now we reject most of them.
133 1.1 mycroft */
134 1.1 mycroft /* Messages (1 byte) */ /* I/T M(andatory) or (O)ptional */
135 1.1 mycroft #define MSG_CMDCOMPLETE 0x00 /* M/M */
136 1.1 mycroft #define MSG_EXTENDED 0x01 /* O/O */
137 1.1 mycroft #define MSG_SAVEDATAPOINTER 0x02 /* O/O */
138 1.1 mycroft #define MSG_RESTOREPOINTERS 0x03 /* O/O */
139 1.1 mycroft #define MSG_DISCONNECT 0x04 /* O/O */
140 1.1 mycroft #define MSG_INITIATOR_DET_ERR 0x05 /* M/M */
141 1.1 mycroft #define MSG_ABORT 0x06 /* O/M */
142 1.1 mycroft #define MSG_MESSAGE_REJECT 0x07 /* M/M */
143 1.1 mycroft #define MSG_NOOP 0x08 /* M/M */
144 1.1 mycroft #define MSG_PARITY_ERR 0x09 /* M/M */
145 1.1 mycroft #define MSG_LINK_CMD_COMPLETE 0x0a /* O/O */
146 1.1 mycroft #define MSG_LINK_CMD_COMPLETEF 0x0b /* O/O */
147 1.1 mycroft #define MSG_BUS_DEV_RESET 0x0c /* O/M */
148 1.1 mycroft #define MSG_ABORT_TAG 0x0d /* O/O */
149 1.1 mycroft #define MSG_CLEAR_QUEUE 0x0e /* O/O */
150 1.1 mycroft #define MSG_INIT_RECOVERY 0x0f /* O/O */
151 1.1 mycroft #define MSG_REL_RECOVERY 0x10 /* O/O */
152 1.1 mycroft #define MSG_TERM_IO_PROC 0x11 /* O/O */
153 1.1 mycroft
154 1.1 mycroft /* Messages (2 byte) */
155 1.1 mycroft #define MSG_SIMPLE_Q_TAG 0x20 /* O/O */
156 1.1 mycroft #define MSG_HEAD_OF_Q_TAG 0x21 /* O/O */
157 1.1 mycroft #define MSG_ORDERED_Q_TAG 0x22 /* O/O */
158 1.1 mycroft #define MSG_IGN_WIDE_RESIDUE 0x23 /* O/O */
159 1.1 mycroft
160 1.1 mycroft /* Identify message */
161 1.1 mycroft #define MSG_IDENTIFY(lun) ((AIC_ALLOW_DISCONNECT ? 0xc0 : 0x80)|((lun) & 0x7))
162 1.1 mycroft #define MSG_ISIDENT(m) ((m) & 0x80)
163 1.1 mycroft
164 1.1 mycroft /* Extended messages (opcode) */
165 1.1 mycroft #define MSG_EXT_SDTR 0x01
166 1.1 mycroft
167 1.1 mycroft /* SCSI Status codes */
168 1.1 mycroft #define ST_GOOD 0x00
169 1.1 mycroft #define ST_CHKCOND 0x02
170 1.1 mycroft #define ST_CONDMET 0x04
171 1.1 mycroft #define ST_BUSY 0x08
172 1.1 mycroft #define ST_INTERMED 0x10
173 1.1 mycroft #define ST_INTERMED_CONDMET 0x14
174 1.1 mycroft #define ST_RESERVATION_CONFLICT 0x18
175 1.1 mycroft #define ST_CMD_TERM 0x22
176 1.1 mycroft #define ST_QUEUE_FULL 0x28
177 1.1 mycroft
178 1.1 mycroft #define ST_MASK 0x3e /* bit 0,6,7 is reserved */
179 1.1 mycroft
180 1.1 mycroft /* AIC6360 definitions */
181 1.1 mycroft #define SCSISEQ (iobase + 0x00) /* SCSI sequence control */
182 1.1 mycroft #define SXFRCTL0 (iobase + 0x01) /* SCSI transfer control 0 */
183 1.1 mycroft #define SXFRCTL1 (iobase + 0x02) /* SCSI transfer control 1 */
184 1.1 mycroft #define SCSISIGI (iobase + 0x03) /* SCSI signal in */
185 1.1 mycroft #define SCSISIGO (iobase + 0x03) /* SCSI signal out */
186 1.1 mycroft #define SCSIRATE (iobase + 0x04) /* SCSI rate control */
187 1.1 mycroft #define SCSIID (iobase + 0x05) /* SCSI ID */
188 1.1 mycroft #define SELID (iobase + 0x05) /* Selection/Reselection ID */
189 1.1 mycroft #define SCSIDAT (iobase + 0x06) /* SCSI Latched Data */
190 1.1 mycroft #define SCSIBUS (iobase + 0x07) /* SCSI Data Bus*/
191 1.1 mycroft #define STCNT0 (iobase + 0x08) /* SCSI transfer count */
192 1.1 mycroft #define STCNT1 (iobase + 0x09)
193 1.1 mycroft #define STCNT2 (iobase + 0x0a)
194 1.1 mycroft #define CLRSINT0 (iobase + 0x0b) /* Clear SCSI interrupts 0 */
195 1.1 mycroft #define SSTAT0 (iobase + 0x0b) /* SCSI interrupt status 0 */
196 1.1 mycroft #define CLRSINT1 (iobase + 0x0c) /* Clear SCSI interrupts 1 */
197 1.1 mycroft #define SSTAT1 (iobase + 0x0c) /* SCSI status 1 */
198 1.1 mycroft #define SSTAT2 (iobase + 0x0d) /* SCSI status 2 */
199 1.1 mycroft #define SCSITEST (iobase + 0x0e) /* SCSI test control */
200 1.1 mycroft #define SSTAT3 (iobase + 0x0e) /* SCSI status 3 */
201 1.1 mycroft #define CLRSERR (iobase + 0x0f) /* Clear SCSI errors */
202 1.1 mycroft #define SSTAT4 (iobase + 0x0f) /* SCSI status 4 */
203 1.1 mycroft #define SIMODE0 (iobase + 0x10) /* SCSI interrupt mode 0 */
204 1.1 mycroft #define SIMODE1 (iobase + 0x11) /* SCSI interrupt mode 1 */
205 1.1 mycroft #define DMACNTRL0 (iobase + 0x12) /* DMA control 0 */
206 1.1 mycroft #define DMACNTRL1 (iobase + 0x13) /* DMA control 1 */
207 1.1 mycroft #define DMASTAT (iobase + 0x14) /* DMA status */
208 1.1 mycroft #define FIFOSTAT (iobase + 0x15) /* FIFO status */
209 1.1 mycroft #define DMADATA (iobase + 0x16) /* DMA data */
210 1.1 mycroft #define DMADATAL (iobase + 0x16) /* DMA data low byte */
211 1.1 mycroft #define DMADATAH (iobase + 0x17) /* DMA data high byte */
212 1.1 mycroft #define BRSTCNTRL (iobase + 0x18) /* Burst Control */
213 1.1 mycroft #define DMADATALONG (iobase + 0x18)
214 1.1 mycroft #define PORTA (iobase + 0x1a) /* Port A */
215 1.1 mycroft #define PORTB (iobase + 0x1b) /* Port B */
216 1.1 mycroft #define REV (iobase + 0x1c) /* Revision (001 for 6360) */
217 1.1 mycroft #define STACK (iobase + 0x1d) /* Stack */
218 1.1 mycroft #define TEST (iobase + 0x1e) /* Test register */
219 1.1 mycroft #define ID (iobase + 0x1f) /* ID register */
220 1.1 mycroft
221 1.1 mycroft #define IDSTRING "(C)1991ADAPTECAIC6360 "
222 1.1 mycroft
223 1.1 mycroft /* What all the bits do */
224 1.1 mycroft
225 1.1 mycroft /* SCSISEQ */
226 1.1 mycroft #define TEMODEO 0x80
227 1.1 mycroft #define ENSELO 0x40
228 1.1 mycroft #define ENSELI 0x20
229 1.1 mycroft #define ENRESELI 0x10
230 1.1 mycroft #define ENAUTOATNO 0x08
231 1.1 mycroft #define ENAUTOATNI 0x04
232 1.1 mycroft #define ENAUTOATNP 0x02
233 1.1 mycroft #define SCSIRSTO 0x01
234 1.1 mycroft
235 1.1 mycroft /* SXFRCTL0 */
236 1.1 mycroft #define SCSIEN 0x80
237 1.1 mycroft #define DMAEN 0x40
238 1.1 mycroft #define CHEN 0x20
239 1.1 mycroft #define CLRSTCNT 0x10
240 1.1 mycroft #define SPIOEN 0x08
241 1.1 mycroft #define CLRCH 0x02
242 1.1 mycroft
243 1.1 mycroft /* SXFRCTL1 */
244 1.1 mycroft #define BITBUCKET 0x80
245 1.1 mycroft #define SWRAPEN 0x40
246 1.1 mycroft #define ENSPCHK 0x20
247 1.1 mycroft #define STIMESEL1 0x10
248 1.1 mycroft #define STIMESEL0 0x08
249 1.1 mycroft #define STIMO_256ms 0x00
250 1.1 mycroft #define STIMO_128ms 0x08
251 1.1 mycroft #define STIMO_64ms 0x10
252 1.1 mycroft #define STIMO_32ms 0x18
253 1.1 mycroft #define ENSTIMER 0x04
254 1.1 mycroft #define BYTEALIGN 0x02
255 1.1 mycroft
256 1.1 mycroft /* SCSISIGI */
257 1.1 mycroft #define CDI 0x80
258 1.1 mycroft #define IOI 0x40
259 1.1 mycroft #define MSGI 0x20
260 1.1 mycroft #define ATNI 0x10
261 1.1 mycroft #define SELI 0x08
262 1.1 mycroft #define BSYI 0x04
263 1.1 mycroft #define REQI 0x02
264 1.1 mycroft #define ACKI 0x01
265 1.1 mycroft
266 1.1 mycroft /* Important! The 3 most significant bits of this register, in initiator mode,
267 1.1 mycroft * represents the "expected" SCSI bus phase and can be used to trigger phase
268 1.1 mycroft * mismatch and phase change interrupts. But more important: If there is a
269 1.1 mycroft * phase mismatch the chip will not transfer any data! This is actually a nice
270 1.1 mycroft * feature as it gives us a bit more control over what is happening when we are
271 1.1 mycroft * bursting data (in) through the FIFOs and the phase suddenly changes from
272 1.1 mycroft * DATA IN to STATUS or MESSAGE IN. The transfer will stop and wait for the
273 1.1 mycroft * proper phase to be set in this register instead of dumping the bits into the
274 1.1 mycroft * FIFOs.
275 1.1 mycroft */
276 1.1 mycroft /* SCSISIGO */
277 1.1 mycroft #define CDO 0x80
278 1.1 mycroft #define CDEXP (CDO)
279 1.1 mycroft #define IOO 0x40
280 1.1 mycroft #define IOEXP (IOO)
281 1.1 mycroft #define MSGO 0x20
282 1.1 mycroft #define MSGEXP (MSGO)
283 1.1 mycroft #define ATNO 0x10
284 1.1 mycroft #define SELO 0x08
285 1.1 mycroft #define BSYO 0x04
286 1.1 mycroft #define REQO 0x02
287 1.1 mycroft #define ACKO 0x01
288 1.1 mycroft
289 1.1 mycroft /* Information transfer phases */
290 1.1 mycroft #define PH_DOUT (0)
291 1.1 mycroft #define PH_DIN (IOI)
292 1.1 mycroft #define PH_CMD (CDI)
293 1.1 mycroft #define PH_STAT (CDI|IOI)
294 1.1 mycroft #define PH_MSGO (MSGI|CDI)
295 1.1 mycroft #define PH_MSGI (MSGI|CDI|IOI)
296 1.1 mycroft
297 1.1 mycroft #define PH_MASK 0xe0
298 1.1 mycroft
299 1.1 mycroft /* Some pseudo phases for getphase()*/
300 1.1 mycroft #define PH_BUSFREE 0x100 /* (Re)Selection no longer valid */
301 1.1 mycroft #define PH_INVALID 0x101 /* (Re)Selection valid, but no REQ yet */
302 1.1 mycroft #define PH_PSBIT 0x100 /* "pseudo" bit */
303 1.1 mycroft
304 1.1 mycroft /* SCSIRATE */
305 1.1 mycroft #define SXFR2 0x40
306 1.1 mycroft #define SXFR1 0x20
307 1.1 mycroft #define SXFR0 0x10
308 1.1 mycroft #define SOFS3 0x08
309 1.1 mycroft #define SOFS2 0x04
310 1.1 mycroft #define SOFS1 0x02
311 1.1 mycroft #define SOFS0 0x01
312 1.1 mycroft
313 1.1 mycroft /* SCSI ID */
314 1.1 mycroft #define OID2 0x40
315 1.1 mycroft #define OID1 0x20
316 1.1 mycroft #define OID0 0x10
317 1.1 mycroft #define OID_S 4 /* shift value */
318 1.1 mycroft #define TID2 0x04
319 1.1 mycroft #define TID1 0x02
320 1.1 mycroft #define TID0 0x01
321 1.1 mycroft #define SCSI_ID_MASK 0x7
322 1.1 mycroft
323 1.1 mycroft /* SCSI selection/reselection ID (both target *and* initiator) */
324 1.1 mycroft #define SELID7 0x80
325 1.1 mycroft #define SELID6 0x40
326 1.1 mycroft #define SELID5 0x20
327 1.1 mycroft #define SELID4 0x10
328 1.1 mycroft #define SELID3 0x08
329 1.1 mycroft #define SELID2 0x04
330 1.1 mycroft #define SELID1 0x02
331 1.1 mycroft #define SELID0 0x01
332 1.1 mycroft
333 1.1 mycroft /* CLRSINT0 Clears what? (interrupt and/or status bit) */
334 1.1 mycroft #define SETSDONE 0x80
335 1.1 mycroft #define CLRSELDO 0x40 /* I */
336 1.1 mycroft #define CLRSELDI 0x20 /* I+ */
337 1.1 mycroft #define CLRSELINGO 0x10 /* I */
338 1.1 mycroft #define CLRSWRAP 0x08 /* I+S */
339 1.1 mycroft #define CLRSDONE 0x04 /* I+S */
340 1.1 mycroft #define CLRSPIORDY 0x02 /* I */
341 1.1 mycroft #define CLRDMADONE 0x01 /* I */
342 1.1 mycroft
343 1.1 mycroft /* SSTAT0 Howto clear */
344 1.1 mycroft #define TARGET 0x80
345 1.1 mycroft #define SELDO 0x40 /* Selfclearing */
346 1.1 mycroft #define SELDI 0x20 /* Selfclearing when CLRSELDI is set */
347 1.1 mycroft #define SELINGO 0x10 /* Selfclearing */
348 1.1 mycroft #define SWRAP 0x08 /* CLRSWAP */
349 1.1 mycroft #define SDONE 0x04 /* Not used in initiator mode */
350 1.1 mycroft #define SPIORDY 0x02 /* Selfclearing (op on SCSIDAT) */
351 1.1 mycroft #define DMADONE 0x01 /* Selfclearing (all FIFOs empty & T/C */
352 1.1 mycroft
353 1.1 mycroft /* CLRSINT1 Clears what? */
354 1.1 mycroft #define CLRSELTIMO 0x80 /* I+S */
355 1.1 mycroft #define CLRATNO 0x40
356 1.1 mycroft #define CLRSCSIRSTI 0x20 /* I+S */
357 1.1 mycroft #define CLRBUSFREE 0x08 /* I+S */
358 1.1 mycroft #define CLRSCSIPERR 0x04 /* I+S */
359 1.1 mycroft #define CLRPHASECHG 0x02 /* I+S */
360 1.1 mycroft #define CLRREQINIT 0x01 /* I+S */
361 1.1 mycroft
362 1.1 mycroft /* SSTAT1 How to clear? When set?*/
363 1.1 mycroft #define SELTO 0x80 /* C select out timeout */
364 1.1 mycroft #define ATNTARG 0x40 /* Not used in initiator mode */
365 1.1 mycroft #define SCSIRSTI 0x20 /* C RST asserted */
366 1.1 mycroft #define PHASEMIS 0x10 /* Selfclearing */
367 1.1 mycroft #define BUSFREE 0x08 /* C bus free condition */
368 1.1 mycroft #define SCSIPERR 0x04 /* C parity error on inbound data */
369 1.1 mycroft #define PHASECHG 0x02 /* C phase in SCSISIGI doesn't match */
370 1.1 mycroft #define REQINIT 0x01 /* C or ACK asserting edge of REQ */
371 1.1 mycroft
372 1.1 mycroft /* SSTAT2 */
373 1.1 mycroft #define SOFFSET 0x20
374 1.1 mycroft #define SEMPTY 0x10
375 1.1 mycroft #define SFULL 0x08
376 1.1 mycroft #define SFCNT2 0x04
377 1.1 mycroft #define SFCNT1 0x02
378 1.1 mycroft #define SFCNT0 0x01
379 1.1 mycroft
380 1.1 mycroft /* SCSITEST */
381 1.1 mycroft #define SCTESTU 0x08
382 1.1 mycroft #define SCTESTD 0x04
383 1.1 mycroft #define STCTEST 0x01
384 1.1 mycroft
385 1.1 mycroft /* SSTAT3 */
386 1.1 mycroft #define SCSICNT3 0x80
387 1.1 mycroft #define SCSICNT2 0x40
388 1.1 mycroft #define SCSICNT1 0x20
389 1.1 mycroft #define SCSICNT0 0x10
390 1.1 mycroft #define OFFCNT3 0x08
391 1.1 mycroft #define OFFCNT2 0x04
392 1.1 mycroft #define OFFCNT1 0x02
393 1.1 mycroft #define OFFCNT0 0x01
394 1.1 mycroft
395 1.1 mycroft /* CLRSERR */
396 1.1 mycroft #define CLRSYNCERR 0x04
397 1.1 mycroft #define CLRFWERR 0x02
398 1.1 mycroft #define CLRFRERR 0x01
399 1.1 mycroft
400 1.1 mycroft /* SSTAT4 */
401 1.1 mycroft #define SYNCERR 0x04
402 1.1 mycroft #define FWERR 0x02
403 1.1 mycroft #define FRERR 0x01
404 1.1 mycroft
405 1.1 mycroft /* SIMODE0 */
406 1.1 mycroft #define ENSELDO 0x40
407 1.1 mycroft #define ENSELDI 0x20
408 1.1 mycroft #define ENSELINGO 0x10
409 1.1 mycroft #define ENSWRAP 0x08
410 1.1 mycroft #define ENSDONE 0x04
411 1.1 mycroft #define ENSPIORDY 0x02
412 1.1 mycroft #define ENDMADONE 0x01
413 1.1 mycroft
414 1.1 mycroft /* SIMODE1 */
415 1.1 mycroft #define ENSELTIMO 0x80
416 1.1 mycroft #define ENATNTARG 0x40
417 1.1 mycroft #define ENSCSIRST 0x20
418 1.1 mycroft #define ENPHASEMIS 0x10
419 1.1 mycroft #define ENBUSFREE 0x08
420 1.1 mycroft #define ENSCSIPERR 0x04
421 1.1 mycroft #define ENPHASECHG 0x02
422 1.1 mycroft #define ENREQINIT 0x01
423 1.1 mycroft
424 1.1 mycroft /* DMACNTRL0 */
425 1.1 mycroft #define ENDMA 0x80
426 1.1 mycroft #define B8MODE 0x40
427 1.1 mycroft #define DMA 0x20
428 1.1 mycroft #define DWORDPIO 0x10
429 1.1 mycroft #define WRITE 0x08
430 1.1 mycroft #define INTEN 0x04
431 1.1 mycroft #define RSTFIFO 0x02
432 1.1 mycroft #define SWINT 0x01
433 1.1 mycroft
434 1.1 mycroft /* DMACNTRL1 */
435 1.1 mycroft #define PWRDWN 0x80
436 1.1 mycroft #define ENSTK32 0x40
437 1.1 mycroft #define STK4 0x10
438 1.1 mycroft #define STK3 0x08
439 1.1 mycroft #define STK2 0x04
440 1.1 mycroft #define STK1 0x02
441 1.1 mycroft #define STK0 0x01
442 1.1 mycroft
443 1.1 mycroft /* DMASTAT */
444 1.1 mycroft #define ATDONE 0x80
445 1.1 mycroft #define WORDRDY 0x40
446 1.1 mycroft #define INTSTAT 0x20
447 1.1 mycroft #define DFIFOFULL 0x10
448 1.1 mycroft #define DFIFOEMP 0x08
449 1.1 mycroft #define DFIFOHF 0x04
450 1.1 mycroft #define DWORDRDY 0x02
451 1.1 mycroft
452 1.1 mycroft /* BRSTCNTRL */
453 1.1 mycroft #define BON3 0x80
454 1.1 mycroft #define BON2 0x40
455 1.1 mycroft #define BON1 0x20
456 1.1 mycroft #define BON0 0x10
457 1.1 mycroft #define BOFF3 0x08
458 1.1 mycroft #define BOFF2 0x04
459 1.1 mycroft #define BOFF1 0x02
460 1.1 mycroft #define BOFF0 0x01
461 1.1 mycroft
462 1.1 mycroft /* TEST */
463 1.1 mycroft #define BOFFTMR 0x40
464 1.1 mycroft #define BONTMR 0x20
465 1.1 mycroft #define STCNTH 0x10
466 1.1 mycroft #define STCNTM 0x08
467 1.1 mycroft #define STCNTL 0x04
468 1.1 mycroft #define SCSIBLK 0x02
469 1.1 mycroft #define DMABLK 0x01
470 1.1 mycroft
471 1.1 mycroft
472 1.1 mycroft #define orreg(reg, val) outb((reg), inb(reg)| (val))
473 1.1 mycroft #define andreg(reg, val) outb((reg), inb(reg)& (val))
474 1.1 mycroft #define nandreg(reg, val) outb((reg), inb(reg)&~(val))
475 1.1 mycroft
476 1.1 mycroft
477 1.1 mycroft
479 1.1 mycroft /* Grabbed from Julians SCSI aha-drivers */
480 1.1 mycroft #ifdef DDB
481 1.1 mycroft int Debugger();
482 1.1 mycroft #else DDB
483 1.1 mycroft #define Debugger() panic("should call debugger here (aic6360.c)")
484 1.1 mycroft #endif DDB
485 1.2 mycroft
486 1.1 mycroft typedef u_long physaddr;
487 1.1 mycroft
488 1.1 mycroft struct aic_dma_seg {
489 1.1 mycroft physaddr addr;
490 1.1 mycroft long len;
491 1.1 mycroft };
492 1.1 mycroft
493 1.1 mycroft extern int delaycount;
494 1.1 mycroft #define FUDGE(X) ((X)>>1) /* get 1 ms spincount */
495 1.1 mycroft #define MINIFUDGE(X) ((X)>>4) /* get (approx) 125us spincount */
496 1.1 mycroft #define AIC_NSEG 16
497 1.1 mycroft #define NUM_CONCURRENT 7 /* Only one per target for now */
498 1.1 mycroft
499 1.1 mycroft /*
500 1.1 mycroft * ACB. Holds additional information for each SCSI command Comments: We
501 1.1 mycroft * need a separate scsi command block because we may need to overwrite it
502 1.1 mycroft * with a request sense command. Basicly, we refrain from fiddling with
503 1.1 mycroft * the scsi_xfer struct (except do the expected updating of return values).
504 1.1 mycroft * We'll generally update: xs->{flags,resid,error,sense,status} and
505 1.1 mycroft * occasionally xs->retries.
506 1.1 mycroft */
507 1.1 mycroft struct acb {
508 1.1 mycroft TAILQ_ENTRY(acb) chain;
509 1.1 mycroft struct scsi_xfer *xs; /* SCSI xfer ctrl block from above */
510 1.1 mycroft int flags; /* Status */
511 1.1 mycroft #define ACB_FREE 0x00
512 1.1 mycroft #define ACB_ACTIVE 0x01
513 1.1 mycroft #define ACB_DONE 0x04
514 1.1 mycroft #define ACB_CHKSENSE 0x08
515 1.1 mycroft /* struct aic_dma_seg dma[AIC_NSEG]; /* Physical addresses+len */
516 1.1 mycroft struct scsi_generic cmd; /* SCSI command block */
517 1.1 mycroft int clen;
518 1.1 mycroft char *daddr; /* Saved data pointer */
519 1.1 mycroft int dleft; /* Residue */
520 1.1 mycroft int stat; /* SCSI status byte */
521 1.1 mycroft };
522 1.1 mycroft
523 1.1 mycroft /*
524 1.1 mycroft * Some info about each (possible) target on the SCSI bus. This should
525 1.1 mycroft * probably have been a "per target+lunit" structure, but we'll leave it at
526 1.1 mycroft * this for now. Is there a way to reliably hook it up to sc->fordriver??
527 1.1 mycroft */
528 1.1 mycroft struct aic_tinfo {
529 1.1 mycroft int cmds; /* #commands processed */
530 1.1 mycroft int dconns; /* #disconnects */
531 1.1 mycroft int touts; /* #timeouts */
532 1.1 mycroft int perrs; /* #parity errors */
533 1.1 mycroft int senses; /* #request sense commands sent */
534 1.1 mycroft ushort lubusy; /* What local units/subr. are busy? */
535 1.1 mycroft u_char flags;
536 1.1 mycroft #define NEED_TO_RESET 0x01 /* Should send a BUS_DEV_RESET */
537 1.1 mycroft #define DO_NEGOTIATE 0x02 /* (Re)Negotiate synchronous options */
538 1.1 mycroft #define TARGET_BUSY 0x04 /* Target is busy, i.e. cmd in progress */
539 1.1 mycroft u_char persgst; /* Period suggestion */
540 1.1 mycroft u_char offsgst; /* Offset suggestion */
541 1.1 mycroft u_char syncdata; /* True negotiated synch parameters */
542 1.1 mycroft } tinfo_t;
543 1.1 mycroft
544 1.1 mycroft /* Register a linenumber (for debugging) */
545 1.2 mycroft #if AIC_DEBUG
546 1.2 mycroft #define LOGLINE(p) \
547 1.2 mycroft do { \
548 1.2 mycroft p->history[p->hp] = __LINE__; \
549 1.2 mycroft p->hp = ++p->hp % AIC_HSIZE; \
550 1.1 mycroft } while (0)
551 1.1 mycroft #else
552 1.1 mycroft #define LOGLINE(p)
553 1.1 mycroft #endif
554 1.1 mycroft
555 1.1 mycroft struct aic_softc { /* One of these per adapter */
556 1.1 mycroft /* Auto config stuff */
557 1.1 mycroft struct device sc_dev; /* This one has to go first! */
558 1.1 mycroft struct isadev sc_id;
559 1.1 mycroft struct intrhand sc_ih;
560 1.1 mycroft struct scsi_link sc_link; /* prototype for subdevs */
561 1.1 mycroft int id_irq; /* IRQ on the EISA bus */
562 1.1 mycroft int id_drq; /* DRQ on the EISA bus */
563 1.1 mycroft u_short iobase; /* Base I/O port */
564 1.1 mycroft /* Lists of command blocks */
565 1.1 mycroft TAILQ_HEAD(acb_list, acb) free_list, ready_list, nexus_list;
566 1.1 mycroft struct acb *nexus; /* current command */
567 1.1 mycroft /* Command blocks and target info */
568 1.1 mycroft struct acb acb[NUM_CONCURRENT];
569 1.1 mycroft struct aic_tinfo tinfo[8];
570 1.1 mycroft /* Data about the current nexus (updated for every cmd switch) */
571 1.1 mycroft u_char *dp; /* Current data pointer */
572 1.1 mycroft int dleft; /* Data left to transfer */
573 1.1 mycroft /* Adapter state */
574 1.1 mycroft short phase; /* Copy of what bus phase we are in */
575 1.1 mycroft short prevphase; /* Copy of what bus phase we were in */
576 1.1 mycroft short state; /* State applicable to the adapter */
577 1.1 mycroft #define AIC_IDLE 0x01
578 1.1 mycroft #define AIC_TMP_UNAVAIL 0x02 /* Don't accept SCSI commands */
579 1.1 mycroft #define AIC_SELECTING 0x03 /* SCSI command is arbiting */
580 1.1 mycroft #define AIC_RESELECTED 0x04 /* Has been reselected */
581 1.1 mycroft #define AIC_HASNEXUS 0x05 /* Actively using the SCSI bus */
582 1.1 mycroft #define AIC_CLEANING 0x06
583 1.1 mycroft short flags;
584 1.1 mycroft #define AIC_DROP_MSGI 0x01 /* Discard all msgs (parity err detected) */
585 1.1 mycroft #define AIC_DOINGDMA 0x02 /* The FIFO data path is active! */
586 1.1 mycroft #define AIC_BUSFREE_OK 0x04 /* Bus free phase is OK. */
587 1.1 mycroft #define AIC_SYNCHNEGO 0x08 /* Synch negotiation in progress. */
588 1.1 mycroft #define AIC_BLOCKED 0x10 /* Don't schedule new scsi bus operations */
589 1.1 mycroft /* Debugging stuff */
590 1.1 mycroft #define AIC_HSIZE 8
591 1.1 mycroft short history[AIC_HSIZE]; /* Store line numbers here. */
592 1.1 mycroft short hp;
593 1.1 mycroft u_char progress; /* Set if interrupt has achieved progress */
594 1.1 mycroft /* Message stuff */
595 1.1 mycroft u_char msgpriq; /* One or more messages to send (encoded) */
596 1.1 mycroft u_char msgout; /* What message is on its way out? */
597 1.1 mycroft #define SEND_DEV_RESET 0x01
598 1.1 mycroft #define SEND_PARITY_ERROR 0x02
599 1.1 mycroft #define SEND_ABORT 0x04
600 1.1 mycroft #define SEND_REJECT 0x08
601 1.1 mycroft #define SEND_INIT_DET_ERR 0x10
602 1.1 mycroft #define SEND_IDENTIFY 0x20
603 1.1 mycroft #define SEND_SDTR 0x40
604 1.1 mycroft #define AIC_MAX_MSG_LEN 8
605 1.1 mycroft u_char omess[AIC_MAX_MSG_LEN]; /* Scratch area for messages */
606 1.1 mycroft u_char *omp; /* Message pointer (for multibyte messages) */
607 1.1 mycroft u_char omlen;
608 1.1 mycroft u_char imess[AIC_MAX_MSG_LEN + 1];
609 1.1 mycroft u_char *imp; /* Message pointer (for multibyte messages) */
610 1.1 mycroft u_char imlen;
611 1.1 mycroft };
612 1.1 mycroft
613 1.1 mycroft #define AIC_SHOWACBS 0x01
614 1.1 mycroft #define AIC_SHOWINTS 0x02
615 1.1 mycroft #define AIC_SHOWCMDS 0x04
616 1.1 mycroft #define AIC_SHOWMISC 0x08
617 1.1 mycroft #define AIC_SHOWTRAC 0x10
618 1.1 mycroft #define AIC_SHOWSTART 0x20
619 1.1 mycroft int aic_debug = 0; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRAC; /**/
620 1.1 mycroft
621 1.1 mycroft #if AIC_DEBUG
622 1.1 mycroft #define AIC_ACBS(str) do {if (aic_debug & AIC_SHOWACBS) printf str;} while (0)
623 1.1 mycroft #define AIC_MISC(str) do {if (aic_debug & AIC_SHOWMISC) printf str;} while (0)
624 1.1 mycroft #define AIC_INTS(str) do {if (aic_debug & AIC_SHOWINTS) printf str;} while (0)
625 1.1 mycroft #define AIC_TRACE(str) do {if (aic_debug & AIC_SHOWTRAC) printf str;} while (0)
626 1.1 mycroft #define AIC_CMDS(str) do {if (aic_debug & AIC_SHOWCMDS) printf str;} while (0)
627 1.1 mycroft #define AIC_START(str) do {if (aic_debug & AIC_SHOWSTART) printf str;}while (0)
628 1.1 mycroft #else
629 1.1 mycroft #define AIC_ACBS(str)
630 1.1 mycroft #define AIC_MISC(str)
631 1.1 mycroft #define AIC_INTS(str)
632 1.1 mycroft #define AIC_TRACE(str)
633 1.1 mycroft #define AIC_CMDS(str)
634 1.1 mycroft #define AIC_START(str)
635 1.1 mycroft #endif
636 1.1 mycroft
637 1.1 mycroft int aicprobe __P((struct device *, struct device *, void *));
638 1.1 mycroft void aicattach __P((struct device *, struct device *, void *));
639 1.1 mycroft void aic_minphys __P((struct buf *));
640 1.1 mycroft u_int aic_adapter_info __P((struct aic_softc *));
641 1.1 mycroft int aicintr __P((struct aic_softc *));
642 1.1 mycroft void aic_init __P((struct aic_softc *));
643 1.1 mycroft void aic_done __P((struct acb *));
644 1.1 mycroft int aic_scsi_cmd __P((struct scsi_xfer *));
645 1.1 mycroft int aic_poll __P((struct aic_softc *, struct acb *));
646 1.1 mycroft void aic_add_timeout __P((struct acb *, int));
647 1.5 cgd void aic_remove_timeout __P((struct acb *));
648 1.1 mycroft void aic_timeout __P((void *arg));
649 1.1 mycroft int aic_find __P((struct aic_softc *));
650 1.1 mycroft void aic_sched __P((struct aic_softc *));
651 1.1 mycroft void aic_scsi_reset __P((struct aic_softc *));
652 1.1 mycroft #if AIC_DEBUG
653 1.1 mycroft void aic_print_active_acb();
654 1.1 mycroft void aic_dump_driver();
655 1.1 mycroft void aic_dump6360();
656 1.1 mycroft #endif
657 1.1 mycroft
658 1.1 mycroft /* Linkup to the rest of the kernel */
659 1.1 mycroft struct cfdriver aiccd = {
660 1.1 mycroft NULL, "aic", aicprobe, aicattach, DV_DULL, sizeof(struct aic_softc)
661 1.1 mycroft };
662 1.1 mycroft
663 1.1 mycroft struct scsi_adapter aic_switch = {
664 1.1 mycroft aic_scsi_cmd,
665 1.1 mycroft aic_minphys,
666 1.1 mycroft 0,
667 1.1 mycroft 0,
668 1.1 mycroft aic_adapter_info,
669 1.1 mycroft "aic"
670 1.1 mycroft };
671 1.1 mycroft
672 1.1 mycroft struct scsi_device aic_dev = {
673 1.1 mycroft NULL, /* Use default error handler */
674 1.1 mycroft NULL, /* have a queue, served by this */
675 1.1 mycroft NULL, /* have no async handler */
676 1.1 mycroft NULL, /* Use default 'done' routine */
677 1.1 mycroft "aic",
678 1.1 mycroft 0
679 1.1 mycroft };
680 1.1 mycroft
681 1.1 mycroft /*
683 1.1 mycroft * INITIALIZATION ROUTINES (probe, attach ++)
684 1.1 mycroft */
685 1.1 mycroft
686 1.1 mycroft /*
687 1.1 mycroft * aicprobe: probe for AIC6360 SCSI-controller
688 1.1 mycroft * returns non-zero value if a controller is found.
689 1.1 mycroft */
690 1.1 mycroft int
691 1.1 mycroft aicprobe(parent, self, aux)
692 1.1 mycroft struct device *parent, *self;
693 1.1 mycroft void *aux;
694 1.1 mycroft {
695 1.1 mycroft struct aic_softc *aic = (void *)self;
696 1.1 mycroft struct isa_attach_args *ia = aux;
697 1.1 mycroft int i, len, ic;
698 1.1 mycroft
699 1.1 mycroft #ifdef NEWCONFIG
700 1.1 mycroft if (ia->ia_iobase == IOBASEUNK)
701 1.1 mycroft return 0;
702 1.1 mycroft #endif
703 1.1 mycroft aic->iobase = ia->ia_iobase;
704 1.1 mycroft if (aic_find(aic) != 0)
705 1.1 mycroft return 0;
706 1.1 mycroft #ifdef NEWCONFIG
707 1.1 mycroft if (ia->ia_irq == IRQUNK)
708 1.1 mycroft ia->ia_irq = (1 << aic->aic_int);
709 1.1 mycroft else if (ia->ia_irq != (1 << aic->aic_int)) {
710 1.1 mycroft printf("aic%d: irq mismatch, %x != %x\n",
711 1.1 mycroft aic->sc_dev.dv_unit, ia->ia_irq, 1 << aic->aic_int);
712 1.1 mycroft return 0;
713 1.1 mycroft }
714 1.1 mycroft
715 1.1 mycroft if (ia->ia_drq == DRQUNK)
716 1.1 mycroft ia->ia_drq = aic->aic_dma;
717 1.1 mycroft else if (ia->ia_drq != aic->aic_dma) {
718 1.1 mycroft printf("aic%d: drq mismatch, %x != %x\n",
719 1.1 mycroft aic->sc_dev.dv_unit, ia->ia_drq, aic->aic_dma);
720 1.1 mycroft return 0;
721 1.1 mycroft }
722 1.1 mycroft #endif
723 1.1 mycroft ia->ia_msize = 0;
724 1.1 mycroft ia->ia_iosize = 0x20;
725 1.1 mycroft return 1;
726 1.1 mycroft }
727 1.1 mycroft
728 1.1 mycroft /* Do the real search-for-device.
729 1.1 mycroft * Prerequisite: aic->iobase should be set to the proper value
730 1.1 mycroft */
731 1.1 mycroft int
732 1.1 mycroft aic_find(aic)
733 1.1 mycroft struct aic_softc *aic;
734 1.1 mycroft {
735 1.1 mycroft u_short iobase = aic->iobase;
736 1.1 mycroft char chip_id[sizeof(IDSTRING)]; /* For chips that support it */
737 1.1 mycroft char *start;
738 1.1 mycroft int i;
739 1.1 mycroft
740 1.1 mycroft /* Remove aic6360 from possible powerdown mode */
741 1.1 mycroft outb(DMACNTRL0, 0);
742 1.1 mycroft
743 1.1 mycroft /* Thanks to mark (at) aggregate.com for the new method for detecting
744 1.1 mycroft * whether the chip is present or not. Bonus: may also work for
745 1.1 mycroft * the AIC-6260!
746 1.1 mycroft */
747 1.1 mycroft AIC_TRACE(("aic: probing for aic-chip at port 0x%x\n",(int)iobase));
748 1.1 mycroft /*
749 1.1 mycroft * Linux also init's the stack to 1-16 and then clears it,
750 1.1 mycroft * 6260's don't appear to have an ID reg - mpg
751 1.1 mycroft */
752 1.1 mycroft /* Push the sequence 0,1,..,15 on the stack */
753 1.2 mycroft #define STSIZE 16
754 1.1 mycroft outb(DMACNTRL1, 0); /* Reset stack pointer */
755 1.1 mycroft for (i = 0; i < STSIZE; i++)
756 1.1 mycroft outb(STACK, i);
757 1.1 mycroft
758 1.1 mycroft /* See if we can pull out the same sequence */
759 1.1 mycroft outb(DMACNTRL1, 0);
760 1.1 mycroft for (i = 0; i < STSIZE && inb(STACK) == i; i++)
761 1.1 mycroft ;
762 1.1 mycroft if (i != STSIZE) {
763 1.1 mycroft AIC_START(("STACK futzed at %d.\n", i));
764 1.1 mycroft return ENXIO;
765 1.1 mycroft }
766 1.1 mycroft
767 1.1 mycroft /* See if we can pull the id string out of the ID register,
768 1.1 mycroft * now only used for informational purposes.
769 1.1 mycroft */
770 1.1 mycroft bzero(chip_id, sizeof(chip_id));
771 1.1 mycroft insb(ID, chip_id, sizeof(IDSTRING)-1);
772 1.1 mycroft AIC_START(("AIC found at 0x%x ", (int)aic->iobase));
773 1.1 mycroft AIC_START(("ID: %s ",chip_id));
774 1.1 mycroft AIC_START(("chip revision %d\n",(int)inb(REV)));
775 1.1 mycroft return 0;
776 1.1 mycroft }
777 1.1 mycroft
778 1.1 mycroft int
779 1.1 mycroft aicprint()
780 1.1 mycroft {
781 1.1 mycroft }
782 1.1 mycroft
783 1.1 mycroft /*
784 1.1 mycroft * Attach the AIC6360, fill out some high and low level data structures
785 1.1 mycroft */
786 1.1 mycroft void
787 1.1 mycroft aicattach(parent, self, aux)
788 1.1 mycroft struct device *parent, *self;
789 1.1 mycroft void *aux;
790 1.1 mycroft {
791 1.1 mycroft struct isa_attach_args *ia = aux;
792 1.1 mycroft struct aic_softc *aic = (void *)self;
793 1.1 mycroft
794 1.1 mycroft AIC_TRACE(("aicattach\n"));
795 1.1 mycroft aic->state = 0;
796 1.1 mycroft aic_init(aic); /* Init chip and driver */
797 1.1 mycroft
798 1.1 mycroft /*
799 1.1 mycroft * Fill in the prototype scsi_link
800 1.1 mycroft */
801 1.1 mycroft aic->sc_link.adapter_softc = aic;
802 1.1 mycroft aic->sc_link.adapter_targ = AIC_SCSI_HOSTID;
803 1.1 mycroft aic->sc_link.adapter = &aic_switch;
804 1.1 mycroft aic->sc_link.device = &aic_dev;
805 1.1 mycroft printf("\n");
806 1.1 mycroft
807 1.1 mycroft #ifdef NEWCONFIG
808 1.1 mycroft isa_establish(&aic->sc_id, &aic->sc_dev);
809 1.1 mycroft #endif
810 1.1 mycroft aic->sc_ih.ih_fun = aicintr;
811 1.1 mycroft aic->sc_ih.ih_arg = aic;
812 1.1 mycroft aic->sc_ih.ih_level = IPL_BIO;
813 1.1 mycroft intr_establish(ia->ia_irq, &aic->sc_ih);
814 1.1 mycroft
815 1.1 mycroft config_found(self, &aic->sc_link, aicprint);
816 1.1 mycroft }
817 1.1 mycroft
818 1.1 mycroft
819 1.1 mycroft /* Initialize AIC6360 chip itself
820 1.1 mycroft * The following conditions should hold:
821 1.1 mycroft * aicprobe should have succeeded, i.e. the iobase address in aic_softc must
822 1.1 mycroft * be valid.
823 1.1 mycroft */
824 1.1 mycroft static void
825 1.1 mycroft aic6360_reset(aic)
826 1.1 mycroft struct aic_softc *aic;
827 1.1 mycroft {
828 1.1 mycroft u_short iobase = aic->iobase;
829 1.1 mycroft
830 1.1 mycroft outb(SCSITEST, 0); /* Doc. recommends to clear these two */
831 1.1 mycroft outb(TEST, 0); /* registers before operations commence */
832 1.1 mycroft
833 1.1 mycroft /* Reset SCSI-FIFO and abort any transfers */
834 1.1 mycroft outb(SXFRCTL0, CHEN|CLRCH|CLRSTCNT);
835 1.1 mycroft
836 1.1 mycroft /* Reset DMA-FIFO */
837 1.1 mycroft outb(DMACNTRL0, RSTFIFO);
838 1.1 mycroft outb(DMACNTRL1, 0);
839 1.1 mycroft
840 1.1 mycroft outb(SCSISEQ, 0); /* Disable all selection features */
841 1.1 mycroft outb(SXFRCTL1, 0);
842 1.1 mycroft
843 1.1 mycroft outb(SIMODE0, 0x00); /* Disable some interrupts */
844 1.1 mycroft outb(CLRSINT0, 0x7f); /* Clear a slew of interrupts */
845 1.1 mycroft
846 1.1 mycroft outb(SIMODE1, 0x00); /* Disable some more interrupts */
847 1.1 mycroft outb(CLRSINT1, 0xef); /* Clear another slew of interrupts */
848 1.1 mycroft
849 1.1 mycroft outb(SCSIRATE, 0); /* Disable synchronous transfers */
850 1.1 mycroft
851 1.1 mycroft outb(CLRSERR, 0x07); /* Haven't seen ant errors (yet) */
852 1.1 mycroft
853 1.1 mycroft outb(SCSIID, AIC_SCSI_HOSTID << OID_S); /* Set our SCSI-ID */
854 1.1 mycroft outb(BRSTCNTRL, EISA_BRST_TIM);
855 1.1 mycroft }
856 1.1 mycroft
857 1.1 mycroft /* Pull the SCSI RST line for 500 us */
858 1.1 mycroft void
859 1.1 mycroft aic_scsi_reset(aic)
860 1.1 mycroft struct aic_softc *aic;
861 1.1 mycroft {
862 1.1 mycroft u_short iobase = aic->iobase;
863 1.1 mycroft
864 1.1 mycroft outb(SCSISEQ, SCSIRSTO);
865 1.1 mycroft delay(500);
866 1.1 mycroft outb(SCSISEQ, 0);
867 1.1 mycroft delay(50);
868 1.1 mycroft }
869 1.1 mycroft
870 1.1 mycroft /*
871 1.1 mycroft * Initialize aic SCSI driver, also (conditonally) reset the SCSI bus.
872 1.1 mycroft * The reinitialization is still buggy (e.g. on SCSI resets).
873 1.1 mycroft */
874 1.1 mycroft void
875 1.1 mycroft aic_init(aic)
876 1.1 mycroft struct aic_softc *aic;
877 1.1 mycroft {
878 1.1 mycroft u_short iobase = aic->iobase;
879 1.1 mycroft struct acb *acb;
880 1.6 mycroft int r;
881 1.1 mycroft
882 1.1 mycroft aic_scsi_reset(aic);
883 1.1 mycroft
884 1.1 mycroft aic6360_reset(aic); /* Clean up our own hardware */
885 1.1 mycroft
886 1.1 mycroft /*XXX*/ /* If not the first time (probably a reset condition),
887 1.1 mycroft * we should clean queues with active commands
888 1.1 mycroft */
889 1.1 mycroft if (aic->state == 0) { /* First time through */
890 1.1 mycroft TAILQ_INIT(&aic->ready_list);
891 1.1 mycroft TAILQ_INIT(&aic->nexus_list);
892 1.1 mycroft TAILQ_INIT(&aic->free_list);
893 1.1 mycroft aic->nexus = 0;
894 1.1 mycroft acb = aic->acb;
895 1.1 mycroft bzero(acb, sizeof(aic->acb));
896 1.1 mycroft for (r = 0; r < sizeof(aic->acb) / sizeof(*acb); r++) {
897 1.1 mycroft TAILQ_INSERT_TAIL(&aic->free_list, acb, chain);
898 1.1 mycroft acb++;
899 1.2 mycroft }
900 1.1 mycroft bzero(&aic->tinfo, sizeof(aic->tinfo));
901 1.1 mycroft } else {
902 1.1 mycroft aic->state = AIC_CLEANING;
903 1.5 cgd if (aic->nexus != NULL) {
904 1.1 mycroft aic->nexus->xs->error = XS_DRIVER_STUFFUP;
905 1.1 mycroft untimeout(aic_timeout, aic->nexus);
906 1.1 mycroft aic_done(aic->nexus);
907 1.1 mycroft }
908 1.1 mycroft aic->nexus = NULL;
909 1.5 cgd while (acb = aic->nexus_list.tqh_first) {
910 1.1 mycroft acb->xs->error = XS_DRIVER_STUFFUP;
911 1.1 mycroft untimeout(aic_timeout, acb);
912 1.1 mycroft aic_done(acb);
913 1.1 mycroft }
914 1.1 mycroft }
915 1.1 mycroft
916 1.1 mycroft aic->phase = aic->prevphase = PH_INVALID;
917 1.1 mycroft aic->hp = 0;
918 1.1 mycroft for (r = 0; r < 7; r++) {
919 1.1 mycroft struct aic_tinfo *tp = &aic->tinfo[r];
920 1.1 mycroft tp->flags = AIC_USE_SYNCHRONOUS ? DO_NEGOTIATE : 0;
921 1.1 mycroft tp->flags |= NEED_TO_RESET;
922 1.1 mycroft tp->persgst = AIC_SYNC_PERIOD;
923 1.1 mycroft tp->offsgst = AIC_SYNC_REQ_ACK_OFS;
924 1.1 mycroft tp->syncdata = 0;
925 1.1 mycroft }
926 1.1 mycroft aic->state = AIC_IDLE;
927 1.1 mycroft outb(DMACNTRL0, INTEN);
928 1.1 mycroft return;
929 1.1 mycroft }
930 1.1 mycroft
931 1.1 mycroft /*
933 1.1 mycroft * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
934 1.1 mycroft */
935 1.1 mycroft
936 1.1 mycroft /*
937 1.1 mycroft * Expected sequence:
938 1.1 mycroft * 1) Command inserted into ready list
939 1.1 mycroft * 2) Command selected for execution
940 1.1 mycroft * 3) Command won arbitration and has selected target device
941 1.1 mycroft * 4) Send message out (identify message, eventually also sync.negotiations)
942 1.1 mycroft * 5) Send command
943 1.1 mycroft * 5a) Receive disconnect message, disconnect.
944 1.1 mycroft * 5b) Reselected by target
945 1.1 mycroft * 5c) Receive identify message from target.
946 1.1 mycroft * 6) Send or receive data
947 1.1 mycroft * 7) Receive status
948 1.1 mycroft * 8) Receive message (command complete etc.)
949 1.1 mycroft * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
950 1.1 mycroft * Repeat 2-8 (no disconnects please...)
951 1.1 mycroft */
952 1.1 mycroft
953 1.1 mycroft /*
954 1.1 mycroft * Start a SCSI-command
955 1.1 mycroft * This function is called by the higher level SCSI-driver to queue/run
956 1.1 mycroft * SCSI-commands.
957 1.1 mycroft */
958 1.1 mycroft int
959 1.1 mycroft aic_scsi_cmd(xs)
960 1.1 mycroft struct scsi_xfer *xs;
961 1.1 mycroft {
962 1.1 mycroft struct scsi_link *sc = xs->sc_link;
963 1.1 mycroft struct aic_softc *aic = sc->adapter_softc;
964 1.1 mycroft struct acb *acb;
965 1.1 mycroft int s, flags;
966 1.1 mycroft u_short iobase = aic->iobase;
967 1.1 mycroft
968 1.1 mycroft SC_DEBUG(sc, SDEV_DB2, ("aic_scsi_cmd\n"));
969 1.1 mycroft AIC_TRACE(("aic_scsi_cmd\n"));
970 1.1 mycroft AIC_MISC(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
971 1.2 mycroft sc->target));
972 1.1 mycroft
973 1.2 mycroft flags = xs->flags;
974 1.1 mycroft
975 1.1 mycroft /* Get a aic command block */
976 1.1 mycroft if (!(flags & SCSI_NOMASK)) {
977 1.1 mycroft /* Critical region */
978 1.1 mycroft s = splbio();
979 1.1 mycroft acb = aic->free_list.tqh_first;
980 1.1 mycroft if (acb) {
981 1.1 mycroft TAILQ_REMOVE(&aic->free_list, acb, chain);
982 1.1 mycroft }
983 1.1 mycroft splx(s);
984 1.1 mycroft } else {
985 1.1 mycroft acb = aic->free_list.tqh_first;
986 1.1 mycroft if (acb) {
987 1.1 mycroft TAILQ_REMOVE(&aic->free_list, acb, chain);
988 1.1 mycroft }
989 1.1 mycroft }
990 1.1 mycroft
991 1.1 mycroft if (acb == NULL) {
992 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
993 1.1 mycroft AIC_MISC(("TRY_AGAIN_LATER"));
994 1.1 mycroft return TRY_AGAIN_LATER;
995 1.1 mycroft }
996 1.1 mycroft
997 1.1 mycroft /* Initialize acb */
998 1.1 mycroft acb->flags = ACB_ACTIVE;
999 1.1 mycroft acb->xs = xs;
1000 1.1 mycroft bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
1001 1.1 mycroft acb->clen = xs->cmdlen;
1002 1.1 mycroft acb->daddr = xs->data;
1003 1.2 mycroft acb->dleft = xs->datalen;
1004 1.1 mycroft acb->stat = 0;
1005 1.1 mycroft
1006 1.1 mycroft if (!(flags & SCSI_NOMASK))
1007 1.5 cgd s = splbio();
1008 1.1 mycroft
1009 1.1 mycroft TAILQ_INSERT_TAIL(&aic->ready_list, acb, chain);
1010 1.1 mycroft timeout(aic_timeout, acb, (xs->timeout*hz)/1000);
1011 1.1 mycroft
1012 1.2 mycroft if (aic->state == AIC_IDLE)
1013 1.1 mycroft aic_sched(aic);
1014 1.1 mycroft
1015 1.1 mycroft if (!(flags & SCSI_NOMASK)) { /* Almost done. Wait outside */
1016 1.1 mycroft splx(s);
1017 1.2 mycroft AIC_MISC(("SUCCESSFULLY_QUEUED"));
1018 1.1 mycroft return SUCCESSFULLY_QUEUED;
1019 1.1 mycroft }
1020 1.1 mycroft
1021 1.1 mycroft /* Not allowed to use interrupts, use polling instead */
1022 1.1 mycroft return aic_poll(aic, acb);
1023 1.1 mycroft }
1024 1.1 mycroft
1025 1.1 mycroft /*
1026 1.1 mycroft * Adjust transfer size in buffer structure
1027 1.1 mycroft */
1028 1.1 mycroft void
1029 1.2 mycroft aic_minphys(bp)
1030 1.1 mycroft struct buf *bp;
1031 1.2 mycroft {
1032 1.2 mycroft
1033 1.1 mycroft AIC_TRACE(("aic_minphys\n"));
1034 1.1 mycroft if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
1035 1.1 mycroft bp->b_bcount = (AIC_NSEG << PGSHIFT);
1036 1.1 mycroft }
1037 1.1 mycroft
1038 1.1 mycroft
1039 1.1 mycroft u_int
1040 1.2 mycroft aic_adapter_info(aic)
1041 1.1 mycroft struct aic_softc *aic;
1042 1.1 mycroft {
1043 1.1 mycroft
1044 1.1 mycroft AIC_TRACE(("aic_adapter_info\n"));
1045 1.1 mycroft return 2; /* One outstanding command per target */
1046 1.1 mycroft }
1047 1.1 mycroft
1048 1.1 mycroft /*
1049 1.1 mycroft * Used when interrupt driven I/O isn't allowed, e.g. during boot.
1050 1.1 mycroft */
1051 1.1 mycroft int
1052 1.1 mycroft aic_poll(aic, acb)
1053 1.1 mycroft struct aic_softc *aic;
1054 1.1 mycroft struct acb *acb;
1055 1.1 mycroft {
1056 1.1 mycroft register u_short iobase = aic->iobase;
1057 1.1 mycroft struct scsi_xfer *xs = acb->xs;
1058 1.1 mycroft int count = xs->timeout * 10;
1059 1.1 mycroft
1060 1.1 mycroft AIC_TRACE(("aic_poll\n"));
1061 1.1 mycroft while (count) {
1062 1.1 mycroft if (inb(DMASTAT) & INTSTAT)
1063 1.1 mycroft aicintr(aic);
1064 1.1 mycroft if (xs->flags & ITSDONE)
1065 1.1 mycroft break;
1066 1.1 mycroft delay(100);
1067 1.1 mycroft count--;
1068 1.1 mycroft }
1069 1.1 mycroft if (count == 0) {
1070 1.1 mycroft AIC_MISC(("aic_poll: timeout"));
1071 1.1 mycroft aic_timeout((caddr_t)acb);
1072 1.1 mycroft }
1073 1.1 mycroft if (xs->error)
1074 1.1 mycroft return HAD_ERROR;
1075 1.1 mycroft return COMPLETE;
1076 1.1 mycroft }
1077 1.1 mycroft
1078 1.1 mycroft /* LOW LEVEL SCSI UTILITIES */
1080 1.1 mycroft
1081 1.1 mycroft /* Determine the SCSI bus phase, return either a real SCSI bus phase or some
1082 1.1 mycroft * pseudo phase we use to detect certain exceptions. This one is a bit tricky.
1083 1.1 mycroft * The bits we peek at:
1084 1.1 mycroft * CDI, MSGI and DI is the 3 SCSI signals determining the bus phase.
1085 1.1 mycroft * These should be qualified by REQI high and ACKI low.
1086 1.1 mycroft * Also peek at SSTAT0[SELDO|SELDI] to detect a passing BUSFREE condition.
1087 1.1 mycroft * No longer detect SCSI RESET or PERR here. They are tested for separately
1088 1.1 mycroft * in the interrupt handler.
1089 1.1 mycroft * Note: If an exception occur at some critical time during the phase
1090 1.1 mycroft * determination we'll most likely return something wildly erronous....
1091 1.1 mycroft */
1092 1.1 mycroft static inline u_short
1093 1.1 mycroft aicphase(aic)
1094 1.1 mycroft struct aic_softc *aic;
1095 1.1 mycroft {
1096 1.1 mycroft register u_short iobase = aic->iobase;
1097 1.1 mycroft register u_char sstat0, sstat1, scsisig;
1098 1.1 mycroft
1099 1.2 mycroft sstat1 = inb(SSTAT1); /* Look for REQINIT (REQ asserted) */
1100 1.1 mycroft scsisig = inb(SCSISIGI); /* Get the SCSI bus signals */
1101 1.1 mycroft sstat0 = inb(SSTAT0); /* Get the selection valid status bits */
1102 1.1 mycroft
1103 1.2 mycroft if (!(inb(SSTAT0) & (SELDO|SELDI))) /* Selection became invalid? */
1104 1.1 mycroft return PH_BUSFREE;
1105 1.1 mycroft
1106 1.1 mycroft /* Selection is still valid */
1107 1.1 mycroft if (!(sstat1 & REQINIT)) /* REQ not asserted ? */
1108 1.1 mycroft return PH_INVALID;
1109 1.1 mycroft
1110 1.1 mycroft /* REQ is asserted, (and ACK is not) */
1111 1.1 mycroft return scsisig & PH_MASK;
1112 1.1 mycroft }
1113 1.1 mycroft
1114 1.1 mycroft
1115 1.1 mycroft /* Schedule a scsi operation. This has now been pulled out of the interrupt
1117 1.1 mycroft * handler so that we may call it from aic_scsi_cmd and aic_done. This may
1118 1.1 mycroft * save us an unecessary interrupt just to get things going. Should only be
1119 1.1 mycroft * called when state == AIC_IDLE and at bio pl.
1120 1.1 mycroft */
1121 1.1 mycroft void
1122 1.1 mycroft aic_sched(aic)
1123 1.1 mycroft register struct aic_softc *aic;
1124 1.1 mycroft {
1125 1.1 mycroft struct scsi_xfer *xs;
1126 1.1 mycroft struct scsi_link *sc;
1127 1.1 mycroft struct acb *acb;
1128 1.1 mycroft u_short iobase = aic->iobase;
1129 1.1 mycroft int t, l;
1130 1.1 mycroft u_char simode0, simode1, scsiseq;
1131 1.1 mycroft
1132 1.1 mycroft AIC_TRACE(("aic_sched\n"));
1133 1.1 mycroft simode0 = ENSELDI;
1134 1.1 mycroft simode1 = ENSCSIRST|ENSCSIPERR|ENREQINIT;
1135 1.1 mycroft scsiseq = ENRESELI;
1136 1.1 mycroft /*
1137 1.1 mycroft * Find first acb in rdy queue that is for a target/lunit
1138 1.1 mycroft * combinations that is not busy.
1139 1.2 mycroft */
1140 1.1 mycroft outb(CLRSINT1, CLRSELTIMO|CLRBUSFREE|CLRSCSIPERR);
1141 1.1 mycroft for (acb = aic->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
1142 1.1 mycroft sc = acb->xs->sc_link;
1143 1.1 mycroft t = sc->target;
1144 1.1 mycroft if (!(aic->tinfo[t].lubusy & (1 << sc->lun))) {
1145 1.1 mycroft TAILQ_REMOVE(&aic->ready_list, acb, chain);
1146 1.1 mycroft aic->nexus = acb;
1147 1.1 mycroft aic->state = AIC_SELECTING;
1148 1.1 mycroft /*
1149 1.1 mycroft * Start selection process. Always enable
1150 1.1 mycroft * reselections. Note: we don't have a nexus yet, so
1151 1.1 mycroft * cannot set aic->state = AIC_HASNEXUS.
1152 1.1 mycroft */
1153 1.1 mycroft simode0 = ENSELDI|ENSELDO;
1154 1.1 mycroft simode1 = ENSCSIRST|ENSCSIPERR|
1155 1.1 mycroft ENREQINIT|ENSELTIMO;
1156 1.1 mycroft scsiseq = ENRESELI|ENSELO|ENAUTOATNO;
1157 1.1 mycroft outb(SCSIID, AIC_SCSI_HOSTID << OID_S | t);
1158 1.1 mycroft outb(SXFRCTL1, STIMO_256ms|ENSTIMER);
1159 1.1 mycroft outb(CLRSINT0, CLRSELDO);
1160 1.1 mycroft break;
1161 1.1 mycroft } else
1162 1.1 mycroft AIC_MISC(("%d:%d busy\n", t, sc->lun));
1163 1.1 mycroft }
1164 1.1 mycroft AIC_MISC(("%sselecting\n",scsiseq&ENSELO?"":"re"));
1165 1.1 mycroft outb(SIMODE0, simode0);
1166 1.1 mycroft outb(SIMODE1, simode1);
1167 1.1 mycroft outb(SCSISEQ, scsiseq);
1168 1.1 mycroft }
1169 1.1 mycroft
1170 1.1 mycroft
1171 1.1 mycroft /*
1173 1.1 mycroft * POST PROCESSING OF SCSI_CMD (usually current)
1174 1.1 mycroft */
1175 1.1 mycroft void
1176 1.1 mycroft aic_done(acb)
1177 1.1 mycroft struct acb *acb;
1178 1.1 mycroft {
1179 1.1 mycroft struct scsi_xfer *xs = acb->xs;
1180 1.1 mycroft struct scsi_link *sc = xs->sc_link;
1181 1.1 mycroft struct aic_softc *aic = sc->adapter_softc;
1182 1.1 mycroft u_short iobase = aic->iobase;
1183 1.1 mycroft struct acb *acb2;
1184 1.1 mycroft
1185 1.1 mycroft AIC_TRACE(("aic_done "));
1186 1.1 mycroft
1187 1.1 mycroft /*
1188 1.1 mycroft * Now, if we've come here with no error code, i.e. we've kept the
1189 1.1 mycroft * initial XS_NOERROR, and the status code signals that we should
1190 1.1 mycroft * check sense, we'll need to set up a request sense cmd block and
1191 1.1 mycroft * push the command back into the ready queue *before* any other
1192 1.1 mycroft * commands for this target/lunit, else we lose the sense info.
1193 1.1 mycroft * We don't support chk sense conditions for the request sense cmd.
1194 1.1 mycroft */
1195 1.1 mycroft if (xs->error == XS_NOERROR && !(acb->flags & ACB_CHKSENSE)) {
1196 1.1 mycroft if ((acb->stat & ST_MASK)==SCSI_CHECK) {
1197 1.1 mycroft struct scsi_sense *ss = (void *)&acb->cmd;
1198 1.1 mycroft AIC_MISC(("requesting sense "));
1199 1.1 mycroft /* First, save the return values */
1200 1.1 mycroft xs->resid = acb->dleft;
1201 1.1 mycroft xs->status = acb->stat;
1202 1.1 mycroft /* Next, setup a request sense command block */
1203 1.1 mycroft bzero(ss, sizeof(*ss));
1204 1.1 mycroft ss->op_code = REQUEST_SENSE;
1205 1.1 mycroft ss->byte2 = sc->lun << 5;
1206 1.1 mycroft ss->length = sizeof(struct scsi_sense_data);
1207 1.1 mycroft acb->clen = sizeof(*ss);
1208 1.1 mycroft acb->daddr = (char *)&xs->sense;
1209 1.1 mycroft acb->dleft = sizeof(struct scsi_sense_data);
1210 1.1 mycroft acb->flags = ACB_ACTIVE|ACB_CHKSENSE;
1211 1.1 mycroft TAILQ_INSERT_HEAD(&aic->ready_list, acb, chain);
1212 1.1 mycroft aic->tinfo[sc->target].lubusy &= ~(1<<sc->lun);
1213 1.1 mycroft aic->tinfo[sc->target].senses++;
1214 1.1 mycroft if (aic->nexus == acb) {
1215 1.1 mycroft aic->nexus = NULL;
1216 1.1 mycroft aic->state = AIC_IDLE;
1217 1.1 mycroft aic_sched(aic);
1218 1.1 mycroft }
1219 1.1 mycroft return;
1220 1.1 mycroft }
1221 1.1 mycroft }
1222 1.1 mycroft
1223 1.1 mycroft if (xs->flags & SCSI_ERR_OK) {
1224 1.1 mycroft xs->resid = 0;
1225 1.1 mycroft xs->error = XS_NOERROR;
1226 1.1 mycroft } else if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE)) {
1227 1.1 mycroft xs->error = XS_SENSE;
1228 1.1 mycroft } else {
1229 1.1 mycroft xs->resid = acb->dleft;
1230 1.1 mycroft }
1231 1.1 mycroft xs->flags |= ITSDONE;
1232 1.1 mycroft
1233 1.1 mycroft #if AIC_DEBUG
1234 1.1 mycroft if (aic_debug & AIC_SHOWMISC) {
1235 1.1 mycroft printf("err=0x%02x ",xs->error);
1236 1.1 mycroft if (xs->error == XS_SENSE)
1237 1.1 mycroft printf("sense=%2x\n", xs->sense.error_code);
1238 1.1 mycroft }
1239 1.1 mycroft if ((xs->resid || xs->error > XS_SENSE) && aic_debug & AIC_SHOWMISC) {
1240 1.1 mycroft if (xs->resid)
1241 1.1 mycroft printf("aic_done: resid=%d\n", xs->resid);
1242 1.1 mycroft if (xs->error)
1243 1.1 mycroft printf("aic_done: error=%d\n", xs->error);
1244 1.1 mycroft }
1245 1.1 mycroft #endif
1246 1.1 mycroft
1247 1.1 mycroft /*
1248 1.1 mycroft * Remove the ACB from whatever queue it's on. We have to do a bit of
1249 1.1 mycroft * a hack to figure out which queue it's on. Note that it is *not*
1250 1.1 mycroft * necessary to cdr down the ready queue, but we must cdr down the
1251 1.1 mycroft * nexus queue and see if it's there, so we can mark the unit as no
1252 1.1 mycroft * longer busy. This code is sickening, but it works.
1253 1.1 mycroft */
1254 1.1 mycroft if (acb == aic->nexus) {
1255 1.1 mycroft aic->state = AIC_IDLE;
1256 1.1 mycroft aic->tinfo[sc->target].lubusy &= ~(1<<sc->lun);
1257 1.1 mycroft aic_sched(aic);
1258 1.1 mycroft } else if (aic->ready_list.tqh_last == &acb->chain.tqe_next) {
1259 1.1 mycroft TAILQ_REMOVE(&aic->ready_list, acb, chain);
1260 1.1 mycroft } else {
1261 1.1 mycroft register struct acb *acb2;
1262 1.1 mycroft for (acb2 = aic->nexus_list.tqh_first; acb2;
1263 1.1 mycroft acb2 = acb2->chain.tqe_next)
1264 1.1 mycroft if (acb2 == acb) {
1265 1.1 mycroft TAILQ_REMOVE(&aic->nexus_list, acb, chain);
1266 1.1 mycroft aic->tinfo[sc->target].lubusy &= ~(1<<sc->lun);
1267 1.1 mycroft /* XXXX Should we call aic_sched() here? */
1268 1.1 mycroft break;
1269 1.1 mycroft }
1270 1.1 mycroft if (acb2)
1271 1.1 mycroft ;
1272 1.1 mycroft else if (acb->chain.tqe_next) {
1273 1.1 mycroft TAILQ_REMOVE(&aic->ready_list, acb, chain);
1274 1.1 mycroft } else {
1275 1.1 mycroft printf("%s: can't find matching acb\n",
1276 1.1 mycroft aic->sc_dev.dv_xname);
1277 1.1 mycroft Debugger();
1278 1.1 mycroft }
1279 1.1 mycroft }
1280 1.1 mycroft /* Put it on the free list. */
1281 1.1 mycroft acb->flags = ACB_FREE;
1282 1.1 mycroft TAILQ_INSERT_HEAD(&aic->free_list, acb, chain);
1283 1.1 mycroft
1284 1.1 mycroft aic->tinfo[sc->target].cmds++;
1285 1.1 mycroft scsi_done(xs);
1286 1.1 mycroft return;
1287 1.1 mycroft }
1288 1.1 mycroft
1289 1.1 mycroft /*
1291 1.1 mycroft * INTERRUPT/PROTOCOL ENGINE
1292 1.1 mycroft */
1293 1.1 mycroft
1294 1.1 mycroft /* The message system:
1295 1.1 mycroft * This is a revamped message system that now should easier accomodate new
1296 1.1 mycroft * messages, if necessary.
1297 1.1 mycroft * Currently we accept these messages:
1298 1.1 mycroft * IDENTIFY (when reselecting)
1299 1.1 mycroft * COMMAND COMPLETE # (expect bus free after messages marked #)
1300 1.1 mycroft * NOOP
1301 1.1 mycroft * MESSAGE REJECT
1302 1.1 mycroft * SYNCHRONOUS DATA TRANSFER REQUEST
1303 1.1 mycroft * SAVE DATA POINTER
1304 1.1 mycroft * RESTORE POINTERS
1305 1.1 mycroft * DISCONNECT #
1306 1.1 mycroft *
1307 1.1 mycroft * We may send these messages in prioritized order:
1308 1.1 mycroft * BUS DEVICE RESET # if SCSI_RESET & xs->flags (or in weird sits.)
1309 1.1 mycroft * MESSAGE PARITY ERROR par. err. during MSGI
1310 1.1 mycroft * MESSAGE REJECT If we get a message we don't know how to handle
1311 1.2 mycroft * ABORT # send on errors
1312 1.2 mycroft * INITIATOR DETECTED ERROR also on errors (SCSI2) (during info xfer)
1313 1.2 mycroft * IDENTIFY At the start of each transfer
1314 1.2 mycroft * SYNCHRONOUS DATA TRANSFER REQUEST if appropriate
1315 1.2 mycroft * NOOP if nothing else fits the bill ...
1316 1.1 mycroft */
1317 1.1 mycroft
1318 1.1 mycroft #define aic_sched_msgout(m) \
1319 1.1 mycroft do { \
1320 1.1 mycroft orreg(SCSISIGO, ATNO); \
1321 1.1 mycroft aic->msgpriq |= (m); \
1322 1.1 mycroft } while (0)
1323 1.1 mycroft
1324 1.1 mycroft #define IS1BYTEMSG(m) (((m) != 1 && (m) < 0x20) || (m) >= 0x80)
1325 1.1 mycroft #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1326 1.1 mycroft #define ISEXTMSG(m) ((m) == 1)
1327 1.1 mycroft /* Precondition:
1328 1.1 mycroft * The SCSI bus is already in the MSGI phase and there is a message byte
1329 1.1 mycroft * on the bus, along with an asserted REQ signal.
1330 1.1 mycroft */
1331 1.1 mycroft static void
1332 1.1 mycroft aic_msgin(aic)
1333 1.1 mycroft register struct aic_softc *aic;
1334 1.1 mycroft {
1335 1.1 mycroft register u_short iobase = aic->iobase;
1336 1.1 mycroft int spincount, extlen;
1337 1.1 mycroft u_char sstat1;
1338 1.1 mycroft
1339 1.1 mycroft AIC_TRACE(("aic_msgin "));
1340 1.1 mycroft outb(SCSISIGO, PH_MSGI);
1341 1.1 mycroft /* Prepare for a new message. A message should (according to the SCSI
1342 1.1 mycroft * standard) be transmitted in one single message_in phase.
1343 1.1 mycroft * If we have been in some other phase, then this is a new message.
1344 1.1 mycroft */
1345 1.1 mycroft if (aic->prevphase != PH_MSGI) {
1346 1.1 mycroft aic->flags &= ~AIC_DROP_MSGI;
1347 1.2 mycroft aic->imlen = 0;
1348 1.1 mycroft }
1349 1.1 mycroft /*
1350 1.1 mycroft * Read a whole message but the last byte. If we shall reject the
1351 1.1 mycroft * message, we shall have to do it, by asserting ATNO, during the
1352 1.1 mycroft * message transfer phase itself.
1353 1.1 mycroft */
1354 1.1 mycroft for (;;) {
1355 1.1 mycroft sstat1 = inb(SSTAT1);
1356 1.1 mycroft /* If parity errors just dump everything on the floor, also
1357 1.1 mycroft * a parity error automatically sets ATNO
1358 1.1 mycroft */
1359 1.1 mycroft if (sstat1 & SCSIPERR) {
1360 1.1 mycroft aic_sched_msgout(SEND_PARITY_ERROR);
1361 1.1 mycroft aic->flags |= AIC_DROP_MSGI;
1362 1.1 mycroft }
1363 1.1 mycroft /*
1364 1.1 mycroft * If we're going to reject the message, don't bother storing
1365 1.1 mycroft * the incoming bytes. But still, we need to ACK them.
1366 1.1 mycroft */
1367 1.1 mycroft if (!(aic->flags & AIC_DROP_MSGI)) {
1368 1.1 mycroft /* Get next message byte */
1369 1.1 mycroft aic->imess[aic->imlen] = inb(SCSIDAT);
1370 1.1 mycroft /*
1371 1.1 mycroft * This testing is suboptimal, but most messages will
1372 1.1 mycroft * be of the one byte variety, so it should not effect
1373 1.1 mycroft * performance significantly.
1374 1.1 mycroft */
1375 1.1 mycroft if (IS1BYTEMSG(aic->imess[0]))
1376 1.1 mycroft break;
1377 1.1 mycroft if (IS2BYTEMSG(aic->imess[0]) && aic->imlen == 1)
1378 1.1 mycroft break;
1379 1.1 mycroft if (ISEXTMSG(aic->imess[0]) && aic->imlen > 0) {
1380 1.1 mycroft if (aic->imlen == AIC_MAX_MSG_LEN) {
1381 1.1 mycroft aic->flags |= AIC_DROP_MSGI;
1382 1.1 mycroft aic_sched_msgout(SEND_REJECT);
1383 1.1 mycroft }
1384 1.1 mycroft extlen = aic->imess[1] ? aic->imess[1] : 256;
1385 1.1 mycroft if (aic->imlen == extlen + 2)
1386 1.1 mycroft break; /* Got it all */
1387 1.1 mycroft }
1388 1.1 mycroft }
1389 1.1 mycroft /* If we reach this spot we're either:
1390 1.1 mycroft * a) in the middle of a multi-byte message or
1391 1.1 mycroft * b) we're dropping bytes
1392 1.1 mycroft */
1393 1.1 mycroft outb(SXFRCTL0, CHEN|SPIOEN);
1394 1.1 mycroft inb(SCSIDAT); /* Really read it (ACK it, that is) */
1395 1.1 mycroft outb(SXFRCTL0, CHEN);
1396 1.1 mycroft aic->imlen++;
1397 1.1 mycroft
1398 1.1 mycroft /*
1399 1.1 mycroft * We expect the bytes in a multibyte message to arrive
1400 1.1 mycroft * relatively close in time, a few microseconds apart.
1401 1.1 mycroft * Therefore we will spinwait for some small amount of time
1402 1.1 mycroft * waiting for the next byte.
1403 1.1 mycroft */
1404 1.1 mycroft spincount = MINIFUDGE(delaycount) * AIC_MSGI_SPIN;
1405 1.1 mycroft LOGLINE(aic);
1406 1.1 mycroft while (spincount-- && !((sstat1 = inb(SSTAT1)) & REQINIT))
1407 1.1 mycroft ;
1408 1.1 mycroft if (spincount == -1 || sstat1 & (PHASEMIS|BUSFREE))
1409 1.1 mycroft return;
1410 1.1 mycroft }
1411 1.1 mycroft /* Now we should have a complete message (1 byte, 2 byte and moderately
1412 1.1 mycroft * long extended messages). We only handle extended messages which
1413 1.1 mycroft * total length is shorter than AIC_MAX_MSG_LEN. Longer messages will
1414 1.1 mycroft * be amputated. (Return XS_BOBBITT ?)
1415 1.1 mycroft */
1416 1.1 mycroft if (aic->state == AIC_HASNEXUS) {
1417 1.1 mycroft struct acb *acb = aic->nexus;
1418 1.4 mycroft struct aic_tinfo *ti = &aic->tinfo[acb->xs->sc_link->target];
1419 1.1 mycroft int offs, per, rate;
1420 1.1 mycroft
1421 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE|ENSCSIPERR);
1422 1.1 mycroft switch (aic->imess[0]) {
1423 1.1 mycroft case MSG_CMDCOMPLETE:
1424 1.1 mycroft if (!acb) {
1425 1.2 mycroft aic_sched_msgout(SEND_ABORT);
1426 1.1 mycroft printf("aic: CMDCOMPLETE but no command?\n");
1427 1.1 mycroft break;
1428 1.1 mycroft }
1429 1.1 mycroft if (aic->dleft < 0) {
1430 1.5 cgd struct scsi_link *sc = acb->xs->sc_link;
1431 1.1 mycroft printf("aic: %d extra bytes from %d:%d\n",
1432 1.1 mycroft -aic->dleft, sc->target, sc->lun);
1433 1.1 mycroft acb->dleft = 0;
1434 1.1 mycroft }
1435 1.1 mycroft acb->xs->resid = acb->dleft = aic->dleft;
1436 1.1 mycroft aic->flags |= AIC_BUSFREE_OK;
1437 1.1 mycroft untimeout(aic_timeout, acb);
1438 1.1 mycroft aic_done(acb);
1439 1.1 mycroft break;
1440 1.1 mycroft case MSG_MESSAGE_REJECT:
1441 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
1442 1.1 mycroft printf("aic: our msg rejected by target\n");
1443 1.1 mycroft if (aic->flags & AIC_SYNCHNEGO) {
1444 1.1 mycroft ti->syncdata = 0;
1445 1.1 mycroft ti->persgst = ti->offsgst = 0;
1446 1.1 mycroft aic->flags &= ~AIC_SYNCHNEGO;
1447 1.1 mycroft ti->flags &= ~DO_NEGOTIATE;
1448 1.1 mycroft }
1449 1.1 mycroft /* Not all targets understand INITIATOR_DETECTED_ERR */
1450 1.4 mycroft if (aic->msgout == SEND_INIT_DET_ERR)
1451 1.1 mycroft aic_sched_msgout(SEND_ABORT);
1452 1.1 mycroft break;
1453 1.1 mycroft case MSG_NOOP: /* Will do! Immediately, sir!*/
1454 1.1 mycroft break; /* Hah, that was easy! */
1455 1.1 mycroft case MSG_DISCONNECT:
1456 1.1 mycroft if (!acb) {
1457 1.1 mycroft aic_sched_msgout(SEND_ABORT);
1458 1.1 mycroft printf("aic: nothing to DISCONNECT\n");
1459 1.1 mycroft break;
1460 1.1 mycroft }
1461 1.1 mycroft ti->dconns++;
1462 1.4 mycroft TAILQ_INSERT_HEAD(&aic->nexus_list, acb, chain);
1463 1.1 mycroft acb = aic->nexus = NULL;
1464 1.1 mycroft aic->state = AIC_IDLE;
1465 1.1 mycroft aic->flags |= AIC_BUSFREE_OK;
1466 1.1 mycroft break;
1467 1.1 mycroft case MSG_SAVEDATAPOINTER:
1468 1.1 mycroft if (!acb) {
1469 1.1 mycroft aic_sched_msgout(SEND_ABORT);
1470 1.1 mycroft printf("aic: no DATAPOINTERs to save\n");
1471 1.4 mycroft break;
1472 1.1 mycroft }
1473 1.1 mycroft acb->dleft = aic->dleft;
1474 1.1 mycroft acb->daddr = aic->dp;
1475 1.1 mycroft break;
1476 1.1 mycroft case MSG_RESTOREPOINTERS:
1477 1.1 mycroft if (!acb) {
1478 1.1 mycroft aic_sched_msgout(SEND_ABORT);
1479 1.1 mycroft printf("aic: no DATAPOINTERs to restore\n");
1480 1.1 mycroft break;
1481 1.1 mycroft }
1482 1.1 mycroft aic->dp = acb->daddr;
1483 1.1 mycroft aic->dleft = acb->dleft;
1484 1.1 mycroft break;
1485 1.1 mycroft case MSG_EXTENDED:
1486 1.1 mycroft switch (aic->imess[2]) {
1487 1.1 mycroft case MSG_EXT_SDTR:
1488 1.1 mycroft per = aic->imess[3] * 4;
1489 1.1 mycroft rate = (per + 49 - 100)/50;
1490 1.1 mycroft offs = aic->imess[4];
1491 1.1 mycroft if (offs == 0)
1492 1.1 mycroft ti->syncdata = 0;
1493 1.2 mycroft else if (rate > 7) {
1494 1.1 mycroft /* Too slow for aic6360. Do asynch
1495 1.1 mycroft * instead. Renegotiate the deal.
1496 1.1 mycroft */
1497 1.1 mycroft ti->persgst = 0;
1498 1.1 mycroft ti->offsgst = 0;
1499 1.4 mycroft aic_sched_msgout(SEND_SDTR);
1500 1.1 mycroft } else {
1501 1.1 mycroft rate = rate<<4 | offs;
1502 1.1 mycroft ti->syncdata = rate;
1503 1.1 mycroft }
1504 1.4 mycroft break;
1505 1.1 mycroft default: /* Extended messages we don't handle */
1506 1.1 mycroft aic_sched_msgout(SEND_REJECT);
1507 1.4 mycroft break;
1508 1.1 mycroft }
1509 1.1 mycroft break;
1510 1.1 mycroft default:
1511 1.1 mycroft aic_sched_msgout(SEND_REJECT);
1512 1.1 mycroft break;
1513 1.1 mycroft }
1514 1.1 mycroft } else if (aic->state == AIC_RESELECTED) {
1515 1.1 mycroft struct scsi_link *sc;
1516 1.1 mycroft struct acb *acb;
1517 1.1 mycroft u_char selid, lunit;
1518 1.1 mycroft /*
1519 1.1 mycroft * Which target is reselecting us? (The ID bit really)
1520 1.1 mycroft */
1521 1.1 mycroft selid = inb(SELID) & ~(1<<AIC_SCSI_HOSTID);
1522 1.1 mycroft if (MSG_ISIDENT(aic->imess[0])) { /* Identify? */
1523 1.1 mycroft AIC_MISC(("searching "));
1524 1.1 mycroft /* Search wait queue for disconnected cmd
1525 1.1 mycroft * The list should be short, so I haven't bothered with
1526 1.1 mycroft * any more sophisticated structures than a simple
1527 1.1 mycroft * singly linked list.
1528 1.1 mycroft */
1529 1.1 mycroft lunit = aic->imess[0] & 0x07;
1530 1.1 mycroft for (acb = aic->nexus_list.tqh_first; acb;
1531 1.1 mycroft acb = acb->chain.tqe_next) {
1532 1.1 mycroft sc = acb->xs->sc_link;
1533 1.1 mycroft if (sc->lun == lunit &&
1534 1.1 mycroft selid == (1<<sc->target)) {
1535 1.1 mycroft TAILQ_REMOVE(&aic->nexus_list, acb,
1536 1.2 mycroft chain);
1537 1.2 mycroft break;
1538 1.1 mycroft }
1539 1.1 mycroft }
1540 1.1 mycroft if (!acb) { /* Invalid reselection! */
1541 1.1 mycroft aic_sched_msgout(SEND_ABORT);
1542 1.1 mycroft printf("aic: invalid reselect (idbit=0x%2x)\n",
1543 1.1 mycroft selid);
1544 1.1 mycroft } else { /* Reestablish nexus */
1545 1.1 mycroft /* Setup driver data structures and
1546 1.1 mycroft * do an implicit RESTORE POINTERS
1547 1.1 mycroft */
1548 1.1 mycroft aic->nexus = acb;
1549 1.2 mycroft aic->dp = acb->daddr;
1550 1.1 mycroft aic->dleft = acb->dleft;
1551 1.2 mycroft aic->tinfo[sc->target].lubusy |= (1<<sc->lun);
1552 1.1 mycroft outb(SCSIRATE,aic->tinfo[sc->target].syncdata);
1553 1.1 mycroft AIC_MISC(("... found acb"));
1554 1.2 mycroft aic->state = AIC_HASNEXUS;
1555 1.2 mycroft }
1556 1.1 mycroft } else {
1557 1.1 mycroft printf("aic: bogus reselect (no IDENTIFY) %0x2x\n",
1558 1.4 mycroft selid);
1559 1.4 mycroft aic_sched_msgout(SEND_DEV_RESET);
1560 1.4 mycroft }
1561 1.4 mycroft } else { /* Neither AIC_HASNEXUS nor AIC_RESELECTED! */
1562 1.4 mycroft printf("aic: unexpected message in; will send DEV_RESET\n");
1563 1.1 mycroft aic_sched_msgout(SEND_DEV_RESET);
1564 1.1 mycroft }
1565 1.1 mycroft /* Must not forget to ACK the last message byte ... */
1566 1.1 mycroft outb(SXFRCTL0, CHEN|SPIOEN);
1567 1.1 mycroft inb(SCSIDAT);
1568 1.1 mycroft outb(SXFRCTL0, CHEN);
1569 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
1570 1.1 mycroft }
1571 1.1 mycroft
1572 1.1 mycroft
1573 1.1 mycroft /* The message out (and in) stuff is a bit complicated:
1574 1.1 mycroft * If the target requests another message (sequence) without
1575 1.1 mycroft * having changed phase in between it really asks for a
1576 1.1 mycroft * retransmit, probably due to parity error(s).
1577 1.1 mycroft * The following messages can be sent:
1578 1.1 mycroft * IDENTIFY @ These 3 stems from scsi command activity
1579 1.1 mycroft * BUS_DEV_RESET @
1580 1.1 mycroft * IDENTIFY + SDTR @
1581 1.1 mycroft * MESSAGE_REJECT if MSGI doesn't make sense
1582 1.1 mycroft * MESSAGE_PARITY_ERROR if MSGI spots a parity error
1583 1.1 mycroft * NOOP if asked for a message and there's nothing to send
1584 1.1 mycroft */
1585 1.1 mycroft static void
1586 1.1 mycroft aic_msgout(aic)
1587 1.1 mycroft register struct aic_softc *aic;
1588 1.1 mycroft {
1589 1.1 mycroft register u_short iobase = aic->iobase;
1590 1.1 mycroft struct aic_tinfo *ti;
1591 1.1 mycroft struct acb *acb;
1592 1.1 mycroft u_char dmastat, scsisig;
1593 1.1 mycroft
1594 1.1 mycroft /* First determine what to send. If we haven't seen a
1595 1.1 mycroft * phasechange this is a retransmission request.
1596 1.1 mycroft */
1597 1.1 mycroft outb(SCSISIGO, PH_MSGO);
1598 1.1 mycroft if (aic->prevphase != PH_MSGO) { /* NOT a retransmit */
1599 1.1 mycroft /* Pick up highest priority message */
1600 1.1 mycroft aic->msgout = aic->msgpriq & -aic->msgpriq; /* What message? */
1601 1.1 mycroft aic->omlen = 1; /* "Default" message len */
1602 1.1 mycroft switch (aic->msgout) {
1603 1.1 mycroft case SEND_SDTR: /* Also implies an IDENTIFY message */
1604 1.1 mycroft acb = aic->nexus;
1605 1.1 mycroft ti = &aic->tinfo[acb->xs->sc_link->target];
1606 1.1 mycroft aic->omess[1] = MSG_EXTENDED;
1607 1.1 mycroft aic->omess[2] = 3;
1608 1.2 mycroft aic->omess[3] = MSG_EXT_SDTR;
1609 1.1 mycroft aic->omess[4] = ti->persgst >> 2;
1610 1.1 mycroft aic->omess[5] = ti->offsgst;
1611 1.1 mycroft aic->omlen = 6;
1612 1.1 mycroft /* Fallthrough! */
1613 1.1 mycroft case SEND_IDENTIFY:
1614 1.1 mycroft if (aic->state != AIC_HASNEXUS) {
1615 1.1 mycroft printf("aic at line %d: no nexus", __LINE__);
1616 1.1 mycroft Debugger();
1617 1.1 mycroft }
1618 1.1 mycroft acb = aic->nexus;
1619 1.1 mycroft aic->omess[0] = MSG_IDENTIFY(acb->xs->sc_link->lun);
1620 1.1 mycroft break;
1621 1.1 mycroft case SEND_DEV_RESET:
1622 1.1 mycroft aic->omess[0] = MSG_BUS_DEV_RESET;
1623 1.1 mycroft aic->flags |= AIC_BUSFREE_OK;
1624 1.1 mycroft break;
1625 1.1 mycroft case SEND_PARITY_ERROR:
1626 1.1 mycroft aic->omess[0] = MSG_PARITY_ERR;
1627 1.1 mycroft break;
1628 1.1 mycroft case SEND_ABORT:
1629 1.1 mycroft aic->omess[0] = MSG_ABORT;
1630 1.1 mycroft aic->flags |= AIC_BUSFREE_OK;
1631 1.1 mycroft break;
1632 1.1 mycroft case SEND_INIT_DET_ERR:
1633 1.1 mycroft aic->omess[0] = MSG_INITIATOR_DET_ERR;
1634 1.1 mycroft break;
1635 1.1 mycroft case SEND_REJECT:
1636 1.4 mycroft aic->omess[0] = MSG_MESSAGE_REJECT;
1637 1.1 mycroft break;
1638 1.1 mycroft default:
1639 1.1 mycroft aic->omess[0] = MSG_NOOP;
1640 1.1 mycroft break;
1641 1.1 mycroft }
1642 1.1 mycroft aic->omp = aic->omess;
1643 1.1 mycroft } else if (aic->omp == &aic->omess[aic->omlen]) {
1644 1.1 mycroft /* Have sent the message at least once, this is a retransmit.
1645 1.1 mycroft */
1646 1.1 mycroft AIC_MISC(("retransmitting "));
1647 1.1 mycroft if (aic->omlen > 1)
1648 1.1 mycroft outb(SCSISIGO, PH_MSGO|ATNO);
1649 1.1 mycroft }
1650 1.1 mycroft /* else, we're in the middle of a multi-byte message */
1651 1.1 mycroft outb(SXFRCTL0, CHEN|SPIOEN);
1652 1.1 mycroft outb(DMACNTRL0, INTEN|RSTFIFO);
1653 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
1654 1.1 mycroft do {
1655 1.1 mycroft LOGLINE(aic);
1656 1.1 mycroft do {
1657 1.1 mycroft aic->phase = aicphase(aic);
1658 1.1 mycroft } while (aic->phase == PH_INVALID);
1659 1.1 mycroft if (aic->phase != PH_MSGO)
1660 1.1 mycroft /* Target left MSGO, possibly to reject our
1661 1.1 mycroft * message
1662 1.2 mycroft */
1663 1.1 mycroft break;
1664 1.1 mycroft /* Clear ATN before last byte */
1665 1.1 mycroft if (aic->omp == &aic->omess[aic->omlen-1])
1666 1.1 mycroft outb(CLRSINT1, CLRATNO);
1667 1.1 mycroft outb(SCSIDAT, *aic->omp++); /* Send MSG */
1668 1.1 mycroft LOGLINE(aic);
1669 1.1 mycroft while (inb(SCSISIGI) & ACKO)
1670 1.1 mycroft ;
1671 1.1 mycroft } while (aic->omp != &aic->omess[aic->omlen]);
1672 1.1 mycroft aic->progress = aic->omp != aic->omess;
1673 1.1 mycroft /* We get here in two ways:
1674 1.1 mycroft * a) phase != MSGO. Target is probably going to reject our message
1675 1.1 mycroft * b) aic->omp == &aic->omess[aic->omlen], i.e. the message has been
1676 1.1 mycroft * transmitted correctly and accepted by the target.
1677 1.1 mycroft */
1678 1.1 mycroft if (aic->phase == PH_MSGO) { /* Message accepted by target! */
1679 1.1 mycroft aic->msgpriq &= ~aic->msgout;
1680 1.1 mycroft aic->msgout = 0;
1681 1.1 mycroft }
1682 1.1 mycroft outb(SXFRCTL0, CHEN); /* Disable SPIO */
1683 1.1 mycroft outb(SIMODE0, 0); /* Setup interrupts before leaving */
1684 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
1685 1.1 mycroft /* Enabled ints: SCSIPERR, SCSIRSTI (unexpected)
1686 1.1 mycroft * REQINIT (expected) BUSFREE (possibly expected)
1687 1.1 mycroft */
1688 1.1 mycroft }
1689 1.1 mycroft
1690 1.1 mycroft /* aic_dataout: perform a data transfer using the FIFO datapath in the aic6360
1691 1.1 mycroft * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
1692 1.1 mycroft * and ACK deasserted (i.e. waiting for a data byte)
1693 1.1 mycroft * This new revision has been optimized (I tried) to make the common case fast,
1694 1.1 mycroft * and the rarer cases (as a result) somewhat more comlex
1695 1.1 mycroft */
1696 1.1 mycroft void
1697 1.6 mycroft aic_dataout(aic)
1698 1.1 mycroft register struct aic_softc *aic;
1699 1.6 mycroft {
1700 1.1 mycroft register u_short iobase = aic->iobase;
1701 1.1 mycroft register u_char dmastat;
1702 1.6 mycroft struct acb *acb = aic->nexus;
1703 1.6 mycroft int amount, olddleft = aic->dleft;
1704 1.1 mycroft #define DOUTAMOUNT 128 /* Full FIFO */
1705 1.6 mycroft
1706 1.1 mycroft /* Enable DATA OUT transfers */
1707 1.1 mycroft outb(SCSISIGO, PH_DOUT);
1708 1.1 mycroft outb(CLRSINT1, CLRPHASECHG);
1709 1.1 mycroft /* Clear FIFOs and counters */
1710 1.1 mycroft outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH);
1711 1.1 mycroft outb(DMACNTRL0, WRITE|INTEN|RSTFIFO);
1712 1.1 mycroft /* Enable FIFOs */
1713 1.1 mycroft outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
1714 1.1 mycroft outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE|INTEN);
1715 1.1 mycroft
1716 1.1 mycroft /* Setup to detect:
1717 1.1 mycroft * PHASEMIS & PHASECHG: target has left the DOUT phase
1718 1.1 mycroft * SCSIRST: something just pulled the RST line.
1719 1.1 mycroft * BUSFREE: target has unexpectedly left the DOUT phase
1720 1.6 mycroft */
1721 1.6 mycroft outb(SIMODE1, ENPHASEMIS|ENSCSIRST|ENBUSFREE|ENPHASECHG);
1722 1.6 mycroft
1723 1.1 mycroft /* I have tried to make the main loop as tight as possible. This
1724 1.6 mycroft * means that some of the code following the loop is a bit more
1725 1.6 mycroft * complex than otherwise.
1726 1.1 mycroft */
1727 1.6 mycroft while (aic->dleft) {
1728 1.6 mycroft int xfer;
1729 1.6 mycroft
1730 1.6 mycroft LOGLINE(aic);
1731 1.6 mycroft
1732 1.6 mycroft for (;;) {
1733 1.6 mycroft dmastat = inb(DMASTAT);
1734 1.1 mycroft if (dmastat & DFIFOEMP)
1735 1.1 mycroft break;
1736 1.6 mycroft if (dmastat & INTSTAT)
1737 1.6 mycroft goto phasechange;
1738 1.6 mycroft }
1739 1.6 mycroft
1740 1.6 mycroft xfer = min(DOUTAMOUNT, aic->dleft);
1741 1.6 mycroft
1742 1.1 mycroft #if AIC_USE_DWORDS
1743 1.6 mycroft if (xfer >= 12) {
1744 1.6 mycroft outsl(DMADATALONG, aic->dp, xfer/4);
1745 1.6 mycroft aic->dleft -= xfer & ~3;
1746 1.6 mycroft aic->dp += xfer & ~3;
1747 1.6 mycroft xfer &= 3;
1748 1.6 mycroft }
1749 1.1 mycroft #else
1750 1.1 mycroft if (xfer >= 8) {
1751 1.6 mycroft outsw(DMADATA, aic->dp, xfer/2);
1752 1.6 mycroft aic->dleft -= xfer & ~1;
1753 1.6 mycroft aic->dp += xfer & ~1;
1754 1.6 mycroft xfer &= 1;
1755 1.6 mycroft }
1756 1.6 mycroft #endif
1757 1.6 mycroft
1758 1.1 mycroft if (xfer) {
1759 1.6 mycroft outb(DMACNTRL0, ENDMA|B8MODE|INTEN);
1760 1.1 mycroft outsb(DMADATA, aic->dp, xfer);
1761 1.6 mycroft aic->dleft -= xfer;
1762 1.1 mycroft aic->dp += xfer;
1763 1.6 mycroft outb(DMACNTRL0, ENDMA|DWORDPIO|INTEN);
1764 1.6 mycroft }
1765 1.6 mycroft }
1766 1.6 mycroft
1767 1.6 mycroft /* See the bytes off chip */
1768 1.1 mycroft for (;;) {
1769 1.6 mycroft dmastat = inb(DMASTAT);
1770 1.6 mycroft if ((dmastat & DFIFOEMP) && (inb(SSTAT2) & SEMPTY))
1771 1.1 mycroft break;
1772 1.6 mycroft if (dmastat & INTSTAT)
1773 1.1 mycroft goto phasechange;
1774 1.1 mycroft }
1775 1.1 mycroft
1776 1.1 mycroft phasechange:
1777 1.1 mycroft /* We now have the data off chip. */
1778 1.1 mycroft outb(SXFRCTL0, CHEN);
1779 1.1 mycroft
1780 1.1 mycroft if (dmastat & INTSTAT) { /* Some sort of phasechange */
1781 1.1 mycroft register u_char sstat2;
1782 1.6 mycroft /* Stop transfers, do some accounting */
1783 1.1 mycroft amount = inb(FIFOSTAT);
1784 1.1 mycroft sstat2 = inb(SSTAT2);
1785 1.1 mycroft if ((sstat2 & 7) == 0)
1786 1.1 mycroft amount += sstat2 & SFULL ? 8 : 0;
1787 1.1 mycroft else
1788 1.1 mycroft amount += sstat2 & 7;
1789 1.1 mycroft aic->dleft += amount;
1790 1.1 mycroft aic->dp -= amount;
1791 1.1 mycroft AIC_MISC(("+%d ", amount));
1792 1.1 mycroft }
1793 1.1 mycroft
1794 1.1 mycroft outb(DMACNTRL0, RSTFIFO|INTEN);
1795 1.1 mycroft LOGLINE(aic);
1796 1.1 mycroft while (inb(SXFRCTL0) & SCSIEN)
1797 1.1 mycroft ;
1798 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
1799 1.1 mycroft /* Enabled ints: BUSFREE, SCSIPERR, SCSIRSTI (unexpected)
1800 1.1 mycroft * REQINIT (expected)
1801 1.1 mycroft */
1802 1.1 mycroft aic->progress = olddleft != aic->dleft;
1803 1.1 mycroft return;
1804 1.1 mycroft }
1805 1.1 mycroft
1806 1.1 mycroft /* aic_datain: perform data transfers using the FIFO datapath in the aic6360
1807 1.1 mycroft * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
1808 1.1 mycroft * and ACK deasserted (i.e. at least one byte is ready).
1809 1.1 mycroft * For now, uses a pretty dumb algorithm, hangs around until all data has been
1810 1.1 mycroft * transferred. This, is OK for fast targets, but not so smart for slow
1811 1.1 mycroft * targets which don't disconnect or for huge transfers.
1812 1.1 mycroft */
1813 1.1 mycroft void
1814 1.6 mycroft aic_datain(aic)
1815 1.1 mycroft register struct aic_softc *aic;
1816 1.1 mycroft {
1817 1.1 mycroft register u_short iobase = aic->iobase;
1818 1.1 mycroft register u_char dmastat;
1819 1.1 mycroft struct acb *acb = aic->nexus;
1820 1.1 mycroft int amount, olddleft = aic->dleft;
1821 1.1 mycroft #define DINAMOUNT 128 /* Default amount of data to transfer */
1822 1.1 mycroft
1823 1.1 mycroft /* Enable DATA IN transfers */
1824 1.1 mycroft outb(SCSISIGO, PH_DIN);
1825 1.1 mycroft outb(CLRSINT1, CLRPHASECHG);
1826 1.1 mycroft /* Clear FIFOs and counters */
1827 1.6 mycroft outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH);
1828 1.1 mycroft outb(DMACNTRL0, INTEN|RSTFIFO);
1829 1.1 mycroft /* Enable FIFOs */
1830 1.1 mycroft outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
1831 1.1 mycroft outb(DMACNTRL0, ENDMA|DWORDPIO|INTEN);
1832 1.6 mycroft
1833 1.6 mycroft outb(SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE|ENPHASECHG);
1834 1.6 mycroft
1835 1.6 mycroft /* We leave this loop if one or more of the following is true:
1836 1.1 mycroft * a) phase != PH_DIN && FIFOs are empty
1837 1.6 mycroft * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
1838 1.6 mycroft */
1839 1.6 mycroft while (aic->dleft) {
1840 1.1 mycroft int done = 0;
1841 1.6 mycroft int xfer;
1842 1.6 mycroft
1843 1.6 mycroft LOGLINE(aic);
1844 1.6 mycroft
1845 1.6 mycroft /* Wait for fifo half full or phase mismatch */
1846 1.6 mycroft for (;;) {
1847 1.6 mycroft dmastat = inb(DMASTAT);
1848 1.6 mycroft if (dmastat & (DFIFOFULL|INTSTAT))
1849 1.6 mycroft break;
1850 1.6 mycroft }
1851 1.6 mycroft
1852 1.6 mycroft if (dmastat & DFIFOFULL)
1853 1.6 mycroft xfer = DINAMOUNT;
1854 1.6 mycroft else {
1855 1.1 mycroft while ((inb(SSTAT2) & SEMPTY) == 0)
1856 1.1 mycroft ;
1857 1.6 mycroft xfer = inb(FIFOSTAT);
1858 1.6 mycroft done = 1;
1859 1.6 mycroft }
1860 1.6 mycroft
1861 1.6 mycroft xfer = min(xfer, aic->dleft);
1862 1.6 mycroft
1863 1.1 mycroft #if AIC_USE_DWORDS
1864 1.6 mycroft if (xfer >= 12) {
1865 1.6 mycroft insl(DMADATALONG, aic->dp, xfer/4);
1866 1.6 mycroft aic->dleft -= xfer & ~3;
1867 1.6 mycroft aic->dp += xfer & ~3;
1868 1.6 mycroft xfer &= 3;
1869 1.6 mycroft }
1870 1.1 mycroft #else
1871 1.6 mycroft if (xfer >= 8) {
1872 1.6 mycroft insw(DMADATA, aic->dp, xfer/2);
1873 1.6 mycroft aic->dleft -= xfer & ~1;
1874 1.6 mycroft aic->dp += xfer & ~1;
1875 1.6 mycroft xfer &= 1;
1876 1.6 mycroft }
1877 1.6 mycroft #endif
1878 1.6 mycroft
1879 1.6 mycroft if (xfer) {
1880 1.6 mycroft outb(DMACNTRL0, ENDMA|B8MODE|INTEN);
1881 1.1 mycroft insb(DMADATA, aic->dp, xfer);
1882 1.1 mycroft aic->dleft -= xfer;
1883 1.6 mycroft aic->dp += xfer;
1884 1.6 mycroft outb(DMACNTRL0, ENDMA|DWORDPIO|INTEN);
1885 1.6 mycroft }
1886 1.6 mycroft
1887 1.6 mycroft if (done)
1888 1.6 mycroft break;
1889 1.1 mycroft }
1890 1.1 mycroft
1891 1.1 mycroft #if 0
1892 1.1 mycroft if (aic->dleft)
1893 1.1 mycroft printf("residual %d\n", aic->dleft);
1894 1.1 mycroft #endif
1895 1.1 mycroft
1896 1.1 mycroft aic->progress = olddleft != aic->dleft;
1897 1.1 mycroft /* Some SCSI-devices are rude enough to transfer more data than what
1898 1.1 mycroft * was requested, e.g. 2048 bytes from a CD-ROM instead of the
1899 1.6 mycroft * requested 512. Test for progress, i.e. real transfers. If no real
1900 1.6 mycroft * transfers have been performed (acb->dleft is probably already zero)
1901 1.6 mycroft * and the FIFO is not empty, waste some bytes....
1902 1.6 mycroft */
1903 1.6 mycroft if (!aic->progress) {
1904 1.6 mycroft int extra = 0;
1905 1.1 mycroft LOGLINE(aic);
1906 1.1 mycroft
1907 1.6 mycroft for (;;) {
1908 1.1 mycroft dmastat = inb(DMASTAT);
1909 1.2 mycroft if (dmastat & DFIFOEMP)
1910 1.1 mycroft break;
1911 1.1 mycroft (void) inb(DMADATA); /* Throw it away */
1912 1.6 mycroft extra++;
1913 1.1 mycroft }
1914 1.1 mycroft
1915 1.6 mycroft AIC_MISC(("aic: %d extra bytes from %d:%d\n", extra,
1916 1.1 mycroft acb->xs->sc_link->target, acb->xs->sc_link->lun));
1917 1.1 mycroft aic->progress = extra;
1918 1.1 mycroft }
1919 1.1 mycroft
1920 1.1 mycroft /* Stop the FIFO data path */
1921 1.1 mycroft outb(SXFRCTL0, CHEN);
1922 1.1 mycroft
1923 1.1 mycroft outb(DMACNTRL0, RSTFIFO|INTEN);
1924 1.1 mycroft /* Come back when REQ is set again */
1925 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
1926 1.1 mycroft LOGLINE(aic);
1927 1.1 mycroft }
1928 1.1 mycroft
1929 1.1 mycroft
1930 1.1 mycroft /*
1931 1.1 mycroft * This is the workhorse routine of the driver.
1932 1.1 mycroft * Deficiencies (for now):
1933 1.1 mycroft * 1) always uses programmed I/O
1934 1.1 mycroft * 2) doesn't support synchronous transfers properly (yet)
1935 1.1 mycroft */
1936 1.1 mycroft
1937 1.1 mycroft int
1938 1.1 mycroft aicintr(aic)
1939 1.1 mycroft register struct aic_softc *aic;
1940 1.1 mycroft {
1941 1.1 mycroft register struct acb *acb;
1942 1.1 mycroft register struct scsi_link *sc;
1943 1.1 mycroft register u_short iobase = aic->iobase;
1944 1.1 mycroft struct scsi_xfer *xs;
1945 1.1 mycroft struct aic_tinfo *ti;
1946 1.1 mycroft int done, amount;
1947 1.1 mycroft u_char sstat0, sstat1, scsisig, dmastat, sstat2;
1948 1.1 mycroft u_char scsiseq, simode0, simode1, sxfrctl0;
1949 1.1 mycroft
1950 1.1 mycroft LOGLINE(aic);
1951 1.1 mycroft /* Clear INTEN. This is important if we're running with edge
1952 1.1 mycroft * triggered interrupts as we don't guarantee that all interrupts will
1953 1.1 mycroft * be served during one single invocation of this routine, i.e. we may
1954 1.1 mycroft * need another edge.
1955 1.1 mycroft */
1956 1.1 mycroft outb(DMACNTRL0, 0);
1957 1.2 mycroft AIC_TRACE(("aicintr\n"));
1958 1.1 mycroft
1959 1.1 mycroft /*
1960 1.1 mycroft * 1st check for abnormal conditions, such as reset or parity errors
1961 1.1 mycroft */
1962 1.1 mycroft sstat1 = inb(SSTAT1);
1963 1.1 mycroft AIC_MISC(("s1:0x%02x ", sstat1));
1964 1.2 mycroft if (sstat1 & (SCSIRSTI|SCSIPERR)) {
1965 1.1 mycroft if (sstat1 & SCSIRSTI) {
1966 1.1 mycroft printf("aic: reset in -- reinitializing....\n");
1967 1.1 mycroft aic_init(aic); /* Restart everything */
1968 1.1 mycroft LOGLINE(aic);
1969 1.1 mycroft outb(DMACNTRL0, INTEN);
1970 1.1 mycroft return 1;
1971 1.1 mycroft } else {
1972 1.1 mycroft printf("aic: SCSI bus parity error\n");
1973 1.1 mycroft outb(CLRSINT1, CLRSCSIPERR);
1974 1.1 mycroft if (aic->prevphase == PH_MSGI)
1975 1.1 mycroft aic_sched_msgout(SEND_PARITY_ERROR);
1976 1.1 mycroft else
1977 1.1 mycroft aic_sched_msgout(SEND_INIT_DET_ERR);
1978 1.1 mycroft }
1979 1.1 mycroft }
1980 1.1 mycroft
1981 1.1 mycroft /*
1982 1.1 mycroft * If we're not already busy doing something test for the following
1983 1.1 mycroft * conditions:
1984 1.1 mycroft * 1) We have been reselected by something
1985 1.1 mycroft * 2) We have selected something successfully
1986 1.2 mycroft * 3) Our selection process has timed out
1987 1.1 mycroft * 4) This is really a bus free interrupt just to get a new command
1988 1.1 mycroft * going?
1989 1.1 mycroft * 5) Spurious interrupt?
1990 1.1 mycroft */
1991 1.1 mycroft sstat0 = inb(SSTAT0);
1992 1.1 mycroft AIC_MISC(("s0:0x%02x ", sstat0));
1993 1.1 mycroft if (aic->state != AIC_HASNEXUS) { /* No nexus yet */
1994 1.1 mycroft if (sstat0 & SELDI) {
1995 1.1 mycroft LOGLINE(aic);
1996 1.1 mycroft /* We have been reselected. Things to do:
1997 1.1 mycroft * a) If we're trying to select something ourselves
1998 1.1 mycroft * back off the current command.
1999 1.1 mycroft * b) "Wait" for a message in phase (IDENTIFY)
2000 1.1 mycroft * c) Call aic_msgin() to get the identify message and
2001 1.1 mycroft * retrieve the disconnected command from the wait
2002 1.1 mycroft * queue.
2003 1.1 mycroft */
2004 1.1 mycroft AIC_MISC(("reselect "));
2005 1.1 mycroft /* If we're trying to select a target ourselves,
2006 1.1 mycroft * push our command back into the rdy list.
2007 1.1 mycroft */
2008 1.1 mycroft if (aic->state == AIC_SELECTING) {
2009 1.1 mycroft AIC_MISC(("backoff selector "));
2010 1.1 mycroft TAILQ_INSERT_HEAD(&aic->ready_list, aic->nexus,
2011 1.1 mycroft chain);
2012 1.1 mycroft aic->nexus = NULL;
2013 1.1 mycroft }
2014 1.1 mycroft aic->state = AIC_RESELECTED;
2015 1.1 mycroft /* Clear interrupts, disable future selection stuff
2016 1.1 mycroft * including select interrupts and timeouts
2017 1.1 mycroft */
2018 1.1 mycroft outb(CLRSINT0, CLRSELDI);
2019 1.1 mycroft outb(SCSISEQ, 0);
2020 1.1 mycroft outb(SIMODE0, 0);
2021 1.1 mycroft /* Setup chip so we may detect spurious busfree
2022 1.2 mycroft * conditions later.
2023 1.1 mycroft */
2024 1.1 mycroft outb(CLRSINT1, CLRBUSFREE);
2025 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|
2026 1.1 mycroft ENSCSIPERR|ENREQINIT);
2027 1.1 mycroft /* Now, we're expecting an IDENTIFY message. */
2028 1.1 mycroft aic->phase = aicphase(aic);
2029 1.1 mycroft if (aic->phase & PH_PSBIT) {
2030 1.1 mycroft LOGLINE(aic);
2031 1.1 mycroft outb(DMACNTRL0, INTEN);
2032 1.1 mycroft return 1; /* Come back when REQ is set */
2033 1.2 mycroft }
2034 1.1 mycroft if (aic->phase == PH_MSGI)
2035 1.1 mycroft aic_msgin(aic); /* Handle identify message */
2036 1.1 mycroft else {
2037 1.1 mycroft /* Things are seriously fucked up.
2038 1.2 mycroft * Pull the brakes, i.e. RST
2039 1.1 mycroft */
2040 1.2 mycroft printf("aic at line %d: target didn't identify\n", __LINE__);
2041 1.1 mycroft Debugger();
2042 1.1 mycroft aic_init(aic);
2043 1.2 mycroft return 1;
2044 1.2 mycroft }
2045 1.2 mycroft if (aic->state != AIC_HASNEXUS) {/* IDENTIFY fail?! */
2046 1.1 mycroft printf("aic at line %d: identify failed\n",
2047 1.1 mycroft __LINE__);
2048 1.2 mycroft aic_init(aic);
2049 1.1 mycroft return 1;
2050 1.1 mycroft } else {
2051 1.1 mycroft outb(SIMODE1,
2052 1.1 mycroft ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
2053 1.1 mycroft /* Fallthrough to HASNEXUS part of aicintr */
2054 1.1 mycroft }
2055 1.1 mycroft } else if (sstat0 & SELDO) {
2056 1.1 mycroft LOGLINE(aic);
2057 1.2 mycroft /* We have selected a target. Things to do:
2058 1.1 mycroft * a) Determine what message(s) to send.
2059 1.1 mycroft * b) Verify that we're still selecting the target.
2060 1.1 mycroft * c) Mark device as busy.
2061 1.1 mycroft */
2062 1.1 mycroft acb = aic->nexus;
2063 1.1 mycroft if (!acb) {
2064 1.1 mycroft printf("aic at line %d: missing acb", __LINE__);
2065 1.1 mycroft Debugger();
2066 1.1 mycroft }
2067 1.1 mycroft sc = acb->xs->sc_link;
2068 1.1 mycroft ti = &aic->tinfo[sc->target];
2069 1.1 mycroft if (acb->xs->flags & SCSI_RESET)
2070 1.1 mycroft aic->msgpriq = SEND_DEV_RESET;
2071 1.1 mycroft else if (ti->flags & DO_NEGOTIATE)
2072 1.1 mycroft aic->msgpriq = SEND_IDENTIFY|SEND_SDTR;
2073 1.1 mycroft else
2074 1.1 mycroft aic->msgpriq = SEND_IDENTIFY;
2075 1.1 mycroft /* Setup chip to enable later testing for busfree
2076 1.1 mycroft * conditions
2077 1.1 mycroft */
2078 1.1 mycroft outb(CLRSINT1, CLRBUSFREE);
2079 1.1 mycroft outb(SCSISEQ, 0); /* Stop selection stuff */
2080 1.1 mycroft nandreg(SIMODE0, ENSELDO); /* No more selectout ints */
2081 1.1 mycroft sstat0 = inb(SSTAT0);
2082 1.1 mycroft if (sstat0 & SELDO) { /* Still selected!? */
2083 1.1 mycroft outb(SIMODE0, 0);
2084 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENSCSIPERR|
2085 1.1 mycroft ENBUSFREE|ENREQINIT);
2086 1.1 mycroft aic->state = AIC_HASNEXUS;
2087 1.1 mycroft aic->flags = 0;
2088 1.1 mycroft aic->prevphase = PH_INVALID;
2089 1.1 mycroft aic->dp = acb->daddr;
2090 1.1 mycroft aic->dleft = acb->dleft;
2091 1.1 mycroft ti->lubusy |= (1<<sc->lun);
2092 1.5 cgd AIC_MISC(("select ok "));
2093 1.1 mycroft } else {
2094 1.1 mycroft /* Has seen busfree since selection, i.e.
2095 1.1 mycroft * a "spurious" selection. Shouldn't happen.
2096 1.1 mycroft */
2097 1.1 mycroft printf("aic: unexpected busfree\n");
2098 1.2 mycroft xs->error = XS_DRIVER_STUFFUP;
2099 1.1 mycroft untimeout(aic_timeout, acb);
2100 1.1 mycroft aic_done(acb);
2101 1.1 mycroft }
2102 1.1 mycroft LOGLINE(aic);
2103 1.1 mycroft outb(DMACNTRL0, INTEN);
2104 1.1 mycroft return 1;
2105 1.2 mycroft } else if (sstat1 & SELTO) {
2106 1.1 mycroft /* Selection timed out. What to do:
2107 1.1 mycroft * Disable selections out and fail the command with
2108 1.1 mycroft * code XS_TIMEOUT.
2109 1.1 mycroft */
2110 1.1 mycroft acb = aic->nexus;
2111 1.1 mycroft if (!acb) {
2112 1.1 mycroft printf("aic at line %d: missing acb", __LINE__);
2113 1.5 cgd Debugger();
2114 1.1 mycroft }
2115 1.1 mycroft outb(SCSISEQ, ENRESELI|ENAUTOATNP);
2116 1.1 mycroft outb(SXFRCTL1, 0);
2117 1.1 mycroft outb(CLRSINT1, CLRSELTIMO);
2118 1.2 mycroft aic->state = AIC_IDLE;
2119 1.1 mycroft acb->xs->error = XS_TIMEOUT;
2120 1.1 mycroft untimeout(aic_timeout, acb);
2121 1.1 mycroft aic_done(acb);
2122 1.1 mycroft LOGLINE(aic);
2123 1.1 mycroft outb(DMACNTRL0, INTEN);
2124 1.1 mycroft return 1;
2125 1.1 mycroft } else {
2126 1.1 mycroft /* Assume a bus free interrupt. What to do:
2127 1.1 mycroft * Start selecting.
2128 1.1 mycroft */
2129 1.1 mycroft if (aic->state == AIC_IDLE)
2130 1.1 mycroft aic_sched(aic);
2131 1.1 mycroft else
2132 1.1 mycroft AIC_MISC(("Extra aic6360 interrupt."));
2133 1.1 mycroft LOGLINE(aic);
2134 1.1 mycroft outb(DMACNTRL0, INTEN);
2135 1.2 mycroft return 1;
2136 1.1 mycroft }
2137 1.1 mycroft }
2138 1.1 mycroft /* Driver is now in state AIC_HASNEXUS, i.e. we have a current command
2139 1.1 mycroft * working the SCSI bus.
2140 1.1 mycroft */
2141 1.1 mycroft acb = aic->nexus;
2142 1.2 mycroft if (aic->state != AIC_HASNEXUS || acb == NULL) {
2143 1.1 mycroft printf("aic: no nexus!!\n");
2144 1.1 mycroft Debugger();
2145 1.1 mycroft }
2146 1.1 mycroft
2147 1.1 mycroft /* What sort of transfer does the bus signal? */
2148 1.2 mycroft aic->phase = aicphase(aic);
2149 1.1 mycroft if (!(aic->phase & PH_PSBIT)) /* not a pseudo phase */
2150 1.1 mycroft outb(SCSISIGO, aic->phase);
2151 1.1 mycroft outb(CLRSINT1, CLRPHASECHG);
2152 1.1 mycroft /* These interrupts are enabled by default:
2153 1.1 mycroft * SCSIRSTI, SCSIPERR, BUSFREE, REQINIT
2154 1.1 mycroft */
2155 1.1 mycroft switch (aic->phase) {
2156 1.1 mycroft case PH_MSGO:
2157 1.1 mycroft LOGLINE(aic);
2158 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
2159 1.1 mycroft printf("PH_MSGO ");
2160 1.1 mycroft aic_msgout(aic);
2161 1.1 mycroft aic->prevphase = PH_MSGO;
2162 1.1 mycroft /* Setup interrupts before leaving */
2163 1.1 mycroft outb(SIMODE0, 0);
2164 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
2165 1.1 mycroft /* Enabled ints: SCSIPERR, SCSIRSTI (unexpected)
2166 1.1 mycroft * REQINIT (expected) BUSFREE (possibly expected)
2167 1.1 mycroft */
2168 1.1 mycroft break;
2169 1.1 mycroft case PH_CMD: /* CMD phase & REQ asserted */
2170 1.1 mycroft LOGLINE(aic);
2171 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
2172 1.1 mycroft printf("PH_CMD 0x%02x (%d) ",
2173 1.1 mycroft acb->cmd.opcode, acb->clen);
2174 1.1 mycroft outb(SCSISIGO, PH_CMD);
2175 1.1 mycroft /* Use FIFO for CMDs. Assumes that no cmd > 128 bytes. OK? */
2176 1.1 mycroft /* Clear hostFIFO and enable EISA-hostFIFO transfers */
2177 1.1 mycroft outb(DMACNTRL0, WRITE|RSTFIFO|INTEN); /* 3(4) */
2178 1.1 mycroft /* Clear scsiFIFO and enable SCSI-interface
2179 1.1 mycroft & hostFIFO-scsiFIFO transfers */
2180 1.1 mycroft outb(SXFRCTL0, CHEN|CLRCH|CLRSTCNT); /* 4 */
2181 1.1 mycroft outb(SXFRCTL0, SCSIEN|DMAEN|CHEN); /* 5 */
2182 1.1 mycroft outb(DMACNTRL0, ENDMA|WRITE|INTEN); /* 3+6 */
2183 1.1 mycroft /* What (polled) interrupts to enable */
2184 1.1 mycroft outb(SIMODE1, ENPHASEMIS|ENSCSIRST|ENBUSFREE|ENSCSIPERR);
2185 1.2 mycroft /* DFIFOEMP is set, FIFO (128 byte) is always big enough */
2186 1.2 mycroft outsw(DMADATA, (short *)&acb->cmd, acb->clen>>1);
2187 1.1 mycroft
2188 1.2 mycroft /* Wait for SCSI FIFO to drain */
2189 1.1 mycroft LOGLINE(aic);
2190 1.1 mycroft do {
2191 1.5 cgd sstat2 = inb(SSTAT2);
2192 1.1 mycroft } while (!(sstat2 & SEMPTY) && !(inb(DMASTAT) & INTSTAT));
2193 1.1 mycroft if (!(inb(SSTAT2) & SEMPTY)) {
2194 1.1 mycroft printf("aic at line %d: SCSI-FIFO didn't drain\n",
2195 1.1 mycroft __LINE__);
2196 1.1 mycroft Debugger();
2197 1.1 mycroft acb->xs->error = XS_DRIVER_STUFFUP;
2198 1.1 mycroft untimeout(aic_timeout, acb);
2199 1.1 mycroft aic_done(acb);
2200 1.1 mycroft aic_init(aic);
2201 1.1 mycroft return 1;
2202 1.2 mycroft }
2203 1.2 mycroft outb(SXFRCTL0, CHEN); /* Clear SCSIEN & DMAEN */
2204 1.1 mycroft outb(SIMODE0, 0);
2205 1.2 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR);
2206 1.1 mycroft LOGLINE(aic);
2207 1.1 mycroft do {
2208 1.5 cgd sxfrctl0 = inb(SXFRCTL0);
2209 1.1 mycroft } while (sxfrctl0 & SCSIEN && !(inb(DMASTAT) & INTSTAT));
2210 1.1 mycroft if (sxfrctl0 & SCSIEN) {
2211 1.1 mycroft printf("aic at line %d: scsi xfer never finished\n",
2212 1.1 mycroft __LINE__);
2213 1.1 mycroft Debugger();
2214 1.1 mycroft acb->xs->error = XS_DRIVER_STUFFUP;
2215 1.1 mycroft untimeout(aic_timeout, acb);
2216 1.1 mycroft aic_done(acb);
2217 1.1 mycroft aic_init(aic);
2218 1.1 mycroft return 1;
2219 1.1 mycroft }
2220 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
2221 1.1 mycroft /* Enabled ints: BUSFREE, SCSIPERR, SCSIRSTI (unexpected)
2222 1.1 mycroft * REQINIT (expected)
2223 1.1 mycroft */
2224 1.1 mycroft aic->prevphase = PH_CMD;
2225 1.1 mycroft break;
2226 1.1 mycroft case PH_DOUT:
2227 1.1 mycroft LOGLINE(aic);
2228 1.1 mycroft AIC_MISC(("PH_DOUT [%d] ",aic->dleft));
2229 1.1 mycroft aic_dataout(aic);
2230 1.1 mycroft aic->prevphase = PH_DOUT;
2231 1.1 mycroft break;
2232 1.1 mycroft case PH_MSGI:
2233 1.1 mycroft LOGLINE(aic);
2234 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
2235 1.1 mycroft printf("PH_MSGI ");
2236 1.1 mycroft aic_msgin(aic);
2237 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
2238 1.1 mycroft aic->prevphase = PH_MSGI;
2239 1.1 mycroft break;
2240 1.1 mycroft case PH_DIN:
2241 1.1 mycroft LOGLINE(aic);
2242 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
2243 1.1 mycroft printf("PH_DIN ");
2244 1.1 mycroft aic_datain(aic);
2245 1.1 mycroft aic->prevphase = PH_DIN;
2246 1.1 mycroft break;
2247 1.1 mycroft case PH_STAT:
2248 1.1 mycroft LOGLINE(aic);
2249 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
2250 1.1 mycroft printf("PH_STAT ");
2251 1.1 mycroft outb(SCSISIGO, PH_STAT);
2252 1.1 mycroft outb(SXFRCTL0, CHEN|SPIOEN);
2253 1.1 mycroft outb(DMACNTRL0, RSTFIFO|INTEN);
2254 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE|ENSCSIPERR);
2255 1.1 mycroft acb->stat = inb(SCSIDAT);
2256 1.1 mycroft outb(SXFRCTL0, CHEN);
2257 1.1 mycroft if (aic_debug & AIC_SHOWMISC)
2258 1.1 mycroft printf("0x%02x ", acb->stat);
2259 1.1 mycroft outb(SIMODE1, ENSCSIRST|ENBUSFREE|ENSCSIPERR|ENREQINIT);
2260 1.1 mycroft aic->prevphase = PH_STAT;
2261 1.1 mycroft break;
2262 1.2 mycroft case PH_INVALID:
2263 1.2 mycroft LOGLINE(aic);
2264 1.2 mycroft break;
2265 1.1 mycroft case PH_BUSFREE:
2266 1.1 mycroft LOGLINE(aic);
2267 1.1 mycroft if (aic->flags & AIC_BUSFREE_OK) { /*It's fun the 1st time.. */
2268 1.1 mycroft aic->flags &= ~AIC_BUSFREE_OK;
2269 1.2 mycroft } else {
2270 1.1 mycroft printf("aic at line %d: unexpected busfree phase\n",
2271 1.1 mycroft __LINE__);
2272 1.1 mycroft Debugger();
2273 1.1 mycroft }
2274 1.1 mycroft break;
2275 1.1 mycroft default:
2276 1.1 mycroft printf("aic at line %d: bogus bus phase\n", __LINE__);
2277 1.1 mycroft Debugger();
2278 1.1 mycroft break;
2279 1.1 mycroft }
2280 1.5 cgd LOGLINE(aic);
2281 1.1 mycroft outb(DMACNTRL0, INTEN);
2282 1.1 mycroft return 1;
2283 1.5 cgd }
2284 1.1 mycroft
2285 1.1 mycroft void
2286 1.1 mycroft aic_timeout(arg)
2287 1.1 mycroft void *arg;
2288 1.1 mycroft {
2289 1.1 mycroft int s = splbio();
2290 1.1 mycroft struct acb *acb = (struct acb *)arg;
2291 1.1 mycroft struct aic_softc *aic;
2292 1.1 mycroft
2293 1.1 mycroft aic = acb->xs->sc_link->adapter_softc;
2294 1.1 mycroft sc_print_addr(acb->xs->sc_link);
2295 1.1 mycroft acb->xs->error = XS_TIMEOUT;
2296 1.1 mycroft printf("timed out\n");
2297 1.1 mycroft
2298 1.1 mycroft aic_done(acb);
2299 1.1 mycroft splx(s);
2300 1.1 mycroft }
2301 1.1 mycroft
2302 1.1 mycroft #ifdef AIC_DEBUG
2304 1.1 mycroft /*
2305 1.1 mycroft * The following functions are mostly used for debugging purposes, either
2306 1.1 mycroft * directly called from the driver or from the kernel debugger.
2307 1.1 mycroft */
2308 1.1 mycroft
2309 1.2 mycroft void
2310 1.2 mycroft aic_show_scsi_cmd(acb)
2311 1.2 mycroft struct acb *acb;
2312 1.2 mycroft {
2313 1.1 mycroft u_char *b = (u_char *)&acb->cmd;
2314 1.1 mycroft struct scsi_link *sc = acb->xs->sc_link;
2315 1.1 mycroft int i;
2316 1.1 mycroft
2317 1.2 mycroft sc_print_addr(sc);
2318 1.2 mycroft if (!(acb->xs->flags & SCSI_RESET)) {
2319 1.1 mycroft for (i = 0; i < acb->clen; i++) {
2320 1.1 mycroft if (i)
2321 1.1 mycroft printf(",");
2322 1.1 mycroft printf("%x", b[i]);
2323 1.1 mycroft }
2324 1.1 mycroft printf("\n");
2325 1.2 mycroft } else
2326 1.2 mycroft printf("RESET\n");
2327 1.1 mycroft }
2328 1.2 mycroft
2329 1.1 mycroft void
2330 1.1 mycroft aic_print_acb(acb)
2331 1.1 mycroft struct acb *acb;
2332 1.1 mycroft {
2333 1.1 mycroft
2334 1.1 mycroft printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
2335 1.1 mycroft printf(" daddr=%x dleft=%d stat=%x\n",
2336 1.1 mycroft (long)acb->daddr, acb->dleft, acb->stat);
2337 1.1 mycroft aic_show_scsi_cmd(acb);
2338 1.1 mycroft }
2339 1.1 mycroft
2340 1.1 mycroft void
2341 1.1 mycroft aic_print_active_acb()
2342 1.1 mycroft {
2343 1.1 mycroft struct acb *acb;
2344 1.1 mycroft struct aic_softc *aic = aiccd.cd_devs[0];
2345 1.1 mycroft
2346 1.1 mycroft printf("ready list:\n");
2347 1.1 mycroft for (acb = aic->ready_list.tqh_first; acb; acb = acb->chain.tqe_next)
2348 1.1 mycroft aic_print_acb(acb);
2349 1.1 mycroft printf("nexus:\n");
2350 1.1 mycroft if (aic->nexus)
2351 1.1 mycroft aic_print_acb(aic->nexus);
2352 1.1 mycroft printf("nexus list:\n");
2353 1.1 mycroft for (acb = aic->nexus_list.tqh_first; acb; acb = acb->chain.tqe_next)
2354 1.1 mycroft aic_print_acb(acb);
2355 1.2 mycroft }
2356 1.1 mycroft
2357 1.2 mycroft void
2358 1.1 mycroft aic_dump6360()
2359 1.2 mycroft {
2360 1.2 mycroft u_short iobase = 0x340;
2361 1.1 mycroft
2362 1.2 mycroft printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIGI=%x\n",
2363 1.1 mycroft inb(SCSISEQ), inb(SXFRCTL0), inb(SXFRCTL1), inb(SCSISIGI));
2364 1.1 mycroft printf(" SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
2365 1.1 mycroft inb(SSTAT0), inb(SSTAT1), inb(SSTAT2), inb(SSTAT3), inb(SSTAT4));
2366 1.1 mycroft printf(" SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n",
2367 1.1 mycroft inb(SIMODE0), inb(SIMODE1), inb(DMACNTRL0), inb(DMACNTRL1),
2368 1.1 mycroft inb(DMASTAT));
2369 1.1 mycroft printf(" FIFOSTAT=%d SCSIBUS=0x%x\n",
2370 1.1 mycroft inb(FIFOSTAT), inb(SCSIBUS));
2371 1.1 mycroft }
2372 1.1 mycroft
2373 1.2 mycroft void
2374 1.1 mycroft aic_dump_driver()
2375 1.2 mycroft {
2376 1.2 mycroft struct aic_softc *aic = aiccd.cd_devs[0];
2377 1.1 mycroft struct aic_tinfo *ti;
2378 1.1 mycroft int i;
2379 1.1 mycroft
2380 1.1 mycroft printf("nexus=%x phase=%x prevphase=%x\n", aic->nexus, aic->phase,
2381 1.1 mycroft aic->prevphase);
2382 1.1 mycroft printf("state=%x msgin=%x msgpriq=%x msgout=%x imlen=%d omlen=%d\n",
2383 1.1 mycroft aic->state, aic->imess[0], aic->msgpriq, aic->msgout, aic->imlen,
2384 1.2 mycroft aic->omlen);
2385 1.1 mycroft printf("history:");
2386 1.1 mycroft i = aic->hp;
2387 1.2 mycroft do {
2388 1.2 mycroft printf(" %d", aic->history[i]);
2389 1.1 mycroft i = (i + 1) % AIC_HSIZE;
2390 1.1 mycroft } while (i != aic->hp);
2391 1.1 mycroft printf("*\n");
2392 for (i = 0; i < 7; i++) {
2393 ti = &aic->tinfo[i];
2394 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2395 i, ti->cmds, ti->dconns, ti->touts);
2396 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2397 }
2398 }
2399 #endif
2400