]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/octeon/ethernet-mem.c
Merge remote-tracking branch 'arm-current/fixes'
[karo-tx-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-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 #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  * cvm_oct_fill_hw_skbuff - 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
42  *
43  * Returns the actual number of buffers allocated.
44  */
45 static 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                         break;
53                 skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
54                 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
55                 cvmx_fpa_free(skb->data, pool, DONT_WRITEBACK(size / 128));
56                 freed--;
57         }
58         return elements - freed;
59 }
60
61 /**
62  * cvm_oct_free_hw_skbuff- free hardware pool skbuffs
63  * @pool:     Pool to allocate an skbuff for
64  * @size:     Size of the buffer needed for the pool
65  * @elements: Number of buffers to allocate
66  */
67 static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
68 {
69         char *memory;
70
71         do {
72                 memory = cvmx_fpa_alloc(pool);
73                 if (memory) {
74                         struct sk_buff *skb =
75                             *(struct sk_buff **)(memory - sizeof(void *));
76                         elements--;
77                         dev_kfree_skb(skb);
78                 }
79         } while (memory);
80
81         if (elements < 0)
82                 pr_warning("Freeing of pool %u had too many skbuffs (%d)\n",
83                      pool, elements);
84         else if (elements > 0)
85                 pr_warning("Freeing of pool %u is missing %d skbuffs\n",
86                        pool, elements);
87 }
88
89 /**
90  * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
91  * @pool:     Pool to populate
92  * @size:     Size of each buffer in the pool
93  * @elements: Number of buffers to allocate
94  *
95  * Returns the actual number of buffers allocated.
96  */
97 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
98 {
99         char *memory;
100         char *fpa;
101         int freed = elements;
102
103         while (freed) {
104                 /*
105                  * FPA memory must be 128 byte aligned.  Since we are
106                  * aligning we need to save the original pointer so we
107                  * can feed it to kfree when the memory is returned to
108                  * the kernel.
109                  *
110                  * We allocate an extra 256 bytes to allow for
111                  * alignment and space for the original pointer saved
112                  * just before the block.
113                  */
114                 memory = kmalloc(size + 256, GFP_ATOMIC);
115                 if (unlikely(memory == NULL)) {
116                         pr_warning("Unable to allocate %u bytes for FPA pool %d\n",
117                                    elements * size, pool);
118                         break;
119                 }
120                 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
121                 *((char **)fpa - 1) = memory;
122                 cvmx_fpa_free(fpa, pool, 0);
123                 freed--;
124         }
125         return elements - freed;
126 }
127
128 /**
129  * cvm_oct_free_hw_memory - Free memory allocated by cvm_oct_fill_hw_memory
130  * @pool:     FPA pool to free
131  * @size:     Size of each buffer in the pool
132  * @elements: Number of buffers that should be in the pool
133  */
134 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
135 {
136         char *memory;
137         char *fpa;
138         do {
139                 fpa = cvmx_fpa_alloc(pool);
140                 if (fpa) {
141                         elements--;
142                         fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
143                         memory = *((char **)fpa - 1);
144                         kfree(memory);
145                 }
146         } while (fpa);
147
148         if (elements < 0)
149                 pr_warning("Freeing of pool %u had too many buffers (%d)\n",
150                         pool, elements);
151         else if (elements > 0)
152                 pr_warning("Warning: Freeing of pool %u is missing %d buffers\n",
153                         pool, elements);
154 }
155
156 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
157 {
158         int freed;
159         if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
160                 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
161         else
162                 freed = cvm_oct_fill_hw_memory(pool, size, elements);
163         return freed;
164 }
165
166 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
167 {
168         if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
169                 cvm_oct_free_hw_skbuff(pool, size, elements);
170         else
171                 cvm_oct_free_hw_memory(pool, size, elements);
172 }