GNU Linux-libre 5.4.241-gnu1
[releases.git] / include / rdma / rdmavt_qp.h
1 #ifndef DEF_RDMAVT_INCQP_H
2 #define DEF_RDMAVT_INCQP_H
3
4 /*
5  * Copyright(c) 2016 - 2019 Intel Corporation.
6  *
7  * This file is provided under a dual BSD/GPLv2 license.  When using or
8  * redistributing this file, you may do so under either license.
9  *
10  * GPL LICENSE SUMMARY
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * BSD LICENSE
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  *  - Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *  - Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in
31  *    the documentation and/or other materials provided with the
32  *    distribution.
33  *  - Neither the name of Intel Corporation nor the names of its
34  *    contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50
51 #include <rdma/rdma_vt.h>
52 #include <rdma/ib_pack.h>
53 #include <rdma/ib_verbs.h>
54 #include <rdma/rdmavt_cq.h>
55 #include <rdma/rvt-abi.h>
56 /*
57  * Atomic bit definitions for r_aflags.
58  */
59 #define RVT_R_WRID_VALID        0
60 #define RVT_R_REWIND_SGE        1
61
62 /*
63  * Bit definitions for r_flags.
64  */
65 #define RVT_R_REUSE_SGE 0x01
66 #define RVT_R_RDMAR_SEQ 0x02
67 #define RVT_R_RSP_NAK   0x04
68 #define RVT_R_RSP_SEND  0x08
69 #define RVT_R_COMM_EST  0x10
70
71 /*
72  * Bit definitions for s_flags.
73  *
74  * RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled
75  * RVT_S_BUSY - send tasklet is processing the QP
76  * RVT_S_TIMER - the RC retry timer is active
77  * RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics
78  * RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs
79  *                         before processing the next SWQE
80  * RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete
81  *                         before processing the next SWQE
82  * RVT_S_WAIT_RNR - waiting for RNR timeout
83  * RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE
84  * RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating
85  *                  next send completion entry not via send DMA
86  * RVT_S_WAIT_PIO - waiting for a send buffer to be available
87  * RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available
88  * RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available
89  * RVT_S_WAIT_KMEM - waiting for kernel memory to be available
90  * RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue
91  * RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests
92  * RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK
93  * RVT_S_ECN - a BECN was queued to the send engine
94  * RVT_S_MAX_BIT_MASK - The max bit that can be used by rdmavt
95  */
96 #define RVT_S_SIGNAL_REQ_WR     0x0001
97 #define RVT_S_BUSY              0x0002
98 #define RVT_S_TIMER             0x0004
99 #define RVT_S_RESP_PENDING      0x0008
100 #define RVT_S_ACK_PENDING       0x0010
101 #define RVT_S_WAIT_FENCE        0x0020
102 #define RVT_S_WAIT_RDMAR        0x0040
103 #define RVT_S_WAIT_RNR          0x0080
104 #define RVT_S_WAIT_SSN_CREDIT   0x0100
105 #define RVT_S_WAIT_DMA          0x0200
106 #define RVT_S_WAIT_PIO          0x0400
107 #define RVT_S_WAIT_TX           0x0800
108 #define RVT_S_WAIT_DMA_DESC     0x1000
109 #define RVT_S_WAIT_KMEM         0x2000
110 #define RVT_S_WAIT_PSN          0x4000
111 #define RVT_S_WAIT_ACK          0x8000
112 #define RVT_S_SEND_ONE          0x10000
113 #define RVT_S_UNLIMITED_CREDIT  0x20000
114 #define RVT_S_ECN               0x40000
115 #define RVT_S_MAX_BIT_MASK      0x800000
116
117 /*
118  * Drivers should use s_flags starting with bit 31 down to the bit next to
119  * RVT_S_MAX_BIT_MASK
120  */
121
122 /*
123  * Wait flags that would prevent any packet type from being sent.
124  */
125 #define RVT_S_ANY_WAIT_IO \
126         (RVT_S_WAIT_PIO | RVT_S_WAIT_TX | \
127          RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)
128
129 /*
130  * Wait flags that would prevent send work requests from making progress.
131  */
132 #define RVT_S_ANY_WAIT_SEND (RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR | \
133         RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \
134         RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)
135
136 #define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)
137
138 /* Number of bits to pay attention to in the opcode for checking qp type */
139 #define RVT_OPCODE_QP_MASK 0xE0
140
141 /* Flags for checking QP state (see ib_rvt_state_ops[]) */
142 #define RVT_POST_SEND_OK                0x01
143 #define RVT_POST_RECV_OK                0x02
144 #define RVT_PROCESS_RECV_OK             0x04
145 #define RVT_PROCESS_SEND_OK             0x08
146 #define RVT_PROCESS_NEXT_SEND_OK        0x10
147 #define RVT_FLUSH_SEND                  0x20
148 #define RVT_FLUSH_RECV                  0x40
149 #define RVT_PROCESS_OR_FLUSH_SEND \
150         (RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)
151 #define RVT_SEND_OR_FLUSH_OR_RECV_OK \
152         (RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND | RVT_PROCESS_RECV_OK)
153
154 /*
155  * Internal send flags
156  */
157 #define RVT_SEND_RESERVE_USED           IB_SEND_RESERVED_START
158 #define RVT_SEND_COMPLETION_ONLY        (IB_SEND_RESERVED_START << 1)
159
160 /**
161  * rvt_ud_wr - IB UD work plus AH cache
162  * @wr: valid IB work request
163  * @attr: pointer to an allocated AH attribute
164  *
165  * Special case the UD WR so we can keep track of the AH attributes.
166  *
167  * NOTE: This data structure is stricly ordered wr then attr. I.e the attr
168  * MUST come after wr.  The ib_ud_wr is sized and copied in rvt_post_one_wr.
169  * The copy assumes that wr is first.
170  */
171 struct rvt_ud_wr {
172         struct ib_ud_wr wr;
173         struct rdma_ah_attr *attr;
174 };
175
176 /*
177  * Send work request queue entry.
178  * The size of the sg_list is determined when the QP is created and stored
179  * in qp->s_max_sge.
180  */
181 struct rvt_swqe {
182         union {
183                 struct ib_send_wr wr;   /* don't use wr.sg_list */
184                 struct rvt_ud_wr ud_wr;
185                 struct ib_reg_wr reg_wr;
186                 struct ib_rdma_wr rdma_wr;
187                 struct ib_atomic_wr atomic_wr;
188         };
189         u32 psn;                /* first packet sequence number */
190         u32 lpsn;               /* last packet sequence number */
191         u32 ssn;                /* send sequence number */
192         u32 length;             /* total length of data in sg_list */
193         void *priv;             /* driver dependent field */
194         struct rvt_sge sg_list[0];
195 };
196
197 /**
198  * struct rvt_krwq - kernel struct receive work request
199  * @p_lock: lock to protect producer of the kernel buffer
200  * @head: index of next entry to fill
201  * @c_lock:lock to protect consumer of the kernel buffer
202  * @tail: index of next entry to pull
203  * @count: count is aproximate of total receive enteries posted
204  * @rvt_rwqe: struct of receive work request queue entry
205  *
206  * This structure is used to contain the head pointer,
207  * tail pointer and receive work queue entries for kernel
208  * mode user.
209  */
210 struct rvt_krwq {
211         spinlock_t p_lock;      /* protect producer */
212         u32 head;               /* new work requests posted to the head */
213
214         /* protect consumer */
215         spinlock_t c_lock ____cacheline_aligned_in_smp;
216         u32 tail;               /* receives pull requests from here. */
217         u32 count;              /* approx count of receive entries posted */
218         struct rvt_rwqe *curr_wq;
219         struct rvt_rwqe wq[];
220 };
221
222 /*
223  * rvt_get_swqe_ah - Return the pointer to the struct rvt_ah
224  * @swqe: valid Send WQE
225  *
226  */
227 static inline struct rvt_ah *rvt_get_swqe_ah(struct rvt_swqe *swqe)
228 {
229         return ibah_to_rvtah(swqe->ud_wr.wr.ah);
230 }
231
232 /**
233  * rvt_get_swqe_ah_attr - Return the cached ah attribute information
234  * @swqe: valid Send WQE
235  *
236  */
237 static inline struct rdma_ah_attr *rvt_get_swqe_ah_attr(struct rvt_swqe *swqe)
238 {
239         return swqe->ud_wr.attr;
240 }
241
242 /**
243  * rvt_get_swqe_remote_qpn - Access the remote QPN value
244  * @swqe: valid Send WQE
245  *
246  */
247 static inline u32 rvt_get_swqe_remote_qpn(struct rvt_swqe *swqe)
248 {
249         return swqe->ud_wr.wr.remote_qpn;
250 }
251
252 /**
253  * rvt_get_swqe_remote_qkey - Acces the remote qkey value
254  * @swqe: valid Send WQE
255  *
256  */
257 static inline u32 rvt_get_swqe_remote_qkey(struct rvt_swqe *swqe)
258 {
259         return swqe->ud_wr.wr.remote_qkey;
260 }
261
262 /**
263  * rvt_get_swqe_pkey_index - Access the pkey index
264  * @swqe: valid Send WQE
265  *
266  */
267 static inline u16 rvt_get_swqe_pkey_index(struct rvt_swqe *swqe)
268 {
269         return swqe->ud_wr.wr.pkey_index;
270 }
271
272 struct rvt_rq {
273         struct rvt_rwq *wq;
274         struct rvt_krwq *kwq;
275         u32 size;               /* size of RWQE array */
276         u8 max_sge;
277         /* protect changes in this struct */
278         spinlock_t lock ____cacheline_aligned_in_smp;
279 };
280
281 /**
282  * rvt_get_rq_count - count numbers of request work queue entries
283  * in circular buffer
284  * @rq: data structure for request queue entry
285  * @head: head indices of the circular buffer
286  * @tail: tail indices of the circular buffer
287  *
288  * Return - total number of entries in the Receive Queue
289  */
290
291 static inline u32 rvt_get_rq_count(struct rvt_rq *rq, u32 head, u32 tail)
292 {
293         u32 count = head - tail;
294
295         if ((s32)count < 0)
296                 count += rq->size;
297         return count;
298 }
299
300 /*
301  * This structure holds the information that the send tasklet needs
302  * to send a RDMA read response or atomic operation.
303  */
304 struct rvt_ack_entry {
305         struct rvt_sge rdma_sge;
306         u64 atomic_data;
307         u32 psn;
308         u32 lpsn;
309         u8 opcode;
310         u8 sent;
311         void *priv;
312 };
313
314 #define RC_QP_SCALING_INTERVAL  5
315
316 #define RVT_OPERATION_PRIV        0x00000001
317 #define RVT_OPERATION_ATOMIC      0x00000002
318 #define RVT_OPERATION_ATOMIC_SGE  0x00000004
319 #define RVT_OPERATION_LOCAL       0x00000008
320 #define RVT_OPERATION_USE_RESERVE 0x00000010
321 #define RVT_OPERATION_IGN_RNR_CNT 0x00000020
322
323 #define RVT_OPERATION_MAX (IB_WR_RESERVED10 + 1)
324
325 /**
326  * rvt_operation_params - op table entry
327  * @length - the length to copy into the swqe entry
328  * @qpt_support - a bit mask indicating QP type support
329  * @flags - RVT_OPERATION flags (see above)
330  *
331  * This supports table driven post send so that
332  * the driver can have differing an potentially
333  * different sets of operations.
334  *
335  **/
336
337 struct rvt_operation_params {
338         size_t length;
339         u32 qpt_support;
340         u32 flags;
341 };
342
343 /*
344  * Common variables are protected by both r_rq.lock and s_lock in that order
345  * which only happens in modify_qp() or changing the QP 'state'.
346  */
347 struct rvt_qp {
348         struct ib_qp ibqp;
349         void *priv; /* Driver private data */
350         /* read mostly fields above and below */
351         struct rdma_ah_attr remote_ah_attr;
352         struct rdma_ah_attr alt_ah_attr;
353         struct rvt_qp __rcu *next;           /* link list for QPN hash table */
354         struct rvt_swqe *s_wq;  /* send work queue */
355         struct rvt_mmap_info *ip;
356
357         unsigned long timeout_jiffies;  /* computed from timeout */
358
359         int srate_mbps;         /* s_srate (below) converted to Mbit/s */
360         pid_t pid;              /* pid for user mode QPs */
361         u32 remote_qpn;
362         u32 qkey;               /* QKEY for this QP (for UD or RD) */
363         u32 s_size;             /* send work queue size */
364
365         u16 pmtu;               /* decoded from path_mtu */
366         u8 log_pmtu;            /* shift for pmtu */
367         u8 state;               /* QP state */
368         u8 allowed_ops;         /* high order bits of allowed opcodes */
369         u8 qp_access_flags;
370         u8 alt_timeout;         /* Alternate path timeout for this QP */
371         u8 timeout;             /* Timeout for this QP */
372         u8 s_srate;
373         u8 s_mig_state;
374         u8 port_num;
375         u8 s_pkey_index;        /* PKEY index to use */
376         u8 s_alt_pkey_index;    /* Alternate path PKEY index to use */
377         u8 r_max_rd_atomic;     /* max number of RDMA read/atomic to receive */
378         u8 s_max_rd_atomic;     /* max number of RDMA read/atomic to send */
379         u8 s_retry_cnt;         /* number of times to retry */
380         u8 s_rnr_retry_cnt;
381         u8 r_min_rnr_timer;     /* retry timeout value for RNR NAKs */
382         u8 s_max_sge;           /* size of s_wq->sg_list */
383         u8 s_draining;
384
385         /* start of read/write fields */
386         atomic_t refcount ____cacheline_aligned_in_smp;
387         wait_queue_head_t wait;
388
389         struct rvt_ack_entry *s_ack_queue;
390         struct rvt_sge_state s_rdma_read_sge;
391
392         spinlock_t r_lock ____cacheline_aligned_in_smp;      /* used for APM */
393         u32 r_psn;              /* expected rcv packet sequence number */
394         unsigned long r_aflags;
395         u64 r_wr_id;            /* ID for current receive WQE */
396         u32 r_ack_psn;          /* PSN for next ACK or atomic ACK */
397         u32 r_len;              /* total length of r_sge */
398         u32 r_rcv_len;          /* receive data len processed */
399         u32 r_msn;              /* message sequence number */
400
401         u8 r_state;             /* opcode of last packet received */
402         u8 r_flags;
403         u8 r_head_ack_queue;    /* index into s_ack_queue[] */
404         u8 r_adefered;          /* defered ack count */
405
406         struct list_head rspwait;       /* link for waiting to respond */
407
408         struct rvt_sge_state r_sge;     /* current receive data */
409         struct rvt_rq r_rq;             /* receive work queue */
410
411         /* post send line */
412         spinlock_t s_hlock ____cacheline_aligned_in_smp;
413         u32 s_head;             /* new entries added here */
414         u32 s_next_psn;         /* PSN for next request */
415         u32 s_avail;            /* number of entries avail */
416         u32 s_ssn;              /* SSN of tail entry */
417         atomic_t s_reserved_used; /* reserved entries in use */
418
419         spinlock_t s_lock ____cacheline_aligned_in_smp;
420         u32 s_flags;
421         struct rvt_sge_state *s_cur_sge;
422         struct rvt_swqe *s_wqe;
423         struct rvt_sge_state s_sge;     /* current send request data */
424         struct rvt_mregion *s_rdma_mr;
425         u32 s_len;              /* total length of s_sge */
426         u32 s_rdma_read_len;    /* total length of s_rdma_read_sge */
427         u32 s_last_psn;         /* last response PSN processed */
428         u32 s_sending_psn;      /* lowest PSN that is being sent */
429         u32 s_sending_hpsn;     /* highest PSN that is being sent */
430         u32 s_psn;              /* current packet sequence number */
431         u32 s_ack_rdma_psn;     /* PSN for sending RDMA read responses */
432         u32 s_ack_psn;          /* PSN for acking sends and RDMA writes */
433         u32 s_tail;             /* next entry to process */
434         u32 s_cur;              /* current work queue entry */
435         u32 s_acked;            /* last un-ACK'ed entry */
436         u32 s_last;             /* last completed entry */
437         u32 s_lsn;              /* limit sequence number (credit) */
438         u32 s_ahgpsn;           /* set to the psn in the copy of the header */
439         u16 s_cur_size;         /* size of send packet in bytes */
440         u16 s_rdma_ack_cnt;
441         u8 s_hdrwords;         /* size of s_hdr in 32 bit words */
442         s8 s_ahgidx;
443         u8 s_state;             /* opcode of last packet sent */
444         u8 s_ack_state;         /* opcode of packet to ACK */
445         u8 s_nak_state;         /* non-zero if NAK is pending */
446         u8 r_nak_state;         /* non-zero if NAK is pending */
447         u8 s_retry;             /* requester retry counter */
448         u8 s_rnr_retry;         /* requester RNR retry counter */
449         u8 s_num_rd_atomic;     /* number of RDMA read/atomic pending */
450         u8 s_tail_ack_queue;    /* index into s_ack_queue[] */
451         u8 s_acked_ack_queue;   /* index into s_ack_queue[] */
452
453         struct rvt_sge_state s_ack_rdma_sge;
454         struct timer_list s_timer;
455         struct hrtimer s_rnr_timer;
456
457         atomic_t local_ops_pending; /* number of fast_reg/local_inv reqs */
458
459         /*
460          * This sge list MUST be last. Do not add anything below here.
461          */
462         struct rvt_sge r_sg_list[0] /* verified SGEs */
463                 ____cacheline_aligned_in_smp;
464 };
465
466 struct rvt_srq {
467         struct ib_srq ibsrq;
468         struct rvt_rq rq;
469         struct rvt_mmap_info *ip;
470         /* send signal when number of RWQEs < limit */
471         u32 limit;
472 };
473
474 static inline struct rvt_srq *ibsrq_to_rvtsrq(struct ib_srq *ibsrq)
475 {
476         return container_of(ibsrq, struct rvt_srq, ibsrq);
477 }
478
479 static inline struct rvt_qp *ibqp_to_rvtqp(struct ib_qp *ibqp)
480 {
481         return container_of(ibqp, struct rvt_qp, ibqp);
482 }
483
484 #define RVT_QPN_MAX                 BIT(24)
485 #define RVT_QPNMAP_ENTRIES          (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)
486 #define RVT_BITS_PER_PAGE           (PAGE_SIZE * BITS_PER_BYTE)
487 #define RVT_BITS_PER_PAGE_MASK      (RVT_BITS_PER_PAGE - 1)
488 #define RVT_QPN_MASK                IB_QPN_MASK
489
490 /*
491  * QPN-map pages start out as NULL, they get allocated upon
492  * first use and are never deallocated. This way,
493  * large bitmaps are not allocated unless large numbers of QPs are used.
494  */
495 struct rvt_qpn_map {
496         void *page;
497 };
498
499 struct rvt_qpn_table {
500         spinlock_t lock; /* protect changes to the qp table */
501         unsigned flags;         /* flags for QP0/1 allocated for each port */
502         u32 last;               /* last QP number allocated */
503         u32 nmaps;              /* size of the map table */
504         u16 limit;
505         u8  incr;
506         /* bit map of free QP numbers other than 0/1 */
507         struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];
508 };
509
510 struct rvt_qp_ibdev {
511         u32 qp_table_size;
512         u32 qp_table_bits;
513         struct rvt_qp __rcu **qp_table;
514         spinlock_t qpt_lock; /* qptable lock */
515         struct rvt_qpn_table qpn_table;
516 };
517
518 /*
519  * There is one struct rvt_mcast for each multicast GID.
520  * All attached QPs are then stored as a list of
521  * struct rvt_mcast_qp.
522  */
523 struct rvt_mcast_qp {
524         struct list_head list;
525         struct rvt_qp *qp;
526 };
527
528 struct rvt_mcast_addr {
529         union ib_gid mgid;
530         u16 lid;
531 };
532
533 struct rvt_mcast {
534         struct rb_node rb_node;
535         struct rvt_mcast_addr mcast_addr;
536         struct list_head qp_list;
537         wait_queue_head_t wait;
538         atomic_t refcount;
539         int n_attached;
540 };
541
542 /*
543  * Since struct rvt_swqe is not a fixed size, we can't simply index into
544  * struct rvt_qp.s_wq.  This function does the array index computation.
545  */
546 static inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,
547                                                 unsigned n)
548 {
549         return (struct rvt_swqe *)((char *)qp->s_wq +
550                                      (sizeof(struct rvt_swqe) +
551                                       qp->s_max_sge *
552                                       sizeof(struct rvt_sge)) * n);
553 }
554
555 /*
556  * Since struct rvt_rwqe is not a fixed size, we can't simply index into
557  * struct rvt_rwq.wq.  This function does the array index computation.
558  */
559 static inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)
560 {
561         return (struct rvt_rwqe *)
562                 ((char *)rq->kwq->curr_wq +
563                  (sizeof(struct rvt_rwqe) +
564                   rq->max_sge * sizeof(struct ib_sge)) * n);
565 }
566
567 /**
568  * rvt_is_user_qp - return if this is user mode QP
569  * @qp - the target QP
570  */
571 static inline bool rvt_is_user_qp(struct rvt_qp *qp)
572 {
573         return !!qp->pid;
574 }
575
576 /**
577  * rvt_get_qp - get a QP reference
578  * @qp - the QP to hold
579  */
580 static inline void rvt_get_qp(struct rvt_qp *qp)
581 {
582         atomic_inc(&qp->refcount);
583 }
584
585 /**
586  * rvt_put_qp - release a QP reference
587  * @qp - the QP to release
588  */
589 static inline void rvt_put_qp(struct rvt_qp *qp)
590 {
591         if (qp && atomic_dec_and_test(&qp->refcount))
592                 wake_up(&qp->wait);
593 }
594
595 /**
596  * rvt_put_swqe - drop mr refs held by swqe
597  * @wqe - the send wqe
598  *
599  * This drops any mr references held by the swqe
600  */
601 static inline void rvt_put_swqe(struct rvt_swqe *wqe)
602 {
603         int i;
604
605         for (i = 0; i < wqe->wr.num_sge; i++) {
606                 struct rvt_sge *sge = &wqe->sg_list[i];
607
608                 rvt_put_mr(sge->mr);
609         }
610 }
611
612 /**
613  * rvt_qp_wqe_reserve - reserve operation
614  * @qp - the rvt qp
615  * @wqe - the send wqe
616  *
617  * This routine used in post send to record
618  * a wqe relative reserved operation use.
619  */
620 static inline void rvt_qp_wqe_reserve(
621         struct rvt_qp *qp,
622         struct rvt_swqe *wqe)
623 {
624         atomic_inc(&qp->s_reserved_used);
625 }
626
627 /**
628  * rvt_qp_wqe_unreserve - clean reserved operation
629  * @qp - the rvt qp
630  * @flags - send wqe flags
631  *
632  * This decrements the reserve use count.
633  *
634  * This call MUST precede the change to
635  * s_last to insure that post send sees a stable
636  * s_avail.
637  *
638  * An smp_mp__after_atomic() is used to insure
639  * the compiler does not juggle the order of the s_last
640  * ring index and the decrementing of s_reserved_used.
641  */
642 static inline void rvt_qp_wqe_unreserve(struct rvt_qp *qp, int flags)
643 {
644         if (unlikely(flags & RVT_SEND_RESERVE_USED)) {
645                 atomic_dec(&qp->s_reserved_used);
646                 /* insure no compiler re-order up to s_last change */
647                 smp_mb__after_atomic();
648         }
649 }
650
651 extern const enum ib_wc_opcode ib_rvt_wc_opcode[];
652
653 /*
654  * Compare the lower 24 bits of the msn values.
655  * Returns an integer <, ==, or > than zero.
656  */
657 static inline int rvt_cmp_msn(u32 a, u32 b)
658 {
659         return (((int)a) - ((int)b)) << 8;
660 }
661
662 /**
663  * rvt_compute_aeth - compute the AETH (syndrome + MSN)
664  * @qp: the queue pair to compute the AETH for
665  *
666  * Returns the AETH.
667  */
668 __be32 rvt_compute_aeth(struct rvt_qp *qp);
669
670 /**
671  * rvt_get_credit - flush the send work queue of a QP
672  * @qp: the qp who's send work queue to flush
673  * @aeth: the Acknowledge Extended Transport Header
674  *
675  * The QP s_lock should be held.
676  */
677 void rvt_get_credit(struct rvt_qp *qp, u32 aeth);
678
679 /**
680  * rvt_restart_sge - rewind the sge state for a wqe
681  * @ss: the sge state pointer
682  * @wqe: the wqe to rewind
683  * @len: the data length from the start of the wqe in bytes
684  *
685  * Returns the remaining data length.
686  */
687 u32 rvt_restart_sge(struct rvt_sge_state *ss, struct rvt_swqe *wqe, u32 len);
688
689 /**
690  * @qp - the qp pair
691  * @len - the length
692  *
693  * Perform a shift based mtu round up divide
694  */
695 static inline u32 rvt_div_round_up_mtu(struct rvt_qp *qp, u32 len)
696 {
697         return (len + qp->pmtu - 1) >> qp->log_pmtu;
698 }
699
700 /**
701  * @qp - the qp pair
702  * @len - the length
703  *
704  * Perform a shift based mtu divide
705  */
706 static inline u32 rvt_div_mtu(struct rvt_qp *qp, u32 len)
707 {
708         return len >> qp->log_pmtu;
709 }
710
711 /**
712  * rvt_timeout_to_jiffies - Convert a ULP timeout input into jiffies
713  * @timeout - timeout input(0 - 31).
714  *
715  * Return a timeout value in jiffies.
716  */
717 static inline unsigned long rvt_timeout_to_jiffies(u8 timeout)
718 {
719         if (timeout > 31)
720                 timeout = 31;
721
722         return usecs_to_jiffies(1U << timeout) * 4096UL / 1000UL;
723 }
724
725 /**
726  * rvt_lookup_qpn - return the QP with the given QPN
727  * @ibp: the ibport
728  * @qpn: the QP number to look up
729  *
730  * The caller must hold the rcu_read_lock(), and keep the lock until
731  * the returned qp is no longer in use.
732  */
733 static inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,
734                                             struct rvt_ibport *rvp,
735                                             u32 qpn) __must_hold(RCU)
736 {
737         struct rvt_qp *qp = NULL;
738
739         if (unlikely(qpn <= 1)) {
740                 qp = rcu_dereference(rvp->qp[qpn]);
741         } else {
742                 u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);
743
744                 for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;
745                         qp = rcu_dereference(qp->next))
746                         if (qp->ibqp.qp_num == qpn)
747                                 break;
748         }
749         return qp;
750 }
751
752 /**
753  * rvt_mod_retry_timer - mod a retry timer
754  * @qp - the QP
755  * @shift - timeout shift to wait for multiple packets
756  * Modify a potentially already running retry timer
757  */
758 static inline void rvt_mod_retry_timer_ext(struct rvt_qp *qp, u8 shift)
759 {
760         struct ib_qp *ibqp = &qp->ibqp;
761         struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
762
763         lockdep_assert_held(&qp->s_lock);
764         qp->s_flags |= RVT_S_TIMER;
765         /* 4.096 usec. * (1 << qp->timeout) */
766         mod_timer(&qp->s_timer, jiffies + rdi->busy_jiffies +
767                   (qp->timeout_jiffies << shift));
768 }
769
770 static inline void rvt_mod_retry_timer(struct rvt_qp *qp)
771 {
772         return rvt_mod_retry_timer_ext(qp, 0);
773 }
774
775 /**
776  * rvt_put_qp_swqe - drop refs held by swqe
777  * @qp: the send qp
778  * @wqe: the send wqe
779  *
780  * This drops any references held by the swqe
781  */
782 static inline void rvt_put_qp_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
783 {
784         rvt_put_swqe(wqe);
785         if (qp->allowed_ops == IB_OPCODE_UD)
786                 rdma_destroy_ah_attr(wqe->ud_wr.attr);
787 }
788
789 /**
790  * rvt_qp_sqwe_incr - increment ring index
791  * @qp: the qp
792  * @val: the starting value
793  *
794  * Return: the new value wrapping as appropriate
795  */
796 static inline u32
797 rvt_qp_swqe_incr(struct rvt_qp *qp, u32 val)
798 {
799         if (++val >= qp->s_size)
800                 val = 0;
801         return val;
802 }
803
804 int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);
805
806 /**
807  * rvt_recv_cq - add a new entry to completion queue
808  *                      by receive queue
809  * @qp: receive queue
810  * @wc: work completion entry to add
811  * @solicited: true if @entry is solicited
812  *
813  * This is wrapper function for rvt_enter_cq function call by
814  * receive queue. If rvt_cq_enter return false, it means cq is
815  * full and the qp is put into error state.
816  */
817 static inline void rvt_recv_cq(struct rvt_qp *qp, struct ib_wc *wc,
818                                bool solicited)
819 {
820         struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.recv_cq);
821
822         if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
823                 rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
824 }
825
826 /**
827  * rvt_send_cq - add a new entry to completion queue
828  *                        by send queue
829  * @qp: send queue
830  * @wc: work completion entry to add
831  * @solicited: true if @entry is solicited
832  *
833  * This is wrapper function for rvt_enter_cq function call by
834  * send queue. If rvt_cq_enter return false, it means cq is
835  * full and the qp is put into error state.
836  */
837 static inline void rvt_send_cq(struct rvt_qp *qp, struct ib_wc *wc,
838                                bool solicited)
839 {
840         struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.send_cq);
841
842         if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
843                 rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
844 }
845
846 /**
847  * rvt_qp_complete_swqe - insert send completion
848  * @qp - the qp
849  * @wqe - the send wqe
850  * @opcode - wc operation (driver dependent)
851  * @status - completion status
852  *
853  * Update the s_last information, and then insert a send
854  * completion into the completion
855  * queue if the qp indicates it should be done.
856  *
857  * See IBTA 10.7.3.1 for info on completion
858  * control.
859  *
860  * Return: new last
861  */
862 static inline u32
863 rvt_qp_complete_swqe(struct rvt_qp *qp,
864                      struct rvt_swqe *wqe,
865                      enum ib_wc_opcode opcode,
866                      enum ib_wc_status status)
867 {
868         bool need_completion;
869         u64 wr_id;
870         u32 byte_len, last;
871         int flags = wqe->wr.send_flags;
872
873         rvt_qp_wqe_unreserve(qp, flags);
874         rvt_put_qp_swqe(qp, wqe);
875
876         need_completion =
877                 !(flags & RVT_SEND_RESERVE_USED) &&
878                 (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
879                 (flags & IB_SEND_SIGNALED) ||
880                 status != IB_WC_SUCCESS);
881         if (need_completion) {
882                 wr_id = wqe->wr.wr_id;
883                 byte_len = wqe->length;
884                 /* above fields required before writing s_last */
885         }
886         last = rvt_qp_swqe_incr(qp, qp->s_last);
887         /* see rvt_qp_is_avail() */
888         smp_store_release(&qp->s_last, last);
889         if (need_completion) {
890                 struct ib_wc w = {
891                         .wr_id = wr_id,
892                         .status = status,
893                         .opcode = opcode,
894                         .qp = &qp->ibqp,
895                         .byte_len = byte_len,
896                 };
897                 rvt_send_cq(qp, &w, status != IB_WC_SUCCESS);
898         }
899         return last;
900 }
901
902 extern const int  ib_rvt_state_ops[];
903
904 struct rvt_dev_info;
905 int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only);
906 void rvt_comm_est(struct rvt_qp *qp);
907 void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err);
908 unsigned long rvt_rnr_tbl_to_usec(u32 index);
909 enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t);
910 void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth);
911 void rvt_del_timers_sync(struct rvt_qp *qp);
912 void rvt_stop_rc_timers(struct rvt_qp *qp);
913 void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift);
914 static inline void rvt_add_retry_timer(struct rvt_qp *qp)
915 {
916         rvt_add_retry_timer_ext(qp, 0);
917 }
918
919 void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
920                   void *data, u32 length,
921                   bool release, bool copy_last);
922 void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
923                        enum ib_wc_status status);
924 void rvt_ruc_loopback(struct rvt_qp *qp);
925
926 /**
927  * struct rvt_qp_iter - the iterator for QPs
928  * @qp - the current QP
929  *
930  * This structure defines the current iterator
931  * state for sequenced access to all QPs relative
932  * to an rvt_dev_info.
933  */
934 struct rvt_qp_iter {
935         struct rvt_qp *qp;
936         /* private: backpointer */
937         struct rvt_dev_info *rdi;
938         /* private: callback routine */
939         void (*cb)(struct rvt_qp *qp, u64 v);
940         /* private: for arg to callback routine */
941         u64 v;
942         /* private: number of SMI,GSI QPs for device */
943         int specials;
944         /* private: current iterator index */
945         int n;
946 };
947
948 /**
949  * ib_cq_tail - Return tail index of cq buffer
950  * @send_cq - The cq for send
951  *
952  * This is called in qp_iter_print to get tail
953  * of cq buffer.
954  */
955 static inline u32 ib_cq_tail(struct ib_cq *send_cq)
956 {
957         struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
958
959         return ibcq_to_rvtcq(send_cq)->ip ?
960                RDMA_READ_UAPI_ATOMIC(cq->queue->tail) :
961                ibcq_to_rvtcq(send_cq)->kqueue->tail;
962 }
963
964 /**
965  * ib_cq_head - Return head index of cq buffer
966  * @send_cq - The cq for send
967  *
968  * This is called in qp_iter_print to get head
969  * of cq buffer.
970  */
971 static inline u32 ib_cq_head(struct ib_cq *send_cq)
972 {
973         struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
974
975         return ibcq_to_rvtcq(send_cq)->ip ?
976                RDMA_READ_UAPI_ATOMIC(cq->queue->head) :
977                ibcq_to_rvtcq(send_cq)->kqueue->head;
978 }
979
980 /**
981  * rvt_free_rq - free memory allocated for rvt_rq struct
982  * @rvt_rq: request queue data structure
983  *
984  * This function should only be called if the rvt_mmap_info()
985  * has not succeeded.
986  */
987 static inline void rvt_free_rq(struct rvt_rq *rq)
988 {
989         kvfree(rq->kwq);
990         rq->kwq = NULL;
991         vfree(rq->wq);
992         rq->wq = NULL;
993 }
994
995 /**
996  * rvt_to_iport - Get the ibport pointer
997  * @qp: the qp pointer
998  *
999  * This function returns the ibport pointer from the qp pointer.
1000  */
1001 static inline struct rvt_ibport *rvt_to_iport(struct rvt_qp *qp)
1002 {
1003         struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
1004
1005         return rdi->ports[qp->port_num - 1];
1006 }
1007
1008 /**
1009  * rvt_rc_credit_avail - Check if there are enough RC credits for the request
1010  * @qp: the qp
1011  * @wqe: the request
1012  *
1013  * This function returns false when there are not enough credits for the given
1014  * request and true otherwise.
1015  */
1016 static inline bool rvt_rc_credit_avail(struct rvt_qp *qp, struct rvt_swqe *wqe)
1017 {
1018         lockdep_assert_held(&qp->s_lock);
1019         if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
1020             rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
1021                 struct rvt_ibport *rvp = rvt_to_iport(qp);
1022
1023                 qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
1024                 rvp->n_rc_crwaits++;
1025                 return false;
1026         }
1027         return true;
1028 }
1029
1030 struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
1031                                      u64 v,
1032                                      void (*cb)(struct rvt_qp *qp, u64 v));
1033 int rvt_qp_iter_next(struct rvt_qp_iter *iter);
1034 void rvt_qp_iter(struct rvt_dev_info *rdi,
1035                  u64 v,
1036                  void (*cb)(struct rvt_qp *qp, u64 v));
1037 void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey);
1038 #endif          /* DEF_RDMAVT_INCQP_H */