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.
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>
47 #include <kern/locks.h>
49 #include <sys/ioctl.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
56 #include <netat/sysglue.h>
57 #include <netat/appletalk.h>
58 #include <netat/ddp.h>
59 #include <netat/at_pcb.h>
60 #include <netat/atp.h>
61 #include <netat/at_var.h>
62 #include <netat/asp.h>
63 #include <netat/at_pat.h>
64 #include <netat/debug.h>
66 static int loop_cnt
; /* for debugging loops */
67 #define CHK_LOOP(str) { \
68 if (loop_cnt++ > 100) { \
74 static int atp_pack_bdsp(struct atp_trans
*, struct atpBDS
*);
75 static int atp_unpack_bdsp(struct atp_state
*, gbuf_t
*, struct atp_rcb
*,
77 void atp_trp_clock(), asp_clock(), asp_clock_locked(), atp_trp_clock_locked();;
79 extern struct atp_rcb_qhead atp_need_rel
;
80 extern int atp_inited
;
81 extern struct atp_state
*atp_used_list
;
82 extern asp_scb_t
*scb_free_list
;
84 extern gbuf_t
*scb_resource_m
;
85 extern gbuf_t
*atp_resource_m
;
86 extern gref_t
*atp_inputQ
[];
87 extern int atp_pidM
[];
88 extern at_ifaddr_t
*ifID_home
;
89 extern lck_mtx_t
* atalk_mutex
;
91 static struct atp_trans
*trp_tmo_list
;
92 struct atp_trans
*trp_tmo_rcb
;
94 /* first bds entry gives number of bds entries in total (hack) */
95 #define get_bds_entries(m) \
96 ((gbuf_len(m) > TOTAL_ATP_HDR_SIZE)? \
97 (UAS_VALUE(((struct atpBDS *)(AT_ATP_HDR(m)->data))->bdsDataSz)): 0)
99 #define atpBDSsize (sizeof(struct atpBDS)*ATP_TRESP_MAX)
104 trp_tmo_rcb
= atp_trans_alloc(0);
105 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
106 atp_trp_clock((void *)&atp_inited
);
107 asp_clock((void *)&atp_inited
);
112 untimeout(asp_clock_locked
, (void *)&atp_inited
);
113 untimeout(atp_trp_clock_locked
, (void *)&atp_inited
);
114 atp_untimout(atp_rcb_timer
, trp_tmo_rcb
);
118 /* allocated in asp_scb_alloc(), which is called
120 if (scb_resource_m
) {
121 gbuf_freem(scb_resource_m
);
125 /* allocated in atp_trans_alloc() */
126 if (atp_resource_m
) {
127 gbuf_freem(atp_resource_m
);
129 atp_trans_free_list
= 0;
135 * write queue put routine .... filter out other than IOCTLs
136 * Version 1.8 of atp_write.c on 89/02/09 17:53:26
141 register gref_t
*gref
;
144 register ioc_t
*iocbp
;
146 struct atp_state
*atp
;
147 struct atp_trans
*trp
;
148 struct atp_rcb
*rcbp
;
151 atp
= (struct atp_state
*)gref
->info
;
153 atp
= (struct atp_state
*)atp
->atp_msgq
;
155 switch(gbuf_type(m
)) {
159 dPrintf(D_M_ATP
, D_L_WARNING
,
160 ("atp_wput: atp_msgq discarded\n"));
166 /* Need to ensure that all copyin/copyout calls are made at
167 * put routine time which should be in the user context. (true when
168 * we are the stream head). The service routine can be called on an
169 * unpredictable context and copyin/copyout calls will get wrong results
170 * or even panic the kernel.
172 iocbp
= (ioc_t
*)gbuf_rptr(m
);
174 switch (iocbp
->ioc_cmd
) {
175 case AT_ATP_BIND_REQ
:
176 if (gbuf_cont(m
) == NULL
) {
177 iocbp
->ioc_rval
= -1;
178 atp_iocnak(atp
, m
, EINVAL
);
181 skt
= *(at_socket
*)gbuf_rptr(gbuf_cont(m
));
182 if ((skt
= (at_socket
)atp_bind(gref
, (unsigned int)skt
, 0)) == 0)
183 atp_iocnak(atp
, m
, EINVAL
);
185 *(at_socket
*)gbuf_rptr(gbuf_cont(m
)) = skt
;
188 atp_dequeue_atp(atp
);
192 case AT_ATP_GET_CHANID
:
193 if (gbuf_cont(m
) == NULL
) {
194 iocbp
->ioc_rval
= -1;
195 atp_iocnak(atp
, m
, EINVAL
);
198 *(gref_t
**)gbuf_rptr(gbuf_cont(m
)) = gref
;
202 /* not the close and not the tickle(?) */
203 case AT_ATP_ISSUE_REQUEST_DEF
:
204 case AT_ATP_ISSUE_REQUEST_DEF_NOTE
: {
205 gbuf_t
*bds
, *tmp
, *m2
;
206 struct atp_rcb
*rcbp
;
210 if ((tmp
= gbuf_cont(m
)) != 0) {
211 if ((bds
= gbuf_dupb(tmp
)) == NULL
) {
212 atp_iocnak(atp
, m
, ENOBUFS
);
215 gbuf_rinc(tmp
,atpBDSsize
);
216 gbuf_wset(bds
,atpBDSsize
);
217 iocbp
->ioc_count
-= atpBDSsize
;
218 gbuf_cont(tmp
) = bds
;
222 * send a response to a transaction
225 if (iocbp
->ioc_count
< TOTAL_ATP_HDR_SIZE
) {
226 atp_iocnak(atp
, m
, EINVAL
);
231 * remove the response from the message
235 iocbp
->ioc_count
= 0;
236 ddp
= AT_DDP_HDR(m2
);
237 athp
= AT_ATP_HDR(m2
);
239 gbuf_cont(m2
) = atp
->atp_msgq
;
244 * search for the corresponding rcb
246 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
247 if (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
) &&
248 rcbp
->rc_socket
.node
== ddp
->dst_node
&&
249 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->dst_net
) &&
250 rcbp
->rc_socket
.socket
== ddp
->dst_socket
)
255 * If it has already been sent then return an error
257 if ((rcbp
&& rcbp
->rc_state
!= RCB_NOTIFIED
) ||
258 (rcbp
== NULL
&& athp
->xo
)) {
259 atp_iocnak(atp
, m
, ENOENT
);
263 if (rcbp
== NULL
) { /* a response for an ALO transaction */
264 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
265 atp_iocnak(atp
, m
, ENOBUFS
);
270 rcbp
->rc_socket
.socket
= ddp
->dst_socket
;
271 rcbp
->rc_socket
.node
= ddp
->dst_node
;
272 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->dst_net
);
273 rcbp
->rc_tid
= UAS_VALUE_NTOH(athp
->tid
);
274 rcbp
->rc_bitmap
= 0xff;
276 rcbp
->rc_state
= RCB_SENDING
;
277 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
279 xcnt
= get_bds_entries(m2
);
280 if ((i
= atp_unpack_bdsp(atp
, m2
, rcbp
, xcnt
, FALSE
))) {
283 atp_iocnak(atp
, m
, i
);
286 atp_send_replies(atp
, rcbp
);
289 * send the ack back to the responder
295 case AT_ATP_GET_POLL
: {
297 gbuf_freem(gbuf_cont(m
));
299 iocbp
->ioc_count
= 0;
303 * search for a waiting request
305 if ((rcbp
= atp
->atp_attached
.head
)) {
307 * Got one, move it to the active response Q
309 gbuf_cont(m
) = rcbp
->rc_ioctl
;
310 rcbp
->rc_ioctl
= NULL
;
312 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
313 rcbp
->rc_state
= RCB_NOTIFIED
;
314 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
316 /* detach rcbp from attached queue,
317 * and free any outstanding resources
324 * None available - can out
326 atp_iocnak(atp
, m
, EAGAIN
);
331 case AT_ATP_CANCEL_REQUEST
: {
333 * Cancel a pending request
335 if (iocbp
->ioc_count
!= sizeof(int)) {
336 atp_iocnak(atp
, m
, EINVAL
);
339 i
= *(int *)gbuf_rptr(gbuf_cont(m
));
340 gbuf_freem(gbuf_cont(m
));
342 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
343 if (trp
->tr_tid
== i
)
347 atp_iocnak(atp
, m
, ENOENT
);
357 if (atalk_peek(gref
, &event
) == -1)
358 atp_iocnak(atp
, m
, EAGAIN
);
360 *gbuf_rptr(gbuf_cont(m
)) = event
;
366 case DDP_IOC_GET_CFG
:
367 #ifdef APPLETALK_DEBUG
368 kprintf("atp_wput: DDP_IOC_GET_CFG\n");
370 if (gbuf_cont(m
) == 0) {
371 atp_iocnak(atp
, m
, EINVAL
);
375 /* *** was ddp_get_cfg() *** */
377 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(m
));
378 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
379 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
380 cfgp
->inet
.socket
= atp
->atp_socket_no
;
381 cfgp
->ddptype
= DDP_ATP
;
383 cfgp
->inet
.net
= atp
->atp_gref
->laddr
.s_net
;
384 cfgp
->inet
.node
= atp
->atp_gref
->laddr
.s_node
;
385 cfgp
->inet
.socket
= atp
->atp_gref
->lport
;
386 cfgp
->ddptype
= atp
->atp_gref
->ddptype
;
389 gbuf_wset(gbuf_cont(m
), sizeof(ddp_addr_t
));
395 * Otherwise pass it on, if possible
397 iocbp
->ioc_private
= (void *)gref
;
409 gbuf_t
*atp_build_release(trp
)
410 register struct atp_trans
*trp
;
413 register at_ddp_t
*ddp
;
414 register at_atp_t
*athp
;
417 * Now try and allocate enough space to send the message
418 * if none is available the caller will schedule
419 * a timeout so we can retry for more space soon
421 if ((m
= (gbuf_t
*)gbuf_alloc(AT_WR_OFFSET
+ATP_HDR_SIZE
, PRI_HI
)) != NULL
) {
422 gbuf_rinc(m
,AT_WR_OFFSET
);
423 gbuf_wset(m
,TOTAL_ATP_HDR_SIZE
);
426 UAS_ASSIGN_HTON(ddp
->checksum
, 0);
427 ddp
->dst_socket
= trp
->tr_socket
.socket
;
428 ddp
->dst_node
= trp
->tr_socket
.node
;
429 NET_ASSIGN(ddp
->dst_net
, trp
->tr_socket
.net
);
430 ddp
->src_node
= trp
->tr_local_node
;
431 NET_NET(ddp
->src_net
, trp
->tr_local_net
);
434 * clear the cmd/xo/eom/sts/unused fields
436 athp
= AT_ATP_HDR(m
);
437 ATP_CLEAR_CONTROL(athp
);
438 athp
->cmd
= ATP_CMD_TREL
;
439 UAS_ASSIGN_HTON(athp
->tid
, trp
->tr_tid
);
445 void atp_send_replies(atp
, rcbp
)
446 register struct atp_state
*atp
;
447 register struct atp_rcb
*rcbp
;
448 { register gbuf_t
*m
;
450 int s_gen
, cnt
, err
, offset
, space
;
451 unsigned char *m0_rptr
= NULL
, *m0_wptr
= NULL
;
452 register at_atp_t
*athp
;
453 register struct atpBDS
*bdsp
;
454 register gbuf_t
*m2
, *m1
, *m0
, *mhdr
;
456 gbuf_t
*mprev
, *mlist
= 0;
457 at_socket src_socket
= (at_socket
)atp
->atp_socket_no
;
458 gbuf_t
*rc_xmt
[ATP_TRESP_MAX
];
460 char ddp_atp_hdr
[TOTAL_ATP_HDR_SIZE
];
462 struct timeval timenow
;
464 if (rcbp
->rc_queue
!= atp
)
466 if (rcbp
->rc_not_sent_bitmap
== 0)
467 goto nothing_to_send
;
469 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send_replies\n"));
471 * Do this for each message that hasn't been sent
473 cnt
= rcbp
->rc_pktcnt
;
474 for (i
= 0; i
< cnt
; i
++) {
476 if (rcbp
->rc_snd
[i
]) {
478 gbuf_alloc(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
,PRI_MED
))
480 for (cnt
= 0; cnt
< i
; cnt
++)
482 gbuf_freeb(rc_xmt
[cnt
]);
483 goto nothing_to_send
;
490 if (gbuf_len(m
) > TOTAL_ATP_HDR_SIZE
)
491 bdsp
= (struct atpBDS
*)(AT_ATP_HDR(m
)->data
);
496 space
= gbuf_msgsize(m0
);
499 for (i
= 0; i
< cnt
; i
++) {
500 if (rcbp
->rc_snd
[i
] == 0) {
501 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
))) {
507 /* setup header fields */
508 gbuf_rinc(mhdr
,AT_WR_OFFSET
);
509 gbuf_wset(mhdr
,TOTAL_ATP_HDR_SIZE
);
510 *(struct ddp_atp
*)(gbuf_rptr(mhdr
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
511 athp
= AT_ATP_HDR(mhdr
);
512 ATP_CLEAR_CONTROL(athp
);
513 athp
->cmd
= ATP_CMD_TRESP
;
516 athp
->eom
= 1; /* for the last fragment */
518 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
519 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
)) && m0
!= 0 && space
> 0) {
520 if ((m1
= m_copym(m0
, offset
, len
, M_DONTWAIT
)) == 0) {
521 for (i
= 0; i
< cnt
; i
++)
523 gbuf_freem(rc_xmt
[i
]);
524 goto nothing_to_send
;
528 gbuf_cont(mhdr
) = m1
;
532 AT_DDP_HDR(mhdr
)->src_socket
= src_socket
;
533 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
,
534 ("atp_send_replies: %d, socket=%d, size=%d\n",
535 i
, atp
->atp_socket_no
, gbuf_msgsize(gbuf_cont(m2
))));
538 gbuf_next(mprev
) = mhdr
;
544 rcbp
->rc_not_sent_bitmap
&= ~atp_mask
[i
];
545 if (rcbp
->rc_not_sent_bitmap
== 0)
549 * on to the next frag
559 * If all replies from this reply block have been sent then
560 * remove it from the queue and mark it so
562 if (rcbp
->rc_queue
!= atp
)
564 rcbp
->rc_rep_waiting
= 0;
567 * If we are doing execute once re-set the rcb timeout
568 * each time we send back any part of the response. Note
569 * that this timer is started when an initial request is
570 * received. Each response reprimes the timer. Duplicate
571 * requests do not reprime the timer.
573 * We have sent all of a response so free the
576 if (rcbp
->rc_xo
&& rcbp
->rc_state
!= RCB_RELEASED
) {
577 getmicrouptime(&timenow
);
578 if (rcbp
->rc_timestamp
== 0) {
579 rcbp
->rc_timestamp
= timenow
.tv_sec
;
580 if (rcbp
->rc_timestamp
== 0)
581 rcbp
->rc_timestamp
= 1;
582 ATP_Q_APPEND(atp_need_rel
, rcbp
, rc_tlist
);
584 rcbp
->rc_state
= RCB_RESPONSE_FULL
;
587 } /* atp_send_replies */
591 atp_pack_bdsp(trp
, bdsp
)
592 register struct atp_trans
*trp
;
593 register struct atpBDS
*bdsp
;
595 register gbuf_t
*m
= NULL
;
596 register int i
, datsize
= 0;
597 struct atpBDS
*bdsbase
= bdsp
;
600 dPrintf(D_M_ATP
, D_L_INFO
, ("atp_pack_bdsp: socket=%d\n",
601 trp
->tr_queue
->atp_socket_no
));
603 for (i
= 0; i
< ATP_TRESP_MAX
; i
++, bdsp
++) {
604 unsigned short bufsize
= UAS_VALUE(bdsp
->bdsBuffSz
);
605 long bufaddr
= UAL_VALUE(bdsp
->bdsBuffAddr
);
607 if ((m
= trp
->tr_rcv
[i
]) == NULL
)
610 /* discard ddp hdr on first packet */
612 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
614 /* this field may contain control information even when
615 no data is present */
616 UAL_UAL(bdsp
->bdsUserData
,
617 (((at_atp_t
*)(gbuf_rptr(m
)))->user_bytes
));
618 gbuf_rinc(m
, ATP_HDR_SIZE
);
620 if ((bufsize
!= 0) && (bufaddr
!= 0)) {
621 /* user expects data back */
623 register char *buf
= (char *)bufaddr
;
626 unsigned short len
= (unsigned short)(gbuf_len(m
));
630 if ((error
= copyout((caddr_t
)gbuf_rptr(m
),
631 CAST_USER_ADDR_T(&buf
[tmp
]),
641 UAS_ASSIGN(bdsp
->bdsDataSz
, tmp
);
644 gbuf_freem(trp
->tr_rcv
[i
]);
645 trp
->tr_rcv
[i
] = NULL
;
648 /* report the number of packets */
649 UAS_ASSIGN(((struct atpBDS
*)bdsbase
)->bdsBuffSz
, i
);
651 dPrintf(D_M_ATP
, D_L_INFO
, (" : size=%d\n",
655 } /* atp_pack_bdsp */
658 /* create an mbuf chain with mbuf packet headers for each ATP response packet
659 * to be sent. m contains the DDP hdr, ATP hdr, and and array of atpBDS structs.
660 * chained to m is an mbuf that contians the actual data pointed to by the atpBDS
664 atp_unpack_bdsp(atp
, m
, rcbp
, cnt
, wait
)
665 struct atp_state
*atp
;
666 gbuf_t
*m
; /* ddp, atp and bdsp gbuf_t */
667 register struct atp_rcb
*rcbp
;
668 register int cnt
, wait
;
670 register struct atpBDS
*bdsp
;
671 register gbuf_t
*m2
, *m1
, *m0
, *mhdr
;
675 at_socket src_socket
;
678 char ddp_atp_hdr
[TOTAL_ATP_HDR_SIZE
];
680 gbuf_t
*mprev
, *mlist
= 0;
681 gbuf_t
*rc_xmt
[ATP_TRESP_MAX
];
682 unsigned char *m0_rptr
, *m0_wptr
;
683 int err
, offset
, space
;
684 struct timeval timenow
;
687 * get the user data structure pointer
689 bdsp
= (struct atpBDS
*)(AT_ATP_HDR(m
)->data
);
692 * Guard against bogus count argument.
694 if ((unsigned) cnt
> ATP_TRESP_MAX
) {
695 dPrintf(D_M_ATP
, D_L_ERROR
,
696 ("atp_unpack_bdsp: bad bds count 0x%x\n", cnt
));
700 if ((src_socket
= (at_socket
)atp
->atp_socket_no
) == 0xFF) {
701 /* comparison was to -1, however src_socket is a u_char */
708 rcbp
->rc_pktcnt
= cnt
;
709 rcbp
->rc_state
= RCB_SENDING
;
710 rcbp
->rc_not_sent_bitmap
= 0;
714 * special case this to
715 * improve AFP write transactions to the server
718 if ((m2
= gbuf_alloc_wait(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
,
721 gbuf_rinc(m2
,AT_WR_OFFSET
);
722 gbuf_wset(m2
,TOTAL_ATP_HDR_SIZE
);
723 *(struct ddp_atp
*)(gbuf_rptr(m2
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
724 athp
= AT_ATP_HDR(m2
);
725 ATP_CLEAR_CONTROL(athp
);
726 athp
->cmd
= ATP_CMD_TRESP
;
728 athp
->eom
= 1; /* there's only 1 fragment */
730 /* *** why only if cnt > 0? *** */
732 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
734 if (!append_copy((struct mbuf
*)m2
,
735 (struct mbuf
*)m0
, wait
)) {
740 * send the message and mark it as sent
742 AT_DDP_HDR(m2
)->src_socket
= src_socket
;
743 dPrintf(D_M_ATP_LOW
, D_L_INFO
,
744 ("atp_unpack_bdsp %d, socket=%d, size=%d, cnt=%d\n",
745 0,atp
->atp_socket_no
,gbuf_msgsize(gbuf_cont(m2
)),cnt
));
750 /* create an array of mbuf packet headers for the packets to be sent
751 * to contain the atp and ddp headers with room at the front for the
754 for (i
= 0; i
< cnt
; i
++) {
755 /* all hdrs, packet data and dst addr storage */
757 gbuf_alloc_wait(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
, wait
)) == NULL
) {
758 for (cnt
= 0; cnt
< i
; cnt
++)
760 gbuf_freeb(rc_xmt
[cnt
]);
765 /* run through the atpBDS structs and create an mbuf for the data
766 * portion of each packet to be sent. these get chained to the mbufs
767 * containing the ATP and DDP headers. this code assumes that no ATP
768 * packet is contained in more than 2 mbufs (e.i crosses mbuf boundary
769 * no more than one time).
773 space
= gbuf_msgsize(m0
);
774 for (i
= 0; i
< cnt
; i
++) { /* for each hdr mbuf */
776 /* setup header fields */
777 gbuf_rinc(mhdr
,AT_WR_OFFSET
);
778 gbuf_wset(mhdr
,TOTAL_ATP_HDR_SIZE
);
779 *(struct ddp_atp
*)(gbuf_rptr(mhdr
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
780 athp
= AT_ATP_HDR(mhdr
);
781 ATP_CLEAR_CONTROL(athp
);
782 athp
->cmd
= ATP_CMD_TRESP
;
785 athp
->eom
= 1; /* for the last fragment */
786 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
788 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
)) != 0 && m0
!= 0 && space
> 0) {
789 if ((m1
= m_copym(m0
, offset
, len
, wait
)) == 0) {
790 for (i
= 0; i
< cnt
; i
++)
792 gbuf_freem(rc_xmt
[i
]);
795 gbuf_cont(mhdr
) = m1
;
800 AT_DDP_HDR(mhdr
)->src_socket
= src_socket
;
801 dPrintf(D_M_ATP_LOW
,D_L_INFO
,
802 ("atp_unpack_bdsp %d, socket=%d, size=%d, cnt=%d\n",
803 i
,atp
->atp_socket_no
,gbuf_msgsize(gbuf_cont(mhdr
)),cnt
));
805 gbuf_next(mprev
) = mhdr
;
810 * on to the next frag
819 getmicrouptime(&timenow
);
820 if (rcbp
->rc_timestamp
== 0) {
821 if ((rcbp
->rc_timestamp
= timenow
.tv_sec
) == 0)
822 rcbp
->rc_timestamp
= 1;
823 ATP_Q_APPEND(atp_need_rel
, rcbp
, rc_tlist
);
830 } /* atp_unpack_bdsp */
832 #define ATP_SOCKET_LAST (DDP_SOCKET_LAST-6)
833 #define ATP_SOCKET_FIRST (DDP_SOCKET_1st_DYNAMIC)
834 static unsigned int sNext
= 0;
836 int atp_bind(gref
, sVal
, flag
)
841 extern unsigned char asp_inpC
[];
842 extern asp_scb_t
*asp_scbQ
[];
843 unsigned char inpC
, sNextUsed
= 0;
844 unsigned int sMin
, sMax
, sSav
;
845 struct atp_state
*atp
;
847 atp
= (struct atp_state
*)gref
->info
;
849 atp
= (struct atp_state
*)atp
->atp_msgq
;
851 sMax
= ATP_SOCKET_LAST
;
852 sMin
= ATP_SOCKET_FIRST
;
853 if (flag
&& (*flag
== 3)) {
862 ((sVal
> sMax
) || (sVal
< 2) || (sVal
== 6) ||
863 (ddp_socket_inuse(sVal
, DDP_ATP
) &&
864 (atp_inputQ
[sVal
] != (gref_t
*)1)))) {
871 for (sVal
=sMin
; sVal
<= sMax
; sVal
++) {
872 if (!ddp_socket_inuse(sVal
, DDP_ATP
) ||
873 atp_inputQ
[sVal
] == (gref_t
*)1)
875 else if (flag
&& (*flag
== 3) && asp_scbQ
[sVal
]) {
876 if ((asp_scbQ
[sVal
]->dflag
== *flag
)
877 && (asp_inpC
[sVal
] < inpC
) ) {
878 inpC
= asp_inpC
[sVal
];
884 if (flag
&& (*flag
== 3)) {
888 sMin
= ATP_SOCKET_FIRST
+40;
892 *flag
= (unsigned char)sSav
;
897 atp
->atp_socket_no
= (short)sVal
;
898 atp_inputQ
[sVal
] = gref
;
900 atp_pidM
[sVal
] = atp
->atp_pid
;
901 else if (*flag
== 3) {
903 if (sNext
> ATP_SOCKET_LAST
)
910 void atp_req_ind(atp
, mioc
)
911 register struct atp_state
*atp
;
912 register gbuf_t
*mioc
;
914 register struct atp_rcb
*rcbp
;
916 if ((rcbp
= atp
->atp_attached
.head
) != 0) {
917 gbuf_cont(mioc
) = rcbp
->rc_ioctl
;
918 rcbp
->rc_ioctl
= NULL
;
920 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
921 rcbp
->rc_state
= RCB_NOTIFIED
;
922 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
926 ((ioc_t
*)gbuf_rptr(mioc
))->ioc_count
= gbuf_msgsize(gbuf_cont(mioc
));
928 ((ioc_t
*)gbuf_rptr(mioc
))->ioc_count
= 0;
929 asp_ack_reply(atp
->atp_gref
, mioc
);
934 void atp_rsp_ind(trp
, mioc
)
935 register struct atp_trans
*trp
;
936 register gbuf_t
*mioc
;
938 register struct atp_state
*atp
= trp
->tr_queue
;
944 switch (trp
->tr_state
) {
946 if (asp_pack_bdsp(trp
, &xm
) < 0)
948 gbuf_cont(mioc
) = trp
->tr_xmt
;
963 dPrintf(D_M_ATP
, D_L_ERROR
,
964 ("atp_rsp_ind: TRANSACTION error\n"));
965 atp_iocnak(atp
, mioc
, err
);
967 gbuf_cont(gbuf_cont(mioc
)) = xm
;
968 atp_iocack(atp
, mioc
);
973 void atp_cancel_req(gref
, tid
)
977 struct atp_state
*atp
;
978 struct atp_trans
*trp
;
980 atp
= (struct atp_state
*)gref
->info
;
982 atp
= (struct atp_state
*)atp
->atp_msgq
;
984 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
985 if (trp
->tr_tid
== tid
)
993 * remove atp from the use list
997 struct atp_state
*atp
;
1000 if (atp
== atp_used_list
) {
1001 if ((atp_used_list
= atp
->atp_trans_waiting
) != 0)
1002 atp
->atp_trans_waiting
->atp_rcb_waiting
= 0;
1003 } else if (atp
->atp_rcb_waiting
) {
1004 if ((atp
->atp_rcb_waiting
->atp_trans_waiting
1005 = atp
->atp_trans_waiting
) != 0)
1006 atp
->atp_trans_waiting
->atp_rcb_waiting
= atp
->atp_rcb_waiting
;
1009 atp
->atp_trans_waiting
= 0;
1010 atp
->atp_rcb_waiting
= 0;
1014 atp_timout(func
, trp
, ticks
)
1016 struct atp_trans
*trp
;
1020 struct atp_trans
*curr_trp
, *prev_trp
;
1022 if (trp
->tr_tmo_func
)
1025 trp
->tr_tmo_func
= func
;
1026 trp
->tr_tmo_delta
= 1+(ticks
>>5);
1028 if (trp_tmo_list
== 0) {
1029 trp
->tr_tmo_next
= trp
->tr_tmo_prev
= 0;
1035 curr_trp
= trp_tmo_list
;
1039 sum
+= curr_trp
->tr_tmo_delta
;
1040 if (sum
> trp
->tr_tmo_delta
) {
1041 sum
-= curr_trp
->tr_tmo_delta
;
1042 trp
->tr_tmo_delta
-= sum
;
1043 curr_trp
->tr_tmo_delta
-= trp
->tr_tmo_delta
;
1046 prev_trp
= curr_trp
;
1047 if ((curr_trp
= curr_trp
->tr_tmo_next
) == 0) {
1048 trp
->tr_tmo_delta
-= sum
;
1054 trp
->tr_tmo_prev
= prev_trp
;
1055 if ((trp
->tr_tmo_next
= prev_trp
->tr_tmo_next
) != 0)
1056 prev_trp
->tr_tmo_next
->tr_tmo_prev
= trp
;
1057 prev_trp
->tr_tmo_next
= trp
;
1059 trp
->tr_tmo_prev
= 0;
1060 trp
->tr_tmo_next
= trp_tmo_list
;
1061 trp_tmo_list
->tr_tmo_prev
= trp
;
1067 atp_untimout(func
, trp
)
1069 struct atp_trans
*trp
;
1072 if (trp
->tr_tmo_func
== 0)
1075 if (trp_tmo_list
== trp
) {
1076 if ((trp_tmo_list
= trp
->tr_tmo_next
) != 0) {
1077 trp_tmo_list
->tr_tmo_prev
= 0;
1078 trp
->tr_tmo_next
->tr_tmo_delta
+= trp
->tr_tmo_delta
;
1081 if ((trp
->tr_tmo_prev
->tr_tmo_next
= trp
->tr_tmo_next
) != 0) {
1082 trp
->tr_tmo_next
->tr_tmo_prev
= trp
->tr_tmo_prev
;
1083 trp
->tr_tmo_next
->tr_tmo_delta
+= trp
->tr_tmo_delta
;
1086 trp
->tr_tmo_func
= 0;
1090 atp_trp_clock_locked(arg
)
1102 struct atp_trans
*trp
;
1103 void (*tr_tmo_func
)();
1106 trp_tmo_list
->tr_tmo_delta
--;
1107 while (((trp
= trp_tmo_list
) != 0) && (trp_tmo_list
->tr_tmo_delta
== 0)) {
1108 if ((trp_tmo_list
= trp
->tr_tmo_next
) != 0)
1109 trp_tmo_list
->tr_tmo_prev
= 0;
1110 if ((tr_tmo_func
= trp
->tr_tmo_func
) != 0) {
1111 trp
->tr_tmo_func
= 0;
1112 (*tr_tmo_func
)(trp
);
1116 timeout(atp_trp_clock_locked
, (void *)arg
, (1<<5));
1120 atp_send_req(gref
, mioc
)
1124 register struct atp_state
*atp
;
1125 register struct atp_trans
*trp
;
1126 register ioc_t
*iocbp
;
1127 register at_atp_t
*athp
;
1128 register at_ddp_t
*ddp
;
1129 gbuf_t
*m
, *m2
, *bds
;
1130 struct atp_set_default
*sdb
;
1135 atp
= (struct atp_state
*)((struct atp_state
*)gref
->info
)->atp_msgq
;
1136 iocbp
= (ioc_t
*)gbuf_rptr(mioc
);
1138 if ((trp
= atp_trans_alloc(atp
)) == NULL
) {
1140 ((asp_scb_t
*)gref
->info
)->stat_msg
= mioc
;
1141 iocbp
->ioc_private
= (void *)gref
;
1142 timeout(atp_retry_req
, mioc
, 10);
1146 m2
= gbuf_cont(mioc
);
1147 if ((bds
= gbuf_dupb(m2
)) == NULL
) {
1148 atp_trans_free(trp
);
1151 gbuf_rinc(m2
,atpBDSsize
);
1152 gbuf_wset(bds
,atpBDSsize
);
1153 iocbp
->ioc_count
-= atpBDSsize
;
1154 gbuf_cont(m2
) = NULL
;
1156 old
= iocbp
->ioc_cmd
;
1157 iocbp
->ioc_cmd
= AT_ATP_ISSUE_REQUEST
;
1158 sdb
= (struct atp_set_default
*)gbuf_rptr(m2
);
1161 * The at_snd_req library routine multiplies seconds by 100.
1162 * We need to divide by 100 in order to obtain the timer.
1164 if ((timer
= (sdb
->def_rate
* HZ
)/100) == 0)
1166 iocbp
->ioc_count
-= sizeof(struct atp_set_default
);
1167 gbuf_rinc(m2
,sizeof(struct atp_set_default
));
1169 trp
->tr_retry
= sdb
->def_retries
;
1170 trp
->tr_timeout
= timer
;
1172 trp
->tr_tid
= atp_tid(atp
);
1176 * Now fill in the header (and remember the bits
1179 athp
= AT_ATP_HDR(m2
);
1180 athp
->cmd
= ATP_CMD_TREQ
;
1181 UAS_ASSIGN_HTON(athp
->tid
, trp
->tr_tid
);
1184 trp
->tr_xo
= athp
->xo
;
1185 trp
->tr_bitmap
= athp
->bitmap
;
1186 ddp
= AT_DDP_HDR(m2
);
1187 ddp
->type
= DDP_ATP
;
1188 ddp
->src_socket
= (at_socket
)atp
->atp_socket_no
;
1189 trp
->tr_socket
.socket
= ddp
->dst_socket
;
1190 trp
->tr_socket
.node
= ddp
->dst_node
;
1191 trp
->tr_socket
.net
= NET_VALUE(ddp
->dst_net
);
1192 trp
->tr_local_socket
= atp
->atp_socket_no
;
1193 trp
->tr_local_node
= ddp
->src_node
;
1194 temp_net
= NET_VALUE(ddp
->src_net
);
1195 NET_ASSIGN_NOSWAP(trp
->tr_local_net
, temp_net
);
1198 /* save the local information in the gref */
1199 atp
->atp_gref
->laddr
.s_net
= NET_VALUE(ddp
->src_net
);
1200 atp
->atp_gref
->laddr
.s_node
= ddp
->src_node
;
1201 atp
->atp_gref
->lport
= ddp
->src_node
;
1202 atp
->atp_gref
->ddptype
= DDP_ATP
;
1206 * Put us in the transaction waiting queue
1208 ATP_Q_APPEND(atp
->atp_trans_wait
, trp
, tr_list
);
1211 * Send the message and set the timer
1213 m
= (gbuf_t
*)copy_pkt(m2
, sizeof(llc_header_t
));
1214 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
)
1215 atp_x_done(trp
); /* no reason to tie up resources */
1217 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
1219 trace_mbufs(D_M_ATP_LOW
, " s", m
);
1222 } /* atp_send_req */
1224 void atp_retry_req(arg
)
1227 gbuf_t
*m
= (gbuf_t
*)arg
;
1232 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(m
))->ioc_private
;
1234 ((asp_scb_t
*)gref
->info
)->stat_msg
= 0;
1235 atp_send_req(gref
, m
);
1240 void atp_send_rsp(gref
, m
, wait
)
1245 register struct atp_state
*atp
;
1246 register struct atp_rcb
*rcbp
;
1247 register at_atp_t
*athp
;
1248 register at_ddp_t
*ddp
;
1252 atp
= (struct atp_state
*)gref
->info
;
1254 atp
= (struct atp_state
*)atp
->atp_msgq
;
1255 ddp
= AT_DDP_HDR(m
);
1256 athp
= AT_ATP_HDR(m
);
1259 * search for the corresponding rcb
1261 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
1262 if ( (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
)) &&
1263 (rcbp
->rc_socket
.node
== ddp
->dst_node
) &&
1264 (rcbp
->rc_socket
.net
== NET_VALUE(ddp
->dst_net
)) &&
1265 (rcbp
->rc_socket
.socket
== ddp
->dst_socket
) )
1270 * If it has already been sent then drop the request
1272 if ((rcbp
&& (rcbp
->rc_state
!= RCB_NOTIFIED
)) ||
1273 (rcbp
== NULL
&& athp
->xo
) ) {
1278 if (rcbp
== NULL
) { /* a response is being sent for an ALO transaction */
1279 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
1284 rcbp
->rc_socket
.socket
= ddp
->dst_socket
;
1285 rcbp
->rc_socket
.node
= ddp
->dst_node
;
1286 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->dst_net
);
1287 rcbp
->rc_tid
= UAS_VALUE_NTOH(athp
->tid
);
1288 rcbp
->rc_bitmap
= 0xff;
1290 rcbp
->rc_state
= RCB_RESPONSE_FULL
;
1291 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
1293 else if (ddp
->src_node
== 0) {
1294 temp_net
= NET_VALUE_NOSWAP(rcbp
->rc_local_net
);
1295 NET_ASSIGN(ddp
->src_net
, temp_net
);
1296 ddp
->src_node
= rcbp
->rc_local_node
;
1299 xcnt
= get_bds_entries(m
);
1300 s
= atp_unpack_bdsp(atp
, m
, rcbp
, xcnt
, wait
);
1302 atp_send_replies(atp
, rcbp
);
1303 } /* atp_send_rsp */
1305 int asp_pack_bdsp(trp
, xm
)
1306 register struct atp_trans
*trp
;
1309 register struct atpBDS
*bdsp
;
1310 register gbuf_t
*m
, *m2
;
1312 gbuf_t
*m_prev
, *m_head
= 0;
1314 dPrintf(D_M_ATP
, D_L_INFO
, ("asp_pack_bdsp: socket=%d\n",
1315 trp
->tr_queue
->atp_socket_no
));
1317 if ((m2
= trp
->tr_bdsp
) == NULL
)
1319 trp
->tr_bdsp
= NULL
;
1320 bdsp
= (struct atpBDS
*)gbuf_rptr(m2
);
1322 for (i
= 0; (i
< ATP_TRESP_MAX
&&
1323 bdsp
< (struct atpBDS
*)(gbuf_wptr(m2
))); i
++) {
1324 if ((m
= trp
->tr_rcv
[i
]) == NULL
)
1327 /* discard ddp hdr on first packet */
1328 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
1331 UAL_UAL(bdsp
->bdsUserData
, (((at_atp_t
*)(gbuf_rptr(m
)))->user_bytes
));
1332 gbuf_rinc(m
, ATP_HDR_SIZE
);
1334 if (UAL_VALUE(bdsp
->bdsBuffAddr
)) {
1337 /* user expects data back */
1342 gbuf_cont(m_prev
) = m
;
1344 tmp
= (short)gbuf_len(m
);
1345 while (gbuf_cont(m
)) {
1347 tmp
+= (short)(gbuf_len(m
));
1351 UAS_ASSIGN(bdsp
->bdsDataSz
, tmp
);
1353 trp
->tr_rcv
[i
] = NULL
;
1358 * report the number of packets
1360 UAS_ASSIGN(((struct atpBDS
*)gbuf_rptr(m2
))->bdsBuffSz
, i
);
1362 if (trp
->tr_xmt
) /* an ioctl block is still held? */
1363 gbuf_cont(trp
->tr_xmt
) = m2
;
1372 dPrintf(D_M_ATP
, D_L_INFO
, (" : size=%d\n",
1373 gbuf_msgsize(*xm
)));
1379 * The following routines are direct entries from system
1380 * calls to allow fast sending and recving of ATP data.
1384 _ATPsndreq(fd
, buf
, len
, nowait
, err
, proc
)
1396 register struct atp_state
*atp
;
1397 register struct atp_trans
*trp
;
1398 register ioc_t
*iocbp
;
1399 register at_atp_t
*athp
;
1400 register at_ddp_t
*ddp
;
1401 struct atp_set_default
*sdb
;
1402 gbuf_t
*m2
, *m
, *mioc
;
1403 char bds
[atpBDSsize
];
1405 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1408 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1409 || (atp
->atp_flags
& ATP_CLOSING
)) {
1410 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPsndreq: stale handle=0x%x, pid=%d\n",
1411 (u_int
) gref
, gref
->pid
));
1417 if (len
< atpBDSsize
+ sizeof(struct atp_set_default
) + TOTAL_ATP_HDR_SIZE
||
1418 len
> atpBDSsize
+ sizeof(struct atp_set_default
) + TOTAL_ATP_HDR_SIZE
+
1425 while ((mioc
= gbuf_alloc(sizeof(ioc_t
), PRI_MED
)) == 0) {
1427 /* the vaue of 10n terms of hz is 100ms */
1429 ts
.tv_nsec
= 100 *1000 * NSEC_PER_USEC
;
1431 rc
= msleep(&atp
->atp_delay_event
, atalk_mutex
, PSOCK
| PCATCH
, "atpmioc", &ts
);
1439 gbuf_wset(mioc
,sizeof(ioc_t
));
1441 while ((m2
= gbuf_alloc(len
, PRI_MED
)) == 0) {
1443 /* the vaue of 10n terms of hz is 100ms */
1445 ts
.tv_nsec
= 100 *1000 * NSEC_PER_USEC
;
1447 rc
= msleep(&atp
->atp_delay_event
, atalk_mutex
, PSOCK
| PCATCH
, "atpm2", &ts
);
1456 gbuf_cont(mioc
) = m2
;
1457 if (((*err
= copyin(CAST_USER_ADDR_T(buf
), (caddr_t
)bds
, atpBDSsize
)) != 0)
1458 || ((*err
= copyin(CAST_USER_ADDR_T(&buf
[atpBDSsize
]),
1459 (caddr_t
)gbuf_rptr(m2
), len
)) != 0)) {
1464 gbuf_set_type(mioc
, MSG_IOCTL
);
1465 iocbp
= (ioc_t
*)gbuf_rptr(mioc
);
1466 iocbp
->ioc_count
= len
;
1467 iocbp
->ioc_cmd
= nowait
? AT_ATP_ISSUE_REQUEST_NOTE
: AT_ATP_ISSUE_REQUEST
;
1468 sdb
= (struct atp_set_default
*)gbuf_rptr(m2
);
1471 * The at_snd_req library routine multiplies seconds by 100.
1472 * We need to divide by 100 in order to obtain the timer.
1474 if ((timer
= (sdb
->def_rate
* HZ
)/100) == 0)
1476 iocbp
->ioc_count
-= sizeof(struct atp_set_default
);
1477 gbuf_rinc(m2
,sizeof(struct atp_set_default
));
1480 * allocate and set up the transaction record
1482 while ((trp
= atp_trans_alloc(atp
)) == 0) {
1484 /* the vaue of 10n terms of hz is 100ms */
1486 ts
.tv_nsec
= 100 *1000 * NSEC_PER_USEC
;
1488 rc
= msleep(&atp
->atp_delay_event
, atalk_mutex
, PSOCK
| PCATCH
, "atptrp", &ts
);
1496 trp
->tr_retry
= sdb
->def_retries
;
1497 trp
->tr_timeout
= timer
;
1498 trp
->tr_bdsp
= NULL
;
1499 trp
->tr_tid
= atp_tid(atp
);
1503 * remember the IOCTL packet so we can ack it
1509 * Now fill in the header (and remember the bits
1512 athp
= AT_ATP_HDR(m2
);
1513 athp
->cmd
= ATP_CMD_TREQ
;
1514 UAS_ASSIGN_HTON(athp
->tid
, trp
->tr_tid
);
1517 trp
->tr_xo
= athp
->xo
;
1518 trp
->tr_bitmap
= athp
->bitmap
;
1519 ddp
= AT_DDP_HDR(m2
);
1520 ddp
->type
= DDP_ATP
;
1521 ddp
->src_socket
= (at_socket
)atp
->atp_socket_no
;
1523 trp
->tr_socket
.socket
= ddp
->dst_socket
;
1524 trp
->tr_socket
.node
= ddp
->dst_node
;
1525 trp
->tr_socket
.net
= NET_VALUE(ddp
->dst_net
);
1526 trp
->tr_local_socket
= atp
->atp_socket_no
;
1529 /* save the local information in the gref */
1530 atp
->atp_gref
->laddr
.s_net
= NET_VALUE(ddp
->src_net
);
1531 atp
->atp_gref
->laddr
.s_node
= ddp
->src_node
;
1532 atp
->atp_gref
->lport
= ddp
->src_node
;
1533 atp
->atp_gref
->ddptype
= DDP_ATP
;
1537 * Put us in the transaction waiting queue
1539 ATP_Q_APPEND(atp
->atp_trans_wait
, trp
, tr_list
);
1542 * Send the message and set the timer
1544 m
= (gbuf_t
*)copy_pkt(m2
, sizeof(llc_header_t
));
1545 if ( !trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
)
1546 atp_x_done(trp
); /* no reason to tie up resources */
1548 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
1558 * wait for the transaction to complete
1560 while ((trp
->tr_state
!= TRANS_DONE
) && (trp
->tr_state
!= TRANS_FAILED
) &&
1561 (trp
->tr_state
!= TRANS_ABORTING
)) {
1562 trp
->tr_rsp_wait
= 1;
1563 rc
= msleep(&trp
->tr_event
, atalk_mutex
, PSOCK
| PCATCH
, "atpsndreq", 0);
1565 trp
->tr_rsp_wait
= 0;
1571 trp
->tr_rsp_wait
= 0;
1574 if (trp
->tr_state
== TRANS_FAILED
|| trp
->tr_state
== TRANS_ABORTING
) {
1576 * transaction timed out, return error
1585 * copy out the recv data
1587 if ((*err
= atp_pack_bdsp(trp
, (struct atpBDS
*)bds
)) != 0) {
1594 * copyout the result info
1596 if ((*err
= copyout((caddr_t
)bds
, CAST_USER_ADDR_T(buf
), atpBDSsize
)) != 0) {
1609 /* entry point for ATP send response. respbuf contains a DDP hdr,
1610 * ATP hdr, and atpBDS array. The bdsDataSz field of the first atpBDS
1611 * struct contains the number of atpBDS structs in the array. resplen
1612 * contains the len of the data in respbuf and datalen contains the
1613 * len of the data buffer holding the response packets which the atpBDS
1614 * struct entries point to.
1617 _ATPsndrsp(fd
, respbuff
, resplen
, datalen
, err
, proc
)
1619 unsigned char *respbuff
;
1631 struct atp_state
*atp
;
1632 struct atpBDS
*bdsp
;
1635 int bds_cnt
, count
, len
;
1638 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1641 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1642 || (atp
->atp_flags
& ATP_CLOSING
)) {
1643 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPsndrsp: stale handle=0x%x, pid=%d\n",
1644 (u_int
) gref
, gref
->pid
));
1652 * allocate buffer and copy in the response info
1654 if (resplen
< 0 || resplen
> TOTAL_ATP_HDR_SIZE
+ sizeof(struct atpBDS
)*ATP_TRESP_MAX
) {
1659 if ((m
= gbuf_alloc_wait(resplen
, TRUE
)) == 0) {
1664 if ((*err
= copyin(CAST_USER_ADDR_T(respbuff
), (caddr_t
)gbuf_rptr(m
), resplen
)) != 0) {
1669 gbuf_wset(m
,resplen
);
1670 ((at_ddp_t
*)gbuf_rptr(m
))->src_node
= 0;
1671 bdsp
= (struct atpBDS
*)(gbuf_rptr(m
) + TOTAL_ATP_HDR_SIZE
);
1674 * allocate buffers and copy in the response data.
1675 * note that only the size field of the atpBDS field
1676 * is used internally in the kernel.
1678 bds_cnt
= get_bds_entries(m
); /* count of # entries */
1679 /* check correctness of parameters */
1680 if (bds_cnt
> ATP_TRESP_MAX
) {
1687 for (size
= 0, count
= 0; count
< bds_cnt
; count
++) {
1688 if (UAS_VALUE(bdsp
[count
].bdsBuffSz
) > ATP_DATA_SIZE
) {
1694 size
+= UAS_VALUE(bdsp
[count
].bdsBuffSz
);
1696 if (size
> datalen
) {
1703 /* get the first mbuf */
1704 if ((mdata
= gbuf_alloc_wait((space
= (size
> MCLBYTES
? MCLBYTES
: size
)), TRUE
)) == 0) {
1710 gbuf_cont(m
) = mdata
;
1711 dataptr
= mtod(mdata
, caddr_t
);
1712 for (count
= 0; count
< bds_cnt
; bdsp
++, count
++) {
1713 if ((bufaddr
= UAL_VALUE(bdsp
->bdsBuffAddr
)) != 0 &&
1714 (len
= UAS_VALUE(bdsp
->bdsBuffSz
)) != 0) {
1715 if (len
> space
) { /* enough room ? */
1716 gbuf_wset(mdata
, dataptr
- mtod(mdata
, caddr_t
)); /* set len of last mbuf */
1717 /* allocate the next mbuf */
1718 if ((gbuf_cont(mdata
) = m_get((M_WAIT
), MSG_DATA
)) == 0) {
1724 mdata
= gbuf_cont(mdata
);
1725 MCLGET(mdata
, M_WAIT
);
1726 if (!(mdata
->m_flags
& M_EXT
)) {
1731 dataptr
= mtod(mdata
, caddr_t
);
1735 if ((*err
= copyin(CAST_USER_ADDR_T(bufaddr
), dataptr
, len
)) != 0) {
1744 gbuf_wset(mdata
, dataptr
- mtod(mdata
, caddr_t
)); /* set len of last mbuf */
1745 gbuf_cont(m
)->m_pkthdr
.len
= size
; /* set packet hdr len */
1747 atp_send_rsp(gref
, m
, TRUE
);
1753 _ATPgetreq(fd
, buf
, buflen
, err
, proc
)
1761 register struct atp_state
*atp
;
1762 register struct atp_rcb
*rcbp
;
1763 register gbuf_t
*m
, *m_head
;
1766 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1769 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1770 || (atp
->atp_flags
& ATP_CLOSING
)) {
1771 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPgetreq: stale handle=0x%x, pid=%d\n",
1772 (u_int
) gref
, gref
->pid
));
1778 if (buflen
< DDP_X_HDR_SIZE
+ ATP_HDR_SIZE
) {
1784 if ((rcbp
= atp
->atp_attached
.head
) != NULL
) {
1786 * Got one, move it to the active response Q
1788 m_head
= rcbp
->rc_ioctl
;
1789 rcbp
->rc_ioctl
= NULL
;
1792 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
1793 rcbp
->rc_state
= RCB_NOTIFIED
;
1794 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
1796 /* detach rcbp from attached queue,
1797 * and free any outstanding resources
1803 * copyout the request data, including the protocol header
1805 for (size
=0, m
=m_head
; m
; m
= gbuf_cont(m
)) {
1806 if ((len
= gbuf_len(m
)) > buflen
)
1808 copyout((caddr_t
)gbuf_rptr(m
), CAST_USER_ADDR_T(&buf
[size
]), len
);
1810 if ((buflen
-= len
) == 0)
1824 _ATPgetrsp(fd
, bdsp
, err
, proc
)
1826 struct atpBDS
*bdsp
;
1831 register struct atp_state
*atp
;
1832 register struct atp_trans
*trp
;
1834 char bds
[atpBDSsize
];
1836 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1839 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1840 || (atp
->atp_flags
& ATP_CLOSING
)) {
1841 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPgetrsp: stale handle=0x%x, pid=%d\n",
1842 (u_int
) gref
, gref
->pid
));
1848 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
1849 dPrintf(D_M_ATP
, D_L_INFO
,
1850 ("ATPgetrsp: atp:0x%x, trp:0x%x, state:%d\n",
1851 (u_int
) atp
, (u_int
) trp
, trp
->tr_state
));
1853 switch (trp
->tr_state
) {
1855 if ((*err
= copyin(CAST_USER_ADDR_T(bdsp
),
1856 (caddr_t
)bds
, sizeof(bds
))) != 0) {
1861 if ((*err
= atp_pack_bdsp(trp
, (struct atpBDS
*)bds
)) != 0) {
1866 tid
= (int)trp
->tr_tid
;
1868 if ((*err
= copyout((caddr_t
)bds
, CAST_USER_ADDR_T(bdsp
), sizeof(bds
))) != 0) {
1877 * transaction timed out, return error
1895 atp_drop_req(gref
, m
)
1899 struct atp_state
*atp
;
1900 struct atp_rcb
*rcbp
;
1904 atp
= (struct atp_state
*)gref
->info
;
1906 atp
= (struct atp_state
*)atp
->atp_msgq
;
1907 ddp
= AT_DDP_HDR(m
);
1908 athp
= AT_ATP_HDR(m
);
1911 * search for the corresponding rcb
1913 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
1914 if ( (rcbp
->rc_tid
== UAS_VALUE_NTOH(athp
->tid
)) &&
1915 (rcbp
->rc_socket
.node
== ddp
->src_node
) &&
1916 (rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
)) &&
1917 (rcbp
->rc_socket
.socket
== ddp
->src_socket
) )