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