]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Control.c
ebe72f1b1d6f5b05c195ad88a7257212c5913d0e
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1995-1998 Apple Computer, Inc.
25 * All Rights Reserved.
29 * From Mike Shoemaker v01.25 07/02/90 for MacOS
30 * 09/07/95 - Modified for performance (Tuyen Nguyen)
31 * Modified for MP, 1996 by Tuyen Nguyen
32 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <machine/spl.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/filedesc.h>
43 #include <sys/fcntl.h>
46 #include <sys/socket.h>
48 #include <netat/sysglue.h>
49 #include <netat/appletalk.h>
50 #include <netat/ddp.h>
51 #include <netat/at_pcb.h>
52 #include <netat/debug.h>
53 #include <netat/adsp.h>
54 #include <netat/adsp_internal.h>
56 /* # of additional ticks to add to any timer that we're queuing up. For
57 * very short delays (1 and 2), the timer fires before the transmit
69 * # of bytes in avail in local receive queue
71 int CalcRecvWdw(sp
) /* (CCBPtr sp) */
76 bytes
= calcRecvQ(sp
);
77 bytes
= sp
->rbuflen
- bytes
; /* get what is left */
79 if (bytes
<= 16) { /* %%% this should be zero */
80 sp
->rbufFull
= 1; /* Save flag that our recv buf is full */
84 return ((bytes
+bytes
+bytes
) >> 2) + 1; /* %%% */
92 register struct mbuf
*m
, *p
;
94 if (((sp
->gref
)->so
)->so_rcv
.sb_mb
)
95 for (p
= ((sp
->gref
)->so
)->so_rcv
.sb_mb
; p
; p
= p
->m_nextpkt
)
96 for (m
= p
; m
; m
= m
->m_next
)
101 if (sp
->rData
) { /* There is data in buffer */
102 if (mb
= sp
->rbuf_mb
) {
104 bytes
+= gbuf_msgsize(mb
);
108 if (mb
= sp
->crbuf_mb
)
109 bytes
+= gbuf_msgsize(mb
);
118 * Check to see if the transmit PB is available and if there is anything
119 * to transmit. Start off any pending transmit.
121 * Normally called from the write completion routine
124 * sp Connection control block
126 * true if sent a packet
128 void CheckSend(sp
) /* (CCBPtr sp) */
132 int attnMsg
; /* True if attention message */
134 register gbuf_t
*mp
; /* send message block */
136 register gbuf_t
*tmp
;
139 char *dp
; /* a data pointer */
140 int use_attention_code
;
141 int len
; /* length used in allocd mblk */
142 int datalen
; /* amount of data attached to mblk */
143 gbuf_t
*mprev
, *mlist
= 0;
147 if (sp
->state
== sClosed
)
150 /* get a message block to hold DDP and
151 * ADSP headers + 2 bytes of attention
152 * code if necessary */
153 if ((mp
= gbuf_alloc(AT_WR_OFFSET
+ DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+ ADSP_OPEN_FRAME_LEN
+ 2,
157 return; /* can't get buffers... do nothing! */
159 ATDISABLE(s
, sp
->lock
);
160 sp
->callSend
= 0; /* Clear flag */
161 use_attention_code
= 0;
165 gbuf_rinc(mp
,AT_WR_OFFSET
);
166 gbuf_wset(mp
,DDPL_FRAME_LEN
); /* leave room for DDP header */
171 i
= sp
->sendCtl
; /* get local copy bitmap of */
172 /* which ctl packets to send. */
175 if (i
& 0x1E) /* One of the open ctrl packets */
178 /* point past ADSP header (no attention) */
179 dp
= ((char *) gbuf_wptr(mp
)) + ADSP_FRAME_LEN
;
180 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, netdw(sp
->firstRtmtSeq
));
182 UAS_ASSIGN(sp
->of
.version
, netw(0x0100)); /* Fill in open connection parms */
183 UAS_ASSIGN(sp
->of
.dstCID
, sp
->remCID
); /* Destination CID */
184 UAL_ASSIGN(sp
->of
.pktAttnRecvSeq
, netdw(sp
->attnRecvSeq
));
185 bcopy((caddr_t
) &sp
->of
, (caddr_t
) dp
, ADSP_OPEN_FRAME_LEN
);
186 len
+= ADSP_OPEN_FRAME_LEN
;
188 if (i
& B_CTL_OREQ
) {
189 UAS_ASSIGN(sp
->f
.CID
, sp
->locCID
);
191 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_OREQ
;
192 } else if (i
& B_CTL_OACK
) {
193 UAS_ASSIGN(sp
->f
.CID
, sp
->locCID
);
195 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_OACK
;
196 } else if (i
& B_CTL_OREQACK
) {
197 UAS_ASSIGN(sp
->f
.CID
, sp
->locCID
);
198 mask
= B_CTL_OREQACK
;
199 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_OREQACK
;
202 UAS_ASSIGN(sp
->f
.CID
, 0);
204 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_ODENY
;
205 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, 0);
208 if (i
& (B_CTL_OREQ
| B_CTL_OREQACK
))
209 /* Need to start up a timer for it */
211 /* It's possible that we've received a duplicate
212 * open request. In this case, there will already be
213 * a timer queued up for the request+ack
214 * packet we sent the first time. So remove the timer
217 RemoveTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
);
218 InsertTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
,
222 /* seq # of next byte to send */
223 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, netdw(sp
->sendSeq
));
225 if (i
& B_CTL_CLOSE
) {
226 sp
->state
= sClosed
; /* Now we're closed */
228 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_CLOSE
;
229 } else if (i
& B_CTL_PROBE
) {
232 ADSP_CONTROL_BIT
| ADSP_CTL_PROBE
| ADSP_ACK_REQ_BIT
;
233 } else if (i
& B_CTL_FRESET
) {
235 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_FRESET
;
236 InsertTimerElem(&adspGlobal
.fastTimers
,
237 &sp
->ResetTimer
, sp
->rtmtInterval
+TX_DLY
);
238 } else if (i
& B_CTL_FRESETACK
) {
239 mask
= B_CTL_FRESETACK
;
240 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_FRESET_ACK
;
242 else if (i
& B_CTL_RETRANSMIT
) {
243 mask
= B_CTL_RETRANSMIT
;
244 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_RETRANSMIT
;
247 dPrintf(D_M_ADSP
, D_L_ERROR
, ("CheckSend: Control bit error\n"));
249 } /* non open control packet */
251 sp
->sendCtl
&= ~mask
;
253 } /* send control packet */
255 if (sp
->sendAttnData
) /* Send attn ready to go? */
257 sp
->sendAttnData
= 0; /* Clear Flags */
259 sp
->sendAttnAck
= 0; /* This will also do an Attn Ack */
262 sp
->f
.descriptor
= ADSP_ATTENTION_BIT
| ADSP_ACK_REQ_BIT
;
263 if (gbuf_cont(sp
->sapb
->mp
)) {
264 gbuf_cont(mp
) = gbuf_dupm(gbuf_cont(sp
->sapb
->mp
));
265 /* Major hack here. The ADSP Attn code is butted up against
266 * the end of the adsp packet header, and the length is
267 * increased by 2. (There is a pad field behind the adsp
268 * header in the CCB just for this purpose.)
271 use_attention_code
++;
273 sp
->f
.data
[0] = high(sp
->sapb
->u
.attnParams
.attnCode
);
274 sp
->f
.data
[1] = low(sp
->sapb
->u
.attnParams
.attnCode
);
275 InsertTimerElem(&adspGlobal
.fastTimers
, &sp
->AttnTimer
,
276 sp
->rtmtInterval
+TX_DLY
);
281 if (sp
->sendAttnAck
) /* Send attn ack ready to go? */
284 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_ATTENTION_BIT
;
289 if ((sp
->state
== sOpen
|| sp
->state
== sClosing
) && /* Correct state */
290 (!sp
->waitingAck
) && /* not waiting for an ACK */
291 (sp
->sData
) && /* have data to send */
292 (GTE(sp
->sendWdwSeq
,sp
->sendSeq
)) && /* he has room to accept it */
293 (sp
->pktSendCnt
< sp
->pktSendMax
)) /* haven't sent too many pkts
297 if (datalen
= attachData(sp
, mp
)) /* attach data to mp */
298 goto sendit
; /* if successful, sendit */
301 if (sp
->sendDataAck
) {
302 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, netdw(sp
->sendSeq
)); /* seq # of next byte */
304 sp
->f
.descriptor
= ADSP_CONTROL_BIT
;
309 * Nothing left to do...
313 ATENABLE(s
, sp
->lock
);
315 adsp_sendddp(sp
, mlist
, 0, &sp
->remoteAddress
, DDP_ADSP
);
321 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, netdw(sp
->attnSendSeq
));
322 UAL_ASSIGN(sp
->f
.pktNextRecvSeq
, netdw(sp
->attnRecvSeq
));
323 UAS_ASSIGN(sp
->f
.pktRecvWdw
, 0); /* Always zero in attn pkt */
326 UAL_ASSIGN(sp
->f
.pktNextRecvSeq
, netdw(sp
->recvSeq
));
327 UAS_ASSIGN(sp
->f
.pktRecvWdw
, netw(CalcRecvWdw(sp
)));
329 if (use_attention_code
) {
330 bcopy((caddr_t
) &sp
->f
, (caddr_t
) gbuf_wptr(mp
), ADSP_FRAME_LEN
+ 2);
331 len
+= ADSP_FRAME_LEN
+ 2;
333 bcopy((caddr_t
) &sp
->f
, (caddr_t
) gbuf_wptr(mp
), ADSP_FRAME_LEN
);
334 len
+= ADSP_FRAME_LEN
;
336 gbuf_winc(mp
,len
); /* update mblk length */
338 gbuf_next(mprev
) = mp
;
343 if (sp
->state
== sClosed
) { /* must have sent a close advice */
344 /* send header + data */
345 ATENABLE(s
, sp
->lock
);
346 adsp_sendddp(sp
, mlist
, 0, &sp
->remoteAddress
, DDP_ADSP
);
347 DoClose(sp
, 0, -1); /* complete close! */
350 ATENABLE(s
, sp
->lock
);
351 if (sp
->state
== sClosing
) /* See if we were waiting on this write */
357 * completepb delivers a paramater block with all its appropriate fields
358 * set back to the user.
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.
365 void completepb(sp
, pb
)
367 register struct adspcmd
*pb
;
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
);
388 int partial
= 0; /* flag for a partial send */
390 register gbuf_t
*smp
; /* send data message block */
391 register gbuf_t
*psmp
; /* previous message block */
395 if (LT(sp
->sendSeq
, sp
->firstRtmtSeq
)) /* Sanity check on send seq */
396 sp
->sendSeq
= sp
->firstRtmtSeq
; /* seq must be oldest in buffer. */
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.
405 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, netdw(sp
->sendSeq
));
407 if (smp
= sp
->sbuf_mb
) /* Get oldest header */
409 else if (smp
= sp
->csbuf_mb
)
412 if (smp
== 0) { /* this shouldn't happen... */
417 * Must find next byte to transmit
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
);
425 } else if (smp
== sp
->csbuf_mb
) { /* seen the current one? */
428 } else if (sp
->csbuf_mb
) { /* look at it */
431 } else { /* no more buffers */
438 if (gbuf_next(smp
) == 0) /* last block */
440 cnt
= bsize
- diff
; /* # of bytes in this block */
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.
449 if ((cnt
< sp
->sendBlocking
) && !sp
->writeFlush
) {
450 InsertTimerElem(&adspGlobal
.fastTimers
, &sp
->FlushTimer
,
452 return 0; /* no data to send */
455 if (cnt
> ADSP_MAX_DATA_LEN
) { /* truncate to one packet */
456 cnt
= ADSP_MAX_DATA_LEN
;
458 sendAckReq
= 0; /* Won't send ack because end of data */
463 /* trim extra bytes off the beginning of the "block" before the copy */
465 if (gbuf_len(smp
) > diff
)
468 diff
-= gbuf_len(smp
);
469 smp
= gbuf_cont(smp
);
471 if((gbuf_cont(mp
) = gbuf_dupm(smp
)) == 0) /* copy the data */
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 */
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
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 */
488 sendAckReq
= 1; /* Make him tell us new recv. window */
489 sp
->noXmitFlow
= 1; /* Don't do flow control calc. */
492 /* trim extra bytes off the tail of the "block" after the copy */
493 if (partial
&& smp
) {
496 while (tcnt
&& smp
) { /* while there are message blocks and data */
497 if (tcnt
>= gbuf_len(smp
)) {
498 tcnt
-= gbuf_len(smp
);
501 smp
= gbuf_cont(smp
);
503 if (psmp
!= smp
) { /* not the first item on the list */
508 gbuf_freem(gbuf_cont(smp
));
515 if (gbuf_cont(smp
)) {
516 gbuf_freem(gbuf_cont(smp
));
524 sp
->sendSeq
+= cnt
+ eom
; /* Update sendSeq field */
526 if (GT(sp
->sendSeq
, sp
->maxSendSeq
)) /* Keep track of >st ever sent */
527 sp
->maxSendSeq
= sp
->sendSeq
;
530 sp
->f
.descriptor
= ADSP_EOM_BIT
;
532 sp
->f
.descriptor
= 0;
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
);