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