1210311Sjmallett/*************************************************************************
2210311SjmallettCopyright (c) 2003-2007  Cavium Networks (support@cavium.com). All rights
3210311Sjmallettreserved.
4210311Sjmallett
5210311Sjmallett
6210311SjmallettRedistribution and use in source and binary forms, with or without
7210311Sjmallettmodification, are permitted provided that the following conditions are
8210311Sjmallettmet:
9210311Sjmallett
10210311Sjmallett    * Redistributions of source code must retain the above copyright
11210311Sjmallett      notice, this list of conditions and the following disclaimer.
12210311Sjmallett
13210311Sjmallett    * Redistributions in binary form must reproduce the above
14210311Sjmallett      copyright notice, this list of conditions and the following
15210311Sjmallett      disclaimer in the documentation and/or other materials provided
16210311Sjmallett      with the distribution.
17210311Sjmallett
18210311Sjmallett    * Neither the name of Cavium Networks nor the names of
19210311Sjmallett      its contributors may be used to endorse or promote products
20210311Sjmallett      derived from this software without specific prior written
21210311Sjmallett      permission.
22210311Sjmallett
23210311SjmallettThis Software, including technical data, may be subject to U.S. export  control laws, including the U.S. Export Administration Act and its  associated regulations, and may be subject to export or import  regulations in other countries.
24210311Sjmallett
25210311SjmallettTO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
26210311SjmallettAND WITH ALL FAULTS AND CAVIUM  NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
27210311Sjmallett
28210311Sjmallett*************************************************************************/
29210311Sjmallett
30210311Sjmallett#include <sys/cdefs.h>
31210311Sjmallett__FBSDID("$FreeBSD$");
32210311Sjmallett
33210311Sjmallett#include <sys/param.h>
34210311Sjmallett#include <sys/systm.h>
35210311Sjmallett#include <sys/bus.h>
36210311Sjmallett#include <sys/endian.h>
37210311Sjmallett#include <sys/kernel.h>
38210311Sjmallett#include <sys/mbuf.h>
39210311Sjmallett#include <sys/socket.h>
40210311Sjmallett
41210311Sjmallett#include <net/ethernet.h>
42210311Sjmallett#include <net/if.h>
43210311Sjmallett
44210311Sjmallett#include "wrapper-cvmx-includes.h"
45210311Sjmallett#include "ethernet-headers.h"
46210311Sjmallett
47210311Sjmallett/**
48210311Sjmallett * Fill the supplied hardware pool with mbufs
49210311Sjmallett *
50210311Sjmallett * @param pool     Pool to allocate an mbuf for
51210311Sjmallett * @param size     Size of the buffer needed for the pool
52210311Sjmallett * @param elements Number of buffers to allocate
53210311Sjmallett */
54215974Sjmallettint cvm_oct_mem_fill_fpa(int pool, int size, int elements)
55210311Sjmallett{
56210311Sjmallett	int freed = elements;
57210311Sjmallett	while (freed) {
58210311Sjmallett		KASSERT(size <= MCLBYTES - 128, ("mbuf clusters are too small"));
59210311Sjmallett
60210311Sjmallett		struct mbuf *m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
61210311Sjmallett		if (__predict_false(m == NULL)) {
62210311Sjmallett			printf("Failed to allocate mbuf for hardware pool %d\n", pool);
63210311Sjmallett			break;
64210311Sjmallett		}
65210311Sjmallett
66210311Sjmallett		m->m_data += 128 - (((uintptr_t)m->m_data) & 0x7f);
67210311Sjmallett		*(struct mbuf **)(m->m_data - sizeof(void *)) = m;
68210311Sjmallett		cvmx_fpa_free(m->m_data, pool, DONT_WRITEBACK(size/128));
69210311Sjmallett		freed--;
70210311Sjmallett	}
71210311Sjmallett	return (elements - freed);
72210311Sjmallett}
73210311Sjmallett
74210311Sjmallett
75210311Sjmallett/**
76210311Sjmallett * Free the supplied hardware pool of mbufs
77210311Sjmallett *
78210311Sjmallett * @param pool     Pool to allocate an mbuf for
79210311Sjmallett * @param size     Size of the buffer needed for the pool
80210311Sjmallett * @param elements Number of buffers to allocate
81210311Sjmallett */
82215974Sjmallettvoid cvm_oct_mem_empty_fpa(int pool, int size, int elements)
83210311Sjmallett{
84210311Sjmallett	char *memory;
85210311Sjmallett
86210311Sjmallett	do {
87210311Sjmallett		memory = cvmx_fpa_alloc(pool);
88210311Sjmallett		if (memory) {
89210311Sjmallett			struct mbuf *m = *(struct mbuf **)(memory - sizeof(void *));
90210311Sjmallett			elements--;
91210311Sjmallett			m_freem(m);
92210311Sjmallett		}
93210311Sjmallett	} while (memory);
94210311Sjmallett
95210311Sjmallett	if (elements < 0)
96210311Sjmallett		printf("Warning: Freeing of pool %u had too many mbufs (%d)\n", pool, elements);
97210311Sjmallett	else if (elements > 0)
98210311Sjmallett		printf("Warning: Freeing of pool %u is missing %d mbufs\n", pool, elements);
99210311Sjmallett}
100