xref: /onnv-gate/usr/src/uts/common/io/bl.c (revision 7656:2621e50fdf4a)
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
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * 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*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Blacklist special file
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/open.h>
390Sstevel@tonic-gate #include <sys/policy.h>
400Sstevel@tonic-gate #include <sys/fm/protocol.h>
410Sstevel@tonic-gate #include <sys/bl.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static dev_info_t *bl_dip;	/* private copy of devinfo pointer */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static int
bl_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)460Sstevel@tonic-gate bl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	switch (cmd) {
490Sstevel@tonic-gate 	case DDI_ATTACH:
500Sstevel@tonic-gate 		break;
510Sstevel@tonic-gate 	case DDI_RESUME:
520Sstevel@tonic-gate 		return (DDI_SUCCESS);
530Sstevel@tonic-gate 	default:
540Sstevel@tonic-gate 		return (DDI_FAILURE);
550Sstevel@tonic-gate 	}
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, ddi_get_name(dip), S_IFCHR,
580Sstevel@tonic-gate 	    ddi_get_instance(dip), DDI_PSEUDO, 0) != DDI_SUCCESS)
590Sstevel@tonic-gate 		return (DDI_FAILURE);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	bl_dip = dip;
620Sstevel@tonic-gate 	return (DDI_SUCCESS);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*ARGSUSED*/
660Sstevel@tonic-gate static int
bl_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)670Sstevel@tonic-gate bl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	switch (cmd) {
700Sstevel@tonic-gate 	case DDI_DETACH:
710Sstevel@tonic-gate 		break;
720Sstevel@tonic-gate 	case DDI_SUSPEND:
730Sstevel@tonic-gate 		return (DDI_SUCCESS);
740Sstevel@tonic-gate 	default:
750Sstevel@tonic-gate 		return (DDI_FAILURE);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	ddi_remove_minor_node(bl_dip, NULL);
790Sstevel@tonic-gate 	return (DDI_SUCCESS);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*ARGSUSED*/
830Sstevel@tonic-gate static int
bl_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)840Sstevel@tonic-gate bl_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	int rc = DDI_SUCCESS;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	switch (infocmd) {
890Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
900Sstevel@tonic-gate 		*result = bl_dip;
910Sstevel@tonic-gate 		break;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
940Sstevel@tonic-gate 		*result = (void *)(uintptr_t)getminor((dev_t)arg);
950Sstevel@tonic-gate 		break;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	default:
980Sstevel@tonic-gate 		*result = NULL;
990Sstevel@tonic-gate 		rc = DDI_FAILURE;
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	return (rc);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*ARGSUSED*/
1060Sstevel@tonic-gate static int
bl_open(dev_t * devp,int flag,int otyp,struct cred * credp)1070Sstevel@tonic-gate bl_open(dev_t *devp, int flag, int otyp, struct cred *credp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
1100Sstevel@tonic-gate 		return (EINVAL);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (secpolicy_blacklist(credp) != 0)
1130Sstevel@tonic-gate 		return (EPERM);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (0);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*ARGSUSED*/
1190Sstevel@tonic-gate static int
bl_ioctl(dev_t dev,int cmd,intptr_t data,int flag,cred_t * cred,int * rvalp)1200Sstevel@tonic-gate bl_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	bl_req_t blr;
1230Sstevel@tonic-gate 	nvlist_t *fmri;
1240Sstevel@tonic-gate 	const char *scheme;
1250Sstevel@tonic-gate 	char class[128];
1260Sstevel@tonic-gate 	char *buf;
1270Sstevel@tonic-gate 	int err;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #ifdef _SYSCALL32
1300Sstevel@tonic-gate 	if (get_udatamodel() != DATAMODEL_NATIVE) {
1310Sstevel@tonic-gate 		bl_req32_t blr32;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		if (copyin((void *)data, &blr32, sizeof (bl_req32_t)) != 0)
1340Sstevel@tonic-gate 			return (EFAULT);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 		blr.bl_fmri = (caddr_t)(uintptr_t)blr32.bl_fmri;
1370Sstevel@tonic-gate 		blr.bl_fmrisz = blr32.bl_fmrisz;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		blr.bl_class = (caddr_t)(uintptr_t)blr32.bl_class;
1400Sstevel@tonic-gate 	} else
1410Sstevel@tonic-gate #endif
1420Sstevel@tonic-gate 	{
1430Sstevel@tonic-gate 		if (copyin((void *)data, &blr, sizeof (bl_req_t)) != 0)
1440Sstevel@tonic-gate 			return (EFAULT);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (blr.bl_fmri == NULL || blr.bl_fmrisz > BL_FMRI_MAX_BUFSIZE ||
1480Sstevel@tonic-gate 	    blr.bl_class == NULL)
1490Sstevel@tonic-gate 		return (EINVAL);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (copyinstr(blr.bl_class, class, sizeof (class), NULL) != 0)
1520Sstevel@tonic-gate 		return (EFAULT);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	buf = kmem_zalloc(blr.bl_fmrisz, KM_SLEEP);
1550Sstevel@tonic-gate 	if (copyin(blr.bl_fmri, buf, blr.bl_fmrisz) != 0) {
1560Sstevel@tonic-gate 		kmem_free(buf, blr.bl_fmrisz);
1570Sstevel@tonic-gate 		return (EFAULT);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	err = nvlist_unpack(buf, blr.bl_fmrisz, &fmri, KM_SLEEP);
1610Sstevel@tonic-gate 	kmem_free(buf, blr.bl_fmrisz);
1620Sstevel@tonic-gate 	if (err != 0)
1630Sstevel@tonic-gate 		return (err);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (nvlist_lookup_string(fmri, FM_FMRI_SCHEME, (char **)&scheme) != 0) {
1660Sstevel@tonic-gate 		nvlist_free(fmri);
1670Sstevel@tonic-gate 		return (EINVAL);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	switch (cmd) {
1710Sstevel@tonic-gate 	case BLIOC_INSERT:
1720Sstevel@tonic-gate 	case BLIOC_DELETE:
1730Sstevel@tonic-gate 		err = blacklist(cmd, scheme, fmri, class);
1740Sstevel@tonic-gate 		break;
1750Sstevel@tonic-gate 	default:
1760Sstevel@tonic-gate 		err = ENOTSUP;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	nvlist_free(fmri);
1800Sstevel@tonic-gate 	return (err);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static struct cb_ops bl_cb_ops = {
1850Sstevel@tonic-gate 	bl_open,		/* open */
1860Sstevel@tonic-gate 	nulldev,		/* close */
1870Sstevel@tonic-gate 	nodev,			/* strategy */
1880Sstevel@tonic-gate 	nodev,			/* print */
1890Sstevel@tonic-gate 	nodev,			/* dump */
1900Sstevel@tonic-gate 	nodev,			/* read */
1910Sstevel@tonic-gate 	nodev,			/* write */
1920Sstevel@tonic-gate 	bl_ioctl,		/* ioctl */
1930Sstevel@tonic-gate 	nodev,			/* devmap */
1940Sstevel@tonic-gate 	nodev,			/* mmap */
1950Sstevel@tonic-gate 	nodev,			/* segmap */
1960Sstevel@tonic-gate 	nochpoll,		/* poll */
1970Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1980Sstevel@tonic-gate 	0,			/* streamtab  */
1990Sstevel@tonic-gate 	D_NEW | D_MP | D_64BIT	/* Driver compatibility flag */
2000Sstevel@tonic-gate };
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate static struct dev_ops bl_ops = {
2030Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2040Sstevel@tonic-gate 	0,			/* devo_refcnt  */
2050Sstevel@tonic-gate 	bl_getinfo,		/* devo_getinfo */
2060Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2070Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2080Sstevel@tonic-gate 	bl_attach,		/* devo_attach */
2090Sstevel@tonic-gate 	bl_detach,		/* devo_detach */
2100Sstevel@tonic-gate 	nodev,			/* devo_reset */
2110Sstevel@tonic-gate 	&bl_cb_ops,		/* devo_cb_ops */
2120Sstevel@tonic-gate 	(struct bus_ops *)0,	/* devo_bus_ops */
213*7656SSherry.Moore@Sun.COM 	NULL,			/* devo_power */
214*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* devo_quiesce */
2150Sstevel@tonic-gate };
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static struct modldrv modldrv = {
218*7656SSherry.Moore@Sun.COM 	&mod_driverops, "blacklist driver", &bl_ops,
2190Sstevel@tonic-gate };
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static struct modlinkage modlinkage = {
2220Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
2230Sstevel@tonic-gate };
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate int
_init(void)2260Sstevel@tonic-gate _init(void)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2320Sstevel@tonic-gate _info(struct modinfo *modinfop)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate int
_fini(void)2380Sstevel@tonic-gate _fini(void)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2410Sstevel@tonic-gate }
242