Merge tag 'perf-for-bpf-2020-05-06' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.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 "mlx5_core.h"
39 #include "lib/eq.h"
40
41 enum {
42         MLX5_PAGES_CANT_GIVE    = 0,
43         MLX5_PAGES_GIVE         = 1,
44         MLX5_PAGES_TAKE         = 2
45 };
46
47 struct mlx5_pages_req {
48         struct mlx5_core_dev *dev;
49         u16     func_id;
50         u8      ec_function;
51         s32     npages;
52         struct work_struct work;
53         u8      release_all;
54 };
55
56 struct fw_page {
57         struct rb_node          rb_node;
58         u64                     addr;
59         struct page            *page;
60         u16                     func_id;
61         unsigned long           bitmask;
62         struct list_head        list;
63         unsigned                free_count;
64 };
65
66 enum {
67         MAX_RECLAIM_TIME_MSECS  = 5000,
68         MAX_RECLAIM_VFS_PAGES_TIME_MSECS = 2 * 1000 * 60,
69 };
70
71 enum {
72         MLX5_MAX_RECLAIM_TIME_MILI      = 5000,
73         MLX5_NUM_4K_IN_PAGE             = PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
74 };
75
76 static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u16 func_id)
77 {
78         struct rb_root *root = &dev->priv.page_root;
79         struct rb_node **new = &root->rb_node;
80         struct rb_node *parent = NULL;
81         struct fw_page *nfp;
82         struct fw_page *tfp;
83         int i;
84
85         while (*new) {
86                 parent = *new;
87                 tfp = rb_entry(parent, struct fw_page, rb_node);
88                 if (tfp->addr < addr)
89                         new = &parent->rb_left;
90                 else if (tfp->addr > addr)
91                         new = &parent->rb_right;
92                 else
93                         return -EEXIST;
94         }
95
96         nfp = kzalloc(sizeof(*nfp), GFP_KERNEL);
97         if (!nfp)
98                 return -ENOMEM;
99
100         nfp->addr = addr;
101         nfp->page = page;
102         nfp->func_id = func_id;
103         nfp->free_count = MLX5_NUM_4K_IN_PAGE;
104         for (i = 0; i < MLX5_NUM_4K_IN_PAGE; i++)
105                 set_bit(i, &nfp->bitmask);
106
107         rb_link_node(&nfp->rb_node, parent, new);
108         rb_insert_color(&nfp->rb_node, root);
109         list_add(&nfp->list, &dev->priv.free_list);
110
111         return 0;
112 }
113
114 static struct fw_page *find_fw_page(struct mlx5_core_dev *dev, u64 addr)
115 {
116         struct rb_root *root = &dev->priv.page_root;
117         struct rb_node *tmp = root->rb_node;
118         struct fw_page *result = NULL;
119         struct fw_page *tfp;
120
121         while (tmp) {
122                 tfp = rb_entry(tmp, struct fw_page, rb_node);
123                 if (tfp->addr < addr) {
124                         tmp = tmp->rb_left;
125                 } else if (tfp->addr > addr) {
126                         tmp = tmp->rb_right;
127                 } else {
128                         result = tfp;
129                         break;
130                 }
131         }
132
133         return result;
134 }
135
136 static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
137                                 s32 *npages, int boot)
138 {
139         u32 out[MLX5_ST_SZ_DW(query_pages_out)] = {};
140         u32 in[MLX5_ST_SZ_DW(query_pages_in)] = {};
141         int err;
142
143         MLX5_SET(query_pages_in, in, opcode, MLX5_CMD_OP_QUERY_PAGES);
144         MLX5_SET(query_pages_in, in, op_mod, boot ?
145                  MLX5_QUERY_PAGES_IN_OP_MOD_BOOT_PAGES :
146                  MLX5_QUERY_PAGES_IN_OP_MOD_INIT_PAGES);
147         MLX5_SET(query_pages_in, in, embedded_cpu_function, mlx5_core_is_ecpf(dev));
148
149         err = mlx5_cmd_exec_inout(dev, query_pages, in, out);
150         if (err)
151                 return err;
152
153         *npages = MLX5_GET(query_pages_out, out, num_pages);
154         *func_id = MLX5_GET(query_pages_out, out, function_id);
155
156         return err;
157 }
158
159 static int alloc_4k(struct mlx5_core_dev *dev, u64 *addr)
160 {
161         struct fw_page *fp;
162         unsigned n;
163
164         if (list_empty(&dev->priv.free_list))
165                 return -ENOMEM;
166
167         fp = list_entry(dev->priv.free_list.next, struct fw_page, list);
168         n = find_first_bit(&fp->bitmask, 8 * sizeof(fp->bitmask));
169         if (n >= MLX5_NUM_4K_IN_PAGE) {
170                 mlx5_core_warn(dev, "alloc 4k bug\n");
171                 return -ENOENT;
172         }
173         clear_bit(n, &fp->bitmask);
174         fp->free_count--;
175         if (!fp->free_count)
176                 list_del(&fp->list);
177
178         *addr = fp->addr + n * MLX5_ADAPTER_PAGE_SIZE;
179
180         return 0;
181 }
182
183 #define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
184
185 static void free_fwp(struct mlx5_core_dev *dev, struct fw_page *fwp)
186 {
187         int n = (fwp->addr & ~MLX5_U64_4K_PAGE_MASK) >> MLX5_ADAPTER_PAGE_SHIFT;
188
189         fwp->free_count++;
190         set_bit(n, &fwp->bitmask);
191         if (fwp->free_count == MLX5_NUM_4K_IN_PAGE) {
192                 rb_erase(&fwp->rb_node, &dev->priv.page_root);
193                 if (fwp->free_count != 1)
194                         list_del(&fwp->list);
195                 dma_unmap_page(dev->device, fwp->addr & MLX5_U64_4K_PAGE_MASK,
196                                PAGE_SIZE, DMA_BIDIRECTIONAL);
197                 __free_page(fwp->page);
198                 kfree(fwp);
199         } else if (fwp->free_count == 1) {
200                 list_add(&fwp->list, &dev->priv.free_list);
201         }
202 }
203
204 static void free_addr(struct mlx5_core_dev *dev, u64 addr)
205 {
206         struct fw_page *fwp;
207
208         fwp = find_fw_page(dev, addr & MLX5_U64_4K_PAGE_MASK);
209         if (!fwp) {
210                 mlx5_core_warn_rl(dev, "page not found\n");
211                 return;
212         }
213         free_fwp(dev, fwp);
214 }
215
216 static int alloc_system_page(struct mlx5_core_dev *dev, u16 func_id)
217 {
218         struct device *device = dev->device;
219         int nid = dev_to_node(device);
220         struct page *page;
221         u64 zero_addr = 1;
222         u64 addr;
223         int err;
224
225         page = alloc_pages_node(nid, GFP_HIGHUSER, 0);
226         if (!page) {
227                 mlx5_core_warn(dev, "failed to allocate page\n");
228                 return -ENOMEM;
229         }
230 map:
231         addr = dma_map_page(device, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
232         if (dma_mapping_error(device, addr)) {
233                 mlx5_core_warn(dev, "failed dma mapping page\n");
234                 err = -ENOMEM;
235                 goto err_mapping;
236         }
237
238         /* Firmware doesn't support page with physical address 0 */
239         if (addr == 0) {
240                 zero_addr = addr;
241                 goto map;
242         }
243
244         err = insert_page(dev, addr, page, func_id);
245         if (err) {
246                 mlx5_core_err(dev, "failed to track allocated page\n");
247                 dma_unmap_page(device, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
248         }
249
250 err_mapping:
251         if (err)
252                 __free_page(page);
253
254         if (zero_addr == 0)
255                 dma_unmap_page(device, zero_addr, PAGE_SIZE,
256                                DMA_BIDIRECTIONAL);
257
258         return err;
259 }
260
261 static void page_notify_fail(struct mlx5_core_dev *dev, u16 func_id,
262                              bool ec_function)
263 {
264         u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
265         int err;
266
267         MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
268         MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_CANT_GIVE);
269         MLX5_SET(manage_pages_in, in, function_id, func_id);
270         MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
271
272         err = mlx5_cmd_exec_in(dev, manage_pages, in);
273         if (err)
274                 mlx5_core_warn(dev, "page notify failed func_id(%d) err(%d)\n",
275                                func_id, err);
276 }
277
278 static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
279                       int notify_fail, bool ec_function)
280 {
281         u32 out[MLX5_ST_SZ_DW(manage_pages_out)] = {0};
282         int inlen = MLX5_ST_SZ_BYTES(manage_pages_in);
283         u64 addr;
284         int err;
285         u32 *in;
286         int i;
287
288         inlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_in, pas[0]);
289         in = kvzalloc(inlen, GFP_KERNEL);
290         if (!in) {
291                 err = -ENOMEM;
292                 mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
293                 goto out_free;
294         }
295
296         for (i = 0; i < npages; i++) {
297 retry:
298                 err = alloc_4k(dev, &addr);
299                 if (err) {
300                         if (err == -ENOMEM)
301                                 err = alloc_system_page(dev, func_id);
302                         if (err)
303                                 goto out_4k;
304
305                         goto retry;
306                 }
307                 MLX5_ARRAY_SET64(manage_pages_in, in, pas, i, addr);
308         }
309
310         MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
311         MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_GIVE);
312         MLX5_SET(manage_pages_in, in, function_id, func_id);
313         MLX5_SET(manage_pages_in, in, input_num_entries, npages);
314         MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
315
316         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
317         if (err) {
318                 mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
319                                func_id, npages, err);
320                 goto out_4k;
321         }
322
323         dev->priv.fw_pages += npages;
324         if (func_id)
325                 dev->priv.vfs_pages += npages;
326         else if (mlx5_core_is_ecpf(dev) && !ec_function)
327                 dev->priv.peer_pf_pages += npages;
328
329         mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x, err %d\n",
330                       npages, ec_function, func_id, err);
331
332         kvfree(in);
333         return 0;
334
335 out_4k:
336         for (i--; i >= 0; i--)
337                 free_addr(dev, MLX5_GET64(manage_pages_in, in, pas[i]));
338 out_free:
339         kvfree(in);
340         if (notify_fail)
341                 page_notify_fail(dev, func_id, ec_function);
342         return err;
343 }
344
345 static void release_all_pages(struct mlx5_core_dev *dev, u32 func_id,
346                               bool ec_function)
347 {
348         struct rb_node *p;
349         int npages = 0;
350
351         p = rb_first(&dev->priv.page_root);
352         while (p) {
353                 struct fw_page *fwp = rb_entry(p, struct fw_page, rb_node);
354
355                 p = rb_next(p);
356                 if (fwp->func_id != func_id)
357                         continue;
358                 free_fwp(dev, fwp);
359                 npages++;
360         }
361
362         dev->priv.fw_pages -= npages;
363         if (func_id)
364                 dev->priv.vfs_pages -= npages;
365         else if (mlx5_core_is_ecpf(dev) && !ec_function)
366                 dev->priv.peer_pf_pages -= npages;
367
368         mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x\n",
369                       npages, ec_function, func_id);
370 }
371
372 static int reclaim_pages_cmd(struct mlx5_core_dev *dev,
373                              u32 *in, int in_size, u32 *out, int out_size)
374 {
375         struct fw_page *fwp;
376         struct rb_node *p;
377         u32 func_id;
378         u32 npages;
379         u32 i = 0;
380
381         if (dev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR)
382                 return mlx5_cmd_exec(dev, in, in_size, out, out_size);
383
384         /* No hard feelings, we want our pages back! */
385         npages = MLX5_GET(manage_pages_in, in, input_num_entries);
386         func_id = MLX5_GET(manage_pages_in, in, function_id);
387
388         p = rb_first(&dev->priv.page_root);
389         while (p && i < npages) {
390                 fwp = rb_entry(p, struct fw_page, rb_node);
391                 p = rb_next(p);
392                 if (fwp->func_id != func_id)
393                         continue;
394
395                 MLX5_ARRAY_SET64(manage_pages_out, out, pas, i, fwp->addr);
396                 i++;
397         }
398
399         MLX5_SET(manage_pages_out, out, output_num_entries, i);
400         return 0;
401 }
402
403 static int reclaim_pages(struct mlx5_core_dev *dev, u32 func_id, int npages,
404                          int *nclaimed, bool ec_function)
405 {
406         int outlen = MLX5_ST_SZ_BYTES(manage_pages_out);
407         u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
408         int num_claimed;
409         u32 *out;
410         int err;
411         int i;
412
413         if (nclaimed)
414                 *nclaimed = 0;
415
416         outlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
417         out = kvzalloc(outlen, GFP_KERNEL);
418         if (!out)
419                 return -ENOMEM;
420
421         MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
422         MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_TAKE);
423         MLX5_SET(manage_pages_in, in, function_id, func_id);
424         MLX5_SET(manage_pages_in, in, input_num_entries, npages);
425         MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
426
427         mlx5_core_dbg(dev, "npages %d, outlen %d\n", npages, outlen);
428         err = reclaim_pages_cmd(dev, in, sizeof(in), out, outlen);
429         if (err) {
430                 mlx5_core_err(dev, "failed reclaiming pages: err %d\n", err);
431                 goto out_free;
432         }
433
434         num_claimed = MLX5_GET(manage_pages_out, out, output_num_entries);
435         if (num_claimed > npages) {
436                 mlx5_core_warn(dev, "fw returned %d, driver asked %d => corruption\n",
437                                num_claimed, npages);
438                 err = -EINVAL;
439                 goto out_free;
440         }
441
442         for (i = 0; i < num_claimed; i++)
443                 free_addr(dev, MLX5_GET64(manage_pages_out, out, pas[i]));
444
445         if (nclaimed)
446                 *nclaimed = num_claimed;
447
448         dev->priv.fw_pages -= num_claimed;
449         if (func_id)
450                 dev->priv.vfs_pages -= num_claimed;
451         else if (mlx5_core_is_ecpf(dev) && !ec_function)
452                 dev->priv.peer_pf_pages -= num_claimed;
453
454 out_free:
455         kvfree(out);
456         return err;
457 }
458
459 static void pages_work_handler(struct work_struct *work)
460 {
461         struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
462         struct mlx5_core_dev *dev = req->dev;
463         int err = 0;
464
465         if (req->release_all)
466                 release_all_pages(dev, req->func_id, req->ec_function);
467         else if (req->npages < 0)
468                 err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL,
469                                     req->ec_function);
470         else if (req->npages > 0)
471                 err = give_pages(dev, req->func_id, req->npages, 1, req->ec_function);
472
473         if (err)
474                 mlx5_core_warn(dev, "%s fail %d\n",
475                                req->npages < 0 ? "reclaim" : "give", err);
476
477         kfree(req);
478 }
479
480 enum {
481         EC_FUNCTION_MASK = 0x8000,
482         RELEASE_ALL_PAGES_MASK = 0x4000,
483 };
484
485 static int req_pages_handler(struct notifier_block *nb,
486                              unsigned long type, void *data)
487 {
488         struct mlx5_pages_req *req;
489         struct mlx5_core_dev *dev;
490         struct mlx5_priv *priv;
491         struct mlx5_eqe *eqe;
492         bool ec_function;
493         bool release_all;
494         u16 func_id;
495         s32 npages;
496
497         priv = mlx5_nb_cof(nb, struct mlx5_priv, pg_nb);
498         dev  = container_of(priv, struct mlx5_core_dev, priv);
499         eqe  = data;
500
501         func_id = be16_to_cpu(eqe->data.req_pages.func_id);
502         npages  = be32_to_cpu(eqe->data.req_pages.num_pages);
503         ec_function = be16_to_cpu(eqe->data.req_pages.ec_function) & EC_FUNCTION_MASK;
504         release_all = be16_to_cpu(eqe->data.req_pages.ec_function) &
505                       RELEASE_ALL_PAGES_MASK;
506         mlx5_core_dbg(dev, "page request for func 0x%x, npages %d, release_all %d\n",
507                       func_id, npages, release_all);
508         req = kzalloc(sizeof(*req), GFP_ATOMIC);
509         if (!req) {
510                 mlx5_core_warn(dev, "failed to allocate pages request\n");
511                 return NOTIFY_DONE;
512         }
513
514         req->dev = dev;
515         req->func_id = func_id;
516         req->npages = npages;
517         req->ec_function = ec_function;
518         req->release_all = release_all;
519         INIT_WORK(&req->work, pages_work_handler);
520         queue_work(dev->priv.pg_wq, &req->work);
521         return NOTIFY_OK;
522 }
523
524 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
525 {
526         u16 uninitialized_var(func_id);
527         s32 uninitialized_var(npages);
528         int err;
529
530         err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
531         if (err)
532                 return err;
533
534         mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
535                       npages, boot ? "boot" : "init", func_id);
536
537         return give_pages(dev, func_id, npages, 0, mlx5_core_is_ecpf(dev));
538 }
539
540 enum {
541         MLX5_BLKS_FOR_RECLAIM_PAGES = 12
542 };
543
544 static int optimal_reclaimed_pages(void)
545 {
546         struct mlx5_cmd_prot_block *block;
547         struct mlx5_cmd_layout *lay;
548         int ret;
549
550         ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
551                MLX5_ST_SZ_BYTES(manage_pages_out)) /
552                MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
553
554         return ret;
555 }
556
557 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
558 {
559         unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
560         struct fw_page *fwp;
561         struct rb_node *p;
562         int nclaimed = 0;
563         int err = 0;
564
565         do {
566                 p = rb_first(&dev->priv.page_root);
567                 if (p) {
568                         fwp = rb_entry(p, struct fw_page, rb_node);
569                         err = reclaim_pages(dev, fwp->func_id,
570                                             optimal_reclaimed_pages(),
571                                             &nclaimed, mlx5_core_is_ecpf(dev));
572
573                         if (err) {
574                                 mlx5_core_warn(dev, "failed reclaiming pages (%d)\n",
575                                                err);
576                                 return err;
577                         }
578                         if (nclaimed)
579                                 end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
580                 }
581                 if (time_after(jiffies, end)) {
582                         mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
583                         break;
584                 }
585         } while (p);
586
587         WARN(dev->priv.fw_pages,
588              "FW pages counter is %d after reclaiming all pages\n",
589              dev->priv.fw_pages);
590         WARN(dev->priv.vfs_pages,
591              "VFs FW pages counter is %d after reclaiming all pages\n",
592              dev->priv.vfs_pages);
593         WARN(dev->priv.peer_pf_pages,
594              "Peer PF FW pages counter is %d after reclaiming all pages\n",
595              dev->priv.peer_pf_pages);
596
597         return 0;
598 }
599
600 int mlx5_pagealloc_init(struct mlx5_core_dev *dev)
601 {
602         dev->priv.page_root = RB_ROOT;
603         INIT_LIST_HEAD(&dev->priv.free_list);
604         dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
605         if (!dev->priv.pg_wq)
606                 return -ENOMEM;
607
608         return 0;
609 }
610
611 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
612 {
613         destroy_workqueue(dev->priv.pg_wq);
614 }
615
616 void mlx5_pagealloc_start(struct mlx5_core_dev *dev)
617 {
618         MLX5_NB_INIT(&dev->priv.pg_nb, req_pages_handler, PAGE_REQUEST);
619         mlx5_eq_notifier_register(dev, &dev->priv.pg_nb);
620 }
621
622 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
623 {
624         mlx5_eq_notifier_unregister(dev, &dev->priv.pg_nb);
625         flush_workqueue(dev->priv.pg_wq);
626 }
627
628 int mlx5_wait_for_pages(struct mlx5_core_dev *dev, int *pages)
629 {
630         unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS);
631         int prev_pages = *pages;
632
633         /* In case of internal error we will free the pages manually later */
634         if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
635                 mlx5_core_warn(dev, "Skipping wait for vf pages stage");
636                 return 0;
637         }
638
639         mlx5_core_dbg(dev, "Waiting for %d pages\n", prev_pages);
640         while (*pages) {
641                 if (time_after(jiffies, end)) {
642                         mlx5_core_warn(dev, "aborting while there are %d pending pages\n", *pages);
643                         return -ETIMEDOUT;
644                 }
645                 if (*pages < prev_pages) {
646                         end = jiffies + msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS);
647                         prev_pages = *pages;
648                 }
649                 msleep(50);
650         }
651
652         mlx5_core_dbg(dev, "All pages received\n");
653         return 0;
654 }