xref: /illumos-gate/usr/src/uts/common/io/bl.c (revision 19397407)
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 /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Blacklist special file
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/open.h>
397c478bd9Sstevel@tonic-gate #include <sys/policy.h>
407c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
417c478bd9Sstevel@tonic-gate #include <sys/bl.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static dev_info_t *bl_dip;	/* private copy of devinfo pointer */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static int
bl_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)467c478bd9Sstevel@tonic-gate bl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	switch (cmd) {
497c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
507c478bd9Sstevel@tonic-gate 		break;
517c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
527c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
537c478bd9Sstevel@tonic-gate 	default:
547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
557c478bd9Sstevel@tonic-gate 	}
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, ddi_get_name(dip), S_IFCHR,
587c478bd9Sstevel@tonic-gate 	    ddi_get_instance(dip), DDI_PSEUDO, 0) != DDI_SUCCESS)
597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	bl_dip = dip;
627c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
667c478bd9Sstevel@tonic-gate static int
bl_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)677c478bd9Sstevel@tonic-gate bl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	switch (cmd) {
707c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
717c478bd9Sstevel@tonic-gate 		break;
727c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
737c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
747c478bd9Sstevel@tonic-gate 	default:
757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(bl_dip, NULL);
797c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
837c478bd9Sstevel@tonic-gate static int
bl_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)847c478bd9Sstevel@tonic-gate bl_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	int rc = DDI_SUCCESS;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	switch (infocmd) {
897c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
907c478bd9Sstevel@tonic-gate 		*result = bl_dip;
917c478bd9Sstevel@tonic-gate 		break;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
947c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)getminor((dev_t)arg);
957c478bd9Sstevel@tonic-gate 		break;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	default:
987c478bd9Sstevel@tonic-gate 		*result = NULL;
997c478bd9Sstevel@tonic-gate 		rc = DDI_FAILURE;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	return (rc);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1067c478bd9Sstevel@tonic-gate static int
bl_open(dev_t * devp,int flag,int otyp,struct cred * credp)1077c478bd9Sstevel@tonic-gate bl_open(dev_t *devp, int flag, int otyp, struct cred *credp)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
1107c478bd9Sstevel@tonic-gate 		return (EINVAL);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (secpolicy_blacklist(credp) != 0)
1137c478bd9Sstevel@tonic-gate 		return (EPERM);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	return (0);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1197c478bd9Sstevel@tonic-gate static int
bl_ioctl(dev_t dev,int cmd,intptr_t data,int flag,cred_t * cred,int * rvalp)1207c478bd9Sstevel@tonic-gate bl_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	bl_req_t blr;
1237c478bd9Sstevel@tonic-gate 	nvlist_t *fmri;
1247c478bd9Sstevel@tonic-gate 	const char *scheme;
1257c478bd9Sstevel@tonic-gate 	char class[128];
1267c478bd9Sstevel@tonic-gate 	char *buf;
1277c478bd9Sstevel@tonic-gate 	int err;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32
1307c478bd9Sstevel@tonic-gate 	if (get_udatamodel() != DATAMODEL_NATIVE) {
1317c478bd9Sstevel@tonic-gate 		bl_req32_t blr32;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		if (copyin((void *)data, &blr32, sizeof (bl_req32_t)) != 0)
1347c478bd9Sstevel@tonic-gate 			return (EFAULT);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		blr.bl_fmri = (caddr_t)(uintptr_t)blr32.bl_fmri;
1377c478bd9Sstevel@tonic-gate 		blr.bl_fmrisz = blr32.bl_fmrisz;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		blr.bl_class = (caddr_t)(uintptr_t)blr32.bl_class;
1407c478bd9Sstevel@tonic-gate 	} else
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 	{
1437c478bd9Sstevel@tonic-gate 		if (copyin((void *)data, &blr, sizeof (bl_req_t)) != 0)
1447c478bd9Sstevel@tonic-gate 			return (EFAULT);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (blr.bl_fmri == NULL || blr.bl_fmrisz > BL_FMRI_MAX_BUFSIZE ||
1487c478bd9Sstevel@tonic-gate 	    blr.bl_class == NULL)
1497c478bd9Sstevel@tonic-gate 		return (EINVAL);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (copyinstr(blr.bl_class, class, sizeof (class), NULL) != 0)
1527c478bd9Sstevel@tonic-gate 		return (EFAULT);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	buf = kmem_zalloc(blr.bl_fmrisz, KM_SLEEP);
1557c478bd9Sstevel@tonic-gate 	if (copyin(blr.bl_fmri, buf, blr.bl_fmrisz) != 0) {
1567c478bd9Sstevel@tonic-gate 		kmem_free(buf, blr.bl_fmrisz);
1577c478bd9Sstevel@tonic-gate 		return (EFAULT);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	err = nvlist_unpack(buf, blr.bl_fmrisz, &fmri, KM_SLEEP);
1617c478bd9Sstevel@tonic-gate 	kmem_free(buf, blr.bl_fmrisz);
1627c478bd9Sstevel@tonic-gate 	if (err != 0)
1637c478bd9Sstevel@tonic-gate 		return (err);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(fmri, FM_FMRI_SCHEME, (char **)&scheme) != 0) {
1667c478bd9Sstevel@tonic-gate 		nvlist_free(fmri);
1677c478bd9Sstevel@tonic-gate 		return (EINVAL);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	switch (cmd) {
1717c478bd9Sstevel@tonic-gate 	case BLIOC_INSERT:
1727c478bd9Sstevel@tonic-gate 	case BLIOC_DELETE:
1737c478bd9Sstevel@tonic-gate 		err = blacklist(cmd, scheme, fmri, class);
1747c478bd9Sstevel@tonic-gate 		break;
1757c478bd9Sstevel@tonic-gate 	default:
1767c478bd9Sstevel@tonic-gate 		err = ENOTSUP;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	nvlist_free(fmri);
1807c478bd9Sstevel@tonic-gate 	return (err);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static struct cb_ops bl_cb_ops = {
1857c478bd9Sstevel@tonic-gate 	bl_open,		/* open */
1867c478bd9Sstevel@tonic-gate 	nulldev,		/* close */
1877c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
1887c478bd9Sstevel@tonic-gate 	nodev,			/* print */
1897c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
1907c478bd9Sstevel@tonic-gate 	nodev,			/* read */
1917c478bd9Sstevel@tonic-gate 	nodev,			/* write */
1927c478bd9Sstevel@tonic-gate 	bl_ioctl,		/* ioctl */
1937c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
1947c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
1957c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
1967c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
1977c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1987c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
1997c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_64BIT	/* Driver compatibility flag */
2007c478bd9Sstevel@tonic-gate };
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static struct dev_ops bl_ops = {
2037c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2047c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt  */
2057c478bd9Sstevel@tonic-gate 	bl_getinfo,		/* devo_getinfo */
2067c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2077c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2087c478bd9Sstevel@tonic-gate 	bl_attach,		/* devo_attach */
2097c478bd9Sstevel@tonic-gate 	bl_detach,		/* devo_detach */
2107c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
2117c478bd9Sstevel@tonic-gate 	&bl_cb_ops,		/* devo_cb_ops */
2127c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0,	/* devo_bus_ops */
213*19397407SSherry Moore 	NULL,			/* devo_power */
214*19397407SSherry Moore 	ddi_quiesce_not_needed,		/* devo_quiesce */
2157c478bd9Sstevel@tonic-gate };
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
218*19397407SSherry Moore 	&mod_driverops, "blacklist driver", &bl_ops,
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2227c478bd9Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate int
_init(void)2267c478bd9Sstevel@tonic-gate _init(void)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2327c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate int
_fini(void)2387c478bd9Sstevel@tonic-gate _fini(void)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2417c478bd9Sstevel@tonic-gate }
242