// Started with ../delay/delay.c
//

/*
 * Copyright (c) 2009 Washington University in St. Louis.
 * All rights reserved
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. The name of the author or Washington University may not be used 
 *       to endorse or promote products derived from this source code 
 *       without specific prior written permission.
 *    4. Conditions of any other entities that contributed to this are also
 *       met. If a copyright notice is present from another entity, it must
 *       be maintained in redistributions of the source code.
 *
 * THIS INTELLECTUAL PROPERTY (WHICH MAY INCLUDE BUT IS NOT LIMITED TO SOFTWARE,
 * FIRMWARE, VHDL, etc) IS PROVIDED BY THE AUTHOR AND WASHINGTON UNIVERSITY 
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR WASHINGTON UNIVERSITY 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS INTELLECTUAL PROPERTY, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * */
/*
 * File: delay++.c  
 * Author: Ken Wong
 * Email: kenw@arl.wustl.edu
 * Organization: Applied Research Laboratory
 * 
 * Derived from: pluginFramework/nstat.c
 *
 * Date Created: 03/27/2009 
 * 
 * Description: delay pkt n msec
 *
 * Modification History:
 * 	3/27/09	version v0
 *
 *
 */

#include <memory.h>
#include "plugin_api.h"
#include "plugin_dl.h"

#include "scratch_rings_WU.h"
#include "sram_rings_WU.h"


//-----------------------------------------------------------
// constants
//-----------------------------------------------------------
			// plugin counters
#define	PKT_COUNT	0	// #pkts received by handle_pkt_user()
#define CB_COUNT	1	// #pkts sent by callback()
#define ERR_COUNT	3	// #errors

			// error numbers
#define	OK_ERR			0
#define	BAD_DELAYQ_INIT_ERR	1
#define	BAD_ENQ_ERR		2

//-----------------------------------------------------------
// typedefs, unions, enums
//-----------------------------------------------------------
union tm_tag {
    long long	tm;
    struct {
	unsigned long	hi;
	unsigned long	lo;
    }	tm2;
};

// sizeof(struct item_tag) = 36 ==> 29,127 items in 1 MB
struct item_tag {
    union tm_tag	tdepart;	// time for pkt to leave
    plugin_out_data	metapkt;
    struct item_tag	*next;
};

struct queue_tag {
    unsigned long	npkts;		// #pkts in queue
    unsigned long	nbytes;		// #bytes in queue
    unsigned long	maxinq;		// max #pkts in queue
    unsigned long	ndrops;		// #overflows from queue
    unsigned long	nerrs;		// #errors other than drops
    struct item_tag	*hd;		// head ptr
    struct item_tag	*tl;		// tail ptr
    struct item_tag *free_hd;		// free list
};


//-----------------------------------------------------------
// Global variables/Registers
//-----------------------------------------------------------
//
// >> thread-specific globals <<
__declspec(gp_reg) int dlNextBlock;  // where to send packets to next
__declspec(gp_reg) int dlFromBlock;  // where to get packets from
__declspec(gp_reg) int msgNextBlock; // where to send control messages to next
__declspec(gp_reg) int msgFromBlock; // where to get control messages from

// see ring_formats.h for struct definitions
volatile __declspec(gp_reg) plc_plugin_data ring_in;	// ring data from PLC
volatile __declspec(gp_reg) plugin_out_data ring_out;	// ring data to nxt blk

const unsigned int SLEEP_CYCLES = 14000;  // cycles between callbacks (10 usec)
__declspec(gp_reg) unsigned int pluginId;		// plugin id (0...7)

// >> user globals <<
__declspec(shared gp_reg) unsigned int delay = 50;	// delay (msec)
__declspec(shared gp_reg) unsigned int npkts = 0;	// #pkts seen by plugin
__declspec(shared gp_reg) unsigned int nsent = 0;	// #pkts sent by plugin
__declspec(shared gp_reg) unsigned int ndrops = 0;	// #pkts dropped
__declspec(shared gp_reg) unsigned int maxinq = 0;	// max # pkts in queue

