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