Leon Romanovsky | 33edc3b | 2018-06-05 07:48:08 +0300 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 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> |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 10 | #include <linux/sched.h> |
| 11 | #include <linux/kref.h> |
| 12 | #include <linux/completion.h> |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 13 | #include <linux/sched/task.h> |
Steve Wise | 73937e8 | 2018-05-03 08:41:42 -0700 | [diff] [blame] | 14 | #include <uapi/rdma/rdma_netlink.h> |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 15 | #include <linux/xarray.h> |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 16 | |
Jason Gunthorpe | 390d577 | 2019-07-09 09:44:47 -0300 | [diff] [blame] | 17 | struct ib_device; |
| 18 | struct sk_buff; |
| 19 | |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 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 | /** |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 37 | * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID) |
| 38 | */ |
| 39 | RDMA_RESTRACK_CM_ID, |
| 40 | /** |
Steve Wise | fccec5b | 2018-03-01 13:58:13 -0800 | [diff] [blame] | 41 | * @RDMA_RESTRACK_MR: Memory Region (MR) |
| 42 | */ |
| 43 | RDMA_RESTRACK_MR, |
| 44 | /** |
Leon Romanovsky | 6061521 | 2018-11-28 13:16:43 +0200 | [diff] [blame] | 45 | * @RDMA_RESTRACK_CTX: Verbs contexts (CTX) |
| 46 | */ |
| 47 | RDMA_RESTRACK_CTX, |
| 48 | /** |
Mark Zhang | 7ade1ff | 2019-07-02 13:02:31 +0300 | [diff] [blame] | 49 | * @RDMA_RESTRACK_COUNTER: Statistic Counter |
| 50 | */ |
| 51 | RDMA_RESTRACK_COUNTER, |
| 52 | /** |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 53 | * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations |
| 54 | */ |
| 55 | RDMA_RESTRACK_MAX |
| 56 | }; |
| 57 | |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 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 | /** |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 94 | * @type: various objects in restrack database |
| 95 | */ |
| 96 | enum rdma_restrack_type type; |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 97 | /** |
| 98 | * @user: user resource |
| 99 | */ |
| 100 | bool user; |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 101 | /** |
| 102 | * @id: ID to expose to users |
| 103 | */ |
| 104 | u32 id; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
Leon Romanovsky | 0ad699c | 2019-01-30 12:48:58 +0200 | [diff] [blame] | 107 | int rdma_restrack_count(struct ib_device *dev, |
Leon Romanovsky | 60c7866 | 2019-08-15 11:38:29 +0300 | [diff] [blame] | 108 | enum rdma_restrack_type type); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 109 | |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 110 | void rdma_restrack_kadd(struct rdma_restrack_entry *res); |
| 111 | void rdma_restrack_uadd(struct rdma_restrack_entry *res); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * rdma_restrack_del() - delete object from the reource tracking database |
| 115 | * @res: resource entry |
| 116 | * @type: actual type of object to operate |
| 117 | */ |
| 118 | void rdma_restrack_del(struct rdma_restrack_entry *res); |
| 119 | |
| 120 | /** |
| 121 | * rdma_is_kernel_res() - check the owner of resource |
| 122 | * @res: resource entry |
| 123 | */ |
| 124 | static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res) |
| 125 | { |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 126 | return !res->user; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * rdma_restrack_get() - grab to protect resource from release |
| 131 | * @res: resource entry |
| 132 | */ |
| 133 | int __must_check rdma_restrack_get(struct rdma_restrack_entry *res); |
| 134 | |
| 135 | /** |
Leon Romanovsky | 0328603 | 2018-03-21 17:12:42 +0200 | [diff] [blame] | 136 | * rdma_restrack_put() - release resource |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 137 | * @res: resource entry |
| 138 | */ |
| 139 | int rdma_restrack_put(struct rdma_restrack_entry *res); |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * rdma_restrack_set_task() - set the task for this resource |
| 143 | * @res: resource entry |
Leon Romanovsky | 2165fc2 | 2018-10-02 11:48:02 +0300 | [diff] [blame] | 144 | * @caller: kernel name, the current task will be used if the caller is NULL. |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 145 | */ |
Leon Romanovsky | 363ad35 | 2018-10-02 11:48:01 +0300 | [diff] [blame] | 146 | void rdma_restrack_set_task(struct rdma_restrack_entry *res, |
Leon Romanovsky | 2165fc2 | 2018-10-02 11:48:02 +0300 | [diff] [blame] | 147 | const char *caller); |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 148 | |
Steve Wise | 73937e8 | 2018-05-03 08:41:42 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Helper functions for rdma drivers when filling out |
| 151 | * nldev driver attributes. |
| 152 | */ |
| 153 | int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value); |
| 154 | int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, |
| 155 | u32 value); |
| 156 | int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value); |
| 157 | int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, |
| 158 | u64 value); |
Erez Alfasi | e1b95ae | 2019-10-16 09:23:07 +0300 | [diff] [blame] | 159 | int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name, |
| 160 | const char *str); |
Erez Alfasi | 4061ff7 | 2019-10-16 09:23:08 +0300 | [diff] [blame] | 161 | int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name, |
| 162 | u64 value); |
Erez Alfasi | e1b95ae | 2019-10-16 09:23:07 +0300 | [diff] [blame] | 163 | |
Leon Romanovsky | 18c4c66 | 2019-02-18 22:25:44 +0200 | [diff] [blame] | 164 | struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev, |
| 165 | enum rdma_restrack_type type, |
| 166 | u32 id); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 167 | #endif /* _RDMA_RESTRACK_H_ */ |