File Index
Files and directories
Sign in
FILE

pagealloc.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/mellanox/mlx5/core/pagealloc.c C
  1/*
  2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  3 *
  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:
  9 *
 10 *     Redistribution and use in source and binary forms, with or
 11 *     without modification, are permitted provided that the following
 12 *     conditions are met:
 13 *
 14 *      - Redistributions of source code must retain the above
 15 *        copyright notice, this list of conditions and the following
 16 *        disclaimer.
 17 *
 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.
 22 *
 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
 30 * SOFTWARE.
 31 */
 32
 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"
 39#include "lib/eq.h"
 40#include "lib/tout.h"
 41
 42enum {
 43	MLX5_PAGES_CANT_GIVE	= 0,
 44	MLX5_PAGES_GIVE		= 1,
 45	MLX5_PAGES_TAKE		= 2
 46};
 47
 48struct mlx5_pages_req {
 49	struct mlx5_core_dev *dev;
 50	u16	func_id;
 51	u8	ec_function;
 52	s32	npages;
 53	struct work_struct work;
 54	u8	release_all;
 55};
 56
 57struct fw_page {
 58	struct rb_node		rb_node;
 59	u64			addr;
 60	struct page	       *page;
 61	u32			function;
 62	unsigned long		bitmask;
 63	struct list_head	list;
 64	unsigned int free_count;
 65};
 66
 67enum {
 68	MLX5_MAX_RECLAIM_TIME_MILI	= 5000,
 69	MLX5_NUM_4K_IN_PAGE		= PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
 70};
 71
 72static u32 get_function(u16 func_id, bool ec_function)
 73{
 74	return (u32)func_id | (ec_function << 16);
 75}
 76
 77static u16 func_id_to_type(struct mlx5_core_dev *dev, u16 func_id, bool ec_function)
 78{
 79	if (!func_id)
 80		return mlx5_core_is_ecpf(dev) && !ec_function ? MLX5_HOST_PF : MLX5_PF;
 81
 82	if (func_id <= max(mlx5_core_max_vfs(dev), mlx5_core_max_ec_vfs(dev))) {
 83		if (ec_function)
 84			return MLX5_EC_VF;
 85		else
 86			return MLX5_VF;
 87	}
 88	return MLX5_SF;
 89}
 90
 91static u32 mlx5_get_ec_function(u32 function)
 92{
 93	return function >> 16;
 94}
 95
 96static u32 mlx5_get_func_id(u32 function)
 97{
 98	return function & 0xffff;
 99}
