aic6360.c revision 1.59 1 /* $NetBSD: aic6360.c,v 1.59 1998/10/10 00:28:33 thorpej Exp $ */
2
3 #include "opt_ddb.h"
4 #ifdef DDB
5 #define integrate
6 #else
7 #define integrate static inline
8 #endif
9
10 /*
11 * Copyright (c) 1994, 1995, 1996 Charles M. Hannum. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Charles M. Hannum.
24 * 4. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * Copyright (c) 1994 Jarle Greipsland
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. The name of the author may not be used to endorse or promote products
39 * derived from this software without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
45 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
50 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 /*
55 * Acknowledgements: Many of the algorithms used in this driver are
56 * inspired by the work of Julian Elischer (julian (at) tfs.com) and
57 * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu). Thanks a million!
58 */
59
60 /* TODO list:
61 * 1) Get the DMA stuff working.
62 * 2) Get the iov/uio stuff working. Is this a good thing ???
63 * 3) Get the synch stuff working.
64 * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
65 */
66
67 /*
68 * A few customizable items:
69 */
70
71 /* Use doubleword transfers to/from SCSI chip. Note: This requires
72 * motherboard support. Basicly, some motherboard chipsets are able to
73 * split a 32 bit I/O operation into two 16 bit I/O operations,
74 * transparently to the processor. This speeds up some things, notably long
75 * data transfers.
76 */
77 #define AIC_USE_DWORDS 0
78
79 /* Synchronous data transfers? */
80 #define AIC_USE_SYNCHRONOUS 0
81 #define AIC_SYNC_REQ_ACK_OFS 8
82
83 /* Wide data transfers? */
84 #define AIC_USE_WIDE 0
85 #define AIC_MAX_WIDTH 0
86
87 /* Max attempts made to transmit a message */
88 #define AIC_MSG_MAX_ATTEMPT 3 /* Not used now XXX */
89
90 /* Use DMA (else we do programmed I/O using string instructions) (not yet!)*/
91 #define AIC_USE_EISA_DMA 0
92 #define AIC_USE_ISA_DMA 0
93
94 /* How to behave on the (E)ISA bus when/if DMAing (on<<4) + off in us */
95 #define EISA_BRST_TIM ((15<<4) + 1) /* 15us on, 1us off */
96
97 /* Some spin loop parameters (essentially how long to wait some places)
98 * The problem(?) is that sometimes we expect either to be able to transmit a
99 * byte or to get a new one from the SCSI bus pretty soon. In order to avoid
100 * returning from the interrupt just to get yanked back for the next byte we
101 * may spin in the interrupt routine waiting for this byte to come. How long?
102 * This is really (SCSI) device and processor dependent. Tuneable, I guess.
103 */
104 #define AIC_MSGIN_SPIN 1 /* Will spinwait upto ?ms for a new msg byte */
105 #define AIC_MSGOUT_SPIN 1
106
107 /* Include debug functions? At the end of this file there are a bunch of
108 * functions that will print out various information regarding queued SCSI
109 * commands, driver state and chip contents. You can call them from the
110 * kernel debugger. If you set AIC_DEBUG to 0 they are not included (the
111 * kernel uses less memory) but you lose the debugging facilities.
112 */
113 #define AIC_DEBUG 1
114
115 #define AIC_ABORT_TIMEOUT 2000 /* time to wait for abort */
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/bus.h>
136 #include <machine/intr.h>
137
138 #include <dev/scsipi/scsi_all.h>
139 #include <dev/scsipi/scsipi_all.h>
140 #include <dev/scsipi/scsi_message.h>
141 #include <dev/scsipi/scsiconf.h>
142
143 #include <dev/ic/aic6360reg.h>
144 #include <dev/ic/aic6360var.h>
145
146
147 #ifndef DDB
149 #define Debugger() panic("should call debugger here (aic6360.c)")
150 #endif /* ! DDB */
151
152 #if AIC_DEBUG
153 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; */
154 #endif
155
156 void aicattach __P((struct aic_softc *));
157 void aic_minphys __P((struct buf *));
158 void aic_init __P((struct aic_softc *));
159 void aic_done __P((struct aic_softc *, struct aic_acb *));
160 void aic_dequeue __P((struct aic_softc *, struct aic_acb *));
161 int aic_scsi_cmd __P((struct scsipi_xfer *));
162 int aic_poll __P((struct aic_softc *, struct scsipi_xfer *, int));
163 integrate void aic_sched_msgout __P((struct aic_softc *, u_char));
164 integrate void aic_setsync __P((struct aic_softc *, struct aic_tinfo *));
165 void aic_select __P((struct aic_softc *, struct aic_acb *));
166 void aic_timeout __P((void *));
167 void aic_sched __P((struct aic_softc *));
168 void aic_scsi_reset __P((struct aic_softc *));
169 void aic_reset __P((struct aic_softc *));
170 void aic_free_acb __P((struct aic_softc *, struct aic_acb *, int));
171 struct aic_acb* aic_get_acb __P((struct aic_softc *, int));
172 int aic_reselect __P((struct aic_softc *, int));
173 void aic_sense __P((struct aic_softc *, struct aic_acb *));
174 void aic_msgin __P((struct aic_softc *));
175 void aic_abort __P((struct aic_softc *, struct aic_acb *));
176 void aic_msgout __P((struct aic_softc *));
177 int aic_dataout_pio __P((struct aic_softc *, u_char *, int));
178 int aic_datain_pio __P((struct aic_softc *, u_char *, int));
179 #if AIC_DEBUG
180 void aic_print_acb __P((struct aic_acb *));
181 void aic_dump_driver __P((struct aic_softc *));
182 void aic_dump6360 __P((struct aic_softc *));
183 void aic_show_scsi_cmd __P((struct aic_acb *));
184 void aic_print_active_acb __P((void));
185 #endif
186
187 struct scsipi_adapter aic_switch = {
188 aic_scsi_cmd, /* scsipi_cmd */
189 aic_minphys, /* scsipi_minphys */
190 NULL, /* scsipi_ioctl */
191 };
192
193 struct scsipi_device aic_dev = {
194 NULL, /* Use default error handler */
195 NULL, /* have a queue, served by this */
196 NULL, /* have no async handler */
197 NULL, /* Use default 'done' routine */
198 };
199
200 /*
202 * INITIALIZATION ROUTINES (probe, attach ++)
203 */
204
205 /* Do the real search-for-device.
206 * Prerequisite: sc->sc_iobase should be set to the proper value
207 */
208 int
209 aic_find(iot, ioh)
210 bus_space_tag_t iot;
211 bus_space_handle_t ioh;
212 {
213 char chip_id[sizeof(IDSTRING)]; /* For chips that support it */
214 int i;
215
216 /* Remove aic6360 from possible powerdown mode */
217 bus_space_write_1(iot, ioh, DMACNTRL0, 0);
218
219 /* Thanks to mark (at) aggregate.com for the new method for detecting
220 * whether the chip is present or not. Bonus: may also work for
221 * the AIC-6260!
222 */
223 AIC_TRACE(("aic: probing for aic-chip\n"));
224 /*
225 * Linux also init's the stack to 1-16 and then clears it,
226 * 6260's don't appear to have an ID reg - mpg
227 */
228 /* Push the sequence 0,1,..,15 on the stack */
229 #define STSIZE 16
230 bus_space_write_1(iot, ioh, DMACNTRL1, 0); /* Reset stack pointer */
231 for (i = 0; i < STSIZE; i++)
232 bus_space_write_1(iot, ioh, STACK, i);
233
234 /* See if we can pull out the same sequence */
235 bus_space_write_1(iot, ioh, DMACNTRL1, 0);
236 for (i = 0; i < STSIZE && bus_space_read_1(iot, ioh, STACK) == i; i++)
237 ;
238 if (i != STSIZE) {
239 AIC_START(("STACK futzed at %d.\n", i));
240 return 0;
241 }
242
243 /* See if we can pull the id string out of the ID register,
244 * now only used for informational purposes.
245 */
246 bzero(chip_id, sizeof(chip_id));
247 bus_space_read_multi_1(iot, ioh, ID, chip_id, sizeof(IDSTRING) - 1);
248 AIC_START(("AIC found ID: %s ",chip_id));
249 AIC_START(("chip revision %d\n",
250 (int)bus_space_read_1(iot, ioh, REV)));
251
252 return 1;
253 }
254
255 /*
256 * Attach the AIC6360, fill out some high and low level data structures
257 */
258 void
259 aicattach(sc)
260 struct aic_softc *sc;
261 {
262
263 AIC_TRACE(("aicattach "));
264 sc->sc_state = AIC_INIT;
265
266 sc->sc_initiator = 7;
267 sc->sc_freq = 20; /* XXXX Assume 20 MHz. */
268
269 /*
270 * These are the bounds of the sync period, based on the frequency of
271 * the chip's clock input and the size and offset of the sync period
272 * register.
273 *
274 * For a 20Mhz clock, this gives us 25, or 100nS, or 10MB/s, as a
275 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a
276 * minimum transfer rate.
277 */
278 sc->sc_minsync = (2 * 250) / sc->sc_freq;
279 sc->sc_maxsync = (9 * 250) / sc->sc_freq;
280
281 aic_init(sc); /* Init chip and driver */
282
283 /*
284 * Fill in the prototype scsipi_link
285 */
286 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
287 sc->sc_link.adapter_softc = sc;
288 sc->sc_link.scsipi_scsi.adapter_target = sc->sc_initiator;
289 sc->sc_link.adapter = &aic_switch;
290 sc->sc_link.device = &aic_dev;
291 sc->sc_link.openings = 2;
292 sc->sc_link.scsipi_scsi.max_target = 7;
293 sc->sc_link.type = BUS_SCSI;
294
295 /*
296 * ask the adapter what subunits are present
297 */
298 config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
299 }
300
301
302 /* Initialize AIC6360 chip itself
303 * The following conditions should hold:
304 * aic_isa_probe should have succeeded, i.e. the iobase address in aic_softc
305 * must be valid.
306 */
307 void
308 aic_reset(sc)
309 struct aic_softc *sc;
310 {
311 bus_space_tag_t iot = sc->sc_iot;
312 bus_space_handle_t ioh = sc->sc_ioh;
313
314 /*
315 * Doc. recommends to clear these two registers before
316 * operations commence
317 */
318 bus_space_write_1(iot, ioh, SCSITEST, 0);
319 bus_space_write_1(iot, ioh, TEST, 0);
320
321 /* Reset SCSI-FIFO and abort any transfers */
322 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | CLRCH | CLRSTCNT);
323
324 /* Reset DMA-FIFO */
325 bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO);
326 bus_space_write_1(iot, ioh, DMACNTRL1, 0);
327
328 /* Disable all selection features */
329 bus_space_write_1(iot, ioh, SCSISEQ, 0);
330 bus_space_write_1(iot, ioh, SXFRCTL1, 0);
331
332 /* Disable some interrupts */
333 bus_space_write_1(iot, ioh, SIMODE0, 0x00);
334 /* Clear a slew of interrupts */
335 bus_space_write_1(iot, ioh, CLRSINT0, 0x7f);
336
337 /* Disable some more interrupts */
338 bus_space_write_1(iot, ioh, SIMODE1, 0x00);
339 /* Clear another slew of interrupts */
340 bus_space_write_1(iot, ioh, CLRSINT1, 0xef);
341
342 /* Disable synchronous transfers */
343 bus_space_write_1(iot, ioh, SCSIRATE, 0);
344
345 /* Haven't seen ant errors (yet) */
346 bus_space_write_1(iot, ioh, CLRSERR, 0x07);
347
348 /* Set our SCSI-ID */
349 bus_space_write_1(iot, ioh, SCSIID, sc->sc_initiator << OID_S);
350 bus_space_write_1(iot, ioh, BRSTCNTRL, EISA_BRST_TIM);
351 }
352
353 /* Pull the SCSI RST line for 500 us */
354 void
355 aic_scsi_reset(sc)
356 struct aic_softc *sc;
357 {
358 bus_space_tag_t iot = sc->sc_iot;
359 bus_space_handle_t ioh = sc->sc_ioh;
360
361 bus_space_write_1(iot, ioh, SCSISEQ, SCSIRSTO);
362 delay(500);
363 bus_space_write_1(iot, ioh, SCSISEQ, 0);
364 delay(50);
365 }
366
367 /*
368 * Initialize aic SCSI driver.
369 */
370 void
371 aic_init(sc)
372 struct aic_softc *sc;
373 {
374 struct aic_acb *acb;
375 int r;
376
377 aic_reset(sc);
378 aic_scsi_reset(sc);
379 aic_reset(sc);
380
381 if (sc->sc_state == AIC_INIT) {
382 /* First time through; initialize. */
383 TAILQ_INIT(&sc->ready_list);
384 TAILQ_INIT(&sc->nexus_list);
385 TAILQ_INIT(&sc->free_list);
386 sc->sc_nexus = NULL;
387 acb = sc->sc_acb;
388 bzero(acb, sizeof(sc->sc_acb));
389 for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
390 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
391 acb++;
392 }
393 bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
394 } else {
395 /* Cancel any active commands. */
396 sc->sc_state = AIC_CLEANING;
397 if ((acb = sc->sc_nexus) != NULL) {
398 acb->xs->error = XS_DRIVER_STUFFUP;
399 untimeout(aic_timeout, acb);
400 aic_done(sc, acb);
401 }
402 while ((acb = sc->nexus_list.tqh_first) != NULL) {
403 acb->xs->error = XS_DRIVER_STUFFUP;
404 untimeout(aic_timeout, acb);
405 aic_done(sc, acb);
406 }
407 }
408
409 sc->sc_prevphase = PH_INVALID;
410 for (r = 0; r < 8; r++) {
411 struct aic_tinfo *ti = &sc->sc_tinfo[r];
412
413 ti->flags = 0;
414 #if AIC_USE_SYNCHRONOUS
415 ti->flags |= DO_SYNC;
416 ti->period = sc->sc_minsync;
417 ti->offset = AIC_SYNC_REQ_ACK_OFS;
418 #else
419 ti->period = ti->offset = 0;
420 #endif
421 #if AIC_USE_WIDE
422 ti->flags |= DO_WIDE;
423 ti->width = AIC_MAX_WIDTH;
424 #else
425 ti->width = 0;
426 #endif
427 }
428
429 sc->sc_state = AIC_IDLE;
430 bus_space_write_1(sc->sc_iot, sc->sc_ioh, DMACNTRL0, INTEN);
431 }
432
433 void
434 aic_free_acb(sc, acb, flags)
435 struct aic_softc *sc;
436 struct aic_acb *acb;
437 int flags;
438 {
439 int s;
440
441 s = splbio();
442
443 acb->flags = 0;
444 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
445
446 /*
447 * If there were none, wake anybody waiting for one to come free,
448 * starting with queued entries.
449 */
450 if (acb->chain.tqe_next == 0)
451 wakeup(&sc->free_list);
452
453 splx(s);
454 }
455
456 struct aic_acb *
457 aic_get_acb(sc, flags)
458 struct aic_softc *sc;
459 int flags;
460 {
461 struct aic_acb *acb;
462 int s;
463
464 s = splbio();
465
466 while ((acb = sc->free_list.tqh_first) == NULL &&
467 (flags & SCSI_NOSLEEP) == 0)
468 tsleep(&sc->free_list, PRIBIO, "aicacb", 0);
469 if (acb) {
470 TAILQ_REMOVE(&sc->free_list, acb, chain);
471 acb->flags |= ACB_ALLOC;
472 }
473
474 splx(s);
475 return acb;
476 }
477
478 /*
480 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
481 */
482
483 /*
484 * Expected sequence:
485 * 1) Command inserted into ready list
486 * 2) Command selected for execution
487 * 3) Command won arbitration and has selected target device
488 * 4) Send message out (identify message, eventually also sync.negotiations)
489 * 5) Send command
490 * 5a) Receive disconnect message, disconnect.
491 * 5b) Reselected by target
492 * 5c) Receive identify message from target.
493 * 6) Send or receive data
494 * 7) Receive status
495 * 8) Receive message (command complete etc.)
496 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
497 * Repeat 2-8 (no disconnects please...)
498 */
499
500 /*
501 * Start a SCSI-command
502 * This function is called by the higher level SCSI-driver to queue/run
503 * SCSI-commands.
504 */
505 int
506 aic_scsi_cmd(xs)
507 struct scsipi_xfer *xs;
508 {
509 struct scsipi_link *sc_link = xs->sc_link;
510 struct aic_softc *sc = sc_link->adapter_softc;
511 struct aic_acb *acb;
512 int s, flags;
513
514 AIC_TRACE(("aic_scsi_cmd "));
515 AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
516 sc_link->scsipi_scsi.target));
517
518 flags = xs->flags;
519 if ((acb = aic_get_acb(sc, flags)) == NULL) {
520 xs->error = XS_DRIVER_STUFFUP;
521 return TRY_AGAIN_LATER;
522 }
523
524 /* Initialize acb */
525 acb->xs = xs;
526 acb->timeout = xs->timeout;
527
528 if (xs->flags & SCSI_RESET) {
529 acb->flags |= ACB_RESET;
530 acb->scsipi_cmd_length = 0;
531 acb->data_length = 0;
532 } else {
533 bcopy(xs->cmd, &acb->scsipi_cmd, xs->cmdlen);
534 acb->scsipi_cmd_length = xs->cmdlen;
535 acb->data_addr = xs->data;
536 acb->data_length = xs->datalen;
537 }
538 acb->target_stat = 0;
539
540 s = splbio();
541
542 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
543 if (sc->sc_state == AIC_IDLE)
544 aic_sched(sc);
545
546 splx(s);
547
548 if ((flags & SCSI_POLL) == 0)
549 return SUCCESSFULLY_QUEUED;
550
551 /* Not allowed to use interrupts, use polling instead */
552 if (aic_poll(sc, xs, acb->timeout)) {
553 aic_timeout(acb);
554 if (aic_poll(sc, xs, acb->timeout))
555 aic_timeout(acb);
556 }
557 return COMPLETE;
558 }
559
560 /*
561 * Adjust transfer size in buffer structure
562 */
563 void
564 aic_minphys(bp)
565 struct buf *bp;
566 {
567
568 AIC_TRACE(("aic_minphys "));
569 if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
570 bp->b_bcount = (AIC_NSEG << PGSHIFT);
571 minphys(bp);
572 }
573
574 /*
575 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
576 */
577 int
578 aic_poll(sc, xs, count)
579 struct aic_softc *sc;
580 struct scsipi_xfer *xs;
581 int count;
582 {
583 bus_space_tag_t iot = sc->sc_iot;
584 bus_space_handle_t ioh = sc->sc_ioh;
585
586 AIC_TRACE(("aic_poll "));
587 while (count) {
588 /*
589 * If we had interrupts enabled, would we
590 * have got an interrupt?
591 */
592 if ((bus_space_read_1(iot, ioh, DMASTAT) & INTSTAT) != 0)
593 aicintr(sc);
594 if ((xs->flags & ITSDONE) != 0)
595 return 0;
596 delay(1000);
597 count--;
598 }
599 return 1;
600 }
601
602 /*
604 * LOW LEVEL SCSI UTILITIES
605 */
606
607 integrate void
608 aic_sched_msgout(sc, m)
609 struct aic_softc *sc;
610 u_char m;
611 {
612 bus_space_tag_t iot = sc->sc_iot;
613 bus_space_handle_t ioh = sc->sc_ioh;
614
615 if (sc->sc_msgpriq == 0)
616 bus_space_write_1(iot, ioh, SCSISIG, sc->sc_phase | ATNO);
617 sc->sc_msgpriq |= m;
618 }
619
620 /*
621 * Set synchronous transfer offset and period.
622 */
623 integrate void
624 aic_setsync(sc, ti)
625 struct aic_softc *sc;
626 struct aic_tinfo *ti;
627 {
628 #if AIC_USE_SYNCHRONOUS
629 bus_space_tag_t iot = sc->sc_iot;
630 bus_space_handle_t ioh = sc->sc_ioh;
631
632 if (ti->offset != 0)
633 bus_space_write_1(iot, ioh, SCSIRATE,
634 ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
635 else
636 bus_space_write_1(iot, ioh, SCSIRATE, 0);
637 #endif
638 }
639
640 /*
641 * Start a selection. This is used by aic_sched() to select an idle target,
642 * and by aic_done() to immediately reselect a target to get sense information.
643 */
644 void
645 aic_select(sc, acb)
646 struct aic_softc *sc;
647 struct aic_acb *acb;
648 {
649 struct scsipi_link *sc_link = acb->xs->sc_link;
650 int target = sc_link->scsipi_scsi.target;
651 struct aic_tinfo *ti = &sc->sc_tinfo[target];
652 bus_space_tag_t iot = sc->sc_iot;
653 bus_space_handle_t ioh = sc->sc_ioh;
654
655 bus_space_write_1(iot, ioh, SCSIID,
656 sc->sc_initiator << OID_S | target);
657 aic_setsync(sc, ti);
658 bus_space_write_1(iot, ioh, SXFRCTL1, STIMO_256ms | ENSTIMER);
659
660 /* Always enable reselections. */
661 bus_space_write_1(iot, ioh, SIMODE0, ENSELDI | ENSELDO);
662 bus_space_write_1(iot, ioh, SIMODE1, ENSCSIRST | ENSELTIMO);
663 bus_space_write_1(iot, ioh, SCSISEQ, ENRESELI | ENSELO | ENAUTOATNO);
664
665 sc->sc_state = AIC_SELECTING;
666 }
667
668 int
669 aic_reselect(sc, message)
670 struct aic_softc *sc;
671 int message;
672 {
673 u_char selid, target, lun;
674 struct aic_acb *acb;
675 struct scsipi_link *sc_link;
676 struct aic_tinfo *ti;
677
678 /*
679 * The SCSI chip made a snapshot of the data bus while the reselection
680 * was being negotiated. This enables us to determine which target did
681 * the reselect.
682 */
683 selid = sc->sc_selid & ~(1 << sc->sc_initiator);
684 if (selid & (selid - 1)) {
685 printf("%s: reselect with invalid selid %02x; "
686 "sending DEVICE RESET\n", sc->sc_dev.dv_xname, selid);
687 AIC_BREAK();
688 goto reset;
689 }
690
691 /* Search wait queue for disconnected cmd
692 * The list should be short, so I haven't bothered with
693 * any more sophisticated structures than a simple
694 * singly linked list.
695 */
696 target = ffs(selid) - 1;
697 lun = message & 0x07;
698 for (acb = sc->nexus_list.tqh_first; acb != NULL;
699 acb = acb->chain.tqe_next) {
700 sc_link = acb->xs->sc_link;
701 if (sc_link->scsipi_scsi.target == target && sc_link->scsipi_scsi.lun == lun)
702 break;
703 }
704 if (acb == NULL) {
705 printf("%s: reselect from target %d lun %d with no nexus; "
706 "sending ABORT\n", sc->sc_dev.dv_xname, target, lun);
707 AIC_BREAK();
708 goto abort;
709 }
710
711 /* Make this nexus active again. */
712 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
713 sc->sc_state = AIC_CONNECTED;
714 sc->sc_nexus = acb;
715 ti = &sc->sc_tinfo[target];
716 ti->lubusy |= (1 << lun);
717 aic_setsync(sc, ti);
718
719 if (acb->flags & ACB_RESET)
720 aic_sched_msgout(sc, SEND_DEV_RESET);
721 else if (acb->flags & ACB_ABORT)
722 aic_sched_msgout(sc, SEND_ABORT);
723
724 /* Do an implicit RESTORE POINTERS. */
725 sc->sc_dp = acb->data_addr;
726 sc->sc_dleft = acb->data_length;
727 sc->sc_cp = (u_char *)&acb->scsipi_cmd;
728 sc->sc_cleft = acb->scsipi_cmd_length;
729
730 return (0);
731
732 reset:
733 aic_sched_msgout(sc, SEND_DEV_RESET);
734 return (1);
735
736 abort:
737 aic_sched_msgout(sc, SEND_ABORT);
738 return (1);
739 }
740
741 /*
743 * Schedule a SCSI operation. This has now been pulled out of the interrupt
744 * handler so that we may call it from aic_scsi_cmd and aic_done. This may
745 * save us an unecessary interrupt just to get things going. Should only be
746 * called when state == AIC_IDLE and at bio pl.
747 */
748 void
749 aic_sched(sc)
750 register struct aic_softc *sc;
751 {
752 struct aic_acb *acb;
753 struct scsipi_link *sc_link;
754 struct aic_tinfo *ti;
755 bus_space_tag_t iot = sc->sc_iot;
756 bus_space_handle_t ioh = sc->sc_ioh;
757
758 /*
759 * Find first acb in ready queue that is for a target/lunit pair that
760 * is not busy.
761 */
762 bus_space_write_1(iot, ioh, CLRSINT1,
763 CLRSELTIMO | CLRBUSFREE | CLRSCSIPERR);
764 for (acb = sc->ready_list.tqh_first; acb != NULL;
765 acb = acb->chain.tqe_next) {
766 sc_link = acb->xs->sc_link;
767 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
768 if ((ti->lubusy & (1 << sc_link->scsipi_scsi.lun)) == 0) {
769 AIC_MISC(("selecting %d:%d ",
770 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
771 TAILQ_REMOVE(&sc->ready_list, acb, chain);
772 sc->sc_nexus = acb;
773 aic_select(sc, acb);
774 return;
775 } else
776 AIC_MISC(("%d:%d busy\n",
777 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
778 }
779 AIC_MISC(("idle "));
780 /* Nothing to start; just enable reselections and wait. */
781 bus_space_write_1(iot, ioh, SIMODE0, ENSELDI);
782 bus_space_write_1(iot, ioh, SIMODE1, ENSCSIRST);
783 bus_space_write_1(iot, ioh, SCSISEQ, ENRESELI);
784 }
785
786 void
788 aic_sense(sc, acb)
789 struct aic_softc *sc;
790 struct aic_acb *acb;
791 {
792 struct scsipi_xfer *xs = acb->xs;
793 struct scsipi_link *sc_link = xs->sc_link;
794 struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
795 struct scsipi_sense *ss = (void *)&acb->scsipi_cmd;
796
797 AIC_MISC(("requesting sense "));
798 /* Next, setup a request sense command block */
799 bzero(ss, sizeof(*ss));
800 ss->opcode = REQUEST_SENSE;
801 ss->byte2 = sc_link->scsipi_scsi.lun << 5;
802 ss->length = sizeof(struct scsipi_sense_data);
803 acb->scsipi_cmd_length = sizeof(*ss);
804 acb->data_addr = (char *)&xs->sense.scsi_sense;
805 acb->data_length = sizeof(struct scsipi_sense_data);
806 acb->flags |= ACB_SENSE;
807 ti->senses++;
808 if (acb->flags & ACB_NEXUS)
809 ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
810 if (acb == sc->sc_nexus) {
811 aic_select(sc, acb);
812 } else {
813 aic_dequeue(sc, acb);
814 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
815 if (sc->sc_state == AIC_IDLE)
816 aic_sched(sc);
817 }
818 }
819
820 /*
821 * POST PROCESSING OF SCSI_CMD (usually current)
822 */
823 void
824 aic_done(sc, acb)
825 struct aic_softc *sc;
826 struct aic_acb *acb;
827 {
828 struct scsipi_xfer *xs = acb->xs;
829 struct scsipi_link *sc_link = xs->sc_link;
830 struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
831
832 AIC_TRACE(("aic_done "));
833
834 /*
835 * Now, if we've come here with no error code, i.e. we've kept the
836 * initial XS_NOERROR, and the status code signals that we should
837 * check sense, we'll need to set up a request sense cmd block and
838 * push the command back into the ready queue *before* any other
839 * commands for this target/lunit, else we lose the sense info.
840 * We don't support chk sense conditions for the request sense cmd.
841 */
842 if (xs->error == XS_NOERROR) {
843 if (acb->flags & ACB_ABORT) {
844 xs->error = XS_DRIVER_STUFFUP;
845 } else if (acb->flags & ACB_SENSE) {
846 xs->error = XS_SENSE;
847 } else if (acb->target_stat == SCSI_CHECK) {
848 /* First, save the return values */
849 xs->resid = acb->data_length;
850 xs->status = acb->target_stat;
851 aic_sense(sc, acb);
852 return;
853 } else {
854 xs->resid = acb->data_length;
855 }
856 }
857
858 xs->flags |= ITSDONE;
859
860 #if AIC_DEBUG
861 if ((aic_debug & AIC_SHOWMISC) != 0) {
862 if (xs->resid != 0)
863 printf("resid=%d ", xs->resid);
864 if (xs->error == XS_SENSE)
865 printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
866 else
867 printf("error=%d\n", xs->error);
868 }
869 #endif
870
871 /*
872 * Remove the ACB from whatever queue it happens to be on.
873 */
874 if (acb->flags & ACB_NEXUS)
875 ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
876 if (acb == sc->sc_nexus) {
877 sc->sc_nexus = NULL;
878 sc->sc_state = AIC_IDLE;
879 aic_sched(sc);
880 } else
881 aic_dequeue(sc, acb);
882
883 aic_free_acb(sc, acb, xs->flags);
884 ti->cmds++;
885 scsipi_done(xs);
886 }
887
888 void
889 aic_dequeue(sc, acb)
890 struct aic_softc *sc;
891 struct aic_acb *acb;
892 {
893
894 if (acb->flags & ACB_NEXUS) {
895 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
896 } else {
897 TAILQ_REMOVE(&sc->ready_list, acb, chain);
898 }
899 }
900
901 /*
903 * INTERRUPT/PROTOCOL ENGINE
904 */
905
906 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
907 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
908 #define ISEXTMSG(m) ((m) == 0x01)
909
910 /*
911 * Precondition:
912 * The SCSI bus is already in the MSGI phase and there is a message byte
913 * on the bus, along with an asserted REQ signal.
914 */
915 void
916 aic_msgin(sc)
917 register struct aic_softc *sc;
918 {
919 bus_space_tag_t iot = sc->sc_iot;
920 bus_space_handle_t ioh = sc->sc_ioh;
921 u_char sstat1;
922 int n;
923
924 AIC_TRACE(("aic_msgin "));
925
926 if (sc->sc_prevphase == PH_MSGIN) {
927 /* This is a continuation of the previous message. */
928 n = sc->sc_imp - sc->sc_imess;
929 goto nextbyte;
930 }
931
932 /* This is a new MESSAGE IN phase. Clean up our state. */
933 sc->sc_flags &= ~AIC_DROP_MSGIN;
934
935 nextmsg:
936 n = 0;
937 sc->sc_imp = &sc->sc_imess[n];
938
939 nextbyte:
940 /*
941 * Read a whole message, but don't ack the last byte. If we reject the
942 * message, we have to assert ATN during the message transfer phase
943 * itself.
944 */
945 for (;;) {
946 for (;;) {
947 sstat1 = bus_space_read_1(iot, ioh, SSTAT1);
948 if ((sstat1 & (REQINIT | PHASECHG | BUSFREE)) != 0)
949 break;
950 /* Wait for REQINIT. XXX Need timeout. */
951 }
952 if ((sstat1 & (PHASECHG | BUSFREE)) != 0) {
953 /*
954 * Target left MESSAGE IN, probably because it
955 * a) noticed our ATN signal, or
956 * b) ran out of messages.
957 */
958 goto out;
959 }
960
961 /* If parity error, just dump everything on the floor. */
962 if ((sstat1 & SCSIPERR) != 0) {
963 sc->sc_flags |= AIC_DROP_MSGIN;
964 aic_sched_msgout(sc, SEND_PARITY_ERROR);
965 }
966
967 /* Gather incoming message bytes if needed. */
968 if ((sc->sc_flags & AIC_DROP_MSGIN) == 0) {
969 if (n >= AIC_MAX_MSG_LEN) {
970 (void) bus_space_read_1(iot, ioh, SCSIDAT);
971 sc->sc_flags |= AIC_DROP_MSGIN;
972 aic_sched_msgout(sc, SEND_REJECT);
973 } else {
974 *sc->sc_imp++ = bus_space_read_1(iot, ioh,
975 SCSIDAT);
976 n++;
977 /*
978 * This testing is suboptimal, but most
979 * messages will be of the one byte variety, so
980 * it should not affect performance
981 * significantly.
982 */
983 if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
984 break;
985 if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
986 break;
987 if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
988 n == sc->sc_imess[1] + 2)
989 break;
990 }
991 } else
992 (void) bus_space_read_1(iot, ioh, SCSIDAT);
993
994 /*
995 * If we reach this spot we're either:
996 * a) in the middle of a multi-byte message, or
997 * b) dropping bytes.
998 */
999 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
1000 /* Ack the last byte read. */
1001 (void) bus_space_read_1(iot, ioh, SCSIDAT);
1002 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
1003 while ((bus_space_read_1(iot, ioh, SCSISIG) & ACKI) != 0)
1004 ;
1005 }
1006
1007 AIC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1008
1009 /* We now have a complete message. Parse it. */
1010 switch (sc->sc_state) {
1011 struct aic_acb *acb;
1012 struct scsipi_link *sc_link;
1013 struct aic_tinfo *ti;
1014
1015 case AIC_CONNECTED:
1016 AIC_ASSERT(sc->sc_nexus != NULL);
1017 acb = sc->sc_nexus;
1018 ti = &sc->sc_tinfo[acb->xs->sc_link->scsipi_scsi.target];
1019
1020 switch (sc->sc_imess[0]) {
1021 case MSG_CMDCOMPLETE:
1022 if (sc->sc_dleft < 0) {
1023 sc_link = acb->xs->sc_link;
1024 printf("%s: %d extra bytes from %d:%d\n",
1025 sc->sc_dev.dv_xname, -sc->sc_dleft,
1026 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun);
1027 acb->data_length = 0;
1028 }
1029 acb->xs->resid = acb->data_length = sc->sc_dleft;
1030 sc->sc_state = AIC_CMDCOMPLETE;
1031 break;
1032
1033 case MSG_PARITY_ERROR:
1034 /* Resend the last message. */
1035 aic_sched_msgout(sc, sc->sc_lastmsg);
1036 break;
1037
1038 case MSG_MESSAGE_REJECT:
1039 AIC_MISC(("message rejected %02x ", sc->sc_lastmsg));
1040 switch (sc->sc_lastmsg) {
1041 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
1042 case SEND_IDENTIFY:
1043 ti->flags &= ~(DO_SYNC | DO_WIDE);
1044 ti->period = ti->offset = 0;
1045 aic_setsync(sc, ti);
1046 ti->width = 0;
1047 break;
1048 #endif
1049 #if AIC_USE_SYNCHRONOUS
1050 case SEND_SDTR:
1051 ti->flags &= ~DO_SYNC;
1052 ti->period = ti->offset = 0;
1053 aic_setsync(sc, ti);
1054 break;
1055 #endif
1056 #if AIC_USE_WIDE
1057 case SEND_WDTR:
1058 ti->flags &= ~DO_WIDE;
1059 ti->width = 0;
1060 break;
1061 #endif
1062 case SEND_INIT_DET_ERR:
1063 aic_sched_msgout(sc, SEND_ABORT);
1064 break;
1065 }
1066 break;
1067
1068 case MSG_NOOP:
1069 break;
1070
1071 case MSG_DISCONNECT:
1072 ti->dconns++;
1073 sc->sc_state = AIC_DISCONNECT;
1074 break;
1075
1076 case MSG_SAVEDATAPOINTER:
1077 acb->data_addr = sc->sc_dp;
1078 acb->data_length = sc->sc_dleft;
1079 break;
1080
1081 case MSG_RESTOREPOINTERS:
1082 sc->sc_dp = acb->data_addr;
1083 sc->sc_dleft = acb->data_length;
1084 sc->sc_cp = (u_char *)&acb->scsipi_cmd;
1085 sc->sc_cleft = acb->scsipi_cmd_length;
1086 break;
1087
1088 case MSG_EXTENDED:
1089 switch (sc->sc_imess[2]) {
1090 #if AIC_USE_SYNCHRONOUS
1091 case MSG_EXT_SDTR:
1092 if (sc->sc_imess[1] != 3)
1093 goto reject;
1094 ti->period = sc->sc_imess[3];
1095 ti->offset = sc->sc_imess[4];
1096 ti->flags &= ~DO_SYNC;
1097 if (ti->offset == 0) {
1098 } else if (ti->period < sc->sc_minsync ||
1099 ti->period > sc->sc_maxsync ||
1100 ti->offset > 8) {
1101 ti->period = ti->offset = 0;
1102 aic_sched_msgout(sc, SEND_SDTR);
1103 } else {
1104 scsi_print_addr(acb->xs->sc_link);
1105 printf("sync, offset %d, "
1106 "period %dnsec\n",
1107 ti->offset, ti->period * 4);
1108 }
1109 aic_setsync(sc, ti);
1110 break;
1111 #endif
1112
1113 #if AIC_USE_WIDE
1114 case MSG_EXT_WDTR:
1115 if (sc->sc_imess[1] != 2)
1116 goto reject;
1117 ti->width = sc->sc_imess[3];
1118 ti->flags &= ~DO_WIDE;
1119 if (ti->width == 0) {
1120 } else if (ti->width > AIC_MAX_WIDTH) {
1121 ti->width = 0;
1122 aic_sched_msgout(sc, SEND_WDTR);
1123 } else {
1124 scsi_print_addr(acb->xs->sc_link);
1125 printf("wide, width %d\n",
1126 1 << (3 + ti->width));
1127 }
1128 break;
1129 #endif
1130
1131 default:
1132 printf("%s: unrecognized MESSAGE EXTENDED; "
1133 "sending REJECT\n", sc->sc_dev.dv_xname);
1134 AIC_BREAK();
1135 goto reject;
1136 }
1137 break;
1138
1139 default:
1140 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1141 sc->sc_dev.dv_xname);
1142 AIC_BREAK();
1143 reject:
1144 aic_sched_msgout(sc, SEND_REJECT);
1145 break;
1146 }
1147 break;
1148
1149 case AIC_RESELECTED:
1150 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1151 printf("%s: reselect without IDENTIFY; "
1152 "sending DEVICE RESET\n", sc->sc_dev.dv_xname);
1153 AIC_BREAK();
1154 goto reset;
1155 }
1156
1157 (void) aic_reselect(sc, sc->sc_imess[0]);
1158 break;
1159
1160 default:
1161 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1162 sc->sc_dev.dv_xname);
1163 AIC_BREAK();
1164 reset:
1165 aic_sched_msgout(sc, SEND_DEV_RESET);
1166 break;
1167
1168 #ifdef notdef
1169 abort:
1170 aic_sched_msgout(sc, SEND_ABORT);
1171 break;
1172 #endif
1173 }
1174
1175 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
1176 /* Ack the last message byte. */
1177 (void) bus_space_read_1(iot, ioh, SCSIDAT);
1178 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
1179 while ((bus_space_read_1(iot, ioh, SCSISIG) & ACKI) != 0)
1180 ;
1181
1182 /* Go get the next message, if any. */
1183 goto nextmsg;
1184
1185 out:
1186 AIC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1187 }
1188
1189 /*
1190 * Send the highest priority, scheduled message.
1191 */
1192 void
1193 aic_msgout(sc)
1194 register struct aic_softc *sc;
1195 {
1196 bus_space_tag_t iot = sc->sc_iot;
1197 bus_space_handle_t ioh = sc->sc_ioh;
1198 #if AIC_USE_SYNCHRONOUS
1199 struct aic_tinfo *ti;
1200 #endif
1201 u_char sstat1;
1202 int n;
1203
1204 AIC_TRACE(("aic_msgout "));
1205
1206 /* Reset the FIFO. */
1207 bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO);
1208 /* Enable REQ/ACK protocol. */
1209 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
1210
1211 if (sc->sc_prevphase == PH_MSGOUT) {
1212 if (sc->sc_omp == sc->sc_omess) {
1213 /*
1214 * This is a retransmission.
1215 *
1216 * We get here if the target stayed in MESSAGE OUT
1217 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1218 * that all of the previously transmitted messages must
1219 * be sent again, in the same order. Therefore, we
1220 * requeue all the previously transmitted messages, and
1221 * start again from the top. Our simple priority
1222 * scheme keeps the messages in the right order.
1223 */
1224 AIC_MISC(("retransmitting "));
1225 sc->sc_msgpriq |= sc->sc_msgoutq;
1226 /*
1227 * Set ATN. If we're just sending a trivial 1-byte
1228 * message, we'll clear ATN later on anyway.
1229 */
1230 bus_space_write_1(iot, ioh, SCSISIG, PH_MSGOUT | ATNO);
1231 } else {
1232 /* This is a continuation of the previous message. */
1233 n = sc->sc_omp - sc->sc_omess;
1234 goto nextbyte;
1235 }
1236 }
1237
1238 /* No messages transmitted so far. */
1239 sc->sc_msgoutq = 0;
1240 sc->sc_lastmsg = 0;
1241
1242 nextmsg:
1243 /* Pick up highest priority message. */
1244 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1245 sc->sc_msgpriq &= ~sc->sc_currmsg;
1246 sc->sc_msgoutq |= sc->sc_currmsg;
1247
1248 /* Build the outgoing message data. */
1249 switch (sc->sc_currmsg) {
1250 case SEND_IDENTIFY:
1251 AIC_ASSERT(sc->sc_nexus != NULL);
1252 sc->sc_omess[0] =
1253 MSG_IDENTIFY(sc->sc_nexus->xs->sc_link->scsipi_scsi.lun, 1);
1254 n = 1;
1255 break;
1256
1257 #if AIC_USE_SYNCHRONOUS
1258 case SEND_SDTR:
1259 AIC_ASSERT(sc->sc_nexus != NULL);
1260 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
1261 sc->sc_omess[4] = MSG_EXTENDED;
1262 sc->sc_omess[3] = 3;
1263 sc->sc_omess[2] = MSG_EXT_SDTR;
1264 sc->sc_omess[1] = ti->period >> 2;
1265 sc->sc_omess[0] = ti->offset;
1266 n = 5;
1267 break;
1268 #endif
1269
1270 #if AIC_USE_WIDE
1271 case SEND_WDTR:
1272 AIC_ASSERT(sc->sc_nexus != NULL);
1273 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target];
1274 sc->sc_omess[3] = MSG_EXTENDED;
1275 sc->sc_omess[2] = 2;
1276 sc->sc_omess[1] = MSG_EXT_WDTR;
1277 sc->sc_omess[0] = ti->width;
1278 n = 4;
1279 break;
1280 #endif
1281
1282 case SEND_DEV_RESET:
1283 sc->sc_flags |= AIC_ABORTING;
1284 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1285 n = 1;
1286 break;
1287
1288 case SEND_REJECT:
1289 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1290 n = 1;
1291 break;
1292
1293 case SEND_PARITY_ERROR:
1294 sc->sc_omess[0] = MSG_PARITY_ERROR;
1295 n = 1;
1296 break;
1297
1298 case SEND_INIT_DET_ERR:
1299 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1300 n = 1;
1301 break;
1302
1303 case SEND_ABORT:
1304 sc->sc_flags |= AIC_ABORTING;
1305 sc->sc_omess[0] = MSG_ABORT;
1306 n = 1;
1307 break;
1308
1309 default:
1310 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1311 sc->sc_dev.dv_xname);
1312 AIC_BREAK();
1313 sc->sc_omess[0] = MSG_NOOP;
1314 n = 1;
1315 break;
1316 }
1317 sc->sc_omp = &sc->sc_omess[n];
1318
1319 nextbyte:
1320 /* Send message bytes. */
1321 for (;;) {
1322 for (;;) {
1323 sstat1 = bus_space_read_1(iot, ioh, SSTAT1);
1324 if ((sstat1 & (REQINIT | PHASECHG | BUSFREE)) != 0)
1325 break;
1326 /* Wait for REQINIT. XXX Need timeout. */
1327 }
1328 if ((sstat1 & (PHASECHG | BUSFREE)) != 0) {
1329 /*
1330 * Target left MESSAGE OUT, possibly to reject
1331 * our message.
1332 *
1333 * If this is the last message being sent, then we
1334 * deassert ATN, since either the target is going to
1335 * ignore this message, or it's going to ask for a
1336 * retransmission via MESSAGE PARITY ERROR (in which
1337 * case we reassert ATN anyway).
1338 */
1339 if (sc->sc_msgpriq == 0)
1340 bus_space_write_1(iot, ioh, CLRSINT1, CLRATNO);
1341 goto out;
1342 }
1343
1344 /* Clear ATN before last byte if this is the last message. */
1345 if (n == 1 && sc->sc_msgpriq == 0)
1346 bus_space_write_1(iot, ioh, CLRSINT1, CLRATNO);
1347 /* Send message byte. */
1348 bus_space_write_1(iot, ioh, SCSIDAT, *--sc->sc_omp);
1349 --n;
1350 /* Keep track of the last message we've sent any bytes of. */
1351 sc->sc_lastmsg = sc->sc_currmsg;
1352 /* Wait for ACK to be negated. XXX Need timeout. */
1353 while ((bus_space_read_1(iot, ioh, SCSISIG) & ACKI) != 0)
1354 ;
1355
1356 if (n == 0)
1357 break;
1358 }
1359
1360 /* We get here only if the entire message has been transmitted. */
1361 if (sc->sc_msgpriq != 0) {
1362 /* There are more outgoing messages. */
1363 goto nextmsg;
1364 }
1365
1366 /*
1367 * The last message has been transmitted. We need to remember the last
1368 * message transmitted (in case the target switches to MESSAGE IN phase
1369 * and sends a MESSAGE REJECT), and the list of messages transmitted
1370 * this time around (in case the target stays in MESSAGE OUT phase to
1371 * request a retransmit).
1372 */
1373
1374 out:
1375 /* Disable REQ/ACK protocol. */
1376 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
1377 }
1378
1379 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the
1381 * aic6360
1382 * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
1383 * and ACK deasserted (i.e. waiting for a data byte)
1384 * This new revision has been optimized (I tried) to make the common case fast,
1385 * and the rarer cases (as a result) somewhat more comlex
1386 */
1387 int
1388 aic_dataout_pio(sc, p, n)
1389 register struct aic_softc *sc;
1390 u_char *p;
1391 int n;
1392 {
1393 bus_space_tag_t iot = sc->sc_iot;
1394 bus_space_handle_t ioh = sc->sc_ioh;
1395 register u_char dmastat = 0;
1396 int out = 0;
1397 #define DOUTAMOUNT 128 /* Full FIFO */
1398
1399 AIC_MISC(("%02x%02x ", bus_space_read_1(iot, ioh, FIFOSTAT),
1400 bus_space_read_1(iot, ioh, SSTAT2)));
1401
1402 /* Clear host FIFO and counter. */
1403 bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO | WRITE);
1404 /* Enable FIFOs. */
1405 bus_space_write_1(iot, ioh, DMACNTRL0, ENDMA | DWORDPIO | WRITE);
1406 bus_space_write_1(iot, ioh, SXFRCTL0, SCSIEN | DMAEN | CHEN);
1407
1408 /* Turn off ENREQINIT for now. */
1409 bus_space_write_1(iot, ioh, SIMODE1,
1410 ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENPHASECHG);
1411
1412 /* I have tried to make the main loop as tight as possible. This
1413 * means that some of the code following the loop is a bit more
1414 * complex than otherwise.
1415 */
1416 while (n > 0) {
1417 for (;;) {
1418 dmastat = bus_space_read_1(iot, ioh, DMASTAT);
1419 if ((dmastat & (DFIFOEMP | INTSTAT)) != 0)
1420 break;
1421 }
1422
1423 if ((dmastat & INTSTAT) != 0)
1424 goto phasechange;
1425
1426 if (n >= DOUTAMOUNT) {
1427 n -= DOUTAMOUNT;
1428 out += DOUTAMOUNT;
1429
1430 #if AIC_USE_DWORDS
1431 bus_space_write_multi_4(iot, ioh, DMADATALONG,
1432 (u_int32_t *) p, DOUTAMOUNT >> 2);
1433 #else
1434 bus_space_write_multi_2(iot, ioh, DMADATA,
1435 (u_int16_t *) p, DOUTAMOUNT >> 1);
1436 #endif
1437
1438 p += DOUTAMOUNT;
1439 } else {
1440 register int xfer;
1441
1442 xfer = n;
1443 AIC_MISC(("%d> ", xfer));
1444
1445 n -= xfer;
1446 out += xfer;
1447
1448 #if AIC_USE_DWORDS
1449 if (xfer >= 12) {
1450 bus_space_write_multi_4(iot, ioh, DMADATALONG,
1451 (u_int32_t *) p, xfer >> 2);
1452 p += xfer & ~3;
1453 xfer &= 3;
1454 }
1455 #else
1456 if (xfer >= 8) {
1457 bus_space_write_multi_2(iot, ioh, DMADATA,
1458 (u_int16_t *) p, xfer >> 1);
1459 p += xfer & ~1;
1460 xfer &= 1;
1461 }
1462 #endif
1463
1464 if (xfer > 0) {
1465 bus_space_write_1(iot, ioh, DMACNTRL0,
1466 ENDMA | B8MODE | WRITE);
1467 bus_space_write_multi_1(iot, ioh, DMADATA,
1468 p, xfer);
1469 p += xfer;
1470 bus_space_write_1(iot, ioh, DMACNTRL0,
1471 ENDMA | DWORDPIO | WRITE);
1472 }
1473 }
1474 }
1475
1476 if (out == 0) {
1477 bus_space_write_1(iot, ioh, SXFRCTL1, BITBUCKET);
1478 for (;;) {
1479 if ((bus_space_read_1(iot, ioh, DMASTAT) & INTSTAT)
1480 != 0)
1481 break;
1482 }
1483 bus_space_write_1(iot, ioh, SXFRCTL1, 0);
1484 AIC_MISC(("extra data "));
1485 } else {
1486 /* See the bytes off chip */
1487 for (;;) {
1488 dmastat = bus_space_read_1(iot, ioh, DMASTAT);
1489 if ((dmastat & INTSTAT) != 0)
1490 goto phasechange;
1491 if ((dmastat & DFIFOEMP) != 0 &&
1492 (bus_space_read_1(iot, ioh, SSTAT2) & SEMPTY) != 0)
1493 break;
1494 }
1495 }
1496
1497 phasechange:
1498 if ((dmastat & INTSTAT) != 0) {
1499 /* Some sort of phase change. */
1500 int amount;
1501
1502 /* Stop transfers, do some accounting */
1503 amount = bus_space_read_1(iot, ioh, FIFOSTAT)
1504 + (bus_space_read_1(iot, ioh, SSTAT2) & 15);
1505 if (amount > 0) {
1506 out -= amount;
1507 bus_space_write_1(iot, ioh, DMACNTRL0,
1508 RSTFIFO | WRITE);
1509 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | CLRCH);
1510 AIC_MISC(("+%d ", amount));
1511 }
1512 }
1513
1514 /* Turn on ENREQINIT again. */
1515 bus_space_write_1(iot, ioh, SIMODE1,
1516 ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
1517
1518 /* Stop the FIFO data path. */
1519 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
1520 bus_space_write_1(iot, ioh, DMACNTRL0, 0);
1521
1522 return out;
1523 }
1524
1525 /* aic_datain_pio: perform data transfers using the FIFO datapath in the
1527 * aic6360
1528 * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
1529 * and ACK deasserted (i.e. at least one byte is ready).
1530 * For now, uses a pretty dumb algorithm, hangs around until all data has been
1531 * transferred. This, is OK for fast targets, but not so smart for slow
1532 * targets which don't disconnect or for huge transfers.
1533 */
1534 int
1535 aic_datain_pio(sc, p, n)
1536 register struct aic_softc *sc;
1537 u_char *p;
1538 int n;
1539 {
1540 bus_space_tag_t iot = sc->sc_iot;
1541 bus_space_handle_t ioh = sc->sc_ioh;
1542 register u_char dmastat;
1543 int in = 0;
1544 #define DINAMOUNT 128 /* Full FIFO */
1545
1546 AIC_MISC(("%02x%02x ", bus_space_read_1(iot, ioh, FIFOSTAT),
1547 bus_space_read_1(iot, ioh, SSTAT2)));
1548
1549 /* Clear host FIFO and counter. */
1550 bus_space_write_1(iot, ioh, DMACNTRL0, RSTFIFO);
1551 /* Enable FIFOs. */
1552 bus_space_write_1(iot, ioh, DMACNTRL0, ENDMA | DWORDPIO);
1553 bus_space_write_1(iot, ioh, SXFRCTL0, SCSIEN | DMAEN | CHEN);
1554
1555 /* Turn off ENREQINIT for now. */
1556 bus_space_write_1(iot, ioh, SIMODE1,
1557 ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENPHASECHG);
1558
1559 /* We leave this loop if one or more of the following is true:
1560 * a) phase != PH_DATAIN && FIFOs are empty
1561 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
1562 */
1563 while (n > 0) {
1564 /* Wait for fifo half full or phase mismatch */
1565 for (;;) {
1566 dmastat = bus_space_read_1(iot, ioh, DMASTAT);
1567 if ((dmastat & (DFIFOFULL | INTSTAT)) != 0)
1568 break;
1569 }
1570
1571 if ((dmastat & DFIFOFULL) != 0) {
1572 n -= DINAMOUNT;
1573 in += DINAMOUNT;
1574
1575 #if AIC_USE_DWORDS
1576 bus_space_read_multi_4(iot, ioh, DMADATALONG,
1577 (u_int32_t *) p, DINAMOUNT >> 2);
1578 #else
1579 bus_space_read_multi_2(iot, ioh, DMADATA,
1580 (u_int16_t *) p, DINAMOUNT >> 1);
1581 #endif
1582
1583 p += DINAMOUNT;
1584 } else {
1585 register int xfer;
1586
1587 xfer = min(bus_space_read_1(iot, ioh, FIFOSTAT), n);
1588 AIC_MISC((">%d ", xfer));
1589
1590 n -= xfer;
1591 in += xfer;
1592
1593 #if AIC_USE_DWORDS
1594 if (xfer >= 12) {
1595 bus_space_read_multi_4(iot, ioh, DMADATALONG,
1596 (u_int32_t *) p, xfer >> 2);
1597 p += xfer & ~3;
1598 xfer &= 3;
1599 }
1600 #else
1601 if (xfer >= 8) {
1602 bus_space_read_multi_2(iot, ioh, DMADATA,
1603 (u_int16_t *) p, xfer >> 1);
1604 p += xfer & ~1;
1605 xfer &= 1;
1606 }
1607 #endif
1608
1609 if (xfer > 0) {
1610 bus_space_write_1(iot, ioh, DMACNTRL0,
1611 ENDMA | B8MODE);
1612 bus_space_read_multi_1(iot, ioh, DMADATA,
1613 p, xfer);
1614 p += xfer;
1615 bus_space_write_1(iot, ioh, DMACNTRL0,
1616 ENDMA | DWORDPIO);
1617 }
1618 }
1619
1620 if ((dmastat & INTSTAT) != 0)
1621 goto phasechange;
1622 }
1623
1624 /* Some SCSI-devices are rude enough to transfer more data than what
1625 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
1626 * requested 512. Test for progress, i.e. real transfers. If no real
1627 * transfers have been performed (n is probably already zero) and the
1628 * FIFO is not empty, waste some bytes....
1629 */
1630 if (in == 0) {
1631 bus_space_write_1(iot, ioh, SXFRCTL1, BITBUCKET);
1632 for (;;) {
1633 if ((bus_space_read_1(iot, ioh, DMASTAT) & INTSTAT)
1634 != 0)
1635 break;
1636 }
1637 bus_space_write_1(iot, ioh, SXFRCTL1, 0);
1638 AIC_MISC(("extra data "));
1639 }
1640
1641 phasechange:
1642 /* Turn on ENREQINIT again. */
1643 bus_space_write_1(iot, ioh, SIMODE1,
1644 ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
1645
1646 /* Stop the FIFO data path. */
1647 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
1648 bus_space_write_1(iot, ioh, DMACNTRL0, 0);
1649
1650 return in;
1651 }
1652
1653 /*
1655 * This is the workhorse routine of the driver.
1656 * Deficiencies (for now):
1657 * 1) always uses programmed I/O
1658 */
1659 int
1660 aicintr(arg)
1661 void *arg;
1662 {
1663 register struct aic_softc *sc = arg;
1664 bus_space_tag_t iot = sc->sc_iot;
1665 bus_space_handle_t ioh = sc->sc_ioh;
1666 u_char sstat0, sstat1;
1667 register struct aic_acb *acb;
1668 register struct scsipi_link *sc_link;
1669 struct aic_tinfo *ti;
1670 int n;
1671
1672 /*
1673 * Clear INTEN. We enable it again before returning. This makes the
1674 * interrupt esssentially level-triggered.
1675 */
1676 bus_space_write_1(iot, ioh, DMACNTRL0, 0);
1677
1678 AIC_TRACE(("aicintr "));
1679
1680 loop:
1681 /*
1682 * First check for abnormal conditions, such as reset.
1683 */
1684 sstat1 = bus_space_read_1(iot, ioh, SSTAT1);
1685 AIC_MISC(("sstat1:0x%02x ", sstat1));
1686
1687 if ((sstat1 & SCSIRSTI) != 0) {
1688 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
1689 goto reset;
1690 }
1691
1692 /*
1693 * Check for less serious errors.
1694 */
1695 if ((sstat1 & SCSIPERR) != 0) {
1696 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
1697 bus_space_write_1(iot, ioh, CLRSINT1, CLRSCSIPERR);
1698 if (sc->sc_prevphase == PH_MSGIN) {
1699 sc->sc_flags |= AIC_DROP_MSGIN;
1700 aic_sched_msgout(sc, SEND_PARITY_ERROR);
1701 } else
1702 aic_sched_msgout(sc, SEND_INIT_DET_ERR);
1703 }
1704
1705 /*
1706 * If we're not already busy doing something test for the following
1707 * conditions:
1708 * 1) We have been reselected by something
1709 * 2) We have selected something successfully
1710 * 3) Our selection process has timed out
1711 * 4) This is really a bus free interrupt just to get a new command
1712 * going?
1713 * 5) Spurious interrupt?
1714 */
1715 switch (sc->sc_state) {
1716 case AIC_IDLE:
1717 case AIC_SELECTING:
1718 sstat0 = bus_space_read_1(iot, ioh, SSTAT0);
1719 AIC_MISC(("sstat0:0x%02x ", sstat0));
1720
1721 if ((sstat0 & TARGET) != 0) {
1722 /*
1723 * We don't currently support target mode.
1724 */
1725 printf("%s: target mode selected; going to BUS FREE\n",
1726 sc->sc_dev.dv_xname);
1727 bus_space_write_1(iot, ioh, SCSISIG, 0);
1728
1729 goto sched;
1730 } else if ((sstat0 & SELDI) != 0) {
1731 AIC_MISC(("reselected "));
1732
1733 /*
1734 * If we're trying to select a target ourselves,
1735 * push our command back into the ready list.
1736 */
1737 if (sc->sc_state == AIC_SELECTING) {
1738 AIC_MISC(("backoff selector "));
1739 AIC_ASSERT(sc->sc_nexus != NULL);
1740 acb = sc->sc_nexus;
1741 sc->sc_nexus = NULL;
1742 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
1743 }
1744
1745 /* Save reselection ID. */
1746 sc->sc_selid = bus_space_read_1(iot, ioh, SELID);
1747
1748 sc->sc_state = AIC_RESELECTED;
1749 } else if ((sstat0 & SELDO) != 0) {
1750 AIC_MISC(("selected "));
1751
1752 /* We have selected a target. Things to do:
1753 * a) Determine what message(s) to send.
1754 * b) Verify that we're still selecting the target.
1755 * c) Mark device as busy.
1756 */
1757 if (sc->sc_state != AIC_SELECTING) {
1758 printf("%s: selection out while idle; "
1759 "resetting\n", sc->sc_dev.dv_xname);
1760 AIC_BREAK();
1761 goto reset;
1762 }
1763 AIC_ASSERT(sc->sc_nexus != NULL);
1764 acb = sc->sc_nexus;
1765 sc_link = acb->xs->sc_link;
1766 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1767
1768 sc->sc_msgpriq = SEND_IDENTIFY;
1769 if (acb->flags & ACB_RESET)
1770 sc->sc_msgpriq |= SEND_DEV_RESET;
1771 else if (acb->flags & ACB_ABORT)
1772 sc->sc_msgpriq |= SEND_ABORT;
1773 else {
1774 #if AIC_USE_SYNCHRONOUS
1775 if ((ti->flags & DO_SYNC) != 0)
1776 sc->sc_msgpriq |= SEND_SDTR;
1777 #endif
1778 #if AIC_USE_WIDE
1779 if ((ti->flags & DO_WIDE) != 0)
1780 sc->sc_msgpriq |= SEND_WDTR;
1781 #endif
1782 }
1783
1784 acb->flags |= ACB_NEXUS;
1785 ti->lubusy |= (1 << sc_link->scsipi_scsi.lun);
1786
1787 /* Do an implicit RESTORE POINTERS. */
1788 sc->sc_dp = acb->data_addr;
1789 sc->sc_dleft = acb->data_length;
1790 sc->sc_cp = (u_char *)&acb->scsipi_cmd;
1791 sc->sc_cleft = acb->scsipi_cmd_length;
1792
1793 /* On our first connection, schedule a timeout. */
1794 if ((acb->xs->flags & SCSI_POLL) == 0)
1795 timeout(aic_timeout, acb,
1796 (acb->timeout * hz) / 1000);
1797
1798 sc->sc_state = AIC_CONNECTED;
1799 } else if ((sstat1 & SELTO) != 0) {
1800 AIC_MISC(("selection timeout "));
1801
1802 if (sc->sc_state != AIC_SELECTING) {
1803 printf("%s: selection timeout while idle; "
1804 "resetting\n", sc->sc_dev.dv_xname);
1805 AIC_BREAK();
1806 goto reset;
1807 }
1808 AIC_ASSERT(sc->sc_nexus != NULL);
1809 acb = sc->sc_nexus;
1810
1811 bus_space_write_1(iot, ioh, SXFRCTL1, 0);
1812 bus_space_write_1(iot, ioh, SCSISEQ, ENRESELI);
1813 bus_space_write_1(iot, ioh, CLRSINT1, CLRSELTIMO);
1814 delay(250);
1815
1816 acb->xs->error = XS_SELTIMEOUT;
1817 goto finish;
1818 } else {
1819 if (sc->sc_state != AIC_IDLE) {
1820 printf("%s: BUS FREE while not idle; "
1821 "state=%d\n",
1822 sc->sc_dev.dv_xname, sc->sc_state);
1823 AIC_BREAK();
1824 goto out;
1825 }
1826
1827 goto sched;
1828 }
1829
1830 /*
1831 * Turn off selection stuff, and prepare to catch bus free
1832 * interrupts, parity errors, and phase changes.
1833 */
1834 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | CLRSTCNT | CLRCH);
1835 bus_space_write_1(iot, ioh, SXFRCTL1, 0);
1836 bus_space_write_1(iot, ioh, SCSISEQ, ENAUTOATNP);
1837 bus_space_write_1(iot, ioh, CLRSINT0, CLRSELDI | CLRSELDO);
1838 bus_space_write_1(iot, ioh, CLRSINT1,
1839 CLRBUSFREE | CLRPHASECHG);
1840 bus_space_write_1(iot, ioh, SIMODE0, 0);
1841 bus_space_write_1(iot, ioh, SIMODE1,
1842 ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT |
1843 ENPHASECHG);
1844
1845 sc->sc_flags = 0;
1846 sc->sc_prevphase = PH_INVALID;
1847 goto dophase;
1848 }
1849
1850 if ((sstat1 & BUSFREE) != 0) {
1851 /* We've gone to BUS FREE phase. */
1852 bus_space_write_1(iot, ioh, CLRSINT1,
1853 CLRBUSFREE | CLRPHASECHG);
1854
1855 switch (sc->sc_state) {
1856 case AIC_RESELECTED:
1857 goto sched;
1858
1859 case AIC_CONNECTED:
1860 AIC_ASSERT(sc->sc_nexus != NULL);
1861 acb = sc->sc_nexus;
1862
1863 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
1864 if (sc->sc_prevphase == PH_MSGOUT) {
1865 /*
1866 * If the target went to BUS FREE phase during
1867 * or immediately after sending a SDTR or WDTR
1868 * message, disable negotiation.
1869 */
1870 sc_link = acb->xs->sc_link;
1871 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1872 switch (sc->sc_lastmsg) {
1873 #if AIC_USE_SYNCHRONOUS
1874 case SEND_SDTR:
1875 ti->flags &= ~DO_SYNC;
1876 ti->period = ti->offset = 0;
1877 break;
1878 #endif
1879 #if AIC_USE_WIDE
1880 case SEND_WDTR:
1881 ti->flags &= ~DO_WIDE;
1882 ti->width = 0;
1883 break;
1884 #endif
1885 }
1886 }
1887 #endif
1888
1889 if ((sc->sc_flags & AIC_ABORTING) == 0) {
1890 /*
1891 * Section 5.1.1 of the SCSI 2 spec suggests
1892 * issuing a REQUEST SENSE following an
1893 * unexpected disconnect. Some devices go into
1894 * a contingent allegiance condition when
1895 * disconnecting, and this is necessary to
1896 * clean up their state.
1897 */
1898 printf("%s: unexpected disconnect; "
1899 "sending REQUEST SENSE\n",
1900 sc->sc_dev.dv_xname);
1901 AIC_BREAK();
1902 aic_sense(sc, acb);
1903 goto out;
1904 }
1905
1906 acb->xs->error = XS_DRIVER_STUFFUP;
1907 goto finish;
1908
1909 case AIC_DISCONNECT:
1910 AIC_ASSERT(sc->sc_nexus != NULL);
1911 acb = sc->sc_nexus;
1912 #if 1 /* XXXX */
1913 acb->data_addr = sc->sc_dp;
1914 acb->data_length = sc->sc_dleft;
1915 #endif
1916 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1917 sc->sc_nexus = NULL;
1918 goto sched;
1919
1920 case AIC_CMDCOMPLETE:
1921 AIC_ASSERT(sc->sc_nexus != NULL);
1922 acb = sc->sc_nexus;
1923 goto finish;
1924 }
1925 }
1926
1927 bus_space_write_1(iot, ioh, CLRSINT1, CLRPHASECHG);
1928
1929 dophase:
1930 if ((sstat1 & REQINIT) == 0) {
1931 /* Wait for REQINIT. */
1932 goto out;
1933 }
1934
1935 sc->sc_phase = bus_space_read_1(iot, ioh, SCSISIG) & PH_MASK;
1936 bus_space_write_1(iot, ioh, SCSISIG, sc->sc_phase);
1937
1938 switch (sc->sc_phase) {
1939 case PH_MSGOUT:
1940 if (sc->sc_state != AIC_CONNECTED &&
1941 sc->sc_state != AIC_RESELECTED)
1942 break;
1943 aic_msgout(sc);
1944 sc->sc_prevphase = PH_MSGOUT;
1945 goto loop;
1946
1947 case PH_MSGIN:
1948 if (sc->sc_state != AIC_CONNECTED &&
1949 sc->sc_state != AIC_RESELECTED)
1950 break;
1951 aic_msgin(sc);
1952 sc->sc_prevphase = PH_MSGIN;
1953 goto loop;
1954
1955 case PH_CMD:
1956 if (sc->sc_state != AIC_CONNECTED)
1957 break;
1958 #if AIC_DEBUG
1959 if ((aic_debug & AIC_SHOWMISC) != 0) {
1960 AIC_ASSERT(sc->sc_nexus != NULL);
1961 acb = sc->sc_nexus;
1962 printf("cmd=0x%02x+%d ",
1963 acb->scsipi_cmd.opcode, acb->scsipi_cmd_length-1);
1964 }
1965 #endif
1966 n = aic_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
1967 sc->sc_cp += n;
1968 sc->sc_cleft -= n;
1969 sc->sc_prevphase = PH_CMD;
1970 goto loop;
1971
1972 case PH_DATAOUT:
1973 if (sc->sc_state != AIC_CONNECTED)
1974 break;
1975 AIC_MISC(("dataout %d ", sc->sc_dleft));
1976 n = aic_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
1977 sc->sc_dp += n;
1978 sc->sc_dleft -= n;
1979 sc->sc_prevphase = PH_DATAOUT;
1980 goto loop;
1981
1982 case PH_DATAIN:
1983 if (sc->sc_state != AIC_CONNECTED)
1984 break;
1985 AIC_MISC(("datain %d ", sc->sc_dleft));
1986 n = aic_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
1987 sc->sc_dp += n;
1988 sc->sc_dleft -= n;
1989 sc->sc_prevphase = PH_DATAIN;
1990 goto loop;
1991
1992 case PH_STAT:
1993 if (sc->sc_state != AIC_CONNECTED)
1994 break;
1995 AIC_ASSERT(sc->sc_nexus != NULL);
1996 acb = sc->sc_nexus;
1997 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN | SPIOEN);
1998 acb->target_stat = bus_space_read_1(iot, ioh, SCSIDAT);
1999 bus_space_write_1(iot, ioh, SXFRCTL0, CHEN);
2000 AIC_MISC(("target_stat=0x%02x ", acb->target_stat));
2001 sc->sc_prevphase = PH_STAT;
2002 goto loop;
2003 }
2004
2005 printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
2006 AIC_BREAK();
2007 reset:
2008 aic_init(sc);
2009 return 1;
2010
2011 finish:
2012 untimeout(aic_timeout, acb);
2013 aic_done(sc, acb);
2014 goto out;
2015
2016 sched:
2017 sc->sc_state = AIC_IDLE;
2018 aic_sched(sc);
2019 goto out;
2020
2021 out:
2022 bus_space_write_1(iot, ioh, DMACNTRL0, INTEN);
2023 return 1;
2024 }
2025
2026 void
2027 aic_abort(sc, acb)
2028 struct aic_softc *sc;
2029 struct aic_acb *acb;
2030 {
2031
2032 /* 2 secs for the abort */
2033 acb->timeout = AIC_ABORT_TIMEOUT;
2034 acb->flags |= ACB_ABORT;
2035
2036 if (acb == sc->sc_nexus) {
2037 /*
2038 * If we're still selecting, the message will be scheduled
2039 * after selection is complete.
2040 */
2041 if (sc->sc_state == AIC_CONNECTED)
2042 aic_sched_msgout(sc, SEND_ABORT);
2043 } else {
2044 aic_dequeue(sc, acb);
2045 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2046 if (sc->sc_state == AIC_IDLE)
2047 aic_sched(sc);
2048 }
2049 }
2050
2051 void
2052 aic_timeout(arg)
2053 void *arg;
2054 {
2055 struct aic_acb *acb = arg;
2056 struct scsipi_xfer *xs = acb->xs;
2057 struct scsipi_link *sc_link = xs->sc_link;
2058 struct aic_softc *sc = sc_link->adapter_softc;
2059 int s;
2060
2061 scsi_print_addr(sc_link);
2062 printf("timed out");
2063
2064 s = splbio();
2065
2066 if (acb->flags & ACB_ABORT) {
2067 /* abort timed out */
2068 printf(" AGAIN\n");
2069 /* XXX Must reset! */
2070 } else {
2071 /* abort the operation that has timed out */
2072 printf("\n");
2073 acb->xs->error = XS_TIMEOUT;
2074 aic_abort(sc, acb);
2075 }
2076
2077 splx(s);
2078 }
2079
2080 #ifdef AIC_DEBUG
2082 /*
2083 * The following functions are mostly used for debugging purposes, either
2084 * directly called from the driver or from the kernel debugger.
2085 */
2086
2087 void
2088 aic_show_scsi_cmd(acb)
2089 struct aic_acb *acb;
2090 {
2091 u_char *b = (u_char *)&acb->scsipi_cmd;
2092 struct scsipi_link *sc_link = acb->xs->sc_link;
2093 int i;
2094
2095 scsi_print_addr(sc_link);
2096 if ((acb->xs->flags & SCSI_RESET) == 0) {
2097 for (i = 0; i < acb->scsipi_cmd_length; i++) {
2098 if (i)
2099 printf(",");
2100 printf("%x", b[i]);
2101 }
2102 printf("\n");
2103 } else
2104 printf("RESET\n");
2105 }
2106
2107 void
2108 aic_print_acb(acb)
2109 struct aic_acb *acb;
2110 {
2111
2112 printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags);
2113 printf(" dp=%p dleft=%d target_stat=%x\n",
2114 acb->data_addr, acb->data_length, acb->target_stat);
2115 aic_show_scsi_cmd(acb);
2116 }
2117
2118 void
2119 aic_print_active_acb()
2120 {
2121 extern struct cfdriver aic_cd;
2122 struct aic_acb *acb;
2123 struct aic_softc *sc = aic_cd.cd_devs[0];
2124
2125 printf("ready list:\n");
2126 for (acb = sc->ready_list.tqh_first; acb != NULL;
2127 acb = acb->chain.tqe_next)
2128 aic_print_acb(acb);
2129 printf("nexus:\n");
2130 if (sc->sc_nexus != NULL)
2131 aic_print_acb(sc->sc_nexus);
2132 printf("nexus list:\n");
2133 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2134 acb = acb->chain.tqe_next)
2135 aic_print_acb(acb);
2136 }
2137
2138 void
2139 aic_dump6360(sc)
2140 struct aic_softc *sc;
2141 {
2142 bus_space_tag_t iot = sc->sc_iot;
2143 bus_space_handle_t ioh = sc->sc_ioh;
2144
2145 printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIG=%x\n",
2146 bus_space_read_1(iot, ioh, SCSISEQ),
2147 bus_space_read_1(iot, ioh, SXFRCTL0),
2148 bus_space_read_1(iot, ioh, SXFRCTL1),
2149 bus_space_read_1(iot, ioh, SCSISIG));
2150 printf(" SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
2151 bus_space_read_1(iot, ioh, SSTAT0),
2152 bus_space_read_1(iot, ioh, SSTAT1),
2153 bus_space_read_1(iot, ioh, SSTAT2),
2154 bus_space_read_1(iot, ioh, SSTAT3),
2155 bus_space_read_1(iot, ioh, SSTAT4));
2156 printf(" SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x "
2157 "DMASTAT=%x\n",
2158 bus_space_read_1(iot, ioh, SIMODE0),
2159 bus_space_read_1(iot, ioh, SIMODE1),
2160 bus_space_read_1(iot, ioh, DMACNTRL0),
2161 bus_space_read_1(iot, ioh, DMACNTRL1),
2162 bus_space_read_1(iot, ioh, DMASTAT));
2163 printf(" FIFOSTAT=%d SCSIBUS=0x%x\n",
2164 bus_space_read_1(iot, ioh, FIFOSTAT),
2165 bus_space_read_1(iot, ioh, SCSIBUS));
2166 }
2167
2168 void
2169 aic_dump_driver(sc)
2170 struct aic_softc *sc;
2171 {
2172 struct aic_tinfo *ti;
2173 int i;
2174
2175 printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2176 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x "
2177 "currmsg=%x\n",
2178 sc->sc_state, sc->sc_imess[0],
2179 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2180 for (i = 0; i < 7; i++) {
2181 ti = &sc->sc_tinfo[i];
2182 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2183 i, ti->cmds, ti->dconns, ti->touts);
2184 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2185 }
2186 }
2187 #endif
2188