]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/adsp_Control.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / bsd / netat / adsp_Control.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1990, 1995-1998 Apple Computer, Inc.
30 * All Rights Reserved.
31 */
32
33/* Control.c
34 * From Mike Shoemaker v01.25 07/02/90 for MacOS
35 * 09/07/95 - Modified for performance (Tuyen Nguyen)
36 * Modified for MP, 1996 by Tuyen Nguyen
37 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
38 */
39
40#include <sys/errno.h>
41#include <sys/types.h>
42#include <sys/param.h>
43#include <machine/spl.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/proc.h>
47#include <sys/filedesc.h>
48#include <sys/fcntl.h>
49#include <sys/mbuf.h>
50#include <sys/time.h>
51#include <sys/socket.h>
52
53#include <netat/sysglue.h>
54#include <netat/appletalk.h>
55#include <netat/ddp.h>
56#include <netat/at_pcb.h>
57#include <netat/debug.h>
58#include <netat/adsp.h>
59#include <netat/adsp_internal.h>
60
61/* # of additional ticks to add to any timer that we're queuing up. For
62 * very short delays (1 and 2), the timer fires before the transmit
63 * even takes place */
64#define TX_DLY 2
65
66int adsp_window = 1;
67
68/*
69 * CalcRecvWdw
70 *
71 * INPUTS:
72 * sp ADSP Stream
73 * OUTPUTS:
74 * # of bytes in avail in local receive queue
75 */
76int CalcRecvWdw(sp) /* (CCBPtr sp) */
77 CCBPtr sp;
78{
79 int bytes;
80
81 bytes = calcRecvQ(sp);
82 bytes = sp->rbuflen - bytes; /* get what is left */
83
84 if (bytes <= 16) { /* %%% this should be zero */
85 sp->rbufFull = 1; /* Save flag that our recv buf is full */
86 return 0;
87 }
88 else
89 return ((bytes+bytes+bytes) >> 2) + 1; /* %%% */
90}
91
92calcRecvQ(sp)
93 CCBPtr sp;
94{
95 int bytes = 0;
96#ifdef AT_Socket
97 register struct mbuf *m, *p;
98
99 if (((sp->gref)->so)->so_rcv.sb_mb)
100 for (p = ((sp->gref)->so)->so_rcv.sb_mb; p; p = p->m_nextpkt)
101 for (m = p; m; m = m->m_next)
102 bytes += m->m_len;
103#else
104 register gbuf_t *mb;
105
106 if (sp->rData) { /* There is data in buffer */
107 if (mb = sp->rbuf_mb) {
108 do {
109 bytes += gbuf_msgsize(mb);
110 mb = gbuf_next(mb);
111 } while (mb);
112 }
113 if (mb = sp->crbuf_mb)
114 bytes += gbuf_msgsize(mb);
115 }
116#endif
117 return bytes;
118}
119
120/*
121 * CheckSend
122 *
123 * Check to see if the transmit PB is available and if there is anything
124 * to transmit. Start off any pending transmit.
125 *
126 * Normally called from the write completion routine
127 *
128 * INPUTS:
129 * sp Connection control block
130 * OUTPUTS:
131 * true if sent a packet
132 */
133void CheckSend(sp) /* (CCBPtr sp) */
134 register CCBPtr sp;
135{
136 int i;
137 int attnMsg; /* True if attention message */
1c79356b
A
138 register gbuf_t *mp; /* send message block */
139#ifdef notdef
140 register gbuf_t *tmp;
141 u_char current;
142#endif
143 char *dp; /* a data pointer */
144 int use_attention_code;
145 int len; /* length used in allocd mblk */
146 int datalen; /* amount of data attached to mblk */
147 gbuf_t *mprev, *mlist = 0;
148
149top:
150
151 if (sp->state == sClosed)
152 return;
153
154 /* get a message block to hold DDP and
155 * ADSP headers + 2 bytes of attention
156 * code if necessary */
157 if ((mp = gbuf_alloc(AT_WR_OFFSET + DDPL_FRAME_LEN + ADSP_FRAME_LEN + ADSP_OPEN_FRAME_LEN + 2,
158 PRI_LO)) == 0) {
159 if (mlist)
160 gbuf_freel(mlist);
161 return; /* can't get buffers... do nothing! */
162 }
1c79356b
A
163 sp->callSend = 0; /* Clear flag */
164 use_attention_code = 0;
165 len = 0;
166 datalen = 0;
167
168 gbuf_rinc(mp,AT_WR_OFFSET);
169 gbuf_wset(mp,DDPL_FRAME_LEN); /* leave room for DDP header */
170
171 if (sp->sendCtl) {
91447636 172 short mask = 0;
1c79356b
A
173
174 i = sp->sendCtl; /* get local copy bitmap of */
175 /* which ctl packets to send. */
176 attnMsg = 0;
177
178 if (i & 0x1E) /* One of the open ctrl packets */
179 {
180
181 /* point past ADSP header (no attention) */
182 dp = ((char *) gbuf_wptr(mp)) + ADSP_FRAME_LEN;
89b3af67 183 UAL_ASSIGN_HTON(sp->f.pktFirstByteSeq, sp->firstRtmtSeq);
1c79356b 184
89b3af67
A
185 UAS_ASSIGN_HTON(sp->of.version, netw(0x0100)); /* Fill in open connection parms */
186 UAS_ASSIGN_HTON(sp->of.dstCID, sp->remCID); /* Destination CID */
187 UAL_ASSIGN_HTON(sp->of.pktAttnRecvSeq, sp->attnRecvSeq);
1c79356b
A
188 bcopy((caddr_t) &sp->of, (caddr_t) dp, ADSP_OPEN_FRAME_LEN);
189 len += ADSP_OPEN_FRAME_LEN;
190
191 if (i & B_CTL_OREQ) {
89b3af67 192 UAS_ASSIGN_HTON(sp->f.CID, sp->locCID);
1c79356b
A
193 mask = B_CTL_OREQ;
194 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_OREQ;
195 } else if (i & B_CTL_OACK) {
89b3af67 196 UAS_ASSIGN_HTON(sp->f.CID, sp->locCID);
1c79356b
A
197 mask = B_CTL_OACK;
198 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_OACK;
199 } else if (i & B_CTL_OREQACK) {
89b3af67 200 UAS_ASSIGN_HTON(sp->f.CID, sp->locCID);
1c79356b
A
201 mask = B_CTL_OREQACK;
202 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_OREQACK;
203 } else /* Deny */
204 {
205 UAS_ASSIGN(sp->f.CID, 0);
206 mask = B_CTL_ODENY;
207 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_ODENY;
208 UAL_ASSIGN(sp->f.pktFirstByteSeq, 0);
209 }
210
211 if (i & (B_CTL_OREQ | B_CTL_OREQACK))
212 /* Need to start up a timer for it */
213 {
214 /* It's possible that we've received a duplicate
215 * open request. In this case, there will already be
216 * a timer queued up for the request+ack
217 * packet we sent the first time. So remove the timer
218 * and start another.
219 */
220 RemoveTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer);
221 InsertTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer,
222 sp->openInterval+1);
223 }
224 } else {
225 /* seq # of next byte to send */
89b3af67 226 UAL_ASSIGN_HTON(sp->f.pktFirstByteSeq, sp->sendSeq);
1c79356b
A
227
228 if (i & B_CTL_CLOSE) {
229 sp->state = sClosed; /* Now we're closed */
230 mask = B_CTL_CLOSE;
231 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_CLOSE;
232 } else if (i & B_CTL_PROBE) {
233 mask = B_CTL_PROBE;
234 sp->f.descriptor =
235 ADSP_CONTROL_BIT | ADSP_CTL_PROBE | ADSP_ACK_REQ_BIT;
236 } else if (i & B_CTL_FRESET) {
237 mask = B_CTL_FRESET;
238 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_FRESET;
239 InsertTimerElem(&adspGlobal.fastTimers,
240 &sp->ResetTimer, sp->rtmtInterval+TX_DLY);
241 } else if (i & B_CTL_FRESETACK) {
242 mask = B_CTL_FRESETACK;
243 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_FRESET_ACK;
244 }
245 else if (i & B_CTL_RETRANSMIT) {
246 mask = B_CTL_RETRANSMIT;
247 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_RETRANSMIT;
248 }
249 else {
250 dPrintf(D_M_ADSP, D_L_ERROR, ("CheckSend: Control bit error\n"));
251 }
252 } /* non open control packet */
253
254 sp->sendCtl &= ~mask;
255 goto sendit;
256 } /* send control packet */
257
258 if (sp->sendAttnData) /* Send attn ready to go? */
259 {
260 sp->sendAttnData = 0; /* Clear Flags */
261 if (sp->sapb) {
262 sp->sendAttnAck = 0; /* This will also do an Attn Ack */
263
264 attnMsg = 1;
265 sp->f.descriptor = ADSP_ATTENTION_BIT | ADSP_ACK_REQ_BIT;
266 if (gbuf_cont(sp->sapb->mp)) {
267 gbuf_cont(mp) = gbuf_dupm(gbuf_cont(sp->sapb->mp));
268 /* Major hack here. The ADSP Attn code is butted up against
269 * the end of the adsp packet header, and the length is
270 * increased by 2. (There is a pad field behind the adsp
271 * header in the CCB just for this purpose.)
272 */
273 }
274 use_attention_code++;
275
276 sp->f.data[0] = high(sp->sapb->u.attnParams.attnCode);
277 sp->f.data[1] = low(sp->sapb->u.attnParams.attnCode);
278 InsertTimerElem(&adspGlobal.fastTimers, &sp->AttnTimer,
279 sp->rtmtInterval+TX_DLY);
280 goto sendit;
281 }
282 } /* attn data */
283
284 if (sp->sendAttnAck) /* Send attn ack ready to go? */
285 {
286 attnMsg = 1;
287 sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_ATTENTION_BIT;
288 sp->sendAttnAck = 0;
289 goto sendit;
290 } /* attn ack */
291
292 if ((sp->state == sOpen || sp->state == sClosing) && /* Correct state */
293 (!sp->waitingAck) && /* not waiting for an ACK */
294 (sp->sData) && /* have data to send */
295 (GTE(sp->sendWdwSeq,sp->sendSeq)) && /* he has room to accept it */
296 (sp->pktSendCnt < sp->pktSendMax)) /* haven't sent too many pkts
297 * in a row. */
298 {
299 attnMsg = 0;
300 if (datalen = attachData(sp, mp)) /* attach data to mp */
301 goto sendit; /* if successful, sendit */
302 }
303
304 if (sp->sendDataAck) {
89b3af67 305 UAL_ASSIGN_HTON(sp->f.pktFirstByteSeq, sp->sendSeq); /* seq # of next byte */
1c79356b
A
306 attnMsg = 0;
307 sp->f.descriptor = ADSP_CONTROL_BIT;
308 goto sendit;
309 }
310
311 /*
312 * Nothing left to do...
313 */
314 if (mp)
315 gbuf_freem(mp);
1c79356b
A
316 if (mlist)
317 adsp_sendddp(sp, mlist, 0, &sp->remoteAddress, DDP_ADSP);
318 return;
319
320sendit:
321
322 if (attnMsg) {
89b3af67
A
323 UAL_ASSIGN_HTON(sp->f.pktFirstByteSeq, sp->attnSendSeq);
324 UAL_ASSIGN_HTON(sp->f.pktNextRecvSeq, sp->attnRecvSeq);
1c79356b
A
325 UAS_ASSIGN(sp->f.pktRecvWdw, 0); /* Always zero in attn pkt */
326 } else {
327 sp->sendDataAck = 0;
89b3af67
A
328 UAL_ASSIGN_HTON(sp->f.pktNextRecvSeq, sp->recvSeq);
329 UAS_ASSIGN_HTON(sp->f.pktRecvWdw, CalcRecvWdw(sp));
1c79356b
A
330 }
331 if (use_attention_code) {
332 bcopy((caddr_t) &sp->f, (caddr_t) gbuf_wptr(mp), ADSP_FRAME_LEN + 2);
333 len += ADSP_FRAME_LEN + 2;
334 } else {
335 bcopy((caddr_t) &sp->f, (caddr_t) gbuf_wptr(mp), ADSP_FRAME_LEN);
336 len += ADSP_FRAME_LEN;
337 }
338 gbuf_winc(mp,len); /* update mblk length */
339 if (mlist)
340 gbuf_next(mprev) = mp;
341 else
342 mlist = mp;
343 mprev = mp;
344
345 if (sp->state == sClosed) { /* must have sent a close advice */
346 /* send header + data */
1c79356b
A
347 adsp_sendddp(sp, mlist, 0, &sp->remoteAddress, DDP_ADSP);
348 DoClose(sp, 0, -1); /* complete close! */
349 return;
350 }
1c79356b
A
351 if (sp->state == sClosing) /* See if we were waiting on this write */
352 CheckOkToClose(sp);
353 goto top;
354}
355
356/*
357 * completepb delivers a paramater block with all its appropriate fields
358 * set back to the user.
359 *
360 * The assumptions here are that the PB is not linked to any queue,
361 * that the fields including ioResult are set, and that the
362 * kernel is no longer interested in the mblks that may or
363 * maynot be linked to this pb.
364 */
365void completepb(sp, pb)
366 register CCBPtr sp;
367 register struct adspcmd *pb;
368{
369 if (sp->gref && (sp->gref->info == (caddr_t)sp->sp_mp)) {
370 if (gbuf_len(pb->mp) > sizeof(struct adspcmd))
371 gbuf_wset(pb->mp,sizeof(struct adspcmd));
372 SndMsgUp(sp->gref, pb->mp);
373 NotifyUser(sp);
374 } else
375 gbuf_freem(pb->mp);
376}
377
378attachData(sp, mp)
379 register CCBPtr sp;
380 register gbuf_t *mp;
381{
382 int seq;
383 int cnt;
384 char eom = 0;
385 int bsize;
386 int diff;
387 char sendAckReq;
388 int partial = 0; /* flag for a partial send */
389 int tcnt = 0;
390 register gbuf_t *smp; /* send data message block */
391 register gbuf_t *psmp; /* previous message block */
392
393 sendAckReq = 0;
394
395 if (LT(sp->sendSeq, sp->firstRtmtSeq)) /* Sanity check on send seq */
396 sp->sendSeq = sp->firstRtmtSeq; /* seq must be oldest in buffer. */
397
398 /* This test and assignment was necessary because the retry VBL could
399 * have fired and reset send Seq to first Rtmt Seq, and then an
400 * expected ACK comes in that bumps first Rtmt Seq up. Then we
401 * have the problem that send Seq is less than first Rtmt Seq.
402 * The easiest fix to this timing dilemma seems to be to reset
403 * sendSeq to first Rtmt Seq if we're sending the first packet.
404 */
89b3af67 405 UAL_ASSIGN_HTON(sp->f.pktFirstByteSeq, sp->sendSeq);
1c79356b
A
406
407 if (smp = sp->sbuf_mb) /* Get oldest header */
408 eom = 1;
409 else if (smp = sp->csbuf_mb)
410 eom = 0;
411
412 if (smp == 0) { /* this shouldn't happen... */
413 sp->sData = 0;
414 return 0;
415 }
416 /*
417 * Must find next byte to transmit
418 */
419 seq = sp->firstRtmtSeq; /* Seq # of oldest in buffer */
420 while ((diff = (sp->sendSeq - seq)) >= ((bsize = gbuf_msgsize(smp)) + eom)) {
421 seq += bsize + eom; /* update sequence # */
422 if (gbuf_next(smp)) { /* if another send buffer */
423 smp = gbuf_next(smp);
424 eom = 1;
425 } else if (smp == sp->csbuf_mb) { /* seen the current one? */
426 smp = 0;
427 break;
428 } else if (sp->csbuf_mb) { /* look at it */
429 smp = sp->csbuf_mb;
430 eom = 0;
431 } else { /* no more buffers */
432 smp = 0;
433 break;
434 }
435 } /* while */
436
437 if (smp) {
438 if (gbuf_next(smp) == 0) /* last block */
439 sendAckReq = 1;
440 cnt = bsize - diff; /* # of bytes in this block */
441 } else
442 cnt = 0;
443
444 /*
445 * Check to see if the number of bytes is less than the 'send
446 * Blocking' setting. If so, then we won't send this data unless
447 * we're flushing. So we set up a timer to force a flush later.
448 */
449 if ((cnt < sp->sendBlocking) && !sp->writeFlush) {
450 InsertTimerElem(&adspGlobal.fastTimers, &sp->FlushTimer,
451 sp->sendInterval);
452 return 0; /* no data to send */
453 }
454
455 if (cnt > ADSP_MAX_DATA_LEN) { /* truncate to one packet */
456 cnt = ADSP_MAX_DATA_LEN;
457 eom = 0;
458 sendAckReq = 0; /* Won't send ack because end of data */
459 partial++;
460 }
461
462 if (smp) {
463 /* trim extra bytes off the beginning of the "block" before the copy */
464 while (diff) {
465 if (gbuf_len(smp) > diff)
466 break;
467 else
468 diff -= gbuf_len(smp);
469 smp = gbuf_cont(smp);
470 }
471 if((gbuf_cont(mp) = gbuf_dupm(smp)) == 0) /* copy the data */
472 return 0;
473 smp = gbuf_cont(mp); /* use the new message blocks */
474 gbuf_rinc(smp,diff); /* and get to the first byte of data to send */
475 }
476 /*
477 * Check to see if this many bytes will close the other end's
478 * receive window. If so, we need to send an ack request along
479 * with this. sendWdwSeq is the seq # of the last byte that
480 * the remote has room for
481 */
482 if ((diff = sp->sendWdwSeq + 1 - sp->sendSeq) <= cnt) {
483 if (diff < cnt) { /* Won't fit exactly */
484 eom = 0; /* so can't send EOM */
485 cnt = diff;
486 partial++;
487 }
488 sendAckReq = 1; /* Make him tell us new recv. window */
489 sp->noXmitFlow = 1; /* Don't do flow control calc. */
490 }
491
492 /* trim extra bytes off the tail of the "block" after the copy */
493 if (partial && smp) {
494 psmp = smp;
495 tcnt = cnt;
496 while (tcnt && smp) { /* while there are message blocks and data */
497 if (tcnt >= gbuf_len(smp)) {
498 tcnt -= gbuf_len(smp);
499 if (tcnt) {
500 psmp = smp;
501 smp = gbuf_cont(smp);
502 } else {
503 if (psmp != smp) { /* not the first item on the list */
504 gbuf_cont(psmp) = 0;
505 gbuf_freem(smp);
506 smp = psmp;
507 } else {
508 gbuf_freem(gbuf_cont(smp));
509 gbuf_cont(smp) = 0;
510 }
511 break;
512 }
513 } else {
514 gbuf_wset(smp,tcnt);
515 if (gbuf_cont(smp)) {
516 gbuf_freem(gbuf_cont(smp));
517 gbuf_cont(smp) = 0;
518 }
519 break;
520 }
521 }
522 }
523
524 sp->sendSeq += cnt + eom; /* Update sendSeq field */
525
526 if (GT(sp->sendSeq, sp->maxSendSeq)) /* Keep track of >st ever sent */
527 sp->maxSendSeq = sp->sendSeq;
528
529 if (eom)
530 sp->f.descriptor = ADSP_EOM_BIT;
531 else
532 sp->f.descriptor = 0;
533
534 if (sendAckReq || (++sp->pktSendCnt >= sp->pktSendMax)) {
535 /* Last packet in a series */
536 sp->f.descriptor |= ADSP_ACK_REQ_BIT; /* We want an ack to this */
537 sp->waitingAck = 1; /* Flag that we're waiting */
538 sp->sendStamp = SysTicks(); /* Save time we sent request */
539 sp->timerSeq = sp->sendSeq; /* Save seq # we want acked */
540 InsertTimerElem(&adspGlobal.fastTimers, &sp->RetryTimer,
541 sp->rtmtInterval+TX_DLY);
542 }
543 return cnt + eom;
544}
545
546
547