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