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_mi_hdl_t 		f_mapped;
64 };
65 
66 struct rdsv3_ib_incoming {
67 	struct list		ii_frags;
68 	struct rdsv3_incoming	ii_inc;
69 };
70 
71 struct rdsv3_ib_connect_private {
72 	/* Add new fields at the end, and don't permute existing fields. */
73 	uint32_be_t		dp_saddr;
74 	uint32_be_t		dp_daddr;
75 	uint8_t			dp_protocol_major;
76 	uint8_t			dp_protocol_minor;
77 	uint16_be_t		dp_protocol_minor_mask; /* bitmask */
78 	uint32_be_t		dp_reserved1;
79 	uint32_be_t		dp_ack_seq;
80 	uint32_be_t		dp_credit;	/* non-zero enables flow ctl */
81 };
82 
83 struct rdsv3_ib_send_work {
84 	struct rdsv3_message	*s_rm;
85 	struct rdsv3_rdma_op	*s_op;
86 	ibt_wrc_opcode_t	s_opcode;
87 	unsigned long		s_queued;
88 };
89 
90 struct rdsv3_ib_recv_work {
91 	struct rdsv3_ib_incoming 	*r_ibinc;
92 	struct rdsv3_page_frag	*r_frag;
93 	ibt_all_wr_t		r_wr;
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 	struct rdsv3_ib_device	*rds_ibdev;
111 	struct rdsv3_connection	*conn;
112 
113 	/* alphabet soup, IBTA style */
114 	struct rdma_cm_id	*i_cm_id;
115 	struct ib_pd		*i_pd;
116 	struct rdsv3_hdrs_mr	*i_mr;
117 	struct ib_cq		*i_send_cq;
118 	struct ib_cq		*i_recv_cq;
119 
120 	/* tx */
121 	struct rdsv3_ib_work_ring	i_send_ring;
122 	struct rdsv3_message	*i_rm;
123 	struct rdsv3_header	*i_send_hdrs;
124 	uint64_t		i_send_hdrs_dma;
125 	struct rdsv3_ib_send_work *i_sends;
126 	ibt_send_wr_t		*i_send_wrs;
127 
128 	/* rx */
129 	ddi_taskq_t		*i_recv_tasklet;
130 	struct mutex		i_recv_mutex;
131 	struct rdsv3_ib_work_ring	i_recv_ring;
132 	struct rdsv3_ib_incoming	*i_ibinc;
133 	uint32_t		i_recv_data_rem;
134 	struct rdsv3_header	*i_recv_hdrs;
135 	uint64_t		i_recv_hdrs_dma;
136 	struct rdsv3_ib_recv_work *i_recvs;
137 	struct rdsv3_page_frag	i_frag;
138 	uint64_t		i_ack_recv;	/* last ACK received */
139 	processorid_t		i_recv_tasklet_cpuid;
140 	/* CPU to which the tasklet taskq should be bound */
141 
142 	/* sending acks */
143 	unsigned long		i_ack_flags;
144 #ifndef KERNEL_HAS_ATOMIC64
145 	kmutex_t		i_ack_lock;	/* protect i_ack_next */
146 	uint64_t		i_ack_next;	/* next ACK to send */
147 #else
148 	atomic64_t		i_ack_next;	/* next ACK to send */
149 #endif
150 	struct rdsv3_header	*i_ack;
151 	ibt_send_wr_t		i_ack_wr;
152 	ibt_wr_ds_t		i_ack_sge;
153 	uint64_t		i_ack_dma;
154 	unsigned long		i_ack_queued;
155 
156 	/*
157 	 * Flow control related information
158 	 *
159 	 * Our algorithm uses a pair variables that we need to access
160 	 * atomically - one for the send credits, and one posted
161 	 * recv credits we need to transfer to remote.
162 	 * Rather than protect them using a slow spinlock, we put both into
163 	 * a single atomic_t and update it using cmpxchg
164 	 */
165 	atomic_t		i_credits;
166 
167 	/* Protocol version specific information */
168 	unsigned int		i_flowctl:1;	/* enable/disable flow ctl */
169 
170 	/* Batched completions */
171 	unsigned int		i_unsignaled_wrs;
172 	long			i_unsignaled_bytes;
173 };
174 
175 /* This assumes that atomic_t is at least 32 bits */
176 #define	IB_GET_SEND_CREDITS(v)	((v) & 0xffff)
177 #define	IB_GET_POST_CREDITS(v)	((v) >> 16)
178 #define	IB_SET_SEND_CREDITS(v)	((v) & 0xffff)
179 #define	IB_SET_POST_CREDITS(v)	((v) << 16)
180 
181 struct rdsv3_ib_ipaddr {
182 	struct list_node	list;
183 	uint32_be_t		ipaddr;
184 };
185 
186 struct rdsv3_ib_device {
187 	struct list_node	list;
188 	struct list		ipaddr_list;
189 	struct list		conn_list;
190 	ib_device_t		*dev;
191 	struct ib_pd		*pd;
192 	ibt_lkey_t		local_dma_lkey;
193 	struct rds_ib_mr_pool	*mr_pool;
194 	unsigned int		fmr_max_remaps;
195 	unsigned int		max_fmrs;
196 	unsigned int		fmr_message_size;
197 	int			max_sge;
198 	unsigned int		max_wrs;
199 	ibt_fmr_pool_hdl_t	fmr_pool_hdl;
200 	kmutex_t		spinlock;	/* protect the above */
201 	ibt_hca_attr_t		hca_attr;
202 };
203 
204 /* bits for i_ack_flags */
205 #define	IB_ACK_IN_FLIGHT	0
206 #define	IB_ACK_REQUESTED	1
207 
208 /* Magic WR_ID for ACKs */
209 #define	RDSV3_IB_ACK_WR_ID	(~(uint64_t)0)
210 
211 struct rdsv3_ib_statistics {
212 	uint64_t	s_ib_connect_raced;
213 	uint64_t	s_ib_listen_closed_stale;
214 	uint64_t	s_ib_tx_cq_call;
215 	uint64_t	s_ib_tx_cq_event;
216 	uint64_t	s_ib_tx_ring_full;
217 	uint64_t	s_ib_tx_throttle;
218 	uint64_t	s_ib_tx_sg_mapping_failure;
219 	uint64_t	s_ib_tx_stalled;
220 	uint64_t	s_ib_tx_credit_updates;
221 	uint64_t	s_ib_rx_cq_call;
222 	uint64_t	s_ib_rx_cq_event;
223 	uint64_t	s_ib_rx_ring_empty;
224 	uint64_t	s_ib_rx_refill_from_cq;
225 	uint64_t	s_ib_rx_refill_from_thread;
226 	uint64_t	s_ib_rx_alloc_limit;
227 	uint64_t	s_ib_rx_credit_updates;
228 	uint64_t	s_ib_ack_sent;
229 	uint64_t	s_ib_ack_send_failure;
230 	uint64_t	s_ib_ack_send_delayed;
231 	uint64_t	s_ib_ack_send_piggybacked;
232 	uint64_t	s_ib_ack_received;
233 	uint64_t	s_ib_rdma_mr_alloc;
234 	uint64_t	s_ib_rdma_mr_free;
235 	uint64_t	s_ib_rdma_mr_used;
236 	uint64_t	s_ib_rdma_mr_pool_flush;
237 	uint64_t	s_ib_rdma_mr_pool_wait;
238 	uint64_t	s_ib_rdma_mr_pool_depleted;
239 };
240 
241 extern struct rdsv3_workqueue_struct_s *rds_ib_wq;
242 
243 /* ib.c */
244 extern struct rdsv3_transport rdsv3_ib_transport;
245 extern void rdsv3_ib_add_one(ib_device_t *device);
246 extern void rdsv3_ib_remove_one(ib_device_t *device);
247 extern struct ib_client rdsv3_ib_client;
248 
249 extern unsigned int fmr_pool_size;
250 extern unsigned int fmr_message_size;
251 extern unsigned int rdsv3_ib_retry_count;
252 
253 extern kmutex_t ib_nodev_conns_lock;
254 extern struct list ib_nodev_conns;
255 
256 /* ib_cm.c */
257 int rdsv3_ib_conn_alloc(struct rdsv3_connection *conn, int gfp);
258 void rdsv3_ib_conn_free(void *arg);
259 int rdsv3_ib_conn_connect(struct rdsv3_connection *conn);
260 void rdsv3_ib_conn_shutdown(struct rdsv3_connection *conn);
261 void rdsv3_conn_drop(struct rdsv3_connection *conn);
262 int rdsv3_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
263     struct rdma_cm_event *event);
264 int rdsv3_ib_cm_initiate_connect(struct rdma_cm_id *cm_id);
265 void rdsv3_ib_cm_connect_complete(struct rdsv3_connection *conn,
266     struct rdma_cm_event *event);
267 
268 /* ib_rdma.c */
269 int rdsv3_ib_update_ipaddr(struct rdsv3_ib_device *rds_ibdev,
270     uint32_be_t ipaddr);
271 void rdsv3_ib_add_conn(struct rdsv3_ib_device *rds_ibdev,
272     struct rdsv3_connection *conn);
273 void rdsv3_ib_remove_conn(struct rdsv3_ib_device *rds_ibdev,
274     struct rdsv3_connection *conn);
275 void __rdsv3_ib_destroy_conns(struct list *list, kmutex_t *list_lock);
276 static inline void rdsv3_ib_destroy_nodev_conns(void)
277 {
278 	__rdsv3_ib_destroy_conns(&ib_nodev_conns, &ib_nodev_conns_lock);
279 }
280 static inline void rdsv3_ib_destroy_conns(struct rdsv3_ib_device *rds_ibdev)
281 {
282 	__rdsv3_ib_destroy_conns(&rds_ibdev->conn_list, &rds_ibdev->spinlock);
283 }
284 
285 int rdsv3_ib_create_mr_pool(struct rdsv3_ib_device *);
286 void rdsv3_ib_destroy_mr_pool(struct rdsv3_ib_device *);
287 void rdsv3_ib_get_mr_info(struct rdsv3_ib_device *rds_ibdev,
288 	struct rdsv3_info_rdma_connection *iinfo);
289 void *rdsv3_ib_get_mr(struct rdsv3_iovec *args, unsigned long nents,
290 	struct rdsv3_sock *rs, uint32_t *key_ret);
291 void rdsv3_ib_sync_mr(void *trans_private, int dir);
292 void rdsv3_ib_free_mr(void *trans_private, int invalidate);
293 void rdsv3_ib_flush_mrs(void);
294 
295 /* ib_recv.c */
296 int rdsv3_ib_recv_init(void);
297 void rdsv3_ib_recv_exit(void);
298 int rdsv3_ib_recv(struct rdsv3_connection *conn);
299 int rdsv3_ib_recv_refill(struct rdsv3_connection *conn, int kptr_gfp,
300     int page_gfp, int prefill);
301 void rdsv3_ib_inc_purge(struct rdsv3_incoming *inc);
302 void rdsv3_ib_inc_free(struct rdsv3_incoming *inc);
303 int rdsv3_ib_inc_copy_to_user(struct rdsv3_incoming *inc, uio_t *uiop,
304     size_t size);
305 void rdsv3_ib_recv_cq_comp_handler(struct ib_cq *cq, void *context);
306 void rdsv3_ib_recv_tasklet_fn(void *data);
307 void rdsv3_ib_recv_init_ring(struct rdsv3_ib_connection *ic);
308 void rdsv3_ib_recv_clear_ring(struct rdsv3_ib_connection *ic);
309 void rdsv3_ib_recv_init_ack(struct rdsv3_ib_connection *ic);
310 void rdsv3_ib_attempt_ack(struct rdsv3_ib_connection *ic);
311 void rdsv3_ib_ack_send_complete(struct rdsv3_ib_connection *ic);
312 uint64_t rdsv3_ib_piggyb_ack(struct rdsv3_ib_connection *ic);
313 
314 /* ib_ring.c */
315 void rdsv3_ib_ring_init(struct rdsv3_ib_work_ring *ring, uint32_t nr);
316 void rdsv3_ib_ring_resize(struct rdsv3_ib_work_ring *ring, uint32_t nr);
317 uint32_t rdsv3_ib_ring_alloc(struct rdsv3_ib_work_ring *ring, uint32_t val,
318     uint32_t *pos);
319 void rdsv3_ib_ring_free(struct rdsv3_ib_work_ring *ring, uint32_t val);
320 void rdsv3_ib_ring_unalloc(struct rdsv3_ib_work_ring *ring, uint32_t val);
321 int rdsv3_ib_ring_empty(struct rdsv3_ib_work_ring *ring);
322 int rdsv3_ib_ring_low(struct rdsv3_ib_work_ring *ring);
323 uint32_t rdsv3_ib_ring_oldest(struct rdsv3_ib_work_ring *ring);
324 uint32_t rdsv3_ib_ring_completed(struct rdsv3_ib_work_ring *ring,
325     uint32_t wr_id, uint32_t oldest);
326 extern rdsv3_wait_queue_t rdsv3_ib_ring_empty_wait;
327 
328 /* ib_send.c */
329 void rdsv3_ib_xmit_complete(struct rdsv3_connection *conn);
330 int rdsv3_ib_xmit(struct rdsv3_connection *conn, struct rdsv3_message *rm,
331     unsigned int hdr_off, unsigned int sg, unsigned int off);
332 void rdsv3_ib_send_cq_comp_handler(struct ib_cq *cq, void *context);
333 void rdsv3_ib_send_init_ring(struct rdsv3_ib_connection *ic);
334 void rdsv3_ib_send_clear_ring(struct rdsv3_ib_connection *ic);
335 int rdsv3_ib_xmit_rdma(struct rdsv3_connection *conn, struct rdsv3_rdma_op *op);
336 void rdsv3_ib_send_add_credits(struct rdsv3_connection *conn,
337     unsigned int credits);
338 void rdsv3_ib_advertise_credits(struct rdsv3_connection *conn,
339     unsigned int posted);
340 int rdsv3_ib_send_grab_credits(struct rdsv3_ib_connection *ic, uint32_t wanted,
341     uint32_t *adv_credits, int need_posted, int max_posted);
342 
343 /* ib_stats.c */
344 RDSV3_DECLARE_PER_CPU(struct rdsv3_ib_statistics, rdsv3_ib_stats);
345 #define	rdsv3_ib_stats_inc(member) rdsv3_stats_inc_which(rdsv3_ib_stats, member)
346 unsigned int rdsv3_ib_stats_info_copy(struct rdsv3_info_iterator *iter,
347     unsigned int avail);
348 
349 /* ib_sysctl.c */
350 int rdsv3_ib_sysctl_init(void);
351 void rdsv3_ib_sysctl_exit(void);
352 extern unsigned long rdsv3_ib_sysctl_max_send_wr;
353 extern unsigned long rdsv3_ib_sysctl_max_recv_wr;
354 extern unsigned long rdsv3_ib_sysctl_max_unsig_wrs;
355 extern unsigned long rdsv3_ib_sysctl_max_unsig_bytes;
356 extern unsigned long rdsv3_ib_sysctl_max_recv_allocation;
357 extern unsigned int rdsv3_ib_sysctl_flow_control;
358 
359 #endif /* _RDSV3_IB_H */
360