1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Standard module for handling DLPI Style 2 attach/detach 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/types.h> 34*0Sstevel@tonic-gate #include <sys/conf.h> 35*0Sstevel@tonic-gate #include <sys/modctl.h> 36*0Sstevel@tonic-gate #include <sys/cmn_err.h> 37*0Sstevel@tonic-gate #include <sys/sunddi.h> 38*0Sstevel@tonic-gate #include <sys/esunddi.h> 39*0Sstevel@tonic-gate #include <sys/strsubr.h> 40*0Sstevel@tonic-gate #include <sys/ddi.h> 41*0Sstevel@tonic-gate #include <sys/dlpi.h> 42*0Sstevel@tonic-gate #include <sys/strsun.h> 43*0Sstevel@tonic-gate #include <sys/policy.h> 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate static struct streamtab drstab; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate static struct fmodsw fsw = { 48*0Sstevel@tonic-gate DRMODNAME, 49*0Sstevel@tonic-gate &drstab, 50*0Sstevel@tonic-gate D_MP 51*0Sstevel@tonic-gate }; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate /* 55*0Sstevel@tonic-gate * Module linkage information for the kernel. 56*0Sstevel@tonic-gate */ 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = { 59*0Sstevel@tonic-gate &mod_strmodops, "dr compatibility for DLPI style 2 drivers %I%", &fsw 60*0Sstevel@tonic-gate }; 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate static struct modlinkage modlinkage = { 64*0Sstevel@tonic-gate MODREV_1, &modlstrmod, NULL 65*0Sstevel@tonic-gate }; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate int 69*0Sstevel@tonic-gate _init(void) 70*0Sstevel@tonic-gate { 71*0Sstevel@tonic-gate return (mod_install(&modlinkage)); 72*0Sstevel@tonic-gate } 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate int 75*0Sstevel@tonic-gate _fini(void) 76*0Sstevel@tonic-gate { 77*0Sstevel@tonic-gate return (mod_remove(&modlinkage)); 78*0Sstevel@tonic-gate } 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate int 81*0Sstevel@tonic-gate _info(struct modinfo *modinfop) 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate static int dropen(queue_t *, dev_t *, int, int, cred_t *); 88*0Sstevel@tonic-gate static int drclose(queue_t *, int, cred_t *); 89*0Sstevel@tonic-gate static int drrput(queue_t *, mblk_t *); 90*0Sstevel@tonic-gate static int drwput(queue_t *, mblk_t *); 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate static struct module_info drinfo = { 93*0Sstevel@tonic-gate 0, 94*0Sstevel@tonic-gate DRMODNAME, 95*0Sstevel@tonic-gate 0, 96*0Sstevel@tonic-gate INFPSZ, 97*0Sstevel@tonic-gate 1, 98*0Sstevel@tonic-gate 0 99*0Sstevel@tonic-gate }; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate static struct qinit drrinit = { 102*0Sstevel@tonic-gate (int (*)())drrput, 103*0Sstevel@tonic-gate NULL, 104*0Sstevel@tonic-gate dropen, 105*0Sstevel@tonic-gate drclose, 106*0Sstevel@tonic-gate NULL, 107*0Sstevel@tonic-gate &drinfo 108*0Sstevel@tonic-gate }; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate static struct qinit drwinit = { 111*0Sstevel@tonic-gate (int (*)())drwput, 112*0Sstevel@tonic-gate NULL, 113*0Sstevel@tonic-gate NULL, 114*0Sstevel@tonic-gate NULL, 115*0Sstevel@tonic-gate NULL, 116*0Sstevel@tonic-gate &drinfo 117*0Sstevel@tonic-gate }; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate static struct streamtab drstab = { 120*0Sstevel@tonic-gate &drrinit, 121*0Sstevel@tonic-gate &drwinit, 122*0Sstevel@tonic-gate NULL, 123*0Sstevel@tonic-gate NULL 124*0Sstevel@tonic-gate }; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate /* 127*0Sstevel@tonic-gate * This module is pushed directly on top of the bottom driver 128*0Sstevel@tonic-gate * in a DLPI style-2 stream by stropen(). It intercepts 129*0Sstevel@tonic-gate * DL_ATTACH_REQ/DL_DETACH_REQ messages on the write side 130*0Sstevel@tonic-gate * and acks on the read side, calls qassociate where needed. 131*0Sstevel@tonic-gate * The primary purpose is to workaround a DR race condition 132*0Sstevel@tonic-gate * affecting non-DDI compliant DLPI style 2 drivers, which may 133*0Sstevel@tonic-gate * cause the system to panic. 134*0Sstevel@tonic-gate * 135*0Sstevel@tonic-gate * The following action is taken: 136*0Sstevel@tonic-gate * Write side (drwput): 137*0Sstevel@tonic-gate * attach request: hold driver instance assuming ppa == instance. 138*0Sstevel@tonic-gate * This way, the instance cannot be detached while the 139*0Sstevel@tonic-gate * driver is processing DL_ATTACH_REQ. 140*0Sstevel@tonic-gate * 141*0Sstevel@tonic-gate * On a successful hold, store the dip in a ring buffer 142*0Sstevel@tonic-gate * to be processed lated by the read side. 143*0Sstevel@tonic-gate * If hold fails (most likely ppa != instance), we store 144*0Sstevel@tonic-gate * NULL in the ring buffer and read side won't take 145*0Sstevel@tonic-gate * any action on ack. 146*0Sstevel@tonic-gate * 147*0Sstevel@tonic-gate * Read side (drrput): 148*0Sstevel@tonic-gate * attach success: if (dip held on write side) associate queue with dip 149*0Sstevel@tonic-gate * attach failure: if (dip held on write side) release hold on dip 150*0Sstevel@tonic-gate * detach success: associate queue with NULL 151*0Sstevel@tonic-gate * detach failure: do nothing 152*0Sstevel@tonic-gate * 153*0Sstevel@tonic-gate * The module assumes that incoming DL_ATTACH_REQ/DL_DETACH_REQ 154*0Sstevel@tonic-gate * messages are ordered (non-concurrent) and the bottom 155*0Sstevel@tonic-gate * driver processes them and sends acknowledgements in the same 156*0Sstevel@tonic-gate * order. This assumption is reasonable because concurrent 157*0Sstevel@tonic-gate * association results in non-deterministic queue behavior. 158*0Sstevel@tonic-gate * The module is coded carefully such that unordered messages 159*0Sstevel@tonic-gate * do not result in a system panic. 160*0Sstevel@tonic-gate * 161*0Sstevel@tonic-gate * The module handles multiple outstanding messages queued 162*0Sstevel@tonic-gate * in the bottom driver. Messages processed on the write side 163*0Sstevel@tonic-gate * but not yet arrived at read side are placed in the ring buffer 164*0Sstevel@tonic-gate * dr_dip[], between dr_nfirst and dr_nlast. The write side is 165*0Sstevel@tonic-gate * producer and the read side is the consumer. The buffer is full 166*0Sstevel@tonic-gate * when dr_nfirst == dr_nlast. 167*0Sstevel@tonic-gate * 168*0Sstevel@tonic-gate * The current size of the ring buffer is 64 (MAX_DLREQS) per stream. 169*0Sstevel@tonic-gate * During normal testing, we have not seen outstanding messages 170*0Sstevel@tonic-gate * above 10. 171*0Sstevel@tonic-gate */ 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate #define MAX_DLREQS 64 174*0Sstevel@tonic-gate #define INCR(x) {(x)++; if ((x) >= MAX_DLREQS) (x) = 0; } 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate struct drstate { 177*0Sstevel@tonic-gate kmutex_t dr_lock; 178*0Sstevel@tonic-gate major_t dr_major; 179*0Sstevel@tonic-gate int dr_nfirst; 180*0Sstevel@tonic-gate int dr_nlast; 181*0Sstevel@tonic-gate dev_info_t *dr_dip[MAX_DLREQS]; 182*0Sstevel@tonic-gate }; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate /* ARGSUSED1 */ 185*0Sstevel@tonic-gate static int 186*0Sstevel@tonic-gate dropen(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *crp) 187*0Sstevel@tonic-gate { 188*0Sstevel@tonic-gate struct drstate *dsp; 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate if (sflag != MODOPEN) { /* must be a pushed module */ 191*0Sstevel@tonic-gate return (EINVAL); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate if (secpolicy_net_rawaccess(crp) != 0) { 195*0Sstevel@tonic-gate return (EPERM); 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate if (q->q_ptr != NULL) { 199*0Sstevel@tonic-gate return (0); /* already open */ 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate dsp = kmem_zalloc(sizeof (*dsp), KM_SLEEP); 203*0Sstevel@tonic-gate dsp->dr_major = getmajor(*devp); 204*0Sstevel@tonic-gate mutex_init(&dsp->dr_lock, NULL, MUTEX_DEFAULT, NULL); 205*0Sstevel@tonic-gate q->q_ptr = OTHERQ(q)->q_ptr = dsp; 206*0Sstevel@tonic-gate qprocson(q); 207*0Sstevel@tonic-gate ddi_assoc_queue_with_devi(q, NULL); 208*0Sstevel@tonic-gate return (0); 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate /* ARGSUSED1 */ 212*0Sstevel@tonic-gate static int 213*0Sstevel@tonic-gate drclose(queue_t *q, int cflag, cred_t *crp) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate struct drstate *dsp = q->q_ptr; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate ASSERT(dsp); 218*0Sstevel@tonic-gate ddi_assoc_queue_with_devi(q, NULL); 219*0Sstevel@tonic-gate qprocsoff(q); 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate mutex_destroy(&dsp->dr_lock); 222*0Sstevel@tonic-gate kmem_free(dsp, sizeof (*dsp)); 223*0Sstevel@tonic-gate q->q_ptr = NULL; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate return (0); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate static int 229*0Sstevel@tonic-gate drrput(queue_t *q, mblk_t *mp) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate struct drstate *dsp; 232*0Sstevel@tonic-gate union DL_primitives *dlp; 233*0Sstevel@tonic-gate dev_info_t *dip; 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate switch (DB_TYPE(mp)) { 236*0Sstevel@tonic-gate case M_PROTO: 237*0Sstevel@tonic-gate case M_PCPROTO: 238*0Sstevel@tonic-gate break; 239*0Sstevel@tonic-gate default: 240*0Sstevel@tonic-gate putnext(q, mp); 241*0Sstevel@tonic-gate return (0); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate /* make sure size is sufficient for dl_primitive */ 245*0Sstevel@tonic-gate if (MBLKL(mp) < sizeof (t_uscalar_t)) { 246*0Sstevel@tonic-gate putnext(q, mp); 247*0Sstevel@tonic-gate return (0); 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate dlp = (union DL_primitives *)mp->b_rptr; 251*0Sstevel@tonic-gate switch (dlp->dl_primitive) { 252*0Sstevel@tonic-gate case DL_OK_ACK: { 253*0Sstevel@tonic-gate /* check for proper size, let upper layer deal with error */ 254*0Sstevel@tonic-gate if (MBLKL(mp) < DL_OK_ACK_SIZE) { 255*0Sstevel@tonic-gate putnext(q, mp); 256*0Sstevel@tonic-gate return (0); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate dsp = q->q_ptr; 260*0Sstevel@tonic-gate switch (dlp->ok_ack.dl_correct_primitive) { 261*0Sstevel@tonic-gate case DL_ATTACH_REQ: 262*0Sstevel@tonic-gate /* 263*0Sstevel@tonic-gate * ddi_assoc_queue_with_devi() will hold dip, 264*0Sstevel@tonic-gate * so release after association. 265*0Sstevel@tonic-gate * 266*0Sstevel@tonic-gate * dip is NULL means we didn't hold dip on read side. 267*0Sstevel@tonic-gate * (unlikely, but possible), so we do nothing. 268*0Sstevel@tonic-gate */ 269*0Sstevel@tonic-gate mutex_enter(&dsp->dr_lock); 270*0Sstevel@tonic-gate dip = dsp->dr_dip[dsp->dr_nlast]; 271*0Sstevel@tonic-gate dsp->dr_dip[dsp->dr_nlast] = NULL; 272*0Sstevel@tonic-gate INCR(dsp->dr_nlast); 273*0Sstevel@tonic-gate mutex_exit(&dsp->dr_lock); 274*0Sstevel@tonic-gate if (dip) { 275*0Sstevel@tonic-gate ddi_assoc_queue_with_devi(q, dip); 276*0Sstevel@tonic-gate ddi_release_devi(dip); 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate break; 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate case DL_DETACH_REQ: 281*0Sstevel@tonic-gate ddi_assoc_queue_with_devi(q, NULL); 282*0Sstevel@tonic-gate break; 283*0Sstevel@tonic-gate default: 284*0Sstevel@tonic-gate break; 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate break; 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate case DL_ERROR_ACK: 289*0Sstevel@tonic-gate if (dlp->error_ack.dl_error_primitive != DL_ATTACH_REQ) 290*0Sstevel@tonic-gate break; 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate dsp = q->q_ptr; 293*0Sstevel@tonic-gate mutex_enter(&dsp->dr_lock); 294*0Sstevel@tonic-gate dip = dsp->dr_dip[dsp->dr_nlast]; 295*0Sstevel@tonic-gate dsp->dr_dip[dsp->dr_nlast] = NULL; 296*0Sstevel@tonic-gate INCR(dsp->dr_nlast); 297*0Sstevel@tonic-gate mutex_exit(&dsp->dr_lock); 298*0Sstevel@tonic-gate /* 299*0Sstevel@tonic-gate * Release dip on attach failure 300*0Sstevel@tonic-gate */ 301*0Sstevel@tonic-gate if (dip) { 302*0Sstevel@tonic-gate ddi_release_devi(dip); 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate break; 305*0Sstevel@tonic-gate default: 306*0Sstevel@tonic-gate break; 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate putnext(q, mp); 310*0Sstevel@tonic-gate return (0); 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate /* 314*0Sstevel@tonic-gate * Detect dl attach, hold the dip to prevent it from detaching 315*0Sstevel@tonic-gate */ 316*0Sstevel@tonic-gate static int 317*0Sstevel@tonic-gate drwput(queue_t *q, mblk_t *mp) 318*0Sstevel@tonic-gate { 319*0Sstevel@tonic-gate struct drstate *dsp; 320*0Sstevel@tonic-gate union DL_primitives *dlp; 321*0Sstevel@tonic-gate dev_info_t *dip; 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate switch (DB_TYPE(mp)) { 324*0Sstevel@tonic-gate case M_PROTO: 325*0Sstevel@tonic-gate case M_PCPROTO: 326*0Sstevel@tonic-gate break; 327*0Sstevel@tonic-gate default: 328*0Sstevel@tonic-gate putnext(q, mp); 329*0Sstevel@tonic-gate return (0); 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate /* make sure size is sufficient for dl_primitive */ 333*0Sstevel@tonic-gate if (MBLKL(mp) < sizeof (t_uscalar_t)) { 334*0Sstevel@tonic-gate putnext(q, mp); 335*0Sstevel@tonic-gate return (0); 336*0Sstevel@tonic-gate } 337*0Sstevel@tonic-gate 338*0Sstevel@tonic-gate dlp = (union DL_primitives *)mp->b_rptr; 339*0Sstevel@tonic-gate switch (dlp->dl_primitive) { 340*0Sstevel@tonic-gate case DL_ATTACH_REQ: 341*0Sstevel@tonic-gate /* 342*0Sstevel@tonic-gate * Check for proper size of the message. 343*0Sstevel@tonic-gate * 344*0Sstevel@tonic-gate * If size is correct, get the ppa and attempt to 345*0Sstevel@tonic-gate * hold the device assuming ppa is instance. 346*0Sstevel@tonic-gate * 347*0Sstevel@tonic-gate * If size is wrong, we can't get the ppa, but 348*0Sstevel@tonic-gate * still increment dr_nfirst because the read side 349*0Sstevel@tonic-gate * will get a error ack on DL_ATTACH_REQ. 350*0Sstevel@tonic-gate */ 351*0Sstevel@tonic-gate dip = NULL; 352*0Sstevel@tonic-gate dsp = q->q_ptr; 353*0Sstevel@tonic-gate if (MBLKL(mp) >= DL_OK_ACK_SIZE) { 354*0Sstevel@tonic-gate dip = ddi_hold_devi_by_instance(dsp->dr_major, 355*0Sstevel@tonic-gate dlp->attach_req.dl_ppa, E_DDI_HOLD_DEVI_NOATTACH); 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate mutex_enter(&dsp->dr_lock); 359*0Sstevel@tonic-gate dsp->dr_dip[dsp->dr_nfirst] = dip; 360*0Sstevel@tonic-gate INCR(dsp->dr_nfirst); 361*0Sstevel@tonic-gate /* 362*0Sstevel@tonic-gate * Check if ring buffer is full. If so, assert in debug 363*0Sstevel@tonic-gate * kernel and produce a warning in non-debug kernel. 364*0Sstevel@tonic-gate */ 365*0Sstevel@tonic-gate ASSERT(dsp->dr_nfirst != dsp->dr_nlast); 366*0Sstevel@tonic-gate if (dsp->dr_nfirst == dsp->dr_nlast) { 367*0Sstevel@tonic-gate cmn_err(CE_WARN, "drcompat: internal buffer full"); 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate mutex_exit(&dsp->dr_lock); 370*0Sstevel@tonic-gate break; 371*0Sstevel@tonic-gate default: 372*0Sstevel@tonic-gate break; 373*0Sstevel@tonic-gate } 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate putnext(q, mp); 376*0Sstevel@tonic-gate return (0); 377*0Sstevel@tonic-gate } 378