xref: /minix3/minix/drivers/net/dpeth/netbuff.c (revision f7df02e7476731c31f12548e38bcadbaf0233f6a)
1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc **  File:	netbuff.c	Jun. 10, 2000
3433d6423SLionel Sambuc **
4433d6423SLionel Sambuc **  Author:	Giovanni Falzoni <gfalzoni@inwind.it>
5433d6423SLionel Sambuc **
6433d6423SLionel Sambuc **  This file contains specific implementation of buffering
7433d6423SLionel Sambuc **  for network packets.
8433d6423SLionel Sambuc */
9433d6423SLionel Sambuc 
10433d6423SLionel Sambuc #include <minix/drivers.h>
1191c4db25SDavid van Moolenbroek #include <minix/netdriver.h>
12433d6423SLionel Sambuc #include "dp.h"
13433d6423SLionel Sambuc 
14433d6423SLionel Sambuc #if (HAVE_BUFFERS == 1)
15433d6423SLionel Sambuc 
16433d6423SLionel Sambuc static m_hdr_t *allocptr = NULL;
17433d6423SLionel Sambuc static char tx_rx_buff[8192];
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc /*
2091c4db25SDavid van Moolenbroek **  Name:	alloc_buff
21433d6423SLionel Sambuc **  Function:	Allocates a buffer from the common pool.
22433d6423SLionel Sambuc */
alloc_buff(dpeth_t * dep,int size)23433d6423SLionel Sambuc void *alloc_buff(dpeth_t *dep, int size)
24433d6423SLionel Sambuc {
25433d6423SLionel Sambuc   m_hdr_t *ptr, *wrk = allocptr;
26433d6423SLionel Sambuc   int units = ((size + sizeof(m_hdr_t) - 1) / sizeof(m_hdr_t)) + 1;
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc   for (ptr = wrk->next;; wrk = ptr, ptr = ptr->next) {
29433d6423SLionel Sambuc 	if (ptr->size >= units) {
30433d6423SLionel Sambuc 		/* Memory is available, carve requested size from pool */
31433d6423SLionel Sambuc 		if (ptr->size == units) {
32433d6423SLionel Sambuc 			wrk->next = ptr->next;
33433d6423SLionel Sambuc 		} else {
34433d6423SLionel Sambuc 			/* Get memory from top address */
35433d6423SLionel Sambuc 			ptr->size -= units;
36433d6423SLionel Sambuc 			ptr += ptr->size;
37433d6423SLionel Sambuc 			ptr->size = units;
38433d6423SLionel Sambuc 		}
39433d6423SLionel Sambuc 		allocptr = wrk;
40433d6423SLionel Sambuc 		return ptr + 1;
41433d6423SLionel Sambuc 	}
42433d6423SLionel Sambuc 	if (ptr == allocptr) break;
43433d6423SLionel Sambuc   }
44433d6423SLionel Sambuc   return NULL;			/* No memory available */
45433d6423SLionel Sambuc }
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc /*
4891c4db25SDavid van Moolenbroek **  Name:	free_buff
49433d6423SLionel Sambuc **  Function:	Returns a buffer to the common pool.
50433d6423SLionel Sambuc */
free_buff(dpeth_t * dep,void * blk)51433d6423SLionel Sambuc void free_buff(dpeth_t *dep, void *blk)
52433d6423SLionel Sambuc {
53433d6423SLionel Sambuc   m_hdr_t *wrk, *ptr = (m_hdr_t *) blk - 1;
54433d6423SLionel Sambuc 
5591c4db25SDavid van Moolenbroek   /* Scan linked list for the correct place */
56433d6423SLionel Sambuc   for (wrk = allocptr; !(ptr > wrk && ptr < wrk->next); wrk = wrk->next)
57433d6423SLionel Sambuc 	if (wrk >= wrk->next && (ptr > wrk || ptr < wrk->next)) break;
58433d6423SLionel Sambuc 
59433d6423SLionel Sambuc   /* Check if adjacent block is free and join blocks */
60433d6423SLionel Sambuc   if (ptr + ptr->size == wrk->next) {
61433d6423SLionel Sambuc 	ptr->size += wrk->next->size;
62433d6423SLionel Sambuc 	ptr->next = wrk->next->next;
63433d6423SLionel Sambuc   } else
64433d6423SLionel Sambuc 	ptr->next = wrk->next;
65433d6423SLionel Sambuc   if (wrk + wrk->size == ptr) {
66433d6423SLionel Sambuc 	wrk->size += ptr->size;
67433d6423SLionel Sambuc 	wrk->next = ptr->next;
68433d6423SLionel Sambuc   } else
69433d6423SLionel Sambuc 	wrk->next = ptr;
70433d6423SLionel Sambuc   allocptr = wrk;		/* Point allocptr to block just released */
71433d6423SLionel Sambuc }
72433d6423SLionel Sambuc 
73433d6423SLionel Sambuc /*
7491c4db25SDavid van Moolenbroek **  Name:	init_buff
75433d6423SLionel Sambuc **  Function:	Initalizes driver data structures.
76433d6423SLionel Sambuc */
init_buff(dpeth_t * dep,buff_t ** tx_buff)77433d6423SLionel Sambuc void init_buff(dpeth_t *dep, buff_t **tx_buff)
78433d6423SLionel Sambuc {
79433d6423SLionel Sambuc 
80433d6423SLionel Sambuc   /* Initializes buffer pool */
81433d6423SLionel Sambuc   if (allocptr == NULL) {
82433d6423SLionel Sambuc 	m_hdr_t *rx = (m_hdr_t *) tx_rx_buff;
83433d6423SLionel Sambuc 	rx->next = allocptr = rx;
84433d6423SLionel Sambuc 	rx->size = 0;
85433d6423SLionel Sambuc 	rx += 1;
86433d6423SLionel Sambuc 	rx->next = NULL;
87433d6423SLionel Sambuc 	rx->size = (sizeof(tx_rx_buff) / sizeof(m_hdr_t)) - 1;
88433d6423SLionel Sambuc 	free_buff(dep, rx + 1);
89433d6423SLionel Sambuc 	dep->de_recvq_tail = dep->de_recvq_head = NULL;
90433d6423SLionel Sambuc 	if (tx_buff != NULL) {
91*f7df02e7SDavid van Moolenbroek 		*tx_buff = alloc_buff(dep,
92*f7df02e7SDavid van Moolenbroek 		    NDEV_ETH_PACKET_MAX + sizeof(buff_t));
93433d6423SLionel Sambuc 		(*tx_buff)->size = 0;
94433d6423SLionel Sambuc 	}
95433d6423SLionel Sambuc   }
96433d6423SLionel Sambuc }
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc #endif				/* HAVE_BUFFERS */
99433d6423SLionel Sambuc 
100433d6423SLionel Sambuc /** netbuff.c **/
101