ib.h 12 KB

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