10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*4582Scth * Common Development and Distribution License (the "License"). 6*4582Scth * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*4582Scth * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/types.h> 280Sstevel@tonic-gate #include <sys/t_lock.h> 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/conf.h> 310Sstevel@tonic-gate #include <sys/systm.h> 320Sstevel@tonic-gate #include <sys/sysmacros.h> 330Sstevel@tonic-gate #include <sys/buf.h> 340Sstevel@tonic-gate #include <sys/cred.h> 350Sstevel@tonic-gate #include <sys/user.h> 360Sstevel@tonic-gate #include <sys/stat.h> 370Sstevel@tonic-gate #include <sys/uio.h> 380Sstevel@tonic-gate #include <sys/vnode.h> 390Sstevel@tonic-gate #include <sys/fs/snode.h> 400Sstevel@tonic-gate #include <sys/open.h> 410Sstevel@tonic-gate #include <sys/kmem.h> 420Sstevel@tonic-gate #include <sys/file.h> 430Sstevel@tonic-gate #include <sys/debug.h> 440Sstevel@tonic-gate #include <sys/tnf_probe.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate /* Don't #include <sys/ddi.h> - it #undef's getmajor() */ 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <sys/sunddi.h> 490Sstevel@tonic-gate #include <sys/sunndi.h> 500Sstevel@tonic-gate #include <sys/sunpm.h> 510Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 520Sstevel@tonic-gate #include <sys/ndi_impldefs.h> 530Sstevel@tonic-gate #include <sys/esunddi.h> 540Sstevel@tonic-gate #include <sys/autoconf.h> 550Sstevel@tonic-gate #include <sys/modctl.h> 560Sstevel@tonic-gate #include <sys/epm.h> 570Sstevel@tonic-gate #include <sys/dacf.h> 580Sstevel@tonic-gate #include <sys/sunmdi.h> 590Sstevel@tonic-gate #include <sys/instance.h> 600Sstevel@tonic-gate #include <sys/sdt.h> 610Sstevel@tonic-gate 620Sstevel@tonic-gate static void i_attach_ctlop(dev_info_t *, ddi_attach_cmd_t, ddi_pre_post_t, int); 630Sstevel@tonic-gate static void i_detach_ctlop(dev_info_t *, ddi_detach_cmd_t, ddi_pre_post_t, int); 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* decide what to do when a double dev_lclose is detected */ 660Sstevel@tonic-gate #ifdef DEBUG 670Sstevel@tonic-gate int dev_lclose_ce = CE_PANIC; 680Sstevel@tonic-gate #else /* DEBUG */ 690Sstevel@tonic-gate int dev_lclose_ce = CE_WARN; 700Sstevel@tonic-gate #endif /* DEBUG */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * Configuration-related entry points for nexus and leaf drivers 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate int 760Sstevel@tonic-gate devi_identify(dev_info_t *devi) 770Sstevel@tonic-gate { 780Sstevel@tonic-gate struct dev_ops *ops; 790Sstevel@tonic-gate int (*fn)(dev_info_t *); 800Sstevel@tonic-gate 810Sstevel@tonic-gate if ((ops = ddi_get_driver(devi)) == NULL || 820Sstevel@tonic-gate (fn = ops->devo_identify) == NULL) 830Sstevel@tonic-gate return (-1); 840Sstevel@tonic-gate 850Sstevel@tonic-gate return ((*fn)(devi)); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate int 890Sstevel@tonic-gate devi_probe(dev_info_t *devi) 900Sstevel@tonic-gate { 910Sstevel@tonic-gate int rv, probe_failed; 920Sstevel@tonic-gate pm_ppm_cookie_t ppm_cookie; 930Sstevel@tonic-gate struct dev_ops *ops; 940Sstevel@tonic-gate int (*fn)(dev_info_t *); 950Sstevel@tonic-gate 960Sstevel@tonic-gate ops = ddi_get_driver(devi); 970Sstevel@tonic-gate ASSERT(ops); 980Sstevel@tonic-gate 990Sstevel@tonic-gate pm_pre_probe(devi, &ppm_cookie); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * probe(9E) in 2.0 implies that you can get 1030Sstevel@tonic-gate * away with not writing one of these .. so we 1040Sstevel@tonic-gate * pretend we're 'nulldev' if we don't find one (sigh). 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate if ((fn = ops->devo_probe) == NULL) 1070Sstevel@tonic-gate rv = DDI_PROBE_DONTCARE; 1080Sstevel@tonic-gate else 1090Sstevel@tonic-gate rv = (*fn)(devi); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate switch (rv) { 1120Sstevel@tonic-gate case DDI_PROBE_DONTCARE: 1130Sstevel@tonic-gate case DDI_PROBE_SUCCESS: 1140Sstevel@tonic-gate probe_failed = 0; 1150Sstevel@tonic-gate break; 1160Sstevel@tonic-gate default: 1170Sstevel@tonic-gate probe_failed = 1; 1180Sstevel@tonic-gate break; 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate pm_post_probe(&ppm_cookie, rv, probe_failed); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate return (rv); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * devi_attach() 1280Sstevel@tonic-gate * attach a device instance to the system if the driver supplies an 1290Sstevel@tonic-gate * attach(9E) entrypoint. 1300Sstevel@tonic-gate */ 1310Sstevel@tonic-gate int 1320Sstevel@tonic-gate devi_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate struct dev_ops *ops; 1350Sstevel@tonic-gate int error; 1360Sstevel@tonic-gate int (*fn)(dev_info_t *, ddi_attach_cmd_t); 1370Sstevel@tonic-gate pm_ppm_cookie_t pc; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate if ((error = mdi_pre_attach(devi, cmd)) != DDI_SUCCESS) { 1400Sstevel@tonic-gate return (error); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate pm_pre_attach(devi, &pc, cmd); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate if ((cmd == DDI_RESUME || cmd == DDI_PM_RESUME) && 1460Sstevel@tonic-gate e_ddi_parental_suspend_resume(devi)) { 1470Sstevel@tonic-gate error = e_ddi_resume(devi, cmd); 1480Sstevel@tonic-gate goto done; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate ops = ddi_get_driver(devi); 1510Sstevel@tonic-gate ASSERT(ops); 1520Sstevel@tonic-gate if ((fn = ops->devo_attach) == NULL) { 1530Sstevel@tonic-gate error = DDI_FAILURE; 1540Sstevel@tonic-gate goto done; 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * Call the driver's attach(9e) entrypoint 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate i_attach_ctlop(devi, cmd, DDI_PRE, 0); 1610Sstevel@tonic-gate error = (*fn)(devi, cmd); 1620Sstevel@tonic-gate i_attach_ctlop(devi, cmd, DDI_POST, error); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate done: 1650Sstevel@tonic-gate pm_post_attach(&pc, error); 1660Sstevel@tonic-gate mdi_post_attach(devi, cmd, error); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate return (error); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * devi_detach() 1730Sstevel@tonic-gate * detach a device instance from the system if the driver supplies a 1740Sstevel@tonic-gate * detach(9E) entrypoint. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate int 1770Sstevel@tonic-gate devi_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1780Sstevel@tonic-gate { 1790Sstevel@tonic-gate struct dev_ops *ops; 1800Sstevel@tonic-gate int error; 1810Sstevel@tonic-gate int (*fn)(dev_info_t *, ddi_detach_cmd_t); 1820Sstevel@tonic-gate pm_ppm_cookie_t pc; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate ASSERT(cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND || 1850Sstevel@tonic-gate cmd == DDI_DETACH); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate if ((cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND) && 1880Sstevel@tonic-gate e_ddi_parental_suspend_resume(devi)) { 1890Sstevel@tonic-gate return (e_ddi_suspend(devi, cmd)); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate ops = ddi_get_driver(devi); 1920Sstevel@tonic-gate ASSERT(ops); 1930Sstevel@tonic-gate if ((fn = ops->devo_detach) == NULL) 1940Sstevel@tonic-gate return (DDI_FAILURE); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate if ((error = mdi_pre_detach(devi, cmd)) != DDI_SUCCESS) { 1970Sstevel@tonic-gate return (error); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate i_detach_ctlop(devi, cmd, DDI_PRE, 0); 2000Sstevel@tonic-gate pm_pre_detach(devi, cmd, &pc); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* 2030Sstevel@tonic-gate * Call the driver's detach routine 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate error = (*fn)(devi, cmd); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate pm_post_detach(&pc, error); 2080Sstevel@tonic-gate i_detach_ctlop(devi, cmd, DDI_POST, error); 2090Sstevel@tonic-gate mdi_post_detach(devi, cmd, error); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate return (error); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate static void 2150Sstevel@tonic-gate i_attach_ctlop(dev_info_t *devi, ddi_attach_cmd_t cmd, ddi_pre_post_t w, 2160Sstevel@tonic-gate int ret) 2170Sstevel@tonic-gate { 2180Sstevel@tonic-gate int error; 2190Sstevel@tonic-gate struct attachspec as; 2200Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(devi); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate as.cmd = cmd; 2230Sstevel@tonic-gate as.when = w; 2240Sstevel@tonic-gate as.pdip = pdip; 2250Sstevel@tonic-gate as.result = ret; 2260Sstevel@tonic-gate (void) ddi_ctlops(devi, devi, DDI_CTLOPS_ATTACH, &as, &error); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate static void 2300Sstevel@tonic-gate i_detach_ctlop(dev_info_t *devi, ddi_detach_cmd_t cmd, ddi_pre_post_t w, 2310Sstevel@tonic-gate int ret) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate int error; 2340Sstevel@tonic-gate struct detachspec ds; 2350Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(devi); 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate ds.cmd = cmd; 2380Sstevel@tonic-gate ds.when = w; 2390Sstevel@tonic-gate ds.pdip = pdip; 2400Sstevel@tonic-gate ds.result = ret; 2410Sstevel@tonic-gate (void) ddi_ctlops(devi, devi, DDI_CTLOPS_DETACH, &ds, &error); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * This entry point not defined by Solaris 2.0 DDI/DKI, so 2460Sstevel@tonic-gate * its inclusion here is somewhat moot. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate int 2490Sstevel@tonic-gate devi_reset(dev_info_t *devi, ddi_reset_cmd_t cmd) 2500Sstevel@tonic-gate { 2510Sstevel@tonic-gate struct dev_ops *ops; 2520Sstevel@tonic-gate int (*fn)(dev_info_t *, ddi_reset_cmd_t); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate if ((ops = ddi_get_driver(devi)) == NULL || 2550Sstevel@tonic-gate (fn = ops->devo_reset) == NULL) 2560Sstevel@tonic-gate return (DDI_FAILURE); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate return ((*fn)(devi, cmd)); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* 2620Sstevel@tonic-gate * Leaf driver entry points. The following [cb]dev_* functions are *not* part 2630Sstevel@tonic-gate * of the DDI, please use functions defined in <sys/sunldi.h> and driver_lyr.c. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate int 2660Sstevel@tonic-gate dev_open(dev_t *devp, int flag, int type, struct cred *cred) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate struct cb_ops *cb; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate cb = devopsp[getmajor(*devp)]->devo_cb_ops; 2710Sstevel@tonic-gate return ((*cb->cb_open)(devp, flag, type, cred)); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate int 2750Sstevel@tonic-gate dev_close(dev_t dev, int flag, int type, struct cred *cred) 2760Sstevel@tonic-gate { 2770Sstevel@tonic-gate struct cb_ops *cb; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate cb = (devopsp[getmajor(dev)])->devo_cb_ops; 2800Sstevel@tonic-gate return ((*cb->cb_close)(dev, flag, type, cred)); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate /* 2840Sstevel@tonic-gate * New Leaf driver open entry point. We make a vnode and go through specfs 2850Sstevel@tonic-gate * in order to obtain open close exclusions guarantees. Note that we drop 2860Sstevel@tonic-gate * OTYP_LYR if it was specified - we are going through specfs and it provides 2870Sstevel@tonic-gate * last close semantics (FKLYR is provided to open(9E)). Also, since 2880Sstevel@tonic-gate * spec_open will drive attach via e_ddi_hold_devi_by_dev for a makespecvp 2890Sstevel@tonic-gate * vnode with no SDIP_SET on the common snode, the dev_lopen caller no longer 2900Sstevel@tonic-gate * needs to call ddi_hold_installed_driver. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate int 2930Sstevel@tonic-gate dev_lopen(dev_t *devp, int flag, int otype, struct cred *cred) 2940Sstevel@tonic-gate { 2950Sstevel@tonic-gate struct vnode *vp; 2960Sstevel@tonic-gate int error; 2970Sstevel@tonic-gate struct vnode *cvp; 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate vp = makespecvp(*devp, (otype == OTYP_BLK) ? VBLK : VCHR); 3000Sstevel@tonic-gate error = VOP_OPEN(&vp, flag | FKLYR, cred); 3010Sstevel@tonic-gate if (error == 0) { 3020Sstevel@tonic-gate /* Pick up the (possibly) new dev_t value. */ 3030Sstevel@tonic-gate *devp = vp->v_rdev; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* 3060Sstevel@tonic-gate * Place extra hold on the common vnode, which contains the 3070Sstevel@tonic-gate * open count, so that it is not destroyed by the VN_RELE of 3080Sstevel@tonic-gate * the shadow makespecvp vnode below. 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate cvp = STOV(VTOCS(vp)); 3110Sstevel@tonic-gate VN_HOLD(cvp); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* release the shadow makespecvp vnode. */ 3150Sstevel@tonic-gate VN_RELE(vp); 3160Sstevel@tonic-gate return (error); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * Leaf driver close entry point. We make a vnode and go through specfs in 3210Sstevel@tonic-gate * order to obtain open close exclusions guarantees. Note that we drop 3220Sstevel@tonic-gate * OTYP_LYR if it was specified - we are going through specfs and it provides 3230Sstevel@tonic-gate * last close semantics (FLKYR is provided to close(9E)). 3240Sstevel@tonic-gate */ 3250Sstevel@tonic-gate int 3260Sstevel@tonic-gate dev_lclose(dev_t dev, int flag, int otype, struct cred *cred) 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate struct vnode *vp; 3290Sstevel@tonic-gate int error; 3300Sstevel@tonic-gate struct vnode *cvp; 3310Sstevel@tonic-gate char *funcname; 3320Sstevel@tonic-gate ulong_t offset; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate vp = makespecvp(dev, (otype == OTYP_BLK) ? VBLK : VCHR); 3350Sstevel@tonic-gate error = VOP_CLOSE(vp, flag | FKLYR, 1, (offset_t)0, cred); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Release the extra dev_lopen hold on the common vnode. We inline a 3390Sstevel@tonic-gate * VN_RELE(cvp) call so that we can detect more dev_lclose calls than 3400Sstevel@tonic-gate * dev_lopen calls without panic. See vn_rele. If our inline of 3410Sstevel@tonic-gate * vn_rele called VOP_INACTIVE(cvp, CRED()) we would panic on the 3420Sstevel@tonic-gate * "release the makespecvp vnode" VN_RELE(vp) that follows - so 3430Sstevel@tonic-gate * instead we diagnose this situation. Note that the driver has 3440Sstevel@tonic-gate * still seen a double close(9E), but that would have occurred with 3450Sstevel@tonic-gate * the old dev_close implementation too. 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate cvp = STOV(VTOCS(vp)); 3480Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 3490Sstevel@tonic-gate switch (cvp->v_count) { 3500Sstevel@tonic-gate default: 3510Sstevel@tonic-gate cvp->v_count--; 3520Sstevel@tonic-gate break; 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate case 0: 3550Sstevel@tonic-gate VTOS(vp)->s_commonvp = NULL; /* avoid panic */ 3560Sstevel@tonic-gate /*FALLTHROUGH*/ 3570Sstevel@tonic-gate case 1: 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * The following message indicates a serious problem in the 3600Sstevel@tonic-gate * identified driver, the driver should be fixed. If obtaining 3610Sstevel@tonic-gate * a panic dump is needed to diagnose the driver problem then 3620Sstevel@tonic-gate * adding "set dev_lclose_ce=3" to /etc/system will cause a 3630Sstevel@tonic-gate * panic when this occurs. 3640Sstevel@tonic-gate */ 3650Sstevel@tonic-gate funcname = modgetsymname((uintptr_t)caller(), &offset); 3660Sstevel@tonic-gate cmn_err(dev_lclose_ce, "dev_lclose: extra close of dev_t 0x%lx " 3670Sstevel@tonic-gate "from %s`%s()", dev, mod_containing_pc(caller()), 3680Sstevel@tonic-gate funcname ? funcname : "unknown..."); 3690Sstevel@tonic-gate break; 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate /* release the makespecvp vnode. */ 3740Sstevel@tonic-gate VN_RELE(vp); 3750Sstevel@tonic-gate return (error); 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * Returns -1 or the instance number of the given dev_t as 3800Sstevel@tonic-gate * interpreted by the device driver. The code may load the driver 3810Sstevel@tonic-gate * but it does not attach any instances. 3820Sstevel@tonic-gate * 3830Sstevel@tonic-gate * Instance is supposed to be a int but drivers have assumed that 3840Sstevel@tonic-gate * the pointer was a pointer to "void *" instead of a pointer to 3850Sstevel@tonic-gate * "int *" so we now explicitly pass a pointer to "void *" and then 3860Sstevel@tonic-gate * cast the result to an int when returning the value. 3870Sstevel@tonic-gate */ 3880Sstevel@tonic-gate int 3890Sstevel@tonic-gate dev_to_instance(dev_t dev) 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate major_t major = getmajor(dev); 3920Sstevel@tonic-gate struct dev_ops *ops; 3930Sstevel@tonic-gate void *vinstance; 3940Sstevel@tonic-gate int error; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* verify that the major number is reasonable and driver is loaded */ 3970Sstevel@tonic-gate if ((major >= devcnt) || 3980Sstevel@tonic-gate ((ops = mod_hold_dev_by_major(major)) == NULL)) 3990Sstevel@tonic-gate return (-1); 4000Sstevel@tonic-gate ASSERT(CB_DRV_INSTALLED(ops)); 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* verify that it supports the getinfo(9E) entry point */ 4030Sstevel@tonic-gate if (ops->devo_getinfo == NULL) { 4040Sstevel@tonic-gate mod_rele_dev_by_major(major); 4050Sstevel@tonic-gate return (-1); 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate /* ask the driver to extract the instance number from the devt */ 4090Sstevel@tonic-gate error = (*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2INSTANCE, 4100Sstevel@tonic-gate (void *)dev, &vinstance); 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* release the driver */ 4130Sstevel@tonic-gate mod_rele_dev_by_major(major); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if (error != DDI_SUCCESS) 4160Sstevel@tonic-gate return (-1); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate return ((int)(uintptr_t)vinstance); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate static void 4220Sstevel@tonic-gate bdev_strategy_tnf_probe(struct buf *bp) 4230Sstevel@tonic-gate { 4240Sstevel@tonic-gate /* Kernel probe */ 4250Sstevel@tonic-gate TNF_PROBE_5(strategy, "io blockio", /* CSTYLED */, 426*4582Scth tnf_device, device, bp->b_edev, 427*4582Scth tnf_diskaddr, block, bp->b_lblkno, 428*4582Scth tnf_size, size, bp->b_bcount, 429*4582Scth tnf_opaque, buf, bp, 430*4582Scth tnf_bioflags, flags, bp->b_flags); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate int 4340Sstevel@tonic-gate bdev_strategy(struct buf *bp) 4350Sstevel@tonic-gate { 4360Sstevel@tonic-gate struct dev_ops *ops; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate ops = devopsp[getmajor(bp->b_edev)]; 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * Before we hit the io:::start probe, we need to fill in the b_dip 4420Sstevel@tonic-gate * field of the buf structure. This should be -- for the most part -- 4430Sstevel@tonic-gate * incredibly cheap. If you're in this code looking to bum cycles, 4440Sstevel@tonic-gate * there is almost certainly bigger game further down the I/O path... 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate (void) ops->devo_getinfo(NULL, DDI_INFO_DEVT2DEVINFO, 4470Sstevel@tonic-gate (void *)bp->b_edev, (void **)&bp->b_dip); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate DTRACE_IO1(start, struct buf *, bp); 4500Sstevel@tonic-gate bp->b_flags |= B_STARTED; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate /* 4530Sstevel@tonic-gate * Call the TNF probe here instead of the inline code 4540Sstevel@tonic-gate * to force our compiler to use the tail call optimization. 4550Sstevel@tonic-gate */ 4560Sstevel@tonic-gate bdev_strategy_tnf_probe(bp); 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate return (ops->devo_cb_ops->cb_strategy(bp)); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate int 4620Sstevel@tonic-gate bdev_print(dev_t dev, caddr_t str) 4630Sstevel@tonic-gate { 4640Sstevel@tonic-gate struct cb_ops *cb; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 4670Sstevel@tonic-gate return ((*cb->cb_print)(dev, str)); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 470*4582Scth /* 471*4582Scth * Return number of DEV_BSIZE byte blocks. 472*4582Scth */ 4730Sstevel@tonic-gate int 4740Sstevel@tonic-gate bdev_size(dev_t dev) 4750Sstevel@tonic-gate { 476*4582Scth uint_t nblocks; 477*4582Scth uint_t blksize; 478*4582Scth 479*4582Scth if ((nblocks = e_ddi_getprop(dev, VBLK, "nblocks", 480*4582Scth DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 481*4582Scth return (-1); 482*4582Scth 483*4582Scth /* Get blksize, default to DEV_BSIZE */ 484*4582Scth if ((blksize = e_ddi_getprop(dev, VBLK, "blksize", 485*4582Scth DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 486*4582Scth blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize", 487*4582Scth DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE); 488*4582Scth 489*4582Scth if (blksize >= DEV_BSIZE) 490*4582Scth return (nblocks * (blksize / DEV_BSIZE)); 491*4582Scth else 492*4582Scth return (nblocks / (DEV_BSIZE / blksize)); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate /* 4960Sstevel@tonic-gate * Same for 64-bit Nblocks property 4970Sstevel@tonic-gate */ 4980Sstevel@tonic-gate uint64_t 4990Sstevel@tonic-gate bdev_Size(dev_t dev) 5000Sstevel@tonic-gate { 501*4582Scth uint64_t nblocks; 502*4582Scth uint_t blksize; 503*4582Scth 504*4582Scth if ((nblocks = e_ddi_getprop_int64(dev, VBLK, "Nblocks", 505*4582Scth DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 506*4582Scth return (-1); 507*4582Scth 508*4582Scth /* Get blksize, default to DEV_BSIZE */ 509*4582Scth if ((blksize = e_ddi_getprop(dev, VBLK, "blksize", 510*4582Scth DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1) 511*4582Scth blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize", 512*4582Scth DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE); 513*4582Scth 514*4582Scth if (blksize >= DEV_BSIZE) 515*4582Scth return (nblocks * (blksize / DEV_BSIZE)); 516*4582Scth else 517*4582Scth return (nblocks / (DEV_BSIZE / blksize)); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate int 5210Sstevel@tonic-gate bdev_dump(dev_t dev, caddr_t addr, daddr_t blkno, int blkcnt) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate struct cb_ops *cb; 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5260Sstevel@tonic-gate return ((*cb->cb_dump)(dev, addr, blkno, blkcnt)); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate int 5300Sstevel@tonic-gate cdev_read(dev_t dev, struct uio *uiop, struct cred *cred) 5310Sstevel@tonic-gate { 5320Sstevel@tonic-gate struct cb_ops *cb; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5350Sstevel@tonic-gate return ((*cb->cb_read)(dev, uiop, cred)); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate int 5390Sstevel@tonic-gate cdev_write(dev_t dev, struct uio *uiop, struct cred *cred) 5400Sstevel@tonic-gate { 5410Sstevel@tonic-gate struct cb_ops *cb; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5440Sstevel@tonic-gate return ((*cb->cb_write)(dev, uiop, cred)); 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate int 5480Sstevel@tonic-gate cdev_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cred, 5490Sstevel@tonic-gate int *rvalp) 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate struct cb_ops *cb; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5540Sstevel@tonic-gate return ((*cb->cb_ioctl)(dev, cmd, arg, mode, cred, rvalp)); 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate int 5580Sstevel@tonic-gate cdev_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len, 5590Sstevel@tonic-gate size_t *maplen, uint_t mode) 5600Sstevel@tonic-gate { 5610Sstevel@tonic-gate struct cb_ops *cb; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5640Sstevel@tonic-gate return ((*cb->cb_devmap)(dev, dhp, off, len, maplen, mode)); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate int 5680Sstevel@tonic-gate cdev_mmap(int (*mapfunc)(dev_t, off_t, int), dev_t dev, off_t off, int prot) 5690Sstevel@tonic-gate { 5700Sstevel@tonic-gate return ((*mapfunc)(dev, off, prot)); 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate int 5740Sstevel@tonic-gate cdev_segmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len, 5750Sstevel@tonic-gate uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp) 5760Sstevel@tonic-gate { 5770Sstevel@tonic-gate struct cb_ops *cb; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5800Sstevel@tonic-gate return ((*cb->cb_segmap)(dev, off, as, addrp, 5810Sstevel@tonic-gate len, prot, maxprot, flags, credp)); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate int 5850Sstevel@tonic-gate cdev_poll(dev_t dev, short events, int anyyet, short *reventsp, 5860Sstevel@tonic-gate struct pollhead **pollhdrp) 5870Sstevel@tonic-gate { 5880Sstevel@tonic-gate struct cb_ops *cb; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate cb = devopsp[getmajor(dev)]->devo_cb_ops; 5910Sstevel@tonic-gate return ((*cb->cb_chpoll)(dev, events, anyyet, reventsp, pollhdrp)); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* 5950Sstevel@tonic-gate * A 'size' property can be provided by a VCHR device. 5960Sstevel@tonic-gate * 5970Sstevel@tonic-gate * Since it's defined as zero for STREAMS devices, so we avoid the 5980Sstevel@tonic-gate * overhead of looking it up. Note also that we don't force an 5990Sstevel@tonic-gate * unused driver into memory simply to ask about it's size. We also 6000Sstevel@tonic-gate * don't bother to ask it its size unless it's already been attached 6010Sstevel@tonic-gate * (the attach routine is the earliest place the property will be created) 6020Sstevel@tonic-gate * 6030Sstevel@tonic-gate * XXX In an ideal world, we'd call this at VOP_GETATTR() time. 6040Sstevel@tonic-gate */ 6050Sstevel@tonic-gate int 6060Sstevel@tonic-gate cdev_size(dev_t dev) 6070Sstevel@tonic-gate { 6080Sstevel@tonic-gate major_t maj; 6090Sstevel@tonic-gate struct devnames *dnp; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if ((maj = getmajor(dev)) >= devcnt) 6120Sstevel@tonic-gate return (0); 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate dnp = &(devnamesp[maj]); 6150Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 6160Sstevel@tonic-gate if (devopsp[maj] && devopsp[maj]->devo_cb_ops && 6170Sstevel@tonic-gate !devopsp[maj]->devo_cb_ops->cb_str) { 6180Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6190Sstevel@tonic-gate return (e_ddi_getprop(dev, VCHR, "size", 6200Sstevel@tonic-gate DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0)); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6230Sstevel@tonic-gate return (0); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate /* 6270Sstevel@tonic-gate * same for 64-bit Size property 6280Sstevel@tonic-gate */ 6290Sstevel@tonic-gate uint64_t 6300Sstevel@tonic-gate cdev_Size(dev_t dev) 6310Sstevel@tonic-gate { 6320Sstevel@tonic-gate major_t maj; 6330Sstevel@tonic-gate struct devnames *dnp; 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate if ((maj = getmajor(dev)) >= devcnt) 6360Sstevel@tonic-gate return (0); 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate dnp = &(devnamesp[maj]); 6390Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 6400Sstevel@tonic-gate if (devopsp[maj] && devopsp[maj]->devo_cb_ops && 6410Sstevel@tonic-gate !devopsp[maj]->devo_cb_ops->cb_str) { 6420Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6430Sstevel@tonic-gate return (e_ddi_getprop_int64(dev, VCHR, "Size", 6440Sstevel@tonic-gate DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0)); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6470Sstevel@tonic-gate return (0); 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate /* 6510Sstevel@tonic-gate * XXX This routine is poorly named, because block devices can and do 6520Sstevel@tonic-gate * have properties (see bdev_size() above). 6530Sstevel@tonic-gate * 6540Sstevel@tonic-gate * XXX fix the comment in devops.h that claims that cb_prop_op 6550Sstevel@tonic-gate * is character-only. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate int 6580Sstevel@tonic-gate cdev_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags, 6590Sstevel@tonic-gate char *name, caddr_t valuep, int *lengthp) 6600Sstevel@tonic-gate { 6610Sstevel@tonic-gate struct cb_ops *cb; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate if ((cb = devopsp[DEVI(dip)->devi_major]->devo_cb_ops) == NULL) 6640Sstevel@tonic-gate return (DDI_PROP_NOT_FOUND); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate return ((*cb->cb_prop_op)(dev, dip, prop_op, mod_flags, 6670Sstevel@tonic-gate name, valuep, lengthp)); 6680Sstevel@tonic-gate } 669