1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * sun4v machine description driver 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/open.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/cred.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/uio.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/ksynch.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/devops.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #include <sys/mdesc.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/mach_descrip.h> 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate #define MDESC_NAME "mdesc" 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /* 55*7c478bd9Sstevel@tonic-gate * Operational state flags 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate #define MDESC_DIDMINOR 0x2 /* Created minors */ 59*7c478bd9Sstevel@tonic-gate #define MDESC_DIDMUTEX 0x8 /* Created mutex */ 60*7c478bd9Sstevel@tonic-gate #define MDESC_DIDCV 0x10 /* Created cv */ 61*7c478bd9Sstevel@tonic-gate #define MDESC_BUSY 0x20 /* Device is busy */ 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate static void *mdesc_state_head; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate struct mdesc_state { 66*7c478bd9Sstevel@tonic-gate int instance; 67*7c478bd9Sstevel@tonic-gate dev_info_t *devi; 68*7c478bd9Sstevel@tonic-gate kmutex_t lock; 69*7c478bd9Sstevel@tonic-gate kcondvar_t cv; 70*7c478bd9Sstevel@tonic-gate size_t mdesc_len; 71*7c478bd9Sstevel@tonic-gate uint8_t *mdesc; 72*7c478bd9Sstevel@tonic-gate int flags; 73*7c478bd9Sstevel@tonic-gate }; 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate static int mdesc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 76*7c478bd9Sstevel@tonic-gate static int mdesc_attach(dev_info_t *, ddi_attach_cmd_t); 77*7c478bd9Sstevel@tonic-gate static int mdesc_detach(dev_info_t *, ddi_detach_cmd_t); 78*7c478bd9Sstevel@tonic-gate static int mdesc_open(dev_t *, int, int, cred_t *); 79*7c478bd9Sstevel@tonic-gate static int mdesc_close(dev_t, int, int, cred_t *); 80*7c478bd9Sstevel@tonic-gate static int mdesc_read(dev_t, struct uio *, cred_t *); 81*7c478bd9Sstevel@tonic-gate static int mdesc_write(dev_t, struct uio *, cred_t *); 82*7c478bd9Sstevel@tonic-gate static int mdesc_rw(dev_t, struct uio *, enum uio_rw); 83*7c478bd9Sstevel@tonic-gate static int mdesc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate static struct cb_ops mdesc_cb_ops = { 86*7c478bd9Sstevel@tonic-gate mdesc_open, /* cb_open */ 87*7c478bd9Sstevel@tonic-gate mdesc_close, /* cb_close */ 88*7c478bd9Sstevel@tonic-gate nodev, /* cb_strategy */ 89*7c478bd9Sstevel@tonic-gate nodev, /* cb_print */ 90*7c478bd9Sstevel@tonic-gate nodev, /* cb_dump */ 91*7c478bd9Sstevel@tonic-gate mdesc_read, /* cb_read */ 92*7c478bd9Sstevel@tonic-gate nodev, /* cb_write */ 93*7c478bd9Sstevel@tonic-gate mdesc_ioctl, /* cb_ioctl */ 94*7c478bd9Sstevel@tonic-gate nodev, /* cb_devmap */ 95*7c478bd9Sstevel@tonic-gate nodev, /* cb_mmap */ 96*7c478bd9Sstevel@tonic-gate nodev, /* cb_segmap */ 97*7c478bd9Sstevel@tonic-gate nochpoll, /* cb_chpoll */ 98*7c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 99*7c478bd9Sstevel@tonic-gate (struct streamtab *)NULL, /* cb_str */ 100*7c478bd9Sstevel@tonic-gate D_MP | D_64BIT, /* cb_flag */ 101*7c478bd9Sstevel@tonic-gate CB_REV, /* cb_rev */ 102*7c478bd9Sstevel@tonic-gate nodev, /* cb_aread */ 103*7c478bd9Sstevel@tonic-gate nodev /* cb_awrite */ 104*7c478bd9Sstevel@tonic-gate }; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate static struct dev_ops mdesc_dev_ops = { 107*7c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 108*7c478bd9Sstevel@tonic-gate 0, /* devo_refcnt */ 109*7c478bd9Sstevel@tonic-gate mdesc_getinfo, /* devo_getinfo */ 110*7c478bd9Sstevel@tonic-gate nulldev, /* devo_identify */ 111*7c478bd9Sstevel@tonic-gate nulldev, /* devo_probe */ 112*7c478bd9Sstevel@tonic-gate mdesc_attach, /* devo_attach */ 113*7c478bd9Sstevel@tonic-gate mdesc_detach, /* devo_detach */ 114*7c478bd9Sstevel@tonic-gate nodev, /* devo_reset */ 115*7c478bd9Sstevel@tonic-gate &mdesc_cb_ops, /* devo_cb_ops */ 116*7c478bd9Sstevel@tonic-gate (struct bus_ops *)NULL, /* devo_bus_ops */ 117*7c478bd9Sstevel@tonic-gate nulldev /* devo_power */ 118*7c478bd9Sstevel@tonic-gate }; 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 121*7c478bd9Sstevel@tonic-gate &mod_driverops, 122*7c478bd9Sstevel@tonic-gate "Machine Description Driver 1.0", 123*7c478bd9Sstevel@tonic-gate &mdesc_dev_ops}; 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 126*7c478bd9Sstevel@tonic-gate MODREV_1, 127*7c478bd9Sstevel@tonic-gate (void *)&modldrv, 128*7c478bd9Sstevel@tonic-gate NULL 129*7c478bd9Sstevel@tonic-gate }; 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate int 139*7c478bd9Sstevel@tonic-gate _init(void) 140*7c478bd9Sstevel@tonic-gate { 141*7c478bd9Sstevel@tonic-gate int retval; 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate if ((retval = ddi_soft_state_init(&mdesc_state_head, 144*7c478bd9Sstevel@tonic-gate sizeof (struct mdesc_state), 1)) != 0) 145*7c478bd9Sstevel@tonic-gate return (retval); 146*7c478bd9Sstevel@tonic-gate if ((retval = mod_install(&modlinkage)) != 0) { 147*7c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&mdesc_state_head); 148*7c478bd9Sstevel@tonic-gate return (retval); 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate return (retval); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate int 158*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 159*7c478bd9Sstevel@tonic-gate { 160*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 161*7c478bd9Sstevel@tonic-gate } 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate int 167*7c478bd9Sstevel@tonic-gate _fini(void) 168*7c478bd9Sstevel@tonic-gate { 169*7c478bd9Sstevel@tonic-gate int retval; 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate if ((retval = mod_remove(&modlinkage)) != 0) 172*7c478bd9Sstevel@tonic-gate return (retval); 173*7c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&mdesc_state_head); 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate return (retval); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 182*7c478bd9Sstevel@tonic-gate static int 183*7c478bd9Sstevel@tonic-gate mdesc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 184*7c478bd9Sstevel@tonic-gate { 185*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 186*7c478bd9Sstevel@tonic-gate int retval = DDI_FAILURE; 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate ASSERT(resultp != NULL); 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate switch (cmd) { 191*7c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 192*7c478bd9Sstevel@tonic-gate if ((mdsp = ddi_get_soft_state(mdesc_state_head, 193*7c478bd9Sstevel@tonic-gate getminor((dev_t)arg))) != NULL) { 194*7c478bd9Sstevel@tonic-gate *resultp = mdsp->devi; 195*7c478bd9Sstevel@tonic-gate retval = DDI_SUCCESS; 196*7c478bd9Sstevel@tonic-gate } else 197*7c478bd9Sstevel@tonic-gate *resultp = NULL; 198*7c478bd9Sstevel@tonic-gate break; 199*7c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 200*7c478bd9Sstevel@tonic-gate *resultp = (void *)getminor((dev_t)arg); 201*7c478bd9Sstevel@tonic-gate retval = DDI_SUCCESS; 202*7c478bd9Sstevel@tonic-gate break; 203*7c478bd9Sstevel@tonic-gate } 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate return (retval); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate static int 212*7c478bd9Sstevel@tonic-gate mdesc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 213*7c478bd9Sstevel@tonic-gate { 214*7c478bd9Sstevel@tonic-gate int instance = ddi_get_instance(dip); 215*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate switch (cmd) { 218*7c478bd9Sstevel@tonic-gate case DDI_ATTACH: 219*7c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(mdesc_state_head, instance) != 220*7c478bd9Sstevel@tonic-gate DDI_SUCCESS) { 221*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s@%d: Unable to allocate state", 222*7c478bd9Sstevel@tonic-gate MDESC_NAME, instance); 223*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 224*7c478bd9Sstevel@tonic-gate } 225*7c478bd9Sstevel@tonic-gate if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == 226*7c478bd9Sstevel@tonic-gate NULL) { 227*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s@%d: Unable to obtain state", 228*7c478bd9Sstevel@tonic-gate MDESC_NAME, instance); 229*7c478bd9Sstevel@tonic-gate ddi_soft_state_free(dip, instance); 230*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, MDESC_NAME, S_IFCHR, instance, 233*7c478bd9Sstevel@tonic-gate DDI_PSEUDO, 0) != DDI_SUCCESS) { 234*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s@%d: Unable to create minor node", 235*7c478bd9Sstevel@tonic-gate MDESC_NAME, instance); 236*7c478bd9Sstevel@tonic-gate (void) mdesc_detach(dip, DDI_DETACH); 237*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate mdsp->flags |= MDESC_DIDMINOR; 240*7c478bd9Sstevel@tonic-gate 241*7c478bd9Sstevel@tonic-gate mdsp->instance = instance; 242*7c478bd9Sstevel@tonic-gate mdsp->devi = dip; 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate mutex_init(&mdsp->lock, NULL, MUTEX_DRIVER, NULL); 245*7c478bd9Sstevel@tonic-gate mdsp->flags |= MDESC_DIDMUTEX; 246*7c478bd9Sstevel@tonic-gate 247*7c478bd9Sstevel@tonic-gate cv_init(&mdsp->cv, NULL, CV_DRIVER, NULL); 248*7c478bd9Sstevel@tonic-gate mdsp->flags |= MDESC_DIDCV; 249*7c478bd9Sstevel@tonic-gate 250*7c478bd9Sstevel@tonic-gate /* point the driver at the kernel's copy of the data */ 251*7c478bd9Sstevel@tonic-gate mdsp->mdesc = (uint8_t *)machine_descrip.va; 252*7c478bd9Sstevel@tonic-gate mdsp->mdesc_len = (machine_descrip.va != NULL) ? 253*7c478bd9Sstevel@tonic-gate machine_descrip.size : 0; 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 256*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 257*7c478bd9Sstevel@tonic-gate case DDI_RESUME: 258*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 259*7c478bd9Sstevel@tonic-gate default: 260*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 261*7c478bd9Sstevel@tonic-gate } 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate static int 267*7c478bd9Sstevel@tonic-gate mdesc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 268*7c478bd9Sstevel@tonic-gate { 269*7c478bd9Sstevel@tonic-gate int instance = ddi_get_instance(dip); 270*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate switch (cmd) { 273*7c478bd9Sstevel@tonic-gate case DDI_DETACH: 274*7c478bd9Sstevel@tonic-gate mdsp = ddi_get_soft_state(mdesc_state_head, instance); 275*7c478bd9Sstevel@tonic-gate if (mdsp != NULL) { 276*7c478bd9Sstevel@tonic-gate ASSERT(!(mdsp->flags & MDESC_BUSY)); 277*7c478bd9Sstevel@tonic-gate if (mdsp->flags & MDESC_DIDCV) 278*7c478bd9Sstevel@tonic-gate cv_destroy(&mdsp->cv); 279*7c478bd9Sstevel@tonic-gate if (mdsp->flags & MDESC_DIDMUTEX) 280*7c478bd9Sstevel@tonic-gate mutex_destroy(&mdsp->lock); 281*7c478bd9Sstevel@tonic-gate if (mdsp->flags & MDESC_DIDMINOR) 282*7c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 283*7c478bd9Sstevel@tonic-gate } 284*7c478bd9Sstevel@tonic-gate ddi_soft_state_free(mdesc_state_head, instance); 285*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 288*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate default: 291*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 292*7c478bd9Sstevel@tonic-gate } 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate 297*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 298*7c478bd9Sstevel@tonic-gate static int 299*7c478bd9Sstevel@tonic-gate mdesc_open(dev_t *devp, int flag, int otyp, cred_t *credp) 300*7c478bd9Sstevel@tonic-gate { 301*7c478bd9Sstevel@tonic-gate int instance = getminor(*devp); 302*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 303*7c478bd9Sstevel@tonic-gate 304*7c478bd9Sstevel@tonic-gate if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL) 305*7c478bd9Sstevel@tonic-gate return (ENXIO); 306*7c478bd9Sstevel@tonic-gate 307*7c478bd9Sstevel@tonic-gate ASSERT(mdsp->instance == instance); 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 310*7c478bd9Sstevel@tonic-gate return (EINVAL); 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate return (0); 313*7c478bd9Sstevel@tonic-gate } 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate 318*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 319*7c478bd9Sstevel@tonic-gate static int 320*7c478bd9Sstevel@tonic-gate mdesc_close(dev_t dev, int flag, int otyp, cred_t *credp) 321*7c478bd9Sstevel@tonic-gate { 322*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 323*7c478bd9Sstevel@tonic-gate int instance = getminor(dev); 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL) 326*7c478bd9Sstevel@tonic-gate return (ENXIO); 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate ASSERT(mdsp->instance == instance); 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 331*7c478bd9Sstevel@tonic-gate return (EINVAL); 332*7c478bd9Sstevel@tonic-gate 333*7c478bd9Sstevel@tonic-gate return (0); 334*7c478bd9Sstevel@tonic-gate } 335*7c478bd9Sstevel@tonic-gate 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate 338*7c478bd9Sstevel@tonic-gate 339*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 340*7c478bd9Sstevel@tonic-gate static int 341*7c478bd9Sstevel@tonic-gate mdesc_read(dev_t dev, struct uio *uiop, cred_t *credp) 342*7c478bd9Sstevel@tonic-gate { 343*7c478bd9Sstevel@tonic-gate return (mdesc_rw(dev, uiop, UIO_READ)); 344*7c478bd9Sstevel@tonic-gate } 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate 349*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 350*7c478bd9Sstevel@tonic-gate static int 351*7c478bd9Sstevel@tonic-gate mdesc_write(dev_t dev, struct uio *uiop, cred_t *credp) 352*7c478bd9Sstevel@tonic-gate { 353*7c478bd9Sstevel@tonic-gate return (ENXIO); /* This driver version does not allow updates */ 354*7c478bd9Sstevel@tonic-gate } 355*7c478bd9Sstevel@tonic-gate 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate 358*7c478bd9Sstevel@tonic-gate 359*7c478bd9Sstevel@tonic-gate static int 360*7c478bd9Sstevel@tonic-gate mdesc_rw(dev_t dev, struct uio *uiop, enum uio_rw rw) 361*7c478bd9Sstevel@tonic-gate { 362*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 363*7c478bd9Sstevel@tonic-gate int instance = getminor(dev); 364*7c478bd9Sstevel@tonic-gate size_t len; 365*7c478bd9Sstevel@tonic-gate int retval; 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate len = uiop->uio_resid; 368*7c478bd9Sstevel@tonic-gate 369*7c478bd9Sstevel@tonic-gate if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL) 370*7c478bd9Sstevel@tonic-gate return (ENXIO); 371*7c478bd9Sstevel@tonic-gate 372*7c478bd9Sstevel@tonic-gate ASSERT(mdsp->instance == instance); 373*7c478bd9Sstevel@tonic-gate 374*7c478bd9Sstevel@tonic-gate if (len == 0) 375*7c478bd9Sstevel@tonic-gate return (0); 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate mutex_enter(&mdsp->lock); 378*7c478bd9Sstevel@tonic-gate 379*7c478bd9Sstevel@tonic-gate while (mdsp->flags & MDESC_BUSY) { 380*7c478bd9Sstevel@tonic-gate if (cv_wait_sig(&mdsp->cv, &mdsp->lock) == 0) { 381*7c478bd9Sstevel@tonic-gate mutex_exit(&mdsp->lock); 382*7c478bd9Sstevel@tonic-gate return (EINTR); 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate } 385*7c478bd9Sstevel@tonic-gate 386*7c478bd9Sstevel@tonic-gate if (uiop->uio_offset < 0 || uiop->uio_offset > mdsp->mdesc_len) { 387*7c478bd9Sstevel@tonic-gate mutex_exit(&mdsp->lock); 388*7c478bd9Sstevel@tonic-gate return (EINVAL); 389*7c478bd9Sstevel@tonic-gate } 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate if (len > (mdsp->mdesc_len - uiop->uio_offset)) 392*7c478bd9Sstevel@tonic-gate len = mdsp->mdesc_len - uiop->uio_offset; 393*7c478bd9Sstevel@tonic-gate 394*7c478bd9Sstevel@tonic-gate /* already checked that offset<mdesc_len above */ 395*7c478bd9Sstevel@tonic-gate if (len == 0) { 396*7c478bd9Sstevel@tonic-gate mutex_exit(&mdsp->lock); 397*7c478bd9Sstevel@tonic-gate return (rw == UIO_WRITE ? ENOSPC : 0); 398*7c478bd9Sstevel@tonic-gate } 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate mdsp->flags |= MDESC_BUSY; 401*7c478bd9Sstevel@tonic-gate mutex_exit(&mdsp->lock); 402*7c478bd9Sstevel@tonic-gate 403*7c478bd9Sstevel@tonic-gate retval = uiomove((void *)(mdsp->mdesc + uiop->uio_offset), 404*7c478bd9Sstevel@tonic-gate len, rw, uiop); 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate mutex_enter(&mdsp->lock); 407*7c478bd9Sstevel@tonic-gate mdsp->flags &= ~MDESC_BUSY; 408*7c478bd9Sstevel@tonic-gate cv_broadcast(&mdsp->cv); 409*7c478bd9Sstevel@tonic-gate mutex_exit(&mdsp->lock); 410*7c478bd9Sstevel@tonic-gate 411*7c478bd9Sstevel@tonic-gate return (retval); 412*7c478bd9Sstevel@tonic-gate } 413*7c478bd9Sstevel@tonic-gate 414*7c478bd9Sstevel@tonic-gate 415*7c478bd9Sstevel@tonic-gate 416*7c478bd9Sstevel@tonic-gate 417*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 418*7c478bd9Sstevel@tonic-gate static int 419*7c478bd9Sstevel@tonic-gate mdesc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 420*7c478bd9Sstevel@tonic-gate int *rvalp) 421*7c478bd9Sstevel@tonic-gate { 422*7c478bd9Sstevel@tonic-gate struct mdesc_state *mdsp; 423*7c478bd9Sstevel@tonic-gate int instance = getminor(dev); 424*7c478bd9Sstevel@tonic-gate 425*7c478bd9Sstevel@tonic-gate if ((mdsp = ddi_get_soft_state(mdesc_state_head, instance)) == NULL) 426*7c478bd9Sstevel@tonic-gate return (ENXIO); 427*7c478bd9Sstevel@tonic-gate 428*7c478bd9Sstevel@tonic-gate ASSERT(mdsp->instance == instance); 429*7c478bd9Sstevel@tonic-gate 430*7c478bd9Sstevel@tonic-gate switch (cmd) { 431*7c478bd9Sstevel@tonic-gate case MDESCIOCGSZ: { 432*7c478bd9Sstevel@tonic-gate /* 433*7c478bd9Sstevel@tonic-gate * We are not guaranteed that ddi_copyout(9F) will read 434*7c478bd9Sstevel@tonic-gate * atomically anything larger than a byte. Therefore we 435*7c478bd9Sstevel@tonic-gate * must duplicate the size before copying it out to the user. 436*7c478bd9Sstevel@tonic-gate */ 437*7c478bd9Sstevel@tonic-gate size_t sz = mdsp->mdesc_len; 438*7c478bd9Sstevel@tonic-gate 439*7c478bd9Sstevel@tonic-gate if (!(mode & FREAD)) 440*7c478bd9Sstevel@tonic-gate return (EACCES); 441*7c478bd9Sstevel@tonic-gate 442*7c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL 443*7c478bd9Sstevel@tonic-gate switch (ddi_model_convert_from(mode & FMODELS)) { 444*7c478bd9Sstevel@tonic-gate case DDI_MODEL_ILP32: { 445*7c478bd9Sstevel@tonic-gate size32_t sz32 = (size32_t)sz; 446*7c478bd9Sstevel@tonic-gate if (ddi_copyout(&sz32, (void *)arg, sizeof (size32_t), 447*7c478bd9Sstevel@tonic-gate mode) != 0) 448*7c478bd9Sstevel@tonic-gate return (EFAULT); 449*7c478bd9Sstevel@tonic-gate return (0); 450*7c478bd9Sstevel@tonic-gate } 451*7c478bd9Sstevel@tonic-gate case DDI_MODEL_NONE: 452*7c478bd9Sstevel@tonic-gate if (ddi_copyout(&sz, (void *)arg, sizeof (size_t), 453*7c478bd9Sstevel@tonic-gate mode) != 0) 454*7c478bd9Sstevel@tonic-gate return (EFAULT); 455*7c478bd9Sstevel@tonic-gate return (0); 456*7c478bd9Sstevel@tonic-gate default: 457*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 458*7c478bd9Sstevel@tonic-gate "mdesc: Invalid data model %d in ioctl\n", 459*7c478bd9Sstevel@tonic-gate ddi_model_convert_from(mode & FMODELS)); 460*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 461*7c478bd9Sstevel@tonic-gate } 462*7c478bd9Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */ 463*7c478bd9Sstevel@tonic-gate if (ddi_copyout(&sz, (void *)arg, sizeof (size_t), mode) != 0) 464*7c478bd9Sstevel@tonic-gate return (EFAULT); 465*7c478bd9Sstevel@tonic-gate return (0); 466*7c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */ 467*7c478bd9Sstevel@tonic-gate } 468*7c478bd9Sstevel@tonic-gate 469*7c478bd9Sstevel@tonic-gate default: 470*7c478bd9Sstevel@tonic-gate return (ENOTTY); 471*7c478bd9Sstevel@tonic-gate } 472*7c478bd9Sstevel@tonic-gate } 473