/*********************************************************************** 
* NOTICE
* All files contained on this disk are subject to the licensing conditions
* issued by MOTOROLA Inc.
*
* All files are copyright 1993 by MOTOROLA Inc. 
************************************************************************/


/********************************************************
 * File:	hdlc_r.c
 *
 * Description:
 *	The driver routines for handling receive
 *	on an HDLC port.
 *
 * Routines:
 *	hdlc_rxfr
 *	hdlc_busy
 *
 * Author:
 *	Jonathan Masel
 ********************************************************/

#include "gct.h"
#include "types.h"
#include "mtypes.h"
#include "msg.h"
#include "hdlc.h"
#include "bss.h"
#include "quicc.h"
#include "status.h"


extern driver_trace(int type, int id, volatile int par);


/********************************************************
 * routine:	hdlc_rxfr
 *
 * description:
 *	This is reached after being sent a RX_IND mesage
 *	by the interrupt routine (interrupt on QUICC
 *	receiving an HDLC frame).
 *	The receive buffer table is flushed of all HDLC
 *	frames received: these frames are sent to the
 *	port's upper layer.
 *	The message sent is only a 32-bit code (not an
 *	allocated MSG structure) and therefore must
 *	not be relm-ed. Its structure corresponds to
 *	the first 32-bits of a normal HDR structure.
 *
 * arguments:
 *	h		the event message received
 *
 * return code:
 *
 * side effects:
 *	Advance quicc_rbd each time a
 *	received buffer is collected.
 *	The rxfr interrupt must be re-enabled after
 *	all received frames have been collected.
 *
 ********************************************************/
