2005-06-29 12:16:46 +02:00
|
|
|
/*
|
|
|
|
** File: netbuff.c Jun. 10, 2000
|
|
|
|
**
|
|
|
|
** Author: Giovanni Falzoni <gfalzoni@inwind.it>
|
|
|
|
**
|
|
|
|
** This file contains specific implementation of buffering
|
|
|
|
** for network packets.
|
|
|
|
*/
|
|
|
|
|
2010-03-22 22:25:22 +01:00
|
|
|
#include <minix/drivers.h>
|
2005-06-29 12:16:46 +02:00
|
|
|
#include <net/gen/ether.h>
|
|
|
|
#include <net/gen/eth_io.h>
|
|
|
|
#include "dp.h"
|
|
|
|
|
2005-08-03 13:53:36 +02:00
|
|
|
#if (HAVE_BUFFERS == 1)
|
2005-06-29 12:16:46 +02:00
|
|
|
|
|
|
|
static m_hdr_t *allocptr = NULL;
|
|
|
|
static char tx_rx_buff[8192];
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Name: void *alloc_buff(dpeth_t *dep, int size)
|
|
|
|
** Function: Allocates a buffer from the common pool.
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void *alloc_buff(dpeth_t *dep, int size)
|
2005-06-29 12:16:46 +02:00
|
|
|
{
|
|
|
|
m_hdr_t *ptr, *wrk = allocptr;
|
|
|
|
int units = ((size + sizeof(m_hdr_t) - 1) / sizeof(m_hdr_t)) + 1;
|
|
|
|
|
|
|
|
lock();
|
|
|
|
for (ptr = wrk->next;; wrk = ptr, ptr = ptr->next) {
|
|
|
|
if (ptr->size >= units) {
|
|
|
|
/* Memory is available, carve requested size from pool */
|
|
|
|
if (ptr->size == units) {
|
|
|
|
wrk->next = ptr->next;
|
|
|
|
} else {
|
|
|
|
/* Get memory from top address */
|
|
|
|
ptr->size -= units;
|
|
|
|
ptr += ptr->size;
|
|
|
|
ptr->size = units;
|
|
|
|
}
|
|
|
|
allocptr = wrk;
|
|
|
|
unlock();
|
|
|
|
return ptr + 1;
|
|
|
|
}
|
|
|
|
if (ptr == allocptr) break;
|
|
|
|
}
|
|
|
|
unlock();
|
|
|
|
return NULL; /* No memory available */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Name: void free_buff(dpeth_t *dep, void *blk)
|
|
|
|
** Function: Returns a buffer to the common pool.
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void free_buff(dpeth_t *dep, void *blk)
|
2005-06-29 12:16:46 +02:00
|
|
|
{
|
|
|
|
m_hdr_t *wrk, *ptr = (m_hdr_t *) blk - 1;
|
|
|
|
|
|
|
|
lock(); /* Scan linked list for the correct place */
|
|
|
|
for (wrk = allocptr; !(ptr > wrk && ptr < wrk->next); wrk = wrk->next)
|
|
|
|
if (wrk >= wrk->next && (ptr > wrk || ptr < wrk->next)) break;
|
|
|
|
|
|
|
|
/* Check if adjacent block is free and join blocks */
|
|
|
|
if (ptr + ptr->size == wrk->next) {
|
|
|
|
ptr->size += wrk->next->size;
|
|
|
|
ptr->next = wrk->next->next;
|
|
|
|
} else
|
|
|
|
ptr->next = wrk->next;
|
|
|
|
if (wrk + wrk->size == ptr) {
|
|
|
|
wrk->size += ptr->size;
|
|
|
|
wrk->next = ptr->next;
|
|
|
|
} else
|
|
|
|
wrk->next = ptr;
|
|
|
|
allocptr = wrk; /* Point allocptr to block just released */
|
|
|
|
unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Name: void init_buff(dpeth_t *dep, buff_t **tx_buff)
|
|
|
|
** Function: Initalizes driver data structures.
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void init_buff(dpeth_t *dep, buff_t **tx_buff)
|
2005-06-29 12:16:46 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Initializes buffer pool */
|
|
|
|
if (allocptr == NULL) {
|
|
|
|
m_hdr_t *rx = (m_hdr_t *) tx_rx_buff;
|
|
|
|
rx->next = allocptr = rx;
|
|
|
|
rx->size = 0;
|
|
|
|
rx += 1;
|
|
|
|
rx->next = NULL;
|
|
|
|
rx->size = (sizeof(tx_rx_buff) / sizeof(m_hdr_t)) - 1;
|
|
|
|
free_buff(dep, rx + 1);
|
|
|
|
dep->de_recvq_tail = dep->de_recvq_head = NULL;
|
|
|
|
if (tx_buff != NULL) {
|
|
|
|
*tx_buff = alloc_buff(dep, ETH_MAX_PACK_SIZE + sizeof(buff_t));
|
|
|
|
(*tx_buff)->size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return; /* Done */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Name: void mem2user(dpeth_t *dep, buff_t *rxbuff);
|
|
|
|
** Function: Copies a packet from local buffer to user area.
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void mem2user(dpeth_t *dep, buff_t *rxbuff)
|
2005-06-29 12:16:46 +02:00
|
|
|
{
|
|
|
|
int bytes, ix = 0;
|
2006-07-10 14:43:38 +02:00
|
|
|
iovec_dat_s_t *iovp = &dep->de_read_iovec;
|
|
|
|
int r, pktsize = rxbuff->size;
|
2005-06-29 12:16:46 +02:00
|
|
|
char *buffer = rxbuff->buffer;
|
|
|
|
|
|
|
|
do { /* Reads chuncks of packet into user buffers */
|
|
|
|
|
|
|
|
bytes = iovp->iod_iovec[ix].iov_size; /* Size of buffer */
|
|
|
|
if (bytes > pktsize) bytes = pktsize;
|
|
|
|
|
|
|
|
/* Reads from Rx buffer to user area */
|
2006-07-10 14:43:38 +02:00
|
|
|
r= sys_safecopyto(iovp->iod_proc_nr, iovp->iod_iovec[ix].iov_grant, 0,
|
|
|
|
(vir_bytes)buffer, bytes, D);
|
|
|
|
if (r != OK)
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("mem2user: sys_safecopyto failed: %d", r);
|
2005-06-29 12:16:46 +02:00
|
|
|
buffer += bytes;
|
|
|
|
|
|
|
|
if (++ix >= IOVEC_NR) { /* Next buffer of IO vector */
|
|
|
|
dp_next_iovec(iovp);
|
|
|
|
ix = 0;
|
|
|
|
}
|
|
|
|
/* Till packet done */
|
|
|
|
} while ((pktsize -= bytes) > 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Name: void user2mem(dpeth_t *dep, buff_t *txbuff)
|
|
|
|
** Function: Copies a packet from user area to local buffer.
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void user2mem(dpeth_t *dep, buff_t *txbuff)
|
2005-06-29 12:16:46 +02:00
|
|
|
{
|
|
|
|
int bytes, ix = 0;
|
2006-07-10 14:43:38 +02:00
|
|
|
iovec_dat_s_t *iovp = &dep->de_write_iovec;
|
|
|
|
int r, pktsize = txbuff->size;
|
2005-06-29 12:16:46 +02:00
|
|
|
char *buffer = txbuff->buffer;
|
|
|
|
|
|
|
|
do { /* Reads chuncks of packet from user buffers */
|
|
|
|
|
|
|
|
bytes = iovp->iod_iovec[ix].iov_size; /* Size of buffer */
|
|
|
|
if (bytes > pktsize) bytes = pktsize;
|
2006-07-10 14:43:38 +02:00
|
|
|
r= sys_safecopyfrom(iovp->iod_proc_nr, iovp->iod_iovec[ix].iov_grant,
|
|
|
|
0, (vir_bytes)buffer, bytes, D);
|
|
|
|
if (r != OK)
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("user2mem: sys_safecopyfrom failed: %d", r);
|
2005-06-29 12:16:46 +02:00
|
|
|
buffer += bytes;
|
|
|
|
|
|
|
|
if (++ix >= IOVEC_NR) { /* Next buffer of IO vector */
|
|
|
|
dp_next_iovec(iovp);
|
|
|
|
ix = 0;
|
|
|
|
}
|
|
|
|
/* Till packet done */
|
|
|
|
} while ((pktsize -= bytes) > 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-03 13:53:36 +02:00
|
|
|
#endif /* HAVE_BUFFERS */
|
2005-06-29 12:16:46 +02:00
|
|
|
|
|
|
|
/** netbuff.c **/
|