2 * Copyright (c) 2013-2017, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <dev/mlx5/driver.h>
32 #include "mlx5_core.h"
34 CTASSERT((uintptr_t)PAGE_MASK > (uintptr_t)PAGE_SIZE);
36 struct mlx5_pages_req {
37 struct mlx5_core_dev *dev;
40 struct work_struct work;
44 struct mlx5_manage_pages_inbox {
45 struct mlx5_inbox_hdr hdr;
52 struct mlx5_manage_pages_outbox {
53 struct mlx5_outbox_hdr hdr;
60 MAX_RECLAIM_TIME_MSECS = 5000,
64 mlx5_fwp_load_mem_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
66 struct mlx5_fw_page *fwp;
69 fwp = (struct mlx5_fw_page *)arg;
70 owned = MLX5_DMA_OWNED(fwp->dev);
73 MLX5_DMA_LOCK(fwp->dev);
76 KASSERT(nseg == 1, ("Number of segments is different from 1"));
77 fwp->dma_addr = segs->ds_addr;
78 fwp->load_done = MLX5_LOAD_ST_SUCCESS;
80 fwp->load_done = MLX5_LOAD_ST_FAILURE;
82 MLX5_DMA_DONE(fwp->dev);
85 MLX5_DMA_UNLOCK(fwp->dev);
89 mlx5_fwp_flush(struct mlx5_fw_page *fwp)
91 unsigned num = fwp->numpages;
94 bus_dmamap_sync(fwp[num].dev->cmd.dma_tag, fwp[num].dma_map, BUS_DMASYNC_PREWRITE);
98 mlx5_fwp_invalidate(struct mlx5_fw_page *fwp)
100 unsigned num = fwp->numpages;
103 bus_dmamap_sync(fwp[num].dev->cmd.dma_tag, fwp[num].dma_map, BUS_DMASYNC_POSTREAD);
104 bus_dmamap_sync(fwp[num].dev->cmd.dma_tag, fwp[num].dma_map, BUS_DMASYNC_PREREAD);
108 struct mlx5_fw_page *
109 mlx5_fwp_alloc(struct mlx5_core_dev *dev, gfp_t flags, unsigned num)
111 struct mlx5_fw_page *fwp;
115 /* check for special case */
117 fwp = kzalloc(sizeof(*fwp), flags);
123 /* we need sleeping context for this function */
124 if (flags & M_NOWAIT)
127 fwp = kzalloc(sizeof(*fwp) * num, flags);
129 /* serialize loading the DMA map(s) */
130 sx_xlock(&dev->cmd.dma_sx);
132 for (x = 0; x != num; x++) {
133 /* store pointer to MLX5 core device */
135 /* store number of pages left from the array */
136 fwp[x].numpages = num - x;
138 /* allocate memory */
139 err = bus_dmamem_alloc(dev->cmd.dma_tag, &fwp[x].virt_addr,
140 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &fwp[x].dma_map);
144 /* load memory into DMA */
146 err = bus_dmamap_load(
147 dev->cmd.dma_tag, fwp[x].dma_map, fwp[x].virt_addr,
148 MLX5_ADAPTER_PAGE_SIZE, &mlx5_fwp_load_mem_cb,
149 fwp + x, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
151 while (fwp[x].load_done == MLX5_LOAD_ST_NONE)
153 MLX5_DMA_UNLOCK(dev);
155 /* check for error */
156 if (fwp[x].load_done != MLX5_LOAD_ST_SUCCESS) {
157 bus_dmamem_free(dev->cmd.dma_tag, fwp[x].virt_addr,
162 sx_xunlock(&dev->cmd.dma_sx);
167 bus_dmamap_unload(dev->cmd.dma_tag, fwp[x].dma_map);
168 bus_dmamem_free(dev->cmd.dma_tag, fwp[x].virt_addr, fwp[x].dma_map);
170 sx_xunlock(&dev->cmd.dma_sx);
175 mlx5_fwp_free(struct mlx5_fw_page *fwp)
177 struct mlx5_core_dev *dev;
184 /* check for special case */
185 if (fwp->numpages == 0) {
193 /* serialize unloading the DMA maps */
194 sx_xlock(&dev->cmd.dma_sx);
196 bus_dmamap_unload(dev->cmd.dma_tag, fwp[num].dma_map);
197 bus_dmamem_free(dev->cmd.dma_tag, fwp[num].virt_addr, fwp[num].dma_map);
199 sx_xunlock(&dev->cmd.dma_sx);
205 mlx5_fwp_get_dma(struct mlx5_fw_page *fwp, size_t offset)
207 size_t index = (offset / MLX5_ADAPTER_PAGE_SIZE);
208 KASSERT(index < fwp->numpages, ("Invalid offset: %lld", (long long)offset));
210 return ((fwp + index)->dma_addr + (offset % MLX5_ADAPTER_PAGE_SIZE));
214 mlx5_fwp_get_virt(struct mlx5_fw_page *fwp, size_t offset)
216 size_t index = (offset / MLX5_ADAPTER_PAGE_SIZE);
217 KASSERT(index < fwp->numpages, ("Invalid offset: %lld", (long long)offset));
219 return ((char *)(fwp + index)->virt_addr + (offset % MLX5_ADAPTER_PAGE_SIZE));
223 mlx5_insert_fw_page_locked(struct mlx5_core_dev *dev, struct mlx5_fw_page *nfp)
225 struct rb_root *root = &dev->priv.page_root;
226 struct rb_node **new = &root->rb_node;
227 struct rb_node *parent = NULL;
228 struct mlx5_fw_page *tfp;
232 tfp = rb_entry(parent, struct mlx5_fw_page, rb_node);
233 if (tfp->dma_addr < nfp->dma_addr)
234 new = &parent->rb_left;
235 else if (tfp->dma_addr > nfp->dma_addr)
236 new = &parent->rb_right;
241 rb_link_node(&nfp->rb_node, parent, new);
242 rb_insert_color(&nfp->rb_node, root);
246 static struct mlx5_fw_page *
247 mlx5_remove_fw_page_locked(struct mlx5_core_dev *dev, bus_addr_t addr)
249 struct rb_root *root = &dev->priv.page_root;
250 struct rb_node *tmp = root->rb_node;
251 struct mlx5_fw_page *result = NULL;
252 struct mlx5_fw_page *tfp;
255 tfp = rb_entry(tmp, struct mlx5_fw_page, rb_node);
256 if (tfp->dma_addr < addr) {
258 } else if (tfp->dma_addr > addr) {
261 rb_erase(&tfp->rb_node, &dev->priv.page_root);
270 alloc_4k(struct mlx5_core_dev *dev, u64 *addr, u16 func_id)
272 struct mlx5_fw_page *fwp;
275 fwp = mlx5_fwp_alloc(dev, GFP_KERNEL, 1);
279 fwp->func_id = func_id;
282 err = mlx5_insert_fw_page_locked(dev, fwp);
283 MLX5_DMA_UNLOCK(dev);
288 /* make sure cached data is cleaned */
289 mlx5_fwp_invalidate(fwp);
291 /* store DMA address */
292 *addr = fwp->dma_addr;
298 free_4k(struct mlx5_core_dev *dev, u64 addr)
300 struct mlx5_fw_page *fwp;
303 fwp = mlx5_remove_fw_page_locked(dev, addr);
304 MLX5_DMA_UNLOCK(dev);
307 mlx5_core_warn(dev, "Cannot free 4K page at 0x%llx\n", (long long)addr);
313 static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
314 s32 *npages, int boot)
316 u32 in[MLX5_ST_SZ_DW(query_pages_in)];
317 u32 out[MLX5_ST_SZ_DW(query_pages_out)];
320 memset(in, 0, sizeof(in));
322 MLX5_SET(query_pages_in, in, opcode, MLX5_CMD_OP_QUERY_PAGES);
323 MLX5_SET(query_pages_in, in, op_mod,
324 boot ? MLX5_BOOT_PAGES : MLX5_INIT_PAGES);
326 memset(out, 0, sizeof(out));
327 err = mlx5_cmd_exec_check_status(dev, in, sizeof(in), out, sizeof(out));
331 *npages = MLX5_GET(query_pages_out, out, num_pages);
332 *func_id = MLX5_GET(query_pages_out, out, function_id);
337 static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
340 struct mlx5_manage_pages_inbox *in;
341 struct mlx5_manage_pages_outbox out;
342 struct mlx5_manage_pages_inbox *nin;
348 inlen = sizeof(*in) + npages * sizeof(in->pas[0]);
349 in = mlx5_vzalloc(inlen);
351 mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
355 memset(&out, 0, sizeof(out));
357 for (i = 0; i < npages; i++) {
358 err = alloc_4k(dev, &addr, func_id);
361 in->pas[i] = cpu_to_be64(addr);
364 in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
365 in->hdr.opmod = cpu_to_be16(MLX5_PAGES_GIVE);
366 in->func_id = cpu_to_be16(func_id);
367 in->num_entries = cpu_to_be32(npages);
368 err = mlx5_cmd_exec(dev, in, inlen, &out, sizeof(out));
370 mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
371 func_id, npages, err);
374 dev->priv.fw_pages += npages;
375 dev->priv.pages_per_func[func_id] += npages;
377 if (out.hdr.status) {
378 err = mlx5_cmd_status_to_err(&out.hdr);
380 mlx5_core_warn(dev, "func_id 0x%x, npages %d, status %d\n",
381 func_id, npages, out.hdr.status);
386 mlx5_core_dbg(dev, "err %d\n", err);
392 nin = kzalloc(sizeof(*nin), GFP_KERNEL);
396 memset(&out, 0, sizeof(out));
397 nin->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
398 nin->hdr.opmod = cpu_to_be16(MLX5_PAGES_CANT_GIVE);
399 nin->func_id = cpu_to_be16(func_id);
400 if (mlx5_cmd_exec(dev, nin, sizeof(*nin), &out, sizeof(out)))
401 mlx5_core_warn(dev, "page notify failed\n");
406 for (i--; i >= 0; i--)
407 free_4k(dev, be64_to_cpu(in->pas[i]));
413 static int reclaim_pages(struct mlx5_core_dev *dev, u32 func_id, int npages,
416 struct mlx5_manage_pages_inbox in;
417 struct mlx5_manage_pages_outbox *out;
427 memset(&in, 0, sizeof(in));
428 outlen = sizeof(*out) + npages * sizeof(out->pas[0]);
429 out = mlx5_vzalloc(outlen);
433 in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
434 in.hdr.opmod = cpu_to_be16(MLX5_PAGES_TAKE);
435 in.func_id = cpu_to_be16(func_id);
436 in.num_entries = cpu_to_be32(npages);
437 mlx5_core_dbg(dev, "npages %d, outlen %d\n", npages, outlen);
438 err = mlx5_cmd_exec(dev, &in, sizeof(in), out, outlen);
440 mlx5_core_err(dev, "failed reclaiming pages\n");
444 if (out->hdr.status) {
445 err = mlx5_cmd_status_to_err(&out->hdr);
449 num_claimed = be32_to_cpu(out->num_entries);
451 *nclaimed = num_claimed;
453 dev->priv.fw_pages -= num_claimed;
454 dev->priv.pages_per_func[func_id] -= num_claimed;
455 for (i = 0; i < num_claimed; i++) {
456 addr = be64_to_cpu(out->pas[i]);
465 static void pages_work_handler(struct work_struct *work)
467 struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
468 struct mlx5_core_dev *dev = req->dev;
472 err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL);
473 else if (req->npages > 0)
474 err = give_pages(dev, req->func_id, req->npages, 1);
477 mlx5_core_warn(dev, "%s fail %d\n",
478 req->npages < 0 ? "reclaim" : "give", err);
483 void mlx5_core_req_pages_handler(struct mlx5_core_dev *dev, u16 func_id,
486 struct mlx5_pages_req *req;
488 req = kzalloc(sizeof(*req), GFP_ATOMIC);
490 mlx5_core_warn(dev, "failed to allocate pages request\n");
495 req->func_id = func_id;
496 req->npages = npages;
497 INIT_WORK(&req->work, pages_work_handler);
498 if (!queue_work(dev->priv.pg_wq, &req->work))
499 mlx5_core_warn(dev, "failed to queue pages handler work\n");
502 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
504 u16 uninitialized_var(func_id);
505 s32 uninitialized_var(npages);
508 err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
512 mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
513 npages, boot ? "boot" : "init", func_id);
515 return give_pages(dev, func_id, npages, 0);
519 MLX5_BLKS_FOR_RECLAIM_PAGES = 12
522 s64 mlx5_wait_for_reclaim_vfs_pages(struct mlx5_core_dev *dev)
524 int end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
528 while (!time_after(jiffies, end)) {
529 /* exclude own function, VFs only */
530 npages = dev->priv.fw_pages - dev->priv.pages_per_func[0];
534 if (npages != prevpages)
535 end = end + msecs_to_jiffies(100);
542 mlx5_core_warn(dev, "FW did not return all VFs pages, will cause to memory leak\n");
547 static int optimal_reclaimed_pages(void)
549 struct mlx5_cmd_prot_block *block;
550 struct mlx5_cmd_layout *lay;
553 ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
554 sizeof(struct mlx5_manage_pages_outbox)) /
555 FIELD_SIZEOF(struct mlx5_manage_pages_outbox, pas[0]);
560 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
562 int end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
563 struct mlx5_fw_page *fwp;
569 p = rb_first(&dev->priv.page_root);
571 fwp = rb_entry(p, struct mlx5_fw_page, rb_node);
572 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
573 --dev->priv.fw_pages;
574 free_4k(dev, fwp->dma_addr);
577 err = reclaim_pages(dev, fwp->func_id,
578 optimal_reclaimed_pages(),
581 mlx5_core_warn(dev, "failed reclaiming pages (%d)\n",
588 end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
590 if (time_after(jiffies, end)) {
591 mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
599 void mlx5_pagealloc_init(struct mlx5_core_dev *dev)
602 dev->priv.page_root = RB_ROOT;
605 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
610 int mlx5_pagealloc_start(struct mlx5_core_dev *dev)
612 dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
613 if (!dev->priv.pg_wq)
619 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
621 destroy_workqueue(dev->priv.pg_wq);