/*********************************************************************** 
* 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:	smc_tm_r.c
 *
 * Description:
 *	The driver routines for handling receive
 *	on an TM port.
 *
 * Routines:
 *	smc_tm_rxfr
 *	smc_tm_busy
 *
 * Author:
 *	Ofrit Lesser
 ********************************************************/

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


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


/********************************************************
 * routine:	smc_tm_rxfr
 *
 * description:
 *	This is reached after being sent a RX_IND mesage
 *	by the interrupt routine (interrupt on QUICC
 *	receiving an Transparent buffer).
 *	The receive buffer table is flushed of all TM
 *	buffers received: these buffers 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.
 *
 ********************************************************/
smc_tm_rxfr(h, child)
HDR *h;
CHILD *child;
{
	GCT *gct;
	register WORK_AREA *wa;
	register unsigned short status;		/* status of BD */
	register R_FRAME *r, *r1;
	register QUICC_BD *nextBD;	/* next BD in frame */
	int crc;	/* number of bytes in crc */
	int flen;	/* accumulative frame length */
	struct smc_regs *regs;

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

	/*
	 * clear event bit
	 * this clears indications of old (masked) events
	 */
	regs->smc_smce = (TM_RBD | TM_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 ){
		regs->smc_smcm |= TM_RBD;
		return;
	}

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


		flen = nextBD->length;

		/*
		 * update status and length
		 * in new received frame,
		 * zero accumulative length
		 * for next frame
		 */
		r->flen = flen;	/* frame length */
		r->blen = flen;	/* frame length */

		driver_trace(RX_IND, r->hdr.id, (int)r);
		/*
		 * 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);
			r->hdr.status = RECEIVE_ERROR;
			r->hdr.err_info = status & R_ERROR;
			wa->rerr_cnt++;
			if( (status&R_ERROR) & ~wa->rxerr_mask )
				smc_return_to_pool(r, child);
			else
				if( (*gct->send)(child->upper, r) )
					smc_return_to_pool(r, child);
		} else
			if( (*gct->send)(child->upper, r) )
				smc_return_to_pool(r, child);
		r1 = r->nextb;
		r->nextb = 0;	/* no more buffs in this frame */
		r = r1;
		wa->rcount++;

		/*
		 * advance rbd (to next Rx BD in table)
		 */
		if( status & R_W )
			nextBD = SMC_RBD_ADDR(wa);
		else
			nextBD++;
	
		wa->rfree++;
		wa->quicc_rbd = nextBD;
		status = nextBD->status;	/* update status */
	}

	wa->rx_q = r;

	/*
	 * replenish the rx BD table
	 */
	status = smc_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 )
			regs->smc_smcm |= TM_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.
	 */
	regs->smc_smcm |= TM_RBD;
}


/********************************************************
 * routine:	smc_tm_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:
 *
 ********************************************************/
smc_tm_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
	 */
	smc_tm_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);
		}
	}
}