#define UNLOCKED 0
#define LOCKED   1
__declspec(shared gp_reg) unsigned int queue_lock;

#define	MAX_QUEUE_SZ	29000
__declspec(shared, sram) struct queue_tag queue;


//-----------------------------------------------------------
// Function prototypes
//-----------------------------------------------------------
void handle_pkt_user();
void handle_msg_user();
void plugin_init_user();

static void wait_packet_signal(SIGNAL *);
static void send_packet_signal(SIGNAL *);

#include "plugin_helpers.h"

// forward reference
int queue_init( __declspec(shared, sram) struct queue_tag *qptr );
struct item_tag *queue_alloc(
		__declspec(shared, sram) struct queue_tag *qptr );
void queue_free( __declspec(shared, sram) struct queue_tag *qptr,
		 struct item_tag *item );
int queue_enq(	__declspec(shared, sram) struct queue_tag *qptr,
		volatile __declspec(gp_reg) plc_plugin_data ring_in,
    		union tm_tag tdepart);
int queue_pop( __declspec(shared sram) struct queue_tag *qptr );


//-----------------------------------------------------------
// New helper functions 
//-----------------------------------------------------------

// handle errors
#define	BAD_QUEUE_INIT_ERR	1	// bad queue_init()
#define	BAD_ENQ_ERR		2	// bad queue_enq()
#define	BAD_POP_EMPTY_ERR	3	// bad queue_pop() - empty queue
#define	BAD_POP_FREE_ERR	4	// bad queue_pop() - free() failed

__declspec(shared gp_reg) unsigned int nerrs;		// #errors
volatile __declspec(shared sram) unsigned int errno[5];	// 1st 5 errors

// record error number
//									<<<<<
static __forceinline void
helper_set_errno( __declspec(local_mem) unsigned int n ) {
    if( nerrs < 5 )	errno[nerrs] = n;
    ++nerrs;
    onl_api_plugin_cntr_inc(pluginId, 0);	// external error counter
}

// set ring_out qid given output port# and external qid
//									<<<<<
static __forceinline void
helper_set_meta_qid(	__declspec(gp_reg) unsigned int out_port,
			__declspec(gp_reg) unsigned int xqid ) {
    ring_out.plugin_qm_data_out.qid = (out_port+1 << 13) | xqid;
}

// reset global counters
//									<<<<<
static __forceinline void
reset_counters( void ) {
    npkts = 0;
    nsent = 0;
    maxinq = 0;
    ndrops = 0;

    nerrs = 0;
    errno[0] = 0;	errno[1] = 0;	errno[2] = 0;	errno[3] = 0;
    errno[4] = 0;

    sleep( SLEEP_CYCLES );		// not sure if I need this
    helper_plugin_cntr_zero( PKT_COUNT );
    helper_plugin_cntr_zero( CB_COUNT );
    helper_plugin_cntr_zero( ERR_COUNT );
}


