17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*19397407SSherry Moore * Common Development and Distribution License (the "License").
6*19397407SSherry Moore * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
227c478bd9Sstevel@tonic-gate /* All Rights Reserved */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
26*19397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
277c478bd9Sstevel@tonic-gate * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * Clone Driver.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
427c478bd9Sstevel@tonic-gate #include <sys/user.h>
437c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
447c478bd9Sstevel@tonic-gate #include <sys/stream.h>
457c478bd9Sstevel@tonic-gate #include <sys/errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
477c478bd9Sstevel@tonic-gate #include <sys/systm.h>
487c478bd9Sstevel@tonic-gate #include <sys/conf.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/cred.h>
517c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
527c478bd9Sstevel@tonic-gate #include <sys/open.h>
537c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
547c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
557c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
567c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
577c478bd9Sstevel@tonic-gate #include <sys/policy.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate int clnopen(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp);
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static struct module_info clnm_info = { 0, "CLONE", 0, 0, 0, 0 };
627c478bd9Sstevel@tonic-gate static struct qinit clnrinit = { NULL, NULL, clnopen, NULL, NULL, &clnm_info,
637c478bd9Sstevel@tonic-gate NULL };
647c478bd9Sstevel@tonic-gate static struct qinit clnwinit = { NULL, NULL, NULL, NULL, NULL, &clnm_info,
657c478bd9Sstevel@tonic-gate NULL };
667c478bd9Sstevel@tonic-gate struct streamtab clninfo = { &clnrinit, &clnwinit };
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static int cln_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
697c478bd9Sstevel@tonic-gate static int cln_attach(dev_info_t *, ddi_attach_cmd_t);
707c478bd9Sstevel@tonic-gate static dev_info_t *cln_dip; /* private copy of devinfo pointer */
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate #define CLONE_CONF_FLAG (D_NEW|D_MP)
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(clone_ops, nulldev, nulldev, cln_attach, nodev, nodev, \
75*19397407SSherry Moore cln_info, CLONE_CONF_FLAG, &clninfo, ddi_quiesce_not_needed);
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
827c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a pseudo driver */
837c478bd9Sstevel@tonic-gate "Clone Pseudodriver 'clone'",
847c478bd9Sstevel@tonic-gate &clone_ops, /* driver ops */
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
887c478bd9Sstevel@tonic-gate MODREV_1,
897c478bd9Sstevel@tonic-gate (void *)&modldrv,
907c478bd9Sstevel@tonic-gate NULL
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate int
_init(void)957c478bd9Sstevel@tonic-gate _init(void)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate int
_fini(void)1017c478bd9Sstevel@tonic-gate _fini(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * Since the clone driver's reference count is unreliable,
1057c478bd9Sstevel@tonic-gate * make sure we are never unloaded.
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate return (EBUSY);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1117c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* ARGSUSED */
1177c478bd9Sstevel@tonic-gate static int
cln_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1187c478bd9Sstevel@tonic-gate cln_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate cln_dip = devi;
1217c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /* ARGSUSED */
1257c478bd9Sstevel@tonic-gate static int
cln_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1267c478bd9Sstevel@tonic-gate cln_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1277c478bd9Sstevel@tonic-gate void **result)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate int error;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate switch (infocmd) {
1327c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
1337c478bd9Sstevel@tonic-gate if (cln_dip == NULL) {
1347c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
1357c478bd9Sstevel@tonic-gate } else {
1367c478bd9Sstevel@tonic-gate *result = (void *)cln_dip;
1377c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate break;
1407c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
1417c478bd9Sstevel@tonic-gate *result = (void *)0;
1427c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
1437c478bd9Sstevel@tonic-gate break;
1447c478bd9Sstevel@tonic-gate default:
1457c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate return (error);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * Clone open. Maj is the major device number of the streams
1527c478bd9Sstevel@tonic-gate * device to open. Look up the device in the cdevsw[]. Attach
1537c478bd9Sstevel@tonic-gate * its qinit structures to the read and write queues and call its
1547c478bd9Sstevel@tonic-gate * open with the sflag set to CLONEOPEN. Swap in a new vnode with
1557c478bd9Sstevel@tonic-gate * the real device number constructed from either
1567c478bd9Sstevel@tonic-gate * a) for old-style drivers:
1577c478bd9Sstevel@tonic-gate * maj and the minor returned by the device open, or
1587c478bd9Sstevel@tonic-gate * b) for new-style drivers:
1597c478bd9Sstevel@tonic-gate * the whole dev passed back as a reference parameter
1607c478bd9Sstevel@tonic-gate * from the device open.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate int
clnopen(queue_t * rq,dev_t * devp,int flag,int sflag,cred_t * crp)1637c478bd9Sstevel@tonic-gate clnopen(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate struct streamtab *str;
1667c478bd9Sstevel@tonic-gate dev_t newdev;
1677c478bd9Sstevel@tonic-gate int error = 0;
1687c478bd9Sstevel@tonic-gate major_t maj;
1697c478bd9Sstevel@tonic-gate minor_t emaj;
1707c478bd9Sstevel@tonic-gate struct qinit *rinit, *winit;
1717c478bd9Sstevel@tonic-gate cdevsw_impl_t *dp;
1727c478bd9Sstevel@tonic-gate uint32_t qflag;
1737c478bd9Sstevel@tonic-gate uint32_t sqtype;
1747c478bd9Sstevel@tonic-gate perdm_t *dmp;
1757c478bd9Sstevel@tonic-gate vnode_t *vp;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if (sflag)
1787c478bd9Sstevel@tonic-gate return (ENXIO);
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate * Get the device to open.
1827c478bd9Sstevel@tonic-gate */
1837c478bd9Sstevel@tonic-gate emaj = getminor(*devp); /* minor is major for a cloned driver */
1847c478bd9Sstevel@tonic-gate maj = etoimajor(emaj); /* get internal major of cloned driver */
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (maj >= devcnt)
1877c478bd9Sstevel@tonic-gate return (ENXIO);
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * NOTE We call ddi_hold_installed_driver() here to attach
1917c478bd9Sstevel@tonic-gate * all instances of the driver, since we do not know
1927c478bd9Sstevel@tonic-gate * a priori which instance the Stream is associated with.
1937c478bd9Sstevel@tonic-gate *
1947c478bd9Sstevel@tonic-gate * For Style-2 network drivers, we know that the association
1957c478bd9Sstevel@tonic-gate * happens at DL_ATTACH time. For other types of drivers,
1967c478bd9Sstevel@tonic-gate * open probably requires attaching instance 0 (pseudo dip).
1977c478bd9Sstevel@tonic-gate *
1987c478bd9Sstevel@tonic-gate * To eliminate ddi_hold_installed_driver(), the following
1997c478bd9Sstevel@tonic-gate * should happen:
2007c478bd9Sstevel@tonic-gate *
2017c478bd9Sstevel@tonic-gate * - GLD be modified to include gld_init(). The driver will
2027c478bd9Sstevel@tonic-gate * register information for gld_open() to succeed. It will
2037c478bd9Sstevel@tonic-gate * also inform framework if driver assigns instance=PPA.
2047c478bd9Sstevel@tonic-gate * - ddi_hold_devi_by_instance() be modified to actively
2057c478bd9Sstevel@tonic-gate * attach the instance via top-down enumeration.
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate if (ddi_hold_installed_driver(maj) == NULL)
2087c478bd9Sstevel@tonic-gate return (ENXIO);
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if ((str = STREAMSTAB(maj)) == NULL) {
2117c478bd9Sstevel@tonic-gate ddi_rele_driver(maj);
2127c478bd9Sstevel@tonic-gate return (ENXIO);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate newdev = makedevice(emaj, 0); /* create new style device number */
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * Check for security here. For DLPI style 2 network
2197c478bd9Sstevel@tonic-gate * drivers, we need to apply the default network policy.
2207c478bd9Sstevel@tonic-gate * Clone is weird in that the network driver isn't loaded
2217c478bd9Sstevel@tonic-gate * and attached at spec_open() time, we need to load the
2227c478bd9Sstevel@tonic-gate * driver to see if it is a network driver. Hence, we
2237c478bd9Sstevel@tonic-gate * check security here (after ddi_hold_installed_driver
2247c478bd9Sstevel@tonic-gate * call above).
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate vp = makespecvp(newdev, VCHR);
2277c478bd9Sstevel@tonic-gate error = secpolicy_spec_open(crp, vp, flag);
2287c478bd9Sstevel@tonic-gate VN_RELE(vp);
2297c478bd9Sstevel@tonic-gate if (error) {
2307c478bd9Sstevel@tonic-gate ddi_rele_driver(maj);
2317c478bd9Sstevel@tonic-gate return (error);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate * Save so that we can restore the q on failure.
2367c478bd9Sstevel@tonic-gate */
2377c478bd9Sstevel@tonic-gate rinit = rq->q_qinfo;
2387c478bd9Sstevel@tonic-gate winit = WR(rq)->q_qinfo;
2397c478bd9Sstevel@tonic-gate ASSERT(rq->q_syncq->sq_type == (SQ_CI|SQ_CO));
2407c478bd9Sstevel@tonic-gate ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate dp = &devimpl[maj];
2437c478bd9Sstevel@tonic-gate ASSERT(str == dp->d_str);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate qflag = dp->d_qflag;
2467c478bd9Sstevel@tonic-gate sqtype = dp->d_sqtype;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /* create perdm_t if needed */
2497c478bd9Sstevel@tonic-gate if (NEED_DM(dp->d_dmp, qflag))
2507c478bd9Sstevel@tonic-gate dp->d_dmp = hold_dm(str, qflag, sqtype);
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate dmp = dp->d_dmp;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * Set the syncq state what qattach started off with. This is safe
2567c478bd9Sstevel@tonic-gate * since no other thread can access this queue at this point
2577c478bd9Sstevel@tonic-gate * (stream open, close, push, and pop are single threaded
2587c478bd9Sstevel@tonic-gate * by the framework.)
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate leavesq(rq->q_syncq, SQ_OPENCLOSE);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * Substitute the real qinit values for the current ones.
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate /* setq might sleep in kmem_alloc - avoid holding locks. */
2667c478bd9Sstevel@tonic-gate setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE);
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate * Open the attached module or driver.
2707c478bd9Sstevel@tonic-gate *
2717c478bd9Sstevel@tonic-gate * If there is an outer perimeter get exclusive access during
2727c478bd9Sstevel@tonic-gate * the open procedure.
2737c478bd9Sstevel@tonic-gate * Bump up the reference count on the queue.
2747c478bd9Sstevel@tonic-gate */
2757c478bd9Sstevel@tonic-gate entersq(rq->q_syncq, SQ_OPENCLOSE);
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate * Call the device open with the stream flag CLONEOPEN. The device
2797c478bd9Sstevel@tonic-gate * will either fail this or return the device number.
2807c478bd9Sstevel@tonic-gate */
2817c478bd9Sstevel@tonic-gate error = (*rq->q_qinfo->qi_qopen)(rq, &newdev, flag, CLONEOPEN, crp);
2827c478bd9Sstevel@tonic-gate if (error != 0)
2837c478bd9Sstevel@tonic-gate goto failed;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate *devp = newdev;
2867c478bd9Sstevel@tonic-gate if (getmajor(newdev) != emaj)
2877c478bd9Sstevel@tonic-gate goto bad_major;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate return (0);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate bad_major:
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate * Close the device
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate (*rq->q_qinfo->qi_qclose)(rq, flag, crp);
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate #ifdef DEBUG
2987c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "cannot clone major number %d(%s)->%d", emaj,
2997c478bd9Sstevel@tonic-gate ddi_major_to_name(emaj), getmajor(newdev));
3007c478bd9Sstevel@tonic-gate #endif
3017c478bd9Sstevel@tonic-gate error = ENXIO;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate failed:
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * open failed; pretty up to look like original
3067c478bd9Sstevel@tonic-gate * queue.
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate if (backq(WR(rq)) && backq(WR(rq))->q_next == WR(rq))
3097c478bd9Sstevel@tonic-gate qprocsoff(rq);
3107c478bd9Sstevel@tonic-gate leavesq(rq->q_syncq, SQ_OPENCLOSE);
3117c478bd9Sstevel@tonic-gate rq->q_next = WR(rq)->q_next = NULL;
3127c478bd9Sstevel@tonic-gate ASSERT(flush_syncq(rq->q_syncq, rq) == 0);
3137c478bd9Sstevel@tonic-gate ASSERT(flush_syncq(WR(rq)->q_syncq, WR(rq)) == 0);
3147c478bd9Sstevel@tonic-gate rq->q_ptr = WR(rq)->q_ptr = NULL;
3157c478bd9Sstevel@tonic-gate /* setq might sleep in kmem_alloc - avoid holding locks. */
3167c478bd9Sstevel@tonic-gate setq(rq, rinit, winit, NULL, QMTSAFE, SQ_CI|SQ_CO,
3177c478bd9Sstevel@tonic-gate B_FALSE);
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /* Restore back to what qattach will expect */
3207c478bd9Sstevel@tonic-gate entersq(rq->q_syncq, SQ_OPENCLOSE);
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate ddi_rele_driver(maj);
3237c478bd9Sstevel@tonic-gate return (error);
3247c478bd9Sstevel@tonic-gate }
325