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