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