1 /* $NetBSD: atapi_wdc.c,v 1.55 2002/09/27 15:37:33 provos 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 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Manuel Bouyer. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: atapi_wdc.c,v 1.55 2002/09/27 15:37:33 provos Exp $"); 36 37 #ifndef WDCDEBUG 38 #define WDCDEBUG 39 #endif /* WDCDEBUG */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/file.h> 45 #include <sys/stat.h> 46 #include <sys/buf.h> 47 #include <sys/malloc.h> 48 #include <sys/device.h> 49 #include <sys/syslog.h> 50 #include <sys/proc.h> 51 #include <sys/dvdio.h> 52 53 #include <machine/intr.h> 54 #include <machine/bus.h> 55 56 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 57 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 58 #define bus_space_write_multi_stream_4 bus_space_write_multi_4 59 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 60 #define bus_space_read_multi_stream_4 bus_space_read_multi_4 61 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 62 63 #include <dev/ata/atareg.h> 64 #include <dev/ata/atavar.h> 65 #include <dev/ic/wdcreg.h> 66 #include <dev/ic/wdcvar.h> 67 68 #include <dev/scsipi/scsi_all.h> /* for SCSI status */ 69 70 #define DEBUG_INTR 0x01 71 #define DEBUG_XFERS 0x02 72 #define DEBUG_STATUS 0x04 73 #define DEBUG_FUNCS 0x08 74 #define DEBUG_PROBE 0x10 75 #ifdef WDCDEBUG 76 int wdcdebug_atapi_mask = 0; 77 #define WDCDEBUG_PRINT(args, level) \ 78 if (wdcdebug_atapi_mask & (level)) \ 79 printf args 80 #else 81 #define WDCDEBUG_PRINT(args, level) 82 #endif 83 84 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */ 85 int wdc_atapi_get_params __P((struct scsipi_channel *, int, int, 86 struct ataparams *)); 87 void wdc_atapi_probe_device __P((struct atapibus_softc *, int)); 88 void wdc_atapi_minphys __P((struct buf *bp)); 89 void wdc_atapi_start __P((struct channel_softc *,struct wdc_xfer *)); 90 int wdc_atapi_intr __P((struct channel_softc *, struct wdc_xfer *, int)); 91 void wdc_atapi_kill_xfer __P((struct channel_softc *, struct wdc_xfer *)); 92 int wdc_atapi_ctrl __P((struct channel_softc *, struct wdc_xfer *, int)); 93 void wdc_atapi_phase_complete __P((struct wdc_xfer *)); 94 void wdc_atapi_done __P((struct channel_softc *, struct wdc_xfer *)); 95 void wdc_atapi_reset __P((struct channel_softc *, struct wdc_xfer *)); 96 void wdc_atapi_scsipi_request __P((struct scsipi_channel *, 97 scsipi_adapter_req_t, void *)); 98 void wdc_atapi_kill_pending __P((struct scsipi_periph *)); 99 void wdc_atapi_polldsc __P((void *arg)); 100 101 #define MAX_SIZE MAXPHYS 102 103 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 }; 110 111 void 112 wdc_atapibus_attach(chp) 113 struct channel_softc *chp; 114 { 115 struct wdc_softc *wdc = chp->wdc; 116 struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic; 117 struct scsipi_channel *chan = &chp->ch_atapi_channel; 118 119 /* 120 * Fill in the scsipi_adapter. 121 */ 122 adapt->adapt_dev = &wdc->sc_dev; 123 adapt->adapt_nchannels = wdc->nchannels; 124 adapt->adapt_request = wdc_atapi_scsipi_request; 125 adapt->adapt_minphys = wdc_atapi_minphys; 126 if (wdc->cap & WDC_CAPABILITY_NOIRQ) 127 adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY; 128 wdc->sc_atapi_adapter.atapi_probe_device = wdc_atapi_probe_device; 129 130 /* 131 * Fill in the scsipi_channel. 132 */ 133 memset(chan, 0, sizeof(*chan)); 134 chan->chan_adapter = adapt; 135 chan->chan_bustype = &wdc_atapi_bustype; 136 chan->chan_channel = chp->channel; 137 chan->chan_flags = SCSIPI_CHAN_OPENINGS; 138 chan->chan_openings = 1; 139 chan->chan_max_periph = 1; 140 chan->chan_ntargets = 2; 141 chan->chan_nluns = 1; 142 143 chp->atapibus = config_found(&wdc->sc_dev, chan, atapiprint); 144 } 145 146 void 147 wdc_atapi_minphys(bp) 148 struct buf *bp; 149 { 150 151 if (bp->b_bcount > MAX_SIZE) 152 bp->b_bcount = MAX_SIZE; 153 minphys(bp); 154 } 155 156 /* 157 * Kill off all pending xfers for a periph. 158 * 159 * Must be called at splbio(). 160 */ 161 void 162 wdc_atapi_kill_pending(periph) 163 struct scsipi_periph *periph; 164 { 165 struct wdc_softc *wdc = 166 (void *)periph->periph_channel->chan_adapter->adapt_dev; 167 struct channel_softc *chp = 168 wdc->channels[periph->periph_channel->chan_channel]; 169 170 wdc_kill_pending(chp); 171 } 172 173 void 174 wdc_atapi_kill_xfer(chp, xfer) 175 struct channel_softc *chp; 176 struct wdc_xfer *xfer; 177 { 178 struct scsipi_xfer *sc_xfer = xfer->cmd; 179 180 callout_stop(&chp->ch_callout); 181 /* remove this command from xfer queue */ 182 wdc_free_xfer(chp, xfer); 183 sc_xfer->error = XS_DRIVER_STUFFUP; 184 scsipi_done(sc_xfer); 185 } 186 187 int 188 wdc_atapi_get_params(chan, drive, flags, id) 189 struct scsipi_channel *chan; 190 int drive, flags; 191 struct ataparams *id; 192 { 193 struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev; 194 struct channel_softc *chp = wdc->channels[chan->chan_channel]; 195 struct wdc_command wdc_c; 196 197 /* if no ATAPI device detected at wdc attach time, skip */ 198 /* 199 * XXX this will break scsireprobe if this is of any interest for 200 * ATAPI devices one day. 201 */ 202 if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) { 203 WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n", 204 drive), DEBUG_PROBE); 205 return -1; 206 } 207 memset(&wdc_c, 0, sizeof(struct wdc_command)); 208 wdc_c.r_command = ATAPI_SOFT_RESET; 209 wdc_c.r_st_bmask = 0; 210 wdc_c.r_st_pmask = 0; 211 wdc_c.flags = AT_POLL; 212 wdc_c.timeout = WDC_RESET_WAIT; 213 if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) { 214 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for" 215 " drive %s:%d:%d: driver failed\n", 216 chp->wdc->sc_dev.dv_xname, chp->channel, drive); 217 panic("wdc_atapi_get_params"); 218 } 219 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 220 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET " 221 "failed for drive %s:%d:%d: error 0x%x\n", 222 chp->wdc->sc_dev.dv_xname, chp->channel, drive, 223 wdc_c.r_error), DEBUG_PROBE); 224 return -1; 225 } 226 chp->ch_drive[drive].state = 0; 227 228 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 229 230 /* Some ATAPI devices need a bit more time after software reset. */ 231 delay(5000); 232 if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) { 233 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE " 234 "failed for drive %s:%d:%d: error 0x%x\n", 235 chp->wdc->sc_dev.dv_xname, chp->channel, drive, 236 wdc_c.r_error), DEBUG_PROBE); 237 return -1; 238 } 239 return 0; 240 } 241 242 void 243 wdc_atapi_probe_device(sc, target) 244 struct atapibus_softc *sc; 245 int target; 246 { 247 struct scsipi_channel *chan = sc->sc_channel; 248 struct scsipi_periph *periph; 249 struct ataparams ids; 250 struct ataparams *id = &ids; 251 struct wdc_softc *wdc = (void *)chan->chan_adapter->adapt_dev; 252 struct channel_softc *chp = wdc->channels[chan->chan_channel]; 253 struct ata_drive_datas *drvp = &chp->ch_drive[target]; 254 struct scsipibus_attach_args sa; 255 char serial_number[21], model[41], firmware_revision[9]; 256 257 /* skip if already attached */ 258 if (scsipi_lookup_periph(chan, target, 0) != NULL) 259 return; 260 261 if (wdc_atapi_get_params(chan, target, 262 XS_CTL_POLL|XS_CTL_NOSLEEP, id) == 0) { 263 #ifdef ATAPI_DEBUG_PROBE 264 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 265 sc->sc_dev.dv_xname, target, 266 id->atap_config & ATAPI_CFG_CMD_MASK, 267 id->atap_config & ATAPI_CFG_DRQ_MASK); 268 #endif 269 periph = scsipi_alloc_periph(M_NOWAIT); 270 if (periph == NULL) { 271 printf("%s: unable to allocate periph for drive %d\n", 272 sc->sc_dev.dv_xname, target); 273 return; 274 } 275 periph->periph_dev = NULL; 276 periph->periph_channel = chan; 277 periph->periph_switch = &atapi_probe_periphsw; 278 periph->periph_target = target; 279 periph->periph_lun = 0; 280 281 #ifdef SCSIPI_DEBUG 282 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI && 283 SCSIPI_DEBUG_TARGET == target) 284 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS; 285 #endif 286 periph->periph_type = ATAPI_CFG_TYPE(id->atap_config); 287 if (id->atap_config & ATAPI_CFG_REMOV) 288 periph->periph_flags |= PERIPH_REMOVABLE; 289 if (periph->periph_type == T_SEQUENTIAL) 290 drvp->drive_flags |= DRIVE_ATAPIST; 291 292 sa.sa_periph = periph; 293 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 294 sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ? 295 T_REMOV : T_FIXED; 296 scsipi_strvis(model, 40, id->atap_model, 40); 297 scsipi_strvis(serial_number, 20, id->atap_serial, 20); 298 scsipi_strvis(firmware_revision, 8, id->atap_revision, 8); 299 sa.sa_inqbuf.vendor = model; 300 sa.sa_inqbuf.product = serial_number; 301 sa.sa_inqbuf.revision = firmware_revision; 302 303 /* 304 * Determine the operating mode capabilities of the device. 305 */ 306 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 307 periph->periph_cap |= PERIPH_CAP_CMD16; 308 /* XXX This is gross. */ 309 periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK); 310 311 drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa); 312 313 if (drvp->drv_softc) 314 wdc_probe_caps(drvp); 315 } 316 } 317 318 void 319 wdc_atapi_scsipi_request(chan, req, arg) 320 struct scsipi_channel *chan; 321 scsipi_adapter_req_t req; 322 void *arg; 323 { 324 struct scsipi_adapter *adapt = chan->chan_adapter; 325 struct scsipi_periph *periph; 326 struct scsipi_xfer *sc_xfer; 327 struct wdc_softc *wdc = (void *)adapt->adapt_dev; 328 struct wdc_xfer *xfer; 329 int channel = chan->chan_channel; 330 int drive, s; 331 332 switch (req) { 333 case ADAPTER_REQ_RUN_XFER: 334 sc_xfer = arg; 335 periph = sc_xfer->xs_periph; 336 drive = periph->periph_target; 337 338 WDCDEBUG_PRINT(("wdc_atapi_scsipi_request %s:%d:%d\n", 339 wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS); 340 if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) { 341 sc_xfer->error = XS_DRIVER_STUFFUP; 342 scsipi_done(sc_xfer); 343 return; 344 } 345 346 xfer = wdc_get_xfer(WDC_NOSLEEP); 347 if (xfer == NULL) { 348 sc_xfer->error = XS_RESOURCE_SHORTAGE; 349 scsipi_done(sc_xfer); 350 return; 351 } 352 353 if (sc_xfer->xs_control & XS_CTL_POLL) 354 xfer->c_flags |= C_POLL; 355 xfer->drive = drive; 356 xfer->c_flags |= C_ATAPI; 357 if (sc_xfer->cmd->opcode == GPCMD_REPORT_KEY || 358 sc_xfer->cmd->opcode == GPCMD_SEND_KEY || 359 sc_xfer->cmd->opcode == GPCMD_READ_DVD_STRUCTURE) { 360 /* 361 * DVD authentication commands must always be done in 362 * PIO mode. 363 */ 364 xfer->c_flags |= C_FORCEPIO; 365 } 366 /* 367 * DMA can't deal with transfers which are not a multiple of 368 * 2 bytes. It's a bug to request such transfers for ATAPI 369 * but as the request can come from userland, we have to 370 * protect against it. 371 * Also some devices seems to not handle DMA xfers of less than 372 * 4 bytes. 373 */ 374 if (sc_xfer->datalen < 4 || (sc_xfer->datalen & 0x01)) 375 xfer->c_flags |= C_FORCEPIO; 376 377 xfer->cmd = sc_xfer; 378 xfer->databuf = sc_xfer->data; 379 xfer->c_bcount = sc_xfer->datalen; 380 xfer->c_start = wdc_atapi_start; 381 xfer->c_intr = wdc_atapi_intr; 382 xfer->c_kill_xfer = wdc_atapi_kill_xfer; 383 xfer->c_dscpoll = 0; 384 s = splbio(); 385 wdc_exec_xfer(wdc->channels[channel], xfer); 386 #ifdef DIAGNOSTIC 387 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 && 388 (sc_xfer->xs_status & XS_STS_DONE) == 0) 389 panic("wdc_atapi_scsipi_request: polled command " 390 "not done"); 391 #endif 392 splx(s); 393 return; 394 395 default: 396 /* Not supported, nothing to do. */ 397 ; 398 } 399 } 400 401 void 402 wdc_atapi_start(chp, xfer) 403 struct channel_softc *chp; 404 struct wdc_xfer *xfer; 405 { 406 struct scsipi_xfer *sc_xfer = xfer->cmd; 407 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 408 409 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n", 410 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, 411 sc_xfer->xs_control), DEBUG_XFERS); 412 /* Adjust C_DMA, it may have changed if we are requesting sense */ 413 if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) && 414 sc_xfer->datalen > 0 && !(xfer->c_flags & C_FORCEPIO)) { 415 if (drvp->n_xfers <= NXFER) 416 drvp->n_xfers++; 417 xfer->c_flags |= C_DMA; 418 } else { 419 xfer->c_flags &= ~C_DMA; 420 } 421 /* start timeout machinery */ 422 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) 423 callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout), 424 wdctimeout, chp); 425 /* Do control operations specially. */ 426 if (drvp->state < READY) { 427 if (drvp->state != RESET) { 428 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n", 429 chp->wdc->sc_dev.dv_xname, chp->channel, 430 xfer->drive, drvp->state); 431 panic("wdc_atapi_start: bad state"); 432 } 433 drvp->state = PIOMODE; 434 wdc_atapi_ctrl(chp, xfer, 0); 435 return; 436 } 437 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 438 WDSD_IBM | (xfer->drive << 4)); 439 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) { 440 printf("wdc_atapi_start: not ready, st = %02x\n", 441 chp->ch_status); 442 sc_xfer->error = XS_TIMEOUT; 443 wdc_atapi_reset(chp, xfer); 444 return; 445 } 446 447 /* 448 * Even with WDCS_ERR, the device should accept a command packet 449 * Limit length to what can be stuffed into the cylinder register 450 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536, 451 * but not all devices do that and it's not obvious from the 452 * ATAPI spec that that behaviour should be expected. If more 453 * data is necessary, multiple data transfer phases will be done. 454 */ 455 456 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD, 457 xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff, 458 0, 0, 0, 459 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0); 460 461 /* 462 * If there is no interrupt for CMD input, busy-wait for it (done in 463 * the interrupt routine. If it is a polled command, call the interrupt 464 * routine until command is done. 465 */ 466 if ((sc_xfer->xs_periph->periph_cap & ATAPI_CFG_DRQ_MASK) != 467 ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) { 468 /* Wait for at last 400ns for status bit to be valid */ 469 DELAY(1); 470 if (chp->ch_flags & WDCF_DMA_WAIT) { 471 wdc_dmawait(chp, xfer, sc_xfer->timeout); 472 chp->ch_flags &= ~WDCF_DMA_WAIT; 473 } 474 wdc_atapi_intr(chp, xfer, 0); 475 } else { 476 chp->ch_flags |= WDCF_IRQ_WAIT; 477 } 478 if (sc_xfer->xs_control & XS_CTL_POLL) { 479 while ((sc_xfer->xs_status & XS_STS_DONE) == 0) { 480 /* Wait for at last 400ns for status bit to be valid */ 481 DELAY(1); 482 wdc_atapi_intr(chp, xfer, 0); 483 } 484 } 485 } 486 487 int 488 wdc_atapi_intr(chp, xfer, irq) 489 struct channel_softc *chp; 490 struct wdc_xfer *xfer; 491 int irq; 492 { 493 struct scsipi_xfer *sc_xfer = xfer->cmd; 494 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 495 int len, phase, i, retries=0; 496 int ire; 497 int dma_flags = 0; 498 void *cmd; 499 500 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n", 501 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR); 502 503 /* Is it not a transfer, but a control operation? */ 504 if (drvp->state < READY) { 505 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n", 506 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 507 drvp->state); 508 panic("wdc_atapi_intr: bad state"); 509 } 510 /* 511 * If we missed an interrupt in a PIO transfer, reset and restart. 512 * Don't try to continue transfer, we may have missed cycles. 513 */ 514 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) { 515 sc_xfer->error = XS_TIMEOUT; 516 wdc_atapi_reset(chp, xfer); 517 return 1; 518 } 519 520 /* Ack interrupt done in wait_for_unbusy */ 521 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 522 WDSD_IBM | (xfer->drive << 4)); 523 if (wait_for_unbusy(chp, 524 (irq == 0) ? sc_xfer->timeout : 0) != 0) { 525 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 526 return 0; /* IRQ was not for us */ 527 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n", 528 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 529 xfer->c_bcount, xfer->c_skip); 530 if (xfer->c_flags & C_DMA) { 531 ata_dmaerr(drvp); 532 } 533 sc_xfer->error = XS_TIMEOUT; 534 wdc_atapi_reset(chp, xfer); 535 return 1; 536 } 537 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 538 chp->wdc->irqack(chp); 539 540 /* 541 * If we missed an IRQ and were using DMA, flag it as a DMA error 542 * and reset device. 543 */ 544 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) { 545 ata_dmaerr(drvp); 546 sc_xfer->error = XS_RESET; 547 wdc_atapi_reset(chp, xfer); 548 return (1); 549 } 550 /* 551 * if the request sense command was aborted, report the short sense 552 * previously recorded, else continue normal processing 553 */ 554 555 if (xfer->c_flags & C_DMA) 556 dma_flags = (sc_xfer->xs_control & XS_CTL_DATA_IN) 557 ? WDC_DMA_READ : 0; 558 again: 559 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) + 560 256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi); 561 ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason); 562 phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ); 563 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x " 564 "ire 0x%x :", xfer->c_bcount, 565 len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR); 566 567 switch (phase) { 568 case PHASE_CMDOUT: 569 cmd = sc_xfer->cmd; 570 WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR); 571 /* Init the DMA channel if necessary */ 572 if (xfer->c_flags & C_DMA) { 573 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg, 574 chp->channel, xfer->drive, 575 xfer->databuf, xfer->c_bcount, dma_flags) != 0) { 576 sc_xfer->error = XS_DRIVER_STUFFUP; 577 break; 578 } 579 } 580 /* send packet command */ 581 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */ 582 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) { 583 if (drvp->drive_flags & DRIVE_CAP32) { 584 bus_space_write_multi_4(chp->data32iot, 585 chp->data32ioh, 0, 586 (u_int32_t *)cmd, 587 sc_xfer->cmdlen >> 2); 588 } else { 589 bus_space_write_multi_2(chp->cmd_iot, 590 chp->cmd_ioh, wd_data, 591 (u_int16_t *)cmd, 592 sc_xfer->cmdlen >> 1); 593 } 594 } else { 595 if (drvp->drive_flags & DRIVE_CAP32) { 596 bus_space_write_multi_stream_4(chp->data32iot, 597 chp->data32ioh, 0, 598 (u_int32_t *)cmd, 599 sc_xfer->cmdlen >> 2); 600 } else { 601 bus_space_write_multi_stream_2(chp->cmd_iot, 602 chp->cmd_ioh, wd_data, 603 (u_int16_t *)cmd, 604 sc_xfer->cmdlen >> 1); 605 } 606 } 607 /* Start the DMA channel if necessary */ 608 if (xfer->c_flags & C_DMA) { 609 (*chp->wdc->dma_start)(chp->wdc->dma_arg, 610 chp->channel, xfer->drive); 611 chp->ch_flags |= WDCF_DMA_WAIT; 612 } 613 614 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 615 chp->ch_flags |= WDCF_IRQ_WAIT; 616 } 617 return 1; 618 619 case PHASE_DATAOUT: 620 /* write data */ 621 WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR); 622 if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 || 623 (xfer->c_flags & C_DMA) != 0) { 624 printf("wdc_atapi_intr: bad data phase DATAOUT\n"); 625 if (xfer->c_flags & C_DMA) { 626 ata_dmaerr(drvp); 627 } 628 sc_xfer->error = XS_TIMEOUT; 629 wdc_atapi_reset(chp, xfer); 630 return 1; 631 } 632 if (xfer->c_bcount < len) { 633 printf("wdc_atapi_intr: warning: write only " 634 "%d of %d requested bytes\n", xfer->c_bcount, len); 635 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) { 636 bus_space_write_multi_2(chp->cmd_iot, 637 chp->cmd_ioh, wd_data, 638 (u_int16_t *)((char *)xfer->databuf + 639 xfer->c_skip), 640 xfer->c_bcount >> 1); 641 } else { 642 bus_space_write_multi_stream_2(chp->cmd_iot, 643 chp->cmd_ioh, wd_data, 644 (u_int16_t *)((char *)xfer->databuf + 645 xfer->c_skip), 646 xfer->c_bcount >> 1); 647 } 648 for (i = xfer->c_bcount; i < len; i += 2) 649 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh, 650 wd_data, 0); 651 xfer->c_skip += xfer->c_bcount; 652 xfer->c_bcount = 0; 653 } else { 654 if (drvp->drive_flags & DRIVE_CAP32) { 655 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 656 bus_space_write_multi_4(chp->data32iot, 657 chp->data32ioh, 0, 658 (u_int32_t *)((char *)xfer->databuf + 659 xfer->c_skip), 660 len >> 2); 661 else 662 bus_space_write_multi_stream_4(chp->data32iot, 663 chp->data32ioh, wd_data, 664 (u_int32_t *)((char *)xfer->databuf + 665 xfer->c_skip), 666 len >> 2); 667 668 xfer->c_skip += len & 0xfffffffc; 669 xfer->c_bcount -= len & 0xfffffffc; 670 len = len & 0x03; 671 } 672 if (len > 0) { 673 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 674 bus_space_write_multi_2(chp->cmd_iot, 675 chp->cmd_ioh, wd_data, 676 (u_int16_t *)((char *)xfer->databuf + 677 xfer->c_skip), 678 len >> 1); 679 else 680 bus_space_write_multi_stream_2(chp->cmd_iot, 681 chp->cmd_ioh, wd_data, 682 (u_int16_t *)((char *)xfer->databuf + 683 xfer->c_skip), 684 len >> 1); 685 xfer->c_skip += len; 686 xfer->c_bcount -= len; 687 } 688 } 689 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 690 chp->ch_flags |= WDCF_IRQ_WAIT; 691 } 692 return 1; 693 694 case PHASE_DATAIN: 695 /* Read data */ 696 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR); 697 if ((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 || 698 (xfer->c_flags & C_DMA) != 0) { 699 printf("wdc_atapi_intr: bad data phase DATAIN\n"); 700 if (xfer->c_flags & C_DMA) { 701 ata_dmaerr(drvp); 702 } 703 sc_xfer->error = XS_TIMEOUT; 704 wdc_atapi_reset(chp, xfer); 705 return 1; 706 } 707 if (xfer->c_bcount < len) { 708 printf("wdc_atapi_intr: warning: reading only " 709 "%d of %d bytes\n", xfer->c_bcount, len); 710 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) { 711 bus_space_read_multi_2(chp->cmd_iot, 712 chp->cmd_ioh, wd_data, 713 (u_int16_t *)((char *)xfer->databuf + 714 xfer->c_skip), 715 xfer->c_bcount >> 1); 716 } else { 717 bus_space_read_multi_stream_2(chp->cmd_iot, 718 chp->cmd_ioh, wd_data, 719 (u_int16_t *)((char *)xfer->databuf + 720 xfer->c_skip), 721 xfer->c_bcount >> 1); 722 } 723 wdcbit_bucket(chp, len - xfer->c_bcount); 724 xfer->c_skip += xfer->c_bcount; 725 xfer->c_bcount = 0; 726 } else { 727 if (drvp->drive_flags & DRIVE_CAP32) { 728 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 729 bus_space_read_multi_4(chp->data32iot, 730 chp->data32ioh, 0, 731 (u_int32_t *)((char *)xfer->databuf + 732 xfer->c_skip), 733 len >> 2); 734 else 735 bus_space_read_multi_stream_4(chp->data32iot, 736 chp->data32ioh, wd_data, 737 (u_int32_t *)((char *)xfer->databuf + 738 xfer->c_skip), 739 len >> 2); 740 741 xfer->c_skip += len & 0xfffffffc; 742 xfer->c_bcount -= len & 0xfffffffc; 743 len = len & 0x03; 744 } 745 if (len > 0) { 746 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 747 bus_space_read_multi_2(chp->cmd_iot, 748 chp->cmd_ioh, wd_data, 749 (u_int16_t *)((char *)xfer->databuf + 750 xfer->c_skip), 751 len >> 1); 752 else 753 bus_space_read_multi_stream_2(chp->cmd_iot, 754 chp->cmd_ioh, wd_data, 755 (u_int16_t *)((char *)xfer->databuf + 756 xfer->c_skip), 757 len >> 1); 758 xfer->c_skip += len; 759 xfer->c_bcount -=len; 760 } 761 } 762 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 763 chp->ch_flags |= WDCF_IRQ_WAIT; 764 } 765 return 1; 766 767 case PHASE_ABORTED: 768 case PHASE_COMPLETED: 769 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR); 770 if (xfer->c_flags & C_DMA) { 771 xfer->c_bcount -= sc_xfer->datalen; 772 } 773 sc_xfer->resid = xfer->c_bcount; 774 wdc_atapi_phase_complete(xfer); 775 return(1); 776 777 default: 778 if (++retries<500) { 779 DELAY(100); 780 chp->ch_status = bus_space_read_1(chp->cmd_iot, 781 chp->cmd_ioh, wd_status); 782 chp->ch_error = bus_space_read_1(chp->cmd_iot, 783 chp->cmd_ioh, wd_error); 784 goto again; 785 } 786 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase); 787 if (chp->ch_status & WDCS_ERR) { 788 sc_xfer->error = XS_SHORTSENSE; 789 sc_xfer->sense.atapi_sense = chp->ch_error; 790 } else { 791 if (xfer->c_flags & C_DMA) { 792 ata_dmaerr(drvp); 793 } 794 sc_xfer->error = XS_RESET; 795 wdc_atapi_reset(chp, xfer); 796 return (1); 797 } 798 } 799 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x " 800 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense), 801 DEBUG_INTR); 802 wdc_atapi_done(chp, xfer); 803 return (1); 804 } 805 806 int 807 wdc_atapi_ctrl(chp, xfer, irq) 808 struct channel_softc *chp; 809 struct wdc_xfer *xfer; 810 int irq; 811 { 812 struct scsipi_xfer *sc_xfer = xfer->cmd; 813 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 814 char *errstring = NULL; 815 int delay = (irq == 0) ? ATAPI_DELAY : 0; 816 817 /* Ack interrupt done in wait_for_unbusy */ 818 again: 819 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n", 820 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state), 821 DEBUG_INTR | DEBUG_FUNCS); 822 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 823 WDSD_IBM | (xfer->drive << 4)); 824 switch (drvp->state) { 825 case PIOMODE: 826 /* Don't try to set mode if controller can't be adjusted */ 827 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0) 828 goto ready; 829 /* Also don't try if the drive didn't report its mode */ 830 if ((drvp->drive_flags & DRIVE_MODE) == 0) 831 goto ready; 832 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 833 0x08 | drvp->PIO_mode, WDSF_SET_MODE); 834 drvp->state = PIOMODE_WAIT; 835 break; 836 case PIOMODE_WAIT: 837 errstring = "piomode"; 838 if (wait_for_unbusy(chp, delay)) 839 goto timeout; 840 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 841 chp->wdc->irqack(chp); 842 if (chp->ch_status & WDCS_ERR) { 843 if (chp->ch_error == WDCE_ABRT) { 844 /* 845 * some ATAPI drives rejects pio settings. 846 * all we can do here is fall back to PIO 0 847 */ 848 drvp->drive_flags &= ~DRIVE_MODE; 849 drvp->drive_flags &= ~(DRIVE_DMA|DRIVE_UDMA); 850 drvp->PIO_mode = 0; 851 drvp->DMA_mode = 0; 852 printf("%s:%d:%d: pio setting rejected, " 853 "falling back to PIO mode 0\n", 854 chp->wdc->sc_dev.dv_xname, 855 chp->channel, xfer->drive); 856 chp->wdc->set_modes(chp); 857 goto ready; 858 } 859 goto error; 860 } 861 /* fall through */ 862 863 case DMAMODE: 864 if (drvp->drive_flags & DRIVE_UDMA) { 865 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 866 0x40 | drvp->UDMA_mode, WDSF_SET_MODE); 867 } else if (drvp->drive_flags & DRIVE_DMA) { 868 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 869 0x20 | drvp->DMA_mode, WDSF_SET_MODE); 870 } else { 871 goto ready; 872 } 873 drvp->state = DMAMODE_WAIT; 874 break; 875 case DMAMODE_WAIT: 876 errstring = "dmamode"; 877 if (wait_for_unbusy(chp, delay)) 878 goto timeout; 879 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 880 chp->wdc->irqack(chp); 881 if (chp->ch_status & WDCS_ERR) 882 goto error; 883 /* fall through */ 884 885 case READY: 886 ready: 887 drvp->state = READY; 888 xfer->c_intr = wdc_atapi_intr; 889 callout_stop(&chp->ch_callout); 890 wdc_atapi_start(chp, xfer); 891 return 1; 892 } 893 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 894 chp->ch_flags |= WDCF_IRQ_WAIT; 895 xfer->c_intr = wdc_atapi_ctrl; 896 } else { 897 goto again; 898 } 899 return 1; 900 901 timeout: 902 if (irq && (xfer->c_flags & C_TIMEOU) == 0) { 903 return 0; /* IRQ was not for us */ 904 } 905 printf("%s:%d:%d: %s timed out\n", 906 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring); 907 sc_xfer->error = XS_TIMEOUT; 908 wdc_atapi_reset(chp, xfer); 909 return 1; 910 error: 911 printf("%s:%d:%d: %s ", 912 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 913 errstring); 914 printf("error (0x%x)\n", chp->ch_error); 915 sc_xfer->error = XS_SHORTSENSE; 916 sc_xfer->sense.atapi_sense = chp->ch_error; 917 wdc_atapi_reset(chp, xfer); 918 return 1; 919 } 920 921 void 922 wdc_atapi_phase_complete(xfer) 923 struct wdc_xfer *xfer; 924 { 925 struct channel_softc *chp = xfer->chp; 926 struct scsipi_xfer *sc_xfer = xfer->cmd; 927 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 928 929 /* wait for DSC if needed */ 930 if (drvp->drive_flags & DRIVE_ATAPIST) { 931 WDCDEBUG_PRINT(("wdc_atapi_phase_complete(%s:%d:%d) " 932 "polldsc %d\n", chp->wdc->sc_dev.dv_xname, chp->channel, 933 xfer->drive, xfer->c_dscpoll), DEBUG_XFERS); 934 if (cold) { 935 if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 936 sc_xfer->timeout)) { 937 printf("%s:%d:%d: wait_for_dsc failed\n", 938 chp->wdc->sc_dev.dv_xname, chp->channel, 939 xfer->drive); 940 sc_xfer->error = XS_TIMEOUT; 941 wdc_atapi_reset(chp, xfer); 942 return; 943 } 944 } else { 945 if (wdcwait(chp, WDCS_DSC, WDCS_DSC, 10)) { 946 /* 10ms not enough, try again in 1 tick */ 947 if (xfer->c_dscpoll++ > 948 mstohz(sc_xfer->timeout)) { 949 printf("%s:%d:%d: wait_for_dsc " 950 "failed\n", 951 chp->wdc->sc_dev.dv_xname, 952 chp->channel, xfer->drive); 953 sc_xfer->error = XS_TIMEOUT; 954 wdc_atapi_reset(chp, xfer); 955 return; 956 } 957 callout_reset(&chp->ch_callout, 1, 958 wdc_atapi_polldsc, xfer); 959 return; 960 } 961 } 962 } 963 964 /* 965 * Some drive occasionally set WDCS_ERR with 966 * "ATA illegal length indication" in the error 967 * register. If we read some data the sense is valid 968 * anyway, so don't report the error. 969 */ 970 if (chp->ch_status & WDCS_ERR && 971 ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 || 972 sc_xfer->resid == sc_xfer->datalen)) { 973 /* save the short sense */ 974 sc_xfer->error = XS_SHORTSENSE; 975 sc_xfer->sense.atapi_sense = chp->ch_error; 976 if ((sc_xfer->xs_periph->periph_quirks & 977 PQUIRK_NOSENSE) == 0) { 978 /* ask scsipi to send a REQUEST_SENSE */ 979 sc_xfer->error = XS_BUSY; 980 sc_xfer->status = SCSI_CHECK; 981 } else if (chp->wdc->dma_status & 982 (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) { 983 ata_dmaerr(drvp); 984 sc_xfer->error = XS_RESET; 985 wdc_atapi_reset(chp, xfer); 986 return; 987 } 988 } 989 if (xfer->c_bcount != 0) { 990 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is " 991 "%d after io\n", xfer->c_bcount), DEBUG_XFERS); 992 } 993 #ifdef DIAGNOSTIC 994 if (xfer->c_bcount < 0) { 995 printf("wdc_atapi_intr warning: bcount value " 996 "is %d after io\n", xfer->c_bcount); 997 } 998 #endif 999 WDCDEBUG_PRINT(("wdc_atapi_phase_complete: wdc_atapi_done(), " 1000 "error 0x%x sense 0x%x\n", sc_xfer->error, 1001 sc_xfer->sense.atapi_sense), DEBUG_INTR); 1002 wdc_atapi_done(chp, xfer); 1003 } 1004 1005 void 1006 wdc_atapi_done(chp, xfer) 1007 struct channel_softc *chp; 1008 struct wdc_xfer *xfer; 1009 { 1010 struct scsipi_xfer *sc_xfer = xfer->cmd; 1011 1012 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n", 1013 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 1014 (u_int)xfer->c_flags), DEBUG_XFERS); 1015 callout_stop(&chp->ch_callout); 1016 /* remove this command from xfer queue */ 1017 wdc_free_xfer(chp, xfer); 1018 1019 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS); 1020 scsipi_done(sc_xfer); 1021 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n", 1022 chp->ch_flags), DEBUG_XFERS); 1023 wdcstart(chp); 1024 } 1025 1026 void 1027 wdc_atapi_reset(chp, xfer) 1028 struct channel_softc *chp; 1029 struct wdc_xfer *xfer; 1030 { 1031 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 1032 struct scsipi_xfer *sc_xfer = xfer->cmd; 1033 1034 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET); 1035 drvp->state = 0; 1036 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) { 1037 printf("%s:%d:%d: reset failed\n", 1038 chp->wdc->sc_dev.dv_xname, chp->channel, 1039 xfer->drive); 1040 sc_xfer->error = XS_SELTIMEOUT; 1041 } 1042 wdc_atapi_done(chp, xfer); 1043 return; 1044 } 1045 1046 void 1047 wdc_atapi_polldsc(arg) 1048 void *arg; 1049 { 1050 wdc_atapi_phase_complete(arg); 1051 } 1052