]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/atp_alloc.c
53e7ae673493cdc3589d7452f1ca5c776295d6e9
[apple/xnu.git] / bsd / netat / atp_alloc.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Modified for MP, 1996 by Tuyen Nguyen */
24 /*
25 * tcb (transaction) allocation routine. If no transaction data structure
26 * is available then put the module on a queue of modules waiting
27 * for transaction structures. When a tcb is available it will be
28 * removed from this list and its write queue will be scheduled.
29 * Version 1.4 of atp_alloc.c on 89/02/09 17:53:01
30 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
31 */
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>
38 #include <sys/proc.h>
39 #include <sys/filedesc.h>
40 #include <sys/fcntl.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43
44 #include <netat/sysglue.h>
45 #include <netat/appletalk.h>
46 #include <netat/ddp.h>
47 #include <netat/debug.h>
48 #include <netat/at_pcb.h>
49 #include <netat/atp.h>
50
51 /*### MacOSX MCLBYTE is 2048, not 4096 like AIX */
52 #define TRPS_PER_BLK 16
53
54 gbuf_t *atp_resource_m = 0;
55 extern atlock_t atpgen_lock;
56 extern caddr_t atp_free_cluster_list;
57 extern void atp_delete_free_clusters();
58
59 struct atp_trans *atp_trans_alloc(atp)
60 struct atp_state *atp;
61 {
62 int s;
63 int i;
64 gbuf_t *m;
65 register struct atp_trans *trp, *trp_array;
66
67 ATDISABLE(s, atpgen_lock);
68 if (atp_trans_free_list == 0) {
69 ATENABLE(s, atpgen_lock);
70 if ((m = gbuf_alloc(TRPS_PER_BLK*sizeof(struct atp_trans),PRI_HI)) == 0)
71 return (struct atp_trans *)0;
72 bzero(gbuf_rptr(m), TRPS_PER_BLK*sizeof(struct atp_trans));
73 trp_array = (struct atp_trans *)gbuf_rptr(m);
74 for (i=0; i < TRPS_PER_BLK-1; i++)
75 trp_array[i].tr_list.next = (struct atp_trans *)&trp_array[i+1];
76 ATDISABLE(s, atpgen_lock);
77 gbuf_cont(m) = atp_resource_m;
78 atp_resource_m = m;
79 trp_array[i].tr_list.next = atp_trans_free_list;
80 atp_trans_free_list = (struct atp_trans *)&trp_array[0];
81 }
82
83 trp = atp_trans_free_list;
84 atp_trans_free_list = trp->tr_list.next;
85 ATENABLE(s, atpgen_lock);
86 trp->tr_queue = atp;
87 trp->tr_state = TRANS_TIMEOUT;
88 trp->tr_local_node = 0;
89 ATLOCKINIT(trp->tr_lock);
90 ATEVENTINIT(trp->tr_event);
91
92 dPrintf(D_M_ATP_LOW, D_L_TRACE,
93 ("atp_trans_alloc(0x%x): alloc'd trp 0x%x\n",
94 (u_int) atp, (u_int) trp));
95 return trp;
96 } /* atp_trans_alloc */
97
98 /*
99 * tcb free routine - if modules are waiting schedule them
100 * always called at 'lock'
101 */
102
103 void atp_trans_free(trp)
104 register struct atp_trans *trp;
105 {
106 int s;
107
108 ATDISABLE(s, atpgen_lock);
109 trp->tr_queue = 0;
110 trp->tr_list.next = atp_trans_free_list;
111 atp_trans_free_list = trp;
112 ATENABLE(s, atpgen_lock);
113 }
114
115 /*
116 * This routine allocates a rcb, if none are available it makes sure the
117 * the write service routine will be called when one is
118 * always called at 'lock'
119 */
120
121 struct atp_rcb *atp_rcb_alloc(atp)
122 struct atp_state *atp;
123 {
124 register struct atp_rcb *rcbp;
125 int s;
126
127 ATDISABLE(s, atpgen_lock);
128 if ((rcbp = atp_rcb_free_list) != NULL) {
129 atp_rcb_free_list = rcbp->rc_list.next;
130 rcbp->rc_queue = atp;
131 rcbp->rc_pktcnt = 0;
132 rcbp->rc_local_node = 0;
133 }
134 ATENABLE(s, atpgen_lock);
135 dPrintf(D_M_ATP_LOW, D_L_TRACE,
136 ("atp_rcb_alloc: allocated rcbp 0x%x\n", (u_int) rcbp));
137 return(rcbp);
138 }
139
140 /*
141 * Here we free rcbs, if required reschedule other people waiting for them
142 * always called at 'lock'
143 */
144
145 void atp_rcb_free(rcbp)
146 register struct atp_rcb *rcbp;
147 {
148 register struct atp_state *atp;
149 register int i;
150 register int rc_state;
151 int s;
152
153 dPrintf(D_M_ATP_LOW, D_L_TRACE,
154 ("atp_rcb_free: freeing rcbp 0x%x\n", (u_int) rcbp));
155 ATDISABLE(s, atpgen_lock);
156 atp = rcbp->rc_queue;
157 if ((rc_state = rcbp->rc_state) == -1) {
158 ATENABLE(s, atpgen_lock);
159 dPrintf(D_M_ATP, D_L_WARNING,
160 ("atp_rcb_free(%d): tid=%d,loc=%d,rem=%d\n",
161 0, rcbp->rc_tid,
162 rcbp->rc_socket.socket, atp->atp_socket_no));
163 return;
164 }
165 rcbp->rc_state = -1;
166 rcbp->rc_xo = 0;
167 rcbp->rc_queue = 0;
168
169 if (rcbp->rc_timestamp) {
170 extern struct atp_rcb_qhead atp_need_rel;
171
172 rcbp->rc_timestamp = 0;
173 ATP_Q_REMOVE(atp_need_rel, rcbp, rc_tlist);
174 rcbp->rc_tlist.prev = NULL;
175 rcbp->rc_tlist.next = NULL;
176 }
177
178 if (rcbp->rc_xmt) {
179 gbuf_freem(rcbp->rc_xmt); /* *** bad free is the second mbuf in this chain *** */
180 rcbp->rc_xmt = NULL;
181 for (i=0; i < rcbp->rc_pktcnt; i++)
182 rcbp->rc_snd[i] = 0;
183 }
184 if (atp_free_cluster_list)
185 atp_delete_free_clusters();
186 if (rc_state != RCB_UNQUEUED) {
187 if (rc_state == RCB_PENDING) {
188 ATP_Q_REMOVE(atp->atp_attached, rcbp, rc_list);
189 } else {
190 ATP_Q_REMOVE(atp->atp_rcb, rcbp, rc_list);
191 }
192 }
193 if (rcbp->rc_ioctl) {
194 gbuf_freem(rcbp->rc_ioctl);
195 rcbp->rc_ioctl = NULL;
196 }
197 rcbp->rc_list.next = atp_rcb_free_list;
198 atp_rcb_free_list = rcbp;
199 ATENABLE(s, atpgen_lock);
200 }