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 "octeon-ethernet.h"
13 #include "ethernet-mem.h"
14 #include "ethernet-defines.h"
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
22 * Returns the actual number of buffers allocated.
24 static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
29 struct sk_buff *skb = dev_alloc_skb(size + 256);
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);
38 return elements - freed;
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
47 static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
52 memory = cvmx_fpa_alloc(pool);
55 *(struct sk_buff **)(memory - sizeof(void *));
62 pr_warn("Freeing of pool %u had too many skbuffs (%d)\n",
64 else if (elements > 0)
65 pr_warn("Freeing of pool %u is missing %d skbuffs\n",
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
75 * Returns the actual number of buffers allocated.
77 static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
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
90 * We allocate an extra 256 bytes to allow for
91 * alignment and space for the original pointer saved
92 * just before the block.
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);
100 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
101 *((char **)fpa - 1) = memory;
102 cvmx_fpa_free(fpa, pool, 0);
105 return elements - freed;
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
114 static void cvm_oct_free_hw_memory(int pool, int size, int elements)
120 fpa = cvmx_fpa_alloc(pool);
123 fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
124 memory = *((char **)fpa - 1);
130 pr_warn("Freeing of pool %u had too many buffers (%d)\n",
132 else if (elements > 0)
133 pr_warn("Warning: Freeing of pool %u is missing %d buffers\n",
137 int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
141 if (pool == CVMX_FPA_PACKET_POOL)
142 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
144 freed = cvm_oct_fill_hw_memory(pool, size, elements);
148 void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
150 if (pool == CVMX_FPA_PACKET_POOL)
151 cvm_oct_free_hw_skbuff(pool, size, elements);
153 cvm_oct_free_hw_memory(pool, size, elements);