1 /*************************************************************************
2 Copyright (c) 2003-2007 Cavium Networks (support@cavium.com). All rights
3 reserved.
4
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12
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.
17
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
21 permission.
22
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.
24
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.
27
28 *************************************************************************/
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: releng/8.4/sys/mips/cavium/octe/ethernet-mem.c 210311 2010-07-20 19:25:11Z jmallett $");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40
41 #include <net/ethernet.h>
42 #include <net/if.h>
43
44 #include "wrapper-cvmx-includes.h"
45 #include "ethernet-headers.h"
46
47 /**
48 * Fill the supplied hardware pool with mbufs
49 *
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
53 */
54 static int cvm_oct_fill_hw_mbuf(int pool, int size, int elements)
55 {
56 int freed = elements;
57 while (freed) {
58 KASSERT(size <= MCLBYTES - 128, ("mbuf clusters are too small"));
59
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);
63 break;
64 }
65
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));
69 freed--;
70 }
71 return (elements - freed);
72 }
73
74
75 /**
76 * Free the supplied hardware pool of mbufs
77 *
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
81 */
82 static void cvm_oct_free_hw_mbuf(int pool, int size, int elements)
83 {
84 char *memory;
85
86 do {
87 memory = cvmx_fpa_alloc(pool);
88 if (memory) {
89 struct mbuf *m = *(struct mbuf **)(memory - sizeof(void *));
90 elements--;
91 m_freem(m);
92 }
93 } while (memory);
94
95 if (elements < 0)
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);
99 }
100
101
102 /**
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.
107 *
108 * @param pool Pool to populate
109 * @param size Size of each buffer in the pool
110 * @param elements Number of buffers to allocate
111 */
112 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
113 {
114 char *memory;
115 int freed = elements;
116
117 if (USE_32BIT_SHARED) {
118 #if 0
119 extern uint64_t octeon_reserve32_memory;
120
121 memory = cvmx_bootmem_alloc_range(elements*size, 128, octeon_reserve32_memory,
122 octeon_reserve32_memory + (CONFIG_CAVIUM_RESERVE32<<20) - 1);
123 if (memory == NULL)
124 panic("Unable to allocate %u bytes for FPA pool %d\n", elements*size, pool);
125
126 printf("Memory range %p - %p reserved for hardware\n", memory, memory + elements*size - 1);
127
128 while (freed) {
129 cvmx_fpa_free(memory, pool, 0);
130 memory += size;
131 freed--;
132 }
133 #else
134 panic("%s: may need to implement using shared memory.", __func__);
135 #endif
136 } else {
137 while (freed) {
138 /* We need to force alignment to 128 bytes here */
139 #if 0
140 memory = kmalloc(size + 127, GFP_ATOMIC);
141 #else
142 panic("%s: not yet implemented.", __func__);
143 #endif
144 if (__predict_false(memory == NULL)) {
145 printf("Unable to allocate %u bytes for FPA pool %d\n", elements*size, pool);
146 break;
147 }
148 memory = (char *)(((unsigned long)memory+127) & -128);
149 cvmx_fpa_free(memory, pool, 0);
150 freed--;
151 }
152 }
153 return (elements - freed);
154 }
155
156
157 /**
158 * Free memory previously allocated with cvm_oct_fill_hw_memory
159 *
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
163 */
164 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
165 {
166 if (USE_32BIT_SHARED) {
167 printf("Warning: 32 shared memory is not freeable\n");
168 } else {
169 char *memory;
170 do {
171 memory = cvmx_fpa_alloc(pool);
172 if (memory) {
173 elements--;
174 #if 0
175 kfree(phys_to_virt(cvmx_ptr_to_phys(memory)));
176 #else
177 panic("%s: not yet implemented.", __func__);
178 #endif
179 }
180 } while (memory);
181
182 if (elements < 0)
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);
186 }
187 }
188
189
190 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
191 {
192 int freed;
193 if (USE_MBUFS_IN_HW)
194 freed = cvm_oct_fill_hw_mbuf(pool, size, elements);
195 else
196 freed = cvm_oct_fill_hw_memory(pool, size, elements);
197 return (freed);
198 }
199
200 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
201 {
202 if (USE_MBUFS_IN_HW)
203 cvm_oct_free_hw_mbuf(pool, size, elements);
204 else
205 cvm_oct_free_hw_memory(pool, size, elements);
206 }
207
Cache object: 0de5dbef7fa5bc0a1843989ff14e9302
|