1 /* $OpenBSD: wd.c,v 1.132 2024/07/22 14:03:22 jsg Exp $ */ 2 /* $NetBSD: wd.c,v 1.193 1999/02/28 17:15:27 explorer Exp $ */ 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /*- 29 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc. 30 * All rights reserved. 31 * 32 * This code is derived from software contributed to The NetBSD Foundation 33 * by Charles M. Hannum and by Onno van der Linden. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 45 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 47 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 54 * POSSIBILITY OF SUCH DAMAGE. 55 */ 56 57 #if 0 58 #include "rnd.h" 59 #endif 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/conf.h> 64 #include <sys/fcntl.h> 65 #include <sys/stat.h> 66 #include <sys/buf.h> 67 #include <sys/uio.h> 68 #include <sys/malloc.h> 69 #include <sys/device.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/syslog.h> 73 #include <sys/timeout.h> 74 #include <sys/dkio.h> 75 #include <sys/reboot.h> 76 77 #include <machine/intr.h> 78 #include <machine/bus.h> 79 80 #include <dev/ata/atareg.h> 81 #include <dev/ata/atavar.h> 82 #include <dev/ata/wdvar.h> 83 #include <dev/ic/wdcreg.h> 84 #include <dev/ic/wdcvar.h> 85 #if 0 86 #include "locators.h" 87 #endif 88 89 #define LBA48_THRESHOLD (0xfffffff) /* 128GB / DEV_BSIZE */ 90 91 #define WDIORETRIES_SINGLE 4 /* number of retries before single-sector */ 92 #define WDIORETRIES 5 /* number of retries before giving up */ 93 #define RECOVERYTIME_MSEC 500 /* time to wait before retrying a cmd */ 94 95 #define DEBUG_INTR 0x01 96 #define DEBUG_XFERS 0x02 97 #define DEBUG_STATUS 0x04 98 #define DEBUG_FUNCS 0x08 99 #define DEBUG_PROBE 0x10 100 #ifdef WDCDEBUG 101 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */ 102 #define WDCDEBUG_PRINT(args, level) do { \ 103 if ((wdcdebug_wd_mask & (level)) != 0) \ 104 printf args; \ 105 } while (0) 106 #else 107 #define WDCDEBUG_PRINT(args, level) 108 #endif 109 110 111 #define sc_drive sc_wdc_bio.drive 112 #define sc_mode sc_wdc_bio.mode 113 #define sc_multi sc_wdc_bio.multi 114 115 int wdprobe(struct device *, void *, void *); 116 void wdattach(struct device *, struct device *, void *); 117 int wddetach(struct device *, int); 118 int wdactivate(struct device *, int); 119 120 const struct cfattach wd_ca = { 121 sizeof(struct wd_softc), wdprobe, wdattach, 122 wddetach, wdactivate 123 }; 124 125 struct cfdriver wd_cd = { 126 NULL, "wd", DV_DISK 127 }; 128 129 void wdgetdefaultlabel(struct wd_softc *, struct disklabel *); 130 int wdgetdisklabel(dev_t dev, struct wd_softc *, struct disklabel *, int); 131 void wdstrategy(struct buf *); 132 void wdstart(void *); 133 void __wdstart(struct wd_softc*, struct buf *); 134 void wdrestart(void *); 135 int wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *); 136 int wd_flushcache(struct wd_softc *, int); 137 void wd_standby(struct wd_softc *, int); 138 139 /* XXX: these should go elsewhere */ 140 cdev_decl(wd); 141 bdev_decl(wd); 142 143 #define wdlookup(unit) (struct wd_softc *)disk_lookup(&wd_cd, (unit)) 144 145 146 int 147 wdprobe(struct device *parent, void *match_, void *aux) 148 { 149 struct ata_atapi_attach *aa_link = aux; 150 struct cfdata *match = match_; 151 152 if (aa_link == NULL) 153 return 0; 154 if (aa_link->aa_type != T_ATA) 155 return 0; 156 157 if (match->cf_loc[0] != -1 && 158 match->cf_loc[0] != aa_link->aa_channel) 159 return 0; 160 161 if (match->cf_loc[1] != -1 && 162 match->cf_loc[1] != aa_link->aa_drv_data->drive) 163 return 0; 164 165 return 1; 166 } 167 168 void 169 wdattach(struct device *parent, struct device *self, void *aux) 170 { 171 struct wd_softc *wd = (void *)self; 172 struct ata_atapi_attach *aa_link= aux; 173 struct wdc_command wdc_c; 174 int i, blank; 175 char buf[41], c, *p, *q; 176 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE); 177 178 wd->openings = aa_link->aa_openings; 179 wd->drvp = aa_link->aa_drv_data; 180 181 strlcpy(wd->drvp->drive_name, wd->sc_dev.dv_xname, 182 sizeof(wd->drvp->drive_name)); 183 wd->drvp->cf_flags = wd->sc_dev.dv_cfdata->cf_flags; 184 185 if ((NERRS_MAX - 2) > 0) 186 wd->drvp->n_dmaerrs = NERRS_MAX - 2; 187 else 188 wd->drvp->n_dmaerrs = 0; 189 190 /* read our drive info */ 191 if (wd_get_params(wd, at_poll, &wd->sc_params) != 0) { 192 printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname); 193 return; 194 } 195 196 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0; 197 i < sizeof(wd->sc_params.atap_model); i++) { 198 c = *p++; 199 if (c == '\0') 200 break; 201 if (c != ' ') { 202 if (blank) { 203 *q++ = ' '; 204 blank = 0; 205 } 206 *q++ = c; 207 } else 208 blank = 1; 209 } 210 *q++ = '\0'; 211 212 printf(": <%s>\n", buf); 213 214 wdc_probe_caps(wd->drvp, &wd->sc_params); 215 wdc_print_caps(wd->drvp); 216 217 if ((wd->sc_params.atap_multi & 0xff) > 1) { 218 wd->sc_multi = wd->sc_params.atap_multi & 0xff; 219 } else { 220 wd->sc_multi = 1; 221 } 222 223 printf("%s: %d-sector PIO,", wd->sc_dev.dv_xname, wd->sc_multi); 224 225 /* use 48-bit LBA if enabled */ 226 if ((wd->sc_params.atap_cmd2_en & ATAPI_CMD2_48AD) != 0) 227 wd->sc_flags |= WDF_LBA48; 228 229 /* Prior to ATA-4, LBA was optional. */ 230 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) 231 wd->sc_flags |= WDF_LBA; 232 #if 0 233 /* ATA-4 requires LBA. */ 234 if (wd->sc_params.atap_ataversion != 0xffff && 235 wd->sc_params.atap_ataversion >= WDC_VER_ATA4) 236 wd->sc_flags |= WDF_LBA; 237 #endif 238 239 if ((wd->sc_flags & WDF_LBA48) != 0) { 240 wd->sc_capacity = 241 (((u_int64_t)wd->sc_params.atap_max_lba[3] << 48) | 242 ((u_int64_t)wd->sc_params.atap_max_lba[2] << 32) | 243 ((u_int64_t)wd->sc_params.atap_max_lba[1] << 16) | 244 (u_int64_t)wd->sc_params.atap_max_lba[0]); 245 printf(" LBA48, %lluMB, %llu sectors\n", 246 wd->sc_capacity / (1048576 / DEV_BSIZE), 247 wd->sc_capacity); 248 } else if ((wd->sc_flags & WDF_LBA) != 0) { 249 wd->sc_capacity = 250 (wd->sc_params.atap_capacity[1] << 16) | 251 wd->sc_params.atap_capacity[0]; 252 printf(" LBA, %lluMB, %llu sectors\n", 253 wd->sc_capacity / (1048576 / DEV_BSIZE), 254 wd->sc_capacity); 255 } else { 256 wd->sc_capacity = 257 wd->sc_params.atap_cylinders * 258 wd->sc_params.atap_heads * 259 wd->sc_params.atap_sectors; 260 printf(" CHS, %lluMB, %d cyl, %d head, %d sec, %llu sectors\n", 261 wd->sc_capacity / (1048576 / DEV_BSIZE), 262 wd->sc_params.atap_cylinders, 263 wd->sc_params.atap_heads, 264 wd->sc_params.atap_sectors, 265 wd->sc_capacity); 266 } 267 WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n", 268 self->dv_xname, wd->sc_params.atap_dmatiming_mimi, 269 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE); 270 271 /* use read look ahead if supported */ 272 if (wd->sc_params.atap_cmd_set1 & WDC_CMD1_AHEAD) { 273 bzero(&wdc_c, sizeof(struct wdc_command)); 274 wdc_c.r_command = SET_FEATURES; 275 wdc_c.r_features = WDSF_READAHEAD_EN; 276 wdc_c.timeout = 1000; 277 wdc_c.flags = at_poll; 278 279 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) { 280 printf("%s: enable look ahead command didn't " 281 "complete\n", wd->sc_dev.dv_xname); 282 } 283 } 284 285 /* use write cache if supported */ 286 if (wd->sc_params.atap_cmd_set1 & WDC_CMD1_CACHE) { 287 bzero(&wdc_c, sizeof(struct wdc_command)); 288 wdc_c.r_command = SET_FEATURES; 289 wdc_c.r_features = WDSF_EN_WR_CACHE; 290 wdc_c.timeout = 1000; 291 wdc_c.flags = at_poll; 292 293 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) { 294 printf("%s: enable write cache command didn't " 295 "complete\n", wd->sc_dev.dv_xname); 296 } 297 } 298 299 /* 300 * FREEZE LOCK the drive so malicious users can't lock it on us. 301 * As there is no harm in issuing this to drives that don't 302 * support the security feature set we just send it, and don't 303 * bother checking if the drive sends a command abort to tell us it 304 * doesn't support it. 305 */ 306 bzero(&wdc_c, sizeof(struct wdc_command)); 307 308 wdc_c.r_command = WDCC_SEC_FREEZE_LOCK; 309 wdc_c.timeout = 1000; 310 wdc_c.flags = at_poll; 311 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) { 312 printf("%s: freeze lock command didn't complete\n", 313 wd->sc_dev.dv_xname); 314 } 315 316 /* 317 * Initialize disk structures. 318 */ 319 wd->sc_dk.dk_name = wd->sc_dev.dv_xname; 320 bufq_init(&wd->sc_bufq, BUFQ_DEFAULT); 321 timeout_set(&wd->sc_restart_timeout, wdrestart, wd); 322 323 /* Attach disk. */ 324 disk_attach(&wd->sc_dev, &wd->sc_dk); 325 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label; 326 } 327 328 int 329 wdactivate(struct device *self, int act) 330 { 331 struct wd_softc *wd = (void *)self; 332 int rv = 0; 333 334 switch (act) { 335 case DVACT_SUSPEND: 336 break; 337 case DVACT_POWERDOWN: 338 wd_flushcache(wd, AT_POLL); 339 if (boothowto & RB_POWERDOWN) 340 wd_standby(wd, AT_POLL); 341 break; 342 case DVACT_RESUME: 343 /* 344 * Do two resets separated by a small delay. The 345 * first wakes the controller, the second resets 346 * the channel. 347 */ 348 wdc_disable_intr(wd->drvp->chnl_softc); 349 wdc_reset_channel(wd->drvp, 1); 350 delay(10000); 351 wdc_reset_channel(wd->drvp, 0); 352 wdc_enable_intr(wd->drvp->chnl_softc); 353 wd_get_params(wd, at_poll, &wd->sc_params); 354 break; 355 } 356 return (rv); 357 } 358 359 int 360 wddetach(struct device *self, int flags) 361 { 362 struct wd_softc *sc = (struct wd_softc *)self; 363 364 timeout_del(&sc->sc_restart_timeout); 365 366 bufq_drain(&sc->sc_bufq); 367 368 disk_gone(wdopen, self->dv_unit); 369 370 /* Detach disk. */ 371 bufq_destroy(&sc->sc_bufq); 372 disk_detach(&sc->sc_dk); 373 374 return (0); 375 } 376 377 /* 378 * Read/write routine for a buffer. Validates the arguments and schedules the 379 * transfer. Does not wait for the transfer to complete. 380 */ 381 void 382 wdstrategy(struct buf *bp) 383 { 384 struct wd_softc *wd; 385 int s; 386 387 wd = wdlookup(DISKUNIT(bp->b_dev)); 388 if (wd == NULL) { 389 bp->b_error = ENXIO; 390 goto bad; 391 } 392 393 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname), 394 DEBUG_XFERS); 395 396 /* If device invalidated (e.g. media change, door open), error. */ 397 if ((wd->sc_flags & WDF_LOADED) == 0) { 398 bp->b_error = EIO; 399 goto bad; 400 } 401 402 /* Validate the request. */ 403 if (bounds_check_with_label(bp, wd->sc_dk.dk_label) == -1) 404 goto done; 405 406 /* Check that the number of sectors can fit in a byte. */ 407 if ((bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) { 408 bp->b_error = EINVAL; 409 goto bad; 410 } 411 412 /* Queue transfer on drive, activate drive and controller if idle. */ 413 bufq_queue(&wd->sc_bufq, bp); 414 s = splbio(); 415 wdstart(wd); 416 splx(s); 417 device_unref(&wd->sc_dev); 418 return; 419 420 bad: 421 bp->b_flags |= B_ERROR; 422 bp->b_resid = bp->b_bcount; 423 done: 424 s = splbio(); 425 biodone(bp); 426 splx(s); 427 if (wd != NULL) 428 device_unref(&wd->sc_dev); 429 } 430 431 /* 432 * Queue a drive for I/O. 433 */ 434 void 435 wdstart(void *arg) 436 { 437 struct wd_softc *wd = arg; 438 struct buf *bp = NULL; 439 440 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname), 441 DEBUG_XFERS); 442 while (wd->openings > 0) { 443 444 /* Is there a buf for us ? */ 445 if ((bp = bufq_dequeue(&wd->sc_bufq)) == NULL) 446 return; 447 /* 448 * Make the command. First lock the device 449 */ 450 wd->openings--; 451 452 wd->retries = 0; 453 __wdstart(wd, bp); 454 } 455 } 456 457 void 458 __wdstart(struct wd_softc *wd, struct buf *bp) 459 { 460 struct disklabel *lp; 461 u_int64_t nsecs; 462 463 lp = wd->sc_dk.dk_label; 464 wd->sc_wdc_bio.blkno = DL_BLKTOSEC(lp, bp->b_blkno + DL_SECTOBLK(lp, 465 DL_GETPOFFSET(&lp->d_partitions[DISKPART(bp->b_dev)]))); 466 wd->sc_wdc_bio.blkdone =0; 467 wd->sc_bp = bp; 468 /* 469 * If we're retrying, retry in single-sector mode. This will give us 470 * the sector number of the problem, and will eventually allow the 471 * transfer to succeed. 472 */ 473 if (wd->retries >= WDIORETRIES_SINGLE) 474 wd->sc_wdc_bio.flags = ATA_SINGLE; 475 else 476 wd->sc_wdc_bio.flags = 0; 477 nsecs = howmany(bp->b_bcount, lp->d_secsize); 478 if ((wd->sc_flags & WDF_LBA48) && 479 /* use LBA48 only if really need */ 480 ((wd->sc_wdc_bio.blkno + nsecs - 1 >= LBA48_THRESHOLD) || 481 (nsecs > 0xff))) 482 wd->sc_wdc_bio.flags |= ATA_LBA48; 483 if (wd->sc_flags & WDF_LBA) 484 wd->sc_wdc_bio.flags |= ATA_LBA; 485 if (bp->b_flags & B_READ) 486 wd->sc_wdc_bio.flags |= ATA_READ; 487 wd->sc_wdc_bio.bcount = bp->b_bcount; 488 wd->sc_wdc_bio.databuf = bp->b_data; 489 wd->sc_wdc_bio.wd = wd; 490 /* Instrumentation. */ 491 disk_busy(&wd->sc_dk); 492 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) { 493 case WDC_TRY_AGAIN: 494 timeout_add_sec(&wd->sc_restart_timeout, 1); 495 break; 496 case WDC_QUEUED: 497 break; 498 case WDC_COMPLETE: 499 /* 500 * This code is never executed because we never set 501 * the ATA_POLL flag above 502 */ 503 #if 0 504 if (wd->sc_wdc_bio.flags & ATA_POLL) 505 wddone(wd); 506 #endif 507 break; 508 default: 509 panic("__wdstart: bad return code from wdc_ata_bio()"); 510 } 511 } 512 513 void 514 wddone(void *v) 515 { 516 struct wd_softc *wd = v; 517 struct buf *bp = wd->sc_bp; 518 char buf[256], *errbuf = buf; 519 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname), 520 DEBUG_XFERS); 521 522 bp->b_resid = wd->sc_wdc_bio.bcount; 523 errbuf[0] = '\0'; 524 switch (wd->sc_wdc_bio.error) { 525 case ERR_NODEV: 526 bp->b_flags |= B_ERROR; 527 bp->b_error = ENXIO; 528 break; 529 case ERR_DMA: 530 errbuf = "DMA error"; 531 goto retry; 532 case ERR_DF: 533 errbuf = "device fault"; 534 goto retry; 535 case TIMEOUT: 536 errbuf = "device timeout"; 537 goto retry; 538 case ERROR: 539 /* Don't care about media change bits */ 540 if (wd->sc_wdc_bio.r_error != 0 && 541 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0) 542 goto noerror; 543 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf, 544 sizeof buf); 545 retry: 546 /* Just reset and retry. Can we do more ? */ 547 wdc_reset_channel(wd->drvp, 0); 548 diskerr(bp, "wd", errbuf, LOG_PRINTF, 549 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label); 550 if (wd->retries++ < WDIORETRIES) { 551 printf(", retrying\n"); 552 timeout_add_msec(&wd->sc_restart_timeout, 553 RECOVERYTIME_MSEC); 554 return; 555 } 556 printf("\n"); 557 bp->b_flags |= B_ERROR; 558 bp->b_error = EIO; 559 break; 560 case NOERROR: 561 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0) 562 printf("%s: soft error (corrected)\n", 563 wd->sc_dev.dv_xname); 564 } 565 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid), 566 bp->b_blkno, (bp->b_flags & B_READ)); 567 biodone(bp); 568 wd->openings++; 569 wdstart(wd); 570 } 571 572 void 573 wdrestart(void *v) 574 { 575 struct wd_softc *wd = v; 576 struct buf *bp = wd->sc_bp; 577 struct channel_softc *chnl; 578 int s; 579 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname), 580 DEBUG_XFERS); 581 582 chnl = (struct channel_softc *)(wd->drvp->chnl_softc); 583 if (chnl->dying) 584 return; 585 586 s = splbio(); 587 disk_unbusy(&wd->sc_dk, 0, 0, (bp->b_flags & B_READ)); 588 __wdstart(v, bp); 589 splx(s); 590 } 591 592 int 593 wdread(dev_t dev, struct uio *uio, int flags) 594 { 595 596 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS); 597 return (physio(wdstrategy, dev, B_READ, minphys, uio)); 598 } 599 600 int 601 wdwrite(dev_t dev, struct uio *uio, int flags) 602 { 603 604 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS); 605 return (physio(wdstrategy, dev, B_WRITE, minphys, uio)); 606 } 607 608 int 609 wdopen(dev_t dev, int flag, int fmt, struct proc *p) 610 { 611 struct wd_softc *wd; 612 struct channel_softc *chnl; 613 int unit, part; 614 int error; 615 616 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS); 617 618 unit = DISKUNIT(dev); 619 wd = wdlookup(unit); 620 if (wd == NULL) 621 return ENXIO; 622 chnl = (struct channel_softc *)(wd->drvp->chnl_softc); 623 if (chnl->dying) 624 return (ENXIO); 625 626 /* 627 * If this is the first open of this device, add a reference 628 * to the adapter. 629 */ 630 if ((error = disk_lock(&wd->sc_dk)) != 0) 631 goto bad4; 632 633 if (wd->sc_dk.dk_openmask != 0) { 634 /* 635 * If any partition is open, but the disk has been invalidated, 636 * disallow further opens. 637 */ 638 if ((wd->sc_flags & WDF_LOADED) == 0) { 639 error = EIO; 640 goto bad3; 641 } 642 } else { 643 if ((wd->sc_flags & WDF_LOADED) == 0) { 644 wd->sc_flags |= WDF_LOADED; 645 646 /* Load the physical device parameters. */ 647 wd_get_params(wd, AT_WAIT, &wd->sc_params); 648 649 /* Load the partition info if not already loaded. */ 650 if (wdgetdisklabel(dev, wd, 651 wd->sc_dk.dk_label, 0) == EIO) { 652 error = EIO; 653 goto bad; 654 } 655 } 656 } 657 658 part = DISKPART(dev); 659 660 if ((error = disk_openpart(&wd->sc_dk, part, fmt, 1)) != 0) 661 goto bad; 662 663 disk_unlock(&wd->sc_dk); 664 device_unref(&wd->sc_dev); 665 return 0; 666 667 bad: 668 if (wd->sc_dk.dk_openmask == 0) { 669 } 670 671 bad3: 672 disk_unlock(&wd->sc_dk); 673 bad4: 674 device_unref(&wd->sc_dev); 675 return error; 676 } 677 678 int 679 wdclose(dev_t dev, int flag, int fmt, struct proc *p) 680 { 681 struct wd_softc *wd; 682 int part = DISKPART(dev); 683 684 wd = wdlookup(DISKUNIT(dev)); 685 if (wd == NULL) 686 return ENXIO; 687 688 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS); 689 690 disk_lock_nointr(&wd->sc_dk); 691 692 disk_closepart(&wd->sc_dk, part, fmt); 693 694 if (wd->sc_dk.dk_openmask == 0) { 695 wd_flushcache(wd, AT_WAIT); 696 /* XXXX Must wait for I/O to complete! */ 697 } 698 699 disk_unlock(&wd->sc_dk); 700 701 device_unref(&wd->sc_dev); 702 return (0); 703 } 704 705 void 706 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp) 707 { 708 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS); 709 bzero(lp, sizeof(struct disklabel)); 710 711 lp->d_secsize = DEV_BSIZE; 712 DL_SETDSIZE(lp, wd->sc_capacity); 713 lp->d_ntracks = wd->sc_params.atap_heads; 714 lp->d_nsectors = wd->sc_params.atap_sectors; 715 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 716 lp->d_ncylinders = DL_GETDSIZE(lp) / lp->d_secpercyl; 717 if (wd->drvp->ata_vers == -1) { 718 lp->d_type = DTYPE_ST506; 719 strncpy(lp->d_typename, "ST506/MFM/RLL", sizeof lp->d_typename); 720 } else { 721 lp->d_type = DTYPE_ESDI; 722 strncpy(lp->d_typename, "ESDI/IDE disk", sizeof lp->d_typename); 723 } 724 /* XXX - user viscopy() like sd.c */ 725 strncpy(lp->d_packname, wd->sc_params.atap_model, sizeof lp->d_packname); 726 lp->d_version = 1; 727 728 lp->d_magic = DISKMAGIC; 729 lp->d_magic2 = DISKMAGIC; 730 lp->d_checksum = dkcksum(lp); 731 } 732 733 /* 734 * Fabricate a default disk label, and try to read the correct one. 735 */ 736 int 737 wdgetdisklabel(dev_t dev, struct wd_softc *wd, struct disklabel *lp, 738 int spoofonly) 739 { 740 int error; 741 742 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS); 743 744 wdgetdefaultlabel(wd, lp); 745 746 if (wd->drvp->state > RECAL) 747 wd->drvp->drive_flags |= DRIVE_RESET; 748 error = readdisklabel(DISKLABELDEV(dev), wdstrategy, lp, 749 spoofonly); 750 if (wd->drvp->state > RECAL) 751 wd->drvp->drive_flags |= DRIVE_RESET; 752 return (error); 753 } 754 755 int 756 wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p) 757 { 758 struct wd_softc *wd; 759 struct disklabel *lp; 760 int error = 0; 761 762 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS); 763 764 wd = wdlookup(DISKUNIT(dev)); 765 if (wd == NULL) 766 return ENXIO; 767 768 if ((wd->sc_flags & WDF_LOADED) == 0) { 769 error = EIO; 770 goto exit; 771 } 772 773 switch (xfer) { 774 case DIOCRLDINFO: 775 lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK); 776 wdgetdisklabel(dev, wd, lp, 0); 777 bcopy(lp, wd->sc_dk.dk_label, sizeof(*lp)); 778 free(lp, M_TEMP, sizeof(*lp)); 779 goto exit; 780 781 case DIOCGPDINFO: 782 wdgetdisklabel(dev, wd, (struct disklabel *)addr, 1); 783 goto exit; 784 785 case DIOCGDINFO: 786 *(struct disklabel *)addr = *(wd->sc_dk.dk_label); 787 goto exit; 788 789 case DIOCGPART: 790 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label; 791 ((struct partinfo *)addr)->part = 792 &wd->sc_dk.dk_label->d_partitions[DISKPART(dev)]; 793 goto exit; 794 795 case DIOCWDINFO: 796 case DIOCSDINFO: 797 if ((flag & FWRITE) == 0) { 798 error = EBADF; 799 goto exit; 800 } 801 802 if ((error = disk_lock(&wd->sc_dk)) != 0) 803 goto exit; 804 805 error = setdisklabel(wd->sc_dk.dk_label, 806 (struct disklabel *)addr, wd->sc_dk.dk_openmask); 807 if (error == 0) { 808 if (wd->drvp->state > RECAL) 809 wd->drvp->drive_flags |= DRIVE_RESET; 810 if (xfer == DIOCWDINFO) 811 error = writedisklabel(DISKLABELDEV(dev), 812 wdstrategy, wd->sc_dk.dk_label); 813 } 814 815 disk_unlock(&wd->sc_dk); 816 goto exit; 817 818 case DIOCCACHESYNC: 819 if ((flag & FWRITE) == 0) { 820 error = EBADF; 821 goto exit; 822 } 823 error = wd_flushcache(wd, AT_WAIT); 824 goto exit; 825 826 default: 827 error = wdc_ioctl(wd->drvp, xfer, addr, flag, p); 828 goto exit; 829 } 830 831 #ifdef DIAGNOSTIC 832 panic("wdioctl: impossible"); 833 #endif 834 835 exit: 836 device_unref(&wd->sc_dev); 837 return (error); 838 } 839 840 #ifdef B_FORMAT 841 int 842 wdformat(struct buf *bp) 843 { 844 845 bp->b_flags |= B_FORMAT; 846 return wdstrategy(bp); 847 } 848 #endif 849 850 daddr_t 851 wdsize(dev_t dev) 852 { 853 struct wd_softc *wd; 854 struct disklabel *lp; 855 int part, omask; 856 daddr_t size; 857 858 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS); 859 860 wd = wdlookup(DISKUNIT(dev)); 861 if (wd == NULL) 862 return (-1); 863 864 part = DISKPART(dev); 865 omask = wd->sc_dk.dk_openmask & (1 << part); 866 867 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0) { 868 size = -1; 869 goto exit; 870 } 871 872 lp = wd->sc_dk.dk_label; 873 size = DL_SECTOBLK(lp, DL_GETPSIZE(&lp->d_partitions[part])); 874 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0) 875 size = -1; 876 877 exit: 878 device_unref(&wd->sc_dev); 879 return (size); 880 } 881 882 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */ 883 static int wddoingadump = 0; 884 static int wddumprecalibrated = 0; 885 static int wddumpmulti = 1; 886 887 /* 888 * Dump core after a system crash. 889 */ 890 int 891 wddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 892 { 893 struct wd_softc *wd; /* disk unit to do the I/O */ 894 struct disklabel *lp; /* disk's disklabel */ 895 int unit, part; 896 int nblks; /* total number of sectors left to write */ 897 int nwrt; /* sectors to write with current i/o. */ 898 int err; 899 char errbuf[256]; 900 901 /* Check if recursive dump; if so, punt. */ 902 if (wddoingadump) 903 return EFAULT; 904 wddoingadump = 1; 905 906 unit = DISKUNIT(dev); 907 wd = wdlookup(unit); 908 if (wd == NULL) 909 return ENXIO; 910 911 part = DISKPART(dev); 912 913 /* Make sure it was initialized. */ 914 if (wd->drvp->state < READY) 915 return ENXIO; 916 917 /* Convert to disk sectors. Request must be a multiple of size. */ 918 lp = wd->sc_dk.dk_label; 919 if ((size % lp->d_secsize) != 0) 920 return EFAULT; 921 nblks = size / lp->d_secsize; 922 blkno = blkno / (lp->d_secsize / DEV_BSIZE); 923 924 /* Check transfer bounds against partition size. */ 925 if ((blkno < 0) || ((blkno + nblks) > DL_GETPSIZE(&lp->d_partitions[part]))) 926 return EINVAL; 927 928 /* Offset block number to start of partition. */ 929 blkno += DL_GETPOFFSET(&lp->d_partitions[part]); 930 931 /* Recalibrate, if first dump transfer. */ 932 if (wddumprecalibrated == 0) { 933 wddumpmulti = wd->sc_multi; 934 wddumprecalibrated = 1; 935 wd->drvp->state = RECAL; 936 } 937 938 while (nblks > 0) { 939 nwrt = min(nblks, wddumpmulti); 940 wd->sc_wdc_bio.blkno = blkno; 941 wd->sc_wdc_bio.flags = ATA_POLL; 942 if (wd->sc_flags & WDF_LBA48) 943 wd->sc_wdc_bio.flags |= ATA_LBA48; 944 if (wd->sc_flags & WDF_LBA) 945 wd->sc_wdc_bio.flags |= ATA_LBA; 946 wd->sc_wdc_bio.bcount = nwrt * lp->d_secsize; 947 wd->sc_wdc_bio.databuf = va; 948 wd->sc_wdc_bio.wd = wd; 949 #ifndef WD_DUMP_NOT_TRUSTED 950 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) { 951 case WDC_TRY_AGAIN: 952 panic("wddump: try again"); 953 break; 954 case WDC_QUEUED: 955 panic("wddump: polled command has been queued"); 956 break; 957 case WDC_COMPLETE: 958 break; 959 } 960 switch(wd->sc_wdc_bio.error) { 961 case TIMEOUT: 962 printf("wddump: device timed out"); 963 err = EIO; 964 break; 965 case ERR_DF: 966 printf("wddump: drive fault"); 967 err = EIO; 968 break; 969 case ERR_DMA: 970 printf("wddump: DMA error"); 971 err = EIO; 972 break; 973 case ERROR: 974 errbuf[0] = '\0'; 975 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf, 976 sizeof errbuf); 977 printf("wddump: %s", errbuf); 978 err = EIO; 979 break; 980 case NOERROR: 981 err = 0; 982 break; 983 default: 984 panic("wddump: unknown error type"); 985 } 986 if (err != 0) { 987 printf("\n"); 988 return err; 989 } 990 #else /* WD_DUMP_NOT_TRUSTED */ 991 /* Let's just talk about this first... */ 992 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n", 993 unit, va, cylin, head, sector); 994 delay(500 * 1000); /* half a second */ 995 #endif 996 997 /* update block count */ 998 nblks -= nwrt; 999 blkno += nwrt; 1000 va += nwrt * lp->d_secsize; 1001 } 1002 1003 wddoingadump = 0; 1004 return 0; 1005 } 1006 1007 int 1008 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params) 1009 { 1010 switch (ata_get_params(wd->drvp, flags, params)) { 1011 case CMD_AGAIN: 1012 return 1; 1013 case CMD_ERR: 1014 /* If we already have drive parameters, reuse them. */ 1015 if (wd->sc_params.atap_cylinders != 0) { 1016 if (params != &wd->sc_params) 1017 bcopy(&wd->sc_params, params, 1018 sizeof(struct ataparams)); 1019 return 0; 1020 } 1021 /* 1022 * We `know' there's a drive here; just assume it's old. 1023 * This geometry is only used to read the MBR and print a 1024 * (false) attach message. 1025 */ 1026 bzero(params, sizeof(struct ataparams)); 1027 strncpy(params->atap_model, "ST506", 1028 sizeof params->atap_model); 1029 params->atap_config = ATA_CFG_FIXED; 1030 params->atap_cylinders = 1024; 1031 params->atap_heads = 8; 1032 params->atap_sectors = 17; 1033 params->atap_multi = 1; 1034 params->atap_capabilities1 = params->atap_capabilities2 = 0; 1035 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */ 1036 return 0; 1037 case CMD_OK: 1038 return 0; 1039 default: 1040 panic("wd_get_params: bad return code from ata_get_params"); 1041 /* NOTREACHED */ 1042 } 1043 } 1044 1045 int 1046 wd_flushcache(struct wd_softc *wd, int flags) 1047 { 1048 struct wdc_command wdc_c; 1049 1050 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */ 1051 return EIO; 1052 bzero(&wdc_c, sizeof(struct wdc_command)); 1053 wdc_c.r_command = (wd->sc_flags & WDF_LBA48 ? WDCC_FLUSHCACHE_EXT : 1054 WDCC_FLUSHCACHE); 1055 wdc_c.r_st_bmask = WDCS_DRDY; 1056 wdc_c.r_st_pmask = WDCS_DRDY; 1057 wdc_c.flags = flags; 1058 wdc_c.timeout = 30000; /* 30s timeout */ 1059 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) { 1060 printf("%s: flush cache command didn't complete\n", 1061 wd->sc_dev.dv_xname); 1062 return EIO; 1063 } 1064 if (wdc_c.flags & ERR_NODEV) 1065 return ENODEV; 1066 if (wdc_c.flags & AT_TIMEOU) { 1067 printf("%s: flush cache command timeout\n", 1068 wd->sc_dev.dv_xname); 1069 return EIO; 1070 } 1071 if (wdc_c.flags & AT_ERROR) { 1072 if (wdc_c.r_error == WDCE_ABRT) /* command not supported */ 1073 return ENODEV; 1074 printf("%s: flush cache command: error 0x%x\n", 1075 wd->sc_dev.dv_xname, wdc_c.r_error); 1076 return EIO; 1077 } 1078 if (wdc_c.flags & AT_DF) { 1079 printf("%s: flush cache command: drive fault\n", 1080 wd->sc_dev.dv_xname); 1081 return EIO; 1082 } 1083 return 0; 1084 } 1085 1086 void 1087 wd_standby(struct wd_softc *wd, int flags) 1088 { 1089 struct wdc_command wdc_c; 1090 1091 bzero(&wdc_c, sizeof(struct wdc_command)); 1092 wdc_c.r_command = WDCC_STANDBY_IMMED; 1093 wdc_c.r_st_bmask = WDCS_DRDY; 1094 wdc_c.r_st_pmask = WDCS_DRDY; 1095 wdc_c.flags = flags; 1096 wdc_c.timeout = 30000; /* 30s timeout */ 1097 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) { 1098 printf("%s: standby command didn't complete\n", 1099 wd->sc_dev.dv_xname); 1100 } 1101 if (wdc_c.flags & AT_TIMEOU) { 1102 printf("%s: standby command timeout\n", 1103 wd->sc_dev.dv_xname); 1104 } 1105 if (wdc_c.flags & AT_DF) { 1106 printf("%s: standby command: drive fault\n", 1107 wd->sc_dev.dv_xname); 1108 } 1109 /* 1110 * Ignore error register, it shouldn't report anything else 1111 * than COMMAND ABORTED, which means the device doesn't support 1112 * standby 1113 */ 1114 } 1115