// Same as dl_sink_packet() but don't do any signalling
//
// set ring_out data from item in queue data.
// 	return 0 if OK; -1 otherwise
//
static __forceinline int
helper_send_from_queue(	__declspec(shared sram) struct queue_tag *qptr,
			__declspec(gp_reg) int dlNextBlock ) {
    int			rc;

    if( dlNextBlock == QM ) {
	plugin_out_data	my_ring_out;	// ring data to next block
    	__declspec(gp_reg) int	out_port;

	out_port = (qptr->hd->metapkt.plugin_plugin_data_out.uc_mc_bits >> 3)
			& 0x7;
	my_ring_out.plugin_qm_data_out.out_port		= out_port;
	my_ring_out.plugin_qm_data_out.qid		=
		qptr->hd->metapkt.plugin_plugin_data_out.qid;
	my_ring_out.plugin_qm_data_out.l3_pkt_len	=
		qptr->hd->metapkt.plugin_plugin_data_out.l3_pkt_len;
	my_ring_out.plugin_qm_data_out.buf_handle_lo24	=
		qptr->hd->metapkt.plugin_plugin_data_out.buf_handle_lo24;
	scr_ring_put_buffer_3word( PLUGIN_TO_QM_RING, my_ring_out.i, 0 );
    } else if( dlNextBlock == PACKET_IN_RING_0 )	return -1;
    else if(	(dlNextBlock == PACKET_IN_RING_1)  ||
		(dlNextBlock == PACKET_IN_RING_2)  ||
		(dlNextBlock == PACKET_IN_RING_3)  ||
		(dlNextBlock == PACKET_IN_RING_4)  ) {
	if( dlNextBlock == PACKET_IN_RING_1 )
	    sram_ring_put_buffer_6word( PLC_TO_PLUGIN_1_SRAM_RING,
						qptr->hd->metapkt.i, 0 );
	else if( dlNextBlock == PACKET_IN_RING_2 )
	    sram_ring_put_buffer_6word( PLC_TO_PLUGIN_2_SRAM_RING,
						qptr->hd->metapkt.i, 0 );
	else if( dlNextBlock == PACKET_IN_RING_3 )
	    sram_ring_put_buffer_6word( PLC_TO_PLUGIN_3_SRAM_RING,
						qptr->hd->metapkt.i, 0 );
	else if( dlNextBlock == PACKET_IN_RING_4 )
	    sram_ring_put_buffer_6word( PLC_TO_PLUGIN_4_SRAM_RING,
						qptr->hd->metapkt.i, 0 );
    } else {					// all other options
    	return -1;
    }

    rc = queue_pop( qptr );
    if( rc == -1 ) {
    	helper_set_errno( BAD_POP_EMPTY_ERR );
	onl_api_plugin_cntr_inc(pluginId, ERR_COUNT);
    } else if( rc == -2 ) {
    	helper_set_errno( BAD_POP_FREE_ERR );
	onl_api_plugin_cntr_inc(pluginId, ERR_COUNT);
    }

    return 0;
}


//-----------------------------------------------------------
// Begin Normal Functions
//-----------------------------------------------------------
void handle_pkt_user()  {
    unsigned long	ninq;
    union tm_tag	tnow;		// current time
    union tm_tag	tdepart;	// depart time
    unsigned int	nticks;		// 16 cycles = 1 tick

    ++npkts;		// pkt counter
    onl_api_plugin_cntr_inc(pluginId, PKT_COUNT);

    tnow.tm2.lo = local_csr_read( local_csr_timestamp_low );	// must be first
    tnow.tm2.hi = local_csr_read( local_csr_timestamp_high );
    nticks = msec2cycles( delay ) >> 4;
    tdepart.tm = tnow.tm + nticks;

#ifdef DEBUG1
// expect 4,375,000 ticks in 50 msec
// i.e., 50 msec / (1/1.4 nsec/cycle * 16 cycles/tick)
helper_sram_dbgmsg_3ul( tnow.tm2.hi, tnow.tm2.lo, nticks);
helper_sram_dbgmsg_3ul( tdepart.tm2.hi, tdepart.tm2.lo, 4375000);
#endif

    ninq = queue_enq( &queue, ring_in, tdepart );
    if( ninq == -1 ) {
    	helper_set_errno( BAD_ENQ_ERR );
	onl_api_plugin_cntr_inc(pluginId, ERR_COUNT);
	++ndrops;			// number of drops
	helper_set_out_to_DROP( );
	return;
    } else {
	if ( ninq > maxinq )	maxinq = ninq;
	helper_set_out_to_DO_NOTHING( );
    }
}

//									<<<<<
void handle_msg_user(){}				// <<<<< NOT USED

//									<<<<<
void plugin_init_user()
{
    if(ctx() == 0)
    {
	reset_counters( );
	queue_lock = UNLOCKED;
	if( queue_init( &queue ) != 0 ) {
	    helper_set_errno( BAD_DELAYQ_INIT_ERR );
	}
    }

    // plugin chaining
    if( pluginId == 0 )		dlNextBlock = PACKET_IN_RING_1;
    else if( pluginId == 1 )	dlNextBlock = PACKET_IN_RING_2;
    else if( pluginId == 2 )	dlNextBlock = PACKET_IN_RING_3;
    else if( pluginId == 3 )	dlNextBlock = PACKET_IN_RING_4;
    else			dlNextBlock = QM;
}


