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