From 747d8a278cef5b7433403855133ee561c002bfa1 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sun, 2 Oct 2011 13:56:54 -0700 Subject: [PATCH] changes coding style, redesign of the startup code --- drivers/net/mmnif.c | 500 ++++++++++++++------------------------------ drivers/net/mmnif.h | 14 +- 2 files changed, 170 insertions(+), 344 deletions(-) diff --git a/drivers/net/mmnif.c b/drivers/net/mmnif.c index 80acc0e3..42450e1d 100644 --- a/drivers/net/mmnif.c +++ b/drivers/net/mmnif.c @@ -361,7 +361,7 @@ inline static void *memcpy_put(void *dest, const void *src, size_t count) * this creates a polling thread which looks for data * itself */ -inline int mmnif_device_schedule() +inline static int mmnif_device_schedule(void) { return create_kernel_task(&polling_thread, mmnif_poll, NULL, NORMAL_PRIO); } @@ -369,7 +369,7 @@ inline int mmnif_device_schedule() /* trigger an interrupt on the remote processor * so he knows there is a packet to read */ -inline int mmnif_trigger_irq(dest_ip) +inline static int mmnif_trigger_irq(dest_ip) { int tmp, x, y, z, addr; int ue = dest_ip - 1; @@ -392,24 +392,22 @@ inline int mmnif_trigger_irq(dest_ip) /* mmnif_get_device_stats(): Returns a copy of the * current device */ -mmnif_device_stats_t mmnif_get_device_stats() +static mmnif_device_stats_t mmnif_get_device_stats(void) { - mmnif_device_stats_t stats = { - 0 - }; - if (!mmnif_dev) - DEBUGPRINTF - ("mmnif_get_device_stats(): the device is not initialized yet.\n"); + mmnif_device_stats_t stats = { 0 }; + if (!mmnif_dev) + DEBUGPRINTF("mmnif_get_device_stats(): the device is not initialized yet.\n"); else stats = ((mmnif_t *) mmnif_dev->state)->stats; + return stats; } /* mmnif_print_stats(): Print the devices stats of the * current device */ -void mmnif_print_stats() +static void mmnif_print_stats(void) { mmnif_t *mmnif; if (!mmnif_dev) @@ -424,47 +422,41 @@ void mmnif_print_stats() DEBUGPRINTF("Received: %d bytes\n", mmnif->stats.rx_bytes); DEBUGPRINTF("interrupts: %d\n", mmnif->stats.rx_intr); DEBUGPRINTF("polling: %d\n", mmnif->stats.rx_poll); - DEBUGPRINTF("Received: %d packets containuing errors\n", - mmnif->stats.rx_err); + DEBUGPRINTF("Received: %d packets containuing errors\n", mmnif->stats.rx_err); DEBUGPRINTF("Transmitted: %d packests successfull\n", mmnif->stats.tx); DEBUGPRINTF("Transmitted: %d bytes\n", mmnif->stats.tx_bytes); - DEBUGPRINTF("Transmitted: %d packests were dropped due to errors\n", - mmnif->stats.tx_err); + DEBUGPRINTF("Transmitted: %d packests were dropped due to errors\n", mmnif->stats.tx_err); } /* mmnif_print_driver_status * */ -void mmnif_print_driver_status() +void mmnif_print_driver_status(void) { mmnif_t *mmnif; int i; if (!mmnif_dev) { - DEBUGPRINTF - ("mmnif_print_driver_status(): the device is not initialized yet.\n"); + DEBUGPRINTF("mmnif_print_driver_status(): the device is not initialized yet.\n"); return; } mmnif = (mmnif_t *) mmnif_dev->state; DEBUGPRINTF("/dev/mmnif driver status: \n\n"); - DEBUGPRINTF("rx_buf: 0x%.8X\n", mmnif->rx_buff); + DEBUGPRINTF("rx_buf: 0xp\n", mmnif->rx_buff); DEBUGPRINTF("free descriptors : %d\n\n", mmnif->rx_buff->dcount); DEBUGPRINTF("descriptor table: (only print descriptors in use)\n"); DEBUGPRINTF("status\taddr\tsize\n"); for (i = 0; i < MMNIF_MAX_DESCRIPTORS; i++) { if (mmnif->rx_buff->desc_table[i].stat != 0) - DEBUGPRINTF("0x%.2X\t0x%.8X\t%X\t\n", + DEBUGPRINTF("0x%.2X\t0x%p\t%X\t\n", mmnif->rx_buff->desc_table[i].stat, mmnif->rx_buff->desc_table[i].addr, mmnif->rx_buff->desc_table[i].len); } - DEBUGPRINTF("\n\nretrive via interrupt : %d\n", - mmnif->rx_buff->iv_intr); - DEBUGPRINTF("ring heap start addr: 0x%.8X\n", - mmnif->rx_buff + sizeof(mm_rx_buffer_t)); - DEBUGPRINTF("head: 0x%X\ttail: 0x%X\n", mmnif->rx_buff->head, - mmnif->rx_buff->tail); + DEBUGPRINTF("\n\nretrive via interrupt : %d\n", mmnif->rx_buff->iv_intr); + DEBUGPRINTF("ring heap start addr: 0x%p\n", mmnif->rx_buff + sizeof(mm_rx_buffer_t)); + DEBUGPRINTF("head: 0x%X\ttail: 0x%X\n", mmnif->rx_buff->head, mmnif->rx_buff->tail); mmnif_print_stats(); } @@ -476,7 +468,7 @@ void mmnif_print_driver_status() * this function deals with some HW details, it checks to wich core this packet * should be routed and returns the destination */ -uint8_t mmnif_get_destination(struct netif *netif, struct pbuf *p) +static uint8_t mmnif_get_destination(struct netif *netif, struct pbuf *p) { struct ip_hdr *iphdr; uint8_t core; @@ -516,8 +508,9 @@ uint8_t mmnif_get_destination(struct netif *netif, struct pbuf *p) * right inside of the buffer which is used for communication * with the remote end */ -uint32_t mmnif_rxbuff_alloc(uint8_t dest, uint16_t len) +static uint32_t mmnif_rxbuff_alloc(uint8_t dest, uint16_t len) { + uint32_t ret; mm_rx_buffer_t *rb = (mm_rx_buffer_t *) ((char *)header_start_address + (dest - 1) * header_size); #if MMNIF_USE_MPB @@ -526,15 +519,13 @@ uint32_t mmnif_rxbuff_alloc(uint8_t dest, uint16_t len) char *memblock = (char *)heap_start_address + (dest - 1) * heap_size; #endif -// memblock = 0xC0000000 + ( dest -1 ) * heap_size; - uint32_t ret; - // if (rb->tail > rb->head) // if ((MMNIF_RX_BUFFERLEN - rb->tail < len)&&(rb->head < len)) // return NULL; // else // if ((rb->head - rb->tail < len)&&(rb->tail != rb->head)) // return NULL; + spinlock_lock(&rb->dlock); if (rb->dcount) { @@ -608,7 +599,7 @@ uint32_t mmnif_rxbuff_alloc(uint8_t dest, uint16_t len) * allocated packet to RDY so the recieve queue knows that it can be * processed further */ -int mmnif_commit_packet(uint8_t dest, uint32_t addr) +static int mmnif_commit_packet(uint8_t dest, uint32_t addr) { mm_rx_buffer_t *rb = (mm_rx_buffer_t *) ((char *)header_start_address + (dest - 1) * header_size); uint32_t i; @@ -631,12 +622,11 @@ int mmnif_commit_packet(uint8_t dest, uint32_t addr) * allocated packet to RDY so the recieve queue knows that it can be * processed further */ -int mmnif_commit_packet_bypass(uint8_t dest, uint32_t addr, int dest_socket) +static int mmnif_commit_packet_bypass(uint8_t dest, uint32_t addr, int dest_socket) { - mm_rx_buffer_t *rb = - (mm_rx_buffer_t *) ((char *)header_start_address + - (dest - 1) * header_size); + mm_rx_buffer_t* rb = (mm_rx_buffer_t *) ((char *)header_start_address + (dest - 1) * header_size); uint32_t i; + for (i = 0; i < MMNIF_MAX_DESCRIPTORS; i++) { if (rb->desc_table[i].addr == addr @@ -647,20 +637,23 @@ int mmnif_commit_packet_bypass(uint8_t dest, uint32_t addr, int dest_socket) return 0; } } + return -1; } /* mmnif_rxbuff_free() : the opposite to mmnif_rxbuff_alloc() a from the receiver * already processed chunk of memory is freed so that it can be allocated again */ -void mmnif_rxbuff_free() +static void mmnif_rxbuff_free(void) { mmnif_t *mmnif = mmnif_dev->state; mm_rx_buffer_t *b = mmnif->rx_buff; uint32_t i, j; uint32_t rpos; + spinlock_lock(&b->dlock); rpos = b->dread; + for (i = 0, j = rpos; i < MMNIF_MAX_DESCRIPTORS; i++) { j = (j + i) % MMNIF_MAX_DESCRIPTORS; @@ -672,31 +665,16 @@ void mmnif_rxbuff_free() if (b->tail > b->head) { b->head += b->desc_table[j].len; - } - - else - { - if ((b-> - desc_table[(j + - 1) % - MMNIF_MAX_DESCRIPTORS].stat != - MMNIF_STATUS_FREE) - && (b->desc_table[j].addr > - b->desc_table[(j + 1) % - MMNIF_MAX_DESCRIPTORS]. - addr)) + } else { + if ((b->desc_table[(j + 1) % MMNIF_MAX_DESCRIPTORS].stat != MMNIF_STATUS_FREE) + && (b->desc_table[j].addr > b->desc_table[(j + 1) % MMNIF_MAX_DESCRIPTORS].addr)) { b->head = 0; - } - - else - { + } else { b->head += b->desc_table[j].len; } } - } - - else + } else break; } spinlock_unlock(&b->dlock); @@ -705,7 +683,7 @@ void mmnif_rxbuff_free() /* * Transmid a packet (called by the lwip) */ -err_t mmnif_tx(struct netif *netif, struct pbuf *p) +static err_t mmnif_tx(struct netif *netif, struct pbuf *p) { mmnif_t *mmnif = netif->state; uint8_t slot = mmnif->tx_queue; @@ -730,6 +708,7 @@ err_t mmnif_tx(struct netif *netif, struct pbuf *p) DEBUGPRINTF("mmnif_tx(): too many packets at once for tx_queue\n"); goto drop_packet; } + if (p->tot_len > MMNIF_TX_BUFFERLEN) { DEBUGPRINTF("mmnif_tx(): packet is longer than %d bytes\n",MMNIF_TX_BUFFERLEN); @@ -776,6 +755,7 @@ realloc: // mmnif_trigger_irq(dest_ip); goto realloc; } + if (!write_address) goto drop_packet; @@ -816,7 +796,7 @@ realloc: DEBUGPRINTF("mmnif_tx(): packet somehow lost during commit\n"); } #ifdef DEBUG_MMNIF_PACKET -// DEBUGPRINTF("\n SEND 0x%.8X with length: %d\n",(char*)heap_start_address + (dest_ip -1)*mpb_size + pos * 1792,p->tot_len +2); +// DEBUGPRINTF("\n SEND 0x%p with length: %d\n",(char*)heap_start_address + (dest_ip -1)*mpb_size + pos * 1792,p->tot_len +2); // hex_dump(p->tot_len, p->payload); #endif @@ -831,6 +811,7 @@ realloc: mmnif->stats.tx_bytes += p->tot_len; if (rb->iv_intr) mmnif_trigger_irq(dest_ip); + return ERR_OK; drop_packet: @@ -845,13 +826,14 @@ drop_packet: spinlock_unlock(&mmnif->lock); LINK_STATS_INC(link.drop); mmnif->stats.tx_err++; + return ERR_IF; } /* mmnif_hashlookup(): looks up a bypass descriptor by * the associated socket */ -bypass_rxdesc_t *mmnif_hashlookup(int s) +static bypass_rxdesc_t *mmnif_hashlookup(int s) { int i; bypass_rxdesc_t *p; @@ -868,7 +850,7 @@ bypass_rxdesc_t *mmnif_hashlookup(int s) /* mmnif_hashadd(): adds a entry to the hashtable * by the socket */ -int mmnif_hashadd(int sock, int rsock, uint8_t dest_ip) +static int mmnif_hashadd(int sock, int rsock, uint8_t dest_ip) { bypass_rxdesc_t *p; int i; @@ -892,13 +874,15 @@ int mmnif_hashadd(int sock, int rsock, uint8_t dest_ip) /* mmnif_hashdelete(): deletes an entry from the * hashtable */ -int mmnif_hashdelete(int sock) +static int mmnif_hashdelete(int sock) { bypass_rxdesc_t *p; int i; + p = mmnif_hashlookup(sock); if (p != 0) return -1; + for (i = 0; i < MMNIF_HASHTABLE_SIZE; i++) { p = &mmnif_hashtable[(sock + i) % MMNIF_HASHTABLE_SIZE]; @@ -916,15 +900,13 @@ int mmnif_hashdelete(int sock) /* * Transmid a packet (with insane speed) */ -err_t mmnif_tx_bypass(struct netif * netif, void *pbuff, uint16_t size, int s) +static err_t mmnif_tx_bypass(struct netif * netif, void *pbuff, uint16_t size, int s) { mmnif_t *mmnif = netif->state; uint32_t write_address; bypass_rxdesc_t *dest = mmnif_hashlookup(s); //uint32_t exp_delay = 2; - mm_rx_buffer_t *rb = - (mm_rx_buffer_t *) ((char *)header_start_address + - (dest->dest_ip - 1) * header_size); + mm_rx_buffer_t *rb = (mm_rx_buffer_t *) ((char *)header_start_address + (dest->dest_ip - 1) * header_size); /* Perform serveral sanity checks on the packet and the buffers: * - is the output packet to big? @@ -937,8 +919,8 @@ err_t mmnif_tx_bypass(struct netif * netif, void *pbuff, uint16_t size, int s) // } /* allocate memory for the packet in the remote buffer */ - realloc:write_address = - mmnif_rxbuff_alloc(dest->dest_ip, CLINE_ALIGN(size)); +realloc: + write_address = mmnif_rxbuff_alloc(dest->dest_ip, CLINE_ALIGN(size)); if (!write_address) { @@ -984,7 +966,7 @@ err_t mmnif_tx_bypass(struct netif * netif, void *pbuff, uint16_t size, int s) DEBUGPRINTF("mmnif_tx(): packet somehow lost during commit\n"); } #ifdef DEBUG_MMNIF_PACKET - // DEBUGPRINTF("\n SEND 0x%.8X with length: %d\n",(char*)mpb_start_address + (dest_ip -1)*mpb_size + pos * 1792,p->tot_len +2); + // DEBUGPRINTF("\n SEND 0x%p with length: %d\n",(char*)mpb_start_address + (dest_ip -1)*mpb_size + pos * 1792,p->tot_len +2); // hex_dump(p->tot_len, p->payload); #endif @@ -1010,6 +992,7 @@ int mmnif_send(int s, void *data, size_t size, int flags) { bypass_rxdesc_t *p = mmnif_hashlookup(s); uint32_t i, j, k, ret; + if (p != 0) { if (size < ((MMNIF_RX_BUFFERLEN / 2) - 1)) @@ -1019,27 +1002,19 @@ int mmnif_send(int s, void *data, size_t size, int flags) { j = size / (((MMNIF_RX_BUFFERLEN / 2) - 1)); k = size - (j * (((MMNIF_RX_BUFFERLEN / 2) - 1))); + for (i = 0; i < j; i++) { - ret = - mmnif_tx_bypass(mmnif_dev, - data + - i * - ((MMNIF_RX_BUFFERLEN / 2) - - 1), - ((MMNIF_RX_BUFFERLEN / 2) - - 1), s); + ret = mmnif_tx_bypass(mmnif_dev, data + i * ((MMNIF_RX_BUFFERLEN / 2) - 1), ((MMNIF_RX_BUFFERLEN / 2) - 1), s); + if (ret < 0) return ret; } - ret = - mmnif_tx_bypass(mmnif_dev, - data + (j - - 1) * ((MMNIF_RX_BUFFERLEN / - 2) - 1), k, s); + ret = mmnif_tx_bypass(mmnif_dev, data + (j - 1) * ((MMNIF_RX_BUFFERLEN / 2) - 1), k, s); return ret; } } + return lwip_send(s, data, size, flags); } @@ -1068,7 +1043,10 @@ err_t mmnif_init(struct netif *netif) #ifdef DEBUG_MMNIF DEBUGPRINTF("mmnif init attempt\n"); -#endif /* */ +#endif + + mmnif_dev = netif; + own_ip_address += RCCE_ue() + 1; /* Alloc and clear memory for the device struct */ @@ -1083,17 +1061,16 @@ err_t mmnif_init(struct netif *netif) /* Alloc and clear shared memory for rx_buff */ header_size = (sizeof(mm_rx_buffer_t)); - DEBUGPRINTF("mmnif_init() : size of mm_rx_buffer_t : %d", - sizeof(mm_rx_buffer_t)); + DEBUGPRINTF("mmnif_init() : size of mm_rx_buffer_t : %d", sizeof(mm_rx_buffer_t)); // align mpb size to the granularity of a page size header_size = (header_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); header_start_address = (void*) RCCE_shmalloc(header_size * MMNIF_CORES); - DEBUGPRINTF("RCCE_shmalloc : 0x%.8X", header_start_address); + DEBUGPRINTF("RCCE_shmalloc : 0x%p", header_start_address); // map physical address in the virtual address space header_start_address = (void*) map_region(0, (size_t) header_start_address, (MMNIF_CORES * header_size) >> PAGE_SHIFT, MAP_KERNEL_SPACE | MAP_NO_CACHE); - DEBUGPRINTF("map_region : 0x%.8X", header_start_address); + DEBUGPRINTF("map_region : 0x%p", header_start_address); mmnif->rx_buff = (mm_rx_buffer_t *) (header_start_address + (header_size) * (own_ip_address - router_ip_address)); /* Alloc and clear shared memory for rx_buff @@ -1105,7 +1082,7 @@ err_t mmnif_init(struct netif *netif) #if MMNIF_USE_MPB heap_start_address = RCCE_malloc(RCCE_LINE_SIZE); - DEBUGPRINTF("MessagePassingBuffer allocated @ : 0x%.8X\n", heap_start_address); + DEBUGPRINTF("MessagePassingBuffer allocated @ : 0x%p\n", heap_start_address); for (i = heap_size / RCCE_LINE_SIZE; i > 0; i--) { if (!RCCE_malloc(RCCE_LINE_SIZE)) ; @@ -1117,41 +1094,34 @@ err_t mmnif_init(struct netif *netif) } } -#else /* */ +#else heap_start_address = (void*) RCCE_shmalloc(heap_size * MMNIF_CORES); n = (int) heap_start_address; - DEBUGPRINTF("RCCE_shmalloc : 0x%.8X", header_start_address); + DEBUGPRINTF("RCCE_shmalloc : 0x%p", header_start_address); // map physical address in the virtual address space #if USE_CACHE //heap_start_address = map_region(0, heap_start_address, (MMNIF_CORES*heap_size) >> PAGE_SHIFT, MAP_KERNEL_SPACE|MAP_MPE); heap_start_address = map_region(0, heap_start_address, (MMNIF_CORES * heap_size) >> PAGE_SHIFT, MAP_KERNEL_SPACE | MAP_NO_CACHE | MAP_MPE); - remap_region(heap_start_address + - (heap_size) * (own_ip_address - router_ip_address), - n + (heap_size) * (own_ip_address - router_ip_address), 1, - MAP_KERNEL_SPACE | MAP_MPE); - + remap_region(heap_start_address + (heap_size) * (own_ip_address - router_ip_address), n + (heap_size) * (own_ip_address - router_ip_address), 1, MAP_KERNEL_SPACE | MAP_MPE); #else heap_start_address = (void*) map_region(0, (size_t) heap_start_address, (MMNIF_CORES * heap_size) >> PAGE_SHIFT, MAP_KERNEL_SPACE | MAP_NO_CACHE | MAP_MPE); - #endif // USE_CAHCE #endif // MMNIF_USE_MPB - DEBUGPRINTF("map_region : 0x%.8X", header_start_address); + DEBUGPRINTF("map_region : 0x%p", header_start_address); #if MMNIF_USE_MPB mmnif->rx_heap = heap_start_address; heap_start_address = heap_start_address - (own_ip_address - router_ip_address) * 0x2000; - DEBUGPRINTF("heap_start_address : 0x%.8X", heap_start_address); + DEBUGPRINTF("heap_start_address : 0x%p", heap_start_address); // heap_start_address = 0xC0000000; // mmnif->rx_heap = heap_start_address + (heap_size) * (own_ip_address - router_ip_address); -#else /* */ - mmnif->rx_heap = - heap_start_address + (heap_size) * (own_ip_address - - router_ip_address); +#else + mmnif->rx_heap = heap_start_address + (heap_size) * (own_ip_address - router_ip_address); +#endif -#endif /* */ if (!(heap_start_address)) { DEBUGPRINTF("mmnif init(): allocating shared memory failed\n"); @@ -1163,25 +1133,23 @@ err_t mmnif_init(struct netif *netif) #if USE_CACHE *((int *)RCCE_fool_write_combine_buffer) = 1; -#endif /* */ +#endif if (own_ip_address == router_ip_address) { kprintf("Test0: MEMCPY 2048B"); tmp1 = get_clock_tick(); for (n = 0; n < 4096; n++) { - memcpy(mmnif->rx_heap + heap_size, mmnif->rx_heap, - 2048); + memcpy(mmnif->rx_heap + heap_size, mmnif->rx_heap, 2048); #if USE_CACHE *((int *)RCCE_fool_write_combine_buffer) = 1; flush_cache(); -#endif /* */ +#endif memcpy(mmnif->rx_heap, mmnif->rx_heap, 2048); } tmp2 = get_clock_tick(); - kprintf("memcpy'd' %d bytes in %d ticks", n * 2048, - (tmp2 - tmp1)); + kprintf("memcpy'd' %d bytes in %d ticks", n * 2048, (tmp2 - tmp1)); } /* set initial values @@ -1214,9 +1182,11 @@ err_t mmnif_init(struct netif *netif) } mmnif->tx_queue = 0; memset(mmnif->tx_buff[0], 0, MMNIF_TX_QUEUELEN * MMNIF_TX_BUFFERLEN); - for (i = 0; i < MMNIF_TX_QUEUELEN - 1; i++) + + for (i=0; itx_buff[i + 1] = mmnif->tx_buff[i] + MMNIF_TX_BUFFERLEN; - for (i = 0; i < MMNIF_HASHTABLE_SIZE; i++) + + for (i=0; irx_buff->acceptors[i].stat = MMNIF_ACC_STAT_CLOSED; mmnif->rx_buff->acceptors[i].nsock = -1; @@ -1244,8 +1215,8 @@ err_t mmnif_init(struct netif *netif) mmnif_dev = netif; /* administrative details */ - netif->name[0] = 'e'; - netif->name[1] = 'n'; + netif->name[0] = 'm'; + netif->name[1] = 'm'; netif->num = num; num++; @@ -1259,15 +1230,24 @@ err_t mmnif_init(struct netif *netif) netif->mtu = MMNIF_TX_BUFFERLEN; /* broadcast capability, keep all default flags */ - netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_LINK_UP; + netif->flags |= NETIF_FLAG_BROADCAST; /* hardware address length */ netif->hwaddr_len = 0; + + /* indicate that the driver is active now */ active = TRUE; + /* If interrupts are not used we immediately add the polling function + * to the queue which would otherwise be done through the IRQ handler. + */ + if (reduce_irq) + mmnif_device_schedule(); + #ifdef DEBUG_MMNIF DEBUGPRINTF("mmnif init complete\n"); #endif + return ERR_OK; } @@ -1375,7 +1355,7 @@ anotherpacket: */ #ifdef DEBUG_MMNIF_PACKET - DEBUGPRINTF("\n RECIEVED - 0x%.8X with legth: %d\n", packet, length); + DEBUGPRINTF("\n RECIEVED - 0x%p with legth: %d\n", packet, length); hex_dump(length, packet); #endif @@ -1436,19 +1416,22 @@ anotherpacket: mmnif->stats.rx_poll++; goto anotherpacket; return; - drop_packet:spinlock_lock(&mmnif->rx_buff->rlock); + +drop_packet: + spinlock_lock(&mmnif->rx_buff->rlock); /*error handling */ spinlock_unlock(&mmnif->rx_buff->rlock); LINK_STATS_INC(link.drop); mmnif->stats.rx_err++; + return; } /* mmnif_rx_bypass(): recieve packets * with insane speed ;) */ -int mmnif_rx_bypass(struct netif *netif, int s, void *data, uint32_t len) +static int mmnif_rx_bypass(struct netif *netif, int s, void *data, uint32_t len) { mmnif_t *mmnif = netif->state; mm_rx_buffer_t *b = mmnif->rx_buff; @@ -1507,7 +1490,7 @@ int mmnif_rx_bypass(struct netif *netif, int s, void *data, uint32_t len) */ #ifdef DEBUG_MMNIF_PACKET - DEBUGPRINTF("\n RECIEVED - 0x%.8X with legth: %d\n", packet, length); + DEBUGPRINTF("\n RECIEVED - 0x%p with legth: %d\n", packet, length); hex_dump(length, packet); #endif /* */ @@ -1537,12 +1520,15 @@ int mmnif_rx_bypass(struct netif *netif, int s, void *data, uint32_t len) else mmnif->stats.rx_poll++; return length; - drop_packet:spinlock_lock(&mmnif->rx_buff->rlock); + +drop_packet: + spinlock_lock(&mmnif->rx_buff->rlock); /*error handling */ spinlock_unlock(&mmnif->rx_buff->rlock); LINK_STATS_INC(link.drop); mmnif->stats.rx_err++; + return -1; } @@ -1559,7 +1545,7 @@ int mmnif_recv(int s, void *data, uint32_t len, int flags) #if MMNIF_FAST_SOCKET_BLOCK sem_wait(&p->sem, 0); -#else /* */ +#else while (!atomic_int32_read(&p->cnt)) { @@ -1570,6 +1556,7 @@ int mmnif_recv(int s, void *data, uint32_t len, int flags) #endif /* */ ret = mmnif_rx_bypass(mmnif_dev, s, data, len); atomic_int32_dec(&p->cnt); + return ret; } @@ -1579,6 +1566,7 @@ int mmnif_recv(int s, void *data, uint32_t len, int flags) int mmnif_socket(int domain, int type, int protocol) { int ret = 0; + if (domain == AF_MMNIF_NET) { spinlock_lock(&pseudolock); @@ -1586,6 +1574,7 @@ int mmnif_socket(int domain, int type, int protocol) spinlock_unlock(&pseudolock); return ret; } + return lwip_socket(domain, type, protocol); } @@ -1608,60 +1597,23 @@ int mmnif_accept(int s, struct sockaddr *addr, socklen_t * addrlen) if (b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS]. stat == MMNIF_ACC_STAT_CLOSED) { - spinlock_lock(&b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].port = port; - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].stat = - MMNIF_ACC_STAT_ACCEPTING; + spinlock_lock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].port = port; + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat = MMNIF_ACC_STAT_ACCEPTING; spinlock_lock(&pseudolock); mmnif_hashadd(npseudosocket, -1, 0); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].nsock = - npseudosocket++; + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock = npseudosocket++; spinlock_unlock(&pseudolock); - spinlock_unlock(&b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); - while (b-> - acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].stat != - MMNIF_ACC_STAT_ACCEPT_ME) + spinlock_unlock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + while (b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat != MMNIF_ACC_STAT_ACCEPT_ME) NOP8; - p = mmnif_hashlookup(b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - nsock); - p->dest_ip = - b->acceptors[(i + port) % - MMNIF_MAX_ACCEPTORS].src_ip; - p->remote_socket = - b->acceptors[(i + port) % - MMNIF_MAX_ACCEPTORS].rsock; - spinlock_lock(&b-> - acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].stat = - MMNIF_ACC_STAT_ACCEPTED; - i = b->acceptors[(i + port) % - MMNIF_MAX_ACCEPTORS].nsock; - spinlock_unlock(&b-> - acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); + p = mmnif_hashlookup(b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock); + p->dest_ip = b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].src_ip; + p->remote_socket = b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].rsock; + spinlock_lock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat = MMNIF_ACC_STAT_ACCEPTED; + i = b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock; + spinlock_unlock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); return i; } } @@ -1672,118 +1624,53 @@ int mmnif_accept(int s, struct sockaddr *addr, socklen_t * addrlen) { for (i = 0; i < MMNIF_MAX_ACCEPTORS; i++) { - if (b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS]. - stat == MMNIF_ACC_STAT_CLOSED) + if (b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat == MMNIF_ACC_STAT_CLOSED) { - spinlock_lock(&b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].port = port; - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].stat = - MMNIF_ACC_STAT_ACCEPTING; + spinlock_lock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].port = port; + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat = MMNIF_ACC_STAT_ACCEPTING; spinlock_lock(&pseudolock); mmnif_hashadd(npseudosocket, -1, 0); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].nsock = - npseudosocket++; + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock = npseudosocket++; spinlock_unlock(&pseudolock); - spinlock_unlock(&b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); - while (b-> - acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].stat != - MMNIF_ACC_STAT_ACCEPT_ME) + spinlock_unlock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + while (b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat != MMNIF_ACC_STAT_ACCEPT_ME) { tmp2 = get_clock_tick(); - if (tmp2 - tmp1 > - MMNIF_AUTO_SOCKET_TIMEOUT) + if (tmp2 - tmp1 > MMNIF_AUTO_SOCKET_TIMEOUT) { - spinlock_lock(&b->acceptors[(i + - port) - % - MMNIF_MAX_ACCEPTORS].alock); - if (b->acceptors - [(i + - port) % - MMNIF_MAX_ACCEPTORS]. - stat == - MMNIF_ACC_STAT_ACCEPT_ME) + spinlock_lock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + if (b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat == MMNIF_ACC_STAT_ACCEPT_ME) { - spinlock_unlock(&b-> - acceptors - [(i + - port) - % - MMNIF_MAX_ACCEPTORS]. - alock); + spinlock_unlock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); break; } #ifdef DEBUG_MMNIF - DEBUGPRINTF - ("mmnif_accept(): Timout occoured, switching to normal accept()"); + DEBUGPRINTF("mmnif_accept(): Timout occoured, switching to normal accept()"); -#endif /* */ - mmnif_hashdelete(b-> - acceptors[(i + - port) - % - MMNIF_MAX_ACCEPTORS]. - nsock); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - stat = - MMNIF_ACC_STAT_CLOSED; - spinlock_unlock(&b-> - acceptors[(i + - port) - % - MMNIF_MAX_ACCEPTORS].alock); +#endif + mmnif_hashdelete(b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock); + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat = MMNIF_ACC_STAT_CLOSED; + spinlock_unlock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); goto normalaccept; } NOP8; } - p = mmnif_hashlookup(b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - nsock); - p->dest_ip = - b->acceptors[(i + port) % - MMNIF_MAX_ACCEPTORS].src_ip; - p->remote_socket = - b->acceptors[(i + port) % - MMNIF_MAX_ACCEPTORS].rsock; - spinlock_lock(&b-> - acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); - b->acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS].stat = - MMNIF_ACC_STAT_ACCEPTED; - i = b->acceptors[(i + port) % - MMNIF_MAX_ACCEPTORS].nsock; - spinlock_unlock(&b-> - acceptors[(i + - port) % - MMNIF_MAX_ACCEPTORS]. - alock); + p = mmnif_hashlookup(b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock); + p->dest_ip = b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].src_ip; + p->remote_socket = b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].rsock; + spinlock_lock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); + b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].stat = MMNIF_ACC_STAT_ACCEPTED; + i = b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].nsock; + spinlock_unlock(&b->acceptors[(i + port) % MMNIF_MAX_ACCEPTORS].alock); return i; } } return -1; } - normalaccept:return lwip_accept(s, addr, addrlen); + +normalaccept: + return lwip_accept(s, addr, addrlen); } /* mmnif_connect(): replacement of lwip_connect for @@ -1939,8 +1826,7 @@ int mmnif_poll(void *e) mmnif_t *mmnif; if (!mmnif_dev) { - DEBUGPRINTF - ("mmnif_poll(): the driver is not initialized yet\n"); + DEBUGPRINTF("mmnif_poll(): the driver is not initialized yet\n"); return -1; } mmnif = (mmnif_t *) mmnif_dev->state; @@ -1988,94 +1874,30 @@ int mmnif_poll(void *e) return 0; } -/* - * Open the interface should be called by kernel to use this network interface - */ -int mmnif_open() -{ - struct ip_addr ipaddr; - struct ip_addr netmask; - struct ip_addr gw; - - /* calculate my own ip address from core number - * Note: core 1 is the router core - */ - IP4_ADDR(&gw, 0, 0, 0, 0); - IP4_ADDR(&ipaddr, 192, 168, 0, RCCE_ue() + 1); - IP4_ADDR(&netmask, 255, 255, 255, 0); - own_ip_address += RCCE_ue() + 1; - mmnif_dev = kmalloc(sizeof(struct netif)); - - /* register our Memory Mapped Virtual IP interface in the lwip stack - * and tell him how to use the interface: - * - mmnif_dev : the device data storage - * - ipaddr : the ip address wich should be used - * - gw : the gateway wicht should be used - * - mmnif_init : the initialization which has to be done in order to use our interface - * - ethernet_input : tells him that he should get ethernet input (inclusice ARP) - * - * Note: Ethernet Input will be removed because its NOT needed and will - * be replaced with ip_input - */ - if (!netif_add - (mmnif_dev, &ipaddr, &netmask, &gw, NULL, - (netif_init_fn) mmnif_init, tcpip_input)) - { - DEBUGPRINTF("mmnif_open() : unable to add network interface\n"); - return -1; - } - - /* set our network interface to the default interface for lwip */ - netif_set_default(mmnif_dev); - - /* tell lwip all initialization is done and we want to set it ab */ - netif_set_up(mmnif_dev); - - /* test if interface is really up */ - if (!netif_is_up(mmnif_dev)) - { - DEBUGPRINTF("mmnif_open(): network interface is not up\n"); - -// return -2; - } - - /* indicate that the driver is active now */ - active = TRUE; - - /* If interrupts are not used we immediately add the polling function - * to the queue which would otherwise be done through the IRQ handler. - */ - if (reduce_irq) - mmnif_device_schedule(); - -#ifdef DEBUG_MMNIF - DEBUGPRINTF("mmnif_dev is open\n"); - -#endif /* */ - return 0; -} - /* * close the interface should be called by kernel to close this interface and release resources * Note: it's temporarly empty. Support will be added. */ -int mmnif_close() +err_t mmnif_shutdown(void) { - mmnif_t *mmnif; - if (!mmnif_dev) - { - DEBUGPRINTF - ("mmnif_close(): you closed the device before it was properly opened -.-* \n"); + err_t err; + mmnif_t* mmnif; + + if (!mmnif_dev) { + DEBUGPRINTF("mmnif_shutdown(): you closed the device before it was properly initialized -.-* \n"); + return ERR_MEM; } mmnif = (mmnif_t *) mmnif_dev->state; + err = netifapi_netif_set_down(mmnif_dev); + /* indicate that the driver is not active anymore * - this will stop the polling thread i.e. */ active = FALSE; kfree(mmnif->tx_buff[0], MMNIF_TX_QUEUELEN * MMNIF_TX_BUFFERLEN); - kfree(mmnif_dev, sizeof(mmnif_t)); //RCCE_shfree(mpb_start_address); - return 0; + + return err; } diff --git a/drivers/net/mmnif.h b/drivers/net/mmnif.h index 97305344..d5d88579 100644 --- a/drivers/net/mmnif.h +++ b/drivers/net/mmnif.h @@ -20,6 +20,11 @@ #ifndef __MMNIF_H__ #define __MMNIF_H__ +#include +#ifdef CONFIG_LWIP +#include +#include /* lwip netif */ + #define AF_MMNIF_NET 0x1337 #define MMNIF_AUTOACTIVATE_FAST_SOCKETS 0 @@ -35,14 +40,13 @@ #define listen(a,b) mmnif_listen(a,b) #endif -/* - * Initialize the network driver for mmn - */ -int mmnif_open(void); -int mmnif_close(void); +err_t mmnif_init(struct netif*); +err_t mmnif_shutdown(void); int mmnif_poll(void *e); int mmnif_worker(void *e); void mmnif_irqhandler(void); void mmnif_print_driver_status(); #endif + +#endif