2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1996-1998 Apple Computer, Inc.
24 * All Rights Reserved.
27 /* Modified for MP, 1996 by Tuyen Nguyen
28 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
30 #include <sys/errno.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <machine/spl.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
44 #include <netat/sysglue.h>
45 #include <netat/appletalk.h>
46 #include <netat/ddp.h>
47 #include <netat/at_pcb.h>
48 #include <netat/atp.h>
49 #include <netat/debug.h>
51 extern atlock_t atpgen_lock
;
53 void atp_send(struct atp_trans
*);
56 * The request timer retries a request, if all retries are used up
62 register struct atp_trans
*trp
;
67 struct atp_state
*atp
;
68 struct atp_trans
*ctrp
;
70 if ((atp
= trp
->tr_queue
) == 0)
72 ATDISABLE(s
, atp
->atp_lock
);
73 if (atp
->atp_flags
& ATP_CLOSING
) {
74 ATENABLE(s
, atp
->atp_lock
);
77 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
82 ATENABLE(s
, atp
->atp_lock
);
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 ATENABLE(s
, atp
->atp_lock
);
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
) {
112 ATENABLE(s
, atp
->atp_lock
);
114 if (trp
->tr_rsp_wait
)
115 thread_wakeup(&trp
->tr_event
);
118 ATENABLE(s
, atp
->atp_lock
);
119 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
123 case AT_ATP_ISSUE_REQUEST_NOTE
:
124 case AT_ATP_ISSUE_REQUEST_TICKLE
:
125 trp
->tr_xmt
= gbuf_cont(m
);
130 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
132 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
134 ATENABLE(s
, atp
->atp_lock
);
141 * atp_free frees up a request, cleaning up the queues and freeing
143 * always called at 'lock'
147 register struct atp_trans
*trp
;
149 register struct atp_state
*atp
;
153 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
154 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
155 ATDISABLE(s
, atpgen_lock
);
156 if (trp
->tr_tmo_func
)
157 atp_untimout(atp_req_timeout
, trp
);
160 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
163 gbuf_freem(trp
->tr_xmt
);
166 for (i
= 0; i
< 8; i
++) {
167 if (trp
->tr_rcv
[i
]) {
168 gbuf_freem(trp
->tr_rcv
[i
]);
169 trp
->tr_rcv
[i
] = NULL
;
173 gbuf_freem(trp
->tr_bdsp
);
177 ATENABLE(s
, atpgen_lock
);
184 * atp_send transmits a request packet by queuing it (if it isn't already) and
185 * scheduling the queue
189 register struct atp_trans
*trp
;
192 struct atp_state
*atp
;
194 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send: trp=0x%x, loc=%d\n",
195 (u_int
) trp
->tr_queue
, trp
->tr_local_socket
));
197 if ((atp
= trp
->tr_queue
) != 0) {
198 if (trp
->tr_state
== TRANS_TIMEOUT
) {
199 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
203 * Now either release the transaction or start the timer
205 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
206 m
= (gbuf_t
*)gbuf_copym(m
);
209 m
= (gbuf_t
*)gbuf_dupm(m
);
211 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
215 trace_mbufs(D_M_ATP_LOW
, " m", m
);
224 * atp_reply sends all the available messages in the bitmap again
225 * by queueing us to the write service routine
229 register struct atp_rcb
*rcbp
;
231 register struct atp_state
*atp
;
235 if ((atp
= rcbp
->rc_queue
) != 0) {
236 ATDISABLE(s
, atp
->atp_lock
);
237 for (i
= 0; i
< rcbp
->rc_pktcnt
; i
++) {
238 if (rcbp
->rc_bitmap
&atp_mask
[i
])
243 if (rcbp
->rc_rep_waiting
== 0) {
244 rcbp
->rc_state
= RCB_SENDING
;
245 rcbp
->rc_rep_waiting
= 1;
246 ATENABLE(s
, atp
->atp_lock
);
247 atp_send_replies(atp
, rcbp
);
249 ATENABLE(s
, atp
->atp_lock
);
255 * The rcb timer just frees the rcb, this happens when we missed a release for XO
261 register struct atp_rcb
*rcbp
;
262 register struct atp_rcb
*next_rcbp
;
263 extern struct atp_rcb_qhead atp_need_rel
;
264 extern struct atp_trans
*trp_tmo_rcb
;
267 ATDISABLE(s
, atpgen_lock
);
268 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
269 next_rcbp
= rcbp
->rc_tlist
.next
;
271 if (abs(time
.tv_sec
- rcbp
->rc_timestamp
) > 30) {
272 ATENABLE(s
, atpgen_lock
);
277 ATENABLE(s
, atpgen_lock
);
278 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
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 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
;
330 ATDISABLE(s
, atpgen_lock
);
331 for (i
= lasttid
;;) {
334 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
335 if (trp
->tr_tid
== i
)
340 ATENABLE(s
, atpgen_lock
);