18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef _RDMA_RESTRACK_H_
78c2ecf20Sopenharmony_ci#define _RDMA_RESTRACK_H_
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/typecheck.h>
108c2ecf20Sopenharmony_ci#include <linux/sched.h>
118c2ecf20Sopenharmony_ci#include <linux/kref.h>
128c2ecf20Sopenharmony_ci#include <linux/completion.h>
138c2ecf20Sopenharmony_ci#include <linux/sched/task.h>
148c2ecf20Sopenharmony_ci#include <uapi/rdma/rdma_netlink.h>
158c2ecf20Sopenharmony_ci#include <linux/xarray.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistruct ib_device;
188c2ecf20Sopenharmony_cistruct sk_buff;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/**
218c2ecf20Sopenharmony_ci * enum rdma_restrack_type - HW objects to track
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_cienum rdma_restrack_type {
248c2ecf20Sopenharmony_ci	/**
258c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_PD: Protection domain (PD)
268c2ecf20Sopenharmony_ci	 */
278c2ecf20Sopenharmony_ci	RDMA_RESTRACK_PD,
288c2ecf20Sopenharmony_ci	/**
298c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
308c2ecf20Sopenharmony_ci	 */
318c2ecf20Sopenharmony_ci	RDMA_RESTRACK_CQ,
328c2ecf20Sopenharmony_ci	/**
338c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_QP: Queue pair (QP)
348c2ecf20Sopenharmony_ci	 */
358c2ecf20Sopenharmony_ci	RDMA_RESTRACK_QP,
368c2ecf20Sopenharmony_ci	/**
378c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
388c2ecf20Sopenharmony_ci	 */
398c2ecf20Sopenharmony_ci	RDMA_RESTRACK_CM_ID,
408c2ecf20Sopenharmony_ci	/**
418c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_MR: Memory Region (MR)
428c2ecf20Sopenharmony_ci	 */
438c2ecf20Sopenharmony_ci	RDMA_RESTRACK_MR,
448c2ecf20Sopenharmony_ci	/**
458c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
468c2ecf20Sopenharmony_ci	 */
478c2ecf20Sopenharmony_ci	RDMA_RESTRACK_CTX,
488c2ecf20Sopenharmony_ci	/**
498c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_COUNTER: Statistic Counter
508c2ecf20Sopenharmony_ci	 */
518c2ecf20Sopenharmony_ci	RDMA_RESTRACK_COUNTER,
528c2ecf20Sopenharmony_ci	/**
538c2ecf20Sopenharmony_ci	 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
548c2ecf20Sopenharmony_ci	 */
558c2ecf20Sopenharmony_ci	RDMA_RESTRACK_MAX
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/**
598c2ecf20Sopenharmony_ci * struct rdma_restrack_entry - metadata per-entry
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cistruct rdma_restrack_entry {
628c2ecf20Sopenharmony_ci	/**
638c2ecf20Sopenharmony_ci	 * @valid: validity indicator
648c2ecf20Sopenharmony_ci	 *
658c2ecf20Sopenharmony_ci	 * The entries are filled during rdma_restrack_add,
668c2ecf20Sopenharmony_ci	 * can be attempted to be free during rdma_restrack_del.
678c2ecf20Sopenharmony_ci	 *
688c2ecf20Sopenharmony_ci	 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	bool			valid;
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * @kref: Protect destroy of the resource
738c2ecf20Sopenharmony_ci	 */
748c2ecf20Sopenharmony_ci	struct kref		kref;
758c2ecf20Sopenharmony_ci	/*
768c2ecf20Sopenharmony_ci	 * @comp: Signal that all consumers of resource are completed their work
778c2ecf20Sopenharmony_ci	 */
788c2ecf20Sopenharmony_ci	struct completion	comp;
798c2ecf20Sopenharmony_ci	/**
808c2ecf20Sopenharmony_ci	 * @task: owner of resource tracking entity
818c2ecf20Sopenharmony_ci	 *
828c2ecf20Sopenharmony_ci	 * There are two types of entities: created by user and created
838c2ecf20Sopenharmony_ci	 * by kernel.
848c2ecf20Sopenharmony_ci	 *
858c2ecf20Sopenharmony_ci	 * This is relevant for the entities created by users.
868c2ecf20Sopenharmony_ci	 * For the entities created by kernel, this pointer will be NULL.
878c2ecf20Sopenharmony_ci	 */
888c2ecf20Sopenharmony_ci	struct task_struct	*task;
898c2ecf20Sopenharmony_ci	/**
908c2ecf20Sopenharmony_ci	 * @kern_name: name of owner for the kernel created entities.
918c2ecf20Sopenharmony_ci	 */
928c2ecf20Sopenharmony_ci	const char		*kern_name;
938c2ecf20Sopenharmony_ci	/**
948c2ecf20Sopenharmony_ci	 * @type: various objects in restrack database
958c2ecf20Sopenharmony_ci	 */
968c2ecf20Sopenharmony_ci	enum rdma_restrack_type	type;
978c2ecf20Sopenharmony_ci	/**
988c2ecf20Sopenharmony_ci	 * @user: user resource
998c2ecf20Sopenharmony_ci	 */
1008c2ecf20Sopenharmony_ci	bool			user;
1018c2ecf20Sopenharmony_ci	/**
1028c2ecf20Sopenharmony_ci	 * @id: ID to expose to users
1038c2ecf20Sopenharmony_ci	 */
1048c2ecf20Sopenharmony_ci	u32 id;
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciint rdma_restrack_count(struct ib_device *dev,
1088c2ecf20Sopenharmony_ci			enum rdma_restrack_type type);
1098c2ecf20Sopenharmony_ci/**
1108c2ecf20Sopenharmony_ci * rdma_is_kernel_res() - check the owner of resource
1118c2ecf20Sopenharmony_ci * @res:  resource entry
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistatic inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	return !res->user;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/**
1198c2ecf20Sopenharmony_ci * rdma_restrack_get() - grab to protect resource from release
1208c2ecf20Sopenharmony_ci * @res:  resource entry
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ciint __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/**
1258c2ecf20Sopenharmony_ci * rdma_restrack_put() - release resource
1268c2ecf20Sopenharmony_ci * @res:  resource entry
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_ciint rdma_restrack_put(struct rdma_restrack_entry *res);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/*
1318c2ecf20Sopenharmony_ci * Helper functions for rdma drivers when filling out
1328c2ecf20Sopenharmony_ci * nldev driver attributes.
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ciint rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
1358c2ecf20Sopenharmony_ciint rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
1368c2ecf20Sopenharmony_ci			       u32 value);
1378c2ecf20Sopenharmony_ciint rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
1388c2ecf20Sopenharmony_ciint rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
1398c2ecf20Sopenharmony_ci			       u64 value);
1408c2ecf20Sopenharmony_ciint rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
1418c2ecf20Sopenharmony_ci			      const char *str);
1428c2ecf20Sopenharmony_ciint rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
1438c2ecf20Sopenharmony_ci				 u64 value);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistruct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
1468c2ecf20Sopenharmony_ci						   enum rdma_restrack_type type,
1478c2ecf20Sopenharmony_ci						   u32 id);
1488c2ecf20Sopenharmony_ci#endif /* _RDMA_RESTRACK_H_ */
149