xref: /titanic_44/usr/src/uts/sun4v/io/qcn.c (revision 136097cead98e48ee9260b7cc3b7edd749c5c808)
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>
52*136097ceSjb145095 #include <sys/hsvc.h>
53*136097ceSjb145095 #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);
70*136097ceSjb145095 static int qcn_transmit_write(queue_t *, mblk_t *);
71*136097ceSjb145095 static int qcn_transmit_putchr(queue_t *, mblk_t *);
72*136097ceSjb145095 static void qcn_receive_read(void);
73*136097ceSjb145095 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);
877c478bd9Sstevel@tonic-gate 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;
947c478bd9Sstevel@tonic-gate static uint64_t	qcn_poll_interval = 5;  /* milli sec */
957c478bd9Sstevel@tonic-gate 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;
190*136097ceSjb145095 	uint64_t	major, minor;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	qcn_state = kmem_zalloc(sizeof (qcn_t), KM_SLEEP);
193*136097ceSjb145095 	qcn_state->qcn_ring = contig_mem_alloc(RINGSIZE);
194*136097ceSjb145095 	if (qcn_state->qcn_ring == NULL)
195*136097ceSjb145095 		cmn_err(CE_PANIC, "console ring allocation failed");
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
198*136097ceSjb145095 	if (error != 0) {
199*136097ceSjb145095 		contig_mem_free(qcn_state->qcn_ring, RINGSIZE);
2007c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state, sizeof (qcn_t));
2017c478bd9Sstevel@tonic-gate 		return (error);
2027c478bd9Sstevel@tonic-gate 	}
203*136097ceSjb145095 	/*
204*136097ceSjb145095 	 * check minor number to see if CONS_WRITE is supported
205*136097ceSjb145095 	 * if so, set up real address of the buffers for hv calls.
206*136097ceSjb145095 	 */
207*136097ceSjb145095 
208*136097ceSjb145095 	if (((hsvc_version(HSVC_GROUP_CORE, &major, &minor) == 0) &&
209*136097ceSjb145095 		(major == QCN_API_MAJOR) && (minor >= QCN_API_MINOR))) {
210*136097ceSjb145095 		qcn_state->cons_write_buffer =
211*136097ceSjb145095 					contig_mem_alloc(CONS_WR_BUF_SIZE);
212*136097ceSjb145095 		if (qcn_state->cons_write_buffer != NULL) {
213*136097ceSjb145095 			qcn_state->cons_write_buf_ra =
214*136097ceSjb145095 					va_to_pa(qcn_state->cons_write_buffer);
215*136097ceSjb145095 			qcn_state->cons_transmit = qcn_transmit_write;
216*136097ceSjb145095 			qcn_state->cons_receive = qcn_receive_read;
217*136097ceSjb145095 			qcn_state->cons_read_buf_ra =
218*136097ceSjb145095 					va_to_pa((char *)RING_ADDR(qcn_state));
219*136097ceSjb145095 		}
220*136097ceSjb145095 	}
221*136097ceSjb145095 	if (qcn_state->cons_transmit == NULL) {
222*136097ceSjb145095 		qcn_state->cons_transmit = qcn_transmit_putchr;
223*136097ceSjb145095 		qcn_state->cons_receive = qcn_receive_getchr;
224*136097ceSjb145095 	}
225*136097ceSjb145095 	return (0);
226*136097ceSjb145095 }
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 	qcn_state->qcn_soft_pri =
4107c478bd9Sstevel@tonic-gate 	    (ddi_iblock_cookie_t)(uint64_t)soft_prip;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_hi_lock, NULL, MUTEX_DRIVER,
4130bd5614cSiskreen 	    (void *)(uintptr_t)(qcn_state->qcn_intr_pri));
4147c478bd9Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_softlock, NULL, MUTEX_DRIVER,
4157c478bd9Sstevel@tonic-gate 	    (void *)(qcn_state->qcn_soft_pri));
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_lock, NULL, MUTEX_DRIVER, NULL);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 *  Enable  interrupts
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
4247c478bd9Sstevel@tonic-gate 		qcn_intr_enable();
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4277c478bd9Sstevel@tonic-gate 	prom_printf("qcn_attach(): qcn driver attached\n");
4287c478bd9Sstevel@tonic-gate #endif
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /* ARGSUSED */
4357c478bd9Sstevel@tonic-gate static int
4367c478bd9Sstevel@tonic-gate qcn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4447c478bd9Sstevel@tonic-gate 	prom_printf("qcn_detach(): QCN driver detached\n");
4457c478bd9Sstevel@tonic-gate #endif
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
4487c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
4497c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4507c478bd9Sstevel@tonic-gate 		if (qcn_poll_cycid != CYCLIC_NONE)
4517c478bd9Sstevel@tonic-gate 			cyclic_remove(qcn_poll_cycid);
4527c478bd9Sstevel@tonic-gate 		qcn_poll_cycid = CYCLIC_NONE;
4537c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate #endif
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (!qcn_state->qcn_polling)
4587c478bd9Sstevel@tonic-gate 		qcn_remove_intrs();
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /* ARGSUSED */
4647c478bd9Sstevel@tonic-gate static int
4657c478bd9Sstevel@tonic-gate qcn_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
4687c478bd9Sstevel@tonic-gate 	int instance = 0;
4697c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4707c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4717c478bd9Sstevel@tonic-gate 		if (qcn_state) {
4727c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4737c478bd9Sstevel@tonic-gate 			prom_printf("qcn_getinfo(): devt2dip %lx\n", arg);
4747c478bd9Sstevel@tonic-gate #endif
4757c478bd9Sstevel@tonic-gate 			*result = (void *)qcn_state->qcn_dip;
4767c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4817c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4827c478bd9Sstevel@tonic-gate 		prom_printf("qcn_getinfo(): devt2instance %lx\n", arg);
4837c478bd9Sstevel@tonic-gate #endif
4847c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
4850bd5614cSiskreen 			*result = (void *)(uintptr_t)instance;
4867c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		break;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	return (error);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate /* streams open & close */
4957c478bd9Sstevel@tonic-gate /* ARGSUSED */
4967c478bd9Sstevel@tonic-gate static int
4977c478bd9Sstevel@tonic-gate qcn_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	tty_common_t *tty;
5007c478bd9Sstevel@tonic-gate 	int unit = getminor(*devp);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5037c478bd9Sstevel@tonic-gate 	prom_printf("qcn_open(): minor %x\n", unit);
5047c478bd9Sstevel@tonic-gate #endif
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (unit != 0)
5077c478bd9Sstevel@tonic-gate 		return (ENXIO);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/* stream already open */
5107c478bd9Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5117c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (!qcn_state) {
5147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "qcn_open: console was not configured by "
5157c478bd9Sstevel@tonic-gate 		    "autoconfig\n");
5167c478bd9Sstevel@tonic-gate 		return (ENXIO);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5207c478bd9Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	tty->t_readq = q;
5237c478bd9Sstevel@tonic-gate 	tty->t_writeq = WR(q);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* Link the RD and WR Q's */
5267c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)qcn_state;
5277c478bd9Sstevel@tonic-gate 	qcn_state->qcn_readq = RD(q);
5287c478bd9Sstevel@tonic-gate 	qcn_state->qcn_writeq = WR(q);
5297c478bd9Sstevel@tonic-gate 	qprocson(q);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5347c478bd9Sstevel@tonic-gate 	prom_printf("qcn_open: opened as dev %lx\n", *devp);
5357c478bd9Sstevel@tonic-gate #endif
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /* ARGSUSED */
5417c478bd9Sstevel@tonic-gate static int
5427c478bd9Sstevel@tonic-gate qcn_close(queue_t *q, int flag, cred_t *credp)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_wbufcid != 0) {
5487c478bd9Sstevel@tonic-gate 		unbufcall(qcn_state->qcn_wbufcid);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	ttycommon_close(&qcn_state->qcn_tty);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	qprocsoff(q);
5537c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
5547c478bd9Sstevel@tonic-gate 	qcn_state->qcn_readq = NULL;
5557c478bd9Sstevel@tonic-gate 	qcn_state->qcn_writeq = NULL;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * Put procedure for write queue.
5627c478bd9Sstevel@tonic-gate  * Respond to M_IOCTL, M_DATA and M_FLUSH messages here;
5637c478bd9Sstevel@tonic-gate  * It put's the data onto internal qcn_output_q.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate static int
5667c478bd9Sstevel@tonic-gate qcn_wput(queue_t *q, mblk_t *mp)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5707c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
5717c478bd9Sstevel@tonic-gate 	int i;
5727c478bd9Sstevel@tonic-gate #endif
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (!mp->b_datap) {
5777c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "qcn_wput: null datap");
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5817c478bd9Sstevel@tonic-gate 	prom_printf("qcn_wput(): QCN wput q=%X mp=%X rd=%X wr=%X type=%X\n",
5827c478bd9Sstevel@tonic-gate 		q, mp, mp->b_rptr, mp->b_wptr, mp->b_datap->db_type);
5837c478bd9Sstevel@tonic-gate #endif
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
5887c478bd9Sstevel@tonic-gate 	case M_IOCTL:
5897c478bd9Sstevel@tonic-gate 	case M_CTL:
5907c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5917c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
5927c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): M_IOCTL cmd=%X TIOC=%X\n",
5937c478bd9Sstevel@tonic-gate 		    iocp->ioc_cmd, TIOC);
5947c478bd9Sstevel@tonic-gate #endif
5957c478bd9Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
5967c478bd9Sstevel@tonic-gate 		case TCSETSW:
5977c478bd9Sstevel@tonic-gate 		case TCSETSF:
5987c478bd9Sstevel@tonic-gate 		case TCSETAW:
5997c478bd9Sstevel@tonic-gate 		case TCSETAF:
6007c478bd9Sstevel@tonic-gate 		case TCSBRK:
6017c478bd9Sstevel@tonic-gate 			/*
6027c478bd9Sstevel@tonic-gate 			 * The change do not take effect until all
6037c478bd9Sstevel@tonic-gate 			 * output queued before them is drained.
6047c478bd9Sstevel@tonic-gate 			 * Put this message on the queue, so that
6057c478bd9Sstevel@tonic-gate 			 * "qcn_start" will see it when it's done
6067c478bd9Sstevel@tonic-gate 			 * with the output before it. Poke the start
6077c478bd9Sstevel@tonic-gate 			 * routine, just in case.
6087c478bd9Sstevel@tonic-gate 			 */
6097c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
6107c478bd9Sstevel@tonic-gate 			qcn_start();
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 		default:
6137c478bd9Sstevel@tonic-gate 			qcn_ioctl(q, mp);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	case M_FLUSH:
6187c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
6197c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
6207c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
6237c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
6247c478bd9Sstevel@tonic-gate 			qreply(q, mp);
6257c478bd9Sstevel@tonic-gate 		} else {
6267c478bd9Sstevel@tonic-gate 			freemsg(mp);
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	case M_STOP:
6317c478bd9Sstevel@tonic-gate 		qcn_stopped = B_TRUE;
6327c478bd9Sstevel@tonic-gate 		freemsg(mp);
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	case M_START:
6367c478bd9Sstevel@tonic-gate 		qcn_stopped = B_FALSE;
6377c478bd9Sstevel@tonic-gate 		freemsg(mp);
6387c478bd9Sstevel@tonic-gate 		qenable(q);	/* Start up delayed messages */
6397c478bd9Sstevel@tonic-gate 		break;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	case M_DATA:
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
6447c478bd9Sstevel@tonic-gate 		 * and poke the start routine.
6457c478bd9Sstevel@tonic-gate 		 */
6467c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
6477c478bd9Sstevel@tonic-gate 		if (mp->b_rptr < mp->b_wptr) {
6487c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): DATA q=%X mp=%X rd=%X wr=%X\n",
6497c478bd9Sstevel@tonic-gate 			q, mp, mp->b_rptr, mp->b_wptr);
6507c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): [");
6517c478bd9Sstevel@tonic-gate 		for (i = 0; i < mp->b_wptr-mp->b_rptr; i++) {
6527c478bd9Sstevel@tonic-gate 			prom_printf("%c", *(mp->b_rptr+i));
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 		prom_printf("]\n");
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate #endif /* QCN_DEBUG */
6577c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
6587c478bd9Sstevel@tonic-gate 		qcn_start();
6597c478bd9Sstevel@tonic-gate 		break;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	default:
6627c478bd9Sstevel@tonic-gate 		freemsg(mp);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
6667c478bd9Sstevel@tonic-gate 	return (0);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate static void
6737c478bd9Sstevel@tonic-gate qcn_ioctl(queue_t *q, mblk_t *mp)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
6767c478bd9Sstevel@tonic-gate 	tty_common_t	*tty;
6777c478bd9Sstevel@tonic-gate 	mblk_t		*datamp;
6787c478bd9Sstevel@tonic-gate 	int		data_size;
6797c478bd9Sstevel@tonic-gate 	int		error = 0;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
6827c478bd9Sstevel@tonic-gate 	prom_printf("qcn_ioctl(): q=%X mp=%X\n", q, mp);
6837c478bd9Sstevel@tonic-gate #endif
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
6867c478bd9Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (tty->t_iocpending != NULL) {
6897c478bd9Sstevel@tonic-gate 		freemsg(tty->t_iocpending);
6907c478bd9Sstevel@tonic-gate 		tty->t_iocpending = NULL;
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 	data_size = ttycommon_ioctl(tty, q, mp, &error);
6937c478bd9Sstevel@tonic-gate 	if (data_size != 0) {
6947c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_wbufcid)
6957c478bd9Sstevel@tonic-gate 			unbufcall(qcn_state->qcn_wbufcid);
6967c478bd9Sstevel@tonic-gate 		/* call qcn_reioctl() */
6977c478bd9Sstevel@tonic-gate 		qcn_state->qcn_wbufcid =
6987c478bd9Sstevel@tonic-gate 		    bufcall(data_size, BPRI_HI, qcn_reioctl, qcn_state);
6997c478bd9Sstevel@tonic-gate 		return;
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (error < 0) {
7037c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
7047c478bd9Sstevel@tonic-gate 		/*
7057c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
7067c478bd9Sstevel@tonic-gate 		 */
7077c478bd9Sstevel@tonic-gate 		error = 0;
7087c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
7097c478bd9Sstevel@tonic-gate 		case TCSBRK:
7107c478bd9Sstevel@tonic-gate 		case TIOCSBRK:
7117c478bd9Sstevel@tonic-gate 		case TIOCCBRK:
7127c478bd9Sstevel@tonic-gate 		case TIOCMSET:
7137c478bd9Sstevel@tonic-gate 		case TIOCMBIS:
7147c478bd9Sstevel@tonic-gate 		case TIOCMBIC:
7157c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7167c478bd9Sstevel@tonic-gate 				qcn_ack(mp, NULL, 0);
7177c478bd9Sstevel@tonic-gate 			else
7187c478bd9Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
7197c478bd9Sstevel@tonic-gate 			break;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		case TIOCMGET:
7227c478bd9Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
7237c478bd9Sstevel@tonic-gate 			if (datamp == NULL) {
7247c478bd9Sstevel@tonic-gate 				error = EAGAIN;
7257c478bd9Sstevel@tonic-gate 				break;
7267c478bd9Sstevel@tonic-gate 			}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 			*(int *)datamp->b_rptr = 0;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7317c478bd9Sstevel@tonic-gate 				qcn_ack(mp, datamp, sizeof (int));
7327c478bd9Sstevel@tonic-gate 			else
7337c478bd9Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
7347c478bd9Sstevel@tonic-gate 			break;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		default:
7377c478bd9Sstevel@tonic-gate 			error = EINVAL;
7387c478bd9Sstevel@tonic-gate 			break;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	if (error != 0) {
7427c478bd9Sstevel@tonic-gate 		iocp->ioc_count = 0;
7437c478bd9Sstevel@tonic-gate 		iocp->ioc_error = error;
7447c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	qreply(q, mp);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate static void
7507c478bd9Sstevel@tonic-gate qcn_reioctl(void *unit)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	queue_t		*q;
7537c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
7547c478bd9Sstevel@tonic-gate 	qcn_t		*qcnp = (qcn_t *)unit;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (!qcnp->qcn_wbufcid)
7577c478bd9Sstevel@tonic-gate 		return;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	qcnp->qcn_wbufcid = 0;
7607c478bd9Sstevel@tonic-gate 	if ((q = qcnp->qcn_tty.t_writeq) == NULL)
7617c478bd9Sstevel@tonic-gate 		return;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if ((mp = qcnp->qcn_tty.t_iocpending) == NULL)
7647c478bd9Sstevel@tonic-gate 		return;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	qcnp->qcn_tty.t_iocpending = NULL;
7677c478bd9Sstevel@tonic-gate 	qcn_ioctl(q, mp);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate static void
7717c478bd9Sstevel@tonic-gate qcn_ack(mblk_t *mp, mblk_t *dp, uint_t size)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	struct iocblk  *iocp = (struct iocblk *)mp->b_rptr;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCACK;
7767c478bd9Sstevel@tonic-gate 	iocp->ioc_count = size;
7777c478bd9Sstevel@tonic-gate 	iocp->ioc_error = 0;
7787c478bd9Sstevel@tonic-gate 	iocp->ioc_rval = 0;
7797c478bd9Sstevel@tonic-gate 	if (mp->b_cont != NULL)
7807c478bd9Sstevel@tonic-gate 		freeb(mp->b_cont);
7817c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
7827c478bd9Sstevel@tonic-gate 		mp->b_cont = dp;
7837c478bd9Sstevel@tonic-gate 		dp->b_wptr += size;
7847c478bd9Sstevel@tonic-gate 	} else
7857c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static void
7897c478bd9Sstevel@tonic-gate qcn_start(void)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	queue_t *q;
7937c478bd9Sstevel@tonic-gate 	mblk_t *mp;
7947c478bd9Sstevel@tonic-gate 	int rv;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/*
7997c478bd9Sstevel@tonic-gate 	 * read stream queue and remove data from the queue and
8007c478bd9Sstevel@tonic-gate 	 * transmit them if possible
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
8037c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
8047c478bd9Sstevel@tonic-gate 	while (mp = getq(q)) {
8057c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
8067c478bd9Sstevel@tonic-gate 			/*
8077c478bd9Sstevel@tonic-gate 			 * These are those IOCTLs queued up
8087c478bd9Sstevel@tonic-gate 			 * do it now
8097c478bd9Sstevel@tonic-gate 			 */
8107c478bd9Sstevel@tonic-gate 			qcn_ioctl(q, mp);
8117c478bd9Sstevel@tonic-gate 			continue;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		/*
8147c478bd9Sstevel@tonic-gate 		 * M_DATA
8157c478bd9Sstevel@tonic-gate 		 */
816*136097ceSjb145095 		rv = qcn_state->cons_transmit(q, mp);
8177c478bd9Sstevel@tonic-gate 		if (rv == EBUSY || rv == EAGAIN)
8187c478bd9Sstevel@tonic-gate 			return;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate static int
823*136097ceSjb145095 qcn_transmit_write(queue_t *q, mblk_t *mp)
824*136097ceSjb145095 {
825*136097ceSjb145095 	mblk_t		*bp;
826*136097ceSjb145095 	size_t		len;
827*136097ceSjb145095 	uint64_t	i;
828*136097ceSjb145095 	uint64_t	retval = 0;
829*136097ceSjb145095 
830*136097ceSjb145095 #ifdef QCN_DEBUG
831*136097ceSjb145095 	prom_printf("qcn_transmit_write(): q=%X mp=%X\n", q, mp);
832*136097ceSjb145095 #endif
833*136097ceSjb145095 
834*136097ceSjb145095 	while (mp) {
835*136097ceSjb145095 		bp = mp;
836*136097ceSjb145095 		len = bp->b_wptr - bp->b_rptr;
837*136097ceSjb145095 		/*
838*136097ceSjb145095 		 * Use the console write call to send a block of characters to
839*136097ceSjb145095 		 * the console.
840*136097ceSjb145095 		 */
841*136097ceSjb145095 		i = (len > CONS_WR_BUF_SIZE) ? CONS_WR_BUF_SIZE : len;
842*136097ceSjb145095 		bcopy(bp->b_rptr, qcn_state->cons_write_buffer, i);
843*136097ceSjb145095 		retval = hv_cnwrite(qcn_state->cons_write_buf_ra, i, &i);
844*136097ceSjb145095 
845*136097ceSjb145095 		if (retval == H_EOK) {
846*136097ceSjb145095 			len -= i;
847*136097ceSjb145095 			bp->b_rptr += i;
848*136097ceSjb145095 			/*
849*136097ceSjb145095 			 * if we have finished with this buf, free
850*136097ceSjb145095 			 * and get the next buf if present.
851*136097ceSjb145095 			 */
852*136097ceSjb145095 			if (len == 0) {
853*136097ceSjb145095 				mp = bp->b_cont;
854*136097ceSjb145095 				freeb(bp);
855*136097ceSjb145095 			}
856*136097ceSjb145095 		} else {
857*136097ceSjb145095 			(void) putbq(q, mp);
858*136097ceSjb145095 
859*136097ceSjb145095 			switch (retval) {
860*136097ceSjb145095 
861*136097ceSjb145095 			case H_EWOULDBLOCK :
862*136097ceSjb145095 				/*
863*136097ceSjb145095 				 * hypervisor cannot process the request -
864*136097ceSjb145095 				 * channel busy.  Try again later.
865*136097ceSjb145095 				 */
866*136097ceSjb145095 				return (EAGAIN);
867*136097ceSjb145095 
868*136097ceSjb145095 			case H_EIO :
869*136097ceSjb145095 				return (EIO);
870*136097ceSjb145095 			default :
871*136097ceSjb145095 				return (ENXIO);
872*136097ceSjb145095 			}
873*136097ceSjb145095 		}
874*136097ceSjb145095 	}
875*136097ceSjb145095 	return (0);
876*136097ceSjb145095 }
877*136097ceSjb145095 
878*136097ceSjb145095 static int
879*136097ceSjb145095 qcn_transmit_putchr(queue_t *q, mblk_t *mp)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	caddr_t		buf;
8827c478bd9Sstevel@tonic-gate 	mblk_t		*bp;
8837c478bd9Sstevel@tonic-gate 	size_t		len;
884*136097ceSjb145095 	uint64_t	i;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
887*136097ceSjb145095 	prom_printf("qcn_transmit_putchr(): q=%X mp=%X\n", q, mp);
8887c478bd9Sstevel@tonic-gate #endif
889*136097ceSjb145095 	while (mp) {
8907c478bd9Sstevel@tonic-gate 		bp = mp;
8917c478bd9Sstevel@tonic-gate 		len = bp->b_wptr - bp->b_rptr;
8927c478bd9Sstevel@tonic-gate 		buf = (caddr_t)bp->b_rptr;
8937c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
8941ae08745Sheppo 			if (hv_cnputchar(buf[i]) == H_EWOULDBLOCK)
8957c478bd9Sstevel@tonic-gate 			break;
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 		if (i != len) {
8987c478bd9Sstevel@tonic-gate 			bp->b_rptr += i;
8997c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
9007c478bd9Sstevel@tonic-gate 			return (EAGAIN);
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 		mp = bp->b_cont;
9037c478bd9Sstevel@tonic-gate 		freeb(bp);
904*136097ceSjb145095 	}
9057c478bd9Sstevel@tonic-gate 	return (0);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * called when SC first establishes console connection
9107c478bd9Sstevel@tonic-gate  * drop all the data on the output queue
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate static void
9137c478bd9Sstevel@tonic-gate qcn_flush(void)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	queue_t *q;
9167c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
9217c478bd9Sstevel@tonic-gate 
9220bd5614cSiskreen 	prom_printf("qcn_flush(): WARNING console output is dropped time=%lx\n",
9237c478bd9Sstevel@tonic-gate 	    gethrestime_sec());
9247c478bd9Sstevel@tonic-gate 	while (mp = getq(q))
9257c478bd9Sstevel@tonic-gate 		freemsg(mp);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static void
9297c478bd9Sstevel@tonic-gate qcn_trigger_softint(void)
9307c478bd9Sstevel@tonic-gate {
931*136097ceSjb145095 	/*
932*136097ceSjb145095 	 * get lock for software ints and see if we need to trigger a soft
933*136097ceSjb145095 	 * interrupt (no pending sofware ints).
934*136097ceSjb145095 	 */
935*136097ceSjb145095 	mutex_enter(&qcn_state->qcn_softlock);
936*136097ceSjb145095 	/*
937*136097ceSjb145095 	 * if we are not currently servicing a software interrupt
938*136097ceSjb145095 	 * (qcn_soft_pend == 0), trigger the service routine to run.
939*136097ceSjb145095 	 */
940*136097ceSjb145095 	if (!qcn_state->qcn_soft_pend++) {
9417c478bd9Sstevel@tonic-gate 		mutex_exit(&qcn_state->qcn_softlock);
9427c478bd9Sstevel@tonic-gate 		(void) ddi_intr_trigger_softint(
9437c478bd9Sstevel@tonic-gate 			    qcn_state->qcn_softint_hdl, NULL);
944*136097ceSjb145095 	} else {
9457c478bd9Sstevel@tonic-gate 		mutex_exit(&qcn_state->qcn_softlock);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9507c478bd9Sstevel@tonic-gate static uint_t
9517c478bd9Sstevel@tonic-gate qcn_soft_intr(caddr_t arg1, caddr_t arg2)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9547c478bd9Sstevel@tonic-gate 	int	cc;
955*136097ceSjb145095 	int	overflow_check;
9567c478bd9Sstevel@tonic-gate 
957*136097ceSjb145095 	/*
958*136097ceSjb145095 	 * grab the lock for the soft int pending.  It is grabbed here so
959*136097ceSjb145095 	 * that the decrement at the end of the do loop is under lock
960*136097ceSjb145095 	 * protection.
961*136097ceSjb145095 	 */
9627c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_softlock);
963*136097ceSjb145095 	do {
964*136097ceSjb145095 		/*
965*136097ceSjb145095 		 * release the soft int lock so that the interrupt routine
966*136097ceSjb145095 		 * will not be held up.
967*136097ceSjb145095 		 */
968*136097ceSjb145095 		mutex_exit(&qcn_state->qcn_softlock);
9697c478bd9Sstevel@tonic-gate 		mutex_enter(&qcn_state->qcn_hi_lock);
9707c478bd9Sstevel@tonic-gate 		if ((cc = RING_CNT(qcn_state)) <= 0) {
9717c478bd9Sstevel@tonic-gate 			mutex_exit(&qcn_state->qcn_hi_lock);
9727c478bd9Sstevel@tonic-gate 			goto out;
9737c478bd9Sstevel@tonic-gate 		}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		if ((mp = allocb(cc, BPRI_MED)) == NULL) {
9767c478bd9Sstevel@tonic-gate 			qcn_input_dropped += cc;
977*136097ceSjb145095 			mutex_exit(&qcn_state->qcn_hi_lock);
9787c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_intr: allocb"
9797c478bd9Sstevel@tonic-gate 			    "failed (console input dropped)");
9807c478bd9Sstevel@tonic-gate 			goto out;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		do {
9847c478bd9Sstevel@tonic-gate 			/* put console input onto stream */
9857c478bd9Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = RING_GET(qcn_state);
9867c478bd9Sstevel@tonic-gate 		} while (--cc);
9877c478bd9Sstevel@tonic-gate 
988*136097ceSjb145095 		if ((overflow_check = qcn_state->qcn_rbuf_overflow) != 0) {
9897c478bd9Sstevel@tonic-gate 			qcn_state->qcn_rbuf_overflow = 0;
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 		mutex_exit(&qcn_state->qcn_hi_lock);
992*136097ceSjb145095 
993*136097ceSjb145095 		if (overflow_check) {
994*136097ceSjb145095 			cmn_err(CE_WARN, "qcn: Ring buffer overflow\n");
995*136097ceSjb145095 		}
996*136097ceSjb145095 
9977c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
9987c478bd9Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate out:
10017c478bd9Sstevel@tonic-gate 		/*
10027c478bd9Sstevel@tonic-gate 		 * If there are pending transmits because hypervisor
10037c478bd9Sstevel@tonic-gate 		 * returned EWOULDBLOCK poke start now.
10047c478bd9Sstevel@tonic-gate 		 */
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_writeq != NULL) {
10077c478bd9Sstevel@tonic-gate 			if (qcn_state->qcn_hangup) {
10087c478bd9Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
10097c478bd9Sstevel@tonic-gate 				flushq(qcn_state->qcn_writeq, FLUSHDATA);
10107c478bd9Sstevel@tonic-gate 				qcn_state->qcn_hangup = 0;
10117c478bd9Sstevel@tonic-gate 			} else {
10127c478bd9Sstevel@tonic-gate 				mutex_enter(&qcn_state->qcn_lock);
10137c478bd9Sstevel@tonic-gate 				qcn_start();
10147c478bd9Sstevel@tonic-gate 				mutex_exit(&qcn_state->qcn_lock);
10157c478bd9Sstevel@tonic-gate 			}
10167c478bd9Sstevel@tonic-gate 		}
1017*136097ceSjb145095 		mutex_enter(&qcn_state->qcn_softlock);
1018*136097ceSjb145095 	} while (qcn_state->qcn_soft_pend-- > 1);
10197c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_softlock);
10207c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10247c478bd9Sstevel@tonic-gate static uint_t
10257c478bd9Sstevel@tonic-gate qcn_hi_intr(caddr_t arg)
10267c478bd9Sstevel@tonic-gate {
1027*136097ceSjb145095 	mutex_enter(&qcn_state->qcn_hi_lock);
1028*136097ceSjb145095 
1029*136097ceSjb145095 	qcn_state->cons_receive();
1030*136097ceSjb145095 
1031*136097ceSjb145095 	mutex_exit(&qcn_state->qcn_hi_lock);
1032*136097ceSjb145095 	qcn_trigger_softint();
1033*136097ceSjb145095 
1034*136097ceSjb145095 	return (DDI_INTR_CLAIMED);
1035*136097ceSjb145095 }
1036*136097ceSjb145095 
1037*136097ceSjb145095 static void
1038*136097ceSjb145095 qcn_receive_read(void)
1039*136097ceSjb145095 {
1040*136097ceSjb145095 	int64_t rv;
1041*136097ceSjb145095 	uint8_t *bufp;
1042*136097ceSjb145095 	int64_t	retcount = 0;
1043*136097ceSjb145095 	int	i;
1044*136097ceSjb145095 
1045*136097ceSjb145095 	do {
1046*136097ceSjb145095 		/*
1047*136097ceSjb145095 		 * Maximize available buffer size
1048*136097ceSjb145095 		 */
1049*136097ceSjb145095 		if (RING_CNT(qcn_state) <= 0) {
1050*136097ceSjb145095 			RING_INIT(qcn_state);
1051*136097ceSjb145095 		}
1052*136097ceSjb145095 		rv = hv_cnread(qcn_state->cons_read_buf_ra +
1053*136097ceSjb145095 				RING_POFF(qcn_state),
1054*136097ceSjb145095 				RING_LEFT(qcn_state),
1055*136097ceSjb145095 				&retcount);
1056*136097ceSjb145095 		bufp = RING_ADDR(qcn_state);
1057*136097ceSjb145095 		if (rv == H_EOK) {
1058*136097ceSjb145095 			/*
1059*136097ceSjb145095 			 * if the alternate break sequence is enabled, test
1060*136097ceSjb145095 			 * the buffer for the sequence and if it is there,
1061*136097ceSjb145095 			 * enter the debugger.
1062*136097ceSjb145095 			 */
1063*136097ceSjb145095 			if (abort_enable == KIOCABORTALTERNATE) {
1064*136097ceSjb145095 				for (i = 0; i < retcount; i++) {
1065*136097ceSjb145095 					if (abort_charseq_recognize(*bufp++)) {
1066*136097ceSjb145095 						abort_sequence_enter(
1067*136097ceSjb145095 								(char *)NULL);
1068*136097ceSjb145095 					}
1069*136097ceSjb145095 				}
1070*136097ceSjb145095 			}
1071*136097ceSjb145095 
1072*136097ceSjb145095 			/* put console input onto stream */
1073*136097ceSjb145095 			if (retcount > 0) {
1074*136097ceSjb145095 				/*
1075*136097ceSjb145095 				 * the characters are already in the ring,
1076*136097ceSjb145095 				 * just update the pointer so the characters
1077*136097ceSjb145095 				 * can be retrieved.
1078*136097ceSjb145095 				 */
1079*136097ceSjb145095 				RING_UPD(qcn_state, retcount);
1080*136097ceSjb145095 			}
1081*136097ceSjb145095 		} else {
1082*136097ceSjb145095 			switch (rv) {
1083*136097ceSjb145095 
1084*136097ceSjb145095 			case H_EWOULDBLOCK :
1085*136097ceSjb145095 				/*
1086*136097ceSjb145095 				 * hypervisor cannot handle the request.
1087*136097ceSjb145095 				 * Try again later.
1088*136097ceSjb145095 				 */
1089*136097ceSjb145095 				break;
1090*136097ceSjb145095 
1091*136097ceSjb145095 
1092*136097ceSjb145095 			case H_BREAK :
1093*136097ceSjb145095 				/*
1094*136097ceSjb145095 				 * on break, unless alternate break sequence is
1095*136097ceSjb145095 				 * enabled, enter the debugger
1096*136097ceSjb145095 				 */
1097*136097ceSjb145095 				if (abort_enable != KIOCABORTALTERNATE)
1098*136097ceSjb145095 					abort_sequence_enter((char *)NULL);
1099*136097ceSjb145095 				break;
1100*136097ceSjb145095 
1101*136097ceSjb145095 			case H_HUP :
1102*136097ceSjb145095 				qcn_state->qcn_hangup = 1;
1103*136097ceSjb145095 				break;
1104*136097ceSjb145095 
1105*136097ceSjb145095 			default :
1106*136097ceSjb145095 				break;
1107*136097ceSjb145095 			}
1108*136097ceSjb145095 		}
1109*136097ceSjb145095 	} while (rv == H_EOK);
1110*136097ceSjb145095 }
1111*136097ceSjb145095 
1112*136097ceSjb145095 static void
1113*136097ceSjb145095 qcn_receive_getchr(void)
1114*136097ceSjb145095 {
11157c478bd9Sstevel@tonic-gate 	int64_t rv;
11167c478bd9Sstevel@tonic-gate 	uint8_t	buf;
11177c478bd9Sstevel@tonic-gate 
1118*136097ceSjb145095 	do {
11197c478bd9Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
1120*136097ceSjb145095 		if (rv == H_EOK) {
11217c478bd9Sstevel@tonic-gate 			if (abort_enable == KIOCABORTALTERNATE) {
11227c478bd9Sstevel@tonic-gate 				if (abort_charseq_recognize(buf)) {
11237c478bd9Sstevel@tonic-gate 					abort_sequence_enter((char *)NULL);
11247c478bd9Sstevel@tonic-gate 				}
11257c478bd9Sstevel@tonic-gate 			}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 			/* put console input onto stream */
11287c478bd9Sstevel@tonic-gate 			if (RING_POK(qcn_state, 1)) {
11297c478bd9Sstevel@tonic-gate 				RING_PUT(qcn_state, buf);
11307c478bd9Sstevel@tonic-gate 			} else {
11317c478bd9Sstevel@tonic-gate 				qcn_state->qcn_rbuf_overflow++;
11327c478bd9Sstevel@tonic-gate 			}
1133*136097ceSjb145095 		} else {
1134*136097ceSjb145095 			if (rv == H_BREAK) {
1135*136097ceSjb145095 				if (abort_enable != KIOCABORTALTERNATE)
1136*136097ceSjb145095 					abort_sequence_enter((char *)NULL);
11377c478bd9Sstevel@tonic-gate 			}
11387c478bd9Sstevel@tonic-gate 
1139*136097ceSjb145095 			if (rv == H_HUP)  {
1140*136097ceSjb145095 				qcn_state->qcn_hangup = 1;
1141*136097ceSjb145095 			}
1142*136097ceSjb145095 			return;
1143*136097ceSjb145095 		}
1144*136097ceSjb145095 	} while (rv == H_EOK);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
11487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11497c478bd9Sstevel@tonic-gate static void
11507c478bd9Sstevel@tonic-gate qcn_poll_handler(void *unused)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11537c478bd9Sstevel@tonic-gate 	int64_t rv;
11547c478bd9Sstevel@tonic-gate 	uint8_t buf;
11557c478bd9Sstevel@tonic-gate 	int qcn_writeq_flush = 0;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/* LINTED: E_CONSTANT_CONDITION */
11587c478bd9Sstevel@tonic-gate 	while (1) {
11597c478bd9Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
11607c478bd9Sstevel@tonic-gate 		if (rv == H_BREAK) {
11617c478bd9Sstevel@tonic-gate 			if (abort_enable != KIOCABORTALTERNATE)
11627c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
11637c478bd9Sstevel@tonic-gate 		}
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 		if (rv == H_HUP)  {
11667c478bd9Sstevel@tonic-gate 			if (qcn_state->qcn_readq) {
11677c478bd9Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
11687c478bd9Sstevel@tonic-gate 				qcn_writeq_flush = 1;
11697c478bd9Sstevel@tonic-gate 			}
11707c478bd9Sstevel@tonic-gate 			goto out;
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		if (rv != H_EOK)
11747c478bd9Sstevel@tonic-gate 			goto out;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		if (abort_enable == KIOCABORTALTERNATE) {
11777c478bd9Sstevel@tonic-gate 			if (abort_charseq_recognize(buf)) {
11787c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
11797c478bd9Sstevel@tonic-gate 			}
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 		/* put console input onto stream */
11837c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
11847c478bd9Sstevel@tonic-gate 			if ((mp = allocb(1, BPRI_MED)) == NULL) {
11857c478bd9Sstevel@tonic-gate 				qcn_input_dropped++;
11867c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "qcn_intr: allocb"
11877c478bd9Sstevel@tonic-gate 				    "failed (console input dropped)");
11887c478bd9Sstevel@tonic-gate 				return;
11897c478bd9Sstevel@tonic-gate 			}
11907c478bd9Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = buf;
11917c478bd9Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate out:
11957c478bd9Sstevel@tonic-gate /*
11967c478bd9Sstevel@tonic-gate  * If there are pending transmits because hypervisor
11977c478bd9Sstevel@tonic-gate  * returned EWOULDBLOCK poke start now.
11987c478bd9Sstevel@tonic-gate  */
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12017c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL) {
12027c478bd9Sstevel@tonic-gate 		if (qcn_writeq_flush) {
12037c478bd9Sstevel@tonic-gate 			flushq(qcn_state->qcn_writeq, FLUSHDATA);
12047c478bd9Sstevel@tonic-gate 		} else {
12057c478bd9Sstevel@tonic-gate 			qcn_start();
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate #endif
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate  * Check for abort character sequence, copied from zs_async.c
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate static boolean_t
12187c478bd9Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	static int state = 0;
12217c478bd9Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (ch == sequence[state]) {
12247c478bd9Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
12257c478bd9Sstevel@tonic-gate 			state = 0;
12267c478bd9Sstevel@tonic-gate 			return (B_TRUE);
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	} else {
12297c478bd9Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 	return (B_FALSE);
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate static int
12367c478bd9Sstevel@tonic-gate qcn_rsrv(queue_t *q)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12417c478bd9Sstevel@tonic-gate 		return (0);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
12467c478bd9Sstevel@tonic-gate 		if (canputnext(q))
12477c478bd9Sstevel@tonic-gate 			putnext(q, mp);
12487c478bd9Sstevel@tonic-gate 		else if (mp->b_datap->db_type >= QPCTL)
12497c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	return (0);
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate /* ARGSUSED */
12587c478bd9Sstevel@tonic-gate static int
12597c478bd9Sstevel@tonic-gate qcn_wsrv(queue_t *q)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12627c478bd9Sstevel@tonic-gate 		return (0);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL)
12677c478bd9Sstevel@tonic-gate 		qcn_start();
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	return (0);
12727c478bd9Sstevel@tonic-gate }
1273