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