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/mlx5/cmd.h>
39 #include "mlx5_core.h"
42 MLX5_PAGES_CANT_GIVE
= 0,
47 struct mlx5_pages_req
{
48 struct mlx5_core_dev
*dev
;
51 struct work_struct work
;
55 struct rb_node rb_node
;
59 unsigned long bitmask
;
60 struct list_head list
;
65 MAX_RECLAIM_TIME_MSECS
= 5000,
66 MAX_RECLAIM_VFS_PAGES_TIME_MSECS
= 2 * 1000 * 60,
70 MLX5_MAX_RECLAIM_TIME_MILI
= 5000,
71 MLX5_NUM_4K_IN_PAGE
= PAGE_SIZE
/ MLX5_ADAPTER_PAGE_SIZE
,
74 static int insert_page(struct mlx5_core_dev
*dev
, u64 addr
, struct page
*page
, u16 func_id
)
76 struct rb_root
*root
= &dev
->priv
.page_root
;
77 struct rb_node
**new = &root
->rb_node
;
78 struct rb_node
*parent
= NULL
;
85 tfp
= rb_entry(parent
, struct fw_page
, rb_node
);
87 new = &parent
->rb_left
;
88 else if (tfp
->addr
> addr
)
89 new = &parent
->rb_right
;
94 nfp
= kzalloc(sizeof(*nfp
), GFP_KERNEL
);
100 nfp
->func_id
= func_id
;
101 nfp
->free_count
= MLX5_NUM_4K_IN_PAGE
;
102 for (i
= 0; i
< MLX5_NUM_4K_IN_PAGE
; i
++)
103 set_bit(i
, &nfp
->bitmask
);
105 rb_link_node(&nfp
->rb_node
, parent
, new);
106 rb_insert_color(&nfp
->rb_node
, root
);
107 list_add(&nfp
->list
, &dev
->priv
.free_list
);
112 static struct fw_page
*find_fw_page(struct mlx5_core_dev
*dev
, u64 addr
)
114 struct rb_root
*root
= &dev
->priv
.page_root
;
115 struct rb_node
*tmp
= root
->rb_node
;
116 struct fw_page
*result
= NULL
;
120 tfp
= rb_entry(tmp
, struct fw_page
, rb_node
);
121 if (tfp
->addr
< addr
) {
123 } else if (tfp
->addr
> addr
) {
134 static int mlx5_cmd_query_pages(struct mlx5_core_dev
*dev
, u16
*func_id
,
135 s32
*npages
, int boot
)
137 u32 out
[MLX5_ST_SZ_DW(query_pages_out
)] = {0};
138 u32 in
[MLX5_ST_SZ_DW(query_pages_in
)] = {0};
141 MLX5_SET(query_pages_in
, in
, opcode
, MLX5_CMD_OP_QUERY_PAGES
);
142 MLX5_SET(query_pages_in
, in
, op_mod
, boot
?
143 MLX5_QUERY_PAGES_IN_OP_MOD_BOOT_PAGES
:
144 MLX5_QUERY_PAGES_IN_OP_MOD_INIT_PAGES
);
146 err
= mlx5_cmd_exec(dev
, in
, sizeof(in
), out
, sizeof(out
));
150 *npages
= MLX5_GET(query_pages_out
, out
, num_pages
);
151 *func_id
= MLX5_GET(query_pages_out
, out
, function_id
);
156 static int alloc_4k(struct mlx5_core_dev
*dev
, u64
*addr
)
161 if (list_empty(&dev
->priv
.free_list
))
164 fp
= list_entry(dev
->priv
.free_list
.next
, struct fw_page
, list
);
165 n
= find_first_bit(&fp
->bitmask
, 8 * sizeof(fp
->bitmask
));
166 if (n
>= MLX5_NUM_4K_IN_PAGE
) {
167 mlx5_core_warn(dev
, "alloc 4k bug\n");
170 clear_bit(n
, &fp
->bitmask
);
175 *addr
= fp
->addr
+ n
* MLX5_ADAPTER_PAGE_SIZE
;
180 #define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
182 static void free_4k(struct mlx5_core_dev
*dev
, u64 addr
)
187 fwp
= find_fw_page(dev
, addr
& MLX5_U64_4K_PAGE_MASK
);
189 mlx5_core_warn(dev
, "page not found\n");
193 n
= (addr
& ~MLX5_U64_4K_PAGE_MASK
) >> MLX5_ADAPTER_PAGE_SHIFT
;
195 set_bit(n
, &fwp
->bitmask
);
196 if (fwp
->free_count
== MLX5_NUM_4K_IN_PAGE
) {
197 rb_erase(&fwp
->rb_node
, &dev
->priv
.page_root
);
198 if (fwp
->free_count
!= 1)
199 list_del(&fwp
->list
);
200 dma_unmap_page(&dev
->pdev
->dev
, addr
& MLX5_U64_4K_PAGE_MASK
,
201 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
202 __free_page(fwp
->page
);
204 } else if (fwp
->free_count
== 1) {
205 list_add(&fwp
->list
, &dev
->priv
.free_list
);
209 static int alloc_system_page(struct mlx5_core_dev
*dev
, u16 func_id
)
215 int nid
= dev_to_node(&dev
->pdev
->dev
);
217 page
= alloc_pages_node(nid
, GFP_HIGHUSER
, 0);
219 mlx5_core_warn(dev
, "failed to allocate page\n");
223 addr
= dma_map_page(&dev
->pdev
->dev
, page
, 0,
224 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
225 if (dma_mapping_error(&dev
->pdev
->dev
, addr
)) {
226 mlx5_core_warn(dev
, "failed dma mapping page\n");
231 /* Firmware doesn't support page with physical address 0 */
237 err
= insert_page(dev
, addr
, page
, func_id
);
239 mlx5_core_err(dev
, "failed to track allocated page\n");
240 dma_unmap_page(&dev
->pdev
->dev
, addr
, PAGE_SIZE
,
249 dma_unmap_page(&dev
->pdev
->dev
, zero_addr
, PAGE_SIZE
,
255 static void page_notify_fail(struct mlx5_core_dev
*dev
, u16 func_id
)
257 u32 out
[MLX5_ST_SZ_DW(manage_pages_out
)] = {0};
258 u32 in
[MLX5_ST_SZ_DW(manage_pages_in
)] = {0};
261 MLX5_SET(manage_pages_in
, in
, opcode
, MLX5_CMD_OP_MANAGE_PAGES
);
262 MLX5_SET(manage_pages_in
, in
, op_mod
, MLX5_PAGES_CANT_GIVE
);
263 MLX5_SET(manage_pages_in
, in
, function_id
, func_id
);
265 err
= mlx5_cmd_exec(dev
, in
, sizeof(in
), out
, sizeof(out
));
267 mlx5_core_warn(dev
, "page notify failed func_id(%d) err(%d)\n",
271 static int give_pages(struct mlx5_core_dev
*dev
, u16 func_id
, int npages
,
274 u32 out
[MLX5_ST_SZ_DW(manage_pages_out
)] = {0};
275 int inlen
= MLX5_ST_SZ_BYTES(manage_pages_in
);
281 inlen
+= npages
* MLX5_FLD_SZ_BYTES(manage_pages_in
, pas
[0]);
282 in
= kvzalloc(inlen
, GFP_KERNEL
);
285 mlx5_core_warn(dev
, "vzalloc failed %d\n", inlen
);
289 for (i
= 0; i
< npages
; i
++) {
291 err
= alloc_4k(dev
, &addr
);
294 err
= alloc_system_page(dev
, func_id
);
300 MLX5_ARRAY_SET64(manage_pages_in
, in
, pas
, i
, addr
);
303 MLX5_SET(manage_pages_in
, in
, opcode
, MLX5_CMD_OP_MANAGE_PAGES
);
304 MLX5_SET(manage_pages_in
, in
, op_mod
, MLX5_PAGES_GIVE
);
305 MLX5_SET(manage_pages_in
, in
, function_id
, func_id
);
306 MLX5_SET(manage_pages_in
, in
, input_num_entries
, npages
);
308 err
= mlx5_cmd_exec(dev
, in
, inlen
, out
, sizeof(out
));
310 mlx5_core_warn(dev
, "func_id 0x%x, npages %d, err %d\n",
311 func_id
, npages
, err
);
315 dev
->priv
.fw_pages
+= npages
;
317 dev
->priv
.vfs_pages
+= npages
;
319 mlx5_core_dbg(dev
, "err %d\n", err
);
325 for (i
--; i
>= 0; i
--)
326 free_4k(dev
, MLX5_GET64(manage_pages_in
, in
, pas
[i
]));
330 page_notify_fail(dev
, func_id
);
334 static int reclaim_pages_cmd(struct mlx5_core_dev
*dev
,
335 u32
*in
, int in_size
, u32
*out
, int out_size
)
343 if (dev
->state
!= MLX5_DEVICE_STATE_INTERNAL_ERROR
)
344 return mlx5_cmd_exec(dev
, in
, in_size
, out
, out_size
);
346 /* No hard feelings, we want our pages back! */
347 npages
= MLX5_GET(manage_pages_in
, in
, input_num_entries
);
348 func_id
= MLX5_GET(manage_pages_in
, in
, function_id
);
350 p
= rb_first(&dev
->priv
.page_root
);
351 while (p
&& i
< npages
) {
352 fwp
= rb_entry(p
, struct fw_page
, rb_node
);
354 if (fwp
->func_id
!= func_id
)
357 MLX5_ARRAY_SET64(manage_pages_out
, out
, pas
, i
, fwp
->addr
);
361 MLX5_SET(manage_pages_out
, out
, output_num_entries
, i
);
365 static int reclaim_pages(struct mlx5_core_dev
*dev
, u32 func_id
, int npages
,
368 int outlen
= MLX5_ST_SZ_BYTES(manage_pages_out
);
369 u32 in
[MLX5_ST_SZ_DW(manage_pages_in
)] = {0};
378 outlen
+= npages
* MLX5_FLD_SZ_BYTES(manage_pages_out
, pas
[0]);
379 out
= kvzalloc(outlen
, GFP_KERNEL
);
383 MLX5_SET(manage_pages_in
, in
, opcode
, MLX5_CMD_OP_MANAGE_PAGES
);
384 MLX5_SET(manage_pages_in
, in
, op_mod
, MLX5_PAGES_TAKE
);
385 MLX5_SET(manage_pages_in
, in
, function_id
, func_id
);
386 MLX5_SET(manage_pages_in
, in
, input_num_entries
, npages
);
388 mlx5_core_dbg(dev
, "npages %d, outlen %d\n", npages
, outlen
);
389 err
= reclaim_pages_cmd(dev
, in
, sizeof(in
), out
, outlen
);
391 mlx5_core_err(dev
, "failed reclaiming pages: err %d\n", err
);
395 num_claimed
= MLX5_GET(manage_pages_out
, out
, output_num_entries
);
396 if (num_claimed
> npages
) {
397 mlx5_core_warn(dev
, "fw returned %d, driver asked %d => corruption\n",
398 num_claimed
, npages
);
403 for (i
= 0; i
< num_claimed
; i
++)
404 free_4k(dev
, MLX5_GET64(manage_pages_out
, out
, pas
[i
]));
407 *nclaimed
= num_claimed
;
409 dev
->priv
.fw_pages
-= num_claimed
;
411 dev
->priv
.vfs_pages
-= num_claimed
;
418 static void pages_work_handler(struct work_struct
*work
)
420 struct mlx5_pages_req
*req
= container_of(work
, struct mlx5_pages_req
, work
);
421 struct mlx5_core_dev
*dev
= req
->dev
;
425 err
= reclaim_pages(dev
, req
->func_id
, -1 * req
->npages
, NULL
);
426 else if (req
->npages
> 0)
427 err
= give_pages(dev
, req
->func_id
, req
->npages
, 1);
430 mlx5_core_warn(dev
, "%s fail %d\n",
431 req
->npages
< 0 ? "reclaim" : "give", err
);
436 void mlx5_core_req_pages_handler(struct mlx5_core_dev
*dev
, u16 func_id
,
439 struct mlx5_pages_req
*req
;
441 req
= kzalloc(sizeof(*req
), GFP_ATOMIC
);
443 mlx5_core_warn(dev
, "failed to allocate pages request\n");
448 req
->func_id
= func_id
;
449 req
->npages
= npages
;
450 INIT_WORK(&req
->work
, pages_work_handler
);
451 queue_work(dev
->priv
.pg_wq
, &req
->work
);
454 int mlx5_satisfy_startup_pages(struct mlx5_core_dev
*dev
, int boot
)
456 u16
uninitialized_var(func_id
);
457 s32
uninitialized_var(npages
);
460 err
= mlx5_cmd_query_pages(dev
, &func_id
, &npages
, boot
);
464 mlx5_core_dbg(dev
, "requested %d %s pages for func_id 0x%x\n",
465 npages
, boot
? "boot" : "init", func_id
);
467 return give_pages(dev
, func_id
, npages
, 0);
471 MLX5_BLKS_FOR_RECLAIM_PAGES
= 12
474 static int optimal_reclaimed_pages(void)
476 struct mlx5_cmd_prot_block
*block
;
477 struct mlx5_cmd_layout
*lay
;
480 ret
= (sizeof(lay
->out
) + MLX5_BLKS_FOR_RECLAIM_PAGES
* sizeof(block
->data
) -
481 MLX5_ST_SZ_BYTES(manage_pages_out
)) /
482 MLX5_FLD_SZ_BYTES(manage_pages_out
, pas
[0]);
487 int mlx5_reclaim_startup_pages(struct mlx5_core_dev
*dev
)
489 unsigned long end
= jiffies
+ msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS
);
496 p
= rb_first(&dev
->priv
.page_root
);
498 fwp
= rb_entry(p
, struct fw_page
, rb_node
);
499 err
= reclaim_pages(dev
, fwp
->func_id
,
500 optimal_reclaimed_pages(),
504 mlx5_core_warn(dev
, "failed reclaiming pages (%d)\n",
509 end
= jiffies
+ msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS
);
511 if (time_after(jiffies
, end
)) {
512 mlx5_core_warn(dev
, "FW did not return all pages. giving up...\n");
517 WARN(dev
->priv
.fw_pages
,
518 "FW pages counter is %d after reclaiming all pages\n",
520 WARN(dev
->priv
.vfs_pages
,
521 "VFs FW pages counter is %d after reclaiming all pages\n",
522 dev
->priv
.vfs_pages
);
527 void mlx5_pagealloc_init(struct mlx5_core_dev
*dev
)
529 dev
->priv
.page_root
= RB_ROOT
;
530 INIT_LIST_HEAD(&dev
->priv
.free_list
);
533 void mlx5_pagealloc_cleanup(struct mlx5_core_dev
*dev
)
538 int mlx5_pagealloc_start(struct mlx5_core_dev
*dev
)
540 dev
->priv
.pg_wq
= create_singlethread_workqueue("mlx5_page_allocator");
541 if (!dev
->priv
.pg_wq
)
547 void mlx5_pagealloc_stop(struct mlx5_core_dev
*dev
)
549 destroy_workqueue(dev
->priv
.pg_wq
);
552 int mlx5_wait_for_vf_pages(struct mlx5_core_dev
*dev
)
554 unsigned long end
= jiffies
+ msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS
);
555 int prev_vfs_pages
= dev
->priv
.vfs_pages
;
557 /* In case of internal error we will free the pages manually later */
558 if (dev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
) {
559 mlx5_core_warn(dev
, "Skipping wait for vf pages stage");
563 mlx5_core_dbg(dev
, "Waiting for %d pages from %s\n", prev_vfs_pages
,
565 while (dev
->priv
.vfs_pages
) {
566 if (time_after(jiffies
, end
)) {
567 mlx5_core_warn(dev
, "aborting while there are %d pending pages\n", dev
->priv
.vfs_pages
);
570 if (dev
->priv
.vfs_pages
< prev_vfs_pages
) {
571 end
= jiffies
+ msecs_to_jiffies(MAX_RECLAIM_VFS_PAGES_TIME_MSECS
);
572 prev_vfs_pages
= dev
->priv
.vfs_pages
;
577 mlx5_core_dbg(dev
, "All pages received from %s\n", dev
->priv
.name
);