]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Read.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / netat / adsp_Read.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 *
27 * dspRead.c
28 *
29 * From v01.17 08/22/90 mbs
30 * Modified for MP, 1996 by Tuyen Nguyen
31 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
32 */
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 #include <sys/socketvar.h>
46
47 #include <netat/sysglue.h>
48 #include <netat/appletalk.h>
49 #include <netat/at_pcb.h>
50 #include <netat/debug.h>
51 #include <netat/adsp.h>
52 #include <netat/adsp_internal.h>
53
54 /*
55 * CheckReadQueue
56 *
57 * Checks to see if there is any data in the receive queue. If there
58 * is data, a pb and the data are queued to the user.
59 *
60 *
61 */
62 extern int adsp_check;
63
64 int CheckReadQueue(sp) /* (CCBPtr sp) */
65 register CCBPtr sp;
66 {
67 register struct adspcmd *pb;
68 int s;
69 unsigned short cnt;
70 char eom = 0;
71 register gbuf_t *mp;
72 register gbuf_t *tmp;
73 gref_t *gref;
74
75 dPrintf(D_M_ADSP, D_L_TRACE, ("CheckReadQueue: sp=0x%x\n", (unsigned)sp));
76 KERNEL_DEBUG(DBG_ADSP_READ, 0, sp, sp->rbuf_mb, sp->rpb, sp->delay);
77 trace_mbufs(D_M_ADSP_LOW, " bCQR m", sp->rbuf_mb);
78 ATDISABLE(s, sp->lock);
79
80 while (sp->rData && (pb = sp->rpb)) { /* have data */
81 dPrintf(D_M_ADSP, D_L_TRACE,
82 (" pb=0x%x, gref=0x%x, ioc=0x%x, reqCount=%d (have data)\n",
83 pb, pb->gref, pb->ioc, pb->u.ioParams.reqCount));
84 KERNEL_DEBUG(DBG_ADSP_READ, 1, pb, pb->gref, pb->ioc, pb->u.ioParams.reqCount);
85 if (pb->u.ioParams.reqCount == 0) {
86 pb->ioResult = 0;
87 sp->rpb = pb->qLink;
88 if (pb->ioc) {
89 KERNEL_DEBUG(DBG_ADSP_READ, 2, pb, pb->gref, pb->ioc, 0);
90 adspioc_ack(0, pb->ioc, pb->gref);
91 } else {
92 KERNEL_DEBUG(DBG_ADSP_READ, 3, pb, pb->gref, 0, 0);
93 completepb(sp, pb);
94 }
95 continue;
96 }
97
98 /* take the first packet off of sp->rbuf_mb or sp->crbuf_mb */
99 if (mp = sp->rbuf_mb) { /* Get header for oldest data */
100 KERNEL_DEBUG(DBG_ADSP_READ, 4, pb, mp, gbuf_msgsize(mp), gbuf_next(mp));
101 sp->rbuf_mb = gbuf_next(mp);
102 gbuf_next(mp) = 0;
103 eom = 1;
104 } else if (mp = sp->crbuf_mb) {
105 KERNEL_DEBUG(DBG_ADSP_READ, 5, pb, mp, gbuf_msgsize(mp), gbuf_next(mp));
106 sp->crbuf_mb = 0;
107 eom = 0;
108 }
109
110 /* Get the first (reqCount-actCount) bytes and tack them onto
111 the end of pb->mp. If eom is set, put the remainder of the
112 data onto the front of sp->rbuf_mb, otherwise sp->crbuf_mb. */
113 cnt = gbuf_msgsize(mp); /* # of data bytes in it. */
114 if (cnt > (unsigned short)(pb->u.ioParams.reqCount - pb->u.ioParams.actCount)) {
115 cnt = pb->u.ioParams.reqCount - pb->u.ioParams.actCount;
116 /* m_split returns the tail */
117 if (!(tmp = (gbuf_t *)m_split(mp, cnt, M_DONTWAIT))) {
118 cnt = 0;
119 tmp = mp;
120 }
121 if (eom) {
122 gbuf_next(tmp) = sp->rbuf_mb;
123 sp->rbuf_mb = tmp;
124 eom = 0;
125 } else
126 sp->crbuf_mb = tmp;
127 }
128 if (cnt) {
129 pb->u.ioParams.actCount += cnt;
130 gbuf_linkb(pb->mp, mp);
131 }
132
133 pb->u.ioParams.eom = eom;
134 /*
135 * Now clean up receive buffer to remove all of the data
136 * we just copied
137 */
138 if ((sp->rbuf_mb == 0) &&
139 (sp->crbuf_mb == 0)) /* no more data blocks */
140 sp->rData = 0;
141 /*
142 * If we've filled the parameter block, unlink it from read
143 * queue and complete it. We also need to do this if the connection
144 * is closed && there is no more stuff to read.
145 */
146 if (eom || (pb->u.ioParams.actCount >= pb->u.ioParams.reqCount) ||
147 ((sp->state == sClosed) && (!sp->rData)) ) {
148 /* end of message, message is full, connection
149 * is closed and all data has been delivered,
150 * or we are not to "delay" data delivery.
151 */
152 pb->ioResult = 0;
153 sp->rpb = pb->qLink; /* dequeue request */
154 if (pb->ioc) { /* data to be delivered at the time of the */
155 mp = gbuf_cont(pb->mp); /* ioctl call */
156 gbuf_cont(pb->mp) = 0;
157 gref = (gref_t *)pb->gref;
158 adspioc_ack(0, pb->ioc, pb->gref);
159 dPrintf(D_M_ADSP, D_L_TRACE, (" (pb->ioc) mp=%x\n", mp));
160 KERNEL_DEBUG(DBG_ADSP_READ, 0x0A, pb, mp,
161 gbuf_next(mp), gbuf_cont(mp));
162 SndMsgUp(gref, mp);
163 dPrintf(D_M_ADSP, D_L_TRACE,
164 (" (data) size req=%d\n", pb->u.ioParams.actCount));
165 KERNEL_DEBUG(DBG_ADSP_READ, 0x0B, pb, pb->ioc,
166 pb->u.ioParams.reqCount, pb->u.ioParams.actCount);
167 } else { /* complete an queued async request */
168 KERNEL_DEBUG(DBG_ADSP_READ, 0x0C, pb, sp,
169 pb->u.ioParams.actCount, sp->delay);
170 completepb(sp, pb);
171 }
172 }
173 } /* while */
174
175 if (pb = sp->rpb) { /* if there is an outstanding request */
176 dPrintf(D_M_ADSP, D_L_TRACE,
177 (" pb=0x%x, ioc=0x%x, reqCount=%d (no more data)\n",
178 pb, pb->ioc, pb->u.ioParams.reqCount));
179 KERNEL_DEBUG(DBG_ADSP_READ, 0x0D, pb, pb->ioc,
180 pb->u.ioParams.reqCount, pb->u.ioParams.actCount);
181
182 if (sp->state == sClosed) {
183 while (pb) {
184 KERNEL_DEBUG(DBG_ADSP_READ, 0x0E, pb, sp, pb->ioc, 0);
185 pb->ioResult = 0;
186 pb->u.ioParams.actCount = 0;
187 pb->u.ioParams.eom = 0;
188 sp->rpb = pb->qLink;
189 if (pb->ioc) {
190 adspioc_ack(0, pb->ioc, pb->gref);
191 } else {
192 completepb(sp, pb);
193 }
194 pb = sp->rpb;
195 }
196 } else if (pb->ioc) { /* if request not complete and this
197 * is an active ioctl, release user */
198 sp->rpb = pb->qLink;
199 pb->ioResult = 1;
200 tmp = gbuf_cont(pb->mp); /* detatch perhaps delayed data */
201 gbuf_cont(pb->mp) = 0;
202 if (mp = gbuf_copym(pb->mp)) { /* otherwise, duplicate user request */
203 KERNEL_DEBUG(DBG_ADSP_READ, 0x0F, pb, sp, pb->mp, 0);
204 adspioc_ack(0, pb->ioc, pb->gref); /* release user */
205 pb = (struct adspcmd *)gbuf_rptr(mp); /* get new parameter block */
206 pb->ioc = 0;
207 pb->mp = mp;
208 gbuf_cont(pb->mp) = tmp; /* reattach data */
209 pb->qLink = sp->rpb; /* requeue the duplicate at the head */
210 sp->rpb = pb;
211 } else { /* there is no data left, but no space
212 * to duplicate the parameter block, so
213 * put what must be a non EOM message
214 * back on the current receive queue, and
215 * error out the user
216 */
217 KERNEL_DEBUG(DBG_ADSP_READ, 0x10, pb, sp, pb->mp, 0);
218 if (tmp) {
219 sp->crbuf_mb = tmp;
220 sp->rData = 1;
221 }
222 pb->ioResult = errDSPQueueSize;
223 adspioc_ack(ENOBUFS, pb->ioc, pb->gref);
224 }
225 }
226 }
227 /*
228 * The receive window has opened. If was previously closed, then we
229 * need to notify the other guy that we now have room to receive more
230 * data. But, in order to cut down on lots of small data packets,
231 * we'll wait until the recieve buffer is /14 empy before telling
232 * him that there's room in our receive buffer.
233 */
234 if (sp->rbufFull && (CalcRecvWdw(sp) > (sp->rbuflen >> 2))) {
235 sp->rbufFull = 0;
236 sp->sendDataAck = 1;
237 sp->callSend = 1;
238 }
239 ATENABLE(s, sp->lock);
240
241 KERNEL_DEBUG(DBG_ADSP_READ, 0x11, sp, 0, 0, 0);
242 trace_mbufs(D_M_ADSP_LOW, " eCQR m", sp->rbuf_mb);
243 return 0;
244 }
245
246 /*
247 * CheckAttn
248 *
249 * Checks to see if there is any attention data and passes the data back
250 * in the passed in pb.
251 *
252 * INPUTS:
253 * sp
254 * pb
255 *
256 * OUTPUTS:
257 *
258 */
259 int CheckAttn(sp, pb) /* (CCBPtr sp) */
260 register CCBPtr sp;
261 register struct adspcmd *pb;
262 {
263 int s;
264 gbuf_t *mp;
265 gref_t *gref;
266
267 dPrintf(D_M_ADSP, D_L_TRACE,
268 ("CheckAttn: sp=0x%x, pb=0x%x\n", (unsigned)sp, (unsigned)pb));
269
270 ATDISABLE(s, sp->lock);
271 if (mp = sp->attn_mb) {
272
273 /*
274 * Deliver the attention data to the user.
275 */
276 gref = (gref_t *)pb->gref;
277 pb->u.attnParams.attnSize = sp->attnSize;
278 pb->u.attnParams.attnCode = sp->attnCode;
279 if (!sp->attnSize) {
280 gbuf_freem(mp);
281 mp = 0;
282 }
283 sp->userFlags &= ~eAttention;
284 /*
285 * Now clean up receive buffer to remove all of the data
286 * we just copied
287 */
288 sp->attn_mb = 0;
289 pb->ioResult = 0;
290 } else {
291 /*
292 * No data...
293 */
294 pb->u.attnParams.attnSize = 0;
295 pb->u.attnParams.attnCode = 0;
296 pb->ioResult = 1; /* not done */
297 }
298 adspioc_ack(0, pb->ioc, pb->gref);
299 if (mp) {
300 SndMsgUp(gref, mp);
301 }
302 ATENABLE(s, sp->lock);
303 return 0;
304 }
305
306 /*
307 * adspRead
308 *
309 * INPUTS:
310 * --> sp stream pointer
311 * --> pb user request parameter block
312 *
313 * OUTPUTS:
314 * <-- actCount actual number of bytes read
315 * <-- eom one if end-of-message, zero otherwise
316 *
317 * ERRORS:
318 * errRefNum bad connection refnum
319 * errState
320 * errFwdReset read terminated by forward reset
321 * errAborted request aborted by Remove or Close call
322 */
323 int adspRead(sp, pb) /* (DSPPBPtr pb) */
324 register CCBPtr sp;
325 register struct adspcmd *pb;
326 {
327 register gbuf_t *mp;
328 int s;
329
330 dPrintf(D_M_ADSP, D_L_TRACE,
331 ("adspRead: sp=0x%x, pb=0x%x\n", (unsigned)sp, (unsigned)pb));
332
333 KERNEL_DEBUG(DBG_ADSP_READ, 0x12, sp, pb, sp->state, sp->rData);
334
335 if (sp == 0) {
336 pb->ioResult = errRefNum;
337 return EINVAL;
338 }
339
340 /*
341 * It's OK to read on a closed, or closing session
342 */
343 ATDISABLE(s, sp->lock);
344 if (sp->state != sOpen && sp->state != sClosing && sp->state != sClosed) {
345 ATENABLE(s, sp->lock);
346 pb->ioResult = errState;
347 return EINVAL;
348 }
349 if (sp->rData && (sp->rpb == 0)) { /* if data, and no queue of pbs */
350 qAddToEnd(&sp->rpb, pb); /* deliver data to user directly */
351 ATENABLE(s, sp->lock);
352 CheckReadQueue(sp);
353 } else if ((pb->u.ioParams.reqCount == 0) && (sp->rpb == 0)) {
354 /* empty read */
355 ATENABLE(s, sp->lock);
356 pb->ioResult = 0;
357 adspioc_ack(0, pb->ioc, pb->gref);
358 return 0;
359 } else {
360 pb->ioResult = 1;
361 if (mp = gbuf_copym(pb->mp)) { /* otherwise, duplicate user request */
362 adspioc_ack(0, pb->ioc, pb->gref); /* release user */
363 pb = (struct adspcmd *)gbuf_rptr(mp); /* get new parameter block */
364 pb->ioc = 0;
365 pb->mp = mp;
366 qAddToEnd(&sp->rpb, pb); /* and queue it for later */
367 ATENABLE(s, sp->lock);
368 } else {
369 ATENABLE(s, sp->lock);
370 pb->ioResult = errDSPQueueSize;
371 return ENOBUFS;
372 }
373 }
374
375 if (sp->callSend) {
376 CheckSend(sp); /* If recv window opened, we might */
377 /* send an unsolicited ACK. */
378 }
379 return 0;
380 }
381
382 /*
383 * dspReadAttention
384 *
385 * INPUTS:
386 * --> sp stream pointer
387 * --> pb user request parameter block
388 *
389 * OUTPUTS:
390 * <-- NONE
391 *
392 * ERRORS:
393 * errRefNum bad connection refnum
394 * errState connection is not in the right state
395 */
396 int adspReadAttention(sp, pb) /* (DSPPBPtr pb) */
397 register CCBPtr sp;
398 register struct adspcmd *pb;
399 {
400 dPrintf(D_M_ADSP, D_L_TRACE,
401 ("adspReadAttention: sp=0x%x, pb=0x%x\n", (unsigned)sp, (unsigned)pb));
402 if (sp == 0) {
403 pb->ioResult = errRefNum;
404 return EINVAL;
405 }
406
407 /*
408 * It's OK to read on a closed, or closing session
409 */
410 if (sp->state != sOpen && sp->state != sClosing && sp->state != sClosed) {
411 pb->ioResult = errState;
412 return EINVAL;
413 }
414
415 CheckAttn(sp, pb); /* Anything in the attention queue */
416 CheckReadQueue(sp); /* check to see if receive window has opened */
417 if (sp->callSend) {
418 CheckSend(sp); /* If recv window opened, we might */
419 /* send an unsolicited ACK. */
420 }
421 return 0;
422 } /* adspReadAttention */