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>
42 #include <sys/ioctl.h>
43 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
49 #include <netat/sysglue.h>
50 #include <netat/appletalk.h>
51 #include <netat/ddp.h>
52 #include <netat/at_pcb.h>
53 #include <netat/atp.h>
54 #include <netat/at_var.h>
55 #include <netat/asp.h>
56 #include <netat/at_pat.h>
57 #include <netat/debug.h>
59 static int loop_cnt
; /* for debugging loops */
60 #define CHK_LOOP(str) { \
61 if (loop_cnt++ > 100) { \
67 static void atp_pack_bdsp(struct atp_trans
*, struct atpBDS
*);
68 static int atp_unpack_bdsp(struct atp_state
*, gbuf_t
*, struct atp_rcb
*,
70 void atp_trp_clock(), asp_clock(), asp_clock_funnel(), atp_trp_clock_funnel();;
72 extern struct atp_rcb_qhead atp_need_rel
;
73 extern int atp_inited
;
74 extern struct atp_state
*atp_used_list
;
75 extern asp_scb_t
*scb_free_list
;
76 extern atlock_t atpgen_lock
;
77 extern atlock_t atpall_lock
;
78 extern atlock_t atptmo_lock
;
80 extern gbuf_t
*scb_resource_m
;
81 extern gbuf_t
*atp_resource_m
;
82 extern gref_t
*atp_inputQ
[];
83 extern int atp_pidM
[];
84 extern at_ifaddr_t
*ifID_home
;
86 static struct atp_trans
*trp_tmo_list
;
87 struct atp_trans
*trp_tmo_rcb
;
89 /* first bds entry gives number of bds entries in total (hack) */
90 #define get_bds_entries(m) \
91 ((gbuf_len(m) > TOTAL_ATP_HDR_SIZE)? \
92 (UAS_VALUE(((struct atpBDS *)(AT_ATP_HDR(m)->data))->bdsDataSz)): 0)
94 #define atpBDSsize (sizeof(struct atpBDS)*ATP_TRESP_MAX)
99 trp_tmo_rcb
= atp_trans_alloc(0);
100 atp_timout(atp_rcb_timer
, trp_tmo_rcb
, 10 * HZ
);
101 atp_trp_clock((void *)&atp_inited
);
102 asp_clock((void *)&atp_inited
);
107 untimeout(asp_clock_funnel
, (void *)&atp_inited
);
108 untimeout(atp_trp_clock_funnel
, (void *)&atp_inited
);
109 atp_untimout(atp_rcb_timer
, trp_tmo_rcb
);
113 /* allocated in asp_scb_alloc(), which is called
115 if (scb_resource_m
) {
116 gbuf_freem(scb_resource_m
);
120 /* allocated in atp_trans_alloc() */
121 if (atp_resource_m
) {
122 gbuf_freem(atp_resource_m
);
124 atp_trans_free_list
= 0;
130 * write queue put routine .... filter out other than IOCTLs
131 * Version 1.8 of atp_write.c on 89/02/09 17:53:26
136 register gref_t
*gref
;
139 register ioc_t
*iocbp
;
141 struct atp_state
*atp
;
142 struct atp_trans
*trp
;
143 struct atp_rcb
*rcbp
;
146 atp
= (struct atp_state
*)gref
->info
;
148 atp
= (struct atp_state
*)atp
->atp_msgq
;
150 switch(gbuf_type(m
)) {
154 dPrintf(D_M_ATP
, D_L_WARNING
,
155 ("atp_wput: atp_msgq discarded\n"));
161 /* Need to ensure that all copyin/copyout calls are made at
162 * put routine time which should be in the user context. (true when
163 * we are the stream head). The service routine can be called on an
164 * unpredictable context and copyin/copyout calls will get wrong results
165 * or even panic the kernel.
167 iocbp
= (ioc_t
*)gbuf_rptr(m
);
169 switch (iocbp
->ioc_cmd
) {
170 case AT_ATP_BIND_REQ
:
171 if (gbuf_cont(m
) == NULL
) {
172 iocbp
->ioc_rval
= -1;
173 atp_iocnak(atp
, m
, EINVAL
);
176 skt
= *(at_socket
*)gbuf_rptr(gbuf_cont(m
));
177 if ((skt
= (at_socket
)atp_bind(gref
, (unsigned int)skt
, 0)) == 0)
178 atp_iocnak(atp
, m
, EINVAL
);
180 *(at_socket
*)gbuf_rptr(gbuf_cont(m
)) = skt
;
183 atp_dequeue_atp(atp
);
187 case AT_ATP_GET_CHANID
:
188 if (gbuf_cont(m
) == NULL
) {
189 iocbp
->ioc_rval
= -1;
190 atp_iocnak(atp
, m
, EINVAL
);
193 *(gref_t
**)gbuf_rptr(gbuf_cont(m
)) = gref
;
197 /* not the close and not the tickle(?) */
198 case AT_ATP_ISSUE_REQUEST_DEF
:
199 case AT_ATP_ISSUE_REQUEST_DEF_NOTE
: {
200 gbuf_t
*bds
, *tmp
, *m2
;
201 struct atp_rcb
*rcbp
;
205 if ((tmp
= gbuf_cont(m
)) != 0) {
206 if ((bds
= gbuf_dupb(tmp
)) == NULL
) {
207 atp_iocnak(atp
, m
, ENOBUFS
);
210 gbuf_rinc(tmp
,atpBDSsize
);
211 gbuf_wset(bds
,atpBDSsize
);
212 iocbp
->ioc_count
-= atpBDSsize
;
213 gbuf_cont(tmp
) = bds
;
217 * send a response to a transaction
220 if (iocbp
->ioc_count
< TOTAL_ATP_HDR_SIZE
) {
221 atp_iocnak(atp
, m
, EINVAL
);
226 * remove the response from the message
230 iocbp
->ioc_count
= 0;
231 ddp
= AT_DDP_HDR(m2
);
232 athp
= AT_ATP_HDR(m2
);
234 gbuf_cont(m2
) = atp
->atp_msgq
;
238 ATDISABLE(s
, atp
->atp_lock
);
240 * search for the corresponding rcb
242 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
243 if (rcbp
->rc_tid
== UAS_VALUE(athp
->tid
) &&
244 rcbp
->rc_socket
.node
== ddp
->dst_node
&&
245 rcbp
->rc_socket
.net
== NET_VALUE(ddp
->dst_net
) &&
246 rcbp
->rc_socket
.socket
== ddp
->dst_socket
)
249 ATENABLE(s
, atp
->atp_lock
);
252 * If it has already been sent then return an error
254 if ((rcbp
&& rcbp
->rc_state
!= RCB_NOTIFIED
) ||
255 (rcbp
== NULL
&& athp
->xo
)) {
256 atp_iocnak(atp
, m
, ENOENT
);
260 if (rcbp
== NULL
) { /* a response for an ALO transaction */
261 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
262 atp_iocnak(atp
, m
, ENOBUFS
);
267 rcbp
->rc_socket
.socket
= ddp
->dst_socket
;
268 rcbp
->rc_socket
.node
= ddp
->dst_node
;
269 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->dst_net
);
270 rcbp
->rc_tid
= UAS_VALUE(athp
->tid
);
271 rcbp
->rc_bitmap
= 0xff;
273 ATDISABLE(s
, atp
->atp_lock
);
274 rcbp
->rc_state
= RCB_SENDING
;
275 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
276 ATENABLE(s
, atp
->atp_lock
);
278 xcnt
= get_bds_entries(m2
);
279 if ((i
= atp_unpack_bdsp(atp
, m2
, rcbp
, xcnt
, FALSE
))) {
282 atp_iocnak(atp
, m
, i
);
285 atp_send_replies(atp
, rcbp
);
288 * send the ack back to the responder
294 case AT_ATP_GET_POLL
: {
296 gbuf_freem(gbuf_cont(m
));
298 iocbp
->ioc_count
= 0;
302 * search for a waiting request
304 ATDISABLE(s
, atp
->atp_lock
);
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
321 ATENABLE(s
, atp
->atp_lock
);
325 * None available - can out
327 ATENABLE(s
, atp
->atp_lock
);
328 atp_iocnak(atp
, m
, EAGAIN
);
333 case AT_ATP_CANCEL_REQUEST
: {
335 * Cancel a pending request
337 if (iocbp
->ioc_count
!= sizeof(int)) {
338 atp_iocnak(atp
, m
, EINVAL
);
341 i
= *(int *)gbuf_rptr(gbuf_cont(m
));
342 gbuf_freem(gbuf_cont(m
));
344 ATDISABLE(s
, atp
->atp_lock
);
345 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
346 if (trp
->tr_tid
== i
)
350 ATENABLE(s
, atp
->atp_lock
);
351 atp_iocnak(atp
, m
, ENOENT
);
353 ATENABLE(s
, atp
->atp_lock
);
362 if (atalk_peek(gref
, &event
) == -1)
363 atp_iocnak(atp
, m
, EAGAIN
);
365 *gbuf_rptr(gbuf_cont(m
)) = event
;
371 case DDP_IOC_GET_CFG
:
372 #ifdef APPLETALK_DEBUG
373 kprintf("atp_wput: DDP_IOC_GET_CFG\n");
375 if (gbuf_cont(m
) == 0) {
376 atp_iocnak(atp
, m
, EINVAL
);
380 /* *** was ddp_get_cfg() *** */
382 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(m
));
383 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
384 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
385 cfgp
->inet
.socket
= atp
->atp_socket_no
;
386 cfgp
->ddptype
= DDP_ATP
;
388 cfgp
->inet
.net
= atp
->atp_gref
->laddr
.s_net
;
389 cfgp
->inet
.node
= atp
->atp_gref
->laddr
.s_node
;
390 cfgp
->inet
.socket
= atp
->atp_gref
->lport
;
391 cfgp
->ddptype
= atp
->atp_gref
->ddptype
;
394 gbuf_wset(gbuf_cont(m
), sizeof(ddp_addr_t
));
400 * Otherwise pass it on, if possible
402 iocbp
->ioc_private
= (void *)gref
;
414 gbuf_t
*atp_build_release(trp
)
415 register struct atp_trans
*trp
;
418 register at_ddp_t
*ddp
;
419 register at_atp_t
*athp
;
422 * Now try and allocate enough space to send the message
423 * if none is available the caller will schedule
424 * a timeout so we can retry for more space soon
426 if ((m
= (gbuf_t
*)gbuf_alloc(AT_WR_OFFSET
+ATP_HDR_SIZE
, PRI_HI
)) != NULL
) {
427 gbuf_rinc(m
,AT_WR_OFFSET
);
428 gbuf_wset(m
,TOTAL_ATP_HDR_SIZE
);
431 UAS_ASSIGN(ddp
->checksum
, 0);
432 ddp
->dst_socket
= trp
->tr_socket
.socket
;
433 ddp
->dst_node
= trp
->tr_socket
.node
;
434 NET_ASSIGN(ddp
->dst_net
, trp
->tr_socket
.net
);
435 ddp
->src_node
= trp
->tr_local_node
;
436 NET_NET(ddp
->src_net
, trp
->tr_local_net
);
439 * clear the cmd/xo/eom/sts/unused fields
441 athp
= AT_ATP_HDR(m
);
442 ATP_CLEAR_CONTROL(athp
);
443 athp
->cmd
= ATP_CMD_TREL
;
444 UAS_ASSIGN(athp
->tid
, trp
->tr_tid
);
450 void atp_send_replies(atp
, rcbp
)
451 register struct atp_state
*atp
;
452 register struct atp_rcb
*rcbp
;
453 { register gbuf_t
*m
;
455 int s_gen
, s
, cnt
, err
, offset
, space
;
456 unsigned char *m0_rptr
= NULL
, *m0_wptr
= NULL
;
457 register at_atp_t
*athp
;
458 register struct atpBDS
*bdsp
;
459 register gbuf_t
*m2
, *m1
, *m0
, *mhdr
;
461 gbuf_t
*mprev
, *mlist
= 0;
462 at_socket src_socket
= (at_socket
)atp
->atp_socket_no
;
463 gbuf_t
*rc_xmt
[ATP_TRESP_MAX
];
465 char ddp_atp_hdr
[TOTAL_ATP_HDR_SIZE
];
468 ATDISABLE(s
, atp
->atp_lock
);
469 if (rcbp
->rc_queue
!= atp
) {
470 ATENABLE(s
, atp
->atp_lock
);
473 if (rcbp
->rc_not_sent_bitmap
== 0)
474 goto nothing_to_send
;
476 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
, ("atp_send_replies\n"));
478 * Do this for each message that hasn't been sent
480 cnt
= rcbp
->rc_pktcnt
;
481 for (i
= 0; i
< cnt
; i
++) {
483 if (rcbp
->rc_snd
[i
]) {
485 gbuf_alloc(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
,PRI_MED
))
487 for (cnt
= 0; cnt
< i
; cnt
++)
489 gbuf_freeb(rc_xmt
[cnt
]);
490 goto nothing_to_send
;
497 if (gbuf_len(m
) > TOTAL_ATP_HDR_SIZE
)
498 bdsp
= (struct atpBDS
*)(AT_ATP_HDR(m
)->data
);
503 space
= gbuf_msgsize(m0
);
504 for (i
= 0; i
< cnt
; i
++) {
505 if (rcbp
->rc_snd
[i
] == 0) {
506 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
))) {
512 /* setup header fields */
513 gbuf_rinc(mhdr
,AT_WR_OFFSET
);
514 gbuf_wset(mhdr
,TOTAL_ATP_HDR_SIZE
);
515 *(struct ddp_atp
*)(gbuf_rptr(mhdr
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
516 athp
= AT_ATP_HDR(mhdr
);
517 ATP_CLEAR_CONTROL(athp
);
518 athp
->cmd
= ATP_CMD_TRESP
;
521 athp
->eom
= 1; /* for the last fragment */
523 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
524 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
)) && m0
!= 0 && space
> 0) {
525 if ((m1
= m_copym(m0
, offset
, len
, M_DONTWAIT
)) == 0) {
526 for (i
= 0; i
< cnt
; i
++)
528 gbuf_freem(rc_xmt
[i
]);
529 goto nothing_to_send
;
533 gbuf_cont(mhdr
) = m1
;
537 AT_DDP_HDR(mhdr
)->src_socket
= src_socket
;
538 dPrintf(D_M_ATP_LOW
, D_L_OUTPUT
,
539 ("atp_send_replies: %d, socket=%d, size=%d\n",
540 i
, atp
->atp_socket_no
, gbuf_msgsize(gbuf_cont(m2
))));
543 gbuf_next(mprev
) = mhdr
;
549 rcbp
->rc_not_sent_bitmap
&= ~atp_mask
[i
];
550 if (rcbp
->rc_not_sent_bitmap
== 0)
554 * on to the next frag
559 ATENABLE(s
, atp
->atp_lock
);
561 ATDISABLE(s
, atp
->atp_lock
);
566 * If all replies from this reply block have been sent then
567 * remove it from the queue and mark it so
569 if (rcbp
->rc_queue
!= atp
) {
570 ATENABLE(s
, atp
->atp_lock
);
573 rcbp
->rc_rep_waiting
= 0;
576 * If we are doing execute once re-set the rcb timeout
577 * each time we send back any part of the response. Note
578 * that this timer is started when an initial request is
579 * received. Each response reprimes the timer. Duplicate
580 * requests do not reprime the timer.
582 * We have sent all of a response so free the
585 if (rcbp
->rc_xo
&& rcbp
->rc_state
!= RCB_RELEASED
) {
586 ATDISABLE(s_gen
, atpgen_lock
);
587 if (rcbp
->rc_timestamp
== 0) {
588 rcbp
->rc_timestamp
= time
.tv_sec
;
589 if (rcbp
->rc_timestamp
== 0)
590 rcbp
->rc_timestamp
= 1;
591 ATP_Q_APPEND(atp_need_rel
, rcbp
, rc_tlist
);
593 rcbp
->rc_state
= RCB_RESPONSE_FULL
;
594 ATENABLE(s_gen
, atpgen_lock
);
597 ATENABLE(s
, atp
->atp_lock
);
598 } /* atp_send_replies */
602 atp_pack_bdsp(trp
, bdsp
)
603 register struct atp_trans
*trp
;
604 register struct atpBDS
*bdsp
;
606 register gbuf_t
*m
= NULL
;
607 register int i
, datsize
= 0;
608 struct atpBDS
*bdsbase
= bdsp
;
610 dPrintf(D_M_ATP
, D_L_INFO
, ("atp_pack_bdsp: socket=%d\n",
611 trp
->tr_queue
->atp_socket_no
));
613 for (i
= 0; i
< ATP_TRESP_MAX
; i
++, bdsp
++) {
614 short bufsize
= UAS_VALUE(bdsp
->bdsBuffSz
);
615 long bufaddr
= UAL_VALUE(bdsp
->bdsBuffAddr
);
617 if ((m
= trp
->tr_rcv
[i
]) == NULL
)
620 /* discard ddp hdr on first packet */
622 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
624 /* this field may contain control information even when
625 no data is present */
626 UAL_UAL(bdsp
->bdsUserData
,
627 (((at_atp_t
*)(gbuf_rptr(m
)))->user_bytes
));
628 gbuf_rinc(m
, ATP_HDR_SIZE
);
630 if ((bufsize
!= 0) && (bufaddr
!= 0)) {
631 /* user expects data back */
633 register char *buf
= (char *)bufaddr
;
636 short len
= (short)(gbuf_len(m
));
640 copyout((caddr_t
)gbuf_rptr(m
),
649 UAS_ASSIGN(bdsp
->bdsDataSz
, tmp
);
652 gbuf_freem(trp
->tr_rcv
[i
]);
653 trp
->tr_rcv
[i
] = NULL
;
656 /* report the number of packets */
657 UAS_ASSIGN(((struct atpBDS
*)bdsbase
)->bdsBuffSz
, i
);
659 dPrintf(D_M_ATP
, D_L_INFO
, (" : size=%d\n",
661 } /* atp_pack_bdsp */
664 /* create an mbuf chain with mbuf packet headers for each ATP response packet
665 * to be sent. m contains the DDP hdr, ATP hdr, and and array of atpBDS structs.
666 * chained to m is an mbuf that contians the actual data pointed to by the atpBDS
670 atp_unpack_bdsp(atp
, m
, rcbp
, cnt
, wait
)
671 struct atp_state
*atp
;
672 gbuf_t
*m
; /* ddp, atp and bdsp gbuf_t */
673 register struct atp_rcb
*rcbp
;
674 register int cnt
, wait
;
676 register struct atpBDS
*bdsp
;
677 register gbuf_t
*m2
, *m1
, *m0
, *mhdr
;
681 at_socket src_socket
;
684 char ddp_atp_hdr
[TOTAL_ATP_HDR_SIZE
];
686 gbuf_t
*mprev
, *mlist
= 0;
687 gbuf_t
*rc_xmt
[ATP_TRESP_MAX
];
688 unsigned char *m0_rptr
, *m0_wptr
;
689 int err
, offset
, space
;
692 * get the user data structure pointer
694 bdsp
= (struct atpBDS
*)(AT_ATP_HDR(m
)->data
);
697 * Guard against bogus count argument.
699 if ((unsigned) cnt
> ATP_TRESP_MAX
) {
700 dPrintf(D_M_ATP
, D_L_ERROR
,
701 ("atp_unpack_bdsp: bad bds count 0x%x\n", cnt
));
705 if ((src_socket
= (at_socket
)atp
->atp_socket_no
) == 0xFF) {
706 /* comparison was to -1, however src_socket is a u_char */
713 rcbp
->rc_pktcnt
= cnt
;
714 rcbp
->rc_state
= RCB_SENDING
;
715 rcbp
->rc_not_sent_bitmap
= 0;
719 * special case this to
720 * improve AFP write transactions to the server
723 if ((m2
= gbuf_alloc_wait(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
,
726 gbuf_rinc(m2
,AT_WR_OFFSET
);
727 gbuf_wset(m2
,TOTAL_ATP_HDR_SIZE
);
728 *(struct ddp_atp
*)(gbuf_rptr(m2
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
729 athp
= AT_ATP_HDR(m2
);
730 ATP_CLEAR_CONTROL(athp
);
731 athp
->cmd
= ATP_CMD_TRESP
;
733 athp
->eom
= 1; /* there's only 1 fragment */
735 /* *** why only if cnt > 0? *** */
737 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
739 if (!append_copy((struct mbuf
*)m2
,
740 (struct mbuf
*)m0
, wait
)) {
745 * send the message and mark it as sent
747 AT_DDP_HDR(m2
)->src_socket
= src_socket
;
748 dPrintf(D_M_ATP_LOW
, D_L_INFO
,
749 ("atp_unpack_bdsp %d, socket=%d, size=%d, cnt=%d\n",
750 0,atp
->atp_socket_no
,gbuf_msgsize(gbuf_cont(m2
)),cnt
));
755 /* create an array of mbuf packet headers for the packets to be sent
756 * to contain the atp and ddp headers with room at the front for the
759 for (i
= 0; i
< cnt
; i
++) {
760 /* all hdrs, packet data and dst addr storage */
762 gbuf_alloc_wait(AT_WR_OFFSET
+TOTAL_ATP_HDR_SIZE
, wait
)) == NULL
) {
763 for (cnt
= 0; cnt
< i
; cnt
++)
765 gbuf_freeb(rc_xmt
[cnt
]);
770 /* run through the atpBDS structs and create an mbuf for the data
771 * portion of each packet to be sent. these get chained to the mbufs
772 * containing the ATP and DDP headers. this code assumes that no ATP
773 * packet is contained in more than 2 mbufs (e.i crosses mbuf boundary
774 * no more than one time).
778 space
= gbuf_msgsize(m0
);
779 for (i
= 0; i
< cnt
; i
++) { /* for each hdr mbuf */
781 /* setup header fields */
782 gbuf_rinc(mhdr
,AT_WR_OFFSET
);
783 gbuf_wset(mhdr
,TOTAL_ATP_HDR_SIZE
);
784 *(struct ddp_atp
*)(gbuf_rptr(mhdr
))= *(struct ddp_atp
*)(gbuf_rptr(m
));
785 athp
= AT_ATP_HDR(mhdr
);
786 ATP_CLEAR_CONTROL(athp
);
787 athp
->cmd
= ATP_CMD_TRESP
;
790 athp
->eom
= 1; /* for the last fragment */
791 UAL_UAL(athp
->user_bytes
, bdsp
->bdsUserData
);
793 if ((len
= UAS_VALUE(bdsp
->bdsBuffSz
)) != 0 && m0
!= 0 && space
> 0) {
794 if ((m1
= m_copym(m0
, offset
, len
, wait
)) == 0) {
795 for (i
= 0; i
< cnt
; i
++)
797 gbuf_freem(rc_xmt
[i
]);
800 gbuf_cont(mhdr
) = m1
;
805 AT_DDP_HDR(mhdr
)->src_socket
= src_socket
;
806 dPrintf(D_M_ATP_LOW
,D_L_INFO
,
807 ("atp_unpack_bdsp %d, socket=%d, size=%d, cnt=%d\n",
808 i
,atp
->atp_socket_no
,gbuf_msgsize(gbuf_cont(mhdr
)),cnt
));
810 gbuf_next(mprev
) = mhdr
;
815 * on to the next frag
824 ATDISABLE(s_gen
, atpgen_lock
);
825 if (rcbp
->rc_timestamp
== 0) {
826 if ((rcbp
->rc_timestamp
= time
.tv_sec
) == 0)
827 rcbp
->rc_timestamp
= 1;
828 ATP_Q_APPEND(atp_need_rel
, rcbp
, rc_tlist
);
830 ATENABLE(s_gen
, atpgen_lock
);
836 } /* atp_unpack_bdsp */
838 #define ATP_SOCKET_LAST (DDP_SOCKET_LAST-6)
839 #define ATP_SOCKET_FIRST (DDP_SOCKET_1st_DYNAMIC)
840 static unsigned int sNext
= 0;
842 int atp_bind(gref
, sVal
, flag
)
847 extern unsigned char asp_inpC
[];
848 extern asp_scb_t
*asp_scbQ
[];
849 unsigned char inpC
, sNextUsed
= 0;
850 unsigned int sMin
, sMax
, sSav
;
851 struct atp_state
*atp
;
854 atp
= (struct atp_state
*)gref
->info
;
856 atp
= (struct atp_state
*)atp
->atp_msgq
;
858 sMax
= ATP_SOCKET_LAST
;
859 sMin
= ATP_SOCKET_FIRST
;
860 ATDISABLE(s
, atpgen_lock
);
861 if (flag
&& (*flag
== 3)) {
870 ((sVal
> sMax
) || (sVal
< 2) || (sVal
== 6) ||
871 (ddp_socket_inuse(sVal
, DDP_ATP
) &&
872 (atp_inputQ
[sVal
] != (gref_t
*)1)))) {
873 ATENABLE(s
, atpgen_lock
);
880 for (sVal
=sMin
; sVal
<= sMax
; sVal
++) {
881 if (!ddp_socket_inuse(sVal
, DDP_ATP
) ||
882 atp_inputQ
[sVal
] == (gref_t
*)1)
884 else if (flag
&& (*flag
== 3) && asp_scbQ
[sVal
]) {
885 if ((asp_scbQ
[sVal
]->dflag
== *flag
)
886 && (asp_inpC
[sVal
] < inpC
) ) {
887 inpC
= asp_inpC
[sVal
];
893 if (flag
&& (*flag
== 3)) {
897 sMin
= ATP_SOCKET_FIRST
+40;
901 *flag
= (unsigned char)sSav
;
903 ATENABLE(s
, atpgen_lock
);
907 atp
->atp_socket_no
= (short)sVal
;
908 atp_inputQ
[sVal
] = gref
;
910 atp_pidM
[sVal
] = atp
->atp_pid
;
911 else if (*flag
== 3) {
913 if (sNext
> ATP_SOCKET_LAST
)
917 ATENABLE(s
, atpgen_lock
);
921 void atp_req_ind(atp
, mioc
)
922 register struct atp_state
*atp
;
923 register gbuf_t
*mioc
;
925 register struct atp_rcb
*rcbp
;
928 if ((rcbp
= atp
->atp_attached
.head
) != 0) {
929 gbuf_cont(mioc
) = rcbp
->rc_ioctl
;
930 rcbp
->rc_ioctl
= NULL
;
931 ATDISABLE(s
, atp
->atp_lock
);
933 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
934 rcbp
->rc_state
= RCB_NOTIFIED
;
935 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
938 ATENABLE(s
, atp
->atp_lock
);
940 ((ioc_t
*)gbuf_rptr(mioc
))->ioc_count
= gbuf_msgsize(gbuf_cont(mioc
));
942 ((ioc_t
*)gbuf_rptr(mioc
))->ioc_count
= 0;
943 asp_ack_reply(atp
->atp_gref
, mioc
);
948 void atp_rsp_ind(trp
, mioc
)
949 register struct atp_trans
*trp
;
950 register gbuf_t
*mioc
;
952 register struct atp_state
*atp
= trp
->tr_queue
;
958 switch (trp
->tr_state
) {
960 if (asp_pack_bdsp(trp
, &xm
) < 0)
962 gbuf_cont(mioc
) = trp
->tr_xmt
;
977 dPrintf(D_M_ATP
, D_L_ERROR
,
978 ("atp_rsp_ind: TRANSACTION error\n"));
979 atp_iocnak(atp
, mioc
, err
);
981 gbuf_cont(gbuf_cont(mioc
)) = xm
;
982 atp_iocack(atp
, mioc
);
987 void atp_cancel_req(gref
, tid
)
992 struct atp_state
*atp
;
993 struct atp_trans
*trp
;
995 atp
= (struct atp_state
*)gref
->info
;
997 atp
= (struct atp_state
*)atp
->atp_msgq
;
999 ATDISABLE(s
, atp
->atp_lock
);
1000 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
1001 if (trp
->tr_tid
== tid
)
1004 ATENABLE(s
, atp
->atp_lock
);
1010 * remove atp from the use list
1013 atp_dequeue_atp(atp
)
1014 struct atp_state
*atp
;
1018 ATDISABLE(s
, atpall_lock
);
1019 if (atp
== atp_used_list
) {
1020 if ((atp_used_list
= atp
->atp_trans_waiting
) != 0)
1021 atp
->atp_trans_waiting
->atp_rcb_waiting
= 0;
1022 } else if (atp
->atp_rcb_waiting
) {
1023 if ((atp
->atp_rcb_waiting
->atp_trans_waiting
1024 = atp
->atp_trans_waiting
) != 0)
1025 atp
->atp_trans_waiting
->atp_rcb_waiting
= atp
->atp_rcb_waiting
;
1028 atp
->atp_trans_waiting
= 0;
1029 atp
->atp_rcb_waiting
= 0;
1030 ATENABLE(s
, atpall_lock
);
1034 atp_timout(func
, trp
, ticks
)
1036 struct atp_trans
*trp
;
1041 struct atp_trans
*curr_trp
, *prev_trp
;
1043 ATDISABLE(s
, atptmo_lock
);
1044 if (trp
->tr_tmo_func
) {
1045 ATENABLE(s
, atptmo_lock
);
1049 trp
->tr_tmo_func
= func
;
1050 trp
->tr_tmo_delta
= 1+(ticks
>>5);
1052 if (trp_tmo_list
== 0) {
1053 trp
->tr_tmo_next
= trp
->tr_tmo_prev
= 0;
1055 ATENABLE(s
, atptmo_lock
);
1060 curr_trp
= trp_tmo_list
;
1064 sum
+= curr_trp
->tr_tmo_delta
;
1065 if (sum
> trp
->tr_tmo_delta
) {
1066 sum
-= curr_trp
->tr_tmo_delta
;
1067 trp
->tr_tmo_delta
-= sum
;
1068 curr_trp
->tr_tmo_delta
-= trp
->tr_tmo_delta
;
1071 prev_trp
= curr_trp
;
1072 if ((curr_trp
= curr_trp
->tr_tmo_next
) == 0) {
1073 trp
->tr_tmo_delta
-= sum
;
1079 trp
->tr_tmo_prev
= prev_trp
;
1080 if ((trp
->tr_tmo_next
= prev_trp
->tr_tmo_next
) != 0)
1081 prev_trp
->tr_tmo_next
->tr_tmo_prev
= trp
;
1082 prev_trp
->tr_tmo_next
= trp
;
1084 trp
->tr_tmo_prev
= 0;
1085 trp
->tr_tmo_next
= trp_tmo_list
;
1086 trp_tmo_list
->tr_tmo_prev
= trp
;
1089 ATENABLE(s
, atptmo_lock
);
1093 atp_untimout(func
, trp
)
1095 struct atp_trans
*trp
;
1099 ATDISABLE(s
, atptmo_lock
);
1100 if (trp
->tr_tmo_func
== 0) {
1101 ATENABLE(s
, atptmo_lock
);
1105 if (trp_tmo_list
== trp
) {
1106 if ((trp_tmo_list
= trp
->tr_tmo_next
) != 0) {
1107 trp_tmo_list
->tr_tmo_prev
= 0;
1108 trp
->tr_tmo_next
->tr_tmo_delta
+= trp
->tr_tmo_delta
;
1111 if ((trp
->tr_tmo_prev
->tr_tmo_next
= trp
->tr_tmo_next
) != 0) {
1112 trp
->tr_tmo_next
->tr_tmo_prev
= trp
->tr_tmo_prev
;
1113 trp
->tr_tmo_next
->tr_tmo_delta
+= trp
->tr_tmo_delta
;
1116 trp
->tr_tmo_func
= 0;
1117 ATENABLE(s
, atptmo_lock
);
1121 atp_trp_clock_funnel(arg
)
1124 thread_funnel_set(network_flock
, TRUE
);
1126 thread_funnel_set(network_flock
, FALSE
);
1134 struct atp_trans
*trp
;
1135 void (*tr_tmo_func
)();
1137 ATDISABLE(s
, atptmo_lock
);
1139 trp_tmo_list
->tr_tmo_delta
--;
1140 while (((trp
= trp_tmo_list
) != 0) && (trp_tmo_list
->tr_tmo_delta
== 0)) {
1141 if ((trp_tmo_list
= trp
->tr_tmo_next
) != 0)
1142 trp_tmo_list
->tr_tmo_prev
= 0;
1143 if ((tr_tmo_func
= trp
->tr_tmo_func
) != 0) {
1144 trp
->tr_tmo_func
= 0;
1145 ATENABLE(s
, atptmo_lock
);
1146 (*tr_tmo_func
)(trp
);
1147 ATDISABLE(s
, atptmo_lock
);
1150 ATENABLE(s
, atptmo_lock
);
1152 timeout(atp_trp_clock_funnel
, (void *)arg
, (1<<5));
1156 atp_send_req(gref
, mioc
)
1160 register struct atp_state
*atp
;
1161 register struct atp_trans
*trp
;
1162 register ioc_t
*iocbp
;
1163 register at_atp_t
*athp
;
1164 register at_ddp_t
*ddp
;
1165 gbuf_t
*m
, *m2
, *bds
;
1166 struct atp_set_default
*sdb
;
1170 atp
= (struct atp_state
*)((struct atp_state
*)gref
->info
)->atp_msgq
;
1171 iocbp
= (ioc_t
*)gbuf_rptr(mioc
);
1173 if ((trp
= atp_trans_alloc(atp
)) == NULL
) {
1175 ((asp_scb_t
*)gref
->info
)->stat_msg
= mioc
;
1176 iocbp
->ioc_private
= (void *)gref
;
1177 timeout(atp_retry_req
, mioc
, 10);
1181 m2
= gbuf_cont(mioc
);
1182 if ((bds
= gbuf_dupb(m2
)) == NULL
) {
1183 atp_trans_free(trp
);
1186 gbuf_rinc(m2
,atpBDSsize
);
1187 gbuf_wset(bds
,atpBDSsize
);
1188 iocbp
->ioc_count
-= atpBDSsize
;
1189 gbuf_cont(m2
) = NULL
;
1191 old
= iocbp
->ioc_cmd
;
1192 iocbp
->ioc_cmd
= AT_ATP_ISSUE_REQUEST
;
1193 sdb
= (struct atp_set_default
*)gbuf_rptr(m2
);
1196 * The at_snd_req library routine multiplies seconds by 100.
1197 * We need to divide by 100 in order to obtain the timer.
1199 if ((timer
= (sdb
->def_rate
* HZ
)/100) == 0)
1201 iocbp
->ioc_count
-= sizeof(struct atp_set_default
);
1202 gbuf_rinc(m2
,sizeof(struct atp_set_default
));
1204 trp
->tr_retry
= sdb
->def_retries
;
1205 trp
->tr_timeout
= timer
;
1207 trp
->tr_tid
= atp_tid(atp
);
1211 * Now fill in the header (and remember the bits
1214 athp
= AT_ATP_HDR(m2
);
1215 athp
->cmd
= ATP_CMD_TREQ
;
1216 UAS_ASSIGN(athp
->tid
, trp
->tr_tid
);
1219 trp
->tr_xo
= athp
->xo
;
1220 trp
->tr_bitmap
= athp
->bitmap
;
1221 ddp
= AT_DDP_HDR(m2
);
1222 ddp
->type
= DDP_ATP
;
1223 ddp
->src_socket
= (at_socket
)atp
->atp_socket_no
;
1224 trp
->tr_socket
.socket
= ddp
->dst_socket
;
1225 trp
->tr_socket
.node
= ddp
->dst_node
;
1226 trp
->tr_socket
.net
= NET_VALUE(ddp
->dst_net
);
1227 trp
->tr_local_socket
= atp
->atp_socket_no
;
1228 trp
->tr_local_node
= ddp
->src_node
;
1229 NET_NET(trp
->tr_local_net
, ddp
->src_net
);
1232 /* save the local information in the gref */
1233 atp
->atp_gref
->laddr
.s_net
= NET_VALUE(ddp
->src_net
);
1234 atp
->atp_gref
->laddr
.s_node
= ddp
->src_node
;
1235 atp
->atp_gref
->lport
= ddp
->src_node
;
1236 atp
->atp_gref
->ddptype
= DDP_ATP
;
1240 * Put us in the transaction waiting queue
1242 ATDISABLE(s
, atp
->atp_lock
);
1243 ATP_Q_APPEND(atp
->atp_trans_wait
, trp
, tr_list
);
1244 ATENABLE(s
, atp
->atp_lock
);
1247 * Send the message and set the timer
1249 m
= (gbuf_t
*)copy_pkt(m2
, sizeof(llc_header_t
));
1250 if (!trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
)
1251 atp_x_done(trp
); /* no reason to tie up resources */
1253 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
1255 trace_mbufs(D_M_ATP_LOW
, " s", m
);
1258 } /* atp_send_req */
1260 void atp_retry_req(arg
)
1263 gbuf_t
*m
= (gbuf_t
*)arg
;
1265 boolean_t funnel_state
;
1267 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1269 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(m
))->ioc_private
;
1271 ((asp_scb_t
*)gref
->info
)->stat_msg
= 0;
1272 atp_send_req(gref
, m
);
1274 (void) thread_funnel_set(network_flock
, FALSE
);
1277 void atp_send_rsp(gref
, m
, wait
)
1282 register struct atp_state
*atp
;
1283 register struct atp_rcb
*rcbp
;
1284 register at_atp_t
*athp
;
1285 register at_ddp_t
*ddp
;
1288 atp
= (struct atp_state
*)gref
->info
;
1290 atp
= (struct atp_state
*)atp
->atp_msgq
;
1291 ddp
= AT_DDP_HDR(m
);
1292 athp
= AT_ATP_HDR(m
);
1295 * search for the corresponding rcb
1297 ATDISABLE(s
, atp
->atp_lock
);
1298 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
1299 if ( (rcbp
->rc_tid
== UAS_VALUE(athp
->tid
)) &&
1300 (rcbp
->rc_socket
.node
== ddp
->dst_node
) &&
1301 (rcbp
->rc_socket
.net
== NET_VALUE(ddp
->dst_net
)) &&
1302 (rcbp
->rc_socket
.socket
== ddp
->dst_socket
) )
1307 * If it has already been sent then drop the request
1309 if ((rcbp
&& (rcbp
->rc_state
!= RCB_NOTIFIED
)) ||
1310 (rcbp
== NULL
&& athp
->xo
) ) {
1311 ATENABLE(s
, atp
->atp_lock
);
1315 ATENABLE(s
, atp
->atp_lock
);
1317 if (rcbp
== NULL
) { /* a response is being sent for an ALO transaction */
1318 if ((rcbp
= atp_rcb_alloc(atp
)) == NULL
) {
1323 rcbp
->rc_socket
.socket
= ddp
->dst_socket
;
1324 rcbp
->rc_socket
.node
= ddp
->dst_node
;
1325 rcbp
->rc_socket
.net
= NET_VALUE(ddp
->dst_net
);
1326 rcbp
->rc_tid
= UAS_VALUE(athp
->tid
);
1327 rcbp
->rc_bitmap
= 0xff;
1329 rcbp
->rc_state
= RCB_RESPONSE_FULL
;
1330 ATDISABLE(s
, atp
->atp_lock
);
1331 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
1332 ATENABLE(s
, atp
->atp_lock
);
1334 else if (ddp
->src_node
== 0) {
1335 NET_NET(ddp
->src_net
, rcbp
->rc_local_net
);
1336 ddp
->src_node
= rcbp
->rc_local_node
;
1339 xcnt
= get_bds_entries(m
);
1340 s
= atp_unpack_bdsp(atp
, m
, rcbp
, xcnt
, wait
);
1342 atp_send_replies(atp
, rcbp
);
1343 } /* atp_send_rsp */
1345 int asp_pack_bdsp(trp
, xm
)
1346 register struct atp_trans
*trp
;
1349 register struct atpBDS
*bdsp
;
1350 register gbuf_t
*m
, *m2
;
1352 gbuf_t
*m_prev
, *m_head
= 0;
1354 dPrintf(D_M_ATP
, D_L_INFO
, ("asp_pack_bdsp: socket=%d\n",
1355 trp
->tr_queue
->atp_socket_no
));
1357 if ((m2
= trp
->tr_bdsp
) == NULL
)
1359 trp
->tr_bdsp
= NULL
;
1360 bdsp
= (struct atpBDS
*)gbuf_rptr(m2
);
1362 for (i
= 0; (i
< ATP_TRESP_MAX
&&
1363 bdsp
< (struct atpBDS
*)(gbuf_wptr(m2
))); i
++) {
1364 if ((m
= trp
->tr_rcv
[i
]) == NULL
)
1367 /* discard ddp hdr on first packet */
1368 gbuf_rinc(m
,DDP_X_HDR_SIZE
);
1371 UAL_UAL(bdsp
->bdsUserData
, (((at_atp_t
*)(gbuf_rptr(m
)))->user_bytes
));
1372 gbuf_rinc(m
, ATP_HDR_SIZE
);
1374 if (UAL_VALUE(bdsp
->bdsBuffAddr
)) {
1377 /* user expects data back */
1382 gbuf_cont(m_prev
) = m
;
1384 tmp
= (short)gbuf_len(m
);
1385 while (gbuf_cont(m
)) {
1387 tmp
+= (short)(gbuf_len(m
));
1391 UAS_ASSIGN(bdsp
->bdsDataSz
, tmp
);
1393 trp
->tr_rcv
[i
] = NULL
;
1398 * report the number of packets
1400 UAS_ASSIGN(((struct atpBDS
*)gbuf_rptr(m2
))->bdsBuffSz
, i
);
1402 if (trp
->tr_xmt
) /* an ioctl block is still held? */
1403 gbuf_cont(trp
->tr_xmt
) = m2
;
1412 dPrintf(D_M_ATP
, D_L_INFO
, (" : size=%d\n",
1413 gbuf_msgsize(*xm
)));
1419 * The following routines are direct entries from system
1420 * calls to allow fast sending and recving of ATP data.
1424 _ATPsndreq(fd
, buf
, len
, nowait
, err
, proc
)
1436 register struct atp_state
*atp
;
1437 register struct atp_trans
*trp
;
1438 register ioc_t
*iocbp
;
1439 register at_atp_t
*athp
;
1440 register at_ddp_t
*ddp
;
1441 struct atp_set_default
*sdb
;
1442 gbuf_t
*m2
, *m
, *mioc
;
1443 char bds
[atpBDSsize
];
1445 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) != 0)
1448 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1449 || (atp
->atp_flags
& ATP_CLOSING
)) {
1450 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPsndreq: stale handle=0x%x, pid=%d\n",
1451 (u_int
) gref
, gref
->pid
));
1457 while ((mioc
= gbuf_alloc(sizeof(ioc_t
), PRI_MED
)) == 0) {
1458 ATDISABLE(s
, atp
->atp_delay_lock
);
1459 rc
= tsleep(&atp
->atp_delay_event
, PSOCK
| PCATCH
, "atpmioc", 10);
1460 ATENABLE(s
, atp
->atp_delay_lock
);
1467 gbuf_wset(mioc
,sizeof(ioc_t
));
1469 while ((m2
= gbuf_alloc(len
, PRI_MED
)) == 0) {
1470 ATDISABLE(s
, atp
->atp_delay_lock
);
1471 rc
= tsleep(&atp
->atp_delay_event
, PSOCK
| PCATCH
, "atpm2", 10);
1472 ATENABLE(s
, atp
->atp_delay_lock
);
1480 gbuf_cont(mioc
) = m2
;
1481 if (((*err
= copyin((caddr_t
)buf
, (caddr_t
)bds
, atpBDSsize
)) != 0)
1482 || ((*err
= copyin((caddr_t
)&buf
[atpBDSsize
],
1483 (caddr_t
)gbuf_rptr(m2
), len
)) != 0)) {
1487 gbuf_set_type(mioc
, MSG_IOCTL
);
1488 iocbp
= (ioc_t
*)gbuf_rptr(mioc
);
1489 iocbp
->ioc_count
= len
;
1490 iocbp
->ioc_cmd
= nowait
? AT_ATP_ISSUE_REQUEST_NOTE
: AT_ATP_ISSUE_REQUEST
;
1491 sdb
= (struct atp_set_default
*)gbuf_rptr(m2
);
1494 * The at_snd_req library routine multiplies seconds by 100.
1495 * We need to divide by 100 in order to obtain the timer.
1497 if ((timer
= (sdb
->def_rate
* HZ
)/100) == 0)
1499 iocbp
->ioc_count
-= sizeof(struct atp_set_default
);
1500 gbuf_rinc(m2
,sizeof(struct atp_set_default
));
1503 * allocate and set up the transaction record
1505 while ((trp
= atp_trans_alloc(atp
)) == 0) {
1506 ATDISABLE(s
, atp
->atp_delay_lock
);
1507 rc
= tsleep(&atp
->atp_delay_event
, PSOCK
| PCATCH
, "atptrp", 10);
1508 ATENABLE(s
, atp
->atp_delay_lock
);
1515 trp
->tr_retry
= sdb
->def_retries
;
1516 trp
->tr_timeout
= timer
;
1517 trp
->tr_bdsp
= NULL
;
1518 trp
->tr_tid
= atp_tid(atp
);
1522 * remember the IOCTL packet so we can ack it
1528 * Now fill in the header (and remember the bits
1531 athp
= AT_ATP_HDR(m2
);
1532 athp
->cmd
= ATP_CMD_TREQ
;
1533 UAS_ASSIGN(athp
->tid
, trp
->tr_tid
);
1536 trp
->tr_xo
= athp
->xo
;
1537 trp
->tr_bitmap
= athp
->bitmap
;
1538 ddp
= AT_DDP_HDR(m2
);
1539 ddp
->type
= DDP_ATP
;
1540 ddp
->src_socket
= (at_socket
)atp
->atp_socket_no
;
1542 trp
->tr_socket
.socket
= ddp
->dst_socket
;
1543 trp
->tr_socket
.node
= ddp
->dst_node
;
1544 trp
->tr_socket
.net
= NET_VALUE(ddp
->dst_net
);
1545 trp
->tr_local_socket
= atp
->atp_socket_no
;
1548 /* save the local information in the gref */
1549 atp
->atp_gref
->laddr
.s_net
= NET_VALUE(ddp
->src_net
);
1550 atp
->atp_gref
->laddr
.s_node
= ddp
->src_node
;
1551 atp
->atp_gref
->lport
= ddp
->src_node
;
1552 atp
->atp_gref
->ddptype
= DDP_ATP
;
1556 * Put us in the transaction waiting queue
1558 ATDISABLE(s
, atp
->atp_lock
);
1559 ATP_Q_APPEND(atp
->atp_trans_wait
, trp
, tr_list
);
1560 ATENABLE(s
, atp
->atp_lock
);
1563 * Send the message and set the timer
1565 m
= (gbuf_t
*)copy_pkt(m2
, sizeof(llc_header_t
));
1566 if ( !trp
->tr_retry
&& !trp
->tr_bitmap
&& !trp
->tr_xo
)
1567 atp_x_done(trp
); /* no reason to tie up resources */
1569 atp_timout(atp_req_timeout
, trp
, trp
->tr_timeout
);
1577 * wait for the transaction to complete
1579 ATDISABLE(s
, trp
->tr_lock
);
1580 while ((trp
->tr_state
!= TRANS_DONE
) && (trp
->tr_state
!= TRANS_FAILED
) &&
1581 (trp
->tr_state
!= TRANS_ABORTING
)) {
1582 trp
->tr_rsp_wait
= 1;
1583 rc
= tsleep(&trp
->tr_event
, PSOCK
| PCATCH
, "atpsndreq", 0);
1585 trp
->tr_rsp_wait
= 0;
1586 ATENABLE(s
, trp
->tr_lock
);
1591 trp
->tr_rsp_wait
= 0;
1592 ATENABLE(s
, trp
->tr_lock
);
1595 if (trp
->tr_state
== TRANS_FAILED
|| trp
->tr_state
== TRANS_ABORTING
) {
1597 * transaction timed out, return error
1605 * copy out the recv data
1607 atp_pack_bdsp(trp
, (struct atpBDS
*)bds
);
1610 * copyout the result info
1612 copyout((caddr_t
)bds
, (caddr_t
)buf
, atpBDSsize
);
1620 /* entry point for ATP send response. respbuf contains a DDP hdr,
1621 * ATP hdr, and atpBDS array. The bdsDataSz field of the first atpBDS
1622 * struct contains the number of atpBDS structs in the array. resplen
1623 * contains the len of the data in respbuf and datalen contains the
1624 * len of the data buffer holding the response packets which the atpBDS
1625 * struct entries point to.
1628 _ATPsndrsp(fd
, respbuff
, resplen
, datalen
, err
, proc
)
1630 unsigned char *respbuff
;
1642 struct atp_state
*atp
;
1643 struct atpBDS
*bdsp
;
1646 int bds_cnt
, count
, len
;
1649 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) != 0)
1652 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1653 || (atp
->atp_flags
& ATP_CLOSING
)) {
1654 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPsndrsp: stale handle=0x%x, pid=%d\n",
1655 (u_int
) gref
, gref
->pid
));
1662 * allocate buffer and copy in the response info
1664 if ((m
= gbuf_alloc_wait(resplen
, TRUE
)) == 0) {
1668 if ((*err
= copyin((caddr_t
)respbuff
, (caddr_t
)gbuf_rptr(m
), resplen
)) != 0) {
1672 gbuf_wset(m
,resplen
);
1673 ((at_ddp_t
*)gbuf_rptr(m
))->src_node
= 0;
1674 bdsp
= (struct atpBDS
*)(gbuf_rptr(m
) + TOTAL_ATP_HDR_SIZE
);
1677 * allocate buffers and copy in the response data.
1678 * note that only the size field of the atpBDS field
1679 * is used internally in the kernel.
1681 bds_cnt
= get_bds_entries(m
); /* count of # entries */
1682 /* check correctness of parameters */
1683 if (bds_cnt
> ATP_TRESP_MAX
) {
1689 for (size
= 0, count
= 0; count
< bds_cnt
; count
++) {
1690 size
+= UAS_VALUE(bdsp
[count
].bdsBuffSz
);
1692 if (size
> datalen
) {
1698 /* get the first mbuf */
1699 if ((mdata
= gbuf_alloc_wait((space
= (size
> MCLBYTES
? MCLBYTES
: size
)), TRUE
)) == 0) {
1704 gbuf_cont(m
) = mdata
;
1705 dataptr
= mtod(mdata
, caddr_t
);
1706 for (count
= 0; count
< bds_cnt
; bdsp
++, count
++) {
1707 if ((bufaddr
= UAL_VALUE(bdsp
->bdsBuffAddr
)) != 0 &&
1708 (len
= UAS_VALUE(bdsp
->bdsBuffSz
)) != 0) {
1709 if (len
> space
) { /* enough room ? */
1710 gbuf_wset(mdata
, dataptr
- mtod(mdata
, caddr_t
)); /* set len of last mbuf */
1711 /* allocate the next mbuf */
1712 if ((gbuf_cont(mdata
) = m_get((M_WAIT
), MSG_DATA
)) == 0) {
1717 mdata
= gbuf_cont(mdata
);
1718 MCLGET(mdata
, M_WAIT
);
1719 if (!(mdata
->m_flags
& M_EXT
)) {
1723 dataptr
= mtod(mdata
, caddr_t
);
1727 if ((*err
= copyin((caddr_t
)bufaddr
, dataptr
, len
)) != 0) {
1735 gbuf_wset(mdata
, dataptr
- mtod(mdata
, caddr_t
)); /* set len of last mbuf */
1736 gbuf_cont(m
)->m_pkthdr
.len
= size
; /* set packet hdr len */
1738 atp_send_rsp(gref
, m
, TRUE
);
1743 _ATPgetreq(fd
, buf
, buflen
, err
, proc
)
1751 register struct atp_state
*atp
;
1752 register struct atp_rcb
*rcbp
;
1753 register gbuf_t
*m
, *m_head
;
1756 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) != 0)
1759 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1760 || (atp
->atp_flags
& ATP_CLOSING
)) {
1761 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPgetreq: stale handle=0x%x, pid=%d\n",
1762 (u_int
) gref
, gref
->pid
));
1767 ATDISABLE(s
, atp
->atp_lock
);
1768 if ((rcbp
= atp
->atp_attached
.head
) != NULL
) {
1770 * Got one, move it to the active response Q
1772 m_head
= rcbp
->rc_ioctl
;
1773 rcbp
->rc_ioctl
= NULL
;
1776 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
1777 rcbp
->rc_state
= RCB_NOTIFIED
;
1778 ATP_Q_APPEND(atp
->atp_rcb
, rcbp
, rc_list
);
1780 /* detach rcbp from attached queue,
1781 * and free any outstanding resources
1785 ATENABLE(s
, atp
->atp_lock
);
1788 * copyout the request data, including the protocol header
1790 for (size
=0, m
=m_head
; m
; m
= gbuf_cont(m
)) {
1791 if ((len
= gbuf_len(m
)) > buflen
)
1793 copyout((caddr_t
)gbuf_rptr(m
), (caddr_t
)&buf
[size
], len
);
1795 if ((buflen
-= len
) == 0)
1802 ATENABLE(s
, atp
->atp_lock
);
1808 _ATPgetrsp(fd
, bdsp
, err
, proc
)
1810 struct atpBDS
*bdsp
;
1815 register struct atp_state
*atp
;
1816 register struct atp_trans
*trp
;
1818 char bds
[atpBDSsize
];
1820 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) != 0)
1823 if ((gref
== 0) || ((atp
= (struct atp_state
*)gref
->info
) == 0)
1824 || (atp
->atp_flags
& ATP_CLOSING
)) {
1825 dPrintf(D_M_ATP
, D_L_ERROR
, ("ATPgetrsp: stale handle=0x%x, pid=%d\n",
1826 (u_int
) gref
, gref
->pid
));
1831 ATDISABLE(s
, atp
->atp_lock
);
1832 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
) {
1833 dPrintf(D_M_ATP
, D_L_INFO
,
1834 ("ATPgetrsp: atp:0x%x, trp:0x%x, state:%d\n",
1835 (u_int
) atp
, (u_int
) trp
, trp
->tr_state
));
1837 switch (trp
->tr_state
) {
1839 ATENABLE(s
, atp
->atp_lock
);
1840 if ((*err
= copyin((caddr_t
)bdsp
,
1841 (caddr_t
)bds
, sizeof(bds
))) != 0)
1843 atp_pack_bdsp(trp
, (struct atpBDS
*)bds
);
1844 tid
= (int)trp
->tr_tid
;
1846 copyout((caddr_t
)bds
, (caddr_t
)bdsp
, sizeof(bds
));
1851 * transaction timed out, return error
1853 ATENABLE(s
, atp
->atp_lock
);
1862 ATENABLE(s
, atp
->atp_lock
);
1869 atp_drop_req(gref
, m
)
1874 struct atp_state
*atp
;
1875 struct atp_rcb
*rcbp
;
1879 atp
= (struct atp_state
*)gref
->info
;
1881 atp
= (struct atp_state
*)atp
->atp_msgq
;
1882 ddp
= AT_DDP_HDR(m
);
1883 athp
= AT_ATP_HDR(m
);
1886 * search for the corresponding rcb
1888 ATDISABLE(s
, atp
->atp_lock
);
1889 for (rcbp
= atp
->atp_rcb
.head
; rcbp
; rcbp
= rcbp
->rc_list
.next
) {
1890 if ( (rcbp
->rc_tid
== UAS_VALUE(athp
->tid
)) &&
1891 (rcbp
->rc_socket
.node
== ddp
->src_node
) &&
1892 (rcbp
->rc_socket
.net
== NET_VALUE(ddp
->src_net
)) &&
1893 (rcbp
->rc_socket
.socket
== ddp
->src_socket
) )
1902 ATENABLE(s
, atp
->atp_lock
);