mlx4: do not set struct device * to NULL
[akaros.git] / kern / drivers / net / mlx4 / icm.c
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_compat.h>
35 #include <linux/mlx4/cmd.h>
36
37 #include "mlx4.h"
38 #include "icm.h"
39 #include "fw.h"
40
41 /*
42  * We allocate in as big chunks as we can, up to a maximum of 256 KB
43  * per chunk.
44  */
45 enum {
46         MLX4_ICM_ALLOC_SIZE     = 1 << 18,
47         MLX4_TABLE_CHUNK_SIZE   = 1 << 18
48 };
49
50 static void mlx4_free_icm_pages(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
51 {
52         int i;
53
54         if (chunk->nsg > 0)
55                 pci_unmap_sg(dev->persist->pdev, chunk->mem, chunk->npages,
56                              PCI_DMA_BIDIRECTIONAL);
57
58         for (i = 0; i < chunk->npages; ++i)
59                 free_cont_pages(page2kva(sg_page(&chunk->mem[i])),
60                                 get_order(chunk->mem[i].length));
61 }
62
63 static void mlx4_free_icm_coherent(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
64 {
65         int i;
66
67         for (i = 0; i < chunk->npages; ++i)
68                 dma_free_coherent(&dev->persist->pdev->linux_dev,
69                                   chunk->mem[i].length,
70                                   lowmem_page_address(sg_page(&chunk->mem[i])),
71                                   sg_dma_address(&chunk->mem[i]));
72 }
73
74 void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent)
75 {
76         struct mlx4_icm_chunk *chunk, *tmp;
77
78         if (!icm)
79                 return;
80
81         list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
82                 if (coherent)
83                         mlx4_free_icm_coherent(dev, chunk);
84                 else
85                         mlx4_free_icm_pages(dev, chunk);
86
87                 kfree(chunk);
88         }
89
90         kfree(icm);
91 }
92
93 static int mlx4_alloc_icm_pages(struct scatterlist *mem, int order,
94                                 gfp_t gfp_mask, int node)
95 {
96         struct page *page;
97
98         page = alloc_pages_node(node, gfp_mask, order);
99         if (!page) {
100                 page = kva2page(get_cont_pages(order, gfp_mask));
101                 if (!page)
102                         return -ENOMEM;
103         }
104
105         sg_set_page(mem, page, PAGE_SIZE << order, 0);
106         return 0;
107 }
108
109 static int mlx4_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
110                                     int order, gfp_t gfp_mask)
111 {
112         void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order,
113                                        &sg_dma_address(mem), gfp_mask);
114         if (!buf)
115                 return -ENOMEM;
116
117         sg_set_buf(mem, buf, PAGE_SIZE << order);
118         assert(!(mem->offset));
119         sg_dma_len(mem) = PAGE_SIZE << order;
120         return 0;
121 }
122
123 struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
124                                 gfp_t gfp_mask, int coherent)
125 {
126         struct mlx4_icm *icm;
127         struct mlx4_icm_chunk *chunk = NULL;
128         int cur_order;
129         int ret;
130
131         /* We use sg_set_buf for coherent allocs, which assumes low memory */
132         assert(!(coherent && (gfp_mask & __GFP_HIGHMEM)));
133
134         icm = kmalloc_node(sizeof(*icm),
135                            gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN),
136                            dev->numa_node);
137         if (!icm) {
138                 icm = kmalloc(sizeof(*icm),
139                               gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
140                 if (!icm)
141                         return NULL;
142         }
143
144         icm->refcount = 0;
145         INIT_LIST_HEAD(&icm->chunk_list);
146
147         cur_order = get_order(MLX4_ICM_ALLOC_SIZE);
148
149         while (npages > 0) {
150                 if (!chunk) {
151                         chunk = kmalloc_node(sizeof(*chunk),
152                                              gfp_mask & ~(__GFP_HIGHMEM |
153                                                           __GFP_NOWARN),
154                                              dev->numa_node);
155                         if (!chunk) {
156                                 chunk = kmalloc(sizeof(*chunk),
157                                                 gfp_mask & ~(__GFP_HIGHMEM |
158                                                              __GFP_NOWARN));
159                                 if (!chunk)
160                                         goto fail;
161                         }
162
163                         sg_init_table(chunk->mem, MLX4_ICM_CHUNK_LEN);
164                         chunk->npages = 0;
165                         chunk->nsg    = 0;
166                         list_add_tail(&chunk->list, &icm->chunk_list);
167                 }
168
169                 while (1 << cur_order > npages)
170                         --cur_order;
171
172                 if (coherent)
173                         ret = mlx4_alloc_icm_coherent(&dev->persist->pdev->linux_dev,
174                                                       &chunk->mem[chunk->npages],
175                                                       cur_order, gfp_mask);
176                 else
177                         ret = mlx4_alloc_icm_pages(&chunk->mem[chunk->npages],
178                                                    cur_order, gfp_mask,
179                                                    dev->numa_node);
180
181                 if (ret) {
182                         if (--cur_order < 0)
183                                 goto fail;
184                         else
185                                 continue;
186                 }
187
188                 ++chunk->npages;
189
190                 if (coherent)
191                         ++chunk->nsg;
192                 else if (chunk->npages == MLX4_ICM_CHUNK_LEN) {
193                         chunk->nsg = pci_map_sg(dev->persist->pdev, chunk->mem,
194                                                 chunk->npages,
195                                                 PCI_DMA_BIDIRECTIONAL);
196
197                         if (chunk->nsg <= 0)
198                                 goto fail;
199                 }
200
201                 if (chunk->npages == MLX4_ICM_CHUNK_LEN)
202                         chunk = NULL;
203
204                 npages -= 1 << cur_order;
205         }
206
207         if (!coherent && chunk) {
208                 chunk->nsg = pci_map_sg(dev->persist->pdev, chunk->mem,
209                                         chunk->npages,
210                                         PCI_DMA_BIDIRECTIONAL);
211
212                 if (chunk->nsg <= 0)
213                         goto fail;
214         }
215
216         return icm;
217
218 fail:
219         mlx4_free_icm(dev, icm, coherent);
220         return NULL;
221 }
222
223 static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm,
224                         uint64_t virt)
225 {
226         return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt);
227 }
228
229 static int mlx4_UNMAP_ICM(struct mlx4_dev *dev, uint64_t virt,
230                           uint32_t page_count)
231 {
232         return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM,
233                         MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
234 }
235
236 int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm)
237 {
238         return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1);
239 }
240
241 int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev)
242 {
243         return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX,
244                         MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
245 }
246
247 int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table,
248                    uint32_t obj,
249                    gfp_t gfp)
250 {
251         uint32_t i = (obj & (table->num_obj - 1)) /
252                         (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
253         int ret = 0;
254
255         qlock(&table->mutex);
256
257         if (table->icm[i]) {
258                 ++table->icm[i]->refcount;
259                 goto out;
260         }
261
262         table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
263                                        (table->lowmem ? gfp : GFP_HIGHUSER) |
264                                        __GFP_NOWARN, table->coherent);
265         if (!table->icm[i]) {
266                 ret = -ENOMEM;
267                 goto out;
268         }
269
270         if (mlx4_MAP_ICM(dev, table->icm[i], table->virt +
271                          (uint64_t) i * MLX4_TABLE_CHUNK_SIZE)) {
272                 mlx4_free_icm(dev, table->icm[i], table->coherent);
273                 table->icm[i] = NULL;
274                 ret = -ENOMEM;
275                 goto out;
276         }
277
278         ++table->icm[i]->refcount;
279
280 out:
281         qunlock(&table->mutex);
282         return ret;
283 }
284
285 void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table,
286                     uint32_t obj)
287 {
288         uint32_t i;
289         uint64_t offset;
290
291         i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
292
293         qlock(&table->mutex);
294
295         if (--table->icm[i]->refcount == 0) {
296                 offset = (uint64_t) i * MLX4_TABLE_CHUNK_SIZE;
297                 mlx4_UNMAP_ICM(dev, table->virt + offset,
298                                MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
299                 mlx4_free_icm(dev, table->icm[i], table->coherent);
300                 table->icm[i] = NULL;
301         }
302
303         qunlock(&table->mutex);
304 }
305
306 void *mlx4_table_find(struct mlx4_icm_table *table, uint32_t obj,
307                         dma_addr_t *dma_handle)
308 {
309         int offset, dma_offset, i;
310         uint64_t idx;
311         struct mlx4_icm_chunk *chunk;
312         struct mlx4_icm *icm;
313         struct page *page = NULL;
314
315         if (!table->lowmem)
316                 return NULL;
317
318         qlock(&table->mutex);
319
320         idx = (uint64_t) (obj & (table->num_obj - 1)) * table->obj_size;
321         icm = table->icm[idx / MLX4_TABLE_CHUNK_SIZE];
322         dma_offset = offset = idx % MLX4_TABLE_CHUNK_SIZE;
323
324         if (!icm)
325                 goto out;
326
327         list_for_each_entry(chunk, &icm->chunk_list, list) {
328                 for (i = 0; i < chunk->npages; ++i) {
329                         if (dma_handle && dma_offset >= 0) {
330                                 if (sg_dma_len(&chunk->mem[i]) > dma_offset)
331                                         *dma_handle = sg_dma_address(&chunk->mem[i]) +
332                                                 dma_offset;
333                                 dma_offset -= sg_dma_len(&chunk->mem[i]);
334                         }
335                         /*
336                          * DMA mapping can merge pages but not split them,
337                          * so if we found the page, dma_handle has already
338                          * been assigned to.
339                          */
340                         if (chunk->mem[i].length > offset) {
341                                 page = sg_page(&chunk->mem[i]);
342                                 goto out;
343                         }
344                         offset -= chunk->mem[i].length;
345                 }
346         }
347
348 out:
349         qunlock(&table->mutex);
350         return page ? lowmem_page_address(page) + offset : NULL;
351 }
352
353 int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
354                          uint32_t start, uint32_t end)
355 {
356         int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size;
357         int err;
358         uint32_t i;
359
360         for (i = start; i <= end; i += inc) {
361                 err = mlx4_table_get(dev, table, i, MEM_WAIT);
362                 if (err)
363                         goto fail;
364         }
365
366         return 0;
367
368 fail:
369         while (i > start) {
370                 i -= inc;
371                 mlx4_table_put(dev, table, i);
372         }
373
374         return err;
375 }
376
377 void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
378                           uint32_t start, uint32_t end)
379 {
380         uint32_t i;
381
382         for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size)
383                 mlx4_table_put(dev, table, i);
384 }
385
386 int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
387                         uint64_t virt, int obj_size,    uint32_t nobj,
388                         int reserved,
389                         int use_lowmem, int use_coherent)
390 {
391         int obj_per_chunk;
392         int num_icm;
393         unsigned chunk_size;
394         int i;
395         uint64_t size;
396
397         obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size;
398         num_icm = (nobj + obj_per_chunk - 1) / obj_per_chunk;
399
400         table->icm      = kzmalloc((num_icm) * (sizeof *table->icm),
401                                    MEM_WAIT);
402         if (!table->icm)
403                 return -ENOMEM;
404         table->virt     = virt;
405         table->num_icm  = num_icm;
406         table->num_obj  = nobj;
407         table->obj_size = obj_size;
408         table->lowmem   = use_lowmem;
409         table->coherent = use_coherent;
410         qlock_init(&table->mutex);
411
412         size = (uint64_t) nobj * obj_size;
413         for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
414                 chunk_size = MLX4_TABLE_CHUNK_SIZE;
415                 if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > size)
416                         chunk_size = PAGE_ALIGN(size -
417                                         i * MLX4_TABLE_CHUNK_SIZE);
418
419                 table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
420                                                (use_lowmem ? MEM_WAIT : GFP_HIGHUSER) |
421                                                __GFP_NOWARN, use_coherent);
422                 if (!table->icm[i])
423                         goto err;
424                 if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) {
425                         mlx4_free_icm(dev, table->icm[i], use_coherent);
426                         table->icm[i] = NULL;
427                         goto err;
428                 }
429
430                 /*
431                  * Add a reference to this ICM chunk so that it never
432                  * gets freed (since it contains reserved firmware objects).
433                  */
434                 ++table->icm[i]->refcount;
435         }
436
437         return 0;
438
439 err:
440         for (i = 0; i < num_icm; ++i)
441                 if (table->icm[i]) {
442                         mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE,
443                                        MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
444                         mlx4_free_icm(dev, table->icm[i], use_coherent);
445                 }
446
447         kfree(table->icm);
448
449         return -ENOMEM;
450 }
451
452 void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table)
453 {
454         int i;
455
456         for (i = 0; i < table->num_icm; ++i)
457                 if (table->icm[i]) {
458                         mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
459                                        MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
460                         mlx4_free_icm(dev, table->icm[i], table->coherent);
461                 }
462
463         kfree(table->icm);
464 }