xref: /titanic_44/usr/src/uts/sun4u/starfire/io/idn.c (revision ee519a1f9541a20bb76ef306dfc8e5616f8a5e26)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 #include <sys/open.h>
32 #include <sys/param.h>
33 #include <sys/machparam.h>
34 #include <sys/systm.h>
35 #include <sys/signal.h>
36 #include <sys/cred.h>
37 #include <sys/user.h>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/uio.h>
41 #include <sys/buf.h>
42 #include <sys/file.h>
43 #include <sys/kmem.h>
44 #include <sys/stat.h>
45 #include <sys/stream.h>
46 #include <sys/stropts.h>
47 #include <sys/strsubr.h>
48 #include <sys/strsun.h>
49 #include <inet/common.h>
50 #include <inet/mi.h>
51 #include <inet/nd.h>
52 #include <sys/poll.h>
53 #include <sys/utsname.h>
54 #include <sys/debug.h>
55 #include <sys/conf.h>
56 #include <sys/ddi.h>
57 #include <sys/sunddi.h>
58 #include <sys/errno.h>
59 #include <sys/modctl.h>
60 #include <sys/machsystm.h>
61 #include <sys/promif.h>
62 #include <sys/prom_plat.h>
63 #include <sys/obpdefs.h>
64 #include <vm/seg_kmem.h>
65 #include <vm/seg_kp.h>
66 #include <sys/kstat.h>
67 #include <sys/membar.h>
68 #include <sys/ivintr.h>
69 #include <sys/vm_machparam.h>
70 #include <sys/x_call.h>
71 #include <sys/cpuvar.h>
72 #include <sys/archsystm.h>
73 #include <sys/dmv.h>
74 
75 #include <sys/idn.h>
76 #include <sys/idn_xf.h>
77 #include <sys/cpu_sgnblk_defs.h>
78 #include <sys/cpu_sgn.h>
79 
80 struct idn_gkstat	sg_kstat;
81 
82 #define	MBXTBL_PART_REPORT	((caddr_t)1)
83 #define	MBXTBL_FULL_REPORT	((caddr_t)2)
84 
85 idn_domain_t	idn_domain[MAX_DOMAINS];
86 idn_global_t	idn;
87 int		idn_debug;
88 int		idn_snoop;
89 int		idn_history;
90 
91 typedef enum {
92 	IDN_GPROPS_OKAY,
93 	IDN_GPROPS_UNCHECKED,
94 	IDN_GPROPS_ERROR
95 } idn_gprops_t;
96 
97 struct idn_history	idnhlog;
98 
99 /*
100  * IDN "tunables".
101  */
102 int		idn_smr_size;
103 int		idn_nwr_size;
104 int		idn_lowat;
105 int		idn_hiwat;
106 int		idn_protocol_nservers;
107 int		idn_awolmsg_interval;
108 int		idn_smr_bufsize;
109 int		idn_slab_bufcount;
110 int		idn_slab_prealloc;
111 int		idn_slab_maxperdomain;
112 int		idn_slab_mintotal;
113 int		idn_window_max;
114 int		idn_window_incr;
115 int		idn_window_emax;
116 int		idn_reclaim_min;
117 int		idn_reclaim_max;
118 int		idn_mbox_per_net;
119 int		idn_max_nets;
120 
121 int		idn_netsvr_spin_count;
122 int		idn_netsvr_wait_min;
123 int		idn_netsvr_wait_max;
124 int		idn_netsvr_wait_shift;
125 
126 int		idn_checksum;
127 
128 int		idn_msgwait_nego;
129 int		idn_msgwait_cfg;
130 int		idn_msgwait_con;
131 int		idn_msgwait_fin;
132 int		idn_msgwait_cmd;
133 int		idn_msgwait_data;
134 
135 int		idn_retryfreq_nego;
136 int		idn_retryfreq_con;
137 int		idn_retryfreq_fin;
138 
139 int		idn_window_emax;	/* calculated */
140 int		idn_slab_maxperdomain;	/* calculated */
141 
142 /*
143  * DMV interrupt support.
144  */
145 int		idn_pil;
146 int		idn_dmv_pending_max;
147 idn_dmv_msg_t	*idn_iv_queue[NCPU];
148 int		idn_intr_index[NCPU];	/* idn_handler ONLY */
149 static idn_dmv_data_t	*idn_dmv_data;
150 
151 int		idn_sigbpil;
152 
153 idnparam_t	idn_param_arr[] = {
154 { 0,		1,		0,		/* 0 */ "idn_modunloadable" },
155 };
156 
157 /*
158  * Parameters that are only accessible in a DEBUG driver.
159  */
160 static char *idn_param_debug_only[] = {
161 #if 0
162 	"idn_checksum",
163 #endif /* 0 */
164 	0
165 };
166 
167 /*
168  * Parameters that are READ-ONLY.
169  */
170 static char *idn_param_read_only[] = {
171 #if 0
172 	"idn_window_emax",
173 	"idn_slab_maxperdomain",
174 #endif /* 0 */
175 	0
176 };
177 
178 static struct idn_global_props {
179 	int		p_min, p_max, p_def;
180 	char		*p_string;
181 	int		*p_var;
182 } idn_global_props[] = {
183 { 0,	0,	0,	"idn_debug",		&idn_debug		},
184 { 0,	1,	0,	"idn_history",		&idn_history		},
185 { 0,	IDN_SMR_MAXSIZE,
186 		0,	"idn_smr_size",		&idn_smr_size		},
187 { 0,	IDN_SMR_MAXSIZE,
188 		0,	"idn_nwr_size",		&idn_nwr_size		},
189 { 1,	512*1024,
190 		1,	"idn_lowat",		&idn_lowat		},
191 { 1*1024,
192 	1*1024*1024,
193 		256*1024,
194 			"idn_hiwat",		&idn_hiwat		},
195 { IDN_SMR_BUFSIZE_MIN,
196 	IDN_SMR_BUFSIZE_MAX,
197 		IDN_SMR_BUFSIZE_DEF,
198 			"idn_smr_bufsize",	&idn_smr_bufsize	},
199 { 4,	1024,	32,	"idn_slab_bufcount",	&idn_slab_bufcount	},
200 { 0,	10,	0,	"idn_slab_prealloc",	&idn_slab_prealloc	},
201 { 2,	MAX_DOMAINS,
202 		8,	"idn_slab_mintotal",	&idn_slab_mintotal	},
203 { 8,	256,	64,	"idn_window_max",	&idn_window_max		},
204 { 0,	32,	8,	"idn_window_incr",	&idn_window_incr	},
205 { 1,	128,	5,	"idn_reclaim_min",	&idn_reclaim_min	},
206 { 0,	128,	0,	"idn_reclaim_max",	&idn_reclaim_max	},
207 { 1,	IDN_MAXMAX_NETS,
208 		8,	"idn_max_nets",		&idn_max_nets		},
209 { 31,	511,	127,	"idn_mbox_per_net",	&idn_mbox_per_net	},
210 { 0,	1,	1,	"idn_checksum",		&idn_checksum		},
211 { 0,	10000,	500,	"idn_netsvr_spin_count",
212 						&idn_netsvr_spin_count	},
213 { 0,	30*100,	40,	"idn_netsvr_wait_min",	&idn_netsvr_wait_min	},
214 { 0,	60*100,	16*100,	"idn_netsvr_wait_max",	&idn_netsvr_wait_max	},
215 { 1,	5,	1,	"idn_netsvr_wait_shift",
216 						&idn_netsvr_wait_shift	},
217 { 1,	MAX_DOMAINS,
218 		IDN_PROTOCOL_NSERVERS,
219 			"idn_protocol_nservers",
220 						&idn_protocol_nservers	},
221 { 0,	3600,	IDN_AWOLMSG_INTERVAL,
222 			"idn_awolmsg_interval",	&idn_awolmsg_interval	},
223 { 10,	300,	IDN_MSGWAIT_NEGO,
224 			"idn_msgwait_nego",	&idn_msgwait_nego	},
225 { 10,	300,	IDN_MSGWAIT_CFG,
226 			"idn_msgwait_cfg",	&idn_msgwait_cfg	},
227 { 10,	300,	IDN_MSGWAIT_CON,
228 			"idn_msgwait_con",	&idn_msgwait_con	},
229 { 10,	300,	IDN_MSGWAIT_FIN,
230 			"idn_msgwait_fin",	&idn_msgwait_fin	},
231 { 10,	300,	IDN_MSGWAIT_CMD,
232 			"idn_msgwait_cmd",	&idn_msgwait_cmd	},
233 { 10,	300,	IDN_MSGWAIT_DATA,
234 			"idn_msgwait_data",	&idn_msgwait_data	},
235 { 1,	60,	IDN_RETRYFREQ_NEGO,
236 			"idn_retryfreq_nego",	&idn_retryfreq_nego	},
237 { 1,	60,	IDN_RETRYFREQ_CON,
238 			"idn_retryfreq_con",	&idn_retryfreq_con	},
239 { 1,	60,	IDN_RETRYFREQ_FIN,
240 			"idn_retryfreq_fin",	&idn_retryfreq_fin	},
241 { 1,	9,	IDN_PIL,
242 			"idn_pil",		&idn_pil		},
243 { 1,	9,	IDN_SIGBPIL,
244 			"idn_sigbpil",		&idn_sigbpil		},
245 { 8,	512,	IDN_DMV_PENDING_MAX,
246 			"idn_dmv_pending_max",	&idn_dmv_pending_max	},
247 { 0,	0,	0,	NULL,			NULL			}
248 };
249 
250 struct idn	*idn_i2s_table[IDN_MAXMAX_NETS << 1];
251 clock_t		idn_msg_waittime[IDN_NUM_MSGTYPES];
252 clock_t		idn_msg_retrytime[(int)IDN_NUM_RETRYTYPES];
253 
254 static caddr_t	idn_ndlist;	/* head of 'named dispatch' var list */
255 
256 static int	idnattach(dev_info_t *, ddi_attach_cmd_t);
257 static int	idndetach(dev_info_t *, ddi_detach_cmd_t);
258 static int	idnopen(register queue_t *, dev_t *, int, int, cred_t *);
259 static int	idnclose(queue_t *, int, cred_t *);
260 static int	idnwput(queue_t *, mblk_t *);
261 static int	idnwsrv(queue_t *);
262 static int	idnrput(queue_t *, mblk_t *);
263 static void	idnioctl(queue_t *, mblk_t *);
264 static idn_gprops_t	idn_check_conf(dev_info_t *dip, processorid_t *cpuid);
265 static int	idn_size_check();
266 static void	idn_xmit_monitor_init();
267 static void	idn_xmit_monitor_deinit();
268 static void	idn_init_msg_waittime();
269 static void	idn_init_msg_retrytime();
270 static void	idn_sigb_setup(cpu_sgnblk_t *sigbp, void *arg);
271 static int	idn_init(dev_info_t *dip);
272 static int	idn_deinit();
273 static void	idn_sigbhandler_create();
274 static void	idn_sigbhandler_kill();
275 static uint_t	idn_sigbhandler_wakeup(caddr_t arg1, caddr_t arg2);
276 static void	idn_sigbhandler_thread(struct sigbintr **sbpp);
277 static void	idn_sigbhandler(processorid_t cpuid, cpu_sgnblk_t *sgnblkp);
278 static int	idn_info(idnsb_info_t *sfp);
279 static int	idn_init_smr();
280 static void	idn_deinit_smr();
281 static int	idn_prom_getsmr(uint_t *smrsz, uint64_t *paddrp,
282 				uint64_t *sizep);
283 static int	idn_init_handler();
284 static void	idn_deinit_handler();
285 static uint_t	idn_handler(caddr_t unused, caddr_t unused2);
286 /*
287  * ioctl services
288  */
289 static int	idnioc_link(idnop_t *idnop);
290 static int	idnioc_unlink(idnop_t *idnop);
291 static int	idn_rw_mem(idnop_t *idnop);
292 static int	idn_send_ping(idnop_t *idnop);
293 
294 static void 	idn_domains_init(struct hwconfig *local_hw);
295 static void 	idn_domains_deinit();
296 static void	idn_retrytask_init();
297 static void	idn_retrytask_deinit();
298 static void	idn_gkstat_init();
299 static void	idn_gkstat_deinit();
300 static int	idn_gkstat_update();
301 static void	idn_timercache_init();
302 static void	idn_timercache_deinit();
303 static void	idn_dopers_init();
304 static void	idn_dopers_deinit();
305 
306 static void	idn_param_cleanup();
307 static int	idn_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
308 static int	idn_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
309 					cred_t *cr);
310 static int	idn_param_register(register idnparam_t *idnpa, int count);
311 static int	idn_slabpool_report(queue_t *wq, mblk_t *mp, caddr_t cp,
312 					cred_t *cr);
313 static int	idn_buffer_report(queue_t *wq, mblk_t *mp, caddr_t cp,
314 					cred_t *cr);
315 static int	idn_mboxtbl_report(queue_t *wq, mblk_t *mp, caddr_t cp,
316 					cred_t *cr);
317 static int	idn_mainmbox_report(queue_t *wq, mblk_t *mp, caddr_t cp,
318 					cred_t *cr);
319 static void	idn_mainmbox_domain_report(queue_t *wq, mblk_t *mp, int domid,
320 					idn_mainmbox_t *mmp, char *mbxtype);
321 static int	idn_global_report(queue_t *wq, mblk_t *mp, caddr_t cp,
322 					cred_t *cr);
323 static int	idn_domain_report(queue_t *wq, mblk_t *mp, caddr_t cp,
324 					cred_t *cr);
325 static int	idn_get_net_binding(queue_t *wq, mblk_t *mp, caddr_t cp,
326 					cred_t *cr);
327 static int	idn_set_net_binding(queue_t *wq, mblk_t *mp, char *value,
328 					caddr_t cp, cred_t *cr);
329 
330 /*
331  * String definitions used for DEBUG and non-DEBUG.
332  */
333 const char	*idnm_str[] = {
334 /*  0 */	"null",
335 /*  1 */	"nego",
336 /*  2 */	"con",
337 /*  3 */	"cfg",
338 /*  4 */	"fin",
339 /*  5 */	"cmd",
340 /*  6 */	"data",
341 };
342 
343 const char	*idnds_str[] = {
344 /*  0 */	"CLOSED",
345 /*  1 */	"NEGO_PEND",
346 /*  2 */	"NEGO_SENT",
347 /*  3 */	"NEGO_RCVD",
348 /*  4 */	"CONFIG",
349 /*  5 */	"CON_PEND",
350 /*  6 */	"CON_SENT",
351 /*  7 */	"CON_RCVD",
352 /*  8 */	"CON_READY",
353 /*  9 */	"CONNECTED",
354 /* 10 */	"FIN_PEND",
355 /* 11 */	"FIN_SENT",
356 /* 12 */	"FIN_RCVD",
357 /* 13 */	"DMAP"
358 };
359 
360 const char	*idnxs_str[] = {
361 /* 0 */		"PEND",
362 /* 1 */		"SENT",
363 /* 2 */		"RCVD",
364 /* 3 */		"FINAL",
365 /* 4 */		"NIL"
366 };
367 
368 const char	*idngs_str[] = {
369 /*  0 */	"OFFLINE",
370 /*  1 */	"CONNECT",
371 /*  2 */	"ONLINE",
372 /*  3 */	"DISCONNECT",
373 /*  4 */	"RECONFIG",
374 /*  5 */	"unknown",
375 /*  6 */	"unknown",
376 /*  7 */	"unknown",
377 /*  8 */	"unknown",
378 /*  9 */	"unknown",
379 /* 10 */	"IGNORE"
380 };
381 
382 const char	*idncmd_str[] = {
383 /*  0 */	"unknown",
384 /*  1 */	"SLABALLOC",
385 /*  2 */	"SLABFREE",
386 /*  3 */	"SLABREAP",
387 /*  4 */	"NODENAME"
388 };
389 
390 const char	*idncon_str[] = {
391 /*  0 */	"OFF",
392 /*  1 */	"NORMAL",
393 /*  2 */	"QUERY"
394 };
395 
396 const char	*idnfin_str[] = {
397 /*  0 */	"OFF",
398 /*  1 */	"NORMAL",
399 /*  2 */	"FORCE_SOFT",
400 /*  3 */	"FORCE_HARD",
401 /*  4 */	"QUERY"
402 };
403 
404 const char	*idnfinopt_str[] = {
405 /*  0 */	"NONE",
406 /*  1 */	"UNLINK",
407 /*  2 */	"RELINK"
408 };
409 
410 const char	*idnfinarg_str[] = {
411 /*  0 */	"NONE",
412 /*  1 */	"SMRBAD",
413 /*  2 */	"CPUCFG",
414 /*  3 */	"HWERR",
415 /*  4 */	"CFGERR_FATAL",
416 /*  5 */	"CFGERR_MTU",
417 /*  6 */	"CFGERR_BUF",
418 /*  7 */	"CFGERR_SLAB",
419 /*  8 */	"CFGERR_NWR",
420 /*  9 */	"CFGERR_NETS",
421 /* 10 */	"CFGERR_MBOX",
422 /* 11 */	"CFGERR_NMCADR",
423 /* 12 */	"CFGERR_MCADR",
424 /* 13 */	"CFGERR_CKSUM",
425 /* 14 */	"CFGERR_SMR",
426 };
427 
428 const char	*idnsync_str[] = {
429 /*  0 */	"NIL",
430 /*  1 */	"CONNECT",
431 /*  2 */	"DISCONNECT"
432 };
433 
434 const char	*idnreg_str[] = {
435 /*  0 */	"REG",
436 /*  1 */	"NEW",
437 /*  2 */	"QUERY"
438 };
439 
440 const char	*idnnack_str[] = {
441 /*  0 */	"unknown",
442 /*  1 */	"NOCONN",
443 /*  2 */	"BADCHAN",
444 /*  3 */	"BADCFG",
445 /*  4 */	"BADCMD",
446 /*  5 */	"RETRY",
447 /*  6 */	"DUP",
448 /*  7 */	"EXIT",
449 /*  8 */	"--reserved1",
450 /*  9 */	"--reserved2",
451 /* 10 */	"--reserved3"
452 };
453 
454 const char	*idnop_str[] = {
455 /*  0 */	"DISCONNECTED",
456 /*  1 */	"CONNECTED",
457 /*  2 */	"ERROR"
458 };
459 
460 const char	*chanop_str[] = {
461 /*  0 */	"OPEN",
462 /*  1 */	"SOFT_CLOSE",
463 /*  2 */	"HARD_CLOSE",
464 /*  3 */	"OFFLINE",
465 /*  4 */	"ONLINE"
466 };
467 
468 const char	*chanaction_str[] = {
469 /*  0 */	"DETACH",
470 /*  1 */	"STOP",
471 /*  2 */	"SUSPEND",
472 /*  3 */	"RESUME",
473 /*  4 */	"RESTART",
474 /*  5 */	"ATTACH"
475 };
476 
477 const char	*timer_str[] = {
478 /* 0 */		"NIL",
479 /* 1 */		"MSG"
480 };
481 
482 static struct module_info idnrinfo = {
483 	IDNIDNUM,		/* mi_idnum */
484 	IDNNAME,		/* mi_idname */
485 	IDNMINPSZ,		/* mi_minpsz */
486 	IDNMAXPSZ,		/* mi_maxpsz */
487 	0,			/* mi_hiwat - see IDN_HIWAT */
488 	0			/* mi_lowat - see IDN_LOWAT */
489 };
490 
491 static struct module_info idnwinfo = {
492 	IDNIDNUM,		/* mi_idnum */
493 	IDNNAME,		/* mi_idname */
494 	IDNMINPSZ,		/* mi_minpsz */
495 	IDNMAXPSZ,		/* mi_maxpsz */
496 	0,			/* mi_hiwat - see IDN_HIWAT */
497 	0			/* mi_lowat - see IDN_LOWAT */
498 };
499 
500 static struct qinit idnrinit = {
501 	idnrput,		/* qi_putp */
502 	NULL,			/* qi_srvp */
503 	idnopen,		/* qi_qopen */
504 	idnclose,		/* qi_qclose */
505 	NULL,			/* qi_qadmin */
506 	&idnrinfo,		/* qi_minfo */
507 	NULL,			/* qi_mstat */
508 	NULL,			/* qi_rwp */
509 	NULL,			/* qi_infop */
510 	STRUIOT_DONTCARE	/* qi_struiot */
511 };
512 
513 static struct qinit idnwinit = {
514 	idnwput,		/* qi_putp */
515 	idnwsrv,		/* qi_srvp */
516 	NULL,			/* qi_qopen */
517 	NULL,			/* qi_qclose */
518 	NULL,			/* qi_qadmin */
519 	&idnwinfo,		/* qi_minfo */
520 	NULL,			/* qi_mstat */
521 	NULL,			/* qi_rwp */
522 	NULL,			/* qi_infop */
523 	STRUIOT_DONTCARE	/* qi_struiot */
524 };
525 
526 struct streamtab idninfo = {
527 	&idnrinit,		/* st_rdinit */
528 	&idnwinit,		/* st_wrinit */
529 	NULL,			/* st_muxrinit */
530 	NULL,			/* st_muxwinit */
531 };
532 
533 /*
534  * Module linkage information (cb_ops & dev_ops) for the kernel.
535  */
536 
537 static struct cb_ops cb_idnops = {
538 	nulldev,		/* cb_open */
539 	nulldev,		/* cb_close */
540 	nodev,			/* cb_strategy */
541 	nodev,			/* cb_print */
542 	nodev,			/* cb_dump */
543 	nodev,			/* cb_read */
544 	nodev,			/* cb_write */
545 	nodev,			/* cb_ioctl */
546 	nodev,			/* cb_devmap */
547 	nodev,			/* cb_mmap */
548 	nodev,			/* cb_segmap */
549 	nochpoll,		/* cb_chpoll */
550 	ddi_prop_op,		/* cb_prop_op */
551 	&idninfo,		/* cb_stream */
552 	D_MP,			/* cb_flag */
553 	CB_REV,			/* cb_rev */
554 	nodev,			/* cb_aread */
555 	nodev,			/* cb_awrite */
556 };
557 
558 static struct dev_ops idnops = {
559 	DEVO_REV,		/* devo_rev */
560 	0,			/* devo_refcnt */
561 	ddi_no_info,		/* devo_getinfo */
562 	nulldev,		/* devo_identify */
563 	nulldev,		/* devo_probe */
564 	idnattach,		/* devo_attach */
565 	idndetach,		/* devo_detach */
566 	nodev,			/* devo_reset */
567 	&cb_idnops,		/* devo_cb_ops */
568 	(struct bus_ops *)NULL,	/* devo_bus_ops */
569 	NULL			/* devo_power */
570 };
571 
572 extern cpuset_t	cpu_ready_set;
573 
574 static struct modldrv modldrv = {
575 	&mod_driverops,		/* This module is a pseudo driver */
576 	IDNDESC " %I%",
577 	&idnops
578 };
579 
580 static struct modlinkage modlinkage = {
581 	MODREV_1,
582 	&modldrv,
583 	NULL
584 };
585 
586 /*
587  * --------------------------------------------------
588  */
589 int
590 _init(void)
591 {
592 	idn.version = IDN_VERSION;
593 
594 	return (mod_install(&modlinkage));
595 }
596 
597 int
598 _fini(void)
599 {
600 	return (mod_remove(&modlinkage));
601 }
602 
603 int
604 _info(struct modinfo *modinfop)
605 {
606 	return (mod_info(&modlinkage, modinfop));
607 }
608 
609 /*
610  * ----------------------------------------------
611  */
612 static int
613 idnattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
614 {
615 	int		instance;
616 	int		doinit = 0;
617 	processorid_t	bcpuid;
618 	struct idn	*sip;
619 	struct idnstr	*stp;
620 	procname_t	proc = "idnattach";
621 
622 
623 #ifndef	lint
624 	ASSERT(sizeof (idnsb_t) == IDNSB_SIZE);
625 	ASSERT((uint_t)&((struct idnsb *)0)->id_hwchkpt[0] == 0x40);
626 #endif	/* lint */
627 
628 	switch (cmd) {
629 	case DDI_RESUME:
630 		sip = ddi_get_driver_private(dip);
631 		/*
632 		 * sip may have not yet been set if the
633 		 * OBP environment variable (idn-smr-size)
634 		 * was not set.
635 		 */
636 		if (sip == NULL)
637 			return (DDI_FAILURE);
638 		/*
639 		 * RESUME IDN services.
640 		 */
641 		IDN_GLOCK_SHARED();
642 		if (idn.state != IDNGS_OFFLINE) {
643 			cmn_err(CE_WARN,
644 				"IDN: 101: not in expected OFFLINE state "
645 				"for DDI_RESUME");
646 			ASSERT(0);
647 		}
648 		IDN_GUNLOCK();
649 
650 		/*
651 		 * RESUME DLPI services.
652 		 */
653 		sip->si_flags &= ~IDNSUSPENDED;
654 
655 		rw_enter(&idn.struprwlock, RW_READER);
656 		for (stp = idn.strup; stp; stp = stp->ss_nextp)
657 			if (stp->ss_sip == sip) {
658 				doinit = 1;
659 				break;
660 			}
661 		rw_exit(&idn.struprwlock);
662 		if (doinit)
663 			(void) idndl_init(sip);
664 
665 		return (DDI_SUCCESS);
666 
667 	case DDI_ATTACH:
668 		break;
669 
670 	default:
671 		return (DDI_FAILURE);
672 	}
673 
674 	instance = ddi_get_instance(dip);
675 
676 	PR_DRV("%s: instance = %d\n", proc, instance);
677 
678 	if (idn_check_conf(dip, &bcpuid) == IDN_GPROPS_ERROR)
679 		return (DDI_FAILURE);
680 
681 	mutex_enter(&idn.siplock);
682 
683 	if (ddi_create_minor_node(dip, IDNNAME, S_IFCHR, instance,
684 				DDI_NT_NET, CLONE_DEV) == DDI_FAILURE) {
685 		mutex_exit(&idn.siplock);
686 		return (DDI_FAILURE);
687 	}
688 
689 	if (idn.smr.ready == 0) {
690 		if (idn_init_smr() == 0) {
691 			idn.enabled = 1;
692 #ifdef DEBUG
693 			cmn_err(CE_NOTE, "!IDN: Driver enabled");
694 #endif /* DEBUG */
695 		} else {
696 			cmn_err(CE_NOTE,
697 				"!IDN: 102: driver disabled "
698 				"- check OBP environment "
699 				"(idn-smr-size)");
700 			mutex_exit(&idn.siplock);
701 			return (DDI_SUCCESS);
702 		}
703 	}
704 
705 	ASSERT(idn.smr.ready || idn.enabled);
706 
707 	if (idn.dip == NULL) {
708 		doinit = 1;
709 
710 		if (idn_size_check()) {
711 			idn_deinit_smr();
712 			ddi_remove_minor_node(dip, NULL);
713 			mutex_exit(&idn.siplock);
714 			return (DDI_FAILURE);
715 		}
716 
717 		if (idn_init(dip)) {
718 			idn_deinit_smr();
719 			ddi_remove_minor_node(dip, NULL);
720 			mutex_exit(&idn.siplock);
721 			return (DDI_FAILURE);
722 		}
723 	}
724 
725 	ASSERT(idn.dip);
726 
727 	/*
728 	 * This must occur _after_ idn_init() since
729 	 * it assumes idn_chanservers_init() has been
730 	 * called.
731 	 */
732 	idn_chanserver_bind(ddi_get_instance(dip), bcpuid);
733 
734 	/*
735 	 * DLPI supporting stuff.
736 	 */
737 	sip = GETSTRUCT(struct idn, 1);
738 	sip->si_dip = dip;
739 	ddi_set_driver_private(dip, sip);
740 	sip->si_nextp = idn.sip;
741 	idn.sip = sip;
742 	IDN_SET_INST2SIP(instance, sip);
743 	mutex_exit(&idn.siplock);
744 
745 	if (doinit)
746 		idndl_dlpi_init();	/* initializes idninfoack */
747 	/*
748 	 * Get our local IDN ethernet address.
749 	 */
750 	idndl_localetheraddr(sip, &sip->si_ouraddr);
751 	idndl_statinit(sip);
752 
753 	if (doinit) {
754 		idn_gkstat_init();
755 		/*
756 		 * Add our sigblock SSP interrupt handler.
757 		 */
758 		mutex_enter(&idn.sigbintr.sb_mutex);
759 		idn_sigbhandler_create();
760 		mutex_exit(&idn.sigbintr.sb_mutex);
761 
762 		if (sgnblk_poll_register(idn_sigbhandler) == 0) {
763 			mutex_enter(&idn.sigbintr.sb_mutex);
764 			idn_sigbhandler_kill();
765 			idn.sigbintr.sb_cpuid = (uchar_t)-1;
766 			idn.sigbintr.sb_busy = IDNSIGB_INACTIVE;
767 			mutex_exit(&idn.sigbintr.sb_mutex);
768 
769 			idn_gkstat_deinit();
770 
771 			mutex_enter(&idn.siplock);
772 			(void) idn_deinit();
773 			IDN_SET_INST2SIP(instance, NULL);
774 			idn.sip = sip->si_nextp;
775 			mutex_exit(&idn.siplock);
776 
777 			ddi_remove_minor_node(dip, NULL);
778 
779 			return (DDI_FAILURE);
780 		}
781 		/*
782 		 * We require sigblkp[cpu0] to be mapped for hardware
783 		 * configuration determination and also auto-linking
784 		 * on bootup.
785 		 */
786 		if (sgnblk_poll_reference(idn_sigb_setup, NULL) != 0) {
787 			sgnblk_poll_unregister(idn_sigbhandler);
788 			mutex_enter(&idn.sigbintr.sb_mutex);
789 			idn_sigbhandler_kill();
790 			idn.sigbintr.sb_cpuid = (uchar_t)-1;
791 			idn.sigbintr.sb_busy = IDNSIGB_INACTIVE;
792 			mutex_exit(&idn.sigbintr.sb_mutex);
793 
794 			idn_gkstat_deinit();
795 
796 			mutex_enter(&idn.siplock);
797 			(void) idn_deinit();
798 			IDN_SET_INST2SIP(instance, NULL);
799 			idn.sip = sip->si_nextp;
800 			mutex_exit(&idn.siplock);
801 
802 			ddi_remove_minor_node(dip, NULL);
803 
804 			cmn_err(CE_WARN,
805 				"IDN: 103: unable to reference sigblock area");
806 
807 			return (DDI_FAILURE);
808 		}
809 
810 		idn_init_autolink();
811 	}
812 
813 	ddi_report_dev(dip);
814 
815 	return (DDI_SUCCESS);
816 }
817 
818 /*
819  * ----------------------------------------------
820  */
821 static int
822 idndetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
823 {
824 	int		err = 0;
825 	int		instance;
826 	struct idn	*sip, *hsip, *tsip;
827 	procname_t	proc = "idndetach";
828 
829 	sip = ddi_get_driver_private(dip);
830 	instance = ddi_get_instance(dip);
831 
832 	switch (cmd) {
833 	case DDI_SUSPEND:
834 		if (sip == NULL)
835 			return (DDI_FAILURE);
836 		/*
837 		 * SUSPEND IDN services.
838 		 * - Actually don't suspend anything, we just
839 		 *   make sure we're not connected per DR protocol.
840 		 *   If we really wanted to suspend it should
841 		 *   be done _after_ DLPI is suspended so that
842 		 *   we're not competing with that traffic.
843 		 */
844 		IDN_GLOCK_SHARED();
845 
846 		if (idn.state != IDNGS_OFFLINE) {
847 			int	d;
848 
849 			cmn_err(CE_WARN,
850 				"IDN: 104: cannot suspend while active "
851 				"(state = %s)",
852 				idngs_str[idn.state]);
853 
854 			for (d = 0; d < MAX_DOMAINS; d++) {
855 				idn_domain_t	*dp;
856 
857 				dp = &idn_domain[d];
858 				if (dp->dcpu < 0)
859 					continue;
860 
861 				cmn_err(CE_CONT,
862 					"IDN: 121: domain %d (CPU %d, name "
863 					"\"%s\", state %s)\n",
864 					d, dp->dcpu, dp->dname,
865 					idnds_str[dp->dstate]);
866 			}
867 			err = 1;
868 		}
869 
870 		IDN_GUNLOCK();
871 
872 		if (err)
873 			return (DDI_FAILURE);
874 		/*
875 		 * SUSPEND DLPI services.
876 		 */
877 		sip->si_flags |= IDNSUSPENDED;
878 
879 		idndl_uninit(sip);
880 
881 		return (DDI_FAILURE);
882 
883 	case DDI_DETACH:
884 		if (idn.enabled == 0) {
885 			ddi_remove_minor_node(dip, NULL);
886 			ASSERT(idn.dip == NULL);
887 			return (DDI_SUCCESS);
888 		}
889 		if (!IDN_MODUNLOADABLE)
890 			return (DDI_FAILURE);
891 		break;
892 
893 	default:
894 		return (DDI_FAILURE);
895 	}
896 
897 	PR_DRV("%s: instance = %d\n", proc, instance);
898 
899 	if (sip == NULL) {
900 		/*
901 		 * No resources allocated.
902 		 */
903 		return (DDI_SUCCESS);
904 	}
905 
906 	mutex_enter(&idn.siplock);
907 	if (idn.sip && (idn.sip->si_nextp == NULL)) {
908 		/*
909 		 * This is our last stream connection
910 		 * going away.  Time to deinit and flag
911 		 * the SSP we're (IDN) DOWN.
912 		 */
913 		if (idn_deinit()) {
914 			/*
915 			 * Must still be active.
916 			 */
917 			mutex_exit(&idn.siplock);
918 			return (DDI_FAILURE);
919 		}
920 		idn_deinit_autolink();
921 		/*
922 		 * Remove our sigblock SSP interrupt handler.
923 		 */
924 		sgnblk_poll_unregister(idn_sigbhandler);
925 		mutex_enter(&idn.sigbintr.sb_mutex);
926 		idn_sigbhandler_kill();
927 		idn.sigbintr.sb_cpuid = (uchar_t)-1;
928 		idn.sigbintr.sb_busy = IDNSIGB_NOTREADY;
929 		mutex_exit(&idn.sigbintr.sb_mutex);
930 		/*
931 		 * Remove our reference to the sigblock area.
932 		 */
933 		sgnblk_poll_unreference(idn_sigb_setup);
934 		idn_gkstat_deinit();
935 	}
936 
937 	ddi_remove_minor_node(dip, NULL);
938 
939 	/*
940 	 * Remove this instance from our linked list.
941 	 */
942 	IDN_SET_INST2SIP(instance, NULL);
943 	if ((hsip = tsip = idn.sip) == sip) {
944 		idn.sip = sip->si_nextp;
945 	} else {
946 		for (; hsip && (sip != hsip); tsip = hsip,
947 						hsip = hsip->si_nextp)
948 			;
949 		if (hsip)
950 			tsip->si_nextp = hsip->si_nextp;
951 	}
952 	mutex_exit(&idn.siplock);
953 	if (sip->si_ksp)
954 		kstat_delete(sip->si_ksp);
955 
956 	ddi_set_driver_private(dip, NULL);
957 
958 	FREESTRUCT(sip, struct idn, 1);
959 
960 	return (DDI_SUCCESS);
961 }
962 
963 /*
964  * ----------------------------------------------
965  */
966 static idn_gprops_t
967 idn_check_conf(dev_info_t *dip, processorid_t *cpuid)
968 {
969 	static idn_gprops_t	global_props = IDN_GPROPS_UNCHECKED;
970 
971 	if (global_props == IDN_GPROPS_UNCHECKED) {
972 		int		p;
973 
974 		global_props = IDN_GPROPS_OKAY;
975 
976 		for (p = 0; idn_global_props[p].p_string; p++) {
977 			char	*str;
978 			int	*var;
979 			int	val, v_min, v_max, v_def;
980 
981 			str = idn_global_props[p].p_string;
982 			var = (int *)idn_global_props[p].p_var;
983 			v_min = idn_global_props[p].p_min;
984 			v_max = idn_global_props[p].p_max;
985 			v_def = idn_global_props[p].p_def;
986 			ASSERT(str && var);
987 
988 			val = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
989 						DDI_PROP_DONTPASS |
990 						DDI_PROP_NOTPROM,
991 						str, v_def);
992 			if ((v_min != v_max) &&
993 				((val < v_min) || (val > v_max))) {
994 				cmn_err(CE_WARN,
995 					"IDN: 105: driver parameter "
996 					"(%s) specified (%d) out of "
997 					"range [%d - %d]",
998 					str, val, v_min, v_max);
999 				global_props = IDN_GPROPS_ERROR;
1000 			} else {
1001 				*var = val;
1002 			}
1003 		}
1004 	}
1005 
1006 	*cpuid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1007 				DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1008 				"bind_cpu", -1);
1009 
1010 	return (global_props);
1011 }
1012 
1013 static int
1014 idn_size_check()
1015 {
1016 	int		i, cnt;
1017 	int		rv = 0;
1018 	ulong_t		mboxareasize;
1019 	int		max_num_slabs;
1020 	procname_t	proc = "idn_size_check";
1021 
1022 	if (IDN_NWR_SIZE == 0)
1023 		IDN_NWR_SIZE = IDN_SMR_SIZE;
1024 
1025 	if (IDN_NWR_SIZE > IDN_SMR_SIZE) {
1026 		cmn_err(CE_WARN,
1027 			"IDN: 106: idn_nwr_size(%d) > idn_smr_size(%d)"
1028 			" - Limiting to %d MB",
1029 			IDN_NWR_SIZE, IDN_SMR_SIZE, IDN_SMR_SIZE);
1030 		IDN_NWR_SIZE = IDN_SMR_SIZE;
1031 	}
1032 
1033 	if (MB2B(IDN_NWR_SIZE) < IDN_SLAB_SIZE) {
1034 		cmn_err(CE_WARN,
1035 			"IDN: 107: memory region(%lu) < slab size(%u)",
1036 			MB2B(IDN_NWR_SIZE), IDN_SLAB_SIZE);
1037 		rv = -1;
1038 	}
1039 
1040 	if (IDN_LOWAT >= IDN_HIWAT) {
1041 		cmn_err(CE_WARN,
1042 			"IDN: 108: idn_lowat(%d) >= idn_hiwat(%d)",
1043 			IDN_LOWAT, IDN_HIWAT);
1044 		rv = -1;
1045 	}
1046 
1047 	mboxareasize = (ulong_t)(IDN_MBOXAREA_SIZE + (IDN_SMR_BUFSIZE - 1));
1048 	mboxareasize &= ~((ulong_t)IDN_SMR_BUFSIZE - 1);
1049 #ifdef DEBUG
1050 	if ((ulong_t)IDN_SLAB_SIZE < mboxareasize) {
1051 		PR_DRV("%s: slab size(%d) < mailbox area(%ld)",
1052 			proc, IDN_SLAB_SIZE, mboxareasize);
1053 		/* not fatal */
1054 	}
1055 #endif /* DEBUG */
1056 
1057 	if ((mboxareasize + (ulong_t)IDN_SLAB_SIZE) > MB2B(IDN_NWR_SIZE)) {
1058 		cmn_err(CE_WARN,
1059 			"IDN: 109: mailbox area(%lu) + slab size(%u) "
1060 			"> nwr region(%lu)",
1061 			mboxareasize, IDN_SLAB_SIZE,
1062 			MB2B(IDN_NWR_SIZE));
1063 		rv = -1;
1064 	}
1065 
1066 	max_num_slabs = (int)((MB2B(IDN_NWR_SIZE) - mboxareasize) /
1067 				(ulong_t)IDN_SLAB_SIZE);
1068 	if (max_num_slabs < IDN_SLAB_MINTOTAL) {
1069 		cmn_err(CE_WARN,
1070 			"IDN: 110: maximum number of slabs(%d) < "
1071 			"minimum required(%d)",
1072 			max_num_slabs, IDN_SLAB_MINTOTAL);
1073 		rv = -1;
1074 	} else {
1075 		IDN_SLAB_MAXPERDOMAIN = max_num_slabs / IDN_SLAB_MINTOTAL;
1076 	}
1077 
1078 #if 0
1079 	if ((IDN_MTU + sizeof (struct ether_header)) > IDN_DATA_SIZE) {
1080 		cmn_err(CE_WARN,
1081 			"IDN: (IDN_MTU(%d) + ether_header(%d)) "
1082 			"> IDN_DATA_SIZE(%lu)",
1083 			IDN_MTU, sizeof (struct ether_header),
1084 			IDN_DATA_SIZE);
1085 		rv = -1;
1086 	}
1087 #endif /* 0 */
1088 
1089 	if (IDN_SMR_BUFSIZE & (IDN_ALIGNSIZE - 1)) {
1090 		cmn_err(CE_WARN,
1091 			"IDN: 111: idn_smr_bufsize(%d) not on a "
1092 			"64 byte boundary", IDN_SMR_BUFSIZE);
1093 		rv = -1;
1094 	}
1095 
1096 	for (i = cnt = 0;
1097 		(cnt <= 1) && (((ulong_t)1 << i) < MB2B(IDN_NWR_SIZE));
1098 		i++)
1099 		if ((1 << i) & IDN_SMR_BUFSIZE)
1100 			cnt++;
1101 	if ((i > 0) && (!cnt || (cnt > 1))) {
1102 		cmn_err(CE_WARN,
1103 			"IDN: 112: idn_smr_bufsize(%d) not a power of 2",
1104 			IDN_SMR_BUFSIZE);
1105 		rv = -1;
1106 	}
1107 
1108 	if ((IDN_MBOX_PER_NET & 1) == 0) {
1109 		cmn_err(CE_WARN,
1110 			"IDN: 113: idn_mbox_per_net(%d) must be an "
1111 			"odd number", IDN_MBOX_PER_NET);
1112 		rv = -1;
1113 	}
1114 
1115 	if (idn.nchannels > 0)
1116 		IDN_WINDOW_EMAX = IDN_WINDOW_MAX +
1117 				((idn.nchannels - 1) * IDN_WINDOW_INCR);
1118 
1119 	if (IDN_NETSVR_WAIT_MIN > IDN_NETSVR_WAIT_MAX) {
1120 		cmn_err(CE_WARN,
1121 			"IDN: 115: idn_netsvr_wait_min(%d) cannot be "
1122 			"greater than idn_netsvr_wait_max(%d)",
1123 			IDN_NETSVR_WAIT_MIN,
1124 			IDN_NETSVR_WAIT_MAX);
1125 		rv = -1;
1126 	}
1127 
1128 	return (rv);
1129 }
1130 
1131 static int
1132 idn_init_smr()
1133 {
1134 	uint64_t	obp_paddr;
1135 	uint64_t	obp_size;	/* in Bytes */
1136 	uint_t		smr_size;	/* in MBytes */
1137 	pgcnt_t		npages;
1138 	procname_t	proc = "idn_init_smr";
1139 
1140 	if (idn.smr.ready)
1141 		return (0);
1142 
1143 	if (idn_prom_getsmr(&smr_size, &obp_paddr, &obp_size) < 0)
1144 		return (-1);
1145 
1146 	PR_PROTO("%s: smr_size = %d, obp_paddr = 0x%lx, obp_size = 0x%lx\n",
1147 		proc, smr_size, obp_paddr, obp_size);
1148 
1149 	if (IDN_SMR_SIZE)
1150 		smr_size = MIN(smr_size, IDN_SMR_SIZE);
1151 
1152 	npages = btopr(MB2B(smr_size));
1153 
1154 	idn.smr.prom_paddr = obp_paddr;
1155 	idn.smr.prom_size = obp_size;
1156 	idn.smr.vaddr = vmem_alloc(heap_arena, ptob(npages), VM_SLEEP);
1157 	ASSERT(((ulong_t)idn.smr.vaddr & MMU_PAGEOFFSET) == 0);
1158 	idn.smr.locpfn = (pfn_t)(obp_paddr >> MMU_PAGESHIFT);
1159 	idn.smr.rempfn = idn.smr.rempfnlim = PFN_INVALID;
1160 	IDN_SMR_SIZE = smr_size;
1161 
1162 	PR_PROTO("%s: smr vaddr = %p\n", proc, idn.smr.vaddr);
1163 
1164 	smr_remap(&kas, idn.smr.vaddr, idn.smr.locpfn, IDN_SMR_SIZE);
1165 
1166 	idn.localid = PADDR_TO_DOMAINID(obp_paddr);
1167 
1168 	idn.smr.ready = 1;
1169 
1170 	return (0);
1171 }
1172 
1173 static void
1174 idn_deinit_smr()
1175 {
1176 	pgcnt_t		npages;
1177 
1178 	if (idn.smr.ready == 0)
1179 		return;
1180 
1181 	smr_remap(&kas, idn.smr.vaddr, PFN_INVALID, IDN_SMR_SIZE);
1182 
1183 	npages = btopr(MB2B(IDN_SMR_SIZE));
1184 
1185 	vmem_free(heap_arena, idn.smr.vaddr, ptob(npages));
1186 
1187 	idn.localid = IDN_NIL_DOMID;
1188 
1189 	IDN_SMR_SIZE = 0;
1190 
1191 	idn.smr.ready = 0;
1192 }
1193 
1194 /*ARGSUSED1*/
1195 static void
1196 idn_sigb_setup(cpu_sgnblk_t *sigbp, void *arg)
1197 {
1198 	procname_t	proc = "idn_sigb_setup";
1199 
1200 	PR_PROTO("%s: Setting sigb to %p\n", proc, sigbp);
1201 
1202 	mutex_enter(&idn.idnsb_mutex);
1203 	if (sigbp == NULL) {
1204 		idn.idnsb = NULL;
1205 		idn.idnsb_eventp = NULL;
1206 		mutex_exit(&idn.idnsb_mutex);
1207 		return;
1208 	}
1209 	idn.idnsb_eventp = (idnsb_event_t *)sigbp->sigb_idn;
1210 	idn.idnsb = (idnsb_t *)&idn.idnsb_eventp->idn_reserved1;
1211 	mutex_exit(&idn.idnsb_mutex);
1212 }
1213 
1214 static int
1215 idn_init(dev_info_t *dip)
1216 {
1217 	struct hwconfig	local_hw;
1218 	procname_t	proc = "idn_init";
1219 
1220 
1221 	ASSERT(MUTEX_HELD(&idn.siplock));
1222 
1223 	if (!idn.enabled) {
1224 		cmn_err(CE_WARN,
1225 			"IDN: 117: IDN not enabled");
1226 		return (-1);
1227 	}
1228 
1229 	if (idn.dip != NULL) {
1230 		PR_DRV("%s: already initialized (dip = 0x%p)\n",
1231 			proc, idn.dip);
1232 		return (0);
1233 	}
1234 
1235 	/*
1236 	 * Determine our local domain's hardware configuration.
1237 	 */
1238 	if (get_hw_config(&local_hw)) {
1239 		cmn_err(CE_WARN,
1240 			"IDN: 118: hardware config not appropriate");
1241 		return (-1);
1242 	}
1243 
1244 	PR_DRV("%s: locpfn = 0x%lx\n", proc, idn.smr.locpfn);
1245 	PR_DRV("%s: rempfn = 0x%lx\n", proc, idn.smr.rempfn);
1246 	PR_DRV("%s: smrsize = %d MB\n", proc, IDN_SMR_SIZE);
1247 
1248 	rw_init(&idn.grwlock, NULL, RW_DEFAULT, NULL);
1249 	rw_init(&idn.struprwlock, NULL, RW_DEFAULT, NULL);
1250 	mutex_init(&idn.sync.sz_mutex, NULL, MUTEX_DEFAULT, NULL);
1251 	mutex_init(&idn.sipwenlock, NULL, MUTEX_DEFAULT, NULL);
1252 
1253 	/*
1254 	 * Calculate proper value for idn.bframe_shift.
1255 	 * Kind of hokey as it assume knowledge of the format
1256 	 * of the idnparam_t structure.
1257 	 */
1258 	{
1259 		int		s;
1260 
1261 		for (s = 0; (1 << s) < IDN_SMR_BUFSIZE_MIN; s++)
1262 			;
1263 		idn.bframe_shift = s;
1264 		PR_DRV("%s: idn.bframe_shift = %d, minbuf = %d\n",
1265 			proc, idn.bframe_shift, IDN_SMR_BUFSIZE_MIN);
1266 
1267 		ASSERT((uint_t)IDN_OFFSET2BFRAME(MB2B(idn_smr_size)) <
1268 			(1 << 24));
1269 	}
1270 
1271 	idn_xmit_monitor_init();
1272 
1273 	/*
1274 	 * Initialize the domain op (dopers) stuff.
1275 	 */
1276 	idn_dopers_init();
1277 
1278 	/*
1279 	 * Initialize the timer (kmem) cache used for timeout
1280 	 * structures.
1281 	 */
1282 	idn_timercache_init();
1283 
1284 	/*
1285 	 * Initialize the slab waiting areas.
1286 	 */
1287 	(void) smr_slabwaiter_init();
1288 
1289 	/*
1290 	 * Initialize retryjob kmem cache.
1291 	 */
1292 	idn_retrytask_init();
1293 
1294 	idn_init_msg_waittime();
1295 	idn_init_msg_retrytime();
1296 
1297 	/*
1298 	 * Initialize idn_domains[] and local domains information
1299 	 * include idn_global information.
1300 	 */
1301 	idn_domains_init(&local_hw);
1302 
1303 	/*
1304 	 * Start up IDN protocol servers.
1305 	 */
1306 	if (idn_protocol_init(idn_protocol_nservers) <= 0) {
1307 		cmn_err(CE_WARN,
1308 			"IDN: 119: failed to initialize %d protocol servers",
1309 			idn_protocol_nservers);
1310 		idn_domains_deinit();
1311 		idn_retrytask_deinit();
1312 		smr_slabwaiter_deinit();
1313 		idn_timercache_deinit();
1314 		idn_dopers_deinit();
1315 		idn_xmit_monitor_deinit();
1316 		mutex_destroy(&idn.sipwenlock);
1317 		mutex_destroy(&idn.sync.sz_mutex);
1318 		rw_destroy(&idn.grwlock);
1319 		rw_destroy(&idn.struprwlock);
1320 		return (-1);
1321 	}
1322 
1323 	/*
1324 	 * Initialize chan_servers array.
1325 	 */
1326 	(void) idn_chanservers_init();
1327 
1328 	/*
1329 	 * Need to register the IDN handler with the DMV subsystem.
1330 	 *
1331 	 * Need to prevent the IDN driver from being unloaded
1332 	 * once loaded since DMV's may come in at any time.
1333 	 * If the driver is not loaded and the idn_dmv_handler
1334 	 * has been registered with the DMV, system will crash.
1335 	 */
1336 	(void) idn_init_handler();
1337 
1338 	idn.dip = dip;
1339 	IDN_GLOCK_EXCL();
1340 	IDN_GSTATE_TRANSITION(IDNGS_OFFLINE);
1341 	IDN_GUNLOCK();
1342 
1343 	return (0);
1344 }
1345 
1346 static int
1347 idn_deinit()
1348 {
1349 	procname_t	proc = "idn_deinit";
1350 
1351 	ASSERT(MUTEX_HELD(&idn.siplock));
1352 
1353 	IDN_GLOCK_EXCL();
1354 
1355 	if (idn.state != IDNGS_OFFLINE) {
1356 		int	d;
1357 
1358 		cmn_err(CE_WARN,
1359 			"IDN: 120: cannot deinit while active "
1360 			"(state = %s)", idngs_str[idn.state]);
1361 
1362 		for (d = 0; d < MAX_DOMAINS; d++) {
1363 			idn_domain_t	*dp;
1364 
1365 			dp = &idn_domain[d];
1366 			if (dp->dcpu < 0)
1367 				continue;
1368 
1369 			cmn_err(CE_CONT,
1370 				"IDN: 121: domain %d (CPU %d, "
1371 				"name \"%s\", state %s)\n",
1372 				d, dp->dcpu, dp->dname,
1373 				idnds_str[dp->dstate]);
1374 		}
1375 		IDN_GUNLOCK();
1376 		return (-1);
1377 	}
1378 
1379 	if (idn.dip == NULL) {
1380 		PR_DRV("%s: already deinitialized\n", proc);
1381 		IDN_GUNLOCK();
1382 		return (0);
1383 	}
1384 
1385 	IDN_GSTATE_TRANSITION(IDNGS_IGNORE);
1386 
1387 	IDN_GUNLOCK();
1388 
1389 	idn_xmit_monitor_deinit();
1390 
1391 	idn_deinit_handler();
1392 
1393 	idn_chanservers_deinit();
1394 
1395 	idn.nchannels = 0;
1396 	ASSERT(idn.chan_servers == NULL);
1397 
1398 	smr_slabpool_deinit();
1399 
1400 	idn_protocol_deinit();
1401 
1402 	idn_domains_deinit();
1403 
1404 	smr_slabwaiter_deinit();
1405 
1406 	idn_retrytask_deinit();
1407 
1408 	idn_timercache_deinit();
1409 
1410 	idn_dopers_deinit();
1411 
1412 	ASSERT(idn.localid == IDN_NIL_DOMID);
1413 
1414 	IDN_SET_MASTERID(IDN_NIL_DOMID);
1415 
1416 	idn_deinit_smr();
1417 
1418 	mutex_destroy(&idn.sipwenlock);
1419 	mutex_destroy(&idn.sync.sz_mutex);
1420 	rw_destroy(&idn.grwlock);
1421 	rw_destroy(&idn.struprwlock);
1422 
1423 	idn.dip = NULL;
1424 
1425 	return (0);
1426 }
1427 
1428 static void
1429 idn_xmit_monitor_init()
1430 {
1431 	mutex_init(&idn.xmit_lock, NULL, MUTEX_DEFAULT, NULL);
1432 	idn.xmit_tid = (timeout_id_t)NULL;
1433 	CHANSET_ZERO(idn.xmit_chanset_wanted);
1434 }
1435 
1436 static void
1437 idn_xmit_monitor_deinit()
1438 {
1439 	timeout_id_t	tid;
1440 
1441 	mutex_enter(&idn.xmit_lock);
1442 	CHANSET_ZERO(idn.xmit_chanset_wanted);
1443 	if ((tid = idn.xmit_tid) != (timeout_id_t)NULL) {
1444 		idn.xmit_tid = (timeout_id_t)NULL;
1445 		mutex_exit(&idn.xmit_lock);
1446 		(void) untimeout(tid);
1447 	} else {
1448 		mutex_exit(&idn.xmit_lock);
1449 	}
1450 	mutex_destroy(&idn.xmit_lock);
1451 }
1452 
1453 static void
1454 idn_init_msg_waittime()
1455 {
1456 	idn_msg_waittime[IDNP_NULL] = -1;
1457 	idn_msg_waittime[IDNP_NEGO] = idn_msgwait_nego * hz;
1458 	idn_msg_waittime[IDNP_CFG]  = idn_msgwait_cfg * hz;
1459 	idn_msg_waittime[IDNP_CON]  = idn_msgwait_con * hz;
1460 	idn_msg_waittime[IDNP_FIN]  = idn_msgwait_fin * hz;
1461 	idn_msg_waittime[IDNP_CMD]  = idn_msgwait_cmd * hz;
1462 	idn_msg_waittime[IDNP_DATA] = idn_msgwait_data * hz;
1463 }
1464 
1465 static void
1466 idn_init_msg_retrytime()
1467 {
1468 	idn_msg_retrytime[(int)IDNRETRY_NIL]	 = -1;
1469 	idn_msg_retrytime[(int)IDNRETRY_NEGO]	 = idn_retryfreq_nego * hz;
1470 	idn_msg_retrytime[(int)IDNRETRY_CON]	 = idn_retryfreq_con * hz;
1471 	idn_msg_retrytime[(int)IDNRETRY_CONQ]	 = idn_retryfreq_con * hz;
1472 	idn_msg_retrytime[(int)IDNRETRY_FIN]	 = idn_retryfreq_fin * hz;
1473 	idn_msg_retrytime[(int)IDNRETRY_FINQ]	 = idn_retryfreq_fin * hz;
1474 }
1475 
1476 /*
1477  * ----------------------------------------------
1478  */
1479 /*ARGSUSED*/
1480 static int
1481 idnopen(register queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *crp)
1482 {
1483 	register int	err = 0;
1484 	int		minordev;
1485 	struct idnstr	*stp, **pstp;
1486 	procname_t	proc = "idnopen";
1487 
1488 	ASSERT(sflag != MODOPEN);
1489 
1490 	IDN_GLOCK_EXCL();
1491 
1492 	rw_enter(&idn.struprwlock, RW_WRITER);
1493 	mutex_enter(&idn.sipwenlock);
1494 	pstp = &idn.strup;
1495 
1496 	if (idn.enabled == 0) {
1497 		PR_DRV("%s: Driver disabled (check OBP:idn-smr-size)\n",
1498 			proc);
1499 		mutex_exit(&idn.sipwenlock);
1500 		rw_exit(&idn.struprwlock);
1501 		IDN_GUNLOCK();
1502 		return (EACCES);
1503 	}
1504 
1505 	if (!idn_ndlist &&
1506 	    idn_param_register(idn_param_arr, A_CNT(idn_param_arr))) {
1507 		PR_DRV("%s: failed to register ndd parameters\n", proc);
1508 		mutex_exit(&idn.sipwenlock);
1509 		rw_exit(&idn.struprwlock);
1510 		IDN_GUNLOCK();
1511 		return (ENOMEM);
1512 	}
1513 	IDN_GUNLOCK();
1514 
1515 	if (sflag == CLONEOPEN) {
1516 		minordev = 0;
1517 		for (stp = *pstp; stp; pstp = &stp->ss_nextp, stp = *pstp) {
1518 			if (minordev < stp->ss_minor)
1519 				break;
1520 			minordev++;
1521 		}
1522 		*devp = makedevice(getmajor(*devp), minordev);
1523 	} else {
1524 		minordev = getminor(*devp);
1525 	}
1526 	if (rq->q_ptr)
1527 		goto done;
1528 
1529 	stp = GETSTRUCT(struct idnstr, 1);
1530 	stp->ss_rq = rq;
1531 	stp->ss_minor = minordev;
1532 	rw_init(&stp->ss_rwlock, NULL, RW_DEFAULT, NULL);
1533 	/*
1534 	 * DLPI stuff
1535 	 */
1536 	stp->ss_sip = NULL;
1537 	stp->ss_state = DL_UNATTACHED;
1538 	stp->ss_sap = 0;
1539 	stp->ss_flags = 0;
1540 	stp->ss_mccount = 0;
1541 	stp->ss_mctab = NULL;
1542 
1543 	/*
1544 	 * Link new entry into list of actives.
1545 	 */
1546 	stp->ss_nextp = *pstp;
1547 	*pstp = stp;
1548 
1549 	WR(rq)->q_ptr = rq->q_ptr = (void *)stp;
1550 	/*
1551 	 * Disable automatic enabling of our write service
1552 	 * procedure.  We control this explicitly.
1553 	 */
1554 	noenable(WR(rq));
1555 
1556 	/*
1557 	 * Set our STREAMs queue maximum packet size that
1558 	 * we'll accept and our high/low water marks.
1559 	 */
1560 	(void) strqset(WR(rq), QMAXPSZ, 0, IDN_DATA_SIZE);
1561 	(void) strqset(WR(rq), QLOWAT,  0, IDN_LOWAT);
1562 	(void) strqset(WR(rq), QHIWAT,  0, IDN_HIWAT);
1563 	(void) strqset(rq, QMAXPSZ, 0, IDN_DATA_SIZE);
1564 	(void) strqset(rq, QLOWAT,  0, IDN_LOWAT);
1565 	(void) strqset(rq, QHIWAT,  0, IDN_HIWAT);
1566 
1567 done:
1568 	mutex_exit(&idn.sipwenlock);
1569 	rw_exit(&idn.struprwlock);
1570 
1571 	(void) qassociate(rq, -1);
1572 	qprocson(rq);
1573 
1574 	return (err);
1575 }
1576 
1577 /*
1578  * ----------------------------------------------
1579  */
1580 /*ARGSUSED1*/
1581 static int
1582 idnclose(queue_t *rq, int flag, cred_t *crp)
1583 {
1584 	struct idnstr	*stp, **pstp;
1585 
1586 	ASSERT(rq->q_ptr);
1587 
1588 	qprocsoff(rq);
1589 	/*
1590 	 * Guaranteed to be single threaded with respect
1591 	 * to this stream at this point.
1592 	 */
1593 
1594 	stp = (struct idnstr *)rq->q_ptr;
1595 
1596 	if (stp->ss_sip)
1597 		idndl_dodetach(stp);
1598 
1599 	rw_enter(&idn.struprwlock, RW_WRITER);
1600 	mutex_enter(&idn.sipwenlock);
1601 	pstp = &idn.strup;
1602 	for (stp = *pstp; stp; pstp = &stp->ss_nextp, stp = *pstp)
1603 		if (stp == (struct idnstr *)rq->q_ptr)
1604 			break;
1605 	ASSERT(stp);
1606 	ASSERT(stp->ss_rq == rq);
1607 	*pstp = stp->ss_nextp;
1608 
1609 	rw_destroy(&stp->ss_rwlock);
1610 	FREESTRUCT(stp, struct idnstr, 1);
1611 
1612 	WR(rq)->q_ptr = rq->q_ptr = NULL;
1613 	mutex_exit(&idn.sipwenlock);
1614 	rw_exit(&idn.struprwlock);
1615 
1616 	idn_param_cleanup();
1617 	(void) qassociate(rq, -1);
1618 
1619 	return (0);
1620 }
1621 
1622 /*
1623  * ----------------------------------------------
1624  */
1625 static int
1626 idnwput(register queue_t *wq, register mblk_t *mp)
1627 {
1628 	register struct idnstr	*stp;
1629 	struct idn		*sip;
1630 	procname_t	proc = "idnwput";
1631 
1632 	stp = (struct idnstr *)wq->q_ptr;
1633 	sip = stp->ss_sip;
1634 
1635 	switch (DB_TYPE(mp)) {
1636 	case M_IOCTL:
1637 		idnioctl(wq, mp);
1638 		break;
1639 
1640 	case M_DATA:
1641 		if (((stp->ss_flags & (IDNSFAST|IDNSRAW)) == 0) ||
1642 				(stp->ss_state != DL_IDLE) ||
1643 				(sip == NULL)) {
1644 			PR_DLPI("%s: fl=0x%x, st=0x%x, ret(EPROTO)\n",
1645 				proc, stp->ss_flags, stp->ss_state);
1646 			merror(wq, mp, EPROTO);
1647 
1648 		} else if (wq->q_first) {
1649 			if (putq(wq, mp) == 0)
1650 				freemsg(mp);
1651 			/*
1652 			 * We're only holding the reader lock,
1653 			 * but that's okay since this field
1654 			 * is just a soft-flag.
1655 			 */
1656 			sip->si_wantw = 1;
1657 			qenable(wq);
1658 
1659 		} else if (sip->si_flags & IDNPROMISC) {
1660 			if (putq(wq, mp) == 0) {
1661 				PR_DLPI("%s: putq failed\n", proc);
1662 				freemsg(mp);
1663 			} else {
1664 				PR_DLPI("%s: putq succeeded\n", proc);
1665 			}
1666 			qenable(wq);
1667 
1668 		} else {
1669 			PR_DLPI("%s: idndl_start(sip=0x%p)\n",
1670 				proc, sip);
1671 			rw_enter(&stp->ss_rwlock, RW_READER);
1672 			(void) idndl_start(wq, mp, sip);
1673 			rw_exit(&stp->ss_rwlock);
1674 		}
1675 		break;
1676 
1677 	case M_PROTO:
1678 	case M_PCPROTO:
1679 		/*
1680 		 * Break the association between the current thread
1681 		 * and the thread that calls idndl_proto() to resolve
1682 		 * the problem of idn_chan_server() threads which
1683 		 * loop back around to call idndl_proto and try to
1684 		 * recursively acquire internal locks.
1685 		 */
1686 		if (putq(wq, mp) == 0)
1687 			freemsg(mp);
1688 		qenable(wq);
1689 		break;
1690 
1691 	case M_FLUSH:
1692 		PR_STR("%s: M_FLUSH request (flush = %d)\n",
1693 			proc, (int)*mp->b_rptr);
1694 		if (*mp->b_rptr & FLUSHW) {
1695 			flushq(wq, FLUSHALL);
1696 			*mp->b_rptr &= ~FLUSHW;
1697 		}
1698 		if (*mp->b_rptr & FLUSHR)
1699 			qreply(wq, mp);
1700 		else
1701 			freemsg(mp);
1702 		break;
1703 
1704 	default:
1705 		PR_STR("%s: unexpected DB_TYPE 0x%x\n",
1706 			proc, DB_TYPE(mp));
1707 		freemsg(mp);
1708 		break;
1709 	}
1710 
1711 	return (0);
1712 }
1713 
1714 /*
1715  * ----------------------------------------------
1716  */
1717 static int
1718 idnwsrv(queue_t *wq)
1719 {
1720 	mblk_t		*mp;
1721 	int		err = 0;
1722 	struct idnstr	*stp;
1723 	struct idn	*sip;
1724 	procname_t	proc = "idnwsrv";
1725 
1726 	stp = (struct idnstr *)wq->q_ptr;
1727 	sip = stp->ss_sip;
1728 
1729 	while (mp = getq(wq)) {
1730 		switch (DB_TYPE(mp)) {
1731 		case M_DATA:
1732 			if (sip) {
1733 				PR_DLPI("%s: idndl_start(sip=0x%p)\n",
1734 					proc, sip);
1735 				rw_enter(&stp->ss_rwlock, RW_READER);
1736 				err = idndl_start(wq, mp, sip);
1737 				rw_exit(&stp->ss_rwlock);
1738 				if (err)
1739 					goto done;
1740 			} else {
1741 				PR_DLPI("%s: NO sip to start msg\n", proc);
1742 				freemsg(mp);
1743 			}
1744 			break;
1745 
1746 		case M_PROTO:
1747 		case M_PCPROTO:
1748 			idndl_proto(wq, mp);
1749 			break;
1750 
1751 		default:
1752 			ASSERT(0);
1753 			PR_STR("%s: unexpected db_type (%d)\n",
1754 				proc, DB_TYPE(mp));
1755 			freemsg(mp);
1756 			break;
1757 		}
1758 	}
1759 done:
1760 	return (0);
1761 }
1762 
1763 /*
1764  * ----------------------------------------------
1765  */
1766 static int
1767 idnrput(register queue_t *rq, register mblk_t *mp)
1768 {
1769 	register int	err = 0;
1770 	procname_t	proc = "idnrput";
1771 
1772 	switch (DB_TYPE(mp)) {
1773 	case M_DATA:
1774 		/*
1775 		 * Should not reach here with data packets
1776 		 * if running DLPI.
1777 		 */
1778 		cmn_err(CE_WARN,
1779 			"IDN: 123: unexpected M_DATA packets for "
1780 			"q_stream 0x%p", rq->q_stream);
1781 		freemsg(mp);
1782 		err = ENXIO;
1783 		break;
1784 
1785 	case M_FLUSH:
1786 		PR_STR("%s: M_FLUSH request (flush = %d)\n",
1787 			proc, (int)*mp->b_rptr);
1788 		if (*mp->b_rptr & FLUSHR)
1789 			flushq(rq, FLUSHALL);
1790 		(void) putnext(rq, mp);
1791 		break;
1792 
1793 	case M_ERROR:
1794 		PR_STR("%s: M_ERROR (error = %d) coming through\n",
1795 			proc, (int)*mp->b_rptr);
1796 		(void) putnext(rq, mp);
1797 		break;
1798 	default:
1799 		PR_STR("%s: unexpected DB_TYPE 0x%x\n",
1800 			proc, DB_TYPE(mp));
1801 		freemsg(mp);
1802 		err = ENXIO;
1803 		break;
1804 	}
1805 
1806 	return (err);
1807 }
1808 
1809 /*
1810  * ----------------------------------------------
1811  * Not allowed to enqueue messages!  Only M_DATA messages
1812  * can be enqueued on the write stream.
1813  * ----------------------------------------------
1814  */
1815 static void
1816 idnioctl(register queue_t *wq, register mblk_t *mp)
1817 {
1818 	register struct iocblk	*iocp;
1819 	register int	cmd;
1820 	idnop_t		*idnop = NULL;
1821 	int		error = 0;
1822 	int		argsize;
1823 	procname_t	proc = "idnioctl";
1824 
1825 	iocp = (struct iocblk *)mp->b_rptr;
1826 	cmd  = iocp->ioc_cmd;
1827 
1828 	/*
1829 	 * Intercept DLPI ioctl's.
1830 	 */
1831 	if (VALID_DLPIOP(cmd)) {
1832 		PR_STR("%s: DLPI ioctl(%d)\n", proc, cmd);
1833 		error = idnioc_dlpi(wq, mp, &argsize);
1834 		goto done;
1835 	}
1836 
1837 	/*
1838 	 * Validate expected arguments.
1839 	 */
1840 	if (!VALID_IDNIOCTL(cmd)) {
1841 		PR_STR("%s: invalid cmd (0x%x)\n", proc, cmd);
1842 		error = EINVAL;
1843 		goto done;
1844 
1845 	} else if (!VALID_NDOP(cmd)) {
1846 		error = miocpullup(mp, sizeof (idnop_t));
1847 		if (error != 0) {
1848 			PR_STR("%s: idnioc(cmd = 0x%x) miocpullup "
1849 			    "failed (%d)\n", proc, cmd, error);
1850 			goto done;
1851 		}
1852 	}
1853 
1854 	argsize = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1855 	idnop = (idnop_t *)mp->b_cont->b_rptr;
1856 
1857 	switch (cmd) {
1858 	case IDNIOC_LINK:
1859 		error = idnioc_link(idnop);
1860 		break;
1861 
1862 	case IDNIOC_UNLINK:
1863 		error = idnioc_unlink(idnop);
1864 		break;
1865 
1866 	case IDNIOC_MEM_RW:
1867 		error = idn_rw_mem(idnop);
1868 		break;
1869 
1870 	case IDNIOC_PING:
1871 		error = idn_send_ping(idnop);
1872 		break;
1873 
1874 	case ND_SET:
1875 		IDN_GLOCK_EXCL();
1876 		if (!nd_getset(wq, idn_ndlist, mp)) {
1877 			IDN_GUNLOCK();
1878 			error = ENOENT;
1879 			break;
1880 		}
1881 		IDN_GUNLOCK();
1882 		qreply(wq, mp);
1883 		return;
1884 
1885 	case ND_GET:
1886 		IDN_GLOCK_SHARED();
1887 		if (!nd_getset(wq, idn_ndlist, mp)) {
1888 			IDN_GUNLOCK();
1889 			error = ENOENT;
1890 			break;
1891 		}
1892 		IDN_GUNLOCK();
1893 		qreply(wq, mp);
1894 		return;
1895 
1896 	default:
1897 		PR_STR("%s: invalid cmd 0x%x\n", proc, cmd);
1898 		error = EINVAL;
1899 		break;
1900 	}
1901 
1902 done:
1903 	if (error == 0)
1904 		miocack(wq, mp, argsize, 0);
1905 	else
1906 		miocnak(wq, mp, 0, error);
1907 }
1908 
1909 /*
1910  * This thread actually services the SSI_LINK/UNLINK calls
1911  * asynchronously that come via BBSRAM.  This is necessary
1912  * since we can't process them from within the context of
1913  * the interrupt handler in which idn_sigbhandler() is
1914  * called.
1915  */
1916 static void
1917 idn_sigbhandler_thread(struct sigbintr **sbpp)
1918 {
1919 	int		d, pri, rv;
1920 	struct sigbintr	*sbp;
1921 	sigbmbox_t	*mbp;
1922 	idn_fin_t	fintype;
1923 	idnsb_data_t	*sdp;
1924 	idnsb_info_t	*sfp;
1925 	idnsb_error_t	*sep;
1926 	idn_domain_t	*dp;
1927 	procname_t	proc = "idn_sigbhandler_thread";
1928 
1929 
1930 	sbp = *sbpp;
1931 
1932 	PR_PROTO("%s: KICKED OFF (sigbintr pointer = 0x%p)\n",
1933 		proc, sbp);
1934 
1935 	ASSERT(sbp == &idn.sigbintr);
1936 
1937 	mutex_enter(&idn.sigbintr.sb_mutex);
1938 
1939 	while (sbp->sb_busy != IDNSIGB_DIE) {
1940 		cpu_sgnblk_t	*sigbp;
1941 
1942 		while ((sbp->sb_busy != IDNSIGB_ACTIVE) &&
1943 				(sbp->sb_busy != IDNSIGB_DIE)) {
1944 			cv_wait(&sbp->sb_cv, &idn.sigbintr.sb_mutex);
1945 			PR_PROTO("%s: AWAKENED (busy = %d)\n",
1946 				proc, (int)sbp->sb_busy);
1947 		}
1948 		if (sbp->sb_busy == IDNSIGB_DIE) {
1949 			PR_PROTO("%s: DIE REQUESTED\n", proc);
1950 			break;
1951 		}
1952 
1953 		if ((sigbp = cpu_sgnblkp[sbp->sb_cpuid]) == NULL) {
1954 			cmn_err(CE_WARN,
1955 				"IDN: 124: sigblk for CPU ID %d "
1956 				"is NULL", sbp->sb_cpuid);
1957 			sbp->sb_busy = IDNSIGB_INACTIVE;
1958 			continue;
1959 		}
1960 
1961 		mbp = &sigbp->sigb_host_mbox;
1962 
1963 		if (mbp->flag != SIGB_MBOX_BUSY) {
1964 			PR_PROTO("%s: sigblk mbox flag (%d) != BUSY (%d)\n",
1965 				proc, mbp->flag, SIGB_MBOX_BUSY);
1966 			sbp->sb_busy = IDNSIGB_INACTIVE;
1967 			continue;
1968 		}
1969 		/*
1970 		 * The sb_busy bit is set and the mailbox flag
1971 		 * indicates BUSY also, so we effectively have things locked.
1972 		 * So, we can drop the critical sb_mutex which we want to
1973 		 * do since it pushes us to PIL 14 while we hold it and we
1974 		 * don't want to run at PIL 14 across IDN code.
1975 		 */
1976 		mutex_exit(&idn.sigbintr.sb_mutex);
1977 
1978 		sdp = (idnsb_data_t *)mbp->data;
1979 		sep = (idnsb_error_t *)&sdp->ssb_error;
1980 		INIT_IDNKERR(sep);
1981 
1982 		if (mbp->len != sizeof (idnsb_data_t)) {
1983 			PR_PROTO("%s: sigblk mbox length (%d) != "
1984 				"expected (%lu)\n", proc, mbp->len,
1985 				sizeof (idnsb_data_t));
1986 			SET_IDNKERR_ERRNO(sep, EINVAL);
1987 			SET_IDNKERR_IDNERR(sep, IDNKERR_DATA_LEN);
1988 			SET_IDNKERR_PARAM0(sep, sizeof (idnsb_data_t));
1989 
1990 			goto sberr;
1991 
1992 		}
1993 		if (idn.enabled == 0) {
1994 #ifdef DEBUG
1995 			cmn_err(CE_NOTE,
1996 				"IDN: 102: driver disabled "
1997 				"- check OBP environment "
1998 				"(idn-smr-size)");
1999 #else /* DEBUG */
2000 			cmn_err(CE_NOTE,
2001 				"!IDN: 102: driver disabled "
2002 				"- check OBP environment "
2003 				"(idn-smr-size)");
2004 #endif /* DEBUG */
2005 			SET_IDNKERR_ERRNO(sep, EACCES);
2006 			SET_IDNKERR_IDNERR(sep, IDNKERR_DRV_DISABLED);
2007 
2008 			goto sberr;
2009 
2010 		}
2011 
2012 		switch (mbp->cmd) {
2013 
2014 		case SSI_LINK:
2015 		{
2016 			idnsb_link_t	slp;
2017 
2018 			bcopy(&sdp->ssb_link, &slp, sizeof (slp));
2019 
2020 			if (slp.master_pri < 0) {
2021 				pri = IDNVOTE_MINPRI;
2022 			} else if (slp.master_pri > 0) {
2023 				/*
2024 				 * If I'm already in a IDN network,
2025 				 * then my vote priority is set to
2026 				 * the max, otherwise it's one-less.
2027 				 */
2028 				pri = IDNVOTE_MAXPRI;
2029 				IDN_GLOCK_SHARED();
2030 				if (idn.ndomains <= 1)
2031 					pri--;
2032 				IDN_GUNLOCK();
2033 			} else {
2034 				pri = IDNVOTE_DEFPRI;
2035 			}
2036 
2037 			PR_PROTO("%s: SSI_LINK(cpuid = %d, domid = %d, "
2038 				"pri = %d (req = %d), t/o = %d)\n",
2039 				proc, slp.cpuid, slp.domid, pri,
2040 				slp.master_pri, slp.timeout);
2041 
2042 			rv = idn_link(slp.domid, slp.cpuid, pri,
2043 					slp.timeout, sep);
2044 			SET_IDNKERR_ERRNO(sep, rv);
2045 			(void) idn_info(&sdp->ssb_info);
2046 			break;
2047 		}
2048 
2049 		case SSI_UNLINK:
2050 		{
2051 			idnsb_unlink_t	sup;
2052 			idn_domain_t	*xdp;
2053 			domainset_t	domset;
2054 
2055 			bcopy(&sdp->ssb_unlink, &sup, sizeof (sup));
2056 
2057 			PR_PROTO("%s: SSI_UNLINK(c = %d, d = %d, bs = 0x%x, "
2058 				"f = %d, is = 0x%x, t/o = %d)\n",
2059 				proc, sup.cpuid, sup.domid, sup.boardset,
2060 				sup.force, sup.idnset, sup.timeout);
2061 
2062 			domset = idn.domset.ds_trans_on |
2063 					idn.domset.ds_connected |
2064 					idn.domset.ds_trans_off |
2065 					idn.domset.ds_awol |
2066 					idn.domset.ds_relink;
2067 
2068 			if (VALID_DOMAINID(sup.domid)) {
2069 				dp = &idn_domain[sup.domid];
2070 			} else if (VALID_CPUID(sup.cpuid)) {
2071 				for (d = 0; d < MAX_DOMAINS; d++) {
2072 					xdp = &idn_domain[d];
2073 
2074 					if ((xdp->dcpu == IDN_NIL_DCPU) &&
2075 					    !DOMAIN_IN_SET(domset, d))
2076 						continue;
2077 
2078 					if (CPU_IN_SET(xdp->dcpuset,
2079 							sup.cpuid))
2080 						break;
2081 				}
2082 				dp = (d == MAX_DOMAINS) ? NULL : xdp;
2083 			}
2084 			if ((dp == NULL) && sup.boardset) {
2085 				for (d = 0; d < MAX_DOMAINS; d++) {
2086 					xdp = &idn_domain[d];
2087 
2088 					if ((xdp->dcpu == IDN_NIL_DCPU) &&
2089 					    !DOMAIN_IN_SET(domset, d))
2090 						continue;
2091 
2092 					if (xdp->dhw.dh_boardset &
2093 							sup.boardset)
2094 						break;
2095 				}
2096 				dp = (d == MAX_DOMAINS) ? NULL : xdp;
2097 			}
2098 			if (dp == NULL) {
2099 				SET_IDNKERR_ERRNO(sep, EINVAL);
2100 				SET_IDNKERR_IDNERR(sep, IDNKERR_INVALID_DOMAIN);
2101 				SET_IDNKERR_PARAM0(sep, sup.domid);
2102 				SET_IDNKERR_PARAM1(sep, sup.cpuid);
2103 				(void) idn_info(&sdp->ssb_info);
2104 				goto sberr;
2105 			} else {
2106 				sup.domid = dp->domid;
2107 			}
2108 
2109 			switch (sup.force) {
2110 			case SSIFORCE_OFF:
2111 				fintype = IDNFIN_NORMAL;
2112 				break;
2113 
2114 			case SSIFORCE_SOFT:
2115 				fintype = IDNFIN_FORCE_SOFT;
2116 				break;
2117 
2118 			case SSIFORCE_HARD:
2119 				fintype = IDNFIN_FORCE_HARD;
2120 				break;
2121 			default:
2122 				SET_IDNKERR_ERRNO(sep, EINVAL);
2123 				SET_IDNKERR_IDNERR(sep, IDNKERR_INVALID_FORCE);
2124 				SET_IDNKERR_PARAM0(sep, sup.force);
2125 				(void) idn_info(&sdp->ssb_info);
2126 				goto sberr;
2127 			}
2128 
2129 			rv = idn_unlink(sup.domid, sup.idnset, fintype,
2130 					IDNFIN_OPT_UNLINK, sup.timeout, sep);
2131 			SET_IDNKERR_ERRNO(sep, rv);
2132 			(void) idn_info(&sdp->ssb_info);
2133 			break;
2134 		}
2135 
2136 		case SSI_INFO:
2137 			sfp = &sdp->ssb_info;
2138 
2139 			PR_PROTO("%s: SSI_INFO\n", proc);
2140 
2141 			rv = idn_info(sfp);
2142 			SET_IDNKERR_ERRNO(sep, rv);
2143 			if (rv != 0) {
2144 				SET_IDNKERR_IDNERR(sep, IDNKERR_INFO_FAILED);
2145 			}
2146 			break;
2147 
2148 		default:
2149 			ASSERT(0);
2150 			SET_IDNKERR_ERRNO(sep, EINVAL);
2151 			SET_IDNKERR_IDNERR(sep, IDNKERR_INVALID_CMD);
2152 			SET_IDNKERR_PARAM0(sep, mbp->cmd);
2153 			break;
2154 		}
2155 
2156 sberr:
2157 
2158 		if (GET_IDNKERR_ERRNO(sep) != 0) {
2159 			cmn_err(CE_WARN,
2160 #ifdef DEBUG
2161 				"IDN: 125: op (%s) failed, returning "
2162 				"(%d/0x%x [%d, %d, %d])",
2163 #else /* DEBUG */
2164 				"!IDN: 125: op (%s) failed, returning "
2165 				"(%d/0x%x [%d, %d, %d])",
2166 #endif /* DEBUG */
2167 				(mbp->cmd == SSI_LINK) ? "LINK" :
2168 				(mbp->cmd == SSI_UNLINK) ? "UNLINK" :
2169 				(mbp->cmd == SSI_INFO) ?
2170 					"INFO" : "UNKNOWN",
2171 				GET_IDNKERR_ERRNO(sep),
2172 				GET_IDNKERR_IDNERR(sep),
2173 				GET_IDNKERR_PARAM0(sep),
2174 				GET_IDNKERR_PARAM1(sep),
2175 				GET_IDNKERR_PARAM2(sep));
2176 		}
2177 
2178 		PR_PROTO("%s: returning errno = %d, idnerr = %d, "
2179 			"params = [%d, %d, %d]\n",
2180 			proc, GET_IDNKERR_ERRNO(sep), GET_IDNKERR_IDNERR(sep),
2181 			GET_IDNKERR_PARAM0(sep), GET_IDNKERR_PARAM1(sep),
2182 			GET_IDNKERR_PARAM2(sep));
2183 
2184 		mutex_enter(&idn.sigbintr.sb_mutex);
2185 		ASSERT((sbp->sb_busy == IDNSIGB_ACTIVE) ||
2186 			(sbp->sb_busy == IDNSIGB_DIE));
2187 		mbp->cmd |= SSI_ACK;
2188 		if (sbp->sb_busy == IDNSIGB_ACTIVE)
2189 			sbp->sb_busy = IDNSIGB_INACTIVE;
2190 		/*
2191 		 * Set flag which kicks off response to SSP.
2192 		 */
2193 		membar_stst_ldst();
2194 		mbp->flag = HOST_TO_CBS;
2195 	}
2196 
2197 	/*
2198 	 * Wake up the dude that killed us!
2199 	 */
2200 	idn.sigb_threadp = NULL;
2201 	cv_signal(&sbp->sb_cv);
2202 	mutex_exit(&idn.sigbintr.sb_mutex);
2203 	thread_exit();
2204 }
2205 
2206 /*
2207  * Create the thread that will service sigb interrupts.
2208  */
2209 static void
2210 idn_sigbhandler_create()
2211 {
2212 	struct sigbintr	*sbp;
2213 
2214 	if (idn.sigb_threadp) {
2215 		cmn_err(CE_WARN,
2216 			"IDN: 126: sigbhandler thread already "
2217 			"exists (0x%p)", idn.sigb_threadp);
2218 		return;
2219 	}
2220 	cv_init(&idn.sigbintr.sb_cv, NULL, CV_DEFAULT, NULL);
2221 	sbp = &idn.sigbintr;
2222 	sbp->sb_busy = IDNSIGB_INACTIVE;
2223 	idn.sigb_threadp = thread_create(NULL, 0,
2224 	    idn_sigbhandler_thread, &sbp, sizeof (sbp), &p0,
2225 	    TS_RUN, minclsyspri);
2226 	sbp->sb_inum = add_softintr((uint_t)idn_sigbpil,
2227 	    idn_sigbhandler_wakeup, 0);
2228 }
2229 
2230 static void
2231 idn_sigbhandler_kill()
2232 {
2233 	if (idn.sigb_threadp) {
2234 		struct sigbintr	*sbp;
2235 
2236 		sbp = &idn.sigbintr;
2237 		if (sbp->sb_inum != 0)
2238 			rem_softintr(sbp->sb_inum);
2239 		sbp->sb_inum = 0;
2240 		sbp->sb_busy = IDNSIGB_DIE;
2241 		cv_signal(&sbp->sb_cv);
2242 		while (idn.sigb_threadp != NULL)
2243 			cv_wait(&sbp->sb_cv, &idn.sigbintr.sb_mutex);
2244 		sbp->sb_busy = IDNSIGB_INACTIVE;
2245 		cv_destroy(&sbp->sb_cv);
2246 	}
2247 }
2248 
2249 /*ARGSUSED0*/
2250 static uint_t
2251 idn_sigbhandler_wakeup(caddr_t arg1, caddr_t arg2)
2252 {
2253 	mutex_enter(&idn.sigbintr.sb_mutex);
2254 	if (idn.sigbintr.sb_busy == IDNSIGB_STARTED) {
2255 		idn.sigbintr.sb_busy = IDNSIGB_ACTIVE;
2256 		cv_signal(&idn.sigbintr.sb_cv);
2257 	}
2258 	mutex_exit(&idn.sigbintr.sb_mutex);
2259 
2260 	return (DDI_INTR_CLAIMED);
2261 }
2262 
2263 static void
2264 idn_sigbhandler(processorid_t cpuid, cpu_sgnblk_t *sgnblkp)
2265 {
2266 	struct sigbintr	*sbp = &idn.sigbintr;
2267 	sigbmbox_t	*mbp;
2268 	idnsb_data_t	*sdp;
2269 	idnsb_error_t	*sep;
2270 	uint32_t	cmd;
2271 	int		sigb_lock = 0;
2272 
2273 	ASSERT(sgnblkp);
2274 
2275 	mbp = &sgnblkp->sigb_host_mbox;
2276 	sdp = (idnsb_data_t *)mbp->data;
2277 	sep = &sdp->ssb_error;
2278 	cmd = mbp->cmd;
2279 
2280 	if ((mbp->flag != CBS_TO_HOST) || !VALID_IDNSIGBCMD(cmd)) {
2281 		/*
2282 		 * Not a valid IDN command.  Just bail out.
2283 		 */
2284 		return;
2285 	}
2286 
2287 	mbp->flag = SIGB_MBOX_BUSY;
2288 	SET_IDNKERR_ERRNO(sep, 0);
2289 
2290 	if (cmd & SSI_ACK) {
2291 		/*
2292 		 * Hmmm...weird, the ACK bit is set.
2293 		 */
2294 		SET_IDNKERR_ERRNO(sep, EPROTO);
2295 		SET_IDNKERR_IDNERR(sep, IDNKERR_INVALID_CMD);
2296 		SET_IDNKERR_PARAM0(sep, cmd);
2297 		goto sigb_done;
2298 	}
2299 
2300 	if (!mutex_tryenter(&idn.sigbintr.sb_mutex)) {
2301 		/*
2302 		 * Couldn't get the lock.  Driver is either
2303 		 * not quite all the way up or is shutting down
2304 		 * for some reason.  Caller should spin again.
2305 		 */
2306 		cmd |= SSI_ACK;
2307 		SET_IDNKERR_ERRNO(sep, EBUSY);
2308 		SET_IDNKERR_IDNERR(sep, IDNKERR_SIGBINTR_LOCKED);
2309 		goto sigb_done;
2310 	}
2311 	sigb_lock = 1;
2312 
2313 	if ((idn.sigb_threadp == NULL) ||
2314 			(sbp->sb_busy == IDNSIGB_NOTREADY)) {
2315 		cmd |= SSI_ACK;
2316 		SET_IDNKERR_ERRNO(sep, EAGAIN);
2317 		SET_IDNKERR_IDNERR(sep, IDNKERR_SIGBINTR_NOTRDY);
2318 		goto sigb_done;
2319 	}
2320 
2321 	if (sbp->sb_busy != IDNSIGB_INACTIVE) {
2322 		cmd |= SSI_ACK;
2323 		SET_IDNKERR_ERRNO(sep, EBUSY);
2324 		SET_IDNKERR_IDNERR(sep, IDNKERR_SIGBINTR_BUSY);
2325 		goto sigb_done;
2326 	}
2327 
2328 	sbp->sb_cpuid = (uchar_t)cpuid & 0xff;
2329 	membar_stst_ldst();
2330 	sbp->sb_busy = IDNSIGB_STARTED;
2331 	/*
2332 	 * The sb_busy bit is set and the mailbox flag
2333 	 * indicates BUSY also, so we effectively have things locked.
2334 	 * So, we can drop the critical sb_mutex which we want to
2335 	 * do since it pushes us to PIL 14 while we hold it and we
2336 	 * don't want to run at PIL 14 across IDN code.
2337 	 *
2338 	 * Send interrupt to cause idn_sigbhandler_thread to wakeup.
2339 	 * We cannot do wakeup (cv_signal) directly from here since
2340 	 * we're executing from a high-level (14) interrupt.
2341 	 */
2342 	setsoftint(sbp->sb_inum);
2343 
2344 sigb_done:
2345 
2346 	if (GET_IDNKERR_ERRNO(sep) != 0) {
2347 		mbp->len = sizeof (idnsb_data_t);
2348 		mbp->cmd = cmd;
2349 		membar_stst_ldst();
2350 		mbp->flag = HOST_TO_CBS;
2351 	}
2352 
2353 	if (sigb_lock)
2354 		mutex_exit(&idn.sigbintr.sb_mutex);
2355 }
2356 
2357 static int
2358 idn_info(idnsb_info_t *sfp)
2359 {
2360 	int		count, d;
2361 	idn_domain_t	*dp;
2362 	idnsb_info_t	sinfo;
2363 	int		local_id, master_id;
2364 	procname_t	proc = "idn_info";
2365 
2366 	bzero(&sinfo, sizeof (sinfo));
2367 	sinfo.master_index = (uchar_t)-1;
2368 	sinfo.master_cpuid = (uchar_t)-1;
2369 	sinfo.local_index  = (uchar_t)-1;
2370 	sinfo.local_cpuid  = (uchar_t)-1;
2371 
2372 	IDN_GLOCK_SHARED();
2373 
2374 	sinfo.idn_state = (uchar_t)idn.state;
2375 
2376 	switch (idn.state) {
2377 	case IDNGS_OFFLINE:
2378 		sinfo.idn_active = SSISTATE_INACTIVE;
2379 		PR_PROTO("%s: idn_state (%s) = INACTIVE\n",
2380 			proc, idngs_str[idn.state]);
2381 		break;
2382 
2383 	case IDNGS_IGNORE:
2384 		PR_PROTO("%s: IGNORING IDN_INFO call...\n", proc);
2385 		IDN_GUNLOCK();
2386 		return (EIO);
2387 
2388 	default:
2389 		sinfo.idn_active = SSISTATE_ACTIVE;
2390 		PR_PROTO("%s: idn_state (%s) = ACTIVE\n",
2391 			proc, idngs_str[idn.state]);
2392 		break;
2393 	}
2394 	master_id = IDN_GET_MASTERID();
2395 	local_id = idn.localid;
2396 
2397 	/*
2398 	 * Need to drop idn.grwlock before acquiring domain locks.
2399 	 */
2400 	IDN_GUNLOCK();
2401 
2402 	IDN_SYNC_LOCK();
2403 
2404 	sinfo.awol_domset = (ushort_t)idn.domset.ds_awol;
2405 	sinfo.conn_domset = (ushort_t)(idn.domset.ds_connected &
2406 					~idn.domset.ds_trans_on);
2407 	DOMAINSET_ADD(sinfo.conn_domset, idn.localid);
2408 
2409 	count = 0;
2410 	for (d = 0; d < MAX_DOMAINS; d++) {
2411 		dp = &idn_domain[d];
2412 
2413 		if (dp->dcpu == IDN_NIL_DCPU)
2414 			continue;
2415 
2416 		IDN_DLOCK_SHARED(d);
2417 		if ((dp->dcpu == IDN_NIL_DCPU) ||
2418 				(dp->dstate == IDNDS_CLOSED)) {
2419 			IDN_DUNLOCK(d);
2420 			continue;
2421 		}
2422 
2423 		count++;
2424 		if (d == local_id) {
2425 			sinfo.local_index = (uchar_t)d;
2426 			sinfo.local_cpuid = (uchar_t)dp->dcpu;
2427 			PR_PROTO("%s: domid %d is LOCAL (cpuid = %d)\n",
2428 				proc, d, dp->dcpu);
2429 		}
2430 		if (d == master_id) {
2431 			sinfo.master_index = (uchar_t)d;
2432 			sinfo.master_cpuid = (uchar_t)dp->dcpu;
2433 			PR_PROTO("%s: domid %d is MASTER (cpuid = %d)\n",
2434 				proc, d, dp->dcpu);
2435 		}
2436 
2437 		sinfo.domain_boardset[d] = (ushort_t)dp->dhw.dh_boardset;
2438 
2439 		IDN_DUNLOCK(d);
2440 	}
2441 
2442 	IDN_SYNC_UNLOCK();
2443 
2444 	bcopy(&sinfo, sfp, sizeof (*sfp));
2445 
2446 	PR_PROTO("%s: Found %d domains within IDNnet\n", proc, count);
2447 
2448 	return (0);
2449 }
2450 
2451 /*
2452  * ----------------------------------------------
2453  * ndd param support routines.
2454  * - Borrowed from tcp.
2455  * ----------------------------------------------
2456  */
2457 static void
2458 idn_param_cleanup()
2459 {
2460 	IDN_GLOCK_EXCL();
2461 	if (!idn.strup && idn_ndlist)
2462 		nd_free(&idn_ndlist);
2463 	IDN_GUNLOCK();
2464 }
2465 
2466 /*ARGSUSED*/
2467 static int
2468 idn_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
2469 {
2470 	idnparam_t	*idnpa = (idnparam_t *)cp;
2471 
2472 	/*
2473 	 * lock grabbed before calling nd_getset.
2474 	 */
2475 	ASSERT(IDN_GLOCK_IS_HELD());
2476 
2477 	(void) mi_mpprintf(mp, "%ld", idnpa->sp_val);
2478 
2479 	return (0);
2480 }
2481 
2482 /*ARGSUSED*/
2483 static int
2484 idn_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
2485 {
2486 	char		*end;
2487 	ulong_t		new_value;
2488 	idnparam_t	*idnpa = (idnparam_t *)cp;
2489 
2490 	/*
2491 	 * lock grabbed before calling nd_getset.
2492 	 */
2493 	ASSERT(IDN_GLOCK_IS_EXCL());
2494 
2495 	new_value = (ulong_t)mi_strtol(value, &end, 10);
2496 
2497 	if ((end == value) ||
2498 		(new_value < idnpa->sp_min) ||
2499 		(new_value > idnpa->sp_max))
2500 		return (EINVAL);
2501 
2502 	if (idn.enabled == 0) {
2503 #ifdef DEBUG
2504 		cmn_err(CE_NOTE,
2505 			"IDN: 102: driver disabled "
2506 			"- check OBP environment "
2507 			"(idn-smr-size)");
2508 #else /* DEBUG */
2509 		cmn_err(CE_NOTE,
2510 			"!IDN: 102: driver disabled "
2511 			"- check OBP environment "
2512 			"(idn-smr-size)");
2513 #endif /* DEBUG */
2514 		return (EACCES);
2515 	}
2516 
2517 	idnpa->sp_val = new_value;
2518 
2519 	return (0);
2520 }
2521 
2522 static int
2523 idn_param_register(register idnparam_t *idnpa, int count)
2524 {
2525 	ASSERT(IDN_GLOCK_IS_EXCL());
2526 
2527 	for (; count > 0; count--, idnpa++) {
2528 		if (idnpa->sp_name && idnpa->sp_name[0]) {
2529 			register int	i;
2530 			ndsetf_t	set_func;
2531 			char		*p;
2532 			/*
2533 			 * Don't advertise in non-DEBUG parameters.
2534 			 */
2535 			for (i = 0; idn_param_debug_only[i]; i++) {
2536 				p = idn_param_debug_only[i];
2537 				if (strcmp(idnpa->sp_name, p) == 0)
2538 					break;
2539 			}
2540 			if (idn_param_debug_only[i])
2541 				continue;
2542 
2543 			/*
2544 			 * Do not register a "set" function for
2545 			 * read-only parameters.
2546 			 */
2547 			for (i = 0; idn_param_read_only[i]; i++) {
2548 				p = idn_param_read_only[i];
2549 				if (strcmp(idnpa->sp_name, p) == 0)
2550 					break;
2551 			}
2552 			if (idn_param_read_only[i])
2553 				set_func = NULL;
2554 			else
2555 				set_func = idn_param_set;
2556 
2557 			if (!nd_load(&idn_ndlist, idnpa->sp_name,
2558 					idn_param_get, set_func,
2559 					(caddr_t)idnpa)) {
2560 				nd_free(&idn_ndlist);
2561 				return (-1);
2562 			}
2563 		}
2564 	}
2565 	if (!nd_load(&idn_ndlist, "idn_slabpool", idn_slabpool_report,
2566 			NULL, NULL)) {
2567 		nd_free(&idn_ndlist);
2568 		return (-1);
2569 	}
2570 	if (!nd_load(&idn_ndlist, "idn_buffers", idn_buffer_report,
2571 			NULL, NULL)) {
2572 		nd_free(&idn_ndlist);
2573 		return (-1);
2574 	}
2575 	if (!nd_load(&idn_ndlist, "idn_mboxtbl", idn_mboxtbl_report,
2576 			NULL, MBXTBL_PART_REPORT)) {
2577 		nd_free(&idn_ndlist);
2578 		return (-1);
2579 	}
2580 	if (!nd_load(&idn_ndlist, "idn_mboxtbl_all", idn_mboxtbl_report,
2581 			NULL, MBXTBL_FULL_REPORT)) {
2582 		nd_free(&idn_ndlist);
2583 		return (-1);
2584 	}
2585 	if (!nd_load(&idn_ndlist, "idn_mainmbox", idn_mainmbox_report,
2586 			NULL, NULL)) {
2587 		nd_free(&idn_ndlist);
2588 		return (-1);
2589 	}
2590 	if (!nd_load(&idn_ndlist, "idn_global", idn_global_report,
2591 			NULL, NULL)) {
2592 		nd_free(&idn_ndlist);
2593 		return (-1);
2594 	}
2595 	if (!nd_load(&idn_ndlist, "idn_domain", idn_domain_report,
2596 			NULL, (caddr_t)0)) {
2597 		nd_free(&idn_ndlist);
2598 		return (-1);
2599 	}
2600 	if (!nd_load(&idn_ndlist, "idn_domain_all", idn_domain_report,
2601 			NULL, (caddr_t)1)) {
2602 		nd_free(&idn_ndlist);
2603 		return (-1);
2604 	}
2605 	if (!nd_load(&idn_ndlist, "idn_bind_net", idn_get_net_binding,
2606 			idn_set_net_binding, NULL)) {
2607 		nd_free(&idn_ndlist);
2608 		return (-1);
2609 	}
2610 
2611 	return (0);
2612 }
2613 
2614 /*ARGSUSED*/
2615 static int
2616 idn_set_net_binding(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
2617 {
2618 	char		*end, *cpup;
2619 	long		net;
2620 	processorid_t	cpuid;
2621 
2622 	/*
2623 	 * lock grabbed before calling nd_getset.
2624 	 */
2625 	ASSERT(IDN_GLOCK_IS_EXCL());
2626 
2627 	if ((cpup = strchr(value, '=')) == NULL)
2628 		return (EINVAL);
2629 
2630 	*cpup++ = '\0';
2631 
2632 	net = mi_strtol(value, &end, 10);
2633 	if ((end == value) || (net < 0) || (net >= IDN_MAX_NETS) ||
2634 				!CHAN_IN_SET(idn.chanset, net))
2635 		return (EINVAL);
2636 
2637 	cpuid = (processorid_t)mi_strtol(cpup, &end, 10);
2638 	if ((end == cpup) || ((cpuid != -1) &&
2639 				(!VALID_CPUID(cpuid) ||
2640 				!CPU_IN_SET(cpu_ready_set, cpuid))))
2641 		return (EINVAL);
2642 
2643 	idn_chanserver_bind(net, cpuid);
2644 
2645 	return (0);
2646 }
2647 
2648 /*ARGSUSED*/
2649 static int
2650 idn_get_net_binding(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
2651 {
2652 	int	c;
2653 
2654 	/*
2655 	 * lock grabbed before calling nd_getset.
2656 	 */
2657 	ASSERT(IDN_GLOCK_IS_HELD());
2658 
2659 	(void) mi_mpprintf(mp,
2660 			"IDN network interfaces/channels active = %d",
2661 			idn.nchannels);
2662 
2663 	if (idn.nchannels == 0)
2664 		return (0);
2665 
2666 	mi_mpprintf(mp, "Net    Cpu");
2667 
2668 	for (c = 0; c < IDN_MAX_NETS; c++) {
2669 		int		bc;
2670 		idn_chansvr_t	*csp;
2671 
2672 		if (!CHAN_IN_SET(idn.chanset, c))
2673 			continue;
2674 
2675 		csp = &idn.chan_servers[c];
2676 
2677 		if ((bc = csp->ch_bound_cpuid) == -1)
2678 			bc = csp->ch_bound_cpuid_pending;
2679 
2680 		if (c < 10)
2681 			mi_mpprintf(mp, " %d      %d", c, bc);
2682 		else
2683 			mi_mpprintf(mp, " %d     %d", c, bc);
2684 	}
2685 
2686 	return (0);
2687 }
2688 
2689 static int
2690 idnioc_link(idnop_t *idnop)
2691 {
2692 	int		rv;
2693 	int		pri;
2694 	idnsb_error_t	err;
2695 	procname_t	proc = "idnioc_link";
2696 
2697 	if (idnop->link.master < 0)
2698 		pri = IDNVOTE_MINPRI;
2699 	else if (idnop->link.master > 0)
2700 		pri = IDNVOTE_MAXPRI;
2701 	else
2702 		pri = IDNVOTE_DEFPRI;
2703 
2704 	PR_DRV("%s: domid = %d, cpuid = %d, pri = %d\n",
2705 		proc, idnop->link.domid, idnop->link.cpuid, pri);
2706 
2707 	rv = idn_link(idnop->link.domid, idnop->link.cpuid,
2708 			pri, idnop->link.wait, &err);
2709 
2710 	return (rv);
2711 }
2712 
2713 static int
2714 idnioc_unlink(idnop_t *idnop)
2715 {
2716 	int		d, cpuid, domid, rv;
2717 	boardset_t	idnset;
2718 	idn_fin_t	fintype;
2719 	idn_domain_t	*dp, *xdp;
2720 	idnsb_error_t	err;
2721 	procname_t	proc = "idnioc_unlink";
2722 
2723 	PR_DRV("%s: domid = %d, cpuid = %d, force = %d\n",
2724 		proc, idnop->unlink.domid, idnop->unlink.cpuid,
2725 		idnop->unlink.force);
2726 
2727 	idnset = BOARDSET_ALL;
2728 	domid = idnop->unlink.domid;
2729 	cpuid = idnop->unlink.cpuid;
2730 	dp = NULL;
2731 
2732 	if (domid == IDN_NIL_DOMID)
2733 		domid = idn.localid;
2734 
2735 	if (VALID_DOMAINID(domid)) {
2736 		dp = &idn_domain[domid];
2737 		if (VALID_CPUID(cpuid) && (dp->dcpu != IDN_NIL_DCPU) &&
2738 					!CPU_IN_SET(dp->dcpuset, cpuid)) {
2739 			dp = NULL;
2740 			PR_PROTO("%s: ERROR: invalid cpuid "
2741 				"(%d) for domain (%d) [cset = 0x%x.x%x]\n",
2742 				proc, cpuid, domid,
2743 				UPPER32_CPUMASK(dp->dcpuset),
2744 				LOWER32_CPUMASK(dp->dcpuset));
2745 		}
2746 	} else if (VALID_CPUID(cpuid)) {
2747 		for (d = 0; d < MAX_DOMAINS; d++) {
2748 			xdp = &idn_domain[d];
2749 
2750 			if (xdp->dcpu == IDN_NIL_DCPU)
2751 				continue;
2752 
2753 			if (CPU_IN_SET(xdp->dcpuset, cpuid))
2754 				break;
2755 		}
2756 		dp = (d == MAX_DOMAINS) ? NULL : xdp;
2757 	}
2758 
2759 	if ((dp == NULL) || (dp->dcpu == IDN_NIL_DCPU))
2760 		return (0);
2761 
2762 	domid = dp->domid;
2763 
2764 	switch (idnop->unlink.force) {
2765 	case SSIFORCE_OFF:
2766 		fintype = IDNFIN_NORMAL;
2767 		break;
2768 
2769 	case SSIFORCE_SOFT:
2770 		fintype = IDNFIN_FORCE_SOFT;
2771 		break;
2772 
2773 	case SSIFORCE_HARD:
2774 		fintype = IDNFIN_FORCE_HARD;
2775 		break;
2776 	default:
2777 		PR_PROTO("%s: invalid force parameter \"%d\"",
2778 			proc, idnop->unlink.force);
2779 		return (EINVAL);
2780 	}
2781 
2782 	rv = idn_unlink(domid, idnset, fintype, IDNFIN_OPT_UNLINK,
2783 			idnop->unlink.wait, &err);
2784 
2785 	return (rv);
2786 }
2787 
2788 static int
2789 idn_send_ping(idnop_t *idnop)
2790 {
2791 	int		domid = idnop->ping.domid;
2792 	int		cpuid = idnop->ping.cpuid;
2793 	int		ocpuid;
2794 	idn_domain_t	*dp;
2795 	idn_msgtype_t	mt;
2796 	procname_t	proc = "idn_send_ping";
2797 
2798 	if ((domid == IDN_NIL_DOMID) && (cpuid == IDN_NIL_DCPU)) {
2799 		cmn_err(CE_WARN,
2800 			"IDN: %s: no valid domain ID or CPU ID given",
2801 			proc);
2802 		return (EINVAL);
2803 	}
2804 	if (domid == IDN_NIL_DOMID)
2805 		domid = MAX_DOMAINS - 1;
2806 
2807 	dp = &idn_domain[domid];
2808 	IDN_DLOCK_EXCL(domid);
2809 	if ((dp->dcpu == IDN_NIL_DCPU) && (cpuid == IDN_NIL_DCPU)) {
2810 		cmn_err(CE_WARN,
2811 			"IDN: %s: no valid target CPU specified",
2812 			proc);
2813 		IDN_DUNLOCK(domid);
2814 		return (EINVAL);
2815 	}
2816 	if (cpuid == IDN_NIL_DCPU)
2817 		cpuid = dp->dcpu;
2818 
2819 	ocpuid = dp->dcpu;
2820 	dp->dcpu = cpuid;
2821 
2822 	/*
2823 	 * XXX - Need a special PING IDN command.
2824 	 */
2825 	mt.mt_mtype = IDNP_DATA | IDNP_ACK;
2826 	mt.mt_atype = 0;
2827 
2828 	(void) IDNXDC(domid, &mt, 0x100, 0x200, 0x300, 0x400);
2829 
2830 	dp->dcpu = ocpuid;
2831 	IDN_DUNLOCK(domid);
2832 
2833 	return (0);
2834 }
2835 
2836 /*
2837  * ----------------------------------------------
2838  */
2839 static void
2840 idn_dopers_init()
2841 {
2842 	int		i;
2843 	dop_waitlist_t	*dwl;
2844 
2845 	if (idn.dopers)
2846 		return;
2847 
2848 	idn.dopers = GETSTRUCT(struct dopers, 1);
2849 
2850 	bzero(idn.dopers, sizeof (struct dopers));
2851 
2852 	dwl = &idn.dopers->_dop_wcache[0];
2853 	for (i = 0; i < (IDNOP_CACHE_SIZE-1); i++)
2854 		dwl[i].dw_next = &dwl[i+1];
2855 	dwl[i].dw_next = NULL;
2856 
2857 	idn.dopers->dop_freelist = &idn.dopers->_dop_wcache[0];
2858 	idn.dopers->dop_waitcount = 0;
2859 	idn.dopers->dop_domset = 0;
2860 	idn.dopers->dop_waitlist = NULL;
2861 
2862 	cv_init(&idn.dopers->dop_cv, NULL, CV_DEFAULT, NULL);
2863 	mutex_init(&idn.dopers->dop_mutex, NULL, MUTEX_DEFAULT, NULL);
2864 }
2865 
2866 static void
2867 idn_dopers_deinit()
2868 {
2869 	dop_waitlist_t	*dwl, *next_dwl;
2870 
2871 
2872 	if (idn.dopers == NULL)
2873 		return;
2874 
2875 	for (dwl = idn.dopers->dop_waitlist; dwl; dwl = next_dwl) {
2876 		next_dwl = dwl->dw_next;
2877 		if (!IDNOP_IN_CACHE(dwl))
2878 			FREESTRUCT(dwl, dop_waitlist_t, 1);
2879 	}
2880 
2881 	cv_destroy(&idn.dopers->dop_cv);
2882 	mutex_destroy(&idn.dopers->dop_mutex);
2883 
2884 	FREESTRUCT(idn.dopers, struct dopers, 1);
2885 	idn.dopers = NULL;
2886 }
2887 
2888 /*
2889  * Reset the dop_errset field in preparation for an
2890  * IDN operation attempt.  This is only called from
2891  * idn_link() and idn_unlink().
2892  */
2893 void *
2894 idn_init_op(idn_opflag_t opflag, domainset_t domset, idnsb_error_t *sep)
2895 {
2896 	dop_waitlist_t	*dwl;
2897 	/*
2898 	 * Clear any outstanding error ops in preparation
2899 	 * for an IDN (link/unlink) operation.
2900 	 */
2901 	mutex_enter(&idn.dopers->dop_mutex);
2902 	if ((dwl = idn.dopers->dop_freelist) == NULL) {
2903 		dwl = GETSTRUCT(dop_waitlist_t, 1);
2904 	} else {
2905 		idn.dopers->dop_freelist = dwl->dw_next;
2906 		bzero(dwl, sizeof (*dwl));
2907 	}
2908 	dwl->dw_op = opflag;
2909 	dwl->dw_reqset = domset;
2910 	dwl->dw_idnerr = sep;
2911 	dwl->dw_next = idn.dopers->dop_waitlist;
2912 
2913 	idn.dopers->dop_waitlist = dwl;
2914 	idn.dopers->dop_waitcount++;
2915 	idn.dopers->dop_domset |= domset;
2916 	mutex_exit(&idn.dopers->dop_mutex);
2917 
2918 	return (dwl);
2919 }
2920 
2921 /*
2922  * Anybody waiting on a opflag operation for any one
2923  * of the domains in domset, needs to be updated to
2924  * additionally wait for new domains in domset.
2925  * This is used, for example, when needing to connect
2926  * to more domains than known at the time of the
2927  * original request.
2928  */
2929 void
2930 idn_add_op(idn_opflag_t opflag, domainset_t domset)
2931 {
2932 	dop_waitlist_t	*dwl;
2933 
2934 	mutex_enter(&idn.dopers->dop_mutex);
2935 	if ((idn.dopers->dop_waitcount == 0) ||
2936 		((idn.dopers->dop_domset & domset) == 0)) {
2937 		mutex_exit(&idn.dopers->dop_mutex);
2938 		return;
2939 	}
2940 	for (dwl = idn.dopers->dop_waitlist; dwl; dwl = dwl->dw_next)
2941 		if ((dwl->dw_op == opflag) && (dwl->dw_reqset & domset))
2942 			dwl->dw_reqset |= domset;
2943 	mutex_exit(&idn.dopers->dop_mutex);
2944 }
2945 
2946 /*
2947  * Mechanism to wakeup any potential users which may be waiting
2948  * for a link/unlink operation to complete.  If an error occurred
2949  * don't update dop_errset unless there was no previous error.
2950  */
2951 void
2952 idn_update_op(idn_opflag_t opflag, domainset_t domset, idnsb_error_t *sep)
2953 {
2954 	int		do_wakeup = 0;
2955 	dop_waitlist_t	*dw;
2956 	procname_t	proc = "idn_update_op";
2957 
2958 	mutex_enter(&idn.dopers->dop_mutex);
2959 	/*
2960 	 * If there are no waiters, or nobody is waiting for
2961 	 * the particular domainset in question, then
2962 	 * just bail.
2963 	 */
2964 	if ((idn.dopers->dop_waitcount == 0) ||
2965 		((idn.dopers->dop_domset & domset) == 0)) {
2966 		mutex_exit(&idn.dopers->dop_mutex);
2967 		PR_PROTO("%s: NO waiters exist (domset=0x%x)\n",
2968 			proc, domset);
2969 		return;
2970 	}
2971 
2972 	for (dw = idn.dopers->dop_waitlist; dw; dw = dw->dw_next) {
2973 		int		d;
2974 		domainset_t	dset, rset;
2975 
2976 		if ((dset = dw->dw_reqset & domset) == 0)
2977 			continue;
2978 
2979 		if (opflag == IDNOP_ERROR) {
2980 			dw->dw_errset |= dset;
2981 			if (sep) {
2982 				for (d = 0; d < MAX_DOMAINS; d++) {
2983 					if (!DOMAIN_IN_SET(dset, d))
2984 						continue;
2985 
2986 					dw->dw_errors[d] =
2987 						(short)GET_IDNKERR_ERRNO(sep);
2988 				}
2989 				bcopy(sep, dw->dw_idnerr, sizeof (*sep));
2990 			}
2991 		} else if (opflag == dw->dw_op) {
2992 			dw->dw_domset |= dset;
2993 		}
2994 
2995 		/*
2996 		 * Check if all the domains are spoken for that
2997 		 * a particular waiter may have been waiting for.
2998 		 * If there's at least one, we'll need to broadcast.
2999 		 */
3000 		rset = (dw->dw_errset | dw->dw_domset) & dw->dw_reqset;
3001 		if (rset == dw->dw_reqset)
3002 			do_wakeup++;
3003 	}
3004 
3005 	PR_PROTO("%s: found %d waiters ready for wakeup\n", proc, do_wakeup);
3006 
3007 	if (do_wakeup > 0)
3008 		cv_broadcast(&idn.dopers->dop_cv);
3009 
3010 	mutex_exit(&idn.dopers->dop_mutex);
3011 }
3012 
3013 void
3014 idn_deinit_op(void *cookie)
3015 {
3016 	domainset_t	domset;
3017 	dop_waitlist_t	*hw, *tw;
3018 	dop_waitlist_t	*dwl = (dop_waitlist_t *)cookie;
3019 
3020 	mutex_enter(&idn.dopers->dop_mutex);
3021 
3022 	ASSERT(idn.dopers->dop_waitlist);
3023 
3024 	if (dwl == idn.dopers->dop_waitlist) {
3025 		idn.dopers->dop_waitlist = dwl->dw_next;
3026 		if (IDNOP_IN_CACHE(dwl)) {
3027 			dwl->dw_next = idn.dopers->dop_freelist;
3028 			idn.dopers->dop_freelist = dwl;
3029 		} else {
3030 			FREESTRUCT(dwl, dop_waitlist_t, 1);
3031 		}
3032 	} else {
3033 		for (tw = idn.dopers->dop_waitlist, hw = tw->dw_next;
3034 				hw;
3035 				tw = hw, hw = hw->dw_next) {
3036 			if (dwl == hw)
3037 				break;
3038 		}
3039 		ASSERT(hw);
3040 
3041 		tw->dw_next = hw->dw_next;
3042 	}
3043 
3044 	/*
3045 	 * Recompute domainset for which waiters might be waiting.
3046 	 * It's possible there may be other waiters waiting for
3047 	 * the same domainset that the current waiter that's leaving
3048 	 * may have been waiting for, so we can't simply delete
3049 	 * the leaving waiter's domainset from dop_domset.
3050 	 */
3051 	for (hw = idn.dopers->dop_waitlist, domset = 0; hw; hw = hw->dw_next)
3052 		domset |= hw->dw_reqset;
3053 
3054 	idn.dopers->dop_waitcount--;
3055 	idn.dopers->dop_domset = domset;
3056 
3057 	mutex_exit(&idn.dopers->dop_mutex);
3058 }
3059 
3060 /*
3061  * Wait until the specified operation succeeds or fails with
3062  * respect to the given domains.  Note the function terminates
3063  * if at least one error occurs.
3064  * This process is necessary since link/unlink operations occur
3065  * asynchronously and we need some way of waiting to find out
3066  * if it indeed completed.
3067  * Timeout value is received indirectly from the SSP and
3068  * represents seconds.
3069  */
3070 int
3071 idn_wait_op(void *cookie, domainset_t *domsetp, int wait_timeout)
3072 {
3073 	int	d, rv, err = 0;
3074 	dop_waitlist_t	*dwl;
3075 
3076 
3077 	dwl = (dop_waitlist_t *)cookie;
3078 
3079 	ASSERT(wait_timeout > 0);
3080 	ASSERT((dwl->dw_op == IDNOP_CONNECTED) ||
3081 		(dwl->dw_op == IDNOP_DISCONNECTED));
3082 
3083 	mutex_enter(&idn.dopers->dop_mutex);
3084 
3085 	while (((dwl->dw_domset | dwl->dw_errset) != dwl->dw_reqset) && !err) {
3086 		rv = cv_timedwait_sig(&idn.dopers->dop_cv,
3087 					&idn.dopers->dop_mutex,
3088 					lbolt + (wait_timeout * hz));
3089 
3090 		if ((dwl->dw_domset | dwl->dw_errset) == dwl->dw_reqset)
3091 			break;
3092 
3093 		switch (rv) {
3094 		case -1:
3095 			/*
3096 			 * timed out
3097 			 */
3098 			cmn_err(CE_WARN,
3099 				"!IDN: 129: %s operation timed out",
3100 				(dwl->dw_op == IDNOP_CONNECTED) ? "LINK" :
3101 				(dwl->dw_op == IDNOP_DISCONNECTED) ? "UNLINK" :
3102 				"UNKNOWN");
3103 			/*FALLTHROUGH*/
3104 		case 0:
3105 			/*
3106 			 * signal, e.g. kill(2)
3107 			 */
3108 			err = 1;
3109 			break;
3110 
3111 		default:
3112 			break;
3113 		}
3114 	}
3115 
3116 	if (dwl->dw_domset == dwl->dw_reqset) {
3117 		rv = 0;
3118 	} else {
3119 		/*
3120 		 * Op failed for some domains or we were awakened.
3121 		 */
3122 		for (d = rv = 0; (d < MAX_DOMAINS) && !rv; d++)
3123 			rv = dwl->dw_errors[d];
3124 	}
3125 	*domsetp = dwl->dw_domset;
3126 
3127 	mutex_exit(&idn.dopers->dop_mutex);
3128 
3129 	idn_deinit_op(cookie);
3130 
3131 	return (rv);
3132 }
3133 
3134 /*
3135  * --------------------------------------------------
3136  * Return any valid (& ready) cpuid for the given board based on
3137  * the given cpuset.
3138  * --------------------------------------------------
3139  */
3140 int
3141 board_to_ready_cpu(int board, cpuset_t cpuset)
3142 {
3143 	int	base_cpuid;
3144 	int	ncpu_board = MAX_CPU_PER_BRD;
3145 
3146 	board *= ncpu_board;
3147 	for (base_cpuid = board;
3148 			base_cpuid < (board + ncpu_board);
3149 			base_cpuid++)
3150 		if (CPU_IN_SET(cpuset, base_cpuid))
3151 			return (base_cpuid);
3152 
3153 	return (-1);
3154 }
3155 
3156 void
3157 idn_domain_resetentry(idn_domain_t *dp)
3158 {
3159 	register int		i;
3160 	procname_t	proc = "idn_domain_resetentry";
3161 
3162 	ASSERT(dp);
3163 	ASSERT(dp->dstate == IDNDS_CLOSED);
3164 	ASSERT(IDN_DLOCK_IS_EXCL(dp->domid));
3165 	ASSERT(IDN_GLOCK_IS_EXCL());
3166 
3167 	ASSERT(dp->domid == (dp - &idn_domain[0]));
3168 
3169 	IDN_FSTATE_TRANSITION(dp, IDNFIN_OFF);
3170 	dp->dname[0]	= '\0';
3171 	dp->dnetid	= (ushort_t)-1;
3172 	dp->dmtu	= 0;
3173 	dp->dbufsize	= 0;
3174 	dp->dslabsize	= 0;
3175 	dp->dnwrsize	= 0;
3176 	dp->dncpus	= 0;
3177 	dp->dcpuindex   = 0;
3178 	CPUSET_ZERO(dp->dcpuset);
3179 	dp->dcpu	= dp->dcpu_last = dp->dcpu_save = IDN_NIL_DCPU;
3180 	dp->dvote.ticket = 0;
3181 	dp->dslab	= NULL;
3182 	dp->dslab_state = DSLAB_STATE_UNKNOWN;
3183 	dp->dnslabs	= 0;
3184 	dp->dio		= 0;
3185 	dp->dioerr	= 0;
3186 	lock_clear(&dp->diowanted);
3187 	bzero(&dp->dhw, sizeof (dp->dhw));
3188 	dp->dxp		= NULL;
3189 	IDN_XSTATE_TRANSITION(dp, IDNXS_NIL);
3190 	dp->dsync.s_cmd = IDNSYNC_NIL;
3191 	dp->dfin_sync   = IDNFIN_SYNC_OFF;
3192 	IDN_RESET_COOKIES(dp->domid);
3193 	dp->dcookie_err = 0;
3194 	bzero(&dp->dawol, sizeof (dp->dawol));
3195 	dp->dtmp = -1;
3196 
3197 	if (dp->dtimerq.tq_queue != NULL) {
3198 		PR_PROTO("%s: WARNING: MSG timerq not empty (count = %d)\n",
3199 			proc, dp->dtimerq.tq_count);
3200 		IDN_MSGTIMER_STOP(dp->domid, 0, 0);
3201 	}
3202 
3203 	for (i = 0; i < NCPU; i++)
3204 		dp->dcpumap[i] = (uchar_t)-1;
3205 }
3206 
3207 int
3208 idn_open_domain(int domid, int cpuid, uint_t ticket)
3209 {
3210 	int		c, new_cpuid;
3211 	idn_domain_t	*dp, *ldp;
3212 	procname_t	proc = "idn_open_domain";
3213 
3214 	ASSERT(IDN_SYNC_IS_LOCKED());
3215 	ASSERT(IDN_DLOCK_IS_EXCL(domid));
3216 
3217 	if (!VALID_DOMAINID(domid)) {
3218 		PR_PROTO("%s: INVALID domainid (%d) "
3219 			"[cpuid = %d, ticket = 0x%x]\n",
3220 			proc, domid, cpuid, ticket);
3221 		return (-1);
3222 	}
3223 
3224 	dp = &idn_domain[domid];
3225 	ldp = &idn_domain[idn.localid];
3226 
3227 	if (dp->dcpu >= 0) {
3228 		PR_PROTO("%s:%d: domain already OPEN (state = %s)\n",
3229 			proc, domid, idnds_str[dp->dstate]);
3230 		return (1);
3231 	}
3232 
3233 	if (DOMAIN_IN_SET(idn.domset.ds_relink, domid)) {
3234 		if (dp->dcpu_save == IDN_NIL_DCPU)
3235 			new_cpuid = cpuid;
3236 		else
3237 			new_cpuid = dp->dcpu_save;
3238 	} else {
3239 		new_cpuid = cpuid;
3240 	}
3241 
3242 	if (new_cpuid == IDN_NIL_DCPU) {
3243 		PR_PROTO("%s:%d: WARNING: invalid cpuid (%d) specified\n",
3244 			proc, domid, new_cpuid);
3245 		return (-1);
3246 	}
3247 
3248 	IDN_GLOCK_EXCL();
3249 
3250 	idn_domain_resetentry(dp);
3251 
3252 	PR_STATE("%s:%d: requested cpuid %d, assigning cpuid %d\n",
3253 		proc, domid, cpuid, new_cpuid);
3254 
3255 	idn_assign_cookie(domid);
3256 
3257 	dp->dcpu = dp->dcpu_save = new_cpuid;
3258 	dp->dvote.ticket = ticket;
3259 	CPUSET_ADD(dp->dcpuset, new_cpuid);
3260 	dp->dncpus = 1;
3261 	for (c = 0; c < NCPU; c++)
3262 		dp->dcpumap[c] = (uchar_t)new_cpuid;
3263 	dp->dhw.dh_nboards = 1;
3264 	dp->dhw.dh_boardset = BOARDSET(CPUID_TO_BOARDID(new_cpuid));
3265 
3266 	if (domid != idn.localid)
3267 		IDN_DLOCK_EXCL(idn.localid);
3268 
3269 	if (idn.ndomains == 1) {
3270 		struct hwconfig	local_hw;
3271 
3272 		/*
3273 		 * We're attempting to connect to our first domain.
3274 		 * Recheck our local hardware configuration before
3275 		 * we go any further in case it changed due to a DR,
3276 		 * and update any structs dependent on this.
3277 		 * ASSUMPTION:
3278 		 *	IDN is unlinked before performing any DRs.
3279 		 */
3280 		PR_PROTO("%s: RECHECKING local HW config.\n", proc);
3281 		if (get_hw_config(&local_hw)) {
3282 			dp->dcpu = IDN_NIL_DCPU;
3283 			cmn_err(CE_WARN,
3284 				"IDN: 118: hardware config not appropriate");
3285 			if (domid != idn.localid)
3286 				IDN_DUNLOCK(idn.localid);
3287 			IDN_GUNLOCK();
3288 			return (-1);
3289 		}
3290 		(void) update_local_hw_config(ldp, &local_hw);
3291 	}
3292 
3293 	idn.ndomains++;
3294 
3295 	if (domid != idn.localid)
3296 		IDN_DUNLOCK(idn.localid);
3297 	IDN_GUNLOCK();
3298 
3299 	IDN_MBOX_LOCK(domid);
3300 	dp->dmbox.m_tbl = NULL;
3301 
3302 	if (domid != idn.localid) {
3303 		dp->dmbox.m_send = idn_mainmbox_init(domid,
3304 							IDNMMBOX_TYPE_SEND);
3305 		dp->dmbox.m_recv = idn_mainmbox_init(domid,
3306 							IDNMMBOX_TYPE_RECV);
3307 	} else {
3308 		/*
3309 		 * The local domain does not need send/recv
3310 		 * mailboxes in its idn_domain[] entry.
3311 		 */
3312 		dp->dmbox.m_send = NULL;
3313 		dp->dmbox.m_recv = NULL;
3314 	}
3315 	IDN_MBOX_UNLOCK(domid);
3316 
3317 	PR_PROTO("%s:%d: new domain (cpu = %d, vote = 0x%x)\n",
3318 		proc, domid, dp->dcpu, dp->dvote.ticket);
3319 
3320 	return (0);
3321 }
3322 
3323 /*
3324  * The local domain never "closes" itself unless the driver
3325  * is doing a idndetach.  It will be reopened during idnattach
3326  * when idn_domains_init is called.
3327  */
3328 void
3329 idn_close_domain(int domid)
3330 {
3331 	uint_t		token;
3332 	idn_domain_t	*dp;
3333 	procname_t	proc = "idn_close_domain";
3334 
3335 	ASSERT(IDN_SYNC_IS_LOCKED());
3336 	ASSERT(IDN_DLOCK_IS_EXCL(domid));
3337 
3338 	dp = &idn_domain[domid];
3339 
3340 	ASSERT(dp->dstate == IDNDS_CLOSED);
3341 
3342 	if (dp->dcpu == IDN_NIL_DCPU) {
3343 		PR_PROTO("%s:%d: DOMAIN ALREADY CLOSED!\n",
3344 			proc, domid);
3345 		return;
3346 	}
3347 
3348 	token = IDN_RETRY_TOKEN(domid, IDN_RETRY_TYPEALL);
3349 
3350 	(void) idn_retry_terminate(token);
3351 
3352 	DOMAINSET_DEL(idn.domset.ds_trans_on, domid);
3353 	DOMAINSET_DEL(idn.domset.ds_ready_on, domid);
3354 	DOMAINSET_DEL(idn.domset.ds_connected, domid);
3355 	DOMAINSET_DEL(idn.domset.ds_trans_off, domid);
3356 	DOMAINSET_DEL(idn.domset.ds_ready_off, domid);
3357 	DOMAINSET_DEL(idn.domset.ds_hwlinked, domid);
3358 	DOMAINSET_DEL(idn.domset.ds_flush, domid);
3359 
3360 	idn_sync_exit(domid, IDNSYNC_CONNECT);
3361 	idn_sync_exit(domid, IDNSYNC_DISCONNECT);
3362 
3363 	IDN_GLOCK_EXCL();
3364 
3365 	if (DOMAIN_IN_SET(idn.domset.ds_awol, domid))
3366 		idn_clear_awol(domid);
3367 
3368 	idn.ndomains--;
3369 
3370 	IDN_GUNLOCK();
3371 
3372 	IDN_MBOX_LOCK(domid);
3373 	dp->dmbox.m_tbl = NULL;
3374 
3375 	if (dp->dmbox.m_send)  {
3376 		idn_mainmbox_deinit(domid, dp->dmbox.m_send);
3377 		dp->dmbox.m_send = NULL;
3378 	}
3379 
3380 	if (dp->dmbox.m_recv) {
3381 		idn_mainmbox_deinit(domid, dp->dmbox.m_recv);
3382 		dp->dmbox.m_recv = NULL;
3383 	}
3384 
3385 	IDN_MBOX_UNLOCK(domid);
3386 
3387 	cmn_err(CE_NOTE,
3388 		"!IDN: 142: link (domain %d, CPU %d) disconnected",
3389 		dp->domid, dp->dcpu);
3390 
3391 	dp->dcpu = IDN_NIL_DCPU;	/* ultimate demise */
3392 
3393 	IDN_RESET_COOKIES(domid);
3394 
3395 	ASSERT(dp->dio <= 0);
3396 	ASSERT(dp->dioerr == 0);
3397 	ASSERT(dp->dslab == NULL);
3398 	ASSERT(dp->dnslabs == 0);
3399 
3400 	IDN_GKSTAT_GLOBAL_EVENT(gk_unlinks, gk_unlink_last);
3401 }
3402 
3403 
3404 /*
3405  * -----------------------------------------------------------------------
3406  */
3407 static void
3408 idn_domains_init(struct hwconfig *local_hw)
3409 {
3410 	register int		i, d;
3411 	idn_domain_t		*ldp;
3412 	uchar_t			*cpumap;
3413 
3414 	ASSERT(local_hw != NULL);
3415 
3416 	cpumap = GETSTRUCT(uchar_t, NCPU * MAX_DOMAINS);
3417 
3418 	for (d = 0; d < MAX_DOMAINS; d++) {
3419 		register idn_domain_t	*dp;
3420 
3421 		dp = &idn_domain[d];
3422 
3423 		dp->domid = d;
3424 
3425 		rw_init(&dp->drwlock, NULL, RW_DEFAULT, NULL);
3426 
3427 		IDN_TIMERQ_INIT(&dp->dtimerq);
3428 
3429 		dp->dstate = IDNDS_CLOSED;
3430 
3431 		mutex_init(&dp->dmbox.m_mutex, NULL, MUTEX_DEFAULT, NULL);
3432 
3433 		dp->dcpumap = cpumap;
3434 
3435 		rw_init(&dp->dslab_rwlock, NULL, RW_DEFAULT, NULL);
3436 
3437 		IDN_DLOCK_EXCL(d);
3438 		IDN_GLOCK_EXCL();
3439 
3440 		idn_domain_resetentry(dp);
3441 
3442 		IDN_GUNLOCK();
3443 
3444 		IDNSB_DOMAIN_UPDATE(dp);
3445 
3446 		IDN_DUNLOCK(d);
3447 
3448 		cpumap += NCPU;
3449 	}
3450 
3451 	IDN_SYNC_LOCK();
3452 
3453 	/*
3454 	 * Update local domain information.
3455 	 */
3456 	ASSERT(idn.smr.locpfn);
3457 	ASSERT(local_hw->dh_nboards && local_hw->dh_boardset);
3458 
3459 	idn.ndomains = 0;	/* note that open_domain will get us to 1 */
3460 
3461 	IDN_DLOCK_EXCL(idn.localid);
3462 	d = idn_open_domain(idn.localid, (int)CPU->cpu_id, 0);
3463 	ASSERT(d == 0);
3464 	IDN_GLOCK_EXCL();
3465 	IDN_SET_MASTERID(IDN_NIL_DOMID);
3466 	IDN_SET_NEW_MASTERID(IDN_NIL_DOMID);
3467 
3468 	ldp = &idn_domain[idn.localid];
3469 
3470 	strncpy(ldp->dname, utsname.nodename, MAXDNAME - 1);
3471 	ldp->dname[MAXDNAME-1] = '\0';
3472 	bcopy(local_hw, &ldp->dhw, sizeof (ldp->dhw));
3473 	ASSERT(idn.ndomains == 1);
3474 	ASSERT((ldp->dhw.dh_nboards > 0) &&
3475 		(ldp->dhw.dh_nboards <= MAX_BOARDS));
3476 	ldp->dnetid	= IDN_DOMID2NETID(ldp->domid);
3477 	ldp->dmtu	= IDN_MTU;
3478 	ldp->dbufsize	= IDN_SMR_BUFSIZE;
3479 	ldp->dslabsize	= (short)IDN_SLAB_BUFCOUNT;
3480 	ldp->dnwrsize	= (short)IDN_NWR_SIZE;
3481 	ldp->dcpuset	= cpu_ready_set;
3482 	ldp->dncpus	= (short)ncpus;
3483 	ldp->dvote.ticket	= IDNVOTE_INITIAL_TICKET;
3484 	ldp->dvote.v.master	= 0;
3485 	ldp->dvote.v.nmembrds	= ldp->dhw.dh_nmcadr - 1;
3486 	ldp->dvote.v.ncpus	= (int)ldp->dncpus - 1;
3487 	ldp->dvote.v.board	= CPUID_TO_BOARDID(ldp->dcpu);
3488 	i = -1;
3489 	for (d = 0; d < NCPU; d++) {
3490 		BUMP_INDEX(ldp->dcpuset, i);
3491 		ldp->dcpumap[d] = (uchar_t)i;
3492 	}
3493 
3494 	CPUSET_ZERO(idn.dc_cpuset);
3495 	CPUSET_OR(idn.dc_cpuset, ldp->dcpuset);
3496 	idn.dc_boardset = ldp->dhw.dh_boardset;
3497 
3498 	/*
3499 	 * Setting the state for ourselves is only relevant
3500 	 * for loopback performance testing.  Anyway, it
3501 	 * makes sense that we always have an established
3502 	 * connection with ourself regardless of IDN :-o
3503 	 */
3504 	IDN_DSTATE_TRANSITION(ldp, IDNDS_CONNECTED);
3505 
3506 	IDN_GUNLOCK();
3507 	IDN_DUNLOCK(idn.localid);
3508 	IDN_SYNC_UNLOCK();
3509 }
3510 
3511 static void
3512 idn_domains_deinit()
3513 {
3514 	register int	d;
3515 
3516 	IDN_SYNC_LOCK();
3517 	IDN_DLOCK_EXCL(idn.localid);
3518 	IDN_DSTATE_TRANSITION(&idn_domain[idn.localid], IDNDS_CLOSED);
3519 	idn_close_domain(idn.localid);
3520 	IDN_DUNLOCK(idn.localid);
3521 	IDN_SYNC_UNLOCK();
3522 	idn.localid = IDN_NIL_DOMID;
3523 
3524 	FREESTRUCT(idn_domain[0].dcpumap, uchar_t, NCPU * MAX_DOMAINS);
3525 
3526 	for (d = 0; d < MAX_DOMAINS; d++) {
3527 		idn_domain_t	*dp;
3528 
3529 		dp = &idn_domain[d];
3530 
3531 		rw_destroy(&dp->dslab_rwlock);
3532 		mutex_destroy(&dp->dmbox.m_mutex);
3533 		rw_destroy(&dp->drwlock);
3534 		IDN_TIMERQ_DEINIT(&dp->dtimerq);
3535 		dp->dcpumap = NULL;
3536 	}
3537 }
3538 
3539 /*
3540  * -----------------------------------------------------------------------
3541  */
3542 static void
3543 idn_retrytask_init()
3544 {
3545 	ASSERT(idn.retryqueue.rq_cache == NULL);
3546 
3547 	mutex_init(&idn.retryqueue.rq_mutex, NULL, MUTEX_DEFAULT, NULL);
3548 	idn.retryqueue.rq_cache = kmem_cache_create("idn_retryjob_cache",
3549 						sizeof (idn_retry_job_t),
3550 						0, NULL, NULL, NULL,
3551 						NULL, NULL, 0);
3552 }
3553 
3554 static void
3555 idn_retrytask_deinit()
3556 {
3557 	if (idn.retryqueue.rq_cache == NULL)
3558 		return;
3559 
3560 	kmem_cache_destroy(idn.retryqueue.rq_cache);
3561 	mutex_destroy(&idn.retryqueue.rq_mutex);
3562 
3563 	bzero(&idn.retryqueue, sizeof (idn.retryqueue));
3564 }
3565 
3566 /*
3567  * -----------------------------------------------------------------------
3568  */
3569 static void
3570 idn_timercache_init()
3571 {
3572 	ASSERT(idn.timer_cache == NULL);
3573 
3574 	idn.timer_cache = kmem_cache_create("idn_timer_cache",
3575 					sizeof (idn_timer_t),
3576 					0, NULL, NULL, NULL,
3577 					NULL, NULL, 0);
3578 }
3579 
3580 static void
3581 idn_timercache_deinit()
3582 {
3583 	if (idn.timer_cache == NULL)
3584 		return;
3585 
3586 	kmem_cache_destroy(idn.timer_cache);
3587 	idn.timer_cache = NULL;
3588 }
3589 
3590 idn_timer_t *
3591 idn_timer_alloc()
3592 {
3593 	idn_timer_t	*tp;
3594 
3595 	tp = kmem_cache_alloc(idn.timer_cache, KM_SLEEP);
3596 	bzero(tp, sizeof (*tp));
3597 	tp->t_forw = tp->t_back = tp;
3598 
3599 	return (tp);
3600 }
3601 
3602 void
3603 idn_timer_free(idn_timer_t *tp)
3604 {
3605 	if (tp == NULL)
3606 		return;
3607 	kmem_cache_free(idn.timer_cache, tp);
3608 }
3609 
3610 void
3611 idn_timerq_init(idn_timerq_t *tq)
3612 {
3613 	mutex_init(&tq->tq_mutex, NULL, MUTEX_DEFAULT, NULL);
3614 	tq->tq_count = 0;
3615 	tq->tq_queue = NULL;
3616 }
3617 
3618 void
3619 idn_timerq_deinit(idn_timerq_t *tq)
3620 {
3621 	ASSERT(tq->tq_queue == NULL);
3622 	mutex_destroy(&tq->tq_mutex);
3623 }
3624 
3625 /*
3626  * Dequeue all the timers of the given subtype from the
3627  * given timerQ.  If subtype is 0, then dequeue all the
3628  * timers.
3629  */
3630 idn_timer_t *
3631 idn_timer_get(idn_timerq_t *tq, int type, ushort_t tcookie)
3632 {
3633 	register idn_timer_t	*tp, *tphead;
3634 
3635 	ASSERT(IDN_TIMERQ_IS_LOCKED(tq));
3636 
3637 	if ((tp = tq->tq_queue) == NULL)
3638 		return (NULL);
3639 
3640 	if (!type) {
3641 		tq->tq_queue = NULL;
3642 		tq->tq_count = 0;
3643 		tphead = tp;
3644 	} else {
3645 		int		count;
3646 		idn_timer_t	*tpnext;
3647 
3648 		tphead = NULL;
3649 		count = tq->tq_count;
3650 		do {
3651 			tpnext = tp->t_forw;
3652 			if ((tp->t_type == type) &&
3653 				(!tcookie || (tp->t_cookie == tcookie))) {
3654 				tp->t_forw->t_back = tp->t_back;
3655 				tp->t_back->t_forw = tp->t_forw;
3656 				if (tphead == NULL) {
3657 					tp->t_forw = tp->t_back = tp;
3658 				} else {
3659 					tp->t_forw = tphead;
3660 					tp->t_back = tphead->t_back;
3661 					tp->t_back->t_forw = tp;
3662 					tphead->t_back = tp;
3663 				}
3664 				tphead = tp;
3665 				if (--(tq->tq_count) == 0)
3666 					tq->tq_queue = NULL;
3667 				else if (tq->tq_queue == tp)
3668 					tq->tq_queue = tpnext;
3669 			}
3670 			tp = tpnext;
3671 		} while (--count > 0);
3672 	}
3673 
3674 	if (tphead) {
3675 		tphead->t_back->t_forw = NULL;
3676 
3677 		for (tp = tphead; tp; tp = tp->t_forw)
3678 			tp->t_onq = 0;
3679 	}
3680 
3681 	return (tphead);
3682 }
3683 
3684 ushort_t
3685 idn_timer_start(idn_timerq_t *tq, idn_timer_t *tp, clock_t tval)
3686 {
3687 	idn_timer_t	*otp;
3688 	ushort_t	tcookie;
3689 	procname_t	proc = "idn_timer_start";
3690 	STRING(str);
3691 
3692 	ASSERT(tq && tp && (tval > 0));
3693 	ASSERT((tp->t_forw == tp) && (tp->t_back == tp));
3694 	ASSERT(tp->t_type != 0);
3695 
3696 	IDN_TIMERQ_LOCK(tq);
3697 	/*
3698 	 * Assign a unique non-zero 8-bit cookie to this timer
3699 	 * if the caller hasn't already preassigned one.
3700 	 */
3701 	while ((tcookie = tp->t_cookie) == 0) {
3702 		tp->t_cookie = (tp->t_type & 0xf) |
3703 					((++tq->tq_cookie & 0xf) << 4);
3704 		/*
3705 		 * Calculated cookie must never conflict
3706 		 * with the public timer cookie.
3707 		 */
3708 		ASSERT(tp->t_cookie != IDN_TIMER_PUBLIC_COOKIE);
3709 	}
3710 
3711 	/*
3712 	 * First have to remove old timers of the
3713 	 * same type and cookie, and get rid of them.
3714 	 */
3715 	otp = idn_timer_get(tq, tp->t_type, tcookie);
3716 
3717 	tq->tq_count++;
3718 
3719 	if (tq->tq_queue == NULL) {
3720 		tq->tq_queue = tp;
3721 		ASSERT((tp->t_forw == tp) && (tp->t_back == tp));
3722 	} else {
3723 		/*
3724 		 * Put me at the end of the list.
3725 		 */
3726 		tp->t_forw = tq->tq_queue;
3727 		tp->t_back = tq->tq_queue->t_back;
3728 		tp->t_back->t_forw = tp;
3729 		tp->t_forw->t_back = tp;
3730 	}
3731 
3732 	tp->t_onq = 1;
3733 	tp->t_q = tq;
3734 	tp->t_id = timeout(idn_timer_expired, (caddr_t)tp, tval);
3735 
3736 
3737 	INUM2STR(tp->t_type, str);
3738 	PR_TIMER("%s: started %s timer (domain = %d, cookie = 0x%x)\n",
3739 		proc, str, tp->t_domid, tcookie);
3740 
3741 	IDN_TIMERQ_UNLOCK(tq);
3742 
3743 	if (otp)
3744 		(void) idn_timer_stopall(otp);
3745 
3746 	return (tcookie);
3747 }
3748 
3749 /*
3750  * Stop all timers of the given subtype.
3751  * If subtype is 0, then stop all timers
3752  * in this timerQ.
3753  */
3754 void
3755 idn_timer_stop(idn_timerq_t *tq, int type, ushort_t tcookie)
3756 {
3757 	idn_timer_t	*tphead;
3758 	procname_t	proc = "idn_timer_stop";
3759 	STRING(str);
3760 
3761 	ASSERT(tq);
3762 
3763 	INUM2STR(type, str);
3764 
3765 	IDN_TIMERQ_LOCK(tq);
3766 
3767 	if (tq->tq_count == 0) {
3768 		PR_TIMER("%s: found no %s timers (count=0)\n", proc, str);
3769 		IDN_TIMERQ_UNLOCK(tq);
3770 		return;
3771 	}
3772 	tphead = idn_timer_get(tq, type, tcookie);
3773 #ifdef DEBUG
3774 	if (tphead == NULL)
3775 		PR_TIMER("%s: found no %s (cookie = 0x%x) "
3776 			"timers (count=%d)!!\n",
3777 			proc, str, tcookie, tq->tq_count);
3778 #endif /* DEBUG */
3779 	IDN_TIMERQ_UNLOCK(tq);
3780 
3781 	if (tphead)
3782 		(void) idn_timer_stopall(tphead);
3783 }
3784 
3785 int
3786 idn_timer_stopall(idn_timer_t *tp)
3787 {
3788 	int		count = 0;
3789 	int		nonactive;
3790 	uint_t		type;
3791 	idn_timer_t	*ntp;
3792 	procname_t	proc = "idn_timer_stopall";
3793 	STRING(str);
3794 
3795 	nonactive = 0;
3796 
3797 	if (tp) {
3798 		/*
3799 		 * Circle should have been broken.
3800 		 */
3801 		ASSERT(tp->t_back->t_forw == NULL);
3802 		type = tp->t_type;
3803 		INUM2STR(type, str);
3804 	}
3805 
3806 	for (; tp; tp = ntp) {
3807 		ntp = tp->t_forw;
3808 		count++;
3809 		ASSERT(tp->t_id != (timeout_id_t)0);
3810 		if (untimeout(tp->t_id) < 0) {
3811 			nonactive++;
3812 			PR_TIMER("%s: bad %s untimeout (domain=%d)\n",
3813 				proc, str, tp->t_domid);
3814 		} else {
3815 			PR_TIMER("%s: good %s untimeout (domain=%d)\n",
3816 				proc, str, tp->t_domid);
3817 		}
3818 		/*
3819 		 * There are two possible outcomes from
3820 		 * the untimeout().  Each ultimately result
3821 		 * in us having to free the timeout structure.
3822 		 *
3823 		 * 1. We successfully aborted a timeout call.
3824 		 *
3825 		 * 2. We failed to find the given timer.  He
3826 		 *    probably just fired off.
3827 		 */
3828 		idn_timer_free(tp);
3829 	}
3830 	PR_TIMER("%s: stopped %d of %d %s timers\n",
3831 		proc, count - nonactive, count, str);
3832 
3833 	return (count);
3834 }
3835 
3836 void
3837 idn_timer_dequeue(idn_timerq_t *tq, idn_timer_t *tp)
3838 {
3839 	ASSERT(tq && tp);
3840 	ASSERT(IDN_TIMERQ_IS_LOCKED(tq));
3841 
3842 	ASSERT(tp->t_q == tq);
3843 
3844 	if (tp->t_onq == 0) {
3845 		/*
3846 		 * We've already been dequeued.
3847 		 */
3848 		ASSERT(tp == tp->t_forw);
3849 		ASSERT(tp == tp->t_back);
3850 	} else {
3851 		/*
3852 		 * We're still in the queue, get out.
3853 		 */
3854 		if (tq->tq_queue == tp)
3855 			tq->tq_queue = tp->t_forw;
3856 		tp->t_forw->t_back = tp->t_back;
3857 		tp->t_back->t_forw = tp->t_forw;
3858 		tp->t_onq = 0;
3859 		if (--(tq->tq_count) == 0) {
3860 			ASSERT(tq->tq_queue == tp);
3861 			tq->tq_queue = NULL;
3862 		}
3863 		tp->t_forw = tp->t_back = tp;
3864 	}
3865 }
3866 
3867 /*
3868  * -----------------------------------------------------------------------
3869  */
3870 /*ARGSUSED*/
3871 static int
3872 idn_slabpool_report(queue_t *wq, mblk_t *mp, caddr_t cp, cred_t *cr)
3873 {
3874 	register int	p, nfree;
3875 	char		dsetstr[128];
3876 
3877 	ASSERT(IDN_GLOCK_IS_HELD());
3878 
3879 	if (idn.slabpool == NULL) {
3880 		(void) mi_mpprintf(mp,
3881 				"IDN slabpool not initialized (masterid = %d)",
3882 				IDN_GET_MASTERID());
3883 		return (0);
3884 	}
3885 
3886 	for (p = nfree = 0; p < idn.slabpool->npools; p++)
3887 		nfree += idn.slabpool->pool[p].nfree;
3888 
3889 	(void) mi_mpprintf(mp,
3890 			"IDN slabpool (ntotal_slabs = %d, nalloc = %d, "
3891 			"npools = %d)",
3892 			idn.slabpool->ntotslabs,
3893 			idn.slabpool->ntotslabs - nfree,
3894 			idn.slabpool->npools);
3895 
3896 	(void) mi_mpprintf(mp, "pool  nslabs  nfree domains");
3897 
3898 	for (p = 0; p < idn.slabpool->npools; p++) {
3899 		register int	d, s;
3900 		uint_t		domset;
3901 
3902 		domset = 0;
3903 		for (s = 0; s < idn.slabpool->pool[p].nslabs; s++) {
3904 			short	dd;
3905 
3906 			dd = idn.slabpool->pool[p].sarray[s].sl_domid;
3907 			if (dd != (short)IDN_NIL_DOMID)
3908 				DOMAINSET_ADD(domset, dd);
3909 		}
3910 		dsetstr[0] = '\0';
3911 		if (domset) {
3912 			for (d = 0; d < MAX_DOMAINS; d++) {
3913 				if (!DOMAIN_IN_SET(domset, d))
3914 					continue;
3915 
3916 				if (dsetstr[0] == '\0')
3917 					(void) sprintf(dsetstr, "%d", d);
3918 				else
3919 					(void) sprintf(dsetstr, "%s %d",
3920 							dsetstr, d);
3921 			}
3922 		}
3923 
3924 		if (p < 10)
3925 			(void) mi_mpprintf(mp, "  %d     %d       %d    %s",
3926 					p, idn.slabpool->pool[p].nslabs,
3927 					idn.slabpool->pool[p].nfree,
3928 					dsetstr);
3929 		else
3930 			(void) mi_mpprintf(mp, " %d     %d       %d    %s",
3931 					p, idn.slabpool->pool[p].nslabs,
3932 					idn.slabpool->pool[p].nfree,
3933 					dsetstr);
3934 	}
3935 	return (0);
3936 }
3937 
3938 /*ARGSUSED*/
3939 static int
3940 idn_buffer_report(queue_t *wq, mblk_t *mp, caddr_t cp, cred_t *cr)
3941 {
3942 	smr_slab_t	*sp;
3943 	register int	d, cnt;
3944 	int		bufcount[MAX_DOMAINS];
3945 	int		spl;
3946 
3947 	ASSERT(IDN_GLOCK_IS_HELD());
3948 
3949 	if (idn.localid == IDN_NIL_DOMID) {
3950 		(void) mi_mpprintf(mp, "IDN not initialized (localid = %d)",
3951 				idn.localid);
3952 		return (0);
3953 	}
3954 
3955 	(void) mi_mpprintf(mp, "Local domain has %d slabs allocated.",
3956 				idn_domain[idn.localid].dnslabs);
3957 
3958 	DSLAB_LOCK_SHARED(idn.localid);
3959 	if ((sp = idn_domain[idn.localid].dslab) == NULL) {
3960 		DSLAB_UNLOCK(idn.localid);
3961 		return (0);
3962 	}
3963 
3964 	bzero(bufcount, sizeof (bufcount));
3965 	cnt = 0;
3966 
3967 	spl = splhi();
3968 	for (; sp; sp = sp->sl_next) {
3969 		smr_slabbuf_t	*bp;
3970 
3971 		while (!lock_try(&sp->sl_lock))
3972 			;
3973 		for (bp = sp->sl_inuse; bp; bp = bp->sb_next) {
3974 			bufcount[bp->sb_domid]++;
3975 			cnt++;
3976 		}
3977 		lock_clear(&sp->sl_lock);
3978 	}
3979 	splx(spl);
3980 
3981 	DSLAB_UNLOCK(idn.localid);
3982 
3983 	(void) mi_mpprintf(mp, "Local domain has %d buffers outstanding.", cnt);
3984 	if (cnt == 0)
3985 		return (0);
3986 
3987 	(void) mi_mpprintf(mp, "Domain  nbufs");
3988 	for (d = 0; d < MAX_DOMAINS; d++)
3989 		if (bufcount[d]) {
3990 			if (d < 10)
3991 				(void) mi_mpprintf(mp, "   %d      %d",
3992 							d, bufcount[d]);
3993 			else
3994 				(void) mi_mpprintf(mp, "  %d      %d",
3995 							d, bufcount[d]);
3996 		}
3997 
3998 	return (0);
3999 }
4000 
4001 static const char *
4002 _get_spaces(int w, int s, int W)
4003 {
4004 	static const char *const _spaces[] = {
4005 		"",			/* 0 */
4006 		" ",			/* 1 */
4007 		"  ", 			/* 2 */
4008 		"   ", 			/* 3 */
4009 		"    ", 		/* 4 */
4010 		"     ", 		/* 5 */
4011 		"      ", 		/* 6 */
4012 		"       ", 		/* 7 */
4013 		"        ", 		/* 8 */
4014 		"         ", 		/* 9 */
4015 		"          ", 		/* 10 */
4016 		"           ", 		/* 11 */
4017 		"            ", 	/* 12 */
4018 		"             ", 	/* 13 */
4019 		"              ", 	/* 14 */
4020 		"               ", 	/* 15 */
4021 		"                ", 	/* 16 */
4022 		"                 ", 	/* 17 */
4023 		"                  ", 	/* 18 */
4024 		"                   ", 	/* 19 */
4025 	};
4026 	return (_spaces[w+s-W]);
4027 }
4028 
4029 #define	_SSS(X, W, w, s) \
4030 	(((w) >= (W)) && (X)) ? _get_spaces((w), (s), (W))
4031 
4032 static const char *
4033 _hexspace(uint64_t v, int sz, int width, int padding)
4034 {
4035 	int	maxnbl = 16;
4036 	int	diff;
4037 	uchar_t	*np;
4038 
4039 	diff = sizeof (uint64_t) - sz;
4040 	np = (uchar_t *)&v + diff;
4041 	maxnbl -= diff << 1;
4042 	while (sz-- > 0) {
4043 		if ((*np & 0xf0) && (width >= maxnbl))
4044 			return (_get_spaces(width, padding, maxnbl));
4045 		maxnbl--;
4046 		if ((*np & 0x0f) && (width >= maxnbl))
4047 			return (_get_spaces(width, padding, maxnbl));
4048 		maxnbl--;
4049 		np++;
4050 	}
4051 	return (_get_spaces(width, padding, 1));
4052 }
4053 
4054 #define	HEXSPACE(v, t, w, s)	_hexspace((uint64_t)(v), sizeof (t), (w), (s))
4055 
4056 #define	DECSPACE(n, w, s) \
4057 	(_SSS((uint_t)(n) >= 10000000, 8, (w), (s)) : \
4058 	_SSS((uint_t)(n) >= 1000000, 7, (w), (s)) : \
4059 	_SSS((uint_t)(n) >= 100000, 6, (w), (s)) : \
4060 	_SSS((uint_t)(n) >= 10000, 5, (w), (s)) : \
4061 	_SSS((uint_t)(n) >= 1000, 4, (w), (s)) : \
4062 	_SSS((uint_t)(n) >= 100, 3, (w), (s)) : \
4063 	_SSS((uint_t)(n) >= 10, 2, (w), (s)) : \
4064 	_get_spaces((w), (s), 1))
4065 
4066 #define	DECSPACE16(n, w, s) \
4067 	(_SSS((n) >= 10000, 5, (w), (s)) : \
4068 	_SSS((n) >= 1000, 4, (w), (s)) : \
4069 	_SSS((n) >= 100, 3, (w), (s)) : \
4070 	_SSS((n) >= 10, 2, (w), (s)) : \
4071 	_get_spaces((w), (s), 1))
4072 
4073 #define	MBXINFO(mtp) \
4074 	&mtp->mt_header, \
4075 		HEXSPACE(&mtp->mt_header, &mtp->mt_header, 16, 2), \
4076 	mtp->mt_header.mh_svr_ready_ptr, \
4077 		HEXSPACE(mtp->mt_header.mh_svr_ready_ptr, \
4078 			mtp->mt_header.mh_svr_ready_ptr, 8, 1), \
4079 	mtp->mt_header.mh_svr_active_ptr, \
4080 		HEXSPACE(mtp->mt_header.mh_svr_active_ptr, \
4081 			mtp->mt_header.mh_svr_active_ptr, 8, 2), \
4082 	*(ushort_t *)(IDN_OFFSET2ADDR(mtp->mt_header.mh_svr_ready_ptr)), \
4083 	DECSPACE16(*(ushort_t *) \
4084 			(IDN_OFFSET2ADDR(mtp->mt_header.mh_svr_ready_ptr)), \
4085 			1, 1), \
4086 	*(ushort_t *)(IDN_OFFSET2ADDR(mtp->mt_header.mh_svr_active_ptr)), \
4087 	DECSPACE16(*(ushort_t *) \
4088 			(IDN_OFFSET2ADDR(mtp->mt_header.mh_svr_active_ptr)), \
4089 			1, 5), \
4090 	mtp->mt_header.mh_cookie, \
4091 		HEXSPACE(mtp->mt_header.mh_cookie, \
4092 			mtp->mt_header.mh_cookie, 8, 2), \
4093 	&mtp->mt_queue[0], \
4094 		HEXSPACE(&mtp->mt_queue[0], &mtp->mt_queue[0], 16, 2)
4095 
4096 /*ARGSUSED*/
4097 static int
4098 idn_mboxtbl_report(queue_t *wq, mblk_t *mp, caddr_t cp, cred_t *cr)
4099 {
4100 	register int		c, n, domid, subdomid;
4101 	register idn_mboxtbl_t	*mtp;
4102 	register idn_mboxmsg_t	*msp;
4103 	idn_mboxtbl_t		*map, *mtbasep;
4104 
4105 
4106 	ASSERT((cp == MBXTBL_PART_REPORT) || (cp == MBXTBL_FULL_REPORT));
4107 
4108 	if (IDN_GLOCK_TRY_SHARED() == 0) {
4109 		(void) mi_mpprintf(mp, "Local domain busy, try again.");
4110 		return (0);
4111 	}
4112 
4113 	if ((map = idn.mboxarea) == NULL) {
4114 		(void) mi_mpprintf(mp,
4115 				"WARNING: Local domain is not master, "
4116 				"ASSUMING idn.smr.vaddr.");
4117 		map = (idn_mboxtbl_t *)idn.smr.vaddr;
4118 	}
4119 
4120 	if (map) {
4121 		(void) mi_mpprintf(mp, "Mailbox Area starts @ 0x%p",
4122 					map);
4123 	} else {
4124 		(void) mi_mpprintf(mp, "Mailbox Area not found.");
4125 		goto repdone;
4126 	}
4127 
4128 	if (!idn.nchannels) {
4129 		(void) mi_mpprintf(mp, "No OPEN channels found");
4130 		goto repdone;
4131 	}
4132 
4133 	for (c = 0; c < IDN_MAX_NETS; c++) {
4134 
4135 		IDN_CHAN_LOCK_GLOBAL(&idn.chan_servers[c]);
4136 		if (!IDN_CHANNEL_IS_ATTACHED(&idn.chan_servers[c])) {
4137 			IDN_CHAN_UNLOCK_GLOBAL(&idn.chan_servers[c]);
4138 			continue;
4139 		}
4140 
4141 		(void) mi_mpprintf(mp,
4142 				"Channel %d ---------------------------"
4143 				"--------------------------"
4144 				"-----------------------------", c);
4145 		(void) mi_mpprintf(mp,
4146 				"  Domain   Header            "
4147 				"Ready/Active Ptrs    "
4148 				"rdy/actv  cookie    Queue             "
4149 				"busy");
4150 
4151 		for (domid = 0; domid < MAX_DOMAINS; domid++) {
4152 			register int	busy_count;
4153 
4154 			if ((cp == MBXTBL_PART_REPORT) &&
4155 				(idn_domain[domid].dcpu == IDN_NIL_DCPU))
4156 				continue;
4157 
4158 			mtbasep = IDN_MBOXAREA_BASE(map, domid);
4159 
4160 			for (subdomid = 0; subdomid < MAX_DOMAINS;
4161 								subdomid++) {
4162 				mtp = IDN_MBOXTBL_PTR(mtbasep, subdomid);
4163 				mtp = IDN_MBOXTBL_PTR_CHAN(mtp, c);
4164 
4165 				if (subdomid == domid) {
4166 					if (subdomid == 0)
4167 						(void) mi_mpprintf(mp,
4168 							"   %x.%x-%d%s%s",
4169 							domid, subdomid, c,
4170 							/*CONSTCOND*/
4171 							DECSPACE(c, 2, 2),
4172 							"-- unused --");
4173 					else
4174 						(void) mi_mpprintf(mp,
4175 							"    .%x-%d%s%s",
4176 							subdomid, c,
4177 							/*CONSTCOND*/
4178 							DECSPACE(c, 2, 2),
4179 							"-- unused --");
4180 					continue;
4181 				}
4182 				busy_count = 0;
4183 				msp = &mtp->mt_queue[0];
4184 				for (n = 0; n < IDN_MMBOX_NUMENTRIES; n++) {
4185 					if (msp[n].ms_owner)
4186 						busy_count++;
4187 				}
4188 				if (subdomid == 0) {
4189 					(void) mi_mpprintf(mp,
4190 						"   %x.%x-%d%s%p%s%x%s/ %x%s"
4191 						"%d%s/ %d%s%x%s%p%s%d%s",
4192 						domid, subdomid, c,
4193 						/*CONSTCOND*/
4194 						DECSPACE(c, 2, 2),
4195 						MBXINFO(mtp), busy_count,
4196 						busy_count ? " <<<<<":"");
4197 				} else {
4198 					(void) mi_mpprintf(mp,
4199 						"    .%x-%d%s%p%s%x%s/ %x%s"
4200 						"%d%s/ %d%s%x%s%p%s%d%s",
4201 						subdomid, c,
4202 						/*CONSTCOND*/
4203 						DECSPACE(c, 2, 2),
4204 						MBXINFO(mtp), busy_count,
4205 						busy_count ? " <<<<<":"");
4206 				}
4207 			}
4208 		}
4209 		IDN_CHAN_UNLOCK_GLOBAL(&idn.chan_servers[c]);
4210 	}
4211 
4212 repdone:
4213 	IDN_GUNLOCK();
4214 
4215 	return (0);
4216 }
4217 
4218 /*ARGSUSED*/
4219 static void
4220 idn_mainmbox_domain_report(queue_t *wq, mblk_t *mp, int domid,
4221 					idn_mainmbox_t *mmp, char *mbxtype)
4222 {
4223 	register int	c;
4224 
4225 	if (mmp == NULL) {
4226 		(void) mi_mpprintf(mp, " %x.%s  -- none --", domid, mbxtype);
4227 		return;
4228 	}
4229 
4230 	for (c = 0; c < IDN_MAX_NETS; mmp++, c++) {
4231 		int	mm_count;
4232 
4233 		IDN_CHAN_LOCK_GLOBAL(&idn.chan_servers[c]);
4234 		if (IDN_CHANNEL_IS_DETACHED(&idn.chan_servers[c])) {
4235 			(void) mi_mpprintf(mp, " %x.%s  %u  -- not open --",
4236 					domid, mbxtype, (int)mmp->mm_channel);
4237 			IDN_CHAN_UNLOCK_GLOBAL(&idn.chan_servers[c]);
4238 			continue;
4239 		}
4240 
4241 		mm_count = ((mmp->mm_count < 0) ? 0 : mmp->mm_count) / 1000;
4242 
4243 		(void) mi_mpprintf(mp, " %x.%s  %d%s%d%s%d%s%p%s%p%s%p%s%d/%d",
4244 					domid, mbxtype,
4245 					(int)mmp->mm_channel,
4246 					/*CONSTCOND*/
4247 					DECSPACE((int)mmp->mm_channel, 5, 2),
4248 					mm_count, DECSPACE(mm_count, 8, 2),
4249 					mmp->mm_dropped,
4250 					DECSPACE(mmp->mm_dropped, 8, 2),
4251 					mmp->mm_smr_mboxp,
4252 					HEXSPACE(mmp->mm_smr_mboxp,
4253 						mmp->mm_smr_mboxp, 16, 2),
4254 					mmp->mm_smr_readyp,
4255 					HEXSPACE(mmp->mm_smr_readyp,
4256 						mmp->mm_smr_readyp, 16, 2),
4257 					mmp->mm_smr_activep,
4258 					HEXSPACE(mmp->mm_smr_activep,
4259 						mmp->mm_smr_activep, 16, 2),
4260 					mmp->mm_qiget, mmp->mm_qiput);
4261 		IDN_CHAN_UNLOCK_GLOBAL(&idn.chan_servers[c]);
4262 	}
4263 }
4264 
4265 /*ARGSUSED2*/
4266 static int
4267 idn_mainmbox_report(queue_t *wq, mblk_t *mp, caddr_t cp, cred_t *cr)
4268 {
4269 	int	domid;
4270 	int	header = 0;
4271 
4272 	/*
4273 	 * Domain 0 never has a send/recv mainmbox so
4274 	 * don't bother printing him.
4275 	 */
4276 	for (domid = 1; domid < MAX_DOMAINS; domid++) {
4277 		idn_domain_t	*dp;
4278 
4279 		dp = &idn_domain[domid];
4280 
4281 		if (dp->dcpu == IDN_NIL_DCPU)
4282 			continue;
4283 		IDN_DLOCK_SHARED(domid);
4284 		if (dp->dcpu == IDN_NIL_DCPU) {
4285 			IDN_DUNLOCK(domid);
4286 			continue;
4287 		}
4288 		if (!header) {
4289 			(void) mi_mpprintf(mp,
4290 					"Domain  Chan   PktCntK   "
4291 					"PktDrop   SMRMbox           "
4292 					"ReadyPtr          "
4293 					"ActvPtr          Miget/Miput");
4294 			header = 1;
4295 		}
4296 
4297 		mutex_enter(&dp->dmbox.m_mutex);
4298 		idn_mainmbox_domain_report(wq, mp, domid,
4299 					idn_domain[domid].dmbox.m_send,
4300 					"snd");
4301 		idn_mainmbox_domain_report(wq, mp, domid,
4302 					idn_domain[domid].dmbox.m_recv,
4303 					"rcv");
4304 		mutex_exit(&dp->dmbox.m_mutex);
4305 
4306 		IDN_DUNLOCK(domid);
4307 
4308 		(void) mi_mpprintf(mp,
4309 				"  ---------------------------------------"
4310 				"------------------------"
4311 				"----------------------------");
4312 	}
4313 
4314 	if (!header)
4315 		(void) mi_mpprintf(mp, "No ACTIVE domain connections exist");
4316 
4317 	return (0);
4318 }
4319 
4320 /*ARGSUSED*/
4321 static int
4322 idn_global_report(queue_t *wq, mblk_t *mp, caddr_t cp, cred_t *cr)
4323 {
4324 	int		i, nactive, masterid, nretry;
4325 	uint_t		locpfn_upper, locpfn_lower,
4326 			rempfn_upper, rempfn_lower;
4327 	uint_t		marea_upper, marea_lower,
4328 			iarea_upper, iarea_lower;
4329 	char		alt_dbuffer[64];
4330 	idn_retry_job_t	*rp;
4331 	domainset_t	retryset;
4332 	domainset_t	connected;
4333 	idn_synczone_t	*zp;
4334 	idn_syncop_t	*sp;
4335 	idn_domain_t	*dp;
4336 	char		*dbp, *dbuffer;
4337 
4338 	if (IDN_SYNC_TRYLOCK() == 0) {
4339 		(void) mi_mpprintf(mp, "Sync lock busy, try again.");
4340 		return (0);
4341 	}
4342 
4343 	if (IDN_GLOCK_TRY_SHARED() == 0) {
4344 		(void) mi_mpprintf(mp, "Local domain busy, try again.");
4345 		IDN_SYNC_UNLOCK();
4346 		return (0);
4347 	}
4348 	if ((dbp = dbuffer = ALLOC_DISPSTRING()) == NULL)
4349 		dbp = alt_dbuffer;
4350 
4351 	(void) mi_mpprintf(mp, "IDN\n    Global State = %s (%d)",
4352 			idngs_str[idn.state], idn.state);
4353 
4354 	(void) mi_mpprintf(mp, "SMR");
4355 	(void) mi_mpprintf(mp, "    vaddr                ");
4356 	(void) mi_mpprintf(mp, "    0x%p", idn.smr.vaddr);
4357 
4358 	(void) mi_mpprintf(mp, "    paddr-local     paddr-remote");
4359 	masterid = IDN_GET_MASTERID();
4360 	locpfn_upper = (uint_t)(idn.smr.locpfn >> (32 - PAGESHIFT));
4361 	locpfn_lower = (uint_t)(idn.smr.locpfn << PAGESHIFT);
4362 	if (idn.smr.rempfn == PFN_INVALID) {
4363 		rempfn_upper = rempfn_lower = 0;
4364 	} else {
4365 		rempfn_upper = (uint_t)(idn.smr.rempfn >> (32 - PAGESHIFT));
4366 		rempfn_lower = (uint_t)(idn.smr.rempfn << PAGESHIFT);
4367 	}
4368 	(void) mi_mpprintf(mp, "    0x%x.%x%s0x%x.%x",
4369 			locpfn_upper, locpfn_lower,
4370 			HEXSPACE(locpfn_lower, locpfn_lower, 8,
4371 				(locpfn_upper < 0x10) ? 4 : 3),
4372 			rempfn_upper, rempfn_lower);
4373 
4374 	(void) mi_mpprintf(mp, "    SMR length  = %d MBytes", IDN_SMR_SIZE);
4375 	(void) mi_mpprintf(mp, "    SMR bufsize = %d Bytes", IDN_SMR_BUFSIZE);
4376 	(void) mi_mpprintf(mp, "    NWR length  = %d MBytes", IDN_NWR_SIZE);
4377 	marea_upper = (uint_t)((uint64_t)IDN_MBOXAREA_SIZE >> 32);
4378 	marea_lower = (uint_t)((uint64_t)IDN_MBOXAREA_SIZE & 0xffffffff);
4379 	iarea_upper = (uint_t)((uint64_t)(MB2B(IDN_NWR_SIZE) -
4380 				(size_t)IDN_MBOXAREA_SIZE) >> 32);
4381 	iarea_lower = (uint_t)((MB2B(IDN_NWR_SIZE) -
4382 				(size_t)IDN_MBOXAREA_SIZE) & 0xffffffff);
4383 	(void) mi_mpprintf(mp,
4384 			"    [ mbox area = 0x%x.%x Bytes, "
4385 			"iobuf area = 0x%x.%x Bytes ]",
4386 			marea_upper, marea_lower, iarea_upper, iarea_lower);
4387 
4388 	(void) mi_mpprintf(mp,
4389 			"\nIDNnet (local domain [id:%d] [name:%s] is %s)",
4390 			idn.localid,
4391 			idn_domain[idn.localid].dname,
4392 			(masterid == IDN_NIL_DOMID) ? "IDLE" :
4393 			(idn.localid == masterid) ? "MASTER" :
4394 			"SLAVE");
4395 	nactive = 0;
4396 	for (i = 0; i < IDN_MAX_NETS; i++) {
4397 		IDN_CHAN_LOCK_GLOBAL(&idn.chan_servers[i]);
4398 		if (IDN_CHANNEL_IS_ACTIVE(&idn.chan_servers[i]))
4399 			nactive++;
4400 		IDN_CHAN_UNLOCK_GLOBAL(&idn.chan_servers[i]);
4401 	}
4402 	(void) mi_mpprintf(mp, "    I/O Networks: (Open = %d, "
4403 			"Active = %d, Max = %d)",
4404 			idn.nchannels, nactive, IDN_MAX_NETS);
4405 	(void) mi_mpprintf(mp, "    Number of Domains  = %d", idn.ndomains);
4406 	(void) mi_mpprintf(mp, "    Number of AWOLs    = %d", idn.nawols);
4407 	/*
4408 	 * During connect domains can possibly be in ds_connected
4409 	 * while still in ds_trans_on.  Only once they leave ds_trans_on
4410 	 * are they really connected.
4411 	 */
4412 	connected = idn.domset.ds_connected & ~idn.domset.ds_trans_on;
4413 	DOMAINSET_ADD(connected, idn.localid);
4414 	boardset2str(connected, dbp);
4415 	(void) mi_mpprintf(mp, "    Connected Domains      = %s", dbp);
4416 	domainset2str(idn.domset.ds_trans_on, dbp);
4417 	(void) mi_mpprintf(mp, "    Pending Domain Links   = %s",
4418 			idn.domset.ds_trans_on ? dbp : "<>");
4419 	domainset2str(idn.domset.ds_trans_off, dbp);
4420 	(void) mi_mpprintf(mp, "    Pending Domain Unlinks = %s",
4421 			idn.domset.ds_trans_off ? dbp : "<>");
4422 	mutex_enter(&idn.retryqueue.rq_mutex);
4423 	nretry = idn.retryqueue.rq_count;
4424 	retryset = 0;
4425 	for (i = 0, rp = idn.retryqueue.rq_jobs; i < nretry; i++,
4426 							rp = rp->rj_next) {
4427 		int	domid;
4428 
4429 		domid = IDN_RETRY_TOKEN2DOMID(rp->rj_token);
4430 		if (VALID_DOMAINID(domid)) {
4431 			DOMAINSET_ADD(retryset, domid);
4432 		}
4433 	}
4434 	mutex_exit(&idn.retryqueue.rq_mutex);
4435 	domainset2str(retryset, dbp);
4436 	(void) mi_mpprintf(mp, "    Retry Jobs:Domains     = %d:%s",
4437 			nretry, retryset ? dbp : "<>");
4438 	domainset2str(idn.domset.ds_hitlist, dbp);
4439 	(void) mi_mpprintf(mp, "    Hitlist Domains        = %s",
4440 			idn.domset.ds_hitlist ? dbp : "<>");
4441 	domainset2str(idn.domset.ds_relink, dbp);
4442 	(void) mi_mpprintf(mp, "    Reconfig Domains       = %s",
4443 			idn.domset.ds_relink ? dbp : "<>");
4444 	if (idn.domset.ds_relink)
4445 		(void) mi_mpprintf(mp, "         new master id = %d",
4446 				IDN_GET_NEW_MASTERID());
4447 	if (masterid == IDN_NIL_DOMID) {
4448 		(void) mi_mpprintf(mp, "    Master Domain: no master");
4449 	} else {
4450 		idn_domain_t	*mdp;
4451 
4452 		mdp = &idn_domain[masterid];
4453 
4454 		(void) mi_mpprintf(mp,
4455 				"    Master Domain (id:name/brds - state):");
4456 
4457 		if (strlen(mdp->dname) > 0)
4458 			strcpy(dbp, mdp->dname);
4459 		else
4460 			boardset2str(mdp->dhw.dh_boardset, dbp);
4461 		if (masterid < 10)
4462 			(void) mi_mpprintf(mp, "         %d: %s - %s",
4463 					masterid, dbp,
4464 					idnds_str[mdp->dstate]);
4465 		else
4466 			(void) mi_mpprintf(mp, "        %d: %s - %s",
4467 					masterid, dbp,
4468 					idnds_str[mdp->dstate]);
4469 	}
4470 	if (idn.ndomains <= 1) {
4471 		(void) mi_mpprintf(mp, "    Slave Domains: none");
4472 	} else {
4473 		int	d;
4474 
4475 		(void) mi_mpprintf(mp,
4476 				"    Slave Domains (id:name/brds - state):");
4477 		for (d = 0; d < MAX_DOMAINS; d++) {
4478 			dp = &idn_domain[d];
4479 
4480 			if ((dp->dcpu == IDN_NIL_DCPU) || (d == masterid))
4481 				continue;
4482 
4483 			if (strlen(dp->dname) > 0)
4484 				strcpy(dbp, dp->dname);
4485 			else
4486 				boardset2str(dp->dhw.dh_boardset, dbp);
4487 			if (d < 10)
4488 				(void) mi_mpprintf(mp, "         %d: %s - %s",
4489 						d, dbp,
4490 						idnds_str[dp->dstate]);
4491 			else
4492 				(void) mi_mpprintf(mp, "        %d: %s - %s",
4493 						d, dbp,
4494 						idnds_str[dp->dstate]);
4495 		}
4496 	}
4497 
4498 	if (idn.nawols == 0) {
4499 		(void) mi_mpprintf(mp, "    AWOL Domains: none");
4500 	} else {
4501 		int	d;
4502 
4503 		(void) mi_mpprintf(mp, "    AWOL Domains (id:name/brds):");
4504 		for (d = 0; d < MAX_DOMAINS; d++) {
4505 			dp = &idn_domain[d];
4506 
4507 			if (!DOMAIN_IN_SET(idn.domset.ds_awol, d) ||
4508 						(dp->dcpu == IDN_NIL_DCPU))
4509 				continue;
4510 
4511 			if (strlen(dp->dname) > 0)
4512 				strcpy(dbp, dp->dname);
4513 			else
4514 				boardset2str(dp->dhw.dh_boardset, dbp);
4515 			if (d < 10)
4516 				(void) mi_mpprintf(mp, "         %d: %s",
4517 						d, dbp);
4518 			else
4519 				(void) mi_mpprintf(mp, "        %d: %s",
4520 						d, dbp);
4521 		}
4522 	}
4523 
4524 	/*CONSTCOND*/
4525 	i = IDN_SYNC_GETZONE(IDNSYNC_CONNECT);
4526 	zp = &idn.sync.sz_zone[i];
4527 	if (zp->sc_cnt == 0) {
4528 		(void) mi_mpprintf(mp, "    Sync Zone (con): [empty]");
4529 	} else {
4530 		(void) mi_mpprintf(mp, "    Sync Zone (con): [%d domains]",
4531 				zp->sc_cnt);
4532 		sp = zp->sc_op;
4533 		for (i = 0; (i < zp->sc_cnt) && sp; i++) {
4534 			(void) mi_mpprintf(mp,
4535 					"                     "
4536 					"%x: x_set =%s0x%x, r_set =%s0x%x",
4537 					sp->s_domid,
4538 					HEXSPACE(sp->s_set_exp,
4539 						sp->s_set_exp, 4, 1),
4540 					sp->s_set_exp,
4541 					HEXSPACE(sp->s_set_rdy,
4542 						sp->s_set_rdy, 4, 1),
4543 					sp->s_set_rdy);
4544 			sp = sp->s_next;
4545 		}
4546 	}
4547 	/*CONSTCOND*/
4548 	i = IDN_SYNC_GETZONE(IDNSYNC_DISCONNECT);
4549 	zp = &idn.sync.sz_zone[i];
4550 	if (zp->sc_cnt == 0) {
4551 		(void) mi_mpprintf(mp, "    Sync Zone (dis): [empty]");
4552 	} else {
4553 		(void) mi_mpprintf(mp, "    Sync Zone (dis): [%d domains]",
4554 				zp->sc_cnt);
4555 		sp = zp->sc_op;
4556 		for (i = 0; (i < zp->sc_cnt) && sp; i++) {
4557 			(void) mi_mpprintf(mp,
4558 					"                     "
4559 					"%x: x_set =%s0x%x, r_set =%s0x%x",
4560 					sp->s_domid,
4561 					HEXSPACE(sp->s_set_exp,
4562 						sp->s_set_exp, 4, 1),
4563 					sp->s_set_exp,
4564 					HEXSPACE(sp->s_set_rdy,
4565 						sp->s_set_rdy, 4, 1),
4566 					sp->s_set_rdy);
4567 			sp = sp->s_next;
4568 		}
4569 	}
4570 
4571 	IDN_GUNLOCK();
4572 	IDN_SYNC_UNLOCK();
4573 
4574 	if (dbuffer) {
4575 		FREE_DISPSTRING(dbuffer);
4576 	}
4577 
4578 	return (0);
4579 }
4580 
4581 /*ARGSUSED*/
4582 static int
4583 idn_domain_report(queue_t *wq, mblk_t *mp, caddr_t cp, cred_t *cr)
4584 {
4585 	int		d, nchan;
4586 	uint_t		domset;
4587 	idn_chanset_t	chanset;
4588 	idn_domain_t	*dp;
4589 	uint_t		pset_upper, pset_lower;
4590 	char		*dbuffer, *dbp;
4591 	char		alt_dbuffer[64];
4592 
4593 
4594 	if (IDN_SYNC_TRYLOCK() == 0) {
4595 		(void) mi_mpprintf(mp, "Sync lock busy, try again.");
4596 		return (0);
4597 	}
4598 
4599 	if (IDN_GLOCK_TRY_SHARED() == 0) {
4600 		(void) mi_mpprintf(mp, "Local domain busy, try again.");
4601 		IDN_SYNC_UNLOCK();
4602 		return (0);
4603 	}
4604 
4605 	if ((dbp = dbuffer = ALLOC_DISPSTRING()) == NULL)
4606 		dbp = alt_dbuffer;
4607 
4608 	if (cp == NULL)
4609 		domset = DOMAINSET(idn.localid);
4610 	else
4611 		domset = DOMAINSET_ALL;
4612 
4613 	for (d = 0; d < MAX_DOMAINS; d++) {
4614 
4615 		if (DOMAIN_IN_SET(domset, d) == 0)
4616 			continue;
4617 
4618 		dp = &idn_domain[d];
4619 
4620 		if (dp->dcpu == IDN_NIL_DCPU)
4621 			continue;
4622 
4623 		if (IDN_DLOCK_TRY_SHARED(d) == 0) {
4624 			if (d < 10)
4625 				(void) mi_mpprintf(mp,
4626 					"Domain %d   (0x%p) busy...",
4627 					d, dp);
4628 			else
4629 				(void) mi_mpprintf(mp,
4630 					"Domain %d  (0x%p) busy...",
4631 					d, dp);
4632 			continue;
4633 		}
4634 		if (dp->dcpu == IDN_NIL_DCPU) {
4635 			IDN_DUNLOCK(d);
4636 			continue;
4637 		}
4638 		if (d < 10)
4639 			(void) mi_mpprintf(mp, "%sDomain %d   (0x%p)",
4640 					(d && (idn.ndomains > 1)) ? "\n" : "",
4641 					d, dp);
4642 		else
4643 			(void) mi_mpprintf(mp, "%sDomain %d  (0x%p)",
4644 					(d && (idn.ndomains > 1)) ? "\n" : "",
4645 					d, dp);
4646 
4647 		if (d == idn.localid)
4648 			(void) mi_mpprintf(mp, "  (local)  State = %s (%d)",
4649 					idnds_str[dp->dstate], dp->dstate);
4650 		else
4651 			(void) mi_mpprintf(mp, "           State = %s (%d)",
4652 					idnds_str[dp->dstate], dp->dstate);
4653 		(void) mi_mpprintf(mp, "           Name = %s, Netid = %d",
4654 				(strlen(dp->dname) > 0) ? dp->dname : "<>",
4655 				(int)dp->dnetid);
4656 
4657 		CHANSET_ZERO(chanset);
4658 		nchan = idn_domain_is_registered(d, -1, &chanset);
4659 		if (dbuffer)
4660 			mask2str(chanset, dbp, 32);
4661 		else
4662 			(void) sprintf(dbp, "0x%x", chanset);
4663 		(void) mi_mpprintf(mp, "           Nchans = %d, Chanset = %s",
4664 				nchan, nchan ? dbp : "<>");
4665 		pset_upper = UPPER32_CPUMASK(dp->dcpuset);
4666 		pset_lower = LOWER32_CPUMASK(dp->dcpuset);
4667 		if (dbuffer)
4668 			boardset2str(dp->dhw.dh_boardset, dbp);
4669 		else
4670 			(void) sprintf(dbp, "0x%x", dp->dhw.dh_boardset);
4671 
4672 		(void) mi_mpprintf(mp, "           Nboards = %d, Brdset = %s",
4673 				dp->dhw.dh_nboards,
4674 				dp->dhw.dh_nboards ? dbp : "<>");
4675 		(void) sprintf(dbp, "0x%x.%x", pset_upper, pset_lower);
4676 		(void) mi_mpprintf(mp, "           Ncpus = %d, Cpuset = %s",
4677 				dp->dncpus, dp->dncpus ? dbp : "<>");
4678 		(void) mi_mpprintf(mp, "           Nmcadr = %d",
4679 				dp->dhw.dh_nmcadr);
4680 		(void) mi_mpprintf(mp,
4681 				"           MsgTimer = %s  (cnt = %d)",
4682 				(dp->dtimerq.tq_count > 0)
4683 				? "active" : "idle",
4684 				dp->dtimerq.tq_count);
4685 		(void) mi_mpprintf(mp, "           Dcpu = %d  "
4686 				"(lastcpu = %d, cpuindex = %d)",
4687 				dp->dcpu, dp->dcpu_last, dp->dcpuindex);
4688 		(void) mi_mpprintf(mp, "           Dio = %d  "
4689 				"(ioerr = %d, iochk = %d, iowanted = %d)",
4690 				dp->dio, dp->dioerr, dp->diocheck ? 1 : 0,
4691 				dp->diowanted ? 1 : 0);
4692 		if (dp->dsync.s_cmd == IDNSYNC_NIL) {
4693 			(void) mi_mpprintf(mp, "           Dsync = %s",
4694 				idnsync_str[IDNSYNC_NIL]);
4695 		} else {
4696 			(void) mi_mpprintf(mp,
4697 				"           Dsync = %s "
4698 				"(x_set = 0x%x, r_set = 0x%x)",
4699 				idnsync_str[dp->dsync.s_cmd],
4700 				(uint_t)dp->dsync.s_set_exp,
4701 				(uint_t)dp->dsync.s_set_rdy);
4702 		}
4703 		(void) mi_mpprintf(mp, "           Dvote = 0x%x",
4704 				dp->dvote.ticket);
4705 		(void) mi_mpprintf(mp, "           Dfin = %s (Sync = %s)",
4706 				idnfin_str[dp->dfin],
4707 				(dp->dfin_sync == IDNFIN_SYNC_OFF) ? "OFF" :
4708 				(dp->dfin_sync == IDNFIN_SYNC_YES) ? "YES" :
4709 				"NO");
4710 		(void) mi_mpprintf(mp, "           Dcookie_err = %s (cnt = %d)",
4711 				dp->dcookie_err ? "YES" : "NO",
4712 				dp->dcookie_errcnt);
4713 		IDN_DUNLOCK(d);
4714 	}
4715 
4716 	IDN_GUNLOCK();
4717 
4718 	if (dbuffer) {
4719 		FREE_DISPSTRING(dbuffer);
4720 	}
4721 
4722 	IDN_SYNC_UNLOCK();
4723 
4724 	return (0);
4725 }
4726 
4727 #define	SNOOP_ENTRIES	2048	/* power of 2 */
4728 
4729 struct snoop_buffer {
4730 /*  0 */	char	io;
4731 /*  1 */	char	board;
4732 /*  2 */	char	trans[14];
4733 
4734 /* 10 */	uint_t	xargs[4];
4735 } *snoop_data, snoop_buffer[SNOOP_ENTRIES+1];
4736 
4737 
4738 int		snoop_index;
4739 kmutex_t	snoop_mutex;
4740 static char	_bd2hexascii[] = {
4741 	'0', '1', '2', '3', '4', '5', '6', '7',
4742 	'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
4743 };
4744 
4745 #define	SNOOP_IDN(in, tr, bd, arg1, arg2, arg3, arg4) \
4746 { \
4747 	if (idn_snoop) { \
4748 		mutex_enter(&snoop_mutex); \
4749 		if (snoop_data == NULL) { \
4750 			snoop_data = (struct snoop_buffer *) \
4751 				(((uint_t)snoop_buffer + 0xf) & ~0xf); \
4752 		} \
4753 		snoop_data[snoop_index].io = ((in) == 0) ? 'o' : 'i'; \
4754 		snoop_data[snoop_index].board = \
4755 				((bd) == -1) ? 'X' : _bd2hexascii[bd]; \
4756 		(void) strncpy(snoop_data[snoop_index].trans, (tr), 14); \
4757 		snoop_data[snoop_index].xargs[0] = (arg1); \
4758 		snoop_data[snoop_index].xargs[1] = (arg2); \
4759 		snoop_data[snoop_index].xargs[2] = (arg3); \
4760 		snoop_data[snoop_index].xargs[3] = (arg4); \
4761 		snoop_index++; \
4762 		snoop_index &= SNOOP_ENTRIES - 1; \
4763 		mutex_exit(&snoop_mutex); \
4764 	} \
4765 }
4766 
4767 /*
4768  * Allocate the circular buffers to be used for
4769  * DMV interrupt processing.
4770  */
4771 static int
4772 idn_init_handler()
4773 {
4774 	int		i, c;
4775 	size_t		len;
4776 	idn_dmv_msg_t	*basep, *ivp;
4777 	uint32_t	ivp_offset;
4778 	procname_t	proc = "idn_init_handler";
4779 
4780 	if (idn.intr.dmv_data != NULL) {
4781 		cmn_err(CE_WARN,
4782 			"IDN: 130: IDN DMV handler already initialized");
4783 		return (-1);
4784 	}
4785 
4786 	/*
4787 	 * This memory will be touched by the low-level
4788 	 * DMV trap handler for IDN.
4789 	 */
4790 	len = sizeof (idn_dmv_data_t);
4791 	len = roundup(len, sizeof (uint64_t));
4792 	len += NCPU * idn_dmv_pending_max * sizeof (idn_dmv_msg_t);
4793 	len = roundup(len, PAGESIZE);
4794 
4795 	PR_PROTO("%s: sizeof (idn_dmv_data_t) = %lu\n",
4796 		proc, sizeof (idn_dmv_data_t));
4797 	PR_PROTO("%s: allocating %lu bytes for dmv data area\n", proc, len);
4798 
4799 	idn.intr.dmv_data_len = len;
4800 	idn.intr.dmv_data = kmem_zalloc(len, KM_SLEEP);
4801 
4802 	PR_PROTO("%s: DMV data area = %p\n", proc, idn.intr.dmv_data);
4803 
4804 	idn_dmv_data = (idn_dmv_data_t *)idn.intr.dmv_data;
4805 	basep = (idn_dmv_msg_t *)roundup((size_t)idn.intr.dmv_data +
4806 					sizeof (idn_dmv_data_t),
4807 					sizeof (uint64_t));
4808 	idn_dmv_data->idn_dmv_qbase = (uint64_t)basep;
4809 
4810 	ivp = basep;
4811 	ivp_offset = 0;
4812 	/*
4813 	 * The buffer queues are allocated per-cpu.
4814 	 */
4815 	for (c = 0, ivp = basep; c < NCPU; ivp++, c++) {
4816 		idn_dmv_data->idn_dmv_cpu[c].idn_dmv_current = ivp_offset;
4817 		idn_iv_queue[c] = ivp;
4818 		ivp_offset += sizeof (idn_dmv_msg_t);
4819 		for (i = 1; i < idn_dmv_pending_max; ivp++, i++) {
4820 			ivp->iv_next = ivp_offset;
4821 			ivp->iv_ready = 0;
4822 			lock_set(&ivp->iv_ready);
4823 			ivp_offset += sizeof (idn_dmv_msg_t);
4824 		}
4825 		ivp->iv_next = idn_dmv_data->idn_dmv_cpu[c].idn_dmv_current;
4826 		ivp->iv_ready = 0;
4827 		lock_set(&ivp->iv_ready);
4828 	}
4829 
4830 	idn.intr.dmv_inum = STARFIRE_DMV_IDN_BASE;
4831 	idn.intr.soft_inum = add_softintr((uint_t)idn_pil, idn_handler, 0);
4832 	idn_dmv_data->idn_soft_inum = idn.intr.soft_inum;
4833 	/*
4834 	 * Make sure everything is out there before
4835 	 * we effectively set it free for use.
4836 	 */
4837 	membar_stld_stst();
4838 
4839 	if (dmv_add_intr(idn.intr.dmv_inum, idn_dmv_handler,
4840 			(caddr_t)idn_dmv_data)) {
4841 		idn_deinit_handler();
4842 		cmn_err(CE_WARN, "IDN: 132: failed to add IDN DMV handler");
4843 		return (-1);
4844 	}
4845 
4846 	return (0);
4847 }
4848 
4849 static void
4850 idn_deinit_handler()
4851 {
4852 	if (idn.intr.dmv_data == NULL)
4853 		return;
4854 
4855 	(void) dmv_rem_intr(idn.intr.dmv_inum);
4856 	rem_softintr(idn.intr.soft_inum);
4857 	kmem_free(idn.intr.dmv_data, idn.intr.dmv_data_len);
4858 	idn.intr.dmv_data = NULL;
4859 }
4860 
4861 /*
4862  * High-level (soft interrupt) handler for DMV interrupts
4863  */
4864 /*ARGSUSED0*/
4865 static uint_t
4866 idn_handler(caddr_t unused, caddr_t unused2)
4867 {
4868 #ifdef DEBUG
4869 	int		count = 0;
4870 #endif /* DEBUG */
4871 	int		cpuid = (int)CPU->cpu_id;
4872 	ushort_t	mtype, atype;
4873 	idn_dmv_msg_t	*xp, *xplimit;
4874 	procname_t	proc = "idn_handler";
4875 
4876 	ASSERT(getpil() >= idn_pil);
4877 	flush_windows();
4878 
4879 	/*
4880 	 * Clear the synchronization flag to indicate that
4881 	 * processing has started.  As long as idn_dmv_active
4882 	 * is non-zero, idn_dmv_handler will queue work without
4883 	 * initiating a soft interrupt.  Since we clear it
4884 	 * first thing at most one pil-interrupt for IDN will
4885 	 * queue up behind the currently active one.  We don't
4886 	 * want to clear this flag at the end because it leaves
4887 	 * a window where an interrupt could get lost (unless it's
4888 	 * pushed by a subsequent interrupt).  The objective in
4889 	 * doing this is to prevent exhausting a cpu's intr_req
4890 	 * structures with interrupts of the same pil level.
4891 	 */
4892 	lock_clear(&idn_dmv_data->idn_dmv_cpu[cpuid].idn_dmv_active);
4893 
4894 	xp = idn_iv_queue[cpuid];
4895 	xplimit = xp + idn_dmv_pending_max;
4896 	xp += idn_intr_index[cpuid];
4897 	/*
4898 	 * As long as there's stuff that's READY in the
4899 	 * queue, keep processing.
4900 	 */
4901 	while (lock_try(&xp->iv_ready)) {
4902 
4903 		ASSERT(lock_try(&xp->iv_inuse) == 0);
4904 
4905 		mtype = (ushort_t)xp->iv_mtype;
4906 		mtype &= IDNP_MSGTYPE_MASK | IDNP_ACKNACK_MASK;
4907 		atype = (ushort_t)xp->iv_atype;
4908 
4909 		if (((int)xp->iv_ver == idn.version) && mtype) {
4910 			idn_protojob_t	*jp;
4911 #ifdef DEBUG
4912 			STRING(mstr);
4913 			STRING(astr);
4914 
4915 			INUM2STR(mtype, mstr);
4916 			if ((mtype & IDNP_MSGTYPE_MASK) == 0) {
4917 				INUM2STR(atype, astr);
4918 				strcat(mstr, "/");
4919 				strcat(mstr, astr);
4920 			}
4921 
4922 			count++;
4923 
4924 			PR_XDC("%s:%d:%d RECV: scpu = %d, msg = 0x%x(%s)\n",
4925 				proc, (int)xp->iv_domid, count,
4926 				(int)xp->iv_cpuid, mtype, mstr);
4927 			PR_XDC("%s:%d:%d R-DATA: a0 = 0x%x, a1 = 0x%x\n",
4928 				proc, (int)xp->iv_domid, count,
4929 				xp->iv_xargs0, xp->iv_xargs1);
4930 			PR_XDC("%s:%d:%d R-DATA: a2 = 0x%x, a3 = 0x%x\n",
4931 				proc, (int)xp->iv_domid, count,
4932 				xp->iv_xargs2, xp->iv_xargs3);
4933 #endif /* DEBUG */
4934 
4935 			if (mtype == IDNP_DATA) {
4936 				jp = NULL;
4937 				/*
4938 				 * The only time we receive pure
4939 				 * data messages at this level is
4940 				 * to wake up the channel server.
4941 				 * Since this is often an urgent
4942 				 * request we'll do it from here
4943 				 * instead of waiting for a proto
4944 				 * server to do it.
4945 				 */
4946 				idn_signal_data_server((int)xp->iv_domid,
4947 						(ushort_t)xp->iv_xargs0);
4948 			} else {
4949 				jp = idn_protojob_alloc(KM_NOSLEEP);
4950 				/*
4951 				 * If the allocation fails, just drop
4952 				 * the message and get on with life.
4953 				 * If memory pressure is this great then
4954 				 * dropping this message is probably
4955 				 * the least of our worries!
4956 				 */
4957 				if (jp) {
4958 					jp->j_msg.m_domid = (int)xp->iv_domid;
4959 					jp->j_msg.m_cpuid = (int)xp->iv_cpuid;
4960 					jp->j_msg.m_msgtype = mtype;
4961 					jp->j_msg.m_acktype = atype;
4962 					jp->j_msg.m_cookie = xp->iv_cookie;
4963 					SET_XARGS(jp->j_msg.m_xargs,
4964 						xp->iv_xargs0, xp->iv_xargs1,
4965 						xp->iv_xargs2, xp->iv_xargs3);
4966 				}
4967 
4968 			}
4969 			membar_ldst_stst();
4970 
4971 			lock_clear(&xp->iv_inuse);
4972 
4973 			if (jp)
4974 				idn_protojob_submit(jp->j_msg.m_domid, jp);
4975 		} else {
4976 			membar_ldst_stst();
4977 			IDN_GKSTAT_INC(gk_dropped_intrs);
4978 			lock_clear(&xp->iv_inuse);
4979 		}
4980 
4981 		if (++xp == xplimit)
4982 			xp = idn_iv_queue[cpuid];
4983 	}
4984 
4985 	idn_intr_index[cpuid] = xp - idn_iv_queue[cpuid];
4986 
4987 	return (DDI_INTR_CLAIMED);
4988 }
4989 
4990 void
4991 idn_awol_event_set(boardset_t boardset)
4992 {
4993 	idnsb_event_t	*sbp;
4994 	procname_t	proc = "idn_awol_event_set";
4995 
4996 	ASSERT(IDN_GLOCK_IS_EXCL());
4997 
4998 	mutex_enter(&idn.idnsb_mutex);
4999 	sbp = idn.idnsb_eventp;
5000 	if (sbp == NULL) {
5001 		cmn_err(CE_WARN, "IDN: 133: sigblock event area missing");
5002 		cmn_err(CE_CONT,
5003 			"IDN: 134: unable to mark boardset (0x%x) AWOL\n",
5004 			boardset);
5005 		mutex_exit(&idn.idnsb_mutex);
5006 		return;
5007 	}
5008 
5009 	if (boardset == 0) {
5010 		PR_PROTO("%s: AWOL BOARDSET is 0, NO EVENT <<<<<<<<<<<<<<<\n",
5011 			proc);
5012 		mutex_exit(&idn.idnsb_mutex);
5013 		return;
5014 	} else {
5015 		PR_PROTO("%s: MARKING BOARDSET (0x%x) AWOL\n", proc, boardset);
5016 	}
5017 	SSIEVENT_ADD(sbp, SSIEVENT_AWOL, boardset);
5018 	mutex_exit(&idn.idnsb_mutex);
5019 }
5020 
5021 void
5022 idn_awol_event_clear(boardset_t boardset)
5023 {
5024 	idnsb_event_t	*sbp;
5025 	procname_t	proc = "idn_awol_event_clear";
5026 
5027 	ASSERT(IDN_GLOCK_IS_EXCL());
5028 
5029 	mutex_enter(&idn.idnsb_mutex);
5030 	sbp = idn.idnsb_eventp;
5031 	if (sbp == NULL) {
5032 		cmn_err(CE_WARN, "IDN: 133: sigblock event area missing");
5033 		cmn_err(CE_CONT,
5034 			"IDN: 134: unable to mark boardset (0x%x) AWOL\n",
5035 			boardset);
5036 		mutex_exit(&idn.idnsb_mutex);
5037 		return;
5038 	}
5039 
5040 	if (boardset == 0) {
5041 		PR_PROTO("%s: AWOL BOARDSET is 0, NO EVENT <<<<<<<<<<<<<<<\n",
5042 			proc);
5043 		mutex_exit(&idn.idnsb_mutex);
5044 		return;
5045 	} else {
5046 		PR_PROTO("%s: CLEARING BOARDSET (0x%x) AWOL\n", proc, boardset);
5047 	}
5048 	SSIEVENT_DEL(sbp, SSIEVENT_AWOL, boardset);
5049 	mutex_exit(&idn.idnsb_mutex);
5050 }
5051 
5052 static void
5053 idn_gkstat_init()
5054 {
5055 	struct	kstat			*ksp;
5056 	struct	idn_gkstat_named	*sgkp;
5057 
5058 #ifdef	kstat
5059 	if ((ksp = kstat_create(IDNNAME, ddi_get_instance(idn.dip),
5060 		IDNNAME, "net", KSTAT_TYPE_NAMED,
5061 		sizeof (struct idn_gkstat_named) / sizeof (kstat_named_t),
5062 		KSTAT_FLAG_PERSISTENT)) == NULL) {
5063 #else
5064 	if ((ksp = kstat_create(IDNNAME, ddi_get_instance(idn.dip),
5065 			IDNNAME, "net", KSTAT_TYPE_NAMED,
5066 			sizeof (struct idn_gkstat_named) /
5067 			sizeof (kstat_named_t), 0)) == NULL) {
5068 #endif /* kstat */
5069 		cmn_err(CE_CONT, "IDN: 135: %s: %s\n",
5070 			IDNNAME, "kstat_create failed");
5071 		return;
5072 	}
5073 
5074 	idn.ksp = ksp;
5075 	sgkp = (struct idn_gkstat_named *)(ksp->ks_data);
5076 	kstat_named_init(&sgkp->sk_curtime,		"curtime",
5077 		KSTAT_DATA_ULONG);
5078 	kstat_named_init(&sgkp->sk_reconfigs,		"reconfigs",
5079 		KSTAT_DATA_ULONG);
5080 	kstat_named_init(&sgkp->sk_reconfig_last,	"reconfig_last",
5081 		KSTAT_DATA_ULONG);
5082 	kstat_named_init(&sgkp->sk_reaps,		"reaps",
5083 		KSTAT_DATA_ULONG);
5084 	kstat_named_init(&sgkp->sk_reap_last,		"reap_last",
5085 		KSTAT_DATA_ULONG);
5086 	kstat_named_init(&sgkp->sk_links,		"links",
5087 		KSTAT_DATA_ULONG);
5088 	kstat_named_init(&sgkp->sk_link_last,		"link_last",
5089 		KSTAT_DATA_ULONG);
5090 	kstat_named_init(&sgkp->sk_unlinks,		"unlinks",
5091 		KSTAT_DATA_ULONG);
5092 	kstat_named_init(&sgkp->sk_unlink_last,		"unlink_last",
5093 		KSTAT_DATA_ULONG);
5094 	kstat_named_init(&sgkp->sk_buffail,		"buf_fail",
5095 		KSTAT_DATA_ULONG);
5096 	kstat_named_init(&sgkp->sk_buffail_last,	"buf_fail_last",
5097 		KSTAT_DATA_ULONG);
5098 	kstat_named_init(&sgkp->sk_slabfail,		"slab_fail",
5099 		KSTAT_DATA_ULONG);
5100 	kstat_named_init(&sgkp->sk_slabfail_last,	"slab_fail_last",
5101 		KSTAT_DATA_ULONG);
5102 	kstat_named_init(&sgkp->sk_slabfail_last,	"slab_fail_last",
5103 		KSTAT_DATA_ULONG);
5104 	kstat_named_init(&sgkp->sk_reap_count,		"reap_count",
5105 		KSTAT_DATA_ULONG);
5106 	kstat_named_init(&sgkp->sk_dropped_intrs,	"dropped_intrs",
5107 		KSTAT_DATA_ULONG);
5108 	ksp->ks_update = idn_gkstat_update;
5109 	ksp->ks_private = (void *)NULL;
5110 	kstat_install(ksp);
5111 }
5112 
5113 static void
5114 idn_gkstat_deinit()
5115 {
5116 	if (idn.ksp)
5117 		kstat_delete(idn.ksp);
5118 	idn.ksp = NULL;
5119 }
5120 
5121 static int
5122 idn_gkstat_update(kstat_t *ksp, int rw)
5123 {
5124 	struct idn_gkstat_named	*sgkp;
5125 
5126 	sgkp = (struct idn_gkstat_named *)ksp->ks_data;
5127 
5128 	if (rw == KSTAT_WRITE) {
5129 		sg_kstat.gk_reconfigs	    = sgkp->sk_reconfigs.value.ul;
5130 		sg_kstat.gk_reconfig_last   = sgkp->sk_reconfig_last.value.ul;
5131 		sg_kstat.gk_reaps	    = sgkp->sk_reaps.value.ul;
5132 		sg_kstat.gk_reap_last	    = sgkp->sk_reap_last.value.ul;
5133 		sg_kstat.gk_links	    = sgkp->sk_links.value.ul;
5134 		sg_kstat.gk_link_last	    = sgkp->sk_link_last.value.ul;
5135 		sg_kstat.gk_unlinks	    = sgkp->sk_unlinks.value.ul;
5136 		sg_kstat.gk_unlink_last	    = sgkp->sk_unlink_last.value.ul;
5137 		sg_kstat.gk_buffail	    = sgkp->sk_buffail.value.ul;
5138 		sg_kstat.gk_buffail_last    = sgkp->sk_buffail_last.value.ul;
5139 		sg_kstat.gk_slabfail	    = sgkp->sk_slabfail.value.ul;
5140 		sg_kstat.gk_slabfail_last   = sgkp->sk_slabfail_last.value.ul;
5141 		sg_kstat.gk_reap_count	    = sgkp->sk_reap_count.value.ul;
5142 		sg_kstat.gk_dropped_intrs   = sgkp->sk_dropped_intrs.value.ul;
5143 	} else {
5144 		sgkp->sk_curtime.value.ul	  = lbolt;
5145 		sgkp->sk_reconfigs.value.ul	  = sg_kstat.gk_reconfigs;
5146 		sgkp->sk_reconfig_last.value.ul	  = sg_kstat.gk_reconfig_last;
5147 		sgkp->sk_reaps.value.ul		  = sg_kstat.gk_reaps;
5148 		sgkp->sk_reap_last.value.ul	  = sg_kstat.gk_reap_last;
5149 		sgkp->sk_links.value.ul		  = sg_kstat.gk_links;
5150 		sgkp->sk_link_last.value.ul	  = sg_kstat.gk_link_last;
5151 		sgkp->sk_unlinks.value.ul	  = sg_kstat.gk_unlinks;
5152 		sgkp->sk_unlink_last.value.ul	  = sg_kstat.gk_unlink_last;
5153 		sgkp->sk_buffail.value.ul	  = sg_kstat.gk_buffail;
5154 		sgkp->sk_buffail_last.value.ul    = sg_kstat.gk_buffail_last;
5155 		sgkp->sk_slabfail.value.ul	  = sg_kstat.gk_slabfail;
5156 		sgkp->sk_slabfail_last.value.ul   = sg_kstat.gk_slabfail_last;
5157 		sgkp->sk_reap_count.value.ul	  = sg_kstat.gk_reap_count;
5158 		sgkp->sk_dropped_intrs.value.ul	  = sg_kstat.gk_dropped_intrs;
5159 	}
5160 
5161 	return (0);
5162 }
5163 
5164 #ifdef DEBUG
5165 #define	RW_HISTORY	100
5166 static uint_t	rw_history[NCPU][RW_HISTORY];
5167 static int	rw_index[NCPU];
5168 #endif /* DEBUG */
5169 
5170 static int
5171 idn_rw_mem(idnop_t *idnop)
5172 {
5173 	uint_t		lo_off, hi_off;
5174 	int		rw, blksize, num;
5175 	int		cpuid;
5176 	register int	n, idx;
5177 	char 		*ibuf, *obuf;
5178 	char		*smraddr;
5179 	struct seg	*segp;
5180 	ulong_t		randx;
5181 	kmutex_t	slock;
5182 	kcondvar_t	scv;
5183 	static int	orig_gstate = IDNGS_IGNORE;
5184 	extern struct  seg	ktextseg;
5185 
5186 #define	RANDOM_INIT()	(randx = lbolt)
5187 #define	RANDOM(a, b)	\
5188 	(((a) >= (b)) ? \
5189 	(a) : (((randx = randx * 1103515245L + 12345) % ((b)-(a))) + (a)))
5190 
5191 	RANDOM_INIT();
5192 
5193 	lo_off  = idnop->rwmem.lo_off;
5194 	hi_off  = idnop->rwmem.hi_off;
5195 	blksize = idnop->rwmem.blksize;
5196 	num	= idnop->rwmem.num;
5197 	rw	= idnop->rwmem.rw;	/* 0 = rd, 1 = wr, 2 = rd/wr */
5198 
5199 	if (((hi_off > (uint_t)MB2B(IDN_SMR_SIZE)) || (lo_off >= hi_off) ||
5200 	    (blksize <= 0) || (blksize > (hi_off - lo_off)) || (num <= 0)) &&
5201 	    (idnop->rwmem.goawol == -1)) {
5202 		return (EINVAL);
5203 	}
5204 
5205 	if (idnop->rwmem.goawol && (orig_gstate == IDNGS_IGNORE)) {
5206 		IDN_GLOCK_EXCL();
5207 		cmn_err(CE_WARN, "IDN: Local domain going into IGNORE MODE!!");
5208 		orig_gstate = idn.state;
5209 		IDN_GSTATE_TRANSITION(IDNGS_IGNORE);
5210 		IDN_GUNLOCK();
5211 
5212 	} else if (!idnop->rwmem.goawol && (orig_gstate != IDNGS_IGNORE)) {
5213 		IDN_GLOCK_EXCL();
5214 		cmn_err(CE_WARN,
5215 			"IDN: Local domain restoring original state %s(%d)",
5216 			idngs_str[orig_gstate], (int)orig_gstate);
5217 		IDN_GSTATE_TRANSITION(orig_gstate);
5218 		orig_gstate = IDNGS_IGNORE;
5219 		IDN_GUNLOCK();
5220 	}
5221 	/*
5222 	 * Just requested AWOL.
5223 	 */
5224 	if (num == 0)
5225 		return (0);
5226 	/*
5227 	 * Default READ only.
5228 	 */
5229 	ibuf = (char *)kmem_alloc(blksize, KM_SLEEP);
5230 	if (rw == 1) {
5231 		/*
5232 		 * WRITE only.
5233 		 */
5234 		obuf = ibuf;
5235 		ibuf = NULL;
5236 	} else if (rw == 2) {
5237 		/*
5238 		 * READ/WRITE.
5239 		 */
5240 		obuf = (char *)kmem_alloc(blksize, KM_SLEEP);
5241 		for (segp = &ktextseg; segp; segp = AS_SEGNEXT(&kas, segp)) {
5242 			if (segp->s_size >= blksize)
5243 				break;
5244 		}
5245 		if (segp == NULL) {
5246 			cmn_err(CE_WARN,
5247 				"IDN: blksize (%d) too large", blksize);
5248 			return (EINVAL);
5249 		}
5250 		bcopy(segp->s_base, obuf, blksize);
5251 	}
5252 
5253 	mutex_init(&slock, NULL, MUTEX_DEFAULT, NULL);
5254 	cv_init(&scv, NULL, CV_DEFAULT, NULL);
5255 
5256 	cmn_err(CE_NOTE,
5257 		"IDN: starting %s of %d blocks of %d bytes each...",
5258 		(rw == 1) ? "W-ONLY" : (rw == 2) ? "RW" : "R-ONLY",
5259 		num, blksize);
5260 
5261 	for (n = 0; n < num; n++) {
5262 		uint_t	rpos;
5263 
5264 		if ((hi_off - lo_off) > blksize)
5265 			rpos = RANDOM(lo_off, (hi_off - blksize));
5266 		else
5267 			rpos = lo_off;
5268 		smraddr = IDN_OFFSET2ADDR(rpos);
5269 
5270 		cpuid = (int)CPU->cpu_id;
5271 #ifdef DEBUG
5272 		idx = rw_index[cpuid]++ % RW_HISTORY;
5273 		rw_history[cpuid][idx] = rpos;
5274 #endif /* DEBUG */
5275 
5276 		switch (rw) {
5277 		case 0:
5278 			bcopy(smraddr, ibuf, blksize);
5279 			break;
5280 		case 1:
5281 			bcopy(obuf, smraddr, blksize);
5282 			break;
5283 		case 2:
5284 			if (n & 1)
5285 				bcopy(obuf, smraddr, blksize);
5286 			else
5287 				bcopy(smraddr, ibuf, blksize);
5288 			break;
5289 		default:
5290 			break;
5291 		}
5292 		if (!(n % 1000)) {
5293 			int	rv;
5294 
5295 			mutex_enter(&slock);
5296 			rv = cv_timedwait_sig(&scv, &slock, lbolt+hz);
5297 			mutex_exit(&slock);
5298 			if (rv == 0)
5299 				break;
5300 		}
5301 	}
5302 
5303 	cv_destroy(&scv);
5304 	mutex_destroy(&slock);
5305 
5306 	if (ibuf)
5307 		kmem_free(ibuf, blksize);
5308 	if (obuf)
5309 		kmem_free(obuf, blksize);
5310 
5311 	return (0);
5312 }
5313 
5314 void
5315 inum2str(uint_t inum, char str[])
5316 {
5317 	uint_t	acknack;
5318 
5319 	str[0] = '\0';
5320 
5321 	acknack = (inum & IDNP_ACKNACK_MASK);
5322 	inum &= ~IDNP_ACKNACK_MASK;
5323 
5324 	if (!inum && !acknack) {
5325 		strcpy(str, idnm_str[0]);
5326 		return;
5327 	}
5328 
5329 	if (inum == 0) {
5330 		strcpy(str, (acknack & IDNP_ACK) ? "ack" : "nack");
5331 	} else {
5332 		if (inum < IDN_NUM_MSGTYPES)
5333 			strcpy(str, idnm_str[inum]);
5334 		else
5335 			sprintf(str, "0x%x?", inum);
5336 		if (acknack) {
5337 			if (acknack & IDNP_ACK)
5338 				strcat(str, "+ack");
5339 			else
5340 				strcat(str, "+nack");
5341 		}
5342 	}
5343 }
5344 
5345 boardset_t
5346 cpuset2boardset(cpuset_t portset)
5347 {
5348 	register int		c;
5349 	register boardset_t	bset;
5350 
5351 	bset = 0;
5352 	for (c = 0; c < NCPU; )
5353 		if (CPU_IN_SET(portset, c)) {
5354 			BOARDSET_ADD(bset, CPUID_TO_BOARDID(c));
5355 			c = (c + 4) & ~3;
5356 		} else {
5357 			c++;
5358 		}
5359 
5360 	return (bset);
5361 }
5362 
5363 void
5364 cpuset2str(cpuset_t cset, char buffer[])
5365 {
5366 	register int	c, n;
5367 
5368 	buffer[0] = '\0';
5369 	for (c = n = 0; c < NCPU; c++) {
5370 		if (!CPU_IN_SET(cset, c))
5371 			continue;
5372 #ifdef DEBUG
5373 		if (strlen(buffer) >= _DSTRLEN) {
5374 			PR_PROTO("************* WARNING WARNING WARNING\n");
5375 			PR_PROTO("cpuset2str(cpu = %d) buffer "
5376 				"OVERFLOW <<<<<<\n", c);
5377 			PR_PROTO("*******************************\n");
5378 			(void) sprintf(&buffer[_DSTRLEN-6], "*OVER");
5379 			return;
5380 		}
5381 #endif /* DEBUG */
5382 		if (n == 0)
5383 			(void) sprintf(buffer, "%d", c);
5384 		else
5385 			(void) sprintf(buffer, "%s, %d", buffer, c);
5386 		n++;
5387 	}
5388 }
5389 
5390 void
5391 domainset2str(domainset_t dset, char buffer[])
5392 {
5393 	/*
5394 	 * Since domainset_t and boardset_t are the
5395 	 * same (max = MAX_DOMAINS = MAX_BOARDS) we
5396 	 * can just overload boardset2str().
5397 	 */
5398 	mask2str((uint_t)dset, buffer, MAX_DOMAINS);
5399 }
5400 
5401 void
5402 boardset2str(boardset_t bset, char buffer[])
5403 {
5404 	mask2str((uint_t)bset, buffer, MAX_BOARDS);
5405 }
5406 
5407 void
5408 mask2str(uint_t mask, char buffer[], int maxnum)
5409 {
5410 	int	n, i;
5411 
5412 	buffer[0] = '\0';
5413 	for (i = n = 0; i < maxnum; i++) {
5414 		if ((mask & (1 << i)) == 0)
5415 			continue;
5416 		if (n == 0)
5417 			(void) sprintf(buffer, "%d", i);
5418 		else
5419 			(void) sprintf(buffer, "%s, %d", buffer, i);
5420 		n++;
5421 	}
5422 }
5423 
5424 int
5425 idnxdc(int domid, idn_msgtype_t *mtp,
5426 	uint_t arg1, uint_t arg2,
5427 	uint_t arg3, uint_t arg4)
5428 {
5429 	int		rv, cpuid, tcpuid;
5430 	uint_t		cookie;
5431 	uint64_t	pdata;
5432 	uint64_t	dmv_word0, dmv_word1, dmv_word2;
5433 	idn_domain_t	*dp = &idn_domain[domid];
5434 	extern kmutex_t	xc_sys_mutex;
5435 	extern int	xc_spl_enter[];
5436 	procname_t	proc = "idnxdc";
5437 
5438 
5439 	if (idn_snoop) {
5440 		int	bd;
5441 		STRING(str);
5442 		STRING(mstr);
5443 		STRING(astr);
5444 
5445 		INUM2STR(mtp->mt_mtype, mstr);
5446 		if ((mtp->mt_mtype & IDNP_MSGTYPE_MASK) == 0) {
5447 			INUM2STR(arg1, astr);
5448 			sprintf(str, "%s/%s", mstr, astr);
5449 		} else {
5450 			strcpy(str, mstr);
5451 		}
5452 		if (dp->dcpu == IDN_NIL_DCPU)
5453 			bd = -1;
5454 		else
5455 			bd = CPUID_TO_BOARDID(dp->dcpu);
5456 		SNOOP_IDN(0, str, bd, arg1, arg2, arg3, arg4);
5457 	}
5458 
5459 	/*
5460 	 * For NEGO messages we send the remote domain the cookie we
5461 	 * expect it to use in subsequent messages that it sends
5462 	 * to us (dcookie_recv).
5463 	 * For other messages, we must use the cookie that the
5464 	 * remote domain assigned to us for sending (dcookie_send).
5465 	 */
5466 	if ((mtp->mt_mtype & IDNP_MSGTYPE_MASK) == IDNP_NEGO)
5467 		cookie = IDN_MAKE_COOKIE(dp->dcookie_recv, mtp->mt_cookie);
5468 	else
5469 		cookie = IDN_MAKE_COOKIE(dp->dcookie_send, mtp->mt_cookie);
5470 
5471 	pdata = IDN_MAKE_PDATA(mtp->mt_mtype, mtp->mt_atype, cookie);
5472 
5473 	dmv_word0 = DMV_MAKE_DMV(idn.intr.dmv_inum, pdata);
5474 	dmv_word1 = ((uint64_t)arg1 << 32) | (uint64_t)arg2;
5475 	dmv_word2 = ((uint64_t)arg3 << 32) | (uint64_t)arg4;
5476 
5477 	ASSERT((dp->dcpu != IDN_NIL_DCPU) ||
5478 		(dp->dcpu_last != IDN_NIL_DCPU));
5479 
5480 	tcpuid = (dp->dcpu == IDN_NIL_DCPU) ?
5481 			dp->dcpu_last : dp->dcpu;
5482 
5483 	if (tcpuid == IDN_NIL_DCPU) {
5484 		PR_PROTO("%s:%d: cpu/cpu_last == NIL_DCPU\n",
5485 			proc, domid);
5486 		return (-1);
5487 	}
5488 
5489 	mutex_enter(&xc_sys_mutex);
5490 	cpuid = (int)CPU->cpu_id;
5491 	xc_spl_enter[cpuid] = 1;
5492 
5493 	idnxf_init_mondo(dmv_word0, dmv_word1, dmv_word2);
5494 
5495 	rv = idnxf_send_mondo(STARFIRE_UPAID2HWMID(tcpuid));
5496 
5497 	xc_spl_enter[cpuid] = 0;
5498 	mutex_exit(&xc_sys_mutex);
5499 
5500 	return (rv);
5501 }
5502 
5503 void
5504 idnxdc_broadcast(domainset_t domset, idn_msgtype_t *mtp,
5505 		uint_t arg1, uint_t arg2,
5506 		uint_t arg3, uint_t arg4)
5507 {
5508 	int	d;
5509 
5510 	for (d = 0; d < MAX_DOMAINS; d++) {
5511 		idn_domain_t	*dp;
5512 
5513 		if (!DOMAIN_IN_SET(domset, d))
5514 			continue;
5515 
5516 		dp = &idn_domain[d];
5517 		if (dp->dcpu == IDN_NIL_DCPU)
5518 			continue;
5519 
5520 		(void) IDNXDC(d, mtp, arg1, arg2, arg3, arg4);
5521 	}
5522 }
5523 
5524 #define	PROM_SMRSIZE	0x1
5525 #define	PROM_SMRADDR	0x2
5526 #define	PROM_SMRPROPS	(PROM_SMRSIZE | PROM_SMRADDR)
5527 /*
5528  * Locate the idn-smr-size property to determine the size of the SMR
5529  * region for the SSI.  Value inherently enables/disables SSI capability.
5530  */
5531 static int
5532 idn_prom_getsmr(uint_t *smrsz, uint64_t *paddrp, uint64_t *sizep)
5533 {
5534 	pnode_t		nodeid;
5535 	int		found = 0;
5536 	int		len;
5537 	uint_t		smrsize = 0;
5538 	uint64_t	obpaddr, obpsize;
5539 	struct smraddr {
5540 		uint32_t	hi_addr;
5541 		uint32_t	lo_addr;
5542 		uint32_t	hi_size;
5543 		uint32_t	lo_size;
5544 	} smraddr;
5545 	procname_t	proc = "idn_prom_getsmr";
5546 
5547 	bzero(&smraddr, sizeof (smraddr));
5548 	/*
5549 	 * idn-smr-size is a property of the "memory" node and
5550 	 * is defined in megabytes.
5551 	 */
5552 	nodeid = prom_finddevice("/memory");
5553 
5554 	if (nodeid != OBP_NONODE) {
5555 		len = prom_getproplen(nodeid, IDN_PROP_SMRSIZE);
5556 		if (len == sizeof (smrsize)) {
5557 			(void) prom_getprop(nodeid, IDN_PROP_SMRSIZE,
5558 				    (caddr_t)&smrsize);
5559 			found |= PROM_SMRSIZE;
5560 		}
5561 		len = prom_getproplen(nodeid, IDN_PROP_SMRADDR);
5562 		if (len  == sizeof (smraddr)) {
5563 			(void) prom_getprop(nodeid, IDN_PROP_SMRADDR,
5564 					(caddr_t)&smraddr);
5565 			found |= PROM_SMRADDR;
5566 		}
5567 	}
5568 
5569 	if (found != PROM_SMRPROPS) {
5570 		if ((found & PROM_SMRSIZE) == 0)
5571 			cmn_err(CE_WARN,
5572 				"IDN: 136: \"%s\" property not found, "
5573 				"disabling IDN",
5574 				IDN_PROP_SMRSIZE);
5575 		if (smrsize && ((found & PROM_SMRADDR) == 0))
5576 			cmn_err(CE_WARN,
5577 				"IDN: 136: \"%s\" property not found, "
5578 				"disabling IDN",
5579 				IDN_PROP_SMRADDR);
5580 		return (-1);
5581 	}
5582 
5583 	if (smrsize == 0) {
5584 		PR_SMR("%s: IDN DISABLED (idn_smr_size = 0)\n", proc);
5585 		cmn_err(CE_NOTE, "!IDN: 137: SMR size is 0, disabling IDN");
5586 
5587 	} else if (smrsize > IDN_SMR_MAXSIZE) {
5588 		PR_SMR("%s: IDN DISABLED (idn_smr_size too big %d > %d MB)\n",
5589 			proc, smrsize, IDN_SMR_MAXSIZE);
5590 		cmn_err(CE_WARN,
5591 			"!IDN: 138: SMR size (%dMB) is too big (max = %dMB), "
5592 			"disabling IDN",
5593 			smrsize, IDN_SMR_MAXSIZE);
5594 		smrsize = 0;
5595 	} else {
5596 		*smrsz = smrsize;
5597 		found &= ~PROM_SMRSIZE;
5598 	}
5599 
5600 	obpaddr = ((uint64_t)smraddr.hi_addr << 32) |
5601 			(uint64_t)smraddr.lo_addr;
5602 	obpsize = ((uint64_t)smraddr.hi_size << 32) |
5603 			(uint64_t)smraddr.lo_size;
5604 
5605 	if (obpsize == 0) {
5606 		if (smrsize > 0) {
5607 			cmn_err(CE_WARN, "!IDN: 139: OBP region for "
5608 				"SMR is 0 length");
5609 		}
5610 	} else if (obpsize < (uint64_t)MB2B(smrsize)) {
5611 		cmn_err(CE_WARN,
5612 			"!IDN: 140: OBP region (%ld B) smaller "
5613 			"than requested size (%ld B)",
5614 			obpsize, MB2B(smrsize));
5615 	} else if ((obpaddr & ((uint64_t)IDN_SMR_ALIGN - 1)) != 0) {
5616 		cmn_err(CE_WARN,
5617 			"!IDN: 141: OBP region (0x%lx) not on (0x%x) "
5618 			"boundary", obpaddr, IDN_SMR_ALIGN);
5619 	} else {
5620 		*sizep = obpsize;
5621 		*paddrp = obpaddr;
5622 		found &= ~PROM_SMRADDR;
5623 	}
5624 
5625 	return (found ? -1 : 0);
5626 }
5627 
5628 void
5629 idn_init_autolink()
5630 {
5631 	idnsb_event_t	*sbp;
5632 	procname_t	proc = "idn_init_autolink";
5633 
5634 	mutex_enter(&idn.idnsb_mutex);
5635 	if ((sbp = idn.idnsb_eventp) == NULL) {
5636 		PR_PROTO("%s: IDN private sigb (event) area is NULL\n", proc);
5637 		mutex_exit(&idn.idnsb_mutex);
5638 		return;
5639 	}
5640 
5641 	PR_PROTO("%s: marking domain IDN ready.\n", proc);
5642 
5643 	bzero(sbp, sizeof (*sbp));
5644 
5645 	sbp->idn_version = (uchar_t)idn.version;
5646 	SSIEVENT_SET(sbp, SSIEVENT_BOOT, 0);
5647 	(void) strncpy(sbp->idn_cookie_str, SSIEVENT_COOKIE,
5648 						SSIEVENT_COOKIE_LEN);
5649 	mutex_exit(&idn.idnsb_mutex);
5650 }
5651 
5652 void
5653 idn_deinit_autolink()
5654 {
5655 	idnsb_event_t	*sbp;
5656 	procname_t	proc = "idn_deinit_autolink";
5657 
5658 	mutex_enter(&idn.idnsb_mutex);
5659 	if ((sbp = idn.idnsb_eventp) == NULL) {
5660 		PR_PROTO("%s: IDN private sigb (event) area is NULL\n", proc);
5661 		mutex_exit(&idn.idnsb_mutex);
5662 		return;
5663 	}
5664 
5665 	PR_PROTO("%s: marking domain IDN unavailable.\n", proc);
5666 
5667 	sbp->idn_version = (uchar_t)idn.version;
5668 	SSIEVENT_CLEAR(sbp, SSIEVENT_BOOT, 0);
5669 	(void) strncpy(sbp->idn_cookie_str, SSIEVENT_COOKIE,
5670 						SSIEVENT_COOKIE_LEN);
5671 	mutex_exit(&idn.idnsb_mutex);
5672 }
5673 
5674 void
5675 _make64cpumask(cpuset_t *csetp, uint_t upper, uint_t lower)
5676 {
5677 	int	c;
5678 
5679 	CPUSET_ZERO(*csetp);
5680 
5681 	for (c = 0; c < 32; c++) {
5682 		if (lower & (1 << c)) {
5683 			CPUSET_ADD(*csetp, c);
5684 		}
5685 		if (upper & (1 << (c + 32))) {
5686 			CPUSET_ADD(*csetp, c + 32);
5687 		}
5688 	}
5689 }
5690 
5691 uint_t
5692 _lower32cpumask(cpuset_t cset)
5693 {
5694 	int	c;
5695 	uint_t	set = 0;
5696 
5697 	for (c = 0; c < 32; c++)
5698 		if (CPU_IN_SET(cset, c))
5699 			set |= 1 << c;
5700 
5701 	return (set);
5702 }
5703 
5704 uint_t
5705 _upper32cpumask(cpuset_t cset)
5706 {
5707 	int	c;
5708 	uint_t	set = 0;
5709 
5710 	for (c = 32; c < NCPU; c++)
5711 		if (CPU_IN_SET(cset, c))
5712 			set |= 1 << (c - 32);
5713 
5714 	return (set);
5715 }
5716 
5717 #ifdef DEBUG
5718 int
5719 debug_idnxdc(char *f, int domid, idn_msgtype_t *mtp,
5720 		uint_t a1, uint_t a2, uint_t a3, uint_t a4)
5721 {
5722 	idn_domain_t	*dp = &idn_domain[domid];
5723 	int		rv, cpuid, bd;
5724 	static int	xx = 0;
5725 	STRING(str);
5726 	STRING(mstr);
5727 	STRING(astr);
5728 
5729 	xx++;
5730 	INUM2STR(mtp->mt_mtype, mstr);
5731 	if ((mtp->mt_mtype & IDNP_MSGTYPE_MASK) == 0) {
5732 		INUM2STR(a1, astr);
5733 		sprintf(str, "%s/%s", mstr, astr);
5734 	} else {
5735 		strcpy(str, mstr);
5736 	}
5737 
5738 	if ((cpuid = dp->dcpu) == IDN_NIL_DCPU)
5739 		bd = -1;
5740 	else
5741 		bd = CPUID_TO_BOARDID(cpuid);
5742 
5743 	SNOOP_IDN(0, str, bd, a1, a2, a3, a4);
5744 
5745 	PR_XDC("%s:%d:%d SENT: scpu = %d, msg = 0x%x(%s)\n",
5746 		f, domid, xx, cpuid, mtp->mt_mtype, str);
5747 	PR_XDC("%s:%d:%d S-DATA: a1 = 0x%x, a2 = 0x%x\n",
5748 		f, domid, xx, a1, a2);
5749 	PR_XDC("%s:%d:%d S-DATA: a3 = 0x%x, a4 = 0x%x\n",
5750 		f, domid, xx, a3, a4);
5751 
5752 	rv = idnxdc(domid, mtp, a1, a2, a3, a4);
5753 	if (rv != 0) {
5754 		PR_XDC("%s:%d:%d: WARNING: idnxdc(cpu %d) FAILED\n",
5755 			f, domid, xx, cpuid);
5756 	}
5757 
5758 	return (rv);
5759 }
5760 
5761 caddr_t
5762 _idn_getstruct(char *structname, int size)
5763 {
5764 	caddr_t		ptr;
5765 	procname_t	proc = "GETSTRUCT";
5766 
5767 	ptr = kmem_zalloc(size, KM_SLEEP);
5768 
5769 	PR_ALLOC("%s: ptr 0x%p, struct(%s), size = %d\n",
5770 		proc, ptr, structname, size);
5771 
5772 	return (ptr);
5773 }
5774 
5775 void
5776 _idn_freestruct(caddr_t ptr, char *structname, int size)
5777 {
5778 	procname_t	proc = "FREESTRUCT";
5779 
5780 	PR_ALLOC("%s: ptr 0x%p, struct(%s), size = %d\n",
5781 		proc, ptr, structname, size);
5782 
5783 	ASSERT(ptr != NULL);
5784 	kmem_free(ptr, size);
5785 }
5786 #endif /* DEBUG */
5787