1 /* $NetBSD: ld.c,v 1.61 2008/08/01 16:09:45 ws Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran and Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Disk driver for use by RAID controllers. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.61 2008/08/01 16:09:45 ws Exp $"); 38 39 #include "rnd.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/device.h> 45 #include <sys/queue.h> 46 #include <sys/proc.h> 47 #include <sys/buf.h> 48 #include <sys/bufq.h> 49 #include <sys/endian.h> 50 #include <sys/disklabel.h> 51 #include <sys/disk.h> 52 #include <sys/dkio.h> 53 #include <sys/stat.h> 54 #include <sys/conf.h> 55 #include <sys/fcntl.h> 56 #include <sys/vnode.h> 57 #include <sys/syslog.h> 58 #include <sys/mutex.h> 59 #if NRND > 0 60 #include <sys/rnd.h> 61 #endif 62 63 #include <dev/ldvar.h> 64 65 #include <prop/proplib.h> 66 67 static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *); 68 static void ldgetdisklabel(struct ld_softc *); 69 static void ldminphys(struct buf *bp); 70 static bool ld_shutdown(device_t, int); 71 static void ldstart(struct ld_softc *, struct buf *); 72 static void ld_set_properties(struct ld_softc *); 73 static void ld_config_interrupts (struct device *); 74 75 extern struct cfdriver ld_cd; 76 77 static dev_type_open(ldopen); 78 static dev_type_close(ldclose); 79 static dev_type_read(ldread); 80 static dev_type_write(ldwrite); 81 static dev_type_ioctl(ldioctl); 82 static dev_type_strategy(ldstrategy); 83 static dev_type_dump(lddump); 84 static dev_type_size(ldsize); 85 86 const struct bdevsw ld_bdevsw = { 87 ldopen, ldclose, ldstrategy, ldioctl, lddump, ldsize, D_DISK 88 }; 89 90 const struct cdevsw ld_cdevsw = { 91 ldopen, ldclose, ldread, ldwrite, ldioctl, 92 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 93 }; 94 95 static struct dkdriver lddkdriver = { ldstrategy, ldminphys }; 96 97 void 98 ldattach(struct ld_softc *sc) 99 { 100 char tbuf[9]; 101 102 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM); 103 104 if ((sc->sc_flags & LDF_ENABLED) == 0) { 105 aprint_normal_dev(&sc->sc_dv, "disabled\n"); 106 return; 107 } 108 109 /* Initialise and attach the disk structure. */ 110 disk_init(&sc->sc_dk, device_xname(&sc->sc_dv), &lddkdriver); 111 disk_attach(&sc->sc_dk); 112 113 if (sc->sc_maxxfer > MAXPHYS) 114 sc->sc_maxxfer = MAXPHYS; 115 116 /* Build synthetic geometry if necessary. */ 117 if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 || 118 sc->sc_ncylinders == 0) { 119 uint64_t ncyl; 120 121 if (sc->sc_secperunit <= 528 * 2048) /* 528MB */ 122 sc->sc_nheads = 16; 123 else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */ 124 sc->sc_nheads = 32; 125 else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */ 126 sc->sc_nheads = 64; 127 else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */ 128 sc->sc_nheads = 128; 129 else 130 sc->sc_nheads = 255; 131 132 sc->sc_nsectors = 63; 133 sc->sc_ncylinders = INT_MAX; 134 ncyl = sc->sc_secperunit / 135 (sc->sc_nheads * sc->sc_nsectors); 136 if (ncyl < INT_MAX) 137 sc->sc_ncylinders = (int)ncyl; 138 } 139 140 format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit * 141 sc->sc_secsize); 142 aprint_normal_dev(&sc->sc_dv, "%s, %d cyl, %d head, %d sec, %d bytes/sect x %"PRIu64" sectors\n", 143 tbuf, sc->sc_ncylinders, sc->sc_nheads, 144 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit); 145 146 ld_set_properties(sc); 147 148 #if NRND > 0 149 /* Attach the device into the rnd source list. */ 150 rnd_attach_source(&sc->sc_rnd_source, device_xname(&sc->sc_dv), 151 RND_TYPE_DISK, 0); 152 #endif 153 154 /* Register with PMF */ 155 if (!pmf_device_register1(&sc->sc_dv, NULL, NULL, ld_shutdown)) 156 aprint_error_dev(&sc->sc_dv, 157 "couldn't establish power handler\n"); 158 159 bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK); 160 161 /* Discover wedges on this disk. */ 162 config_interrupts(&sc->sc_dv, ld_config_interrupts); 163 } 164 165 int 166 ldadjqparam(struct ld_softc *sc, int xmax) 167 { 168 int s; 169 170 s = splbio(); 171 sc->sc_maxqueuecnt = xmax; 172 splx(s); 173 174 return (0); 175 } 176 177 int 178 ldbegindetach(struct ld_softc *sc, int flags) 179 { 180 int s, rv = 0; 181 182 if ((sc->sc_flags & LDF_ENABLED) == 0) 183 return (0); 184 185 if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0) 186 return (EBUSY); 187 188 s = splbio(); 189 sc->sc_maxqueuecnt = 0; 190 sc->sc_flags |= LDF_DETACH; 191 while (sc->sc_queuecnt > 0) { 192 sc->sc_flags |= LDF_DRAIN; 193 rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0); 194 if (rv) 195 break; 196 } 197 splx(s); 198 199 return (rv); 200 } 201 202 void 203 ldenddetach(struct ld_softc *sc) 204 { 205 int s, bmaj, cmaj, i, mn; 206 207 if ((sc->sc_flags & LDF_ENABLED) == 0) 208 return; 209 210 /* Wait for commands queued with the hardware to complete. */ 211 if (sc->sc_queuecnt != 0) 212 if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz)) 213 printf("%s: not drained\n", device_xname(&sc->sc_dv)); 214 215 /* Locate the major numbers. */ 216 bmaj = bdevsw_lookup_major(&ld_bdevsw); 217 cmaj = cdevsw_lookup_major(&ld_cdevsw); 218 219 /* Kill off any queued buffers. */ 220 s = splbio(); 221 bufq_drain(sc->sc_bufq); 222 splx(s); 223 224 bufq_free(sc->sc_bufq); 225 226 /* Nuke the vnodes for any open instances. */ 227 for (i = 0; i < MAXPARTITIONS; i++) { 228 mn = DISKMINOR(device_unit(&sc->sc_dv), i); 229 vdevgone(bmaj, mn, mn, VBLK); 230 vdevgone(cmaj, mn, mn, VCHR); 231 } 232 233 /* Delete all of our wedges. */ 234 dkwedge_delall(&sc->sc_dk); 235 236 /* Detach from the disk list. */ 237 disk_detach(&sc->sc_dk); 238 disk_destroy(&sc->sc_dk); 239 240 #if NRND > 0 241 /* Unhook the entropy source. */ 242 rnd_detach_source(&sc->sc_rnd_source); 243 #endif 244 245 /* Deregister with PMF */ 246 pmf_device_deregister(&sc->sc_dv); 247 248 /* 249 * XXX We can't really flush the cache here, beceause the 250 * XXX device may already be non-existent from the controller's 251 * XXX perspective. 252 */ 253 #if 0 254 /* Flush the device's cache. */ 255 if (sc->sc_flush != NULL) 256 if ((*sc->sc_flush)(sc) != 0) 257 aprint_error_dev(&sc->sc_dv, "unable to flush cache\n"); 258 #endif 259 mutex_destroy(&sc->sc_mutex); 260 } 261 262 /* ARGSUSED */ 263 static bool 264 ld_shutdown(device_t dev, int flags) 265 { 266 struct ld_softc *sc = device_private(dev); 267 268 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0) { 269 printf("%s: unable to flush cache\n", device_xname(dev)); 270 return false; 271 } 272 273 return true; 274 } 275 276 /* ARGSUSED */ 277 static int 278 ldopen(dev_t dev, int flags, int fmt, struct lwp *l) 279 { 280 struct ld_softc *sc; 281 int error, unit, part; 282 283 unit = DISKUNIT(dev); 284 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL) 285 return (ENXIO); 286 if ((sc->sc_flags & LDF_ENABLED) == 0) 287 return (ENODEV); 288 part = DISKPART(dev); 289 290 mutex_enter(&sc->sc_dk.dk_openlock); 291 292 if (sc->sc_dk.dk_openmask == 0) { 293 /* Load the partition info if not already loaded. */ 294 if ((sc->sc_flags & LDF_VLABEL) == 0) 295 ldgetdisklabel(sc); 296 } 297 298 /* Check that the partition exists. */ 299 if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions || 300 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 301 error = ENXIO; 302 goto bad1; 303 } 304 305 /* Ensure only one open at a time. */ 306 switch (fmt) { 307 case S_IFCHR: 308 sc->sc_dk.dk_copenmask |= (1 << part); 309 break; 310 case S_IFBLK: 311 sc->sc_dk.dk_bopenmask |= (1 << part); 312 break; 313 } 314 sc->sc_dk.dk_openmask = 315 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 316 317 error = 0; 318 bad1: 319 mutex_exit(&sc->sc_dk.dk_openlock); 320 return (error); 321 } 322 323 /* ARGSUSED */ 324 static int 325 ldclose(dev_t dev, int flags, int fmt, struct lwp *l) 326 { 327 struct ld_softc *sc; 328 int part, unit; 329 330 unit = DISKUNIT(dev); 331 part = DISKPART(dev); 332 sc = device_lookup_private(&ld_cd, unit); 333 334 mutex_enter(&sc->sc_dk.dk_openlock); 335 336 switch (fmt) { 337 case S_IFCHR: 338 sc->sc_dk.dk_copenmask &= ~(1 << part); 339 break; 340 case S_IFBLK: 341 sc->sc_dk.dk_bopenmask &= ~(1 << part); 342 break; 343 } 344 sc->sc_dk.dk_openmask = 345 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 346 347 if (sc->sc_dk.dk_openmask == 0) { 348 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0) 349 aprint_error_dev(&sc->sc_dv, "unable to flush cache\n"); 350 if ((sc->sc_flags & LDF_KLABEL) == 0) 351 sc->sc_flags &= ~LDF_VLABEL; 352 } 353 354 mutex_exit(&sc->sc_dk.dk_openlock); 355 return (0); 356 } 357 358 /* ARGSUSED */ 359 static int 360 ldread(dev_t dev, struct uio *uio, int ioflag) 361 { 362 363 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio)); 364 } 365 366 /* ARGSUSED */ 367 static int 368 ldwrite(dev_t dev, struct uio *uio, int ioflag) 369 { 370 371 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio)); 372 } 373 374 /* ARGSUSED */ 375 static int 376 ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l) 377 { 378 struct ld_softc *sc; 379 int part, unit, error; 380 #ifdef __HAVE_OLD_DISKLABEL 381 struct disklabel newlabel; 382 #endif 383 struct disklabel *lp; 384 385 unit = DISKUNIT(dev); 386 part = DISKPART(dev); 387 sc = device_lookup_private(&ld_cd, unit); 388 389 error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l); 390 if (error != EPASSTHROUGH) 391 return (error); 392 393 error = 0; 394 switch (cmd) { 395 case DIOCGDINFO: 396 memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel)); 397 return (0); 398 399 #ifdef __HAVE_OLD_DISKLABEL 400 case ODIOCGDINFO: 401 newlabel = *(sc->sc_dk.dk_label); 402 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 403 return ENOTTY; 404 memcpy(addr, &newlabel, sizeof(struct olddisklabel)); 405 return (0); 406 #endif 407 408 case DIOCGPART: 409 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label; 410 ((struct partinfo *)addr)->part = 411 &sc->sc_dk.dk_label->d_partitions[part]; 412 break; 413 414 case DIOCWDINFO: 415 case DIOCSDINFO: 416 #ifdef __HAVE_OLD_DISKLABEL 417 case ODIOCWDINFO: 418 case ODIOCSDINFO: 419 420 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 421 memset(&newlabel, 0, sizeof newlabel); 422 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 423 lp = &newlabel; 424 } else 425 #endif 426 lp = (struct disklabel *)addr; 427 428 if ((flag & FWRITE) == 0) 429 return (EBADF); 430 431 mutex_enter(&sc->sc_dk.dk_openlock); 432 sc->sc_flags |= LDF_LABELLING; 433 434 error = setdisklabel(sc->sc_dk.dk_label, 435 lp, /*sc->sc_dk.dk_openmask : */0, 436 sc->sc_dk.dk_cpulabel); 437 if (error == 0 && (cmd == DIOCWDINFO 438 #ifdef __HAVE_OLD_DISKLABEL 439 || cmd == ODIOCWDINFO 440 #endif 441 )) 442 error = writedisklabel( 443 MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART), 444 ldstrategy, sc->sc_dk.dk_label, 445 sc->sc_dk.dk_cpulabel); 446 447 sc->sc_flags &= ~LDF_LABELLING; 448 mutex_exit(&sc->sc_dk.dk_openlock); 449 break; 450 451 case DIOCKLABEL: 452 if ((flag & FWRITE) == 0) 453 return (EBADF); 454 if (*(int *)addr) 455 sc->sc_flags |= LDF_KLABEL; 456 else 457 sc->sc_flags &= ~LDF_KLABEL; 458 break; 459 460 case DIOCWLABEL: 461 if ((flag & FWRITE) == 0) 462 return (EBADF); 463 if (*(int *)addr) 464 sc->sc_flags |= LDF_WLABEL; 465 else 466 sc->sc_flags &= ~LDF_WLABEL; 467 break; 468 469 case DIOCGDEFLABEL: 470 ldgetdefaultlabel(sc, (struct disklabel *)addr); 471 break; 472 473 #ifdef __HAVE_OLD_DISKLABEL 474 case ODIOCGDEFLABEL: 475 ldgetdefaultlabel(sc, &newlabel); 476 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 477 return ENOTTY; 478 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 479 break; 480 #endif 481 482 case DIOCCACHESYNC: 483 /* 484 * XXX Do we really need to care about having a writable 485 * file descriptor here? 486 */ 487 if ((flag & FWRITE) == 0) 488 error = EBADF; 489 else if (sc->sc_flush) 490 error = (*sc->sc_flush)(sc); 491 else 492 error = 0; /* XXX Error out instead? */ 493 break; 494 495 case DIOCAWEDGE: 496 { 497 struct dkwedge_info *dkw = (void *) addr; 498 499 if ((flag & FWRITE) == 0) 500 return (EBADF); 501 502 /* If the ioctl happens here, the parent is us. */ 503 strlcpy(dkw->dkw_parent, device_xname(&sc->sc_dv), 504 sizeof(dkw->dkw_parent)); 505 return (dkwedge_add(dkw)); 506 } 507 508 case DIOCDWEDGE: 509 { 510 struct dkwedge_info *dkw = (void *) addr; 511 512 if ((flag & FWRITE) == 0) 513 return (EBADF); 514 515 /* If the ioctl happens here, the parent is us. */ 516 strlcpy(dkw->dkw_parent, device_xname(&sc->sc_dv), 517 sizeof(dkw->dkw_parent)); 518 return (dkwedge_del(dkw)); 519 } 520 521 case DIOCLWEDGES: 522 { 523 struct dkwedge_list *dkwl = (void *) addr; 524 525 return (dkwedge_list(&sc->sc_dk, dkwl, l)); 526 } 527 case DIOCGSTRATEGY: 528 { 529 struct disk_strategy *dks = (void *)addr; 530 531 mutex_enter(&sc->sc_mutex); 532 strlcpy(dks->dks_name, bufq_getstrategyname(sc->sc_bufq), 533 sizeof(dks->dks_name)); 534 mutex_exit(&sc->sc_mutex); 535 dks->dks_paramlen = 0; 536 537 return 0; 538 } 539 case DIOCSSTRATEGY: 540 { 541 struct disk_strategy *dks = (void *)addr; 542 struct bufq_state *new, *old; 543 544 if ((flag & FWRITE) == 0) 545 return EPERM; 546 547 if (dks->dks_param != NULL) 548 return EINVAL; 549 550 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */ 551 error = bufq_alloc(&new, dks->dks_name, 552 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK); 553 if (error) 554 return error; 555 556 mutex_enter(&sc->sc_mutex); 557 old = sc->sc_bufq; 558 bufq_move(new, old); 559 sc->sc_bufq = new; 560 mutex_exit(&sc->sc_mutex); 561 bufq_free(old); 562 563 return 0; 564 } 565 default: 566 error = ENOTTY; 567 break; 568 } 569 570 return (error); 571 } 572 573 static void 574 ldstrategy(struct buf *bp) 575 { 576 struct ld_softc *sc; 577 struct disklabel *lp; 578 daddr_t blkno; 579 int s, part; 580 581 sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev)); 582 part = DISKPART(bp->b_dev); 583 584 if ((sc->sc_flags & LDF_DETACH) != 0) { 585 bp->b_error = EIO; 586 goto done; 587 } 588 589 lp = sc->sc_dk.dk_label; 590 591 /* 592 * The transfer must be a whole number of blocks and the offset must 593 * not be negative. 594 */ 595 if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) { 596 bp->b_error = EINVAL; 597 goto done; 598 } 599 600 /* If it's a null transfer, return immediately. */ 601 if (bp->b_bcount == 0) 602 goto done; 603 604 /* 605 * Do bounds checking and adjust the transfer. If error, process. 606 * If past the end of partition, just return. 607 */ 608 if (part != RAW_PART && 609 bounds_check_with_label(&sc->sc_dk, bp, 610 (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) { 611 goto done; 612 } 613 614 /* 615 * Convert the block number to absolute and put it in terms 616 * of the device's logical block size. 617 */ 618 if (lp->d_secsize == DEV_BSIZE) 619 blkno = bp->b_blkno; 620 else if (lp->d_secsize > DEV_BSIZE) 621 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 622 else 623 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 624 625 if (part != RAW_PART) 626 blkno += lp->d_partitions[part].p_offset; 627 628 bp->b_rawblkno = blkno; 629 630 s = splbio(); 631 ldstart(sc, bp); 632 splx(s); 633 return; 634 635 done: 636 bp->b_resid = bp->b_bcount; 637 biodone(bp); 638 } 639 640 static void 641 ldstart(struct ld_softc *sc, struct buf *bp) 642 { 643 int error; 644 645 mutex_enter(&sc->sc_mutex); 646 647 if (bp != NULL) 648 BUFQ_PUT(sc->sc_bufq, bp); 649 650 while (sc->sc_queuecnt < sc->sc_maxqueuecnt) { 651 /* See if there is work to do. */ 652 if ((bp = BUFQ_PEEK(sc->sc_bufq)) == NULL) 653 break; 654 655 disk_busy(&sc->sc_dk); 656 sc->sc_queuecnt++; 657 658 if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) { 659 /* 660 * The back-end is running the job; remove it from 661 * the queue. 662 */ 663 (void) BUFQ_GET(sc->sc_bufq); 664 } else { 665 disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ)); 666 sc->sc_queuecnt--; 667 if (error == EAGAIN) { 668 /* 669 * Temporary resource shortage in the 670 * back-end; just defer the job until 671 * later. 672 * 673 * XXX We might consider a watchdog timer 674 * XXX to make sure we are kicked into action. 675 */ 676 break; 677 } else { 678 (void) BUFQ_GET(sc->sc_bufq); 679 bp->b_error = error; 680 bp->b_resid = bp->b_bcount; 681 mutex_exit(&sc->sc_mutex); 682 biodone(bp); 683 mutex_enter(&sc->sc_mutex); 684 } 685 } 686 } 687 688 mutex_exit(&sc->sc_mutex); 689 } 690 691 void 692 lddone(struct ld_softc *sc, struct buf *bp) 693 { 694 695 if (bp->b_error != 0) { 696 diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label); 697 printf("\n"); 698 } 699 700 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid, 701 (bp->b_flags & B_READ)); 702 #if NRND > 0 703 rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno); 704 #endif 705 biodone(bp); 706 707 mutex_enter(&sc->sc_mutex); 708 if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) { 709 if ((sc->sc_flags & LDF_DRAIN) != 0) { 710 sc->sc_flags &= ~LDF_DRAIN; 711 wakeup(&sc->sc_queuecnt); 712 } 713 mutex_exit(&sc->sc_mutex); 714 ldstart(sc, NULL); 715 } else 716 mutex_exit(&sc->sc_mutex); 717 } 718 719 static int 720 ldsize(dev_t dev) 721 { 722 struct ld_softc *sc; 723 int part, unit, omask, size; 724 725 unit = DISKUNIT(dev); 726 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL) 727 return (ENODEV); 728 if ((sc->sc_flags & LDF_ENABLED) == 0) 729 return (ENODEV); 730 part = DISKPART(dev); 731 732 omask = sc->sc_dk.dk_openmask & (1 << part); 733 734 if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0) 735 return (-1); 736 else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 737 size = -1; 738 else 739 size = sc->sc_dk.dk_label->d_partitions[part].p_size * 740 (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE); 741 if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0) 742 return (-1); 743 744 return (size); 745 } 746 747 /* 748 * Load the label information from the specified device. 749 */ 750 static void 751 ldgetdisklabel(struct ld_softc *sc) 752 { 753 const char *errstring; 754 755 ldgetdefaultlabel(sc, sc->sc_dk.dk_label); 756 757 /* Call the generic disklabel extraction routine. */ 758 errstring = readdisklabel(MAKEDISKDEV(0, device_unit(&sc->sc_dv), 759 RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel); 760 if (errstring != NULL) 761 printf("%s: %s\n", device_xname(&sc->sc_dv), errstring); 762 763 /* In-core label now valid. */ 764 sc->sc_flags |= LDF_VLABEL; 765 } 766 767 /* 768 * Construct a ficticious label. 769 */ 770 static void 771 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp) 772 { 773 774 memset(lp, 0, sizeof(struct disklabel)); 775 776 lp->d_secsize = sc->sc_secsize; 777 lp->d_ntracks = sc->sc_nheads; 778 lp->d_nsectors = sc->sc_nsectors; 779 lp->d_ncylinders = sc->sc_ncylinders; 780 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 781 lp->d_type = DTYPE_LD; 782 strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename)); 783 strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); 784 lp->d_secperunit = sc->sc_secperunit; 785 lp->d_rpm = 7200; 786 lp->d_interleave = 1; 787 lp->d_flags = 0; 788 789 lp->d_partitions[RAW_PART].p_offset = 0; 790 lp->d_partitions[RAW_PART].p_size = 791 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 792 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 793 lp->d_npartitions = RAW_PART + 1; 794 795 lp->d_magic = DISKMAGIC; 796 lp->d_magic2 = DISKMAGIC; 797 lp->d_checksum = dkcksum(lp); 798 } 799 800 /* 801 * Take a dump. 802 */ 803 static int 804 lddump(dev_t dev, daddr_t blkno, void *vav, size_t size) 805 { 806 char *va = vav; 807 struct ld_softc *sc; 808 struct disklabel *lp; 809 int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv; 810 static int dumping; 811 812 unit = DISKUNIT(dev); 813 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL) 814 return (ENXIO); 815 if ((sc->sc_flags & LDF_ENABLED) == 0) 816 return (ENODEV); 817 if (sc->sc_dump == NULL) 818 return (ENXIO); 819 820 /* Check if recursive dump; if so, punt. */ 821 if (dumping) 822 return (EFAULT); 823 dumping = 1; 824 825 /* Convert to disk sectors. Request must be a multiple of size. */ 826 part = DISKPART(dev); 827 lp = sc->sc_dk.dk_label; 828 if ((size % lp->d_secsize) != 0) 829 return (EFAULT); 830 towrt = size / lp->d_secsize; 831 blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */ 832 833 nsects = lp->d_partitions[part].p_size; 834 sectoff = lp->d_partitions[part].p_offset; 835 836 /* Check transfer bounds against partition size. */ 837 if ((blkno < 0) || ((blkno + towrt) > nsects)) 838 return (EINVAL); 839 840 /* Offset block number to start of partition. */ 841 blkno += sectoff; 842 843 /* Start dumping and return when done. */ 844 maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1; 845 while (towrt > 0) { 846 nblk = min(maxblkcnt, towrt); 847 848 if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0) 849 return (rv); 850 851 towrt -= nblk; 852 blkno += nblk; 853 va += nblk * sc->sc_secsize; 854 } 855 856 dumping = 0; 857 return (0); 858 } 859 860 /* 861 * Adjust the size of a transfer. 862 */ 863 static void 864 ldminphys(struct buf *bp) 865 { 866 struct ld_softc *sc; 867 868 sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev)); 869 870 if (bp->b_bcount > sc->sc_maxxfer) 871 bp->b_bcount = sc->sc_maxxfer; 872 minphys(bp); 873 } 874 875 static void 876 ld_set_properties(struct ld_softc *ld) 877 { 878 prop_dictionary_t disk_info, odisk_info, geom; 879 880 disk_info = prop_dictionary_create(); 881 882 geom = prop_dictionary_create(); 883 884 prop_dictionary_set_uint64(geom, "sectors-per-unit", 885 ld->sc_secperunit); 886 887 prop_dictionary_set_uint32(geom, "sector-size", 888 ld->sc_secsize); 889 890 prop_dictionary_set_uint16(geom, "sectors-per-track", 891 ld->sc_nsectors); 892 893 prop_dictionary_set_uint16(geom, "tracks-per-cylinder", 894 ld->sc_nheads); 895 896 prop_dictionary_set_uint64(geom, "cylinders-per-unit", 897 ld->sc_ncylinders); 898 899 prop_dictionary_set(disk_info, "geometry", geom); 900 prop_object_release(geom); 901 902 prop_dictionary_set(device_properties(&ld->sc_dv), 903 "disk-info", disk_info); 904 905 /* 906 * Don't release disk_info here; we keep a reference to it. 907 * disk_detach() will release it when we go away. 908 */ 909 910 odisk_info = ld->sc_dk.dk_info; 911 ld->sc_dk.dk_info = disk_info; 912 if (odisk_info) 913 prop_object_release(odisk_info); 914 } 915 916 static void 917 ld_config_interrupts (struct device *d) 918 { 919 struct ld_softc *sc = device_private(d); 920 dkwedge_discover(&sc->sc_dk); 921 } 922