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