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