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