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