Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 * 
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/*
 *	Copyright (c) 1990, 1995-1998 Apple Computer, Inc.
 *	All Rights Reserved.
 */

/* Control.c 
 * From Mike Shoemaker v01.25 07/02/90 for MacOS
 * 09/07/95 - Modified for performance (Tuyen Nguyen)
 *    Modified for MP, 1996 by Tuyen Nguyen
 *   Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
 */

#include <sys/errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <machine/spl.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/filedesc.h>
#include <sys/fcntl.h>
#include <sys/mbuf.h>
#include <sys/time.h>
#include <sys/socket.h>

#include <netat/sysglue.h>
#include <netat/appletalk.h>
#include <netat/ddp.h>
#include <netat/at_pcb.h>
#include <netat/debug.h>
#include <netat/adsp.h>
#include <netat/adsp_internal.h>

/* # of additional ticks to add to any timer that we're queuing up.  For 
 * very short delays (1 and 2), the timer fires before the transmit 
 * even takes place */
#define TX_DLY	2

int adsp_window = 1;

/*
 * CalcRecvWdw
 *
 * INPUTS:
 *		sp		ADSP Stream
 * OUTPUTS:
 *		# of bytes in avail in local receive queue
 */
int CalcRecvWdw(sp)		/* (CCBPtr sp) */
    CCBPtr sp;
{
    int bytes;

    bytes = calcRecvQ(sp);
    bytes = sp->rbuflen - bytes; /* get what is left */

    if (bytes <= 16) {		/* %%% this should be zero */
	sp->rbufFull = 1;	/* Save flag that our recv buf is full */
	return 0;
    }
    else
	return ((bytes+bytes+bytes) >> 2) + 1; /* %%% */
}

calcRecvQ(sp)
    CCBPtr sp;
{
    int bytes = 0;
#ifdef AT_Socket
    register struct mbuf *m, *p;

    if (((sp->gref)->so)->so_rcv.sb_mb)
      for (p = ((sp->gref)->so)->so_rcv.sb_mb; p; p = p->m_nextpkt)
	for (m = p; m; m = m->m_next)
	  bytes += m->m_len;
#else
    register gbuf_t *mb;

    if (sp->rData) {		/* There is data in buffer */
	if (mb = sp->rbuf_mb) {
	    do {
		bytes += gbuf_msgsize(mb);
		mb = gbuf_next(mb);
	    } while (mb);
	}
	if (mb = sp->crbuf_mb)
	    bytes += gbuf_msgsize(mb);
    }
#endif
    return bytes;
}

/*
 * CheckSend
 * 
 * Check to see if the transmit PB is available and if there is anything 
 * to transmit. Start off any pending transmit.
 *
 * Normally called from the write completion routine
 *
 * INPUTS:
 * 		sp		Connection control block
 * OUTPUTS:
 * 		true if sent a packet	
 */
