10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * PCI nexus HotPlug devctl interface 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/conf.h> 340Sstevel@tonic-gate #include <sys/kmem.h> 350Sstevel@tonic-gate #include <sys/async.h> 360Sstevel@tonic-gate #include <sys/sysmacros.h> 370Sstevel@tonic-gate #include <sys/sunddi.h> 380Sstevel@tonic-gate #include <sys/sunndi.h> 390Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 400Sstevel@tonic-gate #include <sys/pci/pci_obj.h> 410Sstevel@tonic-gate #include <sys/pci_tools.h> 420Sstevel@tonic-gate #include <sys/pci_tools_var.h> 430Sstevel@tonic-gate #include <sys/open.h> 440Sstevel@tonic-gate #include <sys/errno.h> 450Sstevel@tonic-gate #include <sys/file.h> 460Sstevel@tonic-gate #include <sys/policy.h> 470Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate /*LINTLIBRARY*/ 500Sstevel@tonic-gate 510Sstevel@tonic-gate static int pci_open(dev_t *devp, int flags, int otyp, cred_t *credp); 520Sstevel@tonic-gate static int pci_close(dev_t dev, int flags, int otyp, cred_t *credp); 53117Sschwartz static int pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, 54117Sschwartz cred_t *credp, int *rvalp); 550Sstevel@tonic-gate static int pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 560Sstevel@tonic-gate cred_t *credp, int *rvalp); 570Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 580Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp); 590Sstevel@tonic-gate 600Sstevel@tonic-gate struct cb_ops pci_cb_ops = { 610Sstevel@tonic-gate pci_open, /* open */ 620Sstevel@tonic-gate pci_close, /* close */ 630Sstevel@tonic-gate nodev, /* strategy */ 640Sstevel@tonic-gate nodev, /* print */ 650Sstevel@tonic-gate nodev, /* dump */ 660Sstevel@tonic-gate nodev, /* read */ 670Sstevel@tonic-gate nodev, /* write */ 680Sstevel@tonic-gate pci_ioctl, /* ioctl */ 690Sstevel@tonic-gate nodev, /* devmap */ 700Sstevel@tonic-gate nodev, /* mmap */ 710Sstevel@tonic-gate nodev, /* segmap */ 720Sstevel@tonic-gate nochpoll, /* poll */ 730Sstevel@tonic-gate pci_prop_op, /* cb_prop_op */ 740Sstevel@tonic-gate NULL, /* streamtab */ 750Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 760Sstevel@tonic-gate CB_REV, /* rev */ 770Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 780Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 790Sstevel@tonic-gate }; 800Sstevel@tonic-gate 81*162Sschwartz extern struct cb_ops *pcihp_ops; 82*162Sschwartz 830Sstevel@tonic-gate /* ARGSUSED3 */ 840Sstevel@tonic-gate static int 850Sstevel@tonic-gate pci_open(dev_t *devp, int flags, int otyp, cred_t *credp) 860Sstevel@tonic-gate { 870Sstevel@tonic-gate pci_t *pci_p; 88*162Sschwartz int rval; 89*162Sschwartz uint_t orig_pci_soft_state; 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* 920Sstevel@tonic-gate * Make sure the open is for the right file type. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate if (otyp != OTYP_CHR) 950Sstevel@tonic-gate return (EINVAL); 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Get the soft state structure for the device. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate pci_p = DEV_TO_SOFTSTATE(*devp); 1010Sstevel@tonic-gate if (pci_p == NULL) 1020Sstevel@tonic-gate return (ENXIO); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* 1050Sstevel@tonic-gate * Handle the open by tracking the device state. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate DEBUG2(DBG_OPEN, pci_p->pci_dip, "devp=%x: flags=%x\n", devp, flags); 1080Sstevel@tonic-gate mutex_enter(&pci_p->pci_mutex); 109*162Sschwartz orig_pci_soft_state = pci_p->pci_soft_state; 1100Sstevel@tonic-gate if (flags & FEXCL) { 1110Sstevel@tonic-gate if (pci_p->pci_soft_state != PCI_SOFT_STATE_CLOSED) { 1120Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 1130Sstevel@tonic-gate DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n"); 1140Sstevel@tonic-gate return (EBUSY); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN_EXCL; 1170Sstevel@tonic-gate } else { 1180Sstevel@tonic-gate if (pci_p->pci_soft_state == PCI_SOFT_STATE_OPEN_EXCL) { 1190Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 1200Sstevel@tonic-gate DEBUG0(DBG_OPEN, pci_p->pci_dip, "busy\n"); 1210Sstevel@tonic-gate return (EBUSY); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_OPEN; 1240Sstevel@tonic-gate } 125*162Sschwartz 126*162Sschwartz if (pci_p->hotplug_capable == B_TRUE) { 127*162Sschwartz if (rval = pcihp_ops->cb_open(devp, flags, otyp, credp)) { 128*162Sschwartz pci_p->pci_soft_state = orig_pci_soft_state; 129*162Sschwartz mutex_exit(&pci_p->pci_mutex); 130*162Sschwartz return (rval); 131*162Sschwartz } 132*162Sschwartz } 133*162Sschwartz 1340Sstevel@tonic-gate pci_p->pci_open_count++; 1350Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 136*162Sschwartz 1370Sstevel@tonic-gate return (0); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* ARGSUSED */ 1420Sstevel@tonic-gate static int 1430Sstevel@tonic-gate pci_close(dev_t dev, int flags, int otyp, cred_t *credp) 1440Sstevel@tonic-gate { 1450Sstevel@tonic-gate pci_t *pci_p; 146*162Sschwartz int rval; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (otyp != OTYP_CHR) 1490Sstevel@tonic-gate return (EINVAL); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate pci_p = DEV_TO_SOFTSTATE(dev); 1520Sstevel@tonic-gate if (pci_p == NULL) 1530Sstevel@tonic-gate return (ENXIO); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate DEBUG2(DBG_CLOSE, pci_p->pci_dip, "dev=%x: flags=%x\n", dev, flags); 1560Sstevel@tonic-gate mutex_enter(&pci_p->pci_mutex); 157*162Sschwartz 158*162Sschwartz if (pci_p->hotplug_capable == B_TRUE) 159*162Sschwartz if (rval = pcihp_ops->cb_close(dev, flags, otyp, credp)) { 160*162Sschwartz mutex_exit(&pci_p->pci_mutex); 161*162Sschwartz return (rval); 162*162Sschwartz } 163*162Sschwartz 1640Sstevel@tonic-gate pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED; 1650Sstevel@tonic-gate pci_p->pci_open_count = 0; 1660Sstevel@tonic-gate mutex_exit(&pci_p->pci_mutex); 1670Sstevel@tonic-gate return (0); 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* ARGSUSED */ 1710Sstevel@tonic-gate static int 172117Sschwartz pci_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, 173117Sschwartz cred_t *credp, int *rvalp) 1740Sstevel@tonic-gate { 175117Sschwartz int rv = 0; 1760Sstevel@tonic-gate struct devctl_iocdata *dcp; 1770Sstevel@tonic-gate uint_t bus_state; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * We can use the generic implementation for these ioctls 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate switch (cmd) { 1830Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE: 1840Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE: 1850Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE: 1860Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE: 1870Sstevel@tonic-gate return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * read devctl ioctl data 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1940Sstevel@tonic-gate return (EFAULT); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate switch (cmd) { 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate case DEVCTL_DEVICE_RESET: 1990Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_DEVICE_RESET\n"); 2000Sstevel@tonic-gate rv = ENOTSUP; 2010Sstevel@tonic-gate break; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE: 2050Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_QUIESCE\n"); 2060Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 2070Sstevel@tonic-gate if (bus_state == BUS_QUIESCED) 2080Sstevel@tonic-gate break; 2090Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_QUIESCED); 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE: 2130Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_UNQUIESCE\n"); 2140Sstevel@tonic-gate if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS) 2150Sstevel@tonic-gate if (bus_state == BUS_ACTIVE) 2160Sstevel@tonic-gate break; 2170Sstevel@tonic-gate (void) ndi_set_bus_state(dip, BUS_ACTIVE); 2180Sstevel@tonic-gate break; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate case DEVCTL_BUS_RESET: 2210Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESET\n"); 2220Sstevel@tonic-gate rv = ENOTSUP; 2230Sstevel@tonic-gate break; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate case DEVCTL_BUS_RESETALL: 2260Sstevel@tonic-gate DEBUG0(DBG_IOCTL, dip, "DEVCTL_BUS_RESETALL\n"); 2270Sstevel@tonic-gate rv = ENOTSUP; 2280Sstevel@tonic-gate break; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate default: 2310Sstevel@tonic-gate rv = ENOTTY; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate ndi_dc_freehdl(dcp); 2350Sstevel@tonic-gate return (rv); 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 238117Sschwartz 239117Sschwartz static int 240117Sschwartz pci_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 241117Sschwartz { 242117Sschwartz pci_t *pci_p; 243117Sschwartz dev_info_t *dip; 244117Sschwartz minor_t minor = getminor(dev); 245117Sschwartz int rv = ENOTTY; 246117Sschwartz 247117Sschwartz pci_p = DEV_TO_SOFTSTATE(dev); 248117Sschwartz if (pci_p == NULL) 249117Sschwartz return (ENXIO); 250117Sschwartz 251117Sschwartz dip = pci_p->pci_dip; 252117Sschwartz DEBUG2(DBG_IOCTL, dip, "dev=%x: cmd=%x\n", dev, cmd); 253117Sschwartz 254117Sschwartz #ifdef PCI_DMA_TEST 255117Sschwartz if (IS_DMATEST(cmd)) { 256117Sschwartz *rvalp = pci_dma_test(cmd, dip, pci_p, arg); 257117Sschwartz return (0); 258117Sschwartz } 259117Sschwartz #endif 260117Sschwartz 261117Sschwartz switch (PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor)) { 262117Sschwartz case PCI_TOOL_REG_MINOR_NUM: 263117Sschwartz 264117Sschwartz switch (cmd) { 265117Sschwartz case PCITOOL_DEVICE_SET_REG: 266117Sschwartz case PCITOOL_DEVICE_GET_REG: 267117Sschwartz 268117Sschwartz /* Require full privileges. */ 269117Sschwartz if (secpolicy_kmdb(credp)) 270117Sschwartz rv = EPERM; 271117Sschwartz else 272117Sschwartz rv = pcitool_dev_reg_ops( 273117Sschwartz dev, (void *)arg, cmd, mode); 274117Sschwartz break; 275117Sschwartz 276117Sschwartz case PCITOOL_NEXUS_SET_REG: 277117Sschwartz case PCITOOL_NEXUS_GET_REG: 278117Sschwartz 279117Sschwartz /* Require full privileges. */ 280117Sschwartz if (secpolicy_kmdb(credp)) 281117Sschwartz rv = EPERM; 282117Sschwartz else 283117Sschwartz rv = pcitool_bus_reg_ops( 284117Sschwartz dev, (void *)arg, cmd, mode); 285117Sschwartz break; 286117Sschwartz } 287117Sschwartz 288117Sschwartz break; 289117Sschwartz 290117Sschwartz case PCI_TOOL_INTR_MINOR_NUM: 291117Sschwartz 292117Sschwartz switch (cmd) { 293117Sschwartz case PCITOOL_DEVICE_SET_INTR: 294117Sschwartz 295117Sschwartz /* Require PRIV_SYS_RES_CONFIG, same as psradm */ 296117Sschwartz if (secpolicy_ponline(credp)) { 297117Sschwartz rv = EPERM; 298117Sschwartz break; 299117Sschwartz } 300117Sschwartz 301117Sschwartz /*FALLTHRU*/ 302117Sschwartz /* These require no special privileges. */ 303117Sschwartz case PCITOOL_DEVICE_GET_INTR: 304117Sschwartz case PCITOOL_DEVICE_NUM_INTR: 305117Sschwartz rv = pcitool_intr_admn(dev, (void *)arg, cmd, mode); 306117Sschwartz break; 307117Sschwartz } 308117Sschwartz 309117Sschwartz break; 310117Sschwartz 311117Sschwartz case PCIHP_DEVCTL_MINOR: 312*162Sschwartz if (pci_p->hotplug_capable == B_TRUE) 313*162Sschwartz rv = pcihp_ops->cb_ioctl( 314*162Sschwartz dev, cmd, arg, mode, credp, rvalp); 315*162Sschwartz else 316*162Sschwartz rv = pci_devctl_ioctl( 317*162Sschwartz dip, cmd, arg, mode, credp, rvalp); 318117Sschwartz break; 319117Sschwartz 320117Sschwartz default: 321117Sschwartz break; 322117Sschwartz } 323117Sschwartz 324117Sschwartz return (rv); 325117Sschwartz } 326117Sschwartz 3270Sstevel@tonic-gate static int pci_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 3280Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp) 3290Sstevel@tonic-gate { 3300Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 3310Sstevel@tonic-gate "hotplug-capable")) 3320Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, 3330Sstevel@tonic-gate prop_op, flags, name, valuep, lengthp)); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp)); 3360Sstevel@tonic-gate } 337