10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53813Sdp * Common Development and Distribution License (the "License"). 63813Sdp * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 223813Sdp * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Zone Console Driver. 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * This driver, derived from the pts/ptm drivers, is the pseudo console driver 320Sstevel@tonic-gate * for system zones. Its implementation is straightforward. Each instance 330Sstevel@tonic-gate * of the driver represents a global-zone/local-zone pair (this maps in a 340Sstevel@tonic-gate * straightforward way to the commonly used terminal notion of "master side" 350Sstevel@tonic-gate * and "slave side", and we use that terminology throughout). 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * Instances of zcons are onlined as children of /pseudo/zconsnex@1/ 380Sstevel@tonic-gate * by zoneadmd in userland, using the devctl framework; thus the driver 390Sstevel@tonic-gate * does not need to maintain any sort of "admin" node. 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * The driver shuttles I/O from master side to slave side and back. In a break 420Sstevel@tonic-gate * from the pts/ptm semantics, if one side is not open, I/O directed towards 430Sstevel@tonic-gate * it will simply be discarded. This is so that if zoneadmd is not holding 440Sstevel@tonic-gate * the master side console open (i.e. it has died somehow), processes in 450Sstevel@tonic-gate * the zone do not experience any errors and I/O to the console does not 460Sstevel@tonic-gate * hang. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * TODO: we may want to revisit the other direction; i.e. we may want 490Sstevel@tonic-gate * zoneadmd to be able to detect whether no zone processes are holding the 500Sstevel@tonic-gate * console open, an unusual situation. 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate 530Sstevel@tonic-gate #include <sys/types.h> 540Sstevel@tonic-gate #include <sys/cmn_err.h> 550Sstevel@tonic-gate #include <sys/conf.h> 560Sstevel@tonic-gate #include <sys/cred.h> 570Sstevel@tonic-gate #include <sys/ddi.h> 580Sstevel@tonic-gate #include <sys/debug.h> 590Sstevel@tonic-gate #include <sys/devops.h> 600Sstevel@tonic-gate #include <sys/errno.h> 610Sstevel@tonic-gate #include <sys/file.h> 620Sstevel@tonic-gate #include <sys/modctl.h> 630Sstevel@tonic-gate #include <sys/param.h> 640Sstevel@tonic-gate #include <sys/stat.h> 650Sstevel@tonic-gate #include <sys/stream.h> 660Sstevel@tonic-gate #include <sys/stropts.h> 670Sstevel@tonic-gate #include <sys/strsun.h> 680Sstevel@tonic-gate #include <sys/sunddi.h> 690Sstevel@tonic-gate #include <sys/sysmacros.h> 700Sstevel@tonic-gate #include <sys/systm.h> 710Sstevel@tonic-gate #include <sys/types.h> 720Sstevel@tonic-gate #include <sys/zcons.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 750Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t); 760Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t); 770Sstevel@tonic-gate 780Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *); 790Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *); 800Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *); 810Sstevel@tonic-gate static void zc_rsrv(queue_t *); 820Sstevel@tonic-gate static void zc_wsrv(queue_t *); 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* 850Sstevel@tonic-gate * The instance number is encoded in the dev_t in the minor number; the lowest 860Sstevel@tonic-gate * bit of the minor number is used to track the master vs. slave side of the 870Sstevel@tonic-gate * virtual console. The rest of the bits in the minor number are the instance. 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate #define ZC_MASTER_MINOR 0 900Sstevel@tonic-gate #define ZC_SLAVE_MINOR 1 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define ZC_INSTANCE(x) (getminor((x)) >> 1) 930Sstevel@tonic-gate #define ZC_NODE(x) (getminor((x)) & 0x01) 940Sstevel@tonic-gate 950Sstevel@tonic-gate int zcons_debug = 0; 960Sstevel@tonic-gate #define DBG(a) if (zcons_debug) cmn_err(CE_NOTE, a) 970Sstevel@tonic-gate #define DBG1(a, b) if (zcons_debug) cmn_err(CE_NOTE, a, b) 980Sstevel@tonic-gate 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Zone Console Pseudo Terminal Module: stream data structure definitions 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate static struct module_info zc_info = { 1040Sstevel@tonic-gate 31337, /* c0z we r hAx0rs */ 1050Sstevel@tonic-gate "zcons", 1060Sstevel@tonic-gate 0, 1070Sstevel@tonic-gate INFPSZ, 1080Sstevel@tonic-gate 2048, 1090Sstevel@tonic-gate 128 1100Sstevel@tonic-gate }; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate static struct qinit zc_rinit = { 1130Sstevel@tonic-gate NULL, 1140Sstevel@tonic-gate (int (*)()) zc_rsrv, 1150Sstevel@tonic-gate zc_open, 1160Sstevel@tonic-gate zc_close, 1170Sstevel@tonic-gate NULL, 1180Sstevel@tonic-gate &zc_info, 1190Sstevel@tonic-gate NULL 1200Sstevel@tonic-gate }; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate static struct qinit zc_winit = { 1230Sstevel@tonic-gate (int (*)()) zc_wput, 1240Sstevel@tonic-gate (int (*)()) zc_wsrv, 1250Sstevel@tonic-gate NULL, 1260Sstevel@tonic-gate NULL, 1270Sstevel@tonic-gate NULL, 1280Sstevel@tonic-gate &zc_info, 1290Sstevel@tonic-gate NULL 1300Sstevel@tonic-gate }; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate static struct streamtab zc_tab_info = { 1330Sstevel@tonic-gate &zc_rinit, 1340Sstevel@tonic-gate &zc_winit, 1350Sstevel@tonic-gate NULL, 1360Sstevel@tonic-gate NULL 1370Sstevel@tonic-gate }; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate #define ZC_CONF_FLAG (D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL) 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops) 1430Sstevel@tonic-gate */ 1440Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev, zc_attach, zc_detach, nodev, \ 1450Sstevel@tonic-gate zc_getinfo, ZC_CONF_FLAG, &zc_tab_info); 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * Module linkage information for the kernel. 1490Sstevel@tonic-gate */ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate static struct modldrv modldrv = { 152*4959Sedp &mod_driverops, /* Type of module (this is a pseudo driver) */ 153*4959Sedp "Zone console driver", /* description of module */ 154*4959Sedp &zc_ops /* driver ops */ 1550Sstevel@tonic-gate }; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate static struct modlinkage modlinkage = { 1580Sstevel@tonic-gate MODREV_1, 1590Sstevel@tonic-gate &modldrv, 1600Sstevel@tonic-gate NULL 1610Sstevel@tonic-gate }; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate typedef struct zc_state { 1640Sstevel@tonic-gate dev_info_t *zc_devinfo; 1650Sstevel@tonic-gate queue_t *zc_master_rdq; 1660Sstevel@tonic-gate queue_t *zc_slave_rdq; 1670Sstevel@tonic-gate int zc_state; 1680Sstevel@tonic-gate } zc_state_t; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate #define ZC_STATE_MOPEN 0x01 1710Sstevel@tonic-gate #define ZC_STATE_SOPEN 0x02 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate static void *zc_soft_state; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate int 1760Sstevel@tonic-gate _init(void) 1770Sstevel@tonic-gate { 1780Sstevel@tonic-gate int err; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if ((err = ddi_soft_state_init(&zc_soft_state, 1810Sstevel@tonic-gate sizeof (zc_state_t), 0)) != 0) { 1820Sstevel@tonic-gate return (err); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if ((err = mod_install(&modlinkage)) != 0) 1860Sstevel@tonic-gate ddi_soft_state_fini(zc_soft_state); 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate return (err); 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate int 1930Sstevel@tonic-gate _fini(void) 1940Sstevel@tonic-gate { 1950Sstevel@tonic-gate int err; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate if ((err = mod_remove(&modlinkage)) != 0) { 1980Sstevel@tonic-gate return (err); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate ddi_soft_state_fini(&zc_soft_state); 2020Sstevel@tonic-gate return (0); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate int 2060Sstevel@tonic-gate _info(struct modinfo *modinfop) 2070Sstevel@tonic-gate { 2080Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate static int 2120Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2130Sstevel@tonic-gate { 2140Sstevel@tonic-gate zc_state_t *zcs; 2150Sstevel@tonic-gate int instance; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (cmd != DDI_ATTACH) 2180Sstevel@tonic-gate return (DDI_FAILURE); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate instance = ddi_get_instance(dip); 2210Sstevel@tonic-gate if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS) 2220Sstevel@tonic-gate return (DDI_FAILURE); 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR, 2250Sstevel@tonic-gate instance << 1 | ZC_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) || 2260Sstevel@tonic-gate (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR, 2270Sstevel@tonic-gate instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) { 2280Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2290Sstevel@tonic-gate ddi_soft_state_free(zc_soft_state, instance); 2300Sstevel@tonic-gate return (DDI_FAILURE); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) { 2340Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2350Sstevel@tonic-gate ddi_soft_state_free(zc_soft_state, instance); 2360Sstevel@tonic-gate return (DDI_FAILURE); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate zcs->zc_devinfo = dip; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate return (DDI_SUCCESS); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate static int 2440Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2450Sstevel@tonic-gate { 2460Sstevel@tonic-gate zc_state_t *zcs; 2470Sstevel@tonic-gate int instance; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate if (cmd != DDI_DETACH) 2500Sstevel@tonic-gate return (DDI_FAILURE); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate instance = ddi_get_instance(dip); 2530Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 2540Sstevel@tonic-gate return (DDI_FAILURE); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_MOPEN) || 2570Sstevel@tonic-gate (zcs->zc_state & ZC_STATE_SOPEN)) { 2580Sstevel@tonic-gate DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip); 2590Sstevel@tonic-gate return (DDI_FAILURE); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2630Sstevel@tonic-gate ddi_soft_state_free(zc_soft_state, instance); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate return (DDI_SUCCESS); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * zc_getinfo() 2700Sstevel@tonic-gate * getinfo(9e) entrypoint. 2710Sstevel@tonic-gate */ 2720Sstevel@tonic-gate /*ARGSUSED*/ 2730Sstevel@tonic-gate static int 2740Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate zc_state_t *zcs; 2770Sstevel@tonic-gate int instance = ZC_INSTANCE((dev_t)arg); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate switch (infocmd) { 2800Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2810Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 2820Sstevel@tonic-gate return (DDI_FAILURE); 2830Sstevel@tonic-gate *result = zcs->zc_devinfo; 2840Sstevel@tonic-gate return (DDI_SUCCESS); 2850Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 2860Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 2870Sstevel@tonic-gate return (DDI_SUCCESS); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate return (DDI_FAILURE); 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Return the equivalent queue from the other side of the relationship. 2940Sstevel@tonic-gate * e.g.: given the slave's write queue, return the master's write queue. 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate static queue_t * 2970Sstevel@tonic-gate zc_switch(queue_t *qp) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate zc_state_t *zcs = qp->q_ptr; 3000Sstevel@tonic-gate ASSERT(zcs != NULL); 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate if (qp == zcs->zc_master_rdq) 3030Sstevel@tonic-gate return (zcs->zc_slave_rdq); 3040Sstevel@tonic-gate else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL) 3050Sstevel@tonic-gate return (OTHERQ(zcs->zc_slave_rdq)); 3060Sstevel@tonic-gate else if (qp == zcs->zc_slave_rdq) 3070Sstevel@tonic-gate return (zcs->zc_master_rdq); 3080Sstevel@tonic-gate else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL) 3090Sstevel@tonic-gate return (OTHERQ(zcs->zc_master_rdq)); 3100Sstevel@tonic-gate else 3110Sstevel@tonic-gate return (NULL); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * For debugging and outputting messages. Returns the name of the side of 3160Sstevel@tonic-gate * the relationship associated with this queue. 3170Sstevel@tonic-gate */ 3180Sstevel@tonic-gate static const char * 3190Sstevel@tonic-gate zc_side(queue_t *qp) 3200Sstevel@tonic-gate { 3210Sstevel@tonic-gate zc_state_t *zcs = qp->q_ptr; 3220Sstevel@tonic-gate ASSERT(zcs != NULL); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate if (qp == zcs->zc_master_rdq || 3250Sstevel@tonic-gate OTHERQ(qp) == zcs->zc_master_rdq) { 3260Sstevel@tonic-gate return ("master"); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq); 3290Sstevel@tonic-gate return ("slave"); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate /*ARGSUSED*/ 3330Sstevel@tonic-gate static int 3340Sstevel@tonic-gate zc_master_open(zc_state_t *zcs, 3350Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 3360Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 3370Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 3380Sstevel@tonic-gate int sflag, /* open state flag */ 3390Sstevel@tonic-gate cred_t *credp) /* credentials */ 3400Sstevel@tonic-gate { 3410Sstevel@tonic-gate mblk_t *mop; 3420Sstevel@tonic-gate struct stroptions *sop; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * Enforce exclusivity on the master side; the only consumer should 3460Sstevel@tonic-gate * be the zoneadmd for the zone. 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_MOPEN) != 0) 3490Sstevel@tonic-gate return (EBUSY); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) { 3520Sstevel@tonic-gate DBG("zc_master_open(): mop allocation failed\n"); 3530Sstevel@tonic-gate return (ENOMEM); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate zcs->zc_state |= ZC_STATE_MOPEN; 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * q_ptr stores driver private data; stash the soft state data on both 3600Sstevel@tonic-gate * read and write sides of the queue. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = zcs; 3630Sstevel@tonic-gate qprocson(rqp); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * Following qprocson(), the master side is fully plumbed into the 3670Sstevel@tonic-gate * STREAM and may send/receive messages. Setting zcs->zc_master_rdq 3680Sstevel@tonic-gate * will allow the slave to send messages to us (the master). 3690Sstevel@tonic-gate * This cannot occur before qprocson() because the master is not 3700Sstevel@tonic-gate * ready to process them until that point. 3710Sstevel@tonic-gate */ 3720Sstevel@tonic-gate zcs->zc_master_rdq = rqp; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate /* 3750Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue and add 3760Sstevel@tonic-gate * controlling tty as needed. 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 3790Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 3803813Sdp sop = (struct stroptions *)(void *)mop->b_rptr; 3810Sstevel@tonic-gate if (oflag & FNOCTTY) 3820Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT; 3830Sstevel@tonic-gate else 3840Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 3850Sstevel@tonic-gate sop->so_hiwat = 512; 3860Sstevel@tonic-gate sop->so_lowat = 256; 3870Sstevel@tonic-gate putnext(rqp, mop); 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate return (0); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /*ARGSUSED*/ 3930Sstevel@tonic-gate static int 3940Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs, 3950Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 3960Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 3970Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 3980Sstevel@tonic-gate int sflag, /* open state flag */ 3990Sstevel@tonic-gate cred_t *credp) /* credentials */ 4000Sstevel@tonic-gate { 4010Sstevel@tonic-gate mblk_t *mop; 4020Sstevel@tonic-gate struct stroptions *sop; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * The slave side can be opened as many times as needed. 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) { 4080Sstevel@tonic-gate ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs)); 4090Sstevel@tonic-gate return (0); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) { 4130Sstevel@tonic-gate DBG("zc_slave_open(): mop allocation failed\n"); 4140Sstevel@tonic-gate return (ENOMEM); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate zcs->zc_state |= ZC_STATE_SOPEN; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * q_ptr stores driver private data; stash the soft state data on both 4210Sstevel@tonic-gate * read and write sides of the queue. 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = zcs; 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate qprocson(rqp); 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * Must follow qprocson(), since we aren't ready to process until then. 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate zcs->zc_slave_rdq = rqp; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue and add 4340Sstevel@tonic-gate * controlling tty as needed. 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 4370Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 4383813Sdp sop = (struct stroptions *)(void *)mop->b_rptr; 4390Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 4400Sstevel@tonic-gate sop->so_hiwat = 512; 4410Sstevel@tonic-gate sop->so_lowat = 256; 4420Sstevel@tonic-gate putnext(rqp, mop); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate return (0); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate /* 4480Sstevel@tonic-gate * open(9e) entrypoint; checks sflag, and rejects anything unordinary. 4490Sstevel@tonic-gate */ 4500Sstevel@tonic-gate static int 4510Sstevel@tonic-gate zc_open(queue_t *rqp, /* pointer to the read side queue */ 4520Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 4530Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 4540Sstevel@tonic-gate int sflag, /* open state flag */ 4550Sstevel@tonic-gate cred_t *credp) /* credentials */ 4560Sstevel@tonic-gate { 4570Sstevel@tonic-gate int instance = ZC_INSTANCE(*devp); 4580Sstevel@tonic-gate int ret; 4590Sstevel@tonic-gate zc_state_t *zcs; 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate if (sflag != 0) 4620Sstevel@tonic-gate return (EINVAL); 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 4650Sstevel@tonic-gate return (ENXIO); 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate switch (ZC_NODE(*devp)) { 4680Sstevel@tonic-gate case ZC_MASTER_MINOR: 4690Sstevel@tonic-gate ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp); 4700Sstevel@tonic-gate break; 4710Sstevel@tonic-gate case ZC_SLAVE_MINOR: 4720Sstevel@tonic-gate ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp); 4730Sstevel@tonic-gate break; 4740Sstevel@tonic-gate default: 4750Sstevel@tonic-gate ret = ENXIO; 4760Sstevel@tonic-gate break; 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate return (ret); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* 4830Sstevel@tonic-gate * close(9e) entrypoint. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate /*ARGSUSED1*/ 4860Sstevel@tonic-gate static int 4870Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp) 4880Sstevel@tonic-gate { 4890Sstevel@tonic-gate queue_t *wqp; 4900Sstevel@tonic-gate mblk_t *bp; 4910Sstevel@tonic-gate zc_state_t *zcs; 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate zcs = (zc_state_t *)rqp->q_ptr; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if (rqp == zcs->zc_master_rdq) { 4960Sstevel@tonic-gate DBG("Closing master side"); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate zcs->zc_master_rdq = NULL; 4990Sstevel@tonic-gate zcs->zc_state &= ~ZC_STATE_MOPEN; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * qenable slave side write queue so that it can flush 5030Sstevel@tonic-gate * its messages as master's read queue is going away 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate if (zcs->zc_slave_rdq != NULL) { 5060Sstevel@tonic-gate qenable(WR(zcs->zc_slave_rdq)); 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate qprocsoff(rqp); 5100Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = NULL; 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate } else if (rqp == zcs->zc_slave_rdq) { 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate DBG("Closing slave side"); 5150Sstevel@tonic-gate zcs->zc_state &= ~ZC_STATE_SOPEN; 5160Sstevel@tonic-gate zcs->zc_slave_rdq = NULL; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate wqp = WR(rqp); 5190Sstevel@tonic-gate while ((bp = getq(wqp)) != NULL) { 5200Sstevel@tonic-gate if (zcs->zc_master_rdq != NULL) 5210Sstevel@tonic-gate putnext(zcs->zc_master_rdq, bp); 5220Sstevel@tonic-gate else if (bp->b_datap->db_type == M_IOCTL) 5230Sstevel@tonic-gate miocnak(wqp, bp, 0, 0); 5240Sstevel@tonic-gate else 5250Sstevel@tonic-gate freemsg(bp); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* 5290Sstevel@tonic-gate * Qenable master side write queue so that it can flush its 5300Sstevel@tonic-gate * messages as slaves's read queue is going away. 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate if (zcs->zc_master_rdq != NULL) 5330Sstevel@tonic-gate qenable(WR(zcs->zc_master_rdq)); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate qprocsoff(rqp); 5360Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = NULL; 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate return (0); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate static void 5430Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp) 5440Sstevel@tonic-gate { 5450Sstevel@tonic-gate mblk_t *nmp; 5460Sstevel@tonic-gate DBG1("M_FLUSH on %s side", zc_side(qp)); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 5490Sstevel@tonic-gate DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp)); 5500Sstevel@tonic-gate flushq(qp, FLUSHDATA); 5510Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 5520Sstevel@tonic-gate if ((*mp->b_rptr & FLUSHR) == 0) { 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * FLUSHW only. Change to FLUSHR and putnext other side, 5550Sstevel@tonic-gate * then we are done. 5560Sstevel@tonic-gate */ 5570Sstevel@tonic-gate *mp->b_rptr |= FLUSHR; 5580Sstevel@tonic-gate if (zc_switch(RD(qp)) != NULL) { 5590Sstevel@tonic-gate putnext(zc_switch(RD(qp)), mp); 5600Sstevel@tonic-gate return; 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate } else if ((zc_switch(RD(qp)) != NULL) && 5630Sstevel@tonic-gate (nmp = copyb(mp)) != NULL) { 5640Sstevel@tonic-gate /* 5650Sstevel@tonic-gate * It is a FLUSHRW; we copy the mblk and send 5660Sstevel@tonic-gate * it to the other side, since we still need to use 5670Sstevel@tonic-gate * the mblk in FLUSHR processing, below. 5680Sstevel@tonic-gate */ 5690Sstevel@tonic-gate putnext(zc_switch(RD(qp)), nmp); 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 5740Sstevel@tonic-gate DBG("qreply(qp) turning FLUSHR around\n"); 5750Sstevel@tonic-gate qreply(qp, mp); 5760Sstevel@tonic-gate return; 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate freemsg(mp); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate /* 5820Sstevel@tonic-gate * wput(9E) is symmetric for master and slave sides, so this handles both 5830Sstevel@tonic-gate * without splitting the codepath. 5840Sstevel@tonic-gate * 5850Sstevel@tonic-gate * zc_wput() looks at the other side; if there is no process holding that 5860Sstevel@tonic-gate * side open, it frees the message. This prevents processes from hanging 5870Sstevel@tonic-gate * if no one is holding open the console. Otherwise, it putnext's high 5880Sstevel@tonic-gate * priority messages, putnext's normal messages if possible, and otherwise 5890Sstevel@tonic-gate * enqueues the messages; in the case that something is enqueued, wsrv(9E) 5900Sstevel@tonic-gate * will take care of eventually shuttling I/O to the other side. 5910Sstevel@tonic-gate */ 5920Sstevel@tonic-gate static void 5930Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp) 5940Sstevel@tonic-gate { 5950Sstevel@tonic-gate unsigned char type = mp->b_datap->db_type; 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate ASSERT(qp->q_ptr); 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate DBG1("entering zc_wput, %s side", zc_side(qp)); 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (zc_switch(RD(qp)) == NULL) { 6020Sstevel@tonic-gate DBG1("wput to %s side (no one listening)", zc_side(qp)); 6030Sstevel@tonic-gate switch (type) { 6040Sstevel@tonic-gate case M_FLUSH: 6050Sstevel@tonic-gate handle_mflush(qp, mp); 6060Sstevel@tonic-gate break; 6070Sstevel@tonic-gate case M_IOCTL: 6080Sstevel@tonic-gate miocnak(qp, mp, 0, 0); 6090Sstevel@tonic-gate break; 6100Sstevel@tonic-gate default: 6110Sstevel@tonic-gate freemsg(mp); 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate return; 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate if (type >= QPCTL) { 6180Sstevel@tonic-gate DBG1("(hipri) wput, %s side", zc_side(qp)); 6190Sstevel@tonic-gate switch (type) { 6200Sstevel@tonic-gate case M_READ: /* supposedly from ldterm? */ 6210Sstevel@tonic-gate DBG("zc_wput: tossing M_READ\n"); 6220Sstevel@tonic-gate freemsg(mp); 6230Sstevel@tonic-gate break; 6240Sstevel@tonic-gate case M_FLUSH: 6250Sstevel@tonic-gate handle_mflush(qp, mp); 6260Sstevel@tonic-gate break; 6270Sstevel@tonic-gate default: 6280Sstevel@tonic-gate /* 6290Sstevel@tonic-gate * Put this to the other side. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate ASSERT(zc_switch(RD(qp)) != NULL); 6320Sstevel@tonic-gate putnext(zc_switch(RD(qp)), mp); 6330Sstevel@tonic-gate break; 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate DBG1("done (hipri) wput, %s side", zc_side(qp)); 6360Sstevel@tonic-gate return; 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate /* 6400Sstevel@tonic-gate * Only putnext if there isn't already something in the queue. 6410Sstevel@tonic-gate * otherwise things would wind up out of order. 6420Sstevel@tonic-gate */ 6430Sstevel@tonic-gate if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) { 6440Sstevel@tonic-gate DBG("wput: putting message to other side\n"); 6450Sstevel@tonic-gate putnext(RD(zc_switch(qp)), mp); 6460Sstevel@tonic-gate } else { 6470Sstevel@tonic-gate DBG("wput: putting msg onto queue\n"); 6480Sstevel@tonic-gate (void) putq(qp, mp); 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate DBG1("done wput, %s side", zc_side(qp)); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* 6540Sstevel@tonic-gate * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both 6550Sstevel@tonic-gate * without splitting up the codepath. 6560Sstevel@tonic-gate * 6570Sstevel@tonic-gate * Enable the write side of the partner. This triggers the partner to send 6580Sstevel@tonic-gate * messages queued on its write side to this queue's read side. 6590Sstevel@tonic-gate */ 6600Sstevel@tonic-gate static void 6610Sstevel@tonic-gate zc_rsrv(queue_t *qp) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate zc_state_t *zcs; 6640Sstevel@tonic-gate zcs = (zc_state_t *)qp->q_ptr; 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * Care must be taken here, as either of the master or slave side 6680Sstevel@tonic-gate * qptr could be NULL. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq); 6710Sstevel@tonic-gate if (zc_switch(qp) == NULL) { 6720Sstevel@tonic-gate DBG("zc_rsrv: other side isn't listening\n"); 6730Sstevel@tonic-gate return; 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate qenable(WR(zc_switch(qp))); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * This routine is symmetric for master and slave, so it handles both without 6800Sstevel@tonic-gate * splitting up the codepath. 6810Sstevel@tonic-gate * 6820Sstevel@tonic-gate * If there are messages on this queue that can be sent to the other, send 6830Sstevel@tonic-gate * them via putnext(). Else, if queued messages cannot be sent, leave them 6840Sstevel@tonic-gate * on this queue. 6850Sstevel@tonic-gate */ 6860Sstevel@tonic-gate static void 6870Sstevel@tonic-gate zc_wsrv(queue_t *qp) 6880Sstevel@tonic-gate { 6890Sstevel@tonic-gate mblk_t *mp; 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate DBG1("zc_wsrv master (%s) side", zc_side(qp)); 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * Partner has no read queue, so take the data, and throw it away. 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate if (zc_switch(RD(qp)) == NULL) { 6970Sstevel@tonic-gate DBG("zc_wsrv: other side isn't listening"); 6980Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 6990Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL) 7000Sstevel@tonic-gate miocnak(qp, mp, 0, 0); 7010Sstevel@tonic-gate else 7020Sstevel@tonic-gate freemsg(mp); 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate flushq(qp, FLUSHALL); 7050Sstevel@tonic-gate return; 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate /* 7090Sstevel@tonic-gate * while there are messages on this write queue... 7100Sstevel@tonic-gate */ 7110Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 7120Sstevel@tonic-gate /* 7130Sstevel@tonic-gate * Due to the way zc_wput is implemented, we should never 7140Sstevel@tonic-gate * see a control message here. 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate ASSERT(mp->b_datap->db_type < QPCTL); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) { 7190Sstevel@tonic-gate DBG("wsrv: send message to other side\n"); 7200Sstevel@tonic-gate putnext(RD(zc_switch(qp)), mp); 7210Sstevel@tonic-gate } else { 7220Sstevel@tonic-gate DBG("wsrv: putting msg back on queue\n"); 7230Sstevel@tonic-gate (void) putbq(qp, mp); 7240Sstevel@tonic-gate break; 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate } 728