]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/atp_alloc.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / netat / atp_alloc.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
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
13 * file.
14 *
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* Modified for MP, 1996 by Tuyen Nguyen */
26/*
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.
33 */
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>
40#include <sys/proc.h>
41#include <sys/filedesc.h>
42#include <sys/fcntl.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45
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>
52
53/*### MacOSX MCLBYTE is 2048, not 4096 like AIX */
54#define TRPS_PER_BLK 16
55
56gbuf_t *atp_resource_m = 0;
57extern atlock_t atpgen_lock;
0b4e3aa0
A
58extern caddr_t atp_free_cluster_list;
59extern void atp_delete_free_clusters();
1c79356b
A
60
61struct atp_trans *atp_trans_alloc(atp)
62struct atp_state *atp;
63{
64 int s;
65 int i;
66 gbuf_t *m;
67 register struct atp_trans *trp, *trp_array;
68
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;
80 atp_resource_m = m;
81 trp_array[i].tr_list.next = atp_trans_free_list;
82 atp_trans_free_list = (struct atp_trans *)&trp_array[0];
83 }
84
85 trp = atp_trans_free_list;
86 atp_trans_free_list = trp->tr_list.next;
87 ATENABLE(s, atpgen_lock);
88 trp->tr_queue = atp;
89 trp->tr_state = TRANS_TIMEOUT;
90 trp->tr_local_node = 0;
91 ATLOCKINIT(trp->tr_lock);
92 ATEVENTINIT(trp->tr_event);
93
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));
97 return trp;
98} /* atp_trans_alloc */
99
100/*
101 * tcb free routine - if modules are waiting schedule them
102 * always called at 'lock'
103 */
104
105void atp_trans_free(trp)
106register struct atp_trans *trp;
107{
108 int s;
109
110 ATDISABLE(s, atpgen_lock);
111 trp->tr_queue = 0;
112 trp->tr_list.next = atp_trans_free_list;
113 atp_trans_free_list = trp;
114 ATENABLE(s, atpgen_lock);
115}
116
117/*
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'
121 */
122
123struct atp_rcb *atp_rcb_alloc(atp)
124struct atp_state *atp;
125{
126 register struct atp_rcb *rcbp;
127 int s;
128
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;
133 rcbp->rc_pktcnt = 0;
134 rcbp->rc_local_node = 0;
135 }
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));
139 return(rcbp);
140}
141
142/*
143 * Here we free rcbs, if required reschedule other people waiting for them
144 * always called at 'lock'
145 */
146
147void atp_rcb_free(rcbp)
148register struct atp_rcb *rcbp;
149{
150 register struct atp_state *atp;
151 register int i;
152 register int rc_state;
153 int s;
154
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",
163 0, rcbp->rc_tid,
164 rcbp->rc_socket.socket, atp->atp_socket_no));
165 return;
166 }
167 rcbp->rc_state = -1;
168 rcbp->rc_xo = 0;
169 rcbp->rc_queue = 0;
170
171 if (rcbp->rc_timestamp) {
172 extern struct atp_rcb_qhead atp_need_rel;
173
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;
178 }
179
180 if (rcbp->rc_xmt) {
181 gbuf_freem(rcbp->rc_xmt); /* *** bad free is the second mbuf in this chain *** */
182 rcbp->rc_xmt = NULL;
183 for (i=0; i < rcbp->rc_pktcnt; i++)
184 rcbp->rc_snd[i] = 0;
185 }
0b4e3aa0
A
186 if (atp_free_cluster_list)
187 atp_delete_free_clusters();
1c79356b
A
188 if (rc_state != RCB_UNQUEUED) {
189 if (rc_state == RCB_PENDING) {
190 ATP_Q_REMOVE(atp->atp_attached, rcbp, rc_list);
191 } else {
192 ATP_Q_REMOVE(atp->atp_rcb, rcbp, rc_list);
193 }
194 }
195 if (rcbp->rc_ioctl) {
196 gbuf_freem(rcbp->rc_ioctl);
197 rcbp->rc_ioctl = NULL;
198 }
199 rcbp->rc_list.next = atp_rcb_free_list;
200 atp_rcb_free_list = rcbp;
201 ATENABLE(s, atpgen_lock);
202}