aic6360.c revision 1.33 1 /* $NetBSD: aic6360.c,v 1.33 1995/08/12 20:31:24 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 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 minphys(bp);
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_lastmsg);
1475 break;
1476
1477 case MSG_MESSAGE_REJECT:
1478 AIC_MISC(("message rejected %02x ", sc->sc_lastmsg));
1479 switch (sc->sc_lastmsg) {
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 aic_setsync(sc, ti);
1485 ti->width = 0;
1486 break;
1487 #endif
1488 #if AIC_USE_SYNCHRONOUS
1489 case SEND_SDTR:
1490 ti->flags &= ~DO_SYNC;
1491 ti->period = ti->offset = 0;
1492 aic_setsync(sc, ti);
1493 break;
1494 #endif
1495 #if AIC_USE_WIDE
1496 case SEND_WDTR:
1497 ti->flags &= ~DO_WIDE;
1498 ti->width = 0;
1499 break;
1500 #endif
1501 case SEND_INIT_DET_ERR:
1502 sc->sc_flags |= AIC_ABORTING;
1503 aic_sched_msgout(SEND_ABORT);
1504 break;
1505 }
1506 break;
1507
1508 case MSG_NOOP:
1509 break;
1510
1511 case MSG_DISCONNECT:
1512 ti->dconns++;
1513 sc->sc_state = AIC_DISCONNECT;
1514 break;
1515
1516 case MSG_SAVEDATAPOINTER:
1517 acb->data_addr = sc->sc_dp;
1518 acb->data_length = sc->sc_dleft;
1519 break;
1520
1521 case MSG_RESTOREPOINTERS:
1522 sc->sc_dp = acb->data_addr;
1523 sc->sc_dleft = acb->data_length;
1524 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1525 sc->sc_cleft = acb->scsi_cmd_length;
1526 break;
1527
1528 case MSG_EXTENDED:
1529 switch (sc->sc_imess[2]) {
1530 #if AIC_USE_SYNCHRONOUS
1531 case MSG_EXT_SDTR:
1532 if (sc->sc_imess[1] != 3)
1533 goto reject;
1534 ti->period = sc->sc_imess[3];
1535 ti->offset = sc->sc_imess[4];
1536 ti->flags &= ~DO_SYNC;
1537 if (ti->offset == 0) {
1538 } else if (ti->period < sc->sc_minsync ||
1539 ti->period > sc->sc_maxsync ||
1540 ti->offset > 8) {
1541 ti->period = ti->offset = 0;
1542 aic_sched_msgout(SEND_SDTR);
1543 } else {
1544 sc_print_addr(acb->xs->sc_link);
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 if (ti->width == 0) {
1559 } else if (ti->width > AIC_MAX_WIDTH) {
1560 ti->width = 0;
1561 aic_sched_msgout(SEND_WDTR);
1562 } else {
1563 sc_print_addr(acb->xs->sc_link);
1564 printf("wide, width %d\n",
1565 1 << (3 + ti->width));
1566 }
1567 break;
1568 #endif
1569
1570 default:
1571 printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
1572 sc->sc_dev.dv_xname);
1573 AIC_BREAK();
1574 goto reject;
1575 }
1576 break;
1577
1578 default:
1579 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1580 sc->sc_dev.dv_xname);
1581 AIC_BREAK();
1582 reject:
1583 aic_sched_msgout(SEND_REJECT);
1584 break;
1585 }
1586 break;
1587
1588 case AIC_RESELECTED:
1589 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1590 printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
1591 sc->sc_dev.dv_xname);
1592 AIC_BREAK();
1593 goto reset;
1594 }
1595
1596 (void) aic_reselect(sc, sc->sc_imess[0]);
1597 break;
1598
1599 default:
1600 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1601 sc->sc_dev.dv_xname);
1602 AIC_BREAK();
1603 reset:
1604 sc->sc_flags |= AIC_ABORTING;
1605 aic_sched_msgout(SEND_DEV_RESET);
1606 break;
1607
1608 abort:
1609 sc->sc_flags |= AIC_ABORTING;
1610 aic_sched_msgout(SEND_ABORT);
1611 break;
1612 }
1613
1614 outb(SXFRCTL0, CHEN|SPIOEN);
1615 /* Ack the last message byte. */
1616 (void) inb(SCSIDAT);
1617 outb(SXFRCTL0, CHEN);
1618 while ((inb(SCSISIG) & ACKI) != 0)
1619 ;
1620
1621 /* Go get the next message, if any. */
1622 goto nextmsg;
1623
1624 out:
1625 AIC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1626 return (0);
1627 }
1628
1629 /*
1630 * Send the highest priority, scheduled message.
1631 */
1632 void
1633 aic_msgout(sc)
1634 register struct aic_softc *sc;
1635 {
1636 struct aic_acb *acb;
1637 struct aic_tinfo *ti;
1638 u_char sstat1;
1639 int n;
1640
1641 AIC_TRACE(("aic_msgout "));
1642
1643 /*
1644 * Set ATN. If we're just sending a trivial 1-byte message, we'll
1645 * clear ATN later on anyway.
1646 */
1647 outb(SCSISIG, PH_MSGOUT|ATNO);
1648 /* Reset the FIFO. */
1649 outb(DMACNTRL0, RSTFIFO);
1650 /* Enable REQ/ACK protocol. */
1651 outb(SXFRCTL0, CHEN|SPIOEN);
1652
1653 if (sc->sc_prevphase == PH_MSGOUT) {
1654 if (sc->sc_omp == sc->sc_omess) {
1655 /*
1656 * This is a retransmission.
1657 *
1658 * We get here if the target stayed in MESSAGE OUT
1659 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1660 * that all of the previously transmitted messages must
1661 * be sent again, in the same order. Therefore, we
1662 * requeue all the previously transmitted messages, and
1663 * start again from the top. Our simple priority
1664 * scheme keeps the messages in the right order.
1665 */
1666 AIC_MISC(("retransmitting "));
1667 sc->sc_msgpriq |= sc->sc_msgoutq;
1668 } else {
1669 /* This is a continuation of the previous message. */
1670 n = sc->sc_omp - sc->sc_omess;
1671 goto nextbyte;
1672 }
1673 }
1674
1675 /* No messages transmitted so far. */
1676 sc->sc_msgoutq = 0;
1677 sc->sc_lastmsg = 0;
1678
1679 nextmsg:
1680 /* Pick up highest priority message. */
1681 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1682 sc->sc_msgpriq &= ~sc->sc_currmsg;
1683 sc->sc_msgoutq |= sc->sc_currmsg;
1684
1685 /* Build the outgoing message data. */
1686 switch (sc->sc_currmsg) {
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 #ifdef AIC_PICKY
1764 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1765 sc->sc_dev.dv_xname);
1766 AIC_BREAK();
1767 #endif
1768 noop:
1769 sc->sc_omess[0] = MSG_NOOP;
1770 n = 1;
1771 break;
1772
1773 default:
1774 printf("%s: weird MESSAGE OUT; sending NOOP\n",
1775 sc->sc_dev.dv_xname);
1776 AIC_BREAK();
1777 goto noop;
1778 }
1779 sc->sc_omp = &sc->sc_omess[n];
1780
1781 nextbyte:
1782 /* Send message bytes. */
1783 for (;;) {
1784 for (;;) {
1785 sstat1 = inb(SSTAT1);
1786 if ((sstat1 & (REQINIT|BUSFREE)) != 0)
1787 break;
1788 /* Wait for REQINIT. XXX Need timeout. */
1789 }
1790 if ((sstat1 & (PHASECHG|BUSFREE)) != 0) {
1791 /*
1792 * Target left MESSAGE OUT, possibly to reject
1793 * our message.
1794 */
1795 goto out;
1796 }
1797
1798 /* Clear ATN before last byte if this is the last message. */
1799 if (n == 1 && sc->sc_msgpriq == 0)
1800 outb(CLRSINT1, CLRATNO);
1801 /* Send message byte. */
1802 outb(SCSIDAT, *--sc->sc_omp);
1803 --n;
1804 /* Keep track of the last message we've sent any bytes of. */
1805 sc->sc_lastmsg = sc->sc_currmsg;
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 host FIFO and counter. */
1851 outb(DMACNTRL0, RSTFIFO|WRITE);
1852 /* Enable FIFOs. */
1853 outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
1854 outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
1855
1856 /* Turn off ENREQINIT for now. */
1857 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENPHASECHG);
1858
1859 /* I have tried to make the main loop as tight as possible. This
1860 * means that some of the code following the loop is a bit more
1861 * complex than otherwise.
1862 */
1863 while (n > 0) {
1864 int xfer;
1865
1866 for (;;) {
1867 dmastat = inb(DMASTAT);
1868 if ((dmastat & DFIFOEMP) != 0)
1869 break;
1870 if ((dmastat & INTSTAT) != 0)
1871 goto phasechange;
1872 }
1873
1874 xfer = min(DOUTAMOUNT, n);
1875
1876 AIC_MISC(("%d> ", xfer));
1877
1878 n -= xfer;
1879 out += xfer;
1880
1881 #if AIC_USE_DWORDS
1882 if (xfer >= 12) {
1883 outsl(DMADATALONG, p, xfer>>2);
1884 p += xfer & ~3;
1885 xfer &= 3;
1886 }
1887 #else
1888 if (xfer >= 8) {
1889 outsw(DMADATA, p, xfer>>1);
1890 p += xfer & ~1;
1891 xfer &= 1;
1892 }
1893 #endif
1894
1895 if (xfer > 0) {
1896 outb(DMACNTRL0, ENDMA|B8MODE|WRITE);
1897 outsb(DMADATA, p, xfer);
1898 p += xfer;
1899 outb(DMACNTRL0, ENDMA|DWORDPIO|WRITE);
1900 }
1901 }
1902
1903 if (out == 0) {
1904 outb(SXFRCTL1, BITBUCKET);
1905 for (;;) {
1906 if ((inb(DMASTAT) & INTSTAT) != 0)
1907 break;
1908 }
1909 outb(SXFRCTL1, 0);
1910 AIC_MISC(("extra data "));
1911 } else {
1912 /* See the bytes off chip */
1913 for (;;) {
1914 dmastat = inb(DMASTAT);
1915 if ((dmastat & DFIFOEMP) != 0 &&
1916 (inb(SSTAT2) & SEMPTY) != 0)
1917 break;
1918 if ((dmastat & INTSTAT) != 0)
1919 goto phasechange;
1920 }
1921 }
1922
1923 phasechange:
1924 /* Stop the FIFO data path. */
1925 outb(SXFRCTL0, CHEN);
1926 while ((inb(SXFRCTL0) & SCSIEN) != 0)
1927 ;
1928
1929 if ((dmastat & INTSTAT) != 0) {
1930 /* Some sort of phase change. */
1931 int amount;
1932
1933 /* Stop transfers, do some accounting */
1934 amount = inb(FIFOSTAT) + inb(SSTAT2) & 15;
1935 if (amount > 0) {
1936 out -= amount;
1937 outb(SXFRCTL0, CHEN|CLRSTCNT|CLRCH);
1938 AIC_MISC(("+%d ", amount));
1939 }
1940 }
1941
1942 /* Turn on ENREQINIT again. */
1943 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
1944
1945 return out;
1946 }
1947
1948 /* aic_datain_pio: perform data transfers using the FIFO datapath in the aic6360
1950 * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
1951 * and ACK deasserted (i.e. at least one byte is ready).
1952 * For now, uses a pretty dumb algorithm, hangs around until all data has been
1953 * transferred. This, is OK for fast targets, but not so smart for slow
1954 * targets which don't disconnect or for huge transfers.
1955 */
1956 int
1957 aic_datain_pio(sc, p, n)
1958 register struct aic_softc *sc;
1959 u_char *p;
1960 int n;
1961 {
1962 register u_char dmastat;
1963 int in = 0;
1964 #define DINAMOUNT 128 /* Full FIFO */
1965
1966 /* Clear host FIFO and counter. */
1967 outb(DMACNTRL0, RSTFIFO);
1968 /* Enable FIFOs */
1969 outb(SXFRCTL0, SCSIEN|DMAEN|CHEN);
1970 outb(DMACNTRL0, ENDMA|DWORDPIO);
1971
1972 /* Turn off ENREQINIT for now. */
1973 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENPHASECHG);
1974
1975 /* We leave this loop if one or more of the following is true:
1976 * a) phase != PH_DATAIN && FIFOs are empty
1977 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
1978 */
1979 while (n > 0) {
1980 int xfer;
1981
1982 /* Wait for fifo half full or phase mismatch */
1983 for (;;) {
1984 dmastat = inb(DMASTAT);
1985 if ((dmastat & (DFIFOFULL|INTSTAT)) != 0)
1986 break;
1987 }
1988
1989 if ((dmastat & DFIFOFULL) != 0)
1990 xfer = min(DINAMOUNT, n);
1991 else
1992 xfer = min(inb(FIFOSTAT), n);
1993
1994 AIC_MISC((">%d ", xfer));
1995
1996 n -= xfer;
1997 in += xfer;
1998
1999 #if AIC_USE_DWORDS
2000 if (xfer >= 12) {
2001 insl(DMADATALONG, p, xfer>>2);
2002 p += xfer & ~3;
2003 xfer &= 3;
2004 }
2005 #else
2006 if (xfer >= 8) {
2007 insw(DMADATA, p, xfer>>1);
2008 p += xfer & ~1;
2009 xfer &= 1;
2010 }
2011 #endif
2012
2013 if (xfer > 0) {
2014 outb(DMACNTRL0, ENDMA|B8MODE);
2015 insb(DMADATA, p, xfer);
2016 p += xfer;
2017 outb(DMACNTRL0, ENDMA|DWORDPIO);
2018 }
2019
2020 if ((dmastat & INTSTAT) != 0)
2021 goto phasechange;
2022 }
2023
2024 /* Some SCSI-devices are rude enough to transfer more data than what
2025 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
2026 * requested 512. Test for progress, i.e. real transfers. If no real
2027 * transfers have been performed (n is probably already zero) and the
2028 * FIFO is not empty, waste some bytes....
2029 */
2030 if (in == 0) {
2031 outb(SXFRCTL1, BITBUCKET);
2032 for (;;) {
2033 if ((inb(DMASTAT) & INTSTAT) != 0)
2034 break;
2035 }
2036 outb(SXFRCTL1, 0);
2037 AIC_MISC(("extra data "));
2038 }
2039
2040 phasechange:
2041 /* Stop the FIFO data path. */
2042 outb(SXFRCTL0, CHEN);
2043 while ((inb(SXFRCTL0) & SCSIEN) != 0)
2044 ;
2045
2046 /* Turn on ENREQINIT again. */
2047 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
2048
2049 return in;
2050 }
2051
2052 /*
2054 * This is the workhorse routine of the driver.
2055 * Deficiencies (for now):
2056 * 1) always uses programmed I/O
2057 */
2058 int
2059 aicintr(arg)
2060 void *arg;
2061 {
2062 register struct aic_softc *sc = arg;
2063 u_char sstat0, sstat1;
2064 register struct aic_acb *acb;
2065 register struct scsi_link *sc_link;
2066 struct aic_tinfo *ti;
2067 int n;
2068
2069 /*
2070 * Clear INTEN. We enable it again before returning. This makes the
2071 * interrupt esssentially level-triggered.
2072 */
2073 outb(DMACNTRL0, 0);
2074
2075 AIC_TRACE(("aicintr "));
2076
2077 loop:
2078 gotintr:
2079 /*
2080 * First check for abnormal conditions, such as reset.
2081 */
2082 sstat1 = inb(SSTAT1);
2083 AIC_MISC(("sstat1:0x%02x ", sstat1));
2084
2085 if ((sstat1 & SCSIRSTI) != 0) {
2086 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
2087 goto reset;
2088 }
2089
2090 /*
2091 * Check for less serious errors.
2092 */
2093 if ((sstat1 & SCSIPERR) != 0) {
2094 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
2095 outb(CLRSINT1, CLRSCSIPERR);
2096 if (sc->sc_prevphase == PH_MSGIN) {
2097 aic_sched_msgout(SEND_PARITY_ERROR);
2098 sc->sc_flags |= AIC_DROP_MSGIN;
2099 } else
2100 aic_sched_msgout(SEND_INIT_DET_ERR);
2101 }
2102
2103 /*
2104 * If we're not already busy doing something test for the following
2105 * conditions:
2106 * 1) We have been reselected by something
2107 * 2) We have selected something successfully
2108 * 3) Our selection process has timed out
2109 * 4) This is really a bus free interrupt just to get a new command
2110 * going?
2111 * 5) Spurious interrupt?
2112 */
2113 switch (sc->sc_state) {
2114 case AIC_IDLE:
2115 case AIC_SELECTING:
2116 sstat0 = inb(SSTAT0);
2117 AIC_MISC(("sstat0:0x%02x ", sstat0));
2118
2119 if ((sstat0 & TARGET) != 0) {
2120 /*
2121 * We don't currently support target mode.
2122 */
2123 printf("%s: target mode selected; going to bus free\n",
2124 sc->sc_dev.dv_xname);
2125 outb(SCSISIG, 0);
2126
2127 sc->sc_state = AIC_IDLE;
2128 aic_sched(sc);
2129 goto out;
2130 } else if ((sstat0 & SELDI) != 0) {
2131 AIC_MISC(("reselected "));
2132
2133 /*
2134 * If we're trying to select a target ourselves,
2135 * push our command back into the ready list.
2136 */
2137 if (sc->sc_state == AIC_SELECTING) {
2138 AIC_MISC(("backoff selector "));
2139 AIC_ASSERT(sc->sc_nexus != NULL);
2140 acb = sc->sc_nexus;
2141 sc->sc_nexus = NULL;
2142 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2143 }
2144
2145 /* Save reselection ID. */
2146 sc->sc_selid = inb(SELID);
2147
2148 sc->sc_state = AIC_RESELECTED;
2149 } else if ((sstat0 & SELDO) != 0) {
2150 AIC_MISC(("selected "));
2151
2152 /* We have selected a target. Things to do:
2153 * a) Determine what message(s) to send.
2154 * b) Verify that we're still selecting the target.
2155 * c) Mark device as busy.
2156 */
2157 if (sc->sc_state != AIC_SELECTING) {
2158 printf("%s: selection out while idle; resetting\n",
2159 sc->sc_dev.dv_xname);
2160 AIC_BREAK();
2161 goto reset;
2162 }
2163 AIC_ASSERT(sc->sc_nexus != NULL);
2164 acb = sc->sc_nexus;
2165
2166 sc_link = acb->xs->sc_link;
2167 ti = &sc->sc_tinfo[sc_link->target];
2168 if ((acb->xs->flags & SCSI_RESET) == 0) {
2169 sc->sc_msgpriq = SEND_IDENTIFY;
2170 if (acb->flags != ACB_ABORTED) {
2171 #if AIC_USE_SYNCHRONOUS
2172 if ((ti->flags & DO_SYNC) != 0)
2173 sc->sc_msgpriq |= SEND_SDTR;
2174 #endif
2175 #if AIC_USE_WIDE
2176 if ((ti->flags & DO_WIDE) != 0)
2177 sc->sc_msgpriq |= SEND_WDTR;
2178 #endif
2179 } else {
2180 sc->sc_flags |= AIC_ABORTING;
2181 sc->sc_msgpriq |= SEND_ABORT;
2182 }
2183 } else
2184 sc->sc_msgpriq = SEND_DEV_RESET;
2185
2186 ti->lubusy |= (1 << sc_link->lun);
2187
2188 /* Do an implicit RESTORE POINTERS. */
2189 sc->sc_dp = acb->data_addr;
2190 sc->sc_dleft = acb->data_length;
2191 sc->sc_cp = (u_char *)&acb->scsi_cmd;
2192 sc->sc_cleft = acb->scsi_cmd_length;
2193
2194 sc->sc_state = AIC_CONNECTED;
2195 } else if ((sstat1 & SELTO) != 0) {
2196 AIC_MISC(("selection timeout "));
2197
2198 if (sc->sc_state != AIC_SELECTING) {
2199 printf("%s: selection timeout while idle; resetting\n",
2200 sc->sc_dev.dv_xname);
2201 AIC_BREAK();
2202 goto reset;
2203 }
2204 AIC_ASSERT(sc->sc_nexus != NULL);
2205 acb = sc->sc_nexus;
2206
2207 outb(SXFRCTL1, 0);
2208 outb(SCSISEQ, ENRESELI);
2209 outb(CLRSINT1, CLRSELTIMO);
2210
2211 acb->xs->error = XS_SELTIMEOUT;
2212 untimeout(aic_timeout, acb);
2213 delay(250);
2214 aic_done(sc, acb);
2215 goto out;
2216 } else {
2217 #ifdef AIC_PICKY
2218 if (sc->sc_state != AIC_IDLE) {
2219 printf("%s: BUS FREE while not idle; state=%d\n",
2220 sc->sc_dev.dv_xname, sc->sc_state);
2221 AIC_BREAK();
2222 goto out;
2223 }
2224 #endif
2225
2226 aic_sched(sc);
2227 goto out;
2228 }
2229
2230 /*
2231 * Turn off selection stuff, and prepare to catch bus free
2232 * interrupts, parity errors, and phase changes.
2233 */
2234 outb(SXFRCTL1, 0);
2235 outb(SCSISEQ, ENAUTOATNP);
2236 outb(CLRSINT0, CLRSELDI|CLRSELDO);
2237 outb(CLRSINT1, CLRBUSFREE|CLRPHASECHG);
2238 outb(SIMODE0, 0);
2239 outb(SIMODE1, ENSCSIRST|ENSCSIPERR|ENBUSFREE|ENREQINIT|ENPHASECHG);
2240
2241 sc->sc_flags = 0;
2242 sc->sc_prevphase = PH_INVALID;
2243 goto dophase;
2244 }
2245
2246 outb(CLRSINT1, CLRPHASECHG);
2247
2248 if ((sstat1 & BUSFREE) != 0) {
2249 /* We've gone to BUS FREE phase. */
2250 outb(CLRSINT1, CLRBUSFREE);
2251
2252 switch (sc->sc_state) {
2253 case AIC_RESELECTED:
2254 sc->sc_state = AIC_IDLE;
2255 aic_sched(sc);
2256 break;
2257
2258 case AIC_CONNECTED:
2259 if ((sc->sc_flags & AIC_ABORTING) == 0) {
2260 printf("%s: unexpected BUS FREE; aborting\n",
2261 sc->sc_dev.dv_xname);
2262 AIC_BREAK();
2263 }
2264 AIC_ASSERT(sc->sc_nexus != NULL);
2265 acb = sc->sc_nexus;
2266 acb->xs->error = XS_DRIVER_STUFFUP;
2267 goto finish;
2268
2269 case AIC_DISCONNECT:
2270 AIC_ASSERT(sc->sc_nexus != NULL);
2271 acb = sc->sc_nexus;
2272 sc->sc_state = AIC_IDLE;
2273 sc->sc_nexus = NULL;
2274 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
2275 aic_sched(sc);
2276 break;
2277
2278 case AIC_CMDCOMPLETE:
2279 AIC_ASSERT(sc->sc_nexus != NULL);
2280 acb = sc->sc_nexus;
2281 finish:
2282 untimeout(aic_timeout, acb);
2283 aic_done(sc, acb);
2284 break;
2285 }
2286 goto out;
2287 }
2288
2289 dophase:
2290 if ((sstat1 & REQINIT) == 0) {
2291 /* Wait for REQINIT. */
2292 goto out;
2293 }
2294
2295 sc->sc_phase = inb(SCSISIG) & PH_MASK;
2296 outb(SCSISIG, sc->sc_phase);
2297
2298 switch (sc->sc_phase) {
2299 case PH_MSGOUT:
2300 /* If aborting, always handle MESSAGE OUT. */
2301 if ((sc->sc_state & AIC_CONNECTED) == 0 &&
2302 (sc->sc_flags & AIC_ABORTING) == 0)
2303 break;
2304 aic_msgout(sc);
2305 sc->sc_prevphase = PH_MSGOUT;
2306 goto loop;
2307
2308 case PH_MSGIN:
2309 if ((sc->sc_state & (AIC_CONNECTED|AIC_RESELECTED)) == 0)
2310 break;
2311 if (aic_msgin(sc)) {
2312 sc->sc_prevphase = PH_MSGIN;
2313 goto gotintr;
2314 }
2315 sc->sc_prevphase = PH_MSGIN;
2316 goto loop;
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 sc->sc_prevphase = PH_CMD;
2333 goto loop;
2334
2335 case PH_DATAOUT:
2336 if ((sc->sc_state & AIC_CONNECTED) == 0)
2337 break;
2338 AIC_MISC(("dataout dleft=%d ", sc->sc_dleft));
2339 n = aic_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
2340 sc->sc_dp += n;
2341 sc->sc_dleft -= n;
2342 sc->sc_prevphase = PH_DATAOUT;
2343 goto loop;
2344
2345 case PH_DATAIN:
2346 if ((sc->sc_state & AIC_CONNECTED) == 0)
2347 break;
2348 AIC_MISC(("datain "));
2349 n = aic_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
2350 sc->sc_dp += n;
2351 sc->sc_dleft -= n;
2352 sc->sc_prevphase = PH_DATAIN;
2353 goto loop;
2354
2355 case PH_STAT:
2356 if ((sc->sc_state & AIC_CONNECTED) == 0)
2357 break;
2358 AIC_ASSERT(sc->sc_nexus != NULL);
2359 acb = sc->sc_nexus;
2360 /* XXXX Don't clear FIFO. Wait for byte to come in. */
2361 outb(SXFRCTL0, CHEN|SPIOEN);
2362 outb(DMACNTRL0, RSTFIFO);
2363 acb->target_stat = inb(SCSIDAT);
2364 outb(SXFRCTL0, CHEN);
2365 outb(DMACNTRL0, RSTFIFO);
2366 while ((inb(SXFRCTL0) & SCSIEN) != 0)
2367 ;
2368 AIC_MISC(("target_stat=0x%02x ", acb->target_stat));
2369 sc->sc_prevphase = PH_STAT;
2370 goto loop;
2371 }
2372
2373 printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
2374 AIC_BREAK();
2375 reset:
2376 aic_init(sc);
2377 return 1;
2378
2379 out:
2380 outb(DMACNTRL0, INTEN);
2381 return 1;
2382 }
2383
2384 void
2385 aic_abort(sc, acb)
2386 struct aic_softc *sc;
2387 struct aic_acb *acb;
2388 {
2389
2390 if (sc->sc_nexus == acb) {
2391 if (sc->sc_state == AIC_CONNECTED) {
2392 sc->sc_flags |= AIC_ABORTING;
2393 aic_sched_msgout(SEND_ABORT);
2394 }
2395 } else {
2396 aic_dequeue(sc, acb);
2397 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2398 if (sc->sc_state == AIC_IDLE)
2399 aic_sched(sc);
2400 }
2401 }
2402
2403 void
2404 aic_timeout(arg)
2405 void *arg;
2406 {
2407 struct aic_acb *acb = arg;
2408 struct scsi_xfer *xs = acb->xs;
2409 struct scsi_link *sc_link = xs->sc_link;
2410 struct aic_softc *sc = sc_link->adapter_softc;
2411 int s;
2412
2413 sc_print_addr(sc_link);
2414 printf("timed out");
2415
2416 s = splbio();
2417
2418 if (acb->flags == ACB_ABORTED) {
2419 /* abort timed out */
2420 printf(" AGAIN\n");
2421 acb->xs->retries = 0;
2422 aic_done(sc, acb);
2423 } else {
2424 /* abort the operation that has timed out */
2425 printf("\n");
2426 acb->xs->error = XS_TIMEOUT;
2427 acb->flags = ACB_ABORTED;
2428 aic_abort(sc, acb);
2429 /* 2 secs for the abort */
2430 if ((xs->flags & SCSI_POLL) == 0)
2431 timeout(aic_timeout, acb, 2 * hz);
2432 }
2433
2434 splx(s);
2435 }
2436
2437 #ifdef AIC_DEBUG
2439 /*
2440 * The following functions are mostly used for debugging purposes, either
2441 * directly called from the driver or from the kernel debugger.
2442 */
2443
2444 void
2445 aic_show_scsi_cmd(acb)
2446 struct aic_acb *acb;
2447 {
2448 u_char *b = (u_char *)&acb->scsi_cmd;
2449 struct scsi_link *sc_link = acb->xs->sc_link;
2450 int i;
2451
2452 sc_print_addr(sc_link);
2453 if ((acb->xs->flags & SCSI_RESET) == 0) {
2454 for (i = 0; i < acb->scsi_cmd_length; i++) {
2455 if (i)
2456 printf(",");
2457 printf("%x", b[i]);
2458 }
2459 printf("\n");
2460 } else
2461 printf("RESET\n");
2462 }
2463
2464 void
2465 aic_print_acb(acb)
2466 struct aic_acb *acb;
2467 {
2468
2469 printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
2470 printf(" dp=%x dleft=%d target_stat=%x\n",
2471 (long)acb->data_addr, acb->data_length, acb->target_stat);
2472 aic_show_scsi_cmd(acb);
2473 }
2474
2475 void
2476 aic_print_active_acb()
2477 {
2478 struct aic_acb *acb;
2479 struct aic_softc *sc = aiccd.cd_devs[0];
2480
2481 printf("ready list:\n");
2482 for (acb = sc->ready_list.tqh_first; acb != NULL;
2483 acb = acb->chain.tqe_next)
2484 aic_print_acb(acb);
2485 printf("nexus:\n");
2486 if (sc->sc_nexus != NULL)
2487 aic_print_acb(sc->sc_nexus);
2488 printf("nexus list:\n");
2489 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2490 acb = acb->chain.tqe_next)
2491 aic_print_acb(acb);
2492 }
2493
2494 void
2495 aic_dump6360(sc)
2496 struct aic_softc *sc;
2497 {
2498
2499 printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIG=%x\n",
2500 inb(SCSISEQ), inb(SXFRCTL0), inb(SXFRCTL1), inb(SCSISIG));
2501 printf(" SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
2502 inb(SSTAT0), inb(SSTAT1), inb(SSTAT2), inb(SSTAT3), inb(SSTAT4));
2503 printf(" SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n",
2504 inb(SIMODE0), inb(SIMODE1), inb(DMACNTRL0), inb(DMACNTRL1),
2505 inb(DMASTAT));
2506 printf(" FIFOSTAT=%d SCSIBUS=0x%x\n",
2507 inb(FIFOSTAT), inb(SCSIBUS));
2508 }
2509
2510 void
2511 aic_dump_driver(sc)
2512 struct aic_softc *sc;
2513 {
2514 struct aic_tinfo *ti;
2515 int i;
2516
2517 printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2518 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2519 sc->sc_state, sc->sc_imess[0],
2520 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2521 for (i = 0; i < 7; i++) {
2522 ti = &sc->sc_tinfo[i];
2523 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2524 i, ti->cmds, ti->dconns, ti->touts);
2525 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2526 }
2527 }
2528 #endif
2529