2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Modified for MP, 1996 by Tuyen Nguyen */
27 * tcb (transaction) allocation routine. If no transaction data structure
28 * is available then put the module on a queue of modules waiting
29 * for transaction structures. When a tcb is available it will be
30 * removed from this list and its write queue will be scheduled.
31 * Version 1.4 of atp_alloc.c on 89/02/09 17:53:01
32 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
34 #include <sys/errno.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <machine/spl.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/filedesc.h>
42 #include <sys/fcntl.h>
44 #include <sys/socket.h>
46 #include <netat/sysglue.h>
47 #include <netat/appletalk.h>
48 #include <netat/ddp.h>
49 #include <netat/debug.h>
50 #include <netat/at_pcb.h>
51 #include <netat/atp.h>
53 /*### MacOSX MCLBYTE is 2048, not 4096 like AIX */
54 #define TRPS_PER_BLK 16
56 gbuf_t
*atp_resource_m
= 0;
57 extern atlock_t atpgen_lock
;
58 extern caddr_t atp_free_cluster_list
;
59 extern void atp_delete_free_clusters();
61 struct atp_trans
*atp_trans_alloc(atp
)
62 struct atp_state
*atp
;
67 register struct atp_trans
*trp
, *trp_array
;
69 ATDISABLE(s
, atpgen_lock
);
70 if (atp_trans_free_list
== 0) {
71 ATENABLE(s
, atpgen_lock
);
72 if ((m
= gbuf_alloc(TRPS_PER_BLK
*sizeof(struct atp_trans
),PRI_HI
)) == 0)
73 return (struct atp_trans
*)0;
74 bzero(gbuf_rptr(m
), TRPS_PER_BLK
*sizeof(struct atp_trans
));
75 trp_array
= (struct atp_trans
*)gbuf_rptr(m
);
76 for (i
=0; i
< TRPS_PER_BLK
-1; i
++)
77 trp_array
[i
].tr_list
.next
= (struct atp_trans
*)&trp_array
[i
+1];
78 ATDISABLE(s
, atpgen_lock
);
79 gbuf_cont(m
) = atp_resource_m
;
81 trp_array
[i
].tr_list
.next
= atp_trans_free_list
;
82 atp_trans_free_list
= (struct atp_trans
*)&trp_array
[0];
85 trp
= atp_trans_free_list
;
86 atp_trans_free_list
= trp
->tr_list
.next
;
87 ATENABLE(s
, atpgen_lock
);
89 trp
->tr_state
= TRANS_TIMEOUT
;
90 trp
->tr_local_node
= 0;
91 ATLOCKINIT(trp
->tr_lock
);
92 ATEVENTINIT(trp
->tr_event
);
94 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
95 ("atp_trans_alloc(0x%x): alloc'd trp 0x%x\n",
96 (u_int
) atp
, (u_int
) trp
));
98 } /* atp_trans_alloc */
101 * tcb free routine - if modules are waiting schedule them
102 * always called at 'lock'
105 void atp_trans_free(trp
)
106 register struct atp_trans
*trp
;
110 ATDISABLE(s
, atpgen_lock
);
112 trp
->tr_list
.next
= atp_trans_free_list
;
113 atp_trans_free_list
= trp
;
114 ATENABLE(s
, atpgen_lock
);
118 * This routine allocates a rcb, if none are available it makes sure the
119 * the write service routine will be called when one is
120 * always called at 'lock'
123 struct atp_rcb
*atp_rcb_alloc(atp
)
124 struct atp_state
*atp
;
126 register struct atp_rcb
*rcbp
;
129 ATDISABLE(s
, atpgen_lock
);
130 if ((rcbp
= atp_rcb_free_list
) != NULL
) {
131 atp_rcb_free_list
= rcbp
->rc_list
.next
;
132 rcbp
->rc_queue
= atp
;
134 rcbp
->rc_local_node
= 0;
136 ATENABLE(s
, atpgen_lock
);
137 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
138 ("atp_rcb_alloc: allocated rcbp 0x%x\n", (u_int
) rcbp
));
143 * Here we free rcbs, if required reschedule other people waiting for them
144 * always called at 'lock'
147 void atp_rcb_free(rcbp
)
148 register struct atp_rcb
*rcbp
;
150 register struct atp_state
*atp
;
152 register int rc_state
;
155 dPrintf(D_M_ATP_LOW
, D_L_TRACE
,
156 ("atp_rcb_free: freeing rcbp 0x%x\n", (u_int
) rcbp
));
157 ATDISABLE(s
, atpgen_lock
);
158 atp
= rcbp
->rc_queue
;
159 if ((rc_state
= rcbp
->rc_state
) == -1) {
160 ATENABLE(s
, atpgen_lock
);
161 dPrintf(D_M_ATP
, D_L_WARNING
,
162 ("atp_rcb_free(%d): tid=%d,loc=%d,rem=%d\n",
164 rcbp
->rc_socket
.socket
, atp
->atp_socket_no
));
171 if (rcbp
->rc_timestamp
) {
172 extern struct atp_rcb_qhead atp_need_rel
;
174 rcbp
->rc_timestamp
= 0;
175 ATP_Q_REMOVE(atp_need_rel
, rcbp
, rc_tlist
);
176 rcbp
->rc_tlist
.prev
= NULL
;
177 rcbp
->rc_tlist
.next
= NULL
;
181 gbuf_freem(rcbp
->rc_xmt
); /* *** bad free is the second mbuf in this chain *** */
183 for (i
=0; i
< rcbp
->rc_pktcnt
; i
++)
186 if (atp_free_cluster_list
)
187 atp_delete_free_clusters();
188 if (rc_state
!= RCB_UNQUEUED
) {
189 if (rc_state
== RCB_PENDING
) {
190 ATP_Q_REMOVE(atp
->atp_attached
, rcbp
, rc_list
);
192 ATP_Q_REMOVE(atp
->atp_rcb
, rcbp
, rc_list
);
195 if (rcbp
->rc_ioctl
) {
196 gbuf_freem(rcbp
->rc_ioctl
);
197 rcbp
->rc_ioctl
= NULL
;
199 rcbp
->rc_list
.next
= atp_rcb_free_list
;
200 atp_rcb_free_list
= rcbp
;
201 ATENABLE(s
, atpgen_lock
);