//-----------------------------------------------------------
// Begin Normal Functions
//-----------------------------------------------------------
//									<<<<<
void handle_pkt() {
    dl_source_packet( dlFromBlock );
  
    handle_pkt_user( );

    dl_sink_packet( dlNextBlock );
}


/* handle control messages */
//									<<<<<
// op codes:
//   set:
//	delay=	set delay to x msec (e.g., "delay= 50")
//   get:
//	=counts	get (npkts, maxinq, ndrops)
//	=ninq	get ninq (#pkts in queue)
//	=delay	get delay (msec)
//   miscellaneous:
//	reset	reset npkts, maxinq, and ndrops counters
//
void handle_msg()
{
    // assume messages are at most 8 words for now
    __declspec(gp_reg) unsigned int message[8];
    __declspec(gp_reg) onl_api_ctrl_msg_hdr hdr;
    __declspec(local_mem) char inmsgstr[28];			// inbound
    __declspec(local_mem) char outmsgstr[28] = "";		// outbound
    __declspec(sram) char sram_inmsgstr[28];

    char SET_delay[8]	= "delay=";		// request msgs
    char GET_counts[8]	= "=counts";
    char GET_ninq[8]	= "=ninq";
    char GET_delay[8]	= "=delay";
    char RESET[8]	= "reset";
    char BAD_OP_msg[8]	= "BAD OP";		// error msgs
    char BAD_NUM_ARGS_msg[12]= "BAD #ARGS";

    // to get rid of the compiler error:  "Incorrect use of register variable
    // message - check for & operator or non-constant buffer index"
    message[0] = 0;    message[1] = 0;    message[2] = 0;    message[3] = 0;
    message[4] = 0;    message[5] = 0;    message[6] = 0;    message[7] = 0;

    dl_source_message(msgFromBlock, message);

    hdr.value = message[0];
    if( hdr.type != CM_CONTROLMSG )	return;
    if( hdr.response_requested != 1 )	return;

    onl_api_intarr2str( &message[1], inmsgstr );

    memcpy_sram_lmem( sram_inmsgstr, inmsgstr, 28 );

    if( strncmp_sram(sram_inmsgstr, SET_delay, 6) == 0 ) {
    	char	*cmnd_word;		// points to input command field
    	char	*delay_word;		// points to input delay(msec) field
	unsigned int	nwords;

	nwords = helper_count_words( sram_inmsgstr );
	if( nwords != 2 ) {
	    memcpy_lmem_sram( outmsgstr, BAD_NUM_ARGS_msg, 12 );
	} else {
	    cmnd_word = helper_tokenize( sram_inmsgstr );	// get command
	    delay_word = helper_tokenize( cmnd_word+strlen(cmnd_word)+1 );

	    delay = helper_atou_sram( delay_word );
	    helper_sram_outmsg_1ul( delay, outmsgstr );
	}
    } else if( strncmp_sram(sram_inmsgstr, GET_counts, 7) == 0 ) {
	helper_sram_outmsg_3ul( npkts, maxinq, ndrops, outmsgstr );
    } else if( strncmp_sram(sram_inmsgstr, GET_ninq, 5) == 0 ) {
	helper_sram_outmsg_1ul( queue.npkts, outmsgstr );
    } else if( strncmp_sram(sram_inmsgstr, GET_delay, 6) == 0 ) {
	helper_sram_outmsg_1ul( delay, outmsgstr );
    } else if( strncmp_sram(sram_inmsgstr, RESET, 5) == 0 ) {
    	reset_counters( );
    } else {
	memcpy_lmem_sram( outmsgstr, BAD_OP_msg, 8 );
    }

    if( onl_api_str2intarr(outmsgstr, &message[1]) < 0 )	return;

    hdr.type = CM_CONTROLMSGRSP;
    hdr.response_requested = 0;
    hdr.num_words = 7;
    message[0] = hdr.value;

    dl_sink_message(msgNextBlock, message);
}

