wd33c93.c revision 1.28 1 1.28 christos /* $NetBSD: wd33c93.c,v 1.28 2019/02/10 17:13:33 christos Exp $ */
2 1.1 bjh21
3 1.1 bjh21 /*
4 1.1 bjh21 * Copyright (c) 1990 The Regents of the University of California.
5 1.1 bjh21 * All rights reserved.
6 1.1 bjh21 *
7 1.1 bjh21 * This code is derived from software contributed to Berkeley by
8 1.1 bjh21 * Van Jacobson of Lawrence Berkeley Laboratory.
9 1.1 bjh21 *
10 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
11 1.1 bjh21 * modification, are permitted provided that the following conditions
12 1.1 bjh21 * are met:
13 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
14 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
15 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
17 1.1 bjh21 * documentation and/or other materials provided with the distribution.
18 1.1 bjh21 * 3. Neither the name of the University nor the names of its contributors
19 1.1 bjh21 * may be used to endorse or promote products derived from this software
20 1.1 bjh21 * without specific prior written permission.
21 1.1 bjh21 *
22 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 bjh21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 bjh21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 bjh21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 bjh21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 bjh21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 bjh21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 bjh21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 bjh21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 bjh21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 bjh21 * SUCH DAMAGE.
33 1.1 bjh21 *
34 1.1 bjh21 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
35 1.1 bjh21 */
36 1.1 bjh21
37 1.1 bjh21 /*
38 1.1 bjh21 * Changes Copyright (c) 2001 Wayne Knowles
39 1.1 bjh21 * Changes Copyright (c) 1996 Steve Woodford
40 1.1 bjh21 * Original Copyright (c) 1994 Christian E. Hopps
41 1.1 bjh21 *
42 1.1 bjh21 * This code is derived from software contributed to Berkeley by
43 1.1 bjh21 * Van Jacobson of Lawrence Berkeley Laboratory.
44 1.1 bjh21 *
45 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
46 1.1 bjh21 * modification, are permitted provided that the following conditions
47 1.1 bjh21 * are met:
48 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
49 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
50 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
51 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
52 1.1 bjh21 * documentation and/or other materials provided with the distribution.
53 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software
54 1.1 bjh21 * must display the following acknowledgement:
55 1.1 bjh21 * This product includes software developed by the University of
56 1.1 bjh21 * California, Berkeley and its contributors.
57 1.1 bjh21 * 4. Neither the name of the University nor the names of its contributors
58 1.1 bjh21 * may be used to endorse or promote products derived from this software
59 1.1 bjh21 * without specific prior written permission.
60 1.1 bjh21 *
61 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 1.1 bjh21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 1.1 bjh21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 1.1 bjh21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 1.1 bjh21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 1.1 bjh21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 1.1 bjh21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 1.1 bjh21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 1.1 bjh21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.1 bjh21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.1 bjh21 * SUCH DAMAGE.
72 1.1 bjh21 *
73 1.1 bjh21 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
74 1.1 bjh21 */
75 1.1 bjh21
76 1.1 bjh21 /*
77 1.1 bjh21 * This version of the driver is pretty well generic, so should work with
78 1.1 bjh21 * any flavour of WD33C93 chip.
79 1.1 bjh21 */
80 1.1 bjh21
81 1.1 bjh21 #include <sys/cdefs.h>
82 1.28 christos __KERNEL_RCSID(0, "$NetBSD: wd33c93.c,v 1.28 2019/02/10 17:13:33 christos Exp $");
83 1.1 bjh21
84 1.1 bjh21 #include "opt_ddb.h"
85 1.1 bjh21
86 1.1 bjh21 #include <sys/param.h>
87 1.1 bjh21 #include <sys/systm.h>
88 1.1 bjh21 #include <sys/device.h>
89 1.1 bjh21 #include <sys/kernel.h> /* For hz */
90 1.1 bjh21 #include <sys/disklabel.h>
91 1.1 bjh21 #include <sys/buf.h>
92 1.1 bjh21
93 1.1 bjh21 #include <dev/scsipi/scsi_all.h>
94 1.1 bjh21 #include <dev/scsipi/scsipi_all.h>
95 1.1 bjh21 #include <dev/scsipi/scsiconf.h>
96 1.1 bjh21 #include <dev/scsipi/scsi_message.h>
97 1.1 bjh21
98 1.18 ad #include <sys/bus.h>
99 1.1 bjh21
100 1.1 bjh21 #include <dev/ic/wd33c93reg.h>
101 1.1 bjh21 #include <dev/ic/wd33c93var.h>
102 1.1 bjh21
103 1.1 bjh21 /*
104 1.1 bjh21 * SCSI delays
105 1.1 bjh21 * In u-seconds, primarily for state changes on the SPC.
106 1.1 bjh21 */
107 1.1 bjh21 #define SBIC_CMD_WAIT 50000 /* wait per step of 'immediate' cmds */
108 1.1 bjh21 #define SBIC_DATA_WAIT 50000 /* wait per data in/out step */
109 1.1 bjh21 #define SBIC_INIT_WAIT 50000 /* wait per step (both) during init */
110 1.1 bjh21
111 1.1 bjh21 #define STATUS_UNKNOWN 0xff /* uninitialized status */
112 1.1 bjh21
113 1.1 bjh21 /*
114 1.1 bjh21 * Convenience macro for waiting for a particular wd33c93 event
115 1.1 bjh21 */
116 1.1 bjh21 #define SBIC_WAIT(regs, until, timeo) wd33c93_wait(regs, until, timeo, __LINE__)
117 1.1 bjh21
118 1.1 bjh21 void wd33c93_init (struct wd33c93_softc *);
119 1.1 bjh21 void wd33c93_reset (struct wd33c93_softc *);
120 1.1 bjh21 int wd33c93_go (struct wd33c93_softc *, struct wd33c93_acb *);
121 1.1 bjh21 int wd33c93_dmaok (struct wd33c93_softc *, struct scsipi_xfer *);
122 1.1 bjh21 int wd33c93_wait (struct wd33c93_softc *, u_char, int , int);
123 1.1 bjh21 u_char wd33c93_selectbus (struct wd33c93_softc *, struct wd33c93_acb *);
124 1.1 bjh21 int wd33c93_xfout (struct wd33c93_softc *, int, void *);
125 1.1 bjh21 int wd33c93_xfin (struct wd33c93_softc *, int, void *);
126 1.1 bjh21 int wd33c93_poll (struct wd33c93_softc *, struct wd33c93_acb *);
127 1.1 bjh21 int wd33c93_nextstate (struct wd33c93_softc *, struct wd33c93_acb *,
128 1.1 bjh21 u_char, u_char);
129 1.1 bjh21 int wd33c93_abort (struct wd33c93_softc *, struct wd33c93_acb *,
130 1.1 bjh21 const char *);
131 1.1 bjh21 void wd33c93_xferdone (struct wd33c93_softc *);
132 1.1 bjh21 void wd33c93_error (struct wd33c93_softc *, struct wd33c93_acb *);
133 1.1 bjh21 void wd33c93_scsidone (struct wd33c93_softc *, struct wd33c93_acb *, int);
134 1.1 bjh21 void wd33c93_sched (struct wd33c93_softc *);
135 1.1 bjh21 void wd33c93_dequeue (struct wd33c93_softc *, struct wd33c93_acb *);
136 1.1 bjh21 void wd33c93_dma_stop (struct wd33c93_softc *);
137 1.1 bjh21 void wd33c93_dma_setup (struct wd33c93_softc *, int);
138 1.1 bjh21 int wd33c93_msgin_phase (struct wd33c93_softc *, int);
139 1.1 bjh21 void wd33c93_msgin (struct wd33c93_softc *, u_char *, int);
140 1.1 bjh21 void wd33c93_reselect (struct wd33c93_softc *, int, int, int, int);
141 1.1 bjh21 void wd33c93_sched_msgout (struct wd33c93_softc *, u_short);
142 1.1 bjh21 void wd33c93_msgout (struct wd33c93_softc *);
143 1.1 bjh21 void wd33c93_timeout (void *arg);
144 1.1 bjh21 void wd33c93_watchdog (void *arg);
145 1.12 rumble u_char wd33c93_stp2syn (struct wd33c93_softc *, struct wd33c93_tinfo *);
146 1.1 bjh21 void wd33c93_setsync (struct wd33c93_softc *, struct wd33c93_tinfo *);
147 1.1 bjh21 void wd33c93_update_xfer_mode (struct wd33c93_softc *, int);
148 1.1 bjh21
149 1.1 bjh21 static struct pool wd33c93_pool; /* Adapter Control Blocks */
150 1.1 bjh21 static int wd33c93_pool_initialized = 0;
151 1.1 bjh21
152 1.1 bjh21 /*
153 1.1 bjh21 * Timeouts
154 1.1 bjh21 */
155 1.1 bjh21 int wd33c93_cmd_wait = SBIC_CMD_WAIT;
156 1.1 bjh21 int wd33c93_data_wait = SBIC_DATA_WAIT;
157 1.1 bjh21 int wd33c93_init_wait = SBIC_INIT_WAIT;
158 1.1 bjh21
159 1.1 bjh21 int wd33c93_nodma = 0; /* Use polled IO transfers */
160 1.1 bjh21 int wd33c93_nodisc = 0; /* Allow command queues */
161 1.1 bjh21 int wd33c93_notags = 0; /* No Tags */
162 1.1 bjh21
163 1.1 bjh21 /*
164 1.1 bjh21 * Some useful stuff for debugging purposes
165 1.1 bjh21 */
166 1.1 bjh21 #ifdef DEBUG
167 1.1 bjh21
168 1.1 bjh21 #define QPRINTF(a) SBIC_DEBUG(MISC, a)
169 1.1 bjh21
170 1.1 bjh21 int wd33c93_debug = 0; /* Debug flags */
171 1.1 bjh21
172 1.1 bjh21 void wd33c93_print_csr (u_char);
173 1.1 bjh21 void wd33c93_hexdump (u_char *, int);
174 1.1 bjh21
175 1.1 bjh21 #else
176 1.1 bjh21 #define QPRINTF(a) /* */
177 1.1 bjh21 #endif
178 1.1 bjh21
179 1.1 bjh21 static const char *wd33c93_chip_names[] = SBIC_CHIP_LIST;
180 1.1 bjh21
181 1.1 bjh21 /*
182 1.1 bjh21 * Attach instance of driver and probe for sub devices
183 1.1 bjh21 */
184 1.1 bjh21 void
185 1.22 tsutsui wd33c93_attach(struct wd33c93_softc *sc)
186 1.1 bjh21 {
187 1.22 tsutsui struct scsipi_adapter *adapt = &sc->sc_adapter;
188 1.22 tsutsui struct scsipi_channel *chan = &sc->sc_channel;
189 1.1 bjh21
190 1.22 tsutsui adapt->adapt_dev = sc->sc_dev;
191 1.1 bjh21 adapt->adapt_nchannels = 1;
192 1.1 bjh21 adapt->adapt_openings = 256;
193 1.1 bjh21 adapt->adapt_max_periph = 256; /* Max tags per device */
194 1.1 bjh21 adapt->adapt_ioctl = NULL;
195 1.1 bjh21 /* adapt_request initialized by MD interface */
196 1.1 bjh21 /* adapt_minphys initialized by MD interface */
197 1.1 bjh21
198 1.1 bjh21 memset(chan, 0, sizeof(*chan));
199 1.22 tsutsui chan->chan_adapter = &sc->sc_adapter;
200 1.1 bjh21 chan->chan_bustype = &scsi_bustype;
201 1.1 bjh21 chan->chan_channel = 0;
202 1.1 bjh21 chan->chan_ntargets = SBIC_NTARG;
203 1.1 bjh21 chan->chan_nluns = SBIC_NLUN;
204 1.22 tsutsui chan->chan_id = sc->sc_id;
205 1.1 bjh21
206 1.22 tsutsui callout_init(&sc->sc_watchdog, 0);
207 1.1 bjh21
208 1.1 bjh21 /*
209 1.1 bjh21 * Add reference to adapter so that we drop the reference after
210 1.26 skrll * config_found() to make sure the adapter is disabled.
211 1.1 bjh21 */
212 1.22 tsutsui if (scsipi_adapter_addref(&sc->sc_adapter) != 0) {
213 1.22 tsutsui aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
214 1.1 bjh21 return;
215 1.1 bjh21 }
216 1.1 bjh21
217 1.22 tsutsui sc->sc_cfflags = device_cfdata(sc->sc_dev)->cf_flags;
218 1.22 tsutsui wd33c93_init(sc);
219 1.12 rumble
220 1.20 bjh21 aprint_normal(": %s (%d.%d MHz clock, %s, SCSI ID %d)\n",
221 1.22 tsutsui wd33c93_chip_names[sc->sc_chip],
222 1.22 tsutsui sc->sc_clkfreq / 10, sc->sc_clkfreq % 10,
223 1.22 tsutsui (sc->sc_dmamode == SBIC_CTL_DMA) ? "DMA" :
224 1.22 tsutsui (sc->sc_dmamode == SBIC_CTL_DBA_DMA) ? "DBA" :
225 1.22 tsutsui (sc->sc_dmamode == SBIC_CTL_BURST_DMA) ? "BURST DMA" : "PIO",
226 1.22 tsutsui sc->sc_channel.chan_id);
227 1.22 tsutsui if (sc->sc_chip == SBIC_CHIP_WD33C93B) {
228 1.22 tsutsui aprint_normal_dev(sc->sc_dev, "microcode revision 0x%02x",
229 1.22 tsutsui sc->sc_rev);
230 1.22 tsutsui if (sc->sc_minsyncperiod < 50)
231 1.20 bjh21 aprint_normal(", Fast SCSI");
232 1.20 bjh21 aprint_normal("\n");
233 1.12 rumble }
234 1.8 bjh21
235 1.22 tsutsui sc->sc_child = config_found(sc->sc_dev, &sc->sc_channel,
236 1.1 bjh21 scsiprint);
237 1.22 tsutsui scsipi_adapter_delref(&sc->sc_adapter);
238 1.1 bjh21 }
239 1.1 bjh21
240 1.1 bjh21 /*
241 1.1 bjh21 * Initialize driver-private structures
242 1.1 bjh21 */
243 1.1 bjh21 void
244 1.22 tsutsui wd33c93_init(struct wd33c93_softc *sc)
245 1.1 bjh21 {
246 1.1 bjh21 u_int i;
247 1.1 bjh21
248 1.1 bjh21 if (!wd33c93_pool_initialized) {
249 1.1 bjh21 /* All instances share the same pool */
250 1.1 bjh21 pool_init(&wd33c93_pool, sizeof(struct wd33c93_acb), 0, 0, 0,
251 1.11 ad "wd33c93_acb", NULL, IPL_BIO);
252 1.1 bjh21 ++wd33c93_pool_initialized;
253 1.1 bjh21 }
254 1.1 bjh21
255 1.22 tsutsui if (sc->sc_state == 0) {
256 1.22 tsutsui TAILQ_INIT(&sc->ready_list);
257 1.1 bjh21
258 1.22 tsutsui sc->sc_nexus = NULL;
259 1.22 tsutsui sc->sc_disc = 0;
260 1.22 tsutsui memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
261 1.1 bjh21
262 1.22 tsutsui callout_reset(&sc->sc_watchdog, 60 * hz, wd33c93_watchdog, sc);
263 1.1 bjh21 } else
264 1.1 bjh21 panic("wd33c93: reinitializing driver!");
265 1.1 bjh21
266 1.22 tsutsui sc->sc_flags = 0;
267 1.22 tsutsui sc->sc_state = SBIC_IDLE;
268 1.22 tsutsui wd33c93_reset(sc);
269 1.1 bjh21
270 1.1 bjh21 for (i = 0; i < 8; i++) {
271 1.22 tsutsui struct wd33c93_tinfo *ti = &sc->sc_tinfo[i];
272 1.1 bjh21 /*
273 1.16 rumble * cf_flags = 0xTTSSRR
274 1.1 bjh21 *
275 1.1 bjh21 * TT = Bitmask to disable Tagged Queues
276 1.16 rumble * SS = Bitmask to disable Sync negotiation
277 1.1 bjh21 * RR = Bitmask to disable disconnect/reselect
278 1.1 bjh21 */
279 1.1 bjh21 ti->flags = T_NEED_RESET;
280 1.22 tsutsui if (CFFLAGS_NOSYNC(sc->sc_cfflags, i))
281 1.1 bjh21 ti->flags |= T_NOSYNC;
282 1.22 tsutsui if (CFFLAGS_NODISC(sc->sc_cfflags, i) || wd33c93_nodisc)
283 1.1 bjh21 ti->flags |= T_NODISC;
284 1.22 tsutsui ti->period = sc->sc_minsyncperiod;
285 1.1 bjh21 ti->offset = 0;
286 1.1 bjh21 }
287 1.1 bjh21 }
288 1.1 bjh21
289 1.1 bjh21 void
290 1.22 tsutsui wd33c93_reset(struct wd33c93_softc *sc)
291 1.1 bjh21 {
292 1.8 bjh21 u_int my_id, s, div, i;
293 1.1 bjh21 u_char csr, reg;
294 1.1 bjh21
295 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_ABORT);
296 1.22 tsutsui WAIT_CIP(sc);
297 1.1 bjh21
298 1.1 bjh21 s = splbio();
299 1.1 bjh21
300 1.22 tsutsui if (sc->sc_reset != NULL)
301 1.22 tsutsui (*sc->sc_reset)(sc);
302 1.1 bjh21
303 1.22 tsutsui my_id = sc->sc_channel.chan_id & SBIC_ID_MASK;
304 1.1 bjh21
305 1.12 rumble /* Enable advanced features and really(!) advanced features */
306 1.1 bjh21 #if 1
307 1.12 rumble my_id |= (SBIC_ID_EAF | SBIC_ID_RAF); /* XXX - MD Layer */
308 1.1 bjh21 #endif
309 1.1 bjh21
310 1.22 tsutsui SET_SBIC_myid(sc, my_id);
311 1.1 bjh21
312 1.1 bjh21 /* Reset the chip */
313 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_RESET);
314 1.1 bjh21 DELAY(25);
315 1.22 tsutsui SBIC_WAIT(sc, SBIC_ASR_INT, 0);
316 1.1 bjh21
317 1.1 bjh21 /* Set up various chip parameters */
318 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
319 1.1 bjh21
320 1.22 tsutsui GET_SBIC_csr(sc, csr); /* clears interrupt also */
321 1.22 tsutsui GET_SBIC_cdb1(sc, sc->sc_rev); /* valid with RAF on wd33c93b */
322 1.1 bjh21
323 1.1 bjh21 switch (csr) {
324 1.5 bjh21 case SBIC_CSR_RESET:
325 1.22 tsutsui sc->sc_chip = SBIC_CHIP_WD33C93;
326 1.1 bjh21 break;
327 1.5 bjh21 case SBIC_CSR_RESET_AM:
328 1.22 tsutsui SET_SBIC_queue_tag(sc, 0x55);
329 1.22 tsutsui GET_SBIC_queue_tag(sc, reg);
330 1.22 tsutsui sc->sc_chip = (reg == 0x55) ?
331 1.1 bjh21 SBIC_CHIP_WD33C93B : SBIC_CHIP_WD33C93A;
332 1.22 tsutsui SET_SBIC_queue_tag(sc, 0x0);
333 1.1 bjh21 break;
334 1.1 bjh21 default:
335 1.22 tsutsui sc->sc_chip = SBIC_CHIP_UNKNOWN;
336 1.1 bjh21 }
337 1.1 bjh21
338 1.1 bjh21 /*
339 1.12 rumble * Choose a suitable clock divisor and work out the resulting
340 1.12 rumble * sync transfer periods in 4ns units.
341 1.12 rumble */
342 1.22 tsutsui if (sc->sc_clkfreq < 110) {
343 1.12 rumble my_id |= SBIC_ID_FS_8_10;
344 1.12 rumble div = 2;
345 1.22 tsutsui } else if (sc->sc_clkfreq < 160) {
346 1.12 rumble my_id |= SBIC_ID_FS_12_15;
347 1.12 rumble div = 3;
348 1.22 tsutsui } else if (sc->sc_clkfreq < 210) {
349 1.12 rumble my_id |= SBIC_ID_FS_16_20;
350 1.12 rumble div = 4;
351 1.12 rumble } else
352 1.22 tsutsui panic("wd33c93: invalid clock speed %d", sc->sc_clkfreq);
353 1.12 rumble
354 1.12 rumble for (i = 0; i < 7; i++)
355 1.22 tsutsui sc->sc_syncperiods[i] =
356 1.22 tsutsui (i + 2) * div * 1250 / sc->sc_clkfreq;
357 1.22 tsutsui sc->sc_minsyncperiod = sc->sc_syncperiods[0];
358 1.12 rumble SBIC_DEBUG(SYNC, ("available sync periods: %d %d %d %d %d %d %d\n",
359 1.22 tsutsui sc->sc_syncperiods[0], sc->sc_syncperiods[1],
360 1.22 tsutsui sc->sc_syncperiods[2], sc->sc_syncperiods[3],
361 1.22 tsutsui sc->sc_syncperiods[4], sc->sc_syncperiods[5],
362 1.22 tsutsui sc->sc_syncperiods[6]));
363 1.12 rumble
364 1.22 tsutsui if (sc->sc_clkfreq >= 160 && sc->sc_chip == SBIC_CHIP_WD33C93B) {
365 1.12 rumble for (i = 0; i < 3; i++)
366 1.22 tsutsui sc->sc_fsyncperiods[i] =
367 1.22 tsutsui (i + 2) * 2 * 1250 / sc->sc_clkfreq;
368 1.12 rumble SBIC_DEBUG(SYNC, ("available fast sync periods: %d %d %d\n",
369 1.22 tsutsui sc->sc_fsyncperiods[0], sc->sc_fsyncperiods[1],
370 1.22 tsutsui sc->sc_fsyncperiods[2]));
371 1.22 tsutsui sc->sc_minsyncperiod = sc->sc_fsyncperiods[0];
372 1.12 rumble }
373 1.12 rumble
374 1.13 rumble /* Max Sync Offset */
375 1.22 tsutsui if (sc->sc_chip == SBIC_CHIP_WD33C93A ||
376 1.22 tsutsui sc->sc_chip == SBIC_CHIP_WD33C93B)
377 1.22 tsutsui sc->sc_maxoffset = SBIC_SYN_93AB_MAX_OFFSET;
378 1.13 rumble else
379 1.22 tsutsui sc->sc_maxoffset = SBIC_SYN_93_MAX_OFFSET;
380 1.13 rumble
381 1.12 rumble /*
382 1.1 bjh21 * don't allow Selection (SBIC_RID_ES)
383 1.1 bjh21 * until we can handle target mode!!
384 1.1 bjh21 */
385 1.22 tsutsui SET_SBIC_rselid(sc, SBIC_RID_ER);
386 1.1 bjh21
387 1.1 bjh21 /* Asynchronous for now */
388 1.22 tsutsui SET_SBIC_syn(sc, 0);
389 1.1 bjh21
390 1.22 tsutsui sc->sc_flags = 0;
391 1.22 tsutsui sc->sc_state = SBIC_IDLE;
392 1.1 bjh21
393 1.1 bjh21 splx(s);
394 1.1 bjh21 }
395 1.1 bjh21
396 1.1 bjh21 void
397 1.22 tsutsui wd33c93_error(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
398 1.1 bjh21 {
399 1.1 bjh21 struct scsipi_xfer *xs = acb->xs;
400 1.1 bjh21
401 1.1 bjh21 KASSERT(xs);
402 1.1 bjh21
403 1.1 bjh21 if (xs->xs_control & XS_CTL_SILENT)
404 1.1 bjh21 return;
405 1.1 bjh21
406 1.1 bjh21 scsipi_printaddr(xs->xs_periph);
407 1.1 bjh21 printf("SCSI Error\n");
408 1.1 bjh21 }
409 1.1 bjh21
410 1.1 bjh21 /*
411 1.12 rumble * Determine an appropriate value for the synchronous transfer register
412 1.12 rumble * given the period and offset values in *ti.
413 1.12 rumble */
414 1.12 rumble u_char
415 1.22 tsutsui wd33c93_stp2syn(struct wd33c93_softc *sc, struct wd33c93_tinfo *ti)
416 1.12 rumble {
417 1.12 rumble unsigned i;
418 1.12 rumble
419 1.12 rumble /* see if we can handle fast scsi (100-200ns) first */
420 1.22 tsutsui if (ti->period < 50 && sc->sc_minsyncperiod < 50) {
421 1.12 rumble for (i = 0; i < 3; i++)
422 1.22 tsutsui if (sc->sc_fsyncperiods[i] >= ti->period)
423 1.12 rumble return (SBIC_SYN(ti->offset, i + 2, 1));
424 1.12 rumble }
425 1.12 rumble
426 1.12 rumble for (i = 0; i < 7; i++) {
427 1.22 tsutsui if (sc->sc_syncperiods[i] >= ti->period) {
428 1.12 rumble if (i == 6)
429 1.12 rumble return (SBIC_SYN(0, 0, 0));
430 1.12 rumble else
431 1.12 rumble return (SBIC_SYN(ti->offset, i + 2, 0));
432 1.12 rumble }
433 1.12 rumble }
434 1.12 rumble
435 1.12 rumble /* XXX - can't handle it; do async */
436 1.12 rumble return (SBIC_SYN(0, 0, 0));
437 1.12 rumble }
438 1.12 rumble
439 1.12 rumble /*
440 1.1 bjh21 * Setup sync mode for given target
441 1.1 bjh21 */
442 1.1 bjh21 void
443 1.22 tsutsui wd33c93_setsync(struct wd33c93_softc *sc, struct wd33c93_tinfo *ti)
444 1.1 bjh21 {
445 1.12 rumble u_char syncreg;
446 1.1 bjh21
447 1.12 rumble if (ti->flags & T_SYNCMODE)
448 1.22 tsutsui syncreg = wd33c93_stp2syn(sc, ti);
449 1.12 rumble else
450 1.12 rumble syncreg = SBIC_SYN(0, 0, 0);
451 1.1 bjh21
452 1.12 rumble SBIC_DEBUG(SYNC, ("wd33c93_setsync: sync reg = 0x%02x\n", syncreg));
453 1.22 tsutsui SET_SBIC_syn(sc, syncreg);
454 1.1 bjh21 }
455 1.1 bjh21
456 1.1 bjh21 /*
457 1.1 bjh21 * Check if current operation can be done using DMA
458 1.1 bjh21 *
459 1.1 bjh21 * returns 1 if DMA OK, 0 for polled I/O transfer
460 1.1 bjh21 */
461 1.1 bjh21 int
462 1.22 tsutsui wd33c93_dmaok(struct wd33c93_softc *sc, struct scsipi_xfer *xs)
463 1.1 bjh21 {
464 1.23 rumble if (wd33c93_nodma || sc->sc_dmamode == SBIC_CTL_NO_DMA ||
465 1.23 rumble (xs->xs_control & XS_CTL_POLL) || xs->datalen == 0)
466 1.1 bjh21 return (0);
467 1.1 bjh21 return(1);
468 1.1 bjh21 }
469 1.1 bjh21
470 1.1 bjh21 /*
471 1.1 bjh21 * Setup for DMA transfer
472 1.1 bjh21 */
473 1.1 bjh21 void
474 1.22 tsutsui wd33c93_dma_setup(struct wd33c93_softc *sc, int datain)
475 1.1 bjh21 {
476 1.22 tsutsui struct wd33c93_acb *acb = sc->sc_nexus;
477 1.1 bjh21 int s;
478 1.1 bjh21
479 1.22 tsutsui sc->sc_daddr = acb->daddr;
480 1.22 tsutsui sc->sc_dleft = acb->dleft;
481 1.1 bjh21
482 1.1 bjh21 s = splbio();
483 1.1 bjh21 /* Indicate that we're in DMA mode */
484 1.22 tsutsui if (sc->sc_dleft) {
485 1.22 tsutsui sc->sc_dmasetup(sc, &sc->sc_daddr, &sc->sc_dleft,
486 1.22 tsutsui datain, &sc->sc_dleft);
487 1.1 bjh21 }
488 1.1 bjh21 splx(s);
489 1.1 bjh21 return;
490 1.1 bjh21 }
491 1.1 bjh21
492 1.1 bjh21
493 1.1 bjh21 /*
494 1.1 bjh21 * Save DMA pointers. Take into account partial transfer. Shut down DMA.
495 1.1 bjh21 */
496 1.1 bjh21 void
497 1.22 tsutsui wd33c93_dma_stop(struct wd33c93_softc *sc)
498 1.1 bjh21 {
499 1.1 bjh21 size_t count;
500 1.1 bjh21 int asr;
501 1.1 bjh21
502 1.1 bjh21 /* Wait until WD chip is idle */
503 1.1 bjh21 do {
504 1.22 tsutsui GET_SBIC_asr(sc, asr); /* XXX */
505 1.1 bjh21 if (asr & SBIC_ASR_DBR) {
506 1.1 bjh21 printf("wd33c93_dma_stop: asr %02x canceled!\n", asr);
507 1.1 bjh21 break;
508 1.1 bjh21 }
509 1.1 bjh21 } while (asr & (SBIC_ASR_BSY|SBIC_ASR_CIP));
510 1.1 bjh21
511 1.1 bjh21 /* Only need to save pointers if DMA was active */
512 1.22 tsutsui if (sc->sc_flags & SBICF_INDMA) {
513 1.1 bjh21 int s = splbio();
514 1.1 bjh21
515 1.1 bjh21 /* Shut down DMA and flush FIFO's */
516 1.22 tsutsui sc->sc_dmastop(sc);
517 1.1 bjh21
518 1.1 bjh21 /* Fetch the residual count */
519 1.22 tsutsui SBIC_TC_GET(sc, count);
520 1.1 bjh21
521 1.1 bjh21 /* Work out how many bytes were actually transferred */
522 1.22 tsutsui count = sc->sc_tcnt - count;
523 1.1 bjh21
524 1.22 tsutsui if (sc->sc_dleft < count)
525 1.2 bjh21 printf("xfer too large: dleft=%zu resid=%zu\n",
526 1.22 tsutsui sc->sc_dleft, count);
527 1.1 bjh21
528 1.1 bjh21 /* Fixup partial xfers */
529 1.22 tsutsui sc->sc_daddr = (char *)sc->sc_daddr + count;
530 1.22 tsutsui sc->sc_dleft -= count;
531 1.22 tsutsui sc->sc_tcnt = 0;
532 1.22 tsutsui sc->sc_flags &= ~SBICF_INDMA;
533 1.1 bjh21 splx(s);
534 1.1 bjh21 SBIC_DEBUG(DMA, ("dma_stop\n"));
535 1.1 bjh21 }
536 1.1 bjh21 /*
537 1.1 bjh21 * Ensure the WD chip is back in polled I/O mode, with nothing to
538 1.1 bjh21 * transfer.
539 1.1 bjh21 */
540 1.22 tsutsui SBIC_TC_PUT(sc, 0);
541 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
542 1.1 bjh21 }
543 1.1 bjh21
544 1.1 bjh21
545 1.1 bjh21 /*
546 1.1 bjh21 * Handle new request from scsipi layer
547 1.1 bjh21 */
548 1.1 bjh21 void
549 1.1 bjh21 wd33c93_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
550 1.1 bjh21 {
551 1.22 tsutsui struct wd33c93_softc *sc =
552 1.21 bjh21 device_private(chan->chan_adapter->adapt_dev);
553 1.1 bjh21 struct scsipi_xfer *xs;
554 1.1 bjh21 struct scsipi_periph *periph;
555 1.1 bjh21 struct wd33c93_acb *acb;
556 1.1 bjh21 int flags, s;
557 1.1 bjh21
558 1.15 rumble SBIC_DEBUG(MISC, ("wd33c93_scsi_request: req 0x%x\n", (int)req));
559 1.15 rumble
560 1.1 bjh21 switch (req) {
561 1.1 bjh21 case ADAPTER_REQ_RUN_XFER:
562 1.1 bjh21 xs = arg;
563 1.1 bjh21 periph = xs->xs_periph;
564 1.1 bjh21 flags = xs->xs_control;
565 1.1 bjh21
566 1.1 bjh21 if (flags & XS_CTL_DATA_UIO)
567 1.1 bjh21 panic("wd33c93: scsi data uio requested");
568 1.1 bjh21
569 1.22 tsutsui if (sc->sc_nexus && (flags & XS_CTL_POLL))
570 1.1 bjh21 panic("wd33c93_scsicmd: busy");
571 1.1 bjh21
572 1.1 bjh21 s = splbio();
573 1.28 christos acb = pool_get(&wd33c93_pool, PR_NOWAIT);
574 1.1 bjh21 splx(s);
575 1.1 bjh21
576 1.1 bjh21 if (acb == NULL) {
577 1.1 bjh21 scsipi_printaddr(periph);
578 1.1 bjh21 printf("cannot allocate acb\n");
579 1.1 bjh21 xs->error = XS_RESOURCE_SHORTAGE;
580 1.1 bjh21 scsipi_done(xs);
581 1.1 bjh21 return;
582 1.1 bjh21 }
583 1.1 bjh21
584 1.1 bjh21 acb->flags = ACB_ACTIVE;
585 1.1 bjh21 acb->xs = xs;
586 1.1 bjh21 acb->clen = xs->cmdlen;
587 1.1 bjh21 acb->daddr = xs->data;
588 1.1 bjh21 acb->dleft = xs->datalen;
589 1.1 bjh21 acb->timeout = xs->timeout;
590 1.1 bjh21 memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
591 1.1 bjh21
592 1.1 bjh21 if (flags & XS_CTL_POLL) {
593 1.1 bjh21 /*
594 1.1 bjh21 * Complete currently active command(s) before
595 1.1 bjh21 * issuing an immediate command
596 1.1 bjh21 */
597 1.22 tsutsui while (sc->sc_nexus)
598 1.22 tsutsui wd33c93_poll(sc, sc->sc_nexus);
599 1.1 bjh21 }
600 1.1 bjh21
601 1.1 bjh21 s = splbio();
602 1.22 tsutsui TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
603 1.1 bjh21 acb->flags |= ACB_READY;
604 1.1 bjh21
605 1.1 bjh21 /* If nothing is active, try to start it now. */
606 1.22 tsutsui if (sc->sc_state == SBIC_IDLE)
607 1.22 tsutsui wd33c93_sched(sc);
608 1.1 bjh21 splx(s);
609 1.1 bjh21
610 1.1 bjh21 if ((flags & XS_CTL_POLL) == 0)
611 1.1 bjh21 return;
612 1.1 bjh21
613 1.22 tsutsui if (wd33c93_poll(sc, acb)) {
614 1.1 bjh21 wd33c93_timeout(acb);
615 1.22 tsutsui if (wd33c93_poll(sc, acb)) /* 2nd retry for ABORT */
616 1.1 bjh21 wd33c93_timeout(acb);
617 1.1 bjh21 }
618 1.1 bjh21 return;
619 1.1 bjh21
620 1.1 bjh21 case ADAPTER_REQ_GROW_RESOURCES:
621 1.1 bjh21 /* XXX Not supported. */
622 1.1 bjh21 return;
623 1.1 bjh21
624 1.1 bjh21 case ADAPTER_REQ_SET_XFER_MODE:
625 1.1 bjh21 {
626 1.1 bjh21 struct wd33c93_tinfo *ti;
627 1.1 bjh21 struct scsipi_xfer_mode *xm = arg;
628 1.1 bjh21
629 1.22 tsutsui ti = &sc->sc_tinfo[xm->xm_target];
630 1.8 bjh21 ti->flags &= ~T_WANTSYNC;
631 1.1 bjh21
632 1.22 tsutsui if ((CFFLAGS_NOTAGS(sc->sc_cfflags, xm->xm_target) == 0) &&
633 1.1 bjh21 (xm->xm_mode & PERIPH_CAP_TQING) && !wd33c93_notags)
634 1.1 bjh21 ti->flags |= T_TAG;
635 1.1 bjh21 else
636 1.1 bjh21 ti->flags &= ~T_TAG;
637 1.1 bjh21
638 1.8 bjh21 SBIC_DEBUG(SYNC, ("wd33c93_scsi_request: "
639 1.8 bjh21 "target %d: scsipi requested %s\n", xm->xm_target,
640 1.8 bjh21 (xm->xm_mode & PERIPH_CAP_SYNC) ? "sync" : "async"));
641 1.8 bjh21
642 1.1 bjh21 if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
643 1.8 bjh21 (ti->flags & T_NOSYNC) == 0)
644 1.8 bjh21 ti->flags |= T_WANTSYNC;
645 1.1 bjh21 /*
646 1.1 bjh21 * If we're not going to negotiate, send the notification
647 1.1 bjh21 * now, since it won't happen later.
648 1.1 bjh21 */
649 1.8 bjh21 if (!(ti->flags & T_WANTSYNC) == !(ti->flags & T_SYNCMODE))
650 1.22 tsutsui wd33c93_update_xfer_mode(sc, xm->xm_target);
651 1.8 bjh21 else
652 1.8 bjh21 ti->flags |= T_NEGOTIATE;
653 1.1 bjh21 return;
654 1.1 bjh21 }
655 1.1 bjh21
656 1.1 bjh21 }
657 1.1 bjh21 }
658 1.1 bjh21
659 1.1 bjh21 /*
660 1.1 bjh21 * attempt to start the next available command
661 1.1 bjh21 */
662 1.1 bjh21 void
663 1.22 tsutsui wd33c93_sched(struct wd33c93_softc *sc)
664 1.1 bjh21 {
665 1.1 bjh21 struct scsipi_periph *periph = NULL; /* Gag the compiler */
666 1.1 bjh21 struct wd33c93_acb *acb;
667 1.1 bjh21 struct wd33c93_tinfo *ti;
668 1.1 bjh21 struct wd33c93_linfo *li;
669 1.1 bjh21 int lun, tag, flags;
670 1.1 bjh21
671 1.22 tsutsui if (sc->sc_state != SBIC_IDLE)
672 1.1 bjh21 return;
673 1.1 bjh21
674 1.22 tsutsui KASSERT(sc->sc_nexus == NULL);
675 1.1 bjh21
676 1.1 bjh21 /* Loop through the ready list looking for work to do... */
677 1.22 tsutsui TAILQ_FOREACH(acb, &sc->ready_list, chain) {
678 1.1 bjh21 periph = acb->xs->xs_periph;
679 1.1 bjh21 lun = periph->periph_lun;
680 1.22 tsutsui ti = &sc->sc_tinfo[periph->periph_target];
681 1.1 bjh21 li = TINFO_LUN(ti, lun);
682 1.1 bjh21
683 1.1 bjh21 KASSERT(acb->flags & ACB_READY);
684 1.1 bjh21
685 1.1 bjh21 /* Select type of tag for this command */
686 1.1 bjh21 if ((ti->flags & T_NODISC) != 0)
687 1.1 bjh21 tag = 0;
688 1.1 bjh21 else if ((ti->flags & T_TAG) == 0)
689 1.1 bjh21 tag = 0;
690 1.1 bjh21 else if ((acb->flags & ACB_SENSE) != 0)
691 1.1 bjh21 tag = 0;
692 1.1 bjh21 else if (acb->xs->xs_control & XS_CTL_POLL)
693 1.1 bjh21 tag = 0; /* No tags for polled commands */
694 1.1 bjh21 else
695 1.1 bjh21 tag = acb->xs->xs_tag_type;
696 1.1 bjh21
697 1.1 bjh21 if (li == NULL) {
698 1.1 bjh21 /* Initialize LUN info and add to list. */
699 1.1 bjh21 li = malloc(sizeof(*li), M_DEVBUF, M_NOWAIT);
700 1.1 bjh21 if (li == NULL)
701 1.1 bjh21 continue;
702 1.1 bjh21 memset(li, 0, sizeof(*li));
703 1.1 bjh21 li->lun = lun;
704 1.1 bjh21 if (lun < SBIC_NLUN)
705 1.1 bjh21 ti->lun[lun] = li;
706 1.1 bjh21 }
707 1.7 rumble li->last_used = time_second;
708 1.1 bjh21
709 1.1 bjh21 /*
710 1.1 bjh21 * We've found a potential command, but is the target/lun busy?
711 1.1 bjh21 */
712 1.1 bjh21
713 1.1 bjh21 if (tag == 0 && li->untagged == NULL)
714 1.1 bjh21 li->untagged = acb; /* Issue untagged */
715 1.1 bjh21
716 1.1 bjh21 if (li->untagged != NULL) {
717 1.1 bjh21 tag = 0;
718 1.1 bjh21 if ((li->state != L_STATE_BUSY) && li->used == 0) {
719 1.1 bjh21 /* Issue this untagged command now */
720 1.1 bjh21 acb = li->untagged;
721 1.1 bjh21 periph = acb->xs->xs_periph;
722 1.1 bjh21 } else /* Not ready yet */
723 1.1 bjh21 continue;
724 1.1 bjh21 }
725 1.1 bjh21
726 1.1 bjh21 acb->tag_type = tag;
727 1.1 bjh21 if (tag != 0) {
728 1.1 bjh21 if (li->queued[acb->xs->xs_tag_id])
729 1.1 bjh21 printf("queueing to active tag\n");
730 1.1 bjh21 li->queued[acb->xs->xs_tag_id] = acb;
731 1.1 bjh21 acb->tag_id = acb->xs->xs_tag_id;
732 1.1 bjh21 li->used++;
733 1.1 bjh21 break;
734 1.1 bjh21 }
735 1.1 bjh21 if (li->untagged != NULL && (li->state != L_STATE_BUSY)) {
736 1.1 bjh21 li->state = L_STATE_BUSY;
737 1.1 bjh21 break;
738 1.1 bjh21 }
739 1.1 bjh21 if (li->untagged == NULL && tag != 0) {
740 1.1 bjh21 break;
741 1.1 bjh21 } else
742 1.1 bjh21 printf("%d:%d busy\n", periph->periph_target,
743 1.1 bjh21 periph->periph_lun);
744 1.1 bjh21 }
745 1.1 bjh21
746 1.1 bjh21 if (acb == NULL) {
747 1.1 bjh21 SBIC_DEBUG(ACBS, ("wd33c93sched: no work\n"));
748 1.1 bjh21 return; /* did not find an available command */
749 1.1 bjh21 }
750 1.1 bjh21
751 1.1 bjh21 SBIC_DEBUG(ACBS, ("wd33c93_sched(%d,%d)\n", periph->periph_target,
752 1.1 bjh21 periph->periph_lun));
753 1.1 bjh21
754 1.22 tsutsui TAILQ_REMOVE(&sc->ready_list, acb, chain);
755 1.1 bjh21 acb->flags &= ~ACB_READY;
756 1.1 bjh21
757 1.1 bjh21 flags = acb->xs->xs_control;
758 1.1 bjh21 if (flags & XS_CTL_RESET)
759 1.22 tsutsui wd33c93_reset(sc);
760 1.1 bjh21
761 1.1 bjh21 /* XXX - Implicitly call scsidone on select timeout */
762 1.22 tsutsui if (wd33c93_go(sc, acb) != 0 || acb->xs->error == XS_SELTIMEOUT) {
763 1.22 tsutsui acb->dleft = sc->sc_dleft;
764 1.22 tsutsui wd33c93_scsidone(sc, acb, sc->sc_status);
765 1.1 bjh21 return;
766 1.1 bjh21 }
767 1.1 bjh21
768 1.1 bjh21 return;
769 1.1 bjh21 }
770 1.1 bjh21
771 1.1 bjh21 void
772 1.22 tsutsui wd33c93_scsidone(struct wd33c93_softc *sc, struct wd33c93_acb *acb, int status)
773 1.1 bjh21 {
774 1.1 bjh21 struct scsipi_xfer *xs = acb->xs;
775 1.1 bjh21 struct wd33c93_tinfo *ti;
776 1.1 bjh21 struct wd33c93_linfo *li;
777 1.1 bjh21 int s;
778 1.1 bjh21
779 1.1 bjh21 #ifdef DIAGNOSTIC
780 1.22 tsutsui KASSERT(sc->target == xs->xs_periph->periph_target);
781 1.22 tsutsui KASSERT(sc->lun == xs->xs_periph->periph_lun);
782 1.1 bjh21 if (acb == NULL || xs == NULL) {
783 1.1 bjh21 panic("wd33c93_scsidone -- (%d,%d) no scsipi_xfer",
784 1.22 tsutsui sc->target, sc->lun);
785 1.1 bjh21 }
786 1.1 bjh21 KASSERT(acb->flags != ACB_FREE);
787 1.1 bjh21 #endif
788 1.1 bjh21
789 1.1 bjh21 SBIC_DEBUG(ACBS, ("scsidone: (%d,%d)->(%d,%d)%02x\n",
790 1.1 bjh21 xs->xs_periph->periph_target, xs->xs_periph->periph_lun,
791 1.22 tsutsui sc->target, sc->lun, status));
792 1.1 bjh21 callout_stop(&xs->xs_callout);
793 1.1 bjh21
794 1.1 bjh21 xs->status = status & SCSI_STATUS_MASK;
795 1.1 bjh21 xs->resid = acb->dleft;
796 1.1 bjh21
797 1.1 bjh21 if (xs->error == XS_NOERROR) {
798 1.1 bjh21 switch (xs->status) {
799 1.1 bjh21 case SCSI_CHECK:
800 1.1 bjh21 case SCSI_TERMINATED:
801 1.1 bjh21 /* XXX Need to read sense - return busy for now */
802 1.1 bjh21 /*FALLTHROUGH*/
803 1.1 bjh21 case SCSI_QUEUE_FULL:
804 1.1 bjh21 case SCSI_BUSY:
805 1.1 bjh21 xs->error = XS_BUSY;
806 1.1 bjh21 break;
807 1.1 bjh21 }
808 1.1 bjh21 }
809 1.1 bjh21
810 1.22 tsutsui ti = &sc->sc_tinfo[sc->target];
811 1.22 tsutsui li = TINFO_LUN(ti, sc->lun);
812 1.1 bjh21 ti->cmds++;
813 1.1 bjh21 if (xs->error == XS_SELTIMEOUT) {
814 1.1 bjh21 /* Selection timeout -- discard this LUN if empty */
815 1.1 bjh21 if (li->untagged == NULL && li->used == 0) {
816 1.22 tsutsui if (sc->lun < SBIC_NLUN)
817 1.22 tsutsui ti->lun[sc->lun] = NULL;
818 1.1 bjh21 free(li, M_DEVBUF);
819 1.1 bjh21 }
820 1.1 bjh21 }
821 1.1 bjh21
822 1.22 tsutsui wd33c93_dequeue(sc, acb);
823 1.22 tsutsui if (sc->sc_nexus == acb) {
824 1.22 tsutsui sc->sc_state = SBIC_IDLE;
825 1.22 tsutsui sc->sc_nexus = NULL;
826 1.22 tsutsui sc->sc_flags = 0;
827 1.1 bjh21
828 1.22 tsutsui if (!TAILQ_EMPTY(&sc->ready_list))
829 1.22 tsutsui wd33c93_sched(sc);
830 1.1 bjh21 }
831 1.1 bjh21
832 1.1 bjh21 /* place control block back on free list. */
833 1.1 bjh21 s = splbio();
834 1.1 bjh21 acb->flags = ACB_FREE;
835 1.22 tsutsui pool_put(&wd33c93_pool, acb);
836 1.1 bjh21 splx(s);
837 1.1 bjh21
838 1.1 bjh21 scsipi_done(xs);
839 1.1 bjh21 }
840 1.1 bjh21
841 1.1 bjh21 void
842 1.22 tsutsui wd33c93_dequeue(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
843 1.1 bjh21 {
844 1.22 tsutsui struct wd33c93_tinfo *ti = &sc->sc_tinfo[acb->xs->xs_periph->periph_target];
845 1.1 bjh21 struct wd33c93_linfo *li;
846 1.1 bjh21 int lun = acb->xs->xs_periph->periph_lun;
847 1.1 bjh21
848 1.1 bjh21 li = TINFO_LUN(ti, lun);
849 1.1 bjh21 #ifdef DIAGNOSTIC
850 1.1 bjh21 if (li == NULL || li->lun != lun)
851 1.1 bjh21 panic("wd33c93_dequeue: lun %d for ecb %p does not exist",
852 1.1 bjh21 lun, acb);
853 1.1 bjh21 #endif
854 1.1 bjh21 if (li->untagged == acb) {
855 1.1 bjh21 li->state = L_STATE_IDLE;
856 1.1 bjh21 li->untagged = NULL;
857 1.1 bjh21 }
858 1.1 bjh21 if (acb->tag_type && li->queued[acb->tag_id] != NULL) {
859 1.1 bjh21 #ifdef DIAGNOSTIC
860 1.1 bjh21 if (li->queued[acb->tag_id] != NULL &&
861 1.1 bjh21 (li->queued[acb->tag_id] != acb))
862 1.1 bjh21 panic("wd33c93_dequeue: slot %d for lun %d has %p "
863 1.1 bjh21 "instead of acb %p\n", acb->tag_id,
864 1.1 bjh21 lun, li->queued[acb->tag_id], acb);
865 1.1 bjh21 #endif
866 1.1 bjh21 li->queued[acb->tag_id] = NULL;
867 1.1 bjh21 li->used--;
868 1.1 bjh21 }
869 1.1 bjh21 }
870 1.1 bjh21
871 1.1 bjh21
872 1.1 bjh21 int
873 1.22 tsutsui wd33c93_wait(struct wd33c93_softc *sc, u_char until, int timeo, int line)
874 1.1 bjh21 {
875 1.1 bjh21 u_char val;
876 1.1 bjh21
877 1.1 bjh21 if (timeo == 0)
878 1.1 bjh21 timeo = 1000000; /* some large value.. */
879 1.22 tsutsui GET_SBIC_asr(sc, val);
880 1.1 bjh21 while ((val & until) == 0) {
881 1.1 bjh21 if (timeo-- == 0) {
882 1.1 bjh21 int csr;
883 1.22 tsutsui GET_SBIC_csr(sc, csr);
884 1.1 bjh21 printf("wd33c93_wait: TIMEO @%d with asr=x%x csr=x%x\n",
885 1.1 bjh21 line, val, csr);
886 1.1 bjh21 #if defined(DDB) && defined(DEBUG)
887 1.1 bjh21 Debugger();
888 1.1 bjh21 #endif
889 1.1 bjh21 return(val); /* Maybe I should abort */
890 1.1 bjh21 break;
891 1.1 bjh21 }
892 1.1 bjh21 DELAY(1);
893 1.22 tsutsui GET_SBIC_asr(sc, val);
894 1.1 bjh21 }
895 1.1 bjh21 return(val);
896 1.1 bjh21 }
897 1.1 bjh21
898 1.1 bjh21 int
899 1.22 tsutsui wd33c93_abort(struct wd33c93_softc *sc, struct wd33c93_acb *acb,
900 1.1 bjh21 const char *where)
901 1.1 bjh21 {
902 1.1 bjh21 u_char csr, asr;
903 1.1 bjh21
904 1.22 tsutsui GET_SBIC_asr(sc, asr);
905 1.22 tsutsui GET_SBIC_csr(sc, csr);
906 1.1 bjh21
907 1.1 bjh21 scsipi_printaddr(acb->xs->xs_periph);
908 1.1 bjh21 printf ("ABORT in %s: csr=0x%02x, asr=0x%02x\n", where, csr, asr);
909 1.1 bjh21
910 1.1 bjh21 acb->timeout = SBIC_ABORT_TIMEOUT;
911 1.1 bjh21 acb->flags |= ACB_ABORT;
912 1.1 bjh21
913 1.1 bjh21 /*
914 1.1 bjh21 * Clean up chip itself
915 1.1 bjh21 */
916 1.22 tsutsui if (sc->sc_nexus == acb) {
917 1.1 bjh21 /* Reschedule timeout. */
918 1.1 bjh21 callout_reset(&acb->xs->xs_callout, mstohz(acb->timeout),
919 1.1 bjh21 wd33c93_timeout, acb);
920 1.1 bjh21
921 1.1 bjh21 while (asr & SBIC_ASR_DBR) {
922 1.1 bjh21 /*
923 1.1 bjh21 * wd33c93 is jammed w/data. need to clear it
924 1.1 bjh21 * But we don't know what direction it needs to go
925 1.1 bjh21 */
926 1.22 tsutsui GET_SBIC_data(sc, asr);
927 1.1 bjh21 printf("abort %s: clearing data buffer 0x%02x\n",
928 1.1 bjh21 where, asr);
929 1.22 tsutsui GET_SBIC_asr(sc, asr);
930 1.1 bjh21 if (asr & SBIC_ASR_DBR) /* Not the read direction */
931 1.22 tsutsui SET_SBIC_data(sc, asr);
932 1.22 tsutsui GET_SBIC_asr(sc, asr);
933 1.1 bjh21 }
934 1.1 bjh21
935 1.1 bjh21 scsipi_printaddr(acb->xs->xs_periph);
936 1.1 bjh21 printf("sending ABORT command\n");
937 1.1 bjh21
938 1.22 tsutsui WAIT_CIP(sc);
939 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_ABORT);
940 1.22 tsutsui WAIT_CIP(sc);
941 1.1 bjh21
942 1.22 tsutsui GET_SBIC_asr(sc, asr);
943 1.1 bjh21
944 1.1 bjh21 scsipi_printaddr(acb->xs->xs_periph);
945 1.1 bjh21 if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
946 1.1 bjh21 /*
947 1.1 bjh21 * ok, get more drastic..
948 1.1 bjh21 */
949 1.1 bjh21 printf("Resetting bus\n");
950 1.22 tsutsui wd33c93_reset(sc);
951 1.1 bjh21 } else {
952 1.1 bjh21 printf("sending DISCONNECT to target\n");
953 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_DISC);
954 1.22 tsutsui WAIT_CIP(sc);
955 1.1 bjh21
956 1.1 bjh21 do {
957 1.22 tsutsui SBIC_WAIT (sc, SBIC_ASR_INT, 0);
958 1.22 tsutsui GET_SBIC_asr(sc, asr);
959 1.22 tsutsui GET_SBIC_csr(sc, csr);
960 1.1 bjh21 SBIC_DEBUG(MISC, ("csr: 0x%02x, asr: 0x%02x\n",
961 1.1 bjh21 csr, asr));
962 1.1 bjh21 } while ((csr != SBIC_CSR_DISC) &&
963 1.1 bjh21 (csr != SBIC_CSR_DISC_1) &&
964 1.1 bjh21 (csr != SBIC_CSR_CMD_INVALID));
965 1.1 bjh21 }
966 1.22 tsutsui sc->sc_state = SBIC_ERROR;
967 1.22 tsutsui sc->sc_flags = 0;
968 1.1 bjh21 }
969 1.1 bjh21 return SBIC_STATE_ERROR;
970 1.1 bjh21 }
971 1.1 bjh21
972 1.1 bjh21
973 1.1 bjh21 /*
974 1.1 bjh21 * select the bus, return when selected or error.
975 1.1 bjh21 *
976 1.1 bjh21 * Returns the current CSR following selection and optionally MSG out phase.
977 1.1 bjh21 * i.e. the returned CSR *should* indicate CMD phase...
978 1.1 bjh21 * If the return value is 0, some error happened.
979 1.1 bjh21 */
980 1.1 bjh21 u_char
981 1.22 tsutsui wd33c93_selectbus(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
982 1.1 bjh21 {
983 1.1 bjh21 struct scsipi_xfer *xs = acb->xs;
984 1.1 bjh21 struct wd33c93_tinfo *ti;
985 1.1 bjh21 u_char target, lun, asr, csr, id;
986 1.1 bjh21
987 1.22 tsutsui KASSERT(sc->sc_state == SBIC_IDLE);
988 1.1 bjh21
989 1.1 bjh21 target = xs->xs_periph->periph_target;
990 1.1 bjh21 lun = xs->xs_periph->periph_lun;
991 1.22 tsutsui ti = &sc->sc_tinfo[target];
992 1.1 bjh21
993 1.22 tsutsui sc->sc_state = SBIC_SELECTING;
994 1.22 tsutsui sc->target = target;
995 1.22 tsutsui sc->lun = lun;
996 1.1 bjh21
997 1.1 bjh21 SBIC_DEBUG(PHASE, ("wd33c93_selectbus %d: ", target));
998 1.1 bjh21
999 1.1 bjh21 if ((xs->xs_control & XS_CTL_POLL) == 0)
1000 1.1 bjh21 callout_reset(&xs->xs_callout, mstohz(acb->timeout),
1001 1.1 bjh21 wd33c93_timeout, acb);
1002 1.1 bjh21
1003 1.1 bjh21 /*
1004 1.1 bjh21 * issue select
1005 1.1 bjh21 */
1006 1.22 tsutsui SBIC_TC_PUT(sc, 0);
1007 1.22 tsutsui SET_SBIC_selid(sc, target);
1008 1.22 tsutsui SET_SBIC_timeo(sc, SBIC_TIMEOUT(250, sc->sc_clkfreq));
1009 1.1 bjh21
1010 1.22 tsutsui GET_SBIC_asr(sc, asr);
1011 1.1 bjh21 if (asr & (SBIC_ASR_INT|SBIC_ASR_BSY)) {
1012 1.1 bjh21 /* This means we got ourselves reselected upon */
1013 1.1 bjh21 SBIC_DEBUG(PHASE, ("WD busy (reselect?) ASR=%02x\n", asr));
1014 1.1 bjh21 return 0;
1015 1.1 bjh21 }
1016 1.1 bjh21
1017 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_SEL_ATN);
1018 1.22 tsutsui WAIT_CIP(sc);
1019 1.1 bjh21
1020 1.1 bjh21 /*
1021 1.1 bjh21 * wait for select (merged from separate function may need
1022 1.1 bjh21 * cleanup)
1023 1.1 bjh21 */
1024 1.1 bjh21 do {
1025 1.22 tsutsui asr = SBIC_WAIT(sc, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
1026 1.1 bjh21 if (asr & SBIC_ASR_LCI) {
1027 1.1 bjh21 QPRINTF(("late LCI: asr %02x\n", asr));
1028 1.1 bjh21 return 0;
1029 1.1 bjh21 }
1030 1.1 bjh21
1031 1.1 bjh21 /* Clear interrupt */
1032 1.22 tsutsui GET_SBIC_csr (sc, csr);
1033 1.1 bjh21
1034 1.1 bjh21 /* Reselected from under our feet? */
1035 1.1 bjh21 if (csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
1036 1.1 bjh21 SBIC_DEBUG(PHASE, ("got reselected, asr %02x\n", asr));
1037 1.1 bjh21 /*
1038 1.1 bjh21 * We need to handle this now so we don't lock up later
1039 1.1 bjh21 */
1040 1.22 tsutsui wd33c93_nextstate(sc, acb, csr, asr);
1041 1.1 bjh21 return 0;
1042 1.1 bjh21 }
1043 1.1 bjh21
1044 1.1 bjh21 /* Whoops! */
1045 1.1 bjh21 if (csr == SBIC_CSR_SLT || csr == SBIC_CSR_SLT_ATN) {
1046 1.1 bjh21 panic("wd33c93_selectbus: target issued select!");
1047 1.1 bjh21 return 0;
1048 1.1 bjh21 }
1049 1.1 bjh21
1050 1.1 bjh21 } while (csr != (SBIC_CSR_MIS_2 | MESG_OUT_PHASE) &&
1051 1.1 bjh21 csr != (SBIC_CSR_MIS_2 | CMD_PHASE) &&
1052 1.1 bjh21 csr != SBIC_CSR_SEL_TIMEO);
1053 1.1 bjh21
1054 1.1 bjh21 /* Anyone at home? */
1055 1.1 bjh21 if (csr == SBIC_CSR_SEL_TIMEO) {
1056 1.1 bjh21 xs->error = XS_SELTIMEOUT;
1057 1.1 bjh21 SBIC_DEBUG(PHASE, ("-- Selection Timeout\n"));
1058 1.1 bjh21 return 0;
1059 1.1 bjh21 }
1060 1.1 bjh21
1061 1.1 bjh21 SBIC_DEBUG(PHASE, ("Selection Complete\n"));
1062 1.1 bjh21
1063 1.1 bjh21 /* Assume we're now selected */
1064 1.22 tsutsui GET_SBIC_selid(sc, id);
1065 1.1 bjh21 if (id != target) {
1066 1.1 bjh21 /* Something went wrong - wrong target was select */
1067 1.1 bjh21 printf("wd33c93_selectbus: wrong target selected;"
1068 1.1 bjh21 " WANTED %d GOT %d", target, id);
1069 1.1 bjh21 return 0; /* XXX: Need to call nexstate to handle? */
1070 1.1 bjh21 }
1071 1.1 bjh21
1072 1.22 tsutsui sc->sc_flags |= SBICF_SELECTED;
1073 1.22 tsutsui sc->sc_state = SBIC_CONNECTED;
1074 1.1 bjh21
1075 1.1 bjh21 /* setup correct sync mode for this target */
1076 1.22 tsutsui wd33c93_setsync(sc, ti);
1077 1.1 bjh21
1078 1.22 tsutsui if (ti->flags & T_NODISC && sc->sc_disc == 0)
1079 1.22 tsutsui SET_SBIC_rselid (sc, 0); /* Not expecting a reselect */
1080 1.1 bjh21 else
1081 1.22 tsutsui SET_SBIC_rselid (sc, SBIC_RID_ER);
1082 1.1 bjh21
1083 1.1 bjh21 /*
1084 1.1 bjh21 * We only really need to do anything when the target goes to MSG out
1085 1.1 bjh21 * If the device ignored ATN, it's probably old and brain-dead,
1086 1.1 bjh21 * but we'll try to support it anyhow.
1087 1.1 bjh21 * If it doesn't support message out, it definately doesn't
1088 1.1 bjh21 * support synchronous transfers, so no point in even asking...
1089 1.1 bjh21 */
1090 1.1 bjh21 if (csr == (SBIC_CSR_MIS_2 | MESG_OUT_PHASE)) {
1091 1.1 bjh21 if (ti->flags & T_NEGOTIATE) {
1092 1.1 bjh21 /* Inititae a SDTR message */
1093 1.1 bjh21 SBIC_DEBUG(SYNC, ("Sending SDTR to target %d\n", id));
1094 1.8 bjh21 if (ti->flags & T_WANTSYNC) {
1095 1.22 tsutsui ti->period = sc->sc_minsyncperiod;
1096 1.22 tsutsui ti->offset = sc->sc_maxoffset;
1097 1.8 bjh21 } else {
1098 1.8 bjh21 ti->period = 0;
1099 1.8 bjh21 ti->offset = 0;
1100 1.8 bjh21 }
1101 1.1 bjh21 /* Send Sync negotiation message */
1102 1.22 tsutsui sc->sc_omsg[0] = MSG_IDENTIFY(lun, 0); /* No Disc */
1103 1.22 tsutsui sc->sc_omsg[1] = MSG_EXTENDED;
1104 1.22 tsutsui sc->sc_omsg[2] = MSG_EXT_SDTR_LEN;
1105 1.22 tsutsui sc->sc_omsg[3] = MSG_EXT_SDTR;
1106 1.8 bjh21 if (ti->flags & T_WANTSYNC) {
1107 1.22 tsutsui sc->sc_omsg[4] = sc->sc_minsyncperiod;
1108 1.22 tsutsui sc->sc_omsg[5] = sc->sc_maxoffset;
1109 1.8 bjh21 } else {
1110 1.22 tsutsui sc->sc_omsg[4] = 0;
1111 1.22 tsutsui sc->sc_omsg[5] = 0;
1112 1.8 bjh21 }
1113 1.22 tsutsui wd33c93_xfout(sc, 6, sc->sc_omsg);
1114 1.22 tsutsui sc->sc_msgout |= SEND_SDTR; /* may be rejected */
1115 1.22 tsutsui sc->sc_flags |= SBICF_SYNCNEGO;
1116 1.1 bjh21 } else {
1117 1.22 tsutsui if (sc->sc_nexus->tag_type != 0) {
1118 1.1 bjh21 /* Use TAGS */
1119 1.1 bjh21 SBIC_DEBUG(TAGS, ("<select %d:%d TAG=%x>\n",
1120 1.22 tsutsui sc->target, sc->lun,
1121 1.22 tsutsui sc->sc_nexus->tag_id));
1122 1.22 tsutsui sc->sc_omsg[0] = MSG_IDENTIFY(lun, 1);
1123 1.22 tsutsui sc->sc_omsg[1] = sc->sc_nexus->tag_type;
1124 1.22 tsutsui sc->sc_omsg[2] = sc->sc_nexus->tag_id;
1125 1.22 tsutsui wd33c93_xfout(sc, 3, sc->sc_omsg);
1126 1.22 tsutsui sc->sc_msgout |= SEND_TAG;
1127 1.1 bjh21 } else {
1128 1.1 bjh21 int no_disc;
1129 1.1 bjh21
1130 1.1 bjh21 /* Setup LUN nexus and disconnect privilege */
1131 1.1 bjh21 no_disc = xs->xs_control & XS_CTL_POLL ||
1132 1.1 bjh21 ti->flags & T_NODISC;
1133 1.22 tsutsui SEND_BYTE(sc, MSG_IDENTIFY(lun, !no_disc));
1134 1.1 bjh21 }
1135 1.1 bjh21 }
1136 1.1 bjh21 /*
1137 1.1 bjh21 * There's one interrupt still to come:
1138 1.1 bjh21 * the change to CMD phase...
1139 1.1 bjh21 */
1140 1.22 tsutsui SBIC_WAIT(sc, SBIC_ASR_INT , 0);
1141 1.22 tsutsui GET_SBIC_csr(sc, csr);
1142 1.1 bjh21 }
1143 1.1 bjh21
1144 1.1 bjh21 return csr;
1145 1.1 bjh21 }
1146 1.1 bjh21
1147 1.1 bjh21 /*
1148 1.1 bjh21 * Information Transfer *to* a SCSI Target.
1149 1.1 bjh21 *
1150 1.1 bjh21 * Note: Don't expect there to be an interrupt immediately after all
1151 1.1 bjh21 * the data is transferred out. The WD spec sheet says that the Transfer-
1152 1.1 bjh21 * Info command for non-MSG_IN phases only completes when the target
1153 1.1 bjh21 * next asserts 'REQ'. That is, when the SCSI bus changes to a new state.
1154 1.1 bjh21 *
1155 1.1 bjh21 * This can have a nasty effect on commands which take a relatively long
1156 1.1 bjh21 * time to complete, for example a START/STOP unit command may remain in
1157 1.1 bjh21 * CMD phase until the disk has spun up. Only then will the target change
1158 1.1 bjh21 * to STATUS phase. This is really only a problem for immediate commands
1159 1.1 bjh21 * since we don't allow disconnection for them (yet).
1160 1.1 bjh21 */
1161 1.1 bjh21 int
1162 1.22 tsutsui wd33c93_xfout(struct wd33c93_softc *sc, int len, void *bp)
1163 1.1 bjh21 {
1164 1.1 bjh21 int wait = wd33c93_data_wait;
1165 1.1 bjh21 u_char asr, *buf = bp;
1166 1.1 bjh21
1167 1.1 bjh21 QPRINTF(("wd33c93_xfout {%d} %02x %02x %02x %02x %02x "
1168 1.1 bjh21 "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
1169 1.1 bjh21 buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
1170 1.1 bjh21
1171 1.1 bjh21 /*
1172 1.1 bjh21 * sigh.. WD-PROTO strikes again.. sending the command in one go
1173 1.1 bjh21 * causes the chip to lock up if talking to certain (misbehaving?)
1174 1.1 bjh21 * targets. Anyway, this procedure should work for all targets, but
1175 1.1 bjh21 * it's slightly slower due to the overhead
1176 1.1 bjh21 */
1177 1.1 bjh21
1178 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
1179 1.22 tsutsui SBIC_TC_PUT (sc, (unsigned)len);
1180 1.1 bjh21
1181 1.22 tsutsui WAIT_CIP (sc);
1182 1.22 tsutsui SET_SBIC_cmd (sc, SBIC_CMD_XFER_INFO);
1183 1.1 bjh21
1184 1.1 bjh21 /*
1185 1.1 bjh21 * Loop for each byte transferred
1186 1.1 bjh21 */
1187 1.1 bjh21 do {
1188 1.22 tsutsui GET_SBIC_asr (sc, asr);
1189 1.1 bjh21
1190 1.1 bjh21 if (asr & SBIC_ASR_DBR) {
1191 1.1 bjh21 if (len) {
1192 1.22 tsutsui SET_SBIC_data (sc, *buf);
1193 1.1 bjh21 buf++;
1194 1.1 bjh21 len--;
1195 1.1 bjh21 } else {
1196 1.22 tsutsui SET_SBIC_data (sc, 0);
1197 1.1 bjh21 }
1198 1.1 bjh21 wait = wd33c93_data_wait;
1199 1.1 bjh21 }
1200 1.1 bjh21 } while (len && (asr & SBIC_ASR_INT) == 0 && wait-- > 0);
1201 1.1 bjh21
1202 1.1 bjh21 QPRINTF(("wd33c93_xfout done: %d bytes remaining (wait:%d)\n", len, wait));
1203 1.1 bjh21
1204 1.1 bjh21 /*
1205 1.1 bjh21 * Normally, an interrupt will be pending when this routing returns.
1206 1.1 bjh21 */
1207 1.1 bjh21 return(len);
1208 1.1 bjh21 }
1209 1.1 bjh21
1210 1.1 bjh21 /*
1211 1.1 bjh21 * Information Transfer *from* a Scsi Target
1212 1.1 bjh21 * returns # bytes left to read
1213 1.1 bjh21 */
1214 1.1 bjh21 int
1215 1.22 tsutsui wd33c93_xfin(struct wd33c93_softc *sc, int len, void *bp)
1216 1.1 bjh21 {
1217 1.1 bjh21 int wait = wd33c93_data_wait;
1218 1.1 bjh21 u_char *buf = bp;
1219 1.1 bjh21 u_char asr;
1220 1.1 bjh21 #ifdef DEBUG
1221 1.1 bjh21 u_char *obp = bp;
1222 1.1 bjh21 #endif
1223 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
1224 1.22 tsutsui SBIC_TC_PUT (sc, (unsigned)len);
1225 1.1 bjh21
1226 1.22 tsutsui WAIT_CIP (sc);
1227 1.22 tsutsui SET_SBIC_cmd (sc, SBIC_CMD_XFER_INFO);
1228 1.1 bjh21
1229 1.1 bjh21 /*
1230 1.1 bjh21 * Loop for each byte transferred
1231 1.1 bjh21 */
1232 1.1 bjh21 do {
1233 1.22 tsutsui GET_SBIC_asr (sc, asr);
1234 1.1 bjh21
1235 1.1 bjh21 if (asr & SBIC_ASR_DBR) {
1236 1.1 bjh21 if (len) {
1237 1.22 tsutsui GET_SBIC_data (sc, *buf);
1238 1.1 bjh21 buf++;
1239 1.1 bjh21 len--;
1240 1.1 bjh21 } else {
1241 1.1 bjh21 u_char foo;
1242 1.22 tsutsui GET_SBIC_data (sc, foo);
1243 1.25 christos __USE(foo);
1244 1.1 bjh21 }
1245 1.1 bjh21 wait = wd33c93_data_wait;
1246 1.1 bjh21 }
1247 1.1 bjh21
1248 1.1 bjh21 } while ((asr & SBIC_ASR_INT) == 0 && wait-- > 0);
1249 1.1 bjh21
1250 1.1 bjh21 QPRINTF(("wd33c93_xfin {%d} %02x %02x %02x %02x %02x %02x "
1251 1.1 bjh21 "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
1252 1.1 bjh21 obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
1253 1.1 bjh21
1254 1.22 tsutsui SBIC_TC_PUT (sc, 0);
1255 1.1 bjh21
1256 1.1 bjh21 /*
1257 1.1 bjh21 * this leaves with one csr to be read
1258 1.1 bjh21 */
1259 1.1 bjh21 return len;
1260 1.1 bjh21 }
1261 1.1 bjh21
1262 1.1 bjh21
1263 1.1 bjh21 /*
1264 1.1 bjh21 * Finish SCSI xfer command: After the completion interrupt from
1265 1.1 bjh21 * a read/write operation, sequence through the final phases in
1266 1.1 bjh21 * programmed i/o.
1267 1.1 bjh21 */
1268 1.1 bjh21 void
1269 1.22 tsutsui wd33c93_xferdone(struct wd33c93_softc *sc)
1270 1.1 bjh21 {
1271 1.1 bjh21 u_char phase, csr;
1272 1.1 bjh21 int s;
1273 1.1 bjh21
1274 1.1 bjh21 QPRINTF(("{"));
1275 1.1 bjh21 s = splbio();
1276 1.1 bjh21
1277 1.1 bjh21 /*
1278 1.1 bjh21 * have the wd33c93 complete on its own
1279 1.1 bjh21 */
1280 1.22 tsutsui SBIC_TC_PUT(sc, 0);
1281 1.22 tsutsui SET_SBIC_cmd_phase(sc, 0x46);
1282 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_SEL_ATN_XFER);
1283 1.1 bjh21
1284 1.1 bjh21 do {
1285 1.22 tsutsui SBIC_WAIT (sc, SBIC_ASR_INT, 0);
1286 1.22 tsutsui GET_SBIC_csr (sc, csr);
1287 1.1 bjh21 QPRINTF(("%02x:", csr));
1288 1.1 bjh21 } while ((csr != SBIC_CSR_DISC) &&
1289 1.1 bjh21 (csr != SBIC_CSR_DISC_1) &&
1290 1.1 bjh21 (csr != SBIC_CSR_S_XFERRED));
1291 1.1 bjh21
1292 1.22 tsutsui sc->sc_flags &= ~SBICF_SELECTED;
1293 1.22 tsutsui sc->sc_state = SBIC_DISCONNECT;
1294 1.1 bjh21
1295 1.22 tsutsui GET_SBIC_cmd_phase (sc, phase);
1296 1.1 bjh21 QPRINTF(("}%02x", phase));
1297 1.1 bjh21
1298 1.1 bjh21 if (phase == 0x60)
1299 1.22 tsutsui GET_SBIC_tlun(sc, sc->sc_status);
1300 1.1 bjh21 else
1301 1.22 tsutsui wd33c93_error(sc, sc->sc_nexus);
1302 1.1 bjh21
1303 1.22 tsutsui QPRINTF(("=STS:%02x=\n", sc->sc_status));
1304 1.1 bjh21 splx(s);
1305 1.1 bjh21 }
1306 1.1 bjh21
1307 1.1 bjh21
1308 1.1 bjh21 int
1309 1.22 tsutsui wd33c93_go(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
1310 1.1 bjh21 {
1311 1.1 bjh21 struct scsipi_xfer *xs = acb->xs;
1312 1.1 bjh21 int i, dmaok;
1313 1.1 bjh21 u_char csr, asr;
1314 1.1 bjh21
1315 1.22 tsutsui SBIC_DEBUG(ACBS, ("wd33c93_go(%d:%d)\n", sc->target, sc->lun));
1316 1.1 bjh21
1317 1.22 tsutsui sc->sc_nexus = acb;
1318 1.1 bjh21
1319 1.22 tsutsui sc->target = xs->xs_periph->periph_target;
1320 1.22 tsutsui sc->lun = xs->xs_periph->periph_lun;
1321 1.1 bjh21
1322 1.22 tsutsui sc->sc_status = STATUS_UNKNOWN;
1323 1.22 tsutsui sc->sc_daddr = acb->daddr;
1324 1.22 tsutsui sc->sc_dleft = acb->dleft;
1325 1.1 bjh21
1326 1.22 tsutsui sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
1327 1.22 tsutsui sc->sc_flags = 0;
1328 1.1 bjh21
1329 1.22 tsutsui dmaok = wd33c93_dmaok(sc, xs);
1330 1.1 bjh21
1331 1.1 bjh21 if (dmaok == 0)
1332 1.22 tsutsui sc->sc_flags |= SBICF_NODMA;
1333 1.1 bjh21
1334 1.2 bjh21 SBIC_DEBUG(DMA, ("wd33c93_go dmago:%d(tcnt=%zx) dmaok=%dx\n",
1335 1.22 tsutsui sc->target, sc->sc_tcnt, dmaok));
1336 1.1 bjh21
1337 1.1 bjh21 /* select the SCSI bus (it's an error if bus isn't free) */
1338 1.22 tsutsui if ((csr = wd33c93_selectbus(sc, acb)) == 0)
1339 1.1 bjh21 return(0); /* Not done: needs to be rescheduled */
1340 1.1 bjh21
1341 1.1 bjh21 /*
1342 1.1 bjh21 * Lets cycle a while then let the interrupt handler take over.
1343 1.1 bjh21 */
1344 1.22 tsutsui GET_SBIC_asr(sc, asr);
1345 1.1 bjh21 do {
1346 1.1 bjh21 QPRINTF(("go[0x%x] ", csr));
1347 1.1 bjh21
1348 1.1 bjh21 /* Handle the new phase */
1349 1.22 tsutsui i = wd33c93_nextstate(sc, acb, csr, asr);
1350 1.22 tsutsui WAIT_CIP(sc); /* XXX */
1351 1.22 tsutsui if (sc->sc_state == SBIC_CONNECTED) {
1352 1.1 bjh21
1353 1.22 tsutsui GET_SBIC_asr(sc, asr);
1354 1.1 bjh21
1355 1.1 bjh21 if (asr & SBIC_ASR_LCI)
1356 1.1 bjh21 printf("wd33c93_go: LCI asr:%02x csr:%02x\n", asr, csr);
1357 1.1 bjh21
1358 1.1 bjh21 if (asr & SBIC_ASR_INT)
1359 1.22 tsutsui GET_SBIC_csr(sc, csr);
1360 1.1 bjh21 }
1361 1.1 bjh21
1362 1.22 tsutsui } while (sc->sc_state == SBIC_CONNECTED &&
1363 1.1 bjh21 asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
1364 1.1 bjh21
1365 1.22 tsutsui QPRINTF(("> done i=%d stat=%02x\n", i, sc->sc_status));
1366 1.1 bjh21
1367 1.1 bjh21 if (i == SBIC_STATE_DONE) {
1368 1.22 tsutsui if (sc->sc_status == STATUS_UNKNOWN) {
1369 1.1 bjh21 printf("wd33c93_go: done & stat == UNKNOWN\n");
1370 1.1 bjh21 return 1; /* Did we really finish that fast? */
1371 1.1 bjh21 }
1372 1.1 bjh21 }
1373 1.1 bjh21 return 0;
1374 1.1 bjh21 }
1375 1.1 bjh21
1376 1.1 bjh21
1377 1.1 bjh21 int
1378 1.22 tsutsui wd33c93_intr(struct wd33c93_softc *sc)
1379 1.1 bjh21 {
1380 1.1 bjh21 u_char asr, csr;
1381 1.1 bjh21
1382 1.1 bjh21 /*
1383 1.1 bjh21 * pending interrupt?
1384 1.1 bjh21 */
1385 1.22 tsutsui GET_SBIC_asr (sc, asr);
1386 1.1 bjh21 if ((asr & SBIC_ASR_INT) == 0)
1387 1.1 bjh21 return(0);
1388 1.1 bjh21
1389 1.22 tsutsui GET_SBIC_csr(sc, csr);
1390 1.1 bjh21
1391 1.1 bjh21 do {
1392 1.1 bjh21 SBIC_DEBUG(INTS, ("intr[csr=0x%x]", csr));
1393 1.1 bjh21
1394 1.25 christos (void)wd33c93_nextstate(sc, sc->sc_nexus, csr, asr);
1395 1.22 tsutsui WAIT_CIP(sc); /* XXX */
1396 1.22 tsutsui if (sc->sc_state == SBIC_CONNECTED) {
1397 1.22 tsutsui GET_SBIC_asr(sc, asr);
1398 1.1 bjh21
1399 1.1 bjh21 if (asr & SBIC_ASR_LCI)
1400 1.1 bjh21 printf("wd33c93_intr: LCI asr:%02x csr:%02x\n",
1401 1.1 bjh21 asr, csr);
1402 1.1 bjh21
1403 1.1 bjh21 if (asr & SBIC_ASR_INT)
1404 1.22 tsutsui GET_SBIC_csr(sc, csr);
1405 1.1 bjh21 }
1406 1.22 tsutsui } while (sc->sc_state == SBIC_CONNECTED &&
1407 1.1 bjh21 asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
1408 1.1 bjh21
1409 1.1 bjh21 SBIC_DEBUG(INTS, ("intr done. state=%d, asr=0x%02x\n", i, asr));
1410 1.1 bjh21
1411 1.1 bjh21 return(1);
1412 1.1 bjh21 }
1413 1.1 bjh21
1414 1.1 bjh21 /*
1415 1.1 bjh21 * Complete current command using polled I/O. Used when interrupt driven
1416 1.1 bjh21 * I/O is not allowed (ie. during boot and shutdown)
1417 1.1 bjh21 *
1418 1.1 bjh21 * Polled I/O is very processor intensive
1419 1.1 bjh21 */
1420 1.1 bjh21 int
1421 1.22 tsutsui wd33c93_poll(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
1422 1.1 bjh21 {
1423 1.1 bjh21 u_char asr, csr=0;
1424 1.25 christos int count;
1425 1.1 bjh21 struct scsipi_xfer *xs = acb->xs;
1426 1.1 bjh21
1427 1.22 tsutsui SBIC_WAIT(sc, SBIC_ASR_INT, wd33c93_cmd_wait);
1428 1.1 bjh21 for (count=acb->timeout; count;) {
1429 1.22 tsutsui GET_SBIC_asr (sc, asr);
1430 1.1 bjh21 if (asr & SBIC_ASR_LCI)
1431 1.1 bjh21 printf("wd33c93_poll: LCI; asr:%02x csr:%02x\n",
1432 1.1 bjh21 asr, csr);
1433 1.1 bjh21 if (asr & SBIC_ASR_INT) {
1434 1.22 tsutsui GET_SBIC_csr(sc, csr);
1435 1.22 tsutsui sc->sc_flags |= SBICF_NODMA;
1436 1.25 christos (void)wd33c93_nextstate(sc, sc->sc_nexus, csr, asr);
1437 1.22 tsutsui WAIT_CIP(sc); /* XXX */
1438 1.1 bjh21 } else {
1439 1.1 bjh21 DELAY(1000);
1440 1.1 bjh21 count--;
1441 1.1 bjh21 }
1442 1.1 bjh21
1443 1.1 bjh21 if ((xs->xs_status & XS_STS_DONE) != 0)
1444 1.1 bjh21 return (0);
1445 1.1 bjh21
1446 1.22 tsutsui if (sc->sc_state == SBIC_IDLE) {
1447 1.1 bjh21 SBIC_DEBUG(ACBS, ("[poll: rescheduling] "));
1448 1.22 tsutsui wd33c93_sched(sc);
1449 1.1 bjh21 }
1450 1.1 bjh21 }
1451 1.1 bjh21 return (1);
1452 1.1 bjh21 }
1453 1.1 bjh21
1454 1.1 bjh21 static inline int
1455 1.1 bjh21 __verify_msg_format(u_char *p, int len)
1456 1.1 bjh21 {
1457 1.1 bjh21
1458 1.3 bjh21 if (len == 1 && MSG_IS1BYTE(p[0]))
1459 1.1 bjh21 return 1;
1460 1.3 bjh21 if (len == 2 && MSG_IS2BYTE(p[0]))
1461 1.1 bjh21 return 1;
1462 1.3 bjh21 if (len >= 3 && MSG_ISEXTENDED(p[0]) &&
1463 1.1 bjh21 len == p[1] + 2)
1464 1.1 bjh21 return 1;
1465 1.1 bjh21 return 0;
1466 1.1 bjh21 }
1467 1.1 bjh21
1468 1.1 bjh21 /*
1469 1.1 bjh21 * Handle message_in phase
1470 1.1 bjh21 */
1471 1.1 bjh21 int
1472 1.22 tsutsui wd33c93_msgin_phase(struct wd33c93_softc *sc, int reselect)
1473 1.1 bjh21 {
1474 1.1 bjh21 int len;
1475 1.1 bjh21 u_char asr, csr, *msg;
1476 1.1 bjh21
1477 1.22 tsutsui GET_SBIC_asr(sc, asr);
1478 1.25 christos __USE(asr);
1479 1.1 bjh21
1480 1.1 bjh21 SBIC_DEBUG(MSGS, ("wd33c93msgin asr=%02x\n", asr));
1481 1.1 bjh21
1482 1.22 tsutsui GET_SBIC_selid (sc, csr);
1483 1.22 tsutsui SET_SBIC_selid (sc, csr | SBIC_SID_FROM_SCSI);
1484 1.1 bjh21
1485 1.22 tsutsui SBIC_TC_PUT(sc, 0);
1486 1.1 bjh21
1487 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
1488 1.1 bjh21
1489 1.22 tsutsui msg = sc->sc_imsg;
1490 1.1 bjh21 len = 0;
1491 1.1 bjh21
1492 1.1 bjh21 do {
1493 1.1 bjh21 /* Fetch the next byte of the message */
1494 1.22 tsutsui RECV_BYTE(sc, *msg++);
1495 1.1 bjh21 len++;
1496 1.1 bjh21
1497 1.1 bjh21 /*
1498 1.1 bjh21 * get the command completion interrupt, or we
1499 1.1 bjh21 * can't send a new command (LCI)
1500 1.1 bjh21 */
1501 1.22 tsutsui SBIC_WAIT(sc, SBIC_ASR_INT, 0);
1502 1.22 tsutsui GET_SBIC_csr(sc, csr);
1503 1.1 bjh21
1504 1.22 tsutsui if (__verify_msg_format(sc->sc_imsg, len))
1505 1.27 dholland break; /* Complete message received */
1506 1.8 bjh21
1507 1.1 bjh21 /*
1508 1.1 bjh21 * Clear ACK, and wait for the interrupt
1509 1.1 bjh21 * for the next byte or phase change
1510 1.1 bjh21 */
1511 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
1512 1.22 tsutsui SBIC_WAIT(sc, SBIC_ASR_INT, 0);
1513 1.1 bjh21
1514 1.22 tsutsui GET_SBIC_csr(sc, csr);
1515 1.1 bjh21 } while (len < SBIC_MAX_MSGLEN);
1516 1.1 bjh21
1517 1.22 tsutsui if (__verify_msg_format(sc->sc_imsg, len))
1518 1.22 tsutsui wd33c93_msgin(sc, sc->sc_imsg, len);
1519 1.1 bjh21
1520 1.8 bjh21 /*
1521 1.8 bjh21 * Clear ACK, and wait for the interrupt
1522 1.8 bjh21 * for the phase change
1523 1.8 bjh21 */
1524 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
1525 1.22 tsutsui SBIC_WAIT(sc, SBIC_ASR_INT, 0);
1526 1.8 bjh21
1527 1.1 bjh21 /* Should still have one CSR to read */
1528 1.1 bjh21 return SBIC_STATE_RUNNING;
1529 1.1 bjh21 }
1530 1.1 bjh21
1531 1.1 bjh21
1532 1.22 tsutsui void wd33c93_msgin(struct wd33c93_softc *sc, u_char *msgaddr, int msglen)
1533 1.1 bjh21 {
1534 1.22 tsutsui struct wd33c93_acb *acb = sc->sc_nexus;
1535 1.22 tsutsui struct wd33c93_tinfo *ti = &sc->sc_tinfo[sc->target];
1536 1.1 bjh21 struct wd33c93_linfo *li;
1537 1.1 bjh21 u_char asr;
1538 1.1 bjh21
1539 1.22 tsutsui switch (sc->sc_state) {
1540 1.1 bjh21 case SBIC_CONNECTED:
1541 1.1 bjh21 switch (msgaddr[0]) {
1542 1.1 bjh21 case MSG_MESSAGE_REJECT:
1543 1.1 bjh21 SBIC_DEBUG(MSGS, ("msgin: MSG_REJECT, "
1544 1.22 tsutsui "last msgout=%x\n", sc->sc_msgout));
1545 1.22 tsutsui switch (sc->sc_msgout) {
1546 1.1 bjh21 case SEND_TAG:
1547 1.1 bjh21 printf("%s: tagged queuing rejected: "
1548 1.1 bjh21 "target %d\n",
1549 1.22 tsutsui device_xname(sc->sc_dev), sc->target);
1550 1.1 bjh21 ti->flags &= ~T_TAG;
1551 1.22 tsutsui li = TINFO_LUN(ti, sc->lun);
1552 1.1 bjh21 if (acb->tag_type &&
1553 1.1 bjh21 li->queued[acb->tag_id] != NULL) {
1554 1.1 bjh21 li->queued[acb->tag_id] = NULL;
1555 1.1 bjh21 li->used--;
1556 1.1 bjh21 }
1557 1.1 bjh21 acb->tag_type = acb->tag_id = 0;
1558 1.1 bjh21 li->untagged = acb;
1559 1.1 bjh21 li->state = L_STATE_BUSY;
1560 1.1 bjh21 break;
1561 1.1 bjh21
1562 1.1 bjh21 case SEND_SDTR:
1563 1.1 bjh21 printf("%s: sync transfer rejected: target %d\n",
1564 1.22 tsutsui device_xname(sc->sc_dev), sc->target);
1565 1.1 bjh21
1566 1.22 tsutsui sc->sc_flags &= ~SBICF_SYNCNEGO;
1567 1.1 bjh21 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1568 1.22 tsutsui wd33c93_update_xfer_mode(sc,
1569 1.1 bjh21 acb->xs->xs_periph->periph_target);
1570 1.22 tsutsui wd33c93_setsync(sc, ti);
1571 1.1 bjh21
1572 1.1 bjh21 case SEND_INIT_DET_ERR:
1573 1.1 bjh21 goto abort;
1574 1.1 bjh21
1575 1.1 bjh21 default:
1576 1.1 bjh21 SBIC_DEBUG(MSGS, ("Unexpected MSG_REJECT\n"));
1577 1.1 bjh21 break;
1578 1.1 bjh21 }
1579 1.22 tsutsui sc->sc_msgout = 0;
1580 1.1 bjh21 break;
1581 1.1 bjh21
1582 1.1 bjh21 case MSG_HEAD_OF_Q_TAG:
1583 1.1 bjh21 case MSG_ORDERED_Q_TAG:
1584 1.1 bjh21 case MSG_SIMPLE_Q_TAG:
1585 1.1 bjh21 printf("-- Out of phase TAG;"
1586 1.1 bjh21 "Nexus=%d:%d Tag=%02x/%02x\n",
1587 1.22 tsutsui sc->target, sc->lun, msgaddr[0], msgaddr[1]);
1588 1.1 bjh21 break;
1589 1.1 bjh21
1590 1.1 bjh21 case MSG_DISCONNECT:
1591 1.1 bjh21 SBIC_DEBUG(MSGS, ("msgin: DISCONNECT"));
1592 1.1 bjh21 /*
1593 1.1 bjh21 * Mark the fact that all bytes have moved. The
1594 1.1 bjh21 * target may not bother to do a SAVE POINTERS
1595 1.1 bjh21 * at this stage. This flag will set the residual
1596 1.1 bjh21 * count to zero on MSG COMPLETE.
1597 1.1 bjh21 */
1598 1.22 tsutsui if (sc->sc_dleft == 0)
1599 1.1 bjh21 acb->flags |= ACB_COMPLETE;
1600 1.1 bjh21
1601 1.1 bjh21 if (acb->xs->xs_control & XS_CTL_POLL)
1602 1.1 bjh21 /* Don't allow disconnect in immediate mode */
1603 1.1 bjh21 goto reject;
1604 1.1 bjh21 else { /* Allow disconnect */
1605 1.22 tsutsui sc->sc_flags &= ~SBICF_SELECTED;
1606 1.22 tsutsui sc->sc_state = SBIC_DISCONNECT;
1607 1.1 bjh21 }
1608 1.1 bjh21 if ((acb->xs->xs_periph->periph_quirks &
1609 1.1 bjh21 PQUIRK_AUTOSAVE) == 0)
1610 1.1 bjh21 break;
1611 1.1 bjh21 /*FALLTHROUGH*/
1612 1.1 bjh21
1613 1.1 bjh21 case MSG_SAVEDATAPOINTER:
1614 1.1 bjh21 SBIC_DEBUG(MSGS, ("msgin: SAVEDATAPTR"));
1615 1.22 tsutsui acb->daddr = sc->sc_daddr;
1616 1.22 tsutsui acb->dleft = sc->sc_dleft;
1617 1.1 bjh21 break;
1618 1.1 bjh21
1619 1.1 bjh21 case MSG_RESTOREPOINTERS:
1620 1.1 bjh21 SBIC_DEBUG(MSGS, ("msgin: RESTOREPTR"));
1621 1.22 tsutsui sc->sc_daddr = acb->daddr;
1622 1.22 tsutsui sc->sc_dleft = acb->dleft;
1623 1.1 bjh21 break;
1624 1.1 bjh21
1625 1.1 bjh21 case MSG_CMDCOMPLETE:
1626 1.1 bjh21 /*
1627 1.1 bjh21 * !! KLUDGE ALERT !! quite a few drives don't seem to
1628 1.1 bjh21 * really like the current way of sending the
1629 1.1 bjh21 * sync-handshake together with the ident-message, and
1630 1.1 bjh21 * they react by sending command-complete and
1631 1.1 bjh21 * disconnecting right after returning the valid sync
1632 1.1 bjh21 * handshake. So, all I can do is reselect the drive,
1633 1.1 bjh21 * and hope it won't disconnect again. I don't think
1634 1.1 bjh21 * this is valid behavior, but I can't help fixing a
1635 1.1 bjh21 * problem that apparently exists.
1636 1.1 bjh21 *
1637 1.1 bjh21 * Note: we should not get here on `normal' command
1638 1.1 bjh21 * completion, as that condition is handled by the
1639 1.1 bjh21 * high-level sel&xfer resume command used to walk
1640 1.1 bjh21 * thru status/cc-phase.
1641 1.1 bjh21 */
1642 1.1 bjh21 SBIC_DEBUG(MSGS, ("msgin: CMD_COMPLETE"));
1643 1.1 bjh21 SBIC_DEBUG(SYNC, ("GOT MSG %d! target %d"
1644 1.1 bjh21 " acting weird.."
1645 1.1 bjh21 " waiting for disconnect...\n",
1646 1.22 tsutsui msgaddr[0], sc->target));
1647 1.1 bjh21
1648 1.1 bjh21 /* Check to see if wd33c93 is handling this */
1649 1.22 tsutsui GET_SBIC_asr(sc, asr);
1650 1.1 bjh21 if (asr & SBIC_ASR_BSY)
1651 1.1 bjh21 break;
1652 1.1 bjh21
1653 1.1 bjh21 /* XXX: Assume it works and set status to 00 */
1654 1.22 tsutsui sc->sc_status = 0;
1655 1.22 tsutsui sc->sc_state = SBIC_CMDCOMPLETE;
1656 1.1 bjh21 break;
1657 1.1 bjh21
1658 1.1 bjh21 case MSG_EXTENDED:
1659 1.1 bjh21 switch(msgaddr[2]) {
1660 1.1 bjh21 case MSG_EXT_SDTR: /* Sync negotiation */
1661 1.1 bjh21 SBIC_DEBUG(MSGS, ("msgin: EXT_SDTR; "
1662 1.1 bjh21 "period %d, offset %d",
1663 1.1 bjh21 msgaddr[3], msgaddr[4]));
1664 1.1 bjh21 if (msgaddr[1] != 3)
1665 1.1 bjh21 goto reject;
1666 1.1 bjh21
1667 1.8 bjh21 ti->period =
1668 1.22 tsutsui MAX(msgaddr[3], sc->sc_minsyncperiod);
1669 1.22 tsutsui ti->offset = MIN(msgaddr[4], sc->sc_maxoffset);
1670 1.15 rumble
1671 1.15 rumble /*
1672 1.15 rumble * <SGI, IBM DORS-32160, WA6A> will do nothing
1673 1.15 rumble * but attempt sync negotiation until it gets
1674 1.15 rumble * what it wants. To avoid an infinite loop set
1675 1.15 rumble * off by the identify request, oblige them.
1676 1.15 rumble */
1677 1.22 tsutsui if ((sc->sc_flags&SBICF_SYNCNEGO) == 0 &&
1678 1.15 rumble msgaddr[3] != 0)
1679 1.15 rumble ti->flags |= T_WANTSYNC;
1680 1.15 rumble
1681 1.8 bjh21 if (!(ti->flags & T_WANTSYNC))
1682 1.15 rumble ti->period = ti->offset = 0;
1683 1.8 bjh21
1684 1.1 bjh21 ti->flags &= ~T_NEGOTIATE;
1685 1.1 bjh21
1686 1.1 bjh21 if (ti->offset == 0)
1687 1.1 bjh21 ti->flags &= ~T_SYNCMODE; /* Async */
1688 1.8 bjh21 else
1689 1.1 bjh21 ti->flags |= T_SYNCMODE; /* Sync */
1690 1.1 bjh21
1691 1.15 rumble /* target initiated negotiation */
1692 1.22 tsutsui if ((sc->sc_flags&SBICF_SYNCNEGO) == 0)
1693 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_SDTR);
1694 1.22 tsutsui sc->sc_flags &= ~SBICF_SYNCNEGO;
1695 1.1 bjh21
1696 1.1 bjh21 SBIC_DEBUG(SYNC, ("msgin(%d): SDTR(o=%d,p=%d)",
1697 1.22 tsutsui sc->target, ti->offset,
1698 1.1 bjh21 ti->period));
1699 1.22 tsutsui wd33c93_update_xfer_mode(sc,
1700 1.1 bjh21 acb->xs->xs_periph->periph_target);
1701 1.22 tsutsui wd33c93_setsync(sc, ti);
1702 1.1 bjh21 break;
1703 1.1 bjh21
1704 1.1 bjh21 case MSG_EXT_WDTR:
1705 1.8 bjh21 SBIC_DEBUG(MSGS, ("msgin: EXT_WDTR rejected"));
1706 1.8 bjh21 goto reject;
1707 1.1 bjh21
1708 1.1 bjh21 default:
1709 1.1 bjh21 scsipi_printaddr(acb->xs->xs_periph);
1710 1.1 bjh21 printf("unrecognized MESSAGE EXTENDED;"
1711 1.1 bjh21 " sending REJECT\n");
1712 1.1 bjh21 goto reject;
1713 1.1 bjh21 }
1714 1.1 bjh21 break;
1715 1.1 bjh21
1716 1.1 bjh21 default:
1717 1.1 bjh21 scsipi_printaddr(acb->xs->xs_periph);
1718 1.1 bjh21 printf("unrecognized MESSAGE; sending REJECT\n");
1719 1.1 bjh21
1720 1.1 bjh21 reject:
1721 1.1 bjh21 /* We don't support whatever this message is... */
1722 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_REJECT);
1723 1.1 bjh21 break;
1724 1.1 bjh21 }
1725 1.1 bjh21 break;
1726 1.1 bjh21
1727 1.1 bjh21 case SBIC_IDENTIFIED:
1728 1.1 bjh21 /*
1729 1.1 bjh21 * IDENTIFY message was received and queue tag is expected now
1730 1.1 bjh21 */
1731 1.22 tsutsui if ((msgaddr[0]!=MSG_SIMPLE_Q_TAG) || (sc->sc_msgify==0)) {
1732 1.1 bjh21 printf("%s: TAG reselect without IDENTIFY;"
1733 1.1 bjh21 " MSG %x; sending DEVICE RESET\n",
1734 1.22 tsutsui device_xname(sc->sc_dev), msgaddr[0]);
1735 1.1 bjh21 goto reset;
1736 1.1 bjh21 }
1737 1.1 bjh21 SBIC_DEBUG(TAGS, ("TAG %x/%x\n", msgaddr[0], msgaddr[1]));
1738 1.22 tsutsui if (sc->sc_nexus)
1739 1.1 bjh21 printf("*TAG Recv with active nexus!!\n");
1740 1.22 tsutsui wd33c93_reselect(sc, sc->target, sc->lun,
1741 1.1 bjh21 msgaddr[0], msgaddr[1]);
1742 1.1 bjh21 break;
1743 1.1 bjh21
1744 1.1 bjh21 case SBIC_RESELECTED:
1745 1.1 bjh21 /*
1746 1.1 bjh21 * IDENTIFY message with target
1747 1.1 bjh21 */
1748 1.1 bjh21 if (MSG_ISIDENTIFY(msgaddr[0])) {
1749 1.1 bjh21 SBIC_DEBUG(PHASE, ("IFFY[%x] ", msgaddr[0]));
1750 1.22 tsutsui sc->sc_msgify = msgaddr[0];
1751 1.1 bjh21 } else {
1752 1.1 bjh21 printf("%s: reselect without IDENTIFY;"
1753 1.1 bjh21 " MSG %x;"
1754 1.1 bjh21 " sending DEVICE RESET\n",
1755 1.22 tsutsui device_xname(sc->sc_dev), msgaddr[0]);
1756 1.1 bjh21 goto reset;
1757 1.1 bjh21 }
1758 1.1 bjh21 break;
1759 1.1 bjh21
1760 1.1 bjh21 default:
1761 1.1 bjh21 printf("Unexpected MESSAGE IN. State=%d - Sending RESET\n",
1762 1.22 tsutsui sc->sc_state);
1763 1.1 bjh21 reset:
1764 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_DEV_RESET);
1765 1.1 bjh21 break;
1766 1.1 bjh21 abort:
1767 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_ABORT);
1768 1.1 bjh21 break;
1769 1.1 bjh21 }
1770 1.1 bjh21 }
1771 1.1 bjh21
1772 1.1 bjh21 void
1773 1.22 tsutsui wd33c93_sched_msgout(struct wd33c93_softc *sc, u_short msg)
1774 1.1 bjh21 {
1775 1.1 bjh21 u_char asr;
1776 1.1 bjh21
1777 1.1 bjh21 SBIC_DEBUG(SYNC,("sched_msgout: %04x\n", msg));
1778 1.22 tsutsui sc->sc_msgpriq |= msg;
1779 1.1 bjh21
1780 1.1 bjh21 /* Schedule MSGOUT Phase to send message */
1781 1.1 bjh21
1782 1.22 tsutsui WAIT_CIP(sc);
1783 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_SET_ATN);
1784 1.22 tsutsui WAIT_CIP(sc);
1785 1.22 tsutsui GET_SBIC_asr(sc, asr);
1786 1.1 bjh21 if (asr & SBIC_ASR_LCI) {
1787 1.1 bjh21 printf("MSGOUT Failed!\n");
1788 1.1 bjh21 }
1789 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
1790 1.22 tsutsui WAIT_CIP(sc);
1791 1.1 bjh21 }
1792 1.1 bjh21
1793 1.1 bjh21 /*
1794 1.1 bjh21 * Send the highest priority, scheduled message
1795 1.1 bjh21 */
1796 1.1 bjh21 void
1797 1.22 tsutsui wd33c93_msgout(struct wd33c93_softc *sc)
1798 1.1 bjh21 {
1799 1.1 bjh21 struct wd33c93_tinfo *ti;
1800 1.22 tsutsui struct wd33c93_acb *acb = sc->sc_nexus;
1801 1.1 bjh21
1802 1.1 bjh21 if (acb == NULL)
1803 1.1 bjh21 panic("MSGOUT with no nexus");
1804 1.1 bjh21
1805 1.22 tsutsui if (sc->sc_omsglen == 0) {
1806 1.1 bjh21 /* Pick up highest priority message */
1807 1.22 tsutsui sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
1808 1.22 tsutsui sc->sc_msgoutq |= sc->sc_msgout;
1809 1.22 tsutsui sc->sc_msgpriq &= ~sc->sc_msgout;
1810 1.22 tsutsui sc->sc_omsglen = 1; /* "Default" message len */
1811 1.22 tsutsui switch (sc->sc_msgout) {
1812 1.1 bjh21 case SEND_SDTR:
1813 1.22 tsutsui ti = &sc->sc_tinfo[acb->xs->xs_periph->periph_target];
1814 1.22 tsutsui sc->sc_omsg[0] = MSG_EXTENDED;
1815 1.22 tsutsui sc->sc_omsg[1] = MSG_EXT_SDTR_LEN;
1816 1.22 tsutsui sc->sc_omsg[2] = MSG_EXT_SDTR;
1817 1.8 bjh21 if (ti->flags & T_WANTSYNC) {
1818 1.22 tsutsui sc->sc_omsg[3] = ti->period;
1819 1.22 tsutsui sc->sc_omsg[4] = ti->offset;
1820 1.8 bjh21 } else {
1821 1.22 tsutsui sc->sc_omsg[3] = 0;
1822 1.22 tsutsui sc->sc_omsg[4] = 0;
1823 1.8 bjh21 }
1824 1.22 tsutsui sc->sc_omsglen = 5;
1825 1.22 tsutsui if ((sc->sc_flags & SBICF_SYNCNEGO) == 0) {
1826 1.8 bjh21 if (ti->flags & T_WANTSYNC)
1827 1.8 bjh21 ti->flags |= T_SYNCMODE;
1828 1.8 bjh21 else
1829 1.8 bjh21 ti->flags &= ~T_SYNCMODE;
1830 1.22 tsutsui wd33c93_setsync(sc, ti);
1831 1.1 bjh21 }
1832 1.1 bjh21 break;
1833 1.1 bjh21 case SEND_IDENTIFY:
1834 1.22 tsutsui if (sc->sc_state != SBIC_CONNECTED) {
1835 1.1 bjh21 printf("%s at line %d: no nexus\n",
1836 1.22 tsutsui device_xname(sc->sc_dev), __LINE__);
1837 1.1 bjh21 }
1838 1.22 tsutsui sc->sc_omsg[0] =
1839 1.1 bjh21 MSG_IDENTIFY(acb->xs->xs_periph->periph_lun, 0);
1840 1.1 bjh21 break;
1841 1.1 bjh21 case SEND_TAG:
1842 1.22 tsutsui if (sc->sc_state != SBIC_CONNECTED) {
1843 1.1 bjh21 printf("%s at line %d: no nexus\n",
1844 1.22 tsutsui device_xname(sc->sc_dev), __LINE__);
1845 1.1 bjh21 }
1846 1.22 tsutsui sc->sc_omsg[0] = acb->tag_type;
1847 1.22 tsutsui sc->sc_omsg[1] = acb->tag_id;
1848 1.22 tsutsui sc->sc_omsglen = 2;
1849 1.1 bjh21 break;
1850 1.1 bjh21 case SEND_DEV_RESET:
1851 1.22 tsutsui sc->sc_omsg[0] = MSG_BUS_DEV_RESET;
1852 1.22 tsutsui ti = &sc->sc_tinfo[sc->target];
1853 1.1 bjh21 ti->flags &= ~T_SYNCMODE;
1854 1.22 tsutsui wd33c93_update_xfer_mode(sc, sc->target);
1855 1.1 bjh21 if ((ti->flags & T_NOSYNC) == 0)
1856 1.1 bjh21 /* We can re-start sync negotiation */
1857 1.1 bjh21 ti->flags |= T_NEGOTIATE;
1858 1.1 bjh21 break;
1859 1.1 bjh21 case SEND_PARITY_ERROR:
1860 1.22 tsutsui sc->sc_omsg[0] = MSG_PARITY_ERROR;
1861 1.1 bjh21 break;
1862 1.1 bjh21 case SEND_ABORT:
1863 1.22 tsutsui sc->sc_flags |= SBICF_ABORTING;
1864 1.22 tsutsui sc->sc_omsg[0] = MSG_ABORT;
1865 1.1 bjh21 break;
1866 1.1 bjh21 case SEND_INIT_DET_ERR:
1867 1.22 tsutsui sc->sc_omsg[0] = MSG_INITIATOR_DET_ERR;
1868 1.1 bjh21 break;
1869 1.1 bjh21 case SEND_REJECT:
1870 1.22 tsutsui sc->sc_omsg[0] = MSG_MESSAGE_REJECT;
1871 1.1 bjh21 break;
1872 1.1 bjh21 default:
1873 1.1 bjh21 /* Wasn't expecting MSGOUT Phase */
1874 1.22 tsutsui sc->sc_omsg[0] = MSG_NOOP;
1875 1.1 bjh21 break;
1876 1.1 bjh21 }
1877 1.1 bjh21 }
1878 1.1 bjh21
1879 1.22 tsutsui wd33c93_xfout(sc, sc->sc_omsglen, sc->sc_omsg);
1880 1.1 bjh21 }
1881 1.1 bjh21
1882 1.1 bjh21
1883 1.1 bjh21 /*
1884 1.1 bjh21 * wd33c93_nextstate()
1885 1.1 bjh21 * return:
1886 1.1 bjh21 * SBIC_STATE_DONE == done
1887 1.1 bjh21 * SBIC_STATE_RUNNING == working
1888 1.1 bjh21 * SBIC_STATE_DISCONNECT == disconnected
1889 1.1 bjh21 * SBIC_STATE_ERROR == error
1890 1.1 bjh21 */
1891 1.1 bjh21 int
1892 1.22 tsutsui wd33c93_nextstate(struct wd33c93_softc *sc, struct wd33c93_acb *acb, u_char csr, u_char asr)
1893 1.1 bjh21 {
1894 1.1 bjh21 SBIC_DEBUG(PHASE, ("next[a=%02x,c=%02x]: ",asr,csr));
1895 1.1 bjh21
1896 1.1 bjh21 switch (csr) {
1897 1.1 bjh21
1898 1.1 bjh21 case SBIC_CSR_XFERRED | CMD_PHASE:
1899 1.1 bjh21 case SBIC_CSR_MIS | CMD_PHASE:
1900 1.1 bjh21 case SBIC_CSR_MIS_1 | CMD_PHASE:
1901 1.1 bjh21 case SBIC_CSR_MIS_2 | CMD_PHASE:
1902 1.1 bjh21
1903 1.22 tsutsui if (wd33c93_xfout(sc, acb->clen, &acb->cmd))
1904 1.1 bjh21 goto abort;
1905 1.1 bjh21 break;
1906 1.1 bjh21
1907 1.1 bjh21 case SBIC_CSR_XFERRED | STATUS_PHASE:
1908 1.1 bjh21 case SBIC_CSR_MIS | STATUS_PHASE:
1909 1.1 bjh21 case SBIC_CSR_MIS_1 | STATUS_PHASE:
1910 1.1 bjh21 case SBIC_CSR_MIS_2 | STATUS_PHASE:
1911 1.1 bjh21
1912 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
1913 1.1 bjh21
1914 1.1 bjh21 /*
1915 1.1 bjh21 * this should be the normal i/o completion case.
1916 1.1 bjh21 * get the status & cmd complete msg then let the
1917 1.1 bjh21 * device driver look at what happened.
1918 1.1 bjh21 */
1919 1.22 tsutsui wd33c93_xferdone(sc);
1920 1.1 bjh21
1921 1.22 tsutsui wd33c93_dma_stop(sc);
1922 1.1 bjh21
1923 1.1 bjh21 /* Fixup byte count to be passed to higher layer */
1924 1.1 bjh21 acb->dleft = (acb->flags & ACB_COMPLETE) ? 0 :
1925 1.22 tsutsui sc->sc_dleft;
1926 1.1 bjh21
1927 1.1 bjh21 /*
1928 1.1 bjh21 * Indicate to the upper layers that the command is done
1929 1.1 bjh21 */
1930 1.22 tsutsui wd33c93_scsidone(sc, acb, sc->sc_status);
1931 1.1 bjh21
1932 1.1 bjh21 return SBIC_STATE_DONE;
1933 1.1 bjh21
1934 1.1 bjh21
1935 1.1 bjh21 case SBIC_CSR_XFERRED | DATA_IN_PHASE:
1936 1.1 bjh21 case SBIC_CSR_MIS | DATA_IN_PHASE:
1937 1.1 bjh21 case SBIC_CSR_MIS_1 | DATA_IN_PHASE:
1938 1.1 bjh21 case SBIC_CSR_MIS_2 | DATA_IN_PHASE:
1939 1.1 bjh21 case SBIC_CSR_XFERRED | DATA_OUT_PHASE:
1940 1.1 bjh21 case SBIC_CSR_MIS | DATA_OUT_PHASE:
1941 1.1 bjh21 case SBIC_CSR_MIS_1 | DATA_OUT_PHASE:
1942 1.1 bjh21 case SBIC_CSR_MIS_2 | DATA_OUT_PHASE:
1943 1.1 bjh21 /*
1944 1.1 bjh21 * Verify that we expected to transfer data...
1945 1.1 bjh21 */
1946 1.1 bjh21 if (acb->dleft <= 0) {
1947 1.2 bjh21 printf("next: DATA phase with xfer count == %zd, asr:0x%02x csr:0x%02x\n",
1948 1.1 bjh21 acb->dleft, asr, csr);
1949 1.1 bjh21 goto abort;
1950 1.1 bjh21 }
1951 1.1 bjh21
1952 1.1 bjh21 /*
1953 1.1 bjh21 * Should we transfer using PIO or DMA ?
1954 1.1 bjh21 */
1955 1.1 bjh21 if (acb->xs->xs_control & XS_CTL_POLL ||
1956 1.22 tsutsui sc->sc_flags & SBICF_NODMA) {
1957 1.1 bjh21 /* Perfrom transfer using PIO */
1958 1.1 bjh21 int resid;
1959 1.1 bjh21
1960 1.22 tsutsui SBIC_DEBUG(DMA, ("PIO xfer: %d(%p:%zx)\n", sc->target,
1961 1.22 tsutsui sc->sc_daddr, sc->sc_dleft));
1962 1.1 bjh21
1963 1.1 bjh21 if (SBIC_PHASE(csr) == DATA_IN_PHASE)
1964 1.1 bjh21 /* data in */
1965 1.22 tsutsui resid = wd33c93_xfin(sc, sc->sc_dleft,
1966 1.22 tsutsui sc->sc_daddr);
1967 1.1 bjh21 else /* data out */
1968 1.22 tsutsui resid = wd33c93_xfout(sc, sc->sc_dleft,
1969 1.22 tsutsui sc->sc_daddr);
1970 1.1 bjh21
1971 1.22 tsutsui sc->sc_daddr = (char *)sc->sc_daddr +
1972 1.10 he (acb->dleft - resid);
1973 1.22 tsutsui sc->sc_dleft = resid;
1974 1.1 bjh21 } else {
1975 1.1 bjh21 int datain = SBIC_PHASE(csr) == DATA_IN_PHASE;
1976 1.1 bjh21
1977 1.1 bjh21 /* Perform transfer using DMA */
1978 1.22 tsutsui wd33c93_dma_setup(sc, datain);
1979 1.1 bjh21
1980 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI |
1981 1.22 tsutsui sc->sc_dmamode);
1982 1.1 bjh21
1983 1.22 tsutsui SBIC_DEBUG(DMA, ("DMA xfer: %d(%p:%zx)\n", sc->target,
1984 1.22 tsutsui sc->sc_daddr, sc->sc_dleft));
1985 1.1 bjh21
1986 1.1 bjh21 /* Setup byte count for transfer */
1987 1.22 tsutsui SBIC_TC_PUT(sc, (unsigned)sc->sc_dleft);
1988 1.1 bjh21
1989 1.1 bjh21 /* Start the transfer */
1990 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_XFER_INFO);
1991 1.1 bjh21
1992 1.1 bjh21 /* Start the DMA chip going */
1993 1.22 tsutsui sc->sc_tcnt = sc->sc_dmago(sc);
1994 1.1 bjh21
1995 1.1 bjh21 /* Indicate that we're in DMA mode */
1996 1.22 tsutsui sc->sc_flags |= SBICF_INDMA;
1997 1.1 bjh21 }
1998 1.1 bjh21 break;
1999 1.1 bjh21
2000 1.1 bjh21 case SBIC_CSR_XFERRED | MESG_IN_PHASE:
2001 1.1 bjh21 case SBIC_CSR_MIS | MESG_IN_PHASE:
2002 1.1 bjh21 case SBIC_CSR_MIS_1 | MESG_IN_PHASE:
2003 1.1 bjh21 case SBIC_CSR_MIS_2 | MESG_IN_PHASE:
2004 1.1 bjh21
2005 1.22 tsutsui wd33c93_dma_stop(sc);
2006 1.1 bjh21
2007 1.1 bjh21 /* Handle a single message in... */
2008 1.22 tsutsui return wd33c93_msgin_phase(sc, 0);
2009 1.1 bjh21
2010 1.1 bjh21 case SBIC_CSR_MSGIN_W_ACK:
2011 1.1 bjh21
2012 1.1 bjh21 /*
2013 1.1 bjh21 * We should never see this since it's handled in
2014 1.1 bjh21 * 'wd33c93_msgin_phase()' but just for the sake of paranoia...
2015 1.1 bjh21 */
2016 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
2017 1.1 bjh21
2018 1.1 bjh21 printf("Acking unknown msgin CSR:%02x",csr);
2019 1.1 bjh21 break;
2020 1.1 bjh21
2021 1.1 bjh21 case SBIC_CSR_XFERRED | MESG_OUT_PHASE:
2022 1.1 bjh21 case SBIC_CSR_MIS | MESG_OUT_PHASE:
2023 1.1 bjh21 case SBIC_CSR_MIS_1 | MESG_OUT_PHASE:
2024 1.1 bjh21 case SBIC_CSR_MIS_2 | MESG_OUT_PHASE:
2025 1.1 bjh21
2026 1.1 bjh21 /*
2027 1.1 bjh21 * Message out phase. ATN signal has been asserted
2028 1.1 bjh21 */
2029 1.22 tsutsui wd33c93_dma_stop(sc);
2030 1.22 tsutsui wd33c93_msgout(sc);
2031 1.1 bjh21 return SBIC_STATE_RUNNING;
2032 1.1 bjh21
2033 1.1 bjh21 case SBIC_CSR_DISC:
2034 1.1 bjh21 case SBIC_CSR_DISC_1:
2035 1.1 bjh21 SBIC_DEBUG(RSEL, ("wd33c93next target %d disconnected\n",
2036 1.22 tsutsui sc->target));
2037 1.22 tsutsui wd33c93_dma_stop(sc);
2038 1.1 bjh21
2039 1.22 tsutsui sc->sc_nexus = NULL;
2040 1.22 tsutsui sc->sc_state = SBIC_IDLE;
2041 1.22 tsutsui sc->sc_flags = 0;
2042 1.1 bjh21
2043 1.22 tsutsui ++sc->sc_tinfo[sc->target].dconns;
2044 1.22 tsutsui ++sc->sc_disc;
2045 1.1 bjh21
2046 1.1 bjh21 if (acb->xs->xs_control & XS_CTL_POLL || wd33c93_nodisc)
2047 1.1 bjh21 return SBIC_STATE_DISCONNECT;
2048 1.1 bjh21
2049 1.1 bjh21 /* Try to schedule another target */
2050 1.22 tsutsui wd33c93_sched(sc);
2051 1.1 bjh21
2052 1.1 bjh21 return SBIC_STATE_DISCONNECT;
2053 1.1 bjh21
2054 1.1 bjh21 case SBIC_CSR_RSLT_NI:
2055 1.1 bjh21 case SBIC_CSR_RSLT_IFY:
2056 1.1 bjh21 {
2057 1.1 bjh21 /*
2058 1.1 bjh21 * A reselection.
2059 1.1 bjh21 * Note that since we don't enable Advanced Features (assuming
2060 1.1 bjh21 * the WD chip is at least the 'A' revision), we're only ever
2061 1.1 bjh21 * likely to see the 'SBIC_CSR_RSLT_NI' status. But for the
2062 1.1 bjh21 * hell of it, we'll handle it anyway, for all the extra code
2063 1.1 bjh21 * it needs...
2064 1.1 bjh21 */
2065 1.1 bjh21 u_char newtarget, newlun;
2066 1.1 bjh21
2067 1.22 tsutsui if (sc->sc_flags & SBICF_INDMA) {
2068 1.1 bjh21 printf("**** RESELECT WHILE DMA ACTIVE!!! ***\n");
2069 1.22 tsutsui wd33c93_dma_stop(sc);
2070 1.1 bjh21 }
2071 1.1 bjh21
2072 1.22 tsutsui sc->sc_state = SBIC_RESELECTED;
2073 1.22 tsutsui GET_SBIC_rselid(sc, newtarget);
2074 1.1 bjh21
2075 1.1 bjh21 /* check SBIC_RID_SIV? */
2076 1.1 bjh21 newtarget &= SBIC_RID_MASK;
2077 1.1 bjh21
2078 1.1 bjh21 if (csr == SBIC_CSR_RSLT_IFY) {
2079 1.1 bjh21 /* Read Identify msg to avoid lockup */
2080 1.22 tsutsui GET_SBIC_data(sc, newlun);
2081 1.22 tsutsui WAIT_CIP(sc);
2082 1.1 bjh21 newlun &= SBIC_TLUN_MASK;
2083 1.22 tsutsui sc->sc_msgify = MSG_IDENTIFY(newlun, 0);
2084 1.1 bjh21 } else {
2085 1.1 bjh21 /*
2086 1.1 bjh21 * Need to read Identify message the hard way, assuming
2087 1.1 bjh21 * the target even sends us one...
2088 1.1 bjh21 */
2089 1.1 bjh21 for (newlun = 255; newlun; --newlun) {
2090 1.22 tsutsui GET_SBIC_asr(sc, asr);
2091 1.1 bjh21 if (asr & SBIC_ASR_INT)
2092 1.1 bjh21 break;
2093 1.1 bjh21 DELAY(10);
2094 1.1 bjh21 }
2095 1.1 bjh21
2096 1.1 bjh21 /* If we didn't get an interrupt, somethink's up */
2097 1.1 bjh21 if ((asr & SBIC_ASR_INT) == 0) {
2098 1.1 bjh21 printf("%s: Reselect without identify? asr %x\n",
2099 1.22 tsutsui device_xname(sc->sc_dev), asr);
2100 1.1 bjh21 newlun = 0; /* XXXX */
2101 1.1 bjh21 } else {
2102 1.1 bjh21 /*
2103 1.1 bjh21 * We got an interrupt, verify that it's a
2104 1.1 bjh21 * change to message in phase, and if so
2105 1.1 bjh21 * read the message.
2106 1.1 bjh21 */
2107 1.22 tsutsui GET_SBIC_csr(sc,csr);
2108 1.1 bjh21
2109 1.1 bjh21 if (csr == (SBIC_CSR_MIS | MESG_IN_PHASE) ||
2110 1.1 bjh21 csr == (SBIC_CSR_MIS_1 | MESG_IN_PHASE) ||
2111 1.1 bjh21 csr == (SBIC_CSR_MIS_2 | MESG_IN_PHASE)) {
2112 1.1 bjh21 /*
2113 1.1 bjh21 * Yup, gone to message in.
2114 1.1 bjh21 * Fetch the target LUN
2115 1.1 bjh21 */
2116 1.22 tsutsui sc->sc_msgify = 0;
2117 1.22 tsutsui wd33c93_msgin_phase(sc, 1);
2118 1.22 tsutsui newlun = sc->sc_msgify & SBIC_TLUN_MASK;
2119 1.1 bjh21 } else {
2120 1.1 bjh21 /*
2121 1.1 bjh21 * Whoops! Target didn't go to msg_in
2122 1.1 bjh21 * phase!!
2123 1.1 bjh21 */
2124 1.1 bjh21 printf("RSLT_NI - not MESG_IN_PHASE %x\n", csr);
2125 1.1 bjh21 newlun = 0; /* XXXSCW */
2126 1.1 bjh21 }
2127 1.1 bjh21 }
2128 1.1 bjh21 }
2129 1.1 bjh21
2130 1.1 bjh21 /* Ok, we have the identity of the reselecting target. */
2131 1.1 bjh21 SBIC_DEBUG(RSEL, ("wd33c93next: reselect from targ %d lun %d",
2132 1.1 bjh21 newtarget, newlun));
2133 1.22 tsutsui wd33c93_reselect(sc, newtarget, newlun, 0, 0);
2134 1.22 tsutsui sc->sc_disc--;
2135 1.1 bjh21
2136 1.1 bjh21 if (csr == SBIC_CSR_RSLT_IFY)
2137 1.22 tsutsui SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
2138 1.1 bjh21 break;
2139 1.1 bjh21 }
2140 1.1 bjh21
2141 1.1 bjh21 default:
2142 1.1 bjh21 abort:
2143 1.1 bjh21 /* Something unexpected happend -- deal with it. */
2144 1.1 bjh21 printf("next: aborting asr 0x%02x csr 0x%02x\n", asr, csr);
2145 1.1 bjh21
2146 1.1 bjh21 #ifdef DDB
2147 1.1 bjh21 Debugger();
2148 1.1 bjh21 #endif
2149 1.1 bjh21
2150 1.22 tsutsui SET_SBIC_control(sc, SBIC_CTL_EDI | SBIC_CTL_IDI);
2151 1.1 bjh21 if (acb->xs)
2152 1.22 tsutsui wd33c93_error(sc, acb);
2153 1.22 tsutsui wd33c93_abort(sc, acb, "next");
2154 1.1 bjh21
2155 1.22 tsutsui if (sc->sc_flags & SBICF_INDMA) {
2156 1.22 tsutsui wd33c93_dma_stop(sc);
2157 1.22 tsutsui wd33c93_scsidone(sc, acb, STATUS_UNKNOWN);
2158 1.1 bjh21 }
2159 1.1 bjh21 return SBIC_STATE_ERROR;
2160 1.1 bjh21 }
2161 1.1 bjh21 return SBIC_STATE_RUNNING;
2162 1.1 bjh21 }
2163 1.1 bjh21
2164 1.1 bjh21
2165 1.1 bjh21 void
2166 1.22 tsutsui wd33c93_reselect(struct wd33c93_softc *sc, int target, int lun, int tag_type, int tag_id)
2167 1.1 bjh21 {
2168 1.1 bjh21
2169 1.1 bjh21 struct wd33c93_tinfo *ti;
2170 1.1 bjh21 struct wd33c93_linfo *li;
2171 1.1 bjh21 struct wd33c93_acb *acb;
2172 1.1 bjh21
2173 1.22 tsutsui if (sc->sc_nexus) {
2174 1.1 bjh21 /*
2175 1.1 bjh21 * Whoops! We've been reselected with a
2176 1.1 bjh21 * command in progress!
2177 1.1 bjh21 * The best we can do is to put the current
2178 1.1 bjh21 * command back on the ready list and hope
2179 1.1 bjh21 * for the best.
2180 1.1 bjh21 */
2181 1.1 bjh21 SBIC_DEBUG(RSEL, ("%s: reselect with active command\n",
2182 1.22 tsutsui device_xname(sc->sc_dev)));
2183 1.22 tsutsui ti = &sc->sc_tinfo[sc->target];
2184 1.22 tsutsui li = TINFO_LUN(ti, sc->lun);
2185 1.1 bjh21 li->state = L_STATE_IDLE;
2186 1.1 bjh21
2187 1.22 tsutsui wd33c93_dequeue(sc, sc->sc_nexus);
2188 1.22 tsutsui TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain);
2189 1.22 tsutsui sc->sc_nexus->flags |= ACB_READY;
2190 1.1 bjh21
2191 1.22 tsutsui sc->sc_nexus = NULL;
2192 1.1 bjh21 }
2193 1.1 bjh21
2194 1.1 bjh21 /* Setup state for new nexus */
2195 1.1 bjh21 acb = NULL;
2196 1.22 tsutsui sc->sc_flags = SBICF_SELECTED;
2197 1.22 tsutsui sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
2198 1.1 bjh21
2199 1.22 tsutsui ti = &sc->sc_tinfo[target];
2200 1.1 bjh21 li = TINFO_LUN(ti, lun);
2201 1.1 bjh21
2202 1.1 bjh21 if (li != NULL) {
2203 1.1 bjh21 if (li->untagged != NULL && li->state)
2204 1.1 bjh21 acb = li->untagged;
2205 1.1 bjh21 else if (tag_type != MSG_SIMPLE_Q_TAG) {
2206 1.1 bjh21 /* Wait for tag to come by during MESG_IN Phase */
2207 1.22 tsutsui sc->target = target; /* setup I_T_L nexus */
2208 1.22 tsutsui sc->lun = lun;
2209 1.22 tsutsui sc->sc_state = SBIC_IDENTIFIED;
2210 1.1 bjh21 return;
2211 1.1 bjh21 } else if (tag_type)
2212 1.1 bjh21 acb = li->queued[tag_id];
2213 1.1 bjh21 }
2214 1.1 bjh21
2215 1.1 bjh21 if (acb == NULL) {
2216 1.1 bjh21 printf("%s: reselect from target %d lun %d tag %x:%x "
2217 1.1 bjh21 "with no nexus; sending ABORT\n",
2218 1.22 tsutsui device_xname(sc->sc_dev), target, lun, tag_type, tag_id);
2219 1.1 bjh21 goto abort;
2220 1.1 bjh21 }
2221 1.1 bjh21
2222 1.22 tsutsui sc->target = target;
2223 1.22 tsutsui sc->lun = lun;
2224 1.22 tsutsui sc->sc_nexus = acb;
2225 1.22 tsutsui sc->sc_state = SBIC_CONNECTED;
2226 1.1 bjh21
2227 1.22 tsutsui if (!wd33c93_dmaok(sc, acb->xs))
2228 1.22 tsutsui sc->sc_flags |= SBICF_NODMA;
2229 1.8 bjh21
2230 1.1 bjh21 /* Do an implicit RESTORE POINTERS. */
2231 1.22 tsutsui sc->sc_daddr = acb->daddr;
2232 1.22 tsutsui sc->sc_dleft = acb->dleft;
2233 1.1 bjh21
2234 1.1 bjh21 /* Set sync modes for new target */
2235 1.22 tsutsui wd33c93_setsync(sc, ti);
2236 1.1 bjh21
2237 1.1 bjh21 if (acb->flags & ACB_RESET)
2238 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_DEV_RESET);
2239 1.1 bjh21 else if (acb->flags & ACB_ABORT)
2240 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_ABORT);
2241 1.1 bjh21 return;
2242 1.1 bjh21
2243 1.1 bjh21 abort:
2244 1.22 tsutsui wd33c93_sched_msgout(sc, SEND_ABORT);
2245 1.1 bjh21 return;
2246 1.1 bjh21
2247 1.1 bjh21 }
2248 1.1 bjh21
2249 1.1 bjh21 void
2250 1.1 bjh21 wd33c93_update_xfer_mode(struct wd33c93_softc *sc, int target)
2251 1.1 bjh21 {
2252 1.1 bjh21 struct wd33c93_tinfo *ti = &sc->sc_tinfo[target];
2253 1.1 bjh21 struct scsipi_xfer_mode xm;
2254 1.1 bjh21
2255 1.1 bjh21 xm.xm_target = target;
2256 1.1 bjh21 xm.xm_mode = 0;
2257 1.1 bjh21 xm.xm_period = 0;
2258 1.1 bjh21 xm.xm_offset = 0;
2259 1.1 bjh21
2260 1.1 bjh21 if (ti->flags & T_SYNCMODE) {
2261 1.1 bjh21 xm.xm_mode |= PERIPH_CAP_SYNC;
2262 1.1 bjh21 xm.xm_period = ti->period;
2263 1.1 bjh21 xm.xm_offset = ti->offset;
2264 1.1 bjh21 }
2265 1.1 bjh21
2266 1.1 bjh21 if ((ti->flags & (T_NODISC|T_TAG)) == T_TAG)
2267 1.1 bjh21 xm.xm_mode |= PERIPH_CAP_TQING;
2268 1.1 bjh21
2269 1.8 bjh21 SBIC_DEBUG(SYNC, ("wd33c93_update_xfer_mode: reporting target %d %s\n",
2270 1.8 bjh21 xm.xm_target,
2271 1.8 bjh21 (xm.xm_mode & PERIPH_CAP_SYNC) ? "sync" : "async"));
2272 1.8 bjh21
2273 1.1 bjh21 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
2274 1.1 bjh21 }
2275 1.1 bjh21
2276 1.1 bjh21 void
2277 1.1 bjh21 wd33c93_timeout(void *arg)
2278 1.1 bjh21 {
2279 1.1 bjh21 struct wd33c93_acb *acb = arg;
2280 1.1 bjh21 struct scsipi_xfer *xs = acb->xs;
2281 1.1 bjh21 struct scsipi_periph *periph = xs->xs_periph;
2282 1.22 tsutsui struct wd33c93_softc *sc =
2283 1.21 bjh21 device_private(periph->periph_channel->chan_adapter->adapt_dev);
2284 1.1 bjh21 int s, asr;
2285 1.1 bjh21
2286 1.1 bjh21 s = splbio();
2287 1.1 bjh21
2288 1.22 tsutsui GET_SBIC_asr(sc, asr);
2289 1.1 bjh21
2290 1.1 bjh21 scsipi_printaddr(periph);
2291 1.2 bjh21 printf("%s: timed out; asr=0x%02x [acb %p (flags 0x%x, dleft %zx)], "
2292 1.1 bjh21 "<state %d, nexus %p, resid %lx, msg(q %x,o %x)>",
2293 1.22 tsutsui device_xname(sc->sc_dev), asr, acb, acb->flags, acb->dleft,
2294 1.22 tsutsui sc->sc_state, sc->sc_nexus, (long)sc->sc_dleft,
2295 1.22 tsutsui sc->sc_msgpriq, sc->sc_msgout);
2296 1.1 bjh21
2297 1.1 bjh21 if (asr & SBIC_ASR_INT) {
2298 1.1 bjh21 /* We need to service a missed IRQ */
2299 1.22 tsutsui wd33c93_intr(sc);
2300 1.1 bjh21 } else {
2301 1.22 tsutsui (void) wd33c93_abort(sc, sc->sc_nexus, "timeout");
2302 1.1 bjh21 }
2303 1.1 bjh21 splx(s);
2304 1.1 bjh21 }
2305 1.1 bjh21
2306 1.1 bjh21
2307 1.1 bjh21 void
2308 1.1 bjh21 wd33c93_watchdog(void *arg)
2309 1.1 bjh21 {
2310 1.22 tsutsui struct wd33c93_softc *sc = arg;
2311 1.1 bjh21 struct wd33c93_tinfo *ti;
2312 1.1 bjh21 struct wd33c93_linfo *li;
2313 1.1 bjh21 int t, s, l;
2314 1.1 bjh21 /* scrub LUN's that have not been used in the last 10min. */
2315 1.7 rumble time_t old = time_second - (10 * 60);
2316 1.1 bjh21
2317 1.1 bjh21 for (t = 0; t < SBIC_NTARG; t++) {
2318 1.22 tsutsui ti = &sc->sc_tinfo[t];
2319 1.1 bjh21 for (l = 0; l < SBIC_NLUN; l++) {
2320 1.1 bjh21 s = splbio();
2321 1.1 bjh21 li = TINFO_LUN(ti, l);
2322 1.1 bjh21 if (li && li->last_used < old &&
2323 1.1 bjh21 li->untagged == NULL && li->used == 0) {
2324 1.1 bjh21 ti->lun[li->lun] = NULL;
2325 1.1 bjh21 free(li, M_DEVBUF);
2326 1.1 bjh21 }
2327 1.1 bjh21 splx(s);
2328 1.1 bjh21 }
2329 1.1 bjh21 }
2330 1.22 tsutsui callout_reset(&sc->sc_watchdog, 60 * hz, wd33c93_watchdog, sc);
2331 1.1 bjh21 }
2332 1.1 bjh21
2333 1.1 bjh21
2334 1.1 bjh21 #ifdef DEBUG
2335 1.1 bjh21 void
2336 1.1 bjh21 wd33c93_hexdump(u_char *buf, int len)
2337 1.1 bjh21 {
2338 1.1 bjh21 printf("{%d}:", len);
2339 1.1 bjh21 while (len--)
2340 1.1 bjh21 printf(" %02x", *buf++);
2341 1.1 bjh21 printf("\n");
2342 1.1 bjh21 }
2343 1.1 bjh21
2344 1.1 bjh21
2345 1.1 bjh21 void
2346 1.1 bjh21 wd33c93_print_csr(u_char csr)
2347 1.1 bjh21 {
2348 1.1 bjh21 switch (SCSI_PHASE(csr)) {
2349 1.1 bjh21 case CMD_PHASE:
2350 1.1 bjh21 printf("CMD_PHASE\n");
2351 1.1 bjh21 break;
2352 1.1 bjh21
2353 1.1 bjh21 case STATUS_PHASE:
2354 1.1 bjh21 printf("STATUS_PHASE\n");
2355 1.1 bjh21 break;
2356 1.1 bjh21
2357 1.1 bjh21 case DATA_IN_PHASE:
2358 1.1 bjh21 printf("DATAIN_PHASE\n");
2359 1.1 bjh21 break;
2360 1.1 bjh21
2361 1.1 bjh21 case DATA_OUT_PHASE:
2362 1.1 bjh21 printf("DATAOUT_PHASE\n");
2363 1.1 bjh21 break;
2364 1.1 bjh21
2365 1.1 bjh21 case MESG_IN_PHASE:
2366 1.1 bjh21 printf("MESG_IN_PHASE\n");
2367 1.1 bjh21 break;
2368 1.1 bjh21
2369 1.1 bjh21 case MESG_OUT_PHASE:
2370 1.1 bjh21 printf("MESG_OUT_PHASE\n");
2371 1.1 bjh21 break;
2372 1.1 bjh21
2373 1.1 bjh21 default:
2374 1.1 bjh21 switch (csr) {
2375 1.1 bjh21 case SBIC_CSR_DISC_1:
2376 1.1 bjh21 printf("DISC_1\n");
2377 1.1 bjh21 break;
2378 1.1 bjh21
2379 1.1 bjh21 case SBIC_CSR_RSLT_NI:
2380 1.1 bjh21 printf("RESELECT_NO_IFY\n");
2381 1.1 bjh21 break;
2382 1.1 bjh21
2383 1.1 bjh21 case SBIC_CSR_RSLT_IFY:
2384 1.1 bjh21 printf("RESELECT_IFY\n");
2385 1.1 bjh21 break;
2386 1.1 bjh21
2387 1.1 bjh21 case SBIC_CSR_SLT:
2388 1.1 bjh21 printf("SELECT\n");
2389 1.1 bjh21 break;
2390 1.1 bjh21
2391 1.1 bjh21 case SBIC_CSR_SLT_ATN:
2392 1.1 bjh21 printf("SELECT, ATN\n");
2393 1.1 bjh21 break;
2394 1.1 bjh21
2395 1.1 bjh21 case SBIC_CSR_UNK_GROUP:
2396 1.1 bjh21 printf("UNK_GROUP\n");
2397 1.1 bjh21 break;
2398 1.1 bjh21
2399 1.1 bjh21 default:
2400 1.1 bjh21 printf("UNKNOWN csr=%02x\n", csr);
2401 1.1 bjh21 }
2402 1.1 bjh21 }
2403 1.1 bjh21 }
2404 1.1 bjh21 #endif
2405