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 __private_extern__
int atp_resp_seqno2big
= 0;
63 static void atp_trans_complete(struct atp_trans
*);
64 void atp_x_done_locked(void *);
65 void atp_treq_event(void *);
68 * Decide what to do about received messages
69 * Version 1.7 of atp_read.c on 89/02/09 17:53:16
72 void atp_treq_event(void *arg
)
74 register gref_t
*gref
= (gref_t
*)arg
;
76 register struct atp_state
*atp
;
79 atp
= (struct atp_state
*)gref
->info
;
81 atp
= (struct atp_state
*)atp
->atp_msgq
;
84 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) != NULL
) {
85 gbuf_set_type(m
, MSG_IOCTL
);
86 gbuf_wset(m
,sizeof(ioc_t
));
87 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_GET_POLL
;
91 else if ((m
= gbuf_alloc(1, PRI_HI
)) != NULL
) {
94 atalk_putnext(gref
, m
);
98 timeout(atp_treq_event
, gref
, 10);
102 void atp_rput(gref
, m
)
106 register at_atp_t
*athp
;
107 register struct atp_state
*atp
;
108 gbuf_t
*m_asp
= NULL
;
109 struct timeval timenow
;
112 atp
= (struct atp_state
*)gref
->info
;
114 atp
= (struct atp_state
*)atp
->atp_msgq
;
116 switch(gbuf_type(m
)) {
119 * Decode the message, make sure it is an atp
122 if (((AT_DDP_HDR(m
))->type
!= DDP_ATP
) ||
123 (atp
->atp_flags
& ATP_CLOSING
)) {
125 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
126 ("atp_rput: dropping MSG, not atp\n"));
130 athp
= AT_ATP_HDR(m
);
131 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
132 ("atp_rput MSG_DATA: %s (%d)\n",
133 (athp
->cmd
== ATP_CMD_TRESP
)? "TRESP":
134 (athp
->cmd
== ATP_CMD_TREL
)? "TREL":
135 (athp
->cmd
== ATP_CMD_TREQ
)? "TREQ": "unknown",
137 trace_mbufs(D_M_ATP_LOW
, " r", m
);
143 register struct atp_trans
*trp
;
144 register unsigned int seqno
;
145 register at_ddp_t
*ddp
;
148 * we just got a response, find the trans record
151 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
152 if (trp
->tr_tid
== UAS_VALUE_NTOH(athp
->tid
))
157 * If we can't find one then ignore the message
159 seqno
= athp
->bitmap
;
161 atp_resp_seqno2big
++;
163 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
164 ("atp_rput: dropping TRESP seqno too big, tid=%d,loc=%d,rem=%d.%d,seqno=%u\n",
165 UAS_VALUE_NTOH(athp
->tid
),
166 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
172 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
173 ("atp_rput: dropping TRESP, no trp,tid=%d,loc=%d,rem=%d.%d,seqno=%u\n",
174 UAS_VALUE_NTOH(athp
->tid
),
175 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
181 * If no longer valid, drop it
183 if (trp
->tr_state
== TRANS_FAILED
) {
185 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
186 ("atp_rput: dropping TRESP, failed trp,tid=%d,loc=%d,rem=%d.%d\n",
187 UAS_VALUE_NTOH(athp
->tid
),
188 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
));
194 * If we have already received it, ignore it
196 if (!(trp
->tr_bitmap
&atp_mask
[seqno
]) || trp
->tr_rcv
[seqno
]) {
198 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
199 ("atp_rput: dropping TRESP, duplicate,tid=%d,loc=%d,rem=%d.%d,seqno=%u\n",
200 UAS_VALUE_NTOH(athp
->tid
),
201 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
207 * Update the received packet bitmap
210 trp
->tr_bitmap
&= atp_lomask
[seqno
];
212 trp
->tr_bitmap
&= ~atp_mask
[seqno
];
215 * Save the message in the trans record
217 trp
->tr_rcv
[seqno
] = m
;
220 * If it isn't the first message then
224 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
227 * If we now have all the responses then return
228 * the message to the user
230 if (trp
->tr_bitmap
== 0) {
233 * Cancel the request timer and any
236 atp_untimout(atp_req_timeout
, trp
);
239 * Send the results back to the user
246 * If they want treq again, send them
248 atp_untimout(atp_req_timeout
, trp
);
256 { register struct atp_rcb
*rcbp
;
257 register at_ddp_t
*ddp
;
260 * Search for a matching transaction
264 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
265 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
266 rcbp
->rc_socket
.node
== ddp
->src_node
&&
267 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
268 rcbp
->rc_socket
.socket
== ddp
->src_socket
) {
270 * Mark the rcb released
272 rcbp
->rc_not_sent_bitmap
= 0;
273 if (rcbp
->rc_state
== RCB_SENDING
)
274 rcbp
->rc_state
= RCB_RELEASED
;
290 { register struct atp_rcb
*rcbp
;
291 register at_ddp_t
*ddp
;
295 * If it is a request message, first
297 * if matches something in our active
302 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
303 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
304 rcbp
->rc_socket
.node
== ddp
->src_node
&&
305 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
306 rcbp
->rc_socket
.socket
== ddp
->src_socket
)
310 * If this is a new req then do
315 * see if it matches something in the
316 * attached request queue
317 * if it does, just release the message
318 * and go on about our buisness
320 /* we just did this, why do again? -jjs 4-10-95 */
321 for (rcbp
= atp
->atp_attached
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
322 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
323 rcbp
->rc_socket
.node
== ddp
->src_node
&&
324 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
325 rcbp
->rc_socket
.socket
== ddp
->src_socket
) {
327 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
328 ("atp_rput: dropping TREQ, matches req queue\n"));
334 * assume someone is interested in
335 * in an asynchronous incoming request
337 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
341 rcbp
->rc_state
= RCB_UNQUEUED
;
343 rcbp
->rc_local_node
= ddp
->dst_node
;
344 temp_net
= NET_VALUE(ddp
->dst_net
);
345 NET_ASSIGN_NOSWAP(rcbp
->rc_local_net
, temp_net
);
346 rcbp
->rc_socket
.socket
= ddp
->src_socket
;
347 rcbp
->rc_socket
.node
= ddp
->src_node
;
348 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->src_net
);
349 rcbp
->rc_tid
= UAS_VALUE_NTOH(athp
->tid
);
350 rcbp
->rc_bitmap
= athp
->bitmap
;
351 rcbp
->rc_not_sent_bitmap
= athp
->bitmap
;
352 rcbp
->rc_xo
= athp
->xo
;
354 * if async then send it as
356 * otherwise, it is a synchronous ioctl so
359 if (atp
->dflag
) { /* for ASP? */
360 if ((m2
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
))) {
361 gbuf_set_type(m2
, MSG_DATA
);
362 gbuf_wset(m2
,sizeof(ioc_t
));
363 ((ioc_t
*)gbuf_rptr(m2
))->ioc_cmd
= AT_ATP_GET_POLL
;
366 } else if ((m2
= gbuf_alloc(1, PRI_HI
))) {
369 atalk_putnext(gref
, m2
);
372 dPrintf(D_M_ATP
,D_L_WARNING
,
373 ("atp_rput: out of buffer for TREQ\n"));
374 timeout(atp_treq_event
, gref
, 10);
379 * move it to the attached list
381 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
382 ("atp_rput: moving to attached list\n"));
383 rcbp
->rc_state
= RCB_PENDING
;
384 ATP_Q_APPEND(atp
->atp_attached
, rcbp
, rc_list
);
386 atp_req_ind(atp
, m_asp
);
390 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
391 ("atp_rput: found match, state:%d\n",
395 * Otherwise we have found a matching request
396 * look for what to do
398 switch (rcbp
->rc_state
) {
400 case RCB_RESPONSE_FULL
:
402 * If it is one we have in progress
403 * (either have all the responses
404 * or are waiting for them)
405 * update the bitmap and resend
408 getmicrouptime(&timenow
);
409 if (rcbp
->rc_timestamp
) {
410 rcbp
->rc_timestamp
= timenow
.tv_sec
;
411 if (rcbp
->rc_timestamp
== 0)
412 rcbp
->rc_timestamp
= 1;
414 rcbp
->rc_bitmap
= athp
->bitmap
;
415 rcbp
->rc_not_sent_bitmap
= athp
->bitmap
;
423 * If we have a release or
424 * we haven't sent any data yet
442 asp_ack_reply(gref
, m
);
444 atalk_putnext(gref
, m
);
449 asp_nak_reply(gref
, m
);
451 atalk_putnext(gref
, m
);
460 atp_x_done_locked(trp
)
464 atp_x_done((struct atp_trans
*)trp
);
471 register struct atp_trans
*trp
;
473 struct atp_state
*atp
;
478 atp_trans_complete(trp
);
481 * If execute once send a release
483 if ((m
= (gbuf_t
*)atp_build_release(trp
)) != NULL
) {
484 AT_DDP_HDR(m
)->src_socket
= ((struct atp_state
*)
485 trp
->tr_queue
)->atp_socket_no
;
488 * Now send back the transaction reply to the process
489 * or notify the process if required
491 atp_trans_complete(trp
);
495 trp
->tr_state
= TRANS_RELEASE
;
496 timeout(atp_x_done_locked
, trp
, 10);
502 atp_trans_complete(trp
)
503 register struct atp_trans
*trp
;
504 { register gbuf_t
*m
;
506 struct atp_state
*atp
;
508 /* we could gbuf_freem(trp->tr_xmt) here if were not planning to
509 re-use the mbuf later */
512 trp
->tr_state
= TRANS_DONE
;
514 if (gbuf_cont(m
) == NULL
) /* issued via the new interface */
515 type
= AT_ATP_ISSUE_REQUEST_NOTE
;
517 type
= ((ioc_t
*)(gbuf_rptr(m
)))->ioc_cmd
;
519 * free any data following the ioctl blk
521 gbuf_freem(gbuf_cont(m
));
524 dPrintf(D_M_ATP_LOW
, D_L_INPUT
, ("atp_trans_comp: trp=0x%x type = %s\n",
526 (type
==AT_ATP_ISSUE_REQUEST
)? "AT_ATP_ISSUE_REQUEST":
527 (type
==AT_ATP_ISSUE_REQUEST_NOTE
)? "AT_ATP_ISSUE_REQUEST_NOTE" :
531 case AT_ATP_ISSUE_REQUEST
:
534 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
535 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= 0;
536 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= trp
->tr_tid
;
537 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
538 gbuf_set_type(m
, MSG_IOCTL
);
541 if (trp
->tr_bdsp
== NULL
) {
543 if (trp
->tr_rsp_wait
)
544 wakeup(&trp
->tr_event
);
546 gbuf_set_type(m
, MSG_IOCACK
);
547 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
548 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= 0;
549 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= 0;
550 atalk_putnext(trp
->tr_queue
->atp_gref
, m
);
555 case AT_ATP_ISSUE_REQUEST_NOTE
:
558 gbuf_set_type(m
, MSG_DATA
);
559 atalk_putnext(trp
->tr_queue
->atp_gref
, m
);
562 } /* atp_trans_complete */