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