1 /* $NetBSD: mha.c,v 1.51 2008/12/18 05:56:42 isaki Exp $ */
4 * Copyright (c) 1996-1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, Masaru Oki, Takumi Nakamura, Masanobu Saitoh and
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.
34 * Copyright (c) 1994 Jarle Greipsland
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. The name of the author may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
52 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
57 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: mha.c,v 1.51 2008/12/18 05:56:42 isaki Exp $");
66 /* Synchronous data transfers? */
67 #define SPC_USE_SYNCHRONOUS 0
68 #define SPC_SYNC_REQ_ACK_OFS 8
70 /* Default DMA mode? */
71 #define MHA_DMA_LIMIT_XFER 1
72 #define MHA_DMA_BURST_XFER 1
73 #define MHA_DMA_SHORT_BUS_CYCLE 1
75 #define MHA_DMA_DATAIN (0 | (MHA_DMA_LIMIT_XFER << 1) \
76 | (MHA_DMA_BURST_XFER << 2) \
77 | (MHA_DMA_SHORT_BUS_CYCLE << 3))
78 #define MHA_DMA_DATAOUT (1 | (MHA_DMA_LIMIT_XFER << 1) \
79 | (MHA_DMA_BURST_XFER << 2) \
80 | (MHA_DMA_SHORT_BUS_CYCLE << 3))
82 /* Include debug functions? At the end of this file there are a bunch of
83 * functions that will print out various information regarding queued SCSI
84 * commands, driver state and chip contents. You can call them from the
85 * kernel debugger. If you set SPC_DEBUG to 0 they are not included (the
86 * kernel uses less memory) but you lose the debugging facilities.
90 /* End of customizable parameters */
93 * MB86601A SCSI Protocol Controller (SPC) routines for MANKAI Mach-2
96 #include <sys/types.h>
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/kernel.h>
100 #include <sys/errno.h>
101 #include <sys/ioctl.h>
102 #include <sys/device.h>
104 #include <sys/proc.h>
105 #include <sys/queue.h>
107 #include <machine/bus.h>
109 #include <dev/scsipi/scsi_spc.h>
110 #include <dev/scsipi/scsi_all.h>
111 #include <dev/scsipi/scsipi_all.h>
112 #include <dev/scsipi/scsi_message.h>
113 #include <dev/scsipi/scsiconf.h>
115 #include <x68k/x68k/iodevice.h>
116 #include <x68k/dev/mb86601reg.h>
117 #include <x68k/dev/mhavar.h>
118 #include <x68k/dev/intiovar.h>
119 #include <x68k/dev/scsiromvar.h>
122 #define WAIT {if (sc->sc_pc[2]) {printf("[W_%d", __LINE__); while (sc->sc_pc[2] & 0x40);printf("]");}}
124 #define WAIT {while (sc->sc_pc[2] & 0x40);}
127 #define SSR (sc->sc_pc[2])
128 #define SS_IREQUEST 0x80
130 #define SS_DREG_FULL 0x02
132 #define NSR (sc->sc_pc[3])
134 #define SIR (sc->sc_pc[4])
136 #define CMR (sc->sc_pc[5])
137 #define CMD_SEL_AND_CMD 0x00
138 #define CMD_SELECT 0x09
139 #define CMD_SET_ATN 0x0a
140 #define CMD_RESET_ATN 0x0b
141 #define CMD_RESET_ACK 0x0d
142 #define CMD_SEND_FROM_MPU 0x10
143 #define CMD_SEND_FROM_DMA 0x11
144 #define CMD_RECEIVE_TO_MPU 0x12
145 #define CMD_RECEIVE_TO_DMA 0x13
146 #define CMD_RECEIVE_MSG 0x1a
147 #define CMD_RECEIVE_STS 0x1c
148 #define CMD_SOFT_RESET 0x40
149 #define CMD_SCSI_RESET 0x42
150 #define CMD_SET_UP_REG 0x43
152 #define SCR (sc->sc_pc[11])
154 #define TMR (sc->sc_pc[12])
156 #define TM_ASYNC 0x00
158 #define WAR (sc->sc_pc[15])
159 #define WA_MCSBUFWIN 0x00
160 #define WA_UPMWIN 0x80
161 #define WA_INITWIN 0xc0
163 #define MBR (sc->sc_pc[15])
165 #define ISCSR (sc->sc_ps[2])
167 #define CCR (sc->sc_pcx[0])
168 #define OIR (sc->sc_pcx[1])
169 #define AMR (sc->sc_pcx[2])
170 #define SMR (sc->sc_pcx[3])
171 #define SRR (sc->sc_pcx[4])
172 #define STR (sc->sc_pcx[5])
173 #define RTR (sc->sc_pcx[6])
174 #define ATR (sc->sc_pcx[7])
175 #define PER (sc->sc_pcx[8])
176 #define IER (sc->sc_pcx[9])
179 #define GLR (sc->sc_pcx[10])
180 #define DMR (sc->sc_pcx[11])
181 #define IMR (sc->sc_pcx[12])
184 #define Debugger() panic("should call debugger here (mha.c)")
189 #define SPC_SHOWACBS 0x01
190 #define SPC_SHOWINTS 0x02
191 #define SPC_SHOWCMDS 0x04
192 #define SPC_SHOWMISC 0x08
193 #define SPC_SHOWTRAC 0x10
194 #define SPC_SHOWSTART 0x20
195 #define SPC_SHOWPHASE 0x40
196 #define SPC_SHOWDMA 0x80
197 #define SPC_SHOWCCMDS 0x100
198 #define SPC_SHOWMSGS 0x200
199 #define SPC_DOBREAK 0x400
205 SPC_SHOWSTART
|SPC_SHOWTRAC
;
209 #define SPC_ACBS(str) do {if (mha_debug & SPC_SHOWACBS) printf str;} while (0)
210 #define SPC_MISC(str) do {if (mha_debug & SPC_SHOWMISC) printf str;} while (0)
211 #define SPC_INTS(str) do {if (mha_debug & SPC_SHOWINTS) printf str;} while (0)
212 #define SPC_TRACE(str) do {if (mha_debug & SPC_SHOWTRAC) printf str;} while (0)
213 #define SPC_CMDS(str) do {if (mha_debug & SPC_SHOWCMDS) printf str;} while (0)
214 #define SPC_START(str) do {if (mha_debug & SPC_SHOWSTART) printf str;}while (0)
215 #define SPC_PHASE(str) do {if (mha_debug & SPC_SHOWPHASE) printf str;}while (0)
216 #define SPC_DMA(str) do {if (mha_debug & SPC_SHOWDMA) printf str;}while (0)
217 #define SPC_MSGS(str) do {if (mha_debug & SPC_SHOWMSGS) printf str;}while (0)
218 #define SPC_BREAK() do {if ((mha_debug & SPC_DOBREAK) != 0) Debugger();} while (0)
219 #define SPC_ASSERT(x) do {if (x) {} else {printf("%s at line %d: assertion failed\n", device_xname(sc->sc_dev), __LINE__); Debugger();}} while (0)
221 #define SPC_ACBS(str)
222 #define SPC_MISC(str)
223 #define SPC_INTS(str)
224 #define SPC_TRACE(str)
225 #define SPC_CMDS(str)
226 #define SPC_START(str)
227 #define SPC_PHASE(str)
229 #define SPC_MSGS(str)
231 #define SPC_ASSERT(x)
234 int mhamatch(device_t
, cfdata_t
, void *);
235 void mhaattach(device_t
, device_t
, void *);
236 void mhaselect(struct mha_softc
*, u_char
, u_char
, u_char
*, u_char
);
237 void mha_scsi_reset(struct mha_softc
*);
238 void mha_reset(struct mha_softc
*);
239 void mha_free_acb(struct mha_softc
*, struct acb
*, int);
240 void mha_sense(struct mha_softc
*, struct acb
*);
241 void mha_msgin(struct mha_softc
*);
242 void mha_msgout(struct mha_softc
*);
243 int mha_dataout_pio(struct mha_softc
*, u_char
*, int);
244 int mha_datain_pio(struct mha_softc
*, u_char
*, int);
245 int mha_dataout(struct mha_softc
*, u_char
*, int);
246 int mha_datain(struct mha_softc
*, u_char
*, int);
247 void mha_abort(struct mha_softc
*, struct acb
*);
248 void mha_init(struct mha_softc
*);
249 void mha_scsi_request(struct scsipi_channel
*, scsipi_adapter_req_t
, void *);
250 void mha_poll(struct mha_softc
*, struct acb
*);
251 void mha_sched(struct mha_softc
*);
252 void mha_done(struct mha_softc
*, struct acb
*);
254 void mha_timeout(void *);
255 void mha_minphys(struct buf
*);
256 void mha_dequeue(struct mha_softc
*, struct acb
*);
257 inline void mha_setsync(struct mha_softc
*, struct spc_tinfo
*);
259 void mha_print_acb(struct acb
*);
260 void mha_show_scsi_cmd(struct acb
*);
261 void mha_print_active_acb(void);
262 void mha_dump_driver(struct mha_softc
*);
265 static int mha_dataio_dma(int, int, struct mha_softc
*, u_char
*, int);
267 CFATTACH_DECL_NEW(mha
, sizeof(struct mha_softc
),
268 mhamatch
, mhaattach
, NULL
, NULL
);
270 extern struct cfdriver mha_cd
;
273 * returns non-zero value if a controller is found.
276 mhamatch(device_t parent
, cfdata_t cf
, void *aux
)
278 struct intio_attach_args
*ia
= aux
;
279 bus_space_tag_t iot
= ia
->ia_bst
;
280 bus_space_handle_t ioh
;
283 if (ia
->ia_addr
!= 0xea0000)
286 if (intio_map_allocate_region(device_parent(parent
), ia
,
287 INTIO_MAP_TESTONLY
) < 0) /* FAKE */
290 if (bus_space_map(iot
, ia
->ia_addr
, 0x20, BUS_SPACE_MAP_SHIFTED
,
293 if (!badaddr((void *)IIOV(ia
->ia_addr
+ 0)))
295 bus_space_unmap(iot
, ioh
, 0x20);
303 struct mha_softc
*tmpsc
;
306 mhaattach(device_t parent
, device_t self
, void *aux
)
308 struct mha_softc
*sc
= device_private(self
);
309 struct intio_attach_args
*ia
= aux
;
311 tmpsc
= sc
; /* XXX */
314 aprint_normal(": Mankai Mach-2 Fast SCSI Host Adaptor\n");
316 SPC_TRACE(("mhaattach "));
317 sc
->sc_state
= SPC_INIT
;
318 sc
->sc_iobase
= (void *)IIOV(ia
->ia_addr
+ 0x80); /* XXX */
319 intio_map_allocate_region(device_parent(parent
), ia
, INTIO_MAP_ALLOCATE
);
321 sc
->sc_dmat
= ia
->ia_dmat
;
323 sc
->sc_pc
= (volatile u_char
*)sc
->sc_iobase
;
324 sc
->sc_ps
= (volatile u_short
*)sc
->sc_iobase
;
325 sc
->sc_pcx
= &sc
->sc_pc
[0x10];
327 sc
->sc_id
= IODEVbase
->io_sram
[0x70] & 0x7; /* XXX */
329 intio_intr_establish(ia
->ia_intr
, "mha", mhaintr
, sc
);
331 mha_init(sc
); /* Init chip and driver */
333 mha_scsi_reset(sc
); /* XXX: some devices need this. */
335 sc
->sc_phase
= BUSFREE_PHASE
;
338 * Fill in the adapter.
340 sc
->sc_adapter
.adapt_dev
= sc
->sc_dev
;
341 sc
->sc_adapter
.adapt_nchannels
= 1;
342 sc
->sc_adapter
.adapt_openings
= 7;
343 sc
->sc_adapter
.adapt_max_periph
= 1;
344 sc
->sc_adapter
.adapt_ioctl
= NULL
;
345 sc
->sc_adapter
.adapt_minphys
= mha_minphys
;
346 sc
->sc_adapter
.adapt_request
= mha_scsi_request
;
348 sc
->sc_channel
.chan_adapter
= &sc
->sc_adapter
;
349 sc
->sc_channel
.chan_bustype
= &scsi_bustype
;
350 sc
->sc_channel
.chan_channel
= 0;
351 sc
->sc_channel
.chan_ntargets
= 8;
352 sc
->sc_channel
.chan_nluns
= 8;
353 sc
->sc_channel
.chan_id
= sc
->sc_id
;
355 sc
->sc_spcinitialized
= 0;
368 IER
= IE_ALL
; /* \e$B$9$Y$F$N3d$j9~$_$r5v2D\e(B */
377 while (SSR
& SS_IREQUEST
) {
381 CMR
= CMD_SET_UP_REG
; /* setup reg cmd. */
383 SPC_TRACE(("waiting for intr..."));
384 while (!(SSR
& SS_IREQUEST
))
390 config_found(self
, &sc
->sc_channel
, scsiprint
);
395 mha_reset(struct mha_softc
*sc
)
399 CMR
= CMD_SOFT_RESET
;
400 __asm
volatile ("nop"); /* XXX wait (4clk in 20 MHz) ??? */
401 dummy
= sc
->sc_ps
[-1];
402 dummy
= sc
->sc_ps
[-1];
403 dummy
= sc
->sc_ps
[-1];
404 dummy
= sc
->sc_ps
[-1];
405 __asm
volatile ("nop");
406 CMR
= CMD_SOFT_RESET
;
407 sc
->sc_spcinitialized
= 0;
408 CMR
= CMD_SET_UP_REG
; /* setup reg cmd. */
409 while(!sc
->sc_spcinitialized
);
411 sc
->sc_id
= IODEVbase
->io_sram
[0x70] & 0x7; /* XXX */
417 * Pull the SCSI RST line for 500us.
420 mha_scsi_reset(struct mha_softc
*sc
)
423 CMR
= CMD_SCSI_RESET
; /* SCSI RESET */
424 while (!(SSR
&SS_IREQUEST
))
429 * Initialize mha SCSI driver.
432 mha_init(struct mha_softc
*sc
)
437 if (sc
->sc_state
== SPC_INIT
) {
438 /* First time through; initialize. */
439 TAILQ_INIT(&sc
->ready_list
);
440 TAILQ_INIT(&sc
->nexus_list
);
441 TAILQ_INIT(&sc
->free_list
);
444 memset(acb
, 0, sizeof(sc
->sc_acb
));
445 for (r
= 0; r
< sizeof(sc
->sc_acb
) / sizeof(*acb
); r
++) {
446 TAILQ_INSERT_TAIL(&sc
->free_list
, acb
, chain
);
449 memset(&sc
->sc_tinfo
, 0, sizeof(sc
->sc_tinfo
));
451 r
= bus_dmamem_alloc(sc
->sc_dmat
, MAXBSIZE
, 0, 0,
452 sc
->sc_dmaseg
, 1, &sc
->sc_ndmasegs
,
455 panic("mha_init: cannot allocate DMA memory");
456 if (sc
->sc_ndmasegs
!= 1)
457 panic("mha_init: number of segment > 1??");
458 r
= bus_dmamem_map(sc
->sc_dmat
, sc
->sc_dmaseg
, sc
->sc_ndmasegs
,
459 MAXBSIZE
, &sc
->sc_dmabuf
, BUS_DMA_NOWAIT
);
461 panic("mha_init: cannot map DMA memory");
462 r
= bus_dmamap_create(sc
->sc_dmat
, MAXBSIZE
, 1,
463 MAXBSIZE
, 0, BUS_DMA_NOWAIT
,
466 panic("mha_init: cannot create dmamap structure");
467 r
= bus_dmamap_load(sc
->sc_dmat
, sc
->sc_dmamap
,
468 sc
->sc_dmabuf
, MAXBSIZE
, NULL
,
471 panic("mha_init: cannot load DMA buffer into dmamap");
474 /* Cancel any active commands. */
475 sc
->sc_flags
|= SPC_ABORTING
;
476 sc
->sc_state
= SPC_IDLE
;
477 if ((acb
= sc
->sc_nexus
) != NULL
) {
478 acb
->xs
->error
= XS_DRIVER_STUFFUP
;
481 while ((acb
= TAILQ_FIRST(&sc
->nexus_list
)) != NULL
) {
482 acb
->xs
->error
= XS_DRIVER_STUFFUP
;
487 sc
->sc_phase
= sc
->sc_prevphase
= INVALID_PHASE
;
488 for (r
= 0; r
< 8; r
++) {
489 struct spc_tinfo
*ti
= &sc
->sc_tinfo
[r
];
492 #if SPC_USE_SYNCHRONOUS
493 ti
->flags
|= T_SYNCMODE
;
494 ti
->period
= sc
->sc_minsync
;
495 ti
->offset
= SPC_SYNC_REQ_ACK_OFS
;
497 ti
->period
= ti
->offset
= 0;
502 sc
->sc_state
= SPC_IDLE
;
506 mha_free_acb(struct mha_softc
*sc
, struct acb
*acb
, int flags
)
513 TAILQ_INSERT_HEAD(&sc
->free_list
, acb
, chain
);
516 * If there were none, wake anybody waiting for one to come free,
517 * starting with queued entries.
519 if (TAILQ_NEXT(acb
, chain
) == NULL
)
520 wakeup(&sc
->free_list
);
526 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
531 * 1) Command inserted into ready list
532 * 2) Command selected for execution
533 * 3) Command won arbitration and has selected target device
534 * 4) Send message out (identify message, eventually also sync.negotiations)
536 * 5a) Receive disconnect message, disconnect.
537 * 5b) Reselected by target
538 * 5c) Receive identify message from target.
539 * 6) Send or receive data
541 * 8) Receive message (command complete etc.)
542 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
543 * Repeat 2-8 (no disconnects please...)
547 * Start a selection. This is used by mha_sched() to select an idle target,
548 * and by mha_done() to immediately reselect a target to get sense information.
551 mhaselect(struct mha_softc
*sc
, u_char target
, u_char lun
, u_char
*cmd
,
557 s
= splbio(); /* XXX */
559 SPC_TRACE(("[mhaselect(t%d,l%d,cmd:%x)] ", target
, lun
, *(u_char
*)cmd
));
561 /* CDB \e$B$r\e(B SPC \e$B$N\e(B MCS REG \e$B$K%;%C%H$9$k\e(B */
562 /* Now the command into the FIFO */
566 for (i
= 0; i
< clen
; i
++) {
570 SPC_MISC((" %02x", c
));
573 SPC_MISC(("], target=%d\n", target
));
575 memcpy(sc
->sc_pcx
, cmd
, clen
);
578 panic("scsistart: already selected...");
579 sc
->sc_phase
= COMMAND_PHASE
;
581 /* new state ASP_SELECTING */
582 sc
->sc_state
= SPC_SELECTING
;
588 CMR
= CMD_SEL_AND_CMD
; /* select & cmd */
595 mha_reselect(struct mha_softc
*sc
, u_char message
)
597 u_char selid
, target
, lun
;
599 struct scsipi_periph
*periph
;
600 struct spc_tinfo
*ti
;
603 * The SCSI chip made a snapshot of the data bus while the reselection
604 * was being negotiated. This enables us to determine which target did
607 selid
= sc
->sc_selid
& ~(1 << sc
->sc_id
);
608 if (selid
& (selid
- 1)) {
609 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
610 device_xname(sc
->sc_dev
), selid
);
616 * Search wait queue for disconnected cmd
617 * The list should be short, so I haven't bothered with
618 * any more sophisticated structures than a simple
619 * singly linked list.
621 target
= ffs(selid
) - 1;
622 lun
= message
& 0x07;
623 TAILQ_FOREACH(acb
, &sc
->nexus_list
, chain
) {
624 periph
= acb
->xs
->xs_periph
;
625 if (periph
->periph_target
== target
&&
626 periph
->periph_lun
== lun
)
630 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
631 device_xname(sc
->sc_dev
), target
, lun
);
636 /* Make this nexus active again. */
637 TAILQ_REMOVE(&sc
->nexus_list
, acb
, chain
);
638 sc
->sc_state
= SPC_HASNEXUS
;
640 ti
= &sc
->sc_tinfo
[target
];
641 ti
->lubusy
|= (1 << lun
);
644 if (acb
->flags
& ACB_RESET
)
645 mha_sched_msgout(sc
, SEND_DEV_RESET
);
646 else if (acb
->flags
& ACB_ABORTED
)
647 mha_sched_msgout(sc
, SEND_ABORT
);
649 /* Do an implicit RESTORE POINTERS. */
650 sc
->sc_dp
= acb
->daddr
;
651 sc
->sc_dleft
= acb
->dleft
;
652 sc
->sc_cp
= (u_char
*)&acb
->cmd
;
653 sc
->sc_cleft
= acb
->clen
;
658 mha_sched_msgout(sc
, SEND_DEV_RESET
);
662 mha_sched_msgout(sc
, SEND_ABORT
);
667 * Start a SCSI-command
668 * This function is called by the higher level SCSI-driver to queue/run
672 mha_scsi_request(struct scsipi_channel
*chan
, scsipi_adapter_req_t req
,
675 struct scsipi_xfer
*xs
;
676 struct scsipi_periph
*periph
;
677 struct mha_softc
*sc
= (void *)chan
->chan_adapter
->adapt_dev
;
682 case ADAPTER_REQ_RUN_XFER
:
684 periph
= xs
->xs_periph
;
686 SPC_TRACE(("[mha_scsi_cmd] "));
687 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs
->cmd
->opcode
, xs
->cmdlen
,
688 periph
->periph_target
));
690 flags
= xs
->xs_control
;
692 /* Get a mha command block */
694 acb
= TAILQ_FIRST(&sc
->free_list
);
696 TAILQ_REMOVE(&sc
->free_list
, acb
, chain
);
697 ACB_SETQ(acb
, ACB_QNONE
);
701 xs
->error
= XS_RESOURCE_SHORTAGE
;
710 memcpy(&acb
->cmd
, xs
->cmd
, xs
->cmdlen
);
711 acb
->clen
= xs
->cmdlen
;
712 acb
->daddr
= xs
->data
;
713 acb
->dleft
= xs
->datalen
;
717 ACB_SETQ(acb
, ACB_QREADY
);
718 TAILQ_INSERT_TAIL(&sc
->ready_list
, acb
, chain
);
720 callout_reset(&acb
->xs
->xs_callout
,
721 mstohz(xs
->timeout
), mha_timeout
, acb
);
725 * \e$B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k\e(B
727 if (sc
->sc_state
== SPC_IDLE
)
732 if (flags
& XS_CTL_POLL
) {
733 /* Not allowed to use interrupts, use polling instead */
737 SPC_MISC(("SUCCESSFULLY_QUEUED"));
740 case ADAPTER_REQ_GROW_RESOURCES
:
741 /* XXX Not supported. */
744 case ADAPTER_REQ_SET_XFER_MODE
:
745 /* XXX Not supported. */
751 * Adjust transfer size in buffer structure
754 mha_minphys(struct buf
*bp
)
757 SPC_TRACE(("mha_minphys "));
762 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
765 mha_poll(struct mha_softc
*sc
, struct acb
*acb
)
767 struct scsipi_xfer
*xs
= acb
->xs
;
768 int count
= xs
->timeout
* 100;
773 SPC_TRACE(("[mha_poll] "));
777 * If we had interrupts enabled, would we
778 * have got an interrupt?
780 if (SSR
& SS_IREQUEST
)
782 if ((xs
->xs_status
& XS_STS_DONE
) != 0)
786 if (sc
->sc_state
== SPC_IDLE
) {
787 SPC_TRACE(("[mha_poll: rescheduling] "));
795 SPC_MISC(("mha_poll: timeout"));
796 mha_timeout((void *)acb
);
803 * LOW LEVEL SCSI UTILITIES
807 * Set synchronous transfer offset and period.
810 mha_setsync(struct mha_softc
*sc
, struct spc_tinfo
*ti
)
815 * Schedule a SCSI operation. This has now been pulled out of the interrupt
816 * handler so that we may call it from mha_scsi_cmd and mha_done. This may
817 * save us an unecessary interrupt just to get things going. Should only be
818 * called when state == SPC_IDLE and at bio pl.
821 mha_sched(struct mha_softc
*sc
)
823 struct scsipi_periph
*periph
;
827 SPC_TRACE(("[mha_sched] "));
828 if (sc
->sc_state
!= SPC_IDLE
)
829 panic("mha_sched: not IDLE (state=%d)", sc
->sc_state
);
831 if (sc
->sc_flags
& SPC_ABORTING
)
835 * Find first acb in ready queue that is for a target/lunit
836 * combinations that is not busy.
838 TAILQ_FOREACH(acb
, &sc
->ready_list
, chain
) {
839 struct spc_tinfo
*ti
;
840 periph
= acb
->xs
->xs_periph
;
841 t
= periph
->periph_target
;
842 ti
= &sc
->sc_tinfo
[t
];
843 if (!(ti
->lubusy
& (1 << periph
->periph_lun
))) {
844 if ((acb
->flags
& ACB_QBITS
) != ACB_QREADY
)
845 panic("mha: busy entry on ready list");
846 TAILQ_REMOVE(&sc
->ready_list
, acb
, chain
);
847 ACB_SETQ(acb
, ACB_QNONE
);
850 sc
->sc_prevphase
= INVALID_PHASE
;
851 sc
->sc_dp
= acb
->daddr
;
852 sc
->sc_dleft
= acb
->dleft
;
853 ti
->lubusy
|= (1<<periph
->periph_lun
);
854 mhaselect(sc
, t
, periph
->periph_lun
,
855 (u_char
*)&acb
->cmd
, acb
->clen
);
858 SPC_MISC(("%d:%d busy\n",
859 periph
->periph_target
,
860 periph
->periph_lun
));
866 * POST PROCESSING OF SCSI_CMD (usually current)
869 mha_done(struct mha_softc
*sc
, struct acb
*acb
)
871 struct scsipi_xfer
*xs
= acb
->xs
;
872 struct scsipi_periph
*periph
= xs
->xs_periph
;
873 struct spc_tinfo
*ti
= &sc
->sc_tinfo
[periph
->periph_target
];
875 SPC_TRACE(("[mha_done(error:%x)] ", xs
->error
));
878 callout_stop(&acb
->xs
->xs_callout
);
882 * Now, if we've come here with no error code, i.e. we've kept the
883 * initial XS_NOERROR, and the status code signals that we should
884 * check sense, we'll need to set up a request sense cmd block and
885 * push the command back into the ready queue *before* any other
886 * commands for this target/lunit, else we lose the sense info.
887 * We don't support chk sense conditions for the request sense cmd.
889 if (xs
->error
== XS_NOERROR
) {
890 if ((acb
->flags
& ACB_ABORTED
) != 0) {
891 xs
->error
= XS_TIMEOUT
;
892 } else if (acb
->flags
& ACB_CHKSENSE
) {
893 xs
->error
= XS_SENSE
;
895 xs
->status
= acb
->stat
& ST_MASK
;
896 switch (xs
->status
) {
898 xs
->resid
= acb
->dleft
;
904 xs
->resid
= acb
->dleft
;
907 xs
->error
= XS_DRIVER_STUFFUP
;
909 printf("%s: mha_done: bad stat 0x%x\n",
910 device_xname(sc
->sc_dev
), acb
->stat
);
918 if ((mha_debug
& SPC_SHOWMISC
) != 0) {
920 printf("resid=%d ", xs
->resid
);
921 if (xs
->error
== XS_SENSE
)
922 printf("sense=0x%02x\n", xs
->sense
.scsi_sense
.response_code
);
924 printf("error=%d\n", xs
->error
);
929 * Remove the ACB from whatever queue it's on.
931 switch (acb
->flags
& ACB_QBITS
) {
933 if (acb
!= sc
->sc_nexus
) {
934 panic("%s: floating acb", device_xname(sc
->sc_dev
));
937 sc
->sc_state
= SPC_IDLE
;
938 ti
->lubusy
&= ~(1<<periph
->periph_lun
);
942 TAILQ_REMOVE(&sc
->ready_list
, acb
, chain
);
945 TAILQ_REMOVE(&sc
->nexus_list
, acb
, chain
);
946 ti
->lubusy
&= ~(1<<periph
->periph_lun
);
949 panic("%s: dequeue: busy acb on free list",
950 device_xname(sc
->sc_dev
));
953 panic("%s: dequeue: unknown queue %d",
954 device_xname(sc
->sc_dev
), acb
->flags
& ACB_QBITS
);
957 /* Put it on the free list, and clear flags. */
959 TAILQ_INSERT_HEAD(&sc
->free_list
, acb
, chain
);
960 acb
->flags
= ACB_QFREE
;
962 mha_free_acb(sc
, acb
, xs
->xs_control
);
970 mha_dequeue(struct mha_softc
*sc
, struct acb
*acb
)
973 if (acb
->flags
& ACB_QNEXUS
) {
974 TAILQ_REMOVE(&sc
->nexus_list
, acb
, chain
);
976 TAILQ_REMOVE(&sc
->ready_list
, acb
, chain
);
981 * INTERRUPT/PROTOCOL ENGINE
985 * Schedule an outgoing message by prioritizing it, and asserting
986 * attention on the bus. We can only do this when we are the initiator
987 * else there will be an illegal command interrupt.
989 #define mha_sched_msgout(m) \
991 SPC_MISC(("mha_sched_msgout %d ", m)); \
993 sc->sc_msgpriq |= (m); \
998 * The SCSI bus is already in the MSGI phase and there is a message byte
999 * on the bus, along with an asserted REQ signal.
1002 mha_msgin(struct mha_softc
*sc
)
1006 SPC_TRACE(("[mha_msgin(curmsglen:%d)] ", sc
->sc_imlen
));
1009 * Prepare for a new message. A message should (according
1010 * to the SCSI standard) be transmitted in one single
1011 * MESSAGE_IN_PHASE. If we have been in some other phase,
1012 * then this is a new message.
1014 if (sc
->sc_prevphase
!= MESSAGE_IN_PHASE
) {
1015 sc
->sc_flags
&= ~SPC_DROP_MSGI
;
1021 v
= MBR
; /* modified byte */
1024 sc
->sc_imess
[sc
->sc_imlen
] = v
;
1027 * If we're going to reject the message, don't bother storing
1028 * the incoming bytes. But still, we need to ACK them.
1031 if ((sc
->sc_flags
& SPC_DROP_MSGI
)) {
1033 /* ESPCMD(sc, ESPCMD_MSGOK);*/
1034 printf("<dropping msg byte %x>",
1035 sc
->sc_imess
[sc
->sc_imlen
]);
1039 if (sc
->sc_imlen
>= SPC_MAX_MSG_LEN
) {
1040 mha_sched_msgout(SEND_REJECT
);
1041 sc
->sc_flags
|= SPC_DROP_MSGI
;
1045 * This testing is suboptimal, but most
1046 * messages will be of the one byte variety, so
1047 * it should not effect performance
1050 if (sc
->sc_imlen
== 1 && MSG_IS1BYTE(sc
->sc_imess
[0]))
1052 if (sc
->sc_imlen
== 2 && MSG_IS2BYTE(sc
->sc_imess
[0]))
1054 if (sc
->sc_imlen
>= 3 && MSG_ISEXTENDED(sc
->sc_imess
[0]) &&
1055 sc
->sc_imlen
== sc
->sc_imess
[1] + 2)
1059 /* Ack what we have so far */
1060 ESPCMD(sc
, ESPCMD_MSGOK
);
1065 SPC_MSGS(("gotmsg(%x)", sc
->sc_imess
[0]));
1067 * Now we should have a complete message (1 byte, 2 byte
1068 * and moderately long extended messages). We only handle
1069 * extended messages which total length is shorter than
1070 * SPC_MAX_MSG_LEN. Longer messages will be amputated.
1072 if (sc
->sc_state
== SPC_HASNEXUS
) {
1073 struct acb
*acb
= sc
->sc_nexus
;
1074 struct spc_tinfo
*ti
=
1075 &sc
->sc_tinfo
[acb
->xs
->xs_periph
->periph_target
];
1077 switch (sc
->sc_imess
[0]) {
1078 case MSG_CMDCOMPLETE
:
1079 SPC_MSGS(("cmdcomplete "));
1080 if (sc
->sc_dleft
< 0) {
1081 struct scsipi_periph
*periph
= acb
->xs
->xs_periph
;
1082 printf("mha: %d extra bytes from %d:%d\n",
1084 periph
->periph_target
,
1085 periph
->periph_lun
);
1088 acb
->xs
->resid
= acb
->dleft
= sc
->sc_dleft
;
1089 sc
->sc_flags
|= SPC_BUSFREE_OK
;
1092 case MSG_MESSAGE_REJECT
:
1094 if (mha_debug
& SPC_SHOWMSGS
)
1095 printf("%s: our msg rejected by target\n",
1096 device_xname(sc
->sc_dev
));
1098 #if 1 /* XXX - must remember last message */
1099 scsipi_printaddr(acb
->xs
->xs_periph
);
1100 printf("MSG_MESSAGE_REJECT>>");
1102 if (sc
->sc_flags
& SPC_SYNCHNEGO
) {
1103 ti
->period
= ti
->offset
= 0;
1104 sc
->sc_flags
&= ~SPC_SYNCHNEGO
;
1105 ti
->flags
&= ~T_NEGOTIATE
;
1107 /* Not all targets understand INITIATOR_DETECTED_ERR */
1108 if (sc
->sc_msgout
== SEND_INIT_DET_ERR
)
1109 mha_sched_msgout(SEND_ABORT
);
1112 SPC_MSGS(("noop "));
1114 case MSG_DISCONNECT
:
1115 SPC_MSGS(("disconnect "));
1117 sc
->sc_flags
|= SPC_DISCON
;
1118 sc
->sc_flags
|= SPC_BUSFREE_OK
;
1119 if ((acb
->xs
->xs_periph
->periph_quirks
& PQUIRK_AUTOSAVE
) == 0)
1122 case MSG_SAVEDATAPOINTER
:
1123 SPC_MSGS(("save datapointer "));
1124 acb
->dleft
= sc
->sc_dleft
;
1125 acb
->daddr
= sc
->sc_dp
;
1127 case MSG_RESTOREPOINTERS
:
1128 SPC_MSGS(("restore datapointer "));
1130 mha_sched_msgout(SEND_ABORT
);
1131 printf("%s: no DATAPOINTERs to restore\n",
1132 device_xname(sc
->sc_dev
));
1135 sc
->sc_dp
= acb
->daddr
;
1136 sc
->sc_dleft
= acb
->dleft
;
1138 case MSG_PARITY_ERROR
:
1139 printf("%s:target%d: MSG_PARITY_ERROR\n",
1140 device_xname(sc
->sc_dev
),
1141 acb
->xs
->xs_periph
->periph_target
);
1144 SPC_MSGS(("extended(%x) ", sc
->sc_imess
[2]));
1145 switch (sc
->sc_imess
[2]) {
1147 SPC_MSGS(("SDTR period %d, offset %d ",
1148 sc
->sc_imess
[3], sc
->sc_imess
[4]));
1149 ti
->period
= sc
->sc_imess
[3];
1150 ti
->offset
= sc
->sc_imess
[4];
1151 if (sc
->sc_minsync
== 0) {
1152 /* We won't do synch */
1154 mha_sched_msgout(SEND_SDTR
);
1155 } else if (ti
->offset
== 0) {
1156 printf("%s:%d: async\n", "mha",
1157 acb
->xs
->xs_periph
->periph_target
);
1159 sc
->sc_flags
&= ~SPC_SYNCHNEGO
;
1160 } else if (ti
->period
> 124) {
1161 printf("%s:%d: async\n", "mha",
1162 acb
->xs
->xs_periph
->periph_target
);
1164 mha_sched_msgout(SEND_SDTR
);
1168 p
= mha_stp2cpb(sc
, ti
->period
);
1169 ti
->period
= mha_cpb2stp(sc
, p
);
1173 scsipi_printaddr(acb
->xs
->xs_periph
);
1175 if ((sc
->sc_flags
&SPC_SYNCHNEGO
) == 0) {
1176 /* Target initiated negotiation */
1177 if (ti
->flags
& T_SYNCMODE
) {
1178 ti
->flags
&= ~T_SYNCMODE
;
1180 printf("renegotiated ");
1184 /* Clamp to our maxima */
1185 if (ti
->period
< sc
->sc_minsync
)
1186 ti
->period
= sc
->sc_minsync
;
1187 if (ti
->offset
> 15)
1189 mha_sched_msgout(SEND_SDTR
);
1192 sc
->sc_flags
&= ~SPC_SYNCHNEGO
;
1194 ti
->flags
|= T_SYNCMODE
;
1197 ti
->flags
&= ~T_NEGOTIATE
;
1199 default: /* Extended messages we don't handle */
1200 CMR
= CMD_SET_ATN
; /* XXX? */
1205 SPC_MSGS(("ident "));
1206 /* thanks for that ident... */
1207 if (!MSG_ISIDENTIFY(sc
->sc_imess
[0])) {
1208 SPC_MISC(("unknown "));
1209 printf("%s: unimplemented message: %d\n", device_xname(sc
->sc_dev
), sc
->sc_imess
[0]);
1210 CMR
= CMD_SET_ATN
; /* XXX? */
1214 } else if (sc
->sc_state
== SPC_RESELECTED
) {
1215 struct scsipi_periph
*periph
= NULL
;
1217 struct spc_tinfo
*ti
;
1220 if (MSG_ISIDENTIFY(sc
->sc_imess
[0])) { /* Identify? */
1221 SPC_MISC(("searching "));
1223 * Search wait queue for disconnected cmd
1224 * The list should be short, so I haven't bothered with
1225 * any more sophisticated structures than a simple
1226 * singly linked list.
1228 lunit
= sc
->sc_imess
[0] & 0x07;
1229 TAILQ_FOREACH(acb
, &sc
->nexus_list
, chain
) {
1230 periph
= acb
->xs
->xs_periph
;
1231 if (periph
->periph_lun
== lunit
&&
1232 sc
->sc_selid
== (1<<periph
->periph_target
)) {
1233 TAILQ_REMOVE(&sc
->nexus_list
, acb
,
1235 ACB_SETQ(acb
, ACB_QNONE
);
1240 if (!acb
) { /* Invalid reselection! */
1241 mha_sched_msgout(SEND_ABORT
);
1242 printf("mha: invalid reselect (idbit=0x%2x)\n",
1244 } else { /* Reestablish nexus */
1246 * Setup driver data structures and
1247 * do an implicit RESTORE POINTERS
1249 ti
= &sc
->sc_tinfo
[periph
->periph_target
];
1251 sc
->sc_dp
= acb
->daddr
;
1252 sc
->sc_dleft
= acb
->dleft
;
1253 sc
->sc_tinfo
[periph
->periph_target
].lubusy
1254 |= (1<<periph
->periph_lun
);
1255 if (ti
->flags
& T_SYNCMODE
) {
1256 TMR
= TM_SYNC
; /* XXX */
1260 SPC_MISC(("... found acb"));
1261 sc
->sc_state
= SPC_HASNEXUS
;
1264 printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
1265 device_xname(sc
->sc_dev
), sc
->sc_selid
);
1266 mha_sched_msgout(SEND_DEV_RESET
);
1268 } else { /* Neither SPC_HASNEXUS nor SPC_RESELECTED! */
1269 printf("%s: unexpected message in; will send DEV_RESET\n",
1270 device_xname(sc
->sc_dev
));
1271 mha_sched_msgout(SEND_DEV_RESET
);
1274 /* Ack last message byte */
1276 ESPCMD(sc
, ESPCMD_MSGOK
);
1279 /* Done, reset message pointer. */
1280 sc
->sc_flags
&= ~SPC_DROP_MSGI
;
1285 * Send the highest priority, scheduled message.
1288 mha_msgout(struct mha_softc
*sc
)
1290 #if (SPC_USE_SYNCHRONOUS || SPC_USE_WIDE)
1291 struct spc_tinfo
*ti
;
1295 SPC_TRACE(("mha_msgout "));
1297 if (sc
->sc_prevphase
== MESSAGE_OUT_PHASE
) {
1298 if (sc
->sc_omp
== sc
->sc_omess
) {
1300 * This is a retransmission.
1302 * We get here if the target stayed in MESSAGE OUT
1303 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1304 * that all of the previously transmitted messages must
1305 * be sent again, in the same order. Therefore, we
1306 * requeue all the previously transmitted messages, and
1307 * start again from the top. Our simple priority
1308 * scheme keeps the messages in the right order.
1310 SPC_MISC(("retransmitting "));
1311 sc
->sc_msgpriq
|= sc
->sc_msgoutq
;
1313 * Set ATN. If we're just sending a trivial 1-byte
1314 * message, we'll clear ATN later on anyway.
1316 CMR
= CMD_SET_ATN
; /* XXX? */
1318 /* This is a continuation of the previous message. */
1319 n
= sc
->sc_omp
- sc
->sc_omess
;
1324 /* No messages transmitted so far. */
1329 /* Pick up highest priority message. */
1330 sc
->sc_currmsg
= sc
->sc_msgpriq
& -sc
->sc_msgpriq
;
1331 sc
->sc_msgpriq
&= ~sc
->sc_currmsg
;
1332 sc
->sc_msgoutq
|= sc
->sc_currmsg
;
1334 /* Build the outgoing message data. */
1335 switch (sc
->sc_currmsg
) {
1337 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1339 MSG_IDENTIFY(sc
->sc_nexus
->xs
->xs_periph
->periph_lun
, 1);
1343 #if SPC_USE_SYNCHRONOUS
1345 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1346 ti
= &sc
->sc_tinfo
[sc
->sc_nexus
->xs
->xs_periph
->periph_target
];
1347 sc
->sc_omess
[4] = MSG_EXTENDED
;
1348 sc
->sc_omess
[3] = 3;
1349 sc
->sc_omess
[2] = MSG_EXT_SDTR
;
1350 sc
->sc_omess
[1] = ti
->period
>> 2;
1351 sc
->sc_omess
[0] = ti
->offset
;
1358 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1359 ti
= &sc
->sc_tinfo
[sc
->sc_nexus
->xs
->xs_periph
->periph_target
];
1360 sc
->sc_omess
[3] = MSG_EXTENDED
;
1361 sc
->sc_omess
[2] = 2;
1362 sc
->sc_omess
[1] = MSG_EXT_WDTR
;
1363 sc
->sc_omess
[0] = ti
->width
;
1368 case SEND_DEV_RESET
:
1369 sc
->sc_flags
|= SPC_ABORTING
;
1370 sc
->sc_omess
[0] = MSG_BUS_DEV_RESET
;
1375 sc
->sc_omess
[0] = MSG_MESSAGE_REJECT
;
1379 case SEND_PARITY_ERROR
:
1380 sc
->sc_omess
[0] = MSG_PARITY_ERROR
;
1384 case SEND_INIT_DET_ERR
:
1385 sc
->sc_omess
[0] = MSG_INITIATOR_DET_ERR
;
1390 sc
->sc_flags
|= SPC_ABORTING
;
1391 sc
->sc_omess
[0] = MSG_ABORT
;
1396 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1397 device_xname(sc
->sc_dev
));
1399 sc
->sc_omess
[0] = MSG_NOOP
;
1403 sc
->sc_omp
= &sc
->sc_omess
[n
];
1406 /* Send message bytes. */
1407 /* send TRANSFER command. */
1409 sc
->sc_ps
[4] = n
>> 8;
1411 sc
->sc_ps
[-1] = 0x000F; /* burst */
1412 __asm
volatile ("nop");
1413 CMR
= CMD_SEND_FROM_DMA
; /* send from DMA */
1415 if ((SSR
& SS_BUSY
) != 0)
1417 if (SSR
& SS_IREQUEST
)
1423 if ((PSNS
& PSNS_REQ
) != 0)
1425 /* Wait for REQINIT. XXX Need timeout. */
1428 if (SSR
& SS_IREQUEST
) {
1430 * Target left MESSAGE OUT, possibly to reject
1433 * If this is the last message being sent, then we
1434 * deassert ATN, since either the target is going to
1435 * ignore this message, or it's going to ask for a
1436 * retransmission via MESSAGE PARITY ERROR (in which
1437 * case we reassert ATN anyway).
1440 if (sc
->sc_msgpriq
== 0)
1441 CMR
= CMD_RESET_ATN
;
1447 /* Clear ATN before last byte if this is the last message. */
1448 if (n
== 1 && sc
->sc_msgpriq
== 0)
1449 CMR
= CMD_RESET_ATN
;
1452 while ((SSR
& SS_DREG_FULL
) != 0)
1454 /* Send message byte. */
1455 sc
->sc_pc
[0] = *--sc
->sc_omp
;
1457 /* Keep track of the last message we've sent any bytes of. */
1458 sc
->sc_lastmsg
= sc
->sc_currmsg
;
1464 /* We get here only if the entire message has been transmitted. */
1465 if (sc
->sc_msgpriq
!= 0) {
1466 /* There are more outgoing messages. */
1471 * The last message has been transmitted. We need to remember the last
1472 * message transmitted (in case the target switches to MESSAGE IN phase
1473 * and sends a MESSAGE REJECT), and the list of messages transmitted
1474 * this time around (in case the target stays in MESSAGE OUT phase to
1475 * request a retransmit).
1479 /* Disable REQ/ACK protocol. */
1483 /***************************************************************
1490 mha_datain_pio(struct mha_softc
*sc
, u_char
*p
, int n
)
1496 SPC_TRACE(("[mha_datain_pio(%p,%d)", p
, n
));
1500 sc
->sc_ps
[4] = n
>> 8;
1502 /* \e$BHa$7$-%=%U%HE>Aw\e(B */
1503 CMR
= CMD_RECEIVE_TO_MPU
;
1520 SPC_TRACE(("...%d resd]", n
));
1525 mha_dataout_pio(struct mha_softc
*sc
, u_char
*p
, int n
)
1531 SPC_TRACE(("[mha_dataout_pio(%p,%d)", p
, n
));
1535 sc
->sc_ps
[4] = n
>> 8;
1537 /* \e$BHa$7$-%=%U%HE>Aw\e(B */
1538 CMR
= CMD_SEND_FROM_MPU
;
1555 SPC_TRACE(("...%d resd]", n
));
1564 mha_dataio_dma(int dw
, int cw
, struct mha_softc
*sc
, u_char
*p
, int n
)
1569 panic("transfer size exceeds MAXBSIZE");
1570 if (sc
->sc_dmasize
> 0)
1571 panic("DMA request while another DMA transfer is in pregress");
1573 if (cw
== CMD_SEND_FROM_DMA
) {
1574 memcpy(sc
->sc_dmabuf
, p
, n
);
1575 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_dmamap
, 0, n
, BUS_DMASYNC_PREWRITE
);
1577 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_dmamap
, 0, n
, BUS_DMASYNC_PREREAD
);
1582 paddr
= (char *)sc
->sc_dmaseg
[0].ds_addr
;
1583 #if MHA_DMA_SHORT_BUS_CYCLE == 1
1584 if ((*(volatile int *)&IODEVbase
->io_sram
[0xac]) &
1585 (1 << ((paddr_t
)paddr
>> 19)))
1588 sc
->sc_pc
[0x80 + (((long)paddr
>> 16) & 0xFF)] = 0;
1589 sc
->sc_pc
[0x180 + (((long)paddr
>> 8) & 0xFF)] = 0;
1590 sc
->sc_pc
[0x280 + (((long)paddr
>> 0) & 0xFF)] = 0;
1593 sc
->sc_ps
[4] = n
>> 8;
1595 /* DMA \e$BE>Aw@)8f$O0J2<$NDL$j!#\e(B
1596 3 ... short bus cycle
1600 sc
->sc_ps
[-1] = dw
; /* burst */
1601 __asm
volatile ("nop");
1602 CMR
= cw
; /* receive to DMA */
1607 mha_dataout(struct mha_softc
*sc
, u_char
*p
, int n
)
1613 return mha_dataout_pio(sc
, p
, n
);
1614 return mha_dataio_dma(MHA_DMA_DATAOUT
, CMD_SEND_FROM_DMA
, sc
, p
, n
);
1618 mha_datain(struct mha_softc
*sc
, u_char
*p
, int n
)
1620 struct acb
*acb
= sc
->sc_nexus
;
1624 if (acb
->cmd
.opcode
== SCSI_REQUEST_SENSE
|| (n
& 1))
1625 return mha_datain_pio(sc
, p
, n
);
1626 return mha_dataio_dma(MHA_DMA_DATAIN
, CMD_RECEIVE_TO_DMA
, sc
, p
, n
);
1630 * Catch an interrupt from the adaptor
1633 * This is the workhorse routine of the driver.
1634 * Deficiencies (for now):
1635 * 1) always uses programmed I/O
1640 struct mha_softc
*sc
= arg
;
1649 #if 1 /* XXX called during attach? */
1650 if (tmpsc
!= NULL
) {
1651 SPC_MISC(("[%p %p]\n", mha_cd
.cd_devs
, sc
));
1662 * \e$B3d$j9~$_6X;_$K$9$k\e(B
1664 SCTL
&= ~SCTL_INTR_ENAB
;
1667 SPC_TRACE(("[mhaintr]"));
1670 * \e$BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k\e(B
1673 * First check for abnormal conditions, such as reset.
1677 while (((ints
= SSR
) & SS_IREQUEST
) == 0)
1679 SPC_MISC(("ints = 0x%x ", ints
));
1680 #else /* usually? */
1684 while (SSR
& SS_IREQUEST
) {
1687 SPC_MISC(("[r=0x%x]", r
));
1693 "ph=0x%x(ought to be %d)]\n",
1696 acb
->xs
->cmd
->opcode
,
1698 panic("unexpected result.");
1699 case 0x82: /* selection timeout */
1700 SPC_MISC(("selection timeout "));
1701 sc
->sc_phase
= BUSFREE_PHASE
;
1702 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1705 acb
->xs
->error
= XS_SELTIMEOUT
;
1707 continue; /* XXX ??? msaitoh */
1708 case 0x60: /* command completed */
1709 sc
->sc_spcinitialized
++;
1710 if (sc
->sc_phase
== BUSFREE_PHASE
)
1713 if (ph
& PSNS_ACK
) {
1715 /* \e$B$U$D!<$N%3%^%s%I$,=*N;$7$?$i$7$$\e(B */
1716 SPC_MISC(("0x60)phase = %x(ought to be %x)\n",
1717 ph
& PHASE_MASK
, sc
->sc_phase
));
1719 /* switch (sc->sc_phase) {*/
1721 switch (ph
& PHASE_MASK
) {
1724 if (sc
->sc_state
!= SPC_HASNEXUS
)
1725 printf("stsin: !SPC_HASNEXUS->(%d)\n",
1727 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1732 acb
->stat
= sc
->sc_pcx
[0]; /* XXX */
1733 SPC_MISC(("stat=0x%02x ", acb
->stat
));
1734 sc
->sc_prevphase
= STATUS_PHASE
;
1736 case MESSAGE_IN_PHASE
:
1738 sc
->sc_prevphase
= MESSAGE_IN_PHASE
;
1741 if (sc
->sc_dmasize
== 0)
1743 bus_dmamap_sync(sc
->sc_dmat
,
1746 BUS_DMASYNC_POSTREAD
);
1747 memcpy(sc
->sc_p
, sc
->sc_dmabuf
,
1751 case DATA_OUT_PHASE
:
1752 if (sc
->sc_dmasize
== 0)
1754 bus_dmamap_sync(sc
->sc_dmat
,
1757 BUS_DMASYNC_POSTWRITE
);
1762 CMR
= CMD_RESET_ACK
; /* reset ack */
1763 /*mha_done(sc, acb); XXX */
1765 } else if (NSR
& 0x80) { /* nexus */
1767 if (sc
->sc_state
== SPC_SELECTING
) /* XXX msaitoh */
1768 sc
->sc_state
= SPC_HASNEXUS
;
1769 /* \e$B%U%'!<%:$N7h$aBG$A$r$9$k\e(B
1770 \e$B30$l$?$i!"\e(Binitial-phase error(0x54) \e$B$,\e(B
1771 \e$BJV$C$F$/$k$s$GCm0U$7$?$^$(!#\e(B
1772 \e$B$G$b$J$<$+\e(B 0x65 \e$B$,JV$C$F$-$?$j$7$F$M!<$+\e(B? */
1774 if (SSR
& SS_IREQUEST
)
1776 switch (sc
->sc_phase
) {
1778 panic("\e$B8+CN$i$L\e(B phase \e$B$,Mh$A$^$C$?$@$h\e(B");
1779 case MESSAGE_IN_PHASE
:
1780 /* \e$B2?$b$7$J$$\e(B */
1783 sc
->sc_phase
= MESSAGE_IN_PHASE
;
1784 CMR
= CMD_RECEIVE_MSG
; /* receive msg */
1787 sc
->sc_prevphase
= DATA_IN_PHASE
;
1788 if (sc
->sc_dleft
== 0) {
1789 /* \e$BE>Aw%G!<%?$O$b$&$J$$$N$G\e(B
1790 \e$B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&\e(B */
1791 sc
->sc_phase
= STATUS_PHASE
;
1792 CMR
= CMD_RECEIVE_STS
; /* receive sts */
1795 n
= mha_datain(sc
, sc
->sc_dp
,
1800 case DATA_OUT_PHASE
:
1801 sc
->sc_prevphase
= DATA_OUT_PHASE
;
1802 if (sc
->sc_dleft
== 0) {
1803 /* \e$BE>Aw%G!<%?$O$b$&$J$$$N$G\e(B
1804 \e$B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&\e(B */
1805 sc
->sc_phase
= STATUS_PHASE
;
1806 CMR
= CMD_RECEIVE_STS
; /* receive sts */
1809 /* data phase \e$B$NB3$-$r$d$m$&\e(B */
1810 n
= mha_dataout(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1815 /* \e$B:G=i$O\e(B CMD PHASE \e$B$H$$$&$3$H$i$7$$\e(B */
1817 /* \e$B%G!<%?E>Aw$,$"$j$&$k>l9g\e(B */
1818 if (acb
->xs
->xs_control
& XS_CTL_DATA_IN
) {
1819 sc
->sc_phase
= DATA_IN_PHASE
;
1820 n
= mha_datain(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1824 else if (acb
->xs
->xs_control
& XS_CTL_DATA_OUT
) {
1825 sc
->sc_phase
= DATA_OUT_PHASE
;
1826 n
= mha_dataout(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1833 /* \e$B%G!<%?E>Aw$O$J$$$i$7$$\e(B?! */
1835 sc
->sc_phase
= STATUS_PHASE
;
1836 CMR
= CMD_RECEIVE_STS
; /* receive sts */
1843 case 0x31: /* disconnected in xfer progress. */
1844 SPC_MISC(("[0x31]"));
1845 case 0x70: /* disconnected. */
1846 SPC_ASSERT(sc
->sc_flags
& SPC_BUSFREE_OK
);
1847 sc
->sc_phase
= BUSFREE_PHASE
;
1848 sc
->sc_state
= SPC_IDLE
;
1851 SPC_ASSERT(sc
->sc_nexus
!= NULL
);
1852 acb
->xs
->error
= XS_NOERROR
;
1855 TAILQ_INSERT_HEAD(&sc
->nexus_list
, acb
, chain
);
1859 case 0x32: /* phase error in xfer progress. */
1860 SPC_MISC(("[0x32]"));
1862 case 0x65: /* invalid command.
1863 \e$B$J$<$3$s$J$b$N$,=P$k$N$+\e(B
1864 \e$B26$K$OA4$/M}2r$G$-$J$$\e(B */
1866 SPC_MISC(("[0x%04x]", r
));
1869 case 0x54: /* initial-phase error. */
1870 SPC_MISC(("[0x54, ns=%x, ph=%x(ought to be %x)]",
1872 SCR
, sc
->sc_phase
));
1874 case 0x71: /* assert req */
1877 printf("SPC sts=%2x, r=%04x, ns=%x, ph=%x\n",
1882 if (sc
->sc_state
== SPC_SELECTING
) { /* XXX msaitoh */
1883 sc
->sc_state
= SPC_HASNEXUS
;
1886 switch (ph
& PHASE_MASK
) {
1888 printf("phase = %x\n", ph
);
1889 panic("assert req: the phase I don't know!");
1891 sc
->sc_prevphase
= DATA_IN_PHASE
;
1892 SPC_MISC(("DATAIN(%d)...", sc
->sc_dleft
));
1893 n
= mha_datain(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1896 SPC_MISC(("done\n"));
1898 case DATA_OUT_PHASE
:
1899 sc
->sc_prevphase
= DATA_OUT_PHASE
;
1900 SPC_MISC(("DATAOUT\n"));
1901 n
= mha_dataout(sc
, sc
->sc_dp
, sc
->sc_dleft
);
1906 sc
->sc_phase
= STATUS_PHASE
;
1907 SPC_MISC(("[RECV_STS]"));
1909 CMR
= CMD_RECEIVE_STS
; /* receive sts */
1911 case MESSAGE_IN_PHASE
:
1912 sc
->sc_phase
= MESSAGE_IN_PHASE
;
1914 CMR
= CMD_RECEIVE_MSG
;
1926 mha_abort(struct mha_softc
*sc
, struct acb
*acb
)
1928 acb
->flags
|= ACB_ABORTED
;
1930 if (acb
== sc
->sc_nexus
) {
1932 * If we're still selecting, the message will be scheduled
1933 * after selection is complete.
1935 if (sc
->sc_state
== SPC_HASNEXUS
) {
1936 sc
->sc_flags
|= SPC_ABORTING
;
1937 mha_sched_msgout(SEND_ABORT
);
1940 if (sc
->sc_state
== SPC_IDLE
)
1946 mha_timeout(void *arg
)
1948 struct acb
*acb
= (struct acb
*)arg
;
1949 struct scsipi_xfer
*xs
= acb
->xs
;
1950 struct scsipi_periph
*periph
= xs
->xs_periph
;
1951 struct mha_softc
*sc
=
1952 (void *)periph
->periph_channel
->chan_adapter
->adapt_dev
;
1957 scsipi_printaddr(periph
);
1958 printf("%s: timed out [acb %p (flags 0x%x, dleft %x, stat %x)], "
1959 "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) >",
1960 device_xname(sc
->sc_dev
),
1961 acb
, acb
->flags
, acb
->dleft
, acb
->stat
,
1962 sc
->sc_state
, sc
->sc_nexus
, sc
->sc_phase
, sc
->sc_prevphase
,
1963 sc
->sc_dleft
, sc
->sc_msgpriq
, sc
->sc_msgout
1965 printf("[%04x %02x]\n", sc
->sc_ps
[1], SCR
);
1966 panic("timeout, ouch!");
1968 if (acb
->flags
& ACB_ABORTED
) {
1969 /* abort timed out */
1972 mha_init(sc
, 1); /* XXX 1?*/
1975 /* abort the operation that has timed out */
1977 xs
->error
= XS_TIMEOUT
;
1986 * The following functions are mostly used for debugging purposes, either
1987 * directly called from the driver or from the kernel debugger.
1991 mha_show_scsi_cmd(struct acb
*acb
)
1993 u_char
*b
= (u_char
*)&acb
->cmd
;
1994 struct scsipi_periph
*periph
= acb
->xs
->xs_periph
;
1997 scsipi_printaddr(periph
);
1998 if ((acb
->xs
->xs_control
& XS_CTL_RESET
) == 0) {
1999 for (i
= 0; i
< acb
->clen
; i
++) {
2010 mha_print_acb(struct acb
*acb
)
2013 printf("acb@%p xs=%p flags=%x", acb
, acb
->xs
, acb
->flags
);
2014 printf(" dp=%p dleft=%d stat=%x\n",
2015 acb
->daddr
, acb
->dleft
, acb
->stat
);
2016 mha_show_scsi_cmd(acb
);
2020 mha_print_active_acb(void)
2023 struct mha_softc
*sc
= device_lookup_private(&mha_cd
, 0); /* XXX */
2025 printf("ready list:\n");
2026 TAILQ_FOREACH(acb
, &sc
->ready_list
, chain
)
2029 if (sc
->sc_nexus
!= NULL
)
2030 mha_print_acb(sc
->sc_nexus
);
2031 printf("nexus list:\n");
2032 TAILQ_FOREACH(acb
, &sc
->nexus_list
, chain
)
2037 mha_dump_driver(struct mha_softc
*sc
)
2039 struct spc_tinfo
*ti
;
2042 printf("nexus=%p prevphase=%x\n", sc
->sc_nexus
, sc
->sc_prevphase
);
2043 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2044 sc
->sc_state
, sc
->sc_imess
[0],
2045 sc
->sc_msgpriq
, sc
->sc_msgoutq
, sc
->sc_lastmsg
, sc
->sc_currmsg
);
2046 for (i
= 0; i
< 7; i
++) {
2047 ti
= &sc
->sc_tinfo
[i
];
2048 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2049 i
, ti
->cmds
, ti
->dconns
, ti
->touts
);
2050 printf(" %d senses flags=%x\n", ti
->senses
, ti
->flags
);