1 /* $NetBSD: atapi_wdc.c,v 1.134 2019/11/10 21:16:37 chs Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2001 Manuel Bouyer. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.134 2019/11/10 21:16:37 chs Exp $"); 29 30 #ifndef ATADEBUG 31 #define ATADEBUG 32 #endif /* ATADEBUG */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/file.h> 38 #include <sys/stat.h> 39 #include <sys/buf.h> 40 #include <sys/malloc.h> 41 #include <sys/device.h> 42 #include <sys/syslog.h> 43 #include <sys/proc.h> 44 #include <sys/dvdio.h> 45 46 #include <sys/intr.h> 47 #include <sys/bus.h> 48 49 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 50 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 51 #define bus_space_write_multi_stream_4 bus_space_write_multi_4 52 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 53 #define bus_space_read_multi_stream_4 bus_space_read_multi_4 54 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 55 56 #include <dev/ata/ataconf.h> 57 #include <dev/ata/atareg.h> 58 #include <dev/ata/atavar.h> 59 #include <dev/ic/wdcreg.h> 60 #include <dev/ic/wdcvar.h> 61 62 #include <dev/scsipi/scsi_all.h> /* for SCSI status */ 63 64 #define DEBUG_INTR 0x01 65 #define DEBUG_XFERS 0x02 66 #define DEBUG_STATUS 0x04 67 #define DEBUG_FUNCS 0x08 68 #define DEBUG_PROBE 0x10 69 #ifdef ATADEBUG 70 #ifndef ATADEBUG_ATAPI_MASK 71 #define ATADEBUG_ATAPI_MASK 0x0 72 #endif 73 int wdcdebug_atapi_mask = ATADEBUG_ATAPI_MASK; 74 #define ATADEBUG_PRINT(args, level) \ 75 if (wdcdebug_atapi_mask & (level)) \ 76 printf args 77 #else 78 #define ATADEBUG_PRINT(args, level) 79 #endif 80 81 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */ 82 #define ATAPI_MODE_DELAY 1000 /* 1s, timeout for SET_FEATYRE cmds */ 83 84 static int wdc_atapi_get_params(struct scsipi_channel *, int, 85 struct ataparams *); 86 static void wdc_atapi_probe_device(struct atapibus_softc *, int); 87 static void wdc_atapi_minphys (struct buf *bp); 88 static int wdc_atapi_start(struct ata_channel *,struct ata_xfer *); 89 static int wdc_atapi_intr(struct ata_channel *, struct ata_xfer *, int); 90 static void wdc_atapi_kill_xfer(struct ata_channel *, 91 struct ata_xfer *, int); 92 static void wdc_atapi_phase_complete(struct ata_xfer *, int); 93 static void wdc_atapi_poll(struct ata_channel *, struct ata_xfer *); 94 static void wdc_atapi_done(struct ata_channel *, struct ata_xfer *); 95 static void wdc_atapi_reset(struct ata_channel *, struct ata_xfer *); 96 static void wdc_atapi_scsipi_request(struct scsipi_channel *, 97 scsipi_adapter_req_t, void *); 98 static void wdc_atapi_kill_pending(struct scsipi_periph *); 99 static void wdc_atapi_polldsc(void *arg); 100 101 #define MAX_SIZE MAXPHYS 102 103 static const struct scsipi_bustype wdc_atapi_bustype = { 104 SCSIPI_BUSTYPE_ATAPI, 105 atapi_scsipi_cmd, 106 atapi_interpret_sense, 107 atapi_print_addr, 108 wdc_atapi_kill_pending, 109 NULL, 110 }; 111 112 void 113 wdc_atapibus_attach(struct atabus_softc *ata_sc) 114 { 115 struct ata_channel *chp = ata_sc->sc_chan; 116 struct atac_softc *atac = chp->ch_atac; 117 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 118 struct scsipi_channel *chan = &chp->ch_atapi_channel; 119 120 /* 121 * Fill in the scsipi_adapter. 122 */ 123 adapt->adapt_dev = atac->atac_dev; 124 adapt->adapt_nchannels = atac->atac_nchannels; 125 adapt->adapt_request = wdc_atapi_scsipi_request; 126 adapt->adapt_minphys = wdc_atapi_minphys; 127 if (atac->atac_cap & ATAC_CAP_NOIRQ) 128 adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY; 129 atac->atac_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device; 130 131 /* 132 * Fill in the scsipi_channel. 133 */ 134 memset(chan, 0, sizeof(*chan)); 135 chan->chan_adapter = adapt; 136 chan->chan_bustype = &wdc_atapi_bustype; 137 chan->chan_channel = chp->ch_channel; 138 chan->chan_flags = SCSIPI_CHAN_OPENINGS; 139 chan->chan_openings = 1; 140 chan->chan_max_periph = 1; 141 chan->chan_ntargets = 2; 142 chan->chan_nluns = 1; 143 144 chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan, 145 atapiprint); 146 } 147 148 static void 149 wdc_atapi_minphys(struct buf *bp) 150 { 151 152 if (bp->b_bcount > MAX_SIZE) 153 bp->b_bcount = MAX_SIZE; 154 minphys(bp); 155 } 156 157 /* 158 * Kill off all pending xfers for a periph. 159 * 160 * Must be called with adapter lock held 161 */ 162 static void 163 wdc_atapi_kill_pending(struct scsipi_periph *periph) 164 { 165 struct atac_softc *atac = 166 device_private(periph->periph_channel->chan_adapter->adapt_dev); 167 struct ata_channel *chp = 168 atac->atac_channels[periph->periph_channel->chan_channel]; 169 170 ata_kill_pending(&chp->ch_drive[periph->periph_target]); 171 } 172 173 static void 174 wdc_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason) 175 { 176 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 177 bool deactivate = true; 178 179 /* remove this command from xfer queue */ 180 switch (reason) { 181 case KILL_GONE_INACTIVE: 182 deactivate = false; 183 /* FALLTHROUGH */ 184 case KILL_GONE: 185 sc_xfer->error = XS_DRIVER_STUFFUP; 186 break; 187 case KILL_RESET: 188 sc_xfer->error = XS_RESET; 189 break; 190 default: 191 printf("wdc_ata_bio_kill_xfer: unknown reason %d\n", 192 reason); 193 panic("wdc_ata_bio_kill_xfer"); 194 } 195 196 if (deactivate) 197 ata_deactivate_xfer(chp, xfer); 198 199 ata_free_xfer(chp, xfer); 200 scsipi_done(sc_xfer); 201 } 202 203 static int 204 wdc_atapi_get_params(struct scsipi_channel *chan, int drive, 205 struct ataparams *id) 206 { 207 struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev); 208 struct atac_softc *atac = &wdc->sc_atac; 209 struct wdc_regs *wdr = &wdc->regs[chan->chan_channel]; 210 struct ata_channel *chp = atac->atac_channels[chan->chan_channel]; 211 struct ata_xfer *xfer; 212 int rv; 213 214 xfer = ata_get_xfer(chp, false); 215 if (xfer == NULL) { 216 printf("wdc_atapi_get_params: no xfer\n"); 217 return EBUSY; 218 } 219 220 xfer->c_ata_c.r_command = ATAPI_SOFT_RESET; 221 xfer->c_ata_c.r_st_bmask = 0; 222 xfer->c_ata_c.r_st_pmask = 0; 223 xfer->c_ata_c.flags = AT_WAIT | AT_POLL; 224 xfer->c_ata_c.timeout = WDC_RESET_WAIT; 225 if (wdc_exec_command(&chp->ch_drive[drive], xfer) != ATACMD_COMPLETE) { 226 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for" 227 " drive %s:%d:%d: driver failed\n", 228 device_xname(atac->atac_dev), chp->ch_channel, drive); 229 panic("wdc_atapi_get_params"); 230 } 231 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 232 ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET " 233 "failed for drive %s:%d:%d: error 0x%x\n", 234 device_xname(atac->atac_dev), chp->ch_channel, drive, 235 xfer->c_ata_c.r_error), DEBUG_PROBE); 236 rv = -1; 237 goto out_xfer; 238 } 239 chp->ch_drive[drive].state = 0; 240 241 ata_free_xfer(chp, xfer); 242 243 (void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0); 244 245 /* Some ATAPI devices need a bit more time after software reset. */ 246 delay(5000); 247 if (ata_get_params(&chp->ch_drive[drive], AT_WAIT, id) != 0) { 248 ATADEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE " 249 "failed for drive %s:%d:%d\n", 250 device_xname(atac->atac_dev), chp->ch_channel, drive), 251 DEBUG_PROBE); 252 rv = -1; 253 goto out; 254 } 255 rv = 0; 256 out: 257 return rv; 258 259 out_xfer: 260 ata_free_xfer(chp, xfer); 261 return rv; 262 } 263 264 static void 265 wdc_atapi_probe_device(struct atapibus_softc *sc, int target) 266 { 267 struct scsipi_channel *chan = sc->sc_channel; 268 struct scsipi_periph *periph; 269 struct ataparams ids; 270 struct ataparams *id = &ids; 271 struct wdc_softc *wdc = device_private(chan->chan_adapter->adapt_dev); 272 struct atac_softc *atac = &wdc->sc_atac; 273 struct ata_channel *chp = atac->atac_channels[chan->chan_channel]; 274 struct ata_drive_datas *drvp = &chp->ch_drive[target]; 275 struct scsipibus_attach_args sa; 276 char serial_number[21], model[41], firmware_revision[9]; 277 int s; 278 279 /* skip if already attached */ 280 if (scsipi_lookup_periph(chan, target, 0) != NULL) 281 return; 282 283 /* if no ATAPI device detected at wdc attach time, skip */ 284 if (drvp->drive_type != ATA_DRIVET_ATAPI) { 285 ATADEBUG_PRINT(("wdc_atapi_probe_device: " 286 "drive %d not present\n", target), DEBUG_PROBE); 287 return; 288 } 289 290 if (wdc_atapi_get_params(chan, target, id) == 0) { 291 #ifdef ATAPI_DEBUG_PROBE 292 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 293 device_xname(sc->sc_dev), target, 294 id->atap_config & ATAPI_CFG_CMD_MASK, 295 id->atap_config & ATAPI_CFG_DRQ_MASK); 296 #endif 297 periph = scsipi_alloc_periph(M_WAITOK); 298 periph->periph_dev = NULL; 299 periph->periph_channel = chan; 300 periph->periph_switch = &atapi_probe_periphsw; 301 periph->periph_target = target; 302 periph->periph_lun = 0; 303 periph->periph_quirks = PQUIRK_ONLYBIG; 304 305 #ifdef SCSIPI_DEBUG 306 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI && 307 SCSIPI_DEBUG_TARGET == target) 308 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS; 309 #endif 310 periph->periph_type = ATAPI_CFG_TYPE(id->atap_config); 311 if (id->atap_config & ATAPI_CFG_REMOV) 312 periph->periph_flags |= PERIPH_REMOVABLE; 313 if (periph->periph_type == T_SEQUENTIAL) { 314 s = splbio(); 315 drvp->drive_flags |= ATA_DRIVE_ATAPIDSCW; 316 splx(s); 317 } 318 319 sa.sa_periph = periph; 320 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 321 sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ? 322 T_REMOV : T_FIXED; 323 strnvisx(model, sizeof(model), id->atap_model, 324 sizeof(id->atap_model), VIS_TRIM|VIS_SAFE|VIS_OCTAL); 325 strnvisx(serial_number, sizeof(serial_number), 326 id->atap_serial, sizeof(id->atap_serial), 327 VIS_TRIM|VIS_SAFE|VIS_OCTAL); 328 strnvisx(firmware_revision, sizeof(firmware_revision), 329 id->atap_revision, sizeof(id->atap_revision), 330 VIS_TRIM|VIS_SAFE|VIS_OCTAL); 331 sa.sa_inqbuf.vendor = model; 332 sa.sa_inqbuf.product = serial_number; 333 sa.sa_inqbuf.revision = firmware_revision; 334 335 /* 336 * Determine the operating mode capabilities of the device. 337 */ 338 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 339 periph->periph_cap |= PERIPH_CAP_CMD16; 340 /* XXX This is gross. */ 341 periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK); 342 343 drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa); 344 345 if (drvp->drv_softc) 346 ata_probe_caps(drvp); 347 else { 348 s = splbio(); 349 drvp->drive_type = ATA_DRIVET_NONE; 350 splx(s); 351 } 352 } else { 353 s = splbio(); 354 drvp->drive_type = ATA_DRIVET_NONE; 355 splx(s); 356 } 357 } 358 359 static const struct ata_xfer_ops wdc_atapi_xfer_ops = { 360 .c_start = wdc_atapi_start, 361 .c_intr = wdc_atapi_intr, 362 .c_poll = wdc_atapi_poll, 363 .c_abort = wdc_atapi_reset, 364 .c_kill_xfer = wdc_atapi_kill_xfer, 365 }; 366 367 static void 368 wdc_atapi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 369 void *arg) 370 { 371 struct scsipi_adapter *adapt = chan->chan_adapter; 372 struct scsipi_periph *periph; 373 struct scsipi_xfer *sc_xfer; 374 struct wdc_softc *wdc = device_private(adapt->adapt_dev); 375 struct atac_softc *atac = &wdc->sc_atac; 376 struct ata_xfer *xfer; 377 int channel = chan->chan_channel; 378 int drive, s; 379 380 switch (req) { 381 case ADAPTER_REQ_RUN_XFER: 382 sc_xfer = arg; 383 periph = sc_xfer->xs_periph; 384 drive = periph->periph_target; 385 386 ATADEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n", 387 device_xname(atac->atac_dev), channel, drive), 388 DEBUG_XFERS); 389 if (!device_is_active(atac->atac_dev)) { 390 sc_xfer->error = XS_DRIVER_STUFFUP; 391 scsipi_done(sc_xfer); 392 return; 393 } 394 395 xfer = ata_get_xfer(atac->atac_channels[channel], false); 396 if (xfer == NULL) { 397 sc_xfer->error = XS_RESOURCE_SHORTAGE; 398 scsipi_done(sc_xfer); 399 return; 400 } 401 402 if (sc_xfer->xs_control & XS_CTL_POLL) 403 xfer->c_flags |= C_POLL; 404 #if NATA_DMA 405 if ((atac->atac_channels[channel]->ch_drive[drive].drive_flags & 406 (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) && sc_xfer->datalen > 0) 407 xfer->c_flags |= C_DMA; 408 #endif 409 #if NATA_DMA && NATA_PIOBM 410 else 411 #endif 412 #if NATA_PIOBM 413 if ((atac->atac_cap & ATAC_CAP_PIOBM) && 414 sc_xfer->datalen > 0) 415 xfer->c_flags |= C_PIOBM; 416 #endif 417 xfer->c_drive = drive; 418 xfer->c_flags |= C_ATAPI; 419 #if NATA_DMA 420 if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY || 421 sc_xfer->cmd->opcode == GPCMD_SEND_KEY || 422 sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) { 423 /* 424 * DVD authentication commands must always be done in 425 * PIO mode. 426 */ 427 xfer->c_flags &= ~C_DMA; 428 } 429 430 /* 431 * DMA normally can't deal with transfers which are not a 432 * multiple of its databus width. It's a bug to request odd 433 * length transfers for ATAPI. 434 * 435 * Some devices also can't cope with unaligned DMA xfers 436 * either. Also some devices seem to not handle DMA xfers of 437 * less than 4 bytes. 438 * 439 * By enforcing at least 4 byte aligned offset and length for 440 * DMA, we might use PIO where DMA could be allowed but better 441 * safe than sorry as recent problems proved. 442 * 443 * Offending structures that are thus done by PIO instead of 444 * DMA are normally small structures since all bulkdata is 445 * aligned. But as the request may come from userland, we have 446 * to protect against it anyway. 447 * 448 * XXX check for the 32 bit wide flag? 449 */ 450 451 if (((uintptr_t) sc_xfer->data) & 0x03) 452 xfer->c_flags &= ~C_DMA; 453 if ((sc_xfer->datalen < 4) || (sc_xfer->datalen & 0x03)) 454 xfer->c_flags &= ~C_DMA; 455 #endif /* NATA_DMA */ 456 457 xfer->c_databuf = sc_xfer->data; 458 xfer->c_bcount = sc_xfer->datalen; 459 xfer->ops = &wdc_atapi_xfer_ops; 460 xfer->c_scsipi = sc_xfer; 461 xfer->c_atapi.c_dscpoll = 0; 462 s = splbio(); 463 ata_exec_xfer(atac->atac_channels[channel], xfer); 464 #ifdef DIAGNOSTIC 465 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 && 466 (sc_xfer->xs_status & XS_STS_DONE) == 0) 467 panic("wdc_atapi_scsipi_request: polled command " 468 "not done"); 469 #endif 470 splx(s); 471 return; 472 473 default: 474 /* Not supported, nothing to do. */ 475 ; 476 } 477 } 478 479 static int 480 wdc_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer) 481 { 482 struct atac_softc *atac = chp->ch_atac; 483 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 484 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 485 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 486 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 487 int wait_flags = (sc_xfer->xs_control & XS_CTL_POLL) ? AT_POLL : 0; 488 int tfd; 489 const char *errstring; 490 491 ATADEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n", 492 device_xname(atac->atac_dev), chp->ch_channel, drvp->drive, 493 sc_xfer->xs_control), DEBUG_XFERS); 494 495 ata_channel_lock_owned(chp); 496 497 #if NATA_DMA 498 if ((xfer->c_flags & C_DMA) && (drvp->n_xfers <= NXFER)) 499 drvp->n_xfers++; 500 #endif 501 /* Do control operations specially. */ 502 if (__predict_false(drvp->state < READY)) { 503 /* If it's not a polled command, we need the kernel thread */ 504 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0 && 505 (chp->ch_flags & ATACH_TH_RUN) == 0) { 506 return ATASTART_TH; 507 } 508 /* 509 * disable interrupts, all commands here should be quick 510 * enough to be able to poll, and we don't go here that often 511 */ 512 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 513 WDCTL_4BIT | WDCTL_IDS); 514 if (wdc->select) 515 wdc->select(chp, xfer->c_drive); 516 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 517 WDSD_IBM | (xfer->c_drive << 4)); 518 /* Don't try to set mode if controller can't be adjusted */ 519 if (atac->atac_set_modes == NULL) 520 goto ready; 521 /* Also don't try if the drive didn't report its mode */ 522 if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0) 523 goto ready; 524 errstring = "unbusy"; 525 if (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd)) 526 goto timeout; 527 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 528 0x08 | drvp->PIO_mode, WDSF_SET_MODE); 529 errstring = "piomode"; 530 if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags, 531 &tfd)) 532 goto timeout; 533 if (ATACH_ST(tfd) & WDCS_ERR) { 534 if (ATACH_ST(tfd) == WDCE_ABRT) { 535 /* 536 * Some ATAPI drives reject PIO settings. 537 * Fall back to PIO mode 3 since that's the 538 * minimum for ATAPI. 539 */ 540 printf("%s:%d:%d: PIO mode %d rejected, " 541 "falling back to PIO mode 3\n", 542 device_xname(atac->atac_dev), 543 chp->ch_channel, xfer->c_drive, 544 drvp->PIO_mode); 545 if (drvp->PIO_mode > 3) 546 drvp->PIO_mode = 3; 547 } else 548 goto error; 549 } 550 #if NATA_DMA 551 #if NATA_UDMA 552 if (drvp->drive_flags & ATA_DRIVE_UDMA) { 553 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 554 0x40 | drvp->UDMA_mode, WDSF_SET_MODE); 555 } else 556 #endif 557 if (drvp->drive_flags & ATA_DRIVE_DMA) { 558 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 559 0x20 | drvp->DMA_mode, WDSF_SET_MODE); 560 } else { 561 goto ready; 562 } 563 errstring = "dmamode"; 564 if (wdc_wait_for_unbusy(chp, ATAPI_MODE_DELAY, wait_flags, 565 &tfd)) 566 goto timeout; 567 if (ATACH_ST(tfd) & WDCS_ERR) { 568 if (ATACH_ERR(tfd) == WDCE_ABRT) { 569 #if NATA_UDMA 570 if (drvp->drive_flags & ATA_DRIVE_UDMA) 571 goto error; 572 else 573 #endif 574 { 575 /* 576 * The drive rejected our DMA setting. 577 * Fall back to mode 1. 578 */ 579 printf("%s:%d:%d: DMA mode %d rejected, " 580 "falling back to DMA mode 0\n", 581 device_xname(atac->atac_dev), 582 chp->ch_channel, xfer->c_drive, 583 drvp->DMA_mode); 584 if (drvp->DMA_mode > 0) 585 drvp->DMA_mode = 0; 586 } 587 } else 588 goto error; 589 } 590 #endif /* NATA_DMA */ 591 ready: 592 drvp->state = READY; 593 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 594 WDCTL_4BIT); 595 delay(10); /* some drives need a little delay here */ 596 } 597 /* start timeout machinery */ 598 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) 599 callout_reset(&chp->c_timo_callout, mstohz(sc_xfer->timeout), 600 wdctimeout, chp); 601 602 if (wdc->select) 603 wdc->select(chp, xfer->c_drive); 604 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 605 WDSD_IBM | (xfer->c_drive << 4)); 606 switch (wdc_wait_for_unbusy(chp, ATAPI_DELAY, wait_flags, &tfd)) { 607 case WDCWAIT_OK: 608 break; 609 case WDCWAIT_TOUT: 610 printf("wdc_atapi_start: not ready, st = %02x\n", 611 ATACH_ST(tfd)); 612 sc_xfer->error = XS_TIMEOUT; 613 return ATASTART_ABORT; 614 case WDCWAIT_THR: 615 return ATASTART_TH; 616 } 617 618 /* 619 * Even with WDCS_ERR, the device should accept a command packet 620 * Limit length to what can be stuffed into the cylinder register 621 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536, 622 * but not all devices do that and it's not obvious from the 623 * ATAPI spec that that behaviour should be expected. If more 624 * data is necessary, multiple data transfer phases will be done. 625 */ 626 627 wdccommand(chp, xfer->c_drive, ATAPI_PKT_CMD, 628 xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff, 629 0, 0, 0, 630 #if NATA_DMA 631 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 632 #endif 633 0 634 ); 635 636 #if NATA_PIOBM 637 if (xfer->c_flags & C_PIOBM) { 638 int error; 639 int dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN) 640 ? WDC_DMA_READ : 0; 641 if (xfer->c_flags & C_POLL) { 642 /* XXX not supported yet --- fall back to PIO */ 643 xfer->c_flags &= ~C_PIOBM; 644 } else { 645 /* Init the DMA channel. */ 646 error = (*wdc->dma_init)(wdc->dma_arg, 647 chp->ch_channel, xfer->c_drive, 648 (char *)xfer->c_databuf, 649 xfer->c_bcount, 650 dma_flags | WDC_DMA_PIOBM_ATAPI); 651 if (error) { 652 if (error == EINVAL) { 653 /* 654 * We can't do DMA on this transfer 655 * for some reason. Fall back to 656 * PIO. 657 */ 658 xfer->c_flags &= ~C_PIOBM; 659 error = 0; 660 } else { 661 sc_xfer->error = XS_DRIVER_STUFFUP; 662 errstring = "piobm"; 663 goto error; 664 } 665 } 666 } 667 } 668 #endif 669 /* 670 * If there is no interrupt for CMD input, busy-wait for it (done in 671 * the interrupt routine. Poll routine will exit early in this case. 672 */ 673 if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) != 674 ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) 675 return ATASTART_POLL; 676 else { 677 chp->ch_flags |= ATACH_IRQ_WAIT; 678 return ATASTART_STARTED; 679 } 680 681 timeout: 682 printf("%s:%d:%d: %s timed out\n", 683 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 684 errstring); 685 sc_xfer->error = XS_TIMEOUT; 686 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT); 687 delay(10); /* some drives need a little delay here */ 688 return ATASTART_ABORT; 689 690 error: 691 printf("%s:%d:%d: %s ", 692 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 693 errstring); 694 printf("error (0x%x)\n", ATACH_ERR(tfd)); 695 sc_xfer->error = XS_SHORTSENSE; 696 sc_xfer->sense.atapi_sense = ATACH_ERR(tfd); 697 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT); 698 delay(10); /* some drives need a little delay here */ 699 return ATASTART_ABORT; 700 } 701 702 static void 703 wdc_atapi_poll(struct ata_channel *chp, struct ata_xfer *xfer) 704 { 705 /* 706 * If there is no interrupt for CMD input, busy-wait for it (done in 707 * the interrupt routine. If it is a polled command, call the interrupt 708 * routine until command is done. 709 */ 710 const bool poll = ((xfer->c_scsipi->xs_control & XS_CTL_POLL) != 0); 711 712 /* Wait for at last 400ns for status bit to be valid */ 713 DELAY(1); 714 wdc_atapi_intr(chp, xfer, 0); 715 716 if (!poll) 717 return; 718 719 #if NATA_DMA 720 if (chp->ch_flags & ATACH_DMA_WAIT) { 721 wdc_dmawait(chp, xfer, xfer->c_scsipi->timeout); 722 chp->ch_flags &= ~ATACH_DMA_WAIT; 723 } 724 #endif 725 while ((xfer->c_scsipi->xs_status & XS_STS_DONE) == 0) { 726 /* Wait for at last 400ns for status bit to be valid */ 727 DELAY(1); 728 wdc_atapi_intr(chp, xfer, 0); 729 } 730 } 731 732 static int 733 wdc_atapi_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq) 734 { 735 struct atac_softc *atac = chp->ch_atac; 736 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 737 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 738 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 739 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 740 int len, phase, i, retries=0; 741 int ire, tfd; 742 #if NATA_DMA 743 int error; 744 #endif 745 #if NATA_DMA || NATA_PIOBM 746 int dma_flags = 0; 747 #endif 748 void *cmd; 749 750 ATADEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n", 751 device_xname(atac->atac_dev), chp->ch_channel, drvp->drive), 752 DEBUG_INTR); 753 754 ata_channel_lock(chp); 755 756 /* Is it not a transfer, but a control operation? */ 757 if (drvp->state < READY) { 758 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n", 759 device_xname(atac->atac_dev), chp->ch_channel, 760 xfer->c_drive, drvp->state); 761 panic("wdc_atapi_intr: bad state"); 762 } 763 /* 764 * If we missed an interrupt in a PIO transfer, reset and restart. 765 * Don't try to continue transfer, we may have missed cycles. 766 */ 767 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) { 768 ata_channel_unlock(chp); 769 sc_xfer->error = XS_TIMEOUT; 770 wdc_atapi_reset(chp, xfer); 771 return 1; 772 } 773 774 #if NATA_PIOBM 775 /* Transfer-done interrupt for busmastering PIO operation */ 776 if ((xfer->c_flags & C_PIOBM) && (chp->ch_flags & ATACH_PIOBM_WAIT)) { 777 chp->ch_flags &= ~ATACH_PIOBM_WAIT; 778 779 /* restore transfer length */ 780 len = xfer->c_bcount; 781 if (xfer->c_atapi.c_lenoff < 0) 782 len += xfer->c_atapi.c_lenoff; 783 784 if (sc_xfer->xs_control & XS_CTL_DATA_IN) 785 goto end_piobm_datain; 786 else 787 goto end_piobm_dataout; 788 } 789 #endif 790 791 /* Ack interrupt done in wdc_wait_for_unbusy */ 792 if (wdc->select) 793 wdc->select(chp, xfer->c_drive); 794 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 795 WDSD_IBM | (xfer->c_drive << 4)); 796 if (wdc_wait_for_unbusy(chp, 797 (irq == 0) ? sc_xfer->timeout : 0, AT_POLL, &tfd) == WDCWAIT_TOUT) { 798 if (irq && (xfer->c_flags & C_TIMEOU) == 0) { 799 ata_channel_unlock(chp); 800 return 0; /* IRQ was not for us */ 801 } 802 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n", 803 device_xname(atac->atac_dev), chp->ch_channel, 804 xfer->c_drive, xfer->c_bcount, xfer->c_skip); 805 #if NATA_DMA 806 if (xfer->c_flags & C_DMA) { 807 ata_dmaerr(drvp, 808 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 809 } 810 #endif 811 sc_xfer->error = XS_TIMEOUT; 812 ata_channel_unlock(chp); 813 wdc_atapi_reset(chp, xfer); 814 return 1; 815 } 816 if (wdc->irqack) 817 wdc->irqack(chp); 818 819 #if NATA_DMA 820 /* 821 * If we missed an IRQ and were using DMA, flag it as a DMA error 822 * and reset device. 823 */ 824 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) { 825 ata_dmaerr(drvp, (xfer->c_flags & C_POLL) ? AT_POLL : 0); 826 sc_xfer->error = XS_RESET; 827 ata_channel_unlock(chp); 828 wdc_atapi_reset(chp, xfer); 829 return (1); 830 } 831 #endif 832 /* 833 * if the request sense command was aborted, report the short sense 834 * previously recorded, else continue normal processing 835 */ 836 837 #if NATA_DMA || NATA_PIOBM 838 if (xfer->c_flags & (C_DMA | C_PIOBM)) 839 dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN) 840 ? WDC_DMA_READ : 0; 841 #endif 842 again: 843 len = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0) + 844 256 * bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 0); 845 ire = bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_ireason], 0); 846 phase = (ire & (WDCI_CMD | WDCI_IN)) | (ATACH_ST(tfd) & WDCS_DRQ); 847 ATADEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x " 848 "ire 0x%x :", xfer->c_bcount, 849 len, ATACH_ST(tfd), ATACH_ERR(tfd), ire), DEBUG_INTR); 850 851 switch (phase) { 852 case PHASE_CMDOUT: 853 cmd = sc_xfer->cmd; 854 ATADEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR); 855 #if NATA_DMA 856 /* Init the DMA channel if necessary */ 857 if (xfer->c_flags & C_DMA) { 858 error = (*wdc->dma_init)(wdc->dma_arg, 859 chp->ch_channel, xfer->c_drive, 860 xfer->c_databuf, xfer->c_bcount, dma_flags); 861 if (error) { 862 if (error == EINVAL) { 863 /* 864 * We can't do DMA on this transfer 865 * for some reason. Fall back to 866 * PIO. 867 */ 868 xfer->c_flags &= ~C_DMA; 869 error = 0; 870 } else { 871 sc_xfer->error = XS_DRIVER_STUFFUP; 872 break; 873 } 874 } 875 } 876 #endif 877 878 /* send packet command */ 879 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */ 880 wdc->dataout_pio(chp, drvp->drive_flags, cmd, sc_xfer->cmdlen); 881 882 #if NATA_DMA 883 /* Start the DMA channel if necessary */ 884 if (xfer->c_flags & C_DMA) { 885 (*wdc->dma_start)(wdc->dma_arg, 886 chp->ch_channel, xfer->c_drive); 887 chp->ch_flags |= ATACH_DMA_WAIT; 888 } 889 #endif 890 891 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 892 chp->ch_flags |= ATACH_IRQ_WAIT; 893 } 894 895 ata_channel_unlock(chp); 896 return 1; 897 898 case PHASE_DATAOUT: 899 /* write data */ 900 ATADEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR); 901 #if NATA_DMA 902 if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 || 903 (xfer->c_flags & C_DMA) != 0) { 904 printf("wdc_atapi_intr: bad data phase DATAOUT\n"); 905 if (xfer->c_flags & C_DMA) { 906 ata_dmaerr(drvp, 907 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 908 } 909 sc_xfer->error = XS_TIMEOUT; 910 ata_channel_unlock(chp); 911 wdc_atapi_reset(chp, xfer); 912 return 1; 913 } 914 #endif 915 xfer->c_atapi.c_lenoff = len - xfer->c_bcount; 916 if (xfer->c_bcount < len) { 917 printf("wdc_atapi_intr: warning: write only " 918 "%d of %d requested bytes\n", xfer->c_bcount, len); 919 len = xfer->c_bcount; 920 } 921 922 #if NATA_PIOBM 923 if (xfer->c_flags & C_PIOBM) { 924 /* start the busmastering PIO */ 925 (*wdc->piobm_start)(wdc->dma_arg, 926 chp->ch_channel, xfer->c_drive, 927 xfer->c_skip, len, WDC_PIOBM_XFER_IRQ); 928 chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT | 929 ATACH_PIOBM_WAIT; 930 ata_channel_unlock(chp); 931 return 1; 932 } 933 #endif 934 wdc->dataout_pio(chp, drvp->drive_flags, 935 (char *)xfer->c_databuf + xfer->c_skip, len); 936 937 #if NATA_PIOBM 938 end_piobm_dataout: 939 #endif 940 for (i = xfer->c_atapi.c_lenoff; i > 0; i -= 2) 941 bus_space_write_2(wdr->cmd_iot, 942 wdr->cmd_iohs[wd_data], 0, 0); 943 944 xfer->c_skip += len; 945 xfer->c_bcount -= len; 946 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 947 chp->ch_flags |= ATACH_IRQ_WAIT; 948 } 949 ata_channel_unlock(chp); 950 return 1; 951 952 case PHASE_DATAIN: 953 /* Read data */ 954 ATADEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR); 955 #if NATA_DMA 956 if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 || 957 (xfer->c_flags & C_DMA) != 0) { 958 printf("wdc_atapi_intr: bad data phase DATAIN\n"); 959 if (xfer->c_flags & C_DMA) { 960 ata_dmaerr(drvp, 961 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 962 } 963 sc_xfer->error = XS_TIMEOUT; 964 ata_channel_unlock(chp); 965 wdc_atapi_reset(chp, xfer); 966 return 1; 967 } 968 #endif 969 xfer->c_atapi.c_lenoff = len - xfer->c_bcount; 970 if (xfer->c_bcount < len) { 971 printf("wdc_atapi_intr: warning: reading only " 972 "%d of %d bytes\n", xfer->c_bcount, len); 973 len = xfer->c_bcount; 974 } 975 976 #if NATA_PIOBM 977 if (xfer->c_flags & C_PIOBM) { 978 /* start the busmastering PIO */ 979 (*wdc->piobm_start)(wdc->dma_arg, 980 chp->ch_channel, xfer->c_drive, 981 xfer->c_skip, len, WDC_PIOBM_XFER_IRQ); 982 chp->ch_flags |= ATACH_DMA_WAIT | ATACH_IRQ_WAIT | 983 ATACH_PIOBM_WAIT; 984 ata_channel_unlock(chp); 985 return 1; 986 } 987 #endif 988 wdc->datain_pio(chp, drvp->drive_flags, 989 (char *)xfer->c_databuf + xfer->c_skip, len); 990 991 #if NATA_PIOBM 992 end_piobm_datain: 993 #endif 994 if (xfer->c_atapi.c_lenoff > 0) 995 wdcbit_bucket(chp, xfer->c_atapi.c_lenoff); 996 997 xfer->c_skip += len; 998 xfer->c_bcount -= len; 999 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 1000 chp->ch_flags |= ATACH_IRQ_WAIT; 1001 } 1002 ata_channel_unlock(chp); 1003 return 1; 1004 1005 case PHASE_ABORTED: 1006 case PHASE_COMPLETED: 1007 ATADEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR); 1008 #if NATA_DMA 1009 if (xfer->c_flags & C_DMA) { 1010 xfer->c_bcount -= sc_xfer->datalen; 1011 } 1012 #endif 1013 sc_xfer->resid = xfer->c_bcount; 1014 /* this will unlock channel lock too */ 1015 wdc_atapi_phase_complete(xfer, tfd); 1016 return(1); 1017 1018 default: 1019 if (++retries<500) { 1020 DELAY(100); 1021 tfd = ATACH_ERR_ST( 1022 bus_space_read_1(wdr->cmd_iot, 1023 wdr->cmd_iohs[wd_error], 0), 1024 bus_space_read_1(wdr->cmd_iot, 1025 wdr->cmd_iohs[wd_status], 0) 1026 ); 1027 goto again; 1028 } 1029 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase); 1030 if (ATACH_ST(tfd) & WDCS_ERR) { 1031 sc_xfer->error = XS_SHORTSENSE; 1032 sc_xfer->sense.atapi_sense = ATACH_ERR(tfd); 1033 } else { 1034 #if NATA_DMA 1035 if (xfer->c_flags & C_DMA) { 1036 ata_dmaerr(drvp, 1037 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 1038 } 1039 #endif 1040 sc_xfer->error = XS_RESET; 1041 ata_channel_unlock(chp); 1042 wdc_atapi_reset(chp, xfer); 1043 return (1); 1044 } 1045 } 1046 ATADEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x " 1047 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense), 1048 DEBUG_INTR); 1049 ata_channel_unlock(chp); 1050 wdc_atapi_done(chp, xfer); 1051 return (1); 1052 } 1053 1054 static void 1055 wdc_atapi_phase_complete(struct ata_xfer *xfer, int tfd) 1056 { 1057 struct ata_channel *chp = xfer->c_chp; 1058 struct atac_softc *atac = chp->ch_atac; 1059 #if NATA_DMA || NATA_PIOBM 1060 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1061 #endif 1062 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 1063 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 1064 1065 ata_channel_lock_owned(chp); 1066 1067 /* wait for DSC if needed */ 1068 if (drvp->drive_flags & ATA_DRIVE_ATAPIDSCW) { 1069 ATADEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) " 1070 "polldsc %d\n", device_xname(atac->atac_dev), 1071 chp->ch_channel, 1072 xfer->c_drive, xfer->c_atapi.c_dscpoll), DEBUG_XFERS); 1073 #if 1 1074 if (cold) 1075 panic("wdc_atapi_phase_complete: cold"); 1076 #endif 1077 if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10, 1078 AT_POLL, &tfd) == WDCWAIT_TOUT) { 1079 /* 10ms not enough, try again in 1 tick */ 1080 if (xfer->c_atapi.c_dscpoll++ > 1081 mstohz(sc_xfer->timeout)) { 1082 printf("%s:%d:%d: wait_for_dsc " 1083 "failed\n", 1084 device_xname(atac->atac_dev), 1085 chp->ch_channel, xfer->c_drive); 1086 ata_channel_unlock(chp); 1087 sc_xfer->error = XS_TIMEOUT; 1088 wdc_atapi_reset(chp, xfer); 1089 } else { 1090 callout_reset(&chp->c_timo_callout, 1, 1091 wdc_atapi_polldsc, chp); 1092 ata_channel_unlock(chp); 1093 } 1094 return; 1095 } 1096 } 1097 1098 /* 1099 * Some drive occasionally set WDCS_ERR with 1100 * "ATA illegal length indication" in the error 1101 * register. If we read some data the sense is valid 1102 * anyway, so don't report the error. 1103 */ 1104 if (ATACH_ST(tfd) & WDCS_ERR && 1105 ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 || 1106 sc_xfer->resid == sc_xfer->datalen)) { 1107 /* save the short sense */ 1108 sc_xfer->error = XS_SHORTSENSE; 1109 sc_xfer->sense.atapi_sense = ATACH_ERR(tfd); 1110 if ((sc_xfer->xs_periph->periph_quirks & 1111 PQUIRK_NOSENSE) == 0) { 1112 /* ask scsipi to send a REQUEST_SENSE */ 1113 sc_xfer->error = XS_BUSY; 1114 sc_xfer->status = SCSI_CHECK; 1115 } 1116 #if NATA_DMA || NATA_PIOBM 1117 else if (wdc->dma_status & 1118 (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) { 1119 #if NATA_DMA 1120 ata_dmaerr(drvp, 1121 (xfer->c_flags & C_POLL) ? AT_POLL : 0); 1122 #endif 1123 sc_xfer->error = XS_RESET; 1124 ata_channel_unlock(chp); 1125 wdc_atapi_reset(chp, xfer); 1126 return; 1127 } 1128 #endif 1129 } 1130 if (xfer->c_bcount != 0) { 1131 ATADEBUG_PRINT(("wdc_atapi_intr: bcount value is " 1132 "%d after io\n", xfer->c_bcount), DEBUG_XFERS); 1133 } 1134 #ifdef DIAGNOSTIC 1135 if (xfer->c_bcount < 0) { 1136 printf("wdc_atapi_intr warning: bcount value " 1137 "is %d after io\n", xfer->c_bcount); 1138 } 1139 #endif 1140 ATADEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), " 1141 "error 0x%x sense 0x%x\n", sc_xfer->error, 1142 sc_xfer->sense.atapi_sense), DEBUG_INTR); 1143 ata_channel_unlock(chp); 1144 wdc_atapi_done(chp, xfer); 1145 } 1146 1147 static void 1148 wdc_atapi_done(struct ata_channel *chp, struct ata_xfer *xfer) 1149 { 1150 struct atac_softc *atac = chp->ch_atac; 1151 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 1152 1153 ATADEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n", 1154 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 1155 (u_int)xfer->c_flags), DEBUG_XFERS); 1156 1157 if (ata_waitdrain_xfer_check(chp, xfer)) 1158 return; 1159 1160 ata_deactivate_xfer(chp, xfer); 1161 ata_free_xfer(chp, xfer); 1162 1163 ATADEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS); 1164 scsipi_done(sc_xfer); 1165 ATADEBUG_PRINT(("atastart from wdc_atapi_done, flags 0x%x\n", 1166 chp->ch_flags), DEBUG_XFERS); 1167 atastart(chp); 1168 } 1169 1170 static void 1171 wdc_atapi_reset(struct ata_channel *chp, struct ata_xfer *xfer) 1172 { 1173 struct atac_softc *atac = chp->ch_atac; 1174 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive]; 1175 struct scsipi_xfer *sc_xfer = xfer->c_scsipi; 1176 int tfd; 1177 1178 ata_channel_lock(chp); 1179 wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET); 1180 drvp->state = 0; 1181 if (wdc_wait_for_unbusy(chp, WDC_RESET_WAIT, AT_POLL, &tfd) != 0) { 1182 printf("%s:%d:%d: reset failed\n", 1183 device_xname(atac->atac_dev), chp->ch_channel, 1184 xfer->c_drive); 1185 sc_xfer->error = XS_SELTIMEOUT; 1186 } 1187 ata_channel_unlock(chp); 1188 wdc_atapi_done(chp, xfer); 1189 return; 1190 } 1191 1192 static void 1193 wdc_atapi_polldsc(void *arg) 1194 { 1195 struct ata_channel *chp = arg; 1196 struct ata_xfer *xfer = ata_queue_get_active_xfer(chp); 1197 1198 KASSERT(xfer != NULL); 1199 1200 ata_channel_lock(chp); 1201 1202 /* this will unlock channel lock too */ 1203 wdc_atapi_phase_complete(xfer, 0); 1204 } 1205