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