1 /*************************************************************************
2 Copyright (c) 2003-2007 Cavium Networks (support@cavium.com). All rights
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials provided
16 with the distribution.
18 * Neither the name of Cavium Networks nor the names of
19 its contributors may be used to endorse or promote products
20 derived from this software without specific prior written
23 This 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.
25 TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
26 AND 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.
28 *************************************************************************/
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
39 #include <sys/socket.h>
41 #include <net/ethernet.h>
44 #include "wrapper-cvmx-includes.h"
45 #include "ethernet-headers.h"
48 * Fill the supplied hardware pool with mbufs
50 * @param pool Pool to allocate an mbuf for
51 * @param size Size of the buffer needed for the pool
52 * @param elements Number of buffers to allocate
54 static int cvm_oct_fill_hw_mbuf(int pool, int size, int elements)
58 KASSERT(size <= MCLBYTES - 128, ("mbuf clusters are too small"));
60 struct mbuf *m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
61 if (__predict_false(m == NULL)) {
62 printf("Failed to allocate mbuf for hardware pool %d\n", pool);
66 m->m_data += 128 - (((uintptr_t)m->m_data) & 0x7f);
67 *(struct mbuf **)(m->m_data - sizeof(void *)) = m;
68 cvmx_fpa_free(m->m_data, pool, DONT_WRITEBACK(size/128));
71 return (elements - freed);
76 * Free the supplied hardware pool of mbufs
78 * @param pool Pool to allocate an mbuf for
79 * @param size Size of the buffer needed for the pool
80 * @param elements Number of buffers to allocate
82 static void cvm_oct_free_hw_mbuf(int pool, int size, int elements)
87 memory = cvmx_fpa_alloc(pool);
89 struct mbuf *m = *(struct mbuf **)(memory - sizeof(void *));
96 printf("Warning: Freeing of pool %u had too many mbufs (%d)\n", pool, elements);
97 else if (elements > 0)
98 printf("Warning: Freeing of pool %u is missing %d mbufs\n", pool, elements);
103 * This function fills a hardware pool with memory. Depending
104 * on the config defines, this memory might come from the
105 * kernel or global 32bit memory allocated with
106 * cvmx_bootmem_alloc.
108 * @param pool Pool to populate
109 * @param size Size of each buffer in the pool
110 * @param elements Number of buffers to allocate
112 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
115 int freed = elements;
117 if (USE_32BIT_SHARED) {
119 extern uint64_t octeon_reserve32_memory;
121 memory = cvmx_bootmem_alloc_range(elements*size, 128, octeon_reserve32_memory,
122 octeon_reserve32_memory + (CONFIG_CAVIUM_RESERVE32<<20) - 1);
124 panic("Unable to allocate %u bytes for FPA pool %d\n", elements*size, pool);
126 printf("Memory range %p - %p reserved for hardware\n", memory, memory + elements*size - 1);
129 cvmx_fpa_free(memory, pool, 0);
134 panic("%s: may need to implement using shared memory.", __func__);
138 /* We need to force alignment to 128 bytes here */
140 memory = kmalloc(size + 127, GFP_ATOMIC);
142 panic("%s: not yet implemented.", __func__);
144 if (__predict_false(memory == NULL)) {
145 printf("Unable to allocate %u bytes for FPA pool %d\n", elements*size, pool);
148 memory = (char *)(((unsigned long)memory+127) & -128);
149 cvmx_fpa_free(memory, pool, 0);
153 return (elements - freed);
158 * Free memory previously allocated with cvm_oct_fill_hw_memory
160 * @param pool FPA pool to free
161 * @param size Size of each buffer in the pool
162 * @param elements Number of buffers that should be in the pool
164 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
166 if (USE_32BIT_SHARED) {
167 printf("Warning: 32 shared memory is not freeable\n");
171 memory = cvmx_fpa_alloc(pool);
175 kfree(phys_to_virt(cvmx_ptr_to_phys(memory)));
177 panic("%s: not yet implemented.", __func__);
183 printf("Freeing of pool %u had too many buffers (%d)\n", pool, elements);
184 else if (elements > 0)
185 printf("Warning: Freeing of pool %u is missing %d buffers\n", pool, elements);
190 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
194 freed = cvm_oct_fill_hw_mbuf(pool, size, elements);
196 freed = cvm_oct_fill_hw_memory(pool, size, elements);
200 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
203 cvm_oct_free_hw_mbuf(pool, size, elements);
205 cvm_oct_free_hw_memory(pool, size, elements);