2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1996-1998 Apple Computer, Inc.
27 * All Rights Reserved.
30 /* Modified for MP, 1996 by Tuyen Nguyen
31 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
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>
40 #include <sys/filedesc.h>
41 #include <sys/fcntl.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
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>
54 extern atlock_t atpgen_lock
;
56 void atp_send(struct atp_trans
*);
59 * The request timer retries a request, if all retries are used up
65 register struct atp_trans
*trp
;
70 struct atp_state
*atp
;
71 struct atp_trans
*ctrp
;
73 if ((atp
= trp
->tr_queue
) == 0)
75 ATDISABLE(s
, atp
->atp_lock
);
76 if (atp
->atp_flags
& ATP_CLOSING
) {
77 ATENABLE(s
, atp
->atp_lock
);
80 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
85 ATENABLE(s
, atp
->atp_lock
);
89 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
90 m
= trp
->tr_xmt
; /* issued via the new interface */
92 if (trp
->tr_retry
== 0) {
93 trp
->tr_state
= TRANS_FAILED
;
94 if (m
== trp
->tr_xmt
) {
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
);
106 dPrintf(D_M_ATP_LOW
,D_L_INFO
, ("atp_req_timeout: skt=%d\n",
107 trp
->tr_local_socket
));
109 switch(((ioc_t
*)(gbuf_rptr(trp
->tr_xmt
)))->ioc_cmd
) {
110 case AT_ATP_ISSUE_REQUEST
:
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
);
117 if (trp
->tr_rsp_wait
)
118 wakeup(&trp
->tr_event
);
121 ATENABLE(s
, atp
->atp_lock
);
122 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
126 case AT_ATP_ISSUE_REQUEST_NOTE
:
127 case AT_ATP_ISSUE_REQUEST_TICKLE
:
128 trp
->tr_xmt
= gbuf_cont(m
);
133 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
135 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
137 ATENABLE(s
, atp
->atp_lock
);
144 * atp_free frees up a request, cleaning up the queues and freeing
146 * always called at 'lock'
150 register struct atp_trans
*trp
;
152 register struct atp_state
*atp
;
156 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
157 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
159 ATDISABLE(s
, atpgen_lock
);
161 if (trp
->tr_state
== TRANS_ABORTING
) {
162 ATP_Q_REMOVE(atp_trans_abort
, trp
, tr_list
);
163 trp
->tr_state
= TRANS_DONE
;
166 if (trp
->tr_tmo_func
)
167 atp_untimout(atp_req_timeout
, trp
);
170 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
173 gbuf_freem(trp
->tr_xmt
);
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
;
183 gbuf_freem(trp
->tr_bdsp
);
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
);
196 ATENABLE(s
, atpgen_lock
);
202 * atp_send transmits a request packet by queuing it (if it isn't already) and
203 * scheduling the queue
207 register struct atp_trans
*trp
;
210 struct atp_state
*atp
;
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
));
215 if ((atp
= trp
->tr_queue
) != 0) {
216 if (trp
->tr_state
== TRANS_TIMEOUT
) {
217 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
221 * Now either release the transaction or start the timer
223 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
224 m
= (gbuf_t
*)gbuf_copym(m
);
227 m
= (gbuf_t
*)gbuf_dupm(m
);
229 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
233 trace_mbufs(D_M_ATP_LOW
, " m", m
);
242 * atp_reply sends all the available messages in the bitmap again
243 * by queueing us to the write service routine
247 register struct atp_rcb
*rcbp
;
249 register struct atp_state
*atp
;
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
])
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
);
267 ATENABLE(s
, atp
->atp_lock
);
273 * The rcb timer just frees the rcb, this happens when we missed a release for XO
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
;
285 ATDISABLE(s
, atpgen_lock
);
286 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
287 next_rcbp
= rcbp
->rc_tlist
.next
;
289 if (abs(time
.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
);