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