162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _EFA_H_
762306a36Sopenharmony_ci#define _EFA_H_
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/bitops.h>
1062306a36Sopenharmony_ci#include <linux/interrupt.h>
1162306a36Sopenharmony_ci#include <linux/pci.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <rdma/efa-abi.h>
1462306a36Sopenharmony_ci#include <rdma/ib_verbs.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "efa_com_cmd.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define DRV_MODULE_NAME         "efa"
1962306a36Sopenharmony_ci#define DEVICE_NAME             "Elastic Fabric Adapter (EFA)"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define EFA_IRQNAME_SIZE        40
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define EFA_MGMNT_MSIX_VEC_IDX            0
2462306a36Sopenharmony_ci#define EFA_COMP_EQS_VEC_BASE             1
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistruct efa_irq {
2762306a36Sopenharmony_ci	irq_handler_t handler;
2862306a36Sopenharmony_ci	void *data;
2962306a36Sopenharmony_ci	u32 irqn;
3062306a36Sopenharmony_ci	u32 vector;
3162306a36Sopenharmony_ci	cpumask_t affinity_hint_mask;
3262306a36Sopenharmony_ci	char name[EFA_IRQNAME_SIZE];
3362306a36Sopenharmony_ci};
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* Don't use anything other than atomic64 */
3662306a36Sopenharmony_cistruct efa_stats {
3762306a36Sopenharmony_ci	atomic64_t alloc_pd_err;
3862306a36Sopenharmony_ci	atomic64_t create_qp_err;
3962306a36Sopenharmony_ci	atomic64_t create_cq_err;
4062306a36Sopenharmony_ci	atomic64_t reg_mr_err;
4162306a36Sopenharmony_ci	atomic64_t alloc_ucontext_err;
4262306a36Sopenharmony_ci	atomic64_t create_ah_err;
4362306a36Sopenharmony_ci	atomic64_t mmap_err;
4462306a36Sopenharmony_ci	atomic64_t keep_alive_rcvd;
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistruct efa_dev {
4862306a36Sopenharmony_ci	struct ib_device ibdev;
4962306a36Sopenharmony_ci	struct efa_com_dev edev;
5062306a36Sopenharmony_ci	struct pci_dev *pdev;
5162306a36Sopenharmony_ci	struct efa_com_get_device_attr_result dev_attr;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	u64 reg_bar_addr;
5462306a36Sopenharmony_ci	u64 reg_bar_len;
5562306a36Sopenharmony_ci	u64 mem_bar_addr;
5662306a36Sopenharmony_ci	u64 mem_bar_len;
5762306a36Sopenharmony_ci	u64 db_bar_addr;
5862306a36Sopenharmony_ci	u64 db_bar_len;
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	int admin_msix_vector_idx;
6162306a36Sopenharmony_ci	struct efa_irq admin_irq;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	struct efa_stats stats;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/* Array of completion EQs */
6662306a36Sopenharmony_ci	struct efa_eq *eqs;
6762306a36Sopenharmony_ci	unsigned int neqs;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	/* Only stores CQs with interrupts enabled */
7062306a36Sopenharmony_ci	struct xarray cqs_xa;
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct efa_ucontext {
7462306a36Sopenharmony_ci	struct ib_ucontext ibucontext;
7562306a36Sopenharmony_ci	u16 uarn;
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistruct efa_pd {
7962306a36Sopenharmony_ci	struct ib_pd ibpd;
8062306a36Sopenharmony_ci	u16 pdn;
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistruct efa_mr {
8462306a36Sopenharmony_ci	struct ib_mr ibmr;
8562306a36Sopenharmony_ci	struct ib_umem *umem;
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistruct efa_cq {
8962306a36Sopenharmony_ci	struct ib_cq ibcq;
9062306a36Sopenharmony_ci	struct efa_ucontext *ucontext;
9162306a36Sopenharmony_ci	dma_addr_t dma_addr;
9262306a36Sopenharmony_ci	void *cpu_addr;
9362306a36Sopenharmony_ci	struct rdma_user_mmap_entry *mmap_entry;
9462306a36Sopenharmony_ci	struct rdma_user_mmap_entry *db_mmap_entry;
9562306a36Sopenharmony_ci	size_t size;
9662306a36Sopenharmony_ci	u16 cq_idx;
9762306a36Sopenharmony_ci	/* NULL when no interrupts requested */
9862306a36Sopenharmony_ci	struct efa_eq *eq;
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistruct efa_qp {
10262306a36Sopenharmony_ci	struct ib_qp ibqp;
10362306a36Sopenharmony_ci	dma_addr_t rq_dma_addr;
10462306a36Sopenharmony_ci	void *rq_cpu_addr;
10562306a36Sopenharmony_ci	size_t rq_size;
10662306a36Sopenharmony_ci	enum ib_qp_state state;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	/* Used for saving mmap_xa entries */
10962306a36Sopenharmony_ci	struct rdma_user_mmap_entry *sq_db_mmap_entry;
11062306a36Sopenharmony_ci	struct rdma_user_mmap_entry *llq_desc_mmap_entry;
11162306a36Sopenharmony_ci	struct rdma_user_mmap_entry *rq_db_mmap_entry;
11262306a36Sopenharmony_ci	struct rdma_user_mmap_entry *rq_mmap_entry;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	u32 qp_handle;
11562306a36Sopenharmony_ci	u32 max_send_wr;
11662306a36Sopenharmony_ci	u32 max_recv_wr;
11762306a36Sopenharmony_ci	u32 max_send_sge;
11862306a36Sopenharmony_ci	u32 max_recv_sge;
11962306a36Sopenharmony_ci	u32 max_inline_data;
12062306a36Sopenharmony_ci};
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistruct efa_ah {
12362306a36Sopenharmony_ci	struct ib_ah ibah;
12462306a36Sopenharmony_ci	u16 ah;
12562306a36Sopenharmony_ci	/* dest_addr */
12662306a36Sopenharmony_ci	u8 id[EFA_GID_SIZE];
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistruct efa_eq {
13062306a36Sopenharmony_ci	struct efa_com_eq eeq;
13162306a36Sopenharmony_ci	struct efa_irq irq;
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciint efa_query_device(struct ib_device *ibdev,
13562306a36Sopenharmony_ci		     struct ib_device_attr *props,
13662306a36Sopenharmony_ci		     struct ib_udata *udata);
13762306a36Sopenharmony_ciint efa_query_port(struct ib_device *ibdev, u32 port,
13862306a36Sopenharmony_ci		   struct ib_port_attr *props);
13962306a36Sopenharmony_ciint efa_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
14062306a36Sopenharmony_ci		 int qp_attr_mask,
14162306a36Sopenharmony_ci		 struct ib_qp_init_attr *qp_init_attr);
14262306a36Sopenharmony_ciint efa_query_gid(struct ib_device *ibdev, u32 port, int index,
14362306a36Sopenharmony_ci		  union ib_gid *gid);
14462306a36Sopenharmony_ciint efa_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
14562306a36Sopenharmony_ci		   u16 *pkey);
14662306a36Sopenharmony_ciint efa_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata);
14762306a36Sopenharmony_ciint efa_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata);
14862306a36Sopenharmony_ciint efa_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata);
14962306a36Sopenharmony_ciint efa_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr,
15062306a36Sopenharmony_ci		  struct ib_udata *udata);
15162306a36Sopenharmony_ciint efa_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata);
15262306a36Sopenharmony_ciint efa_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
15362306a36Sopenharmony_ci		  struct ib_udata *udata);
15462306a36Sopenharmony_cistruct ib_mr *efa_reg_mr(struct ib_pd *ibpd, u64 start, u64 length,
15562306a36Sopenharmony_ci			 u64 virt_addr, int access_flags,
15662306a36Sopenharmony_ci			 struct ib_udata *udata);
15762306a36Sopenharmony_cistruct ib_mr *efa_reg_user_mr_dmabuf(struct ib_pd *ibpd, u64 start,
15862306a36Sopenharmony_ci				     u64 length, u64 virt_addr,
15962306a36Sopenharmony_ci				     int fd, int access_flags,
16062306a36Sopenharmony_ci				     struct ib_udata *udata);
16162306a36Sopenharmony_ciint efa_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata);
16262306a36Sopenharmony_ciint efa_get_port_immutable(struct ib_device *ibdev, u32 port_num,
16362306a36Sopenharmony_ci			   struct ib_port_immutable *immutable);
16462306a36Sopenharmony_ciint efa_alloc_ucontext(struct ib_ucontext *ibucontext, struct ib_udata *udata);
16562306a36Sopenharmony_civoid efa_dealloc_ucontext(struct ib_ucontext *ibucontext);
16662306a36Sopenharmony_ciint efa_mmap(struct ib_ucontext *ibucontext,
16762306a36Sopenharmony_ci	     struct vm_area_struct *vma);
16862306a36Sopenharmony_civoid efa_mmap_free(struct rdma_user_mmap_entry *rdma_entry);
16962306a36Sopenharmony_ciint efa_create_ah(struct ib_ah *ibah,
17062306a36Sopenharmony_ci		  struct rdma_ah_init_attr *init_attr,
17162306a36Sopenharmony_ci		  struct ib_udata *udata);
17262306a36Sopenharmony_ciint efa_destroy_ah(struct ib_ah *ibah, u32 flags);
17362306a36Sopenharmony_ciint efa_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
17462306a36Sopenharmony_ci		  int qp_attr_mask, struct ib_udata *udata);
17562306a36Sopenharmony_cienum rdma_link_layer efa_port_link_layer(struct ib_device *ibdev,
17662306a36Sopenharmony_ci					 u32 port_num);
17762306a36Sopenharmony_cistruct rdma_hw_stats *efa_alloc_hw_port_stats(struct ib_device *ibdev, u32 port_num);
17862306a36Sopenharmony_cistruct rdma_hw_stats *efa_alloc_hw_device_stats(struct ib_device *ibdev);
17962306a36Sopenharmony_ciint efa_get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats,
18062306a36Sopenharmony_ci		     u32 port_num, int index);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci#endif /* _EFA_H_ */
183