xref: /illumos-gate/usr/src/uts/sun4v/io/qcn.c (revision 54c6b703)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
211ae08745Sheppo 
227c478bd9Sstevel@tonic-gate /*
231ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * sun4v console driver
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/conf.h>
377c478bd9Sstevel@tonic-gate #include <sys/termios.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
407c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
417c478bd9Sstevel@tonic-gate #include <sys/stream.h>
427c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/promif.h>
457c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
487c478bd9Sstevel@tonic-gate #include <sys/intr.h>
497c478bd9Sstevel@tonic-gate #include <sys/spl.h>
507c478bd9Sstevel@tonic-gate #include <sys/qcn.h>
517c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h>
52136097ceSjb #include <sys/hsvc.h>
53136097ceSjb #include <sys/machsystm.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* dev_ops and cb_ops for device driver */
567c478bd9Sstevel@tonic-gate static int qcn_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
577c478bd9Sstevel@tonic-gate static int qcn_attach(dev_info_t *, ddi_attach_cmd_t);
587c478bd9Sstevel@tonic-gate static int qcn_detach(dev_info_t *, ddi_detach_cmd_t);
597c478bd9Sstevel@tonic-gate static int qcn_open(queue_t *, dev_t *, int, int, cred_t *);
607c478bd9Sstevel@tonic-gate static int qcn_close(queue_t *, int, cred_t *);
617c478bd9Sstevel@tonic-gate static int qcn_wput(queue_t *, mblk_t *);
627c478bd9Sstevel@tonic-gate static int qcn_wsrv(queue_t *);
637c478bd9Sstevel@tonic-gate static int qcn_rsrv(queue_t *);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* other internal qcn routines */
667c478bd9Sstevel@tonic-gate static void qcn_ioctl(queue_t *, mblk_t *);
677c478bd9Sstevel@tonic-gate static void qcn_reioctl(void *);
687c478bd9Sstevel@tonic-gate static void qcn_ack(mblk_t *, mblk_t *, uint_t);
697c478bd9Sstevel@tonic-gate static void qcn_start(void);
70136097ceSjb static int qcn_transmit_write(queue_t *, mblk_t *);
71136097ceSjb static int qcn_transmit_putchr(queue_t *, mblk_t *);
72136097ceSjb static void qcn_receive_read(void);
73136097ceSjb static void qcn_receive_getchr(void);
747c478bd9Sstevel@tonic-gate static void qcn_flush(void);
757c478bd9Sstevel@tonic-gate static uint_t qcn_hi_intr(caddr_t arg);
767c478bd9Sstevel@tonic-gate static uint_t qcn_soft_intr(caddr_t arg1, caddr_t arg2);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static qcn_t *qcn_state;
817c478bd9Sstevel@tonic-gate static uchar_t qcn_stopped = B_FALSE;
827c478bd9Sstevel@tonic-gate static int qcn_timeout_period = 20;	/* time out in seconds */
837c478bd9Sstevel@tonic-gate size_t qcn_input_dropped;	/* dropped input character counter */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
867c478bd9Sstevel@tonic-gate static void qcn_poll_handler(void *unused);
87*54c6b703Sjb static cyc_time_t qcn_poll_time;
887c478bd9Sstevel@tonic-gate static cyc_handler_t qcn_poll_cychandler = {
897c478bd9Sstevel@tonic-gate 	qcn_poll_handler,
907c478bd9Sstevel@tonic-gate 	NULL,
917c478bd9Sstevel@tonic-gate 	CY_LOW_LEVEL		/* XXX need softint to make this high */
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate static cyclic_id_t qcn_poll_cycid = CYCLIC_NONE;
94*54c6b703Sjb static uint64_t	qcn_poll_interval = 5;  /* milli sec */
951ae08745Sheppo static uint64_t sb_interval = 0;
961ae08745Sheppo uint_t qcn_force_polling = 0;
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	QCN_MI_IDNUM		0xABCE
1007c478bd9Sstevel@tonic-gate #define	QCN_MI_HIWAT		8192
1017c478bd9Sstevel@tonic-gate #define	QCN_MI_LOWAT		128
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* streams structures */
1047c478bd9Sstevel@tonic-gate static struct module_info minfo = {
1057c478bd9Sstevel@tonic-gate 	QCN_MI_IDNUM,	/* mi_idnum		*/
1067c478bd9Sstevel@tonic-gate 	"qcn",		/* mi_idname		*/
1077c478bd9Sstevel@tonic-gate 	0,		/* mi_minpsz		*/
1087c478bd9Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz		*/
1097c478bd9Sstevel@tonic-gate 	QCN_MI_HIWAT,	/* mi_hiwat		*/
1107c478bd9Sstevel@tonic-gate 	QCN_MI_LOWAT	/* mi_lowat		*/
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static struct qinit rinit = {
1147c478bd9Sstevel@tonic-gate 	putq,		/* qi_putp		*/
1157c478bd9Sstevel@tonic-gate 	qcn_rsrv,	/* qi_srvp		*/
1167c478bd9Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1177c478bd9Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1187c478bd9Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1197c478bd9Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1207c478bd9Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static struct qinit winit = {
1247c478bd9Sstevel@tonic-gate 	qcn_wput,	/* qi_putp		*/
1257c478bd9Sstevel@tonic-gate 	qcn_wsrv,	/* qi_srvp		*/
1267c478bd9Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1277c478bd9Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1287c478bd9Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1297c478bd9Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1307c478bd9Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1317c478bd9Sstevel@tonic-gate };
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static struct streamtab qcnstrinfo = {
1347c478bd9Sstevel@tonic-gate 	&rinit,
1357c478bd9Sstevel@tonic-gate 	&winit,
1367c478bd9Sstevel@tonic-gate 	NULL,
1377c478bd9Sstevel@tonic-gate 	NULL
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* standard device driver structures */
1417c478bd9Sstevel@tonic-gate static struct cb_ops qcn_cb_ops = {
1427c478bd9Sstevel@tonic-gate 	nulldev,		/* open()		*/
1437c478bd9Sstevel@tonic-gate 	nulldev,		/* close()		*/
1447c478bd9Sstevel@tonic-gate 	nodev,			/* strategy()		*/
1457c478bd9Sstevel@tonic-gate 	nodev,			/* print()		*/
1467c478bd9Sstevel@tonic-gate 	nodev,			/* dump()		*/
1477c478bd9Sstevel@tonic-gate 	nodev,			/* read()		*/
1487c478bd9Sstevel@tonic-gate 	nodev,			/* write()		*/
1497c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl()		*/
1507c478bd9Sstevel@tonic-gate 	nodev,			/* devmap()		*/
1517c478bd9Sstevel@tonic-gate 	nodev,			/* mmap()		*/
1527c478bd9Sstevel@tonic-gate 	nodev,			/* segmap()		*/
1537c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll()		*/
1547c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op()		*/
1557c478bd9Sstevel@tonic-gate 	&qcnstrinfo,		/* cb_str		*/
1567c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* cb_flag		*/
1577c478bd9Sstevel@tonic-gate };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static struct dev_ops qcn_ops = {
1607c478bd9Sstevel@tonic-gate 	DEVO_REV,
1617c478bd9Sstevel@tonic-gate 	0,			/* refcnt		*/
1627c478bd9Sstevel@tonic-gate 	qcn_getinfo,		/* getinfo()		*/
1637c478bd9Sstevel@tonic-gate 	nulldev,		/* identify()		*/
1647c478bd9Sstevel@tonic-gate 	nulldev,		/* probe()		*/
1657c478bd9Sstevel@tonic-gate 	qcn_attach,		/* attach()		*/
1667c478bd9Sstevel@tonic-gate 	qcn_detach,		/* detach()		*/
1677c478bd9Sstevel@tonic-gate 	nodev,			/* reset()		*/
1687c478bd9Sstevel@tonic-gate 	&qcn_cb_ops,		/* cb_ops		*/
1697c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* bus_ops		*/
1707c478bd9Sstevel@tonic-gate 	NULL			/* power()		*/
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1747c478bd9Sstevel@tonic-gate 	&mod_driverops,
1757c478bd9Sstevel@tonic-gate 	"sun4v console driver v%I%",
1767c478bd9Sstevel@tonic-gate 	&qcn_ops
1777c478bd9Sstevel@tonic-gate };
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1807c478bd9Sstevel@tonic-gate 	MODREV_1,
1817c478bd9Sstevel@tonic-gate 	(void*)&modldrv,
1827c478bd9Sstevel@tonic-gate 	NULL
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* driver configuration routines */
1867c478bd9Sstevel@tonic-gate int
1877c478bd9Sstevel@tonic-gate _init(void)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	int error;
190136097ceSjb 	uint64_t	major, minor;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	qcn_state = kmem_zalloc(sizeof (qcn_t), KM_SLEEP);
193136097ceSjb 	qcn_state->qcn_ring = contig_mem_alloc(RINGSIZE);
194136097ceSjb 	if (qcn_state->qcn_ring == NULL)
195136097ceSjb 		cmn_err(CE_PANIC, "console ring allocation failed");
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
198136097ceSjb 	if (error != 0) {
199136097ceSjb 		contig_mem_free(qcn_state->qcn_ring, RINGSIZE);
2007c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state, sizeof (qcn_t));
201136097ceSjb 		return (error);
202136097ceSjb 	}
203136097ceSjb 	/*
204136097ceSjb 	 * check minor number to see if CONS_WRITE is supported
205136097ceSjb 	 * if so, set up real address of the buffers for hv calls.
206136097ceSjb 	 */
2077c478bd9Sstevel@tonic-gate 
208136097ceSjb 	if (((hsvc_version(HSVC_GROUP_CORE, &major, &minor) == 0) &&
209136097ceSjb 		(major == QCN_API_MAJOR) && (minor >= QCN_API_MINOR))) {
210136097ceSjb 		qcn_state->cons_write_buffer =
211136097ceSjb 					contig_mem_alloc(CONS_WR_BUF_SIZE);
212136097ceSjb 		if (qcn_state->cons_write_buffer != NULL) {
213136097ceSjb 			qcn_state->cons_write_buf_ra =
214136097ceSjb 					va_to_pa(qcn_state->cons_write_buffer);
215136097ceSjb 			qcn_state->cons_transmit = qcn_transmit_write;
216136097ceSjb 			qcn_state->cons_receive = qcn_receive_read;
217136097ceSjb 			qcn_state->cons_read_buf_ra =
218136097ceSjb 					va_to_pa((char *)RING_ADDR(qcn_state));
219136097ceSjb 		}
220136097ceSjb 	}
221136097ceSjb 	if (qcn_state->cons_transmit == NULL) {
222136097ceSjb 		qcn_state->cons_transmit = qcn_transmit_putchr;
223136097ceSjb 		qcn_state->cons_receive = qcn_receive_getchr;
224136097ceSjb 	}
225136097ceSjb 	return (0);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate int
2297c478bd9Sstevel@tonic-gate _fini(void)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	/* can't remove console driver */
2327c478bd9Sstevel@tonic-gate 	return (EBUSY);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate int
2367c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static int
2427c478bd9Sstevel@tonic-gate qcn_add_intrs(void)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	dev_info_t	*devinfo = qcn_state->qcn_dip;
2457c478bd9Sstevel@tonic-gate 	int		actual, count = 0;
2467c478bd9Sstevel@tonic-gate 	int 		x, y, rc, inum = 0;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/* get number of interrupts */
2507c478bd9Sstevel@tonic-gate 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
2517c478bd9Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (count == 0)) {
2527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/* Allocate an array of interrupt handles */
2567c478bd9Sstevel@tonic-gate 	qcn_state->qcn_intr_size = count * sizeof (ddi_intr_handle_t);
2577c478bd9Sstevel@tonic-gate 	qcn_state->qcn_htable = kmem_zalloc(qcn_state->qcn_intr_size, KM_SLEEP);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* call ddi_intr_alloc() */
2607c478bd9Sstevel@tonic-gate 	rc = ddi_intr_alloc(devinfo, qcn_state->qcn_htable,
2617c478bd9Sstevel@tonic-gate 	    DDI_INTR_TYPE_FIXED, inum, count, &actual,
2627c478bd9Sstevel@tonic-gate 	    DDI_INTR_ALLOC_STRICT);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
2657c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2667c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (actual < count) {
2707c478bd9Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2717c478bd9Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	qcn_state->qcn_intr_cnt = actual;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* Get intr priority */
2817c478bd9Sstevel@tonic-gate 	if (ddi_intr_get_pri(qcn_state->qcn_htable[0],
2827c478bd9Sstevel@tonic-gate 	    &qcn_state->qcn_intr_pri) != DDI_SUCCESS) {
2837c478bd9Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2847c478bd9Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* Call ddi_intr_add_handler() */
2927c478bd9Sstevel@tonic-gate 	for (x = 0; x < actual; x++) {
2937c478bd9Sstevel@tonic-gate 		if (ddi_intr_add_handler(qcn_state->qcn_htable[x],
2947c478bd9Sstevel@tonic-gate 		    (ddi_intr_handler_t *)qcn_hi_intr,
2957c478bd9Sstevel@tonic-gate 		    (caddr_t)qcn_state, NULL) != DDI_SUCCESS) {
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			for (y = 0; y < x; y++) {
2987c478bd9Sstevel@tonic-gate 				(void) ddi_intr_remove_handler(
2997c478bd9Sstevel@tonic-gate 				    qcn_state->qcn_htable[y]);
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			for (y = 0; y < actual; y++) {
3037c478bd9Sstevel@tonic-gate 				(void) ddi_intr_free(qcn_state->qcn_htable[y]);
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 			kmem_free(qcn_state->qcn_htable,
3077c478bd9Sstevel@tonic-gate 			    qcn_state->qcn_intr_size);
3087c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate static void
3167c478bd9Sstevel@tonic-gate qcn_remove_intrs(void)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	int x;
3197c478bd9Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3207c478bd9Sstevel@tonic-gate 		(void) ddi_intr_disable(qcn_state->qcn_htable[x]);
3217c478bd9Sstevel@tonic-gate 		(void) ddi_intr_remove_handler(qcn_state->qcn_htable[x]);
3227c478bd9Sstevel@tonic-gate 		(void) ddi_intr_free(qcn_state->qcn_htable[x]);
3237c478bd9Sstevel@tonic-gate 	}
324ea841a36Sarao 	kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static void
3287c478bd9Sstevel@tonic-gate qcn_intr_enable(void)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	int x;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3337c478bd9Sstevel@tonic-gate 		(void) ddi_intr_enable(qcn_state->qcn_htable[x]);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * qcn_attach is called at startup time.
3397c478bd9Sstevel@tonic-gate  * There is only once instance of this driver.
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate static int
3427c478bd9Sstevel@tonic-gate qcn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	extern int ddi_create_internal_pathname(dev_info_t *, char *,
3457c478bd9Sstevel@tonic-gate 	    int, minor_t);
3467c478bd9Sstevel@tonic-gate 	uint_t soft_prip;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
3497c478bd9Sstevel@tonic-gate 	char *binding_name;
3507c478bd9Sstevel@tonic-gate #endif
3517c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (ddi_create_internal_pathname(dip, "qcn",
3557c478bd9Sstevel@tonic-gate 	    S_IFCHR, 0) != DDI_SUCCESS)
3567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	qcn_state->qcn_soft_pend = 0;
3597c478bd9Sstevel@tonic-gate 	qcn_state->qcn_hangup = 0;
3607c478bd9Sstevel@tonic-gate 	qcn_state->qcn_rbuf_overflow = 0;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* prepare some data structures in soft state */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	qcn_state->qcn_dip = dip;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	qcn_state->qcn_polling = 0;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * This test is for the sole purposes of allowing
3717c478bd9Sstevel@tonic-gate 	 * the console to work on older firmware releases.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	binding_name = ddi_binding_name(qcn_state->qcn_dip);
3741ae08745Sheppo 	if ((strcmp(binding_name, "qcn") == 0) ||
3751ae08745Sheppo 	    (qcn_force_polling))
3767c478bd9Sstevel@tonic-gate 		qcn_state->qcn_polling = 1;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
3797c478bd9Sstevel@tonic-gate 		qcn_poll_time.cyt_when = 0ull;
3807c478bd9Sstevel@tonic-gate 		qcn_poll_time.cyt_interval =
3817c478bd9Sstevel@tonic-gate 		    qcn_poll_interval * 1000ull * 1000ull;
3827c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3837c478bd9Sstevel@tonic-gate 		qcn_poll_cycid = cyclic_add(&qcn_poll_cychandler,
3847c478bd9Sstevel@tonic-gate 		    &qcn_poll_time);
3857c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate #endif
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
3907c478bd9Sstevel@tonic-gate 		if (qcn_add_intrs() != DDI_SUCCESS) {
3917c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_intr failed\n");
3927c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		if (ddi_intr_add_softint(dip, &qcn_state->qcn_softint_hdl,
3957c478bd9Sstevel@tonic-gate 		    DDI_INTR_SOFTPRI_MAX, qcn_soft_intr,
3967c478bd9Sstevel@tonic-gate 		    (caddr_t)qcn_state) != DDI_SUCCESS) {
3977c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_soft_intr failed\n");
3987c478bd9Sstevel@tonic-gate 			qcn_remove_intrs();
3997c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 		if (ddi_intr_get_softint_pri(qcn_state->qcn_softint_hdl,
4027c478bd9Sstevel@tonic-gate 		    &soft_prip) != DDI_SUCCESS) {
4037c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: softint_pri failed\n");
404ea841a36Sarao 			(void) ddi_intr_remove_softint(
405ea841a36Sarao 			    qcn_state->qcn_softint_hdl);
4067c478bd9Sstevel@tonic-gate 			qcn_remove_intrs();
4077c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 
410*54c6b703Sjb 	mutex_init(&qcn_state->qcn_hi_lock, NULL, MUTEX_DRIVER,
411*54c6b703Sjb 	    (void *)(uintptr_t)(qcn_state->qcn_intr_pri));
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_lock, NULL, MUTEX_DRIVER, NULL);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 *  Enable  interrupts
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
4207c478bd9Sstevel@tonic-gate 		qcn_intr_enable();
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4237c478bd9Sstevel@tonic-gate 	prom_printf("qcn_attach(): qcn driver attached\n");
4247c478bd9Sstevel@tonic-gate #endif
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /* ARGSUSED */
4317c478bd9Sstevel@tonic-gate static int
4327c478bd9Sstevel@tonic-gate qcn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4367c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4407c478bd9Sstevel@tonic-gate 	prom_printf("qcn_detach(): QCN driver detached\n");
4417c478bd9Sstevel@tonic-gate #endif
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
4447c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
4457c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4467c478bd9Sstevel@tonic-gate 		if (qcn_poll_cycid != CYCLIC_NONE)
4477c478bd9Sstevel@tonic-gate 			cyclic_remove(qcn_poll_cycid);
4487c478bd9Sstevel@tonic-gate 		qcn_poll_cycid = CYCLIC_NONE;
4497c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate #endif
4527c478bd9Sstevel@tonic-gate 
453*54c6b703Sjb 	if (!qcn_state->qcn_polling)
4547c478bd9Sstevel@tonic-gate 		qcn_remove_intrs();
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /* ARGSUSED */
4607c478bd9Sstevel@tonic-gate static int
4617c478bd9Sstevel@tonic-gate qcn_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
4647c478bd9Sstevel@tonic-gate 	int instance = 0;
4657c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4667c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4677c478bd9Sstevel@tonic-gate 		if (qcn_state) {
4687c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4697c478bd9Sstevel@tonic-gate 			prom_printf("qcn_getinfo(): devt2dip %lx\n", arg);
4707c478bd9Sstevel@tonic-gate #endif
4717c478bd9Sstevel@tonic-gate 			*result = (void *)qcn_state->qcn_dip;
4727c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 		break;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4777c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4787c478bd9Sstevel@tonic-gate 		prom_printf("qcn_getinfo(): devt2instance %lx\n", arg);
4797c478bd9Sstevel@tonic-gate #endif
4807c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
4810bd5614cSiskreen 			*result = (void *)(uintptr_t)instance;
4827c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		break;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	return (error);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /* streams open & close */
4917c478bd9Sstevel@tonic-gate /* ARGSUSED */
4927c478bd9Sstevel@tonic-gate static int
4937c478bd9Sstevel@tonic-gate qcn_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	tty_common_t *tty;
4967c478bd9Sstevel@tonic-gate 	int unit = getminor(*devp);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4997c478bd9Sstevel@tonic-gate 	prom_printf("qcn_open(): minor %x\n", unit);
5007c478bd9Sstevel@tonic-gate #endif
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (unit != 0)
5037c478bd9Sstevel@tonic-gate 		return (ENXIO);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* stream already open */
5067c478bd9Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5077c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (!qcn_state) {
5107c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "qcn_open: console was not configured by "
5117c478bd9Sstevel@tonic-gate 		    "autoconfig\n");
5127c478bd9Sstevel@tonic-gate 		return (ENXIO);
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5167c478bd9Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	tty->t_readq = q;
5197c478bd9Sstevel@tonic-gate 	tty->t_writeq = WR(q);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* Link the RD and WR Q's */
5227c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)qcn_state;
5237c478bd9Sstevel@tonic-gate 	qcn_state->qcn_readq = RD(q);
5247c478bd9Sstevel@tonic-gate 	qcn_state->qcn_writeq = WR(q);
5257c478bd9Sstevel@tonic-gate 	qprocson(q);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5307c478bd9Sstevel@tonic-gate 	prom_printf("qcn_open: opened as dev %lx\n", *devp);
5317c478bd9Sstevel@tonic-gate #endif
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /* ARGSUSED */
5377c478bd9Sstevel@tonic-gate static int
5387c478bd9Sstevel@tonic-gate qcn_close(queue_t *q, int flag, cred_t *credp)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_wbufcid != 0) {
5447c478bd9Sstevel@tonic-gate 		unbufcall(qcn_state->qcn_wbufcid);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	ttycommon_close(&qcn_state->qcn_tty);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	qprocsoff(q);
5497c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
5507c478bd9Sstevel@tonic-gate 	qcn_state->qcn_readq = NULL;
5517c478bd9Sstevel@tonic-gate 	qcn_state->qcn_writeq = NULL;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Put procedure for write queue.
5587c478bd9Sstevel@tonic-gate  * Respond to M_IOCTL, M_DATA and M_FLUSH messages here;
5597c478bd9Sstevel@tonic-gate  * It put's the data onto internal qcn_output_q.
5607c478bd9Sstevel@tonic-gate  */
5617c478bd9Sstevel@tonic-gate static int
5627c478bd9Sstevel@tonic-gate qcn_wput(queue_t *q, mblk_t *mp)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5667c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
5677c478bd9Sstevel@tonic-gate 	int i;
5687c478bd9Sstevel@tonic-gate #endif
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (!mp->b_datap) {
5737c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "qcn_wput: null datap");
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5777c478bd9Sstevel@tonic-gate 	prom_printf("qcn_wput(): QCN wput q=%X mp=%X rd=%X wr=%X type=%X\n",
5787c478bd9Sstevel@tonic-gate 		q, mp, mp->b_rptr, mp->b_wptr, mp->b_datap->db_type);
5797c478bd9Sstevel@tonic-gate #endif
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
5847c478bd9Sstevel@tonic-gate 	case M_IOCTL:
5857c478bd9Sstevel@tonic-gate 	case M_CTL:
5867c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5877c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
5887c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): M_IOCTL cmd=%X TIOC=%X\n",
5897c478bd9Sstevel@tonic-gate 		    iocp->ioc_cmd, TIOC);
5907c478bd9Sstevel@tonic-gate #endif
5917c478bd9Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
5927c478bd9Sstevel@tonic-gate 		case TCSETSW:
5937c478bd9Sstevel@tonic-gate 		case TCSETSF:
5947c478bd9Sstevel@tonic-gate 		case TCSETAW:
5957c478bd9Sstevel@tonic-gate 		case TCSETAF:
5967c478bd9Sstevel@tonic-gate 		case TCSBRK:
5977c478bd9Sstevel@tonic-gate 			/*
5987c478bd9Sstevel@tonic-gate 			 * The change do not take effect until all
5997c478bd9Sstevel@tonic-gate 			 * output queued before them is drained.
6007c478bd9Sstevel@tonic-gate 			 * Put this message on the queue, so that
6017c478bd9Sstevel@tonic-gate 			 * "qcn_start" will see it when it's done
6027c478bd9Sstevel@tonic-gate 			 * with the output before it. Poke the start
6037c478bd9Sstevel@tonic-gate 			 * routine, just in case.
6047c478bd9Sstevel@tonic-gate 			 */
6057c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
6067c478bd9Sstevel@tonic-gate 			qcn_start();
6077c478bd9Sstevel@tonic-gate 			break;
6087c478bd9Sstevel@tonic-gate 		default:
6097c478bd9Sstevel@tonic-gate 			qcn_ioctl(q, mp);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		break;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	case M_FLUSH:
6147c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
6157c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
6167c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
6197c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
6207c478bd9Sstevel@tonic-gate 			qreply(q, mp);
6217c478bd9Sstevel@tonic-gate 		} else {
6227c478bd9Sstevel@tonic-gate 			freemsg(mp);
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 		break;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	case M_STOP:
6277c478bd9Sstevel@tonic-gate 		qcn_stopped = B_TRUE;
6287c478bd9Sstevel@tonic-gate 		freemsg(mp);
6297c478bd9Sstevel@tonic-gate 		break;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	case M_START:
6327c478bd9Sstevel@tonic-gate 		qcn_stopped = B_FALSE;
6337c478bd9Sstevel@tonic-gate 		freemsg(mp);
6347c478bd9Sstevel@tonic-gate 		qenable(q);	/* Start up delayed messages */
6357c478bd9Sstevel@tonic-gate 		break;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	case M_DATA:
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
6407c478bd9Sstevel@tonic-gate 		 * and poke the start routine.
6417c478bd9Sstevel@tonic-gate 		 */
6427c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
6437c478bd9Sstevel@tonic-gate 		if (mp->b_rptr < mp->b_wptr) {
6447c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): DATA q=%X mp=%X rd=%X wr=%X\n",
6457c478bd9Sstevel@tonic-gate 			q, mp, mp->b_rptr, mp->b_wptr);
6467c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): [");
6477c478bd9Sstevel@tonic-gate 		for (i = 0; i < mp->b_wptr-mp->b_rptr; i++) {
6487c478bd9Sstevel@tonic-gate 			prom_printf("%c", *(mp->b_rptr+i));
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 		prom_printf("]\n");
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate #endif /* QCN_DEBUG */
6537c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
6547c478bd9Sstevel@tonic-gate 		qcn_start();
6557c478bd9Sstevel@tonic-gate 		break;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	default:
6587c478bd9Sstevel@tonic-gate 		freemsg(mp);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
6627c478bd9Sstevel@tonic-gate 	return (0);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6677c478bd9Sstevel@tonic-gate  */
6687c478bd9Sstevel@tonic-gate static void
6697c478bd9Sstevel@tonic-gate qcn_ioctl(queue_t *q, mblk_t *mp)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
6727c478bd9Sstevel@tonic-gate 	tty_common_t	*tty;
6737c478bd9Sstevel@tonic-gate 	mblk_t		*datamp;
6747c478bd9Sstevel@tonic-gate 	int		data_size;
6757c478bd9Sstevel@tonic-gate 	int		error = 0;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
6787c478bd9Sstevel@tonic-gate 	prom_printf("qcn_ioctl(): q=%X mp=%X\n", q, mp);
6797c478bd9Sstevel@tonic-gate #endif
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
6827c478bd9Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (tty->t_iocpending != NULL) {
6857c478bd9Sstevel@tonic-gate 		freemsg(tty->t_iocpending);
6867c478bd9Sstevel@tonic-gate 		tty->t_iocpending = NULL;
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	data_size = ttycommon_ioctl(tty, q, mp, &error);
6897c478bd9Sstevel@tonic-gate 	if (data_size != 0) {
6907c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_wbufcid)
6917c478bd9Sstevel@tonic-gate 			unbufcall(qcn_state->qcn_wbufcid);
6927c478bd9Sstevel@tonic-gate 		/* call qcn_reioctl() */
6937c478bd9Sstevel@tonic-gate 		qcn_state->qcn_wbufcid =
6947c478bd9Sstevel@tonic-gate 		    bufcall(data_size, BPRI_HI, qcn_reioctl, qcn_state);
6957c478bd9Sstevel@tonic-gate 		return;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (error < 0) {
6997c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
7007c478bd9Sstevel@tonic-gate 		/*
7017c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
7027c478bd9Sstevel@tonic-gate 		 */
7037c478bd9Sstevel@tonic-gate 		error = 0;
7047c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
7057c478bd9Sstevel@tonic-gate 		case TCSBRK:
7067c478bd9Sstevel@tonic-gate 		case TIOCSBRK:
7077c478bd9Sstevel@tonic-gate 		case TIOCCBRK:
7087c478bd9Sstevel@tonic-gate 		case TIOCMSET:
7097c478bd9Sstevel@tonic-gate 		case TIOCMBIS:
7107c478bd9Sstevel@tonic-gate 		case TIOCMBIC:
7117c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7127c478bd9Sstevel@tonic-gate 				qcn_ack(mp, NULL, 0);
7137c478bd9Sstevel@tonic-gate 			else
7147c478bd9Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
7157c478bd9Sstevel@tonic-gate 			break;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		case TIOCMGET:
7187c478bd9Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
7197c478bd9Sstevel@tonic-gate 			if (datamp == NULL) {
7207c478bd9Sstevel@tonic-gate 				error = EAGAIN;
7217c478bd9Sstevel@tonic-gate 				break;
7227c478bd9Sstevel@tonic-gate 			}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 			*(int *)datamp->b_rptr = 0;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7277c478bd9Sstevel@tonic-gate 				qcn_ack(mp, datamp, sizeof (int));
7287c478bd9Sstevel@tonic-gate 			else
7297c478bd9Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
7307c478bd9Sstevel@tonic-gate 			break;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		default:
7337c478bd9Sstevel@tonic-gate 			error = EINVAL;
7347c478bd9Sstevel@tonic-gate 			break;
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	if (error != 0) {
7387c478bd9Sstevel@tonic-gate 		iocp->ioc_count = 0;
7397c478bd9Sstevel@tonic-gate 		iocp->ioc_error = error;
7407c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	qreply(q, mp);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static void
7467c478bd9Sstevel@tonic-gate qcn_reioctl(void *unit)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	queue_t		*q;
7497c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
7507c478bd9Sstevel@tonic-gate 	qcn_t		*qcnp = (qcn_t *)unit;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (!qcnp->qcn_wbufcid)
7537c478bd9Sstevel@tonic-gate 		return;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	qcnp->qcn_wbufcid = 0;
7567c478bd9Sstevel@tonic-gate 	if ((q = qcnp->qcn_tty.t_writeq) == NULL)
7577c478bd9Sstevel@tonic-gate 		return;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if ((mp = qcnp->qcn_tty.t_iocpending) == NULL)
7607c478bd9Sstevel@tonic-gate 		return;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	qcnp->qcn_tty.t_iocpending = NULL;
7637c478bd9Sstevel@tonic-gate 	qcn_ioctl(q, mp);
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate static void
7677c478bd9Sstevel@tonic-gate qcn_ack(mblk_t *mp, mblk_t *dp, uint_t size)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	struct iocblk  *iocp = (struct iocblk *)mp->b_rptr;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCACK;
7727c478bd9Sstevel@tonic-gate 	iocp->ioc_count = size;
7737c478bd9Sstevel@tonic-gate 	iocp->ioc_error = 0;
7747c478bd9Sstevel@tonic-gate 	iocp->ioc_rval = 0;
7757c478bd9Sstevel@tonic-gate 	if (mp->b_cont != NULL)
7767c478bd9Sstevel@tonic-gate 		freeb(mp->b_cont);
7777c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
7787c478bd9Sstevel@tonic-gate 		mp->b_cont = dp;
7797c478bd9Sstevel@tonic-gate 		dp->b_wptr += size;
7807c478bd9Sstevel@tonic-gate 	} else
7817c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate static void
7857c478bd9Sstevel@tonic-gate qcn_start(void)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	queue_t *q;
7897c478bd9Sstevel@tonic-gate 	mblk_t *mp;
7907c478bd9Sstevel@tonic-gate 	int rv;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/*
7957c478bd9Sstevel@tonic-gate 	 * read stream queue and remove data from the queue and
7967c478bd9Sstevel@tonic-gate 	 * transmit them if possible
7977c478bd9Sstevel@tonic-gate 	 */
7987c478bd9Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
7997c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
8007c478bd9Sstevel@tonic-gate 	while (mp = getq(q)) {
8017c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
8027c478bd9Sstevel@tonic-gate 			/*
8037c478bd9Sstevel@tonic-gate 			 * These are those IOCTLs queued up
8047c478bd9Sstevel@tonic-gate 			 * do it now
8057c478bd9Sstevel@tonic-gate 			 */
8067c478bd9Sstevel@tonic-gate 			qcn_ioctl(q, mp);
8077c478bd9Sstevel@tonic-gate 			continue;
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 		/*
8107c478bd9Sstevel@tonic-gate 		 * M_DATA
8117c478bd9Sstevel@tonic-gate 		 */
812136097ceSjb 		rv = qcn_state->cons_transmit(q, mp);
8137c478bd9Sstevel@tonic-gate 		if (rv == EBUSY || rv == EAGAIN)
8147c478bd9Sstevel@tonic-gate 			return;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate static int
819136097ceSjb qcn_transmit_write(queue_t *q, mblk_t *mp)
8207c478bd9Sstevel@tonic-gate {
821136097ceSjb 	mblk_t		*bp;
822136097ceSjb 	size_t		len;
823136097ceSjb 	uint64_t	i;
824136097ceSjb 	uint64_t	retval = 0;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
827136097ceSjb 	prom_printf("qcn_transmit_write(): q=%X mp=%X\n", q, mp);
8287c478bd9Sstevel@tonic-gate #endif
829136097ceSjb 
830136097ceSjb 	while (mp) {
8317c478bd9Sstevel@tonic-gate 		bp = mp;
8327c478bd9Sstevel@tonic-gate 		len = bp->b_wptr - bp->b_rptr;
833136097ceSjb 		/*
834136097ceSjb 		 * Use the console write call to send a block of characters to
835136097ceSjb 		 * the console.
836136097ceSjb 		 */
837136097ceSjb 		i = (len > CONS_WR_BUF_SIZE) ? CONS_WR_BUF_SIZE : len;
838136097ceSjb 		bcopy(bp->b_rptr, qcn_state->cons_write_buffer, i);
839136097ceSjb 		retval = hv_cnwrite(qcn_state->cons_write_buf_ra, i, &i);
840136097ceSjb 
841136097ceSjb 		if (retval == H_EOK) {
842136097ceSjb 			len -= i;
843136097ceSjb 			bp->b_rptr += i;
844136097ceSjb 			/*
845136097ceSjb 			 * if we have finished with this buf, free
846136097ceSjb 			 * and get the next buf if present.
847136097ceSjb 			 */
848136097ceSjb 			if (len == 0) {
849136097ceSjb 				mp = bp->b_cont;
850136097ceSjb 				freeb(bp);
851136097ceSjb 			}
852136097ceSjb 		} else {
853136097ceSjb 			(void) putbq(q, mp);
8547c478bd9Sstevel@tonic-gate 
855136097ceSjb 			switch (retval) {
856136097ceSjb 
857136097ceSjb 			case H_EWOULDBLOCK :
858136097ceSjb 				/*
859136097ceSjb 				 * hypervisor cannot process the request -
860136097ceSjb 				 * channel busy.  Try again later.
861136097ceSjb 				 */
862136097ceSjb 				return (EAGAIN);
863136097ceSjb 
864136097ceSjb 			case H_EIO :
865136097ceSjb 				return (EIO);
866136097ceSjb 			default :
867136097ceSjb 				return (ENXIO);
868136097ceSjb 			}
869136097ceSjb 		}
870136097ceSjb 	}
871136097ceSjb 	return (0);
872136097ceSjb }
873136097ceSjb 
874136097ceSjb static int
875136097ceSjb qcn_transmit_putchr(queue_t *q, mblk_t *mp)
876136097ceSjb {
877136097ceSjb 	caddr_t		buf;
878136097ceSjb 	mblk_t		*bp;
879136097ceSjb 	size_t		len;
880136097ceSjb 	uint64_t	i;
881136097ceSjb 
882136097ceSjb #ifdef QCN_DEBUG
883136097ceSjb 	prom_printf("qcn_transmit_putchr(): q=%X mp=%X\n", q, mp);
884136097ceSjb #endif
885136097ceSjb 	while (mp) {
886136097ceSjb 		bp = mp;
887136097ceSjb 		len = bp->b_wptr - bp->b_rptr;
888136097ceSjb 		buf = (caddr_t)bp->b_rptr;
8897c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
890*54c6b703Sjb 			if (hv_cnputchar(buf[i]) == H_EWOULDBLOCK)
8914bac2208Snarayan 				break;
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 		if (i != len) {
8947c478bd9Sstevel@tonic-gate 			bp->b_rptr += i;
8957c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
8967c478bd9Sstevel@tonic-gate 			return (EAGAIN);
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 		mp = bp->b_cont;
8997c478bd9Sstevel@tonic-gate 		freeb(bp);
900136097ceSjb 	}
9017c478bd9Sstevel@tonic-gate 	return (0);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * called when SC first establishes console connection
9067c478bd9Sstevel@tonic-gate  * drop all the data on the output queue
9077c478bd9Sstevel@tonic-gate  */
9087c478bd9Sstevel@tonic-gate static void
9097c478bd9Sstevel@tonic-gate qcn_flush(void)
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate 	queue_t *q;
9127c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
9177c478bd9Sstevel@tonic-gate 
9180bd5614cSiskreen 	prom_printf("qcn_flush(): WARNING console output is dropped time=%lx\n",
9197c478bd9Sstevel@tonic-gate 	    gethrestime_sec());
9207c478bd9Sstevel@tonic-gate 	while (mp = getq(q))
9217c478bd9Sstevel@tonic-gate 		freemsg(mp);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate static void
9257c478bd9Sstevel@tonic-gate qcn_trigger_softint(void)
9267c478bd9Sstevel@tonic-gate {
927136097ceSjb 	/*
928136097ceSjb 	 * if we are not currently servicing a software interrupt
929136097ceSjb 	 * (qcn_soft_pend == 0), trigger the service routine to run.
930136097ceSjb 	 */
93132179a93Sjb 	if (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_DO) ==
93232179a93Sjb 				QCN_SP_IDL) {
933136097ceSjb 		(void) ddi_intr_trigger_softint(
9347c478bd9Sstevel@tonic-gate 			    qcn_state->qcn_softint_hdl, NULL);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9397c478bd9Sstevel@tonic-gate static uint_t
9407c478bd9Sstevel@tonic-gate qcn_soft_intr(caddr_t arg1, caddr_t arg2)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9437c478bd9Sstevel@tonic-gate 	int	cc;
944136097ceSjb 	int	overflow_check;
9457c478bd9Sstevel@tonic-gate 
946136097ceSjb 	do {
94732179a93Sjb 		(void) atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IP);
948136097ceSjb 		mutex_enter(&qcn_state->qcn_hi_lock);
949136097ceSjb 		if ((cc = RING_CNT(qcn_state)) <= 0) {
950136097ceSjb 			mutex_exit(&qcn_state->qcn_hi_lock);
951136097ceSjb 			goto out;
952136097ceSjb 		}
9537c478bd9Sstevel@tonic-gate 
954136097ceSjb 		if ((mp = allocb(cc, BPRI_MED)) == NULL) {
955136097ceSjb 			qcn_input_dropped += cc;
956136097ceSjb 			mutex_exit(&qcn_state->qcn_hi_lock);
957136097ceSjb 			cmn_err(CE_WARN, "qcn_intr: allocb"
958136097ceSjb 			    "failed (console input dropped)");
959136097ceSjb 			goto out;
960136097ceSjb 		}
9617c478bd9Sstevel@tonic-gate 
962136097ceSjb 		do {
963136097ceSjb 			/* put console input onto stream */
964136097ceSjb 			*(char *)mp->b_wptr++ = RING_GET(qcn_state);
965136097ceSjb 		} while (--cc);
9667c478bd9Sstevel@tonic-gate 
967136097ceSjb 		if ((overflow_check = qcn_state->qcn_rbuf_overflow) != 0) {
968136097ceSjb 			qcn_state->qcn_rbuf_overflow = 0;
969136097ceSjb 		}
9707c478bd9Sstevel@tonic-gate 		mutex_exit(&qcn_state->qcn_hi_lock);
9717c478bd9Sstevel@tonic-gate 
972136097ceSjb 		if (overflow_check) {
973136097ceSjb 			cmn_err(CE_WARN, "qcn: Ring buffer overflow\n");
974136097ceSjb 		}
975136097ceSjb 
976136097ceSjb 		if (qcn_state->qcn_readq) {
977136097ceSjb 			putnext(qcn_state->qcn_readq, mp);
978136097ceSjb 		}
9797c478bd9Sstevel@tonic-gate out:
980*54c6b703Sjb 		/*
981*54c6b703Sjb 		 * If there are pending transmits because hypervisor
982*54c6b703Sjb 		 * returned EWOULDBLOCK poke start now.
983*54c6b703Sjb 		 */
984*54c6b703Sjb 
985*54c6b703Sjb 		if (qcn_state->qcn_writeq != NULL) {
986*54c6b703Sjb 			if (qcn_state->qcn_hangup) {
987*54c6b703Sjb 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
988136097ceSjb 				flushq(qcn_state->qcn_writeq, FLUSHDATA);
989*54c6b703Sjb 				qcn_state->qcn_hangup = 0;
990*54c6b703Sjb 			} else {
991*54c6b703Sjb 				mutex_enter(&qcn_state->qcn_lock);
992*54c6b703Sjb 				qcn_start();
993*54c6b703Sjb 				mutex_exit(&qcn_state->qcn_lock);
994136097ceSjb 			}
9957c478bd9Sstevel@tonic-gate 		}
99632179a93Sjb 		/*
99732179a93Sjb 		 * now loop if another interrupt came in (qcn_trigger_softint
99832179a93Sjb 		 * called) while we were processing the loop
99932179a93Sjb 		 */
100032179a93Sjb 	} while (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IDL) ==
100132179a93Sjb 								QCN_SP_DO);
10027c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10067c478bd9Sstevel@tonic-gate static uint_t
10077c478bd9Sstevel@tonic-gate qcn_hi_intr(caddr_t arg)
1008136097ceSjb {
1009136097ceSjb 	mutex_enter(&qcn_state->qcn_hi_lock);
1010136097ceSjb 
1011136097ceSjb 	qcn_state->cons_receive();
1012136097ceSjb 
1013136097ceSjb 	mutex_exit(&qcn_state->qcn_hi_lock);
1014136097ceSjb 	qcn_trigger_softint();
1015136097ceSjb 
1016136097ceSjb 	return (DDI_INTR_CLAIMED);
1017136097ceSjb }
1018136097ceSjb 
1019136097ceSjb static void
1020136097ceSjb qcn_receive_read(void)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	int64_t rv;
1023136097ceSjb 	uint8_t *bufp;
1024136097ceSjb 	int64_t	retcount = 0;
1025136097ceSjb 	int	i;
10267c478bd9Sstevel@tonic-gate 
1027136097ceSjb 	do {
1028136097ceSjb 		/*
1029136097ceSjb 		 * Maximize available buffer size
1030136097ceSjb 		 */
1031136097ceSjb 		if (RING_CNT(qcn_state) <= 0) {
1032136097ceSjb 			RING_INIT(qcn_state);
10337c478bd9Sstevel@tonic-gate 		}
1034136097ceSjb 		rv = hv_cnread(qcn_state->cons_read_buf_ra +
1035136097ceSjb 				RING_POFF(qcn_state),
1036136097ceSjb 				RING_LEFT(qcn_state),
1037136097ceSjb 				&retcount);
1038136097ceSjb 		bufp = RING_ADDR(qcn_state);
1039136097ceSjb 		if (rv == H_EOK) {
1040136097ceSjb 			/*
1041136097ceSjb 			 * if the alternate break sequence is enabled, test
1042136097ceSjb 			 * the buffer for the sequence and if it is there,
1043136097ceSjb 			 * enter the debugger.
1044136097ceSjb 			 */
1045136097ceSjb 			if (abort_enable == KIOCABORTALTERNATE) {
1046136097ceSjb 				for (i = 0; i < retcount; i++) {
1047136097ceSjb 					if (abort_charseq_recognize(*bufp++)) {
1048136097ceSjb 						abort_sequence_enter(
1049136097ceSjb 								(char *)NULL);
1050136097ceSjb 					}
1051136097ceSjb 				}
1052136097ceSjb 			}
10537c478bd9Sstevel@tonic-gate 
1054136097ceSjb 			/* put console input onto stream */
1055136097ceSjb 			if (retcount > 0) {
1056136097ceSjb 				/*
1057136097ceSjb 				 * the characters are already in the ring,
1058136097ceSjb 				 * just update the pointer so the characters
1059136097ceSjb 				 * can be retrieved.
1060136097ceSjb 				 */
1061136097ceSjb 				RING_UPD(qcn_state, retcount);
1062136097ceSjb 			}
1063136097ceSjb 		} else {
1064136097ceSjb 			switch (rv) {
10657c478bd9Sstevel@tonic-gate 
1066136097ceSjb 			case H_EWOULDBLOCK :
1067136097ceSjb 				/*
1068136097ceSjb 				 * hypervisor cannot handle the request.
1069136097ceSjb 				 * Try again later.
1070136097ceSjb 				 */
1071136097ceSjb 				break;
10727c478bd9Sstevel@tonic-gate 
1073136097ceSjb 
1074136097ceSjb 			case H_BREAK :
1075136097ceSjb 				/*
1076136097ceSjb 				 * on break, unless alternate break sequence is
1077136097ceSjb 				 * enabled, enter the debugger
1078136097ceSjb 				 */
1079136097ceSjb 				if (abort_enable != KIOCABORTALTERNATE)
1080136097ceSjb 					abort_sequence_enter((char *)NULL);
1081136097ceSjb 				break;
1082136097ceSjb 
1083136097ceSjb 			case H_HUP :
1084136097ceSjb 				qcn_state->qcn_hangup = 1;
1085136097ceSjb 				break;
1086136097ceSjb 
1087136097ceSjb 			default :
1088136097ceSjb 				break;
10897c478bd9Sstevel@tonic-gate 			}
10907c478bd9Sstevel@tonic-gate 		}
1091136097ceSjb 	} while (rv == H_EOK);
1092136097ceSjb }
10937c478bd9Sstevel@tonic-gate 
1094136097ceSjb static void
1095136097ceSjb qcn_receive_getchr(void)
1096136097ceSjb {
1097136097ceSjb 	int64_t rv;
1098136097ceSjb 	uint8_t	buf;
1099136097ceSjb 
1100136097ceSjb 	do {
1101136097ceSjb 		rv = hv_cngetchar(&buf);
1102136097ceSjb 		if (rv == H_EOK) {
1103136097ceSjb 			if (abort_enable == KIOCABORTALTERNATE) {
1104136097ceSjb 				if (abort_charseq_recognize(buf)) {
1105136097ceSjb 					abort_sequence_enter((char *)NULL);
1106136097ceSjb 				}
1107136097ceSjb 			}
1108136097ceSjb 
1109136097ceSjb 			/* put console input onto stream */
1110136097ceSjb 			if (RING_POK(qcn_state, 1)) {
1111136097ceSjb 				RING_PUT(qcn_state, buf);
1112136097ceSjb 			} else {
1113136097ceSjb 				qcn_state->qcn_rbuf_overflow++;
1114136097ceSjb 			}
11157c478bd9Sstevel@tonic-gate 		} else {
1116136097ceSjb 			if (rv == H_BREAK) {
1117136097ceSjb 				if (abort_enable != KIOCABORTALTERNATE)
1118136097ceSjb 					abort_sequence_enter((char *)NULL);
1119136097ceSjb 			}
11207c478bd9Sstevel@tonic-gate 
1121136097ceSjb 			if (rv == H_HUP)  {
1122136097ceSjb 				qcn_state->qcn_hangup = 1;
1123136097ceSjb 			}
1124136097ceSjb 			return;
1125136097ceSjb 		}
1126136097ceSjb 	} while (rv == H_EOK);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
11307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11317c478bd9Sstevel@tonic-gate static void
11327c478bd9Sstevel@tonic-gate qcn_poll_handler(void *unused)
11337c478bd9Sstevel@tonic-gate {
11347c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11357c478bd9Sstevel@tonic-gate 	int64_t rv;
11367c478bd9Sstevel@tonic-gate 	uint8_t buf;
11377c478bd9Sstevel@tonic-gate 	int qcn_writeq_flush = 0;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	/* LINTED: E_CONSTANT_CONDITION */
11407c478bd9Sstevel@tonic-gate 	while (1) {
11417c478bd9Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
11427c478bd9Sstevel@tonic-gate 		if (rv == H_BREAK) {
11437c478bd9Sstevel@tonic-gate 			if (abort_enable != KIOCABORTALTERNATE)
11447c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		if (rv == H_HUP)  {
11487c478bd9Sstevel@tonic-gate 			if (qcn_state->qcn_readq) {
11497c478bd9Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
11507c478bd9Sstevel@tonic-gate 				qcn_writeq_flush = 1;
11517c478bd9Sstevel@tonic-gate 			}
11527c478bd9Sstevel@tonic-gate 			goto out;
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		if (rv != H_EOK)
11567c478bd9Sstevel@tonic-gate 			goto out;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 		if (abort_enable == KIOCABORTALTERNATE) {
11597c478bd9Sstevel@tonic-gate 			if (abort_charseq_recognize(buf)) {
11607c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
11617c478bd9Sstevel@tonic-gate 			}
11627c478bd9Sstevel@tonic-gate 		}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		/* put console input onto stream */
11657c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
11667c478bd9Sstevel@tonic-gate 			if ((mp = allocb(1, BPRI_MED)) == NULL) {
11677c478bd9Sstevel@tonic-gate 				qcn_input_dropped++;
11687c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "qcn_intr: allocb"
11697c478bd9Sstevel@tonic-gate 				    "failed (console input dropped)");
11707c478bd9Sstevel@tonic-gate 				return;
11717c478bd9Sstevel@tonic-gate 			}
11727c478bd9Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = buf;
11737c478bd9Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate out:
11777c478bd9Sstevel@tonic-gate /*
11787c478bd9Sstevel@tonic-gate  * If there are pending transmits because hypervisor
11797c478bd9Sstevel@tonic-gate  * returned EWOULDBLOCK poke start now.
11807c478bd9Sstevel@tonic-gate  */
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
11837c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL) {
11847c478bd9Sstevel@tonic-gate 		if (qcn_writeq_flush) {
11857c478bd9Sstevel@tonic-gate 			flushq(qcn_state->qcn_writeq, FLUSHDATA);
11867c478bd9Sstevel@tonic-gate 		} else {
11877c478bd9Sstevel@tonic-gate 			qcn_start();
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate #endif
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate /*
11957c478bd9Sstevel@tonic-gate  * Check for abort character sequence, copied from zs_async.c
11967c478bd9Sstevel@tonic-gate  */
11977c478bd9Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate static boolean_t
12007c478bd9Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	static int state = 0;
12037c478bd9Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (ch == sequence[state]) {
12067c478bd9Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
12077c478bd9Sstevel@tonic-gate 			state = 0;
12087c478bd9Sstevel@tonic-gate 			return (B_TRUE);
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 	} else {
12117c478bd9Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 	return (B_FALSE);
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate static int
12187c478bd9Sstevel@tonic-gate qcn_rsrv(queue_t *q)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12237c478bd9Sstevel@tonic-gate 		return (0);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
12287c478bd9Sstevel@tonic-gate 		if (canputnext(q))
12297c478bd9Sstevel@tonic-gate 			putnext(q, mp);
12307c478bd9Sstevel@tonic-gate 		else if (mp->b_datap->db_type >= QPCTL)
12317c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	return (0);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /* ARGSUSED */
12407c478bd9Sstevel@tonic-gate static int
12417c478bd9Sstevel@tonic-gate qcn_wsrv(queue_t *q)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12447c478bd9Sstevel@tonic-gate 		return (0);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL)
12497c478bd9Sstevel@tonic-gate 		qcn_start();
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	return (0);
12547c478bd9Sstevel@tonic-gate }
1255