xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_devctl.c (revision 117:1d120397ca7c)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * PCI nexus HotPlug devctl interface
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/async.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/sunndi.h>
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
410Sstevel@tonic-gate #include <sys/pci_tools.h>
420Sstevel@tonic-gate #include <sys/pci_tools_var.h>
430Sstevel@tonic-gate #include <sys/open.h>
440Sstevel@tonic-gate #include <sys/errno.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <sys/policy.h>
470Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*LINTLIBRARY*/
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static int pci_open(dev_t *devp, int flags, int otyp, cred_t *credp);
520Sstevel@tonic-gate static int pci_close(dev_t dev, int flags, int otyp, cred_t *credp);
53*117Sschwartz static int pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode,
54*117Sschwartz 						cred_t *credp, int *rvalp);
550Sstevel@tonic-gate static int pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
560Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
570Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
580Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate struct cb_ops pci_cb_ops = {
610Sstevel@tonic-gate 	pci_open,			/* open */
620Sstevel@tonic-gate 	pci_close,			/* close */
630Sstevel@tonic-gate 	nodev,				/* strategy */
640Sstevel@tonic-gate 	nodev,				/* print */
650Sstevel@tonic-gate 	nodev,				/* dump */
660Sstevel@tonic-gate 	nodev,				/* read */
670Sstevel@tonic-gate 	nodev,				/* write */
680Sstevel@tonic-gate 	pci_ioctl,			/* ioctl */
690Sstevel@tonic-gate 	nodev,				/* devmap */
700Sstevel@tonic-gate 	nodev,				/* mmap */
710Sstevel@tonic-gate 	nodev,				/* segmap */
720Sstevel@tonic-gate 	nochpoll,			/* poll */
730Sstevel@tonic-gate 	pci_prop_op,			/* cb_prop_op */
740Sstevel@tonic-gate 	NULL,				/* streamtab */
750Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
760Sstevel@tonic-gate 	CB_REV,				/* rev */
770Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
780Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* ARGSUSED3 */
820Sstevel@tonic-gate static int
830Sstevel@tonic-gate pci_open(dev_t *devp, int flags, int otyp, cred_t *credp)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	pci_t *pci_p;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
910Sstevel@tonic-gate 		return (EINVAL);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/*
940Sstevel@tonic-gate 	 * Get the soft state structure for the device.
950Sstevel@tonic-gate 	 */
960Sstevel@tonic-gate 	pci_p = DEV_TO_SOFTSTATE(*devp);
970Sstevel@tonic-gate 	if (pci_p == NULL)
980Sstevel@tonic-gate 		return (ENXIO);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	DEBUG2(DBG_OPEN, pci_p->pci_dip, "devp=%x: flags=%x\n", devp, flags);
1040Sstevel@tonic-gate 	mutex_enter(&pci_p->pci_mutex);
1050Sstevel@tonic-gate 	if (flags & FEXCL) {
1060Sstevel@tonic-gate 		if (pci_p->pci_soft_state != PCI_SOFT_STATE_CLOSED) {
1070Sstevel@tonic-gate 			mutex_exit(&pci_p->pci_mutex);
1080Sstevel@tonic-gate 			DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n");
1090Sstevel@tonic-gate 			return (EBUSY);
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 		pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN_EXCL;
1120Sstevel@tonic-gate 	} else {
1130Sstevel@tonic-gate 		if (pci_p->pci_soft_state == PCI_SOFT_STATE_OPEN_EXCL) {
1140Sstevel@tonic-gate 			mutex_exit(&pci_p->pci_mutex);
1150Sstevel@tonic-gate 			DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n");
1160Sstevel@tonic-gate 			return (EBUSY);
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 		pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 	pci_p->pci_open_count++;
1210Sstevel@tonic-gate 	mutex_exit(&pci_p->pci_mutex);
1220Sstevel@tonic-gate 	return (0);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /* ARGSUSED */
1270Sstevel@tonic-gate static int
1280Sstevel@tonic-gate pci_close(dev_t dev, int flags, int otyp, cred_t *credp)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	pci_t *pci_p;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
1330Sstevel@tonic-gate 		return (EINVAL);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	pci_p = DEV_TO_SOFTSTATE(dev);
1360Sstevel@tonic-gate 	if (pci_p == NULL)
1370Sstevel@tonic-gate 		return (ENXIO);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	DEBUG2(DBG_CLOSE, pci_p->pci_dip, "dev=%x: flags=%x\n", dev, flags);
1400Sstevel@tonic-gate 	mutex_enter(&pci_p->pci_mutex);
1410Sstevel@tonic-gate 	pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED;
1420Sstevel@tonic-gate 	pci_p->pci_open_count = 0;
1430Sstevel@tonic-gate 	mutex_exit(&pci_p->pci_mutex);
1440Sstevel@tonic-gate 	return (0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* ARGSUSED */
1480Sstevel@tonic-gate static int
149*117Sschwartz pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode,
150*117Sschwartz     cred_t *credp, int *rvalp)
1510Sstevel@tonic-gate {
152*117Sschwartz 	int rv = 0;
1530Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
1540Sstevel@tonic-gate 	uint_t bus_state;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	switch (cmd) {
1600Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
1610Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
1620Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
1630Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
1640Sstevel@tonic-gate 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * read devctl ioctl data
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1710Sstevel@tonic-gate 		return (EFAULT);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	switch (cmd) {
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
1760Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n");
1770Sstevel@tonic-gate 		rv = ENOTSUP;
1780Sstevel@tonic-gate 		break;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
1820Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n");
1830Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
1840Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
1850Sstevel@tonic-gate 				break;
1860Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_QUIESCED);
1870Sstevel@tonic-gate 		break;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
1900Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n");
1910Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
1920Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
1930Sstevel@tonic-gate 				break;
1940Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_ACTIVE);
1950Sstevel@tonic-gate 		break;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
1980Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n");
1990Sstevel@tonic-gate 		rv = ENOTSUP;
2000Sstevel@tonic-gate 		break;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
2030Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n");
2040Sstevel@tonic-gate 		rv = ENOTSUP;
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	default:
2080Sstevel@tonic-gate 		rv = ENOTTY;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
2120Sstevel@tonic-gate 	return (rv);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
215*117Sschwartz 
216*117Sschwartz static int
217*117Sschwartz pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
218*117Sschwartz {
219*117Sschwartz 	pci_t *pci_p;
220*117Sschwartz 	dev_info_t *dip;
221*117Sschwartz 	minor_t minor = getminor(dev);
222*117Sschwartz 	int rv = ENOTTY;
223*117Sschwartz 
224*117Sschwartz 	pci_p = DEV_TO_SOFTSTATE(dev);
225*117Sschwartz 	if (pci_p == NULL)
226*117Sschwartz 		return (ENXIO);
227*117Sschwartz 
228*117Sschwartz 	dip = pci_p->pci_dip;
229*117Sschwartz 	DEBUG2(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd);
230*117Sschwartz 
231*117Sschwartz #ifdef PCI_DMA_TEST
232*117Sschwartz 	if (IS_DMATEST(cmd)) {
233*117Sschwartz 		*rvalp = pci_dma_test(cmd, dip, pci_p, arg);
234*117Sschwartz 		return (0);
235*117Sschwartz 	}
236*117Sschwartz #endif
237*117Sschwartz 
238*117Sschwartz 	switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) {
239*117Sschwartz 	case PCI_TOOL_REG_MINOR_NUM:
240*117Sschwartz 
241*117Sschwartz 		switch (cmd) {
242*117Sschwartz 		case PCITOOL_DEVICE_SET_REG:
243*117Sschwartz 		case PCITOOL_DEVICE_GET_REG:
244*117Sschwartz 
245*117Sschwartz 			/* Require full privileges. */
246*117Sschwartz 			if (secpolicy_kmdb(credp))
247*117Sschwartz 				rv = EPERM;
248*117Sschwartz 			else
249*117Sschwartz 				rv = pcitool_dev_reg_ops(
250*117Sschwartz 				    dev, (void *)arg, cmd, mode);
251*117Sschwartz 			break;
252*117Sschwartz 
253*117Sschwartz 		case PCITOOL_NEXUS_SET_REG:
254*117Sschwartz 		case PCITOOL_NEXUS_GET_REG:
255*117Sschwartz 
256*117Sschwartz 			/* Require full privileges. */
257*117Sschwartz 			if (secpolicy_kmdb(credp))
258*117Sschwartz 				rv = EPERM;
259*117Sschwartz 			else
260*117Sschwartz 				rv = pcitool_bus_reg_ops(
261*117Sschwartz 				    dev, (void *)arg, cmd, mode);
262*117Sschwartz 			break;
263*117Sschwartz 		}
264*117Sschwartz 
265*117Sschwartz 		break;
266*117Sschwartz 
267*117Sschwartz 	case PCI_TOOL_INTR_MINOR_NUM:
268*117Sschwartz 
269*117Sschwartz 		switch (cmd) {
270*117Sschwartz 		case PCITOOL_DEVICE_SET_INTR:
271*117Sschwartz 
272*117Sschwartz 			/* Require PRIV_SYS_RES_CONFIG, same as psradm */
273*117Sschwartz 			if (secpolicy_ponline(credp)) {
274*117Sschwartz 				rv = EPERM;
275*117Sschwartz 				break;
276*117Sschwartz 			}
277*117Sschwartz 
278*117Sschwartz 		/*FALLTHRU*/
279*117Sschwartz 		/* These require no special privileges. */
280*117Sschwartz 		case PCITOOL_DEVICE_GET_INTR:
281*117Sschwartz 		case PCITOOL_DEVICE_NUM_INTR:
282*117Sschwartz 			rv = pcitool_intr_admn(dev, (void *)arg, cmd, mode);
283*117Sschwartz 			break;
284*117Sschwartz 		}
285*117Sschwartz 
286*117Sschwartz 		break;
287*117Sschwartz 
288*117Sschwartz 	case PCIHP_DEVCTL_MINOR:
289*117Sschwartz 		rv = pci_devctl_ioctl(dip, cmd, arg, mode, credp, rvalp);
290*117Sschwartz 		break;
291*117Sschwartz 
292*117Sschwartz 	default:
293*117Sschwartz 		break;
294*117Sschwartz 	}
295*117Sschwartz 
296*117Sschwartz 	return (rv);
297*117Sschwartz }
298*117Sschwartz 
2990Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3000Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3030Sstevel@tonic-gate 	    "hotplug-capable"))
3040Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip,
3050Sstevel@tonic-gate 		    prop_op, flags, name, valuep, lengthp));
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
3080Sstevel@tonic-gate }
309