xref: /onnv-gate/usr/src/uts/common/ktli/t_kopen.c (revision 0:68f95e015346)
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 2005 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * Kernel TLI-like function to initialize a transport
44*0Sstevel@tonic-gate  * endpoint using the protocol specified.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * Returns:
47*0Sstevel@tonic-gate  * 	0 on success and "tiptr" is set to a valid transport pointer,
48*0Sstevel@tonic-gate  * 	else a positive error code.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <sys/param.h>
52*0Sstevel@tonic-gate #include <sys/types.h>
53*0Sstevel@tonic-gate #include <sys/proc.h>
54*0Sstevel@tonic-gate #include <sys/file.h>
55*0Sstevel@tonic-gate #include <sys/user.h>
56*0Sstevel@tonic-gate #include <sys/vnode.h>
57*0Sstevel@tonic-gate #include <sys/errno.h>
58*0Sstevel@tonic-gate #include <sys/stream.h>
59*0Sstevel@tonic-gate #include <sys/ioctl.h>
60*0Sstevel@tonic-gate #include <sys/stropts.h>
61*0Sstevel@tonic-gate #include <sys/strsubr.h>
62*0Sstevel@tonic-gate #include <sys/tihdr.h>
63*0Sstevel@tonic-gate #include <sys/timod.h>
64*0Sstevel@tonic-gate #include <sys/tiuser.h>
65*0Sstevel@tonic-gate #include <sys/t_kuser.h>
66*0Sstevel@tonic-gate #include <sys/kmem.h>
67*0Sstevel@tonic-gate #include <sys/cmn_err.h>
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static t_scalar_t _t_setsize(t_scalar_t);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate int
72*0Sstevel@tonic-gate t_kopen(file_t *fp, dev_t rdev, int flags, TIUSER **tiptr, cred_t *cr)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	int			madefp = 0;
75*0Sstevel@tonic-gate 	struct T_info_ack	inforeq;
76*0Sstevel@tonic-gate 	int			retval;
77*0Sstevel@tonic-gate 	vnode_t			*vp;
78*0Sstevel@tonic-gate 	struct strioctl		strioc;
79*0Sstevel@tonic-gate 	int			error;
80*0Sstevel@tonic-gate 	TIUSER			*ntiptr;
81*0Sstevel@tonic-gate 	int			rtries = 0;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	KTLILOG(2, "t_kopen: fp %x, ", fp);
84*0Sstevel@tonic-gate 	KTLILOG(2, "rdev %x, ", rdev);
85*0Sstevel@tonic-gate 	KTLILOG(2, "flags %x\n", flags);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	*tiptr = NULL;
88*0Sstevel@tonic-gate 	error = 0;
89*0Sstevel@tonic-gate 	retval = 0;
90*0Sstevel@tonic-gate 	if (fp == NULL) {
91*0Sstevel@tonic-gate 		if (rdev == 0 || rdev == NODEV) {
92*0Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: null device\n", 0);
93*0Sstevel@tonic-gate 			return (EINVAL);
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		/*
97*0Sstevel@tonic-gate 		 * allocate a file pointer, but
98*0Sstevel@tonic-gate 		 * no file descripter.
99*0Sstevel@tonic-gate 		 */
100*0Sstevel@tonic-gate 		if ((error = falloc(NULL, flags, &fp, NULL)) != 0) {
101*0Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: falloc: %d\n", error);
102*0Sstevel@tonic-gate 			return (error);
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 		/* Install proper cred in file */
106*0Sstevel@tonic-gate 		if (cr != fp->f_cred) {
107*0Sstevel@tonic-gate 			crhold(cr);
108*0Sstevel@tonic-gate 			crfree(fp->f_cred);
109*0Sstevel@tonic-gate 			fp->f_cred = cr;
110*0Sstevel@tonic-gate 		}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		vp = makespecvp(rdev, VCHR);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		/*
115*0Sstevel@tonic-gate 		 * this will call the streams open for us.
116*0Sstevel@tonic-gate 		 * Want to retry if error is EAGAIN, the streams open routine
117*0Sstevel@tonic-gate 		 * might fail due to temporarely out of memory.
118*0Sstevel@tonic-gate 		 */
119*0Sstevel@tonic-gate 		do {
120*0Sstevel@tonic-gate 			if ((error = VOP_OPEN(&vp, flags, cr)) == EAGAIN) {
121*0Sstevel@tonic-gate 				(void) delay(hz);
122*0Sstevel@tonic-gate 			}
123*0Sstevel@tonic-gate 		} while (error == EAGAIN && ++rtries < 5);
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 		if (error) {
126*0Sstevel@tonic-gate 			KTLILOG(1, "t_kopen: VOP_OPEN: %d\n", error);
127*0Sstevel@tonic-gate 			unfalloc(fp);
128*0Sstevel@tonic-gate 			VN_RELE(vp);
129*0Sstevel@tonic-gate 			return (error);
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 		/*
132*0Sstevel@tonic-gate 		 * fp is completely initialized so drop the write lock.
133*0Sstevel@tonic-gate 		 * I actually don't need any locking on fp in here since
134*0Sstevel@tonic-gate 		 * there is no fd pointing at it.  However, since I could
135*0Sstevel@tonic-gate 		 * call closef if there is an error and closef requires
136*0Sstevel@tonic-gate 		 * the fp read locked, I will acquire the read lock here
137*0Sstevel@tonic-gate 		 * and make sure I release it before I leave this routine.
138*0Sstevel@tonic-gate 		 */
139*0Sstevel@tonic-gate 		fp->f_vnode = vp;
140*0Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		madefp = 1;
143*0Sstevel@tonic-gate 	} else {
144*0Sstevel@tonic-gate 		vp = fp->f_vnode;
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
148*0Sstevel@tonic-gate 		if (madefp)
149*0Sstevel@tonic-gate 			(void) closef(fp);
150*0Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: not a streams device\n", 0);
151*0Sstevel@tonic-gate 		return (ENOSTR);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * allocate a new transport structure
156*0Sstevel@tonic-gate 	 */
157*0Sstevel@tonic-gate 	ntiptr = kmem_alloc(TIUSERSZ, KM_SLEEP);
158*0Sstevel@tonic-gate 	ntiptr->fp = fp;
159*0Sstevel@tonic-gate 	ntiptr->flags = madefp ? MADE_FP : 0;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	KTLILOG(2, "t_kopen: vp %x, ", vp);
162*0Sstevel@tonic-gate 	KTLILOG(2, "stp %x\n", vp->v_stream);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	/*
165*0Sstevel@tonic-gate 	 * see if TIMOD is already pushed
166*0Sstevel@tonic-gate 	 */
167*0Sstevel@tonic-gate 	error = strioctl(vp, I_FIND, (intptr_t)"timod", 0, K_TO_K, cr, &retval);
168*0Sstevel@tonic-gate 	if (error) {
169*0Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
170*0Sstevel@tonic-gate 		if (madefp)
171*0Sstevel@tonic-gate 			(void) closef(fp);
172*0Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strioctl(I_FIND, timod): %d\n", error);
173*0Sstevel@tonic-gate 		return (error);
174*0Sstevel@tonic-gate 	}
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (retval == 0) {
177*0Sstevel@tonic-gate tryagain:
178*0Sstevel@tonic-gate 		error = strioctl(vp, I_PUSH, (intptr_t)"timod", 0, K_TO_K, cr,
179*0Sstevel@tonic-gate 		    &retval);
180*0Sstevel@tonic-gate 		if (error) {
181*0Sstevel@tonic-gate 			switch (error) {
182*0Sstevel@tonic-gate 			case ENOSPC:
183*0Sstevel@tonic-gate 			case EAGAIN:
184*0Sstevel@tonic-gate 			case ENOSR:
185*0Sstevel@tonic-gate 				/*
186*0Sstevel@tonic-gate 				 * This probably means the master file
187*0Sstevel@tonic-gate 				 * should be tuned.
188*0Sstevel@tonic-gate 				 */
189*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
190*0Sstevel@tonic-gate 				"t_kopen: I_PUSH of timod failed, error %d\n",
191*0Sstevel@tonic-gate 				    error);
192*0Sstevel@tonic-gate 				(void) delay(hz);
193*0Sstevel@tonic-gate 				error = 0;
194*0Sstevel@tonic-gate 				goto tryagain;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 			default:
197*0Sstevel@tonic-gate 				kmem_free(ntiptr, TIUSERSZ);
198*0Sstevel@tonic-gate 				if (madefp)
199*0Sstevel@tonic-gate 					(void) closef(fp);
200*0Sstevel@tonic-gate 				KTLILOG(1, "t_kopen: I_PUSH (timod): %d",
201*0Sstevel@tonic-gate 				    error);
202*0Sstevel@tonic-gate 				return (error);
203*0Sstevel@tonic-gate 			}
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	inforeq.PRIM_type = T_INFO_REQ;
208*0Sstevel@tonic-gate 	strioc.ic_cmd = TI_GETINFO;
209*0Sstevel@tonic-gate 	strioc.ic_timout = 0;
210*0Sstevel@tonic-gate 	strioc.ic_dp = (char *)&inforeq;
211*0Sstevel@tonic-gate 	strioc.ic_len = (int)sizeof (struct T_info_req);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	error = strdoioctl(vp->v_stream, &strioc, FNATIVE, K_TO_K, cr, &retval);
214*0Sstevel@tonic-gate 	if (error) {
215*0Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
216*0Sstevel@tonic-gate 		if (madefp)
217*0Sstevel@tonic-gate 			(void) closef(fp);
218*0Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): %d\n", error);
219*0Sstevel@tonic-gate 		return (error);
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	if (retval) {
223*0Sstevel@tonic-gate 		if ((retval & 0xff) == TSYSERR)
224*0Sstevel@tonic-gate 			error = (retval >> 8) & 0xff;
225*0Sstevel@tonic-gate 		else
226*0Sstevel@tonic-gate 			error = t_tlitosyserr(retval & 0xff);
227*0Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
228*0Sstevel@tonic-gate 		if (madefp)
229*0Sstevel@tonic-gate 			(void) closef(fp);
230*0Sstevel@tonic-gate 		KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): retval: 0x%x\n",
231*0Sstevel@tonic-gate 		    retval);
232*0Sstevel@tonic-gate 		return (error);
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	if (strioc.ic_len != sizeof (struct T_info_ack)) {
236*0Sstevel@tonic-gate 		kmem_free(ntiptr, TIUSERSZ);
237*0Sstevel@tonic-gate 		if (madefp)
238*0Sstevel@tonic-gate 			(void) closef(fp);
239*0Sstevel@tonic-gate 		KTLILOG(1,
240*0Sstevel@tonic-gate 		"t_kopen: strioc.ic_len != sizeof (struct T_info_ack): %d\n",
241*0Sstevel@tonic-gate 		    strioc.ic_len);
242*0Sstevel@tonic-gate 		return (EPROTO);
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	ntiptr->tp_info.addr = _t_setsize(inforeq.ADDR_size);
246*0Sstevel@tonic-gate 	ntiptr->tp_info.options = _t_setsize(inforeq.OPT_size);
247*0Sstevel@tonic-gate 	ntiptr->tp_info.tsdu = _t_setsize(inforeq.TSDU_size);
248*0Sstevel@tonic-gate 	ntiptr->tp_info.etsdu = _t_setsize(inforeq.ETSDU_size);
249*0Sstevel@tonic-gate 	ntiptr->tp_info.connect = _t_setsize(inforeq.CDATA_size);
250*0Sstevel@tonic-gate 	ntiptr->tp_info.discon = _t_setsize(inforeq.DDATA_size);
251*0Sstevel@tonic-gate 	ntiptr->tp_info.servtype = inforeq.SERV_type;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	*tiptr = ntiptr;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	return (0);
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate #define	DEFSIZE	128
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate static t_scalar_t
261*0Sstevel@tonic-gate _t_setsize(t_scalar_t infosize)
262*0Sstevel@tonic-gate {
263*0Sstevel@tonic-gate 	switch (infosize) {
264*0Sstevel@tonic-gate 	case -1:
265*0Sstevel@tonic-gate 		return (DEFSIZE);
266*0Sstevel@tonic-gate 	case -2:
267*0Sstevel@tonic-gate 		return (0);
268*0Sstevel@tonic-gate 	default:
269*0Sstevel@tonic-gate 		return (infosize);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate }
272