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 /* Mark entry as containing driver specific details, it is used to provide QP subtype for now */ 18 #define RESTRACK_DD XA_MARK_1 19 20 struct ib_device; 21 struct sk_buff; 22 23 /** 24 * enum rdma_restrack_type - HW objects to track 25 */ 26 enum rdma_restrack_type { 27 /** 28 * @RDMA_RESTRACK_PD: Protection domain (PD) 29 */ 30 RDMA_RESTRACK_PD, 31 /** 32 * @RDMA_RESTRACK_CQ: Completion queue (CQ) 33 */ 34 RDMA_RESTRACK_CQ, 35 /** 36 * @RDMA_RESTRACK_QP: Queue pair (QP) 37 */ 38 RDMA_RESTRACK_QP, 39 /** 40 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID) 41 */ 42 RDMA_RESTRACK_CM_ID, 43 /** 44 * @RDMA_RESTRACK_MR: Memory Region (MR) 45 */ 46 RDMA_RESTRACK_MR, 47 /** 48 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX) 49 */ 50 RDMA_RESTRACK_CTX, 51 /** 52 * @RDMA_RESTRACK_COUNTER: Statistic Counter 53 */ 54 RDMA_RESTRACK_COUNTER, 55 /** 56 * @RDMA_RESTRACK_SRQ: Shared receive queue (SRQ) 57 */ 58 RDMA_RESTRACK_SRQ, 59 /** 60 * @RDMA_RESTRACK_DMAH: DMA handle 61 */ 62 RDMA_RESTRACK_DMAH, 63 /** 64 * @RDMA_RESTRACK_COMP_CNTR: Completion Counter 65 */ 66 RDMA_RESTRACK_COMP_CNTR, 67 /** 68 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations 69 */ 70 RDMA_RESTRACK_MAX 71 }; 72 73 /** 74 * struct rdma_restrack_entry - metadata per-entry 75 */ 76 struct rdma_restrack_entry { 77 /** 78 * @valid: validity indicator 79 * 80 * The entries are filled during rdma_restrack_add, 81 * can be attempted to be free during rdma_restrack_del. 82 * 83 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI 84 */ 85 bool valid; 86 /** 87 * @no_track: don't add this entry to restrack DB 88 * 89 * This field is used to mark an entry that doesn't need to be added to 90 * internal restrack DB and presented later to the users at the nldev 91 * query stage. 92 */ 93 u8 no_track : 1; 94 /** 95 * @kref: Protect destroy of the resource 96 */ 97 struct kref kref; 98 /** 99 * @comp: Signal that all consumers of resource are completed their work 100 */ 101 struct completion comp; 102 /** 103 * @task: owner of resource tracking entity 104 * 105 * There are two types of entities: created by user and created 106 * by kernel. 107 * 108 * This is relevant for the entities created by users. 109 * For the entities created by kernel, this pointer will be NULL. 110 */ 111 struct task_struct *task; 112 /** 113 * @kern_name: name of owner for the kernel created entities. 114 */ 115 const char *kern_name; 116 /** 117 * @type: various objects in restrack database 118 */ 119 enum rdma_restrack_type type; 120 /** 121 * @user: user resource 122 */ 123 bool user; 124 /** 125 * @id: ID to expose to users 126 */ 127 u32 id; 128 }; 129 130 u32 rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type, 131 bool show_details); 132 /** 133 * rdma_is_kernel_res() - check the owner of resource 134 * @res: resource entry 135 */ 136 static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res) 137 { 138 return !res->user; 139 } 140 141 /** 142 * rdma_restrack_get() - grab to protect resource from release 143 * @res: resource entry 144 */ 145 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res); 146 147 /** 148 * rdma_restrack_put() - release resource 149 * @res: resource entry 150 */ 151 int rdma_restrack_put(struct rdma_restrack_entry *res); 152 153 /* 154 * Helper functions for rdma drivers when filling out 155 * nldev driver attributes. 156 */ 157 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value); 158 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, 159 u32 value); 160 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value); 161 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, 162 u64 value); 163 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name, 164 const char *str); 165 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name, 166 u64 value); 167 168 struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev, 169 enum rdma_restrack_type type, 170 u32 id); 171 172 /** 173 * rdma_restrack_no_track() - don't add resource to the DB 174 * @res: resource entry 175 * 176 * Every user of this API should be cross examined. 177 * Probably you don't need to use this function. 178 */ 179 static inline void rdma_restrack_no_track(struct rdma_restrack_entry *res) 180 { 181 res->no_track = true; 182 } 183 static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry *res) 184 { 185 return !res->no_track; 186 } 187 #endif /* _RDMA_RESTRACK_H_ */ 188