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>
52 #include <netat/sysglue.h>
53 #include <netat/appletalk.h>
54 #include <netat/ddp.h>
55 #include <netat/at_pcb.h>
56 #include <netat/at_var.h>
57 #include <netat/atp.h>
58 #include <netat/asp.h>
59 #include <netat/debug.h>
61 static void atp_trans_complete(struct atp_trans
*);
62 void atp_x_done_locked(void *);
63 void atp_treq_event(void *);
66 * Decide what to do about received messages
67 * Version 1.7 of atp_read.c on 89/02/09 17:53:16
70 void atp_treq_event(void *arg
)
72 register gref_t
*gref
= (gref_t
*)arg
;
74 register struct atp_state
*atp
;
77 atp
= (struct atp_state
*)gref
->info
;
79 atp
= (struct atp_state
*)atp
->atp_msgq
;
82 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) != NULL
) {
83 gbuf_set_type(m
, MSG_IOCTL
);
84 gbuf_wset(m
,sizeof(ioc_t
));
85 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_GET_POLL
;
89 else if ((m
= gbuf_alloc(1, PRI_HI
)) != NULL
) {
92 atalk_putnext(gref
, m
);
96 timeout(atp_treq_event
, gref
, 10);
100 void atp_rput(gref
, m
)
104 register at_atp_t
*athp
;
105 register struct atp_state
*atp
;
106 gbuf_t
*m_asp
= NULL
;
107 struct timeval timenow
;
110 atp
= (struct atp_state
*)gref
->info
;
112 atp
= (struct atp_state
*)atp
->atp_msgq
;
114 switch(gbuf_type(m
)) {
117 * Decode the message, make sure it is an atp
120 if (((AT_DDP_HDR(m
))->type
!= DDP_ATP
) ||
121 (atp
->atp_flags
& ATP_CLOSING
)) {
123 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
124 ("atp_rput: dropping MSG, not atp\n"));
128 athp
= AT_ATP_HDR(m
);
129 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
130 ("atp_rput MSG_DATA: %s (%d)\n",
131 (athp
->cmd
== ATP_CMD_TRESP
)? "TRESP":
132 (athp
->cmd
== ATP_CMD_TREL
)? "TREL":
133 (athp
->cmd
== ATP_CMD_TREQ
)? "TREQ": "unknown",
135 trace_mbufs(D_M_ATP_LOW
, " r", m
);
141 register struct atp_trans
*trp
;
143 register at_ddp_t
*ddp
;
146 * we just got a response, find the trans record
149 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
150 if (trp
->tr_tid
== UAS_VALUE_NTOH(athp
->tid
))
155 * If we can't find one then ignore the message
157 seqno
= athp
->bitmap
;
160 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
161 ("atp_rput: dropping TRESP, no trp,tid=%d,loc=%d,rem=%d.%d,seqno=%d\n",
162 UAS_VALUE_NTOH(athp
->tid
),
163 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
169 * If no longer valid, drop it
171 if (trp
->tr_state
== TRANS_FAILED
) {
173 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
174 ("atp_rput: dropping TRESP, failed trp,tid=%d,loc=%d,rem=%d.%d\n",
175 UAS_VALUE_NTOH(athp
->tid
),
176 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
));
182 * If we have already received it, ignore it
184 if (!(trp
->tr_bitmap
&atp_mask
[seqno
]) || trp
->tr_rcv
[seqno
]) {
186 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
187 ("atp_rput: dropping TRESP, duplicate,tid=%d,loc=%d,rem=%d.%d,seqno=%d\n",
188 UAS_VALUE_NTOH(athp
->tid
),
189 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
195 * Update the received packet bitmap
198 trp
->tr_bitmap
&= atp_lomask
[seqno
];
200 trp
->tr_bitmap
&= ~atp_mask
[seqno
];
203 * Save the message in the trans record
205 trp
->tr_rcv
[seqno
] = m
;
208 * If it isn't the first message then
212 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
215 * If we now have all the responses then return
216 * the message to the user
218 if (trp
->tr_bitmap
== 0) {
221 * Cancel the request timer and any
224 atp_untimout(atp_req_timeout
, trp
);
227 * Send the results back to the user
234 * If they want treq again, send them
236 atp_untimout(atp_req_timeout
, trp
);
244 { register struct atp_rcb
*rcbp
;
245 register at_ddp_t
*ddp
;
248 * Search for a matching transaction
252 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
253 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
254 rcbp
->rc_socket
.node
== ddp
->src_node
&&
255 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
256 rcbp
->rc_socket
.socket
== ddp
->src_socket
) {
258 * Mark the rcb released
260 rcbp
->rc_not_sent_bitmap
= 0;
261 if (rcbp
->rc_state
== RCB_SENDING
)
262 rcbp
->rc_state
= RCB_RELEASED
;
278 { register struct atp_rcb
*rcbp
;
279 register at_ddp_t
*ddp
;
283 * If it is a request message, first
285 * if matches something in our active
290 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
291 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
292 rcbp
->rc_socket
.node
== ddp
->src_node
&&
293 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
294 rcbp
->rc_socket
.socket
== ddp
->src_socket
)
298 * If this is a new req then do
303 * see if it matches something in the
304 * attached request queue
305 * if it does, just release the message
306 * and go on about our buisness
308 /* we just did this, why do again? -jjs 4-10-95 */
309 for (rcbp
= atp
->atp_attached
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
310 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
311 rcbp
->rc_socket
.node
== ddp
->src_node
&&
312 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
313 rcbp
->rc_socket
.socket
== ddp
->src_socket
) {
315 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
316 ("atp_rput: dropping TREQ, matches req queue\n"));
322 * assume someone is interested in
323 * in an asynchronous incoming request
325 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
329 rcbp
->rc_state
= RCB_UNQUEUED
;
331 rcbp
->rc_local_node
= ddp
->dst_node
;
332 temp_net
= NET_VALUE(ddp
->dst_net
);
333 NET_ASSIGN_NOSWAP(rcbp
->rc_local_net
, temp_net
);
334 rcbp
->rc_socket
.socket
= ddp
->src_socket
;
335 rcbp
->rc_socket
.node
= ddp
->src_node
;
336 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->src_net
);
337 rcbp
->rc_tid
= UAS_VALUE_NTOH(athp
->tid
);
338 rcbp
->rc_bitmap
= athp
->bitmap
;
339 rcbp
->rc_not_sent_bitmap
= athp
->bitmap
;
340 rcbp
->rc_xo
= athp
->xo
;
342 * if async then send it as
344 * otherwise, it is a synchronous ioctl so
347 if (atp
->dflag
) { /* for ASP? */
348 if ((m2
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
))) {
349 gbuf_set_type(m2
, MSG_DATA
);
350 gbuf_wset(m2
,sizeof(ioc_t
));
351 ((ioc_t
*)gbuf_rptr(m2
))->ioc_cmd
= AT_ATP_GET_POLL
;
354 } else if ((m2
= gbuf_alloc(1, PRI_HI
))) {
357 atalk_putnext(gref
, m2
);
360 dPrintf(D_M_ATP
,D_L_WARNING
,
361 ("atp_rput: out of buffer for TREQ\n"));
362 timeout(atp_treq_event
, gref
, 10);
367 * move it to the attached list
369 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
370 ("atp_rput: moving to attached list\n"));
371 rcbp
->rc_state
= RCB_PENDING
;
372 ATP_Q_APPEND(atp
->atp_attached
, rcbp
, rc_list
);
374 atp_req_ind(atp
, m_asp
);
378 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
379 ("atp_rput: found match, state:%d\n",
383 * Otherwise we have found a matching request
384 * look for what to do
386 switch (rcbp
->rc_state
) {
388 case RCB_RESPONSE_FULL
:
390 * If it is one we have in progress
391 * (either have all the responses
392 * or are waiting for them)
393 * update the bitmap and resend
396 getmicrouptime(&timenow
);
397 if (rcbp
->rc_timestamp
) {
398 rcbp
->rc_timestamp
= timenow
.tv_sec
;
399 if (rcbp
->rc_timestamp
== 0)
400 rcbp
->rc_timestamp
= 1;
402 rcbp
->rc_bitmap
= athp
->bitmap
;
403 rcbp
->rc_not_sent_bitmap
= athp
->bitmap
;
411 * If we have a release or
412 * we haven't sent any data yet
430 asp_ack_reply(gref
, m
);
432 atalk_putnext(gref
, m
);
437 asp_nak_reply(gref
, m
);
439 atalk_putnext(gref
, m
);
448 atp_x_done_locked(trp
)
452 atp_x_done((struct atp_trans
*)trp
);
459 register struct atp_trans
*trp
;
461 struct atp_state
*atp
;
466 atp_trans_complete(trp
);
469 * If execute once send a release
471 if ((m
= (gbuf_t
*)atp_build_release(trp
)) != NULL
) {
472 AT_DDP_HDR(m
)->src_socket
= ((struct atp_state
*)
473 trp
->tr_queue
)->atp_socket_no
;
476 * Now send back the transaction reply to the process
477 * or notify the process if required
479 atp_trans_complete(trp
);
483 trp
->tr_state
= TRANS_RELEASE
;
484 timeout(atp_x_done_locked
, trp
, 10);
490 atp_trans_complete(trp
)
491 register struct atp_trans
*trp
;
492 { register gbuf_t
*m
;
494 struct atp_state
*atp
;
496 /* we could gbuf_freem(trp->tr_xmt) here if were not planning to
497 re-use the mbuf later */
500 trp
->tr_state
= TRANS_DONE
;
502 if (gbuf_cont(m
) == NULL
) /* issued via the new interface */
503 type
= AT_ATP_ISSUE_REQUEST_NOTE
;
505 type
= ((ioc_t
*)(gbuf_rptr(m
)))->ioc_cmd
;
507 * free any data following the ioctl blk
509 gbuf_freem(gbuf_cont(m
));
512 dPrintf(D_M_ATP_LOW
, D_L_INPUT
, ("atp_trans_comp: trp=0x%x type = %s\n",
514 (type
==AT_ATP_ISSUE_REQUEST
)? "AT_ATP_ISSUE_REQUEST":
515 (type
==AT_ATP_ISSUE_REQUEST_NOTE
)? "AT_ATP_ISSUE_REQUEST_NOTE" :
519 case AT_ATP_ISSUE_REQUEST
:
522 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
523 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= 0;
524 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= trp
->tr_tid
;
525 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
526 gbuf_set_type(m
, MSG_IOCTL
);
529 if (trp
->tr_bdsp
== NULL
) {
531 if (trp
->tr_rsp_wait
)
532 wakeup(&trp
->tr_event
);
534 gbuf_set_type(m
, MSG_IOCACK
);
535 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
536 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= 0;
537 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= 0;
538 atalk_putnext(trp
->tr_queue
->atp_gref
, m
);
543 case AT_ATP_ISSUE_REQUEST_NOTE
:
546 gbuf_set_type(m
, MSG_DATA
);
547 atalk_putnext(trp
->tr_queue
->atp_gref
, m
);
550 } /* atp_trans_complete */