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
52917a9c9Sschwartz * Common Development and Distribution License (the "License").
62917a9c9Sschwartz * 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*26947304SEvan Yan * 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/pci/pci_obj.h>
387c478bd9Sstevel@tonic-gate #include <sys/pci_tools.h>
39d4476ccbSschwartz #include <sys/pci/pci_tools_ext.h>
407c478bd9Sstevel@tonic-gate #include <sys/open.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <sys/policy.h>
447c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static int pci_open(dev_t *devp, int flags, int otyp, cred_t *credp);
497c478bd9Sstevel@tonic-gate static int pci_close(dev_t dev, int flags, int otyp, cred_t *credp);
507851eb82Sschwartz static int pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode,
517851eb82Sschwartz cred_t *credp, int *rvalp);
527c478bd9Sstevel@tonic-gate static int pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
537c478bd9Sstevel@tonic-gate cred_t *credp, int *rvalp);
547c478bd9Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
557c478bd9Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp);
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate struct cb_ops pci_cb_ops = {
587c478bd9Sstevel@tonic-gate pci_open, /* open */
597c478bd9Sstevel@tonic-gate pci_close, /* close */
607c478bd9Sstevel@tonic-gate nodev, /* strategy */
617c478bd9Sstevel@tonic-gate nodev, /* print */
627c478bd9Sstevel@tonic-gate nodev, /* dump */
637c478bd9Sstevel@tonic-gate nodev, /* read */
647c478bd9Sstevel@tonic-gate nodev, /* write */
657c478bd9Sstevel@tonic-gate pci_ioctl, /* ioctl */
667c478bd9Sstevel@tonic-gate nodev, /* devmap */
677c478bd9Sstevel@tonic-gate nodev, /* mmap */
687c478bd9Sstevel@tonic-gate nodev, /* segmap */
697c478bd9Sstevel@tonic-gate nochpoll, /* poll */
707c478bd9Sstevel@tonic-gate pci_prop_op, /* cb_prop_op */
717c478bd9Sstevel@tonic-gate NULL, /* streamtab */
727c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */
737c478bd9Sstevel@tonic-gate CB_REV, /* rev */
747c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
757c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate
78e389d146Sschwartz extern struct cb_ops *pcihp_ops;
79e389d146Sschwartz
807c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
817c478bd9Sstevel@tonic-gate static int
pci_open(dev_t * devp,int flags,int otyp,cred_t * credp)827c478bd9Sstevel@tonic-gate pci_open(dev_t *devp, int flags, int otyp, cred_t *credp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate pci_t *pci_p;
85e389d146Sschwartz int rval;
86e389d146Sschwartz uint_t orig_pci_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 */
977c478bd9Sstevel@tonic-gate pci_p = DEV_TO_SOFTSTATE(*devp);
987c478bd9Sstevel@tonic-gate if (pci_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 DEBUG2(DBG_OPEN, pci_p->pci_dip, "devp=%x: flags=%x\n", devp, flags);
1057c478bd9Sstevel@tonic-gate mutex_enter(&pci_p->pci_mutex);
106e389d146Sschwartz orig_pci_soft_state = pci_p->pci_soft_state;
1077c478bd9Sstevel@tonic-gate if (flags & FEXCL) {
1087c478bd9Sstevel@tonic-gate if (pci_p->pci_soft_state != PCI_SOFT_STATE_CLOSED) {
1097c478bd9Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex);
1107c478bd9Sstevel@tonic-gate DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n");
1117c478bd9Sstevel@tonic-gate return (EBUSY);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN_EXCL;
1147c478bd9Sstevel@tonic-gate } else {
1157c478bd9Sstevel@tonic-gate if (pci_p->pci_soft_state == PCI_SOFT_STATE_OPEN_EXCL) {
1167c478bd9Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex);
1177c478bd9Sstevel@tonic-gate DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n");
1187c478bd9Sstevel@tonic-gate return (EBUSY);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN;
1217c478bd9Sstevel@tonic-gate }
122e389d146Sschwartz
123e389d146Sschwartz if (pci_p->hotplug_capable == B_TRUE) {
124e389d146Sschwartz if (rval = pcihp_ops->cb_open(devp, flags, otyp, credp)) {
125e389d146Sschwartz pci_p->pci_soft_state = orig_pci_soft_state;
126e389d146Sschwartz mutex_exit(&pci_p->pci_mutex);
127e389d146Sschwartz return (rval);
128e389d146Sschwartz }
129e389d146Sschwartz }
130e389d146Sschwartz
1317c478bd9Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex);
132e389d146Sschwartz
1337c478bd9Sstevel@tonic-gate return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /* ARGSUSED */
1387c478bd9Sstevel@tonic-gate static int
pci_close(dev_t dev,int flags,int otyp,cred_t * credp)1397c478bd9Sstevel@tonic-gate pci_close(dev_t dev, int flags, int otyp, cred_t *credp)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate pci_t *pci_p;
142e389d146Sschwartz int rval;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR)
1457c478bd9Sstevel@tonic-gate return (EINVAL);
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate pci_p = DEV_TO_SOFTSTATE(dev);
1487c478bd9Sstevel@tonic-gate if (pci_p == NULL)
1497c478bd9Sstevel@tonic-gate return (ENXIO);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate DEBUG2(DBG_CLOSE, pci_p->pci_dip, "dev=%x: flags=%x\n", dev, flags);
1527c478bd9Sstevel@tonic-gate mutex_enter(&pci_p->pci_mutex);
153e389d146Sschwartz
154e389d146Sschwartz if (pci_p->hotplug_capable == B_TRUE)
155e389d146Sschwartz if (rval = pcihp_ops->cb_close(dev, flags, otyp, credp)) {
156e389d146Sschwartz mutex_exit(&pci_p->pci_mutex);
157e389d146Sschwartz return (rval);
158e389d146Sschwartz }
159e389d146Sschwartz
1607c478bd9Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED;
1617c478bd9Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex);
1627c478bd9Sstevel@tonic-gate return (0);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /* ARGSUSED */
1667c478bd9Sstevel@tonic-gate static int
pci_devctl_ioctl(dev_info_t * dip,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)1677851eb82Sschwartz pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode,
1687851eb82Sschwartz cred_t *credp, int *rvalp)
1697c478bd9Sstevel@tonic-gate {
1707851eb82Sschwartz int rv = 0;
1717c478bd9Sstevel@tonic-gate struct devctl_iocdata *dcp;
1727c478bd9Sstevel@tonic-gate uint_t bus_state;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * We can use the generic implementation for these ioctls
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate switch (cmd) {
1787c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE:
1797c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE:
1807c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE:
1817c478bd9Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE:
1827c478bd9Sstevel@tonic-gate return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * read devctl ioctl data
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1897c478bd9Sstevel@tonic-gate return (EFAULT);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate switch (cmd) {
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate case DEVCTL_DEVICE_RESET:
1947c478bd9Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n");
1957c478bd9Sstevel@tonic-gate rv = ENOTSUP;
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE:
2007c478bd9Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n");
2017c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2027c478bd9Sstevel@tonic-gate if (bus_state == BUS_QUIESCED)
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_QUIESCED);
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE:
2087c478bd9Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n");
2097c478bd9Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
2107c478bd9Sstevel@tonic-gate if (bus_state == BUS_ACTIVE)
2117c478bd9Sstevel@tonic-gate break;
2127c478bd9Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_ACTIVE);
2137c478bd9Sstevel@tonic-gate break;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESET:
2167c478bd9Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n");
2177c478bd9Sstevel@tonic-gate rv = ENOTSUP;
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate case DEVCTL_BUS_RESETALL:
2217c478bd9Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n");
2227c478bd9Sstevel@tonic-gate rv = ENOTSUP;
2237c478bd9Sstevel@tonic-gate break;
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate default:
2267c478bd9Sstevel@tonic-gate rv = ENOTTY;
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate ndi_dc_freehdl(dcp);
2307c478bd9Sstevel@tonic-gate return (rv);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337851eb82Sschwartz
2347851eb82Sschwartz static int
pci_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2357851eb82Sschwartz pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
2367851eb82Sschwartz {
2377851eb82Sschwartz pci_t *pci_p;
2387851eb82Sschwartz dev_info_t *dip;
2397851eb82Sschwartz minor_t minor = getminor(dev);
2407851eb82Sschwartz int rv = ENOTTY;
2417851eb82Sschwartz
2427851eb82Sschwartz pci_p = DEV_TO_SOFTSTATE(dev);
2437851eb82Sschwartz if (pci_p == NULL)
2447851eb82Sschwartz return (ENXIO);
2457851eb82Sschwartz
2467851eb82Sschwartz dip = pci_p->pci_dip;
2477851eb82Sschwartz DEBUG2(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd);
2487851eb82Sschwartz
2497851eb82Sschwartz #ifdef PCI_DMA_TEST
2507851eb82Sschwartz if (IS_DMATEST(cmd)) {
2517851eb82Sschwartz *rvalp = pci_dma_test(cmd, dip, pci_p, arg);
2527851eb82Sschwartz return (0);
2537851eb82Sschwartz }
2547851eb82Sschwartz #endif
2557851eb82Sschwartz
2567851eb82Sschwartz switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) {
2577851eb82Sschwartz case PCI_TOOL_REG_MINOR_NUM:
2587851eb82Sschwartz
2597851eb82Sschwartz switch (cmd) {
2607851eb82Sschwartz case PCITOOL_DEVICE_SET_REG:
2617851eb82Sschwartz case PCITOOL_DEVICE_GET_REG:
2627851eb82Sschwartz
2637851eb82Sschwartz /* Require full privileges. */
2647851eb82Sschwartz if (secpolicy_kmdb(credp))
2657851eb82Sschwartz rv = EPERM;
2667851eb82Sschwartz else
2677851eb82Sschwartz rv = pcitool_dev_reg_ops(
2687851eb82Sschwartz dev, (void *)arg, cmd, mode);
2697851eb82Sschwartz break;
2707851eb82Sschwartz
2717851eb82Sschwartz case PCITOOL_NEXUS_SET_REG:
2727851eb82Sschwartz case PCITOOL_NEXUS_GET_REG:
2737851eb82Sschwartz
2747851eb82Sschwartz /* Require full privileges. */
2757851eb82Sschwartz if (secpolicy_kmdb(credp))
2767851eb82Sschwartz rv = EPERM;
2777851eb82Sschwartz else
2787851eb82Sschwartz rv = pcitool_bus_reg_ops(
2797851eb82Sschwartz dev, (void *)arg, cmd, mode);
2807851eb82Sschwartz break;
2817851eb82Sschwartz }
2827851eb82Sschwartz
2837851eb82Sschwartz break;
2847851eb82Sschwartz
2857851eb82Sschwartz case PCI_TOOL_INTR_MINOR_NUM:
2867851eb82Sschwartz
2877851eb82Sschwartz switch (cmd) {
2887851eb82Sschwartz case PCITOOL_DEVICE_SET_INTR:
2897851eb82Sschwartz
2907851eb82Sschwartz /* Require PRIV_SYS_RES_CONFIG, same as psradm */
2917851eb82Sschwartz if (secpolicy_ponline(credp)) {
2927851eb82Sschwartz rv = EPERM;
2937851eb82Sschwartz break;
2947851eb82Sschwartz }
2957851eb82Sschwartz
2967851eb82Sschwartz /*FALLTHRU*/
2977851eb82Sschwartz /* These require no special privileges. */
2987851eb82Sschwartz case PCITOOL_DEVICE_GET_INTR:
2992917a9c9Sschwartz case PCITOOL_SYSTEM_INTR_INFO:
3007851eb82Sschwartz rv = pcitool_intr_admn(dev, (void *)arg, cmd, mode);
3017851eb82Sschwartz break;
3027851eb82Sschwartz }
3037851eb82Sschwartz
3047851eb82Sschwartz break;
3057851eb82Sschwartz
3068d03b6dbSschwartz /*
3078d03b6dbSschwartz * All non-PCItool ioctls go through here, including:
3088d03b6dbSschwartz * devctl ioctls with minor number PCIHP_DEVCTL_MINOR and
3098d03b6dbSschwartz * those for attachment points with where minor number is the
3108d03b6dbSschwartz * device number.
3118d03b6dbSschwartz */
3128d03b6dbSschwartz default:
313e389d146Sschwartz if (pci_p->hotplug_capable == B_TRUE)
314e389d146Sschwartz rv = pcihp_ops->cb_ioctl(
315e389d146Sschwartz dev, cmd, arg, mode, credp, rvalp);
316e389d146Sschwartz else
317e389d146Sschwartz rv = pci_devctl_ioctl(
318e389d146Sschwartz dip, cmd, arg, mode, credp, rvalp);
3197851eb82Sschwartz break;
3207851eb82Sschwartz }
3217851eb82Sschwartz
3227851eb82Sschwartz return (rv);
3237851eb82Sschwartz }
3247851eb82Sschwartz
pci_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int flags,char * name,caddr_t valuep,int * lengthp)3257c478bd9Sstevel@tonic-gate static int pci_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