xref: /onnv-gate/usr/src/uts/sun4u/io/px/px_lib4u.c (revision 383:45ab49b926a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
3427Sjchu #include <sys/fm/protocol.h>
3527Sjchu #include <sys/fm/util.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/disp.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <sys/vmem.h>
410Sstevel@tonic-gate #include <sys/iommutsb.h>
420Sstevel@tonic-gate #include <sys/cpuvar.h>
4327Sjchu #include <sys/ivintr.h>
44*383Set142600 #include <sys/byteorder.h>
450Sstevel@tonic-gate #include <px_obj.h>
460Sstevel@tonic-gate #include <pcie_pwr.h>
470Sstevel@tonic-gate #include "px_tools_var.h"
480Sstevel@tonic-gate #include <px_regs.h>
490Sstevel@tonic-gate #include <px_csr.h>
5027Sjchu #include <sys/machsystm.h>
510Sstevel@tonic-gate #include "px_lib4u.h"
5227Sjchu #include "px_err.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #pragma weak jbus_stst_order
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern void jbus_stst_order();
570Sstevel@tonic-gate 
580Sstevel@tonic-gate ulong_t px_mmu_dvma_end = 0xfffffffful;
590Sstevel@tonic-gate uint_t px_ranges_phi_mask = 0xfffffffful;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static int px_goto_l23ready(px_t *px_p);
62118Sjchu static int px_goto_l0(px_t *px_p);
63118Sjchu static int px_pre_pwron_check(px_t *px_p);
640Sstevel@tonic-gate static uint32_t px_identity_chip(px_t *px_p);
6527Sjchu static void px_lib_clr_errs(px_t *px_p, px_pec_t *pec_p);
6627Sjchu 
6727Sjchu /*
6827Sjchu  * px_lib_map_registers
6927Sjchu  *
7027Sjchu  * This function is called from the attach routine to map the registers
7127Sjchu  * accessed by this driver.
7227Sjchu  *
7327Sjchu  * used by: px_attach()
7427Sjchu  *
7527Sjchu  * return value: DDI_FAILURE on failure
7627Sjchu  */
7727Sjchu int
7827Sjchu px_lib_map_regs(pxu_t *pxu_p, dev_info_t *dip)
7927Sjchu {
8027Sjchu 	ddi_device_acc_attr_t	attr;
8127Sjchu 	px_reg_bank_t		reg_bank = PX_REG_CSR;
8227Sjchu 
8327Sjchu 	DBG(DBG_ATTACH, dip, "px_lib_map_regs: pxu_p:0x%p, dip 0x%p\n",
8427Sjchu 		pxu_p, dip);
8527Sjchu 
8627Sjchu 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
8727Sjchu 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
8827Sjchu 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
8927Sjchu 
9027Sjchu 	/*
9127Sjchu 	 * PCI CSR Base
9227Sjchu 	 */
9327Sjchu 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
9427Sjchu 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
9527Sjchu 		goto fail;
9627Sjchu 	}
9727Sjchu 
9827Sjchu 	reg_bank++;
9927Sjchu 
10027Sjchu 	/*
10127Sjchu 	 * XBUS CSR Base
10227Sjchu 	 */
10327Sjchu 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
10427Sjchu 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
10527Sjchu 		goto fail;
10627Sjchu 	}
10727Sjchu 
10827Sjchu 	pxu_p->px_address[reg_bank] -= FIRE_CONTROL_STATUS;
10927Sjchu 
11027Sjchu done:
11127Sjchu 	for (; reg_bank >= PX_REG_CSR; reg_bank--) {
11227Sjchu 		DBG(DBG_ATTACH, dip, "reg_bank 0x%x address 0x%p\n",
11327Sjchu 		    reg_bank, pxu_p->px_address[reg_bank]);
11427Sjchu 	}
11527Sjchu 
11627Sjchu 	return (DDI_SUCCESS);
11727Sjchu 
11827Sjchu fail:
11927Sjchu 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
12027Sjchu 	    ddi_driver_name(dip), ddi_get_instance(dip), reg_bank);
12127Sjchu 
12227Sjchu 	for (reg_bank--; reg_bank >= PX_REG_CSR; reg_bank--) {
12327Sjchu 		pxu_p->px_address[reg_bank] = NULL;
12427Sjchu 		ddi_regs_map_free(&pxu_p->px_ac[reg_bank]);
12527Sjchu 	}
12627Sjchu 
12727Sjchu 	return (DDI_FAILURE);
12827Sjchu }
12927Sjchu 
13027Sjchu /*
13127Sjchu  * px_lib_unmap_regs:
13227Sjchu  *
13327Sjchu  * This routine unmaps the registers mapped by map_px_registers.
13427Sjchu  *
13527Sjchu  * used by: px_detach(), and error conditions in px_attach()
13627Sjchu  *
13727Sjchu  * return value: none
13827Sjchu  */
13927Sjchu void
14027Sjchu px_lib_unmap_regs(pxu_t *pxu_p)
14127Sjchu {
14227Sjchu 	int i;
14327Sjchu 
14427Sjchu 	for (i = 0; i < PX_REG_MAX; i++) {
14527Sjchu 		if (pxu_p->px_ac[i])
14627Sjchu 			ddi_regs_map_free(&pxu_p->px_ac[i]);
14727Sjchu 	}
14827Sjchu }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
1510Sstevel@tonic-gate px_lib_dev_init(dev_info_t *dip, devhandle_t *dev_hdl)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
15427Sjchu 	caddr_t		xbc_csr_base, csr_base;
1550Sstevel@tonic-gate 	px_dvma_range_prop_t	px_dvma_range;
1560Sstevel@tonic-gate 	uint32_t	chip_id;
1570Sstevel@tonic-gate 	pxu_t		*pxu_p;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dip 0x%p\n", dip);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if ((chip_id = px_identity_chip(px_p)) == PX_CHIP_UNIDENTIFIED)
1620Sstevel@tonic-gate 		return (DDI_FAILURE);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	switch (chip_id) {
1650Sstevel@tonic-gate 	case FIRE_VER_10:
166225Sess 		cmn_err(CE_WARN, "FIRE Hardware Version 1.0 is not supported");
167225Sess 		return (DDI_FAILURE);
1680Sstevel@tonic-gate 	case FIRE_VER_20:
1690Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "FIRE Hardware Version 2.0\n");
1700Sstevel@tonic-gate 		break;
1710Sstevel@tonic-gate 	default:
17227Sjchu 		cmn_err(CE_WARN, "%s%d: FIRE Hardware Version Unknown\n",
1730Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1740Sstevel@tonic-gate 		return (DDI_FAILURE);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Allocate platform specific structure and link it to
1790Sstevel@tonic-gate 	 * the px state structure.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	pxu_p = kmem_zalloc(sizeof (pxu_t), KM_SLEEP);
1820Sstevel@tonic-gate 	pxu_p->chip_id = chip_id;
1830Sstevel@tonic-gate 	pxu_p->portid  = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1840Sstevel@tonic-gate 	    "portid", -1);
1850Sstevel@tonic-gate 
18627Sjchu 	/* Map in the registers */
18727Sjchu 	if (px_lib_map_regs(pxu_p, dip) == DDI_FAILURE) {
18827Sjchu 		kmem_free(pxu_p, sizeof (pxu_t));
18927Sjchu 
19027Sjchu 		return (DDI_FAILURE);
19127Sjchu 	}
19227Sjchu 
19327Sjchu 	xbc_csr_base = (caddr_t)pxu_p->px_address[PX_REG_XBC];
19427Sjchu 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
19527Sjchu 
1960Sstevel@tonic-gate 	pxu_p->tsb_cookie = iommu_tsb_alloc(pxu_p->portid);
1970Sstevel@tonic-gate 	pxu_p->tsb_size = iommu_tsb_cookie_to_size(pxu_p->tsb_cookie);
1980Sstevel@tonic-gate 	pxu_p->tsb_vaddr = iommu_tsb_cookie_to_va(pxu_p->tsb_cookie);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * Create "virtual-dma" property to support child devices
2020Sstevel@tonic-gate 	 * needing to know DVMA range.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	px_dvma_range.dvma_base = (uint32_t)px_mmu_dvma_end + 1
2050Sstevel@tonic-gate 	    - ((pxu_p->tsb_size >> 3) << MMU_PAGE_SHIFT);
2060Sstevel@tonic-gate 	px_dvma_range.dvma_len = (uint32_t)
2070Sstevel@tonic-gate 	    px_mmu_dvma_end - px_dvma_range.dvma_base + 1;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
2100Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&px_dvma_range,
2110Sstevel@tonic-gate 		sizeof (px_dvma_range_prop_t));
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * Initilize all fire hardware specific blocks.
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	hvio_cb_init(xbc_csr_base, pxu_p);
2160Sstevel@tonic-gate 	hvio_ib_init(csr_base, pxu_p);
2170Sstevel@tonic-gate 	hvio_pec_init(csr_base, pxu_p);
2180Sstevel@tonic-gate 	hvio_mmu_init(csr_base, pxu_p);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	px_p->px_plat_p = (void *)pxu_p;
2210Sstevel@tonic-gate 
22227Sjchu 	/*
22327Sjchu 	 * Initialize all the interrupt handlers
22427Sjchu 	 */
22527Sjchu 	px_err_reg_enable(px_p, PX_ERR_JBC);
22627Sjchu 	px_err_reg_enable(px_p, PX_ERR_MMU);
22727Sjchu 	px_err_reg_enable(px_p, PX_ERR_IMU);
22827Sjchu 	px_err_reg_enable(px_p, PX_ERR_TLU_UE);
22927Sjchu 	px_err_reg_enable(px_p, PX_ERR_TLU_CE);
23027Sjchu 	px_err_reg_enable(px_p, PX_ERR_TLU_OE);
23127Sjchu 	px_err_reg_enable(px_p, PX_ERR_ILU);
23227Sjchu 	px_err_reg_enable(px_p, PX_ERR_LPU_LINK);
23327Sjchu 	px_err_reg_enable(px_p, PX_ERR_LPU_PHY);
23427Sjchu 	px_err_reg_enable(px_p, PX_ERR_LPU_RX);
23527Sjchu 	px_err_reg_enable(px_p, PX_ERR_LPU_TX);
23627Sjchu 	px_err_reg_enable(px_p, PX_ERR_LPU_LTSSM);
23727Sjchu 	px_err_reg_enable(px_p, PX_ERR_LPU_GIGABLZ);
23827Sjchu 
2390Sstevel@tonic-gate 	/* Initilize device handle */
2400Sstevel@tonic-gate 	*dev_hdl = (devhandle_t)csr_base;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	return (DDI_SUCCESS);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate int
2480Sstevel@tonic-gate px_lib_dev_fini(dev_info_t *dip)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
2510Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);
2540Sstevel@tonic-gate 
25527Sjchu 	/*
25627Sjchu 	 * Deinitialize all the interrupt handlers
25727Sjchu 	 */
25827Sjchu 	px_err_reg_disable(px_p, PX_ERR_JBC);
25927Sjchu 	px_err_reg_disable(px_p, PX_ERR_MMU);
26027Sjchu 	px_err_reg_disable(px_p, PX_ERR_IMU);
26127Sjchu 	px_err_reg_disable(px_p, PX_ERR_TLU_UE);
26227Sjchu 	px_err_reg_disable(px_p, PX_ERR_TLU_CE);
26327Sjchu 	px_err_reg_disable(px_p, PX_ERR_TLU_OE);
26427Sjchu 	px_err_reg_disable(px_p, PX_ERR_ILU);
26527Sjchu 	px_err_reg_disable(px_p, PX_ERR_LPU_LINK);
26627Sjchu 	px_err_reg_disable(px_p, PX_ERR_LPU_PHY);
26727Sjchu 	px_err_reg_disable(px_p, PX_ERR_LPU_RX);
26827Sjchu 	px_err_reg_disable(px_p, PX_ERR_LPU_TX);
26927Sjchu 	px_err_reg_disable(px_p, PX_ERR_LPU_LTSSM);
27027Sjchu 	px_err_reg_disable(px_p, PX_ERR_LPU_GIGABLZ);
27127Sjchu 
2720Sstevel@tonic-gate 	iommu_tsb_free(pxu_p->tsb_cookie);
2730Sstevel@tonic-gate 
27427Sjchu 	px_lib_unmap_regs((pxu_t *)px_p->px_plat_p);
27527Sjchu 	kmem_free(px_p->px_plat_p, sizeof (pxu_t));
2760Sstevel@tonic-gate 	px_p->px_plat_p = NULL;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	return (DDI_SUCCESS);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*ARGSUSED*/
2820Sstevel@tonic-gate int
2830Sstevel@tonic-gate px_lib_intr_devino_to_sysino(dev_info_t *dip, devino_t devino,
2840Sstevel@tonic-gate     sysino_t *sysino)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
2870Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
2880Sstevel@tonic-gate 	uint64_t	ret;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: dip 0x%p "
2910Sstevel@tonic-gate 	    "devino 0x%x\n", dip, devino);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	if ((ret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip),
2940Sstevel@tonic-gate 	    pxu_p, devino, sysino)) != H_EOK) {
2950Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip,
2960Sstevel@tonic-gate 		    "hvio_intr_devino_to_sysino failed, ret 0x%lx\n", ret);
2970Sstevel@tonic-gate 		return (DDI_FAILURE);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: sysino 0x%llx\n",
3010Sstevel@tonic-gate 	    *sysino);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	return (DDI_SUCCESS);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*ARGSUSED*/
3070Sstevel@tonic-gate int
3080Sstevel@tonic-gate px_lib_intr_getvalid(dev_info_t *dip, sysino_t sysino,
3090Sstevel@tonic-gate     intr_valid_state_t *intr_valid_state)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	uint64_t	ret;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: dip 0x%p sysino 0x%llx\n",
3140Sstevel@tonic-gate 	    dip, sysino);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if ((ret = hvio_intr_getvalid(DIP_TO_HANDLE(dip),
3170Sstevel@tonic-gate 	    sysino, intr_valid_state)) != H_EOK) {
3180Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_getvalid failed, ret 0x%lx\n",
3190Sstevel@tonic-gate 		    ret);
3200Sstevel@tonic-gate 		return (DDI_FAILURE);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: intr_valid_state 0x%x\n",
3240Sstevel@tonic-gate 	    *intr_valid_state);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	return (DDI_SUCCESS);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /*ARGSUSED*/
3300Sstevel@tonic-gate int
3310Sstevel@tonic-gate px_lib_intr_setvalid(dev_info_t *dip, sysino_t sysino,
3320Sstevel@tonic-gate     intr_valid_state_t intr_valid_state)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	uint64_t	ret;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setvalid: dip 0x%p sysino 0x%llx "
3370Sstevel@tonic-gate 	    "intr_valid_state 0x%x\n", dip, sysino, intr_valid_state);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if ((ret = hvio_intr_setvalid(DIP_TO_HANDLE(dip),
3400Sstevel@tonic-gate 	    sysino, intr_valid_state)) != H_EOK) {
3410Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_setvalid failed, ret 0x%lx\n",
3420Sstevel@tonic-gate 		    ret);
3430Sstevel@tonic-gate 		return (DDI_FAILURE);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	return (DDI_SUCCESS);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*ARGSUSED*/
3500Sstevel@tonic-gate int
3510Sstevel@tonic-gate px_lib_intr_getstate(dev_info_t *dip, sysino_t sysino,
3520Sstevel@tonic-gate     intr_state_t *intr_state)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	uint64_t	ret;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: dip 0x%p sysino 0x%llx\n",
3570Sstevel@tonic-gate 	    dip, sysino);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if ((ret = hvio_intr_getstate(DIP_TO_HANDLE(dip),
3600Sstevel@tonic-gate 	    sysino, intr_state)) != H_EOK) {
3610Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_getstate failed, ret 0x%lx\n",
3620Sstevel@tonic-gate 		    ret);
3630Sstevel@tonic-gate 		return (DDI_FAILURE);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: intr_state 0x%x\n",
3670Sstevel@tonic-gate 	    *intr_state);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	return (DDI_SUCCESS);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate /*ARGSUSED*/
3730Sstevel@tonic-gate int
3740Sstevel@tonic-gate px_lib_intr_setstate(dev_info_t *dip, sysino_t sysino,
3750Sstevel@tonic-gate     intr_state_t intr_state)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	uint64_t	ret;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setstate: dip 0x%p sysino 0x%llx "
3800Sstevel@tonic-gate 	    "intr_state 0x%x\n", dip, sysino, intr_state);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if ((ret = hvio_intr_setstate(DIP_TO_HANDLE(dip),
3830Sstevel@tonic-gate 	    sysino, intr_state)) != H_EOK) {
3840Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_setstate failed, ret 0x%lx\n",
3850Sstevel@tonic-gate 		    ret);
3860Sstevel@tonic-gate 		return (DDI_FAILURE);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	return (DDI_SUCCESS);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*ARGSUSED*/
3930Sstevel@tonic-gate int
3940Sstevel@tonic-gate px_lib_intr_gettarget(dev_info_t *dip, sysino_t sysino, cpuid_t *cpuid)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	uint64_t	ret;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: dip 0x%p sysino 0x%llx\n",
3990Sstevel@tonic-gate 	    dip, sysino);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if ((ret = hvio_intr_gettarget(DIP_TO_HANDLE(dip),
4020Sstevel@tonic-gate 	    sysino, cpuid)) != H_EOK) {
4030Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_gettarget failed, ret 0x%lx\n",
4040Sstevel@tonic-gate 		    ret);
4050Sstevel@tonic-gate 		return (DDI_FAILURE);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: cpuid 0x%x\n", cpuid);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	return (DDI_SUCCESS);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*ARGSUSED*/
4140Sstevel@tonic-gate int
4150Sstevel@tonic-gate px_lib_intr_settarget(dev_info_t *dip, sysino_t sysino, cpuid_t cpuid)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	uint64_t	ret;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_settarget: dip 0x%p sysino 0x%llx "
4200Sstevel@tonic-gate 	    "cpuid 0x%x\n", dip, sysino, cpuid);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if ((ret = hvio_intr_settarget(DIP_TO_HANDLE(dip),
4230Sstevel@tonic-gate 	    sysino, cpuid)) != H_EOK) {
4240Sstevel@tonic-gate 		DBG(DBG_LIB_INT, dip, "hvio_intr_settarget failed, ret 0x%lx\n",
4250Sstevel@tonic-gate 		    ret);
4260Sstevel@tonic-gate 		return (DDI_FAILURE);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	return (DDI_SUCCESS);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate /*ARGSUSED*/
4330Sstevel@tonic-gate int
4340Sstevel@tonic-gate px_lib_intr_reset(dev_info_t *dip)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	devino_t	ino;
4370Sstevel@tonic-gate 	sysino_t	sysino;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	DBG(DBG_LIB_INT, dip, "px_lib_intr_reset: dip 0x%p\n", dip);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/* Reset all Interrupts */
4420Sstevel@tonic-gate 	for (ino = 0; ino < INTERRUPT_MAPPING_ENTRIES; ino++) {
4430Sstevel@tonic-gate 		if (px_lib_intr_devino_to_sysino(dip, ino,
4440Sstevel@tonic-gate 		    &sysino) != DDI_SUCCESS)
4450Sstevel@tonic-gate 			return (BF_FATAL);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		if (px_lib_intr_setstate(dip, sysino,
4480Sstevel@tonic-gate 		    INTR_IDLE_STATE) != DDI_SUCCESS)
4490Sstevel@tonic-gate 			return (BF_FATAL);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	return (BF_NONE);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*ARGSUSED*/
4560Sstevel@tonic-gate int
4570Sstevel@tonic-gate px_lib_iommu_map(dev_info_t *dip, tsbid_t tsbid, pages_t pages,
4580Sstevel@tonic-gate     io_attributes_t io_attributes, void *addr, size_t pfn_index,
4590Sstevel@tonic-gate     int flag)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
4620Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4630Sstevel@tonic-gate 	uint64_t	ret;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: dip 0x%p tsbid 0x%llx "
4660Sstevel@tonic-gate 	    "pages 0x%x atrr 0x%x addr 0x%p pfn_index 0x%llx, flag 0x%x\n",
4670Sstevel@tonic-gate 	    dip, tsbid, pages, io_attributes, addr, pfn_index, flag);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if ((ret = hvio_iommu_map(px_p->px_dev_hdl, pxu_p, tsbid, pages,
4700Sstevel@tonic-gate 	    io_attributes, addr, pfn_index, flag)) != H_EOK) {
4710Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
4720Sstevel@tonic-gate 		    "px_lib_iommu_map failed, ret 0x%lx\n", ret);
4730Sstevel@tonic-gate 		return (DDI_FAILURE);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	return (DDI_SUCCESS);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*ARGSUSED*/
4800Sstevel@tonic-gate int
4810Sstevel@tonic-gate px_lib_iommu_demap(dev_info_t *dip, tsbid_t tsbid, pages_t pages)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
4840Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
4850Sstevel@tonic-gate 	uint64_t	ret;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: dip 0x%p tsbid 0x%llx "
4880Sstevel@tonic-gate 	    "pages 0x%x\n", dip, tsbid, pages);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if ((ret = hvio_iommu_demap(px_p->px_dev_hdl, pxu_p, tsbid, pages))
4910Sstevel@tonic-gate 	    != H_EOK) {
4920Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
4930Sstevel@tonic-gate 		    "px_lib_iommu_demap failed, ret 0x%lx\n", ret);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		return (DDI_FAILURE);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	return (DDI_SUCCESS);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*ARGSUSED*/
5020Sstevel@tonic-gate int
5030Sstevel@tonic-gate px_lib_iommu_getmap(dev_info_t *dip, tsbid_t tsbid,
5040Sstevel@tonic-gate     io_attributes_t *attributes_p, r_addr_t *r_addr_p)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	px_t	*px_p = DIP_TO_STATE(dip);
5070Sstevel@tonic-gate 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
5080Sstevel@tonic-gate 	uint64_t	ret;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: dip 0x%p tsbid 0x%llx\n",
5110Sstevel@tonic-gate 	    dip, tsbid);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if ((ret = hvio_iommu_getmap(DIP_TO_HANDLE(dip), pxu_p, tsbid,
5140Sstevel@tonic-gate 	    attributes_p, r_addr_p)) != H_EOK) {
5150Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5160Sstevel@tonic-gate 		    "hvio_iommu_getmap failed, ret 0x%lx\n", ret);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		return ((ret == H_ENOMAP) ? DDI_DMA_NOMAPPING:DDI_FAILURE);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: attr 0x%x r_addr 0x%llx\n",
5220Sstevel@tonic-gate 	    *attributes_p, *r_addr_p);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	return (DDI_SUCCESS);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate  * Checks dma attributes against system bypass ranges
5300Sstevel@tonic-gate  * The bypass range is determined by the hardware. Return them so the
5310Sstevel@tonic-gate  * common code can do generic checking against them.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate /*ARGSUSED*/
5340Sstevel@tonic-gate int
5350Sstevel@tonic-gate px_lib_dma_bypass_rngchk(ddi_dma_attr_t *attrp, uint64_t *lo_p, uint64_t *hi_p)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	*lo_p = MMU_BYPASS_BASE;
5380Sstevel@tonic-gate 	*hi_p = MMU_BYPASS_END;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	return (DDI_SUCCESS);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate /*ARGSUSED*/
5450Sstevel@tonic-gate int
5460Sstevel@tonic-gate px_lib_iommu_getbypass(dev_info_t *dip, r_addr_t ra,
5470Sstevel@tonic-gate     io_attributes_t io_attributes, io_addr_t *io_addr_p)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	uint64_t	ret;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: dip 0x%p ra 0x%llx "
5520Sstevel@tonic-gate 	    "attr 0x%x\n", dip, ra, io_attributes);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if ((ret = hvio_iommu_getbypass(DIP_TO_HANDLE(dip), ra,
5550Sstevel@tonic-gate 	    io_attributes, io_addr_p)) != H_EOK) {
5560Sstevel@tonic-gate 		DBG(DBG_LIB_DMA, dip,
5570Sstevel@tonic-gate 		    "hvio_iommu_getbypass failed, ret 0x%lx\n", ret);
5580Sstevel@tonic-gate 		return (DDI_FAILURE);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: io_addr 0x%llx\n",
5620Sstevel@tonic-gate 	    *io_addr_p);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	return (DDI_SUCCESS);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate  * bus dma sync entry point.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate /*ARGSUSED*/
5710Sstevel@tonic-gate int
5720Sstevel@tonic-gate px_lib_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
5730Sstevel@tonic-gate 	off_t off, size_t len, uint_t cache_flags)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: dip 0x%p rdip 0x%p "
5780Sstevel@tonic-gate 	    "handle 0x%llx off 0x%x len 0x%x flags 0x%x\n",
5790Sstevel@tonic-gate 	    dip, rdip, handle, off, len, cache_flags);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * jbus_stst_order is found only in certain cpu modules.
5830Sstevel@tonic-gate 	 * Just return success if not present.
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	if (&jbus_stst_order == NULL)
5860Sstevel@tonic-gate 		return (DDI_SUCCESS);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (!(mp->dmai_flags & DMAI_FLAGS_INUSE)) {
58927Sjchu 		cmn_err(CE_WARN, "%s%d: Unbound dma handle %p.",
59027Sjchu 		    ddi_driver_name(rdip), ddi_get_instance(rdip), (void *)mp);
59127Sjchu 
5920Sstevel@tonic-gate 		return (DDI_FAILURE);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_NOSYNC)
5960Sstevel@tonic-gate 		return (DDI_SUCCESS);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * No flush needed when sending data from memory to device.
6000Sstevel@tonic-gate 	 * Nothing to do to "sync" memory to what device would already see.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	if (!(mp->dmai_rflags & DDI_DMA_READ) ||
6030Sstevel@tonic-gate 	    ((cache_flags & PX_DMA_SYNC_DDI_FLAGS) == DDI_DMA_SYNC_FORDEV))
6040Sstevel@tonic-gate 		return (DDI_SUCCESS);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * Perform necessary cpu workaround to ensure jbus ordering.
6080Sstevel@tonic-gate 	 * CPU's internal "invalidate FIFOs" are flushed.
6090Sstevel@tonic-gate 	 */
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate #if !defined(lint)
6120Sstevel@tonic-gate 	kpreempt_disable();
6130Sstevel@tonic-gate #endif
6140Sstevel@tonic-gate 	jbus_stst_order();
6150Sstevel@tonic-gate #if !defined(lint)
6160Sstevel@tonic-gate 	kpreempt_enable();
6170Sstevel@tonic-gate #endif
6180Sstevel@tonic-gate 	return (DDI_SUCCESS);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate /*
6220Sstevel@tonic-gate  * MSIQ Functions:
6230Sstevel@tonic-gate  */
6240Sstevel@tonic-gate /*ARGSUSED*/
6250Sstevel@tonic-gate int
6260Sstevel@tonic-gate px_lib_msiq_init(dev_info_t *dip)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
6290Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
6300Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
6310Sstevel@tonic-gate 	caddr_t		msiq_addr;
6320Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
6330Sstevel@tonic-gate 	size_t		size;
6340Sstevel@tonic-gate 	int		ret;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * Map the EQ memory into the Fire MMU (has to be 512KB aligned)
6400Sstevel@tonic-gate 	 * and then initialize the base address register.
6410Sstevel@tonic-gate 	 *
6420Sstevel@tonic-gate 	 * Allocate entries from Fire IOMMU so that the resulting address
6430Sstevel@tonic-gate 	 * is properly aligned.  Calculate the index of the first allocated
6440Sstevel@tonic-gate 	 * entry.  Note: The size of the mapping is assumed to be a multiple
6450Sstevel@tonic-gate 	 * of the page size.
6460Sstevel@tonic-gate 	 */
6470Sstevel@tonic-gate 	msiq_addr = (caddr_t)(((uint64_t)msiq_state_p->msiq_buf_p +
6480Sstevel@tonic-gate 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
6510Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	pxu_p->msiq_mapped_p = vmem_xalloc(px_p->px_mmu_p->mmu_dvma_map,
6540Sstevel@tonic-gate 	    size, (512 * 1024), 0, 0, NULL, NULL, VM_NOSLEEP | VM_BESTFIT);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	if (pxu_p->msiq_mapped_p == NULL)
6570Sstevel@tonic-gate 		return (DDI_FAILURE);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
6600Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if ((ret = px_lib_iommu_map(px_p->px_dip, PCI_TSBID(0, pg_index),
6630Sstevel@tonic-gate 	    MMU_BTOP(size), PCI_MAP_ATTR_WRITE, (void *)msiq_addr, 0,
6640Sstevel@tonic-gate 	    MMU_MAP_BUF)) != DDI_SUCCESS) {
6650Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
6660Sstevel@tonic-gate 		    "hvio_msiq_init failed, ret 0x%lx\n", ret);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		(void) px_lib_msiq_fini(dip);
6690Sstevel@tonic-gate 		return (DDI_FAILURE);
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	(void) hvio_msiq_init(DIP_TO_HANDLE(dip), pxu_p);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	return (DDI_SUCCESS);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /*ARGSUSED*/
6780Sstevel@tonic-gate int
6790Sstevel@tonic-gate px_lib_msiq_fini(dev_info_t *dip)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
6820Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
6830Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
6840Sstevel@tonic-gate 	px_dvma_addr_t	pg_index;
6850Sstevel@tonic-gate 	size_t		size;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * Unmap and free the EQ memory that had been mapped
6910Sstevel@tonic-gate 	 * into the Fire IOMMU.
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	size = msiq_state_p->msiq_cnt *
6940Sstevel@tonic-gate 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
6970Sstevel@tonic-gate 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	(void) px_lib_iommu_demap(px_p->px_dip,
7000Sstevel@tonic-gate 	    PCI_TSBID(0, pg_index), MMU_BTOP(size));
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/* Free the entries from the Fire MMU */
7030Sstevel@tonic-gate 	vmem_xfree(px_p->px_mmu_p->mmu_dvma_map,
7040Sstevel@tonic-gate 	    (void *)pxu_p->msiq_mapped_p, size);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	return (DDI_SUCCESS);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*ARGSUSED*/
7100Sstevel@tonic-gate int
7110Sstevel@tonic-gate px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
7120Sstevel@tonic-gate     uint_t *msiq_rec_cnt_p)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
7150Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
7160Sstevel@tonic-gate 	uint64_t	*msiq_addr;
7170Sstevel@tonic-gate 	size_t		msiq_size;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
7200Sstevel@tonic-gate 	    dip, msiq_id);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	msiq_addr = (uint64_t *)(((uint64_t)msiq_state_p->msiq_buf_p +
7230Sstevel@tonic-gate 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
7240Sstevel@tonic-gate 	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
7250Sstevel@tonic-gate 	ra_p = (r_addr_t *)((caddr_t)msiq_addr + (msiq_id * msiq_size));
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	*msiq_rec_cnt_p = msiq_state_p->msiq_rec_cnt;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
7300Sstevel@tonic-gate 	    ra_p, *msiq_rec_cnt_p);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	return (DDI_SUCCESS);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /*ARGSUSED*/
7360Sstevel@tonic-gate int
7370Sstevel@tonic-gate px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
7380Sstevel@tonic-gate     pci_msiq_valid_state_t *msiq_valid_state)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	uint64_t	ret;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
7430Sstevel@tonic-gate 	    dip, msiq_id);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
7460Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
7470Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7480Sstevel@tonic-gate 		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
7490Sstevel@tonic-gate 		return (DDI_FAILURE);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
7530Sstevel@tonic-gate 	    *msiq_valid_state);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	return (DDI_SUCCESS);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate /*ARGSUSED*/
7590Sstevel@tonic-gate int
7600Sstevel@tonic-gate px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
7610Sstevel@tonic-gate     pci_msiq_valid_state_t msiq_valid_state)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	uint64_t	ret;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
7660Sstevel@tonic-gate 	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
7690Sstevel@tonic-gate 	    msiq_id, msiq_valid_state)) != H_EOK) {
7700Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7710Sstevel@tonic-gate 		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
7720Sstevel@tonic-gate 		return (DDI_FAILURE);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	return (DDI_SUCCESS);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate /*ARGSUSED*/
7790Sstevel@tonic-gate int
7800Sstevel@tonic-gate px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
7810Sstevel@tonic-gate     pci_msiq_state_t *msiq_state)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	uint64_t	ret;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
7860Sstevel@tonic-gate 	    dip, msiq_id);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
7890Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
7900Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
7910Sstevel@tonic-gate 		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
7920Sstevel@tonic-gate 		return (DDI_FAILURE);
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
7960Sstevel@tonic-gate 	    *msiq_state);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	return (DDI_SUCCESS);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate /*ARGSUSED*/
8020Sstevel@tonic-gate int
8030Sstevel@tonic-gate px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
8040Sstevel@tonic-gate     pci_msiq_state_t msiq_state)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	uint64_t	ret;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
8090Sstevel@tonic-gate 	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
8120Sstevel@tonic-gate 	    msiq_id, msiq_state)) != H_EOK) {
8130Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8140Sstevel@tonic-gate 		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
8150Sstevel@tonic-gate 		return (DDI_FAILURE);
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	return (DDI_SUCCESS);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate /*ARGSUSED*/
8220Sstevel@tonic-gate int
8230Sstevel@tonic-gate px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
8240Sstevel@tonic-gate     msiqhead_t *msiq_head)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	uint64_t	ret;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
8290Sstevel@tonic-gate 	    dip, msiq_id);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
8320Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
8330Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8340Sstevel@tonic-gate 		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
8350Sstevel@tonic-gate 		return (DDI_FAILURE);
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: msiq_head 0x%x\n",
8390Sstevel@tonic-gate 	    *msiq_head);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	return (DDI_SUCCESS);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate /*ARGSUSED*/
8450Sstevel@tonic-gate int
8460Sstevel@tonic-gate px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
8470Sstevel@tonic-gate     msiqhead_t msiq_head)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	uint64_t	ret;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
8520Sstevel@tonic-gate 	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
8550Sstevel@tonic-gate 	    msiq_id, msiq_head)) != H_EOK) {
8560Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8570Sstevel@tonic-gate 		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
8580Sstevel@tonic-gate 		return (DDI_FAILURE);
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	return (DDI_SUCCESS);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*ARGSUSED*/
8650Sstevel@tonic-gate int
8660Sstevel@tonic-gate px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
8670Sstevel@tonic-gate     msiqtail_t *msiq_tail)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	uint64_t	ret;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
8720Sstevel@tonic-gate 	    dip, msiq_id);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
8750Sstevel@tonic-gate 	    msiq_id, msiq_tail)) != H_EOK) {
8760Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip,
8770Sstevel@tonic-gate 		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
8780Sstevel@tonic-gate 		return (DDI_FAILURE);
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
8820Sstevel@tonic-gate 	    *msiq_tail);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	return (DDI_SUCCESS);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate /*ARGSUSED*/
8880Sstevel@tonic-gate void
8890Sstevel@tonic-gate px_lib_get_msiq_rec(dev_info_t *dip, px_msiq_t *msiq_p, msiq_rec_t *msiq_rec_p)
8900Sstevel@tonic-gate {
8910Sstevel@tonic-gate 	eq_rec_t	*eq_rec_p = (eq_rec_t *)msiq_p->msiq_curr;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p eq_rec_p 0x%p\n",
8940Sstevel@tonic-gate 	    dip, eq_rec_p);
8950Sstevel@tonic-gate 
896287Smg140465 	if (!eq_rec_p->eq_rec_fmt_type) {
897287Smg140465 		/* Set msiq_rec_type to zero */
898287Smg140465 		msiq_rec_p->msiq_rec_type = 0;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		return;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: EQ RECORD, "
9040Sstevel@tonic-gate 	    "eq_rec_rid 0x%llx eq_rec_fmt_type 0x%llx "
9050Sstevel@tonic-gate 	    "eq_rec_len 0x%llx eq_rec_addr0 0x%llx "
9060Sstevel@tonic-gate 	    "eq_rec_addr1 0x%llx eq_rec_data0 0x%llx "
9070Sstevel@tonic-gate 	    "eq_rec_data1 0x%llx\n", eq_rec_p->eq_rec_rid,
9080Sstevel@tonic-gate 	    eq_rec_p->eq_rec_fmt_type, eq_rec_p->eq_rec_len,
9090Sstevel@tonic-gate 	    eq_rec_p->eq_rec_addr0, eq_rec_p->eq_rec_addr1,
9100Sstevel@tonic-gate 	    eq_rec_p->eq_rec_data0, eq_rec_p->eq_rec_data1);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * Only upper 4 bits of eq_rec_fmt_type is used
9140Sstevel@tonic-gate 	 * to identify the EQ record type.
9150Sstevel@tonic-gate 	 */
9160Sstevel@tonic-gate 	switch (eq_rec_p->eq_rec_fmt_type >> 3) {
9170Sstevel@tonic-gate 	case EQ_REC_MSI32:
9180Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI32_REC;
9190Sstevel@tonic-gate 
920225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
921225Sess 		    eq_rec_p->eq_rec_data0;
9220Sstevel@tonic-gate 		break;
9230Sstevel@tonic-gate 	case EQ_REC_MSI64:
9240Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSI64_REC;
9250Sstevel@tonic-gate 
926225Sess 		msiq_rec_p->msiq_rec_data.msi.msi_data =
927225Sess 		    eq_rec_p->eq_rec_data0;
9280Sstevel@tonic-gate 		break;
9290Sstevel@tonic-gate 	case EQ_REC_MSG:
9300Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_type = MSG_REC;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_route =
9330Sstevel@tonic-gate 		    eq_rec_p->eq_rec_fmt_type & 7;
9340Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_targ = eq_rec_p->eq_rec_rid;
9350Sstevel@tonic-gate 		msiq_rec_p->msiq_rec_data.msg.msg_code = eq_rec_p->eq_rec_data0;
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 	default:
9380Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_lib_get_msiq_rec: "
9390Sstevel@tonic-gate 		    "0x%lx is an unknown EQ record type",
9400Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
9410Sstevel@tonic-gate 		    eq_rec_p->eq_rec_fmt_type);
9420Sstevel@tonic-gate 		break;
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_rid = eq_rec_p->eq_rec_rid;
9460Sstevel@tonic-gate 	msiq_rec_p->msiq_rec_msi_addr = ((eq_rec_p->eq_rec_addr1 << 16) |
9470Sstevel@tonic-gate 	    (eq_rec_p->eq_rec_addr0 << 2));
9480Sstevel@tonic-gate 
949287Smg140465 	/* Zero out eq_rec_fmt_type field */
950287Smg140465 	eq_rec_p->eq_rec_fmt_type = 0;
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate /*
9540Sstevel@tonic-gate  * MSI Functions:
9550Sstevel@tonic-gate  */
9560Sstevel@tonic-gate /*ARGSUSED*/
9570Sstevel@tonic-gate int
9580Sstevel@tonic-gate px_lib_msi_init(dev_info_t *dip)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
9610Sstevel@tonic-gate 	px_msi_state_t	*msi_state_p = &px_p->px_ib_p->ib_msi_state;
9620Sstevel@tonic-gate 	uint64_t	ret;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if ((ret = hvio_msi_init(DIP_TO_HANDLE(dip),
9670Sstevel@tonic-gate 	    msi_state_p->msi_addr32, msi_state_p->msi_addr64)) != H_EOK) {
9680Sstevel@tonic-gate 		DBG(DBG_LIB_MSIQ, dip, "px_lib_msi_init failed, ret 0x%lx\n",
9690Sstevel@tonic-gate 		    ret);
9700Sstevel@tonic-gate 		return (DDI_FAILURE);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	return (DDI_SUCCESS);
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate /*ARGSUSED*/
9770Sstevel@tonic-gate int
9780Sstevel@tonic-gate px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
9790Sstevel@tonic-gate     msiqid_t *msiq_id)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	uint64_t	ret;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
9840Sstevel@tonic-gate 	    dip, msi_num);
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
9870Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
9880Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
9890Sstevel@tonic-gate 		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
9900Sstevel@tonic-gate 		return (DDI_FAILURE);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
9940Sstevel@tonic-gate 	    *msiq_id);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	return (DDI_SUCCESS);
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate /*ARGSUSED*/
10000Sstevel@tonic-gate int
10010Sstevel@tonic-gate px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
10020Sstevel@tonic-gate     msiqid_t msiq_id, msi_type_t msitype)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	uint64_t	ret;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
10070Sstevel@tonic-gate 	    "msq_id 0x%x\n", dip, msi_num, msiq_id);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
10100Sstevel@tonic-gate 	    msi_num, msiq_id)) != H_EOK) {
10110Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10120Sstevel@tonic-gate 		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
10130Sstevel@tonic-gate 		return (DDI_FAILURE);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	return (DDI_SUCCESS);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*ARGSUSED*/
10200Sstevel@tonic-gate int
10210Sstevel@tonic-gate px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
10220Sstevel@tonic-gate     pci_msi_valid_state_t *msi_valid_state)
10230Sstevel@tonic-gate {
10240Sstevel@tonic-gate 	uint64_t	ret;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
10270Sstevel@tonic-gate 	    dip, msi_num);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
10300Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
10310Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10320Sstevel@tonic-gate 		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
10330Sstevel@tonic-gate 		return (DDI_FAILURE);
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
10370Sstevel@tonic-gate 	    *msi_valid_state);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	return (DDI_SUCCESS);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate /*ARGSUSED*/
10430Sstevel@tonic-gate int
10440Sstevel@tonic-gate px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
10450Sstevel@tonic-gate     pci_msi_valid_state_t msi_valid_state)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	uint64_t	ret;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
10500Sstevel@tonic-gate 	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
10530Sstevel@tonic-gate 	    msi_num, msi_valid_state)) != H_EOK) {
10540Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10550Sstevel@tonic-gate 		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
10560Sstevel@tonic-gate 		return (DDI_FAILURE);
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	return (DDI_SUCCESS);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*ARGSUSED*/
10630Sstevel@tonic-gate int
10640Sstevel@tonic-gate px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
10650Sstevel@tonic-gate     pci_msi_state_t *msi_state)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	uint64_t	ret;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
10700Sstevel@tonic-gate 	    dip, msi_num);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
10730Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
10740Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10750Sstevel@tonic-gate 		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
10760Sstevel@tonic-gate 		return (DDI_FAILURE);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
10800Sstevel@tonic-gate 	    *msi_state);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	return (DDI_SUCCESS);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate /*ARGSUSED*/
10860Sstevel@tonic-gate int
10870Sstevel@tonic-gate px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
10880Sstevel@tonic-gate     pci_msi_state_t msi_state)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate 	uint64_t	ret;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
10930Sstevel@tonic-gate 	    "msi_state 0x%x\n", dip, msi_num, msi_state);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
10960Sstevel@tonic-gate 	    msi_num, msi_state)) != H_EOK) {
10970Sstevel@tonic-gate 		DBG(DBG_LIB_MSI, dip,
10980Sstevel@tonic-gate 		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
10990Sstevel@tonic-gate 		return (DDI_FAILURE);
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	return (DDI_SUCCESS);
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate /*
11060Sstevel@tonic-gate  * MSG Functions:
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate /*ARGSUSED*/
11090Sstevel@tonic-gate int
11100Sstevel@tonic-gate px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11110Sstevel@tonic-gate     msiqid_t *msiq_id)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	uint64_t	ret;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
11160Sstevel@tonic-gate 	    dip, msg_type);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
11190Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
11200Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11210Sstevel@tonic-gate 		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
11220Sstevel@tonic-gate 		return (DDI_FAILURE);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
11260Sstevel@tonic-gate 	    *msiq_id);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	return (DDI_SUCCESS);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /*ARGSUSED*/
11320Sstevel@tonic-gate int
11330Sstevel@tonic-gate px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
11340Sstevel@tonic-gate     msiqid_t msiq_id)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate 	uint64_t	ret;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msi_setstate: dip 0x%p msg_type 0x%x "
11390Sstevel@tonic-gate 	    "msiq_id 0x%x\n", dip, msg_type, msiq_id);
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
11420Sstevel@tonic-gate 	    msg_type, msiq_id)) != H_EOK) {
11430Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11440Sstevel@tonic-gate 		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
11450Sstevel@tonic-gate 		return (DDI_FAILURE);
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	return (DDI_SUCCESS);
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /*ARGSUSED*/
11520Sstevel@tonic-gate int
11530Sstevel@tonic-gate px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
11540Sstevel@tonic-gate     pcie_msg_valid_state_t *msg_valid_state)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	uint64_t	ret;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
11590Sstevel@tonic-gate 	    dip, msg_type);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
11620Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
11630Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11640Sstevel@tonic-gate 		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
11650Sstevel@tonic-gate 		return (DDI_FAILURE);
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
11690Sstevel@tonic-gate 	    *msg_valid_state);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	return (DDI_SUCCESS);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate /*ARGSUSED*/
11750Sstevel@tonic-gate int
11760Sstevel@tonic-gate px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
11770Sstevel@tonic-gate     pcie_msg_valid_state_t msg_valid_state)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate 	uint64_t	ret;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
11820Sstevel@tonic-gate 	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
11850Sstevel@tonic-gate 	    msg_valid_state)) != H_EOK) {
11860Sstevel@tonic-gate 		DBG(DBG_LIB_MSG, dip,
11870Sstevel@tonic-gate 		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
11880Sstevel@tonic-gate 		return (DDI_FAILURE);
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	return (DDI_SUCCESS);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate /*
11950Sstevel@tonic-gate  * Suspend/Resume Functions:
11960Sstevel@tonic-gate  * Currently unsupported by hypervisor
11970Sstevel@tonic-gate  */
11980Sstevel@tonic-gate int
11990Sstevel@tonic-gate px_lib_suspend(dev_info_t *dip)
12000Sstevel@tonic-gate {
12010Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12020Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12030Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12040Sstevel@tonic-gate 	uint64_t	ret;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	DBG(DBG_DETACH, dip, "px_lib_suspend: dip 0x%p\n", dip);
12070Sstevel@tonic-gate 
120827Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
120927Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	if ((ret = hvio_suspend(dev_hdl, pxu_p)) == H_EOK) {
12120Sstevel@tonic-gate 		px_p->px_cb_p->xbc_attachcnt--;
12130Sstevel@tonic-gate 		if (px_p->px_cb_p->xbc_attachcnt == 0)
12140Sstevel@tonic-gate 			if ((ret = hvio_cb_suspend(xbus_dev_hdl, pxu_p))
12150Sstevel@tonic-gate 			    != H_EOK)
12160Sstevel@tonic-gate 				px_p->px_cb_p->xbc_attachcnt++;
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	return ((ret != H_EOK) ? DDI_FAILURE: DDI_SUCCESS);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate void
12230Sstevel@tonic-gate px_lib_resume(dev_info_t *dip)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	px_t		*px_p = DIP_TO_STATE(dip);
12260Sstevel@tonic-gate 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
12270Sstevel@tonic-gate 	devhandle_t	dev_hdl, xbus_dev_hdl;
12280Sstevel@tonic-gate 	devino_t	pec_ino = px_p->px_inos[PX_INTR_PEC];
12290Sstevel@tonic-gate 	devino_t	xbc_ino = px_p->px_inos[PX_INTR_XBC];
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "px_lib_resume: dip 0x%p\n", dip);
12320Sstevel@tonic-gate 
123327Sjchu 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
123427Sjchu 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	px_p->px_cb_p->xbc_attachcnt++;
12370Sstevel@tonic-gate 	if (px_p->px_cb_p->xbc_attachcnt == 1)
12380Sstevel@tonic-gate 		hvio_cb_resume(dev_hdl, xbus_dev_hdl, xbc_ino, pxu_p);
12390Sstevel@tonic-gate 	hvio_resume(dev_hdl, pec_ino, pxu_p);
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate /*
12430Sstevel@tonic-gate  * PCI tool Functions:
12440Sstevel@tonic-gate  * Currently unsupported by hypervisor
12450Sstevel@tonic-gate  */
12460Sstevel@tonic-gate /*ARGSUSED*/
12470Sstevel@tonic-gate int
12480Sstevel@tonic-gate px_lib_tools_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
12490Sstevel@tonic-gate {
12500Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	DBG(DBG_TOOLS, dip, "px_lib_tools_dev_reg_ops: dip 0x%p arg 0x%p "
12530Sstevel@tonic-gate 	    "cmd 0x%x mode 0x%x\n", dip, arg, cmd, mode);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	return (px_dev_reg_ops(dip, arg, cmd, mode, px_p));
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate /*ARGSUSED*/
12590Sstevel@tonic-gate int
12600Sstevel@tonic-gate px_lib_tools_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	DBG(DBG_TOOLS, dip, "px_lib_tools_bus_reg_ops: dip 0x%p arg 0x%p "
12630Sstevel@tonic-gate 	    "cmd 0x%x mode 0x%x\n", dip, arg, cmd, mode);
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	return (px_bus_reg_ops(dip, arg, cmd, mode));
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate /*ARGSUSED*/
12690Sstevel@tonic-gate int
12700Sstevel@tonic-gate px_lib_tools_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
12710Sstevel@tonic-gate {
12720Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	DBG(DBG_TOOLS, dip, "px_lib_tools_intr_admn: dip 0x%p arg 0x%p "
12750Sstevel@tonic-gate 	    "cmd 0x%x mode 0x%x\n", dip, arg, cmd, mode);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	return (px_intr_admn(dip, arg, cmd, mode, px_p));
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate /*
12810Sstevel@tonic-gate  * Misc Functions:
12820Sstevel@tonic-gate  * Currently unsupported by hypervisor
12830Sstevel@tonic-gate  */
12840Sstevel@tonic-gate uint64_t
128527Sjchu px_lib_get_cb(dev_info_t *dip)
12860Sstevel@tonic-gate {
128727Sjchu 	px_t	*px_p = DIP_TO_STATE(dip);
128827Sjchu 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
128927Sjchu 
129027Sjchu 	return (CSR_XR((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1));
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate void
129427Sjchu px_lib_set_cb(dev_info_t *dip, uint64_t val)
12950Sstevel@tonic-gate {
129627Sjchu 	px_t	*px_p = DIP_TO_STATE(dip);
129727Sjchu 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
129827Sjchu 
129927Sjchu 	CSR_XS((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1, val);
13000Sstevel@tonic-gate }
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate /*ARGSUSED*/
13030Sstevel@tonic-gate int
13040Sstevel@tonic-gate px_lib_map_vconfig(dev_info_t *dip,
13050Sstevel@tonic-gate 	ddi_map_req_t *mp, pci_config_offset_t off,
13060Sstevel@tonic-gate 		pci_regspec_t *rp, caddr_t *addrp)
13070Sstevel@tonic-gate {
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * No special config space access services in this layer.
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 	return (DDI_FAILURE);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
131427Sjchu static void
131527Sjchu px_lib_clr_errs(px_t *px_p, px_pec_t *pec_p)
131627Sjchu {
131727Sjchu 	dev_info_t	*rpdip = px_p->px_dip;
131827Sjchu 	px_cb_t		*cb_p = px_p->px_cb_p;
131927Sjchu 	int		err = PX_OK, ret;
132027Sjchu 	int		acctype = pec_p->pec_safeacc_type;
132127Sjchu 	ddi_fm_error_t	derr;
132227Sjchu 
132327Sjchu 	/* Create the derr */
132427Sjchu 	bzero(&derr, sizeof (ddi_fm_error_t));
132527Sjchu 	derr.fme_version = DDI_FME_VERSION;
132627Sjchu 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
132727Sjchu 	derr.fme_flag = acctype;
132827Sjchu 
132927Sjchu 	if (acctype == DDI_FM_ERR_EXPECTED) {
133027Sjchu 		derr.fme_status = DDI_FM_NONFATAL;
133127Sjchu 		ndi_fm_acc_err_set(pec_p->pec_acc_hdl, &derr);
133227Sjchu 	}
133327Sjchu 
133427Sjchu 	mutex_enter(&cb_p->xbc_fm_mutex);
133527Sjchu 
133627Sjchu 	/* send ereport/handle/clear fire registers */
133727Sjchu 	err = px_err_handle(px_p, &derr, PX_LIB_CALL, B_TRUE);
133827Sjchu 
133927Sjchu 	/* Check all child devices for errors */
134027Sjchu 	ret = ndi_fm_handler_dispatch(rpdip, NULL, &derr);
134127Sjchu 
134227Sjchu 	mutex_exit(&cb_p->xbc_fm_mutex);
134327Sjchu 
134427Sjchu 	/*
134527Sjchu 	 * PX_FATAL_HW indicates a condition recovered from Fatal-Reset,
134627Sjchu 	 * therefore it does not cause panic.
134727Sjchu 	 */
134827Sjchu 	if ((err & (PX_FATAL_GOS | PX_FATAL_SW)) || (ret == DDI_FM_FATAL))
134927Sjchu 		fm_panic("Fatal System Port Error has occurred\n");
135027Sjchu }
135127Sjchu 
13520Sstevel@tonic-gate #ifdef  DEBUG
13530Sstevel@tonic-gate int	px_peekfault_cnt = 0;
13540Sstevel@tonic-gate int	px_pokefault_cnt = 0;
13550Sstevel@tonic-gate #endif  /* DEBUG */
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate /*ARGSUSED*/
13580Sstevel@tonic-gate static int
13590Sstevel@tonic-gate px_lib_do_poke(dev_info_t *dip, dev_info_t *rdip,
13600Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
13610Sstevel@tonic-gate {
13620Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
13630Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
13640Sstevel@tonic-gate 	int err = DDI_SUCCESS;
13650Sstevel@tonic-gate 	on_trap_data_t otd;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	mutex_enter(&pec_p->pec_pokefault_mutex);
13680Sstevel@tonic-gate 	pec_p->pec_ontrap_data = &otd;
136927Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/* Set up protected environment. */
13720Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
13730Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&poke_fault;
13760Sstevel@tonic-gate 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
13770Sstevel@tonic-gate 		    (void *)in_args->host_addr);
13780Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
13790Sstevel@tonic-gate 	} else
13800Sstevel@tonic-gate 		err = DDI_FAILURE;
13810Sstevel@tonic-gate 
138227Sjchu 	px_lib_clr_errs(px_p, pec_p);
138327Sjchu 
13840Sstevel@tonic-gate 	if (otd.ot_trap & OT_DATA_ACCESS)
13850Sstevel@tonic-gate 		err = DDI_FAILURE;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	/* Take down protected environment. */
13880Sstevel@tonic-gate 	no_trap();
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
139127Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
13920Sstevel@tonic-gate 	mutex_exit(&pec_p->pec_pokefault_mutex);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate #ifdef  DEBUG
13950Sstevel@tonic-gate 	if (err == DDI_FAILURE)
13960Sstevel@tonic-gate 		px_pokefault_cnt++;
13970Sstevel@tonic-gate #endif
13980Sstevel@tonic-gate 	return (err);
13990Sstevel@tonic-gate }
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate /*ARGSUSED*/
14020Sstevel@tonic-gate static int
14030Sstevel@tonic-gate px_lib_do_caut_put(dev_info_t *dip, dev_info_t *rdip,
14040Sstevel@tonic-gate     peekpoke_ctlops_t *cautacc_ctlops_arg)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
14070Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
14080Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
14090Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
14100Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
14110Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
14140Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
14150Sstevel@tonic-gate 	int err = DDI_SUCCESS;
14160Sstevel@tonic-gate 
141727Sjchu 	/*
141827Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
141927Sjchu 	 * mutex.
142027Sjchu 	 */
14210Sstevel@tonic-gate 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
14220Sstevel@tonic-gate 
142327Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
142427Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
142527Sjchu 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
14280Sstevel@tonic-gate 		for (; repcount; repcount--) {
14290Sstevel@tonic-gate 			switch (size) {
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 			case sizeof (uint8_t):
14320Sstevel@tonic-gate 				i_ddi_put8(hp, (uint8_t *)dev_addr,
14330Sstevel@tonic-gate 				    *(uint8_t *)host_addr);
14340Sstevel@tonic-gate 				break;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 			case sizeof (uint16_t):
14370Sstevel@tonic-gate 				i_ddi_put16(hp, (uint16_t *)dev_addr,
14380Sstevel@tonic-gate 				    *(uint16_t *)host_addr);
14390Sstevel@tonic-gate 				break;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 			case sizeof (uint32_t):
14420Sstevel@tonic-gate 				i_ddi_put32(hp, (uint32_t *)dev_addr,
14430Sstevel@tonic-gate 				    *(uint32_t *)host_addr);
14440Sstevel@tonic-gate 				break;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 			case sizeof (uint64_t):
14470Sstevel@tonic-gate 				i_ddi_put64(hp, (uint64_t *)dev_addr,
14480Sstevel@tonic-gate 				    *(uint64_t *)host_addr);
14490Sstevel@tonic-gate 				break;
14500Sstevel@tonic-gate 			}
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 			host_addr += size;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 			if (flags == DDI_DEV_AUTOINCR)
14550Sstevel@tonic-gate 				dev_addr += size;
14560Sstevel@tonic-gate 
145727Sjchu 			px_lib_clr_errs(px_p, pec_p);
145827Sjchu 
14590Sstevel@tonic-gate 			if (pec_p->pec_ontrap_data->ot_trap & OT_DATA_ACCESS) {
14600Sstevel@tonic-gate 				err = DDI_FAILURE;
14610Sstevel@tonic-gate #ifdef  DEBUG
14620Sstevel@tonic-gate 				px_pokefault_cnt++;
14630Sstevel@tonic-gate #endif
14640Sstevel@tonic-gate 				break;
14650Sstevel@tonic-gate 			}
14660Sstevel@tonic-gate 		}
14670Sstevel@tonic-gate 	}
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
14700Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
147127Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
14720Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
14730Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	return (err);
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate int
14800Sstevel@tonic-gate px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
14810Sstevel@tonic-gate     peekpoke_ctlops_t *in_args)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_put(dip, rdip, in_args) :
14840Sstevel@tonic-gate 	    px_lib_do_poke(dip, rdip, in_args));
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate /*ARGSUSED*/
14890Sstevel@tonic-gate static int
14900Sstevel@tonic-gate px_lib_do_peek(dev_info_t *dip, peekpoke_ctlops_t *in_args)
14910Sstevel@tonic-gate {
149227Sjchu 	px_t *px_p = DIP_TO_STATE(dip);
149327Sjchu 	px_pec_t *pec_p = px_p->px_pec_p;
14940Sstevel@tonic-gate 	int err = DDI_SUCCESS;
14950Sstevel@tonic-gate 	on_trap_data_t otd;
14960Sstevel@tonic-gate 
149727Sjchu 	mutex_enter(&pec_p->pec_pokefault_mutex);
149827Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
149927Sjchu 
15000Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
15010Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&peek_fault;
15040Sstevel@tonic-gate 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
15050Sstevel@tonic-gate 		    (void *)in_args->host_addr);
15060Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
15070Sstevel@tonic-gate 	} else
15080Sstevel@tonic-gate 		err = DDI_FAILURE;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	no_trap();
151127Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
151227Sjchu 	mutex_exit(&pec_p->pec_pokefault_mutex);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate #ifdef  DEBUG
15150Sstevel@tonic-gate 	if (err == DDI_FAILURE)
15160Sstevel@tonic-gate 		px_peekfault_cnt++;
15170Sstevel@tonic-gate #endif
15180Sstevel@tonic-gate 	return (err);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate static int
15230Sstevel@tonic-gate px_lib_do_caut_get(dev_info_t *dip, peekpoke_ctlops_t *cautacc_ctlops_arg)
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
15260Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
15270Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
15280Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
15290Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
15300Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
15330Sstevel@tonic-gate 	px_pec_t *pec_p = px_p->px_pec_p;
15340Sstevel@tonic-gate 	int err = DDI_SUCCESS;
15350Sstevel@tonic-gate 
153627Sjchu 	/*
153727Sjchu 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
153827Sjchu 	 * mutex.
153927Sjchu 	 */
154027Sjchu 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
154127Sjchu 
154227Sjchu 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
154327Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
15440Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (repcount == 1) {
15470Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
15480Sstevel@tonic-gate 			i_ddi_caut_get(size, (void *)dev_addr,
15490Sstevel@tonic-gate 			    (void *)host_addr);
15500Sstevel@tonic-gate 		} else {
15510Sstevel@tonic-gate 			int i;
15520Sstevel@tonic-gate 			uint8_t *ff_addr = (uint8_t *)host_addr;
15530Sstevel@tonic-gate 			for (i = 0; i < size; i++)
15540Sstevel@tonic-gate 				*ff_addr++ = 0xff;
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 			err = DDI_FAILURE;
15570Sstevel@tonic-gate #ifdef  DEBUG
15580Sstevel@tonic-gate 			px_peekfault_cnt++;
15590Sstevel@tonic-gate #endif
15600Sstevel@tonic-gate 		}
15610Sstevel@tonic-gate 	} else {
15620Sstevel@tonic-gate 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
15630Sstevel@tonic-gate 			for (; repcount; repcount--) {
15640Sstevel@tonic-gate 				i_ddi_caut_get(size, (void *)dev_addr,
15650Sstevel@tonic-gate 				    (void *)host_addr);
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 				host_addr += size;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 				if (flags == DDI_DEV_AUTOINCR)
15700Sstevel@tonic-gate 					dev_addr += size;
15710Sstevel@tonic-gate 			}
15720Sstevel@tonic-gate 		} else {
15730Sstevel@tonic-gate 			err = DDI_FAILURE;
15740Sstevel@tonic-gate #ifdef  DEBUG
15750Sstevel@tonic-gate 			px_peekfault_cnt++;
15760Sstevel@tonic-gate #endif
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
15810Sstevel@tonic-gate 	pec_p->pec_ontrap_data = NULL;
158227Sjchu 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
15830Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
15840Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	return (err);
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate /*ARGSUSED*/
15900Sstevel@tonic-gate int
15910Sstevel@tonic-gate px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
15920Sstevel@tonic-gate     peekpoke_ctlops_t *in_args, void *result)
15930Sstevel@tonic-gate {
15940Sstevel@tonic-gate 	result = (void *)in_args->host_addr;
15950Sstevel@tonic-gate 	return (in_args->handle ? px_lib_do_caut_get(dip, in_args) :
15960Sstevel@tonic-gate 	    px_lib_do_peek(dip, in_args));
15970Sstevel@tonic-gate }
1598118Sjchu 
15990Sstevel@tonic-gate /*
16000Sstevel@tonic-gate  * implements PPM interface
16010Sstevel@tonic-gate  */
16020Sstevel@tonic-gate int
16030Sstevel@tonic-gate px_lib_pmctl(int cmd, px_t *px_p)
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate 	ASSERT((cmd & ~PPMREQ_MASK) == PPMREQ);
16060Sstevel@tonic-gate 	switch (cmd) {
16070Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_OFF:
16080Sstevel@tonic-gate 		/*
16090Sstevel@tonic-gate 		 * Currently there is no device power management for
16100Sstevel@tonic-gate 		 * the root complex (fire). When there is we need to make
16110Sstevel@tonic-gate 		 * sure that it is at full power before trying to send the
16120Sstevel@tonic-gate 		 * PME_Turn_Off message.
16130Sstevel@tonic-gate 		 */
16140Sstevel@tonic-gate 		DBG(DBG_PWR, px_p->px_dip,
16150Sstevel@tonic-gate 		    "ioctl: request to send PME_Turn_Off\n");
16160Sstevel@tonic-gate 		return (px_goto_l23ready(px_p));
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	case PPMREQ_PRE_PWR_ON:
1619118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: PRE_PWR_ON request\n");
1620118Sjchu 		return (px_pre_pwron_check(px_p));
1621118Sjchu 
16220Sstevel@tonic-gate 	case PPMREQ_POST_PWR_ON:
1623118Sjchu 		DBG(DBG_PWR, px_p->px_dip, "ioctl: POST_PWR_ON request\n");
1624118Sjchu 		return (px_goto_l0(px_p));
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	default:
16270Sstevel@tonic-gate 		return (DDI_FAILURE);
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate }
16300Sstevel@tonic-gate 
1631287Smg140465 #define	MSEC_TO_USEC	1000
1632287Smg140465 
16330Sstevel@tonic-gate /*
16340Sstevel@tonic-gate  * sends PME_Turn_Off message to put the link in L2/L3 ready state.
16350Sstevel@tonic-gate  * called by px_ioctl.
16360Sstevel@tonic-gate  * returns DDI_SUCCESS or DDI_FAILURE
16370Sstevel@tonic-gate  * 1. Wait for link to be in L1 state (link status reg)
16380Sstevel@tonic-gate  * 2. write to PME_Turn_off reg to boradcast
16390Sstevel@tonic-gate  * 3. set timeout
16400Sstevel@tonic-gate  * 4. If timeout, return failure.
16410Sstevel@tonic-gate  * 5. If PM_TO_Ack, wait till link is in L2/L3 ready
16420Sstevel@tonic-gate  */
16430Sstevel@tonic-gate static int
16440Sstevel@tonic-gate px_goto_l23ready(px_t *px_p)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	pcie_pwr_t	*pwr_p;
164727Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
164827Sjchu 	caddr_t	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
16490Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
16500Sstevel@tonic-gate 	clock_t		end, timeleft;
1651118Sjchu 	int		mutex_held = 1;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	/* If no PM info, return failure */
16540Sstevel@tonic-gate 	if (!PCIE_PMINFO(px_p->px_dip) ||
16550Sstevel@tonic-gate 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
16560Sstevel@tonic-gate 		return (DDI_FAILURE);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
1659118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
16600Sstevel@tonic-gate 	/* Clear the PME_To_ACK receieved flag */
1661118Sjchu 	px_p->px_pm_flags &= ~PX_PMETOACK_RECVD;
1662287Smg140465 	/*
1663287Smg140465 	 * When P25 is the downstream device, after receiving
1664287Smg140465 	 * PME_To_ACK, fire will go to Detect state, which causes
1665287Smg140465 	 * the link down event. Inform FMA that this is expected.
1666287Smg140465 	 * In case of all other cards complaint with the pci express
1667287Smg140465 	 * spec, this will happen when the power is re-applied. FMA
1668287Smg140465 	 * code will clear this flag after one instance of LDN. Since
1669287Smg140465 	 * there will not be a LDN event for the spec compliant cards,
1670287Smg140465 	 * we need to clear the flag after receiving PME_To_ACK.
1671287Smg140465 	 */
1672287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
16730Sstevel@tonic-gate 	if (px_send_pme_turnoff(csr_base) != DDI_SUCCESS) {
16740Sstevel@tonic-gate 		ret = DDI_FAILURE;
16750Sstevel@tonic-gate 		goto l23ready_done;
16760Sstevel@tonic-gate 	}
1677118Sjchu 	px_p->px_pm_flags |= PX_PME_TURNOFF_PENDING;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	end = ddi_get_lbolt() + drv_usectohz(px_pme_to_ack_timeout);
1680118Sjchu 	while (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1681118Sjchu 		timeleft = cv_timedwait(&px_p->px_l23ready_cv,
1682118Sjchu 		    &px_p->px_l23ready_lock, end);
16830Sstevel@tonic-gate 		/*
16840Sstevel@tonic-gate 		 * if cv_timedwait returns -1, it is either
16850Sstevel@tonic-gate 		 * 1) timed out or
16860Sstevel@tonic-gate 		 * 2) there was a pre-mature wakeup but by the time
16870Sstevel@tonic-gate 		 * cv_timedwait is called again end < lbolt i.e.
16880Sstevel@tonic-gate 		 * end is in the past.
16890Sstevel@tonic-gate 		 * 3) By the time we make first cv_timedwait call,
16900Sstevel@tonic-gate 		 * end < lbolt is true.
16910Sstevel@tonic-gate 		 */
16920Sstevel@tonic-gate 		if (timeleft == -1)
16930Sstevel@tonic-gate 			break;
16940Sstevel@tonic-gate 	}
1695118Sjchu 	if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
16960Sstevel@tonic-gate 		/*
16970Sstevel@tonic-gate 		 * Either timedout or interrupt didn't get a
16980Sstevel@tonic-gate 		 * chance to grab the mutex and set the flag.
16990Sstevel@tonic-gate 		 * release the mutex and delay for sometime.
17000Sstevel@tonic-gate 		 * This will 1) give a chance for interrupt to
17010Sstevel@tonic-gate 		 * set the flag 2) creates a delay between two
17020Sstevel@tonic-gate 		 * consequetive requests.
17030Sstevel@tonic-gate 		 */
1704118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
1705287Smg140465 		delay(drv_usectohz(50 * MSEC_TO_USEC));
1706118Sjchu 		mutex_held = 0;
1707118Sjchu 		if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
17080Sstevel@tonic-gate 			ret = DDI_FAILURE;
17090Sstevel@tonic-gate 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
17100Sstevel@tonic-gate 			    " for PME_TO_ACK\n");
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 	}
1713287Smg140465 	px_p->px_pm_flags &=
1714287Smg140465 	    ~(PX_PME_TURNOFF_PENDING | PX_PMETOACK_RECVD | PX_LDN_EXPECTED);
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate l23ready_done:
1717118Sjchu 	if (mutex_held)
1718118Sjchu 		mutex_exit(&px_p->px_l23ready_lock);
1719118Sjchu 	/*
1720118Sjchu 	 * Wait till link is in L1 idle, if sending PME_Turn_Off
1721118Sjchu 	 * was succesful.
1722118Sjchu 	 */
1723118Sjchu 	if (ret == DDI_SUCCESS) {
1724118Sjchu 		if (px_link_wait4l1idle(csr_base) != DDI_SUCCESS) {
1725118Sjchu 			DBG(DBG_PWR, px_p->px_dip, " Link is not at L1"
1726287Smg140465 			    " even though we received PME_To_ACK.\n");
1727287Smg140465 			/*
1728287Smg140465 			 * Workaround for hardware bug with P25.
1729287Smg140465 			 * Due to a hardware bug with P25, link state
1730287Smg140465 			 * will be Detect state rather than L1 after
1731287Smg140465 			 * link is transitioned to L23Ready state. Since
1732287Smg140465 			 * we don't know whether link is L23ready state
1733287Smg140465 			 * without Fire's state being L1_idle, we delay
1734287Smg140465 			 * here just to make sure that we wait till link
1735287Smg140465 			 * is transitioned to L23Ready state.
1736287Smg140465 			 */
1737287Smg140465 			delay(drv_usectohz(100 * MSEC_TO_USEC));
1738287Smg140465 		}
1739287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L3;
1740118Sjchu 
1741118Sjchu 	}
17420Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
17430Sstevel@tonic-gate 	return (ret);
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate 
1746118Sjchu /*
1747118Sjchu  * Message interrupt handler intended to be shared for both
1748118Sjchu  * PME and PME_TO_ACK msg handling, currently only handles
1749118Sjchu  * PME_To_ACK message.
1750118Sjchu  */
1751118Sjchu uint_t
1752118Sjchu px_pmeq_intr(caddr_t arg)
1753118Sjchu {
1754118Sjchu 	px_t	*px_p = (px_t *)arg;
1755118Sjchu 
1756287Smg140465 	DBG(DBG_PWR, px_p->px_dip, " PME_To_ACK received \n");
1757118Sjchu 	mutex_enter(&px_p->px_l23ready_lock);
1758118Sjchu 	cv_broadcast(&px_p->px_l23ready_cv);
1759118Sjchu 	if (px_p->px_pm_flags & PX_PME_TURNOFF_PENDING) {
1760118Sjchu 		px_p->px_pm_flags |= PX_PMETOACK_RECVD;
1761118Sjchu 	} else {
1762118Sjchu 		/*
1763118Sjchu 		 * This maybe the second ack received. If so then,
1764118Sjchu 		 * we should be receiving it during wait4L1 stage.
1765118Sjchu 		 */
1766118Sjchu 		px_p->px_pmetoack_ignored++;
1767118Sjchu 	}
1768118Sjchu 	mutex_exit(&px_p->px_l23ready_lock);
1769118Sjchu 	return (DDI_INTR_CLAIMED);
1770118Sjchu }
1771118Sjchu 
1772118Sjchu static int
1773118Sjchu px_pre_pwron_check(px_t *px_p)
1774118Sjchu {
1775118Sjchu 	pcie_pwr_t	*pwr_p;
1776118Sjchu 
1777118Sjchu 	/* If no PM info, return failure */
1778118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1779118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1780118Sjchu 		return (DDI_FAILURE);
1781118Sjchu 
1782287Smg140465 	/*
1783287Smg140465 	 * For the spec compliant downstream cards link down
1784287Smg140465 	 * is expected when the device is powered on.
1785287Smg140465 	 */
1786287Smg140465 	px_p->px_pm_flags |= PX_LDN_EXPECTED;
1787118Sjchu 	return (pwr_p->pwr_link_lvl == PM_LEVEL_L3 ? DDI_SUCCESS : DDI_FAILURE);
1788118Sjchu }
1789118Sjchu 
1790118Sjchu static int
1791118Sjchu px_goto_l0(px_t *px_p)
1792118Sjchu {
1793118Sjchu 	pcie_pwr_t	*pwr_p;
1794118Sjchu 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1795118Sjchu 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
1796118Sjchu 	int		ret = DDI_SUCCESS;
1797118Sjchu 	clock_t		end, timeleft;
1798118Sjchu 	int		mutex_held = 1;
1799118Sjchu 
1800118Sjchu 	/* If no PM info, return failure */
1801118Sjchu 	if (!PCIE_PMINFO(px_p->px_dip) ||
1802118Sjchu 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1803118Sjchu 		return (DDI_FAILURE);
1804118Sjchu 
1805118Sjchu 	mutex_enter(&pwr_p->pwr_lock);
1806118Sjchu 	mutex_enter(&px_p->px_lupsoft_lock);
1807118Sjchu 	/* Clear the LINKUP_RECVD receieved flag */
1808118Sjchu 	px_p->px_pm_flags &= ~PX_LINKUP_RECVD;
1809287Smg140465 	/*
1810287Smg140465 	 * Set flags LUP_EXPECTED to inform FMA code that LUP is
1811287Smg140465 	 * expected as part of link training and no ereports should
1812287Smg140465 	 * be posted for this event. FMA code will clear this flag
1813287Smg140465 	 * after one instance of this event. In case of P25, there
1814287Smg140465 	 * will not be a LDN event. So clear the flag set at PRE_PWRON
1815287Smg140465 	 * time.
1816287Smg140465 	 */
1817287Smg140465 	px_p->px_pm_flags |=  PX_LUP_EXPECTED;
1818287Smg140465 	px_p->px_pm_flags &= ~PX_LDN_EXPECTED;
1819118Sjchu 	if (px_link_retrain(csr_base) != DDI_SUCCESS) {
1820118Sjchu 		ret = DDI_FAILURE;
1821118Sjchu 		goto l0_done;
1822118Sjchu 	}
1823118Sjchu 	px_p->px_pm_flags |= PX_LINKUP_PENDING;
1824118Sjchu 
1825118Sjchu 	end = ddi_get_lbolt() + drv_usectohz(px_linkup_timeout);
1826118Sjchu 	while (!(px_p->px_pm_flags & PX_LINKUP_RECVD)) {
1827118Sjchu 		timeleft = cv_timedwait(&px_p->px_lup_cv,
1828118Sjchu 		    &px_p->px_lupsoft_lock, end);
1829118Sjchu 		/*
1830118Sjchu 		 * if cv_timedwait returns -1, it is either
1831118Sjchu 		 * 1) timed out or
1832118Sjchu 		 * 2) there was a pre-mature wakeup but by the time
1833118Sjchu 		 * cv_timedwait is called again end < lbolt i.e.
1834118Sjchu 		 * end is in the past.
1835118Sjchu 		 * 3) By the time we make first cv_timedwait call,
1836118Sjchu 		 * end < lbolt is true.
1837118Sjchu 		 */
1838118Sjchu 		if (timeleft == -1)
1839118Sjchu 			break;
1840118Sjchu 	}
1841118Sjchu 	if (!(px_p->px_pm_flags & PX_LINKUP_RECVD)) {
1842118Sjchu 		/*
1843118Sjchu 		 * Either timedout or interrupt didn't get a
1844118Sjchu 		 * chance to grab the mutex and set the flag.
1845118Sjchu 		 * release the mutex and delay for sometime.
1846118Sjchu 		 * This will 1) give a chance for interrupt to
1847118Sjchu 		 * set the flag 2) creates a delay between two
1848118Sjchu 		 * consequetive requests.
1849118Sjchu 		 */
1850118Sjchu 		mutex_exit(&px_p->px_lupsoft_lock);
1851118Sjchu 		mutex_held = 0;
1852287Smg140465 		delay(drv_usectohz(50 * MSEC_TO_USEC));
1853118Sjchu 		if (!(px_p->px_pm_flags & PX_LINKUP_RECVD)) {
1854118Sjchu 			ret = DDI_FAILURE;
1855118Sjchu 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
1856118Sjchu 			    " for link up\n");
1857118Sjchu 		}
1858118Sjchu 	}
1859287Smg140465 	px_p->px_pm_flags &=
1860287Smg140465 	    ~(PX_LINKUP_PENDING | PX_LINKUP_RECVD | PX_LUP_EXPECTED);
1861118Sjchu 
1862118Sjchu l0_done:
1863118Sjchu 	if (mutex_held)
1864118Sjchu 		mutex_exit(&px_p->px_lupsoft_lock);
1865287Smg140465 	px_enable_detect_quiet(csr_base);
1866118Sjchu 	if (ret == DDI_SUCCESS)
1867287Smg140465 		pwr_p->pwr_link_lvl = PM_LEVEL_L0;
1868118Sjchu 	mutex_exit(&pwr_p->pwr_lock);
1869118Sjchu 	return (ret);
1870118Sjchu }
1871118Sjchu 
1872118Sjchu uint_t
1873118Sjchu px_lup_softintr(caddr_t arg)
1874118Sjchu {
1875118Sjchu 	px_t *px_p = (px_t *)arg;
1876118Sjchu 
1877287Smg140465 	DBG(DBG_PWR, px_p->px_dip, " Link up soft interrupt received \n");
1878118Sjchu 	mutex_enter(&px_p->px_lup_lock);
1879118Sjchu 	if (!(px_p->px_lupsoft_pending > 0)) {
1880118Sjchu 		/* Spurious */
1881118Sjchu 		mutex_exit(&px_p->px_lup_lock);
1882118Sjchu 		return (DDI_INTR_UNCLAIMED);
1883118Sjchu 	}
1884118Sjchu 	px_p->px_lupsoft_pending--;
1885118Sjchu 	if (px_p->px_lupsoft_pending > 0) {
1886118Sjchu 		/* More than one lup soft intr posted - unlikely */
1887118Sjchu 		mutex_exit(&px_p->px_lup_lock);
1888118Sjchu 		return (DDI_INTR_UNCLAIMED);
1889118Sjchu 	}
1890118Sjchu 	mutex_exit(&px_p->px_lup_lock);
1891118Sjchu 
1892118Sjchu 	mutex_enter(&px_p->px_lupsoft_lock);
1893118Sjchu 	cv_broadcast(&px_p->px_lup_cv);
1894118Sjchu 	if (px_p->px_pm_flags & PX_LINKUP_PENDING) {
1895118Sjchu 		px_p->px_pm_flags |= PX_LINKUP_RECVD;
1896118Sjchu 	} else {
1897118Sjchu 		/* Nobody waiting for this! */
1898118Sjchu 		px_p->px_lup_ignored++;
1899118Sjchu 	}
1900118Sjchu 	mutex_exit(&px_p->px_lupsoft_lock);
1901118Sjchu 	return (DDI_INTR_CLAIMED);
1902118Sjchu }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate /*
19050Sstevel@tonic-gate  * Extract the drivers binding name to identify which chip we're binding to.
19060Sstevel@tonic-gate  * Whenever a new bus bridge is created, the driver alias entry should be
19070Sstevel@tonic-gate  * added here to identify the device if needed.  If a device isn't added,
19080Sstevel@tonic-gate  * the identity defaults to PX_CHIP_UNIDENTIFIED.
19090Sstevel@tonic-gate  */
19100Sstevel@tonic-gate static uint32_t
19110Sstevel@tonic-gate px_identity_chip(px_t *px_p)
19120Sstevel@tonic-gate {
19130Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
19140Sstevel@tonic-gate 	char		*name = ddi_binding_name(dip);
19150Sstevel@tonic-gate 	uint32_t	revision = 0;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	revision = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
19180Sstevel@tonic-gate 	    "module-revision#", 0);
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	/* Check for Fire driver binding name */
1921226Set142600 	if ((strcmp(name, "pci108e,80f0") == 0) ||
1922226Set142600 	    (strcmp(name, "pciex108e,80f0") == 0)) {
19230Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "px_identity_chip: %s%d: "
19240Sstevel@tonic-gate 		    "name %s module-revision %d\n", ddi_driver_name(dip),
19250Sstevel@tonic-gate 		    ddi_get_instance(dip), name, revision);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 		return (PX_CHIP_ID(PX_CHIP_FIRE, revision, 0x00));
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "%s%d: Unknown PCI Express Host bridge %s %x\n",
19310Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), name, revision);
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	return (PX_CHIP_UNIDENTIFIED);
19340Sstevel@tonic-gate }
193527Sjchu 
193627Sjchu int
193727Sjchu px_err_add_intr(px_fault_t *px_fault_p)
193827Sjchu {
193927Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
194027Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
194127Sjchu 
194227Sjchu 	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
194327Sjchu 		px_fault_p->px_err_func, (caddr_t)px_fault_p, NULL) == 0);
194427Sjchu 
194527Sjchu 	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);
194627Sjchu 
194727Sjchu 	return (DDI_SUCCESS);
194827Sjchu }
194927Sjchu 
195027Sjchu void
195127Sjchu px_err_rem_intr(px_fault_t *px_fault_p)
195227Sjchu {
195327Sjchu 	dev_info_t	*dip = px_fault_p->px_fh_dip;
195427Sjchu 	px_t		*px_p = DIP_TO_STATE(dip);
195527Sjchu 
195627Sjchu 	rem_ivintr(px_fault_p->px_fh_sysino, NULL);
195727Sjchu 
195827Sjchu 	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
195927Sjchu 		IB_INTR_WAIT);
196027Sjchu }
196127Sjchu 
196227Sjchu #ifdef FMA
196327Sjchu void
196427Sjchu px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
196527Sjchu {
196627Sjchu 	/* populate the rc_status by reading the registers - TBD */
196727Sjchu }
196827Sjchu #endif /* FMA */
1969*383Set142600 
1970*383Set142600 /*
1971*383Set142600  * Unprotected raw reads/writes of fabric device's config space.
1972*383Set142600  * Only used for temporary PCI-E Fabric Error Handling.
1973*383Set142600  */
1974*383Set142600 uint32_t
1975*383Set142600 px_fab_get(px_t *px_p, pcie_req_id_t bdf, uint16_t offset) {
1976*383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
1977*383Set142600 	uint64_t	range_prop, base_addr;
1978*383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
1979*383Set142600 	uint32_t	val;
1980*383Set142600 
1981*383Set142600 	/* Get Fire's Physical Base Address */
1982*383Set142600 	range_prop = (((uint64_t)(rp[bank].parent_high & 0x7ff)) << 32) |
1983*383Set142600 	    rp[bank].parent_low;
1984*383Set142600 
1985*383Set142600 	/* Get config space first. */
1986*383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
1987*383Set142600 
1988*383Set142600 	val = ldphysio(base_addr);
1989*383Set142600 
1990*383Set142600 	return (LE_32(val));
1991*383Set142600 }
1992*383Set142600 
1993*383Set142600 void
1994*383Set142600 px_fab_set(px_t *px_p, pcie_req_id_t bdf, uint16_t offset,
1995*383Set142600     uint32_t val) {
1996*383Set142600 	px_ranges_t	*rp = px_p->px_ranges_p;
1997*383Set142600 	uint64_t	range_prop, base_addr;
1998*383Set142600 	int		bank = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
1999*383Set142600 
2000*383Set142600 	/* Get Fire's Physical Base Address */
2001*383Set142600 	range_prop = (((uint64_t)(rp[bank].parent_high & 0x7ff)) << 32) |
2002*383Set142600 	    rp[bank].parent_low;
2003*383Set142600 
2004*383Set142600 	/* Get config space first. */
2005*383Set142600 	base_addr = range_prop + PX_BDF_TO_CFGADDR(bdf, offset);
2006*383Set142600 
2007*383Set142600 	stphysio(base_addr, LE_32(val));
2008*383Set142600 }
2009