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