4 #include <rdma/ib_verbs.h>
5 #include <rdma/rdma_cm.h>
6 #include <linux/interrupt.h>
8 #include <linux/slab.h>
10 #include "rdma_transport.h"
12 #define RDS_FMR_SIZE 256
13 #define RDS_FMR_POOL_SIZE 8192
15 #define RDS_IB_MAX_SGE 8
16 #define RDS_IB_RECV_SGE 2
18 #define RDS_IB_DEFAULT_RECV_WR 1024
19 #define RDS_IB_DEFAULT_SEND_WR 256
21 #define RDS_IB_DEFAULT_RETRY_COUNT 2
23 #define RDS_IB_SUPPORTED_PROTOCOLS 0x00000003 /* minor versions supported */
25 #define RDS_IB_RECYCLE_BATCH_COUNT 32
27 #define RDS_IB_WC_MAX 32
29 extern struct rw_semaphore rds_ib_devices_lock
;
30 extern struct list_head rds_ib_devices
;
33 * IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to
34 * try and minimize the amount of memory tied up both the device and
35 * socket receive queues.
37 struct rds_page_frag
{
38 struct list_head f_item
;
39 struct list_head f_cache_entry
;
40 struct scatterlist f_sg
;
43 struct rds_ib_incoming
{
44 struct list_head ii_frags
;
45 struct list_head ii_cache_entry
;
46 struct rds_incoming ii_inc
;
49 struct rds_ib_cache_head
{
50 struct list_head
*first
;
54 struct rds_ib_refill_cache
{
55 struct rds_ib_cache_head __percpu
*percpu
;
56 struct list_head
*xfer
;
57 struct list_head
*ready
;
60 struct rds_ib_connect_private
{
61 /* Add new fields at the end, and don't permute existing fields. */
66 __be16 dp_protocol_minor_mask
; /* bitmask */
69 __be32 dp_credit
; /* non-zero enables flow ctl */
72 struct rds_ib_send_work
{
74 struct ib_send_wr s_wr
;
75 struct ib_sge s_sge
[RDS_IB_MAX_SGE
];
76 unsigned long s_queued
;
79 struct rds_ib_recv_work
{
80 struct rds_ib_incoming
*r_ibinc
;
81 struct rds_page_frag
*r_frag
;
82 struct ib_recv_wr r_wr
;
83 struct ib_sge r_sge
[2];
86 struct rds_ib_work_ring
{
94 /* Rings are posted with all the allocations they'll need to queue the
95 * incoming message to the receiving socket so this can't fail.
96 * All fragments start with a header, so we can make sure we're not receiving
97 * garbage, and we can tell a small 8 byte fragment from an ACK frame.
99 struct rds_ib_ack_state
{
102 unsigned int ack_required
:1;
103 unsigned int ack_next_valid
:1;
104 unsigned int ack_recv_valid
:1;
108 struct rds_ib_device
;
110 struct rds_ib_connection
{
112 struct list_head ib_node
;
113 struct rds_ib_device
*rds_ibdev
;
114 struct rds_connection
*conn
;
116 /* alphabet soup, IBTA style */
117 struct rdma_cm_id
*i_cm_id
;
119 struct ib_cq
*i_send_cq
;
120 struct ib_cq
*i_recv_cq
;
121 struct ib_wc i_recv_wc
[RDS_IB_WC_MAX
];
123 /* interrupt handling */
124 struct tasklet_struct i_recv_tasklet
;
127 struct rds_ib_work_ring i_send_ring
;
128 struct rm_data_op
*i_data_op
;
129 struct rds_header
*i_send_hdrs
;
131 struct rds_ib_send_work
*i_sends
;
132 atomic_t i_signaled_sends
;
135 struct mutex i_recv_mutex
;
136 struct rds_ib_work_ring i_recv_ring
;
137 struct rds_ib_incoming
*i_ibinc
;
139 struct rds_header
*i_recv_hdrs
;
141 struct rds_ib_recv_work
*i_recvs
;
142 u64 i_ack_recv
; /* last ACK received */
143 struct rds_ib_refill_cache i_cache_incs
;
144 struct rds_ib_refill_cache i_cache_frags
;
147 unsigned long i_ack_flags
;
148 #ifdef KERNEL_HAS_ATOMIC64
149 atomic64_t i_ack_next
; /* next ACK to send */
151 spinlock_t i_ack_lock
; /* protect i_ack_next */
152 u64 i_ack_next
; /* next ACK to send */
154 struct rds_header
*i_ack
;
155 struct ib_send_wr i_ack_wr
;
156 struct ib_sge i_ack_sge
;
158 unsigned long i_ack_queued
;
160 /* Flow control related information
162 * Our algorithm uses a pair variables that we need to access
163 * atomically - one for the send credits, and one posted
164 * recv credits we need to transfer to remote.
165 * Rather than protect them using a slow spinlock, we put both into
166 * a single atomic_t and update it using cmpxchg
170 /* Protocol version specific information */
171 unsigned int i_flowctl
:1; /* enable/disable flow ctl */
173 /* Batched completions */
174 unsigned int i_unsignaled_wrs
;
177 /* This assumes that atomic_t is at least 32 bits */
178 #define IB_GET_SEND_CREDITS(v) ((v) & 0xffff)
179 #define IB_GET_POST_CREDITS(v) ((v) >> 16)
180 #define IB_SET_SEND_CREDITS(v) ((v) & 0xffff)
181 #define IB_SET_POST_CREDITS(v) ((v) << 16)
183 struct rds_ib_ipaddr
{
184 struct list_head list
;
189 struct rds_ib_device
{
190 struct list_head list
;
191 struct list_head ipaddr_list
;
192 struct list_head conn_list
;
193 struct ib_device
*dev
;
195 struct rds_ib_mr_pool
*mr_pool
;
196 unsigned int fmr_max_remaps
;
197 unsigned int max_fmrs
;
199 unsigned int max_wrs
;
200 unsigned int max_initiator_depth
;
201 unsigned int max_responder_resources
;
202 spinlock_t spinlock
; /* protect the above */
204 struct work_struct free_work
;
207 #define ibdev_to_node(ibdev) dev_to_node(ibdev->dma_device)
208 #define rdsibdev_to_node(rdsibdev) ibdev_to_node(rdsibdev->dev)
210 /* bits for i_ack_flags */
211 #define IB_ACK_IN_FLIGHT 0
212 #define IB_ACK_REQUESTED 1
214 /* Magic WR_ID for ACKs */
215 #define RDS_IB_ACK_WR_ID (~(u64) 0)
217 struct rds_ib_statistics
{
218 uint64_t s_ib_connect_raced
;
219 uint64_t s_ib_listen_closed_stale
;
220 uint64_t s_ib_tx_cq_call
;
221 uint64_t s_ib_evt_handler_call
;
222 uint64_t s_ib_tasklet_call
;
223 uint64_t s_ib_tx_cq_event
;
224 uint64_t s_ib_tx_ring_full
;
225 uint64_t s_ib_tx_throttle
;
226 uint64_t s_ib_tx_sg_mapping_failure
;
227 uint64_t s_ib_tx_stalled
;
228 uint64_t s_ib_tx_credit_updates
;
229 uint64_t s_ib_rx_cq_event
;
230 uint64_t s_ib_rx_ring_empty
;
231 uint64_t s_ib_rx_refill_from_cq
;
232 uint64_t s_ib_rx_refill_from_thread
;
233 uint64_t s_ib_rx_alloc_limit
;
234 uint64_t s_ib_rx_credit_updates
;
235 uint64_t s_ib_ack_sent
;
236 uint64_t s_ib_ack_send_failure
;
237 uint64_t s_ib_ack_send_delayed
;
238 uint64_t s_ib_ack_send_piggybacked
;
239 uint64_t s_ib_ack_received
;
240 uint64_t s_ib_rdma_mr_alloc
;
241 uint64_t s_ib_rdma_mr_free
;
242 uint64_t s_ib_rdma_mr_used
;
243 uint64_t s_ib_rdma_mr_pool_flush
;
244 uint64_t s_ib_rdma_mr_pool_wait
;
245 uint64_t s_ib_rdma_mr_pool_depleted
;
246 uint64_t s_ib_atomic_cswp
;
247 uint64_t s_ib_atomic_fadd
;
250 extern struct workqueue_struct
*rds_ib_wq
;
253 * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
256 static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device
*dev
,
257 struct scatterlist
*sglist
,
258 unsigned int sg_dma_len
,
261 struct scatterlist
*sg
;
264 for_each_sg(sglist
, sg
, sg_dma_len
, i
) {
265 ib_dma_sync_single_for_cpu(dev
,
266 ib_sg_dma_address(dev
, sg
),
267 ib_sg_dma_len(dev
, sg
),
271 #define ib_dma_sync_sg_for_cpu rds_ib_dma_sync_sg_for_cpu
273 static inline void rds_ib_dma_sync_sg_for_device(struct ib_device
*dev
,
274 struct scatterlist
*sglist
,
275 unsigned int sg_dma_len
,
278 struct scatterlist
*sg
;
281 for_each_sg(sglist
, sg
, sg_dma_len
, i
) {
282 ib_dma_sync_single_for_device(dev
,
283 ib_sg_dma_address(dev
, sg
),
284 ib_sg_dma_len(dev
, sg
),
288 #define ib_dma_sync_sg_for_device rds_ib_dma_sync_sg_for_device
292 extern struct rds_transport rds_ib_transport
;
293 struct rds_ib_device
*rds_ib_get_client_data(struct ib_device
*device
);
294 void rds_ib_dev_put(struct rds_ib_device
*rds_ibdev
);
295 extern struct ib_client rds_ib_client
;
297 extern unsigned int fmr_message_size
;
298 extern unsigned int rds_ib_retry_count
;
300 extern spinlock_t ib_nodev_conns_lock
;
301 extern struct list_head ib_nodev_conns
;
304 int rds_ib_conn_alloc(struct rds_connection
*conn
, gfp_t gfp
);
305 void rds_ib_conn_free(void *arg
);
306 int rds_ib_conn_connect(struct rds_connection
*conn
);
307 void rds_ib_conn_shutdown(struct rds_connection
*conn
);
308 void rds_ib_state_change(struct sock
*sk
);
309 int rds_ib_listen_init(void);
310 void rds_ib_listen_stop(void);
311 void __rds_ib_conn_error(struct rds_connection
*conn
, const char *, ...);
312 int rds_ib_cm_handle_connect(struct rdma_cm_id
*cm_id
,
313 struct rdma_cm_event
*event
);
314 int rds_ib_cm_initiate_connect(struct rdma_cm_id
*cm_id
);
315 void rds_ib_cm_connect_complete(struct rds_connection
*conn
,
316 struct rdma_cm_event
*event
);
319 #define rds_ib_conn_error(conn, fmt...) \
320 __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
323 int rds_ib_update_ipaddr(struct rds_ib_device
*rds_ibdev
, __be32 ipaddr
);
324 void rds_ib_add_conn(struct rds_ib_device
*rds_ibdev
, struct rds_connection
*conn
);
325 void rds_ib_remove_conn(struct rds_ib_device
*rds_ibdev
, struct rds_connection
*conn
);
326 void rds_ib_destroy_nodev_conns(void);
327 struct rds_ib_mr_pool
*rds_ib_create_mr_pool(struct rds_ib_device
*);
328 void rds_ib_get_mr_info(struct rds_ib_device
*rds_ibdev
, struct rds_info_rdma_connection
*iinfo
);
329 void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool
*);
330 void *rds_ib_get_mr(struct scatterlist
*sg
, unsigned long nents
,
331 struct rds_sock
*rs
, u32
*key_ret
);
332 void rds_ib_sync_mr(void *trans_private
, int dir
);
333 void rds_ib_free_mr(void *trans_private
, int invalidate
);
334 void rds_ib_flush_mrs(void);
335 int rds_ib_fmr_init(void);
336 void rds_ib_fmr_exit(void);
339 int rds_ib_recv_init(void);
340 void rds_ib_recv_exit(void);
341 int rds_ib_recv(struct rds_connection
*conn
);
342 int rds_ib_recv_alloc_caches(struct rds_ib_connection
*ic
);
343 void rds_ib_recv_free_caches(struct rds_ib_connection
*ic
);
344 void rds_ib_recv_refill(struct rds_connection
*conn
, int prefill
, gfp_t gfp
);
345 void rds_ib_inc_free(struct rds_incoming
*inc
);
346 int rds_ib_inc_copy_to_user(struct rds_incoming
*inc
, struct iov_iter
*to
);
347 void rds_ib_recv_cqe_handler(struct rds_ib_connection
*ic
, struct ib_wc
*wc
,
348 struct rds_ib_ack_state
*state
);
349 void rds_ib_recv_tasklet_fn(unsigned long data
);
350 void rds_ib_recv_init_ring(struct rds_ib_connection
*ic
);
351 void rds_ib_recv_clear_ring(struct rds_ib_connection
*ic
);
352 void rds_ib_recv_init_ack(struct rds_ib_connection
*ic
);
353 void rds_ib_attempt_ack(struct rds_ib_connection
*ic
);
354 void rds_ib_ack_send_complete(struct rds_ib_connection
*ic
);
355 u64
rds_ib_piggyb_ack(struct rds_ib_connection
*ic
);
356 void rds_ib_set_ack(struct rds_ib_connection
*ic
, u64 seq
, int ack_required
);
359 void rds_ib_ring_init(struct rds_ib_work_ring
*ring
, u32 nr
);
360 void rds_ib_ring_resize(struct rds_ib_work_ring
*ring
, u32 nr
);
361 u32
rds_ib_ring_alloc(struct rds_ib_work_ring
*ring
, u32 val
, u32
*pos
);
362 void rds_ib_ring_free(struct rds_ib_work_ring
*ring
, u32 val
);
363 void rds_ib_ring_unalloc(struct rds_ib_work_ring
*ring
, u32 val
);
364 int rds_ib_ring_empty(struct rds_ib_work_ring
*ring
);
365 int rds_ib_ring_low(struct rds_ib_work_ring
*ring
);
366 u32
rds_ib_ring_oldest(struct rds_ib_work_ring
*ring
);
367 u32
rds_ib_ring_completed(struct rds_ib_work_ring
*ring
, u32 wr_id
, u32 oldest
);
368 extern wait_queue_head_t rds_ib_ring_empty_wait
;
371 void rds_ib_xmit_complete(struct rds_connection
*conn
);
372 int rds_ib_xmit(struct rds_connection
*conn
, struct rds_message
*rm
,
373 unsigned int hdr_off
, unsigned int sg
, unsigned int off
);
374 void rds_ib_send_cq_comp_handler(struct ib_cq
*cq
, void *context
);
375 void rds_ib_send_init_ring(struct rds_ib_connection
*ic
);
376 void rds_ib_send_clear_ring(struct rds_ib_connection
*ic
);
377 int rds_ib_xmit_rdma(struct rds_connection
*conn
, struct rm_rdma_op
*op
);
378 void rds_ib_send_add_credits(struct rds_connection
*conn
, unsigned int credits
);
379 void rds_ib_advertise_credits(struct rds_connection
*conn
, unsigned int posted
);
380 int rds_ib_send_grab_credits(struct rds_ib_connection
*ic
, u32 wanted
,
381 u32
*adv_credits
, int need_posted
, int max_posted
);
382 int rds_ib_xmit_atomic(struct rds_connection
*conn
, struct rm_atomic_op
*op
);
385 DECLARE_PER_CPU(struct rds_ib_statistics
, rds_ib_stats
);
386 #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
387 unsigned int rds_ib_stats_info_copy(struct rds_info_iterator
*iter
,
391 int rds_ib_sysctl_init(void);
392 void rds_ib_sysctl_exit(void);
393 extern unsigned long rds_ib_sysctl_max_send_wr
;
394 extern unsigned long rds_ib_sysctl_max_recv_wr
;
395 extern unsigned long rds_ib_sysctl_max_unsig_wrs
;
396 extern unsigned long rds_ib_sysctl_max_unsig_bytes
;
397 extern unsigned long rds_ib_sysctl_max_recv_allocation
;
398 extern unsigned int rds_ib_sysctl_flow_control
;