rxe_verbs.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #ifndef RXE_VERBS_H
  7. #define RXE_VERBS_H
  8. #include <linux/interrupt.h>
  9. #include <linux/workqueue.h>
  10. #include "rxe_pool.h"
  11. #include "rxe_task.h"
  12. #include "rxe_hw_counters.h"
  13. static inline int pkey_match(u16 key1, u16 key2)
  14. {
  15. return (((key1 & 0x7fff) != 0) &&
  16. ((key1 & 0x7fff) == (key2 & 0x7fff)) &&
  17. ((key1 & 0x8000) || (key2 & 0x8000))) ? 1 : 0;
  18. }
  19. /* Return >0 if psn_a > psn_b
  20. * 0 if psn_a == psn_b
  21. * <0 if psn_a < psn_b
  22. */
  23. static inline int psn_compare(u32 psn_a, u32 psn_b)
  24. {
  25. s32 diff;
  26. diff = (psn_a - psn_b) << 8;
  27. return diff;
  28. }
  29. struct rxe_ucontext {
  30. struct ib_ucontext ibuc;
  31. struct rxe_pool_elem elem;
  32. };
  33. struct rxe_pd {
  34. struct ib_pd ibpd;
  35. struct rxe_pool_elem elem;
  36. };
  37. struct rxe_ah {
  38. struct ib_ah ibah;
  39. struct rxe_pool_elem elem;
  40. struct rxe_av av;
  41. bool is_user;
  42. int ah_num;
  43. };
  44. struct rxe_cqe {
  45. union {
  46. struct ib_wc ibwc;
  47. struct ib_uverbs_wc uibwc;
  48. };
  49. };
  50. struct rxe_cq {
  51. struct ib_cq ibcq;
  52. struct rxe_pool_elem elem;
  53. struct rxe_queue *queue;
  54. spinlock_t cq_lock;
  55. u8 notify;
  56. bool is_dying;
  57. bool is_user;
  58. struct tasklet_struct comp_task;
  59. atomic_t num_wq;
  60. };
  61. enum wqe_state {
  62. wqe_state_posted,
  63. wqe_state_processing,
  64. wqe_state_pending,
  65. wqe_state_done,
  66. wqe_state_error,
  67. };
  68. struct rxe_sq {
  69. int max_wr;
  70. int max_sge;
  71. int max_inline;
  72. spinlock_t sq_lock; /* guard queue */
  73. struct rxe_queue *queue;
  74. };
  75. struct rxe_rq {
  76. int max_wr;
  77. int max_sge;
  78. spinlock_t producer_lock; /* guard queue producer */
  79. spinlock_t consumer_lock; /* guard queue consumer */
  80. struct rxe_queue *queue;
  81. };
  82. struct rxe_srq {
  83. struct ib_srq ibsrq;
  84. struct rxe_pool_elem elem;
  85. struct rxe_pd *pd;
  86. struct rxe_rq rq;
  87. u32 srq_num;
  88. int limit;
  89. int error;
  90. };
  91. enum rxe_qp_state {
  92. QP_STATE_RESET,
  93. QP_STATE_INIT,
  94. QP_STATE_READY,
  95. QP_STATE_DRAIN, /* req only */
  96. QP_STATE_DRAINED, /* req only */
  97. QP_STATE_ERROR
  98. };
  99. struct rxe_req_info {
  100. enum rxe_qp_state state;
  101. int wqe_index;
  102. u32 psn;
  103. int opcode;
  104. atomic_t rd_atomic;
  105. int wait_fence;
  106. int need_rd_atomic;
  107. int wait_psn;
  108. int need_retry;
  109. int wait_for_rnr_timer;
  110. int noack_pkts;
  111. struct rxe_task task;
  112. };
  113. struct rxe_comp_info {
  114. enum rxe_qp_state state;
  115. u32 psn;
  116. int opcode;
  117. int timeout;
  118. int timeout_retry;
  119. int started_retry;
  120. u32 retry_cnt;
  121. u32 rnr_retry;
  122. struct rxe_task task;
  123. };
  124. enum rdatm_res_state {
  125. rdatm_res_state_next,
  126. rdatm_res_state_new,
  127. rdatm_res_state_replay,
  128. };
  129. struct resp_res {
  130. int type;
  131. int replay;
  132. u32 first_psn;
  133. u32 last_psn;
  134. u32 cur_psn;
  135. enum rdatm_res_state state;
  136. union {
  137. struct {
  138. u64 orig_val;
  139. } atomic;
  140. struct {
  141. u64 va_org;
  142. u32 rkey;
  143. u32 length;
  144. u64 va;
  145. u32 resid;
  146. } read;
  147. };
  148. };
  149. struct rxe_resp_info {
  150. enum rxe_qp_state state;
  151. u32 msn;
  152. u32 psn;
  153. u32 ack_psn;
  154. int opcode;
  155. int drop_msg;
  156. int goto_error;
  157. int sent_psn_nak;
  158. enum ib_wc_status status;
  159. u8 aeth_syndrome;
  160. /* Receive only */
  161. struct rxe_recv_wqe *wqe;
  162. /* RDMA read / atomic only */
  163. u64 va;
  164. u64 offset;
  165. struct rxe_mr *mr;
  166. u32 resid;
  167. u32 rkey;
  168. u32 length;
  169. /* SRQ only */
  170. struct {
  171. struct rxe_recv_wqe wqe;
  172. struct ib_sge sge[RXE_MAX_SGE];
  173. } srq_wqe;
  174. /* Responder resources. It's a circular list where the oldest
  175. * resource is dropped first.
  176. */
  177. struct resp_res *resources;
  178. unsigned int res_head;
  179. unsigned int res_tail;
  180. struct resp_res *res;
  181. struct rxe_task task;
  182. };
  183. struct rxe_qp {
  184. struct ib_qp ibqp;
  185. struct rxe_pool_elem elem;
  186. struct ib_qp_attr attr;
  187. unsigned int valid;
  188. unsigned int mtu;
  189. bool is_user;
  190. struct rxe_pd *pd;
  191. struct rxe_srq *srq;
  192. struct rxe_cq *scq;
  193. struct rxe_cq *rcq;
  194. enum ib_sig_type sq_sig_type;
  195. struct rxe_sq sq;
  196. struct rxe_rq rq;
  197. struct socket *sk;
  198. u32 dst_cookie;
  199. u16 src_port;
  200. struct rxe_av pri_av;
  201. struct rxe_av alt_av;
  202. atomic_t mcg_num;
  203. struct sk_buff_head req_pkts;
  204. struct sk_buff_head resp_pkts;
  205. struct rxe_req_info req;
  206. struct rxe_comp_info comp;
  207. struct rxe_resp_info resp;
  208. atomic_t ssn;
  209. atomic_t skb_out;
  210. int need_req_skb;
  211. /* Timer for retranmitting packet when ACKs have been lost. RC
  212. * only. The requester sets it when it is not already
  213. * started. The responder resets it whenever an ack is
  214. * received.
  215. */
  216. struct timer_list retrans_timer;
  217. u64 qp_timeout_jiffies;
  218. /* Timer for handling RNR NAKS. */
  219. struct timer_list rnr_nak_timer;
  220. spinlock_t state_lock; /* guard requester and completer */
  221. struct execute_work cleanup_work;
  222. };
  223. enum rxe_mr_state {
  224. RXE_MR_STATE_INVALID,
  225. RXE_MR_STATE_FREE,
  226. RXE_MR_STATE_VALID,
  227. };
  228. enum rxe_mr_copy_dir {
  229. RXE_TO_MR_OBJ,
  230. RXE_FROM_MR_OBJ,
  231. };
  232. enum rxe_mr_lookup_type {
  233. RXE_LOOKUP_LOCAL,
  234. RXE_LOOKUP_REMOTE,
  235. };
  236. #define RXE_BUF_PER_MAP (PAGE_SIZE / sizeof(struct rxe_phys_buf))
  237. struct rxe_phys_buf {
  238. u64 addr;
  239. u64 size;
  240. };
  241. struct rxe_map {
  242. struct rxe_phys_buf buf[RXE_BUF_PER_MAP];
  243. };
  244. static inline int rkey_is_mw(u32 rkey)
  245. {
  246. u32 index = rkey >> 8;
  247. return (index >= RXE_MIN_MW_INDEX) && (index <= RXE_MAX_MW_INDEX);
  248. }
  249. struct rxe_mr {
  250. struct rxe_pool_elem elem;
  251. struct ib_mr ibmr;
  252. struct ib_umem *umem;
  253. u32 lkey;
  254. u32 rkey;
  255. enum rxe_mr_state state;
  256. enum ib_mr_type type;
  257. u32 offset;
  258. int access;
  259. int page_shift;
  260. int page_mask;
  261. int map_shift;
  262. int map_mask;
  263. u32 num_buf;
  264. u32 nbuf;
  265. u32 max_buf;
  266. u32 num_map;
  267. atomic_t num_mw;
  268. struct rxe_map **map;
  269. };
  270. enum rxe_mw_state {
  271. RXE_MW_STATE_INVALID = RXE_MR_STATE_INVALID,
  272. RXE_MW_STATE_FREE = RXE_MR_STATE_FREE,
  273. RXE_MW_STATE_VALID = RXE_MR_STATE_VALID,
  274. };
  275. struct rxe_mw {
  276. struct ib_mw ibmw;
  277. struct rxe_pool_elem elem;
  278. spinlock_t lock;
  279. enum rxe_mw_state state;
  280. struct rxe_qp *qp; /* Type 2 only */
  281. struct rxe_mr *mr;
  282. u32 rkey;
  283. int access;
  284. u64 addr;
  285. u64 length;
  286. };
  287. struct rxe_mcg {
  288. struct rb_node node;
  289. struct kref ref_cnt;
  290. struct rxe_dev *rxe;
  291. struct list_head qp_list;
  292. union ib_gid mgid;
  293. atomic_t qp_num;
  294. u32 qkey;
  295. u16 pkey;
  296. };
  297. struct rxe_mca {
  298. struct list_head qp_list;
  299. struct rxe_qp *qp;
  300. };
  301. struct rxe_port {
  302. struct ib_port_attr attr;
  303. __be64 port_guid;
  304. __be64 subnet_prefix;
  305. spinlock_t port_lock; /* guard port */
  306. unsigned int mtu_cap;
  307. /* special QPs */
  308. u32 qp_gsi_index;
  309. };
  310. struct rxe_dev {
  311. struct ib_device ib_dev;
  312. struct ib_device_attr attr;
  313. int max_ucontext;
  314. int max_inline_data;
  315. struct mutex usdev_lock;
  316. struct net_device *ndev;
  317. struct rxe_pool uc_pool;
  318. struct rxe_pool pd_pool;
  319. struct rxe_pool ah_pool;
  320. struct rxe_pool srq_pool;
  321. struct rxe_pool qp_pool;
  322. struct rxe_pool cq_pool;
  323. struct rxe_pool mr_pool;
  324. struct rxe_pool mw_pool;
  325. /* multicast support */
  326. spinlock_t mcg_lock;
  327. struct rb_root mcg_tree;
  328. atomic_t mcg_num;
  329. atomic_t mcg_attach;
  330. spinlock_t pending_lock; /* guard pending_mmaps */
  331. struct list_head pending_mmaps;
  332. spinlock_t mmap_offset_lock; /* guard mmap_offset */
  333. u64 mmap_offset;
  334. atomic64_t stats_counters[RXE_NUM_OF_COUNTERS];
  335. struct rxe_port port;
  336. struct crypto_shash *tfm;
  337. };
  338. static inline void rxe_counter_inc(struct rxe_dev *rxe, enum rxe_counters index)
  339. {
  340. atomic64_inc(&rxe->stats_counters[index]);
  341. }
  342. static inline struct rxe_dev *to_rdev(struct ib_device *dev)
  343. {
  344. return dev ? container_of(dev, struct rxe_dev, ib_dev) : NULL;
  345. }
  346. static inline struct rxe_ucontext *to_ruc(struct ib_ucontext *uc)
  347. {
  348. return uc ? container_of(uc, struct rxe_ucontext, ibuc) : NULL;
  349. }
  350. static inline struct rxe_pd *to_rpd(struct ib_pd *pd)
  351. {
  352. return pd ? container_of(pd, struct rxe_pd, ibpd) : NULL;
  353. }
  354. static inline struct rxe_ah *to_rah(struct ib_ah *ah)
  355. {
  356. return ah ? container_of(ah, struct rxe_ah, ibah) : NULL;
  357. }
  358. static inline struct rxe_srq *to_rsrq(struct ib_srq *srq)
  359. {
  360. return srq ? container_of(srq, struct rxe_srq, ibsrq) : NULL;
  361. }
  362. static inline struct rxe_qp *to_rqp(struct ib_qp *qp)
  363. {
  364. return qp ? container_of(qp, struct rxe_qp, ibqp) : NULL;
  365. }
  366. static inline struct rxe_cq *to_rcq(struct ib_cq *cq)
  367. {
  368. return cq ? container_of(cq, struct rxe_cq, ibcq) : NULL;
  369. }
  370. static inline struct rxe_mr *to_rmr(struct ib_mr *mr)
  371. {
  372. return mr ? container_of(mr, struct rxe_mr, ibmr) : NULL;
  373. }
  374. static inline struct rxe_mw *to_rmw(struct ib_mw *mw)
  375. {
  376. return mw ? container_of(mw, struct rxe_mw, ibmw) : NULL;
  377. }
  378. static inline struct rxe_pd *rxe_ah_pd(struct rxe_ah *ah)
  379. {
  380. return to_rpd(ah->ibah.pd);
  381. }
  382. static inline struct rxe_pd *mr_pd(struct rxe_mr *mr)
  383. {
  384. return to_rpd(mr->ibmr.pd);
  385. }
  386. static inline struct rxe_pd *rxe_mw_pd(struct rxe_mw *mw)
  387. {
  388. return to_rpd(mw->ibmw.pd);
  389. }
  390. int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name);
  391. #endif /* RXE_VERBS_H */