📄 net_mac.c
字号:
/************************************************************************
*
* net_mac.c
*
* The 'NET_MAC' module implements the MAC-layer services to provide
* service access points, which are linked with the 'type'-field
* of the MAC-header.
*
*
* ######################################################################
*
* Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved.
*
* Unpublished rights reserved under the Copyright Laws of the United States of
* America.
*
* This document contains information that is proprietary to MIPS Technologies,
* Inc. ("MIPS Technologies"). Any copying, modifying or use of this information
* (in whole or in part) which is not expressly permitted in writing by MIPS
* Technologies or a contractually-authorized third party is strictly
* prohibited. At a minimum, this information is protected under unfair
* competition laws and the expression of the information contained herein is
* protected under federal copyright laws. Violations thereof may result in
* criminal penalties and fines.
* MIPS Technologies or any contractually-authorized third party reserves the
* right to change the information contained in this document to improve
* function, design or otherwise. MIPS Technologies does not assume any
* liability arising out of the application or use of this information. Any
* license under patent rights or any other intellectual property rights owned
* by MIPS Technologies or third parties shall be conveyed by MIPS Technologies
* or any contractually-authorized third party in a separate license agreement
* between the parties.
* The information contained in this document constitutes one or more of the
* following: commercial computer software, commercial computer software
* documentation or other commercial items. If the user of this information, or
* any related documentation of any kind, including related technical data or
* manuals, is an agency, department, or other entity of the United States
* government ("Government"), the use, duplication, reproduction, release,
* modification, disclosure, or transfer of this information, or any related
* documentation of any kind, is restricted in accordance with Federal
* Acquisition Regulation 12.212 for civilian agencies and Defense Federal
* Acquisition Regulation Supplement 227.7202 for military agencies. The use of
* this information by the Government is further restricted in accordance with
* the terms of the license agreement(s) and/or applicable contract terms and
* conditions covering this information from MIPS Technologies or any
* contractually-authorized third party.
*
************************************************************************/
/************************************************************************
* Include files
************************************************************************/
#include <string.h>
#include <stdio.h>
#include <sysdefs.h>
#include <syserror.h>
#include <sysdev.h>
#include <syscon_api.h>
/* net stuff */
#include <net_api.h>
#include "net.h"
#include "net_mac.h"
/* generic device driver interface */
#include <io_api.h>
/* drivers */
#include <lan_api.h>
/************************************************************************
* Definitions
************************************************************************/
#define MAC_BUFFER_SIZE 0x600
#define MAC_CACHE_LINE_SIZE 0x20
/************************************************************************
* Public variables
************************************************************************/
/* MAC undefined address */
t_mac_addr mac_undefined_adr = { 0, 0, 0, 0, 0, 0 } ;
/* MAC broadcast address */
t_mac_addr mac_broadcast_adr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } ;
/* Our stations MAC-address */
t_mac_addr mac_station_adr ;
/************************************************************************
* Static variables
************************************************************************/
/* Global state of MAC */
static UINT32 NET_MAC_state = MAC_STATE_CLOSED ;
/* SAP table */
static t_mac_sap_context sap_context[MAC_SAP_COUNT] ;
/* Major device number of EN0 */
static UINT32 en0_major_device = 0 ;
/* Minor device number of EN0 */
static UINT32 en0_minor_device = 0 ;
/* Ref. for loopback buffer */
static void *lbbuf ;
/************************************************************************
* Static function prototypes
************************************************************************/
/************************************************************************
*
* NET_MAC_receive
* Description :
* -------------
* In "RX-frame receive int. mode", this function is called from
* the LAN-driver ISR, whenever it receives an Ethernet frame.
*
* In "RX-frame receive polled mode", this function will still
* be called from the LAN-driver, but in this case, the call
* gets started when NET_MAC_poll() calls the LAN-driver 'read'
* entry.
*
* It must check for any SAP's registered to handle the addressed
* SAP and if one found, the registered receive handler is called.
*
*
*
* Parameters :
* ------------
* 'length': length of received ethernet frame
* 'data': pointer for received ethernet frame (in driver's space)
*
*
* Return values :
* ---------------
* 'OK'
*
************************************************************************/
static
UINT32 NET_MAC_receive( UINT32 length,
UINT8 *data ) ;
/************************************************************************
*
* NET_MAC_alloc_sap
* Description :
* -------------
* Allocate a MAC SAP, register user context and return a sp-handle.
*
*
* Parameters :
* ------------
*
* 'mac_sap_id', IN, value of MAC-'type' to bind for
* 'mac_usr_receive', IN, user-receive function to be registered
* 'mac_sp_hd', OUT, handle of MAC to be used by user by call
* of 'send'
*
*
* Return values :
* ---------------
* 'ERROR_NET_MAC_NO_FREE_SAP', No SAP entry could be allocated
* 'OK' = 0x00:
*
*
************************************************************************/
static
UINT32 NET_MAC_alloc_sap(
UINT16 mac_sap_id, /* 'type'-field of mac-header
always in 'cpu' format */
t_mac_usr_receive mac_usr_receive, /* user defined receive handler */
UINT32 *mac_sp_hd ) ; /* service provider defined handle */
/************************************************************************
* Implementation : Public functions
************************************************************************/
/************************************************************************
*
* NET_MAC_init
* Description :
* -------------
* Initialize the MAC module.
*
*
*
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0), successfull initialization
*
************************************************************************/
UINT32 NET_MAC_init( void )
{
t_sys_malloc mem ;
UINT32 rcode ;
int i ;
/* initialize MAC SAP table */
for ( i = 0; i <MAC_SAP_COUNT ; i++)
{
sap_context[i].mac_sap_state = MAC_SAP_STATE_CLOSED ;
sap_context[i].mac_sap = MAC_SAP_UNDEFINED ;
sap_context[i].mac_usr_receive = NULL ;
}
/* get a local copy of this stations MAC address */
IF_ERROR( (rcode),
(SYSCON_read( SYSCON_COM_EN0_MAC_ADDR_ID,
&(mac_station_adr),
sizeof(mac_station_adr)) ) )
/* get a EN0 major device number */
IF_ERROR( (rcode),
(SYSCON_read( SYSCON_COM_EN0_MAJOR_DEVICE_ID,
&(en0_major_device),
sizeof(en0_major_device)) ) )
IF_ERROR( (rcode),
(SYSCON_read( SYSCON_COM_EN0_MINOR_DEVICE_ID,
&(en0_minor_device),
sizeof(en0_minor_device)) ) )
/* allocate data for loopback buffer */
mem.size = MAC_BUFFER_SIZE ;
mem.boundary = MAC_CACHE_LINE_SIZE ;
mem.memory = &(lbbuf) ;
rcode = SYSCON_read( SYSCON_BOARD_MALLOC_ID,
&mem,
sizeof(t_sys_malloc) ) ;
if (rcode != OK)
{
return( rcode ) ;
}
lbbuf = (void*) KSEG0( (UINT32)lbbuf ) ;
/* MAC-module has now been initialized */
NET_MAC_state = MAC_STATE_INITED ;
return(OK) ;
}
/************************************************************************
*
* NET_MAC_open
* Description :
* -------------
* Allocate a MAC-SAP and register user context.
*
*
* Parameters :
* ------------
* 'mac_sap_id', IN, value of MAC-'type' to bind for
* 'mac_usr_receive', IN, user-receive function to be registered
* 'mac_sp_hd', OUT, handle of MAC to be used by user by call
* of 'send'
*
* Return values :
* ---------------
* 'ERROR_NET_MAC_FATAL_STATE' A fatal state has been detected in MAC.
* 'ERROR_NET_MAC_NOT_INITIALIZED' MAC-'init' has not been called.
* 'OK'(=0),
*
*
************************************************************************/
UINT32 NET_MAC_open( UINT16 mac_sap_id, /* 'type'-field of mac-header
always in 'cpu' format */
t_mac_usr_receive mac_usr_receive, /* user defined receive handler */
UINT32 *mac_sp_hd ) /* service provider defined handle */
{
t_LAN_OPEN_desc desc ;
UINT32 rcode = OK ;
switch (NET_MAC_state)
{
case MAC_STATE_CLOSED:
rcode = ERROR_NET_MAC_NOT_INITIALIZED ;
break;
case MAC_STATE_INITED:
/* try allocating a SAP */
rcode = NET_MAC_alloc_sap( mac_sap_id,
mac_usr_receive,
mac_sp_hd ) ;
if ( rcode == OK )
{
/* By first 'open', we need to open driver */
desc.receive = NET_MAC_receive ;
rcode = IO_open( en0_major_device, en0_minor_device, &desc ) ;
if ( rcode == OK )
{
NET_MAC_state = MAC_STATE_OPEN ;
}
}
break;
case MAC_STATE_OPEN:
/* try allocating a SAP */
rcode = NET_MAC_alloc_sap( mac_sap_id,
mac_usr_receive,
mac_sp_hd ) ;
break;
default:
/* we should never arrive here */
rcode = ERROR_NET_MAC_FATAL_STATE ;
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -