1 // SPDX-License-Identifier: GPL-2.0
3 * This file is based on code from OCTEON SDK by Cavium Networks.
5 * Copyright (c) 2003-2010 Cavium Networks
8 #include <linux/kernel.h>
9 #include <linux/netdevice.h>
10 #include <linux/slab.h>
12 #include <asm/octeon/octeon.h>
14 #include "ethernet-mem.h"
15 #include "ethernet-defines.h"
17 #include <asm/octeon/cvmx-fpa.h>
20 * cvm_oct_fill_hw_skbuff - fill the supplied hardware pool with skbuffs
21 * @pool: Pool to allocate an skbuff for
22 * @size: Size of the buffer needed for the pool
23 * @elements: Number of buffers to allocate
25 * Returns the actual number of buffers allocated.
27 static int cvm_oct_fill_hw_skbuff(int pool
, int size
, int elements
)
32 struct sk_buff
*skb
= dev_alloc_skb(size
+ 256);
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);
41 return elements
- freed
;
45 * cvm_oct_free_hw_skbuff- free hardware pool skbuffs
46 * @pool: Pool to allocate an skbuff for
47 * @size: Size of the buffer needed for the pool
48 * @elements: Number of buffers to allocate
50 static void cvm_oct_free_hw_skbuff(int pool
, int size
, int elements
)
55 memory
= cvmx_fpa_alloc(pool
);
58 *(struct sk_buff
**)(memory
- sizeof(void *));
65 pr_warn("Freeing of pool %u had too many skbuffs (%d)\n",
67 else if (elements
> 0)
68 pr_warn("Freeing of pool %u is missing %d skbuffs\n",
73 * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
74 * @pool: Pool to populate
75 * @size: Size of each buffer in the pool
76 * @elements: Number of buffers to allocate
78 * Returns the actual number of buffers allocated.
80 static int cvm_oct_fill_hw_memory(int pool
, int size
, int elements
)
88 * FPA memory must be 128 byte aligned. Since we are
89 * aligning we need to save the original pointer so we
90 * can feed it to kfree when the memory is returned to
93 * We allocate an extra 256 bytes to allow for
94 * alignment and space for the original pointer saved
95 * just before the block.
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
);
103 fpa
= (char *)(((unsigned long)memory
+ 256) & ~0x7fUL
);
104 *((char **)fpa
- 1) = memory
;
105 cvmx_fpa_free(fpa
, pool
, 0);
108 return elements
- freed
;
112 * cvm_oct_free_hw_memory - Free memory allocated by cvm_oct_fill_hw_memory
113 * @pool: FPA pool to free
114 * @size: Size of each buffer in the pool
115 * @elements: Number of buffers that should be in the pool
117 static void cvm_oct_free_hw_memory(int pool
, int size
, int elements
)
123 fpa
= cvmx_fpa_alloc(pool
);
126 fpa
= (char *)phys_to_virt(cvmx_ptr_to_phys(fpa
));
127 memory
= *((char **)fpa
- 1);
133 pr_warn("Freeing of pool %u had too many buffers (%d)\n",
135 else if (elements
> 0)
136 pr_warn("Warning: Freeing of pool %u is missing %d buffers\n",
140 int cvm_oct_mem_fill_fpa(int pool
, int size
, int elements
)
144 if (pool
== CVMX_FPA_PACKET_POOL
)
145 freed
= cvm_oct_fill_hw_skbuff(pool
, size
, elements
);
147 freed
= cvm_oct_fill_hw_memory(pool
, size
, elements
);
151 void cvm_oct_mem_empty_fpa(int pool
, int size
, int elements
)
153 if (pool
== CVMX_FPA_PACKET_POOL
)
154 cvm_oct_free_hw_skbuff(pool
, size
, elements
);
156 cvm_oct_free_hw_memory(pool
, size
, elements
);