iw.h 12 KB

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