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