12#include "rpc_server.h"
14static int NETMAN_RpcSvr_threadID=-1, NETMAN_Rx_threadID=-1, SifHandlerID=-1;
15static unsigned char NETMAN_RpcSvr_ThreadStack[0x1000] ALIGNED(16);
16static unsigned char NETMAN_Rx_ThreadStack[0x1000] ALIGNED(16);
17static unsigned char IsInitialized=0, IsProcessingRx;
19static struct NetManBD *FrameBufferStatus = NULL;
20static struct NetManBD *RxIOPFrameBufferStatus;
21static unsigned short int RxBufferRdPtr, RxBufferNextRdPtr;
24static void NETMAN_RxThread(
void *arg);
26static void ClearBufferLen(
int index,
void *packet,
void *payload)
31 bd = UNCACHED_SEG(&FrameBufferStatus[index]);
34 bd->payload = payload;
37 dmat.src = (
void*)&FrameBufferStatus[index];
38 dmat.dest = &RxIOPFrameBufferStatus[index];
41 while(SifSetDma(&dmat, 1) == 0){ };
44static s32 HandleRxEvent(s32 channel)
50 bd = UNCACHED_SEG(&FrameBufferStatus[RxBufferNextRdPtr]);
58 iWakeupThread(NETMAN_Rx_threadID);
67int NetManRPCAllocRxBuffers(
void)
71 for(i = 0; i < NETMAN_RPC_BLOCK_SIZE; i++)
73 void *packet, *payload;
75 if((packet = NetManNetProtStackAllocRxPacket(NETMAN_NETIF_FRAME_SIZE, &payload)) != NULL)
77 ClearBufferLen(i, packet, payload);
79 printf(
"NETMAN: error - unable to allocate Rx FIFO buffers.\n");
88static void *NETMAN_EE_RPC_Handler(
int fnum,
void *buffer,
int NumBytes)
97 case NETMAN_EE_RPC_FUNC_INIT:
98 RxIOPFrameBufferStatus = *(
void**)buffer;
101 if(FrameBufferStatus == NULL) FrameBufferStatus = memalign(64,
sizeof(
struct NetManBD) * NETMAN_RPC_BLOCK_SIZE);
103 if(FrameBufferStatus != NULL)
105 memset(UNCACHED_SEG(FrameBufferStatus), 0,
sizeof(
struct NetManBD) * NETMAN_RPC_BLOCK_SIZE);
107 RxBufferNextRdPtr = 0;
110 thread.func=&NETMAN_RxThread;
111 thread.stack=NETMAN_Rx_ThreadStack;
112 thread.stack_size=
sizeof(NETMAN_Rx_ThreadStack);
114 thread.initial_priority=0x59;
117 if((NETMAN_Rx_threadID=CreateThread(&
thread)) >= 0)
119 StartThread(NETMAN_Rx_threadID, NULL);
121 SifHandlerID = AddDmacHandler(DMAC_SIF0, &HandleRxEvent, 0);
122 EnableDmac(DMAC_SIF0);
134 case NETMAN_EE_RPC_FUNC_DEINIT:
135 if(FrameBufferStatus != NULL)
137 free(FrameBufferStatus);
138 FrameBufferStatus = NULL;
142 case NETMAN_EE_RPC_FUNC_HANDLE_LINK_STATUS_CHANGE:
143 NetManToggleGlobalNetIFLinkState(*(u32*)buffer);
147 printf(
"NETMAN [EE]: Unrecognized command: 0x%x\n", fnum);
158static void NETMAN_RPC_Thread(
void *arg)
160 static unsigned char cb_rpc_buffer[64]
__attribute__((__aligned__(64)));
164 SifSetRpcQueue(&cb_queue, NETMAN_RpcSvr_threadID);
165 SifRegisterRpc(&cb_srv, NETMAN_RPC_NUMBER, &NETMAN_EE_RPC_Handler, cb_rpc_buffer, NULL, NULL, &cb_queue);
166 SifRpcLoop(&cb_queue);
169static void NETMAN_RxThread(
void *arg)
172 u32 PacketLength, PacketLengthAligned;
179 void *payload, *payloadNext;
180 void *packet, *packetNext;
182 bd = UNCACHED_SEG(&FrameBufferStatus[RxBufferRdPtr]);
186 PacketLength = bd->length;
187 if (PacketLength > 0)
200 payload = bd->payload;
204 while((packetNext = NetManNetProtStackAllocRxPacket(NETMAN_NETIF_FRAME_SIZE, &payloadNext)) == NULL){};
205 RxBufferNextRdPtr = (RxBufferNextRdPtr + 1) % NETMAN_RPC_BLOCK_SIZE;
206 ClearBufferLen(RxBufferRdPtr, packetNext, payloadNext);
209 RxBufferRdPtr = RxBufferNextRdPtr;
212 PacketLengthAligned = (PacketLength + 63) & ~63;
213 SifWriteBackDCache(payload, PacketLengthAligned);
214 NetManNetProtStackReallocRxPacket(packet, PacketLength);
215 NetManNetProtStackEnQRxPacket(packet);
219int NetManInitRPCServer(
void)
226 ThreadData.func=&NETMAN_RPC_Thread;
227 ThreadData.stack=NETMAN_RpcSvr_ThreadStack;
228 ThreadData.stack_size=
sizeof(NETMAN_RpcSvr_ThreadStack);
229 ThreadData.gp_reg=&_gp;
230 ThreadData.initial_priority=0x57;
231 ThreadData.attr=ThreadData.option=0;
233 if((NETMAN_RpcSvr_threadID=CreateThread(&ThreadData))>=0)
235 StartThread(NETMAN_RpcSvr_threadID, NULL);
239 else result=NETMAN_RpcSvr_threadID;
246void NetManDeinitRPCServer(
void)
250 SifRemoveRpc(&cb_srv, &cb_queue);
251 SifRemoveRpcQueue(&cb_queue);
252 RemoveDmacHandler(DMAC_SIF0, SifHandlerID);
254 if(NETMAN_RpcSvr_threadID>=0)
256 TerminateThread(NETMAN_RpcSvr_threadID);
257 DeleteThread(NETMAN_RpcSvr_threadID);
258 NETMAN_RpcSvr_threadID = -1;
261 if(NETMAN_Rx_threadID>=0)
263 TerminateThread(NETMAN_Rx_threadID);
264 DeleteThread(NETMAN_Rx_threadID);
265 NETMAN_Rx_threadID = -1;