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