xref: /illumos-gate/usr/src/uts/sun4u/serengeti/io/sgcn.c (revision 07d06da5)
103831d35Sstevel /*
203831d35Sstevel  * CDDL HEADER START
303831d35Sstevel  *
403831d35Sstevel  * The contents of this file are subject to the terms of the
503831d35Sstevel  * Common Development and Distribution License (the "License").
603831d35Sstevel  * You may not use this file except in compliance with the License.
703831d35Sstevel  *
803831d35Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
903831d35Sstevel  * or http://www.opensolaris.org/os/licensing.
1003831d35Sstevel  * See the License for the specific language governing permissions
1103831d35Sstevel  * and limitations under the License.
1203831d35Sstevel  *
1303831d35Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
1403831d35Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1503831d35Sstevel  * If applicable, add the following below this CDDL HEADER, with the
1603831d35Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
1703831d35Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
1803831d35Sstevel  *
1903831d35Sstevel  * CDDL HEADER END
2003831d35Sstevel  */
2103831d35Sstevel 
2203831d35Sstevel /*
23*07d06da5SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2403831d35Sstevel  * Use is subject to license terms.
2503831d35Sstevel  */
2603831d35Sstevel 
2703831d35Sstevel 
2803831d35Sstevel /*
2903831d35Sstevel  * Serengeti console driver, see sys/sgcn.h for more information
3003831d35Sstevel  * This driver uses the QPAIR form of STREAMS Perimeters to serialize access
3103831d35Sstevel  * to the read and write STREAMS queues.
3203831d35Sstevel  */
3303831d35Sstevel 
3403831d35Sstevel #include <sys/errno.h>
3503831d35Sstevel #include <sys/stat.h>
3603831d35Sstevel #include <sys/kmem.h>
3703831d35Sstevel #include <sys/conf.h>
3803831d35Sstevel #include <sys/termios.h>
3903831d35Sstevel #include <sys/modctl.h>
4003831d35Sstevel #include <sys/kbio.h>
4103831d35Sstevel #include <sys/stropts.h>
4203831d35Sstevel #include <sys/stream.h>
4303831d35Sstevel #include <sys/strsun.h>
4403831d35Sstevel #include <sys/sysmacros.h>
4503831d35Sstevel #include <sys/promif.h>
4603831d35Sstevel #include <sys/prom_plat.h>
4703831d35Sstevel #include <sys/sgsbbc.h>
4803831d35Sstevel #include <sys/sgsbbc_iosram.h>
4903831d35Sstevel #include <sys/sgcn.h>
5003831d35Sstevel #include <sys/serengeti.h>
5103831d35Sstevel #include <sys/ddi.h>
5203831d35Sstevel #include <sys/sunddi.h>
5303831d35Sstevel #include <sys/strsubr.h>
5403831d35Sstevel 
5503831d35Sstevel /*
5603831d35Sstevel  * Here we define several macros for accessing console IOSRAM
5703831d35Sstevel  */
5803831d35Sstevel 
5903831d35Sstevel #define	POINTER(base, field)	((caddr_t)&base.field)
6003831d35Sstevel #define	OFFSETOF(base, field)	((caddr_t)&base.field - (caddr_t)&base)
6103831d35Sstevel 
6203831d35Sstevel #define	RW_CONSOLE_READ		0xAAAA
6303831d35Sstevel #define	RW_CONSOLE_WRITE	0xBBBB
6403831d35Sstevel 
6503831d35Sstevel #define	CONSOLE_READ(buf, len)	sgcn_rw(RW_CONSOLE_READ, buf, len)
6603831d35Sstevel #define	CONSOLE_WRITE(buf, len)	sgcn_rw(RW_CONSOLE_WRITE, buf, len)
6703831d35Sstevel 
6803831d35Sstevel #define	SGCN_MI_IDNUM		0xABCD
6903831d35Sstevel #define	SGCN_MI_HIWAT		2048*2048
7003831d35Sstevel #define	SGCN_MI_LOWAT		128
7103831d35Sstevel 
7203831d35Sstevel /* dev_ops and cb_ops for device driver */
7303831d35Sstevel static int sgcn_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
7403831d35Sstevel static int sgcn_attach(dev_info_t *, ddi_attach_cmd_t);
7503831d35Sstevel static int sgcn_detach(dev_info_t *, ddi_detach_cmd_t);
7603831d35Sstevel static int sgcn_open(queue_t *, dev_t *, int, int, cred_t *);
7703831d35Sstevel static int sgcn_close(queue_t *, int, cred_t *);
7803831d35Sstevel static int sgcn_wput(queue_t *, mblk_t *);
7903831d35Sstevel static int sgcn_wsrv(queue_t *);
8003831d35Sstevel static int sgcn_rsrv(queue_t *);
8103831d35Sstevel 
8203831d35Sstevel /* interrupt handlers */
8303831d35Sstevel static void sgcn_data_in_handler(caddr_t);
8403831d35Sstevel static void sgcn_space_2_out_handler(caddr_t);
8503831d35Sstevel static void sgcn_break_handler(caddr_t);
8603831d35Sstevel 
8703831d35Sstevel /* other internal sgcn routines */
8803831d35Sstevel static void sgcn_ioctl(queue_t *, mblk_t *);
8903831d35Sstevel static void sgcn_reioctl(void *);
9003831d35Sstevel static void sgcn_start(void);
9103831d35Sstevel static int sgcn_transmit(queue_t *, mblk_t *);
9203831d35Sstevel static void sgcn_flush(void);
9303831d35Sstevel static int sgcn_read_header(int, cnsram_header *);
9403831d35Sstevel static int sgcn_rw(int, caddr_t, int);
9503831d35Sstevel static void sgcn_log_error(int, int);
9603831d35Sstevel 
9703831d35Sstevel /* circular buffer routines */
9803831d35Sstevel static int circular_buffer_write(int, int, int, int, caddr_t, int);
9903831d35Sstevel static int circular_buffer_read(int, int, int, int, caddr_t, int);
10003831d35Sstevel 
10103831d35Sstevel static boolean_t abort_charseq_recognize(uchar_t);
10203831d35Sstevel static void sg_abort_seq_handler(char *);
10303831d35Sstevel 
10403831d35Sstevel static	sgcn_t *sgcn_state;
10503831d35Sstevel static uchar_t	sgcn_stopped = FALSE;
10603831d35Sstevel static int	sgcn_timeout_period = 20;	/* time out in seconds */
10703831d35Sstevel 
10803831d35Sstevel /* streams structures */
10903831d35Sstevel static struct module_info minfo = {
11003831d35Sstevel 	SGCN_MI_IDNUM,	/* mi_idnum		*/
11103831d35Sstevel 	"sgcn",		/* mi_idname		*/
11203831d35Sstevel 	0,		/* mi_minpsz		*/
11303831d35Sstevel 	INFPSZ,		/* mi_maxpsz		*/
11403831d35Sstevel 	SGCN_MI_HIWAT,	/* mi_hiwat		*/
11503831d35Sstevel 	SGCN_MI_LOWAT	/* mi_lowat		*/
11603831d35Sstevel };
11703831d35Sstevel 
11803831d35Sstevel static struct qinit rinit = {
11903831d35Sstevel 	putq,		/* qi_putp		*/
12003831d35Sstevel 	sgcn_rsrv,	/* qi_srvp		*/
12103831d35Sstevel 	sgcn_open,	/* qi_qopen		*/
12203831d35Sstevel 	sgcn_close,	/* qi_qclose		*/
12303831d35Sstevel 	NULL,		/* qi_qadmin		*/
12403831d35Sstevel 	&minfo,		/* qi_minfo		*/
12503831d35Sstevel 	NULL		/* qi_mstat		*/
12603831d35Sstevel };
12703831d35Sstevel 
12803831d35Sstevel static struct qinit winit = {
12903831d35Sstevel 	sgcn_wput,	/* qi_putp		*/
13003831d35Sstevel 	sgcn_wsrv,	/* qi_srvp		*/
13103831d35Sstevel 	sgcn_open,	/* qi_qopen		*/
13203831d35Sstevel 	sgcn_close,	/* qi_qclose		*/
13303831d35Sstevel 	NULL,		/* qi_qadmin		*/
13403831d35Sstevel 	&minfo,		/* qi_minfo		*/
13503831d35Sstevel 	NULL		/* qi_mstat		*/
13603831d35Sstevel };
13703831d35Sstevel 
13803831d35Sstevel static struct streamtab sgcnstrinfo = {
13903831d35Sstevel 	&rinit,
14003831d35Sstevel 	&winit,
14103831d35Sstevel 	NULL,
14203831d35Sstevel 	NULL
14303831d35Sstevel };
14403831d35Sstevel 
14503831d35Sstevel /* standard device driver structures */
14603831d35Sstevel static struct cb_ops sgcn_cb_ops = {
14703831d35Sstevel 	nulldev,		/* open()		*/
14803831d35Sstevel 	nulldev,		/* close()		*/
14903831d35Sstevel 	nodev,			/* strategy()		*/
15003831d35Sstevel 	nodev,			/* print()		*/
15103831d35Sstevel 	nodev,			/* dump()		*/
15203831d35Sstevel 	nodev,			/* read()		*/
15303831d35Sstevel 	nodev,			/* write()		*/
15403831d35Sstevel 	nodev,			/* ioctl()		*/
15503831d35Sstevel 	nodev,			/* devmap()		*/
15603831d35Sstevel 	nodev,			/* mmap()		*/
15703831d35Sstevel 	nodev,			/* segmap()		*/
15803831d35Sstevel 	nochpoll,		/* poll()		*/
15903831d35Sstevel 	ddi_prop_op,		/* prop_op()		*/
16003831d35Sstevel 	&sgcnstrinfo,		/* cb_str		*/
16103831d35Sstevel 	D_MP | D_MTQPAIR		/* cb_flag		*/
16203831d35Sstevel };
16303831d35Sstevel 
16403831d35Sstevel static struct dev_ops sgcn_ops = {
16503831d35Sstevel 	DEVO_REV,
16603831d35Sstevel 	0,			/* refcnt		*/
16703831d35Sstevel 	sgcn_getinfo,		/* getinfo()		*/
16803831d35Sstevel 	nulldev,		/* identify()		*/
16903831d35Sstevel 	nulldev,		/* probe()		*/
17003831d35Sstevel 	sgcn_attach,		/* attach()		*/
17103831d35Sstevel 	sgcn_detach,		/* detach()		*/
17203831d35Sstevel 	nodev,			/* reset()		*/
17303831d35Sstevel 	&sgcn_cb_ops,		/* cb_ops		*/
17403831d35Sstevel 	(struct bus_ops *)NULL,	/* bus_ops		*/
17519397407SSherry Moore 	NULL,			/* power()		*/
17619397407SSherry Moore 	ddi_quiesce_not_supported,	/* quiesce	*/
17703831d35Sstevel };
17803831d35Sstevel 
17903831d35Sstevel static struct modldrv modldrv = {
18003831d35Sstevel 	&mod_driverops,
18119397407SSherry Moore 	"Serengeti console driver",
18203831d35Sstevel 	&sgcn_ops
18303831d35Sstevel };
18403831d35Sstevel 
18503831d35Sstevel static struct modlinkage modlinkage = {
18603831d35Sstevel 	MODREV_1,
18703831d35Sstevel 	(void*)&modldrv,
18803831d35Sstevel 	NULL
18903831d35Sstevel };
19003831d35Sstevel 
19103831d35Sstevel 
19203831d35Sstevel /* driver configuration routines */
19303831d35Sstevel int
_init(void)19403831d35Sstevel _init(void)
19503831d35Sstevel {
19603831d35Sstevel 	int error;
19703831d35Sstevel 
19803831d35Sstevel 	sgcn_state = kmem_zalloc(sizeof (sgcn_t), KM_SLEEP);
19903831d35Sstevel 
20003831d35Sstevel 	error = mod_install(&modlinkage);
20103831d35Sstevel 
20203831d35Sstevel 	if (error == 0) {
20303831d35Sstevel 		mutex_init(&sgcn_state->sgcn_lock, NULL, MUTEX_DRIVER, NULL);
20403831d35Sstevel 	} else {
20503831d35Sstevel 		kmem_free(sgcn_state, sizeof (sgcn_t));
20603831d35Sstevel 	}
20703831d35Sstevel 
20803831d35Sstevel 	return (error);
20903831d35Sstevel }
21003831d35Sstevel 
21103831d35Sstevel int
_fini(void)21203831d35Sstevel _fini(void)
21303831d35Sstevel {
21403831d35Sstevel 	/* can't remove console driver */
21503831d35Sstevel 	return (EBUSY);
21603831d35Sstevel }
21703831d35Sstevel 
21803831d35Sstevel int
_info(struct modinfo * modinfop)21903831d35Sstevel _info(struct modinfo *modinfop)
22003831d35Sstevel {
22103831d35Sstevel 	return (mod_info(&modlinkage, modinfop));
22203831d35Sstevel }
22303831d35Sstevel 
22403831d35Sstevel /*
22503831d35Sstevel  * sgcn_attach is called at startup time.
22603831d35Sstevel  * There is only once instance of this driver.
22703831d35Sstevel  */
22803831d35Sstevel static int
sgcn_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)22903831d35Sstevel sgcn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
23003831d35Sstevel {
23103831d35Sstevel 	extern int ddi_create_internal_pathname(
23203831d35Sstevel 	    dev_info_t *, char *, int, minor_t);
23303831d35Sstevel 	cnsram_header	header;
23403831d35Sstevel 	int		rv;
23503831d35Sstevel 
23603831d35Sstevel 	if (cmd != DDI_ATTACH)
23703831d35Sstevel 		return (DDI_FAILURE);
23803831d35Sstevel 
23903831d35Sstevel 	if (ddi_create_internal_pathname(dip, "sgcn", S_IFCHR, 0)
24003831d35Sstevel 	    != DDI_SUCCESS)
24103831d35Sstevel 		return (DDI_FAILURE);
24203831d35Sstevel 
24303831d35Sstevel 	/* prepare some data structures in soft state */
24403831d35Sstevel 	mutex_enter(&sgcn_state->sgcn_lock);
24503831d35Sstevel 
24603831d35Sstevel 	sgcn_state->sgcn_dip = dip;
24703831d35Sstevel 
24803831d35Sstevel 	mutex_exit(&sgcn_state->sgcn_lock);
24903831d35Sstevel 
25003831d35Sstevel 	/*
25103831d35Sstevel 	 * We need to verify IOSRAM is intact at startup time. If by
25203831d35Sstevel 	 * any chance IOSRAM is corrupted, that means SC is not ready.
25303831d35Sstevel 	 * All we can do is stopping.
25403831d35Sstevel 	 */
25503831d35Sstevel 	rv = iosram_read(SBBC_CONSOLE_KEY, 0, (caddr_t)&header,
25619397407SSherry Moore 	    sizeof (cnsram_header));
25703831d35Sstevel 	if (rv != 0)
25803831d35Sstevel 		cmn_err(CE_PANIC, "sgcn_attach(): Reading from IOSRAM failed");
25903831d35Sstevel 	if (header.cnsram_magic != CNSRAM_MAGIC)
26003831d35Sstevel 		cmn_err(CE_PANIC, "sgcn_attach(): Wrong IOSRAM console buffer");
26103831d35Sstevel 	if (!header.cnsram_in_end && !header.cnsram_in_begin)
26203831d35Sstevel 		cmn_err(CE_PANIC, "sgcn_attach(): Wrong IOSRAM input buffer");
26303831d35Sstevel 	if (!header.cnsram_out_end && !header.cnsram_out_begin)
26403831d35Sstevel 		cmn_err(CE_PANIC, "sgcn_attach(): Wrong IOSRAM output buffer");
26503831d35Sstevel 	/*
26603831d35Sstevel 	 * XXX need to add extra check for version no.
26703831d35Sstevel 	 */
26803831d35Sstevel 
26903831d35Sstevel 	/* Allocate console input buffer */
27003831d35Sstevel 	sgcn_state->sgcn_inbuf_size =
27119397407SSherry Moore 	    header.cnsram_in_end - header.cnsram_in_begin;
27203831d35Sstevel 	sgcn_state->sgcn_inbuf =
27319397407SSherry Moore 	    kmem_alloc(sgcn_state->sgcn_inbuf_size, KM_SLEEP);
27403831d35Sstevel #ifdef SGCN_DEBUG
27503831d35Sstevel 	prom_printf("Allocated %d(0x%X) bytes for console\n",
27619397407SSherry Moore 	    sgcn_state->sgcn_inbuf_size);
27703831d35Sstevel #endif
27803831d35Sstevel 
279