// handle periodic functionality
//									<<<<<
// Called ABOUT every 10 usec
//    - We can only guarantee that this thread will not get control sooner
//   	than 10 usec.
//
void callback() {
    union tm_tag	tnow;
    int			rc;

    if ( queue.npkts > 0 ) {
	tnow.tm2.lo = local_csr_read( local_csr_timestamp_low );
	tnow.tm2.hi = local_csr_read( local_csr_timestamp_high );
	if ( tnow.tm >= queue.hd->tdepart.tm ) {	// time for pkt to leave
	    onl_api_plugin_cntr_inc(pluginId, CB_COUNT);
#ifdef DEBUG2
helper_sram_dbgmsg_3ul( tnow.tm2.hi, tnow.tm2.lo, dlNextBlock );
helper_sram_dbgmsg_3ul( queue.npkts, nsent, 0 );
#endif
	    rc = helper_send_from_queue( &queue, dlNextBlock );
	    if( rc == 0 ) {
		++nsent;
#ifdef DEBUG2
helper_sram_dbgmsg_3ul( queue.npkts, nsent, 0 );
#endif
	    } else	onl_api_plugin_cntr_inc(pluginId, ERR_COUNT);
	}
	// send atmost 1 pkt in each callback
    }
    sleep( SLEEP_CYCLES );
}


/* take care of any setup that needs to be done before processing begins */
//									<<<<<
void plugin_init()
{
  /* set the default next block to be the Queue Manager */
  dlNextBlock = QM;

  /* by default, get packets and get and put control messages from input rings
   * based on which microengine we are currently running on; this assumes a
   * default one to one mapping */
  switch(__ME())
  {
    case 0x7:
      pluginId = 0;
      dlFromBlock  = PACKET_IN_RING_0;
      msgFromBlock = MESSAGE_IN_RING_0;
      msgNextBlock = MESSAGE_OUT_RING_0;
      break;
    case 0x10:
      pluginId = 1;
      dlFromBlock  = PACKET_IN_RING_1;
      msgFromBlock = MESSAGE_IN_RING_1;
      msgNextBlock = MESSAGE_OUT_RING_1;

      break;
    case 0x11:
      pluginId = 2;
      dlFromBlock  = PACKET_IN_RING_2;
      msgFromBlock = MESSAGE_IN_RING_2;
      msgNextBlock = MESSAGE_OUT_RING_2;  
      break;
    case 0x12:
      pluginId = 3;
      dlFromBlock  = PACKET_IN_RING_3;
      msgFromBlock = MESSAGE_IN_RING_3;
      msgNextBlock = MESSAGE_OUT_RING_3;    
      break;
    case 0x13:
      pluginId = 4;
      dlFromBlock  = PACKET_IN_RING_4;
      msgFromBlock = MESSAGE_IN_RING_4;
      msgNextBlock = MESSAGE_OUT_RING_4;
      break;
    default:  // keep the compiler happy
      pluginId = 0;
      dlFromBlock  = PACKET_IN_RING_0;
      msgFromBlock = MESSAGE_IN_RING_0;
      msgNextBlock = MESSAGE_OUT_RING_0;
      break;
  }

  plugin_init_user(); // user hook
}


// entry point
//									<<<<<
void main()
{
  int c;

  /* do initialization */
  plugin_init();
  dl_sink_init();
  dl_source_init();

  /* get the current thread's context number (0-7) */
  c = ctx();

  if(c >= FIRST_PACKET_THREAD && c <= LAST_PACKET_THREAD)
  {
    while(1)
    {
      handle_pkt();
    }
  }
#ifdef MESSAGE_THREAD
  else if(c == MESSAGE_THREAD)
  {
    while(1)
    {
      handle_msg();
    }
  }
#endif
#ifdef CALLBACK_THREAD
  else if(c == CALLBACK_THREAD)
  {
    while(1)
    {
      callback();
    }
  }
#endif
}


