Loading...
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-mem.h"
34#include "ethernet-defines.h"
35
36#include <asm/octeon/cvmx-fpa.h>
37
38/**
39 * cvm_oct_fill_hw_skbuff - fill the supplied hardware pool with skbuffs
40 * @pool: Pool to allocate an skbuff for
41 * @size: Size of the buffer needed for the pool
42 * @elements: Number of buffers to allocate
43 *
44 * Returns the actual number of buffers allocated.
45 */
46static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
47{
48 int freed = elements;
49 while (freed) {
50
51 struct sk_buff *skb = dev_alloc_skb(size + 256);
52 if (unlikely(skb == NULL))
53 break;
54 skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
55 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
56 cvmx_fpa_free(skb->data, pool, DONT_WRITEBACK(size / 128));
57 freed--;
58 }
59 return elements - freed;
60}
61
62/**
63 * cvm_oct_free_hw_skbuff- free hardware pool skbuffs
64 * @pool: Pool to allocate an skbuff for
65 * @size: Size of the buffer needed for the pool
66 * @elements: Number of buffers to allocate
67 */
68static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
69{
70 char *memory;
71
72 do {
73 memory = cvmx_fpa_alloc(pool);
74 if (memory) {
75 struct sk_buff *skb =
76 *(struct sk_buff **)(memory - sizeof(void *));
77 elements--;
78 dev_kfree_skb(skb);
79 }
80 } while (memory);
81
82 if (elements < 0)
83 pr_warn("Freeing of pool %u had too many skbuffs (%d)\n",
84 pool, elements);
85 else if (elements > 0)
86 pr_warn("Freeing of pool %u is missing %d skbuffs\n",
87 pool, elements);
88}
89
90/**
91 * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
92 * @pool: Pool to populate
93 * @size: Size of each buffer in the pool
94 * @elements: Number of buffers to allocate
95 *
96 * Returns the actual number of buffers allocated.
97 */
98static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
99{
100 char *memory;
101 char *fpa;
102 int freed = elements;
103
104 while (freed) {
105 /*
106 * FPA memory must be 128 byte aligned. Since we are
107 * aligning we need to save the original pointer so we
108 * can feed it to kfree when the memory is returned to
109 * the kernel.
110 *
111 * We allocate an extra 256 bytes to allow for
112 * alignment and space for the original pointer saved
113 * just before the block.
114 */
115 memory = kmalloc(size + 256, GFP_ATOMIC);
116 if (unlikely(memory == NULL)) {
117 pr_warn("Unable to allocate %u bytes for FPA pool %d\n",
118 elements * size, pool);
119 break;
120 }
121 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
122 *((char **)fpa - 1) = memory;
123 cvmx_fpa_free(fpa, pool, 0);
124 freed--;
125 }
126 return elements - freed;
127}
128
129/**
130 * cvm_oct_free_hw_memory - Free memory allocated by cvm_oct_fill_hw_memory
131 * @pool: FPA pool to free
132 * @size: Size of each buffer in the pool
133 * @elements: Number of buffers that should be in the pool
134 */
135static void cvm_oct_free_hw_memory(int pool, int size, int elements)
136{
137 char *memory;
138 char *fpa;
139 do {
140 fpa = cvmx_fpa_alloc(pool);
141 if (fpa) {
142 elements--;
143 fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
144 memory = *((char **)fpa - 1);
145 kfree(memory);
146 }
147 } while (fpa);
148
149 if (elements < 0)
150 pr_warn("Freeing of pool %u had too many buffers (%d)\n",
151 pool, elements);
152 else if (elements > 0)
153 pr_warn("Warning: Freeing of pool %u is missing %d buffers\n",
154 pool, elements);
155}
156
157int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
158{
159 int freed;
160 if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
161 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
162 else
163 freed = cvm_oct_fill_hw_memory(pool, size, elements);
164 return freed;
165}
166
167void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
168{
169 if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
170 cvm_oct_free_hw_skbuff(pool, size, elements);
171 else
172 cvm_oct_free_hw_memory(pool, size, elements);
173}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file is based on code from OCTEON SDK by Cavium Networks.
4 *
5 * Copyright (c) 2003-2010 Cavium Networks
6 */
7
8#include <linux/kernel.h>
9#include <linux/netdevice.h>
10#include <linux/slab.h>
11
12#include "octeon-ethernet.h"
13#include "ethernet-mem.h"
14#include "ethernet-defines.h"
15
16/**
17 * cvm_oct_fill_hw_skbuff - fill the supplied hardware pool with skbuffs
18 * @pool: Pool to allocate an skbuff for
19 * @size: Size of the buffer needed for the pool
20 * @elements: Number of buffers to allocate
21 *
22 * Returns the actual number of buffers allocated.
23 */
24static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
25{
26 int freed = elements;
27
28 while (freed) {
29 struct sk_buff *skb = dev_alloc_skb(size + 256);
30
31 if (unlikely(!skb))
32 break;
33 skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
34 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
35 cvmx_fpa_free(skb->data, pool, size / 128);
36 freed--;
37 }
38 return elements - freed;
39}
40
41/**
42 * cvm_oct_free_hw_skbuff- free hardware pool skbuffs
43 * @pool: Pool to allocate an skbuff for
44 * @size: Size of the buffer needed for the pool
45 * @elements: Number of buffers to allocate
46 */
47static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
48{
49 char *memory;
50
51 do {
52 memory = cvmx_fpa_alloc(pool);
53 if (memory) {
54 struct sk_buff *skb =
55 *(struct sk_buff **)(memory - sizeof(void *));
56 elements--;
57 dev_kfree_skb(skb);
58 }
59 } while (memory);
60
61 if (elements < 0)
62 pr_warn("Freeing of pool %u had too many skbuffs (%d)\n",
63 pool, elements);
64 else if (elements > 0)
65 pr_warn("Freeing of pool %u is missing %d skbuffs\n",
66 pool, elements);
67}
68
69/**
70 * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
71 * @pool: Pool to populate
72 * @size: Size of each buffer in the pool
73 * @elements: Number of buffers to allocate
74 *
75 * Returns the actual number of buffers allocated.
76 */
77static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
78{
79 char *memory;
80 char *fpa;
81 int freed = elements;
82
83 while (freed) {
84 /*
85 * FPA memory must be 128 byte aligned. Since we are
86 * aligning we need to save the original pointer so we
87 * can feed it to kfree when the memory is returned to
88 * the kernel.
89 *
90 * We allocate an extra 256 bytes to allow for
91 * alignment and space for the original pointer saved
92 * just before the block.
93 */
94 memory = kmalloc(size + 256, GFP_ATOMIC);
95 if (unlikely(!memory)) {
96 pr_warn("Unable to allocate %u bytes for FPA pool %d\n",
97 elements * size, pool);
98 break;
99 }
100 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
101 *((char **)fpa - 1) = memory;
102 cvmx_fpa_free(fpa, pool, 0);
103 freed--;
104 }
105 return elements - freed;
106}
107
108/**
109 * cvm_oct_free_hw_memory - Free memory allocated by cvm_oct_fill_hw_memory
110 * @pool: FPA pool to free
111 * @size: Size of each buffer in the pool
112 * @elements: Number of buffers that should be in the pool
113 */
114static void cvm_oct_free_hw_memory(int pool, int size, int elements)
115{
116 char *memory;
117 char *fpa;
118
119 do {
120 fpa = cvmx_fpa_alloc(pool);
121 if (fpa) {
122 elements--;
123 fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
124 memory = *((char **)fpa - 1);
125 kfree(memory);
126 }
127 } while (fpa);
128
129 if (elements < 0)
130 pr_warn("Freeing of pool %u had too many buffers (%d)\n",
131 pool, elements);
132 else if (elements > 0)
133 pr_warn("Warning: Freeing of pool %u is missing %d buffers\n",
134 pool, elements);
135}
136
137int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
138{
139 int freed;
140
141 if (pool == CVMX_FPA_PACKET_POOL)
142 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
143 else
144 freed = cvm_oct_fill_hw_memory(pool, size, elements);
145 return freed;
146}
147
148void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
149{
150 if (pool == CVMX_FPA_PACKET_POOL)
151 cvm_oct_free_hw_skbuff(pool, size, elements);
152 else
153 cvm_oct_free_hw_memory(pool, size, elements);
154}