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