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