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