1 /* $NetBSD: atapi_wdc.c,v 1.37 2000/06/28 16:39:28 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1998 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 #ifndef WDCDEBUG 36 #define WDCDEBUG 37 #endif /* WDCDEBUG */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/file.h> 43 #include <sys/stat.h> 44 #include <sys/buf.h> 45 #include <sys/malloc.h> 46 #include <sys/device.h> 47 #include <sys/syslog.h> 48 #include <sys/proc.h> 49 50 #include <machine/intr.h> 51 #include <machine/bus.h> 52 53 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 54 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 55 #define bus_space_write_multi_stream_4 bus_space_write_multi_4 56 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 57 #define bus_space_read_multi_stream_4 bus_space_read_multi_4 58 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 59 60 #include <dev/ata/atareg.h> 61 #include <dev/ata/atavar.h> 62 #include <dev/ic/wdcreg.h> 63 #include <dev/ic/wdcvar.h> 64 65 #define DEBUG_INTR 0x01 66 #define DEBUG_XFERS 0x02 67 #define DEBUG_STATUS 0x04 68 #define DEBUG_FUNCS 0x08 69 #define DEBUG_PROBE 0x10 70 #ifdef WDCDEBUG 71 int wdcdebug_atapi_mask = 0; 72 #define WDCDEBUG_PRINT(args, level) \ 73 if (wdcdebug_atapi_mask & (level)) \ 74 printf args 75 #else 76 #define WDCDEBUG_PRINT(args, level) 77 #endif 78 79 #define ATAPI_DELAY 10 /* 10 ms, this is used only before sending a cmd */ 80 81 void wdc_atapi_minphys __P((struct buf *bp)); 82 void wdc_atapi_start __P((struct channel_softc *,struct wdc_xfer *)); 83 int wdc_atapi_intr __P((struct channel_softc *, struct wdc_xfer *, int)); 84 void wdc_atapi_kill_xfer __P((struct channel_softc *, struct wdc_xfer *)); 85 int wdc_atapi_ctrl __P((struct channel_softc *, struct wdc_xfer *, int)); 86 void wdc_atapi_done __P((struct channel_softc *, struct wdc_xfer *)); 87 void wdc_atapi_reset __P((struct channel_softc *, struct wdc_xfer *)); 88 int wdc_atapi_get_params __P((struct scsipi_link *, u_int8_t, int, 89 struct ataparams *)); 90 void wdc_atapi_probedev __P(( struct atapibus_softc *, int)); 91 int wdc_atapi_send_cmd __P((struct scsipi_xfer *sc_xfer)); 92 93 #define MAX_SIZE MAXPHYS 94 95 void 96 wdc_atapibus_attach(chp) 97 struct channel_softc *chp; 98 { 99 struct wdc_softc *wdc = chp->wdc; 100 int channel = chp->channel; 101 struct ata_atapi_attach aa_link; 102 103 /* 104 * Fill in the adapter. 105 */ 106 wdc->sc_atapi_adapter._generic.scsipi_cmd = wdc_atapi_send_cmd; 107 wdc->sc_atapi_adapter._generic.scsipi_minphys = wdc_atapi_minphys; 108 wdc->sc_atapi_adapter.atapi_probedev = wdc_atapi_probedev; 109 wdc->sc_atapi_adapter.atapi_kill_pending = atapi_kill_pending; 110 111 memset(&aa_link, 0, sizeof(struct ata_atapi_attach)); 112 aa_link.aa_type = T_ATAPI; 113 aa_link.aa_channel = channel; 114 aa_link.aa_openings = 1; 115 aa_link.aa_drv_data = chp->ch_drive; /* pass the whole array */ 116 aa_link.aa_bus_private = &wdc->sc_atapi_adapter; 117 chp->atapibus = config_found(&wdc->sc_dev, (void *)&aa_link, 118 atapi_print); 119 } 120 121 void 122 wdc_atapi_minphys (struct buf *bp) 123 { 124 if(bp->b_bcount > MAX_SIZE) 125 bp->b_bcount = MAX_SIZE; 126 minphys(bp); 127 } 128 129 /* 130 * Kill off all pending xfers for a scsipi_link. 131 * 132 * Must be called at splbio(). 133 */ 134 void 135 atapi_kill_pending(sc_link) 136 struct scsipi_link *sc_link; 137 { 138 struct wdc_softc *wdc = (void *)sc_link->adapter_softc; 139 struct channel_softc *chp = 140 wdc->channels[sc_link->scsipi_atapi.channel]; 141 142 wdc_kill_pending(chp); 143 } 144 145 void 146 wdc_atapi_kill_xfer(chp, xfer) 147 struct channel_softc *chp; 148 struct wdc_xfer *xfer; 149 { 150 struct scsipi_xfer *sc_xfer = xfer->cmd; 151 152 callout_stop(&chp->ch_callout); 153 /* remove this command from xfer queue */ 154 wdc_free_xfer(chp, xfer); 155 sc_xfer->xs_status |= XS_STS_DONE; 156 sc_xfer->error = XS_DRIVER_STUFFUP; 157 scsipi_done(sc_xfer); 158 } 159 160 int 161 wdc_atapi_get_params(ab_link, drive, flags, id) 162 struct scsipi_link *ab_link; 163 u_int8_t drive; 164 int flags; 165 struct ataparams *id; 166 { 167 struct wdc_softc *wdc = (void*)ab_link->adapter_softc; 168 struct channel_softc *chp = 169 wdc->channels[ab_link->scsipi_atapi.channel]; 170 struct wdc_command wdc_c; 171 172 /* if no ATAPI device detected at wdc attach time, skip */ 173 /* 174 * XXX this will break scsireprobe if this is of any interest for 175 * ATAPI devices one day. 176 */ 177 if ((chp->ch_drive[drive].drive_flags & DRIVE_ATAPI) == 0) { 178 WDCDEBUG_PRINT(("wdc_atapi_get_params: drive %d not present\n", 179 drive), DEBUG_PROBE); 180 return -1; 181 } 182 memset(&wdc_c, 0, sizeof(struct wdc_command)); 183 wdc_c.r_command = ATAPI_SOFT_RESET; 184 wdc_c.r_st_bmask = 0; 185 wdc_c.r_st_pmask = 0; 186 wdc_c.flags = AT_POLL; 187 wdc_c.timeout = WDC_RESET_WAIT; 188 if (wdc_exec_command(&chp->ch_drive[drive], &wdc_c) != WDC_COMPLETE) { 189 printf("wdc_atapi_get_params: ATAPI_SOFT_RESET failed for" 190 " drive %s:%d:%d: driver failed\n", 191 chp->wdc->sc_dev.dv_xname, chp->channel, drive); 192 panic("wdc_atapi_get_params"); 193 } 194 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 195 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_SOFT_RESET " 196 "failed for drive %s:%d:%d: error 0x%x\n", 197 chp->wdc->sc_dev.dv_xname, chp->channel, drive, 198 wdc_c.r_error), DEBUG_PROBE); 199 return -1; 200 } 201 chp->ch_drive[drive].state = 0; 202 203 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 204 205 /* Some ATAPI devices need a bit more time after software reset. */ 206 delay(5000); 207 if (ata_get_params(&chp->ch_drive[drive], AT_POLL, id) != 0) { 208 WDCDEBUG_PRINT(("wdc_atapi_get_params: ATAPI_IDENTIFY_DEVICE " 209 "failed for drive %s:%d:%d: error 0x%x\n", 210 chp->wdc->sc_dev.dv_xname, chp->channel, drive, 211 wdc_c.r_error), DEBUG_PROBE); 212 return -1; 213 } 214 return COMPLETE; 215 } 216 217 void 218 wdc_atapi_probedev(atapi, target) 219 struct atapibus_softc *atapi; 220 int target; 221 { 222 struct scsipi_link *sc_link; 223 struct scsipibus_attach_args sa; 224 struct ataparams ids; 225 struct ataparams *id = &ids; 226 struct ata_drive_datas *drvp = &atapi->sc_drvs[target]; 227 char serial_number[21], model[41], firmware_revision[9]; 228 229 if (atapi->sc_link[target]) 230 return; 231 if (wdc_atapi_get_params(atapi->adapter_link, target, 232 XS_CTL_POLL|XS_CTL_NOSLEEP, id) == COMPLETE) { 233 #ifdef ATAPI_DEBUG_PROBE 234 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 235 atapi->sc_dev.dv_xname, target, 236 id->atap_config & ATAPI_CFG_CMD_MASK, 237 id->atap_config & ATAPI_CFG_DRQ_MASK); 238 #endif 239 /* 240 * Allocate a device link and try and attach 241 * a driver to this device. If we fail, free 242 * the link. 243 */ 244 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT); 245 if (sc_link == NULL) { 246 printf("%s: can't allocate link for drive %d\n", 247 atapi->sc_dev.dv_xname, target); 248 return; 249 } 250 /* fill in the link (IDE-specific part) */ 251 *sc_link = *atapi->adapter_link; 252 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 253 sc_link->scsipi_atapi.cap |= ACAP_LEN; 254 sc_link->scsipi_atapi.cap |= 255 (id->atap_config & ATAPI_CFG_DRQ_MASK); 256 sa.sa_sc_link = sc_link; 257 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 258 sa.sa_inqbuf.removable = 259 id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED; 260 if (sa.sa_inqbuf.removable) 261 sc_link->flags |= SDEV_REMOVABLE; 262 scsipi_strvis(model, 40, id->atap_model, 40); 263 scsipi_strvis(serial_number, 20, id->atap_serial, 20); 264 scsipi_strvis(firmware_revision, 8, id->atap_revision, 8); 265 sa.sa_inqbuf.vendor = model; 266 sa.sa_inqbuf.product = serial_number; 267 sa.sa_inqbuf.revision = firmware_revision; 268 sa.sa_inqptr = NULL; 269 drvp->drv_softc = atapi_probedev(atapi, target, sc_link, &sa); 270 if (drvp->drv_softc) { 271 wdc_probe_caps(drvp); 272 return; 273 } 274 } 275 } 276 277 int 278 wdc_atapi_send_cmd(sc_xfer) 279 struct scsipi_xfer *sc_xfer; 280 { 281 struct wdc_softc *wdc = (void*)sc_xfer->sc_link->adapter_softc; 282 struct wdc_xfer *xfer; 283 int flags = sc_xfer->xs_control; 284 int channel = sc_xfer->sc_link->scsipi_atapi.channel; 285 int drive = sc_xfer->sc_link->scsipi_atapi.drive; 286 int s, ret; 287 288 WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d\n", 289 wdc->sc_dev.dv_xname, channel, drive), DEBUG_XFERS); 290 291 if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) { 292 sc_xfer->xs_status |= XS_STS_DONE; 293 sc_xfer->error = XS_DRIVER_STUFFUP; 294 scsipi_done(sc_xfer); 295 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) 296 return (SUCCESSFULLY_QUEUED); 297 else 298 return (COMPLETE); 299 } 300 301 xfer = wdc_get_xfer((flags & XS_CTL_NOSLEEP) ? 302 WDC_NOSLEEP : WDC_CANSLEEP); 303 if (xfer == NULL) { 304 return TRY_AGAIN_LATER; 305 } 306 if (sc_xfer->xs_control & XS_CTL_POLL) 307 xfer->c_flags |= C_POLL; 308 xfer->drive = drive; 309 xfer->c_flags |= C_ATAPI; 310 xfer->cmd = sc_xfer; 311 xfer->databuf = sc_xfer->data; 312 xfer->c_bcount = sc_xfer->datalen; 313 xfer->c_start = wdc_atapi_start; 314 xfer->c_intr = wdc_atapi_intr; 315 xfer->c_kill_xfer = wdc_atapi_kill_xfer; 316 s = splbio(); 317 wdc_exec_xfer(wdc->channels[channel], xfer); 318 #ifdef DIAGNOSTIC 319 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 && 320 (sc_xfer->xs_status & XS_STS_DONE) == 0) 321 panic("wdc_atapi_send_cmd: polled command not done"); 322 #endif 323 ret = (sc_xfer->xs_status & XS_STS_DONE) ? 324 COMPLETE : SUCCESSFULLY_QUEUED; 325 splx(s); 326 return ret; 327 } 328 329 void 330 wdc_atapi_start(chp, xfer) 331 struct channel_softc *chp; 332 struct wdc_xfer *xfer; 333 { 334 struct scsipi_xfer *sc_xfer = xfer->cmd; 335 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 336 337 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x \n", 338 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, 339 sc_xfer->xs_control), DEBUG_XFERS); 340 /* Adjust C_DMA, it may have changed if we are requesting sense */ 341 if ((drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) && 342 (sc_xfer->datalen > 0 || (xfer->c_flags & C_SENSE))) { 343 if (drvp->n_xfers <= NXFER) 344 drvp->n_xfers++; 345 xfer->c_flags |= C_DMA; 346 } else { 347 xfer->c_flags &= ~C_DMA; 348 } 349 /* start timeout machinery */ 350 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) 351 callout_reset(&chp->ch_callout, sc_xfer->timeout * hz / 1000, 352 wdctimeout, chp); 353 /* Do control operations specially. */ 354 if (drvp->state < READY) { 355 if (drvp->state != RESET) { 356 printf("%s:%d:%d: bad state %d in wdc_atapi_start\n", 357 chp->wdc->sc_dev.dv_xname, chp->channel, 358 xfer->drive, drvp->state); 359 panic("wdc_atapi_start: bad state"); 360 } 361 drvp->state = PIOMODE; 362 wdc_atapi_ctrl(chp, xfer, 0); 363 return; 364 } 365 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 366 WDSD_IBM | (xfer->drive << 4)); 367 if (wait_for_unbusy(chp, ATAPI_DELAY) < 0) { 368 printf("wdc_atapi_start: not ready, st = %02x\n", 369 chp->ch_status); 370 sc_xfer->error = XS_TIMEOUT; 371 wdc_atapi_reset(chp, xfer); 372 return; 373 } 374 375 /* 376 * Even with WDCS_ERR, the device should accept a command packet 377 * Limit length to what can be stuffed into the cylinder register 378 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536, 379 * but not all devices do that and it's not obvious from the 380 * ATAPI spec that that behaviour should be expected. If more 381 * data is necessary, multiple data transfer phases will be done. 382 */ 383 384 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD, 385 xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff, 386 0, 0, 0, 387 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0); 388 389 /* 390 * If there is no interrupt for CMD input, busy-wait for it (done in 391 * the interrupt routine. If it is a polled command, call the interrupt 392 * routine until command is done. 393 */ 394 if ((sc_xfer->sc_link->scsipi_atapi.cap & ATAPI_CFG_DRQ_MASK) != 395 ATAPI_CFG_IRQ_DRQ || (sc_xfer->xs_control & XS_CTL_POLL)) { 396 /* Wait for at last 400ns for status bit to be valid */ 397 DELAY(1); 398 wdc_atapi_intr(chp, xfer, 0); 399 } else { 400 chp->ch_flags |= WDCF_IRQ_WAIT; 401 } 402 if (sc_xfer->xs_control & XS_CTL_POLL) { 403 while ((sc_xfer->xs_status & XS_STS_DONE) == 0) { 404 /* Wait for at last 400ns for status bit to be valid */ 405 DELAY(1); 406 if (chp->ch_flags & WDCF_DMA_WAIT) { 407 wdc_dmawait(chp, xfer, sc_xfer->timeout); 408 chp->ch_flags &= ~WDCF_DMA_WAIT; 409 } 410 wdc_atapi_intr(chp, xfer, 0); 411 } 412 } 413 } 414 415 int 416 wdc_atapi_intr(chp, xfer, irq) 417 struct channel_softc *chp; 418 struct wdc_xfer *xfer; 419 int irq; 420 { 421 struct scsipi_xfer *sc_xfer = xfer->cmd; 422 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 423 int len, phase, i, retries=0; 424 int ire; 425 int dma_flags = 0; 426 struct scsipi_generic _cmd_reqsense; 427 struct scsipi_sense *cmd_reqsense = 428 (struct scsipi_sense *)&_cmd_reqsense; 429 void *cmd; 430 431 WDCDEBUG_PRINT(("wdc_atapi_intr %s:%d:%d\n", 432 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), DEBUG_INTR); 433 434 /* Is it not a transfer, but a control operation? */ 435 if (drvp->state < READY) { 436 printf("%s:%d:%d: bad state %d in wdc_atapi_intr\n", 437 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 438 drvp->state); 439 panic("wdc_atapi_intr: bad state\n"); 440 } 441 /* 442 * If we missed an interrupt in a PIO transfer, reset and restart. 443 * Don't try to continue transfer, we may have missed cycles. 444 */ 445 if ((xfer->c_flags & (C_TIMEOU | C_DMA)) == C_TIMEOU) { 446 sc_xfer->error = XS_TIMEOUT; 447 wdc_atapi_reset(chp, xfer); 448 return 1; 449 } 450 451 /* Ack interrupt done in wait_for_unbusy */ 452 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 453 WDSD_IBM | (xfer->drive << 4)); 454 if (wait_for_unbusy(chp, 455 (irq == 0) ? sc_xfer->timeout : 0) != 0) { 456 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 457 return 0; /* IRQ was not for us */ 458 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d\n", 459 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 460 xfer->c_bcount, xfer->c_skip); 461 if (xfer->c_flags & C_DMA) { 462 ata_dmaerr(drvp); 463 } 464 sc_xfer->error = XS_TIMEOUT; 465 wdc_atapi_reset(chp, xfer); 466 return 1; 467 } 468 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 469 chp->wdc->irqack(chp); 470 471 /* If we missed an IRQ and were using DMA, flag it as a DMA error */ 472 if ((xfer->c_flags & C_TIMEOU) && (xfer->c_flags & C_DMA)) { 473 ata_dmaerr(drvp); 474 } 475 /* 476 * if the request sense command was aborted, report the short sense 477 * previously recorded, else continue normal processing 478 */ 479 480 if ((xfer->c_flags & C_SENSE) != 0 && 481 (chp->ch_status & WDCS_ERR) != 0 && 482 (chp->ch_error & WDCE_ABRT) != 0) { 483 WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, " 484 "calling wdc_atapi_done(), sense 0x%x\n", 485 sc_xfer->sense.atapi_sense), DEBUG_INTR); 486 wdc_atapi_done(chp, xfer); 487 return 1; 488 } 489 490 if (xfer->c_flags & C_DMA) 491 dma_flags = ((sc_xfer->xs_control & XS_CTL_DATA_IN) || 492 (xfer->c_flags & C_SENSE)) ? WDC_DMA_READ : 0; 493 again: 494 len = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo) + 495 256 * bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi); 496 ire = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_ireason); 497 phase = (ire & (WDCI_CMD | WDCI_IN)) | (chp->ch_status & WDCS_DRQ); 498 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d st 0x%x err 0x%x " 499 "ire 0x%x :", xfer->c_bcount, 500 len, chp->ch_status, chp->ch_error, ire), DEBUG_INTR); 501 502 switch (phase) { 503 case PHASE_CMDOUT: 504 if (xfer->c_flags & C_SENSE) { 505 memset(cmd_reqsense, 0, sizeof(struct scsipi_generic)); 506 cmd_reqsense->opcode = REQUEST_SENSE; 507 cmd_reqsense->length = xfer->c_bcount; 508 cmd = cmd_reqsense; 509 } else { 510 cmd = sc_xfer->cmd; 511 } 512 WDCDEBUG_PRINT(("PHASE_CMDOUT\n"), DEBUG_INTR); 513 /* Init the DMA channel if necessary */ 514 if (xfer->c_flags & C_DMA) { 515 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg, 516 chp->channel, xfer->drive, 517 xfer->databuf, xfer->c_bcount, dma_flags) != 0) { 518 sc_xfer->error = XS_DRIVER_STUFFUP; 519 break; 520 } 521 } 522 /* send packet command */ 523 /* Commands are 12 or 16 bytes long. It's 32-bit aligned */ 524 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) { 525 if (drvp->drive_flags & DRIVE_CAP32) { 526 bus_space_write_multi_4(chp->data32iot, 527 chp->data32ioh, 0, 528 (u_int32_t *)cmd, 529 sc_xfer->cmdlen >> 2); 530 } else { 531 bus_space_write_multi_2(chp->cmd_iot, 532 chp->cmd_ioh, wd_data, 533 (u_int16_t *)cmd, 534 sc_xfer->cmdlen >> 1); 535 } 536 } else { 537 if (drvp->drive_flags & DRIVE_CAP32) { 538 bus_space_write_multi_stream_4(chp->data32iot, 539 chp->data32ioh, 0, 540 (u_int32_t *)cmd, 541 sc_xfer->cmdlen >> 2); 542 } else { 543 bus_space_write_multi_stream_2(chp->cmd_iot, 544 chp->cmd_ioh, wd_data, 545 (u_int16_t *)cmd, 546 sc_xfer->cmdlen >> 1); 547 } 548 } 549 /* Start the DMA channel if necessary */ 550 if (xfer->c_flags & C_DMA) { 551 (*chp->wdc->dma_start)(chp->wdc->dma_arg, 552 chp->channel, xfer->drive); 553 chp->ch_flags |= WDCF_DMA_WAIT; 554 } 555 556 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 557 chp->ch_flags |= WDCF_IRQ_WAIT; 558 } 559 return 1; 560 561 case PHASE_DATAOUT: 562 /* write data */ 563 WDCDEBUG_PRINT(("PHASE_DATAOUT\n"), DEBUG_INTR); 564 if ((sc_xfer->xs_control & XS_CTL_DATA_OUT) == 0 || 565 (xfer->c_flags & C_DMA) != 0) { 566 printf("wdc_atapi_intr: bad data phase DATAOUT\n"); 567 if (xfer->c_flags & C_DMA) { 568 ata_dmaerr(drvp); 569 } 570 sc_xfer->error = XS_TIMEOUT; 571 wdc_atapi_reset(chp, xfer); 572 return 1; 573 } 574 if (xfer->c_bcount < len) { 575 printf("wdc_atapi_intr: warning: write only " 576 "%d of %d requested bytes\n", xfer->c_bcount, len); 577 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) { 578 bus_space_write_multi_2(chp->cmd_iot, 579 chp->cmd_ioh, wd_data, 580 (u_int16_t *)((char *)xfer->databuf + 581 xfer->c_skip), 582 xfer->c_bcount >> 1); 583 } else { 584 bus_space_write_multi_stream_2(chp->cmd_iot, 585 chp->cmd_ioh, wd_data, 586 (u_int16_t *)((char *)xfer->databuf + 587 xfer->c_skip), 588 xfer->c_bcount >> 1); 589 } 590 for (i = xfer->c_bcount; i < len; i += 2) 591 bus_space_write_2(chp->cmd_iot, chp->cmd_ioh, 592 wd_data, 0); 593 xfer->c_skip += xfer->c_bcount; 594 xfer->c_bcount = 0; 595 } else { 596 if (drvp->drive_flags & DRIVE_CAP32) { 597 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 598 bus_space_write_multi_4(chp->data32iot, 599 chp->data32ioh, 0, 600 (u_int32_t *)((char *)xfer->databuf + 601 xfer->c_skip), 602 len >> 2); 603 else 604 bus_space_write_multi_stream_4(chp->data32iot, 605 chp->data32ioh, wd_data, 606 (u_int32_t *)((char *)xfer->databuf + 607 xfer->c_skip), 608 len >> 2); 609 610 xfer->c_skip += len & 0xfffffffc; 611 xfer->c_bcount -= len & 0xfffffffc; 612 len = len & 0x03; 613 } 614 if (len > 0) { 615 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 616 bus_space_write_multi_2(chp->cmd_iot, 617 chp->cmd_ioh, wd_data, 618 (u_int16_t *)((char *)xfer->databuf + 619 xfer->c_skip), 620 len >> 1); 621 else 622 bus_space_write_multi_stream_2(chp->cmd_iot, 623 chp->cmd_ioh, wd_data, 624 (u_int16_t *)((char *)xfer->databuf + 625 xfer->c_skip), 626 len >> 1); 627 xfer->c_skip += len; 628 xfer->c_bcount -= len; 629 } 630 } 631 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 632 chp->ch_flags |= WDCF_IRQ_WAIT; 633 } 634 return 1; 635 636 case PHASE_DATAIN: 637 /* Read data */ 638 WDCDEBUG_PRINT(("PHASE_DATAIN\n"), DEBUG_INTR); 639 if (((sc_xfer->xs_control & XS_CTL_DATA_IN) == 0 && 640 (xfer->c_flags & C_SENSE) == 0) || 641 (xfer->c_flags & C_DMA) != 0) { 642 printf("wdc_atapi_intr: bad data phase DATAIN\n"); 643 if (xfer->c_flags & C_DMA) { 644 ata_dmaerr(drvp); 645 } 646 sc_xfer->error = XS_TIMEOUT; 647 wdc_atapi_reset(chp, xfer); 648 return 1; 649 } 650 if (xfer->c_bcount < len) { 651 printf("wdc_atapi_intr: warning: reading only " 652 "%d of %d bytes\n", xfer->c_bcount, len); 653 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) { 654 bus_space_read_multi_2(chp->cmd_iot, 655 chp->cmd_ioh, wd_data, 656 (u_int16_t *)((char *)xfer->databuf + 657 xfer->c_skip), 658 xfer->c_bcount >> 1); 659 } else { 660 bus_space_read_multi_stream_2(chp->cmd_iot, 661 chp->cmd_ioh, wd_data, 662 (u_int16_t *)((char *)xfer->databuf + 663 xfer->c_skip), 664 xfer->c_bcount >> 1); 665 } 666 wdcbit_bucket(chp, len - xfer->c_bcount); 667 xfer->c_skip += xfer->c_bcount; 668 xfer->c_bcount = 0; 669 } else { 670 if (drvp->drive_flags & DRIVE_CAP32) { 671 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 672 bus_space_read_multi_4(chp->data32iot, 673 chp->data32ioh, 0, 674 (u_int32_t *)((char *)xfer->databuf + 675 xfer->c_skip), 676 len >> 2); 677 else 678 bus_space_read_multi_stream_4(chp->data32iot, 679 chp->data32ioh, wd_data, 680 (u_int32_t *)((char *)xfer->databuf + 681 xfer->c_skip), 682 len >> 2); 683 684 xfer->c_skip += len & 0xfffffffc; 685 xfer->c_bcount -= len & 0xfffffffc; 686 len = len & 0x03; 687 } 688 if (len > 0) { 689 if ((chp->wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)) 690 bus_space_read_multi_2(chp->cmd_iot, 691 chp->cmd_ioh, wd_data, 692 (u_int16_t *)((char *)xfer->databuf + 693 xfer->c_skip), 694 len >> 1); 695 else 696 bus_space_read_multi_stream_2(chp->cmd_iot, 697 chp->cmd_ioh, wd_data, 698 (u_int16_t *)((char *)xfer->databuf + 699 xfer->c_skip), 700 len >> 1); 701 xfer->c_skip += len; 702 xfer->c_bcount -=len; 703 } 704 } 705 if ((sc_xfer->xs_control & XS_CTL_POLL) == 0) { 706 chp->ch_flags |= WDCF_IRQ_WAIT; 707 } 708 return 1; 709 710 case PHASE_ABORTED: 711 case PHASE_COMPLETED: 712 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR); 713 /* turn off DMA channel */ 714 if (xfer->c_flags & C_DMA) { 715 if (xfer->c_flags & C_SENSE) 716 xfer->c_bcount -= 717 sizeof(sc_xfer->sense.scsi_sense); 718 else 719 xfer->c_bcount -= sc_xfer->datalen; 720 } 721 if (xfer->c_flags & C_SENSE) { 722 if ((chp->ch_status & WDCS_ERR) || 723 (chp->wdc->dma_status & 724 (WDC_DMAST_NOIRQ | WDC_DMAST_ERR))) { 725 /* 726 * request sense failed ! it's not suppossed 727 * to be possible 728 */ 729 if (xfer->c_flags & C_DMA) { 730 ata_dmaerr(drvp); 731 } 732 sc_xfer->error = XS_RESET; 733 wdc_atapi_reset(chp, xfer); 734 return (1); 735 } else if (xfer->c_bcount < 736 sizeof(sc_xfer->sense.scsi_sense)) { 737 /* use the sense we just read */ 738 sc_xfer->error = XS_SENSE; 739 } else { 740 /* 741 * command completed, but no data was read. 742 * use the short sense we saved previsouly. 743 */ 744 sc_xfer->error = XS_SHORTSENSE; 745 } 746 } else { 747 sc_xfer->resid = xfer->c_bcount; 748 if (chp->ch_status & WDCS_ERR) { 749 /* save the short sense */ 750 sc_xfer->error = XS_SHORTSENSE; 751 sc_xfer->sense.atapi_sense = chp->ch_error; 752 if ((sc_xfer->sc_link->quirks & 753 ADEV_NOSENSE) == 0) { 754 /* 755 * let the driver issue a 756 * 'request sense' 757 */ 758 xfer->databuf = &sc_xfer->sense; 759 xfer->c_bcount = 760 sizeof(sc_xfer->sense.scsi_sense); 761 xfer->c_skip = 0; 762 xfer->c_flags |= C_SENSE; 763 callout_stop(&chp->ch_callout); 764 wdc_atapi_start(chp, xfer); 765 return 1; 766 } 767 } else if (chp->wdc->dma_status & 768 (WDC_DMAST_NOIRQ | WDC_DMAST_ERR)) { 769 ata_dmaerr(drvp); 770 sc_xfer->error = XS_RESET; 771 wdc_atapi_reset(chp, xfer); 772 return (1); 773 } 774 } 775 if (xfer->c_bcount != 0) { 776 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is " 777 "%d after io\n", xfer->c_bcount), DEBUG_XFERS); 778 } 779 #ifdef DIAGNOSTIC 780 if (xfer->c_bcount < 0) { 781 printf("wdc_atapi_intr warning: bcount value " 782 "is %d after io\n", xfer->c_bcount); 783 } 784 #endif 785 break; 786 787 default: 788 if (++retries<500) { 789 DELAY(100); 790 chp->ch_status = bus_space_read_1(chp->cmd_iot, 791 chp->cmd_ioh, wd_status); 792 chp->ch_error = bus_space_read_1(chp->cmd_iot, 793 chp->cmd_ioh, wd_error); 794 goto again; 795 } 796 printf("wdc_atapi_intr: unknown phase 0x%x\n", phase); 797 if (chp->ch_status & WDCS_ERR) { 798 sc_xfer->error = XS_SHORTSENSE; 799 sc_xfer->sense.atapi_sense = chp->ch_error; 800 } else { 801 if (xfer->c_flags & C_DMA) { 802 ata_dmaerr(drvp); 803 } 804 sc_xfer->error = XS_RESET; 805 wdc_atapi_reset(chp, xfer); 806 return (1); 807 } 808 } 809 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x " 810 "sense 0x%x\n", sc_xfer->error, sc_xfer->sense.atapi_sense), 811 DEBUG_INTR); 812 wdc_atapi_done(chp, xfer); 813 return (1); 814 } 815 816 int 817 wdc_atapi_ctrl(chp, xfer, irq) 818 struct channel_softc *chp; 819 struct wdc_xfer *xfer; 820 int irq; 821 { 822 struct scsipi_xfer *sc_xfer = xfer->cmd; 823 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 824 char *errstring = NULL; 825 int delay = (irq == 0) ? ATAPI_DELAY : 0; 826 827 /* Ack interrupt done in wait_for_unbusy */ 828 again: 829 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n", 830 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state), 831 DEBUG_INTR | DEBUG_FUNCS); 832 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 833 WDSD_IBM | (xfer->drive << 4)); 834 switch (drvp->state) { 835 case PIOMODE: 836 piomode: 837 /* Don't try to set mode if controller can't be adjusted */ 838 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0) 839 goto ready; 840 /* Also don't try if the drive didn't report its mode */ 841 if ((drvp->drive_flags & DRIVE_MODE) == 0) 842 goto ready;; 843 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 844 0x08 | drvp->PIO_mode, WDSF_SET_MODE); 845 drvp->state = PIOMODE_WAIT; 846 break; 847 case PIOMODE_WAIT: 848 errstring = "piomode"; 849 if (wait_for_unbusy(chp, delay)) 850 goto timeout; 851 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 852 chp->wdc->irqack(chp); 853 if (chp->ch_status & WDCS_ERR) { 854 if (drvp->PIO_mode < 3) { 855 drvp->PIO_mode = 3; 856 goto piomode; 857 } else { 858 goto error; 859 } 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_done(chp, xfer) 923 struct channel_softc *chp; 924 struct wdc_xfer *xfer; 925 { 926 struct scsipi_xfer *sc_xfer = xfer->cmd; 927 928 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x\n", 929 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 930 (u_int)xfer->c_flags), DEBUG_XFERS); 931 callout_stop(&chp->ch_callout); 932 /* remove this command from xfer queue */ 933 wdc_free_xfer(chp, xfer); 934 sc_xfer->xs_status |= XS_STS_DONE; 935 936 WDCDEBUG_PRINT(("wdc_atapi_done: scsipi_done\n"), DEBUG_XFERS); 937 scsipi_done(sc_xfer); 938 WDCDEBUG_PRINT(("wdcstart from wdc_atapi_done, flags 0x%x\n", 939 chp->ch_flags), DEBUG_XFERS); 940 wdcstart(chp); 941 } 942 943 void 944 wdc_atapi_reset(chp, xfer) 945 struct channel_softc *chp; 946 struct wdc_xfer *xfer; 947 { 948 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 949 struct scsipi_xfer *sc_xfer = xfer->cmd; 950 951 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET); 952 drvp->state = 0; 953 if (wait_for_unbusy(chp, WDC_RESET_WAIT) != 0) { 954 printf("%s:%d:%d: reset failed\n", 955 chp->wdc->sc_dev.dv_xname, chp->channel, 956 xfer->drive); 957 sc_xfer->error = XS_SELTIMEOUT; 958 } 959 wdc_atapi_done(chp, xfer); 960 return; 961 } 962