iw.h 12 KB

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