void CheckSend(sp)		/* (CCBPtr sp) */
    register CCBPtr sp;
{
    int i;
    int	attnMsg;		/* True if attention message */
    int	s;
    register gbuf_t *mp;	/* send message block */
#ifdef notdef
    register gbuf_t *tmp;
    u_char current;
#endif
    char *dp;			/* a data pointer */
    int use_attention_code;
    int len;			/* length used in allocd mblk */
    int datalen;		/* amount of data attached to mblk */
    gbuf_t *mprev, *mlist = 0;

top:

    if (sp->state == sClosed)
	return;

				/* get a message block to hold DDP and
				 * ADSP headers + 2 bytes of attention
				 * code if necessary */
    if ((mp = gbuf_alloc(AT_WR_OFFSET + DDPL_FRAME_LEN + ADSP_FRAME_LEN + ADSP_OPEN_FRAME_LEN + 2,
		     PRI_LO)) == 0) {
	if (mlist)
		gbuf_freel(mlist);
	return;		/* can't get buffers... do nothing! */
    }
    ATDISABLE(s, sp->lock);
    sp->callSend = 0;		/* Clear flag */
    use_attention_code = 0;
    len = 0;
    datalen = 0;

    gbuf_rinc(mp,AT_WR_OFFSET);
    gbuf_wset(mp,DDPL_FRAME_LEN); /* leave room for DDP header */

    if (sp->sendCtl) {
	short mask = 0;
		
	i = sp->sendCtl;	/* get local copy bitmap of */
				/* which ctl packets to send. */
	attnMsg = 0;
		
	if (i & 0x1E)		/* One of the open ctrl packets */
	{

	    /* point past ADSP header (no attention) */
	    dp = ((char *) gbuf_wptr(mp)) + ADSP_FRAME_LEN; 
	    UAL_ASSIGN(sp->f.pktFirstByteSeq, netdw(sp->firstRtmtSeq));
	    
	    UAS_ASSIGN(sp->of.version, netw(0x0100)); /* Fill in open connection parms */
	    UAS_ASSIGN(sp->of.dstCID, sp->remCID);	/* Destination CID */
	    UAL_ASSIGN(sp->of.pktAttnRecvSeq, netdw(sp->attnRecvSeq));
	    bcopy((caddr_t) &sp->of, (caddr_t) dp, ADSP_OPEN_FRAME_LEN);
	    len += ADSP_OPEN_FRAME_LEN;

	    if (i & B_CTL_OREQ) {
		UAS_ASSIGN(sp->f.CID, sp->locCID);
		mask = B_CTL_OREQ;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_OREQ;
	    } else if (i & B_CTL_OACK) {
		UAS_ASSIGN(sp->f.CID, sp->locCID);
		mask = B_CTL_OACK;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_OACK;
	    } else if (i & B_CTL_OREQACK) {
		UAS_ASSIGN(sp->f.CID, sp->locCID);
		mask = B_CTL_OREQACK;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_OREQACK;
	    } else 		/* Deny */
	    {
		UAS_ASSIGN(sp->f.CID, 0);
		mask = B_CTL_ODENY;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_ODENY;
		UAL_ASSIGN(sp->f.pktFirstByteSeq, 0);
	    }
			
	    if (i & (B_CTL_OREQ | B_CTL_OREQACK)) 
		/* Need to start up a timer for it */
	    {
		/* It's possible that we've received a duplicate 
		 * open request.  In this case, there will already be 
		 * a timer queued up for the request+ack 
		 *  packet we sent the first time.  So remove the timer 
		 * and start another. 
		 */
	        RemoveTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer);
		InsertTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer, 
				sp->openInterval+1);
	    }
	} else {
	    /* seq # of next byte to send */
	    UAL_ASSIGN(sp->f.pktFirstByteSeq, netdw(sp->sendSeq));	
			
	    if (i & B_CTL_CLOSE) {
		sp->state = sClosed; /* Now we're closed */
		mask = B_CTL_CLOSE;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_CLOSE;
	    } else if (i & B_CTL_PROBE) {
		mask = B_CTL_PROBE;
		sp->f.descriptor = 
		    ADSP_CONTROL_BIT | ADSP_CTL_PROBE | ADSP_ACK_REQ_BIT;
	    } else if (i & B_CTL_FRESET) {
		mask = B_CTL_FRESET;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_FRESET;
		InsertTimerElem(&adspGlobal.fastTimers, 
				&sp->ResetTimer, sp->rtmtInterval+TX_DLY);
	    } else if (i & B_CTL_FRESETACK) {
		mask = B_CTL_FRESETACK;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_FRESET_ACK;
	    }
	    else if (i & B_CTL_RETRANSMIT) {
		mask = B_CTL_RETRANSMIT;
		sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_CTL_RETRANSMIT;
	    } 
	    else {
		dPrintf(D_M_ADSP, D_L_ERROR, ("CheckSend: Control bit error\n"));
	   }
	}			/* non open control packet */

	sp->sendCtl &= ~mask; 
	goto sendit;
    }				/* send control packet */

    if (sp->sendAttnData)	/* Send attn ready to go? */
    {
	sp->sendAttnData = 0;	/* Clear Flags */
	if (sp->sapb) {
	    sp->sendAttnAck  = 0; /* This will also do an Attn Ack */
	
	    attnMsg = 1;
	    sp->f.descriptor = ADSP_ATTENTION_BIT | ADSP_ACK_REQ_BIT;
	    if (gbuf_cont(sp->sapb->mp)) {
		gbuf_cont(mp) = gbuf_dupm(gbuf_cont(sp->sapb->mp)); 
		/* Major hack here.  The ADSP Attn code is butted up against 
		 * the end of the adsp packet header, and the length is 
		 * increased by 2.  (There is a pad field behind the adsp
		 * header in the CCB just for this purpose.)
		 */
	    }
	    use_attention_code++;

	    sp->f.data[0] = high(sp->sapb->u.attnParams.attnCode);
	    sp->f.data[1] = low(sp->sapb->u.attnParams.attnCode);
	    InsertTimerElem(&adspGlobal.fastTimers, &sp->AttnTimer, 
			                             sp->rtmtInterval+TX_DLY);
	    goto sendit;
	}
    }				/* attn data */
    
    if (sp->sendAttnAck)	/* Send attn ack ready to go? */
    {	
	attnMsg = 1;
	sp->f.descriptor = ADSP_CONTROL_BIT | ADSP_ATTENTION_BIT;
	sp->sendAttnAck = 0;	
	goto sendit;
    }				/* attn ack */
	
    if ((sp->state == sOpen || sp->state == sClosing) && /* Correct state */
	(!sp->waitingAck) &&	/* not waiting for an ACK */
	(sp->sData) &&		/* have data to send */
	(GTE(sp->sendWdwSeq,sp->sendSeq)) && /* he has room to accept it */
	(sp->pktSendCnt < sp->pktSendMax)) /* haven't sent too many pkts 
					    * in a row. */
    {
	attnMsg = 0;
	if (datalen = attachData(sp, mp)) /* attach data to mp */
	    goto sendit;	/* if successful, sendit */
    }

    if (sp->sendDataAck) {
	UAL_ASSIGN(sp->f.pktFirstByteSeq, netdw(sp->sendSeq)); /* seq # of next byte */
	attnMsg = 0;
	sp->f.descriptor = ADSP_CONTROL_BIT;
	goto sendit;
    }

    /*
     * Nothing left to do...
     */
    if (mp)
	gbuf_freem(mp);
    ATENABLE(s, sp->lock);
    if (mlist)
	adsp_sendddp(sp, mlist, 0, &sp->remoteAddress, DDP_ADSP);
    return;

