1 /* $NetBSD: ninjascsi32.c,v 1.21 2010/11/13 13:52:01 uebayasi Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 2006, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by ITOH Yasufumi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: ninjascsi32.c,v 1.21 2010/11/13 13:52:01 uebayasi Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/callout.h> 38 #include <sys/device.h> 39 #include <sys/kernel.h> 40 #include <sys/buf.h> 41 #include <sys/scsiio.h> 42 #include <sys/proc.h> 43 44 #include <sys/bus.h> 45 #include <sys/intr.h> 46 47 #include <dev/scsipi/scsi_all.h> 48 #include <dev/scsipi/scsipi_all.h> 49 #include <dev/scsipi/scsiconf.h> 50 #include <dev/scsipi/scsi_message.h> 51 52 /* 53 * DualEdge transfer support 54 */ 55 /* #define NJSC32_DUALEDGE */ /* XXX untested */ 56 57 /* 58 * Auto param loading does not work properly (it partially works (works on 59 * start, doesn't on restart) on rev 0x54, it doesn't work at all on rev 0x51), 60 * and it doesn't improve the performance so much, 61 * forget about it. 62 */ 63 #undef NJSC32_AUTOPARAM 64 65 #include <dev/ic/ninjascsi32reg.h> 66 #include <dev/ic/ninjascsi32var.h> 67 68 /* #define NJSC32_DEBUG */ 69 /* #define NJSC32_TRACE */ 70 71 #ifdef NJSC32_DEBUG 72 #define DPRINTF(x) printf x 73 #define DPRINTC(cmd, x) PRINTC(cmd, x) 74 #else 75 #define DPRINTF(x) 76 #define DPRINTC(cmd, x) 77 #endif 78 #ifdef NJSC32_TRACE 79 #define TPRINTF(x) printf x 80 #define TPRINTC(cmd, x) PRINTC(cmd, x) 81 #else 82 #define TPRINTF(x) 83 #define TPRINTC(cmd, x) 84 #endif 85 86 #define PRINTC(cmd, x) do { \ 87 scsi_print_addr((cmd)->c_xs->xs_periph); \ 88 printf x; \ 89 } while (/* CONSTCOND */ 0) 90 91 static void njsc32_scsipi_request(struct scsipi_channel *, 92 scsipi_adapter_req_t, void *); 93 static void njsc32_scsipi_minphys(struct buf *); 94 static int njsc32_scsipi_ioctl(struct scsipi_channel *, u_long, void *, 95 int, struct proc *); 96 97 static void njsc32_init(struct njsc32_softc *, int nosleep); 98 static int njsc32_init_cmds(struct njsc32_softc *); 99 static void njsc32_target_async(struct njsc32_softc *, 100 struct njsc32_target *); 101 static void njsc32_init_targets(struct njsc32_softc *); 102 static void njsc32_add_msgout(struct njsc32_softc *, int); 103 static u_int32_t njsc32_get_auto_msgout(struct njsc32_softc *); 104 #ifdef NJSC32_DUALEDGE 105 static void njsc32_msgout_wdtr(struct njsc32_softc *, int); 106 #endif 107 static void njsc32_msgout_sdtr(struct njsc32_softc *, int period, 108 int offset); 109 static void njsc32_negotiate_xfer(struct njsc32_softc *, 110 struct njsc32_target *); 111 static void njsc32_arbitration_failed(struct njsc32_softc *); 112 static void njsc32_start(struct njsc32_softc *); 113 static void njsc32_run_xfer(struct njsc32_softc *, struct scsipi_xfer *); 114 static void njsc32_end_cmd(struct njsc32_softc *, struct njsc32_cmd *, 115 scsipi_xfer_result_t); 116 static void njsc32_wait_reset_release(void *); 117 static void njsc32_reset_bus(struct njsc32_softc *); 118 static void njsc32_clear_cmds(struct njsc32_softc *, 119 scsipi_xfer_result_t); 120 static void njsc32_set_ptr(struct njsc32_softc *, struct njsc32_cmd *, 121 u_int32_t); 122 static void njsc32_assert_ack(struct njsc32_softc *); 123 static void njsc32_negate_ack(struct njsc32_softc *); 124 static void njsc32_wait_req_negate(struct njsc32_softc *); 125 static void njsc32_reconnect(struct njsc32_softc *, struct njsc32_cmd *); 126 enum njsc32_reselstat { 127 NJSC32_RESEL_ERROR, /* to be rejected */ 128 NJSC32_RESEL_COMPLETE, /* reselection is just complete */ 129 NJSC32_RESEL_THROUGH /* this message is OK (no reply) */ 130 }; 131 static enum njsc32_reselstat njsc32_resel_identify(struct njsc32_softc *, 132 int lun, struct njsc32_cmd **); 133 static enum njsc32_reselstat njsc32_resel_tag(struct njsc32_softc *, 134 int tag, struct njsc32_cmd **); 135 static void njsc32_cmd_reload(struct njsc32_softc *, struct njsc32_cmd *, 136 int); 137 static void njsc32_update_xfer_mode(struct njsc32_softc *, 138 struct njsc32_target *); 139 static void njsc32_msgin(struct njsc32_softc *); 140 static void njsc32_msgout(struct njsc32_softc *); 141 static void njsc32_cmdtimeout(void *); 142 static void njsc32_reseltimeout(void *); 143 144 static inline unsigned 145 njsc32_read_1(struct njsc32_softc *sc, int no) 146 { 147 148 return bus_space_read_1(sc->sc_regt, sc->sc_regh, no); 149 } 150 151 static inline unsigned 152 njsc32_read_2(struct njsc32_softc *sc, int no) 153 { 154 155 return bus_space_read_2(sc->sc_regt, sc->sc_regh, no); 156 } 157 158 static inline u_int32_t 159 njsc32_read_4(struct njsc32_softc *sc, int no) 160 { 161 162 return bus_space_read_4(sc->sc_regt, sc->sc_regh, no); 163 } 164 165 static inline void 166 njsc32_write_1(struct njsc32_softc *sc, int no, int val) 167 { 168 169 bus_space_write_1(sc->sc_regt, sc->sc_regh, no, val); 170 } 171 172 static inline void 173 njsc32_write_2(struct njsc32_softc *sc, int no, int val) 174 { 175 176 bus_space_write_2(sc->sc_regt, sc->sc_regh, no, val); 177 } 178 179 static inline void 180 njsc32_write_4(struct njsc32_softc *sc, int no, u_int32_t val) 181 { 182 183 bus_space_write_4(sc->sc_regt, sc->sc_regh, no, val); 184 } 185 186 static inline unsigned 187 njsc32_ireg_read_1(struct njsc32_softc *sc, int no) 188 { 189 190 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no); 191 return bus_space_read_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW); 192 } 193 194 static inline unsigned 195 njsc32_ireg_read_2(struct njsc32_softc *sc, int no) 196 { 197 198 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no); 199 return bus_space_read_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW); 200 } 201 202 static inline u_int32_t 203 njsc32_ireg_read_4(struct njsc32_softc *sc, int no) 204 { 205 u_int32_t val; 206 207 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no); 208 val = (u_int16_t)bus_space_read_2(sc->sc_regt, sc->sc_regh, 209 NJSC32_REG_DATA_LOW); 210 return val | (bus_space_read_2(sc->sc_regt, sc->sc_regh, 211 NJSC32_REG_DATA_HIGH) << 16); 212 } 213 214 static inline void 215 njsc32_ireg_write_1(struct njsc32_softc *sc, int no, int val) 216 { 217 218 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no); 219 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW, val); 220 } 221 222 static inline void 223 njsc32_ireg_write_2(struct njsc32_softc *sc, int no, int val) 224 { 225 226 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no); 227 bus_space_write_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW, val); 228 } 229 230 static inline void 231 njsc32_ireg_write_4(struct njsc32_softc *sc, int no, u_int32_t val) 232 { 233 234 bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no); 235 bus_space_write_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW, val); 236 bus_space_write_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_HIGH, 237 val >> 16); 238 } 239 240 #define NS(ns) ((ns) / 4) /* nanosecond (>= 50) -> sync value */ 241 #ifdef __STDC__ 242 # define ACKW(n) NJSC32_ACK_WIDTH_ ## n ## CLK 243 # define SMPL(n) (NJSC32_SREQ_SAMPLING_ ## n ## CLK | \ 244 NJSC32_SREQ_SAMPLING_ENABLE) 245 #else 246 # define ACKW(n) NJSC32_ACK_WIDTH_/**/n/**/CLK 247 # define SMPL(n) (NJSC32_SREQ_SAMPLING_/**/n/**/CLK | \ 248 NJSC32_SREQ_SAMPLING_ENABLE) 249 #endif 250 251 #define NJSC32_NSYNCT_MAXSYNC 1 252 #define NJSC32_NSYNCT 16 253 254 /* 40MHz (25ns) */ 255 static const struct njsc32_sync_param njsc32_synct_40M[NJSC32_NSYNCT] = { 256 { 0, 0, 0 }, /* dummy for async */ 257 { NS( 50), ACKW(1), 0 }, /* 20.0 : 50ns, 25ns */ 258 { NS( 75), ACKW(1), SMPL(1) }, /* 13.3 : 75ns, 25ns */ 259 { NS(100), ACKW(2), SMPL(1) }, /* 10.0 : 100ns, 50ns */ 260 { NS(125), ACKW(2), SMPL(2) }, /* 8.0 : 125ns, 50ns */ 261 { NS(150), ACKW(3), SMPL(2) }, /* 6.7 : 150ns, 75ns */ 262 { NS(175), ACKW(3), SMPL(2) }, /* 5.7 : 175ns, 75ns */ 263 { NS(200), ACKW(4), SMPL(2) }, /* 5.0 : 200ns, 100ns */ 264 { NS(225), ACKW(4), SMPL(4) }, /* 4.4 : 225ns, 100ns */ 265 { NS(250), ACKW(4), SMPL(4) }, /* 4.0 : 250ns, 100ns */ 266 { NS(275), ACKW(4), SMPL(4) }, /* 3.64: 275ns, 100ns */ 267 { NS(300), ACKW(4), SMPL(4) }, /* 3.33: 300ns, 100ns */ 268 { NS(325), ACKW(4), SMPL(4) }, /* 3.01: 325ns, 100ns */ 269 { NS(350), ACKW(4), SMPL(4) }, /* 2.86: 350ns, 100ns */ 270 { NS(375), ACKW(4), SMPL(4) }, /* 2.67: 375ns, 100ns */ 271 { NS(400), ACKW(4), SMPL(4) } /* 2.50: 400ns, 100ns */ 272 }; 273 274 #ifdef NJSC32_SUPPORT_OTHER_CLOCKS 275 /* 20MHz (50ns) */ 276 static const struct njsc32_sync_param njsc32_synct_20M[NJSC32_NSYNCT] = { 277 { 0, 0, 0 }, /* dummy for async */ 278 { NS(100), ACKW(1), 0 }, /* 10.0 : 100ns, 50ns */ 279 { NS(150), ACKW(1), SMPL(2) }, /* 6.7 : 150ns, 50ns */ 280 { NS(200), ACKW(2), SMPL(2) }, /* 5.0 : 200ns, 100ns */ 281 { NS(250), ACKW(2), SMPL(4) }, /* 4.0 : 250ns, 100ns */ 282 { NS(300), ACKW(3), SMPL(4) }, /* 3.3 : 300ns, 150ns */ 283 { NS(350), ACKW(3), SMPL(4) }, /* 2.8 : 350ns, 150ns */ 284 { NS(400), ACKW(4), SMPL(4) }, /* 2.5 : 400ns, 200ns */ 285 { NS(450), ACKW(4), SMPL(4) }, /* 2.2 : 450ns, 200ns */ 286 { NS(500), ACKW(4), SMPL(4) }, /* 2.0 : 500ns, 200ns */ 287 { NS(550), ACKW(4), SMPL(4) }, /* 1.82: 550ns, 200ns */ 288 { NS(600), ACKW(4), SMPL(4) }, /* 1.67: 600ns, 200ns */ 289 { NS(650), ACKW(4), SMPL(4) }, /* 1.54: 650ns, 200ns */ 290 { NS(700), ACKW(4), SMPL(4) }, /* 1.43: 700ns, 200ns */ 291 { NS(750), ACKW(4), SMPL(4) }, /* 1.33: 750ns, 200ns */ 292 { NS(800), ACKW(4), SMPL(4) } /* 1.25: 800ns, 200ns */ 293 }; 294 295 /* 33.3MHz (30ns) */ 296 static const struct njsc32_sync_param njsc32_synct_pci[NJSC32_NSYNCT] = { 297 { 0, 0, 0 }, /* dummy for async */ 298 { NS( 60), ACKW(1), 0 }, /* 16.6 : 60ns, 30ns */ 299 { NS( 90), ACKW(1), SMPL(1) }, /* 11.1 : 90ns, 30ns */ 300 { NS(120), ACKW(2), SMPL(2) }, /* 8.3 : 120ns, 60ns */ 301 { NS(150), ACKW(2), SMPL(2) }, /* 6.7 : 150ns, 60ns */ 302 { NS(180), ACKW(3), SMPL(2) }, /* 5.6 : 180ns, 90ns */ 303 { NS(210), ACKW(3), SMPL(4) }, /* 4.8 : 210ns, 90ns */ 304 { NS(240), ACKW(4), SMPL(4) }, /* 4.2 : 240ns, 120ns */ 305 { NS(270), ACKW(4), SMPL(4) }, /* 3.7 : 270ns, 120ns */ 306 { NS(300), ACKW(4), SMPL(4) }, /* 3.3 : 300ns, 120ns */ 307 { NS(330), ACKW(4), SMPL(4) }, /* 3.0 : 330ns, 120ns */ 308 { NS(360), ACKW(4), SMPL(4) }, /* 2.8 : 360ns, 120ns */ 309 { NS(390), ACKW(4), SMPL(4) }, /* 2.6 : 390ns, 120ns */ 310 { NS(420), ACKW(4), SMPL(4) }, /* 2.4 : 420ns, 120ns */ 311 { NS(450), ACKW(4), SMPL(4) }, /* 2.2 : 450ns, 120ns */ 312 { NS(480), ACKW(4), SMPL(4) } /* 2.1 : 480ns, 120ns */ 313 }; 314 #endif /* NJSC32_SUPPORT_OTHER_CLOCKS */ 315 316 #undef NS 317 #undef ACKW 318 #undef SMPL 319 320 /* initialize device */ 321 static void 322 njsc32_init(struct njsc32_softc *sc, int nosleep) 323 { 324 u_int16_t intstat; 325 int i; 326 327 /* block all interrupts */ 328 njsc32_write_2(sc, NJSC32_REG_IRQ, NJSC32_IRQ_MASK_ALL); 329 330 /* clear transfer */ 331 njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0); 332 njsc32_write_4(sc, NJSC32_REG_BM_CNT, 0); 333 334 /* make sure interrupts are cleared */ 335 for (i = 0; ((intstat = njsc32_read_2(sc, NJSC32_REG_IRQ)) 336 & NJSC32_IRQ_INTR_PENDING) && i < 5 /* just not forever */; i++) { 337 DPRINTF(("%s: njsc32_init: intr pending: %#x\n", 338 device_xname(sc->sc_dev), intstat)); 339 } 340 341 /* FIFO threshold */ 342 njsc32_ireg_write_1(sc, NJSC32_IREG_FIFO_THRESHOLD_FULL, 343 NJSC32_FIFO_FULL_BUSMASTER); 344 njsc32_ireg_write_1(sc, NJSC32_IREG_FIFO_THRESHOLD_EMPTY, 345 NJSC32_FIFO_EMPTY_BUSMASTER); 346 347 /* clock source */ 348 njsc32_ireg_write_1(sc, NJSC32_IREG_CLOCK, sc->sc_clk); 349 350 /* memory read multiple */ 351 njsc32_ireg_write_1(sc, NJSC32_IREG_BM, 352 NJSC32_BM_MEMRD_CMD1 | NJSC32_BM_SGT_AUTO_PARA_MEMRD_CMD); 353 354 /* clear parity error and enable parity detection */ 355 njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL, 356 NJSC32_PARITYCTL_CHECK_ENABLE | NJSC32_PARITYCTL_CLEAR_ERROR); 357 358 /* misc configuration */ 359 njsc32_ireg_write_2(sc, NJSC32_IREG_MISC, 360 NJSC32_MISC_SCSI_DIRECTION_DETECTOR_SELECT | 361 NJSC32_MISC_DELAYED_BMSTART | 362 NJSC32_MISC_MASTER_TERMINATION_SELECT | 363 NJSC32_MISC_BMREQ_NEGATE_TIMING_SEL | 364 NJSC32_MISC_AUTOSEL_TIMING_SEL | 365 NJSC32_MISC_BMSTOP_CHANGE2_NONDATA_PHASE); 366 367 /* 368 * Check for termination power (32Bi and some versions of 32UDE). 369 */ 370 if (!nosleep || cold) { 371 DPRINTF(("%s: njsc32_init: checking TERMPWR\n", 372 device_xname(sc->sc_dev))); 373 374 /* First, turn termination power off */ 375 njsc32_ireg_write_1(sc, NJSC32_IREG_TERM_PWR, 0); 376 377 /* give 0.5s to settle */ 378 if (nosleep) 379 delay(500000); 380 else 381 tsleep(sc, PWAIT, "njs_t1", hz / 2); 382 } 383 384 /* supply termination power if not supplied by other devices */ 385 if ((njsc32_ireg_read_1(sc, NJSC32_IREG_TERM_PWR) & 386 NJSC32_TERMPWR_SENSE) == 0) { 387 /* termination power is not present on the bus */ 388 if (sc->sc_flags & NJSC32_CANNOT_SUPPLY_TERMPWR) { 389 /* 390 * CardBus device must not supply termination power 391 * to avoid excessive power consumption. 392 */ 393 printf("%s: no termination power present\n", 394 device_xname(sc->sc_dev)); 395 } else { 396 /* supply termination power */ 397 njsc32_ireg_write_1(sc, NJSC32_IREG_TERM_PWR, 398 NJSC32_TERMPWR_BPWR); 399 400 DPRINTF(("%s: supplying termination power\n", 401 device_xname(sc->sc_dev))); 402 403 /* give 0.5s to settle */ 404 if (!nosleep) 405 tsleep(sc, PWAIT, "njs_t2", hz / 2); 406 } 407 } 408 409 /* stop timer */ 410 njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP); 411 njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP); 412 413 /* default transfer parameter */ 414 njsc32_write_1(sc, NJSC32_REG_SYNC, 0); 415 njsc32_write_1(sc, NJSC32_REG_ACK_WIDTH, NJSC32_ACK_WIDTH_1CLK); 416 njsc32_write_2(sc, NJSC32_REG_SEL_TIMEOUT, 417 NJSC32_SEL_TIMEOUT_TIME); 418 419 /* select interrupt source */ 420 njsc32_ireg_write_2(sc, NJSC32_IREG_IRQ_SELECT, 421 NJSC32_IRQSEL_RESELECT | 422 NJSC32_IRQSEL_PHASE_CHANGE | 423 NJSC32_IRQSEL_SCSIRESET | 424 NJSC32_IRQSEL_TIMER | 425 NJSC32_IRQSEL_FIFO_THRESHOLD | 426 NJSC32_IRQSEL_TARGET_ABORT | 427 NJSC32_IRQSEL_MASTER_ABORT | 428 /* XXX not yet 429 NJSC32_IRQSEL_SERR | 430 NJSC32_IRQSEL_PERR | 431 NJSC32_IRQSEL_BMCNTERR | 432 */ 433 NJSC32_IRQSEL_AUTO_SCSI_SEQ); 434 435 /* interrupts will be unblocked later after bus reset */ 436 437 /* turn LED off */ 438 njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT_DDR, 439 NJSC32_EXTPORT_LED_OFF); 440 njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT, 441 NJSC32_EXTPORT_LED_OFF); 442 443 /* reset SCSI bus so the targets become known state */ 444 njsc32_reset_bus(sc); 445 } 446 447 static int 448 njsc32_init_cmds(struct njsc32_softc *sc) 449 { 450 struct njsc32_cmd *cmd; 451 bus_addr_t dmaaddr; 452 int i, error; 453 454 /* 455 * allocate DMA area for command 456 */ 457 if ((error = bus_dmamem_alloc(sc->sc_dmat, 458 sizeof(struct njsc32_dma_page), PAGE_SIZE, 0, 459 &sc->sc_cmdpg_seg, 1, &sc->sc_cmdpg_nsegs, BUS_DMA_NOWAIT)) != 0) { 460 aprint_error_dev(sc->sc_dev, 461 "unable to allocate cmd page, error = %d\n", 462 error); 463 return 0; 464 } 465 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cmdpg_seg, 466 sc->sc_cmdpg_nsegs, sizeof(struct njsc32_dma_page), 467 (void **)&sc->sc_cmdpg, 468 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 469 aprint_error_dev(sc->sc_dev, 470 "unable to map cmd page, error = %d\n", 471 error); 472 goto fail1; 473 } 474 if ((error = bus_dmamap_create(sc->sc_dmat, 475 sizeof(struct njsc32_dma_page), 1, 476 sizeof(struct njsc32_dma_page), 0, BUS_DMA_NOWAIT, 477 &sc->sc_dmamap_cmdpg)) != 0) { 478 aprint_error_dev(sc->sc_dev, 479 "unable to create cmd DMA map, error = %d\n", 480 error); 481 goto fail2; 482 } 483 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_cmdpg, 484 sc->sc_cmdpg, sizeof(struct njsc32_dma_page), 485 NULL, BUS_DMA_NOWAIT)) != 0) { 486 aprint_error_dev(sc->sc_dev, 487 "unable to load cmd DMA map, error = %d\n", 488 error); 489 goto fail3; 490 } 491 492 memset(sc->sc_cmdpg, 0, sizeof(struct njsc32_dma_page)); 493 dmaaddr = sc->sc_dmamap_cmdpg->dm_segs[0].ds_addr; 494 495 #ifdef NJSC32_AUTOPARAM 496 sc->sc_ap_dma = dmaaddr + offsetof(struct njsc32_dma_page, dp_ap); 497 #endif 498 499 for (i = 0; i < NJSC32_NUM_CMD; i++) { 500 cmd = &sc->sc_cmds[i]; 501 cmd->c_sc = sc; 502 cmd->c_sgt = sc->sc_cmdpg->dp_sg[i]; 503 cmd->c_sgt_dma = dmaaddr + 504 offsetof(struct njsc32_dma_page, dp_sg[i]); 505 cmd->c_flags = 0; 506 507 error = bus_dmamap_create(sc->sc_dmat, 508 NJSC32_MAX_XFER, /* max total map size */ 509 NJSC32_NUM_SG, /* max number of segments */ 510 NJSC32_SGT_MAXSEGLEN, /* max size of a segment */ 511 0, /* boundary */ 512 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &cmd->c_dmamap_xfer); 513 if (error) { 514 aprint_error_dev(sc->sc_dev, 515 "only %d cmd descs available (error = %d)\n", 516 i, error); 517 break; 518 } 519 TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmd, c_q); 520 } 521 522 if (i > 0) 523 return i; 524 525 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_cmdpg); 526 fail3: bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_cmdpg); 527 fail2: bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_cmdpg, 528 sizeof(struct njsc32_dma_page)); 529 fail1: bus_dmamem_free(sc->sc_dmat, &sc->sc_cmdpg_seg, sc->sc_cmdpg_nsegs); 530 531 return 0; 532 } 533 534 static void 535 njsc32_target_async(struct njsc32_softc *sc, struct njsc32_target *target) 536 { 537 538 target->t_sync = 539 NJSC32_SYNC_VAL(sc->sc_sync_max, NJSC32_SYNCOFFSET_ASYNC); 540 target->t_ackwidth = NJSC32_ACK_WIDTH_1CLK; 541 target->t_sample = 0; /* disable */ 542 target->t_syncoffset = NJSC32_SYNCOFFSET_ASYNC; 543 target->t_syncperiod = NJSC32_SYNCPERIOD_ASYNC; 544 } 545 546 static void 547 njsc32_init_targets(struct njsc32_softc *sc) 548 { 549 int id, lun; 550 struct njsc32_lu *lu; 551 552 for (id = 0; id <= NJSC32_MAX_TARGET_ID; id++) { 553 /* cancel negotiation status */ 554 sc->sc_targets[id].t_state = NJSC32_TARST_INIT; 555 556 /* default to async mode */ 557 njsc32_target_async(sc, &sc->sc_targets[id]); 558 559 #ifdef NJSC32_DUALEDGE 560 sc->sc_targets[id].t_xferctl = 0; 561 #endif 562 563 sc->sc_targets[id].t_targetid = 564 (1 << id) | (1 << NJSC32_INITIATOR_ID); 565 566 /* init logical units */ 567 for (lun = 0; lun < NJSC32_NLU; lun++) { 568 lu = &sc->sc_targets[id].t_lus[lun]; 569 lu->lu_cmd = NULL; 570 TAILQ_INIT(&lu->lu_q); 571 } 572 } 573 } 574 575 void 576 njsc32_attach(struct njsc32_softc *sc) 577 { 578 const char *str; 579 #if 1 /* test */ 580 int reg; 581 njsc32_model_t detected_model; 582 #endif 583 584 /* init */ 585 TAILQ_INIT(&sc->sc_freecmd); 586 TAILQ_INIT(&sc->sc_reqcmd); 587 callout_init(&sc->sc_callout, 0); 588 589 #if 1 /* test */ 590 /* 591 * try to distinguish 32Bi and 32UDE 592 */ 593 /* try to set DualEdge bit (exists on 32UDE only) and read it back */ 594 njsc32_write_2(sc, NJSC32_REG_TRANSFER, NJSC32_XFR_DUALEDGE_ENABLE); 595 if ((reg = njsc32_read_2(sc, NJSC32_REG_TRANSFER)) == 0xffff) { 596 /* device was removed? */ 597 aprint_error_dev(sc->sc_dev, "attach failed\n"); 598 return; 599 } else if (reg & NJSC32_XFR_DUALEDGE_ENABLE) { 600 detected_model = NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE; 601 } else { 602 detected_model = NJSC32_MODEL_32BI; 603 } 604 njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0); /* restore */ 605 606 #if 1/*def DIAGNOSTIC*/ 607 /* compare what is configured with what is detected */ 608 if ((sc->sc_model & NJSC32_MODEL_MASK) != 609 (detected_model & NJSC32_MODEL_MASK)) { 610 /* 611 * Please report this error if it happens. 612 */ 613 aprint_error_dev(sc->sc_dev, "model mismatch: %#x vs %#x\n", 614 sc->sc_model, detected_model); 615 return; 616 } 617 #endif 618 #endif 619 620 /* check model */ 621 switch (sc->sc_model & NJSC32_MODEL_MASK) { 622 case NJSC32_MODEL_32BI: 623 str = "Bi"; 624 /* 32Bi doesn't support DualEdge transfer */ 625 KASSERT((sc->sc_model & NJSC32_FLAG_DUALEDGE) == 0); 626 break; 627 case NJSC32_MODEL_32UDE: 628 str = "UDE"; 629 break; 630 default: 631 aprint_error_dev(sc->sc_dev, "unknown model!\n"); 632 return; 633 } 634 aprint_normal_dev(sc->sc_dev, "NJSC-32%s", str); 635 636 switch (sc->sc_clk) { 637 default: 638 #ifdef DIAGNOSTIC 639 panic("njsc32_attach: unknown clk %d", sc->sc_clk); 640 #endif 641 case NJSC32_CLOCK_DIV_4: 642 sc->sc_synct = njsc32_synct_40M; 643 str = "40MHz"; 644 break; 645 #ifdef NJSC32_SUPPORT_OTHER_CLOCKS 646 case NJSC32_CLOCK_DIV_2: 647 sc->sc_synct = njsc32_synct_20M; 648 str = "20MHz"; 649 break; 650 case NJSC32_CLOCK_PCICLK: 651 sc->sc_synct = njsc32_synct_pci; 652 str = "PCI"; 653 break; 654 #endif 655 } 656 aprint_normal(", G/A rev %#x, clk %s%s\n", 657 NJSC32_INDEX_GAREV(njsc32_read_2(sc, NJSC32_REG_INDEX)), str, 658 (sc->sc_model & NJSC32_FLAG_DUALEDGE) ? 659 #ifdef NJSC32_DUALEDGE 660 ", DualEdge" 661 #else 662 ", DualEdge (no driver support)" 663 #endif 664 : ""); 665 666 /* allocate DMA resource */ 667 if ((sc->sc_ncmd = njsc32_init_cmds(sc)) == 0) { 668 aprint_error_dev(sc->sc_dev, "no usable DMA map\n"); 669 return; 670 } 671 sc->sc_flags |= NJSC32_CMDPG_MAPPED; 672 673 sc->sc_curcmd = NULL; 674 sc->sc_nusedcmds = 0; 675 676 sc->sc_sync_max = 1; /* XXX look up EEPROM configuration? */ 677 678 /* initialize hardware and target structure */ 679 njsc32_init(sc, cold); 680 681 /* setup adapter */ 682 sc->sc_adapter.adapt_dev = sc->sc_dev; 683 sc->sc_adapter.adapt_nchannels = 1; 684 sc->sc_adapter.adapt_request = njsc32_scsipi_request; 685 sc->sc_adapter.adapt_minphys = njsc32_scsipi_minphys; 686 sc->sc_adapter.adapt_ioctl = njsc32_scsipi_ioctl; 687 688 sc->sc_adapter.adapt_max_periph = sc->sc_adapter.adapt_openings = 689 sc->sc_ncmd; 690 691 /* setup channel */ 692 sc->sc_channel.chan_adapter = &sc->sc_adapter; 693 sc->sc_channel.chan_bustype = &scsi_bustype; 694 sc->sc_channel.chan_channel = 0; 695 sc->sc_channel.chan_ntargets = NJSC32_NTARGET; 696 sc->sc_channel.chan_nluns = NJSC32_NLU; 697 sc->sc_channel.chan_id = NJSC32_INITIATOR_ID; 698 699 sc->sc_scsi = config_found(sc->sc_dev, &sc->sc_channel, scsiprint); 700 } 701 702 int 703 njsc32_detach(struct njsc32_softc *sc, int flags) 704 { 705 int rv = 0; 706 int i, s; 707 struct njsc32_cmd *cmd; 708 709 callout_stop(&sc->sc_callout); 710 711 s = splbio(); 712 713 /* clear running/disconnected commands */ 714 njsc32_clear_cmds(sc, XS_DRIVER_STUFFUP); 715 716 sc->sc_stat = NJSC32_STAT_DETACH; 717 718 /* clear pending commands */ 719 while ((cmd = TAILQ_FIRST(&sc->sc_reqcmd)) != NULL) { 720 TAILQ_REMOVE(&sc->sc_reqcmd, cmd, c_q); 721 njsc32_end_cmd(sc, cmd, XS_RESET); 722 } 723 724 if (sc->sc_scsi != NULL) 725 rv = config_detach(sc->sc_scsi, flags); 726 727 splx(s); 728 729 /* free DMA resource */ 730 if (sc->sc_flags & NJSC32_CMDPG_MAPPED) { 731 for (i = 0; i < sc->sc_ncmd; i++) { 732 cmd = &sc->sc_cmds[i]; 733 if (cmd->c_flags & NJSC32_CMD_DMA_MAPPED) 734 bus_dmamap_unload(sc->sc_dmat, 735 cmd->c_dmamap_xfer); 736 bus_dmamap_destroy(sc->sc_dmat, cmd->c_dmamap_xfer); 737 } 738 739 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_cmdpg); 740 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_cmdpg); 741 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_cmdpg, 742 sizeof(struct njsc32_dma_page)); 743 bus_dmamem_free(sc->sc_dmat, &sc->sc_cmdpg_seg, 744 sc->sc_cmdpg_nsegs); 745 } 746 747 return 0; 748 } 749 750 static inline void 751 njsc32_cmd_init(struct njsc32_cmd *cmd) 752 { 753 754 cmd->c_flags = 0; 755 756 /* scatter/gather table */ 757 cmd->c_sgtdmaaddr = NJSC32_CMD_DMAADDR_SGT(cmd, 0); 758 cmd->c_sgoffset = 0; 759 cmd->c_sgfixcnt = 0; 760 761 /* data pointer */ 762 cmd->c_dp_cur = cmd->c_dp_saved = cmd->c_dp_max = 0; 763 } 764 765 static inline void 766 njsc32_init_msgout(struct njsc32_softc *sc) 767 { 768 769 sc->sc_msgoutlen = 0; 770 sc->sc_msgoutidx = 0; 771 } 772 773 static void 774 njsc32_add_msgout(struct njsc32_softc *sc, int byte) 775 { 776 777 if (sc->sc_msgoutlen >= NJSC32_MSGOUT_LEN) { 778 printf("njsc32_add_msgout: too many\n"); 779 return; 780 } 781 sc->sc_msgout[sc->sc_msgoutlen++] = byte; 782 } 783 784 static u_int32_t 785 njsc32_get_auto_msgout(struct njsc32_softc *sc) 786 { 787 u_int32_t val; 788 u_int8_t *p; 789 790 val = 0; 791 p = sc->sc_msgout; 792 switch (sc->sc_msgoutlen) { 793 /* 31-24 23-16 15-8 7 ... 1 0 */ 794 case 3: /* MSG3 MSG2 MSG1 V --- cnt */ 795 val |= *p++ << NJSC32_MSGOUT_MSG1_SHIFT; 796 /* FALLTHROUGH */ 797 798 case 2: /* MSG2 MSG1 --- V --- cnt */ 799 val |= *p++ << NJSC32_MSGOUT_MSG2_SHIFT; 800 /* FALLTHROUGH */ 801 802 case 1: /* MSG1 --- --- V --- cnt */ 803 val |= *p++ << NJSC32_MSGOUT_MSG3_SHIFT; 804 val |= NJSC32_MSGOUT_VALID | sc->sc_msgoutlen; 805 break; 806 807 default: 808 break; 809 } 810 return val; 811 } 812 813 #ifdef NJSC32_DUALEDGE 814 /* add Wide Data Transfer Request to the next Message Out */ 815 static void 816 njsc32_msgout_wdtr(struct njsc32_softc *sc, int width) 817 { 818 819 njsc32_add_msgout(sc, MSG_EXTENDED); 820 njsc32_add_msgout(sc, MSG_EXT_WDTR_LEN); 821 njsc32_add_msgout(sc, MSG_EXT_WDTR); 822 njsc32_add_msgout(sc, width); 823 } 824 #endif 825 826 /* add Synchronous Data Transfer Request to the next Message Out */ 827 static void 828 njsc32_msgout_sdtr(struct njsc32_softc *sc, int period, int offset) 829 { 830 831 njsc32_add_msgout(sc, MSG_EXTENDED); 832 njsc32_add_msgout(sc, MSG_EXT_SDTR_LEN); 833 njsc32_add_msgout(sc, MSG_EXT_SDTR); 834 njsc32_add_msgout(sc, period); 835 njsc32_add_msgout(sc, offset); 836 } 837 838 static void 839 njsc32_negotiate_xfer(struct njsc32_softc *sc, struct njsc32_target *target) 840 { 841 842 /* initial negotiation state */ 843 if (target->t_state == NJSC32_TARST_INIT) { 844 #ifdef NJSC32_DUALEDGE 845 if (target->t_flags & NJSC32_TARF_DE) 846 target->t_state = NJSC32_TARST_DE; 847 else 848 #endif 849 if (target->t_flags & NJSC32_TARF_SYNC) 850 target->t_state = NJSC32_TARST_SDTR; 851 else 852 target->t_state = NJSC32_TARST_DONE; 853 } 854 855 switch (target->t_state) { 856 default: 857 case NJSC32_TARST_INIT: 858 #ifdef DIAGNOSTIC 859 panic("njsc32_negotiate_xfer"); 860 /* NOTREACHED */ 861 #endif 862 /* FALLTHROUGH */ 863 case NJSC32_TARST_DONE: 864 /* no more work */ 865 break; 866 867 #ifdef NJSC32_DUALEDGE 868 case NJSC32_TARST_DE: 869 njsc32_msgout_wdtr(sc, 0xde /* XXX? */); 870 break; 871 872 case NJSC32_TARST_WDTR: 873 njsc32_msgout_wdtr(sc, MSG_EXT_WDTR_BUS_8_BIT); 874 break; 875 #endif 876 877 case NJSC32_TARST_SDTR: 878 njsc32_msgout_sdtr(sc, sc->sc_synct[sc->sc_sync_max].sp_period, 879 NJSC32_SYNCOFFSET_MAX); 880 break; 881 882 case NJSC32_TARST_ASYNC: 883 njsc32_msgout_sdtr(sc, NJSC32_SYNCPERIOD_ASYNC, 884 NJSC32_SYNCOFFSET_ASYNC); 885 break; 886 } 887 } 888 889 /* turn LED on */ 890 static inline void 891 njsc32_led_on(struct njsc32_softc *sc) 892 { 893 894 njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT, NJSC32_EXTPORT_LED_ON); 895 } 896 897 /* turn LED off */ 898 static inline void 899 njsc32_led_off(struct njsc32_softc *sc) 900 { 901 902 njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT, NJSC32_EXTPORT_LED_OFF); 903 } 904 905 static void 906 njsc32_arbitration_failed(struct njsc32_softc *sc) 907 { 908 struct njsc32_cmd *cmd; 909 910 if ((cmd = sc->sc_curcmd) == NULL || sc->sc_stat != NJSC32_STAT_ARBIT) 911 return; 912 913 if ((cmd->c_xs->xs_control & XS_CTL_POLL) == 0) 914 callout_stop(&cmd->c_xs->xs_callout); 915 916 sc->sc_stat = NJSC32_STAT_IDLE; 917 sc->sc_curcmd = NULL; 918 919 /* the command is no longer active */ 920 if (--sc->sc_nusedcmds == 0) 921 njsc32_led_off(sc); 922 } 923 924 static inline void 925 njsc32_cmd_load(struct njsc32_softc *sc, struct njsc32_cmd *cmd) 926 { 927 struct njsc32_target *target; 928 struct scsipi_xfer *xs; 929 int i, control, lun; 930 u_int32_t msgoutreg; 931 #ifdef NJSC32_AUTOPARAM 932 struct njsc32_autoparam *ap; 933 #endif 934 935 xs = cmd->c_xs; 936 #ifdef NJSC32_AUTOPARAM 937 ap = &sc->sc_cmdpg->dp_ap; 938 #else 939 /* reset CDB pointer */ 940 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, NJSC32_CMD_CLEAR_CDB_FIFO_PTR); 941 #endif 942 943 /* CDB */ 944 TPRINTC(cmd, ("njsc32_cmd_load: CDB")); 945 for (i = 0; i < xs->cmdlen; i++) { 946 #ifdef NJSC32_AUTOPARAM 947 ap->ap_cdb[i].cdb_data = ((u_int8_t *)xs->cmd)[i]; 948 #else 949 njsc32_write_1(sc, NJSC32_REG_COMMAND_DATA, 950 ((u_int8_t *)xs->cmd)[i]); 951 #endif 952 TPRINTF((" %02x", ((u_int8_t *)cmd->c_xs->cmd)[i])); 953 } 954 #ifdef NJSC32_AUTOPARAM /* XXX needed? */ 955 for ( ; i < NJSC32_AUTOPARAM_CDBLEN; i++) 956 ap->ap_cdb[i].cdb_data = 0; 957 #endif 958 959 control = xs->xs_control; 960 961 /* 962 * Message Out 963 */ 964 njsc32_init_msgout(sc); 965 966 /* Identify */ 967 lun = xs->xs_periph->periph_lun; 968 njsc32_add_msgout(sc, (control & XS_CTL_REQSENSE) ? 969 MSG_IDENTIFY(lun, 0) : MSG_IDENTIFY(lun, 1)); 970 971 /* tagged queueing */ 972 if (control & XS_CTL_TAGMASK) { 973 njsc32_add_msgout(sc, xs->xs_tag_type); 974 njsc32_add_msgout(sc, xs->xs_tag_id); 975 TPRINTF((" (tag %#x %#x)\n", xs->xs_tag_type, xs->xs_tag_id)); 976 } 977 TPRINTF(("\n")); 978 979 target = cmd->c_target; 980 981 /* transfer negotiation */ 982 if (control & XS_CTL_REQSENSE) 983 target->t_state = NJSC32_TARST_INIT; 984 njsc32_negotiate_xfer(sc, target); 985 986 msgoutreg = njsc32_get_auto_msgout(sc); 987 988 #ifdef NJSC32_AUTOPARAM 989 ap->ap_msgout = htole32(msgoutreg); 990 991 ap->ap_sync = target->t_sync; 992 ap->ap_ackwidth = target->t_ackwidth; 993 ap->ap_targetid = target->t_targetid; 994 ap->ap_sample = target->t_sample; 995 996 ap->ap_cmdctl = htole16(NJSC32_CMD_CLEAR_CDB_FIFO_PTR | 997 NJSC32_CMD_AUTO_COMMAND_PHASE | 998 NJSC32_CMD_AUTO_SCSI_START | NJSC32_CMD_AUTO_ATN | 999 NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02); 1000 #ifdef NJSC32_DUALEDGE 1001 ap->ap_xferctl = htole16(cmd->c_xferctl | target->t_xferctl); 1002 #else 1003 ap->ap_xferctl = htole16(cmd->c_xferctl); 1004 #endif 1005 ap->ap_sgtdmaaddr = htole32(cmd->c_sgtdmaaddr); 1006 1007 /* sync njsc32_autoparam */ 1008 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg, 1009 offsetof(struct njsc32_dma_page, dp_ap), /* offset */ 1010 sizeof(struct njsc32_autoparam), 1011 BUS_DMASYNC_PREWRITE); 1012 1013 /* autoparam DMA address */ 1014 njsc32_write_4(sc, NJSC32_REG_SGT_ADR, sc->sc_ap_dma); 1015 1016 /* start command (autoparam) */ 1017 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, 1018 NJSC32_CMD_CLEAR_CDB_FIFO_PTR | NJSC32_CMD_AUTO_PARAMETER); 1019 1020 #else /* not NJSC32_AUTOPARAM */ 1021 1022 njsc32_write_4(sc, NJSC32_REG_SCSI_MSG_OUT, msgoutreg); 1023 1024 /* load parameters */ 1025 njsc32_write_1(sc, NJSC32_REG_TARGET_ID, target->t_targetid); 1026 njsc32_write_1(sc, NJSC32_REG_SYNC, target->t_sync); 1027 njsc32_write_1(sc, NJSC32_REG_ACK_WIDTH, target->t_ackwidth); 1028 njsc32_write_1(sc, NJSC32_REG_SREQ_SAMPLING, target->t_sample); 1029 njsc32_write_4(sc, NJSC32_REG_SGT_ADR, cmd->c_sgtdmaaddr); 1030 #ifdef NJSC32_DUALEDGE 1031 njsc32_write_2(sc, NJSC32_REG_TRANSFER, 1032 cmd->c_xferctl | target->t_xferctl); 1033 #else 1034 njsc32_write_2(sc, NJSC32_REG_TRANSFER, cmd->c_xferctl); 1035 #endif 1036 /* start AutoSCSI */ 1037 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, 1038 NJSC32_CMD_CLEAR_CDB_FIFO_PTR | NJSC32_CMD_AUTO_COMMAND_PHASE | 1039 NJSC32_CMD_AUTO_SCSI_START | NJSC32_CMD_AUTO_ATN | 1040 NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02); 1041 #endif /* not NJSC32_AUTOPARAM */ 1042 } 1043 1044 /* Note: must be called at splbio() */ 1045 static void 1046 njsc32_start(struct njsc32_softc *sc) 1047 { 1048 struct njsc32_cmd *cmd; 1049 1050 /* get a command to issue */ 1051 TAILQ_FOREACH(cmd, &sc->sc_reqcmd, c_q) { 1052 if (cmd->c_lu->lu_cmd == NULL && 1053 ((cmd->c_flags & NJSC32_CMD_TAGGED) || 1054 TAILQ_EMPTY(&cmd->c_lu->lu_q))) 1055 break; /* OK, the logical unit is free */ 1056 } 1057 if (!cmd) 1058 goto out; /* no work to do */ 1059 1060 /* request will always fail if not in bus free phase */ 1061 if (njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) != 1062 NJSC32_BUSMON_BUSFREE) 1063 goto busy; 1064 1065 /* clear parity error and enable parity detection */ 1066 njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL, 1067 NJSC32_PARITYCTL_CHECK_ENABLE | NJSC32_PARITYCTL_CLEAR_ERROR); 1068 1069 njsc32_cmd_load(sc, cmd); 1070 1071 if (sc->sc_nusedcmds++ == 0) 1072 njsc32_led_on(sc); 1073 1074 sc->sc_curcmd = cmd; 1075 sc->sc_stat = NJSC32_STAT_ARBIT; 1076 1077 if ((cmd->c_xs->xs_control & XS_CTL_POLL) == 0) { 1078 callout_reset(&cmd->c_xs->xs_callout, 1079 mstohz(cmd->c_xs->timeout), 1080 njsc32_cmdtimeout, cmd); 1081 } 1082 1083 return; 1084 1085 busy: /* XXX retry counter */ 1086 TPRINTF(("%s: njsc32_start: busy\n", device_xname(sc->sc_dev))); 1087 njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_ARBITRATION_RETRY_TIME); 1088 out: njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0); 1089 } 1090 1091 static void 1092 njsc32_run_xfer(struct njsc32_softc *sc, struct scsipi_xfer *xs) 1093 { 1094 struct scsipi_periph *periph; 1095 int control; 1096 int lun; 1097 struct njsc32_cmd *cmd; 1098 int s, i, error; 1099 1100 periph = xs->xs_periph; 1101 KASSERT((unsigned)periph->periph_target <= NJSC32_MAX_TARGET_ID); 1102 1103 control = xs->xs_control; 1104 lun = periph->periph_lun; 1105 1106 /* 1107 * get a free cmd 1108 * (scsipi layer knows the number of cmds, so this shall never fail) 1109 */ 1110 s = splbio(); 1111 cmd = TAILQ_FIRST(&sc->sc_freecmd); 1112 KASSERT(cmd); 1113 TAILQ_REMOVE(&sc->sc_freecmd, cmd, c_q); 1114 splx(s); 1115 1116 /* 1117 * build a request 1118 */ 1119 njsc32_cmd_init(cmd); 1120 cmd->c_xs = xs; 1121 cmd->c_target = &sc->sc_targets[periph->periph_target]; 1122 cmd->c_lu = &cmd->c_target->t_lus[lun]; 1123 1124 /* tagged queueing */ 1125 if (control & XS_CTL_TAGMASK) { 1126 cmd->c_flags |= NJSC32_CMD_TAGGED; 1127 if (control & XS_CTL_HEAD_TAG) 1128 cmd->c_flags |= NJSC32_CMD_TAGGED_HEAD; 1129 } 1130 1131 /* map DMA buffer */ 1132 cmd->c_datacnt = xs->datalen; 1133 if (xs->datalen) { 1134 /* Is XS_CTL_DATA_UIO ever used anywhere? */ 1135 KASSERT((control & XS_CTL_DATA_UIO) == 0); 1136 1137 error = bus_dmamap_load(sc->sc_dmat, cmd->c_dmamap_xfer, 1138 xs->data, xs->datalen, NULL, 1139 ((control & XS_CTL_NOSLEEP) ? 1140 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | 1141 BUS_DMA_STREAMING | 1142 ((control & XS_CTL_DATA_IN) ? 1143 BUS_DMA_READ : BUS_DMA_WRITE)); 1144 1145 switch (error) { 1146 case 0: 1147 break; 1148 case ENOMEM: 1149 case EAGAIN: 1150 xs->error = XS_RESOURCE_SHORTAGE; 1151 goto map_failed; 1152 default: 1153 xs->error = XS_DRIVER_STUFFUP; 1154 map_failed: 1155 printf("%s: njsc32_run_xfer: map failed, error %d\n", 1156 device_xname(sc->sc_dev), error); 1157 /* put it back to free command list */ 1158 s = splbio(); 1159 TAILQ_INSERT_HEAD(&sc->sc_freecmd, cmd, c_q); 1160 splx(s); 1161 /* abort this transfer */ 1162 scsipi_done(xs); 1163 return; 1164 } 1165 1166 bus_dmamap_sync(sc->sc_dmat, cmd->c_dmamap_xfer, 1167 0, cmd->c_dmamap_xfer->dm_mapsize, 1168 (control & XS_CTL_DATA_IN) ? 1169 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 1170 1171 for (i = 0; i < cmd->c_dmamap_xfer->dm_nsegs; i++) { 1172 cmd->c_sgt[i].sg_addr = 1173 htole32(cmd->c_dmamap_xfer->dm_segs[i].ds_addr); 1174 cmd->c_sgt[i].sg_len = 1175 htole32(cmd->c_dmamap_xfer->dm_segs[i].ds_len); 1176 } 1177 /* end mark */ 1178 cmd->c_sgt[i - 1].sg_len |= htole32(NJSC32_SGT_ENDMARK); 1179 1180 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg, 1181 (char *)cmd->c_sgt - (char *)sc->sc_cmdpg, /* offset */ 1182 NJSC32_SIZE_SGT, 1183 BUS_DMASYNC_PREWRITE); 1184 1185 cmd->c_flags |= NJSC32_CMD_DMA_MAPPED; 1186 1187 /* enable transfer */ 1188 cmd->c_xferctl = 1189 NJSC32_XFR_TRANSFER_GO | NJSC32_XFR_BM_START | 1190 NJSC32_XFR_ALL_COUNT_CLR; 1191 1192 /* XXX How can we specify the DMA direction? */ 1193 1194 #if 0 /* faster write mode? (doesn't work) */ 1195 if ((control & XS_CTL_DATA_IN) == 0) 1196 cmd->c_xferctl |= NJSC32_XFR_ADVANCED_BM_WRITE; 1197 #endif 1198 } else { 1199 /* no data transfer */ 1200 cmd->c_xferctl = 0; 1201 } 1202 1203 /* queue request */ 1204 s = splbio(); 1205 TAILQ_INSERT_TAIL(&sc->sc_reqcmd, cmd, c_q); 1206 1207 /* start the controller if idle */ 1208 if (sc->sc_stat == NJSC32_STAT_IDLE) 1209 njsc32_start(sc); 1210 1211 splx(s); 1212 1213 if (control & XS_CTL_POLL) { 1214 /* wait for completion */ 1215 /* XXX should handle timeout? */ 1216 while ((xs->xs_status & XS_STS_DONE) == 0) { 1217 delay(1000); 1218 njsc32_intr(sc); 1219 } 1220 } 1221 } 1222 1223 static void 1224 njsc32_end_cmd(struct njsc32_softc *sc, struct njsc32_cmd *cmd, 1225 scsipi_xfer_result_t result) 1226 { 1227 struct scsipi_xfer *xs; 1228 int s; 1229 #ifdef DIAGNOSTIC 1230 struct njsc32_cmd *c; 1231 #endif 1232 1233 KASSERT(cmd); 1234 1235 #ifdef DIAGNOSTIC 1236 s = splbio(); 1237 TAILQ_FOREACH(c, &sc->sc_freecmd, c_q) { 1238 if (cmd == c) 1239 panic("njsc32_end_cmd: already in free list"); 1240 } 1241 splx(s); 1242 #endif 1243 xs = cmd->c_xs; 1244 1245 if (cmd->c_flags & NJSC32_CMD_DMA_MAPPED) { 1246 if (cmd->c_datacnt) { 1247 bus_dmamap_sync(sc->sc_dmat, cmd->c_dmamap_xfer, 1248 0, cmd->c_dmamap_xfer->dm_mapsize, 1249 (xs->xs_control & XS_CTL_DATA_IN) ? 1250 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1251 1252 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg, 1253 (char *)cmd->c_sgt - (char *)sc->sc_cmdpg, 1254 NJSC32_SIZE_SGT, BUS_DMASYNC_POSTWRITE); 1255 } 1256 1257 bus_dmamap_unload(sc->sc_dmat, cmd->c_dmamap_xfer); 1258 cmd->c_flags &= ~NJSC32_CMD_DMA_MAPPED; 1259 } 1260 1261 s = splbio(); 1262 if ((xs->xs_control & XS_CTL_POLL) == 0) 1263 callout_stop(&xs->xs_callout); 1264 1265 TAILQ_INSERT_HEAD(&sc->sc_freecmd, cmd, c_q); 1266 splx(s); 1267 1268 xs->error = result; 1269 scsipi_done(xs); 1270 1271 if (--sc->sc_nusedcmds == 0) 1272 njsc32_led_off(sc); 1273 } 1274 1275 /* 1276 * request from scsipi layer 1277 */ 1278 static void 1279 njsc32_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 1280 void *arg) 1281 { 1282 struct njsc32_softc *sc; 1283 struct scsipi_xfer_mode *xm; 1284 struct njsc32_target *target; 1285 1286 sc = device_private(chan->chan_adapter->adapt_dev); 1287 1288 switch (req) { 1289 case ADAPTER_REQ_RUN_XFER: 1290 njsc32_run_xfer(sc, arg); 1291 break; 1292 1293 case ADAPTER_REQ_GROW_RESOURCES: 1294 /* not supported */ 1295 break; 1296 1297 case ADAPTER_REQ_SET_XFER_MODE: 1298 xm = arg; 1299 target = &sc->sc_targets[xm->xm_target]; 1300 1301 target->t_flags = 0; 1302 if (xm->xm_mode & PERIPH_CAP_TQING) 1303 target->t_flags |= NJSC32_TARF_TAG; 1304 if (xm->xm_mode & PERIPH_CAP_SYNC) { 1305 target->t_flags |= NJSC32_TARF_SYNC; 1306 #ifdef NJSC32_DUALEDGE 1307 if (sc->sc_model & NJSC32_FLAG_DUALEDGE) 1308 target->t_flags |= NJSC32_TARF_DE; 1309 #endif 1310 } 1311 #ifdef NJSC32_DUALEDGE 1312 target->t_xferctl = 0; 1313 #endif 1314 target->t_state = NJSC32_TARST_INIT; 1315 njsc32_target_async(sc, target); 1316 1317 break; 1318 default: 1319 break; 1320 } 1321 } 1322 1323 static void 1324 njsc32_scsipi_minphys(struct buf *bp) 1325 { 1326 1327 if (bp->b_bcount > NJSC32_MAX_XFER) 1328 bp->b_bcount = NJSC32_MAX_XFER; 1329 minphys(bp); 1330 } 1331 1332 /* 1333 * On some versions of 32UDE (probably the earlier ones), the controller 1334 * detects continuous bus reset when the termination power is absent. 1335 * Make sure the system won't hang on such situation. 1336 */ 1337 static void 1338 njsc32_wait_reset_release(void *arg) 1339 { 1340 struct njsc32_softc *sc = arg; 1341 struct njsc32_cmd *cmd; 1342 1343 /* clear pending commands */ 1344 while ((cmd = TAILQ_FIRST(&sc->sc_reqcmd)) != NULL) { 1345 TAILQ_REMOVE(&sc->sc_reqcmd, cmd, c_q); 1346 njsc32_end_cmd(sc, cmd, XS_RESET); 1347 } 1348 1349 /* If Bus Reset is not released yet, schedule recheck. */ 1350 if (njsc32_read_2(sc, NJSC32_REG_IRQ) & NJSC32_IRQ_SCSIRESET) { 1351 switch (sc->sc_stat) { 1352 case NJSC32_STAT_RESET: 1353 sc->sc_stat = NJSC32_STAT_RESET1; 1354 break; 1355 case NJSC32_STAT_RESET1: 1356 /* print message if Bus Reset is detected twice */ 1357 sc->sc_stat = NJSC32_STAT_RESET2; 1358 printf("%s: detected excessive bus reset " 1359 "--- missing termination power?\n", 1360 device_xname(sc->sc_dev)); 1361 break; 1362 default: 1363 break; 1364 } 1365 callout_reset(&sc->sc_callout, 1366 hz * 2 /* poll every 2s */, 1367 njsc32_wait_reset_release, sc); 1368 return; 1369 } 1370 1371 if (sc->sc_stat == NJSC32_STAT_RESET2) 1372 printf("%s: bus reset is released\n", device_xname(sc->sc_dev)); 1373 1374 /* unblock interrupts */ 1375 njsc32_write_2(sc, NJSC32_REG_IRQ, 0); 1376 1377 sc->sc_stat = NJSC32_STAT_IDLE; 1378 } 1379 1380 static void 1381 njsc32_reset_bus(struct njsc32_softc *sc) 1382 { 1383 int s; 1384 1385 DPRINTF(("%s: njsc32_reset_bus:\n", device_xname(sc->sc_dev))); 1386 1387 /* block interrupts */ 1388 njsc32_write_2(sc, NJSC32_REG_IRQ, NJSC32_IRQ_MASK_ALL); 1389 1390 sc->sc_stat = NJSC32_STAT_RESET; 1391 1392 /* hold SCSI bus reset */ 1393 njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, NJSC32_SBCTL_RST); 1394 delay(NJSC32_RESET_HOLD_TIME); 1395 1396 /* clear transfer */ 1397 njsc32_clear_cmds(sc, XS_RESET); 1398 1399 /* initialize target structure */ 1400 njsc32_init_targets(sc); 1401 1402 s = splbio(); 1403 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_RESET, NULL); 1404 splx(s); 1405 1406 /* release SCSI bus reset */ 1407 njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, 0); 1408 1409 njsc32_wait_reset_release(sc); 1410 } 1411 1412 /* 1413 * clear running/disconnected commands 1414 */ 1415 static void 1416 njsc32_clear_cmds(struct njsc32_softc *sc, scsipi_xfer_result_t cmdresult) 1417 { 1418 struct njsc32_cmd *cmd; 1419 int id, lun; 1420 struct njsc32_lu *lu; 1421 1422 njsc32_arbitration_failed(sc); 1423 1424 /* clear current transfer */ 1425 if ((cmd = sc->sc_curcmd) != NULL) { 1426 sc->sc_curcmd = NULL; 1427 njsc32_end_cmd(sc, cmd, cmdresult); 1428 } 1429 1430 /* clear disconnected transfers */ 1431 for (id = 0; id <= NJSC32_MAX_TARGET_ID; id++) { 1432 for (lun = 0; lun < NJSC32_NLU; lun++) { 1433 lu = &sc->sc_targets[id].t_lus[lun]; 1434 1435 if ((cmd = lu->lu_cmd) != NULL) { 1436 lu->lu_cmd = NULL; 1437 njsc32_end_cmd(sc, cmd, cmdresult); 1438 } 1439 while ((cmd = TAILQ_FIRST(&lu->lu_q)) != NULL) { 1440 TAILQ_REMOVE(&lu->lu_q, cmd, c_q); 1441 njsc32_end_cmd(sc, cmd, cmdresult); 1442 } 1443 } 1444 } 1445 } 1446 1447 static int 1448 njsc32_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, 1449 void *addr, int flag, struct proc *p) 1450 { 1451 struct njsc32_softc *sc; 1452 1453 sc = device_private(chan->chan_adapter->adapt_dev); 1454 1455 switch (cmd) { 1456 case SCBUSIORESET: 1457 njsc32_init(sc, 0); 1458 return 0; 1459 default: 1460 break; 1461 } 1462 1463 return ENOTTY; 1464 } 1465 1466 /* 1467 * set current data pointer 1468 */ 1469 static inline void 1470 njsc32_set_cur_ptr(struct njsc32_cmd *cmd, u_int32_t pos) 1471 { 1472 1473 /* new current data pointer */ 1474 cmd->c_dp_cur = pos; 1475 1476 /* update number of bytes transferred */ 1477 if (pos > cmd->c_dp_max) 1478 cmd->c_dp_max = pos; 1479 } 1480 1481 /* 1482 * set data pointer for the next transfer 1483 */ 1484 static void 1485 njsc32_set_ptr(struct njsc32_softc *sc, struct njsc32_cmd *cmd, u_int32_t pos) 1486 { 1487 struct njsc32_sgtable *sg; 1488 unsigned sgte; 1489 u_int32_t len; 1490 1491 /* set current pointer */ 1492 njsc32_set_cur_ptr(cmd, pos); 1493 1494 /* undo previous fix if any */ 1495 if (cmd->c_sgfixcnt != 0) { 1496 sg = &cmd->c_sgt[cmd->c_sgoffset]; 1497 sg->sg_addr = htole32(le32toh(sg->sg_addr) - cmd->c_sgfixcnt); 1498 sg->sg_len = htole32(le32toh(sg->sg_len) + cmd->c_sgfixcnt); 1499 cmd->c_sgfixcnt = 0; 1500 } 1501 1502 if (pos >= cmd->c_datacnt) { 1503 /* transfer done */ 1504 #if 1 /*def DIAGNOSTIC*/ 1505 if (pos > cmd->c_datacnt) 1506 printf("%s: pos %u too large\n", 1507 device_xname(sc->sc_dev), pos - cmd->c_datacnt); 1508 #endif 1509 cmd->c_xferctl = 0; /* XXX correct? */ 1510 1511 return; 1512 } 1513 1514 for (sgte = 0, sg = cmd->c_sgt; 1515 sgte < NJSC32_NUM_SG && pos > 0; sgte++, sg++) { 1516 len = le32toh(sg->sg_len) & ~NJSC32_SGT_ENDMARK; 1517 if (pos < len) { 1518 sg->sg_addr = htole32(le32toh(sg->sg_addr) + pos); 1519 sg->sg_len = htole32(le32toh(sg->sg_len) - pos); 1520 cmd->c_sgfixcnt = pos; 1521 break; 1522 } 1523 pos -= len; 1524 #ifdef DIAGNOSTIC 1525 if (sg->sg_len & htole32(NJSC32_SGT_ENDMARK)) { 1526 panic("njsc32_set_ptr: bad pos"); 1527 } 1528 #endif 1529 } 1530 #ifdef DIAGNOSTIC 1531 if (sgte >= NJSC32_NUM_SG) 1532 panic("njsc32_set_ptr: bad sg"); 1533 #endif 1534 if (cmd->c_sgoffset != sgte) { 1535 cmd->c_sgoffset = sgte; 1536 cmd->c_sgtdmaaddr = NJSC32_CMD_DMAADDR_SGT(cmd, sgte); 1537 } 1538 1539 /* XXX overkill */ 1540 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg, 1541 (char *)cmd->c_sgt - (char *)sc->sc_cmdpg, /* offset */ 1542 NJSC32_SIZE_SGT, 1543 BUS_DMASYNC_PREWRITE); 1544 } 1545 1546 /* 1547 * save data pointer 1548 */ 1549 static inline void 1550 njsc32_save_ptr(struct njsc32_cmd *cmd) 1551 { 1552 1553 cmd->c_dp_saved = cmd->c_dp_cur; 1554 } 1555 1556 static void 1557 njsc32_assert_ack(struct njsc32_softc *sc) 1558 { 1559 u_int8_t reg; 1560 1561 reg = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_CONTROL); 1562 reg |= NJSC32_SBCTL_ACK | NJSC32_SBCTL_ACK_ENABLE; 1563 #if 0 /* needed? */ 1564 reg |= NJSC32_SBCTL_AUTODIRECTION; 1565 #endif 1566 njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, reg); 1567 } 1568 1569 static void 1570 njsc32_negate_ack(struct njsc32_softc *sc) 1571 { 1572 u_int8_t reg; 1573 1574 reg = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_CONTROL); 1575 #if 0 /* needed? */ 1576 reg |= NJSC32_SBCTL_ACK_ENABLE; 1577 reg |= NJSC32_SBCTL_AUTODIRECTION; 1578 #endif 1579 reg &= ~NJSC32_SBCTL_ACK; 1580 njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, reg); 1581 } 1582 1583 static void 1584 njsc32_wait_req_negate(struct njsc32_softc *sc) 1585 { 1586 int cnt; 1587 1588 for (cnt = 0; cnt < NJSC32_REQ_TIMEOUT; cnt++) { 1589 if ((njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) & 1590 NJSC32_BUSMON_REQ) == 0) 1591 return; 1592 delay(1); 1593 } 1594 printf("%s: njsc32_wait_req_negate: timed out\n", 1595 device_xname(sc->sc_dev)); 1596 } 1597 1598 static void 1599 njsc32_reconnect(struct njsc32_softc *sc, struct njsc32_cmd *cmd) 1600 { 1601 struct scsipi_xfer *xs; 1602 1603 xs = cmd->c_xs; 1604 if ((xs->xs_control & XS_CTL_POLL) == 0) { 1605 callout_stop(&xs->xs_callout); 1606 callout_reset(&xs->xs_callout, 1607 mstohz(xs->timeout), 1608 njsc32_cmdtimeout, cmd); 1609 } 1610 1611 /* Reconnection implies Restore Pointers */ 1612 njsc32_set_ptr(sc, cmd, cmd->c_dp_saved); 1613 } 1614 1615 static enum njsc32_reselstat 1616 njsc32_resel_identify(struct njsc32_softc *sc, int lun, 1617 struct njsc32_cmd **pcmd) 1618 { 1619 int targetid; 1620 struct njsc32_lu *plu; 1621 struct njsc32_cmd *cmd; 1622 1623 switch (sc->sc_stat) { 1624 case NJSC32_STAT_RESEL: 1625 break; /* OK */ 1626 1627 case NJSC32_STAT_RESEL_LUN: 1628 case NJSC32_STAT_RECONNECT: 1629 /* 1630 * accept and ignore if the LUN is the same as the current one, 1631 * reject otherwise. 1632 */ 1633 return sc->sc_resellun == lun ? 1634 NJSC32_RESEL_THROUGH : NJSC32_RESEL_ERROR; 1635 1636 default: 1637 printf("%s: njsc32_resel_identify: not in reselection\n", 1638 device_xname(sc->sc_dev)); 1639 return NJSC32_RESEL_ERROR; 1640 } 1641 1642 targetid = sc->sc_reselid; 1643 TPRINTF(("%s: njsc32_resel_identify: reselection lun %d\n", 1644 device_xname(sc->sc_dev), lun)); 1645 1646 if (targetid > NJSC32_MAX_TARGET_ID || lun >= NJSC32_NLU) 1647 return NJSC32_RESEL_ERROR; 1648 1649 sc->sc_resellun = lun; 1650 plu = &sc->sc_targets[targetid].t_lus[lun]; 1651 1652 if ((cmd = plu->lu_cmd) != NULL) { 1653 sc->sc_stat = NJSC32_STAT_RECONNECT; 1654 plu->lu_cmd = NULL; 1655 *pcmd = cmd; 1656 TPRINTC(cmd, ("njsc32_resel_identify: I_T_L nexus\n")); 1657 njsc32_reconnect(sc, cmd); 1658 return NJSC32_RESEL_COMPLETE; 1659 } else if (!TAILQ_EMPTY(&plu->lu_q)) { 1660 /* wait for tag */ 1661 sc->sc_stat = NJSC32_STAT_RESEL_LUN; 1662 return NJSC32_RESEL_THROUGH; 1663 } 1664 1665 /* no disconnected commands */ 1666 return NJSC32_RESEL_ERROR; 1667 } 1668 1669 static enum njsc32_reselstat 1670 njsc32_resel_tag(struct njsc32_softc *sc, int tag, struct njsc32_cmd **pcmd) 1671 { 1672 struct njsc32_cmd_head *head; 1673 struct njsc32_cmd *cmd; 1674 1675 TPRINTF(("%s: njsc32_resel_tag: reselection tag %d\n", 1676 device_xname(sc->sc_dev), tag)); 1677 if (sc->sc_stat != NJSC32_STAT_RESEL_LUN) 1678 return NJSC32_RESEL_ERROR; 1679 1680 head = &sc->sc_targets[sc->sc_reselid].t_lus[sc->sc_resellun].lu_q; 1681 1682 /* XXX slow? */ 1683 /* search for the command of the tag */ 1684 TAILQ_FOREACH(cmd, head, c_q) { 1685 if (cmd->c_xs->xs_tag_id == tag) { 1686 sc->sc_stat = NJSC32_STAT_RECONNECT; 1687 TAILQ_REMOVE(head, cmd, c_q); 1688 *pcmd = cmd; 1689 TPRINTC(cmd, ("njsc32_resel_tag: I_T_L_Q nexus\n")); 1690 njsc32_reconnect(sc, cmd); 1691 return NJSC32_RESEL_COMPLETE; 1692 } 1693 } 1694 1695 /* no disconnected commands */ 1696 return NJSC32_RESEL_ERROR; 1697 } 1698 1699 /* 1700 * Reload parameters and restart AutoSCSI. 1701 * 1702 * XXX autoparam doesn't work as expected and we can't use it here. 1703 */ 1704 static void 1705 njsc32_cmd_reload(struct njsc32_softc *sc, struct njsc32_cmd *cmd, int cctl) 1706 { 1707 struct njsc32_target *target; 1708 1709 target = cmd->c_target; 1710 1711 /* clear parity error and enable parity detection */ 1712 njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL, 1713 NJSC32_PARITYCTL_CHECK_ENABLE | NJSC32_PARITYCTL_CLEAR_ERROR); 1714 1715 /* load parameters */ 1716 njsc32_write_1(sc, NJSC32_REG_SYNC, target->t_sync); 1717 njsc32_write_1(sc, NJSC32_REG_ACK_WIDTH, target->t_ackwidth); 1718 njsc32_write_1(sc, NJSC32_REG_SREQ_SAMPLING, target->t_sample); 1719 njsc32_write_4(sc, NJSC32_REG_SGT_ADR, cmd->c_sgtdmaaddr); 1720 #ifdef NJSC32_DUALEDGE 1721 njsc32_write_2(sc, NJSC32_REG_TRANSFER, 1722 cmd->c_xferctl | target->t_xferctl); 1723 #else 1724 njsc32_write_2(sc, NJSC32_REG_TRANSFER, cmd->c_xferctl); 1725 #endif 1726 /* start AutoSCSI */ 1727 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl); 1728 1729 sc->sc_curcmd = cmd; 1730 } 1731 1732 static void 1733 njsc32_update_xfer_mode(struct njsc32_softc *sc, struct njsc32_target *target) 1734 { 1735 struct scsipi_xfer_mode xm; 1736 1737 xm.xm_target = target - sc->sc_targets; /* target ID */ 1738 xm.xm_mode = 0; 1739 xm.xm_period = target->t_syncperiod; 1740 xm.xm_offset = target->t_syncoffset; 1741 if (xm.xm_offset != 0) 1742 xm.xm_mode |= PERIPH_CAP_SYNC; 1743 if (target->t_flags & NJSC32_TARF_TAG) 1744 xm.xm_mode |= PERIPH_CAP_TQING; 1745 1746 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm); 1747 } 1748 1749 static void 1750 njsc32_msgin(struct njsc32_softc *sc) 1751 { 1752 u_int8_t msg0, msg; 1753 int msgcnt; 1754 struct njsc32_cmd *cmd; 1755 enum njsc32_reselstat rstat; 1756 int cctl = 0; 1757 u_int32_t ptr; /* unsigned type ensures 2-complement calculation */ 1758 u_int32_t msgout = 0; 1759 bool reload_params = FALSE; 1760 struct njsc32_target *target; 1761 int idx, period, offset; 1762 1763 /* 1764 * we are in Message In, so the previous Message Out should have 1765 * been done. 1766 */ 1767 njsc32_init_msgout(sc); 1768 1769 /* get a byte of Message In */ 1770 msg = njsc32_read_1(sc, NJSC32_REG_DATA_IN); 1771 TPRINTF(("%s: njsc32_msgin: got %#x\n", device_xname(sc->sc_dev), msg)); 1772 if ((msgcnt = sc->sc_msgincnt) < NJSC32_MSGIN_LEN) 1773 sc->sc_msginbuf[sc->sc_msgincnt] = msg; 1774 1775 njsc32_assert_ack(sc); 1776 1777 msg0 = sc->sc_msginbuf[0]; 1778 cmd = sc->sc_curcmd; 1779 1780 /* check for parity error */ 1781 if (njsc32_read_1(sc, NJSC32_REG_PARITY_STATUS) & 1782 NJSC32_PARITYSTATUS_ERROR_LSB) { 1783 1784 printf("%s: msgin: parity error\n", device_xname(sc->sc_dev)); 1785 1786 /* clear parity error */ 1787 njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL, 1788 NJSC32_PARITYCTL_CHECK_ENABLE | 1789 NJSC32_PARITYCTL_CLEAR_ERROR); 1790 1791 /* respond as Message Parity Error */ 1792 njsc32_add_msgout(sc, MSG_PARITY_ERROR); 1793 1794 /* clear Message In */ 1795 sc->sc_msgincnt = 0; 1796 goto reply; 1797 } 1798 1799 #define WAITNEXTMSG do { sc->sc_msgincnt++; goto restart; } while (0) 1800 #define MSGCOMPLETE do { sc->sc_msgincnt = 0; goto restart; } while (0) 1801 if (MSG_ISIDENTIFY(msg0)) { 1802 /* 1803 * Got Identify message from target. 1804 */ 1805 if ((msg0 & ~MSG_IDENTIFY_LUNMASK) != MSG_IDENTIFYFLAG || 1806 (rstat = njsc32_resel_identify(sc, msg0 & 1807 MSG_IDENTIFY_LUNMASK, &cmd)) == NJSC32_RESEL_ERROR) { 1808 /* 1809 * invalid Identify -> Reject 1810 */ 1811 goto reject; 1812 } 1813 if (rstat == NJSC32_RESEL_COMPLETE) 1814 reload_params = TRUE; 1815 MSGCOMPLETE; 1816 } 1817 1818 if (msg0 == MSG_SIMPLE_Q_TAG) { 1819 if (msgcnt == 0) 1820 WAITNEXTMSG; 1821 1822 /* got whole message */ 1823 sc->sc_msgincnt = 0; 1824 1825 if ((rstat = njsc32_resel_tag(sc, sc->sc_msginbuf[1], &cmd)) 1826 == NJSC32_RESEL_ERROR) { 1827 /* 1828 * invalid Simple Queue Tag -> Abort Tag 1829 */ 1830 printf("%s: msgin: invalid tag\n", 1831 device_xname(sc->sc_dev)); 1832 njsc32_add_msgout(sc, MSG_ABORT_TAG); 1833 goto reply; 1834 } 1835 if (rstat == NJSC32_RESEL_COMPLETE) 1836 reload_params = TRUE; 1837 MSGCOMPLETE; 1838 } 1839 1840 /* I_T_L or I_T_L_Q nexus should be established now */ 1841 if (cmd == NULL) { 1842 printf("%s: msgin %#x without nexus -- sending abort\n", 1843 device_xname(sc->sc_dev), msg0); 1844 njsc32_add_msgout(sc, MSG_ABORT); 1845 goto reply; 1846 } 1847 1848 /* 1849 * extended message 1850 * 0x01 <length (0 stands for 256)> <length bytes> 1851 * (<code> [<parameter> ...]) 1852 */ 1853 #define EXTLENOFF 1 1854 #define EXTCODEOFF 2 1855 if (msg0 == MSG_EXTENDED) { 1856 if (msgcnt < EXTLENOFF || 1857 msgcnt < EXTLENOFF + 1 + 1858 (u_int8_t)(sc->sc_msginbuf[EXTLENOFF] - 1)) 1859 WAITNEXTMSG; 1860 1861 /* got whole message */ 1862 sc->sc_msgincnt = 0; 1863 1864 switch (sc->sc_msginbuf[EXTCODEOFF]) { 1865 case 0: /* Modify Data Pointer */ 1866 if (msgcnt != 5 + EXTCODEOFF - 1) 1867 break; 1868 /* 1869 * parameter is 32bit big-endian signed (2-complement) 1870 * value 1871 */ 1872 ptr = (sc->sc_msginbuf[EXTCODEOFF + 1] << 24) | 1873 (sc->sc_msginbuf[EXTCODEOFF + 2] << 16) | 1874 (sc->sc_msginbuf[EXTCODEOFF + 3] << 8) | 1875 sc->sc_msginbuf[EXTCODEOFF + 4]; 1876 1877 /* new pointer */ 1878 ptr += cmd->c_dp_cur; /* ignore overflow */ 1879 1880 /* reject if ptr is not in data buffer */ 1881 if (ptr > cmd->c_datacnt) 1882 break; 1883 1884 njsc32_set_ptr(sc, cmd, ptr); 1885 goto restart; 1886 1887 case MSG_EXT_SDTR: /* Synchronous Data Transfer Request */ 1888 DPRINTC(cmd, ("SDTR %#x %#x\n", 1889 sc->sc_msginbuf[EXTCODEOFF + 1], 1890 sc->sc_msginbuf[EXTCODEOFF + 2])); 1891 if (msgcnt != MSG_EXT_SDTR_LEN + EXTCODEOFF-1) 1892 break; /* reject */ 1893 1894 target = cmd->c_target; 1895 1896 /* lookup sync period parameters */ 1897 period = sc->sc_msginbuf[EXTCODEOFF + 1]; 1898 for (idx = sc->sc_sync_max; idx < NJSC32_NSYNCT; idx++) 1899 if (sc->sc_synct[idx].sp_period >= period) { 1900 period = sc->sc_synct[idx].sp_period; 1901 break; 1902 } 1903 if (idx >= NJSC32_NSYNCT) { 1904 /* 1905 * We can't meet the timing condition that 1906 * the target requests -- use async. 1907 */ 1908 njsc32_target_async(sc, target); 1909 njsc32_update_xfer_mode(sc, target); 1910 if (target->t_state == NJSC32_TARST_SDTR) { 1911 /* 1912 * We started SDTR exchange -- start 1913 * negotiation again and request async. 1914 */ 1915 target->t_state = NJSC32_TARST_ASYNC; 1916 njsc32_negotiate_xfer(sc, target); 1917 goto reply; 1918 } else { 1919 /* 1920 * The target started SDTR exchange 1921 * -- just reject and fallback 1922 * to async. 1923 */ 1924 goto reject; 1925 } 1926 } 1927 1928 /* check sync offset */ 1929 offset = sc->sc_msginbuf[EXTCODEOFF + 2]; 1930 if (offset > NJSC32_SYNCOFFSET_MAX) { 1931 if (target->t_state == NJSC32_TARST_SDTR) { 1932 printf("%s: wrong sync offset: %d\n", 1933 device_xname(sc->sc_dev), offset); 1934 /* XXX what to do? */ 1935 } 1936 offset = NJSC32_SYNCOFFSET_MAX; 1937 } 1938 1939 target->t_ackwidth = sc->sc_synct[idx].sp_ackw; 1940 target->t_sample = sc->sc_synct[idx].sp_sample; 1941 target->t_syncperiod = period; 1942 target->t_syncoffset = offset; 1943 target->t_sync = NJSC32_SYNC_VAL(idx, offset); 1944 njsc32_update_xfer_mode(sc, target); 1945 1946 if (target->t_state == NJSC32_TARST_SDTR) { 1947 target->t_state = NJSC32_TARST_DONE; 1948 } else { 1949 njsc32_msgout_sdtr(sc, period, offset); 1950 goto reply; 1951 } 1952 goto restart; 1953 1954 case MSG_EXT_WDTR: /* Wide Data Transfer Request */ 1955 DPRINTC(cmd, 1956 ("WDTR %#x\n", sc->sc_msginbuf[EXTCODEOFF + 1])); 1957 #ifdef NJSC32_DUALEDGE 1958 if (msgcnt != MSG_EXT_WDTR_LEN + EXTCODEOFF-1) 1959 break; /* reject */ 1960 1961 /* 1962 * T->I of this message is not used for 1963 * DualEdge negotiation, so the device 1964 * must not be a DualEdge device. 1965 * 1966 * XXX correct? 1967 */ 1968 target = cmd->c_target; 1969 target->t_xferctl = 0; 1970 1971 switch (target->t_state) { 1972 case NJSC32_TARST_DE: 1973 if (sc->sc_msginbuf[EXTCODEOFF + 1] != 1974 MSG_EXT_WDTR_BUS_8_BIT) { 1975 /* 1976 * Oops, we got unexpected WDTR. 1977 * Negotiate for 8bit. 1978 */ 1979 target->t_state = NJSC32_TARST_WDTR; 1980 } else { 1981 target->t_state = NJSC32_TARST_SDTR; 1982 } 1983 njsc32_negotiate_xfer(sc, target); 1984 goto reply; 1985 1986 case NJSC32_TARST_WDTR: 1987 if (sc->sc_msginbuf[EXTCODEOFF + 1] != 1988 MSG_EXT_WDTR_BUS_8_BIT) { 1989 printf("%s: unexpected transfer width:" 1990 " %#x\n", device_xname(sc->sc_dev), 1991 sc->sc_msginbuf[EXTCODEOFF + 1]); 1992 /* XXX what to do? */ 1993 } 1994 target->t_state = NJSC32_TARST_SDTR; 1995 njsc32_negotiate_xfer(sc, target); 1996 goto reply; 1997 1998 default: 1999 /* the target started WDTR exchange */ 2000 DPRINTC(cmd, ("WDTR from target\n")); 2001 2002 target->t_state = NJSC32_TARST_SDTR; 2003 njsc32_target_async(sc, target); 2004 2005 break; /* reject the WDTR (8bit transfer) */ 2006 } 2007 #endif /* NJSC32_DUALEDGE */ 2008 break; /* reject */ 2009 } 2010 DPRINTC(cmd, ("njsc32_msgin: reject ext msg %#x msgincnt %d\n", 2011 sc->sc_msginbuf[EXTCODEOFF], msgcnt)); 2012 goto reject; 2013 } 2014 2015 /* 2byte messages */ 2016 if (MSG_IS2BYTE(msg0)) { 2017 if (msgcnt == 0) 2018 WAITNEXTMSG; 2019 2020 /* got whole message */ 2021 sc->sc_msgincnt = 0; 2022 } 2023 2024 switch (msg0) { 2025 case MSG_CMDCOMPLETE: /* 0x00 */ 2026 case MSG_SAVEDATAPOINTER: /* 0x02 */ 2027 case MSG_DISCONNECT: /* 0x04 */ 2028 /* handled by AutoSCSI */ 2029 PRINTC(cmd, ("msgin: unexpected msg: %#x\n", msg0)); 2030 break; 2031 2032 case MSG_RESTOREPOINTERS: /* 0x03 */ 2033 /* restore data pointer to what was saved */ 2034 DPRINTC(cmd, ("njsc32_msgin: Restore Pointers\n")); 2035 njsc32_set_ptr(sc, cmd, cmd->c_dp_saved); 2036 reload_params = TRUE; 2037 MSGCOMPLETE; 2038 /* NOTREACHED */ 2039 break; 2040 2041 #if 0 /* handled above */ 2042 case MSG_EXTENDED: /* 0x01 */ 2043 #endif 2044 case MSG_MESSAGE_REJECT: /* 0x07 */ 2045 target = cmd->c_target; 2046 DPRINTC(cmd, ("Reject tarst %d\n", target->t_state)); 2047 switch (target->t_state) { 2048 #ifdef NJSC32_DUALEDGE 2049 case NJSC32_TARST_WDTR: 2050 case NJSC32_TARST_DE: 2051 target->t_xferctl = 0; 2052 target->t_state = NJSC32_TARST_SDTR; 2053 njsc32_negotiate_xfer(sc, target); 2054 goto reply; 2055 #endif 2056 case NJSC32_TARST_SDTR: 2057 case NJSC32_TARST_ASYNC: 2058 njsc32_target_async(sc, target); 2059 target->t_state = NJSC32_TARST_DONE; 2060 njsc32_update_xfer_mode(sc, target); 2061 break; 2062 default: 2063 break; 2064 } 2065 goto restart; 2066 2067 case MSG_NOOP: /* 0x08 */ 2068 #ifdef NJSC32_DUALEDGE 2069 target = cmd->c_target; 2070 if (target->t_state == NJSC32_TARST_DE) { 2071 printf("%s: DualEdge transfer\n", 2072 device_xname(sc->sc_dev)); 2073 target->t_xferctl = NJSC32_XFR_DUALEDGE_ENABLE; 2074 /* go to next negotiation */ 2075 target->t_state = NJSC32_TARST_SDTR; 2076 njsc32_negotiate_xfer(sc, target); 2077 goto reply; 2078 } 2079 #endif 2080 goto restart; 2081 2082 case MSG_INITIATOR_DET_ERR: /* 0x05 I->T only */ 2083 case MSG_ABORT: /* 0x06 I->T only */ 2084 case MSG_PARITY_ERROR: /* 0x09 I->T only */ 2085 case MSG_LINK_CMD_COMPLETE: /* 0x0a */ 2086 case MSG_LINK_CMD_COMPLETEF: /* 0x0b */ 2087 case MSG_BUS_DEV_RESET: /* 0x0c I->T only */ 2088 case MSG_ABORT_TAG: /* 0x0d I->T only */ 2089 case MSG_CLEAR_QUEUE: /* 0x0e I->T only */ 2090 2091 #if 0 /* handled above */ 2092 case MSG_SIMPLE_Q_TAG: /* 0x20 */ 2093 #endif 2094 case MSG_HEAD_OF_Q_TAG: /* 0x21 I->T only */ 2095 case MSG_ORDERED_Q_TAG: /* 0x22 I->T only */ 2096 case MSG_IGN_WIDE_RESIDUE: /* 0x23 */ 2097 2098 default: 2099 #ifdef NJSC32_DEBUG 2100 PRINTC(cmd, ("msgin: unsupported msg: %#x", msg0)); 2101 if (MSG_IS2BYTE(msg0)) 2102 printf(" %#x", msg); 2103 printf("\n"); 2104 #endif 2105 break; 2106 } 2107 2108 reject: 2109 njsc32_add_msgout(sc, MSG_MESSAGE_REJECT); 2110 2111 reply: 2112 msgout = njsc32_get_auto_msgout(sc); 2113 2114 restart: 2115 cctl = NJSC32_CMD_CLEAR_CDB_FIFO_PTR | 2116 NJSC32_CMD_AUTO_COMMAND_PHASE | 2117 NJSC32_CMD_AUTO_SCSI_RESTART; 2118 2119 /* 2120 * Be careful the second and latter bytes of Message In 2121 * shall not be absorbed by AutoSCSI. 2122 */ 2123 if (sc->sc_msgincnt == 0) 2124 cctl |= NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02; 2125 2126 if (sc->sc_msgoutlen != 0) 2127 cctl |= NJSC32_CMD_AUTO_ATN; 2128 2129 njsc32_write_4(sc, NJSC32_REG_SCSI_MSG_OUT, msgout); 2130 2131 /* (re)start AutoSCSI (may assert ATN) */ 2132 if (reload_params) { 2133 njsc32_cmd_reload(sc, cmd, cctl); 2134 } else { 2135 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl); 2136 } 2137 2138 /* +ATN -> -REQ: need 90ns delay? */ 2139 2140 njsc32_wait_req_negate(sc); /* wait for REQ negation */ 2141 2142 njsc32_negate_ack(sc); 2143 2144 return; 2145 } 2146 2147 static void 2148 njsc32_msgout(struct njsc32_softc *sc) 2149 { 2150 int cctl; 2151 u_int8_t bus; 2152 unsigned n; 2153 2154 if (sc->sc_msgoutlen == 0) { 2155 /* target entered to Message Out on unexpected timing */ 2156 njsc32_add_msgout(sc, MSG_NOOP); 2157 } 2158 2159 cctl = NJSC32_CMD_CLEAR_CDB_FIFO_PTR | 2160 NJSC32_CMD_AUTO_COMMAND_PHASE | NJSC32_CMD_AUTO_SCSI_RESTART | 2161 NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02; 2162 2163 /* make sure target is in Message Out phase */ 2164 bus = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR); 2165 if ((bus & NJSC32_BUSMON_PHASE_MASK) != NJSC32_PHASE_MESSAGE_OUT) { 2166 /* 2167 * Message Out is aborted by target. 2168 */ 2169 printf("%s: njsc32_msgout: phase change %#x\n", 2170 device_xname(sc->sc_dev), bus); 2171 2172 /* XXX what to do? */ 2173 2174 /* restart AutoSCSI (negate ATN) */ 2175 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl); 2176 2177 sc->sc_msgoutidx = 0; 2178 return; 2179 } 2180 2181 n = sc->sc_msgoutidx; 2182 if (n == sc->sc_msgoutlen - 1) { 2183 /* 2184 * negate ATN before sending ACK 2185 */ 2186 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, 0); 2187 2188 sc->sc_msgoutidx = 0; /* target may retry Message Out */ 2189 } else { 2190 cctl |= NJSC32_CMD_AUTO_ATN; 2191 sc->sc_msgoutidx++; 2192 } 2193 2194 /* Send Message Out */ 2195 njsc32_write_1(sc, NJSC32_REG_SCSI_OUT_LATCH, sc->sc_msgout[n]); 2196 2197 /* DBn -> +ACK: need 55ns delay? */ 2198 2199 njsc32_assert_ack(sc); 2200 njsc32_wait_req_negate(sc); /* wait for REQ negation */ 2201 2202 /* restart AutoSCSI */ 2203 njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl); 2204 2205 njsc32_negate_ack(sc); 2206 2207 /* 2208 * do not reset sc->sc_msgoutlen so the target 2209 * can retry Message Out phase 2210 */ 2211 } 2212 2213 static void 2214 njsc32_cmdtimeout(void *arg) 2215 { 2216 struct njsc32_cmd *cmd = arg; 2217 struct njsc32_softc *sc; 2218 int s; 2219 2220 PRINTC(cmd, ("command timeout\n")); 2221 2222 sc = cmd->c_sc; 2223 2224 s = splbio(); 2225 2226 if (sc->sc_stat == NJSC32_STAT_ARBIT) 2227 njsc32_arbitration_failed(sc); 2228 else { 2229 sc->sc_curcmd = NULL; 2230 sc->sc_stat = NJSC32_STAT_IDLE; 2231 njsc32_end_cmd(sc, cmd, XS_TIMEOUT); 2232 } 2233 2234 /* XXX? */ 2235 njsc32_init(sc, 1); /* bus reset */ 2236 2237 splx(s); 2238 } 2239 2240 static void 2241 njsc32_reseltimeout(void *arg) 2242 { 2243 struct njsc32_cmd *cmd = arg; 2244 struct njsc32_softc *sc; 2245 int s; 2246 2247 PRINTC(cmd, ("reselection timeout\n")); 2248 2249 sc = cmd->c_sc; 2250 2251 s = splbio(); 2252 2253 /* remove from disconnected list */ 2254 if (cmd->c_flags & NJSC32_CMD_TAGGED) { 2255 /* I_T_L_Q */ 2256 KASSERT(cmd->c_lu->lu_cmd == NULL); 2257 TAILQ_REMOVE(&cmd->c_lu->lu_q, cmd, c_q); 2258 } else { 2259 /* I_T_L */ 2260 KASSERT(cmd->c_lu->lu_cmd == cmd); 2261 cmd->c_lu->lu_cmd = NULL; 2262 } 2263 2264 njsc32_end_cmd(sc, cmd, XS_TIMEOUT); 2265 2266 /* XXX? */ 2267 njsc32_init(sc, 1); /* bus reset */ 2268 2269 splx(s); 2270 } 2271 2272 static inline void 2273 njsc32_end_auto(struct njsc32_softc *sc, struct njsc32_cmd *cmd, int auto_phase) 2274 { 2275 struct scsipi_xfer *xs; 2276 2277 if (auto_phase & NJSC32_XPHASE_MSGIN_02) { 2278 /* Message In: 0x02 Save Data Pointer */ 2279 2280 /* 2281 * Adjust saved data pointer 2282 * if the command is not completed yet. 2283 */ 2284 if ((auto_phase & NJSC32_XPHASE_MSGIN_00) == 0 && 2285 (auto_phase & 2286 (NJSC32_XPHASE_DATA_IN | NJSC32_XPHASE_DATA_OUT)) != 0) { 2287 njsc32_save_ptr(cmd); 2288 } 2289 TPRINTF(("BM %u, SGT %u, SACK %u, SAVED_ACK %u\n", 2290 njsc32_read_4(sc, NJSC32_REG_BM_CNT), 2291 njsc32_read_4(sc, NJSC32_REG_SGT_ADR), 2292 njsc32_read_4(sc, NJSC32_REG_SACK_CNT), 2293 njsc32_read_4(sc, NJSC32_REG_SAVED_ACK_CNT))); 2294 } 2295 2296 xs = cmd->c_xs; 2297 2298 if (auto_phase & NJSC32_XPHASE_MSGIN_00) { 2299 /* Command Complete */ 2300 TPRINTC(cmd, ("njsc32_intr: Command Complete\n")); 2301 switch (xs->status) { 2302 case SCSI_CHECK: case SCSI_QUEUE_FULL: case SCSI_BUSY: 2303 /* 2304 * scsipi layer will automatically handle the error 2305 */ 2306 njsc32_end_cmd(sc, cmd, XS_BUSY); 2307 break; 2308 default: 2309 xs->resid -= cmd->c_dp_max; 2310 njsc32_end_cmd(sc, cmd, XS_NOERROR); 2311 break; 2312 } 2313 } else if (auto_phase & NJSC32_XPHASE_MSGIN_04) { 2314 /* Disconnect */ 2315 TPRINTC(cmd, ("njsc32_intr: Disconnect\n")); 2316 2317 /* for ill-designed devices */ 2318 if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) != 0) 2319 njsc32_save_ptr(cmd); 2320 2321 /* 2322 * move current cmd to disconnected list 2323 */ 2324 if (cmd->c_flags & NJSC32_CMD_TAGGED) { 2325 /* I_T_L_Q */ 2326 if (cmd->c_flags & NJSC32_CMD_TAGGED_HEAD) 2327 TAILQ_INSERT_HEAD(&cmd->c_lu->lu_q, cmd, c_q); 2328 else 2329 TAILQ_INSERT_TAIL(&cmd->c_lu->lu_q, cmd, c_q); 2330 } else { 2331 /* I_T_L */ 2332 cmd->c_lu->lu_cmd = cmd; 2333 } 2334 2335 /* 2336 * schedule timeout -- avoid being 2337 * disconnected forever 2338 */ 2339 if ((xs->xs_control & XS_CTL_POLL) == 0) { 2340 callout_stop(&xs->xs_callout); 2341 callout_reset(&xs->xs_callout, mstohz(xs->timeout), 2342 njsc32_reseltimeout, cmd); 2343 } 2344 2345 } else { 2346 /* 2347 * target has come to Bus Free phase 2348 * probably to notify an error 2349 */ 2350 PRINTC(cmd, ("njsc32_intr: unexpected bus free\n")); 2351 /* try Request Sense */ 2352 xs->status = SCSI_CHECK; 2353 njsc32_end_cmd(sc, cmd, XS_BUSY); 2354 } 2355 } 2356 2357 int 2358 njsc32_intr(void *arg) 2359 { 2360 struct njsc32_softc *sc = arg; 2361 u_int16_t intr; 2362 u_int8_t arbstat, bus_phase; 2363 int auto_phase; 2364 int idbit; 2365 struct njsc32_cmd *cmd; 2366 2367 intr = njsc32_read_2(sc, NJSC32_REG_IRQ); 2368 if ((intr & NJSC32_IRQ_INTR_PENDING) == 0) 2369 return 0; /* not mine */ 2370 2371 TPRINTF(("%s: njsc32_intr: %#x\n", device_xname(sc->sc_dev), intr)); 2372 2373 #if 0 /* I don't think this is required */ 2374 /* mask interrupts */ 2375 njsc32_write_2(sc, NJSC32_REG_IRQ, NJSC32_IRQ_MASK_ALL); 2376 #endif 2377 2378 /* we got an interrupt, so stop the timer */ 2379 njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP); 2380 2381 if (intr & NJSC32_IRQ_SCSIRESET) { 2382 printf("%s: detected bus reset\n", device_xname(sc->sc_dev)); 2383 /* make sure all devices on the bus are certainly reset */ 2384 njsc32_reset_bus(sc); 2385 goto out; 2386 } 2387 2388 if (sc->sc_stat == NJSC32_STAT_ARBIT) { 2389 cmd = sc->sc_curcmd; 2390 KASSERT(cmd); 2391 arbstat = njsc32_read_1(sc, NJSC32_REG_ARBITRATION_STAT); 2392 if (arbstat & (NJSC32_ARBSTAT_WIN | NJSC32_ARBSTAT_FAIL)) { 2393 /* 2394 * arbitration done 2395 */ 2396 /* clear arbitration status */ 2397 njsc32_write_1(sc, NJSC32_REG_SET_ARBITRATION, 2398 NJSC32_SETARB_CLEAR); 2399 2400 if (arbstat & NJSC32_ARBSTAT_WIN) { 2401 TPRINTC(cmd, 2402 ("njsc32_intr: arbitration won\n")); 2403 2404 TAILQ_REMOVE(&sc->sc_reqcmd, cmd, c_q); 2405 2406 sc->sc_stat = NJSC32_STAT_CONNECT; 2407 } else { 2408 TPRINTC(cmd, 2409 ("njsc32_intr: arbitration failed\n")); 2410 2411 njsc32_arbitration_failed(sc); 2412 2413 /* XXX delay */ 2414 /* XXX retry counter */ 2415 } 2416 } 2417 } 2418 2419 if (intr & NJSC32_IRQ_TIMER) { 2420 TPRINTF(("%s: njsc32_intr: timer interrupt\n", 2421 device_xname(sc->sc_dev))); 2422 } 2423 2424 if (intr & NJSC32_IRQ_RESELECT) { 2425 /* Reselection from a target */ 2426 njsc32_arbitration_failed(sc); /* just in case */ 2427 if ((cmd = sc->sc_curcmd) != NULL) { 2428 /* ? */ 2429 printf("%s: unexpected reselection\n", 2430 device_xname(sc->sc_dev)); 2431 sc->sc_curcmd = NULL; 2432 sc->sc_stat = NJSC32_STAT_IDLE; 2433 njsc32_end_cmd(sc, cmd, XS_DRIVER_STUFFUP); 2434 } 2435 2436 idbit = njsc32_read_1(sc, NJSC32_REG_RESELECT_ID); 2437 if ((idbit & (1 << NJSC32_INITIATOR_ID)) == 0 || 2438 (sc->sc_reselid = 2439 ffs(idbit & ~(1 << NJSC32_INITIATOR_ID)) - 1) < 0) { 2440 printf("%s: invalid reselection (id: %#x)\n", 2441 device_xname(sc->sc_dev), idbit); 2442 sc->sc_stat = NJSC32_STAT_IDLE; /* XXX ? */ 2443 } else { 2444 sc->sc_stat = NJSC32_STAT_RESEL; 2445 TPRINTF(("%s: njsc32_intr: reselection from %d\n", 2446 device_xname(sc->sc_dev), sc->sc_reselid)); 2447 } 2448 } 2449 2450 if (intr & NJSC32_IRQ_PHASE_CHANGE) { 2451 #if 1 /* XXX probably not needed */ 2452 if (sc->sc_stat == NJSC32_STAT_ARBIT) 2453 PRINTC(sc->sc_curcmd, 2454 ("njsc32_intr: cancel arbitration phase\n")); 2455 njsc32_arbitration_failed(sc); 2456 #endif 2457 /* current bus phase */ 2458 bus_phase = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) & 2459 NJSC32_BUSMON_PHASE_MASK; 2460 2461 switch (bus_phase) { 2462 case NJSC32_PHASE_MESSAGE_IN: 2463 njsc32_msgin(sc); 2464 break; 2465 2466 /* 2467 * target may suddenly become Status / Bus Free phase 2468 * to notify an error condition 2469 */ 2470 case NJSC32_PHASE_STATUS: 2471 printf("%s: unexpected bus phase: Status\n", 2472 device_xname(sc->sc_dev)); 2473 if ((cmd = sc->sc_curcmd) != NULL) { 2474 cmd->c_xs->status = 2475 njsc32_read_1(sc, NJSC32_REG_SCSI_CSB_IN); 2476 TPRINTC(cmd, ("njsc32_intr: Status %d\n", 2477 cmd->c_xs->status)); 2478 } 2479 break; 2480 case NJSC32_PHASE_BUSFREE: 2481 printf("%s: unexpected bus phase: Bus Free\n", 2482 device_xname(sc->sc_dev)); 2483 if ((cmd = sc->sc_curcmd) != NULL) { 2484 sc->sc_curcmd = NULL; 2485 sc->sc_stat = NJSC32_STAT_IDLE; 2486 if (cmd->c_xs->status != SCSI_QUEUE_FULL && 2487 cmd->c_xs->status != SCSI_BUSY) 2488 cmd->c_xs->status = SCSI_CHECK;/* XXX */ 2489 njsc32_end_cmd(sc, cmd, XS_BUSY); 2490 } 2491 goto out; 2492 default: 2493 #ifdef NJSC32_DEBUG 2494 printf("%s: unexpected bus phase: ", 2495 device_xname(sc->sc_dev)); 2496 switch (bus_phase) { 2497 case NJSC32_PHASE_COMMAND: 2498 printf("Command\n"); 2499 break; 2500 case NJSC32_PHASE_MESSAGE_OUT: 2501 printf("Message Out\n"); 2502 break; 2503 case NJSC32_PHASE_DATA_IN: 2504 printf("Data In\n"); 2505 break; 2506 case NJSC32_PHASE_DATA_OUT: 2507 printf("Data Out\n"); 2508 break; 2509 case NJSC32_PHASE_RESELECT: 2510 printf("Reselect\n"); 2511 break; 2512 default: 2513 printf("%#x\n", bus_phase); 2514 break; 2515 } 2516 #else 2517 printf("%s: unexpected bus phase: %#x", 2518 device_xname(sc->sc_dev), bus_phase); 2519 #endif 2520 break; 2521 } 2522 } 2523 2524 if (intr & NJSC32_IRQ_AUTOSCSI) { 2525 /* 2526 * AutoSCSI interrupt 2527 */ 2528 auto_phase = njsc32_read_2(sc, NJSC32_REG_EXECUTE_PHASE); 2529 TPRINTF(("%s: njsc32_intr: AutoSCSI: %#x\n", 2530 device_xname(sc->sc_dev), auto_phase)); 2531 njsc32_write_2(sc, NJSC32_REG_EXECUTE_PHASE, 0); 2532 2533 if (auto_phase & NJSC32_XPHASE_SEL_TIMEOUT) { 2534 cmd = sc->sc_curcmd; 2535 if (cmd == NULL) { 2536 printf("%s: sel no cmd\n", 2537 device_xname(sc->sc_dev)); 2538 goto out; 2539 } 2540 DPRINTC(cmd, ("njsc32_intr: selection timeout\n")); 2541 2542 sc->sc_curcmd = NULL; 2543 sc->sc_stat = NJSC32_STAT_IDLE; 2544 njsc32_end_cmd(sc, cmd, XS_SELTIMEOUT); 2545 2546 goto out; 2547 } 2548 2549 #ifdef NJSC32_TRACE 2550 if (auto_phase & NJSC32_XPHASE_COMMAND) { 2551 /* Command phase has been automatically processed */ 2552 TPRINTF(("%s: njsc32_intr: Command\n", 2553 device_xname(sc->sc_dev))); 2554 } 2555 #endif 2556 #ifdef NJSC32_DEBUG 2557 if (auto_phase & NJSC32_XPHASE_ILLEGAL) { 2558 printf("%s: njsc32_intr: Illegal phase\n", 2559 device_xname(sc->sc_dev)); 2560 } 2561 #endif 2562 2563 if (auto_phase & NJSC32_XPHASE_PAUSED_MSG_IN) { 2564 TPRINTF(("%s: njsc32_intr: Process Message In\n", 2565 device_xname(sc->sc_dev))); 2566 njsc32_msgin(sc); 2567 } 2568 2569 if (auto_phase & NJSC32_XPHASE_PAUSED_MSG_OUT) { 2570 TPRINTF(("%s: njsc32_intr: Process Message Out\n", 2571 device_xname(sc->sc_dev))); 2572 njsc32_msgout(sc); 2573 } 2574 2575 cmd = sc->sc_curcmd; 2576 if (cmd == NULL) { 2577 TPRINTF(("%s: njsc32_intr: no cmd\n", 2578 device_xname(sc->sc_dev))); 2579 goto out; 2580 } 2581 2582 if (auto_phase & 2583 (NJSC32_XPHASE_DATA_IN | NJSC32_XPHASE_DATA_OUT)) { 2584 u_int32_t sackcnt, cntoffset; 2585 2586 #ifdef NJSC32_TRACE 2587 if (auto_phase & NJSC32_XPHASE_DATA_IN) 2588 PRINTC(cmd, ("njsc32_intr: data in done\n")); 2589 if (auto_phase & NJSC32_XPHASE_DATA_OUT) 2590 PRINTC(cmd, ("njsc32_intr: data out done\n")); 2591 printf("BM %u, SGT %u, SACK %u, SAVED_ACK %u\n", 2592 njsc32_read_4(sc, NJSC32_REG_BM_CNT), 2593 njsc32_read_4(sc, NJSC32_REG_SGT_ADR), 2594 njsc32_read_4(sc, NJSC32_REG_SACK_CNT), 2595 njsc32_read_4(sc, NJSC32_REG_SAVED_ACK_CNT)); 2596 #endif 2597 2598 /* 2599 * detected parity error on data transfer? 2600 */ 2601 if (njsc32_read_1(sc, NJSC32_REG_PARITY_STATUS) & 2602 (NJSC32_PARITYSTATUS_ERROR_LSB| 2603 NJSC32_PARITYSTATUS_ERROR_MSB)) { 2604 2605 PRINTC(cmd, ("datain: parity error\n")); 2606 2607 /* clear parity error */ 2608 njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL, 2609 NJSC32_PARITYCTL_CHECK_ENABLE | 2610 NJSC32_PARITYCTL_CLEAR_ERROR); 2611 2612 if (auto_phase & NJSC32_XPHASE_BUS_FREE) { 2613 /* 2614 * XXX command has already finished 2615 * -- what can we do? 2616 * 2617 * It is not clear current command 2618 * caused the error -- reset everything. 2619 */ 2620 njsc32_init(sc, 1); /* XXX */ 2621 } else { 2622 /* XXX does this case occur? */ 2623 #if 1 2624 printf("%s: datain: parity error\n", 2625 device_xname(sc->sc_dev)); 2626 #endif 2627 /* 2628 * Make attention condition and try 2629 * to send Initiator Detected Error 2630 * message. 2631 */ 2632 njsc32_init_msgout(sc); 2633 njsc32_add_msgout(sc, 2634 MSG_INITIATOR_DET_ERR); 2635 njsc32_write_4(sc, 2636 NJSC32_REG_SCSI_MSG_OUT, 2637 njsc32_get_auto_msgout(sc)); 2638 /* restart autoscsi with ATN */ 2639 njsc32_write_2(sc, 2640 NJSC32_REG_COMMAND_CONTROL, 2641 NJSC32_CMD_CLEAR_CDB_FIFO_PTR | 2642 NJSC32_CMD_AUTO_COMMAND_PHASE | 2643 NJSC32_CMD_AUTO_SCSI_RESTART | 2644 NJSC32_CMD_AUTO_MSGIN_00_04 | 2645 NJSC32_CMD_AUTO_MSGIN_02 | 2646 NJSC32_CMD_AUTO_ATN); 2647 } 2648 goto out; 2649 } 2650 2651 /* 2652 * data has been transferred, and current pointer 2653 * is changed 2654 */ 2655 sackcnt = njsc32_read_4(sc, NJSC32_REG_SACK_CNT); 2656 2657 /* 2658 * The controller returns extra ACK count 2659 * if the DMA buffer is not 4byte aligned. 2660 */ 2661 cntoffset = le32toh(cmd->c_sgt[0].sg_addr) & 3; 2662 #ifdef NJSC32_DEBUG 2663 if (cntoffset != 0) { 2664 printf("sackcnt %u, cntoffset %u\n", 2665 sackcnt, cntoffset); 2666 } 2667 #endif 2668 /* advance SCSI pointer */ 2669 njsc32_set_cur_ptr(cmd, 2670 cmd->c_dp_cur + sackcnt - cntoffset); 2671 } 2672 2673 if (auto_phase & NJSC32_XPHASE_MSGOUT) { 2674 /* Message Out phase has been automatically processed */ 2675 TPRINTC(cmd, ("njsc32_intr: Message Out\n")); 2676 if ((auto_phase & NJSC32_XPHASE_PAUSED_MSG_IN) == 0 && 2677 sc->sc_msgoutlen <= NJSC32_MSGOUT_MAX_AUTO) { 2678 njsc32_init_msgout(sc); 2679 } 2680 } 2681 2682 if (auto_phase & NJSC32_XPHASE_STATUS) { 2683 /* Status phase has been automatically processed */ 2684 cmd->c_xs->status = 2685 njsc32_read_1(sc, NJSC32_REG_SCSI_CSB_IN); 2686 TPRINTC(cmd, ("njsc32_intr: Status %#x\n", 2687 cmd->c_xs->status)); 2688 } 2689 2690 if (auto_phase & NJSC32_XPHASE_BUS_FREE) { 2691 /* AutoSCSI is finished */ 2692 2693 TPRINTC(cmd, ("njsc32_intr: Bus Free\n")); 2694 2695 sc->sc_stat = NJSC32_STAT_IDLE; 2696 sc->sc_curcmd = NULL; 2697 2698 njsc32_end_auto(sc, cmd, auto_phase); 2699 } 2700 goto out; 2701 } 2702 2703 if (intr & NJSC32_IRQ_FIFO_THRESHOLD) { 2704 /* XXX We use DMA, and this shouldn't happen */ 2705 printf("%s: njsc32_intr: FIFO\n", device_xname(sc->sc_dev)); 2706 njsc32_init(sc, 1); 2707 goto out; 2708 } 2709 if (intr & NJSC32_IRQ_PCI) { 2710 /* XXX? */ 2711 printf("%s: njsc32_intr: PCI\n", device_xname(sc->sc_dev)); 2712 } 2713 if (intr & NJSC32_IRQ_BMCNTERR) { 2714 /* XXX? */ 2715 printf("%s: njsc32_intr: BM\n", device_xname(sc->sc_dev)); 2716 } 2717 2718 out: 2719 /* go next command if controller is idle */ 2720 if (sc->sc_stat == NJSC32_STAT_IDLE) 2721 njsc32_start(sc); 2722 2723 #if 0 2724 /* enable interrupts */ 2725 njsc32_write_2(sc, NJSC32_REG_IRQ, 0); 2726 #endif 2727 2728 return 1; /* processed */ 2729 } 2730