ib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #ifndef _RDS_IB_H
  2. #define _RDS_IB_H
  3. #include <rdma/ib_verbs.h>
  4. #include <rdma/rdma_cm.h>
  5. #include "rds.h"
  6. #include "rdma_transport.h"
  7. #define RDS_FMR_SIZE 256
  8. #define RDS_FMR_POOL_SIZE 4096
  9. #define RDS_IB_MAX_SGE 8
  10. #define RDS_IB_RECV_SGE 2
  11. #define RDS_IB_DEFAULT_RECV_WR 1024
  12. #define RDS_IB_DEFAULT_SEND_WR 256
  13. #define RDS_IB_DEFAULT_RETRY_COUNT 2
  14. #define RDS_IB_SUPPORTED_PROTOCOLS 0x00000003 /* minor versions supported */
  15. extern struct list_head rds_ib_devices;
  16. /*
  17. * IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to
  18. * try and minimize the amount of memory tied up both the device and
  19. * socket receive queues.
  20. */
  21. /* page offset of the final full frag that fits in the page */
  22. #define RDS_PAGE_LAST_OFF (((PAGE_SIZE / RDS_FRAG_SIZE) - 1) * RDS_FRAG_SIZE)
  23. struct rds_page_frag {
  24. struct list_head f_item;
  25. struct page *f_page;
  26. unsigned long f_offset;
  27. dma_addr_t f_mapped;
  28. };
  29. struct rds_ib_incoming {
  30. struct list_head ii_frags;
  31. struct rds_incoming ii_inc;
  32. };
  33. struct rds_ib_connect_private {
  34. /* Add new fields at the end, and don't permute existing fields. */
  35. __be32 dp_saddr;
  36. __be32 dp_daddr;
  37. u8 dp_protocol_major;
  38. u8 dp_protocol_minor;
  39. __be16 dp_protocol_minor_mask; /* bitmask */
  40. __be32 dp_reserved1;
  41. __be64 dp_ack_seq;
  42. __be32 dp_credit; /* non-zero enables flow ctl */
  43. };
  44. struct rds_ib_send_work {
  45. struct rds_message *s_rm;
  46. struct rds_rdma_op *s_op;
  47. struct ib_send_wr s_wr;
  48. struct ib_sge s_sge[RDS_IB_MAX_SGE];
  49. unsigned long s_queued;
  50. };
  51. struct rds_ib_recv_work {
  52. struct rds_ib_incoming *r_ibinc;
  53. struct rds_page_frag *r_frag;
  54. struct ib_recv_wr r_wr;
  55. struct ib_sge r_sge[2];
  56. };
  57. struct rds_ib_work_ring {
  58. u32 w_nr;
  59. u32 w_alloc_ptr;
  60. u32 w_alloc_ctr;
  61. u32 w_free_ptr;
  62. atomic_t w_free_ctr;
  63. };
  64. struct rds_ib_device;
  65. struct rds_ib_connection {
  66. struct list_head ib_node;
  67. struct rds_ib_device *rds_ibdev;
  68. struct rds_connection *conn;
  69. /* alphabet soup, IBTA style */
  70. struct rdma_cm_id *i_cm_id;
  71. struct ib_pd *i_pd;
  72. struct ib_mr *i_mr;
  73. struct ib_cq *i_send_cq;
  74. struct ib_cq *i_recv_cq;
  75. /* tx */
  76. struct rds_ib_work_ring i_send_ring;
  77. struct rds_message *i_rm;
  78. struct rds_header *i_send_hdrs;
  79. u64 i_send_hdrs_dma;
  80. struct rds_ib_send_work *i_sends;
  81. /* rx */
  82. struct mutex i_recv_mutex;
  83. struct rds_ib_work_ring i_recv_ring;
  84. struct rds_ib_incoming *i_ibinc;
  85. u32 i_recv_data_rem;
  86. struct rds_header *i_recv_hdrs;
  87. u64 i_recv_hdrs_dma;
  88. struct rds_ib_recv_work *i_recvs;
  89. struct rds_page_frag i_frag;
  90. u64 i_ack_recv; /* last ACK received */
  91. /* sending acks */
  92. unsigned long i_ack_flags;
  93. #ifdef KERNEL_HAS_ATOMIC64
  94. atomic64_t i_ack_next; /* next ACK to send */
  95. #else
  96. spinlock_t i_ack_lock; /* protect i_ack_next */
  97. u64 i_ack_next; /* next ACK to send */
  98. #endif
  99. struct rds_header *i_ack;
  100. struct ib_send_wr i_ack_wr;
  101. struct ib_sge i_ack_sge;
  102. u64 i_ack_dma;
  103. unsigned long i_ack_queued;
  104. /* Flow control related information
  105. *
  106. * Our algorithm uses a pair variables that we need to access
  107. * atomically - one for the send credits, and one posted
  108. * recv credits we need to transfer to remote.
  109. * Rather than protect them using a slow spinlock, we put both into
  110. * a single atomic_t and update it using cmpxchg
  111. */
  112. atomic_t i_credits;
  113. /* Protocol version specific information */
  114. unsigned int i_flowctl:1; /* enable/disable flow ctl */
  115. /* Batched completions */
  116. unsigned int i_unsignaled_wrs;
  117. long i_unsignaled_bytes;
  118. };
  119. /* This assumes that atomic_t is at least 32 bits */
  120. #define IB_GET_SEND_CREDITS(v) ((v) & 0xffff)
  121. #define IB_GET_POST_CREDITS(v) ((v) >> 16)
  122. #define IB_SET_SEND_CREDITS(v) ((v) & 0xffff)
  123. #define IB_SET_POST_CREDITS(v) ((v) << 16)
  124. struct rds_ib_ipaddr {
  125. struct list_head list;
  126. __be32 ipaddr;
  127. };
  128. struct rds_ib_device {
  129. struct list_head list;
  130. struct list_head ipaddr_list;
  131. struct list_head conn_list;
  132. struct ib_device *dev;
  133. struct ib_pd *pd;
  134. struct ib_mr *mr;
  135. struct rds_ib_mr_pool *mr_pool;
  136. unsigned int fmr_max_remaps;
  137. unsigned int max_fmrs;
  138. int max_sge;
  139. unsigned int max_wrs;
  140. spinlock_t spinlock; /* protect the above */
  141. };
  142. /* bits for i_ack_flags */
  143. #define IB_ACK_IN_FLIGHT 0
  144. #define IB_ACK_REQUESTED 1
  145. /* Magic WR_ID for ACKs */
  146. #define RDS_IB_ACK_WR_ID (~(u64) 0)
  147. struct rds_ib_statistics {
  148. uint64_t s_ib_connect_raced;
  149. uint64_t s_ib_listen_closed_stale;
  150. uint64_t s_ib_tx_cq_call;
  151. uint64_t s_ib_tx_cq_event;
  152. uint64_t s_ib_tx_ring_full;
  153. uint64_t s_ib_tx_throttle;
  154. uint64_t s_ib_tx_sg_mapping_failure;
  155. uint64_t s_ib_tx_stalled;
  156. uint64_t s_ib_tx_credit_updates;
  157. uint64_t s_ib_rx_cq_call;
  158. uint64_t s_ib_rx_cq_event;
  159. uint64_t s_ib_rx_ring_empty;
  160. uint64_t s_ib_rx_refill_from_cq;
  161. uint64_t s_ib_rx_refill_from_thread;
  162. uint64_t s_ib_rx_alloc_limit;
  163. uint64_t s_ib_rx_credit_updates;
  164. uint64_t s_ib_ack_sent;
  165. uint64_t s_ib_ack_send_failure;
  166. uint64_t s_ib_ack_send_delayed;
  167. uint64_t s_ib_ack_send_piggybacked;
  168. uint64_t s_ib_ack_received;
  169. uint64_t s_ib_rdma_mr_alloc;
  170. uint64_t s_ib_rdma_mr_free;
  171. uint64_t s_ib_rdma_mr_used;
  172. uint64_t s_ib_rdma_mr_pool_flush;
  173. uint64_t s_ib_rdma_mr_pool_wait;
  174. uint64_t s_ib_rdma_mr_pool_depleted;
  175. };
  176. extern struct workqueue_struct *rds_ib_wq;
  177. /*
  178. * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
  179. * doesn't define it.
  180. */
  181. static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device *dev,
  182. struct scatterlist *sg, unsigned int sg_dma_len, int direction)
  183. {
  184. unsigned int i;
  185. for (i = 0; i < sg_dma_len; ++i) {
  186. ib_dma_sync_single_for_cpu(dev,
  187. ib_sg_dma_address(dev, &sg[i]),
  188. ib_sg_dma_len(dev, &sg[i]),
  189. direction);
  190. }
  191. }
  192. #define ib_dma_sync_sg_for_cpu rds_ib_dma_sync_sg_for_cpu
  193. static inline void rds_ib_dma_sync_sg_for_device(struct ib_device *dev,
  194. struct scatterlist *sg, unsigned int sg_dma_len, int direction)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < sg_dma_len; ++i) {
  198. ib_dma_sync_single_for_device(dev,
  199. ib_sg_dma_address(dev, &sg[i]),
  200. ib_sg_dma_len(dev, &sg[i]),
  201. direction);
  202. }
  203. }
  204. #define ib_dma_sync_sg_for_device rds_ib_dma_sync_sg_for_device
  205. /* ib.c */
  206. extern struct rds_transport rds_ib_transport;
  207. extern void rds_ib_add_one(struct ib_device *device);
  208. extern void rds_ib_remove_one(struct ib_device *device);
  209. extern struct ib_client rds_ib_client;
  210. extern unsigned int fmr_pool_size;
  211. extern unsigned int fmr_message_size;
  212. extern unsigned int rds_ib_retry_count;
  213. extern spinlock_t ib_nodev_conns_lock;
  214. extern struct list_head ib_nodev_conns;
  215. /* ib_cm.c */
  216. int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp);
  217. void rds_ib_conn_free(void *arg);
  218. int rds_ib_conn_connect(struct rds_connection *conn);
  219. void rds_ib_conn_shutdown(struct rds_connection *conn);
  220. void rds_ib_state_change(struct sock *sk);
  221. int __init rds_ib_listen_init(void);
  222. void rds_ib_listen_stop(void);
  223. void __rds_ib_conn_error(struct rds_connection *conn, const char *, ...);
  224. int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
  225. struct rdma_cm_event *event);
  226. int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id);
  227. void rds_ib_cm_connect_complete(struct rds_connection *conn,
  228. struct rdma_cm_event *event);
  229. #define rds_ib_conn_error(conn, fmt...) \
  230. __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
  231. /* ib_rdma.c */
  232. int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr);
  233. void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
  234. void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
  235. void __rds_ib_destroy_conns(struct list_head *list, spinlock_t *list_lock);
  236. static inline void rds_ib_destroy_nodev_conns(void)
  237. {
  238. __rds_ib_destroy_conns(&ib_nodev_conns, &ib_nodev_conns_lock);
  239. }
  240. static inline void rds_ib_destroy_conns(struct rds_ib_device *rds_ibdev)
  241. {
  242. __rds_ib_destroy_conns(&rds_ibdev->conn_list, &rds_ibdev->spinlock);
  243. }
  244. struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *);
  245. void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo);
  246. void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *);
  247. void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
  248. struct rds_sock *rs, u32 *key_ret);
  249. void rds_ib_sync_mr(void *trans_private, int dir);
  250. void rds_ib_free_mr(void *trans_private, int invalidate);
  251. void rds_ib_flush_mrs(void);
  252. /* ib_recv.c */
  253. int __init rds_ib_recv_init(void);
  254. void rds_ib_recv_exit(void);
  255. int rds_ib_recv(struct rds_connection *conn);
  256. int rds_ib_recv_refill(struct rds_connection *conn, gfp_t kptr_gfp,
  257. gfp_t page_gfp, int prefill);
  258. void rds_ib_inc_purge(struct rds_incoming *inc);
  259. void rds_ib_inc_free(struct rds_incoming *inc);
  260. int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iovec *iov,
  261. size_t size);
  262. void rds_ib_recv_cq_comp_handler(struct ib_cq *cq, void *context);
  263. void rds_ib_recv_init_ring(struct rds_ib_connection *ic);
  264. void rds_ib_recv_clear_ring(struct rds_ib_connection *ic);
  265. void rds_ib_recv_init_ack(struct rds_ib_connection *ic);
  266. void rds_ib_attempt_ack(struct rds_ib_connection *ic);
  267. void rds_ib_ack_send_complete(struct rds_ib_connection *ic);
  268. u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic);
  269. /* ib_ring.c */
  270. void rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr);
  271. void rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr);
  272. u32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos);
  273. void rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val);
  274. void rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val);
  275. int rds_ib_ring_empty(struct rds_ib_work_ring *ring);
  276. int rds_ib_ring_low(struct rds_ib_work_ring *ring);
  277. u32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring);
  278. u32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest);
  279. extern wait_queue_head_t rds_ib_ring_empty_wait;
  280. /* ib_send.c */
  281. void rds_ib_xmit_complete(struct rds_connection *conn);
  282. int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
  283. unsigned int hdr_off, unsigned int sg, unsigned int off);
  284. void rds_ib_send_cq_comp_handler(struct ib_cq *cq, void *context);
  285. void rds_ib_send_init_ring(struct rds_ib_connection *ic);
  286. void rds_ib_send_clear_ring(struct rds_ib_connection *ic);
  287. int rds_ib_xmit_rdma(struct rds_connection *conn, struct rds_rdma_op *op);
  288. void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits);
  289. void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted);
  290. int rds_ib_send_grab_credits(struct rds_ib_connection *ic, u32 wanted,
  291. u32 *adv_credits, int need_posted, int max_posted);
  292. /* ib_stats.c */
  293. DECLARE_PER_CPU(struct rds_ib_statistics, rds_ib_stats);
  294. #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
  295. unsigned int rds_ib_stats_info_copy(struct rds_info_iterator *iter,
  296. unsigned int avail);
  297. /* ib_sysctl.c */
  298. int __init rds_ib_sysctl_init(void);
  299. void rds_ib_sysctl_exit(void);
  300. extern unsigned long rds_ib_sysctl_max_send_wr;
  301. extern unsigned long rds_ib_sysctl_max_recv_wr;
  302. extern unsigned long rds_ib_sysctl_max_unsig_wrs;
  303. extern unsigned long rds_ib_sysctl_max_unsig_bytes;
  304. extern unsigned long rds_ib_sysctl_max_recv_allocation;
  305. extern unsigned int rds_ib_sysctl_flow_control;
  306. extern ctl_table rds_ib_sysctl_table[];
  307. /*
  308. * Helper functions for getting/setting the header and data SGEs in
  309. * RDS packets (not RDMA)
  310. *
  311. * From version 3.1 onwards, header is in front of data in the sge.
  312. */
  313. static inline struct ib_sge *
  314. rds_ib_header_sge(struct rds_ib_connection *ic, struct ib_sge *sge)
  315. {
  316. if (ic->conn->c_version > RDS_PROTOCOL_3_0)
  317. return &sge[0];
  318. else
  319. return &sge[1];
  320. }
  321. static inline struct ib_sge *
  322. rds_ib_data_sge(struct rds_ib_connection *ic, struct ib_sge *sge)
  323. {
  324. if (ic->conn->c_version > RDS_PROTOCOL_3_0)
  325. return &sge[1];
  326. else
  327. return &sge[0];
  328. }
  329. #endif