1 /**********************************************************************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2010 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>
30 #include <asm/octeon/octeon.h>
32 #include "ethernet-defines.h"
37 * Fill the supplied hardware pool with skbuffs
39 * @pool: Pool to allocate an skbuff for
40 * @size: Size of the buffer needed for the pool
41 * @elements: Number of buffers to allocate
43 static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
48 struct sk_buff *skb = dev_alloc_skb(size + 256);
49 if (unlikely(skb == NULL)) {
51 ("Failed to allocate skb for hardware pool %d\n",
56 skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
57 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
58 cvmx_fpa_free(skb->data, pool, DONT_WRITEBACK(size / 128));
61 return elements - freed;
65 * Free the supplied hardware pool of skbuffs
67 * @pool: Pool to allocate an skbuff for
68 * @size: Size of the buffer needed for the pool
69 * @elements: Number of buffers to allocate
71 static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
76 memory = cvmx_fpa_alloc(pool);
79 *(struct sk_buff **)(memory - sizeof(void *));
86 pr_warning("Freeing of pool %u had too many skbuffs (%d)\n",
88 else if (elements > 0)
89 pr_warning("Freeing of pool %u is missing %d skbuffs\n",
94 * This function fills a hardware pool with memory.
96 * @pool: Pool to populate
97 * @size: Size of each buffer in the pool
98 * @elements: Number of buffers to allocate
100 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
104 int freed = elements;
108 * FPA memory must be 128 byte aligned. Since we are
109 * aligning we need to save the original pointer so we
110 * can feed it to kfree when the memory is returned to
113 * We allocate an extra 256 bytes to allow for
114 * alignment and space for the original pointer saved
115 * just before the block.
117 memory = kmalloc(size + 256, GFP_ATOMIC);
118 if (unlikely(memory == NULL)) {
119 pr_warning("Unable to allocate %u bytes for FPA pool %d\n",
120 elements * size, pool);
123 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
124 *((char **)fpa - 1) = memory;
125 cvmx_fpa_free(fpa, pool, 0);
128 return elements - freed;
132 * Free memory previously allocated with cvm_oct_fill_hw_memory
134 * @pool: FPA pool to free
135 * @size: Size of each buffer in the pool
136 * @elements: Number of buffers that should be in the pool
138 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
143 fpa = cvmx_fpa_alloc(pool);
146 fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
147 memory = *((char **)fpa - 1);
153 pr_warning("Freeing of pool %u had too many buffers (%d)\n",
155 else if (elements > 0)
156 pr_warning("Warning: Freeing of pool %u is missing %d buffers\n",
160 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
163 if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
164 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
166 freed = cvm_oct_fill_hw_memory(pool, size, elements);
170 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
172 if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
173 cvm_oct_free_hw_skbuff(pool, size, elements);
175 cvm_oct_free_hw_memory(pool, size, elements);