1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <rdma/ib_verbs.h>
6 #include <rdma/rdma_cm.h>
7 #include <linux/interrupt.h>
9 #include <linux/slab.h>
11 #include "rdma_transport.h"
13 #define RDS_IB_MAX_SGE 8
14 #define RDS_IB_RECV_SGE 2
16 #define RDS_IB_DEFAULT_RECV_WR 1024
17 #define RDS_IB_DEFAULT_SEND_WR 256
18 #define RDS_IB_DEFAULT_FR_WR 512
20 #define RDS_IB_DEFAULT_RETRY_COUNT 1
22 #define RDS_IB_SUPPORTED_PROTOCOLS 0x00000003 /* minor versions supported */
24 #define RDS_IB_RECYCLE_BATCH_COUNT 32
26 #define RDS_IB_WC_MAX 32
28 extern struct rw_semaphore rds_ib_devices_lock
;
29 extern struct list_head rds_ib_devices
;
32 * IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to
33 * try and minimize the amount of memory tied up both the device and
34 * socket receive queues.
36 struct rds_page_frag
{
37 struct list_head f_item
;
38 struct list_head f_cache_entry
;
39 struct scatterlist f_sg
;
42 struct rds_ib_incoming
{
43 struct list_head ii_frags
;
44 struct list_head ii_cache_entry
;
45 struct rds_incoming ii_inc
;
48 struct rds_ib_cache_head
{
49 struct list_head
*first
;
53 struct rds_ib_refill_cache
{
54 struct rds_ib_cache_head __percpu
*percpu
;
55 struct list_head
*xfer
;
56 struct list_head
*ready
;
59 /* This is the common structure for the IB private data exchange in setting up
60 * an RDS connection. The exchange is different for IPv4 and IPv6 connections.
61 * The reason is that the address size is different and the addresses
62 * exchanged are in the beginning of the structure. Hence it is not possible
63 * for interoperability if same structure is used.
65 struct rds_ib_conn_priv_cmn
{
66 u8 ricpc_protocol_major
;
67 u8 ricpc_protocol_minor
;
68 __be16 ricpc_protocol_minor_mask
; /* bitmask */
71 __be16 ripc_reserved2
;
73 __be32 ricpc_credit
; /* non-zero enables flow ctl */
76 struct rds_ib_connect_private
{
77 /* Add new fields at the end, and don't permute existing fields. */
80 struct rds_ib_conn_priv_cmn dp_cmn
;
83 struct rds6_ib_connect_private
{
84 /* Add new fields at the end, and don't permute existing fields. */
85 struct in6_addr dp_saddr
;
86 struct in6_addr dp_daddr
;
87 struct rds_ib_conn_priv_cmn dp_cmn
;
90 #define dp_protocol_major dp_cmn.ricpc_protocol_major
91 #define dp_protocol_minor dp_cmn.ricpc_protocol_minor
92 #define dp_protocol_minor_mask dp_cmn.ricpc_protocol_minor_mask
93 #define dp_ack_seq dp_cmn.ricpc_ack_seq
94 #define dp_credit dp_cmn.ricpc_credit
96 union rds_ib_conn_priv
{
97 struct rds_ib_connect_private ricp_v4
;
98 struct rds6_ib_connect_private ricp_v6
;
101 struct rds_ib_send_work
{
104 struct ib_send_wr s_wr
;
105 struct ib_rdma_wr s_rdma_wr
;
106 struct ib_atomic_wr s_atomic_wr
;
108 struct ib_sge s_sge
[RDS_IB_MAX_SGE
];
109 unsigned long s_queued
;
112 struct rds_ib_recv_work
{
113 struct rds_ib_incoming
*r_ibinc
;
114 struct rds_page_frag
*r_frag
;
115 struct ib_recv_wr r_wr
;
116 struct ib_sge r_sge
[2];
119 struct rds_ib_work_ring
{
127 /* Rings are posted with all the allocations they'll need to queue the
128 * incoming message to the receiving socket so this can't fail.
129 * All fragments start with a header, so we can make sure we're not receiving
130 * garbage, and we can tell a small 8 byte fragment from an ACK frame.
132 struct rds_ib_ack_state
{
135 unsigned int ack_required
:1;
136 unsigned int ack_next_valid
:1;
137 unsigned int ack_recv_valid
:1;
141 struct rds_ib_device
;
143 struct rds_ib_connection
{
145 struct list_head ib_node
;
146 struct rds_ib_device
*rds_ibdev
;
147 struct rds_connection
*conn
;
149 /* alphabet soup, IBTA style */
150 struct rdma_cm_id
*i_cm_id
;
152 struct ib_cq
*i_send_cq
;
153 struct ib_cq
*i_recv_cq
;
154 struct ib_wc i_send_wc
[RDS_IB_WC_MAX
];
155 struct ib_wc i_recv_wc
[RDS_IB_WC_MAX
];
157 /* To control the number of wrs from fastreg */
158 atomic_t i_fastreg_wrs
;
159 atomic_t i_fastreg_inuse_count
;
161 /* interrupt handling */
162 struct tasklet_struct i_send_tasklet
;
163 struct tasklet_struct i_recv_tasklet
;
166 struct rds_ib_work_ring i_send_ring
;
167 struct rm_data_op
*i_data_op
;
168 struct rds_header
**i_send_hdrs
;
169 dma_addr_t
*i_send_hdrs_dma
;
170 struct rds_ib_send_work
*i_sends
;
171 atomic_t i_signaled_sends
;
174 struct mutex i_recv_mutex
;
175 struct rds_ib_work_ring i_recv_ring
;
176 struct rds_ib_incoming
*i_ibinc
;
178 struct rds_header
**i_recv_hdrs
;
179 dma_addr_t
*i_recv_hdrs_dma
;
180 struct rds_ib_recv_work
*i_recvs
;
181 u64 i_ack_recv
; /* last ACK received */
182 struct rds_ib_refill_cache i_cache_incs
;
183 struct rds_ib_refill_cache i_cache_frags
;
184 atomic_t i_cache_allocs
;
187 unsigned long i_ack_flags
;
188 #ifdef KERNEL_HAS_ATOMIC64
189 atomic64_t i_ack_next
; /* next ACK to send */
191 spinlock_t i_ack_lock
; /* protect i_ack_next */
192 u64 i_ack_next
; /* next ACK to send */
194 struct rds_header
*i_ack
;
195 struct ib_send_wr i_ack_wr
;
196 struct ib_sge i_ack_sge
;
197 dma_addr_t i_ack_dma
;
198 unsigned long i_ack_queued
;
200 /* Flow control related information
202 * Our algorithm uses a pair variables that we need to access
203 * atomically - one for the send credits, and one posted
204 * recv credits we need to transfer to remote.
205 * Rather than protect them using a slow spinlock, we put both into
206 * a single atomic_t and update it using cmpxchg
210 /* Protocol version specific information */
211 unsigned int i_flowctl
:1; /* enable/disable flow ctl */
213 /* Batched completions */
214 unsigned int i_unsignaled_wrs
;
216 /* Endpoint role in connection */
218 atomic_t i_cq_quiesce
;
220 /* Send/Recv vectors */
226 /* This assumes that atomic_t is at least 32 bits */
227 #define IB_GET_SEND_CREDITS(v) ((v) & 0xffff)
228 #define IB_GET_POST_CREDITS(v) ((v) >> 16)
229 #define IB_SET_SEND_CREDITS(v) ((v) & 0xffff)
230 #define IB_SET_POST_CREDITS(v) ((v) << 16)
232 struct rds_ib_ipaddr
{
233 struct list_head list
;
243 struct rds_ib_device
{
244 struct list_head list
;
245 struct list_head ipaddr_list
;
246 struct list_head conn_list
;
247 struct ib_device
*dev
;
249 struct dma_pool
*rid_hdrs_pool
; /* RDS headers DMA pool */
252 unsigned int max_mrs
;
253 struct rds_ib_mr_pool
*mr_1m_pool
;
254 struct rds_ib_mr_pool
*mr_8k_pool
;
255 unsigned int fmr_max_remaps
;
256 unsigned int max_8k_mrs
;
257 unsigned int max_1m_mrs
;
259 unsigned int max_wrs
;
260 unsigned int max_initiator_depth
;
261 unsigned int max_responder_resources
;
262 spinlock_t spinlock
; /* protect the above */
264 struct work_struct free_work
;
268 #define ibdev_to_node(ibdev) dev_to_node((ibdev)->dev.parent)
269 #define rdsibdev_to_node(rdsibdev) ibdev_to_node(rdsibdev->dev)
271 /* bits for i_ack_flags */
272 #define IB_ACK_IN_FLIGHT 0
273 #define IB_ACK_REQUESTED 1
275 /* Magic WR_ID for ACKs */
276 #define RDS_IB_ACK_WR_ID (~(u64) 0)
278 struct rds_ib_statistics
{
279 uint64_t s_ib_connect_raced
;
280 uint64_t s_ib_listen_closed_stale
;
281 uint64_t s_ib_evt_handler_call
;
282 uint64_t s_ib_tasklet_call
;
283 uint64_t s_ib_tx_cq_event
;
284 uint64_t s_ib_tx_ring_full
;
285 uint64_t s_ib_tx_throttle
;
286 uint64_t s_ib_tx_sg_mapping_failure
;
287 uint64_t s_ib_tx_stalled
;
288 uint64_t s_ib_tx_credit_updates
;
289 uint64_t s_ib_rx_cq_event
;
290 uint64_t s_ib_rx_ring_empty
;
291 uint64_t s_ib_rx_refill_from_cq
;
292 uint64_t s_ib_rx_refill_from_thread
;
293 uint64_t s_ib_rx_alloc_limit
;
294 uint64_t s_ib_rx_total_frags
;
295 uint64_t s_ib_rx_total_incs
;
296 uint64_t s_ib_rx_credit_updates
;
297 uint64_t s_ib_ack_sent
;
298 uint64_t s_ib_ack_send_failure
;
299 uint64_t s_ib_ack_send_delayed
;
300 uint64_t s_ib_ack_send_piggybacked
;
301 uint64_t s_ib_ack_received
;
302 uint64_t s_ib_rdma_mr_8k_alloc
;
303 uint64_t s_ib_rdma_mr_8k_free
;
304 uint64_t s_ib_rdma_mr_8k_used
;
305 uint64_t s_ib_rdma_mr_8k_pool_flush
;
306 uint64_t s_ib_rdma_mr_8k_pool_wait
;
307 uint64_t s_ib_rdma_mr_8k_pool_depleted
;
308 uint64_t s_ib_rdma_mr_1m_alloc
;
309 uint64_t s_ib_rdma_mr_1m_free
;
310 uint64_t s_ib_rdma_mr_1m_used
;
311 uint64_t s_ib_rdma_mr_1m_pool_flush
;
312 uint64_t s_ib_rdma_mr_1m_pool_wait
;
313 uint64_t s_ib_rdma_mr_1m_pool_depleted
;
314 uint64_t s_ib_rdma_mr_8k_reused
;
315 uint64_t s_ib_rdma_mr_1m_reused
;
316 uint64_t s_ib_atomic_cswp
;
317 uint64_t s_ib_atomic_fadd
;
318 uint64_t s_ib_recv_added_to_cache
;
319 uint64_t s_ib_recv_removed_from_cache
;
322 extern struct workqueue_struct
*rds_ib_wq
;
325 * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
328 static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device
*dev
,
329 struct scatterlist
*sglist
,
330 unsigned int sg_dma_len
,
333 struct scatterlist
*sg
;
336 for_each_sg(sglist
, sg
, sg_dma_len
, i
) {
337 ib_dma_sync_single_for_cpu(dev
, sg_dma_address(sg
),
338 sg_dma_len(sg
), direction
);
341 #define ib_dma_sync_sg_for_cpu rds_ib_dma_sync_sg_for_cpu
343 static inline void rds_ib_dma_sync_sg_for_device(struct ib_device
*dev
,
344 struct scatterlist
*sglist
,
345 unsigned int sg_dma_len
,
348 struct scatterlist
*sg
;
351 for_each_sg(sglist
, sg
, sg_dma_len
, i
) {
352 ib_dma_sync_single_for_device(dev
, sg_dma_address(sg
),
353 sg_dma_len(sg
), direction
);
356 #define ib_dma_sync_sg_for_device rds_ib_dma_sync_sg_for_device
360 extern struct rds_transport rds_ib_transport
;
361 struct rds_ib_device
*rds_ib_get_client_data(struct ib_device
*device
);
362 void rds_ib_dev_put(struct rds_ib_device
*rds_ibdev
);
363 extern struct ib_client rds_ib_client
;
365 extern unsigned int rds_ib_retry_count
;
367 extern spinlock_t ib_nodev_conns_lock
;
368 extern struct list_head ib_nodev_conns
;
371 int rds_ib_conn_alloc(struct rds_connection
*conn
, gfp_t gfp
);
372 void rds_ib_conn_free(void *arg
);
373 int rds_ib_conn_path_connect(struct rds_conn_path
*cp
);
374 void rds_ib_conn_path_shutdown(struct rds_conn_path
*cp
);
375 void rds_ib_state_change(struct sock
*sk
);
376 int rds_ib_listen_init(void);
377 void rds_ib_listen_stop(void);
379 void __rds_ib_conn_error(struct rds_connection
*conn
, const char *, ...);
380 int rds_ib_cm_handle_connect(struct rdma_cm_id
*cm_id
,
381 struct rdma_cm_event
*event
, bool isv6
);
382 int rds_ib_cm_initiate_connect(struct rdma_cm_id
*cm_id
, bool isv6
);
383 void rds_ib_cm_connect_complete(struct rds_connection
*conn
,
384 struct rdma_cm_event
*event
);
385 struct rds_header
**rds_dma_hdrs_alloc(struct ib_device
*ibdev
,
386 struct dma_pool
*pool
,
387 dma_addr_t
**dma_addrs
, u32 num_hdrs
);
388 void rds_dma_hdrs_free(struct dma_pool
*pool
, struct rds_header
**hdrs
,
389 dma_addr_t
*dma_addrs
, u32 num_hdrs
);
391 #define rds_ib_conn_error(conn, fmt...) \
392 __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
395 int rds_ib_update_ipaddr(struct rds_ib_device
*rds_ibdev
,
396 struct in6_addr
*ipaddr
);
397 void rds_ib_add_conn(struct rds_ib_device
*rds_ibdev
, struct rds_connection
*conn
);
398 void rds_ib_remove_conn(struct rds_ib_device
*rds_ibdev
, struct rds_connection
*conn
);
399 void rds_ib_destroy_nodev_conns(void);
400 void rds_ib_mr_cqe_handler(struct rds_ib_connection
*ic
, struct ib_wc
*wc
);
403 int rds_ib_recv_init(void);
404 void rds_ib_recv_exit(void);
405 int rds_ib_recv_path(struct rds_conn_path
*conn
);
406 int rds_ib_recv_alloc_caches(struct rds_ib_connection
*ic
, gfp_t gfp
);
407 void rds_ib_recv_free_caches(struct rds_ib_connection
*ic
);
408 void rds_ib_recv_refill(struct rds_connection
*conn
, int prefill
, gfp_t gfp
);
409 void rds_ib_inc_free(struct rds_incoming
*inc
);
410 int rds_ib_inc_copy_to_user(struct rds_incoming
*inc
, struct iov_iter
*to
);
411 void rds_ib_recv_cqe_handler(struct rds_ib_connection
*ic
, struct ib_wc
*wc
,
412 struct rds_ib_ack_state
*state
);
413 void rds_ib_recv_tasklet_fn(unsigned long data
);
414 void rds_ib_recv_init_ring(struct rds_ib_connection
*ic
);
415 void rds_ib_recv_clear_ring(struct rds_ib_connection
*ic
);
416 void rds_ib_recv_init_ack(struct rds_ib_connection
*ic
);
417 void rds_ib_attempt_ack(struct rds_ib_connection
*ic
);
418 void rds_ib_ack_send_complete(struct rds_ib_connection
*ic
);
419 u64
rds_ib_piggyb_ack(struct rds_ib_connection
*ic
);
420 void rds_ib_set_ack(struct rds_ib_connection
*ic
, u64 seq
, int ack_required
);
423 void rds_ib_ring_init(struct rds_ib_work_ring
*ring
, u32 nr
);
424 void rds_ib_ring_resize(struct rds_ib_work_ring
*ring
, u32 nr
);
425 u32
rds_ib_ring_alloc(struct rds_ib_work_ring
*ring
, u32 val
, u32
*pos
);
426 void rds_ib_ring_free(struct rds_ib_work_ring
*ring
, u32 val
);
427 void rds_ib_ring_unalloc(struct rds_ib_work_ring
*ring
, u32 val
);
428 int rds_ib_ring_empty(struct rds_ib_work_ring
*ring
);
429 int rds_ib_ring_low(struct rds_ib_work_ring
*ring
);
430 u32
rds_ib_ring_oldest(struct rds_ib_work_ring
*ring
);
431 u32
rds_ib_ring_completed(struct rds_ib_work_ring
*ring
, u32 wr_id
, u32 oldest
);
432 extern wait_queue_head_t rds_ib_ring_empty_wait
;
435 void rds_ib_xmit_path_complete(struct rds_conn_path
*cp
);
436 int rds_ib_xmit(struct rds_connection
*conn
, struct rds_message
*rm
,
437 unsigned int hdr_off
, unsigned int sg
, unsigned int off
);
438 void rds_ib_send_cqe_handler(struct rds_ib_connection
*ic
, struct ib_wc
*wc
);
439 void rds_ib_send_init_ring(struct rds_ib_connection
*ic
);
440 void rds_ib_send_clear_ring(struct rds_ib_connection
*ic
);
441 int rds_ib_xmit_rdma(struct rds_connection
*conn
, struct rm_rdma_op
*op
);
442 void rds_ib_send_add_credits(struct rds_connection
*conn
, unsigned int credits
);
443 void rds_ib_advertise_credits(struct rds_connection
*conn
, unsigned int posted
);
444 int rds_ib_send_grab_credits(struct rds_ib_connection
*ic
, u32 wanted
,
445 u32
*adv_credits
, int need_posted
, int max_posted
);
446 int rds_ib_xmit_atomic(struct rds_connection
*conn
, struct rm_atomic_op
*op
);
449 DECLARE_PER_CPU_SHARED_ALIGNED(struct rds_ib_statistics
, rds_ib_stats
);
450 #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
451 #define rds_ib_stats_add(member, count) \
452 rds_stats_add_which(rds_ib_stats, member, count)
453 unsigned int rds_ib_stats_info_copy(struct rds_info_iterator
*iter
,
457 int rds_ib_sysctl_init(void);
458 void rds_ib_sysctl_exit(void);
459 extern unsigned long rds_ib_sysctl_max_send_wr
;
460 extern unsigned long rds_ib_sysctl_max_recv_wr
;
461 extern unsigned long rds_ib_sysctl_max_unsig_wrs
;
462 extern unsigned long rds_ib_sysctl_max_unsig_bytes
;
463 extern unsigned long rds_ib_sysctl_max_recv_allocation
;
464 extern unsigned int rds_ib_sysctl_flow_control
;