1 /* $NetBSD: wd.c,v 1.385 2010/11/05 15:49:37 dyoung 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /*- 28 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc. 29 * All rights reserved. 30 * 31 * This code is derived from software contributed to The NetBSD Foundation 32 * by Charles M. Hannum and by Onno van der Linden. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 53 * POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 #include <sys/cdefs.h> 57 __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.385 2010/11/05 15:49:37 dyoung Exp $"); 58 59 #include "opt_ata.h" 60 61 #include "rnd.h" 62 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/kernel.h> 66 #include <sys/conf.h> 67 #include <sys/file.h> 68 #include <sys/stat.h> 69 #include <sys/ioctl.h> 70 #include <sys/buf.h> 71 #include <sys/bufq.h> 72 #include <sys/uio.h> 73 #include <sys/malloc.h> 74 #include <sys/device.h> 75 #include <sys/disklabel.h> 76 #include <sys/disk.h> 77 #include <sys/syslog.h> 78 #include <sys/proc.h> 79 #include <sys/reboot.h> 80 #include <sys/vnode.h> 81 #if NRND > 0 82 #include <sys/rnd.h> 83 #endif 84 85 #include <sys/intr.h> 86 #include <sys/bus.h> 87 88 #include <dev/ata/atareg.h> 89 #include <dev/ata/atavar.h> 90 #include <dev/ata/wdvar.h> 91 #include <dev/ic/wdcreg.h> 92 #include <sys/ataio.h> 93 #include "locators.h" 94 95 #include <prop/proplib.h> 96 97 #define WDIORETRIES_SINGLE 4 /* number of retries before single-sector */ 98 #define WDIORETRIES 5 /* number of retries before giving up */ 99 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */ 100 101 #define WDUNIT(dev) DISKUNIT(dev) 102 #define WDPART(dev) DISKPART(dev) 103 #define WDMINOR(unit, part) DISKMINOR(unit, part) 104 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 105 106 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART)) 107 108 #define DEBUG_INTR 0x01 109 #define DEBUG_XFERS 0x02 110 #define DEBUG_STATUS 0x04 111 #define DEBUG_FUNCS 0x08 112 #define DEBUG_PROBE 0x10 113 #ifdef ATADEBUG 114 int wdcdebug_wd_mask = 0x0; 115 #define ATADEBUG_PRINT(args, level) \ 116 if (wdcdebug_wd_mask & (level)) \ 117 printf args 118 #else 119 #define ATADEBUG_PRINT(args, level) 120 #endif 121 122 int wdprobe(device_t, cfdata_t, void *); 123 void wdattach(device_t, device_t, void *); 124 int wddetach(device_t, int); 125 int wdprint(void *, char *); 126 void wdperror(const struct wd_softc *); 127 128 static int wdlastclose(device_t); 129 static bool wd_suspend(device_t, const pmf_qual_t *); 130 static int wd_standby(struct wd_softc *, int); 131 132 CFATTACH_DECL3_NEW(wd, sizeof(struct wd_softc), 133 wdprobe, wdattach, wddetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); 134 135 extern struct cfdriver wd_cd; 136 137 dev_type_open(wdopen); 138 dev_type_close(wdclose); 139 dev_type_read(wdread); 140 dev_type_write(wdwrite); 141 dev_type_ioctl(wdioctl); 142 dev_type_strategy(wdstrategy); 143 dev_type_dump(wddump); 144 dev_type_size(wdsize); 145 146 const struct bdevsw wd_bdevsw = { 147 wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK 148 }; 149 150 const struct cdevsw wd_cdevsw = { 151 wdopen, wdclose, wdread, wdwrite, wdioctl, 152 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 153 }; 154 155 /* 156 * Glue necessary to hook WDCIOCCOMMAND into physio 157 */ 158 159 struct wd_ioctl { 160 LIST_ENTRY(wd_ioctl) wi_list; 161 struct buf wi_bp; 162 struct uio wi_uio; 163 struct iovec wi_iov; 164 atareq_t wi_atareq; 165 struct wd_softc *wi_softc; 166 }; 167 168 LIST_HEAD(, wd_ioctl) wi_head; 169 170 struct wd_ioctl *wi_find(struct buf *); 171 void wi_free(struct wd_ioctl *); 172 struct wd_ioctl *wi_get(void); 173 void wdioctlstrategy(struct buf *); 174 175 void wdgetdefaultlabel(struct wd_softc *, struct disklabel *); 176 void wdgetdisklabel(struct wd_softc *); 177 void wdstart(void *); 178 void wdstart1(struct wd_softc*, struct buf *); 179 void wdrestart(void *); 180 void wddone(void *); 181 int wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *); 182 int wd_flushcache(struct wd_softc *, int); 183 bool wd_shutdown(device_t, int); 184 185 int wd_getcache(struct wd_softc *, int *); 186 int wd_setcache(struct wd_softc *, int); 187 188 struct dkdriver wddkdriver = { wdstrategy, minphys }; 189 190 #ifdef HAS_BAD144_HANDLING 191 static void bad144intern(struct wd_softc *); 192 #endif 193 194 #define WD_QUIRK_SPLIT_MOD15_WRITE 0x0001 /* must split certain writes */ 195 196 #define WD_QUIRK_FMT "\20\1SPLIT_MOD15_WRITE\2FORCE_LBA48" 197 198 /* 199 * Quirk table for IDE drives. Put more-specific matches first, since 200 * a simple globbing routine is used for matching. 201 */ 202 static const struct wd_quirk { 203 const char *wdq_match; /* inquiry pattern to match */ 204 int wdq_quirks; /* drive quirks */ 205 } wd_quirk_table[] = { 206 /* 207 * Some Seagate S-ATA drives have a PHY which can get confused 208 * with the way data is packetized by some S-ATA controllers. 209 * 210 * The work-around is to split in two any write transfer whose 211 * sector count % 15 == 1 (assuming 512 byte sectors). 212 * 213 * XXX This is an incomplete list. There are at least a couple 214 * XXX more model numbers. If you have trouble with such transfers 215 * XXX (8K is the most common) on Seagate S-ATA drives, please 216 * XXX notify thorpej@NetBSD.org. 217 */ 218 { "ST3120023AS", 219 WD_QUIRK_SPLIT_MOD15_WRITE }, 220 { "ST380023AS", 221 WD_QUIRK_SPLIT_MOD15_WRITE }, 222 { NULL, 223 0 } 224 }; 225 226 static const struct wd_quirk * 227 wd_lookup_quirks(const char *name) 228 { 229 const struct wd_quirk *wdq; 230 const char *estr; 231 232 for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) { 233 /* 234 * We only want exact matches (which include matches 235 * against globbing characters). 236 */ 237 if (pmatch(name, wdq->wdq_match, &estr) == 2) 238 return (wdq); 239 } 240 return (NULL); 241 } 242 243 int 244 wdprobe(device_t parent, cfdata_t match, void *aux) 245 { 246 struct ata_device *adev = aux; 247 248 if (adev == NULL) 249 return 0; 250 if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA) 251 return 0; 252 253 if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT && 254 match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive) 255 return 0; 256 return 1; 257 } 258 259 void 260 wdattach(device_t parent, device_t self, void *aux) 261 { 262 struct wd_softc *wd = device_private(self); 263 struct ata_device *adev= aux; 264 int i, blank; 265 char tbuf[41], pbuf[9], c, *p, *q; 266 const struct wd_quirk *wdq; 267 268 wd->sc_dev = self; 269 270 ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE); 271 callout_init(&wd->sc_restart_ch, 0); 272 bufq_alloc(&wd->sc_q, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK); 273 #ifdef WD_SOFTBADSECT 274 SLIST_INIT(&wd->sc_bslist); 275 #endif 276 wd->atabus = adev->adev_bustype; 277 wd->openings = adev->adev_openings; 278 wd->drvp = adev->adev_drv_data; 279 280 wd->drvp->drv_done = wddone; 281 wd->drvp->drv_softc = wd->sc_dev; 282 283 aprint_naive("\n"); 284 285 /* read our drive info */ 286 if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) { 287 aprint_error("\n%s: IDENTIFY failed\n", device_xname(self)); 288 return; 289 } 290 291 for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0; 292 i < sizeof(wd->sc_params.atap_model); i++) { 293 c = *p++; 294 if (c == '\0') 295 break; 296 if (c != ' ') { 297 if (blank) { 298 *q++ = ' '; 299 blank = 0; 300 } 301 *q++ = c; 302 } else 303 blank = 1; 304 } 305 *q++ = '\0'; 306 307 aprint_normal(": <%s>\n", tbuf); 308 309 wdq = wd_lookup_quirks(tbuf); 310 if (wdq != NULL) 311 wd->sc_quirks = wdq->wdq_quirks; 312 313 if (wd->sc_quirks != 0) { 314 char sbuf[sizeof(WD_QUIRK_FMT) + 64]; 315 snprintb(sbuf, sizeof(sbuf), WD_QUIRK_FMT, wd->sc_quirks); 316 aprint_normal_dev(self, "quirks %s\n", sbuf); 317 } 318 319 if ((wd->sc_params.atap_multi & 0xff) > 1) { 320 wd->sc_multi = wd->sc_params.atap_multi & 0xff; 321 } else { 322 wd->sc_multi = 1; 323 } 324 325 aprint_verbose_dev(self, "drive supports %d-sector PIO transfers,", 326 wd->sc_multi); 327 328 /* 48-bit LBA addressing */ 329 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) 330 wd->sc_flags |= WDF_LBA48; 331 332 /* Prior to ATA-4, LBA was optional. */ 333 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) 334 wd->sc_flags |= WDF_LBA; 335 #if 0 336 /* ATA-4 requires LBA. */ 337 if (wd->sc_params.atap_ataversion != 0xffff && 338 wd->sc_params.atap_ataversion >= WDC_VER_ATA4) 339 wd->sc_flags |= WDF_LBA; 340 #endif 341 342 if ((wd->sc_flags & WDF_LBA48) != 0) { 343 aprint_verbose(" LBA48 addressing\n"); 344 wd->sc_capacity = 345 ((u_int64_t) wd->sc_params.atap_max_lba[3] << 48) | 346 ((u_int64_t) wd->sc_params.atap_max_lba[2] << 32) | 347 ((u_int64_t) wd->sc_params.atap_max_lba[1] << 16) | 348 ((u_int64_t) wd->sc_params.atap_max_lba[0] << 0); 349 wd->sc_capacity28 = 350 (wd->sc_params.atap_capacity[1] << 16) | 351 wd->sc_params.atap_capacity[0]; 352 } else if ((wd->sc_flags & WDF_LBA) != 0) { 353 aprint_verbose(" LBA addressing\n"); 354 wd->sc_capacity28 = wd->sc_capacity = 355 (wd->sc_params.atap_capacity[1] << 16) | 356 wd->sc_params.atap_capacity[0]; 357 } else { 358 aprint_verbose(" chs addressing\n"); 359 wd->sc_capacity28 = wd->sc_capacity = 360 wd->sc_params.atap_cylinders * 361 wd->sc_params.atap_heads * 362 wd->sc_params.atap_sectors; 363 } 364 format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * DEV_BSIZE); 365 aprint_normal_dev(self, "%s, %d cyl, %d head, %d sec, " 366 "%d bytes/sect x %llu sectors\n", 367 pbuf, 368 (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity / 369 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) : 370 wd->sc_params.atap_cylinders, 371 wd->sc_params.atap_heads, wd->sc_params.atap_sectors, 372 DEV_BSIZE, (unsigned long long)wd->sc_capacity); 373 374 ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n", 375 device_xname(self), wd->sc_params.atap_dmatiming_mimi, 376 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE); 377 /* 378 * Initialize and attach the disk structure. 379 */ 380 /* we fill in dk_info later */ 381 disk_init(&wd->sc_dk, device_xname(wd->sc_dev), &wddkdriver); 382 disk_attach(&wd->sc_dk); 383 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label; 384 #if NRND > 0 385 rnd_attach_source(&wd->rnd_source, device_xname(wd->sc_dev), 386 RND_TYPE_DISK, 0); 387 #endif 388 389 /* Discover wedges on this disk. */ 390 dkwedge_discover(&wd->sc_dk); 391 392 if (!pmf_device_register1(self, wd_suspend, NULL, wd_shutdown)) 393 aprint_error_dev(self, "couldn't establish power handler\n"); 394 } 395 396 static bool 397 wd_suspend(device_t dv, const pmf_qual_t *qual) 398 { 399 struct wd_softc *sc = device_private(dv); 400 401 wd_flushcache(sc, AT_WAIT); 402 wd_standby(sc, AT_WAIT); 403 return true; 404 } 405 406 int 407 wddetach(device_t self, int flags) 408 { 409 struct wd_softc *sc = device_private(self); 410 int bmaj, cmaj, i, mn, rc, s; 411 412 if ((rc = disk_begindetach(&sc->sc_dk, wdlastclose, self, flags)) != 0) 413 return rc; 414 415 /* locate the major number */ 416 bmaj = bdevsw_lookup_major(&wd_bdevsw); 417 cmaj = cdevsw_lookup_major(&wd_cdevsw); 418 419 /* Nuke the vnodes for any open instances. */ 420 for (i = 0; i < MAXPARTITIONS; i++) { 421 mn = WDMINOR(device_unit(self), i); 422 vdevgone(bmaj, mn, mn, VBLK); 423 vdevgone(cmaj, mn, mn, VCHR); 424 } 425 426 /* Delete all of our wedges. */ 427 dkwedge_delall(&sc->sc_dk); 428 429 s = splbio(); 430 431 /* Kill off any queued buffers. */ 432 bufq_drain(sc->sc_q); 433 434 bufq_free(sc->sc_q); 435 sc->atabus->ata_killpending(sc->drvp); 436 437 splx(s); 438 439 /* Detach disk. */ 440 disk_detach(&sc->sc_dk); 441 disk_destroy(&sc->sc_dk); 442 443 #ifdef WD_SOFTBADSECT 444 /* Clean out the bad sector list */ 445 while (!SLIST_EMPTY(&sc->sc_bslist)) { 446 void *head = SLIST_FIRST(&sc->sc_bslist); 447 SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next); 448 free(head, M_TEMP); 449 } 450 sc->sc_bscount = 0; 451 #endif 452 453 pmf_device_deregister(self); 454 455 #if NRND > 0 456 /* Unhook the entropy source. */ 457 rnd_detach_source(&sc->rnd_source); 458 #endif 459 460 callout_destroy(&sc->sc_restart_ch); 461 462 sc->drvp->drive_flags = 0; /* no drive any more here */ 463 464 return (0); 465 } 466 467 /* 468 * Read/write routine for a buffer. Validates the arguments and schedules the 469 * transfer. Does not wait for the transfer to complete. 470 */ 471 void 472 wdstrategy(struct buf *bp) 473 { 474 struct wd_softc *wd = 475 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev)); 476 struct disklabel *lp = wd->sc_dk.dk_label; 477 daddr_t blkno; 478 int s; 479 480 ATADEBUG_PRINT(("wdstrategy (%s)\n", device_xname(wd->sc_dev)), 481 DEBUG_XFERS); 482 483 /* Valid request? */ 484 if (bp->b_blkno < 0 || 485 (bp->b_bcount % lp->d_secsize) != 0 || 486 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) { 487 bp->b_error = EINVAL; 488 goto done; 489 } 490 491 /* If device invalidated (e.g. media change, door open, 492 * device detachment), then error. 493 */ 494 if ((wd->sc_flags & WDF_LOADED) == 0 || 495 !device_is_enabled(wd->sc_dev)) { 496 bp->b_error = EIO; 497 goto done; 498 } 499 500 /* If it's a null transfer, return immediately. */ 501 if (bp->b_bcount == 0) 502 goto done; 503 504 /* 505 * Do bounds checking, adjust transfer. if error, process. 506 * If end of partition, just return. 507 */ 508 if (WDPART(bp->b_dev) == RAW_PART) { 509 if (bounds_check_with_mediasize(bp, DEV_BSIZE, 510 wd->sc_capacity) <= 0) 511 goto done; 512 } else { 513 if (bounds_check_with_label(&wd->sc_dk, bp, 514 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0) 515 goto done; 516 } 517 518 /* 519 * Now convert the block number to absolute and put it in 520 * terms of the device's logical block size. 521 */ 522 if (lp->d_secsize >= DEV_BSIZE) 523 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 524 else 525 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 526 527 if (WDPART(bp->b_dev) != RAW_PART) 528 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset; 529 530 bp->b_rawblkno = blkno; 531 532 #ifdef WD_SOFTBADSECT 533 /* 534 * If the transfer about to be attempted contains only a block that 535 * is known to be bad then return an error for the transfer without 536 * even attempting to start a transfer up under the premis that we 537 * will just end up doing more retries for a transfer that will end 538 * up failing again. 539 * XXX:SMP - mutex required to protect with DIOCBSFLUSH 540 */ 541 if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) { 542 struct disk_badsectors *dbs; 543 daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1; 544 545 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) 546 if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) || 547 (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){ 548 bp->b_error = EIO; 549 goto done; 550 } 551 } 552 #endif 553 554 /* Queue transfer on drive, activate drive and controller if idle. */ 555 s = splbio(); 556 bufq_put(wd->sc_q, bp); 557 wdstart(wd); 558 splx(s); 559 return; 560 done: 561 /* Toss transfer; we're done early. */ 562 bp->b_resid = bp->b_bcount; 563 biodone(bp); 564 } 565 566 /* 567 * Queue a drive for I/O. 568 */ 569 void 570 wdstart(void *arg) 571 { 572 struct wd_softc *wd = arg; 573 struct buf *bp = NULL; 574 575 ATADEBUG_PRINT(("wdstart %s\n", device_xname(wd->sc_dev)), 576 DEBUG_XFERS); 577 578 if (!device_is_active(wd->sc_dev)) 579 return; 580 581 while (wd->openings > 0) { 582 583 /* Is there a buf for us ? */ 584 if ((bp = bufq_get(wd->sc_q)) == NULL) 585 return; 586 587 /* 588 * Make the command. First lock the device 589 */ 590 wd->openings--; 591 592 wd->retries = 0; 593 wdstart1(wd, bp); 594 } 595 } 596 597 static void 598 wd_split_mod15_write(struct buf *bp) 599 { 600 struct buf *obp = bp->b_private; 601 struct wd_softc *sc = 602 device_lookup_private(&wd_cd, DISKUNIT(obp->b_dev)); 603 int s; 604 605 if (__predict_false(bp->b_error != 0)) { 606 /* 607 * Propagate the error. If this was the first half of 608 * the original transfer, make sure to account for that 609 * in the residual. 610 */ 611 if (bp->b_data == obp->b_data) 612 bp->b_resid += bp->b_bcount; 613 goto done; 614 } 615 616 /* 617 * If this was the second half of the transfer, we're all done! 618 */ 619 if (bp->b_data != obp->b_data) 620 goto done; 621 622 /* 623 * Advance the pointer to the second half and issue that command 624 * using the same opening. 625 */ 626 bp->b_flags = obp->b_flags; 627 bp->b_oflags = obp->b_oflags; 628 bp->b_cflags = obp->b_cflags; 629 bp->b_data = (char *)bp->b_data + bp->b_bcount; 630 bp->b_blkno += (bp->b_bcount / 512); 631 bp->b_rawblkno += (bp->b_bcount / 512); 632 s = splbio(); 633 wdstart1(sc, bp); 634 splx(s); 635 return; 636 637 done: 638 obp->b_error = bp->b_error; 639 obp->b_resid = bp->b_resid; 640 s = splbio(); 641 putiobuf(bp); 642 biodone(obp); 643 sc->openings++; 644 splx(s); 645 /* wddone() will call wdstart() */ 646 } 647 648 void 649 wdstart1(struct wd_softc *wd, struct buf *bp) 650 { 651 652 /* 653 * Deal with the "split mod15 write" quirk. We just divide the 654 * transfer in two, doing the first half and then then second half 655 * with the same command opening. 656 * 657 * Note we MUST do this here, because we can't let insertion 658 * into the bufq cause the transfers to be re-merged. 659 */ 660 if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 && 661 (bp->b_flags & B_READ) == 0 && 662 bp->b_bcount > 512 && 663 ((bp->b_bcount / 512) % 15) == 1)) { 664 struct buf *nbp; 665 666 /* already at splbio */ 667 nbp = getiobuf(NULL, false); 668 if (__predict_false(nbp == NULL)) { 669 /* No memory -- fail the iop. */ 670 bp->b_error = ENOMEM; 671 bp->b_resid = bp->b_bcount; 672 biodone(bp); 673 wd->openings++; 674 return; 675 } 676 677 nbp->b_error = 0; 678 nbp->b_proc = bp->b_proc; 679 nbp->b_dev = bp->b_dev; 680 681 nbp->b_bcount = bp->b_bcount / 2; 682 nbp->b_bufsize = bp->b_bcount / 2; 683 nbp->b_data = bp->b_data; 684 685 nbp->b_blkno = bp->b_blkno; 686 nbp->b_rawblkno = bp->b_rawblkno; 687 688 nbp->b_flags = bp->b_flags; 689 nbp->b_oflags = bp->b_oflags; 690 nbp->b_cflags = bp->b_cflags; 691 nbp->b_iodone = wd_split_mod15_write; 692 693 /* Put ptr to orig buf in b_private and use new buf */ 694 nbp->b_private = bp; 695 696 BIO_COPYPRIO(nbp, bp); 697 698 bp = nbp; 699 } 700 701 wd->sc_wdc_bio.blkno = bp->b_rawblkno; 702 wd->sc_wdc_bio.bcount = bp->b_bcount; 703 wd->sc_wdc_bio.databuf = bp->b_data; 704 wd->sc_wdc_bio.blkdone =0; 705 wd->sc_bp = bp; 706 /* 707 * If we're retrying, retry in single-sector mode. This will give us 708 * the sector number of the problem, and will eventually allow the 709 * transfer to succeed. 710 */ 711 if (wd->retries >= WDIORETRIES_SINGLE) 712 wd->sc_wdc_bio.flags = ATA_SINGLE; 713 else 714 wd->sc_wdc_bio.flags = 0; 715 if (wd->sc_flags & WDF_LBA48 && 716 (wd->sc_wdc_bio.blkno + 717 wd->sc_wdc_bio.bcount / wd->sc_dk.dk_label->d_secsize) > 718 wd->sc_capacity28) 719 wd->sc_wdc_bio.flags |= ATA_LBA48; 720 if (wd->sc_flags & WDF_LBA) 721 wd->sc_wdc_bio.flags |= ATA_LBA; 722 if (bp->b_flags & B_READ) 723 wd->sc_wdc_bio.flags |= ATA_READ; 724 /* Instrumentation. */ 725 disk_busy(&wd->sc_dk); 726 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) { 727 case ATACMD_TRY_AGAIN: 728 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd); 729 break; 730 case ATACMD_QUEUED: 731 case ATACMD_COMPLETE: 732 break; 733 default: 734 panic("wdstart1: bad return code from ata_bio()"); 735 } 736 } 737 738 void 739 wddone(void *v) 740 { 741 struct wd_softc *wd = device_private(v); 742 struct buf *bp = wd->sc_bp; 743 const char *errmsg; 744 int do_perror = 0; 745 746 ATADEBUG_PRINT(("wddone %s\n", device_xname(wd->sc_dev)), 747 DEBUG_XFERS); 748 if (bp == NULL) 749 return; 750 bp->b_resid = wd->sc_wdc_bio.bcount; 751 switch (wd->sc_wdc_bio.error) { 752 case ERR_DMA: 753 errmsg = "DMA error"; 754 goto retry; 755 case ERR_DF: 756 errmsg = "device fault"; 757 goto retry; 758 case TIMEOUT: 759 errmsg = "device timeout"; 760 goto retry; 761 case ERR_RESET: 762 errmsg = "channel reset"; 763 goto retry2; 764 case ERROR: 765 /* Don't care about media change bits */ 766 if (wd->sc_wdc_bio.r_error != 0 && 767 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0) 768 goto noerror; 769 errmsg = "error"; 770 do_perror = 1; 771 retry: /* Just reset and retry. Can we do more ? */ 772 (*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD); 773 retry2: 774 diskerr(bp, "wd", errmsg, LOG_PRINTF, 775 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label); 776 if (wd->retries < WDIORETRIES) 777 printf(", retrying"); 778 printf("\n"); 779 if (do_perror) 780 wdperror(wd); 781 if (wd->retries < WDIORETRIES) { 782 wd->retries++; 783 callout_reset(&wd->sc_restart_ch, RECOVERYTIME, 784 wdrestart, wd); 785 return; 786 } 787 788 #ifdef WD_SOFTBADSECT 789 /* 790 * Not all errors indicate a failed block but those that do, 791 * put the block on the bad-block list for the device. Only 792 * do this for reads because the drive should do it for writes, 793 * itself, according to Manuel. 794 */ 795 if ((bp->b_flags & B_READ) && 796 ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) || 797 (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) { 798 struct disk_badsectors *dbs; 799 800 dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK); 801 dbs->dbs_min = bp->b_rawblkno; 802 dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1; 803 microtime(&dbs->dbs_failedat); 804 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next); 805 wd->sc_bscount++; 806 } 807 #endif 808 bp->b_error = EIO; 809 break; 810 case NOERROR: 811 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0) 812 aprint_error_dev(wd->sc_dev, 813 "soft error (corrected)\n"); 814 break; 815 case ERR_NODEV: 816 bp->b_error = EIO; 817 break; 818 } 819 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid), 820 (bp->b_flags & B_READ)); 821 #if NRND > 0 822 rnd_add_uint32(&wd->rnd_source, bp->b_blkno); 823 #endif 824 /* XXX Yuck, but we don't want to increment openings in this case */ 825 if (__predict_false(bp->b_iodone == wd_split_mod15_write)) 826 biodone(bp); 827 else { 828 biodone(bp); 829 wd->openings++; 830 } 831 wdstart(wd); 832 } 833 834 void 835 wdrestart(void *v) 836 { 837 struct wd_softc *wd = v; 838 struct buf *bp = wd->sc_bp; 839 int s; 840 841 ATADEBUG_PRINT(("wdrestart %s\n", device_xname(wd->sc_dev)), 842 DEBUG_XFERS); 843 s = splbio(); 844 wdstart1(v, bp); 845 splx(s); 846 } 847 848 int 849 wdread(dev_t dev, struct uio *uio, int flags) 850 { 851 852 ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS); 853 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio)); 854 } 855 856 int 857 wdwrite(dev_t dev, struct uio *uio, int flags) 858 { 859 860 ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS); 861 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio)); 862 } 863 864 int 865 wdopen(dev_t dev, int flag, int fmt, struct lwp *l) 866 { 867 struct wd_softc *wd; 868 int part, error; 869 870 ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS); 871 wd = device_lookup_private(&wd_cd, WDUNIT(dev)); 872 if (wd == NULL) 873 return (ENXIO); 874 875 if (! device_is_active(wd->sc_dev)) 876 return (ENODEV); 877 878 part = WDPART(dev); 879 880 mutex_enter(&wd->sc_dk.dk_openlock); 881 882 /* 883 * If there are wedges, and this is not RAW_PART, then we 884 * need to fail. 885 */ 886 if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) { 887 error = EBUSY; 888 goto bad1; 889 } 890 891 /* 892 * If this is the first open of this device, add a reference 893 * to the adapter. 894 */ 895 if (wd->sc_dk.dk_openmask == 0 && 896 (error = wd->atabus->ata_addref(wd->drvp)) != 0) 897 goto bad1; 898 899 if (wd->sc_dk.dk_openmask != 0) { 900 /* 901 * If any partition is open, but the disk has been invalidated, 902 * disallow further opens. 903 */ 904 if ((wd->sc_flags & WDF_LOADED) == 0) { 905 error = EIO; 906 goto bad2; 907 } 908 } else { 909 if ((wd->sc_flags & WDF_LOADED) == 0) { 910 wd->sc_flags |= WDF_LOADED; 911 912 /* Load the physical device parameters. */ 913 wd_get_params(wd, AT_WAIT, &wd->sc_params); 914 915 /* Load the partition info if not already loaded. */ 916 wdgetdisklabel(wd); 917 } 918 } 919 920 /* Check that the partition exists. */ 921 if (part != RAW_PART && 922 (part >= wd->sc_dk.dk_label->d_npartitions || 923 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 924 error = ENXIO; 925 goto bad2; 926 } 927 928 /* Insure only one open at a time. */ 929 switch (fmt) { 930 case S_IFCHR: 931 wd->sc_dk.dk_copenmask |= (1 << part); 932 break; 933 case S_IFBLK: 934 wd->sc_dk.dk_bopenmask |= (1 << part); 935 break; 936 } 937 wd->sc_dk.dk_openmask = 938 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask; 939 940 mutex_exit(&wd->sc_dk.dk_openlock); 941 return 0; 942 943 bad2: 944 if (wd->sc_dk.dk_openmask == 0) 945 wd->atabus->ata_delref(wd->drvp); 946 bad1: 947 mutex_exit(&wd->sc_dk.dk_openlock); 948 return error; 949 } 950 951 /* 952 * Caller must hold wd->sc_dk.dk_openlock. 953 */ 954 static int 955 wdlastclose(device_t self) 956 { 957 struct wd_softc *wd = device_private(self); 958 959 wd_flushcache(wd, AT_WAIT); 960 961 if (! (wd->sc_flags & WDF_KLABEL)) 962 wd->sc_flags &= ~WDF_LOADED; 963 964 wd->atabus->ata_delref(wd->drvp); 965 966 return 0; 967 } 968 969 int 970 wdclose(dev_t dev, int flag, int fmt, struct lwp *l) 971 { 972 struct wd_softc *wd = 973 device_lookup_private(&wd_cd, WDUNIT(dev)); 974 int part = WDPART(dev); 975 976 ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS); 977 978 mutex_enter(&wd->sc_dk.dk_openlock); 979 980 switch (fmt) { 981 case S_IFCHR: 982 wd->sc_dk.dk_copenmask &= ~(1 << part); 983 break; 984 case S_IFBLK: 985 wd->sc_dk.dk_bopenmask &= ~(1 << part); 986 break; 987 } 988 wd->sc_dk.dk_openmask = 989 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask; 990 991 if (wd->sc_dk.dk_openmask == 0) 992 wdlastclose(wd->sc_dev); 993 994 mutex_exit(&wd->sc_dk.dk_openlock); 995 return 0; 996 } 997 998 void 999 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp) 1000 { 1001 1002 ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS); 1003 memset(lp, 0, sizeof(struct disklabel)); 1004 1005 lp->d_secsize = DEV_BSIZE; 1006 lp->d_ntracks = wd->sc_params.atap_heads; 1007 lp->d_nsectors = wd->sc_params.atap_sectors; 1008 lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity / 1009 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) : 1010 wd->sc_params.atap_cylinders; 1011 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 1012 1013 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) 1014 lp->d_type = DTYPE_ST506; 1015 else 1016 lp->d_type = DTYPE_ESDI; 1017 1018 strncpy(lp->d_typename, wd->sc_params.atap_model, 16); 1019 strncpy(lp->d_packname, "fictitious", 16); 1020 if (wd->sc_capacity > UINT32_MAX) 1021 lp->d_secperunit = UINT32_MAX; 1022 else 1023 lp->d_secperunit = wd->sc_capacity; 1024 lp->d_rpm = 3600; 1025 lp->d_interleave = 1; 1026 lp->d_flags = 0; 1027 1028 lp->d_partitions[RAW_PART].p_offset = 0; 1029 lp->d_partitions[RAW_PART].p_size = 1030 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 1031 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1032 lp->d_npartitions = RAW_PART + 1; 1033 1034 lp->d_magic = DISKMAGIC; 1035 lp->d_magic2 = DISKMAGIC; 1036 lp->d_checksum = dkcksum(lp); 1037 } 1038 1039 /* 1040 * Fabricate a default disk label, and try to read the correct one. 1041 */ 1042 void 1043 wdgetdisklabel(struct wd_softc *wd) 1044 { 1045 struct disklabel *lp = wd->sc_dk.dk_label; 1046 const char *errstring; 1047 int s; 1048 1049 ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS); 1050 1051 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); 1052 1053 wdgetdefaultlabel(wd, lp); 1054 1055 wd->sc_badsect[0] = -1; 1056 1057 if (wd->drvp->state > RESET) { 1058 s = splbio(); 1059 wd->drvp->drive_flags |= DRIVE_RESET; 1060 splx(s); 1061 } 1062 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev), 1063 RAW_PART), wdstrategy, lp, 1064 wd->sc_dk.dk_cpulabel); 1065 if (errstring) { 1066 /* 1067 * This probably happened because the drive's default 1068 * geometry doesn't match the DOS geometry. We 1069 * assume the DOS geometry is now in the label and try 1070 * again. XXX This is a kluge. 1071 */ 1072 if (wd->drvp->state > RESET) { 1073 s = splbio(); 1074 wd->drvp->drive_flags |= DRIVE_RESET; 1075 splx(s); 1076 } 1077 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev), 1078 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel); 1079 } 1080 if (errstring) { 1081 aprint_error_dev(wd->sc_dev, "%s\n", errstring); 1082 return; 1083 } 1084 1085 if (wd->drvp->state > RESET) { 1086 s = splbio(); 1087 wd->drvp->drive_flags |= DRIVE_RESET; 1088 splx(s); 1089 } 1090 #ifdef HAS_BAD144_HANDLING 1091 if ((lp->d_flags & D_BADSECT) != 0) 1092 bad144intern(wd); 1093 #endif 1094 } 1095 1096 void 1097 wdperror(const struct wd_softc *wd) 1098 { 1099 static const char *const errstr0_3[] = {"address mark not found", 1100 "track 0 not found", "aborted command", "media change requested", 1101 "id not found", "media changed", "uncorrectable data error", 1102 "bad block detected"}; 1103 static const char *const errstr4_5[] = { 1104 "obsolete (address mark not found)", 1105 "no media/write protected", "aborted command", 1106 "media change requested", "id not found", "media changed", 1107 "uncorrectable data error", "interface CRC error"}; 1108 const char *const *errstr; 1109 int i; 1110 const char *sep = ""; 1111 1112 const char *devname = device_xname(wd->sc_dev); 1113 struct ata_drive_datas *drvp = wd->drvp; 1114 int errno = wd->sc_wdc_bio.r_error; 1115 1116 if (drvp->ata_vers >= 4) 1117 errstr = errstr4_5; 1118 else 1119 errstr = errstr0_3; 1120 1121 printf("%s: (", devname); 1122 1123 if (errno == 0) 1124 printf("error not notified"); 1125 1126 for (i = 0; i < 8; i++) { 1127 if (errno & (1 << i)) { 1128 printf("%s%s", sep, errstr[i]); 1129 sep = ", "; 1130 } 1131 } 1132 printf(")\n"); 1133 } 1134 1135 int 1136 wdioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l) 1137 { 1138 struct wd_softc *wd = 1139 device_lookup_private(&wd_cd, WDUNIT(dev)); 1140 int error = 0, s; 1141 #ifdef __HAVE_OLD_DISKLABEL 1142 struct disklabel *newlabel = NULL; 1143 #endif 1144 1145 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS); 1146 1147 if ((wd->sc_flags & WDF_LOADED) == 0) 1148 return EIO; 1149 1150 error = disk_ioctl(&wd->sc_dk, xfer, addr, flag, l); 1151 if (error != EPASSTHROUGH) 1152 return (error); 1153 1154 switch (xfer) { 1155 #ifdef HAS_BAD144_HANDLING 1156 case DIOCSBAD: 1157 if ((flag & FWRITE) == 0) 1158 return EBADF; 1159 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr; 1160 wd->sc_dk.dk_label->d_flags |= D_BADSECT; 1161 bad144intern(wd); 1162 return 0; 1163 #endif 1164 #ifdef WD_SOFTBADSECT 1165 case DIOCBSLIST : 1166 { 1167 u_int32_t count, missing, skip; 1168 struct disk_badsecinfo dbsi; 1169 struct disk_badsectors *dbs; 1170 size_t available; 1171 uint8_t *laddr; 1172 1173 dbsi = *(struct disk_badsecinfo *)addr; 1174 missing = wd->sc_bscount; 1175 count = 0; 1176 available = dbsi.dbsi_bufsize; 1177 skip = dbsi.dbsi_skip; 1178 laddr = (uint8_t *)dbsi.dbsi_buffer; 1179 1180 /* 1181 * We start this loop with the expectation that all of the 1182 * entries will be missed and decrement this counter each 1183 * time we either skip over one (already copied out) or 1184 * we actually copy it back to user space. The structs 1185 * holding the bad sector information are copied directly 1186 * back to user space whilst the summary is returned via 1187 * the struct passed in via the ioctl. 1188 */ 1189 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) { 1190 if (skip > 0) { 1191 missing--; 1192 skip--; 1193 continue; 1194 } 1195 if (available < sizeof(*dbs)) 1196 break; 1197 available -= sizeof(*dbs); 1198 copyout(dbs, laddr, sizeof(*dbs)); 1199 laddr += sizeof(*dbs); 1200 missing--; 1201 count++; 1202 } 1203 dbsi.dbsi_left = missing; 1204 dbsi.dbsi_copied = count; 1205 *(struct disk_badsecinfo *)addr = dbsi; 1206 return 0; 1207 } 1208 1209 case DIOCBSFLUSH : 1210 /* Clean out the bad sector list */ 1211 while (!SLIST_EMPTY(&wd->sc_bslist)) { 1212 void *head = SLIST_FIRST(&wd->sc_bslist); 1213 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next); 1214 free(head, M_TEMP); 1215 } 1216 wd->sc_bscount = 0; 1217 return 0; 1218 #endif 1219 case DIOCGDINFO: 1220 *(struct disklabel *)addr = *(wd->sc_dk.dk_label); 1221 return 0; 1222 #ifdef __HAVE_OLD_DISKLABEL 1223 case ODIOCGDINFO: 1224 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1225 if (newlabel == NULL) 1226 return EIO; 1227 *newlabel = *(wd->sc_dk.dk_label); 1228 if (newlabel->d_npartitions <= OLDMAXPARTITIONS) 1229 memcpy(addr, newlabel, sizeof (struct olddisklabel)); 1230 else 1231 error = ENOTTY; 1232 free(newlabel, M_TEMP); 1233 return error; 1234 #endif 1235 1236 case DIOCGPART: 1237 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label; 1238 ((struct partinfo *)addr)->part = 1239 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)]; 1240 return 0; 1241 1242 case DIOCWDINFO: 1243 case DIOCSDINFO: 1244 #ifdef __HAVE_OLD_DISKLABEL 1245 case ODIOCWDINFO: 1246 case ODIOCSDINFO: 1247 #endif 1248 { 1249 struct disklabel *lp; 1250 1251 if ((flag & FWRITE) == 0) 1252 return EBADF; 1253 1254 #ifdef __HAVE_OLD_DISKLABEL 1255 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) { 1256 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1257 if (newlabel == NULL) 1258 return EIO; 1259 memset(newlabel, 0, sizeof newlabel); 1260 memcpy(newlabel, addr, sizeof (struct olddisklabel)); 1261 lp = newlabel; 1262 } else 1263 #endif 1264 lp = (struct disklabel *)addr; 1265 1266 mutex_enter(&wd->sc_dk.dk_openlock); 1267 wd->sc_flags |= WDF_LABELLING; 1268 1269 error = setdisklabel(wd->sc_dk.dk_label, 1270 lp, /*wd->sc_dk.dk_openmask : */0, 1271 wd->sc_dk.dk_cpulabel); 1272 if (error == 0) { 1273 if (wd->drvp->state > RESET) { 1274 s = splbio(); 1275 wd->drvp->drive_flags |= DRIVE_RESET; 1276 splx(s); 1277 } 1278 if (xfer == DIOCWDINFO 1279 #ifdef __HAVE_OLD_DISKLABEL 1280 || xfer == ODIOCWDINFO 1281 #endif 1282 ) 1283 error = writedisklabel(WDLABELDEV(dev), 1284 wdstrategy, wd->sc_dk.dk_label, 1285 wd->sc_dk.dk_cpulabel); 1286 } 1287 1288 wd->sc_flags &= ~WDF_LABELLING; 1289 mutex_exit(&wd->sc_dk.dk_openlock); 1290 #ifdef __HAVE_OLD_DISKLABEL 1291 if (newlabel != NULL) 1292 free(newlabel, M_TEMP); 1293 #endif 1294 return error; 1295 } 1296 1297 case DIOCKLABEL: 1298 if (*(int *)addr) 1299 wd->sc_flags |= WDF_KLABEL; 1300 else 1301 wd->sc_flags &= ~WDF_KLABEL; 1302 return 0; 1303 1304 case DIOCWLABEL: 1305 if ((flag & FWRITE) == 0) 1306 return EBADF; 1307 if (*(int *)addr) 1308 wd->sc_flags |= WDF_WLABEL; 1309 else 1310 wd->sc_flags &= ~WDF_WLABEL; 1311 return 0; 1312 1313 case DIOCGDEFLABEL: 1314 wdgetdefaultlabel(wd, (struct disklabel *)addr); 1315 return 0; 1316 #ifdef __HAVE_OLD_DISKLABEL 1317 case ODIOCGDEFLABEL: 1318 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); 1319 if (newlabel == NULL) 1320 return EIO; 1321 wdgetdefaultlabel(wd, newlabel); 1322 if (newlabel->d_npartitions <= OLDMAXPARTITIONS) 1323 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 1324 else 1325 error = ENOTTY; 1326 free(newlabel, M_TEMP); 1327 return error; 1328 #endif 1329 1330 #ifdef notyet 1331 case DIOCWFORMAT: 1332 if ((flag & FWRITE) == 0) 1333 return EBADF; 1334 { 1335 register struct format_op *fop; 1336 struct iovec aiov; 1337 struct uio auio; 1338 1339 fop = (struct format_op *)addr; 1340 aiov.iov_base = fop->df_buf; 1341 aiov.iov_len = fop->df_count; 1342 auio.uio_iov = &aiov; 1343 auio.uio_iovcnt = 1; 1344 auio.uio_resid = fop->df_count; 1345 auio.uio_offset = 1346 fop->df_startblk * wd->sc_dk.dk_label->d_secsize; 1347 auio.uio_vmspace = l->l_proc->p_vmspace; 1348 error = physio(wdformat, NULL, dev, B_WRITE, minphys, 1349 &auio); 1350 fop->df_count -= auio.uio_resid; 1351 fop->df_reg[0] = wdc->sc_status; 1352 fop->df_reg[1] = wdc->sc_error; 1353 return error; 1354 } 1355 #endif 1356 case DIOCGCACHE: 1357 return wd_getcache(wd, (int *)addr); 1358 1359 case DIOCSCACHE: 1360 return wd_setcache(wd, *(int *)addr); 1361 1362 case DIOCCACHESYNC: 1363 return wd_flushcache(wd, AT_WAIT); 1364 1365 case ATAIOCCOMMAND: 1366 /* 1367 * Make sure this command is (relatively) safe first 1368 */ 1369 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 && 1370 (flag & FWRITE) == 0) 1371 return (EBADF); 1372 { 1373 struct wd_ioctl *wi; 1374 atareq_t *atareq = (atareq_t *) addr; 1375 int error1; 1376 1377 wi = wi_get(); 1378 wi->wi_softc = wd; 1379 wi->wi_atareq = *atareq; 1380 1381 if (atareq->datalen && atareq->flags & 1382 (ATACMD_READ | ATACMD_WRITE)) { 1383 void *tbuf; 1384 if (atareq->datalen < DEV_BSIZE 1385 && atareq->command == WDCC_IDENTIFY) { 1386 tbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK); 1387 wi->wi_iov.iov_base = tbuf; 1388 wi->wi_iov.iov_len = DEV_BSIZE; 1389 UIO_SETUP_SYSSPACE(&wi->wi_uio); 1390 } else { 1391 tbuf = NULL; 1392 wi->wi_iov.iov_base = atareq->databuf; 1393 wi->wi_iov.iov_len = atareq->datalen; 1394 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace; 1395 } 1396 wi->wi_uio.uio_iov = &wi->wi_iov; 1397 wi->wi_uio.uio_iovcnt = 1; 1398 wi->wi_uio.uio_resid = atareq->datalen; 1399 wi->wi_uio.uio_offset = 0; 1400 wi->wi_uio.uio_rw = 1401 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE; 1402 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev, 1403 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE, 1404 minphys, &wi->wi_uio); 1405 if (tbuf != NULL && error1 == 0) { 1406 error1 = copyout(tbuf, atareq->databuf, 1407 atareq->datalen); 1408 free(tbuf, M_TEMP); 1409 } 1410 } else { 1411 /* No need to call physio if we don't have any 1412 user data */ 1413 wi->wi_bp.b_flags = 0; 1414 wi->wi_bp.b_data = 0; 1415 wi->wi_bp.b_bcount = 0; 1416 wi->wi_bp.b_dev = 0; 1417 wi->wi_bp.b_proc = l->l_proc; 1418 wdioctlstrategy(&wi->wi_bp); 1419 error1 = wi->wi_bp.b_error; 1420 } 1421 *atareq = wi->wi_atareq; 1422 wi_free(wi); 1423 return(error1); 1424 } 1425 1426 case DIOCAWEDGE: 1427 { 1428 struct dkwedge_info *dkw = (void *) addr; 1429 1430 if ((flag & FWRITE) == 0) 1431 return (EBADF); 1432 1433 /* If the ioctl happens here, the parent is us. */ 1434 strcpy(dkw->dkw_parent, device_xname(wd->sc_dev)); 1435 return (dkwedge_add(dkw)); 1436 } 1437 1438 case DIOCDWEDGE: 1439 { 1440 struct dkwedge_info *dkw = (void *) addr; 1441 1442 if ((flag & FWRITE) == 0) 1443 return (EBADF); 1444 1445 /* If the ioctl happens here, the parent is us. */ 1446 strcpy(dkw->dkw_parent, device_xname(wd->sc_dev)); 1447 return (dkwedge_del(dkw)); 1448 } 1449 1450 case DIOCLWEDGES: 1451 { 1452 struct dkwedge_list *dkwl = (void *) addr; 1453 1454 return (dkwedge_list(&wd->sc_dk, dkwl, l)); 1455 } 1456 1457 case DIOCGSTRATEGY: 1458 { 1459 struct disk_strategy *dks = (void *)addr; 1460 1461 s = splbio(); 1462 strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q), 1463 sizeof(dks->dks_name)); 1464 splx(s); 1465 dks->dks_paramlen = 0; 1466 1467 return 0; 1468 } 1469 1470 case DIOCSSTRATEGY: 1471 { 1472 struct disk_strategy *dks = (void *)addr; 1473 struct bufq_state *new; 1474 struct bufq_state *old; 1475 1476 if ((flag & FWRITE) == 0) { 1477 return EBADF; 1478 } 1479 if (dks->dks_param != NULL) { 1480 return EINVAL; 1481 } 1482 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */ 1483 error = bufq_alloc(&new, dks->dks_name, 1484 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK); 1485 if (error) { 1486 return error; 1487 } 1488 s = splbio(); 1489 old = wd->sc_q; 1490 bufq_move(new, old); 1491 wd->sc_q = new; 1492 splx(s); 1493 bufq_free(old); 1494 1495 return 0; 1496 } 1497 1498 default: 1499 return ENOTTY; 1500 } 1501 1502 #ifdef DIAGNOSTIC 1503 panic("wdioctl: impossible"); 1504 #endif 1505 } 1506 1507 #ifdef B_FORMAT 1508 int 1509 wdformat(struct buf *bp) 1510 { 1511 1512 bp->b_flags |= B_FORMAT; 1513 return wdstrategy(bp); 1514 } 1515 #endif 1516 1517 int 1518 wdsize(dev_t dev) 1519 { 1520 struct wd_softc *wd; 1521 int part, omask; 1522 int size; 1523 1524 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS); 1525 1526 wd = device_lookup_private(&wd_cd, WDUNIT(dev)); 1527 if (wd == NULL) 1528 return (-1); 1529 1530 part = WDPART(dev); 1531 omask = wd->sc_dk.dk_openmask & (1 << part); 1532 1533 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0) 1534 return (-1); 1535 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 1536 size = -1; 1537 else 1538 size = wd->sc_dk.dk_label->d_partitions[part].p_size * 1539 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE); 1540 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0) 1541 return (-1); 1542 return (size); 1543 } 1544 1545 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */ 1546 static int wddoingadump = 0; 1547 static int wddumprecalibrated = 0; 1548 1549 /* 1550 * Dump core after a system crash. 1551 */ 1552 int 1553 wddump(dev_t dev, daddr_t blkno, void *va, size_t size) 1554 { 1555 struct wd_softc *wd; /* disk unit to do the I/O */ 1556 struct disklabel *lp; /* disk's disklabel */ 1557 int part, err; 1558 int nblks; /* total number of sectors left to write */ 1559 1560 /* Check if recursive dump; if so, punt. */ 1561 if (wddoingadump) 1562 return EFAULT; 1563 wddoingadump = 1; 1564 1565 wd = device_lookup_private(&wd_cd, WDUNIT(dev)); 1566 if (wd == NULL) 1567 return (ENXIO); 1568 1569 part = WDPART(dev); 1570 1571 /* Convert to disk sectors. Request must be a multiple of size. */ 1572 lp = wd->sc_dk.dk_label; 1573 if ((size % lp->d_secsize) != 0) 1574 return EFAULT; 1575 nblks = size / lp->d_secsize; 1576 blkno = blkno / (lp->d_secsize / DEV_BSIZE); 1577 1578 /* Check transfer bounds against partition size. */ 1579 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size)) 1580 return EINVAL; 1581 1582 /* Offset block number to start of partition. */ 1583 blkno += lp->d_partitions[part].p_offset; 1584 1585 /* Recalibrate, if first dump transfer. */ 1586 if (wddumprecalibrated == 0) { 1587 wddumprecalibrated = 1; 1588 (*wd->atabus->ata_reset_drive)(wd->drvp, 1589 AT_POLL | AT_RST_EMERG); 1590 wd->drvp->state = RESET; 1591 } 1592 1593 wd->sc_bp = NULL; 1594 wd->sc_wdc_bio.blkno = blkno; 1595 wd->sc_wdc_bio.flags = ATA_POLL; 1596 if (wd->sc_flags & WDF_LBA48 && 1597 (wd->sc_wdc_bio.blkno + nblks) > wd->sc_capacity28) 1598 wd->sc_wdc_bio.flags |= ATA_LBA48; 1599 if (wd->sc_flags & WDF_LBA) 1600 wd->sc_wdc_bio.flags |= ATA_LBA; 1601 wd->sc_wdc_bio.bcount = nblks * lp->d_secsize; 1602 wd->sc_wdc_bio.databuf = va; 1603 #ifndef WD_DUMP_NOT_TRUSTED 1604 switch (err = wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) { 1605 case ATACMD_TRY_AGAIN: 1606 panic("wddump: try again"); 1607 break; 1608 case ATACMD_QUEUED: 1609 panic("wddump: polled command has been queued"); 1610 break; 1611 case ATACMD_COMPLETE: 1612 break; 1613 default: 1614 panic("wddump: unknown atacmd code %d", err); 1615 } 1616 switch(err = wd->sc_wdc_bio.error) { 1617 case TIMEOUT: 1618 printf("wddump: device timed out"); 1619 err = EIO; 1620 break; 1621 case ERR_DF: 1622 printf("wddump: drive fault"); 1623 err = EIO; 1624 break; 1625 case ERR_DMA: 1626 printf("wddump: DMA error"); 1627 err = EIO; 1628 break; 1629 case ERROR: 1630 printf("wddump: "); 1631 wdperror(wd); 1632 err = EIO; 1633 break; 1634 case NOERROR: 1635 err = 0; 1636 break; 1637 default: 1638 panic("wddump: unknown error type %d", err); 1639 } 1640 if (err != 0) { 1641 printf("\n"); 1642 return err; 1643 } 1644 #else /* WD_DUMP_NOT_TRUSTED */ 1645 /* Let's just talk about this first... */ 1646 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n", 1647 unit, va, cylin, head, sector); 1648 delay(500 * 1000); /* half a second */ 1649 #endif 1650 1651 wddoingadump = 0; 1652 return 0; 1653 } 1654 1655 #ifdef HAS_BAD144_HANDLING 1656 /* 1657 * Internalize the bad sector table. 1658 */ 1659 void 1660 bad144intern(struct wd_softc *wd) 1661 { 1662 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad; 1663 struct disklabel *lp = wd->sc_dk.dk_label; 1664 int i = 0; 1665 1666 ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS); 1667 1668 for (; i < NBT_BAD; i++) { 1669 if (bt->bt_bad[i].bt_cyl == 0xffff) 1670 break; 1671 wd->sc_badsect[i] = 1672 bt->bt_bad[i].bt_cyl * lp->d_secpercyl + 1673 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors + 1674 (bt->bt_bad[i].bt_trksec & 0xff); 1675 } 1676 for (; i < NBT_BAD+1; i++) 1677 wd->sc_badsect[i] = -1; 1678 } 1679 #endif 1680 1681 static void 1682 wd_params_to_properties(struct wd_softc *wd, struct ataparams *params) 1683 { 1684 prop_dictionary_t disk_info, odisk_info, geom; 1685 const char *cp; 1686 1687 disk_info = prop_dictionary_create(); 1688 1689 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) 1690 cp = "ST506"; 1691 else { 1692 /* XXX Should have a case for ATA here, too. */ 1693 cp = "ESDI"; 1694 } 1695 prop_dictionary_set_cstring_nocopy(disk_info, "type", cp); 1696 1697 geom = prop_dictionary_create(); 1698 1699 prop_dictionary_set_uint64(geom, "sectors-per-unit", wd->sc_capacity); 1700 1701 prop_dictionary_set_uint32(geom, "sector-size", 1702 DEV_BSIZE /* XXX 512? */); 1703 1704 prop_dictionary_set_uint16(geom, "sectors-per-track", 1705 wd->sc_params.atap_sectors); 1706 1707 prop_dictionary_set_uint16(geom, "tracks-per-cylinder", 1708 wd->sc_params.atap_heads); 1709 1710 if (wd->sc_flags & WDF_LBA) 1711 prop_dictionary_set_uint64(geom, "cylinders-per-unit", 1712 wd->sc_capacity / 1713 (wd->sc_params.atap_heads * 1714 wd->sc_params.atap_sectors)); 1715 else 1716 prop_dictionary_set_uint16(geom, "cylinders-per-unit", 1717 wd->sc_params.atap_cylinders); 1718 1719 prop_dictionary_set(disk_info, "geometry", geom); 1720 prop_object_release(geom); 1721 1722 prop_dictionary_set(device_properties(wd->sc_dev), 1723 "disk-info", disk_info); 1724 1725 /* 1726 * Don't release disk_info here; we keep a reference to it. 1727 * disk_detach() will release it when we go away. 1728 */ 1729 1730 odisk_info = wd->sc_dk.dk_info; 1731 wd->sc_dk.dk_info = disk_info; 1732 if (odisk_info) 1733 prop_object_release(odisk_info); 1734 } 1735 1736 int 1737 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params) 1738 { 1739 1740 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) { 1741 case CMD_AGAIN: 1742 return 1; 1743 case CMD_ERR: 1744 /* 1745 * We `know' there's a drive here; just assume it's old. 1746 * This geometry is only used to read the MBR and print a 1747 * (false) attach message. 1748 */ 1749 strncpy(params->atap_model, "ST506", 1750 sizeof params->atap_model); 1751 params->atap_config = ATA_CFG_FIXED; 1752 params->atap_cylinders = 1024; 1753 params->atap_heads = 8; 1754 params->atap_sectors = 17; 1755 params->atap_multi = 1; 1756 params->atap_capabilities1 = params->atap_capabilities2 = 0; 1757 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */ 1758 /* FALLTHROUGH */ 1759 case CMD_OK: 1760 wd_params_to_properties(wd, params); 1761 return 0; 1762 default: 1763 panic("wd_get_params: bad return code from ata_get_params"); 1764 /* NOTREACHED */ 1765 } 1766 } 1767 1768 int 1769 wd_getcache(struct wd_softc *wd, int *bitsp) 1770 { 1771 struct ataparams params; 1772 1773 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0) 1774 return EIO; 1775 if (params.atap_cmd_set1 == 0x0000 || 1776 params.atap_cmd_set1 == 0xffff || 1777 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) { 1778 *bitsp = 0; 1779 return 0; 1780 } 1781 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ; 1782 if (params.atap_cmd1_en & WDC_CMD1_CACHE) 1783 *bitsp |= DKCACHE_WRITE; 1784 1785 return 0; 1786 } 1787 1788 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF"; 1789 1790 int 1791 wd_setcache(struct wd_softc *wd, int bits) 1792 { 1793 struct ataparams params; 1794 struct ata_command ata_c; 1795 1796 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0) 1797 return EIO; 1798 1799 if (params.atap_cmd_set1 == 0x0000 || 1800 params.atap_cmd_set1 == 0xffff || 1801 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) 1802 return EOPNOTSUPP; 1803 1804 if ((bits & DKCACHE_READ) == 0 || 1805 (bits & DKCACHE_SAVE) != 0) 1806 return EOPNOTSUPP; 1807 1808 memset(&ata_c, 0, sizeof(struct ata_command)); 1809 ata_c.r_command = SET_FEATURES; 1810 ata_c.r_st_bmask = 0; 1811 ata_c.r_st_pmask = 0; 1812 ata_c.timeout = 30000; /* 30s timeout */ 1813 ata_c.flags = AT_WAIT; 1814 if (bits & DKCACHE_WRITE) 1815 ata_c.r_features = WDSF_WRITE_CACHE_EN; 1816 else 1817 ata_c.r_features = WDSF_WRITE_CACHE_DS; 1818 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { 1819 aprint_error_dev(wd->sc_dev, 1820 "wd_setcache command not complete\n"); 1821 return EIO; 1822 } 1823 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 1824 char sbuf[sizeof(at_errbits) + 64]; 1825 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); 1826 aprint_error_dev(wd->sc_dev, "wd_setcache: status=%s\n", sbuf); 1827 return EIO; 1828 } 1829 return 0; 1830 } 1831 1832 static int 1833 wd_standby(struct wd_softc *wd, int flags) 1834 { 1835 struct ata_command ata_c; 1836 1837 memset(&ata_c, 0, sizeof(struct ata_command)); 1838 ata_c.r_command = WDCC_STANDBY_IMMED; 1839 ata_c.r_st_bmask = WDCS_DRDY; 1840 ata_c.r_st_pmask = WDCS_DRDY; 1841 ata_c.flags = flags; 1842 ata_c.timeout = 30000; /* 30s timeout */ 1843 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { 1844 aprint_error_dev(wd->sc_dev, 1845 "standby immediate command didn't complete\n"); 1846 return EIO; 1847 } 1848 if (ata_c.flags & AT_ERROR) { 1849 if (ata_c.r_error == WDCE_ABRT) /* command not supported */ 1850 return ENODEV; 1851 } 1852 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 1853 char sbuf[sizeof(at_errbits) + 64]; 1854 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); 1855 aprint_error_dev(wd->sc_dev, "wd_standby: status=%s\n", sbuf); 1856 return EIO; 1857 } 1858 return 0; 1859 } 1860 1861 int 1862 wd_flushcache(struct wd_softc *wd, int flags) 1863 { 1864 struct ata_command ata_c; 1865 1866 /* 1867 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report 1868 * only ATA-2 and still support it. 1869 */ 1870 if (wd->drvp->ata_vers < 4 && 1871 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 || 1872 wd->sc_params.atap_cmd_set2 == 0xffff)) 1873 return ENODEV; 1874 memset(&ata_c, 0, sizeof(struct ata_command)); 1875 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 && 1876 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) 1877 ata_c.r_command = WDCC_FLUSHCACHE_EXT; 1878 else 1879 ata_c.r_command = WDCC_FLUSHCACHE; 1880 ata_c.r_st_bmask = WDCS_DRDY; 1881 ata_c.r_st_pmask = WDCS_DRDY; 1882 ata_c.flags = flags; 1883 ata_c.timeout = 30000; /* 30s timeout */ 1884 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { 1885 aprint_error_dev(wd->sc_dev, 1886 "flush cache command didn't complete\n"); 1887 return EIO; 1888 } 1889 if (ata_c.flags & AT_ERROR) { 1890 if (ata_c.r_error == WDCE_ABRT) /* command not supported */ 1891 return ENODEV; 1892 } 1893 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 1894 char sbuf[sizeof(at_errbits) + 64]; 1895 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); 1896 aprint_error_dev(wd->sc_dev, "wd_flushcache: status=%s\n", 1897 sbuf); 1898 return EIO; 1899 } 1900 return 0; 1901 } 1902 1903 bool 1904 wd_shutdown(device_t dev, int how) 1905 { 1906 struct wd_softc *wd = device_private(dev); 1907 1908 /* the adapter needs to be enabled */ 1909 if (wd->atabus->ata_addref(wd->drvp)) 1910 return true; /* no need to complain */ 1911 1912 wd_flushcache(wd, AT_POLL); 1913 if ((how & RB_POWERDOWN) == RB_POWERDOWN) 1914 wd_standby(wd, AT_POLL); 1915 return true; 1916 } 1917 1918 /* 1919 * Allocate space for a ioctl queue structure. Mostly taken from 1920 * scsipi_ioctl.c 1921 */ 1922 struct wd_ioctl * 1923 wi_get(void) 1924 { 1925 struct wd_ioctl *wi; 1926 int s; 1927 1928 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO); 1929 buf_init(&wi->wi_bp); 1930 s = splbio(); 1931 LIST_INSERT_HEAD(&wi_head, wi, wi_list); 1932 splx(s); 1933 return (wi); 1934 } 1935 1936 /* 1937 * Free an ioctl structure and remove it from our list 1938 */ 1939 1940 void 1941 wi_free(struct wd_ioctl *wi) 1942 { 1943 int s; 1944 1945 s = splbio(); 1946 LIST_REMOVE(wi, wi_list); 1947 splx(s); 1948 buf_destroy(&wi->wi_bp); 1949 free(wi, M_TEMP); 1950 } 1951 1952 /* 1953 * Find a wd_ioctl structure based on the struct buf. 1954 */ 1955 1956 struct wd_ioctl * 1957 wi_find(struct buf *bp) 1958 { 1959 struct wd_ioctl *wi; 1960 int s; 1961 1962 s = splbio(); 1963 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next) 1964 if (bp == &wi->wi_bp) 1965 break; 1966 splx(s); 1967 return (wi); 1968 } 1969 1970 /* 1971 * Ioctl pseudo strategy routine 1972 * 1973 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What 1974 * happens here is: 1975 * 1976 * - wdioctl() queues a wd_ioctl structure. 1977 * 1978 * - wdioctl() calls physio/wdioctlstrategy based on whether or not 1979 * user space I/O is required. If physio() is called, physio() eventually 1980 * calls wdioctlstrategy(). 1981 * 1982 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command() 1983 * to perform the actual command 1984 * 1985 * The reason for the use of the pseudo strategy routine is because 1986 * when doing I/O to/from user space, physio _really_ wants to be in 1987 * the loop. We could put the entire buffer into the ioctl request 1988 * structure, but that won't scale if we want to do things like download 1989 * microcode. 1990 */ 1991 1992 void 1993 wdioctlstrategy(struct buf *bp) 1994 { 1995 struct wd_ioctl *wi; 1996 struct ata_command ata_c; 1997 int error = 0; 1998 1999 wi = wi_find(bp); 2000 if (wi == NULL) { 2001 printf("wdioctlstrategy: " 2002 "No matching ioctl request found in queue\n"); 2003 error = EINVAL; 2004 goto bad; 2005 } 2006 2007 memset(&ata_c, 0, sizeof(ata_c)); 2008 2009 /* 2010 * Abort if physio broke up the transfer 2011 */ 2012 2013 if (bp->b_bcount != wi->wi_atareq.datalen) { 2014 printf("physio split wd ioctl request... cannot proceed\n"); 2015 error = EIO; 2016 goto bad; 2017 } 2018 2019 /* 2020 * Abort if we didn't get a buffer size that was a multiple of 2021 * our sector size (or was larger than NBBY) 2022 */ 2023 2024 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 || 2025 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >= 2026 (1 << NBBY)) { 2027 error = EINVAL; 2028 goto bad; 2029 } 2030 2031 /* 2032 * Make sure a timeout was supplied in the ioctl request 2033 */ 2034 2035 if (wi->wi_atareq.timeout == 0) { 2036 error = EINVAL; 2037 goto bad; 2038 } 2039 2040 if (wi->wi_atareq.flags & ATACMD_READ) 2041 ata_c.flags |= AT_READ; 2042 else if (wi->wi_atareq.flags & ATACMD_WRITE) 2043 ata_c.flags |= AT_WRITE; 2044 2045 if (wi->wi_atareq.flags & ATACMD_READREG) 2046 ata_c.flags |= AT_READREG; 2047 2048 ata_c.flags |= AT_WAIT; 2049 2050 ata_c.timeout = wi->wi_atareq.timeout; 2051 ata_c.r_command = wi->wi_atareq.command; 2052 ata_c.r_head = wi->wi_atareq.head & 0x0f; 2053 ata_c.r_cyl = wi->wi_atareq.cylinder; 2054 ata_c.r_sector = wi->wi_atareq.sec_num; 2055 ata_c.r_count = wi->wi_atareq.sec_count; 2056 ata_c.r_features = wi->wi_atareq.features; 2057 ata_c.r_st_bmask = WDCS_DRDY; 2058 ata_c.r_st_pmask = WDCS_DRDY; 2059 ata_c.data = wi->wi_bp.b_data; 2060 ata_c.bcount = wi->wi_bp.b_bcount; 2061 2062 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c) 2063 != ATACMD_COMPLETE) { 2064 wi->wi_atareq.retsts = ATACMD_ERROR; 2065 goto bad; 2066 } 2067 2068 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 2069 if (ata_c.flags & AT_ERROR) { 2070 wi->wi_atareq.retsts = ATACMD_ERROR; 2071 wi->wi_atareq.error = ata_c.r_error; 2072 } else if (ata_c.flags & AT_DF) 2073 wi->wi_atareq.retsts = ATACMD_DF; 2074 else 2075 wi->wi_atareq.retsts = ATACMD_TIMEOUT; 2076 } else { 2077 wi->wi_atareq.retsts = ATACMD_OK; 2078 if (wi->wi_atareq.flags & ATACMD_READREG) { 2079 wi->wi_atareq.head = ata_c.r_head ; 2080 wi->wi_atareq.cylinder = ata_c.r_cyl; 2081 wi->wi_atareq.sec_num = ata_c.r_sector; 2082 wi->wi_atareq.sec_count = ata_c.r_count; 2083 wi->wi_atareq.features = ata_c.r_features; 2084 wi->wi_atareq.error = ata_c.r_error; 2085 } 2086 } 2087 2088 bp->b_error = 0; 2089 biodone(bp); 2090 return; 2091 bad: 2092 bp->b_error = error; 2093 biodone(bp); 2094 } 2095