100
101static struct rb_root *page_root_per_function(struct mlx5_core_dev *dev, u32 function)
102{
103	struct rb_root *root;
104	int err;
105
106	root = xa_load(&dev->priv.page_root_xa, function);
107	if (root)
108		return root;
109
110	root = kzalloc(sizeof(*root), GFP_KERNEL);
111	if (!root)
112		return ERR_PTR(-ENOMEM);
113
114	err = xa_insert(&dev->priv.page_root_xa, function, root, GFP_KERNEL);
115	if (err) {
116		kfree(root);
117		return ERR_PTR(err);
118	}
119
120	*root = RB_ROOT;
121
122	return root;
123}
124
125static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u32 function)
126{
127	struct rb_node *parent = NULL;
128	struct rb_root *root;
129	struct rb_node **new;
130	struct fw_page *nfp;
131	struct fw_page *tfp;
132	int i;
133
134	root = page_root_per_function(dev, function);
135	if (IS_ERR(root))
136		return PTR_ERR(root);
137
138	new = &root->rb_node;
139
140	while (*new) {
141		parent = *new;
142		tfp = rb_entry(parent, struct fw_page, rb_node);
143		if (tfp->addr < addr)
144			new = &parent->rb_left;
145		else if (tfp->addr > addr)
146			new = &parent->rb_right;
147		else
148			return -EEXIST;
149	}
150
151	nfp = kzalloc(sizeof(*nfp), GFP_KERNEL);
152	if (!nfp)
153		return -ENOMEM;
154
155	nfp->addr = addr;
156	nfp->page = page;
157	nfp->function = function;
158	nfp->free_count = MLX5_NUM_4K_IN_PAGE;
159	for (i = 0; i < MLX5_NUM_4K_IN_PAGE; i++)
160		set_bit(i, &nfp->bitmask);
161
162	rb_link_node(&nfp->rb_node, parent, new);
163	rb_insert_color(&nfp->rb_node, root);
164	list_add(&nfp->list, &dev->priv.free_list);
165
166	return 0;
167}
168
169static struct fw_page *find_fw_page(struct mlx5_core_dev *dev, u64 addr,
170				    u32 function)
171{
172	struct fw_page *result = NULL;
173	struct rb_root *root;
174	struct rb_node *tmp;
175	struct fw_page *tfp;
176
177	root = xa_load(&dev->priv.page_root_xa, function);
178	if (WARN_ON_ONCE(!root))
179		return NULL;
180
181	tmp = root->rb_node;
182
183	while (tmp) {
184		tfp = rb_entry(tmp, struct fw_page, rb_node);
185		if (tfp->addr < addr) {
186			tmp = tmp->rb_left;
187		} else if (tfp->addr > addr) {
188			tmp = tmp->rb_right;
189		} else {
190			result = tfp;
191			break;
192		}
193	}
194
195	return result;
196}
197
198static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
199				s32 *npages, int boot)
200{
201	u32 out[MLX5_ST_SZ_DW(query_pages_out)] = {};
202	u32 in[MLX5_ST_SZ_DW(query_pages_in)] = {};
203	int err;
204
205	MLX5_SET(query_pages_in, in, opcode, MLX5_CMD_OP_QUERY_PAGES);
206	MLX5_SET(query_pages_in, in, op_mod, boot ?
207		 MLX5_QUERY_PAGES_IN_OP_MOD_BOOT_PAGES :
208		 MLX5_QUERY_PAGES_IN_OP_MOD_INIT_PAGES);
209	MLX5_SET(query_pages_in, in, embedded_cpu_function, mlx5_core_is_ecpf(dev));
210
211	err = mlx5_cmd_exec_inout(dev, query_pages, in, out);
212	if (err)
213		return err;
214
215	*npages = MLX5_GET(query_pages_out, out, num_pages);
216	*func_id = MLX5_GET(query_pages_out, out, function_id);
217
218	return err;
219}
220
221static int alloc_4k(struct mlx5_core_dev *dev, u64 *addr, u32 function)
222{
223	struct fw_page *fp = NULL;
224	struct fw_page *iter;
225	unsigned n;
226
227	list_for_each_entry(iter, &dev->priv.free_list, list) {
228		if (iter->function != function)
229			continue;
230		fp = iter;
231	}
232
233	if (list_empty(&dev->priv.free_list) || !fp)
234		return -ENOMEM;
235
236	n = find_first_bit(&fp->bitmask, 8 * sizeof(fp->bitmask));
237	if (n >= MLX5_NUM_4K_IN_PAGE) {
238		mlx5_core_warn(dev, "alloc 4k bug: fw page = 0x%llx, n = %u, bitmask: %lu, max num of 4K pages: %d\n",
239			       fp->addr, n, fp->bitmask,  MLX5_NUM_4K_IN_PAGE);
240		return -ENOENT;
241	}
242	clear_bit(n, &fp->bitmask);
243	fp->free_count--;
244	if (!fp->free_count)
245		list_del(&fp->list);
246
247	*addr = fp->addr + n * MLX5_ADAPTER_PAGE_SIZE;
248
249	return 0;
250}
251
252#define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
253
254static void free_fwp(struct mlx5_core_dev *dev, struct fw_page *fwp,
255		     bool in_free_list)
256{
257	struct rb_root *root;
258
259	root = xa_load(&dev->priv.page_root_xa, fwp->function);
260	if (WARN_ON_ONCE(!root))
261		return;
262
263	rb_erase(&fwp->rb_node, root);
264	if (in_free_list)
265		list_del(&fwp->list);
266	dma_unmap_page(mlx5_core_dma_dev(dev), fwp->addr & MLX5_U64_4K_PAGE_MASK,
267		       PAGE_SIZE, DMA_BIDIRECTIONAL);
268	__free_page(fwp->page);
269	kfree(fwp);
270}
271
272static void free_4k(struct mlx5_core_dev *dev, u64 addr, u32 function)
273{
274	struct fw_page *fwp;
275	int n;
276
277	fwp = find_fw_page(dev, addr & MLX5_U64_4K_PAGE_MASK, function);
278	if (!fwp) {
279		mlx5_core_warn_rl(dev, "page not found\n");
280		return;
281	}
282	n = (addr & ~MLX5_U64_4K_PAGE_MASK) >> MLX5_ADAPTER_PAGE_SHIFT;
283	fwp->free_count++;
284	set_bit(n, &fwp->bitmask);
285	if (fwp->free_count == MLX5_NUM_4K_IN_PAGE)
286		free_fwp(dev, fwp, fwp->free_count != 1);
287	else if (fwp->free_count == 1)
288		list_add(&fwp->list, &dev->priv.free_list);
289}
290
291static int alloc_system_page(struct mlx5_core_dev *dev, u32 function)
292{
293	struct device *device = mlx5_core_dma_dev(dev);
294	int nid = dev->priv.numa_node;
295	struct page *page;
296	u64 zero_addr = 1;
297	u64 addr;
298	int err;
299
300	page = alloc_pages_node(nid, GFP_HIGHUSER, 0);
301	if (!page) {
302		mlx5_core_warn(dev, "failed to allocate page\n");
303		return -ENOMEM;
304	}
305map:
306	addr = dma_map_page(device, page, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
307	if (dma_mapping_error(device, addr)) {
308		mlx5_core_warn(dev, "failed dma mapping page\n");
309		err = -ENOMEM;
310		goto err_mapping;
311	}
312
313	/* Firmware doesn't support page with physical address 0 */
314	if (addr == 0) {
315		zero_addr = addr;
316		goto map;
317	}
318
319	err = insert_page(dev, addr, page, function);
320	if (err) {
321		mlx5_core_err(dev, "failed to track allocated page\n");
322		dma_unmap_page(device, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
323	}
324
325err_mapping:
326	if (err)
327		__free_page(page);
328
329	if (zero_addr == 0)
330		dma_unmap_page(device, zero_addr, PAGE_SIZE,
331			       DMA_BIDIRECTIONAL);
332
333	return err;
334}
335
336static void page_notify_fail(struct mlx5_core_dev *dev, u16 func_id,
337			     bool ec_function)
338{
339	u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
340	int err;
341
342	MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
343	MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_CANT_GIVE);
344	MLX5_SET(manage_pages_in, in, function_id, func_id);
345	MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
346
347	err = mlx5_cmd_exec_in(dev, manage_pages, in);
348	if (err)
349		mlx5_core_warn(dev, "page notify failed func_id(%d) err(%d)\n",
350			       func_id, err);
351}
352
353static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
354		      int event, bool ec_function)
355{
356	u32 function = get_function(func_id, ec_function);
357	u32 out[MLX5_ST_SZ_DW(manage_pages_out)] = {0};
358	int inlen = MLX5_ST_SZ_BYTES(manage_pages_in);
359	int notify_fail = event;
360	u16 func_type;
361	u64 addr;
362	int err;
363	u32 *in;
364	int i;
365
366	inlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_in, pas[0]);
367	in = kvzalloc(inlen, GFP_KERNEL);
368	if (!in) {
369		err = -ENOMEM;
370		mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
371		goto out_free;
372	}
373
374	for (i = 0; i < npages; i++) {
375retry:
376		err = alloc_4k(dev, &addr, function);
377		if (err) {
378			if (err == -ENOMEM)
379				err = alloc_system_page(dev, function);
380			if (err) {
381				dev->priv.fw_pages_alloc_failed += (npages - i);
382				goto out_4k;
383			}
384
385			goto retry;
386		}
387		MLX5_ARRAY_SET64(manage_pages_in, in, pas, i, addr);
388	}
389
390	MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
391	MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_GIVE);
392	MLX5_SET(manage_pages_in, in, function_id, func_id);
393	MLX5_SET(manage_pages_in, in, input_num_entries, npages);
394	MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
395
396	err = mlx5_cmd_do(dev, in, inlen, out, sizeof(out));
397	if (err == -EREMOTEIO) {
398		notify_fail = 0;
399		/* if triggered by FW and failed by FW ignore */
400		if (event) {
401			err = 0;
402			goto out_dropped;
403		}
404	}
405	err = mlx5_cmd_check(dev, err, in, out);
406	if (err) {
407		mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
408			       func_id, npages, err);
409		goto out_dropped;
410	}
411
412	func_type = func_id_to_type(dev, func_id, ec_function);
413	dev->priv.page_counters[func_type] += npages;
414	dev->priv.fw_pages += npages;
415
416	mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x, err %d\n",
417		      npages, ec_function, func_id, err);
418
419	kvfree(in);
420	return 0;
421
422out_dropped:
423	dev->priv.give_pages_dropped += npages;
424out_4k:
425	for (i--; i >= 0; i--)
426		free_4k(dev, MLX5_GET64(manage_pages_in, in, pas[i]), function);
427out_free:
428	kvfree(in);
429	if (notify_fail)
430		page_notify_fail(dev, func_id, ec_function);
431	return err;
432}
433
434static void release_all_pages(struct mlx5_core_dev *dev, u16 func_id,
435			      bool ec_function)
436{
437	u32 function = get_function(func_id, ec_function);
438	struct rb_root *root;
439	struct rb_node *p;
440	int npages = 0;
441	u16 func_type;
442
443	root = xa_load(&dev->priv.page_root_xa, function);
444	if (WARN_ON_ONCE(!root))
445		return;
446
447	p = rb_first(root);
448	while (p) {
449		struct fw_page *fwp = rb_entry(p, struct fw_page, rb_node);
450
451		p = rb_next(p);
452		npages += (MLX5_NUM_4K_IN_PAGE - fwp->free_count);
453		free_fwp(dev, fwp, fwp->free_count);
454	}
455
456	func_type = func_id_to_type(dev, func_id, ec_function);
457	dev->priv.page_counters[func_type] -= npages;
458	dev->priv.fw_pages -= npages;
459
460	mlx5_core_dbg(dev, "npages %d, ec_function %d, func_id 0x%x\n",
461		      npages, ec_function, func_id);
462}
463
464static u32 fwp_fill_manage_pages_out(struct fw_page *fwp, u32 *out, u32 index,
465				     u32 npages)
466{
467	u32 pages_set = 0;
468	unsigned int n;
469
470	for_each_clear_bit(n, &fwp->bitmask, MLX5_NUM_4K_IN_PAGE) {
471		MLX5_ARRAY_SET64(manage_pages_out, out, pas, index + pages_set,
472				 fwp->addr + (n * MLX5_ADAPTER_PAGE_SIZE));
473		pages_set++;
474
475		if (!--npages)
476			break;
477	}
478
479	return pages_set;
480}
481
482static int reclaim_pages_cmd(struct mlx5_core_dev *dev,
483			     u32 *in, int in_size, u32 *out, int out_size)
484{
485	struct rb_root *root;
486	struct fw_page *fwp;
487	struct rb_node *p;
488	bool ec_function;
489	u32 func_id;
490	u32 npages;
491	u32 i = 0;
492	int err;
493
494	err = mlx5_cmd_do(dev, in, in_size, out, out_size);
495	/* If FW is gone (-ENXIO), proceed to forceful reclaim */
496	if (err != -ENXIO)
497		return err;
498
499	/* No hard feelings, we want our pages back! */
500	npages = MLX5_GET(manage_pages_in, in, input_num_entries);
501	func_id = MLX5_GET(manage_pages_in, in, function_id);
502	ec_function = MLX5_GET(manage_pages_in, in, embedded_cpu_function);
503
504	root = xa_load(&dev->priv.page_root_xa, get_function(func_id, ec_function));
505	if (WARN_ON_ONCE(!root))
506		return -EEXIST;
507
508	p = rb_first(root);
509	while (p && i < npages) {
510		fwp = rb_entry(p, struct fw_page, rb_node);
511		p = rb_next(p);
512
513		i += fwp_fill_manage_pages_out(fwp, out, i, npages - i);
514	}
515
516	MLX5_SET(manage_pages_out, out, output_num_entries, i);
517	return 0;
518}
519
520static int reclaim_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
521			 int *nclaimed, bool event, bool ec_function)
522{
523	u32 function = get_function(func_id, ec_function);
524	int outlen = MLX5_ST_SZ_BYTES(manage_pages_out);
525	u32 in[MLX5_ST_SZ_DW(manage_pages_in)] = {};
526	int num_claimed;
527	u16 func_type;
528	u32 *out;
529	int err;
530	int i;
531
532	if (nclaimed)
533		*nclaimed = 0;
534
535	outlen += npages * MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
536	out = kvzalloc(outlen, GFP_KERNEL);
537	if (!out)
538		return -ENOMEM;
539
540	MLX5_SET(manage_pages_in, in, opcode, MLX5_CMD_OP_MANAGE_PAGES);
541	MLX5_SET(manage_pages_in, in, op_mod, MLX5_PAGES_TAKE);
542	MLX5_SET(manage_pages_in, in, function_id, func_id);
543	MLX5_SET(manage_pages_in, in, input_num_entries, npages);
544	MLX5_SET(manage_pages_in, in, embedded_cpu_function, ec_function);
545
546	mlx5_core_dbg(dev, "func 0x%x, npages %d, outlen %d\n",
547		      func_id, npages, outlen);
548	err = reclaim_pages_cmd(dev, in, sizeof(in), out, outlen);
549	if (err) {
550		npages = MLX5_GET(manage_pages_in, in, input_num_entries);
551		dev->priv.reclaim_pages_discard += npages;
552	}
553	/* if triggered by FW event and failed by FW then ignore */
554	if (event && err == -EREMOTEIO) {
555		err = 0;
556		goto out_free;
557	}
558
559	err = mlx5_cmd_check(dev, err, in, out);
560	if (err) {
561		mlx5_core_err(dev, "failed reclaiming pages: err %d\n", err);
562		goto out_free;
563	}
564
565	num_claimed = MLX5_GET(manage_pages_out, out, output_num_entries);
566	if (num_claimed > npages) {
567		mlx5_core_warn(dev, "fw returned %d, driver asked %d => corruption\n",
568			       num_claimed, npages);
569		err = -EINVAL;
570		goto out_free;
571	}
572
573	for (i = 0; i < num_claimed; i++)
574		free_4k(dev, MLX5_GET64(manage_pages_out, out, pas[i]), function);
575
576	if (nclaimed)
577		*nclaimed = num_claimed;
578
579	func_type = func_id_to_type(dev, func_id, ec_function);
580	dev->priv.page_counters[func_type] -= num_claimed;
581	dev->priv.fw_pages -= num_claimed;
582
583out_free:
584	kvfree(out);
585	return err;
586}
587
588static void pages_work_handler(struct work_struct *work)
589{
590	struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
591	struct mlx5_core_dev *dev = req->dev;
592	int err = 0;
593
594	if (req->release_all)
595		release_all_pages(dev, req->func_id, req->ec_function);
596	else if (req->npages < 0)
597		err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL,
598				    true, req->ec_function);
599	else if (req->npages > 0)
600		err = give_pages(dev, req->func_id, req->npages, 1, req->ec_function);
601
602	if (err)
603		mlx5_core_warn(dev, "%s fail %d\n",
604			       req->npages < 0 ? "reclaim" : "give", err);
605
606	kfree(req);
607}
608
609enum {
610	EC_FUNCTION_MASK = 0x8000,
611	RELEASE_ALL_PAGES_MASK = 0x4000,
612};
613
614/* This limit is based on the capability of the firmware as it cannot release
615 * more than 50000 back to the host in one go.
616 */
617#define MAX_RECLAIM_NPAGES (-50000)
618
619static int req_pages_handler(struct notifier_block *nb,
620			     unsigned long type, void *data)
621{
622	struct mlx5_pages_req *req;
623	struct mlx5_core_dev *dev;
624	struct mlx5_priv *priv;
625	struct mlx5_eqe *eqe;
626	bool ec_function;
627	bool release_all;
628	u16 func_id;
629	s32 npages;
630
631	priv = mlx5_nb_cof(nb, struct mlx5_priv, pg_nb);
632	dev  = container_of(priv, struct mlx5_core_dev, priv);
633	eqe  = data;
634
635	func_id = be16_to_cpu(eqe->data.req_pages.func_id);
636	npages  = be32_to_cpu(eqe->data.req_pages.num_pages);
637	ec_function = be16_to_cpu(eqe->data.req_pages.ec_function) & EC_FUNCTION_MASK;
638	release_all = be16_to_cpu(eqe->data.req_pages.ec_function) &
639		      RELEASE_ALL_PAGES_MASK;
640	mlx5_core_dbg(dev, "page request for func 0x%x, npages %d, release_all %d\n",
641		      func_id, npages, release_all);
642	req = kzalloc(sizeof(*req), GFP_ATOMIC);
643	if (!req) {
644		mlx5_core_warn(dev, "failed to allocate pages request\n");
645		return NOTIFY_DONE;
646	}
647
648	req->dev = dev;
649	req->func_id = func_id;
650
651	/* npages > 0 means HCA asking host to allocate/give pages,
652	 * npages < 0 means HCA asking host to reclaim back the pages allocated.
653	 * Here we are restricting the maximum number of pages that can be
654	 * reclaimed to be MAX_RECLAIM_NPAGES. Note that MAX_RECLAIM_NPAGES is
655	 * a negative value.
656	 * Since MAX_RECLAIM is negative, we are using max() to restrict
657	 * req->npages (and not min ()).
658	 */
659	req->npages = max_t(s32, npages, MAX_RECLAIM_NPAGES);
660	req->ec_function = ec_function;
661	req->release_all = release_all;
662	INIT_WORK(&req->work, pages_work_handler);
663	queue_work(dev->priv.pg_wq, &req->work);
664	return NOTIFY_OK;
665}
666
667int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
668{
669	u16 func_id;
670	s32 npages;
671	int err;
672
673	err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
674	if (err)
675		return err;
676
677	mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
678		      npages, boot ? "boot" : "init", func_id);
679
680	if (!npages)
681		return 0;
682
683	return give_pages(dev, func_id, npages, 0, mlx5_core_is_ecpf(dev));
684}
685
686enum {
687	MLX5_BLKS_FOR_RECLAIM_PAGES = 12
688};
689
690static int optimal_reclaimed_pages(void)
691{
692	struct mlx5_cmd_prot_block *block;
693	struct mlx5_cmd_layout *lay;
694	int ret;
695
696	ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
697	       MLX5_ST_SZ_BYTES(manage_pages_out)) /
698	       MLX5_FLD_SZ_BYTES(manage_pages_out, pas[0]);
699
700	return ret;
701}
702
703static int mlx5_reclaim_root_pages(struct mlx5_core_dev *dev,
704				   struct rb_root *root, u32 function)
705{
706	u64 recl_pages_to_jiffies = msecs_to_jiffies(mlx5_tout_ms(dev, RECLAIM_PAGES));
707	unsigned long end = jiffies + recl_pages_to_jiffies;
708
709	while (!RB_EMPTY_ROOT(root)) {
710		u32 ec_function = mlx5_get_ec_function(function);
711		u32 function_id = mlx5_get_func_id(function);
712		int nclaimed;
713		int err;
714
715		err = reclaim_pages(dev, function_id, optimal_reclaimed_pages(),
716				    &nclaimed, false, ec_function);
717		if (err) {
718			mlx5_core_warn(dev, "reclaim_pages err (%d) func_id=0x%x ec_func=0x%x\n",
719				       err, function_id, ec_function);
720			return err;
721		}
722
723		if (nclaimed)
724			end = jiffies + recl_pages_to_jiffies;
725
726		if (time_after(jiffies, end)) {
727			mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
728			break;
729		}
730	}
731
732	return 0;
733}
734
735int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
736{
737	struct rb_root *root;
738	unsigned long id;
739	void *entry;
740
741	xa_for_each(&dev->priv.page_root_xa, id, entry) {
742		root = entry;
743		mlx5_reclaim_root_pages(dev, root, id);
744		xa_erase(&dev->priv.page_root_xa, id);
745		kfree(root);
746	}
747
748	WARN_ON(!xa_empty(&dev->priv.page_root_xa));
749
750	WARN(dev->priv.fw_pages,
751	     "FW pages counter is %d after reclaiming all pages\n",
752	     dev->priv.fw_pages);
753	WARN(dev->priv.page_counters[MLX5_VF],
754	     "VFs FW pages counter is %d after reclaiming all pages\n",
755	     dev->priv.page_counters[MLX5_VF]);
756	WARN(dev->priv.page_counters[MLX5_HOST_PF],
757	     "External host PF FW pages counter is %d after reclaiming all pages\n",
758	     dev->priv.page_counters[MLX5_HOST_PF]);
759	WARN(dev->priv.page_counters[MLX5_EC_VF],
760	     "EC VFs FW pages counter is %d after reclaiming all pages\n",
761	     dev->priv.page_counters[MLX5_EC_VF]);
762
763	return 0;
764}
765
766int mlx5_pagealloc_init(struct mlx5_core_dev *dev)
767{
768	INIT_LIST_HEAD(&dev->priv.free_list);
769	dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
770	if (!dev->priv.pg_wq)
771		return -ENOMEM;
772
773	xa_init(&dev->priv.page_root_xa);
774	mlx5_pages_debugfs_init(dev);
775
776	return 0;
777}
778
779void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
780{
781	mlx5_pages_debugfs_cleanup(dev);
782	xa_destroy(&dev->priv.page_root_xa);
783	destroy_workqueue(dev->priv.pg_wq);
784}
785
786void mlx5_pagealloc_start(struct mlx5_core_dev *dev)
787{
788	MLX5_NB_INIT(&dev->priv.pg_nb, req_pages_handler, PAGE_REQUEST);
789	mlx5_eq_notifier_register(dev, &dev->priv.pg_nb);
790}
791
792void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
793{
794	mlx5_eq_notifier_unregister(dev, &dev->priv.pg_nb);
795	flush_workqueue(dev->priv.pg_wq);
796}
797
798int mlx5_wait_for_pages(struct mlx5_core_dev *dev, int *pages)
799{
800	u64 recl_vf_pages_to_jiffies = msecs_to_jiffies(mlx5_tout_ms(dev, RECLAIM_VFS_PAGES));
801	unsigned long end = jiffies + recl_vf_pages_to_jiffies;
802	int prev_pages = *pages;
803
804	/* In case of internal error we will free the pages manually later */
805	if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
806		mlx5_core_warn(dev, "Skipping wait for vf pages stage");
807		return 0;
808	}
809
810	mlx5_core_dbg(dev, "Waiting for %d pages\n", prev_pages);
811	while (*pages) {
812		if (time_after(jiffies, end)) {
813			mlx5_core_warn(dev, "aborting while there are %d pending pages\n", *pages);
814			return -ETIMEDOUT;
815		}
816		if (*pages < prev_pages) {
817			end = jiffies + recl_vf_pages_to_jiffies;
818			prev_pages = *pages;
819		}
820		msleep(50);
821	}
822
823	mlx5_core_dbg(dev, "All pages received\n");
824	return 0;
825}
~/ Read-only file preview.