xref: /titanic_41/usr/src/uts/sun4v/io/vldc.c (revision 28cdc3d776761766afeb198769d1b70ed7e0f2e1)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/file.h>
31 #include <sys/errno.h>
32 #include <sys/uio.h>
33 #include <sys/open.h>
34 #include <sys/cred.h>
35 #include <sys/kmem.h>
36 #include <sys/conf.h>
37 #include <sys/cmn_err.h>
38 #include <sys/ksynch.h>
39 #include <sys/modctl.h>
40 #include <sys/stat.h>			/* needed for S_IFBLK and S_IFCHR */
41 #include <sys/debug.h>
42 #include <sys/sysmacros.h>
43 #include <sys/types.h>
44 #include <sys/cred.h>
45 #include <sys/promif.h>
46 #include <sys/ddi.h>
47 #include <sys/sunddi.h>
48 #include <sys/cyclic.h>
49 #include <sys/note.h>
50 #include <sys/mach_descrip.h>
51 #include <sys/mdeg.h>
52 #include <sys/ldc.h>
53 #include <sys/vldc_impl.h>
54 
55 /*
56  * Function prototypes.
57  */
58 
59 /* DDI entrypoints */
60 static int vldc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
61 static int vldc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
62 static int vldc_open(dev_t *devp, int flag, int otyp, cred_t *cred);
63 static int vldc_close(dev_t dev, int flag, int otyp, cred_t *cred);
64 static int vldc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
65     cred_t *credp, int *rvalp);
66 static int vldc_read(dev_t dev, struct uio *uiop, cred_t *credp);
67 static int vldc_write(dev_t dev, struct uio *uiop, cred_t *credp);
68 static int vldc_chpoll(dev_t dev, short events, int anyyet,
69     short *reventsp, struct pollhead **phpp);
70 
71 /* Internal functions */
72 static uint_t i_vldc_cb(uint64_t event, caddr_t arg);
73 static int i_vldc_mdeg_cb(void *cb_argp, mdeg_result_t *resp);
74 static int i_vldc_mdeg_register(vldc_t *vldcp);
75 static int i_vldc_mdeg_unregister(vldc_t *vldcp);
76 static int i_vldc_add_port(vldc_t *vldcp, md_t *mdp, mde_cookie_t node);
77 static int i_vldc_remove_port(vldc_t *vldcp, uint_t portno);
78 static int i_vldc_close_port(vldc_t *vldcp, uint_t portno);
79 
80 /* soft state structure */
81 static void *vldc_ssp;
82 
83 /*
84  * Matching criteria passed to the MDEG to register interest
85  * in changes to 'virtual-device-port' nodes identified by their
86  * 'id' property.
87  */
88 static md_prop_match_t vport_prop_match[] = {
89 	{ MDET_PROP_VAL,    "id"   },
90 	{ MDET_LIST_END,    NULL    }
91 };
92 
93 static mdeg_node_match_t vport_match = { "virtual-device-port",
94 					vport_prop_match };
95 
96 /*
97  * Specification of an MD node passed to the MDEG to filter any
98  * 'virtual-device-port' nodes that do not belong to the specified
99  * node. This template is copied for each vldc instance and filled
100  * in with the appropriate 'name' and 'cfg-handle' values before
101  * being passed to the MDEG.
102  */
103 static mdeg_prop_spec_t vldc_prop_template[] = {
104 	{ MDET_PROP_STR,    "name",		NULL	},
105 	{ MDET_PROP_VAL,    "cfg-handle",	NULL    },
106 	{ MDET_LIST_END,    NULL,		NULL    }
107 };
108 
109 #define	VLDC_MDEG_PROP_NAME(specp)		((specp)[0].ps_str)
110 #define	VLDC_SET_MDEG_PROP_NAME(specp, name)	((specp)[0].ps_str = (name))
111 #define	VLDC_SET_MDEG_PROP_INST(specp, inst)	((specp)[1].ps_val = (inst))
112 
113 
114 static struct cb_ops vldc_cb_ops = {
115 	vldc_open,	/* open */
116 	vldc_close,	/* close */
117 	nodev,		/* strategy */
118 	nodev,		/* print */
119 	nodev,		/* dump */
120 	vldc_read,	/* read */
121 	vldc_write,	/* write */
122 	vldc_ioctl,	/* ioctl */
123 	nodev,		/* devmap */
124 	nodev,		/* mmap */
125 	ddi_segmap,	/* segmap */
126 	vldc_chpoll,	/* chpoll */
127 	ddi_prop_op,	/* prop_op */
128 	NULL,		/* stream */
129 	D_NEW | D_MP	/* flag */
130 };
131 
132 static struct dev_ops vldc_ops = {
133 	DEVO_REV,		/* rev */
134 	0,			/* ref count */
135 	ddi_getinfo_1to1,	/* getinfo */
136 	nulldev,		/* identify */
137 	nulldev,		/* probe */
138 	vldc_attach,		/* attach */
139 	vldc_detach,		/* detach */
140 	nodev,			/* reset */
141 	&vldc_cb_ops,		/* cb_ops */
142 	(struct bus_ops *)NULL	/* bus_ops */
143 };
144 
145 extern struct mod_ops mod_driverops;
146 
147 static struct modldrv md = {
148 	&mod_driverops, 			/* Type - it is a driver */
149 	"sun4v Virtual LDC Driver %I%",	/* Name of the module */
150 	&vldc_ops,				/* driver specific ops */
151 };
152 
153 static struct modlinkage ml = {
154 	MODREV_1,
155 	&md,
156 	NULL
157 };
158 
159 /* maximum MTU and cookie size tunables */
160 uint32_t vldc_max_mtu = VLDC_MAX_MTU;
161 uint64_t vldc_max_cookie = VLDC_MAX_COOKIE;
162 
163 
164 #ifdef DEBUG
165 
166 /*
167  * Print debug messages
168  *
169  * set vldcdbg to 0x7 to enable all messages
170  *
171  * 0x4 - Warnings
172  * 0x2 - All debug messages (most verbose)
173  * 0x1 - Minimal debug messages
174  */
175 
176 int vldcdbg = 0x0;
177 
178 static void
179 vldcdebug(const char *fmt, ...)
180 {
181 	char buf[512];
182 	va_list ap;
183 
184 	va_start(ap, fmt);
185 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
186 	va_end(ap);
187 
188 	cmn_err(CE_CONT, "?%s", buf);
189 }
190 
191 #define	D1	if (vldcdbg & 0x01) vldcdebug
192 #define	D2	if (vldcdbg & 0x02) vldcdebug
193 #define	DWARN	if (vldcdbg & 0x04) vldcdebug
194 
195 #else /* not DEBUG */
196 
197 #define	D1	if (0) printf
198 #define	D2	if (0) printf
199 #define	DWARN	if (0) printf
200 
201 #endif /* not DEBUG */
202 
203 
204 /* _init(9E): initialize the loadable module */
205 int
206 _init(void)
207 {
208 	int error;
209 
210 	/* init the soft state structure */
211 	error = ddi_soft_state_init(&vldc_ssp, sizeof (vldc_t), 1);
212 	if (error != 0) {
213 		return (error);
214 	}
215 
216 	/* Link the driver into the system */
217 	error = mod_install(&ml);
218 
219 	return (error);
220 }
221 
222 /* _info(9E): return information about the loadable module */
223 int
224 _info(struct modinfo *modinfop)
225 {
226 	/* Report status of the dynamically loadable driver module */
227 	return (mod_info(&ml, modinfop));
228 }
229 
230 /* _fini(9E): prepare the module for unloading. */
231 int
232 _fini(void)
233 {
234 	int error;
235 
236 	/* Unlink the driver module from the system */
237 	if ((error = mod_remove(&ml)) == 0) {
238 		/*
239 		 * We have successfully "removed" the driver.
240 		 * destroy soft state
241 		 */
242 		ddi_soft_state_fini(&vldc_ssp);
243 	}
244 
245 	return (error);
246 }
247 
248 /* ldc callback */
249 static uint_t
250 i_vldc_cb(uint64_t event, caddr_t arg)
251 {
252 	vldc_port_t *vport = (vldc_port_t *)arg;
253 	short pollevents = 0;
254 	int rv;
255 
256 	D1("i_vldc_cb: callback invoked port=%d, event=0x%lx\n",
257 	    vport->number, event);
258 
259 	if (event & LDC_EVT_UP) {
260 		pollevents |= POLLOUT;
261 		vport->hanged_up = B_FALSE;
262 
263 	} else if (event & LDC_EVT_DOWN) {
264 		pollevents |= POLLHUP;
265 		vport->hanged_up = B_TRUE;
266 
267 	} else if (event & LDC_EVT_RESET) {
268 		/* do an ldc_up because we can't be sure the other side will */
269 		if ((rv = ldc_up(vport->ldc_handle)) != 0)
270 			if (rv != ECONNREFUSED)
271 				DWARN("i_vldc_cb: port@%d failed to"
272 				    " bring up LDC channel=%ld, err=%d\n",
273 				    vport->number, vport->ldc_id, rv);
274 	}
275 
276 	if (event & LDC_EVT_READ)
277 		pollevents |= POLLIN;
278 
279 	if (pollevents != 0) {
280 		D1("i_vldc_cb: port@%d pollwakeup=0x%x\n",
281 		    vport->number, pollevents);
282 		pollwakeup(&vport->poll, pollevents);
283 	}
284 
285 	return (LDC_SUCCESS);
286 }
287 
288 /* mdeg callback */
289 static int
290 i_vldc_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
291 {
292 	vldc_t		*vldcp;
293 	int		idx;
294 	uint64_t	portno;
295 	int		rv;
296 	md_t		*mdp;
297 	mde_cookie_t	node;
298 
299 	if (resp == NULL) {
300 		D1("i_vldc_mdeg_cb: no result returned\n");
301 		return (MDEG_FAILURE);
302 	}
303 
304 	vldcp = (vldc_t *)cb_argp;
305 
306 	mutex_enter(&vldcp->lock);
307 	if (vldcp->detaching == B_TRUE) {
308 		D1("i_vldc_mdeg_cb: detach in progress\n");
309 		mutex_exit(&vldcp->lock);
310 		return (MDEG_FAILURE);
311 	}
312 
313 	D1("i_vldc_mdeg_cb: added=%d, removed=%d, matched=%d\n",
314 	    resp->added.nelem, resp->removed.nelem, resp->match_prev.nelem);
315 
316 	/* process added ports */
317 	for (idx = 0; idx < resp->added.nelem; idx++) {
318 		mdp = resp->added.mdp;
319 		node = resp->added.mdep[idx];
320 
321 		D1("i_vldc_mdeg_cb: processing added node 0x%lx\n", node);
322 
323 		/* attempt to add a port */
324 		if ((rv = i_vldc_add_port(vldcp, mdp, node)) != MDEG_SUCCESS) {
325 			cmn_err(CE_NOTE, "?i_vldc_mdeg_cb: unable to add port, "
326 			    "err = %d", rv);
327 		}
328 	}
329 
330 	/* process removed ports */
331 	for (idx = 0; idx < resp->removed.nelem; idx++) {
332 		mdp = resp->removed.mdp;
333 		node = resp->removed.mdep[idx];
334 
335 		D1("i_vldc_mdeg_cb: processing removed node 0x%lx\n", node);
336 
337 		/* read in the port's id property */
338 		if (md_get_prop_val(mdp, node, "id", &portno)) {
339 			cmn_err(CE_NOTE, "?i_vldc_mdeg_cb: node 0x%lx of "
340 			    "removed list has no 'id' property", node);
341 			continue;
342 		}
343 
344 		/* attempt to remove a port */
345 		if ((rv = i_vldc_remove_port(vldcp, portno)) != 0) {
346 			cmn_err(CE_NOTE, "?i_vldc_mdeg_cb: unable to remove "
347 			    "port %lu, err %d", portno, rv);
348 		}
349 	}
350 
351 	/*
352 	 * Currently no support for updating already active ports. So, ignore
353 	 * the match_curr and match_prev arrays for now.
354 	 */
355 
356 	mutex_exit(&vldcp->lock);
357 
358 	return (MDEG_SUCCESS);
359 }
360 
361 /* register callback to mdeg */
362 static int
363 i_vldc_mdeg_register(vldc_t *vldcp)
364 {
365 	mdeg_prop_spec_t *pspecp;
366 	mdeg_node_spec_t *inst_specp;
367 	mdeg_handle_t	mdeg_hdl;
368 	size_t		templatesz;
369 	int		inst;
370 	char		*name;
371 	size_t		namesz;
372 	char		*nameprop;
373 	int		rv;
374 
375 	/* get the unique vldc instance assigned by the LDom manager */
376 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vldcp->dip,
377 	    DDI_PROP_DONTPASS, "reg", -1);
378 	if (inst == -1) {
379 		cmn_err(CE_NOTE, "?vldc%d has no 'reg' property",
380 		    ddi_get_instance(vldcp->dip));
381 		return (DDI_FAILURE);
382 	}
383 
384 	/* get the name of the vldc instance */
385 	rv = ddi_prop_lookup_string(DDI_DEV_T_ANY, vldcp->dip,
386 	    DDI_PROP_DONTPASS, "name", &nameprop);
387 	if (rv != DDI_PROP_SUCCESS) {
388 		cmn_err(CE_NOTE, "?vldc%d has no 'name' property",
389 		    ddi_get_instance(vldcp->dip));
390 		return (DDI_FAILURE);
391 	}
392 
393 	D1("i_vldc_mdeg_register: name=%s, instance=%d\n", nameprop, inst);
394 
395 	/*
396 	 * Allocate and initialize a per-instance copy
397 	 * of the global property spec array that will
398 	 * uniquely identify this vldc instance.
399 	 */
400 	templatesz = sizeof (vldc_prop_template);
401 	pspecp = kmem_alloc(templatesz, KM_SLEEP);
402 
403 	bcopy(vldc_prop_template, pspecp, templatesz);
404 
405 	/* copy in the name property */
406 	namesz = strlen(nameprop) + 1;
407 	name = kmem_alloc(namesz, KM_SLEEP);
408 
409 	bcopy(nameprop, name, namesz);
410 	VLDC_SET_MDEG_PROP_NAME(pspecp, name);
411 
412 	/* copy in the instance property */
413 	VLDC_SET_MDEG_PROP_INST(pspecp, inst);
414 
415 	/* initialize the complete prop spec structure */
416 	inst_specp = kmem_alloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
417 	inst_specp->namep = "virtual-device";
418 	inst_specp->specp = pspecp;
419 
420 	/* perform the registration */
421 	rv = mdeg_register(inst_specp, &vport_match, i_vldc_mdeg_cb,
422 	    vldcp, &mdeg_hdl);
423 
424 	if (rv != MDEG_SUCCESS) {
425 		cmn_err(CE_NOTE, "?i_vldc_mdeg_register: mdeg_register "
426 		    "failed, err = %d", rv);
427 		kmem_free(name, namesz);
428 		kmem_free(pspecp, templatesz);
429 		kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
430 		return (DDI_FAILURE);
431 	}
432 
433 	/* save off data that will be needed later */
434 	vldcp->inst_spec = inst_specp;
435 	vldcp->mdeg_hdl = mdeg_hdl;
436 
437 	return (DDI_SUCCESS);
438 }
439 
440 /* unregister callback from mdeg */
441 static int
442 i_vldc_mdeg_unregister(vldc_t *vldcp)
443 {
444 	char	*name;
445 	int	rv;
446 
447 	D1("i_vldc_mdeg_unregister: hdl=0x%lx\n", vldcp->mdeg_hdl);
448 
449 	rv = mdeg_unregister(vldcp->mdeg_hdl);
450 	if (rv != MDEG_SUCCESS) {
451 		return (rv);
452 	}
453 
454 	/*
455 	 * Clean up cached MDEG data
456 	 */
457 	name = VLDC_MDEG_PROP_NAME(vldcp->inst_spec->specp);
458 	if (name != NULL) {
459 		kmem_free(name, strlen(name) + 1);
460 	}
461 	kmem_free(vldcp->inst_spec->specp, sizeof (vldc_prop_template));
462 	vldcp->inst_spec->specp = NULL;
463 
464 	kmem_free(vldcp->inst_spec, sizeof (mdeg_node_spec_t));
465 	vldcp->inst_spec = NULL;
466 
467 	return (MDEG_SUCCESS);
468 }
469 
470 static int
471 i_vldc_get_port_channel(md_t *mdp, mde_cookie_t node, uint64_t *ldc_id)
472 {
473 	int num_nodes, nchan;
474 	size_t listsz;
475 	mde_cookie_t *listp;
476 
477 	/*
478 	 * Find the channel-endpoint node(s) (which should be under this
479 	 * port node) which contain the channel id(s).
480 	 */
481 	if ((num_nodes = md_node_count(mdp)) <= 0) {
482 		cmn_err(CE_NOTE, "?i_vldc_get_port_channel: invalid number of "
483 		    "channel-endpoint nodes found (%d)", num_nodes);
484 		return (-1);
485 	}
486 
487 	/* allocate space for node list */
488 	listsz = num_nodes * sizeof (mde_cookie_t);
489 	listp = kmem_alloc(listsz, KM_SLEEP);
490 
491 	nchan = md_scan_dag(mdp, node, md_find_name(mdp, "channel-endpoint"),
492 	    md_find_name(mdp, "fwd"), listp);
493 
494 	if (nchan <= 0) {
495 		cmn_err(CE_NOTE, "?i_vldc_get_port_channel: no channel-endpoint"
496 		    " nodes found");
497 		kmem_free(listp, listsz);
498 		return (-1);
499 	}
500 
501 	D2("i_vldc_get_port_channel: %d channel-endpoint nodes found", nchan);
502 
503 	/* use property from first node found */
504 	if (md_get_prop_val(mdp, listp[0], "id", ldc_id)) {
505 		cmn_err(CE_NOTE, "?i_vldc_get_port_channel: channel-endpoint "
506 		    "has no 'id' property");
507 		kmem_free(listp, listsz);
508 		return (-1);
509 	}
510 
511 	kmem_free(listp, listsz);
512 
513 	return (0);
514 }
515 
516 /* add a vldc port */
517 static int
518 i_vldc_add_port(vldc_t *vldcp, md_t *mdp, mde_cookie_t node)
519 {
520 	vldc_port_t	*vport;
521 	char		*sname;
522 	uint64_t	portno;
523 	int		vldc_inst;
524 	minor_t		minor;
525 	int		minor_idx;
526 	boolean_t	new_minor;
527 	int		rv;
528 
529 	/* read in the port's id property */
530 	if (md_get_prop_val(mdp, node, "id", &portno)) {
531 		cmn_err(CE_NOTE, "?i_vldc_add_port: node 0x%lx of added "
532 		    "list has no 'id' property", node);
533 		return (MDEG_FAILURE);
534 	}
535 
536 	if (portno >= VLDC_MAX_PORTS) {
537 		cmn_err(CE_NOTE, "?i_vldc_add_port: found port number (%lu) "
538 		    "larger than maximum supported number of ports", portno);
539 		return (MDEG_FAILURE);
540 	}
541 
542 	vport = &(vldcp->port[portno]);
543 
544 	if (vport->minorp != NULL) {
545 		cmn_err(CE_NOTE, "?i_vldc_add_port: trying to add a port (%lu)"
546 		    " which is already bound", portno);
547 		return (MDEG_FAILURE);
548 	}
549 
550 	vport->number = portno;
551 
552 	/* get all channels for this device (currently only one) */
553 	if (i_vldc_get_port_channel(mdp, node, &vport->ldc_id) == -1) {
554 		return (MDEG_FAILURE);
555 	}
556 
557 	/* set the default MTU */
558 	vport->mtu = VLDC_DEFAULT_MTU;
559 
560 	/* get the service being exported by this port */
561 	if (md_get_prop_str(mdp, node, "vldc-svc-name", &sname)) {
562 		cmn_err(CE_NOTE, "?i_vldc_add_port: vdevice has no "
563 		    "'vldc-svc-name' property");
564 		return (MDEG_FAILURE);
565 	}
566 
567 	/* minor number look up */
568 	for (minor_idx = 0; minor_idx < vldcp->minors_assigned;
569 	    minor_idx++) {
570 		if (strcmp(vldcp->minor_tbl[minor_idx].sname, sname) == 0) {
571 			/* found previously assigned minor number */
572 			break;
573 		}
574 	}
575 
576 	new_minor = B_FALSE;
577 	if (minor_idx == vldcp->minors_assigned) {
578 		/* end of lookup - assign new minor number */
579 		if (vldcp->minors_assigned == VLDC_MAX_MINORS) {
580 			cmn_err(CE_NOTE, "?i_vldc_add_port: too many minor "
581 			    "nodes (%d)", minor_idx);
582 			return (MDEG_FAILURE);
583 		}
584 
585 		(void) strlcpy(vldcp->minor_tbl[minor_idx].sname,
586 		    sname, MAXPATHLEN);
587 
588 		vldcp->minors_assigned++;
589 		new_minor = B_TRUE;
590 	}
591 
592 	ASSERT(vldcp->minor_tbl[minor_idx].portno == VLDC_INVALID_PORTNO);
593 
594 	vport->minorp = &vldcp->minor_tbl[minor_idx];
595 	vldcp->minor_tbl[minor_idx].portno = portno;
596 	vldcp->minor_tbl[minor_idx].in_use = 0;
597 
598 	D1("i_vldc_add_port: port@%d  mtu=%d, ldc=%ld, service=%s\n",
599 	    vport->number, vport->mtu, vport->ldc_id, sname);
600 
601 	/*
602 	 * Create a minor node. The minor number is
603 	 * (vldc_inst << VLDC_INST_SHIFT) | minor_idx
604 	 */
605 	vldc_inst = ddi_get_instance(vldcp->dip);
606 
607 	minor = (vldc_inst << VLDC_INST_SHIFT) | (minor_idx);
608 
609 	rv = ddi_create_minor_node(vldcp->dip, sname, S_IFCHR,
610 	    minor, DDI_NT_SERIAL, 0);
611 
612 	if (rv != DDI_SUCCESS) {
613 		cmn_err(CE_NOTE, "?i_vldc_add_port: failed to create minor"
614 		    "node (%u), err = %d", minor, rv);
615 		vldcp->minor_tbl[minor_idx].portno = VLDC_INVALID_PORTNO;
616 		if (new_minor) {
617 			vldcp->minors_assigned--;
618 		}
619 		return (MDEG_FAILURE);
620 	}
621 
622 	/*
623 	 * The port is now bound to a minor node and is initially in the
624 	 * closed state.
625 	 */
626 	vport->status = VLDC_PORT_CLOSED;
627 
628 	D1("i_vldc_add_port: port %lu initialized\n", portno);
629 
630 	return (MDEG_SUCCESS);
631 }
632 
633 /* remove a vldc port */
634 static int
635 i_vldc_remove_port(vldc_t *vldcp, uint_t portno)
636 {
637 	vldc_port_t *vport;
638 	vldc_minor_t *vminor;
639 
640 	vport = &(vldcp->port[portno]);
641 	vminor = vport->minorp;
642 	if (vminor == NULL) {
643 		cmn_err(CE_NOTE, "?i_vldc_remove_port: trying to remove a "
644 		    "port (%u) which is not bound", portno);
645 		return (MDEG_FAILURE);
646 	}
647 
648 	/*
649 	 * Make sure that all new attempts to open or use the minor node
650 	 * associated with the port will fail.
651 	 */
652 	mutex_enter(&vminor->lock);
653 	vminor->portno = VLDC_INVALID_PORTNO;
654 	mutex_exit(&vminor->lock);
655 
656 	/* send hangup to anyone polling */
657 	pollwakeup(&vport->poll, POLLHUP);
658 
659 	/* Now wait for all current users of the minor node to finish. */
660 	mutex_enter(&vminor->lock);
661 	while (vminor->in_use > 0) {
662 		cv_wait(&vminor->cv, &vminor->lock);
663 	}
664 
665 	if ((vport->status == VLDC_PORT_READY) ||
666 	    (vport->status == VLDC_PORT_OPEN)) {
667 		/* close the port before it is torn down */
668 		(void) i_vldc_close_port(vldcp, portno);
669 	}
670 
671 	/* remove minor node */
672 	ddi_remove_minor_node(vldcp->dip, vport->minorp->sname);
673 	vport->minorp = NULL;
674 
675 	mutex_exit(&vminor->lock);
676 
677 	D1("i_vldc_remove_port: removed vldc port %u\n", portno);
678 
679 	return (MDEG_SUCCESS);
680 }
681 
682 /* close a ldc channel */
683 static int
684 i_vldc_ldc_close(vldc_port_t *vport)
685 {
686 	int rv = 0;
687 	int err;
688 
689 	err = ldc_close(vport->ldc_handle);
690 	if (err != 0)
691 		rv = err;
692 	err = ldc_unreg_callback(vport->ldc_handle);
693 	if ((err != 0) && (rv != 0))
694 		rv = err;
695 	err = ldc_fini(vport->ldc_handle);
696 	if ((err != 0) && (rv != 0))
697 		rv = err;
698 
699 	return (rv);
700 }
701 
702 /* close a vldc port */
703 static int
704 i_vldc_close_port(vldc_t *vldcp, uint_t portno)
705 {
706 	vldc_port_t *vport;
707 	int rv;
708 
709 	vport = &(vldcp->port[portno]);
710 
711 	ASSERT(MUTEX_HELD(&vport->minorp->lock));
712 
713 	if (vport->status == VLDC_PORT_CLOSED) {
714 		/* nothing to do */
715 		DWARN("i_vldc_close_port: port %d in an unexpected "
716 		    "state (%d)\n", portno, vport->status);
717 		return (DDI_SUCCESS);
718 	}
719 
720 	rv = DDI_SUCCESS;
721 	if (vport->status == VLDC_PORT_READY) {
722 		rv = i_vldc_ldc_close(vport);
723 	} else {
724 		ASSERT(vport->status == VLDC_PORT_OPEN);
725 	}
726 
727 	/* free memory */
728 	kmem_free(vport->send_buf, vport->mtu);
729 	kmem_free(vport->recv_buf, vport->mtu);
730 
731 	vport->status = VLDC_PORT_CLOSED;
732 
733 	return (rv);
734 }
735 
736 /*
737  * attach(9E): attach a device to the system.
738  * called once for each instance of the device on the system.
739  */
740 static int
741 vldc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
742 {
743 	int 	i, instance;
744 	vldc_t	*vldcp;
745 
746 	switch (cmd) {
747 
748 	case DDI_ATTACH:
749 
750 		instance = ddi_get_instance(dip);
751 
752 		if (ddi_soft_state_zalloc(vldc_ssp, instance) != DDI_SUCCESS) {
753 			return (DDI_FAILURE);
754 		}
755 
756 		vldcp = ddi_get_soft_state(vldc_ssp, instance);
757 		if (vldcp == NULL) {
758 			ddi_soft_state_free(vldc_ssp, instance);
759 			return (ENXIO);
760 		}
761 
762 		D1("vldc_attach: DDI_ATTACH instance=%d\n", instance);
763 
764 		mutex_init(&vldcp->lock, NULL, MUTEX_DRIVER, NULL);
765 		vldcp->dip = dip;
766 		vldcp->detaching = B_FALSE;
767 
768 		for (i = 0; i < VLDC_MAX_PORTS; i++) {
769 			/* No minor node association to start with */
770 			vldcp->port[i].minorp = NULL;
771 		}
772 
773 		for (i = 0; i < VLDC_MAX_MINORS; i++) {
774 			mutex_init(&(vldcp->minor_tbl[i].lock), NULL,
775 			    MUTEX_DRIVER, NULL);
776 			cv_init(&(vldcp->minor_tbl[i].cv), NULL,
777 			    CV_DRIVER, NULL);
778 			/* No port association to start with */
779 			vldcp->minor_tbl[i].portno = VLDC_INVALID_PORTNO;
780 		}
781 
782 		/* Register for MD update notification */
783 		if (i_vldc_mdeg_register(vldcp) != DDI_SUCCESS) {
784 			ddi_soft_state_free(vldc_ssp, instance);
785 			return (DDI_FAILURE);
786 		}
787 
788 		return (DDI_SUCCESS);
789 
790 	case DDI_RESUME:
791 
792 		return (DDI_SUCCESS);
793 
794 	default:
795 
796 		return (DDI_FAILURE);
797 	}
798 }
799 
800 /*
801  * detach(9E): detach a device from the system.
802  */
803 static int
804 vldc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
805 {
806 	int 		i, instance;
807 	vldc_t		*vldcp;
808 
809 	switch (cmd) {
810 
811 	case DDI_DETACH:
812 
813 		instance = ddi_get_instance(dip);
814 
815 		vldcp = ddi_get_soft_state(vldc_ssp, instance);
816 		if (vldcp == NULL) {
817 			return (DDI_FAILURE);
818 		}
819 
820 		D1("vldc_detach: DDI_DETACH instance=%d\n", instance);
821 
822 		mutex_enter(&vldcp->lock);
823 
824 		/* Fail the detach if all ports have not been removed. */
825 		for (i = 0; i < VLDC_MAX_MINORS; i++) {
826 			if (vldcp->minor_tbl[i].portno != VLDC_INVALID_PORTNO) {
827 				D1("vldc_detach: vldc@%d:%d is bound, "
828 				    "detach failed\n",
829 				    instance, vldcp->minor_tbl[i].portno);
830 				mutex_exit(&vldcp->lock);
831 				return (DDI_FAILURE);
832 			}
833 		}
834 
835 		/*
836 		 * Prevent MDEG from adding new ports before the callback can
837 		 * be unregistered. The lock can't be held accross the
838 		 * unregistration call because a callback may be in progress
839 		 * and blocked on the lock.
840 		 */
841 		vldcp->detaching = B_TRUE;
842 
843 		mutex_exit(&vldcp->lock);
844 
845 		if (i_vldc_mdeg_unregister(vldcp) != MDEG_SUCCESS) {
846 			vldcp->detaching = B_FALSE;
847 			return (DDI_FAILURE);
848 		}
849 
850 		/* Tear down all bound ports and free resources. */
851 		for (i = 0; i < VLDC_MAX_MINORS; i++) {
852 			if (vldcp->minor_tbl[i].portno != VLDC_INVALID_PORTNO) {
853 				(void) i_vldc_remove_port(vldcp, i);
854 			}
855 			mutex_destroy(&(vldcp->minor_tbl[i].lock));
856 			cv_destroy(&(vldcp->minor_tbl[i].cv));
857 		}
858 
859 		mutex_destroy(&vldcp->lock);
860 		ddi_soft_state_free(vldc_ssp, instance);
861 
862 		return (DDI_SUCCESS);
863 
864 	case DDI_SUSPEND:
865 
866 		return (DDI_SUCCESS);
867 
868 	default:
869 
870 		return (DDI_FAILURE);
871 	}
872 }
873 
874 /* cb_open */
875 static int
876 vldc_open(dev_t *devp, int flag, int otyp, cred_t *cred)
877 {
878 	_NOTE(ARGUNUSED(flag, otyp, cred))
879 
880 	int instance;
881 	minor_t minor;
882 	uint64_t portno;
883 	vldc_t *vldcp;
884 	vldc_port_t *vport;
885 	vldc_minor_t *vminor;
886 
887 	minor = getminor(*devp);
888 	instance = VLDCINST(minor);
889 	vldcp = ddi_get_soft_state(vldc_ssp, instance);
890 	if (vldcp == NULL)
891 		return (ENXIO);
892 
893 	vminor = VLDCMINOR(vldcp, minor);
894 	mutex_enter(&vminor->lock);
895 	portno = vminor->portno;
896 	if (portno == VLDC_INVALID_PORTNO) {
897 		mutex_exit(&vminor->lock);
898 		return (ENXIO);
899 	}
900 
901 	vport = &(vldcp->port[portno]);
902 
903 	D1("vldc_open: opening vldc@%d:%lu\n", instance, portno);
904 
905 	if (vport->status != VLDC_PORT_CLOSED) {
906 		mutex_exit(&vminor->lock);
907 		return (EBUSY);
908 	}
909 
910 	vport->recv_buf = kmem_alloc(vport->mtu, KM_SLEEP);
911 	vport->send_buf = kmem_alloc(vport->mtu, KM_SLEEP);
912 
913 	vport->is_stream = B_FALSE;	/* assume not a stream */
914 	vport->hanged_up = B_FALSE;
915 
916 	vport->status = VLDC_PORT_OPEN;
917 
918 	mutex_exit(&vminor->lock);
919 
920 	return (DDI_SUCCESS);
921 }
922 
923 /* cb_close */
924 static int
925 vldc_close(dev_t dev, int flag, int otyp, cred_t *cred)
926 {
927 	_NOTE(ARGUNUSED(flag, otyp, cred))
928 
929 	int instance;
930 	minor_t minor;
931 	uint64_t portno;
932 	vldc_t *vldcp;
933 	vldc_minor_t *vminor;
934 	int rv;
935 
936 	minor = getminor(dev);
937 	instance = VLDCINST(minor);
938 	vldcp = ddi_get_soft_state(vldc_ssp, instance);
939 	if (vldcp == NULL) {
940 		return (ENXIO);
941 	}
942 
943 	vminor = VLDCMINOR(vldcp, minor);
944 	mutex_enter(&vminor->lock);
945 	portno = vminor->portno;
946 	if (portno == VLDC_INVALID_PORTNO) {
947 		mutex_exit(&vminor->lock);
948 		return (ENOLINK);
949 	}
950 
951 	D1("vldc_close: closing vldc@%d:%lu\n", instance, portno);
952 
953 	rv = i_vldc_close_port(vldcp, portno);
954 
955 	mutex_exit(&vminor->lock);
956 
957 	return (rv);
958 }
959 
960 static int
961 vldc_set_ldc_mode(vldc_port_t *vport, vldc_t *vldcp, int channel_mode)
962 {
963 	ldc_attr_t attr;
964 	int rv;
965 
966 	ASSERT(MUTEX_HELD(&vport->minorp->lock));
967 
968 	/* validate mode */
969 	switch (channel_mode) {
970 	case LDC_MODE_STREAM:
971 		vport->is_stream = B_TRUE;
972 		break;
973 	case LDC_MODE_RAW:
974 	case LDC_MODE_UNRELIABLE:
975 	case LDC_MODE_RELIABLE:
976 		vport->is_stream = B_FALSE;
977 		break;
978 	default:
979 		return (EINVAL);
980 	}
981 
982 	if (vport->status == VLDC_PORT_READY) {
983 		rv = i_vldc_ldc_close(vport);
984 		vport->status = VLDC_PORT_OPEN;
985 		if (rv != 0) {
986 			DWARN("vldc_set_ldc_mode: i_vldc_ldc_close "
987 			    "failed, rv=%d\n", rv);
988 			return (rv);
989 		}
990 	}
991 
992 	D1("vldc_set_ldc_mode: vport status %d, mode %d\n",
993 	    vport->status, channel_mode);
994 
995 	vport->ldc_mode = channel_mode;
996 
997 	/* initialize the channel */
998 	attr.devclass = LDC_DEV_SERIAL;
999 	attr.instance = ddi_get_instance(vldcp->dip);
1000 	attr.qlen = VLDC_QUEUE_LEN;
1001 	attr.mode = vport->ldc_mode;
1002 
1003 	if ((rv = ldc_init(vport->ldc_id, &attr,
1004 	    &vport->ldc_handle)) != 0) {
1005 		DWARN("vldc_ioctl_opt_op: ldc_init failed, rv=%d\n", rv);
1006 		goto error_init;
1007 	}
1008 
1009 	/* register it */
1010 	if ((rv = ldc_reg_callback(vport->ldc_handle,
1011 	    i_vldc_cb, (caddr_t)vport)) != 0) {
1012 		DWARN("vldc_ioctl_opt_op: ldc_reg_callback failed, rv=%d\n",
1013 		    rv);
1014 		goto error_reg;
1015 	}
1016 
1017 	/* open the channel */
1018 	if ((rv = ldc_open(vport->ldc_handle)) != 0) {
1019 		DWARN("vldc_ioctl_opt_op: ldc_open failed, rv=%d\n", rv);
1020 		goto error_open;
1021 	}
1022 
1023 	vport->status = VLDC_PORT_READY;
1024 
1025 	/*
1026 	 * Attempt to bring the channel up, but do not
1027 	 * fail if the other end is not up yet.
1028 	 */
1029 	rv = ldc_up(vport->ldc_handle);
1030 
1031 	if (rv == ECONNREFUSED) {
1032 		D1("vldc_ioctl_opt_op: remote endpoint not up yet\n");
1033 	} else if (rv != 0) {
1034 		DWARN("vldc_ioctl_opt_op: ldc_up failed, rv=%d\n", rv);
1035 		goto error_up;
1036 	}
1037 
1038 	D1("vldc_ioctl_opt_op: ldc %ld initialized successfully\n",
1039 	    vport->ldc_id);
1040 
1041 	return (0);
1042 
1043 error_up:
1044 	vport->status = VLDC_PORT_OPEN;
1045 	(void) ldc_close(vport->ldc_handle);
1046 error_open:
1047 	(void) ldc_unreg_callback(vport->ldc_handle);
1048 error_reg:
1049 	(void) ldc_fini(vport->ldc_handle);
1050 error_init:
1051 	return (rv);
1052 }
1053 
1054 /* ioctl to read cookie */
1055 static int
1056 i_vldc_ioctl_read_cookie(vldc_port_t *vport, int vldc_instance, void *arg,
1057     int mode)
1058 {
1059 	vldc_data_t copy_info;
1060 	caddr_t buf;
1061 	uint64_t len;
1062 	int rv;
1063 
1064 	if (ddi_copyin(arg, &copy_info, sizeof (copy_info), mode) == -1) {
1065 		return (EFAULT);
1066 	}
1067 
1068 	len = copy_info.length;
1069 	if (len > vldc_max_cookie) {
1070 		return (EINVAL);
1071 	}
1072 
1073 	/* allocate a temporary buffer */
1074 	buf = kmem_alloc(len, KM_SLEEP);
1075 
1076 	mutex_enter(&vport->minorp->lock);
1077 
1078 	D2("i_vldc_ioctl_read_cookie: vldc@%d:%d reading from 0x%lx "
1079 	    "size 0x%lx to 0x%lx\n", vldc_instance, vport->number,
1080 	    copy_info.dst_addr, copy_info.length, copy_info.src_addr);
1081 
1082 	/* read from the HV into the temporary buffer */
1083 	rv = ldc_mem_rdwr_pa(vport->ldc_handle, buf, &len,
1084 	    (caddr_t)copy_info.dst_addr, LDC_COPY_IN);
1085 	if (rv != 0) {
1086 		DWARN("i_vldc_ioctl_read_cookie: vldc@%d:%d cannot read "
1087 		    "address 0x%lx, rv=%d\n", vldc_instance, vport->number,
1088 		    copy_info.dst_addr, rv);
1089 		mutex_exit(&vport->minorp->lock);
1090 		kmem_free(buf, copy_info.length);
1091 		return (EFAULT);
1092 	}
1093 
1094 	D2("i_vldc_ioctl_read_cookie: vldc@%d:%d read succeeded\n",
1095 	    vldc_instance, vport->number);
1096 
1097 	mutex_exit(&vport->minorp->lock);
1098 
1099 	/* copy data from temporary buffer out to the caller and free buffer */
1100 	rv = ddi_copyout(buf, (caddr_t)copy_info.src_addr, len, mode);
1101 	kmem_free(buf, copy_info.length);
1102 	if (rv != 0) {
1103 		return (EFAULT);
1104 	}
1105 
1106 	/* set the structure to reflect outcome */
1107 	copy_info.length = len;
1108 	if (ddi_copyout(&copy_info, arg, sizeof (copy_info), mode) != 0) {
1109 		return (EFAULT);
1110 	}
1111 
1112 	return (0);
1113 }
1114 
1115 /* ioctl to write cookie */
1116 static int
1117 i_vldc_ioctl_write_cookie(vldc_port_t *vport, int vldc_instance, void *arg,
1118     int mode)
1119 {
1120 	vldc_data_t copy_info;
1121 	caddr_t buf;
1122 	uint64_t len;
1123 	int rv;
1124 
1125 	if (ddi_copyin((caddr_t)arg, &copy_info,
1126 	    sizeof (copy_info), mode) != 0) {
1127 		return (EFAULT);
1128 	}
1129 
1130 	len = copy_info.length;
1131 	if (len > vldc_max_cookie) {
1132 		return (EINVAL);
1133 	}
1134 
1135 	D2("i_vldc_ioctl_write_cookie: vldc@%d:%d writing 0x%lx size 0x%lx "
1136 	    "to 0x%lx\n", vldc_instance, vport->number, copy_info.src_addr,
1137 	    copy_info.length, copy_info.dst_addr);
1138 
1139 	/* allocate a temporary buffer */
1140 	buf = kmem_alloc(len, KM_SLEEP);
1141 
1142 	/* copy into the temporary buffer the data to be written to the HV */
1143 	if (ddi_copyin((caddr_t)copy_info.src_addr, buf,
1144 	    copy_info.length, mode) != 0) {
1145 		kmem_free(buf, copy_info.length);
1146 		return (EFAULT);
1147 	}
1148 
1149 	mutex_enter(&vport->minorp->lock);
1150 
1151 	/* write the data from the temporary buffer to the HV */
1152 	rv = ldc_mem_rdwr_pa(vport->ldc_handle, buf, &len,
1153 	    (caddr_t)copy_info.dst_addr, LDC_COPY_OUT);
1154 	if (rv != 0) {
1155 		DWARN("i_vldc_ioctl_write_cookie: vldc@%d:%d failed to write at"
1156 		    " address 0x%lx\n, rv=%d", vldc_instance, vport->number,
1157 		    copy_info.dst_addr, rv);
1158 		mutex_exit(&vport->minorp->lock);
1159 		kmem_free(buf, copy_info.length);
1160 		return (EFAULT);
1161 	}
1162 
1163 	D2("i_vldc_ioctl_write_cookie: vldc@%d:%d write succeeded\n",
1164 	    vldc_instance, vport->number);
1165 
1166 	mutex_exit(&vport->minorp->lock);
1167 
1168 	kmem_free(buf, copy_info.length);
1169 
1170 	/* set the structure to reflect outcome */
1171 	copy_info.length = len;
1172 	if (ddi_copyout(&copy_info, (caddr_t)arg,
1173 	    sizeof (copy_info), mode) != 0) {
1174 		return (EFAULT);
1175 	}
1176 
1177 	return (0);
1178 }
1179 
1180 /* vldc specific ioctl option commands */
1181 static int
1182 i_vldc_ioctl_opt_op(vldc_port_t *vport, vldc_t *vldcp, void *arg, int mode)
1183 {
1184 	vldc_opt_op_t 	vldc_cmd;
1185 	uint32_t	new_mtu;
1186 	int		rv = 0;
1187 
1188 	if (ddi_copyin(arg, &vldc_cmd, sizeof (vldc_cmd), mode) != 0) {
1189 		return (EFAULT);
1190 	}
1191 
1192 	D1("vldc_ioctl_opt_op: op %d\n", vldc_cmd.opt_sel);
1193 
1194 	switch (vldc_cmd.opt_sel) {
1195 
1196 	case VLDC_OPT_MTU_SZ:
1197 
1198 		if (vldc_cmd.op_sel == VLDC_OP_GET) {
1199 			vldc_cmd.opt_val = vport->mtu;
1200 			if (ddi_copyout(&vldc_cmd, arg,
1201 			    sizeof (vldc_cmd), mode) == -1) {
1202 				return (EFAULT);
1203 			}
1204 		} else {
1205 			new_mtu = vldc_cmd.opt_val;
1206 
1207 			if ((new_mtu < LDC_PACKET_SIZE) ||
1208 			    (new_mtu > vldc_max_mtu)) {
1209 				return (EINVAL);
1210 			}
1211 
1212 			mutex_enter(&vport->minorp->lock);
1213 
1214 			if ((vport->status != VLDC_PORT_CLOSED) &&
1215 			    (new_mtu != vport->mtu)) {
1216 				/*
1217 				 * The port has buffers allocated since it is
1218 				 * not closed plus the MTU size has changed.
1219 				 * Reallocate the buffers to the new MTU size.
1220 				 */
1221 				kmem_free(vport->recv_buf, vport->mtu);
1222 				vport->recv_buf = kmem_alloc(new_mtu, KM_SLEEP);
1223 
1224 				kmem_free(vport->send_buf, vport->mtu);
1225 				vport->send_buf = kmem_alloc(new_mtu, KM_SLEEP);
1226 
1227 				vport->mtu = new_mtu;
1228 			}
1229 
1230 			mutex_exit(&vport->minorp->lock);
1231 		}
1232 
1233 		break;
1234 
1235 	case VLDC_OPT_STATUS:
1236 
1237 		if (vldc_cmd.op_sel == VLDC_OP_GET) {
1238 			vldc_cmd.opt_val = vport->status;
1239 			if (ddi_copyout(&vldc_cmd, arg,
1240 			    sizeof (vldc_cmd), mode) == -1) {
1241 				return (EFAULT);
1242 			}
1243 		} else {
1244 			return (ENOTSUP);
1245 		}
1246 
1247 		break;
1248 
1249 	case VLDC_OPT_MODE:
1250 
1251 		if (vldc_cmd.op_sel == VLDC_OP_GET) {
1252 			vldc_cmd.opt_val = vport->ldc_mode;
1253 			if (ddi_copyout(&vldc_cmd, arg,
1254 			    sizeof (vldc_cmd), mode) == -1) {
1255 				return (EFAULT);
1256 			}
1257 		} else {
1258 			mutex_enter(&vport->minorp->lock);
1259 			rv = vldc_set_ldc_mode(vport, vldcp, vldc_cmd.opt_val);
1260 			mutex_exit(&vport->minorp->lock);
1261 		}
1262 
1263 		break;
1264 
1265 	default:
1266 
1267 		D1("vldc_ioctl_opt_op: unsupported op %d\n", vldc_cmd.opt_sel);
1268 		return (ENOTSUP);
1269 	}
1270 
1271 	return (rv);
1272 }
1273 
1274 /* cb_ioctl */
1275 static int
1276 vldc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1277     int *rvalp)
1278 {
1279 	_NOTE(ARGUNUSED(credp, rvalp))
1280 
1281 	int rv = EINVAL;
1282 	int instance;
1283 	minor_t minor;
1284 	uint64_t portno;
1285 	vldc_t *vldcp;
1286 	vldc_port_t *vport;
1287 	vldc_minor_t *vminor;
1288 
1289 	minor = getminor(dev);
1290 	instance = VLDCINST(minor);
1291 	vldcp = ddi_get_soft_state(vldc_ssp, instance);
1292 	if (vldcp == NULL) {
1293 		return (ENXIO);
1294 	}
1295 
1296 	vminor = VLDCMINOR(vldcp, minor);
1297 	mutex_enter(&vminor->lock);
1298 	portno = vminor->portno;
1299 	if (portno == VLDC_INVALID_PORTNO) {
1300 		mutex_exit(&vminor->lock);
1301 		return (ENOLINK);
1302 	}
1303 	vminor->in_use += 1;
1304 	mutex_exit(&vminor->lock);
1305 
1306 	vport = &(vldcp->port[portno]);
1307 
1308 	D1("vldc_ioctl: vldc@%d:%lu cmd=0x%x\n", instance, portno, cmd);
1309 
1310 	switch (cmd) {
1311 
1312 	case VLDC_IOCTL_OPT_OP:
1313 
1314 		rv = i_vldc_ioctl_opt_op(vport, vldcp, (void *)arg,  mode);
1315 		break;
1316 
1317 	case VLDC_IOCTL_READ_COOKIE:
1318 
1319 		rv = i_vldc_ioctl_read_cookie(vport, instance,
1320 		    (void *)arg, mode);
1321 		break;
1322 
1323 	case VLDC_IOCTL_WRITE_COOKIE:
1324 
1325 		rv = i_vldc_ioctl_write_cookie(vport, instance,
1326 		    (void *)arg, mode);
1327 		break;
1328 
1329 	default:
1330 
1331 		DWARN("vldc_ioctl: vldc@%d:%lu unknown cmd=0x%x\n",
1332 		    instance, portno, cmd);
1333 		rv = EINVAL;
1334 		break;
1335 	}
1336 
1337 	mutex_enter(&vminor->lock);
1338 	vminor->in_use -= 1;
1339 	if (vminor->in_use == 0) {
1340 		cv_signal(&vminor->cv);
1341 	}
1342 	mutex_exit(&vminor->lock);
1343 
1344 	D1("vldc_ioctl: rv=%d\n", rv);
1345 
1346 	return (rv);
1347 }
1348 
1349 /* cb_read */
1350 static int
1351 vldc_read(dev_t dev, struct uio *uiop, cred_t *credp)
1352 {
1353 	_NOTE(ARGUNUSED(credp))
1354 
1355 	int instance;
1356 	minor_t minor;
1357 	size_t size = 0;
1358 	uint64_t portno;
1359 	vldc_t *vldcp;
1360 	vldc_port_t *vport;
1361 	vldc_minor_t *vminor;
1362 	int rv = 0;
1363 
1364 	minor = getminor(dev);
1365 	instance = VLDCINST(minor);
1366 	vldcp = ddi_get_soft_state(vldc_ssp, instance);
1367 	if (vldcp == NULL) {
1368 		return (ENXIO);
1369 	}
1370 
1371 	vminor = VLDCMINOR(vldcp, minor);
1372 	mutex_enter(&vminor->lock);
1373 	portno = vminor->portno;
1374 	if (portno == VLDC_INVALID_PORTNO) {
1375 		mutex_exit(&vminor->lock);
1376 		return (ENOLINK);
1377 	}
1378 
1379 	D2("vldc_read: vldc@%d:%lu reading data\n", instance, portno);
1380 
1381 	vport = &(vldcp->port[portno]);
1382 
1383 	/* check the port status */
1384 	if (vport->status != VLDC_PORT_READY) {
1385 		DWARN("vldc_read: vldc@%d:%lu not in the ready state\n",
1386 		    instance, portno);
1387 		mutex_exit(&vminor->lock);
1388 		return (ENOTACTIVE);
1389 	}
1390 
1391 	/* read data */
1392 	size = MIN(vport->mtu, uiop->uio_resid);
1393 	rv = ldc_read(vport->ldc_handle, vport->recv_buf, &size);
1394 
1395 	D2("vldc_read: vldc@%d:%lu ldc_read size=%ld, rv=%d\n",
1396 	    instance, portno, size, rv);
1397 
1398 	if (rv == 0) {
1399 		if (size != 0) {
1400 			rv = uiomove(vport->recv_buf, size, UIO_READ, uiop);
1401 		} else {
1402 			rv = EWOULDBLOCK;
1403 		}
1404 	} else {
1405 		switch (rv) {
1406 		case ENOBUFS:
1407 			break;
1408 		case ETIMEDOUT:
1409 		case EWOULDBLOCK:
1410 			rv = EWOULDBLOCK;
1411 			break;
1412 		default:
1413 			rv = ECONNRESET;
1414 			break;
1415 		}
1416 	}
1417 
1418 	mutex_exit(&vminor->lock);
1419 
1420 	return (rv);
1421 }
1422 
1423 /* cb_write */
1424 static int
1425 vldc_write(dev_t dev, struct uio *uiop, cred_t *credp)
1426 {
1427 	_NOTE(ARGUNUSED(credp))
1428 
1429 	int instance;
1430 	minor_t minor;
1431 	size_t size;
1432 	size_t orig_size;
1433 	uint64_t portno;
1434 	vldc_t *vldcp;
1435 	vldc_port_t *vport;
1436 	vldc_minor_t *vminor;
1437 	int rv = EINVAL;
1438 
1439 	minor = getminor(dev);
1440 	instance = VLDCINST(minor);
1441 	vldcp = ddi_get_soft_state(vldc_ssp, instance);
1442 	if (vldcp == NULL) {
1443 		return (ENXIO);
1444 	}
1445 
1446 	vminor = VLDCMINOR(vldcp, minor);
1447 	mutex_enter(&vminor->lock);
1448 	portno = vminor->portno;
1449 	if (portno == VLDC_INVALID_PORTNO) {
1450 		mutex_exit(&vminor->lock);
1451 		return (ENOLINK);
1452 	}
1453 
1454 	vport = &(vldcp->port[portno]);
1455 
1456 	/* check the port status */
1457 	if (vport->status != VLDC_PORT_READY) {
1458 		DWARN("vldc_write: vldc@%d:%lu not in the ready state\n",
1459 		    instance, portno);
1460 		mutex_exit(&vminor->lock);
1461 		return (ENOTACTIVE);
1462 	}
1463 
1464 	orig_size = uiop->uio_resid;
1465 	size = orig_size;
1466 
1467 	if (size > vport->mtu) {
1468 		if (vport->is_stream) {
1469 			/* can only send MTU size at a time */
1470 			size = vport->mtu;
1471 		} else {
1472 			mutex_exit(&vminor->lock);
1473 			return (EMSGSIZE);
1474 		}
1475 	}
1476 
1477 	D2("vldc_write: vldc@%d:%lu writing %lu bytes\n", instance, portno,
1478 	    size);
1479 
1480 	rv = uiomove(vport->send_buf, size, UIO_WRITE, uiop);
1481 	if (rv == 0) {
1482 		rv = ldc_write(vport->ldc_handle, (caddr_t)vport->send_buf,
1483 			&size);
1484 		if (rv != 0) {
1485 			DWARN("vldc_write: vldc@%d:%lu failed writing %lu "
1486 			    "bytes rv=%d\n", instance, portno, size, rv);
1487 		}
1488 	} else {
1489 		size = 0;
1490 	}
1491 
1492 	mutex_exit(&vminor->lock);
1493 
1494 	/* resid is total number of bytes *not* sent */
1495 	uiop->uio_resid = orig_size - size;
1496 
1497 	return (rv);
1498 }
1499 
1500 /* cb_chpoll */
1501 static int
1502 vldc_chpoll(dev_t dev, short events, int anyyet,  short *reventsp,
1503     struct pollhead **phpp)
1504 {
1505 	int instance;
1506 	minor_t minor;
1507 	uint64_t portno;
1508 	vldc_t *vldcp;
1509 	vldc_port_t *vport;
1510 	vldc_minor_t *vminor;
1511 	ldc_status_t ldc_state;
1512 	boolean_t isempty;
1513 	int rv;
1514 
1515 	minor = getminor(dev);
1516 	instance = VLDCINST(minor);
1517 	vldcp = ddi_get_soft_state(vldc_ssp, instance);
1518 	if (vldcp == NULL) {
1519 		return (ENXIO);
1520 	}
1521 
1522 	vminor = VLDCMINOR(vldcp, minor);
1523 	mutex_enter(&vminor->lock);
1524 	portno = vminor->portno;
1525 	if (portno == VLDC_INVALID_PORTNO) {
1526 		mutex_exit(&vminor->lock);
1527 		return (ENOLINK);
1528 	}
1529 
1530 	vport = &(vldcp->port[portno]);
1531 
1532 	/* check the port status */
1533 	if (vport->status != VLDC_PORT_READY) {
1534 		mutex_exit(&vminor->lock);
1535 		return (ENOTACTIVE);
1536 	}
1537 
1538 	D2("vldc_chpoll: vldc@%d:%lu polling events 0x%x\n",
1539 	    instance, portno, events);
1540 
1541 	rv = ldc_status(vport->ldc_handle, &ldc_state);
1542 	if (rv != 0) {
1543 		DWARN("vldc_chpoll: vldc@%d:%lu could not get ldc status, "
1544 		    "rv=%d\n", instance, portno, rv);
1545 		mutex_exit(&vminor->lock);
1546 		return (EBADFD);
1547 	}
1548 
1549 	*reventsp = 0;
1550 
1551 	if (ldc_state == LDC_UP) {
1552 		/*
1553 		 * Check if the receive queue is empty and if not, signal that
1554 		 * there is data ready to read.
1555 		 */
1556 		if (events & POLLIN) {
1557 			if ((ldc_chkq(vport->ldc_handle, &isempty) == 0) &&
1558 			    (isempty == B_FALSE)) {
1559 				*reventsp |= POLLIN;
1560 			}
1561 		}
1562 
1563 		if (events & POLLOUT)
1564 			*reventsp |= POLLOUT;
1565 
1566 	} else if (vport->hanged_up) {
1567 		*reventsp |= POLLHUP;
1568 		vport->hanged_up = B_FALSE;
1569 	}
1570 
1571 	mutex_exit(&vminor->lock);
1572 
1573 	if (((*reventsp) == 0) && (!anyyet)) {
1574 		*phpp = &vport->poll;
1575 	}
1576 
1577 	D2("vldc_chpoll: vldc@%d:%lu ev=0x%x, rev=0x%x\n",
1578 	    instance, portno, events, *reventsp);
1579 
1580 	return (0);
1581 }
1582