iw.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #ifdef KERNEL_HAS_ATOMIC64
  112. atomic64_t i_ack_next; /* next ACK to send */
  113. #else
  114. spinlock_t i_ack_lock; /* protect i_ack_next */
  115. u64 i_ack_next; /* next ACK to send */
  116. #endif
  117. struct rds_header *i_ack;
  118. struct ib_send_wr i_ack_wr;
  119. struct ib_sge i_ack_sge;
  120. u64 i_ack_dma;
  121. unsigned long i_ack_queued;
  122. /* Flow control related information
  123. *
  124. * Our algorithm uses a pair variables that we need to access
  125. * atomically - one for the send credits, and one posted
  126. * recv credits we need to transfer to remote.
  127. * Rather than protect them using a slow spinlock, we put both into
  128. * a single atomic_t and update it using cmpxchg
  129. */
  130. atomic_t i_credits;
  131. /* Protocol version specific information */
  132. unsigned int i_flowctl:1; /* enable/disable flow ctl */
  133. unsigned int i_dma_local_lkey:1;
  134. unsigned int i_fastreg_posted:1; /* fastreg posted on this connection */
  135. /* Batched completions */
  136. unsigned int i_unsignaled_wrs;
  137. long i_unsignaled_bytes;
  138. };
  139. /* This assumes that atomic_t is at least 32 bits */
  140. #define IB_GET_SEND_CREDITS(v) ((v) & 0xffff)
  141. #define IB_GET_POST_CREDITS(v) ((v) >> 16)
  142. #define IB_SET_SEND_CREDITS(v) ((v) & 0xffff)
  143. #define IB_SET_POST_CREDITS(v) ((v) << 16)
  144. struct rds_iw_cm_id {
  145. struct list_head list;
  146. struct rdma_cm_id *cm_id;
  147. };
  148. struct rds_iw_device {
  149. struct list_head list;
  150. struct list_head cm_id_list;
  151. struct list_head conn_list;
  152. struct ib_device *dev;
  153. struct ib_pd *pd;
  154. struct ib_mr *mr;
  155. struct rds_iw_mr_pool *mr_pool;
  156. int page_shift;
  157. int max_sge;
  158. unsigned int max_wrs;
  159. unsigned int dma_local_lkey:1;
  160. spinlock_t spinlock; /* protect the above */
  161. };
  162. /* bits for i_ack_flags */
  163. #define IB_ACK_IN_FLIGHT 0
  164. #define IB_ACK_REQUESTED 1
  165. /* Magic WR_ID for ACKs */
  166. #define RDS_IW_ACK_WR_ID ((u64)0xffffffffffffffffULL)
  167. #define RDS_IW_FAST_REG_WR_ID ((u64)0xefefefefefefefefULL)
  168. #define RDS_IW_LOCAL_INV_WR_ID ((u64)0xdfdfdfdfdfdfdfdfULL)
  169. struct rds_iw_statistics {
  170. uint64_t s_iw_connect_raced;
  171. uint64_t s_iw_listen_closed_stale;
  172. uint64_t s_iw_tx_cq_call;
  173. uint64_t s_iw_tx_cq_event;
  174. uint64_t s_iw_tx_ring_full;
  175. uint64_t s_iw_tx_throttle;
  176. uint64_t s_iw_tx_sg_mapping_failure;
  177. uint64_t s_iw_tx_stalled;
  178. uint64_t s_iw_tx_credit_updates;
  179. uint64_t s_iw_rx_cq_call;
  180. uint64_t s_iw_rx_cq_event;
  181. uint64_t s_iw_rx_ring_empty;
  182. uint64_t s_iw_rx_refill_from_cq;
  183. uint64_t s_iw_rx_refill_from_thread;
  184. uint64_t s_iw_rx_alloc_limit;
  185. uint64_t s_iw_rx_credit_updates;
  186. uint64_t s_iw_ack_sent;
  187. uint64_t s_iw_ack_send_failure;
  188. uint64_t s_iw_ack_send_delayed;
  189. uint64_t s_iw_ack_send_piggybacked;
  190. uint64_t s_iw_ack_received;
  191. uint64_t s_iw_rdma_mr_alloc;
  192. uint64_t s_iw_rdma_mr_free;
  193. uint64_t s_iw_rdma_mr_used;
  194. uint64_t s_iw_rdma_mr_pool_flush;
  195. uint64_t s_iw_rdma_mr_pool_wait;
  196. uint64_t s_iw_rdma_mr_pool_depleted;
  197. };
  198. extern struct workqueue_struct *rds_iw_wq;
  199. /*
  200. * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
  201. * doesn't define it.
  202. */
  203. static inline void rds_iw_dma_sync_sg_for_cpu(struct ib_device *dev,
  204. struct scatterlist *sg, unsigned int sg_dma_len, int direction)
  205. {
  206. unsigned int i;
  207. for (i = 0; i < sg_dma_len; ++i) {
  208. ib_dma_sync_single_for_cpu(dev,
  209. ib_sg_dma_address(dev, &sg[i]),
  210. ib_sg_dma_len(dev, &sg[i]),
  211. direction);
  212. }
  213. }
  214. #define ib_dma_sync_sg_for_cpu rds_iw_dma_sync_sg_for_cpu
  215. static inline void rds_iw_dma_sync_sg_for_device(struct ib_device *dev,
  216. struct scatterlist *sg, unsigned int sg_dma_len, int direction)
  217. {
  218. unsigned int i;
  219. for (i = 0; i < sg_dma_len; ++i) {
  220. ib_dma_sync_single_for_device(dev,
  221. ib_sg_dma_address(dev, &sg[i]),
  222. ib_sg_dma_len(dev, &sg[i]),
  223. direction);
  224. }
  225. }
  226. #define ib_dma_sync_sg_for_device rds_iw_dma_sync_sg_for_device
  227. static inline u32 rds_iw_local_dma_lkey(struct rds_iw_connection *ic)
  228. {
  229. return ic->i_dma_local_lkey ? ic->i_cm_id->device->local_dma_lkey : ic->i_mr->lkey;
  230. }
  231. /* ib.c */
  232. extern struct rds_transport rds_iw_transport;
  233. extern void rds_iw_add_one(struct ib_device *device);
  234. extern void rds_iw_remove_one(struct ib_device *device);
  235. extern struct ib_client rds_iw_client;
  236. extern unsigned int fastreg_pool_size;
  237. extern unsigned int fastreg_message_size;
  238. extern spinlock_t iw_nodev_conns_lock;
  239. extern struct list_head iw_nodev_conns;
  240. /* ib_cm.c */
  241. int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp);
  242. void rds_iw_conn_free(void *arg);
  243. int rds_iw_conn_connect(struct rds_connection *conn);
  244. void rds_iw_conn_shutdown(struct rds_connection *conn);
  245. void rds_iw_state_change(struct sock *sk);
  246. int __init rds_iw_listen_init(void);
  247. void rds_iw_listen_stop(void);
  248. void __rds_iw_conn_error(struct rds_connection *conn, const char *, ...);
  249. int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id,
  250. struct rdma_cm_event *event);
  251. int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id);
  252. void rds_iw_cm_connect_complete(struct rds_connection *conn,
  253. struct rdma_cm_event *event);
  254. #define rds_iw_conn_error(conn, fmt...) \
  255. __rds_iw_conn_error(conn, KERN_WARNING "RDS/IW: " fmt)
  256. /* ib_rdma.c */
  257. int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id);
  258. void rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn);
  259. void rds_iw_remove_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn);
  260. void __rds_iw_destroy_conns(struct list_head *list, spinlock_t *list_lock);
  261. static inline void rds_iw_destroy_nodev_conns(void)
  262. {
  263. __rds_iw_destroy_conns(&iw_nodev_conns, &iw_nodev_conns_lock);
  264. }
  265. static inline void rds_iw_destroy_conns(struct rds_iw_device *rds_iwdev)
  266. {
  267. __rds_iw_destroy_conns(&rds_iwdev->conn_list, &rds_iwdev->spinlock);
  268. }
  269. struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *);
  270. void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo);
  271. void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *);
  272. void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
  273. struct rds_sock *rs, u32 *key_ret);
  274. void rds_iw_sync_mr(void *trans_private, int dir);
  275. void rds_iw_free_mr(void *trans_private, int invalidate);
  276. void rds_iw_flush_mrs(void);
  277. void rds_iw_remove_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id);
  278. /* ib_recv.c */
  279. int __init rds_iw_recv_init(void);
  280. void rds_iw_recv_exit(void);
  281. int rds_iw_recv(struct rds_connection *conn);
  282. int rds_iw_recv_refill(struct rds_connection *conn, gfp_t kptr_gfp,
  283. gfp_t page_gfp, int prefill);
  284. void rds_iw_inc_purge(struct rds_incoming *inc);
  285. void rds_iw_inc_free(struct rds_incoming *inc);
  286. int rds_iw_inc_copy_to_user(struct rds_incoming *inc, struct iovec *iov,
  287. size_t size);
  288. void rds_iw_recv_cq_comp_handler(struct ib_cq *cq, void *context);
  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 rds_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);
  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 __init 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. extern ctl_table rds_iw_sysctl_table[];
  333. /*
  334. * Helper functions for getting/setting the header and data SGEs in
  335. * RDS packets (not RDMA)
  336. */
  337. static inline struct ib_sge *
  338. rds_iw_header_sge(struct rds_iw_connection *ic, struct ib_sge *sge)
  339. {
  340. return &sge[0];
  341. }
  342. static inline struct ib_sge *
  343. rds_iw_data_sge(struct rds_iw_connection *ic, struct ib_sge *sge)
  344. {
  345. return &sge[1];
  346. }
  347. #endif