rdmavt_qp.h 28 KB

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