2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1996-1998 Apple Computer, Inc.
30 * All Rights Reserved.
33 /* Modified for MP, 1996 by Tuyen Nguyen
34 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/spl.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
46 #include <sys/ioctl.h>
47 #include <sys/malloc.h>
48 #include <sys/socket.h>
50 #include <netat/sysglue.h>
51 #include <netat/appletalk.h>
52 #include <netat/ddp.h>
53 #include <netat/at_pcb.h>
54 #include <netat/atp.h>
55 #include <netat/asp.h>
56 #include <netat/debug.h>
58 extern struct atp_rcb_qhead atp_need_rel
;
59 extern struct atp_trans
*trp_tmo_rcb
;
62 * The request timer retries a request, if all retries are used up
68 register struct atp_trans
*trp
;
72 struct atp_state
*atp
;
73 struct atp_trans
*ctrp
;
75 if ((atp
= trp
->tr_queue
) == 0)
77 if (atp
->atp_flags
& ATP_CLOSING
)
80 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
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 atalk_putnext(gref
, m
);
103 dPrintf(D_M_ATP_LOW
,D_L_INFO
, ("atp_req_timeout: skt=%d\n",
104 trp
->tr_local_socket
));
106 switch(((ioc_t
*)(gbuf_rptr(trp
->tr_xmt
)))->ioc_cmd
) {
107 case AT_ATP_ISSUE_REQUEST
:
109 if (trp
->tr_queue
->dflag
)
110 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
111 else if (trp
->tr_bdsp
== NULL
) {
113 if (trp
->tr_rsp_wait
)
114 wakeup(&trp
->tr_event
);
117 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
121 case AT_ATP_ISSUE_REQUEST_NOTE
:
122 case AT_ATP_ISSUE_REQUEST_TICKLE
:
123 trp
->tr_xmt
= gbuf_cont(m
);
128 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
130 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
138 * atp_free frees up a request, cleaning up the queues and freeing
140 * always called at 'lock'
144 register struct atp_trans
*trp
;
146 register struct atp_state
*atp
;
149 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
150 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
153 if (trp
->tr_state
== TRANS_ABORTING
) {
154 ATP_Q_REMOVE(atp_trans_abort
, trp
, tr_list
);
155 trp
->tr_state
= TRANS_DONE
;
158 if (trp
->tr_tmo_func
)
159 atp_untimout(atp_req_timeout
, trp
);
162 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
165 gbuf_freem(trp
->tr_xmt
);
168 for (i
= 0; i
< 8; i
++) {
169 if (trp
->tr_rcv
[i
]) {
170 gbuf_freem(trp
->tr_rcv
[i
]);
171 trp
->tr_rcv
[i
] = NULL
;
175 gbuf_freem(trp
->tr_bdsp
);
179 if (trp
->tr_rsp_wait
) {
180 trp
->tr_state
= TRANS_ABORTING
;
181 ATP_Q_APPEND(atp_trans_abort
, trp
, tr_list
);
182 wakeup(&trp
->tr_event
);
192 * atp_send transmits a request packet by queuing it (if it isn't already) and
193 * scheduling the queue
197 register struct atp_trans
*trp
;
200 struct atp_state
*atp
;
202 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send: trp=0x%x, loc=%d\n",
203 (u_int
) trp
->tr_queue
, trp
->tr_local_socket
));
205 if ((atp
= trp
->tr_queue
) != 0) {
206 if (trp
->tr_state
== TRANS_TIMEOUT
) {
207 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
211 * Now either release the transaction or start the timer
213 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
214 m
= (gbuf_t
*)gbuf_copym(m
);
217 m
= (gbuf_t
*)gbuf_dupm(m
);
219 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
223 trace_mbufs(D_M_ATP_LOW
, " m", m
);
232 * atp_reply sends all the available messages in the bitmap again
233 * by queueing us to the write service routine
237 register struct atp_rcb
*rcbp
;
239 register struct atp_state
*atp
;
242 if ((atp
= rcbp
->rc_queue
) != 0) {
243 for (i
= 0; i
< rcbp
->rc_pktcnt
; i
++) {
244 if (rcbp
->rc_bitmap
&atp_mask
[i
])
249 if (rcbp
->rc_rep_waiting
== 0) {
250 rcbp
->rc_state
= RCB_SENDING
;
251 rcbp
->rc_rep_waiting
= 1;
252 atp_send_replies(atp
, rcbp
);
259 * The rcb timer just frees the rcb, this happens when we missed a release for XO
262 void atp_rcb_timer(__unused
struct atp_trans
*junk
)
264 register struct atp_rcb
*rcbp
;
265 register struct atp_rcb
*next_rcbp
;
266 struct timeval timenow
;
269 getmicrouptime(&timenow
);
270 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
271 next_rcbp
= rcbp
->rc_tlist
.next
;
273 if ((timenow
.tv_sec
- rcbp
->rc_timestamp
) > 30) {
278 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
281 void atp_iocack(atp
, m
)
282 struct atp_state
*atp
;
285 if (gbuf_type(m
) == MSG_IOCTL
)
286 gbuf_set_type(m
, MSG_IOCACK
);
288 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
290 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
293 asp_ack_reply(atp
->atp_gref
, m
);
295 atalk_putnext(atp
->atp_gref
, m
);
298 void atp_iocnak(atp
, m
, err
)
299 struct atp_state
*atp
;
303 if (gbuf_type(m
) == MSG_IOCTL
)
304 gbuf_set_type(m
, MSG_IOCNAK
);
305 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
306 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
? err
: ENXIO
;
307 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
309 gbuf_freem(gbuf_cont(m
));
314 asp_nak_reply(atp
->atp_gref
, m
);
316 atalk_putnext(atp
->atp_gref
, m
);
320 * Generate a transaction id for a socket
324 register struct atp_state
*atp
;
327 register struct atp_trans
*trp
;
329 for (i
= lasttid
;;) {
332 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
333 if (trp
->tr_tid
== i
)