/* sysNet.c - system-dependent Network Library */
/* Copyright (c) 2007 Wind River Systems, Inc. */
/*
modification history
--------------------
01c,17jan08,dtr Fix compiler warnign.
01b,24aug07,dtr Remove byteswap by default.
01a,07jul07,dtr adopted from cds8548/sysNet.c/01g
*/
/*
DESCRIPTION
This module provides BSP functionality to support the
bootrom 'M' command to modify MAC addresses of on-board
network interfaces.
MAC adddress routines provided by the BSP in this file are:
sysNetMacNVRamAddrGet()
sysNetMacAddrGet()
sysNetMacAddrSet()
This board provides storage in flash for the MAC addresses
of the motfcc and motscc interfaces. This library also
implements a RAM buffer to represent the contents of the
flash. The RAM buffer contains eight entries, which is
more than currently needed by this board, but can be
considered as room for expansion in future boards using
a derivative of this BSP. This RAM buffer is contained
in the array glbEnetAddr[][].
*/
#ifdef ETHERNET_MAC_HANDLER
#include <vxWorks.h>
#include "config.h"
/* locals */
/* defines */
#ifndef MAX_MAC_DEVS
#define MAX_MAC_DEVS 1
#endif
#define MAC_OFFSET_MOTTSEC 0
const char *sysNetDevName[MAX_MAC_DEVS] = {"mottsec"};
/* globals */
IMPORT int dynamicMacAddrGen
(
UINT8 * ifName, /* interface name */
int ifUnit, /* interface unit */
UINT8 * ifMacAddr, /* address buffer */
int ifMacAddrLen /* length of buffer */
);
/* locals */
LOCAL UINT8 glbEnetAddr[MAX_MAC_ADRS][MAC_ADRS_LEN] = {
{ CUST_ENET5, CUST_ENET4, CUST_ENET3_0, WR_ENET2, WR_ENET1, WR_ENET0 },
{ CUST_ENET5, CUST_ENET4, CUST_ENET3_1, WR_ENET2, WR_ENET1, WR_ENET0 },
{ CUST_ENET5, CUST_ENET4, CUST_ENET3_2, WR_ENET2, WR_ENET1, WR_ENET0 }
};
LOCAL UINT8 sysInvalidAddr[2][MAC_ADRS_LEN] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
};
/*
* To comply with 5.5.x API in bootConfig.c, sysEnetAddrSet()
* doesn't take a unit number. So save the MAC change to the
* unit last read from via sysEnetAddrGet()
*/
LOCAL int lastUnit;
/***********************************************************
*
* sysMacOffsetGet - Calculate table offset
*
* This routine calculates which table entry corresponds to
* the specified interface.
*
* Two values are calculated and returned in the parameters
* pointed to by ppEnet and pOffset.
*
* RETURNS: ERROR if the interface is not known; OK otherwise
*
* ERRNO: N/A
*/
STATUS sysMacOffsetGet
(
char * ifName, /* interface name */
int ifUnit, /* interface unit */
char ** ppEnet, /* pointer to glbEnetAddr[][] entry */
int * pOffset /* offset in NVRAM */
)
{
/*
* The address offsets into NVRAM and glbEnetAddr[] are:
*
* mottsec0 - offset 0
*
*/
if ( (strcmp(ifName,"mottsec") == 0) || (strcmp(ifName,"motetsec") == 0) )
{
if ( ifUnit > 3 )
return(ERROR);
*pOffset = ifUnit * (MAC_ADRS_LEN + 2); /* Round up to 32 bit word */
*ppEnet = (char*)glbEnetAddr[ifUnit + MAC_OFFSET_MOTTSEC];
}
else
return(ERROR);
return(OK);
}
/***********************************************************
*
* sysNetMacNVRamAddrGet - Get interface MAC address
*
* This routine gets the current MAC address from the
* Non Volatile RAM, and store it in the ifMacAddr
* buffer provided by the caller.
*
* It is not required for the BSP to provide NVRAM to store
* the MAC address. Also, some interfaces do not allow
* the MAC address to be set by software. In either of
* these cases, this routine simply returns ERROR.
*
* Given a MAC address m0:m1:m2:m3:m4:m5, the byte order
* of ifMacAddr is:
* m5 @ ifMacAddr
* m4 @ ifMacAddr + 1
* m3 @ ifMacAddr + 2
* m2 @ ifMacAddr + 3
* m1 @ ifMacAddr + 4
* m0 @ ifMacAddr + 5
*
* RETURNS: OK, if MAC address available, ERROR otherwise
*
* ERRNO: N/A
*/
STATUS sysNetMacNVRamAddrGet
(
char * ifName,
int ifUnit,
UINT8 * ifMacAddr,
int ifMacAddrLen
)
{
int offset;
char *pEnet;
char tmp[MAC_ADRS_LEN];
/* fetch address line & offset from glbEnetAddr[] table */
if (sysMacOffsetGet(ifName, ifUnit, &pEnet, &offset) != OK)
return(ERROR);
#if (NV_RAM_SIZE != NONE)
/* get MAC address from NvRAM. */
sysNvRamGet (pEnet, ifMacAddrLen, NV_MAC_ADRS_OFFSET+offset);
#endif /* (NV_RAM_SIZE != NONE) */
if ( memcmp(pEnet, sysInvalidAddr[0], MAC_ADRS_LEN) == 0 )
return(ERROR);
if ( memcmp(pEnet, sysInvalidAddr[1], MAC_ADRS_LEN) == 0 )
return(ERROR);
if((pEnet[5]!=WR_ENET0) || (pEnet[4]!=WR_ENET1) || (pEnet[3]!=WR_ENET2))
{
int i,j;
for (i = MAC_ADRS_LEN - 1,j=0; i >= 0; i--,j++)
tmp[j] = pEnet[i];
/* mac address in memory only */
memcpy (ifMacAddr, tmp, ifMacAddrLen);
}
else
memcpy (ifMacAddr, pEnet, ifMacAddrLen);
return (OK);
}
/***********************************************************
*
* sysNetMacAddrGet - Get interface MAC address
*
* This routine gets the current MAC address from the
* network interface, and stores it in the ifMacAddr
* buffer provided by the caller.
*
* If the network interface cannot be queried about the
* MAC address, this routine returns ERROR.
*
* RETURNS: OK, if MAC address available, ERROR otherwise
*
* ERRNO: N/A
*/
STATUS sysNetMacAddrGet
(
char * ifName,
int ifUnit,
UINT8 * ifMacAddr,
int ifMacAddrLen
)
{
/*
* None of our interfaces can be queried directly.
* Return ERROR to indicate that we need to use
* RAM/NVRAM instead.
*/
return(ERROR);
}
/***********************************************************
*
* sysNetMacAddrSet - Save interface MAC address
*
* This routine saves the MAC address specified in
* ifMacAddr to the appropriate location in NVRam (if
* possible) and update the specified interface to use
* the specified MAC address.
*
* If the network interface MAC address cannot be set,
* this routine returns ERROR.
*
* RETURNS: OK, if MAC address available, ERROR otherwise
*
* ERRNO: N/A
*/
STATUS sysNetMacAddrSet
(
char * ifName,
int ifUnit,
UINT8 * ifMacAddr,
int ifMacAddrLen
)
{
int offset;
char *pEnet;
/* fetch address line & offset from glbEnetAddr[] table */
if (sysMacOffsetGet(ifName, ifUnit, &pEnet, &offset) != OK)
return(ERROR);
#if (NV_RAM_SIZE != NONE)
/* check MAC address in NvRAM. */
sysNvRamGet (pEnet, ifMacAddrLen, NV_MAC_ADRS_OFFSET+offset);
if (0 == memcmp (ifMacAddr, pEnet, ifMacAddrLen))
{
/* same address so don't erase and rewrite flash */
printf("Address unchanged\n");
return (OK);
}
if (sysNvRamSet ((char*)ifMacAddr, ifMacAddrLen, NV_MAC_ADRS_OFFSET+offset) != OK)
return (ERROR);
#endif /* (NV_RAM_SIZE != NONE) */
return (OK);
}
/***********************************************************
*
* sysNetMacAddrSetDefault - Save interface MAC address
*
* This routine saves the MAC address specified in
* ifMacAddr to the appropriate location in NVRam (if
* possible) and update the specified interface to use
* the specified MAC address.
*
* If the network interface MAC address cannot be set,
* this routine returns ERROR.
*
* RETURNS: OK, if MAC address available, ERROR otherwise
*
* ERRNO: N/A
*/
STATUS sysNetMacAddrSetDefault
(
char * ifName,
int ifUnit
)
{
int offset;
char *pEnet;
/* fetch address line & offset from glbEnetAddr[] table */
if (sysMacOffsetGet(ifName, ifUnit, &pEnet, &offset) != OK)
return(ERROR);
#if (NV_RAM_SIZE != NONE)
if ( sysNvRamSet ((char*)pEnet, MAC_ADRS_LEN, NV_MAC_ADRS_OFFSET+offset) != OK)
return (ERROR);
#endif /* (NV_RAM_SIZE != NONE) */
return (OK);
}
/***********************************************************
*
* sysMacIndex2Unit - convert index range to unit number
*
* This routine converts an index range 0..MAX_MAC_ADRS-1
* to a unit number.
*
* RETURNS