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 /* 228770SJordan.Vaughan@Sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * Zone Console Driver. 290Sstevel@tonic-gate * 300Sstevel@tonic-gate * This driver, derived from the pts/ptm drivers, is the pseudo console driver 310Sstevel@tonic-gate * for system zones. Its implementation is straightforward. Each instance 320Sstevel@tonic-gate * of the driver represents a global-zone/local-zone pair (this maps in a 330Sstevel@tonic-gate * straightforward way to the commonly used terminal notion of "master side" 340Sstevel@tonic-gate * and "slave side", and we use that terminology throughout). 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Instances of zcons are onlined as children of /pseudo/zconsnex@1/ 370Sstevel@tonic-gate * by zoneadmd in userland, using the devctl framework; thus the driver 380Sstevel@tonic-gate * does not need to maintain any sort of "admin" node. 390Sstevel@tonic-gate * 400Sstevel@tonic-gate * The driver shuttles I/O from master side to slave side and back. In a break 410Sstevel@tonic-gate * from the pts/ptm semantics, if one side is not open, I/O directed towards 420Sstevel@tonic-gate * it will simply be discarded. This is so that if zoneadmd is not holding 430Sstevel@tonic-gate * the master side console open (i.e. it has died somehow), processes in 440Sstevel@tonic-gate * the zone do not experience any errors and I/O to the console does not 450Sstevel@tonic-gate * hang. 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * TODO: we may want to revisit the other direction; i.e. we may want 480Sstevel@tonic-gate * zoneadmd to be able to detect whether no zone processes are holding the 490Sstevel@tonic-gate * console open, an unusual situation. 508770SJordan.Vaughan@Sun.com * 518770SJordan.Vaughan@Sun.com * 528770SJordan.Vaughan@Sun.com * 538770SJordan.Vaughan@Sun.com * MASTER SIDE IOCTLS 548770SJordan.Vaughan@Sun.com * 558770SJordan.Vaughan@Sun.com * The ZC_HOLDSLAVE and ZC_RELEASESLAVE ioctls instruct the master side of the 568770SJordan.Vaughan@Sun.com * console to hold and release a reference to the slave side's vnode. They are 578770SJordan.Vaughan@Sun.com * meant to be issued by zoneadmd after the console device node is created and 588770SJordan.Vaughan@Sun.com * before it is destroyed so that the slave's STREAMS anchor, ptem, is 598770SJordan.Vaughan@Sun.com * preserved when ttymon starts popping STREAMS modules from within the 608770SJordan.Vaughan@Sun.com * associated zone. This guarantees that the zone console will always have 618770SJordan.Vaughan@Sun.com * terminal semantics while the zone is running. 628770SJordan.Vaughan@Sun.com * 638770SJordan.Vaughan@Sun.com * Here is the issue: the ptem module is anchored in the zone console 648770SJordan.Vaughan@Sun.com * (slave side) so that processes within the associated non-global zone will 658770SJordan.Vaughan@Sun.com * fail to pop it off, thus ensuring that the slave will retain terminal 668770SJordan.Vaughan@Sun.com * semantics. When a process attempts to pop the anchor off of a stream, the 678770SJordan.Vaughan@Sun.com * STREAMS subsystem checks whether the calling process' zone is the same as 688770SJordan.Vaughan@Sun.com * that of the process that pushed the anchor onto the stream and cancels the 698770SJordan.Vaughan@Sun.com * pop if they differ. zoneadmd used to hold an open file descriptor for the 708770SJordan.Vaughan@Sun.com * slave while the associated non-global zone ran, thus ensuring that the 718770SJordan.Vaughan@Sun.com * slave's STREAMS anchor would never be popped from within the non-global zone 728770SJordan.Vaughan@Sun.com * (because zoneadmd runs in the global zone). However, this file descriptor 738770SJordan.Vaughan@Sun.com * was removed to make zone console management more robust. sad(7D) is now 748770SJordan.Vaughan@Sun.com * used to automatically set up the slave's STREAMS modules when the zone 758770SJordan.Vaughan@Sun.com * console is freshly opened within the associated non-global zone. However, 768770SJordan.Vaughan@Sun.com * when a process within the non-global zone freshly opens the zone console, the 778770SJordan.Vaughan@Sun.com * anchor is pushed from within the non-global zone, making it possible for 788770SJordan.Vaughan@Sun.com * processes within the non-global zone (e.g., ttymon) to pop the anchor and 798770SJordan.Vaughan@Sun.com * destroy the zone console's terminal semantics. 808770SJordan.Vaughan@Sun.com * 818770SJordan.Vaughan@Sun.com * One solution is to make the zcons device hold the slave open while the 828770SJordan.Vaughan@Sun.com * associated non-global zone runs so that the STREAMS anchor will always be 838770SJordan.Vaughan@Sun.com * associated with the global zone. Unfortunately, the slave cannot be opened 848770SJordan.Vaughan@Sun.com * from within the zcons driver because the driver is not reentrant: it has 858770SJordan.Vaughan@Sun.com * an outer STREAMS perimeter. Therefore, the next best option is for zcons to 868770SJordan.Vaughan@Sun.com * provide an ioctl interface to zoneadmd to manage holding and releasing 878770SJordan.Vaughan@Sun.com * the slave side of the console. It is sufficient to hold the slave side's 888770SJordan.Vaughan@Sun.com * vnode and bump the associated snode's reference count to preserve the slave's 898770SJordan.Vaughan@Sun.com * STREAMS configuration while the associated zone runs, so that's what the 908770SJordan.Vaughan@Sun.com * ioctls do. 918770SJordan.Vaughan@Sun.com * 928770SJordan.Vaughan@Sun.com * 938770SJordan.Vaughan@Sun.com * ZC_HOLDSLAVE 948770SJordan.Vaughan@Sun.com * 958770SJordan.Vaughan@Sun.com * This ioctl takes a file descriptor as an argument. It effectively gets a 968770SJordan.Vaughan@Sun.com * reference to the slave side's minor node's vnode and bumps the associated 978770SJordan.Vaughan@Sun.com * snode's reference count. The vnode reference is stored in the zcons device 988770SJordan.Vaughan@Sun.com * node's soft state. This ioctl succeeds if the given file descriptor refers 998770SJordan.Vaughan@Sun.com * to the slave side's minor node or if there is already a reference to the 1008770SJordan.Vaughan@Sun.com * slave side's minor node's vnode in the device's soft state. 1018770SJordan.Vaughan@Sun.com * 1028770SJordan.Vaughan@Sun.com * 1038770SJordan.Vaughan@Sun.com * ZC_RELEASESLAVE 1048770SJordan.Vaughan@Sun.com * 1058770SJordan.Vaughan@Sun.com * This ioctl takes a file descriptor as an argument. It effectively releases 1068770SJordan.Vaughan@Sun.com * the vnode reference stored in the zcons device node's soft state (which was 1078770SJordan.Vaughan@Sun.com * previously acquired via ZC_HOLDSLAVE) and decrements the reference count of 1088770SJordan.Vaughan@Sun.com * the snode associated with the vnode. This ioctl succeeds if the given file 1098770SJordan.Vaughan@Sun.com * descriptor refers to the slave side's minor node or if no reference to the 1108770SJordan.Vaughan@Sun.com * slave side's minor node's vnode is stored in the device's soft state. 1118770SJordan.Vaughan@Sun.com * 1128770SJordan.Vaughan@Sun.com * 1138770SJordan.Vaughan@Sun.com * Note that the file descriptor arguments for both ioctls must be cast to 1148770SJordan.Vaughan@Sun.com * integers of pointer width. 1158770SJordan.Vaughan@Sun.com * 1168770SJordan.Vaughan@Sun.com * Here's how the dance between zcons and zoneadmd works: 1178770SJordan.Vaughan@Sun.com * 1188770SJordan.Vaughan@Sun.com * Zone boot: 1198770SJordan.Vaughan@Sun.com * 1. While booting the zone, zoneadmd creates an instance of zcons. 1208770SJordan.Vaughan@Sun.com * 2. zoneadmd opens the master and slave sides of the new zone console 1218770SJordan.Vaughan@Sun.com * and issues the ZC_HOLDSLAVE ioctl on the master side, passing its 1228770SJordan.Vaughan@Sun.com * file descriptor for the slave side as the ioctl argument. 1238770SJordan.Vaughan@Sun.com * 3. zcons holds the slave side's vnode, bumps the snode's reference 1248770SJordan.Vaughan@Sun.com * count, and stores a pointer to the vnode in the device's soft 1258770SJordan.Vaughan@Sun.com * state. 1268770SJordan.Vaughan@Sun.com * 4. zoneadmd closes the master and slave sides and continues to boot 1278770SJordan.Vaughan@Sun.com * the zone. 1288770SJordan.Vaughan@Sun.com * 1298770SJordan.Vaughan@Sun.com * Zone halt: 1308770SJordan.Vaughan@Sun.com * 1. While halting the zone, zoneadmd opens the master and slave sides 1318770SJordan.Vaughan@Sun.com * of the zone's console and issues the ZC_RELEASESLAVE ioctl on the 1328770SJordan.Vaughan@Sun.com * master side, passing its file descriptor for the slave side as the 1338770SJordan.Vaughan@Sun.com * ioctl argument. 1348770SJordan.Vaughan@Sun.com * 2. zcons decrements the slave side's snode's reference count, releases 1358770SJordan.Vaughan@Sun.com * the slave's vnode, and eliminates its reference to the vnode in the 1368770SJordan.Vaughan@Sun.com * device's soft state. 1378770SJordan.Vaughan@Sun.com * 3. zoneadmd closes the master and slave sides. 1388770SJordan.Vaughan@Sun.com * 4. zoneadmd destroys the zcons device and continues to halt the zone. 1398770SJordan.Vaughan@Sun.com * 1408770SJordan.Vaughan@Sun.com * It is necessary for zoneadmd to hold the slave open while issuing 1418770SJordan.Vaughan@Sun.com * ZC_RELEASESLAVE because zcons might otherwise release the last reference to 1428770SJordan.Vaughan@Sun.com * the slave's vnode. If it does, then specfs will panic because it will expect 1438770SJordan.Vaughan@Sun.com * that the STREAMS configuration for the vnode was destroyed, which VN_RELE 1448770SJordan.Vaughan@Sun.com * doesn't do. Forcing zoneadmd to hold the slave open guarantees that zcons 1458770SJordan.Vaughan@Sun.com * won't release the vnode's last reference. zoneadmd will properly destroy the 1468770SJordan.Vaughan@Sun.com * vnode and the snode when it closes the file descriptor. 1478770SJordan.Vaughan@Sun.com * 1488770SJordan.Vaughan@Sun.com * Technically, any process that can access the master side can issue these 1498770SJordan.Vaughan@Sun.com * ioctls, but they should be treated as private interfaces for zoneadmd. 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate #include <sys/types.h> 1530Sstevel@tonic-gate #include <sys/cmn_err.h> 1540Sstevel@tonic-gate #include <sys/conf.h> 1550Sstevel@tonic-gate #include <sys/cred.h> 1560Sstevel@tonic-gate #include <sys/ddi.h> 1570Sstevel@tonic-gate #include <sys/debug.h> 1580Sstevel@tonic-gate #include <sys/devops.h> 1590Sstevel@tonic-gate #include <sys/errno.h> 1600Sstevel@tonic-gate #include <sys/file.h> 1618770SJordan.Vaughan@Sun.com #include <sys/kstr.h> 1620Sstevel@tonic-gate #include <sys/modctl.h> 1630Sstevel@tonic-gate #include <sys/param.h> 1640Sstevel@tonic-gate #include <sys/stat.h> 1650Sstevel@tonic-gate #include <sys/stream.h> 1660Sstevel@tonic-gate #include <sys/stropts.h> 1670Sstevel@tonic-gate #include <sys/strsun.h> 1680Sstevel@tonic-gate #include <sys/sunddi.h> 1690Sstevel@tonic-gate #include <sys/sysmacros.h> 1700Sstevel@tonic-gate #include <sys/systm.h> 1710Sstevel@tonic-gate #include <sys/types.h> 1720Sstevel@tonic-gate #include <sys/zcons.h> 1738770SJordan.Vaughan@Sun.com #include <sys/vnode.h> 1748770SJordan.Vaughan@Sun.com #include <sys/fs/snode.h> 175*10670SJordan.Vaughan@Sun.com #include <sys/zone.h> 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 1780Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t); 1790Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *); 1820Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *); 1830Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *); 1840Sstevel@tonic-gate static void zc_rsrv(queue_t *); 1850Sstevel@tonic-gate static void zc_wsrv(queue_t *); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* 1880Sstevel@tonic-gate * The instance number is encoded in the dev_t in the minor number; the lowest 1890Sstevel@tonic-gate * bit of the minor number is used to track the master vs. slave side of the 1900Sstevel@tonic-gate * virtual console. The rest of the bits in the minor number are the instance. 1910Sstevel@tonic-gate */ 1928770SJordan.Vaughan@Sun.com #define ZC_MASTER_MINOR 0 1938770SJordan.Vaughan@Sun.com #define ZC_SLAVE_MINOR 1 1948770SJordan.Vaughan@Sun.com 1958770SJordan.Vaughan@Sun.com #define ZC_INSTANCE(x) (getminor((x)) >> 1) 1968770SJordan.Vaughan@Sun.com #define ZC_NODE(x) (getminor((x)) & 0x01) 1970Sstevel@tonic-gate 1988770SJordan.Vaughan@Sun.com /* 1998770SJordan.Vaughan@Sun.com * This macro converts a zc_state_t pointer to the associated slave minor node's 2008770SJordan.Vaughan@Sun.com * dev_t. 2018770SJordan.Vaughan@Sun.com */ 2028770SJordan.Vaughan@Sun.com #define ZC_STATE_TO_SLAVEDEV(x) (makedevice(ddi_driver_major((x)->zc_devinfo), \ 2038770SJordan.Vaughan@Sun.com (minor_t)(ddi_get_instance((x)->zc_devinfo) << 1 | ZC_SLAVE_MINOR))) 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate int zcons_debug = 0; 2060Sstevel@tonic-gate #define DBG(a) if (zcons_debug) cmn_err(CE_NOTE, a) 2070Sstevel@tonic-gate #define DBG1(a, b) if (zcons_debug) cmn_err(CE_NOTE, a, b) 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * Zone Console Pseudo Terminal Module: stream data structure definitions 2120Sstevel@tonic-gate */ 2130Sstevel@tonic-gate static struct module_info zc_info = { 2140Sstevel@tonic-gate 31337, /* c0z we r hAx0rs */ 2150Sstevel@tonic-gate "zcons", 2160Sstevel@tonic-gate 0, 2170Sstevel@tonic-gate INFPSZ, 2180Sstevel@tonic-gate 2048, 2190Sstevel@tonic-gate 128 2200Sstevel@tonic-gate }; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate static struct qinit zc_rinit = { 2230Sstevel@tonic-gate NULL, 2240Sstevel@tonic-gate (int (*)()) zc_rsrv, 2250Sstevel@tonic-gate zc_open, 2260Sstevel@tonic-gate zc_close, 2270Sstevel@tonic-gate NULL, 2280Sstevel@tonic-gate &zc_info, 2290Sstevel@tonic-gate NULL 2300Sstevel@tonic-gate }; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate static struct qinit zc_winit = { 2330Sstevel@tonic-gate (int (*)()) zc_wput, 2340Sstevel@tonic-gate (int (*)()) zc_wsrv, 2350Sstevel@tonic-gate NULL, 2360Sstevel@tonic-gate NULL, 2370Sstevel@tonic-gate NULL, 2380Sstevel@tonic-gate &zc_info, 2390Sstevel@tonic-gate NULL 2400Sstevel@tonic-gate }; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate static struct streamtab zc_tab_info = { 2430Sstevel@tonic-gate &zc_rinit, 2440Sstevel@tonic-gate &zc_winit, 2450Sstevel@tonic-gate NULL, 2460Sstevel@tonic-gate NULL 2470Sstevel@tonic-gate }; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate #define ZC_CONF_FLAG (D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL) 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops) 2530Sstevel@tonic-gate */ 2540Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev, zc_attach, zc_detach, nodev, \ 2557656SSherry.Moore@Sun.COM zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* 2580Sstevel@tonic-gate * Module linkage information for the kernel. 2590Sstevel@tonic-gate */ 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate static struct modldrv modldrv = { 2624959Sedp &mod_driverops, /* Type of module (this is a pseudo driver) */ 2634959Sedp "Zone console driver", /* description of module */ 2644959Sedp &zc_ops /* driver ops */ 2650Sstevel@tonic-gate }; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate static struct modlinkage modlinkage = { 2680Sstevel@tonic-gate MODREV_1, 2690Sstevel@tonic-gate &modldrv, 2700Sstevel@tonic-gate NULL 2710Sstevel@tonic-gate }; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate typedef struct zc_state { 2740Sstevel@tonic-gate dev_info_t *zc_devinfo; 2750Sstevel@tonic-gate queue_t *zc_master_rdq; 2760Sstevel@tonic-gate queue_t *zc_slave_rdq; 2778770SJordan.Vaughan@Sun.com vnode_t *zc_slave_vnode; 2780Sstevel@tonic-gate int zc_state; 2790Sstevel@tonic-gate } zc_state_t; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate #define ZC_STATE_MOPEN 0x01 2820Sstevel@tonic-gate #define ZC_STATE_SOPEN 0x02 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate static void *zc_soft_state; 2850Sstevel@tonic-gate 2868770SJordan.Vaughan@Sun.com /* 2878770SJordan.Vaughan@Sun.com * List of STREAMS modules that should be pushed onto every slave instance. 2888770SJordan.Vaughan@Sun.com */ 2898770SJordan.Vaughan@Sun.com static char *zcons_mods[] = { 2908770SJordan.Vaughan@Sun.com "ptem", 2918770SJordan.Vaughan@Sun.com "ldterm", 2928770SJordan.Vaughan@Sun.com "ttcompat", 2938770SJordan.Vaughan@Sun.com NULL 2948770SJordan.Vaughan@Sun.com }; 2958770SJordan.Vaughan@Sun.com 2960Sstevel@tonic-gate int 2970Sstevel@tonic-gate _init(void) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate int err; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate if ((err = ddi_soft_state_init(&zc_soft_state, 3020Sstevel@tonic-gate sizeof (zc_state_t), 0)) != 0) { 3030Sstevel@tonic-gate return (err); 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate if ((err = mod_install(&modlinkage)) != 0) 3070Sstevel@tonic-gate ddi_soft_state_fini(zc_soft_state); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate return (err); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate int 3140Sstevel@tonic-gate _fini(void) 3150Sstevel@tonic-gate { 3160Sstevel@tonic-gate int err; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate if ((err = mod_remove(&modlinkage)) != 0) { 3190Sstevel@tonic-gate return (err); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate ddi_soft_state_fini(&zc_soft_state); 3230Sstevel@tonic-gate return (0); 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate int 3270Sstevel@tonic-gate _info(struct modinfo *modinfop) 3280Sstevel@tonic-gate { 3290Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate static int 3330Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3340Sstevel@tonic-gate { 3350Sstevel@tonic-gate zc_state_t *zcs; 3360Sstevel@tonic-gate int instance; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate if (cmd != DDI_ATTACH) 3390Sstevel@tonic-gate return (DDI_FAILURE); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate instance = ddi_get_instance(dip); 3420Sstevel@tonic-gate if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS) 3430Sstevel@tonic-gate return (DDI_FAILURE); 3440Sstevel@tonic-gate 3458770SJordan.Vaughan@Sun.com /* 3468770SJordan.Vaughan@Sun.com * Create the master and slave minor nodes. 3478770SJordan.Vaughan@Sun.com */ 348*10670SJordan.Vaughan@Sun.com if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR, 349*10670SJordan.Vaughan@Sun.com instance << 1 | ZC_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) || 3508770SJordan.Vaughan@Sun.com (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR, 351*10670SJordan.Vaughan@Sun.com instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) { 352*10670SJordan.Vaughan@Sun.com ddi_remove_minor_node(dip, NULL); 353*10670SJordan.Vaughan@Sun.com ddi_soft_state_free(zc_soft_state, instance); 354*10670SJordan.Vaughan@Sun.com return (DDI_FAILURE); 355*10670SJordan.Vaughan@Sun.com } 3568770SJordan.Vaughan@Sun.com 3578770SJordan.Vaughan@Sun.com VERIFY((zcs = ddi_get_soft_state(zc_soft_state, instance)) != NULL); 3580Sstevel@tonic-gate zcs->zc_devinfo = dip; 3598770SJordan.Vaughan@Sun.com return (DDI_SUCCESS); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate static int 3630Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate zc_state_t *zcs; 3660Sstevel@tonic-gate int instance; 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate if (cmd != DDI_DETACH) 3690Sstevel@tonic-gate return (DDI_FAILURE); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate instance = ddi_get_instance(dip); 3720Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 3730Sstevel@tonic-gate return (DDI_FAILURE); 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_MOPEN) || 3760Sstevel@tonic-gate (zcs->zc_state & ZC_STATE_SOPEN)) { 3770Sstevel@tonic-gate DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip); 3780Sstevel@tonic-gate return (DDI_FAILURE); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 3820Sstevel@tonic-gate ddi_soft_state_free(zc_soft_state, instance); 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate return (DDI_SUCCESS); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* 3880Sstevel@tonic-gate * zc_getinfo() 3890Sstevel@tonic-gate * getinfo(9e) entrypoint. 3900Sstevel@tonic-gate */ 3910Sstevel@tonic-gate /*ARGSUSED*/ 3920Sstevel@tonic-gate static int 3930Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 3940Sstevel@tonic-gate { 3950Sstevel@tonic-gate zc_state_t *zcs; 3960Sstevel@tonic-gate int instance = ZC_INSTANCE((dev_t)arg); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate switch (infocmd) { 3990Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 4000Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 4010Sstevel@tonic-gate return (DDI_FAILURE); 4020Sstevel@tonic-gate *result = zcs->zc_devinfo; 4030Sstevel@tonic-gate return (DDI_SUCCESS); 4040Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 4050Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 4060Sstevel@tonic-gate return (DDI_SUCCESS); 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate return (DDI_FAILURE); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * Return the equivalent queue from the other side of the relationship. 4130Sstevel@tonic-gate * e.g.: given the slave's write queue, return the master's write queue. 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate static queue_t * 4160Sstevel@tonic-gate zc_switch(queue_t *qp) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate zc_state_t *zcs = qp->q_ptr; 4190Sstevel@tonic-gate ASSERT(zcs != NULL); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate if (qp == zcs->zc_master_rdq) 4220Sstevel@tonic-gate return (zcs->zc_slave_rdq); 4230Sstevel@tonic-gate else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL) 4240Sstevel@tonic-gate return (OTHERQ(zcs->zc_slave_rdq)); 4250Sstevel@tonic-gate else if (qp == zcs->zc_slave_rdq) 4260Sstevel@tonic-gate return (zcs->zc_master_rdq); 4270Sstevel@tonic-gate else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL) 4280Sstevel@tonic-gate return (OTHERQ(zcs->zc_master_rdq)); 4290Sstevel@tonic-gate else 4300Sstevel@tonic-gate return (NULL); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * For debugging and outputting messages. Returns the name of the side of 4350Sstevel@tonic-gate * the relationship associated with this queue. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate static const char * 4380Sstevel@tonic-gate zc_side(queue_t *qp) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate zc_state_t *zcs = qp->q_ptr; 4410Sstevel@tonic-gate ASSERT(zcs != NULL); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate if (qp == zcs->zc_master_rdq || 4440Sstevel@tonic-gate OTHERQ(qp) == zcs->zc_master_rdq) { 4450Sstevel@tonic-gate return ("master"); 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq); 4480Sstevel@tonic-gate return ("slave"); 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /*ARGSUSED*/ 4520Sstevel@tonic-gate static int 4530Sstevel@tonic-gate zc_master_open(zc_state_t *zcs, 4540Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 4550Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 4560Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 4570Sstevel@tonic-gate int sflag, /* open state flag */ 4580Sstevel@tonic-gate cred_t *credp) /* credentials */ 4590Sstevel@tonic-gate { 4600Sstevel@tonic-gate mblk_t *mop; 4610Sstevel@tonic-gate struct stroptions *sop; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * Enforce exclusivity on the master side; the only consumer should 4650Sstevel@tonic-gate * be the zoneadmd for the zone. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_MOPEN) != 0) 4680Sstevel@tonic-gate return (EBUSY); 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) { 4710Sstevel@tonic-gate DBG("zc_master_open(): mop allocation failed\n"); 4720Sstevel@tonic-gate return (ENOMEM); 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate zcs->zc_state |= ZC_STATE_MOPEN; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate /* 4780Sstevel@tonic-gate * q_ptr stores driver private data; stash the soft state data on both 4790Sstevel@tonic-gate * read and write sides of the queue. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = zcs; 4820Sstevel@tonic-gate qprocson(rqp); 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate /* 4850Sstevel@tonic-gate * Following qprocson(), the master side is fully plumbed into the 4860Sstevel@tonic-gate * STREAM and may send/receive messages. Setting zcs->zc_master_rdq 4870Sstevel@tonic-gate * will allow the slave to send messages to us (the master). 4880Sstevel@tonic-gate * This cannot occur before qprocson() because the master is not 4890Sstevel@tonic-gate * ready to process them until that point. 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate zcs->zc_master_rdq = rqp; 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate /* 4940Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue and add 4950Sstevel@tonic-gate * controlling tty as needed. 4960Sstevel@tonic-gate */ 4970Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 4980Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 4993813Sdp sop = (struct stroptions *)(void *)mop->b_rptr; 5000Sstevel@tonic-gate if (oflag & FNOCTTY) 5010Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT; 5020Sstevel@tonic-gate else 5030Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 5040Sstevel@tonic-gate sop->so_hiwat = 512; 5050Sstevel@tonic-gate sop->so_lowat = 256; 5060Sstevel@tonic-gate putnext(rqp, mop); 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate return (0); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /*ARGSUSED*/ 5120Sstevel@tonic-gate static int 5130Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs, 5140Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 5150Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 5160Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 5170Sstevel@tonic-gate int sflag, /* open state flag */ 5180Sstevel@tonic-gate cred_t *credp) /* credentials */ 5190Sstevel@tonic-gate { 5200Sstevel@tonic-gate mblk_t *mop; 5210Sstevel@tonic-gate struct stroptions *sop; 522*10670SJordan.Vaughan@Sun.com major_t major; 523*10670SJordan.Vaughan@Sun.com minor_t minor; 524*10670SJordan.Vaughan@Sun.com minor_t lastminor; 525*10670SJordan.Vaughan@Sun.com uint_t anchorindex; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate /* 5280Sstevel@tonic-gate * The slave side can be opened as many times as needed. 5290Sstevel@tonic-gate */ 5300Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) { 5310Sstevel@tonic-gate ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs)); 5320Sstevel@tonic-gate return (0); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 535*10670SJordan.Vaughan@Sun.com /* 536*10670SJordan.Vaughan@Sun.com * Set up sad(7D) so that the necessary STREAMS modules will be in 537*10670SJordan.Vaughan@Sun.com * place. A wrinkle is that 'ptem' must be anchored 538*10670SJordan.Vaughan@Sun.com * in place (see streamio(7i)) because we always want the console to 539*10670SJordan.Vaughan@Sun.com * have terminal semantics. 540*10670SJordan.Vaughan@Sun.com */ 541*10670SJordan.Vaughan@Sun.com minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SLAVE_MINOR; 542*10670SJordan.Vaughan@Sun.com major = ddi_driver_major(zcs->zc_devinfo); 543*10670SJordan.Vaughan@Sun.com lastminor = 0; 544*10670SJordan.Vaughan@Sun.com anchorindex = 1; 545*10670SJordan.Vaughan@Sun.com if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor, 546*10670SJordan.Vaughan@Sun.com &anchorindex, zcons_mods) != 0) { 547*10670SJordan.Vaughan@Sun.com DBG("zc_slave_open(): kstr_autopush() failed\n"); 548*10670SJordan.Vaughan@Sun.com return (EIO); 549*10670SJordan.Vaughan@Sun.com } 550*10670SJordan.Vaughan@Sun.com 5510Sstevel@tonic-gate if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) { 5520Sstevel@tonic-gate DBG("zc_slave_open(): mop allocation failed\n"); 5530Sstevel@tonic-gate return (ENOMEM); 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate zcs->zc_state |= ZC_STATE_SOPEN; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate /* 5590Sstevel@tonic-gate * q_ptr stores driver private data; stash the soft state data on both 5600Sstevel@tonic-gate * read and write sides of the queue. 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = zcs; 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate qprocson(rqp); 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate /* 5670Sstevel@tonic-gate * Must follow qprocson(), since we aren't ready to process until then. 5680Sstevel@tonic-gate */ 5690Sstevel@tonic-gate zcs->zc_slave_rdq = rqp; 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate /* 5720Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue and add 5730Sstevel@tonic-gate * controlling tty as needed. 5740Sstevel@tonic-gate */ 5750Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 5760Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 5773813Sdp sop = (struct stroptions *)(void *)mop->b_rptr; 5780Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 5790Sstevel@tonic-gate sop->so_hiwat = 512; 5800Sstevel@tonic-gate sop->so_lowat = 256; 5810Sstevel@tonic-gate putnext(rqp, mop); 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate return (0); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* 5870Sstevel@tonic-gate * open(9e) entrypoint; checks sflag, and rejects anything unordinary. 5880Sstevel@tonic-gate */ 5890Sstevel@tonic-gate static int 5900Sstevel@tonic-gate zc_open(queue_t *rqp, /* pointer to the read side queue */ 5910Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 5920Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 5930Sstevel@tonic-gate int sflag, /* open state flag */ 5940Sstevel@tonic-gate cred_t *credp) /* credentials */ 5950Sstevel@tonic-gate { 5960Sstevel@tonic-gate int instance = ZC_INSTANCE(*devp); 5970Sstevel@tonic-gate int ret; 5980Sstevel@tonic-gate zc_state_t *zcs; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate if (sflag != 0) 6010Sstevel@tonic-gate return (EINVAL); 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 6040Sstevel@tonic-gate return (ENXIO); 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate switch (ZC_NODE(*devp)) { 6070Sstevel@tonic-gate case ZC_MASTER_MINOR: 6080Sstevel@tonic-gate ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp); 6090Sstevel@tonic-gate break; 6100Sstevel@tonic-gate case ZC_SLAVE_MINOR: 6110Sstevel@tonic-gate ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp); 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate default: 6140Sstevel@tonic-gate ret = ENXIO; 6150Sstevel@tonic-gate break; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate return (ret); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate /* 6220Sstevel@tonic-gate * close(9e) entrypoint. 6230Sstevel@tonic-gate */ 6240Sstevel@tonic-gate /*ARGSUSED1*/ 6250Sstevel@tonic-gate static int 6260Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp) 6270Sstevel@tonic-gate { 6280Sstevel@tonic-gate queue_t *wqp; 6290Sstevel@tonic-gate mblk_t *bp; 6300Sstevel@tonic-gate zc_state_t *zcs; 631*10670SJordan.Vaughan@Sun.com major_t major; 632*10670SJordan.Vaughan@Sun.com minor_t minor; 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate zcs = (zc_state_t *)rqp->q_ptr; 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (rqp == zcs->zc_master_rdq) { 6370Sstevel@tonic-gate DBG("Closing master side"); 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate zcs->zc_master_rdq = NULL; 6400Sstevel@tonic-gate zcs->zc_state &= ~ZC_STATE_MOPEN; 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * qenable slave side write queue so that it can flush 6440Sstevel@tonic-gate * its messages as master's read queue is going away 6450Sstevel@tonic-gate */ 6460Sstevel@tonic-gate if (zcs->zc_slave_rdq != NULL) { 6470Sstevel@tonic-gate qenable(WR(zcs->zc_slave_rdq)); 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate qprocsoff(rqp); 6510Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = NULL; 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate } else if (rqp == zcs->zc_slave_rdq) { 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate DBG("Closing slave side"); 6560Sstevel@tonic-gate zcs->zc_state &= ~ZC_STATE_SOPEN; 6570Sstevel@tonic-gate zcs->zc_slave_rdq = NULL; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate wqp = WR(rqp); 6600Sstevel@tonic-gate while ((bp = getq(wqp)) != NULL) { 6610Sstevel@tonic-gate if (zcs->zc_master_rdq != NULL) 6620Sstevel@tonic-gate putnext(zcs->zc_master_rdq, bp); 6630Sstevel@tonic-gate else if (bp->b_datap->db_type == M_IOCTL) 6640Sstevel@tonic-gate miocnak(wqp, bp, 0, 0); 6650Sstevel@tonic-gate else 6660Sstevel@tonic-gate freemsg(bp); 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate /* 6700Sstevel@tonic-gate * Qenable master side write queue so that it can flush its 6710Sstevel@tonic-gate * messages as slaves's read queue is going away. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate if (zcs->zc_master_rdq != NULL) 6740Sstevel@tonic-gate qenable(WR(zcs->zc_master_rdq)); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate qprocsoff(rqp); 6770Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = NULL; 678*10670SJordan.Vaughan@Sun.com 679*10670SJordan.Vaughan@Sun.com /* 680*10670SJordan.Vaughan@Sun.com * Clear the sad configuration so that reopening doesn't fail 681*10670SJordan.Vaughan@Sun.com * to set up sad configuration. 682*10670SJordan.Vaughan@Sun.com */ 683*10670SJordan.Vaughan@Sun.com major = ddi_driver_major(zcs->zc_devinfo); 684*10670SJordan.Vaughan@Sun.com minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SLAVE_MINOR; 685*10670SJordan.Vaughan@Sun.com (void) kstr_autopush(CLR_AUTOPUSH, &major, &minor, NULL, NULL, 686*10670SJordan.Vaughan@Sun.com NULL); 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate return (0); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate static void 6930Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp) 6940Sstevel@tonic-gate { 6950Sstevel@tonic-gate mblk_t *nmp; 6960Sstevel@tonic-gate DBG1("M_FLUSH on %s side", zc_side(qp)); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 6990Sstevel@tonic-gate DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp)); 7000Sstevel@tonic-gate flushq(qp, FLUSHDATA); 7010Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 7020Sstevel@tonic-gate if ((*mp->b_rptr & FLUSHR) == 0) { 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * FLUSHW only. Change to FLUSHR and putnext other side, 7050Sstevel@tonic-gate * then we are done. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate *mp->b_rptr |= FLUSHR; 7080Sstevel@tonic-gate if (zc_switch(RD(qp)) != NULL) { 7090Sstevel@tonic-gate putnext(zc_switch(RD(qp)), mp); 7100Sstevel@tonic-gate return; 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate } else if ((zc_switch(RD(qp)) != NULL) && 7130Sstevel@tonic-gate (nmp = copyb(mp)) != NULL) { 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * It is a FLUSHRW; we copy the mblk and send 7160Sstevel@tonic-gate * it to the other side, since we still need to use 7170Sstevel@tonic-gate * the mblk in FLUSHR processing, below. 7180Sstevel@tonic-gate */ 7190Sstevel@tonic-gate putnext(zc_switch(RD(qp)), nmp); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 7240Sstevel@tonic-gate DBG("qreply(qp) turning FLUSHR around\n"); 7250Sstevel@tonic-gate qreply(qp, mp); 7260Sstevel@tonic-gate return; 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate freemsg(mp); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * wput(9E) is symmetric for master and slave sides, so this handles both 7338770SJordan.Vaughan@Sun.com * without splitting the codepath. (The only exception to this is the 7348770SJordan.Vaughan@Sun.com * processing of zcons ioctls, which is restricted to the master side.) 7350Sstevel@tonic-gate * 7360Sstevel@tonic-gate * zc_wput() looks at the other side; if there is no process holding that 7370Sstevel@tonic-gate * side open, it frees the message. This prevents processes from hanging 7380Sstevel@tonic-gate * if no one is holding open the console. Otherwise, it putnext's high 7390Sstevel@tonic-gate * priority messages, putnext's normal messages if possible, and otherwise 7400Sstevel@tonic-gate * enqueues the messages; in the case that something is enqueued, wsrv(9E) 7410Sstevel@tonic-gate * will take care of eventually shuttling I/O to the other side. 7420Sstevel@tonic-gate */ 7430Sstevel@tonic-gate static void 7440Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp) 7450Sstevel@tonic-gate { 7460Sstevel@tonic-gate unsigned char type = mp->b_datap->db_type; 7478770SJordan.Vaughan@Sun.com zc_state_t *zcs; 7488770SJordan.Vaughan@Sun.com struct iocblk *iocbp; 7498770SJordan.Vaughan@Sun.com file_t *slave_filep; 7508770SJordan.Vaughan@Sun.com struct snode *slave_snodep; 7518770SJordan.Vaughan@Sun.com int slave_fd; 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate ASSERT(qp->q_ptr); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate DBG1("entering zc_wput, %s side", zc_side(qp)); 7560Sstevel@tonic-gate 7578770SJordan.Vaughan@Sun.com /* 7588770SJordan.Vaughan@Sun.com * Process zcons ioctl messages if qp is the master console's write 7598770SJordan.Vaughan@Sun.com * queue. 7608770SJordan.Vaughan@Sun.com */ 7618770SJordan.Vaughan@Sun.com zcs = (zc_state_t *)qp->q_ptr; 7628770SJordan.Vaughan@Sun.com if (zcs->zc_master_rdq != NULL && qp == WR(zcs->zc_master_rdq) && 7638770SJordan.Vaughan@Sun.com type == M_IOCTL) { 7648770SJordan.Vaughan@Sun.com iocbp = (struct iocblk *)(void *)mp->b_rptr; 7658770SJordan.Vaughan@Sun.com switch (iocbp->ioc_cmd) { 7668770SJordan.Vaughan@Sun.com case ZC_HOLDSLAVE: 7678770SJordan.Vaughan@Sun.com /* 7688770SJordan.Vaughan@Sun.com * Hold the slave's vnode and increment the refcount 7698770SJordan.Vaughan@Sun.com * of the snode. If the vnode is already held, then 7708770SJordan.Vaughan@Sun.com * indicate success. 7718770SJordan.Vaughan@Sun.com */ 7728770SJordan.Vaughan@Sun.com if (iocbp->ioc_count != TRANSPARENT) { 7738770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 7748770SJordan.Vaughan@Sun.com return; 7758770SJordan.Vaughan@Sun.com } 7768770SJordan.Vaughan@Sun.com if (zcs->zc_slave_vnode != NULL) { 7778770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 7788770SJordan.Vaughan@Sun.com return; 7798770SJordan.Vaughan@Sun.com } 7808770SJordan.Vaughan@Sun.com 7818770SJordan.Vaughan@Sun.com /* 782*10670SJordan.Vaughan@Sun.com * The process that passed the ioctl must be running in 783*10670SJordan.Vaughan@Sun.com * the global zone. 784*10670SJordan.Vaughan@Sun.com */ 785*10670SJordan.Vaughan@Sun.com if (curzone != global_zone) { 786*10670SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 787*10670SJordan.Vaughan@Sun.com return; 788*10670SJordan.Vaughan@Sun.com } 789*10670SJordan.Vaughan@Sun.com 790*10670SJordan.Vaughan@Sun.com /* 7918770SJordan.Vaughan@Sun.com * The calling process must pass a file descriptor for 7928770SJordan.Vaughan@Sun.com * the slave device. 7938770SJordan.Vaughan@Sun.com */ 7948770SJordan.Vaughan@Sun.com slave_fd = 7958770SJordan.Vaughan@Sun.com (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont-> 7968770SJordan.Vaughan@Sun.com b_rptr; 7978770SJordan.Vaughan@Sun.com slave_filep = getf(slave_fd); 7988770SJordan.Vaughan@Sun.com if (slave_filep == NULL) { 7998770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 8008770SJordan.Vaughan@Sun.com return; 8018770SJordan.Vaughan@Sun.com } 8028770SJordan.Vaughan@Sun.com if (ZC_STATE_TO_SLAVEDEV(zcs) != 8038770SJordan.Vaughan@Sun.com slave_filep->f_vnode->v_rdev) { 8048770SJordan.Vaughan@Sun.com releasef(slave_fd); 8058770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 8068770SJordan.Vaughan@Sun.com return; 8078770SJordan.Vaughan@Sun.com } 8088770SJordan.Vaughan@Sun.com 8098770SJordan.Vaughan@Sun.com /* 8108770SJordan.Vaughan@Sun.com * Get a reference to the slave's vnode. Also bump the 8118770SJordan.Vaughan@Sun.com * reference count on the associated snode. 8128770SJordan.Vaughan@Sun.com */ 8138770SJordan.Vaughan@Sun.com ASSERT(vn_matchops(slave_filep->f_vnode, 8148770SJordan.Vaughan@Sun.com spec_getvnodeops())); 8158770SJordan.Vaughan@Sun.com zcs->zc_slave_vnode = slave_filep->f_vnode; 8168770SJordan.Vaughan@Sun.com VN_HOLD(zcs->zc_slave_vnode); 8178770SJordan.Vaughan@Sun.com slave_snodep = VTOCS(zcs->zc_slave_vnode); 8188770SJordan.Vaughan@Sun.com mutex_enter(&slave_snodep->s_lock); 8198770SJordan.Vaughan@Sun.com ++slave_snodep->s_count; 8208770SJordan.Vaughan@Sun.com mutex_exit(&slave_snodep->s_lock); 8218770SJordan.Vaughan@Sun.com releasef(slave_fd); 8228770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 8238770SJordan.Vaughan@Sun.com return; 8248770SJordan.Vaughan@Sun.com case ZC_RELEASESLAVE: 8258770SJordan.Vaughan@Sun.com /* 8268770SJordan.Vaughan@Sun.com * Release the master's handle on the slave's vnode. 8278770SJordan.Vaughan@Sun.com * If there isn't a handle for the vnode, then indicate 8288770SJordan.Vaughan@Sun.com * success. 8298770SJordan.Vaughan@Sun.com */ 8308770SJordan.Vaughan@Sun.com if (iocbp->ioc_count != TRANSPARENT) { 8318770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 8328770SJordan.Vaughan@Sun.com return; 8338770SJordan.Vaughan@Sun.com } 8348770SJordan.Vaughan@Sun.com if (zcs->zc_slave_vnode == NULL) { 8358770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 8368770SJordan.Vaughan@Sun.com return; 8378770SJordan.Vaughan@Sun.com } 8388770SJordan.Vaughan@Sun.com 8398770SJordan.Vaughan@Sun.com /* 840*10670SJordan.Vaughan@Sun.com * The process that passed the ioctl must be running in 841*10670SJordan.Vaughan@Sun.com * the global zone. 842*10670SJordan.Vaughan@Sun.com */ 843*10670SJordan.Vaughan@Sun.com if (curzone != global_zone) { 844*10670SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 845*10670SJordan.Vaughan@Sun.com return; 846*10670SJordan.Vaughan@Sun.com } 847*10670SJordan.Vaughan@Sun.com 848*10670SJordan.Vaughan@Sun.com /* 8498770SJordan.Vaughan@Sun.com * The process that passed the ioctl must have provided 8508770SJordan.Vaughan@Sun.com * a file descriptor for the slave device. Make sure 8518770SJordan.Vaughan@Sun.com * this is correct. 8528770SJordan.Vaughan@Sun.com */ 8538770SJordan.Vaughan@Sun.com slave_fd = 8548770SJordan.Vaughan@Sun.com (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont-> 8558770SJordan.Vaughan@Sun.com b_rptr; 8568770SJordan.Vaughan@Sun.com slave_filep = getf(slave_fd); 8578770SJordan.Vaughan@Sun.com if (slave_filep == NULL) { 8588770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 8598770SJordan.Vaughan@Sun.com return; 8608770SJordan.Vaughan@Sun.com } 8618770SJordan.Vaughan@Sun.com if (zcs->zc_slave_vnode->v_rdev != 8628770SJordan.Vaughan@Sun.com slave_filep->f_vnode->v_rdev) { 8638770SJordan.Vaughan@Sun.com releasef(slave_fd); 8648770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 8658770SJordan.Vaughan@Sun.com return; 8668770SJordan.Vaughan@Sun.com } 8678770SJordan.Vaughan@Sun.com 8688770SJordan.Vaughan@Sun.com /* 8698770SJordan.Vaughan@Sun.com * Decrement the snode's reference count and release the 8708770SJordan.Vaughan@Sun.com * vnode. 8718770SJordan.Vaughan@Sun.com */ 8728770SJordan.Vaughan@Sun.com ASSERT(vn_matchops(slave_filep->f_vnode, 8738770SJordan.Vaughan@Sun.com spec_getvnodeops())); 8748770SJordan.Vaughan@Sun.com slave_snodep = VTOCS(zcs->zc_slave_vnode); 8758770SJordan.Vaughan@Sun.com mutex_enter(&slave_snodep->s_lock); 8768770SJordan.Vaughan@Sun.com --slave_snodep->s_count; 8778770SJordan.Vaughan@Sun.com mutex_exit(&slave_snodep->s_lock); 8788770SJordan.Vaughan@Sun.com VN_RELE(zcs->zc_slave_vnode); 8798770SJordan.Vaughan@Sun.com zcs->zc_slave_vnode = NULL; 8808770SJordan.Vaughan@Sun.com releasef(slave_fd); 8818770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 8828770SJordan.Vaughan@Sun.com return; 8838770SJordan.Vaughan@Sun.com default: 8848770SJordan.Vaughan@Sun.com break; 8858770SJordan.Vaughan@Sun.com } 8868770SJordan.Vaughan@Sun.com } 8878770SJordan.Vaughan@Sun.com 8880Sstevel@tonic-gate if (zc_switch(RD(qp)) == NULL) { 8890Sstevel@tonic-gate DBG1("wput to %s side (no one listening)", zc_side(qp)); 8900Sstevel@tonic-gate switch (type) { 8910Sstevel@tonic-gate case M_FLUSH: 8920Sstevel@tonic-gate handle_mflush(qp, mp); 8930Sstevel@tonic-gate break; 8940Sstevel@tonic-gate case M_IOCTL: 8950Sstevel@tonic-gate miocnak(qp, mp, 0, 0); 8960Sstevel@tonic-gate break; 8970Sstevel@tonic-gate default: 8980Sstevel@tonic-gate freemsg(mp); 8990Sstevel@tonic-gate break; 9000Sstevel@tonic-gate } 9010Sstevel@tonic-gate return; 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate if (type >= QPCTL) { 9050Sstevel@tonic-gate DBG1("(hipri) wput, %s side", zc_side(qp)); 9060Sstevel@tonic-gate switch (type) { 9070Sstevel@tonic-gate case M_READ: /* supposedly from ldterm? */ 9080Sstevel@tonic-gate DBG("zc_wput: tossing M_READ\n"); 9090Sstevel@tonic-gate freemsg(mp); 9100Sstevel@tonic-gate break; 9110Sstevel@tonic-gate case M_FLUSH: 9120Sstevel@tonic-gate handle_mflush(qp, mp); 9130Sstevel@tonic-gate break; 9140Sstevel@tonic-gate default: 9150Sstevel@tonic-gate /* 9160Sstevel@tonic-gate * Put this to the other side. 9170Sstevel@tonic-gate */ 9180Sstevel@tonic-gate ASSERT(zc_switch(RD(qp)) != NULL); 9190Sstevel@tonic-gate putnext(zc_switch(RD(qp)), mp); 9200Sstevel@tonic-gate break; 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate DBG1("done (hipri) wput, %s side", zc_side(qp)); 9230Sstevel@tonic-gate return; 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate /* 9270Sstevel@tonic-gate * Only putnext if there isn't already something in the queue. 9280Sstevel@tonic-gate * otherwise things would wind up out of order. 9290Sstevel@tonic-gate */ 9300Sstevel@tonic-gate if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) { 9310Sstevel@tonic-gate DBG("wput: putting message to other side\n"); 9320Sstevel@tonic-gate putnext(RD(zc_switch(qp)), mp); 9330Sstevel@tonic-gate } else { 9340Sstevel@tonic-gate DBG("wput: putting msg onto queue\n"); 9350Sstevel@tonic-gate (void) putq(qp, mp); 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate DBG1("done wput, %s side", zc_side(qp)); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate /* 9410Sstevel@tonic-gate * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both 9420Sstevel@tonic-gate * without splitting up the codepath. 9430Sstevel@tonic-gate * 9440Sstevel@tonic-gate * Enable the write side of the partner. This triggers the partner to send 9450Sstevel@tonic-gate * messages queued on its write side to this queue's read side. 9460Sstevel@tonic-gate */ 9470Sstevel@tonic-gate static void 9480Sstevel@tonic-gate zc_rsrv(queue_t *qp) 9490Sstevel@tonic-gate { 9500Sstevel@tonic-gate zc_state_t *zcs; 9510Sstevel@tonic-gate zcs = (zc_state_t *)qp->q_ptr; 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate /* 9540Sstevel@tonic-gate * Care must be taken here, as either of the master or slave side 9550Sstevel@tonic-gate * qptr could be NULL. 9560Sstevel@tonic-gate */ 9570Sstevel@tonic-gate ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq); 9580Sstevel@tonic-gate if (zc_switch(qp) == NULL) { 9590Sstevel@tonic-gate DBG("zc_rsrv: other side isn't listening\n"); 9600Sstevel@tonic-gate return; 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate qenable(WR(zc_switch(qp))); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate /* 9660Sstevel@tonic-gate * This routine is symmetric for master and slave, so it handles both without 9670Sstevel@tonic-gate * splitting up the codepath. 9680Sstevel@tonic-gate * 9690Sstevel@tonic-gate * If there are messages on this queue that can be sent to the other, send 9700Sstevel@tonic-gate * them via putnext(). Else, if queued messages cannot be sent, leave them 9710Sstevel@tonic-gate * on this queue. 9720Sstevel@tonic-gate */ 9730Sstevel@tonic-gate static void 9740Sstevel@tonic-gate zc_wsrv(queue_t *qp) 9750Sstevel@tonic-gate { 9760Sstevel@tonic-gate mblk_t *mp; 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate DBG1("zc_wsrv master (%s) side", zc_side(qp)); 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * Partner has no read queue, so take the data, and throw it away. 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate if (zc_switch(RD(qp)) == NULL) { 9840Sstevel@tonic-gate DBG("zc_wsrv: other side isn't listening"); 9850Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 9860Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL) 9870Sstevel@tonic-gate miocnak(qp, mp, 0, 0); 9880Sstevel@tonic-gate else 9890Sstevel@tonic-gate freemsg(mp); 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate flushq(qp, FLUSHALL); 9920Sstevel@tonic-gate return; 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * while there are messages on this write queue... 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 9990Sstevel@tonic-gate /* 10000Sstevel@tonic-gate * Due to the way zc_wput is implemented, we should never 10010Sstevel@tonic-gate * see a control message here. 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate ASSERT(mp->b_datap->db_type < QPCTL); 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) { 10060Sstevel@tonic-gate DBG("wsrv: send message to other side\n"); 10070Sstevel@tonic-gate putnext(RD(zc_switch(qp)), mp); 10080Sstevel@tonic-gate } else { 10090Sstevel@tonic-gate DBG("wsrv: putting msg back on queue\n"); 10100Sstevel@tonic-gate (void) putbq(qp, mp); 10110Sstevel@tonic-gate break; 10120Sstevel@tonic-gate } 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate } 1015