rdmavt_qp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #ifndef DEF_RDMAVT_INCQP_H
  2. #define DEF_RDMAVT_INCQP_H
  3. /*
  4. * Copyright(c) 2016 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. #include <rdma/ib_verbs.h>
  52. /*
  53. * Atomic bit definitions for r_aflags.
  54. */
  55. #define RVT_R_WRID_VALID 0
  56. #define RVT_R_REWIND_SGE 1
  57. /*
  58. * Bit definitions for r_flags.
  59. */
  60. #define RVT_R_REUSE_SGE 0x01
  61. #define RVT_R_RDMAR_SEQ 0x02
  62. #define RVT_R_RSP_NAK 0x04
  63. #define RVT_R_RSP_SEND 0x08
  64. #define RVT_R_COMM_EST 0x10
  65. /*
  66. * Bit definitions for s_flags.
  67. *
  68. * RVT_S_SIGNAL_REQ_WR - set if QP send WRs contain completion signaled
  69. * RVT_S_BUSY - send tasklet is processing the QP
  70. * RVT_S_TIMER - the RC retry timer is active
  71. * RVT_S_ACK_PENDING - an ACK is waiting to be sent after RDMA read/atomics
  72. * RVT_S_WAIT_FENCE - waiting for all prior RDMA read or atomic SWQEs
  73. * before processing the next SWQE
  74. * RVT_S_WAIT_RDMAR - waiting for a RDMA read or atomic SWQE to complete
  75. * before processing the next SWQE
  76. * RVT_S_WAIT_RNR - waiting for RNR timeout
  77. * RVT_S_WAIT_SSN_CREDIT - waiting for RC credits to process next SWQE
  78. * RVT_S_WAIT_DMA - waiting for send DMA queue to drain before generating
  79. * next send completion entry not via send DMA
  80. * RVT_S_WAIT_PIO - waiting for a send buffer to be available
  81. * RVT_S_WAIT_PIO_DRAIN - waiting for a qp to drain pio packets
  82. * RVT_S_WAIT_TX - waiting for a struct verbs_txreq to be available
  83. * RVT_S_WAIT_DMA_DESC - waiting for DMA descriptors to be available
  84. * RVT_S_WAIT_KMEM - waiting for kernel memory to be available
  85. * RVT_S_WAIT_PSN - waiting for a packet to exit the send DMA queue
  86. * RVT_S_WAIT_ACK - waiting for an ACK packet before sending more requests
  87. * RVT_S_SEND_ONE - send one packet, request ACK, then wait for ACK
  88. * RVT_S_ECN - a BECN was queued to the send engine
  89. */
  90. #define RVT_S_SIGNAL_REQ_WR 0x0001
  91. #define RVT_S_BUSY 0x0002
  92. #define RVT_S_TIMER 0x0004
  93. #define RVT_S_RESP_PENDING 0x0008
  94. #define RVT_S_ACK_PENDING 0x0010
  95. #define RVT_S_WAIT_FENCE 0x0020
  96. #define RVT_S_WAIT_RDMAR 0x0040
  97. #define RVT_S_WAIT_RNR 0x0080
  98. #define RVT_S_WAIT_SSN_CREDIT 0x0100
  99. #define RVT_S_WAIT_DMA 0x0200
  100. #define RVT_S_WAIT_PIO 0x0400
  101. #define RVT_S_WAIT_PIO_DRAIN 0x0800
  102. #define RVT_S_WAIT_TX 0x1000
  103. #define RVT_S_WAIT_DMA_DESC 0x2000
  104. #define RVT_S_WAIT_KMEM 0x4000
  105. #define RVT_S_WAIT_PSN 0x8000
  106. #define RVT_S_WAIT_ACK 0x10000
  107. #define RVT_S_SEND_ONE 0x20000
  108. #define RVT_S_UNLIMITED_CREDIT 0x40000
  109. #define RVT_S_AHG_VALID 0x80000
  110. #define RVT_S_AHG_CLEAR 0x100000
  111. #define RVT_S_ECN 0x200000
  112. /*
  113. * Wait flags that would prevent any packet type from being sent.
  114. */
  115. #define RVT_S_ANY_WAIT_IO (RVT_S_WAIT_PIO | RVT_S_WAIT_TX | \
  116. RVT_S_WAIT_DMA_DESC | RVT_S_WAIT_KMEM)
  117. /*
  118. * Wait flags that would prevent send work requests from making progress.
  119. */
  120. #define RVT_S_ANY_WAIT_SEND (RVT_S_WAIT_FENCE | RVT_S_WAIT_RDMAR | \
  121. RVT_S_WAIT_RNR | RVT_S_WAIT_SSN_CREDIT | RVT_S_WAIT_DMA | \
  122. RVT_S_WAIT_PSN | RVT_S_WAIT_ACK)
  123. #define RVT_S_ANY_WAIT (RVT_S_ANY_WAIT_IO | RVT_S_ANY_WAIT_SEND)
  124. /* Number of bits to pay attention to in the opcode for checking qp type */
  125. #define RVT_OPCODE_QP_MASK 0xE0
  126. /* Flags for checking QP state (see ib_rvt_state_ops[]) */
  127. #define RVT_POST_SEND_OK 0x01
  128. #define RVT_POST_RECV_OK 0x02
  129. #define RVT_PROCESS_RECV_OK 0x04
  130. #define RVT_PROCESS_SEND_OK 0x08
  131. #define RVT_PROCESS_NEXT_SEND_OK 0x10
  132. #define RVT_FLUSH_SEND 0x20
  133. #define RVT_FLUSH_RECV 0x40
  134. #define RVT_PROCESS_OR_FLUSH_SEND \
  135. (RVT_PROCESS_SEND_OK | RVT_FLUSH_SEND)
  136. /*
  137. * Send work request queue entry.
  138. * The size of the sg_list is determined when the QP is created and stored
  139. * in qp->s_max_sge.
  140. */
  141. struct rvt_swqe {
  142. union {
  143. struct ib_send_wr wr; /* don't use wr.sg_list */
  144. struct ib_ud_wr ud_wr;
  145. struct ib_reg_wr reg_wr;
  146. struct ib_rdma_wr rdma_wr;
  147. struct ib_atomic_wr atomic_wr;
  148. };
  149. u32 psn; /* first packet sequence number */
  150. u32 lpsn; /* last packet sequence number */
  151. u32 ssn; /* send sequence number */
  152. u32 length; /* total length of data in sg_list */
  153. struct rvt_sge sg_list[0];
  154. };
  155. /*
  156. * Receive work request queue entry.
  157. * The size of the sg_list is determined when the QP (or SRQ) is created
  158. * and stored in qp->r_rq.max_sge (or srq->rq.max_sge).
  159. */
  160. struct rvt_rwqe {
  161. u64 wr_id;
  162. u8 num_sge;
  163. struct ib_sge sg_list[0];
  164. };
  165. /*
  166. * This structure is used to contain the head pointer, tail pointer,
  167. * and receive work queue entries as a single memory allocation so
  168. * it can be mmap'ed into user space.
  169. * Note that the wq array elements are variable size so you can't
  170. * just index into the array to get the N'th element;
  171. * use get_rwqe_ptr() instead.
  172. */
  173. struct rvt_rwq {
  174. u32 head; /* new work requests posted to the head */
  175. u32 tail; /* receives pull requests from here. */
  176. struct rvt_rwqe wq[0];
  177. };
  178. struct rvt_rq {
  179. struct rvt_rwq *wq;
  180. u32 size; /* size of RWQE array */
  181. u8 max_sge;
  182. /* protect changes in this struct */
  183. spinlock_t lock ____cacheline_aligned_in_smp;
  184. };
  185. /*
  186. * This structure is used by rvt_mmap() to validate an offset
  187. * when an mmap() request is made. The vm_area_struct then uses
  188. * this as its vm_private_data.
  189. */
  190. struct rvt_mmap_info {
  191. struct list_head pending_mmaps;
  192. struct ib_ucontext *context;
  193. void *obj;
  194. __u64 offset;
  195. struct kref ref;
  196. unsigned size;
  197. };
  198. #define RVT_MAX_RDMA_ATOMIC 16
  199. /*
  200. * This structure holds the information that the send tasklet needs
  201. * to send a RDMA read response or atomic operation.
  202. */
  203. struct rvt_ack_entry {
  204. u8 opcode;
  205. u8 sent;
  206. u32 psn;
  207. u32 lpsn;
  208. union {
  209. struct rvt_sge rdma_sge;
  210. u64 atomic_data;
  211. };
  212. };
  213. #define RC_QP_SCALING_INTERVAL 5
  214. /*
  215. * Variables prefixed with s_ are for the requester (sender).
  216. * Variables prefixed with r_ are for the responder (receiver).
  217. * Variables prefixed with ack_ are for responder replies.
  218. *
  219. * Common variables are protected by both r_rq.lock and s_lock in that order
  220. * which only happens in modify_qp() or changing the QP 'state'.
  221. */
  222. struct rvt_qp {
  223. struct ib_qp ibqp;
  224. void *priv; /* Driver private data */
  225. /* read mostly fields above and below */
  226. struct ib_ah_attr remote_ah_attr;
  227. struct ib_ah_attr alt_ah_attr;
  228. struct rvt_qp __rcu *next; /* link list for QPN hash table */
  229. struct rvt_swqe *s_wq; /* send work queue */
  230. struct rvt_mmap_info *ip;
  231. unsigned long timeout_jiffies; /* computed from timeout */
  232. enum ib_mtu path_mtu;
  233. int srate_mbps; /* s_srate (below) converted to Mbit/s */
  234. pid_t pid; /* pid for user mode QPs */
  235. u32 remote_qpn;
  236. u32 qkey; /* QKEY for this QP (for UD or RD) */
  237. u32 s_size; /* send work queue size */
  238. u32 s_ahgpsn; /* set to the psn in the copy of the header */
  239. u16 pmtu; /* decoded from path_mtu */
  240. u8 log_pmtu; /* shift for pmtu */
  241. u8 state; /* QP state */
  242. u8 allowed_ops; /* high order bits of allowed opcodes */
  243. u8 qp_access_flags;
  244. u8 alt_timeout; /* Alternate path timeout for this QP */
  245. u8 timeout; /* Timeout for this QP */
  246. u8 s_srate;
  247. u8 s_mig_state;
  248. u8 port_num;
  249. u8 s_pkey_index; /* PKEY index to use */
  250. u8 s_alt_pkey_index; /* Alternate path PKEY index to use */
  251. u8 r_max_rd_atomic; /* max number of RDMA read/atomic to receive */
  252. u8 s_max_rd_atomic; /* max number of RDMA read/atomic to send */
  253. u8 s_retry_cnt; /* number of times to retry */
  254. u8 s_rnr_retry_cnt;
  255. u8 r_min_rnr_timer; /* retry timeout value for RNR NAKs */
  256. u8 s_max_sge; /* size of s_wq->sg_list */
  257. u8 s_draining;
  258. /* start of read/write fields */
  259. atomic_t refcount ____cacheline_aligned_in_smp;
  260. wait_queue_head_t wait;
  261. struct rvt_ack_entry s_ack_queue[RVT_MAX_RDMA_ATOMIC + 1]
  262. ____cacheline_aligned_in_smp;
  263. struct rvt_sge_state s_rdma_read_sge;
  264. spinlock_t r_lock ____cacheline_aligned_in_smp; /* used for APM */
  265. u32 r_psn; /* expected rcv packet sequence number */
  266. unsigned long r_aflags;
  267. u64 r_wr_id; /* ID for current receive WQE */
  268. u32 r_ack_psn; /* PSN for next ACK or atomic ACK */
  269. u32 r_len; /* total length of r_sge */
  270. u32 r_rcv_len; /* receive data len processed */
  271. u32 r_msn; /* message sequence number */
  272. u8 r_state; /* opcode of last packet received */
  273. u8 r_flags;
  274. u8 r_head_ack_queue; /* index into s_ack_queue[] */
  275. struct list_head rspwait; /* link for waiting to respond */
  276. struct rvt_sge_state r_sge; /* current receive data */
  277. struct rvt_rq r_rq; /* receive work queue */
  278. /* post send line */
  279. spinlock_t s_hlock ____cacheline_aligned_in_smp;
  280. u32 s_head; /* new entries added here */
  281. u32 s_next_psn; /* PSN for next request */
  282. u32 s_avail; /* number of entries avail */
  283. u32 s_ssn; /* SSN of tail entry */
  284. spinlock_t s_lock ____cacheline_aligned_in_smp;
  285. u32 s_flags;
  286. struct rvt_sge_state *s_cur_sge;
  287. struct rvt_swqe *s_wqe;
  288. struct rvt_sge_state s_sge; /* current send request data */
  289. struct rvt_mregion *s_rdma_mr;
  290. u32 s_cur_size; /* size of send packet in bytes */
  291. u32 s_len; /* total length of s_sge */
  292. u32 s_rdma_read_len; /* total length of s_rdma_read_sge */
  293. u32 s_last_psn; /* last response PSN processed */
  294. u32 s_sending_psn; /* lowest PSN that is being sent */
  295. u32 s_sending_hpsn; /* highest PSN that is being sent */
  296. u32 s_psn; /* current packet sequence number */
  297. u32 s_ack_rdma_psn; /* PSN for sending RDMA read responses */
  298. u32 s_ack_psn; /* PSN for acking sends and RDMA writes */
  299. u32 s_tail; /* next entry to process */
  300. u32 s_cur; /* current work queue entry */
  301. u32 s_acked; /* last un-ACK'ed entry */
  302. u32 s_last; /* last completed entry */
  303. u32 s_lsn; /* limit sequence number (credit) */
  304. u16 s_hdrwords; /* size of s_hdr in 32 bit words */
  305. u16 s_rdma_ack_cnt;
  306. s8 s_ahgidx;
  307. u8 s_state; /* opcode of last packet sent */
  308. u8 s_ack_state; /* opcode of packet to ACK */
  309. u8 s_nak_state; /* non-zero if NAK is pending */
  310. u8 r_nak_state; /* non-zero if NAK is pending */
  311. u8 s_retry; /* requester retry counter */
  312. u8 s_rnr_retry; /* requester RNR retry counter */
  313. u8 s_num_rd_atomic; /* number of RDMA read/atomic pending */
  314. u8 s_tail_ack_queue; /* index into s_ack_queue[] */
  315. struct rvt_sge_state s_ack_rdma_sge;
  316. struct timer_list s_timer;
  317. /*
  318. * This sge list MUST be last. Do not add anything below here.
  319. */
  320. struct rvt_sge r_sg_list[0] /* verified SGEs */
  321. ____cacheline_aligned_in_smp;
  322. };
  323. struct rvt_srq {
  324. struct ib_srq ibsrq;
  325. struct rvt_rq rq;
  326. struct rvt_mmap_info *ip;
  327. /* send signal when number of RWQEs < limit */
  328. u32 limit;
  329. };
  330. #define RVT_QPN_MAX BIT(24)
  331. #define RVT_QPNMAP_ENTRIES (RVT_QPN_MAX / PAGE_SIZE / BITS_PER_BYTE)
  332. #define RVT_BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE)
  333. #define RVT_BITS_PER_PAGE_MASK (RVT_BITS_PER_PAGE - 1)
  334. #define RVT_QPN_MASK 0xFFFFFF
  335. /*
  336. * QPN-map pages start out as NULL, they get allocated upon
  337. * first use and are never deallocated. This way,
  338. * large bitmaps are not allocated unless large numbers of QPs are used.
  339. */
  340. struct rvt_qpn_map {
  341. void *page;
  342. };
  343. struct rvt_qpn_table {
  344. spinlock_t lock; /* protect changes to the qp table */
  345. unsigned flags; /* flags for QP0/1 allocated for each port */
  346. u32 last; /* last QP number allocated */
  347. u32 nmaps; /* size of the map table */
  348. u16 limit;
  349. u8 incr;
  350. /* bit map of free QP numbers other than 0/1 */
  351. struct rvt_qpn_map map[RVT_QPNMAP_ENTRIES];
  352. };
  353. struct rvt_qp_ibdev {
  354. u32 qp_table_size;
  355. u32 qp_table_bits;
  356. struct rvt_qp __rcu **qp_table;
  357. spinlock_t qpt_lock; /* qptable lock */
  358. struct rvt_qpn_table qpn_table;
  359. };
  360. /*
  361. * There is one struct rvt_mcast for each multicast GID.
  362. * All attached QPs are then stored as a list of
  363. * struct rvt_mcast_qp.
  364. */
  365. struct rvt_mcast_qp {
  366. struct list_head list;
  367. struct rvt_qp *qp;
  368. };
  369. struct rvt_mcast {
  370. struct rb_node rb_node;
  371. union ib_gid mgid;
  372. struct list_head qp_list;
  373. wait_queue_head_t wait;
  374. atomic_t refcount;
  375. int n_attached;
  376. };
  377. /*
  378. * Since struct rvt_swqe is not a fixed size, we can't simply index into
  379. * struct rvt_qp.s_wq. This function does the array index computation.
  380. */
  381. static inline struct rvt_swqe *rvt_get_swqe_ptr(struct rvt_qp *qp,
  382. unsigned n)
  383. {
  384. return (struct rvt_swqe *)((char *)qp->s_wq +
  385. (sizeof(struct rvt_swqe) +
  386. qp->s_max_sge *
  387. sizeof(struct rvt_sge)) * n);
  388. }
  389. /*
  390. * Since struct rvt_rwqe is not a fixed size, we can't simply index into
  391. * struct rvt_rwq.wq. This function does the array index computation.
  392. */
  393. static inline struct rvt_rwqe *rvt_get_rwqe_ptr(struct rvt_rq *rq, unsigned n)
  394. {
  395. return (struct rvt_rwqe *)
  396. ((char *)rq->wq->wq +
  397. (sizeof(struct rvt_rwqe) +
  398. rq->max_sge * sizeof(struct ib_sge)) * n);
  399. }
  400. extern const int ib_rvt_state_ops[];
  401. struct rvt_dev_info;
  402. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);
  403. #endif /* DEF_RDMAVT_INCQP_H */