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 #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/conf.h> 310Sstevel@tonic-gate #include <sys/ddi.h> 320Sstevel@tonic-gate #include <sys/sunddi.h> 330Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 340Sstevel@tonic-gate #include <sys/ddi_subrdefs.h> 350Sstevel@tonic-gate #include <sys/pci.h> 360Sstevel@tonic-gate #include <sys/autoconf.h> 370Sstevel@tonic-gate #include <sys/cmn_err.h> 380Sstevel@tonic-gate #include <sys/errno.h> 390Sstevel@tonic-gate #include <sys/kmem.h> 400Sstevel@tonic-gate #include <sys/debug.h> 410Sstevel@tonic-gate #include <sys/sysmacros.h> 420Sstevel@tonic-gate #include <sys/ebus.h> 430Sstevel@tonic-gate #include <sys/open.h> 440Sstevel@tonic-gate #include <sys/stat.h> 450Sstevel@tonic-gate #include <sys/file.h> 460Sstevel@tonic-gate #include <sys/sunndi.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #ifdef DEBUG 490Sstevel@tonic-gate uint64_t ebus_debug_flags = 0; 500Sstevel@tonic-gate #endif 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * The values of the following variables are used to initialize 540Sstevel@tonic-gate * the cache line size and latency timer registers in the ebus 550Sstevel@tonic-gate * configuration header. Variables are used instead of constants 560Sstevel@tonic-gate * to allow tuning from the /etc/system file. 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate static uint8_t ebus_cache_line_size = 0x10; /* 64 bytes */ 590Sstevel@tonic-gate static uint8_t ebus_latency_timer = 0x40; /* 64 PCI cycles */ 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * function prototypes for bus ops routines: 630Sstevel@tonic-gate */ 640Sstevel@tonic-gate static int 650Sstevel@tonic-gate ebus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, 660Sstevel@tonic-gate off_t offset, off_t len, caddr_t *addrp); 670Sstevel@tonic-gate static int 680Sstevel@tonic-gate ebus_ctlops(dev_info_t *dip, dev_info_t *rdip, 690Sstevel@tonic-gate ddi_ctl_enum_t op, void *arg, void *result); 700Sstevel@tonic-gate static int 710Sstevel@tonic-gate ebus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op, 720Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result); 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * function prototypes for dev ops routines: 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate static int ebus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 780Sstevel@tonic-gate static int ebus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 790Sstevel@tonic-gate static int ebus_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 800Sstevel@tonic-gate void *arg, void **result); 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * general function prototypes: 840Sstevel@tonic-gate */ 850Sstevel@tonic-gate static int ebus_config(ebus_devstate_t *ebus_p); 860Sstevel@tonic-gate static int ebus_apply_range(ebus_devstate_t *ebus_p, dev_info_t *rdip, 87*558Sjasonwu ebus_regspec_t *ebus_rp, vregspec_t *rp); 88*558Sjasonwu int ebus_get_ranges_prop(ebus_devstate_t *ebus_p); 89*558Sjasonwu static void ebus_get_cells_prop(ebus_devstate_t *ebus_p); 90*558Sjasonwu static void ebus_vreg_dump(ebus_devstate_t *ebus_p, vregspec_t *rp); 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define getprop(dip, name, addr, intp) \ 93506Scth ddi_getlongprop(DDI_DEV_T_ANY, (dip), DDI_PROP_DONTPASS, \ 940Sstevel@tonic-gate (name), (caddr_t)(addr), (intp)) 950Sstevel@tonic-gate 960Sstevel@tonic-gate static int ebus_open(dev_t *devp, int flags, int otyp, cred_t *credp); 970Sstevel@tonic-gate static int ebus_close(dev_t dev, int flags, int otyp, cred_t *credp); 980Sstevel@tonic-gate static int ebus_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 990Sstevel@tonic-gate cred_t *credp, int *rvalp); 1000Sstevel@tonic-gate struct cb_ops ebus_cb_ops = { 1010Sstevel@tonic-gate ebus_open, /* open */ 1020Sstevel@tonic-gate ebus_close, /* close */ 1030Sstevel@tonic-gate nodev, /* strategy */ 1040Sstevel@tonic-gate nodev, /* print */ 1050Sstevel@tonic-gate nodev, /* dump */ 1060Sstevel@tonic-gate nodev, /* read */ 1070Sstevel@tonic-gate nodev, /* write */ 1080Sstevel@tonic-gate ebus_ioctl, /* ioctl */ 1090Sstevel@tonic-gate nodev, /* devmap */ 1100Sstevel@tonic-gate nodev, /* mmap */ 1110Sstevel@tonic-gate nodev, /* segmap */ 1120Sstevel@tonic-gate nochpoll, /* poll */ 1130Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 1140Sstevel@tonic-gate NULL, /* streamtab */ 1150Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 1160Sstevel@tonic-gate CB_REV, /* rev */ 1170Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 1180Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 1190Sstevel@tonic-gate }; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * bus ops and dev ops structures: 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate static struct bus_ops ebus_bus_ops = { 1250Sstevel@tonic-gate BUSO_REV, 1260Sstevel@tonic-gate ebus_map, 1270Sstevel@tonic-gate NULL, 1280Sstevel@tonic-gate NULL, 1290Sstevel@tonic-gate NULL, 1300Sstevel@tonic-gate i_ddi_map_fault, 1310Sstevel@tonic-gate ddi_dma_map, 1320Sstevel@tonic-gate ddi_dma_allochdl, 1330Sstevel@tonic-gate ddi_dma_freehdl, 1340Sstevel@tonic-gate ddi_dma_bindhdl, 1350Sstevel@tonic-gate ddi_dma_unbindhdl, 1360Sstevel@tonic-gate ddi_dma_flush, 1370Sstevel@tonic-gate ddi_dma_win, 1380Sstevel@tonic-gate ddi_dma_mctl, 1390Sstevel@tonic-gate ebus_ctlops, 1400Sstevel@tonic-gate ddi_bus_prop_op, 1410Sstevel@tonic-gate ndi_busop_get_eventcookie, 1420Sstevel@tonic-gate ndi_busop_add_eventcall, 1430Sstevel@tonic-gate ndi_busop_remove_eventcall, 1440Sstevel@tonic-gate ndi_post_event, 1450Sstevel@tonic-gate 0, 1460Sstevel@tonic-gate 0, 1470Sstevel@tonic-gate 0, 1480Sstevel@tonic-gate 0, 1490Sstevel@tonic-gate 0, 1500Sstevel@tonic-gate 0, 1510Sstevel@tonic-gate 0, 1520Sstevel@tonic-gate 0, 1530Sstevel@tonic-gate ebus_intr_ops 1540Sstevel@tonic-gate }; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate static struct dev_ops ebus_ops = { 1570Sstevel@tonic-gate DEVO_REV, 1580Sstevel@tonic-gate 0, 1590Sstevel@tonic-gate ebus_info, 1600Sstevel@tonic-gate nulldev, 1610Sstevel@tonic-gate nulldev, 1620Sstevel@tonic-gate ebus_attach, 1630Sstevel@tonic-gate ebus_detach, 1640Sstevel@tonic-gate nodev, 1650Sstevel@tonic-gate &ebus_cb_ops, 1660Sstevel@tonic-gate &ebus_bus_ops 1670Sstevel@tonic-gate }; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* 1700Sstevel@tonic-gate * module definitions: 1710Sstevel@tonic-gate */ 1720Sstevel@tonic-gate #include <sys/modctl.h> 1730Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate static struct modldrv modldrv = { 1760Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 1770Sstevel@tonic-gate "ebus nexus driver %I%", /* Name of module. */ 1780Sstevel@tonic-gate &ebus_ops, /* driver ops */ 1790Sstevel@tonic-gate }; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate static struct modlinkage modlinkage = { 1820Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 1830Sstevel@tonic-gate }; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /* 1860Sstevel@tonic-gate * driver global data: 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate static void *per_ebus_state; /* per-ebus soft state pointer */ 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate int 1920Sstevel@tonic-gate _init(void) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate int e; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * Initialize per-ebus soft state pointer. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate e = ddi_soft_state_init(&per_ebus_state, sizeof (ebus_devstate_t), 1); 2000Sstevel@tonic-gate if (e != 0) 2010Sstevel@tonic-gate return (e); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * Install the module. 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate e = mod_install(&modlinkage); 2070Sstevel@tonic-gate if (e != 0) 2080Sstevel@tonic-gate ddi_soft_state_fini(&per_ebus_state); 2090Sstevel@tonic-gate return (e); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate int 2130Sstevel@tonic-gate _fini(void) 2140Sstevel@tonic-gate { 2150Sstevel@tonic-gate int e; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * Remove the module. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate e = mod_remove(&modlinkage); 2210Sstevel@tonic-gate if (e != 0) 2220Sstevel@tonic-gate return (e); 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * Free the soft state info. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate ddi_soft_state_fini(&per_ebus_state); 2280Sstevel@tonic-gate return (e); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate int 2320Sstevel@tonic-gate _info(struct modinfo *modinfop) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* device driver entry points */ 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /*ARGSUSED*/ 2400Sstevel@tonic-gate static int 2410Sstevel@tonic-gate ebus_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2420Sstevel@tonic-gate { 2430Sstevel@tonic-gate ebus_devstate_t *ebus_p; /* per ebus state pointer */ 2440Sstevel@tonic-gate int instance; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate instance = getminor((dev_t)arg); 2470Sstevel@tonic-gate ebus_p = get_ebus_soft_state(instance); 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate switch (infocmd) { 2500Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 2510Sstevel@tonic-gate *result = (void *)instance; 252*558Sjasonwu break; 2530Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2540Sstevel@tonic-gate if (ebus_p == NULL) 2550Sstevel@tonic-gate return (DDI_FAILURE); 2560Sstevel@tonic-gate *result = (void *)ebus_p->dip; 257*558Sjasonwu break; 258*558Sjasonwu default: 259*558Sjasonwu return (DDI_FAILURE); 2600Sstevel@tonic-gate } 261*558Sjasonwu 262*558Sjasonwu return (DDI_SUCCESS); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* 2660Sstevel@tonic-gate * attach entry point: 2670Sstevel@tonic-gate * 2680Sstevel@tonic-gate * normal attach: 2690Sstevel@tonic-gate * 2700Sstevel@tonic-gate * create soft state structure (dip, reg, nreg and state fields) 2710Sstevel@tonic-gate * map in configuration header 2720Sstevel@tonic-gate * make sure device is properly configured 2730Sstevel@tonic-gate * report device 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate static int 2760Sstevel@tonic-gate ebus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2770Sstevel@tonic-gate { 2780Sstevel@tonic-gate ebus_devstate_t *ebus_p; /* per ebus state pointer */ 2790Sstevel@tonic-gate int instance; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate DBG1(D_ATTACH, NULL, "dip=%p\n", dip); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate switch (cmd) { 2840Sstevel@tonic-gate case DDI_ATTACH: 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * Allocate soft state for this instance. 2880Sstevel@tonic-gate */ 2890Sstevel@tonic-gate instance = ddi_get_instance(dip); 2900Sstevel@tonic-gate if (ddi_soft_state_zalloc(per_ebus_state, instance) 291*558Sjasonwu != DDI_SUCCESS) { 2920Sstevel@tonic-gate DBG(D_ATTACH, NULL, "failed to alloc soft state\n"); 2930Sstevel@tonic-gate return (DDI_FAILURE); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate ebus_p = get_ebus_soft_state(instance); 2960Sstevel@tonic-gate ebus_p->dip = dip; 2970Sstevel@tonic-gate mutex_init(&ebus_p->ebus_mutex, NULL, MUTEX_DRIVER, NULL); 2980Sstevel@tonic-gate ebus_p->ebus_soft_state = EBUS_SOFT_STATE_CLOSED; 2990Sstevel@tonic-gate 300*558Sjasonwu ebus_get_cells_prop(ebus_p); 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate (void) ddi_prop_create(DDI_DEV_T_NONE, dip, 303*558Sjasonwu DDI_PROP_CANSLEEP, "no-dma-interrupt-sync", NULL, 0); 3040Sstevel@tonic-gate /* Get our ranges property for mapping child registers. */ 305*558Sjasonwu if (ebus_get_ranges_prop(ebus_p) != DDI_SUCCESS) { 306*558Sjasonwu goto attach_fail; 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* 3100Sstevel@tonic-gate * create minor node for devctl interfaces 3110Sstevel@tonic-gate */ 3120Sstevel@tonic-gate if (ddi_create_minor_node(dip, "devctl", S_IFCHR, instance, 313*558Sjasonwu DDI_NT_NEXUS, 0) != DDI_SUCCESS) { 314*558Sjasonwu goto attach_fail; 3150Sstevel@tonic-gate } 316*558Sjasonwu 317*558Sjasonwu if (ebus_config(ebus_p) != DDI_SUCCESS) { 318*558Sjasonwu ddi_remove_minor_node(dip, "devctl"); 319*558Sjasonwu goto attach_fail; 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate /* 3230Sstevel@tonic-gate * Make the pci_report_pmcap() call only for RIO 3240Sstevel@tonic-gate * implementations. 3250Sstevel@tonic-gate */ 3260Sstevel@tonic-gate if (IS_RIO(dip)) { 3270Sstevel@tonic-gate (void) pci_report_pmcap(dip, PCI_PM_IDLESPEED, 3280Sstevel@tonic-gate (void *)EBUS_4MHZ); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate /* 3320Sstevel@tonic-gate * Make the state as attached and report the device. 3330Sstevel@tonic-gate */ 3340Sstevel@tonic-gate ebus_p->state = ATTACHED; 3350Sstevel@tonic-gate ddi_report_dev(dip); 3360Sstevel@tonic-gate DBG(D_ATTACH, ebus_p, "returning\n"); 337*558Sjasonwu break; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate case DDI_RESUME: 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate instance = ddi_get_instance(dip); 3420Sstevel@tonic-gate ebus_p = get_ebus_soft_state(instance); 3430Sstevel@tonic-gate 344*558Sjasonwu (void) ebus_config(ebus_p); 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate ebus_p->state = RESUMED; 347*558Sjasonwu break; 3480Sstevel@tonic-gate } 349*558Sjasonwu 350*558Sjasonwu return (DDI_SUCCESS); 351*558Sjasonwu 352*558Sjasonwu attach_fail: 353*558Sjasonwu mutex_destroy(&ebus_p->ebus_mutex); 354*558Sjasonwu free_ebus_soft_state(instance); 3550Sstevel@tonic-gate return (DDI_FAILURE); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * detach entry point: 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate static int 3620Sstevel@tonic-gate ebus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate int instance = ddi_get_instance(dip); 3650Sstevel@tonic-gate ebus_devstate_t *ebus_p = get_ebus_soft_state(instance); 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate switch (cmd) { 3680Sstevel@tonic-gate case DDI_DETACH: 3690Sstevel@tonic-gate DBG1(D_DETACH, ebus_p, "DDI_DETACH dip=%p\n", dip); 3700Sstevel@tonic-gate 371*558Sjasonwu kmem_free(ebus_p->vrangep, ebus_p->vrange_len); 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate ddi_remove_minor_node(dip, "devctl"); 3740Sstevel@tonic-gate mutex_destroy(&ebus_p->ebus_mutex); 3750Sstevel@tonic-gate free_ebus_soft_state(instance); 376*558Sjasonwu break; 3770Sstevel@tonic-gate case DDI_SUSPEND: 3780Sstevel@tonic-gate DBG1(D_DETACH, ebus_p, "DDI_SUSPEND dip=%p\n", dip); 3790Sstevel@tonic-gate ebus_p->state = SUSPENDED; 380*558Sjasonwu break; 381*558Sjasonwu default: 382*558Sjasonwu DBG(D_ATTACH, NULL, 383*558Sjasonwu "failed to recognize ebus detach command\n"); 384*558Sjasonwu return (DDI_FAILURE); 3850Sstevel@tonic-gate } 386*558Sjasonwu return (DDI_SUCCESS); 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate int 391*558Sjasonwu ebus_get_ranges_prop(ebus_devstate_t *ebus_p) 3920Sstevel@tonic-gate { 393*558Sjasonwu if (ddi_getlongprop(DDI_DEV_T_ANY, ebus_p->dip, DDI_PROP_DONTPASS, 394*558Sjasonwu "ranges", (caddr_t)&ebus_p->vrangep, &ebus_p->vrange_len) 395*558Sjasonwu != DDI_SUCCESS) { 396*558Sjasonwu cmn_err(CE_WARN, "Can't get %s ranges property", 397*558Sjasonwu ddi_get_name(ebus_p->dip)); 398*558Sjasonwu return (DDI_ME_REGSPEC_RANGE); 399*558Sjasonwu } 4000Sstevel@tonic-gate 401*558Sjasonwu ebus_p->vrange_cnt = ebus_p->vrange_len / 402*558Sjasonwu (ebus_p->ebus_paddr_cells + ebus_p->ebus_addr_cells + 403*558Sjasonwu ebus_p->ebus_psz_cells); 4040Sstevel@tonic-gate 405*558Sjasonwu if (ebus_p->vrange_cnt == 0) { 406*558Sjasonwu kmem_free(ebus_p->vrangep, ebus_p->vrange_len); 407*558Sjasonwu DBG(D_ATTACH, NULL, "range is equal to zero\n"); 4080Sstevel@tonic-gate return (DDI_FAILURE); 4090Sstevel@tonic-gate } 410*558Sjasonwu 411*558Sjasonwu return (DDI_SUCCESS); 412*558Sjasonwu } 413*558Sjasonwu 414*558Sjasonwu static void 415*558Sjasonwu ebus_get_cells_prop(ebus_devstate_t *ebus_p) 416*558Sjasonwu { 417*558Sjasonwu dev_info_t *dip = ebus_p->dip; 418*558Sjasonwu dev_info_t *pdip; 419*558Sjasonwu 420*558Sjasonwu ebus_p->ebus_addr_cells = ddi_getprop(DDI_DEV_T_ANY, 421*558Sjasonwu dip, DDI_PROP_DONTPASS, "#address-cells", 2); 422*558Sjasonwu 423*558Sjasonwu pdip = ddi_get_parent(dip); 424*558Sjasonwu ebus_p->ebus_paddr_cells = ddi_getprop(DDI_DEV_T_ANY, 425*558Sjasonwu pdip, DDI_PROP_DONTPASS, "#address-cells", 2); 426*558Sjasonwu 427*558Sjasonwu ASSERT((ebus_p->ebus_paddr_cells == 3) || 428*558Sjasonwu (ebus_p->ebus_paddr_cells == 2)); 429*558Sjasonwu 430*558Sjasonwu ebus_p->ebus_sz_cells = ddi_getprop(DDI_DEV_T_ANY, 431*558Sjasonwu dip, DDI_PROP_DONTPASS, "#size-cells", 2); 432*558Sjasonwu ebus_p->ebus_psz_cells = ddi_getprop(DDI_DEV_T_ANY, 433*558Sjasonwu pdip, DDI_PROP_DONTPASS, "#size-cells", 1); 434*558Sjasonwu 435*558Sjasonwu /* XXX rootnex assumes 1 cell and does not respect #size-cells */ 436*558Sjasonwu if (ddi_root_node() == pdip) 437*558Sjasonwu ebus_p->ebus_psz_cells = 1; 438*558Sjasonwu 439*558Sjasonwu ASSERT((ebus_p->ebus_psz_cells == 2) || 440*558Sjasonwu (ebus_p->ebus_psz_cells == 1)); 441*558Sjasonwu 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* bus driver entry points */ 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * bus map entry point: 4480Sstevel@tonic-gate * 4490Sstevel@tonic-gate * if map request is for an rnumber 4500Sstevel@tonic-gate * get the corresponding regspec from device node 4510Sstevel@tonic-gate * build a new regspec in our parent's format 4520Sstevel@tonic-gate * build a new map_req with the new regspec 4530Sstevel@tonic-gate * call up the tree to complete the mapping 4540Sstevel@tonic-gate */ 4550Sstevel@tonic-gate static int 4560Sstevel@tonic-gate ebus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, 4570Sstevel@tonic-gate off_t off, off_t len, caddr_t *addrp) 4580Sstevel@tonic-gate { 4590Sstevel@tonic-gate ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip)); 4600Sstevel@tonic-gate ebus_regspec_t *ebus_rp, *ebus_regs; 461*558Sjasonwu vregspec_t vreg; 4620Sstevel@tonic-gate ddi_map_req_t p_map_request; 4630Sstevel@tonic-gate int rnumber, i, n; 4640Sstevel@tonic-gate int rval = DDI_SUCCESS; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * Handle the mapping according to its type. 4680Sstevel@tonic-gate */ 4690Sstevel@tonic-gate DBG4(D_MAP, ebus_p, "rdip=%s%d: off=%x len=%x\n", 4700Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip), off, len); 4710Sstevel@tonic-gate switch (mp->map_type) { 4720Sstevel@tonic-gate case DDI_MT_REGSPEC: 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * We assume the register specification is in ebus format. 4760Sstevel@tonic-gate * We must convert it into a PCI format regspec and pass 4770Sstevel@tonic-gate * the request to our parent. 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate DBG3(D_MAP, ebus_p, "rdip=%s%d: REGSPEC - handlep=%p\n", 4800Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip), 4810Sstevel@tonic-gate mp->map_handlep); 4820Sstevel@tonic-gate ebus_rp = (ebus_regspec_t *)mp->map_obj.rp; 4830Sstevel@tonic-gate break; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate case DDI_MT_RNUMBER: 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate /* 4880Sstevel@tonic-gate * Get the "reg" property from the device node and convert 4890Sstevel@tonic-gate * it to our parent's format. 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate rnumber = mp->map_obj.rnumber; 4920Sstevel@tonic-gate DBG4(D_MAP, ebus_p, "rdip=%s%d: rnumber=%x handlep=%p\n", 4930Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip), 4940Sstevel@tonic-gate rnumber, mp->map_handlep); 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate if (getprop(rdip, "reg", &ebus_regs, &i) != DDI_SUCCESS) { 4970Sstevel@tonic-gate DBG(D_MAP, ebus_p, "can't get reg property\n"); 4980Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE); 4990Sstevel@tonic-gate } 500*558Sjasonwu 5010Sstevel@tonic-gate n = i / sizeof (ebus_regspec_t); 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate if (rnumber < 0 || rnumber >= n) { 5040Sstevel@tonic-gate DBG(D_MAP, ebus_p, "rnumber out of range\n"); 5050Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate ebus_rp = &ebus_regs[rnumber]; 5080Sstevel@tonic-gate break; 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate default: 5110Sstevel@tonic-gate return (DDI_ME_INVAL); 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* Adjust our reg property with offset and length */ 5160Sstevel@tonic-gate ebus_rp->addr_low += off; 5170Sstevel@tonic-gate if (len) 5180Sstevel@tonic-gate ebus_rp->size = len; 5190Sstevel@tonic-gate 520*558Sjasonwu rval = ebus_apply_range(ebus_p, rdip, ebus_rp, &vreg); 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate if (mp->map_type == DDI_MT_RNUMBER) 5230Sstevel@tonic-gate kmem_free(ebus_regs, i); 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate if (rval != DDI_SUCCESS) 5260Sstevel@tonic-gate return (rval); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate p_map_request = *mp; 5290Sstevel@tonic-gate p_map_request.map_type = DDI_MT_REGSPEC; 5300Sstevel@tonic-gate 531*558Sjasonwu p_map_request.map_obj.rp = (struct regspec *)&vreg; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate rval = ddi_map(dip, &p_map_request, 0, 0, addrp); 5340Sstevel@tonic-gate DBG1(D_MAP, ebus_p, "parent returned %x\n", rval); 5350Sstevel@tonic-gate return (rval); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 538*558Sjasonwu /* 539*558Sjasonwu * ebus_apply_range generically relocates child's regspec to 540*558Sjasonwu * parent's format according to ebus' range spec 541*558Sjasonwu * 542*558Sjasonwu * Assumptions: 543*558Sjasonwu * - rng_caddr_hi is the space type 544*558Sjasonwu * - rng_caddr_low is the base address 545*558Sjasonwu * - ebus address is 32 bit and ebus entirely lives between 0-4G of 546*558Sjasonwu * parent space, so maths on preg/rng_cell_p[ebus_p->ebus_paddr_cells - 1], 547*558Sjasonwu * preg_cell_p[i], rng_caddr_low and ebus_rp->size are sufficient. 548*558Sjasonwu */ 5490Sstevel@tonic-gate static int 5500Sstevel@tonic-gate ebus_apply_range(ebus_devstate_t *ebus_p, dev_info_t *rdip, 551*558Sjasonwu ebus_regspec_t *ebus_rp, vregspec_t *rp) { 552*558Sjasonwu int b, i; 553*558Sjasonwu int nrange = ebus_p->vrange_cnt; 554*558Sjasonwu uint32_t addr_offset, rng_caddr_hi, rng_caddr_low, rng_sz; 555*558Sjasonwu uint32_t req_addr = ebus_rp->addr_low; 556*558Sjasonwu 557*558Sjasonwu uint32_t *rng_cell_p = (uint32_t *)ebus_p->vrangep; 558*558Sjasonwu int rng_rec_sz = ebus_p->ebus_paddr_cells + ebus_p->ebus_addr_cells + 559*558Sjasonwu ebus_p->ebus_sz_cells; 560*558Sjasonwu uint32_t *preg_cell_p = (uint32_t *)rp; 561*558Sjasonwu int preg_rec_sz = ebus_p->ebus_paddr_cells + ebus_p->ebus_psz_cells; 562*558Sjasonwu 5630Sstevel@tonic-gate static char out_of_range[] = 5640Sstevel@tonic-gate "Out of range register specification from device node <%s>"; 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate DBG3(D_MAP, ebus_p, "Range Matching Addr 0x%x.%x size 0x%x\n", 567*558Sjasonwu ebus_rp->addr_hi, req_addr, ebus_rp->size); 568*558Sjasonwu 569*558Sjasonwu for (b = 0; b < nrange; b++, rng_cell_p += rng_rec_sz) { 570*558Sjasonwu 571*558Sjasonwu rng_caddr_hi = rng_cell_p[0]; 572*558Sjasonwu rng_caddr_low = rng_cell_p[1]; 573*558Sjasonwu rng_sz = rng_cell_p[rng_rec_sz-1]; 5740Sstevel@tonic-gate 575*558Sjasonwu /* Check for correct space */ 576*558Sjasonwu if (ebus_rp->addr_hi != rng_caddr_hi) 577*558Sjasonwu continue; 578*558Sjasonwu 579*558Sjasonwu /* Detect whether request entirely fits within a range */ 580*558Sjasonwu if (req_addr < rng_caddr_low) 581*558Sjasonwu continue; 582*558Sjasonwu 583*558Sjasonwu if ((req_addr + ebus_rp->size - 1) 584*558Sjasonwu > (rng_caddr_low + rng_sz - 1)) 585*558Sjasonwu continue; 5860Sstevel@tonic-gate 587*558Sjasonwu addr_offset = req_addr - rng_caddr_low; 588*558Sjasonwu 589*558Sjasonwu /* parent addr = child addr + offset from ranges */ 590*558Sjasonwu for (i = 0; i < preg_rec_sz; i++) 591*558Sjasonwu preg_cell_p[i] = 0; 592*558Sjasonwu 593*558Sjasonwu /* Copy the physical address */ 594*558Sjasonwu for (i = 0; i < ebus_p->ebus_paddr_cells; i++) 595*558Sjasonwu preg_cell_p[i] = rng_cell_p[ebus_p->ebus_addr_cells+i]; 5960Sstevel@tonic-gate 597*558Sjasonwu preg_cell_p[ebus_p->ebus_paddr_cells-1] += addr_offset; 598*558Sjasonwu 599*558Sjasonwu /* Copy the size */ 600*558Sjasonwu preg_cell_p[preg_rec_sz-1] = min(ebus_rp->size, 601*558Sjasonwu rng_sz - addr_offset); 6020Sstevel@tonic-gate 603*558Sjasonwu #ifdef DEBUG 604*558Sjasonwu ebus_vreg_dump(ebus_p, (vregspec_t *)preg_cell_p); 605*558Sjasonwu #endif /* DEBUG */ 606*558Sjasonwu 607*558Sjasonwu break; 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate if (b == nrange) { 6110Sstevel@tonic-gate cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip)); 6120Sstevel@tonic-gate return (DDI_ME_REGSPEC_RANGE); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 615*558Sjasonwu return (DDI_SUCCESS); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate static int 6190Sstevel@tonic-gate ebus_name_child(dev_info_t *child, char *name, int namelen) 6200Sstevel@tonic-gate { 6210Sstevel@tonic-gate ebus_regspec_t *ebus_rp; 6220Sstevel@tonic-gate int reglen; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Get the address portion of the node name based on the 6260Sstevel@tonic-gate * address/offset. 6270Sstevel@tonic-gate */ 628506Scth if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 6290Sstevel@tonic-gate "reg", (caddr_t)&ebus_rp, ®len) != DDI_SUCCESS) { 6300Sstevel@tonic-gate return (DDI_FAILURE); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate (void) snprintf(name, namelen, "%x,%x", ebus_rp->addr_hi, 6340Sstevel@tonic-gate ebus_rp->addr_low); 6350Sstevel@tonic-gate kmem_free(ebus_rp, reglen); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate return (DDI_SUCCESS); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * control ops entry point: 6420Sstevel@tonic-gate * 6430Sstevel@tonic-gate * Requests handled completely: 6440Sstevel@tonic-gate * DDI_CTLOPS_INITCHILD 6450Sstevel@tonic-gate * DDI_CTLOPS_UNINITCHILD 6460Sstevel@tonic-gate * DDI_CTLOPS_REPORTDEV 6470Sstevel@tonic-gate * DDI_CTLOPS_REGSIZE 6480Sstevel@tonic-gate * DDI_CTLOPS_NREGS 6490Sstevel@tonic-gate * 6500Sstevel@tonic-gate * All others passed to parent. 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate static int 6530Sstevel@tonic-gate ebus_ctlops(dev_info_t *dip, dev_info_t *rdip, 6540Sstevel@tonic-gate ddi_ctl_enum_t op, void *arg, void *result) 6550Sstevel@tonic-gate { 6560Sstevel@tonic-gate #ifdef DEBUG 6570Sstevel@tonic-gate ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip)); 6580Sstevel@tonic-gate #endif 6590Sstevel@tonic-gate ebus_regspec_t *ebus_rp; 6600Sstevel@tonic-gate int i, n; 6610Sstevel@tonic-gate char name[10]; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate switch (op) { 6640Sstevel@tonic-gate case DDI_CTLOPS_INITCHILD: { 6650Sstevel@tonic-gate dev_info_t *child = (dev_info_t *)arg; 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Set the address portion of the node name based on the 6680Sstevel@tonic-gate * address/offset. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_INITCHILD: rdip=%s%d\n", 6710Sstevel@tonic-gate ddi_get_name(child), ddi_get_instance(child)); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate if (ebus_name_child(child, name, 10) != DDI_SUCCESS) { 6740Sstevel@tonic-gate DBG(D_CTLOPS, ebus_p, "can't name child\n"); 6750Sstevel@tonic-gate return (DDI_FAILURE); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate ddi_set_name_addr(child, name); 6790Sstevel@tonic-gate ddi_set_parent_data(child, NULL); 6800Sstevel@tonic-gate return (DDI_SUCCESS); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate case DDI_CTLOPS_UNINITCHILD: 6840Sstevel@tonic-gate DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_UNINITCHILD: rdip=%s%d\n", 6850Sstevel@tonic-gate ddi_get_name((dev_info_t *)arg), 6860Sstevel@tonic-gate ddi_get_instance((dev_info_t *)arg)); 6870Sstevel@tonic-gate ddi_set_name_addr((dev_info_t *)arg, NULL); 6880Sstevel@tonic-gate ddi_remove_minor_node((dev_info_t *)arg, NULL); 6890Sstevel@tonic-gate impl_rem_dev_props((dev_info_t *)arg); 6900Sstevel@tonic-gate return (DDI_SUCCESS); 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate case DDI_CTLOPS_REPORTDEV: 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_REPORTDEV: rdip=%s%d\n", 6950Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip)); 6960Sstevel@tonic-gate cmn_err(CE_CONT, "?%s%d at %s%d: offset %s\n", 6970Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 6980Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 6990Sstevel@tonic-gate ddi_get_name_addr(rdip)); 7000Sstevel@tonic-gate return (DDI_SUCCESS); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate case DDI_CTLOPS_REGSIZE: 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_REGSIZE: rdip=%s%d\n", 7050Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip)); 7060Sstevel@tonic-gate if (getprop(rdip, "reg", &ebus_rp, &i) != DDI_SUCCESS) { 7070Sstevel@tonic-gate DBG(D_CTLOPS, ebus_p, "can't get reg property\n"); 7080Sstevel@tonic-gate return (DDI_FAILURE); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate n = i / sizeof (ebus_regspec_t); 7110Sstevel@tonic-gate if (*(int *)arg < 0 || *(int *)arg >= n) { 7120Sstevel@tonic-gate DBG(D_MAP, ebus_p, "rnumber out of range\n"); 7130Sstevel@tonic-gate kmem_free(ebus_rp, i); 7140Sstevel@tonic-gate return (DDI_FAILURE); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate *((off_t *)result) = ebus_rp[*(int *)arg].size; 7170Sstevel@tonic-gate kmem_free(ebus_rp, i); 7180Sstevel@tonic-gate return (DDI_SUCCESS); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate case DDI_CTLOPS_NREGS: 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_NREGS: rdip=%s%d\n", 7230Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip)); 7240Sstevel@tonic-gate if (getprop(rdip, "reg", &ebus_rp, &i) != DDI_SUCCESS) { 7250Sstevel@tonic-gate DBG(D_CTLOPS, ebus_p, "can't get reg property\n"); 7260Sstevel@tonic-gate return (DDI_FAILURE); 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate *((uint_t *)result) = i / sizeof (ebus_regspec_t); 7290Sstevel@tonic-gate kmem_free(ebus_rp, i); 7300Sstevel@tonic-gate return (DDI_SUCCESS); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * Now pass the request up to our parent. 7350Sstevel@tonic-gate */ 7360Sstevel@tonic-gate DBG2(D_CTLOPS, ebus_p, "passing request to parent: rdip=%s%d\n", 7370Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip)); 7380Sstevel@tonic-gate return (ddi_ctlops(dip, rdip, op, arg, result)); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate struct ebus_string_to_pil { 7420Sstevel@tonic-gate int8_t *string; 7430Sstevel@tonic-gate uint32_t pil; 7440Sstevel@tonic-gate }; 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate static struct ebus_string_to_pil ebus_name_to_pil[] = {{"SUNW,CS4231", 9}, 7470Sstevel@tonic-gate {"audio", 9}, 7480Sstevel@tonic-gate {"fdthree", 8}, 7490Sstevel@tonic-gate {"floppy", 8}, 7500Sstevel@tonic-gate {"ecpp", 3}, 7510Sstevel@tonic-gate {"parallel", 3}, 7520Sstevel@tonic-gate {"su", 12}, 7530Sstevel@tonic-gate {"se", 12}, 7540Sstevel@tonic-gate {"serial", 12}, 7550Sstevel@tonic-gate {"power", 14}}; 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate static struct ebus_string_to_pil ebus_device_type_to_pil[] = {{"serial", 12}, 7580Sstevel@tonic-gate {"block", 8}}; 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate static int 7610Sstevel@tonic-gate ebus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op, 7620Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result) 7630Sstevel@tonic-gate { 7640Sstevel@tonic-gate #ifdef DEBUG 7650Sstevel@tonic-gate ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip)); 7660Sstevel@tonic-gate #endif 7670Sstevel@tonic-gate ddi_ispec_t *ip = (ddi_ispec_t *)hdlp->ih_private; 7680Sstevel@tonic-gate int32_t i, max_children, max_device_types, len; 7690Sstevel@tonic-gate char *name_p, *device_type_p; 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate DBG1(D_INTR, ebus_p, "ip 0x%p\n", ip); 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate /* 7740Sstevel@tonic-gate * NOTE: These ops below will never be supported in this nexus 7750Sstevel@tonic-gate * driver, hence they always return immediately. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate switch (intr_op) { 7780Sstevel@tonic-gate case DDI_INTROP_GETCAP: 7790Sstevel@tonic-gate *(int *)result = 0; 7800Sstevel@tonic-gate return (DDI_SUCCESS); 7810Sstevel@tonic-gate case DDI_INTROP_SETCAP: 7820Sstevel@tonic-gate case DDI_INTROP_SETMASK: 7830Sstevel@tonic-gate case DDI_INTROP_CLRMASK: 7840Sstevel@tonic-gate case DDI_INTROP_GETPENDING: 7850Sstevel@tonic-gate return (DDI_ENOTSUP); 7860Sstevel@tonic-gate default: 7870Sstevel@tonic-gate break; 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate if ((intr_op == DDI_INTROP_SUPPORTED_TYPES) || ip->is_pil) 7910Sstevel@tonic-gate goto done; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate /* 7940Sstevel@tonic-gate * This is a hack to set the PIL for the devices under ebus. 7950Sstevel@tonic-gate * We first look up a device by it's specific name, if we can't 7960Sstevel@tonic-gate * match the name, we try and match it's device_type property. 7970Sstevel@tonic-gate * Lastly we default a PIL level of 1. 7980Sstevel@tonic-gate */ 7990Sstevel@tonic-gate name_p = ddi_node_name(rdip); 8000Sstevel@tonic-gate max_children = sizeof (ebus_name_to_pil) / 8010Sstevel@tonic-gate sizeof (struct ebus_string_to_pil); 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate for (i = 0; i < max_children; i++) { 8040Sstevel@tonic-gate if (strcmp(ebus_name_to_pil[i].string, name_p) == 0) { 8050Sstevel@tonic-gate DBG2(D_INTR, ebus_p, "child name %s; match PIL %d\n", 8060Sstevel@tonic-gate ebus_name_to_pil[i].string, 8070Sstevel@tonic-gate ebus_name_to_pil[i].pil); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate ip->is_pil = ebus_name_to_pil[i].pil; 8100Sstevel@tonic-gate goto done; 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate 814506Scth if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS, 8150Sstevel@tonic-gate "device_type", (caddr_t)&device_type_p, &len) == DDI_SUCCESS) { 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate max_device_types = sizeof (ebus_device_type_to_pil) / 8180Sstevel@tonic-gate sizeof (struct ebus_string_to_pil); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate for (i = 0; i < max_device_types; i++) { 8210Sstevel@tonic-gate if (strcmp(ebus_device_type_to_pil[i].string, 8220Sstevel@tonic-gate device_type_p) == 0) { 8230Sstevel@tonic-gate DBG2(D_INTR, ebus_p, "Device type %s; match " 8240Sstevel@tonic-gate "PIL %d\n", ebus_device_type_to_pil[i]. 8250Sstevel@tonic-gate string, ebus_device_type_to_pil[i].pil); 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate ip->is_pil = ebus_device_type_to_pil[i].pil; 8280Sstevel@tonic-gate break; 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate kmem_free(device_type_p, len); 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * If we get here, we need to set a default value 8370Sstevel@tonic-gate * for the PIL. 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate if (ip->is_pil == 0) { 8400Sstevel@tonic-gate ip->is_pil = 1; 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d assigning default interrupt level %d " 8430Sstevel@tonic-gate "for device %s%d", ddi_get_name(dip), ddi_get_instance(dip), 8440Sstevel@tonic-gate ip->is_pil, ddi_get_name(rdip), ddi_get_instance(rdip)); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate done: 8480Sstevel@tonic-gate /* Pass up the request to our parent. */ 8490Sstevel@tonic-gate return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result)); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 852*558Sjasonwu /* 853*558Sjasonwu * ebus_config: setup pci config space registers: 854*558Sjasonwu * enable bus mastering, memory access and error reporting 855*558Sjasonwu */ 8560Sstevel@tonic-gate static int 8570Sstevel@tonic-gate ebus_config(ebus_devstate_t *ebus_p) 8580Sstevel@tonic-gate { 8590Sstevel@tonic-gate ddi_acc_handle_t conf_handle; 8600Sstevel@tonic-gate uint16_t comm; 861*558Sjasonwu dev_info_t *dip = ebus_p->dip; 862*558Sjasonwu char *devtype_str; 863*558Sjasonwu int devtype_len; 864*558Sjasonwu 865*558Sjasonwu if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_get_parent(dip), 866*558Sjasonwu DDI_PROP_DONTPASS, "device_type", (caddr_t)&devtype_str, 867*558Sjasonwu &devtype_len) != DDI_SUCCESS) { 868*558Sjasonwu cmn_err(CE_WARN, "Can't get %s device_type property", 869*558Sjasonwu ddi_get_name(ddi_get_parent(dip))); 870*558Sjasonwu 871*558Sjasonwu return (DDI_FAILURE); 872*558Sjasonwu } 873*558Sjasonwu 874*558Sjasonwu comm = strcmp(devtype_str, "pci"); 875*558Sjasonwu kmem_free(devtype_str, devtype_len); 876*558Sjasonwu 877*558Sjasonwu if (comm) 878*558Sjasonwu return (DDI_SUCCESS); 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate /* 8810Sstevel@tonic-gate * Make sure the master enable and memory access enable 8820Sstevel@tonic-gate * bits are set in the config command register. 8830Sstevel@tonic-gate */ 8840Sstevel@tonic-gate if (pci_config_setup(ebus_p->dip, &conf_handle) != DDI_SUCCESS) 885*558Sjasonwu return (DDI_FAILURE); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate comm = pci_config_get16(conf_handle, PCI_CONF_COMM), 8880Sstevel@tonic-gate #ifdef DEBUG 8890Sstevel@tonic-gate DBG1(D_MAP, ebus_p, "command register was 0x%x\n", comm); 8900Sstevel@tonic-gate #endif 8910Sstevel@tonic-gate comm |= (PCI_COMM_ME|PCI_COMM_MAE|PCI_COMM_SERR_ENABLE| 8920Sstevel@tonic-gate PCI_COMM_PARITY_DETECT); 8930Sstevel@tonic-gate pci_config_put16(conf_handle, PCI_CONF_COMM, comm), 8940Sstevel@tonic-gate #ifdef DEBUG 8950Sstevel@tonic-gate DBG1(D_MAP, ebus_p, "command register is now 0x%x\n", comm); 8960Sstevel@tonic-gate #endif 8970Sstevel@tonic-gate pci_config_put8(conf_handle, PCI_CONF_CACHE_LINESZ, 8980Sstevel@tonic-gate (uchar_t)ebus_cache_line_size); 8990Sstevel@tonic-gate pci_config_put8(conf_handle, PCI_CONF_LATENCY_TIMER, 9000Sstevel@tonic-gate (uchar_t)ebus_latency_timer); 9010Sstevel@tonic-gate pci_config_teardown(&conf_handle); 902*558Sjasonwu return (DDI_SUCCESS); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate #ifdef DEBUG 9060Sstevel@tonic-gate extern void prom_printf(const char *, ...); 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate static void 9090Sstevel@tonic-gate ebus_debug(uint_t flag, ebus_devstate_t *ebus_p, char *fmt, 9100Sstevel@tonic-gate uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5) 9110Sstevel@tonic-gate { 9120Sstevel@tonic-gate char *s; 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate if (ebus_debug_flags & flag) { 9150Sstevel@tonic-gate switch (flag) { 9160Sstevel@tonic-gate case D_ATTACH: 9170Sstevel@tonic-gate s = "attach"; break; 9180Sstevel@tonic-gate case D_DETACH: 9190Sstevel@tonic-gate s = "detach"; break; 9200Sstevel@tonic-gate case D_MAP: 9210Sstevel@tonic-gate s = "map"; break; 9220Sstevel@tonic-gate case D_CTLOPS: 9230Sstevel@tonic-gate s = "ctlops"; break; 9240Sstevel@tonic-gate case D_INTR: 9250Sstevel@tonic-gate s = "intr"; break; 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate if (ebus_p) 9280Sstevel@tonic-gate cmn_err(CE_CONT, "%s%d: %s: ", 9290Sstevel@tonic-gate ddi_get_name(ebus_p->dip), 9300Sstevel@tonic-gate ddi_get_instance(ebus_p->dip), s); 9310Sstevel@tonic-gate else 9320Sstevel@tonic-gate cmn_err(CE_CONT, "ebus: "); 9330Sstevel@tonic-gate cmn_err(CE_CONT, fmt, a1, a2, a3, a4, a5); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate } 936*558Sjasonwu 937*558Sjasonwu static void 938*558Sjasonwu ebus_vreg_dump(ebus_devstate_t *ebus_p, vregspec_t *rp) 939*558Sjasonwu { 940*558Sjasonwu if (ebus_p->ebus_paddr_cells == 3) { 941*558Sjasonwu DBG5(D_MAP, ebus_p, "(%x,%x,%x)(%x,%x)\n", 942*558Sjasonwu rp->pci_regspec.pci_phys_hi, 943*558Sjasonwu rp->pci_regspec.pci_phys_mid, 944*558Sjasonwu rp->pci_regspec.pci_phys_low, 945*558Sjasonwu rp->pci_regspec.pci_size_hi, 946*558Sjasonwu rp->pci_regspec.pci_size_low); 947*558Sjasonwu } else if (ebus_p->ebus_paddr_cells == 2) { 948*558Sjasonwu DBG3(D_MAP, ebus_p, "%x,%x,%x\n", 949*558Sjasonwu rp->jbus_regspec.regspec_bustype, 950*558Sjasonwu rp->jbus_regspec.regspec_addr, 951*558Sjasonwu rp->jbus_regspec.regspec_size); 952*558Sjasonwu } 953*558Sjasonwu } 954*558Sjasonwu #endif /* DEBUG */ 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate /* ARGSUSED3 */ 9570Sstevel@tonic-gate static int 9580Sstevel@tonic-gate ebus_open(dev_t *devp, int flags, int otyp, cred_t *credp) 9590Sstevel@tonic-gate { 9600Sstevel@tonic-gate ebus_devstate_t *ebus_p; 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate /* 9630Sstevel@tonic-gate * Make sure the open is for the right file type. 9640Sstevel@tonic-gate */ 9650Sstevel@tonic-gate if (otyp != OTYP_CHR) 9660Sstevel@tonic-gate return (EINVAL); 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate /* 9690Sstevel@tonic-gate * Get the soft state structure for the device. 9700Sstevel@tonic-gate */ 9710Sstevel@tonic-gate ebus_p = get_ebus_soft_state(getminor(*devp)); 9720Sstevel@tonic-gate if (ebus_p == NULL) 9730Sstevel@tonic-gate return (ENXIO); 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate /* 9760Sstevel@tonic-gate * Handle the open by tracking the device state. 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate mutex_enter(&ebus_p->ebus_mutex); 9790Sstevel@tonic-gate if (flags & FEXCL) { 9800Sstevel@tonic-gate if (ebus_p->ebus_soft_state != EBUS_SOFT_STATE_CLOSED) { 9810Sstevel@tonic-gate mutex_exit(&ebus_p->ebus_mutex); 9820Sstevel@tonic-gate return (EBUSY); 9830Sstevel@tonic-gate } 9840Sstevel@tonic-gate ebus_p->ebus_soft_state = EBUS_SOFT_STATE_OPEN_EXCL; 9850Sstevel@tonic-gate } else { 9860Sstevel@tonic-gate if (ebus_p->ebus_soft_state == EBUS_SOFT_STATE_OPEN_EXCL) { 9870Sstevel@tonic-gate mutex_exit(&ebus_p->ebus_mutex); 9880Sstevel@tonic-gate return (EBUSY); 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate ebus_p->ebus_soft_state = EBUS_SOFT_STATE_OPEN; 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate mutex_exit(&ebus_p->ebus_mutex); 9930Sstevel@tonic-gate return (0); 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* ARGSUSED */ 9980Sstevel@tonic-gate static int 9990Sstevel@tonic-gate ebus_close(dev_t dev, int flags, int otyp, cred_t *credp) 10000Sstevel@tonic-gate { 10010Sstevel@tonic-gate ebus_devstate_t *ebus_p; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate if (otyp != OTYP_CHR) 10040Sstevel@tonic-gate return (EINVAL); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate ebus_p = get_ebus_soft_state(getminor(dev)); 10070Sstevel@tonic-gate if (ebus_p == NULL) 10080Sstevel@tonic-gate return (ENXIO); 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate mutex_enter(&ebus_p->ebus_mutex); 10110Sstevel@tonic-gate ebus_p->ebus_soft_state = EBUS_SOFT_STATE_CLOSED; 10120Sstevel@tonic-gate mutex_exit(&ebus_p->ebus_mutex); 10130Sstevel@tonic-gate return (0); 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate /* 10180Sstevel@tonic-gate * ebus_ioctl: devctl hotplug controls 10190Sstevel@tonic-gate */ 10200Sstevel@tonic-gate /* ARGSUSED */ 10210Sstevel@tonic-gate static int 10220Sstevel@tonic-gate ebus_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 10230Sstevel@tonic-gate int *rvalp) 10240Sstevel@tonic-gate { 10250Sstevel@tonic-gate ebus_devstate_t *ebus_p; 10260Sstevel@tonic-gate dev_info_t *self; 10270Sstevel@tonic-gate struct devctl_iocdata *dcp; 10280Sstevel@tonic-gate uint_t bus_state; 10290Sstevel@tonic-gate int rv = 0; 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate ebus_p = get_ebus_soft_state(getminor(dev)); 10320Sstevel@tonic-gate if (ebus_p == NULL) 10330Sstevel@tonic-gate return (ENXIO); 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate self = ebus_p->dip; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate /* 10380Sstevel@tonic-gate * We can use the generic implementation for these ioctls 10390Sstevel@tonic-gate */ 10400Sstevel@tonic-gate switch (cmd) { 10410Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE: 10420Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE: 10430Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE: 10440Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE: 10450Sstevel@tonic-gate return (ndi_devctl_ioctl(self, cmd, arg, mode, 0)); 10460Sstevel@tonic-gate } 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate /* 10490Sstevel@tonic-gate * read devctl ioctl data 10500Sstevel@tonic-gate */ 10510Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 10520Sstevel@tonic-gate return (EFAULT); 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate switch (cmd) { 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate case DEVCTL_DEVICE_RESET: 10570Sstevel@tonic-gate rv = ENOTSUP; 10580Sstevel@tonic-gate break; 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE: 10610Sstevel@tonic-gate if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) 10620Sstevel@tonic-gate if (bus_state == BUS_QUIESCED) 10630Sstevel@tonic-gate break; 10640Sstevel@tonic-gate (void) ndi_set_bus_state(self, BUS_QUIESCED); 10650Sstevel@tonic-gate break; 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE: 10680Sstevel@tonic-gate if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) 10690Sstevel@tonic-gate if (bus_state == BUS_ACTIVE) 10700Sstevel@tonic-gate break; 10710Sstevel@tonic-gate (void) ndi_set_bus_state(self, BUS_ACTIVE); 10720Sstevel@tonic-gate break; 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate case DEVCTL_BUS_RESET: 10750Sstevel@tonic-gate rv = ENOTSUP; 10760Sstevel@tonic-gate break; 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate case DEVCTL_BUS_RESETALL: 10790Sstevel@tonic-gate rv = ENOTSUP; 10800Sstevel@tonic-gate break; 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate default: 10830Sstevel@tonic-gate rv = ENOTTY; 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate ndi_dc_freehdl(dcp); 10870Sstevel@tonic-gate return (rv); 10880Sstevel@tonic-gate } 1089