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