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