]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Control.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1990, 1995-1998 Apple Computer, Inc.
32 * All Rights Reserved.
36 * From Mike Shoemaker v01.25 07/02/90 for MacOS
37 * 09/07/95 - Modified for performance (Tuyen Nguyen)
38 * Modified for MP, 1996 by Tuyen Nguyen
39 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
42 #include <sys/errno.h>
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <machine/spl.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/filedesc.h>
50 #include <sys/fcntl.h>
53 #include <sys/socket.h>
55 #include <netat/sysglue.h>
56 #include <netat/appletalk.h>
57 #include <netat/ddp.h>
58 #include <netat/at_pcb.h>
59 #include <netat/debug.h>
60 #include <netat/adsp.h>
61 #include <netat/adsp_internal.h>
63 /* # of additional ticks to add to any timer that we're queuing up. For
64 * very short delays (1 and 2), the timer fires before the transmit
76 * # of bytes in avail in local receive queue
78 int CalcRecvWdw(sp
) /* (CCBPtr sp) */
83 bytes
= calcRecvQ(sp
);
84 bytes
= sp
->rbuflen
- bytes
; /* get what is left */
86 if (bytes
<= 16) { /* %%% this should be zero */
87 sp
->rbufFull
= 1; /* Save flag that our recv buf is full */
91 return ((bytes
+bytes
+bytes
) >> 2) + 1; /* %%% */
99 register struct mbuf
*m
, *p
;
101 if (((sp
->gref
)->so
)->so_rcv
.sb_mb
)
102 for (p
= ((sp
->gref
)->so
)->so_rcv
.sb_mb
; p
; p
= p
->m_nextpkt
)
103 for (m
= p
; m
; m
= m
->m_next
)
108 if (sp
->rData
) { /* There is data in buffer */
109 if (mb
= sp
->rbuf_mb
) {
111 bytes
+= gbuf_msgsize(mb
);
115 if (mb
= sp
->crbuf_mb
)
116 bytes
+= gbuf_msgsize(mb
);
125 * Check to see if the transmit PB is available and if there is anything
126 * to transmit. Start off any pending transmit.
128 * Normally called from the write completion routine
131 * sp Connection control block
133 * true if sent a packet
135 void CheckSend(sp
) /* (CCBPtr sp) */
139 int attnMsg
; /* True if attention message */
140 register gbuf_t
*mp
; /* send message block */
142 register gbuf_t
*tmp
;
145 char *dp
; /* a data pointer */
146 int use_attention_code
;
147 int len
; /* length used in allocd mblk */
148 int datalen
; /* amount of data attached to mblk */
149 gbuf_t
*mprev
, *mlist
= 0;
153 if (sp
->state
== sClosed
)
156 /* get a message block to hold DDP and
157 * ADSP headers + 2 bytes of attention
158 * code if necessary */
159 if ((mp
= gbuf_alloc(AT_WR_OFFSET
+ DDPL_FRAME_LEN
+ ADSP_FRAME_LEN
+ ADSP_OPEN_FRAME_LEN
+ 2,
163 return; /* can't get buffers... do nothing! */
165 sp
->callSend
= 0; /* Clear flag */
166 use_attention_code
= 0;
170 gbuf_rinc(mp
,AT_WR_OFFSET
);
171 gbuf_wset(mp
,DDPL_FRAME_LEN
); /* leave room for DDP header */
176 i
= sp
->sendCtl
; /* get local copy bitmap of */
177 /* which ctl packets to send. */
180 if (i
& 0x1E) /* One of the open ctrl packets */
183 /* point past ADSP header (no attention) */
184 dp
= ((char *) gbuf_wptr(mp
)) + ADSP_FRAME_LEN
;
185 UAL_ASSIGN_HTON(sp
->f
.pktFirstByteSeq
, sp
->firstRtmtSeq
);
187 UAS_ASSIGN_HTON(sp
->of
.version
, netw(0x0100)); /* Fill in open connection parms */
188 UAS_ASSIGN_HTON(sp
->of
.dstCID
, sp
->remCID
); /* Destination CID */
189 UAL_ASSIGN_HTON(sp
->of
.pktAttnRecvSeq
, sp
->attnRecvSeq
);
190 bcopy((caddr_t
) &sp
->of
, (caddr_t
) dp
, ADSP_OPEN_FRAME_LEN
);
191 len
+= ADSP_OPEN_FRAME_LEN
;
193 if (i
& B_CTL_OREQ
) {
194 UAS_ASSIGN_HTON(sp
->f
.CID
, sp
->locCID
);
196 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_OREQ
;
197 } else if (i
& B_CTL_OACK
) {
198 UAS_ASSIGN_HTON(sp
->f
.CID
, sp
->locCID
);
200 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_OACK
;
201 } else if (i
& B_CTL_OREQACK
) {
202 UAS_ASSIGN_HTON(sp
->f
.CID
, sp
->locCID
);
203 mask
= B_CTL_OREQACK
;
204 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_OREQACK
;
207 UAS_ASSIGN(sp
->f
.CID
, 0);
209 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_ODENY
;
210 UAL_ASSIGN(sp
->f
.pktFirstByteSeq
, 0);
213 if (i
& (B_CTL_OREQ
| B_CTL_OREQACK
))
214 /* Need to start up a timer for it */
216 /* It's possible that we've received a duplicate
217 * open request. In this case, there will already be
218 * a timer queued up for the request+ack
219 * packet we sent the first time. So remove the timer
222 RemoveTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
);
223 InsertTimerElem(&adspGlobal
.slowTimers
, &sp
->ProbeTimer
,
227 /* seq # of next byte to send */
228 UAL_ASSIGN_HTON(sp
->f
.pktFirstByteSeq
, sp
->sendSeq
);
230 if (i
& B_CTL_CLOSE
) {
231 sp
->state
= sClosed
; /* Now we're closed */
233 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_CLOSE
;
234 } else if (i
& B_CTL_PROBE
) {
237 ADSP_CONTROL_BIT
| ADSP_CTL_PROBE
| ADSP_ACK_REQ_BIT
;
238 } else if (i
& B_CTL_FRESET
) {
240 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_FRESET
;
241 InsertTimerElem(&adspGlobal
.fastTimers
,
242 &sp
->ResetTimer
, sp
->rtmtInterval
+TX_DLY
);
243 } else if (i
& B_CTL_FRESETACK
) {
244 mask
= B_CTL_FRESETACK
;
245 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_FRESET_ACK
;
247 else if (i
& B_CTL_RETRANSMIT
) {
248 mask
= B_CTL_RETRANSMIT
;
249 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_CTL_RETRANSMIT
;
252 dPrintf(D_M_ADSP
, D_L_ERROR
, ("CheckSend: Control bit error\n"));
254 } /* non open control packet */
256 sp
->sendCtl
&= ~mask
;
258 } /* send control packet */
260 if (sp
->sendAttnData
) /* Send attn ready to go? */
262 sp
->sendAttnData
= 0; /* Clear Flags */
264 sp
->sendAttnAck
= 0; /* This will also do an Attn Ack */
267 sp
->f
.descriptor
= ADSP_ATTENTION_BIT
| ADSP_ACK_REQ_BIT
;
268 if (gbuf_cont(sp
->sapb
->mp
)) {
269 gbuf_cont(mp
) = gbuf_dupm(gbuf_cont(sp
->sapb
->mp
));
270 /* Major hack here. The ADSP Attn code is butted up against
271 * the end of the adsp packet header, and the length is
272 * increased by 2. (There is a pad field behind the adsp
273 * header in the CCB just for this purpose.)
276 use_attention_code
++;
278 sp
->f
.data
[0] = high(sp
->sapb
->u
.attnParams
.attnCode
);
279 sp
->f
.data
[1] = low(sp
->sapb
->u
.attnParams
.attnCode
);
280 InsertTimerElem(&adspGlobal
.fastTimers
, &sp
->AttnTimer
,
281 sp
->rtmtInterval
+TX_DLY
);
286 if (sp
->sendAttnAck
) /* Send attn ack ready to go? */
289 sp
->f
.descriptor
= ADSP_CONTROL_BIT
| ADSP_ATTENTION_BIT
;
294 if ((sp
->state
== sOpen
|| sp
->state
== sClosing
) && /* Correct state */
295 (!sp
->waitingAck
) && /* not waiting for an ACK */
296 (sp
->sData
) && /* have data to send */
297 (GTE(sp
->sendWdwSeq
,sp
->sendSeq
)) && /* he has room to accept it */
298 (sp
->pktSendCnt
< sp
->pktSendMax
)) /* haven't sent too many pkts
302 if (datalen
= attachData(sp
, mp
)) /* attach data to mp */
303 goto sendit
; /* if successful, sendit */
306 if (sp
->sendDataAck
) {
307 UAL_ASSIGN_HTON(sp
->f
.pktFirstByteSeq
, sp
->sendSeq
); /* seq # of next byte */
309 sp
->f
.descriptor
= ADSP_CONTROL_BIT
;
314 * Nothing left to do...
319 adsp_sendddp(sp
, mlist
, 0, &sp
->remoteAddress
, DDP_ADSP
);
325 UAL_ASSIGN_HTON(sp
->f
.pktFirstByteSeq
, sp
->attnSendSeq
);
326 UAL_ASSIGN_HTON(sp
->f
.pktNextRecvSeq
, sp
->attnRecvSeq
);
327 UAS_ASSIGN(sp
->f
.pktRecvWdw
, 0); /* Always zero in attn pkt */
330 UAL_ASSIGN_HTON(sp
->f
.pktNextRecvSeq
, sp
->recvSeq
);
331 UAS_ASSIGN_HTON(sp
->f
.pktRecvWdw
, CalcRecvWdw(sp
));
333 if (use_attention_code
) {
334 bcopy((caddr_t
) &sp
->f
, (caddr_t
) gbuf_wptr(mp
), ADSP_FRAME_LEN
+ 2);
335 len
+= ADSP_FRAME_LEN
+ 2;
337 bcopy((caddr_t
) &sp
->f
, (caddr_t
) gbuf_wptr(mp
), ADSP_FRAME_LEN
);
338 len
+= ADSP_FRAME_LEN
;
340 gbuf_winc(mp
,len
); /* update mblk length */
342 gbuf_next(mprev
) = mp
;
347 if (sp
->state
== sClosed
) { /* must have sent a close advice */
348 /* send header + data */
349 adsp_sendddp(sp
, mlist
, 0, &sp
->remoteAddress
, DDP_ADSP
);
350 DoClose(sp
, 0, -1); /* complete close! */
353 if (sp
->state
== sClosing
) /* See if we were waiting on this write */
359 * completepb delivers a paramater block with all its appropriate fields
360 * set back to the user.
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.
367 void completepb(sp
, pb
)
369 register struct adspcmd
*pb
;
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
);
390 int partial
= 0; /* flag for a partial send */
392 register gbuf_t
*smp
; /* send data message block */
393 register gbuf_t
*psmp
; /* previous message block */
397 if (LT(sp
->sendSeq
, sp
->firstRtmtSeq
)) /* Sanity check on send seq */
398 sp
->sendSeq
= sp
->firstRtmtSeq
; /* seq must be oldest in buffer. */
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.
407 UAL_ASSIGN_HTON(sp
->f
.pktFirstByteSeq
, sp
->sendSeq
);
409 if (smp
= sp
->sbuf_mb
) /* Get oldest header */
411 else if (smp
= sp
->csbuf_mb
)
414 if (smp
== 0) { /* this shouldn't happen... */
419 * Must find next byte to transmit
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
);
427 } else if (smp
== sp
->csbuf_mb
) { /* seen the current one? */
430 } else if (sp
->csbuf_mb
) { /* look at it */
433 } else { /* no more buffers */
440 if (gbuf_next(smp
) == 0) /* last block */
442 cnt
= bsize
- diff
; /* # of bytes in this block */
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.
451 if ((cnt
< sp
->sendBlocking
) && !sp
->writeFlush
) {
452 InsertTimerElem(&adspGlobal
.fastTimers
, &sp
->FlushTimer
,
454 return 0; /* no data to send */
457 if (cnt
> ADSP_MAX_DATA_LEN
) { /* truncate to one packet */
458 cnt
= ADSP_MAX_DATA_LEN
;
460 sendAckReq
= 0; /* Won't send ack because end of data */
465 /* trim extra bytes off the beginning of the "block" before the copy */
467 if (gbuf_len(smp
) > diff
)
470 diff
-= gbuf_len(smp
);
471 smp
= gbuf_cont(smp
);
473 if((gbuf_cont(mp
) = gbuf_dupm(smp
)) == 0) /* copy the data */
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 */
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
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 */
490 sendAckReq
= 1; /* Make him tell us new recv. window */
491 sp
->noXmitFlow
= 1; /* Don't do flow control calc. */
494 /* trim extra bytes off the tail of the "block" after the copy */
495 if (partial
&& smp
) {
498 while (tcnt
&& smp
) { /* while there are message blocks and data */
499 if (tcnt
>= gbuf_len(smp
)) {
500 tcnt
-= gbuf_len(smp
);
503 smp
= gbuf_cont(smp
);
505 if (psmp
!= smp
) { /* not the first item on the list */
510 gbuf_freem(gbuf_cont(smp
));
517 if (gbuf_cont(smp
)) {
518 gbuf_freem(gbuf_cont(smp
));
526 sp
->sendSeq
+= cnt
+ eom
; /* Update sendSeq field */
528 if (GT(sp
->sendSeq
, sp
->maxSendSeq
)) /* Keep track of >st ever sent */
529 sp
->maxSendSeq
= sp
->sendSeq
;
532 sp
->f
.descriptor
= ADSP_EOM_BIT
;
534 sp
->f
.descriptor
= 0;
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
);