xref: /titanic_50/usr/src/uts/sun4v/io/vnet_dds.c (revision e4b86885570d77af552e9cf94f142f4d744fb8c8)
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 #include <sys/modctl.h>
28 #include <sys/prom_plat.h>
29 #include <sys/ddi.h>
30 #include <sys/sunddi.h>
31 #include <sys/sunndi.h>
32 #include <sys/ndi_impldefs.h>
33 #include <sys/ddi_impldefs.h>
34 #include <sys/ethernet.h>
35 #include <sys/machsystm.h>
36 #include <sys/hypervisor_api.h>
37 #include <sys/mach_descrip.h>
38 #include <sys/drctl.h>
39 #include <sys/dr_util.h>
40 #include <sys/mac.h>
41 #include <sys/vnet.h>
42 #include <sys/vnet_mailbox.h>
43 #include <sys/vnet_common.h>
44 #include <sys/hsvc.h>
45 
46 
47 #define	VDDS_MAX_RANGES		6	/* 6 possible VRs */
48 #define	VDDS_MAX_VRINTRS	8	/* limited to 8 intrs/VR */
49 #define	VDDS_MAX_INTR_NUM	64	/* 0-63 or valid */
50 
51 #define	VDDS_INO_RANGE_START(x) (x * VDDS_MAX_VRINTRS)
52 #define	HVCOOKIE(c)	((c) & 0xFFFFFFFFF)
53 #define	NIUCFGHDL(c)	((c) >> 32)
54 
55 
56 /* For "ranges" property */
57 typedef struct vdds_ranges {
58 	uint32_t child_hi;
59 	uint32_t child_lo;
60 	uint32_t parent_hi;
61 	uint32_t parent_lo;
62 	uint32_t size_hi;
63 	uint32_t size_lo;
64 } vdds_ranges_t;
65 
66 /* For "reg" property */
67 typedef struct vdds_reg {
68 	uint32_t addr_hi;
69 	uint32_t addr_lo;
70 	uint32_t size_hi;
71 	uint32_t size_lo;
72 } vdds_reg_t;
73 
74 /* For ddi callback argument */
75 typedef struct vdds_cb_arg {
76 	dev_info_t *dip;
77 	uint64_t cookie;
78 	uint64_t macaddr;
79 	uint32_t max_frame_size;
80 } vdds_cb_arg_t;
81 
82 
83 /* Functions exported to other files */
84 void vdds_mod_init(void);
85 void vdds_mod_fini(void);
86 int vdds_init(vnet_t *vnetp);
87 void vdds_cleanup(vnet_t *vnetp);
88 void vdds_process_dds_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg);
89 void vdds_cleanup_hybrid_res(vnet_t *vnetp);
90 
91 /* Support functions to create/destory Hybrid device */
92 static dev_info_t *vdds_create_niu_node(uint64_t cookie,
93     uint64_t macaddr, uint32_t max_frame_size);
94 static int vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie);
95 static dev_info_t *vdds_create_new_node(vdds_cb_arg_t *cba,
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 	uint32_t	max_frame_size;
366 	uint64_t	hio_cookie;
367 	int		rv;
368 
369 	DBG1(vdds, "DDS task started...");
370 	mutex_enter(&vdds->lock);
371 	switch (vdds->task_flags) {
372 	case VNET_DDS_TASK_ADD_SHARE:
373 		DBG2(vdds, "ADD_SHARE task...");
374 		hio_cookie = dmsg->msg.share_msg.cookie;
375 		/*
376 		 * max-frame-size value need to be set to
377 		 * the full ethernet frame size. That is,
378 		 * header + payload + checksum.
379 		 */
380 		max_frame_size = vnetp->mtu +
381 		    sizeof (struct  ether_vlan_header) + ETHERFCSL;
382 		dip = vdds_create_niu_node(hio_cookie,
383 		    dmsg->msg.share_msg.macaddr, max_frame_size);
384 		if (dip == NULL) {
385 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
386 			DERR(vdds, "Failed to create HIO node");
387 		} else {
388 			vdds->hio_dip = dip;
389 			vdds->hio_cookie = hio_cookie;
390 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE);
391 			/* DERR used only print by default */
392 			DERR(vdds, "Successfully created HIO node");
393 		}
394 		break;
395 
396 	case VNET_DDS_TASK_DEL_SHARE:
397 		DBG2(vdds, "DEL_SHARE task...");
398 		if (vnetp->vdds_info.hio_dip == NULL) {
399 			DBG2(vdds, "NACK: No HIO device destroy");
400 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE);
401 		} else {
402 			rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip,
403 			    vdds->hio_cookie);
404 			if (rv == 0) {
405 				/* use DERR to print by default */
406 				DERR(vdds, "Successfully destroyed"
407 				    " Hybrid node");
408 			} else {
409 				cmn_err(CE_WARN, "vnet%d:Failed to "
410 				    "destroy Hybrid node", vnetp->instance);
411 			}
412 			/* TODO: send ACK even for failure? */
413 			DBG2(vdds, "ACK: HIO device destroyed");
414 			(void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE);
415 			vdds->hio_dip = 0;
416 			vdds->hio_cookie = 0;
417 		}
418 		break;
419 	case VNET_DDS_TASK_REL_SHARE:
420 		DBG2(vdds, "REL_SHARE task...");
421 		if (vnetp->vdds_info.hio_dip != NULL) {
422 			rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip,
423 			    vdds->hio_cookie);
424 			if (rv == 0) {
425 				DERR(vdds, "Successfully destroyed "
426 				    "Hybrid node");
427 			} else {
428 				cmn_err(CE_WARN, "vnet%d:Failed to "
429 				    "destroy HIO node", vnetp->instance);
430 			}
431 			/* TODO: failure case */
432 			(void) vdds_send_dds_rel_msg(vnetp);
433 			vdds->hio_dip = 0;
434 			vdds->hio_cookie = 0;
435 		}
436 		break;
437 	default:
438 		break;
439 	}
440 	vdds->task_flags = 0;
441 	mutex_exit(&vdds->lock);
442 }
443 
444 /*
445  * vdds_send_dds_rel_msg -- Send a DDS_REL_SHARE message.
446  */
447 static int
448 vdds_send_dds_rel_msg(vnet_t *vnetp)
449 {
450 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
451 	vio_dds_msg_t	vmsg;
452 	dds_share_msg_t	*smsg = &vmsg.msg.share_msg;
453 	int rv;
454 
455 	DBG1(vdds, "Sending DDS_VNET_REL_SHARE message");
456 	vmsg.tag.vio_msgtype = VIO_TYPE_CTRL;
457 	vmsg.tag.vio_subtype = VIO_SUBTYPE_INFO;
458 	vmsg.tag.vio_subtype_env = VIO_DDS_INFO;
459 	/* vio_sid filled by the LDC module */
460 	vmsg.dds_class = DDS_VNET_NIU;
461 	vmsg.dds_subclass = DDS_VNET_REL_SHARE;
462 	vmsg.dds_req_id = (++vdds->dds_req_id);
463 	smsg->macaddr = vnet_macaddr_strtoul(vnetp->curr_macaddr);
464 	smsg->cookie = vdds->hio_cookie;
465 	rv = vnet_send_dds_msg(vnetp, &vmsg);
466 	return (rv);
467 }
468 
469 /*
470  * vdds_send_dds_resp_msg -- Send a DDS response message.
471  */
472 static int
473 vdds_send_dds_resp_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg, int ack)
474 {
475 	vnet_dds_info_t *vdds = &vnetp->vdds_info;
476 	int rv;
477 
478 	DBG1(vdds, "Sending a response mesage=%d", ack);
479 	if (ack == B_TRUE) {
480 		dmsg->tag.vio_subtype = VIO_SUBTYPE_ACK;
481 		dmsg->msg.share_resp_msg.status = DDS_VNET_SUCCESS;
482 	} else {
483 		dmsg->tag.vio_subtype = VIO_SUBTYPE_NACK;
484 		dmsg->msg.share_resp_msg.status = DDS_VNET_FAIL;
485 	}
486 	rv = vnet_send_dds_msg(vnetp, dmsg);
487 	return (rv);
488 }
489 
490 /*
491  * vdds_create_niu_node -- Create NIU Hybrid node. The NIU nexus
492  *	node also created if it doesn't exist already.
493  */
494 dev_info_t *
495 vdds_create_niu_node(uint64_t cookie, uint64_t macaddr, uint32_t max_frame_size)
496 {
497 	dev_info_t *nexus_dip;
498 	dev_info_t *niu_dip;
499 	vdds_cb_arg_t cba;
500 
501 	DBG1(NULL, "Called");
502 
503 	if (vdds_hv_hio_capable == B_FALSE) {
504 		return (NULL);
505 	}
506 	/* Check if the nexus node exists already */
507 	nexus_dip = vdds_find_node(cookie, ddi_root_node(),
508 	    vdds_match_niu_nexus);
509 	if (nexus_dip == NULL) {
510 		/*
511 		 * NIU nexus node not found, so create it now.
512 		 */
513 		cba.dip = NULL;
514 		cba.cookie = cookie;
515 		cba.macaddr = macaddr;
516 		cba.max_frame_size = max_frame_size;
517 		nexus_dip = vdds_create_new_node(&cba, NULL,
518 		    vdds_new_nexus_node);
519 		if (nexus_dip == NULL) {
520 			return (NULL);
521 		}
522 	}
523 	DBG2(NULL, "nexus_dip = 0x%p", nexus_dip);
524 
525 	/* Check if NIU node exists already before creating one */
526 	niu_dip = vdds_find_node(cookie, nexus_dip,
527 	    vdds_match_niu_node);
528 	if (niu_dip == NULL) {
529 		cba.dip = NULL;
530 		cba.cookie = cookie;
531 		cba.macaddr = macaddr;
532 		cba.max_frame_size = max_frame_size;
533 		niu_dip = vdds_create_new_node(&cba, nexus_dip,
534 		    vdds_new_niu_node);
535 		if (niu_dip == NULL) {
536 			DWARN(NULL, "create_niu_hio_node returned NULL");
537 		}
538 		/*
539 		 * Do not release the niu_dip to prevent it from
540 		 * auto-detach/unload.
541 		 */
542 		ndi_rele_devi(nexus_dip);
543 		return (niu_dip);
544 
545 	}
546 	DWARN(NULL, "niumx/network node already exists(dip=0x%p)", niu_dip);
547 	ndi_rele_devi(niu_dip);
548 	ndi_rele_devi(nexus_dip);
549 	DBG1(NULL, "returning success");
550 	return (niu_dip);
551 }
552 
553 /*
554  * vdds_destroy_niu_node -- Destroy the NIU node.
555  */
556 int
557 vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie)
558 {
559 	int rv;
560 	dev_info_t *fdip = NULL;
561 	dev_info_t *nexus_dip = ddi_get_parent(niu_dip);
562 
563 
564 	DBG1(NULL, "Called");
565 	ndi_rele_devi(niu_dip);
566 	rv = e_ddi_branch_unconfigure(niu_dip, &fdip, 0);
567 	if (rv != 0) {
568 		DERR(NULL, "Failed to unconfigure niumx/network node dip=0x%p",
569 		    niu_dip);
570 		if (fdip != NULL) {
571 			ndi_rele_devi(fdip);
572 		}
573 		return (EBUSY);
574 	}
575 	rv = e_ddi_branch_destroy(niu_dip, &fdip, 0);
576 	if (rv != 0) {
577 		DERR(NULL, "Failed to destroy niumx/network node dip=0x%p",
578 		    niu_dip);
579 		if (fdip != NULL) {
580 			ndi_rele_devi(fdip);
581 		}
582 		return (EBUSY);
583 	}
584 	/*
585 	 * Cleanup the parent's ranges property set
586 	 * for this Hybrid device.
587 	 */
588 	vdds_release_range_prop(nexus_dip, cookie);
589 	DBG1(NULL, "returning success");
590 	return (0);
591 }
592 
593 /*
594  * vdds_match_niu_nexus -- callback function to verify a node is the
595  *	NIU nexus node.
596  */
597 static int
598 vdds_match_niu_nexus(dev_info_t *dip, void *arg)
599 {
600 	vdds_cb_arg_t	*warg = (vdds_cb_arg_t *)arg;
601 	vdds_reg_t	*reg_p;
602 	char		*name;
603 	uint64_t	hdl;
604 	uint_t		reglen;
605 	int		rv;
606 
607 	if (dip == ddi_root_node()) {
608 		return (DDI_WALK_CONTINUE);
609 	}
610 
611 	name = ddi_node_name(dip);
612 	if (strcmp(name, "niu")  != 0) {
613 		return (DDI_WALK_CONTINUE);
614 	}
615 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
616 	    DDI_PROP_DONTPASS, "reg", (int **)&reg_p, &reglen);
617 	if (rv != DDI_PROP_SUCCESS) {
618 		DWARN(NULL, "Failed to get reg property dip=0x%p", dip);
619 		return (DDI_WALK_CONTINUE);
620 	}
621 
622 	hdl =  reg_p->addr_hi & 0x0FFFFFFF;
623 	ddi_prop_free(reg_p);
624 
625 	DBG2(NULL, "Handle = 0x%lx dip=0x%p", hdl, dip);
626 	if (hdl == NIUCFGHDL(warg->cookie)) {
627 		/* Hold before returning */
628 		ndi_hold_devi(dip);
629 		warg->dip = dip;
630 		DBG2(NULL, "Found dip = 0x%p", dip);
631 		return (DDI_WALK_TERMINATE);
632 	}
633 	return (DDI_WALK_CONTINUE);
634 }
635 
636 /*
637  * vdds_match_niu_node -- callback function to verify a node is the
638  *	NIU Hybrid node.
639  */
640 static int
641 vdds_match_niu_node(dev_info_t *dip, void *arg)
642 {
643 	vdds_cb_arg_t	*warg = (vdds_cb_arg_t *)arg;
644 	char		*name;
645 	vdds_reg_t	*reg_p;
646 	uint_t		reglen;
647 	int		rv;
648 	uint32_t	addr_hi;
649 
650 	name = ddi_node_name(dip);
651 	if (strcmp(name, "network")  != 0) {
652 		return (DDI_WALK_CONTINUE);
653 	}
654 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
655 	    DDI_PROP_DONTPASS, "reg", (int **)&reg_p, &reglen);
656 	if (rv != DDI_PROP_SUCCESS) {
657 		DWARN(NULL, "Failed to get reg property dip=0x%p", dip);
658 		return (DDI_WALK_CONTINUE);
659 	}
660 
661 	addr_hi = reg_p->addr_hi;
662 	DBG1(NULL, "addr_hi = 0x%x dip=0x%p", addr_hi, dip);
663 	ddi_prop_free(reg_p);
664 	if (addr_hi == HVCOOKIE(warg->cookie)) {
665 		ndi_hold_devi(dip);
666 		warg->dip = dip;
667 		DBG1(NULL, "Found dip = 0x%p", dip);
668 		return (DDI_WALK_TERMINATE);
669 	}
670 	return (DDI_WALK_CONTINUE);
671 }
672 
673 /*
674  * vdds_new_nexus_node -- callback function to set all the properties
675  *	a new NIU nexus node.
676  */
677 static int
678 vdds_new_nexus_node(dev_info_t *dip, void *arg, uint_t flags)
679 {
680 	vdds_cb_arg_t	*cba = (vdds_cb_arg_t *)arg;
681 	char		*compat[] = { "SUNW,niumx" };
682 	vdds_ranges_t	*rangesp;
683 	vdds_reg_t	reg;
684 	uint64_t	nranges;
685 	int		n;
686 
687 	DBG1(NULL, "Called dip=0x%p, flags=0x%X", dip, flags);
688 
689 	/* create "niu" property */
690 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "niu") !=
691 	    DDI_SUCCESS) {
692 		DERR(NULL, "Failed to create name property(dip=0x%p)", dip);
693 		return (DDI_WALK_ERROR);
694 	}
695 
696 	/* create "compatible" property */
697 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible",
698 	    compat, 1) != DDI_SUCCESS) {
699 		DERR(NULL, "Failed to create compatible property(dip=0x%p)",
700 		    dip);
701 		return (DDI_WALK_ERROR);
702 	}
703 
704 	/* create "device_type" property */
705 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
706 	    "device_type", "sun4v") != DDI_SUCCESS) {
707 		DERR(NULL, "Failed to create device_type property(dip=0x%p)",
708 		    dip);
709 		return (DDI_WALK_ERROR);
710 	}
711 
712 	/*
713 	 * create "reg" property. The first 28 bits of
714 	 * 'addr_hi'  are NIU cfg_handle, the 0xc in 28-31 bits
715 	 * indicates non-cacheable config.
716 	 */
717 	reg.addr_hi = 0xc0000000 | NIUCFGHDL(cba->cookie);
718 	reg.addr_lo = 0;
719 	reg.size_hi = 0;
720 	reg.size_lo = 0;
721 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
722 	    "reg", (int *)&reg, sizeof (reg)/sizeof (int)) != DDI_SUCCESS) {
723 		DERR(NULL, "Failed to create reg property(dip=0x%p)", dip);
724 		return (DDI_WALK_ERROR);
725 	}
726 
727 	/*
728 	 * Create VDDS_MAX_RANGES so that they are already in place
729 	 * before the children are created. While creating the child
730 	 * we just modify one of this ranges entries.
731 	 */
732 	nranges = VDDS_MAX_RANGES;  /* One range for each VR */
733 	rangesp = (vdds_ranges_t *)kmem_zalloc(
734 	    (sizeof (vdds_ranges_t) * nranges), KM_SLEEP);
735 
736 	for (n = 0; n < nranges; n++) {
737 		/* zero all child_hi/lo */
738 		rangesp[n].child_hi = 0;
739 		rangesp[n].child_lo = 0;
740 	}
741 
742 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
743 	    (int *)rangesp, (nranges * 6)) != DDI_SUCCESS) {
744 		DERR(NULL, "Failed to create ranges property(dip=0x%p)", dip);
745 		kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
746 		return (DDI_WALK_ERROR);
747 	}
748 
749 	/* create "#size-cells" property */
750 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
751 	    "#size-cells", 2) != DDI_SUCCESS) {
752 		DERR(NULL, "Failed to create #size-cells property(dip=0x%p)",
753 		    dip);
754 		kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
755 		return (DDI_WALK_ERROR);
756 	}
757 
758 	/* create "#address-cells" property */
759 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
760 	    "#address-cells", 2) != DDI_SUCCESS) {
761 		DERR(NULL, "Failed to create #address-cells prop(dip=0x%p)",
762 		    dip);
763 		kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
764 		return (DDI_WALK_ERROR);
765 	}
766 
767 	kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges));
768 	cba->dip = dip;
769 	ndi_hold_devi(dip);
770 	DBG1(NULL, "Returning (dip=0x%p)", dip);
771 	return (DDI_WALK_TERMINATE);
772 }
773 
774 /*
775  * vdds_new_niu_node -- callback function to create a new NIU Hybrid node.
776  */
777 static int
778 vdds_new_niu_node(dev_info_t *dip, void *arg, uint_t flags)
779 {
780 	vdds_cb_arg_t *cba = (vdds_cb_arg_t *)arg;
781 	char *compat[] = { "SUNW,niusl" };
782 	uint8_t macaddrbytes[ETHERADDRL];
783 	int interrupts[VDDS_MAX_VRINTRS];
784 	vdds_ranges_t	*prng;
785 	vdds_ranges_t	*prp;
786 	vdds_reg_t	reg;
787 	dev_info_t	*pdip;
788 	uint64_t	start;
789 	uint64_t	size;
790 	int		prnglen;
791 	int		nintr = 0;
792 	int		nrng;
793 	int		rnum;
794 	int		rv;
795 
796 	DBG1(NULL, "Called dip=0x%p flags=0x%X", dip, flags);
797 	pdip = ddi_get_parent(dip);
798 
799 	if (pdip == NULL) {
800 		DWARN(NULL, "Failed to get parent dip(dip=0x%p)", dip);
801 		return (DDI_WALK_ERROR);
802 	}
803 
804 	/* create "network" property */
805 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "network") !=
806 	    DDI_SUCCESS) {
807 		DERR(NULL, "Failed to create name property(dip=0x%p)", dip);
808 		return (DDI_WALK_ERROR);
809 	}
810 
811 	/*
812 	 * create "niutype" property, it is set to n2niu to
813 	 * indicate NIU Hybrid node.
814 	 */
815 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "niutype",
816 	    "n2niu") != DDI_SUCCESS) {
817 		DERR(NULL, "Failed to create niuopmode property(dip=0x%p)",
818 		    dip);
819 		return (DDI_WALK_ERROR);
820 	}
821 
822 	/* create "compatible" property */
823 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible",
824 	    compat, 1) != DDI_SUCCESS) {
825 		DERR(NULL, "Failed to create compatible property(dip=0x%p)",
826 		    dip);
827 		return (DDI_WALK_ERROR);
828 	}
829 
830 	/* create "device_type" property */
831 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
832 	    "device_type", "network") != DDI_SUCCESS) {
833 		DERR(NULL, "Failed to create device_type property(dip=0x%p)",
834 		    dip);
835 		return (DDI_WALK_ERROR);
836 	}
837 
838 	/* create "reg" property */
839 	if (vdds_hv_niu_vr_getinfo(HVCOOKIE(cba->cookie),
840 	    &start, &size) != H_EOK) {
841 		DERR(NULL, "Failed to get vrinfo for cookie(0x%lX)",
842 		    cba->cookie);
843 			return (DDI_WALK_ERROR);
844 	}
845 	reg.addr_hi = HVCOOKIE(cba->cookie);
846 	reg.addr_lo = 0;
847 	reg.size_hi = 0;
848 	reg.size_lo = size;
849 
850 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
851 	    (int *)&reg, sizeof (reg) / sizeof (int)) != DDI_SUCCESS) {
852 		DERR(NULL, "Failed to create reg property(dip=0x%p)", dip);
853 		return (DDI_WALK_ERROR);
854 	}
855 
856 	/*
857 	 * Modify the parent's ranges property to map the "reg" property
858 	 * of the new child.
859 	 */
860 	if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
861 	    "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) {
862 		DERR(NULL,
863 		    "Failed to get parent's ranges property(pdip=0x%p) rv=%d",
864 		    pdip, rv);
865 		return (DDI_WALK_ERROR);
866 	}
867 	nrng = prnglen/(sizeof (vdds_ranges_t));
868 	/*
869 	 * First scan all ranges to see if a range corresponding
870 	 * to this virtual NIU exists already.
871 	 */
872 	for (rnum = 0; rnum < nrng; rnum++) {
873 		prp = &prng[rnum];
874 		if (prp->child_hi == HVCOOKIE(cba->cookie)) {
875 			break;
876 		}
877 	}
878 	if (rnum == nrng) {
879 		/* Now to try to find an empty range */
880 		for (rnum = 0; rnum < nrng; rnum++) {
881 			prp = &prng[rnum];
882 			if (prp->child_hi == 0) {
883 				break;
884 			}
885 		}
886 	}
887 	if (rnum == nrng) {
888 		DERR(NULL, "No free ranges entry found");
889 		return (DDI_WALK_ERROR);
890 	}
891 
892 	/*
893 	 * child_hi will have HV cookie as HV cookie is more like
894 	 * a port in the HybridIO.
895 	 */
896 	prp->child_hi = HVCOOKIE(cba->cookie);
897 	prp->child_lo = 0;
898 	prp->parent_hi = 0x80000000 | (start >> 32);
899 	prp->parent_lo = start & 0x00000000FFFFFFFF;
900 	prp->size_hi = (size >> 32);
901 	prp->size_lo = size & 0x00000000FFFFFFFF;
902 
903 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip, "ranges",
904 	    (int *)prng, (nrng * 6)) != DDI_SUCCESS) {
905 		DERR(NULL, "Failed to update parent ranges prop(pdip=0x%p)",
906 		    pdip);
907 		return (DDI_WALK_ERROR);
908 	}
909 	kmem_free((void *)prng, prnglen);
910 
911 	vnet_macaddr_ultostr(cba->macaddr, macaddrbytes);
912 
913 	/*
914 	 * create "local-mac-address" property, this will be same as
915 	 * the vnet's mac-address.
916 	 */
917 	if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, "local-mac-address",
918 	    macaddrbytes, ETHERADDRL) != DDI_SUCCESS) {
919 		DERR(NULL, "Failed to update mac-addresses property(dip=0x%p)",
920 		    dip);
921 		return (DDI_WALK_ERROR);
922 	}
923 
924 	rv = vdds_get_interrupts(cba->cookie, rnum, interrupts, &nintr);
925 	if (rv != 0) {
926 		DERR(NULL, "Failed to get interrupts for cookie=0x%lx",
927 		    cba->cookie);
928 		return (DDI_WALK_ERROR);
929 	}
930 
931 	/* create "interrupts" property */
932 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts",
933 	    interrupts, nintr) != DDI_SUCCESS) {
934 		DERR(NULL, "Failed to update interrupts property(dip=0x%p)",
935 		    dip);
936 		return (DDI_WALK_ERROR);
937 	}
938 
939 
940 	/* create "max_frame_size" property */
941 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, "max-frame-size",
942 	    cba->max_frame_size) != DDI_SUCCESS) {
943 		DERR(NULL, "Failed to update max-frame-size property(dip=0x%p)",
944 		    dip);
945 		return (DDI_WALK_ERROR);
946 	}
947 
948 	cba->dip = dip;
949 	/* Hold the dip */
950 	ndi_hold_devi(dip);
951 	DBG1(NULL, "Returning dip=0x%p", dip);
952 	return (DDI_WALK_TERMINATE);
953 }
954 
955 
956 /*
957  * vdds_find_node -- A common function to find a NIU nexus or NIU node.
958  */
959 static dev_info_t *
960 vdds_find_node(uint64_t cookie, dev_info_t *sdip,
961 	int (*match_func)(dev_info_t *dip, void *arg))
962 {
963 	vdds_cb_arg_t arg;
964 	dev_info_t *pdip;
965 	int circ;
966 
967 	DBG1(NULL, "Called cookie=%lx\n", cookie);
968 
969 	arg.dip = NULL;
970 	arg.cookie = cookie;
971 
972 	if (pdip = ddi_get_parent(sdip)) {
973 		ndi_hold_devi(pdip);
974 		ndi_devi_enter(pdip, &circ);
975 	}
976 
977 	ddi_walk_devs(sdip, match_func, (void *)&arg);
978 	if (pdip != NULL) {
979 		ndi_devi_exit(pdip, circ);
980 		ndi_rele_devi(pdip);
981 	}
982 
983 	DBG1(NULL, "Returning dip=0x%p", arg.dip);
984 	return (arg.dip);
985 }
986 
987 /*
988  * vdds_create_new_node -- A common function to create NIU nexus/NIU node.
989  */
990 static dev_info_t *
991 vdds_create_new_node(vdds_cb_arg_t *cbap, dev_info_t *pdip,
992     int (*new_node_func)(dev_info_t *dip, void *arg, uint_t flags))
993 {
994 	devi_branch_t br;
995 	int circ, rv;
996 
997 	DBG1(NULL, "Called cookie=0x%lx", cbap->cookie);
998 
999 	br.arg = (void *)cbap;
1000 	br.type = DEVI_BRANCH_SID;
1001 	br.create.sid_branch_create = new_node_func;
1002 	br.devi_branch_callback = NULL;
1003 
1004 	if (pdip == NULL)
1005 		pdip = ddi_root_node();
1006 	ndi_devi_enter(pdip, &circ);
1007 	DBG1(NULL, "calling e_ddi_branch_create");
1008 	if ((rv = e_ddi_branch_create(pdip, &br, NULL,
1009 	    DEVI_BRANCH_CHILD | DEVI_BRANCH_CONFIGURE))) {
1010 		ndi_devi_exit(pdip, circ);
1011 		DERR(NULL, "e_ddi_branch_create failed=%d", rv);
1012 		return (NULL);
1013 	}
1014 	ndi_devi_exit(pdip, circ);
1015 	DBG1(NULL, "Returning(dip=0x%p", cbap->dip);
1016 	return (cbap->dip);
1017 }
1018 
1019 /*
1020  * vdds_get_interrupts -- A function that binds ino's to channels and
1021  *	then provides them to create interrupts property.
1022  */
1023 static int
1024 vdds_get_interrupts(uint64_t cookie, int ino_range, int *intrs, int *nintr)
1025 {
1026 	uint32_t hvcookie = HVCOOKIE(cookie);
1027 	uint64_t txmap;
1028 	uint64_t rxmap;
1029 	uint32_t ino = VDDS_INO_RANGE_START(ino_range);
1030 	int rv;
1031 	uint64_t i;
1032 
1033 	*nintr = 0;
1034 	rv = vdds_hv_niu_vr_get_txmap(hvcookie, &txmap);
1035 	if (rv != H_EOK) {
1036 		DWARN(NULL, "Failed to get txmap for hvcookie=0x%X rv=%d\n",
1037 		    hvcookie, rv);
1038 		return (EIO);
1039 	}
1040 	rv = vdds_hv_niu_vr_get_rxmap(hvcookie, &rxmap);
1041 	if (rv != H_EOK) {
1042 		DWARN(NULL, "Failed to get rxmap for hvcookie=0x%X, rv=%d\n",
1043 		    hvcookie, rv);
1044 		return (EIO);
1045 	}
1046 	/* Check if the number of total channels to be more than 8 */
1047 	for (i = 0; i < 4; i++) {
1048 		if (rxmap & (((uint64_t)0x1) << i)) {
1049 			rv = vdds_hv_niu_vrrx_set_ino(hvcookie, i, ino);
1050 			if (rv != H_EOK) {
1051 				DWARN(NULL, "Failed to get Rx ino for "
1052 				    "hvcookie=0x%X vch_idx=0x%lx rv=%d\n",
1053 				    hvcookie, i, rv);
1054 				return (EIO);
1055 			}
1056 			DWARN(NULL,
1057 			    "hvcookie=0x%X RX vch_idx=0x%lx ino=0x%X\n",
1058 			    hvcookie, i, ino);
1059 			*intrs = ino;
1060 			ino++;
1061 		} else {
1062 			*intrs = VDDS_MAX_INTR_NUM;
1063 		}
1064 		intrs++;
1065 		*nintr += 1;
1066 	}
1067 	for (i = 0; i < 4; i++) {
1068 		if (txmap & (((uint64_t)0x1) << i)) {
1069 			rv = vdds_hv_niu_vrtx_set_ino(hvcookie, i, ino);
1070 			if (rv != H_EOK) {
1071 				DWARN(NULL, "Failed to get Tx ino for "
1072 				    "hvcookie=0x%X vch_idx=0x%lx rv=%d\n",
1073 				    hvcookie, i, rv);
1074 				return (EIO);
1075 			}
1076 			DWARN(NULL, "hvcookie=0x%X TX vch_idx=0x%lx ino=0x%X\n",
1077 			    hvcookie, i, ino);
1078 			*intrs = ino;
1079 			ino++;
1080 		} else {
1081 			*intrs = VDDS_MAX_INTR_NUM;
1082 		}
1083 		intrs++;
1084 		*nintr += 1;
1085 	}
1086 	return (0);
1087 }
1088 
1089 /*
1090  * vdds_release_range_prop -- cleanups an entry in the ranges property
1091  *	corresponding to a cookie.
1092  */
1093 static void
1094 vdds_release_range_prop(dev_info_t *nexus_dip, uint64_t cookie)
1095 {
1096 	vdds_ranges_t *prng;
1097 	vdds_ranges_t *prp;
1098 	int prnglen;
1099 	int nrng;
1100 	int rnum;
1101 	boolean_t success = B_FALSE;
1102 	int rv;
1103 
1104 	if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, nexus_dip, DDI_PROP_DONTPASS,
1105 	    "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) {
1106 		DERR(NULL,
1107 		    "Failed to get nexus ranges property(dip=0x%p) rv=%d",
1108 		    nexus_dip, rv);
1109 		return;
1110 	}
1111 	nrng = prnglen/(sizeof (vdds_ranges_t));
1112 	for (rnum = 0; rnum < nrng; rnum++) {
1113 		prp = &prng[rnum];
1114 		if (prp->child_hi == HVCOOKIE(cookie)) {
1115 			prp->child_hi = 0;
1116 			success = B_TRUE;
1117 			break;
1118 		}
1119 	}
1120 	if (success) {
1121 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE, nexus_dip,
1122 		    "ranges", (int *)prng, (nrng * 6)) != DDI_SUCCESS) {
1123 			DERR(NULL,
1124 			    "Failed to update nexus ranges prop(dip=0x%p)",
1125 			    nexus_dip);
1126 		}
1127 	}
1128 }
1129