sendit:

    if (attnMsg) {
	UAL_ASSIGN(sp->f.pktFirstByteSeq, netdw(sp->attnSendSeq));
	UAL_ASSIGN(sp->f.pktNextRecvSeq, netdw(sp->attnRecvSeq));
	UAS_ASSIGN(sp->f.pktRecvWdw, 0);	/* Always zero in attn pkt */
    } else {
	sp->sendDataAck = 0;
	UAL_ASSIGN(sp->f.pktNextRecvSeq, netdw(sp->recvSeq));
	UAS_ASSIGN(sp->f.pktRecvWdw, netw(CalcRecvWdw(sp)));
    }
    if (use_attention_code) {
	bcopy((caddr_t) &sp->f, (caddr_t) gbuf_wptr(mp), ADSP_FRAME_LEN + 2);
	len += ADSP_FRAME_LEN + 2;
    } else {
	bcopy((caddr_t) &sp->f, (caddr_t) gbuf_wptr(mp), ADSP_FRAME_LEN);
	len += ADSP_FRAME_LEN;
    }
    gbuf_winc(mp,len);		/* update mblk length  */
    if (mlist)
	gbuf_next(mprev) = mp;
    else
	mlist = mp;
    mprev = mp;

    if (sp->state == sClosed) {	/* must have sent a close advice */
				/* send header + data */
	ATENABLE(s, sp->lock);
	adsp_sendddp(sp, mlist, 0, &sp->remoteAddress, DDP_ADSP);
	DoClose(sp, 0, -1);	/* complete close! */
	return;
    }
    ATENABLE(s, sp->lock);
    if (sp->state == sClosing) /* See if we were waiting on this write */
	CheckOkToClose(sp);
    goto top;
}

