rdmavt_qp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #ifndef DEF_RDMAVT_INCQP_H
  2. #define DEF_RDMAVT_INCQP_H
  3. /*
  4. * Copyright(c) 2015 Intel Corporation.
  5. *
  6. * This file is provided under a dual BSD/GPLv2 license. When using or
  7. * redistributing this file, you may do so under either license.
  8. *
  9. * GPL LICENSE SUMMARY
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * - Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * - Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * - Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. #include <rdma/rdma_vt.h>
  50. #include <rdma/ib_pack.h>
  51. /*
  52. * Atomic bit definitions for r_aflags.
  53. */
  54. #define RVT_R_WRID_VALID 0
  55. #define RVT_R_REWIND_SGE 1
  56. /*
  57. * Bit definitions for r_flags.
  58. */
  59. #define RVT_R_REUSE_SGE 0x01
  60. #define RVT_R_RDMAR_SEQ 0x02
  61. #define RVT_R_RSP_NAK 0x04
  62. #define RVT_R_RSP_SEND 0x08
  63. #define RVT_R_COMM_EST 0x10
  64. /*
  65. * Bit definitions for s_flags.
  66. *
  67. * RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled
  68. * RVT_S_BUSY - send tasklet is processing the QP
  69. * RVT_S_TIMER - the RC retry timer is active
  70. * RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics
  71. * RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs
  72. * before processing the next SWQE
  73. * RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete
  74. * before processing the next SWQE
  75. * RVT_S_WAIT_RNR - waiting for RNR timeout
  76. * RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE
  77. * RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating
  78. * next send completion entry not via send DMA
  79. * RVT_S_WAIT_PIO - waiting for a send buffer to be available
  80. * RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available
  81. * RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available
  82. * RVT_S_WAIT_KMEM - waiting for kernel memory to be available
  83. * RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue
  84. * RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests
  85. * RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK
  86. * RVT_S_ECN - a BECN was queued to the send engine
  87. */
  88. #define RVT_S_SIGNAL_REQ_WR 0x0001
  89. #define RVT_S_BUSY 0x0002
  90. #define RVT_S_TIMER 0x0004
  91. #define RVT_S_RESP_PENDING 0x0008
  92. #define RVT_S_ACK_PENDING 0x0010
  93. #define RVT_S_WAIT_FENCE 0x0020
  94. #define RVT_S_WAIT_RDMAR 0x0040
  95. #define RVT_S_WAIT_RNR 0x0080
  96. #define RVT_S_WAIT_SSN_CREDIT 0x0100
  97. #define RVT_S_WAIT_DMA 0x0200
  98. #define RVT_S_WAIT_PIO 0x0400
  99. #define RVT_S_WAIT_TX 0x0800
  100. #define RVT_S_WAIT_DMA_DESC 0x1000
  101. #define RVT_S_WAIT_KMEM 0x2000
  102. #define RVT_S_WAIT_PSN 0x4000
  103. #define RVT_S_WAIT_ACK 0x8000
  104. #define RVT_S_SEND_ONE 0x10000
  105. #define RVT_S_UNLIMITED_CREDIT 0x20000
  106. #define RVT_S_AHG_VALID 0x40000
  107. #define RVT_S_AHG_CLEAR 0x80000
  108. #define RVT_S_ECN 0x100000
  109. /*
  110. * Wait flags that would prevent any packet type from being sent.
  111. */
  112. #define RVT_S_ANY_WAIT_IO (RVT_S_WAIT_PIO | RVT_S_WAIT_TX | \
  113. RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)
  114. /*
  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 | \
  118. RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \
  119. RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)
  120. #define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)
  121. /* Number of bits to pay attention to in the opcode for checking qp type */
  122. #define RVT_OPCODE_QP_MASK 0xE0
  123. /* Flags for checking QP state (see ib_rvt_state_ops[]) */
  124. #define RVT_POST_SEND_OK 0x01
  125. #define RVT_POST_RECV_OK 0x02
  126. #define RVT_PROCESS_RECV_OK 0x04
  127. #define RVT_PROCESS_SEND_OK 0x08
  128. #define RVT_PROCESS_NEXT_SEND_OK 0x10
  129. #define RVT_FLUSH_SEND 0x20
  130. #define RVT_FLUSH_RECV 0x40
  131. #define RVT_PROCESS_OR_FLUSH_SEND \
  132. (RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)
  133. /*
  134. * Send work request queue entry.
  135. * The size of the sg_list is determined when the QP is created and stored
  136. * in qp->s_max_sge.
  137. */
  138. struct rvt_swqe {
  139. union {
  140. struct ib_send_wr wr; /* don't use wr.sg_list */
  141. struct ib_ud_wr ud_wr;
  142. struct ib_reg_wr reg_wr;
  143. struct ib_rdma_wr rdma_wr;
  144. struct ib_atomic_wr atomic_wr;
  145. };
  146. u32 psn; /* first packet sequence number */
  147. u32 lpsn; /* last packet sequence number */
  148. u32 ssn; /* send sequence number */
  149. u32 length; /* total length of data in sg_list */
  150. struct rvt_sge sg_list[0];
  151. };
  152. /*
  153. * Receive work request queue entry.
  154. * The size of the sg_list is determined when the QP (or SRQ) is created
  155. * and stored in qp->r_rq.max_sge (or srq->rq.max_sge).
  156. */
  157. struct rvt_rwqe {
  158. u64 wr_id;
  159. u8 num_sge;
  160. struct ib_sge sg_list[0];
  161. };
  162. /*
  163. * This structure is used to contain the head pointer, tail pointer,
  164. * and receive work queue entries as a single memory allocation so
  165. * it can be mmap'ed into user space.
  166. * Note that the wq array elements are variable size so you can't
  167. * just index into the array to get the N'th element;
  168. * use get_rwqe_ptr() instead.
  169. */
  170. struct rvt_rwq {
  171. u32 head; /* new work requests posted to the head */
  172. u32 tail; /* receives pull requests from here. */
  173. struct rvt_rwqe wq[0];
  174. };
  175. struct rvt_rq {
  176. struct rvt_rwq *wq;
  177. u32 size; /* size of RWQE array */
  178. u8 max_sge;
  179. /* protect changes in this struct */
  180. spinlock_t lock ____cacheline_aligned_in_smp;
  181. };
  182. /*
  183. * This structure is used by rvt_mmap() to validate an offset
  184. * when an mmap() request is made. The vm_area_struct then uses
  185. * this as its vm_private_data.
  186. */
  187. struct rvt_mmap_info {
  188. struct list_head pending_mmaps;
  189. struct ib_ucontext *context;
  190. void *obj;
  191. __u64 offset;
  192. struct kref ref;
  193. unsigned size;
  194. };
  195. #define RVT_MAX_RDMA_ATOMIC 16
  196. /*
  197. * This structure holds the information that the send tasklet needs
  198. * to send a RDMA read response or atomic operation.
  199. */
  200. struct rvt_ack_entry {
  201. u8 opcode;
  202. u8 sent;
  203. u32 psn;
  204. u32 lpsn;
  205. union {
  206. struct rvt_sge rdma_sge;
  207. u64 atomic_data;
  208. };
  209. };
  210. /*
  211. * Variables prefixed with s_ are for the requester (sender).
  212. * Variables prefixed with r_ are for the responder (receiver).
  213. * Variables prefixed with ack_ are for responder replies.
  214. *
  215. * Common variables are protected by both r_rq.lock and s_lock in that order
  216. * which only happens in modify_qp() or changing the QP 'state'.
  217. */
  218. struct rvt_qp {
  219. struct ib_qp ibqp;
  220. void *priv; /* Driver private data */
  221. /* read mostly fields above and below */
  222. struct ib_ah_attr remote_ah_attr;
  223. struct ib_ah_attr alt_ah_attr;
  224. struct rvt_qp __rcu *next; /* link list for QPN hash table */
  225. struct rvt_swqe *s_wq; /* send work queue */
  226. struct rvt_mmap_info *ip;
  227. unsigned long timeout_jiffies; /* computed from timeout */
  228. enum ib_mtu path_mtu;
  229. int srate_mbps; /* s_srate (below) converted to Mbit/s */
  230. u32 remote_qpn;
  231. u32 pmtu; /* decoded from path_mtu */
  232. u32 qkey; /* QKEY for this QP (for UD or RD) */
  233. u32 s_size; /* send work queue size */
  234. u32 s_rnr_timeout; /* number of milliseconds for RNR timeout */
  235. u32 s_ahgpsn; /* set to the psn in the copy of the header */
  236. u8 state; /* QP state */
  237. u8 allowed_ops; /* high order bits of allowed opcodes */
  238. u8 qp_access_flags;
  239. u8 alt_timeout; /* Alternate path timeout for this QP */
  240. u8 timeout; /* Timeout for this QP */
  241. u8 s_srate;
  242. u8 s_mig_state;
  243. u8 port_num;
  244. u8 s_pkey_index; /* PKEY index to use */
  245. u8 s_alt_pkey_index; /* Alternate path PKEY index to use */
  246. u8 r_max_rd_atomic; /* max number of RDMA read/atomic to receive */
  247. u8 s_max_rd_atomic; /* max number of RDMA read/atomic to send */
  248. u8 s_retry_cnt; /* number of times to retry */
  249. u8 s_rnr_retry_cnt;
  250. u8 r_min_rnr_timer; /* retry timeout value for RNR NAKs */
  251. u8 s_max_sge; /* size of s_wq->sg_list */
  252. u8 s_draining;
  253. /* start of read/write fields */
  254. atomic_t refcount ____cacheline_aligned_in_smp;
  255. wait_queue_head_t wait;
  256. struct rvt_ack_entry s_ack_queue[RVT_MAX_RDMA_ATOMIC + 1]
  257. ____cacheline_aligned_in_smp;
  258. struct rvt_sge_state s_rdma_read_sge;
  259. spinlock_t r_lock ____cacheline_aligned_in_smp; /* used for APM */
  260. unsigned long r_aflags;
  261. u64 r_wr_id; /* ID for current receive WQE */
  262. u32 r_ack_psn; /* PSN for next ACK or atomic ACK */
  263. u32 r_len; /* total length of r_sge */
  264. u32 r_rcv_len; /* receive data len processed */
  265. u32 r_psn; /* expected rcv packet sequence number */
  266. u32 r_msn; /* message sequence number */
  267. u8 r_state; /* opcode of last packet received */
  268. u8 r_flags;
  269. u8 r_head_ack_queue; /* index into s_ack_queue[] */
  270. struct list_head rspwait; /* link for waiting to respond */
  271. struct rvt_sge_state r_sge; /* current receive data */
  272. struct rvt_rq r_rq; /* receive work queue */
  273. spinlock_t s_lock ____cacheline_aligned_in_smp;
  274. struct rvt_sge_state *s_cur_sge;
  275. u32 s_flags;
  276. struct rvt_swqe *s_wqe;
  277. struct rvt_sge_state s_sge; /* current send request data */
  278. struct rvt_mregion *s_rdma_mr;
  279. struct sdma_engine *s_sde; /* current sde */
  280. u32 s_cur_size; /* size of send packet in bytes */
  281. u32 s_len; /* total length of s_sge */
  282. u32 s_rdma_read_len; /* total length of s_rdma_read_sge */
  283. u32 s_next_psn; /* PSN for next request */
  284. u32 s_last_psn; /* last response PSN processed */
  285. u32 s_sending_psn; /* lowest PSN that is being sent */
  286. u32 s_sending_hpsn; /* highest PSN that is being sent */
  287. u32 s_psn; /* current packet sequence number */
  288. u32 s_ack_rdma_psn; /* PSN for sending RDMA read responses */
  289. u32 s_ack_psn; /* PSN for acking sends and RDMA writes */
  290. u32 s_head; /* new entries added here */
  291. u32 s_tail; /* next entry to process */
  292. u32 s_cur; /* current work queue entry */
  293. u32 s_acked; /* last un-ACK'ed entry */
  294. u32 s_last; /* last completed entry */
  295. u32 s_ssn; /* SSN of tail entry */
  296. u32 s_lsn; /* limit sequence number (credit) */
  297. u16 s_hdrwords; /* size of s_hdr in 32 bit words */
  298. u16 s_rdma_ack_cnt;
  299. s8 s_ahgidx;
  300. u8 s_state; /* opcode of last packet sent */
  301. u8 s_ack_state; /* opcode of packet to ACK */
  302. u8 s_nak_state; /* non-zero if NAK is pending */
  303. u8 r_nak_state; /* non-zero if NAK is pending */
  304. u8 s_retry; /* requester retry counter */
  305. u8 s_rnr_retry; /* requester RNR retry counter */
  306. u8 s_num_rd_atomic; /* number of RDMA read/atomic pending */
  307. u8 s_tail_ack_queue; /* index into s_ack_queue[] */
  308. struct rvt_sge_state s_ack_rdma_sge;
  309. struct timer_list s_timer;
  310. /*
  311. * This sge list MUST be last. Do not add anything below here.
  312. */
  313. struct rvt_sge r_sg_list[0] /* verified SGEs */
  314. ____cacheline_aligned_in_smp;
  315. };
  316. struct rvt_srq {
  317. struct ib_srq ibsrq;
  318. struct rvt_rq rq;
  319. struct rvt_mmap_info *ip;
  320. /* send signal when number of RWQEs < limit */
  321. u32 limit;
  322. };
  323. #define RVT_QPN_MAX BIT(24)
  324. #define RVT_QPNMAP_ENTRIES (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)
  325. #define RVT_BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE)
  326. #define RVT_BITS_PER_PAGE_MASK (RVT_BITS_PER_PAGE - 1)
  327. #define RVT_QPN_MASK 0xFFFFFF
  328. /*
  329. * QPN-map pages start out as NULL, they get allocated upon
  330. * first use and are never deallocated. This way,
  331. * large bitmaps are not allocated unless large numbers of QPs are used.
  332. */
  333. struct rvt_qpn_map {
  334. void *page;
  335. };
  336. struct rvt_qpn_table {
  337. spinlock_t lock; /* protect changes to the qp table */
  338. unsigned flags; /* flags for QP0/1 allocated for each port */
  339. u32 last; /* last QP number allocated */
  340. u32 nmaps; /* size of the map table */
  341. u16 limit;
  342. u8 incr;
  343. /* bit map of free QP numbers other than 0/1 */
  344. struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];
  345. };
  346. struct rvt_qp_ibdev {
  347. u32 qp_table_size;
  348. u32 qp_table_bits;
  349. struct rvt_qp __rcu **qp_table;
  350. spinlock_t qpt_lock; /* qptable lock */
  351. struct rvt_qpn_table qpn_table;
  352. };
  353. /*
  354. * Since struct rvt_swqe is not a fixed size, we can't simply index into
  355. * struct hfi1_qp.s_wq. This function does the array index computation.
  356. */
  357. static inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,
  358. unsigned n)
  359. {
  360. return (struct rvt_swqe *)((char *)qp->s_wq +
  361. (sizeof(struct rvt_swqe) +
  362. qp->s_max_sge *
  363. sizeof(struct rvt_sge)) * n);
  364. }
  365. /*
  366. * Since struct rvt_rwqe is not a fixed size, we can't simply index into
  367. * struct rvt_rwq.wq. This function does the array index computation.
  368. */
  369. static inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)
  370. {
  371. return (struct rvt_rwqe *)
  372. ((char *)rq->wq->wq +
  373. (sizeof(struct rvt_rwqe) +
  374. rq->max_sge * sizeof(struct ib_sge)) * n);
  375. }
  376. extern const int ib_rvt_state_ops[];
  377. struct rvt_dev_info;
  378. void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp);
  379. void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends);
  380. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);
  381. void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn);
  382. void rvt_dec_qp_cnt(struct rvt_dev_info *rdi);
  383. #endif /* DEF_RDMAVT_INCQP_H */