xref: /titanic_44/usr/src/uts/sun4/io/px/px_intr.c (revision 226aa357088934c59a99a63765ec45e76cf373b1)
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	new_head_index = msiq_p->msiq_curr_head_idx;
236 	msiqhead_t	*curr_head_p;
237 	msiqtail_t	curr_tail_index;
238 	msgcode_t	msg_code;
239 	px_ih_t		*ih_p;
240 	int		i, ret;
241 	ushort_t	msiq_recs2process;
242 
243 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: msiq_id =%x ino=%x pil=%x "
244 	    "ih_size=%x ih_lst=%x\n", msiq_p->msiq_id, ino_p->ino_ino,
245 	    ino_p->ino_pil, ino_p->ino_ih_size, ino_p->ino_ih_head);
246 
247 	/* Read current MSIQ tail index */
248 	px_lib_msiq_gettail(dip, msiq_p->msiq_id, &curr_tail_index);
249 
250 	if (curr_tail_index < new_head_index)
251 		curr_tail_index += msiq_state_p->msiq_rec_cnt;
252 
253 	/*
254 	 * Calculate the number of recs to process by taking the difference
255 	 * between the head and tail pointers. For all records we always
256 	 * verify that we have a valid record type before we do any processing.
257 	 * If triggered, we should always have at least 1 valid record.
258 	 */
259 	msiq_recs2process = curr_tail_index - new_head_index;
260 
261 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x "
262 	    "rec2process %x\n", new_head_index, msiq_recs2process);
263 
264 	curr_head_p = (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p +
265 	    new_head_index * sizeof (msiq_rec_t));
266 
267 	for (i = 0; i < msiq_recs2process; i++) {
268 		/* Read MSIQ record */
269 		px_lib_get_msiq_rec(dip, curr_head_p, msiq_rec_p);
270 
271 		DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSIQ RECORD, "
272 		    "msiq_rec_type 0x%llx msiq_rec_rid 0x%llx\n",
273 		    msiq_rec_p->msiq_rec_type, msiq_rec_p->msiq_rec_rid);
274 
275 		if (!msiq_rec_p->msiq_rec_type)
276 			break;
277 
278 		/* Check MSIQ record type */
279 		switch (msiq_rec_p->msiq_rec_type) {
280 		case MSG_REC:
281 			msg_code = msiq_rec_p->msiq_rec_data.msg.msg_code;
282 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: PCIE MSG "
283 			    "record, msg type 0x%x\n", msg_code);
284 			break;
285 		case MSI32_REC:
286 		case MSI64_REC:
287 			msg_code = msiq_rec_p->msiq_rec_data.msi.msi_data;
288 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSI record, "
289 			    "msi 0x%x\n", msg_code);
290 
291 			/* Clear MSI state */
292 			px_lib_msi_setstate(dip, (msinum_t)msg_code,
293 			    PCI_MSI_STATE_IDLE);
294 			break;
295 		default:
296 			msg_code = 0;
297 			cmn_err(CE_WARN, "%s%d: px_msiq_intr: 0x%x MSIQ "
298 			    "record type is not supported",
299 			    ddi_driver_name(dip), ddi_get_instance(dip),
300 			    msiq_rec_p->msiq_rec_type);
301 			goto next_rec;
302 		}
303 
304 		/*
305 		 * Scan through px_ih_t linked list, searching for the
306 		 * right px_ih_t, matching MSIQ record data.
307 		 */
308 		for (i = 0, ih_p = ino_p->ino_ih_start;
309 		    ih_p && (i < ino_p->ino_ih_size) &&
310 		    ((ih_p->ih_msg_code != msg_code) ||
311 		    (ih_p->ih_rec_type != msiq_rec_p->msiq_rec_type));
312 		    ih_p = ih_p->ih_next, i++);
313 
314 		if ((ih_p->ih_msg_code == msg_code) &&
315 		    (ih_p->ih_rec_type == msiq_rec_p->msiq_rec_type)) {
316 			dev_info_t *dip = ih_p->ih_dip;
317 			uint_t (*handler)() = ih_p->ih_handler;
318 			caddr_t arg1 = ih_p->ih_handler_arg1;
319 			caddr_t arg2 = ih_p->ih_handler_arg2;
320 
321 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: ino=%x data=%x "
322 			    "handler=%p arg1 =%p arg2=%p\n", ino_p->ino_ino,
323 			    msg_code, handler, arg1, arg2);
324 
325 			DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
326 			    void *, handler, caddr_t, arg1, caddr_t, arg2);
327 
328 			/*
329 			 * Special case for PCIE Error Messages.
330 			 * The current frame work doesn't fit PCIE Err Msgs
331 			 * This should be fixed when PCIE MESSAGES as a whole
332 			 * is architected correctly.
333 			 */
334 			if ((msg_code == PCIE_MSG_CODE_ERR_COR) ||
335 			    (msg_code == PCIE_MSG_CODE_ERR_NONFATAL) ||
336 			    (msg_code == PCIE_MSG_CODE_ERR_FATAL)) {
337 				ret = px_err_fabric_intr(px_p, msg_code,
338 				    msiq_rec_p->msiq_rec_rid);
339 			} else
340 				ret = (*handler)(arg1, arg2);
341 
342 			/*
343 			 * Account for time used by this interrupt. Protect
344 			 * against conflicting writes to ih_ticks from
345 			 * ib_intr_dist_all() by using atomic ops.
346 			 */
347 
348 			if (ino_p->ino_pil <= LOCK_LEVEL)
349 				atomic_add_64(&ih_p->ih_ticks, intr_get_time());
350 
351 			DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
352 			    void *, handler, caddr_t, arg1, int, ret);
353 
354 			new_head_index++;
355 		} else {
356 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr:"
357 			    "No matching MSIQ record found\n");
358 		}
359 next_rec:
360 		/* Get the pointer next EQ record */
361 		curr_head_p = (msiqhead_t *)
362 		    ((caddr_t)curr_head_p + sizeof (msiq_rec_t));
363 
364 		/* Check for overflow condition */
365 		if (curr_head_p >= (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p
366 		    + msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t)))
367 			curr_head_p = (msiqhead_t *)msiq_p->msiq_base_p;
368 
369 		/* Zero out msiq_rec_type field */
370 		msiq_rec_p->msiq_rec_type = 0;
371 	}
372 
373 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: # of MSIQ recs processed %x\n",
374 	    (new_head_index - msiq_p->msiq_curr_head_idx));
375 
376 	if (new_head_index <= msiq_p->msiq_curr_head_idx) {
377 		if (px_unclaimed_intr_block) {
378 			return (px_spurintr(ino_p));
379 		}
380 	}
381 
382 	/*  Update MSIQ head index with no of MSIQ records processed */
383 	if (new_head_index >= msiq_state_p->msiq_rec_cnt)
384 		new_head_index -= msiq_state_p->msiq_rec_cnt;
385 
386 	msiq_p->msiq_curr_head_idx = new_head_index;
387 	px_lib_msiq_sethead(dip, msiq_p->msiq_id, new_head_index);
388 
389 	/* Clear the pending state */
390 	if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
391 	    INTR_IDLE_STATE) != DDI_SUCCESS)
392 		return (DDI_INTR_UNCLAIMED);
393 
394 	return (DDI_INTR_CLAIMED);
395 }
396 
397 dev_info_t *
398 px_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
399 {
400 	dev_info_t	*cdip = rdip;
401 
402 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
403 		;
404 
405 	return (cdip);
406 }
407 
408 /* Default class to pil value mapping */
409 px_class_val_t px_default_pil [] = {
410 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
411 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
412 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
413 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
414 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
415 	{0x050000, 0xff0000, 0x9},	/* Memory Controller */
416 	{0x060000, 0xff0000, 0x9},	/* Bridge Controller */
417 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
418 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
419 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
420 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
421 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
422 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
423 };
424 
425 /*
426  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
427  * entry on or above the pci node like
428  *
429  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
430  *
431  * can be used to augment or override entries in the default table below.
432  *
433  * NB: The values below give NICs preference on redistribution, and provide
434  * NICs some isolation from other interrupt sources. We need better interfaces
435  * that allow the NIC driver to identify a specific NIC instance as high
436  * bandwidth, and thus deserving of separation from other low bandwidth
437  * NICs additional isolation from other interrupt sources.
438  *
439  * NB: We treat Infiniband like a NIC.
440  */
441 px_class_val_t px_default_intr_weight [] = {
442 	{0x020000, 0xff0000, 35},	/* Network Controller */
443 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
444 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
445 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
446 };
447 
448 static uint32_t
449 px_match_class_val(uint32_t key, px_class_val_t *rec_p, int nrec,
450     uint32_t default_val)
451 {
452 	int	i;
453 
454 	for (i = 0; i < nrec; rec_p++, i++) {
455 		if ((rec_p->class_code & rec_p->class_mask) ==
456 		    (key & rec_p->class_mask))
457 			return (rec_p->class_val);
458 	}
459 
460 	return (default_val);
461 }
462 
463 /*
464  * px_class_to_val
465  *
466  * Return the configuration value, based on class code and sub class code,
467  * from the specified property based or default px_class_val_t table.
468  */
469 uint32_t
470 px_class_to_val(dev_info_t *rdip, char *property_name, px_class_val_t *rec_p,
471     int nrec, uint32_t default_val)
472 {
473 	int property_len;
474 	uint32_t class_code;
475 	px_class_val_t *conf;
476 	uint32_t val = default_val;
477 
478 	/*
479 	 * Use the "class-code" property to get the base and sub class
480 	 * codes for the requesting device.
481 	 */
482 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
483 	    DDI_PROP_DONTPASS, "class-code", -1);
484 
485 	if (class_code == -1)
486 		return (val);
487 
488 	/* look up the val from the default table */
489 	val = px_match_class_val(class_code, rec_p, nrec, val);
490 
491 	/* see if there is a more specific property specified value */
492 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
493 	    property_name, (caddr_t)&conf, &property_len))
494 		return (val);
495 
496 	if ((property_len % sizeof (px_class_val_t)) == 0)
497 		val = px_match_class_val(class_code, conf,
498 		    property_len / sizeof (px_class_val_t), val);
499 	kmem_free(conf, property_len);
500 	return (val);
501 }
502 
503 /* px_class_to_pil: return the pil for a given device. */
504 uint32_t
505 px_class_to_pil(dev_info_t *rdip)
506 {
507 	uint32_t pil;
508 
509 	/* default pil is 0 (uninitialized) */
510 	pil = px_class_to_val(rdip,
511 	    "pci-class-priorities", px_default_pil,
512 	    sizeof (px_default_pil) / sizeof (px_class_val_t), 0);
513 
514 	/* range check the result */
515 	if (pil >= 0xf)
516 		pil = 0;
517 
518 	return (pil);
519 }
520 
521 /* px_class_to_intr_weight: return the intr_weight for a given device. */
522 static int32_t
523 px_class_to_intr_weight(dev_info_t *rdip)
524 {
525 	int32_t intr_weight;
526 
527 	/* default weight is 0% */
528 	intr_weight = px_class_to_val(rdip,
529 	    "pci-class-intr-weights", px_default_intr_weight,
530 	    sizeof (px_default_intr_weight) / sizeof (px_class_val_t), 0);
531 
532 	/* range check the result */
533 	if (intr_weight < 0)
534 		intr_weight = 0;
535 	if (intr_weight > 1000)
536 		intr_weight = 1000;
537 
538 	return (intr_weight);
539 }
540 
541 /* ARGSUSED */
542 int
543 px_intx_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
544     ddi_intr_handle_impl_t *hdlp, void *result)
545 {
546 	px_t	*px_p = DIP_TO_STATE(dip);
547 	int	ret = DDI_SUCCESS;
548 
549 	DBG(DBG_INTROPS, dip, "px_intx_ops: dip=%x rdip=%x intr_op=%x "
550 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
551 
552 	switch (intr_op) {
553 	case DDI_INTROP_GETCAP:
554 		ret = pci_intx_get_cap(rdip, (int *)result);
555 		break;
556 	case DDI_INTROP_SETCAP:
557 		DBG(DBG_INTROPS, dip, "px_intx_ops: SetCap is not supported\n");
558 		ret = DDI_ENOTSUP;
559 		break;
560 	case DDI_INTROP_ALLOC:
561 		*(int *)result = hdlp->ih_scratch1;
562 		break;
563 	case DDI_INTROP_FREE:
564 		break;
565 	case DDI_INTROP_GETPRI:
566 		*(int *)result = hdlp->ih_pri ?
567 		    hdlp->ih_pri : px_class_to_pil(rdip);
568 		break;
569 	case DDI_INTROP_SETPRI:
570 		break;
571 	case DDI_INTROP_ADDISR:
572 		ret = px_add_intx_intr(dip, rdip, hdlp);
573 		break;
574 	case DDI_INTROP_REMISR:
575 		ret = px_rem_intx_intr(dip, rdip, hdlp);
576 		break;
577 	case DDI_INTROP_ENABLE:
578 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
579 		    hdlp->ih_vector, PX_INTR_STATE_ENABLE, 0, 0);
580 		break;
581 	case DDI_INTROP_DISABLE:
582 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
583 		    hdlp->ih_vector, PX_INTR_STATE_DISABLE, 0, 0);
584 		break;
585 	case DDI_INTROP_SETMASK:
586 		ret = pci_intx_set_mask(rdip);
587 		break;
588 	case DDI_INTROP_CLRMASK:
589 		ret = pci_intx_clr_mask(rdip);
590 		break;
591 	case DDI_INTROP_GETPENDING:
592 		ret = pci_intx_get_pending(rdip, (int *)result);
593 		break;
594 	case DDI_INTROP_NINTRS:
595 	case DDI_INTROP_NAVAIL:
596 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
597 		break;
598 	default:
599 		ret = DDI_ENOTSUP;
600 		break;
601 	}
602 
603 	return (ret);
604 }
605 
606 /* ARGSUSED */
607 int
608 px_msix_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
609     ddi_intr_handle_impl_t *hdlp, void *result)
610 {
611 	px_t			*px_p = DIP_TO_STATE(dip);
612 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
613 	msiq_rec_type_t		msiq_rec_type;
614 	msi_type_t		msi_type;
615 	uint64_t		msi_addr;
616 	msinum_t		msi_num;
617 	msiqid_t		msiq_id;
618 	uint_t			nintrs;
619 	int			i, ret = DDI_SUCCESS;
620 
621 	DBG(DBG_INTROPS, dip, "px_msix_ops: dip=%x rdip=%x intr_op=%x "
622 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
623 
624 	/* Check for MSI64 support */
625 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
626 		msiq_rec_type = MSI64_REC;
627 		msi_type = MSI64_TYPE;
628 		msi_addr = msi_state_p->msi_addr64;
629 	} else {
630 		msiq_rec_type = MSI32_REC;
631 		msi_type = MSI32_TYPE;
632 		msi_addr = msi_state_p->msi_addr32;
633 	}
634 
635 	switch (intr_op) {
636 	case DDI_INTROP_GETCAP:
637 		ret = pci_msi_get_cap(rdip, hdlp->ih_type, (int *)result);
638 		break;
639 	case DDI_INTROP_SETCAP:
640 		DBG(DBG_INTROPS, dip, "px_msix_ops: SetCap is not supported\n");
641 		ret = DDI_ENOTSUP;
642 		break;
643 	case DDI_INTROP_ALLOC:
644 		/*
645 		 * We need to restrict this allocation in future
646 		 * based on Resource Management policies.
647 		 */
648 		if ((ret = px_msi_alloc(px_p, rdip, hdlp->ih_inum,
649 		    hdlp->ih_scratch1, (uintptr_t)hdlp->ih_scratch2, &msi_num,
650 		    (int *)result)) != DDI_SUCCESS) {
651 			DBG(DBG_INTROPS, dip, "px_msix_ops: allocation "
652 			    "failed, rdip 0x%p type 0x%d inum 0x%x "
653 			    "count 0x%x\n", rdip, hdlp->ih_type, hdlp->ih_inum,
654 			    hdlp->ih_scratch1);
655 
656 			return (ret);
657 		}
658 
659 		if ((hdlp->ih_type == DDI_INTR_TYPE_MSIX) &&
660 		    (i_ddi_get_msix(rdip) == NULL)) {
661 			ddi_intr_msix_t		*msix_p;
662 
663 			if (msix_p = pci_msix_init(rdip)) {
664 				i_ddi_set_msix(rdip, msix_p);
665 				break;
666 			}
667 
668 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI-X allocation "
669 			    "failed, rdip 0x%p inum 0x%x\n", rdip,
670 			    hdlp->ih_inum);
671 
672 			(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
673 			    hdlp->ih_scratch1);
674 
675 			return (DDI_FAILURE);
676 		}
677 
678 		break;
679 	case DDI_INTROP_FREE:
680 		(void) pci_msi_disable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
681 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
682 
683 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
684 			goto msi_free;
685 
686 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
687 			break;
688 
689 		if (((i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1) == 0) &&
690 		    (i_ddi_get_msix(rdip))) {
691 			pci_msix_fini(i_ddi_get_msix(rdip));
692 			i_ddi_set_msix(rdip, NULL);
693 		}
694 msi_free:
695 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
696 		    hdlp->ih_scratch1);
697 		break;
698 	case DDI_INTROP_GETPRI:
699 		*(int *)result = hdlp->ih_pri ?
700 		    hdlp->ih_pri : px_class_to_pil(rdip);
701 		break;
702 	case DDI_INTROP_SETPRI:
703 		break;
704 	case DDI_INTROP_ADDISR:
705 		if ((ret = px_msi_get_msinum(px_p, hdlp->ih_dip,
706 		    hdlp->ih_inum, &msi_num)) != DDI_SUCCESS)
707 			return (ret);
708 
709 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
710 		    msiq_rec_type, msi_num, &msiq_id)) != DDI_SUCCESS) {
711 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
712 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
713 			return (ret);
714 		}
715 
716 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
717 
718 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
719 		    msiq_id, msi_type)) != DDI_SUCCESS) {
720 			(void) px_rem_msiq_intr(dip, rdip,
721 			    hdlp, msiq_rec_type, msi_num, msiq_id);
722 			return (ret);
723 		}
724 
725 		if ((ret = px_lib_msi_setstate(dip, msi_num,
726 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
727 			(void) px_rem_msiq_intr(dip, rdip,
728 			    hdlp, msiq_rec_type, msi_num, msiq_id);
729 			return (ret);
730 		}
731 
732 		hdlp->ih_vector = msi_num;
733 		break;
734 	case DDI_INTROP_DUPVEC:
735 		DBG(DBG_INTROPS, dip, "px_msix_ops: dupisr - inum: %x, "
736 		    "new_vector: %x\n", hdlp->ih_inum, hdlp->ih_scratch1);
737 
738 		ret = pci_msix_dup(hdlp->ih_dip, hdlp->ih_inum,
739 		    hdlp->ih_scratch1);
740 		break;
741 	case DDI_INTROP_REMISR:
742 		msi_num = hdlp->ih_vector;
743 
744 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
745 		    &msiq_id)) != DDI_SUCCESS)
746 			return (ret);
747 
748 		if ((ret = px_lib_msi_setstate(dip, msi_num,
749 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
750 			return (ret);
751 
752 		ret = px_rem_msiq_intr(dip, rdip,
753 		    hdlp, msiq_rec_type, msi_num, msiq_id);
754 
755 		hdlp->ih_vector = 0;
756 		break;
757 	case DDI_INTROP_ENABLE:
758 		msi_num = hdlp->ih_vector;
759 
760 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
761 		    PCI_MSI_VALID)) != DDI_SUCCESS)
762 			return (ret);
763 
764 		if (pci_is_msi_enabled(rdip, hdlp->ih_type) != DDI_SUCCESS) {
765 			nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
766 
767 			if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
768 			    nintrs, hdlp->ih_inum, msi_addr,
769 			    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
770 				return (ret);
771 
772 			if ((ret = pci_msi_enable_mode(rdip, hdlp->ih_type,
773 			    hdlp->ih_inum)) != DDI_SUCCESS)
774 				return (ret);
775 		}
776 
777 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
778 		    hdlp->ih_inum)) != DDI_SUCCESS)
779 			return (ret);
780 
781 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
782 			break;
783 
784 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
785 		    &msiq_id)) != DDI_SUCCESS)
786 			return (ret);
787 
788 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
789 		    px_msiqid_to_devino(px_p, msiq_id), PX_INTR_STATE_ENABLE,
790 		    msiq_rec_type, msi_num);
791 
792 		break;
793 	case DDI_INTROP_DISABLE:
794 		msi_num = hdlp->ih_vector;
795 
796 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
797 		    hdlp->ih_inum)) != DDI_SUCCESS)
798 			return (ret);
799 
800 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
801 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
802 			return (ret);
803 
804 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
805 			break;
806 
807 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
808 		    &msiq_id)) != DDI_SUCCESS)
809 			return (ret);
810 
811 		ret = px_ib_update_intr_state(px_p, rdip,
812 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
813 		    PX_INTR_STATE_DISABLE, msiq_rec_type, msi_num);
814 
815 		break;
816 	case DDI_INTROP_BLOCKENABLE:
817 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
818 		msi_num = hdlp->ih_vector;
819 
820 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
821 		    nintrs, hdlp->ih_inum, msi_addr,
822 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
823 			return (ret);
824 
825 		for (i = 0; i < nintrs; i++, msi_num++) {
826 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
827 			    PCI_MSI_VALID)) != DDI_SUCCESS)
828 				return (ret);
829 
830 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
831 			    &msiq_id)) != DDI_SUCCESS)
832 				return (ret);
833 
834 			if ((ret = px_ib_update_intr_state(px_p, rdip,
835 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
836 			    msiq_id), PX_INTR_STATE_ENABLE, msiq_rec_type,
837 			    msi_num)) != DDI_SUCCESS)
838 				return (ret);
839 		}
840 
841 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
842 		break;
843 	case DDI_INTROP_BLOCKDISABLE:
844 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
845 		msi_num = hdlp->ih_vector;
846 
847 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type,
848 		    hdlp->ih_inum)) != DDI_SUCCESS)
849 			return (ret);
850 
851 		for (i = 0; i < nintrs; i++, msi_num++) {
852 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
853 			    PCI_MSI_INVALID)) != DDI_SUCCESS)
854 				return (ret);
855 
856 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
857 			    &msiq_id)) != DDI_SUCCESS)
858 				return (ret);
859 
860 			if ((ret = px_ib_update_intr_state(px_p, rdip,
861 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
862 			    msiq_id), PX_INTR_STATE_DISABLE, msiq_rec_type,
863 			    msi_num)) != DDI_SUCCESS)
864 				return (ret);
865 		}
866 
867 		break;
868 	case DDI_INTROP_SETMASK:
869 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
870 		break;
871 	case DDI_INTROP_CLRMASK:
872 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
873 		break;
874 	case DDI_INTROP_GETPENDING:
875 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
876 		    hdlp->ih_inum, (int *)result);
877 		break;
878 	case DDI_INTROP_NINTRS:
879 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
880 		break;
881 	case DDI_INTROP_NAVAIL:
882 		/* XXX - a new interface may be needed */
883 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
884 		break;
885 	default:
886 		ret = DDI_ENOTSUP;
887 		break;
888 	}
889 
890 	return (ret);
891 }
892 
893 static struct {
894 	kstat_named_t pxintr_ks_name;
895 	kstat_named_t pxintr_ks_type;
896 	kstat_named_t pxintr_ks_cpu;
897 	kstat_named_t pxintr_ks_pil;
898 	kstat_named_t pxintr_ks_time;
899 	kstat_named_t pxintr_ks_ino;
900 	kstat_named_t pxintr_ks_cookie;
901 	kstat_named_t pxintr_ks_devpath;
902 	kstat_named_t pxintr_ks_buspath;
903 } pxintr_ks_template = {
904 	{ "name",	KSTAT_DATA_CHAR },
905 	{ "type",	KSTAT_DATA_CHAR },
906 	{ "cpu",	KSTAT_DATA_UINT64 },
907 	{ "pil",	KSTAT_DATA_UINT64 },
908 	{ "time",	KSTAT_DATA_UINT64 },
909 	{ "ino",	KSTAT_DATA_UINT64 },
910 	{ "cookie",	KSTAT_DATA_UINT64 },
911 	{ "devpath",	KSTAT_DATA_STRING },
912 	{ "buspath",	KSTAT_DATA_STRING },
913 };
914 
915 static uint32_t pxintr_ks_instance;
916 static char ih_devpath[MAXPATHLEN];
917 static char ih_buspath[MAXPATHLEN];
918 kmutex_t pxintr_ks_template_lock;
919 
920 int
921 px_ks_update(kstat_t *ksp, int rw)
922 {
923 	px_ih_t *ih_p = ksp->ks_private;
924 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
925 	px_ib_t *ib_p = ih_p->ih_ino_p->ino_ib_p;
926 	px_t *px_p = ib_p->ib_px_p;
927 	devino_t ino;
928 	sysino_t sysino;
929 
930 	ino = ih_p->ih_ino_p->ino_ino;
931 	(void) px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino);
932 
933 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
934 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
935 	    ddi_get_instance(ih_p->ih_dip));
936 
937 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
938 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
939 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
940 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
941 
942 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
943 
944 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
945 		    (ih_p->ih_rec_type == 0) ? "fixed" : "msi");
946 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 =
947 		    ih_p->ih_ino_p->ino_cpuid;
948 		pxintr_ks_template.pxintr_ks_pil.value.ui64 =
949 		    ih_p->ih_ino_p->ino_pil;
950 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
951 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
952 			ih_p->ih_ino_p->ino_cpuid);
953 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
954 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
955 	} else {
956 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
957 		    "disabled");
958 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
959 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
960 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
961 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
962 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
963 	}
964 	return (0);
965 }
966 
967 void
968 px_create_intr_kstats(px_ih_t *ih_p)
969 {
970 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
971 
972 	ASSERT(ih_p->ih_ksp == NULL);
973 
974 	/*
975 	 * Create pci_intrs::: kstats for all ih types except messages,
976 	 * which represent unusual conditions and don't need to be tracked.
977 	 */
978 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
979 		ih_p->ih_ksp = kstat_create("pci_intrs",
980 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
981 		    "interrupts", KSTAT_TYPE_NAMED,
982 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
983 		    KSTAT_FLAG_VIRTUAL);
984 	}
985 	if (ih_p->ih_ksp != NULL) {
986 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
987 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
988 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
989 		ih_p->ih_ksp->ks_private = ih_p;
990 		ih_p->ih_ksp->ks_update = px_ks_update;
991 	}
992 }
993 
994 /*
995  * px_add_intx_intr:
996  *
997  * This function is called to register INTx and legacy hardware
998  * interrupt pins interrupts.
999  */
1000 int
1001 px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
1002     ddi_intr_handle_impl_t *hdlp)
1003 {
1004 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1005 	px_ib_t		*ib_p = px_p->px_ib_p;
1006 	devino_t	ino;
1007 	px_ih_t		*ih_p;
1008 	px_ib_ino_info_t *ino_p;
1009 	int32_t		weight;
1010 	int		ret = DDI_SUCCESS;
1011 
1012 	ino = hdlp->ih_vector;
1013 
1014 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
1015 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
1016 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
1017 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
1018 
1019 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
1020 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
1021 
1022 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1023 
1024 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
1025 		uint32_t intr_index = hdlp->ih_inum;
1026 		if (px_ib_ino_locate_intr(ino_p, rdip, intr_index, 0, 0)) {
1027 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
1028 			    "dup intr #%d\n", intr_index);
1029 
1030 			ret = DDI_FAILURE;
1031 			goto fail1;
1032 		}
1033 
1034 		/* Save mondo value in hdlp */
1035 		hdlp->ih_vector = ino_p->ino_sysino;
1036 
1037 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
1038 		    != DDI_SUCCESS)
1039 			goto fail1;
1040 	} else {
1041 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
1042 
1043 		if (hdlp->ih_pri == 0)
1044 			hdlp->ih_pri = px_class_to_pil(rdip);
1045 
1046 		/* Save mondo value in hdlp */
1047 		hdlp->ih_vector = ino_p->ino_sysino;
1048 
1049 		DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
1050 		    hdlp->ih_pri, hdlp->ih_vector);
1051 
1052 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1053 		    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ino_p, NULL);
1054 
1055 		ret = i_ddi_add_ivintr(hdlp);
1056 
1057 		/*
1058 		 * Restore original interrupt handler
1059 		 * and arguments in interrupt handle.
1060 		 */
1061 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1062 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
1063 
1064 		if (ret != DDI_SUCCESS)
1065 			goto fail2;
1066 
1067 		/* Save the pil for this ino */
1068 		ino_p->ino_pil = hdlp->ih_pri;
1069 
1070 		/* select cpu, saving it for sharing and removal */
1071 		ino_p->ino_cpuid = intr_dist_cpuid();
1072 
1073 		/* Enable interrupt */
1074 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
1075 	}
1076 
1077 	/* add weight to the cpu that we are already targeting */
1078 	weight = px_class_to_intr_weight(rdip);
1079 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
1080 
1081 	ih_p->ih_ino_p = ino_p;
1082 	px_create_intr_kstats(ih_p);
1083 	if (ih_p->ih_ksp)
1084 		kstat_install(ih_p->ih_ksp);
1085 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1086 
1087 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
1088 	    ino_p->ino_sysino, hdlp->ih_pri);
1089 
1090 	return (ret);
1091 fail2:
1092 	px_ib_delete_ino(ib_p, ino_p);
1093 fail1:
1094 	if (ih_p->ih_config_handle)
1095 		pci_config_teardown(&ih_p->ih_config_handle);
1096 
1097 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1098 	kmem_free(ih_p, sizeof (px_ih_t));
1099 
1100 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
1101 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
1102 
1103 	return (ret);
1104 }
1105 
1106 /*
1107  * px_rem_intx_intr:
1108  *
1109  * This function is called to unregister INTx and legacy hardware
1110  * interrupt pins interrupts.
1111  */
1112 int
1113 px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
1114     ddi_intr_handle_impl_t *hdlp)
1115 {
1116 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1117 	px_ib_t		*ib_p = px_p->px_ib_p;
1118 	devino_t	ino;
1119 	cpuid_t		curr_cpu;
1120 	px_ib_ino_info_t	*ino_p;
1121 	px_ih_t		*ih_p;
1122 	int		ret = DDI_SUCCESS;
1123 
1124 	ino = hdlp->ih_vector;
1125 
1126 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
1127 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
1128 
1129 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1130 
1131 	ino_p = px_ib_locate_ino(ib_p, ino);
1132 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum, 0, 0);
1133 
1134 	/* Get the current cpu */
1135 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
1136 	    &curr_cpu)) != DDI_SUCCESS)
1137 		goto fail;
1138 
1139 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
1140 		goto fail;
1141 
1142 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
1143 
1144 	if (ino_p->ino_ih_size == 0) {
1145 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
1146 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
1147 			goto fail;
1148 
1149 		hdlp->ih_vector = ino_p->ino_sysino;
1150 		i_ddi_rem_ivintr(hdlp);
1151 
1152 		px_ib_delete_ino(ib_p, ino_p);
1153 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
1154 	} else {
1155 		/* Re-enable interrupt only if mapping regsiter still shared */
1156 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
1157 	}
1158 
1159 fail:
1160 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1161 	return (ret);
1162 }
1163 
1164 /*
1165  * px_add_msiq_intr:
1166  *
1167  * This function is called to register MSI/Xs and PCIe message interrupts.
1168  */
1169 int
1170 px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
1171     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
1172     msgcode_t msg_code, msiqid_t *msiq_id_p)
1173 {
1174 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1175 	px_ib_t		*ib_p = px_p->px_ib_p;
1176 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
1177 	devino_t	ino;
1178 	px_ih_t		*ih_p;
1179 	px_ib_ino_info_t	*ino_p;
1180 	int32_t		weight;
1181 	int		ret = DDI_SUCCESS;
1182 
1183 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=%x "
1184 	    "arg1=%x arg2=%x\n", ddi_driver_name(rdip), ddi_get_instance(rdip),
1185 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
1186 
1187 	if ((ret = px_msiq_alloc(px_p, rec_type, msiq_id_p)) != DDI_SUCCESS) {
1188 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1189 		    "msiq allocation failed\n");
1190 		return (ret);
1191 	}
1192 
1193 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
1194 
1195 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
1196 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
1197 
1198 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1199 
1200 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
1201 		uint32_t intr_index = hdlp->ih_inum;
1202 		if (px_ib_ino_locate_intr(ino_p, rdip,
1203 		    intr_index, rec_type, msg_code)) {
1204 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1205 			    "dup intr #%d\n", intr_index);
1206 
1207 			ret = DDI_FAILURE;
1208 			goto fail1;
1209 		}
1210 
1211 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
1212 		    != DDI_SUCCESS)
1213 			goto fail1;
1214 	} else {
1215 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
1216 
1217 		ino_p->ino_msiq_p = msiq_state_p->msiq_p +
1218 		    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
1219 
1220 		if (hdlp->ih_pri == 0)
1221 			hdlp->ih_pri = px_class_to_pil(rdip);
1222 
1223 		/* Save mondo value in hdlp */
1224 		hdlp->ih_vector = ino_p->ino_sysino;
1225 
1226 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
1227 		    hdlp->ih_pri, hdlp->ih_vector);
1228 
1229 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1230 		    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ino_p, NULL);
1231 
1232 		ret = i_ddi_add_ivintr(hdlp);
1233 
1234 		/*
1235 		 * Restore original interrupt handler
1236 		 * and arguments in interrupt handle.
1237 		 */
1238 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1239 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
1240 
1241 		if (ret != DDI_SUCCESS)
1242 			goto fail2;
1243 
1244 		/* Save the pil for this ino */
1245 		ino_p->ino_pil = hdlp->ih_pri;
1246 
1247 		/* Enable MSIQ */
1248 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
1249 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
1250 
1251 		/* select cpu, saving it for sharing and removal */
1252 		ino_p->ino_cpuid = intr_dist_cpuid();
1253 
1254 		/* Enable interrupt */
1255 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino_p->ino_ino);
1256 	}
1257 
1258 	/* add weight to the cpu that we are already targeting */
1259 	weight = px_class_to_intr_weight(rdip);
1260 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
1261 
1262 	ih_p->ih_ino_p = ino_p;
1263 	px_create_intr_kstats(ih_p);
1264 	if (ih_p->ih_ksp)
1265 		kstat_install(ih_p->ih_ksp);
1266 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1267 
1268 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
1269 	    ino_p->ino_sysino, hdlp->ih_pri);
1270 
1271 	return (ret);
1272 fail2:
1273 	px_ib_delete_ino(ib_p, ino_p);
1274 fail1:
1275 	if (ih_p->ih_config_handle)
1276 		pci_config_teardown(&ih_p->ih_config_handle);
1277 
1278 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1279 	kmem_free(ih_p, sizeof (px_ih_t));
1280 
1281 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
1282 	    ino_p->ino_sysino, hdlp->ih_pri);
1283 
1284 	return (ret);
1285 }
1286 
1287 /*
1288  * px_rem_msiq_intr:
1289  *
1290  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1291  */
1292 int
1293 px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
1294     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
1295     msgcode_t msg_code, msiqid_t msiq_id)
1296 {
1297 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
1298 	px_ib_t		*ib_p = px_p->px_ib_p;
1299 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
1300 	cpuid_t		curr_cpu;
1301 	px_ib_ino_info_t *ino_p;
1302 	px_ih_t		*ih_p;
1303 	int		ret = DDI_SUCCESS;
1304 
1305 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
1306 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
1307 
1308 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1309 
1310 	ino_p = px_ib_locate_ino(ib_p, ino);
1311 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum,
1312 	    rec_type, msg_code);
1313 
1314 	/* Get the current cpu */
1315 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
1316 	    &curr_cpu)) != DDI_SUCCESS)
1317 		goto fail;
1318 
1319 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
1320 		goto fail;
1321 
1322 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
1323 
1324 	if (ino_p->ino_ih_size == 0) {
1325 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
1326 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
1327 			goto fail;
1328 
1329 		px_lib_msiq_setvalid(dip, px_devino_to_msiqid(px_p, ino),
1330 		    PCI_MSIQ_INVALID);
1331 
1332 		hdlp->ih_vector = ino_p->ino_sysino;
1333 		i_ddi_rem_ivintr(hdlp);
1334 
1335 		px_ib_delete_ino(ib_p, ino_p);
1336 
1337 		(void) px_msiq_free(px_p, msiq_id);
1338 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
1339 	} else {
1340 		/* Re-enable interrupt only if mapping regsiter still shared */
1341 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
1342 	}
1343 
1344 fail:
1345 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1346 	return (ret);
1347 }
1348