xref: /titanic_50/usr/src/uts/sun4v/io/vnet_dds.c (revision 67318e4a54c292d543e6b077199ce492b3d3a049)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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/modctl.h>
30 #include <sys/prom_plat.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/sunndi.h>
34 #include <sys/ndi_impldefs.h>
35 #include <sys/ddi_impldefs.h>
36 #include <sys/ethernet.h>
37 #include <sys/machsystm.h>
38 #include <sys/hypervisor_api.h>
39 #include <sys/mach_descrip.h>
40 #include <sys/drctl.h>
41 #include <sys/dr_util.h>
42 #include <sys/mac.h>
43 #include <sys/vnet.h>
44 #include <sys/vnet_mailbox.h>
45 #include <sys/vnet_common.h>
46 #include <sys/hsvc.h>
47 
48 
49 #define	VDDS_MAX_RANGES		6	/* 6 possible VRs */
50 #define	VDDS_MAX_VRINTRS	8	/* limited to 8 intrs/VR */
51 #define	VDDS_MAX_INTR_NUM	64	/* 0-63 or valid */
52 
53 #define	VDDS_INO_RANGE_START(x) (x * VDDS_MAX_VRINTRS)
54 #define	HVCOOKIE(c)	((c) & 0xFFFFFFFFF)
55 #define	NIUCFGHDL(c)	((c) >> 32)
56 
57 
58 /* For "ranges" property */
59 typedef struct vdds_ranges {
60 	uint32_t child_hi;
61 	uint32_t child_lo;
62 	uint32_t parent_hi;
63 	uint32_t parent_lo;
64 	uint32_t size_hi;
65 	uint32_t size_lo;
66 } vdds_ranges_t;
67 
68 /* For "reg" property */
69 typedef struct vdds_reg {
70 	uint32_t addr_hi;
71 	uint32_t addr_lo;
72 	uint32_t size_hi;
73 	uint32_t size_lo;
74 } vdds_reg_t;
75 
76 /* For ddi callback argument */
77 typedef struct vdds_cb_arg {
78 	dev_info_t *dip;
79 	uint64_t cookie;
80 	uint64_t macaddr;
81 } vdds_cb_arg_t;
82 
83 
84 /* Functions exported to other files */
85 void vdds_mod_init(void);
86 void vdds_mod_fini(void);
87 int vdds_init(vnet_t *vnetp);
88 void vdds_cleanup(vnet_t *vnetp);
89 void vdds_process_dds_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg);
90 void vdds_cleanup_hybrid_res(vnet_t *vnetp);
91 
92 /* Support functions to create/destory Hybrid device */
93 static dev_info_t *vdds_create_niu_node(uint64_t cookie, uint64_t macaddr);
94 static int vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie);
95 static dev_info_t *vdds_create_new_node(uint64_t cookie, uint64_t macddr,
96     dev_info_t *pdip, int (*new_node_func)(dev_info_t *dip,
97     void *arg, uint_t flags));
98 static int vdds_new_nexus_node(dev_info_t *dip, void *arg, uint_t flags);
99 static int vdds_new_niu_node(dev_info_t *dip, void *arg, uint_t flags);
100 static dev_info_t *vdds_find_node(uint64_t cookie, dev_info_t *sdip,
101 	int (*match_func)(dev_info_t *dip, void *arg));
102 static int vdds_match_niu_nexus(dev_info_t *dip, void *arg);
103 static int vdds_match_niu_node(dev_info_t *dip, void *arg);
104 static int vdds_get_interrupts(uint64_t cookie, int ino_range,
105     int *intrs, int *nintr);
106 
107 /* DDS message processing related functions */
108 static void vdds_process_dds_msg_task(void *arg);
109 static int vdds_send_dds_resp_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg, int ack);
110 static int vdds_send_dds_rel_msg(vnet_t *vnetp);
111 static void vdds_release_range_prop(dev_info_t *nexus_dip, uint64_t cookie);
112 
113 /* Functions imported from other files */
114 extern int vnet_send_dds_msg(vnet_t *vnetp, void *dmsg);
115 
116 /* HV functions that are used in this file */
117 extern uint64_t vdds_hv_niu_vr_getinfo(uint32_t hvcookie,
118     uint64_t *real_start, uint64_t *size);
119 extern uint64_t vdds_hv_niu_vr_get_txmap(uint32_t hvcookie, uint64_t *dma_map);
120 extern uint64_t vdds_hv_niu_vr_get_rxmap(uint32_t hvcookie, uint64_t *dma_map);
121 extern uint64_t vdds_hv_niu_vrtx_set_ino(uint32_t cookie, uint64_t vch_idx,
122     uint32_t ino);
123 extern uint64_t vdds_hv_niu_vrrx_set_ino(uint32_t cookie, uint64_t vch_idx,
124     uint32_t ino);
125 
126 
127 #ifdef DEBUG
128 
129 extern int vnet_dbglevel;
130 
131 static void
132 debug_printf(const char *fname, void *arg,  const char *fmt, ...)
133 {
134 	char    buf[512];
135 	va_list ap;
136 	char    *bufp = buf;
137 	vnet_dds_info_t *vdds = arg;
138 
139 	if (vdds != NULL) {
140 		(void) sprintf(bufp, "vnet%d: %s: ",
141 		    vdds->vnetp->instance, fname);
142 	} else {
143 		(void) sprintf(bufp, "%s: ", fname);
144 	}
145 	bufp += strlen(bufp);
146 	va_start(ap, fmt);
147 	(void) vsprintf(bufp, fmt, ap);
148 	va_end(ap);
149 	cmn_err(CE_CONT, "%s\n", buf);
150 }
151 #endif
152 
153 /*
154  * Hypervisor N2/NIU services information.
155  */
156 static hsvc_info_t niu_hsvc = {
157 	HSVC_REV_1, NULL, HSVC_GROUP_NIU, 1, 1, "vnet_dds"
158 };
159 
160 boolean_t vdds_hv_hio_capable = B_FALSE;
161 
162 /*
163  * vdds_mod_init -- one time initialization.
164  */
165 void
166 vdds_mod_init(void)
167 {
168 	int rv;
169 	uint64_t minor;
170 
171 	rv = hsvc_register(&niu_hsvc, &minor);
172 	/*
173 	 * Only HV version 1.1 is capable of NIU Hybrid IO.
174 	 */
175 	if ((rv == 0) && (minor == 1)) {
176 		vdds_hv_hio_capable = B_TRUE;
177 	}
178 	DBG1(NULL, "HV HIO capable");
179 }
180 
181 /*
182  * vdds_mod_fini -- one time cleanup.
183  */
184 void
185 vdds_mod_fini(void)
186 {
187 	(void) hsvc_unregister(&niu_hsvc);
188 }
189 
190 /*
191  * vdds_init -- vnet instance related DDS related initialization.
192  */
193 int
194 vdds_init(vnet_t *vnetp)
195 {
196 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
197 	char		qname[TASKQ_NAMELEN];
198 
199 	vdds->vnetp = vnetp;
200 	DBG1(vdds, "Initializing..");
201 	(void) snprintf(qname, TASKQ_NAMELEN, "vdds_taskq%d", vnetp->instance);
202 	if ((vdds->dds_taskqp = ddi_taskq_create(vnetp->dip, qname, 1,
203 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
204 		cmn_err(CE_WARN, "!vnet%d: Unable to create DDS task queue",
205 		    vnetp->instance);
206 		return (ENOMEM);
207 	}
208 	mutex_init(&vdds->lock, NULL, MUTEX_DRIVER, NULL);
209 	return (0);
210 }
211 
212 /*
213  * vdds_cleanup -- vnet instance related cleanup.
214  */
215 void
216 vdds_cleanup(vnet_t *vnetp)
217 {
218 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
219 
220 	DBG1(vdds, "Cleanup...");
221 	/* Cleanup/destroy any hybrid resouce that exists */
222 	vdds_cleanup_hybrid_res(vnetp);
223 
224 	/* taskq_destroy will wait for all taskqs to complete */
225 	ddi_taskq_destroy(vdds->dds_taskqp);
226 	vdds->dds_taskqp = NULL;
227 	mutex_destroy(&vdds->lock);
228 	DBG1(vdds, "Cleanup complete");
229 }
230 
231 /*
232  * vdds_cleanup_hybrid_res -- Cleanup Hybrid resource.
233  */
234 void
235 vdds_cleanup_hybrid_res(vnet_t *vnetp)
236 {
237 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
238 
239 	DBG1(vdds, "Hybrid device cleanup...");
240 	mutex_enter(&vdds->lock);
241 	if (vdds->task_flags == VNET_DDS_TASK_ADD_SHARE) {
242 		/*
243 		 * Task for ADD_SHARE is pending, simply
244 		 * cleanup the flags, the task will quit without
245 		 * any changes.
246 		 */
247 		vdds->task_flags = 0;
248 		DBG2(vdds, "Task for ADD is pending, clean flags only");
249 	} else if ((vdds->hio_dip != NULL) && (vdds->task_flags == 0)) {
250 		/*
251 		 * There is no task pending and a hybrid device
252 		 * is present, so dispatch a task to release the share.
253 		 */
254 		vdds->task_flags = VNET_DDS_TASK_REL_SHARE;
255 		(void) ddi_taskq_dispatch(vdds->dds_taskqp,
256 		    vdds_process_dds_msg_task, vnetp, DDI_NOSLEEP);
257 		DBG2(vdds, "Dispatched a task to destroy HIO device");
258 	}
259 	/*
260 	 * Other possible cases include either DEL_SHARE or
261 	 * REL_SHARE as pending. In that case, there is nothing
262 	 * to do as a task is already pending to do the cleanup.
263 	 */
264 	mutex_exit(&vdds->lock);
265 	DBG1(vdds, "Hybrid device cleanup complete");
266 }
267 
268 /*
269  * vdds_process_dds_msg -- Process a DDS message.
270  */
271 void
272 vdds_process_dds_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg)
273 {
274 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
275 	int rv;
276 
277 	DBG1(vdds, "DDS message received...");
278 
279 	if (dmsg->dds_class != DDS_VNET_NIU) {
280 		DBG2(vdds, "Invalid class send NACK");
281 		(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
282 		return;
283 	}
284 	mutex_enter(&vdds->lock);
285 	switch (dmsg->dds_subclass) {
286 	case DDS_VNET_ADD_SHARE:
287 		DBG2(vdds, "DDS_VNET_ADD_SHARE message...");
288 		if ((vdds->task_flags != 0) || (vdds->hio_dip != NULL)) {
289 			/*
290 			 * Either a task is already pending or
291 			 * a hybrid device already exists.
292 			 */
293 			DWARN(vdds, "NACK: Already pending DDS task");
294 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
295 			mutex_exit(&vdds->lock);
296 			return;
297 		}
298 		vdds->task_flags = VNET_DDS_TASK_ADD_SHARE;
299 		bcopy(dmsg, &vnetp->vdds_info.dmsg, sizeof (vio_dds_msg_t));
300 		DBG2(vdds, "Dispatching task for ADD_SHARE");
301 		rv = ddi_taskq_dispatch(vdds->dds_taskqp,
302 		    vdds_process_dds_msg_task, vnetp, DDI_NOSLEEP);
303 		if (rv != 0) {
304 			/* Send NACK */
305 			DBG2(vdds, "NACK: Failed to dispatch task");
306 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
307 			vdds->task_flags = 0;
308 		}
309 		break;
310 
311 	case DDS_VNET_DEL_SHARE:
312 		DBG2(vdds, "DDS_VNET_DEL_SHARE message...");
313 		if (vdds->task_flags == VNET_DDS_TASK_ADD_SHARE) {
314 			/*
315 			 * ADD_SHARE task still pending, simply clear
316 			 * task falgs and ACK.
317 			 */
318 			DBG2(vdds, "ACK:ADD_SHARE task still pending");
319 			vdds->task_flags = 0;
320 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE);
321 			mutex_exit(&vdds->lock);
322 			return;
323 		}
324 		if ((vdds->task_flags == 0) && (vdds->hio_dip == NULL)) {
325 			/* Send NACK */
326 			DBG2(vdds, "NACK:No HIO device exists");
327 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
328 			mutex_exit(&vdds->lock);
329 			return;
330 		}
331 		vdds->task_flags = VNET_DDS_TASK_DEL_SHARE;
332 		bcopy(dmsg, &vdds->dmsg, sizeof (vio_dds_msg_t));
333 		DBG2(vdds, "Dispatching DEL_SHARE task");
334 		rv = ddi_taskq_dispatch(vdds->dds_taskqp,
335 		    vdds_process_dds_msg_task, vnetp, DDI_NOSLEEP);
336 		if (rv != 0) {
337 			/* Send NACK */
338 			DBG2(vdds, "NACK: failed to dispatch task");
339 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
340 			vdds->task_flags = 0;
341 		}
342 		break;
343 	case DDS_VNET_REL_SHARE:
344 		DBG2(vdds, "Reply for REL_SHARE reply=%d",
345 		    dmsg->tag.vio_subtype);
346 		break;
347 	default:
348 		DWARN(vdds, "Discarding Unknown DDS message");
349 		break;
350 	}
351 	mutex_exit(&vdds->lock);
352 }
353 
354 /*
355  * vdds_process_dds_msg_task -- Called from a taskq to process the
356  *	DDS message.
357  */
358 static void
359 vdds_process_dds_msg_task(void *arg)
360 {
361 	vnet_t		*vnetp = arg;
362 	vnet_dds_info_t	*vdds = &vnetp->vdds_info;
363 	vio_dds_msg_t	*dmsg = &vdds->dmsg;
364 	dev_info_t	*dip;
365 	uint64_t	hio_cookie;
366 	int		rv;
367 
368 	DBG1(vdds, "DDS task started...");
369 	mutex_enter(&vdds->lock);
370 	switch (vdds->task_flags) {
371 	case VNET_DDS_TASK_ADD_SHARE:
372 		DBG2(vdds, "ADD_SHARE task...");
373 		hio_cookie = dmsg->msg.share_msg.cookie;
374 		dip = vdds_create_niu_node(hio_cookie,
375 		    dmsg->msg.share_msg.macaddr);
376 		if (dip == NULL) {
377 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
378 			DERR(vdds, "Failed to create HIO node");
379 		} else {
380 			vdds->hio_dip = dip;
381 			vdds->hio_cookie = hio_cookie;
382 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE);
383 			/* DERR used only print by default */
384 			DERR(vdds, "Successfully created HIO node");
385 		}
386 		break;
387 
388 	case VNET_DDS_TASK_DEL_SHARE:
389 		DBG2(vdds, "DEL_SHARE task...");
390 		if (vnetp->vdds_info.hio_dip == NULL) {
391 			DBG2(vdds, "NACK: No HIO device destroy");
392 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
393 		} else {
394 			rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip,
395 			    vdds->hio_cookie);
396 			if (rv == 0) {
397 				/* use DERR to print by default */
398 				DERR(vdds, "Successfully destroyed"
399 				    " Hybrid node");
400 			} else {
401 				cmn_err(CE_WARN, "vnet%d:Failed to "
402 				    "destroy Hybrid node", vnetp->instance);
403 			}
404 			/* TODO: send ACK even for failure? */
405 			DBG2(vdds, "ACK: HIO device destroyed");
406 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE);
407 			vdds->hio_dip = 0;
408 			vdds->hio_cookie = 0;
409 		}
410 		break;
411 	case VNET_DDS_TASK_REL_SHARE:
412 		DBG2(vdds, "REL_SHARE task...");
413 		if (vnetp->vdds_info.hio_dip != NULL) {
414 			rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip,
415 			    vdds->hio_cookie);
416 			if (rv == 0) {
417 				DERR(vdds, "Successfully destroyed "
418 				    "Hybrid node");
419 			} else {
420 				cmn_err(CE_WARN, "vnet%d:Failed to "
421 				    "destroy HIO node", vnetp->instance);
422 			}
423 			/* TODO: failure case */
424 			(void) vdds_send_dds_rel_msg(vnetp);
425 			vdds->hio_dip = 0;
426 			vdds->hio_cookie = 0;
427 		}
428 		break;
429 	default:
430 		break;
431 	}
432 	vdds->task_flags = 0;
433 	mutex_exit(&vdds->lock);
434 }
435 
436 /*
437  * vdds_send_dds_rel_msg -- Send a DDS_REL_SHARE message.
438  */
439 static int
440 vdds_send_dds_rel_msg(vnet_t *vnetp)
441 {
442 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
443 	vio_dds_msg_t	vmsg;
444 	dds_share_msg_t	*smsg = &vmsg.msg.share_msg;
445 	int rv;
446 
447 	DBG1(vdds, "Sending DDS_VNET_REL_SHARE message");
448 	vmsg.tag.vio_msgtype = VIO_TYPE_CTRL;
449 	vmsg.tag.vio_subtype = VIO_SUBTYPE_INFO;
450 	vmsg.tag.vio_subtype_env = VIO_DDS_INFO;
451 	/* vio_sid filled by the LDC module */
452 	vmsg.dds_class = DDS_VNET_NIU;
453 	vmsg.dds_subclass = DDS_VNET_REL_SHARE;
454 	vmsg.dds_req_id = (++vdds->dds_req_id);
455 	smsg->macaddr = vnet_macaddr_strtoul(vnetp->curr_macaddr);
456 	smsg->cookie = vdds->hio_cookie;
457 	rv = vnet_send_dds_msg(vnetp, &vmsg);
458 	return (rv);
459 }
460 
461 /*
462  * vdds_send_dds_resp_msg -- Send a DDS response message.
463  */
464 static int
465 vdds_send_dds_resp_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg, int ack)
466 {
467 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
468 	int rv;
469 
470 	DBG1(vdds, "Sending a response mesage=%d", ack);
471 	if (ack == B_TRUE) {
472 		dmsg->tag.vio_subtype = VIO_SUBTYPE_ACK;
473 		dmsg->msg.share_resp_msg.status = DDS_VNET_SUCCESS;
474 	} else {
475 		dmsg->tag.vio_subtype = VIO_SUBTYPE_NACK;
476 		dmsg->msg.share_resp_msg.status = DDS_VNET_FAIL;
477 	}
478 	rv = vnet_send_dds_msg(vnetp, dmsg);
479 	return (rv);
480 }
481 
482 /*
483  * vdds_create_niu_node -- Create NIU Hybrid node. The NIU nexus
484  *	node also created if it doesn't exist already.
485  */
486 dev_info_t *
487 vdds_create_niu_node(uint64_t cookie, uint64_t macaddr)
488 {
489 	dev_info_t *nexus_dip;
490 	dev_info_t *niu_dip;
491 
492 	DBG1(NULL, "Called");
493 
494 	if (vdds_hv_hio_capable == B_FALSE) {
495 		return (NULL);
496 	}
497 	/* Check if the nexus node exists already */
498 	nexus_dip = vdds_find_node(cookie, ddi_root_node(),
499 	    vdds_match_niu_nexus);
500 	if (nexus_dip == NULL) {
501 		/*
502 		 * NIU nexus node not found, so create it now.
503 		 */
504 		nexus_dip = vdds_create_new_node(cookie, macaddr, NULL,
505 		    vdds_new_nexus_node);
506 		if (nexus_dip == NULL) {
507 			return (NULL);
508 		}
509 	}
510 	DBG2(NULL, "nexus_dip = 0x%p", nexus_dip);
511 
512 	/* Check if NIU node exists already before creating one */
513 	niu_dip = vdds_find_node(cookie, nexus_dip,
514 	    vdds_match_niu_node);
515 	if (niu_dip == NULL) {
516 		niu_dip = vdds_create_new_node(cookie, macaddr, nexus_dip,
517 		    vdds_new_niu_node);
518 		if (niu_dip == NULL) {
519 			DWARN(NULL, "create_niu_hio_node returned NULL");
520 		}
521 		/*
522 		 * Do not release the niu_dip to prevent it from
523 		 * auto-detach/unload.
524 		 */
525 		ndi_rele_devi(nexus_dip);
526 		return (niu_dip);
527 
528 	}
529 	DWARN(NULL, "niumx/network node already exists(dip=0x%p)", niu_dip);
530 	ndi_rele_devi(niu_dip);
531 	ndi_rele_devi(nexus_dip);
532 	DBG1(NULL, "returning success");
533 	return (niu_dip);
534 }
535 
536 /*
537  * vdds_destroy_niu_node -- Destroy the NIU node.
538  */
539 int
540 vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie)
541 {
542 	int rv;
543 	dev_info_t *fdip = NULL;
544 	dev_info_t *nexus_dip = ddi_get_parent(niu_dip);
545 
546 
547 	DBG1(NULL, "Called");
548 	ndi_rele_devi(niu_dip);
549 	rv = e_ddi_branch_unconfigure(niu_dip, &fdip, 0);
550 	if (rv != 0) {
551 		DERR(NULL, "Failed to unconfigure niumx/network node dip=0x%p",
552 		    niu_dip);
553 		if (fdip != NULL) {
554 			ndi_rele_devi(fdip);
555 		}
556 		return (EBUSY);
557 	}
558 	rv = e_ddi_branch_destroy(niu_dip, &fdip, 0);
559 	if (rv != 0) {
560 		DERR(NULL, "Failed to destroy niumx/network node dip=0x%p",
561 		    niu_dip);
562 		if (fdip != NULL) {
563 			ndi_rele_devi(fdip);
564 		}
565 		return (EBUSY);
566 	}
567 	/*
568 	 * Cleanup the parent's ranges property set
569 	 * for this Hybrid device.
570 	 */
571 	vdds_release_range_prop(nexus_dip, cookie);
572 	DBG1(NULL, "returning success");
573 	return (0);
574 }
575 
576 /*
577  * vdds_match_niu_nexus -- callback function to verify a node is the
578  *	NIU nexus node.
579  */
580 static int
581 vdds_match_niu_nexus(dev_info_t *dip, void *arg)
582 {
583 	vdds_cb_arg_t	*warg = (vdds_cb_arg_t *)arg;
584 	vdds_reg_t	*reg_p;
585 	char		*name;
586 	uint64_t	hdl;
587 	uint_t		reglen;
588 	int		rv;
589 
590 	if (dip == ddi_root_node()) {
591 		return (DDI_WALK_CONTINUE);
592 	}
593 
594 	name = ddi_node_name(dip);
595 	if (strcmp(name, "niu")  != 0) {
596 		return (DDI_WALK_CONTINUE);
597 	}
598 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
599 	    DDI_PROP_DONTPASS, "reg", (int **)&reg_p, &reglen);
600 	if (rv != DDI_PROP_SUCCESS) {
601 		DWARN(NULL, "Failed to get reg property dip=0x%p", dip);
602 		return (DDI_WALK_CONTINUE);
603 	}
604 
605 	hdl =  reg_p->addr_hi & 0x0FFFFFFF;
606 	ddi_prop_free(reg_p);
607 
608 	DBG2(NULL, "Handle = 0x%lx dip=0x%p", hdl, dip);
609 	if (hdl == NIUCFGHDL(warg->cookie)) {
610 		/* Hold before returning */
611 		ndi_hold_devi(dip);
612 		warg->dip = dip;
613 		DBG2(NULL, "Found dip = 0x%p", dip);
614 		return (DDI_WALK_TERMINATE);
615 	}
616 	return (DDI_WALK_CONTINUE);
617 }
618 
619 /*
620  * vdds_match_niu_node -- callback function to verify a node is the
621  *	NIU Hybrid node.
622  */
623 static int
624 vdds_match_niu_node(dev_info_t *dip, void *arg)
625 {
626 	vdds_cb_arg_t	*warg = (vdds_cb_arg_t *)arg;
627 	char		*name;
628 	vdds_reg_t	*reg_p;
629 	uint_t		reglen;
630 	int		rv;
631 	uint32_t	addr_hi;
632 
633 	name = ddi_node_name(dip);
634 	if (strcmp(name, "network")  != 0) {
635 		return (DDI_WALK_CONTINUE);
636 	}
637 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
638 	    DDI_PROP_DONTPASS, "reg", (int **)&reg_p, &reglen);
639 	if (rv != DDI_PROP_SUCCESS) {
640 		DWARN(NULL, "Failed to get reg property dip=0x%p", dip);
641 		return (DDI_WALK_CONTINUE);
642 	}
643 
644 	addr_hi = reg_p->addr_hi;
645 	DBG1(NULL, "addr_hi = 0x%x dip=0x%p", addr_hi, dip);
646 	ddi_prop_free(reg_p);
647 	if (addr_hi == HVCOOKIE(warg->cookie)) {
648 		ndi_hold_devi(dip);
649 		warg->dip = dip;
650 		DBG1(NULL, "Found dip = 0x%p", dip);
651 		return (DDI_WALK_TERMINATE);
652 	}
653 	return (DDI_WALK_CONTINUE);
654 }
655 
656 /*
657  * vdds_new_nexus_node -- callback function to set all the properties
658  *	a new NIU nexus node.
659  */
660 static int
661 vdds_new_nexus_node(dev_info_t *dip, void *arg, uint_t flags)
662 {
663 	vdds_cb_arg_t	*cba = (vdds_cb_arg_t *)arg;
664 	char		*compat[] = { "SUNW,niumx" };
665 	vdds_ranges_t	*rangesp;
666 	vdds_reg_t	reg;
667 	uint64_t	nranges;
668 	int		n;
669 
670 	DBG1(NULL, "Called dip=0x%p, flags=0x%X", dip, flags);
671 
672 	/* create "niu" property */
673 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "niu") !=
674 	    DDI_SUCCESS) {
675 		DERR(NULL, "Failed to create name property(dip=0x%p)", dip);
676 		return (DDI_WALK_ERROR);
677 	}
678 
679 	/* create "compatible" property */
680 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible",
681 	    compat, 1) != DDI_SUCCESS) {
682 		DERR(NULL, "Failed to create compatible property(dip=0x%p)",
683 		    dip);
684 		return (DDI_WALK_ERROR);
685 	}
686 
687 	/* create "device_type" property */
688 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
689 	    "device_type", "sun4v") != DDI_SUCCESS) {
690 		DERR(NULL, "Failed to create device_type property(dip=0x%p)",
691 		    dip);
692 		return (DDI_WALK_ERROR);
693 	}
694 
695 	/*
696 	 * create "reg" property. The first 28 bits of
697 	 * 'addr_hi'  are NIU cfg_handle, the 0xc in 28-31 bits
698 	 * indicates non-cacheable config.
699 	 */
700 	reg.addr_hi = 0xc0000000 | NIUCFGHDL(cba->cookie);
701 	reg.addr_lo = 0;
702 	reg.size_hi = 0;
703 	reg.size_lo = 0;
704 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
705 	    "reg", (int *)&reg, sizeof (reg)/sizeof (int)) != DDI_SUCCESS) {
706 		DERR(NULL, "Failed to create reg property(dip=0x%p)", dip);
707 		return (DDI_WALK_ERROR);
708 	}
709 
710 	/*
711 	 * Create VDDS_MAX_RANGES so that they are already in place
712 	 * before the children are created. While creating the child
713 	 * we just modify one of this ranges entries.
714 	 */
715 	nranges = VDDS_MAX_RANGES;  /* One range for each VR */
716 	rangesp = (vdds_ranges_t *)kmem_zalloc(
717 	    (sizeof (vdds_ranges_t) * nranges), KM_SLEEP);
718 
719 	for (n = 0; n < nranges; n++) {
720 		/* zero all child_hi/lo */
721 		rangesp[n].child_hi = 0;
722 		rangesp[n].child_lo = 0;
723 	}
724 
725 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
726 	    (int *)rangesp, (nranges * 6)) != DDI_SUCCESS) {
727 		DERR(NULL, "Failed to create ranges property(dip=0x%p)", dip);
728 		kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
729 		return (DDI_WALK_ERROR);
730 	}
731 
732 	/* create "#size-cells" property */
733 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
734 	    "#size-cells", 2) != DDI_SUCCESS) {
735 		DERR(NULL, "Failed to create #size-cells property(dip=0x%p)",
736 		    dip);
737 		kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
738 		return (DDI_WALK_ERROR);
739 	}
740 
741 	/* create "#address-cells" property */
742 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
743 	    "#address-cells", 2) != DDI_SUCCESS) {
744 		DERR(NULL, "Failed to create #address-cells prop(dip=0x%p)",
745 		    dip);
746 		kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
747 		return (DDI_WALK_ERROR);
748 	}
749 
750 	kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
751 	cba->dip = dip;
752 	ndi_hold_devi(dip);
753 	DBG1(NULL, "Returning (dip=0x%p)", dip);
754 	return (DDI_WALK_TERMINATE);
755 }
756 
757 /*
758  * vdds_new_niu_node -- callback function to create a new NIU Hybrid node.
759  */
760 static int
761 vdds_new_niu_node(dev_info_t *dip, void *arg, uint_t flags)
762 {
763 	vdds_cb_arg_t *cba = (vdds_cb_arg_t *)arg;
764 	char *compat[] = { "SUNW,niusl" };
765 	uint8_t macaddrbytes[ETHERADDRL];
766 	int interrupts[VDDS_MAX_VRINTRS];
767 	vdds_ranges_t	*prng;
768 	vdds_ranges_t	*prp;
769 	vdds_reg_t	reg;
770 	dev_info_t	*pdip;
771 	uint64_t	start;
772 	uint64_t	size;
773 	int		prnglen;
774 	int		nintr = 0;
775 	int		nrng;
776 	int		rnum;
777 	int		rv;
778 
779 	DBG1(NULL, "Called dip=0x%p flags=0x%X", dip, flags);
780 	pdip = ddi_get_parent(dip);
781 
782 	if (pdip == NULL) {
783 		DWARN(NULL, "Failed to get parent dip(dip=0x%p)", dip);
784 		return (DDI_WALK_ERROR);
785 	}
786 
787 	/* create "network" property */
788 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "network") !=
789 	    DDI_SUCCESS) {
790 		DERR(NULL, "Failed to create name property(dip=0x%p)", dip);
791 		return (DDI_WALK_ERROR);
792 	}
793 
794 	/*
795 	 * create "niutype" property, it is set to n2niu to
796 	 * indicate NIU Hybrid node.
797 	 */
798 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "niutype",
799 	    "n2niu") != DDI_SUCCESS) {
800 		DERR(NULL, "Failed to create niuopmode property(dip=0x%p)",
801 		    dip);
802 		return (DDI_WALK_ERROR);
803 	}
804 
805 	/* create "compatible" property */
806 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible",
807 	    compat, 1) != DDI_SUCCESS) {
808 		DERR(NULL, "Failed to create compatible property(dip=0x%p)",
809 		    dip);
810 		return (DDI_WALK_ERROR);
811 	}
812 
813 	/* create "device_type" property */
814 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
815 	    "device_type", "network") != DDI_SUCCESS) {
816 		DERR(NULL, "Failed to create device_type property(dip=0x%p)",
817 		    dip);
818 		return (DDI_WALK_ERROR);
819 	}
820 
821 	/* create "reg" property */
822 	if (vdds_hv_niu_vr_getinfo(HVCOOKIE(cba->cookie),
823 	    &start, &size) != H_EOK) {
824 		DERR(NULL, "Failed to get vrinfo for cookie(0x%lX)",
825 		    cba->cookie);
826 			return (DDI_WALK_ERROR);
827 	}
828 	reg.addr_hi = HVCOOKIE(cba->cookie);
829 	reg.addr_lo = 0;
830 	reg.size_hi = 0;
831 	reg.size_lo = size;
832 
833 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
834 	    (int *)&reg, sizeof (reg) / sizeof (int)) != DDI_SUCCESS) {
835 		DERR(NULL, "Failed to create reg property(dip=0x%p)", dip);
836 		return (DDI_WALK_ERROR);
837 	}
838 
839 	/*
840 	 * Modify the parent's ranges property to map the "reg" property
841 	 * of the new child.
842 	 */
843 	if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
844 	    "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) {
845 		DERR(NULL,
846 		    "Failed to get parent's ranges property(pdip=0x%p) rv=%d",
847 		    pdip, rv);
848 		return (DDI_WALK_ERROR);
849 	}
850 	nrng = prnglen/(sizeof (vdds_ranges_t));
851 	/*
852 	 * First scan all ranges to see if a range corresponding
853 	 * to this virtual NIU exists already.
854 	 */
855 	for (rnum = 0; rnum < nrng; rnum++) {
856 		prp = &prng[rnum];
857 		if (prp->child_hi == HVCOOKIE(cba->cookie)) {
858 			break;
859 		}
860 	}
861 	if (rnum == nrng) {
862 		/* Now to try to find an empty range */
863 		for (rnum = 0; rnum < nrng; rnum++) {
864 			prp = &prng[rnum];
865 			if (prp->child_hi == 0) {
866 				break;
867 			}
868 		}
869 	}
870 	if (rnum == nrng) {
871 		DERR(NULL, "No free ranges entry found");
872 		return (DDI_WALK_ERROR);
873 	}
874 
875 	/*
876 	 * child_hi will have HV cookie as HV cookie is more like
877 	 * a port in the HybridIO.
878 	 */
879 	prp->child_hi = HVCOOKIE(cba->cookie);
880 	prp->child_lo = 0;
881 	prp->parent_hi = 0x80000000 | (start >> 32);
882 	prp->parent_lo = start & 0x00000000FFFFFFFF;
883 	prp->size_hi = (size >> 32);
884 	prp->size_lo = size & 0x00000000FFFFFFFF;
885 
886 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip, "ranges",
887 	    (int *)prng, (nrng * 6)) != DDI_SUCCESS) {
888 		DERR(NULL, "Failed to update parent ranges prop(pdip=0x%p)",
889 		    pdip);
890 		return (DDI_WALK_ERROR);
891 	}
892 	kmem_free((void *)prng, prnglen);
893 
894 	vnet_macaddr_ultostr(cba->macaddr, macaddrbytes);
895 
896 	/*
897 	 * create "local-mac-address" property, this will be same as
898 	 * the vnet's mac-address.
899 	 */
900 	if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, "local-mac-address",
901 	    macaddrbytes, ETHERADDRL) != DDI_SUCCESS) {
902 		DERR(NULL, "Failed to update mac-addresses property(dip=0x%p)",
903 		    dip);
904 		return (DDI_WALK_ERROR);
905 	}
906 
907 	rv = vdds_get_interrupts(cba->cookie, rnum, interrupts, &nintr);
908 	if (rv != 0) {
909 		DERR(NULL, "Failed to get interrupts for cookie=0x%lx",
910 		    cba->cookie);
911 		return (DDI_WALK_ERROR);
912 	}
913 
914 	/* create "interrupts" property */
915 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts",
916 	    interrupts, nintr) != DDI_SUCCESS) {
917 		DERR(NULL, "Failed to update interrupts property(dip=0x%p)",
918 		    dip);
919 		return (DDI_WALK_ERROR);
920 	}
921 
922 
923 	cba->dip = dip;
924 	/* Hold the dip */
925 	ndi_hold_devi(dip);
926 	DBG1(NULL, "Returning dip=0x%p", dip);
927 	return (DDI_WALK_TERMINATE);
928 }
929 
930 
931 /*
932  * vdds_find_node -- A common function to find a NIU nexus or NIU node.
933  */
934 static dev_info_t *
935 vdds_find_node(uint64_t cookie, dev_info_t *sdip,
936 	int (*match_func)(dev_info_t *dip, void *arg))
937 {
938 	vdds_cb_arg_t arg;
939 	dev_info_t *pdip;
940 	int circ;
941 
942 	DBG1(NULL, "Called cookie=%lx\n", cookie);
943 
944 	arg.dip = NULL;
945 	arg.cookie = cookie;
946 
947 	if (pdip = ddi_get_parent(sdip)) {
948 		ndi_hold_devi(pdip);
949 		ndi_devi_enter(pdip, &circ);
950 	}
951 
952 	ddi_walk_devs(sdip, match_func, (void *)&arg);
953 	if (pdip != NULL) {
954 		ndi_devi_exit(pdip, circ);
955 		ndi_rele_devi(pdip);
956 	}
957 
958 	DBG1(NULL, "Returning dip=0x%p", arg.dip);
959 	return (arg.dip);
960 }
961 
962 /*
963  * vdds_create_new_node -- A common function to create NIU nexus/NIU node.
964  */
965 static dev_info_t *
966 vdds_create_new_node(uint64_t cookie, uint64_t macaddr, dev_info_t *pdip,
967     int (*new_node_func)(dev_info_t *dip, void *arg, uint_t flags))
968 {
969 	devi_branch_t br;
970 	int circ, rv;
971 	vdds_cb_arg_t cba;
972 
973 	DBG1(NULL, "Called cookie=0x%lx", cookie);
974 
975 	cba.dip = NULL;
976 	cba.cookie = cookie;
977 	cba.macaddr = macaddr;
978 
979 	br.arg = (void *)&cba;
980 	br.type = DEVI_BRANCH_SID;
981 	br.create.sid_branch_create = new_node_func;
982 	br.devi_branch_callback = NULL;
983 
984 	if (pdip == NULL)
985 		pdip = ddi_root_node();
986 	ndi_devi_enter(pdip, &circ);
987 	DBG1(NULL, "calling e_ddi_branch_create");
988 	if ((rv = e_ddi_branch_create(pdip, &br, NULL,
989 	    DEVI_BRANCH_CHILD | DEVI_BRANCH_CONFIGURE))) {
990 		ndi_devi_exit(pdip, circ);
991 		DERR(NULL, "e_ddi_branch_create failed=%d", rv);
992 		return (NULL);
993 	}
994 	ndi_devi_exit(pdip, circ);
995 	DBG1(NULL, "Returning(dip=0x%p", cba.dip);
996 	return (cba.dip);
997 }
998 
999 /*
1000  * vdds_get_interrupts -- A function that binds ino's to channels and
1001  *	then provides them to create interrupts property.
1002  */
1003 static int
1004 vdds_get_interrupts(uint64_t cookie, int ino_range, int *intrs, int *nintr)
1005 {
1006 	uint32_t hvcookie = HVCOOKIE(cookie);
1007 	uint64_t txmap;
1008 	uint64_t rxmap;
1009 	uint32_t ino = VDDS_INO_RANGE_START(ino_range);
1010 	int rv;
1011 	uint64_t i;
1012 
1013 	*nintr = 0;
1014 	rv = vdds_hv_niu_vr_get_txmap(hvcookie, &txmap);
1015 	if (rv != H_EOK) {
1016 		DWARN(NULL, "Failed to get txmap for hvcookie=0x%X rv=%d\n",
1017 		    hvcookie, rv);
1018 		return (EIO);
1019 	}
1020 	rv = vdds_hv_niu_vr_get_rxmap(hvcookie, &rxmap);
1021 	if (rv != H_EOK) {
1022 		DWARN(NULL, "Failed to get rxmap for hvcookie=0x%X, rv=%d\n",
1023 		    hvcookie, rv);
1024 		return (EIO);
1025 	}
1026 	/* Check if the number of total channels to be more than 8 */
1027 	for (i = 0; i < 4; i++) {
1028 		if (rxmap & (((uint64_t)0x1) << i)) {
1029 			rv = vdds_hv_niu_vrrx_set_ino(hvcookie, i, ino);
1030 			if (rv != H_EOK) {
1031 				DWARN(NULL, "Failed to get Rx ino for "
1032 				    "hvcookie=0x%X vch_idx=0x%lx rv=%d\n",
1033 				    hvcookie, i, rv);
1034 				return (EIO);
1035 			}
1036 			DWARN(NULL,
1037 			    "hvcookie=0x%X RX vch_idx=0x%lx ino=0x%X\n",
1038 			    hvcookie, i, ino);
1039 			*intrs = ino;
1040 			ino++;
1041 		} else {
1042 			*intrs = VDDS_MAX_INTR_NUM;
1043 		}
1044 		intrs++;
1045 		*nintr += 1;
1046 	}
1047 	for (i = 0; i < 4; i++) {
1048 		if (txmap & (((uint64_t)0x1) << i)) {
1049 			rv = vdds_hv_niu_vrtx_set_ino(hvcookie, i, ino);
1050 			if (rv != H_EOK) {
1051 				DWARN(NULL, "Failed to get Tx ino for "
1052 				    "hvcookie=0x%X vch_idx=0x%lx rv=%d\n",
1053 				    hvcookie, i, rv);
1054 				return (EIO);
1055 			}
1056 			DWARN(NULL, "hvcookie=0x%X TX vch_idx=0x%lx ino=0x%X\n",
1057 			    hvcookie, i, ino);
1058 			*intrs = ino;
1059 			ino++;
1060 		} else {
1061 			*intrs = VDDS_MAX_INTR_NUM;
1062 		}
1063 		intrs++;
1064 		*nintr += 1;
1065 	}
1066 	return (0);
1067 }
1068 
1069 /*
1070  * vdds_release_range_prop -- cleanups an entry in the ranges property
1071  *	corresponding to a cookie.
1072  */
1073 static void
1074 vdds_release_range_prop(dev_info_t *nexus_dip, uint64_t cookie)
1075 {
1076 	vdds_ranges_t *prng;
1077 	vdds_ranges_t *prp;
1078 	int prnglen;
1079 	int nrng;
1080 	int rnum;
1081 	boolean_t success = B_FALSE;
1082 	int rv;
1083 
1084 	if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, nexus_dip, DDI_PROP_DONTPASS,
1085 	    "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) {
1086 		DERR(NULL,
1087 		    "Failed to get nexus ranges property(dip=0x%p) rv=%d",
1088 		    nexus_dip, rv);
1089 		return;
1090 	}
1091 	nrng = prnglen/(sizeof (vdds_ranges_t));
1092 	for (rnum = 0; rnum < nrng; rnum++) {
1093 		prp = &prng[rnum];
1094 		if (prp->child_hi == HVCOOKIE(cookie)) {
1095 			prp->child_hi = 0;
1096 			success = B_TRUE;
1097 			break;
1098 		}
1099 	}
1100 	if (success) {
1101 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE, nexus_dip,
1102 		    "ranges", (int *)prng, (nrng * 6)) != DDI_SUCCESS) {
1103 			DERR(NULL,
1104 			    "Failed to update nexus ranges prop(dip=0x%p)",
1105 			    nexus_dip);
1106 		}
1107 	}
1108 }
1109