3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/sys/dev/hea/eni_buffer.c,v 1.5 1999/08/28 00:41:43 peter Exp $
30 * Efficient ENI Adapter Support
31 * -----------------------------
33 * Handle adapter memory buffers for ENI adapters
37 #include <netproto/atm/kern_include.h>
39 #include "eni_stats.h"
43 static int eni_test_memory (Eni_unit *);
46 * The host is going to manage (that is, allocate and free) buffers
47 * in the adapters RAM space. We are going to implement this as a
48 * linked list describing FREE and INUSE memory segments. Initially,
49 * the list contains one element with all memory marked free. As requests
50 * are made, we search the list until we find the first free element
51 * which can satisfy the request. If necessary, we will break the free
52 * element into an INUSE element, and a new FREE element. When freeing
53 * memory, we look at adjacent elements and if one or more are free,
54 * we will combine into a single larger FREE element.
58 * This is for testing purposes. Since there are two versions of
59 * the Efficient adapter with different memory sizes, this allows
60 * us to fool an adapter with more memory into thinking it has less.
62 static int eni_mem_max = MAX_ENI_MEM; /* Default to all available memory */
65 * Size and test adapter RAM
67 * Walk through adapter RAM writing known patterns and reading back
68 * for comparison. We write more than one pattern on the off chance
69 * that we "get lucky" and read what we expected.
72 * eup pointer to device unit structure
75 * size memory size in bytes
78 eni_test_memory(Eni_unit *eup)
85 * Walk through to maximum looking for RAM
87 for ( i = 0; i < MAX_ENI_MEM; i += TEST_STEP ) {
88 mp = (Eni_mem)((int)eup->eu_ram + i);
90 *mp = (u_long)TEST_PAT;
91 /* read pattern, match? */
92 if ( *mp == (u_long)TEST_PAT ) {
93 /* yes - write inverse pattern */
94 *mp = (u_long)~TEST_PAT;
95 /* read pattern, match? */
96 if ( *mp == (u_long)~TEST_PAT ) {
97 /* yes - assume another 1K available */
98 ram_size = i + TEST_STEP;
105 * Clear all RAM to initial value of zero.
106 * This makes sure we don't leave anything funny in the
109 KM_ZERO ( eup->eu_ram, ram_size );
112 * If we'd like to claim to have less memory, here's where
113 * we do so. We take the minimum of what we'd like and what
114 * we really found on the adapter.
116 ram_size = MIN ( ram_size, eni_mem_max );
123 * Initialize our memory allocator.
126 * eup Pointer to per unit structure
129 * size Physical RAM size
130 * -1 failed to initialize memory
134 eni_init_memory(Eni_unit *eup)
138 * Have we (somehow) been called before?
140 if ( eup->eu_memmap != NULL )
142 /* Oops - it's already been initialized */
147 * Allocate initial element which will hold all of memory
149 eup->eu_memmap = (Mbd *)KM_ALLOC(sizeof(Mbd), M_DEVBUF, M_WAITOK);
152 * Test and size memory
154 eup->eu_ramsize = eni_test_memory ( eup );
157 * Initialize a one element list which contains
160 eup->eu_memmap->prev = eup->eu_memmap->next = NULL;
161 eup->eu_memmap->base = (caddr_t)SEGBUF_BASE;
162 eup->eu_memmap->size = eup->eu_ramsize - SEGBUF_BASE;
163 eup->eu_memmap->state = MEM_FREE;
165 return ( eup->eu_ramsize );
169 * Allocate a buffer from adapter RAM. Due to constraints on the card,
170 * we may roundup the size request to the next largest chunksize. Note
171 * also that we must pay attention to address alignment within adapter
175 * eup pointer to per unit structure
176 * size pointer to requested size - in bytes
179 * addr address relative to adapter of allocated memory
180 * size modified to reflect actual size of buffer
184 eni_allocate_buffer(Eni_unit *eup, u_long *size)
188 Mbd *eptr = eup->eu_memmap;
191 * Initial size requested
196 * Find the buffer size which will hold this request. There
197 * are 8 possible sizes, each a power of two up, starting at
198 * 256 words or 1024 bytes.
200 for ( nclicks = 0; nclicks < ENI_BUF_NBIT; nclicks++ )
201 if ( ( 1 << nclicks ) * ENI_BUF_PGSZ >= nsize )
205 * Request was for larger then the card supports
207 if ( nclicks >= ENI_BUF_NBIT ) {
208 eup->eu_stats.eni_st_drv.drv_mm_toobig++;
209 /* Indicate 0 bytes allocated */
211 /* Return NULL buffer */
216 * New size will be buffer size
218 nsize = ( 1 << nclicks ) * ENI_BUF_PGSZ;
221 * Look through memory for a segment large enough to
226 * State must be FREE and size must hold request
228 if ( eptr->state == MEM_FREE && eptr->size >= nsize )
231 * Request will fit - now check if the
232 * alignment needs fixing
234 if ( ((u_int)eptr->base & (nsize-1)) != 0 )
239 * Calculate where the buffer would have to
240 * fall to be aligned.
242 nbase = (caddr_t)((u_int)( eptr->base + nsize ) &
245 * If we use this alignment, will it still fit?
247 if ( (eptr->size - (nbase - eptr->base)) >= 0 )
251 /* Yep - create a new segment */
252 etmp = (Mbd *)KM_ALLOC(sizeof(Mbd), M_DEVBUF, M_WAITOK);
253 /* Place it in the list */
254 etmp->next = eptr->next;
256 etmp->next->prev = etmp;
259 /* Fill in new base and size */
261 etmp->size = eptr->size - ( nbase - eptr->base );
262 /* Adjust old size */
263 eptr->size -= etmp->size;
265 etmp->state = MEM_FREE;
267 /* Done - outa here */
271 break; /* Alignment is okay - we're done */
273 /* Haven't found anything yet - keep looking */
279 /* Found a usable segment - grab what we need */
281 if ( eptr->size == nsize )
282 /* Mark it as INUSE */
283 eptr->state = MEM_INUSE;
287 /* larger then we need - split it */
289 etmp = (Mbd *)KM_ALLOC(sizeof(Mbd), M_DEVBUF, M_WAITOK);
290 /* Place new element in list */
291 etmp->next = eptr->next;
293 etmp->next->prev = etmp;
296 /* Set new base, size and state */
297 etmp->base = eptr->base + nsize;
298 etmp->size = eptr->size - nsize;
299 etmp->state = MEM_FREE;
300 /* Adjust size and state of element we intend to use */
302 eptr->state = MEM_INUSE;
306 /* After all that, did we find a usable buffer? */
309 /* Record another inuse buffer of this size */
311 eup->eu_memclicks[nclicks]++;
314 * Return true size of allocated buffer
318 * Make address relative to start of RAM since
319 * its (the address) for use by the adapter, not
322 return ((caddr_t)eptr->base);
324 eup->eu_stats.eni_st_drv.drv_mm_nobuf++;
325 /* No buffer to return - indicate zero length */
327 /* Return NULL buffer */
333 * Procedure to release a buffer previously allocated from adapter
334 * RAM. When possible, we'll compact memory.
337 * eup pointer to per unit structure
338 * base base adapter address of buffer to be freed
345 eni_free_buffer(Eni_unit *eup, caddr_t base)
347 Mbd *eptr = eup->eu_memmap;
350 /* Look through entire list */
353 /* Is this the buffer to be freed? */
354 if ( eptr->base == base )
357 * We're probably asking for trouble but,
360 if ( eptr->state != MEM_INUSE )
362 eup->eu_stats.eni_st_drv.drv_mm_notuse++;
363 /* Huh? Something's wrong */
366 /* Reset state to FREE */
367 eptr->state = MEM_FREE;
369 /* Determine size for stats info */
370 for ( nclicks = 0; nclicks < ENI_BUF_NBIT; nclicks++ )
371 if ( ( 1 << nclicks ) * ENI_BUF_PGSZ == eptr->size )
374 /* Valid size? Yes - decrement inuse count */
375 if ( nclicks < ENI_BUF_NBIT )
376 eup->eu_memclicks[nclicks]--;
378 /* Try to compact neighbors */
381 if ( eptr->prev->state == MEM_FREE )
384 /* Add to previous block */
385 eptr->prev->size += eptr->size;
386 /* Set prev block to skip this one */
387 eptr->prev->next = eptr->next;
388 /* Set next block to skip this one */
390 eptr->next->prev = eptr->prev;
391 /* Reset to where we want to be */
393 /* and free this element */
394 (void)KM_FREE(etmp, etmp->size, M_DEVBUF);
398 if ( eptr->next->state == MEM_FREE )
400 Mbd *etmp = eptr->next;
402 /* add following block in */
403 eptr->size += etmp->size;
404 /* set next next block to skip next block */
406 etmp->next->prev = eptr;
407 /* skip next block */
408 eptr->next = etmp->next;
409 /* and free next element */
410 (void)KM_FREE(etmp, etmp->size, M_DEVBUF);
413 * We've freed the buffer and done any compaction,
414 * we needn't look any further...
423 /* Oops - failed to find the buffer. This is BAD */
424 eup->eu_stats.eni_st_drv.drv_mm_notfnd++;