siop.c revision 1.37.2.6 1 1.37.2.6 bouyer /* $NetBSD: siop.c,v 1.37.2.6 2001/01/15 09:26:26 bouyer Exp $ */
2 1.37.2.2 bouyer
3 1.37.2.2 bouyer /*
4 1.37.2.2 bouyer * Copyright (c) 2000 Manuel Bouyer.
5 1.37.2.2 bouyer *
6 1.37.2.2 bouyer * Redistribution and use in source and binary forms, with or without
7 1.37.2.2 bouyer * modification, are permitted provided that the following conditions
8 1.37.2.2 bouyer * are met:
9 1.37.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.37.2.2 bouyer * notice, this list of conditions and the following disclaimer.
11 1.37.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.37.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.37.2.2 bouyer * documentation and/or other materials provided with the distribution.
14 1.37.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.37.2.2 bouyer * must display the following acknowledgement:
16 1.37.2.2 bouyer * This product includes software developed by Manuel Bouyer
17 1.37.2.2 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.37.2.2 bouyer * derived from this software without specific prior written permission.
19 1.37.2.2 bouyer *
20 1.37.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.37.2.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.37.2.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.37.2.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.37.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.37.2.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.37.2.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.37.2.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.37.2.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.37.2.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.37.2.2 bouyer *
31 1.37.2.2 bouyer */
32 1.37.2.2 bouyer
33 1.37.2.2 bouyer /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
34 1.37.2.2 bouyer
35 1.37.2.2 bouyer #include <sys/param.h>
36 1.37.2.2 bouyer #include <sys/systm.h>
37 1.37.2.2 bouyer #include <sys/device.h>
38 1.37.2.2 bouyer #include <sys/malloc.h>
39 1.37.2.2 bouyer #include <sys/buf.h>
40 1.37.2.2 bouyer #include <sys/kernel.h>
41 1.37.2.2 bouyer
42 1.37.2.3 bouyer #include <uvm/uvm_extern.h>
43 1.37.2.3 bouyer
44 1.37.2.2 bouyer #include <machine/endian.h>
45 1.37.2.2 bouyer #include <machine/bus.h>
46 1.37.2.2 bouyer
47 1.37.2.2 bouyer #include <dev/microcode/siop/siop.out>
48 1.37.2.2 bouyer
49 1.37.2.2 bouyer #include <dev/scsipi/scsi_all.h>
50 1.37.2.2 bouyer #include <dev/scsipi/scsi_message.h>
51 1.37.2.2 bouyer #include <dev/scsipi/scsipi_all.h>
52 1.37.2.2 bouyer
53 1.37.2.2 bouyer #include <dev/scsipi/scsiconf.h>
54 1.37.2.2 bouyer
55 1.37.2.2 bouyer #include <dev/ic/siopreg.h>
56 1.37.2.2 bouyer #include <dev/ic/siopvar.h>
57 1.37.2.2 bouyer #include <dev/ic/siopvar_common.h>
58 1.37.2.2 bouyer
59 1.37.2.2 bouyer #ifndef DEBUG
60 1.37.2.2 bouyer #undef DEBUG
61 1.37.2.2 bouyer #endif
62 1.37.2.2 bouyer #undef SIOP_DEBUG
63 1.37.2.2 bouyer #undef SIOP_DEBUG_DR
64 1.37.2.2 bouyer #undef SIOP_DEBUG_INTR
65 1.37.2.2 bouyer #undef SIOP_DEBUG_SCHED
66 1.37.2.2 bouyer #undef DUMP_SCRIPT
67 1.37.2.2 bouyer
68 1.37.2.2 bouyer #define SIOP_STATS
69 1.37.2.2 bouyer
70 1.37.2.2 bouyer #ifndef SIOP_DEFAULT_TARGET
71 1.37.2.2 bouyer #define SIOP_DEFAULT_TARGET 7
72 1.37.2.2 bouyer #endif
73 1.37.2.2 bouyer
74 1.37.2.2 bouyer /* number of cmd descriptors per block */
75 1.37.2.3 bouyer #define SIOP_NCMDPB (PAGE_SIZE / sizeof(struct siop_xfer))
76 1.37.2.2 bouyer
77 1.37.2.2 bouyer /* Number of scheduler slot (needs to match script) */
78 1.37.2.2 bouyer #define SIOP_NSLOTS 40
79 1.37.2.2 bouyer
80 1.37.2.2 bouyer void siop_reset __P((struct siop_softc *));
81 1.37.2.2 bouyer void siop_handle_reset __P((struct siop_softc *));
82 1.37.2.2 bouyer int siop_handle_qtag_reject __P((struct siop_cmd *));
83 1.37.2.2 bouyer void siop_scsicmd_end __P((struct siop_cmd *));
84 1.37.2.2 bouyer void siop_start __P((struct siop_softc *));
85 1.37.2.2 bouyer void siop_timeout __P((void *));
86 1.37.2.2 bouyer int siop_scsicmd __P((struct scsipi_xfer *));
87 1.37.2.4 bouyer void siop_scsipi_request __P(( struct scsipi_channel *,
88 1.37.2.4 bouyer scsipi_adapter_req_t, void *));
89 1.37.2.2 bouyer void siop_dump_script __P((struct siop_softc *));
90 1.37.2.2 bouyer int siop_morecbd __P((struct siop_softc *));
91 1.37.2.2 bouyer struct siop_lunsw *siop_get_lunsw __P((struct siop_softc *));
92 1.37.2.2 bouyer void siop_add_reselsw __P((struct siop_softc *, int));
93 1.37.2.2 bouyer void siop_update_scntl3 __P((struct siop_softc *, struct siop_target *));
94 1.37.2.2 bouyer
95 1.37.2.2 bouyer #ifdef SIOP_STATS
96 1.37.2.2 bouyer static int siop_stat_intr = 0;
97 1.37.2.2 bouyer static int siop_stat_intr_shortxfer = 0;
98 1.37.2.2 bouyer static int siop_stat_intr_sdp = 0;
99 1.37.2.2 bouyer static int siop_stat_intr_done = 0;
100 1.37.2.2 bouyer static int siop_stat_intr_xferdisc = 0;
101 1.37.2.2 bouyer static int siop_stat_intr_lunresel = 0;
102 1.37.2.2 bouyer static int siop_stat_intr_qfull = 0;
103 1.37.2.2 bouyer void siop_printstats __P((void));
104 1.37.2.2 bouyer #define INCSTAT(x) x++
105 1.37.2.2 bouyer #else
106 1.37.2.2 bouyer #define INCSTAT(x)
107 1.37.2.2 bouyer #endif
108 1.37.2.2 bouyer
109 1.37.2.2 bouyer static __inline__ void siop_script_sync __P((struct siop_softc *, int));
110 1.37.2.2 bouyer static __inline__ void
111 1.37.2.2 bouyer siop_script_sync(sc, ops)
112 1.37.2.2 bouyer struct siop_softc *sc;
113 1.37.2.2 bouyer int ops;
114 1.37.2.2 bouyer {
115 1.37.2.2 bouyer if ((sc->features & SF_CHIP_RAM) == 0)
116 1.37.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0,
117 1.37.2.3 bouyer PAGE_SIZE, ops);
118 1.37.2.2 bouyer }
119 1.37.2.2 bouyer
120 1.37.2.2 bouyer static __inline__ u_int32_t siop_script_read __P((struct siop_softc *, u_int));
121 1.37.2.2 bouyer static __inline__ u_int32_t
122 1.37.2.2 bouyer siop_script_read(sc, offset)
123 1.37.2.2 bouyer struct siop_softc *sc;
124 1.37.2.2 bouyer u_int offset;
125 1.37.2.2 bouyer {
126 1.37.2.2 bouyer if (sc->features & SF_CHIP_RAM) {
127 1.37.2.2 bouyer return bus_space_read_4(sc->sc_ramt, sc->sc_ramh, offset * 4);
128 1.37.2.2 bouyer } else {
129 1.37.2.2 bouyer return le32toh(sc->sc_script[offset]);
130 1.37.2.2 bouyer }
131 1.37.2.2 bouyer }
132 1.37.2.2 bouyer
133 1.37.2.2 bouyer static __inline__ void siop_script_write __P((struct siop_softc *, u_int,
134 1.37.2.2 bouyer u_int32_t));
135 1.37.2.2 bouyer static __inline__ void
136 1.37.2.2 bouyer siop_script_write(sc, offset, val)
137 1.37.2.2 bouyer struct siop_softc *sc;
138 1.37.2.2 bouyer u_int offset;
139 1.37.2.2 bouyer u_int32_t val;
140 1.37.2.2 bouyer {
141 1.37.2.2 bouyer if (sc->features & SF_CHIP_RAM) {
142 1.37.2.2 bouyer bus_space_write_4(sc->sc_ramt, sc->sc_ramh, offset * 4, val);
143 1.37.2.2 bouyer } else {
144 1.37.2.2 bouyer sc->sc_script[offset] = htole32(val);
145 1.37.2.2 bouyer }
146 1.37.2.2 bouyer }
147 1.37.2.2 bouyer
148 1.37.2.2 bouyer void
149 1.37.2.2 bouyer siop_attach(sc)
150 1.37.2.2 bouyer struct siop_softc *sc;
151 1.37.2.2 bouyer {
152 1.37.2.2 bouyer int error, i;
153 1.37.2.2 bouyer bus_dma_segment_t seg;
154 1.37.2.2 bouyer int rseg;
155 1.37.2.2 bouyer
156 1.37.2.2 bouyer /*
157 1.37.2.2 bouyer * Allocate DMA-safe memory for the script and map it.
158 1.37.2.2 bouyer */
159 1.37.2.2 bouyer if ((sc->features & SF_CHIP_RAM) == 0) {
160 1.37.2.3 bouyer error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
161 1.37.2.3 bouyer PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
162 1.37.2.2 bouyer if (error) {
163 1.37.2.2 bouyer printf("%s: unable to allocate script DMA memory, "
164 1.37.2.2 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
165 1.37.2.2 bouyer return;
166 1.37.2.2 bouyer }
167 1.37.2.3 bouyer error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
168 1.37.2.2 bouyer (caddr_t *)&sc->sc_script, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
169 1.37.2.2 bouyer if (error) {
170 1.37.2.2 bouyer printf("%s: unable to map script DMA memory, "
171 1.37.2.2 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
172 1.37.2.2 bouyer return;
173 1.37.2.2 bouyer }
174 1.37.2.3 bouyer error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
175 1.37.2.3 bouyer PAGE_SIZE, 0, BUS_DMA_NOWAIT, &sc->sc_scriptdma);
176 1.37.2.2 bouyer if (error) {
177 1.37.2.2 bouyer printf("%s: unable to create script DMA map, "
178 1.37.2.2 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
179 1.37.2.2 bouyer return;
180 1.37.2.2 bouyer }
181 1.37.2.2 bouyer error = bus_dmamap_load(sc->sc_dmat, sc->sc_scriptdma,
182 1.37.2.3 bouyer sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
183 1.37.2.2 bouyer if (error) {
184 1.37.2.2 bouyer printf("%s: unable to load script DMA map, "
185 1.37.2.2 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
186 1.37.2.2 bouyer return;
187 1.37.2.2 bouyer }
188 1.37.2.2 bouyer sc->sc_scriptaddr = sc->sc_scriptdma->dm_segs[0].ds_addr;
189 1.37.2.3 bouyer sc->ram_size = PAGE_SIZE;
190 1.37.2.2 bouyer }
191 1.37.2.2 bouyer TAILQ_INIT(&sc->free_list);
192 1.37.2.2 bouyer TAILQ_INIT(&sc->ready_list);
193 1.37.2.2 bouyer TAILQ_INIT(&sc->urgent_list);
194 1.37.2.2 bouyer TAILQ_INIT(&sc->cmds);
195 1.37.2.2 bouyer TAILQ_INIT(&sc->lunsw_list);
196 1.37.2.2 bouyer sc->sc_currschedslot = 0;
197 1.37.2.2 bouyer #ifdef SIOP_DEBUG
198 1.37.2.2 bouyer printf("%s: script size = %d, PHY addr=0x%x, VIRT=%p\n",
199 1.37.2.2 bouyer sc->sc_dev.dv_xname, (int)sizeof(siop_script),
200 1.37.2.2 bouyer (u_int32_t)sc->sc_scriptaddr, sc->sc_script);
201 1.37.2.2 bouyer #endif
202 1.37.2.2 bouyer
203 1.37.2.4 bouyer sc->sc_adapt.adapt_dev = &sc->sc_dev;
204 1.37.2.4 bouyer sc->sc_adapt.adapt_nchannels = 1;
205 1.37.2.4 bouyer sc->sc_adapt.adapt_openings = 225;
206 1.37.2.5 bouyer sc->sc_adapt.adapt_max_periph = SIOP_NTAG - 1;
207 1.37.2.4 bouyer sc->sc_adapt.adapt_ioctl = siop_ioctl;
208 1.37.2.4 bouyer sc->sc_adapt.adapt_minphys = minphys;
209 1.37.2.4 bouyer sc->sc_adapt.adapt_request = siop_scsipi_request;
210 1.37.2.4 bouyer
211 1.37.2.4 bouyer memset(&sc->sc_chan, 0, sizeof(sc->sc_chan));
212 1.37.2.4 bouyer sc->sc_chan.chan_adapter = &sc->sc_adapt;
213 1.37.2.4 bouyer sc->sc_chan.chan_bustype = &scsi_bustype;
214 1.37.2.4 bouyer sc->sc_chan.chan_channel = 0;
215 1.37.2.4 bouyer sc->sc_chan.chan_ntargets = (sc->features & SF_BUS_WIDE) ? 16 : 8;
216 1.37.2.4 bouyer sc->sc_chan.chan_nluns = 8;
217 1.37.2.4 bouyer sc->sc_chan.chan_id = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCID);
218 1.37.2.4 bouyer if (sc->sc_chan.chan_id == 0 ||
219 1.37.2.4 bouyer sc->sc_chan.chan_id >= sc->sc_chan.chan_ntargets)
220 1.37.2.4 bouyer sc->sc_chan.chan_id = SIOP_DEFAULT_TARGET;
221 1.37.2.2 bouyer
222 1.37.2.2 bouyer for (i = 0; i < 16; i++)
223 1.37.2.2 bouyer sc->targets[i] = NULL;
224 1.37.2.2 bouyer
225 1.37.2.2 bouyer /* find min/max sync period for this chip */
226 1.37.2.2 bouyer sc->maxsync = 0;
227 1.37.2.2 bouyer sc->minsync = 255;
228 1.37.2.2 bouyer for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]); i++) {
229 1.37.2.2 bouyer if (sc->clock_period != scf_period[i].clock)
230 1.37.2.2 bouyer continue;
231 1.37.2.2 bouyer if (sc->maxsync < scf_period[i].period)
232 1.37.2.2 bouyer sc->maxsync = scf_period[i].period;
233 1.37.2.2 bouyer if (sc->minsync > scf_period[i].period)
234 1.37.2.2 bouyer sc->minsync = scf_period[i].period;
235 1.37.2.2 bouyer }
236 1.37.2.2 bouyer if (sc->maxsync == 255 || sc->minsync == 0)
237 1.37.2.2 bouyer panic("siop: can't find my sync parameters\n");
238 1.37.2.2 bouyer /* Do a bus reset, so that devices fall back to narrow/async */
239 1.37.2.2 bouyer siop_resetbus(sc);
240 1.37.2.2 bouyer /*
241 1.37.2.2 bouyer * siop_reset() will reset the chip, thus clearing pending interrupts
242 1.37.2.2 bouyer */
243 1.37.2.2 bouyer siop_reset(sc);
244 1.37.2.2 bouyer #ifdef DUMP_SCRIPT
245 1.37.2.2 bouyer siop_dump_script(sc);
246 1.37.2.2 bouyer #endif
247 1.37.2.2 bouyer
248 1.37.2.4 bouyer config_found((struct device*)sc, &sc->sc_chan, scsiprint);
249 1.37.2.2 bouyer }
250 1.37.2.2 bouyer
251 1.37.2.2 bouyer void
252 1.37.2.2 bouyer siop_reset(sc)
253 1.37.2.2 bouyer struct siop_softc *sc;
254 1.37.2.2 bouyer {
255 1.37.2.2 bouyer int i, j;
256 1.37.2.2 bouyer struct siop_lunsw *lunsw;
257 1.37.2.2 bouyer
258 1.37.2.2 bouyer siop_common_reset(sc);
259 1.37.2.2 bouyer
260 1.37.2.2 bouyer /* copy and patch the script */
261 1.37.2.2 bouyer if (sc->features & SF_CHIP_RAM) {
262 1.37.2.2 bouyer bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh, 0,
263 1.37.2.2 bouyer siop_script, sizeof(siop_script) / sizeof(siop_script[0]));
264 1.37.2.2 bouyer for (j = 0; j <
265 1.37.2.2 bouyer (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
266 1.37.2.2 bouyer j++) {
267 1.37.2.2 bouyer bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
268 1.37.2.2 bouyer E_abs_msgin_Used[j] * 4,
269 1.37.2.2 bouyer sc->sc_scriptaddr + Ent_msgin_space);
270 1.37.2.2 bouyer }
271 1.37.2.2 bouyer } else {
272 1.37.2.2 bouyer for (j = 0;
273 1.37.2.2 bouyer j < (sizeof(siop_script) / sizeof(siop_script[0])); j++) {
274 1.37.2.2 bouyer sc->sc_script[j] = htole32(siop_script[j]);
275 1.37.2.2 bouyer }
276 1.37.2.2 bouyer for (j = 0; j <
277 1.37.2.2 bouyer (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
278 1.37.2.2 bouyer j++) {
279 1.37.2.2 bouyer sc->sc_script[E_abs_msgin_Used[j]] =
280 1.37.2.2 bouyer htole32(sc->sc_scriptaddr + Ent_msgin_space);
281 1.37.2.2 bouyer }
282 1.37.2.2 bouyer }
283 1.37.2.2 bouyer sc->script_free_lo = sizeof(siop_script) / sizeof(siop_script[0]);
284 1.37.2.2 bouyer sc->script_free_hi = sc->ram_size / 4;
285 1.37.2.2 bouyer
286 1.37.2.2 bouyer /* free used and unused lun switches */
287 1.37.2.2 bouyer while((lunsw = TAILQ_FIRST(&sc->lunsw_list)) != NULL) {
288 1.37.2.2 bouyer #ifdef SIOP_DEBUG
289 1.37.2.2 bouyer printf("%s: free lunsw at offset %d\n",
290 1.37.2.2 bouyer sc->sc_dev.dv_xname, lunsw->lunsw_off);
291 1.37.2.2 bouyer #endif
292 1.37.2.2 bouyer TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
293 1.37.2.2 bouyer free(lunsw, M_DEVBUF);
294 1.37.2.2 bouyer }
295 1.37.2.2 bouyer TAILQ_INIT(&sc->lunsw_list);
296 1.37.2.2 bouyer /* restore reselect switch */
297 1.37.2.4 bouyer for (i = 0; i < sc->sc_chan.chan_ntargets; i++) {
298 1.37.2.2 bouyer if (sc->targets[i] == NULL)
299 1.37.2.2 bouyer continue;
300 1.37.2.2 bouyer #ifdef SIOP_DEBUG
301 1.37.2.2 bouyer printf("%s: restore sw for target %d\n",
302 1.37.2.2 bouyer sc->sc_dev.dv_xname, i);
303 1.37.2.2 bouyer #endif
304 1.37.2.2 bouyer free(sc->targets[i]->lunsw, M_DEVBUF);
305 1.37.2.2 bouyer sc->targets[i]->lunsw = siop_get_lunsw(sc);
306 1.37.2.2 bouyer if (sc->targets[i]->lunsw == NULL) {
307 1.37.2.2 bouyer printf("%s: can't alloc lunsw for target %d\n",
308 1.37.2.2 bouyer sc->sc_dev.dv_xname, i);
309 1.37.2.2 bouyer break;
310 1.37.2.2 bouyer }
311 1.37.2.2 bouyer siop_add_reselsw(sc, i);
312 1.37.2.2 bouyer }
313 1.37.2.2 bouyer
314 1.37.2.2 bouyer /* start script */
315 1.37.2.2 bouyer if ((sc->features & SF_CHIP_RAM) == 0) {
316 1.37.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0, PAGE_SIZE,
317 1.37.2.2 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
318 1.37.2.2 bouyer }
319 1.37.2.2 bouyer bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
320 1.37.2.2 bouyer sc->sc_scriptaddr + Ent_reselect);
321 1.37.2.2 bouyer }
322 1.37.2.2 bouyer
323 1.37.2.2 bouyer #if 0
324 1.37.2.2 bouyer #define CALL_SCRIPT(ent) do {\
325 1.37.2.2 bouyer printf ("start script DSA 0x%lx DSP 0x%lx\n", \
326 1.37.2.2 bouyer siop_cmd->dsa, \
327 1.37.2.2 bouyer sc->sc_scriptaddr + ent); \
328 1.37.2.2 bouyer bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
329 1.37.2.2 bouyer } while (0)
330 1.37.2.2 bouyer #else
331 1.37.2.2 bouyer #define CALL_SCRIPT(ent) do {\
332 1.37.2.2 bouyer bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
333 1.37.2.2 bouyer } while (0)
334 1.37.2.2 bouyer #endif
335 1.37.2.2 bouyer
336 1.37.2.2 bouyer int
337 1.37.2.2 bouyer siop_intr(v)
338 1.37.2.2 bouyer void *v;
339 1.37.2.2 bouyer {
340 1.37.2.2 bouyer struct siop_softc *sc = v;
341 1.37.2.2 bouyer struct siop_target *siop_target;
342 1.37.2.2 bouyer struct siop_cmd *siop_cmd;
343 1.37.2.2 bouyer struct siop_lun *siop_lun;
344 1.37.2.2 bouyer struct scsipi_xfer *xs;
345 1.37.2.2 bouyer int istat, sist, sstat1, dstat;
346 1.37.2.2 bouyer u_int32_t irqcode;
347 1.37.2.2 bouyer int need_reset = 0;
348 1.37.2.2 bouyer int offset, target, lun, tag;
349 1.37.2.2 bouyer bus_addr_t dsa;
350 1.37.2.2 bouyer struct siop_cbd *cbdp;
351 1.37.2.2 bouyer int freetarget = 0;
352 1.37.2.2 bouyer
353 1.37.2.2 bouyer istat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT);
354 1.37.2.2 bouyer if ((istat & (ISTAT_INTF | ISTAT_DIP | ISTAT_SIP)) == 0)
355 1.37.2.2 bouyer return 0;
356 1.37.2.2 bouyer INCSTAT(siop_stat_intr);
357 1.37.2.2 bouyer if (istat & ISTAT_INTF) {
358 1.37.2.2 bouyer printf("INTRF\n");
359 1.37.2.2 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_INTF);
360 1.37.2.2 bouyer }
361 1.37.2.2 bouyer /* use DSA to find the current siop_cmd */
362 1.37.2.2 bouyer dsa = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA);
363 1.37.2.2 bouyer for (cbdp = TAILQ_FIRST(&sc->cmds); cbdp != NULL;
364 1.37.2.2 bouyer cbdp = TAILQ_NEXT(cbdp, next)) {
365 1.37.2.2 bouyer if (dsa >= cbdp->xferdma->dm_segs[0].ds_addr &&
366 1.37.2.3 bouyer dsa < cbdp->xferdma->dm_segs[0].ds_addr + PAGE_SIZE) {
367 1.37.2.2 bouyer dsa -= cbdp->xferdma->dm_segs[0].ds_addr;
368 1.37.2.2 bouyer siop_cmd = &cbdp->cmds[dsa / sizeof(struct siop_xfer)];
369 1.37.2.2 bouyer siop_table_sync(siop_cmd,
370 1.37.2.2 bouyer BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
371 1.37.2.2 bouyer break;
372 1.37.2.2 bouyer }
373 1.37.2.2 bouyer }
374 1.37.2.2 bouyer if (cbdp == NULL) {
375 1.37.2.2 bouyer siop_cmd = NULL;
376 1.37.2.2 bouyer }
377 1.37.2.2 bouyer if (siop_cmd) {
378 1.37.2.2 bouyer xs = siop_cmd->xs;
379 1.37.2.2 bouyer siop_target = siop_cmd->siop_target;
380 1.37.2.4 bouyer target = siop_cmd->xs->xs_periph->periph_target;
381 1.37.2.4 bouyer lun = siop_cmd->xs->xs_periph->periph_lun;
382 1.37.2.2 bouyer tag = siop_cmd->tag;
383 1.37.2.2 bouyer siop_lun = siop_target->siop_lun[lun];
384 1.37.2.2 bouyer #ifdef DIAGNOSTIC
385 1.37.2.6 bouyer if (siop_cmd->status != CMDST_ACTIVE) {
386 1.37.2.2 bouyer printf("siop_cmd (lun %d) not active (%d)\n",
387 1.37.2.2 bouyer lun, siop_cmd->status);
388 1.37.2.2 bouyer xs = NULL;
389 1.37.2.2 bouyer siop_target = NULL;
390 1.37.2.2 bouyer target = -1;
391 1.37.2.2 bouyer lun = -1;
392 1.37.2.2 bouyer tag = -1;
393 1.37.2.2 bouyer siop_lun = NULL;
394 1.37.2.2 bouyer siop_cmd = NULL;
395 1.37.2.2 bouyer } else if (siop_lun->siop_tag[tag].active != siop_cmd) {
396 1.37.2.2 bouyer printf("siop_cmd (lun %d tag %d) not in siop_lun "
397 1.37.2.2 bouyer "active (%p != %p)\n", lun, tag, siop_cmd,
398 1.37.2.2 bouyer siop_lun->siop_tag[tag].active);
399 1.37.2.2 bouyer }
400 1.37.2.2 bouyer #endif
401 1.37.2.2 bouyer } else {
402 1.37.2.2 bouyer xs = NULL;
403 1.37.2.2 bouyer siop_target = NULL;
404 1.37.2.2 bouyer target = -1;
405 1.37.2.2 bouyer lun = -1;
406 1.37.2.2 bouyer tag = -1;
407 1.37.2.2 bouyer siop_lun = NULL;
408 1.37.2.2 bouyer }
409 1.37.2.2 bouyer if (istat & ISTAT_DIP) {
410 1.37.2.2 bouyer dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DSTAT);
411 1.37.2.2 bouyer if (dstat & DSTAT_SSI) {
412 1.37.2.2 bouyer printf("single step dsp 0x%08x dsa 0x08%x\n",
413 1.37.2.2 bouyer (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
414 1.37.2.2 bouyer sc->sc_scriptaddr),
415 1.37.2.2 bouyer bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
416 1.37.2.2 bouyer if ((dstat & ~(DSTAT_DFE | DSTAT_SSI)) == 0 &&
417 1.37.2.2 bouyer (istat & ISTAT_SIP) == 0) {
418 1.37.2.2 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh,
419 1.37.2.2 bouyer SIOP_DCNTL, bus_space_read_1(sc->sc_rt,
420 1.37.2.2 bouyer sc->sc_rh, SIOP_DCNTL) | DCNTL_STD);
421 1.37.2.2 bouyer }
422 1.37.2.2 bouyer return 1;
423 1.37.2.2 bouyer }
424 1.37.2.2 bouyer if (dstat & ~(DSTAT_SIR | DSTAT_DFE | DSTAT_SSI)) {
425 1.37.2.2 bouyer printf("DMA IRQ:");
426 1.37.2.2 bouyer if (dstat & DSTAT_IID)
427 1.37.2.2 bouyer printf(" Illegal instruction");
428 1.37.2.2 bouyer if (dstat & DSTAT_ABRT)
429 1.37.2.2 bouyer printf(" abort");
430 1.37.2.2 bouyer if (dstat & DSTAT_BF)
431 1.37.2.2 bouyer printf(" bus fault");
432 1.37.2.2 bouyer if (dstat & DSTAT_MDPE)
433 1.37.2.2 bouyer printf(" parity");
434 1.37.2.2 bouyer if (dstat & DSTAT_DFE)
435 1.37.2.2 bouyer printf(" dma fifo empty");
436 1.37.2.2 bouyer printf(", DSP=0x%x DSA=0x%x: ",
437 1.37.2.2 bouyer (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
438 1.37.2.2 bouyer sc->sc_scriptaddr),
439 1.37.2.2 bouyer bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
440 1.37.2.2 bouyer if (siop_cmd)
441 1.37.2.2 bouyer printf("last msg_in=0x%x status=0x%x\n",
442 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[0],
443 1.37.2.2 bouyer le32toh(siop_cmd->siop_tables.status));
444 1.37.2.2 bouyer else
445 1.37.2.2 bouyer printf("%s: current DSA invalid\n",
446 1.37.2.2 bouyer sc->sc_dev.dv_xname);
447 1.37.2.2 bouyer need_reset = 1;
448 1.37.2.2 bouyer }
449 1.37.2.2 bouyer }
450 1.37.2.2 bouyer if (istat & ISTAT_SIP) {
451 1.37.2.2 bouyer if (istat & ISTAT_DIP)
452 1.37.2.2 bouyer delay(10);
453 1.37.2.2 bouyer /*
454 1.37.2.2 bouyer * Can't read sist0 & sist1 independantly, or we have to
455 1.37.2.2 bouyer * insert delay
456 1.37.2.2 bouyer */
457 1.37.2.2 bouyer sist = bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
458 1.37.2.2 bouyer sstat1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1);
459 1.37.2.2 bouyer #ifdef SIOP_DEBUG_INTR
460 1.37.2.2 bouyer printf("scsi interrupt, sist=0x%x sstat1=0x%x "
461 1.37.2.2 bouyer "DSA=0x%x DSP=0x%lx\n", sist,
462 1.37.2.2 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
463 1.37.2.2 bouyer bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
464 1.37.2.2 bouyer (u_long)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
465 1.37.2.2 bouyer sc->sc_scriptaddr));
466 1.37.2.2 bouyer #endif
467 1.37.2.2 bouyer if (sist & SIST0_RST) {
468 1.37.2.2 bouyer siop_handle_reset(sc);
469 1.37.2.2 bouyer siop_start(sc);
470 1.37.2.2 bouyer /* no table to flush here */
471 1.37.2.2 bouyer return 1;
472 1.37.2.2 bouyer }
473 1.37.2.2 bouyer if (sist & SIST0_SGE) {
474 1.37.2.2 bouyer if (siop_cmd)
475 1.37.2.4 bouyer scsipi_printaddr(xs->xs_periph);
476 1.37.2.2 bouyer else
477 1.37.2.2 bouyer printf("%s:", sc->sc_dev.dv_xname);
478 1.37.2.2 bouyer printf("scsi gross error\n");
479 1.37.2.2 bouyer goto reset;
480 1.37.2.2 bouyer }
481 1.37.2.2 bouyer if ((sist & SIST0_MA) && need_reset == 0) {
482 1.37.2.2 bouyer if (siop_cmd) {
483 1.37.2.2 bouyer int scratcha0;
484 1.37.2.2 bouyer dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
485 1.37.2.2 bouyer SIOP_DSTAT);
486 1.37.2.2 bouyer /*
487 1.37.2.2 bouyer * first restore DSA, in case we were in a S/G
488 1.37.2.2 bouyer * operation.
489 1.37.2.2 bouyer */
490 1.37.2.2 bouyer bus_space_write_4(sc->sc_rt, sc->sc_rh,
491 1.37.2.2 bouyer SIOP_DSA, siop_cmd->dsa);
492 1.37.2.2 bouyer scratcha0 = bus_space_read_1(sc->sc_rt,
493 1.37.2.2 bouyer sc->sc_rh, SIOP_SCRATCHA);
494 1.37.2.2 bouyer switch (sstat1 & SSTAT1_PHASE_MASK) {
495 1.37.2.2 bouyer case SSTAT1_PHASE_STATUS:
496 1.37.2.2 bouyer /*
497 1.37.2.2 bouyer * previous phase may be aborted for any reason
498 1.37.2.2 bouyer * ( for example, the target has less data to
499 1.37.2.2 bouyer * transfer than requested). Just go to status
500 1.37.2.2 bouyer * and the command should terminate.
501 1.37.2.2 bouyer */
502 1.37.2.2 bouyer INCSTAT(siop_stat_intr_shortxfer);
503 1.37.2.2 bouyer if ((dstat & DSTAT_DFE) == 0)
504 1.37.2.2 bouyer siop_clearfifo(sc);
505 1.37.2.2 bouyer /* no table to flush here */
506 1.37.2.2 bouyer CALL_SCRIPT(Ent_status);
507 1.37.2.2 bouyer return 1;
508 1.37.2.2 bouyer case SSTAT1_PHASE_MSGIN:
509 1.37.2.2 bouyer /*
510 1.37.2.2 bouyer * target may be ready to disconnect
511 1.37.2.2 bouyer * Save data pointers just in case.
512 1.37.2.2 bouyer */
513 1.37.2.2 bouyer INCSTAT(siop_stat_intr_xferdisc);
514 1.37.2.2 bouyer if (scratcha0 & A_flag_data)
515 1.37.2.2 bouyer siop_sdp(siop_cmd);
516 1.37.2.2 bouyer else if ((dstat & DSTAT_DFE) == 0)
517 1.37.2.2 bouyer siop_clearfifo(sc);
518 1.37.2.2 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh,
519 1.37.2.2 bouyer SIOP_SCRATCHA,
520 1.37.2.2 bouyer scratcha0 & ~A_flag_data);
521 1.37.2.2 bouyer siop_table_sync(siop_cmd,
522 1.37.2.2 bouyer BUS_DMASYNC_PREREAD |
523 1.37.2.2 bouyer BUS_DMASYNC_PREWRITE);
524 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin);
525 1.37.2.2 bouyer return 1;
526 1.37.2.2 bouyer }
527 1.37.2.2 bouyer printf("%s: unexpected phase mismatch %d\n",
528 1.37.2.2 bouyer sc->sc_dev.dv_xname,
529 1.37.2.2 bouyer sstat1 & SSTAT1_PHASE_MASK);
530 1.37.2.2 bouyer } else {
531 1.37.2.2 bouyer printf("%s: phase mismatch without command\n",
532 1.37.2.2 bouyer sc->sc_dev.dv_xname);
533 1.37.2.2 bouyer }
534 1.37.2.2 bouyer need_reset = 1;
535 1.37.2.2 bouyer }
536 1.37.2.2 bouyer if (sist & SIST0_PAR) {
537 1.37.2.2 bouyer /* parity error, reset */
538 1.37.2.2 bouyer if (siop_cmd)
539 1.37.2.4 bouyer scsipi_printaddr(xs->xs_periph);
540 1.37.2.2 bouyer else
541 1.37.2.2 bouyer printf("%s:", sc->sc_dev.dv_xname);
542 1.37.2.2 bouyer printf("parity error\n");
543 1.37.2.2 bouyer goto reset;
544 1.37.2.2 bouyer }
545 1.37.2.2 bouyer if ((sist & (SIST1_STO << 8)) && need_reset == 0) {
546 1.37.2.2 bouyer /* selection time out, assume there's no device here */
547 1.37.2.2 bouyer if (siop_cmd) {
548 1.37.2.2 bouyer siop_cmd->status = CMDST_DONE;
549 1.37.2.2 bouyer xs->error = XS_SELTIMEOUT;
550 1.37.2.2 bouyer freetarget = 1;
551 1.37.2.2 bouyer goto end;
552 1.37.2.2 bouyer } else {
553 1.37.2.2 bouyer printf("%s: selection timeout without "
554 1.37.2.2 bouyer "command\n", sc->sc_dev.dv_xname);
555 1.37.2.2 bouyer need_reset = 1;
556 1.37.2.2 bouyer }
557 1.37.2.2 bouyer }
558 1.37.2.2 bouyer if (sist & SIST0_UDC) {
559 1.37.2.2 bouyer /*
560 1.37.2.2 bouyer * unexpected disconnect. Usually the target signals
561 1.37.2.2 bouyer * a fatal condition this way. Attempt to get sense.
562 1.37.2.2 bouyer */
563 1.37.2.2 bouyer if (siop_cmd) {
564 1.37.2.2 bouyer siop_cmd->siop_tables.status =
565 1.37.2.2 bouyer htole32(SCSI_CHECK);
566 1.37.2.2 bouyer goto end;
567 1.37.2.2 bouyer }
568 1.37.2.2 bouyer printf("%s: unexpected disconnect without "
569 1.37.2.2 bouyer "command\n", sc->sc_dev.dv_xname);
570 1.37.2.2 bouyer goto reset;
571 1.37.2.2 bouyer }
572 1.37.2.2 bouyer if (sist & (SIST1_SBMC << 8)) {
573 1.37.2.2 bouyer /* SCSI bus mode change */
574 1.37.2.2 bouyer if (siop_modechange(sc) == 0 || need_reset == 1)
575 1.37.2.2 bouyer goto reset;
576 1.37.2.2 bouyer if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) {
577 1.37.2.2 bouyer /*
578 1.37.2.2 bouyer * we have a script interrupt, it will
579 1.37.2.2 bouyer * restart the script.
580 1.37.2.2 bouyer */
581 1.37.2.2 bouyer goto scintr;
582 1.37.2.2 bouyer }
583 1.37.2.2 bouyer /*
584 1.37.2.2 bouyer * else we have to restart it ourselve, at the
585 1.37.2.2 bouyer * interrupted instruction.
586 1.37.2.2 bouyer */
587 1.37.2.2 bouyer bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
588 1.37.2.2 bouyer bus_space_read_4(sc->sc_rt, sc->sc_rh,
589 1.37.2.2 bouyer SIOP_DSP) - 8);
590 1.37.2.2 bouyer return 1;
591 1.37.2.2 bouyer }
592 1.37.2.2 bouyer /* Else it's an unhandled exeption (for now). */
593 1.37.2.2 bouyer printf("%s: unhandled scsi interrupt, sist=0x%x sstat1=0x%x "
594 1.37.2.2 bouyer "DSA=0x%x DSP=0x%x\n", sc->sc_dev.dv_xname, sist,
595 1.37.2.2 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
596 1.37.2.2 bouyer bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
597 1.37.2.2 bouyer (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
598 1.37.2.2 bouyer sc->sc_scriptaddr));
599 1.37.2.2 bouyer if (siop_cmd) {
600 1.37.2.2 bouyer siop_cmd->status = CMDST_DONE;
601 1.37.2.2 bouyer xs->error = XS_SELTIMEOUT;
602 1.37.2.2 bouyer goto end;
603 1.37.2.2 bouyer }
604 1.37.2.2 bouyer need_reset = 1;
605 1.37.2.2 bouyer }
606 1.37.2.2 bouyer if (need_reset) {
607 1.37.2.2 bouyer reset:
608 1.37.2.2 bouyer /* fatal error, reset the bus */
609 1.37.2.2 bouyer siop_resetbus(sc);
610 1.37.2.2 bouyer /* no table to flush here */
611 1.37.2.2 bouyer return 1;
612 1.37.2.2 bouyer }
613 1.37.2.2 bouyer
614 1.37.2.2 bouyer scintr:
615 1.37.2.2 bouyer if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) { /* script interrupt */
616 1.37.2.2 bouyer irqcode = bus_space_read_4(sc->sc_rt, sc->sc_rh,
617 1.37.2.2 bouyer SIOP_DSPS);
618 1.37.2.2 bouyer #ifdef SIOP_DEBUG_INTR
619 1.37.2.2 bouyer printf("script interrupt 0x%x\n", irqcode);
620 1.37.2.2 bouyer #endif
621 1.37.2.2 bouyer /*
622 1.37.2.2 bouyer * no command, or an inactive command is only valid for a
623 1.37.2.2 bouyer * reselect interrupt
624 1.37.2.2 bouyer */
625 1.37.2.2 bouyer if ((irqcode & 0x80) == 0) {
626 1.37.2.2 bouyer if (siop_cmd == NULL) {
627 1.37.2.2 bouyer printf("%s: script interrupt (0x%x) with
628 1.37.2.2 bouyer invalid DSA !!!\n", sc->sc_dev.dv_xname,
629 1.37.2.2 bouyer irqcode);
630 1.37.2.2 bouyer goto reset;
631 1.37.2.2 bouyer }
632 1.37.2.6 bouyer if (siop_cmd->status != CMDST_ACTIVE) {
633 1.37.2.2 bouyer printf("%s: command with invalid status "
634 1.37.2.2 bouyer "(IRQ code 0x%x current status %d) !\n",
635 1.37.2.2 bouyer sc->sc_dev.dv_xname,
636 1.37.2.2 bouyer irqcode, siop_cmd->status);
637 1.37.2.2 bouyer xs = NULL;
638 1.37.2.2 bouyer }
639 1.37.2.2 bouyer }
640 1.37.2.2 bouyer switch(irqcode) {
641 1.37.2.2 bouyer case A_int_err:
642 1.37.2.2 bouyer printf("error, DSP=0x%x\n",
643 1.37.2.2 bouyer (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh,
644 1.37.2.2 bouyer SIOP_DSP) - sc->sc_scriptaddr));
645 1.37.2.2 bouyer if (xs) {
646 1.37.2.2 bouyer xs->error = XS_SELTIMEOUT;
647 1.37.2.2 bouyer goto end;
648 1.37.2.2 bouyer } else {
649 1.37.2.2 bouyer goto reset;
650 1.37.2.2 bouyer }
651 1.37.2.2 bouyer case A_int_reseltarg:
652 1.37.2.2 bouyer printf("%s: reselect with invalid target\n",
653 1.37.2.2 bouyer sc->sc_dev.dv_xname);
654 1.37.2.2 bouyer goto reset;
655 1.37.2.2 bouyer case A_int_resellun:
656 1.37.2.2 bouyer INCSTAT(siop_stat_intr_lunresel);
657 1.37.2.2 bouyer target = bus_space_read_1(sc->sc_rt, sc->sc_rh,
658 1.37.2.2 bouyer SIOP_SCRATCHA) & 0xf;
659 1.37.2.2 bouyer lun = bus_space_read_1(sc->sc_rt, sc->sc_rh,
660 1.37.2.2 bouyer SIOP_SCRATCHA + 1);
661 1.37.2.2 bouyer tag = bus_space_read_1(sc->sc_rt, sc->sc_rh,
662 1.37.2.2 bouyer SIOP_SCRATCHA + 2);
663 1.37.2.2 bouyer siop_target = sc->targets[target];
664 1.37.2.2 bouyer if (siop_target == NULL) {
665 1.37.2.2 bouyer printf("%s: reselect with invalid "
666 1.37.2.2 bouyer "target %d\n", sc->sc_dev.dv_xname, target);
667 1.37.2.2 bouyer goto reset;
668 1.37.2.2 bouyer }
669 1.37.2.2 bouyer siop_lun = siop_target->siop_lun[lun];
670 1.37.2.2 bouyer if (siop_lun == NULL) {
671 1.37.2.2 bouyer printf("%s: target %d reselect with invalid "
672 1.37.2.2 bouyer "lun %d\n", sc->sc_dev.dv_xname,
673 1.37.2.2 bouyer target, lun);
674 1.37.2.2 bouyer goto reset;
675 1.37.2.2 bouyer }
676 1.37.2.2 bouyer if (siop_lun->siop_tag[tag].active == NULL) {
677 1.37.2.2 bouyer printf("%s: target %d lun %d tag %d reselect "
678 1.37.2.2 bouyer "without command\n", sc->sc_dev.dv_xname,
679 1.37.2.2 bouyer target, lun, tag);
680 1.37.2.2 bouyer goto reset;
681 1.37.2.2 bouyer }
682 1.37.2.2 bouyer siop_cmd = siop_lun->siop_tag[tag].active;
683 1.37.2.2 bouyer bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
684 1.37.2.2 bouyer siop_cmd->dsa + sizeof(struct siop_xfer_common) +
685 1.37.2.2 bouyer Ent_ldsa_reload_dsa);
686 1.37.2.2 bouyer return 1;
687 1.37.2.2 bouyer case A_int_reseltag:
688 1.37.2.2 bouyer printf("%s: reselect with invalid tag\n",
689 1.37.2.2 bouyer sc->sc_dev.dv_xname);
690 1.37.2.2 bouyer goto reset;
691 1.37.2.2 bouyer case A_int_msgin:
692 1.37.2.2 bouyer {
693 1.37.2.2 bouyer int msgin = bus_space_read_1(sc->sc_rt, sc->sc_rh,
694 1.37.2.2 bouyer SIOP_SFBR);
695 1.37.2.2 bouyer if (msgin == MSG_MESSAGE_REJECT) {
696 1.37.2.2 bouyer int msg, extmsg;
697 1.37.2.2 bouyer if (siop_cmd->siop_tables.msg_out[0] & 0x80) {
698 1.37.2.2 bouyer /*
699 1.37.2.2 bouyer * message was part of a identify +
700 1.37.2.2 bouyer * something else. Identify shoudl't
701 1.37.2.2 bouyer * have been rejected.
702 1.37.2.2 bouyer */
703 1.37.2.2 bouyer msg = siop_cmd->siop_tables.msg_out[1];
704 1.37.2.2 bouyer extmsg =
705 1.37.2.2 bouyer siop_cmd->siop_tables.msg_out[3];
706 1.37.2.2 bouyer } else {
707 1.37.2.2 bouyer msg = siop_cmd->siop_tables.msg_out[0];
708 1.37.2.2 bouyer extmsg =
709 1.37.2.2 bouyer siop_cmd->siop_tables.msg_out[2];
710 1.37.2.2 bouyer }
711 1.37.2.2 bouyer if (msg == MSG_MESSAGE_REJECT) {
712 1.37.2.2 bouyer /* MSG_REJECT for a MSG_REJECT !*/
713 1.37.2.2 bouyer if (xs)
714 1.37.2.4 bouyer scsipi_printaddr(xs->xs_periph);
715 1.37.2.2 bouyer else
716 1.37.2.2 bouyer printf("%s: ",
717 1.37.2.2 bouyer sc->sc_dev.dv_xname);
718 1.37.2.2 bouyer printf("our reject message was "
719 1.37.2.2 bouyer "rejected\n");
720 1.37.2.2 bouyer goto reset;
721 1.37.2.2 bouyer }
722 1.37.2.2 bouyer if (msg == MSG_EXTENDED &&
723 1.37.2.2 bouyer extmsg == MSG_EXT_WDTR) {
724 1.37.2.2 bouyer /* WDTR rejected, initiate sync */
725 1.37.2.2 bouyer if ((siop_target->flags & TARF_SYNC)
726 1.37.2.2 bouyer == 0) {
727 1.37.2.2 bouyer siop_target->status = TARST_OK;
728 1.37.2.4 bouyer siop_update_xfer_mode(sc,
729 1.37.2.4 bouyer target);
730 1.37.2.2 bouyer /* no table to flush here */
731 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin_ack);
732 1.37.2.2 bouyer return 1;
733 1.37.2.2 bouyer }
734 1.37.2.2 bouyer siop_target->status = TARST_SYNC_NEG;
735 1.37.2.2 bouyer siop_sdtr_msg(siop_cmd, 0,
736 1.37.2.2 bouyer sc->minsync, sc->maxoff);
737 1.37.2.2 bouyer siop_table_sync(siop_cmd,
738 1.37.2.2 bouyer BUS_DMASYNC_PREREAD |
739 1.37.2.2 bouyer BUS_DMASYNC_PREWRITE);
740 1.37.2.2 bouyer CALL_SCRIPT(Ent_send_msgout);
741 1.37.2.2 bouyer return 1;
742 1.37.2.2 bouyer } else if (msg == MSG_EXTENDED &&
743 1.37.2.2 bouyer extmsg == MSG_EXT_SDTR) {
744 1.37.2.2 bouyer /* sync rejected */
745 1.37.2.4 bouyer siop_target->offset = 0;
746 1.37.2.4 bouyer siop_target->period = 0;
747 1.37.2.2 bouyer siop_target->status = TARST_OK;
748 1.37.2.4 bouyer siop_update_xfer_mode(sc, target);
749 1.37.2.2 bouyer /* no table to flush here */
750 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin_ack);
751 1.37.2.2 bouyer return 1;
752 1.37.2.2 bouyer } else if (msg == MSG_SIMPLE_Q_TAG ||
753 1.37.2.2 bouyer msg == MSG_HEAD_OF_Q_TAG ||
754 1.37.2.2 bouyer msg == MSG_ORDERED_Q_TAG) {
755 1.37.2.2 bouyer if (siop_handle_qtag_reject(
756 1.37.2.2 bouyer siop_cmd) == -1)
757 1.37.2.2 bouyer goto reset;
758 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin_ack);
759 1.37.2.2 bouyer return 1;
760 1.37.2.2 bouyer }
761 1.37.2.2 bouyer if (xs)
762 1.37.2.4 bouyer scsipi_printaddr(xs->xs_periph);
763 1.37.2.2 bouyer else
764 1.37.2.2 bouyer printf("%s: ", sc->sc_dev.dv_xname);
765 1.37.2.2 bouyer if (msg == MSG_EXTENDED) {
766 1.37.2.2 bouyer printf("scsi message reject, extended "
767 1.37.2.2 bouyer "message sent was 0x%x\n", extmsg);
768 1.37.2.2 bouyer } else {
769 1.37.2.2 bouyer printf("scsi message reject, message "
770 1.37.2.2 bouyer "sent was 0x%x\n", msg);
771 1.37.2.2 bouyer }
772 1.37.2.2 bouyer /* no table to flush here */
773 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin_ack);
774 1.37.2.2 bouyer return 1;
775 1.37.2.2 bouyer }
776 1.37.2.2 bouyer if (xs)
777 1.37.2.4 bouyer scsipi_printaddr(xs->xs_periph);
778 1.37.2.2 bouyer else
779 1.37.2.2 bouyer printf("%s: ", sc->sc_dev.dv_xname);
780 1.37.2.2 bouyer printf("unhandled message 0x%x\n",
781 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[0]);
782 1.37.2.2 bouyer siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
783 1.37.2.2 bouyer siop_cmd->siop_tables.t_msgout.count= htole32(1);
784 1.37.2.2 bouyer siop_table_sync(siop_cmd,
785 1.37.2.2 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
786 1.37.2.2 bouyer CALL_SCRIPT(Ent_send_msgout);
787 1.37.2.2 bouyer return 1;
788 1.37.2.2 bouyer }
789 1.37.2.2 bouyer case A_int_extmsgin:
790 1.37.2.2 bouyer #ifdef SIOP_DEBUG_INTR
791 1.37.2.2 bouyer printf("extended message: msg 0x%x len %d\n",
792 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[2],
793 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[1]);
794 1.37.2.2 bouyer #endif
795 1.37.2.2 bouyer if (siop_cmd->siop_tables.msg_in[1] > 6)
796 1.37.2.2 bouyer printf("%s: extended message too big (%d)\n",
797 1.37.2.2 bouyer sc->sc_dev.dv_xname,
798 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[1]);
799 1.37.2.2 bouyer siop_cmd->siop_tables.t_extmsgdata.count =
800 1.37.2.2 bouyer htole32(siop_cmd->siop_tables.msg_in[1] - 1);
801 1.37.2.2 bouyer siop_table_sync(siop_cmd,
802 1.37.2.2 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
803 1.37.2.2 bouyer CALL_SCRIPT(Ent_get_extmsgdata);
804 1.37.2.2 bouyer return 1;
805 1.37.2.2 bouyer case A_int_extmsgdata:
806 1.37.2.2 bouyer #ifdef SIOP_DEBUG_INTR
807 1.37.2.2 bouyer {
808 1.37.2.2 bouyer int i;
809 1.37.2.2 bouyer printf("extended message: 0x%x, data:",
810 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[2]);
811 1.37.2.2 bouyer for (i = 3; i < 2 + siop_cmd->siop_tables.msg_in[1];
812 1.37.2.2 bouyer i++)
813 1.37.2.2 bouyer printf(" 0x%x",
814 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[i]);
815 1.37.2.2 bouyer printf("\n");
816 1.37.2.2 bouyer }
817 1.37.2.2 bouyer #endif
818 1.37.2.2 bouyer if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_WDTR) {
819 1.37.2.2 bouyer switch (siop_wdtr_neg(siop_cmd)) {
820 1.37.2.2 bouyer case SIOP_NEG_MSGOUT:
821 1.37.2.2 bouyer siop_update_scntl3(sc,
822 1.37.2.2 bouyer siop_cmd->siop_target);
823 1.37.2.2 bouyer siop_table_sync(siop_cmd,
824 1.37.2.2 bouyer BUS_DMASYNC_PREREAD |
825 1.37.2.2 bouyer BUS_DMASYNC_PREWRITE);
826 1.37.2.2 bouyer CALL_SCRIPT(Ent_send_msgout);
827 1.37.2.2 bouyer return(1);
828 1.37.2.2 bouyer case SIOP_NEG_ACK:
829 1.37.2.2 bouyer siop_update_scntl3(sc,
830 1.37.2.2 bouyer siop_cmd->siop_target);
831 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin_ack);
832 1.37.2.2 bouyer return(1);
833 1.37.2.2 bouyer default:
834 1.37.2.2 bouyer panic("invalid retval from "
835 1.37.2.2 bouyer "siop_wdtr_neg()");
836 1.37.2.2 bouyer }
837 1.37.2.2 bouyer return(1);
838 1.37.2.2 bouyer }
839 1.37.2.2 bouyer if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_SDTR) {
840 1.37.2.2 bouyer switch (siop_sdtr_neg(siop_cmd)) {
841 1.37.2.2 bouyer case SIOP_NEG_MSGOUT:
842 1.37.2.2 bouyer siop_update_scntl3(sc,
843 1.37.2.2 bouyer siop_cmd->siop_target);
844 1.37.2.2 bouyer siop_table_sync(siop_cmd,
845 1.37.2.2 bouyer BUS_DMASYNC_PREREAD |
846 1.37.2.2 bouyer BUS_DMASYNC_PREWRITE);
847 1.37.2.2 bouyer CALL_SCRIPT(Ent_send_msgout);
848 1.37.2.2 bouyer return(1);
849 1.37.2.2 bouyer case SIOP_NEG_ACK:
850 1.37.2.2 bouyer siop_update_scntl3(sc,
851 1.37.2.2 bouyer siop_cmd->siop_target);
852 1.37.2.2 bouyer CALL_SCRIPT(Ent_msgin_ack);
853 1.37.2.2 bouyer return(1);
854 1.37.2.2 bouyer default:
855 1.37.2.2 bouyer panic("invalid retval from "
856 1.37.2.2 bouyer "siop_wdtr_neg()");
857 1.37.2.2 bouyer }
858 1.37.2.2 bouyer return(1);
859 1.37.2.2 bouyer }
860 1.37.2.2 bouyer /* send a message reject */
861 1.37.2.2 bouyer siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
862 1.37.2.2 bouyer siop_cmd->siop_tables.t_msgout.count = htole32(1);
863 1.37.2.2 bouyer siop_table_sync(siop_cmd,
864 1.37.2.2 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
865 1.37.2.2 bouyer CALL_SCRIPT(Ent_send_msgout);
866 1.37.2.2 bouyer return 1;
867 1.37.2.2 bouyer case A_int_disc:
868 1.37.2.2 bouyer INCSTAT(siop_stat_intr_sdp);
869 1.37.2.2 bouyer offset = bus_space_read_1(sc->sc_rt, sc->sc_rh,
870 1.37.2.2 bouyer SIOP_SCRATCHA + 1);
871 1.37.2.2 bouyer #ifdef SIOP_DEBUG_DR
872 1.37.2.2 bouyer printf("disconnect offset %d\n", offset);
873 1.37.2.2 bouyer #endif
874 1.37.2.2 bouyer if (offset > SIOP_NSG) {
875 1.37.2.2 bouyer printf("%s: bad offset for disconnect (%d)\n",
876 1.37.2.2 bouyer sc->sc_dev.dv_xname, offset);
877 1.37.2.2 bouyer goto reset;
878 1.37.2.2 bouyer }
879 1.37.2.2 bouyer /*
880 1.37.2.2 bouyer * offset == SIOP_NSG may be a valid condition if
881 1.37.2.2 bouyer * we get a sdp when the xfer is done.
882 1.37.2.2 bouyer * Don't call memmove in this case.
883 1.37.2.2 bouyer */
884 1.37.2.2 bouyer if (offset < SIOP_NSG) {
885 1.37.2.2 bouyer memmove(&siop_cmd->siop_tables.data[0],
886 1.37.2.2 bouyer &siop_cmd->siop_tables.data[offset],
887 1.37.2.2 bouyer (SIOP_NSG - offset) * sizeof(scr_table_t));
888 1.37.2.2 bouyer siop_table_sync(siop_cmd,
889 1.37.2.2 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
890 1.37.2.2 bouyer }
891 1.37.2.2 bouyer CALL_SCRIPT(Ent_script_sched);
892 1.37.2.2 bouyer /* check if we can put some command in scheduler */
893 1.37.2.2 bouyer siop_start(sc);
894 1.37.2.2 bouyer return 1;
895 1.37.2.2 bouyer case A_int_resfail:
896 1.37.2.2 bouyer printf("reselect failed\n");
897 1.37.2.2 bouyer CALL_SCRIPT(Ent_script_sched);
898 1.37.2.2 bouyer return 1;
899 1.37.2.2 bouyer case A_int_done:
900 1.37.2.2 bouyer if (xs == NULL) {
901 1.37.2.2 bouyer printf("%s: done without command, DSA=0x%lx\n",
902 1.37.2.2 bouyer sc->sc_dev.dv_xname, (u_long)siop_cmd->dsa);
903 1.37.2.2 bouyer siop_cmd->status = CMDST_FREE;
904 1.37.2.2 bouyer siop_start(sc);
905 1.37.2.2 bouyer CALL_SCRIPT(Ent_script_sched);
906 1.37.2.2 bouyer return 1;
907 1.37.2.2 bouyer }
908 1.37.2.2 bouyer #ifdef SIOP_DEBUG_INTR
909 1.37.2.2 bouyer printf("done, DSA=0x%lx target id 0x%x last msg "
910 1.37.2.2 bouyer "in=0x%x status=0x%x\n", (u_long)siop_cmd->dsa,
911 1.37.2.2 bouyer le32toh(siop_cmd->siop_tables.id),
912 1.37.2.2 bouyer siop_cmd->siop_tables.msg_in[0],
913 1.37.2.2 bouyer le32toh(siop_cmd->siop_tables.status));
914 1.37.2.2 bouyer #endif
915 1.37.2.2 bouyer INCSTAT(siop_stat_intr_done);
916 1.37.2.6 bouyer siop_cmd->status = CMDST_DONE;
917 1.37.2.2 bouyer goto end;
918 1.37.2.2 bouyer default:
919 1.37.2.2 bouyer printf("unknown irqcode %x\n", irqcode);
920 1.37.2.2 bouyer if (xs) {
921 1.37.2.2 bouyer xs->error = XS_SELTIMEOUT;
922 1.37.2.2 bouyer goto end;
923 1.37.2.2 bouyer }
924 1.37.2.2 bouyer goto reset;
925 1.37.2.2 bouyer }
926 1.37.2.2 bouyer return 1;
927 1.37.2.2 bouyer }
928 1.37.2.2 bouyer /* We just should't get there */
929 1.37.2.2 bouyer panic("siop_intr: I shouldn't be there !");
930 1.37.2.2 bouyer return 1;
931 1.37.2.2 bouyer end:
932 1.37.2.2 bouyer CALL_SCRIPT(Ent_script_sched);
933 1.37.2.2 bouyer siop_lun->siop_tag[tag].active = NULL;
934 1.37.2.5 bouyer siop_scsicmd_end(siop_cmd);
935 1.37.2.2 bouyer if (siop_cmd->status == CMDST_FREE) {
936 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
937 1.37.2.2 bouyer if (freetarget && siop_target->status == TARST_PROBING)
938 1.37.2.2 bouyer siop_del_dev(sc, target, lun);
939 1.37.2.2 bouyer }
940 1.37.2.2 bouyer siop_start(sc);
941 1.37.2.2 bouyer return 1;
942 1.37.2.2 bouyer }
943 1.37.2.2 bouyer
944 1.37.2.2 bouyer void
945 1.37.2.2 bouyer siop_scsicmd_end(siop_cmd)
946 1.37.2.2 bouyer struct siop_cmd *siop_cmd;
947 1.37.2.2 bouyer {
948 1.37.2.2 bouyer struct scsipi_xfer *xs = siop_cmd->xs;
949 1.37.2.2 bouyer struct siop_softc *sc = siop_cmd->siop_sc;
950 1.37.2.2 bouyer
951 1.37.2.5 bouyer xs->status = le32toh(siop_cmd->siop_tables.status);
952 1.37.2.5 bouyer switch(xs->status) {
953 1.37.2.2 bouyer case SCSI_OK:
954 1.37.2.6 bouyer xs->error = XS_NOERROR;
955 1.37.2.2 bouyer break;
956 1.37.2.2 bouyer case SCSI_BUSY:
957 1.37.2.2 bouyer xs->error = XS_BUSY;
958 1.37.2.2 bouyer break;
959 1.37.2.2 bouyer case SCSI_CHECK:
960 1.37.2.6 bouyer xs->error = XS_BUSY;
961 1.37.2.2 bouyer break;
962 1.37.2.2 bouyer case SCSI_QUEUE_FULL:
963 1.37.2.2 bouyer INCSTAT(siop_stat_intr_qfull);
964 1.37.2.2 bouyer #ifdef SIOP_DEBUG
965 1.37.2.2 bouyer printf("%s:%d:%d: queue full (tag %d)\n", sc->sc_dev.dv_xname,
966 1.37.2.4 bouyer xs->xs_periph->periph_target,
967 1.37.2.4 bouyer xs->xs_periph->periph_lun, siop_cmd->tag);
968 1.37.2.2 bouyer #endif
969 1.37.2.5 bouyer xs->error = XS_BUSY;
970 1.37.2.5 bouyer break;
971 1.37.2.2 bouyer case SCSI_SIOP_NOCHECK:
972 1.37.2.2 bouyer /*
973 1.37.2.2 bouyer * don't check status, xs->error is already valid
974 1.37.2.2 bouyer */
975 1.37.2.2 bouyer break;
976 1.37.2.2 bouyer case SCSI_SIOP_NOSTATUS:
977 1.37.2.2 bouyer /*
978 1.37.2.2 bouyer * the status byte was not updated, cmd was
979 1.37.2.2 bouyer * aborted
980 1.37.2.2 bouyer */
981 1.37.2.2 bouyer xs->error = XS_SELTIMEOUT;
982 1.37.2.2 bouyer break;
983 1.37.2.2 bouyer default:
984 1.37.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
985 1.37.2.2 bouyer }
986 1.37.2.6 bouyer if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
987 1.37.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
988 1.37.2.2 bouyer siop_cmd->dmamap_data->dm_mapsize,
989 1.37.2.2 bouyer (xs->xs_control & XS_CTL_DATA_IN) ?
990 1.37.2.2 bouyer BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
991 1.37.2.2 bouyer bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_data);
992 1.37.2.2 bouyer }
993 1.37.2.2 bouyer bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
994 1.37.2.2 bouyer callout_stop(&siop_cmd->xs->xs_callout);
995 1.37.2.2 bouyer siop_cmd->status = CMDST_FREE;
996 1.37.2.2 bouyer xs->resid = 0;
997 1.37.2.5 bouyer scsipi_done (xs);
998 1.37.2.2 bouyer }
999 1.37.2.2 bouyer
1000 1.37.2.2 bouyer /*
1001 1.37.2.2 bouyer * handle a rejected queue tag message: the command will run untagged,
1002 1.37.2.2 bouyer * has to adjust the reselect script.
1003 1.37.2.2 bouyer */
1004 1.37.2.2 bouyer int
1005 1.37.2.2 bouyer siop_handle_qtag_reject(siop_cmd)
1006 1.37.2.2 bouyer struct siop_cmd *siop_cmd;
1007 1.37.2.2 bouyer {
1008 1.37.2.2 bouyer struct siop_softc *sc = siop_cmd->siop_sc;
1009 1.37.2.4 bouyer int target = siop_cmd->xs->xs_periph->periph_target;
1010 1.37.2.4 bouyer int lun = siop_cmd->xs->xs_periph->periph_lun;
1011 1.37.2.2 bouyer int tag = siop_cmd->siop_tables.msg_out[2];
1012 1.37.2.2 bouyer struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
1013 1.37.2.2 bouyer
1014 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1015 1.37.2.2 bouyer printf("%s:%d:%d: tag message %d (%d) rejected (status %d)\n",
1016 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun, tag, siop_cmd->tag,
1017 1.37.2.2 bouyer siop_cmd->status);
1018 1.37.2.2 bouyer #endif
1019 1.37.2.2 bouyer
1020 1.37.2.2 bouyer if (siop_lun->siop_tag[0].active != NULL) {
1021 1.37.2.2 bouyer printf("%s: untagged command already running for target %d "
1022 1.37.2.2 bouyer "lun %d (status %d)\n", sc->sc_dev.dv_xname, target, lun,
1023 1.37.2.2 bouyer siop_lun->siop_tag[0].active->status);
1024 1.37.2.2 bouyer return -1;
1025 1.37.2.2 bouyer }
1026 1.37.2.2 bouyer /* clear tag slot */
1027 1.37.2.2 bouyer siop_lun->siop_tag[tag].active = NULL;
1028 1.37.2.2 bouyer /* add command to non-tagged slot */
1029 1.37.2.2 bouyer siop_lun->siop_tag[0].active = siop_cmd;
1030 1.37.2.2 bouyer siop_cmd->tag = 0;
1031 1.37.2.2 bouyer /* adjust reselect script if there is one */
1032 1.37.2.2 bouyer if (siop_lun->siop_tag[0].reseloff > 0) {
1033 1.37.2.2 bouyer siop_script_write(sc,
1034 1.37.2.2 bouyer siop_lun->siop_tag[0].reseloff + 1,
1035 1.37.2.2 bouyer siop_cmd->dsa + sizeof(struct siop_xfer_common) +
1036 1.37.2.2 bouyer Ent_ldsa_reload_dsa);
1037 1.37.2.2 bouyer }
1038 1.37.2.2 bouyer return 0;
1039 1.37.2.2 bouyer }
1040 1.37.2.2 bouyer
1041 1.37.2.2 bouyer /*
1042 1.37.2.2 bouyer * handle a bus reset: reset chip, unqueue all active commands, free all
1043 1.37.2.2 bouyer * target struct and report loosage to upper layer.
1044 1.37.2.2 bouyer * As the upper layer may requeue immediatly we have to first store
1045 1.37.2.2 bouyer * all active commands in a temporary queue.
1046 1.37.2.2 bouyer */
1047 1.37.2.2 bouyer void
1048 1.37.2.2 bouyer siop_handle_reset(sc)
1049 1.37.2.2 bouyer struct siop_softc *sc;
1050 1.37.2.2 bouyer {
1051 1.37.2.2 bouyer struct cmd_list reset_list;
1052 1.37.2.2 bouyer struct siop_cmd *siop_cmd, *next_siop_cmd;
1053 1.37.2.2 bouyer struct siop_lun *siop_lun;
1054 1.37.2.2 bouyer int target, lun, tag;
1055 1.37.2.2 bouyer /*
1056 1.37.2.2 bouyer * scsi bus reset. reset the chip and restart
1057 1.37.2.2 bouyer * the queue. Need to clean up all active commands
1058 1.37.2.2 bouyer */
1059 1.37.2.2 bouyer printf("%s: scsi bus reset\n", sc->sc_dev.dv_xname);
1060 1.37.2.2 bouyer /* stop, reset and restart the chip */
1061 1.37.2.2 bouyer siop_reset(sc);
1062 1.37.2.2 bouyer TAILQ_INIT(&reset_list);
1063 1.37.2.2 bouyer /*
1064 1.37.2.2 bouyer * Process all commands: first commmands being executed
1065 1.37.2.2 bouyer */
1066 1.37.2.4 bouyer for (target = 0; target < sc->sc_chan.chan_ntargets;
1067 1.37.2.2 bouyer target++) {
1068 1.37.2.2 bouyer if (sc->targets[target] == NULL)
1069 1.37.2.2 bouyer continue;
1070 1.37.2.2 bouyer for (lun = 0; lun < 8; lun++) {
1071 1.37.2.2 bouyer siop_lun = sc->targets[target]->siop_lun[lun];
1072 1.37.2.2 bouyer if (siop_lun == NULL)
1073 1.37.2.2 bouyer continue;
1074 1.37.2.2 bouyer for (tag = 0; tag <
1075 1.37.2.2 bouyer ((sc->targets[target]->flags & TARF_TAG) ?
1076 1.37.2.2 bouyer SIOP_NTAG : 1);
1077 1.37.2.2 bouyer tag++) {
1078 1.37.2.2 bouyer siop_cmd = siop_lun->siop_tag[tag].active;
1079 1.37.2.2 bouyer if (siop_cmd == NULL)
1080 1.37.2.2 bouyer continue;
1081 1.37.2.2 bouyer printf("cmd %p (target %d:%d) in reset list\n",
1082 1.37.2.2 bouyer siop_cmd, target, lun);
1083 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
1084 1.37.2.2 bouyer siop_lun->siop_tag[tag].active = NULL;
1085 1.37.2.2 bouyer }
1086 1.37.2.2 bouyer }
1087 1.37.2.2 bouyer sc->targets[target]->status = TARST_ASYNC;
1088 1.37.2.2 bouyer sc->targets[target]->flags &= ~TARF_ISWIDE;
1089 1.37.2.4 bouyer sc->targets[target]->period = sc->targets[target]->offset = 0;
1090 1.37.2.5 bouyer siop_update_xfer_mode(sc, target);
1091 1.37.2.2 bouyer }
1092 1.37.2.2 bouyer /* Next commands from the urgent list */
1093 1.37.2.2 bouyer for (siop_cmd = TAILQ_FIRST(&sc->urgent_list); siop_cmd != NULL;
1094 1.37.2.2 bouyer siop_cmd = next_siop_cmd) {
1095 1.37.2.2 bouyer next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
1096 1.37.2.2 bouyer siop_cmd->flags &= ~CMDFL_TAG;
1097 1.37.2.2 bouyer printf("cmd %p (target %d:%d) in reset list (wait)\n",
1098 1.37.2.4 bouyer siop_cmd, siop_cmd->xs->xs_periph->periph_target,
1099 1.37.2.4 bouyer siop_cmd->xs->xs_periph->periph_lun);
1100 1.37.2.2 bouyer TAILQ_REMOVE(&sc->urgent_list, siop_cmd, next);
1101 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
1102 1.37.2.2 bouyer }
1103 1.37.2.2 bouyer /* Then command waiting in the input list */
1104 1.37.2.2 bouyer for (siop_cmd = TAILQ_FIRST(&sc->ready_list); siop_cmd != NULL;
1105 1.37.2.2 bouyer siop_cmd = next_siop_cmd) {
1106 1.37.2.2 bouyer next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
1107 1.37.2.2 bouyer siop_cmd->flags &= ~CMDFL_TAG;
1108 1.37.2.2 bouyer printf("cmd %p (target %d:%d) in reset list (wait)\n",
1109 1.37.2.4 bouyer siop_cmd, siop_cmd->xs->xs_periph->periph_target,
1110 1.37.2.4 bouyer siop_cmd->xs->xs_periph->periph_lun);
1111 1.37.2.2 bouyer TAILQ_REMOVE(&sc->ready_list, siop_cmd, next);
1112 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&reset_list, siop_cmd, next);
1113 1.37.2.2 bouyer }
1114 1.37.2.2 bouyer
1115 1.37.2.2 bouyer for (siop_cmd = TAILQ_FIRST(&reset_list); siop_cmd != NULL;
1116 1.37.2.2 bouyer siop_cmd = next_siop_cmd) {
1117 1.37.2.2 bouyer next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
1118 1.37.2.2 bouyer siop_cmd->xs->error = (siop_cmd->flags & CMDFL_TIMEOUT) ?
1119 1.37.2.2 bouyer XS_TIMEOUT : XS_RESET;
1120 1.37.2.2 bouyer siop_cmd->siop_tables.status = htole32(SCSI_SIOP_NOCHECK);
1121 1.37.2.2 bouyer printf("cmd %p (status %d) about to be processed\n", siop_cmd,
1122 1.37.2.2 bouyer siop_cmd->status);
1123 1.37.2.6 bouyer siop_cmd->status = CMDST_DONE;
1124 1.37.2.2 bouyer TAILQ_REMOVE(&reset_list, siop_cmd, next);
1125 1.37.2.2 bouyer siop_scsicmd_end(siop_cmd);
1126 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
1127 1.37.2.2 bouyer }
1128 1.37.2.6 bouyer scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_RESET, NULL);
1129 1.37.2.2 bouyer }
1130 1.37.2.2 bouyer
1131 1.37.2.4 bouyer void
1132 1.37.2.4 bouyer siop_scsipi_request(chan, req, arg)
1133 1.37.2.4 bouyer struct scsipi_channel *chan;
1134 1.37.2.4 bouyer scsipi_adapter_req_t req;
1135 1.37.2.4 bouyer void *arg;
1136 1.37.2.2 bouyer {
1137 1.37.2.4 bouyer struct scsipi_xfer *xs;
1138 1.37.2.4 bouyer struct scsipi_periph *periph;
1139 1.37.2.4 bouyer struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev;
1140 1.37.2.2 bouyer struct siop_cmd *siop_cmd;
1141 1.37.2.2 bouyer int s, error, i;
1142 1.37.2.4 bouyer int target;
1143 1.37.2.4 bouyer int lun;
1144 1.37.2.2 bouyer
1145 1.37.2.4 bouyer switch (req) {
1146 1.37.2.4 bouyer case ADAPTER_REQ_RUN_XFER:
1147 1.37.2.4 bouyer xs = arg;
1148 1.37.2.4 bouyer periph = xs->xs_periph;
1149 1.37.2.4 bouyer target = periph->periph_target;
1150 1.37.2.4 bouyer lun = periph->periph_lun;
1151 1.37.2.4 bouyer
1152 1.37.2.4 bouyer s = splbio();
1153 1.37.2.2 bouyer #ifdef SIOP_DEBUG_SCHED
1154 1.37.2.4 bouyer printf("starting cmd for %d:%d\n", target, lun);
1155 1.37.2.2 bouyer #endif
1156 1.37.2.4 bouyer siop_cmd = TAILQ_FIRST(&sc->free_list);
1157 1.37.2.4 bouyer if (siop_cmd) {
1158 1.37.2.4 bouyer TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
1159 1.37.2.4 bouyer } else {
1160 1.37.2.4 bouyer if (siop_morecbd(sc) == 0) {
1161 1.37.2.4 bouyer siop_cmd = TAILQ_FIRST(&sc->free_list);
1162 1.37.2.2 bouyer #ifdef DIAGNOSTIC
1163 1.37.2.4 bouyer if (siop_cmd == NULL)
1164 1.37.2.4 bouyer panic("siop_morecbd succeed and does nothing");
1165 1.37.2.2 bouyer #endif
1166 1.37.2.4 bouyer TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
1167 1.37.2.4 bouyer }
1168 1.37.2.4 bouyer }
1169 1.37.2.4 bouyer if (siop_cmd == NULL) {
1170 1.37.2.4 bouyer xs->error = XS_RESOURCE_SHORTAGE;
1171 1.37.2.4 bouyer splx(s);
1172 1.37.2.4 bouyer return;
1173 1.37.2.2 bouyer }
1174 1.37.2.2 bouyer #ifdef DIAGNOSTIC
1175 1.37.2.4 bouyer if (siop_cmd->status != CMDST_FREE)
1176 1.37.2.4 bouyer panic("siop_scsicmd: new cmd not free");
1177 1.37.2.2 bouyer #endif
1178 1.37.2.4 bouyer if (sc->targets[target] == NULL) {
1179 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1180 1.37.2.4 bouyer printf("%s: alloc siop_target for target %d\n",
1181 1.37.2.4 bouyer sc->sc_dev.dv_xname, target);
1182 1.37.2.2 bouyer #endif
1183 1.37.2.4 bouyer sc->targets[target] =
1184 1.37.2.4 bouyer malloc(sizeof(struct siop_target),
1185 1.37.2.4 bouyer M_DEVBUF, M_NOWAIT);
1186 1.37.2.4 bouyer if (sc->targets[target] == NULL) {
1187 1.37.2.4 bouyer printf("%s: can't malloc memory for "
1188 1.37.2.4 bouyer "target %d\n", sc->sc_dev.dv_xname, target);
1189 1.37.2.4 bouyer xs->error = XS_RESOURCE_SHORTAGE;
1190 1.37.2.4 bouyer splx(s);
1191 1.37.2.4 bouyer return;
1192 1.37.2.4 bouyer }
1193 1.37.2.4 bouyer sc->targets[target]->status = TARST_PROBING;
1194 1.37.2.4 bouyer sc->targets[target]->flags = 0;
1195 1.37.2.4 bouyer sc->targets[target]->id =
1196 1.37.2.4 bouyer sc->clock_div << 24; /* scntl3 */
1197 1.37.2.4 bouyer sc->targets[target]->id |= target << 16; /* id */
1198 1.37.2.4 bouyer /* sc->targets[target]->id |= 0x0 << 8; scxfer is 0 */
1199 1.37.2.4 bouyer
1200 1.37.2.4 bouyer /* get a lun switch script */
1201 1.37.2.4 bouyer sc->targets[target]->lunsw = siop_get_lunsw(sc);
1202 1.37.2.4 bouyer if (sc->targets[target]->lunsw == NULL) {
1203 1.37.2.4 bouyer printf("%s: can't alloc lunsw for target %d\n",
1204 1.37.2.4 bouyer sc->sc_dev.dv_xname, target);
1205 1.37.2.4 bouyer xs->error = XS_RESOURCE_SHORTAGE;
1206 1.37.2.4 bouyer splx(s);
1207 1.37.2.4 bouyer return;
1208 1.37.2.4 bouyer }
1209 1.37.2.4 bouyer for (i=0; i < 8; i++)
1210 1.37.2.4 bouyer sc->targets[target]->siop_lun[i] = NULL;
1211 1.37.2.4 bouyer siop_add_reselsw(sc, target);
1212 1.37.2.2 bouyer }
1213 1.37.2.2 bouyer if (sc->targets[target]->siop_lun[lun] == NULL) {
1214 1.37.2.4 bouyer sc->targets[target]->siop_lun[lun] =
1215 1.37.2.4 bouyer malloc(sizeof(struct siop_lun), M_DEVBUF, M_NOWAIT);
1216 1.37.2.4 bouyer if (sc->targets[target]->siop_lun[lun] == NULL) {
1217 1.37.2.4 bouyer printf("%s: can't alloc siop_lun for "
1218 1.37.2.4 bouyer "target %d lun %d\n",
1219 1.37.2.4 bouyer sc->sc_dev.dv_xname, target, lun);
1220 1.37.2.4 bouyer xs->error = XS_RESOURCE_SHORTAGE;
1221 1.37.2.4 bouyer splx(s);
1222 1.37.2.4 bouyer return;
1223 1.37.2.4 bouyer }
1224 1.37.2.4 bouyer memset(sc->targets[target]->siop_lun[lun], 0,
1225 1.37.2.4 bouyer sizeof(struct siop_lun));
1226 1.37.2.4 bouyer }
1227 1.37.2.4 bouyer siop_cmd->siop_target = sc->targets[target];
1228 1.37.2.4 bouyer siop_cmd->xs = xs;
1229 1.37.2.4 bouyer siop_cmd->flags = 0;
1230 1.37.2.4 bouyer siop_cmd->status = CMDST_READY;
1231 1.37.2.4 bouyer
1232 1.37.2.4 bouyer /* load the DMA maps */
1233 1.37.2.4 bouyer error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_cmd,
1234 1.37.2.4 bouyer xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
1235 1.37.2.2 bouyer if (error) {
1236 1.37.2.2 bouyer printf("%s: unable to load cmd DMA map: %d",
1237 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1238 1.37.2.2 bouyer xs->error = XS_DRIVER_STUFFUP;
1239 1.37.2.2 bouyer splx(s);
1240 1.37.2.4 bouyer return;
1241 1.37.2.2 bouyer }
1242 1.37.2.4 bouyer if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
1243 1.37.2.4 bouyer error = bus_dmamap_load(sc->sc_dmat,
1244 1.37.2.4 bouyer siop_cmd->dmamap_data, xs->data, xs->datalen,
1245 1.37.2.4 bouyer NULL, BUS_DMA_NOWAIT);
1246 1.37.2.4 bouyer if (error) {
1247 1.37.2.4 bouyer printf("%s: unable to load cmd DMA map: %d",
1248 1.37.2.4 bouyer sc->sc_dev.dv_xname, error);
1249 1.37.2.4 bouyer xs->error = XS_DRIVER_STUFFUP;
1250 1.37.2.4 bouyer bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
1251 1.37.2.4 bouyer splx(s);
1252 1.37.2.4 bouyer return;
1253 1.37.2.4 bouyer }
1254 1.37.2.4 bouyer bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
1255 1.37.2.4 bouyer siop_cmd->dmamap_data->dm_mapsize,
1256 1.37.2.4 bouyer (xs->xs_control & XS_CTL_DATA_IN) ?
1257 1.37.2.4 bouyer BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1258 1.37.2.4 bouyer }
1259 1.37.2.4 bouyer bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_cmd, 0,
1260 1.37.2.4 bouyer siop_cmd->dmamap_cmd->dm_mapsize, BUS_DMASYNC_PREWRITE);
1261 1.37.2.2 bouyer
1262 1.37.2.4 bouyer siop_setuptables(siop_cmd);
1263 1.37.2.2 bouyer
1264 1.37.2.4 bouyer TAILQ_INSERT_TAIL(&sc->ready_list, siop_cmd, next);
1265 1.37.2.4 bouyer siop_start(sc);
1266 1.37.2.4 bouyer if (xs->xs_control & XS_CTL_POLL) {
1267 1.37.2.4 bouyer /* poll for command completion */
1268 1.37.2.4 bouyer while ((xs->xs_status & XS_STS_DONE) == 0) {
1269 1.37.2.4 bouyer delay(1000);
1270 1.37.2.4 bouyer siop_intr(sc);
1271 1.37.2.4 bouyer }
1272 1.37.2.4 bouyer }
1273 1.37.2.4 bouyer splx(s);
1274 1.37.2.4 bouyer return;
1275 1.37.2.4 bouyer
1276 1.37.2.4 bouyer case ADAPTER_REQ_GROW_RESOURCES:
1277 1.37.2.4 bouyer /* XXX Not supported. */
1278 1.37.2.4 bouyer return;
1279 1.37.2.4 bouyer
1280 1.37.2.4 bouyer case ADAPTER_REQ_SET_XFER_MODE:
1281 1.37.2.4 bouyer {
1282 1.37.2.4 bouyer struct scsipi_xfer_mode *xm = arg;
1283 1.37.2.4 bouyer if (sc->targets[xm->xm_target] == NULL)
1284 1.37.2.4 bouyer return;
1285 1.37.2.4 bouyer s = splbio();
1286 1.37.2.5 bouyer if (xm->xm_mode & PERIPH_CAP_TQING)
1287 1.37.2.4 bouyer sc->targets[xm->xm_target]->flags |= TARF_TAG;
1288 1.37.2.4 bouyer if ((xm->xm_mode & PERIPH_CAP_WIDE16) &&
1289 1.37.2.4 bouyer (sc->features & SF_BUS_WIDE))
1290 1.37.2.4 bouyer sc->targets[xm->xm_target]->flags |= TARF_WIDE;
1291 1.37.2.4 bouyer if (xm->xm_mode & PERIPH_CAP_SYNC)
1292 1.37.2.4 bouyer sc->targets[xm->xm_target]->flags |= TARF_SYNC;
1293 1.37.2.4 bouyer if ((xm->xm_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_WIDE16)) ||
1294 1.37.2.4 bouyer sc->targets[xm->xm_target]->status == TARST_PROBING)
1295 1.37.2.4 bouyer sc->targets[xm->xm_target]->status =
1296 1.37.2.4 bouyer TARST_ASYNC;
1297 1.37.2.4 bouyer
1298 1.37.2.4 bouyer for (lun = 0; lun < sc->sc_chan.chan_nluns; lun++) {
1299 1.37.2.4 bouyer if (sc->sc_chan.chan_periphs[xm->xm_target][lun])
1300 1.37.2.4 bouyer /* allocate a lun sw entry for this device */
1301 1.37.2.4 bouyer siop_add_dev(sc, xm->xm_target, lun);
1302 1.37.2.2 bouyer }
1303 1.37.2.4 bouyer
1304 1.37.2.2 bouyer splx(s);
1305 1.37.2.2 bouyer }
1306 1.37.2.4 bouyer }
1307 1.37.2.2 bouyer }
1308 1.37.2.2 bouyer
1309 1.37.2.2 bouyer void
1310 1.37.2.2 bouyer siop_start(sc)
1311 1.37.2.2 bouyer struct siop_softc *sc;
1312 1.37.2.2 bouyer {
1313 1.37.2.2 bouyer struct siop_cmd *siop_cmd, *next_siop_cmd;
1314 1.37.2.2 bouyer struct siop_lun *siop_lun;
1315 1.37.2.2 bouyer u_int32_t dsa;
1316 1.37.2.2 bouyer int timeout;
1317 1.37.2.2 bouyer int target, lun, tag, slot;
1318 1.37.2.2 bouyer int newcmd = 0;
1319 1.37.2.2 bouyer int doingready = 0;
1320 1.37.2.2 bouyer
1321 1.37.2.2 bouyer /*
1322 1.37.2.2 bouyer * first make sure to read valid data
1323 1.37.2.2 bouyer */
1324 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1325 1.37.2.2 bouyer
1326 1.37.2.2 bouyer /*
1327 1.37.2.2 bouyer * The queue management here is a bit tricky: the script always looks
1328 1.37.2.2 bouyer * at the slot from first to last, so if we always use the first
1329 1.37.2.2 bouyer * free slot commands can stay at the tail of the queue ~forever.
1330 1.37.2.2 bouyer * The algorithm used here is to restart from the head when we know
1331 1.37.2.2 bouyer * that the queue is empty, and only add commands after the last one.
1332 1.37.2.2 bouyer * When we're at the end of the queue wait for the script to clear it.
1333 1.37.2.2 bouyer * The best thing to do here would be to implement a circular queue,
1334 1.37.2.2 bouyer * but using only 53c720 features this can be "interesting".
1335 1.37.2.2 bouyer * A mid-way solution could be to implement 2 queues and swap orders.
1336 1.37.2.2 bouyer */
1337 1.37.2.2 bouyer slot = sc->sc_currschedslot;
1338 1.37.2.2 bouyer /*
1339 1.37.2.2 bouyer * If the instruction is 0x80000000 (JUMP foo, IF FALSE) the slot is
1340 1.37.2.2 bouyer * free. As this is the last used slot, all previous slots are free,
1341 1.37.2.2 bouyer * we can restart from 0.
1342 1.37.2.2 bouyer */
1343 1.37.2.2 bouyer if (siop_script_read(sc, (Ent_script_sched_slot0 / 4) + slot * 2) ==
1344 1.37.2.2 bouyer 0x80000000) {
1345 1.37.2.2 bouyer slot = sc->sc_currschedslot = 0;
1346 1.37.2.2 bouyer } else {
1347 1.37.2.2 bouyer slot++;
1348 1.37.2.2 bouyer }
1349 1.37.2.2 bouyer /* first handle commands from the urgent list */
1350 1.37.2.2 bouyer siop_cmd = TAILQ_FIRST(&sc->urgent_list);
1351 1.37.2.2 bouyer again:
1352 1.37.2.2 bouyer for (; siop_cmd != NULL; siop_cmd = next_siop_cmd) {
1353 1.37.2.2 bouyer next_siop_cmd = TAILQ_NEXT(siop_cmd, next);
1354 1.37.2.2 bouyer #ifdef DIAGNOSTIC
1355 1.37.2.6 bouyer if (siop_cmd->status != CMDST_READY)
1356 1.37.2.2 bouyer panic("siop: non-ready cmd in ready list");
1357 1.37.2.2 bouyer #endif
1358 1.37.2.4 bouyer target = siop_cmd->xs->xs_periph->periph_target;
1359 1.37.2.4 bouyer lun = siop_cmd->xs->xs_periph->periph_lun;
1360 1.37.2.2 bouyer siop_lun = sc->targets[target]->siop_lun[lun];
1361 1.37.2.2 bouyer /* if non-tagged command active, wait */
1362 1.37.2.2 bouyer if (siop_lun->siop_tag[0].active != NULL)
1363 1.37.2.2 bouyer continue;
1364 1.37.2.2 bouyer /* find a free tag if needed */
1365 1.37.2.2 bouyer if (siop_cmd->flags & CMDFL_TAG) {
1366 1.37.2.5 bouyer tag = siop_cmd->xs->xs_tag_id + 1;
1367 1.37.2.5 bouyer #ifdef DIAGNOSTIC
1368 1.37.2.5 bouyer if (siop_lun->siop_tag[tag].active != NULL)
1369 1.37.2.5 bouyer panic("siop_start: tag not free");
1370 1.37.2.5 bouyer if (tag >= SIOP_NTAG) {
1371 1.37.2.5 bouyer scsipi_printaddr(siop_cmd->xs->xs_periph);
1372 1.37.2.5 bouyer printf(": tag id %d\n", tag);
1373 1.37.2.5 bouyer panic("siop_start: invalid tag id");
1374 1.37.2.2 bouyer }
1375 1.37.2.5 bouyer #endif
1376 1.37.2.2 bouyer } else {
1377 1.37.2.2 bouyer tag = 0;
1378 1.37.2.2 bouyer }
1379 1.37.2.2 bouyer siop_cmd->tag = tag;
1380 1.37.2.2 bouyer /* find a free scheduler slot and load it */
1381 1.37.2.2 bouyer for (; slot < SIOP_NSLOTS; slot++) {
1382 1.37.2.2 bouyer /*
1383 1.37.2.2 bouyer * If cmd if 0x80000000 the slot is free
1384 1.37.2.2 bouyer */
1385 1.37.2.2 bouyer if (siop_script_read(sc,
1386 1.37.2.2 bouyer (Ent_script_sched_slot0 / 4) + slot * 2) ==
1387 1.37.2.2 bouyer 0x80000000)
1388 1.37.2.2 bouyer break;
1389 1.37.2.2 bouyer }
1390 1.37.2.2 bouyer /* no more free slot, no need to continue */
1391 1.37.2.2 bouyer if (slot == SIOP_NSLOTS) {
1392 1.37.2.2 bouyer goto end;
1393 1.37.2.2 bouyer }
1394 1.37.2.2 bouyer #ifdef SIOP_DEBUG_SCHED
1395 1.37.2.2 bouyer printf("using slot %d for DSA 0x%lx\n", slot,
1396 1.37.2.2 bouyer (u_long)siop_cmd->dsa);
1397 1.37.2.2 bouyer #endif
1398 1.37.2.2 bouyer /* Ok, we can add the tag message */
1399 1.37.2.2 bouyer if (tag > 0) {
1400 1.37.2.2 bouyer #ifdef DIAGNOSTIC
1401 1.37.2.2 bouyer int msgcount =
1402 1.37.2.2 bouyer le32toh(siop_cmd->siop_tables.t_msgout.count);
1403 1.37.2.2 bouyer if (msgcount != 1)
1404 1.37.2.2 bouyer printf("%s:%d:%d: tag %d with msgcount %d\n",
1405 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun, tag,
1406 1.37.2.2 bouyer msgcount);
1407 1.37.2.2 bouyer #endif
1408 1.37.2.5 bouyer siop_cmd->siop_tables.msg_out[1] =
1409 1.37.2.5 bouyer siop_cmd->xs->xs_tag_type;
1410 1.37.2.2 bouyer siop_cmd->siop_tables.msg_out[2] = tag;
1411 1.37.2.2 bouyer siop_cmd->siop_tables.t_msgout.count = htole32(3);
1412 1.37.2.2 bouyer }
1413 1.37.2.2 bouyer /* note that we started a new command */
1414 1.37.2.2 bouyer newcmd = 1;
1415 1.37.2.2 bouyer /* mark command as active */
1416 1.37.2.2 bouyer if (siop_cmd->status == CMDST_READY) {
1417 1.37.2.2 bouyer siop_cmd->status = CMDST_ACTIVE;
1418 1.37.2.2 bouyer } else
1419 1.37.2.2 bouyer panic("siop_start: bad status");
1420 1.37.2.2 bouyer if (doingready)
1421 1.37.2.2 bouyer TAILQ_REMOVE(&sc->ready_list, siop_cmd, next);
1422 1.37.2.2 bouyer else
1423 1.37.2.2 bouyer TAILQ_REMOVE(&sc->urgent_list, siop_cmd, next);
1424 1.37.2.2 bouyer siop_lun->siop_tag[tag].active = siop_cmd;
1425 1.37.2.2 bouyer /* patch scripts with DSA addr */
1426 1.37.2.2 bouyer dsa = siop_cmd->dsa;
1427 1.37.2.2 bouyer /* first reselect switch, if we have an entry */
1428 1.37.2.2 bouyer if (siop_lun->siop_tag[tag].reseloff > 0)
1429 1.37.2.2 bouyer siop_script_write(sc,
1430 1.37.2.2 bouyer siop_lun->siop_tag[tag].reseloff + 1,
1431 1.37.2.2 bouyer dsa + sizeof(struct siop_xfer_common) +
1432 1.37.2.2 bouyer Ent_ldsa_reload_dsa);
1433 1.37.2.2 bouyer /* CMD script: MOVE MEMORY addr */
1434 1.37.2.2 bouyer siop_cmd->siop_xfer->resel[E_ldsa_abs_slot_Used[0]] =
1435 1.37.2.2 bouyer htole32(sc->sc_scriptaddr + Ent_script_sched_slot0 +
1436 1.37.2.2 bouyer slot * 8);
1437 1.37.2.2 bouyer /* scheduler slot: JUMP ldsa_select */
1438 1.37.2.2 bouyer siop_script_write(sc,
1439 1.37.2.2 bouyer (Ent_script_sched_slot0 / 4) + slot * 2 + 1,
1440 1.37.2.2 bouyer dsa + sizeof(struct siop_xfer_common) + Ent_ldsa_select);
1441 1.37.2.2 bouyer /* handle timeout */
1442 1.37.2.2 bouyer if (siop_cmd->status == CMDST_ACTIVE) {
1443 1.37.2.2 bouyer if ((siop_cmd->xs->xs_control &
1444 1.37.2.2 bouyer XS_CTL_POLL) == 0) {
1445 1.37.2.2 bouyer /* start exire timer */
1446 1.37.2.2 bouyer timeout = (u_int64_t) siop_cmd->xs->timeout *
1447 1.37.2.2 bouyer (u_int64_t)hz / 1000;
1448 1.37.2.2 bouyer if (timeout == 0)
1449 1.37.2.2 bouyer timeout = 1;
1450 1.37.2.2 bouyer callout_reset( &siop_cmd->xs->xs_callout,
1451 1.37.2.2 bouyer timeout, siop_timeout, siop_cmd);
1452 1.37.2.2 bouyer }
1453 1.37.2.2 bouyer }
1454 1.37.2.2 bouyer /*
1455 1.37.2.2 bouyer * Change JUMP cmd so that this slot will be handled
1456 1.37.2.2 bouyer */
1457 1.37.2.2 bouyer siop_script_write(sc, (Ent_script_sched_slot0 / 4) + slot * 2,
1458 1.37.2.2 bouyer 0x80080000);
1459 1.37.2.2 bouyer sc->sc_currschedslot = slot;
1460 1.37.2.2 bouyer slot++;
1461 1.37.2.2 bouyer }
1462 1.37.2.2 bouyer if (doingready == 0) {
1463 1.37.2.2 bouyer /* now process ready list */
1464 1.37.2.2 bouyer doingready = 1;
1465 1.37.2.2 bouyer siop_cmd = TAILQ_FIRST(&sc->ready_list);
1466 1.37.2.2 bouyer goto again;
1467 1.37.2.2 bouyer }
1468 1.37.2.2 bouyer
1469 1.37.2.2 bouyer end:
1470 1.37.2.2 bouyer /* if nothing changed no need to flush cache and wakeup script */
1471 1.37.2.2 bouyer if (newcmd == 0)
1472 1.37.2.2 bouyer return;
1473 1.37.2.2 bouyer /* make sure SCRIPT processor will read valid data */
1474 1.37.2.2 bouyer siop_script_sync(sc,BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1475 1.37.2.2 bouyer /* Signal script it has some work to do */
1476 1.37.2.2 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SIGP);
1477 1.37.2.2 bouyer /* and wait for IRQ */
1478 1.37.2.2 bouyer return;
1479 1.37.2.2 bouyer }
1480 1.37.2.2 bouyer
1481 1.37.2.2 bouyer void
1482 1.37.2.2 bouyer siop_timeout(v)
1483 1.37.2.2 bouyer void *v;
1484 1.37.2.2 bouyer {
1485 1.37.2.2 bouyer struct siop_cmd *siop_cmd = v;
1486 1.37.2.2 bouyer struct siop_softc *sc = siop_cmd->siop_sc;
1487 1.37.2.2 bouyer int s;
1488 1.37.2.2 bouyer
1489 1.37.2.4 bouyer scsipi_printaddr(siop_cmd->xs->xs_periph);
1490 1.37.2.2 bouyer printf("command timeout\n");
1491 1.37.2.2 bouyer
1492 1.37.2.2 bouyer s = splbio();
1493 1.37.2.2 bouyer /* reset the scsi bus */
1494 1.37.2.2 bouyer siop_resetbus(sc);
1495 1.37.2.2 bouyer
1496 1.37.2.2 bouyer /* deactivate callout */
1497 1.37.2.2 bouyer callout_stop(&siop_cmd->xs->xs_callout);
1498 1.37.2.2 bouyer /* mark command as being timed out; siop_intr will handle it */
1499 1.37.2.2 bouyer /*
1500 1.37.2.2 bouyer * mark command has being timed out and just return;
1501 1.37.2.2 bouyer * the bus reset will generate an interrupt,
1502 1.37.2.2 bouyer * it will be handled in siop_intr()
1503 1.37.2.2 bouyer */
1504 1.37.2.2 bouyer siop_cmd->flags |= CMDFL_TIMEOUT;
1505 1.37.2.2 bouyer splx(s);
1506 1.37.2.2 bouyer return;
1507 1.37.2.2 bouyer
1508 1.37.2.2 bouyer }
1509 1.37.2.2 bouyer
1510 1.37.2.2 bouyer void
1511 1.37.2.2 bouyer siop_dump_script(sc)
1512 1.37.2.2 bouyer struct siop_softc *sc;
1513 1.37.2.2 bouyer {
1514 1.37.2.2 bouyer int i;
1515 1.37.2.3 bouyer for (i = 0; i < PAGE_SIZE / 4; i += 2) {
1516 1.37.2.2 bouyer printf("0x%04x: 0x%08x 0x%08x", i * 4,
1517 1.37.2.2 bouyer le32toh(sc->sc_script[i]), le32toh(sc->sc_script[i+1]));
1518 1.37.2.2 bouyer if ((le32toh(sc->sc_script[i]) & 0xe0000000) == 0xc0000000) {
1519 1.37.2.2 bouyer i++;
1520 1.37.2.2 bouyer printf(" 0x%08x", le32toh(sc->sc_script[i+1]));
1521 1.37.2.2 bouyer }
1522 1.37.2.2 bouyer printf("\n");
1523 1.37.2.2 bouyer }
1524 1.37.2.2 bouyer }
1525 1.37.2.2 bouyer
1526 1.37.2.2 bouyer int
1527 1.37.2.2 bouyer siop_morecbd(sc)
1528 1.37.2.2 bouyer struct siop_softc *sc;
1529 1.37.2.2 bouyer {
1530 1.37.2.2 bouyer int error, i, j;
1531 1.37.2.2 bouyer bus_dma_segment_t seg;
1532 1.37.2.2 bouyer int rseg;
1533 1.37.2.2 bouyer struct siop_cbd *newcbd;
1534 1.37.2.2 bouyer bus_addr_t dsa;
1535 1.37.2.2 bouyer u_int32_t *scr;
1536 1.37.2.2 bouyer
1537 1.37.2.2 bouyer /* allocate a new list head */
1538 1.37.2.2 bouyer newcbd = malloc(sizeof(struct siop_cbd), M_DEVBUF, M_NOWAIT);
1539 1.37.2.2 bouyer if (newcbd == NULL) {
1540 1.37.2.2 bouyer printf("%s: can't allocate memory for command descriptors "
1541 1.37.2.2 bouyer "head\n", sc->sc_dev.dv_xname);
1542 1.37.2.2 bouyer return ENOMEM;
1543 1.37.2.2 bouyer }
1544 1.37.2.2 bouyer memset(newcbd, 0, sizeof(struct siop_cbd));
1545 1.37.2.2 bouyer
1546 1.37.2.2 bouyer /* allocate cmd list */
1547 1.37.2.2 bouyer newcbd->cmds =
1548 1.37.2.2 bouyer malloc(sizeof(struct siop_cmd) * SIOP_NCMDPB, M_DEVBUF, M_NOWAIT);
1549 1.37.2.2 bouyer if (newcbd->cmds == NULL) {
1550 1.37.2.2 bouyer printf("%s: can't allocate memory for command descriptors\n",
1551 1.37.2.2 bouyer sc->sc_dev.dv_xname);
1552 1.37.2.2 bouyer error = ENOMEM;
1553 1.37.2.2 bouyer goto bad3;
1554 1.37.2.2 bouyer }
1555 1.37.2.2 bouyer memset(newcbd->cmds, 0, sizeof(struct siop_cmd) * SIOP_NCMDPB);
1556 1.37.2.3 bouyer error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg,
1557 1.37.2.3 bouyer 1, &rseg, BUS_DMA_NOWAIT);
1558 1.37.2.2 bouyer if (error) {
1559 1.37.2.2 bouyer printf("%s: unable to allocate cbd DMA memory, error = %d\n",
1560 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1561 1.37.2.2 bouyer goto bad2;
1562 1.37.2.2 bouyer }
1563 1.37.2.3 bouyer error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
1564 1.37.2.2 bouyer (caddr_t *)&newcbd->xfers, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
1565 1.37.2.2 bouyer if (error) {
1566 1.37.2.2 bouyer printf("%s: unable to map cbd DMA memory, error = %d\n",
1567 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1568 1.37.2.2 bouyer goto bad2;
1569 1.37.2.2 bouyer }
1570 1.37.2.3 bouyer error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
1571 1.37.2.2 bouyer BUS_DMA_NOWAIT, &newcbd->xferdma);
1572 1.37.2.2 bouyer if (error) {
1573 1.37.2.2 bouyer printf("%s: unable to create cbd DMA map, error = %d\n",
1574 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1575 1.37.2.2 bouyer goto bad1;
1576 1.37.2.2 bouyer }
1577 1.37.2.2 bouyer error = bus_dmamap_load(sc->sc_dmat, newcbd->xferdma, newcbd->xfers,
1578 1.37.2.3 bouyer PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
1579 1.37.2.2 bouyer if (error) {
1580 1.37.2.2 bouyer printf("%s: unable to load cbd DMA map, error = %d\n",
1581 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1582 1.37.2.2 bouyer goto bad0;
1583 1.37.2.2 bouyer }
1584 1.37.2.2 bouyer #ifdef DEBUG
1585 1.37.2.2 bouyer printf("%s: alloc newcdb at PHY addr 0x%lx\n", sc->sc_dev.dv_xname,
1586 1.37.2.2 bouyer (unsigned long)newcbd->xferdma->dm_segs[0].ds_addr);
1587 1.37.2.2 bouyer #endif
1588 1.37.2.2 bouyer
1589 1.37.2.2 bouyer for (i = 0; i < SIOP_NCMDPB; i++) {
1590 1.37.2.2 bouyer error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SIOP_NSG,
1591 1.37.2.2 bouyer MAXPHYS, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
1592 1.37.2.2 bouyer &newcbd->cmds[i].dmamap_data);
1593 1.37.2.2 bouyer if (error) {
1594 1.37.2.2 bouyer printf("%s: unable to create data DMA map for cbd: "
1595 1.37.2.2 bouyer "error %d\n",
1596 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1597 1.37.2.2 bouyer goto bad0;
1598 1.37.2.2 bouyer }
1599 1.37.2.2 bouyer error = bus_dmamap_create(sc->sc_dmat,
1600 1.37.2.2 bouyer sizeof(struct scsipi_generic), 1,
1601 1.37.2.2 bouyer sizeof(struct scsipi_generic), 0,
1602 1.37.2.2 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
1603 1.37.2.2 bouyer &newcbd->cmds[i].dmamap_cmd);
1604 1.37.2.2 bouyer if (error) {
1605 1.37.2.2 bouyer printf("%s: unable to create cmd DMA map for cbd %d\n",
1606 1.37.2.2 bouyer sc->sc_dev.dv_xname, error);
1607 1.37.2.2 bouyer goto bad0;
1608 1.37.2.2 bouyer }
1609 1.37.2.2 bouyer newcbd->cmds[i].siop_sc = sc;
1610 1.37.2.2 bouyer newcbd->cmds[i].siop_cbdp = newcbd;
1611 1.37.2.2 bouyer newcbd->cmds[i].siop_xfer = &newcbd->xfers[i];
1612 1.37.2.2 bouyer memset(newcbd->cmds[i].siop_xfer, 0,
1613 1.37.2.2 bouyer sizeof(struct siop_xfer));
1614 1.37.2.2 bouyer newcbd->cmds[i].dsa = newcbd->xferdma->dm_segs[0].ds_addr +
1615 1.37.2.2 bouyer i * sizeof(struct siop_xfer);
1616 1.37.2.2 bouyer dsa = newcbd->cmds[i].dsa;
1617 1.37.2.2 bouyer newcbd->cmds[i].status = CMDST_FREE;
1618 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_msgout.count= htole32(1);
1619 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_msgout.addr = htole32(dsa);
1620 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_msgin.count= htole32(1);
1621 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_msgin.addr = htole32(dsa + 8);
1622 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_extmsgin.count= htole32(2);
1623 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_extmsgin.addr = htole32(dsa + 9);
1624 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_extmsgdata.addr =
1625 1.37.2.2 bouyer htole32(dsa + 11);
1626 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_status.count= htole32(1);
1627 1.37.2.2 bouyer newcbd->cmds[i].siop_tables.t_status.addr = htole32(dsa + 16);
1628 1.37.2.2 bouyer
1629 1.37.2.2 bouyer /* The select/reselect script */
1630 1.37.2.2 bouyer scr = &newcbd->cmds[i].siop_xfer->resel[0];
1631 1.37.2.2 bouyer for (j = 0; j < sizeof(load_dsa) / sizeof(load_dsa[0]); j++)
1632 1.37.2.2 bouyer scr[j] = htole32(load_dsa[j]);
1633 1.37.2.2 bouyer /*
1634 1.37.2.2 bouyer * 0x78000000 is a 'move data8 to reg'. data8 is the second
1635 1.37.2.2 bouyer * octet, reg offset is the third.
1636 1.37.2.2 bouyer */
1637 1.37.2.2 bouyer scr[Ent_rdsa0 / 4] =
1638 1.37.2.2 bouyer htole32(0x78100000 | ((dsa & 0x000000ff) << 8));
1639 1.37.2.2 bouyer scr[Ent_rdsa1 / 4] =
1640 1.37.2.2 bouyer htole32(0x78110000 | ( dsa & 0x0000ff00 ));
1641 1.37.2.2 bouyer scr[Ent_rdsa2 / 4] =
1642 1.37.2.2 bouyer htole32(0x78120000 | ((dsa & 0x00ff0000) >> 8));
1643 1.37.2.2 bouyer scr[Ent_rdsa3 / 4] =
1644 1.37.2.2 bouyer htole32(0x78130000 | ((dsa & 0xff000000) >> 16));
1645 1.37.2.2 bouyer scr[E_ldsa_abs_reselected_Used[0]] =
1646 1.37.2.2 bouyer htole32(sc->sc_scriptaddr + Ent_reselected);
1647 1.37.2.2 bouyer scr[E_ldsa_abs_reselect_Used[0]] =
1648 1.37.2.2 bouyer htole32(sc->sc_scriptaddr + Ent_reselect);
1649 1.37.2.2 bouyer scr[E_ldsa_abs_selected_Used[0]] =
1650 1.37.2.2 bouyer htole32(sc->sc_scriptaddr + Ent_selected);
1651 1.37.2.2 bouyer scr[E_ldsa_abs_data_Used[0]] =
1652 1.37.2.2 bouyer htole32(dsa + sizeof(struct siop_xfer_common) +
1653 1.37.2.2 bouyer Ent_ldsa_data);
1654 1.37.2.2 bouyer /* JUMP foo, IF FALSE - used by MOVE MEMORY to clear the slot */
1655 1.37.2.2 bouyer scr[Ent_ldsa_data / 4] = htole32(0x80000000);
1656 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&sc->free_list, &newcbd->cmds[i], next);
1657 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1658 1.37.2.2 bouyer printf("tables[%d]: in=0x%x out=0x%x status=0x%x\n", i,
1659 1.37.2.2 bouyer le32toh(newcbd->cmds[i].siop_tables.t_msgin.addr),
1660 1.37.2.2 bouyer le32toh(newcbd->cmds[i].siop_tables.t_msgout.addr),
1661 1.37.2.2 bouyer le32toh(newcbd->cmds[i].siop_tables.t_status.addr));
1662 1.37.2.2 bouyer #endif
1663 1.37.2.2 bouyer }
1664 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&sc->cmds, newcbd, next);
1665 1.37.2.2 bouyer return 0;
1666 1.37.2.2 bouyer bad0:
1667 1.37.2.2 bouyer bus_dmamap_destroy(sc->sc_dmat, newcbd->xferdma);
1668 1.37.2.2 bouyer bad1:
1669 1.37.2.2 bouyer bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1670 1.37.2.2 bouyer bad2:
1671 1.37.2.2 bouyer free(newcbd->cmds, M_DEVBUF);
1672 1.37.2.2 bouyer bad3:
1673 1.37.2.2 bouyer free(newcbd, M_DEVBUF);
1674 1.37.2.2 bouyer return error;
1675 1.37.2.2 bouyer }
1676 1.37.2.2 bouyer
1677 1.37.2.2 bouyer struct siop_lunsw *
1678 1.37.2.2 bouyer siop_get_lunsw(sc)
1679 1.37.2.2 bouyer struct siop_softc *sc;
1680 1.37.2.2 bouyer {
1681 1.37.2.2 bouyer struct siop_lunsw *lunsw;
1682 1.37.2.2 bouyer int i;
1683 1.37.2.2 bouyer
1684 1.37.2.2 bouyer if (sc->script_free_lo + (sizeof(lun_switch) / sizeof(lun_switch[0])) >=
1685 1.37.2.2 bouyer sc->script_free_hi)
1686 1.37.2.2 bouyer return NULL;
1687 1.37.2.2 bouyer lunsw = TAILQ_FIRST(&sc->lunsw_list);
1688 1.37.2.2 bouyer if (lunsw != NULL) {
1689 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1690 1.37.2.2 bouyer printf("siop_get_lunsw got lunsw at offset %d\n",
1691 1.37.2.2 bouyer lunsw->lunsw_off);
1692 1.37.2.2 bouyer #endif
1693 1.37.2.2 bouyer TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
1694 1.37.2.2 bouyer return lunsw;
1695 1.37.2.2 bouyer }
1696 1.37.2.2 bouyer lunsw = malloc(sizeof(struct siop_lunsw), M_DEVBUF, M_NOWAIT);
1697 1.37.2.2 bouyer if (lunsw == NULL)
1698 1.37.2.2 bouyer return NULL;
1699 1.37.2.2 bouyer memset(lunsw, 0, sizeof(struct siop_lunsw));
1700 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1701 1.37.2.2 bouyer printf("allocating lunsw at offset %d\n", sc->script_free_lo);
1702 1.37.2.2 bouyer #endif
1703 1.37.2.2 bouyer if (sc->features & SF_CHIP_RAM) {
1704 1.37.2.2 bouyer bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh,
1705 1.37.2.2 bouyer sc->script_free_lo * 4, lun_switch,
1706 1.37.2.2 bouyer sizeof(lun_switch) / sizeof(lun_switch[0]));
1707 1.37.2.2 bouyer bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
1708 1.37.2.2 bouyer (sc->script_free_lo + E_abs_lunsw_return_Used[0]) * 4,
1709 1.37.2.2 bouyer sc->sc_scriptaddr + Ent_lunsw_return);
1710 1.37.2.2 bouyer } else {
1711 1.37.2.2 bouyer for (i = 0; i < sizeof(lun_switch) / sizeof(lun_switch[0]);
1712 1.37.2.2 bouyer i++)
1713 1.37.2.2 bouyer sc->sc_script[sc->script_free_lo + i] =
1714 1.37.2.2 bouyer htole32(lun_switch[i]);
1715 1.37.2.2 bouyer sc->sc_script[sc->script_free_lo + E_abs_lunsw_return_Used[0]] =
1716 1.37.2.2 bouyer htole32(sc->sc_scriptaddr + Ent_lunsw_return);
1717 1.37.2.2 bouyer }
1718 1.37.2.2 bouyer lunsw->lunsw_off = sc->script_free_lo;
1719 1.37.2.2 bouyer lunsw->lunsw_size = sizeof(lun_switch) / sizeof(lun_switch[0]);
1720 1.37.2.2 bouyer sc->script_free_lo += lunsw->lunsw_size;
1721 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1722 1.37.2.2 bouyer return lunsw;
1723 1.37.2.2 bouyer }
1724 1.37.2.2 bouyer
1725 1.37.2.2 bouyer void
1726 1.37.2.2 bouyer siop_add_reselsw(sc, target)
1727 1.37.2.2 bouyer struct siop_softc *sc;
1728 1.37.2.2 bouyer int target;
1729 1.37.2.2 bouyer {
1730 1.37.2.2 bouyer int i;
1731 1.37.2.2 bouyer struct siop_lun *siop_lun;
1732 1.37.2.2 bouyer /*
1733 1.37.2.2 bouyer * add an entry to resel switch
1734 1.37.2.2 bouyer */
1735 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_POSTWRITE);
1736 1.37.2.2 bouyer for (i = 0; i < 15; i++) {
1737 1.37.2.2 bouyer sc->targets[target]->reseloff = Ent_resel_targ0 / 4 + i * 2;
1738 1.37.2.2 bouyer if ((siop_script_read(sc, sc->targets[target]->reseloff) & 0xff)
1739 1.37.2.2 bouyer == 0xff) { /* it's free */
1740 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1741 1.37.2.2 bouyer printf("siop: target %d slot %d offset %d\n",
1742 1.37.2.2 bouyer target, i, sc->targets[target]->reseloff);
1743 1.37.2.2 bouyer #endif
1744 1.37.2.2 bouyer /* JUMP abs_foo, IF target | 0x80; */
1745 1.37.2.2 bouyer siop_script_write(sc, sc->targets[target]->reseloff,
1746 1.37.2.2 bouyer 0x800c0080 | target);
1747 1.37.2.2 bouyer siop_script_write(sc, sc->targets[target]->reseloff + 1,
1748 1.37.2.2 bouyer sc->sc_scriptaddr +
1749 1.37.2.2 bouyer sc->targets[target]->lunsw->lunsw_off * 4 +
1750 1.37.2.2 bouyer Ent_lun_switch_entry);
1751 1.37.2.2 bouyer break;
1752 1.37.2.2 bouyer }
1753 1.37.2.2 bouyer }
1754 1.37.2.2 bouyer if (i == 15) /* no free slot, shouldn't happen */
1755 1.37.2.2 bouyer panic("siop: resel switch full");
1756 1.37.2.2 bouyer
1757 1.37.2.2 bouyer sc->sc_ntargets++;
1758 1.37.2.2 bouyer for (i = 0; i < 8; i++) {
1759 1.37.2.2 bouyer siop_lun = sc->targets[target]->siop_lun[i];
1760 1.37.2.2 bouyer if (siop_lun == NULL)
1761 1.37.2.2 bouyer continue;
1762 1.37.2.2 bouyer if (siop_lun->reseloff > 0) {
1763 1.37.2.2 bouyer siop_lun->reseloff = 0;
1764 1.37.2.2 bouyer siop_add_dev(sc, target, i);
1765 1.37.2.2 bouyer }
1766 1.37.2.2 bouyer }
1767 1.37.2.2 bouyer siop_update_scntl3(sc, sc->targets[target]);
1768 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
1769 1.37.2.2 bouyer }
1770 1.37.2.2 bouyer
1771 1.37.2.2 bouyer void
1772 1.37.2.2 bouyer siop_update_scntl3(sc, siop_target)
1773 1.37.2.2 bouyer struct siop_softc *sc;
1774 1.37.2.2 bouyer struct siop_target *siop_target;
1775 1.37.2.2 bouyer {
1776 1.37.2.2 bouyer /* MOVE target->id >> 24 TO SCNTL3 */
1777 1.37.2.2 bouyer siop_script_write(sc,
1778 1.37.2.2 bouyer siop_target->lunsw->lunsw_off + (Ent_restore_scntl3 / 4),
1779 1.37.2.2 bouyer 0x78030000 | ((siop_target->id >> 16) & 0x0000ff00));
1780 1.37.2.2 bouyer /* MOVE target->id >> 8 TO SXFER */
1781 1.37.2.2 bouyer siop_script_write(sc,
1782 1.37.2.2 bouyer siop_target->lunsw->lunsw_off + (Ent_restore_scntl3 / 4) + 2,
1783 1.37.2.2 bouyer 0x78050000 | (siop_target->id & 0x0000ff00));
1784 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
1785 1.37.2.2 bouyer }
1786 1.37.2.2 bouyer
1787 1.37.2.2 bouyer void
1788 1.37.2.2 bouyer siop_add_dev(sc, target, lun)
1789 1.37.2.2 bouyer struct siop_softc *sc;
1790 1.37.2.2 bouyer int target;
1791 1.37.2.2 bouyer int lun;
1792 1.37.2.2 bouyer {
1793 1.37.2.2 bouyer struct siop_lunsw *lunsw;
1794 1.37.2.2 bouyer struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
1795 1.37.2.2 bouyer int i, ntargets;
1796 1.37.2.2 bouyer
1797 1.37.2.2 bouyer if (siop_lun->reseloff > 0)
1798 1.37.2.2 bouyer return;
1799 1.37.2.2 bouyer lunsw = sc->targets[target]->lunsw;
1800 1.37.2.2 bouyer if ((lunsw->lunsw_off + lunsw->lunsw_size) < sc->script_free_lo) {
1801 1.37.2.2 bouyer /*
1802 1.37.2.2 bouyer * can't extend this slot. Probably not worth trying to deal
1803 1.37.2.2 bouyer * with this case
1804 1.37.2.2 bouyer */
1805 1.37.2.2 bouyer #ifdef DEBUG
1806 1.37.2.2 bouyer printf("%s:%d:%d: can't allocate a lun sw slot\n",
1807 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun);
1808 1.37.2.2 bouyer #endif
1809 1.37.2.2 bouyer return;
1810 1.37.2.2 bouyer }
1811 1.37.2.2 bouyer /* count how many free targets we still have to probe */
1812 1.37.2.4 bouyer ntargets = sc->sc_chan.chan_ntargets - 1 - sc->sc_ntargets;
1813 1.37.2.2 bouyer
1814 1.37.2.2 bouyer /*
1815 1.37.2.2 bouyer * we need 8 bytes for the lun sw additionnal entry, and
1816 1.37.2.2 bouyer * eventually sizeof(tag_switch) for the tag switch entry.
1817 1.37.2.2 bouyer * Keep enouth free space for the free targets that could be
1818 1.37.2.2 bouyer * probed later.
1819 1.37.2.2 bouyer */
1820 1.37.2.2 bouyer if (sc->script_free_lo + 2 +
1821 1.37.2.2 bouyer (ntargets * sizeof(lun_switch) / sizeof(lun_switch[0])) >=
1822 1.37.2.2 bouyer ((sc->targets[target]->flags & TARF_TAG) ?
1823 1.37.2.2 bouyer sc->script_free_hi - (sizeof(tag_switch) / sizeof(tag_switch[0])) :
1824 1.37.2.2 bouyer sc->script_free_hi)) {
1825 1.37.2.2 bouyer /*
1826 1.37.2.2 bouyer * not enouth space, probably not worth dealing with it.
1827 1.37.2.2 bouyer * We can hold 13 tagged-queuing capable devices in the 4k RAM.
1828 1.37.2.2 bouyer */
1829 1.37.2.2 bouyer #ifdef DEBUG
1830 1.37.2.2 bouyer printf("%s:%d:%d: not enouth memory for a lun sw slot\n",
1831 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun);
1832 1.37.2.2 bouyer #endif
1833 1.37.2.2 bouyer return;
1834 1.37.2.2 bouyer }
1835 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1836 1.37.2.2 bouyer printf("%s:%d:%d: allocate lun sw entry\n",
1837 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun);
1838 1.37.2.2 bouyer #endif
1839 1.37.2.2 bouyer /* INT int_resellun */
1840 1.37.2.2 bouyer siop_script_write(sc, sc->script_free_lo, 0x98080000);
1841 1.37.2.2 bouyer siop_script_write(sc, sc->script_free_lo + 1, A_int_resellun);
1842 1.37.2.2 bouyer /* Now the slot entry: JUMP abs_foo, IF lun */
1843 1.37.2.2 bouyer siop_script_write(sc, sc->script_free_lo - 2,
1844 1.37.2.2 bouyer 0x800c0000 | lun);
1845 1.37.2.2 bouyer siop_script_write(sc, sc->script_free_lo - 1, 0);
1846 1.37.2.2 bouyer siop_lun->reseloff = sc->script_free_lo - 2;
1847 1.37.2.2 bouyer lunsw->lunsw_size += 2;
1848 1.37.2.2 bouyer sc->script_free_lo += 2;
1849 1.37.2.2 bouyer if (sc->targets[target]->flags & TARF_TAG) {
1850 1.37.2.2 bouyer /* we need a tag switch */
1851 1.37.2.2 bouyer sc->script_free_hi -=
1852 1.37.2.2 bouyer sizeof(tag_switch) / sizeof(tag_switch[0]);
1853 1.37.2.2 bouyer if (sc->features & SF_CHIP_RAM) {
1854 1.37.2.2 bouyer bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh,
1855 1.37.2.2 bouyer sc->script_free_hi * 4, tag_switch,
1856 1.37.2.2 bouyer sizeof(tag_switch) / sizeof(tag_switch[0]));
1857 1.37.2.2 bouyer } else {
1858 1.37.2.2 bouyer for(i = 0;
1859 1.37.2.2 bouyer i < sizeof(tag_switch) / sizeof(tag_switch[0]);
1860 1.37.2.2 bouyer i++) {
1861 1.37.2.2 bouyer sc->sc_script[sc->script_free_hi + i] =
1862 1.37.2.2 bouyer htole32(tag_switch[i]);
1863 1.37.2.2 bouyer }
1864 1.37.2.2 bouyer }
1865 1.37.2.2 bouyer siop_script_write(sc,
1866 1.37.2.2 bouyer siop_lun->reseloff + 1,
1867 1.37.2.2 bouyer sc->sc_scriptaddr + sc->script_free_hi * 4 +
1868 1.37.2.2 bouyer Ent_tag_switch_entry);
1869 1.37.2.2 bouyer
1870 1.37.2.2 bouyer for (i = 0; i < SIOP_NTAG; i++) {
1871 1.37.2.2 bouyer siop_lun->siop_tag[i].reseloff =
1872 1.37.2.2 bouyer sc->script_free_hi + (Ent_resel_tag0 / 4) + i * 2;
1873 1.37.2.2 bouyer }
1874 1.37.2.2 bouyer } else {
1875 1.37.2.2 bouyer /* non-tag case; just work with the lun switch */
1876 1.37.2.2 bouyer siop_lun->siop_tag[0].reseloff =
1877 1.37.2.2 bouyer sc->targets[target]->siop_lun[lun]->reseloff;
1878 1.37.2.2 bouyer }
1879 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
1880 1.37.2.2 bouyer }
1881 1.37.2.2 bouyer
1882 1.37.2.2 bouyer void
1883 1.37.2.2 bouyer siop_del_dev(sc, target, lun)
1884 1.37.2.2 bouyer struct siop_softc *sc;
1885 1.37.2.2 bouyer int target;
1886 1.37.2.2 bouyer int lun;
1887 1.37.2.2 bouyer {
1888 1.37.2.2 bouyer int i;
1889 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1890 1.37.2.2 bouyer printf("%s:%d:%d: free lun sw entry\n",
1891 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun);
1892 1.37.2.2 bouyer #endif
1893 1.37.2.2 bouyer if (sc->targets[target] == NULL)
1894 1.37.2.2 bouyer return;
1895 1.37.2.2 bouyer free(sc->targets[target]->siop_lun[lun], M_DEVBUF);
1896 1.37.2.2 bouyer sc->targets[target]->siop_lun[lun] = NULL;
1897 1.37.2.2 bouyer /* XXX compact sw entry too ? */
1898 1.37.2.2 bouyer /* check if we can free the whole target */
1899 1.37.2.2 bouyer for (i = 0; i < 8; i++) {
1900 1.37.2.2 bouyer if (sc->targets[target]->siop_lun[i] != NULL)
1901 1.37.2.2 bouyer return;
1902 1.37.2.2 bouyer }
1903 1.37.2.2 bouyer #ifdef SIOP_DEBUG
1904 1.37.2.2 bouyer printf("%s: free siop_target for target %d lun %d lunsw offset %d\n",
1905 1.37.2.2 bouyer sc->sc_dev.dv_xname, target, lun,
1906 1.37.2.2 bouyer sc->targets[target]->lunsw->lunsw_off);
1907 1.37.2.2 bouyer #endif
1908 1.37.2.2 bouyer /*
1909 1.37.2.2 bouyer * nothing here, free the target struct and resel
1910 1.37.2.2 bouyer * switch entry
1911 1.37.2.2 bouyer */
1912 1.37.2.2 bouyer siop_script_write(sc, sc->targets[target]->reseloff, 0x800c00ff);
1913 1.37.2.2 bouyer siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
1914 1.37.2.2 bouyer TAILQ_INSERT_TAIL(&sc->lunsw_list, sc->targets[target]->lunsw, next);
1915 1.37.2.2 bouyer free(sc->targets[target], M_DEVBUF);
1916 1.37.2.2 bouyer sc->targets[target] = NULL;
1917 1.37.2.2 bouyer sc->sc_ntargets--;
1918 1.37.2.2 bouyer }
1919 1.37.2.2 bouyer
1920 1.37.2.4 bouyer void
1921 1.37.2.4 bouyer siop_update_xfer_mode(sc, target)
1922 1.37.2.4 bouyer struct siop_softc *sc;
1923 1.37.2.4 bouyer int target;
1924 1.37.2.4 bouyer {
1925 1.37.2.4 bouyer struct siop_target *siop_target = sc->targets[target];
1926 1.37.2.4 bouyer struct scsipi_xfer_mode xm;
1927 1.37.2.4 bouyer
1928 1.37.2.4 bouyer xm.xm_target = target;
1929 1.37.2.4 bouyer xm.xm_mode = 0;
1930 1.37.2.4 bouyer xm.xm_period = 0;
1931 1.37.2.4 bouyer xm.xm_offset = 0;
1932 1.37.2.4 bouyer
1933 1.37.2.4 bouyer if (siop_target->flags & TARF_ISWIDE)
1934 1.37.2.4 bouyer xm.xm_mode |= PERIPH_CAP_WIDE16;
1935 1.37.2.4 bouyer if (siop_target->period) {
1936 1.37.2.4 bouyer xm.xm_period = siop_target->period;
1937 1.37.2.4 bouyer xm.xm_offset = siop_target->offset;
1938 1.37.2.4 bouyer xm.xm_mode |= PERIPH_CAP_SYNC;
1939 1.37.2.4 bouyer }
1940 1.37.2.5 bouyer if (siop_target->flags & TARF_TAG)
1941 1.37.2.5 bouyer xm.xm_mode |= PERIPH_CAP_TQING;
1942 1.37.2.4 bouyer scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, &xm);
1943 1.37.2.4 bouyer }
1944 1.37.2.4 bouyer
1945 1.37.2.2 bouyer #ifdef SIOP_STATS
1946 1.37.2.2 bouyer void
1947 1.37.2.2 bouyer siop_printstats()
1948 1.37.2.2 bouyer {
1949 1.37.2.2 bouyer printf("siop_stat_intr %d\n", siop_stat_intr);
1950 1.37.2.2 bouyer printf("siop_stat_intr_shortxfer %d\n", siop_stat_intr_shortxfer);
1951 1.37.2.2 bouyer printf("siop_stat_intr_xferdisc %d\n", siop_stat_intr_xferdisc);
1952 1.37.2.2 bouyer printf("siop_stat_intr_sdp %d\n", siop_stat_intr_sdp);
1953 1.37.2.2 bouyer printf("siop_stat_intr_done %d\n", siop_stat_intr_done);
1954 1.37.2.2 bouyer printf("siop_stat_intr_lunresel %d\n", siop_stat_intr_lunresel);
1955 1.37.2.2 bouyer printf("siop_stat_intr_qfull %d\n", siop_stat_intr_qfull);
1956 1.37.2.2 bouyer }
1957 1.37.2.2 bouyer #endif
1958