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