PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/net/rds/ib.h

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