ib.h 14 KB

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