hdlc_rxfr(h, child)
HDR *h;
CHILD *child;
{
	GCT *gct;
	register WORK_AREA *wa;
	register unsigned short status;		/* status of BD */
	register R_FRAME *r1, *r2;
			/* r1 = first r_frame in frame */
			/* r2 = current r_frame */
	register R_FRAME *r3;
			/* r3 = previous r_frame */
	register QUICC_BD *nextBD;	/* next BD in frame */
	int flen;	/* accumulative frame length */
	int plen;	/* previous frame length (till last buffer) */
	char eofr;	/* boolean: end of frame recognized */
	char bcount;	/* number of BDs in this frame */
	int crc;	/* number of bytes in crc */

	GETGCT(gct);
	wa = (WORK_AREA *)child->work_area;

	/*
	 * clear event bit
	 * this clears indications of old (masked) events
	 */
	wa->regs->scc_scce = (HDLC_RXF | HDLC_BSY);

	/*
	 * if receive is disabled,
	 * return immediately
	 */
	if( child->flags & RX_DISABLE )
		if( child->flags & RX_ACTIVATE )
			child->flags &= ~RX_ACTIVATE;
		else
			return;

	if( !wa->rx_q->nextb ){
		wa->regs->scc_sccm |= HDLC_RXF;
		return;
	}

	/*
	 * rbd address next BD for receive
	 */
	nextBD = wa->quicc_rbd;
	status = nextBD->status;
	r1 = r2 = r3 = wa->rx_q;
	flen = 0;
	plen = 0;
	eofr = bcount = 0;
	while( !(status & R_E) ){
		/*
		 * first check that r2 has been
		 * linked to the rx BD table
		 */
		if( !(r2->flags & LINKED_TO_TABLE) )
			break;

		bcount++;

		/*
		 * if first is set on the not first
		 * frame, the last buffer(s) where lost (busy?)
		 * and r2 is now start of new frame
		 */
		if( (r1 != r2) && (status & R_F) ){
			r1->flen = plen;
			r1->hdr.status = RECEIVE_ERROR;
			r1->hdr.err_info = R_BUSY;
			r3->nextb = 0;
			wa->rerr_cnt++;
			driver_trace(RX_IND, h->id, (int)r1);
			/* send frame to upper layer */
			if( R_BUSY & ~wa->rxerr_mask )
				return_to_pool(r1, child);
			else
				if( (*gct->send)(child->upper, r1) )
					return_to_pool(r1, child);

			/*
			 * adjust accumulative
			 * frame lengths
			 */
			flen = 0;
			plen = 0;

			/* advance frame pointer */
			r1 = r2;
			eofr = 1;
		}


		/*
		 * update accumulative frame length
		 * on the last BD in a frame, the length
		 * is the length of the entire HDLC frame.
		 */
		plen = flen;
		if( status & R_L ){
			flen = nextBD->length;
			/*
			 * remove the CRC from valid frames
			 * (note that the CRC may be split between
			 * the last two buffers)
			 */
			if( !(status & R_ERROR) ){
				crc = (wa->regs->scc_psmr & CRC32? 4: 2);
				flen -= crc;
			}
			if( status & R_LG )
				r2->blen = wa->fr_lng - plen;
			else if( flen >= plen )
				/* CRC in last buffer */
				r2->blen = flen - plen;
			else {
				/* some of CRC in second last buffer */
				r2->blen = 0;
				r3->blen -= (plen - flen);
				/* release emptied buffer */
				r3->nextb = r2->nextb;
				r2->nextb = 0;
				return_to_pool(r2, child);
				r2 = r3;
			}
		} else {
			flen += nextBD->length;
			r2->blen = nextBD->length;
		}


		/*
		 * if end of frame ...
		 */
		if( status & R_L ){
			/*
			 * update status and length
			 * in new received frame,
			 * zero accumulative length
			 * for next frame
			 */
			r1->flen = flen;	/* frame total length */
			r3 = r2;
			r2 = r2->nextb;
			r3->nextb = 0;	/* no more buffs in this frame */

			/*
			 * prepare for next R_FRAME
			 */
			flen = 0;

			driver_trace(RX_IND, r1->hdr.id, (int)r1);
			/*
			 * pass r_frame to upper layer
			 * (subject to error mask).
			 * buffers are linked via nextb
			 */
			if( status & R_ERROR ){
				driver_trace(RX_ERROR, h->id, (int)nextBD);
				r1->hdr.status = RECEIVE_ERROR;
				r1->hdr.err_info = status & R_ERROR;
				wa->rerr_cnt++;
				if( (status&R_ERROR) & ~wa->rxerr_mask )
					return_to_pool(r1, child);
				else
					if( (*gct->send)(child->upper, r1) )
						return_to_pool(r1, child);
			} else
				if( (*gct->send)(child->upper, r1) )
					return_to_pool(r1, child);
			r1 = r3 = r2;
			wa->rcount++;
			eofr = 1;
		} else {
			r3 = r2;
			r2 = r2->nextb;	/* next R_FRAME for rx */
		}

		/*
		 * advance rbd (to next Rx BD in table)
		 */
		if( status & R_W )
			nextBD = RBD_ADDR(wa);
		else
			nextBD++;

		if( eofr ){
			wa->rfree += bcount;
			eofr = bcount = 0;
			wa->quicc_rbd = nextBD;
		}

		status = nextBD->status;	/* update status */
	}

	wa->rx_q = r1;

	/*
	 * replenish the rx BD table
	 */
	status = replenish_rxbd(child);
	if( status ){
		MSG *m;
		extern MSG *getm();

		driver_trace(BUSY_IND, h->id, status);
		m = getm(BUSY_IND, h->id, 0, 0);
		if( m ){
			m->hdr.status = status;
			if( (*gct->send)( child->upper, m) )
				relm((HDR *)m);
		}
		/*
		 * enable BUSY interrupts if
		 * local busy was just cleared
		 */
		if( status == LOCAL_BUSY_CLEARED )
			wa->regs->scc_sccm |= HDLC_BSY;
	}

	/*
	 * re-enable interrupts
	 * setting the mask will result in an interrupt
	 * if any frames were received during the time
	 * the mask was zero. therefore, frames will not
	 * go unnoticed if received after exiting the while
	 * loop above and before the mask bit is set.
	 */
	wa->regs->scc_sccm |= HDLC_RXF;
}


/********************************************************
 * routine:	hdlc_busy
 *
 * description:
 *	Notify the upper layer that the
 *	busy condition has been encountered.
 *
 * arguments:
 *	h		points to dummy (32 bit) header
 *	child		child structure
 *
 * return code:
 *
 * side effects:
 *
 ********************************************************/
hdlc_busy(h, child)
HDR *h;
CHILD *child;
{
	GCT *gct;
	MSG *m;
	extern MSG *getm();

	/*
	 * mark local busy
	 * (checked when returning to pool)
	 */
	child->flags |= LOCAL_BUSY;

	/*
	 * NOTE: this should be redundant
	 */
	hdlc_rxfr(h, child);

	/*
	 * if still busy, notify upper layer
	 */
	if( child->flags & (LOCAL_BUSY | POOL_EMPTY) ){
		GETGCT(gct);
		driver_trace(BUSY_IND, h->id, LOCAL_BUSY_DETECTED);
		m = getm(BUSY_IND, h->id, 0, 0);
		if( m ){
			m->hdr.status = LOCAL_BUSY_DETECTED;
			if( (*gct->send)(child->upper, m) )
				relm((HDR *)m);
		}
	}
}
