1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/t_lock.h> 28 #include <sys/param.h> 29 #include <sys/conf.h> 30 #include <sys/systm.h> 31 #include <sys/sysmacros.h> 32 #include <sys/buf.h> 33 #include <sys/cred.h> 34 #include <sys/user.h> 35 #include <sys/stat.h> 36 #include <sys/uio.h> 37 #include <sys/vnode.h> 38 #include <sys/fs/snode.h> 39 #include <sys/open.h> 40 #include <sys/kmem.h> 41 #include <sys/file.h> 42 #include <sys/debug.h> 43 #include <sys/tnf_probe.h> 44 45 /* Don't #include <sys/ddi.h> - it #undef's getmajor() */ 46 47 #include <sys/sunddi.h> 48 #include <sys/sunndi.h> 49 #include <sys/sunpm.h> 50 #include <sys/ddi_impldefs.h> 51 #include <sys/ndi_impldefs.h> 52 #include <sys/esunddi.h> 53 #include <sys/autoconf.h> 54 #include <sys/modctl.h> 55 #include <sys/epm.h> 56 #include <sys/dacf.h> 57 #include <sys/sunmdi.h> 58 #include <sys/instance.h> 59 #include <sys/sdt.h> 60 61 static void i_attach_ctlop(dev_info_t *, ddi_attach_cmd_t, ddi_pre_post_t, int); 62 static void i_detach_ctlop(dev_info_t *, ddi_detach_cmd_t, ddi_pre_post_t, int); 63 64 /* decide what to do when a double dev_lclose is detected */ 65 #ifdef DEBUG 66 int dev_lclose_ce = CE_PANIC; 67 #else /* DEBUG */ 68 int dev_lclose_ce = CE_WARN; 69 #endif /* DEBUG */ 70 71 /* 72 * Configuration-related entry points for nexus and leaf drivers 73 */ 74 int 75 devi_identify(dev_info_t *devi) 76 { 77 struct dev_ops *ops; 78 int (*fn)(dev_info_t *); 79 80 if ((ops = ddi_get_driver(devi)) == NULL || 81 (fn = ops->devo_identify) == NULL) 82 return (-1); 83 84 return ((*fn)(devi)); 85 } 86 87 int 88 devi_probe(dev_info_t *devi) 89 { 90 int rv, probe_failed; 91 pm_ppm_cookie_t ppm_cookie; 92 struct dev_ops *ops; 93 int (*fn)(dev_info_t *); 94 95 ops = ddi_get_driver(devi); 96 ASSERT(ops); 97 98 pm_pre_probe(devi, &ppm_cookie); 99 100 /* 101 * probe(9E) in 2.0 implies that you can get 102 * away with not writing one of these .. so we 103 * pretend we're 'nulldev' if we don't find one (sigh). 104 */ 105 if ((fn = ops->devo_probe) == NULL) { 106 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) 107 rv = DDI_PROBE_DONTCARE; 108 else 109 rv = DDI_PROBE_FAILURE; 110 } else 111 rv = (*fn)(devi); 112 113 switch (rv) { 114 case DDI_PROBE_DONTCARE: 115 case DDI_PROBE_SUCCESS: 116 probe_failed = 0; 117 break; 118 default: 119 probe_failed = 1; 120 break; 121 } 122 pm_post_probe(&ppm_cookie, rv, probe_failed); 123 124 return (rv); 125 } 126 127 128 /* 129 * devi_attach() 130 * attach a device instance to the system if the driver supplies an 131 * attach(9E) entrypoint. 132 */ 133 int 134 devi_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 135 { 136 struct dev_ops *ops; 137 int error; 138 int (*fn)(dev_info_t *, ddi_attach_cmd_t); 139 pm_ppm_cookie_t pc; 140 141 if ((error = mdi_pre_attach(devi, cmd)) != DDI_SUCCESS) { 142 return (error); 143 } 144 145 pm_pre_attach(devi, &pc, cmd); 146 147 if ((cmd == DDI_RESUME || cmd == DDI_PM_RESUME) && 148 e_ddi_parental_suspend_resume(devi)) { 149 error = e_ddi_resume(devi, cmd); 150 goto done; 151 } 152 ops = ddi_get_driver(devi); 153 ASSERT(ops); 154 if ((fn = ops->devo_attach) == NULL) { 155 error = DDI_FAILURE; 156 goto done; 157 } 158 159 /* 160 * Call the driver's attach(9e) entrypoint 161 */ 162 i_attach_ctlop(devi, cmd, DDI_PRE, 0); 163 error = (*fn)(devi, cmd); 164 i_attach_ctlop(devi, cmd, DDI_POST, error); 165 166 done: 167 pm_post_attach(&pc, error); 168 mdi_post_attach(devi, cmd, error); 169 170 return (error); 171 } 172 173 /* 174 * devi_detach() 175 * detach a device instance from the system if the driver supplies a 176 * detach(9E) entrypoint. 177 */ 178 int 179 devi_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 180 { 181 struct dev_ops *ops; 182 int error; 183 int (*fn)(dev_info_t *, ddi_detach_cmd_t); 184 pm_ppm_cookie_t pc; 185 186 ASSERT(cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND || 187 cmd == DDI_DETACH); 188 189 if ((cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND) && 190 e_ddi_parental_suspend_resume(devi)) { 191 return (e_ddi_suspend(devi, cmd)); 192 } 193 ops = ddi_get_driver(devi); 194 ASSERT(ops); 195 if ((fn = ops->devo_detach) == NULL) 196 return (DDI_FAILURE); 197 198 if ((error = mdi_pre_detach(devi, cmd)) != DDI_SUCCESS) { 199 return (error); 200 } 201 i_detach_ctlop(devi, cmd, DDI_PRE, 0); 202 pm_pre_detach(devi, cmd, &pc); 203 204 /* 205 * Call the driver's detach routine 206 */ 207 error = (*fn)(devi, cmd); 208 209 pm_post_detach(&pc, error); 210 i_detach_ctlop(devi, cmd, DDI_POST, error); 211 mdi_post_detach(devi, cmd, error); 212 213 return (error); 214 } 215 216 static void 217 i_attach_ctlop(dev_info_t *devi, ddi_attach_cmd_t cmd, ddi_pre_post_t w, 218 int ret) 219 { 220 int error; 221 struct attachspec as; 222 dev_info_t *pdip = ddi_get_parent(devi); 223 224 as.cmd = cmd; 225 as.when = w; 226 as.pdip = pdip; 227 as.result = ret; 228 (void) ddi_ctlops(devi, devi, DDI_CTLOPS_ATTACH, &as, &error); 229 } 230 231 static void 232 i_detach_ctlop(dev_info_t *devi, ddi_detach_cmd_t cmd, ddi_pre_post_t w, 233 int ret) 234 { 235 int error; 236 struct detachspec ds; 237 dev_info_t *pdip = ddi_get_parent(devi); 238 239 ds.cmd = cmd; 240 ds.when = w; 241 ds.pdip = pdip; 242 ds.result = ret; 243 (void) ddi_ctlops(devi, devi, DDI_CTLOPS_DETACH, &ds, &error); 244 } 245 246 /* 247 * This entry point not defined by Solaris 2.0 DDI/DKI, so 248 * its inclusion here is somewhat moot. 249 */ 250 int 251 devi_reset(dev_info_t *devi, ddi_reset_cmd_t cmd) 252 { 253 struct dev_ops *ops; 254 int (*fn)(dev_info_t *, ddi_reset_cmd_t); 255 256 if ((ops = ddi_get_driver(devi)) == NULL || 257 (fn = ops->devo_reset) == NULL) 258 return (DDI_FAILURE); 259 260 return ((*fn)(devi, cmd)); 261 } 262 263 int 264 devi_quiesce(dev_info_t *devi) 265 { 266 struct dev_ops *ops; 267 int (*fn)(dev_info_t *); 268 269 if (((ops = ddi_get_driver(devi)) == NULL) || 270 (ops->devo_rev < 4) || ((fn = ops->devo_quiesce) == NULL)) 271 return (DDI_FAILURE); 272 273 return ((*fn)(devi)); 274 } 275 276 /* 277 * Leaf driver entry points. The following [cb]dev_* functions are *not* part 278 * of the DDI, please use functions defined in <sys/sunldi.h> and driver_lyr.c. 279 */ 280 int 281 dev_open(dev_t *devp, int flag, int type, struct cred *cred) 282 { 283 struct cb_ops *cb; 284 285 cb = devopsp[getmajor(*devp)]->devo_cb_ops; 286 return ((*cb->cb_open)(devp, flag, type, cred)); 287 } 288 289 int 290 dev_close(dev_t dev, int flag, int type, struct cred *cred) 291 { 292 struct cb_ops *cb; 293 294 cb = (devopsp[getmajor(dev)])->devo_cb_ops; 295 return ((*cb->cb_close)(dev, flag, type, cred)); 296 } 297 298 /* 299 * New Leaf driver open entry point. We make a vnode and go through specfs 300 * in order to obtain open close exclusions guarantees. Note that we drop 301 * OTYP_LYR if it was specified - we are going through specfs and it provides 302 * last close semantics (FKLYR is provided to open(9E)). Also, since 303 * spec_open will drive attach via e_ddi_hold_devi_by_dev for a makespecvp 304 * vnode with no SDIP_SET on the common snode, the dev_lopen caller no longer 305 * needs to call ddi_hold_installed_driver. 306 */ 307 int 308 dev_lopen(dev_t *devp, int flag, int otype, struct cred *cred) 309 { 310 struct vnode *vp; 311 int error; 312 struct vnode *cvp; 313 314 vp = makespecvp(*devp, (otype == OTYP_BLK) ? VBLK : VCHR); 315 error = VOP_OPEN(&vp, flag | FKLYR, cred, NULL); 316 if (error == 0) { 317 /* Pick up the (possibly) new dev_t value. */ 318 *devp = vp->v_rdev; 319 320 /* 321 * Place extra hold on the common vnode, which contains the 322 * open count, so that it is not destroyed by the VN_RELE of 323 * the shadow makespecvp vnode below. 324 */ 325 cvp = STOV(VTOCS(vp)); 326 VN_HOLD(cvp); 327 } 328 329 /* release the shadow makespecvp vnode. */ 330 VN_RELE(vp); 331 return (error); 332 } 333 334 /* 335 * Leaf driver close entry point. We make a vnode and go through specfs in 336 * order to obtain open close exclusions guarantees. Note that we drop 337 * OTYP_LYR if it was specified - we are going through specfs and it provides 338 * last close semantics (FLKYR is provided to close(9E)). 339 */ 340 int 341 dev_lclose(dev_t dev, int flag, int otype, struct cred *cred) 342 { 343 struct vnode *vp; 344 int error; 345 struct vnode *cvp; 346 char *funcname; 347 ulong_t offset; 348 349 vp = makespecvp(dev, (otype == OTYP_BLK) ? VBLK : VCHR); 350 error = VOP_CLOSE(vp, flag | FKLYR, 1, (offset_t)0, cred, NULL); 351 352 /* 353 * Release the extra dev_lopen hold on the common vnode. We inline a 354 * VN_RELE(cvp) call so that we can detect more dev_lclose calls than 355 * dev_lopen calls without panic. See vn_rele. If our inline of 356 * vn_rele called VOP_INACTIVE(cvp, CRED(), ...) we would panic on the 357 * "release the makespecvp vnode" VN_RELE(vp) that follows - so 358 * instead we diagnose this situation. Note that the driver has 359 * still seen a double close(9E), but that would have occurred with 360 * the old dev_close implementation too. 361 */ 362 cvp = STOV(VTOCS(vp)); 363 mutex_enter(&cvp->v_lock); 364 switch (cvp->v_count) { 365 default: 366 cvp->v_count--; 367 break; 368 369 case 0: 370 VTOS(vp)->s_commonvp = NULL; /* avoid panic */ 371 /*FALLTHROUGH*/ 372 case 1: 373 /* 374 * The following message indicates a serious problem in the 375 * identified driver, the driver should be fixed. If obtaining 376 * a panic dump is needed to diagnose the driver problem then 377 * adding "set dev_lclose_ce=3" to /etc/system will cause a 378 * panic when this occurs. 379 */ 380 funcname = modgetsymname((uintptr_t)caller(), &offset); 381 cmn_err(dev_lclose_ce, "dev_lclose: extra close of dev_t 0x%lx " 382 "from %s`%s()", dev, mod_containing_pc(caller()), 383 funcname ? funcname : "unknown..."); 384 break; 385 } 386 mutex_exit(&cvp->v_lock); 387 388 /* release the makespecvp vnode. */ 389 VN_RELE(vp); 390 return (error); 391 } 392 393 /* 394 * Returns -1 or the instance number of the given dev_t as 395 * interpreted by the device driver. The code may load the driver 396 * but it does not attach any instances. 397 * 398 * Instance is supposed to be a int but drivers have assumed that 399 * the pointer was a pointer to "void *" instead of a pointer to 400 * "int *" so we now explicitly pass a pointer to "void *" and then 401 * cast the result to an int when returning the value. 402 */ 403 int 404 dev_to_instance(dev_t dev) 405 { 406 major_t major = getmajor(dev); 407 struct dev_ops *ops; 408 void *vinstance; 409 int error; 410 411 /* verify that the driver is loaded */ 412 if ((ops = mod_hold_dev_by_major(major)) == NULL) 413 return (-1); 414 ASSERT(CB_DRV_INSTALLED(ops)); 415 416 /* verify that it supports the getinfo(9E) entry point */ 417 if (ops->devo_getinfo == NULL) { 418 mod_rele_dev_by_major(major); 419 return (-1); 420 } 421 422 /* ask the driver to extract the instance number from the devt */ 423 error = (*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2INSTANCE, 424 (void *)dev, &vinstance); 425 426 /* release the driver */ 427 mod_rele_dev_by_major(major); 428 429 if (error != DDI_SUCCESS) 430 return (-1); 431 432 return ((int)(uintptr_t)vinstance); 433 } 434 435 static void 436 bdev_strategy_tnf_probe(struct buf *bp) 437 { 438 /* Kernel probe */ 439 TNF_PROBE_5(strategy, "io blockio", /* CSTYLED */, 440 tnf_device, device, bp->b_edev, 441 tnf_diskaddr, block, bp->b_lblkno, 442 tnf_size, size, bp->b_bcount, 443 tnf_opaque, buf, bp, 444 tnf_bioflags, flags, bp->b_flags); 445 } 446 447 int 448 bdev_strategy(struct buf *bp) 449 { 450 struct dev_ops *ops; 451 452 ops = devopsp[getmajor(bp->b_edev)]; 453 454 /* 455 * Before we hit the io:::start probe, we need to fill in the b_dip 456 * field of the buf structure. This should be -- for the most part -- 457 * incredibly cheap. If you're in this code looking to bum cycles, 458 * there is almost certainly bigger game further down the I/O path... 459 */ 460 (void) ops->devo_getinfo(NULL, DDI_INFO_DEVT2DEVINFO, 461 (void *)bp->b_edev, (void **)&bp->b_dip); 462 463 DTRACE_IO1(start, struct buf *, bp); 464 bp->b_flags |= B_STARTED; 465 466 /* 467 * Call the TNF probe here instead of the inline code 468 * to force our compiler to use the tail call optimization. 469 */ 470 bdev_strategy_tnf_probe(bp); 471 472 return (ops->devo_cb_ops->cb_strategy(bp)); 473 } 474 475 int 476 bdev_print(dev_t dev, caddr_t str) 477 { 478 struct cb_ops *cb; 479 480 cb = devopsp[getmajor(dev)]->devo_cb_ops; 481 return ((*cb->cb_print)(dev, str)); 482 } 483 484 /* 485 * Return number of DEV_BSIZE byte blocks. 486 */ 487 int 488 bdev_size(dev_t dev) 489 { 490 uint_t nblocks; 491 uint_t blksize; 492 493 if ((nblocks = e_ddi_getprop(dev, VBLK, "nblocks", 494 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 495 return (-1); 496 497 /* Get blksize, default to DEV_BSIZE */ 498 if ((blksize = e_ddi_getprop(dev, VBLK, "blksize", 499 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 500 blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize", 501 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE); 502 503 if (blksize >= DEV_BSIZE) 504 return (nblocks * (blksize / DEV_BSIZE)); 505 else 506 return (nblocks / (DEV_BSIZE / blksize)); 507 } 508 509 /* 510 * Same for 64-bit Nblocks property 511 */ 512 uint64_t 513 bdev_Size(dev_t dev) 514 { 515 uint64_t nblocks; 516 uint_t blksize; 517 518 if ((nblocks = e_ddi_getprop_int64(dev, VBLK, "Nblocks", 519 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 520 return (-1); 521 522 /* Get blksize, default to DEV_BSIZE */ 523 if ((blksize = e_ddi_getprop(dev, VBLK, "blksize", 524 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 525 blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize", 526 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE); 527 528 if (blksize >= DEV_BSIZE) 529 return (nblocks * (blksize / DEV_BSIZE)); 530 else 531 return (nblocks / (DEV_BSIZE / blksize)); 532 } 533 534 int 535 bdev_dump(dev_t dev, caddr_t addr, daddr_t blkno, int blkcnt) 536 { 537 struct cb_ops *cb; 538 539 cb = devopsp[getmajor(dev)]->devo_cb_ops; 540 return ((*cb->cb_dump)(dev, addr, blkno, blkcnt)); 541 } 542 543 int 544 cdev_read(dev_t dev, struct uio *uiop, struct cred *cred) 545 { 546 struct cb_ops *cb; 547 548 cb = devopsp[getmajor(dev)]->devo_cb_ops; 549 return ((*cb->cb_read)(dev, uiop, cred)); 550 } 551 552 int 553 cdev_write(dev_t dev, struct uio *uiop, struct cred *cred) 554 { 555 struct cb_ops *cb; 556 557 cb = devopsp[getmajor(dev)]->devo_cb_ops; 558 return ((*cb->cb_write)(dev, uiop, cred)); 559 } 560 561 int 562 cdev_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cred, 563 int *rvalp) 564 { 565 struct cb_ops *cb; 566 567 cb = devopsp[getmajor(dev)]->devo_cb_ops; 568 return ((*cb->cb_ioctl)(dev, cmd, arg, mode, cred, rvalp)); 569 } 570 571 int 572 cdev_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len, 573 size_t *maplen, uint_t mode) 574 { 575 struct cb_ops *cb; 576 577 cb = devopsp[getmajor(dev)]->devo_cb_ops; 578 return ((*cb->cb_devmap)(dev, dhp, off, len, maplen, mode)); 579 } 580 581 int 582 cdev_mmap(int (*mapfunc)(dev_t, off_t, int), dev_t dev, off_t off, int prot) 583 { 584 return ((*mapfunc)(dev, off, prot)); 585 } 586 587 int 588 cdev_segmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len, 589 uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp) 590 { 591 struct cb_ops *cb; 592 593 cb = devopsp[getmajor(dev)]->devo_cb_ops; 594 return ((*cb->cb_segmap)(dev, off, as, addrp, 595 len, prot, maxprot, flags, credp)); 596 } 597 598 int 599 cdev_poll(dev_t dev, short events, int anyyet, short *reventsp, 600 struct pollhead **pollhdrp) 601 { 602 struct cb_ops *cb; 603 604 cb = devopsp[getmajor(dev)]->devo_cb_ops; 605 return ((*cb->cb_chpoll)(dev, events, anyyet, reventsp, pollhdrp)); 606 } 607 608 /* 609 * A 'size' property can be provided by a VCHR device. 610 * 611 * Since it's defined as zero for STREAMS devices, so we avoid the 612 * overhead of looking it up. Note also that we don't force an 613 * unused driver into memory simply to ask about it's size. We also 614 * don't bother to ask it its size unless it's already been attached 615 * (the attach routine is the earliest place the property will be created) 616 * 617 * XXX In an ideal world, we'd call this at VOP_GETATTR() time. 618 */ 619 int 620 cdev_size(dev_t dev) 621 { 622 major_t maj; 623 struct devnames *dnp; 624 625 if ((maj = getmajor(dev)) >= devcnt) 626 return (0); 627 628 dnp = &(devnamesp[maj]); 629 LOCK_DEV_OPS(&dnp->dn_lock); 630 if (devopsp[maj] && devopsp[maj]->devo_cb_ops && 631 !devopsp[maj]->devo_cb_ops->cb_str) { 632 UNLOCK_DEV_OPS(&dnp->dn_lock); 633 return (e_ddi_getprop(dev, VCHR, "size", 634 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0)); 635 } 636 UNLOCK_DEV_OPS(&dnp->dn_lock); 637 return (0); 638 } 639 640 /* 641 * same for 64-bit Size property 642 */ 643 uint64_t 644 cdev_Size(dev_t dev) 645 { 646 major_t maj; 647 struct devnames *dnp; 648 649 if ((maj = getmajor(dev)) >= devcnt) 650 return (0); 651 652 dnp = &(devnamesp[maj]); 653 LOCK_DEV_OPS(&dnp->dn_lock); 654 if (devopsp[maj] && devopsp[maj]->devo_cb_ops && 655 !devopsp[maj]->devo_cb_ops->cb_str) { 656 UNLOCK_DEV_OPS(&dnp->dn_lock); 657 return (e_ddi_getprop_int64(dev, VCHR, "Size", 658 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0)); 659 } 660 UNLOCK_DEV_OPS(&dnp->dn_lock); 661 return (0); 662 } 663 664 /* 665 * XXX This routine is poorly named, because block devices can and do 666 * have properties (see bdev_size() above). 667 * 668 * XXX fix the comment in devops.h that claims that cb_prop_op 669 * is character-only. 670 */ 671 int 672 cdev_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 673 char *name, caddr_t valuep, int *lengthp) 674 { 675 struct cb_ops *cb; 676 677 if ((cb = devopsp[DEVI(dip)->devi_major]->devo_cb_ops) == NULL) 678 return (DDI_PROP_NOT_FOUND); 679 680 return ((*cb->cb_prop_op)(dev, dip, prop_op, mod_flags, 681 name, valuep, lengthp)); 682 } 683