1db23ae64SCheng Xu /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */ 2db23ae64SCheng Xu 3db23ae64SCheng Xu /* Authors: Cheng Xu <chengyou@linux.alibaba.com> */ 4db23ae64SCheng Xu /* Kai Shen <kaishen@linux.alibaba.com> */ 5db23ae64SCheng Xu /* Copyright (c) 2020-2022, Alibaba Group. */ 6db23ae64SCheng Xu 7db23ae64SCheng Xu #ifndef __ERDMA_VERBS_H__ 8db23ae64SCheng Xu #define __ERDMA_VERBS_H__ 9db23ae64SCheng Xu 10db23ae64SCheng Xu #include "erdma.h" 11db23ae64SCheng Xu 12db23ae64SCheng Xu /* RDMA Capability. */ 13db23ae64SCheng Xu #define ERDMA_MAX_PD (128 * 1024) 14db23ae64SCheng Xu #define ERDMA_MAX_SEND_WR 4096 15db23ae64SCheng Xu #define ERDMA_MAX_ORD 128 16db23ae64SCheng Xu #define ERDMA_MAX_IRD 128 17db23ae64SCheng Xu #define ERDMA_MAX_SGE_RD 1 18db23ae64SCheng Xu #define ERDMA_MAX_CONTEXT (128 * 1024) 19db23ae64SCheng Xu #define ERDMA_MAX_SEND_SGE 6 20db23ae64SCheng Xu #define ERDMA_MAX_RECV_SGE 1 21db23ae64SCheng Xu #define ERDMA_MAX_INLINE (sizeof(struct erdma_sge) * (ERDMA_MAX_SEND_SGE)) 22db23ae64SCheng Xu #define ERDMA_MAX_FRMR_PA 512 23db23ae64SCheng Xu 24db23ae64SCheng Xu enum { 25db23ae64SCheng Xu ERDMA_MMAP_IO_NC = 0, /* no cache */ 26db23ae64SCheng Xu }; 27db23ae64SCheng Xu 28db23ae64SCheng Xu struct erdma_user_mmap_entry { 29db23ae64SCheng Xu struct rdma_user_mmap_entry rdma_entry; 30db23ae64SCheng Xu u64 address; 31db23ae64SCheng Xu u8 mmap_flag; 32db23ae64SCheng Xu }; 33db23ae64SCheng Xu 34db23ae64SCheng Xu struct erdma_ucontext { 35db23ae64SCheng Xu struct ib_ucontext ibucontext; 36db23ae64SCheng Xu 37db23ae64SCheng Xu u32 sdb_type; 38db23ae64SCheng Xu u32 sdb_idx; 39db23ae64SCheng Xu u32 sdb_page_idx; 40db23ae64SCheng Xu u32 sdb_page_off; 41db23ae64SCheng Xu u64 sdb; 42db23ae64SCheng Xu u64 rdb; 43db23ae64SCheng Xu u64 cdb; 44db23ae64SCheng Xu 45db23ae64SCheng Xu struct rdma_user_mmap_entry *sq_db_mmap_entry; 46db23ae64SCheng Xu struct rdma_user_mmap_entry *rq_db_mmap_entry; 47db23ae64SCheng Xu struct rdma_user_mmap_entry *cq_db_mmap_entry; 48db23ae64SCheng Xu 49db23ae64SCheng Xu /* doorbell records */ 50db23ae64SCheng Xu struct list_head dbrecords_page_list; 51db23ae64SCheng Xu struct mutex dbrecords_page_mutex; 52db23ae64SCheng Xu }; 53db23ae64SCheng Xu 54db23ae64SCheng Xu struct erdma_pd { 55db23ae64SCheng Xu struct ib_pd ibpd; 56db23ae64SCheng Xu u32 pdn; 57db23ae64SCheng Xu }; 58db23ae64SCheng Xu 59db23ae64SCheng Xu /* 60db23ae64SCheng Xu * MemoryRegion definition. 61db23ae64SCheng Xu */ 62db23ae64SCheng Xu #define ERDMA_MAX_INLINE_MTT_ENTRIES 4 63db23ae64SCheng Xu #define MTT_SIZE(mtt_cnt) (mtt_cnt << 3) /* per mtt takes 8 Bytes. */ 64db23ae64SCheng Xu #define ERDMA_MR_MAX_MTT_CNT 524288 65db23ae64SCheng Xu #define ERDMA_MTT_ENTRY_SIZE 8 66db23ae64SCheng Xu 67db23ae64SCheng Xu #define ERDMA_MR_TYPE_NORMAL 0 68db23ae64SCheng Xu #define ERDMA_MR_TYPE_FRMR 1 69db23ae64SCheng Xu #define ERDMA_MR_TYPE_DMA 2 70db23ae64SCheng Xu 71db23ae64SCheng Xu #define ERDMA_MR_INLINE_MTT 0 72db23ae64SCheng Xu #define ERDMA_MR_INDIRECT_MTT 1 73db23ae64SCheng Xu 74ece43fadSCheng Xu #define ERDMA_MR_ACC_RA BIT(0) 75ece43fadSCheng Xu #define ERDMA_MR_ACC_LR BIT(1) 76ece43fadSCheng Xu #define ERDMA_MR_ACC_LW BIT(2) 77ece43fadSCheng Xu #define ERDMA_MR_ACC_RR BIT(3) 78ece43fadSCheng Xu #define ERDMA_MR_ACC_RW BIT(4) 79db23ae64SCheng Xu 80db23ae64SCheng Xu static inline u8 to_erdma_access_flags(int access) 81db23ae64SCheng Xu { 82db23ae64SCheng Xu return (access & IB_ACCESS_REMOTE_READ ? ERDMA_MR_ACC_RR : 0) | 83db23ae64SCheng Xu (access & IB_ACCESS_LOCAL_WRITE ? ERDMA_MR_ACC_LW : 0) | 84ece43fadSCheng Xu (access & IB_ACCESS_REMOTE_WRITE ? ERDMA_MR_ACC_RW : 0) | 85ece43fadSCheng Xu (access & IB_ACCESS_REMOTE_ATOMIC ? ERDMA_MR_ACC_RA : 0); 86db23ae64SCheng Xu } 87db23ae64SCheng Xu 88db23ae64SCheng Xu struct erdma_mem { 89db23ae64SCheng Xu struct ib_umem *umem; 90db23ae64SCheng Xu void *mtt_buf; 91db23ae64SCheng Xu u32 mtt_type; 92db23ae64SCheng Xu u32 page_size; 93db23ae64SCheng Xu u32 page_offset; 94db23ae64SCheng Xu u32 page_cnt; 95db23ae64SCheng Xu u32 mtt_nents; 96db23ae64SCheng Xu 97db23ae64SCheng Xu u64 va; 98db23ae64SCheng Xu u64 len; 99db23ae64SCheng Xu 100db23ae64SCheng Xu u64 mtt_entry[ERDMA_MAX_INLINE_MTT_ENTRIES]; 101db23ae64SCheng Xu }; 102db23ae64SCheng Xu 103db23ae64SCheng Xu struct erdma_mr { 104db23ae64SCheng Xu struct ib_mr ibmr; 105db23ae64SCheng Xu struct erdma_mem mem; 106db23ae64SCheng Xu u8 type; 107db23ae64SCheng Xu u8 access; 108db23ae64SCheng Xu u8 valid; 109db23ae64SCheng Xu }; 110db23ae64SCheng Xu 111db23ae64SCheng Xu struct erdma_user_dbrecords_page { 112db23ae64SCheng Xu struct list_head list; 113db23ae64SCheng Xu struct ib_umem *umem; 114db23ae64SCheng Xu u64 va; 115db23ae64SCheng Xu int refcnt; 116db23ae64SCheng Xu }; 117db23ae64SCheng Xu 118db23ae64SCheng Xu struct erdma_uqp { 119db23ae64SCheng Xu struct erdma_mem sq_mtt; 120db23ae64SCheng Xu struct erdma_mem rq_mtt; 121db23ae64SCheng Xu 122db23ae64SCheng Xu dma_addr_t sq_db_info_dma_addr; 123db23ae64SCheng Xu dma_addr_t rq_db_info_dma_addr; 124db23ae64SCheng Xu 125db23ae64SCheng Xu struct erdma_user_dbrecords_page *user_dbr_page; 126db23ae64SCheng Xu 127db23ae64SCheng Xu u32 rq_offset; 128db23ae64SCheng Xu }; 129db23ae64SCheng Xu 130db23ae64SCheng Xu struct erdma_kqp { 131db23ae64SCheng Xu u16 sq_pi; 132db23ae64SCheng Xu u16 sq_ci; 133db23ae64SCheng Xu 134db23ae64SCheng Xu u16 rq_pi; 135db23ae64SCheng Xu u16 rq_ci; 136db23ae64SCheng Xu 137db23ae64SCheng Xu u64 *swr_tbl; 138db23ae64SCheng Xu u64 *rwr_tbl; 139db23ae64SCheng Xu 140db23ae64SCheng Xu void __iomem *hw_sq_db; 141db23ae64SCheng Xu void __iomem *hw_rq_db; 142db23ae64SCheng Xu 143db23ae64SCheng Xu void *sq_buf; 144db23ae64SCheng Xu dma_addr_t sq_buf_dma_addr; 145db23ae64SCheng Xu 146db23ae64SCheng Xu void *rq_buf; 147db23ae64SCheng Xu dma_addr_t rq_buf_dma_addr; 148db23ae64SCheng Xu 149db23ae64SCheng Xu void *sq_db_info; 150db23ae64SCheng Xu void *rq_db_info; 151db23ae64SCheng Xu 152db23ae64SCheng Xu u8 sig_all; 153db23ae64SCheng Xu }; 154db23ae64SCheng Xu 155db23ae64SCheng Xu enum erdma_qp_state { 156db23ae64SCheng Xu ERDMA_QP_STATE_IDLE = 0, 157db23ae64SCheng Xu ERDMA_QP_STATE_RTR = 1, 158db23ae64SCheng Xu ERDMA_QP_STATE_RTS = 2, 159db23ae64SCheng Xu ERDMA_QP_STATE_CLOSING = 3, 160db23ae64SCheng Xu ERDMA_QP_STATE_TERMINATE = 4, 161db23ae64SCheng Xu ERDMA_QP_STATE_ERROR = 5, 162db23ae64SCheng Xu ERDMA_QP_STATE_UNDEF = 7, 163db23ae64SCheng Xu ERDMA_QP_STATE_COUNT = 8 164db23ae64SCheng Xu }; 165db23ae64SCheng Xu 166db23ae64SCheng Xu enum erdma_qp_attr_mask { 167db23ae64SCheng Xu ERDMA_QP_ATTR_STATE = (1 << 0), 168db23ae64SCheng Xu ERDMA_QP_ATTR_LLP_HANDLE = (1 << 2), 169db23ae64SCheng Xu ERDMA_QP_ATTR_ORD = (1 << 3), 170db23ae64SCheng Xu ERDMA_QP_ATTR_IRD = (1 << 4), 171db23ae64SCheng Xu ERDMA_QP_ATTR_SQ_SIZE = (1 << 5), 172db23ae64SCheng Xu ERDMA_QP_ATTR_RQ_SIZE = (1 << 6), 173db23ae64SCheng Xu ERDMA_QP_ATTR_MPA = (1 << 7) 174db23ae64SCheng Xu }; 175db23ae64SCheng Xu 176db23ae64SCheng Xu struct erdma_qp_attrs { 177db23ae64SCheng Xu enum erdma_qp_state state; 178db23ae64SCheng Xu enum erdma_cc_alg cc; /* Congestion control algorithm */ 179db23ae64SCheng Xu u32 sq_size; 180db23ae64SCheng Xu u32 rq_size; 181db23ae64SCheng Xu u32 orq_size; 182db23ae64SCheng Xu u32 irq_size; 183db23ae64SCheng Xu u32 max_send_sge; 184db23ae64SCheng Xu u32 max_recv_sge; 185db23ae64SCheng Xu u32 cookie; 186db23ae64SCheng Xu #define ERDMA_QP_ACTIVE 0 187db23ae64SCheng Xu #define ERDMA_QP_PASSIVE 1 188db23ae64SCheng Xu u8 qp_type; 189db23ae64SCheng Xu u8 pd_len; 190db23ae64SCheng Xu }; 191db23ae64SCheng Xu 192db23ae64SCheng Xu struct erdma_qp { 193db23ae64SCheng Xu struct ib_qp ibqp; 194db23ae64SCheng Xu struct kref ref; 195db23ae64SCheng Xu struct completion safe_free; 196db23ae64SCheng Xu struct erdma_dev *dev; 197db23ae64SCheng Xu struct erdma_cep *cep; 198db23ae64SCheng Xu struct rw_semaphore state_lock; 199db23ae64SCheng Xu 200*54d8fffcSCheng Xu struct delayed_work reflush_dwork; 201*54d8fffcSCheng Xu 202db23ae64SCheng Xu union { 203db23ae64SCheng Xu struct erdma_kqp kern_qp; 204db23ae64SCheng Xu struct erdma_uqp user_qp; 205db23ae64SCheng Xu }; 206db23ae64SCheng Xu 207db23ae64SCheng Xu struct erdma_cq *scq; 208db23ae64SCheng Xu struct erdma_cq *rcq; 209db23ae64SCheng Xu 210db23ae64SCheng Xu struct erdma_qp_attrs attrs; 211db23ae64SCheng Xu spinlock_t lock; 212db23ae64SCheng Xu }; 213db23ae64SCheng Xu 214db23ae64SCheng Xu struct erdma_kcq_info { 215db23ae64SCheng Xu void *qbuf; 216db23ae64SCheng Xu dma_addr_t qbuf_dma_addr; 217db23ae64SCheng Xu u32 ci; 218db23ae64SCheng Xu u32 cmdsn; 219db23ae64SCheng Xu u32 notify_cnt; 220db23ae64SCheng Xu 221db23ae64SCheng Xu spinlock_t lock; 222db23ae64SCheng Xu u8 __iomem *db; 223db23ae64SCheng Xu u64 *db_record; 224db23ae64SCheng Xu }; 225db23ae64SCheng Xu 226db23ae64SCheng Xu struct erdma_ucq_info { 227db23ae64SCheng Xu struct erdma_mem qbuf_mtt; 228db23ae64SCheng Xu struct erdma_user_dbrecords_page *user_dbr_page; 229db23ae64SCheng Xu dma_addr_t db_info_dma_addr; 230db23ae64SCheng Xu }; 231db23ae64SCheng Xu 232db23ae64SCheng Xu struct erdma_cq { 233db23ae64SCheng Xu struct ib_cq ibcq; 234db23ae64SCheng Xu u32 cqn; 235db23ae64SCheng Xu 236db23ae64SCheng Xu u32 depth; 237db23ae64SCheng Xu u32 assoc_eqn; 238db23ae64SCheng Xu 239db23ae64SCheng Xu union { 240db23ae64SCheng Xu struct erdma_kcq_info kern_cq; 241db23ae64SCheng Xu struct erdma_ucq_info user_cq; 242db23ae64SCheng Xu }; 243db23ae64SCheng Xu }; 244db23ae64SCheng Xu 245db23ae64SCheng Xu #define QP_ID(qp) ((qp)->ibqp.qp_num) 246db23ae64SCheng Xu 247db23ae64SCheng Xu static inline struct erdma_qp *find_qp_by_qpn(struct erdma_dev *dev, int id) 248db23ae64SCheng Xu { 249db23ae64SCheng Xu return (struct erdma_qp *)xa_load(&dev->qp_xa, id); 250db23ae64SCheng Xu } 251db23ae64SCheng Xu 252db23ae64SCheng Xu static inline struct erdma_cq *find_cq_by_cqn(struct erdma_dev *dev, int id) 253db23ae64SCheng Xu { 254db23ae64SCheng Xu return (struct erdma_cq *)xa_load(&dev->cq_xa, id); 255db23ae64SCheng Xu } 256db23ae64SCheng Xu 257db23ae64SCheng Xu void erdma_qp_get(struct erdma_qp *qp); 258db23ae64SCheng Xu void erdma_qp_put(struct erdma_qp *qp); 259db23ae64SCheng Xu int erdma_modify_qp_internal(struct erdma_qp *qp, struct erdma_qp_attrs *attrs, 260db23ae64SCheng Xu enum erdma_qp_attr_mask mask); 261db23ae64SCheng Xu void erdma_qp_llp_close(struct erdma_qp *qp); 262db23ae64SCheng Xu void erdma_qp_cm_drop(struct erdma_qp *qp); 263db23ae64SCheng Xu 264db23ae64SCheng Xu static inline struct erdma_ucontext *to_ectx(struct ib_ucontext *ibctx) 265db23ae64SCheng Xu { 266db23ae64SCheng Xu return container_of(ibctx, struct erdma_ucontext, ibucontext); 267db23ae64SCheng Xu } 268db23ae64SCheng Xu 269db23ae64SCheng Xu static inline struct erdma_pd *to_epd(struct ib_pd *pd) 270db23ae64SCheng Xu { 271db23ae64SCheng Xu return container_of(pd, struct erdma_pd, ibpd); 272db23ae64SCheng Xu } 273db23ae64SCheng Xu 274db23ae64SCheng Xu static inline struct erdma_mr *to_emr(struct ib_mr *ibmr) 275db23ae64SCheng Xu { 276db23ae64SCheng Xu return container_of(ibmr, struct erdma_mr, ibmr); 277db23ae64SCheng Xu } 278db23ae64SCheng Xu 279db23ae64SCheng Xu static inline struct erdma_qp *to_eqp(struct ib_qp *qp) 280db23ae64SCheng Xu { 281db23ae64SCheng Xu return container_of(qp, struct erdma_qp, ibqp); 282db23ae64SCheng Xu } 283db23ae64SCheng Xu 284db23ae64SCheng Xu static inline struct erdma_cq *to_ecq(struct ib_cq *ibcq) 285db23ae64SCheng Xu { 286db23ae64SCheng Xu return container_of(ibcq, struct erdma_cq, ibcq); 287db23ae64SCheng Xu } 288db23ae64SCheng Xu 289db23ae64SCheng Xu static inline struct erdma_user_mmap_entry * 290db23ae64SCheng Xu to_emmap(struct rdma_user_mmap_entry *ibmmap) 291db23ae64SCheng Xu { 292db23ae64SCheng Xu return container_of(ibmmap, struct erdma_user_mmap_entry, rdma_entry); 293db23ae64SCheng Xu } 294db23ae64SCheng Xu 295db23ae64SCheng Xu int erdma_alloc_ucontext(struct ib_ucontext *ibctx, struct ib_udata *data); 296db23ae64SCheng Xu void erdma_dealloc_ucontext(struct ib_ucontext *ibctx); 297db23ae64SCheng Xu int erdma_query_device(struct ib_device *dev, struct ib_device_attr *attr, 298db23ae64SCheng Xu struct ib_udata *data); 299db23ae64SCheng Xu int erdma_get_port_immutable(struct ib_device *dev, u32 port, 300db23ae64SCheng Xu struct ib_port_immutable *ib_port_immutable); 301db23ae64SCheng Xu int erdma_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 302db23ae64SCheng Xu struct ib_udata *data); 303db23ae64SCheng Xu int erdma_query_port(struct ib_device *dev, u32 port, 304db23ae64SCheng Xu struct ib_port_attr *attr); 305db23ae64SCheng Xu int erdma_query_gid(struct ib_device *dev, u32 port, int idx, 306db23ae64SCheng Xu union ib_gid *gid); 307db23ae64SCheng Xu int erdma_alloc_pd(struct ib_pd *ibpd, struct ib_udata *data); 308db23ae64SCheng Xu int erdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata); 309db23ae64SCheng Xu int erdma_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attr, 310db23ae64SCheng Xu struct ib_udata *data); 311db23ae64SCheng Xu int erdma_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int mask, 312db23ae64SCheng Xu struct ib_qp_init_attr *init_attr); 313db23ae64SCheng Xu int erdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int mask, 314db23ae64SCheng Xu struct ib_udata *data); 315db23ae64SCheng Xu int erdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata); 316db23ae64SCheng Xu int erdma_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata); 317db23ae64SCheng Xu int erdma_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags); 318db23ae64SCheng Xu struct ib_mr *erdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len, 319db23ae64SCheng Xu u64 virt, int access, struct ib_udata *udata); 320db23ae64SCheng Xu struct ib_mr *erdma_get_dma_mr(struct ib_pd *ibpd, int rights); 321db23ae64SCheng Xu int erdma_dereg_mr(struct ib_mr *ibmr, struct ib_udata *data); 322db23ae64SCheng Xu int erdma_mmap(struct ib_ucontext *ctx, struct vm_area_struct *vma); 323db23ae64SCheng Xu void erdma_mmap_free(struct rdma_user_mmap_entry *rdma_entry); 324db23ae64SCheng Xu void erdma_qp_get_ref(struct ib_qp *ibqp); 325db23ae64SCheng Xu void erdma_qp_put_ref(struct ib_qp *ibqp); 326db23ae64SCheng Xu struct ib_qp *erdma_get_ibqp(struct ib_device *dev, int id); 327db23ae64SCheng Xu int erdma_post_send(struct ib_qp *ibqp, const struct ib_send_wr *send_wr, 328db23ae64SCheng Xu const struct ib_send_wr **bad_send_wr); 329db23ae64SCheng Xu int erdma_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *recv_wr, 330db23ae64SCheng Xu const struct ib_recv_wr **bad_recv_wr); 331db23ae64SCheng Xu int erdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc); 332db23ae64SCheng Xu struct ib_mr *erdma_ib_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, 333db23ae64SCheng Xu u32 max_num_sg); 334db23ae64SCheng Xu int erdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents, 335db23ae64SCheng Xu unsigned int *sg_offset); 336db23ae64SCheng Xu void erdma_port_event(struct erdma_dev *dev, enum ib_event_type reason); 3379bdb9350SCheng Xu void erdma_set_mtu(struct erdma_dev *dev, u32 mtu); 338db23ae64SCheng Xu 339db23ae64SCheng Xu #endif 340