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