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