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 /* 22d5ace945SErwin T Tsaur * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * PCI nexus HotPlug devctl interface 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/conf.h> 317c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 327c478bd9Sstevel@tonic-gate #include <sys/async.h> 337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 357c478bd9Sstevel@tonic-gate #include <sys/sunndi.h> 367c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 377c478bd9Sstevel@tonic-gate #include <sys/open.h> 387c478bd9Sstevel@tonic-gate #include <sys/errno.h> 397c478bd9Sstevel@tonic-gate #include <sys/file.h> 4069cd775fSschwartz #include <sys/policy.h> 417c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 427c478bd9Sstevel@tonic-gate #include "px_obj.h" 4369cd775fSschwartz #include <sys/pci_tools.h> 44d4476ccbSschwartz #include "px_tools_ext.h" 45*d4bc0535SKrishna Elango #include <sys/pcie_pwr.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate static int px_open(dev_t *devp, int flags, int otyp, cred_t *credp); 507c478bd9Sstevel@tonic-gate static int px_close(dev_t dev, int flags, int otyp, cred_t *credp); 517c478bd9Sstevel@tonic-gate static int px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 527c478bd9Sstevel@tonic-gate cred_t *credp, int *rvalp); 537c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 547c478bd9Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp); 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate struct cb_ops px_cb_ops = { 577c478bd9Sstevel@tonic-gate px_open, /* open */ 587c478bd9Sstevel@tonic-gate px_close, /* close */ 597c478bd9Sstevel@tonic-gate nodev, /* strategy */ 607c478bd9Sstevel@tonic-gate nodev, /* print */ 617c478bd9Sstevel@tonic-gate nodev, /* dump */ 627c478bd9Sstevel@tonic-gate nodev, /* read */ 637c478bd9Sstevel@tonic-gate nodev, /* write */ 647c478bd9Sstevel@tonic-gate px_ioctl, /* ioctl */ 657c478bd9Sstevel@tonic-gate nodev, /* devmap */ 667c478bd9Sstevel@tonic-gate nodev, /* mmap */ 677c478bd9Sstevel@tonic-gate nodev, /* segmap */ 687c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 697c478bd9Sstevel@tonic-gate px_prop_op, /* cb_prop_op */ 707c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 717c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 727c478bd9Sstevel@tonic-gate CB_REV, /* rev */ 737c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 747c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 757c478bd9Sstevel@tonic-gate }; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* ARGSUSED3 */ 787c478bd9Sstevel@tonic-gate static int 797c478bd9Sstevel@tonic-gate px_open(dev_t *devp, int flags, int otyp, cred_t *credp) 807c478bd9Sstevel@tonic-gate { 817c478bd9Sstevel@tonic-gate px_t *px_p; 82b65731f1Skini int rval; 83b65731f1Skini uint_t orig_px_soft_state; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * Make sure the open is for the right file type. 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 897c478bd9Sstevel@tonic-gate return (EINVAL); 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* 927c478bd9Sstevel@tonic-gate * Get the soft state structure for the device. 937c478bd9Sstevel@tonic-gate */ 9436fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(*devp); 957c478bd9Sstevel@tonic-gate if (px_p == NULL) 967c478bd9Sstevel@tonic-gate return (ENXIO); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * Handle the open by tracking the device state. 1007c478bd9Sstevel@tonic-gate */ 1017c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "devp=%x: flags=%x\n", devp, flags); 1027c478bd9Sstevel@tonic-gate mutex_enter(&px_p->px_mutex); 103b65731f1Skini orig_px_soft_state = px_p->px_soft_state; 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 } 119b65731f1Skini 120b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 121b65731f1Skini if (rval = (pcihp_get_cb_ops())->cb_open(devp, flags, 122b65731f1Skini otyp, credp)) { 123b65731f1Skini px_p->px_soft_state = orig_px_soft_state; 124b65731f1Skini mutex_exit(&px_p->px_mutex); 125b65731f1Skini return (rval); 126b65731f1Skini } 127b65731f1Skini 1287c478bd9Sstevel@tonic-gate px_p->px_open_count++; 1297c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1307c478bd9Sstevel@tonic-gate return (0); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1357c478bd9Sstevel@tonic-gate static int 1367c478bd9Sstevel@tonic-gate px_close(dev_t dev, int flags, int otyp, cred_t *credp) 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate px_t *px_p; 139b65731f1Skini int rval; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 1427c478bd9Sstevel@tonic-gate return (EINVAL); 1437c478bd9Sstevel@tonic-gate 14436fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(dev); 1457c478bd9Sstevel@tonic-gate if (px_p == NULL) 1467c478bd9Sstevel@tonic-gate return (ENXIO); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate DBG(DBG_CLOSE, px_p->px_dip, "dev=%x: flags=%x\n", dev, flags); 1497c478bd9Sstevel@tonic-gate mutex_enter(&px_p->px_mutex); 150b65731f1Skini 151b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 152b65731f1Skini if (rval = (pcihp_get_cb_ops())->cb_close(dev, flags, 153b65731f1Skini otyp, credp)) { 154b65731f1Skini mutex_exit(&px_p->px_mutex); 155b65731f1Skini return (rval); 156b65731f1Skini } 157b65731f1Skini 1587c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_CLOSED; 1597c478bd9Sstevel@tonic-gate px_p->px_open_count = 0; 1607c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1617c478bd9Sstevel@tonic-gate return (0); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1657c478bd9Sstevel@tonic-gate static int 1667c478bd9Sstevel@tonic-gate px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate px_t *px_p; 1697c478bd9Sstevel@tonic-gate dev_info_t *dip; 1707c478bd9Sstevel@tonic-gate struct devctl_iocdata *dcp; 1717c478bd9Sstevel@tonic-gate uint_t bus_state; 1727c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS; 17369cd775fSschwartz int minor = getminor(dev); 1747c478bd9Sstevel@tonic-gate 17536fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(dev); 1767c478bd9Sstevel@tonic-gate if (px_p == NULL) 1777c478bd9Sstevel@tonic-gate return (ENXIO); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate dip = px_p->px_dip; 1807c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd); 18169cd775fSschwartz 1827c478bd9Sstevel@tonic-gate #ifdef PX_DMA_TEST 1837c478bd9Sstevel@tonic-gate if (IS_DMATEST(cmd)) { 1847c478bd9Sstevel@tonic-gate *rvalp = px_dma_test(cmd, dip, px_p, arg); 1857c478bd9Sstevel@tonic-gate return (0); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate #endif /* PX_DMA_TEST */ 1887c478bd9Sstevel@tonic-gate 18969cd775fSschwartz switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) { 19069cd775fSschwartz 1917c478bd9Sstevel@tonic-gate /* 1927c478bd9Sstevel@tonic-gate * PCI tools. 1937c478bd9Sstevel@tonic-gate */ 19469cd775fSschwartz case PCI_TOOL_REG_MINOR_NUM: 1957c478bd9Sstevel@tonic-gate 19669cd775fSschwartz switch (cmd) { 19769cd775fSschwartz case PCITOOL_DEVICE_SET_REG: 19869cd775fSschwartz case PCITOOL_DEVICE_GET_REG: 1997c478bd9Sstevel@tonic-gate 20069cd775fSschwartz /* Require full privileges. */ 20169cd775fSschwartz if (secpolicy_kmdb(credp)) 20269cd775fSschwartz rv = EPERM; 20369cd775fSschwartz else 20469cd775fSschwartz rv = pxtool_dev_reg_ops(dip, 20569cd775fSschwartz (void *)arg, cmd, mode); 20669cd775fSschwartz break; 2077c478bd9Sstevel@tonic-gate 20869cd775fSschwartz case PCITOOL_NEXUS_SET_REG: 20969cd775fSschwartz case PCITOOL_NEXUS_GET_REG: 2107c478bd9Sstevel@tonic-gate 21169cd775fSschwartz /* Require full privileges. */ 21269cd775fSschwartz if (secpolicy_kmdb(credp)) 21369cd775fSschwartz rv = EPERM; 21469cd775fSschwartz else 21569cd775fSschwartz rv = pxtool_bus_reg_ops(dip, 21669cd775fSschwartz (void *)arg, cmd, mode); 21769cd775fSschwartz break; 21869cd775fSschwartz 21969cd775fSschwartz default: 22069cd775fSschwartz rv = ENOTTY; 22169cd775fSschwartz } 2227c478bd9Sstevel@tonic-gate return (rv); 22369cd775fSschwartz 22469cd775fSschwartz case PCI_TOOL_INTR_MINOR_NUM: 22569cd775fSschwartz 22669cd775fSschwartz switch (cmd) { 22769cd775fSschwartz case PCITOOL_DEVICE_SET_INTR: 22869cd775fSschwartz 229d5ace945SErwin T Tsaur /* Require full privileges. */ 230d5ace945SErwin T Tsaur if (secpolicy_kmdb(credp)) { 231d5ace945SErwin T Tsaur rv = EPERM; 232d5ace945SErwin T Tsaur break; 233d5ace945SErwin T Tsaur } 23469cd775fSschwartz 23569cd775fSschwartz /*FALLTHRU*/ 23669cd775fSschwartz /* These require no special privileges. */ 23769cd775fSschwartz case PCITOOL_DEVICE_GET_INTR: 2382917a9c9Sschwartz case PCITOOL_SYSTEM_INTR_INFO: 23969cd775fSschwartz rv = pxtool_intr(dip, (void *)arg, cmd, mode); 24069cd775fSschwartz break; 24169cd775fSschwartz 24269cd775fSschwartz default: 24369cd775fSschwartz rv = ENOTTY; 24469cd775fSschwartz } 2457c478bd9Sstevel@tonic-gate return (rv); 24669cd775fSschwartz 2477c478bd9Sstevel@tonic-gate default: 248b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 249b65731f1Skini return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, 250b65731f1Skini arg, mode, credp, rvalp)); 2517c478bd9Sstevel@tonic-gate break; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 25469cd775fSschwartz if ((cmd & ~PPMREQ_MASK) == PPMREQ) { 25569cd775fSschwartz 25669cd775fSschwartz /* Need privileges to use these ioctls. */ 25769cd775fSschwartz if (drv_priv(credp)) { 25869cd775fSschwartz DBG(DBG_TOOLS, dip, 25969cd775fSschwartz "px_tools: Insufficient privileges\n"); 26069cd775fSschwartz 26169cd775fSschwartz return (EPERM); 26269cd775fSschwartz } 2637c478bd9Sstevel@tonic-gate return (px_lib_pmctl(cmd, px_p)); 26469cd775fSschwartz } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * We can use the generic implementation for these ioctls 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate switch (cmd) { 2707c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE: 2717c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE: 2727c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE: 2737c478bd9Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE: 2747c478bd9Sstevel@tonic-gate return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate /* 2787c478bd9Sstevel@tonic-gate * read devctl ioctl data 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 2817c478bd9Sstevel@tonic-gate return (EFAULT); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate switch (cmd) { 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_RESET: 2867c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n"); 2877c478bd9Sstevel@tonic-gate rv = ENOTSUP; 2887c478bd9Sstevel@tonic-gate break; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE: 2927c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n"); 2937c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 2947c478bd9Sstevel@tonic-gate if (bus_state == BUS_QUIESCED) 2957c478bd9Sstevel@tonic-gate break; 2967c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_QUIESCED); 2977c478bd9Sstevel@tonic-gate break; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE: 3007c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n"); 3017c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 3027c478bd9Sstevel@tonic-gate if (bus_state == BUS_ACTIVE) 3037c478bd9Sstevel@tonic-gate break; 3047c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_ACTIVE); 3057c478bd9Sstevel@tonic-gate break; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESET: 3087c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n"); 3097c478bd9Sstevel@tonic-gate rv = ENOTSUP; 3107c478bd9Sstevel@tonic-gate break; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESETALL: 3137c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n"); 3147c478bd9Sstevel@tonic-gate rv = ENOTSUP; 3157c478bd9Sstevel@tonic-gate break; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate default: 3187c478bd9Sstevel@tonic-gate rv = ENOTTY; 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate ndi_dc_freehdl(dcp); 3227c478bd9Sstevel@tonic-gate return (rv); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 3267c478bd9Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3297c478bd9Sstevel@tonic-gate "hotplug-capable")) 3307c478bd9Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, 3317c478bd9Sstevel@tonic-gate prop_op, flags, name, valuep, lengthp)); 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp)); 3347c478bd9Sstevel@tonic-gate } 335