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 57aadd8d4Skini * Common Development and Distribution License (the "License"). 67aadd8d4Skini * 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*2917a9c9Sschwartz * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * PCI nexus HotPlug devctl interface 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/conf.h> 337c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 347c478bd9Sstevel@tonic-gate #include <sys/async.h> 357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 377c478bd9Sstevel@tonic-gate #include <sys/sunndi.h> 387c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 397c478bd9Sstevel@tonic-gate #include <sys/open.h> 407c478bd9Sstevel@tonic-gate #include <sys/errno.h> 417c478bd9Sstevel@tonic-gate #include <sys/file.h> 4269cd775fSschwartz #include <sys/policy.h> 437c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 447c478bd9Sstevel@tonic-gate #include "px_obj.h" 4569cd775fSschwartz #include <sys/pci_tools.h> 46d4476ccbSschwartz #include "px_tools_ext.h" 477c478bd9Sstevel@tonic-gate #include "pcie_pwr.h" 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static int px_open(dev_t *devp, int flags, int otyp, cred_t *credp); 527c478bd9Sstevel@tonic-gate static int px_close(dev_t dev, int flags, int otyp, cred_t *credp); 537c478bd9Sstevel@tonic-gate static int px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 547c478bd9Sstevel@tonic-gate cred_t *credp, int *rvalp); 557c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 567c478bd9Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp); 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate struct cb_ops px_cb_ops = { 597c478bd9Sstevel@tonic-gate px_open, /* open */ 607c478bd9Sstevel@tonic-gate px_close, /* close */ 617c478bd9Sstevel@tonic-gate nodev, /* strategy */ 627c478bd9Sstevel@tonic-gate nodev, /* print */ 637c478bd9Sstevel@tonic-gate nodev, /* dump */ 647c478bd9Sstevel@tonic-gate nodev, /* read */ 657c478bd9Sstevel@tonic-gate nodev, /* write */ 667c478bd9Sstevel@tonic-gate px_ioctl, /* ioctl */ 677c478bd9Sstevel@tonic-gate nodev, /* devmap */ 687c478bd9Sstevel@tonic-gate nodev, /* mmap */ 697c478bd9Sstevel@tonic-gate nodev, /* segmap */ 707c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 717c478bd9Sstevel@tonic-gate px_prop_op, /* cb_prop_op */ 727c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 737c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 747c478bd9Sstevel@tonic-gate CB_REV, /* rev */ 757c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 767c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 777c478bd9Sstevel@tonic-gate }; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* ARGSUSED3 */ 807c478bd9Sstevel@tonic-gate static int 817c478bd9Sstevel@tonic-gate px_open(dev_t *devp, int flags, int otyp, cred_t *credp) 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate px_t *px_p; 84b65731f1Skini int rval; 85b65731f1Skini uint_t orig_px_soft_state; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * Make sure the open is for the right file type. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 917c478bd9Sstevel@tonic-gate return (EINVAL); 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * Get the soft state structure for the device. 957c478bd9Sstevel@tonic-gate */ 9636fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(*devp); 977c478bd9Sstevel@tonic-gate if (px_p == NULL) 987c478bd9Sstevel@tonic-gate return (ENXIO); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Handle the open by tracking the device state. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "devp=%x: flags=%x\n", devp, flags); 1047c478bd9Sstevel@tonic-gate mutex_enter(&px_p->px_mutex); 105b65731f1Skini orig_px_soft_state = px_p->px_soft_state; 1067c478bd9Sstevel@tonic-gate if (flags & FEXCL) { 1077c478bd9Sstevel@tonic-gate if (px_p->px_soft_state != PX_SOFT_STATE_CLOSED) { 1087c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1097c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "busy\n"); 1107c478bd9Sstevel@tonic-gate return (EBUSY); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_OPEN_EXCL; 1137c478bd9Sstevel@tonic-gate } else { 1147c478bd9Sstevel@tonic-gate if (px_p->px_soft_state == PX_SOFT_STATE_OPEN_EXCL) { 1157c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1167c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "busy\n"); 1177c478bd9Sstevel@tonic-gate return (EBUSY); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_OPEN; 1207c478bd9Sstevel@tonic-gate } 121b65731f1Skini 122b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 123b65731f1Skini if (rval = (pcihp_get_cb_ops())->cb_open(devp, flags, 124b65731f1Skini otyp, credp)) { 125b65731f1Skini px_p->px_soft_state = orig_px_soft_state; 126b65731f1Skini mutex_exit(&px_p->px_mutex); 127b65731f1Skini return (rval); 128b65731f1Skini } 129b65731f1Skini 1307c478bd9Sstevel@tonic-gate px_p->px_open_count++; 1317c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1327c478bd9Sstevel@tonic-gate return (0); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1377c478bd9Sstevel@tonic-gate static int 1387c478bd9Sstevel@tonic-gate px_close(dev_t dev, int flags, int otyp, cred_t *credp) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate px_t *px_p; 141b65731f1Skini int rval; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 1447c478bd9Sstevel@tonic-gate return (EINVAL); 1457c478bd9Sstevel@tonic-gate 14636fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(dev); 1477c478bd9Sstevel@tonic-gate if (px_p == NULL) 1487c478bd9Sstevel@tonic-gate return (ENXIO); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate DBG(DBG_CLOSE, px_p->px_dip, "dev=%x: flags=%x\n", dev, flags); 1517c478bd9Sstevel@tonic-gate mutex_enter(&px_p->px_mutex); 152b65731f1Skini 153b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 154b65731f1Skini if (rval = (pcihp_get_cb_ops())->cb_close(dev, flags, 155b65731f1Skini otyp, credp)) { 156b65731f1Skini mutex_exit(&px_p->px_mutex); 157b65731f1Skini return (rval); 158b65731f1Skini } 159b65731f1Skini 1607c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_CLOSED; 1617c478bd9Sstevel@tonic-gate px_p->px_open_count = 0; 1627c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1637c478bd9Sstevel@tonic-gate return (0); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1677c478bd9Sstevel@tonic-gate static int 1687c478bd9Sstevel@tonic-gate px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate px_t *px_p; 1717c478bd9Sstevel@tonic-gate dev_info_t *dip; 1727c478bd9Sstevel@tonic-gate struct devctl_iocdata *dcp; 1737c478bd9Sstevel@tonic-gate uint_t bus_state; 1747c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS; 17569cd775fSschwartz int minor = getminor(dev); 1767c478bd9Sstevel@tonic-gate 17736fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(dev); 1787c478bd9Sstevel@tonic-gate if (px_p == NULL) 1797c478bd9Sstevel@tonic-gate return (ENXIO); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate dip = px_p->px_dip; 1827c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd); 18369cd775fSschwartz 1847c478bd9Sstevel@tonic-gate #ifdef PX_DMA_TEST 1857c478bd9Sstevel@tonic-gate if (IS_DMATEST(cmd)) { 1867c478bd9Sstevel@tonic-gate *rvalp = px_dma_test(cmd, dip, px_p, arg); 1877c478bd9Sstevel@tonic-gate return (0); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate #endif /* PX_DMA_TEST */ 1907c478bd9Sstevel@tonic-gate 19169cd775fSschwartz switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) { 19269cd775fSschwartz 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate * PCI tools. 1957c478bd9Sstevel@tonic-gate */ 19669cd775fSschwartz case PCI_TOOL_REG_MINOR_NUM: 1977c478bd9Sstevel@tonic-gate 19869cd775fSschwartz switch (cmd) { 19969cd775fSschwartz case PCITOOL_DEVICE_SET_REG: 20069cd775fSschwartz case PCITOOL_DEVICE_GET_REG: 2017c478bd9Sstevel@tonic-gate 20269cd775fSschwartz /* Require full privileges. */ 20369cd775fSschwartz if (secpolicy_kmdb(credp)) 20469cd775fSschwartz rv = EPERM; 20569cd775fSschwartz else 20669cd775fSschwartz rv = pxtool_dev_reg_ops(dip, 20769cd775fSschwartz (void *)arg, cmd, mode); 20869cd775fSschwartz break; 2097c478bd9Sstevel@tonic-gate 21069cd775fSschwartz case PCITOOL_NEXUS_SET_REG: 21169cd775fSschwartz case PCITOOL_NEXUS_GET_REG: 2127c478bd9Sstevel@tonic-gate 21369cd775fSschwartz /* Require full privileges. */ 21469cd775fSschwartz if (secpolicy_kmdb(credp)) 21569cd775fSschwartz rv = EPERM; 21669cd775fSschwartz else 21769cd775fSschwartz rv = pxtool_bus_reg_ops(dip, 21869cd775fSschwartz (void *)arg, cmd, mode); 21969cd775fSschwartz break; 22069cd775fSschwartz 22169cd775fSschwartz default: 22269cd775fSschwartz rv = ENOTTY; 22369cd775fSschwartz } 2247c478bd9Sstevel@tonic-gate return (rv); 22569cd775fSschwartz 22669cd775fSschwartz case PCI_TOOL_INTR_MINOR_NUM: 22769cd775fSschwartz 22869cd775fSschwartz switch (cmd) { 22969cd775fSschwartz case PCITOOL_DEVICE_SET_INTR: 23069cd775fSschwartz 23169cd775fSschwartz /* 23269cd775fSschwartz * Require PRIV_SYS_RES_CONFIG, 23369cd775fSschwartz * same as psradm 23469cd775fSschwartz */ 23569cd775fSschwartz if (secpolicy_ponline(credp)) { 23669cd775fSschwartz rv = EPERM; 23769cd775fSschwartz break; 23869cd775fSschwartz } 23969cd775fSschwartz 24069cd775fSschwartz /*FALLTHRU*/ 24169cd775fSschwartz /* These require no special privileges. */ 24269cd775fSschwartz case PCITOOL_DEVICE_GET_INTR: 243*2917a9c9Sschwartz case PCITOOL_SYSTEM_INTR_INFO: 24469cd775fSschwartz rv = pxtool_intr(dip, (void *)arg, cmd, mode); 24569cd775fSschwartz break; 24669cd775fSschwartz 24769cd775fSschwartz default: 24869cd775fSschwartz rv = ENOTTY; 24969cd775fSschwartz } 2507c478bd9Sstevel@tonic-gate return (rv); 25169cd775fSschwartz 2527c478bd9Sstevel@tonic-gate default: 253b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 254b65731f1Skini return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, 255b65731f1Skini arg, mode, credp, rvalp)); 2567c478bd9Sstevel@tonic-gate break; 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 25969cd775fSschwartz if ((cmd & ~PPMREQ_MASK) == PPMREQ) { 26069cd775fSschwartz 26169cd775fSschwartz /* Need privileges to use these ioctls. */ 26269cd775fSschwartz if (drv_priv(credp)) { 26369cd775fSschwartz DBG(DBG_TOOLS, dip, 26469cd775fSschwartz "px_tools: Insufficient privileges\n"); 26569cd775fSschwartz 26669cd775fSschwartz return (EPERM); 26769cd775fSschwartz } 2687c478bd9Sstevel@tonic-gate return (px_lib_pmctl(cmd, px_p)); 26969cd775fSschwartz } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * We can use the generic implementation for these ioctls 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate switch (cmd) { 2757c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE: 2767c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE: 2777c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE: 2787c478bd9Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE: 2797c478bd9Sstevel@tonic-gate return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate /* 2837c478bd9Sstevel@tonic-gate * read devctl ioctl data 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 2867c478bd9Sstevel@tonic-gate return (EFAULT); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate switch (cmd) { 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_RESET: 2917c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n"); 2927c478bd9Sstevel@tonic-gate rv = ENOTSUP; 2937c478bd9Sstevel@tonic-gate break; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE: 2977c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n"); 2987c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 2997c478bd9Sstevel@tonic-gate if (bus_state == BUS_QUIESCED) 3007c478bd9Sstevel@tonic-gate break; 3017c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_QUIESCED); 3027c478bd9Sstevel@tonic-gate break; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE: 3057c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n"); 3067c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 3077c478bd9Sstevel@tonic-gate if (bus_state == BUS_ACTIVE) 3087c478bd9Sstevel@tonic-gate break; 3097c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_ACTIVE); 3107c478bd9Sstevel@tonic-gate break; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESET: 3137c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n"); 3147c478bd9Sstevel@tonic-gate rv = ENOTSUP; 3157c478bd9Sstevel@tonic-gate break; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESETALL: 3187c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n"); 3197c478bd9Sstevel@tonic-gate rv = ENOTSUP; 3207c478bd9Sstevel@tonic-gate break; 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate default: 3237c478bd9Sstevel@tonic-gate rv = ENOTTY; 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate ndi_dc_freehdl(dcp); 3277c478bd9Sstevel@tonic-gate return (rv); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 3317c478bd9Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3347c478bd9Sstevel@tonic-gate "hotplug-capable")) 3357c478bd9Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, 3367c478bd9Sstevel@tonic-gate prop_op, flags, name, valuep, lengthp)); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp)); 3397c478bd9Sstevel@tonic-gate } 340