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