xref: /onnv-gate/usr/src/uts/sun4v/io/qcn.c (revision 5974:05fbdff2571b)
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*5974Sjm22469  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * sun4v console driver
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/conf.h>
370Sstevel@tonic-gate #include <sys/termios.h>
380Sstevel@tonic-gate #include <sys/modctl.h>
390Sstevel@tonic-gate #include <sys/kbio.h>
400Sstevel@tonic-gate #include <sys/stropts.h>
410Sstevel@tonic-gate #include <sys/stream.h>
420Sstevel@tonic-gate #include <sys/strsun.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
440Sstevel@tonic-gate #include <sys/promif.h>
450Sstevel@tonic-gate #include <sys/ddi.h>
460Sstevel@tonic-gate #include <sys/sunddi.h>
470Sstevel@tonic-gate #include <sys/cyclic.h>
480Sstevel@tonic-gate #include <sys/intr.h>
490Sstevel@tonic-gate #include <sys/spl.h>
500Sstevel@tonic-gate #include <sys/qcn.h>
510Sstevel@tonic-gate #include <sys/hypervisor_api.h>
522282Sjb145095 #include <sys/hsvc.h>
532282Sjb145095 #include <sys/machsystm.h>
54*5974Sjm22469 #include <sys/consdev.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* dev_ops and cb_ops for device driver */
570Sstevel@tonic-gate static int qcn_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
580Sstevel@tonic-gate static int qcn_attach(dev_info_t *, ddi_attach_cmd_t);
590Sstevel@tonic-gate static int qcn_detach(dev_info_t *, ddi_detach_cmd_t);
600Sstevel@tonic-gate static int qcn_open(queue_t *, dev_t *, int, int, cred_t *);
610Sstevel@tonic-gate static int qcn_close(queue_t *, int, cred_t *);
620Sstevel@tonic-gate static int qcn_wput(queue_t *, mblk_t *);
630Sstevel@tonic-gate static int qcn_wsrv(queue_t *);
640Sstevel@tonic-gate static int qcn_rsrv(queue_t *);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* other internal qcn routines */
670Sstevel@tonic-gate static void qcn_ioctl(queue_t *, mblk_t *);
680Sstevel@tonic-gate static void qcn_reioctl(void *);
690Sstevel@tonic-gate static void qcn_ack(mblk_t *, mblk_t *, uint_t);
700Sstevel@tonic-gate static void qcn_start(void);
712282Sjb145095 static int qcn_transmit_write(queue_t *, mblk_t *);
722282Sjb145095 static int qcn_transmit_putchr(queue_t *, mblk_t *);
732282Sjb145095 static void qcn_receive_read(void);
742282Sjb145095 static void qcn_receive_getchr(void);
750Sstevel@tonic-gate static void qcn_flush(void);
760Sstevel@tonic-gate static uint_t qcn_hi_intr(caddr_t arg);
770Sstevel@tonic-gate static uint_t qcn_soft_intr(caddr_t arg1, caddr_t arg2);
780Sstevel@tonic-gate 
79*5974Sjm22469 /* functions required for polled io */
80*5974Sjm22469 static boolean_t qcn_polledio_ischar(cons_polledio_arg_t arg);
81*5974Sjm22469 static int qcn_polledio_getchar(cons_polledio_arg_t arg);
82*5974Sjm22469 static void qcn_polledio_putchar(cons_polledio_arg_t arg, uchar_t c);
83*5974Sjm22469 static void qcn_polledio_enter(cons_polledio_arg_t arg);
84*5974Sjm22469 static void qcn_polledio_exit(cons_polledio_arg_t arg);
85*5974Sjm22469 
86*5974Sjm22469 
870Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static qcn_t *qcn_state;
900Sstevel@tonic-gate static uchar_t qcn_stopped = B_FALSE;
910Sstevel@tonic-gate static int qcn_timeout_period = 20;	/* time out in seconds */
920Sstevel@tonic-gate size_t qcn_input_dropped;	/* dropped input character counter */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #ifdef QCN_POLLING
950Sstevel@tonic-gate static void qcn_poll_handler(void *unused);
962954Sjb145095 static cyc_time_t qcn_poll_time;
970Sstevel@tonic-gate static cyc_handler_t qcn_poll_cychandler = {
980Sstevel@tonic-gate 	qcn_poll_handler,
990Sstevel@tonic-gate 	NULL,
1000Sstevel@tonic-gate 	CY_LOW_LEVEL		/* XXX need softint to make this high */
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate static cyclic_id_t qcn_poll_cycid = CYCLIC_NONE;
1032954Sjb145095 static uint64_t	qcn_poll_interval = 5;  /* milli sec */
1041991Sheppo static uint64_t sb_interval = 0;
1051991Sheppo uint_t qcn_force_polling = 0;
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #define	QCN_MI_IDNUM		0xABCE
1090Sstevel@tonic-gate #define	QCN_MI_HIWAT		8192
1100Sstevel@tonic-gate #define	QCN_MI_LOWAT		128
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /* streams structures */
1130Sstevel@tonic-gate static struct module_info minfo = {
1140Sstevel@tonic-gate 	QCN_MI_IDNUM,	/* mi_idnum		*/
1150Sstevel@tonic-gate 	"qcn",		/* mi_idname		*/
1160Sstevel@tonic-gate 	0,		/* mi_minpsz		*/
1170Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz		*/
1180Sstevel@tonic-gate 	QCN_MI_HIWAT,	/* mi_hiwat		*/
1190Sstevel@tonic-gate 	QCN_MI_LOWAT	/* mi_lowat		*/
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static struct qinit rinit = {
1230Sstevel@tonic-gate 	putq,		/* qi_putp		*/
1240Sstevel@tonic-gate 	qcn_rsrv,	/* qi_srvp		*/
1250Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1260Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1270Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1280Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1290Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1300Sstevel@tonic-gate };
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static struct qinit winit = {
1330Sstevel@tonic-gate 	qcn_wput,	/* qi_putp		*/
1340Sstevel@tonic-gate 	qcn_wsrv,	/* qi_srvp		*/
1350Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1360Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1370Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1380Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1390Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1400Sstevel@tonic-gate };
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static struct streamtab qcnstrinfo = {
1430Sstevel@tonic-gate 	&rinit,
1440Sstevel@tonic-gate 	&winit,
1450Sstevel@tonic-gate 	NULL,
1460Sstevel@tonic-gate 	NULL
1470Sstevel@tonic-gate };
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* standard device driver structures */
1500Sstevel@tonic-gate static struct cb_ops qcn_cb_ops = {
1510Sstevel@tonic-gate 	nulldev,		/* open()		*/
1520Sstevel@tonic-gate 	nulldev,		/* close()		*/
1530Sstevel@tonic-gate 	nodev,			/* strategy()		*/
1540Sstevel@tonic-gate 	nodev,			/* print()		*/
1550Sstevel@tonic-gate 	nodev,			/* dump()		*/
1560Sstevel@tonic-gate 	nodev,			/* read()		*/
1570Sstevel@tonic-gate 	nodev,			/* write()		*/
1580Sstevel@tonic-gate 	nodev,			/* ioctl()		*/
1590Sstevel@tonic-gate 	nodev,			/* devmap()		*/
1600Sstevel@tonic-gate 	nodev,			/* mmap()		*/
1610Sstevel@tonic-gate 	nodev,			/* segmap()		*/
1620Sstevel@tonic-gate 	nochpoll,		/* poll()		*/
1630Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op()		*/
1640Sstevel@tonic-gate 	&qcnstrinfo,		/* cb_str		*/
1650Sstevel@tonic-gate 	D_NEW | D_MP		/* cb_flag		*/
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static struct dev_ops qcn_ops = {
1690Sstevel@tonic-gate 	DEVO_REV,
1700Sstevel@tonic-gate 	0,			/* refcnt		*/
1710Sstevel@tonic-gate 	qcn_getinfo,		/* getinfo()		*/
1720Sstevel@tonic-gate 	nulldev,		/* identify()		*/
1730Sstevel@tonic-gate 	nulldev,		/* probe()		*/
1740Sstevel@tonic-gate 	qcn_attach,		/* attach()		*/
1750Sstevel@tonic-gate 	qcn_detach,		/* detach()		*/
1760Sstevel@tonic-gate 	nodev,			/* reset()		*/
1770Sstevel@tonic-gate 	&qcn_cb_ops,		/* cb_ops		*/
1780Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* bus_ops		*/
1790Sstevel@tonic-gate 	NULL			/* power()		*/
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static struct modldrv modldrv = {
1830Sstevel@tonic-gate 	&mod_driverops,
1840Sstevel@tonic-gate 	"sun4v console driver v%I%",
1850Sstevel@tonic-gate 	&qcn_ops
1860Sstevel@tonic-gate };
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static struct modlinkage modlinkage = {
1890Sstevel@tonic-gate 	MODREV_1,
1900Sstevel@tonic-gate 	(void*)&modldrv,
1910Sstevel@tonic-gate 	NULL
1920Sstevel@tonic-gate };
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /* driver configuration routines */
1950Sstevel@tonic-gate int
1960Sstevel@tonic-gate _init(void)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int error;
1992282Sjb145095 	uint64_t	major, minor;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	qcn_state = kmem_zalloc(sizeof (qcn_t), KM_SLEEP);
2022282Sjb145095 	qcn_state->qcn_ring = contig_mem_alloc(RINGSIZE);
2032282Sjb145095 	if (qcn_state->qcn_ring == NULL)
2042282Sjb145095 		cmn_err(CE_PANIC, "console ring allocation failed");
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	error = mod_install(&modlinkage);
2072282Sjb145095 	if (error != 0) {
2082282Sjb145095 		contig_mem_free(qcn_state->qcn_ring, RINGSIZE);
2090Sstevel@tonic-gate 		kmem_free(qcn_state, sizeof (qcn_t));
2102282Sjb145095 		return (error);
2112282Sjb145095 	}
2122282Sjb145095 	/*
2132282Sjb145095 	 * check minor number to see if CONS_WRITE is supported
2142282Sjb145095 	 * if so, set up real address of the buffers for hv calls.
2152282Sjb145095 	 */
2160Sstevel@tonic-gate 
2172282Sjb145095 	if (((hsvc_version(HSVC_GROUP_CORE, &major, &minor) == 0) &&
2182282Sjb145095 		(major == QCN_API_MAJOR) && (minor >= QCN_API_MINOR))) {
2192282Sjb145095 		qcn_state->cons_write_buffer =
2202282Sjb145095 					contig_mem_alloc(CONS_WR_BUF_SIZE);
2212282Sjb145095 		if (qcn_state->cons_write_buffer != NULL) {
2222282Sjb145095 			qcn_state->cons_write_buf_ra =
2232282Sjb145095 					va_to_pa(qcn_state->cons_write_buffer);
2242282Sjb145095 			qcn_state->cons_transmit = qcn_transmit_write;
2252282Sjb145095 			qcn_state->cons_receive = qcn_receive_read;
2262282Sjb145095 			qcn_state->cons_read_buf_ra =
2272282Sjb145095 					va_to_pa((char *)RING_ADDR(qcn_state));
2282282Sjb145095 		}
2292282Sjb145095 	}
2302282Sjb145095 	if (qcn_state->cons_transmit == NULL) {
2312282Sjb145095 		qcn_state->cons_transmit = qcn_transmit_putchr;
2322282Sjb145095 		qcn_state->cons_receive = qcn_receive_getchr;
2332282Sjb145095 	}
2342282Sjb145095 	return (0);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate int
2380Sstevel@tonic-gate _fini(void)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	/* can't remove console driver */
2410Sstevel@tonic-gate 	return (EBUSY);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate int
2450Sstevel@tonic-gate _info(struct modinfo *modinfop)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static int
2510Sstevel@tonic-gate qcn_add_intrs(void)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	dev_info_t	*devinfo = qcn_state->qcn_dip;
2540Sstevel@tonic-gate 	int		actual, count = 0;
2550Sstevel@tonic-gate 	int 		x, y, rc, inum = 0;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/* get number of interrupts */
2590Sstevel@tonic-gate 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
2600Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (count == 0)) {
2610Sstevel@tonic-gate 		return (DDI_FAILURE);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/* Allocate an array of interrupt handles */
2650Sstevel@tonic-gate 	qcn_state->qcn_intr_size = count * sizeof (ddi_intr_handle_t);
2660Sstevel@tonic-gate 	qcn_state->qcn_htable = kmem_zalloc(qcn_state->qcn_intr_size, KM_SLEEP);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/* call ddi_intr_alloc() */
2690Sstevel@tonic-gate 	rc = ddi_intr_alloc(devinfo, qcn_state->qcn_htable,
2700Sstevel@tonic-gate 	    DDI_INTR_TYPE_FIXED, inum, count, &actual,
2710Sstevel@tonic-gate 	    DDI_INTR_ALLOC_STRICT);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
2740Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2750Sstevel@tonic-gate 		return (DDI_FAILURE);
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (actual < count) {
2790Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2800Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2840Sstevel@tonic-gate 		return (DDI_FAILURE);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	qcn_state->qcn_intr_cnt = actual;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/* Get intr priority */
2900Sstevel@tonic-gate 	if (ddi_intr_get_pri(qcn_state->qcn_htable[0],
2910Sstevel@tonic-gate 	    &qcn_state->qcn_intr_pri) != DDI_SUCCESS) {
2920Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2930Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2970Sstevel@tonic-gate 		return (DDI_FAILURE);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/* Call ddi_intr_add_handler() */
3010Sstevel@tonic-gate 	for (x = 0; x < actual; x++) {
3020Sstevel@tonic-gate 		if (ddi_intr_add_handler(qcn_state->qcn_htable[x],
3030Sstevel@tonic-gate 		    (ddi_intr_handler_t *)qcn_hi_intr,
3040Sstevel@tonic-gate 		    (caddr_t)qcn_state, NULL) != DDI_SUCCESS) {
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 			for (y = 0; y < x; y++) {
3070Sstevel@tonic-gate 				(void) ddi_intr_remove_handler(
3080Sstevel@tonic-gate 				    qcn_state->qcn_htable[y]);
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 			for (y = 0; y < actual; y++) {
3120Sstevel@tonic-gate 				(void) ddi_intr_free(qcn_state->qcn_htable[y]);
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 			kmem_free(qcn_state->qcn_htable,
3160Sstevel@tonic-gate 			    qcn_state->qcn_intr_size);
3170Sstevel@tonic-gate 			return (DDI_FAILURE);
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	return (DDI_SUCCESS);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate static void
3250Sstevel@tonic-gate qcn_remove_intrs(void)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	int x;
3280Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3290Sstevel@tonic-gate 		(void) ddi_intr_disable(qcn_state->qcn_htable[x]);
3300Sstevel@tonic-gate 		(void) ddi_intr_remove_handler(qcn_state->qcn_htable[x]);
3310Sstevel@tonic-gate 		(void) ddi_intr_free(qcn_state->qcn_htable[x]);
3320Sstevel@tonic-gate 	}
333288Sarao 	kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate static void
3370Sstevel@tonic-gate qcn_intr_enable(void)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	int x;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3420Sstevel@tonic-gate 		(void) ddi_intr_enable(qcn_state->qcn_htable[x]);
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * qcn_attach is called at startup time.
3480Sstevel@tonic-gate  * There is only once instance of this driver.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate static int
3510Sstevel@tonic-gate qcn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	extern int ddi_create_internal_pathname(dev_info_t *, char *,
3540Sstevel@tonic-gate 	    int, minor_t);
3550Sstevel@tonic-gate 	uint_t soft_prip;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate #ifdef QCN_POLLING
3580Sstevel@tonic-gate 	char *binding_name;
3590Sstevel@tonic-gate #endif
3600Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3610Sstevel@tonic-gate 		return (DDI_FAILURE);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (ddi_create_internal_pathname(dip, "qcn",
3640Sstevel@tonic-gate 	    S_IFCHR, 0) != DDI_SUCCESS)
3650Sstevel@tonic-gate 		return (DDI_FAILURE);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	qcn_state->qcn_soft_pend = 0;
3680Sstevel@tonic-gate 	qcn_state->qcn_hangup = 0;
3690Sstevel@tonic-gate 	qcn_state->qcn_rbuf_overflow = 0;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/* prepare some data structures in soft state */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	qcn_state->qcn_dip = dip;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	qcn_state->qcn_polling = 0;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate #ifdef QCN_POLLING
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * This test is for the sole purposes of allowing
3800Sstevel@tonic-gate 	 * the console to work on older firmware releases.
3810Sstevel@tonic-gate 	 */
3820Sstevel@tonic-gate 	binding_name = ddi_binding_name(qcn_state->qcn_dip);
3831991Sheppo 	if ((strcmp(binding_name, "qcn") == 0) ||
3841991Sheppo 	    (qcn_force_polling))
3850Sstevel@tonic-gate 		qcn_state->qcn_polling = 1;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
3880Sstevel@tonic-gate 		qcn_poll_time.cyt_when = 0ull;
3890Sstevel@tonic-gate 		qcn_poll_time.cyt_interval =
3900Sstevel@tonic-gate 		    qcn_poll_interval * 1000ull * 1000ull;
3910Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3920Sstevel@tonic-gate 		qcn_poll_cycid = cyclic_add(&qcn_poll_cychandler,
3930Sstevel@tonic-gate 		    &qcn_poll_time);
3940Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate #endif
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
3990Sstevel@tonic-gate 		if (qcn_add_intrs() != DDI_SUCCESS) {
4000Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_intr failed\n");
4010Sstevel@tonic-gate 			return (DDI_FAILURE);
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		if (ddi_intr_add_softint(dip, &qcn_state->qcn_softint_hdl,
4040Sstevel@tonic-gate 		    DDI_INTR_SOFTPRI_MAX, qcn_soft_intr,
4050Sstevel@tonic-gate 		    (caddr_t)qcn_state) != DDI_SUCCESS) {
4060Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_soft_intr failed\n");
4070Sstevel@tonic-gate 			qcn_remove_intrs();
4080Sstevel@tonic-gate 			return (DDI_FAILURE);
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 		if (ddi_intr_get_softint_pri(qcn_state->qcn_softint_hdl,
4110Sstevel@tonic-gate 		    &soft_prip) != DDI_SUCCESS) {
4120Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: softint_pri failed\n");
413288Sarao 			(void) ddi_intr_remove_softint(
414288Sarao 			    qcn_state->qcn_softint_hdl);
4150Sstevel@tonic-gate 			qcn_remove_intrs();
4160Sstevel@tonic-gate 			return (DDI_FAILURE);
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 
4192954Sjb145095 	mutex_init(&qcn_state->qcn_hi_lock, NULL, MUTEX_DRIVER,
4202954Sjb145095 	    (void *)(uintptr_t)(qcn_state->qcn_intr_pri));
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_lock, NULL, MUTEX_DRIVER, NULL);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/*
426*5974Sjm22469 	 * Fill in the polled I/O structure.
427*5974Sjm22469 	 */
428*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
429*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_argument =
430*5974Sjm22469 	    (cons_polledio_arg_t)qcn_state;
431*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_putchar = qcn_polledio_putchar;
432*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_getchar = qcn_polledio_getchar;
433*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_ischar = qcn_polledio_ischar;
434*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_enter = qcn_polledio_enter;
435*5974Sjm22469 	qcn_state->qcn_polledio.cons_polledio_exit = qcn_polledio_exit;
436*5974Sjm22469 
437*5974Sjm22469 	/*
4380Sstevel@tonic-gate 	 *  Enable  interrupts
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
4410Sstevel@tonic-gate 		qcn_intr_enable();
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate #ifdef QCN_DEBUG
4440Sstevel@tonic-gate 	prom_printf("qcn_attach(): qcn driver attached\n");
4450Sstevel@tonic-gate #endif
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	return (DDI_SUCCESS);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /* ARGSUSED */
4520Sstevel@tonic-gate static int
4530Sstevel@tonic-gate qcn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4570Sstevel@tonic-gate 		return (DDI_FAILURE);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate #ifdef QCN_DEBUG
4610Sstevel@tonic-gate 	prom_printf("qcn_detach(): QCN driver detached\n");
4620Sstevel@tonic-gate #endif
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate #ifdef QCN_POLLING
4650Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
4660Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4670Sstevel@tonic-gate 		if (qcn_poll_cycid != CYCLIC_NONE)
4680Sstevel@tonic-gate 			cyclic_remove(qcn_poll_cycid);
4690Sstevel@tonic-gate 		qcn_poll_cycid = CYCLIC_NONE;
4700Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate #endif
4730Sstevel@tonic-gate 
4742954Sjb145095 	if (!qcn_state->qcn_polling)
4750Sstevel@tonic-gate 		qcn_remove_intrs();
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	return (DDI_SUCCESS);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /* ARGSUSED */
4810Sstevel@tonic-gate static int
4820Sstevel@tonic-gate qcn_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	int error = DDI_FAILURE;
4850Sstevel@tonic-gate 	int instance = 0;
4860Sstevel@tonic-gate 	switch (infocmd) {
4870Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4880Sstevel@tonic-gate 		if (qcn_state) {
4890Sstevel@tonic-gate #ifdef QCN_DEBUG
4900Sstevel@tonic-gate 			prom_printf("qcn_getinfo(): devt2dip %lx\n", arg);
4910Sstevel@tonic-gate #endif
4920Sstevel@tonic-gate 			*result = (void *)qcn_state->qcn_dip;
4930Sstevel@tonic-gate 			error = DDI_SUCCESS;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 		break;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4980Sstevel@tonic-gate #ifdef QCN_DEBUG
4990Sstevel@tonic-gate 		prom_printf("qcn_getinfo(): devt2instance %lx\n", arg);
5000Sstevel@tonic-gate #endif
5010Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
502911Siskreen 			*result = (void *)(uintptr_t)instance;
5030Sstevel@tonic-gate 			error = DDI_SUCCESS;
5040Sstevel@tonic-gate 		}
5050Sstevel@tonic-gate 		break;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	return (error);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate /* streams open & close */
5120Sstevel@tonic-gate /* ARGSUSED */
5130Sstevel@tonic-gate static int
5140Sstevel@tonic-gate qcn_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	tty_common_t *tty;
5170Sstevel@tonic-gate 	int unit = getminor(*devp);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate #ifdef QCN_DEBUG
5200Sstevel@tonic-gate 	prom_printf("qcn_open(): minor %x\n", unit);
5210Sstevel@tonic-gate #endif
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (unit != 0)
5240Sstevel@tonic-gate 		return (ENXIO);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/* stream already open */
5270Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5280Sstevel@tonic-gate 		return (DDI_SUCCESS);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (!qcn_state) {
5310Sstevel@tonic-gate 		cmn_err(CE_WARN, "qcn_open: console was not configured by "
5320Sstevel@tonic-gate 		    "autoconfig\n");
5330Sstevel@tonic-gate 		return (ENXIO);
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5370Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	tty->t_readq = q;
5400Sstevel@tonic-gate 	tty->t_writeq = WR(q);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/* Link the RD and WR Q's */
5430Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)qcn_state;
5440Sstevel@tonic-gate 	qcn_state->qcn_readq = RD(q);
5450Sstevel@tonic-gate 	qcn_state->qcn_writeq = WR(q);
5460Sstevel@tonic-gate 	qprocson(q);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate #ifdef QCN_DEBUG
5510Sstevel@tonic-gate 	prom_printf("qcn_open: opened as dev %lx\n", *devp);
5520Sstevel@tonic-gate #endif
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	return (DDI_SUCCESS);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate /* ARGSUSED */
5580Sstevel@tonic-gate static int
5590Sstevel@tonic-gate qcn_close(queue_t *q, int flag, cred_t *credp)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	if (qcn_state->qcn_wbufcid != 0) {
5650Sstevel@tonic-gate 		unbufcall(qcn_state->qcn_wbufcid);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	ttycommon_close(&qcn_state->qcn_tty);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	qprocsoff(q);
5700Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
5710Sstevel@tonic-gate 	qcn_state->qcn_readq = NULL;
5720Sstevel@tonic-gate 	qcn_state->qcn_writeq = NULL;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	return (DDI_SUCCESS);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Put procedure for write queue.
5790Sstevel@tonic-gate  * Respond to M_IOCTL, M_DATA and M_FLUSH messages here;
5800Sstevel@tonic-gate  * It put's the data onto internal qcn_output_q.
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate static int
5830Sstevel@tonic-gate qcn_wput(queue_t *q, mblk_t *mp)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate #ifdef QCN_DEBUG
5870Sstevel@tonic-gate 	struct iocblk *iocp;
5880Sstevel@tonic-gate 	int i;
5890Sstevel@tonic-gate #endif
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (!mp->b_datap) {
5940Sstevel@tonic-gate 		cmn_err(CE_PANIC, "qcn_wput: null datap");
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate #ifdef QCN_DEBUG
5980Sstevel@tonic-gate 	prom_printf("qcn_wput(): QCN wput q=%X mp=%X rd=%X wr=%X type=%X\n",
5990Sstevel@tonic-gate 		q, mp, mp->b_rptr, mp->b_wptr, mp->b_datap->db_type);
6000Sstevel@tonic-gate #endif
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
6050Sstevel@tonic-gate 	case M_IOCTL:
6060Sstevel@tonic-gate 	case M_CTL:
6070Sstevel@tonic-gate #ifdef QCN_DEBUG
6080Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
6090Sstevel@tonic-gate 		prom_printf("qcn_wput(): M_IOCTL cmd=%X TIOC=%X\n",
6100Sstevel@tonic-gate 		    iocp->ioc_cmd, TIOC);
6110Sstevel@tonic-gate #endif
6120Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
6130Sstevel@tonic-gate 		case TCSETSW:
6140Sstevel@tonic-gate 		case TCSETSF:
6150Sstevel@tonic-gate 		case TCSETAW:
6160Sstevel@tonic-gate 		case TCSETAF:
6170Sstevel@tonic-gate 		case TCSBRK:
6180Sstevel@tonic-gate 			/*
6190Sstevel@tonic-gate 			 * The change do not take effect until all
6200Sstevel@tonic-gate 			 * output queued before them is drained.
6210Sstevel@tonic-gate 			 * Put this message on the queue, so that
6220Sstevel@tonic-gate 			 * "qcn_start" will see it when it's done
6230Sstevel@tonic-gate 			 * with the output before it. Poke the start
6240Sstevel@tonic-gate 			 * routine, just in case.
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			(void) putq(q, mp);
6270Sstevel@tonic-gate 			qcn_start();
6280Sstevel@tonic-gate 			break;
6290Sstevel@tonic-gate 		default:
6303419Sjb145095 			mutex_exit(&qcn_state->qcn_lock);
6310Sstevel@tonic-gate 			qcn_ioctl(q, mp);
6323419Sjb145095 			mutex_enter(&qcn_state->qcn_lock);
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		break;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	case M_FLUSH:
6370Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
6380Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
6390Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
6420Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
6430Sstevel@tonic-gate 			qreply(q, mp);
6440Sstevel@tonic-gate 		} else {
6450Sstevel@tonic-gate 			freemsg(mp);
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 		break;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	case M_STOP:
6500Sstevel@tonic-gate 		qcn_stopped = B_TRUE;
6510Sstevel@tonic-gate 		freemsg(mp);
6520Sstevel@tonic-gate 		break;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	case M_START:
6550Sstevel@tonic-gate 		qcn_stopped = B_FALSE;
6560Sstevel@tonic-gate 		freemsg(mp);
6570Sstevel@tonic-gate 		qenable(q);	/* Start up delayed messages */
6580Sstevel@tonic-gate 		break;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	case M_DATA:
6610Sstevel@tonic-gate 		/*
6620Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
6630Sstevel@tonic-gate 		 * and poke the start routine.
6640Sstevel@tonic-gate 		 */
6650Sstevel@tonic-gate #ifdef QCN_DEBUG
6660Sstevel@tonic-gate 		if (mp->b_rptr < mp->b_wptr) {
6670Sstevel@tonic-gate 		prom_printf("qcn_wput(): DATA q=%X mp=%X rd=%X wr=%X\n",
6680Sstevel@tonic-gate 			q, mp, mp->b_rptr, mp->b_wptr);
6690Sstevel@tonic-gate 		prom_printf("qcn_wput(): [");
6700Sstevel@tonic-gate 		for (i = 0; i < mp->b_wptr-mp->b_rptr; i++) {
6710Sstevel@tonic-gate 			prom_printf("%c", *(mp->b_rptr+i));
6720Sstevel@tonic-gate 		}
6730Sstevel@tonic-gate 		prom_printf("]\n");
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate #endif /* QCN_DEBUG */
6760Sstevel@tonic-gate 		(void) putq(q, mp);
6770Sstevel@tonic-gate 		qcn_start();
6780Sstevel@tonic-gate 		break;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	default:
6810Sstevel@tonic-gate 		freemsg(mp);
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
6853419Sjb145095 
6860Sstevel@tonic-gate 	return (0);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6910Sstevel@tonic-gate  */
6920Sstevel@tonic-gate static void
6930Sstevel@tonic-gate qcn_ioctl(queue_t *q, mblk_t *mp)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	struct iocblk	*iocp;
6960Sstevel@tonic-gate 	tty_common_t	*tty;
6970Sstevel@tonic-gate 	mblk_t		*datamp;
6980Sstevel@tonic-gate 	int		data_size;
6990Sstevel@tonic-gate 	int		error = 0;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate #ifdef QCN_DEBUG
7020Sstevel@tonic-gate 	prom_printf("qcn_ioctl(): q=%X mp=%X\n", q, mp);
7030Sstevel@tonic-gate #endif
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
706*5974Sjm22469 
7070Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if (tty->t_iocpending != NULL) {
7100Sstevel@tonic-gate 		freemsg(tty->t_iocpending);
7110Sstevel@tonic-gate 		tty->t_iocpending = NULL;
7120Sstevel@tonic-gate 	}
713*5974Sjm22469 
714*5974Sjm22469 	/*
715*5974Sjm22469 	 * Handle the POLLEDIO ioctls now because ttycommon_ioctl
716*5974Sjm22469 	 * (below) frees up the message block (mp->b_cont) which
717*5974Sjm22469 	 * contains the pointer used to pass back results.
718*5974Sjm22469 	 */
719*5974Sjm22469 	switch (iocp->ioc_cmd) {
720*5974Sjm22469 	case CONSOPENPOLLEDIO:
721*5974Sjm22469 		error = miocpullup(mp, sizeof (struct cons_polledio *));
722*5974Sjm22469 		if (error != 0)
723*5974Sjm22469 			break;
724*5974Sjm22469 
725*5974Sjm22469 		*(struct cons_polledio **)mp->b_cont->b_rptr =
726*5974Sjm22469 		    &qcn_state->qcn_polledio;
727*5974Sjm22469 
728*5974Sjm22469 		mp->b_datap->db_type = M_IOCACK;
729*5974Sjm22469 		break;
730*5974Sjm22469 
731*5974Sjm22469 	case CONSCLOSEPOLLEDIO:
732*5974Sjm22469 		mp->b_datap->db_type = M_IOCACK;
733*5974Sjm22469 		iocp->ioc_error = 0;
734*5974Sjm22469 		iocp->ioc_rval = 0;
735*5974Sjm22469 		break;
736*5974Sjm22469 
737*5974Sjm22469 	default:
738*5974Sjm22469 		data_size = ttycommon_ioctl(tty, q, mp, &error);
739*5974Sjm22469 		if (data_size != 0) {
740*5974Sjm22469 			if (qcn_state->qcn_wbufcid)
741*5974Sjm22469 				unbufcall(qcn_state->qcn_wbufcid);
742*5974Sjm22469 			/* call qcn_reioctl() */
743*5974Sjm22469 			qcn_state->qcn_wbufcid =
744*5974Sjm22469 			    bufcall(data_size, BPRI_HI, qcn_reioctl, qcn_state);
745*5974Sjm22469 			return;
746*5974Sjm22469 		}
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7493419Sjb145095 	mutex_enter(&qcn_state->qcn_lock);
7503419Sjb145095 
7510Sstevel@tonic-gate 	if (error < 0) {
7520Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
7530Sstevel@tonic-gate 		/*
7540Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		error = 0;
7570Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
7580Sstevel@tonic-gate 		case TCSBRK:
7590Sstevel@tonic-gate 		case TIOCSBRK:
7600Sstevel@tonic-gate 		case TIOCCBRK:
7610Sstevel@tonic-gate 		case TIOCMSET:
7620Sstevel@tonic-gate 		case TIOCMBIS:
7630Sstevel@tonic-gate 		case TIOCMBIC:
7640Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7650Sstevel@tonic-gate 				qcn_ack(mp, NULL, 0);
7660Sstevel@tonic-gate 			else
7670Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
7680Sstevel@tonic-gate 			break;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		case TIOCMGET:
7710Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
7720Sstevel@tonic-gate 			if (datamp == NULL) {
7730Sstevel@tonic-gate 				error = EAGAIN;
7740Sstevel@tonic-gate 				break;
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 			*(int *)datamp->b_rptr = 0;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7800Sstevel@tonic-gate 				qcn_ack(mp, datamp, sizeof (int));
7810Sstevel@tonic-gate 			else
7820Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
7830Sstevel@tonic-gate 			break;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 		default:
7860Sstevel@tonic-gate 			error = EINVAL;
7870Sstevel@tonic-gate 			break;
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	if (error != 0) {
7910Sstevel@tonic-gate 		iocp->ioc_count = 0;
7920Sstevel@tonic-gate 		iocp->ioc_error = error;
7930Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7940Sstevel@tonic-gate 	}
7953419Sjb145095 	mutex_exit(&qcn_state->qcn_lock);
7960Sstevel@tonic-gate 	qreply(q, mp);
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate static void
8000Sstevel@tonic-gate qcn_reioctl(void *unit)
8010Sstevel@tonic-gate {
8020Sstevel@tonic-gate 	queue_t		*q;
8030Sstevel@tonic-gate 	mblk_t		*mp;
8040Sstevel@tonic-gate 	qcn_t		*qcnp = (qcn_t *)unit;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (!qcnp->qcn_wbufcid)
8070Sstevel@tonic-gate 		return;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	qcnp->qcn_wbufcid = 0;
8100Sstevel@tonic-gate 	if ((q = qcnp->qcn_tty.t_writeq) == NULL)
8110Sstevel@tonic-gate 		return;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	if ((mp = qcnp->qcn_tty.t_iocpending) == NULL)
8140Sstevel@tonic-gate 		return;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	qcnp->qcn_tty.t_iocpending = NULL;
8170Sstevel@tonic-gate 	qcn_ioctl(q, mp);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate static void
8210Sstevel@tonic-gate qcn_ack(mblk_t *mp, mblk_t *dp, uint_t size)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	struct iocblk  *iocp = (struct iocblk *)mp->b_rptr;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCACK;
8260Sstevel@tonic-gate 	iocp->ioc_count = size;
8270Sstevel@tonic-gate 	iocp->ioc_error = 0;
8280Sstevel@tonic-gate 	iocp->ioc_rval = 0;
8290Sstevel@tonic-gate 	if (mp->b_cont != NULL)
8300Sstevel@tonic-gate 		freeb(mp->b_cont);
8310Sstevel@tonic-gate 	if (dp != NULL) {
8320Sstevel@tonic-gate 		mp->b_cont = dp;
8330Sstevel@tonic-gate 		dp->b_wptr += size;
8340Sstevel@tonic-gate 	} else
8350Sstevel@tonic-gate 		mp->b_cont = NULL;
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate static void
8390Sstevel@tonic-gate qcn_start(void)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	queue_t *q;
8430Sstevel@tonic-gate 	mblk_t *mp;
8440Sstevel@tonic-gate 	int rv;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/*
8490Sstevel@tonic-gate 	 * read stream queue and remove data from the queue and
8500Sstevel@tonic-gate 	 * transmit them if possible
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
8530Sstevel@tonic-gate 	ASSERT(q != NULL);
8540Sstevel@tonic-gate 	while (mp = getq(q)) {
8550Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
8560Sstevel@tonic-gate 			/*
8570Sstevel@tonic-gate 			 * These are those IOCTLs queued up
8580Sstevel@tonic-gate 			 * do it now
8590Sstevel@tonic-gate 			 */
8603419Sjb145095 			mutex_exit(&qcn_state->qcn_lock);
8610Sstevel@tonic-gate 			qcn_ioctl(q, mp);
8623419Sjb145095 			mutex_enter(&qcn_state->qcn_lock);
8630Sstevel@tonic-gate 			continue;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 		/*
8660Sstevel@tonic-gate 		 * M_DATA
8670Sstevel@tonic-gate 		 */
8682282Sjb145095 		rv = qcn_state->cons_transmit(q, mp);
8690Sstevel@tonic-gate 		if (rv == EBUSY || rv == EAGAIN)
8700Sstevel@tonic-gate 			return;
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate static int
8752282Sjb145095 qcn_transmit_write(queue_t *q, mblk_t *mp)
8760Sstevel@tonic-gate {
8772282Sjb145095 	mblk_t		*bp;
8782282Sjb145095 	size_t		len;
8792282Sjb145095 	uint64_t	i;
8802282Sjb145095 	uint64_t	retval = 0;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate #ifdef QCN_DEBUG
8832282Sjb145095 	prom_printf("qcn_transmit_write(): q=%X mp=%X\n", q, mp);
8840Sstevel@tonic-gate #endif
8852282Sjb145095 
8862282Sjb145095 	while (mp) {
8872282Sjb145095 		bp = mp;
8882282Sjb145095 		len = bp->b_wptr - bp->b_rptr;
8892282Sjb145095 		/*
8902282Sjb145095 		 * Use the console write call to send a block of characters to
8912282Sjb145095 		 * the console.
8922282Sjb145095 		 */
8932282Sjb145095 		i = (len > CONS_WR_BUF_SIZE) ? CONS_WR_BUF_SIZE : len;
8942282Sjb145095 		bcopy(bp->b_rptr, qcn_state->cons_write_buffer, i);
8952282Sjb145095 		retval = hv_cnwrite(qcn_state->cons_write_buf_ra, i, &i);
8962282Sjb145095 
8972282Sjb145095 		if (retval == H_EOK) {
8982282Sjb145095 			len -= i;
8992282Sjb145095 			bp->b_rptr += i;
9002282Sjb145095 			/*
9012282Sjb145095 			 * if we have finished with this buf, free
9022282Sjb145095 			 * and get the next buf if present.
9032282Sjb145095 			 */
9042282Sjb145095 			if (len == 0) {
9052282Sjb145095 				mp = bp->b_cont;
9062282Sjb145095 				freeb(bp);
9072282Sjb145095 			}
9082282Sjb145095 		} else {
9092282Sjb145095 			(void) putbq(q, mp);
9102282Sjb145095 
9112282Sjb145095 			switch (retval) {
9122282Sjb145095 
9132282Sjb145095 			case H_EWOULDBLOCK :
9142282Sjb145095 				/*
9152282Sjb145095 				 * hypervisor cannot process the request -
9162282Sjb145095 				 * channel busy.  Try again later.
9172282Sjb145095 				 */
9182282Sjb145095 				return (EAGAIN);
9192282Sjb145095 
9202282Sjb145095 			case H_EIO :
9212282Sjb145095 				return (EIO);
9222282Sjb145095 			default :
9232282Sjb145095 				return (ENXIO);
9242282Sjb145095 			}
9252282Sjb145095 		}
9262282Sjb145095 	}
9272282Sjb145095 	return (0);
9282282Sjb145095 }
9292282Sjb145095 
9302282Sjb145095 static int
9312282Sjb145095 qcn_transmit_putchr(queue_t *q, mblk_t *mp)
9322282Sjb145095 {
9332282Sjb145095 	caddr_t		buf;
9342282Sjb145095 	mblk_t		*bp;
9352282Sjb145095 	size_t		len;
9362282Sjb145095 	uint64_t	i;
9372282Sjb145095 
9382282Sjb145095 #ifdef QCN_DEBUG
9392282Sjb145095 	prom_printf("qcn_transmit_putchr(): q=%X mp=%X\n", q, mp);
9402282Sjb145095 #endif
9412282Sjb145095 	while (mp) {
9420Sstevel@tonic-gate 		bp = mp;
9430Sstevel@tonic-gate 		len = bp->b_wptr - bp->b_rptr;
9440Sstevel@tonic-gate 		buf = (caddr_t)bp->b_rptr;
9450Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
9462954Sjb145095 			if (hv_cnputchar(buf[i]) == H_EWOULDBLOCK)
9472531Snarayan 				break;
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 		if (i != len) {
9500Sstevel@tonic-gate 			bp->b_rptr += i;
9510Sstevel@tonic-gate 			(void) putbq(q, mp);
9520Sstevel@tonic-gate 			return (EAGAIN);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		mp = bp->b_cont;
9550Sstevel@tonic-gate 		freeb(bp);
9562282Sjb145095 	}
9570Sstevel@tonic-gate 	return (0);
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate /*
9610Sstevel@tonic-gate  * called when SC first establishes console connection
9620Sstevel@tonic-gate  * drop all the data on the output queue
9630Sstevel@tonic-gate  */
9640Sstevel@tonic-gate static void
9650Sstevel@tonic-gate qcn_flush(void)
9660Sstevel@tonic-gate {
9670Sstevel@tonic-gate 	queue_t *q;
9680Sstevel@tonic-gate 	mblk_t *mp;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
9730Sstevel@tonic-gate 
974911Siskreen 	prom_printf("qcn_flush(): WARNING console output is dropped time=%lx\n",
9750Sstevel@tonic-gate 	    gethrestime_sec());
9760Sstevel@tonic-gate 	while (mp = getq(q))
9770Sstevel@tonic-gate 		freemsg(mp);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate static void
9810Sstevel@tonic-gate qcn_trigger_softint(void)
9820Sstevel@tonic-gate {
9832282Sjb145095 	/*
9842282Sjb145095 	 * if we are not currently servicing a software interrupt
9852282Sjb145095 	 * (qcn_soft_pend == 0), trigger the service routine to run.
9862282Sjb145095 	 */
9872432Sjb145095 	if (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_DO) ==
9882432Sjb145095 				QCN_SP_IDL) {
9892282Sjb145095 		(void) ddi_intr_trigger_softint(
9900Sstevel@tonic-gate 			    qcn_state->qcn_softint_hdl, NULL);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*ARGSUSED*/
9950Sstevel@tonic-gate static uint_t
9960Sstevel@tonic-gate qcn_soft_intr(caddr_t arg1, caddr_t arg2)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	mblk_t *mp;
9990Sstevel@tonic-gate 	int	cc;
10002282Sjb145095 	int	overflow_check;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	do {
10032432Sjb145095 		(void) atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IP);
10040Sstevel@tonic-gate 		mutex_enter(&qcn_state->qcn_hi_lock);
10053419Sjb145095 		cc = RING_CNT(qcn_state);
10063419Sjb145095 		mutex_exit(&qcn_state->qcn_hi_lock);
10073419Sjb145095 		if (cc <= 0) {
10082282Sjb145095 			goto out;
10092282Sjb145095 		}
10100Sstevel@tonic-gate 
10112282Sjb145095 		if ((mp = allocb(cc, BPRI_MED)) == NULL) {
10123419Sjb145095 			mutex_enter(&qcn_state->qcn_hi_lock);
10132282Sjb145095 			qcn_input_dropped += cc;
10142282Sjb145095 			mutex_exit(&qcn_state->qcn_hi_lock);
10152282Sjb145095 			cmn_err(CE_WARN, "qcn_intr: allocb"
10162282Sjb145095 			    "failed (console input dropped)");
10172282Sjb145095 			goto out;
10182282Sjb145095 		}
10192282Sjb145095 
10203419Sjb145095 		mutex_enter(&qcn_state->qcn_hi_lock);
10212282Sjb145095 		do {
10222282Sjb145095 			/* put console input onto stream */
10232282Sjb145095 			*(char *)mp->b_wptr++ = RING_GET(qcn_state);
10242282Sjb145095 		} while (--cc);
10252282Sjb145095 
10262282Sjb145095 		if ((overflow_check = qcn_state->qcn_rbuf_overflow) != 0) {
10272282Sjb145095 			qcn_state->qcn_rbuf_overflow = 0;
10282282Sjb145095 		}
10292282Sjb145095 		mutex_exit(&qcn_state->qcn_hi_lock);
10300Sstevel@tonic-gate 
10312282Sjb145095 		if (overflow_check) {
10322282Sjb145095 			cmn_err(CE_WARN, "qcn: Ring buffer overflow\n");
10332282Sjb145095 		}
10342282Sjb145095 
10352282Sjb145095 		if (qcn_state->qcn_readq) {
10362282Sjb145095 			putnext(qcn_state->qcn_readq, mp);
10370Sstevel@tonic-gate 		}
10382282Sjb145095 out:
10392954Sjb145095 		/*
10402954Sjb145095 		 * If there are pending transmits because hypervisor
10412954Sjb145095 		 * returned EWOULDBLOCK poke start now.
10422954Sjb145095 		 */
10432954Sjb145095 
10442954Sjb145095 		if (qcn_state->qcn_writeq != NULL) {
10452954Sjb145095 			if (qcn_state->qcn_hangup) {
10462954Sjb145095 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
10472282Sjb145095 				flushq(qcn_state->qcn_writeq, FLUSHDATA);
10482954Sjb145095 				qcn_state->qcn_hangup = 0;
10492954Sjb145095 			} else {
10502954Sjb145095 				mutex_enter(&qcn_state->qcn_lock);
10512954Sjb145095 				qcn_start();
10522954Sjb145095 				mutex_exit(&qcn_state->qcn_lock);
10532282Sjb145095 			}
10542282Sjb145095 		}
10552432Sjb145095 		/*
10562432Sjb145095 		 * now loop if another interrupt came in (qcn_trigger_softint
10572432Sjb145095 		 * called) while we were processing the loop
10582432Sjb145095 		 */
10592432Sjb145095 	} while (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IDL) ==
10602432Sjb145095 								QCN_SP_DO);
10610Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate /*ARGSUSED*/
10650Sstevel@tonic-gate static uint_t
10660Sstevel@tonic-gate qcn_hi_intr(caddr_t arg)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_hi_lock);
10690Sstevel@tonic-gate 
10702282Sjb145095 	qcn_state->cons_receive();
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_hi_lock);
10730Sstevel@tonic-gate 	qcn_trigger_softint();
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10782282Sjb145095 static void
10792282Sjb145095 qcn_receive_read(void)
10802282Sjb145095 {
10812282Sjb145095 	int64_t rv;
10822282Sjb145095 	uint8_t *bufp;
10832282Sjb145095 	int64_t	retcount = 0;
10842282Sjb145095 	int	i;
10852282Sjb145095 
10862282Sjb145095 	do {
10872282Sjb145095 		/*
10882282Sjb145095 		 * Maximize available buffer size
10892282Sjb145095 		 */
10902282Sjb145095 		if (RING_CNT(qcn_state) <= 0) {
10912282Sjb145095 			RING_INIT(qcn_state);
10922282Sjb145095 		}
10932282Sjb145095 		rv = hv_cnread(qcn_state->cons_read_buf_ra +
10942282Sjb145095 				RING_POFF(qcn_state),
10952282Sjb145095 				RING_LEFT(qcn_state),
10962282Sjb145095 				&retcount);
10972282Sjb145095 		bufp = RING_ADDR(qcn_state);
10982282Sjb145095 		if (rv == H_EOK) {
10992282Sjb145095 			/*
11002282Sjb145095 			 * if the alternate break sequence is enabled, test
11012282Sjb145095 			 * the buffer for the sequence and if it is there,
11022282Sjb145095 			 * enter the debugger.
11032282Sjb145095 			 */
11042282Sjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
11052282Sjb145095 				for (i = 0; i < retcount; i++) {
11062282Sjb145095 					if (abort_charseq_recognize(*bufp++)) {
11072282Sjb145095 						abort_sequence_enter(
11082282Sjb145095 								(char *)NULL);
11092282Sjb145095 					}
11102282Sjb145095 				}
11112282Sjb145095 			}
11122282Sjb145095 
11132282Sjb145095 			/* put console input onto stream */
11142282Sjb145095 			if (retcount > 0) {
11152282Sjb145095 				/*
11162282Sjb145095 				 * the characters are already in the ring,
11172282Sjb145095 				 * just update the pointer so the characters
11182282Sjb145095 				 * can be retrieved.
11192282Sjb145095 				 */
11202282Sjb145095 				RING_UPD(qcn_state, retcount);
11212282Sjb145095 			}
11222282Sjb145095 		} else {
11232282Sjb145095 			switch (rv) {
11242282Sjb145095 
11252282Sjb145095 			case H_EWOULDBLOCK :
11262282Sjb145095 				/*
11272282Sjb145095 				 * hypervisor cannot handle the request.
11282282Sjb145095 				 * Try again later.
11292282Sjb145095 				 */
11302282Sjb145095 				break;
11312282Sjb145095 
11322282Sjb145095 
11332282Sjb145095 			case H_BREAK :
11342282Sjb145095 				/*
11352282Sjb145095 				 * on break, unless alternate break sequence is
11362282Sjb145095 				 * enabled, enter the debugger
11372282Sjb145095 				 */
11382282Sjb145095 				if (abort_enable != KIOCABORTALTERNATE)
11392282Sjb145095 					abort_sequence_enter((char *)NULL);
11402282Sjb145095 				break;
11412282Sjb145095 
11422282Sjb145095 			case H_HUP :
11432282Sjb145095 				qcn_state->qcn_hangup = 1;
11442282Sjb145095 				break;
11452282Sjb145095 
11462282Sjb145095 			default :
11472282Sjb145095 				break;
11482282Sjb145095 			}
11492282Sjb145095 		}
11502282Sjb145095 	} while (rv == H_EOK);
11512282Sjb145095 }
11522282Sjb145095 
11532282Sjb145095 static void
11542282Sjb145095 qcn_receive_getchr(void)
11552282Sjb145095 {
11562282Sjb145095 	int64_t rv;
11572282Sjb145095 	uint8_t	buf;
11582282Sjb145095 
11592282Sjb145095 	do {
11602282Sjb145095 		rv = hv_cngetchar(&buf);
11612282Sjb145095 		if (rv == H_EOK) {
11622282Sjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
11632282Sjb145095 				if (abort_charseq_recognize(buf)) {
11642282Sjb145095 					abort_sequence_enter((char *)NULL);
11652282Sjb145095 				}
11662282Sjb145095 			}
11672282Sjb145095 
11682282Sjb145095 			/* put console input onto stream */
11692282Sjb145095 			if (RING_POK(qcn_state, 1)) {
11702282Sjb145095 				RING_PUT(qcn_state, buf);
11712282Sjb145095 			} else {
11722282Sjb145095 				qcn_state->qcn_rbuf_overflow++;
11732282Sjb145095 			}
11742282Sjb145095 		} else {
11752282Sjb145095 			if (rv == H_BREAK) {
11762282Sjb145095 				if (abort_enable != KIOCABORTALTERNATE)
11772282Sjb145095 					abort_sequence_enter((char *)NULL);
11782282Sjb145095 			}
11792282Sjb145095 
11802282Sjb145095 			if (rv == H_HUP)  {
11812282Sjb145095 				qcn_state->qcn_hangup = 1;
11822282Sjb145095 			}
11832282Sjb145095 			return;
11842282Sjb145095 		}
11852282Sjb145095 	} while (rv == H_EOK);
11862282Sjb145095 }
11872282Sjb145095 
11880Sstevel@tonic-gate #ifdef QCN_POLLING
11890Sstevel@tonic-gate /*ARGSUSED*/
11900Sstevel@tonic-gate static void
11910Sstevel@tonic-gate qcn_poll_handler(void *unused)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	mblk_t *mp;
11940Sstevel@tonic-gate 	int64_t rv;
11950Sstevel@tonic-gate 	uint8_t buf;
11960Sstevel@tonic-gate 	int qcn_writeq_flush = 0;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	/* LINTED: E_CONSTANT_CONDITION */
11990Sstevel@tonic-gate 	while (1) {
12000Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
12010Sstevel@tonic-gate 		if (rv == H_BREAK) {
12020Sstevel@tonic-gate 			if (abort_enable != KIOCABORTALTERNATE)
12030Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		if (rv == H_HUP)  {
12070Sstevel@tonic-gate 			if (qcn_state->qcn_readq) {
12080Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
12090Sstevel@tonic-gate 				qcn_writeq_flush = 1;
12100Sstevel@tonic-gate 			}
12110Sstevel@tonic-gate 			goto out;
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		if (rv != H_EOK)
12150Sstevel@tonic-gate 			goto out;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		if (abort_enable == KIOCABORTALTERNATE) {
12180Sstevel@tonic-gate 			if (abort_charseq_recognize(buf)) {
12190Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
12200Sstevel@tonic-gate 			}
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		/* put console input onto stream */
12240Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
12250Sstevel@tonic-gate 			if ((mp = allocb(1, BPRI_MED)) == NULL) {
12260Sstevel@tonic-gate 				qcn_input_dropped++;
12270Sstevel@tonic-gate 				cmn_err(CE_WARN, "qcn_intr: allocb"
12280Sstevel@tonic-gate 				    "failed (console input dropped)");
12290Sstevel@tonic-gate 				return;
12300Sstevel@tonic-gate 			}
12310Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = buf;
12320Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate out:
12360Sstevel@tonic-gate /*
12370Sstevel@tonic-gate  * If there are pending transmits because hypervisor
12380Sstevel@tonic-gate  * returned EWOULDBLOCK poke start now.
12390Sstevel@tonic-gate  */
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12420Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL) {
12430Sstevel@tonic-gate 		if (qcn_writeq_flush) {
12440Sstevel@tonic-gate 			flushq(qcn_state->qcn_writeq, FLUSHDATA);
12450Sstevel@tonic-gate 		} else {
12460Sstevel@tonic-gate 			qcn_start();
12470Sstevel@tonic-gate 		}
12480Sstevel@tonic-gate 	}
12490Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate #endif
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate /*
12540Sstevel@tonic-gate  * Check for abort character sequence, copied from zs_async.c
12550Sstevel@tonic-gate  */
12560Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate static boolean_t
12590Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
12600Sstevel@tonic-gate {
12610Sstevel@tonic-gate 	static int state = 0;
12620Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	if (ch == sequence[state]) {
12650Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
12660Sstevel@tonic-gate 			state = 0;
12670Sstevel@tonic-gate 			return (B_TRUE);
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 	} else {
12700Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 	return (B_FALSE);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate static int
12770Sstevel@tonic-gate qcn_rsrv(queue_t *q)
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	mblk_t	*mp;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12820Sstevel@tonic-gate 		return (0);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
12870Sstevel@tonic-gate 		if (canputnext(q))
12880Sstevel@tonic-gate 			putnext(q, mp);
12890Sstevel@tonic-gate 		else if (mp->b_datap->db_type >= QPCTL)
12900Sstevel@tonic-gate 			(void) putbq(q, mp);
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	return (0);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /* ARGSUSED */
12990Sstevel@tonic-gate static int
13000Sstevel@tonic-gate qcn_wsrv(queue_t *q)
13010Sstevel@tonic-gate {
13020Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
13030Sstevel@tonic-gate 		return (0);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL)
13080Sstevel@tonic-gate 		qcn_start();
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	return (0);
13130Sstevel@tonic-gate }
1314*5974Sjm22469 
1315*5974Sjm22469 static boolean_t
1316*5974Sjm22469 qcn_polledio_ischar(cons_polledio_arg_t arg)
1317*5974Sjm22469 {
1318*5974Sjm22469 	qcn_t *state = (qcn_t *)arg;
1319*5974Sjm22469 
1320*5974Sjm22469 	if (state->qcn_char_available)
1321*5974Sjm22469 		return (B_TRUE);
1322*5974Sjm22469 
1323*5974Sjm22469 	return (state->qcn_char_available =
1324*5974Sjm22469 	    (hv_cngetchar(&state->qcn_hold_char) == H_EOK));
1325*5974Sjm22469 }
1326*5974Sjm22469 
1327*5974Sjm22469 
1328*5974Sjm22469 static int
1329*5974Sjm22469 qcn_polledio_getchar(cons_polledio_arg_t arg)
1330*5974Sjm22469 {
1331*5974Sjm22469 	qcn_t *state = (qcn_t *)arg;
1332*5974Sjm22469 
1333*5974Sjm22469 	while (!qcn_polledio_ischar(arg))
1334*5974Sjm22469 		drv_usecwait(10);
1335*5974Sjm22469 
1336*5974Sjm22469 	state->qcn_char_available = B_FALSE;
1337*5974Sjm22469 
1338*5974Sjm22469 	return ((int)state->qcn_hold_char);
1339*5974Sjm22469 }
1340*5974Sjm22469 
1341*5974Sjm22469 static void
1342*5974Sjm22469 qcn_polledio_putchar(cons_polledio_arg_t arg, uchar_t c)
1343*5974Sjm22469 {
1344*5974Sjm22469 	if (c == '\n')
1345*5974Sjm22469 		qcn_polledio_putchar(arg, '\r');
1346*5974Sjm22469 
1347*5974Sjm22469 	while (hv_cnputchar((uint8_t)c) == H_EWOULDBLOCK)
1348*5974Sjm22469 		drv_usecwait(10);
1349*5974Sjm22469 }
1350*5974Sjm22469 
1351*5974Sjm22469 static void
1352*5974Sjm22469 qcn_polledio_enter(cons_polledio_arg_t arg)
1353*5974Sjm22469 {
1354*5974Sjm22469 	qcn_t *state = (qcn_t *)arg;
1355*5974Sjm22469 
1356*5974Sjm22469 	state->qcn_char_available = B_FALSE;
1357*5974Sjm22469 }
1358*5974Sjm22469 
1359*5974Sjm22469 /* ARGSUSED */
1360*5974Sjm22469 static void
1361*5974Sjm22469 qcn_polledio_exit(cons_polledio_arg_t arg)
1362*5974Sjm22469 {
1363*5974Sjm22469 }
1364