{net, ib}/mlx5: Make cache line size determination at runtime.
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / alloc.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/errno.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/export.h>
37 #include <linux/bitmap.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/vmalloc.h>
40 #include <linux/mlx5/driver.h>
41
42 #include "mlx5_core.h"
43
44 struct mlx5_db_pgdir {
45         struct list_head        list;
46         unsigned long          *bitmap;
47         __be32                 *db_page;
48         dma_addr_t              db_dma;
49 };
50
51 /* Handling for queue buffers -- we allocate a bunch of memory and
52  * register it in a memory region at HCA virtual address 0.
53  */
54
55 static void *mlx5_dma_zalloc_coherent_node(struct mlx5_core_dev *dev,
56                                            size_t size, dma_addr_t *dma_handle,
57                                            int node)
58 {
59         struct mlx5_priv *priv = &dev->priv;
60         int original_node;
61         void *cpu_handle;
62
63         mutex_lock(&priv->alloc_mutex);
64         original_node = dev_to_node(&dev->pdev->dev);
65         set_dev_node(&dev->pdev->dev, node);
66         cpu_handle = dma_zalloc_coherent(&dev->pdev->dev, size,
67                                          dma_handle, GFP_KERNEL);
68         set_dev_node(&dev->pdev->dev, original_node);
69         mutex_unlock(&priv->alloc_mutex);
70         return cpu_handle;
71 }
72
73 int mlx5_buf_alloc_node(struct mlx5_core_dev *dev, int size,
74                         struct mlx5_buf *buf, int node)
75 {
76         dma_addr_t t;
77
78         buf->size = size;
79         buf->npages       = 1;
80         buf->page_shift   = (u8)get_order(size) + PAGE_SHIFT;
81         buf->direct.buf   = mlx5_dma_zalloc_coherent_node(dev, size,
82                                                           &t, node);
83         if (!buf->direct.buf)
84                 return -ENOMEM;
85
86         buf->direct.map = t;
87
88         while (t & ((1 << buf->page_shift) - 1)) {
89                 --buf->page_shift;
90                 buf->npages *= 2;
91         }
92
93         return 0;
94 }
95
96 int mlx5_buf_alloc(struct mlx5_core_dev *dev, int size, struct mlx5_buf *buf)
97 {
98         return mlx5_buf_alloc_node(dev, size, buf, dev->priv.numa_node);
99 }
100 EXPORT_SYMBOL_GPL(mlx5_buf_alloc);
101
102 void mlx5_buf_free(struct mlx5_core_dev *dev, struct mlx5_buf *buf)
103 {
104         dma_free_coherent(&dev->pdev->dev, buf->size, buf->direct.buf,
105                           buf->direct.map);
106 }
107 EXPORT_SYMBOL_GPL(mlx5_buf_free);
108
109 static struct mlx5_db_pgdir *mlx5_alloc_db_pgdir(struct mlx5_core_dev *dev,
110                                                  int node)
111 {
112         u32 db_per_page = PAGE_SIZE / cache_line_size();
113         struct mlx5_db_pgdir *pgdir;
114
115         pgdir = kzalloc(sizeof(*pgdir), GFP_KERNEL);
116         if (!pgdir)
117                 return NULL;
118
119         pgdir->bitmap = kcalloc(BITS_TO_LONGS(db_per_page),
120                                 sizeof(unsigned long),
121                                 GFP_KERNEL);
122
123         if (!pgdir->bitmap) {
124                 kfree(pgdir);
125                 return NULL;
126         }
127
128         bitmap_fill(pgdir->bitmap, db_per_page);
129
130         pgdir->db_page = mlx5_dma_zalloc_coherent_node(dev, PAGE_SIZE,
131                                                        &pgdir->db_dma, node);
132         if (!pgdir->db_page) {
133                 kfree(pgdir->bitmap);
134                 kfree(pgdir);
135                 return NULL;
136         }
137
138         return pgdir;
139 }
140
141 static int mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir *pgdir,
142                                     struct mlx5_db *db)
143 {
144         u32 db_per_page = PAGE_SIZE / cache_line_size();
145         int offset;
146         int i;
147
148         i = find_first_bit(pgdir->bitmap, db_per_page);
149         if (i >= db_per_page)
150                 return -ENOMEM;
151
152         __clear_bit(i, pgdir->bitmap);
153
154         db->u.pgdir = pgdir;
155         db->index   = i;
156         offset = db->index * cache_line_size();
157         db->db      = pgdir->db_page + offset / sizeof(*pgdir->db_page);
158         db->dma     = pgdir->db_dma  + offset;
159
160         db->db[0] = 0;
161         db->db[1] = 0;
162
163         return 0;
164 }
165
166 int mlx5_db_alloc_node(struct mlx5_core_dev *dev, struct mlx5_db *db, int node)
167 {
168         struct mlx5_db_pgdir *pgdir;
169         int ret = 0;
170
171         mutex_lock(&dev->priv.pgdir_mutex);
172
173         list_for_each_entry(pgdir, &dev->priv.pgdir_list, list)
174                 if (!mlx5_alloc_db_from_pgdir(pgdir, db))
175                         goto out;
176
177         pgdir = mlx5_alloc_db_pgdir(dev, node);
178         if (!pgdir) {
179                 ret = -ENOMEM;
180                 goto out;
181         }
182
183         list_add(&pgdir->list, &dev->priv.pgdir_list);
184
185         /* This should never fail -- we just allocated an empty page: */
186         WARN_ON(mlx5_alloc_db_from_pgdir(pgdir, db));
187
188 out:
189         mutex_unlock(&dev->priv.pgdir_mutex);
190
191         return ret;
192 }
193 EXPORT_SYMBOL_GPL(mlx5_db_alloc_node);
194
195 int mlx5_db_alloc(struct mlx5_core_dev *dev, struct mlx5_db *db)
196 {
197         return mlx5_db_alloc_node(dev, db, dev->priv.numa_node);
198 }
199 EXPORT_SYMBOL_GPL(mlx5_db_alloc);
200
201 void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db)
202 {
203         u32 db_per_page = PAGE_SIZE / cache_line_size();
204         mutex_lock(&dev->priv.pgdir_mutex);
205
206         __set_bit(db->index, db->u.pgdir->bitmap);
207
208         if (bitmap_full(db->u.pgdir->bitmap, db_per_page)) {
209                 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
210                                   db->u.pgdir->db_page, db->u.pgdir->db_dma);
211                 list_del(&db->u.pgdir->list);
212                 kfree(db->u.pgdir->bitmap);
213                 kfree(db->u.pgdir);
214         }
215
216         mutex_unlock(&dev->priv.pgdir_mutex);
217 }
218 EXPORT_SYMBOL_GPL(mlx5_db_free);
219
220
221 void mlx5_fill_page_array(struct mlx5_buf *buf, __be64 *pas)
222 {
223         u64 addr;
224         int i;
225
226         for (i = 0; i < buf->npages; i++) {
227                 addr = buf->direct.map + (i << buf->page_shift);
228
229                 pas[i] = cpu_to_be64(addr);
230         }
231 }
232 EXPORT_SYMBOL_GPL(mlx5_fill_page_array);