]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Open.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / netat / adsp_Open.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 /* adspOpen.c v01.20
29 *
30 * From v01.20 08/23/90 Mike Shoemaker for MacOS
31 * Modified for MP, 1996 by Tuyen Nguyen
32 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
33 */
34
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <machine/spl.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/filedesc.h>
43 #include <sys/fcntl.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/time.h>
48
49 #include <netat/sysglue.h>
50 #include <netat/appletalk.h>
51 #include <netat/at_pcb.h>
52 #include <netat/debug.h>
53 #include <netat/adsp.h>
54 #include <netat/adsp_internal.h>
55
56
57 /*
58 * NextCID
59 *
60 * Create a unique connection ID.
61 *
62 * INPUTS:
63 * none
64 * OUTPUTS:
65 * unique connection ID
66 */
67 unsigned short NextCID()
68 {
69 unsigned short num;
70 register CCB *queue;
71
72 while (1) {
73 num = ++adspGlobal.lastCID;
74 /* qfind_w below is in 68K assembly */
75 /* point to the first element */
76 queue = (CCB *)AT_ADSP_STREAMS;
77 while (queue) {
78 /* and scan .. */
79 if (queue->locCID == num)
80 break;
81 queue = queue->ccbLink;
82 }
83 if (queue == (CCBPtr)NULL)
84 break;
85 }
86 return num;
87 }
88
89 static byte xlateStateTbl[4] = /* The value to be given to the CCB's state. */
90 { /* indexed by ocMode */
91 sOpening, /* ocRequest */
92 sPassive, /* ocPassive */
93 sOpening, /* ocAccept */
94 sOpen /* ocEstablish */
95 };
96 static byte xlateOpenTbl[4] = /* Value to use for open state. */
97 { /* indexed by ocMode */
98 O_STATE_OPENWAIT, /* ocRequest */
99 O_STATE_LISTEN, /* ocPassive */
100 O_STATE_ESTABLISHED, /* ocAccept */
101 O_STATE_OPEN /* ocEstablish */
102 };
103
104 /*
105 * adspOpen
106 *
107 * INPUTS:
108 * --> ccbRefNum refnum of connection end
109 * --> remoteCID connection id of remote connection end
110 * --> remoteAddress internet address of remote connection end
111 * --> filterAddress filter for incoming open connection requests
112 * --> sendSeq initial send sequence number to use
113 * --> sendWindow initial size of remote end's receive buffer
114 * --> recvSeq initial receive sequence number to use
115 * --> attnSendSeq initial attention send sequence number
116 * --> attnRecvSeq initial receive sequence number
117 * --> ocMode connection opening mode
118 * --> ocMaximum maximum retries of open connection request
119 *
120 * OUTPUTS:
121 * <-- localCID connection identifier of this connection end
122 * <-- remoteCID connection id of remote connection end
123 * <-- remoteAddress
124 * <-- sendSeq
125 * <-- sendWindow
126 * <-- attnSendSeq
127 *
128 * ERRORS:
129 * errRefNum bad connection refnum
130 * errState connection end must be closed
131 * errOpening open connection attempt failed
132 * errAborted request aborted by a remove or close call
133 */
134 int adspOpen(sp, pb) /* (DSPPBPtr pb) */
135 register CCBPtr sp;
136 register struct adspcmd *pb;
137 {
138 extern int adsp_pidM[];
139
140 int ocMode;
141 register gbuf_t *mp;
142
143 if (sp == 0) {
144 pb->ioResult = errRefNum; /* Unknown refnum */
145 return EINVAL;
146 }
147
148 if ((sp->state != sClosed) ||
149 (sp->removing)) { /* The CCB must be closed */
150 pb->ioResult = errState;
151 return EALREADY;
152 }
153
154 ocMode = pb->u.openParams.ocMode; /* get a local copy of open mode */
155 if (ocMode == ocRequest)
156 adsp_pidM[pb->socket] = 0;
157
158 /*
159 * Save parameters. Fill in defaults if zero
160 */
161 if (pb->u.openParams.ocInterval)
162 sp->openInterval = pb->u.openParams.ocInterval;
163 else
164 sp->openInterval = ocIntervalDefault;
165
166 if (pb->u.openParams.ocMaximum)
167 sp->openRetrys = pb->u.openParams.ocMaximum;
168 else
169 sp->openRetrys = ocMaximumDefault;
170
171 sp->remoteAddress = *((AddrUnionPtr)&pb->u.openParams.remoteAddress);
172 /* Not used for passive */
173 /*
174 * Clear out send/receive buffers.
175 */
176 if (sp->sbuf_mb) { /* clear the send queue */
177 gbuf_freel(sp->sbuf_mb);
178 sp->sbuf_mb = 0;
179 }
180 if (sp->csbuf_mb) {
181 gbuf_freem(sp->csbuf_mb);
182 sp->csbuf_mb = 0;
183 }
184 if (sp->rbuf_mb) { /* clear the receive queue */
185 gbuf_freel(sp->rbuf_mb);
186 sp->rbuf_mb = 0;
187 }
188 if (sp->crbuf_mb) {
189 gbuf_freem(sp->crbuf_mb);
190 sp->crbuf_mb = 0;
191 }
192
193 sp->rData = 0; /* Flag both buffers as empty */
194 sp->sData = 0;
195 sp->recvQPending = 0; /* No bytes in receive queue */
196
197 /*
198 * Clear all of those pesky flags
199 */
200 sp->userFlags = 0;
201 sp->sendDataAck = 0;
202 sp->sendAttnAck = 0;
203 sp->sendAttnData = 0;
204 sp->callSend = 0;
205 sp->removing = 0;
206 sp->writeFlush = 0;
207
208 /*
209 * Reset round-trip timers
210 */
211 sp->roundTrip = sp->rtmtInterval;
212 sp->deviation = 0;
213
214 /*
215 * Reset stuff for retransmit advice packet
216 */
217 sp->badSeqCnt = 0;
218 /*
219 * Reset flow control variables
220 */
221 sp->pktSendMax = 1; /* Slow start says we should set this to 1 */
222 sp->pktSendCnt = 0;
223 sp->rbufFull = 0;
224 sp->resentData = 0;
225 sp->noXmitFlow = 0;
226 sp->waitingAck = 0;
227
228 /*
229 * Copy required information out of parameter block
230 */
231 if (ocMode == ocAccept || ocMode == ocEstablish) {
232 sp->remCID = pb->u.openParams.remoteCID;
233 sp->sendSeq = sp->firstRtmtSeq = pb->u.openParams.sendSeq;
234 sp->sendWdwSeq = sp->sendSeq + pb->u.openParams.sendWindow;
235 sp->attnSendSeq = pb->u.openParams.attnSendSeq;
236 } else { /* accept or establish */
237 sp->remCID = 0;
238 sp->sendSeq = 0;
239 sp->sendWdwSeq = 0;
240 sp->attnSendSeq = 0;
241 }
242
243 if (ocMode == ocEstablish) { /* Only set these if establish mode */
244 sp->recvSeq = pb->u.openParams.recvSeq;
245 sp->attnRecvSeq = pb->u.openParams.attnRecvSeq;
246 UAS_ASSIGN_HTON(sp->f.CID, sp->locCID); /* Preset the CID in the ADSP header */
247 /* This is done elsewhere for all other modes */
248 InsertTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer,
249 sp->probeInterval);
250 } else { /* establish */
251 /* All other modes need a CID assigned */
252 sp->locCID = NextCID();
253 sp->recvSeq = 0;
254 sp->attnRecvSeq = 0;
255 }
256
257 /*
258 * Now set the state variables for this CCB.
259 */
260
261 sp->openState = xlateOpenTbl[ocMode-ocRequest];
262 sp->state = xlateStateTbl[ocMode-ocRequest];
263
264 if (ocMode == ocEstablish) { /* For establish call, we're done */
265 pb->ioResult = 0;
266 adspioc_ack(0, pb->ioc, pb->gref);
267 return 0;
268 }
269
270 pb->qLink = 0; /* Clear link field before putting on queue */
271 mp = gbuf_copym(pb->mp); /* Save parameter block to match later */
272
273 if (mp == 0) {
274 pb->ioResult = errDSPQueueSize;
275 return ENOBUFS;
276 }
277 pb->ioResult = 1; /* not open -> not done */
278 adspioc_ack(0, pb->ioc, pb->gref); /* release user */
279 sp->opb = (struct adspcmd *)gbuf_rptr(mp);
280 sp->opb->ioc = 0; /* unlink saved pb from ioctl block */
281 sp->opb->mp = mp;
282
283 /*
284 * For request & accept, need to send a packet
285 */
286 if ((ocMode == ocRequest) || (ocMode == ocAccept)) {
287 sp->sendCtl |= (1 << (ocMode == ocRequest ?
288 ADSP_CTL_OREQ : ADSP_CTL_OREQACK));
289 CheckSend(sp);
290 }
291 return 0;
292 }
293
294 int adspMode(pb)
295 register struct adspcmd *pb;
296 {
297 return pb->u.openParams.ocMode;
298 }