1 /* $NetBSD: ata.c,v 1.62 2004/10/30 23:10:37 bouyer Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Manuel Bouyer. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.62 2004/10/30 23:10:37 bouyer Exp $"); 34 35 #ifndef ATADEBUG 36 #define ATADEBUG 37 #endif /* ATADEBUG */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/device.h> 44 #include <sys/conf.h> 45 #include <sys/fcntl.h> 46 #include <sys/proc.h> 47 #include <sys/pool.h> 48 #include <sys/kthread.h> 49 #include <sys/errno.h> 50 #include <sys/ataio.h> 51 52 #include <machine/intr.h> 53 #include <machine/bus.h> 54 55 #include <dev/ata/atareg.h> 56 #include <dev/ata/atavar.h> 57 58 #include "locators.h" 59 60 #include "atapibus.h" 61 #include "ataraid.h" 62 63 #if NATARAID > 0 64 #include <dev/ata/ata_raidvar.h> 65 #endif 66 67 #define DEBUG_FUNCS 0x08 68 #define DEBUG_PROBE 0x10 69 #define DEBUG_DETACH 0x20 70 #define DEBUG_XFERS 0x40 71 #ifdef ATADEBUG 72 int atadebug_mask = 0; 73 #define ATADEBUG_PRINT(args, level) \ 74 if (atadebug_mask & (level)) \ 75 printf args 76 #else 77 #define ATADEBUG_PRINT(args, level) 78 #endif 79 80 POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL); 81 82 /* 83 * A queue of atabus instances, used to ensure the same bus probe order 84 * for a given hardware configuration at each boot. 85 */ 86 struct atabus_initq_head atabus_initq_head = 87 TAILQ_HEAD_INITIALIZER(atabus_initq_head); 88 struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER; 89 90 /***************************************************************************** 91 * ATA bus layer. 92 * 93 * ATA controllers attach an atabus instance, which handles probing the bus 94 * for drives, etc. 95 *****************************************************************************/ 96 97 dev_type_open(atabusopen); 98 dev_type_close(atabusclose); 99 dev_type_ioctl(atabusioctl); 100 101 const struct cdevsw atabus_cdevsw = { 102 atabusopen, atabusclose, noread, nowrite, atabusioctl, 103 nostop, notty, nopoll, nommap, nokqfilter, 104 }; 105 106 extern struct cfdriver atabus_cd; 107 108 109 /* 110 * atabusprint: 111 * 112 * Autoconfiguration print routine used by ATA controllers when 113 * attaching an atabus instance. 114 */ 115 int 116 atabusprint(void *aux, const char *pnp) 117 { 118 struct ata_channel *chan = aux; 119 120 if (pnp) 121 aprint_normal("atabus at %s", pnp); 122 aprint_normal(" channel %d", chan->ch_channel); 123 124 return (UNCONF); 125 } 126 127 /* 128 * ataprint: 129 * 130 * Autoconfiguration print routine. 131 */ 132 int 133 ataprint(void *aux, const char *pnp) 134 { 135 struct ata_device *adev = aux; 136 137 if (pnp) 138 aprint_normal("wd at %s", pnp); 139 aprint_normal(" drive %d", adev->adev_drv_data->drive); 140 141 return (UNCONF); 142 } 143 144 /* 145 * ata_channel_attach: 146 * 147 * Common parts of attaching an atabus to an ATA controller channel. 148 */ 149 void 150 ata_channel_attach(struct ata_channel *chp) 151 { 152 153 if (chp->ch_flags & ATACH_DISABLED) 154 return; 155 156 callout_init(&chp->ch_callout); 157 158 TAILQ_INIT(&chp->ch_queue->queue_xfer); 159 chp->ch_queue->queue_freeze = 0; 160 chp->ch_queue->active_xfer = NULL; 161 162 chp->atabus = config_found(&chp->ch_atac->atac_dev, chp, atabusprint); 163 } 164 165 static void 166 atabusconfig(struct atabus_softc *atabus_sc) 167 { 168 struct ata_channel *chp = atabus_sc->sc_chan; 169 struct atac_softc *atac = chp->ch_atac; 170 int i, s; 171 struct atabus_initq *atabus_initq = NULL; 172 173 /* Probe for the drives. */ 174 (*atac->atac_probe)(chp); 175 176 ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n", 177 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags), 178 DEBUG_PROBE); 179 180 /* If no drives, abort here */ 181 for (i = 0; i < chp->ch_ndrive; i++) 182 if ((chp->ch_drive[i].drive_flags & DRIVE) != 0) 183 break; 184 if (i == chp->ch_ndrive) 185 goto out; 186 187 /* Shortcut in case we've been shutdown */ 188 if (chp->ch_flags & ATACH_SHUTDOWN) 189 goto out; 190 191 /* Make sure the devices probe in atabus order to avoid jitter. */ 192 simple_lock(&atabus_interlock); 193 while(1) { 194 atabus_initq = TAILQ_FIRST(&atabus_initq_head); 195 if (atabus_initq->atabus_sc == atabus_sc) 196 break; 197 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0, 198 &atabus_interlock); 199 } 200 simple_unlock(&atabus_interlock); 201 202 /* 203 * Attach an ATAPI bus, if needed. 204 */ 205 for (i = 0; i < chp->ch_ndrive; i++) { 206 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) { 207 #if NATAPIBUS > 0 208 (*atac->atac_atapibus_attach)(atabus_sc); 209 #else 210 /* 211 * Fake the autoconfig "not configured" message 212 */ 213 aprint_normal("atapibus at %s not configured\n", 214 atac->atac_dev.dv_xname); 215 chp->atapibus = NULL; 216 s = splbio(); 217 for (i = 0; i < chp->ch_ndrive; i++) 218 chp->ch_drive[i].drive_flags &= ~DRIVE_ATAPI; 219 splx(s); 220 #endif 221 break; 222 } 223 } 224 225 for (i = 0; i < chp->ch_ndrive; i++) { 226 struct ata_device adev; 227 if ((chp->ch_drive[i].drive_flags & 228 (DRIVE_ATA | DRIVE_OLD)) == 0) { 229 continue; 230 } 231 memset(&adev, 0, sizeof(struct ata_device)); 232 adev.adev_bustype = atac->atac_bustype_ata; 233 adev.adev_channel = chp->ch_channel; 234 adev.adev_openings = 1; 235 adev.adev_drv_data = &chp->ch_drive[i]; 236 chp->ata_drives[i] = config_found(&atabus_sc->sc_dev, 237 &adev, ataprint); 238 if (chp->ata_drives[i] != NULL) 239 ata_probe_caps(&chp->ch_drive[i]); 240 else { 241 s = splbio(); 242 chp->ch_drive[i].drive_flags &= 243 ~(DRIVE_ATA | DRIVE_OLD); 244 splx(s); 245 } 246 } 247 248 /* now that we know the drives, the controller can set its modes */ 249 if (atac->atac_set_modes) { 250 (*atac->atac_set_modes)(chp); 251 ata_print_modes(chp); 252 } 253 #if NATARAID > 0 254 if (atac->atac_cap & ATAC_CAP_RAID) 255 for (i = 0; i < chp->ch_ndrive; i++) 256 if (chp->ata_drives[i] != NULL) 257 ata_raid_check_component(chp->ata_drives[i]); 258 #endif /* NATARAID > 0 */ 259 260 /* 261 * reset drive_flags for unattached devices, reset state for attached 262 * ones 263 */ 264 s = splbio(); 265 for (i = 0; i < chp->ch_ndrive; i++) { 266 if (chp->ch_drive[i].drv_softc == NULL) 267 chp->ch_drive[i].drive_flags = 0; 268 else 269 chp->ch_drive[i].state = 0; 270 } 271 splx(s); 272 273 out: 274 if (atabus_initq == NULL) { 275 simple_lock(&atabus_interlock); 276 while(1) { 277 atabus_initq = TAILQ_FIRST(&atabus_initq_head); 278 if (atabus_initq->atabus_sc == atabus_sc) 279 break; 280 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0, 281 &atabus_interlock); 282 } 283 simple_unlock(&atabus_interlock); 284 } 285 simple_lock(&atabus_interlock); 286 TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq); 287 simple_unlock(&atabus_interlock); 288 289 free(atabus_initq, M_DEVBUF); 290 wakeup(&atabus_initq_head); 291 292 ata_delref(chp); 293 294 config_pending_decr(); 295 } 296 297 /* 298 * atabus_thread: 299 * 300 * Worker thread for the ATA bus. 301 */ 302 static void 303 atabus_thread(void *arg) 304 { 305 struct atabus_softc *sc = arg; 306 struct ata_channel *chp = sc->sc_chan; 307 struct ata_xfer *xfer; 308 int i, s; 309 310 s = splbio(); 311 chp->ch_flags |= ATACH_TH_RUN; 312 313 /* 314 * Probe the drives. Reset all flags to 0 to indicate to controllers 315 * that can re-probe that all drives must be probed.. 316 * 317 * Note: ch_ndrive may be changed during the probe. 318 */ 319 for (i = 0; i < ATA_MAXDRIVES; i++) 320 chp->ch_drive[i].drive_flags = 0; 321 splx(s); 322 323 /* Configure the devices on the bus. */ 324 atabusconfig(sc); 325 326 for (;;) { 327 s = splbio(); 328 if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 && 329 (chp->ch_queue->active_xfer == NULL || 330 chp->ch_queue->queue_freeze == 0)) { 331 chp->ch_flags &= ~ATACH_TH_RUN; 332 (void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0); 333 chp->ch_flags |= ATACH_TH_RUN; 334 } 335 if (chp->ch_flags & ATACH_SHUTDOWN) { 336 splx(s); 337 break; 338 } 339 if (chp->ch_flags & ATACH_TH_RESET) { 340 /* 341 * ata_reset_channel() will freeze 2 times, so 342 * unfreeze one time. Not a problem as we're at splbio 343 */ 344 chp->ch_queue->queue_freeze--; 345 ata_reset_channel(chp, AT_WAIT | chp->ch_reset_flags); 346 } else if (chp->ch_queue->active_xfer != NULL && 347 chp->ch_queue->queue_freeze == 1) { 348 /* 349 * Caller has bumped queue_freeze, decrease it. 350 */ 351 chp->ch_queue->queue_freeze--; 352 xfer = chp->ch_queue->active_xfer; 353 KASSERT(xfer != NULL); 354 (*xfer->c_start)(xfer->c_chp, xfer); 355 } else if (chp->ch_queue->queue_freeze > 1) 356 panic("ata_thread: queue_freeze"); 357 splx(s); 358 } 359 chp->ch_thread = NULL; 360 wakeup((void *)&chp->ch_flags); 361 kthread_exit(0); 362 } 363 364 /* 365 * atabus_create_thread: 366 * 367 * Helper routine to create the ATA bus worker thread. 368 */ 369 static void 370 atabus_create_thread(void *arg) 371 { 372 struct atabus_softc *sc = arg; 373 struct ata_channel *chp = sc->sc_chan; 374 int error; 375 376 if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread, 377 "%s", sc->sc_dev.dv_xname)) != 0) 378 aprint_error("%s: unable to create kernel thread: error %d\n", 379 sc->sc_dev.dv_xname, error); 380 } 381 382 /* 383 * atabus_match: 384 * 385 * Autoconfiguration match routine. 386 */ 387 static int 388 atabus_match(struct device *parent, struct cfdata *cf, void *aux) 389 { 390 struct ata_channel *chp = aux; 391 392 if (chp == NULL) 393 return (0); 394 395 if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel && 396 cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT) 397 return (0); 398 399 return (1); 400 } 401 402 /* 403 * atabus_attach: 404 * 405 * Autoconfiguration attach routine. 406 */ 407 static void 408 atabus_attach(struct device *parent, struct device *self, void *aux) 409 { 410 struct atabus_softc *sc = (void *) self; 411 struct ata_channel *chp = aux; 412 struct atabus_initq *initq; 413 414 sc->sc_chan = chp; 415 416 aprint_normal("\n"); 417 aprint_naive("\n"); 418 419 if (ata_addref(chp)) 420 return; 421 422 initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK); 423 initq->atabus_sc = sc; 424 TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq); 425 config_pending_incr(); 426 kthread_create(atabus_create_thread, sc); 427 } 428 429 /* 430 * atabus_activate: 431 * 432 * Autoconfiguration activation routine. 433 */ 434 static int 435 atabus_activate(struct device *self, enum devact act) 436 { 437 struct atabus_softc *sc = (void *) self; 438 struct ata_channel *chp = sc->sc_chan; 439 struct device *dev = NULL; 440 int s, i, error = 0; 441 442 s = splbio(); 443 switch (act) { 444 case DVACT_ACTIVATE: 445 error = EOPNOTSUPP; 446 break; 447 448 case DVACT_DEACTIVATE: 449 /* 450 * We might deactivate the children of atapibus twice 451 * (once bia atapibus, once directly), but since the 452 * generic autoconfiguration code maintains the DVF_ACTIVE 453 * flag, it's safe. 454 */ 455 if ((dev = chp->atapibus) != NULL) { 456 error = config_deactivate(dev); 457 if (error) 458 goto out; 459 } 460 461 for (i = 0; i < chp->ch_ndrive; i++) { 462 if ((dev = chp->ch_drive[i].drv_softc) != NULL) { 463 ATADEBUG_PRINT(("atabus_activate: %s: " 464 "deactivating %s\n", sc->sc_dev.dv_xname, 465 dev->dv_xname), 466 DEBUG_DETACH); 467 error = config_deactivate(dev); 468 if (error) 469 goto out; 470 } 471 } 472 break; 473 } 474 out: 475 splx(s); 476 477 #ifdef ATADEBUG 478 if (dev != NULL && error != 0) 479 ATADEBUG_PRINT(("atabus_activate: %s: " 480 "error %d deactivating %s\n", sc->sc_dev.dv_xname, 481 error, dev->dv_xname), DEBUG_DETACH); 482 #endif /* ATADEBUG */ 483 484 return (error); 485 } 486 487 /* 488 * atabus_detach: 489 * 490 * Autoconfiguration detach routine. 491 */ 492 static int 493 atabus_detach(struct device *self, int flags) 494 { 495 struct atabus_softc *sc = (void *) self; 496 struct ata_channel *chp = sc->sc_chan; 497 struct device *dev = NULL; 498 int s, i, error = 0; 499 500 /* Shutdown the channel. */ 501 s = splbio(); /* XXX ALSO NEED AN INTERLOCK HERE. */ 502 chp->ch_flags |= ATACH_SHUTDOWN; 503 splx(s); 504 wakeup(&chp->ch_thread); 505 while (chp->ch_thread != NULL) 506 (void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0); 507 508 /* 509 * Detach atapibus and its children. 510 */ 511 if ((dev = chp->atapibus) != NULL) { 512 ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n", 513 sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH); 514 error = config_detach(dev, flags); 515 if (error) 516 goto out; 517 } 518 519 /* 520 * Detach our other children. 521 */ 522 for (i = 0; i < chp->ch_ndrive; i++) { 523 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) 524 continue; 525 if ((dev = chp->ch_drive[i].drv_softc) != NULL) { 526 ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n", 527 sc->sc_dev.dv_xname, dev->dv_xname), 528 DEBUG_DETACH); 529 error = config_detach(dev, flags); 530 if (error) 531 goto out; 532 } 533 } 534 535 out: 536 #ifdef ATADEBUG 537 if (dev != NULL && error != 0) 538 ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n", 539 sc->sc_dev.dv_xname, error, dev->dv_xname), 540 DEBUG_DETACH); 541 #endif /* ATADEBUG */ 542 543 return (error); 544 } 545 546 CFATTACH_DECL(atabus, sizeof(struct atabus_softc), 547 atabus_match, atabus_attach, atabus_detach, atabus_activate); 548 549 /***************************************************************************** 550 * Common ATA bus operations. 551 *****************************************************************************/ 552 553 /* Get the disk's parameters */ 554 int 555 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags, 556 struct ataparams *prms) 557 { 558 char tb[DEV_BSIZE]; 559 struct ata_command ata_c; 560 struct ata_channel *chp = drvp->chnl_softc; 561 struct atac_softc *atac = chp->ch_atac; 562 563 #if BYTE_ORDER == LITTLE_ENDIAN 564 int i; 565 u_int16_t *p; 566 #endif 567 568 ATADEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS); 569 570 memset(tb, 0, DEV_BSIZE); 571 memset(prms, 0, sizeof(struct ataparams)); 572 memset(&ata_c, 0, sizeof(struct ata_command)); 573 574 if (drvp->drive_flags & DRIVE_ATA) { 575 ata_c.r_command = WDCC_IDENTIFY; 576 ata_c.r_st_bmask = WDCS_DRDY; 577 ata_c.r_st_pmask = 0; 578 ata_c.timeout = 3000; /* 3s */ 579 } else if (drvp->drive_flags & DRIVE_ATAPI) { 580 ata_c.r_command = ATAPI_IDENTIFY_DEVICE; 581 ata_c.r_st_bmask = 0; 582 ata_c.r_st_pmask = 0; 583 ata_c.timeout = 10000; /* 10s */ 584 } else { 585 ATADEBUG_PRINT(("ata_get_parms: no disks\n"), 586 DEBUG_FUNCS|DEBUG_PROBE); 587 return CMD_ERR; 588 } 589 ata_c.flags = AT_READ | flags; 590 ata_c.data = tb; 591 ata_c.bcount = DEV_BSIZE; 592 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp, 593 &ata_c) != ATACMD_COMPLETE) { 594 ATADEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"), 595 DEBUG_FUNCS|DEBUG_PROBE); 596 return CMD_AGAIN; 597 } 598 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 599 ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n", 600 ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE); 601 return CMD_ERR; 602 } else { 603 /* if we didn't read any data something is wrong */ 604 if ((ata_c.flags & AT_XFDONE) == 0) 605 return CMD_ERR; 606 /* Read in parameter block. */ 607 memcpy(prms, tb, sizeof(struct ataparams)); 608 #if BYTE_ORDER == LITTLE_ENDIAN 609 /* 610 * Shuffle string byte order. 611 * ATAPI Mitsumi and NEC drives don't need this. 612 */ 613 if ((prms->atap_config & WDC_CFG_ATAPI_MASK) == 614 WDC_CFG_ATAPI && 615 ((prms->atap_model[0] == 'N' && 616 prms->atap_model[1] == 'E') || 617 (prms->atap_model[0] == 'F' && 618 prms->atap_model[1] == 'X'))) 619 return 0; 620 for (i = 0; i < sizeof(prms->atap_model); i += 2) { 621 p = (u_short *)(prms->atap_model + i); 622 *p = ntohs(*p); 623 } 624 for (i = 0; i < sizeof(prms->atap_serial); i += 2) { 625 p = (u_short *)(prms->atap_serial + i); 626 *p = ntohs(*p); 627 } 628 for (i = 0; i < sizeof(prms->atap_revision); i += 2) { 629 p = (u_short *)(prms->atap_revision + i); 630 *p = ntohs(*p); 631 } 632 #endif 633 return CMD_OK; 634 } 635 } 636 637 int 638 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags) 639 { 640 struct ata_command ata_c; 641 struct ata_channel *chp = drvp->chnl_softc; 642 struct atac_softc *atac = chp->ch_atac; 643 644 ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS); 645 memset(&ata_c, 0, sizeof(struct ata_command)); 646 647 ata_c.r_command = SET_FEATURES; 648 ata_c.r_st_bmask = 0; 649 ata_c.r_st_pmask = 0; 650 ata_c.r_features = WDSF_SET_MODE; 651 ata_c.r_count = mode; 652 ata_c.flags = flags; 653 ata_c.timeout = 1000; /* 1s */ 654 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp, 655 &ata_c) != ATACMD_COMPLETE) 656 return CMD_AGAIN; 657 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 658 return CMD_ERR; 659 } 660 return CMD_OK; 661 } 662 663 void 664 ata_dmaerr(struct ata_drive_datas *drvp, int flags) 665 { 666 /* 667 * Downgrade decision: if we get NERRS_MAX in NXFER. 668 * We start with n_dmaerrs set to NERRS_MAX-1 so that the 669 * first error within the first NXFER ops will immediatly trigger 670 * a downgrade. 671 * If we got an error and n_xfers is bigger than NXFER reset counters. 672 */ 673 drvp->n_dmaerrs++; 674 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) { 675 ata_downgrade_mode(drvp, flags); 676 drvp->n_dmaerrs = NERRS_MAX-1; 677 drvp->n_xfers = 0; 678 return; 679 } 680 if (drvp->n_xfers > NXFER) { 681 drvp->n_dmaerrs = 1; /* just got an error */ 682 drvp->n_xfers = 1; /* restart counting from this error */ 683 } 684 } 685 686 /* 687 * Add a command to the queue and start controller. 688 * 689 * MUST BE CALLED AT splbio()! 690 */ 691 void 692 ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer) 693 { 694 695 ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer, 696 chp->ch_channel, xfer->c_drive), DEBUG_XFERS); 697 698 /* complete xfer setup */ 699 xfer->c_chp = chp; 700 701 /* insert at the end of command list */ 702 TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain); 703 ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n", 704 chp->ch_flags), DEBUG_XFERS); 705 /* 706 * if polling and can sleep, wait for the xfer to be at head of queue 707 */ 708 if ((xfer->c_flags & (C_POLL | C_WAIT)) == (C_POLL | C_WAIT)) { 709 while (chp->ch_queue->active_xfer != NULL || 710 TAILQ_FIRST(&chp->ch_queue->queue_xfer) != xfer) { 711 xfer->c_flags |= C_WAITACT; 712 tsleep(xfer, PRIBIO, "ataact", 0); 713 xfer->c_flags &= ~C_WAITACT; 714 if (xfer->c_flags & C_FREE) { 715 ata_free_xfer(chp, xfer); 716 return; 717 } 718 } 719 } 720 atastart(chp); 721 } 722 723 /* 724 * Start I/O on a controller, for the given channel. 725 * The first xfer may be not for our channel if the channel queues 726 * are shared. 727 * 728 * MUST BE CALLED AT splbio()! 729 */ 730 void 731 atastart(struct ata_channel *chp) 732 { 733 struct atac_softc *atac = chp->ch_atac; 734 struct ata_xfer *xfer; 735 736 #ifdef ATA_DEBUG 737 int spl1, spl2; 738 739 spl1 = splbio(); 740 spl2 = splbio(); 741 if (spl2 != spl1) { 742 printf("atastart: not at splbio()\n"); 743 panic("atastart"); 744 } 745 splx(spl2); 746 splx(spl1); 747 #endif /* ATA_DEBUG */ 748 749 /* is there a xfer ? */ 750 if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL) 751 return; 752 753 /* adjust chp, in case we have a shared queue */ 754 chp = xfer->c_chp; 755 756 if (chp->ch_queue->active_xfer != NULL) { 757 return; /* channel aleady active */ 758 } 759 if (__predict_false(chp->ch_queue->queue_freeze > 0)) { 760 return; /* queue froozen */ 761 } 762 /* 763 * if someone is waiting for the command to be active, wake it up 764 * and let it process the command 765 */ 766 if (xfer->c_flags & C_WAITACT) { 767 ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d " 768 "wait active\n", xfer, chp->ch_channel, xfer->c_drive), 769 DEBUG_XFERS); 770 wakeup(xfer); 771 return; 772 } 773 #ifdef DIAGNOSTIC 774 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) 775 panic("atastart: channel waiting for irq"); 776 #endif 777 if (atac->atac_claim_hw) 778 if (!(*atac->atac_claim_hw)(chp, 0)) 779 return; 780 781 ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer, 782 chp->ch_channel, xfer->c_drive), DEBUG_XFERS); 783 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) { 784 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET; 785 chp->ch_drive[xfer->c_drive].state = 0; 786 } 787 chp->ch_queue->active_xfer = xfer; 788 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain); 789 790 if (atac->atac_cap & ATAC_CAP_NOIRQ) 791 KASSERT(xfer->c_flags & C_POLL); 792 793 xfer->c_start(chp, xfer); 794 } 795 796 struct ata_xfer * 797 ata_get_xfer(int flags) 798 { 799 struct ata_xfer *xfer; 800 int s; 801 802 s = splbio(); 803 xfer = pool_get(&ata_xfer_pool, 804 ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK)); 805 splx(s); 806 if (xfer != NULL) { 807 memset(xfer, 0, sizeof(struct ata_xfer)); 808 } 809 return xfer; 810 } 811 812 void 813 ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer) 814 { 815 struct atac_softc *atac = chp->ch_atac; 816 int s; 817 818 if (xfer->c_flags & C_WAITACT) { 819 /* Someone is waiting for this xfer, so we can't free now */ 820 xfer->c_flags |= C_FREE; 821 wakeup(xfer); 822 return; 823 } 824 825 if (atac->atac_free_hw) 826 (*atac->atac_free_hw)(chp); 827 s = splbio(); 828 pool_put(&ata_xfer_pool, xfer); 829 splx(s); 830 } 831 832 /* 833 * Kill off all pending xfers for a ata_channel. 834 * 835 * Must be called at splbio(). 836 */ 837 void 838 ata_kill_pending(struct ata_drive_datas *drvp) 839 { 840 struct ata_channel *chp = drvp->chnl_softc; 841 struct ata_xfer *xfer, *next_xfer; 842 int s = splbio(); 843 844 for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer); 845 xfer != NULL; xfer = next_xfer) { 846 next_xfer = TAILQ_NEXT(xfer, c_xferchain); 847 if (xfer->c_chp != chp || xfer->c_drive != drvp->drive) 848 continue; 849 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain); 850 (*xfer->c_kill_xfer)(chp, xfer, KILL_GONE); 851 } 852 853 while ((xfer = chp->ch_queue->active_xfer) != NULL) { 854 if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) { 855 drvp->drive_flags |= DRIVE_WAITDRAIN; 856 (void) tsleep(&chp->ch_queue->active_xfer, 857 PRIBIO, "atdrn", 0); 858 } else { 859 /* no more xfer for us */ 860 break; 861 } 862 } 863 splx(s); 864 } 865 866 /* 867 * ata_reset_channel: 868 * 869 * Reset and ATA channel. 870 * 871 * MUST BE CALLED AT splbio()! 872 */ 873 void 874 ata_reset_channel(struct ata_channel *chp, int flags) 875 { 876 struct atac_softc *atac = chp->ch_atac; 877 int drive; 878 879 #ifdef ATA_DEBUG 880 int spl1, spl2; 881 882 spl1 = splbio(); 883 spl2 = splbio(); 884 if (spl2 != spl1) { 885 printf("ata_reset_channel: not at splbio()\n"); 886 panic("ata_reset_channel"); 887 } 888 splx(spl2); 889 splx(spl1); 890 #endif /* ATA_DEBUG */ 891 892 chp->ch_queue->queue_freeze++; 893 894 /* 895 * If we can poll or wait it's OK, otherwise wake up the 896 * kernel thread to do it for us. 897 */ 898 if ((flags & (AT_POLL | AT_WAIT)) == 0) { 899 if (chp->ch_flags & ATACH_TH_RESET) { 900 /* No need to schedule a reset more than one time. */ 901 chp->ch_queue->queue_freeze--; 902 return; 903 } 904 chp->ch_flags |= ATACH_TH_RESET; 905 chp->ch_reset_flags = flags & (AT_RST_EMERG | AT_RST_NOCMD); 906 wakeup(&chp->ch_thread); 907 return; 908 } 909 910 (*atac->atac_bustype_ata->ata_reset_channel)(chp, flags); 911 912 for (drive = 0; drive < chp->ch_ndrive; drive++) 913 chp->ch_drive[drive].state = 0; 914 915 chp->ch_flags &= ~ATACH_TH_RESET; 916 if ((flags & AT_RST_EMERG) == 0) { 917 chp->ch_queue->queue_freeze--; 918 atastart(chp); 919 } else { 920 /* make sure that we can use polled commands */ 921 TAILQ_INIT(&chp->ch_queue->queue_xfer); 922 chp->ch_queue->queue_freeze = 0; 923 chp->ch_queue->active_xfer = NULL; 924 } 925 } 926 927 int 928 ata_addref(struct ata_channel *chp) 929 { 930 struct atac_softc *atac = chp->ch_atac; 931 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 932 int s, error = 0; 933 934 s = splbio(); 935 if (adapt->adapt_refcnt++ == 0 && 936 adapt->adapt_enable != NULL) { 937 error = (*adapt->adapt_enable)(&atac->atac_dev, 1); 938 if (error) 939 adapt->adapt_refcnt--; 940 } 941 splx(s); 942 return (error); 943 } 944 945 void 946 ata_delref(struct ata_channel *chp) 947 { 948 struct atac_softc *atac = chp->ch_atac; 949 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 950 int s; 951 952 s = splbio(); 953 if (adapt->adapt_refcnt-- == 1 && 954 adapt->adapt_enable != NULL) 955 (void) (*adapt->adapt_enable)(&atac->atac_dev, 0); 956 splx(s); 957 } 958 959 void 960 ata_print_modes(struct ata_channel *chp) 961 { 962 struct atac_softc *atac = chp->ch_atac; 963 int drive; 964 struct ata_drive_datas *drvp; 965 966 for (drive = 0; drive < 2; drive++) { 967 drvp = &chp->ch_drive[drive]; 968 if ((drvp->drive_flags & DRIVE) == 0) 969 continue; 970 aprint_normal("%s(%s:%d:%d): using PIO mode %d", 971 drvp->drv_softc->dv_xname, 972 atac->atac_dev.dv_xname, 973 chp->ch_channel, drive, drvp->PIO_mode); 974 if (drvp->drive_flags & DRIVE_DMA) 975 aprint_normal(", DMA mode %d", drvp->DMA_mode); 976 if (drvp->drive_flags & DRIVE_UDMA) { 977 aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode); 978 if (drvp->UDMA_mode == 2) 979 aprint_normal(" (Ultra/33)"); 980 else if (drvp->UDMA_mode == 4) 981 aprint_normal(" (Ultra/66)"); 982 else if (drvp->UDMA_mode == 5) 983 aprint_normal(" (Ultra/100)"); 984 else if (drvp->UDMA_mode == 6) 985 aprint_normal(" (Ultra/133)"); 986 } 987 if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) 988 aprint_normal(" (using DMA data transfers)"); 989 aprint_normal("\n"); 990 } 991 } 992 993 /* 994 * downgrade the transfer mode of a drive after an error. return 1 if 995 * downgrade was possible, 0 otherwise. 996 * 997 * MUST BE CALLED AT splbio()! 998 */ 999 int 1000 ata_downgrade_mode(struct ata_drive_datas *drvp, int flags) 1001 { 1002 struct ata_channel *chp = drvp->chnl_softc; 1003 struct atac_softc *atac = chp->ch_atac; 1004 struct device *drv_dev = drvp->drv_softc; 1005 int cf_flags = drv_dev->dv_cfdata->cf_flags; 1006 1007 /* if drive or controller don't know its mode, we can't do much */ 1008 if ((drvp->drive_flags & DRIVE_MODE) == 0 || 1009 (atac->atac_set_modes == NULL)) 1010 return 0; 1011 /* current drive mode was set by a config flag, let it this way */ 1012 if ((cf_flags & ATA_CONFIG_PIO_SET) || 1013 (cf_flags & ATA_CONFIG_DMA_SET) || 1014 (cf_flags & ATA_CONFIG_UDMA_SET)) 1015 return 0; 1016 1017 /* 1018 * If we were using Ultra-DMA mode, downgrade to the next lower mode. 1019 */ 1020 if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) { 1021 drvp->UDMA_mode--; 1022 printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n", 1023 drv_dev->dv_xname, drvp->UDMA_mode); 1024 } 1025 1026 /* 1027 * If we were using ultra-DMA, don't downgrade to multiword DMA. 1028 */ 1029 else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) { 1030 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 1031 drvp->PIO_mode = drvp->PIO_cap; 1032 printf("%s: transfer error, downgrading to PIO mode %d\n", 1033 drv_dev->dv_xname, drvp->PIO_mode); 1034 } else /* already using PIO, can't downgrade */ 1035 return 0; 1036 1037 (*atac->atac_set_modes)(chp); 1038 ata_print_modes(chp); 1039 /* reset the channel, which will shedule all drives for setup */ 1040 ata_reset_channel(chp, flags | AT_RST_NOCMD); 1041 return 1; 1042 } 1043 1044 /* 1045 * Probe drive's capabilities, for use by the controller later 1046 * Assumes drvp points to an existing drive. 1047 */ 1048 void 1049 ata_probe_caps(struct ata_drive_datas *drvp) 1050 { 1051 struct ataparams params, params2; 1052 struct ata_channel *chp = drvp->chnl_softc; 1053 struct atac_softc *atac = chp->ch_atac; 1054 struct device *drv_dev = drvp->drv_softc; 1055 int i, printed, s; 1056 char *sep = ""; 1057 int cf_flags; 1058 1059 if (ata_get_params(drvp, AT_WAIT, ¶ms) != CMD_OK) { 1060 /* IDENTIFY failed. Can't tell more about the device */ 1061 return; 1062 } 1063 if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) == 1064 (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) { 1065 /* 1066 * Controller claims 16 and 32 bit transfers. 1067 * Re-do an IDENTIFY with 32-bit transfers, 1068 * and compare results. 1069 */ 1070 s = splbio(); 1071 drvp->drive_flags |= DRIVE_CAP32; 1072 splx(s); 1073 ata_get_params(drvp, AT_WAIT, ¶ms2); 1074 if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) { 1075 /* Not good. fall back to 16bits */ 1076 s = splbio(); 1077 drvp->drive_flags &= ~DRIVE_CAP32; 1078 splx(s); 1079 } else { 1080 aprint_normal("%s: 32-bit data port\n", 1081 drv_dev->dv_xname); 1082 } 1083 } 1084 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */ 1085 if (params.atap_ata_major > 0x01 && 1086 params.atap_ata_major != 0xffff) { 1087 for (i = 14; i > 0; i--) { 1088 if (params.atap_ata_major & (1 << i)) { 1089 aprint_normal("%s: ATA version %d\n", 1090 drv_dev->dv_xname, i); 1091 drvp->ata_vers = i; 1092 break; 1093 } 1094 } 1095 } 1096 #endif 1097 1098 /* An ATAPI device is at last PIO mode 3 */ 1099 if (drvp->drive_flags & DRIVE_ATAPI) 1100 drvp->PIO_mode = 3; 1101 1102 /* 1103 * It's not in the specs, but it seems that some drive 1104 * returns 0xffff in atap_extensions when this field is invalid 1105 */ 1106 if (params.atap_extensions != 0xffff && 1107 (params.atap_extensions & WDC_EXT_MODES)) { 1108 printed = 0; 1109 /* 1110 * XXX some drives report something wrong here (they claim to 1111 * support PIO mode 8 !). As mode is coded on 3 bits in 1112 * SET FEATURE, limit it to 7 (so limit i to 4). 1113 * If higher mode than 7 is found, abort. 1114 */ 1115 for (i = 7; i >= 0; i--) { 1116 if ((params.atap_piomode_supp & (1 << i)) == 0) 1117 continue; 1118 if (i > 4) 1119 return; 1120 /* 1121 * See if mode is accepted. 1122 * If the controller can't set its PIO mode, 1123 * assume the defaults are good, so don't try 1124 * to set it 1125 */ 1126 if (atac->atac_set_modes) 1127 /* 1128 * It's OK to pool here, it's fast enouth 1129 * to not bother waiting for interrupt 1130 */ 1131 if (ata_set_mode(drvp, 0x08 | (i + 3), 1132 AT_WAIT) != CMD_OK) 1133 continue; 1134 if (!printed) { 1135 aprint_normal("%s: drive supports PIO mode %d", 1136 drv_dev->dv_xname, i + 3); 1137 sep = ","; 1138 printed = 1; 1139 } 1140 /* 1141 * If controller's driver can't set its PIO mode, 1142 * get the highter one for the drive. 1143 */ 1144 if (atac->atac_set_modes == NULL || 1145 atac->atac_pio_cap >= i + 3) { 1146 drvp->PIO_mode = i + 3; 1147 drvp->PIO_cap = i + 3; 1148 break; 1149 } 1150 } 1151 if (!printed) { 1152 /* 1153 * We didn't find a valid PIO mode. 1154 * Assume the values returned for DMA are buggy too 1155 */ 1156 return; 1157 } 1158 s = splbio(); 1159 drvp->drive_flags |= DRIVE_MODE; 1160 splx(s); 1161 printed = 0; 1162 for (i = 7; i >= 0; i--) { 1163 if ((params.atap_dmamode_supp & (1 << i)) == 0) 1164 continue; 1165 if ((atac->atac_cap & ATAC_CAP_DMA) && 1166 atac->atac_set_modes != NULL) 1167 if (ata_set_mode(drvp, 0x20 | i, AT_WAIT) 1168 != CMD_OK) 1169 continue; 1170 if (!printed) { 1171 aprint_normal("%s DMA mode %d", sep, i); 1172 sep = ","; 1173 printed = 1; 1174 } 1175 if (atac->atac_cap & ATAC_CAP_DMA) { 1176 if (atac->atac_set_modes != NULL && 1177 atac->atac_dma_cap < i) 1178 continue; 1179 drvp->DMA_mode = i; 1180 drvp->DMA_cap = i; 1181 s = splbio(); 1182 drvp->drive_flags |= DRIVE_DMA; 1183 splx(s); 1184 } 1185 break; 1186 } 1187 if (params.atap_extensions & WDC_EXT_UDMA_MODES) { 1188 printed = 0; 1189 for (i = 7; i >= 0; i--) { 1190 if ((params.atap_udmamode_supp & (1 << i)) 1191 == 0) 1192 continue; 1193 if (atac->atac_set_modes != NULL && 1194 (atac->atac_cap & ATAC_CAP_UDMA)) 1195 if (ata_set_mode(drvp, 0x40 | i, 1196 AT_WAIT) != CMD_OK) 1197 continue; 1198 if (!printed) { 1199 aprint_normal("%s Ultra-DMA mode %d", 1200 sep, i); 1201 if (i == 2) 1202 aprint_normal(" (Ultra/33)"); 1203 else if (i == 4) 1204 aprint_normal(" (Ultra/66)"); 1205 else if (i == 5) 1206 aprint_normal(" (Ultra/100)"); 1207 else if (i == 6) 1208 aprint_normal(" (Ultra/133)"); 1209 sep = ","; 1210 printed = 1; 1211 } 1212 if (atac->atac_cap & ATAC_CAP_UDMA) { 1213 if (atac->atac_set_modes != NULL && 1214 atac->atac_udma_cap < i) 1215 continue; 1216 drvp->UDMA_mode = i; 1217 drvp->UDMA_cap = i; 1218 s = splbio(); 1219 drvp->drive_flags |= DRIVE_UDMA; 1220 splx(s); 1221 } 1222 break; 1223 } 1224 } 1225 aprint_normal("\n"); 1226 } 1227 1228 s = splbio(); 1229 drvp->drive_flags &= ~DRIVE_NOSTREAM; 1230 if (drvp->drive_flags & DRIVE_ATAPI) { 1231 if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM) 1232 drvp->drive_flags |= DRIVE_NOSTREAM; 1233 } else { 1234 if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM) 1235 drvp->drive_flags |= DRIVE_NOSTREAM; 1236 } 1237 splx(s); 1238 1239 /* Try to guess ATA version here, if it didn't get reported */ 1240 if (drvp->ata_vers == 0) { 1241 if (drvp->drive_flags & DRIVE_UDMA) 1242 drvp->ata_vers = 4; /* should be at last ATA-4 */ 1243 else if (drvp->PIO_cap > 2) 1244 drvp->ata_vers = 2; /* should be at last ATA-2 */ 1245 } 1246 cf_flags = drv_dev->dv_cfdata->cf_flags; 1247 if (cf_flags & ATA_CONFIG_PIO_SET) { 1248 s = splbio(); 1249 drvp->PIO_mode = 1250 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF; 1251 drvp->drive_flags |= DRIVE_MODE; 1252 splx(s); 1253 } 1254 if ((atac->atac_cap & ATAC_CAP_DMA) == 0) { 1255 /* don't care about DMA modes */ 1256 return; 1257 } 1258 if (cf_flags & ATA_CONFIG_DMA_SET) { 1259 s = splbio(); 1260 if ((cf_flags & ATA_CONFIG_DMA_MODES) == 1261 ATA_CONFIG_DMA_DISABLE) { 1262 drvp->drive_flags &= ~DRIVE_DMA; 1263 } else { 1264 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >> 1265 ATA_CONFIG_DMA_OFF; 1266 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE; 1267 } 1268 splx(s); 1269 } 1270 if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) { 1271 /* don't care about UDMA modes */ 1272 return; 1273 } 1274 if (cf_flags & ATA_CONFIG_UDMA_SET) { 1275 s = splbio(); 1276 if ((cf_flags & ATA_CONFIG_UDMA_MODES) == 1277 ATA_CONFIG_UDMA_DISABLE) { 1278 drvp->drive_flags &= ~DRIVE_UDMA; 1279 } else { 1280 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >> 1281 ATA_CONFIG_UDMA_OFF; 1282 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE; 1283 } 1284 splx(s); 1285 } 1286 } 1287 1288 /* management of the /dev/atabus* devices */ 1289 int 1290 atabusopen(dev_t dev, int flag, int fmt, struct proc *p) 1291 { 1292 struct atabus_softc *sc; 1293 int error, unit = minor(dev); 1294 1295 if (unit >= atabus_cd.cd_ndevs || 1296 (sc = atabus_cd.cd_devs[unit]) == NULL) 1297 return (ENXIO); 1298 1299 if (sc->sc_flags & ATABUSCF_OPEN) 1300 return (EBUSY); 1301 1302 if ((error = ata_addref(sc->sc_chan)) != 0) 1303 return (error); 1304 1305 sc->sc_flags |= ATABUSCF_OPEN; 1306 1307 return (0); 1308 } 1309 1310 1311 int 1312 atabusclose(dev_t dev, int flag, int fmt, struct proc *p) 1313 { 1314 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)]; 1315 1316 ata_delref(sc->sc_chan); 1317 1318 sc->sc_flags &= ~ATABUSCF_OPEN; 1319 1320 return (0); 1321 } 1322 1323 int 1324 atabusioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1325 { 1326 struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)]; 1327 struct ata_channel *chp = sc->sc_chan; 1328 int min_drive, max_drive, drive; 1329 int error; 1330 int s; 1331 1332 /* 1333 * Enforce write permission for ioctls that change the 1334 * state of the bus. Host adapter specific ioctls must 1335 * be checked by the adapter driver. 1336 */ 1337 switch (cmd) { 1338 case ATABUSIOSCAN: 1339 case ATABUSIODETACH: 1340 case ATABUSIORESET: 1341 if ((flag & FWRITE) == 0) 1342 return (EBADF); 1343 } 1344 1345 switch (cmd) { 1346 case ATABUSIORESET: 1347 s = splbio(); 1348 ata_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL); 1349 splx(s); 1350 error = 0; 1351 break; 1352 case ATABUSIOSCAN: 1353 { 1354 #if 0 1355 struct atabusioscan_args *a= 1356 (struct atabusioscan_args *)addr; 1357 #endif 1358 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) || 1359 (chp->ch_drive[1].drive_flags & DRIVE_OLD)) 1360 return (EOPNOTSUPP); 1361 return (EOPNOTSUPP); 1362 } 1363 case ATABUSIODETACH: 1364 { 1365 struct atabusioscan_args *a= 1366 (struct atabusioscan_args *)addr; 1367 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) || 1368 (chp->ch_drive[1].drive_flags & DRIVE_OLD)) 1369 return (EOPNOTSUPP); 1370 switch (a->at_dev) { 1371 case -1: 1372 min_drive = 0; 1373 max_drive = 1; 1374 break; 1375 case 0: 1376 case 1: 1377 min_drive = max_drive = a->at_dev; 1378 break; 1379 default: 1380 return (EINVAL); 1381 } 1382 for (drive = min_drive; drive <= max_drive; drive++) { 1383 if (chp->ch_drive[drive].drv_softc != NULL) { 1384 error = config_detach( 1385 chp->ch_drive[drive].drv_softc, 0); 1386 if (error) 1387 return (error); 1388 chp->ch_drive[drive].drv_softc = NULL; 1389 } 1390 } 1391 error = 0; 1392 break; 1393 } 1394 default: 1395 error = ENOTTY; 1396 } 1397 return (error); 1398 }; 1399