/*
 * completepb delivers a paramater block with all its appropriate fields
 * set back to the user.  
 *
 * The assumptions here are that the PB is not linked to any queue, 
 * that the fields including ioResult are set, and that the 
 * kernel is no longer interested in the mblks that may or
 * maynot be linked to this pb.
 */
void completepb(sp, pb)
    register CCBPtr sp;
    register struct adspcmd *pb;
{
    if (sp->gref && (sp->gref->info == (caddr_t)sp->sp_mp)) {
	if (gbuf_len(pb->mp) > sizeof(struct adspcmd))
		gbuf_wset(pb->mp,sizeof(struct adspcmd));
	SndMsgUp(sp->gref, pb->mp);
	NotifyUser(sp);
    } else
	gbuf_freem(pb->mp);
}

attachData(sp, mp)
    register CCBPtr sp;
    register gbuf_t *mp;
{
    int	seq;
    int cnt;
    char eom = 0;
    int bsize;
    int diff;
    char sendAckReq;
    int partial = 0;		/* flag for a partial send */
    int tcnt = 0;
    register gbuf_t *smp;	/* send data message block */
    register gbuf_t *psmp;	/* previous message block */

    sendAckReq = 0;

    if (LT(sp->sendSeq, sp->firstRtmtSeq)) /* Sanity check on send seq */
	sp->sendSeq = sp->firstRtmtSeq; /* seq must be oldest in buffer. */

    /* This test and assignment was necessary because the retry VBL could 
     * have fired and reset send Seq to first Rtmt Seq, and then an 
     * expected ACK comes in that bumps first Rtmt Seq up.  Then we 
     * have the problem that send Seq is less than first Rtmt Seq.
     * The easiest fix to this timing dilemma seems to be to reset 
     * sendSeq to first Rtmt Seq if we're sending the first packet.
     */
    UAL_ASSIGN(sp->f.pktFirstByteSeq, netdw(sp->sendSeq));
		
    if (smp = sp->sbuf_mb) /* Get oldest header */
	eom = 1;
    else if (smp = sp->csbuf_mb)
	eom = 0;

    if (smp == 0) {		/* this shouldn't happen... */
	    sp->sData = 0;
	    return 0;
    }
    /*
     * Must find next byte to transmit
     */
    seq = sp->firstRtmtSeq;	/* Seq # of oldest in buffer */
    while ((diff = (sp->sendSeq - seq)) >= ((bsize = gbuf_msgsize(smp)) + eom)) {
	seq += bsize + eom;	/* update sequence # */
	if (gbuf_next(smp)) { /* if another send buffer */
	    smp = gbuf_next(smp);
	    eom = 1;
	} else if (smp == sp->csbuf_mb) { /* seen the current one? */
	    smp = 0;
	    break;
	} else if (sp->csbuf_mb) { /* look at it */
	    smp = sp->csbuf_mb;
	    eom = 0;
	} else {		/* no more buffers */
	    smp = 0;
	    break;
	}
    }			/* while */
    
    if (smp) {
	if (gbuf_next(smp) == 0)	/* last block */
	    sendAckReq = 1;
	cnt = bsize - diff;	/* # of bytes in this block */
    } else
	cnt = 0;

    /*
     * Check to see if the number of bytes is less than the 'send 
     * Blocking' setting. If so, then we won't send this data unless 
     * we're flushing.  So we set up a timer to force a flush later.
     */
    if ((cnt < sp->sendBlocking) && !sp->writeFlush) {
        InsertTimerElem(&adspGlobal.fastTimers, &sp->FlushTimer, 
			                         sp->sendInterval);
	return 0;		/* no data to send */
    }

    if (cnt > ADSP_MAX_DATA_LEN) { /* truncate to one packet */
	cnt = ADSP_MAX_DATA_LEN;
	eom = 0;
	sendAckReq = 0;	/* Won't send ack because end of data */
	partial++;
    }

    if (smp) {
	/* trim extra bytes off the beginning of the "block" before the copy */
	while (diff) {
	    if (gbuf_len(smp) > diff)
		break;
	    else
		diff -= gbuf_len(smp);
	    smp = gbuf_cont(smp);
        }
	if((gbuf_cont(mp) = gbuf_dupm(smp)) == 0) /* copy the data */
	    return 0;
	smp = gbuf_cont(mp);	/* use the new message blocks */
        gbuf_rinc(smp,diff);	/* and get to the first byte of data to send */
    }
    /*
     * Check to see if this many bytes will close the other end's 
     * receive window. If so, we need to send an ack request along 
     * with this.  sendWdwSeq is the seq # of the last byte that 
     * the remote has room for
     */
    if ((diff = sp->sendWdwSeq + 1 - sp->sendSeq) <= cnt) {
	if (diff < cnt) { /* Won't fit exactly */
	    eom = 0;	/* so can't send EOM */
	    cnt = diff;
	    partial++;
	}
	sendAckReq = 1;		/* Make him tell us new recv. window */
	sp->noXmitFlow = 1;	/* Don't do flow control calc. */
    }
    
    /* trim extra bytes off the tail of the "block" after the copy */
    if (partial && smp) {
	psmp = smp;
	tcnt = cnt;
	while (tcnt && smp) { /* while there are message blocks and data */
	    if (tcnt >= gbuf_len(smp)) {
		tcnt -= gbuf_len(smp);
		if (tcnt) {
		    psmp = smp;
		    smp = gbuf_cont(smp);
		} else {
		    if (psmp != smp) { /* not the first item on the list */
			gbuf_cont(psmp) = 0;
			gbuf_freem(smp);
			smp = psmp;
		    } else {
			gbuf_freem(gbuf_cont(smp));
			gbuf_cont(smp) = 0;
		    }
		    break;
		}
	    } else {
		gbuf_wset(smp,tcnt);
		if (gbuf_cont(smp)) {
		    gbuf_freem(gbuf_cont(smp));
		    gbuf_cont(smp) = 0;
		}
		break;
	    }
	}
    }

    sp->sendSeq += cnt + eom;	/* Update sendSeq field */

    if (GT(sp->sendSeq, sp->maxSendSeq)) /* Keep track of >st ever sent */
	sp->maxSendSeq = sp->sendSeq;
	
    if (eom)
	sp->f.descriptor = ADSP_EOM_BIT;
    else
	sp->f.descriptor = 0;
	
    if (sendAckReq || (++sp->pktSendCnt >= sp->pktSendMax)) {
	/* Last packet in a series */
	sp->f.descriptor |= ADSP_ACK_REQ_BIT; /* We want an ack to this */
	sp->waitingAck = 1;	/* Flag that we're waiting */
	sp->sendStamp = SysTicks(); /* Save time we sent request */
	sp->timerSeq = sp->sendSeq; /* Save seq # we want acked */
	InsertTimerElem(&adspGlobal.fastTimers, &sp->RetryTimer, 
			sp->rtmtInterval+TX_DLY);
    }
    return cnt + eom;
}