// --------------------------------------------------------------------------
// queueing functions
//
//	queue_init		initialize free list and queue descriptor
//	queue_enq		enqueue an item onto a queue
//	queue_pop		pop an item from a queue
//	queue_alloc		allocate space for an item from the free list
//	queue_free		put an item back onto the free list
//
// --------------------------------------------------------------------------

// initialize queue
//									<<<<<
int
queue_init( __declspec(shared, sram) struct queue_tag *qptr ) {
    int			i;
    int			K = MAX_QUEUE_SZ-1;
    struct item_tag	*item_ptr;

    if ( pluginId == 0)		item_ptr = (struct item_tag *) 0xC0100000;
    else if ( pluginId == 1)	item_ptr = (struct item_tag *) 0xC0200000;
    else if ( pluginId == 2)	item_ptr = (struct item_tag *) 0xC0300000;
    else if ( pluginId == 3)	item_ptr = (struct item_tag *) 0xC0400000;
    else if ( pluginId == 4)	item_ptr = (struct item_tag *) 0xC0500000;
    else	return -1;

    qptr->free_hd = item_ptr;
    qptr->hd = qptr->tl = 0;

    qptr->npkts = 0;
    qptr->nbytes = 0;
    qptr->maxinq = 0;
    qptr->ndrops = 0;
    qptr->nerrs = 0;

    (item_ptr+K)->next = 0;

    for (i=0; i<K; i++) {
    	item_ptr->next = item_ptr+1;
	++item_ptr;
    }

    return 0;
}

// insert item at end of queue
// 	return number of items if OK; else -1
int
queue_enq(	__declspec(shared, sram) struct queue_tag *qptr,
		volatile __declspec(gp_reg) plc_plugin_data ring_in,
    		union tm_tag tdepart ) {
    struct item_tag	*item;

    while( queue_lock == LOCKED )	ctx_swap();
    queue_lock = LOCKED;

	item = queue_alloc( &queue );
	if( item == 0 ) {
	    ++qptr->ndrops;
	    return -1;
	}

//helper_sram_dbgmsg_3ul( 0, queue.npkts, nsent );

	item->metapkt.i[0] = ring_in.i[0];
	item->metapkt.i[1] = ring_in.i[1];
	item->metapkt.i[2] = ring_in.i[2];
	item->metapkt.i[3] = ring_in.i[3];
	item->metapkt.i[4] = ring_in.i[4];
	item->metapkt.i[5] = ring_in.i[5];
	item->tdepart = tdepart;

	if( qptr->npkts == 0 )	qptr->hd = item;
	else			qptr->tl->next = item;
	qptr->tl = item;

	++(qptr->npkts);
	if( qptr->npkts > qptr->maxinq )	qptr->maxinq = qptr->npkts;

    queue_lock = UNLOCKED;

    return qptr->npkts;
}


// pop front of list
int
queue_pop( __declspec(shared, sram) struct queue_tag *qptr ) {
    struct item_tag	*item;

    while( queue_lock == LOCKED )	ctx_swap();
    queue_lock = LOCKED;

	if( qptr->npkts <= 0 ) {
	    ++qptr->nerrs;
	    return -1;
	}

	item = qptr->hd;
	qptr->hd = item->next;
	--(qptr->npkts);
	if( qptr->npkts == 0 )	qptr->tl = 0;
	queue_free( qptr, item );

    queue_lock = UNLOCKED;
    return 0;
}

// allocate an item
struct item_tag *
queue_alloc( __declspec(shared, sram) struct queue_tag *qptr ) {
    struct item_tag *item;

    if( qptr->free_hd == 0 )	return 0;

    item = qptr->free_hd;
    qptr->free_hd = item->next;
    return item;
}

// free an item
void
queue_free(	__declspec(shared, sram) struct queue_tag *qptr,
		struct item_tag *item ) {
    if( item == 0 )	return;
    item->next = qptr->free_hd;
    qptr->free_hd = item;
}

