xref: /illumos-gate/usr/src/uts/sun4v/io/qcn.c (revision ec16669a)
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 /*
23*ec16669aSZach Kissel  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * sun4v console driver
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/termios.h>
367c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
377c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
387c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
397c478bd9Sstevel@tonic-gate #include <sys/stream.h>
407c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
427c478bd9Sstevel@tonic-gate #include <sys/promif.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
467c478bd9Sstevel@tonic-gate #include <sys/intr.h>
477c478bd9Sstevel@tonic-gate #include <sys/spl.h>
487c478bd9Sstevel@tonic-gate #include <sys/qcn.h>
497c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h>
50136097ceSjb #include <sys/hsvc.h>
51136097ceSjb #include <sys/machsystm.h>
521b83305cSjm #include <sys/consdev.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* dev_ops and cb_ops for device driver */
557c478bd9Sstevel@tonic-gate static int qcn_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
567c478bd9Sstevel@tonic-gate static int qcn_attach(dev_info_t *, ddi_attach_cmd_t);
577c478bd9Sstevel@tonic-gate static int qcn_detach(dev_info_t *, ddi_detach_cmd_t);
587c478bd9Sstevel@tonic-gate static int qcn_open(queue_t *, dev_t *, int, int, cred_t *);
597c478bd9Sstevel@tonic-gate static int qcn_close(queue_t *, int, cred_t *);
607c478bd9Sstevel@tonic-gate static int qcn_wput(queue_t *, mblk_t *);
617c478bd9Sstevel@tonic-gate static int qcn_wsrv(queue_t *);
627c478bd9Sstevel@tonic-gate static int qcn_rsrv(queue_t *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* other internal qcn routines */
657c478bd9Sstevel@tonic-gate static void qcn_ioctl(queue_t *, mblk_t *);
667c478bd9Sstevel@tonic-gate static void qcn_reioctl(void *);
677c478bd9Sstevel@tonic-gate static void qcn_ack(mblk_t *, mblk_t *, uint_t);
687c478bd9Sstevel@tonic-gate static void qcn_start(void);
69136097ceSjb static int qcn_transmit_write(queue_t *, mblk_t *);
70136097ceSjb static int qcn_transmit_putchr(queue_t *, mblk_t *);
71136097ceSjb static void qcn_receive_read(void);
72136097ceSjb static void qcn_receive_getchr(void);
737c478bd9Sstevel@tonic-gate static void qcn_flush(void);
747c478bd9Sstevel@tonic-gate static uint_t qcn_hi_intr(caddr_t arg);
757c478bd9Sstevel@tonic-gate static uint_t qcn_soft_intr(caddr_t arg1, caddr_t arg2);
767c478bd9Sstevel@tonic-gate 
771b83305cSjm /* functions required for polled io */
781b83305cSjm static boolean_t qcn_polledio_ischar(cons_polledio_arg_t arg);
791b83305cSjm static int qcn_polledio_getchar(cons_polledio_arg_t arg);
801b83305cSjm static void qcn_polledio_putchar(cons_polledio_arg_t arg, uchar_t c);
811b83305cSjm static void qcn_polledio_enter(cons_polledio_arg_t arg);
821b83305cSjm static void qcn_polledio_exit(cons_polledio_arg_t arg);
831b83305cSjm 
841b83305cSjm 
857c478bd9Sstevel@tonic-gate static boolean_t abort_charseq_recognize(uchar_t);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static qcn_t *qcn_state;
887c478bd9Sstevel@tonic-gate static uchar_t qcn_stopped = B_FALSE;
897c478bd9Sstevel@tonic-gate static int qcn_timeout_period = 20;	/* time out in seconds */
907c478bd9Sstevel@tonic-gate size_t qcn_input_dropped;	/* dropped input character counter */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
937c478bd9Sstevel@tonic-gate static void qcn_poll_handler(void *unused);
9454c6b703Sjb static cyc_time_t qcn_poll_time;
957c478bd9Sstevel@tonic-gate static cyc_handler_t qcn_poll_cychandler = {
967c478bd9Sstevel@tonic-gate 	qcn_poll_handler,
977c478bd9Sstevel@tonic-gate 	NULL,
987c478bd9Sstevel@tonic-gate 	CY_LOW_LEVEL		/* XXX need softint to make this high */
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate static cyclic_id_t qcn_poll_cycid = CYCLIC_NONE;
10154c6b703Sjb static uint64_t	qcn_poll_interval = 5;  /* milli sec */
1021ae08745Sheppo static uint64_t sb_interval = 0;
1031ae08745Sheppo uint_t qcn_force_polling = 0;
1047c478bd9Sstevel@tonic-gate #endif
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	QCN_MI_IDNUM		0xABCE
1077c478bd9Sstevel@tonic-gate #define	QCN_MI_HIWAT		8192
1087c478bd9Sstevel@tonic-gate #define	QCN_MI_LOWAT		128
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* streams structures */
1117c478bd9Sstevel@tonic-gate static struct module_info minfo = {
1127c478bd9Sstevel@tonic-gate 	QCN_MI_IDNUM,	/* mi_idnum		*/
1137c478bd9Sstevel@tonic-gate 	"qcn",		/* mi_idname		*/
1147c478bd9Sstevel@tonic-gate 	0,		/* mi_minpsz		*/
1157c478bd9Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz		*/
1167c478bd9Sstevel@tonic-gate 	QCN_MI_HIWAT,	/* mi_hiwat		*/
1177c478bd9Sstevel@tonic-gate 	QCN_MI_LOWAT	/* mi_lowat		*/
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static struct qinit rinit = {
1217c478bd9Sstevel@tonic-gate 	putq,		/* qi_putp		*/
1227c478bd9Sstevel@tonic-gate 	qcn_rsrv,	/* qi_srvp		*/
1237c478bd9Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1247c478bd9Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1257c478bd9Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1267c478bd9Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1277c478bd9Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static struct qinit winit = {
1317c478bd9Sstevel@tonic-gate 	qcn_wput,	/* qi_putp		*/
1327c478bd9Sstevel@tonic-gate 	qcn_wsrv,	/* qi_srvp		*/
1337c478bd9Sstevel@tonic-gate 	qcn_open,	/* qi_qopen		*/
1347c478bd9Sstevel@tonic-gate 	qcn_close,	/* qi_qclose		*/
1357c478bd9Sstevel@tonic-gate 	NULL,		/* qi_qadmin		*/
1367c478bd9Sstevel@tonic-gate 	&minfo,		/* qi_minfo		*/
1377c478bd9Sstevel@tonic-gate 	NULL		/* qi_mstat		*/
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static struct streamtab qcnstrinfo = {
1417c478bd9Sstevel@tonic-gate 	&rinit,
1427c478bd9Sstevel@tonic-gate 	&winit,
1437c478bd9Sstevel@tonic-gate 	NULL,
1447c478bd9Sstevel@tonic-gate 	NULL
1457c478bd9Sstevel@tonic-gate };
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* standard device driver structures */
1487c478bd9Sstevel@tonic-gate static struct cb_ops qcn_cb_ops = {
1497c478bd9Sstevel@tonic-gate 	nulldev,		/* open()		*/
1507c478bd9Sstevel@tonic-gate 	nulldev,		/* close()		*/
1517c478bd9Sstevel@tonic-gate 	nodev,			/* strategy()		*/
1527c478bd9Sstevel@tonic-gate 	nodev,			/* print()		*/
1537c478bd9Sstevel@tonic-gate 	nodev,			/* dump()		*/
1547c478bd9Sstevel@tonic-gate 	nodev,			/* read()		*/
1557c478bd9Sstevel@tonic-gate 	nodev,			/* write()		*/
1567c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl()		*/
1577c478bd9Sstevel@tonic-gate 	nodev,			/* devmap()		*/
1587c478bd9Sstevel@tonic-gate 	nodev,			/* mmap()		*/
1597c478bd9Sstevel@tonic-gate 	nodev,			/* segmap()		*/
1607c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll()		*/
1617c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op()		*/
1627c478bd9Sstevel@tonic-gate 	&qcnstrinfo,		/* cb_str		*/
1637c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* cb_flag		*/
1647c478bd9Sstevel@tonic-gate };
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static struct dev_ops qcn_ops = {
1677c478bd9Sstevel@tonic-gate 	DEVO_REV,
1687c478bd9Sstevel@tonic-gate 	0,			/* refcnt		*/
1697c478bd9Sstevel@tonic-gate 	qcn_getinfo,		/* getinfo()		*/
1707c478bd9Sstevel@tonic-gate 	nulldev,		/* identify()		*/
1717c478bd9Sstevel@tonic-gate 	nulldev,		/* probe()		*/
1727c478bd9Sstevel@tonic-gate 	qcn_attach,		/* attach()		*/
1737c478bd9Sstevel@tonic-gate 	qcn_detach,		/* detach()		*/
1747c478bd9Sstevel@tonic-gate 	nodev,			/* reset()		*/
1757c478bd9Sstevel@tonic-gate 	&qcn_cb_ops,		/* cb_ops		*/
1767c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* bus_ops		*/
17719397407SSherry Moore 	NULL,			/* power()		*/
17819397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce()		*/
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1827c478bd9Sstevel@tonic-gate 	&mod_driverops,
18319397407SSherry Moore 	"sun4v console driver",
1847c478bd9Sstevel@tonic-gate 	&qcn_ops
1857c478bd9Sstevel@tonic-gate };
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1887c478bd9Sstevel@tonic-gate 	MODREV_1,
1897c478bd9Sstevel@tonic-gate 	(void*)&modldrv,
1907c478bd9Sstevel@tonic-gate 	NULL
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /* driver configuration routines */
1947c478bd9Sstevel@tonic-gate int
_init(void)1957c478bd9Sstevel@tonic-gate _init(void)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	int error;
198136097ceSjb 	uint64_t	major, minor;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	qcn_state = kmem_zalloc(sizeof (qcn_t), KM_SLEEP);
201136097ceSjb 	qcn_state->qcn_ring = contig_mem_alloc(RINGSIZE);
202136097ceSjb 	if (qcn_state->qcn_ring == NULL)
203136097ceSjb 		cmn_err(CE_PANIC, "console ring allocation failed");
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
206136097ceSjb 	if (error != 0) {
207136097ceSjb 		contig_mem_free(qcn_state->qcn_ring, RINGSIZE);
2087c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state, sizeof (qcn_t));
209136097ceSjb 		return (error);
210136097ceSjb 	}
211136097ceSjb 	/*
212136097ceSjb 	 * check minor number to see if CONS_WRITE is supported
213136097ceSjb 	 * if so, set up real address of the buffers for hv calls.
214136097ceSjb 	 */
2157c478bd9Sstevel@tonic-gate 
216136097ceSjb 	if (((hsvc_version(HSVC_GROUP_CORE, &major, &minor) == 0) &&
21719397407SSherry Moore 	    (major == QCN_API_MAJOR) && (minor >= QCN_API_MINOR))) {
218136097ceSjb 		qcn_state->cons_write_buffer =
21919397407SSherry Moore 		    contig_mem_alloc(CONS_WR_BUF_SIZE);
220136097ceSjb 		if (qcn_state->cons_write_buffer != NULL) {
221136097ceSjb 			qcn_state->cons_write_buf_ra =
22219397407SSherry Moore 			    va_to_pa(qcn_state->cons_write_buffer);
223136097ceSjb 			qcn_state->cons_transmit = qcn_transmit_write;
224136097ceSjb 			qcn_state->cons_receive = qcn_receive_read;
225136097ceSjb 			qcn_state->cons_read_buf_ra =
22619397407SSherry Moore 			    va_to_pa((char *)RING_ADDR(qcn_state));
227136097ceSjb 		}
228136097ceSjb 	}
229136097ceSjb 	if (qcn_state->cons_transmit == NULL) {
230136097ceSjb 		qcn_state->cons_transmit = qcn_transmit_putchr;
231136097ceSjb 		qcn_state->cons_receive = qcn_receive_getchr;
232136097ceSjb 	}
233136097ceSjb 	return (0);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate int
_fini(void)2377c478bd9Sstevel@tonic-gate _fini(void)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	/* can't remove console driver */
2407c478bd9Sstevel@tonic-gate 	return (EBUSY);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2447c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int
qcn_add_intrs(void)2507c478bd9Sstevel@tonic-gate qcn_add_intrs(void)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	dev_info_t	*devinfo = qcn_state->qcn_dip;
2537c478bd9Sstevel@tonic-gate 	int		actual, count = 0;
2547c478bd9Sstevel@tonic-gate 	int 		x, y, rc, inum = 0;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* get number of interrupts */
2587c478bd9Sstevel@tonic-gate 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
2597c478bd9Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (count == 0)) {
2607c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* Allocate an array of interrupt handles */
2647c478bd9Sstevel@tonic-gate 	qcn_state->qcn_intr_size = count * sizeof (ddi_intr_handle_t);
2657c478bd9Sstevel@tonic-gate 	qcn_state->qcn_htable = kmem_zalloc(qcn_state->qcn_intr_size, KM_SLEEP);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* call ddi_intr_alloc() */
2687c478bd9Sstevel@tonic-gate 	rc = ddi_intr_alloc(devinfo, qcn_state->qcn_htable,
2697c478bd9Sstevel@tonic-gate 	    DDI_INTR_TYPE_FIXED, inum, count, &actual,
2707c478bd9Sstevel@tonic-gate 	    DDI_INTR_ALLOC_STRICT);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
2737c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (actual < count) {
2787c478bd9Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2797c478bd9Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	qcn_state->qcn_intr_cnt = actual;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* Get intr priority */
2897c478bd9Sstevel@tonic-gate 	if (ddi_intr_get_pri(qcn_state->qcn_htable[0],
2907c478bd9Sstevel@tonic-gate 	    &qcn_state->qcn_intr_pri) != DDI_SUCCESS) {
2917c478bd9Sstevel@tonic-gate 		for (x = 0; x < actual; x++) {
2927c478bd9Sstevel@tonic-gate 			(void) ddi_intr_free(qcn_state->qcn_htable[x]);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
2967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* Call ddi_intr_add_handler() */
3007c478bd9Sstevel@tonic-gate 	for (x = 0; x < actual; x++) {
3017c478bd9Sstevel@tonic-gate 		if (ddi_intr_add_handler(qcn_state->qcn_htable[x],
3027c478bd9Sstevel@tonic-gate 		    (ddi_intr_handler_t *)qcn_hi_intr,
3037c478bd9Sstevel@tonic-gate 		    (caddr_t)qcn_state, NULL) != DDI_SUCCESS) {
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			for (y = 0; y < x; y++) {
3067c478bd9Sstevel@tonic-gate 				(void) ddi_intr_remove_handler(
3077c478bd9Sstevel@tonic-gate 				    qcn_state->qcn_htable[y]);
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			for (y = 0; y < actual; y++) {
3117c478bd9Sstevel@tonic-gate 				(void) ddi_intr_free(qcn_state->qcn_htable[y]);
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			kmem_free(qcn_state->qcn_htable,
3157c478bd9Sstevel@tonic-gate 			    qcn_state->qcn_intr_size);
3167c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static void
qcn_remove_intrs(void)3247c478bd9Sstevel@tonic-gate qcn_remove_intrs(void)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	int x;
3277c478bd9Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3287c478bd9Sstevel@tonic-gate 		(void) ddi_intr_disable(qcn_state->qcn_htable[x]);
3297c478bd9Sstevel@tonic-gate 		(void) ddi_intr_remove_handler(qcn_state->qcn_htable[x]);
3307c478bd9Sstevel@tonic-gate 		(void) ddi_intr_free(qcn_state->qcn_htable[x]);
3317c478bd9Sstevel@tonic-gate 	}
332ea841a36Sarao 	kmem_free(qcn_state->qcn_htable, qcn_state->qcn_intr_size);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static void
qcn_intr_enable(void)3367c478bd9Sstevel@tonic-gate qcn_intr_enable(void)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	int x;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	for (x = 0; x < qcn_state->qcn_intr_cnt; x++) {
3417c478bd9Sstevel@tonic-gate 		(void) ddi_intr_enable(qcn_state->qcn_htable[x]);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * qcn_attach is called at startup time.
3477c478bd9Sstevel@tonic-gate  * There is only once instance of this driver.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate static int
qcn_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3507c478bd9Sstevel@tonic-gate qcn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	extern int ddi_create_internal_pathname(dev_info_t *, char *,
3537c478bd9Sstevel@tonic-gate 	    int, minor_t);
3547c478bd9Sstevel@tonic-gate 	uint_t soft_prip;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
3577c478bd9Sstevel@tonic-gate 	char *binding_name;
3587c478bd9Sstevel@tonic-gate #endif
3597c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3607c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (ddi_create_internal_pathname(dip, "qcn",
3637c478bd9Sstevel@tonic-gate 	    S_IFCHR, 0) != DDI_SUCCESS)
3647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	qcn_state->qcn_soft_pend = 0;
3677c478bd9Sstevel@tonic-gate 	qcn_state->qcn_hangup = 0;
3687c478bd9Sstevel@tonic-gate 	qcn_state->qcn_rbuf_overflow = 0;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/* prepare some data structures in soft state */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	qcn_state->qcn_dip = dip;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	qcn_state->qcn_polling = 0;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * This test is for the sole purposes of allowing
3797c478bd9Sstevel@tonic-gate 	 * the console to work on older firmware releases.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	binding_name = ddi_binding_name(qcn_state->qcn_dip);
3821ae08745Sheppo 	if ((strcmp(binding_name, "qcn") == 0) ||
3831ae08745Sheppo 	    (qcn_force_polling))
3847c478bd9Sstevel@tonic-gate 		qcn_state->qcn_polling = 1;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
3877c478bd9Sstevel@tonic-gate 		qcn_poll_time.cyt_when = 0ull;
3887c478bd9Sstevel@tonic-gate 		qcn_poll_time.cyt_interval =
3897c478bd9Sstevel@tonic-gate 		    qcn_poll_interval * 1000ull * 1000ull;
3907c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
3917c478bd9Sstevel@tonic-gate 		qcn_poll_cycid = cyclic_add(&qcn_poll_cychandler,
3927c478bd9Sstevel@tonic-gate 		    &qcn_poll_time);
3937c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate #endif
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
3987c478bd9Sstevel@tonic-gate 		if (qcn_add_intrs() != DDI_SUCCESS) {
3997c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_intr failed\n");
4007c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		if (ddi_intr_add_softint(dip, &qcn_state->qcn_softint_hdl,
4037c478bd9Sstevel@tonic-gate 		    DDI_INTR_SOFTPRI_MAX, qcn_soft_intr,
4047c478bd9Sstevel@tonic-gate 		    (caddr_t)qcn_state) != DDI_SUCCESS) {
4057c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: add_soft_intr failed\n");
4067c478bd9Sstevel@tonic-gate 			qcn_remove_intrs();
4077c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		if (ddi_intr_get_softint_pri(qcn_state->qcn_softint_hdl,
4107c478bd9Sstevel@tonic-gate 		    &soft_prip) != DDI_SUCCESS) {
4117c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "qcn_attach: softint_pri failed\n");
412ea841a36Sarao 			(void) ddi_intr_remove_softint(
413ea841a36Sarao 			    qcn_state->qcn_softint_hdl);
4147c478bd9Sstevel@tonic-gate 			qcn_remove_intrs();
4157c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 
41854c6b703Sjb 	mutex_init(&qcn_state->qcn_hi_lock, NULL, MUTEX_DRIVER,
41954c6b703Sjb 	    (void *)(uintptr_t)(qcn_state->qcn_intr_pri));
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	mutex_init(&qcn_state->qcn_lock, NULL, MUTEX_DRIVER, NULL);
4237c478bd9Sstevel@tonic-gate 
4241b83305cSjm 	/*
4251b83305cSjm 	 * Fill in the polled I/O structure.
4261b83305cSjm 	 */
4271b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
4281b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_argument =
4291b83305cSjm 	    (cons_polledio_arg_t)qcn_state;
4301b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_putchar = qcn_polledio_putchar;
4311b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_getchar = qcn_polledio_getchar;
4321b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_ischar = qcn_polledio_ischar;
4331b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_enter = qcn_polledio_enter;
4341b83305cSjm 	qcn_state->qcn_polledio.cons_polledio_exit = qcn_polledio_exit;
4351b83305cSjm 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 *  Enable  interrupts
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	if (!qcn_state->qcn_polling) {
4407c478bd9Sstevel@tonic-gate 		qcn_intr_enable();
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4437c478bd9Sstevel@tonic-gate 	prom_printf("qcn_attach(): qcn driver attached\n");
4447c478bd9Sstevel@tonic-gate #endif
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /* ARGSUSED */
4517c478bd9Sstevel@tonic-gate static int
qcn_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4527c478bd9Sstevel@tonic-gate qcn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4607c478bd9Sstevel@tonic-gate 	prom_printf("qcn_detach(): QCN driver detached\n");
4617c478bd9Sstevel@tonic-gate #endif
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
4647c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_polling) {
4657c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4667c478bd9Sstevel@tonic-gate 		if (qcn_poll_cycid != CYCLIC_NONE)
4677c478bd9Sstevel@tonic-gate 			cyclic_remove(qcn_poll_cycid);
4687c478bd9Sstevel@tonic-gate 		qcn_poll_cycid = CYCLIC_NONE;
4697c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate #endif
4727c478bd9Sstevel@tonic-gate 
47354c6b703Sjb 	if (!qcn_state->qcn_polling)
4747c478bd9Sstevel@tonic-gate 		qcn_remove_intrs();
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /* ARGSUSED */
4807c478bd9Sstevel@tonic-gate static int
qcn_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4817c478bd9Sstevel@tonic-gate qcn_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
4847c478bd9Sstevel@tonic-gate 	int instance = 0;
4857c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4867c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4877c478bd9Sstevel@tonic-gate 		if (qcn_state) {
4887c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4897c478bd9Sstevel@tonic-gate 			prom_printf("qcn_getinfo(): devt2dip %lx\n", arg);
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate 			*result = (void *)qcn_state->qcn_dip;
4927c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 		break;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4977c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
4987c478bd9Sstevel@tonic-gate 		prom_printf("qcn_getinfo(): devt2instance %lx\n", arg);
4997c478bd9Sstevel@tonic-gate #endif
5007c478bd9Sstevel@tonic-gate 		if (getminor((dev_t)arg) == 0) {
5010bd5614cSiskreen 			*result = (void *)(uintptr_t)instance;
5027c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		break;
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	return (error);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /* streams open & close */
5117c478bd9Sstevel@tonic-gate /* ARGSUSED */
5127c478bd9Sstevel@tonic-gate static int
qcn_open(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * credp)5137c478bd9Sstevel@tonic-gate qcn_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	tty_common_t *tty;
5167c478bd9Sstevel@tonic-gate 	int unit = getminor(*devp);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5197c478bd9Sstevel@tonic-gate 	prom_printf("qcn_open(): minor %x\n", unit);
5207c478bd9Sstevel@tonic-gate #endif
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (unit != 0)
5237c478bd9Sstevel@tonic-gate 		return (ENXIO);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* stream already open */
5267c478bd9Sstevel@tonic-gate 	if (q->q_ptr != NULL)
5277c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (!qcn_state) {
5307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "qcn_open: console was not configured by "
5317c478bd9Sstevel@tonic-gate 		    "autoconfig\n");
5327c478bd9Sstevel@tonic-gate 		return (ENXIO);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
5367c478bd9Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	tty->t_readq = q;
5397c478bd9Sstevel@tonic-gate 	tty->t_writeq = WR(q);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/* Link the RD and WR Q's */
5427c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = (caddr_t)qcn_state;
5437c478bd9Sstevel@tonic-gate 	qcn_state->qcn_readq = RD(q);
5447c478bd9Sstevel@tonic-gate 	qcn_state->qcn_writeq = WR(q);
5457c478bd9Sstevel@tonic-gate 	qprocson(q);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5507c478bd9Sstevel@tonic-gate 	prom_printf("qcn_open: opened as dev %lx\n", *devp);
5517c478bd9Sstevel@tonic-gate #endif
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /* ARGSUSED */
5577c478bd9Sstevel@tonic-gate static int
qcn_close(queue_t * q,int flag,cred_t * credp)5587c478bd9Sstevel@tonic-gate qcn_close(queue_t *q, int flag, cred_t *credp)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_wbufcid != 0) {
5647c478bd9Sstevel@tonic-gate 		unbufcall(qcn_state->qcn_wbufcid);
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	ttycommon_close(&qcn_state->qcn_tty);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	qprocsoff(q);
5697c478bd9Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
5707c478bd9Sstevel@tonic-gate 	qcn_state->qcn_readq = NULL;
5717c478bd9Sstevel@tonic-gate 	qcn_state->qcn_writeq = NULL;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Put procedure for write queue.
5787c478bd9Sstevel@tonic-gate  * Respond to M_IOCTL, M_DATA and M_FLUSH messages here;
5797c478bd9Sstevel@tonic-gate  * It put's the data onto internal qcn_output_q.
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate static int
qcn_wput(queue_t * q,mblk_t * mp)5827c478bd9Sstevel@tonic-gate qcn_wput(queue_t *q, mblk_t *mp)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5867c478bd9Sstevel@tonic-gate 	struct iocblk *iocp;
5877c478bd9Sstevel@tonic-gate 	int i;
5887c478bd9Sstevel@tonic-gate #endif
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	ASSERT(qcn_state == q->q_ptr);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (!mp->b_datap) {
5937c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "qcn_wput: null datap");
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
5977c478bd9Sstevel@tonic-gate 	prom_printf("qcn_wput(): QCN wput q=%X mp=%X rd=%X wr=%X type=%X\n",
59819397407SSherry Moore 	    q, mp, mp->b_rptr, mp->b_wptr, mp->b_datap->db_type);
5997c478bd9Sstevel@tonic-gate #endif
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
6047c478bd9Sstevel@tonic-gate 	case M_IOCTL:
6057c478bd9Sstevel@tonic-gate 	case M_CTL:
6067c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
6077c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
6087c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): M_IOCTL cmd=%X TIOC=%X\n",
6097c478bd9Sstevel@tonic-gate 		    iocp->ioc_cmd, TIOC);
6107c478bd9Sstevel@tonic-gate #endif
6117c478bd9Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
6127c478bd9Sstevel@tonic-gate 		case TCSETSW:
6137c478bd9Sstevel@tonic-gate 		case TCSETSF:
6147c478bd9Sstevel@tonic-gate 		case TCSETAW:
6157c478bd9Sstevel@tonic-gate 		case TCSETAF:
6167c478bd9Sstevel@tonic-gate 		case TCSBRK:
6177c478bd9Sstevel@tonic-gate 			/*
6187c478bd9Sstevel@tonic-gate 			 * The change do not take effect until all
6197c478bd9Sstevel@tonic-gate 			 * output queued before them is drained.
6207c478bd9Sstevel@tonic-gate 			 * Put this message on the queue, so that
6217c478bd9Sstevel@tonic-gate 			 * "qcn_start" will see it when it's done
6227c478bd9Sstevel@tonic-gate 			 * with the output before it. Poke the start
6237c478bd9Sstevel@tonic-gate 			 * routine, just in case.
6247c478bd9Sstevel@tonic-gate 			 */
6257c478bd9Sstevel@tonic-gate 			(void) putq(q, mp);
6267c478bd9Sstevel@tonic-gate 			qcn_start();
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		default:
62909921062Sjb 			mutex_exit(&qcn_state->qcn_lock);
6307c478bd9Sstevel@tonic-gate 			qcn_ioctl(q, mp);
63109921062Sjb 			mutex_enter(&qcn_state->qcn_lock);
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	case M_FLUSH:
6367c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
6377c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
6387c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
6417c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
6427c478bd9Sstevel@tonic-gate 			qreply(q, mp);
6437c478bd9Sstevel@tonic-gate 		} else {
6447c478bd9Sstevel@tonic-gate 			freemsg(mp);
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		break;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	case M_STOP:
6497c478bd9Sstevel@tonic-gate 		qcn_stopped = B_TRUE;
6507c478bd9Sstevel@tonic-gate 		freemsg(mp);
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	case M_START:
6547c478bd9Sstevel@tonic-gate 		qcn_stopped = B_FALSE;
6557c478bd9Sstevel@tonic-gate 		freemsg(mp);
6567c478bd9Sstevel@tonic-gate 		qenable(q);	/* Start up delayed messages */
6577c478bd9Sstevel@tonic-gate 		break;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	case M_DATA:
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Queue the message up to be transmitted,
6627c478bd9Sstevel@tonic-gate 		 * and poke the start routine.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
6657c478bd9Sstevel@tonic-gate 		if (mp->b_rptr < mp->b_wptr) {
6667c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): DATA q=%X mp=%X rd=%X wr=%X\n",
66719397407SSherry Moore 		    q, mp, mp->b_rptr, mp->b_wptr);
6687c478bd9Sstevel@tonic-gate 		prom_printf("qcn_wput(): [");
6697c478bd9Sstevel@tonic-gate 		for (i = 0; i < mp->b_wptr-mp->b_rptr; i++) {
6707c478bd9Sstevel@tonic-gate 			prom_printf("%c", *(mp->b_rptr+i));
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 		prom_printf("]\n");
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate #endif /* QCN_DEBUG */
6757c478bd9Sstevel@tonic-gate 		(void) putq(q, mp);
6767c478bd9Sstevel@tonic-gate 		qcn_start();
6777c478bd9Sstevel@tonic-gate 		break;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	default:
6807c478bd9Sstevel@tonic-gate 		freemsg(mp);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
68409921062Sjb 
6857c478bd9Sstevel@tonic-gate 	return (0);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * Process an "ioctl" message sent down to us.
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate static void
qcn_ioctl(queue_t * q,mblk_t * mp)6927c478bd9Sstevel@tonic-gate qcn_ioctl(queue_t *q, mblk_t *mp)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
6957c478bd9Sstevel@tonic-gate 	tty_common_t	*tty;
6967c478bd9Sstevel@tonic-gate 	mblk_t		*datamp;
6977c478bd9Sstevel@tonic-gate 	int		data_size;
6987c478bd9Sstevel@tonic-gate 	int		error = 0;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
7017c478bd9Sstevel@tonic-gate 	prom_printf("qcn_ioctl(): q=%X mp=%X\n", q, mp);
7027c478bd9Sstevel@tonic-gate #endif
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7051b83305cSjm 
7067c478bd9Sstevel@tonic-gate 	tty = &(qcn_state->qcn_tty);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (tty->t_iocpending != NULL) {
7097c478bd9Sstevel@tonic-gate 		freemsg(tty->t_iocpending);
7107c478bd9Sstevel@tonic-gate 		tty->t_iocpending = NULL;
7117c478bd9Sstevel@tonic-gate 	}
7121b83305cSjm 
7131b83305cSjm 	/*
7141b83305cSjm 	 * Handle the POLLEDIO ioctls now because ttycommon_ioctl
7151b83305cSjm 	 * (below) frees up the message block (mp->b_cont) which
7161b83305cSjm 	 * contains the pointer used to pass back results.
7171b83305cSjm 	 */
7181b83305cSjm 	switch (iocp->ioc_cmd) {
7191b83305cSjm 	case CONSOPENPOLLEDIO:
7201b83305cSjm 		error = miocpullup(mp, sizeof (struct cons_polledio *));
7211b83305cSjm 		if (error != 0)
7221b83305cSjm 			break;
7231b83305cSjm 
7241b83305cSjm 		*(struct cons_polledio **)mp->b_cont->b_rptr =
7251b83305cSjm 		    &qcn_state->qcn_polledio;
7261b83305cSjm 
7271b83305cSjm 		mp->b_datap->db_type = M_IOCACK;
7281b83305cSjm 		break;
7291b83305cSjm 
7301b83305cSjm 	case CONSCLOSEPOLLEDIO:
7311b83305cSjm 		mp->b_datap->db_type = M_IOCACK;
7321b83305cSjm 		iocp->ioc_error = 0;
7331b83305cSjm 		iocp->ioc_rval = 0;
7341b83305cSjm 		break;
7351b83305cSjm 
7361b83305cSjm 	default:
7371b83305cSjm 		data_size = ttycommon_ioctl(tty, q, mp, &error);
7381b83305cSjm 		if (data_size != 0) {
7391b83305cSjm 			if (qcn_state->qcn_wbufcid)
7401b83305cSjm 				unbufcall(qcn_state->qcn_wbufcid);
7411b83305cSjm 			/* call qcn_reioctl() */
7421b83305cSjm 			qcn_state->qcn_wbufcid =
7431b83305cSjm 			    bufcall(data_size, BPRI_HI, qcn_reioctl, qcn_state);
7441b83305cSjm 			return;
7451b83305cSjm 		}
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
74809921062Sjb 	mutex_enter(&qcn_state->qcn_lock);
74909921062Sjb 
7507c478bd9Sstevel@tonic-gate 	if (error < 0) {
7517c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * "ttycommon_ioctl" didn't do anything; we process it here.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		error = 0;
7567c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
7577c478bd9Sstevel@tonic-gate 		case TCSBRK:
7587c478bd9Sstevel@tonic-gate 		case TIOCSBRK:
7597c478bd9Sstevel@tonic-gate 		case TIOCCBRK:
7607c478bd9Sstevel@tonic-gate 		case TIOCMSET:
7617c478bd9Sstevel@tonic-gate 		case TIOCMBIS:
7627c478bd9Sstevel@tonic-gate 		case TIOCMBIC:
7637c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7647c478bd9Sstevel@tonic-gate 				qcn_ack(mp, NULL, 0);
7657c478bd9Sstevel@tonic-gate 			else
7667c478bd9Sstevel@tonic-gate 				mcopyin(mp, NULL, sizeof (int), NULL);
7677c478bd9Sstevel@tonic-gate 			break;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		case TIOCMGET:
7707c478bd9Sstevel@tonic-gate 			datamp = allocb(sizeof (int), BPRI_MED);
7717c478bd9Sstevel@tonic-gate 			if (datamp == NULL) {
7727c478bd9Sstevel@tonic-gate 				error = EAGAIN;
7737c478bd9Sstevel@tonic-gate 				break;
7747c478bd9Sstevel@tonic-gate 			}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 			*(int *)datamp->b_rptr = 0;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 			if (iocp->ioc_count != TRANSPARENT)
7797c478bd9Sstevel@tonic-gate 				qcn_ack(mp, datamp, sizeof (int));
7807c478bd9Sstevel@tonic-gate 			else
7817c478bd9Sstevel@tonic-gate 				mcopyout(mp, NULL, sizeof (int), NULL, datamp);
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		default:
7857c478bd9Sstevel@tonic-gate 			error = EINVAL;
7867c478bd9Sstevel@tonic-gate 			break;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	if (error != 0) {
7907c478bd9Sstevel@tonic-gate 		iocp->ioc_count = 0;
7917c478bd9Sstevel@tonic-gate 		iocp->ioc_error = error;
7927c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
7937c478bd9Sstevel@tonic-gate 	}
79409921062Sjb 	mutex_exit(&qcn_state->qcn_lock);
7957c478bd9Sstevel@tonic-gate 	qreply(q, mp);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate static void
qcn_reioctl(void * unit)7997c478bd9Sstevel@tonic-gate qcn_reioctl(void *unit)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	queue_t		*q;
8027c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
8037c478bd9Sstevel@tonic-gate 	qcn_t		*qcnp = (qcn_t *)unit;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (!qcnp->qcn_wbufcid)
8067c478bd9Sstevel@tonic-gate 		return;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	qcnp->qcn_wbufcid = 0;
8097c478bd9Sstevel@tonic-gate 	if ((q = qcnp->qcn_tty.t_writeq) == NULL)
8107c478bd9Sstevel@tonic-gate 		return;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if ((mp = qcnp->qcn_tty.t_iocpending) == NULL)
8137c478bd9Sstevel@tonic-gate 		return;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	qcnp->qcn_tty.t_iocpending = NULL;
8167c478bd9Sstevel@tonic-gate 	qcn_ioctl(q, mp);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate static void
qcn_ack(mblk_t * mp,mblk_t * dp,uint_t size)8207c478bd9Sstevel@tonic-gate qcn_ack(mblk_t *mp, mblk_t *dp, uint_t size)
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	struct iocblk  *iocp = (struct iocblk *)mp->b_rptr;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_IOCACK;
8257c478bd9Sstevel@tonic-gate 	iocp->ioc_count = size;
8267c478bd9Sstevel@tonic-gate 	iocp->ioc_error = 0;
8277c478bd9Sstevel@tonic-gate 	iocp->ioc_rval = 0;
8287c478bd9Sstevel@tonic-gate 	if (mp->b_cont != NULL)
8297c478bd9Sstevel@tonic-gate 		freeb(mp->b_cont);
8307c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
8317c478bd9Sstevel@tonic-gate 		mp->b_cont = dp;
8327c478bd9Sstevel@tonic-gate 		dp->b_wptr += size;
8337c478bd9Sstevel@tonic-gate 	} else
8347c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate static void
qcn_start(void)8387c478bd9Sstevel@tonic-gate qcn_start(void)
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	queue_t *q;
8427c478bd9Sstevel@tonic-gate 	mblk_t *mp;
8437c478bd9Sstevel@tonic-gate 	int rv;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/*
8487c478bd9Sstevel@tonic-gate 	 * read stream queue and remove data from the queue and
8497c478bd9Sstevel@tonic-gate 	 * transmit them if possible
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
8527c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
8537c478bd9Sstevel@tonic-gate 	while (mp = getq(q)) {
8547c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_IOCTL) {
8557c478bd9Sstevel@tonic-gate 			/*
8567c478bd9Sstevel@tonic-gate 			 * These are those IOCTLs queued up
8577c478bd9Sstevel@tonic-gate 			 * do it now
8587c478bd9Sstevel@tonic-gate 			 */
85909921062Sjb 			mutex_exit(&qcn_state->qcn_lock);
8607c478bd9Sstevel@tonic-gate 			qcn_ioctl(q, mp);
86109921062Sjb 			mutex_enter(&qcn_state->qcn_lock);
8627c478bd9Sstevel@tonic-gate 			continue;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * M_DATA
8667c478bd9Sstevel@tonic-gate 		 */
867136097ceSjb 		rv = qcn_state->cons_transmit(q, mp);
8687c478bd9Sstevel@tonic-gate 		if (rv == EBUSY || rv == EAGAIN)
8697c478bd9Sstevel@tonic-gate 			return;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate static int
qcn_transmit_write(queue_t * q,mblk_t * mp)874136097ceSjb qcn_transmit_write(queue_t *q, mblk_t *mp)
8757c478bd9Sstevel@tonic-gate {
876136097ceSjb 	mblk_t		*bp;
877136097ceSjb 	size_t		len;
878136097ceSjb 	uint64_t	i;
879136097ceSjb 	uint64_t	retval = 0;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate #ifdef QCN_DEBUG
882136097ceSjb 	prom_printf("qcn_transmit_write(): q=%X mp=%X\n", q, mp);
8837c478bd9Sstevel@tonic-gate #endif
884136097ceSjb 
885136097ceSjb 	while (mp) {
8867c478bd9Sstevel@tonic-gate 		bp = mp;
8877c478bd9Sstevel@tonic-gate 		len = bp->b_wptr - bp->b_rptr;
888136097ceSjb 		/*
889136097ceSjb 		 * Use the console write call to send a block of characters to
890136097ceSjb 		 * the console.
891136097ceSjb 		 */
892136097ceSjb 		i = (len > CONS_WR_BUF_SIZE) ? CONS_WR_BUF_SIZE : len;
893136097ceSjb 		bcopy(bp->b_rptr, qcn_state->cons_write_buffer, i);
894136097ceSjb 		retval = hv_cnwrite(qcn_state->cons_write_buf_ra, i, &i);
895136097ceSjb 
896136097ceSjb 		if (retval == H_EOK) {
897136097ceSjb 			len -= i;
898136097ceSjb 			bp->b_rptr += i;
899136097ceSjb 			/*
900136097ceSjb 			 * if we have finished with this buf, free
901136097ceSjb 			 * and get the next buf if present.
902136097ceSjb 			 */
903136097ceSjb 			if (len == 0) {
904136097ceSjb 				mp = bp->b_cont;
905136097ceSjb 				freeb(bp);
906136097ceSjb 			}
907136097ceSjb 		} else {
908136097ceSjb 			(void) putbq(q, mp);
9097c478bd9Sstevel@tonic-gate 
910136097ceSjb 			switch (retval) {
911136097ceSjb 
912136097ceSjb 			case H_EWOULDBLOCK :
913136097ceSjb 				/*
914136097ceSjb 				 * hypervisor cannot process the request -
915136097ceSjb 				 * channel busy.  Try again later.
916136097ceSjb 				 */
917136097ceSjb 				return (EAGAIN);
918136097ceSjb 
919136097ceSjb 			case H_EIO :
920136097ceSjb 				return (EIO);
921136097ceSjb 			default :
922136097ceSjb 				return (ENXIO);
923136097ceSjb 			}
924136097ceSjb 		}
925136097ceSjb 	}
926136097ceSjb 	return (0);
927136097ceSjb }
928136097ceSjb 
929136097ceSjb static int
qcn_transmit_putchr(queue_t * q,mblk_t * mp)930136097ceSjb qcn_transmit_putchr(queue_t *q, mblk_t *mp)
931136097ceSjb {
932136097ceSjb 	caddr_t		buf;
933136097ceSjb 	mblk_t		*bp;
934136097ceSjb 	size_t		len;
935136097ceSjb 	uint64_t	i;
936136097ceSjb 
937136097ceSjb #ifdef QCN_DEBUG
938136097ceSjb 	prom_printf("qcn_transmit_putchr(): q=%X mp=%X\n", q, mp);
939136097ceSjb #endif
940136097ceSjb 	while (mp) {
941136097ceSjb 		bp = mp;
942136097ceSjb 		len = bp->b_wptr - bp->b_rptr;
943136097ceSjb 		buf = (caddr_t)bp->b_rptr;
9447c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
94554c6b703Sjb 			if (hv_cnputchar(buf[i]) == H_EWOULDBLOCK)
9464bac2208Snarayan 				break;
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 		if (i != len) {
9497c478bd9Sstevel@tonic-gate 			bp->b_rptr += i;
9507c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
9517c478bd9Sstevel@tonic-gate 			return (EAGAIN);
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 		mp = bp->b_cont;
9547c478bd9Sstevel@tonic-gate 		freeb(bp);
955136097ceSjb 	}
9567c478bd9Sstevel@tonic-gate 	return (0);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate  * called when SC first establishes console connection
9617c478bd9Sstevel@tonic-gate  * drop all the data on the output queue
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate static void
qcn_flush(void)9647c478bd9Sstevel@tonic-gate qcn_flush(void)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate 	queue_t *q;
9677c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&qcn_state->qcn_lock));
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	q = qcn_state->qcn_writeq;
9727c478bd9Sstevel@tonic-gate 
9730bd5614cSiskreen 	prom_printf("qcn_flush(): WARNING console output is dropped time=%lx\n",
9747c478bd9Sstevel@tonic-gate 	    gethrestime_sec());
9757c478bd9Sstevel@tonic-gate 	while (mp = getq(q))
9767c478bd9Sstevel@tonic-gate 		freemsg(mp);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate static void
qcn_trigger_softint(void)9807c478bd9Sstevel@tonic-gate qcn_trigger_softint(void)
9817c478bd9Sstevel@tonic-gate {
982136097ceSjb 	/*
983136097ceSjb 	 * if we are not currently servicing a software interrupt
984136097ceSjb 	 * (qcn_soft_pend == 0), trigger the service routine to run.
985136097ceSjb 	 */
98632179a93Sjb 	if (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_DO) ==
98719397407SSherry Moore 	    QCN_SP_IDL) {
988136097ceSjb 		(void) ddi_intr_trigger_softint(
98919397407SSherry Moore 		    qcn_state->qcn_softint_hdl, NULL);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9947c478bd9Sstevel@tonic-gate static uint_t
qcn_soft_intr(caddr_t arg1,caddr_t arg2)9957c478bd9Sstevel@tonic-gate qcn_soft_intr(caddr_t arg1, caddr_t arg2)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9987c478bd9Sstevel@tonic-gate 	int	cc;
999136097ceSjb 	int	overflow_check;
10007c478bd9Sstevel@tonic-gate 
1001136097ceSjb 	do {
100232179a93Sjb 		(void) atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IP);
1003136097ceSjb 		mutex_enter(&qcn_state->qcn_hi_lock);
100409921062Sjb 		cc = RING_CNT(qcn_state);
100509921062Sjb 		mutex_exit(&qcn_state->qcn_hi_lock);
100609921062Sjb 		if (cc <= 0) {
1007136097ceSjb 			goto out;
1008136097ceSjb 		}
10097c478bd9Sstevel@tonic-gate 
1010136097ceSjb 		if ((mp = allocb(cc, BPRI_MED)) == NULL) {
101109921062Sjb 			mutex_enter(&qcn_state->qcn_hi_lock);
1012136097ceSjb 			qcn_input_dropped += cc;
1013136097ceSjb 			mutex_exit(&qcn_state->qcn_hi_lock);
1014136097ceSjb 			cmn_err(CE_WARN, "qcn_intr: allocb"
1015136097ceSjb 			    "failed (console input dropped)");
1016136097ceSjb 			goto out;
1017136097ceSjb 		}
10187c478bd9Sstevel@tonic-gate 
101909921062Sjb 		mutex_enter(&qcn_state->qcn_hi_lock);
1020136097ceSjb 		do {
1021136097ceSjb 			/* put console input onto stream */
1022136097ceSjb 			*(char *)mp->b_wptr++ = RING_GET(qcn_state);
1023136097ceSjb 		} while (--cc);
10247c478bd9Sstevel@tonic-gate 
1025136097ceSjb 		if ((overflow_check = qcn_state->qcn_rbuf_overflow) != 0) {
1026136097ceSjb 			qcn_state->qcn_rbuf_overflow = 0;
1027136097ceSjb 		}
10287c478bd9Sstevel@tonic-gate 		mutex_exit(&qcn_state->qcn_hi_lock);
10297c478bd9Sstevel@tonic-gate 
1030136097ceSjb 		if (overflow_check) {
1031136097ceSjb 			cmn_err(CE_WARN, "qcn: Ring buffer overflow\n");
1032136097ceSjb 		}
1033136097ceSjb 
1034136097ceSjb 		if (qcn_state->qcn_readq) {
1035136097ceSjb 			putnext(qcn_state->qcn_readq, mp);
1036136097ceSjb 		}
10377c478bd9Sstevel@tonic-gate out:
103854c6b703Sjb 		/*
103954c6b703Sjb 		 * If there are pending transmits because hypervisor
104054c6b703Sjb 		 * returned EWOULDBLOCK poke start now.
104154c6b703Sjb 		 */
104254c6b703Sjb 
104354c6b703Sjb 		if (qcn_state->qcn_writeq != NULL) {
104454c6b703Sjb 			if (qcn_state->qcn_hangup) {
104554c6b703Sjb 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
1046136097ceSjb 				flushq(qcn_state->qcn_writeq, FLUSHDATA);
104754c6b703Sjb 				qcn_state->qcn_hangup = 0;
104854c6b703Sjb 			} else {
104954c6b703Sjb 				mutex_enter(&qcn_state->qcn_lock);
105054c6b703Sjb 				qcn_start();
105154c6b703Sjb 				mutex_exit(&qcn_state->qcn_lock);
1052136097ceSjb 			}
10537c478bd9Sstevel@tonic-gate 		}
105432179a93Sjb 		/*
105532179a93Sjb 		 * now loop if another interrupt came in (qcn_trigger_softint
105632179a93Sjb 		 * called) while we were processing the loop
105732179a93Sjb 		 */
105832179a93Sjb 	} while (atomic_swap_uint(&qcn_state->qcn_soft_pend, QCN_SP_IDL) ==
105919397407SSherry Moore 	    QCN_SP_DO);
10607c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10647c478bd9Sstevel@tonic-gate static uint_t
qcn_hi_intr(caddr_t arg)10657c478bd9Sstevel@tonic-gate qcn_hi_intr(caddr_t arg)
1066136097ceSjb {
1067136097ceSjb 	mutex_enter(&qcn_state->qcn_hi_lock);
1068136097ceSjb 
1069136097ceSjb 	qcn_state->cons_receive();
1070136097ceSjb 
1071136097ceSjb 	mutex_exit(&qcn_state->qcn_hi_lock);
1072136097ceSjb 	qcn_trigger_softint();
1073136097ceSjb 
1074136097ceSjb 	return (DDI_INTR_CLAIMED);
1075136097ceSjb }
1076136097ceSjb 
1077136097ceSjb static void
qcn_receive_read(void)1078136097ceSjb qcn_receive_read(void)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	int64_t rv;
1081136097ceSjb 	uint8_t *bufp;
1082136097ceSjb 	int64_t	retcount = 0;
1083136097ceSjb 	int	i;
10847c478bd9Sstevel@tonic-gate 
1085136097ceSjb 	do {
1086136097ceSjb 		/*
1087136097ceSjb 		 * Maximize available buffer size
1088136097ceSjb 		 */
1089136097ceSjb 		if (RING_CNT(qcn_state) <= 0) {
1090136097ceSjb 			RING_INIT(qcn_state);
10917c478bd9Sstevel@tonic-gate 		}
1092136097ceSjb 		rv = hv_cnread(qcn_state->cons_read_buf_ra +
109319397407SSherry Moore 		    RING_POFF(qcn_state),
109419397407SSherry Moore 		    RING_LEFT(qcn_state),
109519397407SSherry Moore 		    &retcount);
1096136097ceSjb 		bufp = RING_ADDR(qcn_state);
1097136097ceSjb 		if (rv == H_EOK) {
1098136097ceSjb 			/*
1099136097ceSjb 			 * if the alternate break sequence is enabled, test
1100136097ceSjb 			 * the buffer for the sequence and if it is there,
1101136097ceSjb 			 * enter the debugger.
1102136097ceSjb 			 */
1103136097ceSjb 			if (abort_enable == KIOCABORTALTERNATE) {
1104136097ceSjb 				for (i = 0; i < retcount; i++) {
1105136097ceSjb 					if (abort_charseq_recognize(*bufp++)) {
1106136097ceSjb 						abort_sequence_enter(
110719397407SSherry Moore 						    (char *)NULL);
1108136097ceSjb 					}
1109136097ceSjb 				}
1110136097ceSjb 			}
11117c478bd9Sstevel@tonic-gate 
1112136097ceSjb 			/* put console input onto stream */
1113136097ceSjb 			if (retcount > 0) {
1114136097ceSjb 				/*
1115136097ceSjb 				 * the characters are already in the ring,
1116136097ceSjb 				 * just update the pointer so the characters
1117136097ceSjb 				 * can be retrieved.
1118136097ceSjb 				 */
1119136097ceSjb 				RING_UPD(qcn_state, retcount);
1120136097ceSjb 			}
1121136097ceSjb 		} else {
1122136097ceSjb 			switch (rv) {
11237c478bd9Sstevel@tonic-gate 
1124136097ceSjb 			case H_EWOULDBLOCK :
1125136097ceSjb 				/*
1126136097ceSjb 				 * hypervisor cannot handle the request.
1127136097ceSjb 				 * Try again later.
1128136097ceSjb 				 */
1129136097ceSjb 				break;
11307c478bd9Sstevel@tonic-gate 
1131136097ceSjb 
1132136097ceSjb 			case H_BREAK :
1133136097ceSjb 				/*
113441c9e69bSZach Kissel 				 * on break enter the debugger
1135136097ceSjb 				 */
1136*ec16669aSZach Kissel 				if (abort_enable != KIOCABORTALTERNATE)
1137*ec16669aSZach Kissel 					abort_sequence_enter((char *)NULL);
1138136097ceSjb 				break;
1139136097ceSjb 
1140136097ceSjb 			case H_HUP :
1141136097ceSjb 				qcn_state->qcn_hangup = 1;
1142136097ceSjb 				break;
1143136097ceSjb 
1144136097ceSjb 			default :
1145136097ceSjb 				break;
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 		}
1148136097ceSjb 	} while (rv == H_EOK);
1149136097ceSjb }
11507c478bd9Sstevel@tonic-gate 
1151136097ceSjb static void
qcn_receive_getchr(void)1152136097ceSjb qcn_receive_getchr(void)
1153136097ceSjb {
1154136097ceSjb 	int64_t rv;
1155136097ceSjb 	uint8_t	buf;
1156136097ceSjb 
1157136097ceSjb 	do {
1158136097ceSjb 		rv = hv_cngetchar(&buf);
1159136097ceSjb 		if (rv == H_EOK) {
1160136097ceSjb 			if (abort_enable == KIOCABORTALTERNATE) {
1161136097ceSjb 				if (abort_charseq_recognize(buf)) {
1162136097ceSjb 					abort_sequence_enter((char *)NULL);
1163136097ceSjb 				}
1164136097ceSjb 			}
1165136097ceSjb 
1166136097ceSjb 			/* put console input onto stream */
1167136097ceSjb 			if (RING_POK(qcn_state, 1)) {
1168136097ceSjb 				RING_PUT(qcn_state, buf);
1169136097ceSjb 			} else {
1170136097ceSjb 				qcn_state->qcn_rbuf_overflow++;
1171136097ceSjb 			}
11727c478bd9Sstevel@tonic-gate 		} else {
1173136097ceSjb 			if (rv == H_BREAK) {
1174*ec16669aSZach Kissel 				if (abort_enable != KIOCABORTENABLE)
1175*ec16669aSZach Kissel 					abort_sequence_enter((char *)NULL);
1176136097ceSjb 			}
11777c478bd9Sstevel@tonic-gate 
1178136097ceSjb 			if (rv == H_HUP)  {
1179136097ceSjb 				qcn_state->qcn_hangup = 1;
1180136097ceSjb 			}
1181136097ceSjb 			return;
1182136097ceSjb 		}
1183136097ceSjb 	} while (rv == H_EOK);
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate #ifdef QCN_POLLING
11877c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11887c478bd9Sstevel@tonic-gate static void
qcn_poll_handler(void * unused)11897c478bd9Sstevel@tonic-gate qcn_poll_handler(void *unused)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11927c478bd9Sstevel@tonic-gate 	int64_t rv;
11937c478bd9Sstevel@tonic-gate 	uint8_t buf;
11947c478bd9Sstevel@tonic-gate 	int qcn_writeq_flush = 0;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/* LINTED: E_CONSTANT_CONDITION */
11977c478bd9Sstevel@tonic-gate 	while (1) {
11987c478bd9Sstevel@tonic-gate 		rv = hv_cngetchar(&buf);
11997c478bd9Sstevel@tonic-gate 		if (rv == H_BREAK) {
1200*ec16669aSZach Kissel 			if (abort_enable != KIOCABORTALTERNATE)
1201*ec16669aSZach Kissel 				abort_sequence_enter((char *)NULL);
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 		if (rv == H_HUP)  {
12057c478bd9Sstevel@tonic-gate 			if (qcn_state->qcn_readq) {
12067c478bd9Sstevel@tonic-gate 				(void) putctl(qcn_state->qcn_readq, M_HANGUP);
12077c478bd9Sstevel@tonic-gate 				qcn_writeq_flush = 1;
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 			goto out;
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		if (rv != H_EOK)
12137c478bd9Sstevel@tonic-gate 			goto out;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		if (abort_enable == KIOCABORTALTERNATE) {
12167c478bd9Sstevel@tonic-gate 			if (abort_charseq_recognize(buf)) {
12177c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
12187c478bd9Sstevel@tonic-gate 			}
12197c478bd9Sstevel@tonic-gate 		}
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 		/* put console input onto stream */
12227c478bd9Sstevel@tonic-gate 		if (qcn_state->qcn_readq) {
12237c478bd9Sstevel@tonic-gate 			if ((mp = allocb(1, BPRI_MED)) == NULL) {
12247c478bd9Sstevel@tonic-gate 				qcn_input_dropped++;
12257c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "qcn_intr: allocb"
12267c478bd9Sstevel@tonic-gate 				    "failed (console input dropped)");
12277c478bd9Sstevel@tonic-gate 				return;
12287c478bd9Sstevel@tonic-gate 			}
12297c478bd9Sstevel@tonic-gate 			*(char *)mp->b_wptr++ = buf;
12307c478bd9Sstevel@tonic-gate 			putnext(qcn_state->qcn_readq, mp);
12317c478bd9Sstevel@tonic-gate 		}
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate out:
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate  * If there are pending transmits because hypervisor
12367c478bd9Sstevel@tonic-gate  * returned EWOULDBLOCK poke start now.
12377c478bd9Sstevel@tonic-gate  */
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12407c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL) {
12417c478bd9Sstevel@tonic-gate 		if (qcn_writeq_flush) {
12427c478bd9Sstevel@tonic-gate 			flushq(qcn_state->qcn_writeq, FLUSHDATA);
12437c478bd9Sstevel@tonic-gate 		} else {
12447c478bd9Sstevel@tonic-gate 			qcn_start();
12457c478bd9Sstevel@tonic-gate 		}
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12487c478bd9Sstevel@tonic-gate }
12497c478bd9Sstevel@tonic-gate #endif
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * Check for abort character sequence, copied from zs_async.c
12537c478bd9Sstevel@tonic-gate  */
12547c478bd9Sstevel@tonic-gate #define	CNTRL(c) ((c)&037)
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate static boolean_t
abort_charseq_recognize(uchar_t ch)12577c478bd9Sstevel@tonic-gate abort_charseq_recognize(uchar_t ch)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	static int state = 0;
12607c478bd9Sstevel@tonic-gate 	static char sequence[] = { '\r', '~', CNTRL('b') };
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	if (ch == sequence[state]) {
12637c478bd9Sstevel@tonic-gate 		if (++state >= sizeof (sequence)) {
12647c478bd9Sstevel@tonic-gate 			state = 0;
12657c478bd9Sstevel@tonic-gate 			return (B_TRUE);
12667c478bd9Sstevel@tonic-gate 		}
12677c478bd9Sstevel@tonic-gate 	} else {
12687c478bd9Sstevel@tonic-gate 		state = (ch == sequence[0]) ? 1 : 0;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 	return (B_FALSE);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate static int
qcn_rsrv(queue_t * q)12757c478bd9Sstevel@tonic-gate qcn_rsrv(queue_t *q)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
12807c478bd9Sstevel@tonic-gate 		return (0);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
12857c478bd9Sstevel@tonic-gate 		if (canputnext(q))
12867c478bd9Sstevel@tonic-gate 			putnext(q, mp);
12877c478bd9Sstevel@tonic-gate 		else if (mp->b_datap->db_type >= QPCTL)
12887c478bd9Sstevel@tonic-gate 			(void) putbq(q, mp);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	return (0);
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate /* ARGSUSED */
12977c478bd9Sstevel@tonic-gate static int
qcn_wsrv(queue_t * q)12987c478bd9Sstevel@tonic-gate qcn_wsrv(queue_t *q)
12997c478bd9Sstevel@tonic-gate {
13007c478bd9Sstevel@tonic-gate 	if (qcn_stopped == B_TRUE)
13017c478bd9Sstevel@tonic-gate 		return (0);
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	mutex_enter(&qcn_state->qcn_lock);
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if (qcn_state->qcn_writeq != NULL)
13067c478bd9Sstevel@tonic-gate 		qcn_start();
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	mutex_exit(&qcn_state->qcn_lock);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	return (0);
13117c478bd9Sstevel@tonic-gate }
13121b83305cSjm 
13131b83305cSjm static boolean_t
qcn_polledio_ischar(cons_polledio_arg_t arg)13141b83305cSjm qcn_polledio_ischar(cons_polledio_arg_t arg)
13151b83305cSjm {
13161b83305cSjm 	qcn_t *state = (qcn_t *)arg;
13171b83305cSjm 
13181b83305cSjm 	if (state->qcn_char_available)
13191b83305cSjm 		return (B_TRUE);
13201b83305cSjm 
13211b83305cSjm 	return (state->qcn_char_available =
13221b83305cSjm 	    (hv_cngetchar(&state->qcn_hold_char) == H_EOK));
13231b83305cSjm }
13241b83305cSjm 
13251b83305cSjm 
13261b83305cSjm static int
qcn_polledio_getchar(cons_polledio_arg_t arg)13271b83305cSjm qcn_polledio_getchar(cons_polledio_arg_t arg)
13281b83305cSjm {
13291b83305cSjm 	qcn_t *state = (qcn_t *)arg;
13301b83305cSjm 
13311b83305cSjm 	while (!qcn_polledio_ischar(arg))
13321b83305cSjm 		drv_usecwait(10);
13331b83305cSjm 
13341b83305cSjm 	state->qcn_char_available = B_FALSE;
13351b83305cSjm 
13361b83305cSjm 	return ((int)state->qcn_hold_char);
13371b83305cSjm }
13381b83305cSjm 
13391b83305cSjm static void
qcn_polledio_putchar(cons_polledio_arg_t arg,uchar_t c)13401b83305cSjm qcn_polledio_putchar(cons_polledio_arg_t arg, uchar_t c)
13411b83305cSjm {
13421b83305cSjm 	if (c == '\n')
13431b83305cSjm 		qcn_polledio_putchar(arg, '\r');
13441b83305cSjm 
13451b83305cSjm 	while (hv_cnputchar((uint8_t)c) == H_EWOULDBLOCK)
13461b83305cSjm 		drv_usecwait(10);
13471b83305cSjm }
13481b83305cSjm 
13491b83305cSjm static void
qcn_polledio_enter(cons_polledio_arg_t arg)13501b83305cSjm qcn_polledio_enter(cons_polledio_arg_t arg)
13511b83305cSjm {
13521b83305cSjm 	qcn_t *state = (qcn_t *)arg;
13531b83305cSjm 
13541b83305cSjm 	state->qcn_char_available = B_FALSE;
13551b83305cSjm }
13561b83305cSjm 
13571b83305cSjm /* ARGSUSED */
13581b83305cSjm static void
qcn_polledio_exit(cons_polledio_arg_t arg)13591b83305cSjm qcn_polledio_exit(cons_polledio_arg_t arg)
13601b83305cSjm {
13611b83305cSjm }
1362