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