1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/kernel.h>
28#include <linux/netdevice.h>
29#include <linux/slab.h>
30
31#include <asm/octeon/octeon.h>
32
33#include "ethernet-defines.h"
34
35#include <asm/octeon/cvmx-fpa.h>
36
37
38
39
40
41
42
43
44
45static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
46{
47 int freed = elements;
48 while (freed) {
49
50 struct sk_buff *skb = dev_alloc_skb(size + 256);
51 if (unlikely(skb == NULL)) {
52 pr_warning
53 ("Failed to allocate skb for hardware pool %d\n",
54 pool);
55 break;
56 }
57
58 skb_reserve(skb, 256 - (((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));
61 freed--;
62 }
63 return elements - freed;
64}
65
66
67
68
69
70
71
72static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
73{
74 char *memory;
75
76 do {
77 memory = cvmx_fpa_alloc(pool);
78 if (memory) {
79 struct sk_buff *skb =
80 *(struct sk_buff **)(memory - sizeof(void *));
81 elements--;
82 dev_kfree_skb(skb);
83 }
84 } while (memory);
85
86 if (elements < 0)
87 pr_warning("Freeing of pool %u had too many skbuffs (%d)\n",
88 pool, elements);
89 else if (elements > 0)
90 pr_warning("Freeing of pool %u is missing %d skbuffs\n",
91 pool, elements);
92}
93
94
95
96
97
98
99
100
101
102static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
103{
104 char *memory;
105 char *fpa;
106 int freed = elements;
107
108 while (freed) {
109
110
111
112
113
114
115
116
117
118
119 memory = kmalloc(size + 256, GFP_ATOMIC);
120 if (unlikely(memory == NULL)) {
121 pr_warning("Unable to allocate %u bytes for FPA pool %d\n",
122 elements * size, pool);
123 break;
124 }
125 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
126 *((char **)fpa - 1) = memory;
127 cvmx_fpa_free(fpa, pool, 0);
128 freed--;
129 }
130 return elements - freed;
131}
132
133
134
135
136
137
138
139static void cvm_oct_free_hw_memory(int pool, int size, int elements)
140{
141 char *memory;
142 char *fpa;
143 do {
144 fpa = cvmx_fpa_alloc(pool);
145 if (fpa) {
146 elements--;
147 fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
148 memory = *((char **)fpa - 1);
149 kfree(memory);
150 }
151 } while (fpa);
152
153 if (elements < 0)
154 pr_warning("Freeing of pool %u had too many buffers (%d)\n",
155 pool, elements);
156 else if (elements > 0)
157 pr_warning("Warning: Freeing of pool %u is missing %d buffers\n",
158 pool, elements);
159}
160
161int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
162{
163 int freed;
164 if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
165 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
166 else
167 freed = cvm_oct_fill_hw_memory(pool, size, elements);
168 return freed;
169}
170
171void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
172{
173 if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
174 cvm_oct_free_hw_skbuff(pool, size, elements);
175 else
176 cvm_oct_free_hw_memory(pool, size, elements);
177}
178