blob: 79d109c472423188e2b8566122a4a6e06572ddab [file] [log] [blame]
Leon Romanovsky33edc3b2018-06-05 07:48:08 +03001/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
Leon Romanovsky02d88832018-01-28 11:17:20 +02002/*
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>
Leon Romanovsky02d88832018-01-28 11:17:20 +020010#include <linux/sched.h>
11#include <linux/kref.h>
12#include <linux/completion.h>
Steve Wise00313982018-03-01 13:57:44 -080013#include <linux/sched/task.h>
Steve Wise73937e82018-05-03 08:41:42 -070014#include <uapi/rdma/rdma_netlink.h>
Leon Romanovskyfd47c2f2019-02-18 22:25:43 +020015#include <linux/xarray.h>
Leon Romanovsky02d88832018-01-28 11:17:20 +020016
Jason Gunthorpe390d5772019-07-09 09:44:47 -030017struct ib_device;
18struct sk_buff;
19
Leon Romanovsky02d88832018-01-28 11:17:20 +020020/**
21 * enum rdma_restrack_type - HW objects to track
22 */
23enum 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 /**
Steve Wise00313982018-03-01 13:57:44 -080037 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
38 */
39 RDMA_RESTRACK_CM_ID,
40 /**
Steve Wisefccec5b2018-03-01 13:58:13 -080041 * @RDMA_RESTRACK_MR: Memory Region (MR)
42 */
43 RDMA_RESTRACK_MR,
44 /**
Leon Romanovsky60615212018-11-28 13:16:43 +020045 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
46 */
47 RDMA_RESTRACK_CTX,
48 /**
Mark Zhang7ade1ff2019-07-02 13:02:31 +030049 * @RDMA_RESTRACK_COUNTER: Statistic Counter
50 */
51 RDMA_RESTRACK_COUNTER,
52 /**
Neta Ostrovsky48f8a702021-04-18 16:41:24 +030053 * @RDMA_RESTRACK_SRQ: Shared receive queue (SRQ)
54 */
55 RDMA_RESTRACK_SRQ,
56 /**
Leon Romanovsky02d88832018-01-28 11:17:20 +020057 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
58 */
59 RDMA_RESTRACK_MAX
60};
61
Leon Romanovsky02d88832018-01-28 11:17:20 +020062/**
63 * struct rdma_restrack_entry - metadata per-entry
64 */
65struct rdma_restrack_entry {
66 /**
67 * @valid: validity indicator
68 *
69 * The entries are filled during rdma_restrack_add,
70 * can be attempted to be free during rdma_restrack_del.
71 *
72 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
73 */
74 bool valid;
Leon Romanovsky2b1f7472020-11-17 09:01:47 +020075 /**
76 * @no_track: don't add this entry to restrack DB
77 *
78 * This field is used to mark an entry that doesn't need to be added to
79 * internal restrack DB and presented later to the users at the nldev
80 * query stage.
81 */
82 u8 no_track : 1;
Leon Romanovsky02d88832018-01-28 11:17:20 +020083 /*
84 * @kref: Protect destroy of the resource
85 */
86 struct kref kref;
87 /*
88 * @comp: Signal that all consumers of resource are completed their work
89 */
90 struct completion comp;
91 /**
92 * @task: owner of resource tracking entity
93 *
94 * There are two types of entities: created by user and created
95 * by kernel.
96 *
97 * This is relevant for the entities created by users.
98 * For the entities created by kernel, this pointer will be NULL.
99 */
100 struct task_struct *task;
101 /**
102 * @kern_name: name of owner for the kernel created entities.
103 */
104 const char *kern_name;
105 /**
Leon Romanovsky02d88832018-01-28 11:17:20 +0200106 * @type: various objects in restrack database
107 */
108 enum rdma_restrack_type type;
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200109 /**
110 * @user: user resource
111 */
112 bool user;
Leon Romanovskyfd47c2f2019-02-18 22:25:43 +0200113 /**
114 * @id: ID to expose to users
115 */
116 u32 id;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200117};
118
Leon Romanovsky0ad699c2019-01-30 12:48:58 +0200119int rdma_restrack_count(struct ib_device *dev,
Leon Romanovsky60c78662019-08-15 11:38:29 +0300120 enum rdma_restrack_type type);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200121/**
Leon Romanovsky02d88832018-01-28 11:17:20 +0200122 * rdma_is_kernel_res() - check the owner of resource
123 * @res: resource entry
124 */
Leon Romanovskyb09c4d72020-09-22 12:11:06 +0300125static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
Leon Romanovsky02d88832018-01-28 11:17:20 +0200126{
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200127 return !res->user;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200128}
129
130/**
131 * rdma_restrack_get() - grab to protect resource from release
132 * @res: resource entry
133 */
134int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
135
136/**
Leon Romanovsky03286032018-03-21 17:12:42 +0200137 * rdma_restrack_put() - release resource
Leon Romanovsky02d88832018-01-28 11:17:20 +0200138 * @res: resource entry
139 */
140int rdma_restrack_put(struct rdma_restrack_entry *res);
Steve Wise00313982018-03-01 13:57:44 -0800141
Steve Wise73937e82018-05-03 08:41:42 -0700142/*
143 * Helper functions for rdma drivers when filling out
144 * nldev driver attributes.
145 */
146int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
147int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
148 u32 value);
149int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
150int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
151 u64 value);
Erez Alfasie1b95ae2019-10-16 09:23:07 +0300152int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
153 const char *str);
Erez Alfasi4061ff72019-10-16 09:23:08 +0300154int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
155 u64 value);
Erez Alfasie1b95ae2019-10-16 09:23:07 +0300156
Leon Romanovsky18c4c662019-02-18 22:25:44 +0200157struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
158 enum rdma_restrack_type type,
159 u32 id);
Leon Romanovsky2b1f7472020-11-17 09:01:47 +0200160
161/**
162 * rdma_restrack_no_track() - don't add resource to the DB
163 * @res: resource entry
164 *
165 * Every user of thie API should be cross examined.
166 * Probaby you don't need to use this function.
167 */
168static inline void rdma_restrack_no_track(struct rdma_restrack_entry *res)
169{
170 res->no_track = true;
171}
172static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry *res)
173{
174 return !res->no_track;
175}
Leon Romanovsky02d88832018-01-28 11:17:20 +0200176#endif /* _RDMA_RESTRACK_H_ */