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 static void atp_trans_complete();
53 void atp_x_done_locked(void *);
54 extern void atp_req_timeout();
57 * Decide what to do about received messages
58 * Version 1.7 of atp_read.c on 89/02/09 17:53:16
61 void atp_treq_event(void *arg
)
63 register gref_t
*gref
= (gref_t
*)arg
;
65 register struct atp_state
*atp
;
68 atp
= (struct atp_state
*)gref
->info
;
70 atp
= (struct atp_state
*)atp
->atp_msgq
;
73 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) != NULL
) {
74 gbuf_set_type(m
, MSG_IOCTL
);
75 gbuf_wset(m
,sizeof(ioc_t
));
76 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_GET_POLL
;
80 else if ((m
= gbuf_alloc(1, PRI_HI
)) != NULL
) {
83 atalk_putnext(gref
, m
);
87 timeout(atp_treq_event
, gref
, 10);
91 void atp_rput(gref
, m
)
95 register at_atp_t
*athp
;
96 register struct atp_state
*atp
;
99 struct timeval timenow
;
102 atp
= (struct atp_state
*)gref
->info
;
104 atp
= (struct atp_state
*)atp
->atp_msgq
;
106 switch(gbuf_type(m
)) {
109 * Decode the message, make sure it is an atp
112 if (((AT_DDP_HDR(m
))->type
!= DDP_ATP
) ||
113 (atp
->atp_flags
& ATP_CLOSING
)) {
115 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
116 ("atp_rput: dropping MSG, not atp\n"));
120 athp
= AT_ATP_HDR(m
);
121 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
122 ("atp_rput MSG_DATA: %s (%d)\n",
123 (athp
->cmd
== ATP_CMD_TRESP
)? "TRESP":
124 (athp
->cmd
== ATP_CMD_TREL
)? "TREL":
125 (athp
->cmd
== ATP_CMD_TREQ
)? "TREQ": "unknown",
127 trace_mbufs(D_M_ATP_LOW
, " r", m
);
133 register struct atp_trans
*trp
;
135 register at_ddp_t
*ddp
;
138 * we just got a response, find the trans record
141 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
142 if (trp
->tr_tid
== UAS_VALUE_NTOH(athp
->tid
))
147 * If we can't find one then ignore the message
149 seqno
= athp
->bitmap
;
152 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
153 ("atp_rput: dropping TRESP, no trp,tid=%d,loc=%d,rem=%d.%d,seqno=%d\n",
154 UAS_VALUE_NTOH(athp
->tid
),
155 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
161 * If no longer valid, drop it
163 if (trp
->tr_state
== TRANS_FAILED
) {
165 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
166 ("atp_rput: dropping TRESP, failed trp,tid=%d,loc=%d,rem=%d.%d\n",
167 UAS_VALUE_NTOH(athp
->tid
),
168 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
));
174 * If we have already received it, ignore it
176 if (!(trp
->tr_bitmap
&atp_mask
[seqno
]) || trp
->tr_rcv
[seqno
]) {
178 dPrintf(D_M_ATP_LOW
, (D_L_INPUT
|D_L_ERROR
),
179 ("atp_rput: dropping TRESP, duplicate,tid=%d,loc=%d,rem=%d.%d,seqno=%d\n",
180 UAS_VALUE_NTOH(athp
->tid
),
181 ddp
->dst_socket
, ddp
->src_node
, ddp
->src_socket
, seqno
));
187 * Update the received packet bitmap
190 trp
->tr_bitmap
&= atp_lomask
[seqno
];
192 trp
->tr_bitmap
&= ~atp_mask
[seqno
];
195 * Save the message in the trans record
197 trp
->tr_rcv
[seqno
] = m
;
200 * If it isn't the first message then
204 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
207 * If we now have all the responses then return
208 * the message to the user
210 if (trp
->tr_bitmap
== 0) {
213 * Cancel the request timer and any
216 atp_untimout(atp_req_timeout
, trp
);
219 * Send the results back to the user
226 * If they want treq again, send them
228 atp_untimout(atp_req_timeout
, trp
);
236 { register struct atp_rcb
*rcbp
;
237 register at_ddp_t
*ddp
;
240 * Search for a matching transaction
244 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
245 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
246 rcbp
->rc_socket
.node
== ddp
->src_node
&&
247 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
248 rcbp
->rc_socket
.socket
== ddp
->src_socket
) {
250 * Mark the rcb released
252 rcbp
->rc_not_sent_bitmap
= 0;
253 if (rcbp
->rc_state
== RCB_SENDING
)
254 rcbp
->rc_state
= RCB_RELEASED
;
270 { register struct atp_rcb
*rcbp
;
271 register at_ddp_t
*ddp
;
275 * If it is a request message, first
277 * if matches something in our active
282 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
283 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
284 rcbp
->rc_socket
.node
== ddp
->src_node
&&
285 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
286 rcbp
->rc_socket
.socket
== ddp
->src_socket
)
290 * If this is a new req then do
295 * see if it matches something in the
296 * attached request queue
297 * if it does, just release the message
298 * and go on about our buisness
300 /* we just did this, why do again? -jjs 4-10-95 */
301 for (rcbp
= atp
->atp_attached
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
302 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
303 rcbp
->rc_socket
.node
== ddp
->src_node
&&
304 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
) &&
305 rcbp
->rc_socket
.socket
== ddp
->src_socket
) {
307 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
308 ("atp_rput: dropping TREQ, matches req queue\n"));
314 * assume someone is interested in
315 * in an asynchronous incoming request
317 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
321 rcbp
->rc_state
= RCB_UNQUEUED
;
323 rcbp
->rc_local_node
= ddp
->dst_node
;
324 temp_net
= NET_VALUE(ddp
->dst_net
);
325 NET_ASSIGN_NOSWAP(rcbp
->rc_local_net
, temp_net
);
326 rcbp
->rc_socket
.socket
= ddp
->src_socket
;
327 rcbp
->rc_socket
.node
= ddp
->src_node
;
328 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->src_net
);
329 rcbp
->rc_tid
= UAS_VALUE_NTOH(athp
->tid
);
330 rcbp
->rc_bitmap
= athp
->bitmap
;
331 rcbp
->rc_not_sent_bitmap
= athp
->bitmap
;
332 rcbp
->rc_xo
= athp
->xo
;
334 * if async then send it as
336 * otherwise, it is a synchronous ioctl so
339 if (atp
->dflag
) { /* for ASP? */
340 if ((m2
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
))) {
341 gbuf_set_type(m2
, MSG_DATA
);
342 gbuf_wset(m2
,sizeof(ioc_t
));
343 ((ioc_t
*)gbuf_rptr(m2
))->ioc_cmd
= AT_ATP_GET_POLL
;
346 } else if ((m2
= gbuf_alloc(1, PRI_HI
))) {
349 atalk_putnext(gref
, m2
);
352 dPrintf(D_M_ATP
,D_L_WARNING
,
353 ("atp_rput: out of buffer for TREQ\n"));
354 timeout(atp_treq_event
, gref
, 10);
359 * move it to the attached list
361 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
362 ("atp_rput: moving to attached list\n"));
363 rcbp
->rc_state
= RCB_PENDING
;
364 ATP_Q_APPEND(atp
->atp_attached
, rcbp
, rc_list
);
366 atp_req_ind(atp
, m_asp
);
370 dPrintf(D_M_ATP_LOW
, D_L_INPUT
,
371 ("atp_rput: found match, state:%d\n",
375 * Otherwise we have found a matching request
376 * look for what to do
378 switch (rcbp
->rc_state
) {
380 case RCB_RESPONSE_FULL
:
382 * If it is one we have in progress
383 * (either have all the responses
384 * or are waiting for them)
385 * update the bitmap and resend
388 getmicrouptime(&timenow
);
389 if (rcbp
->rc_timestamp
) {
390 rcbp
->rc_timestamp
= timenow
.tv_sec
;
391 if (rcbp
->rc_timestamp
== 0)
392 rcbp
->rc_timestamp
= 1;
394 rcbp
->rc_bitmap
= athp
->bitmap
;
395 rcbp
->rc_not_sent_bitmap
= athp
->bitmap
;
403 * If we have a release or
404 * we haven't sent any data yet
422 asp_ack_reply(gref
, m
);
424 atalk_putnext(gref
, m
);
429 asp_nak_reply(gref
, m
);
431 atalk_putnext(gref
, m
);
440 atp_x_done_locked(trp
)
444 atp_x_done((struct atp_trans
*)trp
);
451 register struct atp_trans
*trp
;
453 struct atp_state
*atp
;
458 atp_trans_complete(trp
);
461 * If execute once send a release
463 if ((m
= (gbuf_t
*)atp_build_release(trp
)) != NULL
) {
464 AT_DDP_HDR(m
)->src_socket
= ((struct atp_state
*)
465 trp
->tr_queue
)->atp_socket_no
;
468 * Now send back the transaction reply to the process
469 * or notify the process if required
471 atp_trans_complete(trp
);
475 trp
->tr_state
= TRANS_RELEASE
;
476 timeout(atp_x_done_locked
, trp
, 10);
482 atp_trans_complete(trp
)
483 register struct atp_trans
*trp
;
484 { register gbuf_t
*m
;
486 struct atp_state
*atp
;
488 /* we could gbuf_freem(trp->tr_xmt) here if were not planning to
489 re-use the mbuf later */
492 trp
->tr_state
= TRANS_DONE
;
494 if (gbuf_cont(m
) == NULL
) /* issued via the new interface */
495 type
= AT_ATP_ISSUE_REQUEST_NOTE
;
497 type
= ((ioc_t
*)(gbuf_rptr(m
)))->ioc_cmd
;
499 * free any data following the ioctl blk
501 gbuf_freem(gbuf_cont(m
));
504 dPrintf(D_M_ATP_LOW
, D_L_INPUT
, ("atp_trans_comp: trp=0x%x type = %s\n",
506 (type
==AT_ATP_ISSUE_REQUEST
)? "AT_ATP_ISSUE_REQUEST":
507 (type
==AT_ATP_ISSUE_REQUEST_NOTE
)? "AT_ATP_ISSUE_REQUEST_NOTE" :
511 case AT_ATP_ISSUE_REQUEST
:
514 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
515 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= 0;
516 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= trp
->tr_tid
;
517 ((ioc_t
*)gbuf_rptr(m
))->ioc_cmd
= AT_ATP_REQUEST_COMPLETE
;
518 gbuf_set_type(m
, MSG_IOCTL
);
521 if (trp
->tr_bdsp
== NULL
) {
523 if (trp
->tr_rsp_wait
)
524 wakeup(&trp
->tr_event
);
526 gbuf_set_type(m
, MSG_IOCACK
);
527 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
528 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= 0;
529 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= 0;
530 atalk_putnext(trp
->tr_queue
->atp_gref
, m
);
535 case AT_ATP_ISSUE_REQUEST_NOTE
:
538 gbuf_set_type(m
, MSG_DATA
);
539 atalk_putnext(trp
->tr_queue
->atp_gref
, m
);
542 } /* atp_trans_complete */