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/delay.h>
36 #include <linux/mlx5/driver.h>
37 #include <linux/xarray.h>
38 #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;
64 unsigned int free_count;
68 MLX5_MAX_RECLAIM_TIME_MILI = 5000,
69 MLX5_NUM_4K_IN_PAGE = PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
72 static u32 get_function(u16 func_id, bool ec_function)
74 return (u32)func_id | (ec_function << 16);
77 static struct rb_root *page_root_per_function(struct mlx5_core_dev *dev, u32 function)
82 root = xa_load(&dev->priv.page_root_xa, function);
86 root = kzalloc(sizeof(*root), GFP_KERNEL);
88 return ERR_PTR(-ENOMEM);
90 err = xa_insert(&dev->priv.page_root_xa, function, root, GFP_KERNEL);
101 static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u32 function)
103 struct rb_node *parent = NULL;
104 struct rb_root *root;
105 struct rb_node **new;
110 root = page_root_per_function(dev, function);
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->function = function;
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, function);
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, u32 function)
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->function != function)
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: fw page = 0x%llx, n = %u, bitmask: %lu, max num of 4K pages: %d\n",
215 fp->addr, n, fp->bitmask, MLX5_NUM_4K_IN_PAGE);
218 clear_bit(n, &fp->bitmask);
223 *addr = fp->addr + n * MLX5_ADAPTER_PAGE_SIZE;
228 #define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
230 static void free_fwp(struct mlx5_core_dev *dev, struct fw_page *fwp,
233 struct rb_root *root;
235 root = xa_load(&dev->priv.page_root_xa, fwp->function);
236 if (WARN_ON_ONCE(!root))
239 rb_erase(&fwp->rb_node, root);
241 list_del(&fwp->list);
242 dma_unmap_page(mlx5_core_dma_dev(dev), fwp->addr & MLX5_U64_4K_PAGE_MASK,
243 PAGE_SIZE, DMA_BIDIRECTIONAL);
244 __free_page(fwp->page);
248 static void free_4k(struct mlx5_core_dev *dev, u64 addr, u32 function)
253 fwp = find_fw_page(dev, addr & MLX5_U64_4K_PAGE_MASK, function);
255 mlx5_core_warn_rl(dev, "page not found\n");
258 n = (addr & ~MLX5_U64_4K_PAGE_MASK) >> MLX5_ADAPTER_PAGE_SHIFT;
260 set_bit(n, &fwp->bitmask);
261 if (fwp->free_count == MLX5_NUM_4K_IN_PAGE)
262 free_fwp(dev, fwp, fwp->free_count != 1);
263 else if (fwp->free_count == 1)
264 list_add(&fwp->list, &dev->priv.free_list);
267 static int alloc_system_page(struct mlx5_core_dev *dev, u32 function)
269 struct device *device = mlx5_core_dma_dev(dev);
270 int nid = dev_to_node(device);
276 page = alloc_pages_node(nid, GFP_HIGHUSER, 0);
278 mlx5_core_warn(dev, "failed to allocate page\n");
282 addr = dma_map_page(device, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
283 if (dma_mapping_error(device, addr)) {
284 mlx5_core_warn(dev, "failed dma mapping page\n");
289 /* Firmware doesn't support page with physical address 0 */
295 err = insert_page(dev, addr, page, function);
297 mlx5_core_err(dev, "failed to track allocated page\n");
298 dma_unmap_page(device, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
306 dma_unmap_page(device, zero_addr, PAGE_SIZE,
312 static void page_notify_fail(struct mlx5_core_dev *dev, u16 func_id,
315 u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
318 MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
319 MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_CANT_GIVE);
320 MLX5_SET(manage_pages_in, in, function_id, func_id);
321 MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
323 err = mlx5_cmd_exec_in(dev, manage_pages, in);
325 mlx5_core_warn(dev, "page notify failed func_id(%d) err(%d)\n",
329 static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
330 int event, bool ec_function)
332 u32 function = get_function(func_id, ec_function);
333 u32 out[MLX5_ST_SZ_DW(manage_pages_out)] = {0};
334 int inlen = MLX5_ST_SZ_BYTES(manage_pages_in);
335 int notify_fail = event;
341 inlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_in, pas[0]);
342 in = kvzalloc(inlen, GFP_KERNEL);
345 mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
349 for (i = 0; i < npages; i++) {
351 err = alloc_4k(dev, &addr, function);
354 err = alloc_system_page(dev, function);
356 dev->priv.fw_pages_alloc_failed += (npages - i);
362 MLX5_ARRAY_SET64(manage_pages_in, in, pas, i, addr);
365 MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
366 MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_GIVE);
367 MLX5_SET(manage_pages_in, in, function_id, func_id);
368 MLX5_SET(manage_pages_in, in, input_num_entries, npages);
369 MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
371 err = mlx5_cmd_do(dev, in, inlen, out, sizeof(out));
372 if (err == -EREMOTEIO) {
374 /* if triggered by FW and failed by FW ignore */
380 err = mlx5_cmd_check(dev, err, in, out);
382 mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
383 func_id, npages, err);
387 dev->priv.fw_pages += npages;
389 dev->priv.vfs_pages += npages;
390 else if (mlx5_core_is_ecpf(dev) && !ec_function)
391 dev->priv.host_pf_pages += npages;
393 mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x, err %d\n",
394 npages, ec_function, func_id, err);
400 dev->priv.give_pages_dropped += npages;
402 for (i--; i >= 0; i--)
403 free_4k(dev, MLX5_GET64(manage_pages_in, in, pas[i]), function);
407 page_notify_fail(dev, func_id, ec_function);
411 static void release_all_pages(struct mlx5_core_dev *dev, u16 func_id,
414 u32 function = get_function(func_id, ec_function);
415 struct rb_root *root;
419 root = xa_load(&dev->priv.page_root_xa, function);
420 if (WARN_ON_ONCE(!root))
425 struct fw_page *fwp = rb_entry(p, struct fw_page, rb_node);
428 npages += (MLX5_NUM_4K_IN_PAGE - fwp->free_count);
429 free_fwp(dev, fwp, fwp->free_count);
432 dev->priv.fw_pages -= npages;
434 dev->priv.vfs_pages -= npages;
435 else if (mlx5_core_is_ecpf(dev) && !ec_function)
436 dev->priv.host_pf_pages -= npages;
438 mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x\n",
439 npages, ec_function, func_id);
442 static u32 fwp_fill_manage_pages_out(struct fw_page *fwp, u32 *out, u32 index,
448 for_each_clear_bit(n, &fwp->bitmask, MLX5_NUM_4K_IN_PAGE) {
449 MLX5_ARRAY_SET64(manage_pages_out, out, pas, index + pages_set,
450 fwp->addr + (n * MLX5_ADAPTER_PAGE_SIZE));
460 static int reclaim_pages_cmd(struct mlx5_core_dev *dev,
461 u32 *in, int in_size, u32 *out, int out_size)
463 struct rb_root *root;
471 if (!mlx5_cmd_is_down(dev))
472 return mlx5_cmd_do(dev, in, in_size, out, out_size);
474 /* No hard feelings, we want our pages back! */
475 npages = MLX5_GET(manage_pages_in, in, input_num_entries);
476 func_id = MLX5_GET(manage_pages_in, in, function_id);
477 ec_function = MLX5_GET(manage_pages_in, in, embedded_cpu_function);
479 root = xa_load(&dev->priv.page_root_xa, get_function(func_id, ec_function));
480 if (WARN_ON_ONCE(!root))
484 while (p && i < npages) {
485 fwp = rb_entry(p, struct fw_page, rb_node);
488 i += fwp_fill_manage_pages_out(fwp, out, i, npages - i);
491 MLX5_SET(manage_pages_out, out, output_num_entries, i);
495 static int reclaim_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
496 int *nclaimed, bool event, bool ec_function)
498 u32 function = get_function(func_id, ec_function);
499 int outlen = MLX5_ST_SZ_BYTES(manage_pages_out);
500 u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
509 outlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
510 out = kvzalloc(outlen, GFP_KERNEL);
514 MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
515 MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_TAKE);
516 MLX5_SET(manage_pages_in, in, function_id, func_id);
517 MLX5_SET(manage_pages_in, in, input_num_entries, npages);
518 MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
520 mlx5_core_dbg(dev, "func 0x%x, npages %d, outlen %d\n",
521 func_id, npages, outlen);
522 err = reclaim_pages_cmd(dev, in, sizeof(in), out, outlen);
524 npages = MLX5_GET(manage_pages_in, in, input_num_entries);
525 dev->priv.reclaim_pages_discard += npages;
527 /* if triggered by FW event and failed by FW then ignore */
528 if (event && err == -EREMOTEIO) {
533 err = mlx5_cmd_check(dev, err, in, out);
535 mlx5_core_err(dev, "failed reclaiming pages: err %d\n", err);
539 num_claimed = MLX5_GET(manage_pages_out, out, output_num_entries);
540 if (num_claimed > npages) {
541 mlx5_core_warn(dev, "fw returned %d, driver asked %d => corruption\n",
542 num_claimed, npages);
547 for (i = 0; i < num_claimed; i++)
548 free_4k(dev, MLX5_GET64(manage_pages_out, out, pas[i]), function);
551 *nclaimed = num_claimed;
553 dev->priv.fw_pages -= num_claimed;
555 dev->priv.vfs_pages -= num_claimed;
556 else if (mlx5_core_is_ecpf(dev) && !ec_function)
557 dev->priv.host_pf_pages -= num_claimed;
564 static void pages_work_handler(struct work_struct *work)
566 struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
567 struct mlx5_core_dev *dev = req->dev;
570 if (req->release_all)
571 release_all_pages(dev, req->func_id, req->ec_function);
572 else if (req->npages < 0)
573 err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL,
574 true, req->ec_function);
575 else if (req->npages > 0)
576 err = give_pages(dev, req->func_id, req->npages, 1, req->ec_function);
579 mlx5_core_warn(dev, "%s fail %d\n",
580 req->npages < 0 ? "reclaim" : "give", err);
586 EC_FUNCTION_MASK = 0x8000,
587 RELEASE_ALL_PAGES_MASK = 0x4000,
590 static int req_pages_handler(struct notifier_block *nb,
591 unsigned long type, void *data)
593 struct mlx5_pages_req *req;
594 struct mlx5_core_dev *dev;
595 struct mlx5_priv *priv;
596 struct mlx5_eqe *eqe;
602 priv = mlx5_nb_cof(nb, struct mlx5_priv, pg_nb);
603 dev = container_of(priv, struct mlx5_core_dev, priv);
606 func_id = be16_to_cpu(eqe->data.req_pages.func_id);
607 npages = be32_to_cpu(eqe->data.req_pages.num_pages);
608 ec_function = be16_to_cpu(eqe->data.req_pages.ec_function) & EC_FUNCTION_MASK;
609 release_all = be16_to_cpu(eqe->data.req_pages.ec_function) &
610 RELEASE_ALL_PAGES_MASK;
611 mlx5_core_dbg(dev, "page request for func 0x%x, npages %d, release_all %d\n",
612 func_id, npages, release_all);
613 req = kzalloc(sizeof(*req), GFP_ATOMIC);
615 mlx5_core_warn(dev, "failed to allocate pages request\n");
620 req->func_id = func_id;
621 req->npages = npages;
622 req->ec_function = ec_function;
623 req->release_all = release_all;
624 INIT_WORK(&req->work, pages_work_handler);
625 queue_work(dev->priv.pg_wq, &req->work);
629 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
635 err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
639 mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
640 npages, boot ? "boot" : "init", func_id);
642 return give_pages(dev, func_id, npages, 0, mlx5_core_is_ecpf(dev));
646 MLX5_BLKS_FOR_RECLAIM_PAGES = 12
649 static int optimal_reclaimed_pages(void)
651 struct mlx5_cmd_prot_block *block;
652 struct mlx5_cmd_layout *lay;
655 ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
656 MLX5_ST_SZ_BYTES(manage_pages_out)) /
657 MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
662 static int mlx5_reclaim_root_pages(struct mlx5_core_dev *dev,
663 struct rb_root *root, u16 func_id)
665 u64 recl_pages_to_jiffies = msecs_to_jiffies(mlx5_tout_ms(dev, RECLAIM_PAGES));
666 unsigned long end = jiffies + recl_pages_to_jiffies;
668 while (!RB_EMPTY_ROOT(root)) {
672 err = reclaim_pages(dev, func_id, optimal_reclaimed_pages(),
673 &nclaimed, false, mlx5_core_is_ecpf(dev));
675 mlx5_core_warn(dev, "failed reclaiming pages (%d) for func id 0x%x\n",
681 end = jiffies + recl_pages_to_jiffies;
683 if (time_after(jiffies, end)) {
684 mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
692 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
694 struct rb_root *root;
698 xa_for_each(&dev->priv.page_root_xa, id, entry) {
700 mlx5_reclaim_root_pages(dev, root, id);
701 xa_erase(&dev->priv.page_root_xa, id);
705 WARN_ON(!xa_empty(&dev->priv.page_root_xa));
707 WARN(dev->priv.fw_pages,
708 "FW pages counter is %d after reclaiming all pages\n",
710 WARN(dev->priv.vfs_pages,
711 "VFs FW pages counter is %d after reclaiming all pages\n",
712 dev->priv.vfs_pages);
713 WARN(dev->priv.host_pf_pages,
714 "External host PF FW pages counter is %d after reclaiming all pages\n",
715 dev->priv.host_pf_pages);
720 int mlx5_pagealloc_init(struct mlx5_core_dev *dev)
722 INIT_LIST_HEAD(&dev->priv.free_list);
723 dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
724 if (!dev->priv.pg_wq)
727 xa_init(&dev->priv.page_root_xa);
728 mlx5_pages_debugfs_init(dev);
733 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
735 mlx5_pages_debugfs_cleanup(dev);
736 xa_destroy(&dev->priv.page_root_xa);
737 destroy_workqueue(dev->priv.pg_wq);
740 void mlx5_pagealloc_start(struct mlx5_core_dev *dev)
742 MLX5_NB_INIT(&dev->priv.pg_nb, req_pages_handler, PAGE_REQUEST);
743 mlx5_eq_notifier_register(dev, &dev->priv.pg_nb);
746 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
748 mlx5_eq_notifier_unregister(dev, &dev->priv.pg_nb);
749 flush_workqueue(dev->priv.pg_wq);
752 int mlx5_wait_for_pages(struct mlx5_core_dev *dev, int *pages)
754 u64 recl_vf_pages_to_jiffies = msecs_to_jiffies(mlx5_tout_ms(dev, RECLAIM_VFS_PAGES));
755 unsigned long end = jiffies + recl_vf_pages_to_jiffies;
756 int prev_pages = *pages;
758 /* In case of internal error we will free the pages manually later */
759 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
760 mlx5_core_warn(dev, "Skipping wait for vf pages stage");
764 mlx5_core_dbg(dev, "Waiting for %d pages\n", prev_pages);
766 if (time_after(jiffies, end)) {
767 mlx5_core_warn(dev, "aborting while there are %d pending pages\n", *pages);
770 if (*pages < prev_pages) {
771 end = jiffies + recl_vf_pages_to_jiffies;
777 mlx5_core_dbg(dev, "All pages received\n");