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 /* 22*8770SJordan.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. 50*8770SJordan.Vaughan@Sun.com * 51*8770SJordan.Vaughan@Sun.com * 52*8770SJordan.Vaughan@Sun.com * 53*8770SJordan.Vaughan@Sun.com * MASTER SIDE IOCTLS 54*8770SJordan.Vaughan@Sun.com * 55*8770SJordan.Vaughan@Sun.com * The ZC_HOLDSLAVE and ZC_RELEASESLAVE ioctls instruct the master side of the 56*8770SJordan.Vaughan@Sun.com * console to hold and release a reference to the slave side's vnode. They are 57*8770SJordan.Vaughan@Sun.com * meant to be issued by zoneadmd after the console device node is created and 58*8770SJordan.Vaughan@Sun.com * before it is destroyed so that the slave's STREAMS anchor, ptem, is 59*8770SJordan.Vaughan@Sun.com * preserved when ttymon starts popping STREAMS modules from within the 60*8770SJordan.Vaughan@Sun.com * associated zone. This guarantees that the zone console will always have 61*8770SJordan.Vaughan@Sun.com * terminal semantics while the zone is running. 62*8770SJordan.Vaughan@Sun.com * 63*8770SJordan.Vaughan@Sun.com * Here is the issue: the ptem module is anchored in the zone console 64*8770SJordan.Vaughan@Sun.com * (slave side) so that processes within the associated non-global zone will 65*8770SJordan.Vaughan@Sun.com * fail to pop it off, thus ensuring that the slave will retain terminal 66*8770SJordan.Vaughan@Sun.com * semantics. When a process attempts to pop the anchor off of a stream, the 67*8770SJordan.Vaughan@Sun.com * STREAMS subsystem checks whether the calling process' zone is the same as 68*8770SJordan.Vaughan@Sun.com * that of the process that pushed the anchor onto the stream and cancels the 69*8770SJordan.Vaughan@Sun.com * pop if they differ. zoneadmd used to hold an open file descriptor for the 70*8770SJordan.Vaughan@Sun.com * slave while the associated non-global zone ran, thus ensuring that the 71*8770SJordan.Vaughan@Sun.com * slave's STREAMS anchor would never be popped from within the non-global zone 72*8770SJordan.Vaughan@Sun.com * (because zoneadmd runs in the global zone). However, this file descriptor 73*8770SJordan.Vaughan@Sun.com * was removed to make zone console management more robust. sad(7D) is now 74*8770SJordan.Vaughan@Sun.com * used to automatically set up the slave's STREAMS modules when the zone 75*8770SJordan.Vaughan@Sun.com * console is freshly opened within the associated non-global zone. However, 76*8770SJordan.Vaughan@Sun.com * when a process within the non-global zone freshly opens the zone console, the 77*8770SJordan.Vaughan@Sun.com * anchor is pushed from within the non-global zone, making it possible for 78*8770SJordan.Vaughan@Sun.com * processes within the non-global zone (e.g., ttymon) to pop the anchor and 79*8770SJordan.Vaughan@Sun.com * destroy the zone console's terminal semantics. 80*8770SJordan.Vaughan@Sun.com * 81*8770SJordan.Vaughan@Sun.com * One solution is to make the zcons device hold the slave open while the 82*8770SJordan.Vaughan@Sun.com * associated non-global zone runs so that the STREAMS anchor will always be 83*8770SJordan.Vaughan@Sun.com * associated with the global zone. Unfortunately, the slave cannot be opened 84*8770SJordan.Vaughan@Sun.com * from within the zcons driver because the driver is not reentrant: it has 85*8770SJordan.Vaughan@Sun.com * an outer STREAMS perimeter. Therefore, the next best option is for zcons to 86*8770SJordan.Vaughan@Sun.com * provide an ioctl interface to zoneadmd to manage holding and releasing 87*8770SJordan.Vaughan@Sun.com * the slave side of the console. It is sufficient to hold the slave side's 88*8770SJordan.Vaughan@Sun.com * vnode and bump the associated snode's reference count to preserve the slave's 89*8770SJordan.Vaughan@Sun.com * STREAMS configuration while the associated zone runs, so that's what the 90*8770SJordan.Vaughan@Sun.com * ioctls do. 91*8770SJordan.Vaughan@Sun.com * 92*8770SJordan.Vaughan@Sun.com * 93*8770SJordan.Vaughan@Sun.com * ZC_HOLDSLAVE 94*8770SJordan.Vaughan@Sun.com * 95*8770SJordan.Vaughan@Sun.com * This ioctl takes a file descriptor as an argument. It effectively gets a 96*8770SJordan.Vaughan@Sun.com * reference to the slave side's minor node's vnode and bumps the associated 97*8770SJordan.Vaughan@Sun.com * snode's reference count. The vnode reference is stored in the zcons device 98*8770SJordan.Vaughan@Sun.com * node's soft state. This ioctl succeeds if the given file descriptor refers 99*8770SJordan.Vaughan@Sun.com * to the slave side's minor node or if there is already a reference to the 100*8770SJordan.Vaughan@Sun.com * slave side's minor node's vnode in the device's soft state. 101*8770SJordan.Vaughan@Sun.com * 102*8770SJordan.Vaughan@Sun.com * 103*8770SJordan.Vaughan@Sun.com * ZC_RELEASESLAVE 104*8770SJordan.Vaughan@Sun.com * 105*8770SJordan.Vaughan@Sun.com * This ioctl takes a file descriptor as an argument. It effectively releases 106*8770SJordan.Vaughan@Sun.com * the vnode reference stored in the zcons device node's soft state (which was 107*8770SJordan.Vaughan@Sun.com * previously acquired via ZC_HOLDSLAVE) and decrements the reference count of 108*8770SJordan.Vaughan@Sun.com * the snode associated with the vnode. This ioctl succeeds if the given file 109*8770SJordan.Vaughan@Sun.com * descriptor refers to the slave side's minor node or if no reference to the 110*8770SJordan.Vaughan@Sun.com * slave side's minor node's vnode is stored in the device's soft state. 111*8770SJordan.Vaughan@Sun.com * 112*8770SJordan.Vaughan@Sun.com * 113*8770SJordan.Vaughan@Sun.com * Note that the file descriptor arguments for both ioctls must be cast to 114*8770SJordan.Vaughan@Sun.com * integers of pointer width. 115*8770SJordan.Vaughan@Sun.com * 116*8770SJordan.Vaughan@Sun.com * Here's how the dance between zcons and zoneadmd works: 117*8770SJordan.Vaughan@Sun.com * 118*8770SJordan.Vaughan@Sun.com * Zone boot: 119*8770SJordan.Vaughan@Sun.com * 1. While booting the zone, zoneadmd creates an instance of zcons. 120*8770SJordan.Vaughan@Sun.com * 2. zoneadmd opens the master and slave sides of the new zone console 121*8770SJordan.Vaughan@Sun.com * and issues the ZC_HOLDSLAVE ioctl on the master side, passing its 122*8770SJordan.Vaughan@Sun.com * file descriptor for the slave side as the ioctl argument. 123*8770SJordan.Vaughan@Sun.com * 3. zcons holds the slave side's vnode, bumps the snode's reference 124*8770SJordan.Vaughan@Sun.com * count, and stores a pointer to the vnode in the device's soft 125*8770SJordan.Vaughan@Sun.com * state. 126*8770SJordan.Vaughan@Sun.com * 4. zoneadmd closes the master and slave sides and continues to boot 127*8770SJordan.Vaughan@Sun.com * the zone. 128*8770SJordan.Vaughan@Sun.com * 129*8770SJordan.Vaughan@Sun.com * Zone halt: 130*8770SJordan.Vaughan@Sun.com * 1. While halting the zone, zoneadmd opens the master and slave sides 131*8770SJordan.Vaughan@Sun.com * of the zone's console and issues the ZC_RELEASESLAVE ioctl on the 132*8770SJordan.Vaughan@Sun.com * master side, passing its file descriptor for the slave side as the 133*8770SJordan.Vaughan@Sun.com * ioctl argument. 134*8770SJordan.Vaughan@Sun.com * 2. zcons decrements the slave side's snode's reference count, releases 135*8770SJordan.Vaughan@Sun.com * the slave's vnode, and eliminates its reference to the vnode in the 136*8770SJordan.Vaughan@Sun.com * device's soft state. 137*8770SJordan.Vaughan@Sun.com * 3. zoneadmd closes the master and slave sides. 138*8770SJordan.Vaughan@Sun.com * 4. zoneadmd destroys the zcons device and continues to halt the zone. 139*8770SJordan.Vaughan@Sun.com * 140*8770SJordan.Vaughan@Sun.com * It is necessary for zoneadmd to hold the slave open while issuing 141*8770SJordan.Vaughan@Sun.com * ZC_RELEASESLAVE because zcons might otherwise release the last reference to 142*8770SJordan.Vaughan@Sun.com * the slave's vnode. If it does, then specfs will panic because it will expect 143*8770SJordan.Vaughan@Sun.com * that the STREAMS configuration for the vnode was destroyed, which VN_RELE 144*8770SJordan.Vaughan@Sun.com * doesn't do. Forcing zoneadmd to hold the slave open guarantees that zcons 145*8770SJordan.Vaughan@Sun.com * won't release the vnode's last reference. zoneadmd will properly destroy the 146*8770SJordan.Vaughan@Sun.com * vnode and the snode when it closes the file descriptor. 147*8770SJordan.Vaughan@Sun.com * 148*8770SJordan.Vaughan@Sun.com * Technically, any process that can access the master side can issue these 149*8770SJordan.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> 161*8770SJordan.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> 173*8770SJordan.Vaughan@Sun.com #include <sys/vnode.h> 174*8770SJordan.Vaughan@Sun.com #include <sys/fs/snode.h> 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 1770Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t); 1780Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *); 1810Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *); 1820Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *); 1830Sstevel@tonic-gate static void zc_rsrv(queue_t *); 1840Sstevel@tonic-gate static void zc_wsrv(queue_t *); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * The instance number is encoded in the dev_t in the minor number; the lowest 1880Sstevel@tonic-gate * bit of the minor number is used to track the master vs. slave side of the 1890Sstevel@tonic-gate * virtual console. The rest of the bits in the minor number are the instance. 1900Sstevel@tonic-gate */ 191*8770SJordan.Vaughan@Sun.com #define ZC_MASTER_MINOR 0 192*8770SJordan.Vaughan@Sun.com #define ZC_SLAVE_MINOR 1 193*8770SJordan.Vaughan@Sun.com 194*8770SJordan.Vaughan@Sun.com #define ZC_INSTANCE(x) (getminor((x)) >> 1) 195*8770SJordan.Vaughan@Sun.com #define ZC_NODE(x) (getminor((x)) & 0x01) 1960Sstevel@tonic-gate 197*8770SJordan.Vaughan@Sun.com /* 198*8770SJordan.Vaughan@Sun.com * This macro converts a zc_state_t pointer to the associated slave minor node's 199*8770SJordan.Vaughan@Sun.com * dev_t. 200*8770SJordan.Vaughan@Sun.com */ 201*8770SJordan.Vaughan@Sun.com #define ZC_STATE_TO_SLAVEDEV(x) (makedevice(ddi_driver_major((x)->zc_devinfo), \ 202*8770SJordan.Vaughan@Sun.com (minor_t)(ddi_get_instance((x)->zc_devinfo) << 1 | ZC_SLAVE_MINOR))) 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate int zcons_debug = 0; 2050Sstevel@tonic-gate #define DBG(a) if (zcons_debug) cmn_err(CE_NOTE, a) 2060Sstevel@tonic-gate #define DBG1(a, b) if (zcons_debug) cmn_err(CE_NOTE, a, b) 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * Zone Console Pseudo Terminal Module: stream data structure definitions 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate static struct module_info zc_info = { 2130Sstevel@tonic-gate 31337, /* c0z we r hAx0rs */ 2140Sstevel@tonic-gate "zcons", 2150Sstevel@tonic-gate 0, 2160Sstevel@tonic-gate INFPSZ, 2170Sstevel@tonic-gate 2048, 2180Sstevel@tonic-gate 128 2190Sstevel@tonic-gate }; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate static struct qinit zc_rinit = { 2220Sstevel@tonic-gate NULL, 2230Sstevel@tonic-gate (int (*)()) zc_rsrv, 2240Sstevel@tonic-gate zc_open, 2250Sstevel@tonic-gate zc_close, 2260Sstevel@tonic-gate NULL, 2270Sstevel@tonic-gate &zc_info, 2280Sstevel@tonic-gate NULL 2290Sstevel@tonic-gate }; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate static struct qinit zc_winit = { 2320Sstevel@tonic-gate (int (*)()) zc_wput, 2330Sstevel@tonic-gate (int (*)()) zc_wsrv, 2340Sstevel@tonic-gate NULL, 2350Sstevel@tonic-gate NULL, 2360Sstevel@tonic-gate NULL, 2370Sstevel@tonic-gate &zc_info, 2380Sstevel@tonic-gate NULL 2390Sstevel@tonic-gate }; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate static struct streamtab zc_tab_info = { 2420Sstevel@tonic-gate &zc_rinit, 2430Sstevel@tonic-gate &zc_winit, 2440Sstevel@tonic-gate NULL, 2450Sstevel@tonic-gate NULL 2460Sstevel@tonic-gate }; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate #define ZC_CONF_FLAG (D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL) 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops) 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev, zc_attach, zc_detach, nodev, \ 2547656SSherry.Moore@Sun.COM zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * Module linkage information for the kernel. 2580Sstevel@tonic-gate */ 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate static struct modldrv modldrv = { 2614959Sedp &mod_driverops, /* Type of module (this is a pseudo driver) */ 2624959Sedp "Zone console driver", /* description of module */ 2634959Sedp &zc_ops /* driver ops */ 2640Sstevel@tonic-gate }; 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate static struct modlinkage modlinkage = { 2670Sstevel@tonic-gate MODREV_1, 2680Sstevel@tonic-gate &modldrv, 2690Sstevel@tonic-gate NULL 2700Sstevel@tonic-gate }; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate typedef struct zc_state { 2730Sstevel@tonic-gate dev_info_t *zc_devinfo; 2740Sstevel@tonic-gate queue_t *zc_master_rdq; 2750Sstevel@tonic-gate queue_t *zc_slave_rdq; 276*8770SJordan.Vaughan@Sun.com vnode_t *zc_slave_vnode; 2770Sstevel@tonic-gate int zc_state; 2780Sstevel@tonic-gate } zc_state_t; 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate #define ZC_STATE_MOPEN 0x01 2810Sstevel@tonic-gate #define ZC_STATE_SOPEN 0x02 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate static void *zc_soft_state; 2840Sstevel@tonic-gate 285*8770SJordan.Vaughan@Sun.com /* 286*8770SJordan.Vaughan@Sun.com * List of STREAMS modules that should be pushed onto every slave instance. 287*8770SJordan.Vaughan@Sun.com */ 288*8770SJordan.Vaughan@Sun.com static char *zcons_mods[] = { 289*8770SJordan.Vaughan@Sun.com "ptem", 290*8770SJordan.Vaughan@Sun.com "ldterm", 291*8770SJordan.Vaughan@Sun.com "ttcompat", 292*8770SJordan.Vaughan@Sun.com NULL 293*8770SJordan.Vaughan@Sun.com }; 294*8770SJordan.Vaughan@Sun.com 2950Sstevel@tonic-gate int 2960Sstevel@tonic-gate _init(void) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate int err; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate if ((err = ddi_soft_state_init(&zc_soft_state, 3010Sstevel@tonic-gate sizeof (zc_state_t), 0)) != 0) { 3020Sstevel@tonic-gate return (err); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate if ((err = mod_install(&modlinkage)) != 0) 3060Sstevel@tonic-gate ddi_soft_state_fini(zc_soft_state); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate return (err); 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate int 3130Sstevel@tonic-gate _fini(void) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate int err; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate if ((err = mod_remove(&modlinkage)) != 0) { 3180Sstevel@tonic-gate return (err); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate ddi_soft_state_fini(&zc_soft_state); 3220Sstevel@tonic-gate return (0); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate int 3260Sstevel@tonic-gate _info(struct modinfo *modinfop) 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 331*8770SJordan.Vaughan@Sun.com /* 332*8770SJordan.Vaughan@Sun.com * This is a convenience function that clears a device's autopush configuration. 333*8770SJordan.Vaughan@Sun.com * It is meant to be used on the slave side of the console. Unlike calling 334*8770SJordan.Vaughan@Sun.com * kstr_autopush() directly, this function outputs a warning via cmn_err() if 335*8770SJordan.Vaughan@Sun.com * kstr_autopush() fails. 'dip' must be non-NULL in debug builds. Both 336*8770SJordan.Vaughan@Sun.com * 'major' and 'minor' must be valid. 337*8770SJordan.Vaughan@Sun.com */ 338*8770SJordan.Vaughan@Sun.com static void 339*8770SJordan.Vaughan@Sun.com zc_clearautopush(dev_info_t *dip, major_t major, minor_t minor) 340*8770SJordan.Vaughan@Sun.com { 341*8770SJordan.Vaughan@Sun.com char *devicepathp; 342*8770SJordan.Vaughan@Sun.com 343*8770SJordan.Vaughan@Sun.com if (kstr_autopush(CLR_AUTOPUSH, &major, &minor, NULL, NULL, NULL) != 344*8770SJordan.Vaughan@Sun.com 0 && zcons_debug != 0) { 345*8770SJordan.Vaughan@Sun.com devicepathp = (char *)kmem_alloc(MAXPATHLEN * sizeof (char), 346*8770SJordan.Vaughan@Sun.com KM_SLEEP); 347*8770SJordan.Vaughan@Sun.com (void) ddi_pathname(dip, devicepathp); 348*8770SJordan.Vaughan@Sun.com cmn_err(CE_NOTE, "zc_detach: could not clear sad configuration " 349*8770SJordan.Vaughan@Sun.com "for device %s\n", devicepathp); 350*8770SJordan.Vaughan@Sun.com kmem_free(devicepathp, MAXPATHLEN * sizeof (char)); 351*8770SJordan.Vaughan@Sun.com } 352*8770SJordan.Vaughan@Sun.com } 353*8770SJordan.Vaughan@Sun.com 3540Sstevel@tonic-gate static int 3550Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate zc_state_t *zcs; 3580Sstevel@tonic-gate int instance; 359*8770SJordan.Vaughan@Sun.com major_t major; 360*8770SJordan.Vaughan@Sun.com minor_t minor; 361*8770SJordan.Vaughan@Sun.com minor_t lastminor; 362*8770SJordan.Vaughan@Sun.com uint_t anchorindex; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate if (cmd != DDI_ATTACH) 3650Sstevel@tonic-gate return (DDI_FAILURE); 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate instance = ddi_get_instance(dip); 3680Sstevel@tonic-gate if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS) 3690Sstevel@tonic-gate return (DDI_FAILURE); 3700Sstevel@tonic-gate 371*8770SJordan.Vaughan@Sun.com /* 372*8770SJordan.Vaughan@Sun.com * Set up sad(7D) so that the necessary STREAMS modules will be in place 373*8770SJordan.Vaughan@Sun.com * when the slave is opened. A wrinkle is that 'ptem' must be anchored 374*8770SJordan.Vaughan@Sun.com * in place (see streamio(7i)) because we always want the console to 375*8770SJordan.Vaughan@Sun.com * have terminal semantics. 376*8770SJordan.Vaughan@Sun.com */ 377*8770SJordan.Vaughan@Sun.com minor = instance << 1 | ZC_SLAVE_MINOR; 378*8770SJordan.Vaughan@Sun.com lastminor = 0; 379*8770SJordan.Vaughan@Sun.com major = ddi_driver_major(dip); 380*8770SJordan.Vaughan@Sun.com anchorindex = 1; 381*8770SJordan.Vaughan@Sun.com if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor, 382*8770SJordan.Vaughan@Sun.com &anchorindex, zcons_mods) != 0) 383*8770SJordan.Vaughan@Sun.com goto commonfail; 3840Sstevel@tonic-gate 385*8770SJordan.Vaughan@Sun.com /* 386*8770SJordan.Vaughan@Sun.com * Create the master and slave minor nodes. 387*8770SJordan.Vaughan@Sun.com */ 388*8770SJordan.Vaughan@Sun.com if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR, minor, 389*8770SJordan.Vaughan@Sun.com DDI_PSEUDO, 0) == DDI_FAILURE) || 390*8770SJordan.Vaughan@Sun.com (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR, 391*8770SJordan.Vaughan@Sun.com instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) 392*8770SJordan.Vaughan@Sun.com goto failwithautopush; 393*8770SJordan.Vaughan@Sun.com 394*8770SJordan.Vaughan@Sun.com VERIFY((zcs = ddi_get_soft_state(zc_soft_state, instance)) != NULL); 3950Sstevel@tonic-gate zcs->zc_devinfo = dip; 396*8770SJordan.Vaughan@Sun.com return (DDI_SUCCESS); 3970Sstevel@tonic-gate 398*8770SJordan.Vaughan@Sun.com failwithautopush: 399*8770SJordan.Vaughan@Sun.com zc_clearautopush(dip, major, minor); 400*8770SJordan.Vaughan@Sun.com ddi_remove_minor_node(dip, NULL); 401*8770SJordan.Vaughan@Sun.com commonfail: 402*8770SJordan.Vaughan@Sun.com ddi_soft_state_free(zc_soft_state, instance); 403*8770SJordan.Vaughan@Sun.com return (DDI_FAILURE); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate static int 4070Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate zc_state_t *zcs; 4100Sstevel@tonic-gate int instance; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate if (cmd != DDI_DETACH) 4130Sstevel@tonic-gate return (DDI_FAILURE); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate instance = ddi_get_instance(dip); 4160Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 4170Sstevel@tonic-gate return (DDI_FAILURE); 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_MOPEN) || 4200Sstevel@tonic-gate (zcs->zc_state & ZC_STATE_SOPEN)) { 4210Sstevel@tonic-gate DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip); 4220Sstevel@tonic-gate return (DDI_FAILURE); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 425*8770SJordan.Vaughan@Sun.com /* 426*8770SJordan.Vaughan@Sun.com * Clear the sad configuration so that reattaching doesn't fail to 427*8770SJordan.Vaughan@Sun.com * set up sad configuration. 428*8770SJordan.Vaughan@Sun.com */ 429*8770SJordan.Vaughan@Sun.com zc_clearautopush(dip, ddi_driver_major(dip), instance << 1 | 430*8770SJordan.Vaughan@Sun.com ZC_SLAVE_MINOR); 431*8770SJordan.Vaughan@Sun.com 4320Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 4330Sstevel@tonic-gate ddi_soft_state_free(zc_soft_state, instance); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate return (DDI_SUCCESS); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * zc_getinfo() 4400Sstevel@tonic-gate * getinfo(9e) entrypoint. 4410Sstevel@tonic-gate */ 4420Sstevel@tonic-gate /*ARGSUSED*/ 4430Sstevel@tonic-gate static int 4440Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 4450Sstevel@tonic-gate { 4460Sstevel@tonic-gate zc_state_t *zcs; 4470Sstevel@tonic-gate int instance = ZC_INSTANCE((dev_t)arg); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate switch (infocmd) { 4500Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 4510Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 4520Sstevel@tonic-gate return (DDI_FAILURE); 4530Sstevel@tonic-gate *result = zcs->zc_devinfo; 4540Sstevel@tonic-gate return (DDI_SUCCESS); 4550Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 4560Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 4570Sstevel@tonic-gate return (DDI_SUCCESS); 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate return (DDI_FAILURE); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * Return the equivalent queue from the other side of the relationship. 4640Sstevel@tonic-gate * e.g.: given the slave's write queue, return the master's write queue. 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate static queue_t * 4670Sstevel@tonic-gate zc_switch(queue_t *qp) 4680Sstevel@tonic-gate { 4690Sstevel@tonic-gate zc_state_t *zcs = qp->q_ptr; 4700Sstevel@tonic-gate ASSERT(zcs != NULL); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if (qp == zcs->zc_master_rdq) 4730Sstevel@tonic-gate return (zcs->zc_slave_rdq); 4740Sstevel@tonic-gate else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL) 4750Sstevel@tonic-gate return (OTHERQ(zcs->zc_slave_rdq)); 4760Sstevel@tonic-gate else if (qp == zcs->zc_slave_rdq) 4770Sstevel@tonic-gate return (zcs->zc_master_rdq); 4780Sstevel@tonic-gate else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL) 4790Sstevel@tonic-gate return (OTHERQ(zcs->zc_master_rdq)); 4800Sstevel@tonic-gate else 4810Sstevel@tonic-gate return (NULL); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate /* 4850Sstevel@tonic-gate * For debugging and outputting messages. Returns the name of the side of 4860Sstevel@tonic-gate * the relationship associated with this queue. 4870Sstevel@tonic-gate */ 4880Sstevel@tonic-gate static const char * 4890Sstevel@tonic-gate zc_side(queue_t *qp) 4900Sstevel@tonic-gate { 4910Sstevel@tonic-gate zc_state_t *zcs = qp->q_ptr; 4920Sstevel@tonic-gate ASSERT(zcs != NULL); 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate if (qp == zcs->zc_master_rdq || 4950Sstevel@tonic-gate OTHERQ(qp) == zcs->zc_master_rdq) { 4960Sstevel@tonic-gate return ("master"); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq); 4990Sstevel@tonic-gate return ("slave"); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /*ARGSUSED*/ 5030Sstevel@tonic-gate static int 5040Sstevel@tonic-gate zc_master_open(zc_state_t *zcs, 5050Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 5060Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 5070Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 5080Sstevel@tonic-gate int sflag, /* open state flag */ 5090Sstevel@tonic-gate cred_t *credp) /* credentials */ 5100Sstevel@tonic-gate { 5110Sstevel@tonic-gate mblk_t *mop; 5120Sstevel@tonic-gate struct stroptions *sop; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Enforce exclusivity on the master side; the only consumer should 5160Sstevel@tonic-gate * be the zoneadmd for the zone. 5170Sstevel@tonic-gate */ 5180Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_MOPEN) != 0) 5190Sstevel@tonic-gate return (EBUSY); 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) { 5220Sstevel@tonic-gate DBG("zc_master_open(): mop allocation failed\n"); 5230Sstevel@tonic-gate return (ENOMEM); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate zcs->zc_state |= ZC_STATE_MOPEN; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* 5290Sstevel@tonic-gate * q_ptr stores driver private data; stash the soft state data on both 5300Sstevel@tonic-gate * read and write sides of the queue. 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = zcs; 5330Sstevel@tonic-gate qprocson(rqp); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * Following qprocson(), the master side is fully plumbed into the 5370Sstevel@tonic-gate * STREAM and may send/receive messages. Setting zcs->zc_master_rdq 5380Sstevel@tonic-gate * will allow the slave to send messages to us (the master). 5390Sstevel@tonic-gate * This cannot occur before qprocson() because the master is not 5400Sstevel@tonic-gate * ready to process them until that point. 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate zcs->zc_master_rdq = rqp; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue and add 5460Sstevel@tonic-gate * controlling tty as needed. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 5490Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 5503813Sdp sop = (struct stroptions *)(void *)mop->b_rptr; 5510Sstevel@tonic-gate if (oflag & FNOCTTY) 5520Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT; 5530Sstevel@tonic-gate else 5540Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 5550Sstevel@tonic-gate sop->so_hiwat = 512; 5560Sstevel@tonic-gate sop->so_lowat = 256; 5570Sstevel@tonic-gate putnext(rqp, mop); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate return (0); 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate /*ARGSUSED*/ 5630Sstevel@tonic-gate static int 5640Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs, 5650Sstevel@tonic-gate queue_t *rqp, /* pointer to the read side queue */ 5660Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 5670Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 5680Sstevel@tonic-gate int sflag, /* open state flag */ 5690Sstevel@tonic-gate cred_t *credp) /* credentials */ 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate mblk_t *mop; 5720Sstevel@tonic-gate struct stroptions *sop; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * The slave side can be opened as many times as needed. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) { 5780Sstevel@tonic-gate ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs)); 5790Sstevel@tonic-gate return (0); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) { 5830Sstevel@tonic-gate DBG("zc_slave_open(): mop allocation failed\n"); 5840Sstevel@tonic-gate return (ENOMEM); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate zcs->zc_state |= ZC_STATE_SOPEN; 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* 5900Sstevel@tonic-gate * q_ptr stores driver private data; stash the soft state data on both 5910Sstevel@tonic-gate * read and write sides of the queue. 5920Sstevel@tonic-gate */ 5930Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = zcs; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate qprocson(rqp); 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate /* 5980Sstevel@tonic-gate * Must follow qprocson(), since we aren't ready to process until then. 5990Sstevel@tonic-gate */ 6000Sstevel@tonic-gate zcs->zc_slave_rdq = rqp; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate /* 6030Sstevel@tonic-gate * set up hi/lo water marks on stream head read queue and add 6040Sstevel@tonic-gate * controlling tty as needed. 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate mop->b_datap->db_type = M_SETOPTS; 6070Sstevel@tonic-gate mop->b_wptr += sizeof (struct stroptions); 6083813Sdp sop = (struct stroptions *)(void *)mop->b_rptr; 6090Sstevel@tonic-gate sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY; 6100Sstevel@tonic-gate sop->so_hiwat = 512; 6110Sstevel@tonic-gate sop->so_lowat = 256; 6120Sstevel@tonic-gate putnext(rqp, mop); 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate return (0); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * open(9e) entrypoint; checks sflag, and rejects anything unordinary. 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate static int 6210Sstevel@tonic-gate zc_open(queue_t *rqp, /* pointer to the read side queue */ 6220Sstevel@tonic-gate dev_t *devp, /* pointer to stream tail's dev */ 6230Sstevel@tonic-gate int oflag, /* the user open(2) supplied flags */ 6240Sstevel@tonic-gate int sflag, /* open state flag */ 6250Sstevel@tonic-gate cred_t *credp) /* credentials */ 6260Sstevel@tonic-gate { 6270Sstevel@tonic-gate int instance = ZC_INSTANCE(*devp); 6280Sstevel@tonic-gate int ret; 6290Sstevel@tonic-gate zc_state_t *zcs; 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate if (sflag != 0) 6320Sstevel@tonic-gate return (EINVAL); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) 6350Sstevel@tonic-gate return (ENXIO); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate switch (ZC_NODE(*devp)) { 6380Sstevel@tonic-gate case ZC_MASTER_MINOR: 6390Sstevel@tonic-gate ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp); 6400Sstevel@tonic-gate break; 6410Sstevel@tonic-gate case ZC_SLAVE_MINOR: 6420Sstevel@tonic-gate ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp); 6430Sstevel@tonic-gate break; 6440Sstevel@tonic-gate default: 6450Sstevel@tonic-gate ret = ENXIO; 6460Sstevel@tonic-gate break; 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate return (ret); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * close(9e) entrypoint. 6540Sstevel@tonic-gate */ 6550Sstevel@tonic-gate /*ARGSUSED1*/ 6560Sstevel@tonic-gate static int 6570Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp) 6580Sstevel@tonic-gate { 6590Sstevel@tonic-gate queue_t *wqp; 6600Sstevel@tonic-gate mblk_t *bp; 6610Sstevel@tonic-gate zc_state_t *zcs; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate zcs = (zc_state_t *)rqp->q_ptr; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate if (rqp == zcs->zc_master_rdq) { 6660Sstevel@tonic-gate DBG("Closing master side"); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate zcs->zc_master_rdq = NULL; 6690Sstevel@tonic-gate zcs->zc_state &= ~ZC_STATE_MOPEN; 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate /* 6720Sstevel@tonic-gate * qenable slave side write queue so that it can flush 6730Sstevel@tonic-gate * its messages as master's read queue is going away 6740Sstevel@tonic-gate */ 6750Sstevel@tonic-gate if (zcs->zc_slave_rdq != NULL) { 6760Sstevel@tonic-gate qenable(WR(zcs->zc_slave_rdq)); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate qprocsoff(rqp); 6800Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = NULL; 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate } else if (rqp == zcs->zc_slave_rdq) { 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate DBG("Closing slave side"); 6850Sstevel@tonic-gate zcs->zc_state &= ~ZC_STATE_SOPEN; 6860Sstevel@tonic-gate zcs->zc_slave_rdq = NULL; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate wqp = WR(rqp); 6890Sstevel@tonic-gate while ((bp = getq(wqp)) != NULL) { 6900Sstevel@tonic-gate if (zcs->zc_master_rdq != NULL) 6910Sstevel@tonic-gate putnext(zcs->zc_master_rdq, bp); 6920Sstevel@tonic-gate else if (bp->b_datap->db_type == M_IOCTL) 6930Sstevel@tonic-gate miocnak(wqp, bp, 0, 0); 6940Sstevel@tonic-gate else 6950Sstevel@tonic-gate freemsg(bp); 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * Qenable master side write queue so that it can flush its 7000Sstevel@tonic-gate * messages as slaves's read queue is going away. 7010Sstevel@tonic-gate */ 7020Sstevel@tonic-gate if (zcs->zc_master_rdq != NULL) 7030Sstevel@tonic-gate qenable(WR(zcs->zc_master_rdq)); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate qprocsoff(rqp); 7060Sstevel@tonic-gate WR(rqp)->q_ptr = rqp->q_ptr = NULL; 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate return (0); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate static void 7130Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp) 7140Sstevel@tonic-gate { 7150Sstevel@tonic-gate mblk_t *nmp; 7160Sstevel@tonic-gate DBG1("M_FLUSH on %s side", zc_side(qp)); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 7190Sstevel@tonic-gate DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp)); 7200Sstevel@tonic-gate flushq(qp, FLUSHDATA); 7210Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 7220Sstevel@tonic-gate if ((*mp->b_rptr & FLUSHR) == 0) { 7230Sstevel@tonic-gate /* 7240Sstevel@tonic-gate * FLUSHW only. Change to FLUSHR and putnext other side, 7250Sstevel@tonic-gate * then we are done. 7260Sstevel@tonic-gate */ 7270Sstevel@tonic-gate *mp->b_rptr |= FLUSHR; 7280Sstevel@tonic-gate if (zc_switch(RD(qp)) != NULL) { 7290Sstevel@tonic-gate putnext(zc_switch(RD(qp)), mp); 7300Sstevel@tonic-gate return; 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate } else if ((zc_switch(RD(qp)) != NULL) && 7330Sstevel@tonic-gate (nmp = copyb(mp)) != NULL) { 7340Sstevel@tonic-gate /* 7350Sstevel@tonic-gate * It is a FLUSHRW; we copy the mblk and send 7360Sstevel@tonic-gate * it to the other side, since we still need to use 7370Sstevel@tonic-gate * the mblk in FLUSHR processing, below. 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate putnext(zc_switch(RD(qp)), nmp); 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 7440Sstevel@tonic-gate DBG("qreply(qp) turning FLUSHR around\n"); 7450Sstevel@tonic-gate qreply(qp, mp); 7460Sstevel@tonic-gate return; 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate freemsg(mp); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* 7520Sstevel@tonic-gate * wput(9E) is symmetric for master and slave sides, so this handles both 753*8770SJordan.Vaughan@Sun.com * without splitting the codepath. (The only exception to this is the 754*8770SJordan.Vaughan@Sun.com * processing of zcons ioctls, which is restricted to the master side.) 7550Sstevel@tonic-gate * 7560Sstevel@tonic-gate * zc_wput() looks at the other side; if there is no process holding that 7570Sstevel@tonic-gate * side open, it frees the message. This prevents processes from hanging 7580Sstevel@tonic-gate * if no one is holding open the console. Otherwise, it putnext's high 7590Sstevel@tonic-gate * priority messages, putnext's normal messages if possible, and otherwise 7600Sstevel@tonic-gate * enqueues the messages; in the case that something is enqueued, wsrv(9E) 7610Sstevel@tonic-gate * will take care of eventually shuttling I/O to the other side. 7620Sstevel@tonic-gate */ 7630Sstevel@tonic-gate static void 7640Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp) 7650Sstevel@tonic-gate { 7660Sstevel@tonic-gate unsigned char type = mp->b_datap->db_type; 767*8770SJordan.Vaughan@Sun.com zc_state_t *zcs; 768*8770SJordan.Vaughan@Sun.com struct iocblk *iocbp; 769*8770SJordan.Vaughan@Sun.com file_t *slave_filep; 770*8770SJordan.Vaughan@Sun.com struct snode *slave_snodep; 771*8770SJordan.Vaughan@Sun.com int slave_fd; 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate ASSERT(qp->q_ptr); 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate DBG1("entering zc_wput, %s side", zc_side(qp)); 7760Sstevel@tonic-gate 777*8770SJordan.Vaughan@Sun.com /* 778*8770SJordan.Vaughan@Sun.com * Process zcons ioctl messages if qp is the master console's write 779*8770SJordan.Vaughan@Sun.com * queue. 780*8770SJordan.Vaughan@Sun.com */ 781*8770SJordan.Vaughan@Sun.com zcs = (zc_state_t *)qp->q_ptr; 782*8770SJordan.Vaughan@Sun.com if (zcs->zc_master_rdq != NULL && qp == WR(zcs->zc_master_rdq) && 783*8770SJordan.Vaughan@Sun.com type == M_IOCTL) { 784*8770SJordan.Vaughan@Sun.com iocbp = (struct iocblk *)(void *)mp->b_rptr; 785*8770SJordan.Vaughan@Sun.com switch (iocbp->ioc_cmd) { 786*8770SJordan.Vaughan@Sun.com case ZC_HOLDSLAVE: 787*8770SJordan.Vaughan@Sun.com /* 788*8770SJordan.Vaughan@Sun.com * Hold the slave's vnode and increment the refcount 789*8770SJordan.Vaughan@Sun.com * of the snode. If the vnode is already held, then 790*8770SJordan.Vaughan@Sun.com * indicate success. 791*8770SJordan.Vaughan@Sun.com */ 792*8770SJordan.Vaughan@Sun.com if (iocbp->ioc_count != TRANSPARENT) { 793*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 794*8770SJordan.Vaughan@Sun.com return; 795*8770SJordan.Vaughan@Sun.com } 796*8770SJordan.Vaughan@Sun.com if (zcs->zc_slave_vnode != NULL) { 797*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 798*8770SJordan.Vaughan@Sun.com return; 799*8770SJordan.Vaughan@Sun.com } 800*8770SJordan.Vaughan@Sun.com 801*8770SJordan.Vaughan@Sun.com /* 802*8770SJordan.Vaughan@Sun.com * The calling process must pass a file descriptor for 803*8770SJordan.Vaughan@Sun.com * the slave device. 804*8770SJordan.Vaughan@Sun.com */ 805*8770SJordan.Vaughan@Sun.com slave_fd = 806*8770SJordan.Vaughan@Sun.com (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont-> 807*8770SJordan.Vaughan@Sun.com b_rptr; 808*8770SJordan.Vaughan@Sun.com slave_filep = getf(slave_fd); 809*8770SJordan.Vaughan@Sun.com if (slave_filep == NULL) { 810*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 811*8770SJordan.Vaughan@Sun.com return; 812*8770SJordan.Vaughan@Sun.com } 813*8770SJordan.Vaughan@Sun.com if (ZC_STATE_TO_SLAVEDEV(zcs) != 814*8770SJordan.Vaughan@Sun.com slave_filep->f_vnode->v_rdev) { 815*8770SJordan.Vaughan@Sun.com releasef(slave_fd); 816*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 817*8770SJordan.Vaughan@Sun.com return; 818*8770SJordan.Vaughan@Sun.com } 819*8770SJordan.Vaughan@Sun.com 820*8770SJordan.Vaughan@Sun.com /* 821*8770SJordan.Vaughan@Sun.com * Get a reference to the slave's vnode. Also bump the 822*8770SJordan.Vaughan@Sun.com * reference count on the associated snode. 823*8770SJordan.Vaughan@Sun.com */ 824*8770SJordan.Vaughan@Sun.com ASSERT(vn_matchops(slave_filep->f_vnode, 825*8770SJordan.Vaughan@Sun.com spec_getvnodeops())); 826*8770SJordan.Vaughan@Sun.com zcs->zc_slave_vnode = slave_filep->f_vnode; 827*8770SJordan.Vaughan@Sun.com VN_HOLD(zcs->zc_slave_vnode); 828*8770SJordan.Vaughan@Sun.com slave_snodep = VTOCS(zcs->zc_slave_vnode); 829*8770SJordan.Vaughan@Sun.com mutex_enter(&slave_snodep->s_lock); 830*8770SJordan.Vaughan@Sun.com ++slave_snodep->s_count; 831*8770SJordan.Vaughan@Sun.com mutex_exit(&slave_snodep->s_lock); 832*8770SJordan.Vaughan@Sun.com releasef(slave_fd); 833*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 834*8770SJordan.Vaughan@Sun.com return; 835*8770SJordan.Vaughan@Sun.com case ZC_RELEASESLAVE: 836*8770SJordan.Vaughan@Sun.com /* 837*8770SJordan.Vaughan@Sun.com * Release the master's handle on the slave's vnode. 838*8770SJordan.Vaughan@Sun.com * If there isn't a handle for the vnode, then indicate 839*8770SJordan.Vaughan@Sun.com * success. 840*8770SJordan.Vaughan@Sun.com */ 841*8770SJordan.Vaughan@Sun.com if (iocbp->ioc_count != TRANSPARENT) { 842*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 843*8770SJordan.Vaughan@Sun.com return; 844*8770SJordan.Vaughan@Sun.com } 845*8770SJordan.Vaughan@Sun.com if (zcs->zc_slave_vnode == NULL) { 846*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 847*8770SJordan.Vaughan@Sun.com return; 848*8770SJordan.Vaughan@Sun.com } 849*8770SJordan.Vaughan@Sun.com 850*8770SJordan.Vaughan@Sun.com /* 851*8770SJordan.Vaughan@Sun.com * The process that passed the ioctl must have provided 852*8770SJordan.Vaughan@Sun.com * a file descriptor for the slave device. Make sure 853*8770SJordan.Vaughan@Sun.com * this is correct. 854*8770SJordan.Vaughan@Sun.com */ 855*8770SJordan.Vaughan@Sun.com slave_fd = 856*8770SJordan.Vaughan@Sun.com (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont-> 857*8770SJordan.Vaughan@Sun.com b_rptr; 858*8770SJordan.Vaughan@Sun.com slave_filep = getf(slave_fd); 859*8770SJordan.Vaughan@Sun.com if (slave_filep == NULL) { 860*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 861*8770SJordan.Vaughan@Sun.com return; 862*8770SJordan.Vaughan@Sun.com } 863*8770SJordan.Vaughan@Sun.com if (zcs->zc_slave_vnode->v_rdev != 864*8770SJordan.Vaughan@Sun.com slave_filep->f_vnode->v_rdev) { 865*8770SJordan.Vaughan@Sun.com releasef(slave_fd); 866*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, EINVAL); 867*8770SJordan.Vaughan@Sun.com return; 868*8770SJordan.Vaughan@Sun.com } 869*8770SJordan.Vaughan@Sun.com 870*8770SJordan.Vaughan@Sun.com /* 871*8770SJordan.Vaughan@Sun.com * Decrement the snode's reference count and release the 872*8770SJordan.Vaughan@Sun.com * vnode. 873*8770SJordan.Vaughan@Sun.com */ 874*8770SJordan.Vaughan@Sun.com ASSERT(vn_matchops(slave_filep->f_vnode, 875*8770SJordan.Vaughan@Sun.com spec_getvnodeops())); 876*8770SJordan.Vaughan@Sun.com slave_snodep = VTOCS(zcs->zc_slave_vnode); 877*8770SJordan.Vaughan@Sun.com mutex_enter(&slave_snodep->s_lock); 878*8770SJordan.Vaughan@Sun.com --slave_snodep->s_count; 879*8770SJordan.Vaughan@Sun.com mutex_exit(&slave_snodep->s_lock); 880*8770SJordan.Vaughan@Sun.com VN_RELE(zcs->zc_slave_vnode); 881*8770SJordan.Vaughan@Sun.com zcs->zc_slave_vnode = NULL; 882*8770SJordan.Vaughan@Sun.com releasef(slave_fd); 883*8770SJordan.Vaughan@Sun.com miocack(qp, mp, 0, 0); 884*8770SJordan.Vaughan@Sun.com return; 885*8770SJordan.Vaughan@Sun.com default: 886*8770SJordan.Vaughan@Sun.com break; 887*8770SJordan.Vaughan@Sun.com } 888*8770SJordan.Vaughan@Sun.com } 889*8770SJordan.Vaughan@Sun.com 8900Sstevel@tonic-gate if (zc_switch(RD(qp)) == NULL) { 8910Sstevel@tonic-gate DBG1("wput to %s side (no one listening)", zc_side(qp)); 8920Sstevel@tonic-gate switch (type) { 8930Sstevel@tonic-gate case M_FLUSH: 8940Sstevel@tonic-gate handle_mflush(qp, mp); 8950Sstevel@tonic-gate break; 8960Sstevel@tonic-gate case M_IOCTL: 8970Sstevel@tonic-gate miocnak(qp, mp, 0, 0); 8980Sstevel@tonic-gate break; 8990Sstevel@tonic-gate default: 9000Sstevel@tonic-gate freemsg(mp); 9010Sstevel@tonic-gate break; 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate return; 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate if (type >= QPCTL) { 9070Sstevel@tonic-gate DBG1("(hipri) wput, %s side", zc_side(qp)); 9080Sstevel@tonic-gate switch (type) { 9090Sstevel@tonic-gate case M_READ: /* supposedly from ldterm? */ 9100Sstevel@tonic-gate DBG("zc_wput: tossing M_READ\n"); 9110Sstevel@tonic-gate freemsg(mp); 9120Sstevel@tonic-gate break; 9130Sstevel@tonic-gate case M_FLUSH: 9140Sstevel@tonic-gate handle_mflush(qp, mp); 9150Sstevel@tonic-gate break; 9160Sstevel@tonic-gate default: 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * Put this to the other side. 9190Sstevel@tonic-gate */ 9200Sstevel@tonic-gate ASSERT(zc_switch(RD(qp)) != NULL); 9210Sstevel@tonic-gate putnext(zc_switch(RD(qp)), mp); 9220Sstevel@tonic-gate break; 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate DBG1("done (hipri) wput, %s side", zc_side(qp)); 9250Sstevel@tonic-gate return; 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate /* 9290Sstevel@tonic-gate * Only putnext if there isn't already something in the queue. 9300Sstevel@tonic-gate * otherwise things would wind up out of order. 9310Sstevel@tonic-gate */ 9320Sstevel@tonic-gate if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) { 9330Sstevel@tonic-gate DBG("wput: putting message to other side\n"); 9340Sstevel@tonic-gate putnext(RD(zc_switch(qp)), mp); 9350Sstevel@tonic-gate } else { 9360Sstevel@tonic-gate DBG("wput: putting msg onto queue\n"); 9370Sstevel@tonic-gate (void) putq(qp, mp); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate DBG1("done wput, %s side", zc_side(qp)); 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate /* 9430Sstevel@tonic-gate * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both 9440Sstevel@tonic-gate * without splitting up the codepath. 9450Sstevel@tonic-gate * 9460Sstevel@tonic-gate * Enable the write side of the partner. This triggers the partner to send 9470Sstevel@tonic-gate * messages queued on its write side to this queue's read side. 9480Sstevel@tonic-gate */ 9490Sstevel@tonic-gate static void 9500Sstevel@tonic-gate zc_rsrv(queue_t *qp) 9510Sstevel@tonic-gate { 9520Sstevel@tonic-gate zc_state_t *zcs; 9530Sstevel@tonic-gate zcs = (zc_state_t *)qp->q_ptr; 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * Care must be taken here, as either of the master or slave side 9570Sstevel@tonic-gate * qptr could be NULL. 9580Sstevel@tonic-gate */ 9590Sstevel@tonic-gate ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq); 9600Sstevel@tonic-gate if (zc_switch(qp) == NULL) { 9610Sstevel@tonic-gate DBG("zc_rsrv: other side isn't listening\n"); 9620Sstevel@tonic-gate return; 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate qenable(WR(zc_switch(qp))); 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate /* 9680Sstevel@tonic-gate * This routine is symmetric for master and slave, so it handles both without 9690Sstevel@tonic-gate * splitting up the codepath. 9700Sstevel@tonic-gate * 9710Sstevel@tonic-gate * If there are messages on this queue that can be sent to the other, send 9720Sstevel@tonic-gate * them via putnext(). Else, if queued messages cannot be sent, leave them 9730Sstevel@tonic-gate * on this queue. 9740Sstevel@tonic-gate */ 9750Sstevel@tonic-gate static void 9760Sstevel@tonic-gate zc_wsrv(queue_t *qp) 9770Sstevel@tonic-gate { 9780Sstevel@tonic-gate mblk_t *mp; 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate DBG1("zc_wsrv master (%s) side", zc_side(qp)); 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * Partner has no read queue, so take the data, and throw it away. 9840Sstevel@tonic-gate */ 9850Sstevel@tonic-gate if (zc_switch(RD(qp)) == NULL) { 9860Sstevel@tonic-gate DBG("zc_wsrv: other side isn't listening"); 9870Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 9880Sstevel@tonic-gate if (mp->b_datap->db_type == M_IOCTL) 9890Sstevel@tonic-gate miocnak(qp, mp, 0, 0); 9900Sstevel@tonic-gate else 9910Sstevel@tonic-gate freemsg(mp); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate flushq(qp, FLUSHALL); 9940Sstevel@tonic-gate return; 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * while there are messages on this write queue... 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 10010Sstevel@tonic-gate /* 10020Sstevel@tonic-gate * Due to the way zc_wput is implemented, we should never 10030Sstevel@tonic-gate * see a control message here. 10040Sstevel@tonic-gate */ 10050Sstevel@tonic-gate ASSERT(mp->b_datap->db_type < QPCTL); 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) { 10080Sstevel@tonic-gate DBG("wsrv: send message to other side\n"); 10090Sstevel@tonic-gate putnext(RD(zc_switch(qp)), mp); 10100Sstevel@tonic-gate } else { 10110Sstevel@tonic-gate DBG("wsrv: putting msg back on queue\n"); 10120Sstevel@tonic-gate (void) putbq(qp, mp); 10130Sstevel@tonic-gate break; 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate } 1017