1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /*
3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
4 */
5
6 #ifndef _RDMA_RESTRACK_H_
7 #define _RDMA_RESTRACK_H_
8
9 #include <linux/typecheck.h>
10 #include <linux/sched.h>
11 #include <linux/kref.h>
12 #include <linux/completion.h>
13 #include <linux/sched/task.h>
14 #include <uapi/rdma/rdma_netlink.h>
15 #include <linux/xarray.h>
16
17 struct ib_device;
18 struct sk_buff;
19
20 /**
21 * enum rdma_restrack_type - HW objects to track
22 */
23 enum rdma_restrack_type {
24 /**
25 * @RDMA_RESTRACK_PD: Protection domain (PD)
26 */
27 RDMA_RESTRACK_PD,
28 /**
29 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
30 */
31 RDMA_RESTRACK_CQ,
32 /**
33 * @RDMA_RESTRACK_QP: Queue pair (QP)
34 */
35 RDMA_RESTRACK_QP,
36 /**
37 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
38 */
39 RDMA_RESTRACK_CM_ID,
40 /**
41 * @RDMA_RESTRACK_MR: Memory Region (MR)
42 */
43 RDMA_RESTRACK_MR,
44 /**
45 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
46 */
47 RDMA_RESTRACK_CTX,
48 /**
49 * @RDMA_RESTRACK_COUNTER: Statistic Counter
50 */
51 RDMA_RESTRACK_COUNTER,
52 /**
53 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
54 */
55 RDMA_RESTRACK_MAX
56 };
57
58 /**
59 * struct rdma_restrack_entry - metadata per-entry
60 */
61 struct rdma_restrack_entry {
62 /**
63 * @valid: validity indicator
64 *
65 * The entries are filled during rdma_restrack_add,
66 * can be attempted to be free during rdma_restrack_del.
67 *
68 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
69 */
70 bool valid;
71 /*
72 * @kref: Protect destroy of the resource
73 */
74 struct kref kref;
75 /*
76 * @comp: Signal that all consumers of resource are completed their work
77 */
78 struct completion comp;
79 /**
80 * @task: owner of resource tracking entity
81 *
82 * There are two types of entities: created by user and created
83 * by kernel.
84 *
85 * This is relevant for the entities created by users.
86 * For the entities created by kernel, this pointer will be NULL.
87 */
88 struct task_struct *task;
89 /**
90 * @kern_name: name of owner for the kernel created entities.
91 */
92 const char *kern_name;
93 /**
94 * @type: various objects in restrack database
95 */
96 enum rdma_restrack_type type;
97 /**
98 * @user: user resource
99 */
100 bool user;
101 /**
102 * @id: ID to expose to users
103 */
104 u32 id;
105 };
106
107 int rdma_restrack_count(struct ib_device *dev,
108 enum rdma_restrack_type type);
109 /**
110 * rdma_is_kernel_res() - check the owner of resource
111 * @res: resource entry
112 */
rdma_is_kernel_res(const struct rdma_restrack_entry * res)113 static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
114 {
115 return !res->user;
116 }
117
118 /**
119 * rdma_restrack_get() - grab to protect resource from release
120 * @res: resource entry
121 */
122 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
123
124 /**
125 * rdma_restrack_put() - release resource
126 * @res: resource entry
127 */
128 int rdma_restrack_put(struct rdma_restrack_entry *res);
129
130 /*
131 * Helper functions for rdma drivers when filling out
132 * nldev driver attributes.
133 */
134 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
135 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
136 u32 value);
137 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
138 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
139 u64 value);
140 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
141 const char *str);
142 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
143 u64 value);
144
145 struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
146 enum rdma_restrack_type type,
147 u32 id);
148 #endif /* _RDMA_RESTRACK_H_ */
149