/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */1/*2* Copyright(c) 2016 - 2019 Intel Corporation.3*/45#ifndef DEF_RDMA_VT_H6#define DEF_RDMA_VT_H78/*9* Structure that low level drivers will populate in order to register with the10* rdmavt layer.11*/1213#include <linux/spinlock.h>14#include <linux/list.h>15#include <linux/hash.h>16#include <rdma/ib_verbs.h>17#include <rdma/ib_mad.h>18#include <rdma/rdmavt_mr.h>1920#define RVT_MAX_PKEY_VALUES 162122#define RVT_MAX_TRAP_LEN 100 /* Limit pending trap list */23#define RVT_MAX_TRAP_LISTS 5 /*((IB_NOTICE_TYPE_INFO & 0x0F) + 1)*/24#define RVT_TRAP_TIMEOUT 4096 /* 4.096 usec */2526struct trap_list {27u32 list_len;28struct list_head list;29};3031struct rvt_qp;32struct rvt_qpn_table;33struct rvt_ibport {34struct rvt_qp __rcu *qp[2];35struct ib_mad_agent *send_agent; /* agent for SMI (traps) */36struct rb_root mcast_tree;37spinlock_t lock; /* protect changes in this struct */3839/* non-zero when timer is set */40unsigned long mkey_lease_timeout;41unsigned long trap_timeout;42__be64 gid_prefix; /* in network order */43__be64 mkey;44u64 tid;45u32 port_cap_flags;46u16 port_cap3_flags;47u32 pma_sample_start;48u32 pma_sample_interval;49__be16 pma_counter_select[5];50u16 pma_tag;51u16 mkey_lease_period;52u32 sm_lid;53u8 sm_sl;54u8 mkeyprot;55u8 subnet_timeout;56u8 vl_high_limit;5758/*59* Driver is expected to keep these up to date. These60* counters are informational only and not required to be61* completely accurate.62*/63u64 n_rc_resends;64u64 n_seq_naks;65u64 n_rdma_seq;66u64 n_rnr_naks;67u64 n_other_naks;68u64 n_loop_pkts;69u64 n_pkt_drops;70u64 n_vl15_dropped;71u64 n_rc_timeouts;72u64 n_dmawait;73u64 n_unaligned;74u64 n_rc_dupreq;75u64 n_rc_seqnak;76u64 n_rc_crwaits;77u16 pkey_violations;78u16 qkey_violations;79u16 mkey_violations;8081/* Hot-path per CPU counters to avoid cacheline trading to update */82u64 z_rc_acks;83u64 z_rc_qacks;84u64 z_rc_delayed_comp;85u64 __percpu *rc_acks;86u64 __percpu *rc_qacks;87u64 __percpu *rc_delayed_comp;8889void *priv; /* driver private data */9091/*92* The pkey table is allocated and maintained by the driver. Drivers93* need to have access to this before registering with rdmav. However94* rdmavt will need access to it so drivers need to provide this during95* the attach port API call.96*/97u16 *pkey_table;9899struct rvt_ah *sm_ah;100101/*102* Keep a list of traps that have not been repressed. They will be103* resent based on trap_timer.104*/105struct trap_list trap_lists[RVT_MAX_TRAP_LISTS];106struct timer_list trap_timer;107};108109#define RVT_CQN_MAX 16 /* maximum length of cq name */110111#define RVT_SGE_COPY_MEMCPY 0112#define RVT_SGE_COPY_CACHELESS 1113#define RVT_SGE_COPY_ADAPTIVE 2114115/*116* Things that are driver specific, module parameters in hfi1 and qib117*/118struct rvt_driver_params {119struct ib_device_attr props;120121/*122* Anything driver specific that is not covered by props123* For instance special module parameters. Goes here.124*/125unsigned int lkey_table_size;126unsigned int qp_table_size;127unsigned int sge_copy_mode;128unsigned int wss_threshold;129unsigned int wss_clean_period;130int qpn_start;131int qpn_inc;132int qpn_res_start;133int qpn_res_end;134int nports;135int npkeys;136int node;137int psn_mask;138int psn_shift;139int psn_modify_mask;140u32 core_cap_flags;141u32 max_mad_size;142u8 qos_shift;143u8 max_rdma_atomic;144u8 extra_rdma_atomic;145u8 reserved_operations;146};147148/* User context */149struct rvt_ucontext {150struct ib_ucontext ibucontext;151};152153/* Protection domain */154struct rvt_pd {155struct ib_pd ibpd;156bool user;157};158159/* Address handle */160struct rvt_ah {161struct ib_ah ibah;162struct rdma_ah_attr attr;163u8 vl;164u8 log_pmtu;165};166167/*168* This structure is used by rvt_mmap() to validate an offset169* when an mmap() request is made. The vm_area_struct then uses170* this as its vm_private_data.171*/172struct rvt_mmap_info {173struct list_head pending_mmaps;174struct ib_ucontext *context;175void *obj;176__u64 offset;177struct kref ref;178u32 size;179};180181/* memory working set size */182struct rvt_wss {183unsigned long *entries;184atomic_t total_count;185atomic_t clean_counter;186atomic_t clean_entry;187188int threshold;189int num_entries;190long pages_mask;191unsigned int clean_period;192};193194struct rvt_dev_info;195struct rvt_swqe;196struct rvt_driver_provided {197/*198* Which functions are required depends on which verbs rdmavt is199* providing and which verbs the driver is overriding. See200* check_support() for details.201*/202203/* hot path calldowns in a single cacheline */204205/*206* Give the driver a notice that there is send work to do. It is up to207* the driver to generally push the packets out, this just queues the208* work with the driver. There are two variants here. The no_lock209* version requires the s_lock not to be held. The other assumes the210* s_lock is held.211*/212bool (*schedule_send)(struct rvt_qp *qp);213bool (*schedule_send_no_lock)(struct rvt_qp *qp);214215/*216* Driver specific work request setup and checking.217* This function is allowed to perform any setup, checks, or218* adjustments required to the SWQE in order to be usable by219* underlying protocols. This includes private data structure220* allocations.221*/222int (*setup_wqe)(struct rvt_qp *qp, struct rvt_swqe *wqe,223bool *call_send);224225/*226* Sometimes rdmavt needs to kick the driver's send progress. That is227* done by this call back.228*/229void (*do_send)(struct rvt_qp *qp);230231/*232* Returns a pointer to the underlying hardware's PCI device. This is233* used to display information as to what hardware is being referenced234* in an output message235*/236struct pci_dev * (*get_pci_dev)(struct rvt_dev_info *rdi);237238/*239* Allocate a private queue pair data structure for driver specific240* information which is opaque to rdmavt. Errors are returned via241* ERR_PTR(err). The driver is free to return NULL or a valid242* pointer.243*/244void * (*qp_priv_alloc)(struct rvt_dev_info *rdi, struct rvt_qp *qp);245246/*247* Init a structure allocated with qp_priv_alloc(). This should be248* called after all qp fields have been initialized in rdmavt.249*/250int (*qp_priv_init)(struct rvt_dev_info *rdi, struct rvt_qp *qp,251struct ib_qp_init_attr *init_attr);252253/*254* Free the driver's private qp structure.255*/256void (*qp_priv_free)(struct rvt_dev_info *rdi, struct rvt_qp *qp);257258/*259* Inform the driver the particular qp in question has been reset so260* that it can clean up anything it needs to.261*/262void (*notify_qp_reset)(struct rvt_qp *qp);263264/*265* Get a path mtu from the driver based on qp attributes.266*/267int (*get_pmtu_from_attr)(struct rvt_dev_info *rdi, struct rvt_qp *qp,268struct ib_qp_attr *attr);269270/*271* Notify driver that it needs to flush any outstanding IO requests that272* are waiting on a qp.273*/274void (*flush_qp_waiters)(struct rvt_qp *qp);275276/*277* Notify driver to stop its queue of sending packets. Nothing else278* should be posted to the queue pair after this has been called.279*/280void (*stop_send_queue)(struct rvt_qp *qp);281282/*283* Have the driver drain any in progress operations284*/285void (*quiesce_qp)(struct rvt_qp *qp);286287/*288* Inform the driver a qp has went to error state.289*/290void (*notify_error_qp)(struct rvt_qp *qp);291292/*293* Get an MTU for a qp.294*/295u32 (*mtu_from_qp)(struct rvt_dev_info *rdi, struct rvt_qp *qp,296u32 pmtu);297/*298* Convert an mtu to a path mtu299*/300int (*mtu_to_path_mtu)(u32 mtu);301302/*303* Get the guid of a port in big endian byte order304*/305int (*get_guid_be)(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,306int guid_index, __be64 *guid);307308/*309* Query driver for the state of the port.310*/311int (*query_port_state)(struct rvt_dev_info *rdi, u32 port_num,312struct ib_port_attr *props);313314/*315* Tell driver to shutdown a port316*/317int (*shut_down_port)(struct rvt_dev_info *rdi, u32 port_num);318319/* Tell driver to send a trap for changed port capabilities */320void (*cap_mask_chg)(struct rvt_dev_info *rdi, u32 port_num);321322/*323* The following functions can be safely ignored completely. Any use of324* these is checked for NULL before blindly calling. Rdmavt should also325* be functional if drivers omit these.326*/327328/* Called to inform the driver that all qps should now be freed. */329unsigned (*free_all_qps)(struct rvt_dev_info *rdi);330331/* Driver specific AH validation */332int (*check_ah)(struct ib_device *, struct rdma_ah_attr *);333334/* Inform the driver a new AH has been created */335void (*notify_new_ah)(struct ib_device *, struct rdma_ah_attr *,336struct rvt_ah *);337338/* Let the driver pick the next queue pair number*/339int (*alloc_qpn)(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,340enum ib_qp_type type, u32 port_num);341342/* Determine if its safe or allowed to modify the qp */343int (*check_modify_qp)(struct rvt_qp *qp, struct ib_qp_attr *attr,344int attr_mask, struct ib_udata *udata);345346/* Driver specific QP modification/notification-of */347void (*modify_qp)(struct rvt_qp *qp, struct ib_qp_attr *attr,348int attr_mask, struct ib_udata *udata);349350/* Notify driver a mad agent has been created */351void (*notify_create_mad_agent)(struct rvt_dev_info *rdi, int port_idx);352353/* Notify driver a mad agent has been removed */354void (*notify_free_mad_agent)(struct rvt_dev_info *rdi, int port_idx);355356/* Notify driver to restart rc */357void (*notify_restart_rc)(struct rvt_qp *qp, u32 psn, int wait);358359/* Get and return CPU to pin CQ processing thread */360int (*comp_vect_cpu_lookup)(struct rvt_dev_info *rdi, int comp_vect);361};362363struct rvt_dev_info {364struct ib_device ibdev; /* Keep this first. Nothing above here */365366/*367* Prior to calling for registration the driver will be responsible for368* allocating space for this structure.369*370* The driver will also be responsible for filling in certain members of371* dparms.props. The driver needs to fill in dparms exactly as it would372* want values reported to a ULP. This will be returned to the caller373* in rdmavt's device. The driver should also therefore refrain from374* modifying this directly after registration with rdmavt.375*/376377/* Driver specific properties */378struct rvt_driver_params dparms;379380/* post send table */381const struct rvt_operation_params *post_parms;382383/* opcode translation table */384const enum ib_wc_opcode *wc_opcode;385386/* Driver specific helper functions */387struct rvt_driver_provided driver_f;388389struct rvt_mregion __rcu *dma_mr;390struct rvt_lkey_table lkey_table;391392/* Internal use */393int n_pds_allocated;394spinlock_t n_pds_lock; /* Protect pd allocated count */395396int n_ahs_allocated;397spinlock_t n_ahs_lock; /* Protect ah allocated count */398399u32 n_srqs_allocated;400spinlock_t n_srqs_lock; /* Protect srqs allocated count */401402int flags;403struct rvt_ibport **ports;404405/* QP */406struct rvt_qp_ibdev *qp_dev;407u32 n_qps_allocated; /* number of QPs allocated for device */408u32 n_rc_qps; /* number of RC QPs allocated for device */409u32 busy_jiffies; /* timeout scaling based on RC QP count */410spinlock_t n_qps_lock; /* protect qps, rc qps and busy jiffy counts */411412/* memory maps */413struct list_head pending_mmaps;414spinlock_t mmap_offset_lock; /* protect mmap_offset */415u32 mmap_offset;416spinlock_t pending_lock; /* protect pending mmap list */417418/* CQ */419u32 n_cqs_allocated; /* number of CQs allocated for device */420spinlock_t n_cqs_lock; /* protect count of in use cqs */421422/* Multicast */423u32 n_mcast_grps_allocated; /* number of mcast groups allocated */424spinlock_t n_mcast_grps_lock;425426/* Memory Working Set Size */427struct rvt_wss *wss;428};429430/**431* rvt_set_ibdev_name - Craft an IB device name from client info432* @rdi: pointer to the client rvt_dev_info structure433* @name: client specific name434* @unit: client specific unit number.435*/436static inline void rvt_set_ibdev_name(struct rvt_dev_info *rdi,437const char *fmt, const char *name,438const int unit)439{440/*441* FIXME: rvt and its users want to touch the ibdev before442* registration and have things like the name work. We don't have the443* infrastructure in the core to support this directly today, hack it444* to work by setting the name manually here.445*/446dev_set_name(&rdi->ibdev.dev, fmt, name, unit);447strscpy(rdi->ibdev.name, dev_name(&rdi->ibdev.dev), IB_DEVICE_NAME_MAX);448}449450/**451* rvt_get_ibdev_name - return the IB name452* @rdi: rdmavt device453*454* Return the registered name of the device.455*/456static inline const char *rvt_get_ibdev_name(const struct rvt_dev_info *rdi)457{458return dev_name(&rdi->ibdev.dev);459}460461static inline struct rvt_pd *ibpd_to_rvtpd(struct ib_pd *ibpd)462{463return container_of(ibpd, struct rvt_pd, ibpd);464}465466static inline struct rvt_ah *ibah_to_rvtah(struct ib_ah *ibah)467{468return container_of(ibah, struct rvt_ah, ibah);469}470471static inline struct rvt_dev_info *ib_to_rvt(struct ib_device *ibdev)472{473return container_of(ibdev, struct rvt_dev_info, ibdev);474}475476static inline unsigned rvt_get_npkeys(struct rvt_dev_info *rdi)477{478/*479* All ports have same number of pkeys.480*/481return rdi->dparms.npkeys;482}483484/*485* Return the max atomic suitable for determining486* the size of the ack ring buffer in a QP.487*/488static inline unsigned int rvt_max_atomic(struct rvt_dev_info *rdi)489{490return rdi->dparms.max_rdma_atomic +491rdi->dparms.extra_rdma_atomic + 1;492}493494static inline unsigned int rvt_size_atomic(struct rvt_dev_info *rdi)495{496return rdi->dparms.max_rdma_atomic +497rdi->dparms.extra_rdma_atomic;498}499500/*501* Return the indexed PKEY from the port PKEY table.502*/503static inline u16 rvt_get_pkey(struct rvt_dev_info *rdi,504int port_index,505unsigned index)506{507if (index >= rvt_get_npkeys(rdi))508return 0;509else510return rdi->ports[port_index]->pkey_table[index];511}512513struct rvt_dev_info *rvt_alloc_device(size_t size, int nports);514void rvt_dealloc_device(struct rvt_dev_info *rdi);515int rvt_register_device(struct rvt_dev_info *rvd);516void rvt_unregister_device(struct rvt_dev_info *rvd);517int rvt_check_ah(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr);518int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,519int port_index, u16 *pkey_table);520int rvt_fast_reg_mr(struct rvt_qp *qp, struct ib_mr *ibmr, u32 key,521int access);522int rvt_invalidate_rkey(struct rvt_qp *qp, u32 rkey);523int rvt_rkey_ok(struct rvt_qp *qp, struct rvt_sge *sge,524u32 len, u64 vaddr, u32 rkey, int acc);525int rvt_lkey_ok(struct rvt_lkey_table *rkt, struct rvt_pd *pd,526struct rvt_sge *isge, struct rvt_sge *last_sge,527struct ib_sge *sge, int acc);528struct rvt_mcast *rvt_mcast_find(struct rvt_ibport *ibp, union ib_gid *mgid,529u16 lid);530531#endif /* DEF_RDMA_VT_H */532533534