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