xref: /illumos-gate/usr/src/uts/sun4/io/px/px_devctl.c (revision d4476ccb)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * PCI nexus HotPlug devctl interface
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/async.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/open.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
4369cd775fSschwartz #include <sys/policy.h>
447c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
457c478bd9Sstevel@tonic-gate #include "px_obj.h"
4669cd775fSschwartz #include <sys/pci_tools.h>
47*d4476ccbSschwartz #include "px_tools_ext.h"
487c478bd9Sstevel@tonic-gate #include "pcie_pwr.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int px_open(dev_t *devp, int flags, int otyp, cred_t *credp);
537c478bd9Sstevel@tonic-gate static int px_close(dev_t dev, int flags, int otyp, cred_t *credp);
547c478bd9Sstevel@tonic-gate static int px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
557c478bd9Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
567c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
577c478bd9Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct cb_ops px_cb_ops = {
607c478bd9Sstevel@tonic-gate 	px_open,			/* open */
617c478bd9Sstevel@tonic-gate 	px_close,			/* close */
627c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
637c478bd9Sstevel@tonic-gate 	nodev,				/* print */
647c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
657c478bd9Sstevel@tonic-gate 	nodev,				/* read */
667c478bd9Sstevel@tonic-gate 	nodev,				/* write */
677c478bd9Sstevel@tonic-gate 	px_ioctl,			/* ioctl */
687c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
697c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
707c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
717c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
727c478bd9Sstevel@tonic-gate 	px_prop_op,			/* cb_prop_op */
737c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
747c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
757c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
767c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
777c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
817c478bd9Sstevel@tonic-gate static int
827c478bd9Sstevel@tonic-gate px_open(dev_t *devp, int flags, int otyp, cred_t *credp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	px_t *px_p;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
907c478bd9Sstevel@tonic-gate 		return (EINVAL);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Get the soft state structure for the device.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	px_p = DEV_TO_SOFTSTATE(*devp);
967c478bd9Sstevel@tonic-gate 	if (px_p == NULL)
977c478bd9Sstevel@tonic-gate 		return (ENXIO);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	DBG(DBG_OPEN, px_p->px_dip, "devp=%x: flags=%x\n", devp, flags);
1037c478bd9Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
1047c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
1057c478bd9Sstevel@tonic-gate 		if (px_p->px_soft_state != PX_SOFT_STATE_CLOSED) {
1067c478bd9Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
1077c478bd9Sstevel@tonic-gate 			DBG(DBG_OPEN, px_p->px_dip, "busy\n");
1087c478bd9Sstevel@tonic-gate 			return (EBUSY);
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_OPEN_EXCL;
1117c478bd9Sstevel@tonic-gate 	} else {
1127c478bd9Sstevel@tonic-gate 		if (px_p->px_soft_state == PX_SOFT_STATE_OPEN_EXCL) {
1137c478bd9Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
1147c478bd9Sstevel@tonic-gate 			DBG(DBG_OPEN, px_p->px_dip, "busy\n");
1157c478bd9Sstevel@tonic-gate 			return (EBUSY);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_OPEN;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	px_p->px_open_count++;
1207c478bd9Sstevel@tonic-gate 	mutex_exit(&px_p->px_mutex);
1217c478bd9Sstevel@tonic-gate 	return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* ARGSUSED */
1267c478bd9Sstevel@tonic-gate static int
1277c478bd9Sstevel@tonic-gate px_close(dev_t dev, int flags, int otyp, cred_t *credp)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	px_t *px_p;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
1327c478bd9Sstevel@tonic-gate 		return (EINVAL);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	px_p = DEV_TO_SOFTSTATE(dev);
1357c478bd9Sstevel@tonic-gate 	if (px_p == NULL)
1367c478bd9Sstevel@tonic-gate 		return (ENXIO);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	DBG(DBG_CLOSE, px_p->px_dip, "dev=%x: flags=%x\n", dev, flags);
1397c478bd9Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
1407c478bd9Sstevel@tonic-gate 	px_p->px_soft_state = PX_SOFT_STATE_CLOSED;
1417c478bd9Sstevel@tonic-gate 	px_p->px_open_count = 0;
1427c478bd9Sstevel@tonic-gate 	mutex_exit(&px_p->px_mutex);
1437c478bd9Sstevel@tonic-gate 	return (0);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* ARGSUSED */
1477c478bd9Sstevel@tonic-gate static int
1487c478bd9Sstevel@tonic-gate px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	px_t *px_p;
1517c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
1527c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
1537c478bd9Sstevel@tonic-gate 	uint_t bus_state;
1547c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
15569cd775fSschwartz 	int minor = getminor(dev);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	px_p = DEV_TO_SOFTSTATE(dev);
1587c478bd9Sstevel@tonic-gate 	if (px_p == NULL)
1597c478bd9Sstevel@tonic-gate 		return (ENXIO);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	dip = px_p->px_dip;
1627c478bd9Sstevel@tonic-gate 	DBG(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd);
16369cd775fSschwartz 
1647c478bd9Sstevel@tonic-gate #ifdef	PX_DMA_TEST
1657c478bd9Sstevel@tonic-gate 	if (IS_DMATEST(cmd)) {
1667c478bd9Sstevel@tonic-gate 		*rvalp = px_dma_test(cmd, dip, px_p, arg);
1677c478bd9Sstevel@tonic-gate 		return (0);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate #endif	/* PX_DMA_TEST */
1707c478bd9Sstevel@tonic-gate 
17169cd775fSschwartz 	switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) {
17269cd775fSschwartz 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * PCI tools.
1757c478bd9Sstevel@tonic-gate 	 */
17669cd775fSschwartz 	case PCI_TOOL_REG_MINOR_NUM:
1777c478bd9Sstevel@tonic-gate 
17869cd775fSschwartz 		switch (cmd) {
17969cd775fSschwartz 		case PCITOOL_DEVICE_SET_REG:
18069cd775fSschwartz 		case PCITOOL_DEVICE_GET_REG:
1817c478bd9Sstevel@tonic-gate 
18269cd775fSschwartz 			/* Require full privileges. */
18369cd775fSschwartz 			if (secpolicy_kmdb(credp))
18469cd775fSschwartz 				rv = EPERM;
18569cd775fSschwartz 			else
18669cd775fSschwartz 				rv = pxtool_dev_reg_ops(dip,
18769cd775fSschwartz 				    (void *)arg, cmd, mode);
18869cd775fSschwartz 			break;
1897c478bd9Sstevel@tonic-gate 
19069cd775fSschwartz 		case PCITOOL_NEXUS_SET_REG:
19169cd775fSschwartz 		case PCITOOL_NEXUS_GET_REG:
1927c478bd9Sstevel@tonic-gate 
19369cd775fSschwartz 			/* Require full privileges. */
19469cd775fSschwartz 			if (secpolicy_kmdb(credp))
19569cd775fSschwartz 				rv = EPERM;
19669cd775fSschwartz 			else
19769cd775fSschwartz 				rv = pxtool_bus_reg_ops(dip,
19869cd775fSschwartz 				    (void *)arg, cmd, mode);
19969cd775fSschwartz 			break;
20069cd775fSschwartz 
20169cd775fSschwartz 		default:
20269cd775fSschwartz 			rv = ENOTTY;
20369cd775fSschwartz 		}
2047c478bd9Sstevel@tonic-gate 		return (rv);
20569cd775fSschwartz 
20669cd775fSschwartz 	case PCI_TOOL_INTR_MINOR_NUM:
20769cd775fSschwartz 
20869cd775fSschwartz 		switch (cmd) {
20969cd775fSschwartz 		case PCITOOL_DEVICE_SET_INTR:
21069cd775fSschwartz 
21169cd775fSschwartz 		/*
21269cd775fSschwartz 		 * Require PRIV_SYS_RES_CONFIG,
21369cd775fSschwartz 		 * same as psradm
21469cd775fSschwartz 		 */
21569cd775fSschwartz 		if (secpolicy_ponline(credp)) {
21669cd775fSschwartz 			rv = EPERM;
21769cd775fSschwartz 			break;
21869cd775fSschwartz 		}
21969cd775fSschwartz 
22069cd775fSschwartz 		/*FALLTHRU*/
22169cd775fSschwartz 		/* These require no special privileges. */
22269cd775fSschwartz 		case PCITOOL_DEVICE_GET_INTR:
22369cd775fSschwartz 		case PCITOOL_DEVICE_NUM_INTR:
22469cd775fSschwartz 			rv = pxtool_intr(dip, (void *)arg, cmd, mode);
22569cd775fSschwartz 			break;
22669cd775fSschwartz 
22769cd775fSschwartz 		default:
22869cd775fSschwartz 			rv = ENOTTY;
22969cd775fSschwartz 		}
2307c478bd9Sstevel@tonic-gate 		return (rv);
23169cd775fSschwartz 
2327c478bd9Sstevel@tonic-gate 	default:
2337c478bd9Sstevel@tonic-gate 		break;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
23669cd775fSschwartz 	if ((cmd & ~PPMREQ_MASK) == PPMREQ) {
23769cd775fSschwartz 
23869cd775fSschwartz 		/* Need privileges to use these ioctls. */
23969cd775fSschwartz 		if (drv_priv(credp)) {
24069cd775fSschwartz 			DBG(DBG_TOOLS, dip,
24169cd775fSschwartz 			    "px_tools: Insufficient privileges\n");
24269cd775fSschwartz 
24369cd775fSschwartz 			return (EPERM);
24469cd775fSschwartz 		}
2457c478bd9Sstevel@tonic-gate 		return (px_lib_pmctl(cmd, px_p));
24669cd775fSschwartz 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	switch (cmd) {
2527c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
2537c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
2547c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
2557c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
2567c478bd9Sstevel@tonic-gate 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * read devctl ioctl data
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
2637c478bd9Sstevel@tonic-gate 		return (EFAULT);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	switch (cmd) {
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
2687c478bd9Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n");
2697c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
2707c478bd9Sstevel@tonic-gate 		break;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
2747c478bd9Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n");
2757c478bd9Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2767c478bd9Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
2777c478bd9Sstevel@tonic-gate 				break;
2787c478bd9Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_QUIESCED);
2797c478bd9Sstevel@tonic-gate 		break;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
2827c478bd9Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n");
2837c478bd9Sstevel@tonic-gate 		if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2847c478bd9Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
2857c478bd9Sstevel@tonic-gate 				break;
2867c478bd9Sstevel@tonic-gate 		(void) ndi_set_bus_state(dip, BUS_ACTIVE);
2877c478bd9Sstevel@tonic-gate 		break;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
2907c478bd9Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n");
2917c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
2957c478bd9Sstevel@tonic-gate 		DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n");
2967c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
2977c478bd9Sstevel@tonic-gate 		break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	default:
3007c478bd9Sstevel@tonic-gate 		rv = ENOTTY;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
3047c478bd9Sstevel@tonic-gate 	return (rv);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3087c478bd9Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3117c478bd9Sstevel@tonic-gate 	    "hotplug-capable"))
3127c478bd9Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip,
3137c478bd9Sstevel@tonic-gate 		    prop_op, flags, name, valuep, lengthp));
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
3167c478bd9Sstevel@tonic-gate }
317