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