xref: /illumos-gate/usr/src/uts/sun4/io/px/px_intr.c (revision b0fc0e77)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5102cb92eSjohnny  * Common Development and Distribution License (the "License").
6102cb92eSjohnny  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22102cb92eSjohnny  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * PX nexus interrupt handling:
307c478bd9Sstevel@tonic-gate  *	PX device interrupt handler wrapper
317c478bd9Sstevel@tonic-gate  *	PIL lookup routine
327c478bd9Sstevel@tonic-gate  *	PX device interrupt related initchild code
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/async.h>
387c478bd9Sstevel@tonic-gate #include <sys/spl.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
40f8d2de6bSjchu #include <sys/fm/protocol.h>
41f8d2de6bSjchu #include <sys/fm/util.h>
427c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
437c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
447c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
457c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
467c478bd9Sstevel@tonic-gate #include "px_obj.h"
47f8d2de6bSjchu #include <sys/ontrap.h>
48f8d2de6bSjchu #include <sys/membar.h>
496d44af1bSesolom #include <sys/clock.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * interrupt jabber:
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
557c478bd9Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
567c478bd9Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
577c478bd9Sstevel@tonic-gate  * px_intr_wrapper() which normally idles the ino's state machine which would
587c478bd9Sstevel@tonic-gate  * precipitate another trip round the loop.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
617c478bd9Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
627c478bd9Sstevel@tonic-gate  * beginning of px_intr_wrapper() explaining how the 'interrupt jabber
637c478bd9Sstevel@tonic-gate  * protection' code does this.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
707c478bd9Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
717c478bd9Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate static int
74*b0fc0e77Sgovinda px_spurintr(px_ino_pil_t *ipil_p)
757c478bd9Sstevel@tonic-gate {
76*b0fc0e77Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
77*b0fc0e77Sgovinda 	px_ih_t		*ih_p = ipil_p->ipil_ih_start;
78*b0fc0e77Sgovinda 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
79*b0fc0e77Sgovinda 	char		*err_fmt_str;
80*b0fc0e77Sgovinda 	boolean_t	blocked = B_FALSE;
81*b0fc0e77Sgovinda 	int		i;
827c478bd9Sstevel@tonic-gate 
83*b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max)
847c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
857c478bd9Sstevel@tonic-gate 
86*b0fc0e77Sgovinda 	if (!ino_p->ino_unclaimed_intrs)
877c478bd9Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
887c478bd9Sstevel@tonic-gate 
89*b0fc0e77Sgovinda 	ino_p->ino_unclaimed_intrs++;
907c478bd9Sstevel@tonic-gate 
91*b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs <= px_unclaimed_intr_max)
927c478bd9Sstevel@tonic-gate 		goto clear;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
957c478bd9Sstevel@tonic-gate 	    > px_spurintr_duration) {
96*b0fc0e77Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
977c478bd9Sstevel@tonic-gate 		goto clear;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
100*b0fc0e77Sgovinda 	blocked = B_TRUE;
1017c478bd9Sstevel@tonic-gate 	goto warn;
1027c478bd9Sstevel@tonic-gate clear:
1037c478bd9Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
1047c478bd9Sstevel@tonic-gate warn:
1057c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(px_p->px_dip), ino_p->ino_ino);
106*b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next)
1077c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
1087c478bd9Sstevel@tonic-gate 		    ih_p->ih_inum);
1097c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
110*b0fc0e77Sgovinda 
111*b0fc0e77Sgovinda 	/* Clear the pending state */
112*b0fc0e77Sgovinda 	if (blocked == B_FALSE) {
113*b0fc0e77Sgovinda 		if (px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
114*b0fc0e77Sgovinda 		    INTR_IDLE_STATE) != DDI_SUCCESS)
115*b0fc0e77Sgovinda 			return (DDI_INTR_UNCLAIMED);
116*b0fc0e77Sgovinda 	}
117*b0fc0e77Sgovinda 
1187c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate extern uint64_t intr_get_time(void);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
124a195726fSgovinda  * px_intx_intr (INTx or legacy interrupt handler)
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
1277c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
1287c478bd9Sstevel@tonic-gate  * examines the return codes.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
1317c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
1327c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
1337c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
1347c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
1357c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
1367c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
1377c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
1407c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate uint_t
1437c478bd9Sstevel@tonic-gate px_intx_intr(caddr_t arg)
1447c478bd9Sstevel@tonic-gate {
145*b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p = (px_ino_pil_t *)arg;
146*b0fc0e77Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
1477c478bd9Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
148*b0fc0e77Sgovinda 	px_ih_t		*ih_p = ipil_p->ipil_ih_start;
149*b0fc0e77Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
150*b0fc0e77Sgovinda 	uint_t		result = 0, r = DDI_INTR_UNCLAIMED;
1517c478bd9Sstevel@tonic-gate 	int		i;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1547c478bd9Sstevel@tonic-gate 	    "ino=%x sysino=%llx pil=%x ih_size=%x ih_lst=%x\n",
155*b0fc0e77Sgovinda 	    ino_p->ino_ino, ino_p->ino_sysino, ipil_p->ipil_pil,
156*b0fc0e77Sgovinda 	    ipil_p->ipil_ih_size, ipil_p->ipil_ih_head);
1577c478bd9Sstevel@tonic-gate 
158*b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
1597c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
1607c478bd9Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
1617c478bd9Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
1627c478bd9Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PX_INTR_STATE_DISABLE) {
1657c478bd9Sstevel@tonic-gate 			DBG(DBG_INTX_INTR, px_p->px_dip,
1667c478bd9Sstevel@tonic-gate 			    "px_intx_intr: %s%d interrupt %d is disabled\n",
1677c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
1687c478bd9Sstevel@tonic-gate 			    ino_p->ino_ino);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 			continue;
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1747c478bd9Sstevel@tonic-gate 		    "ino=%x handler=%p arg1 =%p arg2 = %p\n",
1757c478bd9Sstevel@tonic-gate 		    ino_p->ino_ino, handler, arg1, arg2);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
1787c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
1847c478bd9Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
1857c478bd9Sstevel@tonic-gate 		 * using atomic ops.
1867c478bd9Sstevel@tonic-gate 		 */
1877c478bd9Sstevel@tonic-gate 
188*b0fc0e77Sgovinda 		if (pil <= LOCK_LEVEL)
1897c478bd9Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
1927c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		result += r;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		if (px_check_all_handlers)
1977c478bd9Sstevel@tonic-gate 			continue;
1987c478bd9Sstevel@tonic-gate 		if (result)
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
202*b0fc0e77Sgovinda 	if (result)
203*b0fc0e77Sgovinda 		ino_p->ino_claimed |= (1 << pil);
204*b0fc0e77Sgovinda 
205*b0fc0e77Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
206*b0fc0e77Sgovinda 	if (pil != ino_p->ino_lopil)
207*b0fc0e77Sgovinda 		return (DDI_INTR_CLAIMED);
208*b0fc0e77Sgovinda 
209*b0fc0e77Sgovinda 	if (!ino_p->ino_claimed) {
210*b0fc0e77Sgovinda 		if (px_unclaimed_intr_block)
211*b0fc0e77Sgovinda 			return (px_spurintr(ipil_p));
212*b0fc0e77Sgovinda 	}
2137c478bd9Sstevel@tonic-gate 
214*b0fc0e77Sgovinda 	ino_p->ino_unclaimed_intrs = 0;
215*b0fc0e77Sgovinda 	ino_p->ino_claimed = 0;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
218*b0fc0e77Sgovinda 	if (px_lib_intr_setstate(px_p->px_dip,
2197c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, INTR_IDLE_STATE) != DDI_SUCCESS)
2207c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
226a195726fSgovinda  * px_msiq_intr (MSI/X or PCIe MSG interrupt handler)
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2297c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2307c478bd9Sstevel@tonic-gate  * examines the return codes.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2337c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2347c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2357c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2367c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2377c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2387c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2397c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
2407c478bd9Sstevel@tonic-gate  *
2417c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2427c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate uint_t
2457c478bd9Sstevel@tonic-gate px_msiq_intr(caddr_t arg)
2467c478bd9Sstevel@tonic-gate {
247*b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p = (px_ino_pil_t *)arg;
248*b0fc0e77Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
2497c478bd9Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
2507c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
2517c478bd9Sstevel@tonic-gate 	px_msiq_t	*msiq_p = ino_p->ino_msiq_p;
2527c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
253*b0fc0e77Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
2547c478bd9Sstevel@tonic-gate 	msiq_rec_t	msiq_rec, *msiq_rec_p = &msiq_rec;
255023ccc1eSegillett 	msiqhead_t	*curr_head_p;
256023ccc1eSegillett 	msiqtail_t	curr_tail_index;
2577c478bd9Sstevel@tonic-gate 	msgcode_t	msg_code;
2587c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
259*b0fc0e77Sgovinda 	uint_t		ret = DDI_INTR_UNCLAIMED;
260*b0fc0e77Sgovinda 	int		i, j;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: msiq_id =%x ino=%x pil=%x "
2637c478bd9Sstevel@tonic-gate 	    "ih_size=%x ih_lst=%x\n", msiq_p->msiq_id, ino_p->ino_ino,
264*b0fc0e77Sgovinda 	    ipil_p->ipil_pil, ipil_p->ipil_ih_size, ipil_p->ipil_ih_head);
265*b0fc0e77Sgovinda 
266*b0fc0e77Sgovinda 	/*
267*b0fc0e77Sgovinda 	 * The px_msiq_intr() handles multiple interrupt priorities and it
268*b0fc0e77Sgovinda 	 * will set msiq->msiq_rec2process to the number of MSIQ records to
269*b0fc0e77Sgovinda 	 * process while handling the highest priority interrupt. Subsequent
270*b0fc0e77Sgovinda 	 * lower priority interrupts will just process any unprocessed MSIQ
271*b0fc0e77Sgovinda 	 * records or will just return immediately.
272*b0fc0e77Sgovinda 	 */
273*b0fc0e77Sgovinda 	if (msiq_p->msiq_recs2process == 0) {
274*b0fc0e77Sgovinda 		/* Read current MSIQ tail index */
275*b0fc0e77Sgovinda 		px_lib_msiq_gettail(dip, msiq_p->msiq_id, &curr_tail_index);
276*b0fc0e77Sgovinda 		msiq_p->msiq_new_head_index = msiq_p->msiq_curr_head_index;
277*b0fc0e77Sgovinda 
278*b0fc0e77Sgovinda 		if (curr_tail_index < msiq_p->msiq_curr_head_index)
279*b0fc0e77Sgovinda 			curr_tail_index += msiq_state_p->msiq_rec_cnt;
2807c478bd9Sstevel@tonic-gate 
281*b0fc0e77Sgovinda 		msiq_p->msiq_recs2process = curr_tail_index -
282*b0fc0e77Sgovinda 		    msiq_p->msiq_curr_head_index;
283*b0fc0e77Sgovinda 	}
284*b0fc0e77Sgovinda 
285*b0fc0e77Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x new_head %x "
286*b0fc0e77Sgovinda 	    "rec2process %x\n", msiq_p->msiq_curr_head_index,
287*b0fc0e77Sgovinda 	    msiq_p->msiq_new_head_index, msiq_p->msiq_recs2process);
2887c478bd9Sstevel@tonic-gate 
289*b0fc0e77Sgovinda 	/* If all MSIQ records are already processed, just return immediately */
290*b0fc0e77Sgovinda 	if ((msiq_p->msiq_new_head_index - msiq_p->msiq_curr_head_index)
291*b0fc0e77Sgovinda 	    == msiq_p->msiq_recs2process)
292*b0fc0e77Sgovinda 		goto intr_done;
293*b0fc0e77Sgovinda 
294*b0fc0e77Sgovinda 	curr_head_p = (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p +
295*b0fc0e77Sgovinda 	    (msiq_p->msiq_curr_head_index * sizeof (msiq_rec_t)));
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
298023ccc1eSegillett 	 * Calculate the number of recs to process by taking the difference
299023ccc1eSegillett 	 * between the head and tail pointers. For all records we always
300023ccc1eSegillett 	 * verify that we have a valid record type before we do any processing.
301*b0fc0e77Sgovinda 	 * If triggered, we should always have at least one valid record.
3027c478bd9Sstevel@tonic-gate 	 */
303*b0fc0e77Sgovinda 	for (i = 0; i < msiq_p->msiq_recs2process; i++) {
304*b0fc0e77Sgovinda 		/* Read next MSIQ record */
305023ccc1eSegillett 		px_lib_get_msiq_rec(dip, curr_head_p, msiq_rec_p);
306023ccc1eSegillett 
3077c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSIQ RECORD, "
3087c478bd9Sstevel@tonic-gate 		    "msiq_rec_type 0x%llx msiq_rec_rid 0x%llx\n",
3097c478bd9Sstevel@tonic-gate 		    msiq_rec_p->msiq_rec_type, msiq_rec_p->msiq_rec_rid);
3107c478bd9Sstevel@tonic-gate 
311023ccc1eSegillett 		if (!msiq_rec_p->msiq_rec_type)
312*b0fc0e77Sgovinda 			goto next_rec;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/* Check MSIQ record type */
3157c478bd9Sstevel@tonic-gate 		switch (msiq_rec_p->msiq_rec_type) {
3167c478bd9Sstevel@tonic-gate 		case MSG_REC:
3177c478bd9Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msg.msg_code;
3187c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: PCIE MSG "
3197c478bd9Sstevel@tonic-gate 			    "record, msg type 0x%x\n", msg_code);
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		case MSI32_REC:
3227c478bd9Sstevel@tonic-gate 		case MSI64_REC:
3237c478bd9Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msi.msi_data;
3247c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSI record, "
3257c478bd9Sstevel@tonic-gate 			    "msi 0x%x\n", msg_code);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			/* Clear MSI state */
3287c478bd9Sstevel@tonic-gate 			px_lib_msi_setstate(dip, (msinum_t)msg_code,
3297c478bd9Sstevel@tonic-gate 			    PCI_MSI_STATE_IDLE);
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 		default:
3327c478bd9Sstevel@tonic-gate 			msg_code = 0;
3337c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_msiq_intr: 0x%x MSIQ "
3347c478bd9Sstevel@tonic-gate 			    "record type is not supported",
3357c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3367c478bd9Sstevel@tonic-gate 			    msiq_rec_p->msiq_rec_type);
337*b0fc0e77Sgovinda 
3387c478bd9Sstevel@tonic-gate 			goto next_rec;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * Scan through px_ih_t linked list, searching for the
3437c478bd9Sstevel@tonic-gate 		 * right px_ih_t, matching MSIQ record data.
3447c478bd9Sstevel@tonic-gate 		 */
345*b0fc0e77Sgovinda 		for (j = 0, ih_p = ipil_p->ipil_ih_start;
346*b0fc0e77Sgovinda 		    ih_p && (j < ipil_p->ipil_ih_size) &&
34707f14c08Sgovinda 		    ((ih_p->ih_msg_code != msg_code) ||
34807f14c08Sgovinda 		    (ih_p->ih_rec_type != msiq_rec_p->msiq_rec_type));
3498c02a06eSegillett 		    ih_p = ih_p->ih_next, j++);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if ((ih_p->ih_msg_code == msg_code) &&
3527c478bd9Sstevel@tonic-gate 		    (ih_p->ih_rec_type == msiq_rec_p->msiq_rec_type)) {
3537c478bd9Sstevel@tonic-gate 			dev_info_t *dip = ih_p->ih_dip;
3547c478bd9Sstevel@tonic-gate 			uint_t (*handler)() = ih_p->ih_handler;
3557c478bd9Sstevel@tonic-gate 			caddr_t arg1 = ih_p->ih_handler_arg1;
3567c478bd9Sstevel@tonic-gate 			caddr_t arg2 = ih_p->ih_handler_arg2;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: ino=%x data=%x "
3597c478bd9Sstevel@tonic-gate 			    "handler=%p arg1 =%p arg2=%p\n", ino_p->ino_ino,
3607c478bd9Sstevel@tonic-gate 			    msg_code, handler, arg1, arg2);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3637c478bd9Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, caddr_t, arg2);
3647c478bd9Sstevel@tonic-gate 
365f8d2de6bSjchu 			/*
366f8d2de6bSjchu 			 * Special case for PCIE Error Messages.
367f8d2de6bSjchu 			 * The current frame work doesn't fit PCIE Err Msgs
368f8d2de6bSjchu 			 * This should be fixed when PCIE MESSAGES as a whole
369f8d2de6bSjchu 			 * is architected correctly.
370f8d2de6bSjchu 			 */
371f8d2de6bSjchu 			if ((msg_code == PCIE_MSG_CODE_ERR_COR) ||
372f8d2de6bSjchu 			    (msg_code == PCIE_MSG_CODE_ERR_NONFATAL) ||
373f8d2de6bSjchu 			    (msg_code == PCIE_MSG_CODE_ERR_FATAL)) {
374f8d2de6bSjchu 				ret = px_err_fabric_intr(px_p, msg_code,
375f8d2de6bSjchu 				    msiq_rec_p->msiq_rec_rid);
376f8d2de6bSjchu 			} else
377f8d2de6bSjchu 				ret = (*handler)(arg1, arg2);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 			/*
3807c478bd9Sstevel@tonic-gate 			 * Account for time used by this interrupt. Protect
3817c478bd9Sstevel@tonic-gate 			 * against conflicting writes to ih_ticks from
3827c478bd9Sstevel@tonic-gate 			 * ib_intr_dist_all() by using atomic ops.
3837c478bd9Sstevel@tonic-gate 			 */
3847c478bd9Sstevel@tonic-gate 
385*b0fc0e77Sgovinda 			if (pil <= LOCK_LEVEL)
3867c478bd9Sstevel@tonic-gate 				atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3897c478bd9Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, int, ret);
390023ccc1eSegillett 
391*b0fc0e77Sgovinda 			msiq_p->msiq_new_head_index++;
392*b0fc0e77Sgovinda 			px_lib_clr_msiq_rec(dip, curr_head_p);
3937c478bd9Sstevel@tonic-gate 		} else {
3947c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr:"
395023ccc1eSegillett 			    "No matching MSIQ record found\n");
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate next_rec:
398023ccc1eSegillett 		/* Get the pointer next EQ record */
399023ccc1eSegillett 		curr_head_p = (msiqhead_t *)
400023ccc1eSegillett 		    ((caddr_t)curr_head_p + sizeof (msiq_rec_t));
401023ccc1eSegillett 
402023ccc1eSegillett 		/* Check for overflow condition */
403023ccc1eSegillett 		if (curr_head_p >= (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p
404*b0fc0e77Sgovinda 		    + (msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t))))
405023ccc1eSegillett 			curr_head_p = (msiqhead_t *)msiq_p->msiq_base_p;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
408*b0fc0e77Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: No of MSIQ recs processed %x\n",
409*b0fc0e77Sgovinda 	    (msiq_p->msiq_new_head_index - msiq_p->msiq_curr_head_index));
410023ccc1eSegillett 
411*b0fc0e77Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x new_head %x "
412*b0fc0e77Sgovinda 	    "rec2process %x\n", msiq_p->msiq_curr_head_index,
413*b0fc0e77Sgovinda 	    msiq_p->msiq_new_head_index, msiq_p->msiq_recs2process);
414*b0fc0e77Sgovinda 
415*b0fc0e77Sgovinda 	/* ino_claimed used just for debugging purpose */
416*b0fc0e77Sgovinda 	if (ret)
417*b0fc0e77Sgovinda 		ino_p->ino_claimed |= (1 << pil);
418*b0fc0e77Sgovinda 
419*b0fc0e77Sgovinda intr_done:
420*b0fc0e77Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
421*b0fc0e77Sgovinda 	if (pil != ino_p->ino_lopil)
422*b0fc0e77Sgovinda 		return (DDI_INTR_CLAIMED);
423*b0fc0e77Sgovinda 
424*b0fc0e77Sgovinda 	if (msiq_p->msiq_new_head_index <= msiq_p->msiq_curr_head_index)  {
425*b0fc0e77Sgovinda 		if (px_unclaimed_intr_block)
426*b0fc0e77Sgovinda 			return (px_spurintr(ipil_p));
427023ccc1eSegillett 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*  Update MSIQ head index with no of MSIQ records processed */
430*b0fc0e77Sgovinda 	if (msiq_p->msiq_new_head_index >= msiq_state_p->msiq_rec_cnt)
431*b0fc0e77Sgovinda 		msiq_p->msiq_new_head_index -= msiq_state_p->msiq_rec_cnt;
4327c478bd9Sstevel@tonic-gate 
433*b0fc0e77Sgovinda 	msiq_p->msiq_curr_head_index = msiq_p->msiq_new_head_index;
434*b0fc0e77Sgovinda 	px_lib_msiq_sethead(dip, msiq_p->msiq_id, msiq_p->msiq_new_head_index);
435*b0fc0e77Sgovinda 
436*b0fc0e77Sgovinda 	msiq_p->msiq_new_head_index = 0;
437*b0fc0e77Sgovinda 	msiq_p->msiq_recs2process = 0;
438*b0fc0e77Sgovinda 	ino_p->ino_claimed = 0;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
4417c478bd9Sstevel@tonic-gate 	if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
4427c478bd9Sstevel@tonic-gate 	    INTR_IDLE_STATE) != DDI_SUCCESS)
4437c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate dev_info_t *
4497c478bd9Sstevel@tonic-gate px_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip = rdip;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
4547c478bd9Sstevel@tonic-gate 		;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (cdip);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /* Default class to pil value mapping */
4607c478bd9Sstevel@tonic-gate px_class_val_t px_default_pil [] = {
4617c478bd9Sstevel@tonic-gate 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
4627c478bd9Sstevel@tonic-gate 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
4637c478bd9Sstevel@tonic-gate 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
4647c478bd9Sstevel@tonic-gate 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
4657c478bd9Sstevel@tonic-gate 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
46644bb982bSgovinda 	{0x050000, 0xff0000, 0x9},	/* Memory Controller */
46744bb982bSgovinda 	{0x060000, 0xff0000, 0x9},	/* Bridge Controller */
4687c478bd9Sstevel@tonic-gate 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
4697c478bd9Sstevel@tonic-gate 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
4707c478bd9Sstevel@tonic-gate 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
4717c478bd9Sstevel@tonic-gate 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
4727c478bd9Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
4737c478bd9Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
4747c478bd9Sstevel@tonic-gate };
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
4787c478bd9Sstevel@tonic-gate  * entry on or above the pci node like
4797c478bd9Sstevel@tonic-gate  *
4807c478bd9Sstevel@tonic-gate  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * can be used to augment or override entries in the default table below.
4837c478bd9Sstevel@tonic-gate  *
4847c478bd9Sstevel@tonic-gate  * NB: The values below give NICs preference on redistribution, and provide
4857c478bd9Sstevel@tonic-gate  * NICs some isolation from other interrupt sources. We need better interfaces
4867c478bd9Sstevel@tonic-gate  * that allow the NIC driver to identify a specific NIC instance as high
4877c478bd9Sstevel@tonic-gate  * bandwidth, and thus deserving of separation from other low bandwidth
4887c478bd9Sstevel@tonic-gate  * NICs additional isolation from other interrupt sources.
4897c478bd9Sstevel@tonic-gate  *
4907c478bd9Sstevel@tonic-gate  * NB: We treat Infiniband like a NIC.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate px_class_val_t px_default_intr_weight [] = {
4937c478bd9Sstevel@tonic-gate 	{0x020000, 0xff0000, 35},	/* Network Controller */
4947c478bd9Sstevel@tonic-gate 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
4957c478bd9Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
4967c478bd9Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
4977c478bd9Sstevel@tonic-gate };
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static uint32_t
5007c478bd9Sstevel@tonic-gate px_match_class_val(uint32_t key, px_class_val_t *rec_p, int nrec,
5017c478bd9Sstevel@tonic-gate     uint32_t default_val)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	int	i;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrec; rec_p++, i++) {
5067c478bd9Sstevel@tonic-gate 		if ((rec_p->class_code & rec_p->class_mask) ==
5077c478bd9Sstevel@tonic-gate 		    (key & rec_p->class_mask))
5087c478bd9Sstevel@tonic-gate 			return (rec_p->class_val);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	return (default_val);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * px_class_to_val
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  * Return the configuration value, based on class code and sub class code,
5187c478bd9Sstevel@tonic-gate  * from the specified property based or default px_class_val_t table.
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate uint32_t
5217c478bd9Sstevel@tonic-gate px_class_to_val(dev_info_t *rdip, char *property_name, px_class_val_t *rec_p,
5227c478bd9Sstevel@tonic-gate     int nrec, uint32_t default_val)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	int property_len;
5257c478bd9Sstevel@tonic-gate 	uint32_t class_code;
5267c478bd9Sstevel@tonic-gate 	px_class_val_t *conf;
5277c478bd9Sstevel@tonic-gate 	uint32_t val = default_val;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * Use the "class-code" property to get the base and sub class
5317c478bd9Sstevel@tonic-gate 	 * codes for the requesting device.
5327c478bd9Sstevel@tonic-gate 	 */
5337c478bd9Sstevel@tonic-gate 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
5347c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "class-code", -1);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (class_code == -1)
5377c478bd9Sstevel@tonic-gate 		return (val);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/* look up the val from the default table */
5407c478bd9Sstevel@tonic-gate 	val = px_match_class_val(class_code, rec_p, nrec, val);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* see if there is a more specific property specified value */
5437c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
5447c478bd9Sstevel@tonic-gate 	    property_name, (caddr_t)&conf, &property_len))
5457c478bd9Sstevel@tonic-gate 		return (val);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if ((property_len % sizeof (px_class_val_t)) == 0)
5487c478bd9Sstevel@tonic-gate 		val = px_match_class_val(class_code, conf,
5497c478bd9Sstevel@tonic-gate 		    property_len / sizeof (px_class_val_t), val);
5507c478bd9Sstevel@tonic-gate 	kmem_free(conf, property_len);
5517c478bd9Sstevel@tonic-gate 	return (val);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /* px_class_to_pil: return the pil for a given device. */
5557c478bd9Sstevel@tonic-gate uint32_t
5567c478bd9Sstevel@tonic-gate px_class_to_pil(dev_info_t *rdip)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	uint32_t pil;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/* default pil is 0 (uninitialized) */
5617c478bd9Sstevel@tonic-gate 	pil = px_class_to_val(rdip,
5627c478bd9Sstevel@tonic-gate 	    "pci-class-priorities", px_default_pil,
5637c478bd9Sstevel@tonic-gate 	    sizeof (px_default_pil) / sizeof (px_class_val_t), 0);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/* range check the result */
5667c478bd9Sstevel@tonic-gate 	if (pil >= 0xf)
5677c478bd9Sstevel@tonic-gate 		pil = 0;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	return (pil);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /* px_class_to_intr_weight: return the intr_weight for a given device. */
5737c478bd9Sstevel@tonic-gate static int32_t
5747c478bd9Sstevel@tonic-gate px_class_to_intr_weight(dev_info_t *rdip)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	int32_t intr_weight;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/* default weight is 0% */
5797c478bd9Sstevel@tonic-gate 	intr_weight = px_class_to_val(rdip,
5807c478bd9Sstevel@tonic-gate 	    "pci-class-intr-weights", px_default_intr_weight,
5817c478bd9Sstevel@tonic-gate 	    sizeof (px_default_intr_weight) / sizeof (px_class_val_t), 0);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/* range check the result */
5847c478bd9Sstevel@tonic-gate 	if (intr_weight < 0)
5857c478bd9Sstevel@tonic-gate 		intr_weight = 0;
5867c478bd9Sstevel@tonic-gate 	if (intr_weight > 1000)
5877c478bd9Sstevel@tonic-gate 		intr_weight = 1000;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	return (intr_weight);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /* ARGSUSED */
5937c478bd9Sstevel@tonic-gate int
5947c478bd9Sstevel@tonic-gate px_intx_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5957c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
5967c478bd9Sstevel@tonic-gate {
597a195726fSgovinda 	px_t	*px_p = DIP_TO_STATE(dip);
598a195726fSgovinda 	int	ret = DDI_SUCCESS;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intx_ops: dip=%x rdip=%x intr_op=%x "
6017c478bd9Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	switch (intr_op) {
6047c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
6057c478bd9Sstevel@tonic-gate 		ret = pci_intx_get_cap(rdip, (int *)result);
6067c478bd9Sstevel@tonic-gate 		break;
6077c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
6087c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_intx_ops: SetCap is not supported\n");
6097c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
6107c478bd9Sstevel@tonic-gate 		break;
6117c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
6127c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
6137c478bd9Sstevel@tonic-gate 		break;
6147c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
617a195726fSgovinda 		*(int *)result = hdlp->ih_pri ?
618a195726fSgovinda 		    hdlp->ih_pri : px_class_to_pil(rdip);
6197c478bd9Sstevel@tonic-gate 		break;
6207c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
6217c478bd9Sstevel@tonic-gate 		break;
6227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
6237c478bd9Sstevel@tonic-gate 		ret = px_add_intx_intr(dip, rdip, hdlp);
6247c478bd9Sstevel@tonic-gate 		break;
6257c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
6267c478bd9Sstevel@tonic-gate 		ret = px_rem_intx_intr(dip, rdip, hdlp);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
6297c478bd9Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
630*b0fc0e77Sgovinda 		    hdlp->ih_vector, hdlp->ih_pri, PX_INTR_STATE_ENABLE, 0, 0);
6317c478bd9Sstevel@tonic-gate 		break;
6327c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
6337c478bd9Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
634*b0fc0e77Sgovinda 		    hdlp->ih_vector, hdlp->ih_pri, PX_INTR_STATE_DISABLE, 0, 0);
6357c478bd9Sstevel@tonic-gate 		break;
6367c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
6377c478bd9Sstevel@tonic-gate 		ret = pci_intx_set_mask(rdip);
6387c478bd9Sstevel@tonic-gate 		break;
6397c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
6407c478bd9Sstevel@tonic-gate 		ret = pci_intx_clr_mask(rdip);
6417c478bd9Sstevel@tonic-gate 		break;
6427c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
6437c478bd9Sstevel@tonic-gate 		ret = pci_intx_get_pending(rdip, (int *)result);
6447c478bd9Sstevel@tonic-gate 		break;
6457c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
6467c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
647a54f81fbSanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
6487c478bd9Sstevel@tonic-gate 		break;
6497c478bd9Sstevel@tonic-gate 	default:
6507c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	return (ret);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /* ARGSUSED */
6587c478bd9Sstevel@tonic-gate int
6597c478bd9Sstevel@tonic-gate px_msix_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
6607c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	px_t			*px_p = DIP_TO_STATE(dip);
6637c478bd9Sstevel@tonic-gate 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
6649c75c6bfSgovinda 	msiq_rec_type_t		msiq_rec_type;
6659c75c6bfSgovinda 	msi_type_t		msi_type;
6669c75c6bfSgovinda 	uint64_t		msi_addr;
6677c478bd9Sstevel@tonic-gate 	msinum_t		msi_num;
6687c478bd9Sstevel@tonic-gate 	msiqid_t		msiq_id;
6697c478bd9Sstevel@tonic-gate 	uint_t			nintrs;
6707c478bd9Sstevel@tonic-gate 	int			i, ret = DDI_SUCCESS;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_msix_ops: dip=%x rdip=%x intr_op=%x "
6737c478bd9Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
6747c478bd9Sstevel@tonic-gate 
6759c75c6bfSgovinda 	/* Check for MSI64 support */
67607f14c08Sgovinda 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
6779c75c6bfSgovinda 		msiq_rec_type = MSI64_REC;
6789c75c6bfSgovinda 		msi_type = MSI64_TYPE;
67907f14c08Sgovinda 		msi_addr = msi_state_p->msi_addr64;
6809c75c6bfSgovinda 	} else {
6819c75c6bfSgovinda 		msiq_rec_type = MSI32_REC;
6829c75c6bfSgovinda 		msi_type = MSI32_TYPE;
6839c75c6bfSgovinda 		msi_addr = msi_state_p->msi_addr32;
6849c75c6bfSgovinda 	}
6859c75c6bfSgovinda 
6867c478bd9Sstevel@tonic-gate 	switch (intr_op) {
6877c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
6887c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_cap(rdip, hdlp->ih_type, (int *)result);
6897c478bd9Sstevel@tonic-gate 		break;
6907c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
6917c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: SetCap is not supported\n");
6927c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
6937c478bd9Sstevel@tonic-gate 		break;
6947c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
6957c478bd9Sstevel@tonic-gate 		/*
6967c478bd9Sstevel@tonic-gate 		 * We need to restrict this allocation in future
6977c478bd9Sstevel@tonic-gate 		 * based on Resource Management policies.
6987c478bd9Sstevel@tonic-gate 		 */
6997c478bd9Sstevel@tonic-gate 		if ((ret = px_msi_alloc(px_p, rdip, hdlp->ih_inum,
70020036fe5Segillett 		    hdlp->ih_scratch1, (uintptr_t)hdlp->ih_scratch2, &msi_num,
70120036fe5Segillett 		    (int *)result)) != DDI_SUCCESS) {
70220036fe5Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: allocation "
70320036fe5Segillett 			    "failed, rdip 0x%p type 0x%d inum 0x%x "
70420036fe5Segillett 			    "count 0x%x\n", rdip, hdlp->ih_type, hdlp->ih_inum,
70520036fe5Segillett 			    hdlp->ih_scratch1);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 			return (ret);
7087c478bd9Sstevel@tonic-gate 		}
7097c478bd9Sstevel@tonic-gate 
71020036fe5Segillett 		if ((hdlp->ih_type == DDI_INTR_TYPE_MSIX) &&
71120036fe5Segillett 		    (i_ddi_get_msix(rdip) == NULL)) {
71220036fe5Segillett 			ddi_intr_msix_t		*msix_p;
71320036fe5Segillett 
71420036fe5Segillett 			if (msix_p = pci_msix_init(rdip)) {
71520036fe5Segillett 				i_ddi_set_msix(rdip, msix_p);
71620036fe5Segillett 				break;
71720036fe5Segillett 			}
71820036fe5Segillett 
71920036fe5Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI-X allocation "
72020036fe5Segillett 			    "failed, rdip 0x%p inum 0x%x\n", rdip,
72120036fe5Segillett 			    hdlp->ih_inum);
72220036fe5Segillett 
72320036fe5Segillett 			(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
72420036fe5Segillett 			    hdlp->ih_scratch1);
72520036fe5Segillett 
72620036fe5Segillett 			return (DDI_FAILURE);
72720036fe5Segillett 		}
72820036fe5Segillett 
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
73195003185Segillett 		(void) pci_msi_disable_mode(rdip, hdlp->ih_type, NULL);
7327c478bd9Sstevel@tonic-gate 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
73320036fe5Segillett 
73420036fe5Segillett 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
73520036fe5Segillett 			goto msi_free;
73620036fe5Segillett 
73720036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
73820036fe5Segillett 			break;
73920036fe5Segillett 
74020036fe5Segillett 		if (((i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1) == 0) &&
74120036fe5Segillett 		    (i_ddi_get_msix(rdip))) {
74220036fe5Segillett 			pci_msix_fini(i_ddi_get_msix(rdip));
74320036fe5Segillett 			i_ddi_set_msix(rdip, NULL);
74420036fe5Segillett 		}
74520036fe5Segillett msi_free:
7467c478bd9Sstevel@tonic-gate 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
7477c478bd9Sstevel@tonic-gate 		    hdlp->ih_scratch1);
7487c478bd9Sstevel@tonic-gate 		break;
7497c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
7507c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_pri ?
7517c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri : px_class_to_pil(rdip);
7527c478bd9Sstevel@tonic-gate 		break;
7537c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
7547c478bd9Sstevel@tonic-gate 		break;
7557c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
7567c478bd9Sstevel@tonic-gate 		if ((ret = px_msi_get_msinum(px_p, hdlp->ih_dip,
7577c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, &msi_num)) != DDI_SUCCESS)
7587c478bd9Sstevel@tonic-gate 			return (ret);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
7619c75c6bfSgovinda 		    msiq_rec_type, msi_num, &msiq_id)) != DDI_SUCCESS) {
7627c478bd9Sstevel@tonic-gate 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
7637c478bd9Sstevel@tonic-gate 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
7647c478bd9Sstevel@tonic-gate 			return (ret);
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
7709c75c6bfSgovinda 		    msiq_id, msi_type)) != DDI_SUCCESS) {
7717c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
7729c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
7737c478bd9Sstevel@tonic-gate 			return (ret);
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
7777c478bd9Sstevel@tonic-gate 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
7787c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
7799c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
7807c478bd9Sstevel@tonic-gate 			return (ret);
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = msi_num;
7847c478bd9Sstevel@tonic-gate 		break;
7857c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DUPVEC:
78620036fe5Segillett 		DBG(DBG_INTROPS, dip, "px_msix_ops: dupisr - inum: %x, "
78720036fe5Segillett 		    "new_vector: %x\n", hdlp->ih_inum, hdlp->ih_scratch1);
78820036fe5Segillett 
78920036fe5Segillett 		ret = pci_msix_dup(hdlp->ih_dip, hdlp->ih_inum,
79020036fe5Segillett 		    hdlp->ih_scratch1);
7917c478bd9Sstevel@tonic-gate 		break;
7927c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
7937c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
7967c478bd9Sstevel@tonic-gate 		    &msiq_id)) != DDI_SUCCESS)
7977c478bd9Sstevel@tonic-gate 			return (ret);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
8009c75c6bfSgovinda 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
8017c478bd9Sstevel@tonic-gate 			return (ret);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		ret = px_rem_msiq_intr(dip, rdip,
8049c75c6bfSgovinda 		    hdlp, msiq_rec_type, msi_num, msiq_id);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = 0;
8077c478bd9Sstevel@tonic-gate 		break;
8087c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
8097c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
8127c478bd9Sstevel@tonic-gate 		    PCI_MSI_VALID)) != DDI_SUCCESS)
8137c478bd9Sstevel@tonic-gate 			return (ret);
8147c478bd9Sstevel@tonic-gate 
81595003185Segillett 		if ((pci_is_msi_enabled(rdip, hdlp->ih_type) != DDI_SUCCESS) ||
81695003185Segillett 		    (hdlp->ih_type == DDI_INTR_TYPE_MSIX)) {
8177c478bd9Sstevel@tonic-gate 			nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 			if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
8209c75c6bfSgovinda 			    nintrs, hdlp->ih_inum, msi_addr,
82195003185Segillett 			    hdlp->ih_type == DDI_INTR_TYPE_MSIX ?
82295003185Segillett 			    msi_num : msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
8237c478bd9Sstevel@tonic-gate 				return (ret);
8247c478bd9Sstevel@tonic-gate 
82595003185Segillett 			if ((ret = pci_msi_enable_mode(rdip, hdlp->ih_type))
82695003185Segillett 			    != DDI_SUCCESS)
8277c478bd9Sstevel@tonic-gate 				return (ret);
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 
83036fe4a92Segillett 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
83136fe4a92Segillett 		    hdlp->ih_inum)) != DDI_SUCCESS)
83236fe4a92Segillett 			return (ret);
83336fe4a92Segillett 
83420036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
83520036fe5Segillett 			break;
83620036fe5Segillett 
83736fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
83836fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
83936fe4a92Segillett 			return (ret);
84036fe4a92Segillett 
84136fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
842*b0fc0e77Sgovinda 		    px_msiqid_to_devino(px_p, msiq_id), hdlp->ih_pri,
843*b0fc0e77Sgovinda 		    PX_INTR_STATE_ENABLE, msiq_rec_type, msi_num);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		break;
8467c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
8477c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
8507c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
8517c478bd9Sstevel@tonic-gate 			return (ret);
8527c478bd9Sstevel@tonic-gate 
85336fe4a92Segillett 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
85436fe4a92Segillett 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
85536fe4a92Segillett 			return (ret);
85636fe4a92Segillett 
85720036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
85820036fe5Segillett 			break;
85920036fe5Segillett 
86036fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
86136fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
86236fe4a92Segillett 			return (ret);
86336fe4a92Segillett 
86436fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip,
86536fe4a92Segillett 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
866*b0fc0e77Sgovinda 		    hdlp->ih_pri, PX_INTR_STATE_DISABLE, msiq_rec_type,
867*b0fc0e77Sgovinda 		    msi_num);
86836fe4a92Segillett 
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKENABLE:
8717c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
8727c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
8759c75c6bfSgovinda 		    nintrs, hdlp->ih_inum, msi_addr,
8767c478bd9Sstevel@tonic-gate 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
8777c478bd9Sstevel@tonic-gate 			return (ret);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
8807c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
8817c478bd9Sstevel@tonic-gate 			    PCI_MSI_VALID)) != DDI_SUCCESS)
8827c478bd9Sstevel@tonic-gate 				return (ret);
88336fe4a92Segillett 
88436fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
88536fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
88636fe4a92Segillett 				return (ret);
88736fe4a92Segillett 
88836fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
88936fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
890*b0fc0e77Sgovinda 			    msiq_id), hdlp->ih_pri, PX_INTR_STATE_ENABLE,
891*b0fc0e77Sgovinda 			    msiq_rec_type, msi_num)) != DDI_SUCCESS)
89236fe4a92Segillett 				return (ret);
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 
89595003185Segillett 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type);
8967c478bd9Sstevel@tonic-gate 		break;
8977c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKDISABLE:
8987c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
8997c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type,
90295003185Segillett 		    hdlp->ih_cap & DDI_INTR_FLAG_BLOCK)) != DDI_SUCCESS)
9037c478bd9Sstevel@tonic-gate 			return (ret);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
9067c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
9077c478bd9Sstevel@tonic-gate 			    PCI_MSI_INVALID)) != DDI_SUCCESS)
9087c478bd9Sstevel@tonic-gate 				return (ret);
90936fe4a92Segillett 
91036fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
91136fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
91236fe4a92Segillett 				return (ret);
91336fe4a92Segillett 
91436fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
91536fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
916*b0fc0e77Sgovinda 			    msiq_id), hdlp->ih_pri, PX_INTR_STATE_DISABLE,
917*b0fc0e77Sgovinda 			    msiq_rec_type, msi_num)) != DDI_SUCCESS)
91836fe4a92Segillett 				return (ret);
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		break;
9227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
9237c478bd9Sstevel@tonic-gate 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
9247c478bd9Sstevel@tonic-gate 		break;
9257c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
9267c478bd9Sstevel@tonic-gate 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
9277c478bd9Sstevel@tonic-gate 		break;
9287c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
9297c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
9307c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, (int *)result);
9317c478bd9Sstevel@tonic-gate 		break;
9327c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
9337c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
9347c478bd9Sstevel@tonic-gate 		break;
9357c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
9367c478bd9Sstevel@tonic-gate 		/* XXX - a new interface may be needed */
9377c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
9387c478bd9Sstevel@tonic-gate 		break;
9397c478bd9Sstevel@tonic-gate 	default:
9407c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
9417c478bd9Sstevel@tonic-gate 		break;
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	return (ret);
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9476d44af1bSesolom static struct {
9486d44af1bSesolom 	kstat_named_t pxintr_ks_name;
9496d44af1bSesolom 	kstat_named_t pxintr_ks_type;
9506d44af1bSesolom 	kstat_named_t pxintr_ks_cpu;
9516d44af1bSesolom 	kstat_named_t pxintr_ks_pil;
9526d44af1bSesolom 	kstat_named_t pxintr_ks_time;
9536d44af1bSesolom 	kstat_named_t pxintr_ks_ino;
9546d44af1bSesolom 	kstat_named_t pxintr_ks_cookie;
9556d44af1bSesolom 	kstat_named_t pxintr_ks_devpath;
9566d44af1bSesolom 	kstat_named_t pxintr_ks_buspath;
9576d44af1bSesolom } pxintr_ks_template = {
9586d44af1bSesolom 	{ "name",	KSTAT_DATA_CHAR },
9596d44af1bSesolom 	{ "type",	KSTAT_DATA_CHAR },
9606d44af1bSesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
9616d44af1bSesolom 	{ "pil",	KSTAT_DATA_UINT64 },
9626d44af1bSesolom 	{ "time",	KSTAT_DATA_UINT64 },
9636d44af1bSesolom 	{ "ino",	KSTAT_DATA_UINT64 },
9646d44af1bSesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
9656d44af1bSesolom 	{ "devpath",	KSTAT_DATA_STRING },
9666d44af1bSesolom 	{ "buspath",	KSTAT_DATA_STRING },
9676d44af1bSesolom };
9686d44af1bSesolom 
9696d44af1bSesolom static uint32_t pxintr_ks_instance;
970d48713b8Sesolom static char ih_devpath[MAXPATHLEN];
971d48713b8Sesolom static char ih_buspath[MAXPATHLEN];
9726d44af1bSesolom kmutex_t pxintr_ks_template_lock;
9736d44af1bSesolom 
9746d44af1bSesolom int
9756d44af1bSesolom px_ks_update(kstat_t *ksp, int rw)
9766d44af1bSesolom {
9776d44af1bSesolom 	px_ih_t *ih_p = ksp->ks_private;
9786d44af1bSesolom 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
979*b0fc0e77Sgovinda 	px_ino_pil_t *ipil_p = ih_p->ih_ipil_p;
980*b0fc0e77Sgovinda 	px_ino_t *ino_p = ipil_p->ipil_ino_p;
981*b0fc0e77Sgovinda 	px_t *px_p = ino_p->ino_ib_p->ib_px_p;
9826d44af1bSesolom 	devino_t ino;
9836d44af1bSesolom 	sysino_t sysino;
9846d44af1bSesolom 
985*b0fc0e77Sgovinda 	ino = ino_p->ino_ino;
9866d44af1bSesolom 	(void) px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino);
9876d44af1bSesolom 
9886d44af1bSesolom 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
9896d44af1bSesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
9906d44af1bSesolom 	    ddi_get_instance(ih_p->ih_dip));
9916d44af1bSesolom 
9926d44af1bSesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
9936d44af1bSesolom 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
9946d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
9956d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
9966d44af1bSesolom 
997e1d9f4e6Sschwartz 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
998e1d9f4e6Sschwartz 
999e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
1000e1d9f4e6Sschwartz 		    (ih_p->ih_rec_type == 0) ? "fixed" : "msi");
1001*b0fc0e77Sgovinda 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = ino_p->ino_cpuid;
1002*b0fc0e77Sgovinda 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = ipil_p->ipil_pil;
1003e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
1004e1d9f4e6Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
1005*b0fc0e77Sgovinda 		    ino_p->ino_cpuid);
1006e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
1007e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
1008e1d9f4e6Sschwartz 	} else {
1009e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
1010e1d9f4e6Sschwartz 		    "disabled");
1011e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
1012e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
1013e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
1014e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
1015e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
1016e1d9f4e6Sschwartz 	}
10176d44af1bSesolom 	return (0);
10186d44af1bSesolom }
10196d44af1bSesolom 
10206d44af1bSesolom void
10216d44af1bSesolom px_create_intr_kstats(px_ih_t *ih_p)
10226d44af1bSesolom {
10236d44af1bSesolom 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
10246d44af1bSesolom 
10256d44af1bSesolom 	ASSERT(ih_p->ih_ksp == NULL);
10266d44af1bSesolom 
10276d44af1bSesolom 	/*
10286d44af1bSesolom 	 * Create pci_intrs::: kstats for all ih types except messages,
10296d44af1bSesolom 	 * which represent unusual conditions and don't need to be tracked.
10306d44af1bSesolom 	 */
10316d44af1bSesolom 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
10326d44af1bSesolom 		ih_p->ih_ksp = kstat_create("pci_intrs",
10336d44af1bSesolom 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
10346d44af1bSesolom 		    "interrupts", KSTAT_TYPE_NAMED,
10356d44af1bSesolom 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
10366d44af1bSesolom 		    KSTAT_FLAG_VIRTUAL);
10376d44af1bSesolom 	}
10386d44af1bSesolom 	if (ih_p->ih_ksp != NULL) {
10396d44af1bSesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
10406d44af1bSesolom 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
10416d44af1bSesolom 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
10426d44af1bSesolom 		ih_p->ih_ksp->ks_private = ih_p;
10436d44af1bSesolom 		ih_p->ih_ksp->ks_update = px_ks_update;
10446d44af1bSesolom 	}
10456d44af1bSesolom }
10466d44af1bSesolom 
1047a195726fSgovinda /*
1048a195726fSgovinda  * px_add_intx_intr:
1049a195726fSgovinda  *
1050a195726fSgovinda  * This function is called to register INTx and legacy hardware
1051a195726fSgovinda  * interrupt pins interrupts.
1052a195726fSgovinda  */
10537c478bd9Sstevel@tonic-gate int
10547c478bd9Sstevel@tonic-gate px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
10557c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
10587c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
10597c478bd9Sstevel@tonic-gate 	devino_t	ino;
10607c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
1061*b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1062*b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p, *ipil_list;
10637c478bd9Sstevel@tonic-gate 	int32_t		weight;
10647c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
10697c478bd9Sstevel@tonic-gate 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
10707c478bd9Sstevel@tonic-gate 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
10717c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
10747c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10777c478bd9Sstevel@tonic-gate 
1078*b0fc0e77Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
1079*b0fc0e77Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p : NULL;
1080*b0fc0e77Sgovinda 
1081*b0fc0e77Sgovinda 	/* Sharing ino */
1082*b0fc0e77Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
1083*b0fc0e77Sgovinda 		if (px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, 0, 0)) {
10847c478bd9Sstevel@tonic-gate 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
1085*b0fc0e77Sgovinda 			    "dup intr #%d\n", hdlp->ih_inum);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
10887c478bd9Sstevel@tonic-gate 			goto fail1;
10897c478bd9Sstevel@tonic-gate 		}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
10927c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
10937c478bd9Sstevel@tonic-gate 
1094*b0fc0e77Sgovinda 		if ((ret = px_ib_ino_add_intr(px_p, ipil_p,
1095*b0fc0e77Sgovinda 		    ih_p)) != DDI_SUCCESS)
10967c478bd9Sstevel@tonic-gate 			goto fail1;
10977c478bd9Sstevel@tonic-gate 
1098*b0fc0e77Sgovinda 		goto ino_done;
1099*b0fc0e77Sgovinda 	}
11007c478bd9Sstevel@tonic-gate 
1101*b0fc0e77Sgovinda 	if (hdlp->ih_pri == 0)
1102*b0fc0e77Sgovinda 		hdlp->ih_pri = px_class_to_pil(rdip);
11037c478bd9Sstevel@tonic-gate 
1104*b0fc0e77Sgovinda 	ipil_p = px_ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
1105*b0fc0e77Sgovinda 	ino_p = ipil_p->ipil_ino_p;
11067c478bd9Sstevel@tonic-gate 
1107*b0fc0e77Sgovinda 	/* Save mondo value in hdlp */
1108*b0fc0e77Sgovinda 	hdlp->ih_vector = ino_p->ino_sysino;
11097c478bd9Sstevel@tonic-gate 
1110*b0fc0e77Sgovinda 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
1111*b0fc0e77Sgovinda 	    hdlp->ih_pri, hdlp->ih_vector);
11127c478bd9Sstevel@tonic-gate 
1113*b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1114*b0fc0e77Sgovinda 	    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ipil_p, NULL);
1115*b0fc0e77Sgovinda 
1116*b0fc0e77Sgovinda 	ret = i_ddi_add_ivintr(hdlp);
11177c478bd9Sstevel@tonic-gate 
1118*b0fc0e77Sgovinda 	/*
1119*b0fc0e77Sgovinda 	 * Restore original interrupt handler
1120*b0fc0e77Sgovinda 	 * and arguments in interrupt handle.
1121*b0fc0e77Sgovinda 	 */
1122*b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1123*b0fc0e77Sgovinda 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
11247c478bd9Sstevel@tonic-gate 
1125*b0fc0e77Sgovinda 	if (ret != DDI_SUCCESS)
1126*b0fc0e77Sgovinda 		goto fail2;
11277c478bd9Sstevel@tonic-gate 
1128*b0fc0e77Sgovinda 	/* Save the pil for this ino */
1129*b0fc0e77Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
1130*b0fc0e77Sgovinda 
1131*b0fc0e77Sgovinda 	/* Select cpu, saving it for sharing and removal */
1132*b0fc0e77Sgovinda 	if (ipil_list == NULL) {
11337c478bd9Sstevel@tonic-gate 		ino_p->ino_cpuid = intr_dist_cpuid();
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
11367c478bd9Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 
1139*b0fc0e77Sgovinda ino_done:
1140*b0fc0e77Sgovinda 	/* Add weight to the cpu that we are already targeting */
11417c478bd9Sstevel@tonic-gate 	weight = px_class_to_intr_weight(rdip);
11427c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
11437c478bd9Sstevel@tonic-gate 
1144*b0fc0e77Sgovinda 	ih_p->ih_ipil_p = ipil_p;
11456d44af1bSesolom 	px_create_intr_kstats(ih_p);
11467c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
11477c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
11487c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
11517c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	return (ret);
11547c478bd9Sstevel@tonic-gate fail2:
1155*b0fc0e77Sgovinda 	px_ib_delete_ino_pil(ib_p, ipil_p);
11567c478bd9Sstevel@tonic-gate fail1:
11577c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
11587c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
11617c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
11647c478bd9Sstevel@tonic-gate 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	return (ret);
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
1169a195726fSgovinda /*
1170a195726fSgovinda  * px_rem_intx_intr:
1171a195726fSgovinda  *
1172a195726fSgovinda  * This function is called to unregister INTx and legacy hardware
1173a195726fSgovinda  * interrupt pins interrupts.
1174a195726fSgovinda  */
11757c478bd9Sstevel@tonic-gate int
11767c478bd9Sstevel@tonic-gate px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
11777c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
11787c478bd9Sstevel@tonic-gate {
11797c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
11807c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
11817c478bd9Sstevel@tonic-gate 	devino_t	ino;
11827c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
1183*b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1184*b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p;
11857c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
11867c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
11917c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
1196*b0fc0e77Sgovinda 	ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
1197*b0fc0e77Sgovinda 	ih_p = px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, 0, 0);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
12007c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
12017c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
12027c478bd9Sstevel@tonic-gate 		goto fail;
12037c478bd9Sstevel@tonic-gate 
1204*b0fc0e77Sgovinda 	if ((ret = px_ib_ino_rem_intr(px_p, ipil_p, ih_p)) != DDI_SUCCESS)
12057c478bd9Sstevel@tonic-gate 		goto fail;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
12087c478bd9Sstevel@tonic-gate 
1209*b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
12107c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
12117c478bd9Sstevel@tonic-gate 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
12127c478bd9Sstevel@tonic-gate 			goto fail;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
12157c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
12167c478bd9Sstevel@tonic-gate 
1217*b0fc0e77Sgovinda 		px_ib_delete_ino_pil(ib_p, ipil_p);
1218*b0fc0e77Sgovinda 	}
1219*b0fc0e77Sgovinda 
1220*b0fc0e77Sgovinda 	if (ino_p->ino_ipil_size == 0) {
1221*b0fc0e77Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
12227c478bd9Sstevel@tonic-gate 	} else {
12237c478bd9Sstevel@tonic-gate 		/* Re-enable interrupt only if mapping regsiter still shared */
1224*b0fc0e77Sgovinda 		PX_INTR_ENABLE(px_p->px_dip, hdlp->ih_vector, curr_cpu);
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate fail:
12287c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12297c478bd9Sstevel@tonic-gate 	return (ret);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
1232a195726fSgovinda /*
1233a195726fSgovinda  * px_add_msiq_intr:
1234a195726fSgovinda  *
1235a195726fSgovinda  * This function is called to register MSI/Xs and PCIe message interrupts.
1236a195726fSgovinda  */
12377c478bd9Sstevel@tonic-gate int
12387c478bd9Sstevel@tonic-gate px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
12397c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
12407c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t *msiq_id_p)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
12437c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
12447c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
12457c478bd9Sstevel@tonic-gate 	devino_t	ino;
12467c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
1247*b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1248*b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p, *ipil_list;
12497c478bd9Sstevel@tonic-gate 	int32_t		weight;
12507c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=%x "
12537c478bd9Sstevel@tonic-gate 	    "arg1=%x arg2=%x\n", ddi_driver_name(rdip), ddi_get_instance(rdip),
12547c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	if ((ret = px_msiq_alloc(px_p, rec_type, msiq_id_p)) != DDI_SUCCESS) {
12577c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
12587c478bd9Sstevel@tonic-gate 		    "msiq allocation failed\n");
12597c478bd9Sstevel@tonic-gate 		return (ret);
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
12657c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
12687c478bd9Sstevel@tonic-gate 
1269*b0fc0e77Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
1270*b0fc0e77Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p : NULL;
1271*b0fc0e77Sgovinda 
1272*b0fc0e77Sgovinda 	/* Sharing ino */
1273*b0fc0e77Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
1274*b0fc0e77Sgovinda 		if (px_ib_intr_locate_ih(ipil_p, rdip,
1275*b0fc0e77Sgovinda 		    hdlp->ih_inum, rec_type, msg_code)) {
12767c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1277*b0fc0e77Sgovinda 			    "dup intr #%d\n", hdlp->ih_inum);
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
12807c478bd9Sstevel@tonic-gate 			goto fail1;
12817c478bd9Sstevel@tonic-gate 		}
12827c478bd9Sstevel@tonic-gate 
1283*b0fc0e77Sgovinda 		/* Save mondo value in hdlp */
1284*b0fc0e77Sgovinda 		hdlp->ih_vector = ino_p->ino_sysino;
1285*b0fc0e77Sgovinda 
1286*b0fc0e77Sgovinda 		if ((ret = px_ib_ino_add_intr(px_p, ipil_p,
1287*b0fc0e77Sgovinda 		    ih_p)) != DDI_SUCCESS)
12887c478bd9Sstevel@tonic-gate 			goto fail1;
12897c478bd9Sstevel@tonic-gate 
1290*b0fc0e77Sgovinda 		goto ino_done;
1291*b0fc0e77Sgovinda 	}
12927c478bd9Sstevel@tonic-gate 
1293*b0fc0e77Sgovinda 	if (hdlp->ih_pri == 0)
1294*b0fc0e77Sgovinda 		hdlp->ih_pri = px_class_to_pil(rdip);
12957c478bd9Sstevel@tonic-gate 
1296*b0fc0e77Sgovinda 	ipil_p = px_ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
1297*b0fc0e77Sgovinda 	ino_p = ipil_p->ipil_ino_p;
12987c478bd9Sstevel@tonic-gate 
1299*b0fc0e77Sgovinda 	ino_p->ino_msiq_p = msiq_state_p->msiq_p +
1300*b0fc0e77Sgovinda 	    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
13017c478bd9Sstevel@tonic-gate 
1302*b0fc0e77Sgovinda 	/* Save mondo value in hdlp */
1303*b0fc0e77Sgovinda 	hdlp->ih_vector = ino_p->ino_sysino;
13047c478bd9Sstevel@tonic-gate 
1305*b0fc0e77Sgovinda 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
1306*b0fc0e77Sgovinda 	    hdlp->ih_pri, hdlp->ih_vector);
13077c478bd9Sstevel@tonic-gate 
1308*b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1309*b0fc0e77Sgovinda 	    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ipil_p, NULL);
1310*b0fc0e77Sgovinda 
1311*b0fc0e77Sgovinda 	ret = i_ddi_add_ivintr(hdlp);
1312*b0fc0e77Sgovinda 
1313*b0fc0e77Sgovinda 	/*
1314*b0fc0e77Sgovinda 	 * Restore original interrupt handler
1315*b0fc0e77Sgovinda 	 * and arguments in interrupt handle.
1316*b0fc0e77Sgovinda 	 */
1317*b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1318*b0fc0e77Sgovinda 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
13197c478bd9Sstevel@tonic-gate 
1320*b0fc0e77Sgovinda 	if (ret != DDI_SUCCESS)
1321*b0fc0e77Sgovinda 		goto fail2;
13227c478bd9Sstevel@tonic-gate 
1323*b0fc0e77Sgovinda 	/* Save the pil for this ino */
1324*b0fc0e77Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
1325*b0fc0e77Sgovinda 
1326*b0fc0e77Sgovinda 	/* Select cpu, saving it for sharing and removal */
1327*b0fc0e77Sgovinda 	if (ipil_list == NULL) {
1328*b0fc0e77Sgovinda 		ino_p->ino_cpuid = intr_dist_cpuid();
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 		/* Enable MSIQ */
13317c478bd9Sstevel@tonic-gate 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
13327c478bd9Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
1335*b0fc0e77Sgovinda 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 
1338*b0fc0e77Sgovinda ino_done:
1339*b0fc0e77Sgovinda 	/* Add weight to the cpu that we are already targeting */
13407c478bd9Sstevel@tonic-gate 	weight = px_class_to_intr_weight(rdip);
13417c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
13427c478bd9Sstevel@tonic-gate 
1343*b0fc0e77Sgovinda 	ih_p->ih_ipil_p = ipil_p;
13446d44af1bSesolom 	px_create_intr_kstats(ih_p);
13457c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
13467c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
13477c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
13507c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	return (ret);
13537c478bd9Sstevel@tonic-gate fail2:
1354*b0fc0e77Sgovinda 	px_ib_delete_ino_pil(ib_p, ipil_p);
13557c478bd9Sstevel@tonic-gate fail1:
13567c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
13577c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
13607c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
13637c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	return (ret);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
1368a195726fSgovinda /*
1369a195726fSgovinda  * px_rem_msiq_intr:
1370a195726fSgovinda  *
1371a195726fSgovinda  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1372a195726fSgovinda  */
13737c478bd9Sstevel@tonic-gate int
13747c478bd9Sstevel@tonic-gate px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
13757c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
13767c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t msiq_id)
13777c478bd9Sstevel@tonic-gate {
13787c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
13797c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
13807c478bd9Sstevel@tonic-gate 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
13817c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
1382*b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1383*b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p;
13847c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
13857c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
13887c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
1393*b0fc0e77Sgovinda 	ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
1394*b0fc0e77Sgovinda 	ih_p = px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, rec_type,
1395*b0fc0e77Sgovinda 	    msg_code);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
13987c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
13997c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
14007c478bd9Sstevel@tonic-gate 		goto fail;
14017c478bd9Sstevel@tonic-gate 
1402*b0fc0e77Sgovinda 	if ((ret = px_ib_ino_rem_intr(px_p, ipil_p, ih_p)) != DDI_SUCCESS)
14037c478bd9Sstevel@tonic-gate 		goto fail;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
14067c478bd9Sstevel@tonic-gate 
1407*b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
14087c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
14097c478bd9Sstevel@tonic-gate 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
14107c478bd9Sstevel@tonic-gate 			goto fail;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
14137c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
14147c478bd9Sstevel@tonic-gate 
1415*b0fc0e77Sgovinda 		px_ib_delete_ino_pil(ib_p, ipil_p);
1416*b0fc0e77Sgovinda 
1417*b0fc0e77Sgovinda 		if (ino_p->ino_ipil_size == 0)
1418*b0fc0e77Sgovinda 			px_lib_msiq_setvalid(dip,
1419*b0fc0e77Sgovinda 			    px_devino_to_msiqid(px_p, ino), PCI_MSIQ_INVALID);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 		(void) px_msiq_free(px_p, msiq_id);
1422*b0fc0e77Sgovinda 	}
1423*b0fc0e77Sgovinda 
1424*b0fc0e77Sgovinda 	if (ino_p->ino_ipil_size == 0) {
1425*b0fc0e77Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
14267c478bd9Sstevel@tonic-gate 	} else {
14277c478bd9Sstevel@tonic-gate 		/* Re-enable interrupt only if mapping regsiter still shared */
1428*b0fc0e77Sgovinda 		PX_INTR_ENABLE(px_p->px_dip, hdlp->ih_vector, curr_cpu);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate fail:
14327c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
14337c478bd9Sstevel@tonic-gate 	return (ret);
14347c478bd9Sstevel@tonic-gate }
1435