xref: /onnv-gate/usr/src/uts/common/io/drcompat.c (revision 7862:f8b6a07acfd6)
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
5*7862SRichard.Bean@Sun.COM  * Common Development and Distribution License (the "License").
6*7862SRichard.Bean@Sun.COM  * 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*7862SRichard.Bean@Sun.COM  * Copyright 2008 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  * Standard module for handling DLPI Style 2 attach/detach
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/esunddi.h>
360Sstevel@tonic-gate #include <sys/strsubr.h>
370Sstevel@tonic-gate #include <sys/ddi.h>
380Sstevel@tonic-gate #include <sys/dlpi.h>
390Sstevel@tonic-gate #include <sys/strsun.h>
400Sstevel@tonic-gate #include <sys/policy.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static struct streamtab drstab;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static struct fmodsw fsw = {
450Sstevel@tonic-gate 	DRMODNAME,
460Sstevel@tonic-gate 	&drstab,
470Sstevel@tonic-gate 	D_MP
480Sstevel@tonic-gate };
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Module linkage information for the kernel.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
56*7862SRichard.Bean@Sun.COM 	&mod_strmodops, "dr compatibility for DLPI style 2 drivers", &fsw
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static struct modlinkage modlinkage = {
610Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
620Sstevel@tonic-gate };
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int
_init(void)660Sstevel@tonic-gate _init(void)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	return (mod_install(&modlinkage));
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate int
_fini(void)720Sstevel@tonic-gate _fini(void)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate int
_info(struct modinfo * modinfop)780Sstevel@tonic-gate _info(struct modinfo *modinfop)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int	dropen(queue_t *, dev_t *, int, int, cred_t *);
850Sstevel@tonic-gate static int	drclose(queue_t *, int, cred_t *);
860Sstevel@tonic-gate static int	drrput(queue_t *, mblk_t *);
870Sstevel@tonic-gate static int	drwput(queue_t *, mblk_t *);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct module_info drinfo = {
900Sstevel@tonic-gate 	0,
910Sstevel@tonic-gate 	DRMODNAME,
920Sstevel@tonic-gate 	0,
930Sstevel@tonic-gate 	INFPSZ,
940Sstevel@tonic-gate 	1,
950Sstevel@tonic-gate 	0
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static struct qinit drrinit = {
990Sstevel@tonic-gate 	(int (*)())drrput,
1000Sstevel@tonic-gate 	NULL,
1010Sstevel@tonic-gate 	dropen,
1020Sstevel@tonic-gate 	drclose,
1030Sstevel@tonic-gate 	NULL,
1040Sstevel@tonic-gate 	&drinfo
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static struct qinit drwinit = {
1080Sstevel@tonic-gate 	(int (*)())drwput,
1090Sstevel@tonic-gate 	NULL,
1100Sstevel@tonic-gate 	NULL,
1110Sstevel@tonic-gate 	NULL,
1120Sstevel@tonic-gate 	NULL,
1130Sstevel@tonic-gate 	&drinfo
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static struct streamtab drstab = {
1170Sstevel@tonic-gate 	&drrinit,
1180Sstevel@tonic-gate 	&drwinit,
1190Sstevel@tonic-gate 	NULL,
1200Sstevel@tonic-gate 	NULL
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * This module is pushed directly on top of the bottom driver
1250Sstevel@tonic-gate  * in a DLPI style-2 stream by stropen(). It intercepts
1260Sstevel@tonic-gate  * DL_ATTACH_REQ/DL_DETACH_REQ messages on the write side
1270Sstevel@tonic-gate  * and acks on the read side, calls qassociate where needed.
1280Sstevel@tonic-gate  * The primary purpose is to workaround a DR race condition
1290Sstevel@tonic-gate  * affecting non-DDI compliant DLPI style 2 drivers, which may
1300Sstevel@tonic-gate  * cause the system to panic.
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  * The following action is taken:
1330Sstevel@tonic-gate  * Write side (drwput):
1340Sstevel@tonic-gate  *	attach request:	hold driver instance assuming ppa == instance.
1350Sstevel@tonic-gate  *		This way, the instance cannot be detached while the
1360Sstevel@tonic-gate  *		driver is processing DL_ATTACH_REQ.
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  *		On a successful hold, store the dip in a ring buffer
1390Sstevel@tonic-gate  *		to be processed lated by the read side.
1400Sstevel@tonic-gate  *		If hold fails (most likely ppa != instance), we store
1410Sstevel@tonic-gate  *		NULL in the ring buffer and read side won't take
1420Sstevel@tonic-gate  *		any action on ack.
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * Read side (drrput):
1450Sstevel@tonic-gate  *	attach success: if (dip held on write side) associate queue with dip
1460Sstevel@tonic-gate  *	attach failure:	if (dip held on write side) release hold on dip
1470Sstevel@tonic-gate  *	detach success: associate queue with NULL
1480Sstevel@tonic-gate  *	detach failure:	do nothing
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  * The module assumes that incoming DL_ATTACH_REQ/DL_DETACH_REQ
1510Sstevel@tonic-gate  * messages are ordered (non-concurrent) and the bottom
1520Sstevel@tonic-gate  * driver processes them and sends acknowledgements in the same
1530Sstevel@tonic-gate  * order. This assumption is reasonable because concurrent
1540Sstevel@tonic-gate  * association results in non-deterministic queue behavior.
1550Sstevel@tonic-gate  * The module is coded carefully such that unordered messages
1560Sstevel@tonic-gate  * do not result in a system panic.
1570Sstevel@tonic-gate  *
1580Sstevel@tonic-gate  * The module handles multiple outstanding messages queued
1590Sstevel@tonic-gate  * in the bottom driver. Messages processed on the write side
1600Sstevel@tonic-gate  * but not yet arrived at read side are placed in the ring buffer
1610Sstevel@tonic-gate  * dr_dip[], between dr_nfirst and dr_nlast. The write side is
1620Sstevel@tonic-gate  * producer and the read side is the consumer. The buffer is full
1630Sstevel@tonic-gate  * when dr_nfirst == dr_nlast.
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  * The current size of the ring buffer is 64 (MAX_DLREQS) per stream.
1660Sstevel@tonic-gate  * During normal testing, we have not seen outstanding messages
1670Sstevel@tonic-gate  * above 10.
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #define	MAX_DLREQS	64
1710Sstevel@tonic-gate #define	INCR(x)		{(x)++; if ((x) >= MAX_DLREQS) (x) = 0; }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate struct drstate {
1740Sstevel@tonic-gate 	kmutex_t dr_lock;
1750Sstevel@tonic-gate 	major_t dr_major;
1760Sstevel@tonic-gate 	int dr_nfirst;
1770Sstevel@tonic-gate 	int dr_nlast;
1780Sstevel@tonic-gate 	dev_info_t *dr_dip[MAX_DLREQS];
1790Sstevel@tonic-gate };
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /* ARGSUSED1 */
1820Sstevel@tonic-gate static int
dropen(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * crp)1830Sstevel@tonic-gate dropen(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *crp)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	struct drstate *dsp;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (sflag != MODOPEN) {	/* must be a pushed module */
1880Sstevel@tonic-gate 		return (EINVAL);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (secpolicy_net_rawaccess(crp) != 0) {
1920Sstevel@tonic-gate 		return (EPERM);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (q->q_ptr != NULL) {
1960Sstevel@tonic-gate 		return (0);	/* already open */
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	dsp = kmem_zalloc(sizeof (*dsp), KM_SLEEP);
2000Sstevel@tonic-gate 	dsp->dr_major = getmajor(*devp);
2010Sstevel@tonic-gate 	mutex_init(&dsp->dr_lock, NULL, MUTEX_DEFAULT, NULL);
2020Sstevel@tonic-gate 	q->q_ptr = OTHERQ(q)->q_ptr = dsp;
2030Sstevel@tonic-gate 	qprocson(q);
2040Sstevel@tonic-gate 	ddi_assoc_queue_with_devi(q, NULL);
2050Sstevel@tonic-gate 	return (0);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /* ARGSUSED1 */
2090Sstevel@tonic-gate static int
drclose(queue_t * q,int cflag,cred_t * crp)2100Sstevel@tonic-gate drclose(queue_t *q, int cflag, cred_t *crp)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	struct drstate *dsp = q->q_ptr;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	ASSERT(dsp);
2150Sstevel@tonic-gate 	ddi_assoc_queue_with_devi(q, NULL);
2160Sstevel@tonic-gate 	qprocsoff(q);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	mutex_destroy(&dsp->dr_lock);
2190Sstevel@tonic-gate 	kmem_free(dsp, sizeof (*dsp));
2200Sstevel@tonic-gate 	q->q_ptr = NULL;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	return (0);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate static int
drrput(queue_t * q,mblk_t * mp)2260Sstevel@tonic-gate drrput(queue_t *q, mblk_t *mp)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	struct drstate *dsp;
2290Sstevel@tonic-gate 	union DL_primitives *dlp;
2300Sstevel@tonic-gate 	dev_info_t *dip;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
2330Sstevel@tonic-gate 	case M_PROTO:
2340Sstevel@tonic-gate 	case M_PCPROTO:
2350Sstevel@tonic-gate 		break;
2360Sstevel@tonic-gate 	default:
2370Sstevel@tonic-gate 		putnext(q, mp);
2380Sstevel@tonic-gate 		return (0);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* make sure size is sufficient for dl_primitive */
2420Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (t_uscalar_t)) {
2430Sstevel@tonic-gate 		putnext(q, mp);
2440Sstevel@tonic-gate 		return (0);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
2480Sstevel@tonic-gate 	switch (dlp->dl_primitive) {
2490Sstevel@tonic-gate 	case DL_OK_ACK: {
2500Sstevel@tonic-gate 		/* check for proper size, let upper layer deal with error */
2510Sstevel@tonic-gate 		if (MBLKL(mp) < DL_OK_ACK_SIZE) {
2520Sstevel@tonic-gate 			putnext(q, mp);
2530Sstevel@tonic-gate 			return (0);
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		dsp = q->q_ptr;
2570Sstevel@tonic-gate 		switch (dlp->ok_ack.dl_correct_primitive) {
2580Sstevel@tonic-gate 		case DL_ATTACH_REQ:
2590Sstevel@tonic-gate 			/*
2600Sstevel@tonic-gate 			 * ddi_assoc_queue_with_devi() will hold dip,
2610Sstevel@tonic-gate 			 * so release after association.
2620Sstevel@tonic-gate 			 *
2630Sstevel@tonic-gate 			 * dip is NULL means we didn't hold dip on read side.
2640Sstevel@tonic-gate 			 * (unlikely, but possible), so we do nothing.
2650Sstevel@tonic-gate 			 */
2660Sstevel@tonic-gate 			mutex_enter(&dsp->dr_lock);
2670Sstevel@tonic-gate 			dip = dsp->dr_dip[dsp->dr_nlast];
2680Sstevel@tonic-gate 			dsp->dr_dip[dsp->dr_nlast] = NULL;
2690Sstevel@tonic-gate 			INCR(dsp->dr_nlast);
2700Sstevel@tonic-gate 			mutex_exit(&dsp->dr_lock);
2710Sstevel@tonic-gate 			if (dip) {
2720Sstevel@tonic-gate 				ddi_assoc_queue_with_devi(q, dip);
2730Sstevel@tonic-gate 				ddi_release_devi(dip);
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		case DL_DETACH_REQ:
2780Sstevel@tonic-gate 			ddi_assoc_queue_with_devi(q, NULL);
2790Sstevel@tonic-gate 			break;
2800Sstevel@tonic-gate 		default:
2810Sstevel@tonic-gate 			break;
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 		break;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	case DL_ERROR_ACK:
2860Sstevel@tonic-gate 		if (dlp->error_ack.dl_error_primitive != DL_ATTACH_REQ)
2870Sstevel@tonic-gate 			break;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		dsp = q->q_ptr;
2900Sstevel@tonic-gate 		mutex_enter(&dsp->dr_lock);
2910Sstevel@tonic-gate 		dip = dsp->dr_dip[dsp->dr_nlast];
2920Sstevel@tonic-gate 		dsp->dr_dip[dsp->dr_nlast] = NULL;
2930Sstevel@tonic-gate 		INCR(dsp->dr_nlast);
2940Sstevel@tonic-gate 		mutex_exit(&dsp->dr_lock);
2950Sstevel@tonic-gate 		/*
2960Sstevel@tonic-gate 		 * Release dip on attach failure
2970Sstevel@tonic-gate 		 */
2980Sstevel@tonic-gate 		if (dip) {
2990Sstevel@tonic-gate 			ddi_release_devi(dip);
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 		break;
3020Sstevel@tonic-gate 	default:
3030Sstevel@tonic-gate 		break;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	putnext(q, mp);
3070Sstevel@tonic-gate 	return (0);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Detect dl attach, hold the dip to prevent it from detaching
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate static int
drwput(queue_t * q,mblk_t * mp)3140Sstevel@tonic-gate drwput(queue_t *q, mblk_t *mp)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	struct drstate *dsp;
3170Sstevel@tonic-gate 	union DL_primitives *dlp;
3180Sstevel@tonic-gate 	dev_info_t *dip;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
3210Sstevel@tonic-gate 	case M_PROTO:
3220Sstevel@tonic-gate 	case M_PCPROTO:
3230Sstevel@tonic-gate 		break;
3240Sstevel@tonic-gate 	default:
3250Sstevel@tonic-gate 		putnext(q, mp);
3260Sstevel@tonic-gate 		return (0);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/* make sure size is sufficient for dl_primitive */
3300Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (t_uscalar_t)) {
3310Sstevel@tonic-gate 		putnext(q, mp);
3320Sstevel@tonic-gate 		return (0);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
3360Sstevel@tonic-gate 	switch (dlp->dl_primitive) {
3370Sstevel@tonic-gate 	case DL_ATTACH_REQ:
3380Sstevel@tonic-gate 		/*
3390Sstevel@tonic-gate 		 * Check for proper size of the message.
3400Sstevel@tonic-gate 		 *
3410Sstevel@tonic-gate 		 * If size is correct, get the ppa and attempt to
3420Sstevel@tonic-gate 		 * hold the device assuming ppa is instance.
3430Sstevel@tonic-gate 		 *
3440Sstevel@tonic-gate 		 * If size is wrong, we can't get the ppa, but
3450Sstevel@tonic-gate 		 * still increment dr_nfirst because the read side
3460Sstevel@tonic-gate 		 * will get a error ack on DL_ATTACH_REQ.
3470Sstevel@tonic-gate 		 */
3480Sstevel@tonic-gate 		dip = NULL;
3490Sstevel@tonic-gate 		dsp = q->q_ptr;
3500Sstevel@tonic-gate 		if (MBLKL(mp) >= DL_OK_ACK_SIZE) {
3510Sstevel@tonic-gate 			dip = ddi_hold_devi_by_instance(dsp->dr_major,
3520Sstevel@tonic-gate 			    dlp->attach_req.dl_ppa, E_DDI_HOLD_DEVI_NOATTACH);
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		mutex_enter(&dsp->dr_lock);
3560Sstevel@tonic-gate 		dsp->dr_dip[dsp->dr_nfirst] = dip;
3570Sstevel@tonic-gate 		INCR(dsp->dr_nfirst);
3580Sstevel@tonic-gate 		/*
3590Sstevel@tonic-gate 		 * Check if ring buffer is full. If so, assert in debug
3600Sstevel@tonic-gate 		 * kernel and produce a warning in non-debug kernel.
3610Sstevel@tonic-gate 		 */
3620Sstevel@tonic-gate 		ASSERT(dsp->dr_nfirst != dsp->dr_nlast);
3630Sstevel@tonic-gate 		if (dsp->dr_nfirst == dsp->dr_nlast) {
3640Sstevel@tonic-gate 			cmn_err(CE_WARN, "drcompat: internal buffer full");
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 		mutex_exit(&dsp->dr_lock);
3670Sstevel@tonic-gate 		break;
3680Sstevel@tonic-gate 	default:
3690Sstevel@tonic-gate 		break;
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	putnext(q, mp);
3730Sstevel@tonic-gate 	return (0);
3740Sstevel@tonic-gate }
375