2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1996-1998 Apple Computer, Inc.
25 * All Rights Reserved.
28 /* Modified for MP, 1996 by Tuyen Nguyen
29 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <machine/spl.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/filedesc.h>
39 #include <sys/fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/malloc.h>
43 #include <sys/socket.h>
45 #include <netat/sysglue.h>
46 #include <netat/appletalk.h>
47 #include <netat/ddp.h>
48 #include <netat/at_pcb.h>
49 #include <netat/atp.h>
50 #include <netat/debug.h>
52 extern atlock_t atpgen_lock
;
54 void atp_send(struct atp_trans
*);
57 * The request timer retries a request, if all retries are used up
63 register struct atp_trans
*trp
;
68 struct atp_state
*atp
;
69 struct atp_trans
*ctrp
;
71 if ((atp
= trp
->tr_queue
) == 0)
73 ATDISABLE(s
, atp
->atp_lock
);
74 if (atp
->atp_flags
& ATP_CLOSING
) {
75 ATENABLE(s
, atp
->atp_lock
);
78 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
83 ATENABLE(s
, atp
->atp_lock
);
87 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
88 m
= trp
->tr_xmt
; /* issued via the new interface */
90 if (trp
->tr_retry
== 0) {
91 trp
->tr_state
= TRANS_FAILED
;
92 if (m
== trp
->tr_xmt
) {
97 gbuf_set_type(m
, MSG_DATA
);
98 gref
= trp
->tr_queue
->atp_gref
;
99 ATENABLE(s
, atp
->atp_lock
);
100 atalk_putnext(gref
, m
);
104 dPrintf(D_M_ATP_LOW
,D_L_INFO
, ("atp_req_timeout: skt=%d\n",
105 trp
->tr_local_socket
));
107 switch(((ioc_t
*)(gbuf_rptr(trp
->tr_xmt
)))->ioc_cmd
) {
108 case AT_ATP_ISSUE_REQUEST
:
110 if (trp
->tr_queue
->dflag
)
111 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
112 else if (trp
->tr_bdsp
== NULL
) {
113 ATENABLE(s
, atp
->atp_lock
);
115 if (trp
->tr_rsp_wait
)
116 wakeup(&trp
->tr_event
);
119 ATENABLE(s
, atp
->atp_lock
);
120 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
124 case AT_ATP_ISSUE_REQUEST_NOTE
:
125 case AT_ATP_ISSUE_REQUEST_TICKLE
:
126 trp
->tr_xmt
= gbuf_cont(m
);
131 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
133 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
135 ATENABLE(s
, atp
->atp_lock
);
142 * atp_free frees up a request, cleaning up the queues and freeing
144 * always called at 'lock'
148 register struct atp_trans
*trp
;
150 register struct atp_state
*atp
;
154 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
155 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
157 ATDISABLE(s
, atpgen_lock
);
159 if (trp
->tr_state
== TRANS_ABORTING
) {
160 ATP_Q_REMOVE(atp_trans_abort
, trp
, tr_list
);
161 trp
->tr_state
= TRANS_DONE
;
164 if (trp
->tr_tmo_func
)
165 atp_untimout(atp_req_timeout
, trp
);
168 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
171 gbuf_freem(trp
->tr_xmt
);
174 for (i
= 0; i
< 8; i
++) {
175 if (trp
->tr_rcv
[i
]) {
176 gbuf_freem(trp
->tr_rcv
[i
]);
177 trp
->tr_rcv
[i
] = NULL
;
181 gbuf_freem(trp
->tr_bdsp
);
185 if (trp
->tr_rsp_wait
) {
186 trp
->tr_state
= TRANS_ABORTING
;
187 ATP_Q_APPEND(atp_trans_abort
, trp
, tr_list
);
188 wakeup(&trp
->tr_event
);
189 ATENABLE(s
, atpgen_lock
);
194 ATENABLE(s
, atpgen_lock
);
200 * atp_send transmits a request packet by queuing it (if it isn't already) and
201 * scheduling the queue
205 register struct atp_trans
*trp
;
208 struct atp_state
*atp
;
210 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send: trp=0x%x, loc=%d\n",
211 (u_int
) trp
->tr_queue
, trp
->tr_local_socket
));
213 if ((atp
= trp
->tr_queue
) != 0) {
214 if (trp
->tr_state
== TRANS_TIMEOUT
) {
215 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
219 * Now either release the transaction or start the timer
221 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
222 m
= (gbuf_t
*)gbuf_copym(m
);
225 m
= (gbuf_t
*)gbuf_dupm(m
);
227 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
231 trace_mbufs(D_M_ATP_LOW
, " m", m
);
240 * atp_reply sends all the available messages in the bitmap again
241 * by queueing us to the write service routine
245 register struct atp_rcb
*rcbp
;
247 register struct atp_state
*atp
;
251 if ((atp
= rcbp
->rc_queue
) != 0) {
252 ATDISABLE(s
, atp
->atp_lock
);
253 for (i
= 0; i
< rcbp
->rc_pktcnt
; i
++) {
254 if (rcbp
->rc_bitmap
&atp_mask
[i
])
259 if (rcbp
->rc_rep_waiting
== 0) {
260 rcbp
->rc_state
= RCB_SENDING
;
261 rcbp
->rc_rep_waiting
= 1;
262 ATENABLE(s
, atp
->atp_lock
);
263 atp_send_replies(atp
, rcbp
);
265 ATENABLE(s
, atp
->atp_lock
);
271 * The rcb timer just frees the rcb, this happens when we missed a release for XO
277 register struct atp_rcb
*rcbp
;
278 register struct atp_rcb
*next_rcbp
;
279 extern struct atp_rcb_qhead atp_need_rel
;
280 extern struct atp_trans
*trp_tmo_rcb
;
281 struct timeval timenow
;
284 ATDISABLE(s
, atpgen_lock
);
285 getmicrouptime(&timenow
);
286 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
287 next_rcbp
= rcbp
->rc_tlist
.next
;
289 if (abs(timenow
.tv_sec
- rcbp
->rc_timestamp
) > 30) {
290 ATENABLE(s
, atpgen_lock
);
295 ATENABLE(s
, atpgen_lock
);
296 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
300 struct atp_state
*atp
;
303 if (gbuf_type(m
) == MSG_IOCTL
)
304 gbuf_set_type(m
, MSG_IOCACK
);
306 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
308 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
311 asp_ack_reply(atp
->atp_gref
, m
);
313 atalk_putnext(atp
->atp_gref
, m
);
316 atp_iocnak(atp
, m
, err
)
317 struct atp_state
*atp
;
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;
327 gbuf_freem(gbuf_cont(m
));
332 asp_nak_reply(atp
->atp_gref
, m
);
334 atalk_putnext(atp
->atp_gref
, m
);
338 * Generate a transaction id for a socket
342 register struct atp_state
*atp
;
345 register struct atp_trans
*trp
;
348 ATDISABLE(s
, atpgen_lock
);
349 for (i
= lasttid
;;) {
352 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
353 if (trp
->tr_tid
== i
)
358 ATENABLE(s
, atpgen_lock
);