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