xref: /onnv-gate/usr/src/uts/sun4v/io/qcn.c (revision 12429:ad96a5dc4f6f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51991Sheppo  * Common Development and Distribution License (the "License").
61991Sheppo  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211991Sheppo 
220Sstevel@tonic-gate /*
23*12429SZachary.Kissel@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * sun4v console driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/termios.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/kbio.h>
380Sstevel@tonic-gate #include <sys/stropts.h>
390Sstevel@tonic-gate #include <sys/stream.h>
400Sstevel@tonic-gate #include <sys/strsun.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/promif.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/cyclic.h>
460Sstevel@tonic-gate #include <sys/intr.h>
470Sstevel@tonic-gate #include <sys/spl.h>
480Sstevel@tonic-gate #include <sys/qcn.h>
490Sstevel@tonic-gate #include <sys/hypervisor_api.h>
502282Sjb145095 #include <sys/hsvc.h>
512282Sjb145095 #include <sys/machsystm.h>
525974Sjm22469 #include <sys/consdev.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* dev_ops and cb_ops for device driver */
550Sstevel@tonic-gate static int qcn_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
560Sstevel@tonic-gate static int qcn_attach(dev_info_t *, ddi_attach_cmd_t);
570Sstevel@tonic-gate static int qcn_detach(dev_info_t *, ddi_detach_cmd_t);
580Sstevel@tonic-gate static int qcn_open(queue_t *, dev_t *, int, int, cred_t *);
590Sstevel@tonic-gate static int qcn_close(queue_t *, int, cred_t *);
600Sstevel@tonic-gate static int qcn_wput(queue_t *, mblk_t *);
610Sstevel@tonic-gate static int qcn_wsrv(queue_t *);
620Sstevel@tonic-gate static int qcn_rsrv(queue_t *);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* other internal qcn routines */
650Sstevel@tonic-gate static void qcn_ioctl(queue_t *, mblk_t *);
660Sstevel@tonic-gate static void qcn_reioctl(void *);
670Sstevel@tonic-gate static void qcn_ack(mblk_t *, mblk_t *, uint_t);
680Sstevel@tonic-gate static void qcn_start(void);
692282Sjb145095 static int qcn_transmit_write(queue_t *, mblk_t *);
702282Sjb145095 static int qcn_transmit_putchr(queue_t *, mblk_t *);
712282Sjb145095 static void qcn_receive_read(void);
722282Sjb145095 static void qcn_receive_getchr(void);
730Sstevel@tonic-gate static void qcn_flush(void);
740Sstevel@tonic-gate static uint_t qcn_hi_intr(caddr_t arg);
750Sstevel@tonic-gate static uint_t qcn_soft_intr(caddr_t arg1, caddr_t arg2);
760Sstevel@tonic-gate 
775974Sjm22469 /* functions required for polled io */
785974Sjm22469 static boolean_t qcn_polledio_ischar(cons_polledio_arg_t arg);
795974Sjm22469 static int qcn_polledio_getchar(cons_polledio_arg_t arg);
805974Sjm22469 static void qcn_polledio_putchar(cons_polledio_arg_t arg, uchar_t c);
815974Sjm22469 static void qcn_polledio_enter(cons_polledio_arg_t arg);
825974Sjm22469 static void qcn_polledio_exit(cons_polledio_arg_t arg);
835974Sjm22469 
845974Sjm22469 
850Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static qcn_t *qcn_state;
880Sstevel@tonic-gate static uchar_t qcn_stopped = B_FALSE;
890Sstevel@tonic-gate static int qcn_timeout_period = 20;	/* time out in seconds */
900Sstevel@tonic-gate size_t qcn_input_dropped;	/* dropped input character counter */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #ifdef QCN_POLLING
930Sstevel@tonic-gate static void qcn_poll_handler(void *unused);
942954Sjb145095 static cyc_time_t qcn_poll_time;
950Sstevel@tonic-gate static cyc_handler_t qcn_poll_cychandler = {
960Sstevel@tonic-gate 	qcn_poll_handler,
970Sstevel@tonic-gate 	NULL,
980Sstevel@tonic-gate 	CY_LOW_LEVEL		/* XXX need softint to make this high */
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate static cyclic_id_t qcn_poll_cycid = CYCLIC_NONE;
1012954Sjb145095 static uint64_t	qcn_poll_interval = 5;  /* milli sec */
1021991Sheppo static uint64_t sb_interval = 0;
1031991Sheppo uint_t qcn_force_polling = 0;
1040Sstevel@tonic-gate #endif
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	QCN_MI_IDNUM		0xABCE
1070Sstevel@tonic-gate #define	QCN_MI_HIWAT		8192
1080Sstevel@tonic-gate #define	QCN_MI_LOWAT		128
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /* streams structures */
1110Sstevel@tonic-gate static struct module_info minfo = {
1120Sstevel@tonic-gate 	QCN_MI_IDNUM,	/* mi_idnum		*/
1130Sstevel@tonic-gate 	"qcn",		/* mi_idname		*/
1140Sstevel@tonic-gate 	0,		/* mi_minpsz		*/
1150Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz		*/
1160Sstevel@tonic-gate 	QCN_MI_HIWAT,	/* mi_hiwat		*/
1170Sstevel@tonic-gate 	QCN_MI_LOWAT	/* mi_lowat		*/
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static struct qinit rinit = {
1210Sstevel@tonic-gate 	putq,		/* qi_putp		*/
1220Sstevel@tonic-gate 	qcn_rsrv,	/* qi_srvp		*/
1230Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1240Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1250Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1260Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1270Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static struct qinit winit = {
1310Sstevel@tonic-gate 	qcn_wput,	/* qi_putp		*/
1320Sstevel@tonic-gate 	qcn_wsrv,	/* qi_srvp		*/
1330Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1340Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1350Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1360Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1370Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static struct streamtab qcnstrinfo = {
1410Sstevel@tonic-gate 	&rinit,
1420Sstevel@tonic-gate 	&winit,
1430Sstevel@tonic-gate 	NULL,
1440Sstevel@tonic-gate 	NULL
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* standard device driver structures */
1480Sstevel@tonic-gate static struct cb_ops qcn_cb_ops = {
1490Sstevel@tonic-gate 	nulldev,		/* open()		*/
1500Sstevel@tonic-gate 	nulldev,		/* close()		*/
1510Sstevel@tonic-gate 	nodev,			/* strategy()		*/
1520Sstevel@tonic-gate 	nodev,			/* print()		*/
1530Sstevel@tonic-gate 	nodev,			/* dump()		*/
1540Sstevel@tonic-gate 	nodev,			/* read()		*/
1550Sstevel@tonic-gate 	nodev,			/* write()		*/
1560Sstevel@tonic-gate 	nodev,			/* ioctl()		*/
1570Sstevel@tonic-gate 	nodev,			/* devmap()		*/
1580Sstevel@tonic-gate 	nodev,			/* mmap()		*/
1590Sstevel@tonic-gate 	nodev,			/* segmap()		*/
1600Sstevel@tonic-gate 	nochpoll,		/* poll()		*/
1610Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op()		*/
1620Sstevel@tonic-gate 	&qcnstrinfo,		/* cb_str		*/
1630Sstevel@tonic-gate 	D_NEW | D_MP		/* cb_flag		*/
1640Sstevel@tonic-gate };
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static struct dev_ops qcn_ops = {
1670Sstevel@tonic-gate 	DEVO_REV,
1680Sstevel@tonic-gate 	0,			/* refcnt		*/
1690Sstevel@tonic-gate 	qcn_getinfo,		/* getinfo()		*/
1700Sstevel@tonic-gate 	nulldev,		/* identify()		*/
1710Sstevel@tonic-gate 	nulldev,		/* probe()		*/
1720Sstevel@tonic-gate 	qcn_attach,		/* attach()		*/
1730Sstevel@tonic-gate 	qcn_detach,		/* detach()		*/
1740Sstevel@tonic-gate 	nodev,			/* reset()		*/
1750Sstevel@tonic-gate 	&qcn_cb_ops,		/* cb_ops		*/
1760Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* bus_ops		*/
1777656SSherry.Moore@Sun.COM 	NULL,			/* power()		*/
1787656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce()		*/
1790Sstevel@tonic-gate };
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate static struct modldrv modldrv = {
1820Sstevel@tonic-gate 	&mod_driverops,
1837656SSherry.Moore@Sun.COM 	"sun4v console driver",
1840Sstevel@tonic-gate 	&qcn_ops
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static struct modlinkage modlinkage = {
1880Sstevel@tonic-gate 	MODREV_1,
1890Sstevel@tonic-gate 	(void*)&modldrv,
1900Sstevel@tonic-gate 	NULL
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /* driver configuration routines */
1940Sstevel@tonic-gate int
_init(void)1950Sstevel@tonic-gate _init(void)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	int error;
1982282Sjb145095 	uint64_t	major, minor;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	qcn_state = kmem_zalloc(sizeof (qcn_t), KM_SLEEP);
2012282Sjb145095 	qcn_state->qcn_ring = contig_mem_alloc(RINGSIZE);
2022282Sjb145095 	if (qcn_state->qcn_ring == NULL)
2032282Sjb145095 		cmn_err(CE_PANIC, "console ring allocation failed");
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	error = mod_install(&modlinkage);
2062282Sjb145095 	if (error != 0) {
2072282Sjb145095 		contig_mem_free(qcn_state->qcn_ring, RINGSIZE);
2080Sstevel@tonic-gate 		kmem_free(qcn_state, sizeof (qcn_t));
2092282Sjb145095 		return (error);
2102282Sjb145095 	}
2112282Sjb145095 	/*
2122282Sjb145095 	 * check minor number to see if CONS_WRITE is supported
2132282Sjb145095 	 * if so, set up real address of the buffers for hv calls.
2142282Sjb145095 	 */
2150Sstevel@tonic-gate 
2162282Sjb145095 	if (((hsvc_version(HSVC_GROUP_CORE, &major, &minor) == 0) &&
2177656SSherry.Moore@Sun.COM 	    (major == QCN_API_MAJOR) && (minor >= QCN_API_MINOR))) {
2182282Sjb145095 		qcn_state->cons_write_buffer =
2197656SSherry.Moore@Sun.COM 		    contig_mem_alloc(CONS_WR_BUF_SIZE);
2202282Sjb145095 		if (qcn_state->cons_write_buffer != NULL) {
2212282Sjb145095 			qcn_state->cons_write_buf_ra =
2227656SSherry.Moore@Sun.COM 			    va_to_pa(qcn_state->cons_write_buffer);
2232282Sjb145095 			qcn_state->cons_transmit = qcn_transmit_write;
2242282Sjb145095 			qcn_state->cons_receive = qcn_receive_read;
2252282Sjb145095 			qcn_state->cons_read_buf_ra =
2267656SSherry.Moore@Sun.COM 			    va_to_pa((char *)RING_ADDR(qcn_state));
2272282Sjb145095 		}
2282282Sjb145095 	}
2292282Sjb145095 	if (qcn_state->cons_transmit == NULL) {
2302282Sjb145095 		qcn_state->cons_transmit = qcn_transmit_putchr;
2312282Sjb145095 		qcn_state->cons_receive = qcn_receive_getchr;
2322282Sjb145095 	}
2332282Sjb145095 	return (0);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate int
_fini(void)2370Sstevel@tonic-gate _fini(void)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	/* can't remove console driver */
2400Sstevel@tonic-gate 	return (EBUSY);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2440Sstevel@tonic-gate _info(struct modinfo *modinfop)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate static int
qcn_add_intrs(void)2500Sstevel@tonic-gate qcn_add_intrs(void)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	dev_info_t	*devinfo = qcn_state->qcn_dip;
2530Sstevel@tonic-gate 	int		actual, count = 0;
2540Sstevel@tonic-gate 	int 		x, y, rc, inum = 0;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/* get number of interrupts */
2580Sstevel@tonic-gate 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
2590Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (count == 0)) {
2600Sstevel@tonic-gate 		return (DDI_FAILURE);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/* Allocate an array of interrupt handles */
2640Sstevel@tonic-gate 	qcn_state->qcn_intr_size = count * sizeof (ddi_intr_handle_t);
2650Sstevel@tonic-gate 	qcn_state->qcn_htable = kmem_zalloc(qcn_state->qcn_intr_size, KM_SLEEP);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/* call ddi_intr_alloc() */
2680Sstevel@tonic-gate 	rc = ddi_intr_alloc(devinfo, qcn_state->qcn_htable,
2690Sstevel@tonic-gate 	    DDI_INTR_TYPE_FIXED, inum, count, &actual,
2700Sstevel@tonic-gate 	    DDI_INTR_ALLOC_STRICT);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
2730Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2740Sstevel@tonic-gate 		return (DDI_FAILURE);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (actual < count) {
2780Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2790Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2830Sstevel@tonic-gate 		return (DDI_FAILURE);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	qcn_state->qcn_intr_cnt = actual;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/* Get intr priority */
2890Sstevel@tonic-gate 	if (ddi_intr_get_pri(qcn_state->qcn_htable[0],
2900Sstevel@tonic-gate 	    &qcn_state->qcn_intr_pri) != DDI_SUCCESS) {
2910Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2920Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2960Sstevel@tonic-gate 		return (DDI_FAILURE);
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/* Call ddi_intr_add_handler() */
3000Sstevel@tonic-gate 	for (x = 0; x < actual; x++) {
3010Sstevel@tonic-gate 		if (ddi_intr_add_handler(qcn_state->qcn_htable[x],
3020Sstevel@tonic-gate 		    (ddi_intr_handler_t *)qcn_hi_intr,
3030Sstevel@tonic-gate 		    (caddr_t)qcn_state, NULL) != DDI_SUCCESS) {
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 			for (y = 0; y < x; y++) {
3060Sstevel@tonic-gate 				(void) ddi_intr_remove_handler(
3070Sstevel@tonic-gate 				    qcn_state->qcn_htable[y]);
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 			for (y = 0; y < actual; y++) {
3110Sstevel@tonic-gate 				(void) ddi_intr_free(qcn_state->qcn_htable[y]);
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			kmem_free(qcn_state->qcn_htable,
3150Sstevel@tonic-gate 			    qcn_state->qcn_intr_size);
3160Sstevel@tonic-gate 			return (DDI_FAILURE);
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	return (DDI_SUCCESS);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static void
qcn_remove_intrs(void)3240Sstevel@tonic-gate qcn_remove_intrs(void)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	int x;
3270Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3280Sstevel@tonic-gate 		(void) ddi_intr_disable(qcn_state->qcn_htable[x]);
3290Sstevel@tonic-gate 		(void) ddi_intr_remove_handler(qcn_state->qcn_htable[x]);
3300Sstevel@tonic-gate 		(void) ddi_intr_free(qcn_state->qcn_htable[x]);
3310Sstevel@tonic-gate 	}
332288Sarao 	kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate static void
qcn_intr_enable(void)3360Sstevel@tonic-gate qcn_intr_enable(void)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	int x;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3410Sstevel@tonic-gate 		(void) ddi_intr_enable(qcn_state->qcn_htable[x]);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * qcn_attach is called at startup time.
3470Sstevel@tonic-gate  * There is only once instance of this driver.
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate static int
qcn_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3500Sstevel@tonic-gate qcn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	extern int ddi_create_internal_pathname(dev_info_t *, char *,
3530Sstevel@tonic-gate 	    int, minor_t);
3540Sstevel@tonic-gate 	uint_t soft_prip;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate #ifdef QCN_POLLING
3570Sstevel@tonic-gate 	char *binding_name;
3580Sstevel@tonic-gate #endif
3590Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3600Sstevel@tonic-gate 		return (DDI_FAILURE);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (ddi_create_internal_pathname(dip, "qcn",
3630Sstevel@tonic-gate 	    S_IFCHR, 0) != DDI_SUCCESS)
3640Sstevel@tonic-gate 		return (DDI_FAILURE);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	qcn_state->qcn_soft_pend = 0;
3670Sstevel@tonic-gate 	qcn_state->qcn_hangup = 0;
3680Sstevel@tonic-gate 	qcn_state->qcn_rbuf_overflow = 0;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/* prepare some data structures in soft state */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	qcn_state->qcn_dip = dip;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	qcn_state->qcn_polling = 0;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate #ifdef QCN_POLLING
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * This test is for the sole purposes of allowing
3790Sstevel@tonic-gate 	 * the console to work on older firmware releases.
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	binding_name = ddi_binding_name(qcn_state->qcn_dip);
3821991Sheppo 	if ((strcmp(binding_name, "qcn") == 0) ||
3831991Sheppo 	    (qcn_force_polling))
3840Sstevel@tonic-gate 		qcn_state->qcn_polling = 1;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
3870Sstevel@tonic-gate 		qcn_poll_time.cyt_when = 0ull;
3880Sstevel@tonic-gate 		qcn_poll_time.cyt_interval =
3890Sstevel@tonic-gate 		    qcn_poll_interval * 1000ull * 1000ull;
3900Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3910Sstevel@tonic-gate 		qcn_poll_cycid = cyclic_add(&qcn_poll_cychandler,
3920Sstevel@tonic-gate 		    &qcn_poll_time);
3930Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate #endif
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
3980Sstevel@tonic-gate 		if (qcn_add_intrs() != DDI_SUCCESS) {
3990Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_intr failed\n");
4000Sstevel@tonic-gate 			return (DDI_FAILURE);
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 		if (ddi_intr_add_softint(dip, &qcn_state->qcn_softint_hdl,
4030Sstevel@tonic-gate 		    DDI_INTR_SOFTPRI_MAX, qcn_soft_intr,
4040Sstevel@tonic-gate 		    (caddr_t)qcn_state) != DDI_SUCCESS) {
4050Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_soft_intr failed\n");
4060Sstevel@tonic-gate 			qcn_remove_intrs();
4070Sstevel@tonic-gate 			return (DDI_FAILURE);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 		if (ddi_intr_get_softint_pri(qcn_state->qcn_softint_hdl,
4100Sstevel@tonic-gate 		    &soft_prip) != DDI_SUCCESS) {
4110Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: softint_pri failed\n");
412288Sarao 			(void) ddi_intr_remove_softint(
413288Sarao 			    qcn_state->qcn_softint_hdl);
4140Sstevel@tonic-gate 			qcn_remove_intrs();
4150Sstevel@tonic-gate 			return (DDI_FAILURE);
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 
4182954Sjb145095 	mutex_init(&qcn_state->qcn_hi_lock, NULL, MUTEX_DRIVER,
4192954Sjb145095 	    (void *)(uintptr_t)(qcn_state->qcn_intr_pri));
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_lock, NULL, MUTEX_DRIVER, NULL);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/*
4255974Sjm22469 	 * Fill in the polled I/O structure.
4265974Sjm22469 	 */
4275974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
4285974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_argument =
4295974Sjm22469 	    (cons_polledio_arg_t)qcn_state;
4305974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_putchar = qcn_polledio_putchar;
4315974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_getchar = qcn_polledio_getchar;
4325974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_ischar = qcn_polledio_ischar;
4335974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_enter = qcn_polledio_enter;
4345974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_exit = qcn_polledio_exit;
4355974Sjm22469 
4365974Sjm22469 	/*
4370Sstevel@tonic-gate 	 *  Enable  interrupts
4380Sstevel@tonic-gate 	 */
4390Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
4400Sstevel@tonic-gate 		qcn_intr_enable();
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate #ifdef QCN_DEBUG
4430Sstevel@tonic-gate 	prom_printf("qcn_attach(): qcn driver attached\n");
4440Sstevel@tonic-gate #endif
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	return (DDI_SUCCESS);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /* ARGSUSED */
4510Sstevel@tonic-gate static int
qcn_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4520Sstevel@tonic-gate qcn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4560Sstevel@tonic-gate 		return (DDI_FAILURE);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate #ifdef QCN_DEBUG
4600Sstevel@tonic-gate 	prom_printf("qcn_detach(): QCN driver detached\n");
4610Sstevel@tonic-gate #endif
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate #ifdef QCN_POLLING
4640Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
4650Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4660Sstevel@tonic-gate 		if (qcn_poll_cycid != CYCLIC_NONE)
4670Sstevel@tonic-gate 			cyclic_remove(qcn_poll_cycid);
4680Sstevel@tonic-gate 		qcn_poll_cycid = CYCLIC_NONE;
4690Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate #endif
4720Sstevel@tonic-gate 
4732954Sjb145095 	if (!qcn_state->qcn_polling)
4740Sstevel@tonic-gate 		qcn_remove_intrs();
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	return (DDI_SUCCESS);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /* ARGSUSED */
4800Sstevel@tonic-gate static int
qcn_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4810Sstevel@tonic-gate qcn_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	int error = DDI_FAILURE;
4840Sstevel@tonic-gate 	int instance = 0;
4850Sstevel@tonic-gate 	switch (infocmd) {
4860Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4870Sstevel@tonic-gate 		if (qcn_state) {
4880Sstevel@tonic-gate #ifdef QCN_DEBUG
4890Sstevel@tonic-gate 			prom_printf("qcn_getinfo(): devt2dip %lx\n", arg);
4900Sstevel@tonic-gate #endif
4910Sstevel@tonic-gate 			*result = (void *)qcn_state->qcn_dip;
4920Sstevel@tonic-gate 			error = DDI_SUCCESS;
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 		break;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4970Sstevel@tonic-gate #ifdef QCN_DEBUG
4980Sstevel@tonic-gate 		prom_printf("qcn_getinfo(): devt2instance %lx\n", arg);
4990Sstevel@tonic-gate #endif
5000Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
501911Siskreen 			*result = (void *)(uintptr_t)instance;
5020Sstevel@tonic-gate 			error = DDI_SUCCESS;
5030Sstevel@tonic-gate 		}
5040Sstevel@tonic-gate 		break;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	return (error);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /* streams open & close */
5110Sstevel@tonic-gate /* ARGSUSED */
5120Sstevel@tonic-gate static int
qcn_open(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * credp)5130Sstevel@tonic-gate qcn_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	tty_common_t *tty;
5160Sstevel@tonic-gate 	int unit = getminor(*devp);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate #ifdef QCN_DEBUG
5190Sstevel@tonic-gate 	prom_printf("qcn_open(): minor %x\n", unit);
5200Sstevel@tonic-gate #endif
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (unit != 0)
5230Sstevel@tonic-gate 		return (ENXIO);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* stream already open */
5260Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5270Sstevel@tonic-gate 		return (DDI_SUCCESS);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (!qcn_state) {
5300Sstevel@tonic-gate 		cmn_err(CE_WARN, "qcn_open: console was not configured by "
5310Sstevel@tonic-gate 		    "autoconfig\n");
5320Sstevel@tonic-gate 		return (ENXIO);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5360Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	tty->t_readq = q;
5390Sstevel@tonic-gate 	tty->t_writeq = WR(q);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/* Link the RD and WR Q's */
5420Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)qcn_state;
5430Sstevel@tonic-gate 	qcn_state->qcn_readq = RD(q);
5440Sstevel@tonic-gate 	qcn_state->qcn_writeq = WR(q);
5450Sstevel@tonic-gate 	qprocson(q);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate #ifdef QCN_DEBUG
5500Sstevel@tonic-gate 	prom_printf("qcn_open: opened as dev %lx\n", *devp);
5510Sstevel@tonic-gate #endif
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	return (DDI_SUCCESS);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /* ARGSUSED */
5570Sstevel@tonic-gate static int
qcn_close(queue_t * q,int flag,cred_t * credp)5580Sstevel@tonic-gate qcn_close(queue_t *q, int flag, cred_t *credp)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (qcn_state->qcn_wbufcid != 0) {
5640Sstevel@tonic-gate 		unbufcall(qcn_state->qcn_wbufcid);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 	ttycommon_close(&qcn_state->qcn_tty);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	qprocsoff(q);
5690Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
5700Sstevel@tonic-gate 	qcn_state->qcn_readq = NULL;
5710Sstevel@tonic-gate 	qcn_state->qcn_writeq = NULL;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	return (DDI_SUCCESS);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate  * Put procedure for write queue.
5780Sstevel@tonic-gate  * Respond to M_IOCTL, M_DATA and M_FLUSH messages here;
5790Sstevel@tonic-gate  * It put's the data onto internal qcn_output_q.
5800Sstevel@tonic-gate  */
5810Sstevel@tonic-gate static int
qcn_wput(queue_t * q,mblk_t * mp)5820Sstevel@tonic-gate qcn_wput(queue_t *q, mblk_t *mp)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate #ifdef QCN_DEBUG
5860Sstevel@tonic-gate 	struct iocblk *iocp;
5870Sstevel@tonic-gate 	int i;
5880Sstevel@tonic-gate #endif
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (!mp->b_datap) {
5930Sstevel@tonic-gate 		cmn_err(CE_PANIC, "qcn_wput: null datap");
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate #ifdef QCN_DEBUG
5970Sstevel@tonic-gate 	prom_printf("qcn_wput(): QCN wput q=%X mp=%X rd=%X wr=%X type=%X\n",
5987656SSherry.Moore@Sun.COM 	    q, mp, mp->b_rptr, mp->b_wptr, mp->b_datap->db_type);
5990Sstevel@tonic-gate #endif
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
6040Sstevel@tonic-gate 	case M_IOCTL:
6050Sstevel@tonic-gate 	case M_CTL:
6060Sstevel@tonic-gate #ifdef QCN_DEBUG
6070Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
6080Sstevel@tonic-gate 		prom_printf("qcn_wput(): M_IOCTL cmd=%X TIOC=%X\n",
6090Sstevel@tonic-gate 		    iocp->ioc_cmd, TIOC);
6100Sstevel@tonic-gate #endif
6110Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
6120Sstevel@tonic-gate 		case TCSETSW:
6130Sstevel@tonic-gate 		case TCSETSF:
6140Sstevel@tonic-gate 		case TCSETAW:
6150Sstevel@tonic-gate 		case TCSETAF:
6160Sstevel@tonic-gate 		case TCSBRK:
6170Sstevel@tonic-gate 			/*
6180Sstevel@tonic-gate 			 * The change do not take effect until all
6190Sstevel@tonic-gate 			 * output queued before them is drained.
6200Sstevel@tonic-gate 			 * Put this message on the queue, so that
6210Sstevel@tonic-gate 			 * "qcn_start" will see it when it's done
6220Sstevel@tonic-gate 			 * with the output before it. Poke the start
6230Sstevel@tonic-gate 			 * routine, just in case.
6240Sstevel@tonic-gate 			 */
6250Sstevel@tonic-gate 			(void) putq(q, mp);
6260Sstevel@tonic-gate 			qcn_start();
6270Sstevel@tonic-gate 			break;
6280Sstevel@tonic-gate 		default:
6293419Sjb145095 			mutex_exit(&qcn_state->qcn_lock);
6300Sstevel@tonic-gate 			qcn_ioctl(q, mp);
6313419Sjb145095 			mutex_enter(&qcn_state->qcn_lock);
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 		break;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	case M_FLUSH:
6360Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
6370Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
6380Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
6410Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
6420Sstevel@tonic-gate 			qreply(q, mp);
6430Sstevel@tonic-gate 		} else {
6440Sstevel@tonic-gate 			freemsg(mp);
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 		break;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	case M_STOP:
6490Sstevel@tonic-gate 		qcn_stopped = B_TRUE;
6500Sstevel@tonic-gate 		freemsg(mp);
6510Sstevel@tonic-gate 		break;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	case M_START:
6540Sstevel@tonic-gate 		qcn_stopped = B_FALSE;
6550Sstevel@tonic-gate 		freemsg(mp);
6560Sstevel@tonic-gate 		qenable(q);	/* Start up delayed messages */
6570Sstevel@tonic-gate 		break;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	case M_DATA:
6600Sstevel@tonic-gate 		/*
6610Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
6620Sstevel@tonic-gate 		 * and poke the start routine.
6630Sstevel@tonic-gate 		 */
6640Sstevel@tonic-gate #ifdef QCN_DEBUG
6650Sstevel@tonic-gate 		if (mp->b_rptr < mp->b_wptr) {
6660Sstevel@tonic-gate 		prom_printf("qcn_wput(): DATA q=%X mp=%X rd=%X wr=%X\n",
6677656SSherry.Moore@Sun.COM 		    q, mp, mp->b_rptr, mp->b_wptr);
6680Sstevel@tonic-gate 		prom_printf("qcn_wput(): [");
6690Sstevel@tonic-gate 		for (i = 0; i < mp->b_wptr-mp->b_rptr; i++) {
6700Sstevel@tonic-gate 			prom_printf("%c", *(mp->b_rptr+i));
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 		prom_printf("]\n");
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate #endif /* QCN_DEBUG */
6750Sstevel@tonic-gate 		(void) putq(q, mp);
6760Sstevel@tonic-gate 		qcn_start();
6770Sstevel@tonic-gate 		break;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	default:
6800Sstevel@tonic-gate 		freemsg(mp);
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
6843419Sjb145095 
6850Sstevel@tonic-gate 	return (0);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6900Sstevel@tonic-gate  */
6910Sstevel@tonic-gate static void
qcn_ioctl(queue_t * q,mblk_t * mp)6920Sstevel@tonic-gate qcn_ioctl(queue_t *q, mblk_t *mp)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	struct iocblk	*iocp;
6950Sstevel@tonic-gate 	tty_common_t	*tty;
6960Sstevel@tonic-gate 	mblk_t		*datamp;
6970Sstevel@tonic-gate 	int		data_size;
6980Sstevel@tonic-gate 	int		error = 0;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate #ifdef QCN_DEBUG
7010Sstevel@tonic-gate 	prom_printf("qcn_ioctl(): q=%X mp=%X\n", q, mp);
7020Sstevel@tonic-gate #endif
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7055974Sjm22469 
7060Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	if (tty->t_iocpending != NULL) {
7090Sstevel@tonic-gate 		freemsg(tty->t_iocpending);
7100Sstevel@tonic-gate 		tty->t_iocpending = NULL;
7110Sstevel@tonic-gate 	}
7125974Sjm22469 
7135974Sjm22469 	/*
7145974Sjm22469 	 * Handle the POLLEDIO ioctls now because ttycommon_ioctl
7155974Sjm22469 	 * (below) frees up the message block (mp->b_cont) which
7165974Sjm22469 	 * contains the pointer used to pass back results.
7175974Sjm22469 	 */
7185974Sjm22469 	switch (iocp->ioc_cmd) {
7195974Sjm22469 	case CONSOPENPOLLEDIO:
7205974Sjm22469 		error = miocpullup(mp, sizeof (struct cons_polledio *));
7215974Sjm22469 		if (error != 0)
7225974Sjm22469 			break;
7235974Sjm22469 
7245974Sjm22469 		*(struct cons_polledio **)mp->b_cont->b_rptr =
7255974Sjm22469 		    &qcn_state->qcn_polledio;
7265974Sjm22469 
7275974Sjm22469 		mp->b_datap->db_type = M_IOCACK;
7285974Sjm22469 		break;
7295974Sjm22469 
7305974Sjm22469 	case CONSCLOSEPOLLEDIO:
7315974Sjm22469 		mp->b_datap->db_type = M_IOCACK;
7325974Sjm22469 		iocp->ioc_error = 0;
7335974Sjm22469 		iocp->ioc_rval = 0;
7345974Sjm22469 		break;
7355974Sjm22469 
7365974Sjm22469 	default:
7375974Sjm22469 		data_size = ttycommon_ioctl(tty, q, mp, &error);
7385974Sjm22469 		if (data_size != 0) {
7395974Sjm22469 			if (qcn_state->qcn_wbufcid)
7405974Sjm22469 				unbufcall(qcn_state->qcn_wbufcid);
7415974Sjm22469 			/* call qcn_reioctl() */
7425974Sjm22469 			qcn_state->qcn_wbufcid =
7435974Sjm22469 			    bufcall(data_size, BPRI_HI, qcn_reioctl, qcn_state);
7445974Sjm22469 			return;
7455974Sjm22469 		}
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7483419Sjb145095 	mutex_enter(&qcn_state->qcn_lock);
7493419Sjb145095 
7500Sstevel@tonic-gate 	if (error < 0) {
7510Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
7540Sstevel@tonic-gate 		 */
7550Sstevel@tonic-gate 		error = 0;
7560Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
7570Sstevel@tonic-gate 		case TCSBRK:
7580Sstevel@tonic-gate 		case TIOCSBRK:
7590Sstevel@tonic-gate 		case TIOCCBRK:
7600Sstevel@tonic-gate 		case TIOCMSET:
7610Sstevel@tonic-gate 		case TIOCMBIS:
7620Sstevel@tonic-gate 		case TIOCMBIC:
7630Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7640Sstevel@tonic-gate 				qcn_ack(mp, NULL, 0);
7650Sstevel@tonic-gate 			else
7660Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
7670Sstevel@tonic-gate 			break;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 		case TIOCMGET:
7700Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
7710Sstevel@tonic-gate 			if (datamp == NULL) {
7720Sstevel@tonic-gate 				error = EAGAIN;
7730Sstevel@tonic-gate 				break;
7740Sstevel@tonic-gate 			}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 			*(int *)datamp->b_rptr = 0;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7790Sstevel@tonic-gate 				qcn_ack(mp, datamp, sizeof (int));
7800Sstevel@tonic-gate 			else
7810Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
7820Sstevel@tonic-gate 			break;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		default:
7850Sstevel@tonic-gate 			error = EINVAL;
7860Sstevel@tonic-gate 			break;
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	if (error != 0) {
7900Sstevel@tonic-gate 		iocp->ioc_count = 0;
7910Sstevel@tonic-gate 		iocp->ioc_error = error;
7920Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7930Sstevel@tonic-gate 	}
7943419Sjb145095 	mutex_exit(&qcn_state->qcn_lock);
7950Sstevel@tonic-gate 	qreply(q, mp);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate static void
qcn_reioctl(void * unit)7990Sstevel@tonic-gate qcn_reioctl(void *unit)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	queue_t		*q;
8020Sstevel@tonic-gate 	mblk_t		*mp;
8030Sstevel@tonic-gate 	qcn_t		*qcnp = (qcn_t *)unit;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (!qcnp->qcn_wbufcid)
8060Sstevel@tonic-gate 		return;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	qcnp->qcn_wbufcid = 0;
8090Sstevel@tonic-gate 	if ((q = qcnp->qcn_tty.t_writeq) == NULL)
8100Sstevel@tonic-gate 		return;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if ((mp = qcnp->qcn_tty.t_iocpending) == NULL)
8130Sstevel@tonic-gate 		return;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	qcnp->qcn_tty.t_iocpending = NULL;
8160Sstevel@tonic-gate 	qcn_ioctl(q, mp);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate static void
qcn_ack(mblk_t * mp,mblk_t * dp,uint_t size)8200Sstevel@tonic-gate qcn_ack(mblk_t *mp, mblk_t *dp, uint_t size)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	struct iocblk  *iocp = (struct iocblk *)mp->b_rptr;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCACK;
8250Sstevel@tonic-gate 	iocp->ioc_count = size;
8260Sstevel@tonic-gate 	iocp->ioc_error = 0;
8270Sstevel@tonic-gate 	iocp->ioc_rval = 0;
8280Sstevel@tonic-gate 	if (mp->b_cont != NULL)
8290Sstevel@tonic-gate 		freeb(mp->b_cont);
8300Sstevel@tonic-gate 	if (dp != NULL) {
8310Sstevel@tonic-gate 		mp->b_cont = dp;
8320Sstevel@tonic-gate 		dp->b_wptr += size;
8330Sstevel@tonic-gate 	} else
8340Sstevel@tonic-gate 		mp->b_cont = NULL;
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate static void
qcn_start(void)8380Sstevel@tonic-gate qcn_start(void)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	queue_t *q;
8420Sstevel@tonic-gate 	mblk_t *mp;
8430Sstevel@tonic-gate 	int rv;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * read stream queue and remove data from the queue and
8490Sstevel@tonic-gate 	 * transmit them if possible
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
8520Sstevel@tonic-gate 	ASSERT(q != NULL);
8530Sstevel@tonic-gate 	while (mp = getq(q)) {
8540Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
8550Sstevel@tonic-gate 			/*
8560Sstevel@tonic-gate 			 * These are those IOCTLs queued up
8570Sstevel@tonic-gate 			 * do it now
8580Sstevel@tonic-gate 			 */
8593419Sjb145095 			mutex_exit(&qcn_state->qcn_lock);
8600Sstevel@tonic-gate 			qcn_ioctl(q, mp);
8613419Sjb145095 			mutex_enter(&qcn_state->qcn_lock);
8620Sstevel@tonic-gate 			continue;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * M_DATA
8660Sstevel@tonic-gate 		 */
8672282Sjb145095 		rv = qcn_state->cons_transmit(q, mp);
8680Sstevel@tonic-gate 		if (rv == EBUSY || rv == EAGAIN)
8690Sstevel@tonic-gate 			return;
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static int
qcn_transmit_write(queue_t * q,mblk_t * mp)8742282Sjb145095 qcn_transmit_write(queue_t *q, mblk_t *mp)
8750Sstevel@tonic-gate {
8762282Sjb145095 	mblk_t		*bp;
8772282Sjb145095 	size_t		len;
8782282Sjb145095 	uint64_t	i;
8792282Sjb145095 	uint64_t	retval = 0;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate #ifdef QCN_DEBUG
8822282Sjb145095 	prom_printf("qcn_transmit_write(): q=%X mp=%X\n", q, mp);
8830Sstevel@tonic-gate #endif
8842282Sjb145095 
8852282Sjb145095 	while (mp) {
8862282Sjb145095 		bp = mp;
8872282Sjb145095 		len = bp->b_wptr - bp->b_rptr;
8882282Sjb145095 		/*
8892282Sjb145095 		 * Use the console write call to send a block of characters to
8902282Sjb145095 		 * the console.
8912282Sjb145095 		 */
8922282Sjb145095 		i = (len > CONS_WR_BUF_SIZE) ? CONS_WR_BUF_SIZE : len;
8932282Sjb145095 		bcopy(bp->b_rptr, qcn_state->cons_write_buffer, i);
8942282Sjb145095 		retval = hv_cnwrite(qcn_state->cons_write_buf_ra, i, &i);
8952282Sjb145095 
8962282Sjb145095 		if (retval == H_EOK) {
8972282Sjb145095 			len -= i;
8982282Sjb145095 			bp->b_rptr += i;
8992282Sjb145095 			/*
9002282Sjb145095 			 * if we have finished with this buf, free
9012282Sjb145095 			 * and get the next buf if present.
9022282Sjb145095 			 */
9032282Sjb145095 			if (len == 0) {
9042282Sjb145095 				mp = bp->b_cont;
9052282Sjb145095 				freeb(bp);
9062282Sjb145095 			}
9072282Sjb145095 		} else {
9082282Sjb145095 			(void) putbq(q, mp);
9092282Sjb145095 
9102282Sjb145095 			switch (retval) {
9112282Sjb145095 
9122282Sjb145095 			case H_EWOULDBLOCK :
9132282Sjb145095 				/*
9142282Sjb145095 				 * hypervisor cannot process the request -
9152282Sjb145095 				 * channel busy.  Try again later.
9162282Sjb145095 				 */
9172282Sjb145095 				return (EAGAIN);
9182282Sjb145095 
9192282Sjb145095 			case H_EIO :
9202282Sjb145095 				return (EIO);
9212282Sjb145095 			default :
9222282Sjb145095 				return (ENXIO);
9232282Sjb145095 			}
9242282Sjb145095 		}
9252282Sjb145095 	}
9262282Sjb145095 	return (0);
9272282Sjb145095 }
9282282Sjb145095 
9292282Sjb145095 static int
qcn_transmit_putchr(queue_t * q,mblk_t * mp)9302282Sjb145095 qcn_transmit_putchr(queue_t *q, mblk_t *mp)
9312282Sjb145095 {
9322282Sjb145095 	caddr_t		buf;
9332282Sjb145095 	mblk_t		*bp;
9342282Sjb145095 	size_t		len;
9352282Sjb145095 	uint64_t	i;
9362282Sjb145095 
9372282Sjb145095 #ifdef QCN_DEBUG
9382282Sjb145095 	prom_printf("qcn_transmit_putchr(): q=%X mp=%X\n", q, mp);
9392282Sjb145095 #endif
9402282Sjb145095 	while (mp) {
9410Sstevel@tonic-gate 		bp = mp;
9420Sstevel@tonic-gate 		len = bp->b_wptr - bp->b_rptr;
9430Sstevel@tonic-gate 		buf = (caddr_t)bp->b_rptr;
9440Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
9452954Sjb145095 			if (hv_cnputchar(buf[i]) == H_EWOULDBLOCK)
9462531Snarayan 				break;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 		if (i != len) {
9490Sstevel@tonic-gate 			bp->b_rptr += i;
9500Sstevel@tonic-gate 			(void) putbq(q, mp);
9510Sstevel@tonic-gate 			return (EAGAIN);
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 		mp = bp->b_cont;
9540Sstevel@tonic-gate 		freeb(bp);
9552282Sjb145095 	}
9560Sstevel@tonic-gate 	return (0);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /*
9600Sstevel@tonic-gate  * called when SC first establishes console connection
9610Sstevel@tonic-gate  * drop all the data on the output queue
9620Sstevel@tonic-gate  */
9630Sstevel@tonic-gate static void
qcn_flush(void)9640Sstevel@tonic-gate qcn_flush(void)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	queue_t *q;
9670Sstevel@tonic-gate 	mblk_t *mp;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
9720Sstevel@tonic-gate 
973911Siskreen 	prom_printf("qcn_flush(): WARNING console output is dropped time=%lx\n",
9740Sstevel@tonic-gate 	    gethrestime_sec());
9750Sstevel@tonic-gate 	while (mp = getq(q))
9760Sstevel@tonic-gate 		freemsg(mp);
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate static void
qcn_trigger_softint(void)9800Sstevel@tonic-gate qcn_trigger_softint(void)
9810Sstevel@tonic-gate {
9822282Sjb145095 	/*
9832282Sjb145095 	 * if we are not currently servicing a software interrupt
9842282Sjb145095 	 * (qcn_soft_pend == 0), trigger the service routine to run.
9852282Sjb145095 	 */
9862432Sjb145095 	if (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_DO) ==
9877656SSherry.Moore@Sun.COM 	    QCN_SP_IDL) {
9882282Sjb145095 		(void) ddi_intr_trigger_softint(
9897656SSherry.Moore@Sun.COM 		    qcn_state->qcn_softint_hdl, NULL);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate /*ARGSUSED*/
9940Sstevel@tonic-gate static uint_t
qcn_soft_intr(caddr_t arg1,caddr_t arg2)9950Sstevel@tonic-gate qcn_soft_intr(caddr_t arg1, caddr_t arg2)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate 	mblk_t *mp;
9980Sstevel@tonic-gate 	int	cc;
9992282Sjb145095 	int	overflow_check;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	do {
10022432Sjb145095 		(void) atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IP);
10030Sstevel@tonic-gate 		mutex_enter(&qcn_state->qcn_hi_lock);
10043419Sjb145095 		cc = RING_CNT(qcn_state);
10053419Sjb145095 		mutex_exit(&qcn_state->qcn_hi_lock);
10063419Sjb145095 		if (cc <= 0) {
10072282Sjb145095 			goto out;
10082282Sjb145095 		}
10090Sstevel@tonic-gate 
10102282Sjb145095 		if ((mp = allocb(cc, BPRI_MED)) == NULL) {
10113419Sjb145095 			mutex_enter(&qcn_state->qcn_hi_lock);
10122282Sjb145095 			qcn_input_dropped += cc;
10132282Sjb145095 			mutex_exit(&qcn_state->qcn_hi_lock);
10142282Sjb145095 			cmn_err(CE_WARN, "qcn_intr: allocb"
10152282Sjb145095 			    "failed (console input dropped)");
10162282Sjb145095 			goto out;
10172282Sjb145095 		}
10182282Sjb145095 
10193419Sjb145095 		mutex_enter(&qcn_state->qcn_hi_lock);
10202282Sjb145095 		do {
10212282Sjb145095 			/* put console input onto stream */
10222282Sjb145095 			*(char *)mp->b_wptr++ = RING_GET(qcn_state);
10232282Sjb145095 		} while (--cc);
10242282Sjb145095 
10252282Sjb145095 		if ((overflow_check = qcn_state->qcn_rbuf_overflow) != 0) {
10262282Sjb145095 			qcn_state->qcn_rbuf_overflow = 0;
10272282Sjb145095 		}
10282282Sjb145095 		mutex_exit(&qcn_state->qcn_hi_lock);
10290Sstevel@tonic-gate 
10302282Sjb145095 		if (overflow_check) {
10312282Sjb145095 			cmn_err(CE_WARN, "qcn: Ring buffer overflow\n");
10322282Sjb145095 		}
10332282Sjb145095 
10342282Sjb145095 		if (qcn_state->qcn_readq) {
10352282Sjb145095 			putnext(qcn_state->qcn_readq, mp);
10360Sstevel@tonic-gate 		}
10372282Sjb145095 out:
10382954Sjb145095 		/*
10392954Sjb145095 		 * If there are pending transmits because hypervisor
10402954Sjb145095 		 * returned EWOULDBLOCK poke start now.
10412954Sjb145095 		 */
10422954Sjb145095 
10432954Sjb145095 		if (qcn_state->qcn_writeq != NULL) {
10442954Sjb145095 			if (qcn_state->qcn_hangup) {
10452954Sjb145095 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
10462282Sjb145095 				flushq(qcn_state->qcn_writeq, FLUSHDATA);
10472954Sjb145095 				qcn_state->qcn_hangup = 0;
10482954Sjb145095 			} else {
10492954Sjb145095 				mutex_enter(&qcn_state->qcn_lock);
10502954Sjb145095 				qcn_start();
10512954Sjb145095 				mutex_exit(&qcn_state->qcn_lock);
10522282Sjb145095 			}
10532282Sjb145095 		}
10542432Sjb145095 		/*
10552432Sjb145095 		 * now loop if another interrupt came in (qcn_trigger_softint
10562432Sjb145095 		 * called) while we were processing the loop
10572432Sjb145095 		 */
10582432Sjb145095 	} while (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IDL) ==
10597656SSherry.Moore@Sun.COM 	    QCN_SP_DO);
10600Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /*ARGSUSED*/
10640Sstevel@tonic-gate static uint_t
qcn_hi_intr(caddr_t arg)10650Sstevel@tonic-gate qcn_hi_intr(caddr_t arg)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_hi_lock);
10680Sstevel@tonic-gate 
10692282Sjb145095 	qcn_state->cons_receive();
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_hi_lock);
10720Sstevel@tonic-gate 	qcn_trigger_softint();
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10772282Sjb145095 static void
qcn_receive_read(void)10782282Sjb145095 qcn_receive_read(void)
10792282Sjb145095 {
10802282Sjb145095 	int64_t rv;
10812282Sjb145095 	uint8_t *bufp;
10822282Sjb145095 	int64_t	retcount = 0;
10832282Sjb145095 	int	i;
10842282Sjb145095 
10852282Sjb145095 	do {
10862282Sjb145095 		/*
10872282Sjb145095 		 * Maximize available buffer size
10882282Sjb145095 		 */
10892282Sjb145095 		if (RING_CNT(qcn_state) <= 0) {
10902282Sjb145095 			RING_INIT(qcn_state);
10912282Sjb145095 		}
10922282Sjb145095 		rv = hv_cnread(qcn_state->cons_read_buf_ra +
10937656SSherry.Moore@Sun.COM 		    RING_POFF(qcn_state),
10947656SSherry.Moore@Sun.COM 		    RING_LEFT(qcn_state),
10957656SSherry.Moore@Sun.COM 		    &retcount);
10962282Sjb145095 		bufp = RING_ADDR(qcn_state);
10972282Sjb145095 		if (rv == H_EOK) {
10982282Sjb145095 			/*
10992282Sjb145095 			 * if the alternate break sequence is enabled, test
11002282Sjb145095 			 * the buffer for the sequence and if it is there,
11012282Sjb145095 			 * enter the debugger.
11022282Sjb145095 			 */
11032282Sjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
11042282Sjb145095 				for (i = 0; i < retcount; i++) {
11052282Sjb145095 					if (abort_charseq_recognize(*bufp++)) {
11062282Sjb145095 						abort_sequence_enter(
11077656SSherry.Moore@Sun.COM 						    (char *)NULL);
11082282Sjb145095 					}
11092282Sjb145095 				}
11102282Sjb145095 			}
11112282Sjb145095 
11122282Sjb145095 			/* put console input onto stream */
11132282Sjb145095 			if (retcount > 0) {
11142282Sjb145095 				/*
11152282Sjb145095 				 * the characters are already in the ring,
11162282Sjb145095 				 * just update the pointer so the characters
11172282Sjb145095 				 * can be retrieved.
11182282Sjb145095 				 */
11192282Sjb145095 				RING_UPD(qcn_state, retcount);
11202282Sjb145095 			}
11212282Sjb145095 		} else {
11222282Sjb145095 			switch (rv) {
11232282Sjb145095 
11242282Sjb145095 			case H_EWOULDBLOCK :
11252282Sjb145095 				/*
11262282Sjb145095 				 * hypervisor cannot handle the request.
11272282Sjb145095 				 * Try again later.
11282282Sjb145095 				 */
11292282Sjb145095 				break;
11302282Sjb145095 
11312282Sjb145095 
11322282Sjb145095 			case H_BREAK :
11332282Sjb145095 				/*
11349250SZachary.Kissel@Sun.COM 				 * on break enter the debugger
11352282Sjb145095 				 */
1136*12429SZachary.Kissel@Sun.COM 				if (abort_enable != KIOCABORTALTERNATE)
1137*12429SZachary.Kissel@Sun.COM 					abort_sequence_enter((char *)NULL);
11382282Sjb145095 				break;
11392282Sjb145095 
11402282Sjb145095 			case H_HUP :
11412282Sjb145095 				qcn_state->qcn_hangup = 1;
11422282Sjb145095 				break;
11432282Sjb145095 
11442282Sjb145095 			default :
11452282Sjb145095 				break;
11462282Sjb145095 			}
11472282Sjb145095 		}
11482282Sjb145095 	} while (rv == H_EOK);
11492282Sjb145095 }
11502282Sjb145095 
11512282Sjb145095 static void
qcn_receive_getchr(void)11522282Sjb145095 qcn_receive_getchr(void)
11532282Sjb145095 {
11542282Sjb145095 	int64_t rv;
11552282Sjb145095 	uint8_t	buf;
11562282Sjb145095 
11572282Sjb145095 	do {
11582282Sjb145095 		rv = hv_cngetchar(&buf);
11592282Sjb145095 		if (rv == H_EOK) {
11602282Sjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
11612282Sjb145095 				if (abort_charseq_recognize(buf)) {
11622282Sjb145095 					abort_sequence_enter((char *)NULL);
11632282Sjb145095 				}
11642282Sjb145095 			}
11652282Sjb145095 
11662282Sjb145095 			/* put console input onto stream */
11672282Sjb145095 			if (RING_POK(qcn_state, 1)) {
11682282Sjb145095 				RING_PUT(qcn_state, buf);
11692282Sjb145095 			} else {
11702282Sjb145095 				qcn_state->qcn_rbuf_overflow++;
11712282Sjb145095 			}
11722282Sjb145095 		} else {
11732282Sjb145095 			if (rv == H_BREAK) {
1174*12429SZachary.Kissel@Sun.COM 				if (abort_enable != KIOCABORTENABLE)
1175*12429SZachary.Kissel@Sun.COM 					abort_sequence_enter((char *)NULL);
11762282Sjb145095 			}
11772282Sjb145095 
11782282Sjb145095 			if (rv == H_HUP)  {
11792282Sjb145095 				qcn_state->qcn_hangup = 1;
11802282Sjb145095 			}
11812282Sjb145095 			return;
11822282Sjb145095 		}
11832282Sjb145095 	} while (rv == H_EOK);
11842282Sjb145095 }
11852282Sjb145095 
11860Sstevel@tonic-gate #ifdef QCN_POLLING
11870Sstevel@tonic-gate /*ARGSUSED*/
11880Sstevel@tonic-gate static void
qcn_poll_handler(void * unused)11890Sstevel@tonic-gate qcn_poll_handler(void *unused)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	mblk_t *mp;
11920Sstevel@tonic-gate 	int64_t rv;
11930Sstevel@tonic-gate 	uint8_t buf;
11940Sstevel@tonic-gate 	int qcn_writeq_flush = 0;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/* LINTED: E_CONSTANT_CONDITION */
11970Sstevel@tonic-gate 	while (1) {
11980Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
11990Sstevel@tonic-gate 		if (rv == H_BREAK) {
1200*12429SZachary.Kissel@Sun.COM 			if (abort_enable != KIOCABORTALTERNATE)
1201*12429SZachary.Kissel@Sun.COM 				abort_sequence_enter((char *)NULL);
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 		if (rv == H_HUP)  {
12050Sstevel@tonic-gate 			if (qcn_state->qcn_readq) {
12060Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
12070Sstevel@tonic-gate 				qcn_writeq_flush = 1;
12080Sstevel@tonic-gate 			}
12090Sstevel@tonic-gate 			goto out;
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 		if (rv != H_EOK)
12130Sstevel@tonic-gate 			goto out;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 		if (abort_enable == KIOCABORTALTERNATE) {
12160Sstevel@tonic-gate 			if (abort_charseq_recognize(buf)) {
12170Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
12180Sstevel@tonic-gate 			}
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		/* put console input onto stream */
12220Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
12230Sstevel@tonic-gate 			if ((mp = allocb(1, BPRI_MED)) == NULL) {
12240Sstevel@tonic-gate 				qcn_input_dropped++;
12250Sstevel@tonic-gate 				cmn_err(CE_WARN, "qcn_intr: allocb"
12260Sstevel@tonic-gate 				    "failed (console input dropped)");
12270Sstevel@tonic-gate 				return;
12280Sstevel@tonic-gate 			}
12290Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = buf;
12300Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate out:
12340Sstevel@tonic-gate /*
12350Sstevel@tonic-gate  * If there are pending transmits because hypervisor
12360Sstevel@tonic-gate  * returned EWOULDBLOCK poke start now.
12370Sstevel@tonic-gate  */
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12400Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL) {
12410Sstevel@tonic-gate 		if (qcn_writeq_flush) {
12420Sstevel@tonic-gate 			flushq(qcn_state->qcn_writeq, FLUSHDATA);
12430Sstevel@tonic-gate 		} else {
12440Sstevel@tonic-gate 			qcn_start();
12450Sstevel@tonic-gate 		}
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate #endif
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate /*
12520Sstevel@tonic-gate  * Check for abort character sequence, copied from zs_async.c
12530Sstevel@tonic-gate  */
12540Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate static boolean_t
abort_charseq_recognize(uchar_t ch)12570Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	static int state = 0;
12600Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	if (ch == sequence[state]) {
12630Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
12640Sstevel@tonic-gate 			state = 0;
12650Sstevel@tonic-gate 			return (B_TRUE);
12660Sstevel@tonic-gate 		}
12670Sstevel@tonic-gate 	} else {
12680Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 	return (B_FALSE);
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate static int
qcn_rsrv(queue_t * q)12750Sstevel@tonic-gate qcn_rsrv(queue_t *q)
12760Sstevel@tonic-gate {
12770Sstevel@tonic-gate 	mblk_t	*mp;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12800Sstevel@tonic-gate 		return (0);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
12850Sstevel@tonic-gate 		if (canputnext(q))
12860Sstevel@tonic-gate 			putnext(q, mp);
12870Sstevel@tonic-gate 		else if (mp->b_datap->db_type >= QPCTL)
12880Sstevel@tonic-gate 			(void) putbq(q, mp);
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	return (0);
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate /* ARGSUSED */
12970Sstevel@tonic-gate static int
qcn_wsrv(queue_t * q)12980Sstevel@tonic-gate qcn_wsrv(queue_t *q)
12990Sstevel@tonic-gate {
13000Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
13010Sstevel@tonic-gate 		return (0);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL)
13060Sstevel@tonic-gate 		qcn_start();
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	return (0);
13110Sstevel@tonic-gate }
13125974Sjm22469 
13135974Sjm22469 static boolean_t
qcn_polledio_ischar(cons_polledio_arg_t arg)13145974Sjm22469 qcn_polledio_ischar(cons_polledio_arg_t arg)
13155974Sjm22469 {
13165974Sjm22469 	qcn_t *state = (qcn_t *)arg;
13175974Sjm22469 
13185974Sjm22469 	if (state->qcn_char_available)
13195974Sjm22469 		return (B_TRUE);
13205974Sjm22469 
13215974Sjm22469 	return (state->qcn_char_available =
13225974Sjm22469 	    (hv_cngetchar(&state->qcn_hold_char) == H_EOK));
13235974Sjm22469 }
13245974Sjm22469 
13255974Sjm22469 
13265974Sjm22469 static int
qcn_polledio_getchar(cons_polledio_arg_t arg)13275974Sjm22469 qcn_polledio_getchar(cons_polledio_arg_t arg)
13285974Sjm22469 {
13295974Sjm22469 	qcn_t *state = (qcn_t *)arg;
13305974Sjm22469 
13315974Sjm22469 	while (!qcn_polledio_ischar(arg))
13325974Sjm22469 		drv_usecwait(10);
13335974Sjm22469 
13345974Sjm22469 	state->qcn_char_available = B_FALSE;
13355974Sjm22469 
13365974Sjm22469 	return ((int)state->qcn_hold_char);
13375974Sjm22469 }
13385974Sjm22469 
13395974Sjm22469 static void
qcn_polledio_putchar(cons_polledio_arg_t arg,uchar_t c)13405974Sjm22469 qcn_polledio_putchar(cons_polledio_arg_t arg, uchar_t c)
13415974Sjm22469 {
13425974Sjm22469 	if (c == '\n')
13435974Sjm22469 		qcn_polledio_putchar(arg, '\r');
13445974Sjm22469 
13455974Sjm22469 	while (hv_cnputchar((uint8_t)c) == H_EWOULDBLOCK)
13465974Sjm22469 		drv_usecwait(10);
13475974Sjm22469 }
13485974Sjm22469 
13495974Sjm22469 static void
qcn_polledio_enter(cons_polledio_arg_t arg)13505974Sjm22469 qcn_polledio_enter(cons_polledio_arg_t arg)
13515974Sjm22469 {
13525974Sjm22469 	qcn_t *state = (qcn_t *)arg;
13535974Sjm22469 
13545974Sjm22469 	state->qcn_char_available = B_FALSE;
13555974Sjm22469 }
13565974Sjm22469 
13575974Sjm22469 /* ARGSUSED */
13585974Sjm22469 static void
qcn_polledio_exit(cons_polledio_arg_t arg)13595974Sjm22469 qcn_polledio_exit(cons_polledio_arg_t arg)
13605974Sjm22469 {
13615974Sjm22469 }
1362