]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/atp_open.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / netat / atp_open.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1996-1998 Apple Computer, Inc.
32 * All Rights Reserved.
33 */
34
35 /* Modified for MP, 1996 by Tuyen Nguyen
36 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
37 */
38 #define ATP_DECLARE
39
40 #include <sys/errno.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <machine/spl.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/filedesc.h>
48 #include <sys/fcntl.h>
49 #include <sys/mbuf.h>
50 #include <sys/ioctl.h>
51 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 #include <vm/vm_kern.h> /* for kernel_map */
54
55 #include <netat/sysglue.h>
56 #include <netat/appletalk.h>
57 #include <netat/ddp.h>
58 #include <netat/at_pcb.h>
59 #include <netat/atp.h>
60 #include <netat/debug.h>
61
62 /*
63 * The init routine creates all the free lists
64 * Version 1.4 of atp_open.c on 89/02/09 17:53:11
65 */
66
67 int atp_inited = 0;
68 struct atp_rcb_qhead atp_need_rel;
69
70 /**********/
71 int atp_pidM[256];
72 gref_t *atp_inputQ[256];
73 struct atp_state *atp_used_list;
74
75 int atp_input(mp)
76 gbuf_t *mp;
77 {
78 register gref_t *gref;
79
80 switch (gbuf_type(mp)) {
81 case MSG_DATA:
82 gref = atp_inputQ[((at_ddp_t *)gbuf_rptr(mp))->dst_socket];
83 if ((gref == 0) || (gref == (gref_t *)1)) {
84 dPrintf(D_M_ATP, D_L_WARNING, ("atp_input: no socket, skt=%d\n",
85 ((at_ddp_t *)gbuf_rptr(mp))->dst_socket));
86 gbuf_freem(mp);
87 return 0;
88 }
89 break;
90
91 case MSG_IOCACK:
92 case MSG_IOCNAK:
93 gref = (gref_t *)((ioc_t *)gbuf_rptr(mp))->ioc_private;
94 break;
95
96 case MSG_IOCTL:
97 default:
98 dPrintf(D_M_ATP, D_L_WARNING, ("atp_input: unknown msg, type=%d\n",
99 gbuf_type(mp)));
100 gbuf_freem(mp);
101 return 0;
102 }
103
104 atp_rput(gref, mp);
105 return 0;
106 }
107
108 /**********/
109 void atp_init()
110 {
111 int i;
112
113 if (!atp_inited) {
114 atp_inited = 1;
115 atp_used_list = 0;
116 atp_trans_abort.head = NULL;
117 atp_trans_abort.tail = NULL;
118
119 atp_need_rel.head = NULL;
120 atp_need_rel.tail = NULL;
121
122 bzero(atp_inputQ, sizeof(atp_inputQ));
123 bzero(atp_pidM, sizeof(atp_pidM));
124 asp_init();
125 }
126 }
127
128 /*
129 * The open routine allocates a state structure
130 */
131
132 /*ARGSUSED*/
133 int atp_open(gref, flag)
134 gref_t *gref;
135 int flag;
136 {
137 register struct atp_state *atp;
138 register int i;
139 vm_offset_t temp;
140
141 /*
142 * Allocate and init state and reply control block lists
143 * if this is the first open
144 */
145 if (atp_rcb_data == NULL) {
146 if (kmem_alloc(kernel_map, &temp, sizeof(struct atp_rcb) * NATP_RCB) != KERN_SUCCESS)
147 return(ENOMEM);
148 if (atp_rcb_data == NULL) {
149 bzero((caddr_t)temp, sizeof(struct atp_rcb) * NATP_RCB);
150 atp_rcb_data = (struct atp_rcb*)temp;
151 for (i = 0; i < NATP_RCB; i++) {
152 atp_rcb_data[i].rc_list.next = atp_rcb_free_list;
153 atp_rcb_free_list = &atp_rcb_data[i];
154 }
155 } else
156 kmem_free(kernel_map, temp, sizeof(struct atp_rcb) * NATP_RCB); /* already allocated by another process */
157 }
158
159 if (atp_state_data == NULL) {
160 if (kmem_alloc(kernel_map, &temp, sizeof(struct atp_state) * NATP_STATE) != KERN_SUCCESS)
161 return(ENOMEM);
162 if (atp_state_data == NULL) {
163 bzero((caddr_t)temp, sizeof(struct atp_state) * NATP_STATE);
164 atp_state_data = (struct atp_state*) temp;
165 for (i = 0; i < NATP_STATE; i++) {
166 atp_state_data[i].atp_trans_waiting = atp_free_list;
167 atp_free_list = &atp_state_data[i];
168 }
169 } else
170 kmem_free(kernel_map, temp, sizeof(struct atp_state) * NATP_STATE);
171 }
172
173
174 /*
175 * If no atp structure available return failure
176 */
177
178 if ((atp = atp_free_list) == NULL)
179 return(EAGAIN);
180
181 /*
182 * Update free list
183 */
184
185 atp_free_list = atp->atp_trans_waiting;
186
187 /*
188 * Initialize the data structure
189 */
190
191 atp->dflag = 0;
192 atp->atp_trans_wait.head = NULL;
193 atp->atp_trans_waiting = NULL;
194 atp->atp_gref = gref;
195 atp->atp_retry = 10;
196 atp->atp_timeout = HZ/8;
197 atp->atp_rcb_waiting = NULL;
198 atp->atp_rcb.head = NULL;
199 atp->atp_flags = T_MPSAFE;
200 atp->atp_socket_no = -1;
201 atp->atp_pid = gref->pid;
202 atp->atp_msgq = 0;
203 ATEVENTINIT(atp->atp_event);
204 ATEVENTINIT(atp->atp_delay_event);
205 gref->info = (void *)atp;
206
207 /*
208 * Return success
209 */
210
211 if (flag) {
212 if ((atp->atp_trans_waiting = atp_used_list) != 0)
213 atp->atp_trans_waiting->atp_rcb_waiting = atp;
214 atp_used_list = atp;
215 }
216 return(0);
217 }
218
219 /*
220 * The close routine frees all the data structures
221 */
222
223 /*ARGSUSED*/
224 int atp_close(gref, flag)
225 gref_t *gref;
226 int flag;
227 {
228 extern void atp_req_timeout();
229 register struct atp_state *atp;
230 register struct atp_trans *trp;
231 register struct atp_rcb *rcbp;
232 int socket;
233 pid_t pid;
234
235 atp = (struct atp_state *)gref->info;
236 if (atp->dflag)
237 atp = (struct atp_state *)atp->atp_msgq;
238 if (atp->atp_msgq) {
239 gbuf_freem(atp->atp_msgq);
240 atp->atp_msgq = 0;
241 }
242
243 atp->atp_flags |= ATP_CLOSING;
244 socket = atp->atp_socket_no;
245 if (socket != -1)
246 atp_inputQ[socket] = (gref_t *)1;
247
248 /*
249 * blow away all pending timers
250 */
251 for (trp = atp->atp_trans_wait.head; trp; trp = trp->tr_list.next)
252 atp_untimout(atp_req_timeout, trp);
253
254 /*
255 * Release pending transactions + rcbs
256 */
257 while ((trp = atp->atp_trans_wait.head))
258 atp_free(trp);
259 while ((rcbp = atp->atp_rcb.head))
260 atp_rcb_free(rcbp);
261 while ((rcbp = atp->atp_attached.head))
262 atp_rcb_free(rcbp);
263
264 if (flag && (socket == -1))
265 atp_dequeue_atp(atp);
266
267 /*
268 * free the state variable
269 */
270 atp->atp_socket_no = -1;
271 atp->atp_trans_waiting = atp_free_list;
272 atp_free_list = atp;
273
274 if (socket != -1) {
275 pid = (pid_t)atp_pidM[socket];
276 atp_pidM[socket] = 0;
277 atp_inputQ[socket] = NULL;
278 if (pid)
279 ddp_notify_nbp(socket, pid, DDP_ATP);
280 }
281
282 return 0;
283 }