18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2020 Intel Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef DEF_RDMAVT_INCQP_H
78c2ecf20Sopenharmony_ci#define DEF_RDMAVT_INCQP_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <rdma/rdma_vt.h>
108c2ecf20Sopenharmony_ci#include <rdma/ib_pack.h>
118c2ecf20Sopenharmony_ci#include <rdma/ib_verbs.h>
128c2ecf20Sopenharmony_ci#include <rdma/rdmavt_cq.h>
138c2ecf20Sopenharmony_ci#include <rdma/rvt-abi.h>
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Atomic bit definitions for r_aflags.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci#define RVT_R_WRID_VALID        0
188c2ecf20Sopenharmony_ci#define RVT_R_REWIND_SGE        1
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Bit definitions for r_flags.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define RVT_R_REUSE_SGE 0x01
248c2ecf20Sopenharmony_ci#define RVT_R_RDMAR_SEQ 0x02
258c2ecf20Sopenharmony_ci#define RVT_R_RSP_NAK   0x04
268c2ecf20Sopenharmony_ci#define RVT_R_RSP_SEND  0x08
278c2ecf20Sopenharmony_ci#define RVT_R_COMM_EST  0x10
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * If a packet's QP[23:16] bits match this value, then it is
318c2ecf20Sopenharmony_ci * a PSM packet and the hardware will expect a KDETH header
328c2ecf20Sopenharmony_ci * following the BTH.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define RVT_KDETH_QP_PREFIX       0x80
358c2ecf20Sopenharmony_ci#define RVT_KDETH_QP_SUFFIX       0xffff
368c2ecf20Sopenharmony_ci#define RVT_KDETH_QP_PREFIX_MASK  0x00ff0000
378c2ecf20Sopenharmony_ci#define RVT_KDETH_QP_PREFIX_SHIFT 16
388c2ecf20Sopenharmony_ci#define RVT_KDETH_QP_BASE         (u32)(RVT_KDETH_QP_PREFIX << \
398c2ecf20Sopenharmony_ci					RVT_KDETH_QP_PREFIX_SHIFT)
408c2ecf20Sopenharmony_ci#define RVT_KDETH_QP_MAX          (u32)(RVT_KDETH_QP_BASE + RVT_KDETH_QP_SUFFIX)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/*
438c2ecf20Sopenharmony_ci * If a packet's LNH == BTH and DEST QPN[23:16] in the BTH match this
448c2ecf20Sopenharmony_ci * prefix value, then it is an AIP packet with a DETH containing the entropy
458c2ecf20Sopenharmony_ci * value in byte 4 following the BTH.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci#define RVT_AIP_QP_PREFIX       0x81
488c2ecf20Sopenharmony_ci#define RVT_AIP_QP_SUFFIX       0xffff
498c2ecf20Sopenharmony_ci#define RVT_AIP_QP_PREFIX_MASK  0x00ff0000
508c2ecf20Sopenharmony_ci#define RVT_AIP_QP_PREFIX_SHIFT 16
518c2ecf20Sopenharmony_ci#define RVT_AIP_QP_BASE         (u32)(RVT_AIP_QP_PREFIX << \
528c2ecf20Sopenharmony_ci				      RVT_AIP_QP_PREFIX_SHIFT)
538c2ecf20Sopenharmony_ci#define RVT_AIP_QPN_MAX         BIT(RVT_AIP_QP_PREFIX_SHIFT)
548c2ecf20Sopenharmony_ci#define RVT_AIP_QP_MAX          (u32)(RVT_AIP_QP_BASE + RVT_AIP_QPN_MAX - 1)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * Bit definitions for s_flags.
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled
608c2ecf20Sopenharmony_ci * RVT_S_BUSY - send tasklet is processing the QP
618c2ecf20Sopenharmony_ci * RVT_S_TIMER - the RC retry timer is active
628c2ecf20Sopenharmony_ci * RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics
638c2ecf20Sopenharmony_ci * RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs
648c2ecf20Sopenharmony_ci *                         before processing the next SWQE
658c2ecf20Sopenharmony_ci * RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete
668c2ecf20Sopenharmony_ci *                         before processing the next SWQE
678c2ecf20Sopenharmony_ci * RVT_S_WAIT_RNR - waiting for RNR timeout
688c2ecf20Sopenharmony_ci * RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE
698c2ecf20Sopenharmony_ci * RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating
708c2ecf20Sopenharmony_ci *                  next send completion entry not via send DMA
718c2ecf20Sopenharmony_ci * RVT_S_WAIT_PIO - waiting for a send buffer to be available
728c2ecf20Sopenharmony_ci * RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available
738c2ecf20Sopenharmony_ci * RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available
748c2ecf20Sopenharmony_ci * RVT_S_WAIT_KMEM - waiting for kernel memory to be available
758c2ecf20Sopenharmony_ci * RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue
768c2ecf20Sopenharmony_ci * RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests
778c2ecf20Sopenharmony_ci * RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK
788c2ecf20Sopenharmony_ci * RVT_S_ECN - a BECN was queued to the send engine
798c2ecf20Sopenharmony_ci * RVT_S_MAX_BIT_MASK - The max bit that can be used by rdmavt
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ci#define RVT_S_SIGNAL_REQ_WR	0x0001
828c2ecf20Sopenharmony_ci#define RVT_S_BUSY		0x0002
838c2ecf20Sopenharmony_ci#define RVT_S_TIMER		0x0004
848c2ecf20Sopenharmony_ci#define RVT_S_RESP_PENDING	0x0008
858c2ecf20Sopenharmony_ci#define RVT_S_ACK_PENDING	0x0010
868c2ecf20Sopenharmony_ci#define RVT_S_WAIT_FENCE	0x0020
878c2ecf20Sopenharmony_ci#define RVT_S_WAIT_RDMAR	0x0040
888c2ecf20Sopenharmony_ci#define RVT_S_WAIT_RNR		0x0080
898c2ecf20Sopenharmony_ci#define RVT_S_WAIT_SSN_CREDIT	0x0100
908c2ecf20Sopenharmony_ci#define RVT_S_WAIT_DMA		0x0200
918c2ecf20Sopenharmony_ci#define RVT_S_WAIT_PIO		0x0400
928c2ecf20Sopenharmony_ci#define RVT_S_WAIT_TX		0x0800
938c2ecf20Sopenharmony_ci#define RVT_S_WAIT_DMA_DESC	0x1000
948c2ecf20Sopenharmony_ci#define RVT_S_WAIT_KMEM		0x2000
958c2ecf20Sopenharmony_ci#define RVT_S_WAIT_PSN		0x4000
968c2ecf20Sopenharmony_ci#define RVT_S_WAIT_ACK		0x8000
978c2ecf20Sopenharmony_ci#define RVT_S_SEND_ONE		0x10000
988c2ecf20Sopenharmony_ci#define RVT_S_UNLIMITED_CREDIT	0x20000
998c2ecf20Sopenharmony_ci#define RVT_S_ECN		0x40000
1008c2ecf20Sopenharmony_ci#define RVT_S_MAX_BIT_MASK	0x800000
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/*
1038c2ecf20Sopenharmony_ci * Drivers should use s_flags starting with bit 31 down to the bit next to
1048c2ecf20Sopenharmony_ci * RVT_S_MAX_BIT_MASK
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/*
1088c2ecf20Sopenharmony_ci * Wait flags that would prevent any packet type from being sent.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_ci#define RVT_S_ANY_WAIT_IO \
1118c2ecf20Sopenharmony_ci	(RVT_S_WAIT_PIO | RVT_S_WAIT_TX | \
1128c2ecf20Sopenharmony_ci	 RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/*
1158c2ecf20Sopenharmony_ci * Wait flags that would prevent send work requests from making progress.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ci#define RVT_S_ANY_WAIT_SEND (RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR | \
1188c2ecf20Sopenharmony_ci	RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \
1198c2ecf20Sopenharmony_ci	RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Number of bits to pay attention to in the opcode for checking qp type */
1248c2ecf20Sopenharmony_ci#define RVT_OPCODE_QP_MASK 0xE0
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/* Flags for checking QP state (see ib_rvt_state_ops[]) */
1278c2ecf20Sopenharmony_ci#define RVT_POST_SEND_OK                0x01
1288c2ecf20Sopenharmony_ci#define RVT_POST_RECV_OK                0x02
1298c2ecf20Sopenharmony_ci#define RVT_PROCESS_RECV_OK             0x04
1308c2ecf20Sopenharmony_ci#define RVT_PROCESS_SEND_OK             0x08
1318c2ecf20Sopenharmony_ci#define RVT_PROCESS_NEXT_SEND_OK        0x10
1328c2ecf20Sopenharmony_ci#define RVT_FLUSH_SEND			0x20
1338c2ecf20Sopenharmony_ci#define RVT_FLUSH_RECV			0x40
1348c2ecf20Sopenharmony_ci#define RVT_PROCESS_OR_FLUSH_SEND \
1358c2ecf20Sopenharmony_ci	(RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)
1368c2ecf20Sopenharmony_ci#define RVT_SEND_OR_FLUSH_OR_RECV_OK \
1378c2ecf20Sopenharmony_ci	(RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND | RVT_PROCESS_RECV_OK)
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/*
1408c2ecf20Sopenharmony_ci * Internal send flags
1418c2ecf20Sopenharmony_ci */
1428c2ecf20Sopenharmony_ci#define RVT_SEND_RESERVE_USED           IB_SEND_RESERVED_START
1438c2ecf20Sopenharmony_ci#define RVT_SEND_COMPLETION_ONLY	(IB_SEND_RESERVED_START << 1)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/**
1468c2ecf20Sopenharmony_ci * rvt_ud_wr - IB UD work plus AH cache
1478c2ecf20Sopenharmony_ci * @wr: valid IB work request
1488c2ecf20Sopenharmony_ci * @attr: pointer to an allocated AH attribute
1498c2ecf20Sopenharmony_ci *
1508c2ecf20Sopenharmony_ci * Special case the UD WR so we can keep track of the AH attributes.
1518c2ecf20Sopenharmony_ci *
1528c2ecf20Sopenharmony_ci * NOTE: This data structure is stricly ordered wr then attr. I.e the attr
1538c2ecf20Sopenharmony_ci * MUST come after wr.  The ib_ud_wr is sized and copied in rvt_post_one_wr.
1548c2ecf20Sopenharmony_ci * The copy assumes that wr is first.
1558c2ecf20Sopenharmony_ci */
1568c2ecf20Sopenharmony_cistruct rvt_ud_wr {
1578c2ecf20Sopenharmony_ci	struct ib_ud_wr wr;
1588c2ecf20Sopenharmony_ci	struct rdma_ah_attr *attr;
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/*
1628c2ecf20Sopenharmony_ci * Send work request queue entry.
1638c2ecf20Sopenharmony_ci * The size of the sg_list is determined when the QP is created and stored
1648c2ecf20Sopenharmony_ci * in qp->s_max_sge.
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_cistruct rvt_swqe {
1678c2ecf20Sopenharmony_ci	union {
1688c2ecf20Sopenharmony_ci		struct ib_send_wr wr;   /* don't use wr.sg_list */
1698c2ecf20Sopenharmony_ci		struct rvt_ud_wr ud_wr;
1708c2ecf20Sopenharmony_ci		struct ib_reg_wr reg_wr;
1718c2ecf20Sopenharmony_ci		struct ib_rdma_wr rdma_wr;
1728c2ecf20Sopenharmony_ci		struct ib_atomic_wr atomic_wr;
1738c2ecf20Sopenharmony_ci	};
1748c2ecf20Sopenharmony_ci	u32 psn;                /* first packet sequence number */
1758c2ecf20Sopenharmony_ci	u32 lpsn;               /* last packet sequence number */
1768c2ecf20Sopenharmony_ci	u32 ssn;                /* send sequence number */
1778c2ecf20Sopenharmony_ci	u32 length;             /* total length of data in sg_list */
1788c2ecf20Sopenharmony_ci	void *priv;             /* driver dependent field */
1798c2ecf20Sopenharmony_ci	struct rvt_sge sg_list[];
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci * struct rvt_krwq - kernel struct receive work request
1848c2ecf20Sopenharmony_ci * @p_lock: lock to protect producer of the kernel buffer
1858c2ecf20Sopenharmony_ci * @head: index of next entry to fill
1868c2ecf20Sopenharmony_ci * @c_lock:lock to protect consumer of the kernel buffer
1878c2ecf20Sopenharmony_ci * @tail: index of next entry to pull
1888c2ecf20Sopenharmony_ci * @count: count is aproximate of total receive enteries posted
1898c2ecf20Sopenharmony_ci * @rvt_rwqe: struct of receive work request queue entry
1908c2ecf20Sopenharmony_ci *
1918c2ecf20Sopenharmony_ci * This structure is used to contain the head pointer,
1928c2ecf20Sopenharmony_ci * tail pointer and receive work queue entries for kernel
1938c2ecf20Sopenharmony_ci * mode user.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_cistruct rvt_krwq {
1968c2ecf20Sopenharmony_ci	spinlock_t p_lock;	/* protect producer */
1978c2ecf20Sopenharmony_ci	u32 head;               /* new work requests posted to the head */
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* protect consumer */
2008c2ecf20Sopenharmony_ci	spinlock_t c_lock ____cacheline_aligned_in_smp;
2018c2ecf20Sopenharmony_ci	u32 tail;               /* receives pull requests from here. */
2028c2ecf20Sopenharmony_ci	u32 count;		/* approx count of receive entries posted */
2038c2ecf20Sopenharmony_ci	struct rvt_rwqe *curr_wq;
2048c2ecf20Sopenharmony_ci	struct rvt_rwqe wq[];
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/*
2088c2ecf20Sopenharmony_ci * rvt_get_swqe_ah - Return the pointer to the struct rvt_ah
2098c2ecf20Sopenharmony_ci * @swqe: valid Send WQE
2108c2ecf20Sopenharmony_ci *
2118c2ecf20Sopenharmony_ci */
2128c2ecf20Sopenharmony_cistatic inline struct rvt_ah *rvt_get_swqe_ah(struct rvt_swqe *swqe)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	return ibah_to_rvtah(swqe->ud_wr.wr.ah);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/**
2188c2ecf20Sopenharmony_ci * rvt_get_swqe_ah_attr - Return the cached ah attribute information
2198c2ecf20Sopenharmony_ci * @swqe: valid Send WQE
2208c2ecf20Sopenharmony_ci *
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_cistatic inline struct rdma_ah_attr *rvt_get_swqe_ah_attr(struct rvt_swqe *swqe)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	return swqe->ud_wr.attr;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/**
2288c2ecf20Sopenharmony_ci * rvt_get_swqe_remote_qpn - Access the remote QPN value
2298c2ecf20Sopenharmony_ci * @swqe: valid Send WQE
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci */
2328c2ecf20Sopenharmony_cistatic inline u32 rvt_get_swqe_remote_qpn(struct rvt_swqe *swqe)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	return swqe->ud_wr.wr.remote_qpn;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/**
2388c2ecf20Sopenharmony_ci * rvt_get_swqe_remote_qkey - Acces the remote qkey value
2398c2ecf20Sopenharmony_ci * @swqe: valid Send WQE
2408c2ecf20Sopenharmony_ci *
2418c2ecf20Sopenharmony_ci */
2428c2ecf20Sopenharmony_cistatic inline u32 rvt_get_swqe_remote_qkey(struct rvt_swqe *swqe)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	return swqe->ud_wr.wr.remote_qkey;
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/**
2488c2ecf20Sopenharmony_ci * rvt_get_swqe_pkey_index - Access the pkey index
2498c2ecf20Sopenharmony_ci * @swqe: valid Send WQE
2508c2ecf20Sopenharmony_ci *
2518c2ecf20Sopenharmony_ci */
2528c2ecf20Sopenharmony_cistatic inline u16 rvt_get_swqe_pkey_index(struct rvt_swqe *swqe)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	return swqe->ud_wr.wr.pkey_index;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistruct rvt_rq {
2588c2ecf20Sopenharmony_ci	struct rvt_rwq *wq;
2598c2ecf20Sopenharmony_ci	struct rvt_krwq *kwq;
2608c2ecf20Sopenharmony_ci	u32 size;               /* size of RWQE array */
2618c2ecf20Sopenharmony_ci	u8 max_sge;
2628c2ecf20Sopenharmony_ci	/* protect changes in this struct */
2638c2ecf20Sopenharmony_ci	spinlock_t lock ____cacheline_aligned_in_smp;
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci/**
2678c2ecf20Sopenharmony_ci * rvt_get_rq_count - count numbers of request work queue entries
2688c2ecf20Sopenharmony_ci * in circular buffer
2698c2ecf20Sopenharmony_ci * @rq: data structure for request queue entry
2708c2ecf20Sopenharmony_ci * @head: head indices of the circular buffer
2718c2ecf20Sopenharmony_ci * @tail: tail indices of the circular buffer
2728c2ecf20Sopenharmony_ci *
2738c2ecf20Sopenharmony_ci * Return - total number of entries in the Receive Queue
2748c2ecf20Sopenharmony_ci */
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic inline u32 rvt_get_rq_count(struct rvt_rq *rq, u32 head, u32 tail)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	u32 count = head - tail;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	if ((s32)count < 0)
2818c2ecf20Sopenharmony_ci		count += rq->size;
2828c2ecf20Sopenharmony_ci	return count;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci/*
2868c2ecf20Sopenharmony_ci * This structure holds the information that the send tasklet needs
2878c2ecf20Sopenharmony_ci * to send a RDMA read response or atomic operation.
2888c2ecf20Sopenharmony_ci */
2898c2ecf20Sopenharmony_cistruct rvt_ack_entry {
2908c2ecf20Sopenharmony_ci	struct rvt_sge rdma_sge;
2918c2ecf20Sopenharmony_ci	u64 atomic_data;
2928c2ecf20Sopenharmony_ci	u32 psn;
2938c2ecf20Sopenharmony_ci	u32 lpsn;
2948c2ecf20Sopenharmony_ci	u8 opcode;
2958c2ecf20Sopenharmony_ci	u8 sent;
2968c2ecf20Sopenharmony_ci	void *priv;
2978c2ecf20Sopenharmony_ci};
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci#define	RC_QP_SCALING_INTERVAL	5
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci#define RVT_OPERATION_PRIV        0x00000001
3028c2ecf20Sopenharmony_ci#define RVT_OPERATION_ATOMIC      0x00000002
3038c2ecf20Sopenharmony_ci#define RVT_OPERATION_ATOMIC_SGE  0x00000004
3048c2ecf20Sopenharmony_ci#define RVT_OPERATION_LOCAL       0x00000008
3058c2ecf20Sopenharmony_ci#define RVT_OPERATION_USE_RESERVE 0x00000010
3068c2ecf20Sopenharmony_ci#define RVT_OPERATION_IGN_RNR_CNT 0x00000020
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci#define RVT_OPERATION_MAX (IB_WR_RESERVED10 + 1)
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci/**
3118c2ecf20Sopenharmony_ci * rvt_operation_params - op table entry
3128c2ecf20Sopenharmony_ci * @length - the length to copy into the swqe entry
3138c2ecf20Sopenharmony_ci * @qpt_support - a bit mask indicating QP type support
3148c2ecf20Sopenharmony_ci * @flags - RVT_OPERATION flags (see above)
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * This supports table driven post send so that
3178c2ecf20Sopenharmony_ci * the driver can have differing an potentially
3188c2ecf20Sopenharmony_ci * different sets of operations.
3198c2ecf20Sopenharmony_ci *
3208c2ecf20Sopenharmony_ci **/
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistruct rvt_operation_params {
3238c2ecf20Sopenharmony_ci	size_t length;
3248c2ecf20Sopenharmony_ci	u32 qpt_support;
3258c2ecf20Sopenharmony_ci	u32 flags;
3268c2ecf20Sopenharmony_ci};
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci/*
3298c2ecf20Sopenharmony_ci * Common variables are protected by both r_rq.lock and s_lock in that order
3308c2ecf20Sopenharmony_ci * which only happens in modify_qp() or changing the QP 'state'.
3318c2ecf20Sopenharmony_ci */
3328c2ecf20Sopenharmony_cistruct rvt_qp {
3338c2ecf20Sopenharmony_ci	struct ib_qp ibqp;
3348c2ecf20Sopenharmony_ci	void *priv; /* Driver private data */
3358c2ecf20Sopenharmony_ci	/* read mostly fields above and below */
3368c2ecf20Sopenharmony_ci	struct rdma_ah_attr remote_ah_attr;
3378c2ecf20Sopenharmony_ci	struct rdma_ah_attr alt_ah_attr;
3388c2ecf20Sopenharmony_ci	struct rvt_qp __rcu *next;           /* link list for QPN hash table */
3398c2ecf20Sopenharmony_ci	struct rvt_swqe *s_wq;  /* send work queue */
3408c2ecf20Sopenharmony_ci	struct rvt_mmap_info *ip;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	unsigned long timeout_jiffies;  /* computed from timeout */
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	int srate_mbps;		/* s_srate (below) converted to Mbit/s */
3458c2ecf20Sopenharmony_ci	pid_t pid;		/* pid for user mode QPs */
3468c2ecf20Sopenharmony_ci	u32 remote_qpn;
3478c2ecf20Sopenharmony_ci	u32 qkey;               /* QKEY for this QP (for UD or RD) */
3488c2ecf20Sopenharmony_ci	u32 s_size;             /* send work queue size */
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	u16 pmtu;		/* decoded from path_mtu */
3518c2ecf20Sopenharmony_ci	u8 log_pmtu;		/* shift for pmtu */
3528c2ecf20Sopenharmony_ci	u8 state;               /* QP state */
3538c2ecf20Sopenharmony_ci	u8 allowed_ops;		/* high order bits of allowed opcodes */
3548c2ecf20Sopenharmony_ci	u8 qp_access_flags;
3558c2ecf20Sopenharmony_ci	u8 alt_timeout;         /* Alternate path timeout for this QP */
3568c2ecf20Sopenharmony_ci	u8 timeout;             /* Timeout for this QP */
3578c2ecf20Sopenharmony_ci	u8 s_srate;
3588c2ecf20Sopenharmony_ci	u8 s_mig_state;
3598c2ecf20Sopenharmony_ci	u8 port_num;
3608c2ecf20Sopenharmony_ci	u8 s_pkey_index;        /* PKEY index to use */
3618c2ecf20Sopenharmony_ci	u8 s_alt_pkey_index;    /* Alternate path PKEY index to use */
3628c2ecf20Sopenharmony_ci	u8 r_max_rd_atomic;     /* max number of RDMA read/atomic to receive */
3638c2ecf20Sopenharmony_ci	u8 s_max_rd_atomic;     /* max number of RDMA read/atomic to send */
3648c2ecf20Sopenharmony_ci	u8 s_retry_cnt;         /* number of times to retry */
3658c2ecf20Sopenharmony_ci	u8 s_rnr_retry_cnt;
3668c2ecf20Sopenharmony_ci	u8 r_min_rnr_timer;     /* retry timeout value for RNR NAKs */
3678c2ecf20Sopenharmony_ci	u8 s_max_sge;           /* size of s_wq->sg_list */
3688c2ecf20Sopenharmony_ci	u8 s_draining;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* start of read/write fields */
3718c2ecf20Sopenharmony_ci	atomic_t refcount ____cacheline_aligned_in_smp;
3728c2ecf20Sopenharmony_ci	wait_queue_head_t wait;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	struct rvt_ack_entry *s_ack_queue;
3758c2ecf20Sopenharmony_ci	struct rvt_sge_state s_rdma_read_sge;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	spinlock_t r_lock ____cacheline_aligned_in_smp;      /* used for APM */
3788c2ecf20Sopenharmony_ci	u32 r_psn;              /* expected rcv packet sequence number */
3798c2ecf20Sopenharmony_ci	unsigned long r_aflags;
3808c2ecf20Sopenharmony_ci	u64 r_wr_id;            /* ID for current receive WQE */
3818c2ecf20Sopenharmony_ci	u32 r_ack_psn;          /* PSN for next ACK or atomic ACK */
3828c2ecf20Sopenharmony_ci	u32 r_len;              /* total length of r_sge */
3838c2ecf20Sopenharmony_ci	u32 r_rcv_len;          /* receive data len processed */
3848c2ecf20Sopenharmony_ci	u32 r_msn;              /* message sequence number */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	u8 r_state;             /* opcode of last packet received */
3878c2ecf20Sopenharmony_ci	u8 r_flags;
3888c2ecf20Sopenharmony_ci	u8 r_head_ack_queue;    /* index into s_ack_queue[] */
3898c2ecf20Sopenharmony_ci	u8 r_adefered;          /* defered ack count */
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	struct list_head rspwait;       /* link for waiting to respond */
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	struct rvt_sge_state r_sge;     /* current receive data */
3948c2ecf20Sopenharmony_ci	struct rvt_rq r_rq;             /* receive work queue */
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* post send line */
3978c2ecf20Sopenharmony_ci	spinlock_t s_hlock ____cacheline_aligned_in_smp;
3988c2ecf20Sopenharmony_ci	u32 s_head;             /* new entries added here */
3998c2ecf20Sopenharmony_ci	u32 s_next_psn;         /* PSN for next request */
4008c2ecf20Sopenharmony_ci	u32 s_avail;            /* number of entries avail */
4018c2ecf20Sopenharmony_ci	u32 s_ssn;              /* SSN of tail entry */
4028c2ecf20Sopenharmony_ci	atomic_t s_reserved_used; /* reserved entries in use */
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	spinlock_t s_lock ____cacheline_aligned_in_smp;
4058c2ecf20Sopenharmony_ci	u32 s_flags;
4068c2ecf20Sopenharmony_ci	struct rvt_sge_state *s_cur_sge;
4078c2ecf20Sopenharmony_ci	struct rvt_swqe *s_wqe;
4088c2ecf20Sopenharmony_ci	struct rvt_sge_state s_sge;     /* current send request data */
4098c2ecf20Sopenharmony_ci	struct rvt_mregion *s_rdma_mr;
4108c2ecf20Sopenharmony_ci	u32 s_len;              /* total length of s_sge */
4118c2ecf20Sopenharmony_ci	u32 s_rdma_read_len;    /* total length of s_rdma_read_sge */
4128c2ecf20Sopenharmony_ci	u32 s_last_psn;         /* last response PSN processed */
4138c2ecf20Sopenharmony_ci	u32 s_sending_psn;      /* lowest PSN that is being sent */
4148c2ecf20Sopenharmony_ci	u32 s_sending_hpsn;     /* highest PSN that is being sent */
4158c2ecf20Sopenharmony_ci	u32 s_psn;              /* current packet sequence number */
4168c2ecf20Sopenharmony_ci	u32 s_ack_rdma_psn;     /* PSN for sending RDMA read responses */
4178c2ecf20Sopenharmony_ci	u32 s_ack_psn;          /* PSN for acking sends and RDMA writes */
4188c2ecf20Sopenharmony_ci	u32 s_tail;             /* next entry to process */
4198c2ecf20Sopenharmony_ci	u32 s_cur;              /* current work queue entry */
4208c2ecf20Sopenharmony_ci	u32 s_acked;            /* last un-ACK'ed entry */
4218c2ecf20Sopenharmony_ci	u32 s_last;             /* last completed entry */
4228c2ecf20Sopenharmony_ci	u32 s_lsn;              /* limit sequence number (credit) */
4238c2ecf20Sopenharmony_ci	u32 s_ahgpsn;           /* set to the psn in the copy of the header */
4248c2ecf20Sopenharmony_ci	u16 s_cur_size;         /* size of send packet in bytes */
4258c2ecf20Sopenharmony_ci	u16 s_rdma_ack_cnt;
4268c2ecf20Sopenharmony_ci	u8 s_hdrwords;         /* size of s_hdr in 32 bit words */
4278c2ecf20Sopenharmony_ci	s8 s_ahgidx;
4288c2ecf20Sopenharmony_ci	u8 s_state;             /* opcode of last packet sent */
4298c2ecf20Sopenharmony_ci	u8 s_ack_state;         /* opcode of packet to ACK */
4308c2ecf20Sopenharmony_ci	u8 s_nak_state;         /* non-zero if NAK is pending */
4318c2ecf20Sopenharmony_ci	u8 r_nak_state;         /* non-zero if NAK is pending */
4328c2ecf20Sopenharmony_ci	u8 s_retry;             /* requester retry counter */
4338c2ecf20Sopenharmony_ci	u8 s_rnr_retry;         /* requester RNR retry counter */
4348c2ecf20Sopenharmony_ci	u8 s_num_rd_atomic;     /* number of RDMA read/atomic pending */
4358c2ecf20Sopenharmony_ci	u8 s_tail_ack_queue;    /* index into s_ack_queue[] */
4368c2ecf20Sopenharmony_ci	u8 s_acked_ack_queue;   /* index into s_ack_queue[] */
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	struct rvt_sge_state s_ack_rdma_sge;
4398c2ecf20Sopenharmony_ci	struct timer_list s_timer;
4408c2ecf20Sopenharmony_ci	struct hrtimer s_rnr_timer;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	atomic_t local_ops_pending; /* number of fast_reg/local_inv reqs */
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/*
4458c2ecf20Sopenharmony_ci	 * This sge list MUST be last. Do not add anything below here.
4468c2ecf20Sopenharmony_ci	 */
4478c2ecf20Sopenharmony_ci	struct rvt_sge r_sg_list[] /* verified SGEs */
4488c2ecf20Sopenharmony_ci		____cacheline_aligned_in_smp;
4498c2ecf20Sopenharmony_ci};
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistruct rvt_srq {
4528c2ecf20Sopenharmony_ci	struct ib_srq ibsrq;
4538c2ecf20Sopenharmony_ci	struct rvt_rq rq;
4548c2ecf20Sopenharmony_ci	struct rvt_mmap_info *ip;
4558c2ecf20Sopenharmony_ci	/* send signal when number of RWQEs < limit */
4568c2ecf20Sopenharmony_ci	u32 limit;
4578c2ecf20Sopenharmony_ci};
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic inline struct rvt_srq *ibsrq_to_rvtsrq(struct ib_srq *ibsrq)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	return container_of(ibsrq, struct rvt_srq, ibsrq);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic inline struct rvt_qp *ibqp_to_rvtqp(struct ib_qp *ibqp)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	return container_of(ibqp, struct rvt_qp, ibqp);
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci#define RVT_QPN_MAX                 BIT(24)
4708c2ecf20Sopenharmony_ci#define RVT_QPNMAP_ENTRIES          (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)
4718c2ecf20Sopenharmony_ci#define RVT_BITS_PER_PAGE           (PAGE_SIZE * BITS_PER_BYTE)
4728c2ecf20Sopenharmony_ci#define RVT_BITS_PER_PAGE_MASK      (RVT_BITS_PER_PAGE - 1)
4738c2ecf20Sopenharmony_ci#define RVT_QPN_MASK		    IB_QPN_MASK
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci/*
4768c2ecf20Sopenharmony_ci * QPN-map pages start out as NULL, they get allocated upon
4778c2ecf20Sopenharmony_ci * first use and are never deallocated. This way,
4788c2ecf20Sopenharmony_ci * large bitmaps are not allocated unless large numbers of QPs are used.
4798c2ecf20Sopenharmony_ci */
4808c2ecf20Sopenharmony_cistruct rvt_qpn_map {
4818c2ecf20Sopenharmony_ci	void *page;
4828c2ecf20Sopenharmony_ci};
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistruct rvt_qpn_table {
4858c2ecf20Sopenharmony_ci	spinlock_t lock; /* protect changes to the qp table */
4868c2ecf20Sopenharmony_ci	unsigned flags;         /* flags for QP0/1 allocated for each port */
4878c2ecf20Sopenharmony_ci	u32 last;               /* last QP number allocated */
4888c2ecf20Sopenharmony_ci	u32 nmaps;              /* size of the map table */
4898c2ecf20Sopenharmony_ci	u16 limit;
4908c2ecf20Sopenharmony_ci	u8  incr;
4918c2ecf20Sopenharmony_ci	/* bit map of free QP numbers other than 0/1 */
4928c2ecf20Sopenharmony_ci	struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];
4938c2ecf20Sopenharmony_ci};
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistruct rvt_qp_ibdev {
4968c2ecf20Sopenharmony_ci	u32 qp_table_size;
4978c2ecf20Sopenharmony_ci	u32 qp_table_bits;
4988c2ecf20Sopenharmony_ci	struct rvt_qp __rcu **qp_table;
4998c2ecf20Sopenharmony_ci	spinlock_t qpt_lock; /* qptable lock */
5008c2ecf20Sopenharmony_ci	struct rvt_qpn_table qpn_table;
5018c2ecf20Sopenharmony_ci};
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci/*
5048c2ecf20Sopenharmony_ci * There is one struct rvt_mcast for each multicast GID.
5058c2ecf20Sopenharmony_ci * All attached QPs are then stored as a list of
5068c2ecf20Sopenharmony_ci * struct rvt_mcast_qp.
5078c2ecf20Sopenharmony_ci */
5088c2ecf20Sopenharmony_cistruct rvt_mcast_qp {
5098c2ecf20Sopenharmony_ci	struct list_head list;
5108c2ecf20Sopenharmony_ci	struct rvt_qp *qp;
5118c2ecf20Sopenharmony_ci};
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistruct rvt_mcast_addr {
5148c2ecf20Sopenharmony_ci	union ib_gid mgid;
5158c2ecf20Sopenharmony_ci	u16 lid;
5168c2ecf20Sopenharmony_ci};
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistruct rvt_mcast {
5198c2ecf20Sopenharmony_ci	struct rb_node rb_node;
5208c2ecf20Sopenharmony_ci	struct rvt_mcast_addr mcast_addr;
5218c2ecf20Sopenharmony_ci	struct list_head qp_list;
5228c2ecf20Sopenharmony_ci	wait_queue_head_t wait;
5238c2ecf20Sopenharmony_ci	atomic_t refcount;
5248c2ecf20Sopenharmony_ci	int n_attached;
5258c2ecf20Sopenharmony_ci};
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci/*
5288c2ecf20Sopenharmony_ci * Since struct rvt_swqe is not a fixed size, we can't simply index into
5298c2ecf20Sopenharmony_ci * struct rvt_qp.s_wq.  This function does the array index computation.
5308c2ecf20Sopenharmony_ci */
5318c2ecf20Sopenharmony_cistatic inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,
5328c2ecf20Sopenharmony_ci						unsigned n)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	return (struct rvt_swqe *)((char *)qp->s_wq +
5358c2ecf20Sopenharmony_ci				     (sizeof(struct rvt_swqe) +
5368c2ecf20Sopenharmony_ci				      qp->s_max_sge *
5378c2ecf20Sopenharmony_ci				      sizeof(struct rvt_sge)) * n);
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci/*
5418c2ecf20Sopenharmony_ci * Since struct rvt_rwqe is not a fixed size, we can't simply index into
5428c2ecf20Sopenharmony_ci * struct rvt_rwq.wq.  This function does the array index computation.
5438c2ecf20Sopenharmony_ci */
5448c2ecf20Sopenharmony_cistatic inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	return (struct rvt_rwqe *)
5478c2ecf20Sopenharmony_ci		((char *)rq->kwq->curr_wq +
5488c2ecf20Sopenharmony_ci		 (sizeof(struct rvt_rwqe) +
5498c2ecf20Sopenharmony_ci		  rq->max_sge * sizeof(struct ib_sge)) * n);
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci/**
5538c2ecf20Sopenharmony_ci * rvt_is_user_qp - return if this is user mode QP
5548c2ecf20Sopenharmony_ci * @qp - the target QP
5558c2ecf20Sopenharmony_ci */
5568c2ecf20Sopenharmony_cistatic inline bool rvt_is_user_qp(struct rvt_qp *qp)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	return !!qp->pid;
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/**
5628c2ecf20Sopenharmony_ci * rvt_get_qp - get a QP reference
5638c2ecf20Sopenharmony_ci * @qp - the QP to hold
5648c2ecf20Sopenharmony_ci */
5658c2ecf20Sopenharmony_cistatic inline void rvt_get_qp(struct rvt_qp *qp)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	atomic_inc(&qp->refcount);
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci/**
5718c2ecf20Sopenharmony_ci * rvt_put_qp - release a QP reference
5728c2ecf20Sopenharmony_ci * @qp - the QP to release
5738c2ecf20Sopenharmony_ci */
5748c2ecf20Sopenharmony_cistatic inline void rvt_put_qp(struct rvt_qp *qp)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	if (qp && atomic_dec_and_test(&qp->refcount))
5778c2ecf20Sopenharmony_ci		wake_up(&qp->wait);
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci/**
5818c2ecf20Sopenharmony_ci * rvt_put_swqe - drop mr refs held by swqe
5828c2ecf20Sopenharmony_ci * @wqe - the send wqe
5838c2ecf20Sopenharmony_ci *
5848c2ecf20Sopenharmony_ci * This drops any mr references held by the swqe
5858c2ecf20Sopenharmony_ci */
5868c2ecf20Sopenharmony_cistatic inline void rvt_put_swqe(struct rvt_swqe *wqe)
5878c2ecf20Sopenharmony_ci{
5888c2ecf20Sopenharmony_ci	int i;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	for (i = 0; i < wqe->wr.num_sge; i++) {
5918c2ecf20Sopenharmony_ci		struct rvt_sge *sge = &wqe->sg_list[i];
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		rvt_put_mr(sge->mr);
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci/**
5988c2ecf20Sopenharmony_ci * rvt_qp_wqe_reserve - reserve operation
5998c2ecf20Sopenharmony_ci * @qp - the rvt qp
6008c2ecf20Sopenharmony_ci * @wqe - the send wqe
6018c2ecf20Sopenharmony_ci *
6028c2ecf20Sopenharmony_ci * This routine used in post send to record
6038c2ecf20Sopenharmony_ci * a wqe relative reserved operation use.
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_cistatic inline void rvt_qp_wqe_reserve(
6068c2ecf20Sopenharmony_ci	struct rvt_qp *qp,
6078c2ecf20Sopenharmony_ci	struct rvt_swqe *wqe)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	atomic_inc(&qp->s_reserved_used);
6108c2ecf20Sopenharmony_ci}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci/**
6138c2ecf20Sopenharmony_ci * rvt_qp_wqe_unreserve - clean reserved operation
6148c2ecf20Sopenharmony_ci * @qp - the rvt qp
6158c2ecf20Sopenharmony_ci * @flags - send wqe flags
6168c2ecf20Sopenharmony_ci *
6178c2ecf20Sopenharmony_ci * This decrements the reserve use count.
6188c2ecf20Sopenharmony_ci *
6198c2ecf20Sopenharmony_ci * This call MUST precede the change to
6208c2ecf20Sopenharmony_ci * s_last to insure that post send sees a stable
6218c2ecf20Sopenharmony_ci * s_avail.
6228c2ecf20Sopenharmony_ci *
6238c2ecf20Sopenharmony_ci * An smp_mp__after_atomic() is used to insure
6248c2ecf20Sopenharmony_ci * the compiler does not juggle the order of the s_last
6258c2ecf20Sopenharmony_ci * ring index and the decrementing of s_reserved_used.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cistatic inline void rvt_qp_wqe_unreserve(struct rvt_qp *qp, int flags)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	if (unlikely(flags & RVT_SEND_RESERVE_USED)) {
6308c2ecf20Sopenharmony_ci		atomic_dec(&qp->s_reserved_used);
6318c2ecf20Sopenharmony_ci		/* insure no compiler re-order up to s_last change */
6328c2ecf20Sopenharmony_ci		smp_mb__after_atomic();
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ciextern const enum ib_wc_opcode ib_rvt_wc_opcode[];
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci/*
6398c2ecf20Sopenharmony_ci * Compare the lower 24 bits of the msn values.
6408c2ecf20Sopenharmony_ci * Returns an integer <, ==, or > than zero.
6418c2ecf20Sopenharmony_ci */
6428c2ecf20Sopenharmony_cistatic inline int rvt_cmp_msn(u32 a, u32 b)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	return (((int)a) - ((int)b)) << 8;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci__be32 rvt_compute_aeth(struct rvt_qp *qp);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_civoid rvt_get_credit(struct rvt_qp *qp, u32 aeth);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ciu32 rvt_restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe, u32 len);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci/**
6548c2ecf20Sopenharmony_ci * rvt_div_round_up_mtu - round up divide
6558c2ecf20Sopenharmony_ci * @qp - the qp pair
6568c2ecf20Sopenharmony_ci * @len - the length
6578c2ecf20Sopenharmony_ci *
6588c2ecf20Sopenharmony_ci * Perform a shift based mtu round up divide
6598c2ecf20Sopenharmony_ci */
6608c2ecf20Sopenharmony_cistatic inline u32 rvt_div_round_up_mtu(struct rvt_qp *qp, u32 len)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	return (len + qp->pmtu - 1) >> qp->log_pmtu;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/**
6668c2ecf20Sopenharmony_ci * @qp - the qp pair
6678c2ecf20Sopenharmony_ci * @len - the length
6688c2ecf20Sopenharmony_ci *
6698c2ecf20Sopenharmony_ci * Perform a shift based mtu divide
6708c2ecf20Sopenharmony_ci */
6718c2ecf20Sopenharmony_cistatic inline u32 rvt_div_mtu(struct rvt_qp *qp, u32 len)
6728c2ecf20Sopenharmony_ci{
6738c2ecf20Sopenharmony_ci	return len >> qp->log_pmtu;
6748c2ecf20Sopenharmony_ci}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci/**
6778c2ecf20Sopenharmony_ci * rvt_timeout_to_jiffies - Convert a ULP timeout input into jiffies
6788c2ecf20Sopenharmony_ci * @timeout - timeout input(0 - 31).
6798c2ecf20Sopenharmony_ci *
6808c2ecf20Sopenharmony_ci * Return a timeout value in jiffies.
6818c2ecf20Sopenharmony_ci */
6828c2ecf20Sopenharmony_cistatic inline unsigned long rvt_timeout_to_jiffies(u8 timeout)
6838c2ecf20Sopenharmony_ci{
6848c2ecf20Sopenharmony_ci	if (timeout > 31)
6858c2ecf20Sopenharmony_ci		timeout = 31;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	return usecs_to_jiffies(1U << timeout) * 4096UL / 1000UL;
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci/**
6918c2ecf20Sopenharmony_ci * rvt_lookup_qpn - return the QP with the given QPN
6928c2ecf20Sopenharmony_ci * @ibp: the ibport
6938c2ecf20Sopenharmony_ci * @qpn: the QP number to look up
6948c2ecf20Sopenharmony_ci *
6958c2ecf20Sopenharmony_ci * The caller must hold the rcu_read_lock(), and keep the lock until
6968c2ecf20Sopenharmony_ci * the returned qp is no longer in use.
6978c2ecf20Sopenharmony_ci */
6988c2ecf20Sopenharmony_cistatic inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,
6998c2ecf20Sopenharmony_ci					    struct rvt_ibport *rvp,
7008c2ecf20Sopenharmony_ci					    u32 qpn) __must_hold(RCU)
7018c2ecf20Sopenharmony_ci{
7028c2ecf20Sopenharmony_ci	struct rvt_qp *qp = NULL;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	if (unlikely(qpn <= 1)) {
7058c2ecf20Sopenharmony_ci		qp = rcu_dereference(rvp->qp[qpn]);
7068c2ecf20Sopenharmony_ci	} else {
7078c2ecf20Sopenharmony_ci		u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci		for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;
7108c2ecf20Sopenharmony_ci			qp = rcu_dereference(qp->next))
7118c2ecf20Sopenharmony_ci			if (qp->ibqp.qp_num == qpn)
7128c2ecf20Sopenharmony_ci				break;
7138c2ecf20Sopenharmony_ci	}
7148c2ecf20Sopenharmony_ci	return qp;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci/**
7188c2ecf20Sopenharmony_ci * rvt_mod_retry_timer - mod a retry timer
7198c2ecf20Sopenharmony_ci * @qp - the QP
7208c2ecf20Sopenharmony_ci * @shift - timeout shift to wait for multiple packets
7218c2ecf20Sopenharmony_ci * Modify a potentially already running retry timer
7228c2ecf20Sopenharmony_ci */
7238c2ecf20Sopenharmony_cistatic inline void rvt_mod_retry_timer_ext(struct rvt_qp *qp, u8 shift)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	struct ib_qp *ibqp = &qp->ibqp;
7268c2ecf20Sopenharmony_ci	struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	lockdep_assert_held(&qp->s_lock);
7298c2ecf20Sopenharmony_ci	qp->s_flags |= RVT_S_TIMER;
7308c2ecf20Sopenharmony_ci	/* 4.096 usec. * (1 << qp->timeout) */
7318c2ecf20Sopenharmony_ci	mod_timer(&qp->s_timer, jiffies + rdi->busy_jiffies +
7328c2ecf20Sopenharmony_ci		  (qp->timeout_jiffies << shift));
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic inline void rvt_mod_retry_timer(struct rvt_qp *qp)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	return rvt_mod_retry_timer_ext(qp, 0);
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci/**
7418c2ecf20Sopenharmony_ci * rvt_put_qp_swqe - drop refs held by swqe
7428c2ecf20Sopenharmony_ci * @qp: the send qp
7438c2ecf20Sopenharmony_ci * @wqe: the send wqe
7448c2ecf20Sopenharmony_ci *
7458c2ecf20Sopenharmony_ci * This drops any references held by the swqe
7468c2ecf20Sopenharmony_ci */
7478c2ecf20Sopenharmony_cistatic inline void rvt_put_qp_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	rvt_put_swqe(wqe);
7508c2ecf20Sopenharmony_ci	if (qp->allowed_ops == IB_OPCODE_UD)
7518c2ecf20Sopenharmony_ci		rdma_destroy_ah_attr(wqe->ud_wr.attr);
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci/**
7558c2ecf20Sopenharmony_ci * rvt_qp_sqwe_incr - increment ring index
7568c2ecf20Sopenharmony_ci * @qp: the qp
7578c2ecf20Sopenharmony_ci * @val: the starting value
7588c2ecf20Sopenharmony_ci *
7598c2ecf20Sopenharmony_ci * Return: the new value wrapping as appropriate
7608c2ecf20Sopenharmony_ci */
7618c2ecf20Sopenharmony_cistatic inline u32
7628c2ecf20Sopenharmony_cirvt_qp_swqe_incr(struct rvt_qp *qp, u32 val)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	if (++val >= qp->s_size)
7658c2ecf20Sopenharmony_ci		val = 0;
7668c2ecf20Sopenharmony_ci	return val;
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ciint rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci/**
7728c2ecf20Sopenharmony_ci * rvt_recv_cq - add a new entry to completion queue
7738c2ecf20Sopenharmony_ci *			by receive queue
7748c2ecf20Sopenharmony_ci * @qp: receive queue
7758c2ecf20Sopenharmony_ci * @wc: work completion entry to add
7768c2ecf20Sopenharmony_ci * @solicited: true if @entry is solicited
7778c2ecf20Sopenharmony_ci *
7788c2ecf20Sopenharmony_ci * This is wrapper function for rvt_enter_cq function call by
7798c2ecf20Sopenharmony_ci * receive queue. If rvt_cq_enter return false, it means cq is
7808c2ecf20Sopenharmony_ci * full and the qp is put into error state.
7818c2ecf20Sopenharmony_ci */
7828c2ecf20Sopenharmony_cistatic inline void rvt_recv_cq(struct rvt_qp *qp, struct ib_wc *wc,
7838c2ecf20Sopenharmony_ci			       bool solicited)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.recv_cq);
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
7888c2ecf20Sopenharmony_ci		rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci/**
7928c2ecf20Sopenharmony_ci * rvt_send_cq - add a new entry to completion queue
7938c2ecf20Sopenharmony_ci *                        by send queue
7948c2ecf20Sopenharmony_ci * @qp: send queue
7958c2ecf20Sopenharmony_ci * @wc: work completion entry to add
7968c2ecf20Sopenharmony_ci * @solicited: true if @entry is solicited
7978c2ecf20Sopenharmony_ci *
7988c2ecf20Sopenharmony_ci * This is wrapper function for rvt_enter_cq function call by
7998c2ecf20Sopenharmony_ci * send queue. If rvt_cq_enter return false, it means cq is
8008c2ecf20Sopenharmony_ci * full and the qp is put into error state.
8018c2ecf20Sopenharmony_ci */
8028c2ecf20Sopenharmony_cistatic inline void rvt_send_cq(struct rvt_qp *qp, struct ib_wc *wc,
8038c2ecf20Sopenharmony_ci			       bool solicited)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.send_cq);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
8088c2ecf20Sopenharmony_ci		rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci/**
8128c2ecf20Sopenharmony_ci * rvt_qp_complete_swqe - insert send completion
8138c2ecf20Sopenharmony_ci * @qp - the qp
8148c2ecf20Sopenharmony_ci * @wqe - the send wqe
8158c2ecf20Sopenharmony_ci * @opcode - wc operation (driver dependent)
8168c2ecf20Sopenharmony_ci * @status - completion status
8178c2ecf20Sopenharmony_ci *
8188c2ecf20Sopenharmony_ci * Update the s_last information, and then insert a send
8198c2ecf20Sopenharmony_ci * completion into the completion
8208c2ecf20Sopenharmony_ci * queue if the qp indicates it should be done.
8218c2ecf20Sopenharmony_ci *
8228c2ecf20Sopenharmony_ci * See IBTA 10.7.3.1 for info on completion
8238c2ecf20Sopenharmony_ci * control.
8248c2ecf20Sopenharmony_ci *
8258c2ecf20Sopenharmony_ci * Return: new last
8268c2ecf20Sopenharmony_ci */
8278c2ecf20Sopenharmony_cistatic inline u32
8288c2ecf20Sopenharmony_cirvt_qp_complete_swqe(struct rvt_qp *qp,
8298c2ecf20Sopenharmony_ci		     struct rvt_swqe *wqe,
8308c2ecf20Sopenharmony_ci		     enum ib_wc_opcode opcode,
8318c2ecf20Sopenharmony_ci		     enum ib_wc_status status)
8328c2ecf20Sopenharmony_ci{
8338c2ecf20Sopenharmony_ci	bool need_completion;
8348c2ecf20Sopenharmony_ci	u64 wr_id;
8358c2ecf20Sopenharmony_ci	u32 byte_len, last;
8368c2ecf20Sopenharmony_ci	int flags = wqe->wr.send_flags;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	rvt_qp_wqe_unreserve(qp, flags);
8398c2ecf20Sopenharmony_ci	rvt_put_qp_swqe(qp, wqe);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	need_completion =
8428c2ecf20Sopenharmony_ci		!(flags & RVT_SEND_RESERVE_USED) &&
8438c2ecf20Sopenharmony_ci		(!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
8448c2ecf20Sopenharmony_ci		(flags & IB_SEND_SIGNALED) ||
8458c2ecf20Sopenharmony_ci		status != IB_WC_SUCCESS);
8468c2ecf20Sopenharmony_ci	if (need_completion) {
8478c2ecf20Sopenharmony_ci		wr_id = wqe->wr.wr_id;
8488c2ecf20Sopenharmony_ci		byte_len = wqe->length;
8498c2ecf20Sopenharmony_ci		/* above fields required before writing s_last */
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_ci	last = rvt_qp_swqe_incr(qp, qp->s_last);
8528c2ecf20Sopenharmony_ci	/* see rvt_qp_is_avail() */
8538c2ecf20Sopenharmony_ci	smp_store_release(&qp->s_last, last);
8548c2ecf20Sopenharmony_ci	if (need_completion) {
8558c2ecf20Sopenharmony_ci		struct ib_wc w = {
8568c2ecf20Sopenharmony_ci			.wr_id = wr_id,
8578c2ecf20Sopenharmony_ci			.status = status,
8588c2ecf20Sopenharmony_ci			.opcode = opcode,
8598c2ecf20Sopenharmony_ci			.qp = &qp->ibqp,
8608c2ecf20Sopenharmony_ci			.byte_len = byte_len,
8618c2ecf20Sopenharmony_ci		};
8628c2ecf20Sopenharmony_ci		rvt_send_cq(qp, &w, status != IB_WC_SUCCESS);
8638c2ecf20Sopenharmony_ci	}
8648c2ecf20Sopenharmony_ci	return last;
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ciextern const int  ib_rvt_state_ops[];
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_cistruct rvt_dev_info;
8708c2ecf20Sopenharmony_ciint rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only);
8718c2ecf20Sopenharmony_civoid rvt_comm_est(struct rvt_qp *qp);
8728c2ecf20Sopenharmony_civoid rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err);
8738c2ecf20Sopenharmony_ciunsigned long rvt_rnr_tbl_to_usec(u32 index);
8748c2ecf20Sopenharmony_cienum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t);
8758c2ecf20Sopenharmony_civoid rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth);
8768c2ecf20Sopenharmony_civoid rvt_del_timers_sync(struct rvt_qp *qp);
8778c2ecf20Sopenharmony_civoid rvt_stop_rc_timers(struct rvt_qp *qp);
8788c2ecf20Sopenharmony_civoid rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift);
8798c2ecf20Sopenharmony_cistatic inline void rvt_add_retry_timer(struct rvt_qp *qp)
8808c2ecf20Sopenharmony_ci{
8818c2ecf20Sopenharmony_ci	rvt_add_retry_timer_ext(qp, 0);
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_civoid rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
8858c2ecf20Sopenharmony_ci		  void *data, u32 length,
8868c2ecf20Sopenharmony_ci		  bool release, bool copy_last);
8878c2ecf20Sopenharmony_civoid rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
8888c2ecf20Sopenharmony_ci		       enum ib_wc_status status);
8898c2ecf20Sopenharmony_civoid rvt_ruc_loopback(struct rvt_qp *qp);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci/**
8928c2ecf20Sopenharmony_ci * struct rvt_qp_iter - the iterator for QPs
8938c2ecf20Sopenharmony_ci * @qp - the current QP
8948c2ecf20Sopenharmony_ci *
8958c2ecf20Sopenharmony_ci * This structure defines the current iterator
8968c2ecf20Sopenharmony_ci * state for sequenced access to all QPs relative
8978c2ecf20Sopenharmony_ci * to an rvt_dev_info.
8988c2ecf20Sopenharmony_ci */
8998c2ecf20Sopenharmony_cistruct rvt_qp_iter {
9008c2ecf20Sopenharmony_ci	struct rvt_qp *qp;
9018c2ecf20Sopenharmony_ci	/* private: backpointer */
9028c2ecf20Sopenharmony_ci	struct rvt_dev_info *rdi;
9038c2ecf20Sopenharmony_ci	/* private: callback routine */
9048c2ecf20Sopenharmony_ci	void (*cb)(struct rvt_qp *qp, u64 v);
9058c2ecf20Sopenharmony_ci	/* private: for arg to callback routine */
9068c2ecf20Sopenharmony_ci	u64 v;
9078c2ecf20Sopenharmony_ci	/* private: number of SMI,GSI QPs for device */
9088c2ecf20Sopenharmony_ci	int specials;
9098c2ecf20Sopenharmony_ci	/* private: current iterator index */
9108c2ecf20Sopenharmony_ci	int n;
9118c2ecf20Sopenharmony_ci};
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci/**
9148c2ecf20Sopenharmony_ci * ib_cq_tail - Return tail index of cq buffer
9158c2ecf20Sopenharmony_ci * @send_cq - The cq for send
9168c2ecf20Sopenharmony_ci *
9178c2ecf20Sopenharmony_ci * This is called in qp_iter_print to get tail
9188c2ecf20Sopenharmony_ci * of cq buffer.
9198c2ecf20Sopenharmony_ci */
9208c2ecf20Sopenharmony_cistatic inline u32 ib_cq_tail(struct ib_cq *send_cq)
9218c2ecf20Sopenharmony_ci{
9228c2ecf20Sopenharmony_ci	struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	return ibcq_to_rvtcq(send_cq)->ip ?
9258c2ecf20Sopenharmony_ci	       RDMA_READ_UAPI_ATOMIC(cq->queue->tail) :
9268c2ecf20Sopenharmony_ci	       ibcq_to_rvtcq(send_cq)->kqueue->tail;
9278c2ecf20Sopenharmony_ci}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci/**
9308c2ecf20Sopenharmony_ci * ib_cq_head - Return head index of cq buffer
9318c2ecf20Sopenharmony_ci * @send_cq - The cq for send
9328c2ecf20Sopenharmony_ci *
9338c2ecf20Sopenharmony_ci * This is called in qp_iter_print to get head
9348c2ecf20Sopenharmony_ci * of cq buffer.
9358c2ecf20Sopenharmony_ci */
9368c2ecf20Sopenharmony_cistatic inline u32 ib_cq_head(struct ib_cq *send_cq)
9378c2ecf20Sopenharmony_ci{
9388c2ecf20Sopenharmony_ci	struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	return ibcq_to_rvtcq(send_cq)->ip ?
9418c2ecf20Sopenharmony_ci	       RDMA_READ_UAPI_ATOMIC(cq->queue->head) :
9428c2ecf20Sopenharmony_ci	       ibcq_to_rvtcq(send_cq)->kqueue->head;
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci/**
9468c2ecf20Sopenharmony_ci * rvt_free_rq - free memory allocated for rvt_rq struct
9478c2ecf20Sopenharmony_ci * @rvt_rq: request queue data structure
9488c2ecf20Sopenharmony_ci *
9498c2ecf20Sopenharmony_ci * This function should only be called if the rvt_mmap_info()
9508c2ecf20Sopenharmony_ci * has not succeeded.
9518c2ecf20Sopenharmony_ci */
9528c2ecf20Sopenharmony_cistatic inline void rvt_free_rq(struct rvt_rq *rq)
9538c2ecf20Sopenharmony_ci{
9548c2ecf20Sopenharmony_ci	kvfree(rq->kwq);
9558c2ecf20Sopenharmony_ci	rq->kwq = NULL;
9568c2ecf20Sopenharmony_ci	vfree(rq->wq);
9578c2ecf20Sopenharmony_ci	rq->wq = NULL;
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci/**
9618c2ecf20Sopenharmony_ci * rvt_to_iport - Get the ibport pointer
9628c2ecf20Sopenharmony_ci * @qp: the qp pointer
9638c2ecf20Sopenharmony_ci *
9648c2ecf20Sopenharmony_ci * This function returns the ibport pointer from the qp pointer.
9658c2ecf20Sopenharmony_ci */
9668c2ecf20Sopenharmony_cistatic inline struct rvt_ibport *rvt_to_iport(struct rvt_qp *qp)
9678c2ecf20Sopenharmony_ci{
9688c2ecf20Sopenharmony_ci	struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	return rdi->ports[qp->port_num - 1];
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci/**
9748c2ecf20Sopenharmony_ci * rvt_rc_credit_avail - Check if there are enough RC credits for the request
9758c2ecf20Sopenharmony_ci * @qp: the qp
9768c2ecf20Sopenharmony_ci * @wqe: the request
9778c2ecf20Sopenharmony_ci *
9788c2ecf20Sopenharmony_ci * This function returns false when there are not enough credits for the given
9798c2ecf20Sopenharmony_ci * request and true otherwise.
9808c2ecf20Sopenharmony_ci */
9818c2ecf20Sopenharmony_cistatic inline bool rvt_rc_credit_avail(struct rvt_qp *qp, struct rvt_swqe *wqe)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	lockdep_assert_held(&qp->s_lock);
9848c2ecf20Sopenharmony_ci	if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
9858c2ecf20Sopenharmony_ci	    rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
9868c2ecf20Sopenharmony_ci		struct rvt_ibport *rvp = rvt_to_iport(qp);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci		qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
9898c2ecf20Sopenharmony_ci		rvp->n_rc_crwaits++;
9908c2ecf20Sopenharmony_ci		return false;
9918c2ecf20Sopenharmony_ci	}
9928c2ecf20Sopenharmony_ci	return true;
9938c2ecf20Sopenharmony_ci}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_cistruct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
9968c2ecf20Sopenharmony_ci				     u64 v,
9978c2ecf20Sopenharmony_ci				     void (*cb)(struct rvt_qp *qp, u64 v));
9988c2ecf20Sopenharmony_ciint rvt_qp_iter_next(struct rvt_qp_iter *iter);
9998c2ecf20Sopenharmony_civoid rvt_qp_iter(struct rvt_dev_info *rdi,
10008c2ecf20Sopenharmony_ci		 u64 v,
10018c2ecf20Sopenharmony_ci		 void (*cb)(struct rvt_qp *qp, u64 v));
10028c2ecf20Sopenharmony_civoid rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey);
10038c2ecf20Sopenharmony_ci#endif          /* DEF_RDMAVT_INCQP_H */
1004