17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*462be471Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "uucp.h"
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include "pk.h"
367c478bd9Sstevel@tonic-gate #include <sys/buf.h>
377c478bd9Sstevel@tonic-gate
38*462be471Sceastha extern void xlatecntl();
397c478bd9Sstevel@tonic-gate extern void pkcntl(), pkoutput(), pkclose(), pkreset(), pkzero(),
407c478bd9Sstevel@tonic-gate pkgetpack(), pkxstart();
417c478bd9Sstevel@tonic-gate extern int pkread(), pkwrite(), pksack();
427c478bd9Sstevel@tonic-gate static int pksize(), chksum(), pkaccept();
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate extern int Connodata; /* Continuous No Valid Data Count */
457c478bd9Sstevel@tonic-gate extern int xpacksize;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * receive control messages
497c478bd9Sstevel@tonic-gate * c -> message type fields
507c478bd9Sstevel@tonic-gate * pk -> line control unit
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate void
pkcntl(c,pk)537c478bd9Sstevel@tonic-gate pkcntl(c, pk)
54*462be471Sceastha int c;
55*462be471Sceastha struct pack *pk;
567c478bd9Sstevel@tonic-gate {
57*462be471Sceastha int cntl, val;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate val = c & MOD8;
607c478bd9Sstevel@tonic-gate cntl = (c>>3) & MOD8;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate if ( ! ISCNTL(c) ) {
637c478bd9Sstevel@tonic-gate logent("PK0", "not cntl");
647c478bd9Sstevel@tonic-gate return;
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if (Debug >= 9)
687c478bd9Sstevel@tonic-gate xlatecntl(0, c);
697c478bd9Sstevel@tonic-gate switch(cntl) {
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate case INITB:
727c478bd9Sstevel@tonic-gate val++;
737c478bd9Sstevel@tonic-gate pk->p_xsize = xpacksize = pksizes[val];
747c478bd9Sstevel@tonic-gate pk->p_lpsize = val;
757c478bd9Sstevel@tonic-gate pk->p_bits = 1;
767c478bd9Sstevel@tonic-gate if (pk->p_state & LIVE) {
777c478bd9Sstevel@tonic-gate pk->p_msg |= M_INITC;
787c478bd9Sstevel@tonic-gate break;
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate pk->p_state |= INITb;
817c478bd9Sstevel@tonic-gate if ((pk->p_state & INITa)==0) {
827c478bd9Sstevel@tonic-gate break;
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate pk->p_rmsg &= ~M_INITA;
857c478bd9Sstevel@tonic-gate pk->p_msg |= M_INITC;
867c478bd9Sstevel@tonic-gate break;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate case INITC:
897c478bd9Sstevel@tonic-gate if ((pk->p_state&INITab)==INITab) {
907c478bd9Sstevel@tonic-gate pk->p_state = LIVE;
917c478bd9Sstevel@tonic-gate pk->p_rmsg &= ~M_INITB;
927c478bd9Sstevel@tonic-gate } else
937c478bd9Sstevel@tonic-gate pk->p_msg |= M_INITB;
947c478bd9Sstevel@tonic-gate if (val)
957c478bd9Sstevel@tonic-gate pk->p_swindow = val;
967c478bd9Sstevel@tonic-gate break;
977c478bd9Sstevel@tonic-gate case INITA:
987c478bd9Sstevel@tonic-gate if (val==0 && pk->p_state&LIVE) {
997c478bd9Sstevel@tonic-gate logent("PK0", "alloc change not implemented");
1007c478bd9Sstevel@tonic-gate break;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate if (val) {
1037c478bd9Sstevel@tonic-gate pk->p_state |= INITa;
1047c478bd9Sstevel@tonic-gate pk->p_msg |= M_INITB;
1057c478bd9Sstevel@tonic-gate pk->p_rmsg |= M_INITB;
1067c478bd9Sstevel@tonic-gate pk->p_swindow = val;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate case RJ:
1107c478bd9Sstevel@tonic-gate pk->p_state |= RXMIT;
1117c478bd9Sstevel@tonic-gate pk->p_msg |= M_RR;
1127c478bd9Sstevel@tonic-gate DEBUG(9, "pkcntl: RJ: Connodata=%d\n", Connodata);
1137c478bd9Sstevel@tonic-gate /* FALLTHRU */
1147c478bd9Sstevel@tonic-gate case RR:
1157c478bd9Sstevel@tonic-gate pk->p_rpr = val;
1167c478bd9Sstevel@tonic-gate (void) pksack(pk);
1177c478bd9Sstevel@tonic-gate break;
1187c478bd9Sstevel@tonic-gate case CLOSE:
1197c478bd9Sstevel@tonic-gate pk->p_state = DOWN+RCLOSE;
1207c478bd9Sstevel@tonic-gate return;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate if (pk->p_msg)
1237c478bd9Sstevel@tonic-gate pkoutput(pk);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate static int
pkaccept()1277c478bd9Sstevel@tonic-gate pkaccept()
1287c478bd9Sstevel@tonic-gate {
129*462be471Sceastha struct pack *pk;
130*462be471Sceastha int x,seq;
1317c478bd9Sstevel@tonic-gate char m, cntl, *p, imask, **bp;
1327c478bd9Sstevel@tonic-gate int bad,accept,skip,t,cc;
1337c478bd9Sstevel@tonic-gate unsigned short sum;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate pk = Pk;
1367c478bd9Sstevel@tonic-gate bad = accept = skip = 0;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * wait for input
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate x = next[pk->p_pr];
1427c478bd9Sstevel@tonic-gate while ((imask=pk->p_imap) == 0 && pk->p_rcount==0) {
1437c478bd9Sstevel@tonic-gate pkgetpack(pk);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate pk->p_imap = 0;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * determine input window in m.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate t = (~(-1<<pk->p_rwindow)) <<x;
1527c478bd9Sstevel@tonic-gate m = t;
1537c478bd9Sstevel@tonic-gate m |= t>>8;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * mark newly accepted input buffers
1587c478bd9Sstevel@tonic-gate */
1597c478bd9Sstevel@tonic-gate for(x=0; x<8; x++) {
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate if ((imask & mask[x]) == 0)
1627c478bd9Sstevel@tonic-gate continue;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if (((cntl=pk->p_is[x])&0200)==0) {
1657c478bd9Sstevel@tonic-gate bad++;
1667c478bd9Sstevel@tonic-gate free:
1677c478bd9Sstevel@tonic-gate bp = (char **)pk->p_ib[x];
1687c478bd9Sstevel@tonic-gate *bp = (char *)pk->p_ipool;
1697c478bd9Sstevel@tonic-gate pk->p_ipool = bp;
1707c478bd9Sstevel@tonic-gate pk->p_is[x] = 0;
1717c478bd9Sstevel@tonic-gate continue;
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate pk->p_is[x] = (char) ~(B_COPY+B_MARK);
1757c478bd9Sstevel@tonic-gate sum = (unsigned)chksum(pk->p_ib[x], pk->p_rsize) ^ (unsigned)(cntl&0377);
1767c478bd9Sstevel@tonic-gate sum += pk->p_isum[x];
1777c478bd9Sstevel@tonic-gate if (sum == CHECK) {
1787c478bd9Sstevel@tonic-gate seq = (cntl>>3) & MOD8;
1797c478bd9Sstevel@tonic-gate if (m & mask[seq]) {
1807c478bd9Sstevel@tonic-gate if (pk->p_is[seq] & (B_COPY | B_MARK)) {
1817c478bd9Sstevel@tonic-gate dup:
1827c478bd9Sstevel@tonic-gate pk->p_msg |= M_RR;
1837c478bd9Sstevel@tonic-gate skip++;
1847c478bd9Sstevel@tonic-gate goto free;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate if (x != seq) {
1877c478bd9Sstevel@tonic-gate p = pk->p_ib[x];
1887c478bd9Sstevel@tonic-gate pk->p_ib[x] = pk->p_ib[seq];
1897c478bd9Sstevel@tonic-gate pk->p_is[x] = pk->p_is[seq];
1907c478bd9Sstevel@tonic-gate pk->p_ib[seq] = p;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate pk->p_is[seq] = B_MARK;
1937c478bd9Sstevel@tonic-gate accept++;
1947c478bd9Sstevel@tonic-gate cc = 0;
1957c478bd9Sstevel@tonic-gate if (cntl&B_SHORT) {
1967c478bd9Sstevel@tonic-gate pk->p_is[seq] = B_MARK+B_SHORT;
1977c478bd9Sstevel@tonic-gate p = pk->p_ib[seq];
1987c478bd9Sstevel@tonic-gate cc = (unsigned)*p++ & 0377;
1997c478bd9Sstevel@tonic-gate if (cc & 0200) {
2007c478bd9Sstevel@tonic-gate cc &= 0177;
2017c478bd9Sstevel@tonic-gate cc |= *p << 7;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate pk->p_isum[seq] = pk->p_rsize - cc;
2057c478bd9Sstevel@tonic-gate } else {
2067c478bd9Sstevel@tonic-gate goto dup;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate } else {
2097c478bd9Sstevel@tonic-gate bad++;
2107c478bd9Sstevel@tonic-gate goto free;
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate * scan window again turning marked buffers into
2167c478bd9Sstevel@tonic-gate * COPY buffers and looking for missing sequence
2177c478bd9Sstevel@tonic-gate * numbers.
2187c478bd9Sstevel@tonic-gate */
2197c478bd9Sstevel@tonic-gate accept = 0;
2207c478bd9Sstevel@tonic-gate for(x=next[pk->p_pr],t= -1; m & mask[x]; x = next[x]) {
2217c478bd9Sstevel@tonic-gate if (pk->p_is[x] & B_MARK)
2227c478bd9Sstevel@tonic-gate pk->p_is[x] |= B_COPY;
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate if (pk->p_is[x] & B_COPY) {
2257c478bd9Sstevel@tonic-gate if (t >= 0) {
2267c478bd9Sstevel@tonic-gate bp = (char **)pk->p_ib[x];
2277c478bd9Sstevel@tonic-gate *bp = (char *)pk->p_ipool;
2287c478bd9Sstevel@tonic-gate pk->p_ipool = bp;
2297c478bd9Sstevel@tonic-gate pk->p_is[x] = 0;
2307c478bd9Sstevel@tonic-gate skip++;
2317c478bd9Sstevel@tonic-gate } else
2327c478bd9Sstevel@tonic-gate accept++;
2337c478bd9Sstevel@tonic-gate } else if (t<0)
2347c478bd9Sstevel@tonic-gate t = x;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate if (bad) {
2387c478bd9Sstevel@tonic-gate pk->p_msg |= M_RJ;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate if (skip) {
2427c478bd9Sstevel@tonic-gate pk->p_msg |= M_RR;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate pk->p_rcount = accept;
2467c478bd9Sstevel@tonic-gate return(accept);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate int
pkread(ibuf,icount)2517c478bd9Sstevel@tonic-gate pkread(ibuf, icount)
2527c478bd9Sstevel@tonic-gate char *ibuf;
2537c478bd9Sstevel@tonic-gate int icount;
2547c478bd9Sstevel@tonic-gate {
255*462be471Sceastha struct pack *pk;
256*462be471Sceastha int x;
2577c478bd9Sstevel@tonic-gate int is,cc,xfr,count;
2587c478bd9Sstevel@tonic-gate char *cp, **bp;
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate pk = Pk;
2617c478bd9Sstevel@tonic-gate xfr = 0;
2627c478bd9Sstevel@tonic-gate count = 0;
2637c478bd9Sstevel@tonic-gate while (pkaccept()==0)
2647c478bd9Sstevel@tonic-gate ;
2657c478bd9Sstevel@tonic-gate Connodata = 0; /* accecpted a packet -- good data */
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate while (icount) {
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate x = next[pk->p_pr];
2717c478bd9Sstevel@tonic-gate is = pk->p_is[x];
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate if (is & B_COPY) {
2747c478bd9Sstevel@tonic-gate cc = MIN(pk->p_isum[x], icount);
2757c478bd9Sstevel@tonic-gate if (cc==0 && xfr) {
2767c478bd9Sstevel@tonic-gate break;
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate if (is & B_RESID)
2797c478bd9Sstevel@tonic-gate cp = pk->p_rptr;
2807c478bd9Sstevel@tonic-gate else {
2817c478bd9Sstevel@tonic-gate cp = pk->p_ib[x];
2827c478bd9Sstevel@tonic-gate if (is & B_SHORT) {
2837c478bd9Sstevel@tonic-gate if (*cp++ & 0200)
2847c478bd9Sstevel@tonic-gate cp++;
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate if (cc)
2887c478bd9Sstevel@tonic-gate memcpy(ibuf, cp, cc);
2897c478bd9Sstevel@tonic-gate ibuf += cc;
2907c478bd9Sstevel@tonic-gate icount -= cc;
2917c478bd9Sstevel@tonic-gate count += cc;
2927c478bd9Sstevel@tonic-gate xfr++;
2937c478bd9Sstevel@tonic-gate pk->p_isum[x] -= cc;
2947c478bd9Sstevel@tonic-gate if (pk->p_isum[x] == 0) {
2957c478bd9Sstevel@tonic-gate pk->p_pr = x;
2967c478bd9Sstevel@tonic-gate bp = (char **)pk->p_ib[x];
2977c478bd9Sstevel@tonic-gate *bp = (char *)pk->p_ipool;
2987c478bd9Sstevel@tonic-gate pk->p_ipool = bp;
2997c478bd9Sstevel@tonic-gate pk->p_is[x] = 0;
3007c478bd9Sstevel@tonic-gate pk->p_rcount--;
3017c478bd9Sstevel@tonic-gate pk->p_msg |= M_RR;
3027c478bd9Sstevel@tonic-gate } else {
3037c478bd9Sstevel@tonic-gate pk->p_rptr = cp+cc;
3047c478bd9Sstevel@tonic-gate pk->p_is[x] |= B_RESID;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate if (cc==0)
3077c478bd9Sstevel@tonic-gate break;
3087c478bd9Sstevel@tonic-gate } else
3097c478bd9Sstevel@tonic-gate break;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate pkoutput(pk);
3127c478bd9Sstevel@tonic-gate return(count);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate /* return number of bytes writtten */
3167c478bd9Sstevel@tonic-gate int
pkwrite(ibuf,icount)3177c478bd9Sstevel@tonic-gate pkwrite(ibuf, icount)
3187c478bd9Sstevel@tonic-gate char *ibuf;
3197c478bd9Sstevel@tonic-gate int icount;
3207c478bd9Sstevel@tonic-gate {
321*462be471Sceastha struct pack *pk;
322*462be471Sceastha int x;
3237c478bd9Sstevel@tonic-gate caddr_t cp;
3247c478bd9Sstevel@tonic-gate int partial;
3257c478bd9Sstevel@tonic-gate int cc, fc, count;
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate pk = Pk;
3287c478bd9Sstevel@tonic-gate if (pk->p_state&DOWN || !pk->p_state&LIVE) {
3297c478bd9Sstevel@tonic-gate return(-1);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate count = icount;
3337c478bd9Sstevel@tonic-gate do {
3347c478bd9Sstevel@tonic-gate while (pk->p_xcount>=pk->p_swindow) {
3357c478bd9Sstevel@tonic-gate pkoutput(pk);
3367c478bd9Sstevel@tonic-gate pkgetpack(pk);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate x = next[pk->p_pscopy];
3397c478bd9Sstevel@tonic-gate while (pk->p_os[x]!=B_NULL) {
3407c478bd9Sstevel@tonic-gate pkgetpack(pk);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate pk->p_os[x] = B_MARK;
3437c478bd9Sstevel@tonic-gate pk->p_pscopy = x;
3447c478bd9Sstevel@tonic-gate pk->p_xcount++;
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate cp = pk->p_ob[x] = (caddr_t) malloc((unsigned) pk->p_xsize);
3477c478bd9Sstevel@tonic-gate partial = 0;
3487c478bd9Sstevel@tonic-gate if ((int)icount < pk->p_xsize) {
3497c478bd9Sstevel@tonic-gate cc = icount;
3507c478bd9Sstevel@tonic-gate fc = pk->p_xsize - cc;
3517c478bd9Sstevel@tonic-gate *cp = fc&0177;
3527c478bd9Sstevel@tonic-gate if (fc > 127) {
3537c478bd9Sstevel@tonic-gate *cp++ |= 0200;
3547c478bd9Sstevel@tonic-gate *cp++ = fc>>7;
3557c478bd9Sstevel@tonic-gate } else
3567c478bd9Sstevel@tonic-gate cp++;
3577c478bd9Sstevel@tonic-gate partial = B_SHORT;
3587c478bd9Sstevel@tonic-gate } else
3597c478bd9Sstevel@tonic-gate cc = pk->p_xsize;
3607c478bd9Sstevel@tonic-gate memcpy(cp, ibuf, cc);
3617c478bd9Sstevel@tonic-gate ibuf += cc;
3627c478bd9Sstevel@tonic-gate icount -= cc;
3637c478bd9Sstevel@tonic-gate pk->p_osum[x] = chksum(pk->p_ob[x], pk->p_xsize);
3647c478bd9Sstevel@tonic-gate pk->p_os[x] = B_READY+partial;
3657c478bd9Sstevel@tonic-gate pkoutput(pk);
3667c478bd9Sstevel@tonic-gate } while (icount);
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate return(count);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate int
pksack(pk)3727c478bd9Sstevel@tonic-gate pksack(pk)
373*462be471Sceastha struct pack *pk;
3747c478bd9Sstevel@tonic-gate {
375*462be471Sceastha int x, i;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate i = 0;
3787c478bd9Sstevel@tonic-gate for(x=pk->p_ps; x!=pk->p_rpr; ) {
3797c478bd9Sstevel@tonic-gate x = next[x];
3807c478bd9Sstevel@tonic-gate if (pk->p_os[x]&B_SENT) {
3817c478bd9Sstevel@tonic-gate i++;
3827c478bd9Sstevel@tonic-gate Connodata = 0;
3837c478bd9Sstevel@tonic-gate pk->p_os[x] = B_NULL;
3847c478bd9Sstevel@tonic-gate pk->p_state &= ~WAITO;
3857c478bd9Sstevel@tonic-gate pk->p_xcount--;
3867c478bd9Sstevel@tonic-gate free((char *) pk->p_ob[x]);
3877c478bd9Sstevel@tonic-gate pk->p_ps = x;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate return(i);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate void
pkoutput(pk)3957c478bd9Sstevel@tonic-gate pkoutput(pk)
396*462be471Sceastha struct pack *pk;
3977c478bd9Sstevel@tonic-gate {
398*462be471Sceastha int x;
3997c478bd9Sstevel@tonic-gate char bstate;
4007c478bd9Sstevel@tonic-gate int i;
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate if (pk->p_obusy++) {
4037c478bd9Sstevel@tonic-gate pk->p_obusy--;
4047c478bd9Sstevel@tonic-gate return;
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate * find seq number and buffer state
4107c478bd9Sstevel@tonic-gate * of next output packet
4117c478bd9Sstevel@tonic-gate */
4127c478bd9Sstevel@tonic-gate if (pk->p_state&RXMIT)
4137c478bd9Sstevel@tonic-gate pk->p_nxtps = next[pk->p_rpr];
4147c478bd9Sstevel@tonic-gate x = pk->p_nxtps;
4157c478bd9Sstevel@tonic-gate bstate = pk->p_os[x];
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate * Send control packet if indicated
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate if (pk->p_msg) {
4227c478bd9Sstevel@tonic-gate if (pk->p_msg & ~M_RR || !(bstate&B_READY) ) {
4237c478bd9Sstevel@tonic-gate x = pk->p_msg;
4247c478bd9Sstevel@tonic-gate for(i=0; i<8; i++)
4257c478bd9Sstevel@tonic-gate if (x&1)
4267c478bd9Sstevel@tonic-gate break;
4277c478bd9Sstevel@tonic-gate else
4287c478bd9Sstevel@tonic-gate x >>= 1;
4297c478bd9Sstevel@tonic-gate x = i;
4307c478bd9Sstevel@tonic-gate x <<= 3;
4317c478bd9Sstevel@tonic-gate switch(i) {
4327c478bd9Sstevel@tonic-gate case CLOSE:
4337c478bd9Sstevel@tonic-gate break;
4347c478bd9Sstevel@tonic-gate case RJ:
4357c478bd9Sstevel@tonic-gate case RR:
4367c478bd9Sstevel@tonic-gate x += pk->p_pr;
4377c478bd9Sstevel@tonic-gate break;
4387c478bd9Sstevel@tonic-gate case INITB:
4397c478bd9Sstevel@tonic-gate x += pksize(pk->p_rsize);
4407c478bd9Sstevel@tonic-gate break;
4417c478bd9Sstevel@tonic-gate case INITC:
4427c478bd9Sstevel@tonic-gate x += pk->p_rwindow;
4437c478bd9Sstevel@tonic-gate break;
4447c478bd9Sstevel@tonic-gate case INITA:
4457c478bd9Sstevel@tonic-gate x += pk->p_rwindow;
4467c478bd9Sstevel@tonic-gate break;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate pk->p_msg &= ~mask[i];
4507c478bd9Sstevel@tonic-gate pkxstart(pk, x, -1);
4517c478bd9Sstevel@tonic-gate goto out;
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * Don't send data packets if line is marked dead.
4587c478bd9Sstevel@tonic-gate */
4597c478bd9Sstevel@tonic-gate if (pk->p_state&DOWN) {
4607c478bd9Sstevel@tonic-gate goto out;
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate * Start transmission (or retransmission) of data packets.
4657c478bd9Sstevel@tonic-gate */
4667c478bd9Sstevel@tonic-gate if (bstate & (B_READY|B_SENT)) {
4677c478bd9Sstevel@tonic-gate char seq;
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate bstate |= B_SENT;
4707c478bd9Sstevel@tonic-gate seq = x;
4717c478bd9Sstevel@tonic-gate pk->p_nxtps = next[x];
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate x = 0200+pk->p_pr+(seq<<3);
4747c478bd9Sstevel@tonic-gate if (bstate & B_SHORT)
4757c478bd9Sstevel@tonic-gate x |= 0100;
4767c478bd9Sstevel@tonic-gate pkxstart(pk, x, seq);
4777c478bd9Sstevel@tonic-gate pk->p_os[seq] = bstate;
4787c478bd9Sstevel@tonic-gate pk->p_state &= ~RXMIT;
4797c478bd9Sstevel@tonic-gate pk->p_nout++;
4807c478bd9Sstevel@tonic-gate goto out;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate * enable timeout if there's nothing to send
4857c478bd9Sstevel@tonic-gate * and transmission buffers are languishing
4867c478bd9Sstevel@tonic-gate */
4877c478bd9Sstevel@tonic-gate if (pk->p_xcount) {
4887c478bd9Sstevel@tonic-gate pk->p_timer = 2;
4897c478bd9Sstevel@tonic-gate pk->p_state |= WAITO;
4907c478bd9Sstevel@tonic-gate } else
4917c478bd9Sstevel@tonic-gate pk->p_state &= ~WAITO;
4927c478bd9Sstevel@tonic-gate out:
4937c478bd9Sstevel@tonic-gate pk->p_obusy = 0;
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate * shut down line by ignoring new input
4987c478bd9Sstevel@tonic-gate * letting output drain
4997c478bd9Sstevel@tonic-gate * releasing space
5007c478bd9Sstevel@tonic-gate */
5017c478bd9Sstevel@tonic-gate void
pkclose()5027c478bd9Sstevel@tonic-gate pkclose()
5037c478bd9Sstevel@tonic-gate {
504*462be471Sceastha struct pack *pk;
505*462be471Sceastha int i;
5067c478bd9Sstevel@tonic-gate int rcheck;
5077c478bd9Sstevel@tonic-gate char **bp;
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate pk = Pk;
5107c478bd9Sstevel@tonic-gate pk->p_state |= DRAINO;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate * try to flush output
5147c478bd9Sstevel@tonic-gate */
5157c478bd9Sstevel@tonic-gate i = 0;
5167c478bd9Sstevel@tonic-gate pk->p_timer = 2;
5177c478bd9Sstevel@tonic-gate while (pk->p_xcount && pk->p_state&LIVE) {
5187c478bd9Sstevel@tonic-gate if (pk->p_state&(RCLOSE+DOWN) || ++i > 2)
5197c478bd9Sstevel@tonic-gate break;
5207c478bd9Sstevel@tonic-gate pkoutput(pk);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate pk->p_timer = 0;
5237c478bd9Sstevel@tonic-gate pk->p_state |= DOWN;
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate * try to exchange CLOSE messages
5277c478bd9Sstevel@tonic-gate */
5287c478bd9Sstevel@tonic-gate i = 0;
5297c478bd9Sstevel@tonic-gate while ((pk->p_state&RCLOSE)==0 && i<2) {
5307c478bd9Sstevel@tonic-gate pk->p_msg = M_CLOSE;
5317c478bd9Sstevel@tonic-gate pk->p_timer = 2;
5327c478bd9Sstevel@tonic-gate pkoutput(pk);
5337c478bd9Sstevel@tonic-gate i++;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate * free space
5387c478bd9Sstevel@tonic-gate */
5397c478bd9Sstevel@tonic-gate rcheck = 0;
5407c478bd9Sstevel@tonic-gate for (i=0;i<8;i++) {
5417c478bd9Sstevel@tonic-gate if (pk->p_os[i]!=B_NULL) {
5427c478bd9Sstevel@tonic-gate free((char *) pk->p_ob[i]);
5437c478bd9Sstevel@tonic-gate pk->p_xcount--;
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate if (pk->p_is[i]!=B_NULL) {
5467c478bd9Sstevel@tonic-gate free((char *) pk->p_ib[i]);
5477c478bd9Sstevel@tonic-gate rcheck++;
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate while (pk->p_ipool != NULL) {
5517c478bd9Sstevel@tonic-gate bp = pk->p_ipool;
5527c478bd9Sstevel@tonic-gate pk->p_ipool = (char **)*bp;
5537c478bd9Sstevel@tonic-gate rcheck++;
5547c478bd9Sstevel@tonic-gate free((char *) bp);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate if (rcheck != pk->p_rwindow) {
5577c478bd9Sstevel@tonic-gate logent("PK0", "pkclose rcheck != p_rwindow");
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate free((char *) pk);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate void
pkreset(pk)5647c478bd9Sstevel@tonic-gate pkreset(pk)
565*462be471Sceastha struct pack *pk;
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate pk->p_ps = pk->p_pr = pk->p_rpr = 0;
5697c478bd9Sstevel@tonic-gate pk->p_nxtps = 1;
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate static int
chksum(s,n)5737c478bd9Sstevel@tonic-gate chksum(s,n)
574*462be471Sceastha char *s;
575*462be471Sceastha int n;
5767c478bd9Sstevel@tonic-gate {
577*462be471Sceastha short sum;
578*462be471Sceastha unsigned short t;
579*462be471Sceastha short x;
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate sum = -1;
5827c478bd9Sstevel@tonic-gate x = 0;
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate do {
5857c478bd9Sstevel@tonic-gate if (sum<0) {
5867c478bd9Sstevel@tonic-gate sum <<= 1;
5877c478bd9Sstevel@tonic-gate sum++;
5887c478bd9Sstevel@tonic-gate } else
5897c478bd9Sstevel@tonic-gate sum <<= 1;
5907c478bd9Sstevel@tonic-gate t = sum;
5917c478bd9Sstevel@tonic-gate sum += (unsigned)*s++ & 0377;
5927c478bd9Sstevel@tonic-gate x += sum^n;
5937c478bd9Sstevel@tonic-gate if ((unsigned short)sum <= t) {
5947c478bd9Sstevel@tonic-gate sum ^= x;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate } while (--n > 0);
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate return(sum);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate static int
pksize(n)6027c478bd9Sstevel@tonic-gate pksize(n)
603*462be471Sceastha int n;
6047c478bd9Sstevel@tonic-gate {
605*462be471Sceastha int k;
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate n >>= 5;
6087c478bd9Sstevel@tonic-gate for(k=0; n >>= 1; k++);
6097c478bd9Sstevel@tonic-gate return(k);
6107c478bd9Sstevel@tonic-gate }
611