xref: /illumos-gate/usr/src/uts/sun4/io/px/px_intr.c (revision 03831d35)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PX nexus interrupt handling:
30  *	PX device interrupt handler wrapper
31  *	PIL lookup routine
32  *	PX device interrupt related initchild code
33  */
34 
35 #include <sys/types.h>
36 #include <sys/kmem.h>
37 #include <sys/async.h>
38 #include <sys/spl.h>
39 #include <sys/sunddi.h>
40 #include <sys/fm/protocol.h>
41 #include <sys/fm/util.h>
42 #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
43 #include <sys/ddi_impldefs.h>
44 #include <sys/sdt.h>
45 #include <sys/atomic.h>
46 #include "px_obj.h"
47 #include <sys/ontrap.h>
48 #include <sys/membar.h>
49 #include <sys/clock.h>
50 
51 /*
52  * interrupt jabber:
53  *
54  * When an interrupt line is jabbering, every time the state machine for the
55  * associated ino is idled, a new mondo will be sent and the ino will go into
56  * the pending state again. The mondo will cause a new call to
57  * px_intr_wrapper() which normally idles the ino's state machine which would
58  * precipitate another trip round the loop.
59  *
60  * The loop can be broken by preventing the ino's state machine from being
61  * idled when an interrupt line is jabbering. See the comment at the
62  * beginning of px_intr_wrapper() explaining how the 'interrupt jabber
63  * protection' code does this.
64  */
65 
66 /*LINTLIBRARY*/
67 
68 /*
69  * If the unclaimed interrupt count has reached the limit set by
70  * pci_unclaimed_intr_max within the time limit, then all interrupts
71  * on this ino is blocked by not idling the interrupt state machine.
72  */
73 static int
74 px_spurintr(px_ib_ino_info_t *ino_p)
75 {
76 	px_ih_t	*ih_p = ino_p->ino_ih_start;
77 	px_t	*px_p = ino_p->ino_ib_p->ib_px_p;
78 	char	*err_fmt_str;
79 	int	i;
80 
81 	if (ino_p->ino_unclaimed > px_unclaimed_intr_max)
82 		return (DDI_INTR_CLAIMED);
83 
84 	if (!ino_p->ino_unclaimed)
85 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
86 
87 	ino_p->ino_unclaimed++;
88 
89 	if (ino_p->ino_unclaimed <= px_unclaimed_intr_max)
90 		goto clear;
91 
92 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
93 	    > px_spurintr_duration) {
94 		ino_p->ino_unclaimed = 0;
95 		goto clear;
96 	}
97 	err_fmt_str = "%s%d: ino 0x%x blocked";
98 	goto warn;
99 clear:
100 	/* Clear the pending state */
101 	if (px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
102 	    INTR_IDLE_STATE) != DDI_SUCCESS)
103 		return (DDI_INTR_UNCLAIMED);
104 
105 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
106 warn:
107 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(px_p->px_dip), ino_p->ino_ino);
108 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next)
109 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
110 		    ih_p->ih_inum);
111 	cmn_err(CE_CONT, "!\n");
112 	return (DDI_INTR_CLAIMED);
113 }
114 
115 extern uint64_t intr_get_time(void);
116 
117 /*
118  * px_intx_intr (INTx or legacy interrupt handler)
119  *
120  * This routine is used as wrapper around interrupt handlers installed by child
121  * device drivers.  This routine invokes the driver interrupt handlers and
122  * examines the return codes.
123  *
124  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
125  * least one handler claims the interrupt then the counter is halved and the
126  * interrupt state machine is idled. If no handler claims the interrupt then
127  * the counter is incremented by one and the state machine is idled.
128  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
129  * then the interrupt state machine is not idled thus preventing any further
130  * interrupts on that ino. The state machine will only be idled again if a
131  * handler is subsequently added or removed.
132  *
133  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
134  * DDI_INTR_UNCLAIMED otherwise.
135  */
136 uint_t
137 px_intx_intr(caddr_t arg)
138 {
139 	px_ib_ino_info_t *ino_p = (px_ib_ino_info_t *)arg;
140 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
141 	px_ih_t		*ih_p = ino_p->ino_ih_start;
142 	uint_t		result = 0, r;
143 	int		i;
144 
145 	DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
146 	    "ino=%x sysino=%llx pil=%x ih_size=%x ih_lst=%x\n",
147 	    ino_p->ino_ino, ino_p->ino_sysino, ino_p->ino_pil,
148 	    ino_p->ino_ih_size, ino_p->ino_ih_head);
149 
150 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next) {
151 		dev_info_t *dip = ih_p->ih_dip;
152 		uint_t (*handler)() = ih_p->ih_handler;
153 		caddr_t arg1 = ih_p->ih_handler_arg1;
154 		caddr_t arg2 = ih_p->ih_handler_arg2;
155 
156 		if (ih_p->ih_intr_state == PX_INTR_STATE_DISABLE) {
157 			DBG(DBG_INTX_INTR, px_p->px_dip,
158 			    "px_intx_intr: %s%d interrupt %d is disabled\n",
159 			    ddi_driver_name(dip), ddi_get_instance(dip),
160 			    ino_p->ino_ino);
161 
162 			continue;
163 		}
164 
165 		DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
166 		    "ino=%x handler=%p arg1 =%p arg2 = %p\n",
167 		    ino_p->ino_ino, handler, arg1, arg2);
168 
169 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
170 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
171 
172 		r = (*handler)(arg1, arg2);
173 
174 		/*
175 		 * Account for time used by this interrupt. Protect against
176 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
177 		 * using atomic ops.
178 		 */
179 
180 		if (ino_p->ino_pil <= LOCK_LEVEL)
181 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
182 
183 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
184 		    void *, handler, caddr_t, arg1, int, r);
185 
186 		result += r;
187 
188 		if (px_check_all_handlers)
189 			continue;
190 		if (result)
191 			break;
192 	}
193 
194 	if (!result && px_unclaimed_intr_block)
195 		return (px_spurintr(ino_p));
196 
197 	ino_p->ino_unclaimed = 0;
198 
199 	/* Clear the pending state */
200 	if (px_lib_intr_setstate(ino_p->ino_ib_p->ib_px_p->px_dip,
201 	    ino_p->ino_sysino, INTR_IDLE_STATE) != DDI_SUCCESS)
202 		return (DDI_INTR_UNCLAIMED);
203 
204 	return (DDI_INTR_CLAIMED);
205 }
206 
207 /*
208  * px_msiq_intr (MSI/X or PCIe MSG interrupt handler)
209  *
210  * This routine is used as wrapper around interrupt handlers installed by child
211  * device drivers.  This routine invokes the driver interrupt handlers and
212  * examines the return codes.
213  *
214  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
215  * least one handler claims the interrupt then the counter is halved and the
216  * interrupt state machine is idled. If no handler claims the interrupt then
217  * the counter is incremented by one and the state machine is idled.
218  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
219  * then the interrupt state machine is not idled thus preventing any further
220  * interrupts on that ino. The state machine will only be idled again if a
221  * handler is subsequently added or removed.
222  *
223  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
224  * DDI_INTR_UNCLAIMED otherwise.
225  */
226 uint_t
227 px_msiq_intr(caddr_t arg)
228 {
229 	px_ib_ino_info_t	*ino_p = (px_ib_ino_info_t *)arg;
230 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
231 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
232 	px_msiq_t	*msiq_p = ino_p->ino_msiq_p;
233 	dev_info_t	*dip = px_p->px_dip;
234 	msiq_rec_t	msiq_rec, *msiq_rec_p = &msiq_rec;
235 	msiqhead_t	curr_msiq_rec_cnt, new_msiq_rec_cnt;
236 	msgcode_t	msg_code;
237 	px_ih_t		*ih_p;
238 	int		i, ret;
239 
240 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: msiq_id =%x ino=%x pil=%x "
241 	    "ih_size=%x ih_lst=%x\n", msiq_p->msiq_id, ino_p->ino_ino,
242 	    ino_p->ino_pil, ino_p->ino_ih_size, ino_p->ino_ih_head);
243 
244 	/* Read current MSIQ head index */
245 	px_lib_msiq_gethead(dip, msiq_p->msiq_id, &curr_msiq_rec_cnt);
246 	msiq_p->msiq_curr = (uint64_t)((caddr_t)msiq_p->msiq_base +
247 	    curr_msiq_rec_cnt * sizeof (msiq_rec_t));
248 	new_msiq_rec_cnt = curr_msiq_rec_cnt;
249 
250 	/* Read next MSIQ record */
251 	px_lib_get_msiq_rec(dip, msiq_p, msiq_rec_p);
252 
253 	/*
254 	 * Process current MSIQ record as long as record type
255 	 * field is non-zero.
256 	 */
257 	while (msiq_rec_p->msiq_rec_type) {
258 		DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSIQ RECORD, "
259 		    "msiq_rec_type 0x%llx msiq_rec_rid 0x%llx\n",
260 		    msiq_rec_p->msiq_rec_type, msiq_rec_p->msiq_rec_rid);
261 
262 		/* Get the pointer next EQ record */
263 		msiq_p->msiq_curr = (uint64_t)
264 		    ((caddr_t)msiq_p->msiq_curr + sizeof (msiq_rec_t));
265 
266 		/* Check for overflow condition */
267 		if (msiq_p->msiq_curr >= (uint64_t)((caddr_t)msiq_p->msiq_base +
268 		    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t)))
269 			msiq_p->msiq_curr = msiq_p->msiq_base;
270 
271 		/* Check MSIQ record type */
272 		switch (msiq_rec_p->msiq_rec_type) {
273 		case MSG_REC:
274 			msg_code = msiq_rec_p->msiq_rec_data.msg.msg_code;
275 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: PCIE MSG "
276 			    "record, msg type 0x%x\n", msg_code);
277 			break;
278 		case MSI32_REC:
279 		case MSI64_REC:
280 			msg_code = msiq_rec_p->msiq_rec_data.msi.msi_data;
281 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSI record, "
282 			    "msi 0x%x\n", msg_code);
283 
284 			/* Clear MSI state */
285 			px_lib_msi_setstate(dip, (msinum_t)msg_code,
286 			    PCI_MSI_STATE_IDLE);
287 			break;
288 		default:
289 			msg_code = 0;
290 			cmn_err(CE_WARN, "%s%d: px_msiq_intr: 0x%x MSIQ "
291 			    "record type is not supported",
292 			    ddi_driver_name(dip), ddi_get_instance(dip),
293 			    msiq_rec_p->msiq_rec_type);
294 			goto next_rec;
295 		}
296 
297 		/*
298 		 * Scan through px_ih_t linked list, searching for the
299 		 * right px_ih_t, matching MSIQ record data.
300 		 */
301 		for (i = 0, ih_p = ino_p->ino_ih_start;
302 		    ih_p && (i < ino_p->ino_ih_size) &&
303 		    ((ih_p->ih_msg_code != msg_code) ||
304 		    (ih_p->ih_rec_type != msiq_rec_p->msiq_rec_type));
305 		    ih_p = ih_p->ih_next, i++);
306 
307 		if ((ih_p->ih_msg_code == msg_code) &&
308 		    (ih_p->ih_rec_type == msiq_rec_p->msiq_rec_type)) {
309 			dev_info_t *dip = ih_p->ih_dip;
310 			uint_t (*handler)() = ih_p->ih_handler;
311 			caddr_t arg1 = ih_p->ih_handler_arg1;
312 			caddr_t arg2 = ih_p->ih_handler_arg2;
313 
314 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: ino=%x data=%x "
315 			    "handler=%p arg1 =%p arg2=%p\n", ino_p->ino_ino,
316 			    msg_code, handler, arg1, arg2);
317 
318 			DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
319 			    void *, handler, caddr_t, arg1, caddr_t, arg2);
320 
321 			/*
322 			 * Special case for PCIE Error Messages.
323 			 * The current frame work doesn't fit PCIE Err Msgs
324 			 * This should be fixed when PCIE MESSAGES as a whole
325 			 * is architected correctly.
326 			 */
327 			if ((msg_code == PCIE_MSG_CODE_ERR_COR) ||
328 			    (msg_code == PCIE_MSG_CODE_ERR_NONFATAL) ||
329 			    (msg_code == PCIE_MSG_CODE_ERR_FATAL)) {
330 				ret = px_err_fabric_intr(px_p, msg_code,
331 				    msiq_rec_p->msiq_rec_rid);
332 			} else
333 				ret = (*handler)(arg1, arg2);
334 
335 			/*
336 			 * Account for time used by this interrupt. Protect
337 			 * against conflicting writes to ih_ticks from
338 			 * ib_intr_dist_all() by using atomic ops.
339 			 */
340 
341 			if (ino_p->ino_pil <= LOCK_LEVEL)
342 				atomic_add_64(&ih_p->ih_ticks, intr_get_time());
343 
344 			DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
345 			    void *, handler, caddr_t, arg1, int, ret);
346 		} else {
347 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr:"
348 			    "Not found matching MSIQ record\n");
349 
350 			/* px_spurintr(ino_p); */
351 			ino_p->ino_unclaimed++;
352 		}
353 
354 next_rec:
355 		new_msiq_rec_cnt++;
356 
357 		/* Zero out msiq_rec_type field */
358 		msiq_rec_p->msiq_rec_type = 0;
359 
360 		/* Read next MSIQ record */
361 		px_lib_get_msiq_rec(dip, msiq_p, msiq_rec_p);
362 	}
363 
364 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: No of MSIQ recs processed %x\n",
365 	    (new_msiq_rec_cnt - curr_msiq_rec_cnt));
366 
367 	/*  Update MSIQ head index with no of MSIQ records processed */
368 	if (new_msiq_rec_cnt > curr_msiq_rec_cnt)  {
369 		if (new_msiq_rec_cnt >= msiq_state_p->msiq_rec_cnt)
370 			new_msiq_rec_cnt -= msiq_state_p->msiq_rec_cnt;
371 
372 		px_lib_msiq_sethead(dip, msiq_p->msiq_id, new_msiq_rec_cnt);
373 	}
374 
375 	/* Clear the pending state */
376 	if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
377 	    INTR_IDLE_STATE) != DDI_SUCCESS)
378 		return (DDI_INTR_UNCLAIMED);
379 
380 	return (DDI_INTR_CLAIMED);
381 }
382 
383 dev_info_t *
384 px_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
385 {
386 	dev_info_t	*cdip = rdip;
387 
388 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
389 		;
390 
391 	return (cdip);
392 }
393 
394 /* Default class to pil value mapping */
395 px_class_val_t px_default_pil [] = {
396 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
397 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
398 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
399 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
400 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
401 	{0x050000, 0xff0000, 0x9},	/* Memory Controller */
402 	{0x060000, 0xff0000, 0x9},	/* Bridge Controller */
403 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
404 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
405 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
406 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
407 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
408 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
409 };
410 
411 /*
412  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
413  * entry on or above the pci node like
414  *
415  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
416  *
417  * can be used to augment or override entries in the default table below.
418  *
419  * NB: The values below give NICs preference on redistribution, and provide
420  * NICs some isolation from other interrupt sources. We need better interfaces
421  * that allow the NIC driver to identify a specific NIC instance as high
422  * bandwidth, and thus deserving of separation from other low bandwidth
423  * NICs additional isolation from other interrupt sources.
424  *
425  * NB: We treat Infiniband like a NIC.
426  */
427 px_class_val_t px_default_intr_weight [] = {
428 	{0x020000, 0xff0000, 35},	/* Network Controller */
429 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
430 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
431 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
432 };
433 
434 static uint32_t
435 px_match_class_val(uint32_t key, px_class_val_t *rec_p, int nrec,
436     uint32_t default_val)
437 {
438 	int	i;
439 
440 	for (i = 0; i < nrec; rec_p++, i++) {
441 		if ((rec_p->class_code & rec_p->class_mask) ==
442 		    (key & rec_p->class_mask))
443 			return (rec_p->class_val);
444 	}
445 
446 	return (default_val);
447 }
448 
449 /*
450  * px_class_to_val
451  *
452  * Return the configuration value, based on class code and sub class code,
453  * from the specified property based or default px_class_val_t table.
454  */
455 uint32_t
456 px_class_to_val(dev_info_t *rdip, char *property_name, px_class_val_t *rec_p,
457     int nrec, uint32_t default_val)
458 {
459 	int property_len;
460 	uint32_t class_code;
461 	px_class_val_t *conf;
462 	uint32_t val = default_val;
463 
464 	/*
465 	 * Use the "class-code" property to get the base and sub class
466 	 * codes for the requesting device.
467 	 */
468 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
469 	    DDI_PROP_DONTPASS, "class-code", -1);
470 
471 	if (class_code == -1)
472 		return (val);
473 
474 	/* look up the val from the default table */
475 	val = px_match_class_val(class_code, rec_p, nrec, val);
476 
477 	/* see if there is a more specific property specified value */
478 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
479 	    property_name, (caddr_t)&conf, &property_len))
480 		return (val);
481 
482 	if ((property_len % sizeof (px_class_val_t)) == 0)
483 		val = px_match_class_val(class_code, conf,
484 		    property_len / sizeof (px_class_val_t), val);
485 	kmem_free(conf, property_len);
486 	return (val);
487 }
488 
489 /* px_class_to_pil: return the pil for a given device. */
490 uint32_t
491 px_class_to_pil(dev_info_t *rdip)
492 {
493 	uint32_t pil;
494 
495 	/* default pil is 0 (uninitialized) */
496 	pil = px_class_to_val(rdip,
497 	    "pci-class-priorities", px_default_pil,
498 	    sizeof (px_default_pil) / sizeof (px_class_val_t), 0);
499 
500 	/* range check the result */
501 	if (pil >= 0xf)
502 		pil = 0;
503 
504 	return (pil);
505 }
506 
507 /* px_class_to_intr_weight: return the intr_weight for a given device. */
508 static int32_t
509 px_class_to_intr_weight(dev_info_t *rdip)
510 {
511 	int32_t intr_weight;
512 
513 	/* default weight is 0% */
514 	intr_weight = px_class_to_val(rdip,
515 	    "pci-class-intr-weights", px_default_intr_weight,
516 	    sizeof (px_default_intr_weight) / sizeof (px_class_val_t), 0);
517 
518 	/* range check the result */
519 	if (intr_weight < 0)
520 		intr_weight = 0;
521 	if (intr_weight > 1000)
522 		intr_weight = 1000;
523 
524 	return (intr_weight);
525 }
526 
527 /* ARGSUSED */
528 int
529 px_intx_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
530     ddi_intr_handle_impl_t *hdlp, void *result)
531 {
532 	px_t	*px_p = DIP_TO_STATE(dip);
533 	int	ret = DDI_SUCCESS;
534 
535 	DBG(DBG_INTROPS, dip, "px_intx_ops: dip=%x rdip=%x intr_op=%x "
536 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
537 
538 	switch (intr_op) {
539 	case DDI_INTROP_GETCAP:
540 		ret = pci_intx_get_cap(rdip, (int *)result);
541 		break;
542 	case DDI_INTROP_SETCAP:
543 		DBG(DBG_INTROPS, dip, "px_intx_ops: SetCap is not supported\n");
544 		ret = DDI_ENOTSUP;
545 		break;
546 	case DDI_INTROP_ALLOC:
547 		*(int *)result = hdlp->ih_scratch1;
548 		break;
549 	case DDI_INTROP_FREE:
550 		break;
551 	case DDI_INTROP_GETPRI:
552 		*(int *)result = hdlp->ih_pri ?
553 		    hdlp->ih_pri : px_class_to_pil(rdip);
554 		break;
555 	case DDI_INTROP_SETPRI:
556 		break;
557 	case DDI_INTROP_ADDISR:
558 		ret = px_add_intx_intr(dip, rdip, hdlp);
559 		break;
560 	case DDI_INTROP_REMISR:
561 		ret = px_rem_intx_intr(dip, rdip, hdlp);
562 		break;
563 	case DDI_INTROP_ENABLE:
564 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
565 		    hdlp->ih_vector, PX_INTR_STATE_ENABLE, 0, 0);
566 		break;
567 	case DDI_INTROP_DISABLE:
568 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
569 		    hdlp->ih_vector, PX_INTR_STATE_DISABLE, 0, 0);
570 		break;
571 	case DDI_INTROP_SETMASK:
572 		ret = pci_intx_set_mask(rdip);
573 		break;
574 	case DDI_INTROP_CLRMASK:
575 		ret = pci_intx_clr_mask(rdip);
576 		break;
577 	case DDI_INTROP_GETPENDING:
578 		ret = pci_intx_get_pending(rdip, (int *)result);
579 		break;
580 	case DDI_INTROP_NINTRS:
581 	case DDI_INTROP_NAVAIL:
582 		*(int *)result = i_ddi_get_nintrs(rdip);
583 		break;
584 	default:
585 		ret = DDI_ENOTSUP;
586 		break;
587 	}
588 
589 	return (ret);
590 }
591 
592 /* ARGSUSED */
593 int
594 px_msix_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
595     ddi_intr_handle_impl_t *hdlp, void *result)
596 {
597 	px_t			*px_p = DIP_TO_STATE(dip);
598 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
599 	msiq_rec_type_t		msiq_rec_type;
600 	msi_type_t		msi_type;
601 	uint64_t		msi_addr;
602 	msinum_t		msi_num;
603 	msiqid_t		msiq_id;
604 	uint_t			nintrs;
605 	int			i, ret = DDI_SUCCESS;
606 
607 	DBG(DBG_INTROPS, dip, "px_msix_ops: dip=%x rdip=%x intr_op=%x "
608 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
609 
610 	/* Check for MSI64 support */
611 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
612 		msiq_rec_type = MSI64_REC;
613 		msi_type = MSI64_TYPE;
614 		msi_addr = msi_state_p->msi_addr64;
615 	} else {
616 		msiq_rec_type = MSI32_REC;
617 		msi_type = MSI32_TYPE;
618 		msi_addr = msi_state_p->msi_addr32;
619 	}
620 
621 	switch (intr_op) {
622 	case DDI_INTROP_GETCAP:
623 		ret = pci_msi_get_cap(rdip, hdlp->ih_type, (int *)result);
624 		break;
625 	case DDI_INTROP_SETCAP:
626 		DBG(DBG_INTROPS, dip, "px_msix_ops: SetCap is not supported\n");
627 		ret = DDI_ENOTSUP;
628 		break;
629 	case DDI_INTROP_ALLOC:
630 		/*
631 		 * We need to restrict this allocation in future
632 		 * based on Resource Management policies.
633 		 */
634 		if ((ret = px_msi_alloc(px_p, rdip, hdlp->ih_inum,
635 		    hdlp->ih_scratch1, (int)hdlp->ih_scratch2, &msi_num,
636 		    (int *)result)) != DDI_SUCCESS) {
637 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI allocation "
638 			    "failed, rdip 0x%p inum 0x%x count 0x%x\n",
639 			    rdip, hdlp->ih_inum, hdlp->ih_scratch1);
640 
641 			return (ret);
642 		}
643 
644 		break;
645 	case DDI_INTROP_FREE:
646 		(void) pci_msi_disable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
647 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
648 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
649 		    hdlp->ih_scratch1);
650 		break;
651 	case DDI_INTROP_GETPRI:
652 		*(int *)result = hdlp->ih_pri ?
653 		    hdlp->ih_pri : px_class_to_pil(rdip);
654 		break;
655 	case DDI_INTROP_SETPRI:
656 		break;
657 	case DDI_INTROP_ADDISR:
658 		if ((ret = px_msi_get_msinum(px_p, hdlp->ih_dip,
659 		    hdlp->ih_inum, &msi_num)) != DDI_SUCCESS)
660 			return (ret);
661 
662 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
663 		    msiq_rec_type, msi_num, &msiq_id)) != DDI_SUCCESS) {
664 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
665 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
666 			return (ret);
667 		}
668 
669 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
670 
671 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
672 		    msiq_id, msi_type)) != DDI_SUCCESS) {
673 			(void) px_rem_msiq_intr(dip, rdip,
674 			    hdlp, msiq_rec_type, msi_num, msiq_id);
675 			return (ret);
676 		}
677 
678 		if ((ret = px_lib_msi_setstate(dip, msi_num,
679 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
680 			(void) px_rem_msiq_intr(dip, rdip,
681 			    hdlp, msiq_rec_type, msi_num, msiq_id);
682 			return (ret);
683 		}
684 
685 		hdlp->ih_vector = msi_num;
686 		break;
687 	case DDI_INTROP_DUPVEC:
688 		DBG(DBG_INTROPS, dip, "px_msix_ops: DupIsr is not supported\n");
689 		ret = DDI_ENOTSUP;
690 		break;
691 	case DDI_INTROP_REMISR:
692 		msi_num = hdlp->ih_vector;
693 
694 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
695 		    &msiq_id)) != DDI_SUCCESS)
696 			return (ret);
697 
698 		if ((ret = px_lib_msi_setstate(dip, msi_num,
699 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
700 			return (ret);
701 
702 		ret = px_rem_msiq_intr(dip, rdip,
703 		    hdlp, msiq_rec_type, msi_num, msiq_id);
704 
705 		hdlp->ih_vector = 0;
706 		break;
707 	case DDI_INTROP_ENABLE:
708 		msi_num = hdlp->ih_vector;
709 
710 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
711 		    PCI_MSI_VALID)) != DDI_SUCCESS)
712 			return (ret);
713 
714 		if (pci_is_msi_enabled(rdip, hdlp->ih_type) != DDI_SUCCESS) {
715 			nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
716 
717 			if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
718 			    nintrs, hdlp->ih_inum, msi_addr,
719 			    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
720 				return (ret);
721 
722 			if ((ret = pci_msi_enable_mode(rdip, hdlp->ih_type,
723 			    hdlp->ih_inum)) != DDI_SUCCESS)
724 				return (ret);
725 		}
726 
727 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
728 		    hdlp->ih_inum)) != DDI_SUCCESS)
729 			return (ret);
730 
731 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
732 		    &msiq_id)) != DDI_SUCCESS)
733 			return (ret);
734 
735 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
736 		    px_msiqid_to_devino(px_p, msiq_id), PX_INTR_STATE_ENABLE,
737 		    msiq_rec_type, msi_num);
738 
739 		break;
740 	case DDI_INTROP_DISABLE:
741 		msi_num = hdlp->ih_vector;
742 
743 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
744 		    hdlp->ih_inum)) != DDI_SUCCESS)
745 			return (ret);
746 
747 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
748 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
749 			return (ret);
750 
751 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
752 		    &msiq_id)) != DDI_SUCCESS)
753 			return (ret);
754 
755 		ret = px_ib_update_intr_state(px_p, rdip,
756 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
757 		    PX_INTR_STATE_DISABLE, msiq_rec_type, msi_num);
758 
759 		break;
760 	case DDI_INTROP_BLOCKENABLE:
761 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
762 		msi_num = hdlp->ih_vector;
763 
764 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
765 		    nintrs, hdlp->ih_inum, msi_addr,
766 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
767 			return (ret);
768 
769 		for (i = 0; i < nintrs; i++, msi_num++) {
770 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
771 			    PCI_MSI_VALID)) != DDI_SUCCESS)
772 				return (ret);
773 
774 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
775 			    &msiq_id)) != DDI_SUCCESS)
776 				return (ret);
777 
778 			if ((ret = px_ib_update_intr_state(px_p, rdip,
779 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
780 			    msiq_id), PX_INTR_STATE_ENABLE, msiq_rec_type,
781 			    msi_num)) != DDI_SUCCESS)
782 				return (ret);
783 		}
784 
785 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
786 		break;
787 	case DDI_INTROP_BLOCKDISABLE:
788 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
789 		msi_num = hdlp->ih_vector;
790 
791 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type,
792 		    hdlp->ih_inum)) != DDI_SUCCESS)
793 			return (ret);
794 
795 		for (i = 0; i < nintrs; i++, msi_num++) {
796 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
797 			    PCI_MSI_INVALID)) != DDI_SUCCESS)
798 				return (ret);
799 
800 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
801 			    &msiq_id)) != DDI_SUCCESS)
802 				return (ret);
803 
804 			if ((ret = px_ib_update_intr_state(px_p, rdip,
805 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
806 			    msiq_id), PX_INTR_STATE_DISABLE, msiq_rec_type,
807 			    msi_num)) != DDI_SUCCESS)
808 				return (ret);
809 		}
810 
811 		break;
812 	case DDI_INTROP_SETMASK:
813 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
814 		break;
815 	case DDI_INTROP_CLRMASK:
816 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
817 		break;
818 	case DDI_INTROP_GETPENDING:
819 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
820 		    hdlp->ih_inum, (int *)result);
821 		break;
822 	case DDI_INTROP_NINTRS:
823 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
824 		break;
825 	case DDI_INTROP_NAVAIL:
826 		/* XXX - a new interface may be needed */
827 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
828 		break;
829 	default:
830 		ret = DDI_ENOTSUP;
831 		break;
832 	}
833 
834 	return (ret);
835 }
836 
837 static struct {
838 	kstat_named_t pxintr_ks_name;
839 	kstat_named_t pxintr_ks_type;
840 	kstat_named_t pxintr_ks_cpu;
841 	kstat_named_t pxintr_ks_pil;
842 	kstat_named_t pxintr_ks_time;
843 	kstat_named_t pxintr_ks_ino;
844 	kstat_named_t pxintr_ks_cookie;
845 	kstat_named_t pxintr_ks_devpath;
846 	kstat_named_t pxintr_ks_buspath;
847 } pxintr_ks_template = {
848 	{ "name",	KSTAT_DATA_CHAR },
849 	{ "type",	KSTAT_DATA_CHAR },
850 	{ "cpu",	KSTAT_DATA_UINT64 },
851 	{ "pil",	KSTAT_DATA_UINT64 },
852 	{ "time",	KSTAT_DATA_UINT64 },
853 	{ "ino",	KSTAT_DATA_UINT64 },
854 	{ "cookie",	KSTAT_DATA_UINT64 },
855 	{ "devpath",	KSTAT_DATA_STRING },
856 	{ "buspath",	KSTAT_DATA_STRING },
857 };
858 
859 static uint32_t pxintr_ks_instance;
860 kmutex_t pxintr_ks_template_lock;
861 
862 int
863 px_ks_update(kstat_t *ksp, int rw)
864 {
865 	px_ih_t *ih_p = ksp->ks_private;
866 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
867 	px_ib_t *ib_p = ih_p->ih_ino_p->ino_ib_p;
868 	px_t *px_p = ib_p->ib_px_p;
869 	devino_t ino;
870 	sysino_t sysino;
871 	char ih_devpath[MAXPATHLEN];
872 	char ih_buspath[MAXPATHLEN];
873 
874 	ino = ih_p->ih_ino_p->ino_ino;
875 	(void) px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino);
876 
877 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
878 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
879 	    ddi_get_instance(ih_p->ih_dip));
880 
881 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
882 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
883 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
884 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
885 
886 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
887 
888 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
889 		    (ih_p->ih_rec_type == 0) ? "fixed" : "msi");
890 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 =
891 		    ih_p->ih_ino_p->ino_cpuid;
892 		pxintr_ks_template.pxintr_ks_pil.value.ui64 =
893 		    ih_p->ih_ino_p->ino_pil;
894 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
895 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
896 			ih_p->ih_ino_p->ino_cpuid);
897 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
898 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
899 	} else {
900 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
901 		    "disabled");
902 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
903 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
904 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
905 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
906 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
907 	}
908 	return (0);
909 }
910 
911 void
912 px_create_intr_kstats(px_ih_t *ih_p)
913 {
914 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
915 
916 	ASSERT(ih_p->ih_ksp == NULL);
917 
918 	/*
919 	 * Create pci_intrs::: kstats for all ih types except messages,
920 	 * which represent unusual conditions and don't need to be tracked.
921 	 */
922 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
923 		ih_p->ih_ksp = kstat_create("pci_intrs",
924 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
925 		    "interrupts", KSTAT_TYPE_NAMED,
926 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
927 		    KSTAT_FLAG_VIRTUAL);
928 	}
929 	if (ih_p->ih_ksp != NULL) {
930 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
931 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
932 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
933 		ih_p->ih_ksp->ks_private = ih_p;
934 		ih_p->ih_ksp->ks_update = px_ks_update;
935 	}
936 }
937 
938 /*
939  * px_add_intx_intr:
940  *
941  * This function is called to register INTx and legacy hardware
942  * interrupt pins interrupts.
943  */
944 int
945 px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
946     ddi_intr_handle_impl_t *hdlp)
947 {
948 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
949 	px_ib_t		*ib_p = px_p->px_ib_p;
950 	devino_t	ino;
951 	px_ih_t		*ih_p;
952 	px_ib_ino_info_t *ino_p;
953 	int32_t		weight;
954 	int		ret = DDI_SUCCESS;
955 
956 	ino = hdlp->ih_vector;
957 
958 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
959 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
960 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
961 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
962 
963 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
964 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
965 
966 	mutex_enter(&ib_p->ib_ino_lst_mutex);
967 
968 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
969 		uint32_t intr_index = hdlp->ih_inum;
970 		if (px_ib_ino_locate_intr(ino_p, rdip, intr_index, 0, 0)) {
971 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
972 			    "dup intr #%d\n", intr_index);
973 
974 			ret = DDI_FAILURE;
975 			goto fail1;
976 		}
977 
978 		/* Save mondo value in hdlp */
979 		hdlp->ih_vector = ino_p->ino_sysino;
980 
981 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
982 		    != DDI_SUCCESS)
983 			goto fail1;
984 	} else {
985 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
986 
987 		if (hdlp->ih_pri == 0)
988 			hdlp->ih_pri = px_class_to_pil(rdip);
989 
990 		/* Save mondo value in hdlp */
991 		hdlp->ih_vector = ino_p->ino_sysino;
992 
993 		DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
994 		    hdlp->ih_pri, hdlp->ih_vector);
995 
996 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
997 		    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ino_p, NULL);
998 
999 		ret = i_ddi_add_ivintr(hdlp);
1000 
1001 		/*
1002 		 * Restore original interrupt handler
1003 		 * and arguments in interrupt handle.
1004 		 */
1005 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1006 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
1007 
1008 		if (ret != DDI_SUCCESS)
1009 			goto fail2;
1010 
1011 		/* Save the pil for this ino */
1012 		ino_p->ino_pil = hdlp->ih_pri;
1013 
1014 		/* select cpu, saving it for sharing and removal */
1015 		ino_p->ino_cpuid = intr_dist_cpuid();
1016 
1017 		/* Enable interrupt */
1018 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
1019 	}
1020 
1021 	/* add weight to the cpu that we are already targeting */
1022 	weight = px_class_to_intr_weight(rdip);
1023 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
1024 
1025 	ih_p->ih_ino_p = ino_p;
1026 	px_create_intr_kstats(ih_p);
1027 	if (ih_p->ih_ksp)
1028 		kstat_install(ih_p->ih_ksp);
1029 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1030 
1031 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
1032 	    ino_p->ino_sysino, hdlp->ih_pri);
1033 
1034 	return (ret);
1035 fail2:
1036 	px_ib_delete_ino(ib_p, ino_p);
1037 fail1:
1038 	if (ih_p->ih_config_handle)
1039 		pci_config_teardown(&ih_p->ih_config_handle);
1040 
1041 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1042 	kmem_free(ih_p, sizeof (px_ih_t));
1043 
1044 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
1045 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
1046 
1047 	return (ret);
1048 }
1049 
1050 /*
1051  * px_rem_intx_intr:
1052  *
1053  * This function is called to unregister INTx and legacy hardware
1054  * interrupt pins interrupts.
1055  */
1056 int
1057 px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
1058     ddi_intr_handle_impl_t *hdlp)
1059 {
1060 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1061 	px_ib_t		*ib_p = px_p->px_ib_p;
1062 	devino_t	ino;
1063 	cpuid_t		curr_cpu;
1064 	px_ib_ino_info_t	*ino_p;
1065 	px_ih_t		*ih_p;
1066 	int		ret = DDI_SUCCESS;
1067 
1068 	ino = hdlp->ih_vector;
1069 
1070 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
1071 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
1072 
1073 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1074 
1075 	ino_p = px_ib_locate_ino(ib_p, ino);
1076 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum, 0, 0);
1077 
1078 	/* Get the current cpu */
1079 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
1080 	    &curr_cpu)) != DDI_SUCCESS)
1081 		goto fail;
1082 
1083 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
1084 		goto fail;
1085 
1086 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
1087 
1088 	if (ino_p->ino_ih_size == 0) {
1089 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
1090 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
1091 			goto fail;
1092 
1093 		hdlp->ih_vector = ino_p->ino_sysino;
1094 		i_ddi_rem_ivintr(hdlp);
1095 
1096 		px_ib_delete_ino(ib_p, ino_p);
1097 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
1098 	} else {
1099 		/* Re-enable interrupt only if mapping regsiter still shared */
1100 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
1101 	}
1102 
1103 fail:
1104 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1105 	return (ret);
1106 }
1107 
1108 /*
1109  * px_add_msiq_intr:
1110  *
1111  * This function is called to register MSI/Xs and PCIe message interrupts.
1112  */
1113 int
1114 px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
1115     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
1116     msgcode_t msg_code, msiqid_t *msiq_id_p)
1117 {
1118 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1119 	px_ib_t		*ib_p = px_p->px_ib_p;
1120 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
1121 	devino_t	ino;
1122 	px_ih_t		*ih_p;
1123 	px_ib_ino_info_t	*ino_p;
1124 	int32_t		weight;
1125 	int		ret = DDI_SUCCESS;
1126 
1127 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=%x "
1128 	    "arg1=%x arg2=%x\n", ddi_driver_name(rdip), ddi_get_instance(rdip),
1129 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
1130 
1131 	if ((ret = px_msiq_alloc(px_p, rec_type, msiq_id_p)) != DDI_SUCCESS) {
1132 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1133 		    "msiq allocation failed\n");
1134 		return (ret);
1135 	}
1136 
1137 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
1138 
1139 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
1140 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
1141 
1142 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1143 
1144 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
1145 		uint32_t intr_index = hdlp->ih_inum;
1146 		if (px_ib_ino_locate_intr(ino_p, rdip,
1147 		    intr_index, rec_type, msg_code)) {
1148 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1149 			    "dup intr #%d\n", intr_index);
1150 
1151 			ret = DDI_FAILURE;
1152 			goto fail1;
1153 		}
1154 
1155 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
1156 		    != DDI_SUCCESS)
1157 			goto fail1;
1158 	} else {
1159 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
1160 
1161 		ino_p->ino_msiq_p = msiq_state_p->msiq_p +
1162 		    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
1163 
1164 		if (hdlp->ih_pri == 0)
1165 			hdlp->ih_pri = px_class_to_pil(rdip);
1166 
1167 		/* Save mondo value in hdlp */
1168 		hdlp->ih_vector = ino_p->ino_sysino;
1169 
1170 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
1171 		    hdlp->ih_pri, hdlp->ih_vector);
1172 
1173 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1174 		    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ino_p, NULL);
1175 
1176 		ret = i_ddi_add_ivintr(hdlp);
1177 
1178 		/*
1179 		 * Restore original interrupt handler
1180 		 * and arguments in interrupt handle.
1181 		 */
1182 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1183 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
1184 
1185 		if (ret != DDI_SUCCESS)
1186 			goto fail2;
1187 
1188 		/* Save the pil for this ino */
1189 		ino_p->ino_pil = hdlp->ih_pri;
1190 
1191 		/* Enable MSIQ */
1192 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
1193 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
1194 
1195 		/* select cpu, saving it for sharing and removal */
1196 		ino_p->ino_cpuid = intr_dist_cpuid();
1197 
1198 		/* Enable interrupt */
1199 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino_p->ino_ino);
1200 	}
1201 
1202 	/* add weight to the cpu that we are already targeting */
1203 	weight = px_class_to_intr_weight(rdip);
1204 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
1205 
1206 	ih_p->ih_ino_p = ino_p;
1207 	px_create_intr_kstats(ih_p);
1208 	if (ih_p->ih_ksp)
1209 		kstat_install(ih_p->ih_ksp);
1210 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1211 
1212 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
1213 	    ino_p->ino_sysino, hdlp->ih_pri);
1214 
1215 	return (ret);
1216 fail2:
1217 	px_ib_delete_ino(ib_p, ino_p);
1218 fail1:
1219 	if (ih_p->ih_config_handle)
1220 		pci_config_teardown(&ih_p->ih_config_handle);
1221 
1222 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1223 	kmem_free(ih_p, sizeof (px_ih_t));
1224 
1225 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
1226 	    ino_p->ino_sysino, hdlp->ih_pri);
1227 
1228 	return (ret);
1229 }
1230 
1231 /*
1232  * px_rem_msiq_intr:
1233  *
1234  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1235  */
1236 int
1237 px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
1238     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
1239     msgcode_t msg_code, msiqid_t msiq_id)
1240 {
1241 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1242 	px_ib_t		*ib_p = px_p->px_ib_p;
1243 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
1244 	cpuid_t		curr_cpu;
1245 	px_ib_ino_info_t *ino_p;
1246 	px_ih_t		*ih_p;
1247 	int		ret = DDI_SUCCESS;
1248 
1249 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
1250 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
1251 
1252 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1253 
1254 	ino_p = px_ib_locate_ino(ib_p, ino);
1255 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum,
1256 	    rec_type, msg_code);
1257 
1258 	/* Get the current cpu */
1259 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
1260 	    &curr_cpu)) != DDI_SUCCESS)
1261 		goto fail;
1262 
1263 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
1264 		goto fail;
1265 
1266 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
1267 
1268 	if (ino_p->ino_ih_size == 0) {
1269 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
1270 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
1271 			goto fail;
1272 
1273 		px_lib_msiq_setvalid(dip, px_devino_to_msiqid(px_p, ino),
1274 		    PCI_MSIQ_INVALID);
1275 
1276 		hdlp->ih_vector = ino_p->ino_sysino;
1277 		i_ddi_rem_ivintr(hdlp);
1278 
1279 		px_ib_delete_ino(ib_p, ino_p);
1280 
1281 		(void) px_msiq_free(px_p, msiq_id);
1282 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
1283 	} else {
1284 		/* Re-enable interrupt only if mapping regsiter still shared */
1285 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
1286 	}
1287 
1288 fail:
1289 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1290 	return (ret);
1291 }
1292