7090521471b2014e749ee21098527cfab2657101
[deliverable/linux.git] / drivers / staging / octeon / ethernet-mem.c
1 /**********************************************************************
2 * Author: Cavium Networks
3 *
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
6 *
7 * Copyright (c) 2003-2007 Cavium Networks
8 *
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.
12 *
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
17 * details.
18 *
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/.
23 *
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
30 #include <asm/octeon/octeon.h>
31
32 #include "ethernet-defines.h"
33
34 #include "cvmx-fpa.h"
35
36 /**
37 * Fill the supplied hardware pool with skbuffs
38 *
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
42 */
43 static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
44 {
45 int freed = elements;
46 while (freed) {
47
48 struct sk_buff *skb = dev_alloc_skb(size + 128);
49 if (unlikely(skb == NULL)) {
50 pr_warning
51 ("Failed to allocate skb for hardware pool %d\n",
52 pool);
53 break;
54 }
55
56 skb_reserve(skb, 128 - (((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));
59 freed--;
60 }
61 return elements - freed;
62 }
63
64 /**
65 * Free the supplied hardware pool of skbuffs
66 *
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
70 */
71 static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
72 {
73 char *memory;
74
75 do {
76 memory = cvmx_fpa_alloc(pool);
77 if (memory) {
78 struct sk_buff *skb =
79 *(struct sk_buff **)(memory - sizeof(void *));
80 elements--;
81 dev_kfree_skb(skb);
82 }
83 } while (memory);
84
85 if (elements < 0)
86 pr_warning("Freeing of pool %u had too many skbuffs (%d)\n",
87 pool, elements);
88 else if (elements > 0)
89 pr_warning("Freeing of pool %u is missing %d skbuffs\n",
90 pool, elements);
91 }
92
93 /**
94 * This function fills a hardware pool with memory. Depending
95 * on the config defines, this memory might come from the
96 * kernel or global 32bit memory allocated with
97 * cvmx_bootmem_alloc.
98 *
99 * @pool: Pool to populate
100 * @size: Size of each buffer in the pool
101 * @elements: Number of buffers to allocate
102 */
103 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
104 {
105 char *memory;
106 int freed = elements;
107
108 while (freed) {
109 /* We need to force alignment to 128 bytes here */
110 memory = kmalloc(size + 127, GFP_ATOMIC);
111 if (unlikely(memory == NULL)) {
112 pr_warning("Unable to allocate %u bytes for FPA pool %d\n",
113 elements * size, pool);
114 break;
115 }
116 memory = (char *)(((unsigned long)memory + 127) & -128);
117 cvmx_fpa_free(memory, pool, 0);
118 freed--;
119 }
120 return elements - freed;
121 }
122
123 /**
124 * Free memory previously allocated with cvm_oct_fill_hw_memory
125 *
126 * @pool: FPA pool to free
127 * @size: Size of each buffer in the pool
128 * @elements: Number of buffers that should be in the pool
129 */
130 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
131 {
132 char *memory;
133 do {
134 memory = cvmx_fpa_alloc(pool);
135 if (memory) {
136 elements--;
137 kfree(phys_to_virt(cvmx_ptr_to_phys(memory)));
138 }
139 } while (memory);
140
141 if (elements < 0)
142 pr_warning("Freeing of pool %u had too many buffers (%d)\n",
143 pool, elements);
144 else if (elements > 0)
145 pr_warning("Warning: Freeing of pool %u is missing %d buffers\n",
146 pool, elements);
147 }
148
149 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
150 {
151 int freed;
152 if (USE_SKBUFFS_IN_HW)
153 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
154 else
155 freed = cvm_oct_fill_hw_memory(pool, size, elements);
156 return freed;
157 }
158
159 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
160 {
161 if (USE_SKBUFFS_IN_HW)
162 cvm_oct_free_hw_skbuff(pool, size, elements);
163 else
164 cvm_oct_free_hw_memory(pool, size, elements);
165 }
This page took 0.053668 seconds and 4 git commands to generate.