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