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