ib.h 13 KB

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