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