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