2 * Copyright (c) 2006 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>
59 extern atlock_t atpgen_lock
;
61 void atp_send(struct atp_trans
*);
64 * The request timer retries a request, if all retries are used up
70 register struct atp_trans
*trp
;
75 struct atp_state
*atp
;
76 struct atp_trans
*ctrp
;
78 if ((atp
= trp
->tr_queue
) == 0)
80 ATDISABLE(s
, atp
->atp_lock
);
81 if (atp
->atp_flags
& ATP_CLOSING
) {
82 ATENABLE(s
, atp
->atp_lock
);
85 for (ctrp
= atp
->atp_trans_wait
.head
; ctrp
; ctrp
= ctrp
->tr_list
.next
) {
90 ATENABLE(s
, atp
->atp_lock
);
94 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
95 m
= trp
->tr_xmt
; /* issued via the new interface */
97 if (trp
->tr_retry
== 0) {
98 trp
->tr_state
= TRANS_FAILED
;
99 if (m
== trp
->tr_xmt
) {
104 gbuf_set_type(m
, MSG_DATA
);
105 gref
= trp
->tr_queue
->atp_gref
;
106 ATENABLE(s
, atp
->atp_lock
);
107 atalk_putnext(gref
, m
);
111 dPrintf(D_M_ATP_LOW
,D_L_INFO
, ("atp_req_timeout: skt=%d\n",
112 trp
->tr_local_socket
));
114 switch(((ioc_t
*)(gbuf_rptr(trp
->tr_xmt
)))->ioc_cmd
) {
115 case AT_ATP_ISSUE_REQUEST
:
117 if (trp
->tr_queue
->dflag
)
118 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
119 else if (trp
->tr_bdsp
== NULL
) {
120 ATENABLE(s
, atp
->atp_lock
);
122 if (trp
->tr_rsp_wait
)
123 wakeup(&trp
->tr_event
);
126 ATENABLE(s
, atp
->atp_lock
);
127 atp_iocnak(trp
->tr_queue
, m
, ETIMEDOUT
);
131 case AT_ATP_ISSUE_REQUEST_NOTE
:
132 case AT_ATP_ISSUE_REQUEST_TICKLE
:
133 trp
->tr_xmt
= gbuf_cont(m
);
138 (AT_ATP_HDR(m
))->bitmap
= trp
->tr_bitmap
;
140 if (trp
->tr_retry
!= (unsigned int) ATP_INFINITE_RETRIES
)
142 ATENABLE(s
, atp
->atp_lock
);
149 * atp_free frees up a request, cleaning up the queues and freeing
151 * always called at 'lock'
155 register struct atp_trans
*trp
;
157 register struct atp_state
*atp
;
161 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
162 ("atp_free: freeing trp 0x%x\n", (u_int
) trp
));
164 ATDISABLE(s
, atpgen_lock
);
166 if (trp
->tr_state
== TRANS_ABORTING
) {
167 ATP_Q_REMOVE(atp_trans_abort
, trp
, tr_list
);
168 trp
->tr_state
= TRANS_DONE
;
171 if (trp
->tr_tmo_func
)
172 atp_untimout(atp_req_timeout
, trp
);
175 ATP_Q_REMOVE(atp
->atp_trans_wait
, trp
, tr_list
);
178 gbuf_freem(trp
->tr_xmt
);
181 for (i
= 0; i
< 8; i
++) {
182 if (trp
->tr_rcv
[i
]) {
183 gbuf_freem(trp
->tr_rcv
[i
]);
184 trp
->tr_rcv
[i
] = NULL
;
188 gbuf_freem(trp
->tr_bdsp
);
192 if (trp
->tr_rsp_wait
) {
193 trp
->tr_state
= TRANS_ABORTING
;
194 ATP_Q_APPEND(atp_trans_abort
, trp
, tr_list
);
195 wakeup(&trp
->tr_event
);
196 ATENABLE(s
, atpgen_lock
);
201 ATENABLE(s
, atpgen_lock
);
207 * atp_send transmits a request packet by queuing it (if it isn't already) and
208 * scheduling the queue
212 register struct atp_trans
*trp
;
215 struct atp_state
*atp
;
217 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send: trp=0x%x, loc=%d\n",
218 (u_int
) trp
->tr_queue
, trp
->tr_local_socket
));
220 if ((atp
= trp
->tr_queue
) != 0) {
221 if (trp
->tr_state
== TRANS_TIMEOUT
) {
222 if ((m
= gbuf_cont(trp
->tr_xmt
)) == NULL
)
226 * Now either release the transaction or start the timer
228 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
) {
229 m
= (gbuf_t
*)gbuf_copym(m
);
232 m
= (gbuf_t
*)gbuf_dupm(m
);
234 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
238 trace_mbufs(D_M_ATP_LOW
, " m", m
);
247 * atp_reply sends all the available messages in the bitmap again
248 * by queueing us to the write service routine
252 register struct atp_rcb
*rcbp
;
254 register struct atp_state
*atp
;
258 if ((atp
= rcbp
->rc_queue
) != 0) {
259 ATDISABLE(s
, atp
->atp_lock
);
260 for (i
= 0; i
< rcbp
->rc_pktcnt
; i
++) {
261 if (rcbp
->rc_bitmap
&atp_mask
[i
])
266 if (rcbp
->rc_rep_waiting
== 0) {
267 rcbp
->rc_state
= RCB_SENDING
;
268 rcbp
->rc_rep_waiting
= 1;
269 ATENABLE(s
, atp
->atp_lock
);
270 atp_send_replies(atp
, rcbp
);
272 ATENABLE(s
, atp
->atp_lock
);
278 * The rcb timer just frees the rcb, this happens when we missed a release for XO
284 register struct atp_rcb
*rcbp
;
285 register struct atp_rcb
*next_rcbp
;
286 extern struct atp_rcb_qhead atp_need_rel
;
287 extern struct atp_trans
*trp_tmo_rcb
;
288 struct timeval timenow
;
291 ATDISABLE(s
, atpgen_lock
);
292 getmicrouptime(&timenow
);
293 for (rcbp
= atp_need_rel
.head
; rcbp
; rcbp
= next_rcbp
) {
294 next_rcbp
= rcbp
->rc_tlist
.next
;
296 if (abs(timenow
.tv_sec
- rcbp
->rc_timestamp
) > 30) {
297 ATENABLE(s
, atpgen_lock
);
302 ATENABLE(s
, atpgen_lock
);
303 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
307 struct atp_state
*atp
;
310 if (gbuf_type(m
) == MSG_IOCTL
)
311 gbuf_set_type(m
, MSG_IOCACK
);
313 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
315 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
318 asp_ack_reply(atp
->atp_gref
, m
);
320 atalk_putnext(atp
->atp_gref
, m
);
323 atp_iocnak(atp
, m
, err
)
324 struct atp_state
*atp
;
328 if (gbuf_type(m
) == MSG_IOCTL
)
329 gbuf_set_type(m
, MSG_IOCNAK
);
330 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
331 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
? err
: ENXIO
;
332 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
334 gbuf_freem(gbuf_cont(m
));
339 asp_nak_reply(atp
->atp_gref
, m
);
341 atalk_putnext(atp
->atp_gref
, m
);
345 * Generate a transaction id for a socket
349 register struct atp_state
*atp
;
352 register struct atp_trans
*trp
;
355 ATDISABLE(s
, atpgen_lock
);
356 for (i
= lasttid
;;) {
359 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
360 if (trp
->tr_tid
== i
)
365 ATENABLE(s
, atpgen_lock
);