2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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/xarray.h>
39 #include "mlx5_core.h"
43 MLX5_PAGES_CANT_GIVE = 0,
48 struct mlx5_pages_req {
49 struct mlx5_core_dev *dev;
53 struct work_struct work;
58 struct rb_node rb_node;
62 unsigned long bitmask;
63 struct list_head list;
68 MAX_RECLAIM_TIME_MSECS = 5000,
69 MAX_RECLAIM_VFS_PAGES_TIME_MSECS = 2 * 1000 * 60,
73 MLX5_MAX_RECLAIM_TIME_MILI = 5000,
74 MLX5_NUM_4K_IN_PAGE = PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
77 static struct rb_root *page_root_per_func_id(struct mlx5_core_dev *dev, u16 func_id)
82 root = xa_load(&dev->priv.page_root_xa, func_id);
86 root = kzalloc(sizeof(*root), GFP_KERNEL);
88 return ERR_PTR(-ENOMEM);
90 err = xa_insert(&dev->priv.page_root_xa, func_id, root, GFP_KERNEL);
101 static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u16 func_id)
103 struct rb_node *parent = NULL;
104 struct rb_root *root;
105 struct rb_node **new;
110 root = page_root_per_func_id(dev, func_id);
112 return PTR_ERR(root);
114 new = &root->rb_node;
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;
127 nfp = kzalloc(sizeof(*nfp), GFP_KERNEL);
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);
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);
145 static struct fw_page *find_fw_page(struct mlx5_core_dev *dev, u64 addr,
148 struct fw_page *result = NULL;
149 struct rb_root *root;
153 root = xa_load(&dev->priv.page_root_xa, func_id);
154 if (WARN_ON_ONCE(!root))
160 tfp = rb_entry(tmp, struct fw_page, rb_node);
161 if (tfp->addr < addr) {
163 } else if (tfp->addr > addr) {
174 static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
175 s32 *npages, int boot)
177 u32 out[MLX5_ST_SZ_DW(query_pages_out)] = {};
178 u32 in[MLX5_ST_SZ_DW(query_pages_in)] = {};
181 MLX5_SET(query_pages_in, in, opcode, MLX5_CMD_OP_QUERY_PAGES);
182 MLX5_SET(query_pages_in, in, op_mod, boot ?
183 MLX5_QUERY_PAGES_IN_OP_MOD_BOOT_PAGES :
184 MLX5_QUERY_PAGES_IN_OP_MOD_INIT_PAGES);
185 MLX5_SET(query_pages_in, in, embedded_cpu_function, mlx5_core_is_ecpf(dev));
187 err = mlx5_cmd_exec_inout(dev, query_pages, in, out);
191 *npages = MLX5_GET(query_pages_out, out, num_pages);
192 *func_id = MLX5_GET(query_pages_out, out, function_id);
197 static int alloc_4k(struct mlx5_core_dev *dev, u64 *addr, u16 func_id)
199 struct fw_page *fp = NULL;
200 struct fw_page *iter;
203 list_for_each_entry(iter, &dev->priv.free_list, list) {
204 if (iter->func_id != func_id)
209 if (list_empty(&dev->priv.free_list) || !fp)
212 n = find_first_bit(&fp->bitmask, 8 * sizeof(fp->bitmask));
213 if (n >= MLX5_NUM_4K_IN_PAGE) {
214 mlx5_core_warn(dev, "alloc 4k bug\n");
217 clear_bit(n, &fp->bitmask);
222 *addr = fp->addr + n * MLX5_ADAPTER_PAGE_SIZE;
227 #define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
229 static void free_fwp(struct mlx5_core_dev *dev, struct fw_page *fwp,
232 struct rb_root *root;
234 root = xa_load(&dev->priv.page_root_xa, fwp->func_id);
235 if (WARN_ON_ONCE(!root))
238 rb_erase(&fwp->rb_node, root);
240 list_del(&fwp->list);
241 dma_unmap_page(mlx5_core_dma_dev(dev), fwp->addr & MLX5_U64_4K_PAGE_MASK,
242 PAGE_SIZE, DMA_BIDIRECTIONAL);
243 __free_page(fwp->page);
247 static void free_4k(struct mlx5_core_dev *dev, u64 addr, u32 func_id)
252 fwp = find_fw_page(dev, addr & MLX5_U64_4K_PAGE_MASK, func_id);
254 mlx5_core_warn_rl(dev, "page not found\n");
257 n = (addr & ~MLX5_U64_4K_PAGE_MASK) >> MLX5_ADAPTER_PAGE_SHIFT;
259 set_bit(n, &fwp->bitmask);
260 if (fwp->free_count == MLX5_NUM_4K_IN_PAGE)
261 free_fwp(dev, fwp, fwp->free_count != 1);
262 else if (fwp->free_count == 1)
263 list_add(&fwp->list, &dev->priv.free_list);
266 static int alloc_system_page(struct mlx5_core_dev *dev, u16 func_id)
268 struct device *device = mlx5_core_dma_dev(dev);
269 int nid = dev_to_node(device);
275 page = alloc_pages_node(nid, GFP_HIGHUSER, 0);
277 mlx5_core_warn(dev, "failed to allocate page\n");
281 addr = dma_map_page(device, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
282 if (dma_mapping_error(device, addr)) {
283 mlx5_core_warn(dev, "failed dma mapping page\n");
288 /* Firmware doesn't support page with physical address 0 */
294 err = insert_page(dev, addr, page, func_id);
296 mlx5_core_err(dev, "failed to track allocated page\n");
297 dma_unmap_page(device, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
305 dma_unmap_page(device, zero_addr, PAGE_SIZE,
311 static void page_notify_fail(struct mlx5_core_dev *dev, u16 func_id,
314 u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
317 MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
318 MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_CANT_GIVE);
319 MLX5_SET(manage_pages_in, in, function_id, func_id);
320 MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
322 err = mlx5_cmd_exec_in(dev, manage_pages, in);
324 mlx5_core_warn(dev, "page notify failed func_id(%d) err(%d)\n",
328 static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
329 int notify_fail, bool ec_function)
331 u32 out[MLX5_ST_SZ_DW(manage_pages_out)] = {0};
332 int inlen = MLX5_ST_SZ_BYTES(manage_pages_in);
338 inlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_in, pas[0]);
339 in = kvzalloc(inlen, GFP_KERNEL);
342 mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
346 for (i = 0; i < npages; i++) {
348 err = alloc_4k(dev, &addr, func_id);
351 err = alloc_system_page(dev, func_id);
357 MLX5_ARRAY_SET64(manage_pages_in, in, pas, i, addr);
360 MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
361 MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_GIVE);
362 MLX5_SET(manage_pages_in, in, function_id, func_id);
363 MLX5_SET(manage_pages_in, in, input_num_entries, npages);
364 MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
366 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
368 mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
369 func_id, npages, err);
373 dev->priv.fw_pages += npages;
375 dev->priv.vfs_pages += npages;
376 else if (mlx5_core_is_ecpf(dev) && !ec_function)
377 dev->priv.peer_pf_pages += npages;
379 mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x, err %d\n",
380 npages, ec_function, func_id, err);
386 for (i--; i >= 0; i--)
387 free_4k(dev, MLX5_GET64(manage_pages_in, in, pas[i]), func_id);
391 page_notify_fail(dev, func_id, ec_function);
395 static void release_all_pages(struct mlx5_core_dev *dev, u32 func_id,
398 struct rb_root *root;
402 root = xa_load(&dev->priv.page_root_xa, func_id);
403 if (WARN_ON_ONCE(!root))
408 struct fw_page *fwp = rb_entry(p, struct fw_page, rb_node);
411 npages += (MLX5_NUM_4K_IN_PAGE - fwp->free_count);
412 free_fwp(dev, fwp, fwp->free_count);
415 dev->priv.fw_pages -= npages;
417 dev->priv.vfs_pages -= npages;
418 else if (mlx5_core_is_ecpf(dev) && !ec_function)
419 dev->priv.peer_pf_pages -= npages;
421 mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x\n",
422 npages, ec_function, func_id);
425 static int reclaim_pages_cmd(struct mlx5_core_dev *dev,
426 u32 *in, int in_size, u32 *out, int out_size)
428 struct rb_root *root;
435 if (!mlx5_cmd_is_down(dev))
436 return mlx5_cmd_exec(dev, in, in_size, out, out_size);
438 /* No hard feelings, we want our pages back! */
439 npages = MLX5_GET(manage_pages_in, in, input_num_entries);
440 func_id = MLX5_GET(manage_pages_in, in, function_id);
442 root = xa_load(&dev->priv.page_root_xa, func_id);
443 if (WARN_ON_ONCE(!root))
447 while (p && i < npages) {
448 fwp = rb_entry(p, struct fw_page, rb_node);
451 MLX5_ARRAY_SET64(manage_pages_out, out, pas, i, fwp->addr);
455 MLX5_SET(manage_pages_out, out, output_num_entries, i);
459 static int reclaim_pages(struct mlx5_core_dev *dev, u32 func_id, int npages,
460 int *nclaimed, bool ec_function)
462 int outlen = MLX5_ST_SZ_BYTES(manage_pages_out);
463 u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
472 outlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
473 out = kvzalloc(outlen, GFP_KERNEL);
477 MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
478 MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_TAKE);
479 MLX5_SET(manage_pages_in, in, function_id, func_id);
480 MLX5_SET(manage_pages_in, in, input_num_entries, npages);
481 MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
483 mlx5_core_dbg(dev, "func 0x%x, npages %d, outlen %d\n",
484 func_id, npages, outlen);
485 err = reclaim_pages_cmd(dev, in, sizeof(in), out, outlen);
487 mlx5_core_err(dev, "failed reclaiming pages: err %d\n", err);
491 num_claimed = MLX5_GET(manage_pages_out, out, output_num_entries);
492 if (num_claimed > npages) {
493 mlx5_core_warn(dev, "fw returned %d, driver asked %d => corruption\n",
494 num_claimed, npages);
499 for (i = 0; i < num_claimed; i++)
500 free_4k(dev, MLX5_GET64(manage_pages_out, out, pas[i]), func_id);
503 *nclaimed = num_claimed;
505 dev->priv.fw_pages -= num_claimed;
507 dev->priv.vfs_pages -= num_claimed;
508 else if (mlx5_core_is_ecpf(dev) && !ec_function)
509 dev->priv.peer_pf_pages -= num_claimed;
516 static void pages_work_handler(struct work_struct *work)
518 struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
519 struct mlx5_core_dev *dev = req->dev;
522 if (req->release_all)
523 release_all_pages(dev, req->func_id, req->ec_function);
524 else if (req->npages < 0)
525 err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL,
527 else if (req->npages > 0)
528 err = give_pages(dev, req->func_id, req->npages, 1, req->ec_function);
531 mlx5_core_warn(dev, "%s fail %d\n",
532 req->npages < 0 ? "reclaim" : "give", err);
538 EC_FUNCTION_MASK = 0x8000,
539 RELEASE_ALL_PAGES_MASK = 0x4000,
542 static int req_pages_handler(struct notifier_block *nb,
543 unsigned long type, void *data)
545 struct mlx5_pages_req *req;
546 struct mlx5_core_dev *dev;
547 struct mlx5_priv *priv;
548 struct mlx5_eqe *eqe;
554 priv = mlx5_nb_cof(nb, struct mlx5_priv, pg_nb);
555 dev = container_of(priv, struct mlx5_core_dev, priv);
558 func_id = be16_to_cpu(eqe->data.req_pages.func_id);
559 npages = be32_to_cpu(eqe->data.req_pages.num_pages);
560 ec_function = be16_to_cpu(eqe->data.req_pages.ec_function) & EC_FUNCTION_MASK;
561 release_all = be16_to_cpu(eqe->data.req_pages.ec_function) &
562 RELEASE_ALL_PAGES_MASK;
563 mlx5_core_dbg(dev, "page request for func 0x%x, npages %d, release_all %d\n",
564 func_id, npages, release_all);
565 req = kzalloc(sizeof(*req), GFP_ATOMIC);
567 mlx5_core_warn(dev, "failed to allocate pages request\n");
572 req->func_id = func_id;
573 req->npages = npages;
574 req->ec_function = ec_function;
575 req->release_all = release_all;
576 INIT_WORK(&req->work, pages_work_handler);
577 queue_work(dev->priv.pg_wq, &req->work);
581 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
587 err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
591 mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
592 npages, boot ? "boot" : "init", func_id);
594 return give_pages(dev, func_id, npages, 0, mlx5_core_is_ecpf(dev));
598 MLX5_BLKS_FOR_RECLAIM_PAGES = 12
601 static int optimal_reclaimed_pages(void)
603 struct mlx5_cmd_prot_block *block;
604 struct mlx5_cmd_layout *lay;
607 ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
608 MLX5_ST_SZ_BYTES(manage_pages_out)) /
609 MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
614 static int mlx5_reclaim_root_pages(struct mlx5_core_dev *dev,
615 struct rb_root *root, u16 func_id)
617 unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
619 while (!RB_EMPTY_ROOT(root)) {
623 err = reclaim_pages(dev, func_id, optimal_reclaimed_pages(),
624 &nclaimed, mlx5_core_is_ecpf(dev));
626 mlx5_core_warn(dev, "failed reclaiming pages (%d) for func id 0x%x\n",
632 end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
634 if (time_after(jiffies, end)) {
635 mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
643 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
645 struct rb_root *root;
649 xa_for_each(&dev->priv.page_root_xa, id, entry) {
651 mlx5_reclaim_root_pages(dev, root, id);
652 xa_erase(&dev->priv.page_root_xa, id);
656 WARN_ON(!xa_empty(&dev->priv.page_root_xa));
658 WARN(dev->priv.fw_pages,
659 "FW pages counter is %d after reclaiming all pages\n",
661 WARN(dev->priv.vfs_pages,
662 "VFs FW pages counter is %d after reclaiming all pages\n",
663 dev->priv.vfs_pages);
664 WARN(dev->priv.peer_pf_pages,
665 "Peer PF FW pages counter is %d after reclaiming all pages\n",
666 dev->priv.peer_pf_pages);
671 int mlx5_pagealloc_init(struct mlx5_core_dev *dev)
673 INIT_LIST_HEAD(&dev->priv.free_list);
674 dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
675 if (!dev->priv.pg_wq)
678 xa_init(&dev->priv.page_root_xa);
683 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
685 xa_destroy(&dev->priv.page_root_xa);
686 destroy_workqueue(dev->priv.pg_wq);
689 void mlx5_pagealloc_start(struct mlx5_core_dev *dev)
691 MLX5_NB_INIT(&dev->priv.pg_nb, req_pages_handler, PAGE_REQUEST);
692 mlx5_eq_notifier_register(dev, &dev->priv.pg_nb);
695 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
697 mlx5_eq_notifier_unregister(dev, &dev->priv.pg_nb);
698 flush_workqueue(dev->priv.pg_wq);
701 int mlx5_wait_for_pages(struct mlx5_core_dev *dev, int *pages)
703 unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS);
704 int prev_pages = *pages;
706 /* In case of internal error we will free the pages manually later */
707 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
708 mlx5_core_warn(dev, "Skipping wait for vf pages stage");
712 mlx5_core_dbg(dev, "Waiting for %d pages\n", prev_pages);
714 if (time_after(jiffies, end)) {
715 mlx5_core_warn(dev, "aborting while there are %d pending pages\n", *pages);
718 if (*pages < prev_pages) {
719 end = jiffies + msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS);
725 mlx5_core_dbg(dev, "All pages received\n");