RDMA/restrack: Translate from ID to restrack object
[linux-2.6-microblaze.git] / include / rdma / restrack.h
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/rwsem.h>
11 #include <linux/sched.h>
12 #include <linux/kref.h>
13 #include <linux/completion.h>
14 #include <linux/sched/task.h>
15 #include <uapi/rdma/rdma_netlink.h>
16 #include <linux/xarray.h>
17
18 /**
19  * enum rdma_restrack_type - HW objects to track
20  */
21 enum rdma_restrack_type {
22         /**
23          * @RDMA_RESTRACK_PD: Protection domain (PD)
24          */
25         RDMA_RESTRACK_PD,
26         /**
27          * @RDMA_RESTRACK_CQ: Completion queue (CQ)
28          */
29         RDMA_RESTRACK_CQ,
30         /**
31          * @RDMA_RESTRACK_QP: Queue pair (QP)
32          */
33         RDMA_RESTRACK_QP,
34         /**
35          * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
36          */
37         RDMA_RESTRACK_CM_ID,
38         /**
39          * @RDMA_RESTRACK_MR: Memory Region (MR)
40          */
41         RDMA_RESTRACK_MR,
42         /**
43          * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
44          */
45         RDMA_RESTRACK_CTX,
46         /**
47          * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
48          */
49         RDMA_RESTRACK_MAX
50 };
51
52 struct ib_device;
53 struct rdma_restrack_entry;
54
55 /**
56  * struct rdma_restrack_root - main resource tracking management
57  * entity, per-device
58  */
59 struct rdma_restrack_root {
60         /*
61          * @rwsem: Read/write lock to protect lists
62          */
63         struct rw_semaphore     rwsem;
64         /**
65          * @xa: Array of XArray structures to hold restrack entries.
66          * We want to use array of XArrays because insertion is type
67          * dependent. For types with xisiting unique ID (like QPN),
68          * we will insert to that unique index. For other types,
69          * we insert based on pointers and auto-allocate unique index.
70          */
71         struct xarray xa[RDMA_RESTRACK_MAX];
72         /**
73          * @next_id: Next ID to support cyclic allocation
74          */
75         u32 next_id[RDMA_RESTRACK_MAX];
76 };
77
78 /**
79  * struct rdma_restrack_entry - metadata per-entry
80  */
81 struct rdma_restrack_entry {
82         /**
83          * @valid: validity indicator
84          *
85          * The entries are filled during rdma_restrack_add,
86          * can be attempted to be free during rdma_restrack_del.
87          *
88          * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
89          */
90         bool                    valid;
91         /*
92          * @kref: Protect destroy of the resource
93          */
94         struct kref             kref;
95         /*
96          * @comp: Signal that all consumers of resource are completed their work
97          */
98         struct completion       comp;
99         /**
100          * @task: owner of resource tracking entity
101          *
102          * There are two types of entities: created by user and created
103          * by kernel.
104          *
105          * This is relevant for the entities created by users.
106          * For the entities created by kernel, this pointer will be NULL.
107          */
108         struct task_struct      *task;
109         /**
110          * @kern_name: name of owner for the kernel created entities.
111          */
112         const char              *kern_name;
113         /**
114          * @type: various objects in restrack database
115          */
116         enum rdma_restrack_type type;
117         /**
118          * @user: user resource
119          */
120         bool                    user;
121         /**
122          * @id: ID to expose to users
123          */
124         u32 id;
125 };
126
127 void rdma_restrack_init(struct ib_device *dev);
128 void rdma_restrack_clean(struct ib_device *dev);
129 int rdma_restrack_count(struct ib_device *dev,
130                         enum rdma_restrack_type type,
131                         struct pid_namespace *ns);
132
133 void rdma_restrack_kadd(struct rdma_restrack_entry *res);
134 void rdma_restrack_uadd(struct rdma_restrack_entry *res);
135
136 /**
137  * rdma_restrack_del() - delete object from the reource tracking database
138  * @res:  resource entry
139  * @type: actual type of object to operate
140  */
141 void rdma_restrack_del(struct rdma_restrack_entry *res);
142
143 /**
144  * rdma_is_kernel_res() - check the owner of resource
145  * @res:  resource entry
146  */
147 static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
148 {
149         return !res->user;
150 }
151
152 /**
153  * rdma_restrack_get() - grab to protect resource from release
154  * @res:  resource entry
155  */
156 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
157
158 /**
159  * rdma_restrack_put() - release resource
160  * @res:  resource entry
161  */
162 int rdma_restrack_put(struct rdma_restrack_entry *res);
163
164 /**
165  * rdma_restrack_set_task() - set the task for this resource
166  * @res:  resource entry
167  * @caller: kernel name, the current task will be used if the caller is NULL.
168  */
169 void rdma_restrack_set_task(struct rdma_restrack_entry *res,
170                             const char *caller);
171
172 /*
173  * Helper functions for rdma drivers when filling out
174  * nldev driver attributes.
175  */
176 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
177 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
178                                u32 value);
179 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
180 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
181                                u64 value);
182 struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
183                                                    enum rdma_restrack_type type,
184                                                    u32 id);
185 #endif /* _RDMA_RESTRACK_H_ */