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