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