/*
    Copyright 2000 (c) by Oliver Pfister,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
	
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/IO/ia32/Drivers/IOPciNetPerformanceTest.c,v $
    Author(s):             
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Creation Date:         
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
*/
#include "Messages.h"
#include "Syscall.h"
#include "IOPciNetPerformanceTest.h"
#include "performanceTest.h"
#include "IOPci.h"
#include "IOPciNetDeviceStats.h"
#include "IOPciAm79c97x.h"
#include "IOPciAm79c970.h"
#include "Support.h"
#include "video.h"
#include "Video.h"

/* XXX this is a hack */
IODeviceDesc pciNetPerfTest = {
                                NULL, NULL,
                                "netPerformanceTest",
                                (Address)0, /* Screen IO: No Direct Memory Access allowed */
                                NULL,       /* buffer must be allocated by driver */
                                -1,
                                NULL,
                                0, /* thread id filled in after start */
                                NULL,
                                NULL,
                                NULL,
                                ioNetPerformanceTest_init,
                                NULL, /* private msg handler */
                                FALSE,
                                NULL /* data extension */
                              };

// Global strings
static char strReadWriteQuit[] = "Net Performance: r read, w write, f read/write, q to quit : ";
static char strReadWriteQuitRestart[] = "r read, w write, f read/write, q to quit, x to restart net card : ";
static char strReadMode[] = "in read mode";
static char strWriteMode[] = "in write mode";
static char strReadWriteMode[] = "in read/write mode";
static char strVirtualMemAllocFailed[] = "NET TEST : Virtual Memory Allocation Error\n";
static char strVirtualMemFreeFailed[] = "NET TEST : Virtual Memory Free Error\n";


static char str[100];

static PerformanceTestDesc performanceTestDesc;
static PerformanceTest performanceTest = &performanceTestDesc;

//extern IODeviceDesc ioDeviceTable[IO_DEVCOUNT];


Error ioNetPerformanceTest_init(IODevice devPtr)
{
  int deviceNumber;
  ThreadId testThreadId;

  deviceNumber = AM79C970;
  tmStart( &testThreadId, (ThreadMainFunction)ioNetPerformanceTestMain,
           (ThreadArg)deviceNumber,"NetPerfTest970");

  deviceNumber = AM79C97X;
  tmStart( &testThreadId, (ThreadMainFunction)ioNetPerformanceTestMain,
           (ThreadArg)deviceNumber,"NetPerfTest97x");

  return IO_INITOK;
}


Error ioNetPerformanceTest_read(IODevice devPtr, ThreadId threadId, char* bufPtr, long int* sizePtr)
{
  *sizePtr = 0;
  return IO_READOK;
}


Error ioNetPerformanceTest_write(IODevice devPtr, ThreadId threadId, char* bufPtr, long int* sizePtr)
{
  *sizePtr = 0;
  return IO_WRITEOK;
}


