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 51624Spjha * Common Development and Distribution License (the "License"). 61624Spjha * 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 /* 221624Spjha * Copyright 2006 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 * Sun4u PCI to PCI bus bridge nexus driver 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/conf.h> 330Sstevel@tonic-gate #include <sys/kmem.h> 340Sstevel@tonic-gate #include <sys/debug.h> 350Sstevel@tonic-gate #include <sys/modctl.h> 360Sstevel@tonic-gate #include <sys/autoconf.h> 370Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 380Sstevel@tonic-gate #include <sys/ddi_subrdefs.h> 390Sstevel@tonic-gate #include <sys/pci.h> 40*3156Sgirish #include <sys/pci_impl.h> 411624Spjha #include <sys/pci_cap.h> 420Sstevel@tonic-gate #include <sys/pci/pci_nexus.h> 430Sstevel@tonic-gate #include <sys/pci/pci_regs.h> 440Sstevel@tonic-gate #include <sys/ddi.h> 450Sstevel@tonic-gate #include <sys/sunndi.h> 460Sstevel@tonic-gate #include <sys/sunddi.h> 470Sstevel@tonic-gate #include <sys/fm/protocol.h> 480Sstevel@tonic-gate #include <sys/ddifm.h> 490Sstevel@tonic-gate #include <sys/pci/pci_pwr.h> 500Sstevel@tonic-gate #include <sys/pci/pci_debug.h> 510Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 520Sstevel@tonic-gate #include <sys/open.h> 530Sstevel@tonic-gate #include <sys/stat.h> 540Sstevel@tonic-gate #include <sys/file.h> 550Sstevel@tonic-gate 560Sstevel@tonic-gate #define NUM_LOGICAL_SLOTS 32 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define PPB_RANGE_LEN 2 590Sstevel@tonic-gate 600Sstevel@tonic-gate #define PPB_32BIT_IO 1 610Sstevel@tonic-gate #define PPB_32bit_MEM 1 620Sstevel@tonic-gate 630Sstevel@tonic-gate #define PPB_MEMGRAIN 0x100000 640Sstevel@tonic-gate #define PPB_IOGRAIN 0x1000 650Sstevel@tonic-gate 660Sstevel@tonic-gate #define PPB_16bit_IOADDR(addr) ((uint16_t)(((uint8_t)(addr) & 0xF0) << 8)) 670Sstevel@tonic-gate #define PPB_LADDR(lo, hi) (((uint16_t)(hi) << 16) | (uint16_t)(lo)) 680Sstevel@tonic-gate #define PPB_32bit_MEMADDR(addr) (PPB_LADDR(0, ((uint16_t)(addr) & 0xFFF0))) 690Sstevel@tonic-gate 700Sstevel@tonic-gate typedef struct slot_table { 710Sstevel@tonic-gate uchar_t bus_id[128]; 720Sstevel@tonic-gate uchar_t slot_name[32]; 730Sstevel@tonic-gate uint8_t device_no; 740Sstevel@tonic-gate uint8_t phys_slot_num; 750Sstevel@tonic-gate } slot_table_t; 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * The variable controls the default setting of the command register 790Sstevel@tonic-gate * for pci devices. See ppb_initchild() for details. 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate static ushort_t ppb_command_default = PCI_COMM_SERR_ENABLE | 820Sstevel@tonic-gate PCI_COMM_WAIT_CYC_ENAB | 830Sstevel@tonic-gate PCI_COMM_PARITY_DETECT | 840Sstevel@tonic-gate PCI_COMM_ME | 850Sstevel@tonic-gate PCI_COMM_MAE | 860Sstevel@tonic-gate PCI_COMM_IO; 870Sstevel@tonic-gate 880Sstevel@tonic-gate static int ppb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *, 890Sstevel@tonic-gate off_t, off_t, caddr_t *); 900Sstevel@tonic-gate static int ppb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, 910Sstevel@tonic-gate void *, void *); 920Sstevel@tonic-gate static int ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip, 930Sstevel@tonic-gate ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result); 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * fm_init busop to initialize our children 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate static int ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap, 990Sstevel@tonic-gate ddi_iblock_cookie_t *ibc); 1000Sstevel@tonic-gate static void ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle); 1010Sstevel@tonic-gate static void ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle); 1020Sstevel@tonic-gate static int ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op, 1030Sstevel@tonic-gate void *arg, void *result); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate struct bus_ops ppb_bus_ops = { 1060Sstevel@tonic-gate BUSO_REV, 1070Sstevel@tonic-gate ppb_bus_map, 1080Sstevel@tonic-gate 0, 1090Sstevel@tonic-gate 0, 1100Sstevel@tonic-gate 0, 1110Sstevel@tonic-gate i_ddi_map_fault, 1120Sstevel@tonic-gate ddi_dma_map, 1130Sstevel@tonic-gate ddi_dma_allochdl, 1140Sstevel@tonic-gate ddi_dma_freehdl, 1150Sstevel@tonic-gate ddi_dma_bindhdl, 1160Sstevel@tonic-gate ddi_dma_unbindhdl, 1170Sstevel@tonic-gate ddi_dma_flush, 1180Sstevel@tonic-gate ddi_dma_win, 1190Sstevel@tonic-gate ddi_dma_mctl, 1200Sstevel@tonic-gate ppb_ctlops, 1210Sstevel@tonic-gate ddi_bus_prop_op, 1220Sstevel@tonic-gate ndi_busop_get_eventcookie, /* (*bus_get_eventcookie)(); */ 1230Sstevel@tonic-gate ndi_busop_add_eventcall, /* (*bus_add_eventcall)(); */ 1240Sstevel@tonic-gate ndi_busop_remove_eventcall, /* (*bus_remove_eventcall)(); */ 1250Sstevel@tonic-gate ndi_post_event, /* (*bus_post_event)(); */ 1260Sstevel@tonic-gate 0, /* (*bus_intr_ctl)(); */ 1270Sstevel@tonic-gate 0, /* (*bus_config)(); */ 1280Sstevel@tonic-gate 0, /* (*bus_unconfig)(); */ 1290Sstevel@tonic-gate ppb_fm_init_child, /* (*bus_fm_init)(); */ 1300Sstevel@tonic-gate NULL, /* (*bus_fm_fini)(); */ 1310Sstevel@tonic-gate ppb_bus_enter, /* (*bus_enter)() */ 1320Sstevel@tonic-gate ppb_bus_exit, /* (*bus_exit)() */ 1330Sstevel@tonic-gate ppb_bus_power, /* (*bus_power)() */ 1340Sstevel@tonic-gate ppb_intr_ops /* (*bus_intr_op)(); */ 1350Sstevel@tonic-gate }; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate static int ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp); 1380Sstevel@tonic-gate static int ppb_close(dev_t dev, int flags, int otyp, cred_t *credp); 1390Sstevel@tonic-gate static int ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 1400Sstevel@tonic-gate cred_t *credp, int *rvalp); 1410Sstevel@tonic-gate static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 1420Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp); 143*3156Sgirish static int ppb_get_bdf_from_dip(dev_info_t *dip, uint32_t *bdf); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate static struct cb_ops ppb_cb_ops = { 1460Sstevel@tonic-gate ppb_open, /* open */ 1470Sstevel@tonic-gate ppb_close, /* close */ 1480Sstevel@tonic-gate nulldev, /* strategy */ 1490Sstevel@tonic-gate nulldev, /* print */ 1500Sstevel@tonic-gate nulldev, /* dump */ 1510Sstevel@tonic-gate nulldev, /* read */ 1520Sstevel@tonic-gate nulldev, /* write */ 1530Sstevel@tonic-gate ppb_ioctl, /* ioctl */ 1540Sstevel@tonic-gate nodev, /* devmap */ 1550Sstevel@tonic-gate nodev, /* mmap */ 1560Sstevel@tonic-gate nodev, /* segmap */ 1570Sstevel@tonic-gate nochpoll, /* poll */ 1580Sstevel@tonic-gate ppb_prop_op, /* cb_prop_op */ 1590Sstevel@tonic-gate NULL, /* streamtab */ 1600Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 1610Sstevel@tonic-gate CB_REV, /* rev */ 1620Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 1630Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 1640Sstevel@tonic-gate }; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static int ppb_probe(dev_info_t *); 1670Sstevel@tonic-gate static int ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd); 1680Sstevel@tonic-gate static int ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd); 1690Sstevel@tonic-gate static int ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 1700Sstevel@tonic-gate void *arg, void **result); 1710Sstevel@tonic-gate static int ppb_pwr(dev_info_t *dip, int component, int level); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate struct dev_ops ppb_ops = { 1740Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 1750Sstevel@tonic-gate 0, /* refcnt */ 1760Sstevel@tonic-gate ppb_info, /* info */ 1770Sstevel@tonic-gate nulldev, /* identify */ 1780Sstevel@tonic-gate ppb_probe, /* probe */ 1790Sstevel@tonic-gate ppb_attach, /* attach */ 1800Sstevel@tonic-gate ppb_detach, /* detach */ 1810Sstevel@tonic-gate nulldev, /* reset */ 1820Sstevel@tonic-gate &ppb_cb_ops, /* driver operations */ 1830Sstevel@tonic-gate &ppb_bus_ops, /* bus operations */ 1840Sstevel@tonic-gate ppb_pwr 1850Sstevel@tonic-gate }; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* 1880Sstevel@tonic-gate * Module linkage information for the kernel. 1890Sstevel@tonic-gate */ 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate static struct modldrv modldrv = { 1920Sstevel@tonic-gate &mod_driverops, /* Type of module */ 193506Scth "Standard PCI to PCI bridge nexus driver %I%", 1940Sstevel@tonic-gate &ppb_ops, /* driver ops */ 1950Sstevel@tonic-gate }; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate static struct modlinkage modlinkage = { 1980Sstevel@tonic-gate MODREV_1, 1990Sstevel@tonic-gate (void *)&modldrv, 2000Sstevel@tonic-gate NULL 2010Sstevel@tonic-gate }; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * soft state pointer and structure template: 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate static void *ppb_state; 2070Sstevel@tonic-gate 208946Smathue struct ppb_cfg_state { 2090Sstevel@tonic-gate dev_info_t *dip; 2100Sstevel@tonic-gate ushort_t command; 2110Sstevel@tonic-gate uchar_t cache_line_size; 2120Sstevel@tonic-gate uchar_t latency_timer; 2130Sstevel@tonic-gate uchar_t header_type; 2140Sstevel@tonic-gate uchar_t sec_latency_timer; 2150Sstevel@tonic-gate ushort_t bridge_control; 2160Sstevel@tonic-gate }; 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate typedef struct { 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate dev_info_t *dip; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * configuration register state for the bus: 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate uchar_t ppb_cache_line_size; 2260Sstevel@tonic-gate uchar_t ppb_latency_timer; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * PM support 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate ddi_acc_handle_t ppb_conf_hdl; 2321624Spjha uint16_t ppb_pm_cap_ptr; 2330Sstevel@tonic-gate pci_pwr_t *ppb_pwr_p; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* 2360Sstevel@tonic-gate * HP support 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate boolean_t hotplug_capable; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate kmutex_t ppb_mutex; 2410Sstevel@tonic-gate uint_t ppb_soft_state; 2420Sstevel@tonic-gate #define PPB_SOFT_STATE_CLOSED 0x00 2430Sstevel@tonic-gate #define PPB_SOFT_STATE_OPEN 0x01 2440Sstevel@tonic-gate #define PPB_SOFT_STATE_OPEN_EXCL 0x02 2450Sstevel@tonic-gate int fm_cap; 2460Sstevel@tonic-gate ddi_iblock_cookie_t fm_ibc; 2470Sstevel@tonic-gate } ppb_devstate_t; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * The following variable enables a workaround for the following obp bug: 2510Sstevel@tonic-gate * 2520Sstevel@tonic-gate * 1234181 - obp should set latency timer registers in pci 2530Sstevel@tonic-gate * configuration header 2540Sstevel@tonic-gate * 2550Sstevel@tonic-gate * Until this bug gets fixed in the obp, the following workaround should 2560Sstevel@tonic-gate * be enabled. 2570Sstevel@tonic-gate */ 2580Sstevel@tonic-gate static uint_t ppb_set_latency_timer_register = 1; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* 2610Sstevel@tonic-gate * The following variable enables a workaround for an obp bug to be 2620Sstevel@tonic-gate * submitted. A bug requesting a workaround fof this problem has 2630Sstevel@tonic-gate * been filed: 2640Sstevel@tonic-gate * 2650Sstevel@tonic-gate * 1235094 - need workarounds on positron nexus drivers to set cache 2660Sstevel@tonic-gate * line size registers 2670Sstevel@tonic-gate * 2680Sstevel@tonic-gate * Until this bug gets fixed in the obp, the following workaround should 2690Sstevel@tonic-gate * be enabled. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate static uint_t ppb_set_cache_line_size_register = 1; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* 2740Sstevel@tonic-gate * forward function declarations: 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * FMA error callback 2790Sstevel@tonic-gate * Register error handling callback with our parent. We will just call 2800Sstevel@tonic-gate * our children's error callbacks and return their status. 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate static int ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, 2830Sstevel@tonic-gate const void *impl_data); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * init/fini routines to alloc/dealloc fm structures and 2870Sstevel@tonic-gate * register/unregister our callback. 2880Sstevel@tonic-gate */ 2890Sstevel@tonic-gate static void ppb_fm_init(ppb_devstate_t *ppb_p); 2900Sstevel@tonic-gate static void ppb_fm_fini(ppb_devstate_t *ppb_p); 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate static void ppb_removechild(dev_info_t *); 2930Sstevel@tonic-gate static int ppb_initchild(dev_info_t *child); 2940Sstevel@tonic-gate static dev_info_t *get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip); 2950Sstevel@tonic-gate static void ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *dip); 2960Sstevel@tonic-gate static void ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip); 2970Sstevel@tonic-gate static void ppb_init_hotplug(ppb_devstate_t *ppb); 2980Sstevel@tonic-gate static void ppb_create_ranges_prop(dev_info_t *, ddi_acc_handle_t); 299964Smathue uint64_t pci_debug_flags = 0; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate int 3020Sstevel@tonic-gate _init(void) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate int e; 3050Sstevel@tonic-gate if ((e = ddi_soft_state_init(&ppb_state, sizeof (ppb_devstate_t), 3060Sstevel@tonic-gate 1)) == 0 && (e = mod_install(&modlinkage)) != 0) 3070Sstevel@tonic-gate ddi_soft_state_fini(&ppb_state); 3080Sstevel@tonic-gate return (e); 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate int 3120Sstevel@tonic-gate _fini(void) 3130Sstevel@tonic-gate { 3140Sstevel@tonic-gate int e; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate if ((e = mod_remove(&modlinkage)) == 0) 3170Sstevel@tonic-gate ddi_soft_state_fini(&ppb_state); 3180Sstevel@tonic-gate return (e); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate int 3220Sstevel@tonic-gate _info(struct modinfo *modinfop) 3230Sstevel@tonic-gate { 3240Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate /*ARGSUSED*/ 3280Sstevel@tonic-gate static int 3290Sstevel@tonic-gate ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 3300Sstevel@tonic-gate { 3310Sstevel@tonic-gate ppb_devstate_t *ppb_p; /* per ppb state pointer */ 3320Sstevel@tonic-gate minor_t minor = getminor((dev_t)arg); 3330Sstevel@tonic-gate int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 3360Sstevel@tonic-gate instance); 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate switch (infocmd) { 3390Sstevel@tonic-gate default: 3400Sstevel@tonic-gate return (DDI_FAILURE); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 343946Smathue *result = (void *)(uintptr_t)instance; 3440Sstevel@tonic-gate return (DDI_SUCCESS); 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 3470Sstevel@tonic-gate if (ppb_p == NULL) 3480Sstevel@tonic-gate return (DDI_FAILURE); 3490Sstevel@tonic-gate *result = (void *)ppb_p->dip; 3500Sstevel@tonic-gate return (DDI_SUCCESS); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /*ARGSUSED*/ 3550Sstevel@tonic-gate static int 3560Sstevel@tonic-gate ppb_probe(register dev_info_t *devi) 3570Sstevel@tonic-gate { 3580Sstevel@tonic-gate return (DDI_PROBE_SUCCESS); 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /*ARGSUSED*/ 3620Sstevel@tonic-gate static int 3630Sstevel@tonic-gate ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate int instance; 3660Sstevel@tonic-gate ppb_devstate_t *ppb; 3670Sstevel@tonic-gate ddi_acc_handle_t config_handle; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate switch (cmd) { 3700Sstevel@tonic-gate case DDI_ATTACH: 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * Make sure the "device_type" property exists. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 3760Sstevel@tonic-gate "device_type", "pci"); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * Allocate and get soft state structure. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate instance = ddi_get_instance(devi); 3820Sstevel@tonic-gate if (ddi_soft_state_zalloc(ppb_state, instance) != DDI_SUCCESS) 3830Sstevel@tonic-gate return (DDI_FAILURE); 3840Sstevel@tonic-gate ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, instance); 3850Sstevel@tonic-gate ppb->dip = devi; 3860Sstevel@tonic-gate mutex_init(&ppb->ppb_mutex, NULL, MUTEX_DRIVER, NULL); 3870Sstevel@tonic-gate ppb->ppb_soft_state = PPB_SOFT_STATE_CLOSED; 3880Sstevel@tonic-gate if (pci_config_setup(devi, &config_handle) != DDI_SUCCESS) { 3890Sstevel@tonic-gate mutex_destroy(&ppb->ppb_mutex); 3900Sstevel@tonic-gate ddi_soft_state_free(ppb_state, instance); 3910Sstevel@tonic-gate return (DDI_FAILURE); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate ppb_pwr_setup(ppb, devi); 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate if (PM_CAPABLE(ppb->ppb_pwr_p)) { 3960Sstevel@tonic-gate mutex_enter(&ppb->ppb_pwr_p->pwr_mutex); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * Before reading config registers, make sure power is 4000Sstevel@tonic-gate * on, and remains on. 4010Sstevel@tonic-gate */ 4020Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_fp++; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate pci_pwr_change(ppb->ppb_pwr_p, 4050Sstevel@tonic-gate ppb->ppb_pwr_p->current_lvl, 4060Sstevel@tonic-gate pci_pwr_new_lvl(ppb->ppb_pwr_p)); 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate ppb->ppb_cache_line_size = 4100Sstevel@tonic-gate pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ); 4110Sstevel@tonic-gate ppb->ppb_latency_timer = 4120Sstevel@tonic-gate pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER); 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* 4150Sstevel@tonic-gate * Check whether the "ranges" property is present. 4160Sstevel@tonic-gate * Otherwise create the ranges property by reading 4170Sstevel@tonic-gate * the configuration registers 4180Sstevel@tonic-gate */ 4190Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 4200Sstevel@tonic-gate "ranges") == 0) { 4210Sstevel@tonic-gate ppb_create_ranges_prop(devi, config_handle); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate pci_config_teardown(&config_handle); 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate if (PM_CAPABLE(ppb->ppb_pwr_p)) { 4270Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_fp--; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate pci_pwr_change(ppb->ppb_pwr_p, 4300Sstevel@tonic-gate ppb->ppb_pwr_p->current_lvl, 4310Sstevel@tonic-gate pci_pwr_new_lvl(ppb->ppb_pwr_p)); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* 4370Sstevel@tonic-gate * Initialize hotplug support on this bus. At minimum 4380Sstevel@tonic-gate * (for non hotplug bus) this would create ":devctl" minor 4390Sstevel@tonic-gate * node to support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls 4400Sstevel@tonic-gate * to this bus. This all takes place if this nexus has hot-plug 4410Sstevel@tonic-gate * slots and successfully initializes Hot Plug Framework. 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate ppb->hotplug_capable = B_FALSE; 4440Sstevel@tonic-gate ppb_init_hotplug(ppb); 4450Sstevel@tonic-gate if (ppb->hotplug_capable == B_FALSE) { 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * create minor node for devctl interfaces 4480Sstevel@tonic-gate */ 4490Sstevel@tonic-gate if (ddi_create_minor_node(devi, "devctl", S_IFCHR, 4500Sstevel@tonic-gate PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR), 4510Sstevel@tonic-gate DDI_NT_NEXUS, 0) != DDI_SUCCESS) { 4520Sstevel@tonic-gate if (ppb->ppb_pwr_p != NULL) { 4530Sstevel@tonic-gate ppb_pwr_teardown(ppb, devi); 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate mutex_destroy(&ppb->ppb_mutex); 4560Sstevel@tonic-gate ddi_soft_state_free(ppb_state, instance); 4570Sstevel@tonic-gate return (DDI_FAILURE); 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate DEBUG1(DBG_ATTACH, devi, 4620Sstevel@tonic-gate "ppb_attach(): this nexus %s hotplug slots\n", 4630Sstevel@tonic-gate ppb->hotplug_capable == B_TRUE ? "has":"has no"); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate ppb_fm_init(ppb); 4660Sstevel@tonic-gate ddi_report_dev(devi); 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate return (DDI_SUCCESS); 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate case DDI_RESUME: 4710Sstevel@tonic-gate /* 4720Sstevel@tonic-gate * Get the soft state structure for the bridge. 4730Sstevel@tonic-gate */ 4740Sstevel@tonic-gate ppb = (ppb_devstate_t *) 4750Sstevel@tonic-gate ddi_get_soft_state(ppb_state, ddi_get_instance(devi)); 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate pci_pwr_resume(devi, ppb->ppb_pwr_p); 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate return (DDI_SUCCESS); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate return (DDI_FAILURE); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate /*ARGSUSED*/ 4850Sstevel@tonic-gate static int 4860Sstevel@tonic-gate ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 4870Sstevel@tonic-gate { 4880Sstevel@tonic-gate ppb_devstate_t *ppb; 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate switch (cmd) { 4910Sstevel@tonic-gate case DDI_DETACH: 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * And finally free the per-pci soft state after 4940Sstevel@tonic-gate * uninitializing hotplug support for this bus. 4950Sstevel@tonic-gate */ 4960Sstevel@tonic-gate ppb = (ppb_devstate_t *) 4970Sstevel@tonic-gate ddi_get_soft_state(ppb_state, ddi_get_instance(devi)); 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate ppb_fm_fini(ppb); 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate if (ppb->hotplug_capable == B_TRUE) 5020Sstevel@tonic-gate if (pcihp_uninit(devi) == DDI_FAILURE) 5030Sstevel@tonic-gate return (DDI_FAILURE); 5040Sstevel@tonic-gate else 5050Sstevel@tonic-gate ddi_remove_minor_node(devi, "devctl"); 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type"); 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate if (ppb->ppb_pwr_p != NULL) { 5100Sstevel@tonic-gate ppb_pwr_teardown(ppb, devi); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate mutex_destroy(&ppb->ppb_mutex); 5130Sstevel@tonic-gate ddi_soft_state_free(ppb_state, ddi_get_instance(devi)); 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate return (DDI_SUCCESS); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate case DDI_SUSPEND: 5180Sstevel@tonic-gate ppb = (ppb_devstate_t *) 5190Sstevel@tonic-gate ddi_get_soft_state(ppb_state, ddi_get_instance(devi)); 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate pci_pwr_suspend(devi, ppb->ppb_pwr_p); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate return (DDI_SUCCESS); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate return (DDI_FAILURE); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /*ARGSUSED*/ 5290Sstevel@tonic-gate static int 5300Sstevel@tonic-gate ppb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, 5310Sstevel@tonic-gate off_t offset, off_t len, caddr_t *vaddrp) 5320Sstevel@tonic-gate { 5330Sstevel@tonic-gate register dev_info_t *pdip; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate pdip = (dev_info_t *)DEVI(dip)->devi_parent; 5360Sstevel@tonic-gate return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map) 5370Sstevel@tonic-gate (pdip, rdip, mp, offset, len, vaddrp)); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /*ARGSUSED*/ 5410Sstevel@tonic-gate static int 5420Sstevel@tonic-gate ppb_ctlops(dev_info_t *dip, dev_info_t *rdip, 5430Sstevel@tonic-gate ddi_ctl_enum_t ctlop, void *arg, void *result) 5440Sstevel@tonic-gate { 5450Sstevel@tonic-gate pci_regspec_t *drv_regp; 5460Sstevel@tonic-gate int reglen; 5470Sstevel@tonic-gate int rn; 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate int totreg; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate switch (ctlop) { 5520Sstevel@tonic-gate case DDI_CTLOPS_REPORTDEV: 5530Sstevel@tonic-gate if (rdip == (dev_info_t *)0) 5540Sstevel@tonic-gate return (DDI_FAILURE); 5550Sstevel@tonic-gate cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n", 5560Sstevel@tonic-gate ddi_node_name(rdip), ddi_get_name_addr(rdip), 5570Sstevel@tonic-gate ddi_driver_name(rdip), 5580Sstevel@tonic-gate ddi_get_instance(rdip)); 5590Sstevel@tonic-gate return (DDI_SUCCESS); 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate case DDI_CTLOPS_INITCHILD: 5620Sstevel@tonic-gate return (ppb_initchild((dev_info_t *)arg)); 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate case DDI_CTLOPS_UNINITCHILD: 5650Sstevel@tonic-gate ppb_removechild((dev_info_t *)arg); 5660Sstevel@tonic-gate return (DDI_SUCCESS); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate case DDI_CTLOPS_SIDDEV: 5690Sstevel@tonic-gate return (DDI_SUCCESS); 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate case DDI_CTLOPS_REGSIZE: 5720Sstevel@tonic-gate case DDI_CTLOPS_NREGS: 5730Sstevel@tonic-gate if (rdip == (dev_info_t *)0) 5740Sstevel@tonic-gate return (DDI_FAILURE); 5750Sstevel@tonic-gate break; 5760Sstevel@tonic-gate default: 5770Sstevel@tonic-gate return (ddi_ctlops(dip, rdip, ctlop, arg, result)); 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate *(int *)result = 0; 581506Scth if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, 5820Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg", 5830Sstevel@tonic-gate (caddr_t)&drv_regp, ®len) != DDI_SUCCESS) 5840Sstevel@tonic-gate return (DDI_FAILURE); 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate totreg = reglen / sizeof (pci_regspec_t); 5870Sstevel@tonic-gate if (ctlop == DDI_CTLOPS_NREGS) 5880Sstevel@tonic-gate *(int *)result = totreg; 5890Sstevel@tonic-gate else if (ctlop == DDI_CTLOPS_REGSIZE) { 5900Sstevel@tonic-gate rn = *(int *)arg; 5910Sstevel@tonic-gate if (rn >= totreg) { 5920Sstevel@tonic-gate kmem_free(drv_regp, reglen); 5930Sstevel@tonic-gate return (DDI_FAILURE); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate *(off_t *)result = drv_regp[rn].pci_size_low | 5960Sstevel@tonic-gate ((uint64_t)drv_regp[rn].pci_size_hi << 32); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate kmem_free(drv_regp, reglen); 6000Sstevel@tonic-gate return (DDI_SUCCESS); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate static dev_info_t * 6050Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip) 6060Sstevel@tonic-gate { 6070Sstevel@tonic-gate dev_info_t *cdip = rdip; 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip)) 6100Sstevel@tonic-gate ; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate return (cdip); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate static int 6170Sstevel@tonic-gate ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op, 6180Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result) 6190Sstevel@tonic-gate { 620693Sgovinda dev_info_t *cdip = rdip; 621693Sgovinda pci_regspec_t *pci_rp; 622693Sgovinda int reglen, len; 623693Sgovinda uint32_t d, intr; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 6260Sstevel@tonic-gate goto done; 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate /* 6290Sstevel@tonic-gate * If the interrupt-map property is defined at this 6300Sstevel@tonic-gate * node, it will have performed the interrupt 6310Sstevel@tonic-gate * translation as part of the property, so no 6320Sstevel@tonic-gate * rotation needs to be done. 6330Sstevel@tonic-gate */ 6340Sstevel@tonic-gate if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6350Sstevel@tonic-gate "interrupt-map", &len) == DDI_PROP_SUCCESS) 6360Sstevel@tonic-gate goto done; 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate cdip = get_my_childs_dip(dip, rdip); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Use the devices reg property to determine its 6420Sstevel@tonic-gate * PCI bus number and device number. 6430Sstevel@tonic-gate */ 644506Scth if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS, 6450Sstevel@tonic-gate "reg", (caddr_t)&pci_rp, ®len) != DDI_SUCCESS) 6460Sstevel@tonic-gate return (DDI_FAILURE); 6470Sstevel@tonic-gate 648693Sgovinda intr = hdlp->ih_vector; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate /* Spin the interrupt */ 6510Sstevel@tonic-gate d = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate if ((intr >= PCI_INTA) && (intr <= PCI_INTD)) 654693Sgovinda hdlp->ih_vector = ((intr - 1 + (d % 4)) % 4 + 1); 6550Sstevel@tonic-gate else 6560Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: %s: PCI intr=%x out of range", 6570Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip), 6580Sstevel@tonic-gate ddi_driver_name(dip), intr); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate kmem_free(pci_rp, reglen); 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate done: 6630Sstevel@tonic-gate /* Pass up the request to our parent. */ 6640Sstevel@tonic-gate return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result)); 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate static int 6680Sstevel@tonic-gate ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op, 6690Sstevel@tonic-gate void *arg, void *result) 6700Sstevel@tonic-gate { 6710Sstevel@tonic-gate ppb_devstate_t *ppb; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 6740Sstevel@tonic-gate ddi_get_instance(dip)); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate return (pci_pwr_ops(ppb->ppb_pwr_p, dip, impl_arg, op, arg, result)); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * name_child 6820Sstevel@tonic-gate * 6830Sstevel@tonic-gate * This function is called from init_child to name a node. It is 6840Sstevel@tonic-gate * also passed as a callback for node merging functions. 6850Sstevel@tonic-gate * 6860Sstevel@tonic-gate * return value: DDI_SUCCESS, DDI_FAILURE 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate static int 6890Sstevel@tonic-gate ppb_name_child(dev_info_t *child, char *name, int namelen) 6900Sstevel@tonic-gate { 6910Sstevel@tonic-gate pci_regspec_t *pci_rp; 6920Sstevel@tonic-gate uint_t slot, func; 6930Sstevel@tonic-gate char **unit_addr; 6940Sstevel@tonic-gate uint_t n; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate /* 6970Sstevel@tonic-gate * Pseudo nodes indicate a prototype node with per-instance 6980Sstevel@tonic-gate * properties to be merged into the real h/w device node. 6990Sstevel@tonic-gate * The interpretation of the unit-address is DD[,F] 7000Sstevel@tonic-gate * where DD is the device id and F is the function. 7010Sstevel@tonic-gate */ 7020Sstevel@tonic-gate if (ndi_dev_is_persistent_node(child) == 0) { 7030Sstevel@tonic-gate if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child, 7040Sstevel@tonic-gate DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) != 7050Sstevel@tonic-gate DDI_PROP_SUCCESS) { 7060Sstevel@tonic-gate cmn_err(CE_WARN, "cannot name node from %s.conf", 7070Sstevel@tonic-gate ddi_driver_name(child)); 7080Sstevel@tonic-gate return (DDI_FAILURE); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate if (n != 1 || *unit_addr == NULL || **unit_addr == 0) { 7110Sstevel@tonic-gate cmn_err(CE_WARN, "unit-address property in %s.conf" 7120Sstevel@tonic-gate " not well-formed", ddi_driver_name(child)); 7130Sstevel@tonic-gate ddi_prop_free(unit_addr); 7140Sstevel@tonic-gate return (DDI_FAILURE); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate (void) snprintf(name, namelen, "%s", *unit_addr); 7170Sstevel@tonic-gate ddi_prop_free(unit_addr); 7180Sstevel@tonic-gate return (DDI_SUCCESS); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * Get the address portion of the node name based on 7230Sstevel@tonic-gate * the function and device number. 7240Sstevel@tonic-gate */ 7250Sstevel@tonic-gate if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 7260Sstevel@tonic-gate "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) { 7270Sstevel@tonic-gate return (DDI_FAILURE); 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate slot = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi); 7310Sstevel@tonic-gate func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi); 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate if (func != 0) 7340Sstevel@tonic-gate (void) snprintf(name, namelen, "%x,%x", slot, func); 7350Sstevel@tonic-gate else 7360Sstevel@tonic-gate (void) snprintf(name, namelen, "%x", slot); 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate ddi_prop_free(pci_rp); 7390Sstevel@tonic-gate return (DDI_SUCCESS); 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate static int 7430Sstevel@tonic-gate ppb_initchild(dev_info_t *child) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate char name[MAXNAMELEN]; 7460Sstevel@tonic-gate ddi_acc_handle_t config_handle; 7470Sstevel@tonic-gate ushort_t command_preserve, command; 7480Sstevel@tonic-gate uint_t n; 7490Sstevel@tonic-gate ushort_t bcr; 7500Sstevel@tonic-gate uchar_t header_type; 7510Sstevel@tonic-gate uchar_t min_gnt, latency_timer; 7520Sstevel@tonic-gate ppb_devstate_t *ppb; 753*3156Sgirish pci_parent_data_t *pd_p; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * Name the child 7570Sstevel@tonic-gate */ 7580Sstevel@tonic-gate if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS) 7590Sstevel@tonic-gate return (DDI_FAILURE); 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate ddi_set_name_addr(child, name); 7620Sstevel@tonic-gate ddi_set_parent_data(child, NULL); 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * Pseudo nodes indicate a prototype node with per-instance 7660Sstevel@tonic-gate * properties to be merged into the real h/w device node. 7670Sstevel@tonic-gate * The interpretation of the unit-address is DD[,F] 7680Sstevel@tonic-gate * where DD is the device id and F is the function. 7690Sstevel@tonic-gate */ 7700Sstevel@tonic-gate if (ndi_dev_is_persistent_node(child) == 0) { 7710Sstevel@tonic-gate extern int pci_allow_pseudo_children; 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate /* 7740Sstevel@tonic-gate * Try to merge the properties from this prototype 7750Sstevel@tonic-gate * node into real h/w nodes. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) { 7780Sstevel@tonic-gate /* 7790Sstevel@tonic-gate * Merged ok - return failure to remove the node. 7800Sstevel@tonic-gate */ 7810Sstevel@tonic-gate ppb_removechild(child); 7820Sstevel@tonic-gate return (DDI_FAILURE); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* workaround for ddivs to run under PCI */ 7860Sstevel@tonic-gate if (pci_allow_pseudo_children) 7870Sstevel@tonic-gate return (DDI_SUCCESS); 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate /* 7900Sstevel@tonic-gate * The child was not merged into a h/w node, 7910Sstevel@tonic-gate * but there's not much we can do with it other 7920Sstevel@tonic-gate * than return failure to cause the node to be removed. 7930Sstevel@tonic-gate */ 7940Sstevel@tonic-gate cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged", 7950Sstevel@tonic-gate ddi_driver_name(child), ddi_get_name_addr(child), 7960Sstevel@tonic-gate ddi_driver_name(child)); 7970Sstevel@tonic-gate ppb_removechild(child); 7980Sstevel@tonic-gate return (DDI_NOT_WELL_FORMED); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate ddi_set_parent_data(child, NULL); 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 8040Sstevel@tonic-gate ddi_get_instance(ddi_get_parent(child))); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate /* 8070Sstevel@tonic-gate * If hardware is PM capable, set up the power info structure. 8080Sstevel@tonic-gate * This also ensures the the bus will not be off (0MHz) otherwise 8090Sstevel@tonic-gate * system panics during a bus access. 8100Sstevel@tonic-gate */ 8110Sstevel@tonic-gate if (PM_CAPABLE(ppb->ppb_pwr_p)) { 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Create a pwr_info struct for child. Bus will be 8140Sstevel@tonic-gate * at full speed after creating info. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate pci_pwr_create_info(ppb->ppb_pwr_p, child); 8170Sstevel@tonic-gate #ifdef DEBUG 8180Sstevel@tonic-gate ASSERT(ppb->ppb_pwr_p->current_lvl == PM_LEVEL_B0); 8190Sstevel@tonic-gate #endif 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate /* 8230Sstevel@tonic-gate * If configuration registers were previously saved by 8240Sstevel@tonic-gate * child (before it entered D3), then let the child do the 8250Sstevel@tonic-gate * restore to set up the config regs as it'll first need to 8260Sstevel@tonic-gate * power the device out of D3. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 8290Sstevel@tonic-gate "config-regs-saved-by-child") == 1) { 8300Sstevel@tonic-gate DEBUG2(DBG_PWR, ddi_get_parent(child), 8310Sstevel@tonic-gate "INITCHILD: config regs to be restored by child" 8320Sstevel@tonic-gate " for %s@%s\n", ddi_node_name(child), 8330Sstevel@tonic-gate ddi_get_name_addr(child)); 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate return (DDI_SUCCESS); 8360Sstevel@tonic-gate } 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate DEBUG2(DBG_PWR, ddi_get_parent(child), 8390Sstevel@tonic-gate "INITCHILD: config regs setup for %s@%s\n", 8400Sstevel@tonic-gate ddi_node_name(child), ddi_get_name_addr(child)); 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) { 8430Sstevel@tonic-gate if (PM_CAPABLE(ppb->ppb_pwr_p)) { 8440Sstevel@tonic-gate pci_pwr_rm_info(ppb->ppb_pwr_p, child); 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate return (DDI_FAILURE); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * Determine the configuration header type. 8520Sstevel@tonic-gate */ 8530Sstevel@tonic-gate header_type = pci_config_get8(config_handle, PCI_CONF_HEADER); 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate /* 8560Sstevel@tonic-gate * Support for the "command-preserve" property. 8570Sstevel@tonic-gate */ 8580Sstevel@tonic-gate command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child, 8590Sstevel@tonic-gate DDI_PROP_DONTPASS, "command-preserve", 0); 8600Sstevel@tonic-gate command = pci_config_get16(config_handle, PCI_CONF_COMM); 8610Sstevel@tonic-gate command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB); 8620Sstevel@tonic-gate command |= (ppb_command_default & ~command_preserve); 8630Sstevel@tonic-gate pci_config_put16(config_handle, PCI_CONF_COMM, command); 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate /* 8660Sstevel@tonic-gate * If the device has a bus control register then program it 8670Sstevel@tonic-gate * based on the settings in the command register. 8680Sstevel@tonic-gate */ 8690Sstevel@tonic-gate if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) { 8700Sstevel@tonic-gate bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL); 8710Sstevel@tonic-gate if (ppb_command_default & PCI_COMM_PARITY_DETECT) 8720Sstevel@tonic-gate bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE; 8730Sstevel@tonic-gate if (ppb_command_default & PCI_COMM_SERR_ENABLE) 8740Sstevel@tonic-gate bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE; 8750Sstevel@tonic-gate bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE; 8760Sstevel@tonic-gate pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /* 8800Sstevel@tonic-gate * Initialize cache-line-size configuration register if needed. 8810Sstevel@tonic-gate */ 8820Sstevel@tonic-gate if (ppb_set_cache_line_size_register && 8830Sstevel@tonic-gate ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 8840Sstevel@tonic-gate "cache-line-size", 0) == 0) { 8850Sstevel@tonic-gate pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ, 8860Sstevel@tonic-gate ppb->ppb_cache_line_size); 8870Sstevel@tonic-gate n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ); 8880Sstevel@tonic-gate if (n != 0) { 8890Sstevel@tonic-gate (void) ndi_prop_update_int(DDI_DEV_T_NONE, child, 8900Sstevel@tonic-gate "cache-line-size", n); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * Initialize latency timer configuration registers if needed. 8960Sstevel@tonic-gate */ 8970Sstevel@tonic-gate if (ppb_set_latency_timer_register && 8980Sstevel@tonic-gate ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 8990Sstevel@tonic-gate "latency-timer", 0) == 0) { 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) { 9020Sstevel@tonic-gate latency_timer = ppb->ppb_latency_timer; 9030Sstevel@tonic-gate pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER, 9040Sstevel@tonic-gate ppb->ppb_latency_timer); 9050Sstevel@tonic-gate } else { 9060Sstevel@tonic-gate min_gnt = pci_config_get8(config_handle, 9070Sstevel@tonic-gate PCI_CONF_MIN_G); 9080Sstevel@tonic-gate latency_timer = min_gnt * 8; 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER, 9110Sstevel@tonic-gate latency_timer); 9120Sstevel@tonic-gate n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER); 9130Sstevel@tonic-gate if (n != 0) { 9140Sstevel@tonic-gate (void) ndi_prop_update_int(DDI_DEV_T_NONE, child, 9150Sstevel@tonic-gate "latency-timer", n); 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * Check to see if the XMITS/PCI-X workaround applies. 9210Sstevel@tonic-gate */ 9220Sstevel@tonic-gate n = ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_NOTPROM, 9230Sstevel@tonic-gate "pcix-update-cmd-reg", -1); 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate if (n != -1) { 9260Sstevel@tonic-gate extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value); 9270Sstevel@tonic-gate DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ " 9280Sstevel@tonic-gate "Workaround: value = %x\n", n); 9290Sstevel@tonic-gate pcix_set_cmd_reg(child, n); 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 932*3156Sgirish /* Allocate memory for pci parent data */ 933*3156Sgirish pd_p = kmem_zalloc(sizeof (pci_parent_data_t), KM_SLEEP); 934*3156Sgirish 935*3156Sgirish /* 936*3156Sgirish * Retrieve and save BDF and PCIE2PCI bridge's secondary bus 937*3156Sgirish * information in the parent private data structure. 938*3156Sgirish */ 939*3156Sgirish if (ppb_get_bdf_from_dip(child, &pd_p->pci_bdf) != DDI_SUCCESS) { 940*3156Sgirish kmem_free(pd_p, sizeof (pci_parent_data_t)); 941*3156Sgirish pci_config_teardown(&config_handle); 942*3156Sgirish return (DDI_FAILURE); 943*3156Sgirish } 944*3156Sgirish 945*3156Sgirish pd_p->pci_sec_bus = ddi_prop_get_int(DDI_DEV_T_ANY, child, 0, 946*3156Sgirish "pcie2pci-sec-bus", 0); 947*3156Sgirish 948*3156Sgirish ddi_set_parent_data(child, (void *)pd_p); 9490Sstevel@tonic-gate pci_config_teardown(&config_handle); 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate return (DDI_SUCCESS); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate static void 9550Sstevel@tonic-gate ppb_removechild(dev_info_t *dip) 9560Sstevel@tonic-gate { 9570Sstevel@tonic-gate ppb_devstate_t *ppb; 958*3156Sgirish pci_parent_data_t *pd_p; 959*3156Sgirish 960*3156Sgirish if (pd_p = ddi_get_parent_data(dip)) { 961*3156Sgirish ddi_set_parent_data(dip, NULL); 962*3156Sgirish kmem_free(pd_p, sizeof (pci_parent_data_t)); 963*3156Sgirish } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 9660Sstevel@tonic-gate ddi_get_instance(ddi_get_parent(dip))); 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate if (PM_CAPABLE(ppb->ppb_pwr_p)) { 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate DEBUG2(DBG_PWR, ddi_get_parent(dip), 9710Sstevel@tonic-gate "UNINITCHILD: removing pwr_info for %s@%s\n", 9720Sstevel@tonic-gate ddi_node_name(dip), ddi_get_name_addr(dip)); 9730Sstevel@tonic-gate pci_pwr_rm_info(ppb->ppb_pwr_p, dip); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate ddi_set_name_addr(dip, NULL); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate /* 9790Sstevel@tonic-gate * Strip the node to properly convert it back to prototype form 9800Sstevel@tonic-gate */ 9810Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate impl_rem_dev_props(dip); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* 9870Sstevel@tonic-gate * If bridge is PM capable, set up PM state for nexus. 9880Sstevel@tonic-gate */ 9890Sstevel@tonic-gate static void 9900Sstevel@tonic-gate ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *pdip) 9910Sstevel@tonic-gate { 9920Sstevel@tonic-gate char *comp_array[5]; 9930Sstevel@tonic-gate int i; 9940Sstevel@tonic-gate ddi_acc_handle_t conf_hdl; 9950Sstevel@tonic-gate uint8_t pmcsr_bse; 9960Sstevel@tonic-gate uint16_t pmcap; 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * Determine if bridge is PM capable. If not, leave ppb_pwr_p NULL 10000Sstevel@tonic-gate * and return. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate if (pci_config_setup(pdip, &ppb->ppb_conf_hdl) != DDI_SUCCESS) { 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate return; 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate conf_hdl = ppb->ppb_conf_hdl; 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate /* 10101624Spjha * Locate and store the power management cap_ptr for future references. 10110Sstevel@tonic-gate */ 10121624Spjha if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &ppb->ppb_pm_cap_ptr)) 10131624Spjha == DDI_FAILURE) { 10140Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "bridge does not support PM. PCI" 10150Sstevel@tonic-gate " PM data structure not found in config header\n"); 10160Sstevel@tonic-gate pci_config_teardown(&conf_hdl); 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate return; 10190Sstevel@tonic-gate } 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate /* 10220Sstevel@tonic-gate * Allocate PM state structure for ppb. 10230Sstevel@tonic-gate */ 10240Sstevel@tonic-gate ppb->ppb_pwr_p = (pci_pwr_t *) 10250Sstevel@tonic-gate kmem_zalloc(sizeof (pci_pwr_t), KM_SLEEP); 10260Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_fp = 0; 10270Sstevel@tonic-gate 10281624Spjha pmcsr_bse = PCI_CAP_GET8(conf_hdl, NULL, ppb->ppb_pm_cap_ptr, 10291624Spjha PCI_PMCSR_BSE); 10300Sstevel@tonic-gate 10311624Spjha pmcap = PCI_CAP_GET16(conf_hdl, NULL, ppb->ppb_pm_cap_ptr, 10321624Spjha PCI_PMCAP); 10330Sstevel@tonic-gate 10341774Spjha if (pmcap == PCI_CAP_EINVAL16 || pmcsr_bse == PCI_CAP_EINVAL8) { 10351624Spjha pci_config_teardown(&conf_hdl); 10361624Spjha return; 10371624Spjha } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (pmcap & PCI_PMCAP_D1) { 10400Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "setup: B1 state supported\n"); 10410Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B1_CAPABLE; 10420Sstevel@tonic-gate } else { 10430Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "setup: B1 state NOT supported\n"); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate if (pmcap & PCI_PMCAP_D2) { 10460Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "setup: B2 state supported\n"); 10470Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE; 10480Sstevel@tonic-gate } else { 10490Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "setup: B2 via D2 NOT supported\n"); 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate if (pmcsr_bse & PCI_PMCSR_BSE_BPCC_EN) { 10530Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, 10541624Spjha "setup: bridge power/clock control enable\n"); 10550Sstevel@tonic-gate } else { 10560Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, 10571624Spjha "setup: bridge power/clock control disabled\n"); 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t)); 10600Sstevel@tonic-gate ppb->ppb_pwr_p = NULL; 10610Sstevel@tonic-gate pci_config_teardown(&conf_hdl); 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate return; 10640Sstevel@tonic-gate } 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate /* 10670Sstevel@tonic-gate * PCI states D0 and D3 always are supported for normal PCI 10680Sstevel@tonic-gate * devices. D1 and D2 are optional which are checked for above. 10690Sstevel@tonic-gate * Bridge function states D0-D3 correspond to secondary bus states 10700Sstevel@tonic-gate * B0-B3, EXCEPT if PCI_PMCSR_BSE_B2_B3 is set. In this case, setting 10710Sstevel@tonic-gate * the bridge function to D3 will set the bridge bus to state B2 instead 10720Sstevel@tonic-gate * of B3. D2 will not correspond to B2 (and in fact, probably 10730Sstevel@tonic-gate * won't be D2 capable). Implicitly, this means that if 10740Sstevel@tonic-gate * PCI_PMCSR_BSE_B2_B3 is set, the bus will not be B3 capable. 10750Sstevel@tonic-gate */ 10760Sstevel@tonic-gate if (pmcsr_bse & PCI_PMCSR_BSE_B2_B3) { 10770Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE; 10780Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "B2 supported via D3\n"); 10790Sstevel@tonic-gate } else { 10800Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B3_CAPABLE; 10810Sstevel@tonic-gate DEBUG0(DBG_PWR, pdip, "B3 supported via D3\n"); 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate ppb->ppb_pwr_p->pwr_dip = pdip; 10850Sstevel@tonic-gate mutex_init(&ppb->ppb_pwr_p->pwr_mutex, NULL, MUTEX_DRIVER, NULL); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate i = 0; 10880Sstevel@tonic-gate comp_array[i++] = "NAME=PCI bridge PM"; 10890Sstevel@tonic-gate if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) { 10900Sstevel@tonic-gate comp_array[i++] = "0=Clock/Power Off (B3)"; 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) { 10930Sstevel@tonic-gate comp_array[i++] = "1=Clock Off (B2)"; 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) { 10960Sstevel@tonic-gate comp_array[i++] = "2=Bus Inactive (B1)"; 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate comp_array[i++] = "3=Full Power (B0)"; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate /* 11010Sstevel@tonic-gate * Create pm-components property. It does not already exist. 11020Sstevel@tonic-gate */ 11030Sstevel@tonic-gate if (ddi_prop_update_string_array(DDI_DEV_T_NONE, pdip, 11040Sstevel@tonic-gate "pm-components", comp_array, i) != DDI_PROP_SUCCESS) { 11050Sstevel@tonic-gate cmn_err(CE_WARN, 11060Sstevel@tonic-gate "%s%d pm-components prop update failed", 11070Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip)); 11080Sstevel@tonic-gate pci_config_teardown(&conf_hdl); 11090Sstevel@tonic-gate mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex); 11100Sstevel@tonic-gate kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t)); 11110Sstevel@tonic-gate ppb->ppb_pwr_p = NULL; 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate return; 11140Sstevel@tonic-gate } 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate if (ddi_prop_create(DDI_DEV_T_NONE, pdip, DDI_PROP_CANSLEEP, 11170Sstevel@tonic-gate "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) { 11180Sstevel@tonic-gate cmn_err(CE_WARN, 11190Sstevel@tonic-gate "%s%d fail to create pm-want-child-notification? prop", 11200Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip)); 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip, "pm-components"); 11230Sstevel@tonic-gate pci_config_teardown(&conf_hdl); 11240Sstevel@tonic-gate mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex); 11250Sstevel@tonic-gate kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t)); 11260Sstevel@tonic-gate ppb->ppb_pwr_p = NULL; 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate return; 11290Sstevel@tonic-gate } 11300Sstevel@tonic-gate 11310Sstevel@tonic-gate ppb->ppb_pwr_p->current_lvl = 11321624Spjha pci_pwr_current_lvl(ppb->ppb_pwr_p); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate /* 11360Sstevel@tonic-gate * Remove PM state for nexus. 11370Sstevel@tonic-gate */ 11380Sstevel@tonic-gate static void 11390Sstevel@tonic-gate ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip) 11400Sstevel@tonic-gate { 11410Sstevel@tonic-gate int low_lvl; 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* 11440Sstevel@tonic-gate * Determine the lowest power level supported. 11450Sstevel@tonic-gate */ 11460Sstevel@tonic-gate if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) { 11470Sstevel@tonic-gate low_lvl = PM_LEVEL_B3; 11480Sstevel@tonic-gate } else { 11490Sstevel@tonic-gate low_lvl = PM_LEVEL_B2; 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate if (pm_lower_power(dip, PCI_PM_COMP_0, low_lvl) != DDI_SUCCESS) { 11530Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d failed to lower power", 11540Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate pci_config_teardown(&ppb->ppb_conf_hdl); 11580Sstevel@tonic-gate mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex); 11590Sstevel@tonic-gate kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t)); 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate if (ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components") != 11620Sstevel@tonic-gate DDI_PROP_SUCCESS) { 11630Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d unable to remove prop pm-components", 11640Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate if (ddi_prop_remove(DDI_DEV_T_NONE, dip, 11680Sstevel@tonic-gate "pm-want-child-notification?") != DDI_PROP_SUCCESS) { 11690Sstevel@tonic-gate cmn_err(CE_WARN, 11700Sstevel@tonic-gate "%s%d unable to remove prop pm-want_child_notification?", 11710Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * Examine the pmcsr register and return the software defined 11770Sstevel@tonic-gate * state (the difference being whether D3 means B2 or B3). 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate int 11800Sstevel@tonic-gate pci_pwr_current_lvl(pci_pwr_t *pwr_p) 11810Sstevel@tonic-gate { 11820Sstevel@tonic-gate ppb_devstate_t *ppb; 11830Sstevel@tonic-gate uint16_t pmcsr; 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate /* 11860Sstevel@tonic-gate * Find out current power level 11870Sstevel@tonic-gate */ 11880Sstevel@tonic-gate ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 11890Sstevel@tonic-gate ddi_get_instance(pwr_p->pwr_dip)); 11900Sstevel@tonic-gate 11911624Spjha if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL, 11921774Spjha ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16) 11931624Spjha return (DDI_FAILURE); 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate switch (pmcsr & PCI_PMCSR_STATE_MASK) { 11960Sstevel@tonic-gate case PCI_PMCSR_D0: 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate return (PM_LEVEL_B0); 11990Sstevel@tonic-gate case PCI_PMCSR_D1: 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate return (PM_LEVEL_B1); 12020Sstevel@tonic-gate case PCI_PMCSR_D2: 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate return (PM_LEVEL_B2); 12050Sstevel@tonic-gate case PCI_PMCSR_D3HOT: 12060Sstevel@tonic-gate if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) { 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate return (PM_LEVEL_B2); 12090Sstevel@tonic-gate } else { 12100Sstevel@tonic-gate 12110Sstevel@tonic-gate return (PM_LEVEL_B3); 12120Sstevel@tonic-gate } 12130Sstevel@tonic-gate } 1214946Smathue /*NOTREACHED*/ 1215946Smathue return (PM_LEVEL_B3); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate 12180Sstevel@tonic-gate /* 12190Sstevel@tonic-gate * Power entry point. Called by the PM framework to change the 12200Sstevel@tonic-gate * current power state of the bus. This function must first verify that 12210Sstevel@tonic-gate * the requested power change is still valid. 12220Sstevel@tonic-gate */ 12230Sstevel@tonic-gate /*ARGSUSED*/ 12240Sstevel@tonic-gate static int 12250Sstevel@tonic-gate ppb_pwr(dev_info_t *dip, int component, int lvl) 12260Sstevel@tonic-gate { 12270Sstevel@tonic-gate ppb_devstate_t *ppb; 12280Sstevel@tonic-gate uint16_t pmcsr; 12290Sstevel@tonic-gate char *str; 12300Sstevel@tonic-gate int lowest_lvl; 12310Sstevel@tonic-gate int old_lvl; 12320Sstevel@tonic-gate int new_lvl; 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 12350Sstevel@tonic-gate ddi_get_instance(dip)); 12360Sstevel@tonic-gate if (ppb == NULL) { 12370Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d ppb_pwr: can't get soft state", 12380Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate return (DDI_FAILURE); 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate DEBUG1(DBG_PWR, dip, "ppb_pwr(): ENTER level = %d\n", lvl); 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate mutex_enter(&ppb->ppb_pwr_p->pwr_mutex); 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate /* 12480Sstevel@tonic-gate * Find out if the power setting is possible. If it is not, 12490Sstevel@tonic-gate * set component busy and return failure. If it is possible, 12500Sstevel@tonic-gate * and it is the lowest pwr setting possible, set component 12510Sstevel@tonic-gate * busy so that the framework does not try to lower any further. 12520Sstevel@tonic-gate */ 12530Sstevel@tonic-gate lowest_lvl = pci_pwr_new_lvl(ppb->ppb_pwr_p); 12540Sstevel@tonic-gate if (lowest_lvl > lvl) { 12550Sstevel@tonic-gate pci_pwr_component_busy(ppb->ppb_pwr_p); 12560Sstevel@tonic-gate DEBUG2(DBG_PWR, dip, "ppb_pwr: failing power request " 12570Sstevel@tonic-gate "lowest allowed is %d requested is %d\n", 12580Sstevel@tonic-gate lowest_lvl, lvl); 12590Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate return (DDI_FAILURE); 12620Sstevel@tonic-gate } else if (lowest_lvl == lvl) { 12630Sstevel@tonic-gate pci_pwr_component_busy(ppb->ppb_pwr_p); 12640Sstevel@tonic-gate } else { 12650Sstevel@tonic-gate pci_pwr_component_idle(ppb->ppb_pwr_p); 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate 12681624Spjha if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL, 12691774Spjha ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16) 12701624Spjha return (DDI_FAILURE); 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* 12730Sstevel@tonic-gate * Save the current power level. This is the actual function level, 12740Sstevel@tonic-gate * not the translated bridge level stored in pwr_p->current_lvl 12750Sstevel@tonic-gate */ 12760Sstevel@tonic-gate old_lvl = pmcsr & PCI_PMCSR_STATE_MASK; 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate pmcsr &= ~PCI_PMCSR_STATE_MASK; 12790Sstevel@tonic-gate switch (lvl) { 12800Sstevel@tonic-gate case PM_LEVEL_B0: 12810Sstevel@tonic-gate str = "PM_LEVEL_B0 (full speed)"; 12820Sstevel@tonic-gate pmcsr |= PCI_PMCSR_D0; 12830Sstevel@tonic-gate break; 12840Sstevel@tonic-gate case PM_LEVEL_B1: 12850Sstevel@tonic-gate str = "PM_LEVEL_B1 (light sleep. No bus traffic allowed)"; 12860Sstevel@tonic-gate if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) == 0) { 12870Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d PCI PM state B1 not supported", 12880Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 12910Sstevel@tonic-gate return (DDI_FAILURE); 12920Sstevel@tonic-gate } 12930Sstevel@tonic-gate pmcsr |= PCI_PMCSR_D1; 12940Sstevel@tonic-gate break; 12950Sstevel@tonic-gate case PM_LEVEL_B2: 12960Sstevel@tonic-gate str = "PM_LEVEL_B2 (clock off)"; 12970Sstevel@tonic-gate if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) == 0) { 12980Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d PM state B2 not supported...", 12990Sstevel@tonic-gate ddi_driver_name(dip), 13000Sstevel@tonic-gate ddi_get_instance(dip)); 13010Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate return (DDI_FAILURE); 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) { 13070Sstevel@tonic-gate /* 13080Sstevel@tonic-gate * If B3 isn't supported, use D3 for B2 to avoid the 13090Sstevel@tonic-gate * possible case that D2 for B2 isn't supported. 13100Sstevel@tonic-gate * Saves and extra check and state flag.. 13110Sstevel@tonic-gate */ 13120Sstevel@tonic-gate pmcsr |= PCI_PMCSR_D3HOT; 13130Sstevel@tonic-gate } else { 13140Sstevel@tonic-gate pmcsr |= PCI_PMCSR_D2; 13150Sstevel@tonic-gate } 13160Sstevel@tonic-gate break; 13170Sstevel@tonic-gate case PM_LEVEL_B3: 13180Sstevel@tonic-gate str = "PM_LEVEL_B30 (clock and power off)"; 13190Sstevel@tonic-gate if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) { 13200Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d PM state B3 not supported...", 13210Sstevel@tonic-gate ddi_driver_name(dip), 13220Sstevel@tonic-gate ddi_get_instance(dip)); 13230Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate return (DDI_FAILURE); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate pmcsr |= PCI_PMCSR_D3HOT; 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate break; 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate default: 13320Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d Unknown PM state %d", 13330Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), lvl); 13340Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate return (DDI_FAILURE); 13370Sstevel@tonic-gate } 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate new_lvl = pmcsr & PCI_PMCSR_STATE_MASK; 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate /* 13420Sstevel@tonic-gate * Save config regs if going into HW state D3 (B2 or B3) 13430Sstevel@tonic-gate */ 13440Sstevel@tonic-gate if ((old_lvl != PCI_PMCSR_D3HOT) && (new_lvl == PCI_PMCSR_D3HOT)) { 13450Sstevel@tonic-gate DEBUG0(DBG_PWR, dip, "ppb_pwr(): SAVING CONFIG REGS\n"); 13460Sstevel@tonic-gate if (pci_save_config_regs(dip) != DDI_SUCCESS) { 13470Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d Save config regs failed", 13480Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 13490Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate return (DDI_FAILURE); 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate } 13540Sstevel@tonic-gate 13551624Spjha PCI_CAP_PUT16(ppb->ppb_conf_hdl, NULL, ppb->ppb_pm_cap_ptr, PCI_PMCSR, 13561624Spjha pmcsr); 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate /* 13590Sstevel@tonic-gate * No bus transactions should occur without waiting for 13600Sstevel@tonic-gate * settle time specified in PCI PM spec rev 2.1 sec 5.6.1 13610Sstevel@tonic-gate * To make things simple, just use the max time specified for 13620Sstevel@tonic-gate * all state transitions. 13630Sstevel@tonic-gate */ 13640Sstevel@tonic-gate delay(drv_usectohz(PCI_CLK_SETTLE_TIME)); 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate /* 13670Sstevel@tonic-gate * Restore configuration registers if coming out of HW state D3 13680Sstevel@tonic-gate */ 13690Sstevel@tonic-gate if ((old_lvl == PCI_PMCSR_D3HOT) && (new_lvl != PCI_PMCSR_D3HOT)) { 13700Sstevel@tonic-gate DEBUG0(DBG_PWR, dip, "ppb_pwr(): RESTORING CONFIG REGS\n"); 13710Sstevel@tonic-gate if (pci_restore_config_regs(dip) != DDI_SUCCESS) { 13720Sstevel@tonic-gate panic("%s%d restore config regs failed", 13730Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 13740Sstevel@tonic-gate } 13750Sstevel@tonic-gate /*NOTREACHED*/ 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate ppb->ppb_pwr_p->current_lvl = lvl; 13790Sstevel@tonic-gate 13800Sstevel@tonic-gate mutex_exit(&ppb->ppb_pwr_p->pwr_mutex); 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate DEBUG1(DBG_PWR, dip, "ppb_set_pwr: set PM state to %s\n\n", str); 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate return (DDI_SUCCESS); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * Initialize hotplug framework if we are hotpluggable. 13890Sstevel@tonic-gate * Sets flag in the soft state if Hot Plug is supported and initialized 13900Sstevel@tonic-gate * properly. 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate /*ARGSUSED*/ 13930Sstevel@tonic-gate static void 13940Sstevel@tonic-gate ppb_init_hotplug(ppb_devstate_t *ppb) 13950Sstevel@tonic-gate { 13960Sstevel@tonic-gate if (ddi_prop_exists(DDI_DEV_T_ANY, ppb->dip, DDI_PROP_DONTPASS, 13970Sstevel@tonic-gate "hotplug-capable")) { 13980Sstevel@tonic-gate (void) modload("misc", "pcihp"); 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate if (pcihp_init(ppb->dip) != DDI_SUCCESS) { 14010Sstevel@tonic-gate cmn_err(CE_WARN, 14020Sstevel@tonic-gate "%s #%d: Failed setting hotplug framework", 14030Sstevel@tonic-gate ddi_driver_name(ppb->dip), 14040Sstevel@tonic-gate ddi_get_instance(ppb->dip)); 14050Sstevel@tonic-gate } else 14060Sstevel@tonic-gate ppb->hotplug_capable = B_TRUE; 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate static void 14120Sstevel@tonic-gate ppb_create_ranges_prop(dev_info_t *dip, 14130Sstevel@tonic-gate ddi_acc_handle_t config_handle) 14140Sstevel@tonic-gate { 14150Sstevel@tonic-gate uint32_t base, limit; 14160Sstevel@tonic-gate ppb_ranges_t ranges[PPB_RANGE_LEN]; 14170Sstevel@tonic-gate uint8_t io_base_lo, io_limit_lo; 14180Sstevel@tonic-gate uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit; 14190Sstevel@tonic-gate int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int); 14200Sstevel@tonic-gate 14210Sstevel@tonic-gate io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW); 14220Sstevel@tonic-gate io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW); 14230Sstevel@tonic-gate io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI); 14240Sstevel@tonic-gate io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI); 14250Sstevel@tonic-gate mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE); 14260Sstevel@tonic-gate mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT); 14270Sstevel@tonic-gate 14280Sstevel@tonic-gate /* 14290Sstevel@tonic-gate * Create ranges for IO space 14300Sstevel@tonic-gate */ 14310Sstevel@tonic-gate ranges[i].size_low = ranges[i].size_high = 0; 14320Sstevel@tonic-gate ranges[i].parent_mid = ranges[i].child_mid = 14330Sstevel@tonic-gate ranges[i].parent_high = 0; 14340Sstevel@tonic-gate ranges[i].child_high = ranges[i].parent_high |= 14350Sstevel@tonic-gate (PCI_REG_REL_M | PCI_ADDR_IO); 14360Sstevel@tonic-gate base = PPB_16bit_IOADDR(io_base_lo); 14370Sstevel@tonic-gate limit = PPB_16bit_IOADDR(io_limit_lo); 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate if ((io_base_lo & 0xf) == PPB_32BIT_IO) { 14400Sstevel@tonic-gate base = PPB_LADDR(base, io_base_hi); 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate if ((io_limit_lo & 0xf) == PPB_32BIT_IO) { 14430Sstevel@tonic-gate limit = PPB_LADDR(limit, io_limit_hi); 14440Sstevel@tonic-gate } 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate if ((io_base_lo & PPB_32BIT_IO) && (io_limit_hi > 0)) { 14470Sstevel@tonic-gate base = PPB_LADDR(base, io_base_hi); 14480Sstevel@tonic-gate limit = PPB_LADDR(limit, io_limit_hi); 14490Sstevel@tonic-gate } 14500Sstevel@tonic-gate 14510Sstevel@tonic-gate /* 14520Sstevel@tonic-gate * Create ranges for 32bit memory space 14530Sstevel@tonic-gate */ 14540Sstevel@tonic-gate base = PPB_32bit_MEMADDR(mem_base); 14550Sstevel@tonic-gate limit = PPB_32bit_MEMADDR(mem_limit); 14560Sstevel@tonic-gate ranges[i].size_low = ranges[i].size_high = 0; 14570Sstevel@tonic-gate ranges[i].parent_mid = ranges[i].child_mid = 14580Sstevel@tonic-gate ranges[i].parent_high = 0; 14590Sstevel@tonic-gate ranges[i].child_high = ranges[i].parent_high |= 14600Sstevel@tonic-gate (PCI_REG_REL_M | PCI_ADDR_MEM32); 14610Sstevel@tonic-gate ranges[i].child_low = ranges[i].parent_low = base; 14620Sstevel@tonic-gate if (limit >= base) { 14630Sstevel@tonic-gate ranges[i].size_low = limit - base + PPB_MEMGRAIN; 14640Sstevel@tonic-gate i++; 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate if (i) { 14680Sstevel@tonic-gate (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges", 14690Sstevel@tonic-gate (int *)ranges, i * rangelen); 14700Sstevel@tonic-gate } 14710Sstevel@tonic-gate } 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* ARGSUSED */ 14740Sstevel@tonic-gate static int 14750Sstevel@tonic-gate ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp) 14760Sstevel@tonic-gate { 14770Sstevel@tonic-gate ppb_devstate_t *ppb_p; 14780Sstevel@tonic-gate minor_t minor = getminor(*devp); 14790Sstevel@tonic-gate int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor); 14800Sstevel@tonic-gate 14810Sstevel@tonic-gate /* 14820Sstevel@tonic-gate * Make sure the open is for the right file type. 14830Sstevel@tonic-gate */ 14840Sstevel@tonic-gate if (otyp != OTYP_CHR) 14850Sstevel@tonic-gate return (EINVAL); 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * Get the soft state structure for the device. 14890Sstevel@tonic-gate */ 14900Sstevel@tonic-gate ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 14910Sstevel@tonic-gate instance); 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate if (ppb_p == NULL) 14940Sstevel@tonic-gate return (ENXIO); 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate if (ppb_p->hotplug_capable == B_TRUE) 14970Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_open(devp, flags, 14980Sstevel@tonic-gate otyp, credp)); 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate /* 15010Sstevel@tonic-gate * Handle the open by tracking the device state. 15020Sstevel@tonic-gate */ 15030Sstevel@tonic-gate mutex_enter(&ppb_p->ppb_mutex); 15040Sstevel@tonic-gate if (flags & FEXCL) { 15050Sstevel@tonic-gate if (ppb_p->ppb_soft_state != PPB_SOFT_STATE_CLOSED) { 15060Sstevel@tonic-gate mutex_exit(&ppb_p->ppb_mutex); 15070Sstevel@tonic-gate return (EBUSY); 15080Sstevel@tonic-gate } 15090Sstevel@tonic-gate ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN_EXCL; 15100Sstevel@tonic-gate } else { 15110Sstevel@tonic-gate if (ppb_p->ppb_soft_state == PPB_SOFT_STATE_OPEN_EXCL) { 15120Sstevel@tonic-gate mutex_exit(&ppb_p->ppb_mutex); 15130Sstevel@tonic-gate return (EBUSY); 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN; 15160Sstevel@tonic-gate } 15170Sstevel@tonic-gate mutex_exit(&ppb_p->ppb_mutex); 15180Sstevel@tonic-gate return (0); 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate /* ARGSUSED */ 15230Sstevel@tonic-gate static int 15240Sstevel@tonic-gate ppb_close(dev_t dev, int flags, int otyp, cred_t *credp) 15250Sstevel@tonic-gate { 15260Sstevel@tonic-gate ppb_devstate_t *ppb_p; 15270Sstevel@tonic-gate minor_t minor = getminor(dev); 15280Sstevel@tonic-gate int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor); 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate if (otyp != OTYP_CHR) 15310Sstevel@tonic-gate return (EINVAL); 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 15340Sstevel@tonic-gate instance); 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate if (ppb_p == NULL) 15370Sstevel@tonic-gate return (ENXIO); 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate if (ppb_p->hotplug_capable == B_TRUE) 15400Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_close(dev, flags, 15410Sstevel@tonic-gate otyp, credp)); 15420Sstevel@tonic-gate 15430Sstevel@tonic-gate mutex_enter(&ppb_p->ppb_mutex); 15440Sstevel@tonic-gate ppb_p->ppb_soft_state = PPB_SOFT_STATE_CLOSED; 15450Sstevel@tonic-gate mutex_exit(&ppb_p->ppb_mutex); 15460Sstevel@tonic-gate return (0); 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate /* 15510Sstevel@tonic-gate * ppb_ioctl: devctl hotplug controls 15520Sstevel@tonic-gate */ 15530Sstevel@tonic-gate /* ARGSUSED */ 15540Sstevel@tonic-gate static int 15550Sstevel@tonic-gate ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 15560Sstevel@tonic-gate int *rvalp) 15570Sstevel@tonic-gate { 15580Sstevel@tonic-gate ppb_devstate_t *ppb_p; 15590Sstevel@tonic-gate dev_info_t *self; 15600Sstevel@tonic-gate struct devctl_iocdata *dcp; 15610Sstevel@tonic-gate uint_t bus_state; 15620Sstevel@tonic-gate int rv = 0; 15630Sstevel@tonic-gate minor_t minor = getminor(dev); 15640Sstevel@tonic-gate int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor); 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 15670Sstevel@tonic-gate instance); 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate if (ppb_p == NULL) 15700Sstevel@tonic-gate return (ENXIO); 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate if (ppb_p->hotplug_capable == B_TRUE) 15730Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, 15740Sstevel@tonic-gate arg, mode, credp, rvalp)); 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate self = ppb_p->dip; 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate /* 15790Sstevel@tonic-gate * We can use the generic implementation for these ioctls 15800Sstevel@tonic-gate */ 15810Sstevel@tonic-gate switch (cmd) { 15820Sstevel@tonic-gate case DEVCTL_DEVICE_GETSTATE: 15830Sstevel@tonic-gate case DEVCTL_DEVICE_ONLINE: 15840Sstevel@tonic-gate case DEVCTL_DEVICE_OFFLINE: 15850Sstevel@tonic-gate case DEVCTL_BUS_GETSTATE: 15860Sstevel@tonic-gate return (ndi_devctl_ioctl(self, cmd, arg, mode, 0)); 15870Sstevel@tonic-gate } 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate /* 15900Sstevel@tonic-gate * read devctl ioctl data 15910Sstevel@tonic-gate */ 15920Sstevel@tonic-gate if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 15930Sstevel@tonic-gate return (EFAULT); 15940Sstevel@tonic-gate 15950Sstevel@tonic-gate switch (cmd) { 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate case DEVCTL_DEVICE_RESET: 15980Sstevel@tonic-gate rv = ENOTSUP; 15990Sstevel@tonic-gate break; 16000Sstevel@tonic-gate 16010Sstevel@tonic-gate case DEVCTL_BUS_QUIESCE: 16020Sstevel@tonic-gate if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) 16030Sstevel@tonic-gate if (bus_state == BUS_QUIESCED) 16040Sstevel@tonic-gate break; 16050Sstevel@tonic-gate (void) ndi_set_bus_state(self, BUS_QUIESCED); 16060Sstevel@tonic-gate break; 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate case DEVCTL_BUS_UNQUIESCE: 16090Sstevel@tonic-gate if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) 16100Sstevel@tonic-gate if (bus_state == BUS_ACTIVE) 16110Sstevel@tonic-gate break; 16120Sstevel@tonic-gate (void) ndi_set_bus_state(self, BUS_ACTIVE); 16130Sstevel@tonic-gate break; 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate case DEVCTL_BUS_RESET: 16160Sstevel@tonic-gate rv = ENOTSUP; 16170Sstevel@tonic-gate break; 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate case DEVCTL_BUS_RESETALL: 16200Sstevel@tonic-gate rv = ENOTSUP; 16210Sstevel@tonic-gate break; 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate default: 16240Sstevel@tonic-gate rv = ENOTTY; 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate ndi_dc_freehdl(dcp); 16280Sstevel@tonic-gate return (rv); 16290Sstevel@tonic-gate } 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, 16320Sstevel@tonic-gate int flags, char *name, caddr_t valuep, int *lengthp) 16330Sstevel@tonic-gate { 16340Sstevel@tonic-gate ppb_devstate_t *ppb_p; 16350Sstevel@tonic-gate minor_t minor = getminor(dev); 16360Sstevel@tonic-gate int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor); 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 16390Sstevel@tonic-gate instance); 16400Sstevel@tonic-gate 16410Sstevel@tonic-gate if (ppb_p == NULL) 16420Sstevel@tonic-gate return (ENXIO); 16430Sstevel@tonic-gate 16440Sstevel@tonic-gate if (ppb_p->hotplug_capable == B_TRUE) 16450Sstevel@tonic-gate return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op, 16460Sstevel@tonic-gate flags, name, valuep, lengthp)); 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp)); 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate 1651*3156Sgirish static int 1652*3156Sgirish ppb_get_bdf_from_dip(dev_info_t *dip, uint32_t *bdf) 1653*3156Sgirish { 1654*3156Sgirish pci_regspec_t *regspec; 1655*3156Sgirish int reglen; 1656*3156Sgirish 1657*3156Sgirish if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1658*3156Sgirish "reg", (int **)®spec, (uint_t *)®len) != DDI_SUCCESS) 1659*3156Sgirish return (DDI_FAILURE); 1660*3156Sgirish 1661*3156Sgirish if (reglen < (sizeof (pci_regspec_t) / sizeof (int))) { 1662*3156Sgirish ddi_prop_free(regspec); 1663*3156Sgirish return (DDI_FAILURE); 1664*3156Sgirish } 1665*3156Sgirish 1666*3156Sgirish /* Get phys_hi from first element. All have same bdf. */ 1667*3156Sgirish *bdf = (regspec->pci_phys_hi & (PCI_REG_BDFR_M ^ PCI_REG_REG_M)) >> 8; 1668*3156Sgirish 1669*3156Sgirish ddi_prop_free(regspec); 1670*3156Sgirish return (DDI_SUCCESS); 1671*3156Sgirish } 1672*3156Sgirish 16730Sstevel@tonic-gate /* 16740Sstevel@tonic-gate * Initialize our FMA resources 16750Sstevel@tonic-gate */ 16760Sstevel@tonic-gate static void 16770Sstevel@tonic-gate ppb_fm_init(ppb_devstate_t *ppb_p) 16780Sstevel@tonic-gate { 16790Sstevel@tonic-gate ppb_p->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE | 16800Sstevel@tonic-gate DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE; 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate /* 16830Sstevel@tonic-gate * Request our capability level and get our parents capability 16840Sstevel@tonic-gate * and ibc. 16850Sstevel@tonic-gate */ 16860Sstevel@tonic-gate ddi_fm_init(ppb_p->dip, &ppb_p->fm_cap, &ppb_p->fm_ibc); 16870Sstevel@tonic-gate ASSERT((ppb_p->fm_cap & DDI_FM_EREPORT_CAPABLE) && 16880Sstevel@tonic-gate (ppb_p->fm_cap & DDI_FM_ERRCB_CAPABLE)); 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate pci_ereport_setup(ppb_p->dip); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate /* 16930Sstevel@tonic-gate * Register error callback with our parent. 16940Sstevel@tonic-gate */ 16950Sstevel@tonic-gate ddi_fm_handler_register(ppb_p->dip, ppb_err_callback, NULL); 16960Sstevel@tonic-gate } 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate /* 16990Sstevel@tonic-gate * Breakdown our FMA resources 17000Sstevel@tonic-gate */ 17010Sstevel@tonic-gate static void 17020Sstevel@tonic-gate ppb_fm_fini(ppb_devstate_t *ppb_p) 17030Sstevel@tonic-gate { 17040Sstevel@tonic-gate /* 17050Sstevel@tonic-gate * Clean up allocated fm structures 17060Sstevel@tonic-gate */ 17070Sstevel@tonic-gate ddi_fm_handler_unregister(ppb_p->dip); 17080Sstevel@tonic-gate pci_ereport_teardown(ppb_p->dip); 17090Sstevel@tonic-gate ddi_fm_fini(ppb_p->dip); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate /* 17130Sstevel@tonic-gate * Initialize FMA resources for children devices. Called when 17140Sstevel@tonic-gate * child calls ddi_fm_init(). 17150Sstevel@tonic-gate */ 17160Sstevel@tonic-gate /*ARGSUSED*/ 17170Sstevel@tonic-gate static int 17180Sstevel@tonic-gate ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap, 17190Sstevel@tonic-gate ddi_iblock_cookie_t *ibc) 17200Sstevel@tonic-gate { 17210Sstevel@tonic-gate ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, 17220Sstevel@tonic-gate ddi_get_instance(dip)); 17230Sstevel@tonic-gate *ibc = ppb_p->fm_ibc; 17240Sstevel@tonic-gate return (ppb_p->fm_cap); 17250Sstevel@tonic-gate } 17260Sstevel@tonic-gate 17270Sstevel@tonic-gate /* 17280Sstevel@tonic-gate * FMA registered error callback 17290Sstevel@tonic-gate */ 17300Sstevel@tonic-gate static int 17310Sstevel@tonic-gate ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data) 17320Sstevel@tonic-gate { 17330Sstevel@tonic-gate ASSERT(impl_data == NULL); 17341865Sdilpreet pci_ereport_post(dip, derr, NULL); 17351865Sdilpreet return (derr->fme_status); 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate static void 17390Sstevel@tonic-gate ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle) 17400Sstevel@tonic-gate { 17410Sstevel@tonic-gate i_ndi_busop_access_enter(dip, handle); 17420Sstevel@tonic-gate } 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate /* ARGSUSED */ 17450Sstevel@tonic-gate static void 17460Sstevel@tonic-gate ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle) 17470Sstevel@tonic-gate { 17480Sstevel@tonic-gate i_ndi_busop_access_exit(dip, handle); 17490Sstevel@tonic-gate } 1750