260 some drivers erroneously discount a major of 0
[illumos-gate.git] / usr / src / uts / common / ktli / t_kopen.c
blobaec085ba06abaf625096b4635e3aed063b5e2496
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
40 * Kernel TLI-like function to initialize a transport
41 * endpoint using the protocol specified.
43 * Returns:
44 * 0 on success and "tiptr" is set to a valid transport pointer,
45 * else a positive error code.
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/proc.h>
51 #include <sys/file.h>
52 #include <sys/user.h>
53 #include <sys/vnode.h>
54 #include <sys/errno.h>
55 #include <sys/stream.h>
56 #include <sys/ioctl.h>
57 #include <sys/stropts.h>
58 #include <sys/strsubr.h>
59 #include <sys/tihdr.h>
60 #include <sys/timod.h>
61 #include <sys/tiuser.h>
62 #include <sys/t_kuser.h>
63 #include <sys/kmem.h>
64 #include <sys/cmn_err.h>
66 static t_scalar_t _t_setsize(t_scalar_t);
68 int
69 t_kopen(file_t *fp, dev_t rdev, int flags, TIUSER **tiptr, cred_t *cr)
71 int madefp = 0;
72 struct T_info_ack inforeq;
73 int retval;
74 vnode_t *vp;
75 struct strioctl strioc;
76 int error;
77 TIUSER *ntiptr;
78 int rtries = 0;
81 * Special case for install: miniroot needs to be able to access files
82 * via NFS as though it were always in the global zone.
84 if (nfs_global_client_only != 0)
85 cr = kcred;
87 KTLILOG(2, "t_kopen: fp %x, ", fp);
88 KTLILOG(2, "rdev %x, ", rdev);
89 KTLILOG(2, "flags %x\n", flags);
91 *tiptr = NULL;
92 error = 0;
93 retval = 0;
94 if (fp == NULL) {
95 if (rdev == NODEV) {
96 KTLILOG(1, "t_kopen: null device\n", 0);
97 return (EINVAL);
101 * allocate a file pointer, but
102 * no file descripter.
104 if ((error = falloc(NULL, flags, &fp, NULL)) != 0) {
105 KTLILOG(1, "t_kopen: falloc: %d\n", error);
106 return (error);
109 /* Install proper cred in file */
110 if (cr != fp->f_cred) {
111 crhold(cr);
112 crfree(fp->f_cred);
113 fp->f_cred = cr;
116 vp = makespecvp(rdev, VCHR);
119 * this will call the streams open for us.
120 * Want to retry if error is EAGAIN, the streams open routine
121 * might fail due to temporarely out of memory.
123 do {
124 if ((error = VOP_OPEN(&vp, flags, cr, NULL))
125 == EAGAIN) {
126 (void) delay(hz);
128 } while (error == EAGAIN && ++rtries < 5);
130 if (error) {
131 KTLILOG(1, "t_kopen: VOP_OPEN: %d\n", error);
132 unfalloc(fp);
133 VN_RELE(vp);
134 return (error);
137 * fp is completely initialized so drop the write lock.
138 * I actually don't need any locking on fp in here since
139 * there is no fd pointing at it. However, since I could
140 * call closef if there is an error and closef requires
141 * the fp read locked, I will acquire the read lock here
142 * and make sure I release it before I leave this routine.
144 fp->f_vnode = vp;
145 mutex_exit(&fp->f_tlock);
147 madefp = 1;
148 } else {
149 vp = fp->f_vnode;
152 if (vp->v_stream == NULL) {
153 if (madefp)
154 (void) closef(fp);
155 KTLILOG(1, "t_kopen: not a streams device\n", 0);
156 return (ENOSTR);
160 * allocate a new transport structure
162 ntiptr = kmem_alloc(TIUSERSZ, KM_SLEEP);
163 ntiptr->fp = fp;
164 ntiptr->flags = madefp ? MADE_FP : 0;
166 KTLILOG(2, "t_kopen: vp %x, ", vp);
167 KTLILOG(2, "stp %x\n", vp->v_stream);
170 * see if TIMOD is already pushed
172 error = strioctl(vp, I_FIND, (intptr_t)"timod", 0, K_TO_K, cr, &retval);
173 if (error) {
174 kmem_free(ntiptr, TIUSERSZ);
175 if (madefp)
176 (void) closef(fp);
177 KTLILOG(1, "t_kopen: strioctl(I_FIND, timod): %d\n", error);
178 return (error);
181 if (retval == 0) {
182 tryagain:
183 error = strioctl(vp, I_PUSH, (intptr_t)"timod", 0, K_TO_K, cr,
184 &retval);
185 if (error) {
186 switch (error) {
187 case ENOSPC:
188 case EAGAIN:
189 case ENOSR:
191 * This probably means the master file
192 * should be tuned.
194 cmn_err(CE_WARN,
195 "t_kopen: I_PUSH of timod failed, error %d\n",
196 error);
197 (void) delay(hz);
198 error = 0;
199 goto tryagain;
201 default:
202 kmem_free(ntiptr, TIUSERSZ);
203 if (madefp)
204 (void) closef(fp);
205 KTLILOG(1, "t_kopen: I_PUSH (timod): %d",
206 error);
207 return (error);
212 inforeq.PRIM_type = T_INFO_REQ;
213 strioc.ic_cmd = TI_GETINFO;
214 strioc.ic_timout = 0;
215 strioc.ic_dp = (char *)&inforeq;
216 strioc.ic_len = (int)sizeof (struct T_info_req);
218 error = strdoioctl(vp->v_stream, &strioc, FNATIVE, K_TO_K, cr, &retval);
219 if (error) {
220 kmem_free(ntiptr, TIUSERSZ);
221 if (madefp)
222 (void) closef(fp);
223 KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): %d\n", error);
224 return (error);
227 if (retval) {
228 if ((retval & 0xff) == TSYSERR)
229 error = (retval >> 8) & 0xff;
230 else
231 error = t_tlitosyserr(retval & 0xff);
232 kmem_free(ntiptr, TIUSERSZ);
233 if (madefp)
234 (void) closef(fp);
235 KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): retval: 0x%x\n",
236 retval);
237 return (error);
240 if (strioc.ic_len != sizeof (struct T_info_ack)) {
241 kmem_free(ntiptr, TIUSERSZ);
242 if (madefp)
243 (void) closef(fp);
244 KTLILOG(1,
245 "t_kopen: strioc.ic_len != sizeof (struct T_info_ack): %d\n",
246 strioc.ic_len);
247 return (EPROTO);
250 ntiptr->tp_info.addr = _t_setsize(inforeq.ADDR_size);
251 ntiptr->tp_info.options = _t_setsize(inforeq.OPT_size);
252 ntiptr->tp_info.tsdu = _t_setsize(inforeq.TSDU_size);
253 ntiptr->tp_info.etsdu = _t_setsize(inforeq.ETSDU_size);
254 ntiptr->tp_info.connect = _t_setsize(inforeq.CDATA_size);
255 ntiptr->tp_info.discon = _t_setsize(inforeq.DDATA_size);
256 ntiptr->tp_info.servtype = inforeq.SERV_type;
258 *tiptr = ntiptr;
260 return (0);
263 #define DEFSIZE 128
265 static t_scalar_t
266 _t_setsize(t_scalar_t infosize)
268 switch (infosize) {
269 case -1:
270 return (DEFSIZE);
271 case -2:
272 return (0);
273 default:
274 return (infosize);