1 /**********************************************************************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2007 Cavium Networks
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 **********************************************************************/
27 #include <linux/kernel.h>
28 #include <linux/netdevice.h>
29 #include <linux/mii.h>
32 #include <asm/octeon/octeon.h>
34 #include "ethernet-defines.h"
39 * Fill the supplied hardware pool with skbuffs
41 * @pool: Pool to allocate an skbuff for
42 * @size: Size of the buffer needed for the pool
43 * @elements: Number of buffers to allocate
45 static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
50 struct sk_buff *skb = dev_alloc_skb(size + 128);
51 if (unlikely(skb == NULL)) {
53 ("Failed to allocate skb for hardware pool %d\n",
58 skb_reserve(skb, 128 - (((unsigned long)skb->data) & 0x7f));
59 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
60 cvmx_fpa_free(skb->data, pool, DONT_WRITEBACK(size / 128));
63 return elements - freed;
67 * Free the supplied hardware pool of skbuffs
69 * @pool: Pool to allocate an skbuff for
70 * @size: Size of the buffer needed for the pool
71 * @elements: Number of buffers to allocate
73 static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
78 memory = cvmx_fpa_alloc(pool);
81 *(struct sk_buff **)(memory - sizeof(void *));
88 pr_warning("Freeing of pool %u had too many skbuffs (%d)\n",
90 else if (elements > 0)
91 pr_warning("Freeing of pool %u is missing %d skbuffs\n",
96 * This function fills a hardware pool with memory. Depending
97 * on the config defines, this memory might come from the
98 * kernel or global 32bit memory allocated with
101 * @pool: Pool to populate
102 * @size: Size of each buffer in the pool
103 * @elements: Number of buffers to allocate
105 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
108 int freed = elements;
110 if (USE_32BIT_SHARED) {
111 extern uint64_t octeon_reserve32_memory;
114 cvmx_bootmem_alloc_range(elements * size, 128,
115 octeon_reserve32_memory,
116 octeon_reserve32_memory +
117 (CONFIG_CAVIUM_RESERVE32 << 20) -
120 panic("Unable to allocate %u bytes for FPA pool %d\n",
121 elements * size, pool);
123 pr_notice("Memory range %p - %p reserved for "
124 "hardware\n", memory,
125 memory + elements * size - 1);
128 cvmx_fpa_free(memory, pool, 0);
134 /* We need to force alignment to 128 bytes here */
135 memory = kmalloc(size + 127, GFP_ATOMIC);
136 if (unlikely(memory == NULL)) {
137 pr_warning("Unable to allocate %u bytes for "
139 elements * size, pool);
142 memory = (char *)(((unsigned long)memory + 127) & -128);
143 cvmx_fpa_free(memory, pool, 0);
147 return elements - freed;
151 * Free memory previously allocated with cvm_oct_fill_hw_memory
153 * @pool: FPA pool to free
154 * @size: Size of each buffer in the pool
155 * @elements: Number of buffers that should be in the pool
157 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
159 if (USE_32BIT_SHARED) {
160 pr_warning("Warning: 32 shared memory is not freeable\n");
164 memory = cvmx_fpa_alloc(pool);
167 kfree(phys_to_virt(cvmx_ptr_to_phys(memory)));
172 pr_warning("Freeing of pool %u had too many "
175 else if (elements > 0)
176 pr_warning("Warning: Freeing of pool %u is "
177 "missing %d buffers\n",
182 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
185 if (USE_SKBUFFS_IN_HW)
186 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
188 freed = cvm_oct_fill_hw_memory(pool, size, elements);
192 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
194 if (USE_SKBUFFS_IN_HW)
195 cvm_oct_free_hw_skbuff(pool, size, elements);
197 cvm_oct_free_hw_memory(pool, size, elements);