1 /* $OpenBSD: atapiscsi.c,v 1.102 2014/01/19 00:03:13 dlg Exp $ */ 2 3 /* 4 * This code is derived from code with the copyright below. 5 */ 6 7 /* 8 * Copyright (c) 1996, 1998 Manuel Bouyer. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/device.h> 38 #include <sys/buf.h> 39 #include <sys/disklabel.h> 40 #include <sys/malloc.h> 41 #include <sys/proc.h> 42 #include <sys/reboot.h> 43 #include <sys/file.h> 44 #include <sys/ioctl.h> 45 #include <sys/timeout.h> 46 #include <scsi/scsi_all.h> 47 #include <scsi/scsi_disk.h> 48 #include <scsi/scsi_tape.h> 49 #include <scsi/scsiconf.h> 50 51 #include <machine/bus.h> 52 #include <machine/cpu.h> 53 #include <machine/intr.h> 54 55 #include <dev/ata/atareg.h> 56 #include <dev/ata/atavar.h> 57 #include <dev/ic/wdcreg.h> 58 #include <dev/ic/wdcvar.h> 59 #include <dev/ic/wdcevent.h> 60 61 /* drive states stored in ata_drive_datas */ 62 enum atapi_drive_states { 63 ATAPI_RESET_BASE_STATE = 0, 64 ATAPI_DEVICE_RESET_WAIT_STATE = 1, 65 ATAPI_IDENTIFY_STATE = 2, 66 ATAPI_IDENTIFY_WAIT_STATE = 3, 67 ATAPI_PIOMODE_STATE = 4, 68 ATAPI_PIOMODE_WAIT_STATE = 5, 69 ATAPI_DMAMODE_STATE = 6, 70 ATAPI_DMAMODE_WAIT_STATE = 7, 71 ATAPI_READY_STATE = 8 72 }; 73 74 #define DEBUG_INTR 0x01 75 #define DEBUG_XFERS 0x02 76 #define DEBUG_STATUS 0x04 77 #define DEBUG_FUNCS 0x08 78 #define DEBUG_PROBE 0x10 79 #define DEBUG_DSC 0x20 80 #define DEBUG_POLL 0x40 81 #define DEBUG_ERRORS 0x80 /* Debug error handling code */ 82 83 #if defined(WDCDEBUG) 84 #ifndef WDCDEBUG_ATAPI_MASK 85 #define WDCDEBUG_ATAPI_MASK 0x00 86 #endif 87 int wdcdebug_atapi_mask = WDCDEBUG_ATAPI_MASK; 88 #define WDCDEBUG_PRINT(args, level) do { \ 89 if ((wdcdebug_atapi_mask & (level)) != 0) \ 90 printf args; \ 91 } while (0) 92 #else 93 #define WDCDEBUG_PRINT(args, level) 94 #endif 95 96 /* 10 ms, this is used only before sending a cmd. */ 97 #define ATAPI_DELAY 10 98 #define ATAPI_RESET_DELAY 1000 99 #define ATAPI_RESET_WAIT 2000 100 #define ATAPI_CTRL_WAIT 4000 101 102 /* When polling, let the exponential backoff max out at 1 second's interval. */ 103 #define ATAPI_POLL_MAXTIC (hz) 104 105 void wdc_atapi_start(struct channel_softc *,struct wdc_xfer *); 106 107 void wdc_atapi_timer_handler(void *); 108 109 void wdc_atapi_real_start(struct channel_softc *, struct wdc_xfer *, 110 int, struct atapi_return_args *); 111 void wdc_atapi_real_start_2(struct channel_softc *, struct wdc_xfer *, 112 int, struct atapi_return_args *); 113 void wdc_atapi_intr_command(struct channel_softc *, struct wdc_xfer *, 114 int, struct atapi_return_args *); 115 void wdc_atapi_intr_data(struct channel_softc *, struct wdc_xfer *, 116 int, struct atapi_return_args *); 117 void wdc_atapi_intr_complete(struct channel_softc *, struct wdc_xfer *, 118 int, struct atapi_return_args *); 119 void wdc_atapi_pio_intr(struct channel_softc *, struct wdc_xfer *, 120 int, struct atapi_return_args *); 121 void wdc_atapi_send_packet(struct channel_softc *, struct wdc_xfer *, 122 int, struct atapi_return_args *); 123 void wdc_atapi_ctrl(struct channel_softc *, struct wdc_xfer *, 124 int, struct atapi_return_args *); 125 126 char *wdc_atapi_in_data_phase(struct wdc_xfer *, int, int); 127 128 int wdc_atapi_intr(struct channel_softc *, struct wdc_xfer *, int); 129 void wdc_atapi_done(struct channel_softc *, struct wdc_xfer *, 130 int, struct atapi_return_args *); 131 void wdc_atapi_reset(struct channel_softc *, struct wdc_xfer *, 132 int, struct atapi_return_args *); 133 void wdc_atapi_reset_2(struct channel_softc *, struct wdc_xfer *, 134 int, struct atapi_return_args *); 135 136 void wdc_atapi_tape_done(struct channel_softc *, struct wdc_xfer *, 137 int, struct atapi_return_args *); 138 #define MAX_SIZE MAXPHYS 139 140 struct atapiscsi_softc; 141 struct atapiscsi_xfer; 142 143 int atapiscsi_match(struct device *, void *, void *); 144 void atapiscsi_attach(struct device *, struct device *, void *); 145 int atapiscsi_activate(struct device *, int); 146 int atapiscsi_detach(struct device *, int); 147 int atapi_to_scsi_sense(struct scsi_xfer *, u_int8_t); 148 149 enum atapi_state { as_none, as_data, as_completed }; 150 151 struct atapiscsi_softc { 152 struct device sc_dev; 153 struct scsi_link sc_adapterlink; 154 struct channel_softc *chp; 155 enum atapi_state protocol_phase; 156 157 int drive; 158 }; 159 160 void wdc_atapi_minphys(struct buf *bp, struct scsi_link *sl); 161 int wdc_atapi_ioctl(struct scsi_link *, u_long, caddr_t, int); 162 void wdc_atapi_send_cmd(struct scsi_xfer *sc_xfer); 163 164 static struct scsi_adapter atapiscsi_switch = 165 { 166 wdc_atapi_send_cmd, 167 wdc_atapi_minphys, 168 NULL, 169 NULL, 170 wdc_atapi_ioctl 171 }; 172 173 /* Inital version shares bus_link structure so it can easily 174 be "attached to current" wdc driver */ 175 176 struct cfattach atapiscsi_ca = { 177 sizeof(struct atapiscsi_softc), atapiscsi_match, atapiscsi_attach, 178 atapiscsi_detach, atapiscsi_activate 179 }; 180 181 struct cfdriver atapiscsi_cd = { 182 NULL, "atapiscsi", DV_DULL 183 }; 184 185 186 int 187 atapiscsi_match(struct device *parent, void *match, void *aux) 188 { 189 struct ata_atapi_attach *aa_link = aux; 190 struct cfdata *cf = match; 191 192 if (aa_link == NULL) 193 return (0); 194 195 if (aa_link->aa_type != T_ATAPI) 196 return (0); 197 198 if (cf->cf_loc[0] != aa_link->aa_channel && 199 cf->cf_loc[0] != -1) 200 return (0); 201 202 return (1); 203 } 204 205 void 206 atapiscsi_attach(struct device *parent, struct device *self, void *aux) 207 { 208 struct atapiscsi_softc *as = (struct atapiscsi_softc *)self; 209 struct ata_atapi_attach *aa_link = aux; 210 struct scsibus_attach_args saa; 211 struct ata_drive_datas *drvp = aa_link->aa_drv_data; 212 struct channel_softc *chp = drvp->chnl_softc; 213 struct ataparams *id = &drvp->id; 214 struct device *child; 215 216 extern struct scsi_iopool wdc_xfer_iopool; 217 218 printf("\n"); 219 220 /* Initialize shared data. */ 221 scsi_init(); 222 223 #ifdef WDCDEBUG 224 if (chp->wdc->sc_dev.dv_cfdata->cf_flags & WDC_OPTION_PROBE_VERBOSE) 225 wdcdebug_atapi_mask |= DEBUG_PROBE; 226 #endif 227 228 as->chp = chp; 229 as->drive = drvp->drive; 230 as->sc_adapterlink.adapter_softc = as; 231 as->sc_adapterlink.adapter_target = 7; 232 as->sc_adapterlink.adapter_buswidth = 2; 233 as->sc_adapterlink.adapter = &atapiscsi_switch; 234 as->sc_adapterlink.luns = 1; 235 as->sc_adapterlink.openings = 1; 236 as->sc_adapterlink.flags = SDEV_ATAPI; 237 as->sc_adapterlink.pool = &wdc_xfer_iopool; 238 239 strlcpy(drvp->drive_name, as->sc_dev.dv_xname, 240 sizeof(drvp->drive_name)); 241 drvp->cf_flags = as->sc_dev.dv_cfdata->cf_flags; 242 243 wdc_probe_caps(drvp, id); 244 245 WDCDEBUG_PRINT( 246 ("general config %04x capabilities %04x ", 247 id->atap_config, id->atap_capabilities1), 248 DEBUG_PROBE); 249 250 if ((NERRS_MAX - 2) > 0) 251 drvp->n_dmaerrs = NERRS_MAX - 2; 252 else 253 drvp->n_dmaerrs = 0; 254 drvp->drive_flags |= DRIVE_DEVICE_RESET; 255 256 /* Tape drives do funny DSC stuff */ 257 if (ATAPI_CFG_TYPE(id->atap_config) == 258 ATAPI_CFG_TYPE_SEQUENTIAL) 259 drvp->atapi_cap |= ACAP_DSC; 260 261 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == 262 ATAPI_CFG_CMD_16) 263 drvp->atapi_cap |= ACAP_LEN; 264 265 drvp->atapi_cap |= 266 (id->atap_config & ATAPI_CFG_DRQ_MASK); 267 268 WDCDEBUG_PRINT(("driver caps %04x\n", drvp->atapi_cap), 269 DEBUG_PROBE); 270 271 bzero(&saa, sizeof(saa)); 272 saa.saa_sc_link = &as->sc_adapterlink; 273 274 child = config_found((struct device *)as, &saa, scsiprint); 275 276 if (child != NULL) { 277 struct scsibus_softc *scsi = (struct scsibus_softc *)child; 278 struct scsi_link *link = scsi_get_link(scsi, 0, 0); 279 280 if (link) { 281 strlcpy(drvp->drive_name, 282 ((struct device *)(link->device_softc))->dv_xname, 283 sizeof(drvp->drive_name)); 284 285 wdc_print_caps(drvp); 286 } 287 } 288 289 #ifdef WDCDEBUG 290 if (chp->wdc->sc_dev.dv_cfdata->cf_flags & WDC_OPTION_PROBE_VERBOSE) 291 wdcdebug_atapi_mask &= ~DEBUG_PROBE; 292 #endif 293 } 294 295 int 296 atapiscsi_activate(struct device *self, int act) 297 { 298 struct atapiscsi_softc *as = (void *)self; 299 struct channel_softc *chp = as->chp; 300 struct ata_drive_datas *drvp = &chp->ch_drive[as->drive]; 301 302 switch (act) { 303 case DVACT_SUSPEND: 304 break; 305 case DVACT_RESUME: 306 /* 307 * Do two resets separated by a small delay. The 308 * first wakes the controller, the second resets 309 * the channel 310 */ 311 wdc_disable_intr(chp); 312 wdc_reset_channel(drvp, 1); 313 delay(10000); 314 wdc_reset_channel(drvp, 0); 315 wdc_enable_intr(chp); 316 break; 317 } 318 return (0); 319 } 320 321 int 322 atapiscsi_detach(struct device *dev, int flags) 323 { 324 return (config_detach_children(dev, flags)); 325 } 326 327 void 328 wdc_atapi_send_cmd(struct scsi_xfer *sc_xfer) 329 { 330 struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc; 331 struct channel_softc *chp = as->chp; 332 struct ata_drive_datas *drvp = &chp->ch_drive[as->drive]; 333 struct wdc_xfer *xfer; 334 int s; 335 int idx; 336 337 WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d start\n", 338 chp->wdc->sc_dev.dv_xname, chp->channel, as->drive), DEBUG_XFERS); 339 340 if (sc_xfer->sc_link->target != 0) { 341 sc_xfer->error = XS_DRIVER_STUFFUP; 342 scsi_done(sc_xfer); 343 return; 344 } 345 346 xfer = sc_xfer->io; 347 wdc_scrub_xfer(xfer); 348 if (sc_xfer->flags & SCSI_POLL) 349 xfer->c_flags |= C_POLL; 350 xfer->drive = as->drive; 351 xfer->c_flags |= C_ATAPI; 352 xfer->cmd = sc_xfer; 353 xfer->databuf = sc_xfer->data; 354 xfer->c_bcount = sc_xfer->datalen; 355 xfer->c_start = wdc_atapi_start; 356 xfer->c_intr = wdc_atapi_intr; 357 358 timeout_set(&xfer->atapi_poll_to, wdc_atapi_timer_handler, chp); 359 360 WDCDEBUG_PRINT(("wdc_atapi_send_cmd %s:%d:%d ", 361 chp->wdc->sc_dev.dv_xname, chp->channel, as->drive), 362 DEBUG_XFERS | DEBUG_ERRORS); 363 364 for (idx = 0; idx < sc_xfer->cmdlen; idx++) { 365 WDCDEBUG_PRINT((" %02x", 366 ((unsigned char *)sc_xfer->cmd)[idx]), 367 DEBUG_XFERS | DEBUG_ERRORS); 368 } 369 WDCDEBUG_PRINT(("\n"), DEBUG_XFERS | DEBUG_ERRORS); 370 371 s = splbio(); 372 373 if (drvp->atapi_cap & ACAP_DSC) { 374 WDCDEBUG_PRINT(("about to send cmd 0x%x ", 375 sc_xfer->cmd->opcode), DEBUG_DSC); 376 switch (sc_xfer->cmd->opcode) { 377 case READ: 378 case WRITE: 379 xfer->c_flags |= C_MEDIA_ACCESS; 380 381 /* If we are not in buffer availability mode, 382 we limit the first request to 0 bytes, which 383 gets us into buffer availability mode without 384 holding the bus. */ 385 if (!(drvp->drive_flags & DRIVE_DSCBA)) { 386 xfer->c_bcount = 0; 387 xfer->transfer_len = 388 _3btol(((struct scsi_rw_tape *) 389 sc_xfer->cmd)->len); 390 _lto3b(0, 391 ((struct scsi_rw_tape *) 392 sc_xfer->cmd)->len); 393 xfer->c_done = wdc_atapi_tape_done; 394 WDCDEBUG_PRINT( 395 ("R/W in completion mode, do 0 blocks\n"), 396 DEBUG_DSC); 397 } else 398 WDCDEBUG_PRINT(("R/W %d blocks %d bytes\n", 399 _3btol(((struct scsi_rw_tape *) 400 sc_xfer->cmd)->len), 401 sc_xfer->datalen), 402 DEBUG_DSC); 403 404 /* DSC will change to buffer availability mode. 405 We reflect this in wdc_atapi_intr. */ 406 break; 407 408 case ERASE: /* Media access commands */ 409 case LOAD: 410 case REWIND: 411 case SPACE: 412 case WRITE_FILEMARKS: 413 #if 0 414 case LOCATE: 415 case READ_POSITION: 416 #endif 417 418 xfer->c_flags |= C_MEDIA_ACCESS; 419 break; 420 421 default: 422 WDCDEBUG_PRINT(("no media access\n"), DEBUG_DSC); 423 } 424 } 425 426 wdc_exec_xfer(chp, xfer); 427 splx(s); 428 } 429 430 void 431 wdc_atapi_minphys (struct buf *bp, struct scsi_link *sl) 432 { 433 if (bp->b_bcount > MAX_SIZE) 434 bp->b_bcount = MAX_SIZE; 435 minphys(bp); 436 } 437 438 int 439 wdc_atapi_ioctl (struct scsi_link *sc_link, u_long cmd, caddr_t addr, int flag) 440 { 441 struct atapiscsi_softc *as = sc_link->adapter_softc; 442 struct channel_softc *chp = as->chp; 443 struct ata_drive_datas *drvp = &chp->ch_drive[as->drive]; 444 445 if (sc_link->target != 0) 446 return ENOTTY; 447 448 return (wdc_ioctl(drvp, cmd, addr, flag, curproc)); 449 } 450 451 452 /* 453 * Returns 1 if we experienced an ATA-level abort command 454 * (ABRT bit set but no additional sense) 455 * 0 if normal command processing 456 */ 457 int 458 atapi_to_scsi_sense(struct scsi_xfer *xfer, u_int8_t flags) 459 { 460 struct scsi_sense_data *sense = &xfer->sense; 461 int ret = 0; 462 463 xfer->error = XS_SHORTSENSE; 464 465 sense->error_code = SSD_ERRCODE_VALID | SSD_ERRCODE_CURRENT; 466 sense->flags = (flags >> 4); 467 468 WDCDEBUG_PRINT(("Atapi error: %d ", (flags >> 4)), DEBUG_ERRORS); 469 470 if ((flags & 4) && (sense->flags == 0)) { 471 sense->flags = SKEY_ABORTED_COMMAND; 472 WDCDEBUG_PRINT(("ABRT "), DEBUG_ERRORS); 473 ret = 1; 474 } 475 476 if (flags & 0x1) { 477 sense->flags |= SSD_ILI; 478 WDCDEBUG_PRINT(("ILI "), DEBUG_ERRORS); 479 } 480 481 if (flags & 0x2) { 482 sense->flags |= SSD_EOM; 483 WDCDEBUG_PRINT(("EOM "), DEBUG_ERRORS); 484 } 485 486 /* Media change requested */ 487 /* Let's ignore these in version 1 */ 488 if (flags & 0x8) { 489 WDCDEBUG_PRINT(("MCR "), DEBUG_ERRORS); 490 if (sense->flags == 0) 491 xfer->error = XS_NOERROR; 492 } 493 494 WDCDEBUG_PRINT(("\n"), DEBUG_ERRORS); 495 return (ret); 496 } 497 498 int wdc_atapi_drive_selected(struct channel_softc *, int); 499 500 int 501 wdc_atapi_drive_selected(struct channel_softc *chp, int drive) 502 { 503 u_int8_t reg = CHP_READ_REG(chp, wdr_sdh); 504 505 WDC_LOG_REG(chp, wdr_sdh, reg); 506 507 return ((reg & 0x10) == (drive << 4)); 508 } 509 510 enum atapi_context { 511 ctxt_process = 0, 512 ctxt_timer = 1, 513 ctxt_interrupt = 2 514 }; 515 516 void wdc_atapi_the_machine(struct channel_softc *, struct wdc_xfer *, 517 enum atapi_context); 518 519 void wdc_atapi_the_poll_machine(struct channel_softc *, struct wdc_xfer *); 520 521 void 522 wdc_atapi_start(struct channel_softc *chp, struct wdc_xfer *xfer) 523 { 524 xfer->next = wdc_atapi_real_start; 525 526 wdc_atapi_the_machine(chp, xfer, ctxt_process); 527 } 528 529 530 void 531 wdc_atapi_timer_handler(void *arg) 532 { 533 struct channel_softc *chp = arg; 534 struct wdc_xfer *xfer; 535 int s; 536 537 s = splbio(); 538 xfer = TAILQ_FIRST(&chp->ch_queue->sc_xfer); 539 if (xfer == NULL || 540 !timeout_triggered(&xfer->atapi_poll_to)) { 541 splx(s); 542 return; 543 } 544 xfer->c_flags &= ~C_POLL_MACHINE; 545 timeout_del(&xfer->atapi_poll_to); 546 chp->ch_flags &= ~WDCF_IRQ_WAIT; 547 wdc_atapi_the_machine(chp, xfer, ctxt_timer); 548 splx(s); 549 } 550 551 552 int 553 wdc_atapi_intr(struct channel_softc *chp, struct wdc_xfer *xfer, int irq) 554 { 555 timeout_del(&chp->ch_timo); 556 557 /* XXX we should consider an alternate signaling regime here */ 558 if (xfer->c_flags & C_TIMEOU) { 559 xfer->c_flags &= ~C_TIMEOU; 560 wdc_atapi_the_machine(chp, xfer, ctxt_timer); 561 return (0); 562 } 563 564 wdc_atapi_the_machine(chp, xfer, ctxt_interrupt); 565 566 return (-1); 567 } 568 569 struct atapi_return_args { 570 int timeout; 571 int delay; 572 int expect_irq; 573 }; 574 575 #define ARGS_INIT {-1, 0, 0} 576 577 void 578 wdc_atapi_the_poll_machine(struct channel_softc *chp, struct wdc_xfer *xfer) 579 { 580 int idx = 0; 581 int current_timeout = 10; 582 583 584 while (1) { 585 struct atapi_return_args retargs = ARGS_INIT; 586 idx++; 587 588 (xfer->next)(chp, xfer, (current_timeout * 1000 <= idx), 589 &retargs); 590 591 if (xfer->next == NULL) { 592 wdc_free_xfer(chp, xfer); 593 wdcstart(chp); 594 return; 595 } 596 597 if (retargs.timeout != -1) { 598 current_timeout = retargs.timeout; 599 idx = 0; 600 } 601 602 if (retargs.delay != 0) { 603 delay (1000 * retargs.delay); 604 idx += 1000 * retargs.delay; 605 } 606 607 DELAY(1); 608 } 609 } 610 611 612 void 613 wdc_atapi_the_machine(struct channel_softc *chp, struct wdc_xfer *xfer, 614 enum atapi_context ctxt) 615 { 616 int idx = 0; 617 extern int ticks; 618 int timeout_delay = hz / 10; 619 620 if (xfer->c_flags & C_POLL) { 621 wdc_disable_intr(chp); 622 623 if (ctxt != ctxt_process) { 624 if (ctxt == ctxt_interrupt) 625 xfer->endticks = 1; 626 627 return; 628 } 629 630 wdc_atapi_the_poll_machine(chp, xfer); 631 return; 632 } 633 634 /* Don't go through more than 50 state machine steps 635 before yielding. This tries to limit the amount of time 636 spent at high SPL */ 637 for (idx = 0; idx < 50; idx++) { 638 struct atapi_return_args retargs = ARGS_INIT; 639 640 (xfer->next)(chp, xfer, 641 xfer->endticks && (ticks - xfer->endticks >= 0), 642 &retargs); 643 644 if (retargs.timeout != -1) 645 /* 646 * Add 1 tick to compensate for the fact that we 647 * can be just microseconds before the tick changes. 648 */ 649 xfer->endticks = 650 max((retargs.timeout * hz) / 1000, 1) + 1 + ticks; 651 652 if (xfer->next == NULL) { 653 if (xfer->c_flags & C_POLL_MACHINE) 654 timeout_del(&xfer->atapi_poll_to); 655 656 wdc_free_xfer(chp, xfer); 657 wdcstart(chp); 658 659 return; 660 } 661 662 if (retargs.expect_irq) { 663 int timeout_period; 664 chp->ch_flags |= WDCF_IRQ_WAIT; 665 timeout_period = xfer->endticks - ticks; 666 if (timeout_period < 1) 667 timeout_period = 1; 668 timeout_add(&chp->ch_timo, timeout_period); 669 return; 670 } 671 672 if (retargs.delay != 0) { 673 timeout_delay = max(retargs.delay * hz / 1000, 1); 674 break; 675 } 676 677 DELAY(1); 678 } 679 680 timeout_add(&xfer->atapi_poll_to, timeout_delay); 681 xfer->c_flags |= C_POLL_MACHINE; 682 683 return; 684 } 685 686 687 void wdc_atapi_update_status(struct channel_softc *); 688 689 void 690 wdc_atapi_update_status(struct channel_softc *chp) 691 { 692 chp->ch_status = CHP_READ_REG(chp, wdr_status); 693 694 WDC_LOG_STATUS(chp, chp->ch_status); 695 696 if (chp->ch_status == 0xff && (chp->ch_flags & WDCF_ONESLAVE)) { 697 wdc_set_drive(chp, 1); 698 699 chp->ch_status = CHP_READ_REG(chp, wdr_status); 700 WDC_LOG_STATUS(chp, chp->ch_status); 701 } 702 703 if ((chp->ch_status & (WDCS_BSY | WDCS_ERR)) == WDCS_ERR) { 704 chp->ch_error = CHP_READ_REG(chp, wdr_error); 705 WDC_LOG_ERROR(chp, chp->ch_error); 706 } 707 } 708 709 void 710 wdc_atapi_real_start(struct channel_softc *chp, struct wdc_xfer *xfer, 711 int timeout, struct atapi_return_args *ret) 712 { 713 #ifdef WDCDEBUG 714 struct scsi_xfer *sc_xfer = xfer->cmd; 715 #endif 716 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 717 718 /* 719 * Only set the DMA flag if the transfer is reasonably large. 720 * At least one older drive failed to complete a 4 byte DMA transfer. 721 */ 722 723 /* Turn off DMA flag on REQUEST SENSE */ 724 725 if (!(xfer->c_flags & (C_POLL | C_SENSE | C_MEDIA_ACCESS)) && 726 (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) && 727 (xfer->c_bcount > 100)) 728 xfer->c_flags |= C_DMA; 729 else 730 xfer->c_flags &= ~C_DMA; 731 732 733 wdc_set_drive(chp, xfer->drive); 734 735 DELAY(1); 736 737 xfer->next = wdc_atapi_real_start_2; 738 ret->timeout = ATAPI_DELAY; 739 740 WDCDEBUG_PRINT(("wdc_atapi_start %s:%d:%d, scsi flags 0x%x, " 741 "ATA flags 0x%x\n", 742 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, 743 sc_xfer->flags, xfer->c_flags), DEBUG_XFERS); 744 745 746 return; 747 } 748 749 750 void 751 wdc_atapi_real_start_2(struct channel_softc *chp, struct wdc_xfer *xfer, 752 int timeout, struct atapi_return_args *ret) 753 { 754 struct scsi_xfer *sc_xfer = xfer->cmd; 755 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 756 757 if (timeout) { 758 printf("wdc_atapi_start: not ready, st = %02x\n", 759 chp->ch_status); 760 761 sc_xfer->error = XS_TIMEOUT; 762 xfer->next = wdc_atapi_reset; 763 return; 764 } else { 765 wdc_atapi_update_status(chp); 766 767 if (chp->ch_status & (WDCS_BSY | WDCS_DRQ)) 768 return; 769 } 770 771 /* Do control operations specially. */ 772 if (drvp->state < ATAPI_READY_STATE) { 773 xfer->next = wdc_atapi_ctrl; 774 return; 775 } 776 777 xfer->next = wdc_atapi_send_packet; 778 return; 779 } 780 781 782 void 783 wdc_atapi_send_packet(struct channel_softc *chp, struct wdc_xfer *xfer, 784 int timeout, struct atapi_return_args *ret) 785 { 786 struct scsi_xfer *sc_xfer = xfer->cmd; 787 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 788 789 /* 790 * Even with WDCS_ERR, the device should accept a command packet. 791 * Limit length to what can be stuffed into the cylinder register 792 * (16 bits). Some CD-ROMs seem to interpret '0' as 65536, 793 * but not all devices do that and it's not obvious from the 794 * ATAPI spec that this behaviour should be expected. If more 795 * data is necessary, multiple data transfer phases will be done. 796 */ 797 798 wdccommand(chp, xfer->drive, ATAPI_PKT_CMD, 799 xfer->c_bcount <= 0xfffe ? xfer->c_bcount : 0xfffe, 800 0, 0, 0, 801 (xfer->c_flags & C_DMA) ? ATAPI_PKT_CMD_FTRE_DMA : 0); 802 803 if (xfer->c_flags & C_DMA) 804 drvp->n_xfers++; 805 806 DELAY(1); 807 808 xfer->next = wdc_atapi_intr_command; 809 ret->timeout = sc_xfer->timeout; 810 811 if ((drvp->atapi_cap & ATAPI_CFG_DRQ_MASK) == ATAPI_CFG_IRQ_DRQ) { 812 /* We expect an IRQ to tell us of the next state */ 813 ret->expect_irq = 1; 814 } 815 816 WDCDEBUG_PRINT(("wdc_atapi_send_packet %s:%d:%d command sent\n", 817 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive 818 ), DEBUG_XFERS); 819 return; 820 } 821 822 void 823 wdc_atapi_intr_command(struct channel_softc *chp, struct wdc_xfer *xfer, 824 int timeout, struct atapi_return_args *ret) 825 { 826 struct scsi_xfer *sc_xfer = xfer->cmd; 827 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 828 struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc; 829 int i; 830 u_int8_t cmd[16]; 831 struct scsi_sense *cmd_reqsense; 832 int cmdlen = (drvp->atapi_cap & ACAP_LEN) ? 16 : 12; 833 int dma_flags = ((sc_xfer->flags & SCSI_DATA_IN) || 834 (xfer->c_flags & C_SENSE)) ? WDC_DMA_READ : 0; 835 836 wdc_atapi_update_status(chp); 837 838 if ((chp->ch_status & WDCS_BSY) || !(chp->ch_status & WDCS_DRQ)) { 839 if (timeout) 840 goto timeout; 841 842 return; 843 } 844 845 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 846 chp->wdc->irqack(chp); 847 848 bzero(cmd, sizeof(cmd)); 849 850 if (xfer->c_flags & C_SENSE) { 851 cmd_reqsense = (struct scsi_sense *)&cmd[0]; 852 cmd_reqsense->opcode = REQUEST_SENSE; 853 cmd_reqsense->length = xfer->c_bcount; 854 } else 855 bcopy(sc_xfer->cmd, cmd, sc_xfer->cmdlen); 856 857 WDC_LOG_ATAPI_CMD(chp, xfer->drive, xfer->c_flags, 858 cmdlen, cmd); 859 860 for (i = 0; i < 12; i++) 861 WDCDEBUG_PRINT(("%02x ", cmd[i]), DEBUG_INTR); 862 WDCDEBUG_PRINT((": PHASE_CMDOUT\n"), DEBUG_INTR); 863 864 /* Init the DMA channel if necessary */ 865 if (xfer->c_flags & C_DMA) { 866 if ((*chp->wdc->dma_init)(chp->wdc->dma_arg, 867 chp->channel, xfer->drive, xfer->databuf, 868 xfer->c_bcount, dma_flags) != 0) { 869 sc_xfer->error = XS_DRIVER_STUFFUP; 870 871 xfer->next = wdc_atapi_reset; 872 return; 873 } 874 } 875 876 wdc_output_bytes(drvp, cmd, cmdlen); 877 878 /* Start the DMA channel if necessary */ 879 if (xfer->c_flags & C_DMA) { 880 (*chp->wdc->dma_start)(chp->wdc->dma_arg, 881 chp->channel, xfer->drive); 882 xfer->next = wdc_atapi_intr_complete; 883 } else { 884 if (xfer->c_bcount == 0) 885 as->protocol_phase = as_completed; 886 else 887 as->protocol_phase = as_data; 888 889 xfer->next = wdc_atapi_pio_intr; 890 } 891 892 ret->expect_irq = 1; 893 894 /* If we read/write to a tape we will get into buffer 895 availability mode. */ 896 if (drvp->atapi_cap & ACAP_DSC) { 897 if ((sc_xfer->cmd->opcode == READ || 898 sc_xfer->cmd->opcode == WRITE)) { 899 drvp->drive_flags |= DRIVE_DSCBA; 900 WDCDEBUG_PRINT(("set DSCBA\n"), DEBUG_DSC); 901 } else if ((xfer->c_flags & C_MEDIA_ACCESS) && 902 (drvp->drive_flags & DRIVE_DSCBA)) { 903 /* Clause 3.2.4 of QIC-157 D. 904 905 Any media access command other than read or 906 write will switch DSC back to completion 907 mode */ 908 drvp->drive_flags &= ~DRIVE_DSCBA; 909 WDCDEBUG_PRINT(("clear DCSBA\n"), DEBUG_DSC); 910 } 911 } 912 913 return; 914 915 timeout: 916 printf ("%s:%d:%d: device timeout waiting to send SCSI packet\n", 917 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive); 918 919 sc_xfer->error = XS_TIMEOUT; 920 xfer->next = wdc_atapi_reset; 921 return; 922 } 923 924 925 char * 926 wdc_atapi_in_data_phase(struct wdc_xfer *xfer, int len, int ire) 927 { 928 struct scsi_xfer *sc_xfer = xfer->cmd; 929 struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc; 930 char *message; 931 932 if (as->protocol_phase != as_data) { 933 message = "unexpected data phase"; 934 goto unexpected_state; 935 } 936 937 if (ire & WDCI_CMD) { 938 message = "unexpectedly in command phase"; 939 goto unexpected_state; 940 } 941 942 if (!(xfer->c_flags & C_SENSE)) { 943 if (!(sc_xfer->flags & (SCSI_DATA_IN | SCSI_DATA_OUT))) { 944 message = "data phase where none expected"; 945 goto unexpected_state; 946 } 947 948 /* Make sure polarities match */ 949 if (((ire & WDCI_IN) == WDCI_IN) == 950 ((sc_xfer->flags & SCSI_DATA_OUT) == SCSI_DATA_OUT)) { 951 message = "data transfer direction disagreement"; 952 goto unexpected_state; 953 } 954 } else { 955 if (!(ire & WDCI_IN)) { 956 message = "data transfer direction disagreement during sense"; 957 goto unexpected_state; 958 } 959 } 960 961 if (len == 0) { 962 message = "zero length transfer requested in data phase"; 963 goto unexpected_state; 964 } 965 966 967 return (0); 968 969 unexpected_state: 970 971 return (message); 972 } 973 974 void 975 wdc_atapi_intr_data(struct channel_softc *chp, struct wdc_xfer *xfer, 976 int timeout, struct atapi_return_args *ret) 977 { 978 struct scsi_xfer *sc_xfer = xfer->cmd; 979 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 980 int len, ire; 981 char *message; 982 int tohost; 983 984 len = (CHP_READ_REG(chp, wdr_cyl_hi) << 8) | 985 CHP_READ_REG(chp, wdr_cyl_lo); 986 WDC_LOG_REG(chp, wdr_cyl_lo, len); 987 988 ire = CHP_READ_REG(chp, wdr_ireason); 989 WDC_LOG_REG(chp, wdr_ireason, ire); 990 991 if ((message = wdc_atapi_in_data_phase(xfer, len, ire))) { 992 /* The drive has dropped BSY before setting up the 993 registers correctly for DATA phase. This drive is 994 not compliant with ATA/ATAPI-4. 995 996 Give the drive 100ms to get its house in order 997 before we try again. */ 998 WDCDEBUG_PRINT(("wdc_atapi_intr: %s\n", message), 999 DEBUG_ERRORS); 1000 1001 if (!timeout) { 1002 ret->delay = 100; 1003 return; 1004 } 1005 } 1006 1007 tohost = ((sc_xfer->flags & SCSI_DATA_IN) != 0 || 1008 (xfer->c_flags & C_SENSE) != 0); 1009 1010 if (xfer->c_bcount >= len) { 1011 WDCDEBUG_PRINT(("wdc_atapi_intr: c_bcount %d len %d " 1012 "st 0x%b err 0x%x " 1013 "ire 0x%x\n", xfer->c_bcount, 1014 len, chp->ch_status, WDCS_BITS, chp->ch_error, ire), 1015 DEBUG_INTR); 1016 1017 /* Common case */ 1018 if (!tohost) 1019 wdc_output_bytes(drvp, (u_int8_t *)xfer->databuf + 1020 xfer->c_skip, len); 1021 else 1022 wdc_input_bytes(drvp, (u_int8_t *)xfer->databuf + 1023 xfer->c_skip, len); 1024 1025 xfer->c_skip += len; 1026 xfer->c_bcount -= len; 1027 } else { 1028 /* Exceptional case - drive want to transfer more 1029 data than we have buffer for */ 1030 if (!tohost) { 1031 /* Wouldn't it be better to just abort here rather 1032 than to write random stuff to drive? */ 1033 printf("wdc_atapi_intr: warning: device requesting " 1034 "%d bytes, only %d left in buffer\n", len, xfer->c_bcount); 1035 1036 wdc_output_bytes(drvp, (u_int8_t *)xfer->databuf + 1037 xfer->c_skip, xfer->c_bcount); 1038 1039 CHP_WRITE_RAW_MULTI_2(chp, NULL, 1040 len - xfer->c_bcount); 1041 } else { 1042 printf("wdc_atapi_intr: warning: reading only " 1043 "%d of %d bytes\n", xfer->c_bcount, len); 1044 1045 wdc_input_bytes(drvp, 1046 (char *)xfer->databuf + xfer->c_skip, 1047 xfer->c_bcount); 1048 wdcbit_bucket(chp, len - xfer->c_bcount); 1049 } 1050 1051 xfer->c_skip += xfer->c_bcount; 1052 xfer->c_bcount = 0; 1053 } 1054 1055 ret->expect_irq = 1; 1056 xfer->next = wdc_atapi_pio_intr; 1057 1058 return; 1059 } 1060 1061 void 1062 wdc_atapi_intr_complete(struct channel_softc *chp, struct wdc_xfer *xfer, 1063 int timeout, struct atapi_return_args *ret) 1064 { 1065 struct scsi_xfer *sc_xfer = xfer->cmd; 1066 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 1067 struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc; 1068 1069 WDCDEBUG_PRINT(("PHASE_COMPLETED\n"), DEBUG_INTR); 1070 1071 if (xfer->c_flags & C_DMA) { 1072 int retry; 1073 1074 if (timeout) { 1075 sc_xfer->error = XS_TIMEOUT; 1076 ata_dmaerr(drvp); 1077 1078 xfer->next = wdc_atapi_reset; 1079 return; 1080 } 1081 1082 for (retry = 5; retry > 0; retry--) { 1083 wdc_atapi_update_status(chp); 1084 if ((chp->ch_status & (WDCS_BSY | WDCS_DRQ)) == 0) 1085 break; 1086 DELAY(5); 1087 } 1088 if (retry == 0) { 1089 ret->expect_irq = 1; 1090 return; 1091 } 1092 1093 chp->wdc->dma_status = 1094 (*chp->wdc->dma_finish) 1095 (chp->wdc->dma_arg, chp->channel, 1096 xfer->drive, 1); 1097 1098 if (chp->wdc->dma_status & WDC_DMAST_UNDER) 1099 xfer->c_bcount = 1; 1100 else 1101 xfer->c_bcount = 0; 1102 } 1103 1104 as->protocol_phase = as_none; 1105 1106 if (xfer->c_flags & C_SENSE) { 1107 if (chp->ch_status & WDCS_ERR) { 1108 if (chp->ch_error & WDCE_ABRT) { 1109 WDCDEBUG_PRINT(("wdc_atapi_intr: request_sense aborted, " 1110 "calling wdc_atapi_done()" 1111 ), DEBUG_INTR); 1112 xfer->next = wdc_atapi_done; 1113 return; 1114 } 1115 1116 /* 1117 * request sense failed ! it's not supposed 1118 * to be possible 1119 */ 1120 sc_xfer->error = XS_SHORTSENSE; 1121 } else if (xfer->c_bcount < sizeof(sc_xfer->sense)) { 1122 /* use the sense we just read */ 1123 sc_xfer->error = XS_SENSE; 1124 } else { 1125 /* 1126 * command completed, but no data was read. 1127 * use the short sense we saved previously. 1128 */ 1129 sc_xfer->error = XS_SHORTSENSE; 1130 } 1131 } else { 1132 sc_xfer->resid = xfer->c_bcount; 1133 if (chp->ch_status & WDCS_ERR) { 1134 if (!atapi_to_scsi_sense(sc_xfer, chp->ch_error) && 1135 (sc_xfer->sc_link->quirks & 1136 ADEV_NOSENSE) == 0) { 1137 /* 1138 * let the driver issue a 1139 * 'request sense' 1140 */ 1141 xfer->databuf = &sc_xfer->sense; 1142 xfer->c_bcount = sizeof(sc_xfer->sense); 1143 xfer->c_skip = 0; 1144 xfer->c_done = NULL; 1145 xfer->c_flags |= C_SENSE; 1146 xfer->next = wdc_atapi_real_start; 1147 return; 1148 } 1149 } 1150 } 1151 1152 if ((xfer->c_flags & C_DMA) && 1153 (chp->wdc->dma_status & ~WDC_DMAST_UNDER)) { 1154 ata_dmaerr(drvp); 1155 sc_xfer->error = XS_RESET; 1156 1157 xfer->next = wdc_atapi_reset; 1158 return; 1159 } 1160 1161 1162 if (xfer->c_bcount != 0) { 1163 WDCDEBUG_PRINT(("wdc_atapi_intr: bcount value is " 1164 "%d after io\n", xfer->c_bcount), DEBUG_XFERS); 1165 } 1166 #ifdef DIAGNOSTIC 1167 if (xfer->c_bcount < 0) { 1168 printf("wdc_atapi_intr warning: bcount value " 1169 "is %d after io\n", xfer->c_bcount); 1170 } 1171 #endif 1172 1173 WDCDEBUG_PRINT(("wdc_atapi_intr: wdc_atapi_done() (end), error 0x%x " 1174 "\n", sc_xfer->error), 1175 DEBUG_INTR); 1176 1177 1178 if (xfer->c_done) 1179 xfer->next = xfer->c_done; 1180 else 1181 xfer->next = wdc_atapi_done; 1182 1183 return; 1184 } 1185 1186 void 1187 wdc_atapi_pio_intr(struct channel_softc *chp, struct wdc_xfer *xfer, 1188 int timeout, struct atapi_return_args *ret) 1189 { 1190 struct scsi_xfer *sc_xfer = xfer->cmd; 1191 struct atapiscsi_softc *as = sc_xfer->sc_link->adapter_softc; 1192 u_int8_t ireason; 1193 1194 wdc_atapi_update_status(chp); 1195 1196 if (chp->ch_status & WDCS_BSY) { 1197 if (timeout) 1198 goto timeout; 1199 1200 return; 1201 } 1202 1203 if (!wdc_atapi_drive_selected(chp, xfer->drive)) { 1204 WDCDEBUG_PRINT(("wdc_atapi_intr_for_us: wrong drive selected\n"), DEBUG_INTR); 1205 wdc_set_drive(chp, xfer->drive); 1206 delay (1); 1207 1208 if (!timeout) 1209 return; 1210 } 1211 1212 if ((xfer->c_flags & C_MEDIA_ACCESS) && 1213 !(chp->ch_status & (WDCS_DSC | WDCS_DRQ))) { 1214 if (timeout) 1215 goto timeout; 1216 1217 ret->delay = 100; 1218 return; 1219 } 1220 1221 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 1222 chp->wdc->irqack(chp); 1223 1224 ireason = CHP_READ_REG(chp, wdr_ireason); 1225 WDC_LOG_REG(chp, wdr_ireason, ireason); 1226 1227 WDCDEBUG_PRINT(("Phase %d, (0x%b, 0x%x) ", as->protocol_phase, 1228 chp->ch_status, WDCS_BITS, ireason), DEBUG_INTR ); 1229 1230 switch (as->protocol_phase) { 1231 case as_data: 1232 if ((chp->ch_status & WDCS_DRQ) || 1233 (ireason & 3) != 3) { 1234 if (timeout) 1235 goto timeout; 1236 1237 wdc_atapi_intr_data(chp, xfer, timeout, ret); 1238 return; 1239 } 1240 1241 case as_completed: 1242 if ((chp->ch_status & WDCS_DRQ) || 1243 (ireason & 3) != 3) { 1244 if (timeout) 1245 goto timeout; 1246 1247 ret->delay = 100; 1248 return; 1249 } 1250 1251 wdc_atapi_intr_complete(chp, xfer, timeout, ret); 1252 return; 1253 1254 default: 1255 printf ("atapiscsi: Shouldn't get here\n"); 1256 sc_xfer->error = XS_DRIVER_STUFFUP; 1257 xfer->next = wdc_atapi_reset; 1258 return; 1259 } 1260 1261 return; 1262 timeout: 1263 ireason = CHP_READ_REG(chp, wdr_ireason); 1264 WDC_LOG_REG(chp, wdr_ireason, ireason); 1265 1266 printf("%s:%d:%d: device timeout, c_bcount=%d, c_skip=%d, " 1267 "status=0x%b, ireason=0x%x\n", 1268 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 1269 xfer->c_bcount, xfer->c_skip, chp->ch_status, WDCS_BITS, ireason); 1270 1271 sc_xfer->error = XS_TIMEOUT; 1272 xfer->next = wdc_atapi_reset; 1273 return; 1274 } 1275 1276 void 1277 wdc_atapi_ctrl(struct channel_softc *chp, struct wdc_xfer *xfer, 1278 int timeout, struct atapi_return_args *ret) 1279 { 1280 struct scsi_xfer *sc_xfer = xfer->cmd; 1281 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 1282 char *errstring = NULL; 1283 1284 wdc_atapi_update_status(chp); 1285 1286 if (!timeout) { 1287 switch (drvp->state) { 1288 case ATAPI_IDENTIFY_WAIT_STATE: 1289 if (chp->ch_status & WDCS_BSY) 1290 return; 1291 break; 1292 default: 1293 if (chp->ch_status & (WDCS_BSY | WDCS_DRQ)) 1294 return; 1295 break; 1296 } 1297 } 1298 1299 if (!wdc_atapi_drive_selected(chp, xfer->drive)) 1300 { 1301 wdc_set_drive(chp, xfer->drive); 1302 delay (1); 1303 } 1304 1305 if (timeout) { 1306 int trigger_timeout = 1; 1307 1308 switch (drvp->state) { 1309 case ATAPI_DEVICE_RESET_WAIT_STATE: 1310 errstring = "Device Reset Wait"; 1311 drvp->drive_flags &= ~DRIVE_DEVICE_RESET; 1312 break; 1313 1314 case ATAPI_IDENTIFY_WAIT_STATE: 1315 errstring = "Identify"; 1316 if (!(chp->ch_status & WDCS_BSY) && 1317 (chp->ch_status & (WDCS_DRQ | WDCS_ERR))) 1318 trigger_timeout = 0; 1319 1320 break; 1321 1322 case ATAPI_PIOMODE_STATE: 1323 errstring = "Post-Identify"; 1324 if (!(chp->ch_status & (WDCS_BSY | WDCS_DRQ))) 1325 trigger_timeout = 0; 1326 break; 1327 1328 case ATAPI_PIOMODE_WAIT_STATE: 1329 errstring = "PIOMODE"; 1330 if (chp->ch_status & (WDCS_BSY | WDCS_DRQ)) 1331 drvp->drive_flags &= ~DRIVE_MODE; 1332 else 1333 trigger_timeout = 0; 1334 break; 1335 case ATAPI_DMAMODE_WAIT_STATE: 1336 errstring = "dmamode"; 1337 if (chp->ch_status & (WDCS_BSY | WDCS_DRQ)) 1338 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 1339 else 1340 trigger_timeout = 0; 1341 break; 1342 1343 default: 1344 errstring = "unknown state"; 1345 break; 1346 } 1347 1348 if (trigger_timeout) 1349 goto timeout; 1350 } 1351 1352 WDCDEBUG_PRINT(("wdc_atapi_ctrl %s:%d:%d state %d\n", 1353 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive, drvp->state), 1354 DEBUG_INTR | DEBUG_FUNCS); 1355 1356 switch (drvp->state) { 1357 /* My ATAPI slave device likes to assert DASP-/PDIAG- until 1358 it is DEVICE RESET. This causes the LED to stay on. 1359 1360 There is a trade-off here. This drive will cause any 1361 play-back or seeks happening to be interrupted. 1362 1363 Note that the bus reset that triggered this state 1364 (which may have been caused by the other drive on 1365 the chain) need not interrupt this playback. It happens 1366 to on my Smart & Friendly CD burner. 1367 1368 - csapuntz@ 1369 */ 1370 case ATAPI_RESET_BASE_STATE: 1371 if ((drvp->drive_flags & DRIVE_DEVICE_RESET) == 0) { 1372 drvp->state = ATAPI_IDENTIFY_STATE; 1373 break; 1374 } 1375 1376 wdccommandshort(chp, drvp->drive, ATAPI_DEVICE_RESET); 1377 drvp->state = ATAPI_DEVICE_RESET_WAIT_STATE; 1378 ret->delay = ATAPI_RESET_DELAY; 1379 ret->timeout = ATAPI_RESET_WAIT; 1380 break; 1381 1382 case ATAPI_DEVICE_RESET_WAIT_STATE: 1383 /* FALLTHROUGH */ 1384 1385 case ATAPI_IDENTIFY_STATE: 1386 wdccommandshort(chp, drvp->drive, ATAPI_IDENTIFY_DEVICE); 1387 drvp->state = ATAPI_IDENTIFY_WAIT_STATE; 1388 ret->delay = 10; 1389 ret->timeout = ATAPI_RESET_WAIT; 1390 break; 1391 1392 case ATAPI_IDENTIFY_WAIT_STATE: { 1393 int idx = 0; 1394 1395 while ((chp->ch_status & WDCS_DRQ) && 1396 idx++ < 20) { 1397 wdcbit_bucket(chp, 512); 1398 1399 DELAY(1); 1400 wdc_atapi_update_status(chp); 1401 } 1402 1403 drvp->state = ATAPI_PIOMODE_STATE; 1404 /* 1405 * Note, we can't go directly to set PIO mode 1406 * because the drive is free to assert BSY 1407 * after the transfer 1408 */ 1409 break; 1410 } 1411 1412 case ATAPI_PIOMODE_STATE: 1413 /* Don't try to set mode if controller can't be adjusted */ 1414 if ((chp->wdc->cap & WDC_CAPABILITY_MODE) == 0) 1415 goto ready; 1416 /* Also don't try if the drive didn't report its mode */ 1417 if ((drvp->drive_flags & DRIVE_MODE) == 0) 1418 goto ready; 1419 /* SET FEATURES 0x08 is only for PIO mode > 2 */ 1420 if (drvp->PIO_mode <= 2) 1421 goto ready; 1422 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 1423 0x08 | drvp->PIO_mode, WDSF_SET_MODE); 1424 drvp->state = ATAPI_PIOMODE_WAIT_STATE; 1425 ret->timeout = ATAPI_CTRL_WAIT; 1426 ret->expect_irq = 1; 1427 break; 1428 case ATAPI_PIOMODE_WAIT_STATE: 1429 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 1430 chp->wdc->irqack(chp); 1431 if (chp->ch_status & WDCS_ERR) { 1432 /* Downgrade straight to PIO mode 3 */ 1433 drvp->PIO_mode = 3; 1434 chp->wdc->set_modes(chp); 1435 } 1436 /* FALLTHROUGH */ 1437 1438 case ATAPI_DMAMODE_STATE: 1439 if (drvp->drive_flags & DRIVE_UDMA) { 1440 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 1441 0x40 | drvp->UDMA_mode, WDSF_SET_MODE); 1442 } else if (drvp->drive_flags & DRIVE_DMA) { 1443 wdccommand(chp, drvp->drive, SET_FEATURES, 0, 0, 0, 1444 0x20 | drvp->DMA_mode, WDSF_SET_MODE); 1445 } else { 1446 goto ready; 1447 } 1448 drvp->state = ATAPI_DMAMODE_WAIT_STATE; 1449 1450 ret->timeout = ATAPI_CTRL_WAIT; 1451 ret->expect_irq = 1; 1452 break; 1453 1454 case ATAPI_DMAMODE_WAIT_STATE: 1455 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 1456 chp->wdc->irqack(chp); 1457 if (chp->ch_status & WDCS_ERR) 1458 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 1459 /* FALLTHROUGH */ 1460 1461 case ATAPI_READY_STATE: 1462 ready: 1463 drvp->state = ATAPI_READY_STATE; 1464 xfer->next = wdc_atapi_real_start; 1465 break; 1466 } 1467 return; 1468 1469 timeout: 1470 printf("%s:%d:%d: %s timed out\n", 1471 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, errstring); 1472 sc_xfer->error = XS_TIMEOUT; 1473 xfer->next = wdc_atapi_reset; 1474 return; 1475 1476 } 1477 1478 void 1479 wdc_atapi_tape_done(struct channel_softc *chp, struct wdc_xfer *xfer, 1480 int timeout, struct atapi_return_args *ret) 1481 { 1482 struct scsi_xfer *sc_xfer = xfer->cmd; 1483 1484 if (sc_xfer->error != XS_NOERROR) { 1485 xfer->next = wdc_atapi_done; 1486 return; 1487 } 1488 1489 _lto3b(xfer->transfer_len, 1490 ((struct scsi_rw_tape *) 1491 sc_xfer->cmd)->len); 1492 1493 xfer->c_bcount = sc_xfer->datalen; 1494 xfer->c_done = NULL; 1495 xfer->c_skip = 0; 1496 1497 xfer->next = wdc_atapi_real_start; 1498 return; 1499 } 1500 1501 1502 void 1503 wdc_atapi_done(struct channel_softc *chp, struct wdc_xfer *xfer, 1504 int timeout, struct atapi_return_args *ret) 1505 { 1506 struct scsi_xfer *sc_xfer = xfer->cmd; 1507 1508 WDCDEBUG_PRINT(("wdc_atapi_done %s:%d:%d: flags 0x%x error 0x%x\n", 1509 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive, 1510 (u_int)xfer->c_flags, sc_xfer->error), DEBUG_XFERS); 1511 WDC_LOG_ATAPI_DONE(chp, xfer->drive, xfer->c_flags, sc_xfer->error); 1512 1513 if (xfer->c_flags & C_POLL) 1514 wdc_enable_intr(chp); 1515 1516 scsi_done(sc_xfer); 1517 1518 xfer->next = NULL; 1519 return; 1520 } 1521 1522 1523 void 1524 wdc_atapi_reset(struct channel_softc *chp, struct wdc_xfer *xfer, 1525 int timeout, struct atapi_return_args *ret) 1526 { 1527 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 1528 1529 if (drvp->state == 0) { 1530 xfer->next = wdc_atapi_done; 1531 return; 1532 } 1533 1534 WDCDEBUG_PRINT(("wdc_atapi_reset\n"), DEBUG_XFERS); 1535 wdccommandshort(chp, xfer->drive, ATAPI_SOFT_RESET); 1536 drvp->state = ATAPI_IDENTIFY_STATE; 1537 1538 drvp->n_resets++; 1539 /* Some ATAPI devices need extra time to find their 1540 brains after a reset 1541 */ 1542 xfer->next = wdc_atapi_reset_2; 1543 ret->delay = ATAPI_RESET_DELAY; 1544 ret->timeout = ATAPI_RESET_WAIT; 1545 return; 1546 } 1547 1548 void 1549 wdc_atapi_reset_2(struct channel_softc *chp, struct wdc_xfer *xfer, 1550 int timeout, struct atapi_return_args *ret) 1551 { 1552 struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive]; 1553 struct scsi_xfer *sc_xfer = xfer->cmd; 1554 1555 if (timeout) { 1556 printf("%s:%d:%d: soft reset failed\n", 1557 chp->wdc->sc_dev.dv_xname, chp->channel, 1558 xfer->drive); 1559 sc_xfer->error = XS_SELTIMEOUT; 1560 wdc_reset_channel(drvp, 0); 1561 1562 xfer->next = wdc_atapi_done; 1563 return; 1564 } 1565 1566 wdc_atapi_update_status(chp); 1567 1568 if (chp->ch_status & (WDCS_BSY | WDCS_DRQ)) { 1569 return; 1570 } 1571 1572 xfer->next = wdc_atapi_done; 1573 return; 1574 } 1575