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