1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #ifndef _RDSV3_IB_H
26 #define	_RDSV3_IB_H
27 
28 #include <sys/rds.h>
29 
30 #include <sys/ib/clients/rdsv3/rdsv3.h>
31 #include <sys/ib/clients/rdsv3/rdma_transport.h>
32 
33 #define	RDSV3_FMR_SIZE			256
34 #define	RDSV3_FMR_POOL_SIZE		(12 * 1024)
35 
36 #define	RDSV3_IB_SEND_WRS		64
37 
38 #define	RDSV3_IB_MAX_SGE		8
39 #define	RDSV3_IB_RECV_SGE 		2
40 
41 #define	RDSV3_IB_DEFAULT_RECV_WR	1024
42 #define	RDSV3_IB_DEFAULT_SEND_WR	256
43 
44 #define	RDSV3_IB_DEFAULT_RETRY_COUNT	2
45 
46 /* minor versions supported */
47 #define	RDSV3_IB_SUPPORTED_PROTOCOLS	0x00000003
48 
49 extern struct list rdsv3_ib_devices;
50 
51 /*
52  * IB posts RDSV3_FRAG_SIZE fragments of pages to the receive queues to
53  * try and minimize the amount of memory tied up both the device and
54  * socket receive queues.
55  */
56 /* page offset of the final full frag that fits in the page */
57 #define	RDSV3_PAGE_LAST_OFF	\
58 	(((PAGE_SIZE  / RDSV3_FRAG_SIZE) - 1) * RDSV3_FRAG_SIZE)
59 struct rdsv3_page_frag {
60 	struct list_node	f_item;
61 	caddr_t			f_page;
62 	unsigned long		f_offset;
63 	ibt_wr_ds_t		f_sge;
64 	ibt_mi_hdl_t 		f_mapped;
65 };
66 
67 struct rdsv3_ib_incoming {
68 	struct list		ii_frags;
69 	struct rdsv3_incoming	ii_inc;
70 };
71 
72 struct rdsv3_ib_connect_private {
73 	/* Add new fields at the end, and don't permute existing fields. */
74 	uint32_be_t		dp_saddr;
75 	uint32_be_t		dp_daddr;
76 	uint8_t			dp_protocol_major;
77 	uint8_t			dp_protocol_minor;
78 	uint16_be_t		dp_protocol_minor_mask; /* bitmask */
79 	uint32_be_t		dp_reserved1;
80 	uint32_be_t		dp_ack_seq;
81 	uint32_be_t		dp_credit;	/* non-zero enables flow ctl */
82 };
83 
84 struct rdsv3_ib_send_work {
85 	struct rdsv3_message	*s_rm;
86 	struct rdsv3_rdma_op	*s_op;
87 	ibt_wrc_opcode_t	s_opcode;
88 	unsigned long		s_queued;
89 };
90 
91 struct rdsv3_ib_recv_work {
92 	struct rdsv3_ib_incoming 	*r_ibinc;
93 	struct rdsv3_page_frag		*r_frag;
94 	ibt_wr_ds_t			r_sge[2];
95 };
96 
97 struct rdsv3_ib_work_ring {
98 	uint32_t	w_nr;
99 	uint32_t	w_alloc_ptr;
100 	uint32_t	w_alloc_ctr;
101 	uint32_t	w_free_ptr;
102 	atomic_t	w_free_ctr;
103 };
104 
105 struct rdsv3_ib_device;
106 
107 struct rdsv3_ib_connection {
108 
109 	struct list_node	ib_node;
110 	boolean_t		i_on_dev_list;
111 	struct rdsv3_ib_device	*rds_ibdev;
112 	struct rdsv3_connection	*conn;
113 
114 	/* alphabet soup, IBTA style */
115 	struct rdma_cm_id	*i_cm_id;
116 	struct ib_pd		*i_pd;
117 	struct rdsv3_hdrs_mr	*i_mr;
118 	struct ib_cq		*i_send_cq;
119 	struct ib_cq		*i_recv_cq;
120 
121 	/* tx */
122 	struct rdsv3_ib_work_ring	i_send_ring;
123 	struct rdsv3_message	*i_rm;
124 	struct rdsv3_header	*i_send_hdrs;
125 	uint64_t		i_send_hdrs_dma;
126 	struct rdsv3_ib_send_work *i_sends;
127 	ibt_send_wr_t		*i_send_wrs;
128 
129 	/* rx */
130 	ddi_taskq_t		*i_recv_tasklet;
131 	struct mutex		i_recv_mutex;
132 	struct rdsv3_ib_work_ring	i_recv_ring;
133 	struct rdsv3_ib_incoming	*i_ibinc;
134 	uint32_t		i_recv_data_rem;
135 	struct rdsv3_header	*i_recv_hdrs;
136 	uint64_t		i_recv_hdrs_dma;
137 	struct rdsv3_ib_recv_work *i_recvs;
138 	ibt_recv_wr_t		*i_recv_wrs;
139 	struct rdsv3_page_frag	i_frag;
140 	uint64_t		i_ack_recv;	/* last ACK received */
141 	processorid_t		i_recv_tasklet_cpuid;
142 	/* CPU to which the tasklet taskq should be bound */
143 
144 	/* sending acks */
145 	unsigned long		i_ack_flags;
146 #ifdef KERNEL_HAS_ATOMIC64
147 	atomic64_t		i_ack_next;	/* next ACK to send */
148 #else
149 	kmutex_t		i_ack_lock;	/* protect i_ack_next */
150 	uint64_t		i_ack_next;	/* next ACK to send */
151 #endif
152 	struct rdsv3_header	*i_ack;
153 	ibt_send_wr_t		i_ack_wr;
154 	ibt_wr_ds_t		i_ack_sge;
155 	uint64_t		i_ack_dma;
156 	unsigned long		i_ack_queued;
157 
158 	/*
159 	 * Flow control related information
160 	 *
161 	 * Our algorithm uses a pair variables that we need to access
162 	 * atomically - one for the send credits, and one posted
163 	 * recv credits we need to transfer to remote.
164 	 * Rather than protect them using a slow spinlock, we put both into
165 	 * a single atomic_t and update it using cmpxchg
166 	 */
167 	atomic_t		i_credits;
168 
169 	/* Protocol version specific information */
170 	unsigned int		i_flowctl:1;	/* enable/disable flow ctl */
171 
172 	/* Batched completions */
173 	unsigned int		i_unsignaled_wrs;
174 	long			i_unsignaled_bytes;
175 };
176 
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)
182 
183 struct rdsv3_ib_ipaddr {
184 	struct list_node	list;
185 	uint32_be_t		ipaddr;
186 };
187 
188 struct rdsv3_ib_device {
189 	struct list_node	list;
190 	struct list		ipaddr_list;
191 	struct list		conn_list;
192 	ib_device_t		*dev;
193 	struct ib_pd		*pd;
194 	struct kmem_cache	*ib_frag_slab;
195 	struct rds_ib_mr_pool	*mr_pool;
196 	unsigned int		fmr_max_remaps;
197 	unsigned int		max_fmrs;
198 	unsigned int		fmr_message_size;
199 	int			max_sge;
200 	unsigned int		max_wrs;
201 	ibt_fmr_pool_hdl_t	fmr_pool_hdl;
202 	kmutex_t		spinlock;	/* protect the above */
203 	ibt_hca_attr_t		hca_attr;
204 };
205 
206 /* bits for i_ack_flags */
207 #define	IB_ACK_IN_FLIGHT	0
208 #define	IB_ACK_REQUESTED	1
209 
210 /* Magic WR_ID for ACKs */
211 #define	RDSV3_IB_ACK_WR_ID	(~(uint64_t)0)
212 
213 struct rdsv3_ib_statistics {
214 	uint64_t	s_ib_connect_raced;
215 	uint64_t	s_ib_listen_closed_stale;
216 	uint64_t	s_ib_tx_cq_call;
217 	uint64_t	s_ib_tx_cq_event;
218 	uint64_t	s_ib_tx_ring_full;
219 	uint64_t	s_ib_tx_throttle;
220 	uint64_t	s_ib_tx_sg_mapping_failure;
221 	uint64_t	s_ib_tx_stalled;
222 	uint64_t	s_ib_tx_credit_updates;
223 	uint64_t	s_ib_rx_cq_call;
224 	uint64_t	s_ib_rx_cq_event;
225 	uint64_t	s_ib_rx_ring_empty;
226 	uint64_t	s_ib_rx_refill_from_cq;
227 	uint64_t	s_ib_rx_refill_from_thread;
228 	uint64_t	s_ib_rx_alloc_limit;
229 	uint64_t	s_ib_rx_credit_updates;
230 	uint64_t	s_ib_ack_sent;
231 	uint64_t	s_ib_ack_send_failure;
232 	uint64_t	s_ib_ack_send_delayed;
233 	uint64_t	s_ib_ack_send_piggybacked;
234 	uint64_t	s_ib_ack_received;
235 	uint64_t	s_ib_rdma_mr_alloc;
236 	uint64_t	s_ib_rdma_mr_free;
237 	uint64_t	s_ib_rdma_mr_used;
238 	uint64_t	s_ib_rdma_mr_pool_flush;
239 	uint64_t	s_ib_rdma_mr_pool_wait;
240 	uint64_t	s_ib_rdma_mr_pool_depleted;
241 };
242 
243 extern struct rdsv3_workqueue_struct_s *rds_ib_wq;
244 
245 /* ib.c */
246 extern struct rdsv3_transport rdsv3_ib_transport;
247 extern void rdsv3_ib_add_one(ib_device_t *device);
248 extern void rdsv3_ib_remove_one(ib_device_t *device);
249 extern struct ib_client rdsv3_ib_client;
250 
251 extern unsigned int fmr_pool_size;
252 extern unsigned int fmr_message_size;
253 extern unsigned int rdsv3_ib_retry_count;
254 
255 extern kmutex_t ib_nodev_conns_lock;
256 extern struct list ib_nodev_conns;
257 
258 /* ib_cm.c */
259 int rdsv3_ib_conn_alloc(struct rdsv3_connection *conn, int gfp);
260 void rdsv3_ib_conn_free(void *arg);
261 int rdsv3_ib_conn_connect(struct rdsv3_connection *conn);
262 void rdsv3_ib_conn_shutdown(struct rdsv3_connection *conn);
263 void rdsv3_conn_drop(struct rdsv3_connection *conn);
264 int rdsv3_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
265     struct rdma_cm_event *event);
266 int rdsv3_ib_cm_initiate_connect(struct rdma_cm_id *cm_id);
267 void rdsv3_ib_cm_connect_complete(struct rdsv3_connection *conn,
268     struct rdma_cm_event *event);
269 
270 /* ib_rdma.c */
271 int rdsv3_ib_update_ipaddr(struct rdsv3_ib_device *rds_ibdev,
272     uint32_be_t ipaddr);
273 void rdsv3_ib_add_conn(struct rdsv3_ib_device *rds_ibdev,
274     struct rdsv3_connection *conn);
275 void rdsv3_ib_remove_conn(struct rdsv3_ib_device *rds_ibdev,
276     struct rdsv3_connection *conn);
277 void __rdsv3_ib_destroy_conns(struct list *list, kmutex_t *list_lock);
278 static inline void rdsv3_ib_destroy_nodev_conns(void)
279 {
280 	__rdsv3_ib_destroy_conns(&ib_nodev_conns, &ib_nodev_conns_lock);
281 }
282 static inline void rdsv3_ib_destroy_conns(struct rdsv3_ib_device *rds_ibdev)
283 {
284 	__rdsv3_ib_destroy_conns(&rds_ibdev->conn_list, &rds_ibdev->spinlock);
285 }
286 
287 int rdsv3_ib_create_mr_pool(struct rdsv3_ib_device *);
288 void rdsv3_ib_destroy_mr_pool(struct rdsv3_ib_device *);
289 void rdsv3_ib_get_mr_info(struct rdsv3_ib_device *rds_ibdev,
290 	struct rdsv3_info_rdma_connection *iinfo);
291 void *rdsv3_ib_get_mr(struct rdsv3_iovec *args, unsigned long nents,
292 	struct rdsv3_sock *rs, uint32_t *key_ret);
293 void rdsv3_ib_sync_mr(void *trans_private, int dir);
294 void rdsv3_ib_free_mr(void *trans_private, int invalidate);
295 void rdsv3_ib_flush_mrs(void);
296 
297 /* ib_recv.c */
298 int rdsv3_ib_recv_init(void);
299 void rdsv3_ib_recv_exit(void);
300 int rdsv3_ib_recv(struct rdsv3_connection *conn);
301 int rdsv3_ib_recv_refill(struct rdsv3_connection *conn, int kmflags,
302     int prefill);
303 void rdsv3_ib_inc_purge(struct rdsv3_incoming *inc);
304 void rdsv3_ib_inc_free(struct rdsv3_incoming *inc);
305 int rdsv3_ib_inc_copy_to_user(struct rdsv3_incoming *inc, uio_t *uiop,
306     size_t size);
307 void rdsv3_ib_recv_cq_comp_handler(struct ib_cq *cq, void *context);
308 void rdsv3_ib_recv_tasklet_fn(void *data);
309 void rdsv3_ib_recv_init_ring(struct rdsv3_ib_connection *ic);
310 void rdsv3_ib_recv_clear_ring(struct rdsv3_ib_connection *ic);
311 void rdsv3_ib_recv_init_ack(struct rdsv3_ib_connection *ic);
312 void rdsv3_ib_attempt_ack(struct rdsv3_ib_connection *ic);
313 void rdsv3_ib_ack_send_complete(struct rdsv3_ib_connection *ic);
314 uint64_t rdsv3_ib_piggyb_ack(struct rdsv3_ib_connection *ic);
315 
316 /* ib_ring.c */
317 void rdsv3_ib_ring_init(struct rdsv3_ib_work_ring *ring, uint32_t nr);
318 void rdsv3_ib_ring_resize(struct rdsv3_ib_work_ring *ring, uint32_t nr);
319 uint32_t rdsv3_ib_ring_alloc(struct rdsv3_ib_work_ring *ring, uint32_t val,
320     uint32_t *pos);
321 void rdsv3_ib_ring_free(struct rdsv3_ib_work_ring *ring, uint32_t val);
322 void rdsv3_ib_ring_unalloc(struct rdsv3_ib_work_ring *ring, uint32_t val);
323 int rdsv3_ib_ring_empty(struct rdsv3_ib_work_ring *ring);
324 int rdsv3_ib_ring_low(struct rdsv3_ib_work_ring *ring);
325 uint32_t rdsv3_ib_ring_oldest(struct rdsv3_ib_work_ring *ring);
326 uint32_t rdsv3_ib_ring_completed(struct rdsv3_ib_work_ring *ring,
327     uint32_t wr_id, uint32_t oldest);
328 extern rdsv3_wait_queue_t rdsv3_ib_ring_empty_wait;
329 
330 /* ib_send.c */
331 void rdsv3_ib_xmit_complete(struct rdsv3_connection *conn);
332 int rdsv3_ib_xmit(struct rdsv3_connection *conn, struct rdsv3_message *rm,
333     unsigned int hdr_off, unsigned int sg, unsigned int off);
334 void rdsv3_ib_send_cq_comp_handler(struct ib_cq *cq, void *context);
335 void rdsv3_ib_send_init_ring(struct rdsv3_ib_connection *ic);
336 void rdsv3_ib_send_clear_ring(struct rdsv3_ib_connection *ic);
337 int rdsv3_ib_xmit_rdma(struct rdsv3_connection *conn, struct rdsv3_rdma_op *op);
338 void rdsv3_ib_send_add_credits(struct rdsv3_connection *conn,
339     unsigned int credits);
340 void rdsv3_ib_advertise_credits(struct rdsv3_connection *conn,
341     unsigned int posted);
342 int rdsv3_ib_send_grab_credits(struct rdsv3_ib_connection *ic, uint32_t wanted,
343     uint32_t *adv_credits, int need_posted);
344 
345 /* ib_stats.c */
346 RDSV3_DECLARE_PER_CPU(struct rdsv3_ib_statistics, rdsv3_ib_stats);
347 #define	rdsv3_ib_stats_inc(member) rdsv3_stats_inc_which(rdsv3_ib_stats, member)
348 unsigned int rdsv3_ib_stats_info_copy(struct rdsv3_info_iterator *iter,
349     unsigned int avail);
350 
351 /* ib_sysctl.c */
352 int rdsv3_ib_sysctl_init(void);
353 void rdsv3_ib_sysctl_exit(void);
354 extern unsigned long rdsv3_ib_sysctl_max_send_wr;
355 extern unsigned long rdsv3_ib_sysctl_max_recv_wr;
356 extern unsigned long rdsv3_ib_sysctl_max_unsig_wrs;
357 extern unsigned long rdsv3_ib_sysctl_max_unsig_bytes;
358 extern unsigned long rdsv3_ib_sysctl_max_recv_allocation;
359 extern unsigned int rdsv3_ib_sysctl_flow_control;
360 
361 #endif /* _RDSV3_IB_H */
362