rdmavt_qp.h 28 KB

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