aic6360.c revision 1.31 1 /* $NetBSD: aic6360.c,v 1.31 1995/04/17 12:08:32 cgd 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 1
74 #define AIC_SYNC_REQ_ACK_OFS 8
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 <dev/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 void *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_lastmsg; /* Message last transmitted */
529 u_char sc_currmsg; /* Message currently ready to transmit */
530 #define SEND_DEV_RESET 0x01
531 #define SEND_PARITY_ERROR 0x02
532 #define SEND_ABORT 0x04
533 #define SEND_REJECT 0x08
534 #define SEND_INIT_DET_ERR 0x10
535 #define SEND_IDENTIFY 0x20
536 #define SEND_SDTR 0x40
537 #define SEND_WDTR 0x80
538 #define AIC_MAX_MSG_LEN 8
539 u_char sc_omess[AIC_MAX_MSG_LEN];
540 u_char *sc_omp; /* Outgoing message pointer */
541 u_char sc_imess[AIC_MAX_MSG_LEN];
542 u_char *sc_imp; /* Incoming message pointer */
543
544 /* Hardware stuff */
545 int sc_initiator; /* Our scsi id */
546 int sc_freq; /* Clock frequency in MHz */
547 int sc_minsync; /* Minimum sync period / 4 */
548 int sc_maxsync; /* Maximum sync period / 4 */
549 };
550
551 #if AIC_DEBUG
552 #define AIC_SHOWACBS 0x01
553 #define AIC_SHOWINTS 0x02
554 #define AIC_SHOWCMDS 0x04
555 #define AIC_SHOWMISC 0x08
556 #define AIC_SHOWTRACE 0x10
557 #define AIC_SHOWSTART 0x20
558 #define AIC_DOBREAK 0x40
559 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; /**/
560 #define AIC_PRINT(b, s) do {if ((aic_debug & (b)) != 0) printf s;} while (0)
561 #define AIC_BREAK() do {if ((aic_debug & AIC_DOBREAK) != 0) Debugger();} while (0)
562 #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)
563 #else
564 #define AIC_PRINT(b, s)
565 #define AIC_BREAK()
566 #define AIC_ASSERT(x)
567 #endif
568
569 #define AIC_ACBS(s) AIC_PRINT(AIC_SHOWACBS, s)
570 #define AIC_INTS(s) AIC_PRINT(AIC_SHOWINTS, s)
571 #define AIC_CMDS(s) AIC_PRINT(AIC_SHOWCMDS, s)
572 #define AIC_MISC(s) AIC_PRINT(AIC_SHOWMISC, s)
573 #define AIC_TRACE(s) AIC_PRINT(AIC_SHOWTRACE, s)
574 #define AIC_START(s) AIC_PRINT(AIC_SHOWSTART, s)
575
576 int aicprobe __P((struct device *, void *, void *));
577 void aicattach __P((struct device *, struct device *, void *));
578 void aic_minphys __P((struct buf *));
579 int aicintr __P((void *));
580 void aic_init __P((struct aic_softc *));
581 void aic_done __P((struct aic_softc *, struct aic_acb *));
582 void aic_dequeue __P((struct aic_softc *, struct aic_acb *));
583 int aic_scsi_cmd __P((struct scsi_xfer *));
584 int aic_poll __P((struct aic_softc *, struct scsi_xfer *, int));
585 void aic_select __P((struct aic_softc *, struct aic_acb *));
586 void aic_timeout __P((void *));
587 int aic_find __P((struct aic_softc *));
588 void aic_sched __P((struct aic_softc *));
589 void aic_scsi_reset __P((struct aic_softc *));
590 void aic_reset __P((struct aic_softc *));
591 #if AIC_DEBUG
592 void aic_print_active_acb();
593 void aic_dump_driver();
594 void aic_dump6360();
595 #endif
596
597 struct cfdriver aiccd = {
598 NULL, "aic", aicprobe, aicattach, DV_DULL, sizeof(struct aic_softc)
599 };
600
601 struct scsi_adapter aic_switch = {
602 aic_scsi_cmd,
603 aic_minphys,
604 0,
605 0,
606 };
607
608 struct scsi_device aic_dev = {
609 NULL, /* Use default error handler */
610 NULL, /* have a queue, served by this */
611 NULL, /* have no async handler */
612 NULL, /* Use default 'done' routine */
613 };
614
615 /*
617 * INITIALIZATION ROUTINES (probe, attach ++)
618 */
619
620 /*
621 * aicprobe: probe for AIC6360 SCSI-controller
622 * returns non-zero value if a controller is found.
623 */
624 int
625 aicprobe(parent, match, aux)
626 struct device *parent;
627 void *match, *aux;
628 {
629 struct aic_softc *sc = match;
630 struct isa_attach_args *ia = aux;
631 int i, len, ic;
632
633 #ifdef NEWCONFIG
634 if (ia->ia_iobase == IOBASEUNK)
635 return 0;
636 #endif
637
638 sc->sc_iobase = ia->ia_iobase;
639 if (aic_find(sc) != 0)
640 return 0;
641
642 #ifdef NEWCONFIG
643 if (ia->ia_irq != IRQUNK) {
644 if (ia->ia_irq != sc->sc_irq) {
645 printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
646 sc->sc_dev.dv_xname, ia->ia_irq, sc->sc_irq);
647 return 0;
648 }
649 } else
650 ia->ia_irq = sc->sc_irq;
651
652 if (ia->ia_drq != DRQUNK) {
653 if (ia->ia_drq != sc->sc_drq) {
654 printf("%s: drq mismatch; kernel configured %d != board configured %d\n",
655 sc->sc_dev.dv_xname, ia->ia_drq, sc->sc_drq);
656 return 0;
657 }
658 } else
659 ia->ia_drq = sc->sc_drq;
660 #endif
661
662 ia->ia_msize = 0;
663 ia->ia_iosize = 0x20;
664 return 1;
665 }
666
667 /* Do the real search-for-device.
668 * Prerequisite: sc->sc_iobase should be set to the proper value
669 */
670 int
671 aic_find(sc)
672 struct aic_softc *sc;
673 {
674 char chip_id[sizeof(IDSTRING)]; /* For chips that support it */
675 char *start;
676 int i;
677
678 /* Remove aic6360 from possible powerdown mode */
679 outb(DMACNTRL0, 0);
680
681 /* Thanks to mark (at) aggregate.com for the new method for detecting
682 * whether the chip is present or not. Bonus: may also work for
683 * the AIC-6260!
684 */
685 AIC_TRACE(("aic: probing for aic-chip at port 0x%x\n",
686 sc->sc_iobase));
687 /*
688 * Linux also init's the stack to 1-16 and then clears it,
689 * 6260's don't appear to have an ID reg - mpg
690 */
691 /* Push the sequence 0,1,..,15 on the stack */
692 #define STSIZE 16
693 outb(DMACNTRL1, 0); /* Reset stack pointer */
694 for (i = 0; i < STSIZE; i++)
695 outb(STACK, i);
696
697 /* See if we can pull out the same sequence */
698 outb(DMACNTRL1, 0);
699 for (i = 0; i < STSIZE && inb(STACK) == i; i++)
700 ;
701 if (i != STSIZE) {
702 AIC_START(("STACK futzed at %d.\n", i));
703 return ENXIO;
704 }
705
706 /* See if we can pull the id string out of the ID register,
707 * now only used for informational purposes.
708 */
709 bzero(chip_id, sizeof(chip_id));
710 insb(ID, chip_id, sizeof(IDSTRING)-1);
711 AIC_START(("AIC found at 0x%x ", sc->sc_iobase));
712 AIC_START(("ID: %s ",chip_id));
713 AIC_START(("chip revision %d\n",(int)inb(REV)));
714
715 sc->sc_initiator = 7;
716 sc->sc_freq = 20; /* XXXX Assume 20 MHz. */
717
718 /*
719 * These are the bounds of the sync period, based on the frequency of
720 * the chip's clock input and the size and offset of the sync period
721 * register.
722 *
723 * For a 20Mhz clock, this gives us 25, or 100nS, or 10MB/s, as a
724 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a
725 * minimum transfer rate.
726 */
727 sc->sc_minsync = (2 * 250) / sc->sc_freq;
728 sc->sc_maxsync = (9 * 250) / sc->sc_freq;
729
730 return 0;
731 }
732
733 int
734 aicprint()
735 {
736
737 }
738
739 /*
740 * Attach the AIC6360, fill out some high and low level data structures
741 */
742 void
743 aicattach(parent, self, aux)
744 struct device *parent, *self;
745 void *aux;
746 {
747 struct isa_attach_args *ia = aux;
748 struct aic_softc *sc = (void *)self;
749
750 AIC_TRACE(("aicattach "));
751 sc->sc_state = 0;
752 aic_init(sc); /* Init chip and driver */
753
754 /*
755 * Fill in the prototype scsi_link
756 */
757 sc->sc_link.adapter_softc = sc;
758 sc->sc_link.adapter_target = sc->sc_initiator;
759 sc->sc_link.adapter = &aic_switch;
760 sc->sc_link.device = &aic_dev;
761 sc->sc_link.openings = 2;
762
763 printf("\n");
764
765 #ifdef NEWCONFIG
766 isa_establish(&sc->sc_id, &sc->sc_dev);
767 #endif
768 sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
769 aicintr, sc);
770
771 config_found(self, &sc->sc_link, aicprint);
772 }
773
774
775 /* Initialize AIC6360 chip itself
776 * The following conditions should hold:
777 * aicprobe should have succeeded, i.e. the iobase address in aic_softc must
778 * be valid.
779 */
780 void
781 aic_reset(sc)
782 struct aic_softc *sc;
783 {
784
785 outb(SCSITEST, 0); /* Doc. recommends to clear these two */
786 outb(TEST, 0); /* registers before operations commence */
787
788 /* Reset SCSI-FIFO and abort any transfers */
789 outb(SXFRCTL0, CHEN|CLRCH|CLRSTCNT);
790
791 /* Reset DMA-FIFO */
792 outb(DMACNTRL0, RSTFIFO);
793 outb(DMACNTRL1, 0);
794
795 outb(SCSISEQ, 0); /* Disable all selection features */
796 outb(SXFRCTL1, 0);
797
798 outb(SIMODE0, 0x00); /* Disable some interrupts */
799 outb(CLRSINT0, 0x7f); /* Clear a slew of interrupts */
800
801 outb(SIMODE1, 0x00); /* Disable some more interrupts */
802 outb(CLRSINT1, 0xef); /* Clear another slew of interrupts */
803
804 outb(SCSIRATE, 0); /* Disable synchronous transfers */
805
806 outb(CLRSERR, 0x07); /* Haven't seen ant errors (yet) */
807
808 outb(SCSIID, sc->sc_initiator << OID_S); /* Set our SCSI-ID */
809 outb(BRSTCNTRL, EISA_BRST_TIM);
810 }
811
812 /* Pull the SCSI RST line for 500 us */
813 void
814 aic_scsi_reset(sc)
815 struct aic_softc *sc;
816 {
817
818 outb(SCSISEQ, SCSIRSTO);
819 delay(500);
820 outb(SCSISEQ, 0);
821 delay(50);
822 }
823
824 /*
825 * Initialize aic SCSI driver.
826 */
827 void
828 aic_init(sc)
829 struct aic_softc *sc;
830 {
831 struct aic_acb *acb;
832 int r;
833
834 aic_reset(sc);
835 aic_scsi_reset(sc);
836 aic_reset(sc);
837
838 if (sc->sc_state == 0) {
839 /* First time through; initialize. */
840 TAILQ_INIT(&sc->ready_list);
841 TAILQ_INIT(&sc->nexus_list);
842 TAILQ_INIT(&sc->free_list);
843 sc->sc_nexus = NULL;
844 acb = sc->sc_acb;
845 bzero(acb, sizeof(sc->sc_acb));
846 for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
847 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
848 acb++;
849 }
850 bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
851 } else {
852 /* Cancel any active commands. */
853 sc->sc_state = AIC_CLEANING;
854 if ((acb = sc->sc_nexus) != NULL) {
855 acb->xs->error = XS_DRIVER_STUFFUP;
856 untimeout(aic_timeout, acb);
857 aic_done(sc, acb);
858 }
859 while (acb = sc->nexus_list.tqh_first) {
860 acb->xs->error = XS_DRIVER_STUFFUP;
861 untimeout(aic_timeout, acb);
862 aic_done(sc, acb);
863 }
864 }
865
866 sc->sc_prevphase = PH_INVALID;
867 for (r = 0; r < 8; r++) {
868 struct aic_tinfo *ti = &sc->sc_tinfo[r];
869
870 ti->flags = 0;
871 #if AIC_USE_SYNCHRONOUS
872 ti->flags |= DO_SYNC;
873 ti->period = sc->sc_minsync;
874 ti->offset = AIC_SYNC_REQ_ACK_OFS;
875 #else
876 ti->period = ti->offset = 0;
877 #endif
878 #if AIC_USE_WIDE
879 ti->flags |= DO_WIDE;
880 ti->width = AIC_MAX_WIDTH;
881 #else
882 ti->width = 0;
883 #endif
884 }
885
886 sc->sc_state = AIC_IDLE;
887 outb(DMACNTRL0, INTEN);
888 }
889
890 void
891 aic_free_acb(sc, acb, flags)
892 struct aic_softc *sc;
893 struct aic_acb *acb;
894 int flags;
895 {
896 int s;
897
898 s = splbio();
899
900 acb->flags = ACB_FREE;
901 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
902 if (acb->chain.tqe_next == 0)
903 wakeup(&sc->free_list);
904
905 splx(s);
906 }
907
908 struct aic_acb *
909 aic_get_acb(sc, flags)
910 struct aic_softc *sc;
911 int flags;
912 {
913 int s;
914 struct aic_acb *acb;
915
916 /* Get a aic command block */
917 s = splbio();
918
919 while ((acb = sc->free_list.tqh_first) == NULL &&
920 (flags & SCSI_NOSLEEP) == 0)
921 tsleep(&sc->free_list, PRIBIO, "aicacb", 0);
922 if (acb) {
923 TAILQ_REMOVE(&sc->free_list, acb, chain);
924 acb->flags = ACB_ACTIVE;
925 }
926
927 splx(s);
928 return acb;
929 }
930
931 /*
933 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
934 */
935
936 /*
937 * Expected sequence:
938 * 1) Command inserted into ready list
939 * 2) Command selected for execution
940 * 3) Command won arbitration and has selected target device
941 * 4) Send message out (identify message, eventually also sync.negotiations)
942 * 5) Send command
943 * 5a) Receive disconnect message, disconnect.
944 * 5b) Reselected by target
945 * 5c) Receive identify message from target.
946 * 6) Send or receive data
947 * 7) Receive status
948 * 8) Receive message (command complete etc.)
949 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
950 * Repeat 2-8 (no disconnects please...)
951 */
952
953 /*
954 * Start a SCSI-command
955 * This function is called by the higher level SCSI-driver to queue/run
956 * SCSI-commands.
957 */
958 int
959 aic_scsi_cmd(xs)
960 struct scsi_xfer *xs;
961 {
962 struct scsi_link *sc_link = xs->sc_link;
963 struct aic_softc *sc = sc_link->adapter_softc;
964 struct aic_acb *acb;
965 int s, flags;
966
967 AIC_TRACE(("aic_scsi_cmd "));
968 AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
969 sc_link->target));
970
971 flags = xs->flags;
972 if ((flags & (ITSDONE|INUSE)) != INUSE) {
973 printf("%s: done or not in use?\n", sc->sc_dev.dv_xname);
974 xs->flags &= ~ITSDONE;
975 xs->flags |= INUSE;
976 }
977
978 if ((acb = aic_get_acb(sc, flags)) == NULL) {
979 xs->error = XS_DRIVER_STUFFUP;
980 return TRY_AGAIN_LATER;
981 }
982
983 /* Initialize acb */
984 acb->xs = xs;
985 bcopy(xs->cmd, &acb->scsi_cmd, xs->cmdlen);
986 acb->scsi_cmd_length = xs->cmdlen;
987 acb->data_addr = xs->data;
988 acb->data_length = xs->datalen;
989 acb->target_stat = 0;
990
991 s = splbio();
992
993 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
994 if (sc->sc_state == AIC_IDLE)
995 aic_sched(sc);
996
997 if ((flags & SCSI_POLL) == 0) { /* Almost done. Wait outside */
998 timeout(aic_timeout, acb, (xs->timeout * hz) / 1000);
999 splx(s);
1000 return SUCCESSFULLY_QUEUED;
1001 }
1002
1003 splx(s);
1004
1005 /* Not allowed to use interrupts, use polling instead */
1006 if (aic_poll(sc, xs, xs->timeout)) {
1007 aic_timeout(acb);
1008 if (aic_poll(sc, xs, 2000))
1009 aic_timeout(acb);
1010 }
1011 return COMPLETE;
1012 }
1013
1014 /*
1015 * Adjust transfer size in buffer structure
1016 */
1017 void
1018 aic_minphys(bp)
1019 struct buf *bp;
1020 {
1021
1022 AIC_TRACE(("aic_minphys "));
1023 if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
1024 bp->b_bcount = (AIC_NSEG << PGSHIFT);
1025 }
1026
1027 /*
1028 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
1029 */
1030 int
1031 aic_poll(sc, xs, count)
1032 struct aic_softc *sc;
1033 struct scsi_xfer *xs;
1034 int count;
1035 {
1036
1037 AIC_TRACE(("aic_poll "));
1038 while (count) {
1039 /*
1040 * If we had interrupts enabled, would we
1041 * have got an interrupt?
1042 */
1043 if ((inb(DMASTAT) & INTSTAT) != 0)
1044 aicintr(sc);
1045 if ((xs->flags & ITSDONE) != 0)
1046 return 0;
1047 delay(1000);
1048 count--;
1049 }
1050 return 1;
1051 }
1052
1053 /*
1055 * LOW LEVEL SCSI UTILITIES
1056 */
1057
1058 #define aic_sched_msgout(m) \
1059 do { \
1060 if (sc->sc_msgpriq == 0) \
1061 outb(SCSISIG, sc->sc_phase|ATNO); \
1062 sc->sc_msgpriq |= (m); \
1063 } while (0)
1064
1065 #if AIC_USE_SYNCHRONOUS
1066 /*
1067 * Set synchronous transfer offset and period.
1068 */
1069 static inline void
1070 aic_setsync(sc, ti)
1071 struct aic_softc *sc;
1072 struct aic_tinfo *ti;
1073 {
1074
1075 if (ti->offset != 0)
1076 outb(SCSIRATE,
1077 ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
1078 else
1079 outb(SCSIRATE, 0);
1080 }
1081 #else
1082 #define aic_setsync(sc, ti)
1083 #endif
1084
1085 /*
1086 * Start a selection. This is used by aic_sched() to select an idle target,
1087 * and by aic_done() to immediately reselect a target to get sense information.
1088 */
1089 void
1090 aic_select(sc, acb)
1091 struct aic_softc *sc;
1092 struct aic_acb *acb;
1093 {
1094 struct scsi_link *sc_link = acb->xs->sc_link;
1095 int target = sc_link->target;
1096 struct aic_tinfo *ti = &sc->sc_tinfo[target];
1097
1098 outb(SCSIID, sc->sc_initiator << OID_S | target);
1099 aic_setsync(sc, ti);
1100 outb(SXFRCTL1, STIMO_256ms|ENSTIMER);
1101
1102 /* Always enable reselections. */
1103 outb(SIMODE0, ENSELDI|ENSELDO);
1104 outb(SIMODE1, ENSCSIRST|ENSELTIMO);
1105 outb(SCSISEQ, ENRESELI|ENSELO|ENAUTOATNO);
1106
1107 sc->sc_state = AIC_SELECTING;
1108 }
1109
1110 int
1111 aic_reselect(sc, message)
1112 struct aic_softc *sc;
1113 u_char message;
1114 {
1115 u_char selid, target, lun;
1116 struct aic_acb *acb;
1117 struct scsi_link *sc_link;
1118 struct aic_tinfo *ti;
1119
1120 /*
1121 * The SCSI chip made a snapshot of the data bus while the reselection
1122 * was being negotiated. This enables us to determine which target did
1123 * the reselect.
1124 */
1125 selid = sc->sc_selid & ~(1 << sc->sc_initiator);
1126 if (selid & (selid - 1)) {
1127 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
1128 sc->sc_dev.dv_xname, selid);
1129 AIC_BREAK();
1130 goto reset;
1131 }
1132
1133 /* Search wait queue for disconnected cmd
1134 * The list should be short, so I haven't bothered with
1135 * any more sophisticated structures than a simple
1136 * singly linked list.
1137 */
1138 target = ffs(selid) - 1;
1139 lun = message & 0x07;
1140 for (acb = sc->nexus_list.tqh_first; acb != NULL;
1141 acb = acb->chain.tqe_next) {
1142 sc_link = acb->xs->sc_link;
1143 if (sc_link->target == target && sc_link->lun == lun)
1144 break;
1145 }
1146 if (acb == NULL) {
1147 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
1148 sc->sc_dev.dv_xname, target, lun);
1149 AIC_BREAK();
1150 goto abort;
1151 }
1152
1153 /* Make this nexus active again. */
1154 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1155 sc->sc_state = AIC_CONNECTED;
1156 sc->sc_nexus = acb;
1157 ti = &sc->sc_tinfo[target];
1158 ti->lubusy |= (1 << lun);
1159 aic_setsync(sc, ti);
1160
1161 /* Do an implicit RESTORE POINTERS. */
1162 sc->sc_dp = acb->data_addr;
1163 sc->sc_dleft = acb->data_length;
1164 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1165 sc->sc_cleft = acb->scsi_cmd_length;
1166
1167 return (0);
1168
1169 reset:
1170 sc->sc_flags |= AIC_ABORTING;
1171 aic_sched_msgout(SEND_DEV_RESET);
1172 return (1);
1173
1174 abort:
1175 sc->sc_flags |= AIC_ABORTING;
1176 aic_sched_msgout(SEND_ABORT);
1177 return (1);
1178 }
1179
1180 /*
1182 * Schedule a SCSI operation. This has now been pulled out of the interrupt
1183 * handler so that we may call it from aic_scsi_cmd and aic_done. This may
1184 * save us an unecessary interrupt just to get things going. Should only be
1185 * called when state == AIC_IDLE and at bio pl.
1186 */
1187 void
1188 aic_sched(sc)
1189 register struct aic_softc *sc;
1190 {
1191 struct aic_acb *acb;
1192 struct scsi_link *sc_link;
1193 struct aic_tinfo *ti;
1194
1195 /*
1196 * Find first acb in ready queue that is for a target/lunit pair that
1197 * is not busy.
1198 */
1199 outb(CLRSINT1, CLRSELTIMO|CLRBUSFREE|CLRSCSIPERR);
1200 for (acb = sc->ready_list.tqh_first; acb != NULL;
1201 acb = acb->chain.tqe_next) {
1202 sc_link = acb->xs->sc_link;
1203 ti = &sc->sc_tinfo[sc_link->target];
1204 if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
1205 AIC_MISC(("selecting %d:%d ",
1206 sc_link->target, sc_link->lun));
1207 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1208 sc->sc_nexus = acb;
1209 aic_select(sc, acb);
1210 return;
1211 } else
1212 AIC_MISC(("%d:%d busy\n",
1213 sc_link->target, sc_link->lun));
1214 }
1215 AIC_MISC(("idle "));
1216 /* Nothing to start; just enable reselections and wait. */
1217 outb(SIMODE0, ENSELDI);
1218 outb(SIMODE1, ENSCSIRST);
1219 outb(SCSISEQ, ENRESELI);
1220 }
1221
1222 /*
1224 * POST PROCESSING OF SCSI_CMD (usually current)
1225 */
1226 void
1227 aic_done(sc, acb)
1228 struct aic_softc *sc;
1229 struct aic_acb *acb;
1230 {
1231 struct scsi_xfer *xs = acb->xs;
1232 struct scsi_link *sc_link = xs->sc_link;
1233 struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->target];
1234
1235 AIC_TRACE(("aic_done "));
1236
1237 /*
1238 * Now, if we've come here with no error code, i.e. we've kept the
1239 * initial XS_NOERROR, and the status code signals that we should
1240 * check sense, we'll need to set up a request sense cmd block and
1241 * push the command back into the ready queue *before* any other
1242 * commands for this target/lunit, else we lose the sense info.
1243 * We don't support chk sense conditions for the request sense cmd.
1244 */
1245 if (xs->error == XS_NOERROR) {
1246 if (acb->flags == ACB_ABORTED) {
1247 xs->error = XS_DRIVER_STUFFUP;
1248 } else if (acb->flags == ACB_CHKSENSE) {
1249 xs->error = XS_SENSE;
1250 } else if (acb->target_stat == SCSI_CHECK) {
1251 struct scsi_sense *ss = (void *)&acb->scsi_cmd;
1252
1253 AIC_MISC(("requesting sense "));
1254 /* First, save the return values */
1255 xs->resid = acb->data_length;
1256 xs->status = acb->target_stat;
1257 /* Next, setup a request sense command block */
1258 bzero(ss, sizeof(*ss));
1259 ss->opcode = REQUEST_SENSE;
1260 ss->byte2 = sc_link->lun << 5;
1261 ss->length = sizeof(struct scsi_sense_data);
1262 acb->scsi_cmd_length = sizeof(*ss);
1263 acb->data_addr = (char *)&xs->sense;
1264 acb->data_length = sizeof(struct scsi_sense_data);
1265 acb->flags = ACB_CHKSENSE;
1266 ti->senses++;
1267 ti->lubusy &= ~(1<<sc_link->lun);
1268 if (acb == sc->sc_nexus) {
1269 aic_select(sc, acb);
1270 } else {
1271 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
1272 }
1273 return;
1274 } else {
1275 xs->resid = acb->data_length;
1276 }
1277 }
1278
1279 xs->flags |= ITSDONE;
1280
1281 #if AIC_DEBUG
1282 if ((aic_debug & AIC_SHOWMISC) != 0) {
1283 if (xs->resid != 0)
1284 printf("resid=%d ", xs->resid);
1285 if (xs->error == XS_SENSE)
1286 printf("sense=0x%02x\n", xs->sense.error_code);
1287 else
1288 printf("error=%d\n", xs->error);
1289 }
1290 #endif
1291
1292 /*
1293 * Remove the ACB from whatever queue it's on. We have to do a bit of
1294 * a hack to figure out which queue it's on. Note that it is *not*
1295 * necessary to cdr down the ready queue, but we must cdr down the
1296 * nexus queue and see if it's there, so we can mark the unit as no
1297 * longer busy. This code is sickening, but it works.
1298 */
1299 if (acb == sc->sc_nexus) {
1300 ti->lubusy &= ~(1 << sc_link->lun);
1301 sc->sc_state = AIC_IDLE;
1302 sc->sc_nexus = NULL;
1303 aic_sched(sc);
1304 } else
1305 aic_dequeue(sc, acb);
1306
1307 aic_free_acb(sc, acb, xs->flags);
1308 ti->cmds++;
1309 scsi_done(xs);
1310 }
1311
1312 void
1313 aic_dequeue(sc, acb)
1314 struct aic_softc *sc;
1315 struct aic_acb *acb;
1316 {
1317 struct scsi_link *sc_link = acb->xs->sc_link;
1318 struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->target];
1319
1320 if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
1321 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1322 } else {
1323 register struct aic_acb *acb2;
1324 for (acb2 = sc->nexus_list.tqh_first; acb2 != NULL;
1325 acb2 = acb2->chain.tqe_next) {
1326 if (acb2 == acb)
1327 break;
1328 }
1329 if (acb2 != NULL) {
1330 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1331 ti->lubusy &= ~(1 << sc_link->lun);
1332 } else if (acb->chain.tqe_next) {
1333 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1334 } else {
1335 printf("%s: can't find matching acb\n",
1336 sc->sc_dev.dv_xname);
1337 Debugger();
1338 }
1339 }
1340 }
1341
1342 /*
1344 * INTERRUPT/PROTOCOL ENGINE
1345 */
1346
1347 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
1348 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1349 #define ISEXTMSG(m) ((m) == 0x01)
1350
1351 /*
1352 * Precondition:
1353 * The SCSI bus is already in the MSGI phase and there is a message byte
1354 * on the bus, along with an asserted REQ signal.
1355 */
1356 int
1357 aic_msgin(sc)
1358 register struct aic_softc *sc;
1359 {
1360 u_char sstat1;
1361 int n;
1362
1363 AIC_TRACE(("aic_msgin "));
1364
1365 if (sc->sc_prevphase == PH_MSGIN) {
1366 /* This is a continuation of the previous message. */
1367 n = sc->sc_imp - sc->sc_imess;
1368 goto nextbyte;
1369 }
1370
1371 /* This is a new MESSAGE IN phase. Clean up our state. */
1372 sc->sc_flags &= ~AIC_DROP_MSGIN;
1373
1374 nextmsg:
1375 n = 0;
1376 sc->sc_imp = &sc->sc_imess[n];
1377
1378 nextbyte:
1379 /*
1380 * Read a whole message, but don't ack the last byte. If we reject the
1381 * message, we have to assert ATN during the message transfer phase
1382 * itself.
1383 */
1384 for (;;) {
1385 for (;;) {
1386 sstat1 = inb(SSTAT1);
1387 if ((sstat1 & (REQINIT|BUSFREE)) != 0)
1388 break;
1389 /* Wait for REQINIT. XXX Need timeout. */
1390 }
1391 if ((sstat1 & (PHASECHG|BUSFREE)) != 0) {
1392 /*
1393 * Target left MESSAGE IN, probably because it
1394 * a) noticed our ATN signal, or
1395 * b) ran out of messages.
1396 */
1397 return (1);
1398 }
1399
1400 /* If parity error, just dump everything on the floor. */
1401 if ((sstat1 & SCSIPERR) != 0) {
1402 aic_sched_msgout(SEND_PARITY_ERROR);
1403 sc->sc_flags |= AIC_DROP_MSGIN;
1404 }
1405
1406 /* Gather incoming message bytes if needed. */
1407 if ((sc->sc_flags & AIC_DROP_MSGIN) == 0) {
1408 if (n >= AIC_MAX_MSG_LEN) {
1409 (void) inb(SCSIDAT);
1410 aic_sched_msgout(SEND_REJECT);
1411 sc->sc_flags |= AIC_DROP_MSGIN;
1412 } else {
1413 *sc->sc_imp++ = inb(SCSIDAT);
1414 n++;
1415 /*
1416 * This testing is suboptimal, but most
1417 * messages will be of the one byte variety, so
1418 * it should not affect performance
1419 * significantly.
1420 */
1421 if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
1422 break;
1423 if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
1424 break;
1425 if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
1426 n == sc->sc_imess[1] + 2)
1427 break;
1428 }
1429 } else
1430 (void) inb(SCSIDAT);
1431
1432 /*
1433 * If we reach this spot we're either:
1434 * a) in the middle of a multi-byte message, or
1435 * b) dropping bytes.
1436 */
1437 outb(SXFRCTL0, CHEN|SPIOEN);
1438 /* Ack the last byte read. */
1439 (void) inb(SCSIDAT);
1440 outb(SXFRCTL0, CHEN);
1441 while ((inb(SCSISIG) & ACKI) != 0)
1442 ;
1443 }
1444
1445 AIC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1446
1447 /* We now have a complete message. Parse it. */
1448 switch (sc->sc_state) {
1449 struct aic_acb *acb;
1450 struct scsi_link *sc_link;
1451 struct aic_tinfo *ti;
1452
1453 case AIC_CONNECTED:
1454 AIC_ASSERT(sc->sc_nexus != NULL);
1455 acb = sc->sc_nexus;
1456 ti = &sc->sc_tinfo[acb->xs->sc_link->target];
1457
1458 switch (sc->sc_imess[0]) {
1459 case MSG_CMDCOMPLETE:
1460 if (sc->sc_dleft < 0) {
1461 sc_link = acb->xs->sc_link;
1462 printf("%s: %d extra bytes from %d:%d\n",
1463 sc->sc_dev.dv_xname, -sc->sc_dleft,
1464 sc_link->target, sc_link->lun);
1465 acb->data_length = 0;
1466 }
1467 acb->xs->resid = acb->data_length = sc->sc_dleft;
1468 sc->sc_state = AIC_CMDCOMPLETE;
1469 break;
1470
1471 case MSG_PARITY_ERROR:
1472 /* Resend the last message. */
1473 aic_sched_msgout(sc->sc_lastmsg);
1474 break;
1475
1476 case MSG_MESSAGE_REJECT:
1477 AIC_MISC(("message rejected %02x ", sc->sc_lastmsg));
1478 switch (sc->sc_lastmsg) {
1479 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
1480 case SEND_IDENTIFY:
1481 ti->flags &= ~(DO_SYNC|DO_WIDE);
1482 ti->period = ti->offset = 0;
1483 aic_setsync(sc, ti);
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 aic_setsync(sc, ti);
1492 break;
1493 #endif
1494 #if AIC_USE_WIDE
1495 case SEND_WDTR:
1496 ti->flags &= ~DO_WIDE;
1497 ti->width = 0;
1498 break;
1499 #endif
1500 case SEND_INIT_DET_ERR:
1501 sc->sc_flags |= AIC_ABORTING;
1502 aic_sched_msgout(SEND_ABORT);
1503 break;
1504 }
1505 break;
1506
1507 case MSG_NOOP:
1508 break;
1509
1510 case MSG_DISCONNECT:
1511 ti->dconns++;
1512 sc->sc_state = AIC_DISCONNECT;
1513 break;
1514
1515 case MSG_SAVEDATAPOINTER:
1516 acb->data_addr = sc->sc_dp;
1517 acb->data_length = sc->sc_dleft;
1518 break;
1519
1520 case MSG_RESTOREPOINTERS:
1521 sc->sc_dp = acb->data_addr;
1522 sc->sc_dleft = acb->data_length;
1523 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1524 sc->sc_cleft = acb->scsi_cmd_length;
1525 break;
1526
1527 case MSG_EXTENDED:
1528 switch (sc->sc_imess[2]) {
1529 #if AIC_USE_SYNCHRONOUS
1530 case MSG_EXT_SDTR:
1531 if (sc->sc_imess[1] != 3)
1532 goto reject;
1533 ti->period = sc->sc_imess[3];
1534 ti->offset = sc->sc_imess[4];
1535 ti->flags &= ~DO_SYNC;
1536 if (ti->offset == 0) {
1537 } else if (ti->period < sc->sc_minsync ||
1538 ti->period > sc->sc_maxsync ||
1539 ti->offset > 8) {
1540 ti->period = ti->offset = 0;
1541 aic_sched_msgout(SEND_SDTR);
1542 } else {
1543 sc_print_addr(acb->xs->sc_link);
1544 printf("sync, offset %d, period %dnsec\n",
1545 ti->offset, ti->period * 4);
1546 }
1547 aic_setsync(sc, ti);
1548 break;
1549 #endif
1550
1551 #if AIC_USE_WIDE
1552 case MSG_EXT_WDTR:
1553 if (sc->sc_imess[1] != 2)
1554 goto reject;
1555 ti->width = sc->sc_imess[3];
1556 ti->flags &= ~DO_WIDE;
1557 if (ti->width == 0) {
1558 } else if (ti->width > AIC_MAX_WIDTH) {
1559 ti->width = 0;
1560 aic_sched_msgout(SEND_WDTR);
1561 } else {
1562 sc_print_addr(acb->xs->sc_link);
1563 printf("wide, width %d\n",
1564 1 << (3 + ti->width));
1565 }
1566 break;
1567 #endif
1568
1569 default:
1570 printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
1571 sc->sc_dev.dv_xname);
1572 AIC_BREAK();
1573 goto reject;
1574 }
1575 break;
1576
1577 default:
1578 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1579 sc->sc_dev.dv_xname);
1580 AIC_BREAK();
1581 reject:
1582 aic_sched_msgout(SEND_REJECT);
1583 break;
1584 }
1585 break;
1586
1587 case AIC_RESELECTED:
1588 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1589 printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
1590 sc->sc_dev.dv_xname);
1591 AIC_BREAK();
1592 goto reset;
1593 }
1594
1595 (void) aic_reselect(sc, sc->sc_imess[0]);
1596 break;
1597
1598 default:
1599 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1600 sc->sc_dev.dv_xname);
1601 AIC_BREAK();
1602 reset:
1603 sc->sc_flags |= AIC_ABORTING;
1604 aic_sched_msgout(SEND_DEV_RESET);
1605 break;
1606
1607 abort:
1608 sc->sc_flags |= AIC_ABORTING;
1609 aic_sched_msgout(SEND_ABORT);
1610 break;
1611 }
1612
1613 outb(SXFRCTL0, CHEN|SPIOEN);
1614 /* Ack the last message byte. */
1615 (void) inb(SCSIDAT);
1616 outb(SXFRCTL0, CHEN);
1617 while ((inb(SCSISIG) & ACKI) != 0)
1618 ;
1619
1620 /* Go get the next message, if any. */
1621 goto nextmsg;
1622
1623 out:
1624 AIC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1625 return (0);
1626 }
1627
1628 /*
1629 * Send the highest priority, scheduled message.
1630 */
1631 void
1632 aic_msgout(sc)
1633 register struct aic_softc *sc;
1634 {
1635 struct aic_acb *acb;
1636 struct aic_tinfo *ti;
1637 u_char sstat1;
1638 int n;
1639
1640 AIC_TRACE(("aic_msgout "));
1641
1642 /*
1643 * Set ATN. If we're just sending a trivial 1-byte message, we'll
1644 * clear ATN later on anyway.
1645 */
1646 outb(SCSISIG, PH_MSGOUT|ATNO);
1647 /* Reset the FIFO. */
1648 outb(DMACNTRL0, RSTFIFO);
1649 /* Enable REQ/ACK protocol. */
1650 outb(SXFRCTL0, CHEN|SPIOEN);
1651
1652 if (sc->sc_prevphase == PH_MSGOUT) {
1653 if (sc->sc_omp == sc->sc_omess) {
1654 /*
1655 * This is a retransmission.
1656 *
1657 * We get here if the target stayed in MESSAGE OUT
1658 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1659 * that all of the previously transmitted messages must
1660 * be sent again, in the same order. Therefore, we
1661 * requeue all the previously transmitted messages, and
1662 * start again from the top. Our simple priority
1663 * scheme keeps the messages in the right order.
1664 */
1665 AIC_MISC(("retransmitting "));
1666 sc->sc_msgpriq |= sc->sc_msgoutq;
1667 } else {
1668 /* This is a continuation of the previous message. */
1669 n = sc->sc_omp - sc->sc_omess;
1670 goto nextbyte;
1671 }
1672 }
1673
1674 /* No messages transmitted so far. */
1675 sc->sc_msgoutq = 0;
1676 sc->sc_lastmsg = 0;
1677
1678 nextmsg:
1679 /* Pick up highest priority message. */
1680 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1681 sc->sc_msgpriq &= ~sc->sc_currmsg;
1682 sc->sc_msgoutq |= sc->sc_currmsg;
1683
1684 /* Build the outgoing message data. */
1685 switch (sc->sc_currmsg) {
1686 case SEND_IDENTIFY:
1687 if (sc->sc_state != AIC_CONNECTED) {
1688 printf("%s: SEND_IDENTIFY while not connected; sending NOOP\n",
1689 sc->sc_dev.dv_xname);
1690 AIC_BREAK();
1691 goto noop;
1692 }
1693 AIC_ASSERT(sc->sc_nexus != NULL);
1694 acb = sc->sc_nexus;
1695 sc->sc_omess[0] = MSG_IDENTIFY(acb->xs->sc_link->lun, 1);
1696 n = 1;
1697 break;
1698
1699 #if AIC_USE_SYNCHRONOUS
1700 case SEND_SDTR:
1701 if (sc->sc_state != AIC_CONNECTED) {
1702 printf("%s: SEND_SDTR while not connected; sending NOOP\n",
1703 sc->sc_dev.dv_xname);
1704 AIC_BREAK();
1705 goto noop;
1706 }
1707 AIC_ASSERT(sc->sc_nexus != NULL);
1708 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
1709 sc->sc_omess[4] = MSG_EXTENDED;
1710 sc->sc_omess[3] = 3;
1711 sc->sc_omess[2] = MSG_EXT_SDTR;
1712 sc->sc_omess[1] = ti->period >> 2;
1713 sc->sc_omess[0] = ti->offset;
1714 n = 5;
1715 break;
1716 #endif
1717
1718 #if AIC_USE_WIDE
1719 case SEND_WDTR:
1720 if (sc->sc_state != AIC_CONNECTED) {
1721 printf("%s: SEND_WDTR while not connected; sending NOOP\n",
1722 sc->sc_dev.dv_xname);
1723 AIC_BREAK();
1724 goto noop;
1725 }
1726 AIC_ASSERT(sc->sc_nexus != NULL);
1727 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
1728 sc->sc_omess[3] = MSG_EXTENDED;
1729 sc->sc_omess[2] = 2;
1730 sc->sc_omess[1] = MSG_EXT_WDTR;
1731 sc->sc_omess[0] = ti->width;
1732 n = 4;
1733 break;
1734 #endif
1735
1736 case SEND_DEV_RESET:
1737 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1738 n = 1;
1739 break;
1740
1741 case SEND_REJECT:
1742 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1743 n = 1;
1744 break;
1745
1746 case SEND_PARITY_ERROR:
1747 sc->sc_omess[0] = MSG_PARITY_ERROR;
1748 n = 1;
1749 break;
1750
1751 case SEND_INIT_DET_ERR:
1752 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1753 n = 1;
1754 break;
1755
1756 case SEND_ABORT:
1757 sc->sc_omess[0] = MSG_ABORT;
1758 n = 1;
1759 break;
1760
1761 case 0:
1762 #ifdef AIC_PICKY
1763 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1764 sc->sc_dev.dv_xname);
1765 AIC_BREAK();
1766 #endif
1767 noop:
1768 sc->sc_omess[0] = MSG_NOOP;
1769 n = 1;
1770 break;
1771
1772 default:
1773 printf("%s: weird MESSAGE OUT; sending NOOP\n",
1774 sc->sc_dev.dv_xname);
1775 AIC_BREAK();
1776 goto noop;
1777 }
1778 sc->sc_omp = &sc->sc_omess[n];
1779
1780 nextbyte:
1781 /* Send message bytes. */
1782 for (;;) {
1783 for (;;) {
1784 sstat1 = inb(SSTAT1);
1785 if ((sstat1 & (REQINIT|BUSFREE)) != 0)
1786 break;
1787 /* Wait for REQINIT. XXX Need timeout. */
1788 }
1789 if ((sstat1 & (PHASECHG|BUSFREE)) != 0) {
1790 /*
1791 * Target left MESSAGE OUT, possibly to reject
1792 * our message.
1793 */
1794 goto out;
1795 }
1796
1797 /* Clear ATN before last byte if this is the last message. */
1798 if (n == 1 && sc->sc_msgpriq == 0)
1799 outb(CLRSINT1, CLRATNO);
1800 /* Send message byte. */
1801 outb(SCSIDAT, *--sc->sc_omp);
1802 --n;
1803 /* Keep track of the last message we've sent any bytes of. */
1804 sc->sc_lastmsg = sc->sc_currmsg;
1805 /* Wait for ACK to be negated. XXX Need timeout. */
1806 while ((inb(SCSISIG) & ACKI) != 0)
1807 ;
1808
1809 if (n == 0)
1810 break;
1811 }
1812
1813 /* We get here only if the entire message has been transmitted. */
1814 if (sc->sc_msgpriq != 0) {
1815 /* There are more outgoing messages. */
1816 goto nextmsg;
1817 }
1818
1819 /*
1820 * The last message has been transmitted. We need to remember the last
1821 * message transmitted (in case the target switches to MESSAGE IN phase
1822 * and sends a MESSAGE REJECT), and the list of messages transmitted
1823 * this time around (in case the target stays in MESSAGE OUT phase to
1824 * request a retransmit).
1825 */
1826
1827 out:
1828 /* Disable REQ/ACK protocol. */
1829 outb(SXFRCTL0, CHEN);
1830 }
1831
1832 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the aic6360
1834 * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
1835 * and ACK deasserted (i.e. waiting for a data byte)
1836 * This new revision has been optimized (I tried) to make the common case fast,
1837 * and the rarer cases (as a result) somewhat more comlex
1838 */
1839 int
1840 aic_dataout_pio(sc, p, n)
1841 register struct aic_softc *sc;
1842 u_char *p;
1843 int n;
1844 {
1845 register u_char dmastat;
1846 int out = 0;
1847 #define DOUTAMOUNT 128 /* Full FIFO */
1848
1849 /* Clear host FIFO and counter. */
1850 outb(DMACNTRL0, RSTFIFO|WRITE);
1851 /* Enable FIFOs. */
1852 outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
1853 outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
1854
1855 /* Turn off ENREQINIT for now. */
1856 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENPHASECHG);
1857
1858 /* I have tried to make the main loop as tight as possible. This
1859 * means that some of the code following the loop is a bit more
1860 * complex than otherwise.
1861 */
1862 while (n > 0) {
1863 int xfer;
1864
1865 for (;;) {
1866 dmastat = inb(DMASTAT);
1867 if ((dmastat & DFIFOEMP) != 0)
1868 break;
1869 if ((dmastat & INTSTAT) != 0)
1870 goto phasechange;
1871 }
1872
1873 xfer = min(DOUTAMOUNT, n);
1874
1875 AIC_MISC(("%d> ", xfer));
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 if (out == 0) {
1903 outb(SXFRCTL1, BITBUCKET);
1904 for (;;) {
1905 if ((inb(DMASTAT) & INTSTAT) != 0)
1906 break;
1907 }
1908 outb(SXFRCTL1, 0);
1909 AIC_MISC(("extra data "));
1910 } else {
1911 /* See the bytes off chip */
1912 for (;;) {
1913 dmastat = inb(DMASTAT);
1914 if ((dmastat & DFIFOEMP) != 0 &&
1915 (inb(SSTAT2) & SEMPTY) != 0)
1916 break;
1917 if ((dmastat & INTSTAT) != 0)
1918 goto phasechange;
1919 }
1920 }
1921
1922 phasechange:
1923 /* Stop the FIFO data path. */
1924 outb(SXFRCTL0, CHEN);
1925 while ((inb(SXFRCTL0) & SCSIEN) != 0)
1926 ;
1927
1928 if ((dmastat & INTSTAT) != 0) {
1929 /* Some sort of phase change. */
1930 int amount;
1931
1932 /* Stop transfers, do some accounting */
1933 amount = inb(FIFOSTAT) + inb(SSTAT2) & 15;
1934 if (amount > 0) {
1935 out -= amount;
1936 outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH);
1937 AIC_MISC(("+%d ", amount));
1938 }
1939 }
1940
1941 /* Turn on ENREQINIT again. */
1942 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
1943
1944 return out;
1945 }
1946
1947 /* aic_datain_pio: perform data transfers using the FIFO datapath in the aic6360
1949 * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
1950 * and ACK deasserted (i.e. at least one byte is ready).
1951 * For now, uses a pretty dumb algorithm, hangs around until all data has been
1952 * transferred. This, is OK for fast targets, but not so smart for slow
1953 * targets which don't disconnect or for huge transfers.
1954 */
1955 int
1956 aic_datain_pio(sc, p, n)
1957 register struct aic_softc *sc;
1958 u_char *p;
1959 int n;
1960 {
1961 register u_char dmastat;
1962 int in = 0;
1963 #define DINAMOUNT 128 /* Full FIFO */
1964
1965 /* Clear host FIFO and counter. */
1966 outb(DMACNTRL0, RSTFIFO);
1967 /* Enable FIFOs */
1968 outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
1969 outb(DMACNTRL0, ENDMA|DWORDPIO);
1970
1971 /* Turn off ENREQINIT for now. */
1972 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENPHASECHG);
1973
1974 /* We leave this loop if one or more of the following is true:
1975 * a) phase != PH_DATAIN && FIFOs are empty
1976 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
1977 */
1978 while (n > 0) {
1979 int xfer;
1980
1981 /* Wait for fifo half full or phase mismatch */
1982 for (;;) {
1983 dmastat = inb(DMASTAT);
1984 if ((dmastat & (DFIFOFULL|INTSTAT)) != 0)
1985 break;
1986 }
1987
1988 if ((dmastat & DFIFOFULL) != 0)
1989 xfer = min(DINAMOUNT, n);
1990 else
1991 xfer = min(inb(FIFOSTAT), n);
1992
1993 AIC_MISC((">%d ", xfer));
1994
1995 n -= xfer;
1996 in += xfer;
1997
1998 #if AIC_USE_DWORDS
1999 if (xfer >= 12) {
2000 insl(DMADATALONG, p, xfer>>2);
2001 p += xfer & ~3;
2002 xfer &= 3;
2003 }
2004 #else
2005 if (xfer >= 8) {
2006 insw(DMADATA, p, xfer>>1);
2007 p += xfer & ~1;
2008 xfer &= 1;
2009 }
2010 #endif
2011
2012 if (xfer > 0) {
2013 outb(DMACNTRL0, ENDMA|B8MODE);
2014 insb(DMADATA, p, xfer);
2015 p += xfer;
2016 outb(DMACNTRL0, ENDMA|DWORDPIO);
2017 }
2018
2019 if ((dmastat & INTSTAT) != 0)
2020 goto phasechange;
2021 }
2022
2023 /* Some SCSI-devices are rude enough to transfer more data than what
2024 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
2025 * requested 512. Test for progress, i.e. real transfers. If no real
2026 * transfers have been performed (n is probably already zero) and the
2027 * FIFO is not empty, waste some bytes....
2028 */
2029 if (in == 0) {
2030 outb(SXFRCTL1, BITBUCKET);
2031 for (;;) {
2032 if ((inb(DMASTAT) & INTSTAT) != 0)
2033 break;
2034 }
2035 outb(SXFRCTL1, 0);
2036 AIC_MISC(("extra data "));
2037 }
2038
2039 phasechange:
2040 /* Stop the FIFO data path. */
2041 outb(SXFRCTL0, CHEN);
2042 while ((inb(SXFRCTL0) & SCSIEN) != 0)
2043 ;
2044
2045 /* Turn on ENREQINIT again. */
2046 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
2047
2048 return in;
2049 }
2050
2051 /*
2053 * This is the workhorse routine of the driver.
2054 * Deficiencies (for now):
2055 * 1) always uses programmed I/O
2056 */
2057 int
2058 aicintr(arg)
2059 void *arg;
2060 {
2061 register struct aic_softc *sc = arg;
2062 u_char sstat0, sstat1;
2063 register struct aic_acb *acb;
2064 register struct scsi_link *sc_link;
2065 struct aic_tinfo *ti;
2066 int n;
2067
2068 /*
2069 * Clear INTEN. We enable it again before returning. This makes the
2070 * interrupt esssentially level-triggered.
2071 */
2072 outb(DMACNTRL0, 0);
2073
2074 AIC_TRACE(("aicintr "));
2075
2076 loop:
2077 gotintr:
2078 /*
2079 * First check for abnormal conditions, such as reset.
2080 */
2081 sstat1 = inb(SSTAT1);
2082 AIC_MISC(("sstat1:0x%02x ", sstat1));
2083
2084 if ((sstat1 & SCSIRSTI) != 0) {
2085 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
2086 goto reset;
2087 }
2088
2089 /*
2090 * Check for less serious errors.
2091 */
2092 if ((sstat1 & SCSIPERR) != 0) {
2093 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
2094 outb(CLRSINT1, CLRSCSIPERR);
2095 if (sc->sc_prevphase == PH_MSGIN) {
2096 aic_sched_msgout(SEND_PARITY_ERROR);
2097 sc->sc_flags |= AIC_DROP_MSGIN;
2098 } else
2099 aic_sched_msgout(SEND_INIT_DET_ERR);
2100 }
2101
2102 /*
2103 * If we're not already busy doing something test for the following
2104 * conditions:
2105 * 1) We have been reselected by something
2106 * 2) We have selected something successfully
2107 * 3) Our selection process has timed out
2108 * 4) This is really a bus free interrupt just to get a new command
2109 * going?
2110 * 5) Spurious interrupt?
2111 */
2112 switch (sc->sc_state) {
2113 case AIC_IDLE:
2114 case AIC_SELECTING:
2115 sstat0 = inb(SSTAT0);
2116 AIC_MISC(("sstat0:0x%02x ", sstat0));
2117
2118 if ((sstat0 & TARGET) != 0) {
2119 /*
2120 * We don't currently support target mode.
2121 */
2122 printf("%s: target mode selected; going to bus free\n",
2123 sc->sc_dev.dv_xname);
2124 outb(SCSISIG, 0);
2125
2126 sc->sc_state = AIC_IDLE;
2127 aic_sched(sc);
2128 goto out;
2129 } else if ((sstat0 & SELDI) != 0) {
2130 AIC_MISC(("reselected "));
2131
2132 /*
2133 * If we're trying to select a target ourselves,
2134 * push our command back into the ready list.
2135 */
2136 if (sc->sc_state == AIC_SELECTING) {
2137 AIC_MISC(("backoff selector "));
2138 AIC_ASSERT(sc->sc_nexus != NULL);
2139 acb = sc->sc_nexus;
2140 sc->sc_nexus = NULL;
2141 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2142 }
2143
2144 /* Save reselection ID. */
2145 sc->sc_selid = inb(SELID);
2146
2147 sc->sc_state = AIC_RESELECTED;
2148 } else if ((sstat0 & SELDO) != 0) {
2149 AIC_MISC(("selected "));
2150
2151 /* We have selected a target. Things to do:
2152 * a) Determine what message(s) to send.
2153 * b) Verify that we're still selecting the target.
2154 * c) Mark device as busy.
2155 */
2156 if (sc->sc_state != AIC_SELECTING) {
2157 printf("%s: selection out while idle; resetting\n",
2158 sc->sc_dev.dv_xname);
2159 AIC_BREAK();
2160 goto reset;
2161 }
2162 AIC_ASSERT(sc->sc_nexus != NULL);
2163 acb = sc->sc_nexus;
2164
2165 sc_link = acb->xs->sc_link;
2166 ti = &sc->sc_tinfo[sc_link->target];
2167 if ((acb->xs->flags & SCSI_RESET) == 0) {
2168 sc->sc_msgpriq = SEND_IDENTIFY;
2169 if (acb->flags != ACB_ABORTED) {
2170 #if AIC_USE_SYNCHRONOUS
2171 if ((ti->flags & DO_SYNC) != 0)
2172 sc->sc_msgpriq |= SEND_SDTR;
2173 #endif
2174 #if AIC_USE_WIDE
2175 if ((ti->flags & DO_WIDE) != 0)
2176 sc->sc_msgpriq |= SEND_WDTR;
2177 #endif
2178 } else {
2179 sc->sc_flags |= AIC_ABORTING;
2180 sc->sc_msgpriq |= SEND_ABORT;
2181 }
2182 } else
2183 sc->sc_msgpriq = SEND_DEV_RESET;
2184
2185 ti->lubusy |= (1 << sc_link->lun);
2186
2187 /* Do an implicit RESTORE POINTERS. */
2188 sc->sc_dp = acb->data_addr;
2189 sc->sc_dleft = acb->data_length;
2190 sc->sc_cp = (u_char *)&acb->scsi_cmd;
2191 sc->sc_cleft = acb->scsi_cmd_length;
2192
2193 sc->sc_state = AIC_CONNECTED;
2194 } else if ((sstat1 & SELTO) != 0) {
2195 AIC_MISC(("selection timeout "));
2196
2197 if (sc->sc_state != AIC_SELECTING) {
2198 printf("%s: selection timeout while idle; resetting\n",
2199 sc->sc_dev.dv_xname);
2200 AIC_BREAK();
2201 goto reset;
2202 }
2203 AIC_ASSERT(sc->sc_nexus != NULL);
2204 acb = sc->sc_nexus;
2205
2206 outb(SXFRCTL1, 0);
2207 outb(SCSISEQ, ENRESELI);
2208 outb(CLRSINT1, CLRSELTIMO);
2209
2210 acb->xs->error = XS_SELTIMEOUT;
2211 untimeout(aic_timeout, acb);
2212 delay(250);
2213 aic_done(sc, acb);
2214 goto out;
2215 } else {
2216 #ifdef AIC_PICKY
2217 if (sc->sc_state != AIC_IDLE) {
2218 printf("%s: BUS FREE while not idle; state=%d\n",
2219 sc->sc_dev.dv_xname, sc->sc_state);
2220 AIC_BREAK();
2221 goto out;
2222 }
2223 #endif
2224
2225 aic_sched(sc);
2226 goto out;
2227 }
2228
2229 /*
2230 * Turn off selection stuff, and prepare to catch bus free
2231 * interrupts, parity errors, and phase changes.
2232 */
2233 outb(SXFRCTL1, 0);
2234 outb(SCSISEQ, ENAUTOATNP);
2235 outb(CLRSINT0, CLRSELDI|CLRSELDO);
2236 outb(CLRSINT1, CLRBUSFREE|CLRPHASECHG);
2237 outb(SIMODE0, 0);
2238 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
2239
2240 sc->sc_flags = 0;
2241 sc->sc_prevphase = PH_INVALID;
2242 goto dophase;
2243 }
2244
2245 outb(CLRSINT1, CLRPHASECHG);
2246
2247 if ((sstat1 & BUSFREE) != 0) {
2248 /* We've gone to BUS FREE phase. */
2249 outb(CLRSINT1, CLRBUSFREE);
2250
2251 switch (sc->sc_state) {
2252 case AIC_RESELECTED:
2253 sc->sc_state = AIC_IDLE;
2254 aic_sched(sc);
2255 break;
2256
2257 case AIC_CONNECTED:
2258 if ((sc->sc_flags & AIC_ABORTING) == 0) {
2259 printf("%s: unexpected BUS FREE; aborting\n",
2260 sc->sc_dev.dv_xname);
2261 AIC_BREAK();
2262 }
2263 AIC_ASSERT(sc->sc_nexus != NULL);
2264 acb = sc->sc_nexus;
2265 acb->xs->error = XS_DRIVER_STUFFUP;
2266 goto finish;
2267
2268 case AIC_DISCONNECT:
2269 AIC_ASSERT(sc->sc_nexus != NULL);
2270 acb = sc->sc_nexus;
2271 sc->sc_state = AIC_IDLE;
2272 sc->sc_nexus = NULL;
2273 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
2274 aic_sched(sc);
2275 break;
2276
2277 case AIC_CMDCOMPLETE:
2278 AIC_ASSERT(sc->sc_nexus != NULL);
2279 acb = sc->sc_nexus;
2280 finish:
2281 untimeout(aic_timeout, acb);
2282 aic_done(sc, acb);
2283 break;
2284 }
2285 goto out;
2286 }
2287
2288 dophase:
2289 if ((sstat1 & REQINIT) == 0) {
2290 /* Wait for REQINIT. */
2291 goto out;
2292 }
2293
2294 sc->sc_phase = inb(SCSISIG) & PH_MASK;
2295 outb(SCSISIG, sc->sc_phase);
2296
2297 switch (sc->sc_phase) {
2298 case PH_MSGOUT:
2299 /* If aborting, always handle MESSAGE OUT. */
2300 if ((sc->sc_state & AIC_CONNECTED) == 0 &&
2301 (sc->sc_flags & AIC_ABORTING) == 0)
2302 break;
2303 aic_msgout(sc);
2304 sc->sc_prevphase = PH_MSGOUT;
2305 goto loop;
2306
2307 case PH_MSGIN:
2308 if ((sc->sc_state & (AIC_CONNECTED|AIC_RESELECTED)) == 0)
2309 break;
2310 if (aic_msgin(sc)) {
2311 sc->sc_prevphase = PH_MSGIN;
2312 goto gotintr;
2313 }
2314 sc->sc_prevphase = PH_MSGIN;
2315 goto loop;
2316
2317 case PH_CMD:
2318 if ((sc->sc_state & AIC_CONNECTED) == 0)
2319 break;
2320 #if AIC_DEBUG
2321 if ((aic_debug & AIC_SHOWMISC) != 0) {
2322 AIC_ASSERT(sc->sc_nexus != NULL);
2323 acb = sc->sc_nexus;
2324 printf("cmd=0x%02x+%d ",
2325 acb->scsi_cmd.opcode, acb->scsi_cmd_length-1);
2326 }
2327 #endif
2328 n = aic_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
2329 sc->sc_cp += n;
2330 sc->sc_cleft -= n;
2331 sc->sc_prevphase = PH_CMD;
2332 goto loop;
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 sc->sc_prevphase = PH_DATAOUT;
2342 goto loop;
2343
2344 case PH_DATAIN:
2345 if ((sc->sc_state & AIC_CONNECTED) == 0)
2346 break;
2347 AIC_MISC(("datain "));
2348 n = aic_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
2349 sc->sc_dp += n;
2350 sc->sc_dleft -= n;
2351 sc->sc_prevphase = PH_DATAIN;
2352 goto loop;
2353
2354 case PH_STAT:
2355 if ((sc->sc_state & AIC_CONNECTED) == 0)
2356 break;
2357 AIC_ASSERT(sc->sc_nexus != NULL);
2358 acb = sc->sc_nexus;
2359 /* XXXX Don't clear FIFO. Wait for byte to come in. */
2360 outb(SXFRCTL0, CHEN|SPIOEN);
2361 outb(DMACNTRL0, RSTFIFO);
2362 acb->target_stat = inb(SCSIDAT);
2363 outb(SXFRCTL0, CHEN);
2364 outb(DMACNTRL0, RSTFIFO);
2365 while ((inb(SXFRCTL0) & SCSIEN) != 0)
2366 ;
2367 AIC_MISC(("target_stat=0x%02x ", acb->target_stat));
2368 sc->sc_prevphase = PH_STAT;
2369 goto loop;
2370 }
2371
2372 printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
2373 AIC_BREAK();
2374 reset:
2375 aic_init(sc);
2376 return 1;
2377
2378 out:
2379 outb(DMACNTRL0, INTEN);
2380 return 1;
2381 }
2382
2383 void
2384 aic_abort(sc, acb)
2385 struct aic_softc *sc;
2386 struct aic_acb *acb;
2387 {
2388
2389 if (sc->sc_nexus == acb) {
2390 if (sc->sc_state == AIC_CONNECTED) {
2391 sc->sc_flags |= AIC_ABORTING;
2392 aic_sched_msgout(SEND_ABORT);
2393 }
2394 } else {
2395 aic_dequeue(sc, acb);
2396 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2397 if (sc->sc_state == AIC_IDLE)
2398 aic_sched(sc);
2399 }
2400 }
2401
2402 void
2403 aic_timeout(arg)
2404 void *arg;
2405 {
2406 struct aic_acb *acb = arg;
2407 struct scsi_xfer *xs = acb->xs;
2408 struct scsi_link *sc_link = xs->sc_link;
2409 struct aic_softc *sc = sc_link->adapter_softc;
2410 int s;
2411
2412 sc_print_addr(sc_link);
2413 printf("timed out");
2414
2415 s = splbio();
2416
2417 if (acb->flags == ACB_ABORTED) {
2418 /* abort timed out */
2419 printf(" AGAIN\n");
2420 acb->xs->retries = 0;
2421 aic_done(sc, acb);
2422 } else {
2423 /* abort the operation that has timed out */
2424 printf("\n");
2425 acb->xs->error = XS_TIMEOUT;
2426 acb->flags = ACB_ABORTED;
2427 aic_abort(sc, acb);
2428 /* 2 secs for the abort */
2429 if ((xs->flags & SCSI_POLL) == 0)
2430 timeout(aic_timeout, acb, 2 * hz);
2431 }
2432
2433 splx(s);
2434 }
2435
2436 #ifdef AIC_DEBUG
2438 /*
2439 * The following functions are mostly used for debugging purposes, either
2440 * directly called from the driver or from the kernel debugger.
2441 */
2442
2443 void
2444 aic_show_scsi_cmd(acb)
2445 struct aic_acb *acb;
2446 {
2447 u_char *b = (u_char *)&acb->scsi_cmd;
2448 struct scsi_link *sc_link = acb->xs->sc_link;
2449 int i;
2450
2451 sc_print_addr(sc_link);
2452 if ((acb->xs->flags & SCSI_RESET) == 0) {
2453 for (i = 0; i < acb->scsi_cmd_length; i++) {
2454 if (i)
2455 printf(",");
2456 printf("%x", b[i]);
2457 }
2458 printf("\n");
2459 } else
2460 printf("RESET\n");
2461 }
2462
2463 void
2464 aic_print_acb(acb)
2465 struct aic_acb *acb;
2466 {
2467
2468 printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
2469 printf(" dp=%x dleft=%d target_stat=%x\n",
2470 (long)acb->data_addr, acb->data_length, acb->target_stat);
2471 aic_show_scsi_cmd(acb);
2472 }
2473
2474 void
2475 aic_print_active_acb()
2476 {
2477 struct aic_acb *acb;
2478 struct aic_softc *sc = aiccd.cd_devs[0];
2479
2480 printf("ready list:\n");
2481 for (acb = sc->ready_list.tqh_first; acb != NULL;
2482 acb = acb->chain.tqe_next)
2483 aic_print_acb(acb);
2484 printf("nexus:\n");
2485 if (sc->sc_nexus != NULL)
2486 aic_print_acb(sc->sc_nexus);
2487 printf("nexus list:\n");
2488 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2489 acb = acb->chain.tqe_next)
2490 aic_print_acb(acb);
2491 }
2492
2493 void
2494 aic_dump6360(sc)
2495 struct aic_softc *sc;
2496 {
2497
2498 printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIG=%x\n",
2499 inb(SCSISEQ), inb(SXFRCTL0), inb(SXFRCTL1), inb(SCSISIG));
2500 printf(" SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
2501 inb(SSTAT0), inb(SSTAT1), inb(SSTAT2), inb(SSTAT3), inb(SSTAT4));
2502 printf(" SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n",
2503 inb(SIMODE0), inb(SIMODE1), inb(DMACNTRL0), inb(DMACNTRL1),
2504 inb(DMASTAT));
2505 printf(" FIFOSTAT=%d SCSIBUS=0x%x\n",
2506 inb(FIFOSTAT), inb(SCSIBUS));
2507 }
2508
2509 void
2510 aic_dump_driver(sc)
2511 struct aic_softc *sc;
2512 {
2513 struct aic_tinfo *ti;
2514 int i;
2515
2516 printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2517 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2518 sc->sc_state, sc->sc_imess[0],
2519 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2520 for (i = 0; i < 7; i++) {
2521 ti = &sc->sc_tinfo[i];
2522 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2523 i, ti->cmds, ti->dconns, ti->touts);
2524 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2525 }
2526 }
2527 #endif
2528