9eeee0545f1cf294ecddb7bb0becc873df8906da
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / pagealloc.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/highmem.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/delay.h>
37 #include <linux/mlx5/driver.h>
38 #include <linux/mlx5/cmd.h>
39 #include "mlx5_core.h"
40
41 enum {
42         MLX5_PAGES_CANT_GIVE    = 0,
43         MLX5_PAGES_GIVE         = 1,
44         MLX5_PAGES_TAKE         = 2
45 };
46
47 enum {
48         MLX5_BOOT_PAGES         = 1,
49         MLX5_INIT_PAGES         = 2,
50         MLX5_POST_INIT_PAGES    = 3
51 };
52
53 struct mlx5_pages_req {
54         struct mlx5_core_dev *dev;
55         u16     func_id;
56         s32     npages;
57         struct work_struct work;
58 };
59
60 struct fw_page {
61         struct rb_node          rb_node;
62         u64                     addr;
63         struct page            *page;
64         u16                     func_id;
65         unsigned long           bitmask;
66         struct list_head        list;
67         unsigned                free_count;
68 };
69
70 struct mlx5_query_pages_inbox {
71         struct mlx5_inbox_hdr   hdr;
72         u8                      rsvd[8];
73 };
74
75 struct mlx5_query_pages_outbox {
76         struct mlx5_outbox_hdr  hdr;
77         __be16                  rsvd;
78         __be16                  func_id;
79         __be32                  num_pages;
80 };
81
82 struct mlx5_manage_pages_inbox {
83         struct mlx5_inbox_hdr   hdr;
84         __be16                  rsvd;
85         __be16                  func_id;
86         __be32                  num_entries;
87         __be64                  pas[0];
88 };
89
90 struct mlx5_manage_pages_outbox {
91         struct mlx5_outbox_hdr  hdr;
92         __be32                  num_entries;
93         u8                      rsvd[4];
94         __be64                  pas[0];
95 };
96
97 enum {
98         MAX_RECLAIM_TIME_MSECS  = 5000,
99         MAX_RECLAIM_VFS_PAGES_TIME_MSECS = 2 * 1000 * 60,
100 };
101
102 enum {
103         MLX5_MAX_RECLAIM_TIME_MILI      = 5000,
104         MLX5_NUM_4K_IN_PAGE             = PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
105 };
106
107 static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u16 func_id)
108 {
109         struct rb_root *root = &dev->priv.page_root;
110         struct rb_node **new = &root->rb_node;
111         struct rb_node *parent = NULL;
112         struct fw_page *nfp;
113         struct fw_page *tfp;
114         int i;
115
116         while (*new) {
117                 parent = *new;
118                 tfp = rb_entry(parent, struct fw_page, rb_node);
119                 if (tfp->addr < addr)
120                         new = &parent->rb_left;
121                 else if (tfp->addr > addr)
122                         new = &parent->rb_right;
123                 else
124                         return -EEXIST;
125         }
126
127         nfp = kzalloc(sizeof(*nfp), GFP_KERNEL);
128         if (!nfp)
129                 return -ENOMEM;
130
131         nfp->addr = addr;
132         nfp->page = page;
133         nfp->func_id = func_id;
134         nfp->free_count = MLX5_NUM_4K_IN_PAGE;
135         for (i = 0; i < MLX5_NUM_4K_IN_PAGE; i++)
136                 set_bit(i, &nfp->bitmask);
137
138         rb_link_node(&nfp->rb_node, parent, new);
139         rb_insert_color(&nfp->rb_node, root);
140         list_add(&nfp->list, &dev->priv.free_list);
141
142         return 0;
143 }
144
145 static struct fw_page *find_fw_page(struct mlx5_core_dev *dev, u64 addr)
146 {
147         struct rb_root *root = &dev->priv.page_root;
148         struct rb_node *tmp = root->rb_node;
149         struct fw_page *result = NULL;
150         struct fw_page *tfp;
151
152         while (tmp) {
153                 tfp = rb_entry(tmp, struct fw_page, rb_node);
154                 if (tfp->addr < addr) {
155                         tmp = tmp->rb_left;
156                 } else if (tfp->addr > addr) {
157                         tmp = tmp->rb_right;
158                 } else {
159                         result = tfp;
160                         break;
161                 }
162         }
163
164         return result;
165 }
166
167 static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
168                                 s32 *npages, int boot)
169 {
170         struct mlx5_query_pages_inbox   in;
171         struct mlx5_query_pages_outbox  out;
172         int err;
173
174         memset(&in, 0, sizeof(in));
175         memset(&out, 0, sizeof(out));
176         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_PAGES);
177         in.hdr.opmod = boot ? cpu_to_be16(MLX5_BOOT_PAGES) : cpu_to_be16(MLX5_INIT_PAGES);
178
179         err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
180         if (err)
181                 return err;
182
183         if (out.hdr.status)
184                 return mlx5_cmd_status_to_err(&out.hdr);
185
186         *npages = be32_to_cpu(out.num_pages);
187         *func_id = be16_to_cpu(out.func_id);
188
189         return err;
190 }
191
192 static int alloc_4k(struct mlx5_core_dev *dev, u64 *addr)
193 {
194         struct fw_page *fp;
195         unsigned n;
196
197         if (list_empty(&dev->priv.free_list))
198                 return -ENOMEM;
199
200         fp = list_entry(dev->priv.free_list.next, struct fw_page, list);
201         n = find_first_bit(&fp->bitmask, 8 * sizeof(fp->bitmask));
202         if (n >= MLX5_NUM_4K_IN_PAGE) {
203                 mlx5_core_warn(dev, "alloc 4k bug\n");
204                 return -ENOENT;
205         }
206         clear_bit(n, &fp->bitmask);
207         fp->free_count--;
208         if (!fp->free_count)
209                 list_del(&fp->list);
210
211         *addr = fp->addr + n * MLX5_ADAPTER_PAGE_SIZE;
212
213         return 0;
214 }
215
216 #define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
217
218 static void free_4k(struct mlx5_core_dev *dev, u64 addr)
219 {
220         struct fw_page *fwp;
221         int n;
222
223         fwp = find_fw_page(dev, addr & MLX5_U64_4K_PAGE_MASK);
224         if (!fwp) {
225                 mlx5_core_warn(dev, "page not found\n");
226                 return;
227         }
228
229         n = (addr & ~MLX5_U64_4K_PAGE_MASK) >> MLX5_ADAPTER_PAGE_SHIFT;
230         fwp->free_count++;
231         set_bit(n, &fwp->bitmask);
232         if (fwp->free_count == MLX5_NUM_4K_IN_PAGE) {
233                 rb_erase(&fwp->rb_node, &dev->priv.page_root);
234                 if (fwp->free_count != 1)
235                         list_del(&fwp->list);
236                 dma_unmap_page(&dev->pdev->dev, addr & MLX5_U64_4K_PAGE_MASK,
237                                PAGE_SIZE, DMA_BIDIRECTIONAL);
238                 __free_page(fwp->page);
239                 kfree(fwp);
240         } else if (fwp->free_count == 1) {
241                 list_add(&fwp->list, &dev->priv.free_list);
242         }
243 }
244
245 static int alloc_system_page(struct mlx5_core_dev *dev, u16 func_id)
246 {
247         struct page *page;
248         u64 addr;
249         int err;
250         int nid = dev_to_node(&dev->pdev->dev);
251
252         page = alloc_pages_node(nid, GFP_HIGHUSER, 0);
253         if (!page) {
254                 mlx5_core_warn(dev, "failed to allocate page\n");
255                 return -ENOMEM;
256         }
257         addr = dma_map_page(&dev->pdev->dev, page, 0,
258                             PAGE_SIZE, DMA_BIDIRECTIONAL);
259         if (dma_mapping_error(&dev->pdev->dev, addr)) {
260                 mlx5_core_warn(dev, "failed dma mapping page\n");
261                 err = -ENOMEM;
262                 goto out_alloc;
263         }
264         err = insert_page(dev, addr, page, func_id);
265         if (err) {
266                 mlx5_core_err(dev, "failed to track allocated page\n");
267                 goto out_mapping;
268         }
269
270         return 0;
271
272 out_mapping:
273         dma_unmap_page(&dev->pdev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
274
275 out_alloc:
276         __free_page(page);
277
278         return err;
279 }
280
281 static void page_notify_fail(struct mlx5_core_dev *dev, u16 func_id)
282 {
283         struct mlx5_manage_pages_inbox *in;
284         struct mlx5_manage_pages_outbox out;
285         int err;
286
287         in = kzalloc(sizeof(*in), GFP_KERNEL);
288         if (!in)
289                 return;
290
291         memset(&out, 0, sizeof(out));
292         in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
293         in->hdr.opmod = cpu_to_be16(MLX5_PAGES_CANT_GIVE);
294         in->func_id = cpu_to_be16(func_id);
295         err = mlx5_cmd_exec(dev, in, sizeof(*in), &out, sizeof(out));
296         if (!err)
297                 err = mlx5_cmd_status_to_err(&out.hdr);
298
299         if (err)
300                 mlx5_core_warn(dev, "page notify failed\n");
301
302         kfree(in);
303 }
304
305 static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
306                       int notify_fail)
307 {
308         struct mlx5_manage_pages_inbox *in;
309         struct mlx5_manage_pages_outbox out;
310         int inlen;
311         u64 addr;
312         int err;
313         int i;
314
315         inlen = sizeof(*in) + npages * sizeof(in->pas[0]);
316         in = mlx5_vzalloc(inlen);
317         if (!in) {
318                 err = -ENOMEM;
319                 mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
320                 goto out_free;
321         }
322         memset(&out, 0, sizeof(out));
323
324         for (i = 0; i < npages; i++) {
325 retry:
326                 err = alloc_4k(dev, &addr);
327                 if (err) {
328                         if (err == -ENOMEM)
329                                 err = alloc_system_page(dev, func_id);
330                         if (err)
331                                 goto out_4k;
332
333                         goto retry;
334                 }
335                 in->pas[i] = cpu_to_be64(addr);
336         }
337
338         in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
339         in->hdr.opmod = cpu_to_be16(MLX5_PAGES_GIVE);
340         in->func_id = cpu_to_be16(func_id);
341         in->num_entries = cpu_to_be32(npages);
342         err = mlx5_cmd_exec(dev, in, inlen, &out, sizeof(out));
343         if (err) {
344                 mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
345                                func_id, npages, err);
346                 goto out_4k;
347         }
348         dev->priv.fw_pages += npages;
349
350         err = mlx5_cmd_status_to_err(&out.hdr);
351         if (err) {
352                 mlx5_core_warn(dev, "func_id 0x%x, npages %d, status %d\n",
353                                func_id, npages, out.hdr.status);
354                 goto out_4k;
355         }
356
357         dev->priv.fw_pages += npages;
358         if (func_id)
359                 dev->priv.vfs_pages += npages;
360
361         mlx5_core_dbg(dev, "err %d\n", err);
362
363         kvfree(in);
364         return 0;
365
366 out_4k:
367         for (i--; i >= 0; i--)
368                 free_4k(dev, be64_to_cpu(in->pas[i]));
369 out_free:
370         kvfree(in);
371         if (notify_fail)
372                 page_notify_fail(dev, func_id);
373         return err;
374 }
375
376 static int reclaim_pages(struct mlx5_core_dev *dev, u32 func_id, int npages,
377                          int *nclaimed)
378 {
379         struct mlx5_manage_pages_inbox   in;
380         struct mlx5_manage_pages_outbox *out;
381         int num_claimed;
382         int outlen;
383         u64 addr;
384         int err;
385         int i;
386
387         if (nclaimed)
388                 *nclaimed = 0;
389
390         memset(&in, 0, sizeof(in));
391         outlen = sizeof(*out) + npages * sizeof(out->pas[0]);
392         out = mlx5_vzalloc(outlen);
393         if (!out)
394                 return -ENOMEM;
395
396         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
397         in.hdr.opmod = cpu_to_be16(MLX5_PAGES_TAKE);
398         in.func_id = cpu_to_be16(func_id);
399         in.num_entries = cpu_to_be32(npages);
400         mlx5_core_dbg(dev, "npages %d, outlen %d\n", npages, outlen);
401         err = mlx5_cmd_exec(dev, &in, sizeof(in), out, outlen);
402         if (err) {
403                 mlx5_core_err(dev, "failed reclaiming pages\n");
404                 goto out_free;
405         }
406         dev->priv.fw_pages -= npages;
407
408         if (out->hdr.status) {
409                 err = mlx5_cmd_status_to_err(&out->hdr);
410                 goto out_free;
411         }
412
413         num_claimed = be32_to_cpu(out->num_entries);
414         if (num_claimed > npages) {
415                 mlx5_core_warn(dev, "fw returned %d, driver asked %d => corruption\n",
416                                num_claimed, npages);
417                 err = -EINVAL;
418                 goto out_free;
419         }
420         if (nclaimed)
421                 *nclaimed = num_claimed;
422
423         for (i = 0; i < num_claimed; i++) {
424                 addr = be64_to_cpu(out->pas[i]);
425                 free_4k(dev, addr);
426         }
427         dev->priv.fw_pages -= num_claimed;
428         if (func_id)
429                 dev->priv.vfs_pages -= num_claimed;
430
431 out_free:
432         kvfree(out);
433         return err;
434 }
435
436 static void pages_work_handler(struct work_struct *work)
437 {
438         struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
439         struct mlx5_core_dev *dev = req->dev;
440         int err = 0;
441
442         if (req->npages < 0)
443                 err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL);
444         else if (req->npages > 0)
445                 err = give_pages(dev, req->func_id, req->npages, 1);
446
447         if (err)
448                 mlx5_core_warn(dev, "%s fail %d\n",
449                                req->npages < 0 ? "reclaim" : "give", err);
450
451         kfree(req);
452 }
453
454 void mlx5_core_req_pages_handler(struct mlx5_core_dev *dev, u16 func_id,
455                                  s32 npages)
456 {
457         struct mlx5_pages_req *req;
458
459         req = kzalloc(sizeof(*req), GFP_ATOMIC);
460         if (!req) {
461                 mlx5_core_warn(dev, "failed to allocate pages request\n");
462                 return;
463         }
464
465         req->dev = dev;
466         req->func_id = func_id;
467         req->npages = npages;
468         INIT_WORK(&req->work, pages_work_handler);
469         queue_work(dev->priv.pg_wq, &req->work);
470 }
471
472 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
473 {
474         u16 uninitialized_var(func_id);
475         s32 uninitialized_var(npages);
476         int err;
477
478         err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
479         if (err)
480                 return err;
481
482         mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
483                       npages, boot ? "boot" : "init", func_id);
484
485         return give_pages(dev, func_id, npages, 0);
486 }
487
488 enum {
489         MLX5_BLKS_FOR_RECLAIM_PAGES = 12
490 };
491
492 static int optimal_reclaimed_pages(void)
493 {
494         struct mlx5_cmd_prot_block *block;
495         struct mlx5_cmd_layout *lay;
496         int ret;
497
498         ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
499                sizeof(struct mlx5_manage_pages_outbox)) /
500                FIELD_SIZEOF(struct mlx5_manage_pages_outbox, pas[0]);
501
502         return ret;
503 }
504
505 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
506 {
507         unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
508         struct fw_page *fwp;
509         struct rb_node *p;
510         int nclaimed = 0;
511         int err = 0;
512
513         do {
514                 p = rb_first(&dev->priv.page_root);
515                 if (p) {
516                         fwp = rb_entry(p, struct fw_page, rb_node);
517                         if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
518                                 free_4k(dev, fwp->addr);
519                                 nclaimed = 1;
520                         } else {
521                                 err = reclaim_pages(dev, fwp->func_id,
522                                                     optimal_reclaimed_pages(),
523                                                     &nclaimed);
524                         }
525                         if (err) {
526                                 mlx5_core_warn(dev, "failed reclaiming pages (%d)\n",
527                                                err);
528                                 return err;
529                         }
530                         if (nclaimed)
531                                 end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
532                 }
533                 if (time_after(jiffies, end)) {
534                         mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
535                         break;
536                 }
537         } while (p);
538
539         return 0;
540 }
541
542 void mlx5_pagealloc_init(struct mlx5_core_dev *dev)
543 {
544         dev->priv.page_root = RB_ROOT;
545         INIT_LIST_HEAD(&dev->priv.free_list);
546 }
547
548 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
549 {
550         /* nothing */
551 }
552
553 int mlx5_pagealloc_start(struct mlx5_core_dev *dev)
554 {
555         dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
556         if (!dev->priv.pg_wq)
557                 return -ENOMEM;
558
559         return 0;
560 }
561
562 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
563 {
564         destroy_workqueue(dev->priv.pg_wq);
565 }
566
567 int mlx5_wait_for_vf_pages(struct mlx5_core_dev *dev)
568 {
569         unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS);
570         int prev_vfs_pages = dev->priv.vfs_pages;
571
572         mlx5_core_dbg(dev, "Waiting for %d pages from %s\n", prev_vfs_pages,
573                       dev->priv.name);
574         while (dev->priv.vfs_pages) {
575                 if (time_after(jiffies, end)) {
576                         mlx5_core_warn(dev, "aborting while there are %d pending pages\n", dev->priv.vfs_pages);
577                         return -ETIMEDOUT;
578                 }
579                 if (dev->priv.vfs_pages < prev_vfs_pages) {
580                         end = jiffies + msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS);
581                         prev_vfs_pages = dev->priv.vfs_pages;
582                 }
583                 msleep(50);
584         }
585
586         mlx5_core_dbg(dev, "All pages received from %s\n", dev->priv.name);
587         return 0;
588 }