1 /* $NetBSD: wdc.c,v 1.98 2001/06/13 18:17:38 bjh21 Exp $ */ 2 3 4 /* 5 * Copyright (c) 1998 Manuel Bouyer. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Manuel Bouyer. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 1998 The NetBSD Foundation, Inc. 35 * All rights reserved. 36 * 37 * This code is derived from software contributed to The NetBSD Foundation 38 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * CODE UNTESTED IN THE CURRENT REVISION: 71 * 72 */ 73 74 #ifndef WDCDEBUG 75 #define WDCDEBUG 76 #endif /* WDCDEBUG */ 77 78 #include <sys/param.h> 79 #include <sys/systm.h> 80 #include <sys/kernel.h> 81 #include <sys/conf.h> 82 #include <sys/buf.h> 83 #include <sys/device.h> 84 #include <sys/malloc.h> 85 #include <sys/pool.h> 86 #include <sys/syslog.h> 87 #include <sys/proc.h> 88 89 #include <machine/intr.h> 90 #include <machine/bus.h> 91 92 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 93 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 94 #define bus_space_write_multi_stream_4 bus_space_write_multi_4 95 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 96 #define bus_space_read_multi_stream_4 bus_space_read_multi_4 97 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 98 99 #include <dev/ata/atavar.h> 100 #include <dev/ata/atareg.h> 101 #include <dev/ic/wdcreg.h> 102 #include <dev/ic/wdcvar.h> 103 104 #include "atapibus.h" 105 106 #define WDCDELAY 100 /* 100 microseconds */ 107 #define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY) 108 #if 0 109 /* If you enable this, it will report any delays more than WDCDELAY * N long. */ 110 #define WDCNDELAY_DEBUG 50 111 #endif 112 113 struct pool wdc_xfer_pool; 114 115 static void __wdcerror __P((struct channel_softc*, char *)); 116 static int __wdcwait_reset __P((struct channel_softc *, int)); 117 void __wdccommand_done __P((struct channel_softc *, struct wdc_xfer *)); 118 void __wdccommand_start __P((struct channel_softc *, struct wdc_xfer *)); 119 int __wdccommand_intr __P((struct channel_softc *, struct wdc_xfer *, int)); 120 int wdprint __P((void *, const char *)); 121 122 123 #define DEBUG_INTR 0x01 124 #define DEBUG_XFERS 0x02 125 #define DEBUG_STATUS 0x04 126 #define DEBUG_FUNCS 0x08 127 #define DEBUG_PROBE 0x10 128 #define DEBUG_DETACH 0x20 129 #define DEBUG_DELAY 0x40 130 #ifdef WDCDEBUG 131 int wdcdebug_mask = 0; 132 int wdc_nxfer = 0; 133 #define WDCDEBUG_PRINT(args, level) if (wdcdebug_mask & (level)) printf args 134 #else 135 #define WDCDEBUG_PRINT(args, level) 136 #endif 137 138 int 139 wdprint(aux, pnp) 140 void *aux; 141 const char *pnp; 142 { 143 struct ata_atapi_attach *aa_link = aux; 144 if (pnp) 145 printf("drive at %s", pnp); 146 printf(" channel %d drive %d", aa_link->aa_channel, 147 aa_link->aa_drv_data->drive); 148 return (UNCONF); 149 } 150 151 int 152 atapiprint(aux, pnp) 153 void *aux; 154 const char *pnp; 155 { 156 struct ata_atapi_attach *aa_link = aux; 157 if (pnp) 158 printf("atapibus at %s", pnp); 159 printf(" channel %d", aa_link->aa_channel); 160 return (UNCONF); 161 } 162 163 /* Test to see controller with at last one attached drive is there. 164 * Returns a bit for each possible drive found (0x01 for drive 0, 165 * 0x02 for drive 1). 166 * Logic: 167 * - If a status register is at 0xff, assume there is no drive here 168 * (ISA has pull-up resistors). Similarly if the status register has 169 * the value we last wrote to the bus (for IDE interfaces without pullups). 170 * If no drive at all -> return. 171 * - reset the controller, wait for it to complete (may take up to 31s !). 172 * If timeout -> return. 173 * - test ATA/ATAPI signatures. If at last one drive found -> return. 174 * - try an ATA command on the master. 175 */ 176 177 int 178 wdcprobe(chp) 179 struct channel_softc *chp; 180 { 181 u_int8_t st0, st1, sc, sn, cl, ch; 182 u_int8_t ret_value = 0x03; 183 u_int8_t drive; 184 int found; 185 186 /* 187 * Sanity check to see if the wdc channel responds at all. 188 */ 189 190 if (chp->wdc == NULL || 191 (chp->wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) { 192 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 193 WDSD_IBM); 194 delay(10); 195 st0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 196 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 197 WDSD_IBM | 0x10); 198 delay(10); 199 st1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 200 201 WDCDEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n", 202 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", 203 chp->channel, st0, st1), DEBUG_PROBE); 204 205 if (st0 == 0xff || st0 == WDSD_IBM) 206 ret_value &= ~0x01; 207 if (st1 == 0xff || st1 == (WDSD_IBM | 0x10)) 208 ret_value &= ~0x02; 209 if (ret_value == 0) 210 return 0; 211 } 212 213 /* assert SRST, wait for reset to complete */ 214 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 215 WDSD_IBM); 216 delay(10); 217 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, 218 WDCTL_RST | WDCTL_IDS); 219 DELAY(1000); 220 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, 221 WDCTL_IDS); 222 delay(1000); 223 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error); 224 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT); 225 delay(10); 226 227 ret_value = __wdcwait_reset(chp, ret_value); 228 WDCDEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n", 229 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", chp->channel, 230 ret_value), DEBUG_PROBE); 231 232 /* if reset failed, there's nothing here */ 233 if (ret_value == 0) 234 return 0; 235 236 /* 237 * Test presence of drives. First test register signatures looking for 238 * ATAPI devices. If it's not an ATAPI and reset said there may be 239 * something here assume it's ATA or OLD. Ghost will be killed later in 240 * attach routine. 241 */ 242 found = 0; 243 for (drive = 0; drive < 2; drive++) { 244 if ((ret_value & (0x01 << drive)) == 0) 245 continue; 246 if (1 < ++found && chp->wdc != NULL && 247 (chp->wdc->cap & WDC_CAPABILITY_SINGLE_DRIVE)) { 248 /* 249 * Ignore second drive if WDC_CAPABILITY_SINGLE_DRIVE 250 * is set. 251 * 252 * Some CF Card (for ex. IBM MicroDrive and SanDisk) 253 * doesn't seem to implement drive select command. In 254 * this case, you can't eliminate ghost drive properly. 255 */ 256 WDCDEBUG_PRINT(("%s:%d:%d: ignored.\n", 257 chp->wdc->sc_dev.dv_xname, 258 chp->channel, drive), DEBUG_PROBE); 259 break; 260 } 261 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 262 WDSD_IBM | (drive << 4)); 263 delay(10); 264 /* Save registers contents */ 265 sc = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt); 266 sn = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_sector); 267 cl = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo); 268 ch = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi); 269 270 WDCDEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x " 271 "cl=0x%x ch=0x%x\n", 272 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", 273 chp->channel, drive, sc, sn, cl, ch), DEBUG_PROBE); 274 /* 275 * sc & sn are supposted to be 0x1 for ATAPI but in some cases 276 * we get wrong values here, so ignore it. 277 */ 278 if (cl == 0x14 && ch == 0xeb) { 279 chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI; 280 } else { 281 chp->ch_drive[drive].drive_flags |= DRIVE_ATA; 282 if (chp->wdc == NULL || 283 (chp->wdc->cap & WDC_CAPABILITY_PREATA) != 0) 284 chp->ch_drive[drive].drive_flags |= DRIVE_OLD; 285 } 286 } 287 return (ret_value); 288 } 289 290 void 291 wdcattach(chp) 292 struct channel_softc *chp; 293 { 294 int channel_flags, ctrl_flags, i, error; 295 struct ata_atapi_attach aa_link; 296 struct ataparams params; 297 static int inited = 0; 298 299 callout_init(&chp->ch_callout); 300 301 if ((error = wdc_addref(chp)) != 0) { 302 printf("%s: unable to enable controller\n", 303 chp->wdc->sc_dev.dv_xname); 304 return; 305 } 306 307 if (wdcprobe(chp) == 0) 308 /* If no drives, abort attach here. */ 309 goto out; 310 311 /* initialise global data */ 312 if (inited == 0) { 313 /* Initialize the wdc_xfer pool. */ 314 pool_init(&wdc_xfer_pool, sizeof(struct wdc_xfer), 0, 315 0, 0, "wdcspl", 0, NULL, NULL, M_DEVBUF); 316 inited++; 317 } 318 TAILQ_INIT(&chp->ch_queue->sc_xfer); 319 320 for (i = 0; i < 2; i++) { 321 chp->ch_drive[i].chnl_softc = chp; 322 chp->ch_drive[i].drive = i; 323 /* 324 * Init error counter so that an error withing the first xfers 325 * will trigger a downgrade 326 */ 327 chp->ch_drive[i].n_dmaerrs = NERRS_MAX-1; 328 329 /* If controller can't do 16bit flag the drives as 32bit */ 330 if ((chp->wdc->cap & 331 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) == 332 WDC_CAPABILITY_DATA32) 333 chp->ch_drive[i].drive_flags |= DRIVE_CAP32; 334 if ((chp->ch_drive[i].drive_flags & DRIVE) == 0) 335 continue; 336 337 /* 338 * Wait a bit, some devices are weird just after a reset. 339 * Then issue a IDENTIFY command, to try to detect slave ghost 340 */ 341 delay(100); 342 error = ata_get_params(&chp->ch_drive[i], AT_POLL, ¶ms); 343 if (error != CMD_OK) { 344 delay(1000000); 345 error = ata_get_params(&chp->ch_drive[i], AT_POLL, 346 ¶ms); 347 } 348 if (error == CMD_OK) { 349 /* If IDENTIFY succeded, this is not an OLD ctrl */ 350 chp->ch_drive[0].drive_flags &= ~DRIVE_OLD; 351 chp->ch_drive[1].drive_flags &= ~DRIVE_OLD; 352 } else { 353 chp->ch_drive[i].drive_flags &= 354 ~(DRIVE_ATA | DRIVE_ATAPI); 355 WDCDEBUG_PRINT(("%s:%d:%d: IDENTIFY failed (%d)\n", 356 chp->wdc->sc_dev.dv_xname, 357 chp->channel, i, error), DEBUG_PROBE); 358 if ((chp->ch_drive[i].drive_flags & DRIVE_OLD) == 0) 359 continue; 360 /* 361 * Pre-ATA drive ? 362 * Test registers writability (Error register not 363 * writable, but cyllo is), then try an ATA command. 364 */ 365 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 366 WDSD_IBM | (i << 4)); 367 delay(10); 368 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, 369 wd_error, 0x58); 370 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, 371 wd_cyl_lo, 0xa5); 372 if (bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 373 wd_error == 0x58) || 374 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 375 wd_cyl_lo) != 0xa5) { 376 WDCDEBUG_PRINT(("%s:%d:%d: register " 377 "writability failed\n", 378 chp->wdc->sc_dev.dv_xname, 379 chp->channel, i), DEBUG_PROBE); 380 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD; 381 } 382 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 383 WDSD_IBM | (i << 4)); 384 delay(100); 385 if (wait_for_ready(chp, 10000) != 0) { 386 WDCDEBUG_PRINT(("%s:%d:%d: not ready\n", 387 chp->wdc->sc_dev.dv_xname, 388 chp->channel, i), DEBUG_PROBE); 389 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD; 390 continue; 391 } 392 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, 393 wd_command, WDCC_RECAL); 394 if (wait_for_ready(chp, 10000) != 0) { 395 WDCDEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n", 396 chp->wdc->sc_dev.dv_xname, 397 chp->channel, i), DEBUG_PROBE); 398 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD; 399 } 400 } 401 } 402 ctrl_flags = chp->wdc->sc_dev.dv_cfdata->cf_flags; 403 channel_flags = (ctrl_flags >> (NBBY * chp->channel)) & 0xff; 404 405 WDCDEBUG_PRINT(("wdcattach: ch_drive_flags 0x%x 0x%x\n", 406 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags), 407 DEBUG_PROBE); 408 409 /* If no drives, abort here */ 410 if ((chp->ch_drive[0].drive_flags & DRIVE) == 0 && 411 (chp->ch_drive[1].drive_flags & DRIVE) == 0) 412 goto out; 413 414 /* 415 * Attach an ATAPI bus, if needed. 416 */ 417 if ((chp->ch_drive[0].drive_flags & DRIVE_ATAPI) || 418 (chp->ch_drive[1].drive_flags & DRIVE_ATAPI)) { 419 #if NATAPIBUS > 0 420 wdc_atapibus_attach(chp); 421 #else 422 /* 423 * Fills in a fake aa_link and call config_found, so that 424 * the config machinery will print 425 * "atapibus at xxx not configured" 426 */ 427 memset(&aa_link, 0, sizeof(struct ata_atapi_attach)); 428 aa_link.aa_type = T_ATAPI; 429 aa_link.aa_channel = chp->channel; 430 aa_link.aa_openings = 1; 431 aa_link.aa_drv_data = 0; 432 aa_link.aa_bus_private = NULL; 433 chp->atapibus = config_found(&chp->wdc->sc_dev, 434 (void *)&aa_link, atapiprint); 435 #endif 436 } 437 438 for (i = 0; i < 2; i++) { 439 if ((chp->ch_drive[i].drive_flags & 440 (DRIVE_ATA | DRIVE_OLD)) == 0) { 441 continue; 442 } 443 memset(&aa_link, 0, sizeof(struct ata_atapi_attach)); 444 aa_link.aa_type = T_ATA; 445 aa_link.aa_channel = chp->channel; 446 aa_link.aa_openings = 1; 447 aa_link.aa_drv_data = &chp->ch_drive[i]; 448 if (config_found(&chp->wdc->sc_dev, (void *)&aa_link, wdprint)) 449 wdc_probe_caps(&chp->ch_drive[i]); 450 } 451 452 /* 453 * reset drive_flags for unnatached devices, reset state for attached 454 * ones 455 */ 456 for (i = 0; i < 2; i++) { 457 if (chp->ch_drive[i].drv_softc == NULL) 458 chp->ch_drive[i].drive_flags = 0; 459 else 460 chp->ch_drive[i].state = 0; 461 } 462 463 /* 464 * Reset channel. The probe, with some combinations of ATA/ATAPI 465 * devices keep it in a mostly working, but strange state (with busy 466 * led on) 467 */ 468 if ((chp->wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) { 469 delay(50); 470 wdcreset(chp, VERBOSE); 471 /* 472 * Read status registers to avoid spurious interrupts. 473 */ 474 for (i = 1; i >= 0; i--) { 475 if (chp->ch_drive[i].drive_flags & DRIVE) { 476 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, 477 wd_sdh, WDSD_IBM | (i << 4)); 478 if (wait_for_unbusy(chp, 10000) < 0) 479 printf("%s:%d:%d: device busy\n", 480 chp->wdc->sc_dev.dv_xname, 481 chp->channel, i); 482 } 483 } 484 } 485 486 out: 487 wdc_delref(chp); 488 } 489 490 /* 491 * Call activate routine of underlying devices. 492 */ 493 int 494 wdcactivate(self, act) 495 struct device *self; 496 enum devact act; 497 { 498 struct wdc_softc *wdc = (struct wdc_softc *)self; 499 struct channel_softc *chp; 500 struct device *sc = 0; 501 int s, i, j, error = 0; 502 503 s = splbio(); 504 switch (act) { 505 case DVACT_ACTIVATE: 506 error = EOPNOTSUPP; 507 break; 508 509 case DVACT_DEACTIVATE: 510 for (i = 0; i < wdc->nchannels; i++) { 511 chp = wdc->channels[i]; 512 513 /* 514 * We might call deactivate routine for 515 * the children of atapibus twice (once via 516 * atapibus, once directly), but since 517 * config_deactivate maintains DVF_ACTIVE flag, 518 * it's safe. 519 */ 520 sc = chp->atapibus; 521 if (sc != NULL) { 522 error = config_deactivate(sc); 523 if (error != 0) 524 goto out; 525 } 526 527 for (j = 0; j < 2; j++) { 528 sc = chp->ch_drive[j].drv_softc; 529 WDCDEBUG_PRINT(("wdcactivate: %s:" 530 " deactivating %s\n", wdc->sc_dev.dv_xname, 531 sc == NULL ? "nodrv" : sc->dv_xname), 532 DEBUG_DETACH); 533 if (sc != NULL) { 534 error = config_deactivate(sc); 535 if (error != 0) 536 goto out; 537 } 538 } 539 } 540 break; 541 } 542 543 out: 544 splx(s); 545 546 #ifdef WDCDEBUG 547 if (sc && error != 0) 548 WDCDEBUG_PRINT(("wdcactivate: %s: error %d deactivating %s\n", 549 wdc->sc_dev.dv_xname, error, sc->dv_xname), DEBUG_DETACH); 550 #endif 551 return (error); 552 } 553 554 int 555 wdcdetach(self, flags) 556 struct device *self; 557 int flags; 558 { 559 struct wdc_softc *wdc = (struct wdc_softc *)self; 560 struct channel_softc *chp; 561 struct device *sc = 0; 562 int i, j, error = 0; 563 564 for (i = 0; i < wdc->nchannels; i++) { 565 chp = wdc->channels[i]; 566 567 /* 568 * Detach atapibus and its children. 569 */ 570 sc = chp->atapibus; 571 if (sc != NULL) { 572 WDCDEBUG_PRINT(("wdcdetach: %s: detaching %s\n", 573 wdc->sc_dev.dv_xname, sc->dv_xname), DEBUG_DETACH); 574 error = config_detach(sc, flags); 575 if (error != 0) 576 goto out; 577 } 578 579 /* 580 * Detach our other children. 581 */ 582 for (j = 0; j < 2; j++) { 583 sc = chp->ch_drive[j].drv_softc; 584 WDCDEBUG_PRINT(("wdcdetach: %s: detaching %s\n", 585 wdc->sc_dev.dv_xname, 586 sc == NULL ? "nodrv" : sc->dv_xname), 587 DEBUG_DETACH); 588 if (sc != NULL) { 589 error = config_detach(sc, flags); 590 if (error != 0) 591 goto out; 592 } 593 } 594 595 wdc_kill_pending(chp); 596 } 597 598 out: 599 #ifdef WDCDEBUG 600 if (sc && error != 0) 601 WDCDEBUG_PRINT(("wdcdetach: %s: error %d detaching %s\n", 602 wdc->sc_dev.dv_xname, error, sc->dv_xname), DEBUG_DETACH); 603 #endif 604 return (error); 605 } 606 607 /* 608 * Start I/O on a controller, for the given channel. 609 * The first xfer may be not for our channel if the channel queues 610 * are shared. 611 */ 612 void 613 wdcstart(chp) 614 struct channel_softc *chp; 615 { 616 struct wdc_xfer *xfer; 617 618 #ifdef WDC_DIAGNOSTIC 619 int spl1, spl2; 620 621 spl1 = splbio(); 622 spl2 = splbio(); 623 if (spl2 != spl1) { 624 printf("wdcstart: not at splbio()\n"); 625 panic("wdcstart"); 626 } 627 splx(spl2); 628 splx(spl1); 629 #endif /* WDC_DIAGNOSTIC */ 630 631 /* is there a xfer ? */ 632 if ((xfer = chp->ch_queue->sc_xfer.tqh_first) == NULL) 633 return; 634 635 /* adjust chp, in case we have a shared queue */ 636 chp = xfer->chp; 637 638 if ((chp->ch_flags & WDCF_ACTIVE) != 0 ) { 639 return; /* channel aleady active */ 640 } 641 #ifdef DIAGNOSTIC 642 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0) 643 panic("wdcstart: channel waiting for irq\n"); 644 #endif 645 if (chp->wdc->cap & WDC_CAPABILITY_HWLOCK) 646 if (!(*chp->wdc->claim_hw)(chp, 0)) 647 return; 648 649 WDCDEBUG_PRINT(("wdcstart: xfer %p channel %d drive %d\n", xfer, 650 chp->channel, xfer->drive), DEBUG_XFERS); 651 chp->ch_flags |= WDCF_ACTIVE; 652 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_RESET) { 653 chp->ch_drive[xfer->drive].drive_flags &= ~DRIVE_RESET; 654 chp->ch_drive[xfer->drive].state = 0; 655 } 656 if (chp->wdc->cap & WDC_CAPABILITY_NOIRQ) 657 KASSERT(xfer->c_flags & C_POLL); 658 xfer->c_start(chp, xfer); 659 } 660 661 /* restart an interrupted I/O */ 662 void 663 wdcrestart(v) 664 void *v; 665 { 666 struct channel_softc *chp = v; 667 int s; 668 669 s = splbio(); 670 wdcstart(chp); 671 splx(s); 672 } 673 674 675 /* 676 * Interrupt routine for the controller. Acknowledge the interrupt, check for 677 * errors on the current operation, mark it done if necessary, and start the 678 * next request. Also check for a partially done transfer, and continue with 679 * the next chunk if so. 680 */ 681 int 682 wdcintr(arg) 683 void *arg; 684 { 685 struct channel_softc *chp = arg; 686 struct wdc_xfer *xfer; 687 int ret; 688 689 if ((chp->wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) { 690 WDCDEBUG_PRINT(("wdcintr: deactivated controller\n"), 691 DEBUG_INTR); 692 return (0); 693 } 694 if ((chp->ch_flags & WDCF_IRQ_WAIT) == 0) { 695 WDCDEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR); 696 return (0); 697 } 698 699 WDCDEBUG_PRINT(("wdcintr\n"), DEBUG_INTR); 700 xfer = chp->ch_queue->sc_xfer.tqh_first; 701 if (chp->ch_flags & WDCF_DMA_WAIT) { 702 chp->wdc->dma_status = 703 (*chp->wdc->dma_finish)(chp->wdc->dma_arg, chp->channel, 704 xfer->drive, 0); 705 if (chp->wdc->dma_status & WDC_DMAST_NOIRQ) { 706 /* IRQ not for us, not detected by DMA engine */ 707 return 0; 708 } 709 chp->ch_flags &= ~WDCF_DMA_WAIT; 710 } 711 chp->ch_flags &= ~WDCF_IRQ_WAIT; 712 ret = xfer->c_intr(chp, xfer, 1); 713 if (ret == 0) /* irq was not for us, still waiting for irq */ 714 chp->ch_flags |= WDCF_IRQ_WAIT; 715 return (ret); 716 } 717 718 /* Put all disk in RESET state */ 719 void wdc_reset_channel(drvp) 720 struct ata_drive_datas *drvp; 721 { 722 struct channel_softc *chp = drvp->chnl_softc; 723 int drive; 724 WDCDEBUG_PRINT(("ata_reset_channel %s:%d for drive %d\n", 725 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), 726 DEBUG_FUNCS); 727 (void) wdcreset(chp, VERBOSE); 728 for (drive = 0; drive < 2; drive++) { 729 chp->ch_drive[drive].state = 0; 730 } 731 } 732 733 int 734 wdcreset(chp, verb) 735 struct channel_softc *chp; 736 int verb; 737 { 738 int drv_mask1, drv_mask2; 739 740 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 741 WDSD_IBM); /* master */ 742 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, 743 WDCTL_RST | WDCTL_IDS); 744 delay(1000); 745 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, 746 WDCTL_IDS); 747 delay(1000); 748 (void) bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_error); 749 bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, 750 WDCTL_4BIT); 751 752 drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00; 753 drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00; 754 drv_mask2 = __wdcwait_reset(chp, drv_mask1); 755 if (verb && drv_mask2 != drv_mask1) { 756 printf("%s channel %d: reset failed for", 757 chp->wdc->sc_dev.dv_xname, chp->channel); 758 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0) 759 printf(" drive 0"); 760 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0) 761 printf(" drive 1"); 762 printf("\n"); 763 } 764 return (drv_mask1 != drv_mask2) ? 1 : 0; 765 } 766 767 static int 768 __wdcwait_reset(chp, drv_mask) 769 struct channel_softc *chp; 770 int drv_mask; 771 { 772 int timeout; 773 u_int8_t st0, st1; 774 #ifdef WDCDEBUG 775 u_int8_t sc0, sn0, cl0, ch0; 776 u_int8_t sc1, sn1, cl1, ch1; 777 #endif 778 /* wait for BSY to deassert */ 779 for (timeout = 0; timeout < WDCNDELAY_RST;timeout++) { 780 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 781 WDSD_IBM); /* master */ 782 delay(10); 783 st0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 784 #ifdef WDCDEBUG 785 sc0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt); 786 sn0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_sector); 787 cl0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo); 788 ch0 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi); 789 #endif 790 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 791 WDSD_IBM | 0x10); /* slave */ 792 delay(10); 793 st1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 794 #ifdef WDCDEBUG 795 sc1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt); 796 sn1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_sector); 797 cl1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo); 798 ch1 = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi); 799 #endif 800 801 if ((drv_mask & 0x01) == 0) { 802 /* no master */ 803 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) { 804 /* No master, slave is ready, it's done */ 805 goto end; 806 } 807 } else if ((drv_mask & 0x02) == 0) { 808 /* no slave */ 809 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) { 810 /* No slave, master is ready, it's done */ 811 goto end; 812 } 813 } else { 814 /* Wait for both master and slave to be ready */ 815 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) { 816 goto end; 817 } 818 } 819 delay(WDCDELAY); 820 } 821 /* Reset timed out. Maybe it's because drv_mask was not rigth */ 822 if (st0 & WDCS_BSY) 823 drv_mask &= ~0x01; 824 if (st1 & WDCS_BSY) 825 drv_mask &= ~0x02; 826 end: 827 WDCDEBUG_PRINT(("%s:%d:0: after reset, sc=0x%x sn=0x%x " 828 "cl=0x%x ch=0x%x\n", 829 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", 830 chp->channel, sc0, sn0, cl0, ch0), DEBUG_PROBE); 831 WDCDEBUG_PRINT(("%s:%d:1: after reset, sc=0x%x sn=0x%x " 832 "cl=0x%x ch=0x%x\n", 833 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", 834 chp->channel, sc1, sn1, cl1, ch1), DEBUG_PROBE); 835 836 WDCDEBUG_PRINT(("%s:%d: wdcwait_reset() end, st0=0x%x, st1=0x%x\n", 837 chp->wdc ? chp->wdc->sc_dev.dv_xname : "wdcprobe", chp->channel, 838 st0, st1), DEBUG_PROBE); 839 840 return drv_mask; 841 } 842 843 /* 844 * Wait for a drive to be !BSY, and have mask in its status register. 845 * return -1 for a timeout after "timeout" ms. 846 */ 847 int 848 wdcwait(chp, mask, bits, timeout) 849 struct channel_softc *chp; 850 int mask, bits, timeout; 851 { 852 u_char status; 853 int time = 0; 854 #ifdef WDCNDELAY_DEBUG 855 extern int cold; 856 #endif 857 858 WDCDEBUG_PRINT(("wdcwait %s:%d\n", chp->wdc ?chp->wdc->sc_dev.dv_xname 859 :"none", chp->channel), DEBUG_STATUS); 860 chp->ch_error = 0; 861 862 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */ 863 864 for (;;) { 865 chp->ch_status = status = 866 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_status); 867 if ((status & WDCS_BSY) == 0 && (status & mask) == bits) 868 break; 869 if (++time > timeout) { 870 WDCDEBUG_PRINT(("wdcwait: timeout (time=%d), " 871 "status %x error %x (mask 0x%x bits 0x%x)\n", 872 time, status, 873 bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 874 wd_error), mask, bits), 875 DEBUG_STATUS | DEBUG_PROBE | DEBUG_DELAY); 876 return -1; 877 } 878 delay(WDCDELAY); 879 } 880 #ifdef WDCDEBUG 881 if (time > 0 && (wdcdebug_mask & DEBUG_DELAY)) 882 printf("wdcwait: did busy-wait, time=%d\n", time); 883 #endif 884 if (status & WDCS_ERR) 885 chp->ch_error = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 886 wd_error); 887 #ifdef WDCNDELAY_DEBUG 888 /* After autoconfig, there should be no long delays. */ 889 if (!cold && time > WDCNDELAY_DEBUG) { 890 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first; 891 if (xfer == NULL) 892 printf("%s channel %d: warning: busy-wait took %dus\n", 893 chp->wdc->sc_dev.dv_xname, chp->channel, 894 WDCDELAY * time); 895 else 896 printf("%s:%d:%d: warning: busy-wait took %dus\n", 897 chp->wdc->sc_dev.dv_xname, chp->channel, 898 xfer->drive, 899 WDCDELAY * time); 900 } 901 #endif 902 return 0; 903 } 904 905 /* 906 * Busy-wait for DMA to complete 907 */ 908 int 909 wdc_dmawait(chp, xfer, timeout) 910 struct channel_softc *chp; 911 struct wdc_xfer *xfer; 912 int timeout; 913 { 914 int time; 915 for (time = 0; time < timeout * 1000 / WDCDELAY; time++) { 916 chp->wdc->dma_status = 917 (*chp->wdc->dma_finish)(chp->wdc->dma_arg, 918 chp->channel, xfer->drive, 0); 919 if ((chp->wdc->dma_status & WDC_DMAST_NOIRQ) == 0) 920 return 0; 921 delay(WDCDELAY); 922 } 923 /* timeout, force a DMA halt */ 924 chp->wdc->dma_status = (*chp->wdc->dma_finish)(chp->wdc->dma_arg, 925 chp->channel, xfer->drive, 1); 926 return 1; 927 } 928 929 void 930 wdctimeout(arg) 931 void *arg; 932 { 933 struct channel_softc *chp = (struct channel_softc *)arg; 934 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first; 935 int s; 936 937 WDCDEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS); 938 939 s = splbio(); 940 if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0) { 941 __wdcerror(chp, "lost interrupt"); 942 printf("\ttype: %s tc_bcount: %d tc_skip: %d\n", 943 (xfer->c_flags & C_ATAPI) ? "atapi" : "ata", 944 xfer->c_bcount, 945 xfer->c_skip); 946 if (chp->ch_flags & WDCF_DMA_WAIT) { 947 chp->wdc->dma_status = 948 (*chp->wdc->dma_finish)(chp->wdc->dma_arg, 949 chp->channel, xfer->drive, 1); 950 chp->ch_flags &= ~WDCF_DMA_WAIT; 951 } 952 /* 953 * Call the interrupt routine. If we just missed and interrupt, 954 * it will do what's needed. Else, it will take the needed 955 * action (reset the device). 956 * Before that we need to reinstall the timeout callback, 957 * in case it will miss another irq while in this transfer 958 * We arbitray chose it to be 1s 959 */ 960 callout_reset(&chp->ch_callout, hz, wdctimeout, chp); 961 xfer->c_flags |= C_TIMEOU; 962 chp->ch_flags &= ~WDCF_IRQ_WAIT; 963 xfer->c_intr(chp, xfer, 1); 964 } else 965 __wdcerror(chp, "missing untimeout"); 966 splx(s); 967 } 968 969 /* 970 * Probe drive's capabilites, for use by the controller later 971 * Assumes drvp points to an existing drive. 972 * XXX this should be a controller-indep function 973 */ 974 void 975 wdc_probe_caps(drvp) 976 struct ata_drive_datas *drvp; 977 { 978 struct ataparams params, params2; 979 struct channel_softc *chp = drvp->chnl_softc; 980 struct device *drv_dev = drvp->drv_softc; 981 struct wdc_softc *wdc = chp->wdc; 982 int i, printed; 983 char *sep = ""; 984 int cf_flags; 985 986 if (ata_get_params(drvp, AT_POLL, ¶ms) != CMD_OK) { 987 /* IDENTIFY failed. Can't tell more about the device */ 988 return; 989 } 990 if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) == 991 (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) { 992 /* 993 * Controller claims 16 and 32 bit transfers. 994 * Re-do an IDENTIFY with 32-bit transfers, 995 * and compare results. 996 */ 997 drvp->drive_flags |= DRIVE_CAP32; 998 ata_get_params(drvp, AT_POLL, ¶ms2); 999 if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) { 1000 /* Not good. fall back to 16bits */ 1001 drvp->drive_flags &= ~DRIVE_CAP32; 1002 } else { 1003 printf("%s: 32-bit data port", drv_dev->dv_xname); 1004 } 1005 } 1006 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */ 1007 if (params.atap_ata_major > 0x01 && 1008 params.atap_ata_major != 0xffff) { 1009 for (i = 14; i > 0; i--) { 1010 if (params.atap_ata_major & (1 << i)) { 1011 if ((drvp->drive_flags & DRIVE_CAP32) == 0) 1012 printf("%s: ", drv_dev->dv_xname); 1013 else 1014 printf(", "); 1015 printf("ATA version %d\n", i); 1016 drvp->ata_vers = i; 1017 break; 1018 } 1019 } 1020 } else 1021 #endif 1022 if (drvp->drive_flags & DRIVE_CAP32) 1023 printf("\n"); 1024 1025 /* An ATAPI device is at last PIO mode 3 */ 1026 if (drvp->drive_flags & DRIVE_ATAPI) 1027 drvp->PIO_mode = 3; 1028 1029 /* 1030 * It's not in the specs, but it seems that some drive 1031 * returns 0xffff in atap_extensions when this field is invalid 1032 */ 1033 if (params.atap_extensions != 0xffff && 1034 (params.atap_extensions & WDC_EXT_MODES)) { 1035 printed = 0; 1036 /* 1037 * XXX some drives report something wrong here (they claim to 1038 * support PIO mode 8 !). As mode is coded on 3 bits in 1039 * SET FEATURE, limit it to 7 (so limit i to 4). 1040 * If higther mode than 7 is found, abort. 1041 */ 1042 for (i = 7; i >= 0; i--) { 1043 if ((params.atap_piomode_supp & (1 << i)) == 0) 1044 continue; 1045 if (i > 4) 1046 return; 1047 /* 1048 * See if mode is accepted. 1049 * If the controller can't set its PIO mode, 1050 * assume the defaults are good, so don't try 1051 * to set it 1052 */ 1053 if ((wdc->cap & WDC_CAPABILITY_MODE) != 0) 1054 if (ata_set_mode(drvp, 0x08 | (i + 3), 1055 AT_POLL) != CMD_OK) 1056 continue; 1057 if (!printed) { 1058 printf("%s: drive supports PIO mode %d", 1059 drv_dev->dv_xname, i + 3); 1060 sep = ","; 1061 printed = 1; 1062 } 1063 /* 1064 * If controller's driver can't set its PIO mode, 1065 * get the highter one for the drive. 1066 */ 1067 if ((wdc->cap & WDC_CAPABILITY_MODE) == 0 || 1068 wdc->PIO_cap >= i + 3) { 1069 drvp->PIO_mode = i + 3; 1070 drvp->PIO_cap = i + 3; 1071 break; 1072 } 1073 } 1074 if (!printed) { 1075 /* 1076 * We didn't find a valid PIO mode. 1077 * Assume the values returned for DMA are buggy too 1078 */ 1079 return; 1080 } 1081 drvp->drive_flags |= DRIVE_MODE; 1082 printed = 0; 1083 for (i = 7; i >= 0; i--) { 1084 if ((params.atap_dmamode_supp & (1 << i)) == 0) 1085 continue; 1086 if ((wdc->cap & WDC_CAPABILITY_DMA) && 1087 (wdc->cap & WDC_CAPABILITY_MODE)) 1088 if (ata_set_mode(drvp, 0x20 | i, AT_POLL) 1089 != CMD_OK) 1090 continue; 1091 if (!printed) { 1092 printf("%s DMA mode %d", sep, i); 1093 sep = ","; 1094 printed = 1; 1095 } 1096 if (wdc->cap & WDC_CAPABILITY_DMA) { 1097 if ((wdc->cap & WDC_CAPABILITY_MODE) && 1098 wdc->DMA_cap < i) 1099 continue; 1100 drvp->DMA_mode = i; 1101 drvp->DMA_cap = i; 1102 drvp->drive_flags |= DRIVE_DMA; 1103 } 1104 break; 1105 } 1106 if (params.atap_extensions & WDC_EXT_UDMA_MODES) { 1107 printed = 0; 1108 for (i = 7; i >= 0; i--) { 1109 if ((params.atap_udmamode_supp & (1 << i)) 1110 == 0) 1111 continue; 1112 if ((wdc->cap & WDC_CAPABILITY_MODE) && 1113 (wdc->cap & WDC_CAPABILITY_UDMA)) 1114 if (ata_set_mode(drvp, 0x40 | i, 1115 AT_POLL) != CMD_OK) 1116 continue; 1117 if (!printed) { 1118 printf("%s Ultra-DMA mode %d", sep, i); 1119 if (i == 2) 1120 printf(" (Ultra/33)"); 1121 else if (i == 4) 1122 printf(" (Ultra/66)"); 1123 else if (i == 5) 1124 printf(" (Ultra/100)"); 1125 sep = ","; 1126 printed = 1; 1127 } 1128 if (wdc->cap & WDC_CAPABILITY_UDMA) { 1129 if ((wdc->cap & WDC_CAPABILITY_MODE) && 1130 wdc->UDMA_cap < i) 1131 continue; 1132 drvp->UDMA_mode = i; 1133 drvp->UDMA_cap = i; 1134 drvp->drive_flags |= DRIVE_UDMA; 1135 } 1136 break; 1137 } 1138 } 1139 printf("\n"); 1140 } 1141 1142 /* Try to guess ATA version here, if it didn't get reported */ 1143 if (drvp->ata_vers == 0) { 1144 if (drvp->drive_flags & DRIVE_UDMA) 1145 drvp->ata_vers = 4; /* should be at last ATA-4 */ 1146 else if (drvp->PIO_cap > 2) 1147 drvp->ata_vers = 2; /* should be at last ATA-2 */ 1148 } 1149 cf_flags = drv_dev->dv_cfdata->cf_flags; 1150 if (cf_flags & ATA_CONFIG_PIO_SET) { 1151 drvp->PIO_mode = 1152 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF; 1153 drvp->drive_flags |= DRIVE_MODE; 1154 } 1155 if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) { 1156 /* don't care about DMA modes */ 1157 return; 1158 } 1159 if (cf_flags & ATA_CONFIG_DMA_SET) { 1160 if ((cf_flags & ATA_CONFIG_DMA_MODES) == 1161 ATA_CONFIG_DMA_DISABLE) { 1162 drvp->drive_flags &= ~DRIVE_DMA; 1163 } else { 1164 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >> 1165 ATA_CONFIG_DMA_OFF; 1166 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE; 1167 } 1168 } 1169 if (cf_flags & ATA_CONFIG_UDMA_SET) { 1170 if ((cf_flags & ATA_CONFIG_UDMA_MODES) == 1171 ATA_CONFIG_UDMA_DISABLE) { 1172 drvp->drive_flags &= ~DRIVE_UDMA; 1173 } else { 1174 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >> 1175 ATA_CONFIG_UDMA_OFF; 1176 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE; 1177 } 1178 } 1179 } 1180 1181 /* 1182 * downgrade the transfer mode of a drive after an error. return 1 if 1183 * downgrade was possible, 0 otherwise. 1184 */ 1185 int 1186 wdc_downgrade_mode(drvp) 1187 struct ata_drive_datas *drvp; 1188 { 1189 struct channel_softc *chp = drvp->chnl_softc; 1190 struct device *drv_dev = drvp->drv_softc; 1191 struct wdc_softc *wdc = chp->wdc; 1192 int cf_flags = drv_dev->dv_cfdata->cf_flags; 1193 1194 /* if drive or controller don't know its mode, we can't do much */ 1195 if ((drvp->drive_flags & DRIVE_MODE) == 0 || 1196 (wdc->cap & WDC_CAPABILITY_MODE) == 0) 1197 return 0; 1198 /* current drive mode was set by a config flag, let it this way */ 1199 if ((cf_flags & ATA_CONFIG_PIO_SET) || 1200 (cf_flags & ATA_CONFIG_DMA_SET) || 1201 (cf_flags & ATA_CONFIG_UDMA_SET)) 1202 return 0; 1203 1204 /* 1205 * If we were using Ultra-DMA mode > 2, downgrade to mode 2 first. 1206 * Maybe we didn't properly notice the cable type 1207 * If we were using Ultra-DMA mode 2, downgrade to mode 1 first. 1208 * It helps in some cases. 1209 */ 1210 if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) { 1211 drvp->UDMA_mode = (drvp->UDMA_mode == 2) ? 1 : 2; 1212 printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n", 1213 drv_dev->dv_xname, drvp->UDMA_mode); 1214 } 1215 1216 /* 1217 * If we were using ultra-DMA, don't downgrade to multiword DMA 1218 * if we noticed a CRC error. It has been noticed that CRC errors 1219 * in ultra-DMA lead to silent data corruption in multiword DMA. 1220 * Data corruption is less likely to occur in PIO mode. 1221 */ 1222 else if ((drvp->drive_flags & DRIVE_UDMA) && 1223 (drvp->drive_flags & DRIVE_DMAERR) == 0) { 1224 drvp->drive_flags &= ~DRIVE_UDMA; 1225 drvp->drive_flags |= DRIVE_DMA; 1226 drvp->DMA_mode = drvp->DMA_cap; 1227 printf("%s: transfer error, downgrading to DMA mode %d\n", 1228 drv_dev->dv_xname, drvp->DMA_mode); 1229 } else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) { 1230 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 1231 drvp->PIO_mode = drvp->PIO_cap; 1232 printf("%s: transfer error, downgrading to PIO mode %d\n", 1233 drv_dev->dv_xname, drvp->PIO_mode); 1234 } else /* already using PIO, can't downgrade */ 1235 return 0; 1236 1237 wdc->set_modes(chp); 1238 /* reset the channel, which will shedule all drives for setup */ 1239 wdc_reset_channel(drvp); 1240 return 1; 1241 } 1242 1243 int 1244 wdc_exec_command(drvp, wdc_c) 1245 struct ata_drive_datas *drvp; 1246 struct wdc_command *wdc_c; 1247 { 1248 struct channel_softc *chp = drvp->chnl_softc; 1249 struct wdc_xfer *xfer; 1250 int s, ret; 1251 1252 WDCDEBUG_PRINT(("wdc_exec_command %s:%d:%d\n", 1253 chp->wdc->sc_dev.dv_xname, chp->channel, drvp->drive), 1254 DEBUG_FUNCS); 1255 1256 /* set up an xfer and queue. Wait for completion */ 1257 xfer = wdc_get_xfer(wdc_c->flags & AT_WAIT ? WDC_CANSLEEP : 1258 WDC_NOSLEEP); 1259 if (xfer == NULL) { 1260 return WDC_TRY_AGAIN; 1261 } 1262 1263 if (chp->wdc->cap & WDC_CAPABILITY_NOIRQ) 1264 wdc_c->flags |= AT_POLL; 1265 if (wdc_c->flags & AT_POLL) 1266 xfer->c_flags |= C_POLL; 1267 xfer->drive = drvp->drive; 1268 xfer->databuf = wdc_c->data; 1269 xfer->c_bcount = wdc_c->bcount; 1270 xfer->cmd = wdc_c; 1271 xfer->c_start = __wdccommand_start; 1272 xfer->c_intr = __wdccommand_intr; 1273 xfer->c_kill_xfer = __wdccommand_done; 1274 1275 s = splbio(); 1276 wdc_exec_xfer(chp, xfer); 1277 #ifdef DIAGNOSTIC 1278 if ((wdc_c->flags & AT_POLL) != 0 && 1279 (wdc_c->flags & AT_DONE) == 0) 1280 panic("wdc_exec_command: polled command not done\n"); 1281 #endif 1282 if (wdc_c->flags & AT_DONE) { 1283 ret = WDC_COMPLETE; 1284 } else { 1285 if (wdc_c->flags & AT_WAIT) { 1286 while ((wdc_c->flags & AT_DONE) == 0) { 1287 tsleep(wdc_c, PRIBIO, "wdccmd", 0); 1288 } 1289 ret = WDC_COMPLETE; 1290 } else { 1291 ret = WDC_QUEUED; 1292 } 1293 } 1294 splx(s); 1295 return ret; 1296 } 1297 1298 void 1299 __wdccommand_start(chp, xfer) 1300 struct channel_softc *chp; 1301 struct wdc_xfer *xfer; 1302 { 1303 int drive = xfer->drive; 1304 struct wdc_command *wdc_c = xfer->cmd; 1305 1306 WDCDEBUG_PRINT(("__wdccommand_start %s:%d:%d\n", 1307 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), 1308 DEBUG_FUNCS); 1309 1310 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 1311 WDSD_IBM | (drive << 4)); 1312 if (wdcwait(chp, wdc_c->r_st_bmask | WDCS_DRQ, wdc_c->r_st_bmask, 1313 wdc_c->timeout) != 0) { 1314 wdc_c->flags |= AT_TIMEOU; 1315 __wdccommand_done(chp, xfer); 1316 return; 1317 } 1318 wdccommand(chp, drive, wdc_c->r_command, wdc_c->r_cyl, wdc_c->r_head, 1319 wdc_c->r_sector, wdc_c->r_count, wdc_c->r_precomp); 1320 if ((wdc_c->flags & AT_POLL) == 0) { 1321 chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */ 1322 callout_reset(&chp->ch_callout, wdc_c->timeout / 1000 * hz, 1323 wdctimeout, chp); 1324 return; 1325 } 1326 /* 1327 * Polled command. Wait for drive ready or drq. Done in intr(). 1328 * Wait for at last 400ns for status bit to be valid. 1329 */ 1330 delay(10); 1331 __wdccommand_intr(chp, xfer, 0); 1332 } 1333 1334 int 1335 __wdccommand_intr(chp, xfer, irq) 1336 struct channel_softc *chp; 1337 struct wdc_xfer *xfer; 1338 int irq; 1339 { 1340 struct wdc_command *wdc_c = xfer->cmd; 1341 int bcount = wdc_c->bcount; 1342 char *data = wdc_c->data; 1343 1344 WDCDEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n", 1345 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), DEBUG_INTR); 1346 if (wdcwait(chp, wdc_c->r_st_pmask, wdc_c->r_st_pmask, 1347 (irq == 0) ? wdc_c->timeout : 0)) { 1348 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 1349 return 0; /* IRQ was not for us */ 1350 wdc_c->flags |= AT_TIMEOU; 1351 __wdccommand_done(chp, xfer); 1352 return 1; 1353 } 1354 if (chp->wdc->cap & WDC_CAPABILITY_IRQACK) 1355 chp->wdc->irqack(chp); 1356 if (wdc_c->flags & AT_READ) { 1357 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_CAP32) { 1358 bus_space_read_multi_4(chp->data32iot, chp->data32ioh, 1359 0, (u_int32_t*)data, bcount >> 2); 1360 data += bcount & 0xfffffffc; 1361 bcount = bcount & 0x03; 1362 } 1363 if (bcount > 0) 1364 bus_space_read_multi_2(chp->cmd_iot, chp->cmd_ioh, 1365 wd_data, (u_int16_t *)data, bcount >> 1); 1366 } else if (wdc_c->flags & AT_WRITE) { 1367 if (chp->ch_drive[xfer->drive].drive_flags & DRIVE_CAP32) { 1368 bus_space_write_multi_4(chp->data32iot, chp->data32ioh, 1369 0, (u_int32_t*)data, bcount >> 2); 1370 data += bcount & 0xfffffffc; 1371 bcount = bcount & 0x03; 1372 } 1373 if (bcount > 0) 1374 bus_space_write_multi_2(chp->cmd_iot, chp->cmd_ioh, 1375 wd_data, (u_int16_t *)data, bcount >> 1); 1376 } 1377 __wdccommand_done(chp, xfer); 1378 return 1; 1379 } 1380 1381 void 1382 __wdccommand_done(chp, xfer) 1383 struct channel_softc *chp; 1384 struct wdc_xfer *xfer; 1385 { 1386 struct wdc_command *wdc_c = xfer->cmd; 1387 1388 WDCDEBUG_PRINT(("__wdccommand_done %s:%d:%d\n", 1389 chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive), DEBUG_FUNCS); 1390 1391 callout_stop(&chp->ch_callout); 1392 1393 if (chp->ch_status & WDCS_DWF) 1394 wdc_c->flags |= AT_DF; 1395 if (chp->ch_status & WDCS_ERR) { 1396 wdc_c->flags |= AT_ERROR; 1397 wdc_c->r_error = chp->ch_error; 1398 } 1399 wdc_c->flags |= AT_DONE; 1400 if ((wdc_c->flags & AT_READREG) != 0 && 1401 (chp->wdc->sc_dev.dv_flags & DVF_ACTIVE) != 0 && 1402 (wdc_c->flags & (AT_ERROR | AT_DF)) == 0) { 1403 wdc_c->r_head = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1404 wd_sdh); 1405 wdc_c->r_cyl = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1406 wd_cyl_hi) << 8; 1407 wdc_c->r_cyl |= bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1408 wd_cyl_lo); 1409 wdc_c->r_sector = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1410 wd_sector); 1411 wdc_c->r_count = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1412 wd_seccnt); 1413 wdc_c->r_error = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1414 wd_error); 1415 wdc_c->r_precomp = bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, 1416 wd_precomp); 1417 } 1418 wdc_free_xfer(chp, xfer); 1419 if (wdc_c->flags & AT_WAIT) 1420 wakeup(wdc_c); 1421 else if (wdc_c->callback) 1422 wdc_c->callback(wdc_c->callback_arg); 1423 wdcstart(chp); 1424 return; 1425 } 1426 1427 /* 1428 * Send a command. The drive should be ready. 1429 * Assumes interrupts are blocked. 1430 */ 1431 void 1432 wdccommand(chp, drive, command, cylin, head, sector, count, precomp) 1433 struct channel_softc *chp; 1434 u_int8_t drive; 1435 u_int8_t command; 1436 u_int16_t cylin; 1437 u_int8_t head, sector, count, precomp; 1438 { 1439 WDCDEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d " 1440 "sector=%d count=%d precomp=%d\n", chp->wdc->sc_dev.dv_xname, 1441 chp->channel, drive, command, cylin, head, sector, count, precomp), 1442 DEBUG_FUNCS); 1443 1444 /* Select drive, head, and addressing mode. */ 1445 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 1446 WDSD_IBM | (drive << 4) | head); 1447 /* Load parameters. wd_features(ATA/ATAPI) = wd_precomp(ST506) */ 1448 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_precomp, 1449 precomp); 1450 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_lo, cylin); 1451 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_cyl_hi, cylin >> 8); 1452 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sector, sector); 1453 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_seccnt, count); 1454 1455 /* Send command. */ 1456 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command, command); 1457 return; 1458 } 1459 1460 /* 1461 * Simplified version of wdccommand(). Unbusy/ready/drq must be 1462 * tested by the caller. 1463 */ 1464 void 1465 wdccommandshort(chp, drive, command) 1466 struct channel_softc *chp; 1467 int drive; 1468 int command; 1469 { 1470 1471 WDCDEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n", 1472 chp->wdc->sc_dev.dv_xname, chp->channel, drive, command), 1473 DEBUG_FUNCS); 1474 1475 /* Select drive. */ 1476 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_sdh, 1477 WDSD_IBM | (drive << 4)); 1478 1479 bus_space_write_1(chp->cmd_iot, chp->cmd_ioh, wd_command, command); 1480 } 1481 1482 /* Add a command to the queue and start controller. Must be called at splbio */ 1483 1484 void 1485 wdc_exec_xfer(chp, xfer) 1486 struct channel_softc *chp; 1487 struct wdc_xfer *xfer; 1488 { 1489 WDCDEBUG_PRINT(("wdc_exec_xfer %p channel %d drive %d\n", xfer, 1490 chp->channel, xfer->drive), DEBUG_XFERS); 1491 1492 /* complete xfer setup */ 1493 xfer->chp = chp; 1494 1495 /* 1496 * If we are a polled command, and the list is not empty, 1497 * we are doing a dump. Drop the list to allow the polled command 1498 * to complete, we're going to reboot soon anyway. 1499 */ 1500 if ((xfer->c_flags & C_POLL) != 0 && 1501 chp->ch_queue->sc_xfer.tqh_first != NULL) { 1502 TAILQ_INIT(&chp->ch_queue->sc_xfer); 1503 } 1504 /* insert at the end of command list */ 1505 TAILQ_INSERT_TAIL(&chp->ch_queue->sc_xfer,xfer , c_xferchain); 1506 WDCDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n", 1507 chp->ch_flags), DEBUG_XFERS); 1508 wdcstart(chp); 1509 } 1510 1511 struct wdc_xfer * 1512 wdc_get_xfer(flags) 1513 int flags; 1514 { 1515 struct wdc_xfer *xfer; 1516 int s; 1517 1518 s = splbio(); 1519 xfer = pool_get(&wdc_xfer_pool, 1520 ((flags & WDC_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK)); 1521 splx(s); 1522 memset(xfer, 0, sizeof(struct wdc_xfer)); 1523 return xfer; 1524 } 1525 1526 void 1527 wdc_free_xfer(chp, xfer) 1528 struct channel_softc *chp; 1529 struct wdc_xfer *xfer; 1530 { 1531 struct wdc_softc *wdc = chp->wdc; 1532 int s; 1533 1534 if (wdc->cap & WDC_CAPABILITY_HWLOCK) 1535 (*wdc->free_hw)(chp); 1536 s = splbio(); 1537 chp->ch_flags &= ~WDCF_ACTIVE; 1538 TAILQ_REMOVE(&chp->ch_queue->sc_xfer, xfer, c_xferchain); 1539 pool_put(&wdc_xfer_pool, xfer); 1540 splx(s); 1541 } 1542 1543 /* 1544 * Kill off all pending xfers for a channel_softc. 1545 * 1546 * Must be called at splbio(). 1547 */ 1548 void 1549 wdc_kill_pending(chp) 1550 struct channel_softc *chp; 1551 { 1552 struct wdc_xfer *xfer; 1553 1554 while ((xfer = TAILQ_FIRST(&chp->ch_queue->sc_xfer)) != NULL) { 1555 chp = xfer->chp; 1556 (*xfer->c_kill_xfer)(chp, xfer); 1557 } 1558 } 1559 1560 static void 1561 __wdcerror(chp, msg) 1562 struct channel_softc *chp; 1563 char *msg; 1564 { 1565 struct wdc_xfer *xfer = chp->ch_queue->sc_xfer.tqh_first; 1566 1567 if (xfer == NULL) 1568 printf("%s:%d: %s\n", chp->wdc->sc_dev.dv_xname, chp->channel, 1569 msg); 1570 else 1571 printf("%s:%d:%d: %s\n", chp->wdc->sc_dev.dv_xname, 1572 chp->channel, xfer->drive, msg); 1573 } 1574 1575 /* 1576 * the bit bucket 1577 */ 1578 void 1579 wdcbit_bucket(chp, size) 1580 struct channel_softc *chp; 1581 int size; 1582 { 1583 1584 for (; size >= 2; size -= 2) 1585 (void)bus_space_read_2(chp->cmd_iot, chp->cmd_ioh, wd_data); 1586 if (size) 1587 (void)bus_space_read_1(chp->cmd_iot, chp->cmd_ioh, wd_data); 1588 } 1589 1590 int 1591 wdc_addref(chp) 1592 struct channel_softc *chp; 1593 { 1594 struct wdc_softc *wdc = chp->wdc; 1595 struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic; 1596 int s, error = 0; 1597 1598 s = splbio(); 1599 if (adapt->adapt_refcnt++ == 0 && 1600 adapt->adapt_enable != NULL) { 1601 error = (*adapt->adapt_enable)(&wdc->sc_dev, 1); 1602 if (error) 1603 adapt->adapt_refcnt--; 1604 } 1605 splx(s); 1606 return (error); 1607 } 1608 1609 void 1610 wdc_delref(chp) 1611 struct channel_softc *chp; 1612 { 1613 struct wdc_softc *wdc = chp->wdc; 1614 struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic; 1615 int s; 1616 1617 s = splbio(); 1618 if (adapt->adapt_refcnt-- == 1 && 1619 adapt->adapt_enable != NULL) 1620 (void) (*adapt->adapt_enable)(&wdc->sc_dev, 0); 1621 splx(s); 1622 } 1623 1624 void 1625 wdc_print_modes(struct channel_softc *chp) 1626 { 1627 int drive; 1628 struct ata_drive_datas *drvp; 1629 1630 for (drive = 0; drive < 2; drive++) { 1631 drvp = &chp->ch_drive[drive]; 1632 if ((drvp->drive_flags & DRIVE) == 0) 1633 continue; 1634 printf("%s(%s:%d:%d): using PIO mode %d", 1635 drvp->drv_softc->dv_xname, 1636 chp->wdc->sc_dev.dv_xname, 1637 chp->channel, drive, drvp->PIO_mode); 1638 if (drvp->drive_flags & DRIVE_DMA) 1639 printf(", DMA mode %d", drvp->DMA_mode); 1640 if (drvp->drive_flags & DRIVE_UDMA) { 1641 printf(", Ultra-DMA mode %d", drvp->UDMA_mode); 1642 if (drvp->UDMA_mode == 2) 1643 printf(" (Ultra/33)"); 1644 else if (drvp->UDMA_mode == 4) 1645 printf(" (Ultra/66)"); 1646 else if (drvp->UDMA_mode == 5) 1647 printf(" (Ultra/100)"); 1648 } 1649 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) 1650 printf(" (using DMA data transfers)"); 1651 printf("\n"); 1652 } 1653 } 1654