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