xref: /onnv-gate/usr/src/uts/sun4v/io/qcn.c (revision 2282:7461ced9b80d)
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 /*
231991Sheppo  * Copyright 2006 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>
52*2282Sjb145095 #include <sys/hsvc.h>
53*2282Sjb145095 #include <sys/machsystm.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* dev_ops and cb_ops for device driver */
560Sstevel@tonic-gate static int qcn_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
570Sstevel@tonic-gate static int qcn_attach(dev_info_t *, ddi_attach_cmd_t);
580Sstevel@tonic-gate static int qcn_detach(dev_info_t *, ddi_detach_cmd_t);
590Sstevel@tonic-gate static int qcn_open(queue_t *, dev_t *, int, int, cred_t *);
600Sstevel@tonic-gate static int qcn_close(queue_t *, int, cred_t *);
610Sstevel@tonic-gate static int qcn_wput(queue_t *, mblk_t *);
620Sstevel@tonic-gate static int qcn_wsrv(queue_t *);
630Sstevel@tonic-gate static int qcn_rsrv(queue_t *);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* other internal qcn routines */
660Sstevel@tonic-gate static void qcn_ioctl(queue_t *, mblk_t *);
670Sstevel@tonic-gate static void qcn_reioctl(void *);
680Sstevel@tonic-gate static void qcn_ack(mblk_t *, mblk_t *, uint_t);
690Sstevel@tonic-gate static void qcn_start(void);
70*2282Sjb145095 static int qcn_transmit_write(queue_t *, mblk_t *);
71*2282Sjb145095 static int qcn_transmit_putchr(queue_t *, mblk_t *);
72*2282Sjb145095 static void qcn_receive_read(void);
73*2282Sjb145095 static void qcn_receive_getchr(void);
740Sstevel@tonic-gate static void qcn_flush(void);
750Sstevel@tonic-gate static uint_t qcn_hi_intr(caddr_t arg);
760Sstevel@tonic-gate static uint_t qcn_soft_intr(caddr_t arg1, caddr_t arg2);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static qcn_t *qcn_state;
810Sstevel@tonic-gate static uchar_t qcn_stopped = B_FALSE;
820Sstevel@tonic-gate static int qcn_timeout_period = 20;	/* time out in seconds */
830Sstevel@tonic-gate size_t qcn_input_dropped;	/* dropped input character counter */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #ifdef QCN_POLLING
860Sstevel@tonic-gate static void qcn_poll_handler(void *unused);
870Sstevel@tonic-gate static cyc_time_t qcn_poll_time;
880Sstevel@tonic-gate static cyc_handler_t qcn_poll_cychandler = {
890Sstevel@tonic-gate 	qcn_poll_handler,
900Sstevel@tonic-gate 	NULL,
910Sstevel@tonic-gate 	CY_LOW_LEVEL		/* XXX need softint to make this high */
920Sstevel@tonic-gate };
930Sstevel@tonic-gate static cyclic_id_t qcn_poll_cycid = CYCLIC_NONE;
940Sstevel@tonic-gate static uint64_t	qcn_poll_interval = 5;  /* milli sec */
951991Sheppo static uint64_t sb_interval = 0;
961991Sheppo uint_t qcn_force_polling = 0;
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	QCN_MI_IDNUM		0xABCE
1000Sstevel@tonic-gate #define	QCN_MI_HIWAT		8192
1010Sstevel@tonic-gate #define	QCN_MI_LOWAT		128
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* streams structures */
1040Sstevel@tonic-gate static struct module_info minfo = {
1050Sstevel@tonic-gate 	QCN_MI_IDNUM,	/* mi_idnum		*/
1060Sstevel@tonic-gate 	"qcn",		/* mi_idname		*/
1070Sstevel@tonic-gate 	0,		/* mi_minpsz		*/
1080Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz		*/
1090Sstevel@tonic-gate 	QCN_MI_HIWAT,	/* mi_hiwat		*/
1100Sstevel@tonic-gate 	QCN_MI_LOWAT	/* mi_lowat		*/
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static struct qinit rinit = {
1140Sstevel@tonic-gate 	putq,		/* qi_putp		*/
1150Sstevel@tonic-gate 	qcn_rsrv,	/* qi_srvp		*/
1160Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1170Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1180Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1190Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1200Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static struct qinit winit = {
1240Sstevel@tonic-gate 	qcn_wput,	/* qi_putp		*/
1250Sstevel@tonic-gate 	qcn_wsrv,	/* qi_srvp		*/
1260Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1270Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1280Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1290Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1300Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1310Sstevel@tonic-gate };
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static struct streamtab qcnstrinfo = {
1340Sstevel@tonic-gate 	&rinit,
1350Sstevel@tonic-gate 	&winit,
1360Sstevel@tonic-gate 	NULL,
1370Sstevel@tonic-gate 	NULL
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /* standard device driver structures */
1410Sstevel@tonic-gate static struct cb_ops qcn_cb_ops = {
1420Sstevel@tonic-gate 	nulldev,		/* open()		*/
1430Sstevel@tonic-gate 	nulldev,		/* close()		*/
1440Sstevel@tonic-gate 	nodev,			/* strategy()		*/
1450Sstevel@tonic-gate 	nodev,			/* print()		*/
1460Sstevel@tonic-gate 	nodev,			/* dump()		*/
1470Sstevel@tonic-gate 	nodev,			/* read()		*/
1480Sstevel@tonic-gate 	nodev,			/* write()		*/
1490Sstevel@tonic-gate 	nodev,			/* ioctl()		*/
1500Sstevel@tonic-gate 	nodev,			/* devmap()		*/
1510Sstevel@tonic-gate 	nodev,			/* mmap()		*/
1520Sstevel@tonic-gate 	nodev,			/* segmap()		*/
1530Sstevel@tonic-gate 	nochpoll,		/* poll()		*/
1540Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op()		*/
1550Sstevel@tonic-gate 	&qcnstrinfo,		/* cb_str		*/
1560Sstevel@tonic-gate 	D_NEW | D_MP		/* cb_flag		*/
1570Sstevel@tonic-gate };
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static struct dev_ops qcn_ops = {
1600Sstevel@tonic-gate 	DEVO_REV,
1610Sstevel@tonic-gate 	0,			/* refcnt		*/
1620Sstevel@tonic-gate 	qcn_getinfo,		/* getinfo()		*/
1630Sstevel@tonic-gate 	nulldev,		/* identify()		*/
1640Sstevel@tonic-gate 	nulldev,		/* probe()		*/
1650Sstevel@tonic-gate 	qcn_attach,		/* attach()		*/
1660Sstevel@tonic-gate 	qcn_detach,		/* detach()		*/
1670Sstevel@tonic-gate 	nodev,			/* reset()		*/
1680Sstevel@tonic-gate 	&qcn_cb_ops,		/* cb_ops		*/
1690Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* bus_ops		*/
1700Sstevel@tonic-gate 	NULL			/* power()		*/
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static struct modldrv modldrv = {
1740Sstevel@tonic-gate 	&mod_driverops,
1750Sstevel@tonic-gate 	"sun4v console driver v%I%",
1760Sstevel@tonic-gate 	&qcn_ops
1770Sstevel@tonic-gate };
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static struct modlinkage modlinkage = {
1800Sstevel@tonic-gate 	MODREV_1,
1810Sstevel@tonic-gate 	(void*)&modldrv,
1820Sstevel@tonic-gate 	NULL
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /* driver configuration routines */
1860Sstevel@tonic-gate int
1870Sstevel@tonic-gate _init(void)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	int error;
190*2282Sjb145095 	uint64_t	major, minor;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	qcn_state = kmem_zalloc(sizeof (qcn_t), KM_SLEEP);
193*2282Sjb145095 	qcn_state->qcn_ring = contig_mem_alloc(RINGSIZE);
194*2282Sjb145095 	if (qcn_state->qcn_ring == NULL)
195*2282Sjb145095 		cmn_err(CE_PANIC, "console ring allocation failed");
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	error = mod_install(&modlinkage);
198*2282Sjb145095 	if (error != 0) {
199*2282Sjb145095 		contig_mem_free(qcn_state->qcn_ring, RINGSIZE);
2000Sstevel@tonic-gate 		kmem_free(qcn_state, sizeof (qcn_t));
201*2282Sjb145095 		return (error);
202*2282Sjb145095 	}
203*2282Sjb145095 	/*
204*2282Sjb145095 	 * check minor number to see if CONS_WRITE is supported
205*2282Sjb145095 	 * if so, set up real address of the buffers for hv calls.
206*2282Sjb145095 	 */
2070Sstevel@tonic-gate 
208*2282Sjb145095 	if (((hsvc_version(HSVC_GROUP_CORE, &major, &minor) == 0) &&
209*2282Sjb145095 		(major == QCN_API_MAJOR) && (minor >= QCN_API_MINOR))) {
210*2282Sjb145095 		qcn_state->cons_write_buffer =
211*2282Sjb145095 					contig_mem_alloc(CONS_WR_BUF_SIZE);
212*2282Sjb145095 		if (qcn_state->cons_write_buffer != NULL) {
213*2282Sjb145095 			qcn_state->cons_write_buf_ra =
214*2282Sjb145095 					va_to_pa(qcn_state->cons_write_buffer);
215*2282Sjb145095 			qcn_state->cons_transmit = qcn_transmit_write;
216*2282Sjb145095 			qcn_state->cons_receive = qcn_receive_read;
217*2282Sjb145095 			qcn_state->cons_read_buf_ra =
218*2282Sjb145095 					va_to_pa((char *)RING_ADDR(qcn_state));
219*2282Sjb145095 		}
220*2282Sjb145095 	}
221*2282Sjb145095 	if (qcn_state->cons_transmit == NULL) {
222*2282Sjb145095 		qcn_state->cons_transmit = qcn_transmit_putchr;
223*2282Sjb145095 		qcn_state->cons_receive = qcn_receive_getchr;
224*2282Sjb145095 	}
225*2282Sjb145095 	return (0);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate int
2290Sstevel@tonic-gate _fini(void)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	/* can't remove console driver */
2320Sstevel@tonic-gate 	return (EBUSY);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate int
2360Sstevel@tonic-gate _info(struct modinfo *modinfop)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static int
2420Sstevel@tonic-gate qcn_add_intrs(void)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	dev_info_t	*devinfo = qcn_state->qcn_dip;
2450Sstevel@tonic-gate 	int		actual, count = 0;
2460Sstevel@tonic-gate 	int 		x, y, rc, inum = 0;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/* get number of interrupts */
2500Sstevel@tonic-gate 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
2510Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (count == 0)) {
2520Sstevel@tonic-gate 		return (DDI_FAILURE);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* Allocate an array of interrupt handles */
2560Sstevel@tonic-gate 	qcn_state->qcn_intr_size = count * sizeof (ddi_intr_handle_t);
2570Sstevel@tonic-gate 	qcn_state->qcn_htable = kmem_zalloc(qcn_state->qcn_intr_size, KM_SLEEP);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/* call ddi_intr_alloc() */
2600Sstevel@tonic-gate 	rc = ddi_intr_alloc(devinfo, qcn_state->qcn_htable,
2610Sstevel@tonic-gate 	    DDI_INTR_TYPE_FIXED, inum, count, &actual,
2620Sstevel@tonic-gate 	    DDI_INTR_ALLOC_STRICT);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
2650Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2660Sstevel@tonic-gate 		return (DDI_FAILURE);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (actual < count) {
2700Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2710Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 
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 	qcn_state->qcn_intr_cnt = actual;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/* Get intr priority */
2810Sstevel@tonic-gate 	if (ddi_intr_get_pri(qcn_state->qcn_htable[0],
2820Sstevel@tonic-gate 	    &qcn_state->qcn_intr_pri) != DDI_SUCCESS) {
2830Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2840Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2880Sstevel@tonic-gate 		return (DDI_FAILURE);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/* Call ddi_intr_add_handler() */
2920Sstevel@tonic-gate 	for (x = 0; x < actual; x++) {
2930Sstevel@tonic-gate 		if (ddi_intr_add_handler(qcn_state->qcn_htable[x],
2940Sstevel@tonic-gate 		    (ddi_intr_handler_t *)qcn_hi_intr,
2950Sstevel@tonic-gate 		    (caddr_t)qcn_state, NULL) != DDI_SUCCESS) {
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 			for (y = 0; y < x; y++) {
2980Sstevel@tonic-gate 				(void) ddi_intr_remove_handler(
2990Sstevel@tonic-gate 				    qcn_state->qcn_htable[y]);
3000Sstevel@tonic-gate 			}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 			for (y = 0; y < actual; y++) {
3030Sstevel@tonic-gate 				(void) ddi_intr_free(qcn_state->qcn_htable[y]);
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 			kmem_free(qcn_state->qcn_htable,
3070Sstevel@tonic-gate 			    qcn_state->qcn_intr_size);
3080Sstevel@tonic-gate 			return (DDI_FAILURE);
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (DDI_SUCCESS);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static void
3160Sstevel@tonic-gate qcn_remove_intrs(void)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	int x;
3190Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3200Sstevel@tonic-gate 		(void) ddi_intr_disable(qcn_state->qcn_htable[x]);
3210Sstevel@tonic-gate 		(void) ddi_intr_remove_handler(qcn_state->qcn_htable[x]);
3220Sstevel@tonic-gate 		(void) ddi_intr_free(qcn_state->qcn_htable[x]);
3230Sstevel@tonic-gate 	}
324288Sarao 	kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate static void
3280Sstevel@tonic-gate qcn_intr_enable(void)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	int x;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3330Sstevel@tonic-gate 		(void) ddi_intr_enable(qcn_state->qcn_htable[x]);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * qcn_attach is called at startup time.
3390Sstevel@tonic-gate  * There is only once instance of this driver.
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate static int
3420Sstevel@tonic-gate qcn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	extern int ddi_create_internal_pathname(dev_info_t *, char *,
3450Sstevel@tonic-gate 	    int, minor_t);
3460Sstevel@tonic-gate 	uint_t soft_prip;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate #ifdef QCN_POLLING
3490Sstevel@tonic-gate 	char *binding_name;
3500Sstevel@tonic-gate #endif
3510Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3520Sstevel@tonic-gate 		return (DDI_FAILURE);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (ddi_create_internal_pathname(dip, "qcn",
3550Sstevel@tonic-gate 	    S_IFCHR, 0) != DDI_SUCCESS)
3560Sstevel@tonic-gate 		return (DDI_FAILURE);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	qcn_state->qcn_soft_pend = 0;
3590Sstevel@tonic-gate 	qcn_state->qcn_hangup = 0;
3600Sstevel@tonic-gate 	qcn_state->qcn_rbuf_overflow = 0;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/* prepare some data structures in soft state */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	qcn_state->qcn_dip = dip;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	qcn_state->qcn_polling = 0;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate #ifdef QCN_POLLING
3690Sstevel@tonic-gate 	/*
3700Sstevel@tonic-gate 	 * This test is for the sole purposes of allowing
3710Sstevel@tonic-gate 	 * the console to work on older firmware releases.
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	binding_name = ddi_binding_name(qcn_state->qcn_dip);
3741991Sheppo 	if ((strcmp(binding_name, "qcn") == 0) ||
3751991Sheppo 	    (qcn_force_polling))
3760Sstevel@tonic-gate 		qcn_state->qcn_polling = 1;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
3790Sstevel@tonic-gate 		qcn_poll_time.cyt_when = 0ull;
3800Sstevel@tonic-gate 		qcn_poll_time.cyt_interval =
3810Sstevel@tonic-gate 		    qcn_poll_interval * 1000ull * 1000ull;
3820Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3830Sstevel@tonic-gate 		qcn_poll_cycid = cyclic_add(&qcn_poll_cychandler,
3840Sstevel@tonic-gate 		    &qcn_poll_time);
3850Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate #endif
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
3900Sstevel@tonic-gate 		if (qcn_add_intrs() != DDI_SUCCESS) {
3910Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_intr failed\n");
3920Sstevel@tonic-gate 			return (DDI_FAILURE);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		if (ddi_intr_add_softint(dip, &qcn_state->qcn_softint_hdl,
3950Sstevel@tonic-gate 		    DDI_INTR_SOFTPRI_MAX, qcn_soft_intr,
3960Sstevel@tonic-gate 		    (caddr_t)qcn_state) != DDI_SUCCESS) {
3970Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_soft_intr failed\n");
3980Sstevel@tonic-gate 			qcn_remove_intrs();
3990Sstevel@tonic-gate 			return (DDI_FAILURE);
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 		if (ddi_intr_get_softint_pri(qcn_state->qcn_softint_hdl,
4020Sstevel@tonic-gate 		    &soft_prip) != DDI_SUCCESS) {
4030Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: softint_pri failed\n");
404288Sarao 			(void) ddi_intr_remove_softint(
405288Sarao 			    qcn_state->qcn_softint_hdl);
4060Sstevel@tonic-gate 			qcn_remove_intrs();
4070Sstevel@tonic-gate 			return (DDI_FAILURE);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 	qcn_state->qcn_soft_pri =
4100Sstevel@tonic-gate 	    (ddi_iblock_cookie_t)(uint64_t)soft_prip;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_hi_lock, NULL, MUTEX_DRIVER,
413911Siskreen 	    (void *)(uintptr_t)(qcn_state->qcn_intr_pri));
4140Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_softlock, NULL, MUTEX_DRIVER,
4150Sstevel@tonic-gate 	    (void *)(qcn_state->qcn_soft_pri));
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_lock, NULL, MUTEX_DRIVER, NULL);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 *  Enable  interrupts
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
4240Sstevel@tonic-gate 		qcn_intr_enable();
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate #ifdef QCN_DEBUG
4270Sstevel@tonic-gate 	prom_printf("qcn_attach(): qcn driver attached\n");
4280Sstevel@tonic-gate #endif
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	return (DDI_SUCCESS);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /* ARGSUSED */
4350Sstevel@tonic-gate static int
4360Sstevel@tonic-gate qcn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4400Sstevel@tonic-gate 		return (DDI_FAILURE);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate #ifdef QCN_DEBUG
4440Sstevel@tonic-gate 	prom_printf("qcn_detach(): QCN driver detached\n");
4450Sstevel@tonic-gate #endif
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate #ifdef QCN_POLLING
4480Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
4490Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4500Sstevel@tonic-gate 		if (qcn_poll_cycid != CYCLIC_NONE)
4510Sstevel@tonic-gate 			cyclic_remove(qcn_poll_cycid);
4520Sstevel@tonic-gate 		qcn_poll_cycid = CYCLIC_NONE;
4530Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate #endif
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (!qcn_state->qcn_polling)
4580Sstevel@tonic-gate 		qcn_remove_intrs();
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	return (DDI_SUCCESS);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate /* ARGSUSED */
4640Sstevel@tonic-gate static int
4650Sstevel@tonic-gate qcn_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	int error = DDI_FAILURE;
4680Sstevel@tonic-gate 	int instance = 0;
4690Sstevel@tonic-gate 	switch (infocmd) {
4700Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4710Sstevel@tonic-gate 		if (qcn_state) {
4720Sstevel@tonic-gate #ifdef QCN_DEBUG
4730Sstevel@tonic-gate 			prom_printf("qcn_getinfo(): devt2dip %lx\n", arg);
4740Sstevel@tonic-gate #endif
4750Sstevel@tonic-gate 			*result = (void *)qcn_state->qcn_dip;
4760Sstevel@tonic-gate 			error = DDI_SUCCESS;
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 		break;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4810Sstevel@tonic-gate #ifdef QCN_DEBUG
4820Sstevel@tonic-gate 		prom_printf("qcn_getinfo(): devt2instance %lx\n", arg);
4830Sstevel@tonic-gate #endif
4840Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
485911Siskreen 			*result = (void *)(uintptr_t)instance;
4860Sstevel@tonic-gate 			error = DDI_SUCCESS;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		break;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	return (error);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /* streams open & close */
4950Sstevel@tonic-gate /* ARGSUSED */
4960Sstevel@tonic-gate static int
4970Sstevel@tonic-gate qcn_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	tty_common_t *tty;
5000Sstevel@tonic-gate 	int unit = getminor(*devp);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate #ifdef QCN_DEBUG
5030Sstevel@tonic-gate 	prom_printf("qcn_open(): minor %x\n", unit);
5040Sstevel@tonic-gate #endif
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if (unit != 0)
5070Sstevel@tonic-gate 		return (ENXIO);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/* stream already open */
5100Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5110Sstevel@tonic-gate 		return (DDI_SUCCESS);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (!qcn_state) {
5140Sstevel@tonic-gate 		cmn_err(CE_WARN, "qcn_open: console was not configured by "
5150Sstevel@tonic-gate 		    "autoconfig\n");
5160Sstevel@tonic-gate 		return (ENXIO);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5200Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	tty->t_readq = q;
5230Sstevel@tonic-gate 	tty->t_writeq = WR(q);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* Link the RD and WR Q's */
5260Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)qcn_state;
5270Sstevel@tonic-gate 	qcn_state->qcn_readq = RD(q);
5280Sstevel@tonic-gate 	qcn_state->qcn_writeq = WR(q);
5290Sstevel@tonic-gate 	qprocson(q);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate #ifdef QCN_DEBUG
5340Sstevel@tonic-gate 	prom_printf("qcn_open: opened as dev %lx\n", *devp);
5350Sstevel@tonic-gate #endif
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	return (DDI_SUCCESS);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /* ARGSUSED */
5410Sstevel@tonic-gate static int
5420Sstevel@tonic-gate qcn_close(queue_t *q, int flag, cred_t *credp)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (qcn_state->qcn_wbufcid != 0) {
5480Sstevel@tonic-gate 		unbufcall(qcn_state->qcn_wbufcid);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	ttycommon_close(&qcn_state->qcn_tty);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	qprocsoff(q);
5530Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
5540Sstevel@tonic-gate 	qcn_state->qcn_readq = NULL;
5550Sstevel@tonic-gate 	qcn_state->qcn_writeq = NULL;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	return (DDI_SUCCESS);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate /*
5610Sstevel@tonic-gate  * Put procedure for write queue.
5620Sstevel@tonic-gate  * Respond to M_IOCTL, M_DATA and M_FLUSH messages here;
5630Sstevel@tonic-gate  * It put's the data onto internal qcn_output_q.
5640Sstevel@tonic-gate  */
5650Sstevel@tonic-gate static int
5660Sstevel@tonic-gate qcn_wput(queue_t *q, mblk_t *mp)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate #ifdef QCN_DEBUG
5700Sstevel@tonic-gate 	struct iocblk *iocp;
5710Sstevel@tonic-gate 	int i;
5720Sstevel@tonic-gate #endif
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (!mp->b_datap) {
5770Sstevel@tonic-gate 		cmn_err(CE_PANIC, "qcn_wput: null datap");
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate #ifdef QCN_DEBUG
5810Sstevel@tonic-gate 	prom_printf("qcn_wput(): QCN wput q=%X mp=%X rd=%X wr=%X type=%X\n",
5820Sstevel@tonic-gate 		q, mp, mp->b_rptr, mp->b_wptr, mp->b_datap->db_type);
5830Sstevel@tonic-gate #endif
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
5880Sstevel@tonic-gate 	case M_IOCTL:
5890Sstevel@tonic-gate 	case M_CTL:
5900Sstevel@tonic-gate #ifdef QCN_DEBUG
5910Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
5920Sstevel@tonic-gate 		prom_printf("qcn_wput(): M_IOCTL cmd=%X TIOC=%X\n",
5930Sstevel@tonic-gate 		    iocp->ioc_cmd, TIOC);
5940Sstevel@tonic-gate #endif
5950Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
5960Sstevel@tonic-gate 		case TCSETSW:
5970Sstevel@tonic-gate 		case TCSETSF:
5980Sstevel@tonic-gate 		case TCSETAW:
5990Sstevel@tonic-gate 		case TCSETAF:
6000Sstevel@tonic-gate 		case TCSBRK:
6010Sstevel@tonic-gate 			/*
6020Sstevel@tonic-gate 			 * The change do not take effect until all
6030Sstevel@tonic-gate 			 * output queued before them is drained.
6040Sstevel@tonic-gate 			 * Put this message on the queue, so that
6050Sstevel@tonic-gate 			 * "qcn_start" will see it when it's done
6060Sstevel@tonic-gate 			 * with the output before it. Poke the start
6070Sstevel@tonic-gate 			 * routine, just in case.
6080Sstevel@tonic-gate 			 */
6090Sstevel@tonic-gate 			(void) putq(q, mp);
6100Sstevel@tonic-gate 			qcn_start();
6110Sstevel@tonic-gate 			break;
6120Sstevel@tonic-gate 		default:
6130Sstevel@tonic-gate 			qcn_ioctl(q, mp);
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 		break;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	case M_FLUSH:
6180Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
6190Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
6200Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
6210Sstevel@tonic-gate 		}
6220Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
6230Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
6240Sstevel@tonic-gate 			qreply(q, mp);
6250Sstevel@tonic-gate 		} else {
6260Sstevel@tonic-gate 			freemsg(mp);
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 		break;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	case M_STOP:
6310Sstevel@tonic-gate 		qcn_stopped = B_TRUE;
6320Sstevel@tonic-gate 		freemsg(mp);
6330Sstevel@tonic-gate 		break;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	case M_START:
6360Sstevel@tonic-gate 		qcn_stopped = B_FALSE;
6370Sstevel@tonic-gate 		freemsg(mp);
6380Sstevel@tonic-gate 		qenable(q);	/* Start up delayed messages */
6390Sstevel@tonic-gate 		break;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	case M_DATA:
6420Sstevel@tonic-gate 		/*
6430Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
6440Sstevel@tonic-gate 		 * and poke the start routine.
6450Sstevel@tonic-gate 		 */
6460Sstevel@tonic-gate #ifdef QCN_DEBUG
6470Sstevel@tonic-gate 		if (mp->b_rptr < mp->b_wptr) {
6480Sstevel@tonic-gate 		prom_printf("qcn_wput(): DATA q=%X mp=%X rd=%X wr=%X\n",
6490Sstevel@tonic-gate 			q, mp, mp->b_rptr, mp->b_wptr);
6500Sstevel@tonic-gate 		prom_printf("qcn_wput(): [");
6510Sstevel@tonic-gate 		for (i = 0; i < mp->b_wptr-mp->b_rptr; i++) {
6520Sstevel@tonic-gate 			prom_printf("%c", *(mp->b_rptr+i));
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 		prom_printf("]\n");
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate #endif /* QCN_DEBUG */
6570Sstevel@tonic-gate 		(void) putq(q, mp);
6580Sstevel@tonic-gate 		qcn_start();
6590Sstevel@tonic-gate 		break;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	default:
6620Sstevel@tonic-gate 		freemsg(mp);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
6660Sstevel@tonic-gate 	return (0);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6710Sstevel@tonic-gate  */
6720Sstevel@tonic-gate static void
6730Sstevel@tonic-gate qcn_ioctl(queue_t *q, mblk_t *mp)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	struct iocblk	*iocp;
6760Sstevel@tonic-gate 	tty_common_t	*tty;
6770Sstevel@tonic-gate 	mblk_t		*datamp;
6780Sstevel@tonic-gate 	int		data_size;
6790Sstevel@tonic-gate 	int		error = 0;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate #ifdef QCN_DEBUG
6820Sstevel@tonic-gate 	prom_printf("qcn_ioctl(): q=%X mp=%X\n", q, mp);
6830Sstevel@tonic-gate #endif
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
6860Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (tty->t_iocpending != NULL) {
6890Sstevel@tonic-gate 		freemsg(tty->t_iocpending);
6900Sstevel@tonic-gate 		tty->t_iocpending = NULL;
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	data_size = ttycommon_ioctl(tty, q, mp, &error);
6930Sstevel@tonic-gate 	if (data_size != 0) {
6940Sstevel@tonic-gate 		if (qcn_state->qcn_wbufcid)
6950Sstevel@tonic-gate 			unbufcall(qcn_state->qcn_wbufcid);
6960Sstevel@tonic-gate 		/* call qcn_reioctl() */
6970Sstevel@tonic-gate 		qcn_state->qcn_wbufcid =
6980Sstevel@tonic-gate 		    bufcall(data_size, BPRI_HI, qcn_reioctl, qcn_state);
6990Sstevel@tonic-gate 		return;
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (error < 0) {
7030Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
7040Sstevel@tonic-gate 		/*
7050Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
7060Sstevel@tonic-gate 		 */
7070Sstevel@tonic-gate 		error = 0;
7080Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
7090Sstevel@tonic-gate 		case TCSBRK:
7100Sstevel@tonic-gate 		case TIOCSBRK:
7110Sstevel@tonic-gate 		case TIOCCBRK:
7120Sstevel@tonic-gate 		case TIOCMSET:
7130Sstevel@tonic-gate 		case TIOCMBIS:
7140Sstevel@tonic-gate 		case TIOCMBIC:
7150Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7160Sstevel@tonic-gate 				qcn_ack(mp, NULL, 0);
7170Sstevel@tonic-gate 			else
7180Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
7190Sstevel@tonic-gate 			break;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		case TIOCMGET:
7220Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
7230Sstevel@tonic-gate 			if (datamp == NULL) {
7240Sstevel@tonic-gate 				error = EAGAIN;
7250Sstevel@tonic-gate 				break;
7260Sstevel@tonic-gate 			}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 			*(int *)datamp->b_rptr = 0;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7310Sstevel@tonic-gate 				qcn_ack(mp, datamp, sizeof (int));
7320Sstevel@tonic-gate 			else
7330Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
7340Sstevel@tonic-gate 			break;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		default:
7370Sstevel@tonic-gate 			error = EINVAL;
7380Sstevel@tonic-gate 			break;
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	if (error != 0) {
7420Sstevel@tonic-gate 		iocp->ioc_count = 0;
7430Sstevel@tonic-gate 		iocp->ioc_error = error;
7440Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 	qreply(q, mp);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate static void
7500Sstevel@tonic-gate qcn_reioctl(void *unit)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	queue_t		*q;
7530Sstevel@tonic-gate 	mblk_t		*mp;
7540Sstevel@tonic-gate 	qcn_t		*qcnp = (qcn_t *)unit;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (!qcnp->qcn_wbufcid)
7570Sstevel@tonic-gate 		return;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	qcnp->qcn_wbufcid = 0;
7600Sstevel@tonic-gate 	if ((q = qcnp->qcn_tty.t_writeq) == NULL)
7610Sstevel@tonic-gate 		return;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if ((mp = qcnp->qcn_tty.t_iocpending) == NULL)
7640Sstevel@tonic-gate 		return;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	qcnp->qcn_tty.t_iocpending = NULL;
7670Sstevel@tonic-gate 	qcn_ioctl(q, mp);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate static void
7710Sstevel@tonic-gate qcn_ack(mblk_t *mp, mblk_t *dp, uint_t size)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	struct iocblk  *iocp = (struct iocblk *)mp->b_rptr;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCACK;
7760Sstevel@tonic-gate 	iocp->ioc_count = size;
7770Sstevel@tonic-gate 	iocp->ioc_error = 0;
7780Sstevel@tonic-gate 	iocp->ioc_rval = 0;
7790Sstevel@tonic-gate 	if (mp->b_cont != NULL)
7800Sstevel@tonic-gate 		freeb(mp->b_cont);
7810Sstevel@tonic-gate 	if (dp != NULL) {
7820Sstevel@tonic-gate 		mp->b_cont = dp;
7830Sstevel@tonic-gate 		dp->b_wptr += size;
7840Sstevel@tonic-gate 	} else
7850Sstevel@tonic-gate 		mp->b_cont = NULL;
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate static void
7890Sstevel@tonic-gate qcn_start(void)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	queue_t *q;
7930Sstevel@tonic-gate 	mblk_t *mp;
7940Sstevel@tonic-gate 	int rv;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * read stream queue and remove data from the queue and
8000Sstevel@tonic-gate 	 * transmit them if possible
8010Sstevel@tonic-gate 	 */
8020Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
8030Sstevel@tonic-gate 	ASSERT(q != NULL);
8040Sstevel@tonic-gate 	while (mp = getq(q)) {
8050Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
8060Sstevel@tonic-gate 			/*
8070Sstevel@tonic-gate 			 * These are those IOCTLs queued up
8080Sstevel@tonic-gate 			 * do it now
8090Sstevel@tonic-gate 			 */
8100Sstevel@tonic-gate 			qcn_ioctl(q, mp);
8110Sstevel@tonic-gate 			continue;
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * M_DATA
8150Sstevel@tonic-gate 		 */
816*2282Sjb145095 		rv = qcn_state->cons_transmit(q, mp);
8170Sstevel@tonic-gate 		if (rv == EBUSY || rv == EAGAIN)
8180Sstevel@tonic-gate 			return;
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate static int
823*2282Sjb145095 qcn_transmit_write(queue_t *q, mblk_t *mp)
8240Sstevel@tonic-gate {
825*2282Sjb145095 	mblk_t		*bp;
826*2282Sjb145095 	size_t		len;
827*2282Sjb145095 	uint64_t	i;
828*2282Sjb145095 	uint64_t	retval = 0;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate #ifdef QCN_DEBUG
831*2282Sjb145095 	prom_printf("qcn_transmit_write(): q=%X mp=%X\n", q, mp);
8320Sstevel@tonic-gate #endif
833*2282Sjb145095 
834*2282Sjb145095 	while (mp) {
835*2282Sjb145095 		bp = mp;
836*2282Sjb145095 		len = bp->b_wptr - bp->b_rptr;
837*2282Sjb145095 		/*
838*2282Sjb145095 		 * Use the console write call to send a block of characters to
839*2282Sjb145095 		 * the console.
840*2282Sjb145095 		 */
841*2282Sjb145095 		i = (len > CONS_WR_BUF_SIZE) ? CONS_WR_BUF_SIZE : len;
842*2282Sjb145095 		bcopy(bp->b_rptr, qcn_state->cons_write_buffer, i);
843*2282Sjb145095 		retval = hv_cnwrite(qcn_state->cons_write_buf_ra, i, &i);
844*2282Sjb145095 
845*2282Sjb145095 		if (retval == H_EOK) {
846*2282Sjb145095 			len -= i;
847*2282Sjb145095 			bp->b_rptr += i;
848*2282Sjb145095 			/*
849*2282Sjb145095 			 * if we have finished with this buf, free
850*2282Sjb145095 			 * and get the next buf if present.
851*2282Sjb145095 			 */
852*2282Sjb145095 			if (len == 0) {
853*2282Sjb145095 				mp = bp->b_cont;
854*2282Sjb145095 				freeb(bp);
855*2282Sjb145095 			}
856*2282Sjb145095 		} else {
857*2282Sjb145095 			(void) putbq(q, mp);
858*2282Sjb145095 
859*2282Sjb145095 			switch (retval) {
860*2282Sjb145095 
861*2282Sjb145095 			case H_EWOULDBLOCK :
862*2282Sjb145095 				/*
863*2282Sjb145095 				 * hypervisor cannot process the request -
864*2282Sjb145095 				 * channel busy.  Try again later.
865*2282Sjb145095 				 */
866*2282Sjb145095 				return (EAGAIN);
867*2282Sjb145095 
868*2282Sjb145095 			case H_EIO :
869*2282Sjb145095 				return (EIO);
870*2282Sjb145095 			default :
871*2282Sjb145095 				return (ENXIO);
872*2282Sjb145095 			}
873*2282Sjb145095 		}
874*2282Sjb145095 	}
875*2282Sjb145095 	return (0);
876*2282Sjb145095 }
877*2282Sjb145095 
878*2282Sjb145095 static int
879*2282Sjb145095 qcn_transmit_putchr(queue_t *q, mblk_t *mp)
880*2282Sjb145095 {
881*2282Sjb145095 	caddr_t		buf;
882*2282Sjb145095 	mblk_t		*bp;
883*2282Sjb145095 	size_t		len;
884*2282Sjb145095 	uint64_t	i;
885*2282Sjb145095 
886*2282Sjb145095 #ifdef QCN_DEBUG
887*2282Sjb145095 	prom_printf("qcn_transmit_putchr(): q=%X mp=%X\n", q, mp);
888*2282Sjb145095 #endif
889*2282Sjb145095 	while (mp) {
8900Sstevel@tonic-gate 		bp = mp;
8910Sstevel@tonic-gate 		len = bp->b_wptr - bp->b_rptr;
8920Sstevel@tonic-gate 		buf = (caddr_t)bp->b_rptr;
8930Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
8941991Sheppo 			if (hv_cnputchar(buf[i]) == H_EWOULDBLOCK)
895*2282Sjb145095 			break;
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 		if (i != len) {
8980Sstevel@tonic-gate 			bp->b_rptr += i;
8990Sstevel@tonic-gate 			(void) putbq(q, mp);
9000Sstevel@tonic-gate 			return (EAGAIN);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 		mp = bp->b_cont;
9030Sstevel@tonic-gate 		freeb(bp);
904*2282Sjb145095 	}
9050Sstevel@tonic-gate 	return (0);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate /*
9090Sstevel@tonic-gate  * called when SC first establishes console connection
9100Sstevel@tonic-gate  * drop all the data on the output queue
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate static void
9130Sstevel@tonic-gate qcn_flush(void)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	queue_t *q;
9160Sstevel@tonic-gate 	mblk_t *mp;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
9210Sstevel@tonic-gate 
922911Siskreen 	prom_printf("qcn_flush(): WARNING console output is dropped time=%lx\n",
9230Sstevel@tonic-gate 	    gethrestime_sec());
9240Sstevel@tonic-gate 	while (mp = getq(q))
9250Sstevel@tonic-gate 		freemsg(mp);
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate static void
9290Sstevel@tonic-gate qcn_trigger_softint(void)
9300Sstevel@tonic-gate {
931*2282Sjb145095 	/*
932*2282Sjb145095 	 * get lock for software ints and see if we need to trigger a soft
933*2282Sjb145095 	 * interrupt (no pending sofware ints).
934*2282Sjb145095 	 */
935*2282Sjb145095 	mutex_enter(&qcn_state->qcn_softlock);
936*2282Sjb145095 	/*
937*2282Sjb145095 	 * if we are not currently servicing a software interrupt
938*2282Sjb145095 	 * (qcn_soft_pend == 0), trigger the service routine to run.
939*2282Sjb145095 	 */
940*2282Sjb145095 	if (!qcn_state->qcn_soft_pend++) {
941*2282Sjb145095 		mutex_exit(&qcn_state->qcn_softlock);
942*2282Sjb145095 		(void) ddi_intr_trigger_softint(
9430Sstevel@tonic-gate 			    qcn_state->qcn_softint_hdl, NULL);
944*2282Sjb145095 	} else {
945*2282Sjb145095 		mutex_exit(&qcn_state->qcn_softlock);
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate /*ARGSUSED*/
9500Sstevel@tonic-gate static uint_t
9510Sstevel@tonic-gate qcn_soft_intr(caddr_t arg1, caddr_t arg2)
9520Sstevel@tonic-gate {
9530Sstevel@tonic-gate 	mblk_t *mp;
9540Sstevel@tonic-gate 	int	cc;
955*2282Sjb145095 	int	overflow_check;
9560Sstevel@tonic-gate 
957*2282Sjb145095 	/*
958*2282Sjb145095 	 * grab the lock for the soft int pending.  It is grabbed here so
959*2282Sjb145095 	 * that the decrement at the end of the do loop is under lock
960*2282Sjb145095 	 * protection.
961*2282Sjb145095 	 */
962*2282Sjb145095 	mutex_enter(&qcn_state->qcn_softlock);
9630Sstevel@tonic-gate 	do {
964*2282Sjb145095 		/*
965*2282Sjb145095 		 * release the soft int lock so that the interrupt routine
966*2282Sjb145095 		 * will not be held up.
967*2282Sjb145095 		 */
968*2282Sjb145095 		mutex_exit(&qcn_state->qcn_softlock);
9690Sstevel@tonic-gate 		mutex_enter(&qcn_state->qcn_hi_lock);
970*2282Sjb145095 		if ((cc = RING_CNT(qcn_state)) <= 0) {
971*2282Sjb145095 			mutex_exit(&qcn_state->qcn_hi_lock);
972*2282Sjb145095 			goto out;
973*2282Sjb145095 		}
9740Sstevel@tonic-gate 
975*2282Sjb145095 		if ((mp = allocb(cc, BPRI_MED)) == NULL) {
976*2282Sjb145095 			qcn_input_dropped += cc;
977*2282Sjb145095 			mutex_exit(&qcn_state->qcn_hi_lock);
978*2282Sjb145095 			cmn_err(CE_WARN, "qcn_intr: allocb"
979*2282Sjb145095 			    "failed (console input dropped)");
980*2282Sjb145095 			goto out;
981*2282Sjb145095 		}
982*2282Sjb145095 
983*2282Sjb145095 		do {
984*2282Sjb145095 			/* put console input onto stream */
985*2282Sjb145095 			*(char *)mp->b_wptr++ = RING_GET(qcn_state);
986*2282Sjb145095 		} while (--cc);
987*2282Sjb145095 
988*2282Sjb145095 		if ((overflow_check = qcn_state->qcn_rbuf_overflow) != 0) {
989*2282Sjb145095 			qcn_state->qcn_rbuf_overflow = 0;
990*2282Sjb145095 		}
991*2282Sjb145095 		mutex_exit(&qcn_state->qcn_hi_lock);
9920Sstevel@tonic-gate 
993*2282Sjb145095 		if (overflow_check) {
994*2282Sjb145095 			cmn_err(CE_WARN, "qcn: Ring buffer overflow\n");
995*2282Sjb145095 		}
996*2282Sjb145095 
997*2282Sjb145095 		if (qcn_state->qcn_readq) {
998*2282Sjb145095 			putnext(qcn_state->qcn_readq, mp);
9990Sstevel@tonic-gate 		}
1000*2282Sjb145095 out:
1001*2282Sjb145095 		/*
1002*2282Sjb145095 		 * If there are pending transmits because hypervisor
1003*2282Sjb145095 		 * returned EWOULDBLOCK poke start now.
1004*2282Sjb145095 		 */
1005*2282Sjb145095 
1006*2282Sjb145095 		if (qcn_state->qcn_writeq != NULL) {
1007*2282Sjb145095 			if (qcn_state->qcn_hangup) {
1008*2282Sjb145095 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
1009*2282Sjb145095 				flushq(qcn_state->qcn_writeq, FLUSHDATA);
1010*2282Sjb145095 				qcn_state->qcn_hangup = 0;
1011*2282Sjb145095 			} else {
1012*2282Sjb145095 				mutex_enter(&qcn_state->qcn_lock);
1013*2282Sjb145095 				qcn_start();
1014*2282Sjb145095 				mutex_exit(&qcn_state->qcn_lock);
1015*2282Sjb145095 			}
1016*2282Sjb145095 		}
1017*2282Sjb145095 		mutex_enter(&qcn_state->qcn_softlock);
1018*2282Sjb145095 	} while (qcn_state->qcn_soft_pend-- > 1);
10190Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_softlock);
10200Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate /*ARGSUSED*/
10240Sstevel@tonic-gate static uint_t
10250Sstevel@tonic-gate qcn_hi_intr(caddr_t arg)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_hi_lock);
10280Sstevel@tonic-gate 
1029*2282Sjb145095 	qcn_state->cons_receive();
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_hi_lock);
10320Sstevel@tonic-gate 	qcn_trigger_softint();
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
1037*2282Sjb145095 static void
1038*2282Sjb145095 qcn_receive_read(void)
1039*2282Sjb145095 {
1040*2282Sjb145095 	int64_t rv;
1041*2282Sjb145095 	uint8_t *bufp;
1042*2282Sjb145095 	int64_t	retcount = 0;
1043*2282Sjb145095 	int	i;
1044*2282Sjb145095 
1045*2282Sjb145095 	do {
1046*2282Sjb145095 		/*
1047*2282Sjb145095 		 * Maximize available buffer size
1048*2282Sjb145095 		 */
1049*2282Sjb145095 		if (RING_CNT(qcn_state) <= 0) {
1050*2282Sjb145095 			RING_INIT(qcn_state);
1051*2282Sjb145095 		}
1052*2282Sjb145095 		rv = hv_cnread(qcn_state->cons_read_buf_ra +
1053*2282Sjb145095 				RING_POFF(qcn_state),
1054*2282Sjb145095 				RING_LEFT(qcn_state),
1055*2282Sjb145095 				&retcount);
1056*2282Sjb145095 		bufp = RING_ADDR(qcn_state);
1057*2282Sjb145095 		if (rv == H_EOK) {
1058*2282Sjb145095 			/*
1059*2282Sjb145095 			 * if the alternate break sequence is enabled, test
1060*2282Sjb145095 			 * the buffer for the sequence and if it is there,
1061*2282Sjb145095 			 * enter the debugger.
1062*2282Sjb145095 			 */
1063*2282Sjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
1064*2282Sjb145095 				for (i = 0; i < retcount; i++) {
1065*2282Sjb145095 					if (abort_charseq_recognize(*bufp++)) {
1066*2282Sjb145095 						abort_sequence_enter(
1067*2282Sjb145095 								(char *)NULL);
1068*2282Sjb145095 					}
1069*2282Sjb145095 				}
1070*2282Sjb145095 			}
1071*2282Sjb145095 
1072*2282Sjb145095 			/* put console input onto stream */
1073*2282Sjb145095 			if (retcount > 0) {
1074*2282Sjb145095 				/*
1075*2282Sjb145095 				 * the characters are already in the ring,
1076*2282Sjb145095 				 * just update the pointer so the characters
1077*2282Sjb145095 				 * can be retrieved.
1078*2282Sjb145095 				 */
1079*2282Sjb145095 				RING_UPD(qcn_state, retcount);
1080*2282Sjb145095 			}
1081*2282Sjb145095 		} else {
1082*2282Sjb145095 			switch (rv) {
1083*2282Sjb145095 
1084*2282Sjb145095 			case H_EWOULDBLOCK :
1085*2282Sjb145095 				/*
1086*2282Sjb145095 				 * hypervisor cannot handle the request.
1087*2282Sjb145095 				 * Try again later.
1088*2282Sjb145095 				 */
1089*2282Sjb145095 				break;
1090*2282Sjb145095 
1091*2282Sjb145095 
1092*2282Sjb145095 			case H_BREAK :
1093*2282Sjb145095 				/*
1094*2282Sjb145095 				 * on break, unless alternate break sequence is
1095*2282Sjb145095 				 * enabled, enter the debugger
1096*2282Sjb145095 				 */
1097*2282Sjb145095 				if (abort_enable != KIOCABORTALTERNATE)
1098*2282Sjb145095 					abort_sequence_enter((char *)NULL);
1099*2282Sjb145095 				break;
1100*2282Sjb145095 
1101*2282Sjb145095 			case H_HUP :
1102*2282Sjb145095 				qcn_state->qcn_hangup = 1;
1103*2282Sjb145095 				break;
1104*2282Sjb145095 
1105*2282Sjb145095 			default :
1106*2282Sjb145095 				break;
1107*2282Sjb145095 			}
1108*2282Sjb145095 		}
1109*2282Sjb145095 	} while (rv == H_EOK);
1110*2282Sjb145095 }
1111*2282Sjb145095 
1112*2282Sjb145095 static void
1113*2282Sjb145095 qcn_receive_getchr(void)
1114*2282Sjb145095 {
1115*2282Sjb145095 	int64_t rv;
1116*2282Sjb145095 	uint8_t	buf;
1117*2282Sjb145095 
1118*2282Sjb145095 	do {
1119*2282Sjb145095 		rv = hv_cngetchar(&buf);
1120*2282Sjb145095 		if (rv == H_EOK) {
1121*2282Sjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
1122*2282Sjb145095 				if (abort_charseq_recognize(buf)) {
1123*2282Sjb145095 					abort_sequence_enter((char *)NULL);
1124*2282Sjb145095 				}
1125*2282Sjb145095 			}
1126*2282Sjb145095 
1127*2282Sjb145095 			/* put console input onto stream */
1128*2282Sjb145095 			if (RING_POK(qcn_state, 1)) {
1129*2282Sjb145095 				RING_PUT(qcn_state, buf);
1130*2282Sjb145095 			} else {
1131*2282Sjb145095 				qcn_state->qcn_rbuf_overflow++;
1132*2282Sjb145095 			}
1133*2282Sjb145095 		} else {
1134*2282Sjb145095 			if (rv == H_BREAK) {
1135*2282Sjb145095 				if (abort_enable != KIOCABORTALTERNATE)
1136*2282Sjb145095 					abort_sequence_enter((char *)NULL);
1137*2282Sjb145095 			}
1138*2282Sjb145095 
1139*2282Sjb145095 			if (rv == H_HUP)  {
1140*2282Sjb145095 				qcn_state->qcn_hangup = 1;
1141*2282Sjb145095 			}
1142*2282Sjb145095 			return;
1143*2282Sjb145095 		}
1144*2282Sjb145095 	} while (rv == H_EOK);
1145*2282Sjb145095 }
1146*2282Sjb145095 
11470Sstevel@tonic-gate #ifdef QCN_POLLING
11480Sstevel@tonic-gate /*ARGSUSED*/
11490Sstevel@tonic-gate static void
11500Sstevel@tonic-gate qcn_poll_handler(void *unused)
11510Sstevel@tonic-gate {
11520Sstevel@tonic-gate 	mblk_t *mp;
11530Sstevel@tonic-gate 	int64_t rv;
11540Sstevel@tonic-gate 	uint8_t buf;
11550Sstevel@tonic-gate 	int qcn_writeq_flush = 0;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/* LINTED: E_CONSTANT_CONDITION */
11580Sstevel@tonic-gate 	while (1) {
11590Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
11600Sstevel@tonic-gate 		if (rv == H_BREAK) {
11610Sstevel@tonic-gate 			if (abort_enable != KIOCABORTALTERNATE)
11620Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
11630Sstevel@tonic-gate 		}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 		if (rv == H_HUP)  {
11660Sstevel@tonic-gate 			if (qcn_state->qcn_readq) {
11670Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
11680Sstevel@tonic-gate 				qcn_writeq_flush = 1;
11690Sstevel@tonic-gate 			}
11700Sstevel@tonic-gate 			goto out;
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 		if (rv != H_EOK)
11740Sstevel@tonic-gate 			goto out;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		if (abort_enable == KIOCABORTALTERNATE) {
11770Sstevel@tonic-gate 			if (abort_charseq_recognize(buf)) {
11780Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
11790Sstevel@tonic-gate 			}
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		/* put console input onto stream */
11830Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
11840Sstevel@tonic-gate 			if ((mp = allocb(1, BPRI_MED)) == NULL) {
11850Sstevel@tonic-gate 				qcn_input_dropped++;
11860Sstevel@tonic-gate 				cmn_err(CE_WARN, "qcn_intr: allocb"
11870Sstevel@tonic-gate 				    "failed (console input dropped)");
11880Sstevel@tonic-gate 				return;
11890Sstevel@tonic-gate 			}
11900Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = buf;
11910Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
11920Sstevel@tonic-gate 		}
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate out:
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate  * If there are pending transmits because hypervisor
11970Sstevel@tonic-gate  * returned EWOULDBLOCK poke start now.
11980Sstevel@tonic-gate  */
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12010Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL) {
12020Sstevel@tonic-gate 		if (qcn_writeq_flush) {
12030Sstevel@tonic-gate 			flushq(qcn_state->qcn_writeq, FLUSHDATA);
12040Sstevel@tonic-gate 		} else {
12050Sstevel@tonic-gate 			qcn_start();
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate #endif
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate /*
12130Sstevel@tonic-gate  * Check for abort character sequence, copied from zs_async.c
12140Sstevel@tonic-gate  */
12150Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate static boolean_t
12180Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
12190Sstevel@tonic-gate {
12200Sstevel@tonic-gate 	static int state = 0;
12210Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	if (ch == sequence[state]) {
12240Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
12250Sstevel@tonic-gate 			state = 0;
12260Sstevel@tonic-gate 			return (B_TRUE);
12270Sstevel@tonic-gate 		}
12280Sstevel@tonic-gate 	} else {
12290Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	return (B_FALSE);
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate static int
12360Sstevel@tonic-gate qcn_rsrv(queue_t *q)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	mblk_t	*mp;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12410Sstevel@tonic-gate 		return (0);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
12460Sstevel@tonic-gate 		if (canputnext(q))
12470Sstevel@tonic-gate 			putnext(q, mp);
12480Sstevel@tonic-gate 		else if (mp->b_datap->db_type >= QPCTL)
12490Sstevel@tonic-gate 			(void) putbq(q, mp);
12500Sstevel@tonic-gate 	}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	return (0);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate /* ARGSUSED */
12580Sstevel@tonic-gate static int
12590Sstevel@tonic-gate qcn_wsrv(queue_t *q)
12600Sstevel@tonic-gate {
12610Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12620Sstevel@tonic-gate 		return (0);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL)
12670Sstevel@tonic-gate 		qcn_start();
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	return (0);
12720Sstevel@tonic-gate }
1273