/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */1/*2* Copyright(c) 2016 - 2020 Intel Corporation.3*/45#ifndef DEF_RDMAVT_INCQP_H6#define DEF_RDMAVT_INCQP_H78#include <rdma/rdma_vt.h>9#include <rdma/ib_pack.h>10#include <rdma/ib_verbs.h>11#include <rdma/rdmavt_cq.h>12#include <rdma/rvt-abi.h>13#include <linux/vmalloc.h>14/*15* Atomic bit definitions for r_aflags.16*/17#define RVT_R_WRID_VALID 018#define RVT_R_REWIND_SGE 11920/*21* Bit definitions for r_flags.22*/23#define RVT_R_REUSE_SGE 0x0124#define RVT_R_RDMAR_SEQ 0x0225#define RVT_R_RSP_NAK 0x0426#define RVT_R_RSP_SEND 0x0827#define RVT_R_COMM_EST 0x102829/*30* If a packet's QP[23:16] bits match this value, then it is31* a PSM packet and the hardware will expect a KDETH header32* following the BTH.33*/34#define RVT_KDETH_QP_PREFIX 0x8035#define RVT_KDETH_QP_SUFFIX 0xffff36#define RVT_KDETH_QP_PREFIX_MASK 0x00ff000037#define RVT_KDETH_QP_PREFIX_SHIFT 1638#define RVT_KDETH_QP_BASE (u32)(RVT_KDETH_QP_PREFIX << \39RVT_KDETH_QP_PREFIX_SHIFT)40#define RVT_KDETH_QP_MAX (u32)(RVT_KDETH_QP_BASE + RVT_KDETH_QP_SUFFIX)4142/*43* If a packet's LNH == BTH and DEST QPN[23:16] in the BTH match this44* prefix value, then it is an AIP packet with a DETH containing the entropy45* value in byte 4 following the BTH.46*/47#define RVT_AIP_QP_PREFIX 0x8148#define RVT_AIP_QP_SUFFIX 0xffff49#define RVT_AIP_QP_PREFIX_MASK 0x00ff000050#define RVT_AIP_QP_PREFIX_SHIFT 1651#define RVT_AIP_QP_BASE (u32)(RVT_AIP_QP_PREFIX << \52RVT_AIP_QP_PREFIX_SHIFT)53#define RVT_AIP_QPN_MAX BIT(RVT_AIP_QP_PREFIX_SHIFT)54#define RVT_AIP_QP_MAX (u32)(RVT_AIP_QP_BASE + RVT_AIP_QPN_MAX - 1)5556/*57* Bit definitions for s_flags.58*59* RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled60* RVT_S_BUSY - send tasklet is processing the QP61* RVT_S_TIMER - the RC retry timer is active62* RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics63* RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs64* before processing the next SWQE65* RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete66* before processing the next SWQE67* RVT_S_WAIT_RNR - waiting for RNR timeout68* RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE69* RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating70* next send completion entry not via send DMA71* RVT_S_WAIT_PIO - waiting for a send buffer to be available72* RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available73* RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available74* RVT_S_WAIT_KMEM - waiting for kernel memory to be available75* RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue76* RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests77* RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK78* RVT_S_ECN - a BECN was queued to the send engine79* RVT_S_MAX_BIT_MASK - The max bit that can be used by rdmavt80*/81#define RVT_S_SIGNAL_REQ_WR 0x000182#define RVT_S_BUSY 0x000283#define RVT_S_TIMER 0x000484#define RVT_S_RESP_PENDING 0x000885#define RVT_S_ACK_PENDING 0x001086#define RVT_S_WAIT_FENCE 0x002087#define RVT_S_WAIT_RDMAR 0x004088#define RVT_S_WAIT_RNR 0x008089#define RVT_S_WAIT_SSN_CREDIT 0x010090#define RVT_S_WAIT_DMA 0x020091#define RVT_S_WAIT_PIO 0x040092#define RVT_S_WAIT_TX 0x080093#define RVT_S_WAIT_DMA_DESC 0x100094#define RVT_S_WAIT_KMEM 0x200095#define RVT_S_WAIT_PSN 0x400096#define RVT_S_WAIT_ACK 0x800097#define RVT_S_SEND_ONE 0x1000098#define RVT_S_UNLIMITED_CREDIT 0x2000099#define RVT_S_ECN 0x40000100#define RVT_S_MAX_BIT_MASK 0x800000101102/*103* Drivers should use s_flags starting with bit 31 down to the bit next to104* RVT_S_MAX_BIT_MASK105*/106107/*108* Wait flags that would prevent any packet type from being sent.109*/110#define RVT_S_ANY_WAIT_IO \111(RVT_S_WAIT_PIO | RVT_S_WAIT_TX | \112RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)113114/*115* Wait flags that would prevent send work requests from making progress.116*/117#define RVT_S_ANY_WAIT_SEND (RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR | \118RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \119RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)120121#define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)122123/* Number of bits to pay attention to in the opcode for checking qp type */124#define RVT_OPCODE_QP_MASK 0xE0125126/* Flags for checking QP state (see ib_rvt_state_ops[]) */127#define RVT_POST_SEND_OK 0x01128#define RVT_POST_RECV_OK 0x02129#define RVT_PROCESS_RECV_OK 0x04130#define RVT_PROCESS_SEND_OK 0x08131#define RVT_PROCESS_NEXT_SEND_OK 0x10132#define RVT_FLUSH_SEND 0x20133#define RVT_FLUSH_RECV 0x40134#define RVT_PROCESS_OR_FLUSH_SEND \135(RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)136#define RVT_SEND_OR_FLUSH_OR_RECV_OK \137(RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND | RVT_PROCESS_RECV_OK)138139/*140* Internal send flags141*/142#define RVT_SEND_RESERVE_USED IB_SEND_RESERVED_START143#define RVT_SEND_COMPLETION_ONLY (IB_SEND_RESERVED_START << 1)144145/**146* struct rvt_ud_wr - IB UD work plus AH cache147* @wr: valid IB work request148* @attr: pointer to an allocated AH attribute149*150* Special case the UD WR so we can keep track of the AH attributes.151*152* NOTE: This data structure is stricly ordered wr then attr. I.e the attr153* MUST come after wr. The ib_ud_wr is sized and copied in rvt_post_one_wr.154* The copy assumes that wr is first.155*/156struct rvt_ud_wr {157struct ib_ud_wr wr;158struct rdma_ah_attr *attr;159};160161/*162* Send work request queue entry.163* The size of the sg_list is determined when the QP is created and stored164* in qp->s_max_sge.165*/166struct rvt_swqe {167union {168struct ib_send_wr wr; /* don't use wr.sg_list */169struct rvt_ud_wr ud_wr;170struct ib_reg_wr reg_wr;171struct ib_rdma_wr rdma_wr;172struct ib_atomic_wr atomic_wr;173};174u32 psn; /* first packet sequence number */175u32 lpsn; /* last packet sequence number */176u32 ssn; /* send sequence number */177u32 length; /* total length of data in sg_list */178void *priv; /* driver dependent field */179struct rvt_sge sg_list[];180};181182/**183* struct rvt_krwq - kernel struct receive work request184* @p_lock: lock to protect producer of the kernel buffer185* @head: index of next entry to fill186* @c_lock: lock to protect consumer of the kernel buffer187* @tail: index of next entry to pull188* @count: count is approximate of total receive entries posted189* @curr_wq: struct of receive work request queue entry190*191* This structure is used to contain the head pointer,192* tail pointer and receive work queue entries for kernel193* mode user.194*/195struct rvt_krwq {196spinlock_t p_lock; /* protect producer */197u32 head; /* new work requests posted to the head */198199/* protect consumer */200spinlock_t c_lock ____cacheline_aligned_in_smp;201u32 tail; /* receives pull requests from here. */202u32 count; /* approx count of receive entries posted */203struct rvt_rwqe *curr_wq;204struct rvt_rwqe wq[];205};206207/*208* rvt_get_swqe_ah - Return the pointer to the struct rvt_ah209* @swqe: valid Send WQE210*211*/212static inline struct rvt_ah *rvt_get_swqe_ah(struct rvt_swqe *swqe)213{214return ibah_to_rvtah(swqe->ud_wr.wr.ah);215}216217/**218* rvt_get_swqe_ah_attr - Return the cached ah attribute information219* @swqe: valid Send WQE220*221*/222static inline struct rdma_ah_attr *rvt_get_swqe_ah_attr(struct rvt_swqe *swqe)223{224return swqe->ud_wr.attr;225}226227/**228* rvt_get_swqe_remote_qpn - Access the remote QPN value229* @swqe: valid Send WQE230*231*/232static inline u32 rvt_get_swqe_remote_qpn(struct rvt_swqe *swqe)233{234return swqe->ud_wr.wr.remote_qpn;235}236237/**238* rvt_get_swqe_remote_qkey - Acces the remote qkey value239* @swqe: valid Send WQE240*241*/242static inline u32 rvt_get_swqe_remote_qkey(struct rvt_swqe *swqe)243{244return swqe->ud_wr.wr.remote_qkey;245}246247/**248* rvt_get_swqe_pkey_index - Access the pkey index249* @swqe: valid Send WQE250*251*/252static inline u16 rvt_get_swqe_pkey_index(struct rvt_swqe *swqe)253{254return swqe->ud_wr.wr.pkey_index;255}256257struct rvt_rq {258struct rvt_rwq *wq;259struct rvt_krwq *kwq;260u32 size; /* size of RWQE array */261u8 max_sge;262/* protect changes in this struct */263spinlock_t lock ____cacheline_aligned_in_smp;264};265266/**267* rvt_get_rq_count - count numbers of request work queue entries268* in circular buffer269* @rq: data structure for request queue entry270* @head: head indices of the circular buffer271* @tail: tail indices of the circular buffer272*273* Return - total number of entries in the Receive Queue274*/275276static inline u32 rvt_get_rq_count(struct rvt_rq *rq, u32 head, u32 tail)277{278u32 count = head - tail;279280if ((s32)count < 0)281count += rq->size;282return count;283}284285/*286* This structure holds the information that the send tasklet needs287* to send a RDMA read response or atomic operation.288*/289struct rvt_ack_entry {290struct rvt_sge rdma_sge;291u64 atomic_data;292u32 psn;293u32 lpsn;294u8 opcode;295u8 sent;296void *priv;297};298299#define RC_QP_SCALING_INTERVAL 5300301#define RVT_OPERATION_PRIV 0x00000001302#define RVT_OPERATION_ATOMIC 0x00000002303#define RVT_OPERATION_ATOMIC_SGE 0x00000004304#define RVT_OPERATION_LOCAL 0x00000008305#define RVT_OPERATION_USE_RESERVE 0x00000010306#define RVT_OPERATION_IGN_RNR_CNT 0x00000020307308#define RVT_OPERATION_MAX (IB_WR_RESERVED10 + 1)309310/**311* struct rvt_operation_params - op table entry312* @length: the length to copy into the swqe entry313* @qpt_support: a bit mask indicating QP type support314* @flags: RVT_OPERATION flags (see above)315*316* This supports table driven post send so that317* the driver can have differing an potentially318* different sets of operations.319*320**/321322struct rvt_operation_params {323size_t length;324u32 qpt_support;325u32 flags;326};327328/*329* Common variables are protected by both r_rq.lock and s_lock in that order330* which only happens in modify_qp() or changing the QP 'state'.331*/332struct rvt_qp {333struct ib_qp ibqp;334void *priv; /* Driver private data */335/* read mostly fields above and below */336struct rdma_ah_attr remote_ah_attr;337struct rdma_ah_attr alt_ah_attr;338struct rvt_qp __rcu *next; /* link list for QPN hash table */339struct rvt_swqe *s_wq; /* send work queue */340struct rvt_mmap_info *ip;341342unsigned long timeout_jiffies; /* computed from timeout */343344int srate_mbps; /* s_srate (below) converted to Mbit/s */345pid_t pid; /* pid for user mode QPs */346u32 remote_qpn;347u32 qkey; /* QKEY for this QP (for UD or RD) */348u32 s_size; /* send work queue size */349350u16 pmtu; /* decoded from path_mtu */351u8 log_pmtu; /* shift for pmtu */352u8 state; /* QP state */353u8 allowed_ops; /* high order bits of allowed opcodes */354u8 qp_access_flags;355u8 alt_timeout; /* Alternate path timeout for this QP */356u8 timeout; /* Timeout for this QP */357u8 s_srate;358u8 s_mig_state;359u8 port_num;360u8 s_pkey_index; /* PKEY index to use */361u8 s_alt_pkey_index; /* Alternate path PKEY index to use */362u8 r_max_rd_atomic; /* max number of RDMA read/atomic to receive */363u8 s_max_rd_atomic; /* max number of RDMA read/atomic to send */364u8 s_retry_cnt; /* number of times to retry */365u8 s_rnr_retry_cnt;366u8 r_min_rnr_timer; /* retry timeout value for RNR NAKs */367u8 s_max_sge; /* size of s_wq->sg_list */368u8 s_draining;369370/* start of read/write fields */371atomic_t refcount ____cacheline_aligned_in_smp;372wait_queue_head_t wait;373374struct rvt_ack_entry *s_ack_queue;375struct rvt_sge_state s_rdma_read_sge;376377spinlock_t r_lock ____cacheline_aligned_in_smp; /* used for APM */378u32 r_psn; /* expected rcv packet sequence number */379unsigned long r_aflags;380u64 r_wr_id; /* ID for current receive WQE */381u32 r_ack_psn; /* PSN for next ACK or atomic ACK */382u32 r_len; /* total length of r_sge */383u32 r_rcv_len; /* receive data len processed */384u32 r_msn; /* message sequence number */385386u8 r_state; /* opcode of last packet received */387u8 r_flags;388u8 r_head_ack_queue; /* index into s_ack_queue[] */389u8 r_adefered; /* defered ack count */390391struct list_head rspwait; /* link for waiting to respond */392393struct rvt_sge_state r_sge; /* current receive data */394struct rvt_rq r_rq; /* receive work queue */395396/* post send line */397spinlock_t s_hlock ____cacheline_aligned_in_smp;398u32 s_head; /* new entries added here */399u32 s_next_psn; /* PSN for next request */400u32 s_avail; /* number of entries avail */401u32 s_ssn; /* SSN of tail entry */402atomic_t s_reserved_used; /* reserved entries in use */403404spinlock_t s_lock ____cacheline_aligned_in_smp;405u32 s_flags;406struct rvt_sge_state *s_cur_sge;407struct rvt_swqe *s_wqe;408struct rvt_sge_state s_sge; /* current send request data */409struct rvt_mregion *s_rdma_mr;410u32 s_len; /* total length of s_sge */411u32 s_rdma_read_len; /* total length of s_rdma_read_sge */412u32 s_last_psn; /* last response PSN processed */413u32 s_sending_psn; /* lowest PSN that is being sent */414u32 s_sending_hpsn; /* highest PSN that is being sent */415u32 s_psn; /* current packet sequence number */416u32 s_ack_rdma_psn; /* PSN for sending RDMA read responses */417u32 s_ack_psn; /* PSN for acking sends and RDMA writes */418u32 s_tail; /* next entry to process */419u32 s_cur; /* current work queue entry */420u32 s_acked; /* last un-ACK'ed entry */421u32 s_last; /* last completed entry */422u32 s_lsn; /* limit sequence number (credit) */423u32 s_ahgpsn; /* set to the psn in the copy of the header */424u16 s_cur_size; /* size of send packet in bytes */425u16 s_rdma_ack_cnt;426u8 s_hdrwords; /* size of s_hdr in 32 bit words */427s8 s_ahgidx;428u8 s_state; /* opcode of last packet sent */429u8 s_ack_state; /* opcode of packet to ACK */430u8 s_nak_state; /* non-zero if NAK is pending */431u8 r_nak_state; /* non-zero if NAK is pending */432u8 s_retry; /* requester retry counter */433u8 s_rnr_retry; /* requester RNR retry counter */434u8 s_num_rd_atomic; /* number of RDMA read/atomic pending */435u8 s_tail_ack_queue; /* index into s_ack_queue[] */436u8 s_acked_ack_queue; /* index into s_ack_queue[] */437438struct rvt_sge_state s_ack_rdma_sge;439struct timer_list s_timer;440struct hrtimer s_rnr_timer;441442atomic_t local_ops_pending; /* number of fast_reg/local_inv reqs */443444/*445* This sge list MUST be last. Do not add anything below here.446*/447struct rvt_sge *r_sg_list /* verified SGEs */448____cacheline_aligned_in_smp;449};450451struct rvt_srq {452struct ib_srq ibsrq;453struct rvt_rq rq;454struct rvt_mmap_info *ip;455/* send signal when number of RWQEs < limit */456u32 limit;457};458459static inline struct rvt_srq *ibsrq_to_rvtsrq(struct ib_srq *ibsrq)460{461return container_of(ibsrq, struct rvt_srq, ibsrq);462}463464static inline struct rvt_qp *ibqp_to_rvtqp(struct ib_qp *ibqp)465{466return container_of(ibqp, struct rvt_qp, ibqp);467}468469#define RVT_QPN_MAX BIT(24)470#define RVT_QPNMAP_ENTRIES (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)471#define RVT_BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE)472#define RVT_BITS_PER_PAGE_MASK (RVT_BITS_PER_PAGE - 1)473#define RVT_QPN_MASK IB_QPN_MASK474475/*476* QPN-map pages start out as NULL, they get allocated upon477* first use and are never deallocated. This way,478* large bitmaps are not allocated unless large numbers of QPs are used.479*/480struct rvt_qpn_map {481void *page;482};483484struct rvt_qpn_table {485spinlock_t lock; /* protect changes to the qp table */486unsigned flags; /* flags for QP0/1 allocated for each port */487u32 last; /* last QP number allocated */488u32 nmaps; /* size of the map table */489u16 limit;490u8 incr;491/* bit map of free QP numbers other than 0/1 */492struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];493};494495struct rvt_qp_ibdev {496u32 qp_table_size;497u32 qp_table_bits;498struct rvt_qp __rcu **qp_table;499spinlock_t qpt_lock; /* qptable lock */500struct rvt_qpn_table qpn_table;501};502503/*504* There is one struct rvt_mcast for each multicast GID.505* All attached QPs are then stored as a list of506* struct rvt_mcast_qp.507*/508struct rvt_mcast_qp {509struct list_head list;510struct rvt_qp *qp;511};512513struct rvt_mcast_addr {514union ib_gid mgid;515u16 lid;516};517518struct rvt_mcast {519struct rb_node rb_node;520struct rvt_mcast_addr mcast_addr;521struct list_head qp_list;522wait_queue_head_t wait;523atomic_t refcount;524int n_attached;525};526527/*528* Since struct rvt_swqe is not a fixed size, we can't simply index into529* struct rvt_qp.s_wq. This function does the array index computation.530*/531static inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,532unsigned n)533{534return (struct rvt_swqe *)((char *)qp->s_wq +535(sizeof(struct rvt_swqe) +536qp->s_max_sge *537sizeof(struct rvt_sge)) * n);538}539540/*541* Since struct rvt_rwqe is not a fixed size, we can't simply index into542* struct rvt_rwq.wq. This function does the array index computation.543*/544static inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)545{546return (struct rvt_rwqe *)547((char *)rq->kwq->curr_wq +548(sizeof(struct rvt_rwqe) +549rq->max_sge * sizeof(struct ib_sge)) * n);550}551552/**553* rvt_is_user_qp - return if this is user mode QP554* @qp: the target QP555*/556static inline bool rvt_is_user_qp(struct rvt_qp *qp)557{558return !!qp->pid;559}560561/**562* rvt_get_qp - get a QP reference563* @qp: the QP to hold564*/565static inline void rvt_get_qp(struct rvt_qp *qp)566{567atomic_inc(&qp->refcount);568}569570/**571* rvt_put_qp - release a QP reference572* @qp: the QP to release573*/574static inline void rvt_put_qp(struct rvt_qp *qp)575{576if (qp && atomic_dec_and_test(&qp->refcount))577wake_up(&qp->wait);578}579580/**581* rvt_put_swqe - drop mr refs held by swqe582* @wqe: the send wqe583*584* This drops any mr references held by the swqe585*/586static inline void rvt_put_swqe(struct rvt_swqe *wqe)587{588int i;589590for (i = 0; i < wqe->wr.num_sge; i++) {591struct rvt_sge *sge = &wqe->sg_list[i];592593rvt_put_mr(sge->mr);594}595}596597/**598* rvt_qp_wqe_reserve - reserve operation599* @qp: the rvt qp600* @wqe: the send wqe601*602* This routine used in post send to record603* a wqe relative reserved operation use.604*/605static inline void rvt_qp_wqe_reserve(606struct rvt_qp *qp,607struct rvt_swqe *wqe)608{609atomic_inc(&qp->s_reserved_used);610}611612/**613* rvt_qp_wqe_unreserve - clean reserved operation614* @qp: the rvt qp615* @flags: send wqe flags616*617* This decrements the reserve use count.618*619* This call MUST precede the change to620* s_last to insure that post send sees a stable621* s_avail.622*623* An smp_mp__after_atomic() is used to insure624* the compiler does not juggle the order of the s_last625* ring index and the decrementing of s_reserved_used.626*/627static inline void rvt_qp_wqe_unreserve(struct rvt_qp *qp, int flags)628{629if (unlikely(flags & RVT_SEND_RESERVE_USED)) {630atomic_dec(&qp->s_reserved_used);631/* insure no compiler re-order up to s_last change */632smp_mb__after_atomic();633}634}635636extern const enum ib_wc_opcode ib_rvt_wc_opcode[];637638/*639* Compare the lower 24 bits of the msn values.640* Returns an integer <, ==, or > than zero.641*/642static inline int rvt_cmp_msn(u32 a, u32 b)643{644return (((int)a) - ((int)b)) << 8;645}646647__be32 rvt_compute_aeth(struct rvt_qp *qp);648649void rvt_get_credit(struct rvt_qp *qp, u32 aeth);650651u32 rvt_restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe, u32 len);652653/**654* rvt_div_round_up_mtu - round up divide655* @qp: the qp pair656* @len: the length657*658* Perform a shift based mtu round up divide659*/660static inline u32 rvt_div_round_up_mtu(struct rvt_qp *qp, u32 len)661{662return (len + qp->pmtu - 1) >> qp->log_pmtu;663}664665/**666* rvt_div_mtu - shift-based divide667* @qp: the qp pair668* @len: the length669*670* Perform a shift based mtu divide671*/672static inline u32 rvt_div_mtu(struct rvt_qp *qp, u32 len)673{674return len >> qp->log_pmtu;675}676677/**678* rvt_timeout_to_jiffies - Convert a ULP timeout input into jiffies679* @timeout: timeout input(0 - 31).680*681* Return a timeout value in jiffies.682*/683static inline unsigned long rvt_timeout_to_jiffies(u8 timeout)684{685if (timeout > 31)686timeout = 31;687688return usecs_to_jiffies(1U << timeout) * 4096UL / 1000UL;689}690691/**692* rvt_lookup_qpn - return the QP with the given QPN693* @rdi: rvt device info structure694* @rvp: the ibport695* @qpn: the QP number to look up696*697* The caller must hold the rcu_read_lock(), and keep the lock until698* the returned qp is no longer in use.699*/700static inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,701struct rvt_ibport *rvp,702u32 qpn) __must_hold(RCU)703{704struct rvt_qp *qp = NULL;705706if (unlikely(qpn <= 1)) {707qp = rcu_dereference(rvp->qp[qpn]);708} else {709u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);710711for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;712qp = rcu_dereference(qp->next))713if (qp->ibqp.qp_num == qpn)714break;715}716return qp;717}718719/**720* rvt_mod_retry_timer_ext - mod a retry timer721* @qp: the QP722* @shift: timeout shift to wait for multiple packets723* Modify a potentially already running retry timer724*/725static inline void rvt_mod_retry_timer_ext(struct rvt_qp *qp, u8 shift)726{727struct ib_qp *ibqp = &qp->ibqp;728struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);729730lockdep_assert_held(&qp->s_lock);731qp->s_flags |= RVT_S_TIMER;732/* 4.096 usec. * (1 << qp->timeout) */733mod_timer(&qp->s_timer, jiffies + rdi->busy_jiffies +734(qp->timeout_jiffies << shift));735}736737static inline void rvt_mod_retry_timer(struct rvt_qp *qp)738{739return rvt_mod_retry_timer_ext(qp, 0);740}741742/**743* rvt_put_qp_swqe - drop refs held by swqe744* @qp: the send qp745* @wqe: the send wqe746*747* This drops any references held by the swqe748*/749static inline void rvt_put_qp_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)750{751rvt_put_swqe(wqe);752if (qp->allowed_ops == IB_OPCODE_UD)753rdma_destroy_ah_attr(wqe->ud_wr.attr);754}755756/**757* rvt_qp_swqe_incr - increment ring index758* @qp: the qp759* @val: the starting value760*761* Return: the new value wrapping as appropriate762*/763static inline u32764rvt_qp_swqe_incr(struct rvt_qp *qp, u32 val)765{766if (++val >= qp->s_size)767val = 0;768return val;769}770771int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);772773/**774* rvt_recv_cq - add a new entry to completion queue775* by receive queue776* @qp: receive queue777* @wc: work completion entry to add778* @solicited: true if @entry is solicited779*780* This is wrapper function for rvt_enter_cq function call by781* receive queue. If rvt_cq_enter return false, it means cq is782* full and the qp is put into error state.783*/784static inline void rvt_recv_cq(struct rvt_qp *qp, struct ib_wc *wc,785bool solicited)786{787struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.recv_cq);788789if (unlikely(!rvt_cq_enter(cq, wc, solicited)))790rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);791}792793/**794* rvt_send_cq - add a new entry to completion queue795* by send queue796* @qp: send queue797* @wc: work completion entry to add798* @solicited: true if @entry is solicited799*800* This is wrapper function for rvt_enter_cq function call by801* send queue. If rvt_cq_enter return false, it means cq is802* full and the qp is put into error state.803*/804static inline void rvt_send_cq(struct rvt_qp *qp, struct ib_wc *wc,805bool solicited)806{807struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.send_cq);808809if (unlikely(!rvt_cq_enter(cq, wc, solicited)))810rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);811}812813/**814* rvt_qp_complete_swqe - insert send completion815* @qp: the qp816* @wqe: the send wqe817* @opcode: wc operation (driver dependent)818* @status: completion status819*820* Update the s_last information, and then insert a send821* completion into the completion822* queue if the qp indicates it should be done.823*824* See IBTA 10.7.3.1 for info on completion825* control.826*827* Return: new last828*/829static inline u32830rvt_qp_complete_swqe(struct rvt_qp *qp,831struct rvt_swqe *wqe,832enum ib_wc_opcode opcode,833enum ib_wc_status status)834{835bool need_completion;836u64 wr_id;837u32 byte_len, last;838int flags = wqe->wr.send_flags;839840rvt_qp_wqe_unreserve(qp, flags);841rvt_put_qp_swqe(qp, wqe);842843need_completion =844!(flags & RVT_SEND_RESERVE_USED) &&845(!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||846(flags & IB_SEND_SIGNALED) ||847status != IB_WC_SUCCESS);848if (need_completion) {849wr_id = wqe->wr.wr_id;850byte_len = wqe->length;851/* above fields required before writing s_last */852}853last = rvt_qp_swqe_incr(qp, qp->s_last);854/* see rvt_qp_is_avail() */855smp_store_release(&qp->s_last, last);856if (need_completion) {857struct ib_wc w = {858.wr_id = wr_id,859.status = status,860.opcode = opcode,861.qp = &qp->ibqp,862.byte_len = byte_len,863};864rvt_send_cq(qp, &w, status != IB_WC_SUCCESS);865}866return last;867}868869extern const int ib_rvt_state_ops[];870871struct rvt_dev_info;872int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only);873void rvt_comm_est(struct rvt_qp *qp);874void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err);875unsigned long rvt_rnr_tbl_to_usec(u32 index);876enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t);877void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth);878void rvt_del_timers_sync(struct rvt_qp *qp);879void rvt_stop_rc_timers(struct rvt_qp *qp);880void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift);881static inline void rvt_add_retry_timer(struct rvt_qp *qp)882{883rvt_add_retry_timer_ext(qp, 0);884}885886void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,887void *data, u32 length,888bool release, bool copy_last);889void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,890enum ib_wc_status status);891void rvt_ruc_loopback(struct rvt_qp *qp);892893/**894* struct rvt_qp_iter - the iterator for QPs895* @qp: the current QP896*897* This structure defines the current iterator898* state for sequenced access to all QPs relative899* to an rvt_dev_info.900*/901struct rvt_qp_iter {902struct rvt_qp *qp;903/* private: backpointer */904struct rvt_dev_info *rdi;905/* private: callback routine */906void (*cb)(struct rvt_qp *qp, u64 v);907/* private: for arg to callback routine */908u64 v;909/* private: number of SMI,GSI QPs for device */910int specials;911/* private: current iterator index */912int n;913};914915/**916* ib_cq_tail - Return tail index of cq buffer917* @send_cq: The cq for send918*919* This is called in qp_iter_print to get tail920* of cq buffer.921*/922static inline u32 ib_cq_tail(struct ib_cq *send_cq)923{924struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);925926return ibcq_to_rvtcq(send_cq)->ip ?927RDMA_READ_UAPI_ATOMIC(cq->queue->tail) :928ibcq_to_rvtcq(send_cq)->kqueue->tail;929}930931/**932* ib_cq_head - Return head index of cq buffer933* @send_cq: The cq for send934*935* This is called in qp_iter_print to get head936* of cq buffer.937*/938static inline u32 ib_cq_head(struct ib_cq *send_cq)939{940struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);941942return ibcq_to_rvtcq(send_cq)->ip ?943RDMA_READ_UAPI_ATOMIC(cq->queue->head) :944ibcq_to_rvtcq(send_cq)->kqueue->head;945}946947/**948* rvt_free_rq - free memory allocated for rvt_rq struct949* @rq: request queue data structure950*951* This function should only be called if the rvt_mmap_info()952* has not succeeded.953*/954static inline void rvt_free_rq(struct rvt_rq *rq)955{956kvfree(rq->kwq);957rq->kwq = NULL;958vfree(rq->wq);959rq->wq = NULL;960}961962/**963* rvt_to_iport - Get the ibport pointer964* @qp: the qp pointer965*966* This function returns the ibport pointer from the qp pointer.967*/968static inline struct rvt_ibport *rvt_to_iport(struct rvt_qp *qp)969{970struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);971972return rdi->ports[qp->port_num - 1];973}974975/**976* rvt_rc_credit_avail - Check if there are enough RC credits for the request977* @qp: the qp978* @wqe: the request979*980* This function returns false when there are not enough credits for the given981* request and true otherwise.982*/983static inline bool rvt_rc_credit_avail(struct rvt_qp *qp, struct rvt_swqe *wqe)984{985lockdep_assert_held(&qp->s_lock);986if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&987rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {988struct rvt_ibport *rvp = rvt_to_iport(qp);989990qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;991rvp->n_rc_crwaits++;992return false;993}994return true;995}996997struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,998u64 v,999void (*cb)(struct rvt_qp *qp, u64 v));1000int rvt_qp_iter_next(struct rvt_qp_iter *iter);1001void rvt_qp_iter(struct rvt_dev_info *rdi,1002u64 v,1003void (*cb)(struct rvt_qp *qp, u64 v));1004void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey);1005#endif /* DEF_RDMAVT_INCQP_H */100610071008