xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_devctl.c (revision 10923:df470fd79c3c)
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
54397Sschwartz  * Common Development and Distribution License (the "License").
64397Sschwartz  * 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*10923SEvan.Yan@Sun.COM  * Copyright 2009 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  * PCI nexus HotPlug devctl interface
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/async.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/sunndi.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
380Sstevel@tonic-gate #include <sys/pci_tools.h>
39777Sschwartz #include <sys/pci/pci_tools_ext.h>
400Sstevel@tonic-gate #include <sys/open.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/policy.h>
440Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*LINTLIBRARY*/
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static int pci_open(dev_t *devp, int flags, int otyp, cred_t *credp);
490Sstevel@tonic-gate static int pci_close(dev_t dev, int flags, int otyp, cred_t *credp);
50117Sschwartz static int pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode,
51117Sschwartz 						cred_t *credp, int *rvalp);
520Sstevel@tonic-gate static int pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
530Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
540Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
550Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct cb_ops pci_cb_ops = {
580Sstevel@tonic-gate 	pci_open,			/* open */
590Sstevel@tonic-gate 	pci_close,			/* close */
600Sstevel@tonic-gate 	nodev,				/* strategy */
610Sstevel@tonic-gate 	nodev,				/* print */
620Sstevel@tonic-gate 	nodev,				/* dump */
630Sstevel@tonic-gate 	nodev,				/* read */
640Sstevel@tonic-gate 	nodev,				/* write */
650Sstevel@tonic-gate 	pci_ioctl,			/* ioctl */
660Sstevel@tonic-gate 	nodev,				/* devmap */
670Sstevel@tonic-gate 	nodev,				/* mmap */
680Sstevel@tonic-gate 	nodev,				/* segmap */
690Sstevel@tonic-gate 	nochpoll,			/* poll */
700Sstevel@tonic-gate 	pci_prop_op,			/* cb_prop_op */
710Sstevel@tonic-gate 	NULL,				/* streamtab */
720Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
730Sstevel@tonic-gate 	CB_REV,				/* rev */
740Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
750Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
78162Sschwartz extern struct cb_ops *pcihp_ops;
79162Sschwartz 
800Sstevel@tonic-gate /* ARGSUSED3 */
810Sstevel@tonic-gate static int
pci_open(dev_t * devp,int flags,int otyp,cred_t * credp)820Sstevel@tonic-gate pci_open(dev_t *devp, int flags, int otyp, cred_t *credp)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	pci_t *pci_p;
85162Sschwartz 	int rval;
86162Sschwartz 	uint_t orig_pci_soft_state;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
900Sstevel@tonic-gate 	 */
910Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
920Sstevel@tonic-gate 		return (EINVAL);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * Get the soft state structure for the device.
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	pci_p = DEV_TO_SOFTSTATE(*devp);
980Sstevel@tonic-gate 	if (pci_p == NULL)
990Sstevel@tonic-gate 		return (ENXIO);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/*
1020Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	DEBUG2(DBG_OPEN, pci_p->pci_dip, "devp=%x: flags=%x\n", devp, flags);
1050Sstevel@tonic-gate 	mutex_enter(&pci_p->pci_mutex);
106162Sschwartz 	orig_pci_soft_state = pci_p->pci_soft_state;
1070Sstevel@tonic-gate 	if (flags & FEXCL) {
1080Sstevel@tonic-gate 		if (pci_p->pci_soft_state != PCI_SOFT_STATE_CLOSED) {
1090Sstevel@tonic-gate 			mutex_exit(&pci_p->pci_mutex);
1100Sstevel@tonic-gate 			DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n");
1110Sstevel@tonic-gate 			return (EBUSY);
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 		pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN_EXCL;
1140Sstevel@tonic-gate 	} else {
1150Sstevel@tonic-gate 		if (pci_p->pci_soft_state == PCI_SOFT_STATE_OPEN_EXCL) {
1160Sstevel@tonic-gate 			mutex_exit(&pci_p->pci_mutex);
1170Sstevel@tonic-gate 			DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n");
1180Sstevel@tonic-gate 			return (EBUSY);
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 		pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN;
1210Sstevel@tonic-gate 	}
122162Sschwartz 
123162Sschwartz 	if (pci_p->hotplug_capable == B_TRUE) {
124162Sschwartz 		if (rval = pcihp_ops->cb_open(devp, flags, otyp, credp)) {
125162Sschwartz 			pci_p->pci_soft_state = orig_pci_soft_state;
126162Sschwartz 			mutex_exit(&pci_p->pci_mutex);
127162Sschwartz 			return (rval);
128162Sschwartz 		}
129162Sschwartz 	}
130162Sschwartz 
1310Sstevel@tonic-gate 	mutex_exit(&pci_p->pci_mutex);
132162Sschwartz 
1330Sstevel@tonic-gate 	return (0);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* ARGSUSED */
1380Sstevel@tonic-gate static int
pci_close(dev_t dev,int flags,int otyp,cred_t * credp)1390Sstevel@tonic-gate pci_close(dev_t dev, int flags, int otyp, cred_t *credp)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	pci_t *pci_p;
142162Sschwartz 	int rval;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
1450Sstevel@tonic-gate 		return (EINVAL);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	pci_p = DEV_TO_SOFTSTATE(dev);
1480Sstevel@tonic-gate 	if (pci_p == NULL)
1490Sstevel@tonic-gate 		return (ENXIO);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	DEBUG2(DBG_CLOSE, pci_p->pci_dip, "dev=%x: flags=%x\n", dev, flags);
1520Sstevel@tonic-gate 	mutex_enter(&pci_p->pci_mutex);
153162Sschwartz 
154162Sschwartz 	if (pci_p->hotplug_capable == B_TRUE)
155162Sschwartz 		if (rval = pcihp_ops->cb_close(dev, flags, otyp, credp)) {
156162Sschwartz 			mutex_exit(&pci_p->pci_mutex);
157162Sschwartz 			return (rval);
158162Sschwartz 		}
159162Sschwartz 
1600Sstevel@tonic-gate 	pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED;
1610Sstevel@tonic-gate 	mutex_exit(&pci_p->pci_mutex);
1620Sstevel@tonic-gate 	return (0);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /* ARGSUSED */
1660Sstevel@tonic-gate static int
pci_devctl_ioctl(dev_info_t * dip,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)167117Sschwartz pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode,
168117Sschwartz     cred_t *credp, int *rvalp)
1690Sstevel@tonic-gate {
170117Sschwartz 	int rv = 0;
1710Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
1720Sstevel@tonic-gate 	uint_t bus_state;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
1760Sstevel@tonic-gate 	 */
1770Sstevel@tonic-gate 	switch (cmd) {
1780Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
1790Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
1800Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
1810Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
1820Sstevel@tonic-gate 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * read devctl ioctl data
1870Sstevel@tonic-gate 	 */
1880Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1890Sstevel@tonic-gate 		return (EFAULT);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	switch (cmd) {
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
1940Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n");
1950Sstevel@tonic-gate 		rv = ENOTSUP;
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
2000Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n");
2010Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2020Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
2030Sstevel@tonic-gate 				break;
2040Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_QUIESCED);
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
2080Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n");
2090Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2100Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
2110Sstevel@tonic-gate 				break;
2120Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_ACTIVE);
2130Sstevel@tonic-gate 		break;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
2160Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n");
2170Sstevel@tonic-gate 		rv = ENOTSUP;
2180Sstevel@tonic-gate 		break;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
2210Sstevel@tonic-gate 		DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n");
2220Sstevel@tonic-gate 		rv = ENOTSUP;
2230Sstevel@tonic-gate 		break;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	default:
2260Sstevel@tonic-gate 		rv = ENOTTY;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
2300Sstevel@tonic-gate 	return (rv);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
233117Sschwartz 
234117Sschwartz static int
pci_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)235117Sschwartz pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
236117Sschwartz {
237117Sschwartz 	pci_t *pci_p;
238117Sschwartz 	dev_info_t *dip;
239117Sschwartz 	minor_t minor = getminor(dev);
240117Sschwartz 	int rv = ENOTTY;
241117Sschwartz 
242117Sschwartz 	pci_p = DEV_TO_SOFTSTATE(dev);
243117Sschwartz 	if (pci_p == NULL)
244117Sschwartz 		return (ENXIO);
245117Sschwartz 
246117Sschwartz 	dip = pci_p->pci_dip;
247117Sschwartz 	DEBUG2(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd);
248117Sschwartz 
249117Sschwartz #ifdef PCI_DMA_TEST
250117Sschwartz 	if (IS_DMATEST(cmd)) {
251117Sschwartz 		*rvalp = pci_dma_test(cmd, dip, pci_p, arg);
252117Sschwartz 		return (0);
253117Sschwartz 	}
254117Sschwartz #endif
255117Sschwartz 
256117Sschwartz 	switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) {
257117Sschwartz 	case PCI_TOOL_REG_MINOR_NUM:
258117Sschwartz 
259117Sschwartz 		switch (cmd) {
260117Sschwartz 		case PCITOOL_DEVICE_SET_REG:
261117Sschwartz 		case PCITOOL_DEVICE_GET_REG:
262117Sschwartz 
263117Sschwartz 			/* Require full privileges. */
264117Sschwartz 			if (secpolicy_kmdb(credp))
265117Sschwartz 				rv = EPERM;
266117Sschwartz 			else
267117Sschwartz 				rv = pcitool_dev_reg_ops(
268117Sschwartz 				    dev, (void *)arg, cmd, mode);
269117Sschwartz 			break;
270117Sschwartz 
271117Sschwartz 		case PCITOOL_NEXUS_SET_REG:
272117Sschwartz 		case PCITOOL_NEXUS_GET_REG:
273117Sschwartz 
274117Sschwartz 			/* Require full privileges. */
275117Sschwartz 			if (secpolicy_kmdb(credp))
276117Sschwartz 				rv = EPERM;
277117Sschwartz 			else
278117Sschwartz 				rv = pcitool_bus_reg_ops(
279117Sschwartz 				    dev, (void *)arg, cmd, mode);
280117Sschwartz 			break;
281117Sschwartz 		}
282117Sschwartz 
283117Sschwartz 		break;
284117Sschwartz 
285117Sschwartz 	case PCI_TOOL_INTR_MINOR_NUM:
286117Sschwartz 
287117Sschwartz 		switch (cmd) {
288117Sschwartz 		case PCITOOL_DEVICE_SET_INTR:
289117Sschwartz 
290117Sschwartz 			/* Require PRIV_SYS_RES_CONFIG, same as psradm */
291117Sschwartz 			if (secpolicy_ponline(credp)) {
292117Sschwartz 				rv = EPERM;
293117Sschwartz 				break;
294117Sschwartz 			}
295117Sschwartz 
296117Sschwartz 		/*FALLTHRU*/
297117Sschwartz 		/* These require no special privileges. */
298117Sschwartz 		case PCITOOL_DEVICE_GET_INTR:
2994397Sschwartz 		case PCITOOL_SYSTEM_INTR_INFO:
300117Sschwartz 			rv = pcitool_intr_admn(dev, (void *)arg, cmd, mode);
301117Sschwartz 			break;
302117Sschwartz 		}
303117Sschwartz 
304117Sschwartz 		break;
305117Sschwartz 
306663Sschwartz 	/*
307663Sschwartz 	 * All non-PCItool ioctls go through here, including:
308663Sschwartz 	 *   devctl ioctls with minor number PCIHP_DEVCTL_MINOR and
309663Sschwartz 	 *   those for attachment points with where minor number is the
310663Sschwartz 	 *   device number.
311663Sschwartz 	 */
312663Sschwartz 	default:
313162Sschwartz 		if (pci_p->hotplug_capable == B_TRUE)
314162Sschwartz 			rv = pcihp_ops->cb_ioctl(
315162Sschwartz 			    dev, cmd, arg, mode, credp, rvalp);
316162Sschwartz 		else
317162Sschwartz 			rv = pci_devctl_ioctl(
318162Sschwartz 			    dip, cmd, arg, mode, credp, rvalp);
319117Sschwartz 		break;
320117Sschwartz 	}
321117Sschwartz 
322117Sschwartz 	return (rv);
323117Sschwartz }
324117Sschwartz 
pci_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int flags,char * name,caddr_t valuep,int * lengthp)3250Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3260Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3290Sstevel@tonic-gate 	    "hotplug-capable"))
3300Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip,
3310Sstevel@tonic-gate 		    prop_op, flags, name, valuep, lengthp));
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
3340Sstevel@tonic-gate }
335