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.
32 #include <sys/errno.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <machine/spl.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/filedesc.h>
40 #include <sys/fcntl.h>
41 #include <kern/locks.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
50 #include <netat/sysglue.h>
51 #include <netat/appletalk.h>
52 #include <netat/ddp.h>
53 #include <netat/at_pcb.h>
54 #include <netat/atp.h>
55 #include <netat/at_var.h>
56 #include <netat/asp.h>
57 #include <netat/at_pat.h>
58 #include <netat/debug.h>
60 static int loop_cnt
; /* for debugging loops */
61 #define CHK_LOOP(str) { \
62 if (loop_cnt++ > 100) { \
68 static void atp_pack_bdsp(struct atp_trans
*, struct atpBDS
*);
69 static int atp_unpack_bdsp(struct atp_state
*, gbuf_t
*, struct atp_rcb
*,
71 void atp_trp_clock(), asp_clock(), asp_clock_locked(), atp_trp_clock_locked();;
73 extern struct atp_rcb_qhead atp_need_rel
;
74 extern int atp_inited
;
75 extern struct atp_state
*atp_used_list
;
76 extern asp_scb_t
*scb_free_list
;
77 extern atlock_t atpgen_lock
;
78 extern atlock_t atpall_lock
;
79 extern atlock_t atptmo_lock
;
81 extern gbuf_t
*scb_resource_m
;
82 extern gbuf_t
*atp_resource_m
;
83 extern gref_t
*atp_inputQ
[];
84 extern int atp_pidM
[];
85 extern at_ifaddr_t
*ifID_home
;
86 extern lck_mtx_t
* atalk_mutex
;
88 static struct atp_trans
*trp_tmo_list
;
89 struct atp_trans
*trp_tmo_rcb
;
91 /* first bds entry gives number of bds entries in total (hack) */
92 #define get_bds_entries(m) \
93 ((gbuf_len(m) > TOTAL_ATP_HDR_SIZE)? \
94 (UAS_VALUE(((struct atpBDS *)(AT_ATP_HDR(m)->data))->bdsDataSz)): 0)
96 #define atpBDSsize (sizeof(struct atpBDS)*ATP_TRESP_MAX)
101 trp_tmo_rcb
= atp_trans_alloc(0);
102 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
103 atp_trp_clock((void *)&atp_inited
);
104 asp_clock((void *)&atp_inited
);
109 untimeout(asp_clock_locked
, (void *)&atp_inited
);
110 untimeout(atp_trp_clock_locked
, (void *)&atp_inited
);
111 atp_untimout(atp_rcb_timer
, trp_tmo_rcb
);
115 /* allocated in asp_scb_alloc(), which is called
117 if (scb_resource_m
) {
118 gbuf_freem(scb_resource_m
);
122 /* allocated in atp_trans_alloc() */
123 if (atp_resource_m
) {
124 gbuf_freem(atp_resource_m
);
126 atp_trans_free_list
= 0;
132 * write queue put routine .... filter out other than IOCTLs
133 * Version 1.8 of atp_write.c on 89/02/09 17:53:26
138 register gref_t
*gref
;
141 register ioc_t
*iocbp
;
143 struct atp_state
*atp
;
144 struct atp_trans
*trp
;
145 struct atp_rcb
*rcbp
;
148 atp
= (struct atp_state
*)gref
->info
;
150 atp
= (struct atp_state
*)atp
->atp_msgq
;
152 switch(gbuf_type(m
)) {
156 dPrintf(D_M_ATP
, D_L_WARNING
,
157 ("atp_wput: atp_msgq discarded\n"));
163 /* Need to ensure that all copyin/copyout calls are made at
164 * put routine time which should be in the user context. (true when
165 * we are the stream head). The service routine can be called on an
166 * unpredictable context and copyin/copyout calls will get wrong results
167 * or even panic the kernel.
169 iocbp
= (ioc_t
*)gbuf_rptr(m
);
171 switch (iocbp
->ioc_cmd
) {
172 case AT_ATP_BIND_REQ
:
173 if (gbuf_cont(m
) == NULL
) {
174 iocbp
->ioc_rval
= -1;
175 atp_iocnak(atp
, m
, EINVAL
);
178 skt
= *(at_socket
*)gbuf_rptr(gbuf_cont(m
));
179 if ((skt
= (at_socket
)atp_bind(gref
, (unsigned int)skt
, 0)) == 0)
180 atp_iocnak(atp
, m
, EINVAL
);
182 *(at_socket
*)gbuf_rptr(gbuf_cont(m
)) = skt
;
185 atp_dequeue_atp(atp
);
189 case AT_ATP_GET_CHANID
:
190 if (gbuf_cont(m
) == NULL
) {
191 iocbp
->ioc_rval
= -1;
192 atp_iocnak(atp
, m
, EINVAL
);
195 *(gref_t
**)gbuf_rptr(gbuf_cont(m
)) = gref
;
199 /* not the close and not the tickle(?) */
200 case AT_ATP_ISSUE_REQUEST_DEF
:
201 case AT_ATP_ISSUE_REQUEST_DEF_NOTE
: {
202 gbuf_t
*bds
, *tmp
, *m2
;
203 struct atp_rcb
*rcbp
;
207 if ((tmp
= gbuf_cont(m
)) != 0) {
208 if ((bds
= gbuf_dupb(tmp
)) == NULL
) {
209 atp_iocnak(atp
, m
, ENOBUFS
);
212 gbuf_rinc(tmp
,atpBDSsize
);
213 gbuf_wset(bds
,atpBDSsize
);
214 iocbp
->ioc_count
-= atpBDSsize
;
215 gbuf_cont(tmp
) = bds
;
219 * send a response to a transaction
222 if (iocbp
->ioc_count
< TOTAL_ATP_HDR_SIZE
) {
223 atp_iocnak(atp
, m
, EINVAL
);
228 * remove the response from the message
232 iocbp
->ioc_count
= 0;
233 ddp
= AT_DDP_HDR(m2
);
234 athp
= AT_ATP_HDR(m2
);
236 gbuf_cont(m2
) = atp
->atp_msgq
;
240 ATDISABLE(s
, atp
->atp_lock
);
242 * search for the corresponding rcb
244 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
245 if (rcbp
->rc_tid
== UAS_VALUE(athp
->tid
) &&
246 rcbp
->rc_socket
.node
== ddp
->dst_node
&&
247 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->dst_net
) &&
248 rcbp
->rc_socket
.socket
== ddp
->dst_socket
)
251 ATENABLE(s
, atp
->atp_lock
);
254 * If it has already been sent then return an error
256 if ((rcbp
&& rcbp
->rc_state
!= RCB_NOTIFIED
) ||
257 (rcbp
== NULL
&& athp
->xo
)) {
258 atp_iocnak(atp
, m
, ENOENT
);
262 if (rcbp
== NULL
) { /* a response for an ALO transaction */
263 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
264 atp_iocnak(atp
, m
, ENOBUFS
);
269 rcbp
->rc_socket
.socket
= ddp
->dst_socket
;
270 rcbp
->rc_socket
.node
= ddp
->dst_node
;
271 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->dst_net
);
272 rcbp
->rc_tid
= UAS_VALUE(athp
->tid
);
273 rcbp
->rc_bitmap
= 0xff;
275 ATDISABLE(s
, atp
->atp_lock
);
276 rcbp
->rc_state
= RCB_SENDING
;
277 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
278 ATENABLE(s
, atp
->atp_lock
);
280 xcnt
= get_bds_entries(m2
);
281 if ((i
= atp_unpack_bdsp(atp
, m2
, rcbp
, xcnt
, FALSE
))) {
284 atp_iocnak(atp
, m
, i
);
287 atp_send_replies(atp
, rcbp
);
290 * send the ack back to the responder
296 case AT_ATP_GET_POLL
: {
298 gbuf_freem(gbuf_cont(m
));
300 iocbp
->ioc_count
= 0;
304 * search for a waiting request
306 ATDISABLE(s
, atp
->atp_lock
);
307 if ((rcbp
= atp
->atp_attached
.head
)) {
309 * Got one, move it to the active response Q
311 gbuf_cont(m
) = rcbp
->rc_ioctl
;
312 rcbp
->rc_ioctl
= NULL
;
314 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
315 rcbp
->rc_state
= RCB_NOTIFIED
;
316 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
318 /* detach rcbp from attached queue,
319 * and free any outstanding resources
323 ATENABLE(s
, atp
->atp_lock
);
327 * None available - can out
329 ATENABLE(s
, atp
->atp_lock
);
330 atp_iocnak(atp
, m
, EAGAIN
);
335 case AT_ATP_CANCEL_REQUEST
: {
337 * Cancel a pending request
339 if (iocbp
->ioc_count
!= sizeof(int)) {
340 atp_iocnak(atp
, m
, EINVAL
);
343 i
= *(int *)gbuf_rptr(gbuf_cont(m
));
344 gbuf_freem(gbuf_cont(m
));
346 ATDISABLE(s
, atp
->atp_lock
);
347 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
348 if (trp
->tr_tid
== i
)
352 ATENABLE(s
, atp
->atp_lock
);
353 atp_iocnak(atp
, m
, ENOENT
);
355 ATENABLE(s
, atp
->atp_lock
);
364 if (atalk_peek(gref
, &event
) == -1)
365 atp_iocnak(atp
, m
, EAGAIN
);
367 *gbuf_rptr(gbuf_cont(m
)) = event
;
373 case DDP_IOC_GET_CFG
:
374 #ifdef APPLETALK_DEBUG
375 kprintf("atp_wput: DDP_IOC_GET_CFG\n");
377 if (gbuf_cont(m
) == 0) {
378 atp_iocnak(atp
, m
, EINVAL
);
382 /* *** was ddp_get_cfg() *** */
384 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(m
));
385 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
386 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
387 cfgp
->inet
.socket
= atp
->atp_socket_no
;
388 cfgp
->ddptype
= DDP_ATP
;
390 cfgp
->inet
.net
= atp
->atp_gref
->laddr
.s_net
;
391 cfgp
->inet
.node
= atp
->atp_gref
->laddr
.s_node
;
392 cfgp
->inet
.socket
= atp
->atp_gref
->lport
;
393 cfgp
->ddptype
= atp
->atp_gref
->ddptype
;
396 gbuf_wset(gbuf_cont(m
), sizeof(ddp_addr_t
));
402 * Otherwise pass it on, if possible
404 iocbp
->ioc_private
= (void *)gref
;
416 gbuf_t
*atp_build_release(trp
)
417 register struct atp_trans
*trp
;
420 register at_ddp_t
*ddp
;
421 register at_atp_t
*athp
;
424 * Now try and allocate enough space to send the message
425 * if none is available the caller will schedule
426 * a timeout so we can retry for more space soon
428 if ((m
= (gbuf_t
*)gbuf_alloc(AT_WR_OFFSET
+ATP_HDR_SIZE
, PRI_HI
)) != NULL
) {
429 gbuf_rinc(m
,AT_WR_OFFSET
);
430 gbuf_wset(m
,TOTAL_ATP_HDR_SIZE
);
433 UAS_ASSIGN(ddp
->checksum
, 0);
434 ddp
->dst_socket
= trp
->tr_socket
.socket
;
435 ddp
->dst_node
= trp
->tr_socket
.node
;
436 NET_ASSIGN(ddp
->dst_net
, trp
->tr_socket
.net
);
437 ddp
->src_node
= trp
->tr_local_node
;
438 NET_NET(ddp
->src_net
, trp
->tr_local_net
);
441 * clear the cmd/xo/eom/sts/unused fields
443 athp
= AT_ATP_HDR(m
);
444 ATP_CLEAR_CONTROL(athp
);
445 athp
->cmd
= ATP_CMD_TREL
;
446 UAS_ASSIGN(athp
->tid
, trp
->tr_tid
);
452 void atp_send_replies(atp
, rcbp
)
453 register struct atp_state
*atp
;
454 register struct atp_rcb
*rcbp
;
455 { register gbuf_t
*m
;
457 int s_gen
, s
, cnt
, err
, offset
, space
;
458 unsigned char *m0_rptr
= NULL
, *m0_wptr
= NULL
;
459 register at_atp_t
*athp
;
460 register struct atpBDS
*bdsp
;
461 register gbuf_t
*m2
, *m1
, *m0
, *mhdr
;
463 gbuf_t
*mprev
, *mlist
= 0;
464 at_socket src_socket
= (at_socket
)atp
->atp_socket_no
;
465 gbuf_t
*rc_xmt
[ATP_TRESP_MAX
];
467 char ddp_atp_hdr
[TOTAL_ATP_HDR_SIZE
];
469 struct timeval timenow
;
471 ATDISABLE(s
, atp
->atp_lock
);
472 if (rcbp
->rc_queue
!= atp
) {
473 ATENABLE(s
, atp
->atp_lock
);
476 if (rcbp
->rc_not_sent_bitmap
== 0)
477 goto nothing_to_send
;
479 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send_replies\n"));
481 * Do this for each message that hasn't been sent
483 cnt
= rcbp
->rc_pktcnt
;
484 for (i
= 0; i
< cnt
; i
++) {
486 if (rcbp
->rc_snd
[i
]) {
488 gbuf_alloc(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
,PRI_MED
))
490 for (cnt
= 0; cnt
< i
; cnt
++)
492 gbuf_freeb(rc_xmt
[cnt
]);
493 goto nothing_to_send
;
500 if (gbuf_len(m
) > TOTAL_ATP_HDR_SIZE
)
501 bdsp
= (struct atpBDS
*)(AT_ATP_HDR(m
)->data
);
506 space
= gbuf_msgsize(m0
);
509 for (i
= 0; i
< cnt
; i
++) {
510 if (rcbp
->rc_snd
[i
] == 0) {
511 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
))) {
517 /* setup header fields */
518 gbuf_rinc(mhdr
,AT_WR_OFFSET
);
519 gbuf_wset(mhdr
,TOTAL_ATP_HDR_SIZE
);
520 *(struct ddp_atp
*)(gbuf_rptr(mhdr
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
521 athp
= AT_ATP_HDR(mhdr
);
522 ATP_CLEAR_CONTROL(athp
);
523 athp
->cmd
= ATP_CMD_TRESP
;
526 athp
->eom
= 1; /* for the last fragment */
528 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
529 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
)) && m0
!= 0 && space
> 0) {
530 if ((m1
= m_copym(m0
, offset
, len
, M_DONTWAIT
)) == 0) {
531 for (i
= 0; i
< cnt
; i
++)
533 gbuf_freem(rc_xmt
[i
]);
534 goto nothing_to_send
;
538 gbuf_cont(mhdr
) = m1
;
542 AT_DDP_HDR(mhdr
)->src_socket
= src_socket
;
543 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
,
544 ("atp_send_replies: %d, socket=%d, size=%d\n",
545 i
, atp
->atp_socket_no
, gbuf_msgsize(gbuf_cont(m2
))));
548 gbuf_next(mprev
) = mhdr
;
554 rcbp
->rc_not_sent_bitmap
&= ~atp_mask
[i
];
555 if (rcbp
->rc_not_sent_bitmap
== 0)
559 * on to the next frag
564 ATENABLE(s
, atp
->atp_lock
);
566 ATDISABLE(s
, atp
->atp_lock
);
571 * If all replies from this reply block have been sent then
572 * remove it from the queue and mark it so
574 if (rcbp
->rc_queue
!= atp
) {
575 ATENABLE(s
, atp
->atp_lock
);
578 rcbp
->rc_rep_waiting
= 0;
581 * If we are doing execute once re-set the rcb timeout
582 * each time we send back any part of the response. Note
583 * that this timer is started when an initial request is
584 * received. Each response reprimes the timer. Duplicate
585 * requests do not reprime the timer.
587 * We have sent all of a response so free the
590 if (rcbp
->rc_xo
&& rcbp
->rc_state
!= RCB_RELEASED
) {
591 getmicrouptime(&timenow
);
592 ATDISABLE(s_gen
, atpgen_lock
);
593 if (rcbp
->rc_timestamp
== 0) {
594 rcbp
->rc_timestamp
= timenow
.tv_sec
;
595 if (rcbp
->rc_timestamp
== 0)
596 rcbp
->rc_timestamp
= 1;
597 ATP_Q_APPEND(atp_need_rel
, rcbp
, rc_tlist
);
599 rcbp
->rc_state
= RCB_RESPONSE_FULL
;
600 ATENABLE(s_gen
, atpgen_lock
);
603 ATENABLE(s
, atp
->atp_lock
);
604 } /* atp_send_replies */
608 atp_pack_bdsp(trp
, bdsp
)
609 register struct atp_trans
*trp
;
610 register struct atpBDS
*bdsp
;
612 register gbuf_t
*m
= NULL
;
613 register int i
, datsize
= 0;
614 struct atpBDS
*bdsbase
= bdsp
;
616 dPrintf(D_M_ATP
, D_L_INFO
, ("atp_pack_bdsp: socket=%d\n",
617 trp
->tr_queue
->atp_socket_no
));
619 for (i
= 0; i
< ATP_TRESP_MAX
; i
++, bdsp
++) {
620 short bufsize
= UAS_VALUE(bdsp
->bdsBuffSz
);
621 long bufaddr
= UAL_VALUE(bdsp
->bdsBuffAddr
);
623 if ((m
= trp
->tr_rcv
[i
]) == NULL
)
626 /* discard ddp hdr on first packet */
628 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
630 /* this field may contain control information even when
631 no data is present */
632 UAL_UAL(bdsp
->bdsUserData
,
633 (((at_atp_t
*)(gbuf_rptr(m
)))->user_bytes
));
634 gbuf_rinc(m
, ATP_HDR_SIZE
);
636 if ((bufsize
!= 0) && (bufaddr
!= 0)) {
637 /* user expects data back */
639 register char *buf
= (char *)bufaddr
;
642 short len
= (short)(gbuf_len(m
));
646 copyout((caddr_t
)gbuf_rptr(m
),
647 CAST_USER_ADDR_T(&buf
[tmp
]),
655 UAS_ASSIGN(bdsp
->bdsDataSz
, tmp
);
658 gbuf_freem(trp
->tr_rcv
[i
]);
659 trp
->tr_rcv
[i
] = NULL
;
662 /* report the number of packets */
663 UAS_ASSIGN(((struct atpBDS
*)bdsbase
)->bdsBuffSz
, i
);
665 dPrintf(D_M_ATP
, D_L_INFO
, (" : size=%d\n",
667 } /* atp_pack_bdsp */
670 /* create an mbuf chain with mbuf packet headers for each ATP response packet
671 * to be sent. m contains the DDP hdr, ATP hdr, and and array of atpBDS structs.
672 * chained to m is an mbuf that contians the actual data pointed to by the atpBDS
676 atp_unpack_bdsp(atp
, m
, rcbp
, cnt
, wait
)
677 struct atp_state
*atp
;
678 gbuf_t
*m
; /* ddp, atp and bdsp gbuf_t */
679 register struct atp_rcb
*rcbp
;
680 register int cnt
, wait
;
682 register struct atpBDS
*bdsp
;
683 register gbuf_t
*m2
, *m1
, *m0
, *mhdr
;
687 at_socket src_socket
;
690 char ddp_atp_hdr
[TOTAL_ATP_HDR_SIZE
];
692 gbuf_t
*mprev
, *mlist
= 0;
693 gbuf_t
*rc_xmt
[ATP_TRESP_MAX
];
694 unsigned char *m0_rptr
, *m0_wptr
;
695 int err
, offset
, space
;
696 struct timeval timenow
;
699 * get the user data structure pointer
701 bdsp
= (struct atpBDS
*)(AT_ATP_HDR(m
)->data
);
704 * Guard against bogus count argument.
706 if ((unsigned) cnt
> ATP_TRESP_MAX
) {
707 dPrintf(D_M_ATP
, D_L_ERROR
,
708 ("atp_unpack_bdsp: bad bds count 0x%x\n", cnt
));
712 if ((src_socket
= (at_socket
)atp
->atp_socket_no
) == 0xFF) {
713 /* comparison was to -1, however src_socket is a u_char */
720 rcbp
->rc_pktcnt
= cnt
;
721 rcbp
->rc_state
= RCB_SENDING
;
722 rcbp
->rc_not_sent_bitmap
= 0;
726 * special case this to
727 * improve AFP write transactions to the server
730 if ((m2
= gbuf_alloc_wait(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
,
733 gbuf_rinc(m2
,AT_WR_OFFSET
);
734 gbuf_wset(m2
,TOTAL_ATP_HDR_SIZE
);
735 *(struct ddp_atp
*)(gbuf_rptr(m2
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
736 athp
= AT_ATP_HDR(m2
);
737 ATP_CLEAR_CONTROL(athp
);
738 athp
->cmd
= ATP_CMD_TRESP
;
740 athp
->eom
= 1; /* there's only 1 fragment */
742 /* *** why only if cnt > 0? *** */
744 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
746 if (!append_copy((struct mbuf
*)m2
,
747 (struct mbuf
*)m0
, wait
)) {
752 * send the message and mark it as sent
754 AT_DDP_HDR(m2
)->src_socket
= src_socket
;
755 dPrintf(D_M_ATP_LOW
, D_L_INFO
,
756 ("atp_unpack_bdsp %d, socket=%d, size=%d, cnt=%d\n",
757 0,atp
->atp_socket_no
,gbuf_msgsize(gbuf_cont(m2
)),cnt
));
762 /* create an array of mbuf packet headers for the packets to be sent
763 * to contain the atp and ddp headers with room at the front for the
766 for (i
= 0; i
< cnt
; i
++) {
767 /* all hdrs, packet data and dst addr storage */
769 gbuf_alloc_wait(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
, wait
)) == NULL
) {
770 for (cnt
= 0; cnt
< i
; cnt
++)
772 gbuf_freeb(rc_xmt
[cnt
]);
777 /* run through the atpBDS structs and create an mbuf for the data
778 * portion of each packet to be sent. these get chained to the mbufs
779 * containing the ATP and DDP headers. this code assumes that no ATP
780 * packet is contained in more than 2 mbufs (e.i crosses mbuf boundary
781 * no more than one time).
785 space
= gbuf_msgsize(m0
);
786 for (i
= 0; i
< cnt
; i
++) { /* for each hdr mbuf */
788 /* setup header fields */
789 gbuf_rinc(mhdr
,AT_WR_OFFSET
);
790 gbuf_wset(mhdr
,TOTAL_ATP_HDR_SIZE
);
791 *(struct ddp_atp
*)(gbuf_rptr(mhdr
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
792 athp
= AT_ATP_HDR(mhdr
);
793 ATP_CLEAR_CONTROL(athp
);
794 athp
->cmd
= ATP_CMD_TRESP
;
797 athp
->eom
= 1; /* for the last fragment */
798 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
800 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
)) != 0 && m0
!= 0 && space
> 0) {
801 if ((m1
= m_copym(m0
, offset
, len
, wait
)) == 0) {
802 for (i
= 0; i
< cnt
; i
++)
804 gbuf_freem(rc_xmt
[i
]);
807 gbuf_cont(mhdr
) = m1
;
812 AT_DDP_HDR(mhdr
)->src_socket
= src_socket
;
813 dPrintf(D_M_ATP_LOW
,D_L_INFO
,
814 ("atp_unpack_bdsp %d, socket=%d, size=%d, cnt=%d\n",
815 i
,atp
->atp_socket_no
,gbuf_msgsize(gbuf_cont(mhdr
)),cnt
));
817 gbuf_next(mprev
) = mhdr
;
822 * on to the next frag
831 getmicrouptime(&timenow
);
832 ATDISABLE(s_gen
, atpgen_lock
);
833 if (rcbp
->rc_timestamp
== 0) {
834 if ((rcbp
->rc_timestamp
= timenow
.tv_sec
) == 0)
835 rcbp
->rc_timestamp
= 1;
836 ATP_Q_APPEND(atp_need_rel
, rcbp
, rc_tlist
);
838 ATENABLE(s_gen
, atpgen_lock
);
844 } /* atp_unpack_bdsp */
846 #define ATP_SOCKET_LAST (DDP_SOCKET_LAST-6)
847 #define ATP_SOCKET_FIRST (DDP_SOCKET_1st_DYNAMIC)
848 static unsigned int sNext
= 0;
850 int atp_bind(gref
, sVal
, flag
)
855 extern unsigned char asp_inpC
[];
856 extern asp_scb_t
*asp_scbQ
[];
857 unsigned char inpC
, sNextUsed
= 0;
858 unsigned int sMin
, sMax
, sSav
;
859 struct atp_state
*atp
;
862 atp
= (struct atp_state
*)gref
->info
;
864 atp
= (struct atp_state
*)atp
->atp_msgq
;
866 sMax
= ATP_SOCKET_LAST
;
867 sMin
= ATP_SOCKET_FIRST
;
868 ATDISABLE(s
, atpgen_lock
);
869 if (flag
&& (*flag
== 3)) {
878 ((sVal
> sMax
) || (sVal
< 2) || (sVal
== 6) ||
879 (ddp_socket_inuse(sVal
, DDP_ATP
) &&
880 (atp_inputQ
[sVal
] != (gref_t
*)1)))) {
881 ATENABLE(s
, atpgen_lock
);
888 for (sVal
=sMin
; sVal
<= sMax
; sVal
++) {
889 if (!ddp_socket_inuse(sVal
, DDP_ATP
) ||
890 atp_inputQ
[sVal
] == (gref_t
*)1)
892 else if (flag
&& (*flag
== 3) && asp_scbQ
[sVal
]) {
893 if ((asp_scbQ
[sVal
]->dflag
== *flag
)
894 && (asp_inpC
[sVal
] < inpC
) ) {
895 inpC
= asp_inpC
[sVal
];
901 if (flag
&& (*flag
== 3)) {
905 sMin
= ATP_SOCKET_FIRST
+40;
909 *flag
= (unsigned char)sSav
;
911 ATENABLE(s
, atpgen_lock
);
915 atp
->atp_socket_no
= (short)sVal
;
916 atp_inputQ
[sVal
] = gref
;
918 atp_pidM
[sVal
] = atp
->atp_pid
;
919 else if (*flag
== 3) {
921 if (sNext
> ATP_SOCKET_LAST
)
925 ATENABLE(s
, atpgen_lock
);
929 void atp_req_ind(atp
, mioc
)
930 register struct atp_state
*atp
;
931 register gbuf_t
*mioc
;
933 register struct atp_rcb
*rcbp
;
936 if ((rcbp
= atp
->atp_attached
.head
) != 0) {
937 gbuf_cont(mioc
) = rcbp
->rc_ioctl
;
938 rcbp
->rc_ioctl
= NULL
;
939 ATDISABLE(s
, atp
->atp_lock
);
941 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
942 rcbp
->rc_state
= RCB_NOTIFIED
;
943 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
946 ATENABLE(s
, atp
->atp_lock
);
948 ((ioc_t
*)gbuf_rptr(mioc
))->ioc_count
= gbuf_msgsize(gbuf_cont(mioc
));
950 ((ioc_t
*)gbuf_rptr(mioc
))->ioc_count
= 0;
951 asp_ack_reply(atp
->atp_gref
, mioc
);
956 void atp_rsp_ind(trp
, mioc
)
957 register struct atp_trans
*trp
;
958 register gbuf_t
*mioc
;
960 register struct atp_state
*atp
= trp
->tr_queue
;
966 switch (trp
->tr_state
) {
968 if (asp_pack_bdsp(trp
, &xm
) < 0)
970 gbuf_cont(mioc
) = trp
->tr_xmt
;
985 dPrintf(D_M_ATP
, D_L_ERROR
,
986 ("atp_rsp_ind: TRANSACTION error\n"));
987 atp_iocnak(atp
, mioc
, err
);
989 gbuf_cont(gbuf_cont(mioc
)) = xm
;
990 atp_iocack(atp
, mioc
);
995 void atp_cancel_req(gref
, tid
)
1000 struct atp_state
*atp
;
1001 struct atp_trans
*trp
;
1003 atp
= (struct atp_state
*)gref
->info
;
1005 atp
= (struct atp_state
*)atp
->atp_msgq
;
1007 ATDISABLE(s
, atp
->atp_lock
);
1008 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
1009 if (trp
->tr_tid
== tid
)
1012 ATENABLE(s
, atp
->atp_lock
);
1018 * remove atp from the use list
1021 atp_dequeue_atp(atp
)
1022 struct atp_state
*atp
;
1026 ATDISABLE(s
, atpall_lock
);
1027 if (atp
== atp_used_list
) {
1028 if ((atp_used_list
= atp
->atp_trans_waiting
) != 0)
1029 atp
->atp_trans_waiting
->atp_rcb_waiting
= 0;
1030 } else if (atp
->atp_rcb_waiting
) {
1031 if ((atp
->atp_rcb_waiting
->atp_trans_waiting
1032 = atp
->atp_trans_waiting
) != 0)
1033 atp
->atp_trans_waiting
->atp_rcb_waiting
= atp
->atp_rcb_waiting
;
1036 atp
->atp_trans_waiting
= 0;
1037 atp
->atp_rcb_waiting
= 0;
1038 ATENABLE(s
, atpall_lock
);
1042 atp_timout(func
, trp
, ticks
)
1044 struct atp_trans
*trp
;
1049 struct atp_trans
*curr_trp
, *prev_trp
;
1051 ATDISABLE(s
, atptmo_lock
);
1052 if (trp
->tr_tmo_func
) {
1053 ATENABLE(s
, atptmo_lock
);
1057 trp
->tr_tmo_func
= func
;
1058 trp
->tr_tmo_delta
= 1+(ticks
>>5);
1060 if (trp_tmo_list
== 0) {
1061 trp
->tr_tmo_next
= trp
->tr_tmo_prev
= 0;
1063 ATENABLE(s
, atptmo_lock
);
1068 curr_trp
= trp_tmo_list
;
1072 sum
+= curr_trp
->tr_tmo_delta
;
1073 if (sum
> trp
->tr_tmo_delta
) {
1074 sum
-= curr_trp
->tr_tmo_delta
;
1075 trp
->tr_tmo_delta
-= sum
;
1076 curr_trp
->tr_tmo_delta
-= trp
->tr_tmo_delta
;
1079 prev_trp
= curr_trp
;
1080 if ((curr_trp
= curr_trp
->tr_tmo_next
) == 0) {
1081 trp
->tr_tmo_delta
-= sum
;
1087 trp
->tr_tmo_prev
= prev_trp
;
1088 if ((trp
->tr_tmo_next
= prev_trp
->tr_tmo_next
) != 0)
1089 prev_trp
->tr_tmo_next
->tr_tmo_prev
= trp
;
1090 prev_trp
->tr_tmo_next
= trp
;
1092 trp
->tr_tmo_prev
= 0;
1093 trp
->tr_tmo_next
= trp_tmo_list
;
1094 trp_tmo_list
->tr_tmo_prev
= trp
;
1097 ATENABLE(s
, atptmo_lock
);
1101 atp_untimout(func
, trp
)
1103 struct atp_trans
*trp
;
1107 ATDISABLE(s
, atptmo_lock
);
1108 if (trp
->tr_tmo_func
== 0) {
1109 ATENABLE(s
, atptmo_lock
);
1113 if (trp_tmo_list
== trp
) {
1114 if ((trp_tmo_list
= trp
->tr_tmo_next
) != 0) {
1115 trp_tmo_list
->tr_tmo_prev
= 0;
1116 trp
->tr_tmo_next
->tr_tmo_delta
+= trp
->tr_tmo_delta
;
1119 if ((trp
->tr_tmo_prev
->tr_tmo_next
= trp
->tr_tmo_next
) != 0) {
1120 trp
->tr_tmo_next
->tr_tmo_prev
= trp
->tr_tmo_prev
;
1121 trp
->tr_tmo_next
->tr_tmo_delta
+= trp
->tr_tmo_delta
;
1124 trp
->tr_tmo_func
= 0;
1125 ATENABLE(s
, atptmo_lock
);
1129 atp_trp_clock_locked(arg
)
1142 struct atp_trans
*trp
;
1143 void (*tr_tmo_func
)();
1145 ATDISABLE(s
, atptmo_lock
);
1147 trp_tmo_list
->tr_tmo_delta
--;
1148 while (((trp
= trp_tmo_list
) != 0) && (trp_tmo_list
->tr_tmo_delta
== 0)) {
1149 if ((trp_tmo_list
= trp
->tr_tmo_next
) != 0)
1150 trp_tmo_list
->tr_tmo_prev
= 0;
1151 if ((tr_tmo_func
= trp
->tr_tmo_func
) != 0) {
1152 trp
->tr_tmo_func
= 0;
1153 ATENABLE(s
, atptmo_lock
);
1154 (*tr_tmo_func
)(trp
);
1155 ATDISABLE(s
, atptmo_lock
);
1158 ATENABLE(s
, atptmo_lock
);
1160 timeout(atp_trp_clock_locked
, (void *)arg
, (1<<5));
1164 atp_send_req(gref
, mioc
)
1168 register struct atp_state
*atp
;
1169 register struct atp_trans
*trp
;
1170 register ioc_t
*iocbp
;
1171 register at_atp_t
*athp
;
1172 register at_ddp_t
*ddp
;
1173 gbuf_t
*m
, *m2
, *bds
;
1174 struct atp_set_default
*sdb
;
1178 atp
= (struct atp_state
*)((struct atp_state
*)gref
->info
)->atp_msgq
;
1179 iocbp
= (ioc_t
*)gbuf_rptr(mioc
);
1181 if ((trp
= atp_trans_alloc(atp
)) == NULL
) {
1183 ((asp_scb_t
*)gref
->info
)->stat_msg
= mioc
;
1184 iocbp
->ioc_private
= (void *)gref
;
1185 timeout(atp_retry_req
, mioc
, 10);
1189 m2
= gbuf_cont(mioc
);
1190 if ((bds
= gbuf_dupb(m2
)) == NULL
) {
1191 atp_trans_free(trp
);
1194 gbuf_rinc(m2
,atpBDSsize
);
1195 gbuf_wset(bds
,atpBDSsize
);
1196 iocbp
->ioc_count
-= atpBDSsize
;
1197 gbuf_cont(m2
) = NULL
;
1199 old
= iocbp
->ioc_cmd
;
1200 iocbp
->ioc_cmd
= AT_ATP_ISSUE_REQUEST
;
1201 sdb
= (struct atp_set_default
*)gbuf_rptr(m2
);
1204 * The at_snd_req library routine multiplies seconds by 100.
1205 * We need to divide by 100 in order to obtain the timer.
1207 if ((timer
= (sdb
->def_rate
* HZ
)/100) == 0)
1209 iocbp
->ioc_count
-= sizeof(struct atp_set_default
);
1210 gbuf_rinc(m2
,sizeof(struct atp_set_default
));
1212 trp
->tr_retry
= sdb
->def_retries
;
1213 trp
->tr_timeout
= timer
;
1215 trp
->tr_tid
= atp_tid(atp
);
1219 * Now fill in the header (and remember the bits
1222 athp
= AT_ATP_HDR(m2
);
1223 athp
->cmd
= ATP_CMD_TREQ
;
1224 UAS_ASSIGN(athp
->tid
, trp
->tr_tid
);
1227 trp
->tr_xo
= athp
->xo
;
1228 trp
->tr_bitmap
= athp
->bitmap
;
1229 ddp
= AT_DDP_HDR(m2
);
1230 ddp
->type
= DDP_ATP
;
1231 ddp
->src_socket
= (at_socket
)atp
->atp_socket_no
;
1232 trp
->tr_socket
.socket
= ddp
->dst_socket
;
1233 trp
->tr_socket
.node
= ddp
->dst_node
;
1234 trp
->tr_socket
.net
= NET_VALUE(ddp
->dst_net
);
1235 trp
->tr_local_socket
= atp
->atp_socket_no
;
1236 trp
->tr_local_node
= ddp
->src_node
;
1237 NET_NET(trp
->tr_local_net
, ddp
->src_net
);
1240 /* save the local information in the gref */
1241 atp
->atp_gref
->laddr
.s_net
= NET_VALUE(ddp
->src_net
);
1242 atp
->atp_gref
->laddr
.s_node
= ddp
->src_node
;
1243 atp
->atp_gref
->lport
= ddp
->src_node
;
1244 atp
->atp_gref
->ddptype
= DDP_ATP
;
1248 * Put us in the transaction waiting queue
1250 ATDISABLE(s
, atp
->atp_lock
);
1251 ATP_Q_APPEND(atp
->atp_trans_wait
, trp
, tr_list
);
1252 ATENABLE(s
, atp
->atp_lock
);
1255 * Send the message and set the timer
1257 m
= (gbuf_t
*)copy_pkt(m2
, sizeof(llc_header_t
));
1258 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
)
1259 atp_x_done(trp
); /* no reason to tie up resources */
1261 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
1263 trace_mbufs(D_M_ATP_LOW
, " s", m
);
1266 } /* atp_send_req */
1268 void atp_retry_req(arg
)
1271 gbuf_t
*m
= (gbuf_t
*)arg
;
1276 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(m
))->ioc_private
;
1278 ((asp_scb_t
*)gref
->info
)->stat_msg
= 0;
1279 atp_send_req(gref
, m
);
1284 void atp_send_rsp(gref
, m
, wait
)
1289 register struct atp_state
*atp
;
1290 register struct atp_rcb
*rcbp
;
1291 register at_atp_t
*athp
;
1292 register at_ddp_t
*ddp
;
1295 atp
= (struct atp_state
*)gref
->info
;
1297 atp
= (struct atp_state
*)atp
->atp_msgq
;
1298 ddp
= AT_DDP_HDR(m
);
1299 athp
= AT_ATP_HDR(m
);
1302 * search for the corresponding rcb
1304 ATDISABLE(s
, atp
->atp_lock
);
1305 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
1306 if ( (rcbp
->rc_tid
== UAS_VALUE(athp
->tid
)) &&
1307 (rcbp
->rc_socket
.node
== ddp
->dst_node
) &&
1308 (rcbp
->rc_socket
.net
== NET_VALUE(ddp
->dst_net
)) &&
1309 (rcbp
->rc_socket
.socket
== ddp
->dst_socket
) )
1314 * If it has already been sent then drop the request
1316 if ((rcbp
&& (rcbp
->rc_state
!= RCB_NOTIFIED
)) ||
1317 (rcbp
== NULL
&& athp
->xo
) ) {
1318 ATENABLE(s
, atp
->atp_lock
);
1322 ATENABLE(s
, atp
->atp_lock
);
1324 if (rcbp
== NULL
) { /* a response is being sent for an ALO transaction */
1325 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
1330 rcbp
->rc_socket
.socket
= ddp
->dst_socket
;
1331 rcbp
->rc_socket
.node
= ddp
->dst_node
;
1332 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->dst_net
);
1333 rcbp
->rc_tid
= UAS_VALUE(athp
->tid
);
1334 rcbp
->rc_bitmap
= 0xff;
1336 rcbp
->rc_state
= RCB_RESPONSE_FULL
;
1337 ATDISABLE(s
, atp
->atp_lock
);
1338 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
1339 ATENABLE(s
, atp
->atp_lock
);
1341 else if (ddp
->src_node
== 0) {
1342 NET_NET(ddp
->src_net
, rcbp
->rc_local_net
);
1343 ddp
->src_node
= rcbp
->rc_local_node
;
1346 xcnt
= get_bds_entries(m
);
1347 s
= atp_unpack_bdsp(atp
, m
, rcbp
, xcnt
, wait
);
1349 atp_send_replies(atp
, rcbp
);
1350 } /* atp_send_rsp */
1352 int asp_pack_bdsp(trp
, xm
)
1353 register struct atp_trans
*trp
;
1356 register struct atpBDS
*bdsp
;
1357 register gbuf_t
*m
, *m2
;
1359 gbuf_t
*m_prev
, *m_head
= 0;
1361 dPrintf(D_M_ATP
, D_L_INFO
, ("asp_pack_bdsp: socket=%d\n",
1362 trp
->tr_queue
->atp_socket_no
));
1364 if ((m2
= trp
->tr_bdsp
) == NULL
)
1366 trp
->tr_bdsp
= NULL
;
1367 bdsp
= (struct atpBDS
*)gbuf_rptr(m2
);
1369 for (i
= 0; (i
< ATP_TRESP_MAX
&&
1370 bdsp
< (struct atpBDS
*)(gbuf_wptr(m2
))); i
++) {
1371 if ((m
= trp
->tr_rcv
[i
]) == NULL
)
1374 /* discard ddp hdr on first packet */
1375 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
1378 UAL_UAL(bdsp
->bdsUserData
, (((at_atp_t
*)(gbuf_rptr(m
)))->user_bytes
));
1379 gbuf_rinc(m
, ATP_HDR_SIZE
);
1381 if (UAL_VALUE(bdsp
->bdsBuffAddr
)) {
1384 /* user expects data back */
1389 gbuf_cont(m_prev
) = m
;
1391 tmp
= (short)gbuf_len(m
);
1392 while (gbuf_cont(m
)) {
1394 tmp
+= (short)(gbuf_len(m
));
1398 UAS_ASSIGN(bdsp
->bdsDataSz
, tmp
);
1400 trp
->tr_rcv
[i
] = NULL
;
1405 * report the number of packets
1407 UAS_ASSIGN(((struct atpBDS
*)gbuf_rptr(m2
))->bdsBuffSz
, i
);
1409 if (trp
->tr_xmt
) /* an ioctl block is still held? */
1410 gbuf_cont(trp
->tr_xmt
) = m2
;
1419 dPrintf(D_M_ATP
, D_L_INFO
, (" : size=%d\n",
1420 gbuf_msgsize(*xm
)));
1426 * The following routines are direct entries from system
1427 * calls to allow fast sending and recving of ATP data.
1431 _ATPsndreq(fd
, buf
, len
, nowait
, err
, proc
)
1443 register struct atp_state
*atp
;
1444 register struct atp_trans
*trp
;
1445 register ioc_t
*iocbp
;
1446 register at_atp_t
*athp
;
1447 register at_ddp_t
*ddp
;
1448 struct atp_set_default
*sdb
;
1449 gbuf_t
*m2
, *m
, *mioc
;
1450 char bds
[atpBDSsize
];
1452 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1455 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1456 || (atp
->atp_flags
& ATP_CLOSING
)) {
1457 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPsndreq: stale handle=0x%x, pid=%d\n",
1458 (u_int
) gref
, gref
->pid
));
1465 while ((mioc
= gbuf_alloc(sizeof(ioc_t
), PRI_MED
)) == 0) {
1467 /* the vaue of 10n terms of hz is 100ms */
1469 ts
.tv_nsec
= 100 *1000 * NSEC_PER_USEC
;
1471 ATDISABLE(s
, atp
->atp_delay_lock
);
1472 rc
= msleep(&atp
->atp_delay_event
, atalk_mutex
, PSOCK
| PCATCH
, "atpmioc", &ts
);
1473 ATENABLE(s
, atp
->atp_delay_lock
);
1481 gbuf_wset(mioc
,sizeof(ioc_t
));
1483 while ((m2
= gbuf_alloc(len
, PRI_MED
)) == 0) {
1485 /* the vaue of 10n terms of hz is 100ms */
1487 ts
.tv_nsec
= 100 *1000 * NSEC_PER_USEC
;
1489 ATDISABLE(s
, atp
->atp_delay_lock
);
1490 rc
= msleep(&atp
->atp_delay_event
, atalk_mutex
, PSOCK
| PCATCH
, "atpm2", &ts
);
1491 ATENABLE(s
, atp
->atp_delay_lock
);
1500 gbuf_cont(mioc
) = m2
;
1501 if (((*err
= copyin(CAST_USER_ADDR_T(buf
), (caddr_t
)bds
, atpBDSsize
)) != 0)
1502 || ((*err
= copyin(CAST_USER_ADDR_T(&buf
[atpBDSsize
]),
1503 (caddr_t
)gbuf_rptr(m2
), len
)) != 0)) {
1508 gbuf_set_type(mioc
, MSG_IOCTL
);
1509 iocbp
= (ioc_t
*)gbuf_rptr(mioc
);
1510 iocbp
->ioc_count
= len
;
1511 iocbp
->ioc_cmd
= nowait
? AT_ATP_ISSUE_REQUEST_NOTE
: AT_ATP_ISSUE_REQUEST
;
1512 sdb
= (struct atp_set_default
*)gbuf_rptr(m2
);
1515 * The at_snd_req library routine multiplies seconds by 100.
1516 * We need to divide by 100 in order to obtain the timer.
1518 if ((timer
= (sdb
->def_rate
* HZ
)/100) == 0)
1520 iocbp
->ioc_count
-= sizeof(struct atp_set_default
);
1521 gbuf_rinc(m2
,sizeof(struct atp_set_default
));
1524 * allocate and set up the transaction record
1526 while ((trp
= atp_trans_alloc(atp
)) == 0) {
1528 /* the vaue of 10n terms of hz is 100ms */
1530 ts
.tv_nsec
= 100 *1000 * NSEC_PER_USEC
;
1532 ATDISABLE(s
, atp
->atp_delay_lock
);
1533 rc
= msleep(&atp
->atp_delay_event
, atalk_mutex
, PSOCK
| PCATCH
, "atptrp", &ts
);
1534 ATENABLE(s
, atp
->atp_delay_lock
);
1542 trp
->tr_retry
= sdb
->def_retries
;
1543 trp
->tr_timeout
= timer
;
1544 trp
->tr_bdsp
= NULL
;
1545 trp
->tr_tid
= atp_tid(atp
);
1549 * remember the IOCTL packet so we can ack it
1555 * Now fill in the header (and remember the bits
1558 athp
= AT_ATP_HDR(m2
);
1559 athp
->cmd
= ATP_CMD_TREQ
;
1560 UAS_ASSIGN(athp
->tid
, trp
->tr_tid
);
1563 trp
->tr_xo
= athp
->xo
;
1564 trp
->tr_bitmap
= athp
->bitmap
;
1565 ddp
= AT_DDP_HDR(m2
);
1566 ddp
->type
= DDP_ATP
;
1567 ddp
->src_socket
= (at_socket
)atp
->atp_socket_no
;
1569 trp
->tr_socket
.socket
= ddp
->dst_socket
;
1570 trp
->tr_socket
.node
= ddp
->dst_node
;
1571 trp
->tr_socket
.net
= NET_VALUE(ddp
->dst_net
);
1572 trp
->tr_local_socket
= atp
->atp_socket_no
;
1575 /* save the local information in the gref */
1576 atp
->atp_gref
->laddr
.s_net
= NET_VALUE(ddp
->src_net
);
1577 atp
->atp_gref
->laddr
.s_node
= ddp
->src_node
;
1578 atp
->atp_gref
->lport
= ddp
->src_node
;
1579 atp
->atp_gref
->ddptype
= DDP_ATP
;
1583 * Put us in the transaction waiting queue
1585 ATDISABLE(s
, atp
->atp_lock
);
1586 ATP_Q_APPEND(atp
->atp_trans_wait
, trp
, tr_list
);
1587 ATENABLE(s
, atp
->atp_lock
);
1590 * Send the message and set the timer
1592 m
= (gbuf_t
*)copy_pkt(m2
, sizeof(llc_header_t
));
1593 if ( !trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
)
1594 atp_x_done(trp
); /* no reason to tie up resources */
1596 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
1606 * wait for the transaction to complete
1608 ATDISABLE(s
, trp
->tr_lock
);
1609 while ((trp
->tr_state
!= TRANS_DONE
) && (trp
->tr_state
!= TRANS_FAILED
) &&
1610 (trp
->tr_state
!= TRANS_ABORTING
)) {
1611 trp
->tr_rsp_wait
= 1;
1612 rc
= msleep(&trp
->tr_event
, atalk_mutex
, PSOCK
| PCATCH
, "atpsndreq", 0);
1614 trp
->tr_rsp_wait
= 0;
1615 ATENABLE(s
, trp
->tr_lock
);
1621 trp
->tr_rsp_wait
= 0;
1622 ATENABLE(s
, trp
->tr_lock
);
1625 if (trp
->tr_state
== TRANS_FAILED
|| trp
->tr_state
== TRANS_ABORTING
) {
1627 * transaction timed out, return error
1636 * copy out the recv data
1638 atp_pack_bdsp(trp
, (struct atpBDS
*)bds
);
1641 * copyout the result info
1643 copyout((caddr_t
)bds
, CAST_USER_ADDR_T(buf
), atpBDSsize
);
1652 /* entry point for ATP send response. respbuf contains a DDP hdr,
1653 * ATP hdr, and atpBDS array. The bdsDataSz field of the first atpBDS
1654 * struct contains the number of atpBDS structs in the array. resplen
1655 * contains the len of the data in respbuf and datalen contains the
1656 * len of the data buffer holding the response packets which the atpBDS
1657 * struct entries point to.
1660 _ATPsndrsp(fd
, respbuff
, resplen
, datalen
, err
, proc
)
1662 unsigned char *respbuff
;
1674 struct atp_state
*atp
;
1675 struct atpBDS
*bdsp
;
1678 int bds_cnt
, count
, len
;
1681 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1684 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1685 || (atp
->atp_flags
& ATP_CLOSING
)) {
1686 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPsndrsp: stale handle=0x%x, pid=%d\n",
1687 (u_int
) gref
, gref
->pid
));
1695 * allocate buffer and copy in the response info
1697 if ((m
= gbuf_alloc_wait(resplen
, TRUE
)) == 0) {
1702 if ((*err
= copyin(CAST_USER_ADDR_T(respbuff
), (caddr_t
)gbuf_rptr(m
), resplen
)) != 0) {
1707 gbuf_wset(m
,resplen
);
1708 ((at_ddp_t
*)gbuf_rptr(m
))->src_node
= 0;
1709 bdsp
= (struct atpBDS
*)(gbuf_rptr(m
) + TOTAL_ATP_HDR_SIZE
);
1712 * allocate buffers and copy in the response data.
1713 * note that only the size field of the atpBDS field
1714 * is used internally in the kernel.
1716 bds_cnt
= get_bds_entries(m
); /* count of # entries */
1717 /* check correctness of parameters */
1718 if (bds_cnt
> ATP_TRESP_MAX
) {
1725 for (size
= 0, count
= 0; count
< bds_cnt
; count
++) {
1726 size
+= UAS_VALUE(bdsp
[count
].bdsBuffSz
);
1728 if (size
> datalen
) {
1735 /* get the first mbuf */
1736 if ((mdata
= gbuf_alloc_wait((space
= (size
> MCLBYTES
? MCLBYTES
: size
)), TRUE
)) == 0) {
1742 gbuf_cont(m
) = mdata
;
1743 dataptr
= mtod(mdata
, caddr_t
);
1744 for (count
= 0; count
< bds_cnt
; bdsp
++, count
++) {
1745 if ((bufaddr
= UAL_VALUE(bdsp
->bdsBuffAddr
)) != 0 &&
1746 (len
= UAS_VALUE(bdsp
->bdsBuffSz
)) != 0) {
1747 if (len
> space
) { /* enough room ? */
1748 gbuf_wset(mdata
, dataptr
- mtod(mdata
, caddr_t
)); /* set len of last mbuf */
1749 /* allocate the next mbuf */
1750 if ((gbuf_cont(mdata
) = m_get((M_WAIT
), MSG_DATA
)) == 0) {
1756 mdata
= gbuf_cont(mdata
);
1757 MCLGET(mdata
, M_WAIT
);
1758 if (!(mdata
->m_flags
& M_EXT
)) {
1763 dataptr
= mtod(mdata
, caddr_t
);
1767 if ((*err
= copyin(CAST_USER_ADDR_T(bufaddr
), dataptr
, len
)) != 0) {
1776 gbuf_wset(mdata
, dataptr
- mtod(mdata
, caddr_t
)); /* set len of last mbuf */
1777 gbuf_cont(m
)->m_pkthdr
.len
= size
; /* set packet hdr len */
1779 atp_send_rsp(gref
, m
, TRUE
);
1785 _ATPgetreq(fd
, buf
, buflen
, err
, proc
)
1793 register struct atp_state
*atp
;
1794 register struct atp_rcb
*rcbp
;
1795 register gbuf_t
*m
, *m_head
;
1798 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1801 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1802 || (atp
->atp_flags
& ATP_CLOSING
)) {
1803 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPgetreq: stale handle=0x%x, pid=%d\n",
1804 (u_int
) gref
, gref
->pid
));
1810 ATDISABLE(s
, atp
->atp_lock
);
1811 if ((rcbp
= atp
->atp_attached
.head
) != NULL
) {
1813 * Got one, move it to the active response Q
1815 m_head
= rcbp
->rc_ioctl
;
1816 rcbp
->rc_ioctl
= NULL
;
1819 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
1820 rcbp
->rc_state
= RCB_NOTIFIED
;
1821 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
1823 /* detach rcbp from attached queue,
1824 * and free any outstanding resources
1828 ATENABLE(s
, atp
->atp_lock
);
1831 * copyout the request data, including the protocol header
1833 for (size
=0, m
=m_head
; m
; m
= gbuf_cont(m
)) {
1834 if ((len
= gbuf_len(m
)) > buflen
)
1836 copyout((caddr_t
)gbuf_rptr(m
), CAST_USER_ADDR_T(&buf
[size
]), len
);
1838 if ((buflen
-= len
) == 0)
1846 ATENABLE(s
, atp
->atp_lock
);
1853 _ATPgetrsp(fd
, bdsp
, err
, proc
)
1855 struct atpBDS
*bdsp
;
1860 register struct atp_state
*atp
;
1861 register struct atp_trans
*trp
;
1863 char bds
[atpBDSsize
];
1865 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) != 0)
1868 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1869 || (atp
->atp_flags
& ATP_CLOSING
)) {
1870 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPgetrsp: stale handle=0x%x, pid=%d\n",
1871 (u_int
) gref
, gref
->pid
));
1877 ATDISABLE(s
, atp
->atp_lock
);
1878 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
1879 dPrintf(D_M_ATP
, D_L_INFO
,
1880 ("ATPgetrsp: atp:0x%x, trp:0x%x, state:%d\n",
1881 (u_int
) atp
, (u_int
) trp
, trp
->tr_state
));
1883 switch (trp
->tr_state
) {
1885 ATENABLE(s
, atp
->atp_lock
);
1886 if ((*err
= copyin(CAST_USER_ADDR_T(bdsp
),
1887 (caddr_t
)bds
, sizeof(bds
))) != 0) {
1891 atp_pack_bdsp(trp
, (struct atpBDS
*)bds
);
1892 tid
= (int)trp
->tr_tid
;
1894 copyout((caddr_t
)bds
, CAST_USER_ADDR_T(bdsp
), sizeof(bds
));
1900 * transaction timed out, return error
1902 ATENABLE(s
, atp
->atp_lock
);
1912 ATENABLE(s
, atp
->atp_lock
);
1920 atp_drop_req(gref
, m
)
1925 struct atp_state
*atp
;
1926 struct atp_rcb
*rcbp
;
1930 atp
= (struct atp_state
*)gref
->info
;
1932 atp
= (struct atp_state
*)atp
->atp_msgq
;
1933 ddp
= AT_DDP_HDR(m
);
1934 athp
= AT_ATP_HDR(m
);
1937 * search for the corresponding rcb
1939 ATDISABLE(s
, atp
->atp_lock
);
1940 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
1941 if ( (rcbp
->rc_tid
== UAS_VALUE(athp
->tid
)) &&
1942 (rcbp
->rc_socket
.node
== ddp
->src_node
) &&
1943 (rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
)) &&
1944 (rcbp
->rc_socket
.socket
== ddp
->src_socket
) )
1953 ATENABLE(s
, atp
->atp_lock
);