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