2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1996-1998 Apple Computer, Inc.
32 * All Rights Reserved.
35 /* Modified for MP, 1996 by Tuyen Nguyen
36 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <machine/spl.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
48 #include <sys/ioctl.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
52 #include <netat/sysglue.h>
53 #include <netat/appletalk.h>
54 #include <netat/ddp.h>
55 #include <netat/at_pcb.h>
56 #include <netat/atp.h>
57 #include <netat/debug.h>
60 void atp_send(struct atp_trans
*);
63 * The request timer retries a request, if all retries are used up
69 register struct atp_trans
*trp
;
73 struct atp_state
*atp
;
74 struct atp_trans
*ctrp
;
76 if ((atp
= trp
->tr_queue
) == 0)
78 if (atp
->atp_flags
& ATP_CLOSING
)
81 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
88 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
89 m
= trp
->tr_xmt
; /* issued via the new interface */
91 if (trp
->tr_retry
== 0) {
92 trp
->tr_state
= TRANS_FAILED
;
93 if (m
== trp
->tr_xmt
) {
98 gbuf_set_type(m
, MSG_DATA
);
99 gref
= trp
->tr_queue
->atp_gref
;
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
) {
114 if (trp
->tr_rsp_wait
)
115 wakeup(&trp
->tr_event
);
118 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
122 case AT_ATP_ISSUE_REQUEST_NOTE
:
123 case AT_ATP_ISSUE_REQUEST_TICKLE
:
124 trp
->tr_xmt
= gbuf_cont(m
);
129 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
131 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
139 * atp_free frees up a request, cleaning up the queues and freeing
141 * always called at 'lock'
145 register struct atp_trans
*trp
;
147 register struct atp_state
*atp
;
150 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
151 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
154 if (trp
->tr_state
== TRANS_ABORTING
) {
155 ATP_Q_REMOVE(atp_trans_abort
, trp
, tr_list
);
156 trp
->tr_state
= TRANS_DONE
;
159 if (trp
->tr_tmo_func
)
160 atp_untimout(atp_req_timeout
, trp
);
163 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
166 gbuf_freem(trp
->tr_xmt
);
169 for (i
= 0; i
< 8; i
++) {
170 if (trp
->tr_rcv
[i
]) {
171 gbuf_freem(trp
->tr_rcv
[i
]);
172 trp
->tr_rcv
[i
] = NULL
;
176 gbuf_freem(trp
->tr_bdsp
);
180 if (trp
->tr_rsp_wait
) {
181 trp
->tr_state
= TRANS_ABORTING
;
182 ATP_Q_APPEND(atp_trans_abort
, trp
, tr_list
);
183 wakeup(&trp
->tr_event
);
193 * atp_send transmits a request packet by queuing it (if it isn't already) and
194 * scheduling the queue
198 register struct atp_trans
*trp
;
201 struct atp_state
*atp
;
203 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send: trp=0x%x, loc=%d\n",
204 (u_int
) trp
->tr_queue
, trp
->tr_local_socket
));
206 if ((atp
= trp
->tr_queue
) != 0) {
207 if (trp
->tr_state
== TRANS_TIMEOUT
) {
208 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
212 * Now either release the transaction or start the timer
214 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
215 m
= (gbuf_t
*)gbuf_copym(m
);
218 m
= (gbuf_t
*)gbuf_dupm(m
);
220 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
224 trace_mbufs(D_M_ATP_LOW
, " m", m
);
233 * atp_reply sends all the available messages in the bitmap again
234 * by queueing us to the write service routine
238 register struct atp_rcb
*rcbp
;
240 register struct atp_state
*atp
;
243 if ((atp
= rcbp
->rc_queue
) != 0) {
244 for (i
= 0; i
< rcbp
->rc_pktcnt
; i
++) {
245 if (rcbp
->rc_bitmap
&atp_mask
[i
])
250 if (rcbp
->rc_rep_waiting
== 0) {
251 rcbp
->rc_state
= RCB_SENDING
;
252 rcbp
->rc_rep_waiting
= 1;
253 atp_send_replies(atp
, rcbp
);
260 * The rcb timer just frees the rcb, this happens when we missed a release for XO
265 register struct atp_rcb
*rcbp
;
266 register struct atp_rcb
*next_rcbp
;
267 extern struct atp_rcb_qhead atp_need_rel
;
268 extern struct atp_trans
*trp_tmo_rcb
;
269 struct timeval timenow
;
272 getmicrouptime(&timenow
);
273 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
274 next_rcbp
= rcbp
->rc_tlist
.next
;
276 if ((timenow
.tv_sec
- rcbp
->rc_timestamp
) > 30) {
281 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
285 struct atp_state
*atp
;
288 if (gbuf_type(m
) == MSG_IOCTL
)
289 gbuf_set_type(m
, MSG_IOCACK
);
291 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
293 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
296 asp_ack_reply(atp
->atp_gref
, m
);
298 atalk_putnext(atp
->atp_gref
, m
);
301 atp_iocnak(atp
, m
, err
)
302 struct atp_state
*atp
;
306 if (gbuf_type(m
) == MSG_IOCTL
)
307 gbuf_set_type(m
, MSG_IOCNAK
);
308 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
309 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
? err
: ENXIO
;
310 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
312 gbuf_freem(gbuf_cont(m
));
317 asp_nak_reply(atp
->atp_gref
, m
);
319 atalk_putnext(atp
->atp_gref
, m
);
323 * Generate a transaction id for a socket
327 register struct atp_state
*atp
;
330 register struct atp_trans
*trp
;
332 for (i
= lasttid
;;) {
335 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
336 if (trp
->tr_tid
== i
)