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