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/debug.h>
58 void atp_send(struct atp_trans
*);
61 * The request timer retries a request, if all retries are used up
67 register struct atp_trans
*trp
;
71 struct atp_state
*atp
;
72 struct atp_trans
*ctrp
;
74 if ((atp
= trp
->tr_queue
) == 0)
76 if (atp
->atp_flags
& ATP_CLOSING
)
79 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
86 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
87 m
= trp
->tr_xmt
; /* issued via the new interface */
89 if (trp
->tr_retry
== 0) {
90 trp
->tr_state
= TRANS_FAILED
;
91 if (m
== trp
->tr_xmt
) {
96 gbuf_set_type(m
, MSG_DATA
);
97 gref
= trp
->tr_queue
->atp_gref
;
98 atalk_putnext(gref
, m
);
102 dPrintf(D_M_ATP_LOW
,D_L_INFO
, ("atp_req_timeout: skt=%d\n",
103 trp
->tr_local_socket
));
105 switch(((ioc_t
*)(gbuf_rptr(trp
->tr_xmt
)))->ioc_cmd
) {
106 case AT_ATP_ISSUE_REQUEST
:
108 if (trp
->tr_queue
->dflag
)
109 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
110 else if (trp
->tr_bdsp
== NULL
) {
112 if (trp
->tr_rsp_wait
)
113 wakeup(&trp
->tr_event
);
116 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
120 case AT_ATP_ISSUE_REQUEST_NOTE
:
121 case AT_ATP_ISSUE_REQUEST_TICKLE
:
122 trp
->tr_xmt
= gbuf_cont(m
);
127 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
129 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
137 * atp_free frees up a request, cleaning up the queues and freeing
139 * always called at 'lock'
143 register struct atp_trans
*trp
;
145 register struct atp_state
*atp
;
148 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
149 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
152 if (trp
->tr_state
== TRANS_ABORTING
) {
153 ATP_Q_REMOVE(atp_trans_abort
, trp
, tr_list
);
154 trp
->tr_state
= TRANS_DONE
;
157 if (trp
->tr_tmo_func
)
158 atp_untimout(atp_req_timeout
, trp
);
161 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
164 gbuf_freem(trp
->tr_xmt
);
167 for (i
= 0; i
< 8; i
++) {
168 if (trp
->tr_rcv
[i
]) {
169 gbuf_freem(trp
->tr_rcv
[i
]);
170 trp
->tr_rcv
[i
] = NULL
;
174 gbuf_freem(trp
->tr_bdsp
);
178 if (trp
->tr_rsp_wait
) {
179 trp
->tr_state
= TRANS_ABORTING
;
180 ATP_Q_APPEND(atp_trans_abort
, trp
, tr_list
);
181 wakeup(&trp
->tr_event
);
191 * atp_send transmits a request packet by queuing it (if it isn't already) and
192 * scheduling the queue
196 register struct atp_trans
*trp
;
199 struct atp_state
*atp
;
201 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send: trp=0x%x, loc=%d\n",
202 (u_int
) trp
->tr_queue
, trp
->tr_local_socket
));
204 if ((atp
= trp
->tr_queue
) != 0) {
205 if (trp
->tr_state
== TRANS_TIMEOUT
) {
206 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
210 * Now either release the transaction or start the timer
212 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
213 m
= (gbuf_t
*)gbuf_copym(m
);
216 m
= (gbuf_t
*)gbuf_dupm(m
);
218 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
222 trace_mbufs(D_M_ATP_LOW
, " m", m
);
231 * atp_reply sends all the available messages in the bitmap again
232 * by queueing us to the write service routine
236 register struct atp_rcb
*rcbp
;
238 register struct atp_state
*atp
;
241 if ((atp
= rcbp
->rc_queue
) != 0) {
242 for (i
= 0; i
< rcbp
->rc_pktcnt
; i
++) {
243 if (rcbp
->rc_bitmap
&atp_mask
[i
])
248 if (rcbp
->rc_rep_waiting
== 0) {
249 rcbp
->rc_state
= RCB_SENDING
;
250 rcbp
->rc_rep_waiting
= 1;
251 atp_send_replies(atp
, rcbp
);
258 * The rcb timer just frees the rcb, this happens when we missed a release for XO
263 register struct atp_rcb
*rcbp
;
264 register struct atp_rcb
*next_rcbp
;
265 extern struct atp_rcb_qhead atp_need_rel
;
266 extern struct atp_trans
*trp_tmo_rcb
;
267 struct timeval timenow
;
270 getmicrouptime(&timenow
);
271 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
272 next_rcbp
= rcbp
->rc_tlist
.next
;
274 if ((timenow
.tv_sec
- rcbp
->rc_timestamp
) > 30) {
279 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
283 struct atp_state
*atp
;
286 if (gbuf_type(m
) == MSG_IOCTL
)
287 gbuf_set_type(m
, MSG_IOCACK
);
289 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
291 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
294 asp_ack_reply(atp
->atp_gref
, m
);
296 atalk_putnext(atp
->atp_gref
, m
);
299 atp_iocnak(atp
, m
, err
)
300 struct atp_state
*atp
;
304 if (gbuf_type(m
) == MSG_IOCTL
)
305 gbuf_set_type(m
, MSG_IOCNAK
);
306 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
307 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
? err
: ENXIO
;
308 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
310 gbuf_freem(gbuf_cont(m
));
315 asp_nak_reply(atp
->atp_gref
, m
);
317 atalk_putnext(atp
->atp_gref
, m
);
321 * Generate a transaction id for a socket
325 register struct atp_state
*atp
;
328 register struct atp_trans
*trp
;
330 for (i
= lasttid
;;) {
333 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
334 if (trp
->tr_tid
== i
)