1 /*
2 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34 #include <linux/errno.h>
35 #include <linux/mm.h>
36 #include <linux/scatterlist.h>
37 #include <linux/slab.h>
38
39 #include <linux/mlx4/cmd.h>
40
41 #include "mlx4.h"
42 #include "icm.h"
43 #include "fw.h"
44
45 /*
46 * We allocate in as big chunks as we can, up to a maximum of 256 KB
47 * per chunk. Note that the chunks are not necessarily in contiguous
48 * physical memory.
49 */
50 enum {
51 MLX4_ICM_ALLOC_SIZE = 1 << 18,
52 MLX4_TABLE_CHUNK_SIZE = 1 << 18,
53 };
54
mlx4_free_icm_pages(struct mlx4_dev * dev,struct mlx4_icm_chunk * chunk)55 static void mlx4_free_icm_pages(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
56 {
57 int i;
58
59 if (chunk->nsg > 0)
60 dma_unmap_sg(&dev->persist->pdev->dev, chunk->sg, chunk->npages,
61 DMA_BIDIRECTIONAL);
62
63 for (i = 0; i < chunk->npages; ++i)
64 __free_pages(sg_page(&chunk->sg[i]),
65 get_order(chunk->sg[i].length));
66 }
67
mlx4_free_icm_coherent(struct mlx4_dev * dev,struct mlx4_icm_chunk * chunk)68 static void mlx4_free_icm_coherent(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
69 {
70 int i;
71
72 for (i = 0; i < chunk->npages; ++i)
73 dma_free_coherent(&dev->persist->pdev->dev,
74 chunk->buf[i].size,
75 chunk->buf[i].addr,
76 chunk->buf[i].dma_addr);
77 }
78
mlx4_free_icm(struct mlx4_dev * dev,struct mlx4_icm * icm,int coherent)79 void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent)
80 {
81 struct mlx4_icm_chunk *chunk, *tmp;
82
83 if (!icm)
84 return;
85
86 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
87 if (coherent)
88 mlx4_free_icm_coherent(dev, chunk);
89 else
90 mlx4_free_icm_pages(dev, chunk);
91
92 kfree(chunk);
93 }
94
95 kfree(icm);
96 }
97
mlx4_alloc_icm_pages(struct scatterlist * mem,int order,gfp_t gfp_mask,int node)98 static int mlx4_alloc_icm_pages(struct scatterlist *mem, int order,
99 gfp_t gfp_mask, int node)
100 {
101 struct page *page;
102
103 page = alloc_pages_node(node, gfp_mask, order);
104 if (!page) {
105 page = alloc_pages(gfp_mask, order);
106 if (!page)
107 return -ENOMEM;
108 }
109
110 sg_set_page(mem, page, PAGE_SIZE << order, 0);
111 return 0;
112 }
113
mlx4_alloc_icm_coherent(struct device * dev,struct mlx4_icm_buf * buf,int order,gfp_t gfp_mask)114 static int mlx4_alloc_icm_coherent(struct device *dev, struct mlx4_icm_buf *buf,
115 int order, gfp_t gfp_mask)
116 {
117 buf->addr = dma_alloc_coherent(dev, PAGE_SIZE << order,
118 &buf->dma_addr, gfp_mask);
119 if (!buf->addr)
120 return -ENOMEM;
121
122 if (offset_in_page(buf->addr)) {
123 dma_free_coherent(dev, PAGE_SIZE << order, buf->addr,
124 buf->dma_addr);
125 return -ENOMEM;
126 }
127
128 buf->size = PAGE_SIZE << order;
129 return 0;
130 }
131
mlx4_alloc_icm(struct mlx4_dev * dev,int npages,gfp_t gfp_mask,int coherent)132 struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
133 gfp_t gfp_mask, int coherent)
134 {
135 struct mlx4_icm *icm;
136 struct mlx4_icm_chunk *chunk = NULL;
137 int cur_order;
138 gfp_t mask;
139 int ret;
140
141 /* We use sg_set_buf for coherent allocs, which assumes low memory */
142 BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
143
144 icm = kmalloc_node(sizeof(*icm),
145 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN),
146 dev->numa_node);
147 if (!icm) {
148 icm = kmalloc_obj(*icm,
149 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
150 if (!icm)
151 return NULL;
152 }
153
154 icm->refcount = 0;
155 INIT_LIST_HEAD(&icm->chunk_list);
156
157 cur_order = get_order(MLX4_ICM_ALLOC_SIZE);
158
159 while (npages > 0) {
160 if (!chunk) {
161 chunk = kzalloc_node(sizeof(*chunk),
162 gfp_mask & ~(__GFP_HIGHMEM |
163 __GFP_NOWARN),
164 dev->numa_node);
165 if (!chunk) {
166 chunk = kzalloc_obj(*chunk,
167 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
168 if (!chunk)
169 goto fail;
170 }
171 chunk->coherent = coherent;
172
173 if (!coherent)
174 sg_init_table(chunk->sg, MLX4_ICM_CHUNK_LEN);
175 list_add_tail(&chunk->list, &icm->chunk_list);
176 }
177
178 while (1 << cur_order > npages)
179 --cur_order;
180
181 mask = gfp_mask;
182 if (cur_order)
183 mask &= ~__GFP_DIRECT_RECLAIM;
184
185 if (coherent)
186 ret = mlx4_alloc_icm_coherent(&dev->persist->pdev->dev,
187 &chunk->buf[chunk->npages],
188 cur_order, mask);
189 else
190 ret = mlx4_alloc_icm_pages(&chunk->sg[chunk->npages],
191 cur_order, mask,
192 dev->numa_node);
193
194 if (ret) {
195 if (--cur_order < 0)
196 goto fail;
197 else
198 continue;
199 }
200
201 ++chunk->npages;
202
203 if (coherent)
204 ++chunk->nsg;
205 else if (chunk->npages == MLX4_ICM_CHUNK_LEN) {
206 chunk->nsg = dma_map_sg(&dev->persist->pdev->dev,
207 chunk->sg, chunk->npages,
208 DMA_BIDIRECTIONAL);
209
210 if (!chunk->nsg)
211 goto fail;
212 }
213
214 if (chunk->npages == MLX4_ICM_CHUNK_LEN)
215 chunk = NULL;
216
217 npages -= 1 << cur_order;
218 }
219
220 if (!coherent && chunk) {
221 chunk->nsg = dma_map_sg(&dev->persist->pdev->dev, chunk->sg,
222 chunk->npages, DMA_BIDIRECTIONAL);
223
224 if (!chunk->nsg)
225 goto fail;
226 }
227
228 return icm;
229
230 fail:
231 mlx4_free_icm(dev, icm, coherent);
232 return NULL;
233 }
234
mlx4_MAP_ICM(struct mlx4_dev * dev,struct mlx4_icm * icm,u64 virt)235 static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm, u64 virt)
236 {
237 return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt);
238 }
239
mlx4_UNMAP_ICM(struct mlx4_dev * dev,u64 virt,u32 page_count)240 static int mlx4_UNMAP_ICM(struct mlx4_dev *dev, u64 virt, u32 page_count)
241 {
242 return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM,
243 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
244 }
245
mlx4_MAP_ICM_AUX(struct mlx4_dev * dev,struct mlx4_icm * icm)246 int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm)
247 {
248 return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1);
249 }
250
mlx4_UNMAP_ICM_AUX(struct mlx4_dev * dev)251 int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev)
252 {
253 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX,
254 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
255 }
256
mlx4_table_get(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 obj)257 int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj)
258 {
259 u32 i = (obj & (table->num_obj - 1)) /
260 (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
261 int ret = 0;
262
263 mutex_lock(&table->mutex);
264
265 if (table->icm[i]) {
266 ++table->icm[i]->refcount;
267 goto out;
268 }
269
270 table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
271 (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
272 __GFP_NOWARN, table->coherent);
273 if (!table->icm[i]) {
274 ret = -ENOMEM;
275 goto out;
276 }
277
278 if (mlx4_MAP_ICM(dev, table->icm[i], table->virt +
279 (u64) i * MLX4_TABLE_CHUNK_SIZE)) {
280 mlx4_free_icm(dev, table->icm[i], table->coherent);
281 table->icm[i] = NULL;
282 ret = -ENOMEM;
283 goto out;
284 }
285
286 ++table->icm[i]->refcount;
287
288 out:
289 mutex_unlock(&table->mutex);
290 return ret;
291 }
292
mlx4_table_put(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 obj)293 void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj)
294 {
295 u32 i;
296 u64 offset;
297
298 i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
299
300 mutex_lock(&table->mutex);
301
302 if (--table->icm[i]->refcount == 0) {
303 offset = (u64) i * MLX4_TABLE_CHUNK_SIZE;
304 mlx4_UNMAP_ICM(dev, table->virt + offset,
305 MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
306 mlx4_free_icm(dev, table->icm[i], table->coherent);
307 table->icm[i] = NULL;
308 }
309
310 mutex_unlock(&table->mutex);
311 }
312
mlx4_table_find(struct mlx4_icm_table * table,u32 obj,dma_addr_t * dma_handle)313 void *mlx4_table_find(struct mlx4_icm_table *table, u32 obj,
314 dma_addr_t *dma_handle)
315 {
316 int offset, dma_offset, i;
317 u64 idx;
318 struct mlx4_icm_chunk *chunk;
319 struct mlx4_icm *icm;
320 void *addr = NULL;
321
322 if (!table->lowmem)
323 return NULL;
324
325 mutex_lock(&table->mutex);
326
327 idx = (u64) (obj & (table->num_obj - 1)) * table->obj_size;
328 icm = table->icm[idx / MLX4_TABLE_CHUNK_SIZE];
329 dma_offset = offset = idx % MLX4_TABLE_CHUNK_SIZE;
330
331 if (!icm)
332 goto out;
333
334 list_for_each_entry(chunk, &icm->chunk_list, list) {
335 for (i = 0; i < chunk->npages; ++i) {
336 dma_addr_t dma_addr;
337 size_t len;
338
339 if (table->coherent) {
340 len = chunk->buf[i].size;
341 dma_addr = chunk->buf[i].dma_addr;
342 addr = chunk->buf[i].addr;
343 } else {
344 struct page *page;
345
346 len = sg_dma_len(&chunk->sg[i]);
347 dma_addr = sg_dma_address(&chunk->sg[i]);
348
349 /* XXX: we should never do this for highmem
350 * allocation. This function either needs
351 * to be split, or the kernel virtual address
352 * return needs to be made optional.
353 */
354 page = sg_page(&chunk->sg[i]);
355 addr = lowmem_page_address(page);
356 }
357
358 if (dma_handle && dma_offset >= 0) {
359 if (len > dma_offset)
360 *dma_handle = dma_addr + dma_offset;
361 dma_offset -= len;
362 }
363
364 /*
365 * DMA mapping can merge pages but not split them,
366 * so if we found the page, dma_handle has already
367 * been assigned to.
368 */
369 if (len > offset)
370 goto out;
371 offset -= len;
372 }
373 }
374
375 addr = NULL;
376 out:
377 mutex_unlock(&table->mutex);
378 return addr ? addr + offset : NULL;
379 }
380
mlx4_table_get_range(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 start,u32 end)381 int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
382 u32 start, u32 end)
383 {
384 int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size;
385 int err;
386 u32 i;
387
388 for (i = start; i <= end; i += inc) {
389 err = mlx4_table_get(dev, table, i);
390 if (err)
391 goto fail;
392 }
393
394 return 0;
395
396 fail:
397 while (i > start) {
398 i -= inc;
399 mlx4_table_put(dev, table, i);
400 }
401
402 return err;
403 }
404
mlx4_table_put_range(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 start,u32 end)405 void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
406 u32 start, u32 end)
407 {
408 u32 i;
409
410 for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size)
411 mlx4_table_put(dev, table, i);
412 }
413
mlx4_init_icm_table(struct mlx4_dev * dev,struct mlx4_icm_table * table,u64 virt,int obj_size,u32 nobj,int reserved,int use_lowmem,int use_coherent)414 int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
415 u64 virt, int obj_size, u32 nobj, int reserved,
416 int use_lowmem, int use_coherent)
417 {
418 int obj_per_chunk;
419 int num_icm;
420 unsigned chunk_size;
421 int i;
422 u64 size;
423
424 obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size;
425 if (WARN_ON(!obj_per_chunk))
426 return -EINVAL;
427 num_icm = DIV_ROUND_UP(nobj, obj_per_chunk);
428
429 table->icm = kvzalloc_objs(*table->icm, num_icm);
430 if (!table->icm)
431 return -ENOMEM;
432 table->virt = virt;
433 table->num_icm = num_icm;
434 table->num_obj = nobj;
435 table->obj_size = obj_size;
436 table->lowmem = use_lowmem;
437 table->coherent = use_coherent;
438 mutex_init(&table->mutex);
439
440 size = (u64) nobj * obj_size;
441 for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
442 chunk_size = MLX4_TABLE_CHUNK_SIZE;
443 if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > size)
444 chunk_size = PAGE_ALIGN(size -
445 i * MLX4_TABLE_CHUNK_SIZE);
446
447 table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
448 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
449 __GFP_NOWARN, use_coherent);
450 if (!table->icm[i])
451 goto err;
452 if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) {
453 mlx4_free_icm(dev, table->icm[i], use_coherent);
454 table->icm[i] = NULL;
455 goto err;
456 }
457
458 /*
459 * Add a reference to this ICM chunk so that it never
460 * gets freed (since it contains reserved firmware objects).
461 */
462 ++table->icm[i]->refcount;
463 }
464
465 return 0;
466
467 err:
468 for (i = 0; i < num_icm; ++i)
469 if (table->icm[i]) {
470 mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE,
471 MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
472 mlx4_free_icm(dev, table->icm[i], use_coherent);
473 }
474
475 kvfree(table->icm);
476
477 return -ENOMEM;
478 }
479
mlx4_cleanup_icm_table(struct mlx4_dev * dev,struct mlx4_icm_table * table)480 void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table)
481 {
482 int i;
483
484 for (i = 0; i < table->num_icm; ++i)
485 if (table->icm[i]) {
486 mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
487 MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
488 mlx4_free_icm(dev, table->icm[i], table->coherent);
489 }
490
491 kvfree(table->icm);
492 }
493