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 /* 23*b65731f1Skini * Copyright 2006 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> 47d4476ccbSschwartz #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; 85*b65731f1Skini int rval; 86*b65731f1Skini uint_t orig_px_soft_state; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * Make sure the open is for the right file type. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 927c478bd9Sstevel@tonic-gate return (EINVAL); 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * Get the soft state structure for the device. 967c478bd9Sstevel@tonic-gate */ 9736fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(*devp); 987c478bd9Sstevel@tonic-gate if (px_p == NULL) 997c478bd9Sstevel@tonic-gate return (ENXIO); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Handle the open by tracking the device state. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "devp=%x: flags=%x\n", devp, flags); 1057c478bd9Sstevel@tonic-gate mutex_enter(&px_p->px_mutex); 106*b65731f1Skini orig_px_soft_state = px_p->px_soft_state; 1077c478bd9Sstevel@tonic-gate if (flags & FEXCL) { 1087c478bd9Sstevel@tonic-gate if (px_p->px_soft_state != PX_SOFT_STATE_CLOSED) { 1097c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1107c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "busy\n"); 1117c478bd9Sstevel@tonic-gate return (EBUSY); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_OPEN_EXCL; 1147c478bd9Sstevel@tonic-gate } else { 1157c478bd9Sstevel@tonic-gate if (px_p->px_soft_state == PX_SOFT_STATE_OPEN_EXCL) { 1167c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1177c478bd9Sstevel@tonic-gate DBG(DBG_OPEN, px_p->px_dip, "busy\n"); 1187c478bd9Sstevel@tonic-gate return (EBUSY); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_OPEN; 1217c478bd9Sstevel@tonic-gate } 122*b65731f1Skini 123*b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 124*b65731f1Skini if (rval = (pcihp_get_cb_ops())->cb_open(devp, flags, 125*b65731f1Skini otyp, credp)) { 126*b65731f1Skini px_p->px_soft_state = orig_px_soft_state; 127*b65731f1Skini mutex_exit(&px_p->px_mutex); 128*b65731f1Skini return (rval); 129*b65731f1Skini } 130*b65731f1Skini 1317c478bd9Sstevel@tonic-gate px_p->px_open_count++; 1327c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1337c478bd9Sstevel@tonic-gate return (0); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1387c478bd9Sstevel@tonic-gate static int 1397c478bd9Sstevel@tonic-gate px_close(dev_t dev, int flags, int otyp, cred_t *credp) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate px_t *px_p; 142*b65731f1Skini int rval; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 1457c478bd9Sstevel@tonic-gate return (EINVAL); 1467c478bd9Sstevel@tonic-gate 14736fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(dev); 1487c478bd9Sstevel@tonic-gate if (px_p == NULL) 1497c478bd9Sstevel@tonic-gate return (ENXIO); 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate DBG(DBG_CLOSE, px_p->px_dip, "dev=%x: flags=%x\n", dev, flags); 1527c478bd9Sstevel@tonic-gate mutex_enter(&px_p->px_mutex); 153*b65731f1Skini 154*b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 155*b65731f1Skini if (rval = (pcihp_get_cb_ops())->cb_close(dev, flags, 156*b65731f1Skini otyp, credp)) { 157*b65731f1Skini mutex_exit(&px_p->px_mutex); 158*b65731f1Skini return (rval); 159*b65731f1Skini } 160*b65731f1Skini 1617c478bd9Sstevel@tonic-gate px_p->px_soft_state = PX_SOFT_STATE_CLOSED; 1627c478bd9Sstevel@tonic-gate px_p->px_open_count = 0; 1637c478bd9Sstevel@tonic-gate mutex_exit(&px_p->px_mutex); 1647c478bd9Sstevel@tonic-gate return (0); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1687c478bd9Sstevel@tonic-gate static int 1697c478bd9Sstevel@tonic-gate px_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate px_t *px_p; 1727c478bd9Sstevel@tonic-gate dev_info_t *dip; 1737c478bd9Sstevel@tonic-gate struct devctl_iocdata *dcp; 1747c478bd9Sstevel@tonic-gate uint_t bus_state; 1757c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS; 17669cd775fSschwartz int minor = getminor(dev); 1777c478bd9Sstevel@tonic-gate 17836fe4a92Segillett px_p = PX_DEV_TO_SOFTSTATE(dev); 1797c478bd9Sstevel@tonic-gate if (px_p == NULL) 1807c478bd9Sstevel@tonic-gate return (ENXIO); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate dip = px_p->px_dip; 1837c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd); 18469cd775fSschwartz 1857c478bd9Sstevel@tonic-gate #ifdef PX_DMA_TEST 1867c478bd9Sstevel@tonic-gate if (IS_DMATEST(cmd)) { 1877c478bd9Sstevel@tonic-gate *rvalp = px_dma_test(cmd, dip, px_p, arg); 1887c478bd9Sstevel@tonic-gate return (0); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate #endif /* PX_DMA_TEST */ 1917c478bd9Sstevel@tonic-gate 19269cd775fSschwartz switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) { 19369cd775fSschwartz 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * PCI tools. 1967c478bd9Sstevel@tonic-gate */ 19769cd775fSschwartz case PCI_TOOL_REG_MINOR_NUM: 1987c478bd9Sstevel@tonic-gate 19969cd775fSschwartz switch (cmd) { 20069cd775fSschwartz case PCITOOL_DEVICE_SET_REG: 20169cd775fSschwartz case PCITOOL_DEVICE_GET_REG: 2027c478bd9Sstevel@tonic-gate 20369cd775fSschwartz /* Require full privileges. */ 20469cd775fSschwartz if (secpolicy_kmdb(credp)) 20569cd775fSschwartz rv = EPERM; 20669cd775fSschwartz else 20769cd775fSschwartz rv = pxtool_dev_reg_ops(dip, 20869cd775fSschwartz (void *)arg, cmd, mode); 20969cd775fSschwartz break; 2107c478bd9Sstevel@tonic-gate 21169cd775fSschwartz case PCITOOL_NEXUS_SET_REG: 21269cd775fSschwartz case PCITOOL_NEXUS_GET_REG: 2137c478bd9Sstevel@tonic-gate 21469cd775fSschwartz /* Require full privileges. */ 21569cd775fSschwartz if (secpolicy_kmdb(credp)) 21669cd775fSschwartz rv = EPERM; 21769cd775fSschwartz else 21869cd775fSschwartz rv = pxtool_bus_reg_ops(dip, 21969cd775fSschwartz (void *)arg, cmd, mode); 22069cd775fSschwartz break; 22169cd775fSschwartz 22269cd775fSschwartz default: 22369cd775fSschwartz rv = ENOTTY; 22469cd775fSschwartz } 2257c478bd9Sstevel@tonic-gate return (rv); 22669cd775fSschwartz 22769cd775fSschwartz case PCI_TOOL_INTR_MINOR_NUM: 22869cd775fSschwartz 22969cd775fSschwartz switch (cmd) { 23069cd775fSschwartz case PCITOOL_DEVICE_SET_INTR: 23169cd775fSschwartz 23269cd775fSschwartz /* 23369cd775fSschwartz * Require PRIV_SYS_RES_CONFIG, 23469cd775fSschwartz * same as psradm 23569cd775fSschwartz */ 23669cd775fSschwartz if (secpolicy_ponline(credp)) { 23769cd775fSschwartz rv = EPERM; 23869cd775fSschwartz break; 23969cd775fSschwartz } 24069cd775fSschwartz 24169cd775fSschwartz /*FALLTHRU*/ 24269cd775fSschwartz /* These require no special privileges. */ 24369cd775fSschwartz case PCITOOL_DEVICE_GET_INTR: 24469cd775fSschwartz case PCITOOL_DEVICE_NUM_INTR: 24569cd775fSschwartz rv = pxtool_intr(dip, (void *)arg, cmd, mode); 24669cd775fSschwartz break; 24769cd775fSschwartz 24869cd775fSschwartz default: 24969cd775fSschwartz rv = ENOTTY; 25069cd775fSschwartz } 2517c478bd9Sstevel@tonic-gate return (rv); 25269cd775fSschwartz 2537c478bd9Sstevel@tonic-gate default: 254*b65731f1Skini if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE) 255*b65731f1Skini return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, 256*b65731f1Skini arg, mode, credp, rvalp)); 2577c478bd9Sstevel@tonic-gate break; 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 26069cd775fSschwartz if ((cmd & ~PPMREQ_MASK) == PPMREQ) { 26169cd775fSschwartz 26269cd775fSschwartz /* Need privileges to use these ioctls. */ 26369cd775fSschwartz if (drv_priv(credp)) { 26469cd775fSschwartz DBG(DBG_TOOLS, dip, 26569cd775fSschwartz "px_tools: Insufficient privileges\n"); 26669cd775fSschwartz 26769cd775fSschwartz return (EPERM); 26869cd775fSschwartz } 2697c478bd9Sstevel@tonic-gate return (px_lib_pmctl(cmd, px_p)); 27069cd775fSschwartz } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* 2737c478bd9Sstevel@tonic-gate * We can use the generic implementation for these ioctls 2747c478bd9Sstevel@tonic-gate */ 2757c478bd9Sstevel@tonic-gate switch (cmd) { 2767c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE: 2777c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE: 2787c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE: 2797c478bd9Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE: 2807c478bd9Sstevel@tonic-gate return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate /* 2847c478bd9Sstevel@tonic-gate * read devctl ioctl data 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 2877c478bd9Sstevel@tonic-gate return (EFAULT); 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate switch (cmd) { 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_RESET: 2927c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n"); 2937c478bd9Sstevel@tonic-gate rv = ENOTSUP; 2947c478bd9Sstevel@tonic-gate break; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE: 2987c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n"); 2997c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 3007c478bd9Sstevel@tonic-gate if (bus_state == BUS_QUIESCED) 3017c478bd9Sstevel@tonic-gate break; 3027c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_QUIESCED); 3037c478bd9Sstevel@tonic-gate break; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE: 3067c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n"); 3077c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 3087c478bd9Sstevel@tonic-gate if (bus_state == BUS_ACTIVE) 3097c478bd9Sstevel@tonic-gate break; 3107c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_ACTIVE); 3117c478bd9Sstevel@tonic-gate break; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESET: 3147c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n"); 3157c478bd9Sstevel@tonic-gate rv = ENOTSUP; 3167c478bd9Sstevel@tonic-gate break; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESETALL: 3197c478bd9Sstevel@tonic-gate DBG(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n"); 3207c478bd9Sstevel@tonic-gate rv = ENOTSUP; 3217c478bd9Sstevel@tonic-gate break; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate default: 3247c478bd9Sstevel@tonic-gate rv = ENOTTY; 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate ndi_dc_freehdl(dcp); 3287c478bd9Sstevel@tonic-gate return (rv); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate static int px_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 3327c478bd9Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp) 3337c478bd9Sstevel@tonic-gate { 3347c478bd9Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3357c478bd9Sstevel@tonic-gate "hotplug-capable")) 3367c478bd9Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, 3377c478bd9Sstevel@tonic-gate prop_op, flags, name, valuep, lengthp)); 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp)); 3407c478bd9Sstevel@tonic-gate } 341