7392 remove event channel support from lofi and implement lofi_devlink_cache.
[unleashed.git] / usr / src / uts / common / os / driver.c
blobfa992e5e45503bb65de672fedb0a7686fcb39ed1
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/t_lock.h>
28 #include <sys/param.h>
29 #include <sys/conf.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/buf.h>
33 #include <sys/cred.h>
34 #include <sys/user.h>
35 #include <sys/stat.h>
36 #include <sys/uio.h>
37 #include <sys/vnode.h>
38 #include <sys/fs/snode.h>
39 #include <sys/open.h>
40 #include <sys/kmem.h>
41 #include <sys/file.h>
42 #include <sys/debug.h>
43 #include <sys/tnf_probe.h>
45 /* Don't #include <sys/ddi.h> - it #undef's getmajor() */
47 #include <sys/sunddi.h>
48 #include <sys/sunndi.h>
49 #include <sys/sunpm.h>
50 #include <sys/ddi_impldefs.h>
51 #include <sys/ndi_impldefs.h>
52 #include <sys/esunddi.h>
53 #include <sys/autoconf.h>
54 #include <sys/modctl.h>
55 #include <sys/epm.h>
56 #include <sys/dacf.h>
57 #include <sys/sunmdi.h>
58 #include <sys/instance.h>
59 #include <sys/sdt.h>
61 static void i_attach_ctlop(dev_info_t *, ddi_attach_cmd_t, ddi_pre_post_t, int);
62 static void i_detach_ctlop(dev_info_t *, ddi_detach_cmd_t, ddi_pre_post_t, int);
64 /* decide what to do when a double dev_lclose is detected */
65 #ifdef DEBUG
66 int dev_lclose_ce = CE_PANIC;
67 #else /* DEBUG */
68 int dev_lclose_ce = CE_WARN;
69 #endif /* DEBUG */
72 * Configuration-related entry points for nexus and leaf drivers
74 int
75 devi_identify(dev_info_t *devi)
77 struct dev_ops *ops;
78 int (*fn)(dev_info_t *);
80 if ((ops = ddi_get_driver(devi)) == NULL ||
81 (fn = ops->devo_identify) == NULL)
82 return (-1);
84 return ((*fn)(devi));
87 int
88 devi_probe(dev_info_t *devi)
90 int rv, probe_failed;
91 pm_ppm_cookie_t ppm_cookie;
92 struct dev_ops *ops;
93 int (*fn)(dev_info_t *);
95 ops = ddi_get_driver(devi);
96 ASSERT(ops);
98 pm_pre_probe(devi, &ppm_cookie);
101 * probe(9E) in 2.0 implies that you can get
102 * away with not writing one of these .. so we
103 * pretend we're 'nulldev' if we don't find one (sigh).
105 if ((fn = ops->devo_probe) == NULL) {
106 if (ddi_dev_is_sid(devi) == DDI_SUCCESS)
107 rv = DDI_PROBE_DONTCARE;
108 else
109 rv = DDI_PROBE_FAILURE;
110 } else
111 rv = (*fn)(devi);
113 switch (rv) {
114 case DDI_PROBE_DONTCARE:
115 case DDI_PROBE_SUCCESS:
116 probe_failed = 0;
117 break;
118 default:
119 probe_failed = 1;
120 break;
122 pm_post_probe(&ppm_cookie, rv, probe_failed);
124 return (rv);
129 * devi_attach()
130 * attach a device instance to the system if the driver supplies an
131 * attach(9E) entrypoint.
134 devi_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
136 struct dev_ops *ops;
137 int error;
138 int (*fn)(dev_info_t *, ddi_attach_cmd_t);
139 pm_ppm_cookie_t pc;
141 if ((error = mdi_pre_attach(devi, cmd)) != DDI_SUCCESS) {
142 return (error);
145 pm_pre_attach(devi, &pc, cmd);
147 if ((cmd == DDI_RESUME || cmd == DDI_PM_RESUME) &&
148 e_ddi_parental_suspend_resume(devi)) {
149 error = e_ddi_resume(devi, cmd);
150 goto done;
152 ops = ddi_get_driver(devi);
153 ASSERT(ops);
154 if ((fn = ops->devo_attach) == NULL) {
155 error = DDI_FAILURE;
156 goto done;
160 * Call the driver's attach(9e) entrypoint
162 i_attach_ctlop(devi, cmd, DDI_PRE, 0);
163 error = (*fn)(devi, cmd);
164 i_attach_ctlop(devi, cmd, DDI_POST, error);
166 done:
167 pm_post_attach(&pc, error);
168 mdi_post_attach(devi, cmd, error);
170 return (error);
174 * devi_detach()
175 * detach a device instance from the system if the driver supplies a
176 * detach(9E) entrypoint.
179 devi_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
181 struct dev_ops *ops;
182 int error;
183 int (*fn)(dev_info_t *, ddi_detach_cmd_t);
184 pm_ppm_cookie_t pc;
186 ASSERT(cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND ||
187 cmd == DDI_DETACH);
189 if ((cmd == DDI_SUSPEND || cmd == DDI_PM_SUSPEND) &&
190 e_ddi_parental_suspend_resume(devi)) {
191 return (e_ddi_suspend(devi, cmd));
193 ops = ddi_get_driver(devi);
194 ASSERT(ops);
195 if ((fn = ops->devo_detach) == NULL)
196 return (DDI_FAILURE);
198 if ((error = mdi_pre_detach(devi, cmd)) != DDI_SUCCESS) {
199 return (error);
201 i_detach_ctlop(devi, cmd, DDI_PRE, 0);
202 pm_pre_detach(devi, cmd, &pc);
205 * Call the driver's detach routine
207 error = (*fn)(devi, cmd);
209 pm_post_detach(&pc, error);
210 i_detach_ctlop(devi, cmd, DDI_POST, error);
211 mdi_post_detach(devi, cmd, error);
213 return (error);
216 static void
217 i_attach_ctlop(dev_info_t *devi, ddi_attach_cmd_t cmd, ddi_pre_post_t w,
218 int ret)
220 int error;
221 struct attachspec as;
222 dev_info_t *pdip = ddi_get_parent(devi);
224 as.cmd = cmd;
225 as.when = w;
226 as.pdip = pdip;
227 as.result = ret;
228 (void) ddi_ctlops(devi, devi, DDI_CTLOPS_ATTACH, &as, &error);
231 static void
232 i_detach_ctlop(dev_info_t *devi, ddi_detach_cmd_t cmd, ddi_pre_post_t w,
233 int ret)
235 int error;
236 struct detachspec ds;
237 dev_info_t *pdip = ddi_get_parent(devi);
239 ds.cmd = cmd;
240 ds.when = w;
241 ds.pdip = pdip;
242 ds.result = ret;
243 (void) ddi_ctlops(devi, devi, DDI_CTLOPS_DETACH, &ds, &error);
247 * This entry point not defined by Solaris 2.0 DDI/DKI, so
248 * its inclusion here is somewhat moot.
251 devi_reset(dev_info_t *devi, ddi_reset_cmd_t cmd)
253 struct dev_ops *ops;
254 int (*fn)(dev_info_t *, ddi_reset_cmd_t);
256 if ((ops = ddi_get_driver(devi)) == NULL ||
257 (fn = ops->devo_reset) == NULL)
258 return (DDI_FAILURE);
260 return ((*fn)(devi, cmd));
264 devi_quiesce(dev_info_t *devi)
266 struct dev_ops *ops;
267 int (*fn)(dev_info_t *);
269 if (((ops = ddi_get_driver(devi)) == NULL) ||
270 (ops->devo_rev < 4) || ((fn = ops->devo_quiesce) == NULL))
271 return (DDI_FAILURE);
273 return ((*fn)(devi));
277 * Leaf driver entry points. The following [cb]dev_* functions are *not* part
278 * of the DDI, please use functions defined in <sys/sunldi.h> and driver_lyr.c.
281 dev_open(dev_t *devp, int flag, int type, struct cred *cred)
283 struct cb_ops *cb;
285 cb = devopsp[getmajor(*devp)]->devo_cb_ops;
286 return ((*cb->cb_open)(devp, flag, type, cred));
290 dev_close(dev_t dev, int flag, int type, struct cred *cred)
292 struct cb_ops *cb;
294 cb = (devopsp[getmajor(dev)])->devo_cb_ops;
295 return ((*cb->cb_close)(dev, flag, type, cred));
299 * New Leaf driver open entry point. We make a vnode and go through specfs
300 * in order to obtain open close exclusions guarantees. Note that we drop
301 * OTYP_LYR if it was specified - we are going through specfs and it provides
302 * last close semantics (FKLYR is provided to open(9E)). Also, since
303 * spec_open will drive attach via e_ddi_hold_devi_by_dev for a makespecvp
304 * vnode with no SDIP_SET on the common snode, the dev_lopen caller no longer
305 * needs to call ddi_hold_installed_driver.
308 dev_lopen(dev_t *devp, int flag, int otype, struct cred *cred)
310 struct vnode *vp;
311 int error;
312 struct vnode *cvp;
314 vp = makespecvp(*devp, (otype == OTYP_BLK) ? VBLK : VCHR);
315 error = VOP_OPEN(&vp, flag | FKLYR, cred, NULL);
316 if (error == 0) {
317 /* Pick up the (possibly) new dev_t value. */
318 *devp = vp->v_rdev;
321 * Place extra hold on the common vnode, which contains the
322 * open count, so that it is not destroyed by the VN_RELE of
323 * the shadow makespecvp vnode below.
325 cvp = STOV(VTOCS(vp));
326 VN_HOLD(cvp);
329 /* release the shadow makespecvp vnode. */
330 VN_RELE(vp);
331 return (error);
335 * Leaf driver close entry point. We make a vnode and go through specfs in
336 * order to obtain open close exclusions guarantees. Note that we drop
337 * OTYP_LYR if it was specified - we are going through specfs and it provides
338 * last close semantics (FLKYR is provided to close(9E)).
341 dev_lclose(dev_t dev, int flag, int otype, struct cred *cred)
343 struct vnode *vp;
344 int error;
345 struct vnode *cvp;
346 char *funcname;
347 ulong_t offset;
349 vp = makespecvp(dev, (otype == OTYP_BLK) ? VBLK : VCHR);
350 error = VOP_CLOSE(vp, flag | FKLYR, 1, (offset_t)0, cred, NULL);
353 * Release the extra dev_lopen hold on the common vnode. We inline a
354 * VN_RELE(cvp) call so that we can detect more dev_lclose calls than
355 * dev_lopen calls without panic. See vn_rele. If our inline of
356 * vn_rele called VOP_INACTIVE(cvp, CRED(), ...) we would panic on the
357 * "release the makespecvp vnode" VN_RELE(vp) that follows - so
358 * instead we diagnose this situation. Note that the driver has
359 * still seen a double close(9E), but that would have occurred with
360 * the old dev_close implementation too.
362 cvp = STOV(VTOCS(vp));
363 mutex_enter(&cvp->v_lock);
364 switch (cvp->v_count) {
365 default:
366 cvp->v_count--;
367 break;
369 case 0:
370 VTOS(vp)->s_commonvp = NULL; /* avoid panic */
371 /*FALLTHROUGH*/
372 case 1:
374 * The following message indicates a serious problem in the
375 * identified driver, the driver should be fixed. If obtaining
376 * a panic dump is needed to diagnose the driver problem then
377 * adding "set dev_lclose_ce=3" to /etc/system will cause a
378 * panic when this occurs.
380 funcname = modgetsymname((uintptr_t)caller(), &offset);
381 cmn_err(dev_lclose_ce, "dev_lclose: extra close of dev_t 0x%lx "
382 "from %s`%s()", dev, mod_containing_pc(caller()),
383 funcname ? funcname : "unknown...");
384 break;
386 mutex_exit(&cvp->v_lock);
388 /* release the makespecvp vnode. */
389 VN_RELE(vp);
390 return (error);
394 * Returns -1 or the instance number of the given dev_t as
395 * interpreted by the device driver. The code may load the driver
396 * but it does not attach any instances.
398 * Instance is supposed to be a int but drivers have assumed that
399 * the pointer was a pointer to "void *" instead of a pointer to
400 * "int *" so we now explicitly pass a pointer to "void *" and then
401 * cast the result to an int when returning the value.
404 dev_to_instance(dev_t dev)
406 major_t major = getmajor(dev);
407 struct dev_ops *ops;
408 void *vinstance;
409 int error;
411 /* verify that the driver is loaded */
412 if ((ops = mod_hold_dev_by_major(major)) == NULL)
413 return (-1);
414 ASSERT(CB_DRV_INSTALLED(ops));
416 /* verify that it supports the getinfo(9E) entry point */
417 if (ops->devo_getinfo == NULL) {
418 mod_rele_dev_by_major(major);
419 return (-1);
422 /* ask the driver to extract the instance number from the devt */
423 error = (*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2INSTANCE,
424 (void *)dev, &vinstance);
426 /* release the driver */
427 mod_rele_dev_by_major(major);
429 if (error != DDI_SUCCESS)
430 return (-1);
432 return ((int)(uintptr_t)vinstance);
435 static void
436 bdev_strategy_tnf_probe(struct buf *bp)
438 /* Kernel probe */
439 TNF_PROBE_5(strategy, "io blockio", /* CSTYLED */,
440 tnf_device, device, bp->b_edev,
441 tnf_diskaddr, block, bp->b_lblkno,
442 tnf_size, size, bp->b_bcount,
443 tnf_opaque, buf, bp,
444 tnf_bioflags, flags, bp->b_flags);
448 bdev_strategy(struct buf *bp)
450 struct dev_ops *ops;
452 ops = devopsp[getmajor(bp->b_edev)];
455 * Before we hit the io:::start probe, we need to fill in the b_dip
456 * field of the buf structure. This should be -- for the most part --
457 * incredibly cheap. If you're in this code looking to bum cycles,
458 * there is almost certainly bigger game further down the I/O path...
460 (void) ops->devo_getinfo(NULL, DDI_INFO_DEVT2DEVINFO,
461 (void *)bp->b_edev, (void **)&bp->b_dip);
463 DTRACE_IO1(start, struct buf *, bp);
464 bp->b_flags |= B_STARTED;
467 * Call the TNF probe here instead of the inline code
468 * to force our compiler to use the tail call optimization.
470 bdev_strategy_tnf_probe(bp);
472 return (ops->devo_cb_ops->cb_strategy(bp));
476 bdev_print(dev_t dev, caddr_t str)
478 struct cb_ops *cb;
480 cb = devopsp[getmajor(dev)]->devo_cb_ops;
481 return ((*cb->cb_print)(dev, str));
485 * Return number of DEV_BSIZE byte blocks.
488 bdev_size(dev_t dev)
490 uint_t nblocks;
491 uint_t blksize;
493 if ((nblocks = e_ddi_getprop(dev, VBLK, "nblocks",
494 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
495 return (-1);
497 /* Get blksize, default to DEV_BSIZE */
498 if ((blksize = e_ddi_getprop(dev, VBLK, "blksize",
499 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
500 blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize",
501 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE);
503 if (blksize >= DEV_BSIZE)
504 return (nblocks * (blksize / DEV_BSIZE));
505 else
506 return (nblocks / (DEV_BSIZE / blksize));
510 * Same for 64-bit Nblocks property
512 uint64_t
513 bdev_Size(dev_t dev)
515 uint64_t nblocks;
516 uint_t blksize;
518 if ((nblocks = e_ddi_getprop_int64(dev, VBLK, "Nblocks",
519 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
520 return (-1);
522 /* Get blksize, default to DEV_BSIZE */
523 if ((blksize = e_ddi_getprop(dev, VBLK, "blksize",
524 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, -1)) == -1)
525 blksize = e_ddi_getprop(DDI_DEV_T_ANY, VBLK, "device-blksize",
526 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, DEV_BSIZE);
528 if (blksize >= DEV_BSIZE)
529 return (nblocks * (blksize / DEV_BSIZE));
530 else
531 return (nblocks / (DEV_BSIZE / blksize));
535 bdev_dump(dev_t dev, caddr_t addr, daddr_t blkno, int blkcnt)
537 struct cb_ops *cb;
539 cb = devopsp[getmajor(dev)]->devo_cb_ops;
540 return ((*cb->cb_dump)(dev, addr, blkno, blkcnt));
544 cdev_read(dev_t dev, struct uio *uiop, struct cred *cred)
546 struct cb_ops *cb;
548 cb = devopsp[getmajor(dev)]->devo_cb_ops;
549 return ((*cb->cb_read)(dev, uiop, cred));
553 cdev_write(dev_t dev, struct uio *uiop, struct cred *cred)
555 struct cb_ops *cb;
557 cb = devopsp[getmajor(dev)]->devo_cb_ops;
558 return ((*cb->cb_write)(dev, uiop, cred));
562 cdev_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cred,
563 int *rvalp)
565 struct cb_ops *cb;
567 cb = devopsp[getmajor(dev)]->devo_cb_ops;
568 return ((*cb->cb_ioctl)(dev, cmd, arg, mode, cred, rvalp));
572 cdev_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
573 size_t *maplen, uint_t mode)
575 struct cb_ops *cb;
577 cb = devopsp[getmajor(dev)]->devo_cb_ops;
578 return ((*cb->cb_devmap)(dev, dhp, off, len, maplen, mode));
582 cdev_mmap(int (*mapfunc)(dev_t, off_t, int), dev_t dev, off_t off, int prot)
584 return ((*mapfunc)(dev, off, prot));
588 cdev_segmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
589 uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
591 struct cb_ops *cb;
593 cb = devopsp[getmajor(dev)]->devo_cb_ops;
594 return ((*cb->cb_segmap)(dev, off, as, addrp,
595 len, prot, maxprot, flags, credp));
599 cdev_poll(dev_t dev, short events, int anyyet, short *reventsp,
600 struct pollhead **pollhdrp)
602 struct cb_ops *cb;
604 cb = devopsp[getmajor(dev)]->devo_cb_ops;
605 return ((*cb->cb_chpoll)(dev, events, anyyet, reventsp, pollhdrp));
609 * A 'size' property can be provided by a VCHR device.
611 * Since it's defined as zero for STREAMS devices, so we avoid the
612 * overhead of looking it up. Note also that we don't force an
613 * unused driver into memory simply to ask about it's size. We also
614 * don't bother to ask it its size unless it's already been attached
615 * (the attach routine is the earliest place the property will be created)
617 * XXX In an ideal world, we'd call this at VOP_GETATTR() time.
620 cdev_size(dev_t dev)
622 major_t maj;
623 struct devnames *dnp;
625 if ((maj = getmajor(dev)) >= devcnt)
626 return (0);
628 dnp = &(devnamesp[maj]);
629 LOCK_DEV_OPS(&dnp->dn_lock);
630 if (devopsp[maj] && devopsp[maj]->devo_cb_ops &&
631 !devopsp[maj]->devo_cb_ops->cb_str) {
632 UNLOCK_DEV_OPS(&dnp->dn_lock);
633 return (e_ddi_getprop(dev, VCHR, "size",
634 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0));
636 UNLOCK_DEV_OPS(&dnp->dn_lock);
637 return (0);
641 * same for 64-bit Size property
643 uint64_t
644 cdev_Size(dev_t dev)
646 major_t maj;
647 struct devnames *dnp;
649 if ((maj = getmajor(dev)) >= devcnt)
650 return (0);
652 dnp = &(devnamesp[maj]);
653 LOCK_DEV_OPS(&dnp->dn_lock);
654 if (devopsp[maj] && devopsp[maj]->devo_cb_ops &&
655 !devopsp[maj]->devo_cb_ops->cb_str) {
656 UNLOCK_DEV_OPS(&dnp->dn_lock);
657 return (e_ddi_getprop_int64(dev, VCHR, "Size",
658 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 0));
660 UNLOCK_DEV_OPS(&dnp->dn_lock);
661 return (0);
665 * XXX This routine is poorly named, because block devices can and do
666 * have properties (see bdev_size() above).
668 * XXX fix the comment in devops.h that claims that cb_prop_op
669 * is character-only.
672 cdev_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
673 char *name, caddr_t valuep, int *lengthp)
675 struct cb_ops *cb;
677 if ((cb = devopsp[DEVI(dip)->devi_major]->devo_cb_ops) == NULL)
678 return (DDI_PROP_NOT_FOUND);
680 return ((*cb->cb_prop_op)(dev, dip, prop_op, mod_flags,
681 name, valuep, lengthp));