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