]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/atp_misc.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / netat / atp_misc.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1996-1998 Apple Computer, Inc.
27 * All Rights Reserved.
28 */
29
30 /* Modified for MP, 1996 by Tuyen Nguyen
31 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
32 */
33 #include <sys/errno.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <machine/spl.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/filedesc.h>
41 #include <sys/fcntl.h>
42 #include <sys/mbuf.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46
47 #include <netat/sysglue.h>
48 #include <netat/appletalk.h>
49 #include <netat/ddp.h>
50 #include <netat/at_pcb.h>
51 #include <netat/atp.h>
52 #include <netat/debug.h>
53
54 extern atlock_t atpgen_lock;
55 void atp_free();
56 void atp_send(struct atp_trans *);
57
58 /*
59 * The request timer retries a request, if all retries are used up
60 * it returns a NAK
61 */
62
63 void
64 atp_req_timeout(trp)
65 register struct atp_trans *trp;
66 {
67 int s;
68 register gbuf_t *m;
69 gref_t *gref;
70 struct atp_state *atp;
71 struct atp_trans *ctrp;
72
73 if ((atp = trp->tr_queue) == 0)
74 return;
75 ATDISABLE(s, atp->atp_lock);
76 if (atp->atp_flags & ATP_CLOSING) {
77 ATENABLE(s, atp->atp_lock);
78 return;
79 }
80 for (ctrp = atp->atp_trans_wait.head; ctrp; ctrp = ctrp->tr_list.next) {
81 if (ctrp == trp)
82 break;
83 }
84 if (ctrp != trp) {
85 ATENABLE(s, atp->atp_lock);
86 return;
87 }
88
89 if ((m = gbuf_cont(trp->tr_xmt)) == NULL)
90 m = trp->tr_xmt; /* issued via the new interface */
91
92 if (trp->tr_retry == 0) {
93 trp->tr_state = TRANS_FAILED;
94 if (m == trp->tr_xmt) {
95 trp->tr_xmt = NULL;
96 l_notify:
97 gbuf_wset(m,1);
98 *gbuf_rptr(m) = 99;
99 gbuf_set_type(m, MSG_DATA);
100 gref = trp->tr_queue->atp_gref;
101 ATENABLE(s, atp->atp_lock);
102 atalk_putnext(gref, m);
103
104 return;
105 }
106 dPrintf(D_M_ATP_LOW,D_L_INFO, ("atp_req_timeout: skt=%d\n",
107 trp->tr_local_socket));
108 m = trp->tr_xmt;
109 switch(((ioc_t *)(gbuf_rptr(trp->tr_xmt)))->ioc_cmd) {
110 case AT_ATP_ISSUE_REQUEST:
111 trp->tr_xmt = NULL;
112 if (trp->tr_queue->dflag)
113 ((ioc_t *)gbuf_rptr(m))->ioc_cmd = AT_ATP_REQUEST_COMPLETE;
114 else if (trp->tr_bdsp == NULL) {
115 ATENABLE(s, atp->atp_lock);
116 gbuf_freem(m);
117 if (trp->tr_rsp_wait)
118 wakeup(&trp->tr_event);
119 break;
120 }
121 ATENABLE(s, atp->atp_lock);
122 atp_iocnak(trp->tr_queue, m, ETIMEDOUT);
123 atp_free(trp);
124 return;
125
126 case AT_ATP_ISSUE_REQUEST_NOTE:
127 case AT_ATP_ISSUE_REQUEST_TICKLE:
128 trp->tr_xmt = gbuf_cont(m);
129 gbuf_cont(m) = NULL;
130 goto l_notify;
131 }
132 } else {
133 (AT_ATP_HDR(m))->bitmap = trp->tr_bitmap;
134
135 if (trp->tr_retry != (unsigned int) ATP_INFINITE_RETRIES)
136 trp->tr_retry--;
137 ATENABLE(s, atp->atp_lock);
138 atp_send(trp);
139 }
140 }
141
142
143 /*
144 * atp_free frees up a request, cleaning up the queues and freeing
145 * the request packet
146 * always called at 'lock'
147 */
148
149 void atp_free(trp)
150 register struct atp_trans *trp;
151 {
152 register struct atp_state *atp;
153 register int i;
154 int s;
155
156 dPrintf(D_M_ATP_LOW, D_L_TRACE,
157 ("atp_free: freeing trp 0x%x\n", (u_int) trp));
158
159 ATDISABLE(s, atpgen_lock);
160
161 if (trp->tr_state == TRANS_ABORTING) {
162 ATP_Q_REMOVE(atp_trans_abort, trp, tr_list);
163 trp->tr_state = TRANS_DONE;
164 }
165 else {
166 if (trp->tr_tmo_func)
167 atp_untimout(atp_req_timeout, trp);
168
169 atp = trp->tr_queue;
170 ATP_Q_REMOVE(atp->atp_trans_wait, trp, tr_list);
171
172 if (trp->tr_xmt) {
173 gbuf_freem(trp->tr_xmt);
174 trp->tr_xmt = NULL;
175 }
176 for (i = 0; i < 8; i++) {
177 if (trp->tr_rcv[i]) {
178 gbuf_freem(trp->tr_rcv[i]);
179 trp->tr_rcv[i] = NULL;
180 }
181 }
182 if (trp->tr_bdsp) {
183 gbuf_freem(trp->tr_bdsp);
184 trp->tr_bdsp = NULL;
185 }
186
187 if (trp->tr_rsp_wait) {
188 trp->tr_state = TRANS_ABORTING;
189 ATP_Q_APPEND(atp_trans_abort, trp, tr_list);
190 wakeup(&trp->tr_event);
191 ATENABLE(s, atpgen_lock);
192 return;
193 }
194 }
195
196 ATENABLE(s, atpgen_lock);
197 atp_trans_free(trp);
198 } /* atp_free */
199
200
201 /*
202 * atp_send transmits a request packet by queuing it (if it isn't already) and
203 * scheduling the queue
204 */
205
206 void atp_send(trp)
207 register struct atp_trans *trp;
208 {
209 gbuf_t *m;
210 struct atp_state *atp;
211
212 dPrintf(D_M_ATP_LOW, D_L_OUTPUT, ("atp_send: trp=0x%x, loc=%d\n",
213 (u_int) trp->tr_queue, trp->tr_local_socket));
214
215 if ((atp = trp->tr_queue) != 0) {
216 if (trp->tr_state == TRANS_TIMEOUT) {
217 if ((m = gbuf_cont(trp->tr_xmt)) == NULL)
218 m = trp->tr_xmt;
219
220 /*
221 * Now either release the transaction or start the timer
222 */
223 if (!trp->tr_retry && !trp->tr_bitmap && !trp->tr_xo) {
224 m = (gbuf_t *)gbuf_copym(m);
225 atp_x_done(trp);
226 } else {
227 m = (gbuf_t *)gbuf_dupm(m);
228
229 atp_timout(atp_req_timeout, trp, trp->tr_timeout);
230 }
231
232 if (m) {
233 trace_mbufs(D_M_ATP_LOW, " m", m);
234 DDP_OUTPUT(m);
235 }
236 }
237 }
238 }
239
240
241 /*
242 * atp_reply sends all the available messages in the bitmap again
243 * by queueing us to the write service routine
244 */
245
246 void atp_reply(rcbp)
247 register struct atp_rcb *rcbp;
248 {
249 register struct atp_state *atp;
250 register int i;
251 int s;
252
253 if ((atp = rcbp->rc_queue) != 0) {
254 ATDISABLE(s, atp->atp_lock);
255 for (i = 0; i < rcbp->rc_pktcnt; i++) {
256 if (rcbp->rc_bitmap&atp_mask[i])
257 rcbp->rc_snd[i] = 1;
258 else
259 rcbp->rc_snd[i] = 0;
260 }
261 if (rcbp->rc_rep_waiting == 0) {
262 rcbp->rc_state = RCB_SENDING;
263 rcbp->rc_rep_waiting = 1;
264 ATENABLE(s, atp->atp_lock);
265 atp_send_replies(atp, rcbp);
266 } else
267 ATENABLE(s, atp->atp_lock);
268 }
269 }
270
271
272 /*
273 * The rcb timer just frees the rcb, this happens when we missed a release for XO
274 */
275
276 void atp_rcb_timer()
277 {
278 int s;
279 register struct atp_rcb *rcbp;
280 register struct atp_rcb *next_rcbp;
281 extern struct atp_rcb_qhead atp_need_rel;
282 extern struct atp_trans *trp_tmo_rcb;
283
284 l_again:
285 ATDISABLE(s, atpgen_lock);
286 for (rcbp = atp_need_rel.head; rcbp; rcbp = next_rcbp) {
287 next_rcbp = rcbp->rc_tlist.next;
288
289 if (abs(time.tv_sec - rcbp->rc_timestamp) > 30) {
290 ATENABLE(s, atpgen_lock);
291 atp_rcb_free(rcbp);
292 goto l_again;
293 }
294 }
295 ATENABLE(s, atpgen_lock);
296 atp_timout(atp_rcb_timer, trp_tmo_rcb, 10 * HZ);
297 }
298
299 atp_iocack(atp, m)
300 struct atp_state *atp;
301 register gbuf_t *m;
302 {
303 if (gbuf_type(m) == MSG_IOCTL)
304 gbuf_set_type(m, MSG_IOCACK);
305 if (gbuf_cont(m))
306 ((ioc_t *)gbuf_rptr(m))->ioc_count = gbuf_msgsize(gbuf_cont(m));
307 else
308 ((ioc_t *)gbuf_rptr(m))->ioc_count = 0;
309
310 if (atp->dflag)
311 asp_ack_reply(atp->atp_gref, m);
312 else
313 atalk_putnext(atp->atp_gref, m);
314 }
315
316 atp_iocnak(atp, m, err)
317 struct atp_state *atp;
318 register gbuf_t *m;
319 register int err;
320 {
321 if (gbuf_type(m) == MSG_IOCTL)
322 gbuf_set_type(m, MSG_IOCNAK);
323 ((ioc_t *)gbuf_rptr(m))->ioc_count = 0;
324 ((ioc_t *)gbuf_rptr(m))->ioc_error = err ? err : ENXIO;
325 ((ioc_t *)gbuf_rptr(m))->ioc_rval = -1;
326 if (gbuf_cont(m)) {
327 gbuf_freem(gbuf_cont(m));
328 gbuf_cont(m) = NULL;
329 }
330
331 if (atp->dflag)
332 asp_nak_reply(atp->atp_gref, m);
333 else
334 atalk_putnext(atp->atp_gref, m);
335 }
336
337 /*
338 * Generate a transaction id for a socket
339 */
340 static int lasttid;
341 atp_tid(atp)
342 register struct atp_state *atp;
343 {
344 register int i;
345 register struct atp_trans *trp;
346 int s;
347
348 ATDISABLE(s, atpgen_lock);
349 for (i = lasttid;;) {
350 i = (i+1)&0xffff;
351
352 for (trp = atp->atp_trans_wait.head; trp; trp = trp->tr_list.next) {
353 if (trp->tr_tid == i)
354 break;
355 }
356 if (trp == NULL) {
357 lasttid = i;
358 ATENABLE(s, atpgen_lock);
359 return(i);
360 }
361 }
362 }