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