00cc91df6b46a2448d2da9040fa3ba196644b2a2
[linux-flexiantxendom0-natty.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-2010 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  * cvm_oct_fill_hw_skbuff - fill the supplied hardware pool with skbuffs
38  * @pool:     Pool to allocate an skbuff for
39  * @size:     Size of the buffer needed for the pool
40  * @elements: Number of buffers to allocate
41  *
42  * Returns the actual number of buffers allocated.
43  */
44 static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
45 {
46         int freed = elements;
47         while (freed) {
48
49                 struct sk_buff *skb = dev_alloc_skb(size + 256);
50                 if (unlikely(skb == NULL)) {
51                         pr_warning
52                             ("Failed to allocate skb for hardware pool %d\n",
53                              pool);
54                         break;
55                 }
56
57                 skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
58                 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
59                 cvmx_fpa_free(skb->data, pool, DONT_WRITEBACK(size / 128));
60                 freed--;
61         }
62         return elements - freed;
63 }
64
65 /**
66  * cvm_oct_free_hw_skbuff- free hardware pool 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
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  * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
95  * @pool:     Pool to populate
96  * @size:     Size of each buffer in the pool
97  * @elements: Number of buffers to allocate
98  *
99  * Returns the actual number of buffers allocated.
100  */
101 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
102 {
103         char *memory;
104         char *fpa;
105         int freed = elements;
106
107         while (freed) {
108                 /*
109                  * FPA memory must be 128 byte aligned.  Since we are
110                  * aligning we need to save the original pointer so we
111                  * can feed it to kfree when the memory is returned to
112                  * the kernel.
113                  *
114                  * We allocate an extra 256 bytes to allow for
115                  * alignment and space for the original pointer saved
116                  * just before the block.
117                  */
118                 memory = kmalloc(size + 256, GFP_ATOMIC);
119                 if (unlikely(memory == NULL)) {
120                         pr_warning("Unable to allocate %u bytes for FPA pool %d\n",
121                                    elements * size, pool);
122                         break;
123                 }
124                 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
125                 *((char **)fpa - 1) = memory;
126                 cvmx_fpa_free(fpa, pool, 0);
127                 freed--;
128         }
129         return elements - freed;
130 }
131
132 /**
133  * cvm_oct_free_hw_memory - Free memory allocated by 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
137  */
138 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
139 {
140         char *memory;
141         char *fpa;
142         do {
143                 fpa = cvmx_fpa_alloc(pool);
144                 if (fpa) {
145                         elements--;
146                         fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
147                         memory = *((char **)fpa - 1);
148                         kfree(memory);
149                 }
150         } while (fpa);
151
152         if (elements < 0)
153                 pr_warning("Freeing of pool %u had too many buffers (%d)\n",
154                         pool, elements);
155         else if (elements > 0)
156                 pr_warning("Warning: Freeing of pool %u is missing %d buffers\n",
157                         pool, elements);
158 }
159
160 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
161 {
162         int freed;
163         if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
164                 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
165         else
166                 freed = cvm_oct_fill_hw_memory(pool, size, elements);
167         return freed;
168 }
169
170 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
171 {
172         if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
173                 cvm_oct_free_hw_skbuff(pool, size, elements);
174         else
175                 cvm_oct_free_hw_memory(pool, size, elements);
176 }