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