xref: /onnv-gate/usr/src/uts/common/io/pci_intr_lib.c (revision 1624:16ffeb79fcfc)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1624Spjha  * Common Development and Distribution License (the "License").
6*1624Spjha  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1624Spjha  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Support for MSI, MSIX and INTx
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/pci.h>
35*1624Spjha #include <sys/pci_cap.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/bitmap.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
40965Sgovinda  * MSI-X BIR Index Table:
41965Sgovinda  *
42965Sgovinda  * BAR indicator register (BIR) to Base Address register.
43965Sgovinda  */
44965Sgovinda static	uchar_t pci_msix_bir_index[8] = {0x10, 0x14, 0x18, 0x1c,
45965Sgovinda 					0x20, 0x24, 0xff, 0xff};
46965Sgovinda 
47965Sgovinda /*
480Sstevel@tonic-gate  * Library utility functions
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * pci_check_pciex:
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * check whether the device has PCI-E capability
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate int
580Sstevel@tonic-gate pci_check_pciex(dev_info_t *dip)
590Sstevel@tonic-gate {
60*1624Spjha 	ddi_acc_handle_t h;
61*1624Spjha 	ushort_t cap_off;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_check_pciex: dip: 0x%p, driver: %s, "
640Sstevel@tonic-gate 	    "binding: %s, nodename: %s\n", (void *)dip, ddi_driver_name(dip),
650Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_node_name(dip)));
660Sstevel@tonic-gate 
67*1624Spjha 	if (pci_config_setup(dip, &h) != DDI_SUCCESS) {
680Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_check_pciex: "
690Sstevel@tonic-gate 		    "pci_config_setup() failed\n"));
700Sstevel@tonic-gate 		return (DDI_FAILURE);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
73*1624Spjha 	if ((PCI_CAP_LOCATE(h, PCI_CAP_ID_PCI_E, &cap_off))
74*1624Spjha 			== DDI_SUCCESS) {
75*1624Spjha 		pci_config_teardown(&h);
76*1624Spjha 		return (DDI_SUCCESS);
770Sstevel@tonic-gate 	}
78*1624Spjha 
79*1624Spjha 	pci_config_teardown(&h);
800Sstevel@tonic-gate 	return (DDI_FAILURE);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * pci_get_msi_ctrl:
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *	Helper function that returns with 'cfg_hdl', MSI/X ctrl pointer,
880Sstevel@tonic-gate  *	and caps_ptr for MSI/X if these are found.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate static int
910Sstevel@tonic-gate pci_get_msi_ctrl(dev_info_t *dip, int type, ushort_t *msi_ctrl,
92*1624Spjha     ushort_t *caps_ptr, ddi_acc_handle_t *h)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	*msi_ctrl = *caps_ptr = 0;
950Sstevel@tonic-gate 
96*1624Spjha 	if (pci_config_setup(dip, h) != DDI_SUCCESS) {
970Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_get_msi_ctrl: "
98*1624Spjha 		    "%s%d can't get config handle",
990Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip)));
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		return (DDI_FAILURE);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
104*1624Spjha 	if ((PCI_CAP_LOCATE(*h, PCI_CAP_ID_MSI, caps_ptr) == DDI_SUCCESS) &&
105*1624Spjha 		(type == DDI_INTR_TYPE_MSI)) {
106*1624Spjha 		if ((*msi_ctrl = PCI_CAP_GET16(*h, NULL, *caps_ptr,
107*1624Spjha 			PCI_MSI_CTRL)) == DDI_FAILURE)
108*1624Spjha 			goto done;
1090Sstevel@tonic-gate 
110*1624Spjha 		DDI_INTR_NEXDBG((CE_CONT, "pci_get_msi_ctrl: MSI "
111*1624Spjha 		    "caps_ptr=%x msi_ctrl=%x\n", *caps_ptr, *msi_ctrl));
112*1624Spjha 
113*1624Spjha 		return (DDI_SUCCESS);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
116*1624Spjha 	if ((PCI_CAP_LOCATE(*h, PCI_CAP_ID_MSI_X, caps_ptr) == DDI_SUCCESS) &&
117*1624Spjha 		(type == DDI_INTR_TYPE_MSIX)) {
118*1624Spjha 		if ((*msi_ctrl = PCI_CAP_GET16(*h, NULL, *caps_ptr,
119*1624Spjha 			PCI_MSIX_CTRL)) == DDI_FAILURE)
120*1624Spjha 			goto done;
1210Sstevel@tonic-gate 
122*1624Spjha 		DDI_INTR_NEXDBG((CE_CONT, "pci_get_msi_ctrl: MSI-X "
123*1624Spjha 		    "caps_ptr=%x msi_ctrl=%x\n", *caps_ptr, *msi_ctrl));
1240Sstevel@tonic-gate 
125*1624Spjha 		return (DDI_SUCCESS);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
128*1624Spjha done:
129*1624Spjha 	pci_config_teardown(h);
1300Sstevel@tonic-gate 	return (DDI_FAILURE);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * pci_msi_get_cap:
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * Get the capabilities of the MSI/X interrupt
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate int
1400Sstevel@tonic-gate pci_msi_get_cap(dev_info_t *rdip, int type, int *flagsp)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
1430Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_cap: rdip = 0x%p\n",
1460Sstevel@tonic-gate 	    (void *)rdip));
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	*flagsp = 0;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
1510Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
1520Sstevel@tonic-gate 		return (DDI_FAILURE);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
1550Sstevel@tonic-gate 		if (msi_ctrl &  PCI_MSI_64BIT_MASK)
1560Sstevel@tonic-gate 			*flagsp |= DDI_INTR_FLAG_MSI64;
1570Sstevel@tonic-gate 		if (msi_ctrl & PCI_MSI_PVM_MASK)
1580Sstevel@tonic-gate 			*flagsp |= (DDI_INTR_FLAG_MASKABLE |
1590Sstevel@tonic-gate 			    DDI_INTR_FLAG_PENDING);
1600Sstevel@tonic-gate 		else
1610Sstevel@tonic-gate 			*flagsp |= DDI_INTR_FLAG_BLOCK;
1620Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
1630Sstevel@tonic-gate 		/* MSI-X supports PVM, 64bit by default */
1640Sstevel@tonic-gate 		*flagsp |= (DDI_INTR_FLAG_MASKABLE | DDI_INTR_FLAG_MSI64 |
1650Sstevel@tonic-gate 		    DDI_INTR_FLAG_PENDING);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	*flagsp |= DDI_INTR_FLAG_EDGE;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_cap: flags = 0x%x\n", *flagsp));
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
1730Sstevel@tonic-gate 	return (DDI_SUCCESS);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * pci_msi_configure:
1790Sstevel@tonic-gate  *
1800Sstevel@tonic-gate  * Configure address/data and number MSI/Xs fields in the MSI/X
1810Sstevel@tonic-gate  * capability structure.
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate /* ARGSUSED */
1840Sstevel@tonic-gate int
1850Sstevel@tonic-gate pci_msi_configure(dev_info_t *rdip, int type, int count, int inum,
1860Sstevel@tonic-gate     uint64_t addr, uint64_t data)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
189*1624Spjha 	ddi_acc_handle_t	h;
1900Sstevel@tonic-gate 
191965Sgovinda 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: rdip = 0x%p type 0x%x "
192965Sgovinda 	    "count 0x%x inum 0x%x addr 0x%" PRIx64 " data 0x%" PRIx64 "\n",
193965Sgovinda 	    (void *)rdip, type, count, inum, addr, data));
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
196*1624Spjha 	    &caps_ptr, &h) != DDI_SUCCESS)
1970Sstevel@tonic-gate 		return (DDI_FAILURE);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
2000Sstevel@tonic-gate 		/* Set the bits to inform how many MSIs are enabled */
2010Sstevel@tonic-gate 		msi_ctrl |= ((highbit(count) -1) << PCI_MSI_MME_SHIFT);
202*1624Spjha 		PCI_CAP_PUT16(h, NULL, caps_ptr, PCI_MSI_CTRL, msi_ctrl);
2030Sstevel@tonic-gate 
204965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: msi_ctrl = %x\n",
205*1624Spjha 		    PCI_CAP_GET16(h, NULL, caps_ptr, PCI_MSI_CTRL)));
206965Sgovinda 
2070Sstevel@tonic-gate 		/* Set the "data" and "addr" bits */
208*1624Spjha 		PCI_CAP_PUT32(h, NULL, caps_ptr, PCI_MSI_ADDR_OFFSET, addr);
2090Sstevel@tonic-gate 
210965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: msi_addr = %x\n",
211*1624Spjha 			PCI_CAP_GET32(h, NULL, caps_ptr, PCI_MSI_ADDR_OFFSET)));
212965Sgovinda 
2130Sstevel@tonic-gate 		if (msi_ctrl &  PCI_MSI_64BIT_MASK) {
214*1624Spjha 			PCI_CAP_PUT32(h, NULL, caps_ptr, PCI_MSI_ADDR_OFFSET
215*1624Spjha 				+ 4, addr >> 32);
216965Sgovinda 
217*1624Spjha 			DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: upper "
218*1624Spjha 				"32bit msi_addr = %x\n", PCI_CAP_GET32(h, NULL,
219*1624Spjha 				caps_ptr, PCI_MSI_ADDR_OFFSET + 4)));
220965Sgovinda 
221*1624Spjha 			PCI_CAP_PUT16(h, NULL, caps_ptr, PCI_MSI_64BIT_DATA,
222*1624Spjha 				data);
223965Sgovinda 
224*1624Spjha 			DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: msi_data "
225*1624Spjha 				"= %x\n", PCI_CAP_GET16(h, NULL, caps_ptr,
226*1624Spjha 				PCI_MSI_64BIT_DATA)));
2270Sstevel@tonic-gate 		} else {
228*1624Spjha 			PCI_CAP_PUT16(h, NULL, caps_ptr, PCI_MSI_32BIT_DATA,
229*1624Spjha 				data);
230965Sgovinda 
231*1624Spjha 			DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: msi_data "
232*1624Spjha 				"= %x\n", PCI_CAP_GET16(h, NULL, caps_ptr,
233*1624Spjha 				PCI_MSI_32BIT_DATA)));
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
23642Sagiri 		uintptr_t	off;
2370Sstevel@tonic-gate 		ddi_intr_msix_t	*msix_p = i_ddi_get_msix(rdip);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		/* Offset into the "inum"th entry in the MSI-X table */
24042Sagiri 		off = (uintptr_t)msix_p->msix_tbl_addr +
241965Sgovinda 		    (inum * PCI_MSIX_VECTOR_SIZE);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		/* Set the "data" and "addr" bits */
2440Sstevel@tonic-gate 		ddi_put32(msix_p->msix_tbl_hdl,
245965Sgovinda 		    (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), data);
246965Sgovinda 
247965Sgovinda 		ddi_put64(msix_p->msix_tbl_hdl,
248965Sgovinda 		    (uint64_t *)(off + PCI_MSIX_LOWER_ADDR_OFFSET), addr);
2490Sstevel@tonic-gate 
250965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msi_configure: "
251965Sgovinda 		    "msix_addr 0x%" PRIx64 " msix_data 0x%x\n",
252965Sgovinda 		    ddi_get64(msix_p->msix_tbl_hdl,
253965Sgovinda 		    (uint64_t *)(off + PCI_MSIX_LOWER_ADDR_OFFSET)),
254965Sgovinda 		    ddi_get32(msix_p->msix_tbl_hdl,
255965Sgovinda 		    (uint32_t *)(off + PCI_MSIX_DATA_OFFSET))));
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
258*1624Spjha 	pci_config_teardown(&h);
2590Sstevel@tonic-gate 	return (DDI_SUCCESS);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * pci_msi_unconfigure:
2650Sstevel@tonic-gate  *
2660Sstevel@tonic-gate  * Unconfigure address/data and number MSI/Xs fields in the MSI/X
2670Sstevel@tonic-gate  * capability structure.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate /* ARGSUSED */
2700Sstevel@tonic-gate int
2710Sstevel@tonic-gate pci_msi_unconfigure(dev_info_t *rdip, int type, int inum)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	ushort_t		msi_ctrl, caps_ptr;
274*1624Spjha 	ddi_acc_handle_t	h;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_unconfigure: rdip = 0x%p\n",
2770Sstevel@tonic-gate 	    (void *)rdip));
2780Sstevel@tonic-gate 
279*1624Spjha 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl, &caps_ptr, &h) !=
280*1624Spjha 		DDI_SUCCESS)
2810Sstevel@tonic-gate 		return (DDI_FAILURE);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
2840Sstevel@tonic-gate 		msi_ctrl &= (~PCI_MSI_MME_MASK);
285*1624Spjha 		PCI_CAP_PUT16(h, NULL, caps_ptr, PCI_MSI_CTRL, msi_ctrl);
2860Sstevel@tonic-gate 
287*1624Spjha 		PCI_CAP_PUT32(h, NULL, caps_ptr, PCI_MSI_ADDR_OFFSET, 0);
288*1624Spjha 
2890Sstevel@tonic-gate 		if (msi_ctrl &  PCI_MSI_64BIT_MASK) {
290*1624Spjha 			PCI_CAP_PUT16(h, NULL, caps_ptr, PCI_MSI_64BIT_DATA,
291*1624Spjha 				0);
292*1624Spjha 			PCI_CAP_PUT32(h, NULL, caps_ptr, PCI_MSI_ADDR_OFFSET
293*1624Spjha 				+ 4, 0);
2940Sstevel@tonic-gate 		} else {
295*1624Spjha 			PCI_CAP_PUT16(h, NULL, caps_ptr, PCI_MSI_32BIT_DATA,
296*1624Spjha 				0);
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 
299*1624Spjha 		DDI_INTR_NEXDBG((CE_CONT, "pci_msi_unconfigure: msi_ctrl "
300*1624Spjha 		    "= %x\n", PCI_CAP_GET16(h, NULL, caps_ptr, PCI_MSI_CTRL)));
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
30342Sagiri 		uintptr_t	off;
3040Sstevel@tonic-gate 		ddi_intr_msix_t	*msix_p = i_ddi_get_msix(rdip);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		/* Offset into the "inum"th entry in the MSI-X table */
30742Sagiri 		off = (uintptr_t)msix_p->msix_tbl_addr +
308965Sgovinda 		    (inum * PCI_MSIX_VECTOR_SIZE);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		/* Reset the "data" and "addr" bits */
3110Sstevel@tonic-gate 		ddi_put32(msix_p->msix_tbl_hdl,
312965Sgovinda 		    (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), 0);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		ddi_put64(msix_p->msix_tbl_hdl, (uint64_t *)off, 0);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
317*1624Spjha 	pci_config_teardown(&h);
3180Sstevel@tonic-gate 	return (DDI_SUCCESS);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  * pci_is_msi_enabled:
3240Sstevel@tonic-gate  *
3250Sstevel@tonic-gate  * This function returns DDI_SUCCESS if MSI/X is already enabled, otherwise
3260Sstevel@tonic-gate  * it returns DDI_FAILURE.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate int
3290Sstevel@tonic-gate pci_is_msi_enabled(dev_info_t *rdip, int type)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
3320Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
3330Sstevel@tonic-gate 	int			ret = DDI_FAILURE;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_is_msi_enabled: rdip = 0x%p, "
3360Sstevel@tonic-gate 	    "type  = 0x%x\n", (void *)rdip, type));
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
3390Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
3400Sstevel@tonic-gate 		return (DDI_FAILURE);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if ((type == DDI_INTR_TYPE_MSI) && (msi_ctrl & PCI_MSI_ENABLE_BIT))
3430Sstevel@tonic-gate 		ret = DDI_SUCCESS;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if ((type == DDI_INTR_TYPE_MSIX) && (msi_ctrl & PCI_MSIX_ENABLE_BIT))
3460Sstevel@tonic-gate 		ret = DDI_SUCCESS;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
3490Sstevel@tonic-gate 	return (ret);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * pci_msi_enable_mode:
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * This function sets the MSI_ENABLE bit in the capability structure
3570Sstevel@tonic-gate  * (for MSI) and MSIX_ENABLE bit in the MSI-X capability structure.
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate int
3600Sstevel@tonic-gate pci_msi_enable_mode(dev_info_t *rdip, int type, int inum)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
3630Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_enable_mode: rdip = 0x%p, "
3660Sstevel@tonic-gate 	    "inum  = 0x%x\n", (void *)rdip, inum));
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
3690Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
3700Sstevel@tonic-gate 		return (DDI_FAILURE);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
3730Sstevel@tonic-gate 		if (msi_ctrl & PCI_MSI_ENABLE_BIT)
3740Sstevel@tonic-gate 			goto finished;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		msi_ctrl |= PCI_MSI_ENABLE_BIT;
377*1624Spjha 		PCI_CAP_PUT16(cfg_hdle, NULL, caps_ptr, PCI_MSI_CTRL, msi_ctrl);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
38042Sagiri 		uintptr_t	off;
3810Sstevel@tonic-gate 		ddi_intr_msix_t	*msix_p;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		if (msi_ctrl & PCI_MSIX_ENABLE_BIT)
3840Sstevel@tonic-gate 			goto finished;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		msi_ctrl |= PCI_MSIX_ENABLE_BIT;
387*1624Spjha 		PCI_CAP_PUT16(cfg_hdle, NULL, caps_ptr, PCI_MSIX_CTRL,
388*1624Spjha 			msi_ctrl);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		msix_p = i_ddi_get_msix(rdip);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 		/* Offset into the "inum"th entry in the MSI-X table */
393965Sgovinda 		off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
3940Sstevel@tonic-gate 		    PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/* Clear the Mask bit */
397965Sgovinda 		ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, 0x0);
3980Sstevel@tonic-gate 
399965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msi_enable: "
400965Sgovinda 		    "msix_vector_mask 0x%x\n",
401965Sgovinda 		    ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off)));
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate finished:
4050Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_enable_mode: msi_ctrl = %x\n",
4060Sstevel@tonic-gate 	    msi_ctrl));
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
4090Sstevel@tonic-gate 	return (DDI_SUCCESS);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * pci_msi_disable_mode:
4150Sstevel@tonic-gate  *
4160Sstevel@tonic-gate  * This function resets the MSI_ENABLE bit in the capability structure
4170Sstevel@tonic-gate  * (for MSI) and MSIX_ENABLE bit in the MSI-X capability structure.
4180Sstevel@tonic-gate  */
4190Sstevel@tonic-gate int
4200Sstevel@tonic-gate pci_msi_disable_mode(dev_info_t *rdip, int type, int inum)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
4230Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_disable_mode: rdip = 0x%p "
4260Sstevel@tonic-gate 	    "inum = 0x%x\n", (void *)rdip, inum));
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
4290Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
4300Sstevel@tonic-gate 		return (DDI_FAILURE);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/* Reset the "enable" bit */
4330Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
4340Sstevel@tonic-gate 		if (!(msi_ctrl & PCI_MSI_ENABLE_BIT))
4350Sstevel@tonic-gate 			goto finished;
4360Sstevel@tonic-gate 		msi_ctrl &= ~PCI_MSI_ENABLE_BIT;
437*1624Spjha 		PCI_CAP_PUT16(cfg_hdle, NULL, caps_ptr, PCI_MSI_CTRL, msi_ctrl);
4380Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
43942Sagiri 		uintptr_t		off;
4400Sstevel@tonic-gate 		ddi_intr_msix_t		*msix_p;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		if (!(msi_ctrl & PCI_MSIX_ENABLE_BIT))
4430Sstevel@tonic-gate 			goto finished;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		msix_p = i_ddi_get_msix(rdip);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		/* Offset into the "inum"th entry in the MSI-X table */
448965Sgovinda 		off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
4490Sstevel@tonic-gate 		    PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		/* Set the Mask bit */
452965Sgovinda 		ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, 0x1);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate finished:
4560Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_disable_mode: msi_ctrl = %x\n",
4570Sstevel@tonic-gate 	    msi_ctrl));
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
4600Sstevel@tonic-gate 	return (DDI_SUCCESS);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * pci_msi_set_mask:
4660Sstevel@tonic-gate  *
4670Sstevel@tonic-gate  * Set the mask bit in the MSI/X capability structure
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate /* ARGSUSED */
4700Sstevel@tonic-gate int
4710Sstevel@tonic-gate pci_msi_set_mask(dev_info_t *rdip, int type, int inum)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	int			offset;
4740Sstevel@tonic-gate 	int			ret = DDI_FAILURE;
4750Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
4760Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
477965Sgovinda 	uint32_t		mask_bits;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_set_mask: rdip = 0x%p, "
4800Sstevel@tonic-gate 	    "type = 0x%x\n", (void *)rdip, type));
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
4830Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
4840Sstevel@tonic-gate 		return (DDI_FAILURE);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
4870Sstevel@tonic-gate 		if (!(msi_ctrl &  PCI_MSI_PVM_MASK))
4880Sstevel@tonic-gate 			goto done;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		offset = (msi_ctrl &  PCI_MSI_64BIT_MASK) ?
4910Sstevel@tonic-gate 		    PCI_MSI_64BIT_MASKBITS : PCI_MSI_32BIT_MASK;
4920Sstevel@tonic-gate 
493*1624Spjha 		if ((mask_bits = PCI_CAP_GET32(cfg_hdle, NULL, caps_ptr,
494*1624Spjha 			offset)) == DDI_FAILURE)
495*1624Spjha 			goto done;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		mask_bits |= (1 << inum);
4980Sstevel@tonic-gate 
499*1624Spjha 		PCI_CAP_PUT32(cfg_hdle, NULL, caps_ptr, offset, mask_bits);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
50242Sagiri 		uintptr_t		off;
5030Sstevel@tonic-gate 		ddi_intr_msix_t		*msix_p;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		/* Set function mask */
5060Sstevel@tonic-gate 		if (msi_ctrl & PCI_MSIX_FUNCTION_MASK) {
5070Sstevel@tonic-gate 			ret = DDI_SUCCESS;
5080Sstevel@tonic-gate 			goto done;
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		msix_p = i_ddi_get_msix(rdip);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		/* Offset into the "inum"th entry in the MSI-X table */
514965Sgovinda 		off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
5150Sstevel@tonic-gate 		    PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		/* Set the Mask bit */
518965Sgovinda 		ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, 0x1);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	ret = DDI_SUCCESS;
5220Sstevel@tonic-gate done:
5230Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
5240Sstevel@tonic-gate 	return (ret);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate  * pci_msi_clr_mask:
5300Sstevel@tonic-gate  *
5310Sstevel@tonic-gate  * Clear the mask bit in the MSI/X capability structure
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate /* ARGSUSED */
5340Sstevel@tonic-gate int
5350Sstevel@tonic-gate pci_msi_clr_mask(dev_info_t *rdip, int type, int inum)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
5380Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
5390Sstevel@tonic-gate 	int			offset;
5400Sstevel@tonic-gate 	int			ret = DDI_FAILURE;
541965Sgovinda 	uint32_t		mask_bits;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_clr_mask: rdip = 0x%p, "
5440Sstevel@tonic-gate 	    "type = 0x%x\n", (void *)rdip, type));
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
5470Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
5480Sstevel@tonic-gate 		return (DDI_FAILURE);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
5510Sstevel@tonic-gate 		if (!(msi_ctrl &  PCI_MSI_PVM_MASK))
5520Sstevel@tonic-gate 			goto done;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		offset = (msi_ctrl &  PCI_MSI_64BIT_MASK) ?
5550Sstevel@tonic-gate 		    PCI_MSI_64BIT_MASKBITS : PCI_MSI_32BIT_MASK;
556*1624Spjha 		if ((mask_bits = PCI_CAP_GET32(cfg_hdle, NULL, caps_ptr,
557*1624Spjha 			offset)) == DDI_FAILURE)
558*1624Spjha 			goto done;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		mask_bits &= ~(1 << inum);
5610Sstevel@tonic-gate 
562*1624Spjha 		PCI_CAP_PUT32(cfg_hdle, NULL, caps_ptr, offset, mask_bits);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
56542Sagiri 		uintptr_t		off;
5660Sstevel@tonic-gate 		ddi_intr_msix_t		*msix_p;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 		if (msi_ctrl & PCI_MSIX_FUNCTION_MASK) {
5690Sstevel@tonic-gate 			ret = DDI_SUCCESS;
5700Sstevel@tonic-gate 			goto done;
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		msix_p = i_ddi_get_msix(rdip);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		/* Offset into the "inum"th entry in the MSI-X table */
576965Sgovinda 		off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
5770Sstevel@tonic-gate 		    PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		/* Clear the Mask bit */
580965Sgovinda 		ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, 0x0);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	ret = DDI_SUCCESS;
5840Sstevel@tonic-gate done:
5850Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
5860Sstevel@tonic-gate 	return (ret);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * pci_msi_get_pending:
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  * Get the pending bit from the MSI/X capability structure
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate /* ARGSUSED */
5960Sstevel@tonic-gate int
5970Sstevel@tonic-gate pci_msi_get_pending(dev_info_t *rdip, int type, int inum, int *pendingp)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
6000Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
6010Sstevel@tonic-gate 	int			offset;
6020Sstevel@tonic-gate 	int			ret = DDI_FAILURE;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_pending: rdip = 0x%p\n",
6050Sstevel@tonic-gate 	    (void *)rdip));
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
6080Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
6090Sstevel@tonic-gate 		return (DDI_FAILURE);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
6120Sstevel@tonic-gate 		uint32_t	pending_bits;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 		if (!(msi_ctrl &  PCI_MSI_PVM_MASK)) {
6150Sstevel@tonic-gate 			DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_pending: "
6160Sstevel@tonic-gate 			    "PVM is not supported\n"));
6170Sstevel@tonic-gate 			goto done;
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		offset = (msi_ctrl &  PCI_MSI_64BIT_MASK) ?
6210Sstevel@tonic-gate 		    PCI_MSI_64BIT_PENDING : PCI_MSI_32BIT_PENDING;
6220Sstevel@tonic-gate 
623*1624Spjha 		if ((pending_bits = PCI_CAP_GET32(cfg_hdle, NULL, caps_ptr,
624*1624Spjha 			offset)) == DDI_FAILURE)
625*1624Spjha 			goto done;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		*pendingp = pending_bits & ~(1 >> inum);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
63042Sagiri 		uintptr_t	off;
6310Sstevel@tonic-gate 		uint64_t	pending_bits;
6320Sstevel@tonic-gate 		ddi_intr_msix_t	*msix_p = i_ddi_get_msix(rdip);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		/* Offset into the PBA array which has entry for "inum" */
635965Sgovinda 		off = (uintptr_t)msix_p->msix_pba_addr + (inum / 64);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		/* Read the PBA array */
638965Sgovinda 		pending_bits = ddi_get64(msix_p->msix_pba_hdl, (uint64_t *)off);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		*pendingp = pending_bits & ~(1 >> inum);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	ret = DDI_SUCCESS;
6440Sstevel@tonic-gate done:
6450Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
6460Sstevel@tonic-gate 	return (ret);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * pci_msi_get_nintrs:
6520Sstevel@tonic-gate  *
6530Sstevel@tonic-gate  * For a given type (MSI/X) returns the number of interrupts supported
6540Sstevel@tonic-gate  */
6550Sstevel@tonic-gate int
6560Sstevel@tonic-gate pci_msi_get_nintrs(dev_info_t *rdip, int type, int *nintrs)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
6590Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_nintrs: rdip = 0x%p\n",
6620Sstevel@tonic-gate 	    (void *)rdip));
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
6650Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
6660Sstevel@tonic-gate 		return (DDI_FAILURE);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
6690Sstevel@tonic-gate 		*nintrs = 1 << ((msi_ctrl & PCI_MSI_MMC_MASK) >>
6700Sstevel@tonic-gate 		    PCI_MSI_MMC_SHIFT);
6710Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
6720Sstevel@tonic-gate 		if (msi_ctrl &  PCI_MSIX_TBL_SIZE_MASK)
6730Sstevel@tonic-gate 			*nintrs = (msi_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1;
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_nintrs: "
6770Sstevel@tonic-gate 	    "nintr = 0x%x\n", *nintrs));
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
6800Sstevel@tonic-gate 	return (DDI_SUCCESS);
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate  * pci_msi_set_nintrs:
6860Sstevel@tonic-gate  *
6870Sstevel@tonic-gate  * For a given type (MSI/X) sets the number of interrupts supported
6880Sstevel@tonic-gate  * by the system.
6890Sstevel@tonic-gate  * For MSI: Return an error if this func is called for navail > 32
6900Sstevel@tonic-gate  * For MSI-X: Return an error if this func is called for navail > 2048
6910Sstevel@tonic-gate  */
6920Sstevel@tonic-gate int
6930Sstevel@tonic-gate pci_msi_set_nintrs(dev_info_t *rdip, int type, int navail)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
6960Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_set_nintrs: rdip = 0x%p, "
6990Sstevel@tonic-gate 	    "navail = 0x%x\n", (void *)rdip, navail));
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/* Check for valid input argument */
7020Sstevel@tonic-gate 	if (((type == DDI_INTR_TYPE_MSI) && (navail > PCI_MSI_MAX_INTRS)) ||
7030Sstevel@tonic-gate 	    ((type == DDI_INTR_TYPE_MSIX) && (navail >  PCI_MSIX_MAX_INTRS)))
7040Sstevel@tonic-gate 		return (DDI_EINVAL);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, type, &msi_ctrl,
7070Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
7080Sstevel@tonic-gate 		return (DDI_FAILURE);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI) {
7110Sstevel@tonic-gate 		msi_ctrl |= ((highbit(navail) -1) << PCI_MSI_MME_SHIFT);
7120Sstevel@tonic-gate 
713*1624Spjha 		PCI_CAP_PUT16(cfg_hdle, NULL, caps_ptr, PCI_MSI_CTRL, msi_ctrl);
7140Sstevel@tonic-gate 	} else if (type == DDI_INTR_TYPE_MSIX) {
7150Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_msi_set_nintrs: unsupported\n"));
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
7190Sstevel@tonic-gate 	return (DDI_SUCCESS);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * pci_msi_get_supported_type:
7250Sstevel@tonic-gate  *
7260Sstevel@tonic-gate  * Returns DDI_INTR_TYPE_MSI and/or DDI_INTR_TYPE_MSIX as supported
7270Sstevel@tonic-gate  * types if device supports them. A DDI_FAILURE is returned otherwise.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate int
7300Sstevel@tonic-gate pci_msi_get_supported_type(dev_info_t *rdip, int *typesp)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	ushort_t		caps_ptr, msi_ctrl;
7330Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_supported_type: "
7360Sstevel@tonic-gate 	    "rdip = 0x%p\n", (void *)rdip));
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	*typesp = 0;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, DDI_INTR_TYPE_MSI, &msi_ctrl,
7410Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) == DDI_SUCCESS) {
7420Sstevel@tonic-gate 		*typesp |= DDI_INTR_TYPE_MSI;
7430Sstevel@tonic-gate 		pci_config_teardown(&cfg_hdle);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (pci_get_msi_ctrl(rdip, DDI_INTR_TYPE_MSIX, &msi_ctrl,
7470Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) == DDI_SUCCESS) {
7480Sstevel@tonic-gate 		*typesp |= DDI_INTR_TYPE_MSIX;
7490Sstevel@tonic-gate 		pci_config_teardown(&cfg_hdle);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msi_get_supported_type: "
7530Sstevel@tonic-gate 	    "rdip = 0x%p types 0x%x\n", (void *)rdip, *typesp));
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	return (*typesp == 0 ? DDI_FAILURE : DDI_SUCCESS);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * pci_msix_init:
7610Sstevel@tonic-gate  *	This function initializes the various handles/addrs etc.
7620Sstevel@tonic-gate  *	needed for MSI-X support. It also allocates a private
7630Sstevel@tonic-gate  *	structure to keep track of these.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate ddi_intr_msix_t *
7660Sstevel@tonic-gate pci_msix_init(dev_info_t *rdip)
7670Sstevel@tonic-gate {
768965Sgovinda 	uint_t			rnumber, breg, nregs;
7690Sstevel@tonic-gate 	size_t			msix_tbl_size;
7700Sstevel@tonic-gate 	size_t			pba_tbl_size;
771965Sgovinda 	ushort_t		caps_ptr, msix_ctrl;
7720Sstevel@tonic-gate 	ddi_intr_msix_t		*msix_p;
7730Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdle;
774965Sgovinda 	pci_regspec_t		*rp;
775965Sgovinda 	int			reg_size, addr_space, offset, *regs_list;
776965Sgovinda 	int			i, ret;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: rdip = %p\n", (void *)rdip));
7790Sstevel@tonic-gate 
780965Sgovinda 	if (pci_get_msi_ctrl(rdip, DDI_INTR_TYPE_MSIX, &msix_ctrl,
7810Sstevel@tonic-gate 	    &caps_ptr, &cfg_hdle) != DDI_SUCCESS)
7820Sstevel@tonic-gate 		return (NULL);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	msix_p = kmem_zalloc(sizeof (ddi_intr_msix_t), KM_SLEEP);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/*
7870Sstevel@tonic-gate 	 * Initialize the devacc structure
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	msix_p->msix_dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
7900Sstevel@tonic-gate 	msix_p->msix_dev_attr.devacc_attr_endian_flags =
7910Sstevel@tonic-gate 	    DDI_STRUCTURE_LE_ACC;
7920Sstevel@tonic-gate 	msix_p->msix_dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
7930Sstevel@tonic-gate 
794965Sgovinda 	/* Map the entire MSI-X vector table */
795*1624Spjha 	msix_p->msix_tbl_offset = PCI_CAP_GET32(cfg_hdle, NULL, caps_ptr,
796*1624Spjha 		PCI_MSIX_TBL_OFFSET);
797965Sgovinda 
798965Sgovinda 	if ((breg = pci_msix_bir_index[msix_p->msix_tbl_offset &
799965Sgovinda 	    PCI_MSIX_TBL_BIR_MASK]) == 0xff)
800965Sgovinda 		goto fail1;
801965Sgovinda 
802965Sgovinda 	msix_p->msix_tbl_offset = msix_p->msix_tbl_offset &
803965Sgovinda 	    ~PCI_MSIX_TBL_BIR_MASK;
804965Sgovinda 	msix_tbl_size = ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) *
805965Sgovinda 	    PCI_MSIX_VECTOR_SIZE;
8060Sstevel@tonic-gate 
807965Sgovinda 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: MSI-X table offset 0x%x "
808965Sgovinda 	    "breg 0x%x size 0x%lx\n", msix_p->msix_tbl_offset, breg,
809965Sgovinda 	    msix_tbl_size));
810965Sgovinda 
811965Sgovinda 	if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
812965Sgovinda 	    DDI_PROP_DONTPASS, "reg", (int **)&regs_list, &nregs))
813965Sgovinda 	    != DDI_PROP_SUCCESS) {
814965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: "
815965Sgovinda 		    "ddi_prop_lookup_int_array failed %d\n", ret));
816965Sgovinda 
817965Sgovinda 		goto fail1;
818965Sgovinda 	}
819965Sgovinda 
820965Sgovinda 	reg_size = sizeof (pci_regspec_t) / sizeof (int);
8210Sstevel@tonic-gate 
822965Sgovinda 	for (i = 1, rnumber = 0; i < nregs/reg_size; i++) {
823965Sgovinda 		rp = (pci_regspec_t *)&regs_list[i * reg_size];
824965Sgovinda 		addr_space = rp->pci_phys_hi & PCI_ADDR_MASK;
825965Sgovinda 		offset = PCI_REG_REG_G(rp->pci_phys_hi);
826965Sgovinda 
827965Sgovinda 		if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) ||
828965Sgovinda 		    (addr_space == PCI_ADDR_MEM64))) {
829965Sgovinda 			rnumber = i;
830965Sgovinda 			break;
831965Sgovinda 		}
832965Sgovinda 	}
833965Sgovinda 
834965Sgovinda 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: MSI-X rnum = %d\n", rnumber));
835965Sgovinda 
836965Sgovinda 	if (rnumber == 0) {
837965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: "
838965Sgovinda 		    "no mtaching reg number for offset 0x%x\n", breg));
839965Sgovinda 
840965Sgovinda 		goto fail2;
841965Sgovinda 	}
842965Sgovinda 
843965Sgovinda 	if ((ret = ddi_regs_map_setup(rdip, rnumber,
844965Sgovinda 	    (caddr_t *)&msix_p->msix_tbl_addr, msix_p->msix_tbl_offset,
845965Sgovinda 	    msix_tbl_size, &msix_p->msix_dev_attr,
846965Sgovinda 	    &msix_p->msix_tbl_hdl)) != DDI_SUCCESS) {
847965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: MSI-X Table "
848965Sgovinda 		    "ddi_regs_map_setup failed %d\n", ret));
849965Sgovinda 
850965Sgovinda 		goto fail2;
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/*
8540Sstevel@tonic-gate 	 * Map in the MSI-X Pending Bit Array
8550Sstevel@tonic-gate 	 */
856*1624Spjha 	msix_p->msix_pba_offset = PCI_CAP_GET32(cfg_hdle, NULL, caps_ptr,
857*1624Spjha 		PCI_MSIX_PBA_OFFSET);
858965Sgovinda 
859965Sgovinda 	if ((breg = pci_msix_bir_index[msix_p->msix_pba_offset &
860965Sgovinda 	    PCI_MSIX_PBA_BIR_MASK]) == 0xff)
861965Sgovinda 		goto fail3;
862965Sgovinda 
863965Sgovinda 	msix_p->msix_pba_offset = msix_p->msix_pba_offset &
864965Sgovinda 	    ~PCI_MSIX_PBA_BIR_MASK;
865965Sgovinda 	pba_tbl_size = ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1)/8;
866965Sgovinda 
867965Sgovinda 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: PBA table offset 0x%x "
868965Sgovinda 	    "breg 0x%x size 0x%lx\n", msix_p->msix_pba_offset, breg,
869965Sgovinda 	    pba_tbl_size));
870965Sgovinda 
871965Sgovinda 	for (i = 1, rnumber = 0; i < nregs/reg_size; i++) {
872965Sgovinda 		rp = (pci_regspec_t *)&regs_list[i * reg_size];
873965Sgovinda 		addr_space = rp->pci_phys_hi & PCI_ADDR_MASK;
874965Sgovinda 		offset = PCI_REG_REG_G(rp->pci_phys_hi);
8750Sstevel@tonic-gate 
876965Sgovinda 		if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) ||
877965Sgovinda 		    (addr_space == PCI_ADDR_MEM64))) {
878965Sgovinda 			ddi_prop_free(regs_list);
879965Sgovinda 			rnumber = i;
880965Sgovinda 			break;
881965Sgovinda 		}
882965Sgovinda 	}
883965Sgovinda 
884965Sgovinda 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: PBA rnum = %d\n", rnumber));
885965Sgovinda 
886965Sgovinda 	if (rnumber == 0) {
887965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: "
888965Sgovinda 		    "no mtaching reg number for offset 0x%x\n", breg));
889965Sgovinda 
890965Sgovinda 		goto fail3;
891965Sgovinda 	}
892965Sgovinda 
893965Sgovinda 	if ((ret = ddi_regs_map_setup(rdip, rnumber,
894965Sgovinda 	    (caddr_t *)&msix_p->msix_pba_addr, msix_p->msix_pba_offset,
895965Sgovinda 	    pba_tbl_size, &msix_p->msix_dev_attr,
896965Sgovinda 	    &msix_p->msix_pba_hdl)) != DDI_SUCCESS) {
897965Sgovinda 		DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: PBA "
898965Sgovinda 		    "ddi_regs_map_setup failed %d\n", ret));
899965Sgovinda 
900965Sgovinda 		goto fail3;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_init: msix_p = 0x%p DONE!!\n",
9040Sstevel@tonic-gate 	    (void *)msix_p));
9050Sstevel@tonic-gate 
906965Sgovinda 	goto done;
907965Sgovinda 
908965Sgovinda fail3:
909965Sgovinda 	ddi_regs_map_free(&msix_p->msix_tbl_hdl);
910965Sgovinda fail2:
911965Sgovinda 	ddi_prop_free(regs_list);
912965Sgovinda fail1:
913965Sgovinda 	kmem_free(msix_p, sizeof (ddi_intr_msix_t));
914965Sgovinda 	msix_p = NULL;
915965Sgovinda done:
9160Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdle);
9170Sstevel@tonic-gate 	return (msix_p);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*
9220Sstevel@tonic-gate  * pci_msix_fini:
9230Sstevel@tonic-gate  *	This function cleans up previously allocated handles/addrs etc.
9240Sstevel@tonic-gate  *	It is only called if no more MSI-X interrupts are being used.
9250Sstevel@tonic-gate  */
9260Sstevel@tonic-gate void
9270Sstevel@tonic-gate pci_msix_fini(ddi_intr_msix_t *msix_p)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_msix_fini: msix_p = 0x%p\n",
9300Sstevel@tonic-gate 	    (void *)msix_p));
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	ddi_regs_map_free(&msix_p->msix_pba_hdl);
9330Sstevel@tonic-gate 	ddi_regs_map_free(&msix_p->msix_tbl_hdl);
9340Sstevel@tonic-gate 	kmem_free(msix_p, sizeof (ddi_intr_msix_t));
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * Next set of routines are for INTx (legacy) PCI interrupt
9410Sstevel@tonic-gate  * support only.
9420Sstevel@tonic-gate  */
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate /*
9450Sstevel@tonic-gate  * pci_intx_get_cap:
9460Sstevel@tonic-gate  *	For non-MSI devices that comply to PCI v2.3 or greater;
9470Sstevel@tonic-gate  *	read the command register. Bit 10 implies interrupt disable.
9480Sstevel@tonic-gate  *	Set this bit and then read the status register bit 3.
9490Sstevel@tonic-gate  *	Bit 3 of status register is Interrupt state.
9500Sstevel@tonic-gate  *	If it is set; then the device supports 'Masking'
9510Sstevel@tonic-gate  *
9520Sstevel@tonic-gate  *	Reset the device back to the original state.
9530Sstevel@tonic-gate  */
9540Sstevel@tonic-gate int
9550Sstevel@tonic-gate pci_intx_get_cap(dev_info_t *dip, int *flagsp)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	uint16_t		cmdreg, savereg;
9580Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdl;
9590Sstevel@tonic-gate #ifdef	DEBUG
9600Sstevel@tonic-gate 	uint16_t		statreg;
9610Sstevel@tonic-gate #endif /* DEBUG */
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	*flagsp = 0;
9640Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_cap: %s%d: called\n",
9650Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip)));
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	if (pci_config_setup(dip, &cfg_hdl) != DDI_SUCCESS) {
9680Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_cap: can't get "
9690Sstevel@tonic-gate 		    "config handle\n"));
9700Sstevel@tonic-gate 		return (DDI_FAILURE);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	savereg = pci_config_get16(cfg_hdl, PCI_CONF_COMM);
9740Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_cap: "
9750Sstevel@tonic-gate 	    "command register was 0x%x\n", savereg));
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/* Disable the interrupts */
9780Sstevel@tonic-gate 	cmdreg = savereg | PCI_COMM_INTX_DISABLE;
9790Sstevel@tonic-gate 	pci_config_put16(cfg_hdl, PCI_CONF_COMM, cmdreg);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate #ifdef	DEBUG
9820Sstevel@tonic-gate 	statreg = pci_config_get16(cfg_hdl, PCI_CONF_STAT);
9830Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_cap: "
9840Sstevel@tonic-gate 	    "status register is 0x%x\n", statreg));
9850Sstevel@tonic-gate #endif /* DEBUG */
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/* Read the bit back */
9880Sstevel@tonic-gate 	cmdreg = pci_config_get16(cfg_hdl, PCI_CONF_COMM);
9890Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_cap: "
9900Sstevel@tonic-gate 	    "command register is now 0x%x\n", cmdreg));
9910Sstevel@tonic-gate 
992693Sgovinda 	*flagsp = DDI_INTR_FLAG_LEVEL;
993693Sgovinda 
9940Sstevel@tonic-gate 	if (cmdreg & PCI_COMM_INTX_DISABLE) {
9950Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_cap: "
9960Sstevel@tonic-gate 		    "masking supported\n"));
997693Sgovinda 		*flagsp |= (DDI_INTR_FLAG_MASKABLE |
998693Sgovinda 		    DDI_INTR_FLAG_PENDING);
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	/* Restore the device back to the original state and return */
10020Sstevel@tonic-gate 	pci_config_put16(cfg_hdl, PCI_CONF_COMM, savereg);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdl);
10050Sstevel@tonic-gate 	return (DDI_SUCCESS);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate /*
10100Sstevel@tonic-gate  * pci_intx_clr_mask:
10110Sstevel@tonic-gate  *	For non-MSI devices that comply to PCI v2.3 or greater;
10120Sstevel@tonic-gate  *	clear the bit10 in the command register.
10130Sstevel@tonic-gate  */
10140Sstevel@tonic-gate int
10150Sstevel@tonic-gate pci_intx_clr_mask(dev_info_t *dip)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	uint16_t		cmdreg;
10180Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdl;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_clr_mask: %s%d: called\n",
10210Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip)));
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if (pci_config_setup(dip, &cfg_hdl) != DDI_SUCCESS) {
10240Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_intx_clr_mask: can't get "
10250Sstevel@tonic-gate 		    "config handle\n"));
10260Sstevel@tonic-gate 		return (DDI_FAILURE);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	cmdreg = pci_config_get16(cfg_hdl, PCI_CONF_COMM);
10300Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_clr_mask: "
10310Sstevel@tonic-gate 	    "command register was 0x%x\n", cmdreg));
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	/* Enable the interrupts */
10340Sstevel@tonic-gate 	cmdreg &= ~PCI_COMM_INTX_DISABLE;
10350Sstevel@tonic-gate 	pci_config_put16(cfg_hdl, PCI_CONF_COMM, cmdreg);
10360Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdl);
10370Sstevel@tonic-gate 	return (DDI_SUCCESS);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate /*
10420Sstevel@tonic-gate  * pci_intx_set_mask:
10430Sstevel@tonic-gate  *	For non-MSI devices that comply to PCI v2.3 or greater;
10440Sstevel@tonic-gate  *	set the bit10 in the command register.
10450Sstevel@tonic-gate  */
10460Sstevel@tonic-gate int
10470Sstevel@tonic-gate pci_intx_set_mask(dev_info_t *dip)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	uint16_t		cmdreg;
10500Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdl;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_set_mask: %s%d: called\n",
10530Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip)));
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	if (pci_config_setup(dip, &cfg_hdl) != DDI_SUCCESS) {
10560Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_intx_set_mask: can't get "
10570Sstevel@tonic-gate 		    "config handle\n"));
10580Sstevel@tonic-gate 		return (DDI_FAILURE);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	cmdreg = pci_config_get16(cfg_hdl, PCI_CONF_COMM);
10620Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_set_mask: "
10630Sstevel@tonic-gate 	    "command register was 0x%x\n", cmdreg));
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/* Disable the interrupts */
10660Sstevel@tonic-gate 	cmdreg |= PCI_COMM_INTX_DISABLE;
10670Sstevel@tonic-gate 	pci_config_put16(cfg_hdl, PCI_CONF_COMM, cmdreg);
10680Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdl);
10690Sstevel@tonic-gate 	return (DDI_SUCCESS);
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate /*
10730Sstevel@tonic-gate  * pci_intx_get_pending:
10740Sstevel@tonic-gate  *	For non-MSI devices that comply to PCI v2.3 or greater;
10750Sstevel@tonic-gate  *	read the status register. Bit 3 of status register is
10760Sstevel@tonic-gate  *	Interrupt state. If it is set; then the interrupt is
10770Sstevel@tonic-gate  *	'Pending'.
10780Sstevel@tonic-gate  */
10790Sstevel@tonic-gate int
10800Sstevel@tonic-gate pci_intx_get_pending(dev_info_t *dip, int *pendingp)
10810Sstevel@tonic-gate {
10820Sstevel@tonic-gate 	uint16_t		statreg;
10830Sstevel@tonic-gate 	ddi_acc_handle_t	cfg_hdl;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	*pendingp = 0;
10860Sstevel@tonic-gate 	DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_pending: %s%d: called\n",
10870Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip)));
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	if (pci_config_setup(dip, &cfg_hdl) != DDI_SUCCESS) {
10900Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_pending: can't get "
10910Sstevel@tonic-gate 		    "config handle\n"));
10920Sstevel@tonic-gate 		return (DDI_FAILURE);
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	statreg = pci_config_get16(cfg_hdl, PCI_CONF_STAT);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	if (statreg & PCI_STAT_INTR) {
10980Sstevel@tonic-gate 		DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_pending: "
10990Sstevel@tonic-gate 		    "interrupt is pending\n"));
11000Sstevel@tonic-gate 		*pendingp = 1;
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	pci_config_teardown(&cfg_hdl);
11040Sstevel@tonic-gate 	return (DDI_SUCCESS);
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate  * pci_devclass_to_ipl:
11100Sstevel@tonic-gate  *	translate from device class to ipl
11110Sstevel@tonic-gate  *	NOTE: This function is  added here as pci_intx_get_ispec()
11120Sstevel@tonic-gate  *	calls this to figure out the priority.
11130Sstevel@tonic-gate  *	It is moved over from x86 pci.c
11140Sstevel@tonic-gate  */
11150Sstevel@tonic-gate int
11160Sstevel@tonic-gate pci_devclass_to_ipl(int class)
11170Sstevel@tonic-gate {
11180Sstevel@tonic-gate 	int	base_cl;
11190Sstevel@tonic-gate 	int	ipl;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	base_cl = (class & 0xff0000) >> 16;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	/*
11240Sstevel@tonic-gate 	 * Use the class code values to construct an ipl for the device.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	switch (base_cl) {
11270Sstevel@tonic-gate 	default:
11280Sstevel@tonic-gate 	case PCI_CLASS_NONE:
11290Sstevel@tonic-gate 		ipl = 1;
11300Sstevel@tonic-gate 		break;
11310Sstevel@tonic-gate 	case PCI_CLASS_MASS:
11320Sstevel@tonic-gate 		ipl = 0x5;
11330Sstevel@tonic-gate 		break;
11340Sstevel@tonic-gate 	case PCI_CLASS_NET:
11350Sstevel@tonic-gate 		ipl = 0x6;
11360Sstevel@tonic-gate 		break;
11370Sstevel@tonic-gate 	case PCI_CLASS_DISPLAY:
11380Sstevel@tonic-gate 		ipl = 0x9;
11390Sstevel@tonic-gate 		break;
11400Sstevel@tonic-gate 	/*
11410Sstevel@tonic-gate 	 * for high priority interrupt handlers, use level 12
11420Sstevel@tonic-gate 	 * as the highest for device drivers
11430Sstevel@tonic-gate 	 */
11440Sstevel@tonic-gate 	case PCI_CLASS_MM:
11450Sstevel@tonic-gate 		ipl = 0xc;
11460Sstevel@tonic-gate 		break;
11470Sstevel@tonic-gate 	case PCI_CLASS_MEM:
11480Sstevel@tonic-gate 		ipl = 0xc;
11490Sstevel@tonic-gate 		break;
11500Sstevel@tonic-gate 	case PCI_CLASS_BRIDGE:
11510Sstevel@tonic-gate 		ipl = 0xc;
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 	return (ipl);
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate /*
11590Sstevel@tonic-gate  * pci_intx_get_ispec:
11600Sstevel@tonic-gate  *	Get intrspec for PCI devices (legacy support)
11610Sstevel@tonic-gate  *	NOTE: This is moved here from x86 pci.c and is
11620Sstevel@tonic-gate  *	needed here as pci-ide.c uses it as well
11630Sstevel@tonic-gate  */
11640Sstevel@tonic-gate /*ARGSUSED*/
11650Sstevel@tonic-gate ddi_intrspec_t
11660Sstevel@tonic-gate pci_intx_get_ispec(dev_info_t *dip, dev_info_t *rdip, int inum)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	int				class, *intpriorities;
11690Sstevel@tonic-gate 	uint_t				num_intpriorities;
11700Sstevel@tonic-gate 	struct intrspec			*ispec;
11710Sstevel@tonic-gate 	ddi_acc_handle_t		cfg_hdl;
11720Sstevel@tonic-gate 	struct ddi_parent_private_data	*pdptr;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if ((pdptr = ddi_get_parent_data(rdip)) == NULL)
11750Sstevel@tonic-gate 		return (NULL);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	ispec = pdptr->par_intr;
11780Sstevel@tonic-gate 	ASSERT(ispec);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/* check if the intrspec_pri has been initialized */
11810Sstevel@tonic-gate 	if (!ispec->intrspec_pri) {
11820Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
11830Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "interrupt-priorities",
11840Sstevel@tonic-gate 		    &intpriorities, &num_intpriorities) == DDI_PROP_SUCCESS) {
11850Sstevel@tonic-gate 			if (inum < num_intpriorities)
11860Sstevel@tonic-gate 				ispec->intrspec_pri = intpriorities[inum];
11870Sstevel@tonic-gate 			ddi_prop_free(intpriorities);
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		/* If still no priority, guess based on the class code */
11910Sstevel@tonic-gate 		if (ispec->intrspec_pri == 0) {
11920Sstevel@tonic-gate 			/* get 'class' property to derive the intr priority */
11930Sstevel@tonic-gate 			class = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
11940Sstevel@tonic-gate 			    DDI_PROP_DONTPASS, "class-code", -1);
11950Sstevel@tonic-gate 			ispec->intrspec_pri = (class == -1) ? 1 :
11960Sstevel@tonic-gate 			    pci_devclass_to_ipl(class);
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	/* Get interrupt line value */
12010Sstevel@tonic-gate 	if (!ispec->intrspec_vec) {
12020Sstevel@tonic-gate 		if (pci_config_setup(rdip, &cfg_hdl) != DDI_SUCCESS) {
12030Sstevel@tonic-gate 			DDI_INTR_NEXDBG((CE_CONT, "pci_intx_get_iline: "
12040Sstevel@tonic-gate 			    "can't get config handle\n"));
12050Sstevel@tonic-gate 			return (NULL);
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		ispec->intrspec_vec = pci_config_get8(cfg_hdl, PCI_CONF_ILINE);
12090Sstevel@tonic-gate 		pci_config_teardown(&cfg_hdl);
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	return ((ddi_intrspec_t)ispec);
12130Sstevel@tonic-gate }
1214