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