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