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