rdmavt_qp.h 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. * 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 aproximate of total receive enteries posted
  174. * @rvt_rwqe: 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. * 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. * @qp - the qp pair
  594. * @len - the length
  595. *
  596. * Perform a shift based mtu divide
  597. */
  598. static inline u32 rvt_div_mtu(struct rvt_qp *qp, u32 len)
  599. {
  600. return len >> qp->log_pmtu;
  601. }
  602. /**
  603. * rvt_timeout_to_jiffies - Convert a ULP timeout input into jiffies
  604. * @timeout - timeout input(0 - 31).
  605. *
  606. * Return a timeout value in jiffies.
  607. */
  608. static inline unsigned long rvt_timeout_to_jiffies(u8 timeout)
  609. {
  610. if (timeout > 31)
  611. timeout = 31;
  612. return usecs_to_jiffies(1U << timeout) * 4096UL / 1000UL;
  613. }
  614. /**
  615. * rvt_lookup_qpn - return the QP with the given QPN
  616. * @ibp: the ibport
  617. * @qpn: the QP number to look up
  618. *
  619. * The caller must hold the rcu_read_lock(), and keep the lock until
  620. * the returned qp is no longer in use.
  621. */
  622. static inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,
  623. struct rvt_ibport *rvp,
  624. u32 qpn) __must_hold(RCU)
  625. {
  626. struct rvt_qp *qp = NULL;
  627. if (unlikely(qpn <= 1)) {
  628. qp = rcu_dereference(rvp->qp[qpn]);
  629. } else {
  630. u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);
  631. for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;
  632. qp = rcu_dereference(qp->next))
  633. if (qp->ibqp.qp_num == qpn)
  634. break;
  635. }
  636. return qp;
  637. }
  638. /**
  639. * rvt_mod_retry_timer - mod a retry timer
  640. * @qp - the QP
  641. * @shift - timeout shift to wait for multiple packets
  642. * Modify a potentially already running retry timer
  643. */
  644. static inline void rvt_mod_retry_timer_ext(struct rvt_qp *qp, u8 shift)
  645. {
  646. struct ib_qp *ibqp = &qp->ibqp;
  647. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  648. lockdep_assert_held(&qp->s_lock);
  649. qp->s_flags |= RVT_S_TIMER;
  650. /* 4.096 usec. * (1 << qp->timeout) */
  651. mod_timer(&qp->s_timer, jiffies + rdi->busy_jiffies +
  652. (qp->timeout_jiffies << shift));
  653. }
  654. static inline void rvt_mod_retry_timer(struct rvt_qp *qp)
  655. {
  656. return rvt_mod_retry_timer_ext(qp, 0);
  657. }
  658. /**
  659. * rvt_put_qp_swqe - drop refs held by swqe
  660. * @qp: the send qp
  661. * @wqe: the send wqe
  662. *
  663. * This drops any references held by the swqe
  664. */
  665. static inline void rvt_put_qp_swqe(struct rvt_qp *qp, struct rvt_swqe *wqe)
  666. {
  667. rvt_put_swqe(wqe);
  668. if (qp->allowed_ops == IB_OPCODE_UD)
  669. rdma_destroy_ah_attr(wqe->ud_wr.attr);
  670. }
  671. /**
  672. * rvt_qp_sqwe_incr - increment ring index
  673. * @qp: the qp
  674. * @val: the starting value
  675. *
  676. * Return: the new value wrapping as appropriate
  677. */
  678. static inline u32
  679. rvt_qp_swqe_incr(struct rvt_qp *qp, u32 val)
  680. {
  681. if (++val >= qp->s_size)
  682. val = 0;
  683. return val;
  684. }
  685. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err);
  686. /**
  687. * rvt_recv_cq - add a new entry to completion queue
  688. * by receive queue
  689. * @qp: receive queue
  690. * @wc: work completion entry to add
  691. * @solicited: true if @entry is solicited
  692. *
  693. * This is wrapper function for rvt_enter_cq function call by
  694. * receive queue. If rvt_cq_enter return false, it means cq is
  695. * full and the qp is put into error state.
  696. */
  697. static inline void rvt_recv_cq(struct rvt_qp *qp, struct ib_wc *wc,
  698. bool solicited)
  699. {
  700. struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.recv_cq);
  701. if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
  702. rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
  703. }
  704. /**
  705. * rvt_send_cq - add a new entry to completion queue
  706. * by send queue
  707. * @qp: send queue
  708. * @wc: work completion entry to add
  709. * @solicited: true if @entry is solicited
  710. *
  711. * This is wrapper function for rvt_enter_cq function call by
  712. * send queue. If rvt_cq_enter return false, it means cq is
  713. * full and the qp is put into error state.
  714. */
  715. static inline void rvt_send_cq(struct rvt_qp *qp, struct ib_wc *wc,
  716. bool solicited)
  717. {
  718. struct rvt_cq *cq = ibcq_to_rvtcq(qp->ibqp.send_cq);
  719. if (unlikely(!rvt_cq_enter(cq, wc, solicited)))
  720. rvt_error_qp(qp, IB_WC_LOC_QP_OP_ERR);
  721. }
  722. /**
  723. * rvt_qp_complete_swqe - insert send completion
  724. * @qp - the qp
  725. * @wqe - the send wqe
  726. * @opcode - wc operation (driver dependent)
  727. * @status - completion status
  728. *
  729. * Update the s_last information, and then insert a send
  730. * completion into the completion
  731. * queue if the qp indicates it should be done.
  732. *
  733. * See IBTA 10.7.3.1 for info on completion
  734. * control.
  735. *
  736. * Return: new last
  737. */
  738. static inline u32
  739. rvt_qp_complete_swqe(struct rvt_qp *qp,
  740. struct rvt_swqe *wqe,
  741. enum ib_wc_opcode opcode,
  742. enum ib_wc_status status)
  743. {
  744. bool need_completion;
  745. u64 wr_id;
  746. u32 byte_len, last;
  747. int flags = wqe->wr.send_flags;
  748. rvt_qp_wqe_unreserve(qp, flags);
  749. rvt_put_qp_swqe(qp, wqe);
  750. need_completion =
  751. !(flags & RVT_SEND_RESERVE_USED) &&
  752. (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
  753. (flags & IB_SEND_SIGNALED) ||
  754. status != IB_WC_SUCCESS);
  755. if (need_completion) {
  756. wr_id = wqe->wr.wr_id;
  757. byte_len = wqe->length;
  758. /* above fields required before writing s_last */
  759. }
  760. last = rvt_qp_swqe_incr(qp, qp->s_last);
  761. /* see rvt_qp_is_avail() */
  762. smp_store_release(&qp->s_last, last);
  763. if (need_completion) {
  764. struct ib_wc w = {
  765. .wr_id = wr_id,
  766. .status = status,
  767. .opcode = opcode,
  768. .qp = &qp->ibqp,
  769. .byte_len = byte_len,
  770. };
  771. rvt_send_cq(qp, &w, status != IB_WC_SUCCESS);
  772. }
  773. return last;
  774. }
  775. extern const int ib_rvt_state_ops[];
  776. struct rvt_dev_info;
  777. int rvt_get_rwqe(struct rvt_qp *qp, bool wr_id_only);
  778. void rvt_comm_est(struct rvt_qp *qp);
  779. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err);
  780. unsigned long rvt_rnr_tbl_to_usec(u32 index);
  781. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t);
  782. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth);
  783. void rvt_del_timers_sync(struct rvt_qp *qp);
  784. void rvt_stop_rc_timers(struct rvt_qp *qp);
  785. void rvt_add_retry_timer_ext(struct rvt_qp *qp, u8 shift);
  786. static inline void rvt_add_retry_timer(struct rvt_qp *qp)
  787. {
  788. rvt_add_retry_timer_ext(qp, 0);
  789. }
  790. void rvt_copy_sge(struct rvt_qp *qp, struct rvt_sge_state *ss,
  791. void *data, u32 length,
  792. bool release, bool copy_last);
  793. void rvt_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
  794. enum ib_wc_status status);
  795. void rvt_ruc_loopback(struct rvt_qp *qp);
  796. /**
  797. * struct rvt_qp_iter - the iterator for QPs
  798. * @qp - the current QP
  799. *
  800. * This structure defines the current iterator
  801. * state for sequenced access to all QPs relative
  802. * to an rvt_dev_info.
  803. */
  804. struct rvt_qp_iter {
  805. struct rvt_qp *qp;
  806. /* private: backpointer */
  807. struct rvt_dev_info *rdi;
  808. /* private: callback routine */
  809. void (*cb)(struct rvt_qp *qp, u64 v);
  810. /* private: for arg to callback routine */
  811. u64 v;
  812. /* private: number of SMI,GSI QPs for device */
  813. int specials;
  814. /* private: current iterator index */
  815. int n;
  816. };
  817. /**
  818. * ib_cq_tail - Return tail index of cq buffer
  819. * @send_cq - The cq for send
  820. *
  821. * This is called in qp_iter_print to get tail
  822. * of cq buffer.
  823. */
  824. static inline u32 ib_cq_tail(struct ib_cq *send_cq)
  825. {
  826. struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
  827. return ibcq_to_rvtcq(send_cq)->ip ?
  828. RDMA_READ_UAPI_ATOMIC(cq->queue->tail) :
  829. ibcq_to_rvtcq(send_cq)->kqueue->tail;
  830. }
  831. /**
  832. * ib_cq_head - Return head index of cq buffer
  833. * @send_cq - The cq for send
  834. *
  835. * This is called in qp_iter_print to get head
  836. * of cq buffer.
  837. */
  838. static inline u32 ib_cq_head(struct ib_cq *send_cq)
  839. {
  840. struct rvt_cq *cq = ibcq_to_rvtcq(send_cq);
  841. return ibcq_to_rvtcq(send_cq)->ip ?
  842. RDMA_READ_UAPI_ATOMIC(cq->queue->head) :
  843. ibcq_to_rvtcq(send_cq)->kqueue->head;
  844. }
  845. /**
  846. * rvt_free_rq - free memory allocated for rvt_rq struct
  847. * @rvt_rq: request queue data structure
  848. *
  849. * This function should only be called if the rvt_mmap_info()
  850. * has not succeeded.
  851. */
  852. static inline void rvt_free_rq(struct rvt_rq *rq)
  853. {
  854. kvfree(rq->kwq);
  855. rq->kwq = NULL;
  856. vfree(rq->wq);
  857. rq->wq = NULL;
  858. }
  859. /**
  860. * rvt_to_iport - Get the ibport pointer
  861. * @qp: the qp pointer
  862. *
  863. * This function returns the ibport pointer from the qp pointer.
  864. */
  865. static inline struct rvt_ibport *rvt_to_iport(struct rvt_qp *qp)
  866. {
  867. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  868. return rdi->ports[qp->port_num - 1];
  869. }
  870. /**
  871. * rvt_rc_credit_avail - Check if there are enough RC credits for the request
  872. * @qp: the qp
  873. * @wqe: the request
  874. *
  875. * This function returns false when there are not enough credits for the given
  876. * request and true otherwise.
  877. */
  878. static inline bool rvt_rc_credit_avail(struct rvt_qp *qp, struct rvt_swqe *wqe)
  879. {
  880. lockdep_assert_held(&qp->s_lock);
  881. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT) &&
  882. rvt_cmp_msn(wqe->ssn, qp->s_lsn + 1) > 0) {
  883. struct rvt_ibport *rvp = rvt_to_iport(qp);
  884. qp->s_flags |= RVT_S_WAIT_SSN_CREDIT;
  885. rvp->n_rc_crwaits++;
  886. return false;
  887. }
  888. return true;
  889. }
  890. struct rvt_qp_iter *rvt_qp_iter_init(struct rvt_dev_info *rdi,
  891. u64 v,
  892. void (*cb)(struct rvt_qp *qp, u64 v));
  893. int rvt_qp_iter_next(struct rvt_qp_iter *iter);
  894. void rvt_qp_iter(struct rvt_dev_info *rdi,
  895. u64 v,
  896. void (*cb)(struct rvt_qp *qp, u64 v));
  897. void rvt_qp_mr_clean(struct rvt_qp *qp, u32 lkey);
  898. #endif /* DEF_RDMAVT_INCQP_H */