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