xref: /onnv-gate/usr/src/uts/sun4/io/px/px_devctl.c (revision 624:8c5206bfd8f1)
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/open.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/file.h>
43*624Sschwartz #include <sys/policy.h>
440Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
450Sstevel@tonic-gate #include "px_obj.h"
46*624Sschwartz #include <sys/pci_tools.h>
470Sstevel@tonic-gate #include "px_tools.h"
480Sstevel@tonic-gate #include "pcie_pwr.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*LINTLIBRARY*/
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static int px_open(dev_t *devp, int flags, int otyp, cred_t *credp);
530Sstevel@tonic-gate static int px_close(dev_t dev, int flags, int otyp, cred_t *credp);
540Sstevel@tonic-gate static int px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
550Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
560Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
570Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate struct cb_ops px_cb_ops = {
600Sstevel@tonic-gate 	px_open,			/* open */
610Sstevel@tonic-gate 	px_close,			/* close */
620Sstevel@tonic-gate 	nodev,				/* strategy */
630Sstevel@tonic-gate 	nodev,				/* print */
640Sstevel@tonic-gate 	nodev,				/* dump */
650Sstevel@tonic-gate 	nodev,				/* read */
660Sstevel@tonic-gate 	nodev,				/* write */
670Sstevel@tonic-gate 	px_ioctl,			/* ioctl */
680Sstevel@tonic-gate 	nodev,				/* devmap */
690Sstevel@tonic-gate 	nodev,				/* mmap */
700Sstevel@tonic-gate 	nodev,				/* segmap */
710Sstevel@tonic-gate 	nochpoll,			/* poll */
720Sstevel@tonic-gate 	px_prop_op,			/* cb_prop_op */
730Sstevel@tonic-gate 	NULL,				/* streamtab */
740Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
750Sstevel@tonic-gate 	CB_REV,				/* rev */
760Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
770Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
780Sstevel@tonic-gate };
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* ARGSUSED3 */
810Sstevel@tonic-gate static int
820Sstevel@tonic-gate px_open(dev_t *devp, int flags, int otyp, cred_t *credp)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	px_t *px_p;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
900Sstevel@tonic-gate 		return (EINVAL);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * Get the soft state structure for the device.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	px_p = DEV_TO_SOFTSTATE(*devp);
960Sstevel@tonic-gate 	if (px_p == NULL)
970Sstevel@tonic-gate 		return (ENXIO);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
1010Sstevel@tonic-gate 	 */
1020Sstevel@tonic-gate 	DBG(DBG_OPEN, px_p->px_dip, "devp=%x: flags=%x\n", devp, flags);
1030Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
1040Sstevel@tonic-gate 	if (flags & FEXCL) {
1050Sstevel@tonic-gate 		if (px_p->px_soft_state != PX_SOFT_STATE_CLOSED) {
1060Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
1070Sstevel@tonic-gate 			DBG(DBG_OPEN, px_p->px_dip, "busy\n");
1080Sstevel@tonic-gate 			return (EBUSY);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_OPEN_EXCL;
1110Sstevel@tonic-gate 	} else {
1120Sstevel@tonic-gate 		if (px_p->px_soft_state == PX_SOFT_STATE_OPEN_EXCL) {
1130Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
1140Sstevel@tonic-gate 			DBG(DBG_OPEN, px_p->px_dip, "busy\n");
1150Sstevel@tonic-gate 			return (EBUSY);
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_OPEN;
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 	px_p->px_open_count++;
1200Sstevel@tonic-gate 	mutex_exit(&px_p->px_mutex);
1210Sstevel@tonic-gate 	return (0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /* ARGSUSED */
1260Sstevel@tonic-gate static int
1270Sstevel@tonic-gate px_close(dev_t dev, int flags, int otyp, cred_t *credp)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	px_t *px_p;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
1320Sstevel@tonic-gate 		return (EINVAL);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	px_p = DEV_TO_SOFTSTATE(dev);
1350Sstevel@tonic-gate 	if (px_p == NULL)
1360Sstevel@tonic-gate 		return (ENXIO);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	DBG(DBG_CLOSE, px_p->px_dip, "dev=%x: flags=%x\n", dev, flags);
1390Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
1400Sstevel@tonic-gate 	px_p->px_soft_state = PX_SOFT_STATE_CLOSED;
1410Sstevel@tonic-gate 	px_p->px_open_count = 0;
1420Sstevel@tonic-gate 	mutex_exit(&px_p->px_mutex);
1430Sstevel@tonic-gate 	return (0);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* ARGSUSED */
1470Sstevel@tonic-gate static int
1480Sstevel@tonic-gate px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	px_t *px_p;
1510Sstevel@tonic-gate 	dev_info_t *dip;
1520Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
1530Sstevel@tonic-gate 	uint_t bus_state;
1540Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
155*624Sschwartz 	int minor = getminor(dev);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	px_p = DEV_TO_SOFTSTATE(dev);
1580Sstevel@tonic-gate 	if (px_p == NULL)
1590Sstevel@tonic-gate 		return (ENXIO);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	dip = px_p->px_dip;
1620Sstevel@tonic-gate 	DBG(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd);
163*624Sschwartz 
1640Sstevel@tonic-gate #ifdef	PX_DMA_TEST
1650Sstevel@tonic-gate 	if (IS_DMATEST(cmd)) {
1660Sstevel@tonic-gate 		*rvalp = px_dma_test(cmd, dip, px_p, arg);
1670Sstevel@tonic-gate 		return (0);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate #endif	/* PX_DMA_TEST */
1700Sstevel@tonic-gate 
171*624Sschwartz 	switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) {
172*624Sschwartz 
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * PCI tools.
1750Sstevel@tonic-gate 	 */
176*624Sschwartz 	case PCI_TOOL_REG_MINOR_NUM:
1770Sstevel@tonic-gate 
178*624Sschwartz 		switch (cmd) {
179*624Sschwartz 		case PCITOOL_DEVICE_SET_REG:
180*624Sschwartz 		case PCITOOL_DEVICE_GET_REG:
181*624Sschwartz 
182*624Sschwartz 			/* Require full privileges. */
183*624Sschwartz 			if (secpolicy_kmdb(credp))
184*624Sschwartz 				rv = EPERM;
185*624Sschwartz 			else
186*624Sschwartz 				rv = pxtool_dev_reg_ops(dip,
187*624Sschwartz 				    (void *)arg, cmd, mode);
188*624Sschwartz 			break;
189*624Sschwartz 
190*624Sschwartz 		case PCITOOL_NEXUS_SET_REG:
191*624Sschwartz 		case PCITOOL_NEXUS_GET_REG:
192*624Sschwartz 
193*624Sschwartz 			/* Require full privileges. */
194*624Sschwartz 			if (secpolicy_kmdb(credp))
195*624Sschwartz 				rv = EPERM;
196*624Sschwartz 			else
197*624Sschwartz 				rv = pxtool_bus_reg_ops(dip,
198*624Sschwartz 				    (void *)arg, cmd, mode);
199*624Sschwartz 			break;
200*624Sschwartz 
201*624Sschwartz 		default:
202*624Sschwartz 			rv = ENOTTY;
203*624Sschwartz 		}
204*624Sschwartz 		return (rv);
205*624Sschwartz 
206*624Sschwartz 	case PCI_TOOL_INTR_MINOR_NUM:
207*624Sschwartz 
208*624Sschwartz 		switch (cmd) {
209*624Sschwartz 		case PCITOOL_DEVICE_SET_INTR:
210*624Sschwartz 
211*624Sschwartz 		/*
212*624Sschwartz 		 * Require PRIV_SYS_RES_CONFIG,
213*624Sschwartz 		 * same as psradm
214*624Sschwartz 		 */
215*624Sschwartz 		if (secpolicy_ponline(credp)) {
216*624Sschwartz 			rv = EPERM;
217*624Sschwartz 			break;
218*624Sschwartz 		}
219*624Sschwartz 
220*624Sschwartz 		/*FALLTHRU*/
221*624Sschwartz 		/* These require no special privileges. */
222*624Sschwartz 		case PCITOOL_DEVICE_GET_INTR:
223*624Sschwartz 		case PCITOOL_DEVICE_NUM_INTR:
224*624Sschwartz 			rv = pxtool_intr(dip, (void *)arg, cmd, mode);
225*624Sschwartz 			break;
226*624Sschwartz 
227*624Sschwartz 		default:
228*624Sschwartz 			rv = ENOTTY;
229*624Sschwartz 		}
230*624Sschwartz 		return (rv);
231*624Sschwartz 
232*624Sschwartz 	default:
233*624Sschwartz 		break;
234*624Sschwartz 	}
235*624Sschwartz 
236*624Sschwartz 	if ((cmd & ~PPMREQ_MASK) == PPMREQ) {
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		/* Need privileges to use these ioctls. */
2390Sstevel@tonic-gate 		if (drv_priv(credp)) {
2400Sstevel@tonic-gate 			DBG(DBG_TOOLS, dip,
2410Sstevel@tonic-gate 			    "px_tools: Insufficient privileges\n");
2420Sstevel@tonic-gate 
243*624Sschwartz 			return (EPERM);
2440Sstevel@tonic-gate 		}
245*624Sschwartz 		return (px_lib_pmctl(cmd, px_p));
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	switch (cmd) {
2520Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
2530Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
2540Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
2550Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
2560Sstevel@tonic-gate 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * read devctl ioctl data
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
2630Sstevel@tonic-gate 		return (EFAULT);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	switch (cmd) {
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
2680Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n");
2690Sstevel@tonic-gate 		rv = ENOTSUP;
2700Sstevel@tonic-gate 		break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
2740Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n");
2750Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2760Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
2770Sstevel@tonic-gate 				break;
2780Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_QUIESCED);
2790Sstevel@tonic-gate 		break;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
2820Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n");
2830Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2840Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
2850Sstevel@tonic-gate 				break;
2860Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_ACTIVE);
2870Sstevel@tonic-gate 		break;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
2900Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n");
2910Sstevel@tonic-gate 		rv = ENOTSUP;
2920Sstevel@tonic-gate 		break;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
2950Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n");
2960Sstevel@tonic-gate 		rv = ENOTSUP;
2970Sstevel@tonic-gate 		break;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	default:
3000Sstevel@tonic-gate 		rv = ENOTTY;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
3040Sstevel@tonic-gate 	return (rv);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3080Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3110Sstevel@tonic-gate 	    "hotplug-capable"))
3120Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip,
3130Sstevel@tonic-gate 		    prop_op, flags, name, valuep, lengthp));
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
3160Sstevel@tonic-gate }
317