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