1 /* $NetBSD: wd.c,v 1.476 2026/07/04 00:13:39 jakllsch 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.476 2026/07/04 00:13:39 jakllsch Exp $"); 58 59 #include "opt_ata.h" 60 #include "opt_wd.h" 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/kernel.h> 65 #include <sys/conf.h> 66 #include <sys/file.h> 67 #include <sys/stat.h> 68 #include <sys/ioctl.h> 69 #include <sys/buf.h> 70 #include <sys/bufq.h> 71 #include <sys/uio.h> 72 #include <sys/device.h> 73 #include <sys/disklabel.h> 74 #include <sys/disk.h> 75 #include <sys/dkbad.h> 76 #include <sys/syslog.h> 77 #include <sys/proc.h> 78 #include <sys/reboot.h> 79 #include <sys/vnode.h> 80 #include <sys/rndsource.h> 81 82 #include <sys/intr.h> 83 #include <sys/bus.h> 84 85 #include <dev/ata/atareg.h> 86 #include <dev/ata/atavar.h> 87 #include <dev/ata/wdvar.h> 88 #include <dev/ic/wdcreg.h> 89 #include <sys/ataio.h> 90 #include "locators.h" 91 92 #include <prop/proplib.h> 93 94 #define WDIORETRIES_SINGLE 4 /* number of retries for single-sector */ 95 #define WDIORETRIES 5 /* number of retries before giving up */ 96 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */ 97 98 #define WDUNIT(dev) DISKUNIT(dev) 99 #define WDPART(dev) DISKPART(dev) 100 #define WDMINOR(unit, part) DISKMINOR(unit, part) 101 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) 102 103 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART)) 104 105 #define DEBUG_FUNCS 0x08 106 #define DEBUG_PROBE 0x10 107 #define DEBUG_DETACH 0x20 108 #define DEBUG_XFERS 0x40 109 #ifdef ATADEBUG 110 #ifndef ATADEBUG_WD_MASK 111 #define ATADEBUG_WD_MASK 0x0 112 #endif 113 int wdcdebug_wd_mask = ATADEBUG_WD_MASK; 114 #define ATADEBUG_PRINT(args, level) \ 115 if (wdcdebug_wd_mask & (level)) \ 116 printf args 117 #else 118 #define ATADEBUG_PRINT(args, level) 119 #endif 120 121 static int wdprobe(device_t, cfdata_t, void *); 122 static void wdattach(device_t, device_t, void *); 123 static int wddetach(device_t, int); 124 static void wdperror(const struct wd_softc *, struct ata_xfer *); 125 126 static void wdminphys(struct buf *); 127 128 static int wd_firstopen(device_t, dev_t, int, int); 129 static int wd_lastclose(device_t); 130 static bool wd_suspend(device_t, const pmf_qual_t *); 131 static int wd_standby(struct wd_softc *, int); 132 133 CFATTACH_DECL3_NEW(wd, sizeof(struct wd_softc), 134 wdprobe, wdattach, wddetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); 135 136 extern struct cfdriver wd_cd; 137 138 static dev_type_open(wdopen); 139 static dev_type_close(wdclose); 140 static dev_type_read(wdread); 141 static dev_type_write(wdwrite); 142 static dev_type_ioctl(wdioctl); 143 static dev_type_strategy(wdstrategy); 144 static dev_type_dump(wddump); 145 static dev_type_size(wdsize); 146 static dev_type_discard(wddiscard); 147 148 const struct bdevsw wd_bdevsw = { 149 .d_open = wdopen, 150 .d_close = wdclose, 151 .d_strategy = wdstrategy, 152 .d_ioctl = wdioctl, 153 .d_dump = wddump, 154 .d_psize = wdsize, 155 .d_discard = wddiscard, 156 .d_cfdriver = &wd_cd, 157 .d_devtounit = disklabel_dev_unit, 158 .d_flag = D_DISK 159 }; 160 161 const struct cdevsw wd_cdevsw = { 162 .d_open = wdopen, 163 .d_close = wdclose, 164 .d_read = wdread, 165 .d_write = wdwrite, 166 .d_ioctl = wdioctl, 167 .d_stop = nostop, 168 .d_tty = notty, 169 .d_poll = nopoll, 170 .d_mmap = nommap, 171 .d_kqfilter = nokqfilter, 172 .d_discard = wddiscard, 173 .d_cfdriver = &wd_cd, 174 .d_devtounit = disklabel_dev_unit, 175 .d_flag = D_DISK 176 }; 177 178 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */ 179 static int wddoingadump = 0; 180 static int wddumprecalibrated = 0; 181 182 /* 183 * Glue necessary to hook WDCIOCCOMMAND into physio 184 */ 185 186 struct wd_ioctl { 187 LIST_ENTRY(wd_ioctl) wi_list; 188 struct buf wi_bp; 189 struct uio wi_uio; 190 struct iovec wi_iov; 191 atareq_t wi_atareq; 192 struct wd_softc *wi_softc; 193 }; 194 195 static struct wd_ioctl *wi_find(struct buf *); 196 static void wi_free(struct wd_ioctl *); 197 static struct wd_ioctl *wi_get(struct wd_softc *); 198 static void wdioctlstrategy(struct buf *); 199 200 static void wdrestart(void *); 201 static void wdstart1(struct wd_softc *, struct buf *, struct ata_xfer *); 202 static int wd_diskstart(device_t, struct buf *); 203 static int wd_dumpblocks(device_t, void *, daddr_t, int); 204 static void wd_iosize(device_t, int *); 205 static int wd_discard(device_t, off_t, off_t); 206 static void wdbioretry(void *); 207 static void wdbiorequeue(void *); 208 static void wddone(device_t, struct ata_xfer *); 209 static int wd_get_params(struct wd_softc *, struct ataparams *); 210 static void wd_set_geometry(struct wd_softc *); 211 static int wd_flushcache(struct wd_softc *, int); 212 static int wd_trim(struct wd_softc *, daddr_t, long); 213 static bool wd_shutdown(device_t, int); 214 215 static int wd_getcache(struct wd_softc *, int *); 216 static int wd_setcache(struct wd_softc *, int); 217 218 static void wd_sysctl_attach(struct wd_softc *); 219 static void wd_sysctl_detach(struct wd_softc *); 220 221 static const struct dkdriver wddkdriver = { 222 .d_open = wdopen, 223 .d_close = wdclose, 224 .d_strategy = wdstrategy, 225 .d_minphys = wdminphys, 226 .d_diskstart = wd_diskstart, 227 .d_dumpblocks = wd_dumpblocks, 228 .d_iosize = wd_iosize, 229 .d_firstopen = wd_firstopen, 230 .d_lastclose = wd_lastclose, 231 .d_discard = wd_discard 232 }; 233 234 #define WD_QUIRK_SPLIT_MOD15_WRITE 0x0001 /* must split certain writes */ 235 236 #define WD_QUIRK_FMT "\20\1SPLIT_MOD15_WRITE" 237 238 /* 239 * Quirk table for IDE drives. Put more-specific matches first, since 240 * a simple globing routine is used for matching. 241 */ 242 static const struct wd_quirk { 243 const char *wdq_match; /* inquiry pattern to match */ 244 int wdq_quirks; /* drive quirks */ 245 } wd_quirk_table[] = { 246 /* 247 * Some Seagate S-ATA drives have a PHY which can get confused 248 * with the way data is packetized by some S-ATA controllers. 249 * 250 * The work-around is to split in two any write transfer whose 251 * sector count % 15 == 1 (assuming 512 byte sectors). 252 * 253 * XXX This is an incomplete list. There are at least a couple 254 * XXX more model numbers. If you have trouble with such transfers 255 * XXX (8K is the most common) on Seagate S-ATA drives, please 256 * XXX notify thorpej (at) NetBSD.org. 257 * 258 * The ST360015AS has not yet been confirmed to have this 259 * issue, however, it is the only other drive in the 260 * Seagate Barracuda Serial ATA V family. 261 * 262 */ 263 { "ST3120023AS", WD_QUIRK_SPLIT_MOD15_WRITE }, 264 { "ST380023AS", WD_QUIRK_SPLIT_MOD15_WRITE }, 265 { "ST360015AS", WD_QUIRK_SPLIT_MOD15_WRITE }, 266 { NULL, 267 0 } 268 }; 269 270 static const struct wd_quirk * 271 wd_lookup_quirks(const char *name) 272 { 273 const struct wd_quirk *wdq; 274 const char *estr; 275 276 for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) { 277 /* 278 * We only want exact matches (which include matches 279 * against globbing characters). 280 */ 281 if (pmatch(name, wdq->wdq_match, &estr) == 2) 282 return (wdq); 283 } 284 return (NULL); 285 } 286 287 static int 288 wdprobe(device_t parent, cfdata_t match, void *aux) 289 { 290 struct ata_device *adev = aux; 291 292 if (adev == NULL) 293 return 0; 294 if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA) 295 return 0; 296 297 if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT && 298 match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive) 299 return 0; 300 return 1; 301 } 302 303 static void 304 wdattach(device_t parent, device_t self, void *aux) 305 { 306 struct wd_softc *wd = device_private(self); 307 struct dk_softc *dksc = &wd->sc_dksc; 308 struct ata_device *adev= aux; 309 int i, blank; 310 uint32_t firstaligned = 0, alignment = 1; 311 char tbuf[41],pbuf[9], c, *p, *q; 312 const struct wd_quirk *wdq; 313 int dtype = DKTYPE_UNKNOWN; 314 315 dksc->sc_dev = self; 316 317 ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE); 318 mutex_init(&wd->sc_lock, MUTEX_DEFAULT, IPL_BIO); 319 #ifdef WD_SOFTBADSECT 320 SLIST_INIT(&wd->sc_bslist); 321 cv_init(&wd->sc_bslist_cv, "wdbadsect"); 322 #endif 323 wd->atabus = adev->adev_bustype; 324 wd->inflight = 0; 325 wd->drvp = adev->adev_drv_data; 326 327 wd->drvp->drv_openings = 1; 328 wd->drvp->drv_done = wddone; 329 wd->drvp->drv_softc = dksc->sc_dev; /* done in atabusconfig_thread() 330 but too late */ 331 332 SLIST_INIT(&wd->sc_retry_list); 333 SLIST_INIT(&wd->sc_requeue_list); 334 callout_init(&wd->sc_retry_callout, 0); /* XXX MPSAFE */ 335 callout_init(&wd->sc_requeue_callout, 0); /* XXX MPSAFE */ 336 callout_init(&wd->sc_restart_diskqueue, 0); /* XXX MPSAFE */ 337 338 aprint_naive("\n"); 339 aprint_normal("\n"); 340 341 /* read our drive info */ 342 if (wd_get_params(wd, &wd->sc_params) != 0) { 343 aprint_error_dev(self, "IDENTIFY failed\n"); 344 goto out; 345 } 346 347 for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0; 348 i < sizeof(wd->sc_params.atap_model); i++) { 349 c = *p++; 350 if (c == '\0') 351 break; 352 if (c != ' ') { 353 if (blank) { 354 *q++ = ' '; 355 blank = 0; 356 } 357 *q++ = c; 358 } else 359 blank = 1; 360 } 361 *q++ = '\0'; 362 363 wd->sc_typename = kmem_asprintf("%s", tbuf); 364 aprint_normal_dev(self, "<%s>\n", wd->sc_typename); 365 366 wdq = wd_lookup_quirks(tbuf); 367 if (wdq != NULL) 368 wd->sc_quirks = wdq->wdq_quirks; 369 370 if (wd->sc_quirks != 0) { 371 char sbuf[sizeof(WD_QUIRK_FMT) + 64]; 372 snprintb(sbuf, sizeof(sbuf), WD_QUIRK_FMT, wd->sc_quirks); 373 aprint_normal_dev(self, "quirks %s\n", sbuf); 374 375 if (wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) { 376 aprint_error_dev(self, "drive corrupts write transfers with certain controllers, consider replacing\n"); 377 } 378 } 379 380 if ((wd->sc_params.atap_multi & 0xff) > 1) { 381 wd->drvp->multi = wd->sc_params.atap_multi & 0xff; 382 } else { 383 wd->drvp->multi = 1; 384 } 385 386 aprint_verbose_dev(self, "drive supports %d-sector PIO transfers,", 387 wd->drvp->multi); 388 389 /* 48-bit LBA addressing */ 390 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) 391 wd->sc_flags |= WDF_LBA48; 392 393 /* Prior to ATA-4, LBA was optional. */ 394 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) 395 wd->sc_flags |= WDF_LBA; 396 #if 0 397 /* ATA-4 requires LBA. */ 398 if (wd->sc_params.atap_ataversion != 0xffff && 399 wd->sc_params.atap_ataversion >= WDC_VER_ATA4) 400 wd->sc_flags |= WDF_LBA; 401 #endif 402 403 if ((wd->sc_flags & WDF_LBA48) != 0) { 404 aprint_verbose(" LBA48 addressing\n"); 405 wd->sc_capacity = 406 ((uint64_t) wd->sc_params.atap_max_lba[3] << 48) | 407 ((uint64_t) wd->sc_params.atap_max_lba[2] << 32) | 408 ((uint64_t) wd->sc_params.atap_max_lba[1] << 16) | 409 ((uint64_t) wd->sc_params.atap_max_lba[0] << 0); 410 wd->sc_capacity28 = 411 (wd->sc_params.atap_capacity[1] << 16) | 412 wd->sc_params.atap_capacity[0]; 413 /* 414 * Force LBA48 addressing for invalid numbers. 415 */ 416 if (wd->sc_capacity28 > 0xfffffff) 417 wd->sc_capacity28 = 0xfffffff; 418 } else if ((wd->sc_flags & WDF_LBA) != 0) { 419 aprint_verbose(" LBA addressing\n"); 420 wd->sc_capacity28 = 421 (wd->sc_params.atap_capacity[1] << 16) | 422 wd->sc_params.atap_capacity[0]; 423 /* 424 * Limit capacity to LBA28 numbers to avoid overflow. 425 */ 426 if (wd->sc_capacity28 > 0xfffffff) 427 wd->sc_capacity28 = 0xfffffff; 428 wd->sc_capacity = wd->sc_capacity28; 429 } else { 430 aprint_verbose(" chs addressing\n"); 431 wd->sc_capacity = 432 wd->sc_params.atap_cylinders * 433 wd->sc_params.atap_heads * 434 wd->sc_params.atap_sectors; 435 /* 436 * LBA28 size is ignored for CHS addressing. Use a reasonable 437 * value for debugging. The CHS values may be artificial and 438 * are mostly ignored. 439 */ 440 if (wd->sc_capacity < 0xfffffff) 441 wd->sc_capacity28 = wd->sc_capacity; 442 else 443 wd->sc_capacity28 = 0xfffffff; 444 } 445 if ((wd->sc_params.atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID 446 && ((wd->sc_params.atap_secsz & ATA_SECSZ_LLS) != 0)) { 447 wd->sc_blksize = 2ULL * 448 ((uint32_t)((wd->sc_params.atap_lls_secsz[1] << 16) | 449 wd->sc_params.atap_lls_secsz[0])); 450 } else { 451 wd->sc_blksize = 512; 452 } 453 if ((wd->sc_params.atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID 454 && ((wd->sc_params.atap_secsz & ATA_SECSZ_LPS) != 0)) { 455 alignment = 1 << 456 (wd->sc_params.atap_secsz & ATA_SECSZ_LPS_SZMSK); 457 if ((wd->sc_params.atap_logical_align & ATA_LA_VALID_MASK) == 458 ATA_LA_VALID) { 459 firstaligned = 460 wd->sc_params.atap_logical_align & ATA_LA_MASK; 461 } 462 } 463 wd->sc_capacity512 = (wd->sc_capacity * wd->sc_blksize) / DEV_BSIZE; 464 format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * wd->sc_blksize); 465 aprint_normal_dev(self, "%s, %d cyl, %d head, %d sec, " 466 "%d bytes/sect x %llu sectors", 467 pbuf, 468 (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity / 469 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) : 470 wd->sc_params.atap_cylinders, 471 wd->sc_params.atap_heads, wd->sc_params.atap_sectors, 472 wd->sc_blksize, (unsigned long long)wd->sc_capacity); 473 if (alignment != 1) { 474 aprint_normal(" (%d bytes/physsect", 475 alignment * wd->sc_blksize); 476 if (firstaligned != 0) { 477 aprint_normal("; first aligned sector: %jd", 478 (intmax_t)firstaligned); 479 } 480 aprint_normal(")"); 481 } 482 aprint_normal("\n"); 483 484 ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n", 485 device_xname(self), wd->sc_params.atap_dmatiming_mimi, 486 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE); 487 488 if (wd->sc_blksize <= 0 || !powerof2(wd->sc_blksize) || 489 wd->sc_blksize < DEV_BSIZE || wd->sc_blksize > MAXPHYS) { 490 aprint_normal_dev(self, "WARNING: block size %u " 491 "might not actually work\n", wd->sc_blksize); 492 } 493 494 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) 495 dtype = DKTYPE_ST506; 496 else 497 dtype = DKTYPE_ESDI; 498 499 out: 500 /* 501 * Initialize and attach the disk structure. 502 */ 503 dk_init(dksc, self, dtype); 504 disk_init(&dksc->sc_dkdev, dksc->sc_xname, &wddkdriver); 505 506 /* Attach dk and disk subsystems */ 507 dk_attach(dksc); 508 disk_attach(&dksc->sc_dkdev); 509 wd_set_geometry(wd); 510 511 bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK); 512 513 /* reference to label structure, used by ata code */ 514 wd->drvp->lp = dksc->sc_dkdev.dk_label; 515 516 /* Discover wedges on this disk. */ 517 dkwedge_discover(&dksc->sc_dkdev); 518 519 if (!pmf_device_register1(self, wd_suspend, NULL, wd_shutdown)) 520 aprint_error_dev(self, "couldn't establish power handler\n"); 521 522 wd_sysctl_attach(wd); 523 } 524 525 static bool 526 wd_suspend(device_t dv, const pmf_qual_t *qual) 527 { 528 struct wd_softc *sc = device_private(dv); 529 530 /* the adapter needs to be enabled */ 531 if (sc->atabus->ata_addref(sc->drvp)) 532 return true; /* no need to complain */ 533 534 wd_flushcache(sc, AT_WAIT); 535 wd_standby(sc, AT_WAIT); 536 537 sc->atabus->ata_delref(sc->drvp); 538 return true; 539 } 540 541 static int 542 wddetach(device_t self, int flags) 543 { 544 struct wd_softc *wd = device_private(self); 545 struct dk_softc *dksc = &wd->sc_dksc; 546 int bmaj, cmaj, i, mn, rc; 547 548 if ((rc = disk_begindetach(&dksc->sc_dkdev, wd_lastclose, self, flags)) != 0) 549 return rc; 550 551 /* locate the major number */ 552 bmaj = bdevsw_lookup_major(&wd_bdevsw); 553 cmaj = cdevsw_lookup_major(&wd_cdevsw); 554 555 /* Nuke the vnodes for any open instances. */ 556 for (i = 0; i < MAXPARTITIONS; i++) { 557 mn = WDMINOR(device_unit(self), i); 558 vdevgone(bmaj, mn, mn, VBLK); 559 vdevgone(cmaj, mn, mn, VCHR); 560 } 561 562 dk_drain(dksc); 563 564 /* Kill off any pending commands. */ 565 mutex_enter(&wd->sc_lock); 566 wd->atabus->ata_killpending(wd->drvp); 567 568 callout_halt(&wd->sc_retry_callout, &wd->sc_lock); 569 callout_destroy(&wd->sc_retry_callout); 570 callout_halt(&wd->sc_requeue_callout, &wd->sc_lock); 571 callout_destroy(&wd->sc_requeue_callout); 572 callout_halt(&wd->sc_restart_diskqueue, &wd->sc_lock); 573 callout_destroy(&wd->sc_restart_diskqueue); 574 575 mutex_exit(&wd->sc_lock); 576 577 bufq_free(dksc->sc_bufq); 578 579 /* Delete all of our wedges. */ 580 dkwedge_delall(&dksc->sc_dkdev); 581 582 if (flags & DETACH_POWEROFF) 583 wd_standby(wd, AT_POLL); 584 585 /* Detach from the disk list. */ 586 disk_detach(&dksc->sc_dkdev); 587 disk_destroy(&dksc->sc_dkdev); 588 589 dk_detach(dksc); 590 591 #ifdef WD_SOFTBADSECT 592 /* Clean out the bad sector list */ 593 while (!SLIST_EMPTY(&wd->sc_bslist)) { 594 struct disk_badsectors *dbs = SLIST_FIRST(&wd->sc_bslist); 595 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next); 596 kmem_free(dbs, sizeof(*dbs)); 597 } 598 wd->sc_bscount = 0; 599 #endif 600 if (wd->sc_typename != NULL) { 601 kmem_free(wd->sc_typename, strlen(wd->sc_typename) + 1); 602 wd->sc_typename = NULL; 603 } 604 605 pmf_device_deregister(self); 606 607 wd_sysctl_detach(wd); 608 609 #ifdef WD_SOFTBADSECT 610 KASSERT(SLIST_EMPTY(&wd->sc_bslist)); 611 cv_destroy(&wd->sc_bslist_cv); 612 #endif 613 614 mutex_destroy(&wd->sc_lock); 615 616 wd->drvp->drive_type = ATA_DRIVET_NONE; /* no drive any more here */ 617 wd->drvp->drive_flags = 0; 618 619 return (0); 620 } 621 622 /* 623 * Read/write routine for a buffer. Validates the arguments and schedules the 624 * transfer. Does not wait for the transfer to complete. 625 */ 626 static void 627 wdstrategy(struct buf *bp) 628 { 629 struct wd_softc *wd = 630 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev)); 631 struct dk_softc *dksc = &wd->sc_dksc; 632 633 ATADEBUG_PRINT(("wdstrategy (%s)\n", dksc->sc_xname), 634 DEBUG_XFERS); 635 636 /* If device invalidated (e.g. media change, door open, 637 * device detachment), then error. 638 */ 639 if ((wd->sc_flags & WDF_LOADED) == 0 || 640 !device_is_enabled(dksc->sc_dev)) 641 goto err; 642 643 #ifdef WD_SOFTBADSECT 644 /* 645 * If the transfer about to be attempted contains only a block that 646 * is known to be bad then return an error for the transfer without 647 * even attempting to start a transfer up under the premis that we 648 * will just end up doing more retries for a transfer that will end 649 * up failing again. 650 */ 651 if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) { 652 struct disklabel *lp = dksc->sc_dkdev.dk_label; 653 struct disk_badsectors *dbs; 654 daddr_t blkno, maxblk; 655 656 /* convert the block number to absolute */ 657 if (lp->d_secsize >= DEV_BSIZE) 658 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 659 else 660 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 661 if (WDPART(bp->b_dev) != RAW_PART) 662 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset; 663 maxblk = blkno + (bp->b_bcount / wd->sc_blksize) - 1; 664 665 mutex_enter(&wd->sc_lock); 666 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) 667 if ((dbs->dbs_min <= bp->b_rawblkno && 668 bp->b_rawblkno <= dbs->dbs_max) || 669 (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){ 670 mutex_exit(&wd->sc_lock); 671 goto err; 672 } 673 mutex_exit(&wd->sc_lock); 674 } 675 #endif 676 677 dk_strategy(dksc, bp); 678 return; 679 680 err: 681 bp->b_error = EIO; 682 bp->b_resid = bp->b_bcount; 683 biodone(bp); 684 } 685 686 static void 687 wdstart1(struct wd_softc *wd, struct buf *bp, struct ata_xfer *xfer) 688 { 689 struct dk_softc *dksc = &wd->sc_dksc; 690 const uint32_t secsize = dksc->sc_dkdev.dk_geom.dg_secsize; 691 692 KASSERT(bp == xfer->c_bio.bp || xfer->c_bio.bp == NULL); 693 KASSERT((xfer->c_flags & (C_WAITACT|C_FREE)) == 0); 694 KASSERT(mutex_owned(&wd->sc_lock)); 695 696 /* Reset state, so that retries don't use stale info */ 697 if (__predict_false(xfer->c_retries > 0)) { 698 xfer->c_flags = 0; 699 memset(&xfer->c_bio, 0, sizeof(xfer->c_bio)); 700 } 701 702 xfer->c_bio.blkno = bp->b_rawblkno; 703 xfer->c_bio.bcount = bp->b_bcount; 704 xfer->c_bio.databuf = bp->b_data; 705 xfer->c_bio.blkdone = 0; 706 xfer->c_bio.bp = bp; 707 708 /* Adjust blkno and bcount if xfer has been already partially done */ 709 if (__predict_false(xfer->c_skip > 0)) { 710 KASSERT(xfer->c_skip < xfer->c_bio.bcount); 711 KASSERT((xfer->c_skip % secsize) == 0); 712 xfer->c_bio.bcount -= xfer->c_skip; 713 xfer->c_bio.blkno += xfer->c_skip / secsize; 714 } 715 716 #ifdef WD_CHAOS_MONKEY 717 /* 718 * Override blkno to be over device capacity to trigger error, 719 * but only if it's read, to avoid trashing disk contents should 720 * the command be clipped, or otherwise misinterpreted, by the 721 * driver or controller. 722 */ 723 if (BUF_ISREAD(bp) && xfer->c_retries == 0 && wd->drv_chaos_freq > 0 && 724 (++wd->drv_chaos_cnt % wd->drv_chaos_freq) == 0) { 725 device_printf(dksc->sc_dev, "%s: chaos xfer %"PRIxPTR"\n", 726 __func__, (intptr_t)xfer & PAGE_MASK); 727 xfer->c_bio.blkno = 7777777 + wd->sc_capacity; 728 xfer->c_flags |= C_CHAOS; 729 } 730 #endif 731 732 /* 733 * If we're retrying, retry in single-sector mode. This will give us 734 * the sector number of the problem, and will eventually allow the 735 * transfer to succeed. If FUA is requested, we can't actually 736 * do this, as ATA_SINGLE is usually executed as PIO transfer by drivers 737 * which support it, and that isn't compatible with NCQ/FUA. 738 */ 739 if (xfer->c_retries >= WDIORETRIES_SINGLE && 740 (bp->b_flags & B_MEDIA_FUA) == 0) 741 xfer->c_bio.flags = ATA_SINGLE; 742 else 743 xfer->c_bio.flags = 0; 744 745 /* 746 * request LBA48 transfers when supported by the controller 747 * and needed by transfer offset or size. 748 */ 749 if (wd->sc_flags & WDF_LBA48 && 750 (((xfer->c_bio.blkno + xfer->c_bio.bcount / secsize) > 751 wd->sc_capacity28) || 752 ((xfer->c_bio.bcount / secsize) > 128))) 753 xfer->c_bio.flags |= ATA_LBA48; 754 755 /* 756 * If NCQ was negotiated, always use it for the first several attempts. 757 * Since device cancels all outstanding requests on error, downgrade 758 * to non-NCQ on retry, so that the retried transfer would not cause 759 * cascade failure for the other transfers if it fails again. 760 * If FUA was requested, we can't downgrade, as that would violate 761 * the semantics - FUA would not be honored. In that case, continue 762 * retrying with NCQ. 763 */ 764 if (WD_USE_NCQ(wd) && (xfer->c_retries < WDIORETRIES_SINGLE || 765 (bp->b_flags & B_MEDIA_FUA) != 0)) { 766 xfer->c_bio.flags |= ATA_LBA48; 767 xfer->c_flags |= C_NCQ; 768 769 if (WD_USE_NCQ_PRIO(wd) && 770 BIO_GETPRIO(bp) == BPRIO_TIMECRITICAL) 771 xfer->c_bio.flags |= ATA_PRIO_HIGH; 772 } 773 774 if (wd->sc_flags & WDF_LBA) 775 xfer->c_bio.flags |= ATA_LBA; 776 if (bp->b_flags & B_READ) { 777 xfer->c_bio.flags |= ATA_READ; 778 } else { 779 /* it's a write */ 780 wd->sc_flags |= WDF_DIRTY; 781 } 782 if (bp->b_flags & B_MEDIA_FUA) { 783 /* If not using NCQ, the command WRITE DMA FUA EXT is LBA48 */ 784 KASSERT((wd->sc_flags & WDF_LBA48) != 0); 785 if ((xfer->c_flags & C_NCQ) == 0) 786 xfer->c_bio.flags |= ATA_LBA48; 787 788 xfer->c_bio.flags |= ATA_FUA; 789 } 790 791 if (xfer->c_retries == 0) 792 wd->inflight++; 793 mutex_exit(&wd->sc_lock); 794 795 /* Queue the xfer */ 796 wd->atabus->ata_bio(wd->drvp, xfer); 797 798 mutex_enter(&wd->sc_lock); 799 } 800 801 static int 802 wd_diskstart(device_t dev, struct buf *bp) 803 { 804 struct wd_softc *wd = device_private(dev); 805 #ifdef ATADEBUG 806 struct dk_softc *dksc = &wd->sc_dksc; 807 #endif 808 struct ata_xfer *xfer; 809 struct ata_channel *chp; 810 unsigned openings; 811 int ticks; 812 813 mutex_enter(&wd->sc_lock); 814 815 chp = wd->drvp->chnl_softc; 816 817 ata_channel_lock(chp); 818 openings = ata_queue_openings(chp); 819 ata_channel_unlock(chp); 820 821 openings = uimin(openings, wd->drvp->drv_openings); 822 823 if (wd->inflight >= openings) { 824 /* 825 * pretend we run out of memory when the queue is full, 826 * so that the operation is retried after a minimal 827 * delay. 828 */ 829 xfer = NULL; 830 ticks = 1; 831 } else { 832 /* 833 * If there is no available memory, retry later. This 834 * happens very rarely and only under memory pressure, 835 * so wait relatively long before retry. 836 */ 837 xfer = ata_get_xfer(chp, false); 838 ticks = hz/2; 839 } 840 841 if (xfer == NULL) { 842 ATADEBUG_PRINT(("wd_diskstart %s no xfer\n", 843 dksc->sc_xname), DEBUG_XFERS); 844 845 /* 846 * The disk queue is pushed automatically when an I/O 847 * operation finishes or another one is queued. We 848 * need this extra timeout because an ATA channel 849 * might be shared by more than one disk queue and 850 * all queues need to be restarted when another slot 851 * becomes available. 852 */ 853 if (!callout_pending(&wd->sc_restart_diskqueue)) { 854 callout_reset(&wd->sc_restart_diskqueue, ticks, 855 wdrestart, dev); 856 } 857 858 mutex_exit(&wd->sc_lock); 859 return EAGAIN; 860 } 861 862 wdstart1(wd, bp, xfer); 863 864 mutex_exit(&wd->sc_lock); 865 866 return 0; 867 } 868 869 /* 870 * Queue a drive for I/O. 871 */ 872 static void 873 wdrestart(void *x) 874 { 875 device_t self = x; 876 struct wd_softc *wd = device_private(self); 877 struct dk_softc *dksc = &wd->sc_dksc; 878 879 ATADEBUG_PRINT(("wdstart %s\n", dksc->sc_xname), 880 DEBUG_XFERS); 881 882 if (!device_is_active(dksc->sc_dev)) 883 return; 884 885 dk_start(dksc, NULL); 886 } 887 888 static void 889 wddone(device_t self, struct ata_xfer *xfer) 890 { 891 struct wd_softc *wd = device_private(self); 892 struct dk_softc *dksc = &wd->sc_dksc; 893 const char *errmsg; 894 int do_perror = 0; 895 struct buf *bp; 896 897 ATADEBUG_PRINT(("wddone %s\n", dksc->sc_xname), 898 DEBUG_XFERS); 899 900 if (__predict_false(wddoingadump)) { 901 /* just drop it to the floor */ 902 ata_free_xfer(wd->drvp->chnl_softc, xfer); 903 return; 904 } 905 906 bp = xfer->c_bio.bp; 907 KASSERT(bp != NULL); 908 909 bp->b_resid = xfer->c_bio.bcount; 910 switch (xfer->c_bio.error) { 911 case ERR_DMA: 912 errmsg = "DMA error"; 913 goto retry; 914 case ERR_DF: 915 errmsg = "device fault"; 916 goto retry; 917 case TIMEOUT: 918 errmsg = "device timeout"; 919 goto retry; 920 case REQUEUE: 921 errmsg = "requeue"; 922 goto retry2; 923 case ERR_RESET: 924 errmsg = "channel reset"; 925 goto retry2; 926 case ERROR: 927 /* Don't care about media change bits */ 928 if (xfer->c_bio.r_error != 0 && 929 (xfer->c_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0) 930 goto noerror; 931 errmsg = "error"; 932 do_perror = 1; 933 retry: /* Just reset and retry. Can we do more ? */ 934 if ((xfer->c_flags & C_RECOVERED) == 0) { 935 int wflags = (xfer->c_flags & C_POLL) ? AT_POLL : 0; 936 ata_channel_lock(wd->drvp->chnl_softc); 937 ata_thread_run(wd->drvp->chnl_softc, wflags, 938 ATACH_TH_DRIVE_RESET, wd->drvp->drive); 939 ata_channel_unlock(wd->drvp->chnl_softc); 940 } 941 retry2: 942 mutex_enter(&wd->sc_lock); 943 944 diskerr(bp, "wd", errmsg, LOG_PRINTF, 945 xfer->c_bio.blkdone, dksc->sc_dkdev.dk_label); 946 if (xfer->c_retries < WDIORETRIES) 947 printf(", xfer %"PRIxPTR", retry %d", 948 (intptr_t)xfer & PAGE_MASK, 949 xfer->c_retries); 950 printf("\n"); 951 if (do_perror) 952 wdperror(wd, xfer); 953 954 if (xfer->c_retries < WDIORETRIES) { 955 xfer->c_retries++; 956 957 /* Rerun ASAP if just requeued */ 958 if (xfer->c_bio.error == REQUEUE) { 959 SLIST_INSERT_HEAD(&wd->sc_requeue_list, xfer, 960 c_retrychain); 961 callout_reset(&wd->sc_requeue_callout, 962 1, wdbiorequeue, wd); 963 } else { 964 SLIST_INSERT_HEAD(&wd->sc_retry_list, xfer, 965 c_retrychain); 966 callout_reset(&wd->sc_retry_callout, 967 RECOVERYTIME, wdbioretry, wd); 968 } 969 970 mutex_exit(&wd->sc_lock); 971 return; 972 } 973 974 mutex_exit(&wd->sc_lock); 975 976 #ifdef WD_SOFTBADSECT 977 /* 978 * Not all errors indicate a failed block but those that do, 979 * put the block on the bad-block list for the device. Only 980 * do this for reads because the drive should do it for writes, 981 * itself, according to Manuel. 982 */ 983 if ((bp->b_flags & B_READ) && 984 ((wd->drvp->ata_vers >= 4 && xfer->c_bio.r_error & 64) || 985 (wd->drvp->ata_vers < 4 && xfer->c_bio.r_error & 192))) { 986 struct disk_badsectors *dbs; 987 988 dbs = kmem_zalloc(sizeof *dbs, KM_NOSLEEP); 989 if (dbs == NULL) { 990 device_printf(dksc->sc_dev, 991 "failed to add bad block to list\n"); 992 goto out; 993 } 994 995 dbs->dbs_min = bp->b_rawblkno; 996 dbs->dbs_max = dbs->dbs_min + 997 (bp->b_bcount /wd->sc_blksize) - 1; 998 microtime(&dbs->dbs_failedat); 999 1000 mutex_enter(&wd->sc_lock); 1001 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next); 1002 wd->sc_bscount++; 1003 mutex_exit(&wd->sc_lock); 1004 } 1005 out: 1006 #endif 1007 bp->b_error = EIO; 1008 break; 1009 case NOERROR: 1010 #ifdef WD_CHAOS_MONKEY 1011 /* 1012 * For example Parallels AHCI emulation doesn't actually 1013 * return error for the invalid I/O, so just re-run 1014 * the request and do not panic. 1015 */ 1016 if (__predict_false(xfer->c_flags & C_CHAOS)) { 1017 xfer->c_bio.error = REQUEUE; 1018 errmsg = "chaos noerror"; 1019 goto retry2; 1020 } 1021 #endif 1022 1023 noerror: if ((xfer->c_bio.flags & ATA_CORR) || xfer->c_retries > 0) 1024 device_printf(dksc->sc_dev, 1025 "soft error (corrected) xfer %"PRIxPTR"\n", 1026 (intptr_t)xfer & PAGE_MASK); 1027 break; 1028 case ERR_NODEV: 1029 bp->b_error = EIO; 1030 break; 1031 } 1032 if (__predict_false(bp->b_error != 0) && bp->b_resid == 0) { 1033 /* 1034 * the disk or controller sometimes report a complete 1035 * xfer, when there has been an error. This is wrong, 1036 * assume nothing got transferred in this case 1037 */ 1038 bp->b_resid = bp->b_bcount; 1039 } 1040 1041 ata_free_xfer(wd->drvp->chnl_softc, xfer); 1042 1043 mutex_enter(&wd->sc_lock); 1044 wd->inflight--; 1045 mutex_exit(&wd->sc_lock); 1046 dk_done(dksc, bp); 1047 dk_start(dksc, NULL); 1048 } 1049 1050 static void 1051 wdbioretry(void *v) 1052 { 1053 struct wd_softc *wd = v; 1054 struct ata_xfer *xfer; 1055 1056 ATADEBUG_PRINT(("%s %s\n", __func__, wd->sc_dksc.sc_xname), 1057 DEBUG_XFERS); 1058 1059 mutex_enter(&wd->sc_lock); 1060 while ((xfer = SLIST_FIRST(&wd->sc_retry_list))) { 1061 SLIST_REMOVE_HEAD(&wd->sc_retry_list, c_retrychain); 1062 wdstart1(wd, xfer->c_bio.bp, xfer); 1063 } 1064 mutex_exit(&wd->sc_lock); 1065 } 1066 1067 static void 1068 wdbiorequeue(void *v) 1069 { 1070 struct wd_softc *wd = v; 1071 struct ata_xfer *xfer; 1072 1073 ATADEBUG_PRINT(("%s %s\n", __func__, wd->sc_dksc.sc_xname), 1074 DEBUG_XFERS); 1075 1076 mutex_enter(&wd->sc_lock); 1077 while ((xfer = SLIST_FIRST(&wd->sc_requeue_list))) { 1078 SLIST_REMOVE_HEAD(&wd->sc_requeue_list, c_retrychain); 1079 wdstart1(wd, xfer->c_bio.bp, xfer); 1080 } 1081 mutex_exit(&wd->sc_lock); 1082 } 1083 1084 static void 1085 wdminphys(struct buf *bp) 1086 { 1087 const struct wd_softc * const wd = 1088 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev)); 1089 int maxsectors; 1090 1091 /* 1092 * The limit is actually 65536 for LBA48 and 256 for non-LBA48, 1093 * but that requires to set the count for the ATA command 1094 * to 0, which is somewhat error prone, so better stay safe. 1095 */ 1096 if (wd->sc_flags & WDF_LBA48) 1097 maxsectors = 65535; 1098 else 1099 maxsectors = 128; 1100 1101 if (bp->b_bcount > (wd->sc_blksize * maxsectors)) 1102 bp->b_bcount = (wd->sc_blksize * maxsectors); 1103 1104 minphys(bp); 1105 } 1106 1107 static void 1108 wd_iosize(device_t dev, int *count) 1109 { 1110 struct buf B; 1111 int bmaj; 1112 1113 bmaj = bdevsw_lookup_major(&wd_bdevsw); 1114 B.b_dev = MAKEWDDEV(bmaj,device_unit(dev),RAW_PART); 1115 B.b_bcount = *count; 1116 1117 wdminphys(&B); 1118 1119 *count = B.b_bcount; 1120 } 1121 1122 static int 1123 wdread(dev_t dev, struct uio *uio, int flags) 1124 { 1125 1126 ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS); 1127 return (physio(wdstrategy, NULL, dev, B_READ, wdminphys, uio)); 1128 } 1129 1130 static int 1131 wdwrite(dev_t dev, struct uio *uio, int flags) 1132 { 1133 1134 ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS); 1135 return (physio(wdstrategy, NULL, dev, B_WRITE, wdminphys, uio)); 1136 } 1137 1138 /* 1139 * The bad144 table is consulted in _wdc_ata_bio_start(), which is 1140 * called with the channel lock held. Thus, we must serialize using 1141 * that lock when manipulating anything related to the bad144 table. 1142 */ 1143 #define WD_BAD144_LOCK(wd) mutex_enter(&(wd)->drvp->chnl_softc->ch_lock) 1144 #define WD_BAD144_UNLOCK(wd) mutex_exit(&(wd)->drvp->chnl_softc->ch_lock) 1145 1146 static void 1147 wd_load_bad144(struct wd_softc *wd, dev_t dev) 1148 { 1149 struct dk_softc * const dksc = &wd->sc_dksc; 1150 const struct disklabel * const lp = dksc->sc_dkdev.dk_label; 1151 struct bad144_context *bad144 = NULL; 1152 int error = 0; 1153 1154 /* 1155 * We are called from wdopen() if the disklabel indicates there 1156 * is a bad144 table present on the disk. We only want to attempt 1157 * to read the bad144 table once, on the very first open, because 1158 * we are going to print diagnostic information in case there is 1159 * some inconsistency that prevents bad144 from working. 1160 */ 1161 1162 WD_BAD144_LOCK(wd); 1163 1164 if (wd->sc_tried_bad144 || wd->drvp->bad144 != NULL) { 1165 /* 1166 * This is not the first pass through, or someone raced 1167 * with us and beat us to it. 1168 */ 1169 goto out_unlock; 1170 } 1171 1172 /* Don't hold this lock while we allocate memory and perform I/O. */ 1173 WD_BAD144_UNLOCK(wd); 1174 1175 bad144 = bad144_init(lp->d_secsize, lp->d_ncylinders, lp->d_ntracks, 1176 lp->d_nsectors); 1177 if (bad144 == NULL) { 1178 printf("%s: disklabel indicates BAD144 but " 1179 "BAD144 not supported on this drive", 1180 device_xname(dksc->sc_dev)); 1181 goto out; 1182 } 1183 1184 error = bad144_load(bad144, WDLABELDEV(dev), wdstrategy); 1185 if (error == ESRCH) { 1186 /* No bad144 table on this drive?? */ 1187 printf("%s: disklabel indicates BAD144 but " 1188 "BAD144 table not found", 1189 device_xname(dksc->sc_dev)); 1190 } else if (error == EINVAL) { 1191 printf("%s: invalid BAD144 table", 1192 device_xname(dksc->sc_dev)); 1193 } else if (error) { 1194 printf("%s: error %d reading BAD144 table", 1195 device_xname(dksc->sc_dev), error); 1196 } 1197 1198 out: 1199 WD_BAD144_LOCK(wd); 1200 if (bad144 != NULL && error == 0) { 1201 /* 1202 * Don't replace the in-use table if we lost the 1203 * race; the one we just read in is probably stale. 1204 */ 1205 if (wd->drvp->bad144 == NULL) { 1206 wd->drvp->bad144 = bad144; 1207 bad144 = NULL; 1208 } 1209 } 1210 wd->sc_tried_bad144 = true; 1211 1212 out_unlock: 1213 WD_BAD144_UNLOCK(wd); 1214 if (bad144 != NULL) { 1215 bad144_fini(bad144); 1216 } 1217 } 1218 1219 static int 1220 wdopen(dev_t dev, int flag, int fmt, struct lwp *l) 1221 { 1222 struct wd_softc *wd; 1223 struct dk_softc *dksc; 1224 int unit, part, error; 1225 1226 ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS); 1227 unit = WDUNIT(dev); 1228 wd = device_lookup_private(&wd_cd, unit); 1229 if (wd == NULL) 1230 return (ENXIO); 1231 dksc = &wd->sc_dksc; 1232 1233 if (! device_is_active(dksc->sc_dev)) 1234 return (ENODEV); 1235 1236 part = WDPART(dev); 1237 1238 if (wd->sc_capacity == 0) 1239 return (ENODEV); 1240 1241 /* 1242 * If any partition is open, but the disk has been invalidated, 1243 * disallow further opens. 1244 */ 1245 if ((wd->sc_flags & (WDF_OPEN | WDF_LOADED)) == WDF_OPEN) { 1246 if (part != RAW_PART || fmt != S_IFCHR) 1247 return EIO; 1248 } 1249 1250 error = dk_open(dksc, dev, flag, fmt, l); 1251 1252 /* 1253 * If the disklabel indicates we have a bad144 table, 1254 * read that in if we haven't already. 1255 */ 1256 if (error == 0 && 1257 (dksc->sc_dkdev.dk_label->d_flags & D_BADSECT) != 0) { 1258 wd_load_bad144(wd, dev); 1259 } 1260 1261 return error; 1262 } 1263 1264 /* 1265 * Serialized by caller 1266 */ 1267 static int 1268 wd_firstopen(device_t self, dev_t dev, int flag, int fmt) 1269 { 1270 struct wd_softc *wd = device_private(self); 1271 struct dk_softc *dksc = &wd->sc_dksc; 1272 int error; 1273 1274 error = wd->atabus->ata_addref(wd->drvp); 1275 if (error) 1276 return error; 1277 1278 if ((wd->sc_flags & WDF_LOADED) == 0) { 1279 int param_error; 1280 1281 /* Load the physical device parameters. */ 1282 param_error = wd_get_params(wd, &wd->sc_params); 1283 if (param_error != 0) { 1284 aprint_error_dev(dksc->sc_dev, "IDENTIFY failed\n"); 1285 error = EIO; 1286 goto bad; 1287 } 1288 wd_set_geometry(wd); 1289 wd->sc_flags |= WDF_LOADED; 1290 } 1291 1292 wd->sc_flags |= WDF_OPEN; 1293 return 0; 1294 1295 bad: 1296 wd->atabus->ata_delref(wd->drvp); 1297 return error; 1298 } 1299 1300 /* 1301 * Caller must hold wd->sc_dk.dk_openlock. 1302 */ 1303 static int 1304 wd_lastclose(device_t self) 1305 { 1306 struct wd_softc *wd = device_private(self); 1307 1308 KASSERTMSG(bufq_peek(wd->sc_dksc.sc_bufq) == NULL, "bufq not empty"); 1309 1310 if (wd->sc_flags & WDF_DIRTY) 1311 wd_flushcache(wd, AT_WAIT); 1312 1313 wd->atabus->ata_delref(wd->drvp); 1314 wd->sc_flags &= ~WDF_OPEN; 1315 1316 return 0; 1317 } 1318 1319 static int 1320 wdclose(dev_t dev, int flag, int fmt, struct lwp *l) 1321 { 1322 struct wd_softc *wd; 1323 struct dk_softc *dksc; 1324 int unit; 1325 1326 unit = WDUNIT(dev); 1327 wd = device_lookup_private(&wd_cd, unit); 1328 dksc = &wd->sc_dksc; 1329 1330 return dk_close(dksc, dev, flag, fmt, l); 1331 } 1332 1333 void 1334 wdperror(const struct wd_softc *wd, struct ata_xfer *xfer) 1335 { 1336 static const char *const errstr0_3[] = {"address mark not found", 1337 "track 0 not found", "aborted command", "media change requested", 1338 "id not found", "media changed", "uncorrectable data error", 1339 "bad block detected"}; 1340 static const char *const errstr4_5[] = { 1341 "obsolete (address mark not found)", 1342 "no media/write protected", "aborted command", 1343 "media change requested", "id not found", "media changed", 1344 "uncorrectable data error", "interface CRC error"}; 1345 const char *const *errstr; 1346 int i; 1347 const char *sep = ""; 1348 1349 const struct dk_softc *dksc = &wd->sc_dksc; 1350 const char *devname = dksc->sc_xname; 1351 struct ata_drive_datas *drvp = wd->drvp; 1352 int errno = xfer->c_bio.r_error; 1353 1354 if (drvp->ata_vers >= 4) 1355 errstr = errstr4_5; 1356 else 1357 errstr = errstr0_3; 1358 1359 printf("%s: (", devname); 1360 1361 if (errno == 0) 1362 printf("error not notified"); 1363 1364 for (i = 0; i < 8; i++) { 1365 if (errno & (1 << i)) { 1366 printf("%s%s", sep, errstr[i]); 1367 sep = ", "; 1368 } 1369 } 1370 printf(")\n"); 1371 } 1372 1373 static int 1374 wd_diocsbad(struct wd_softc *wd, const struct dkbad *dkb) 1375 { 1376 struct dk_softc *dksc = &wd->sc_dksc; 1377 const struct disklabel * const lp = dksc->sc_dkdev.dk_label; 1378 struct bad144_context *bad144, *obad144; 1379 int error; 1380 1381 bad144 = bad144_init(lp->d_secsize, lp->d_ncylinders, lp->d_ntracks, 1382 lp->d_nsectors); 1383 if (bad144 == NULL) { 1384 return ENOTSUP; 1385 } 1386 1387 error = bad144_set(bad144, dkb); 1388 if (error) { 1389 bad144_fini(bad144); 1390 return error; 1391 } 1392 1393 WD_BAD144_LOCK(wd); 1394 obad144 = wd->drvp->bad144; 1395 wd->drvp->bad144 = bad144; 1396 WD_BAD144_UNLOCK(wd); 1397 1398 if (obad144 != NULL) { 1399 bad144_fini(obad144); 1400 } 1401 1402 return 0; 1403 } 1404 1405 int 1406 wdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1407 { 1408 struct wd_softc *wd = 1409 device_lookup_private(&wd_cd, WDUNIT(dev)); 1410 struct dk_softc *dksc = &wd->sc_dksc; 1411 1412 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS); 1413 1414 if ((wd->sc_flags & WDF_LOADED) == 0) 1415 return EIO; 1416 1417 switch (cmd) { 1418 case DIOCSBAD: 1419 if ((flag & FWRITE) == 0) 1420 return EBADF; 1421 return wd_diocsbad(wd, (struct dkbad *)addr); 1422 1423 #ifdef WD_SOFTBADSECT 1424 case DIOCBSLIST: { 1425 uint32_t count, missing, skip; 1426 struct disk_badsecinfo dbsi; 1427 struct disk_badsectors *dbs, dbsbuf; 1428 size_t available; 1429 uint8_t *laddr; 1430 int error; 1431 1432 dbsi = *(struct disk_badsecinfo *)addr; 1433 missing = wd->sc_bscount; 1434 count = 0; 1435 available = dbsi.dbsi_bufsize; 1436 skip = dbsi.dbsi_skip; 1437 laddr = (uint8_t *)dbsi.dbsi_buffer; 1438 1439 /* 1440 * We start this loop with the expectation that all of the 1441 * entries will be missed and decrement this counter each 1442 * time we either skip over one (already copied out) or 1443 * we actually copy it back to user space. The structs 1444 * holding the bad sector information are copied directly 1445 * back to user space whilst the summary is returned via 1446 * the struct passed in via the ioctl. 1447 */ 1448 error = 0; 1449 mutex_enter(&wd->sc_lock); 1450 wd->sc_bslist_inuse++; 1451 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) { 1452 if (skip > 0) { 1453 missing--; 1454 skip--; 1455 continue; 1456 } 1457 if (available < sizeof(*dbs)) 1458 break; 1459 available -= sizeof(*dbs); 1460 memset(&dbsbuf, 0, sizeof(dbsbuf)); 1461 dbsbuf.dbs_min = dbs->dbs_min; 1462 dbsbuf.dbs_max = dbs->dbs_max; 1463 dbsbuf.dbs_failedat = dbs->dbs_failedat; 1464 mutex_exit(&wd->sc_lock); 1465 error = copyout(&dbsbuf, laddr, sizeof(dbsbuf)); 1466 mutex_enter(&wd->sc_lock); 1467 if (error) 1468 break; 1469 laddr += sizeof(*dbs); 1470 missing--; 1471 count++; 1472 } 1473 if (--wd->sc_bslist_inuse == 0) 1474 cv_broadcast(&wd->sc_bslist_cv); 1475 mutex_exit(&wd->sc_lock); 1476 dbsi.dbsi_left = missing; 1477 dbsi.dbsi_copied = count; 1478 *(struct disk_badsecinfo *)addr = dbsi; 1479 1480 /* 1481 * If we copied anything out, ignore error and return 1482 * success -- can't back it out. 1483 */ 1484 return count ? 0 : error; 1485 } 1486 1487 case DIOCBSFLUSH: { 1488 int error; 1489 1490 /* Clean out the bad sector list */ 1491 mutex_enter(&wd->sc_lock); 1492 while (wd->sc_bslist_inuse) { 1493 error = cv_wait_sig(&wd->sc_bslist_cv, &wd->sc_lock); 1494 if (error) { 1495 mutex_exit(&wd->sc_lock); 1496 return error; 1497 } 1498 } 1499 while (!SLIST_EMPTY(&wd->sc_bslist)) { 1500 struct disk_badsectors *dbs = 1501 SLIST_FIRST(&wd->sc_bslist); 1502 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next); 1503 mutex_exit(&wd->sc_lock); 1504 kmem_free(dbs, sizeof(*dbs)); 1505 mutex_enter(&wd->sc_lock); 1506 } 1507 mutex_exit(&wd->sc_lock); 1508 wd->sc_bscount = 0; 1509 return 0; 1510 } 1511 #endif 1512 1513 #ifdef notyet 1514 case DIOCWFORMAT: 1515 if ((flag & FWRITE) == 0) 1516 return EBADF; 1517 { 1518 register struct format_op *fop; 1519 struct iovec aiov; 1520 struct uio auio; 1521 int error1; 1522 1523 fop = (struct format_op *)addr; 1524 aiov.iov_base = fop->df_buf; 1525 aiov.iov_len = fop->df_count; 1526 auio.uio_iov = &aiov; 1527 auio.uio_iovcnt = 1; 1528 auio.uio_resid = fop->df_count; 1529 auio.uio_offset = 1530 fop->df_startblk * wd->sc_dk.dk_label->d_secsize; 1531 auio.uio_vmspace = l->l_proc->p_vmspace; 1532 error1 = physio(wdformat, NULL, dev, B_WRITE, wdminphys, 1533 &auio); 1534 fop->df_count -= auio.uio_resid; 1535 fop->df_reg[0] = wdc->sc_status; 1536 fop->df_reg[1] = wdc->sc_error; 1537 return error1; 1538 } 1539 #endif 1540 case DIOCGCACHE: 1541 return wd_getcache(wd, (int *)addr); 1542 1543 case DIOCSCACHE: 1544 return wd_setcache(wd, *(int *)addr); 1545 1546 case DIOCCACHESYNC: 1547 return wd_flushcache(wd, AT_WAIT); 1548 1549 case ATAIOCCOMMAND: 1550 /* 1551 * Make sure this command is (relatively) safe first 1552 */ 1553 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 && 1554 (flag & FWRITE) == 0) 1555 return (EBADF); 1556 { 1557 struct wd_ioctl *wi; 1558 atareq_t *atareq = (atareq_t *) addr; 1559 int error1; 1560 1561 wi = wi_get(wd); 1562 wi->wi_atareq = *atareq; 1563 1564 if (atareq->datalen && atareq->flags & 1565 (ATACMD_READ | ATACMD_WRITE)) { 1566 void *tbuf; 1567 if (atareq->datalen < DEV_BSIZE 1568 && atareq->command == WDCC_IDENTIFY) { 1569 tbuf = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 1570 wi->wi_iov.iov_base = tbuf; 1571 wi->wi_iov.iov_len = DEV_BSIZE; 1572 UIO_SETUP_SYSSPACE(&wi->wi_uio); 1573 } else { 1574 tbuf = NULL; 1575 wi->wi_iov.iov_base = atareq->databuf; 1576 wi->wi_iov.iov_len = atareq->datalen; 1577 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace; 1578 } 1579 wi->wi_uio.uio_iov = &wi->wi_iov; 1580 wi->wi_uio.uio_iovcnt = 1; 1581 wi->wi_uio.uio_resid = atareq->datalen; 1582 wi->wi_uio.uio_offset = 0; 1583 wi->wi_uio.uio_rw = 1584 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE; 1585 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev, 1586 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE, 1587 wdminphys, &wi->wi_uio); 1588 if (tbuf != NULL && error1 == 0) { 1589 error1 = copyout(tbuf, atareq->databuf, 1590 atareq->datalen); 1591 kmem_free(tbuf, DEV_BSIZE); 1592 } 1593 } else { 1594 /* No need to call physio if we don't have any 1595 user data */ 1596 wi->wi_bp.b_flags = 0; 1597 wi->wi_bp.b_data = 0; 1598 wi->wi_bp.b_bcount = 0; 1599 wi->wi_bp.b_dev = dev; 1600 wi->wi_bp.b_proc = l->l_proc; 1601 wdioctlstrategy(&wi->wi_bp); 1602 error1 = wi->wi_bp.b_error; 1603 } 1604 *atareq = wi->wi_atareq; 1605 wi_free(wi); 1606 return(error1); 1607 } 1608 1609 default: 1610 return dk_ioctl(dksc, dev, cmd, addr, flag, l); 1611 } 1612 1613 #ifdef DIAGNOSTIC 1614 panic("wdioctl: impossible"); 1615 #endif 1616 } 1617 1618 static int 1619 wd_discard(device_t dev, off_t pos, off_t len) 1620 { 1621 struct wd_softc *wd = device_private(dev); 1622 daddr_t bno; 1623 long size, done; 1624 long maxatonce, amount; 1625 int result; 1626 1627 if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7) 1628 || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) { 1629 /* not supported; ignore request */ 1630 ATADEBUG_PRINT(("wddiscard (unsupported)\n"), DEBUG_FUNCS); 1631 return 0; 1632 } 1633 maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/ 1634 1635 ATADEBUG_PRINT(("wddiscard\n"), DEBUG_FUNCS); 1636 1637 if ((wd->sc_flags & WDF_LOADED) == 0) 1638 return EIO; 1639 1640 /* round the start up and the end down */ 1641 bno = (pos + wd->sc_blksize - 1) / wd->sc_blksize; 1642 size = ((pos + len) / wd->sc_blksize) - bno; 1643 1644 done = 0; 1645 while (done < size) { 1646 amount = size - done; 1647 if (amount > maxatonce) { 1648 amount = maxatonce; 1649 } 1650 result = wd_trim(wd, bno + done, amount); 1651 if (result) { 1652 return result; 1653 } 1654 done += amount; 1655 } 1656 return 0; 1657 } 1658 1659 static int 1660 wddiscard(dev_t dev, off_t pos, off_t len) 1661 { 1662 struct wd_softc *wd; 1663 struct dk_softc *dksc; 1664 int unit; 1665 1666 unit = WDUNIT(dev); 1667 wd = device_lookup_private(&wd_cd, unit); 1668 dksc = &wd->sc_dksc; 1669 1670 return dk_discard(dksc, dev, pos, len); 1671 } 1672 1673 #ifdef B_FORMAT 1674 int 1675 wdformat(struct buf *bp) 1676 { 1677 1678 bp->b_flags |= B_FORMAT; 1679 return wdstrategy(bp); 1680 } 1681 #endif 1682 1683 int 1684 wdsize(dev_t dev) 1685 { 1686 struct wd_softc *wd; 1687 struct dk_softc *dksc; 1688 int unit; 1689 1690 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS); 1691 1692 unit = WDUNIT(dev); 1693 wd = device_lookup_private(&wd_cd, unit); 1694 if (wd == NULL) 1695 return (-1); 1696 dksc = &wd->sc_dksc; 1697 1698 if (!device_is_active(dksc->sc_dev)) 1699 return (-1); 1700 1701 return dk_size(dksc, dev); 1702 } 1703 1704 /* 1705 * Dump core after a system crash. 1706 */ 1707 static int 1708 wddump(dev_t dev, daddr_t blkno, void *va, size_t size) 1709 { 1710 struct wd_softc *wd; 1711 struct dk_softc *dksc; 1712 int unit; 1713 1714 /* Check if recursive dump; if so, punt. */ 1715 if (wddoingadump) 1716 return EFAULT; 1717 wddoingadump = 1; 1718 1719 unit = WDUNIT(dev); 1720 wd = device_lookup_private(&wd_cd, unit); 1721 if (wd == NULL) 1722 return (ENXIO); 1723 dksc = &wd->sc_dksc; 1724 1725 return dk_dump(dksc, dev, blkno, va, size, 0); 1726 } 1727 1728 static int 1729 wd_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk) 1730 { 1731 struct wd_softc *wd = device_private(dev); 1732 struct dk_softc *dksc = &wd->sc_dksc; 1733 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom; 1734 struct ata_xfer *xfer = &wd->dump_xfer; 1735 int err; 1736 1737 /* Recalibrate, if first dump transfer. */ 1738 if (wddumprecalibrated == 0) { 1739 wddumprecalibrated = 1; 1740 ata_channel_lock(wd->drvp->chnl_softc); 1741 /* This will directly execute the reset due to AT_POLL */ 1742 ata_thread_run(wd->drvp->chnl_softc, AT_POLL, 1743 ATACH_TH_DRIVE_RESET, wd->drvp->drive); 1744 1745 wd->drvp->state = RESET; 1746 ata_channel_unlock(wd->drvp->chnl_softc); 1747 } 1748 1749 memset(xfer, 0, sizeof(*xfer)); 1750 xfer->c_flags |= C_PRIVATE_ALLOC | C_SKIP_QUEUE; 1751 1752 xfer->c_bio.blkno = blkno; 1753 xfer->c_bio.flags = ATA_POLL; 1754 if (wd->sc_flags & WDF_LBA48 && 1755 (xfer->c_bio.blkno + nblk) > wd->sc_capacity28) 1756 xfer->c_bio.flags |= ATA_LBA48; 1757 if (wd->sc_flags & WDF_LBA) 1758 xfer->c_bio.flags |= ATA_LBA; 1759 xfer->c_bio.bcount = nblk * dg->dg_secsize; 1760 xfer->c_bio.databuf = va; 1761 #ifndef WD_DUMP_NOT_TRUSTED 1762 /* This will poll until the bio is complete */ 1763 wd->atabus->ata_bio(wd->drvp, xfer); 1764 1765 switch(err = xfer->c_bio.error) { 1766 case TIMEOUT: 1767 printf("wddump: device timed out"); 1768 err = EIO; 1769 break; 1770 case ERR_DF: 1771 printf("wddump: drive fault"); 1772 err = EIO; 1773 break; 1774 case ERR_DMA: 1775 printf("wddump: DMA error"); 1776 err = EIO; 1777 break; 1778 case ERROR: 1779 printf("wddump: "); 1780 wdperror(wd, xfer); 1781 err = EIO; 1782 break; 1783 case NOERROR: 1784 err = 0; 1785 break; 1786 default: 1787 panic("wddump: unknown error type %x", err); 1788 } 1789 1790 if (err != 0) { 1791 printf("\n"); 1792 return err; 1793 } 1794 #else /* WD_DUMP_NOT_TRUSTED */ 1795 /* Let's just talk about this first... */ 1796 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n", 1797 unit, va, cylin, head, sector); 1798 delay(500 * 1000); /* half a second */ 1799 #endif 1800 1801 wddoingadump = 0; 1802 return 0; 1803 } 1804 1805 static void 1806 wd_set_geometry(struct wd_softc *wd) 1807 { 1808 struct dk_softc *dksc = &wd->sc_dksc; 1809 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom; 1810 1811 memset(dg, 0, sizeof(*dg)); 1812 1813 dg->dg_secperunit = wd->sc_capacity; 1814 dg->dg_secsize = wd->sc_blksize; 1815 dg->dg_nsectors = wd->sc_params.atap_sectors; 1816 dg->dg_ntracks = wd->sc_params.atap_heads; 1817 if ((wd->sc_flags & WDF_LBA) == 0) 1818 dg->dg_ncylinders = wd->sc_params.atap_cylinders; 1819 if ((wd->sc_params.atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID 1820 && ((wd->sc_params.atap_secsz & ATA_SECSZ_LPS) != 0)) { 1821 dg->dg_physsecsize = wd->sc_blksize << (wd->sc_params.atap_secsz & 1822 ATA_SECSZ_LPS_SZMSK); 1823 if ((wd->sc_params.atap_logical_align & ATA_LA_VALID_MASK) == 1824 ATA_LA_VALID) { 1825 dg->dg_alignedsec = (wd->sc_params.atap_logical_align & 1826 ATA_LA_MASK) & ((1u << (wd->sc_params.atap_secsz & 1827 ATA_SECSZ_LPS_SZMSK)) - 1); 1828 } 1829 } 1830 1831 disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, wd->sc_typename); 1832 } 1833 1834 int 1835 wd_get_params(struct wd_softc *wd, struct ataparams *params) 1836 { 1837 int retry = 0; 1838 struct ata_channel *chp = wd->drvp->chnl_softc; 1839 const int flags = AT_WAIT; 1840 1841 again: 1842 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) { 1843 case CMD_AGAIN: 1844 return 1; 1845 case CMD_ERR: 1846 if (retry == 0) { 1847 retry++; 1848 ata_channel_lock(chp); 1849 (*wd->atabus->ata_reset_drive)(wd->drvp, flags, NULL); 1850 ata_channel_unlock(chp); 1851 goto again; 1852 } 1853 1854 if (wd->drvp->drive_type != ATA_DRIVET_OLD) 1855 return 1; 1856 /* 1857 * We `know' there's a drive here; just assume it's old. 1858 * This geometry is only used to read the MBR and print a 1859 * (false) attach message. 1860 */ 1861 strncpy(params->atap_model, "ST506", 1862 sizeof params->atap_model); 1863 params->atap_config = ATA_CFG_FIXED; 1864 params->atap_cylinders = 1024; 1865 params->atap_heads = 8; 1866 params->atap_sectors = 17; 1867 params->atap_multi = 1; 1868 params->atap_capabilities1 = params->atap_capabilities2 = 0; 1869 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */ 1870 /* FALLTHROUGH */ 1871 case CMD_OK: 1872 return 0; 1873 default: 1874 panic("wd_get_params: bad return code from ata_get_params"); 1875 /* NOTREACHED */ 1876 } 1877 } 1878 1879 int 1880 wd_getcache(struct wd_softc *wd, int *bitsp) 1881 { 1882 struct ataparams params; 1883 1884 if (wd_get_params(wd, ¶ms) != 0) 1885 return EIO; 1886 if (params.atap_cmd_set1 == 0x0000 || 1887 params.atap_cmd_set1 == 0xffff || 1888 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) { 1889 *bitsp = 0; 1890 return 0; 1891 } 1892 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ; 1893 if (params.atap_cmd1_en & WDC_CMD1_CACHE) 1894 *bitsp |= DKCACHE_WRITE; 1895 1896 if (WD_USE_NCQ(wd) || (wd->drvp->drive_flags & ATA_DRIVE_WFUA)) 1897 *bitsp |= DKCACHE_FUA; 1898 1899 return 0; 1900 } 1901 1902 1903 static int 1904 wd_check_error(const struct dk_softc *dksc, const struct ata_xfer *xfer, 1905 const char *func) 1906 { 1907 static const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF"; 1908 1909 int flags = xfer->c_ata_c.flags; 1910 1911 if ((flags & AT_ERROR) != 0 && xfer->c_ata_c.r_error == WDCE_ABRT) { 1912 /* command not supported */ 1913 aprint_debug_dev(dksc->sc_dev, "%s: not supported\n", func); 1914 return ENODEV; 1915 } 1916 if (flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 1917 char sbuf[sizeof(at_errbits) + 64]; 1918 snprintb(sbuf, sizeof(sbuf), at_errbits, flags); 1919 device_printf(dksc->sc_dev, "%s: status=%s\n", func, sbuf); 1920 return EIO; 1921 } 1922 return 0; 1923 } 1924 1925 int 1926 wd_setcache(struct wd_softc *wd, int bits) 1927 { 1928 struct dk_softc *dksc = &wd->sc_dksc; 1929 struct ataparams params; 1930 struct ata_xfer *xfer; 1931 int error; 1932 1933 if (wd_get_params(wd, ¶ms) != 0) 1934 return EIO; 1935 1936 if (params.atap_cmd_set1 == 0x0000 || 1937 params.atap_cmd_set1 == 0xffff || 1938 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) 1939 return EOPNOTSUPP; 1940 1941 if ((bits & DKCACHE_READ) == 0 || 1942 (bits & DKCACHE_SAVE) != 0) 1943 return EOPNOTSUPP; 1944 1945 xfer = ata_get_xfer(wd->drvp->chnl_softc, true); 1946 1947 xfer->c_ata_c.r_command = SET_FEATURES; 1948 xfer->c_ata_c.r_st_bmask = 0; 1949 xfer->c_ata_c.r_st_pmask = 0; 1950 xfer->c_ata_c.timeout = 30000; /* 30s timeout */ 1951 xfer->c_ata_c.flags = AT_WAIT; 1952 if (bits & DKCACHE_WRITE) 1953 xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_EN; 1954 else 1955 xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_DS; 1956 1957 wd->atabus->ata_exec_command(wd->drvp, xfer); 1958 ata_wait_cmd(wd->drvp->chnl_softc, xfer); 1959 1960 error = wd_check_error(dksc, xfer, __func__); 1961 ata_free_xfer(wd->drvp->chnl_softc, xfer); 1962 return error; 1963 } 1964 1965 static int 1966 wd_standby(struct wd_softc *wd, int flags) 1967 { 1968 struct dk_softc *dksc = &wd->sc_dksc; 1969 struct ata_xfer *xfer; 1970 int error; 1971 1972 aprint_debug_dev(dksc->sc_dev, "standby immediate\n"); 1973 xfer = ata_get_xfer(wd->drvp->chnl_softc, true); 1974 1975 xfer->c_ata_c.r_command = WDCC_STANDBY_IMMED; 1976 xfer->c_ata_c.r_st_bmask = WDCS_DRDY; 1977 xfer->c_ata_c.r_st_pmask = WDCS_DRDY; 1978 xfer->c_ata_c.flags = flags; 1979 xfer->c_ata_c.timeout = 30000; /* 30s timeout */ 1980 1981 wd->atabus->ata_exec_command(wd->drvp, xfer); 1982 ata_wait_cmd(wd->drvp->chnl_softc, xfer); 1983 1984 error = wd_check_error(dksc, xfer, __func__); 1985 ata_free_xfer(wd->drvp->chnl_softc, xfer); 1986 return error; 1987 } 1988 1989 int 1990 wd_flushcache(struct wd_softc *wd, int flags) 1991 { 1992 struct dk_softc *dksc = &wd->sc_dksc; 1993 struct ata_xfer *xfer; 1994 int error; 1995 1996 /* 1997 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report 1998 * only ATA-2 and still support it. 1999 */ 2000 if (wd->drvp->ata_vers < 4 && 2001 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 || 2002 wd->sc_params.atap_cmd_set2 == 0xffff)) 2003 return ENODEV; 2004 2005 xfer = ata_get_xfer(wd->drvp->chnl_softc, true); 2006 2007 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 && 2008 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) { 2009 xfer->c_ata_c.r_command = WDCC_FLUSHCACHE_EXT; 2010 flags |= AT_LBA48; 2011 } else 2012 xfer->c_ata_c.r_command = WDCC_FLUSHCACHE; 2013 xfer->c_ata_c.r_st_bmask = WDCS_DRDY; 2014 xfer->c_ata_c.r_st_pmask = WDCS_DRDY; 2015 xfer->c_ata_c.flags = flags | AT_READREG; 2016 xfer->c_ata_c.timeout = 300000; /* 5m timeout */ 2017 2018 wd->atabus->ata_exec_command(wd->drvp, xfer); 2019 ata_wait_cmd(wd->drvp->chnl_softc, xfer); 2020 2021 error = wd_check_error(dksc, xfer, __func__); 2022 wd->sc_flags &= ~WDF_DIRTY; 2023 ata_free_xfer(wd->drvp->chnl_softc, xfer); 2024 return error; 2025 } 2026 2027 /* 2028 * Execute TRIM command, assumes sleep context. 2029 */ 2030 static int 2031 wd_trim(struct wd_softc *wd, daddr_t bno, long size) 2032 { 2033 struct dk_softc *dksc = &wd->sc_dksc; 2034 struct ata_xfer *xfer; 2035 int error; 2036 unsigned char *req; 2037 2038 xfer = ata_get_xfer(wd->drvp->chnl_softc, true); 2039 2040 req = kmem_zalloc(512, KM_SLEEP); 2041 req[0] = bno & 0xff; 2042 req[1] = (bno >> 8) & 0xff; 2043 req[2] = (bno >> 16) & 0xff; 2044 req[3] = (bno >> 24) & 0xff; 2045 req[4] = (bno >> 32) & 0xff; 2046 req[5] = (bno >> 40) & 0xff; 2047 req[6] = size & 0xff; 2048 req[7] = (size >> 8) & 0xff; 2049 2050 /* 2051 * XXX We could possibly use NCQ TRIM, which supports executing 2052 * this command concurrently. It would need some investigation, some 2053 * early or not so early disk firmware caused data loss with NCQ TRIM. 2054 * atastart() et.al would need to be adjusted to allow and support 2055 * running several non-I/O ATA commands in parallel. 2056 */ 2057 2058 xfer->c_ata_c.r_command = ATA_DATA_SET_MANAGEMENT; 2059 xfer->c_ata_c.r_count = 1; 2060 xfer->c_ata_c.r_features = ATA_SUPPORT_DSM_TRIM; 2061 xfer->c_ata_c.r_st_bmask = WDCS_DRDY; 2062 xfer->c_ata_c.r_st_pmask = WDCS_DRDY; 2063 xfer->c_ata_c.timeout = 30000; /* 30s timeout */ 2064 xfer->c_ata_c.data = req; 2065 xfer->c_ata_c.bcount = 512; 2066 xfer->c_ata_c.flags |= AT_WRITE | AT_WAIT; 2067 2068 wd->atabus->ata_exec_command(wd->drvp, xfer); 2069 ata_wait_cmd(wd->drvp->chnl_softc, xfer); 2070 2071 kmem_free(req, 512); 2072 error = wd_check_error(dksc, xfer, __func__); 2073 ata_free_xfer(wd->drvp->chnl_softc, xfer); 2074 return error; 2075 } 2076 2077 bool 2078 wd_shutdown(device_t dev, int how) 2079 { 2080 struct wd_softc *wd = device_private(dev); 2081 2082 /* the adapter needs to be enabled */ 2083 if (wd->atabus->ata_addref(wd->drvp)) 2084 return true; /* no need to complain */ 2085 2086 wd_flushcache(wd, AT_POLL); 2087 if ((how & RB_POWERDOWN) == RB_POWERDOWN) 2088 wd_standby(wd, AT_POLL); 2089 return true; 2090 } 2091 2092 /* 2093 * Allocate space for a ioctl queue structure. Mostly taken from 2094 * scsipi_ioctl.c 2095 */ 2096 struct wd_ioctl * 2097 wi_get(struct wd_softc *wd) 2098 { 2099 struct wd_ioctl *wi; 2100 2101 wi = kmem_zalloc(sizeof(struct wd_ioctl), KM_SLEEP); 2102 wi->wi_softc = wd; 2103 buf_init(&wi->wi_bp); 2104 2105 return (wi); 2106 } 2107 2108 /* 2109 * Free an ioctl structure and remove it from our list 2110 */ 2111 2112 void 2113 wi_free(struct wd_ioctl *wi) 2114 { 2115 buf_destroy(&wi->wi_bp); 2116 kmem_free(wi, sizeof(*wi)); 2117 } 2118 2119 /* 2120 * Find a wd_ioctl structure based on the struct buf. 2121 */ 2122 2123 struct wd_ioctl * 2124 wi_find(struct buf *bp) 2125 { 2126 return container_of(bp, struct wd_ioctl, wi_bp); 2127 } 2128 2129 static uint 2130 wi_sector_size(const struct wd_ioctl * const wi) 2131 { 2132 switch (wi->wi_atareq.command) { 2133 case WDCC_READ: 2134 case WDCC_WRITE: 2135 case WDCC_READMULTI: 2136 case WDCC_WRITEMULTI: 2137 case WDCC_READDMA: 2138 case WDCC_WRITEDMA: 2139 case WDCC_READ_EXT: 2140 case WDCC_WRITE_EXT: 2141 case WDCC_READMULTI_EXT: 2142 case WDCC_WRITEMULTI_EXT: 2143 case WDCC_READDMA_EXT: 2144 case WDCC_WRITEDMA_EXT: 2145 case WDCC_READ_FPDMA_QUEUED: 2146 case WDCC_WRITE_FPDMA_QUEUED: 2147 return wi->wi_softc->sc_blksize; 2148 default: 2149 return 512; 2150 } 2151 } 2152 2153 /* 2154 * Ioctl pseudo strategy routine 2155 * 2156 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What 2157 * happens here is: 2158 * 2159 * - wdioctl() queues a wd_ioctl structure. 2160 * 2161 * - wdioctl() calls physio/wdioctlstrategy based on whether or not 2162 * user space I/O is required. If physio() is called, physio() eventually 2163 * calls wdioctlstrategy(). 2164 * 2165 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command() 2166 * to perform the actual command 2167 * 2168 * The reason for the use of the pseudo strategy routine is because 2169 * when doing I/O to/from user space, physio _really_ wants to be in 2170 * the loop. We could put the entire buffer into the ioctl request 2171 * structure, but that won't scale if we want to do things like download 2172 * microcode. 2173 */ 2174 2175 void 2176 wdioctlstrategy(struct buf *bp) 2177 { 2178 struct wd_ioctl *wi; 2179 struct ata_xfer *xfer; 2180 int error = 0; 2181 2182 wi = wi_find(bp); 2183 if (wi == NULL) { 2184 printf("wdioctlstrategy: " 2185 "No matching ioctl request found in queue\n"); 2186 error = EINVAL; 2187 goto out2; 2188 } 2189 2190 xfer = ata_get_xfer(wi->wi_softc->drvp->chnl_softc, true); 2191 2192 /* 2193 * Abort if physio broke up the transfer 2194 */ 2195 2196 if (bp->b_bcount != wi->wi_atareq.datalen) { 2197 printf("physio split wd ioctl request... cannot proceed\n"); 2198 error = EIO; 2199 goto out; 2200 } 2201 2202 /* 2203 * Abort if we didn't get a buffer size that was a multiple of 2204 * our sector size (or overflows CHS/LBA28 sector count) 2205 */ 2206 2207 if ((bp->b_bcount % wi_sector_size(wi)) != 0 || 2208 (bp->b_bcount / wi_sector_size(wi)) >= 2209 (1 << NBBY)) { 2210 error = EINVAL; 2211 goto out; 2212 } 2213 2214 /* 2215 * Make sure a timeout was supplied in the ioctl request 2216 */ 2217 2218 if (wi->wi_atareq.timeout == 0) { 2219 error = EINVAL; 2220 goto out; 2221 } 2222 2223 if (wi->wi_atareq.flags & ATACMD_READ) 2224 xfer->c_ata_c.flags |= AT_READ; 2225 else if (wi->wi_atareq.flags & ATACMD_WRITE) 2226 xfer->c_ata_c.flags |= AT_WRITE; 2227 2228 if (wi->wi_atareq.flags & ATACMD_READREG) 2229 xfer->c_ata_c.flags |= AT_READREG; 2230 2231 if ((wi->wi_atareq.flags & ATACMD_LBA) != 0) 2232 xfer->c_ata_c.flags |= AT_LBA; 2233 2234 xfer->c_ata_c.flags |= AT_WAIT; 2235 2236 xfer->c_ata_c.timeout = wi->wi_atareq.timeout; 2237 xfer->c_ata_c.r_command = wi->wi_atareq.command; 2238 xfer->c_ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) | 2239 (wi->wi_atareq.cylinder << 8) | 2240 wi->wi_atareq.sec_num; 2241 xfer->c_ata_c.r_count = wi->wi_atareq.sec_count; 2242 xfer->c_ata_c.r_features = wi->wi_atareq.features; 2243 xfer->c_ata_c.r_st_bmask = WDCS_DRDY; 2244 xfer->c_ata_c.r_st_pmask = WDCS_DRDY; 2245 xfer->c_ata_c.data = wi->wi_bp.b_data; 2246 xfer->c_ata_c.bcount = wi->wi_bp.b_bcount; 2247 2248 wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, xfer); 2249 ata_wait_cmd(wi->wi_softc->drvp->chnl_softc, xfer); 2250 2251 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 2252 if (xfer->c_ata_c.flags & AT_ERROR) { 2253 wi->wi_atareq.retsts = ATACMD_ERROR; 2254 wi->wi_atareq.error = xfer->c_ata_c.r_error; 2255 } else if (xfer->c_ata_c.flags & AT_DF) 2256 wi->wi_atareq.retsts = ATACMD_DF; 2257 else 2258 wi->wi_atareq.retsts = ATACMD_TIMEOUT; 2259 } else { 2260 wi->wi_atareq.retsts = ATACMD_OK; 2261 if (wi->wi_atareq.flags & ATACMD_READREG) { 2262 wi->wi_atareq.command = xfer->c_ata_c.r_status; 2263 wi->wi_atareq.features = xfer->c_ata_c.r_error; 2264 wi->wi_atareq.sec_count = xfer->c_ata_c.r_count; 2265 wi->wi_atareq.sec_num = xfer->c_ata_c.r_lba & 0xff; 2266 wi->wi_atareq.head = (xfer->c_ata_c.r_device & 0xf0) | 2267 ((xfer->c_ata_c.r_lba >> 24) & 0x0f); 2268 wi->wi_atareq.cylinder = 2269 (xfer->c_ata_c.r_lba >> 8) & 0xffff; 2270 wi->wi_atareq.error = xfer->c_ata_c.r_error; 2271 } 2272 } 2273 2274 out: 2275 ata_free_xfer(wi->wi_softc->drvp->chnl_softc, xfer); 2276 out2: 2277 bp->b_error = error; 2278 if (error) 2279 bp->b_resid = bp->b_bcount; 2280 biodone(bp); 2281 } 2282 2283 static void 2284 wd_sysctl_attach(struct wd_softc *wd) 2285 { 2286 struct dk_softc *dksc = &wd->sc_dksc; 2287 const struct sysctlnode *node; 2288 int error; 2289 2290 /* sysctl set-up */ 2291 if (sysctl_createv(&wd->nodelog, 0, NULL, &node, 2292 0, CTLTYPE_NODE, dksc->sc_xname, 2293 SYSCTL_DESCR("wd driver settings"), 2294 NULL, 0, NULL, 0, 2295 CTL_HW, CTL_CREATE, CTL_EOL) != 0) { 2296 aprint_error_dev(dksc->sc_dev, 2297 "could not create %s.%s sysctl node\n", 2298 "hw", dksc->sc_xname); 2299 return; 2300 } 2301 2302 wd->drv_ncq = true; 2303 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL, 2304 CTLFLAG_READWRITE, CTLTYPE_BOOL, "use_ncq", 2305 SYSCTL_DESCR("use NCQ if supported"), 2306 NULL, 0, &wd->drv_ncq, 0, 2307 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL)) 2308 != 0) { 2309 aprint_error_dev(dksc->sc_dev, 2310 "could not create %s.%s.use_ncq sysctl - error %d\n", 2311 "hw", dksc->sc_xname, error); 2312 return; 2313 } 2314 2315 wd->drv_ncq_prio = false; 2316 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL, 2317 CTLFLAG_READWRITE, CTLTYPE_BOOL, "use_ncq_prio", 2318 SYSCTL_DESCR("use NCQ PRIORITY if supported"), 2319 NULL, 0, &wd->drv_ncq_prio, 0, 2320 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL)) 2321 != 0) { 2322 aprint_error_dev(dksc->sc_dev, 2323 "could not create %s.%s.use_ncq_prio sysctl - error %d\n", 2324 "hw", dksc->sc_xname, error); 2325 return; 2326 } 2327 2328 #ifdef WD_CHAOS_MONKEY 2329 wd->drv_chaos_freq = 0; 2330 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL, 2331 CTLFLAG_READWRITE, CTLTYPE_INT, "chaos_freq", 2332 SYSCTL_DESCR("simulated bio read error rate"), 2333 NULL, 0, &wd->drv_chaos_freq, 0, 2334 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL)) 2335 != 0) { 2336 aprint_error_dev(dksc->sc_dev, 2337 "could not create %s.%s.chaos_freq sysctl - error %d\n", 2338 "hw", dksc->sc_xname, error); 2339 return; 2340 } 2341 2342 wd->drv_chaos_cnt = 0; 2343 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL, 2344 CTLFLAG_READONLY, CTLTYPE_INT, "chaos_cnt", 2345 SYSCTL_DESCR("number of processed bio reads"), 2346 NULL, 0, &wd->drv_chaos_cnt, 0, 2347 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL)) 2348 != 0) { 2349 aprint_error_dev(dksc->sc_dev, 2350 "could not create %s.%s.chaos_cnt sysctl - error %d\n", 2351 "hw", dksc->sc_xname, error); 2352 return; 2353 } 2354 #endif 2355 2356 } 2357 2358 static void 2359 wd_sysctl_detach(struct wd_softc *wd) 2360 { 2361 sysctl_teardown(&wd->nodelog); 2362 } 2363 2364 #ifdef ATADEBUG 2365 int wddebug(void); 2366 2367 int 2368 wddebug(void) 2369 { 2370 struct wd_softc *wd; 2371 struct dk_softc *dksc; 2372 int unit; 2373 2374 for (unit = 0; unit <= 3; unit++) { 2375 wd = device_lookup_private(&wd_cd, unit); 2376 if (wd == NULL) 2377 continue; 2378 dksc = &wd->sc_dksc; 2379 printf("%s fl %x bufq %p:\n", 2380 dksc->sc_xname, wd->sc_flags, bufq_peek(dksc->sc_bufq)); 2381 2382 atachannel_debug(wd->drvp->chnl_softc); 2383 } 2384 return 0; 2385 } 2386 #endif /* ATADEBUG */ 2387