void ioNetPerformanceTestMain(ThreadArg devNum)
{
  /*	int i;
  	char c;
  	IOPciDeviceDesc* devPciPtr;
  	unsigned int bytes;
  	ThreadId thisId, parentId, devId, consoleId;
  	char* rxFrame;
  	char* txFrame;
  	unsigned long int rx_length, tx_length, size;
   
  	i=0;
  	devPciPtr = (IOPciDeviceDesc*)(ioDeviceTable[(int)devNum].extension);
   
  	tmGetInfo(SELF, &thisId, &parentId);
   
  	if (ioOpen(PCI, (int)devNum, &devId) != IO_OPENOK)
  		tmKill(thisId);
   
  	if (ioInit(devId) == IO_INITFAILED)
  		tmKill(thisId);
   
  	ioOpen(IO_CONSOLE, &consoleId);
  	ioInit(consoleId);
  	size=str_len(strReadWriteQuit);
    	printf("\n");
  	ioWrite(consoleId, strReadWriteQuit, &size);
  	c = 0;
  	while(c!='w' && c!='r' && c!='f')
  	{
  		size=1;
  		ioRead(consoleId, &c, &size); 
      	if (c=='q')
      	{
        		printf(" Goodbye.\n");
        		tmKill(thisId);
      	}
  	}
  	ClearScreen();
   
  	while(1)
  	{
  		if (c=='r')
  		{
  	    	rx_length = 1024;
  	    	if (vmAlloc((Address*)&rxFrame, rx_length) == VM_ALLOCFAILED)
  	  			ERROR(strVirtualMemAllocFailed);
  	    	while(1)
  	    	{
  	  			bytes=0;
  	  			initPerformanceTest(performanceTest);
  	  			startPerformanceTest(performanceTest);
  	  			for (i=0 ; i<5000 ; i++)	
  	  			{
  	  	  			rx_length = 1024;
  	  	  			ioRead(devId, rxFrame, &rx_length);
  	  	  			if (rx_length)
  	  					bytes = bytes + rx_length;
  	  			}
  	  			stopPerformanceTest(performanceTest);
  	  			snprintf(str, sizeof(str), "%iMBit/s  ", (bytes*8)/performanceTest->deltaMicroSeconds);
  	  			ClearScreen();
  	  			__printstr((1*80+66)*2,0x1E,(unsigned char*)strReadMode);
  	  			__printstr((24*80+10)*2,0x1E,(unsigned char*)str);
  	  			if ((int)devNum == IO_AM79C970)
  	  	  			IOPciNetDevicePrintStats(&((IOAm79c970*)devPciPtr->extension)->stats);
  	  			else
  	  	  			IOPciNetDevicePrintStats(&((IOAm79c97x*)devPciPtr->extension)->stats);
  	  			printf(strReadWriteQuitRestart);
  	  			size=1;
  	  			ioRead(consoleId, &c, &size); 
  	  			if (c=='w' || c=='r' || c=='f')
  	  	  			break;
  	  			if (c=='q')
  	  			{
  	  	  			__printstr((24*80+10)*2,0x1E,"           ");
  	  	  			printf(" Goodbye.");
  	  	  			tmKill(thisId);
  	  			}
  	  			if (c=='x')
  	  			{
  	  	  			if ((int)devNum == IO_AM79C970)
  	  					am79c970Restart(&(ioDeviceTable[(int)devNum])); 
  	  	  			else
  	  					am79c97xRestart(&(ioDeviceTable[(int)devNum])); 
  	  	  			c = 'r';
  	  			}
  	    	}
  	    	if (vmFree((Address)rxFrame) == VM_FREEFAILED)
  	  			ERROR(strVirtualMemFreeFailed);
  	  	}
  	  	else if (c=='w')
  	  	{
  	    	tx_length = 1024;
  	    	if (vmAlloc((Address*)&txFrame, tx_length) == VM_ALLOCFAILED)
  	  			ERROR(strVirtualMemAllocFailed);
  	    	for(i=0 ; i<6 ; i++)
  	  			txFrame[i] = 0xFF;
  	    	for(i=6 ; i<1024 ; i++)
  	  			txFrame[i] = i/4;
  	    	while(1)
  	    	{
  	  			bytes=0;
  	  			initPerformanceTest(performanceTest);
  	  			startPerformanceTest(performanceTest);
  	  			for (i=0 ; i<5000 ; i++)	
  	  			{
  	  	  			tx_length = 1024;
  	  	  			ioWrite(devId, txFrame, &tx_length);
  	  	  			if (tx_length)
  	  					bytes = bytes + tx_length;
  	  			}
  	  			stopPerformanceTest(performanceTest);
  	  			snprintf(str, sizeof(str), "%iMBit/s  ", (bytes*8)/performanceTest->deltaMicroSeconds);
  	  			ClearScreen();
  	  			__printstr((1*80+65)*2,0x1E,(unsigned char*)strWriteMode);
  	  			__printstr((24*80+10)*2,0x1E,(unsigned char*)str);
  	  			if ((int)devNum == IO_AM79C970)
  	  	  			IOPciNetDevicePrintStats(&((IOAm79c970*)devPciPtr->extension)->stats);
  	  			else
  	  	  			IOPciNetDevicePrintStats(&((IOAm79c97x*)devPciPtr->extension)->stats);
  	  			printf(strReadWriteQuitRestart);
  	  			size=1;
  	  			ioRead(consoleId, &c, &size); 
  	  			if (c=='w' || c=='r' || c=='f')
  	  	  			break;
  	  			if (c=='q')
  	  			{
  	  	  			__printstr((24*80+10)*2,0x1E,"           ");
  	  	  			printf(" Goodbye.");
  	  	  			tmKill(thisId);
  	  			}
  	  			if (c=='x')
  	  			{
  	  	  			if ((int)devNum == IO_AM79C970)
  	  					am79c970Restart(&(ioDeviceTable[(int)devNum])); 
  	  	  			else
  	  					am79c97xRestart(&(ioDeviceTable[(int)devNum])); 
  	  	  			c = 'w';
  	  			}
  	    	}
  	    	if (vmFree((Address)txFrame) == VM_FREEFAILED)
  	  			ERROR(strVirtualMemFreeFailed);
  	  	}
  	  	else if (c=='f')
  	  	{
  	    	rx_length = 1024;
  	    	if (vmAlloc((Address*)&rxFrame, rx_length) == VM_ALLOCFAILED)
  	  			ERROR(strVirtualMemAllocFailed);
  	    	tx_length = 1024;
  	    	if (vmAlloc((Address*)&txFrame, tx_length) == VM_ALLOCFAILED)
  	  			ERROR(strVirtualMemAllocFailed);
  	    	for(i=0 ; i<6 ; i++)
  	  			txFrame[i] = 0xFF;
  	    	for(i=6 ; i<1024 ; i++)
  	  			txFrame[i] = i/4;
  	    	while(1)
  	    	{
  	  			bytes=0;
  	  			initPerformanceTest(performanceTest);
  	  			startPerformanceTest(performanceTest);
  	  			for (i=0 ; i<5000 ; i++)	
  	  			{
  	  	  			rx_length = 1024;
  	  	  			ioRead(devId, rxFrame, &rx_length);
  	  	  			if (rx_length)
  	  				bytes = bytes + rx_length;
  	  	  			tx_length = 1024;
  	  	  			ioWrite(devId, txFrame, &tx_length);
  	  	  			if (tx_length)
  	  				bytes = bytes + tx_length;
  	  				}
  	  			stopPerformanceTest(performanceTest);
  	  			snprintf(str, sizeof(str), "%iMBit/s  ", (bytes*8)/performanceTest->deltaMicroSeconds);
  	  			ClearScreen();
  	  			__printstr((1*80+60)*2,0x1E,(unsigned char*)strReadWriteMode);
  	  			__printstr((24*80+10)*2,0x1E,(unsigned char*)str);
  	  			if ((int)devNum == IO_AM79C970)
  	  	  			IOPciNetDevicePrintStats(&((IOAm79c970*)devPciPtr->extension)->stats);
  	  			else
  	  	  			IOPciNetDevicePrintStats(&((IOAm79c97x*)devPciPtr->extension)->stats);
  	  			printf(strReadWriteQuitRestart);
  	  			size=1;
  	  			ioRead(consoleId, &c, &size); 
  	  			if (c=='w' || c=='r' || c=='f')
  	  	  			break;
  	  			if (c=='q')
  	  			{
  	  	  			__printstr((24*80+10)*2,0x1E,"           ");
  	  	  			printf(" Goodbye.");
  	  	  			tmKill(thisId);
  	  			}
  	  			if (c=='x')
  	  			{
  	  	  			if ((int)devNum == IO_AM79C970)
  	  					am79c970Restart(&(ioDeviceTable[(int)devNum])); 
  	  	  			else
  	  					am79c97xRestart(&(ioDeviceTable[(int)devNum])); 
  	  	  			c = 'f';
  	  			}
  	    	}
  	    	if (vmFree((Address)rxFrame) == VM_FREEFAILED)
  	  			ERROR(strVirtualMemFreeFailed);
  	    	if (vmFree((Address)txFrame) == VM_FREEFAILED)
  	  			ERROR(strVirtualMemFreeFailed);
  	  	}
  	} */
}
