/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */1/*2* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.3*/45#ifndef _RDMA_RESTRACK_H_6#define _RDMA_RESTRACK_H_78#include <linux/typecheck.h>9#include <linux/sched.h>10#include <linux/kref.h>11#include <linux/completion.h>12#include <linux/sched/task.h>13#include <uapi/rdma/rdma_netlink.h>14#include <linux/xarray.h>1516/* Mark entry as containing driver specific details, it is used to provide QP subtype for now */17#define RESTRACK_DD XA_MARK_11819struct ib_device;20struct sk_buff;2122/**23* enum rdma_restrack_type - HW objects to track24*/25enum rdma_restrack_type {26/**27* @RDMA_RESTRACK_PD: Protection domain (PD)28*/29RDMA_RESTRACK_PD,30/**31* @RDMA_RESTRACK_CQ: Completion queue (CQ)32*/33RDMA_RESTRACK_CQ,34/**35* @RDMA_RESTRACK_QP: Queue pair (QP)36*/37RDMA_RESTRACK_QP,38/**39* @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)40*/41RDMA_RESTRACK_CM_ID,42/**43* @RDMA_RESTRACK_MR: Memory Region (MR)44*/45RDMA_RESTRACK_MR,46/**47* @RDMA_RESTRACK_CTX: Verbs contexts (CTX)48*/49RDMA_RESTRACK_CTX,50/**51* @RDMA_RESTRACK_COUNTER: Statistic Counter52*/53RDMA_RESTRACK_COUNTER,54/**55* @RDMA_RESTRACK_SRQ: Shared receive queue (SRQ)56*/57RDMA_RESTRACK_SRQ,58/**59* @RDMA_RESTRACK_DMAH: DMA handle60*/61RDMA_RESTRACK_DMAH,62/**63* @RDMA_RESTRACK_MAX: Last entry, used for array dclarations64*/65RDMA_RESTRACK_MAX66};6768/**69* struct rdma_restrack_entry - metadata per-entry70*/71struct rdma_restrack_entry {72/**73* @valid: validity indicator74*75* The entries are filled during rdma_restrack_add,76* can be attempted to be free during rdma_restrack_del.77*78* As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI79*/80bool valid;81/**82* @no_track: don't add this entry to restrack DB83*84* This field is used to mark an entry that doesn't need to be added to85* internal restrack DB and presented later to the users at the nldev86* query stage.87*/88u8 no_track : 1;89/*90* @kref: Protect destroy of the resource91*/92struct kref kref;93/*94* @comp: Signal that all consumers of resource are completed their work95*/96struct completion comp;97/**98* @task: owner of resource tracking entity99*100* There are two types of entities: created by user and created101* by kernel.102*103* This is relevant for the entities created by users.104* For the entities created by kernel, this pointer will be NULL.105*/106struct task_struct *task;107/**108* @kern_name: name of owner for the kernel created entities.109*/110const char *kern_name;111/**112* @type: various objects in restrack database113*/114enum rdma_restrack_type type;115/**116* @user: user resource117*/118bool user;119/**120* @id: ID to expose to users121*/122u32 id;123};124125int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type,126bool show_details);127/**128* rdma_is_kernel_res() - check the owner of resource129* @res: resource entry130*/131static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)132{133return !res->user;134}135136/**137* rdma_restrack_get() - grab to protect resource from release138* @res: resource entry139*/140int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);141142/**143* rdma_restrack_put() - release resource144* @res: resource entry145*/146int rdma_restrack_put(struct rdma_restrack_entry *res);147148/*149* Helper functions for rdma drivers when filling out150* nldev driver attributes.151*/152int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);153int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,154u32 value);155int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);156int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,157u64 value);158int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,159const char *str);160int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,161u64 value);162163struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,164enum rdma_restrack_type type,165u32 id);166167/**168* rdma_restrack_no_track() - don't add resource to the DB169* @res: resource entry170*171* Every user of this API should be cross examined.172* Probably you don't need to use this function.173*/174static inline void rdma_restrack_no_track(struct rdma_restrack_entry *res)175{176res->no_track = true;177}178static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry *res)179{180return !res->no_track;181}182#endif /* _RDMA_RESTRACK_H_ */183184185