1 /* $NetBSD: mb89352.c,v 1.50 2009/11/12 20:14:04 dyoung Exp $ */
2 /* NecBSD: mb89352.c,v 1.4 1998/03/14 07:31:20 kmatsuda Exp */
5 * Copyright (c) 1996-1999,2004 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum, Masaru Oki and Kouichi Matsuda.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1994 Jarle Greipsland
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. The name of the author may not be used to endorse or promote products
44 * derived from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
48 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
55 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
59 * [NetBSD for NEC PC-98 series]
60 * Copyright (c) 1996, 1997, 1998
61 * NetBSD/pc98 porting staff. All rights reserved.
62 * Copyright (c) 1996, 1997, 1998
63 * Kouichi Matsuda. All rights reserved.
67 * Acknowledgements: Many of the algorithms used in this driver are
68 * inspired by the work of Julian Elischer (julian@tfs.com) and
69 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million!
73 * 1) Get the DMA stuff working.
74 * 2) Get the iov/uio stuff working. Is this a good thing ???
75 * 3) Get the synch stuff working.
76 * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: mb89352.c,v 1.50 2009/11/12 20:14:04 dyoung Exp $");
85 #define integrate inline static
89 * A few customizable items:
92 /* Synchronous data transfers? */
93 #define SPC_USE_SYNCHRONOUS 0
94 #define SPC_SYNC_REQ_ACK_OFS 8
96 /* Wide data transfers? */
97 #define SPC_USE_WIDE 0
98 #define SPC_MAX_WIDTH 0
100 /* Max attempts made to transmit a message */
101 #define SPC_MSG_MAX_ATTEMPT 3 /* Not used now XXX */
104 * Some spin loop parameters (essentially how long to wait some places)
105 * The problem(?) is that sometimes we expect either to be able to transmit a
106 * byte or to get a new one from the SCSI bus pretty soon. In order to avoid
107 * returning from the interrupt just to get yanked back for the next byte we
108 * may spin in the interrupt routine waiting for this byte to come. How long?
109 * This is really (SCSI) device and processor dependent. Tuneable, I guess.
111 #define SPC_MSGIN_SPIN 1 /* Will spinwait upto ?ms for a new msg byte */
112 #define SPC_MSGOUT_SPIN 1
115 * Include debug functions? At the end of this file there are a bunch of
116 * functions that will print out various information regarding queued SCSI
117 * commands, driver state and chip contents. You can call them from the
118 * kernel debugger. If you set SPC_DEBUG to 0 they are not included (the
119 * kernel uses less memory) but you lose the debugging facilities.
125 #define SPC_ABORT_TIMEOUT 2000 /* time to wait for abort */
127 /* threshold length for DMA transfer */
128 #define SPC_MIN_DMA_LEN 32
130 #ifdef x68k /* XXX it seems x68k SPC SCSI hardware has some quirks */
131 #define NEED_DREQ_ON_HARDWARE_XFER
132 #define NO_MANUAL_XFER
135 /* End of customizable parameters */
138 * MB89352 SCSI Protocol Controller (SPC) routines.
143 #include <sys/param.h>
144 #include <sys/systm.h>
145 #include <sys/kernel.h>
146 #include <sys/errno.h>
147 #include <sys/ioctl.h>
148 #include <sys/device.h>
150 #include <sys/proc.h>
151 #include <sys/queue.h>
153 #include <sys/intr.h>
156 #include <dev/scsipi/scsi_all.h>
157 #include <dev/scsipi/scsipi_all.h>
158 #include <dev/scsipi/scsi_message.h>
159 #include <dev/scsipi/scsiconf.h>
161 #include <dev/ic/mb89352reg.h>
162 #include <dev/ic/mb89352var.h>
165 #define Debugger() panic("should call debugger here (mb89352.c)")
169 int spc_debug
= 0x00; /* SPC_SHOWSTART|SPC_SHOWMISC|SPC_SHOWTRACE; */
172 void spc_done(struct spc_softc
*, struct spc_acb
*);
173 void spc_dequeue(struct spc_softc
*, struct spc_acb
*);
174 void spc_scsipi_request(struct scsipi_channel
*, scsipi_adapter_req_t
,
176 int spc_poll(struct spc_softc
*, struct scsipi_xfer
*, int);
177 integrate
void spc_sched_msgout(struct spc_softc
*, uint8_t);
178 integrate
void spc_setsync(struct spc_softc
*, struct spc_tinfo
*);
179 void spc_select(struct spc_softc
*, struct spc_acb
*);
180 void spc_timeout(void *);
181 void spc_scsi_reset(struct spc_softc
*);
182 void spc_reset(struct spc_softc
*);
183 void spc_free_acb(struct spc_softc
*, struct spc_acb
*, int);
184 struct spc_acb
* spc_get_acb(struct spc_softc
*);
185 int spc_reselect(struct spc_softc
*, int);
186 void spc_msgin(struct spc_softc
*);
187 void spc_abort(struct spc_softc
*, struct spc_acb
*);
188 void spc_msgout(struct spc_softc
*);
189 int spc_dataout_pio(struct spc_softc
*, uint8_t *, int);
190 int spc_datain_pio(struct spc_softc
*, uint8_t *, int);
192 void spc_print_acb(struct spc_acb
*);
193 void spc_dump_driver(struct spc_softc
*);
194 void spc_dump89352(struct spc_softc
*);
195 void spc_show_scsi_cmd(struct spc_acb
*);
196 void spc_print_active_acb(void);
199 extern struct cfdriver spc_cd
;
202 * INITIALIZATION ROUTINES (probe, attach ++)
206 * Do the real search-for-device.
207 * Prerequisite: sc->sc_iobase should be set to the proper value
210 spc_find(bus_space_tag_t iot
, bus_space_handle_t ioh
, int bdid
)
212 long timeout
= SPC_ABORT_TIMEOUT
;
214 SPC_TRACE(("spc: probing for spc-chip\n"));
216 * Disable interrupts then reset the FUJITSU chip.
218 bus_space_write_1(iot
, ioh
, SCTL
, SCTL_DISABLE
| SCTL_CTRLRST
);
219 bus_space_write_1(iot
, ioh
, SCMD
, 0);
220 bus_space_write_1(iot
, ioh
, PCTL
, 0);
221 bus_space_write_1(iot
, ioh
, TEMP
, 0);
222 bus_space_write_1(iot
, ioh
, TCH
, 0);
223 bus_space_write_1(iot
, ioh
, TCM
, 0);
224 bus_space_write_1(iot
, ioh
, TCL
, 0);
225 bus_space_write_1(iot
, ioh
, INTS
, 0);
226 bus_space_write_1(iot
, ioh
, SCTL
,
227 SCTL_DISABLE
| SCTL_ABRT_ENAB
| SCTL_PARITY_ENAB
| SCTL_RESEL_ENAB
);
228 bus_space_write_1(iot
, ioh
, BDID
, bdid
);
230 bus_space_write_1(iot
, ioh
, SCTL
,
231 bus_space_read_1(iot
, ioh
, SCTL
) & ~SCTL_DISABLE
);
233 /* The following detection is derived from spc.c
234 * (by Takahide Matsutsuka) in FreeBSD/pccard-test.
236 while (bus_space_read_1(iot
, ioh
, PSNS
) && timeout
) {
241 printf("spc: find failed\n");
245 SPC_START(("SPC found"));
250 spc_attach(struct spc_softc
*sc
)
252 struct scsipi_adapter
*adapt
= &sc
->sc_adapter
;
253 struct scsipi_channel
*chan
= &sc
->sc_channel
;
255 SPC_TRACE(("spc_attach "));
256 sc
->sc_state
= SPC_INIT
;
258 sc
->sc_freq
= 20; /* XXXX Assume 20 MHz. */
260 #if SPC_USE_SYNCHRONOUS
262 * These are the bounds of the sync period, based on the frequency of
263 * the chip's clock input and the size and offset of the sync period
266 * For a 20MHz clock, this gives us 25, or 100ns, or 10MB/s, as a
267 * maximum transfer rate, and 112.5, or 450ns, or 2.22MB/s, as a
268 * minimum transfer rate.
270 sc
->sc_minsync
= (2 * 250) / sc
->sc_freq
;
271 sc
->sc_maxsync
= (9 * 250) / sc
->sc_freq
;
275 * Fill in the adapter.
277 adapt
->adapt_dev
= sc
->sc_dev
;
278 adapt
->adapt_nchannels
= 1;
279 adapt
->adapt_openings
= 7;
280 adapt
->adapt_max_periph
= 1;
281 adapt
->adapt_request
= spc_scsipi_request
;
282 adapt
->adapt_minphys
= minphys
;
284 chan
->chan_adapter
= &sc
->sc_adapter
;
285 chan
->chan_bustype
= &scsi_bustype
;
286 chan
->chan_channel
= 0;
287 chan
->chan_ntargets
= 8;
288 chan
->chan_nluns
= 8;
289 chan
->chan_id
= sc
->sc_initiator
;
292 * Add reference to adapter so that we drop the reference after
293 * config_found() to make sure the adatper is disabled.
295 if (scsipi_adapter_addref(adapt
) != 0) {
296 aprint_error_dev(sc
->sc_dev
, "unable to enable controller\n");
300 spc_init(sc
, 1); /* Init chip and driver */
303 * ask the adapter what subunits are present
305 sc
->sc_child
= config_found(sc
->sc_dev
, chan
, scsiprint
);
306 scsipi_adapter_delref(adapt
);
310 spc_childdet(device_t self
, device_t child
)
312 struct spc_softc
*sc
= device_private(self
);
314 if (sc
->sc_child
== child
)
319 spc_detach(device_t self
, int flags
)
321 struct spc_softc
*sc
= device_private(self
);
324 if (sc
->sc_child
!= NULL
)
325 rv
= config_detach(sc
->sc_child
, flags
);
331 * Initialize MB89352 chip itself
332 * The following conditions should hold:
333 * spc_isa_probe should have succeeded, i.e. the iobase address in spc_softc
337 spc_reset(struct spc_softc
*sc
)
339 bus_space_tag_t iot
= sc
->sc_iot
;
340 bus_space_handle_t ioh
= sc
->sc_ioh
;
342 SPC_TRACE(("spc_reset "));
344 * Disable interrupts then reset the FUJITSU chip.
346 bus_space_write_1(iot
, ioh
, SCTL
, SCTL_DISABLE
| SCTL_CTRLRST
);
347 bus_space_write_1(iot
, ioh
, SCMD
, 0);
348 bus_space_write_1(iot
, ioh
, TMOD
, 0);
349 bus_space_write_1(iot
, ioh
, PCTL
, 0);
350 bus_space_write_1(iot
, ioh
, TEMP
, 0);
351 bus_space_write_1(iot
, ioh
, TCH
, 0);
352 bus_space_write_1(iot
, ioh
, TCM
, 0);
353 bus_space_write_1(iot
, ioh
, TCL
, 0);
354 bus_space_write_1(iot
, ioh
, INTS
, 0);
355 bus_space_write_1(iot
, ioh
, SCTL
,
356 SCTL_DISABLE
| SCTL_ABRT_ENAB
| SCTL_PARITY_ENAB
| SCTL_RESEL_ENAB
);
357 bus_space_write_1(iot
, ioh
, BDID
, sc
->sc_initiator
);
359 bus_space_write_1(iot
, ioh
, SCTL
,
360 bus_space_read_1(iot
, ioh
, SCTL
) & ~SCTL_DISABLE
);
365 * Pull the SCSI RST line for 500us.
368 spc_scsi_reset(struct spc_softc
*sc
)
370 bus_space_tag_t iot
= sc
->sc_iot
;
371 bus_space_handle_t ioh
= sc
->sc_ioh
;
373 SPC_TRACE(("spc_scsi_reset "));
374 bus_space_write_1(iot
, ioh
, SCMD
,
375 bus_space_read_1(iot
, ioh
, SCMD
) | SCMD_RST
);
377 bus_space_write_1(iot
, ioh
, SCMD
,
378 bus_space_read_1(iot
, ioh
, SCMD
) & ~SCMD_RST
);
383 * Initialize spc SCSI driver.
386 spc_init(struct spc_softc
*sc
, int bus_reset
)
391 SPC_TRACE(("spc_init "));
398 if (sc
->sc_state
== SPC_INIT
) {
399 /* First time through; initialize. */
400 TAILQ_INIT(&sc
->ready_list
);
401 TAILQ_INIT(&sc
->nexus_list
);
402 TAILQ_INIT(&sc
->free_list
);
405 memset(acb
, 0, sizeof(sc
->sc_acb
));
406 for (r
= 0; r
< sizeof(sc
->sc_acb
) / sizeof(*acb
); r
++) {
407 TAILQ_INSERT_TAIL(&sc
->free_list
, acb
, chain
);
410 memset(&sc
->sc_tinfo
, 0, sizeof(sc
->sc_tinfo
));
412 /* Cancel any active commands. */
413 sc
->sc_state
= SPC_CLEANING
;
414 if ((acb
= sc
->sc_nexus
) != NULL
) {
415 acb
->xs
->error
= XS_DRIVER_STUFFUP
;
416 callout_stop(&acb
->xs
->xs_callout
);
419 while ((acb
= TAILQ_FIRST(&sc
->nexus_list
)) != NULL
) {
420 acb
->xs
->error
= XS_DRIVER_STUFFUP
;
421 callout_stop(&acb
->xs
->xs_callout
);
426 sc
->sc_prevphase
= PH_INVALID
;
427 for (r
= 0; r
< 8; r
++) {
428 struct spc_tinfo
*ti
= &sc
->sc_tinfo
[r
];
431 #if SPC_USE_SYNCHRONOUS
432 ti
->flags
|= DO_SYNC
;
433 ti
->period
= sc
->sc_minsync
;
434 ti
->offset
= SPC_SYNC_REQ_ACK_OFS
;
436 ti
->period
= ti
->offset
= 0;
439 ti
->flags
|= DO_WIDE
;
440 ti
->width
= SPC_MAX_WIDTH
;
446 sc
->sc_state
= SPC_IDLE
;
447 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, SCTL
,
448 bus_space_read_1(sc
->sc_iot
, sc
->sc_ioh
, SCTL
) | SCTL_INTR_ENAB
);
452 spc_free_acb(struct spc_softc
*sc
, struct spc_acb
*acb
, int flags
)
456 SPC_TRACE(("spc_free_acb "));
460 TAILQ_INSERT_HEAD(&sc
->free_list
, acb
, chain
);
465 spc_get_acb(struct spc_softc
*sc
)
470 SPC_TRACE(("spc_get_acb "));
472 acb
= TAILQ_FIRST(&sc
->free_list
);
474 TAILQ_REMOVE(&sc
->free_list
, acb
, chain
);
475 acb
->flags
|= ACB_ALLOC
;
482 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
487 * 1) Command inserted into ready list
488 * 2) Command selected for execution
489 * 3) Command won arbitration and has selected target device
490 * 4) Send message out (identify message, eventually also sync.negotiations)
492 * 5a) Receive disconnect message, disconnect.
493 * 5b) Reselected by target
494 * 5c) Receive identify message from target.
495 * 6) Send or receive data
497 * 8) Receive message (command complete etc.)
501 * Start a SCSI-command
502 * This function is called by the higher level SCSI-driver to queue/run
506 spc_scsipi_request(struct scsipi_channel
*chan
, scsipi_adapter_req_t req
,
509 struct scsipi_xfer
*xs
;
510 struct scsipi_periph
*periph
;
511 struct spc_softc
*sc
= device_private(chan
->chan_adapter
->adapt_dev
);
516 case ADAPTER_REQ_RUN_XFER
:
518 periph
= xs
->xs_periph
;
519 SPC_TRACE(("spc_scsipi_request "));
520 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs
->cmd
->opcode
, xs
->cmdlen
,
521 periph
->periph_target
));
523 flags
= xs
->xs_control
;
524 acb
= spc_get_acb(sc
);
527 * This should nerver happen as we track the resources
531 scsipi_printaddr(periph
);
532 printf("unable to allocate acb\n");
533 panic("spc_scsipi_request");
539 acb
->timeout
= xs
->timeout
;
541 if (xs
->xs_control
& XS_CTL_RESET
) {
542 acb
->flags
|= ACB_RESET
;
543 acb
->scsipi_cmd_length
= 0;
544 acb
->data_length
= 0;
546 memcpy(&acb
->scsipi_cmd
, xs
->cmd
, xs
->cmdlen
);
547 acb
->scsipi_cmd_length
= xs
->cmdlen
;
548 acb
->data_addr
= xs
->data
;
549 acb
->data_length
= xs
->datalen
;
551 acb
->target_stat
= 0;
555 TAILQ_INSERT_TAIL(&sc
->ready_list
, acb
, chain
);
557 * Start scheduling unless a queue process is in progress.
559 if (sc
->sc_state
== SPC_IDLE
)
562 * After successful sending, check if we should return just now.
563 * If so, return SUCCESSFULLY_QUEUED.
568 if ((flags
& XS_CTL_POLL
) == 0)
571 /* Not allowed to use interrupts, use polling instead */
573 if (spc_poll(sc
, xs
, acb
->timeout
)) {
575 if (spc_poll(sc
, xs
, acb
->timeout
))
580 case ADAPTER_REQ_GROW_RESOURCES
:
581 /* XXX Not supported. */
583 case ADAPTER_REQ_SET_XFER_MODE
:
586 * We don't support Sync, Wide, or Tagged Command Queuing.
587 * Just callback now, to report this.
589 struct scsipi_xfer_mode
*xm
= arg
;
594 scsipi_async_event(chan
, ASYNC_EVENT_XFER_MODE
, xm
);
601 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
604 spc_poll(struct spc_softc
*sc
, struct scsipi_xfer
*xs
, int count
)
606 bus_space_tag_t iot
= sc
->sc_iot
;
607 bus_space_handle_t ioh
= sc
->sc_ioh
;
609 SPC_TRACE(("spc_poll "));
612 * If we had interrupts enabled, would we
613 * have got an interrupt?
615 if (bus_space_read_1(iot
, ioh
, INTS
) != 0)
617 if ((xs
->xs_status
& XS_STS_DONE
) != 0)
626 * LOW LEVEL SCSI UTILITIES
630 spc_sched_msgout(struct spc_softc
*sc
, uint8_t m
)
632 bus_space_tag_t iot
= sc
->sc_iot
;
633 bus_space_handle_t ioh
= sc
->sc_ioh
;
635 SPC_TRACE(("spc_sched_msgout "));
636 if (sc
->sc_msgpriq
== 0)
637 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_SET_ATN
);
642 * Set synchronous transfer offset and period.
645 spc_setsync(struct spc_softc
*sc
, struct spc_tinfo
*ti
)
647 #if SPC_USE_SYNCHRONOUS
648 bus_space_tag_t iot
= sc
->sc_iot
;
649 bus_space_handle_t ioh
= sc
->sc_ioh
;
651 SPC_TRACE(("spc_setsync "));
653 bus_space_write_1(iot
, ioh
, TMOD
,
654 ((ti
->period
* sc
->sc_freq
) / 250 - 2) << 4 | ti
->offset
);
656 bus_space_write_1(iot
, ioh
, TMOD
, 0);
661 * Start a selection. This is used by spc_sched() to select an idle target.
664 spc_select(struct spc_softc
*sc
, struct spc_acb
*acb
)
666 struct scsipi_periph
*periph
= acb
->xs
->xs_periph
;
667 int target
= periph
->periph_target
;
668 struct spc_tinfo
*ti
= &sc
->sc_tinfo
[target
];
669 bus_space_tag_t iot
= sc
->sc_iot
;
670 bus_space_handle_t ioh
= sc
->sc_ioh
;
672 SPC_TRACE(("spc_select "));
676 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_SET_ATN
);
679 bus_space_write_1(iot
, ioh
, PCTL
, 0);
680 bus_space_write_1(iot
, ioh
, TEMP
,
681 (1 << sc
->sc_initiator
) | (1 << target
));
683 * Setup BSY timeout (selection timeout).
684 * 250ms according to the SCSI specification.
685 * T = (X * 256 + 15) * Tclf * 2 (Tclf = 200ns on x68k)
686 * To setup 256ms timeout,
687 * 128000ns/200ns = X * 256 + 15
691 * ==> tch = 2, tcm = 113 (correct?)
693 /* Time to the information transfer phase start. */
694 /* XXX These values should be calculated from sc_freq */
695 bus_space_write_1(iot
, ioh
, TCH
, 2);
696 bus_space_write_1(iot
, ioh
, TCM
, 113);
697 bus_space_write_1(iot
, ioh
, TCL
, 3);
698 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_SELECT
);
700 sc
->sc_state
= SPC_SELECTING
;
704 spc_reselect(struct spc_softc
*sc
, int message
)
706 uint8_t selid
, target
, lun
;
708 struct scsipi_periph
*periph
;
709 struct spc_tinfo
*ti
;
711 SPC_TRACE(("spc_reselect "));
713 * The SCSI chip made a snapshot of the data bus while the reselection
714 * was being negotiated. This enables us to determine which target did
717 selid
= sc
->sc_selid
& ~(1 << sc
->sc_initiator
);
718 if (selid
& (selid
- 1)) {
719 printf("%s: reselect with invalid selid %02x; "
720 "sending DEVICE RESET\n", device_xname(sc
->sc_dev
), selid
);
726 * Search wait queue for disconnected cmd
727 * The list should be short, so I haven't bothered with
728 * any more sophisticated structures than a simple
729 * singly linked list.
731 target
= ffs(selid
) - 1;
732 lun
= message
& 0x07;
733 TAILQ_FOREACH(acb
, &sc
->nexus_list
, chain
) {
734 periph
= acb
->xs
->xs_periph
;
735 if (periph
->periph_target
== target
&&
736 periph
->periph_lun
== lun
)
740 printf("%s: reselect from target %d lun %d with no nexus; "
741 "sending ABORT\n", device_xname(sc
->sc_dev
), target
, lun
);
746 /* Make this nexus active again. */
747 TAILQ_REMOVE(&sc
->nexus_list
, acb
, chain
);
748 sc
->sc_state
= SPC_CONNECTED
;
750 ti
= &sc
->sc_tinfo
[target
];
751 ti
->lubusy
|= (1 << lun
);
754 if (acb
->flags
& ACB_RESET
)
755 spc_sched_msgout(sc
, SEND_DEV_RESET
);
756 else if (acb
->flags
& ACB_ABORT
)
757 spc_sched_msgout(sc
, SEND_ABORT
);
759 /* Do an implicit RESTORE POINTERS. */
760 sc
->sc_dp
= acb
->data_addr
;
761 sc
->sc_dleft
= acb
->data_length
;
762 sc
->sc_cp
= (uint8_t *)&acb
->scsipi_cmd
;
763 sc
->sc_cleft
= acb
->scsipi_cmd_length
;
768 spc_sched_msgout(sc
, SEND_DEV_RESET
);
772 spc_sched_msgout(sc
, SEND_ABORT
);
777 * Schedule a SCSI operation. This has now been pulled out of the interrupt
778 * handler so that we may call it from spc_scsi_cmd and spc_done. This may
779 * save us an unnecessary interrupt just to get things going. Should only be
780 * called when state == SPC_IDLE and at bio pl.
783 spc_sched(struct spc_softc
*sc
)
786 struct scsipi_periph
*periph
;
787 struct spc_tinfo
*ti
;
789 /* missing the hw, just return and wait for our hw */
790 if (sc
->sc_flags
& SPC_INACTIVE
)
792 SPC_TRACE(("spc_sched "));
794 * Find first acb in ready queue that is for a target/lunit pair that
797 TAILQ_FOREACH(acb
, &sc
->ready_list
, chain
) {
798 periph
= acb
->xs
->xs_periph
;
799 ti
= &sc
->sc_tinfo
[periph
->periph_target
];
800 if ((ti
->lubusy
& (1 << periph
->periph_lun
)) == 0) {
801 SPC_MISC(("selecting %d:%d ",
802 periph
->periph_target
, periph
->periph_lun
));
803 TAILQ_REMOVE(&sc
->ready_list
, acb
, chain
);
808 SPC_MISC(("%d:%d busy\n",
809 periph
->periph_target
, periph
->periph_lun
));
813 /* Nothing to start; just enable reselections and wait. */
817 * POST PROCESSING OF SCSI_CMD (usually current)
820 spc_done(struct spc_softc
*sc
, struct spc_acb
*acb
)
822 struct scsipi_xfer
*xs
= acb
->xs
;
823 struct scsipi_periph
*periph
= xs
->xs_periph
;
824 struct spc_tinfo
*ti
= &sc
->sc_tinfo
[periph
->periph_target
];
826 SPC_TRACE(("spc_done "));
828 if (xs
->error
== XS_NOERROR
) {
829 if (acb
->flags
& ACB_ABORT
) {
830 xs
->error
= XS_DRIVER_STUFFUP
;
832 switch (acb
->target_stat
) {
834 /* First, save the return values */
835 xs
->resid
= acb
->data_length
;
838 xs
->status
= acb
->target_stat
;
842 xs
->resid
= acb
->data_length
;
845 xs
->error
= XS_DRIVER_STUFFUP
;
847 printf("%s: spc_done: bad stat 0x%x\n",
848 device_xname(sc
->sc_dev
), acb
->target_stat
);
856 if ((spc_debug
& SPC_SHOWMISC
) != 0) {
858 printf("resid=%d ", xs
->resid
);
860 printf("error=%d\n", xs
->error
);
865 * Remove the ACB from whatever queue it happens to be on.
867 if (acb
->flags
& ACB_NEXUS
)
868 ti
->lubusy
&= ~(1 << periph
->periph_lun
);
869 if (acb
== sc
->sc_nexus
) {
871 sc
->sc_state
= SPC_IDLE
;
874 spc_dequeue(sc
, acb
);
876 spc_free_acb(sc
, acb
, xs
->xs_control
);
882 spc_dequeue(struct spc_softc
*sc
, struct spc_acb
*acb
)
885 SPC_TRACE(("spc_dequeue "));
886 if (acb
->flags
& ACB_NEXUS
)
887 TAILQ_REMOVE(&sc
->nexus_list
, acb
, chain
);
889 TAILQ_REMOVE(&sc
->ready_list
, acb
, chain
);
893 * INTERRUPT/PROTOCOL ENGINE
898 * The SCSI bus is already in the MSGI phase and there is a message byte
899 * on the bus, along with an asserted REQ signal.
902 spc_msgin(struct spc_softc
*sc
)
904 bus_space_tag_t iot
= sc
->sc_iot
;
905 bus_space_handle_t ioh
= sc
->sc_ioh
;
909 SPC_TRACE(("spc_msgin "));
911 if (sc
->sc_prevphase
== PH_MSGIN
) {
912 /* This is a continuation of the previous message. */
913 n
= sc
->sc_imp
- sc
->sc_imess
;
917 /* This is a new MESSAGE IN phase. Clean up our state. */
918 sc
->sc_flags
&= ~SPC_DROP_MSGIN
;
922 sc
->sc_imp
= &sc
->sc_imess
[n
];
926 * Read a whole message, but don't ack the last byte. If we reject the
927 * message, we have to assert ATN during the message transfer phase
931 #ifdef NO_MANUAL_XFER /* XXX */
932 if (bus_space_read_1(iot
, ioh
, INTS
) != 0) {
934 * Target left MESSAGE IN, probably because it
935 * a) noticed our ATN signal, or
936 * b) ran out of messages.
941 /* If parity error, just dump everything on the floor. */
942 if ((bus_space_read_1(iot
, ioh
, SERR
) &
943 (SERR_SCSI_PAR
|SERR_SPC_PAR
)) != 0) {
944 sc
->sc_flags
|= SPC_DROP_MSGIN
;
945 spc_sched_msgout(sc
, SEND_PARITY_ERROR
);
948 #ifdef NO_MANUAL_XFER /* XXX */
949 /* send TRANSFER command. */
950 bus_space_write_1(iot
, ioh
, TCH
, 0);
951 bus_space_write_1(iot
, ioh
, TCM
, 0);
952 bus_space_write_1(iot
, ioh
, TCL
, 1);
953 bus_space_write_1(iot
, ioh
, PCTL
,
954 sc
->sc_phase
| PCTL_BFINT_ENAB
);
955 #ifdef NEED_DREQ_ON_HARDWARE_XFER
956 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_XFR
);
958 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_XFR
| SCMD_PROG_XFR
);
961 if ((bus_space_read_1(iot
, ioh
, SSTS
) &
962 SSTS_DREG_EMPTY
) == 0)
964 if (bus_space_read_1(iot
, ioh
, INTS
) != 0)
967 msg
= bus_space_read_1(iot
, ioh
, DREG
);
969 if ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_ATN
) != 0)
970 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ATN
);
971 bus_space_write_1(iot
, ioh
, PCTL
, PCTL_BFINT_ENAB
| PH_MSGIN
);
973 while ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) == 0) {
974 if ((bus_space_read_1(iot
, ioh
, PSNS
) & PH_MASK
)
976 bus_space_read_1(iot
, ioh
, INTS
) != 0)
978 * Target left MESSAGE IN, probably because it
979 * a) noticed our ATN signal, or
980 * b) ran out of messages.
983 DELAY(1); /* XXX needs timeout */
986 msg
= bus_space_read_1(iot
, ioh
, TEMP
);
989 /* Gather incoming message bytes if needed. */
990 if ((sc
->sc_flags
& SPC_DROP_MSGIN
) == 0) {
991 if (n
>= SPC_MAX_MSG_LEN
) {
992 sc
->sc_flags
|= SPC_DROP_MSGIN
;
993 spc_sched_msgout(sc
, SEND_REJECT
);
998 * This testing is suboptimal, but most
999 * messages will be of the one byte variety, so
1000 * it should not affect performance
1003 if (n
== 1 && MSG_IS1BYTE(sc
->sc_imess
[0]))
1005 if (n
== 2 && MSG_IS2BYTE(sc
->sc_imess
[0]))
1007 if (n
>= 3 && MSG_ISEXTENDED(sc
->sc_imess
[0]) &&
1008 n
== sc
->sc_imess
[1] + 2)
1013 * If we reach this spot we're either:
1014 * a) in the middle of a multi-byte message, or
1015 * b) dropping bytes.
1018 #ifndef NO_MANUAL_XFER /* XXX */
1019 /* Ack the last byte read. */
1020 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_SET_ACK
);
1021 while ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) != 0)
1022 DELAY(1); /* XXX needs timeout */
1023 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ACK
);
1027 SPC_MISC(("n=%d imess=0x%02x ", n
, sc
->sc_imess
[0]));
1029 /* We now have a complete message. Parse it. */
1030 switch (sc
->sc_state
) {
1031 struct spc_acb
*acb
;
1032 struct spc_tinfo
*ti
;
1035 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1037 ti
= &sc
->sc_tinfo
[acb
->xs
->xs_periph
->periph_target
];
1039 switch (sc
->sc_imess
[0]) {
1040 case MSG_CMDCOMPLETE
:
1042 if (sc
->sc_dleft
< 0) {
1043 periph
= acb
->xs
->xs_periph
;
1044 printf("%s: %ld extra bytes from %d:%d\n",
1045 device_xname(sc
->sc_dev
),
1046 (long)-sc
->sc_dleft
,
1047 periph
->periph_target
, periph
->periph_lun
);
1051 acb
->xs
->resid
= acb
->data_length
= sc
->sc_dleft
;
1052 sc
->sc_state
= SPC_CMDCOMPLETE
;
1055 case MSG_PARITY_ERROR
:
1056 /* Resend the last message. */
1057 spc_sched_msgout(sc
, sc
->sc_lastmsg
);
1060 case MSG_MESSAGE_REJECT
:
1061 SPC_MISC(("message rejected %02x ", sc
->sc_lastmsg
));
1062 switch (sc
->sc_lastmsg
) {
1063 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
1065 ti
->flags
&= ~(DO_SYNC
| DO_WIDE
);
1066 ti
->period
= ti
->offset
= 0;
1067 spc_setsync(sc
, ti
);
1071 #if SPC_USE_SYNCHRONOUS
1073 ti
->flags
&= ~DO_SYNC
;
1074 ti
->period
= ti
->offset
= 0;
1075 spc_setsync(sc
, ti
);
1080 ti
->flags
&= ~DO_WIDE
;
1084 case SEND_INIT_DET_ERR
:
1085 spc_sched_msgout(sc
, SEND_ABORT
);
1093 case MSG_DISCONNECT
:
1095 sc
->sc_state
= SPC_DISCONNECT
;
1098 case MSG_SAVEDATAPOINTER
:
1099 acb
->data_addr
= sc
->sc_dp
;
1100 acb
->data_length
= sc
->sc_dleft
;
1103 case MSG_RESTOREPOINTERS
:
1104 sc
->sc_dp
= acb
->data_addr
;
1105 sc
->sc_dleft
= acb
->data_length
;
1106 sc
->sc_cp
= (uint8_t *)&acb
->scsipi_cmd
;
1107 sc
->sc_cleft
= acb
->scsipi_cmd_length
;
1111 switch (sc
->sc_imess
[2]) {
1112 #if SPC_USE_SYNCHRONOUS
1114 if (sc
->sc_imess
[1] != 3)
1116 ti
->period
= sc
->sc_imess
[3];
1117 ti
->offset
= sc
->sc_imess
[4];
1118 ti
->flags
&= ~DO_SYNC
;
1119 if (ti
->offset
== 0) {
1120 } else if (ti
->period
< sc
->sc_minsync
||
1121 ti
->period
> sc
->sc_maxsync
||
1123 ti
->period
= ti
->offset
= 0;
1124 spc_sched_msgout(sc
, SEND_SDTR
);
1126 scsipi_printaddr(acb
->xs
->xs_periph
);
1127 printf("sync, offset %d, "
1129 ti
->offset
, ti
->period
* 4);
1131 spc_setsync(sc
, ti
);
1137 if (sc
->sc_imess
[1] != 2)
1139 ti
->width
= sc
->sc_imess
[3];
1140 ti
->flags
&= ~DO_WIDE
;
1141 if (ti
->width
== 0) {
1142 } else if (ti
->width
> SPC_MAX_WIDTH
) {
1144 spc_sched_msgout(sc
, SEND_WDTR
);
1146 scsipi_printaddr(acb
->xs
->xs_periph
);
1147 printf("wide, width %d\n",
1148 1 << (3 + ti
->width
));
1154 printf("%s: unrecognized MESSAGE EXTENDED; "
1156 device_xname(sc
->sc_dev
));
1163 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1164 device_xname(sc
->sc_dev
));
1167 spc_sched_msgout(sc
, SEND_REJECT
);
1172 case SPC_RESELECTED
:
1173 if (!MSG_ISIDENTIFY(sc
->sc_imess
[0])) {
1174 printf("%s: reselect without IDENTIFY; "
1175 "sending DEVICE RESET\n", device_xname(sc
->sc_dev
));
1180 (void) spc_reselect(sc
, sc
->sc_imess
[0]);
1184 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1185 device_xname(sc
->sc_dev
));
1188 spc_sched_msgout(sc
, SEND_DEV_RESET
);
1193 spc_sched_msgout(sc
, SEND_ABORT
);
1198 #ifndef NO_MANUAL_XFER /* XXX */
1199 /* Ack the last message byte. */
1200 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_SET_ACK
);
1201 while ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) != 0)
1202 DELAY(1); /* XXX needs timeout */
1203 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ACK
);
1206 /* Go get the next message, if any. */
1210 #ifdef NO_MANUAL_XFER /* XXX */
1211 /* Ack the last message byte. */
1212 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ACK
);
1214 SPC_MISC(("n=%d imess=0x%02x ", n
, sc
->sc_imess
[0]));
1218 * Send the highest priority, scheduled message.
1221 spc_msgout(struct spc_softc
*sc
)
1223 bus_space_tag_t iot
= sc
->sc_iot
;
1224 bus_space_handle_t ioh
= sc
->sc_ioh
;
1225 #if SPC_USE_SYNCHRONOUS
1226 struct spc_tinfo
*ti
;
1230 SPC_TRACE(("spc_msgout "));
1232 if (sc
->sc_prevphase
== PH_MSGOUT
) {
1233 if (sc
->sc_omp
== sc
->sc_omess
) {
1235 * This is a retransmission.
1237 * We get here if the target stayed in MESSAGE OUT
1238 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1239 * that all of the previously transmitted messages must
1240 * be sent again, in the same order. Therefore, we
1241 * requeue all the previously transmitted messages, and
1242 * start again from the top. Our simple priority
1243 * scheme keeps the messages in the right order.
1245 SPC_MISC(("retransmitting "));
1246 sc
->sc_msgpriq
|= sc
->sc_msgoutq
;
1248 * Set ATN. If we're just sending a trivial 1-byte
1249 * message, we'll clear ATN later on anyway.
1251 bus_space_write_1(iot
, ioh
, SCMD
,
1252 SCMD_SET_ATN
); /* XXX? */
1254 /* This is a continuation of the previous message. */
1255 n
= sc
->sc_omp
- sc
->sc_omess
;
1260 /* No messages transmitted so far. */
1265 /* Pick up highest priority message. */
1266 sc
->sc_currmsg
= sc
->sc_msgpriq
& -sc
->sc_msgpriq
;
1267 sc
->sc_msgpriq
&= ~sc
->sc_currmsg
;
1268 sc
->sc_msgoutq
|= sc
->sc_currmsg
;
1270 /* Build the outgoing message data. */
1271 switch (sc
->sc_currmsg
) {
1273 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1275 MSG_IDENTIFY(sc
->sc_nexus
->xs
->xs_periph
->periph_lun
, 1);
1279 #if SPC_USE_SYNCHRONOUS
1281 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1282 ti
= &sc
->sc_tinfo
[sc
->sc_nexus
->xs
->xs_periph
->periph_target
];
1283 sc
->sc_omess
[4] = MSG_EXTENDED
;
1284 sc
->sc_omess
[3] = MSG_EXT_SDTR_LEN
;
1285 sc
->sc_omess
[2] = MSG_EXT_SDTR
;
1286 sc
->sc_omess
[1] = ti
->period
>> 2;
1287 sc
->sc_omess
[0] = ti
->offset
;
1294 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1295 ti
= &sc
->sc_tinfo
[sc
->sc_nexus
->xs
->xs_periph
->periph_target
];
1296 sc
->sc_omess
[3] = MSG_EXTENDED
;
1297 sc
->sc_omess
[2] = MSG_EXT_WDTR_LEN
;
1298 sc
->sc_omess
[1] = MSG_EXT_WDTR
;
1299 sc
->sc_omess
[0] = ti
->width
;
1304 case SEND_DEV_RESET
:
1305 sc
->sc_flags
|= SPC_ABORTING
;
1306 sc
->sc_omess
[0] = MSG_BUS_DEV_RESET
;
1311 sc
->sc_omess
[0] = MSG_MESSAGE_REJECT
;
1315 case SEND_PARITY_ERROR
:
1316 sc
->sc_omess
[0] = MSG_PARITY_ERROR
;
1320 case SEND_INIT_DET_ERR
:
1321 sc
->sc_omess
[0] = MSG_INITIATOR_DET_ERR
;
1326 sc
->sc_flags
|= SPC_ABORTING
;
1327 sc
->sc_omess
[0] = MSG_ABORT
;
1332 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1333 device_xname(sc
->sc_dev
));
1335 sc
->sc_omess
[0] = MSG_NOOP
;
1339 sc
->sc_omp
= &sc
->sc_omess
[n
];
1342 /* Send message bytes. */
1343 /* send TRANSFER command. */
1344 bus_space_write_1(iot
, ioh
, TCH
, n
>> 16);
1345 bus_space_write_1(iot
, ioh
, TCM
, n
>> 8);
1346 bus_space_write_1(iot
, ioh
, TCL
, n
);
1347 bus_space_write_1(iot
, ioh
, PCTL
, sc
->sc_phase
| PCTL_BFINT_ENAB
);
1348 #ifdef NEED_DREQ_ON_HARDWARE_XFER
1349 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_XFR
); /* XXX */
1351 bus_space_write_1(iot
, ioh
, SCMD
,
1352 SCMD_XFR
| SCMD_PROG_XFR
);
1355 if ((bus_space_read_1(iot
, ioh
, SSTS
) & SSTS_BUSY
) != 0)
1357 if (bus_space_read_1(iot
, ioh
, INTS
) != 0)
1363 if ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) != 0)
1365 /* Wait for REQINIT. XXX Need timeout. */
1368 if (bus_space_read_1(iot
, ioh
, INTS
) != 0) {
1370 * Target left MESSAGE OUT, possibly to reject
1373 * If this is the last message being sent, then we
1374 * deassert ATN, since either the target is going to
1375 * ignore this message, or it's going to ask for a
1376 * retransmission via MESSAGE PARITY ERROR (in which
1377 * case we reassert ATN anyway).
1380 if (sc
->sc_msgpriq
== 0)
1381 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ATN
);
1387 /* Clear ATN before last byte if this is the last message. */
1388 if (n
== 1 && sc
->sc_msgpriq
== 0)
1389 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ATN
);
1392 while ((bus_space_read_1(iot
, ioh
, SSTS
) & SSTS_DREG_FULL
) != 0)
1394 /* Send message byte. */
1395 bus_space_write_1(iot
, ioh
, DREG
, *--sc
->sc_omp
);
1397 /* Keep track of the last message we've sent any bytes of. */
1398 sc
->sc_lastmsg
= sc
->sc_currmsg
;
1400 /* Wait for ACK to be negated. XXX Need timeout. */
1401 while ((bus_space_read_1(iot
, ioh
, PSNS
) & ACKI
) != 0)
1409 /* We get here only if the entire message has been transmitted. */
1410 if (sc
->sc_msgpriq
!= 0) {
1411 /* There are more outgoing messages. */
1416 * The last message has been transmitted. We need to remember the last
1417 * message transmitted (in case the target switches to MESSAGE IN phase
1418 * and sends a MESSAGE REJECT), and the list of messages transmitted
1419 * this time around (in case the target stays in MESSAGE OUT phase to
1420 * request a retransmit).
1424 /* Disable REQ/ACK protocol. */
1429 * spc_dataout_pio: perform a data transfer using the FIFO datapath in the spc
1430 * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
1431 * and ACK deasserted (i.e. waiting for a data byte)
1433 * This new revision has been optimized (I tried) to make the common case fast,
1434 * and the rarer cases (as a result) somewhat more comlex
1437 spc_dataout_pio(struct spc_softc
*sc
, uint8_t *p
, int n
)
1439 bus_space_tag_t iot
= sc
->sc_iot
;
1440 bus_space_handle_t ioh
= sc
->sc_ioh
;
1441 uint8_t intstat
= 0;
1443 #define DOUTAMOUNT 8 /* Full FIFO */
1445 SPC_TRACE(("spc_dataout_pio "));
1446 /* send TRANSFER command. */
1447 bus_space_write_1(iot
, ioh
, TCH
, n
>> 16);
1448 bus_space_write_1(iot
, ioh
, TCM
, n
>> 8);
1449 bus_space_write_1(iot
, ioh
, TCL
, n
);
1450 bus_space_write_1(iot
, ioh
, PCTL
, sc
->sc_phase
| PCTL_BFINT_ENAB
);
1451 #ifdef NEED_DREQ_ON_HARDWARE_XFER
1452 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_XFR
); /* XXX */
1454 bus_space_write_1(iot
, ioh
, SCMD
,
1455 SCMD_XFR
| SCMD_PROG_XFR
); /* XXX */
1458 if ((bus_space_read_1(iot
, ioh
, SSTS
) & SSTS_BUSY
) != 0)
1460 if (bus_space_read_1(iot
, ioh
, INTS
) != 0)
1465 * I have tried to make the main loop as tight as possible. This
1466 * means that some of the code following the loop is a bit more
1467 * complex than otherwise.
1473 intstat
= bus_space_read_1(iot
, ioh
, INTS
);
1474 /* Wait till buffer is empty. */
1475 if ((bus_space_read_1(iot
, ioh
, SSTS
) &
1476 SSTS_DREG_EMPTY
) != 0)
1478 /* Break on interrupt. */
1484 xfer
= min(DOUTAMOUNT
, n
);
1486 SPC_MISC(("%d> ", xfer
));
1491 bus_space_write_multi_1(iot
, ioh
, DREG
, p
, xfer
);
1497 if (bus_space_read_1(iot
, ioh
, INTS
) != 0)
1501 SPC_MISC(("extra data "));
1503 /* See the bytes off chip */
1505 /* Wait till buffer is empty. */
1506 if ((bus_space_read_1(iot
, ioh
, SSTS
) &
1507 SSTS_DREG_EMPTY
) != 0)
1509 intstat
= bus_space_read_1(iot
, ioh
, INTS
);
1510 /* Break on interrupt. */
1518 /* Stop the FIFO data path. */
1521 /* Some sort of phase change. */
1524 amount
= (bus_space_read_1(iot
, ioh
, TCH
) << 16) |
1525 (bus_space_read_1(iot
, ioh
, TCM
) << 8) |
1526 bus_space_read_1(iot
, ioh
, TCL
);
1529 SPC_MISC(("+%d ", amount
));
1537 * spc_datain_pio: perform data transfers using the FIFO datapath in the spc
1538 * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
1539 * and ACK deasserted (i.e. at least one byte is ready).
1541 * For now, uses a pretty dumb algorithm, hangs around until all data has been
1542 * transferred. This, is OK for fast targets, but not so smart for slow
1543 * targets which don't disconnect or for huge transfers.
1546 spc_datain_pio(struct spc_softc
*sc
, uint8_t *p
, int n
)
1548 bus_space_tag_t iot
= sc
->sc_iot
;
1549 bus_space_handle_t ioh
= sc
->sc_ioh
;
1551 uint8_t intstat
, sstat
;
1552 #define DINAMOUNT 8 /* Full FIFO */
1554 SPC_TRACE(("spc_datain_pio "));
1555 /* send TRANSFER command. */
1556 bus_space_write_1(iot
, ioh
, TCH
, n
>> 16);
1557 bus_space_write_1(iot
, ioh
, TCM
, n
>> 8);
1558 bus_space_write_1(iot
, ioh
, TCL
, n
);
1559 bus_space_write_1(iot
, ioh
, PCTL
, sc
->sc_phase
| PCTL_BFINT_ENAB
);
1560 #ifdef NEED_DREQ_ON_HARDWARE_XFER
1561 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_XFR
); /* XXX */
1563 bus_space_write_1(iot
, ioh
, SCMD
,
1564 SCMD_XFR
| SCMD_PROG_XFR
); /* XXX */
1568 * We leave this loop if one or more of the following is true:
1569 * a) phase != PH_DATAIN && FIFOs are empty
1570 * b) reset has occurred or busfree is detected.
1574 sstat
= bus_space_read_1(iot
, ioh
, SSTS
);
1575 if ((sstat
& SSTS_DREG_FULL
) != 0) {
1578 bus_space_read_multi_1(iot
, ioh
, DREG
, p
, DINAMOUNT
);
1580 } else if ((sstat
& SSTS_DREG_EMPTY
) == 0) {
1583 *p
++ = bus_space_read_1(iot
, ioh
, DREG
);
1587 intstat
= bus_space_read_1(iot
, ioh
, INTS
);
1592 * Some SCSI-devices are rude enough to transfer more data than what
1593 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
1594 * requested 512. Test for progress, i.e. real transfers. If no real
1595 * transfers have been performed (n is probably already zero) and the
1596 * FIFO is not empty, waste some bytes....
1600 sstat
= bus_space_read_1(iot
, ioh
, SSTS
);
1601 if ((sstat
& SSTS_DREG_EMPTY
) == 0) {
1602 (void) bus_space_read_1(iot
, ioh
, DREG
);
1606 intstat
= bus_space_read_1(iot
, ioh
, INTS
);
1610 SPC_MISC(("extra data "));
1614 /* Stop the FIFO data path. */
1620 * Catch an interrupt from the adaptor
1623 * This is the workhorse routine of the driver.
1624 * Deficiencies (for now):
1625 * 1) always uses programmed I/O
1630 struct spc_softc
*sc
= arg
;
1631 bus_space_tag_t iot
= sc
->sc_iot
;
1632 bus_space_handle_t ioh
= sc
->sc_ioh
;
1634 struct spc_acb
*acb
;
1635 struct scsipi_periph
*periph
;
1636 struct spc_tinfo
*ti
;
1639 SPC_TRACE(("spc_intr "));
1641 ints
= bus_space_read_1(iot
, ioh
, INTS
);
1646 * Disable interrupt.
1648 bus_space_write_1(iot
, ioh
, SCTL
,
1649 bus_space_read_1(iot
, ioh
, SCTL
) & ~SCTL_INTR_ENAB
);
1651 if (sc
->sc_dma_done
!= NULL
&&
1652 sc
->sc_state
== SPC_CONNECTED
&&
1653 (sc
->sc_flags
& SPC_DOINGDMA
) != 0 &&
1654 (sc
->sc_phase
== PH_DATAOUT
|| sc
->sc_phase
== PH_DATAIN
)) {
1655 (*sc
->sc_dma_done
)(sc
);
1660 * Loop until transfer completion.
1663 * First check for abnormal conditions, such as reset.
1665 ints
= bus_space_read_1(iot
, ioh
, INTS
);
1666 SPC_MISC(("ints = 0x%x ", ints
));
1668 if ((ints
& INTS_RST
) != 0) {
1669 printf("%s: SCSI bus reset\n", device_xname(sc
->sc_dev
));
1674 * Check for less serious errors.
1676 if ((bus_space_read_1(iot
, ioh
, SERR
) & (SERR_SCSI_PAR
|SERR_SPC_PAR
))
1678 printf("%s: SCSI bus parity error\n", device_xname(sc
->sc_dev
));
1679 if (sc
->sc_prevphase
== PH_MSGIN
) {
1680 sc
->sc_flags
|= SPC_DROP_MSGIN
;
1681 spc_sched_msgout(sc
, SEND_PARITY_ERROR
);
1683 spc_sched_msgout(sc
, SEND_INIT_DET_ERR
);
1687 * If we're not already busy doing something test for the following
1689 * 1) We have been reselected by something
1690 * 2) We have selected something successfully
1691 * 3) Our selection process has timed out
1692 * 4) This is really a bus free interrupt just to get a new command
1694 * 5) Spurious interrupt?
1696 switch (sc
->sc_state
) {
1699 SPC_MISC(("ints:0x%02x ", ints
));
1701 if ((ints
& INTS_SEL
) != 0) {
1703 * We don't currently support target mode.
1705 printf("%s: target mode selected; going to BUS FREE\n",
1706 device_xname(sc
->sc_dev
));
1709 } else if ((ints
& INTS_RESEL
) != 0) {
1710 SPC_MISC(("reselected "));
1713 * If we're trying to select a target ourselves,
1714 * push our command back into the ready list.
1716 if (sc
->sc_state
== SPC_SELECTING
) {
1717 SPC_MISC(("backoff selector "));
1718 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1720 sc
->sc_nexus
= NULL
;
1721 TAILQ_INSERT_HEAD(&sc
->ready_list
, acb
, chain
);
1724 /* Save reselection ID. */
1725 sc
->sc_selid
= bus_space_read_1(iot
, ioh
, TEMP
);
1727 sc
->sc_state
= SPC_RESELECTED
;
1728 } else if ((ints
& INTS_CMD_DONE
) != 0) {
1729 SPC_MISC(("selected "));
1732 * We have selected a target. Things to do:
1733 * a) Determine what message(s) to send.
1734 * b) Verify that we're still selecting the target.
1735 * c) Mark device as busy.
1737 if (sc
->sc_state
!= SPC_SELECTING
) {
1738 printf("%s: selection out while idle; "
1739 "resetting\n", device_xname(sc
->sc_dev
));
1743 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1745 periph
= acb
->xs
->xs_periph
;
1746 ti
= &sc
->sc_tinfo
[periph
->periph_target
];
1748 sc
->sc_msgpriq
= SEND_IDENTIFY
;
1749 if (acb
->flags
& ACB_RESET
)
1750 sc
->sc_msgpriq
|= SEND_DEV_RESET
;
1751 else if (acb
->flags
& ACB_ABORT
)
1752 sc
->sc_msgpriq
|= SEND_ABORT
;
1754 #if SPC_USE_SYNCHRONOUS
1755 if ((ti
->flags
& DO_SYNC
) != 0)
1756 sc
->sc_msgpriq
|= SEND_SDTR
;
1759 if ((ti
->flags
& DO_WIDE
) != 0)
1760 sc
->sc_msgpriq
|= SEND_WDTR
;
1764 acb
->flags
|= ACB_NEXUS
;
1765 ti
->lubusy
|= (1 << periph
->periph_lun
);
1767 /* Do an implicit RESTORE POINTERS. */
1768 sc
->sc_dp
= acb
->data_addr
;
1769 sc
->sc_dleft
= acb
->data_length
;
1770 sc
->sc_cp
= (uint8_t *)&acb
->scsipi_cmd
;
1771 sc
->sc_cleft
= acb
->scsipi_cmd_length
;
1773 /* On our first connection, schedule a timeout. */
1774 if ((acb
->xs
->xs_control
& XS_CTL_POLL
) == 0)
1775 callout_reset(&acb
->xs
->xs_callout
,
1776 mstohz(acb
->timeout
), spc_timeout
, acb
);
1778 sc
->sc_state
= SPC_CONNECTED
;
1779 } else if ((ints
& INTS_TIMEOUT
) != 0) {
1780 SPC_MISC(("selection timeout "));
1782 if (sc
->sc_state
!= SPC_SELECTING
) {
1783 printf("%s: selection timeout while idle; "
1784 "resetting\n", device_xname(sc
->sc_dev
));
1788 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1793 acb
->xs
->error
= XS_SELTIMEOUT
;
1796 if (sc
->sc_state
!= SPC_IDLE
) {
1797 printf("%s: BUS FREE while not idle; "
1799 device_xname(sc
->sc_dev
), sc
->sc_state
);
1808 * Turn off selection stuff, and prepare to catch bus free
1809 * interrupts, parity errors, and phase changes.
1813 sc
->sc_prevphase
= PH_INVALID
;
1817 if ((ints
& INTS_DISCON
) != 0) {
1818 /* We've gone to BUS FREE phase. */
1819 /* disable disconnect interrupt */
1820 bus_space_write_1(iot
, ioh
, PCTL
,
1821 bus_space_read_1(iot
, ioh
, PCTL
) & ~PCTL_BFINT_ENAB
);
1822 /* XXX reset interrput */
1823 bus_space_write_1(iot
, ioh
, INTS
, ints
);
1825 switch (sc
->sc_state
) {
1826 case SPC_RESELECTED
:
1830 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1833 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
1834 if (sc
->sc_prevphase
== PH_MSGOUT
) {
1836 * If the target went to BUS FREE phase during
1837 * or immediately after sending a SDTR or WDTR
1838 * message, disable negotiation.
1840 periph
= acb
->xs
->xs_periph
;
1841 ti
= &sc
->sc_tinfo
[periph
->periph_target
];
1842 switch (sc
->sc_lastmsg
) {
1843 #if SPC_USE_SYNCHRONOUS
1845 ti
->flags
&= ~DO_SYNC
;
1846 ti
->period
= ti
->offset
= 0;
1851 ti
->flags
&= ~DO_WIDE
;
1859 if ((sc
->sc_flags
& SPC_ABORTING
) == 0) {
1861 * Section 5.1.1 of the SCSI 2 spec suggests
1862 * issuing a REQUEST SENSE following an
1863 * unexpected disconnect. Some devices go into
1864 * a contingent allegiance condition when
1865 * disconnecting, and this is necessary to
1866 * clean up their state.
1868 printf("%s: unexpected disconnect; "
1869 "sending REQUEST SENSE\n",
1870 device_xname(sc
->sc_dev
));
1872 acb
->target_stat
= SCSI_CHECK
;
1873 acb
->xs
->error
= XS_NOERROR
;
1877 acb
->xs
->error
= XS_DRIVER_STUFFUP
;
1880 case SPC_DISCONNECT
:
1881 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1883 TAILQ_INSERT_HEAD(&sc
->nexus_list
, acb
, chain
);
1884 sc
->sc_nexus
= NULL
;
1887 case SPC_CMDCOMPLETE
:
1888 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1893 else if ((ints
& INTS_CMD_DONE
) != 0 &&
1894 sc
->sc_prevphase
== PH_MSGIN
&&
1895 sc
->sc_state
!= SPC_CONNECTED
)
1900 if ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) == 0) {
1901 /* Wait for REQINIT. */
1905 bus_space_write_1(iot
, ioh
, INTS
, ints
);
1907 while ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) == 0)
1908 delay(1); /* need timeout XXX */
1914 sc
->sc_phase
= bus_space_read_1(iot
, ioh
, PSNS
) & PH_MASK
;
1916 bus_space_write_1(iot
, ioh
, PCTL
, sc
->sc_phase
);
1919 SPC_MISC(("phase=%d\n", sc
->sc_phase
));
1920 switch (sc
->sc_phase
) {
1922 if (sc
->sc_state
!= SPC_CONNECTED
&&
1923 sc
->sc_state
!= SPC_RESELECTED
)
1926 sc
->sc_prevphase
= PH_MSGOUT
;
1930 if (sc
->sc_state
!= SPC_CONNECTED
&&
1931 sc
->sc_state
!= SPC_RESELECTED
)
1934 sc
->sc_prevphase
= PH_MSGIN
;
1938 if (sc
->sc_state
!= SPC_CONNECTED
)
1941 if ((spc_debug
& SPC_SHOWMISC
) != 0) {
1942 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1944 printf("cmd=0x%02x+%d ",
1945 acb
->scsipi_cmd
.opcode
, acb
->scsipi_cmd_length
- 1);
1948 n
= spc_dataout_pio(sc
, sc
->sc_cp
, sc
->sc_cleft
);
1951 sc
->sc_prevphase
= PH_CMD
;
1955 if (sc
->sc_state
!= SPC_CONNECTED
)
1957 SPC_MISC(("dataout dleft=%d ", sc
->sc_dleft
));
1958 if (sc
->sc_dma_start
!= NULL
&&
1959 sc
->sc_dleft
> SPC_MIN_DMA_LEN
) {
1960 (*sc
->sc_dma_start
)(sc
, sc
->sc_dp
, sc
->sc_dleft
, 0);
1961 sc
->sc_prevphase
= PH_DATAOUT
;
1964 n
= spc_dataout_pio(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1967 sc
->sc_prevphase
= PH_DATAOUT
;
1971 if (sc
->sc_state
!= SPC_CONNECTED
)
1973 SPC_MISC(("datain "));
1974 if (sc
->sc_dma_start
!= NULL
&&
1975 sc
->sc_dleft
> SPC_MIN_DMA_LEN
) {
1976 (*sc
->sc_dma_start
)(sc
, sc
->sc_dp
, sc
->sc_dleft
, 1);
1977 sc
->sc_prevphase
= PH_DATAIN
;
1980 n
= spc_datain_pio(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1983 sc
->sc_prevphase
= PH_DATAIN
;
1987 if (sc
->sc_state
!= SPC_CONNECTED
)
1989 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1992 if ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_ATN
) != 0)
1993 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ATN
);
1994 bus_space_write_1(iot
, ioh
, PCTL
, PCTL_BFINT_ENAB
| PH_STAT
);
1995 while ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) == 0)
1996 DELAY(1); /* XXX needs timeout */
1997 acb
->target_stat
= bus_space_read_1(iot
, ioh
, TEMP
);
1998 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_SET_ACK
);
1999 while ((bus_space_read_1(iot
, ioh
, PSNS
) & PSNS_REQ
) != 0)
2000 DELAY(1); /* XXX needs timeout */
2001 bus_space_write_1(iot
, ioh
, SCMD
, SCMD_RST_ACK
);
2003 SPC_MISC(("target_stat=0x%02x ", acb
->target_stat
));
2004 sc
->sc_prevphase
= PH_STAT
;
2008 printf("%s: unexpected bus phase; resetting\n",
2009 device_xname(sc
->sc_dev
));
2016 callout_stop(&acb
->xs
->xs_callout
);
2017 bus_space_write_1(iot
, ioh
, INTS
, ints
);
2023 sc
->sc_state
= SPC_IDLE
;
2029 bus_space_write_1(iot
, ioh
, INTS
, ints
);
2030 bus_space_write_1(iot
, ioh
, SCTL
,
2031 bus_space_read_1(iot
, ioh
, SCTL
) | SCTL_INTR_ENAB
);
2036 spc_abort(struct spc_softc
*sc
, struct spc_acb
*acb
)
2039 /* 2 secs for the abort */
2040 acb
->timeout
= SPC_ABORT_TIMEOUT
;
2041 acb
->flags
|= ACB_ABORT
;
2043 if (acb
== sc
->sc_nexus
) {
2045 * If we're still selecting, the message will be scheduled
2046 * after selection is complete.
2048 if (sc
->sc_state
== SPC_CONNECTED
)
2049 spc_sched_msgout(sc
, SEND_ABORT
);
2051 spc_dequeue(sc
, acb
);
2052 TAILQ_INSERT_HEAD(&sc
->ready_list
, acb
, chain
);
2053 if (sc
->sc_state
== SPC_IDLE
)
2059 spc_timeout(void *arg
)
2061 struct spc_acb
*acb
= arg
;
2062 struct scsipi_xfer
*xs
= acb
->xs
;
2063 struct scsipi_periph
*periph
= xs
->xs_periph
;
2064 struct spc_softc
*sc
;
2067 sc
= device_private(periph
->periph_channel
->chan_adapter
->adapt_dev
);
2068 scsipi_printaddr(periph
);
2069 printf("timed out");
2073 if (acb
->flags
& ACB_ABORT
) {
2074 /* abort timed out */
2076 /* XXX Must reset! */
2078 /* abort the operation that has timed out */
2080 acb
->xs
->error
= XS_TIMEOUT
;
2089 * The following functions are mostly used for debugging purposes, either
2090 * directly called from the driver or from the kernel debugger.
2094 spc_show_scsi_cmd(struct spc_acb
*acb
)
2096 uint8_t *b
= (uint8_t *)&acb
->scsipi_cmd
;
2099 scsipi_printaddr(acb
->xs
->xs_periph
);
2100 if ((acb
->xs
->xs_control
& XS_CTL_RESET
) == 0) {
2101 for (i
= 0; i
< acb
->scsipi_cmd_length
; i
++) {
2112 spc_print_acb(struct spc_acb
*acb
)
2115 printf("acb@%p xs=%p flags=%x", acb
, acb
->xs
, acb
->flags
);
2116 printf(" dp=%p dleft=%d target_stat=%x\n",
2117 acb
->data_addr
, acb
->data_length
, acb
->target_stat
);
2118 spc_show_scsi_cmd(acb
);
2122 spc_print_active_acb(void)
2124 struct spc_acb
*acb
;
2125 struct spc_softc
*sc
= device_lookup_private(&spc_cd
, 0); /* XXX */
2127 printf("ready list:\n");
2128 TAILQ_FOREACH(acb
, &sc
->ready_list
, chain
)
2131 if (sc
->sc_nexus
!= NULL
)
2132 spc_print_acb(sc
->sc_nexus
);
2133 printf("nexus list:\n");
2134 TAILQ_FOREACH(acb
, &sc
->nexus_list
, chain
)
2139 spc_dump89352(struct spc_softc
*sc
)
2141 bus_space_tag_t iot
= sc
->sc_iot
;
2142 bus_space_handle_t ioh
= sc
->sc_ioh
;
2144 printf("mb89352: BDID=%x SCTL=%x SCMD=%x TMOD=%x\n",
2145 bus_space_read_1(iot
, ioh
, BDID
),
2146 bus_space_read_1(iot
, ioh
, SCTL
),
2147 bus_space_read_1(iot
, ioh
, SCMD
),
2148 bus_space_read_1(iot
, ioh
, TMOD
));
2149 printf(" INTS=%x PSNS=%x SSTS=%x SERR=%x PCTL=%x\n",
2150 bus_space_read_1(iot
, ioh
, INTS
),
2151 bus_space_read_1(iot
, ioh
, PSNS
),
2152 bus_space_read_1(iot
, ioh
, SSTS
),
2153 bus_space_read_1(iot
, ioh
, SERR
),
2154 bus_space_read_1(iot
, ioh
, PCTL
));
2155 printf(" MBC=%x DREG=%x TEMP=%x TCH=%x TCM=%x\n",
2156 bus_space_read_1(iot
, ioh
, MBC
),
2158 bus_space_read_1(iot
, ioh
, DREG
),
2162 bus_space_read_1(iot
, ioh
, TEMP
),
2163 bus_space_read_1(iot
, ioh
, TCH
),
2164 bus_space_read_1(iot
, ioh
, TCM
));
2165 printf(" TCL=%x EXBF=%x\n",
2166 bus_space_read_1(iot
, ioh
, TCL
),
2167 bus_space_read_1(iot
, ioh
, EXBF
));
2171 spc_dump_driver(struct spc_softc
*sc
)
2173 struct spc_tinfo
*ti
;
2176 printf("nexus=%p prevphase=%x\n", sc
->sc_nexus
, sc
->sc_prevphase
);
2177 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x "
2178 "currmsg=%x\n", sc
->sc_state
, sc
->sc_imess
[0],
2179 sc
->sc_msgpriq
, sc
->sc_msgoutq
, sc
->sc_lastmsg
, sc
->sc_currmsg
);
2180 for (i
= 0; i
< 7; i
++) {
2181 ti
= &sc
->sc_tinfo
[i
];
2182 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2183 i
, ti
->cmds
, ti
->dconns
, ti
->touts
);
2184 printf(" %d senses flags=%x\n", ti
->senses
, ti
->flags
);