2 * Copyright (c) 2013, Mellanox Technologies inc. 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 <asm-generic/kmap_types.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/mlx5/driver.h>
37 #include <linux/mlx5/cmd.h>
38 #include "mlx5_core.h"
41 MLX5_PAGES_CANT_GIVE = 0,
46 struct mlx5_pages_req {
47 struct mlx5_core_dev *dev;
50 struct work_struct work;
54 struct rb_node rb_node;
60 struct mlx5_query_pages_inbox {
61 struct mlx5_inbox_hdr hdr;
65 struct mlx5_query_pages_outbox {
66 struct mlx5_outbox_hdr hdr;
73 struct mlx5_manage_pages_inbox {
74 struct mlx5_inbox_hdr hdr;
83 struct mlx5_manage_pages_outbox {
84 struct mlx5_outbox_hdr hdr;
91 static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u16 func_id)
93 struct rb_root *root = &dev->priv.page_root;
94 struct rb_node **new = &root->rb_node;
95 struct rb_node *parent = NULL;
101 tfp = rb_entry(parent, struct fw_page, rb_node);
102 if (tfp->addr < addr)
103 new = &parent->rb_left;
104 else if (tfp->addr > addr)
105 new = &parent->rb_right;
110 nfp = kmalloc(sizeof(*nfp), GFP_KERNEL);
116 nfp->func_id = func_id;
118 rb_link_node(&nfp->rb_node, parent, new);
119 rb_insert_color(&nfp->rb_node, root);
124 static struct page *remove_page(struct mlx5_core_dev *dev, u64 addr)
126 struct rb_root *root = &dev->priv.page_root;
127 struct rb_node *tmp = root->rb_node;
128 struct page *result = NULL;
132 tfp = rb_entry(tmp, struct fw_page, rb_node);
133 if (tfp->addr < addr) {
135 } else if (tfp->addr > addr) {
138 rb_erase(&tfp->rb_node, root);
148 static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
149 s16 *pages, s16 *init_pages)
151 struct mlx5_query_pages_inbox in;
152 struct mlx5_query_pages_outbox out;
155 memset(&in, 0, sizeof(in));
156 memset(&out, 0, sizeof(out));
157 in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_PAGES);
158 err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
163 return mlx5_cmd_status_to_err(&out.hdr);
166 *pages = be16_to_cpu(out.num_pages);
168 *init_pages = be16_to_cpu(out.init_pages);
169 *func_id = be16_to_cpu(out.func_id);
174 static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
177 struct mlx5_manage_pages_inbox *in;
178 struct mlx5_manage_pages_outbox out;
185 inlen = sizeof(*in) + npages * sizeof(in->pas[0]);
186 in = mlx5_vzalloc(inlen);
188 mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
191 memset(&out, 0, sizeof(out));
193 for (i = 0; i < npages; i++) {
194 page = alloc_page(GFP_HIGHUSER);
197 mlx5_core_warn(dev, "failed to allocate page\n");
200 addr = dma_map_page(&dev->pdev->dev, page, 0,
201 PAGE_SIZE, DMA_BIDIRECTIONAL);
202 if (dma_mapping_error(&dev->pdev->dev, addr)) {
203 mlx5_core_warn(dev, "failed dma mapping page\n");
208 err = insert_page(dev, addr, page, func_id);
210 mlx5_core_err(dev, "failed to track allocated page\n");
211 dma_unmap_page(&dev->pdev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
216 in->pas[i] = cpu_to_be64(addr);
219 in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
220 in->hdr.opmod = cpu_to_be16(MLX5_PAGES_GIVE);
221 in->func_id = cpu_to_be16(func_id);
222 in->num_entries = cpu_to_be16(npages);
223 err = mlx5_cmd_exec(dev, in, inlen, &out, sizeof(out));
224 mlx5_core_dbg(dev, "err %d\n", err);
226 mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n", func_id, npages, err);
229 dev->priv.fw_pages += npages;
231 if (out.hdr.status) {
232 err = mlx5_cmd_status_to_err(&out.hdr);
234 mlx5_core_warn(dev, "func_id 0x%x, npages %d, status %d\n", func_id, npages, out.hdr.status);
239 mlx5_core_dbg(dev, "err %d\n", err);
245 memset(in, 0, inlen);
246 memset(&out, 0, sizeof(out));
247 in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
248 in->hdr.opmod = cpu_to_be16(MLX5_PAGES_CANT_GIVE);
249 if (mlx5_cmd_exec(dev, in, sizeof(*in), &out, sizeof(out)))
250 mlx5_core_warn(dev, "\n");
252 for (i--; i >= 0; i--) {
253 addr = be64_to_cpu(in->pas[i]);
254 page = remove_page(dev, addr);
256 mlx5_core_err(dev, "BUG: can't remove page at addr 0x%llx\n",
260 dma_unmap_page(&dev->pdev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
269 static int reclaim_pages(struct mlx5_core_dev *dev, u32 func_id, int npages,
272 struct mlx5_manage_pages_inbox in;
273 struct mlx5_manage_pages_outbox *out;
281 memset(&in, 0, sizeof(in));
282 outlen = sizeof(*out) + npages * sizeof(out->pas[0]);
283 out = mlx5_vzalloc(outlen);
287 in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
288 in.hdr.opmod = cpu_to_be16(MLX5_PAGES_TAKE);
289 in.func_id = cpu_to_be16(func_id);
290 in.num_entries = cpu_to_be16(npages);
291 mlx5_core_dbg(dev, "npages %d, outlen %d\n", npages, outlen);
292 err = mlx5_cmd_exec(dev, &in, sizeof(in), out, outlen);
294 mlx5_core_err(dev, "failed recliaming pages\n");
297 dev->priv.fw_pages -= npages;
299 if (out->hdr.status) {
300 err = mlx5_cmd_status_to_err(&out->hdr);
304 num_claimed = be16_to_cpu(out->num_entries);
306 *nclaimed = num_claimed;
308 for (i = 0; i < num_claimed; i++) {
309 addr = be64_to_cpu(out->pas[i]);
310 page = remove_page(dev, addr);
312 mlx5_core_warn(dev, "FW reported unknown DMA address 0x%llx\n", addr);
314 dma_unmap_page(&dev->pdev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
324 static void pages_work_handler(struct work_struct *work)
326 struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
327 struct mlx5_core_dev *dev = req->dev;
331 err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL);
332 else if (req->npages > 0)
333 err = give_pages(dev, req->func_id, req->npages, 1);
336 mlx5_core_warn(dev, "%s fail %d\n", req->npages < 0 ?
337 "reclaim" : "give", err);
342 void mlx5_core_req_pages_handler(struct mlx5_core_dev *dev, u16 func_id,
345 struct mlx5_pages_req *req;
347 req = kzalloc(sizeof(*req), GFP_ATOMIC);
349 mlx5_core_warn(dev, "failed to allocate pages request\n");
354 req->func_id = func_id;
355 req->npages = npages;
356 INIT_WORK(&req->work, pages_work_handler);
357 queue_work(dev->priv.pg_wq, &req->work);
360 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev)
362 s16 uninitialized_var(init_pages);
363 u16 uninitialized_var(func_id);
366 err = mlx5_cmd_query_pages(dev, &func_id, NULL, &init_pages);
370 mlx5_core_dbg(dev, "requested %d init pages for func_id 0x%x\n", init_pages, func_id);
372 return give_pages(dev, func_id, init_pages, 0);
375 static int optimal_reclaimed_pages(void)
377 struct mlx5_cmd_prot_block *block;
378 struct mlx5_cmd_layout *lay;
381 ret = (sizeof(lay->in) + sizeof(block->data) -
382 sizeof(struct mlx5_manage_pages_outbox)) / 8;
387 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
389 unsigned long end = jiffies + msecs_to_jiffies(5000);
395 p = rb_first(&dev->priv.page_root);
397 fwp = rb_entry(p, struct fw_page, rb_node);
398 err = reclaim_pages(dev, fwp->func_id, optimal_reclaimed_pages(), NULL);
400 mlx5_core_warn(dev, "failed reclaiming pages (%d)\n", err);
404 if (time_after(jiffies, end)) {
405 mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
413 void mlx5_pagealloc_init(struct mlx5_core_dev *dev)
415 dev->priv.page_root = RB_ROOT;
418 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
423 int mlx5_pagealloc_start(struct mlx5_core_dev *dev)
425 dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
426 if (!dev->priv.pg_wq)
432 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
434 destroy_workqueue(dev->priv.pg_wq);