blob: 5a5cdb3cd740b2902905264b8600b819f0ee7bdc [file] [log] [blame]
Aaro Koskinen67620982015-04-04 22:51:21 +03001/*
2 * This file is based on code from OCTEON SDK by Cavium Networks.
David Daney80ff0fd2009-05-05 17:35:21 -07003 *
David Daney166bdaa2010-01-27 13:22:53 -08004 * Copyright (c) 2003-2010 Cavium Networks
David Daney80ff0fd2009-05-05 17:35:21 -07005 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, Version 2, as
8 * published by the Free Software Foundation.
Aaro Koskinen67620982015-04-04 22:51:21 +03009 */
10
David Daney80ff0fd2009-05-05 17:35:21 -070011#include <linux/kernel.h>
12#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Daney80ff0fd2009-05-05 17:35:21 -070014
15#include <asm/octeon/octeon.h>
16
Aaro Koskinen5c2f26d2014-03-02 00:09:09 +020017#include "ethernet-mem.h"
David Daney80ff0fd2009-05-05 17:35:21 -070018#include "ethernet-defines.h"
19
David Daneyaf866492011-11-22 14:47:00 +000020#include <asm/octeon/cvmx-fpa.h>
David Daney80ff0fd2009-05-05 17:35:21 -070021
22/**
David Daneyec977c52010-02-16 17:25:32 -080023 * cvm_oct_fill_hw_skbuff - fill the supplied hardware pool with skbuffs
David Daney80ff0fd2009-05-05 17:35:21 -070024 * @pool: Pool to allocate an skbuff for
25 * @size: Size of the buffer needed for the pool
26 * @elements: Number of buffers to allocate
David Daneyec977c52010-02-16 17:25:32 -080027 *
28 * Returns the actual number of buffers allocated.
David Daney80ff0fd2009-05-05 17:35:21 -070029 */
30static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
31{
32 int freed = elements;
David Daney80ff0fd2009-05-05 17:35:21 -070033
Nicolas Koch883d29e2014-06-03 21:56:31 +020034 while (freed) {
David Daney166bdaa2010-01-27 13:22:53 -080035 struct sk_buff *skb = dev_alloc_skb(size + 256);
Nicolas Koch883d29e2014-06-03 21:56:31 +020036
Aaro Koskinena5de43c2013-09-05 21:44:00 +030037 if (unlikely(skb == NULL))
David Daney80ff0fd2009-05-05 17:35:21 -070038 break;
David Daney166bdaa2010-01-27 13:22:53 -080039 skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
David Daney80ff0fd2009-05-05 17:35:21 -070040 *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
Aaro Koskinenc93b0e72015-04-04 22:51:19 +030041 cvmx_fpa_free(skb->data, pool, size / 128);
David Daney80ff0fd2009-05-05 17:35:21 -070042 freed--;
43 }
44 return elements - freed;
45}
46
47/**
David Daneyec977c52010-02-16 17:25:32 -080048 * cvm_oct_free_hw_skbuff- free hardware pool skbuffs
David Daney80ff0fd2009-05-05 17:35:21 -070049 * @pool: Pool to allocate an skbuff for
50 * @size: Size of the buffer needed for the pool
51 * @elements: Number of buffers to allocate
52 */
53static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
54{
55 char *memory;
56
57 do {
58 memory = cvmx_fpa_alloc(pool);
59 if (memory) {
60 struct sk_buff *skb =
61 *(struct sk_buff **)(memory - sizeof(void *));
62 elements--;
63 dev_kfree_skb(skb);
64 }
65 } while (memory);
66
67 if (elements < 0)
Himangi Saraogi8a125b02014-03-03 06:33:54 +053068 pr_warn("Freeing of pool %u had too many skbuffs (%d)\n",
Nicolas Koch883d29e2014-06-03 21:56:31 +020069 pool, elements);
David Daney80ff0fd2009-05-05 17:35:21 -070070 else if (elements > 0)
Himangi Saraogi8a125b02014-03-03 06:33:54 +053071 pr_warn("Freeing of pool %u is missing %d skbuffs\n",
Nicolas Koch883d29e2014-06-03 21:56:31 +020072 pool, elements);
David Daney80ff0fd2009-05-05 17:35:21 -070073}
74
75/**
David Daneyec977c52010-02-16 17:25:32 -080076 * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
David Daney80ff0fd2009-05-05 17:35:21 -070077 * @pool: Pool to populate
78 * @size: Size of each buffer in the pool
79 * @elements: Number of buffers to allocate
David Daneyec977c52010-02-16 17:25:32 -080080 *
81 * Returns the actual number of buffers allocated.
David Daney80ff0fd2009-05-05 17:35:21 -070082 */
83static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
84{
85 char *memory;
David Daney166bdaa2010-01-27 13:22:53 -080086 char *fpa;
David Daney80ff0fd2009-05-05 17:35:21 -070087 int freed = elements;
88
David Daney6568a232010-01-07 11:05:01 -080089 while (freed) {
David Daney166bdaa2010-01-27 13:22:53 -080090 /*
91 * FPA memory must be 128 byte aligned. Since we are
92 * aligning we need to save the original pointer so we
93 * can feed it to kfree when the memory is returned to
94 * the kernel.
95 *
96 * We allocate an extra 256 bytes to allow for
97 * alignment and space for the original pointer saved
98 * just before the block.
99 */
100 memory = kmalloc(size + 256, GFP_ATOMIC);
David Daney6568a232010-01-07 11:05:01 -0800101 if (unlikely(memory == NULL)) {
Himangi Saraogi8a125b02014-03-03 06:33:54 +0530102 pr_warn("Unable to allocate %u bytes for FPA pool %d\n",
Nicolas Koch883d29e2014-06-03 21:56:31 +0200103 elements * size, pool);
David Daney6568a232010-01-07 11:05:01 -0800104 break;
David Daney80ff0fd2009-05-05 17:35:21 -0700105 }
David Daney166bdaa2010-01-27 13:22:53 -0800106 fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
107 *((char **)fpa - 1) = memory;
108 cvmx_fpa_free(fpa, pool, 0);
David Daney6568a232010-01-07 11:05:01 -0800109 freed--;
David Daney80ff0fd2009-05-05 17:35:21 -0700110 }
111 return elements - freed;
112}
113
114/**
David Daneyec977c52010-02-16 17:25:32 -0800115 * cvm_oct_free_hw_memory - Free memory allocated by cvm_oct_fill_hw_memory
David Daney80ff0fd2009-05-05 17:35:21 -0700116 * @pool: FPA pool to free
117 * @size: Size of each buffer in the pool
118 * @elements: Number of buffers that should be in the pool
119 */
120static void cvm_oct_free_hw_memory(int pool, int size, int elements)
121{
David Daney6568a232010-01-07 11:05:01 -0800122 char *memory;
David Daney166bdaa2010-01-27 13:22:53 -0800123 char *fpa;
Nicolas Koch883d29e2014-06-03 21:56:31 +0200124
David Daney6568a232010-01-07 11:05:01 -0800125 do {
David Daney166bdaa2010-01-27 13:22:53 -0800126 fpa = cvmx_fpa_alloc(pool);
127 if (fpa) {
David Daney6568a232010-01-07 11:05:01 -0800128 elements--;
David Daney166bdaa2010-01-27 13:22:53 -0800129 fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
130 memory = *((char **)fpa - 1);
131 kfree(memory);
David Daney6568a232010-01-07 11:05:01 -0800132 }
David Daney166bdaa2010-01-27 13:22:53 -0800133 } while (fpa);
David Daney80ff0fd2009-05-05 17:35:21 -0700134
David Daney6568a232010-01-07 11:05:01 -0800135 if (elements < 0)
Himangi Saraogi8a125b02014-03-03 06:33:54 +0530136 pr_warn("Freeing of pool %u had too many buffers (%d)\n",
David Daney6568a232010-01-07 11:05:01 -0800137 pool, elements);
138 else if (elements > 0)
Himangi Saraogi8a125b02014-03-03 06:33:54 +0530139 pr_warn("Warning: Freeing of pool %u is missing %d buffers\n",
David Daney6568a232010-01-07 11:05:01 -0800140 pool, elements);
David Daney80ff0fd2009-05-05 17:35:21 -0700141}
142
143int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
144{
145 int freed;
Nicolas Koch883d29e2014-06-03 21:56:31 +0200146
Aaro Koskinen3a990f32015-04-04 22:51:17 +0300147 if (pool == CVMX_FPA_PACKET_POOL)
David Daney80ff0fd2009-05-05 17:35:21 -0700148 freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
149 else
150 freed = cvm_oct_fill_hw_memory(pool, size, elements);
151 return freed;
152}
153
154void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
155{
Aaro Koskinen3a990f32015-04-04 22:51:17 +0300156 if (pool == CVMX_FPA_PACKET_POOL)
David Daney80ff0fd2009-05-05 17:35:21 -0700157 cvm_oct_free_hw_skbuff(pool, size, elements);
158 else
159 cvm_oct_free_hw_memory(pool, size, elements);
160}