Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
0002 /*
0003  * Copyright(c) 2016 - 2020 Intel Corporation.
0004  */
0005 
0006 #ifndef DEF_RDMAVT_INCQP_H
0007 #define DEF_RDMAVT_INCQP_H
0008 
0009 #include <rdma/rdma_vt.h>
0010 #include <rdma/ib_pack.h>
0011 #include <rdma/ib_verbs.h>
0012 #include <rdma/rdmavt_cq.h>
0013 #include <rdma/rvt-abi.h>
0014 /*
0015  * Atomic bit definitions for r_aflags.
0016  */
0017 #define RVT_R_WRID_VALID        0
0018 #define RVT_R_REWIND_SGE        1
0019 
0020 /*
0021  * Bit definitions for r_flags.
0022  */
0023 #define RVT_R_REUSE_SGE 0x01
0024 #define RVT_R_RDMAR_SEQ 0x02
0025 #define RVT_R_RSP_NAK   0x04
0026 #define RVT_R_RSP_SEND  0x08
0027 #define RVT_R_COMM_EST  0x10
0028 
0029 /*
0030  * If a packet's QP[23:16] bits match this value, then it is
0031  * a PSM packet and the hardware will expect a KDETH header
0032  * following the BTH.
0033  */
0034 #define RVT_KDETH_QP_PREFIX       0x80
0035 #define RVT_KDETH_QP_SUFFIX       0xffff
0036 #define RVT_KDETH_QP_PREFIX_MASK  0x00ff0000
0037 #define RVT_KDETH_QP_PREFIX_SHIFT 16
0038 #define RVT_KDETH_QP_BASE         (u32)(RVT_KDETH_QP_PREFIX << \
0039                     RVT_KDETH_QP_PREFIX_SHIFT)
0040 #define RVT_KDETH_QP_MAX          (u32)(RVT_KDETH_QP_BASE + RVT_KDETH_QP_SUFFIX)
0041 
0042 /*
0043  * If a packet's LNH == BTH and DEST QPN[23:16] in the BTH match this
0044  * prefix value, then it is an AIP packet with a DETH containing the entropy
0045  * value in byte 4 following the BTH.
0046  */
0047 #define RVT_AIP_QP_PREFIX       0x81
0048 #define RVT_AIP_QP_SUFFIX       0xffff
0049 #define RVT_AIP_QP_PREFIX_MASK  0x00ff0000
0050 #define RVT_AIP_QP_PREFIX_SHIFT 16
0051 #define RVT_AIP_QP_BASE         (u32)(RVT_AIP_QP_PREFIX << \
0052                       RVT_AIP_QP_PREFIX_SHIFT)
0053 #define RVT_AIP_QPN_MAX         BIT(RVT_AIP_QP_PREFIX_SHIFT)
0054 #define RVT_AIP_QP_MAX          (u32)(RVT_AIP_QP_BASE + RVT_AIP_QPN_MAX - 1)
0055 
0056 /*
0057  * Bit definitions for s_flags.
0058  *
0059  * RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled
0060  * RVT_S_BUSY - send tasklet is processing the QP
0061  * RVT_S_TIMER - the RC retry timer is active
0062  * RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics
0063  * RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs
0064  *                         before processing the next SWQE
0065  * RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete
0066  *                         before processing the next SWQE
0067  * RVT_S_WAIT_RNR - waiting for RNR timeout
0068  * RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE
0069  * RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating
0070  *                  next send completion entry not via send DMA
0071  * RVT_S_WAIT_PIO - waiting for a send buffer to be available
0072  * RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available
0073  * RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available
0074  * RVT_S_WAIT_KMEM - waiting for kernel memory to be available
0075  * RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue
0076  * RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests
0077  * RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK
0078  * RVT_S_ECN - a BECN was queued to the send engine
0079  * RVT_S_MAX_BIT_MASK - The max bit that can be used by rdmavt
0080  */
0081 #define RVT_S_SIGNAL_REQ_WR 0x0001
0082 #define RVT_S_BUSY      0x0002
0083 #define RVT_S_TIMER     0x0004
0084 #define RVT_S_RESP_PENDING  0x0008
0085 #define RVT_S_ACK_PENDING   0x0010
0086 #define RVT_S_WAIT_FENCE    0x0020
0087 #define RVT_S_WAIT_RDMAR    0x0040
0088 #define RVT_S_WAIT_RNR      0x0080
0089 #define RVT_S_WAIT_SSN_CREDIT   0x0100
0090 #define RVT_S_WAIT_DMA      0x0200
0091 #define RVT_S_WAIT_PIO      0x0400
0092 #define RVT_S_WAIT_TX       0x0800
0093 #define RVT_S_WAIT_DMA_DESC 0x1000
0094 #define RVT_S_WAIT_KMEM     0x2000
0095 #define RVT_S_WAIT_PSN      0x4000
0096 #define RVT_S_WAIT_ACK      0x8000
0097 #define RVT_S_SEND_ONE      0x10000
0098 #define RVT_S_UNLIMITED_CREDIT  0x20000
0099 #define RVT_S_ECN       0x40000
0100 #define RVT_S_MAX_BIT_MASK  0x800000
0101 
0102 /*
0103  * Drivers should use s_flags starting with bit 31 down to the bit next to
0104  * RVT_S_MAX_BIT_MASK
0105  */
0106 
0107 /*
0108  * Wait flags that would prevent any packet type from being sent.
0109  */
0110 #define RVT_S_ANY_WAIT_IO \
0111     (RVT_S_WAIT_PIO | RVT_S_WAIT_TX | \
0112      RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)
0113 
0114 /*
0115  * Wait flags that would prevent send work requests from making progress.
0116  */
0117 #define RVT_S_ANY_WAIT_SEND (RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR | \
0118     RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \
0119     RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)
0120 
0121 #define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)
0122 
0123 /* Number of bits to pay attention to in the opcode for checking qp type */
0124 #define RVT_OPCODE_QP_MASK 0xE0
0125 
0126 /* Flags for checking QP state (see ib_rvt_state_ops[]) */
0127 #define RVT_POST_SEND_OK                0x01
0128 #define RVT_POST_RECV_OK                0x02
0129 #define RVT_PROCESS_RECV_OK             0x04
0130 #define RVT_PROCESS_SEND_OK             0x08
0131 #define RVT_PROCESS_NEXT_SEND_OK        0x10
0132 #define RVT_FLUSH_SEND          0x20
0133 #define RVT_FLUSH_RECV          0x40
0134 #define RVT_PROCESS_OR_FLUSH_SEND \
0135     (RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)
0136 #define RVT_SEND_OR_FLUSH_OR_RECV_OK \
0137     (RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND | RVT_PROCESS_RECV_OK)
0138 
0139 /*
0140  * Internal send flags
0141  */
0142 #define RVT_SEND_RESERVE_USED           IB_SEND_RESERVED_START
0143 #define RVT_SEND_COMPLETION_ONLY    (IB_SEND_RESERVED_START << 1)
0144 
0145 /**
0146  * rvt_ud_wr - IB UD work plus AH cache
0147  * @wr: valid IB work request
0148  * @attr: pointer to an allocated AH attribute
0149  *
0150  * Special case the UD WR so we can keep track of the AH attributes.
0151  *
0152  * NOTE: This data structure is stricly ordered wr then attr. I.e the attr
0153  * MUST come after wr.  The ib_ud_wr is sized and copied in rvt_post_one_wr.
0154  * The copy assumes that wr is first.
0155  */
0156 struct rvt_ud_wr {
0157     struct ib_ud_wr wr;
0158     struct rdma_ah_attr *attr;
0159 };
0160 
0161 /*
0162  * Send work request queue entry.
0163  * The size of the sg_list is determined when the QP is created and stored
0164  * in qp->s_max_sge.
0165  */
0166 struct rvt_swqe {
0167     union {
0168         struct ib_send_wr wr;   /* don't use wr.sg_list */
0169         struct rvt_ud_wr ud_wr;
0170         struct ib_reg_wr reg_wr;
0171         struct ib_rdma_wr rdma_wr;
0172         struct ib_atomic_wr atomic_wr;
0173     };
0174     u32 psn;                /* first packet sequence number */
0175     u32 lpsn;               /* last packet sequence number */
0176     u32 ssn;                /* send sequence number */
0177     u32 length;             /* total length of data in sg_list */
0178     void *priv;             /* driver dependent field */
0179     struct rvt_sge sg_list[];
0180 };
0181 
0182 /**
0183  * struct rvt_krwq - kernel struct receive work request
0184  * @p_lock: lock to protect producer of the kernel buffer
0185  * @head: index of next entry to fill
0186  * @c_lock:lock to protect consumer of the kernel buffer
0187  * @tail: index of next entry to pull
0188  * @count: count is aproximate of total receive enteries posted
0189  * @rvt_rwqe: struct of receive work request queue entry
0190  *
0191  * This structure is used to contain the head pointer,
0192  * tail pointer and receive work queue entries for kernel
0193  * mode user.
0194  */
0195 struct rvt_krwq {
0196     spinlock_t p_lock;  /* protect producer */
0197     u32 head;               /* new work requests posted to the head */
0198 
0199     /* protect consumer */
0200     spinlock_t c_lock ____cacheline_aligned_in_smp;
0201     u32 tail;               /* receives pull requests from here. */
0202     u32 count;      /* approx count of receive entries posted */
0203     struct rvt_rwqe *curr_wq;
0204     struct rvt_rwqe wq[];
0205 };
0206 
0207 /*
0208  * rvt_get_swqe_ah - Return the pointer to the struct rvt_ah
0209  * @swqe: valid Send WQE
0210  *
0211  */
0212 static inline struct rvt_ah *rvt_get_swqe_ah(struct rvt_swqe *swqe)
0213 {
0214     return ibah_to_rvtah(swqe->ud_wr.wr.ah);
0215 }
0216 
0217 /**
0218  * rvt_get_swqe_ah_attr - Return the cached ah attribute information
0219  * @swqe: valid Send WQE
0220  *
0221  */
0222 static inline struct rdma_ah_attr *rvt_get_swqe_ah_attr(struct rvt_swqe *swqe)
0223 {
0224     return swqe->ud_wr.attr;
0225 }
0226 
0227 /**
0228  * rvt_get_swqe_remote_qpn - Access the remote QPN value
0229  * @swqe: valid Send WQE
0230  *
0231  */
0232 static inline u32 rvt_get_swqe_remote_qpn(struct rvt_swqe *swqe)
0233 {
0234     return swqe->ud_wr.wr.remote_qpn;
0235 }
0236 
0237 /**
0238  * rvt_get_swqe_remote_qkey - Acces the remote qkey value
0239  * @swqe: valid Send WQE
0240  *
0241  */
0242 static inline u32 rvt_get_swqe_remote_qkey(struct rvt_swqe *swqe)
0243 {
0244     return swqe->ud_wr.wr.remote_qkey;
0245 }
0246 
0247 /**
0248  * rvt_get_swqe_pkey_index - Access the pkey index
0249  * @swqe: valid Send WQE
0250  *
0251  */
0252 static inline u16 rvt_get_swqe_pkey_index(struct rvt_swqe *swqe)
0253 {
0254     return swqe->ud_wr.wr.pkey_index;
0255 }
0256 
0257 struct rvt_rq {
0258     struct rvt_rwq *wq;
0259     struct rvt_krwq *kwq;
0260     u32 size;               /* size of RWQE array */
0261     u8 max_sge;
0262     /* protect changes in this struct */
0263     spinlock_t lock ____cacheline_aligned_in_smp;
0264 };
0265 
0266 /**
0267  * rvt_get_rq_count - count numbers of request work queue entries
0268  * in circular buffer
0269  * @rq: data structure for request queue entry
0270  * @head: head indices of the circular buffer
0271  * @tail: tail indices of the circular buffer
0272  *
0273  * Return - total number of entries in the Receive Queue
0274  */
0275 
0276 static inline u32 rvt_get_rq_count(struct rvt_rq *rq, u32 head, u32 tail)
0277 {
0278     u32 count = head - tail;
0279 
0280     if ((s32)count < 0)
0281         count += rq->size;
0282     return count;
0283 }
0284 
0285 /*
0286  * This structure holds the information that the send tasklet needs
0287  * to send a RDMA read response or atomic operation.
0288  */
0289 struct rvt_ack_entry {
0290     struct rvt_sge rdma_sge;
0291     u64 atomic_data;
0292     u32 psn;
0293     u32 lpsn;
0294     u8 opcode;
0295     u8 sent;
0296     void *priv;
0297 };
0298 
0299 #define RC_QP_SCALING_INTERVAL  5
0300 
0301 #define RVT_OPERATION_PRIV        0x00000001
0302 #define RVT_OPERATION_ATOMIC      0x00000002
0303 #define RVT_OPERATION_ATOMIC_SGE  0x00000004
0304 #define RVT_OPERATION_LOCAL       0x00000008
0305 #define RVT_OPERATION_USE_RESERVE 0x00000010
0306 #define RVT_OPERATION_IGN_RNR_CNT 0x00000020
0307 
0308 #define RVT_OPERATION_MAX (IB_WR_RESERVED10 + 1)
0309 
0310 /**
0311  * rvt_operation_params - op table entry
0312  * @length - the length to copy into the swqe entry
0313  * @qpt_support - a bit mask indicating QP type support
0314  * @flags - RVT_OPERATION flags (see above)
0315  *
0316  * This supports table driven post send so that
0317  * the driver can have differing an potentially
0318  * different sets of operations.
0319  *
0320  **/
0321 
0322 struct rvt_operation_params {
0323     size_t length;
0324     u32 qpt_support;
0325     u32 flags;
0326 };
0327 
0328 /*
0329  * Common variables are protected by both r_rq.lock and s_lock in that order
0330  * which only happens in modify_qp() or changing the QP 'state'.
0331  */
0332 struct rvt_qp {
0333     struct ib_qp ibqp;
0334     void *priv; /* Driver private data */
0335     /* read mostly fields above and below */
0336     struct rdma_ah_attr remote_ah_attr;
0337     struct rdma_ah_attr alt_ah_attr;
0338     struct rvt_qp __rcu *next;           /* link list for QPN hash table */
0339     struct rvt_swqe *s_wq;  /* send work queue */
0340     struct rvt_mmap_info *ip;
0341 
0342     unsigned long timeout_jiffies;  /* computed from timeout */
0343 
0344     int srate_mbps;     /* s_srate (below) converted to Mbit/s */
0345     pid_t pid;      /* pid for user mode QPs */
0346     u32 remote_qpn;
0347     u32 qkey;               /* QKEY for this QP (for UD or RD) */
0348     u32 s_size;             /* send work queue size */
0349 
0350     u16 pmtu;       /* decoded from path_mtu */
0351     u8 log_pmtu;        /* shift for pmtu */
0352     u8 state;               /* QP state */
0353     u8 allowed_ops;     /* high order bits of allowed opcodes */
0354     u8 qp_access_flags;
0355     u8 alt_timeout;         /* Alternate path timeout for this QP */
0356     u8 timeout;             /* Timeout for this QP */
0357     u8 s_srate;
0358     u8 s_mig_state;
0359     u8 port_num;
0360     u8 s_pkey_index;        /* PKEY index to use */
0361     u8 s_alt_pkey_index;    /* Alternate path PKEY index to use */
0362     u8 r_max_rd_atomic;     /* max number of RDMA read/atomic to receive */
0363     u8 s_max_rd_atomic;     /* max number of RDMA read/atomic to send */
0364     u8 s_retry_cnt;         /* number of times to retry */
0365     u8 s_rnr_retry_cnt;
0366     u8 r_min_rnr_timer;     /* retry timeout value for RNR NAKs */
0367     u8 s_max_sge;           /* size of s_wq->sg_list */
0368     u8 s_draining;
0369 
0370     /* start of read/write fields */
0371     atomic_t refcount ____cacheline_aligned_in_smp;
0372     wait_queue_head_t wait;
0373 
0374     struct rvt_ack_entry *s_ack_queue;
0375     struct rvt_sge_state s_rdma_read_sge;
0376 
0377     spinlock_t r_lock ____cacheline_aligned_in_smp;      /* used for APM */
0378     u32 r_psn;              /* expected rcv packet sequence number */
0379     unsigned long r_aflags;
0380     u64 r_wr_id;            /* ID for current receive WQE */
0381     u32 r_ack_psn;          /* PSN for next ACK or atomic ACK */
0382     u32 r_len;              /* total length of r_sge */
0383     u32 r_rcv_len;          /* receive data len processed */
0384     u32 r_msn;              /* message sequence number */
0385 
0386     u8 r_state;             /* opcode of last packet received */
0387     u8 r_flags;
0388     u8 r_head_ack_queue;    /* index into s_ack_queue[] */
0389     u8 r_adefered;          /* defered ack count */
0390 
0391     struct list_head rspwait;       /* link for waiting to respond */
0392 
0393     struct rvt_sge_state r_sge;     /* current receive data */
0394     struct rvt_rq r_rq;             /* receive work queue */
0395 
0396     /* post send line */
0397     spinlock_t s_hlock ____cacheline_aligned_in_smp;
0398     u32 s_head;             /* new entries added here */
0399     u32 s_next_psn;         /* PSN for next request */
0400     u32 s_avail;            /* number of entries avail */
0401     u32 s_ssn;              /* SSN of tail entry */
0402     atomic_t s_reserved_used; /* reserved entries in use */
0403 
0404     spinlock_t s_lock ____cacheline_aligned_in_smp;
0405     u32 s_flags;
0406     struct rvt_sge_state *s_cur_sge;
0407     struct rvt_swqe *s_wqe;
0408     struct rvt_sge_state s_sge;     /* current send request data */
0409     struct rvt_mregion *s_rdma_mr;
0410     u32 s_len;              /* total length of s_sge */
0411     u32 s_rdma_read_len;    /* total length of s_rdma_read_sge */
0412     u32 s_last_psn;         /* last response PSN processed */
0413     u32 s_sending_psn;      /* lowest PSN that is being sent */
0414     u32 s_sending_hpsn;     /* highest PSN that is being sent */
0415     u32 s_psn;              /* current packet sequence number */
0416     u32 s_ack_rdma_psn;     /* PSN for sending RDMA read responses */
0417     u32 s_ack_psn;          /* PSN for acking sends and RDMA writes */
0418     u32 s_tail;             /* next entry to process */
0419     u32 s_cur;              /* current work queue entry */
0420     u32 s_acked;            /* last un-ACK'ed entry */
0421     u32 s_last;             /* last completed entry */
0422     u32 s_lsn;              /* limit sequence number (credit) */
0423     u32 s_ahgpsn;           /* set to the psn in the copy of the header */
0424     u16 s_cur_size;         /* size of send packet in bytes */
0425     u16 s_rdma_ack_cnt;
0426     u8 s_hdrwords;         /* size of s_hdr in 32 bit words */
0427     s8 s_ahgidx;
0428     u8 s_state;             /* opcode of last packet sent */
0429     u8 s_ack_state;         /* opcode of packet to ACK */
0430     u8 s_nak_state;         /* non-zero if NAK is pending */
0431     u8 r_nak_state;         /* non-zero if NAK is pending */
0432     u8 s_retry;             /* requester retry counter */
0433     u8 s_rnr_retry;         /* requester RNR retry counter */
0434     u8 s_num_rd_atomic;     /* number of RDMA read/atomic pending */
0435     u8 s_tail_ack_queue;    /* index into s_ack_queue[] */
0436     u8 s_acked_ack_queue;   /* index into s_ack_queue[] */
0437 
0438     struct rvt_sge_state s_ack_rdma_sge;
0439     struct timer_list s_timer;
0440     struct hrtimer s_rnr_timer;
0441 
0442     atomic_t local_ops_pending; /* number of fast_reg/local_inv reqs */
0443 
0444     /*
0445      * This sge list MUST be last. Do not add anything below here.
0446      */
0447     struct rvt_sge *r_sg_list /* verified SGEs */
0448         ____cacheline_aligned_in_smp;
0449 };
0450 
0451 struct rvt_srq {
0452     struct ib_srq ibsrq;
0453     struct rvt_rq rq;
0454     struct rvt_mmap_info *ip;
0455     /* send signal when number of RWQEs < limit */
0456     u32 limit;
0457 };
0458 
0459 static inline struct rvt_srq *ibsrq_to_rvtsrq(struct ib_srq *ibsrq)
0460 {
0461     return container_of(ibsrq, struct rvt_srq, ibsrq);
0462 }
0463 
0464 static inline struct rvt_qp *ibqp_to_rvtqp(struct ib_qp *ibqp)
0465 {
0466     return container_of(ibqp, struct rvt_qp, ibqp);
0467 }
0468 
0469 #define RVT_QPN_MAX                 BIT(24)
0470 #define RVT_QPNMAP_ENTRIES          (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)
0471 #define RVT_BITS_PER_PAGE           (PAGE_SIZE * BITS_PER_BYTE)
0472 #define RVT_BITS_PER_PAGE_MASK      (RVT_BITS_PER_PAGE - 1)
0473 #define RVT_QPN_MASK            IB_QPN_MASK
0474 
0475 /*
0476  * QPN-map pages start out as NULL, they get allocated upon
0477  * first use and are never deallocated. This way,
0478  * large bitmaps are not allocated unless large numbers of QPs are used.
0479  */
0480 struct rvt_qpn_map {
0481     void *page;
0482 };
0483 
0484 struct rvt_qpn_table {
0485     spinlock_t lock; /* protect changes to the qp table */
0486     unsigned flags;         /* flags for QP0/1 allocated for each port */
0487     u32 last;               /* last QP number allocated */
0488     u32 nmaps;              /* size of the map table */
0489     u16 limit;
0490     u8  incr;
0491     /* bit map of free QP numbers other than 0/1 */
0492     struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];
0493 };
0494 
0495 struct rvt_qp_ibdev {
0496     u32 qp_table_size;
0497     u32 qp_table_bits;
0498     struct rvt_qp __rcu **qp_table;
0499     spinlock_t qpt_lock; /* qptable lock */
0500     struct rvt_qpn_table qpn_table;
0501 };
0502 
0503 /*
0504  * There is one struct rvt_mcast for each multicast GID.
0505  * All attached QPs are then stored as a list of
0506  * struct rvt_mcast_qp.
0507  */
0508 struct rvt_mcast_qp {
0509     struct list_head list;
0510     struct rvt_qp *qp;
0511 };
0512 
0513 struct rvt_mcast_addr {
0514     union ib_gid mgid;
0515     u16 lid;
0516 };
0517 
0518 struct rvt_mcast {
0519     struct rb_node rb_node;
0520     struct rvt_mcast_addr mcast_addr;
0521     struct list_head qp_list;
0522     wait_queue_head_t wait;
0523     atomic_t refcount;
0524     int n_attached;
0525 };
0526 
0527 /*
0528  * Since struct rvt_swqe is not a fixed size, we can't simply index into
0529  * struct rvt_qp.s_wq.  This function does the array index computation.
0530  */
0531 static inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,
0532                         unsigned n)
0533 {
0534     return (struct rvt_swqe *)((char *)qp->s_wq +
0535                      (sizeof(struct rvt_swqe) +
0536                       qp->s_max_sge *
0537                       sizeof(struct rvt_sge)) * n);
0538 }
0539 
0540 /*
0541  * Since struct rvt_rwqe is not a fixed size, we can't simply index into
0542  * struct rvt_rwq.wq.  This function does the array index computation.
0543  */
0544 static inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)
0545 {
0546     return (struct rvt_rwqe *)
0547         ((char *)rq->kwq->curr_wq +
0548          (sizeof(struct rvt_rwqe) +
0549           rq->max_sge * sizeof(struct ib_sge)) * n);
0550 }
0551 
0552 /**
0553  * rvt_is_user_qp - return if this is user mode QP
0554  * @qp - the target QP
0555  */
0556 static inline bool rvt_is_user_qp(struct rvt_qp *qp)
0557 {
0558     return !!qp->pid;
0559 }
0560 
0561 /**
0562  * rvt_get_qp - get a QP reference
0563  * @qp - the QP to hold
0564  */
0565 static inline void rvt_get_qp(struct rvt_qp *qp)
0566 {
0567     atomic_inc(&qp->refcount);
0568 }
0569 
0570 /**
0571  * rvt_put_qp - release a QP reference
0572  * @qp - the QP to release
0573  */
0574 static inline void rvt_put_qp(struct rvt_qp *qp)
0575 {
0576     if (qp && atomic_dec_and_test(&qp->refcount))
0577         wake_up(&qp->wait);
0578 }
0579 
0580 /**
0581  * rvt_put_swqe - drop mr refs held by swqe
0582  * @wqe - the send wqe
0583  *
0584  * This drops any mr references held by the swqe
0585  */
0586 static inline void rvt_put_swqe(struct rvt_swqe *wqe)
0587 {
0588     int i;
0589 
0590     for (i = 0; i < wqe->wr.num_sge; i++) {
0591         struct rvt_sge *sge = &wqe->sg_list[i];
0592 
0593         rvt_put_mr(sge->mr);
0594     }
0595 }
0596 
0597 /**
0598  * rvt_qp_wqe_reserve - reserve operation
0599  * @qp - the rvt qp
0600  * @wqe - the send wqe
0601  *
0602  * This routine used in post send to record
0603  * a wqe relative reserved operation use.
0604  */
0605 static inline void rvt_qp_wqe_reserve(
0606     struct rvt_qp *qp,
0607     struct rvt_swqe *wqe)
0608 {
0609     atomic_inc(&qp->s_reserved_used);
0610 }
0611 
0612 /**
0613  * rvt_qp_wqe_unreserve - clean reserved operation
0614  * @qp - the rvt qp
0615  * @flags - send wqe flags
0616  *
0617  * This decrements the reserve use count.
0618  *
0619  * This call MUST precede the change to
0620  * s_last to insure that post send sees a stable
0621  * s_avail.
0622  *
0623  * An smp_mp__after_atomic() is used to insure
0624  * the compiler does not juggle the order of the s_last
0625  * ring index and the decrementing of s_reserved_used.
0626  */
0627 static inline void rvt_qp_wqe_unreserve(struct rvt_qp *qp, int flags)
0628 {
0629     if (unlikely(flags & RVT_SEND_RESERVE_USED)) {
0630         atomic_dec(&qp->s_reserved_used);
0631         /* insure no compiler re-order up to s_last change */
0632         smp_mb__after_atomic();
0633     }
0634 }
0635 
0636 extern const enum ib_wc_opcode ib_rvt_wc_opcode[];
0637 
0638 /*
0639  * Compare the lower 24 bits of the msn values.
0640  * Returns an integer <, ==, or > than zero.
0641  */
0642 static inline int rvt_cmp_msn(u32 a, u32 b)
0643 {
0644     return (((int)a) - ((int)b)) << 8;
0645 }
0646 
0647 __be32 rvt_compute_aeth(struct rvt_qp *qp);
0648 
0649 void rvt_get_credit(struct rvt_qp *qp, u32 aeth);
0650 
0651 u32 rvt_restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe, u32 len);
0652 
0653 /**
0654  * rvt_div_round_up_mtu - round up divide
0655  * @qp - the qp pair
0656  * @len - the length
0657  *
0658  * Perform a shift based mtu round up divide
0659  */
0660 static inline u32 rvt_div_round_up_mtu(struct rvt_qp *qp, u32 len)
0661 {
0662     return (len + qp->pmtu - 1) >> qp->log_pmtu;
0663 }
0664 
0665 /**
0666  * @qp - the qp pair
0667  * @len - the length
0668  *
0669  * Perform a shift based mtu divide
0670  */
0671 static inline u32 rvt_div_mtu(struct rvt_qp *qp, u32 len)
0672 {
0673     return len >> qp->log_pmtu;
0674 }
0675 
0676 /**
0677  * rvt_timeout_to_jiffies - Convert a ULP timeout input into jiffies
0678  * @timeout - timeout input(0 - 31).
0679  *
0680  * Return a timeout value in jiffies.
0681  */
0682 static inline unsigned long rvt_timeout_to_jiffies(u8 timeout)
0683 {
0684     if (timeout > 31)
0685         timeout = 31;
0686 
0687     return usecs_to_jiffies(1U << timeout) * 4096UL / 1000UL;
0688 }
0689 
0690 /**
0691  * rvt_lookup_qpn - return the QP with the given QPN
0692  * @ibp: the ibport
0693  * @qpn: the QP number to look up
0694  *
0695  * The caller must hold the rcu_read_lock(), and keep the lock until
0696  * the returned qp is no longer in use.
0697  */
0698 static inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,
0699                         struct rvt_ibport *rvp,
0700                         u32 qpn) __must_hold(RCU)
0701 {
0702     struct rvt_qp *qp = NULL;
0703 
0704     if (unlikely(qpn <= 1)) {
0705         qp = rcu_dereference(rvp->qp[qpn]);
0706     } else {
0707         u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);
0708 
0709         for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;
0710             qp = rcu_dereference(qp->next))
0711             if (qp->ibqp.qp_num == qpn)
0712                 break;
0713     }
0714     return qp;
0715 }
0716 
0717 /**
0718  * rvt_mod_retry_timer - mod a retry timer
0719  * @qp - the QP
0720  * @shift - timeout shift to wait for multiple packets
0721  * Modify a potentially already running retry timer
0722  */
0723 static inline void rvt_mod_retry_timer_ext(struct rvt_qp *qp, u8 shift)
0724 {
0725     struct ib_qp *ibqp = &qp->ibqp;
0726     struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
0727 
0728     lockdep_assert_held(&qp->s_lock);
0729     qp->s_flags |= RVT_S_TIMER;
0730     /* 4.096 usec. * (1 << qp->timeout) */
0731     mod_timer(&qp->s_timer, jiffies + rdi->busy_jiffies +
0732           (qp->timeout_jiffies << shift));
0733 }
0734 
0735 static inline void rvt_mod_retry_timer(struct rvt_qp *qp)
0736 {
0737     return rvt_mod_retry_timer_ext(qp, 0);
0738 }
0739 
0740 /**
0741  * rvt_put_qp_swqe - drop refs held by swqe
0742  * @qp: the send qp
0743  * @wqe: the send wqe
0744  *
0745  * This drops any references held by the swqe
0746  */
0747 static inline void rvt_put_qp_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
0748 {
0749     rvt_put_swqe(wqe);
0750     if (qp->allowed_ops == IB_OPCODE_UD)
0751         rdma_destroy_ah_attr(wqe->ud_wr.attr);
0752 }
0753 
0754 /**
0755  * rvt_qp_sqwe_incr - increment ring index
0756  * @qp: the qp
0757  * @val: the starting value
0758  *
0759  * Return: the new value wrapping as appropriate
0760  */
0761 static inline u32
0762 rvt_qp_swqe_incr(struct rvt_qp *qp, u32 val)
0763 {
0764     if (++val >= qp->s_size)
0765         val = 0;
0766     return val;
0767 }
0768 
0769 int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);
0770 
0771 /**
0772  * rvt_recv_cq - add a new entry to completion queue
0773  *          by receive queue
0774  * @qp: receive queue
0775  * @wc: work completion entry to add
0776  * @solicited: true if @entry is solicited
0777  *
0778  * This is wrapper function for rvt_enter_cq function call by
0779  * receive queue. If rvt_cq_enter return false, it means cq is
0780  * full and the qp is put into error state.
0781  */
0782 static inline void rvt_recv_cq(struct rvt_qp *qp, struct ib_wc *wc,
0783                    bool solicited)
0784 {
0785     struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.recv_cq);
0786 
0787     if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
0788         rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
0789 }
0790 
0791 /**
0792  * rvt_send_cq - add a new entry to completion queue
0793  *                        by send queue
0794  * @qp: send queue
0795  * @wc: work completion entry to add
0796  * @solicited: true if @entry is solicited
0797  *
0798  * This is wrapper function for rvt_enter_cq function call by
0799  * send queue. If rvt_cq_enter return false, it means cq is
0800  * full and the qp is put into error state.
0801  */
0802 static inline void rvt_send_cq(struct rvt_qp *qp, struct ib_wc *wc,
0803                    bool solicited)
0804 {
0805     struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.send_cq);
0806 
0807     if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
0808         rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
0809 }
0810 
0811 /**
0812  * rvt_qp_complete_swqe - insert send completion
0813  * @qp - the qp
0814  * @wqe - the send wqe
0815  * @opcode - wc operation (driver dependent)
0816  * @status - completion status
0817  *
0818  * Update the s_last information, and then insert a send
0819  * completion into the completion
0820  * queue if the qp indicates it should be done.
0821  *
0822  * See IBTA 10.7.3.1 for info on completion
0823  * control.
0824  *
0825  * Return: new last
0826  */
0827 static inline u32
0828 rvt_qp_complete_swqe(struct rvt_qp *qp,
0829              struct rvt_swqe *wqe,
0830              enum ib_wc_opcode opcode,
0831              enum ib_wc_status status)
0832 {
0833     bool need_completion;
0834     u64 wr_id;
0835     u32 byte_len, last;
0836     int flags = wqe->wr.send_flags;
0837 
0838     rvt_qp_wqe_unreserve(qp, flags);
0839     rvt_put_qp_swqe(qp, wqe);
0840 
0841     need_completion =
0842         !(flags & RVT_SEND_RESERVE_USED) &&
0843         (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
0844         (flags & IB_SEND_SIGNALED) ||
0845         status != IB_WC_SUCCESS);
0846     if (need_completion) {
0847         wr_id = wqe->wr.wr_id;
0848         byte_len = wqe->length;
0849         /* above fields required before writing s_last */
0850     }
0851     last = rvt_qp_swqe_incr(qp, qp->s_last);
0852     /* see rvt_qp_is_avail() */
0853     smp_store_release(&qp->s_last, last);
0854     if (need_completion) {
0855         struct ib_wc w = {
0856             .wr_id = wr_id,
0857             .status = status,
0858             .opcode = opcode,
0859             .qp = &qp->ibqp,
0860             .byte_len = byte_len,
0861         };
0862         rvt_send_cq(qp, &w, status != IB_WC_SUCCESS);
0863     }
0864     return last;
0865 }
0866 
0867 extern const int  ib_rvt_state_ops[];
0868 
0869 struct rvt_dev_info;
0870 int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only);
0871 void rvt_comm_est(struct rvt_qp *qp);
0872 void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err);
0873 unsigned long rvt_rnr_tbl_to_usec(u32 index);
0874 enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t);
0875 void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth);
0876 void rvt_del_timers_sync(struct rvt_qp *qp);
0877 void rvt_stop_rc_timers(struct rvt_qp *qp);
0878 void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift);
0879 static inline void rvt_add_retry_timer(struct rvt_qp *qp)
0880 {
0881     rvt_add_retry_timer_ext(qp, 0);
0882 }
0883 
0884 void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
0885           void *data, u32 length,
0886           bool release, bool copy_last);
0887 void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
0888                enum ib_wc_status status);
0889 void rvt_ruc_loopback(struct rvt_qp *qp);
0890 
0891 /**
0892  * struct rvt_qp_iter - the iterator for QPs
0893  * @qp - the current QP
0894  *
0895  * This structure defines the current iterator
0896  * state for sequenced access to all QPs relative
0897  * to an rvt_dev_info.
0898  */
0899 struct rvt_qp_iter {
0900     struct rvt_qp *qp;
0901     /* private: backpointer */
0902     struct rvt_dev_info *rdi;
0903     /* private: callback routine */
0904     void (*cb)(struct rvt_qp *qp, u64 v);
0905     /* private: for arg to callback routine */
0906     u64 v;
0907     /* private: number of SMI,GSI QPs for device */
0908     int specials;
0909     /* private: current iterator index */
0910     int n;
0911 };
0912 
0913 /**
0914  * ib_cq_tail - Return tail index of cq buffer
0915  * @send_cq - The cq for send
0916  *
0917  * This is called in qp_iter_print to get tail
0918  * of cq buffer.
0919  */
0920 static inline u32 ib_cq_tail(struct ib_cq *send_cq)
0921 {
0922     struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
0923 
0924     return ibcq_to_rvtcq(send_cq)->ip ?
0925            RDMA_READ_UAPI_ATOMIC(cq->queue->tail) :
0926            ibcq_to_rvtcq(send_cq)->kqueue->tail;
0927 }
0928 
0929 /**
0930  * ib_cq_head - Return head index of cq buffer
0931  * @send_cq - The cq for send
0932  *
0933  * This is called in qp_iter_print to get head
0934  * of cq buffer.
0935  */
0936 static inline u32 ib_cq_head(struct ib_cq *send_cq)
0937 {
0938     struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
0939 
0940     return ibcq_to_rvtcq(send_cq)->ip ?
0941            RDMA_READ_UAPI_ATOMIC(cq->queue->head) :
0942            ibcq_to_rvtcq(send_cq)->kqueue->head;
0943 }
0944 
0945 /**
0946  * rvt_free_rq - free memory allocated for rvt_rq struct
0947  * @rvt_rq: request queue data structure
0948  *
0949  * This function should only be called if the rvt_mmap_info()
0950  * has not succeeded.
0951  */
0952 static inline void rvt_free_rq(struct rvt_rq *rq)
0953 {
0954     kvfree(rq->kwq);
0955     rq->kwq = NULL;
0956     vfree(rq->wq);
0957     rq->wq = NULL;
0958 }
0959 
0960 /**
0961  * rvt_to_iport - Get the ibport pointer
0962  * @qp: the qp pointer
0963  *
0964  * This function returns the ibport pointer from the qp pointer.
0965  */
0966 static inline struct rvt_ibport *rvt_to_iport(struct rvt_qp *qp)
0967 {
0968     struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
0969 
0970     return rdi->ports[qp->port_num - 1];
0971 }
0972 
0973 /**
0974  * rvt_rc_credit_avail - Check if there are enough RC credits for the request
0975  * @qp: the qp
0976  * @wqe: the request
0977  *
0978  * This function returns false when there are not enough credits for the given
0979  * request and true otherwise.
0980  */
0981 static inline bool rvt_rc_credit_avail(struct rvt_qp *qp, struct rvt_swqe *wqe)
0982 {
0983     lockdep_assert_held(&qp->s_lock);
0984     if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
0985         rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
0986         struct rvt_ibport *rvp = rvt_to_iport(qp);
0987 
0988         qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
0989         rvp->n_rc_crwaits++;
0990         return false;
0991     }
0992     return true;
0993 }
0994 
0995 struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
0996                      u64 v,
0997                      void (*cb)(struct rvt_qp *qp, u64 v));
0998 int rvt_qp_iter_next(struct rvt_qp_iter *iter);
0999 void rvt_qp_iter(struct rvt_dev_info *rdi,
1000          u64 v,
1001          void (*cb)(struct rvt_qp *qp, u64 v));
1002 void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey);
1003 #endif          /* DEF_RDMAVT_INCQP_H */