ib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _RDS_IB_H
  3. #define _RDS_IB_H
  4. #include <rdma/ib_verbs.h>
  5. #include <rdma/rdma_cm.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/pci.h>
  8. #include <linux/slab.h>
  9. #include "rds.h"
  10. #include "rdma_transport.h"
  11. #define RDS_IB_MAX_SGE 8
  12. #define RDS_IB_RECV_SGE 2
  13. #define RDS_IB_DEFAULT_RECV_WR 1024
  14. #define RDS_IB_DEFAULT_SEND_WR 256
  15. #define RDS_IB_DEFAULT_FR_WR 256
  16. #define RDS_IB_DEFAULT_FR_INV_WR 256
  17. #define RDS_IB_DEFAULT_RETRY_COUNT 1
  18. #define RDS_IB_SUPPORTED_PROTOCOLS 0x00000003 /* minor versions supported */
  19. #define RDS_IB_RECYCLE_BATCH_COUNT 32
  20. #define RDS_IB_WC_MAX 32
  21. extern struct rw_semaphore rds_ib_devices_lock;
  22. extern struct list_head rds_ib_devices;
  23. /*
  24. * IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to
  25. * try and minimize the amount of memory tied up both the device and
  26. * socket receive queues.
  27. */
  28. struct rds_page_frag {
  29. struct list_head f_item;
  30. struct list_head f_cache_entry;
  31. struct scatterlist f_sg;
  32. };
  33. struct rds_ib_incoming {
  34. struct list_head ii_frags;
  35. struct list_head ii_cache_entry;
  36. struct rds_incoming ii_inc;
  37. };
  38. struct rds_ib_cache_head {
  39. struct list_head *first;
  40. unsigned long count;
  41. };
  42. struct rds_ib_refill_cache {
  43. struct rds_ib_cache_head __percpu *percpu;
  44. struct list_head *xfer;
  45. struct list_head *ready;
  46. };
  47. struct rds_ib_connect_private {
  48. /* Add new fields at the end, and don't permute existing fields. */
  49. __be32 dp_saddr;
  50. __be32 dp_daddr;
  51. u8 dp_protocol_major;
  52. u8 dp_protocol_minor;
  53. __be16 dp_protocol_minor_mask; /* bitmask */
  54. __be32 dp_reserved1;
  55. __be64 dp_ack_seq;
  56. __be32 dp_credit; /* non-zero enables flow ctl */
  57. };
  58. struct rds_ib_send_work {
  59. void *s_op;
  60. union {
  61. struct ib_send_wr s_wr;
  62. struct ib_rdma_wr s_rdma_wr;
  63. struct ib_atomic_wr s_atomic_wr;
  64. };
  65. struct ib_sge s_sge[RDS_IB_MAX_SGE];
  66. unsigned long s_queued;
  67. };
  68. struct rds_ib_recv_work {
  69. struct rds_ib_incoming *r_ibinc;
  70. struct rds_page_frag *r_frag;
  71. struct ib_recv_wr r_wr;
  72. struct ib_sge r_sge[2];
  73. };
  74. struct rds_ib_work_ring {
  75. u32 w_nr;
  76. u32 w_alloc_ptr;
  77. u32 w_alloc_ctr;
  78. u32 w_free_ptr;
  79. atomic_t w_free_ctr;
  80. };
  81. /* Rings are posted with all the allocations they'll need to queue the
  82. * incoming message to the receiving socket so this can't fail.
  83. * All fragments start with a header, so we can make sure we're not receiving
  84. * garbage, and we can tell a small 8 byte fragment from an ACK frame.
  85. */
  86. struct rds_ib_ack_state {
  87. u64 ack_next;
  88. u64 ack_recv;
  89. unsigned int ack_required:1;
  90. unsigned int ack_next_valid:1;
  91. unsigned int ack_recv_valid:1;
  92. };
  93. struct rds_ib_device;
  94. struct rds_ib_connection {
  95. struct list_head ib_node;
  96. struct rds_ib_device *rds_ibdev;
  97. struct rds_connection *conn;
  98. /* alphabet soup, IBTA style */
  99. struct rdma_cm_id *i_cm_id;
  100. struct ib_pd *i_pd;
  101. struct ib_cq *i_send_cq;
  102. struct ib_cq *i_recv_cq;
  103. struct ib_wc i_send_wc[RDS_IB_WC_MAX];
  104. struct ib_wc i_recv_wc[RDS_IB_WC_MAX];
  105. /* To control the number of wrs from fastreg */
  106. atomic_t i_fastreg_wrs;
  107. atomic_t i_fastunreg_wrs;
  108. /* interrupt handling */
  109. struct tasklet_struct i_send_tasklet;
  110. struct tasklet_struct i_recv_tasklet;
  111. /* tx */
  112. struct rds_ib_work_ring i_send_ring;
  113. struct rm_data_op *i_data_op;
  114. struct rds_header *i_send_hdrs;
  115. dma_addr_t i_send_hdrs_dma;
  116. struct rds_ib_send_work *i_sends;
  117. atomic_t i_signaled_sends;
  118. /* rx */
  119. struct mutex i_recv_mutex;
  120. struct rds_ib_work_ring i_recv_ring;
  121. struct rds_ib_incoming *i_ibinc;
  122. u32 i_recv_data_rem;
  123. struct rds_header *i_recv_hdrs;
  124. dma_addr_t i_recv_hdrs_dma;
  125. struct rds_ib_recv_work *i_recvs;
  126. u64 i_ack_recv; /* last ACK received */
  127. struct rds_ib_refill_cache i_cache_incs;
  128. struct rds_ib_refill_cache i_cache_frags;
  129. atomic_t i_cache_allocs;
  130. /* sending acks */
  131. unsigned long i_ack_flags;
  132. #ifdef KERNEL_HAS_ATOMIC64
  133. atomic64_t i_ack_next; /* next ACK to send */
  134. #else
  135. spinlock_t i_ack_lock; /* protect i_ack_next */
  136. u64 i_ack_next; /* next ACK to send */
  137. #endif
  138. struct rds_header *i_ack;
  139. struct ib_send_wr i_ack_wr;
  140. struct ib_sge i_ack_sge;
  141. dma_addr_t i_ack_dma;
  142. unsigned long i_ack_queued;
  143. /* Flow control related information
  144. *
  145. * Our algorithm uses a pair variables that we need to access
  146. * atomically - one for the send credits, and one posted
  147. * recv credits we need to transfer to remote.
  148. * Rather than protect them using a slow spinlock, we put both into
  149. * a single atomic_t and update it using cmpxchg
  150. */
  151. atomic_t i_credits;
  152. /* Protocol version specific information */
  153. unsigned int i_flowctl:1; /* enable/disable flow ctl */
  154. /* Batched completions */
  155. unsigned int i_unsignaled_wrs;
  156. /* Endpoint role in connection */
  157. bool i_active_side;
  158. atomic_t i_cq_quiesce;
  159. /* Send/Recv vectors */
  160. int i_scq_vector;
  161. int i_rcq_vector;
  162. };
  163. /* This assumes that atomic_t is at least 32 bits */
  164. #define IB_GET_SEND_CREDITS(v) ((v) & 0xffff)
  165. #define IB_GET_POST_CREDITS(v) ((v) >> 16)
  166. #define IB_SET_SEND_CREDITS(v) ((v) & 0xffff)
  167. #define IB_SET_POST_CREDITS(v) ((v) << 16)
  168. struct rds_ib_ipaddr {
  169. struct list_head list;
  170. __be32 ipaddr;
  171. struct rcu_head rcu;
  172. };
  173. enum {
  174. RDS_IB_MR_8K_POOL,
  175. RDS_IB_MR_1M_POOL,
  176. };
  177. struct rds_ib_device {
  178. struct list_head list;
  179. struct list_head ipaddr_list;
  180. struct list_head conn_list;
  181. struct ib_device *dev;
  182. struct ib_pd *pd;
  183. bool has_fmr;
  184. bool has_fr;
  185. bool use_fastreg;
  186. unsigned int max_mrs;
  187. struct rds_ib_mr_pool *mr_1m_pool;
  188. struct rds_ib_mr_pool *mr_8k_pool;
  189. unsigned int fmr_max_remaps;
  190. unsigned int max_8k_mrs;
  191. unsigned int max_1m_mrs;
  192. int max_sge;
  193. unsigned int max_wrs;
  194. unsigned int max_initiator_depth;
  195. unsigned int max_responder_resources;
  196. spinlock_t spinlock; /* protect the above */
  197. refcount_t refcount;
  198. struct work_struct free_work;
  199. int *vector_load;
  200. };
  201. #define ibdev_to_node(ibdev) dev_to_node((ibdev)->dev.parent)
  202. #define rdsibdev_to_node(rdsibdev) ibdev_to_node(rdsibdev->dev)
  203. /* bits for i_ack_flags */
  204. #define IB_ACK_IN_FLIGHT 0
  205. #define IB_ACK_REQUESTED 1
  206. /* Magic WR_ID for ACKs */
  207. #define RDS_IB_ACK_WR_ID (~(u64) 0)
  208. struct rds_ib_statistics {
  209. uint64_t s_ib_connect_raced;
  210. uint64_t s_ib_listen_closed_stale;
  211. uint64_t s_ib_evt_handler_call;
  212. uint64_t s_ib_tasklet_call;
  213. uint64_t s_ib_tx_cq_event;
  214. uint64_t s_ib_tx_ring_full;
  215. uint64_t s_ib_tx_throttle;
  216. uint64_t s_ib_tx_sg_mapping_failure;
  217. uint64_t s_ib_tx_stalled;
  218. uint64_t s_ib_tx_credit_updates;
  219. uint64_t s_ib_rx_cq_event;
  220. uint64_t s_ib_rx_ring_empty;
  221. uint64_t s_ib_rx_refill_from_cq;
  222. uint64_t s_ib_rx_refill_from_thread;
  223. uint64_t s_ib_rx_alloc_limit;
  224. uint64_t s_ib_rx_total_frags;
  225. uint64_t s_ib_rx_total_incs;
  226. uint64_t s_ib_rx_credit_updates;
  227. uint64_t s_ib_ack_sent;
  228. uint64_t s_ib_ack_send_failure;
  229. uint64_t s_ib_ack_send_delayed;
  230. uint64_t s_ib_ack_send_piggybacked;
  231. uint64_t s_ib_ack_received;
  232. uint64_t s_ib_rdma_mr_8k_alloc;
  233. uint64_t s_ib_rdma_mr_8k_free;
  234. uint64_t s_ib_rdma_mr_8k_used;
  235. uint64_t s_ib_rdma_mr_8k_pool_flush;
  236. uint64_t s_ib_rdma_mr_8k_pool_wait;
  237. uint64_t s_ib_rdma_mr_8k_pool_depleted;
  238. uint64_t s_ib_rdma_mr_1m_alloc;
  239. uint64_t s_ib_rdma_mr_1m_free;
  240. uint64_t s_ib_rdma_mr_1m_used;
  241. uint64_t s_ib_rdma_mr_1m_pool_flush;
  242. uint64_t s_ib_rdma_mr_1m_pool_wait;
  243. uint64_t s_ib_rdma_mr_1m_pool_depleted;
  244. uint64_t s_ib_rdma_mr_8k_reused;
  245. uint64_t s_ib_rdma_mr_1m_reused;
  246. uint64_t s_ib_atomic_cswp;
  247. uint64_t s_ib_atomic_fadd;
  248. uint64_t s_ib_recv_added_to_cache;
  249. uint64_t s_ib_recv_removed_from_cache;
  250. };
  251. extern struct workqueue_struct *rds_ib_wq;
  252. /*
  253. * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
  254. * doesn't define it.
  255. */
  256. static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device *dev,
  257. struct scatterlist *sglist,
  258. unsigned int sg_dma_len,
  259. int direction)
  260. {
  261. struct scatterlist *sg;
  262. unsigned int i;
  263. for_each_sg(sglist, sg, sg_dma_len, i) {
  264. ib_dma_sync_single_for_cpu(dev,
  265. ib_sg_dma_address(dev, sg),
  266. ib_sg_dma_len(dev, sg),
  267. direction);
  268. }
  269. }
  270. #define ib_dma_sync_sg_for_cpu rds_ib_dma_sync_sg_for_cpu
  271. static inline void rds_ib_dma_sync_sg_for_device(struct ib_device *dev,
  272. struct scatterlist *sglist,
  273. unsigned int sg_dma_len,
  274. int direction)
  275. {
  276. struct scatterlist *sg;
  277. unsigned int i;
  278. for_each_sg(sglist, sg, sg_dma_len, i) {
  279. ib_dma_sync_single_for_device(dev,
  280. ib_sg_dma_address(dev, sg),
  281. ib_sg_dma_len(dev, sg),
  282. direction);
  283. }
  284. }
  285. #define ib_dma_sync_sg_for_device rds_ib_dma_sync_sg_for_device
  286. /* ib.c */
  287. extern struct rds_transport rds_ib_transport;
  288. struct rds_ib_device *rds_ib_get_client_data(struct ib_device *device);
  289. void rds_ib_dev_put(struct rds_ib_device *rds_ibdev);
  290. extern struct ib_client rds_ib_client;
  291. extern unsigned int rds_ib_retry_count;
  292. extern spinlock_t ib_nodev_conns_lock;
  293. extern struct list_head ib_nodev_conns;
  294. /* ib_cm.c */
  295. int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp);
  296. void rds_ib_conn_free(void *arg);
  297. int rds_ib_conn_path_connect(struct rds_conn_path *cp);
  298. void rds_ib_conn_path_shutdown(struct rds_conn_path *cp);
  299. void rds_ib_state_change(struct sock *sk);
  300. int rds_ib_listen_init(void);
  301. void rds_ib_listen_stop(void);
  302. __printf(2, 3)
  303. void __rds_ib_conn_error(struct rds_connection *conn, const char *, ...);
  304. int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
  305. struct rdma_cm_event *event);
  306. int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id);
  307. void rds_ib_cm_connect_complete(struct rds_connection *conn,
  308. struct rdma_cm_event *event);
  309. #define rds_ib_conn_error(conn, fmt...) \
  310. __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
  311. /* ib_rdma.c */
  312. int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr);
  313. void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
  314. void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
  315. void rds_ib_destroy_nodev_conns(void);
  316. void rds_ib_mr_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc);
  317. /* ib_recv.c */
  318. int rds_ib_recv_init(void);
  319. void rds_ib_recv_exit(void);
  320. int rds_ib_recv_path(struct rds_conn_path *conn);
  321. int rds_ib_recv_alloc_caches(struct rds_ib_connection *ic, gfp_t gfp);
  322. void rds_ib_recv_free_caches(struct rds_ib_connection *ic);
  323. void rds_ib_recv_refill(struct rds_connection *conn, int prefill, gfp_t gfp);
  324. void rds_ib_inc_free(struct rds_incoming *inc);
  325. int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
  326. void rds_ib_recv_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc,
  327. struct rds_ib_ack_state *state);
  328. void rds_ib_recv_tasklet_fn(unsigned long data);
  329. void rds_ib_recv_init_ring(struct rds_ib_connection *ic);
  330. void rds_ib_recv_clear_ring(struct rds_ib_connection *ic);
  331. void rds_ib_recv_init_ack(struct rds_ib_connection *ic);
  332. void rds_ib_attempt_ack(struct rds_ib_connection *ic);
  333. void rds_ib_ack_send_complete(struct rds_ib_connection *ic);
  334. u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic);
  335. void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq, int ack_required);
  336. /* ib_ring.c */
  337. void rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr);
  338. void rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr);
  339. u32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos);
  340. void rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val);
  341. void rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val);
  342. int rds_ib_ring_empty(struct rds_ib_work_ring *ring);
  343. int rds_ib_ring_low(struct rds_ib_work_ring *ring);
  344. u32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring);
  345. u32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest);
  346. extern wait_queue_head_t rds_ib_ring_empty_wait;
  347. /* ib_send.c */
  348. void rds_ib_xmit_path_complete(struct rds_conn_path *cp);
  349. int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
  350. unsigned int hdr_off, unsigned int sg, unsigned int off);
  351. void rds_ib_send_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc);
  352. void rds_ib_send_init_ring(struct rds_ib_connection *ic);
  353. void rds_ib_send_clear_ring(struct rds_ib_connection *ic);
  354. int rds_ib_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op);
  355. void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits);
  356. void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted);
  357. int rds_ib_send_grab_credits(struct rds_ib_connection *ic, u32 wanted,
  358. u32 *adv_credits, int need_posted, int max_posted);
  359. int rds_ib_xmit_atomic(struct rds_connection *conn, struct rm_atomic_op *op);
  360. /* ib_stats.c */
  361. DECLARE_PER_CPU(struct rds_ib_statistics, rds_ib_stats);
  362. #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
  363. #define rds_ib_stats_add(member, count) \
  364. rds_stats_add_which(rds_ib_stats, member, count)
  365. unsigned int rds_ib_stats_info_copy(struct rds_info_iterator *iter,
  366. unsigned int avail);
  367. /* ib_sysctl.c */
  368. int rds_ib_sysctl_init(void);
  369. void rds_ib_sysctl_exit(void);
  370. extern unsigned long rds_ib_sysctl_max_send_wr;
  371. extern unsigned long rds_ib_sysctl_max_recv_wr;
  372. extern unsigned long rds_ib_sysctl_max_unsig_wrs;
  373. extern unsigned long rds_ib_sysctl_max_unsig_bytes;
  374. extern unsigned long rds_ib_sysctl_max_recv_allocation;
  375. extern unsigned int rds_ib_sysctl_flow_control;
  376. #endif