#ifndef DEF_RDMAVT_INCQP_H1#define DEF_RDMAVT_INCQP_H23/*-4* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.05*6* Copyright(c) 2016 Intel Corporation.7*8* This file is provided under a dual BSD/GPLv2 license. When using or9* redistributing this file, you may do so under either license.10*11* GPL LICENSE SUMMARY12*13* This program is free software; you can redistribute it and/or modify14* it under the terms of version 2 of the GNU General Public License as15* published by the Free Software Foundation.16*17* This program is distributed in the hope that it will be useful, but18* WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU20* General Public License for more details.21*22* BSD LICENSE23*24* Redistribution and use in source and binary forms, with or without25* modification, are permitted provided that the following conditions26* are met:27*28* - Redistributions of source code must retain the above copyright29* notice, this list of conditions and the following disclaimer.30* - Redistributions in binary form must reproduce the above copyright31* notice, this list of conditions and the following disclaimer in32* the documentation and/or other materials provided with the33* distribution.34* - Neither the name of Intel Corporation nor the names of its35* contributors may be used to endorse or promote products derived36* from this software without specific prior written permission.37*38* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS39* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT40* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR41* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT42* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,43* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT44* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,45* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY46* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT47* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE48* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.49*/5051#include <rdma/rdma_vt.h>52#include <rdma/ib_pack.h>53#include <rdma/ib_verbs.h>54/*55* Atomic bit definitions for r_aflags.56*/57#define RVT_R_WRID_VALID 058#define RVT_R_REWIND_SGE 15960/*61* Bit definitions for r_flags.62*/63#define RVT_R_REUSE_SGE 0x0164#define RVT_R_RDMAR_SEQ 0x0265#define RVT_R_RSP_NAK 0x0466#define RVT_R_RSP_SEND 0x0867#define RVT_R_COMM_EST 0x106869/*70* Bit definitions for s_flags.71*72* RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled73* RVT_S_BUSY - send tasklet is processing the QP74* RVT_S_TIMER - the RC retry timer is active75* RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics76* RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs77* before processing the next SWQE78* RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete79* before processing the next SWQE80* RVT_S_WAIT_RNR - waiting for RNR timeout81* RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE82* RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating83* next send completion entry not via send DMA84* RVT_S_WAIT_PIO - waiting for a send buffer to be available85* RVT_S_WAIT_PIO_DRAIN - waiting for a qp to drain pio packets86* RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available87* RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available88* RVT_S_WAIT_KMEM - waiting for kernel memory to be available89* RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue90* RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests91* RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK92* RVT_S_ECN - a BECN was queued to the send engine93*/94#define RVT_S_SIGNAL_REQ_WR 0x000195#define RVT_S_BUSY 0x000296#define RVT_S_TIMER 0x000497#define RVT_S_RESP_PENDING 0x000898#define RVT_S_ACK_PENDING 0x001099#define RVT_S_WAIT_FENCE 0x0020100#define RVT_S_WAIT_RDMAR 0x0040101#define RVT_S_WAIT_RNR 0x0080102#define RVT_S_WAIT_SSN_CREDIT 0x0100103#define RVT_S_WAIT_DMA 0x0200104#define RVT_S_WAIT_PIO 0x0400105#define RVT_S_WAIT_PIO_DRAIN 0x0800106#define RVT_S_WAIT_TX 0x1000107#define RVT_S_WAIT_DMA_DESC 0x2000108#define RVT_S_WAIT_KMEM 0x4000109#define RVT_S_WAIT_PSN 0x8000110#define RVT_S_WAIT_ACK 0x10000111#define RVT_S_SEND_ONE 0x20000112#define RVT_S_UNLIMITED_CREDIT 0x40000113#define RVT_S_AHG_VALID 0x80000114#define RVT_S_AHG_CLEAR 0x100000115#define RVT_S_ECN 0x200000116117/*118* Wait flags that would prevent any packet type from being sent.119*/120#define RVT_S_ANY_WAIT_IO \121(RVT_S_WAIT_PIO | RVT_S_WAIT_PIO_DRAIN | RVT_S_WAIT_TX | \122RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)123124/*125* Wait flags that would prevent send work requests from making progress.126*/127#define RVT_S_ANY_WAIT_SEND (RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR | \128RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \129RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)130131#define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)132133/* Number of bits to pay attention to in the opcode for checking qp type */134#define RVT_OPCODE_QP_MASK 0xE0135136/* Flags for checking QP state (see ib_rvt_state_ops[]) */137#define RVT_POST_SEND_OK 0x01138#define RVT_POST_RECV_OK 0x02139#define RVT_PROCESS_RECV_OK 0x04140#define RVT_PROCESS_SEND_OK 0x08141#define RVT_PROCESS_NEXT_SEND_OK 0x10142#define RVT_FLUSH_SEND 0x20143#define RVT_FLUSH_RECV 0x40144#define RVT_PROCESS_OR_FLUSH_SEND \145(RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)146147/*148* Internal send flags149*/150#define RVT_SEND_RESERVE_USED IB_SEND_RESERVED_START151#define RVT_SEND_COMPLETION_ONLY (IB_SEND_RESERVED_START << 1)152153/*154* Send work request queue entry.155* The size of the sg_list is determined when the QP is created and stored156* in qp->s_max_sge.157*/158struct rvt_swqe {159union {160struct ib_send_wr wr; /* don't use wr.sg_list */161struct ib_ud_wr ud_wr;162struct ib_reg_wr reg_wr;163struct ib_rdma_wr rdma_wr;164struct ib_atomic_wr atomic_wr;165};166u32 psn; /* first packet sequence number */167u32 lpsn; /* last packet sequence number */168u32 ssn; /* send sequence number */169u32 length; /* total length of data in sg_list */170struct rvt_sge sg_list[0];171};172173/*174* Receive work request queue entry.175* The size of the sg_list is determined when the QP (or SRQ) is created176* and stored in qp->r_rq.max_sge (or srq->rq.max_sge).177*/178struct rvt_rwqe {179u64 wr_id;180u8 num_sge;181struct ib_sge sg_list[0];182};183184/*185* This structure is used to contain the head pointer, tail pointer,186* and receive work queue entries as a single memory allocation so187* it can be mmap'ed into user space.188* Note that the wq array elements are variable size so you can't189* just index into the array to get the N'th element;190* use get_rwqe_ptr() instead.191*/192struct rvt_rwq {193u32 head; /* new work requests posted to the head */194u32 tail; /* receives pull requests from here. */195struct rvt_rwqe wq[0];196};197198struct rvt_rq {199struct rvt_rwq *wq;200u32 size; /* size of RWQE array */201u8 max_sge;202/* protect changes in this struct */203spinlock_t lock ____cacheline_aligned_in_smp;204};205206/*207* This structure is used by rvt_mmap() to validate an offset208* when an mmap() request is made. The vm_area_struct then uses209* this as its vm_private_data.210*/211struct rvt_mmap_info {212struct list_head pending_mmaps;213struct ib_ucontext *context;214void *obj;215__u64 offset;216struct kref ref;217unsigned size;218};219220/*221* This structure holds the information that the send tasklet needs222* to send a RDMA read response or atomic operation.223*/224struct rvt_ack_entry {225struct rvt_sge rdma_sge;226u64 atomic_data;227u32 psn;228u32 lpsn;229u8 opcode;230u8 sent;231};232233#define RC_QP_SCALING_INTERVAL 5234235#define RVT_OPERATION_PRIV 0x00000001236#define RVT_OPERATION_ATOMIC 0x00000002237#define RVT_OPERATION_ATOMIC_SGE 0x00000004238#define RVT_OPERATION_LOCAL 0x00000008239#define RVT_OPERATION_USE_RESERVE 0x00000010240241#define RVT_OPERATION_MAX (IB_WR_RESERVED10 + 1)242243/**244* rvt_operation_params - op table entry245* @length - the length to copy into the swqe entry246* @qpt_support - a bit mask indicating QP type support247* @flags - RVT_OPERATION flags (see above)248*249* This supports table driven post send so that250* the driver can have differing an potentially251* different sets of operations.252*253**/254255struct rvt_operation_params {256size_t length;257u32 qpt_support;258u32 flags;259};260261/*262* Common variables are protected by both r_rq.lock and s_lock in that order263* which only happens in modify_qp() or changing the QP 'state'.264*/265struct rvt_qp {266struct ib_qp ibqp;267void *priv; /* Driver private data */268/* read mostly fields above and below */269struct ib_ah_attr remote_ah_attr;270struct ib_ah_attr alt_ah_attr;271struct rvt_qp __rcu *next; /* link list for QPN hash table */272struct rvt_swqe *s_wq; /* send work queue */273struct rvt_mmap_info *ip;274275unsigned long timeout_jiffies; /* computed from timeout */276277enum ib_mtu path_mtu;278int srate_mbps; /* s_srate (below) converted to Mbit/s */279pid_t pid; /* pid for user mode QPs */280u32 remote_qpn;281u32 qkey; /* QKEY for this QP (for UD or RD) */282u32 s_size; /* send work queue size */283u32 s_ahgpsn; /* set to the psn in the copy of the header */284285u16 pmtu; /* decoded from path_mtu */286u8 log_pmtu; /* shift for pmtu */287u8 state; /* QP state */288u8 allowed_ops; /* high order bits of allowed opcodes */289u8 qp_access_flags;290u8 alt_timeout; /* Alternate path timeout for this QP */291u8 timeout; /* Timeout for this QP */292u8 s_srate;293u8 s_mig_state;294u8 port_num;295u8 s_pkey_index; /* PKEY index to use */296u8 s_alt_pkey_index; /* Alternate path PKEY index to use */297u8 r_max_rd_atomic; /* max number of RDMA read/atomic to receive */298u8 s_max_rd_atomic; /* max number of RDMA read/atomic to send */299u8 s_retry_cnt; /* number of times to retry */300u8 s_rnr_retry_cnt;301u8 r_min_rnr_timer; /* retry timeout value for RNR NAKs */302u8 s_max_sge; /* size of s_wq->sg_list */303u8 s_draining;304305/* start of read/write fields */306atomic_t refcount ____cacheline_aligned_in_smp;307wait_queue_head_t wait;308309struct rvt_ack_entry *s_ack_queue;310struct rvt_sge_state s_rdma_read_sge;311312spinlock_t r_lock ____cacheline_aligned_in_smp; /* used for APM */313u32 r_psn; /* expected rcv packet sequence number */314unsigned long r_aflags;315u64 r_wr_id; /* ID for current receive WQE */316u32 r_ack_psn; /* PSN for next ACK or atomic ACK */317u32 r_len; /* total length of r_sge */318u32 r_rcv_len; /* receive data len processed */319u32 r_msn; /* message sequence number */320321u8 r_state; /* opcode of last packet received */322u8 r_flags;323u8 r_head_ack_queue; /* index into s_ack_queue[] */324325struct list_head rspwait; /* link for waiting to respond */326327struct rvt_sge_state r_sge; /* current receive data */328struct rvt_rq r_rq; /* receive work queue */329330/* post send line */331spinlock_t s_hlock ____cacheline_aligned_in_smp;332u32 s_head; /* new entries added here */333u32 s_next_psn; /* PSN for next request */334u32 s_avail; /* number of entries avail */335u32 s_ssn; /* SSN of tail entry */336atomic_t s_reserved_used; /* reserved entries in use */337338spinlock_t s_lock ____cacheline_aligned_in_smp;339u32 s_flags;340struct rvt_sge_state *s_cur_sge;341struct rvt_swqe *s_wqe;342struct rvt_sge_state s_sge; /* current send request data */343struct rvt_mregion *s_rdma_mr;344u32 s_cur_size; /* size of send packet in bytes */345u32 s_len; /* total length of s_sge */346u32 s_rdma_read_len; /* total length of s_rdma_read_sge */347u32 s_last_psn; /* last response PSN processed */348u32 s_sending_psn; /* lowest PSN that is being sent */349u32 s_sending_hpsn; /* highest PSN that is being sent */350u32 s_psn; /* current packet sequence number */351u32 s_ack_rdma_psn; /* PSN for sending RDMA read responses */352u32 s_ack_psn; /* PSN for acking sends and RDMA writes */353u32 s_tail; /* next entry to process */354u32 s_cur; /* current work queue entry */355u32 s_acked; /* last un-ACK'ed entry */356u32 s_last; /* last completed entry */357u32 s_lsn; /* limit sequence number (credit) */358u16 s_hdrwords; /* size of s_hdr in 32 bit words */359u16 s_rdma_ack_cnt;360s8 s_ahgidx;361u8 s_state; /* opcode of last packet sent */362u8 s_ack_state; /* opcode of packet to ACK */363u8 s_nak_state; /* non-zero if NAK is pending */364u8 r_nak_state; /* non-zero if NAK is pending */365u8 s_retry; /* requester retry counter */366u8 s_rnr_retry; /* requester RNR retry counter */367u8 s_num_rd_atomic; /* number of RDMA read/atomic pending */368u8 s_tail_ack_queue; /* index into s_ack_queue[] */369370struct rvt_sge_state s_ack_rdma_sge;371struct timer_list s_timer;372373atomic_t local_ops_pending; /* number of fast_reg/local_inv reqs */374375/*376* This sge list MUST be last. Do not add anything below here.377*/378struct rvt_sge r_sg_list[0] /* verified SGEs */379____cacheline_aligned_in_smp;380};381382struct rvt_srq {383struct ib_srq ibsrq;384struct rvt_rq rq;385struct rvt_mmap_info *ip;386/* send signal when number of RWQEs < limit */387u32 limit;388};389390#define RVT_QPN_MAX BIT(24)391#define RVT_QPNMAP_ENTRIES (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)392#define RVT_BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE)393#define RVT_BITS_PER_PAGE_MASK (RVT_BITS_PER_PAGE - 1)394#define RVT_QPN_MASK 0xFFFFFF395396/*397* QPN-map pages start out as NULL, they get allocated upon398* first use and are never deallocated. This way,399* large bitmaps are not allocated unless large numbers of QPs are used.400*/401struct rvt_qpn_map {402void *page;403};404405struct rvt_qpn_table {406spinlock_t lock; /* protect changes to the qp table */407unsigned flags; /* flags for QP0/1 allocated for each port */408u32 last; /* last QP number allocated */409u32 nmaps; /* size of the map table */410u16 limit;411u8 incr;412/* bit map of free QP numbers other than 0/1 */413struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];414};415416struct rvt_qp_ibdev {417u32 qp_table_size;418u32 qp_table_bits;419struct rvt_qp __rcu **qp_table;420spinlock_t qpt_lock; /* qptable lock */421struct rvt_qpn_table qpn_table;422};423424/*425* There is one struct rvt_mcast for each multicast GID.426* All attached QPs are then stored as a list of427* struct rvt_mcast_qp.428*/429struct rvt_mcast_qp {430struct list_head list;431struct rvt_qp *qp;432};433434struct rvt_mcast {435struct rb_node rb_node;436union ib_gid mgid;437struct list_head qp_list;438wait_queue_head_t wait;439atomic_t refcount;440int n_attached;441};442443/*444* Since struct rvt_swqe is not a fixed size, we can't simply index into445* struct rvt_qp.s_wq. This function does the array index computation.446*/447static inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,448unsigned n)449{450return (struct rvt_swqe *)((char *)qp->s_wq +451(sizeof(struct rvt_swqe) +452qp->s_max_sge *453sizeof(struct rvt_sge)) * n);454}455456/*457* Since struct rvt_rwqe is not a fixed size, we can't simply index into458* struct rvt_rwq.wq. This function does the array index computation.459*/460static inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)461{462return (struct rvt_rwqe *)463((char *)rq->wq->wq +464(sizeof(struct rvt_rwqe) +465rq->max_sge * sizeof(struct ib_sge)) * n);466}467468/**469* rvt_get_qp - get a QP reference470* @qp - the QP to hold471*/472static inline void rvt_get_qp(struct rvt_qp *qp)473{474atomic_inc(&qp->refcount);475}476477/**478* rvt_put_qp - release a QP reference479* @qp - the QP to release480*/481static inline void rvt_put_qp(struct rvt_qp *qp)482{483if (qp && atomic_dec_and_test(&qp->refcount))484wake_up(&qp->wait);485}486487/**488* rvt_qp_wqe_reserve - reserve operation489* @qp - the rvt qp490* @wqe - the send wqe491*492* This routine used in post send to record493* a wqe relative reserved operation use.494*/495static inline void rvt_qp_wqe_reserve(496struct rvt_qp *qp,497struct rvt_swqe *wqe)498{499wqe->wr.send_flags |= RVT_SEND_RESERVE_USED;500atomic_inc(&qp->s_reserved_used);501}502503/**504* rvt_qp_wqe_unreserve - clean reserved operation505* @qp - the rvt qp506* @wqe - the send wqe507*508* This decrements the reserve use count.509*510* This call MUST precede the change to511* s_last to insure that post send sees a stable512* s_avail.513*514* An smp_mp__after_atomic() is used to insure515* the compiler does not juggle the order of the s_last516* ring index and the decrementing of s_reserved_used.517*/518static inline void rvt_qp_wqe_unreserve(519struct rvt_qp *qp,520struct rvt_swqe *wqe)521{522if (unlikely(wqe->wr.send_flags & RVT_SEND_RESERVE_USED)) {523wqe->wr.send_flags &= ~RVT_SEND_RESERVE_USED;524atomic_dec(&qp->s_reserved_used);525/* insure no compiler re-order up to s_last change */526smp_mb__after_atomic();527}528}529530extern const int ib_rvt_state_ops[];531532struct rvt_dev_info;533int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);534535#endif /* DEF_RDMAVT_INCQP_H */536537538