xref: /titanic_44/usr/src/uts/sun4/io/px/px_ib.c (revision 73cc0e021f4115db3085cd78083c42c8be4559e3)
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 2007 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 Interrupt Block implementation
30  */
31 
32 #include <sys/types.h>
33 #include <sys/kmem.h>
34 #include <sys/async.h>
35 #include <sys/systm.h>		/* panicstr */
36 #include <sys/spl.h>
37 #include <sys/sunddi.h>
38 #include <sys/machsystm.h>	/* intr_dist_add */
39 #include <sys/ddi_impldefs.h>
40 #include <sys/cpuvar.h>
41 #include <sys/time.h>
42 #include "px_obj.h"
43 
44 /*LINTLIBRARY*/
45 
46 static void px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight);
47 static void px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p,
48     uint32_t cpu_id);
49 static uint_t px_ib_intr_reset(void *arg);
50 static void px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
51     char *path_name, int instance);
52 
53 extern uint64_t xc_tick_jump_limit;
54 
55 int
56 px_ib_attach(px_t *px_p)
57 {
58 	dev_info_t	*dip = px_p->px_dip;
59 	px_ib_t		*ib_p;
60 	sysino_t	sysino;
61 	px_fault_t	*fault_p = &px_p->px_fault;
62 
63 	DBG(DBG_IB, dip, "px_ib_attach\n");
64 
65 	if (px_lib_intr_devino_to_sysino(px_p->px_dip,
66 	    px_p->px_inos[PX_INTR_PEC], &sysino) != DDI_SUCCESS)
67 		return (DDI_FAILURE);
68 
69 	/*
70 	 * Allocate interrupt block state structure and link it to
71 	 * the px state structure.
72 	 */
73 	ib_p = kmem_zalloc(sizeof (px_ib_t), KM_SLEEP);
74 	px_p->px_ib_p = ib_p;
75 	ib_p->ib_px_p = px_p;
76 	ib_p->ib_ino_lst = (px_ino_t *)NULL;
77 
78 	mutex_init(&ib_p->ib_intr_lock, NULL, MUTEX_DRIVER, NULL);
79 	mutex_init(&ib_p->ib_ino_lst_mutex, NULL, MUTEX_DRIVER, NULL);
80 
81 	bus_func_register(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
82 
83 	intr_dist_add_weighted(px_ib_intr_redist, ib_p);
84 
85 	/*
86 	 * Initialize PEC fault data structure
87 	 */
88 	fault_p->px_fh_dip = dip;
89 	fault_p->px_fh_sysino = sysino;
90 	fault_p->px_err_func = px_err_dmc_pec_intr;
91 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_PEC];
92 
93 	return (DDI_SUCCESS);
94 }
95 
96 void
97 px_ib_detach(px_t *px_p)
98 {
99 	px_ib_t		*ib_p = px_p->px_ib_p;
100 	dev_info_t	*dip = px_p->px_dip;
101 
102 	DBG(DBG_IB, dip, "px_ib_detach\n");
103 
104 	bus_func_unregister(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
105 	intr_dist_rem_weighted(px_ib_intr_redist, ib_p);
106 
107 	mutex_destroy(&ib_p->ib_ino_lst_mutex);
108 	mutex_destroy(&ib_p->ib_intr_lock);
109 
110 	px_ib_free_ino_all(ib_p);
111 
112 	px_p->px_ib_p = NULL;
113 	kmem_free(ib_p, sizeof (px_ib_t));
114 }
115 
116 void
117 px_ib_intr_enable(px_t *px_p, cpuid_t cpu_id, devino_t ino)
118 {
119 	px_ib_t		*ib_p = px_p->px_ib_p;
120 	sysino_t	sysino;
121 
122 	/*
123 	 * Determine the cpu for the interrupt
124 	 */
125 	mutex_enter(&ib_p->ib_intr_lock);
126 
127 	DBG(DBG_IB, px_p->px_dip,
128 	    "px_ib_intr_enable: ino=%x cpu_id=%x\n", ino, cpu_id);
129 
130 	if (px_lib_intr_devino_to_sysino(px_p->px_dip, ino,
131 	    &sysino) != DDI_SUCCESS) {
132 		DBG(DBG_IB, px_p->px_dip,
133 		    "px_ib_intr_enable: px_intr_devino_to_sysino() failed\n");
134 
135 		mutex_exit(&ib_p->ib_intr_lock);
136 		return;
137 	}
138 
139 	PX_INTR_ENABLE(px_p->px_dip, sysino, cpu_id);
140 	px_lib_intr_setstate(px_p->px_dip, sysino, INTR_IDLE_STATE);
141 
142 	mutex_exit(&ib_p->ib_intr_lock);
143 }
144 
145 /*ARGSUSED*/
146 void
147 px_ib_intr_disable(px_ib_t *ib_p, devino_t ino, int wait)
148 {
149 	sysino_t	sysino;
150 
151 	mutex_enter(&ib_p->ib_intr_lock);
152 
153 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_disable: ino=%x\n", ino);
154 
155 	/* Disable the interrupt */
156 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip, ino,
157 	    &sysino) != DDI_SUCCESS) {
158 		DBG(DBG_IB, ib_p->ib_px_p->px_dip,
159 		    "px_ib_intr_disable: px_intr_devino_to_sysino() failed\n");
160 
161 		mutex_exit(&ib_p->ib_intr_lock);
162 		return;
163 	}
164 
165 	PX_INTR_DISABLE(ib_p->ib_px_p->px_dip, sysino);
166 
167 	mutex_exit(&ib_p->ib_intr_lock);
168 }
169 
170 
171 void
172 px_ib_intr_dist_en(dev_info_t *dip, cpuid_t cpu_id, devino_t ino,
173     boolean_t wait_flag)
174 {
175 	uint32_t	old_cpu_id;
176 	sysino_t	sysino;
177 	intr_valid_state_t	enabled = 0;
178 	hrtime_t	start_time, prev, curr, interval, jump;
179 	hrtime_t	intr_timeout;
180 	intr_state_t	intr_state;
181 	int		e = DDI_SUCCESS;
182 
183 	DBG(DBG_IB, dip, "px_ib_intr_dist_en: ino=0x%x\n", ino);
184 
185 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
186 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
187 		    "px_intr_devino_to_sysino() failed, ino 0x%x\n", ino);
188 		return;
189 	}
190 
191 	/* Skip enabling disabled interrupts */
192 	if (px_lib_intr_getvalid(dip, sysino, &enabled) != DDI_SUCCESS) {
193 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: px_intr_getvalid() "
194 		    "failed, sysino 0x%x\n", sysino);
195 		return;
196 	}
197 	if (!enabled)
198 		return;
199 
200 	/* Done if redistributed onto the same cpuid */
201 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
202 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
203 		    "px_intr_gettarget() failed\n");
204 		return;
205 	}
206 	if (cpu_id == old_cpu_id)
207 		return;
208 
209 	if (!wait_flag)
210 		goto done;
211 
212 	/* Busy wait on pending interrupts */
213 	PX_INTR_DISABLE(dip, sysino);
214 
215 	intr_timeout = px_intrpend_timeout;
216 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
217 
218 	for (curr = start_time = gethrtime(); !panicstr &&
219 	    ((e = px_lib_intr_getstate(dip, sysino, &intr_state)) ==
220 	    DDI_SUCCESS) &&
221 	    (intr_state == INTR_DELIVERED_STATE); /* */) {
222 		/*
223 		 * If we have a really large jump in hrtime, it is most
224 		 * probably because we entered the debugger (or OBP,
225 		 * in general). So, we adjust the timeout accordingly
226 		 * to prevent declaring an interrupt timeout. The
227 		 * master-interrupt mechanism in OBP should deliver
228 		 * the interrupts properly.
229 		 */
230 		prev = curr;
231 		curr = gethrtime();
232 		interval = curr - prev;
233 		if (interval > jump)
234 			intr_timeout += interval;
235 		if (curr - start_time > intr_timeout) {
236 			cmn_err(CE_WARN,
237 			    "%s%d: px_ib_intr_dist_en: sysino 0x%lx(ino 0x%x) "
238 			    "from cpu id 0x%x to 0x%x timeout",
239 			    ddi_driver_name(dip), ddi_get_instance(dip),
240 			    sysino, ino, old_cpu_id, cpu_id);
241 
242 			e = DDI_FAILURE;
243 			break;
244 		}
245 	}
246 
247 	if (e != DDI_SUCCESS)
248 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: failed, "
249 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
250 
251 done:
252 	PX_INTR_ENABLE(dip, sysino, cpu_id);
253 }
254 
255 static void
256 px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p, uint32_t cpu_id)
257 {
258 	extern kmutex_t pxintr_ks_template_lock;
259 	hrtime_t ticks;
260 
261 	/*
262 	 * Because we are updating two fields in ih_t we must lock
263 	 * pxintr_ks_template_lock to prevent someone from reading the
264 	 * kstats after we set ih_ticks to 0 and before we increment
265 	 * ih_nsec to compensate.
266 	 *
267 	 * We must also protect against the interrupt arriving and incrementing
268 	 * ih_ticks between the time we read it and when we reset it to 0.
269 	 * To do this we use atomic_swap.
270 	 */
271 
272 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
273 
274 	mutex_enter(&pxintr_ks_template_lock);
275 	ticks = atomic_swap_64(&ih_p->ih_ticks, 0);
276 	ih_p->ih_nsec += (uint64_t)tick2ns(ticks, cpu_id);
277 	mutex_exit(&pxintr_ks_template_lock);
278 }
279 
280 
281 /*
282  * Redistribute interrupts of the specified weight. The first call has a weight
283  * of weight_max, which can be used to trigger initialization for
284  * redistribution. The inos with weight [weight_max, inf.) should be processed
285  * on the "weight == weight_max" call.  This first call is followed by calls
286  * of decreasing weights, inos of that weight should be processed.  The final
287  * call specifies a weight of zero, this can be used to trigger processing of
288  * stragglers.
289  */
290 static void
291 px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight)
292 {
293 	px_ib_t		*ib_p = (px_ib_t *)arg;
294 	px_t		*px_p = ib_p->ib_px_p;
295 	dev_info_t	*dip = px_p->px_dip;
296 	px_ino_t	*ino_p;
297 	px_ino_pil_t	*ipil_p;
298 	px_ih_t		*ih_lst;
299 	int32_t		dweight = 0;
300 	int		i;
301 
302 	/* Redistribute internal interrupts */
303 	if (weight == 0) {
304 		mutex_enter(&ib_p->ib_intr_lock);
305 		px_ib_intr_dist_en(dip, intr_dist_cpuid(),
306 		    px_p->px_inos[PX_INTR_PEC], B_FALSE);
307 		mutex_exit(&ib_p->ib_intr_lock);
308 
309 		px_hp_intr_redist(px_p);
310 	}
311 
312 	/* Redistribute device interrupts */
313 	mutex_enter(&ib_p->ib_ino_lst_mutex);
314 
315 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
316 		uint32_t orig_cpuid;
317 
318 		/*
319 		 * Recomputes the sum of interrupt weights of devices that
320 		 * share the same ino upon first call marked by
321 		 * (weight == weight_max).
322 		 */
323 		if (weight == weight_max) {
324 			ino_p->ino_intr_weight = 0;
325 
326 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
327 			    ipil_p = ipil_p->ipil_next_p) {
328 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
329 				    i < ipil_p->ipil_ih_size; i++,
330 				    ih_lst = ih_lst->ih_next) {
331 					dweight = i_ddi_get_intr_weight(
332 					    ih_lst->ih_dip);
333 					if (dweight > 0)
334 						ino_p->ino_intr_weight +=
335 						    dweight;
336 				}
337 			}
338 		}
339 
340 		/*
341 		 * As part of redistributing weighted interrupts over cpus,
342 		 * nexus redistributes device interrupts and updates
343 		 * cpu weight. The purpose is for the most light weighted
344 		 * cpu to take the next interrupt and gain weight, therefore
345 		 * attention demanding device gains more cpu attention by
346 		 * making itself heavy.
347 		 */
348 		if ((weight == ino_p->ino_intr_weight) ||
349 		    ((weight >= weight_max) &&
350 		    (ino_p->ino_intr_weight >= weight_max))) {
351 			orig_cpuid = ino_p->ino_cpuid;
352 			if (cpu[orig_cpuid] == NULL)
353 				orig_cpuid = CPU->cpu_id;
354 
355 			/* select cpuid to target and mark ino established */
356 			ino_p->ino_cpuid = intr_dist_cpuid();
357 
358 			/* Add device weight to targeted cpu. */
359 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
360 			    ipil_p = ipil_p->ipil_next_p) {
361 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
362 				    i < ipil_p->ipil_ih_size; i++,
363 				    ih_lst = ih_lst->ih_next) {
364 
365 					dweight = i_ddi_get_intr_weight(
366 					    ih_lst->ih_dip);
367 					intr_dist_cpuid_add_device_weight(
368 					    ino_p->ino_cpuid, ih_lst->ih_dip,
369 					    dweight);
370 
371 					/*
372 					 * Different cpus may have different
373 					 * clock speeds. to account for this,
374 					 * whenever an interrupt is moved to a
375 					 * new CPU, we convert the accumulated
376 					 * ticks into nsec, based upon the clock
377 					 * rate of the prior CPU.
378 					 *
379 					 * It is possible that the prior CPU no
380 					 * longer exists. In this case, fall
381 					 * back to using this CPU's clock rate.
382 					 *
383 					 * Note that the value in ih_ticks has
384 					 * already been corrected for any power
385 					 * savings mode which might have been
386 					 * in effect.
387 					 */
388 					px_ib_cpu_ticks_to_ih_nsec(ib_p, ih_lst,
389 					    orig_cpuid);
390 				}
391 			}
392 
393 			/* enable interrupt on new targeted cpu */
394 			px_ib_intr_dist_en(dip, ino_p->ino_cpuid,
395 			    ino_p->ino_ino, B_TRUE);
396 		}
397 	}
398 	mutex_exit(&ib_p->ib_ino_lst_mutex);
399 }
400 
401 /*
402  * Reset interrupts to IDLE.  This function is called during
403  * panic handling after redistributing interrupts; it's needed to
404  * support dumping to network devices after 'sync' from OBP.
405  *
406  * N.B.  This routine runs in a context where all other threads
407  * are permanently suspended.
408  */
409 static uint_t
410 px_ib_intr_reset(void *arg)
411 {
412 	px_ib_t		*ib_p = (px_ib_t *)arg;
413 
414 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_reset\n");
415 
416 	if (px_lib_intr_reset(ib_p->ib_px_p->px_dip) != DDI_SUCCESS)
417 		return (BF_FATAL);
418 
419 	return (BF_NONE);
420 }
421 
422 /*
423  * Locate px_ino_t structure on ib_p->ib_ino_lst according to ino#
424  * returns NULL if not found.
425  */
426 px_ino_t *
427 px_ib_locate_ino(px_ib_t *ib_p, devino_t ino_num)
428 {
429 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
430 
431 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
432 
433 	for (; ino_p && ino_p->ino_ino != ino_num; ino_p = ino_p->ino_next_p)
434 		;
435 
436 	return (ino_p);
437 }
438 
439 px_ino_pil_t *
440 px_ib_new_ino_pil(px_ib_t *ib_p, devino_t ino_num, uint_t pil, px_ih_t *ih_p)
441 {
442 	px_ino_pil_t	*ipil_p = kmem_zalloc(sizeof (px_ino_pil_t), KM_SLEEP);
443 	px_ino_t	*ino_p;
444 
445 	if ((ino_p = px_ib_locate_ino(ib_p, ino_num)) == NULL) {
446 		sysino_t	sysino;
447 
448 		if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip,
449 		    ino_num, &sysino) != DDI_SUCCESS)
450 			return (NULL);
451 
452 		ino_p = kmem_zalloc(sizeof (px_ino_t), KM_SLEEP);
453 
454 		ino_p->ino_next_p = ib_p->ib_ino_lst;
455 		ib_p->ib_ino_lst = ino_p;
456 
457 		ino_p->ino_ino = ino_num;
458 		ino_p->ino_sysino = sysino;
459 		ino_p->ino_ib_p = ib_p;
460 		ino_p->ino_unclaimed_intrs = 0;
461 		ino_p->ino_lopil = pil;
462 	}
463 
464 	ih_p->ih_next = ih_p;
465 	ipil_p->ipil_pil = pil;
466 	ipil_p->ipil_ih_head = ih_p;
467 	ipil_p->ipil_ih_tail = ih_p;
468 	ipil_p->ipil_ih_start = ih_p;
469 	ipil_p->ipil_ih_size = 1;
470 	ipil_p->ipil_ino_p = ino_p;
471 
472 	ipil_p->ipil_next_p = ino_p->ino_ipil_p;
473 	ino_p->ino_ipil_p = ipil_p;
474 	ino_p->ino_ipil_size++;
475 
476 	if (ino_p->ino_lopil > pil)
477 		ino_p->ino_lopil = pil;
478 
479 	return (ipil_p);
480 }
481 
482 void
483 px_ib_delete_ino_pil(px_ib_t *ib_p, px_ino_pil_t *ipil_p)
484 {
485 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
486 	ushort_t	pil = ipil_p->ipil_pil;
487 	px_ino_pil_t	*prev, *next;
488 
489 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
490 
491 	if (ino_p->ino_ipil_p == ipil_p)
492 		ino_p->ino_ipil_p = ipil_p->ipil_next_p;
493 	else {
494 		for (prev = next = ino_p->ino_ipil_p; next != ipil_p;
495 		    prev = next, next = next->ipil_next_p)
496 			;
497 
498 		if (prev)
499 			prev->ipil_next_p = ipil_p->ipil_next_p;
500 	}
501 
502 	kmem_free(ipil_p, sizeof (px_ino_pil_t));
503 
504 	if (ino_p->ino_lopil == pil) {
505 		for (next = ino_p->ino_ipil_p; next;
506 		    next = next->ipil_next_p) {
507 			if (pil > next->ipil_pil)
508 				pil = next->ipil_pil;
509 		}
510 
511 		ino_p->ino_lopil = pil;
512 	}
513 
514 	if (--ino_p->ino_ipil_size)
515 		return;
516 
517 	if (ib_p->ib_ino_lst == ino_p)
518 		ib_p->ib_ino_lst = ino_p->ino_next_p;
519 	else {
520 		px_ino_t	*list = ib_p->ib_ino_lst;
521 
522 		for (; list->ino_next_p != ino_p; list = list->ino_next_p)
523 			;
524 		list->ino_next_p = ino_p->ino_next_p;
525 	}
526 }
527 
528 /*
529  * Free all ino when we are detaching.
530  */
531 void
532 px_ib_free_ino_all(px_ib_t *ib_p)
533 {
534 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
535 	px_ino_t	*next = NULL;
536 
537 	while (ino_p) {
538 		next = ino_p->ino_next_p;
539 		kmem_free(ino_p, sizeof (px_ino_t));
540 		ino_p = next;
541 	}
542 }
543 
544 /*
545  * Locate px_ino_pil_t structure on ino_p->ino_ipil_p according to ino#
546  * returns NULL if not found.
547  */
548 px_ino_pil_t *
549 px_ib_ino_locate_ipil(px_ino_t *ino_p, uint_t pil)
550 {
551 	px_ino_pil_t	*ipil_p = ino_p->ino_ipil_p;
552 
553 	for (; ipil_p && ipil_p->ipil_pil != pil; ipil_p = ipil_p->ipil_next_p)
554 		;
555 
556 	return (ipil_p);
557 }
558 
559 int
560 px_ib_ino_add_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
561 {
562 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
563 	px_ib_t		*ib_p = ino_p->ino_ib_p;
564 	devino_t	ino = ino_p->ino_ino;
565 	sysino_t	sysino = ino_p->ino_sysino;
566 	dev_info_t	*dip = px_p->px_dip;
567 	cpuid_t		curr_cpu;
568 	hrtime_t	start_time;
569 	intr_state_t	intr_state;
570 	int		ret = DDI_SUCCESS;
571 
572 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
573 	ASSERT(ib_p == px_p->px_ib_p);
574 
575 	DBG(DBG_IB, dip, "px_ib_ino_add_intr ino=%x\n", ino_p->ino_ino);
576 
577 	/* Disable the interrupt */
578 	if ((ret = px_lib_intr_gettarget(dip, sysino,
579 	    &curr_cpu)) != DDI_SUCCESS) {
580 		DBG(DBG_IB, dip,
581 		    "px_ib_ino_add_intr px_intr_gettarget() failed\n");
582 
583 		return (ret);
584 	}
585 
586 	PX_INTR_DISABLE(dip, sysino);
587 
588 	/* Busy wait on pending interrupt */
589 	for (start_time = gethrtime(); !panicstr &&
590 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
591 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
592 		if (gethrtime() - start_time > px_intrpend_timeout) {
593 			cmn_err(CE_WARN, "%s%d: px_ib_ino_add_intr: pending "
594 			    "sysino 0x%lx(ino 0x%x) timeout",
595 			    ddi_driver_name(dip), ddi_get_instance(dip),
596 			    sysino, ino);
597 
598 			ret = DDI_FAILURE;
599 			break;
600 		}
601 	}
602 
603 	/*
604 	 * If the interrupt was previously blocked (left in pending state)
605 	 * because of jabber we need to clear the pending state in case the
606 	 * jabber has gone away.
607 	 */
608 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
609 		cmn_err(CE_WARN,
610 		    "%s%d: px_ib_ino_add_intr: ino 0x%x has been unblocked",
611 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
612 
613 		ino_p->ino_unclaimed_intrs = 0;
614 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
615 	}
616 
617 	if (ret != DDI_SUCCESS) {
618 		DBG(DBG_IB, dip, "px_ib_ino_add_intr: failed, "
619 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
620 
621 		return (ret);
622 	}
623 
624 	/* Link up px_ih_t */
625 	ih_p->ih_next = ipil_p->ipil_ih_head;
626 	ipil_p->ipil_ih_tail->ih_next = ih_p;
627 	ipil_p->ipil_ih_tail = ih_p;
628 
629 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
630 	ipil_p->ipil_ih_size++;
631 
632 	/* Re-enable interrupt */
633 	PX_INTR_ENABLE(dip, sysino, curr_cpu);
634 
635 	return (ret);
636 }
637 
638 /*
639  * Removes px_ih_t from the ino's link list.
640  * uses hardware mutex to lock out interrupt threads.
641  * Side effects: interrupt belongs to that ino is turned off on return.
642  * if we are sharing PX slot with other inos, the caller needs
643  * to turn it back on.
644  */
645 int
646 px_ib_ino_rem_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
647 {
648 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
649 	devino_t	ino = ino_p->ino_ino;
650 	sysino_t	sysino = ino_p->ino_sysino;
651 	dev_info_t	*dip = px_p->px_dip;
652 	px_ih_t		*ih_lst = ipil_p->ipil_ih_head;
653 	hrtime_t	start_time;
654 	intr_state_t	intr_state;
655 	int		i, ret = DDI_SUCCESS;
656 
657 	ASSERT(MUTEX_HELD(&ino_p->ino_ib_p->ib_ino_lst_mutex));
658 
659 	DBG(DBG_IB, px_p->px_dip, "px_ib_ino_rem_intr ino=%x\n",
660 	    ino_p->ino_ino);
661 
662 	/* Disable the interrupt */
663 	PX_INTR_DISABLE(px_p->px_dip, sysino);
664 
665 	if (ipil_p->ipil_ih_size == 1) {
666 		if (ih_lst != ih_p)
667 			goto not_found;
668 
669 		/* No need to set head/tail as ino_p will be freed */
670 		goto reset;
671 	}
672 
673 	/* Busy wait on pending interrupt */
674 	for (start_time = gethrtime(); !panicstr &&
675 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
676 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
677 		if (gethrtime() - start_time > px_intrpend_timeout) {
678 			cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: pending "
679 			    "sysino 0x%lx(ino 0x%x) timeout",
680 			    ddi_driver_name(dip), ddi_get_instance(dip),
681 			    sysino, ino);
682 
683 			ret = DDI_FAILURE;
684 			break;
685 		}
686 	}
687 
688 	/*
689 	 * If the interrupt was previously blocked (left in pending state)
690 	 * because of jabber we need to clear the pending state in case the
691 	 * jabber has gone away.
692 	 */
693 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
694 		cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: "
695 		    "ino 0x%x has been unblocked",
696 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
697 
698 		ino_p->ino_unclaimed_intrs = 0;
699 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
700 	}
701 
702 	if (ret != DDI_SUCCESS) {
703 		DBG(DBG_IB, dip, "px_ib_ino_rem_intr: failed, "
704 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
705 
706 		return (ret);
707 	}
708 
709 	/* Search the link list for ih_p */
710 	for (i = 0; (i < ipil_p->ipil_ih_size) &&
711 	    (ih_lst->ih_next != ih_p); i++, ih_lst = ih_lst->ih_next)
712 		;
713 
714 	if (ih_lst->ih_next != ih_p)
715 		goto not_found;
716 
717 	/* Remove ih_p from the link list and maintain the head/tail */
718 	ih_lst->ih_next = ih_p->ih_next;
719 
720 	if (ipil_p->ipil_ih_head == ih_p)
721 		ipil_p->ipil_ih_head = ih_p->ih_next;
722 	if (ipil_p->ipil_ih_tail == ih_p)
723 		ipil_p->ipil_ih_tail = ih_lst;
724 
725 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
726 
727 reset:
728 	if (ih_p->ih_config_handle)
729 		pci_config_teardown(&ih_p->ih_config_handle);
730 	if (ih_p->ih_ksp != NULL)
731 		kstat_delete(ih_p->ih_ksp);
732 
733 	kmem_free(ih_p, sizeof (px_ih_t));
734 	ipil_p->ipil_ih_size--;
735 
736 	return (ret);
737 
738 not_found:
739 	DBG(DBG_R_INTX, ino_p->ino_ib_p->ib_px_p->px_dip,
740 	    "ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
741 
742 	return (DDI_FAILURE);
743 }
744 
745 px_ih_t *
746 px_ib_intr_locate_ih(px_ino_pil_t *ipil_p, dev_info_t *rdip,
747     uint32_t inum, msiq_rec_type_t rec_type, msgcode_t msg_code)
748 {
749 	px_ih_t	*ih_p = ipil_p->ipil_ih_head;
750 	int	i;
751 
752 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
753 		if ((ih_p->ih_dip == rdip) && (ih_p->ih_inum == inum) &&
754 		    (ih_p->ih_rec_type == rec_type) &&
755 		    (ih_p->ih_msg_code == msg_code))
756 			return (ih_p);
757 	}
758 
759 	return ((px_ih_t *)NULL);
760 }
761 
762 px_ih_t *
763 px_ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
764     uint_t (*int_handler)(caddr_t int_handler_arg1, caddr_t int_handler_arg2),
765     caddr_t int_handler_arg1, caddr_t int_handler_arg2,
766     msiq_rec_type_t rec_type, msgcode_t msg_code)
767 {
768 	px_ih_t	*ih_p;
769 
770 	ih_p = kmem_alloc(sizeof (px_ih_t), KM_SLEEP);
771 	ih_p->ih_dip = rdip;
772 	ih_p->ih_inum = inum;
773 	ih_p->ih_intr_state = PX_INTR_STATE_DISABLE;
774 	ih_p->ih_handler = int_handler;
775 	ih_p->ih_handler_arg1 = int_handler_arg1;
776 	ih_p->ih_handler_arg2 = int_handler_arg2;
777 	ih_p->ih_config_handle = NULL;
778 	ih_p->ih_rec_type = rec_type;
779 	ih_p->ih_msg_code = msg_code;
780 	ih_p->ih_nsec = 0;
781 	ih_p->ih_ticks = 0;
782 	ih_p->ih_ksp = NULL;
783 
784 	return (ih_p);
785 }
786 
787 int
788 px_ib_update_intr_state(px_t *px_p, dev_info_t *rdip,
789     uint_t inum, devino_t ino, uint_t pil,
790     uint_t new_intr_state, msiq_rec_type_t rec_type,
791     msgcode_t msg_code)
792 {
793 	px_ib_t		*ib_p = px_p->px_ib_p;
794 	px_ino_t	*ino_p;
795 	px_ino_pil_t	*ipil_p;
796 	px_ih_t		*ih_p;
797 	int		ret = DDI_FAILURE;
798 
799 	DBG(DBG_IB, px_p->px_dip, "px_ib_update_intr_state: %s%d "
800 	    "inum %x devino %x pil %x state %x\n", ddi_driver_name(rdip),
801 	    ddi_get_instance(rdip), inum, ino, pil, new_intr_state);
802 
803 	mutex_enter(&ib_p->ib_ino_lst_mutex);
804 
805 	ino_p = px_ib_locate_ino(ib_p, ino);
806 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, pil))) {
807 		if (ih_p = px_ib_intr_locate_ih(ipil_p, rdip, inum, rec_type,
808 		    msg_code)) {
809 			ih_p->ih_intr_state = new_intr_state;
810 			ret = DDI_SUCCESS;
811 		}
812 	}
813 
814 	mutex_exit(&ib_p->ib_ino_lst_mutex);
815 	return (ret);
816 }
817 
818 
819 static void
820 px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
821     char *path_name, int instance)
822 {
823 	(void) strncpy(dev->driver_name, driver_name, MAXMODCONFNAME-1);
824 	dev->driver_name[MAXMODCONFNAME] = '\0';
825 	(void) strncpy(dev->path, path_name, MAXPATHLEN-1);
826 	dev->dev_inst = instance;
827 }
828 
829 
830 /*
831  * Return the dips or number of dips associated with a given interrupt block.
832  * Size of dips array arg is passed in as dips_ret arg.
833  * Number of dips returned is returned in dips_ret arg.
834  * Array of dips gets returned in the dips argument.
835  * Function returns number of dips existing for the given interrupt block.
836  *
837  * Note: this function assumes an enabled/valid INO, which is why it returns
838  * the px node and (Internal) when it finds no other devices (and *devs_ret > 0)
839  */
840 uint8_t
841 pxtool_ib_get_ino_devs(
842     px_t *px_p, uint32_t ino, uint8_t *devs_ret, pcitool_intr_dev_t *devs)
843 {
844 	px_ib_t		*ib_p = px_p->px_ib_p;
845 	px_ino_t	*ino_p;
846 	px_ino_pil_t	*ipil_p;
847 	px_ih_t 	*ih_p;
848 	uint32_t 	num_devs = 0;
849 	char		pathname[MAXPATHLEN];
850 	int		i, j;
851 
852 	mutex_enter(&ib_p->ib_ino_lst_mutex);
853 	ino_p = px_ib_locate_ino(ib_p, ino);
854 	if (ino_p != NULL) {
855 		for (j = 0, ipil_p = ino_p->ino_ipil_p; ipil_p;
856 		    ipil_p = ipil_p->ipil_next_p) {
857 			num_devs += ipil_p->ipil_ih_size;
858 
859 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
860 			    ((i < ipil_p->ipil_ih_size) && (i < *devs_ret));
861 			    i++, j++, ih_p = ih_p->ih_next) {
862 				(void) ddi_pathname(ih_p->ih_dip, pathname);
863 				px_fill_in_intr_devs(&devs[i],
864 				    (char *)ddi_driver_name(ih_p->ih_dip),
865 				    pathname, ddi_get_instance(ih_p->ih_dip));
866 			}
867 		}
868 
869 		*devs_ret = j;
870 	} else if (*devs_ret > 0) {
871 		(void) ddi_pathname(px_p->px_dip, pathname);
872 		strcat(pathname, " (Internal)");
873 		px_fill_in_intr_devs(&devs[0],
874 		    (char *)ddi_driver_name(px_p->px_dip),  pathname,
875 		    ddi_get_instance(px_p->px_dip));
876 		num_devs = *devs_ret = 1;
877 	}
878 
879 	mutex_exit(&ib_p->ib_ino_lst_mutex);
880 
881 	return (num_devs);
882 }
883 
884 
885 void
886 px_ib_log_new_cpu(px_ib_t *ib_p, uint32_t old_cpu_id, uint32_t new_cpu_id,
887     uint32_t ino)
888 {
889 	px_ino_t	*ino_p;
890 	px_ino_pil_t	*ipil_p;
891 	px_ih_t 	*ih_p;
892 	int		i;
893 
894 	mutex_enter(&ib_p->ib_ino_lst_mutex);
895 
896 	/* Log in OS data structures the new CPU. */
897 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {
898 
899 		/* Log in OS data structures the new CPU. */
900 		ino_p->ino_cpuid = new_cpu_id;
901 
902 		for (ipil_p = ino_p->ino_ipil_p; ipil_p;
903 		    ipil_p = ipil_p->ipil_next_p) {
904 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
905 			    (i < ipil_p->ipil_ih_size);
906 			    i++, ih_p = ih_p->ih_next) {
907 				/*
908 				 * Account for any residual time
909 				 * to be logged for old cpu.
910 				 */
911 				px_ib_cpu_ticks_to_ih_nsec(ib_p,
912 				    ih_p, old_cpu_id);
913 			}
914 		}
915 	}
916 
917 	mutex_exit(&ib_p->ib_ino_lst_mutex);
918 }
919