iw.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #ifndef _RDS_IW_H
  2. #define _RDS_IW_H
  3. #include <linux/interrupt.h>
  4. #include <rdma/ib_verbs.h>
  5. #include <rdma/rdma_cm.h>
  6. #include "rds.h"
  7. #include "rdma_transport.h"
  8. #define RDS_FASTREG_SIZE 20
  9. #define RDS_FASTREG_POOL_SIZE 2048
  10. #define RDS_IW_MAX_SGE 8
  11. #define RDS_IW_RECV_SGE 2
  12. #define RDS_IW_DEFAULT_RECV_WR 1024
  13. #define RDS_IW_DEFAULT_SEND_WR 256
  14. #define RDS_IW_SUPPORTED_PROTOCOLS 0x00000003 /* minor versions supported */
  15. extern struct list_head rds_iw_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_iw_incoming {
  30. struct list_head ii_frags;
  31. struct rds_incoming ii_inc;
  32. };
  33. struct rds_iw_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_iw_scatterlist {
  45. struct scatterlist *list;
  46. unsigned int len;
  47. int dma_len;
  48. unsigned int dma_npages;
  49. unsigned int bytes;
  50. };
  51. struct rds_iw_mapping {
  52. spinlock_t m_lock; /* protect the mapping struct */
  53. struct list_head m_list;
  54. struct rds_iw_mr *m_mr;
  55. uint32_t m_rkey;
  56. struct rds_iw_scatterlist m_sg;
  57. };
  58. struct rds_iw_send_work {
  59. struct rds_message *s_rm;
  60. /* We should really put these into a union: */
  61. struct rm_rdma_op *s_op;
  62. struct rds_iw_mapping *s_mapping;
  63. struct ib_mr *s_mr;
  64. unsigned char s_remap_count;
  65. union {
  66. struct ib_send_wr s_send_wr;
  67. struct ib_rdma_wr s_rdma_wr;
  68. struct ib_reg_wr s_reg_wr;
  69. };
  70. struct ib_sge s_sge[RDS_IW_MAX_SGE];
  71. unsigned long s_queued;
  72. };
  73. struct rds_iw_recv_work {
  74. struct rds_iw_incoming *r_iwinc;
  75. struct rds_page_frag *r_frag;
  76. struct ib_recv_wr r_wr;
  77. struct ib_sge r_sge[2];
  78. };
  79. struct rds_iw_work_ring {
  80. u32 w_nr;
  81. u32 w_alloc_ptr;
  82. u32 w_alloc_ctr;
  83. u32 w_free_ptr;
  84. atomic_t w_free_ctr;
  85. };
  86. struct rds_iw_device;
  87. struct rds_iw_connection {
  88. struct list_head iw_node;
  89. struct rds_iw_device *rds_iwdev;
  90. struct rds_connection *conn;
  91. /* alphabet soup, IBTA style */
  92. struct rdma_cm_id *i_cm_id;
  93. struct ib_pd *i_pd;
  94. struct ib_mr *i_mr;
  95. struct ib_cq *i_send_cq;
  96. struct ib_cq *i_recv_cq;
  97. /* tx */
  98. struct rds_iw_work_ring i_send_ring;
  99. struct rds_message *i_rm;
  100. struct rds_header *i_send_hdrs;
  101. u64 i_send_hdrs_dma;
  102. struct rds_iw_send_work *i_sends;
  103. /* rx */
  104. struct tasklet_struct i_recv_tasklet;
  105. struct mutex i_recv_mutex;
  106. struct rds_iw_work_ring i_recv_ring;
  107. struct rds_iw_incoming *i_iwinc;
  108. u32 i_recv_data_rem;
  109. struct rds_header *i_recv_hdrs;
  110. u64 i_recv_hdrs_dma;
  111. struct rds_iw_recv_work *i_recvs;
  112. struct rds_page_frag i_frag;
  113. u64 i_ack_recv; /* last ACK received */
  114. /* sending acks */
  115. unsigned long i_ack_flags;
  116. #ifdef KERNEL_HAS_ATOMIC64
  117. atomic64_t i_ack_next; /* next ACK to send */
  118. #else
  119. spinlock_t i_ack_lock; /* protect i_ack_next */
  120. u64 i_ack_next; /* next ACK to send */
  121. #endif
  122. struct rds_header *i_ack;
  123. struct ib_send_wr i_ack_wr;
  124. struct ib_sge i_ack_sge;
  125. u64 i_ack_dma;
  126. unsigned long i_ack_queued;
  127. /* Flow control related information
  128. *
  129. * Our algorithm uses a pair variables that we need to access
  130. * atomically - one for the send credits, and one posted
  131. * recv credits we need to transfer to remote.
  132. * Rather than protect them using a slow spinlock, we put both into
  133. * a single atomic_t and update it using cmpxchg
  134. */
  135. atomic_t i_credits;
  136. /* Protocol version specific information */
  137. unsigned int i_flowctl:1; /* enable/disable flow ctl */
  138. unsigned int i_dma_local_lkey:1;
  139. unsigned int i_fastreg_posted:1; /* fastreg posted on this connection */
  140. /* Batched completions */
  141. unsigned int i_unsignaled_wrs;
  142. long i_unsignaled_bytes;
  143. };
  144. /* This assumes that atomic_t is at least 32 bits */
  145. #define IB_GET_SEND_CREDITS(v) ((v) & 0xffff)
  146. #define IB_GET_POST_CREDITS(v) ((v) >> 16)
  147. #define IB_SET_SEND_CREDITS(v) ((v) & 0xffff)
  148. #define IB_SET_POST_CREDITS(v) ((v) << 16)
  149. struct rds_iw_cm_id {
  150. struct list_head list;
  151. struct rdma_cm_id *cm_id;
  152. };
  153. struct rds_iw_device {
  154. struct list_head list;
  155. struct list_head cm_id_list;
  156. struct list_head conn_list;
  157. struct ib_device *dev;
  158. struct ib_pd *pd;
  159. struct ib_mr *mr;
  160. struct rds_iw_mr_pool *mr_pool;
  161. int max_sge;
  162. unsigned int max_wrs;
  163. unsigned int dma_local_lkey:1;
  164. spinlock_t spinlock; /* protect the above */
  165. };
  166. /* bits for i_ack_flags */
  167. #define IB_ACK_IN_FLIGHT 0
  168. #define IB_ACK_REQUESTED 1
  169. /* Magic WR_ID for ACKs */
  170. #define RDS_IW_ACK_WR_ID ((u64)0xffffffffffffffffULL)
  171. #define RDS_IW_REG_WR_ID ((u64)0xefefefefefefefefULL)
  172. #define RDS_IW_LOCAL_INV_WR_ID ((u64)0xdfdfdfdfdfdfdfdfULL)
  173. struct rds_iw_statistics {
  174. uint64_t s_iw_connect_raced;
  175. uint64_t s_iw_listen_closed_stale;
  176. uint64_t s_iw_tx_cq_call;
  177. uint64_t s_iw_tx_cq_event;
  178. uint64_t s_iw_tx_ring_full;
  179. uint64_t s_iw_tx_throttle;
  180. uint64_t s_iw_tx_sg_mapping_failure;
  181. uint64_t s_iw_tx_stalled;
  182. uint64_t s_iw_tx_credit_updates;
  183. uint64_t s_iw_rx_cq_call;
  184. uint64_t s_iw_rx_cq_event;
  185. uint64_t s_iw_rx_ring_empty;
  186. uint64_t s_iw_rx_refill_from_cq;
  187. uint64_t s_iw_rx_refill_from_thread;
  188. uint64_t s_iw_rx_alloc_limit;
  189. uint64_t s_iw_rx_credit_updates;
  190. uint64_t s_iw_ack_sent;
  191. uint64_t s_iw_ack_send_failure;
  192. uint64_t s_iw_ack_send_delayed;
  193. uint64_t s_iw_ack_send_piggybacked;
  194. uint64_t s_iw_ack_received;
  195. uint64_t s_iw_rdma_mr_alloc;
  196. uint64_t s_iw_rdma_mr_free;
  197. uint64_t s_iw_rdma_mr_used;
  198. uint64_t s_iw_rdma_mr_pool_flush;
  199. uint64_t s_iw_rdma_mr_pool_wait;
  200. uint64_t s_iw_rdma_mr_pool_depleted;
  201. };
  202. extern struct workqueue_struct *rds_iw_wq;
  203. /*
  204. * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
  205. * doesn't define it.
  206. */
  207. static inline void rds_iw_dma_sync_sg_for_cpu(struct ib_device *dev,
  208. struct scatterlist *sg, unsigned int sg_dma_len, int direction)
  209. {
  210. unsigned int i;
  211. for (i = 0; i < sg_dma_len; ++i) {
  212. ib_dma_sync_single_for_cpu(dev,
  213. ib_sg_dma_address(dev, &sg[i]),
  214. ib_sg_dma_len(dev, &sg[i]),
  215. direction);
  216. }
  217. }
  218. #define ib_dma_sync_sg_for_cpu rds_iw_dma_sync_sg_for_cpu
  219. static inline void rds_iw_dma_sync_sg_for_device(struct ib_device *dev,
  220. struct scatterlist *sg, unsigned int sg_dma_len, int direction)
  221. {
  222. unsigned int i;
  223. for (i = 0; i < sg_dma_len; ++i) {
  224. ib_dma_sync_single_for_device(dev,
  225. ib_sg_dma_address(dev, &sg[i]),
  226. ib_sg_dma_len(dev, &sg[i]),
  227. direction);
  228. }
  229. }
  230. #define ib_dma_sync_sg_for_device rds_iw_dma_sync_sg_for_device
  231. static inline u32 rds_iw_local_dma_lkey(struct rds_iw_connection *ic)
  232. {
  233. return ic->i_dma_local_lkey ? ic->i_cm_id->device->local_dma_lkey : ic->i_mr->lkey;
  234. }
  235. /* ib.c */
  236. extern struct rds_transport rds_iw_transport;
  237. extern struct ib_client rds_iw_client;
  238. extern unsigned int fastreg_pool_size;
  239. extern unsigned int fastreg_message_size;
  240. extern spinlock_t iw_nodev_conns_lock;
  241. extern struct list_head iw_nodev_conns;
  242. /* ib_cm.c */
  243. int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp);
  244. void rds_iw_conn_free(void *arg);
  245. int rds_iw_conn_connect(struct rds_connection *conn);
  246. void rds_iw_conn_shutdown(struct rds_connection *conn);
  247. void rds_iw_state_change(struct sock *sk);
  248. int rds_iw_listen_init(void);
  249. void rds_iw_listen_stop(void);
  250. void __rds_iw_conn_error(struct rds_connection *conn, const char *, ...);
  251. int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id,
  252. struct rdma_cm_event *event);
  253. int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id);
  254. void rds_iw_cm_connect_complete(struct rds_connection *conn,
  255. struct rdma_cm_event *event);
  256. #define rds_iw_conn_error(conn, fmt...) \
  257. __rds_iw_conn_error(conn, KERN_WARNING "RDS/IW: " fmt)
  258. /* ib_rdma.c */
  259. int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id);
  260. void rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn);
  261. void rds_iw_remove_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn);
  262. void __rds_iw_destroy_conns(struct list_head *list, spinlock_t *list_lock);
  263. static inline void rds_iw_destroy_nodev_conns(void)
  264. {
  265. __rds_iw_destroy_conns(&iw_nodev_conns, &iw_nodev_conns_lock);
  266. }
  267. static inline void rds_iw_destroy_conns(struct rds_iw_device *rds_iwdev)
  268. {
  269. __rds_iw_destroy_conns(&rds_iwdev->conn_list, &rds_iwdev->spinlock);
  270. }
  271. struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *);
  272. void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo);
  273. void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *);
  274. void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
  275. struct rds_sock *rs, u32 *key_ret);
  276. void rds_iw_sync_mr(void *trans_private, int dir);
  277. void rds_iw_free_mr(void *trans_private, int invalidate);
  278. void rds_iw_flush_mrs(void);
  279. /* ib_recv.c */
  280. int rds_iw_recv_init(void);
  281. void rds_iw_recv_exit(void);
  282. int rds_iw_recv(struct rds_connection *conn);
  283. int rds_iw_recv_refill(struct rds_connection *conn, gfp_t kptr_gfp,
  284. gfp_t page_gfp, int prefill);
  285. void rds_iw_inc_free(struct rds_incoming *inc);
  286. int rds_iw_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
  287. void rds_iw_recv_cq_comp_handler(struct ib_cq *cq, void *context);
  288. void rds_iw_recv_tasklet_fn(unsigned long data);
  289. void rds_iw_recv_init_ring(struct rds_iw_connection *ic);
  290. void rds_iw_recv_clear_ring(struct rds_iw_connection *ic);
  291. void rds_iw_recv_init_ack(struct rds_iw_connection *ic);
  292. void rds_iw_attempt_ack(struct rds_iw_connection *ic);
  293. void rds_iw_ack_send_complete(struct rds_iw_connection *ic);
  294. u64 rds_iw_piggyb_ack(struct rds_iw_connection *ic);
  295. /* ib_ring.c */
  296. void rds_iw_ring_init(struct rds_iw_work_ring *ring, u32 nr);
  297. void rds_iw_ring_resize(struct rds_iw_work_ring *ring, u32 nr);
  298. u32 rds_iw_ring_alloc(struct rds_iw_work_ring *ring, u32 val, u32 *pos);
  299. void rds_iw_ring_free(struct rds_iw_work_ring *ring, u32 val);
  300. void rds_iw_ring_unalloc(struct rds_iw_work_ring *ring, u32 val);
  301. int rds_iw_ring_empty(struct rds_iw_work_ring *ring);
  302. int rds_iw_ring_low(struct rds_iw_work_ring *ring);
  303. u32 rds_iw_ring_oldest(struct rds_iw_work_ring *ring);
  304. u32 rds_iw_ring_completed(struct rds_iw_work_ring *ring, u32 wr_id, u32 oldest);
  305. extern wait_queue_head_t rds_iw_ring_empty_wait;
  306. /* ib_send.c */
  307. void rds_iw_xmit_complete(struct rds_connection *conn);
  308. int rds_iw_xmit(struct rds_connection *conn, struct rds_message *rm,
  309. unsigned int hdr_off, unsigned int sg, unsigned int off);
  310. void rds_iw_send_cq_comp_handler(struct ib_cq *cq, void *context);
  311. void rds_iw_send_init_ring(struct rds_iw_connection *ic);
  312. void rds_iw_send_clear_ring(struct rds_iw_connection *ic);
  313. int rds_iw_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op);
  314. void rds_iw_send_add_credits(struct rds_connection *conn, unsigned int credits);
  315. void rds_iw_advertise_credits(struct rds_connection *conn, unsigned int posted);
  316. int rds_iw_send_grab_credits(struct rds_iw_connection *ic, u32 wanted,
  317. u32 *adv_credits, int need_posted, int max_posted);
  318. /* ib_stats.c */
  319. DECLARE_PER_CPU(struct rds_iw_statistics, rds_iw_stats);
  320. #define rds_iw_stats_inc(member) rds_stats_inc_which(rds_iw_stats, member)
  321. unsigned int rds_iw_stats_info_copy(struct rds_info_iterator *iter,
  322. unsigned int avail);
  323. /* ib_sysctl.c */
  324. int rds_iw_sysctl_init(void);
  325. void rds_iw_sysctl_exit(void);
  326. extern unsigned long rds_iw_sysctl_max_send_wr;
  327. extern unsigned long rds_iw_sysctl_max_recv_wr;
  328. extern unsigned long rds_iw_sysctl_max_unsig_wrs;
  329. extern unsigned long rds_iw_sysctl_max_unsig_bytes;
  330. extern unsigned long rds_iw_sysctl_max_recv_allocation;
  331. extern unsigned int rds_iw_sysctl_flow_control;
  332. /*
  333. * Helper functions for getting/setting the header and data SGEs in
  334. * RDS packets (not RDMA)
  335. */
  336. static inline struct ib_sge *
  337. rds_iw_header_sge(struct rds_iw_connection *ic, struct ib_sge *sge)
  338. {
  339. return &sge[0];
  340. }
  341. static inline struct ib_sge *
  342. rds_iw_data_sge(struct rds_iw_connection *ic, struct ib_sge *sge)
  343. {
  344. return &sge[1];
  345. }
  346. #endif