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