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 51865Sdilpreet * Common Development and Distribution License (the "License"). 61865Sdilpreet * 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 */ 211900Seota 220Sstevel@tonic-gate /* 23*7240Srh87107 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * sun4 specific DDI implementation 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate #include <sys/cpuvar.h> 330Sstevel@tonic-gate #include <sys/ddi_subrdefs.h> 340Sstevel@tonic-gate #include <sys/machsystm.h> 350Sstevel@tonic-gate #include <sys/sunndi.h> 360Sstevel@tonic-gate #include <sys/sysmacros.h> 370Sstevel@tonic-gate #include <sys/ontrap.h> 380Sstevel@tonic-gate #include <vm/seg_kmem.h> 390Sstevel@tonic-gate #include <sys/membar.h> 400Sstevel@tonic-gate #include <sys/dditypes.h> 410Sstevel@tonic-gate #include <sys/ndifm.h> 420Sstevel@tonic-gate #include <sys/fm/io/ddi.h> 430Sstevel@tonic-gate #include <sys/ivintr.h> 440Sstevel@tonic-gate #include <sys/bootconf.h> 450Sstevel@tonic-gate #include <sys/conf.h> 460Sstevel@tonic-gate #include <sys/ethernet.h> 470Sstevel@tonic-gate #include <sys/idprom.h> 480Sstevel@tonic-gate #include <sys/promif.h> 490Sstevel@tonic-gate #include <sys/prom_plat.h> 500Sstevel@tonic-gate #include <sys/systeminfo.h> 510Sstevel@tonic-gate #include <sys/fpu/fpusystm.h> 520Sstevel@tonic-gate #include <sys/vm.h> 530Sstevel@tonic-gate #include <sys/fs/dv_node.h> 540Sstevel@tonic-gate #include <sys/fs/snode.h> 550Sstevel@tonic-gate #include <sys/ddi_isa.h> 561991Sheppo #include <sys/modhash.h> 574845Svikram #include <sys/modctl.h> 584845Svikram #include <sys/sunldi_impl.h> 590Sstevel@tonic-gate 600Sstevel@tonic-gate dev_info_t *get_intr_parent(dev_info_t *, dev_info_t *, 61693Sgovinda ddi_intr_handle_impl_t *); 620Sstevel@tonic-gate #pragma weak get_intr_parent 630Sstevel@tonic-gate 640Sstevel@tonic-gate int process_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t, 650Sstevel@tonic-gate ddi_intr_handle_impl_t *, void *); 660Sstevel@tonic-gate #pragma weak process_intr_ops 670Sstevel@tonic-gate 680Sstevel@tonic-gate void cells_1275_copy(prop_1275_cell_t *, prop_1275_cell_t *, int32_t); 690Sstevel@tonic-gate prop_1275_cell_t *cells_1275_cmp(prop_1275_cell_t *, prop_1275_cell_t *, 700Sstevel@tonic-gate int32_t len); 710Sstevel@tonic-gate #pragma weak cells_1275_copy 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * Wrapper for ddi_prop_lookup_int_array(). 750Sstevel@tonic-gate * This is handy because it returns the prop length in 760Sstevel@tonic-gate * bytes which is what most of the callers require. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate 790Sstevel@tonic-gate static int 800Sstevel@tonic-gate get_prop_int_array(dev_info_t *di, char *pname, int **pval, uint_t *plen) 810Sstevel@tonic-gate { 820Sstevel@tonic-gate int ret; 830Sstevel@tonic-gate 840Sstevel@tonic-gate if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, di, 850Sstevel@tonic-gate DDI_PROP_DONTPASS, pname, pval, plen)) == DDI_PROP_SUCCESS) { 860Sstevel@tonic-gate *plen = (*plen) * (uint_t)sizeof (int); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate return (ret); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* 920Sstevel@tonic-gate * SECTION: DDI Node Configuration 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * init_regspec_64: 970Sstevel@tonic-gate * 980Sstevel@tonic-gate * If the parent #size-cells is 2, convert the upa-style or 990Sstevel@tonic-gate * safari-style reg property from 2-size cells to 1 size cell 1000Sstevel@tonic-gate * format, ignoring the size_hi, which must be zero for devices. 1010Sstevel@tonic-gate * (It won't be zero in the memory list properties in the memory 1020Sstevel@tonic-gate * nodes, but that doesn't matter here.) 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate struct ddi_parent_private_data * 1050Sstevel@tonic-gate init_regspec_64(dev_info_t *dip) 1060Sstevel@tonic-gate { 1070Sstevel@tonic-gate struct ddi_parent_private_data *pd; 1080Sstevel@tonic-gate dev_info_t *parent; 1090Sstevel@tonic-gate int size_cells; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * If there are no "reg"s in the child node, return. 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate pd = ddi_get_parent_data(dip); 1150Sstevel@tonic-gate if ((pd == NULL) || (pd->par_nreg == 0)) { 1160Sstevel@tonic-gate return (pd); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate parent = ddi_get_parent(dip); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent, 1210Sstevel@tonic-gate DDI_PROP_DONTPASS, "#size-cells", 1); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate if (size_cells != 1) { 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate int n, j; 1260Sstevel@tonic-gate struct regspec *irp; 1270Sstevel@tonic-gate struct reg_64 { 1280Sstevel@tonic-gate uint_t addr_hi, addr_lo, size_hi, size_lo; 1290Sstevel@tonic-gate }; 1300Sstevel@tonic-gate struct reg_64 *r64_rp; 1310Sstevel@tonic-gate struct regspec *rp; 1320Sstevel@tonic-gate uint_t len = 0; 1330Sstevel@tonic-gate int *reg_prop; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate ASSERT(size_cells == 2); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * We already looked the property up once before if 1390Sstevel@tonic-gate * pd is non-NULL. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate (void) ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 1420Sstevel@tonic-gate DDI_PROP_DONTPASS, OBP_REG, ®_prop, &len); 1430Sstevel@tonic-gate ASSERT(len != 0); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate n = sizeof (struct reg_64) / sizeof (int); 1460Sstevel@tonic-gate n = len / n; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * We're allocating a buffer the size of the PROM's property, 1500Sstevel@tonic-gate * but we're only using a smaller portion when we assign it 1510Sstevel@tonic-gate * to a regspec. We do this so that in the 1520Sstevel@tonic-gate * impl_ddi_sunbus_removechild function, we will 1530Sstevel@tonic-gate * always free the right amount of memory. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate irp = rp = (struct regspec *)reg_prop; 1560Sstevel@tonic-gate r64_rp = (struct reg_64 *)pd->par_reg; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate for (j = 0; j < n; ++j, ++rp, ++r64_rp) { 1590Sstevel@tonic-gate ASSERT(r64_rp->size_hi == 0); 1600Sstevel@tonic-gate rp->regspec_bustype = r64_rp->addr_hi; 1610Sstevel@tonic-gate rp->regspec_addr = r64_rp->addr_lo; 1620Sstevel@tonic-gate rp->regspec_size = r64_rp->size_lo; 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate ddi_prop_free((void *)pd->par_reg); 1660Sstevel@tonic-gate pd->par_nreg = n; 1670Sstevel@tonic-gate pd->par_reg = irp; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate return (pd); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Create a ddi_parent_private_data structure from the ddi properties of 1740Sstevel@tonic-gate * the dev_info node. 1750Sstevel@tonic-gate * 1760Sstevel@tonic-gate * The "reg" is required if the driver wishes to create mappings on behalf 1770Sstevel@tonic-gate * of the device. The "reg" property is assumed to be a list of at least 1780Sstevel@tonic-gate * one triplet 1790Sstevel@tonic-gate * 1800Sstevel@tonic-gate * <bustype, address, size>*1 1810Sstevel@tonic-gate * 1820Sstevel@tonic-gate * The "interrupt" property is no longer part of parent private data on 1830Sstevel@tonic-gate * sun4u. The interrupt parent is may not be the device tree parent. 1840Sstevel@tonic-gate * 1850Sstevel@tonic-gate * The "ranges" property describes the mapping of child addresses to parent 1860Sstevel@tonic-gate * addresses. 1870Sstevel@tonic-gate * 1880Sstevel@tonic-gate * N.B. struct rangespec is defined for the following default values: 1890Sstevel@tonic-gate * parent child 1900Sstevel@tonic-gate * #address-cells 2 2 1910Sstevel@tonic-gate * #size-cells 1 1 1920Sstevel@tonic-gate * This function doesn't deal with non-default cells and will not create 1930Sstevel@tonic-gate * ranges in such cases. 1940Sstevel@tonic-gate */ 1950Sstevel@tonic-gate void 1960Sstevel@tonic-gate make_ddi_ppd(dev_info_t *child, struct ddi_parent_private_data **ppd) 1970Sstevel@tonic-gate { 1980Sstevel@tonic-gate struct ddi_parent_private_data *pdptr; 1990Sstevel@tonic-gate int *reg_prop, *rng_prop; 2000Sstevel@tonic-gate uint_t reg_len = 0, rng_len = 0; 2010Sstevel@tonic-gate dev_info_t *parent; 2020Sstevel@tonic-gate int parent_addr_cells, parent_size_cells; 2030Sstevel@tonic-gate int child_addr_cells, child_size_cells; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate *ppd = pdptr = kmem_zalloc(sizeof (*pdptr), KM_SLEEP); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * root node has no parent private data, so *ppd should 2090Sstevel@tonic-gate * be initialized for naming to work properly. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate if ((parent = ddi_get_parent(child)) == NULL) 2120Sstevel@tonic-gate return; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * Set reg field of parent data from "reg" property 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate if ((get_prop_int_array(child, OBP_REG, ®_prop, ®_len) 2180Sstevel@tonic-gate == DDI_PROP_SUCCESS) && (reg_len != 0)) { 2190Sstevel@tonic-gate pdptr->par_nreg = (int)(reg_len / sizeof (struct regspec)); 2200Sstevel@tonic-gate pdptr->par_reg = (struct regspec *)reg_prop; 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* 2240Sstevel@tonic-gate * "ranges" property ... 2250Sstevel@tonic-gate * 2260Sstevel@tonic-gate * This function does not handle cases where #address-cells != 2 2270Sstevel@tonic-gate * and * min(parent, child) #size-cells != 1 (see bugid 4211124). 2280Sstevel@tonic-gate * 2290Sstevel@tonic-gate * Nexus drivers with such exceptions (e.g. pci ranges) 2300Sstevel@tonic-gate * should either create a separate function for handling 2310Sstevel@tonic-gate * ranges or not use parent private data to store ranges. 2320Sstevel@tonic-gate */ 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* root node has no ranges */ 2350Sstevel@tonic-gate if ((parent = ddi_get_parent(child)) == NULL) 2360Sstevel@tonic-gate return; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate child_addr_cells = ddi_prop_get_int(DDI_DEV_T_ANY, child, 2390Sstevel@tonic-gate DDI_PROP_DONTPASS, "#address-cells", 2); 2400Sstevel@tonic-gate child_size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, child, 2410Sstevel@tonic-gate DDI_PROP_DONTPASS, "#size-cells", 1); 2420Sstevel@tonic-gate parent_addr_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent, 2430Sstevel@tonic-gate DDI_PROP_DONTPASS, "#address-cells", 2); 2440Sstevel@tonic-gate parent_size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent, 2450Sstevel@tonic-gate DDI_PROP_DONTPASS, "#size-cells", 1); 2460Sstevel@tonic-gate if (child_addr_cells != 2 || parent_addr_cells != 2 || 2470Sstevel@tonic-gate (child_size_cells != 1 && parent_size_cells != 1)) { 2480Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "!ranges not made in parent data; " 2490Sstevel@tonic-gate "#address-cells or #size-cells have non-default value")); 2500Sstevel@tonic-gate return; 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if (get_prop_int_array(child, OBP_RANGES, &rng_prop, &rng_len) 2540Sstevel@tonic-gate == DDI_PROP_SUCCESS) { 2550Sstevel@tonic-gate pdptr->par_nrng = rng_len / (int)(sizeof (struct rangespec)); 2560Sstevel@tonic-gate pdptr->par_rng = (struct rangespec *)rng_prop; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* 2610Sstevel@tonic-gate * Free ddi_parent_private_data structure 2620Sstevel@tonic-gate */ 2630Sstevel@tonic-gate void 2640Sstevel@tonic-gate impl_free_ddi_ppd(dev_info_t *dip) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate struct ddi_parent_private_data *pdptr = ddi_get_parent_data(dip); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate if (pdptr == NULL) 2690Sstevel@tonic-gate return; 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate if (pdptr->par_nrng != 0) 2720Sstevel@tonic-gate ddi_prop_free((void *)pdptr->par_rng); 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (pdptr->par_nreg != 0) 2750Sstevel@tonic-gate ddi_prop_free((void *)pdptr->par_reg); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate kmem_free(pdptr, sizeof (*pdptr)); 2780Sstevel@tonic-gate ddi_set_parent_data(dip, NULL); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * Name a child of sun busses based on the reg spec. 2830Sstevel@tonic-gate * Handles the following properties: 2840Sstevel@tonic-gate * 2850Sstevel@tonic-gate * Property value 2860Sstevel@tonic-gate * Name type 2870Sstevel@tonic-gate * 2880Sstevel@tonic-gate * reg register spec 2890Sstevel@tonic-gate * interrupts new (bus-oriented) interrupt spec 2900Sstevel@tonic-gate * ranges range spec 2910Sstevel@tonic-gate * 2920Sstevel@tonic-gate * This may be called multiple times, independent of 2930Sstevel@tonic-gate * initchild calls. 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate static int 2960Sstevel@tonic-gate impl_sunbus_name_child(dev_info_t *child, char *name, int namelen) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate struct ddi_parent_private_data *pdptr; 2990Sstevel@tonic-gate struct regspec *rp; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* 3020Sstevel@tonic-gate * Fill in parent-private data and this function returns to us 3030Sstevel@tonic-gate * an indication if it used "registers" to fill in the data. 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate if (ddi_get_parent_data(child) == NULL) { 3060Sstevel@tonic-gate make_ddi_ppd(child, &pdptr); 3070Sstevel@tonic-gate ddi_set_parent_data(child, pdptr); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* 3110Sstevel@tonic-gate * No reg property, return null string as address 3120Sstevel@tonic-gate * (e.g. root node) 3130Sstevel@tonic-gate */ 3140Sstevel@tonic-gate name[0] = '\0'; 3150Sstevel@tonic-gate if (sparc_pd_getnreg(child) == 0) { 3160Sstevel@tonic-gate return (DDI_SUCCESS); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate rp = sparc_pd_getreg(child, 0); 3200Sstevel@tonic-gate (void) snprintf(name, namelen, "%x,%x", 3210Sstevel@tonic-gate rp->regspec_bustype, rp->regspec_addr); 3220Sstevel@tonic-gate return (DDI_SUCCESS); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate /* 3270Sstevel@tonic-gate * Called from the bus_ctl op of some drivers. 3280Sstevel@tonic-gate * to implement the DDI_CTLOPS_INITCHILD operation. 3290Sstevel@tonic-gate * 3300Sstevel@tonic-gate * NEW drivers should NOT use this function, but should declare 3310Sstevel@tonic-gate * there own initchild/uninitchild handlers. (This function assumes 3320Sstevel@tonic-gate * the layout of the parent private data and the format of "reg", 3330Sstevel@tonic-gate * "ranges", "interrupts" properties and that #address-cells and 3340Sstevel@tonic-gate * #size-cells of the parent bus are defined to be default values.) 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate int 3370Sstevel@tonic-gate impl_ddi_sunbus_initchild(dev_info_t *child) 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate char name[MAXNAMELEN]; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate (void) impl_sunbus_name_child(child, name, MAXNAMELEN); 3420Sstevel@tonic-gate ddi_set_name_addr(child, name); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * Try to merge .conf node. If successful, return failure to 3460Sstevel@tonic-gate * remove this child. 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate if ((ndi_dev_is_persistent_node(child) == 0) && 3490Sstevel@tonic-gate (ndi_merge_node(child, impl_sunbus_name_child) == DDI_SUCCESS)) { 3500Sstevel@tonic-gate impl_ddi_sunbus_removechild(child); 3510Sstevel@tonic-gate return (DDI_FAILURE); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate return (DDI_SUCCESS); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * A better name for this function would be impl_ddi_sunbus_uninitchild() 3580Sstevel@tonic-gate * It does not remove the child, it uninitializes it, reclaiming the 3590Sstevel@tonic-gate * resources taken by impl_ddi_sunbus_initchild. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate void 3620Sstevel@tonic-gate impl_ddi_sunbus_removechild(dev_info_t *dip) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate impl_free_ddi_ppd(dip); 3650Sstevel@tonic-gate ddi_set_name_addr(dip, NULL); 3660Sstevel@tonic-gate /* 3670Sstevel@tonic-gate * Strip the node to properly convert it back to prototype form 3680Sstevel@tonic-gate */ 3690Sstevel@tonic-gate impl_rem_dev_props(dip); 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * SECTION: DDI Interrupt 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate void 3770Sstevel@tonic-gate cells_1275_copy(prop_1275_cell_t *from, prop_1275_cell_t *to, int32_t len) 3780Sstevel@tonic-gate { 3790Sstevel@tonic-gate int i; 3800Sstevel@tonic-gate for (i = 0; i < len; i++) 3810Sstevel@tonic-gate *to = *from; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate prop_1275_cell_t * 3850Sstevel@tonic-gate cells_1275_cmp(prop_1275_cell_t *cell1, prop_1275_cell_t *cell2, int32_t len) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate prop_1275_cell_t *match_cell = 0; 3880Sstevel@tonic-gate int32_t i; 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate for (i = 0; i < len; i++) 3910Sstevel@tonic-gate if (cell1[i] != cell2[i]) { 3920Sstevel@tonic-gate match_cell = &cell1[i]; 3930Sstevel@tonic-gate break; 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate return (match_cell); 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate /* 400693Sgovinda * get_intr_parent() is a generic routine that process a 1275 interrupt 401693Sgovinda * map (imap) property. This function returns a dev_info_t structure 402693Sgovinda * which claims ownership of the interrupt domain. 403693Sgovinda * It also returns the new interrupt translation within this new domain. 404693Sgovinda * If an interrupt-parent or interrupt-map property are not found, 405693Sgovinda * then we fallback to using the device tree's parent. 406693Sgovinda * 407693Sgovinda * imap entry format: 408693Sgovinda * <reg>,<interrupt>,<phandle>,<translated interrupt> 409693Sgovinda * reg - The register specification in the interrupts domain 410693Sgovinda * interrupt - The interrupt specification 411693Sgovinda * phandle - PROM handle of the device that owns the xlated interrupt domain 412693Sgovinda * translated interrupt - interrupt specifier in the parents domain 413693Sgovinda * note: <reg>,<interrupt> - The reg and interrupt can be combined to create 414693Sgovinda * a unique entry called a unit interrupt specifier. 415693Sgovinda * 416693Sgovinda * Here's the processing steps: 417693Sgovinda * step1 - If the interrupt-parent property exists, create the ispec and 418693Sgovinda * return the dip of the interrupt parent. 419693Sgovinda * step2 - Extract the interrupt-map property and the interrupt-map-mask 420693Sgovinda * If these don't exist, just return the device tree parent. 421693Sgovinda * step3 - build up the unit interrupt specifier to match against the 422693Sgovinda * interrupt map property 423693Sgovinda * step4 - Scan the interrupt-map property until a match is found 424693Sgovinda * step4a - Extract the interrupt parent 425693Sgovinda * step4b - Compare the unit interrupt specifier 4260Sstevel@tonic-gate */ 427693Sgovinda dev_info_t * 428693Sgovinda get_intr_parent(dev_info_t *pdip, dev_info_t *dip, ddi_intr_handle_impl_t *hdlp) 4290Sstevel@tonic-gate { 430693Sgovinda prop_1275_cell_t *imap, *imap_mask, *scan, *reg_p, *match_req; 431693Sgovinda int32_t imap_sz, imap_cells, imap_scan_cells, imap_mask_sz, 432693Sgovinda addr_cells, intr_cells, reg_len, i, j; 433693Sgovinda int32_t match_found = 0; 434693Sgovinda dev_info_t *intr_parent_dip = NULL; 435693Sgovinda uint32_t *intr = &hdlp->ih_vector; 436693Sgovinda uint32_t nodeid; 437693Sgovinda #ifdef DEBUG 438693Sgovinda static int debug = 0; 439693Sgovinda #endif 4400Sstevel@tonic-gate 441693Sgovinda /* 442693Sgovinda * step1 443693Sgovinda * If we have an interrupt-parent property, this property represents 444693Sgovinda * the nodeid of our interrupt parent. 445693Sgovinda */ 446693Sgovinda if ((nodeid = ddi_getprop(DDI_DEV_T_ANY, dip, 0, 447693Sgovinda "interrupt-parent", -1)) != -1) { 448693Sgovinda intr_parent_dip = e_ddi_nodeid_to_dip(nodeid); 449693Sgovinda ASSERT(intr_parent_dip); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* 452693Sgovinda * Attach the interrupt parent. 453693Sgovinda * 454693Sgovinda * N.B. e_ddi_nodeid_to_dip() isn't safe under DR. 455693Sgovinda * Also, interrupt parent isn't held. This needs 456693Sgovinda * to be revisited if DR-capable platforms implement 457693Sgovinda * interrupt redirection. 458693Sgovinda */ 459693Sgovinda if (i_ddi_attach_node_hierarchy(intr_parent_dip) 460693Sgovinda != DDI_SUCCESS) { 461693Sgovinda ndi_rele_devi(intr_parent_dip); 462693Sgovinda return (NULL); 463693Sgovinda } 464693Sgovinda 465693Sgovinda return (intr_parent_dip); 466693Sgovinda } 467693Sgovinda 468693Sgovinda /* 469693Sgovinda * step2 470693Sgovinda * Get interrupt map structure from PROM property 471693Sgovinda */ 472693Sgovinda if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, 473693Sgovinda "interrupt-map", (caddr_t)&imap, &imap_sz) 474693Sgovinda != DDI_PROP_SUCCESS) { 475693Sgovinda /* 476693Sgovinda * If we don't have an imap property, default to using the 477693Sgovinda * device tree. 4780Sstevel@tonic-gate */ 479693Sgovinda 480693Sgovinda ndi_hold_devi(pdip); 481693Sgovinda return (pdip); 482693Sgovinda } 483693Sgovinda 484693Sgovinda /* Get the interrupt mask property */ 485693Sgovinda if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, 486693Sgovinda "interrupt-map-mask", (caddr_t)&imap_mask, &imap_mask_sz) 487693Sgovinda != DDI_PROP_SUCCESS) { 488693Sgovinda /* 489693Sgovinda * If we don't find this property, we have to fail the request 490693Sgovinda * because the 1275 imap property wasn't defined correctly. 491693Sgovinda */ 492693Sgovinda ASSERT(intr_parent_dip == NULL); 493693Sgovinda goto exit2; 494693Sgovinda } 495693Sgovinda 496693Sgovinda /* Get the address cell size */ 497693Sgovinda addr_cells = ddi_getprop(DDI_DEV_T_ANY, pdip, 0, 498693Sgovinda "#address-cells", 2); 499693Sgovinda 500693Sgovinda /* Get the interrupts cell size */ 501693Sgovinda intr_cells = ddi_getprop(DDI_DEV_T_ANY, pdip, 0, 502693Sgovinda "#interrupt-cells", 1); 503693Sgovinda 504693Sgovinda /* 505693Sgovinda * step3 506693Sgovinda * Now lets build up the unit interrupt specifier e.g. reg,intr 507693Sgovinda * and apply the imap mask. match_req will hold this when we're 508693Sgovinda * through. 509693Sgovinda */ 510693Sgovinda if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg", 511693Sgovinda (caddr_t)®_p, ®_len) != DDI_SUCCESS) { 512693Sgovinda ASSERT(intr_parent_dip == NULL); 513693Sgovinda goto exit3; 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 516693Sgovinda match_req = kmem_alloc(CELLS_1275_TO_BYTES(addr_cells) + 517693Sgovinda CELLS_1275_TO_BYTES(intr_cells), KM_SLEEP); 518693Sgovinda 519693Sgovinda for (i = 0; i < addr_cells; i++) 520693Sgovinda match_req[i] = (reg_p[i] & imap_mask[i]); 521693Sgovinda 522693Sgovinda for (j = 0; j < intr_cells; i++, j++) 523693Sgovinda match_req[i] = (intr[j] & imap_mask[i]); 524693Sgovinda 525693Sgovinda /* Calculate the imap size in cells */ 526693Sgovinda imap_cells = BYTES_TO_1275_CELLS(imap_sz); 527693Sgovinda 528693Sgovinda #ifdef DEBUG 529693Sgovinda if (debug) 530693Sgovinda prom_printf("reg cell size 0x%x, intr cell size 0x%x, " 531693Sgovinda "match_request 0x%p, imap 0x%p\n", addr_cells, intr_cells, 532*7240Srh87107 (void *)match_req, (void *)imap); 533693Sgovinda #endif 534693Sgovinda 535693Sgovinda /* 536693Sgovinda * Scan the imap property looking for a match of the interrupt unit 537693Sgovinda * specifier. This loop is rather complex since the data within the 538693Sgovinda * imap property may vary in size. 539693Sgovinda */ 540693Sgovinda for (scan = imap, imap_scan_cells = i = 0; 541693Sgovinda imap_scan_cells < imap_cells; scan += i, imap_scan_cells += i) { 542693Sgovinda int new_intr_cells; 543693Sgovinda 544693Sgovinda /* Set the index to the nodeid field */ 545693Sgovinda i = addr_cells + intr_cells; 546693Sgovinda 547693Sgovinda /* 548693Sgovinda * step4a 549693Sgovinda * Translate the nodeid field to a dip 550693Sgovinda */ 551693Sgovinda ASSERT(intr_parent_dip == NULL); 552693Sgovinda intr_parent_dip = e_ddi_nodeid_to_dip((uint_t)scan[i++]); 553693Sgovinda 554693Sgovinda ASSERT(intr_parent_dip != 0); 555693Sgovinda #ifdef DEBUG 556693Sgovinda if (debug) 557*7240Srh87107 prom_printf("scan 0x%p\n", (void *)scan); 558693Sgovinda #endif 559693Sgovinda /* 560693Sgovinda * The tmp_dip describes the new domain, get it's interrupt 561693Sgovinda * cell size 562693Sgovinda */ 563693Sgovinda new_intr_cells = ddi_getprop(DDI_DEV_T_ANY, intr_parent_dip, 0, 564693Sgovinda "#interrupts-cells", 1); 5650Sstevel@tonic-gate 566693Sgovinda /* 567693Sgovinda * step4b 568693Sgovinda * See if we have a match on the interrupt unit specifier 569693Sgovinda */ 570693Sgovinda if (cells_1275_cmp(match_req, scan, addr_cells + intr_cells) 571693Sgovinda == 0) { 572693Sgovinda uint32_t *intr; 573693Sgovinda 574693Sgovinda match_found = 1; 575693Sgovinda 576693Sgovinda /* 577693Sgovinda * If we have an imap parent whose not in our device 578693Sgovinda * tree path, we need to hold and install that driver. 579693Sgovinda */ 580693Sgovinda if (i_ddi_attach_node_hierarchy(intr_parent_dip) 581693Sgovinda != DDI_SUCCESS) { 582693Sgovinda ndi_rele_devi(intr_parent_dip); 583693Sgovinda intr_parent_dip = (dev_info_t *)NULL; 584693Sgovinda goto exit4; 585693Sgovinda } 5860Sstevel@tonic-gate 587693Sgovinda /* 588693Sgovinda * We need to handcraft an ispec along with a bus 589693Sgovinda * interrupt value, so we can dup it into our 590693Sgovinda * standard ispec structure. 591693Sgovinda */ 592693Sgovinda /* Extract the translated interrupt information */ 593693Sgovinda intr = kmem_alloc( 594693Sgovinda CELLS_1275_TO_BYTES(new_intr_cells), KM_SLEEP); 595693Sgovinda 596693Sgovinda for (j = 0; j < new_intr_cells; j++, i++) 597693Sgovinda intr[j] = scan[i]; 598693Sgovinda 599693Sgovinda cells_1275_copy(intr, &hdlp->ih_vector, new_intr_cells); 6000Sstevel@tonic-gate 601693Sgovinda kmem_free(intr, CELLS_1275_TO_BYTES(new_intr_cells)); 602693Sgovinda 603693Sgovinda #ifdef DEBUG 604693Sgovinda if (debug) 605*7240Srh87107 prom_printf("dip 0x%p\n", 606*7240Srh87107 (void *)intr_parent_dip); 607693Sgovinda #endif 608693Sgovinda break; 609693Sgovinda } else { 610693Sgovinda #ifdef DEBUG 611693Sgovinda if (debug) 612*7240Srh87107 prom_printf("dip 0x%p\n", 613*7240Srh87107 (void *)intr_parent_dip); 614693Sgovinda #endif 615693Sgovinda ndi_rele_devi(intr_parent_dip); 616693Sgovinda intr_parent_dip = NULL; 617693Sgovinda i += new_intr_cells; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 621693Sgovinda /* 622693Sgovinda * If we haven't found our interrupt parent at this point, fallback 623693Sgovinda * to using the device tree. 624693Sgovinda */ 625693Sgovinda if (!match_found) { 626693Sgovinda ndi_hold_devi(pdip); 627693Sgovinda ASSERT(intr_parent_dip == NULL); 628693Sgovinda intr_parent_dip = pdip; 629693Sgovinda } 630693Sgovinda 631693Sgovinda ASSERT(intr_parent_dip != NULL); 632693Sgovinda 633693Sgovinda exit4: 634693Sgovinda kmem_free(reg_p, reg_len); 635693Sgovinda kmem_free(match_req, CELLS_1275_TO_BYTES(addr_cells) + 636693Sgovinda CELLS_1275_TO_BYTES(intr_cells)); 637693Sgovinda 638693Sgovinda exit3: 639693Sgovinda kmem_free(imap_mask, imap_mask_sz); 640693Sgovinda 641693Sgovinda exit2: 642693Sgovinda kmem_free(imap, imap_sz); 643693Sgovinda 644693Sgovinda return (intr_parent_dip); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * process_intr_ops: 6490Sstevel@tonic-gate * 6500Sstevel@tonic-gate * Process the interrupt op via the interrupt parent. 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate int 6530Sstevel@tonic-gate process_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t op, 6540Sstevel@tonic-gate ddi_intr_handle_impl_t *hdlp, void *result) 6550Sstevel@tonic-gate { 6560Sstevel@tonic-gate int ret = DDI_FAILURE; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate if (NEXUS_HAS_INTR_OP(pdip)) { 6590Sstevel@tonic-gate ret = (*(DEVI(pdip)->devi_ops->devo_bus_ops-> 6600Sstevel@tonic-gate bus_intr_op)) (pdip, rdip, op, hdlp, result); 6610Sstevel@tonic-gate } else { 6620Sstevel@tonic-gate cmn_err(CE_WARN, "Failed to process interrupt " 6630Sstevel@tonic-gate "for %s%d due to down-rev nexus driver %s%d", 6640Sstevel@tonic-gate ddi_get_name(rdip), ddi_get_instance(rdip), 6650Sstevel@tonic-gate ddi_get_name(pdip), ddi_get_instance(pdip)); 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate return (ret); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate 671693Sgovinda /*ARGSUSED*/ 672693Sgovinda uint_t 673693Sgovinda softlevel1(caddr_t arg) 674693Sgovinda { 675693Sgovinda softint(); 676693Sgovinda return (1); 677693Sgovinda } 678693Sgovinda 679693Sgovinda /* 680693Sgovinda * indirection table, to save us some large switch statements 681693Sgovinda * NOTE: This must agree with "INTLEVEL_foo" constants in 682693Sgovinda * <sys/avintr.h> 683693Sgovinda */ 684693Sgovinda struct autovec *const vectorlist[] = { 0 }; 685693Sgovinda 686693Sgovinda /* 687693Sgovinda * This value is exported here for the functions in avintr.c 688693Sgovinda */ 689693Sgovinda const uint_t maxautovec = (sizeof (vectorlist) / sizeof (vectorlist[0])); 690693Sgovinda 691693Sgovinda /* 692693Sgovinda * Check for machine specific interrupt levels which cannot be reassigned by 693693Sgovinda * settrap(), sun4u version. 694693Sgovinda * 695693Sgovinda * sun4u does not support V8 SPARC "fast trap" handlers. 696693Sgovinda */ 697693Sgovinda /*ARGSUSED*/ 698693Sgovinda int 699693Sgovinda exclude_settrap(int lvl) 700693Sgovinda { 701693Sgovinda return (1); 702693Sgovinda } 703693Sgovinda 704693Sgovinda /* 705693Sgovinda * Check for machine specific interrupt levels which cannot have interrupt 706693Sgovinda * handlers added. We allow levels 1 through 15; level 0 is nonsense. 707693Sgovinda */ 708693Sgovinda /*ARGSUSED*/ 709693Sgovinda int 710693Sgovinda exclude_level(int lvl) 711693Sgovinda { 712693Sgovinda return ((lvl < 1) || (lvl > 15)); 713693Sgovinda } 714693Sgovinda 715693Sgovinda /* 716693Sgovinda * Wrapper functions used by New DDI interrupt framework. 717693Sgovinda */ 718693Sgovinda 719693Sgovinda /* 720693Sgovinda * i_ddi_intr_ops: 721693Sgovinda */ 722693Sgovinda int 723693Sgovinda i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op, 724693Sgovinda ddi_intr_handle_impl_t *hdlp, void *result) 725693Sgovinda { 726693Sgovinda dev_info_t *pdip = ddi_get_parent(dip); 727693Sgovinda int ret = DDI_FAILURE; 728693Sgovinda 729693Sgovinda /* 730693Sgovinda * The following check is required to address 731693Sgovinda * one of the test case of ADDI test suite. 732693Sgovinda */ 733693Sgovinda if (pdip == NULL) 734693Sgovinda return (DDI_FAILURE); 735693Sgovinda 736693Sgovinda if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 737693Sgovinda return (process_intr_ops(pdip, rdip, op, hdlp, result)); 738693Sgovinda 739693Sgovinda if (hdlp->ih_vector == 0) 740693Sgovinda hdlp->ih_vector = i_ddi_get_inum(rdip, hdlp->ih_inum); 741693Sgovinda 742693Sgovinda if (hdlp->ih_pri == 0) 743693Sgovinda hdlp->ih_pri = i_ddi_get_intr_pri(rdip, hdlp->ih_inum); 744693Sgovinda 745693Sgovinda switch (op) { 746693Sgovinda case DDI_INTROP_ADDISR: 747693Sgovinda case DDI_INTROP_REMISR: 748693Sgovinda case DDI_INTROP_ENABLE: 749693Sgovinda case DDI_INTROP_DISABLE: 750693Sgovinda case DDI_INTROP_BLOCKENABLE: 751693Sgovinda case DDI_INTROP_BLOCKDISABLE: 752693Sgovinda /* 753693Sgovinda * Try and determine our parent and possibly an interrupt 754693Sgovinda * translation. intr parent dip returned held 755693Sgovinda */ 756693Sgovinda if ((pdip = get_intr_parent(pdip, dip, hdlp)) == NULL) 757693Sgovinda goto done; 758693Sgovinda } 759693Sgovinda 760693Sgovinda ret = process_intr_ops(pdip, rdip, op, hdlp, result); 761693Sgovinda 762693Sgovinda done: 763693Sgovinda switch (op) { 764693Sgovinda case DDI_INTROP_ADDISR: 765693Sgovinda case DDI_INTROP_REMISR: 766693Sgovinda case DDI_INTROP_ENABLE: 767693Sgovinda case DDI_INTROP_DISABLE: 768693Sgovinda case DDI_INTROP_BLOCKENABLE: 769693Sgovinda case DDI_INTROP_BLOCKDISABLE: 770693Sgovinda /* Release hold acquired in get_intr_parent() */ 771693Sgovinda if (pdip) 772693Sgovinda ndi_rele_devi(pdip); 773693Sgovinda } 774693Sgovinda 775693Sgovinda hdlp->ih_vector = 0; 776693Sgovinda 777693Sgovinda return (ret); 778693Sgovinda } 779693Sgovinda 7800Sstevel@tonic-gate /* 7810Sstevel@tonic-gate * i_ddi_add_ivintr: 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate /*ARGSUSED*/ 7840Sstevel@tonic-gate int 7850Sstevel@tonic-gate i_ddi_add_ivintr(ddi_intr_handle_impl_t *hdlp) 7860Sstevel@tonic-gate { 7870Sstevel@tonic-gate /* 7880Sstevel@tonic-gate * If the PIL was set and is valid use it, otherwise 7890Sstevel@tonic-gate * default it to 1 7900Sstevel@tonic-gate */ 7910Sstevel@tonic-gate if ((hdlp->ih_pri < 1) || (hdlp->ih_pri > PIL_MAX)) 7920Sstevel@tonic-gate hdlp->ih_pri = 1; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate VERIFY(add_ivintr(hdlp->ih_vector, hdlp->ih_pri, 7952973Sgovinda (intrfunc)hdlp->ih_cb_func, hdlp->ih_cb_arg1, 7962973Sgovinda hdlp->ih_cb_arg2, NULL) == 0); 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate return (DDI_SUCCESS); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate /* 8020Sstevel@tonic-gate * i_ddi_rem_ivintr: 8030Sstevel@tonic-gate */ 8040Sstevel@tonic-gate /*ARGSUSED*/ 8050Sstevel@tonic-gate void 8060Sstevel@tonic-gate i_ddi_rem_ivintr(ddi_intr_handle_impl_t *hdlp) 8070Sstevel@tonic-gate { 8082973Sgovinda VERIFY(rem_ivintr(hdlp->ih_vector, hdlp->ih_pri) == 0); 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate /* 812693Sgovinda * i_ddi_get_inum - Get the interrupt number property from the 813693Sgovinda * specified device. Note that this function is called only for 814693Sgovinda * the FIXED interrupt type. 815693Sgovinda */ 816693Sgovinda uint32_t 817693Sgovinda i_ddi_get_inum(dev_info_t *dip, uint_t inumber) 818693Sgovinda { 819693Sgovinda int32_t intrlen, intr_cells, max_intrs; 820693Sgovinda prop_1275_cell_t *ip, intr_sz; 821693Sgovinda uint32_t intr = 0; 822693Sgovinda 823693Sgovinda if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS | 824693Sgovinda DDI_PROP_CANSLEEP, 825693Sgovinda "interrupts", (caddr_t)&ip, &intrlen) == DDI_SUCCESS) { 826693Sgovinda 827693Sgovinda intr_cells = ddi_getprop(DDI_DEV_T_ANY, dip, 0, 828693Sgovinda "#interrupt-cells", 1); 829693Sgovinda 830693Sgovinda /* adjust for number of bytes */ 831693Sgovinda intr_sz = CELLS_1275_TO_BYTES(intr_cells); 832693Sgovinda 833693Sgovinda /* Calculate the number of interrupts */ 834693Sgovinda max_intrs = intrlen / intr_sz; 835693Sgovinda 836693Sgovinda if (inumber < max_intrs) { 837693Sgovinda prop_1275_cell_t *intrp = ip; 838693Sgovinda 839693Sgovinda /* Index into interrupt property */ 840693Sgovinda intrp += (inumber * intr_cells); 841693Sgovinda 842693Sgovinda cells_1275_copy(intrp, &intr, intr_cells); 843693Sgovinda } 844693Sgovinda 845693Sgovinda kmem_free(ip, intrlen); 846693Sgovinda } 847693Sgovinda 848693Sgovinda return (intr); 849693Sgovinda } 850693Sgovinda 851693Sgovinda /* 852693Sgovinda * i_ddi_get_intr_pri - Get the interrupt-priorities property from 853693Sgovinda * the specified device. Note that this function is called only for 854693Sgovinda * the FIXED interrupt type. 855693Sgovinda */ 856693Sgovinda uint32_t 857693Sgovinda i_ddi_get_intr_pri(dev_info_t *dip, uint_t inumber) 858693Sgovinda { 859693Sgovinda uint32_t *intr_prio_p; 860693Sgovinda uint32_t pri = 0; 861693Sgovinda int32_t i; 862693Sgovinda 863693Sgovinda /* 864693Sgovinda * Use the "interrupt-priorities" property to determine the 865693Sgovinda * the pil/ipl for the interrupt handler. 866693Sgovinda */ 867693Sgovinda if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 868693Sgovinda "interrupt-priorities", (caddr_t)&intr_prio_p, 869693Sgovinda &i) == DDI_SUCCESS) { 870693Sgovinda if (inumber < (i / sizeof (int32_t))) 871693Sgovinda pri = intr_prio_p[inumber]; 872693Sgovinda kmem_free(intr_prio_p, i); 873693Sgovinda } 874693Sgovinda 875693Sgovinda return (pri); 876693Sgovinda } 877693Sgovinda 878693Sgovinda int 8792580Sanish i_ddi_get_intx_nintrs(dev_info_t *dip) 880693Sgovinda { 881693Sgovinda int32_t intrlen; 882693Sgovinda prop_1275_cell_t intr_sz; 883693Sgovinda prop_1275_cell_t *ip; 884693Sgovinda int32_t ret = 0; 885693Sgovinda 886693Sgovinda if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS | 887693Sgovinda DDI_PROP_CANSLEEP, 888693Sgovinda "interrupts", (caddr_t)&ip, &intrlen) == DDI_SUCCESS) { 889693Sgovinda 890693Sgovinda intr_sz = ddi_getprop(DDI_DEV_T_ANY, dip, 0, 891693Sgovinda "#interrupt-cells", 1); 892693Sgovinda /* adjust for number of bytes */ 893693Sgovinda intr_sz = CELLS_1275_TO_BYTES(intr_sz); 894693Sgovinda 895693Sgovinda ret = intrlen / intr_sz; 896693Sgovinda 897693Sgovinda kmem_free(ip, intrlen); 898693Sgovinda } 899693Sgovinda 900693Sgovinda return (ret); 901693Sgovinda } 902693Sgovinda 903693Sgovinda /* 9042973Sgovinda * i_ddi_add_softint - allocate and add a software interrupt. 9052973Sgovinda * 9062973Sgovinda * NOTE: All software interrupts that are registered through DDI 9072973Sgovinda * should be triggered only on a single target or CPU. 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate int 9100Sstevel@tonic-gate i_ddi_add_softint(ddi_softint_hdl_impl_t *hdlp) 9110Sstevel@tonic-gate { 9122973Sgovinda if ((hdlp->ih_private = (void *)add_softintr(hdlp->ih_pri, 9132973Sgovinda hdlp->ih_cb_func, hdlp->ih_cb_arg1, SOFTINT_ST)) == NULL) 9140Sstevel@tonic-gate return (DDI_FAILURE); 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate return (DDI_SUCCESS); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9192973Sgovinda /* 9202973Sgovinda * i_ddi_remove_softint - remove and free a software interrupt. 9212973Sgovinda */ 9220Sstevel@tonic-gate void 9230Sstevel@tonic-gate i_ddi_remove_softint(ddi_softint_hdl_impl_t *hdlp) 9240Sstevel@tonic-gate { 9250Sstevel@tonic-gate ASSERT(hdlp->ih_private != NULL); 926693Sgovinda 9272973Sgovinda if (rem_softintr((uint64_t)hdlp->ih_private) == 0) 9282973Sgovinda hdlp->ih_private = NULL; 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9312973Sgovinda /* 9322973Sgovinda * i_ddi_trigger_softint - trigger a software interrupt. 9332973Sgovinda */ 9340Sstevel@tonic-gate int 935278Sgovinda i_ddi_trigger_softint(ddi_softint_hdl_impl_t *hdlp, void *arg2) 9360Sstevel@tonic-gate { 9372973Sgovinda int ret; 9382973Sgovinda 9390Sstevel@tonic-gate ASSERT(hdlp->ih_private != NULL); 9400Sstevel@tonic-gate 9412973Sgovinda /* Update the second argument for the software interrupt */ 9422973Sgovinda if ((ret = update_softint_arg2((uint64_t)hdlp->ih_private, arg2)) == 0) 9432973Sgovinda setsoftint((uint64_t)hdlp->ih_private); 9442973Sgovinda 9452973Sgovinda return (ret ? DDI_EPENDING : DDI_SUCCESS); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9482973Sgovinda /* 9492973Sgovinda * i_ddi_set_softint_pri - change software interrupt priority. 9502973Sgovinda */ 9510Sstevel@tonic-gate /* ARGSUSED */ 9520Sstevel@tonic-gate int 9530Sstevel@tonic-gate i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *hdlp, uint_t old_pri) 9540Sstevel@tonic-gate { 9552973Sgovinda int ret; 9562973Sgovinda 9570Sstevel@tonic-gate ASSERT(hdlp->ih_private != NULL); 9580Sstevel@tonic-gate 9592973Sgovinda /* Update the interrupt priority for the software interrupt */ 9602973Sgovinda ret = update_softint_pri((uint64_t)hdlp->ih_private, hdlp->ih_pri); 9612973Sgovinda 9622973Sgovinda return (ret ? DDI_FAILURE : DDI_SUCCESS); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 965916Sschwartz /*ARGSUSED*/ 966916Sschwartz void 967916Sschwartz i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *hdlp) 968916Sschwartz { 969916Sschwartz } 970916Sschwartz 971916Sschwartz /*ARGSUSED*/ 972916Sschwartz void 973916Sschwartz i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *hdlp) 974916Sschwartz { 975916Sschwartz } 976916Sschwartz 9770Sstevel@tonic-gate /* 9780Sstevel@tonic-gate * SECTION: DDI Memory/DMA 9790Sstevel@tonic-gate */ 9800Sstevel@tonic-gate 9811900Seota /* set HAT endianess attributes from ddi_device_acc_attr */ 9821900Seota void 9831900Seota i_ddi_devacc_to_hatacc(ddi_device_acc_attr_t *devaccp, uint_t *hataccp) 9841900Seota { 9851900Seota if (devaccp != NULL) { 9861900Seota if (devaccp->devacc_attr_endian_flags == DDI_STRUCTURE_LE_ACC) { 9871900Seota *hataccp &= ~HAT_ENDIAN_MASK; 9881900Seota *hataccp |= HAT_STRUCTURE_LE; 9891900Seota } 9901900Seota } 9911900Seota } 9921900Seota 9931900Seota /* 9941900Seota * Check if the specified cache attribute is supported on the platform. 9951900Seota * This function must be called before i_ddi_cacheattr_to_hatacc(). 9961900Seota */ 9971900Seota boolean_t 9981900Seota i_ddi_check_cache_attr(uint_t flags) 9991900Seota { 10001900Seota /* 10011900Seota * The cache attributes are mutually exclusive. Any combination of 10021900Seota * the attributes leads to a failure. 10031900Seota */ 10041900Seota uint_t cache_attr = IOMEM_CACHE_ATTR(flags); 10051900Seota if ((cache_attr != 0) && ((cache_attr & (cache_attr - 1)) != 0)) 10061900Seota return (B_FALSE); 10071900Seota 10081900Seota /* 10091900Seota * On the sparc architecture, only IOMEM_DATA_CACHED is meaningful, 10101900Seota * but others lead to a failure. 10111900Seota */ 10121900Seota if (cache_attr & IOMEM_DATA_CACHED) 10131900Seota return (B_TRUE); 10141900Seota else 10151900Seota return (B_FALSE); 10161900Seota } 10171900Seota 10181900Seota /* set HAT cache attributes from the cache attributes */ 10191900Seota void 10201900Seota i_ddi_cacheattr_to_hatacc(uint_t flags, uint_t *hataccp) 10211900Seota { 10221900Seota uint_t cache_attr = IOMEM_CACHE_ATTR(flags); 10231900Seota static char *fname = "i_ddi_cacheattr_to_hatacc"; 10241900Seota #if defined(lint) 10251900Seota *hataccp = *hataccp; 10261900Seota #endif 10271900Seota /* 10281900Seota * set HAT attrs according to the cache attrs. 10291900Seota */ 10301900Seota switch (cache_attr) { 10311900Seota /* 10321900Seota * The cache coherency is always maintained on SPARC, and 10331900Seota * nothing is required. 10341900Seota */ 10351900Seota case IOMEM_DATA_CACHED: 10361900Seota break; 10371900Seota /* 10381900Seota * Both IOMEM_DATA_UC_WRITE_COMBINED and IOMEM_DATA_UNCACHED are 10391900Seota * not supported on SPARC -- this case must not occur because the 10401900Seota * cache attribute is scrutinized before this function is called. 10411900Seota */ 10421900Seota case IOMEM_DATA_UNCACHED: 10431900Seota case IOMEM_DATA_UC_WR_COMBINE: 10441900Seota default: 10451900Seota cmn_err(CE_WARN, "%s: cache_attr=0x%x is ignored.", 10461900Seota fname, cache_attr); 10471900Seota } 10481900Seota } 10491900Seota 10500Sstevel@tonic-gate static vmem_t *little_endian_arena; 10510Sstevel@tonic-gate static vmem_t *big_endian_arena; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate static void * 10540Sstevel@tonic-gate segkmem_alloc_le(vmem_t *vmp, size_t size, int flag) 10550Sstevel@tonic-gate { 10560Sstevel@tonic-gate return (segkmem_xalloc(vmp, NULL, size, flag, HAT_STRUCTURE_LE, 10570Sstevel@tonic-gate segkmem_page_create, NULL)); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate static void * 10610Sstevel@tonic-gate segkmem_alloc_be(vmem_t *vmp, size_t size, int flag) 10620Sstevel@tonic-gate { 10630Sstevel@tonic-gate return (segkmem_xalloc(vmp, NULL, size, flag, HAT_STRUCTURE_BE, 10640Sstevel@tonic-gate segkmem_page_create, NULL)); 10650Sstevel@tonic-gate } 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate void 10680Sstevel@tonic-gate ka_init(void) 10690Sstevel@tonic-gate { 10700Sstevel@tonic-gate little_endian_arena = vmem_create("little_endian", NULL, 0, 1, 10710Sstevel@tonic-gate segkmem_alloc_le, segkmem_free, heap_arena, 0, VM_SLEEP); 10720Sstevel@tonic-gate big_endian_arena = vmem_create("big_endian", NULL, 0, 1, 10730Sstevel@tonic-gate segkmem_alloc_be, segkmem_free, heap_arena, 0, VM_SLEEP); 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * Allocate from the system, aligned on a specific boundary. 10780Sstevel@tonic-gate * The alignment, if non-zero, must be a power of 2. 10790Sstevel@tonic-gate */ 10800Sstevel@tonic-gate static void * 10810Sstevel@tonic-gate kalloca(size_t size, size_t align, int cansleep, uint_t endian_flags) 10820Sstevel@tonic-gate { 10830Sstevel@tonic-gate size_t *addr, *raddr, rsize; 10840Sstevel@tonic-gate size_t hdrsize = 4 * sizeof (size_t); /* must be power of 2 */ 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate align = MAX(align, hdrsize); 10870Sstevel@tonic-gate ASSERT((align & (align - 1)) == 0); 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate /* 10900Sstevel@tonic-gate * We need to allocate 10910Sstevel@tonic-gate * rsize = size + hdrsize + align - MIN(hdrsize, buffer_alignment) 10920Sstevel@tonic-gate * bytes to be sure we have enough freedom to satisfy the request. 10930Sstevel@tonic-gate * Since the buffer alignment depends on the request size, this is 10940Sstevel@tonic-gate * not straightforward to use directly. 10950Sstevel@tonic-gate * 10960Sstevel@tonic-gate * kmem guarantees that any allocation of a 64-byte multiple will be 10970Sstevel@tonic-gate * 64-byte aligned. Since rounding up the request could add more 10980Sstevel@tonic-gate * than we save, we compute the size with and without alignment, and 10990Sstevel@tonic-gate * use the smaller of the two. 11000Sstevel@tonic-gate */ 11010Sstevel@tonic-gate rsize = size + hdrsize + align; 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate if (endian_flags == DDI_STRUCTURE_LE_ACC) { 11040Sstevel@tonic-gate raddr = vmem_alloc(little_endian_arena, rsize, 11050Sstevel@tonic-gate cansleep ? VM_SLEEP : VM_NOSLEEP); 11060Sstevel@tonic-gate } else { 11070Sstevel@tonic-gate raddr = vmem_alloc(big_endian_arena, rsize, 11080Sstevel@tonic-gate cansleep ? VM_SLEEP : VM_NOSLEEP); 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate if (raddr == NULL) 11120Sstevel@tonic-gate return (NULL); 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align); 11150Sstevel@tonic-gate ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize); 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate addr[-3] = (size_t)endian_flags; 11180Sstevel@tonic-gate addr[-2] = (size_t)raddr; 11190Sstevel@tonic-gate addr[-1] = rsize; 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate return (addr); 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate static void 11250Sstevel@tonic-gate kfreea(void *addr) 11260Sstevel@tonic-gate { 11270Sstevel@tonic-gate size_t *saddr = addr; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate if (saddr[-3] == DDI_STRUCTURE_LE_ACC) 11300Sstevel@tonic-gate vmem_free(little_endian_arena, (void *)saddr[-2], saddr[-1]); 11310Sstevel@tonic-gate else 11320Sstevel@tonic-gate vmem_free(big_endian_arena, (void *)saddr[-2], saddr[-1]); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate int 11360Sstevel@tonic-gate i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr, 11371900Seota size_t length, int cansleep, int flags, 11380Sstevel@tonic-gate ddi_device_acc_attr_t *accattrp, 11390Sstevel@tonic-gate caddr_t *kaddrp, size_t *real_length, ddi_acc_hdl_t *handlep) 11400Sstevel@tonic-gate { 11410Sstevel@tonic-gate caddr_t a; 11421900Seota int iomin, align, streaming; 11430Sstevel@tonic-gate uint_t endian_flags = DDI_NEVERSWAP_ACC; 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate #if defined(lint) 11460Sstevel@tonic-gate *handlep = *handlep; 11470Sstevel@tonic-gate #endif 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate /* 11500Sstevel@tonic-gate * Check legality of arguments 11510Sstevel@tonic-gate */ 11520Sstevel@tonic-gate if (length == 0 || kaddrp == NULL || attr == NULL) { 11530Sstevel@tonic-gate return (DDI_FAILURE); 11540Sstevel@tonic-gate } 11551900Seota 11560Sstevel@tonic-gate if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 || 11570Sstevel@tonic-gate (attr->dma_attr_align & (attr->dma_attr_align - 1)) || 11580Sstevel@tonic-gate (attr->dma_attr_minxfer & (attr->dma_attr_minxfer - 1))) { 11590Sstevel@tonic-gate return (DDI_FAILURE); 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate /* 11631900Seota * check if a streaming sequential xfer is requested. 11641900Seota */ 11651900Seota streaming = (flags & DDI_DMA_STREAMING) ? 1 : 0; 11661900Seota 11671900Seota /* 11680Sstevel@tonic-gate * Drivers for 64-bit capable SBus devices will encode 11690Sstevel@tonic-gate * the burtsizes for 64-bit xfers in the upper 16-bits. 11700Sstevel@tonic-gate * For DMA alignment, we use the most restrictive 11710Sstevel@tonic-gate * alignment of 32-bit and 64-bit xfers. 11720Sstevel@tonic-gate */ 11730Sstevel@tonic-gate iomin = (attr->dma_attr_burstsizes & 0xffff) | 11740Sstevel@tonic-gate ((attr->dma_attr_burstsizes >> 16) & 0xffff); 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * If a driver set burtsizes to 0, we give him byte alignment. 11770Sstevel@tonic-gate * Otherwise align at the burtsizes boundary. 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate if (iomin == 0) 11800Sstevel@tonic-gate iomin = 1; 11810Sstevel@tonic-gate else 11820Sstevel@tonic-gate iomin = 1 << (ddi_fls(iomin) - 1); 11830Sstevel@tonic-gate iomin = maxbit(iomin, attr->dma_attr_minxfer); 11840Sstevel@tonic-gate iomin = maxbit(iomin, attr->dma_attr_align); 11850Sstevel@tonic-gate iomin = ddi_iomin(dip, iomin, streaming); 11860Sstevel@tonic-gate if (iomin == 0) 11870Sstevel@tonic-gate return (DDI_FAILURE); 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate ASSERT((iomin & (iomin - 1)) == 0); 11900Sstevel@tonic-gate ASSERT(iomin >= attr->dma_attr_minxfer); 11910Sstevel@tonic-gate ASSERT(iomin >= attr->dma_attr_align); 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate length = P2ROUNDUP(length, iomin); 11940Sstevel@tonic-gate align = iomin; 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate if (accattrp != NULL) 11970Sstevel@tonic-gate endian_flags = accattrp->devacc_attr_endian_flags; 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate a = kalloca(length, align, cansleep, endian_flags); 12000Sstevel@tonic-gate if ((*kaddrp = a) == 0) { 12010Sstevel@tonic-gate return (DDI_FAILURE); 12020Sstevel@tonic-gate } else { 12030Sstevel@tonic-gate if (real_length) { 12040Sstevel@tonic-gate *real_length = length; 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate if (handlep) { 12070Sstevel@tonic-gate /* 12080Sstevel@tonic-gate * assign handle information 12090Sstevel@tonic-gate */ 12100Sstevel@tonic-gate impl_acc_hdl_init(handlep); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate return (DDI_SUCCESS); 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate } 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate /* 12170Sstevel@tonic-gate * covert old DMA limits structure to DMA attribute structure 12180Sstevel@tonic-gate * and continue 12190Sstevel@tonic-gate */ 12200Sstevel@tonic-gate int 12210Sstevel@tonic-gate i_ddi_mem_alloc_lim(dev_info_t *dip, ddi_dma_lim_t *limits, 12220Sstevel@tonic-gate size_t length, int cansleep, int streaming, 12230Sstevel@tonic-gate ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp, 12240Sstevel@tonic-gate uint_t *real_length, ddi_acc_hdl_t *ap) 12250Sstevel@tonic-gate { 12260Sstevel@tonic-gate ddi_dma_attr_t dma_attr, *attrp; 12270Sstevel@tonic-gate size_t rlen; 12280Sstevel@tonic-gate int ret; 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate ASSERT(limits); 12310Sstevel@tonic-gate attrp = &dma_attr; 12320Sstevel@tonic-gate attrp->dma_attr_version = DMA_ATTR_V0; 12330Sstevel@tonic-gate attrp->dma_attr_addr_lo = (uint64_t)limits->dlim_addr_lo; 12340Sstevel@tonic-gate attrp->dma_attr_addr_hi = (uint64_t)limits->dlim_addr_hi; 12350Sstevel@tonic-gate attrp->dma_attr_count_max = (uint64_t)-1; 12360Sstevel@tonic-gate attrp->dma_attr_align = 1; 12370Sstevel@tonic-gate attrp->dma_attr_burstsizes = (uint_t)limits->dlim_burstsizes; 12380Sstevel@tonic-gate attrp->dma_attr_minxfer = (uint32_t)limits->dlim_minxfer; 12390Sstevel@tonic-gate attrp->dma_attr_maxxfer = (uint64_t)-1; 12400Sstevel@tonic-gate attrp->dma_attr_seg = (uint64_t)limits->dlim_cntr_max; 12410Sstevel@tonic-gate attrp->dma_attr_sgllen = 1; 12420Sstevel@tonic-gate attrp->dma_attr_granular = 1; 12430Sstevel@tonic-gate attrp->dma_attr_flags = 0; 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate ret = i_ddi_mem_alloc(dip, attrp, length, cansleep, streaming, 12460Sstevel@tonic-gate accattrp, kaddrp, &rlen, ap); 12470Sstevel@tonic-gate if (ret == DDI_SUCCESS) { 12480Sstevel@tonic-gate if (real_length) 12490Sstevel@tonic-gate *real_length = (uint_t)rlen; 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate return (ret); 12520Sstevel@tonic-gate } 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate /* ARGSUSED */ 12550Sstevel@tonic-gate void 12561900Seota i_ddi_mem_free(caddr_t kaddr, ddi_acc_hdl_t *ap) 12570Sstevel@tonic-gate { 12580Sstevel@tonic-gate kfreea(kaddr); 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate /* 12620Sstevel@tonic-gate * SECTION: DDI Data Access 12630Sstevel@tonic-gate */ 12640Sstevel@tonic-gate 12650Sstevel@tonic-gate static uintptr_t impl_acc_hdl_id = 0; 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate /* 12680Sstevel@tonic-gate * access handle allocator 12690Sstevel@tonic-gate */ 12700Sstevel@tonic-gate ddi_acc_hdl_t * 12710Sstevel@tonic-gate impl_acc_hdl_get(ddi_acc_handle_t hdl) 12720Sstevel@tonic-gate { 12730Sstevel@tonic-gate /* 12740Sstevel@tonic-gate * Extract the access handle address from the DDI implemented 12750Sstevel@tonic-gate * access handle 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate return (&((ddi_acc_impl_t *)hdl)->ahi_common); 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate ddi_acc_handle_t 12810Sstevel@tonic-gate impl_acc_hdl_alloc(int (*waitfp)(caddr_t), caddr_t arg) 12820Sstevel@tonic-gate { 12830Sstevel@tonic-gate ddi_acc_impl_t *hp; 12840Sstevel@tonic-gate on_trap_data_t *otp; 12850Sstevel@tonic-gate int sleepflag; 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate sleepflag = ((waitfp == (int (*)())KM_SLEEP) ? KM_SLEEP : KM_NOSLEEP); 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate /* 12900Sstevel@tonic-gate * Allocate and initialize the data access handle and error status. 12910Sstevel@tonic-gate */ 12920Sstevel@tonic-gate if ((hp = kmem_zalloc(sizeof (ddi_acc_impl_t), sleepflag)) == NULL) 12930Sstevel@tonic-gate goto fail; 12940Sstevel@tonic-gate if ((hp->ahi_err = (ndi_err_t *)kmem_zalloc( 12950Sstevel@tonic-gate sizeof (ndi_err_t), sleepflag)) == NULL) { 12960Sstevel@tonic-gate kmem_free(hp, sizeof (ddi_acc_impl_t)); 12970Sstevel@tonic-gate goto fail; 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate if ((otp = (on_trap_data_t *)kmem_zalloc( 13000Sstevel@tonic-gate sizeof (on_trap_data_t), sleepflag)) == NULL) { 13010Sstevel@tonic-gate kmem_free(hp->ahi_err, sizeof (ndi_err_t)); 13020Sstevel@tonic-gate kmem_free(hp, sizeof (ddi_acc_impl_t)); 13030Sstevel@tonic-gate goto fail; 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate hp->ahi_err->err_ontrap = otp; 13060Sstevel@tonic-gate hp->ahi_common.ah_platform_private = (void *)hp; 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate return ((ddi_acc_handle_t)hp); 13090Sstevel@tonic-gate fail: 13100Sstevel@tonic-gate if ((waitfp != (int (*)())KM_SLEEP) && 13110Sstevel@tonic-gate (waitfp != (int (*)())KM_NOSLEEP)) 13120Sstevel@tonic-gate ddi_set_callback(waitfp, arg, &impl_acc_hdl_id); 13130Sstevel@tonic-gate return (NULL); 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate void 13170Sstevel@tonic-gate impl_acc_hdl_free(ddi_acc_handle_t handle) 13180Sstevel@tonic-gate { 13190Sstevel@tonic-gate ddi_acc_impl_t *hp; 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate /* 13220Sstevel@tonic-gate * The supplied (ddi_acc_handle_t) is actually a (ddi_acc_impl_t *), 13230Sstevel@tonic-gate * because that's what we allocated in impl_acc_hdl_alloc() above. 13240Sstevel@tonic-gate */ 13250Sstevel@tonic-gate hp = (ddi_acc_impl_t *)handle; 13260Sstevel@tonic-gate if (hp) { 13270Sstevel@tonic-gate kmem_free(hp->ahi_err->err_ontrap, sizeof (on_trap_data_t)); 13280Sstevel@tonic-gate kmem_free(hp->ahi_err, sizeof (ndi_err_t)); 13290Sstevel@tonic-gate kmem_free(hp, sizeof (ddi_acc_impl_t)); 13300Sstevel@tonic-gate if (impl_acc_hdl_id) 13310Sstevel@tonic-gate ddi_run_callback(&impl_acc_hdl_id); 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate } 13340Sstevel@tonic-gate 13351865Sdilpreet #define PCI_GET_MP_PFN(mp, page_no) ((mp)->dmai_ndvmapages == 1 ? \ 13361865Sdilpreet (pfn_t)(mp)->dmai_iopte:(((pfn_t *)(mp)->dmai_iopte)[page_no])) 13371865Sdilpreet 13381865Sdilpreet /* 13391865Sdilpreet * Function called after a dma fault occurred to find out whether the 13401865Sdilpreet * fault address is associated with a driver that is able to handle faults 13411865Sdilpreet * and recover from faults. 13421865Sdilpreet */ 13431865Sdilpreet /* ARGSUSED */ 13441865Sdilpreet int 13451865Sdilpreet impl_dma_check(dev_info_t *dip, const void *handle, const void *addr, 13461865Sdilpreet const void *not_used) 13471865Sdilpreet { 13481865Sdilpreet ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle; 13491865Sdilpreet pfn_t fault_pfn = mmu_btop(*(uint64_t *)addr); 13501865Sdilpreet pfn_t comp_pfn; 13511865Sdilpreet 13521865Sdilpreet /* 13531865Sdilpreet * The driver has to set DDI_DMA_FLAGERR to recover from dma faults. 13541865Sdilpreet */ 13551865Sdilpreet int page; 13561865Sdilpreet 13571865Sdilpreet ASSERT(mp); 13581865Sdilpreet for (page = 0; page < mp->dmai_ndvmapages; page++) { 13591865Sdilpreet comp_pfn = PCI_GET_MP_PFN(mp, page); 13601865Sdilpreet if (fault_pfn == comp_pfn) 13611865Sdilpreet return (DDI_FM_NONFATAL); 13621865Sdilpreet } 13631865Sdilpreet return (DDI_FM_UNKNOWN); 13641865Sdilpreet } 13651865Sdilpreet 13661865Sdilpreet /* 13671865Sdilpreet * Function used to check if a given access handle owns the failing address. 13681865Sdilpreet * Called by ndi_fmc_error, when we detect a PIO error. 13691865Sdilpreet */ 13701865Sdilpreet /* ARGSUSED */ 13711865Sdilpreet static int 13721865Sdilpreet impl_acc_check(dev_info_t *dip, const void *handle, const void *addr, 13731865Sdilpreet const void *not_used) 13741865Sdilpreet { 13751865Sdilpreet pfn_t pfn, fault_pfn; 13761865Sdilpreet ddi_acc_hdl_t *hp; 13771865Sdilpreet 13781865Sdilpreet hp = impl_acc_hdl_get((ddi_acc_handle_t)handle); 13791865Sdilpreet 13801865Sdilpreet ASSERT(hp); 13811865Sdilpreet 13821865Sdilpreet if (addr != NULL) { 13831865Sdilpreet pfn = hp->ah_pfn; 13841865Sdilpreet fault_pfn = mmu_btop(*(uint64_t *)addr); 13851865Sdilpreet if (fault_pfn >= pfn && fault_pfn < (pfn + hp->ah_pnum)) 13861865Sdilpreet return (DDI_FM_NONFATAL); 13871865Sdilpreet } 13881865Sdilpreet return (DDI_FM_UNKNOWN); 13891865Sdilpreet } 13901865Sdilpreet 13910Sstevel@tonic-gate void 13920Sstevel@tonic-gate impl_acc_err_init(ddi_acc_hdl_t *handlep) 13930Sstevel@tonic-gate { 13940Sstevel@tonic-gate int fmcap; 13950Sstevel@tonic-gate ndi_err_t *errp; 13960Sstevel@tonic-gate on_trap_data_t *otp; 13970Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handlep; 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate fmcap = ddi_fm_capable(handlep->ah_dip); 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate if (handlep->ah_acc.devacc_attr_version < DDI_DEVICE_ATTR_V1 || 14020Sstevel@tonic-gate !DDI_FM_ACC_ERR_CAP(fmcap)) { 14030Sstevel@tonic-gate handlep->ah_acc.devacc_attr_access = DDI_DEFAULT_ACC; 14040Sstevel@tonic-gate } else if (DDI_FM_ACC_ERR_CAP(fmcap)) { 14050Sstevel@tonic-gate if (handlep->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) { 14060Sstevel@tonic-gate i_ddi_drv_ereport_post(handlep->ah_dip, DVR_EFMCAP, 14070Sstevel@tonic-gate NULL, DDI_NOSLEEP); 14080Sstevel@tonic-gate } else { 14090Sstevel@tonic-gate errp = hp->ahi_err; 14100Sstevel@tonic-gate otp = (on_trap_data_t *)errp->err_ontrap; 14110Sstevel@tonic-gate otp->ot_handle = (void *)(hp); 14120Sstevel@tonic-gate otp->ot_prot = OT_DATA_ACCESS; 14130Sstevel@tonic-gate if (handlep->ah_acc.devacc_attr_access == 14140Sstevel@tonic-gate DDI_CAUTIOUS_ACC) 14150Sstevel@tonic-gate otp->ot_trampoline = 14160Sstevel@tonic-gate (uintptr_t)&i_ddi_caut_trampoline; 14170Sstevel@tonic-gate else 14180Sstevel@tonic-gate otp->ot_trampoline = 14190Sstevel@tonic-gate (uintptr_t)&i_ddi_prot_trampoline; 14200Sstevel@tonic-gate errp->err_status = DDI_FM_OK; 14210Sstevel@tonic-gate errp->err_expected = DDI_FM_ERR_UNEXPECTED; 14221865Sdilpreet errp->err_cf = impl_acc_check; 14230Sstevel@tonic-gate } 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate void 14280Sstevel@tonic-gate impl_acc_hdl_init(ddi_acc_hdl_t *handlep) 14290Sstevel@tonic-gate { 14300Sstevel@tonic-gate ddi_acc_impl_t *hp; 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate ASSERT(handlep); 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate hp = (ddi_acc_impl_t *)handlep; 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate /* 14370Sstevel@tonic-gate * check for SW byte-swapping 14380Sstevel@tonic-gate */ 14390Sstevel@tonic-gate hp->ahi_get8 = i_ddi_get8; 14400Sstevel@tonic-gate hp->ahi_put8 = i_ddi_put8; 14410Sstevel@tonic-gate hp->ahi_rep_get8 = i_ddi_rep_get8; 14420Sstevel@tonic-gate hp->ahi_rep_put8 = i_ddi_rep_put8; 14430Sstevel@tonic-gate if (handlep->ah_acc.devacc_attr_endian_flags & DDI_STRUCTURE_LE_ACC) { 14440Sstevel@tonic-gate hp->ahi_get16 = i_ddi_swap_get16; 14450Sstevel@tonic-gate hp->ahi_get32 = i_ddi_swap_get32; 14460Sstevel@tonic-gate hp->ahi_get64 = i_ddi_swap_get64; 14470Sstevel@tonic-gate hp->ahi_put16 = i_ddi_swap_put16; 14480Sstevel@tonic-gate hp->ahi_put32 = i_ddi_swap_put32; 14490Sstevel@tonic-gate hp->ahi_put64 = i_ddi_swap_put64; 14500Sstevel@tonic-gate hp->ahi_rep_get16 = i_ddi_swap_rep_get16; 14510Sstevel@tonic-gate hp->ahi_rep_get32 = i_ddi_swap_rep_get32; 14520Sstevel@tonic-gate hp->ahi_rep_get64 = i_ddi_swap_rep_get64; 14530Sstevel@tonic-gate hp->ahi_rep_put16 = i_ddi_swap_rep_put16; 14540Sstevel@tonic-gate hp->ahi_rep_put32 = i_ddi_swap_rep_put32; 14550Sstevel@tonic-gate hp->ahi_rep_put64 = i_ddi_swap_rep_put64; 14560Sstevel@tonic-gate } else { 14570Sstevel@tonic-gate hp->ahi_get16 = i_ddi_get16; 14580Sstevel@tonic-gate hp->ahi_get32 = i_ddi_get32; 14590Sstevel@tonic-gate hp->ahi_get64 = i_ddi_get64; 14600Sstevel@tonic-gate hp->ahi_put16 = i_ddi_put16; 14610Sstevel@tonic-gate hp->ahi_put32 = i_ddi_put32; 14620Sstevel@tonic-gate hp->ahi_put64 = i_ddi_put64; 14630Sstevel@tonic-gate hp->ahi_rep_get16 = i_ddi_rep_get16; 14640Sstevel@tonic-gate hp->ahi_rep_get32 = i_ddi_rep_get32; 14650Sstevel@tonic-gate hp->ahi_rep_get64 = i_ddi_rep_get64; 14660Sstevel@tonic-gate hp->ahi_rep_put16 = i_ddi_rep_put16; 14670Sstevel@tonic-gate hp->ahi_rep_put32 = i_ddi_rep_put32; 14680Sstevel@tonic-gate hp->ahi_rep_put64 = i_ddi_rep_put64; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate /* Legacy fault flags and support */ 14720Sstevel@tonic-gate hp->ahi_fault_check = i_ddi_acc_fault_check; 14730Sstevel@tonic-gate hp->ahi_fault_notify = i_ddi_acc_fault_notify; 14740Sstevel@tonic-gate hp->ahi_fault = 0; 14750Sstevel@tonic-gate impl_acc_err_init(handlep); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate void 14790Sstevel@tonic-gate i_ddi_acc_set_fault(ddi_acc_handle_t handle) 14800Sstevel@tonic-gate { 14810Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle; 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate if (!hp->ahi_fault) { 14840Sstevel@tonic-gate hp->ahi_fault = 1; 14850Sstevel@tonic-gate (*hp->ahi_fault_notify)(hp); 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate } 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate void 14900Sstevel@tonic-gate i_ddi_acc_clr_fault(ddi_acc_handle_t handle) 14910Sstevel@tonic-gate { 14920Sstevel@tonic-gate ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle; 14930Sstevel@tonic-gate 14940Sstevel@tonic-gate if (hp->ahi_fault) { 14950Sstevel@tonic-gate hp->ahi_fault = 0; 14960Sstevel@tonic-gate (*hp->ahi_fault_notify)(hp); 14970Sstevel@tonic-gate } 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate /* ARGSUSED */ 15010Sstevel@tonic-gate void 15020Sstevel@tonic-gate i_ddi_acc_fault_notify(ddi_acc_impl_t *hp) 15030Sstevel@tonic-gate { 15040Sstevel@tonic-gate /* Default version, does nothing */ 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate /* 15080Sstevel@tonic-gate * SECTION: Misc functions 15090Sstevel@tonic-gate */ 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate /* 15120Sstevel@tonic-gate * instance wrappers 15130Sstevel@tonic-gate */ 15140Sstevel@tonic-gate /*ARGSUSED*/ 15150Sstevel@tonic-gate uint_t 15160Sstevel@tonic-gate impl_assign_instance(dev_info_t *dip) 15170Sstevel@tonic-gate { 15180Sstevel@tonic-gate return ((uint_t)-1); 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate /*ARGSUSED*/ 15220Sstevel@tonic-gate int 15230Sstevel@tonic-gate impl_keep_instance(dev_info_t *dip) 15240Sstevel@tonic-gate { 15250Sstevel@tonic-gate return (DDI_FAILURE); 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /*ARGSUSED*/ 15290Sstevel@tonic-gate int 15300Sstevel@tonic-gate impl_free_instance(dev_info_t *dip) 15310Sstevel@tonic-gate { 15320Sstevel@tonic-gate return (DDI_FAILURE); 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate /*ARGSUSED*/ 15360Sstevel@tonic-gate int 15370Sstevel@tonic-gate impl_check_cpu(dev_info_t *devi) 15380Sstevel@tonic-gate { 15390Sstevel@tonic-gate return (DDI_SUCCESS); 15400Sstevel@tonic-gate } 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate 15430Sstevel@tonic-gate static const char *nocopydevs[] = { 15440Sstevel@tonic-gate "SUNW,ffb", 15450Sstevel@tonic-gate "SUNW,afb", 15460Sstevel@tonic-gate NULL 15470Sstevel@tonic-gate }; 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate /* 15500Sstevel@tonic-gate * Perform a copy from a memory mapped device (whose devinfo pointer is devi) 15510Sstevel@tonic-gate * separately mapped at devaddr in the kernel to a kernel buffer at kaddr. 15520Sstevel@tonic-gate */ 15530Sstevel@tonic-gate /*ARGSUSED*/ 15540Sstevel@tonic-gate int 15550Sstevel@tonic-gate e_ddi_copyfromdev(dev_info_t *devi, 15560Sstevel@tonic-gate off_t off, const void *devaddr, void *kaddr, size_t len) 15570Sstevel@tonic-gate { 15580Sstevel@tonic-gate const char **argv; 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate for (argv = nocopydevs; *argv; argv++) 15610Sstevel@tonic-gate if (strcmp(ddi_binding_name(devi), *argv) == 0) { 15620Sstevel@tonic-gate bzero(kaddr, len); 15630Sstevel@tonic-gate return (0); 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate bcopy(devaddr, kaddr, len); 15670Sstevel@tonic-gate return (0); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate /* 15710Sstevel@tonic-gate * Perform a copy to a memory mapped device (whose devinfo pointer is devi) 15720Sstevel@tonic-gate * separately mapped at devaddr in the kernel from a kernel buffer at kaddr. 15730Sstevel@tonic-gate */ 15740Sstevel@tonic-gate /*ARGSUSED*/ 15750Sstevel@tonic-gate int 15760Sstevel@tonic-gate e_ddi_copytodev(dev_info_t *devi, 15770Sstevel@tonic-gate off_t off, const void *kaddr, void *devaddr, size_t len) 15780Sstevel@tonic-gate { 15790Sstevel@tonic-gate const char **argv; 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate for (argv = nocopydevs; *argv; argv++) 15820Sstevel@tonic-gate if (strcmp(ddi_binding_name(devi), *argv) == 0) 15830Sstevel@tonic-gate return (1); 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate bcopy(kaddr, devaddr, len); 15860Sstevel@tonic-gate return (0); 15870Sstevel@tonic-gate } 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate /* 15900Sstevel@tonic-gate * Boot Configuration 15910Sstevel@tonic-gate */ 15920Sstevel@tonic-gate idprom_t idprom; 15930Sstevel@tonic-gate 15940Sstevel@tonic-gate /* 15950Sstevel@tonic-gate * Configure the hardware on the system. 15960Sstevel@tonic-gate * Called before the rootfs is mounted 15970Sstevel@tonic-gate */ 15980Sstevel@tonic-gate void 15990Sstevel@tonic-gate configure(void) 16000Sstevel@tonic-gate { 16010Sstevel@tonic-gate extern void i_ddi_init_root(); 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate /* We better have released boot by this time! */ 16040Sstevel@tonic-gate ASSERT(!bootops); 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate /* 16070Sstevel@tonic-gate * Determine whether or not to use the fpu, V9 SPARC cpus 16080Sstevel@tonic-gate * always have one. Could check for existence of a fp queue, 16090Sstevel@tonic-gate * Ultra I, II and IIa do not have a fp queue. 16100Sstevel@tonic-gate */ 16110Sstevel@tonic-gate if (fpu_exists) 16120Sstevel@tonic-gate fpu_probe(); 16130Sstevel@tonic-gate else 16140Sstevel@tonic-gate cmn_err(CE_CONT, "FPU not in use\n"); 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate #if 0 /* XXXQ - not necessary for sun4u */ 16170Sstevel@tonic-gate /* 16180Sstevel@tonic-gate * This following line fixes bugid 1041296; we need to do a 16190Sstevel@tonic-gate * prom_nextnode(0) because this call ALSO patches the DMA+ 16200Sstevel@tonic-gate * bug in Campus-B and Phoenix. The prom uncaches the traptable 16210Sstevel@tonic-gate * page as a side-effect of devr_next(0) (which prom_nextnode calls), 16220Sstevel@tonic-gate * so this *must* be executed early on. (XXX This is untrue for sun4u) 16230Sstevel@tonic-gate */ 1624789Sahrens (void) prom_nextnode((pnode_t)0); 16250Sstevel@tonic-gate #endif 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate /* 16280Sstevel@tonic-gate * Initialize devices on the machine. 16290Sstevel@tonic-gate * Uses configuration tree built by the PROMs to determine what 16300Sstevel@tonic-gate * is present, and builds a tree of prototype dev_info nodes 16310Sstevel@tonic-gate * corresponding to the hardware which identified itself. 16320Sstevel@tonic-gate */ 16330Sstevel@tonic-gate i_ddi_init_root(); 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate #ifdef DDI_PROP_DEBUG 16360Sstevel@tonic-gate (void) ddi_prop_debug(1); /* Enable property debugging */ 16370Sstevel@tonic-gate #endif /* DDI_PROP_DEBUG */ 16380Sstevel@tonic-gate } 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate /* 16410Sstevel@tonic-gate * The "status" property indicates the operational status of a device. 16420Sstevel@tonic-gate * If this property is present, the value is a string indicating the 16430Sstevel@tonic-gate * status of the device as follows: 16440Sstevel@tonic-gate * 16450Sstevel@tonic-gate * "okay" operational. 16460Sstevel@tonic-gate * "disabled" not operational, but might become operational. 16470Sstevel@tonic-gate * "fail" not operational because a fault has been detected, 16480Sstevel@tonic-gate * and it is unlikely that the device will become 16490Sstevel@tonic-gate * operational without repair. no additional details 16500Sstevel@tonic-gate * are available. 16510Sstevel@tonic-gate * "fail-xxx" not operational because a fault has been detected, 16520Sstevel@tonic-gate * and it is unlikely that the device will become 16530Sstevel@tonic-gate * operational without repair. "xxx" is additional 16540Sstevel@tonic-gate * human-readable information about the particular 16550Sstevel@tonic-gate * fault condition that was detected. 16560Sstevel@tonic-gate * 16570Sstevel@tonic-gate * The absence of this property means that the operational status is 16580Sstevel@tonic-gate * unknown or okay. 16590Sstevel@tonic-gate * 16600Sstevel@tonic-gate * This routine checks the status property of the specified device node 16610Sstevel@tonic-gate * and returns 0 if the operational status indicates failure, and 1 otherwise. 16620Sstevel@tonic-gate * 16630Sstevel@tonic-gate * The property may exist on plug-in cards the existed before IEEE 1275-1994. 16640Sstevel@tonic-gate * And, in that case, the property may not even be a string. So we carefully 16650Sstevel@tonic-gate * check for the value "fail", in the beginning of the string, noting 16660Sstevel@tonic-gate * the property length. 16670Sstevel@tonic-gate */ 16680Sstevel@tonic-gate int 16690Sstevel@tonic-gate status_okay(int id, char *buf, int buflen) 16700Sstevel@tonic-gate { 16710Sstevel@tonic-gate char status_buf[OBP_MAXPROPNAME]; 16720Sstevel@tonic-gate char *bufp = buf; 16730Sstevel@tonic-gate int len = buflen; 16740Sstevel@tonic-gate int proplen; 16750Sstevel@tonic-gate static const char *status = "status"; 16760Sstevel@tonic-gate static const char *fail = "fail"; 16770Sstevel@tonic-gate size_t fail_len = strlen(fail); 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate /* 16800Sstevel@tonic-gate * Get the proplen ... if it's smaller than "fail", 16810Sstevel@tonic-gate * or doesn't exist ... then we don't care, since 16820Sstevel@tonic-gate * the value can't begin with the char string "fail". 16830Sstevel@tonic-gate * 16840Sstevel@tonic-gate * NB: proplen, if it's a string, includes the NULL in the 16850Sstevel@tonic-gate * the size of the property, and fail_len does not. 16860Sstevel@tonic-gate */ 1687789Sahrens proplen = prom_getproplen((pnode_t)id, (caddr_t)status); 16880Sstevel@tonic-gate if (proplen <= fail_len) /* nonexistent or uninteresting len */ 16890Sstevel@tonic-gate return (1); 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate /* 16920Sstevel@tonic-gate * if a buffer was provided, use it 16930Sstevel@tonic-gate */ 16940Sstevel@tonic-gate if ((buf == (char *)NULL) || (buflen <= 0)) { 16950Sstevel@tonic-gate bufp = status_buf; 16960Sstevel@tonic-gate len = sizeof (status_buf); 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate *bufp = (char)0; 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate /* 17010Sstevel@tonic-gate * Get the property into the buffer, to the extent of the buffer, 17020Sstevel@tonic-gate * and in case the buffer is smaller than the property size, 17030Sstevel@tonic-gate * NULL terminate the buffer. (This handles the case where 17040Sstevel@tonic-gate * a buffer was passed in and the caller wants to print the 17050Sstevel@tonic-gate * value, but the buffer was too small). 17060Sstevel@tonic-gate */ 1707789Sahrens (void) prom_bounded_getprop((pnode_t)id, (caddr_t)status, 17080Sstevel@tonic-gate (caddr_t)bufp, len); 17090Sstevel@tonic-gate *(bufp + len - 1) = (char)0; 17100Sstevel@tonic-gate 17110Sstevel@tonic-gate /* 17120Sstevel@tonic-gate * If the value begins with the char string "fail", 17130Sstevel@tonic-gate * then it means the node is failed. We don't care 17140Sstevel@tonic-gate * about any other values. We assume the node is ok 17150Sstevel@tonic-gate * although it might be 'disabled'. 17160Sstevel@tonic-gate */ 17170Sstevel@tonic-gate if (strncmp(bufp, fail, fail_len) == 0) 17180Sstevel@tonic-gate return (0); 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate return (1); 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate /* 17250Sstevel@tonic-gate * We set the cpu type from the idprom, if we can. 17260Sstevel@tonic-gate * Note that we just read out the contents of it, for the most part. 17270Sstevel@tonic-gate */ 17280Sstevel@tonic-gate void 17290Sstevel@tonic-gate setcputype(void) 17300Sstevel@tonic-gate { 17310Sstevel@tonic-gate /* 17320Sstevel@tonic-gate * We cache the idprom info early on so that we don't 17330Sstevel@tonic-gate * rummage through the NVRAM unnecessarily later. 17340Sstevel@tonic-gate */ 17350Sstevel@tonic-gate (void) prom_getidprom((caddr_t)&idprom, sizeof (idprom)); 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate /* 17390Sstevel@tonic-gate * Here is where we actually infer meanings to the members of idprom_t 17400Sstevel@tonic-gate */ 17410Sstevel@tonic-gate void 17420Sstevel@tonic-gate parse_idprom(void) 17430Sstevel@tonic-gate { 17440Sstevel@tonic-gate if (idprom.id_format == IDFORM_1) { 17450Sstevel@tonic-gate uint_t i; 17460Sstevel@tonic-gate 17470Sstevel@tonic-gate (void) localetheraddr((struct ether_addr *)idprom.id_ether, 17480Sstevel@tonic-gate (struct ether_addr *)NULL); 17490Sstevel@tonic-gate 17500Sstevel@tonic-gate i = idprom.id_machine << 24; 17510Sstevel@tonic-gate i = i + idprom.id_serial; 17520Sstevel@tonic-gate numtos((ulong_t)i, hw_serial); 17530Sstevel@tonic-gate } else 17540Sstevel@tonic-gate prom_printf("Invalid format code in IDprom.\n"); 17550Sstevel@tonic-gate } 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate /* 17580Sstevel@tonic-gate * Allow for implementation specific correction of PROM property values. 17590Sstevel@tonic-gate */ 17600Sstevel@tonic-gate /*ARGSUSED*/ 17610Sstevel@tonic-gate void 17620Sstevel@tonic-gate impl_fix_props(dev_info_t *dip, dev_info_t *ch_dip, char *name, int len, 17630Sstevel@tonic-gate caddr_t buffer) 17640Sstevel@tonic-gate { 17650Sstevel@tonic-gate /* 17660Sstevel@tonic-gate * There are no adjustments needed in this implementation. 17670Sstevel@tonic-gate */ 17680Sstevel@tonic-gate } 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate /* 17710Sstevel@tonic-gate * The following functions ready a cautious request to go up to the nexus 17720Sstevel@tonic-gate * driver. It is up to the nexus driver to decide how to process the request. 17730Sstevel@tonic-gate * It may choose to call i_ddi_do_caut_get/put in this file, or do it 17740Sstevel@tonic-gate * differently. 17750Sstevel@tonic-gate */ 17760Sstevel@tonic-gate 17770Sstevel@tonic-gate static void 17780Sstevel@tonic-gate i_ddi_caut_getput_ctlops( 17790Sstevel@tonic-gate ddi_acc_impl_t *hp, uint64_t host_addr, uint64_t dev_addr, size_t size, 17800Sstevel@tonic-gate size_t repcount, uint_t flags, ddi_ctl_enum_t cmd) 17810Sstevel@tonic-gate { 17820Sstevel@tonic-gate peekpoke_ctlops_t cautacc_ctlops_arg; 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate cautacc_ctlops_arg.size = size; 17850Sstevel@tonic-gate cautacc_ctlops_arg.dev_addr = dev_addr; 17860Sstevel@tonic-gate cautacc_ctlops_arg.host_addr = host_addr; 17870Sstevel@tonic-gate cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp; 17880Sstevel@tonic-gate cautacc_ctlops_arg.repcount = repcount; 17890Sstevel@tonic-gate cautacc_ctlops_arg.flags = flags; 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate (void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd, 17920Sstevel@tonic-gate &cautacc_ctlops_arg, NULL); 17930Sstevel@tonic-gate } 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate uint8_t 17960Sstevel@tonic-gate i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr) 17970Sstevel@tonic-gate { 17980Sstevel@tonic-gate uint8_t value; 17990Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18000Sstevel@tonic-gate sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK); 18010Sstevel@tonic-gate 18020Sstevel@tonic-gate return (value); 18030Sstevel@tonic-gate } 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate uint16_t 18060Sstevel@tonic-gate i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr) 18070Sstevel@tonic-gate { 18080Sstevel@tonic-gate uint16_t value; 18090Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18100Sstevel@tonic-gate sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK); 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate return (value); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate 18150Sstevel@tonic-gate uint32_t 18160Sstevel@tonic-gate i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr) 18170Sstevel@tonic-gate { 18180Sstevel@tonic-gate uint32_t value; 18190Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18200Sstevel@tonic-gate sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK); 18210Sstevel@tonic-gate 18220Sstevel@tonic-gate return (value); 18230Sstevel@tonic-gate } 18240Sstevel@tonic-gate 18250Sstevel@tonic-gate uint64_t 18260Sstevel@tonic-gate i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr) 18270Sstevel@tonic-gate { 18280Sstevel@tonic-gate uint64_t value; 18290Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18300Sstevel@tonic-gate sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK); 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate return (value); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate 18350Sstevel@tonic-gate void 18360Sstevel@tonic-gate i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value) 18370Sstevel@tonic-gate { 18380Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18390Sstevel@tonic-gate sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE); 18400Sstevel@tonic-gate } 18410Sstevel@tonic-gate 18420Sstevel@tonic-gate void 18430Sstevel@tonic-gate i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value) 18440Sstevel@tonic-gate { 18450Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18460Sstevel@tonic-gate sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE); 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate void 18500Sstevel@tonic-gate i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value) 18510Sstevel@tonic-gate { 18520Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18530Sstevel@tonic-gate sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE); 18540Sstevel@tonic-gate } 18550Sstevel@tonic-gate 18560Sstevel@tonic-gate void 18570Sstevel@tonic-gate i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value) 18580Sstevel@tonic-gate { 18590Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr, 18600Sstevel@tonic-gate sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE); 18610Sstevel@tonic-gate } 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate void 18640Sstevel@tonic-gate i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr, 18650Sstevel@tonic-gate size_t repcount, uint_t flags) 18660Sstevel@tonic-gate { 18670Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 18680Sstevel@tonic-gate sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK); 18690Sstevel@tonic-gate } 18700Sstevel@tonic-gate 18710Sstevel@tonic-gate void 18720Sstevel@tonic-gate i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr, 18730Sstevel@tonic-gate uint16_t *dev_addr, size_t repcount, uint_t flags) 18740Sstevel@tonic-gate { 18750Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 18760Sstevel@tonic-gate sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK); 18770Sstevel@tonic-gate } 18780Sstevel@tonic-gate 18790Sstevel@tonic-gate void 18800Sstevel@tonic-gate i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr, 18810Sstevel@tonic-gate uint32_t *dev_addr, size_t repcount, uint_t flags) 18820Sstevel@tonic-gate { 18830Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 18840Sstevel@tonic-gate sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK); 18850Sstevel@tonic-gate } 18860Sstevel@tonic-gate 18870Sstevel@tonic-gate void 18880Sstevel@tonic-gate i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr, 18890Sstevel@tonic-gate uint64_t *dev_addr, size_t repcount, uint_t flags) 18900Sstevel@tonic-gate { 18910Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 18920Sstevel@tonic-gate sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK); 18930Sstevel@tonic-gate } 18940Sstevel@tonic-gate 18950Sstevel@tonic-gate void 18960Sstevel@tonic-gate i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr, 18970Sstevel@tonic-gate size_t repcount, uint_t flags) 18980Sstevel@tonic-gate { 18990Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 19000Sstevel@tonic-gate sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE); 19010Sstevel@tonic-gate } 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate void 19040Sstevel@tonic-gate i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr, 19050Sstevel@tonic-gate uint16_t *dev_addr, size_t repcount, uint_t flags) 19060Sstevel@tonic-gate { 19070Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 19080Sstevel@tonic-gate sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE); 19090Sstevel@tonic-gate } 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate void 19120Sstevel@tonic-gate i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr, 19130Sstevel@tonic-gate uint32_t *dev_addr, size_t repcount, uint_t flags) 19140Sstevel@tonic-gate { 19150Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 19160Sstevel@tonic-gate sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE); 19170Sstevel@tonic-gate } 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate void 19200Sstevel@tonic-gate i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr, 19210Sstevel@tonic-gate uint64_t *dev_addr, size_t repcount, uint_t flags) 19220Sstevel@tonic-gate { 19230Sstevel@tonic-gate i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr, 19240Sstevel@tonic-gate sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE); 19250Sstevel@tonic-gate } 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate /* 19280Sstevel@tonic-gate * This is called only to process peek/poke when the DIP is NULL. 19290Sstevel@tonic-gate * Assume that this is for memory, as nexi take care of device safe accesses. 19300Sstevel@tonic-gate */ 19310Sstevel@tonic-gate int 19320Sstevel@tonic-gate peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args) 19330Sstevel@tonic-gate { 19340Sstevel@tonic-gate int err = DDI_SUCCESS; 19350Sstevel@tonic-gate on_trap_data_t otd; 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate /* Set up protected environment. */ 19380Sstevel@tonic-gate if (!on_trap(&otd, OT_DATA_ACCESS)) { 19390Sstevel@tonic-gate uintptr_t tramp = otd.ot_trampoline; 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate if (cmd == DDI_CTLOPS_POKE) { 19420Sstevel@tonic-gate otd.ot_trampoline = (uintptr_t)&poke_fault; 19430Sstevel@tonic-gate err = do_poke(in_args->size, (void *)in_args->dev_addr, 19440Sstevel@tonic-gate (void *)in_args->host_addr); 19450Sstevel@tonic-gate } else { 19460Sstevel@tonic-gate otd.ot_trampoline = (uintptr_t)&peek_fault; 19470Sstevel@tonic-gate err = do_peek(in_args->size, (void *)in_args->dev_addr, 19480Sstevel@tonic-gate (void *)in_args->host_addr); 19490Sstevel@tonic-gate } 19500Sstevel@tonic-gate otd.ot_trampoline = tramp; 19510Sstevel@tonic-gate } else 19520Sstevel@tonic-gate err = DDI_FAILURE; 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate /* Take down protected environment. */ 19550Sstevel@tonic-gate no_trap(); 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate return (err); 19580Sstevel@tonic-gate } 19591991Sheppo 19601991Sheppo /* 19611991Sheppo * Platform independent DR routines 19621991Sheppo */ 19631991Sheppo 19641991Sheppo static int 19651991Sheppo ndi2errno(int n) 19661991Sheppo { 19671991Sheppo int err = 0; 19681991Sheppo 19691991Sheppo switch (n) { 19701991Sheppo case NDI_NOMEM: 19711991Sheppo err = ENOMEM; 19721991Sheppo break; 19731991Sheppo case NDI_BUSY: 19741991Sheppo err = EBUSY; 19751991Sheppo break; 19761991Sheppo case NDI_FAULT: 19771991Sheppo err = EFAULT; 19781991Sheppo break; 19791991Sheppo case NDI_FAILURE: 19801991Sheppo err = EIO; 19811991Sheppo break; 19821991Sheppo case NDI_SUCCESS: 19831991Sheppo break; 19841991Sheppo case NDI_BADHANDLE: 19851991Sheppo default: 19861991Sheppo err = EINVAL; 19871991Sheppo break; 19881991Sheppo } 19891991Sheppo return (err); 19901991Sheppo } 19911991Sheppo 19921991Sheppo /* 19931991Sheppo * Prom tree node list 19941991Sheppo */ 19951991Sheppo struct ptnode { 19961991Sheppo pnode_t nodeid; 19971991Sheppo struct ptnode *next; 19981991Sheppo }; 19991991Sheppo 20001991Sheppo /* 20011991Sheppo * Prom tree walk arg 20021991Sheppo */ 20031991Sheppo struct pta { 20041991Sheppo dev_info_t *pdip; 20051991Sheppo devi_branch_t *bp; 20061991Sheppo uint_t flags; 20071991Sheppo dev_info_t *fdip; 20081991Sheppo struct ptnode *head; 20091991Sheppo }; 20101991Sheppo 20111991Sheppo static void 20121991Sheppo visit_node(pnode_t nodeid, struct pta *ap) 20131991Sheppo { 20141991Sheppo struct ptnode **nextp; 20151991Sheppo int (*select)(pnode_t, void *, uint_t); 20161991Sheppo 20171991Sheppo ASSERT(nodeid != OBP_NONODE && nodeid != OBP_BADNODE); 20181991Sheppo 20191991Sheppo select = ap->bp->create.prom_branch_select; 20201991Sheppo 20211991Sheppo ASSERT(select); 20221991Sheppo 20231991Sheppo if (select(nodeid, ap->bp->arg, 0) == DDI_SUCCESS) { 20241991Sheppo 20251991Sheppo for (nextp = &ap->head; *nextp; nextp = &(*nextp)->next) 20261991Sheppo ; 20271991Sheppo 20281991Sheppo *nextp = kmem_zalloc(sizeof (struct ptnode), KM_SLEEP); 20291991Sheppo 20301991Sheppo (*nextp)->nodeid = nodeid; 20311991Sheppo } 20321991Sheppo 20331991Sheppo if ((ap->flags & DEVI_BRANCH_CHILD) == DEVI_BRANCH_CHILD) 20341991Sheppo return; 20351991Sheppo 20361991Sheppo nodeid = prom_childnode(nodeid); 20371991Sheppo while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) { 20381991Sheppo visit_node(nodeid, ap); 20391991Sheppo nodeid = prom_nextnode(nodeid); 20401991Sheppo } 20411991Sheppo } 20421991Sheppo 20434845Svikram /* 20444845Svikram * NOTE: The caller of this function must check for device contracts 20454845Svikram * or LDI callbacks against this dip before setting the dip offline. 20464845Svikram */ 20471991Sheppo static int 20484845Svikram set_infant_dip_offline(dev_info_t *dip, void *arg) 20491991Sheppo { 20504845Svikram char *path = (char *)arg; 20514845Svikram 20521991Sheppo ASSERT(dip); 20534845Svikram ASSERT(arg); 20544845Svikram 20554845Svikram if (i_ddi_node_state(dip) >= DS_ATTACHED) { 20564845Svikram (void) ddi_pathname(dip, path); 20574845Svikram cmn_err(CE_WARN, "Attempt to set offline flag on attached " 20584845Svikram "node: %s", path); 20594845Svikram return (DDI_FAILURE); 20604845Svikram } 20611991Sheppo 20621991Sheppo mutex_enter(&(DEVI(dip)->devi_lock)); 20631991Sheppo if (!DEVI_IS_DEVICE_OFFLINE(dip)) 20641991Sheppo DEVI_SET_DEVICE_OFFLINE(dip); 20651991Sheppo mutex_exit(&(DEVI(dip)->devi_lock)); 20661991Sheppo 20674845Svikram return (DDI_SUCCESS); 20684845Svikram } 20694845Svikram 20704845Svikram typedef struct result { 20714845Svikram char *path; 20724845Svikram int result; 20734845Svikram } result_t; 20744845Svikram 20754845Svikram static int 20764845Svikram dip_set_offline(dev_info_t *dip, void *arg) 20774845Svikram { 20784845Svikram int end; 20794845Svikram result_t *resp = (result_t *)arg; 20804845Svikram 20814845Svikram ASSERT(dip); 20824845Svikram ASSERT(resp); 20834845Svikram 20844845Svikram /* 20854845Svikram * We stop the walk if e_ddi_offline_notify() returns 20864845Svikram * failure, because this implies that one or more consumers 20874845Svikram * (either LDI or contract based) has blocked the offline. 20884845Svikram * So there is no point in conitnuing the walk 20894845Svikram */ 20904845Svikram if (e_ddi_offline_notify(dip) == DDI_FAILURE) { 20914845Svikram resp->result = DDI_FAILURE; 20924845Svikram return (DDI_WALK_TERMINATE); 20934845Svikram } 20944845Svikram 20954845Svikram /* 20964845Svikram * If set_infant_dip_offline() returns failure, it implies 20974845Svikram * that we failed to set a particular dip offline. This 20984845Svikram * does not imply that the offline as a whole should fail. 20994845Svikram * We want to do the best we can, so we continue the walk. 21004845Svikram */ 21014845Svikram if (set_infant_dip_offline(dip, resp->path) == DDI_SUCCESS) 21024845Svikram end = DDI_SUCCESS; 21034845Svikram else 21044845Svikram end = DDI_FAILURE; 21054845Svikram 21064845Svikram e_ddi_offline_finalize(dip, end); 21074845Svikram 21081991Sheppo return (DDI_WALK_CONTINUE); 21091991Sheppo } 21101991Sheppo 21114845Svikram /* 21124845Svikram * The call to e_ddi_offline_notify() exists for the 21134845Svikram * unlikely error case that a branch we are trying to 21144845Svikram * create already exists and has device contracts or LDI 21154845Svikram * event callbacks against it. 21164845Svikram * 21174845Svikram * We allow create to succeed for such branches only if 21184845Svikram * no constraints block the offline. 21194845Svikram */ 21204845Svikram static int 21214845Svikram branch_set_offline(dev_info_t *dip, char *path) 21224845Svikram { 21234845Svikram int circ; 21244845Svikram int end; 21254845Svikram result_t res; 21264845Svikram 21274845Svikram 21284845Svikram if (e_ddi_offline_notify(dip) == DDI_FAILURE) { 21294845Svikram return (DDI_FAILURE); 21304845Svikram } 21314845Svikram 21324845Svikram if (set_infant_dip_offline(dip, path) == DDI_SUCCESS) 21334845Svikram end = DDI_SUCCESS; 21344845Svikram else 21354845Svikram end = DDI_FAILURE; 21364845Svikram 21374845Svikram e_ddi_offline_finalize(dip, end); 21384845Svikram 21394845Svikram if (end == DDI_FAILURE) 21404845Svikram return (DDI_FAILURE); 21414845Svikram 21424845Svikram res.result = DDI_SUCCESS; 21434845Svikram res.path = path; 21444845Svikram 21454845Svikram ndi_devi_enter(dip, &circ); 21464845Svikram ddi_walk_devs(ddi_get_child(dip), dip_set_offline, &res); 21474845Svikram ndi_devi_exit(dip, circ); 21484845Svikram 21494845Svikram return (res.result); 21504845Svikram } 21514845Svikram 21521991Sheppo /*ARGSUSED*/ 21531991Sheppo static int 21541991Sheppo create_prom_branch(void *arg, int has_changed) 21551991Sheppo { 21564845Svikram int circ; 21571991Sheppo int exists, rv; 21581991Sheppo pnode_t nodeid; 21591991Sheppo struct ptnode *tnp; 21601991Sheppo dev_info_t *dip; 21611991Sheppo struct pta *ap = arg; 21621991Sheppo devi_branch_t *bp; 21634845Svikram char *path; 21641991Sheppo 21651991Sheppo ASSERT(ap); 21661991Sheppo ASSERT(ap->fdip == NULL); 21671991Sheppo ASSERT(ap->pdip && ndi_dev_is_prom_node(ap->pdip)); 21681991Sheppo 21691991Sheppo bp = ap->bp; 21701991Sheppo 21711991Sheppo nodeid = ddi_get_nodeid(ap->pdip); 21721991Sheppo if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE) { 21731991Sheppo cmn_err(CE_WARN, "create_prom_branch: invalid " 21741991Sheppo "nodeid: 0x%x", nodeid); 21751991Sheppo return (EINVAL); 21761991Sheppo } 21771991Sheppo 21781991Sheppo ap->head = NULL; 21791991Sheppo 21801991Sheppo nodeid = prom_childnode(nodeid); 21811991Sheppo while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) { 21821991Sheppo visit_node(nodeid, ap); 21831991Sheppo nodeid = prom_nextnode(nodeid); 21841991Sheppo } 21851991Sheppo 21861991Sheppo if (ap->head == NULL) 21871991Sheppo return (ENODEV); 21881991Sheppo 21894845Svikram path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 21901991Sheppo rv = 0; 21911991Sheppo while ((tnp = ap->head) != NULL) { 21921991Sheppo ap->head = tnp->next; 21931991Sheppo 21941991Sheppo ndi_devi_enter(ap->pdip, &circ); 21951991Sheppo 21961991Sheppo /* 21971991Sheppo * Check if the branch already exists. 21981991Sheppo */ 21991991Sheppo exists = 0; 22001991Sheppo dip = e_ddi_nodeid_to_dip(tnp->nodeid); 22011991Sheppo if (dip != NULL) { 22021991Sheppo exists = 1; 22031991Sheppo 22041991Sheppo /* Parent is held busy, so release hold */ 22051991Sheppo ndi_rele_devi(dip); 22061991Sheppo #ifdef DEBUG 22071991Sheppo cmn_err(CE_WARN, "create_prom_branch: dip(%p) exists" 22081991Sheppo " for nodeid 0x%x", (void *)dip, tnp->nodeid); 22091991Sheppo #endif 22101991Sheppo } else { 22111991Sheppo dip = i_ddi_create_branch(ap->pdip, tnp->nodeid); 22121991Sheppo } 22131991Sheppo 22141991Sheppo kmem_free(tnp, sizeof (struct ptnode)); 22151991Sheppo 22164845Svikram /* 22174845Svikram * Hold the branch if it is not already held 22184845Svikram */ 22194845Svikram if (dip && !exists) { 22204845Svikram e_ddi_branch_hold(dip); 22214845Svikram } 22224845Svikram 22234845Svikram ASSERT(dip == NULL || e_ddi_branch_held(dip)); 22244845Svikram 22254845Svikram /* 22264845Svikram * Set all dips in the newly created branch offline so that 22274845Svikram * only a "configure" operation can attach 22284845Svikram * the branch 22294845Svikram */ 22304845Svikram if (dip == NULL || branch_set_offline(dip, path) 22314845Svikram == DDI_FAILURE) { 22321991Sheppo ndi_devi_exit(ap->pdip, circ); 22331991Sheppo rv = EIO; 22341991Sheppo continue; 22351991Sheppo } 22361991Sheppo 22371991Sheppo ASSERT(ddi_get_parent(dip) == ap->pdip); 22381991Sheppo 22391991Sheppo ndi_devi_exit(ap->pdip, circ); 22401991Sheppo 22411991Sheppo if (ap->flags & DEVI_BRANCH_CONFIGURE) { 22421991Sheppo int error = e_ddi_branch_configure(dip, &ap->fdip, 0); 22431991Sheppo if (error && rv == 0) 22441991Sheppo rv = error; 22451991Sheppo } 22461991Sheppo 22471991Sheppo /* 22481991Sheppo * Invoke devi_branch_callback() (if it exists) only for 22491991Sheppo * newly created branches 22501991Sheppo */ 22511991Sheppo if (bp->devi_branch_callback && !exists) 22521991Sheppo bp->devi_branch_callback(dip, bp->arg, 0); 22531991Sheppo } 22541991Sheppo 22554845Svikram kmem_free(path, MAXPATHLEN); 22564845Svikram 22571991Sheppo return (rv); 22581991Sheppo } 22591991Sheppo 22601991Sheppo static int 22611991Sheppo sid_node_create(dev_info_t *pdip, devi_branch_t *bp, dev_info_t **rdipp) 22621991Sheppo { 22631991Sheppo int rv, circ, len; 22644845Svikram int i, flags, ret; 22651991Sheppo dev_info_t *dip; 22661991Sheppo char *nbuf; 22674845Svikram char *path; 22681991Sheppo static const char *noname = "<none>"; 22691991Sheppo 22701991Sheppo ASSERT(pdip); 22711991Sheppo ASSERT(DEVI_BUSY_OWNED(pdip)); 22721991Sheppo 22731991Sheppo flags = 0; 22741991Sheppo 22751991Sheppo /* 22761991Sheppo * Creating the root of a branch ? 22771991Sheppo */ 22781991Sheppo if (rdipp) { 22791991Sheppo *rdipp = NULL; 22801991Sheppo flags = DEVI_BRANCH_ROOT; 22811991Sheppo } 22821991Sheppo 22831991Sheppo ndi_devi_alloc_sleep(pdip, (char *)noname, DEVI_SID_NODEID, &dip); 22841991Sheppo rv = bp->create.sid_branch_create(dip, bp->arg, flags); 22851991Sheppo 22861991Sheppo nbuf = kmem_alloc(OBP_MAXDRVNAME, KM_SLEEP); 22871991Sheppo 22881991Sheppo if (rv == DDI_WALK_ERROR) { 22891991Sheppo cmn_err(CE_WARN, "e_ddi_branch_create: Error setting" 22901991Sheppo " properties on devinfo node %p", (void *)dip); 22911991Sheppo goto fail; 22921991Sheppo } 22931991Sheppo 22941991Sheppo len = OBP_MAXDRVNAME; 22951991Sheppo if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, 22961991Sheppo DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "name", nbuf, &len) 22971991Sheppo != DDI_PROP_SUCCESS) { 22981991Sheppo cmn_err(CE_WARN, "e_ddi_branch_create: devinfo node %p has" 22991991Sheppo "no name property", (void *)dip); 23001991Sheppo goto fail; 23011991Sheppo } 23021991Sheppo 23031991Sheppo ASSERT(i_ddi_node_state(dip) == DS_PROTO); 23041991Sheppo if (ndi_devi_set_nodename(dip, nbuf, 0) != NDI_SUCCESS) { 23051991Sheppo cmn_err(CE_WARN, "e_ddi_branch_create: cannot set name (%s)" 23061991Sheppo " for devinfo node %p", nbuf, (void *)dip); 23071991Sheppo goto fail; 23081991Sheppo } 23091991Sheppo 23101991Sheppo kmem_free(nbuf, OBP_MAXDRVNAME); 23111991Sheppo 23121991Sheppo /* 23131991Sheppo * Ignore bind failures just like boot does 23141991Sheppo */ 23151991Sheppo (void) ndi_devi_bind_driver(dip, 0); 23161991Sheppo 23171991Sheppo switch (rv) { 23181991Sheppo case DDI_WALK_CONTINUE: 23191991Sheppo case DDI_WALK_PRUNESIB: 23201991Sheppo ndi_devi_enter(dip, &circ); 23211991Sheppo 23221991Sheppo i = DDI_WALK_CONTINUE; 23231991Sheppo for (; i == DDI_WALK_CONTINUE; ) { 23241991Sheppo i = sid_node_create(dip, bp, NULL); 23251991Sheppo } 23261991Sheppo 23271991Sheppo ASSERT(i == DDI_WALK_ERROR || i == DDI_WALK_PRUNESIB); 23281991Sheppo if (i == DDI_WALK_ERROR) 23291991Sheppo rv = i; 23301991Sheppo /* 23311991Sheppo * If PRUNESIB stop creating siblings 23321991Sheppo * of dip's child. Subsequent walk behavior 23331991Sheppo * is determined by rv returned by dip. 23341991Sheppo */ 23351991Sheppo 23361991Sheppo ndi_devi_exit(dip, circ); 23371991Sheppo break; 23381991Sheppo case DDI_WALK_TERMINATE: 23391991Sheppo /* 23401991Sheppo * Don't create children and ask our parent 23411991Sheppo * to not create siblings either. 23421991Sheppo */ 23431991Sheppo rv = DDI_WALK_PRUNESIB; 23441991Sheppo break; 23451991Sheppo case DDI_WALK_PRUNECHILD: 23461991Sheppo /* 23471991Sheppo * Don't create children, but ask parent to continue 23481991Sheppo * with siblings. 23491991Sheppo */ 23501991Sheppo rv = DDI_WALK_CONTINUE; 23511991Sheppo break; 23521991Sheppo default: 23531991Sheppo ASSERT(0); 23541991Sheppo break; 23551991Sheppo } 23561991Sheppo 23571991Sheppo if (rdipp) 23581991Sheppo *rdipp = dip; 23591991Sheppo 23601991Sheppo /* 23614845Svikram * Set device offline - only the "configure" op should cause an attach. 23624845Svikram * Note that it is safe to set the dip offline without checking 23634845Svikram * for either device contract or layered driver (LDI) based constraints 23644845Svikram * since there cannot be any contracts or LDI opens of this device. 23654845Svikram * This is because this node is a newly created dip with the parent busy 23664845Svikram * held, so no other thread can come in and attach this dip. A dip that 23674845Svikram * has never been attached cannot have contracts since by definition 23684845Svikram * a device contract (an agreement between a process and a device minor 23694845Svikram * node) can only be created against a device that has minor nodes 23704845Svikram * i.e is attached. Similarly an LDI open will only succeed if the 23714845Svikram * dip is attached. We assert below that the dip is not attached. 23721991Sheppo */ 23734845Svikram ASSERT(i_ddi_node_state(dip) < DS_ATTACHED); 23744845Svikram path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23754845Svikram ret = set_infant_dip_offline(dip, path); 23764845Svikram ASSERT(ret == DDI_SUCCESS); 23774845Svikram kmem_free(path, MAXPATHLEN); 23781991Sheppo 23791991Sheppo return (rv); 23801991Sheppo fail: 23811991Sheppo (void) ndi_devi_free(dip); 23821991Sheppo kmem_free(nbuf, OBP_MAXDRVNAME); 23831991Sheppo return (DDI_WALK_ERROR); 23841991Sheppo } 23851991Sheppo 23861991Sheppo static int 23871991Sheppo create_sid_branch( 23881991Sheppo dev_info_t *pdip, 23891991Sheppo devi_branch_t *bp, 23901991Sheppo dev_info_t **dipp, 23911991Sheppo uint_t flags) 23921991Sheppo { 23931991Sheppo int rv = 0, state = DDI_WALK_CONTINUE; 23941991Sheppo dev_info_t *rdip; 23951991Sheppo 23961991Sheppo while (state == DDI_WALK_CONTINUE) { 23971991Sheppo int circ; 23981991Sheppo 23991991Sheppo ndi_devi_enter(pdip, &circ); 24001991Sheppo 24011991Sheppo state = sid_node_create(pdip, bp, &rdip); 24021991Sheppo if (rdip == NULL) { 24031991Sheppo ndi_devi_exit(pdip, circ); 24041991Sheppo ASSERT(state == DDI_WALK_ERROR); 24051991Sheppo break; 24061991Sheppo } 24071991Sheppo 24081991Sheppo e_ddi_branch_hold(rdip); 24091991Sheppo 24101991Sheppo ndi_devi_exit(pdip, circ); 24111991Sheppo 24121991Sheppo if (flags & DEVI_BRANCH_CONFIGURE) { 24131991Sheppo int error = e_ddi_branch_configure(rdip, dipp, 0); 24141991Sheppo if (error && rv == 0) 24151991Sheppo rv = error; 24161991Sheppo } 24171991Sheppo 24181991Sheppo /* 24191991Sheppo * devi_branch_callback() is optional 24201991Sheppo */ 24211991Sheppo if (bp->devi_branch_callback) 24221991Sheppo bp->devi_branch_callback(rdip, bp->arg, 0); 24231991Sheppo } 24241991Sheppo 24251991Sheppo ASSERT(state == DDI_WALK_ERROR || state == DDI_WALK_PRUNESIB); 24261991Sheppo 24271991Sheppo return (state == DDI_WALK_ERROR ? EIO : rv); 24281991Sheppo } 24291991Sheppo 24301991Sheppo int 24311991Sheppo e_ddi_branch_create( 24321991Sheppo dev_info_t *pdip, 24331991Sheppo devi_branch_t *bp, 24341991Sheppo dev_info_t **dipp, 24351991Sheppo uint_t flags) 24361991Sheppo { 24371991Sheppo int prom_devi, sid_devi, error; 24381991Sheppo 24391991Sheppo if (pdip == NULL || bp == NULL || bp->type == 0) 24401991Sheppo return (EINVAL); 24411991Sheppo 24421991Sheppo prom_devi = (bp->type == DEVI_BRANCH_PROM) ? 1 : 0; 24431991Sheppo sid_devi = (bp->type == DEVI_BRANCH_SID) ? 1 : 0; 24441991Sheppo 24451991Sheppo if (prom_devi && bp->create.prom_branch_select == NULL) 24461991Sheppo return (EINVAL); 24471991Sheppo else if (sid_devi && bp->create.sid_branch_create == NULL) 24481991Sheppo return (EINVAL); 24491991Sheppo else if (!prom_devi && !sid_devi) 24501991Sheppo return (EINVAL); 24511991Sheppo 24521991Sheppo if (flags & DEVI_BRANCH_EVENT) 24531991Sheppo return (EINVAL); 24541991Sheppo 24551991Sheppo if (prom_devi) { 24561991Sheppo struct pta pta = {0}; 24571991Sheppo 24581991Sheppo pta.pdip = pdip; 24591991Sheppo pta.bp = bp; 24601991Sheppo pta.flags = flags; 24611991Sheppo 24621991Sheppo error = prom_tree_access(create_prom_branch, &pta, NULL); 24631991Sheppo 24641991Sheppo if (dipp) 24651991Sheppo *dipp = pta.fdip; 24661991Sheppo else if (pta.fdip) 24671991Sheppo ndi_rele_devi(pta.fdip); 24681991Sheppo } else { 24691991Sheppo error = create_sid_branch(pdip, bp, dipp, flags); 24701991Sheppo } 24711991Sheppo 24721991Sheppo return (error); 24731991Sheppo } 24741991Sheppo 24751991Sheppo int 24761991Sheppo e_ddi_branch_configure(dev_info_t *rdip, dev_info_t **dipp, uint_t flags) 24771991Sheppo { 24781991Sheppo int circ, rv; 24791991Sheppo char *devnm; 24801991Sheppo dev_info_t *pdip; 24811991Sheppo 24821991Sheppo if (dipp) 24831991Sheppo *dipp = NULL; 24841991Sheppo 24851991Sheppo if (rdip == NULL || flags != 0 || (flags & DEVI_BRANCH_EVENT)) 24861991Sheppo return (EINVAL); 24871991Sheppo 24881991Sheppo pdip = ddi_get_parent(rdip); 24891991Sheppo 24901991Sheppo ndi_devi_enter(pdip, &circ); 24911991Sheppo 24921991Sheppo if (!e_ddi_branch_held(rdip)) { 24931991Sheppo ndi_devi_exit(pdip, circ); 24941991Sheppo cmn_err(CE_WARN, "e_ddi_branch_configure: " 24951991Sheppo "dip(%p) not held", (void *)rdip); 24961991Sheppo return (EINVAL); 24971991Sheppo } 24981991Sheppo 24991991Sheppo if (i_ddi_node_state(rdip) < DS_INITIALIZED) { 25001991Sheppo /* 25011991Sheppo * First attempt to bind a driver. If we fail, return 25021991Sheppo * success (On some platforms, dips for some device 25031991Sheppo * types (CPUs) may not have a driver) 25041991Sheppo */ 25051991Sheppo if (ndi_devi_bind_driver(rdip, 0) != NDI_SUCCESS) { 25061991Sheppo ndi_devi_exit(pdip, circ); 25071991Sheppo return (0); 25081991Sheppo } 25091991Sheppo 25101991Sheppo if (ddi_initchild(pdip, rdip) != DDI_SUCCESS) { 25111991Sheppo rv = NDI_FAILURE; 25121991Sheppo goto out; 25131991Sheppo } 25141991Sheppo } 25151991Sheppo 25161991Sheppo ASSERT(i_ddi_node_state(rdip) >= DS_INITIALIZED); 25171991Sheppo 25181991Sheppo devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP); 25191991Sheppo 25201991Sheppo (void) ddi_deviname(rdip, devnm); 25211991Sheppo 25221991Sheppo if ((rv = ndi_devi_config_one(pdip, devnm+1, &rdip, 25231991Sheppo NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) { 25241991Sheppo /* release hold from ndi_devi_config_one() */ 25251991Sheppo ndi_rele_devi(rdip); 25261991Sheppo } 25271991Sheppo 25281991Sheppo kmem_free(devnm, MAXNAMELEN + 1); 25291991Sheppo out: 25301991Sheppo if (rv != NDI_SUCCESS && dipp) { 25311991Sheppo ndi_hold_devi(rdip); 25321991Sheppo *dipp = rdip; 25331991Sheppo } 25341991Sheppo ndi_devi_exit(pdip, circ); 25351991Sheppo return (ndi2errno(rv)); 25361991Sheppo } 25371991Sheppo 25381991Sheppo void 25391991Sheppo e_ddi_branch_hold(dev_info_t *rdip) 25401991Sheppo { 25411991Sheppo if (e_ddi_branch_held(rdip)) { 25421991Sheppo cmn_err(CE_WARN, "e_ddi_branch_hold: branch already held"); 25431991Sheppo return; 25441991Sheppo } 25451991Sheppo 25461991Sheppo mutex_enter(&DEVI(rdip)->devi_lock); 25471991Sheppo if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) == 0) { 25481991Sheppo DEVI(rdip)->devi_flags |= DEVI_BRANCH_HELD; 25491991Sheppo DEVI(rdip)->devi_ref++; 25501991Sheppo } 25511991Sheppo ASSERT(DEVI(rdip)->devi_ref > 0); 25521991Sheppo mutex_exit(&DEVI(rdip)->devi_lock); 25531991Sheppo } 25541991Sheppo 25551991Sheppo int 25561991Sheppo e_ddi_branch_held(dev_info_t *rdip) 25571991Sheppo { 25581991Sheppo int rv = 0; 25591991Sheppo 25601991Sheppo mutex_enter(&DEVI(rdip)->devi_lock); 25611991Sheppo if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) && 25621991Sheppo DEVI(rdip)->devi_ref > 0) { 25631991Sheppo rv = 1; 25641991Sheppo } 25651991Sheppo mutex_exit(&DEVI(rdip)->devi_lock); 25661991Sheppo 25671991Sheppo return (rv); 25681991Sheppo } 25691991Sheppo void 25701991Sheppo e_ddi_branch_rele(dev_info_t *rdip) 25711991Sheppo { 25721991Sheppo mutex_enter(&DEVI(rdip)->devi_lock); 25731991Sheppo DEVI(rdip)->devi_flags &= ~DEVI_BRANCH_HELD; 25741991Sheppo DEVI(rdip)->devi_ref--; 25751991Sheppo mutex_exit(&DEVI(rdip)->devi_lock); 25761991Sheppo } 25771991Sheppo 25781991Sheppo int 25791991Sheppo e_ddi_branch_unconfigure( 25801991Sheppo dev_info_t *rdip, 25811991Sheppo dev_info_t **dipp, 25821991Sheppo uint_t flags) 25831991Sheppo { 25841991Sheppo int circ, rv; 25851991Sheppo int destroy; 25861991Sheppo char *devnm; 25871991Sheppo uint_t nflags; 25881991Sheppo dev_info_t *pdip; 25891991Sheppo 25901991Sheppo if (dipp) 25911991Sheppo *dipp = NULL; 25921991Sheppo 25931991Sheppo if (rdip == NULL) 25941991Sheppo return (EINVAL); 25951991Sheppo 25961991Sheppo pdip = ddi_get_parent(rdip); 25971991Sheppo 25981991Sheppo ASSERT(pdip); 25991991Sheppo 26001991Sheppo /* 26011991Sheppo * Check if caller holds pdip busy - can cause deadlocks during 26021991Sheppo * devfs_clean() 26031991Sheppo */ 26041991Sheppo if (DEVI_BUSY_OWNED(pdip)) { 26051991Sheppo cmn_err(CE_WARN, "e_ddi_branch_unconfigure: failed: parent" 26061991Sheppo " devinfo node(%p) is busy held", (void *)pdip); 26071991Sheppo return (EINVAL); 26081991Sheppo } 26091991Sheppo 26101991Sheppo destroy = (flags & DEVI_BRANCH_DESTROY) ? 1 : 0; 26111991Sheppo 26121991Sheppo devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP); 26131991Sheppo 26141991Sheppo ndi_devi_enter(pdip, &circ); 26151991Sheppo (void) ddi_deviname(rdip, devnm); 26161991Sheppo ndi_devi_exit(pdip, circ); 26171991Sheppo 26181991Sheppo /* 26191991Sheppo * ddi_deviname() returns a component name with / prepended. 26201991Sheppo */ 26214411Svikram (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE); 26221991Sheppo 26231991Sheppo ndi_devi_enter(pdip, &circ); 26241991Sheppo 26251991Sheppo /* 26261991Sheppo * Recreate device name as it may have changed state (init/uninit) 26271991Sheppo * when parent busy lock was dropped for devfs_clean() 26281991Sheppo */ 26291991Sheppo (void) ddi_deviname(rdip, devnm); 26301991Sheppo 26311991Sheppo if (!e_ddi_branch_held(rdip)) { 26321991Sheppo kmem_free(devnm, MAXNAMELEN + 1); 26331991Sheppo ndi_devi_exit(pdip, circ); 26341991Sheppo cmn_err(CE_WARN, "e_ddi_%s_branch: dip(%p) not held", 26351991Sheppo destroy ? "destroy" : "unconfigure", (void *)rdip); 26361991Sheppo return (EINVAL); 26371991Sheppo } 26381991Sheppo 26391991Sheppo /* 26401991Sheppo * Release hold on the branch. This is ok since we are holding the 26411991Sheppo * parent busy. If rdip is not removed, we must do a hold on the 26421991Sheppo * branch before returning. 26431991Sheppo */ 26441991Sheppo e_ddi_branch_rele(rdip); 26451991Sheppo 26461991Sheppo nflags = NDI_DEVI_OFFLINE; 26471991Sheppo if (destroy || (flags & DEVI_BRANCH_DESTROY)) { 26481991Sheppo nflags |= NDI_DEVI_REMOVE; 26491991Sheppo destroy = 1; 26501991Sheppo } else { 26511991Sheppo nflags |= NDI_UNCONFIG; /* uninit but don't remove */ 26521991Sheppo } 26531991Sheppo 26541991Sheppo if (flags & DEVI_BRANCH_EVENT) 26551991Sheppo nflags |= NDI_POST_EVENT; 26561991Sheppo 26571991Sheppo if (i_ddi_devi_attached(pdip) && 26581991Sheppo (i_ddi_node_state(rdip) >= DS_INITIALIZED)) { 26591991Sheppo rv = ndi_devi_unconfig_one(pdip, devnm+1, dipp, nflags); 26601991Sheppo } else { 26611991Sheppo rv = e_ddi_devi_unconfig(rdip, dipp, nflags); 26621991Sheppo if (rv == NDI_SUCCESS) { 26631991Sheppo ASSERT(!destroy || ddi_get_child(rdip) == NULL); 26641991Sheppo rv = ndi_devi_offline(rdip, nflags); 26651991Sheppo } 26661991Sheppo } 26671991Sheppo 26681991Sheppo if (!destroy || rv != NDI_SUCCESS) { 26691991Sheppo /* The dip still exists, so do a hold */ 26701991Sheppo e_ddi_branch_hold(rdip); 26711991Sheppo } 26721991Sheppo out: 26731991Sheppo kmem_free(devnm, MAXNAMELEN + 1); 26741991Sheppo ndi_devi_exit(pdip, circ); 26751991Sheppo return (ndi2errno(rv)); 26761991Sheppo } 26771991Sheppo 26781991Sheppo int 26791991Sheppo e_ddi_branch_destroy(dev_info_t *rdip, dev_info_t **dipp, uint_t flag) 26801991Sheppo { 26811991Sheppo return (e_ddi_branch_unconfigure(rdip, dipp, 26821991Sheppo flag|DEVI_BRANCH_DESTROY)); 26831991Sheppo } 26841991Sheppo 26851991Sheppo /* 26861991Sheppo * Number of chains for hash table 26871991Sheppo */ 26881991Sheppo #define NUMCHAINS 17 26891991Sheppo 26901991Sheppo /* 26911991Sheppo * Devinfo busy arg 26921991Sheppo */ 26931991Sheppo struct devi_busy { 26941991Sheppo int dv_total; 26951991Sheppo int s_total; 26961991Sheppo mod_hash_t *dv_hash; 26971991Sheppo mod_hash_t *s_hash; 26981991Sheppo int (*callback)(dev_info_t *, void *, uint_t); 26991991Sheppo void *arg; 27001991Sheppo }; 27011991Sheppo 27021991Sheppo static int 27031991Sheppo visit_dip(dev_info_t *dip, void *arg) 27041991Sheppo { 27051991Sheppo uintptr_t sbusy, dvbusy, ref; 27061991Sheppo struct devi_busy *bsp = arg; 27071991Sheppo 27081991Sheppo ASSERT(bsp->callback); 27091991Sheppo 27101991Sheppo /* 27111991Sheppo * A dip cannot be busy if its reference count is 0 27121991Sheppo */ 27131991Sheppo if ((ref = e_ddi_devi_holdcnt(dip)) == 0) { 27141991Sheppo return (bsp->callback(dip, bsp->arg, 0)); 27151991Sheppo } 27161991Sheppo 27171991Sheppo if (mod_hash_find(bsp->dv_hash, dip, (mod_hash_val_t *)&dvbusy)) 27181991Sheppo dvbusy = 0; 27191991Sheppo 27201991Sheppo /* 27211991Sheppo * To catch device opens currently maintained on specfs common snodes. 27221991Sheppo */ 27231991Sheppo if (mod_hash_find(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy)) 27241991Sheppo sbusy = 0; 27251991Sheppo 27261991Sheppo #ifdef DEBUG 27271991Sheppo if (ref < sbusy || ref < dvbusy) { 27281991Sheppo cmn_err(CE_WARN, "dip(%p): sopen = %lu, dvopen = %lu " 27291991Sheppo "dip ref = %lu\n", (void *)dip, sbusy, dvbusy, ref); 27301991Sheppo } 27311991Sheppo #endif 27321991Sheppo 27331991Sheppo dvbusy = (sbusy > dvbusy) ? sbusy : dvbusy; 27341991Sheppo 27351991Sheppo return (bsp->callback(dip, bsp->arg, dvbusy)); 27361991Sheppo } 27371991Sheppo 27381991Sheppo static int 27391991Sheppo visit_snode(struct snode *sp, void *arg) 27401991Sheppo { 27411991Sheppo uintptr_t sbusy; 27421991Sheppo dev_info_t *dip; 27431991Sheppo int count; 27441991Sheppo struct devi_busy *bsp = arg; 27451991Sheppo 27461991Sheppo ASSERT(sp); 27471991Sheppo 27481991Sheppo /* 27491991Sheppo * The stable lock is held. This prevents 27501991Sheppo * the snode and its associated dip from 27511991Sheppo * going away. 27521991Sheppo */ 27531991Sheppo dip = NULL; 27541991Sheppo count = spec_devi_open_count(sp, &dip); 27551991Sheppo 27561991Sheppo if (count <= 0) 27571991Sheppo return (DDI_WALK_CONTINUE); 27581991Sheppo 27591991Sheppo ASSERT(dip); 27601991Sheppo 27611991Sheppo if (mod_hash_remove(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy)) 27621991Sheppo sbusy = count; 27631991Sheppo else 27641991Sheppo sbusy += count; 27651991Sheppo 27661991Sheppo if (mod_hash_insert(bsp->s_hash, dip, (mod_hash_val_t)sbusy)) { 27671991Sheppo cmn_err(CE_WARN, "%s: s_hash insert failed: dip=0x%p, " 27681991Sheppo "sbusy = %lu", "e_ddi_branch_referenced", 27691991Sheppo (void *)dip, sbusy); 27701991Sheppo } 27711991Sheppo 27721991Sheppo bsp->s_total += count; 27731991Sheppo 27741991Sheppo return (DDI_WALK_CONTINUE); 27751991Sheppo } 27761991Sheppo 27771991Sheppo static void 27781991Sheppo visit_dvnode(struct dv_node *dv, void *arg) 27791991Sheppo { 27801991Sheppo uintptr_t dvbusy; 27811991Sheppo uint_t count; 27821991Sheppo struct vnode *vp; 27831991Sheppo struct devi_busy *bsp = arg; 27841991Sheppo 27851991Sheppo ASSERT(dv && dv->dv_devi); 27861991Sheppo 27871991Sheppo vp = DVTOV(dv); 27881991Sheppo 27891991Sheppo mutex_enter(&vp->v_lock); 27901991Sheppo count = vp->v_count; 27911991Sheppo mutex_exit(&vp->v_lock); 27921991Sheppo 27931991Sheppo if (!count) 27941991Sheppo return; 27951991Sheppo 27961991Sheppo if (mod_hash_remove(bsp->dv_hash, dv->dv_devi, 27971991Sheppo (mod_hash_val_t *)&dvbusy)) 27981991Sheppo dvbusy = count; 27991991Sheppo else 28001991Sheppo dvbusy += count; 28011991Sheppo 28021991Sheppo if (mod_hash_insert(bsp->dv_hash, dv->dv_devi, 28031991Sheppo (mod_hash_val_t)dvbusy)) { 28041991Sheppo cmn_err(CE_WARN, "%s: dv_hash insert failed: dip=0x%p, " 28051991Sheppo "dvbusy=%lu", "e_ddi_branch_referenced", 28061991Sheppo (void *)dv->dv_devi, dvbusy); 28071991Sheppo } 28081991Sheppo 28091991Sheppo bsp->dv_total += count; 28101991Sheppo } 28111991Sheppo 28121991Sheppo /* 28131991Sheppo * Returns reference count on success or -1 on failure. 28141991Sheppo */ 28151991Sheppo int 28161991Sheppo e_ddi_branch_referenced( 28171991Sheppo dev_info_t *rdip, 28181991Sheppo int (*callback)(dev_info_t *dip, void *arg, uint_t ref), 28191991Sheppo void *arg) 28201991Sheppo { 28211991Sheppo int circ; 28221991Sheppo char *path; 28231991Sheppo dev_info_t *pdip; 28241991Sheppo struct devi_busy bsa = {0}; 28251991Sheppo 28261991Sheppo ASSERT(rdip); 28271991Sheppo 28281991Sheppo path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 28291991Sheppo 28301991Sheppo ndi_hold_devi(rdip); 28311991Sheppo 28321991Sheppo pdip = ddi_get_parent(rdip); 28331991Sheppo 28341991Sheppo ASSERT(pdip); 28351991Sheppo 28361991Sheppo /* 28371991Sheppo * Check if caller holds pdip busy - can cause deadlocks during 28381991Sheppo * devfs_walk() 28391991Sheppo */ 28401991Sheppo if (!e_ddi_branch_held(rdip) || DEVI_BUSY_OWNED(pdip)) { 28411991Sheppo cmn_err(CE_WARN, "e_ddi_branch_referenced: failed: " 28421991Sheppo "devinfo branch(%p) not held or parent busy held", 28431991Sheppo (void *)rdip); 28441991Sheppo ndi_rele_devi(rdip); 28451991Sheppo kmem_free(path, MAXPATHLEN); 28461991Sheppo return (-1); 28471991Sheppo } 28481991Sheppo 28491991Sheppo ndi_devi_enter(pdip, &circ); 28501991Sheppo (void) ddi_pathname(rdip, path); 28511991Sheppo ndi_devi_exit(pdip, circ); 28521991Sheppo 28531991Sheppo bsa.dv_hash = mod_hash_create_ptrhash("dv_node busy hash", NUMCHAINS, 28541991Sheppo mod_hash_null_valdtor, sizeof (struct dev_info)); 28551991Sheppo 28561991Sheppo bsa.s_hash = mod_hash_create_ptrhash("snode busy hash", NUMCHAINS, 28571991Sheppo mod_hash_null_valdtor, sizeof (struct snode)); 28581991Sheppo 28591991Sheppo if (devfs_walk(path, visit_dvnode, &bsa)) { 28601991Sheppo cmn_err(CE_WARN, "e_ddi_branch_referenced: " 28611991Sheppo "devfs walk failed for: %s", path); 28621991Sheppo kmem_free(path, MAXPATHLEN); 28631991Sheppo bsa.s_total = bsa.dv_total = -1; 28641991Sheppo goto out; 28651991Sheppo } 28661991Sheppo 28671991Sheppo kmem_free(path, MAXPATHLEN); 28681991Sheppo 28691991Sheppo /* 28701991Sheppo * Walk the snode table to detect device opens, which are currently 28711991Sheppo * maintained on specfs common snodes. 28721991Sheppo */ 28731991Sheppo spec_snode_walk(visit_snode, &bsa); 28741991Sheppo 28751991Sheppo if (callback == NULL) 28761991Sheppo goto out; 28771991Sheppo 28781991Sheppo bsa.callback = callback; 28791991Sheppo bsa.arg = arg; 28801991Sheppo 28811991Sheppo if (visit_dip(rdip, &bsa) == DDI_WALK_CONTINUE) { 28821991Sheppo ndi_devi_enter(rdip, &circ); 28831991Sheppo ddi_walk_devs(ddi_get_child(rdip), visit_dip, &bsa); 28841991Sheppo ndi_devi_exit(rdip, circ); 28851991Sheppo } 28861991Sheppo 28871991Sheppo out: 28881991Sheppo ndi_rele_devi(rdip); 28891991Sheppo mod_hash_destroy_ptrhash(bsa.s_hash); 28901991Sheppo mod_hash_destroy_ptrhash(bsa.dv_hash); 28911991Sheppo return (bsa.s_total > bsa.dv_total ? bsa.s_total : bsa.dv_total); 28921991Sheppo } 2893