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