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_IMPL_H
26 #define	_RDSV3_IMPL_H
27 
28 #include <sys/atomic.h>
29 
30 /*
31  * This file is only present in Solaris
32  */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 extern dev_info_t	*rdsv3_dev_info;
39 
40 #define	uint16_be_t	uint16_t
41 #define	uint32_be_t	uint32_t
42 #define	uint64_be_t	uint64_t
43 
44 /*
45  * RDS Well known service id
46  * Format: 0x1h00144Fhhhhhhhh
47  *         "00144F" is the Sun OUI
48  * 'h' can be any hex-decimal digit.
49  */
50 #define	RDS_SERVICE_ID		0x1000144F00000001ULL
51 
52 /*
53  * Atomic operations
54  */
55 typedef unsigned int	atomic_t;
56 #define	ATOMIC_INIT(a)	a
57 
58 #define	atomic_get(p)	(*(p))
59 
60 #define	atomic_cmpset_long(p, c, n) \
61 	((c == atomic_cas_uint(p, c, n)) ? c : -1)
62 
63 #define	atomic_dec_and_test(a)			\
64 	(atomic_dec_uint_nv((a)) == 0)
65 
66 #define	atomic_cmpxchg(a, o, n)			\
67 	atomic_cas_uint(a, o, n)
68 
69 #ifdef _LP64
70 #define	set_bit(b, p) \
71 	atomic_or_ulong(((volatile ulong_t *)(void *)(p)) + ((b) >> 6), \
72 	1ul << ((b) & 0x3f))
73 
74 #define	clear_bit(b, p) \
75 	atomic_and_ulong(((volatile ulong_t *)(void *)(p)) + ((b) >> 6), \
76 	~(1ul << ((b) & 0x3f)))
77 
78 #define	test_bit(b, p) \
79 	(((volatile ulong_t *)(void *)(p))[(b) >> 6] & (1ul << ((b) & 0x3f)))
80 
81 #define	test_and_set_bit(b, p) \
82 	atomic_set_long_excl(((ulong_t *)(void *)(p)) +		\
83 	    ((b) >> 6), ((b) & 0x3f))
84 #define	test_and_clear_bit(b, p) \
85 	!atomic_clear_long_excl(((ulong_t *)(void *)(p)) + ((b) >> 6), \
86 	((b) & 0x3f))
87 #else
88 #define	set_bit(b, p) \
89 	atomic_or_uint(((volatile uint_t *)(void *)p) + (b >> 5), \
90 	1ul << (b & 0x1f))
91 
92 #define	clear_bit(b, p) \
93 	atomic_and_uint(((volatile uint_t *)(void *)p) + (b >> 5), \
94 	~(1ul << (b & 0x1f)))
95 
96 #define	test_bit(b, p) \
97 	(((volatile uint_t *)(void *)p)[b >> 5] & (1ul << (b & 0x1f)))
98 
99 #define	test_and_set_bit(b, p) \
100 	atomic_set_long_excl(((ulong_t *)(void *)p) + (b >> 5), (b & 0x1f))
101 #define	test_and_clear_bit(b, p) \
102 	!atomic_clear_long_excl(((ulong_t *)(void *)p) + (b >> 5), (b & 0x1f))
103 #endif
104 
105 /*
106  * These macros and/or constants are used instead of Linux
107  * generic_{test,__{clear,set}}_le_bit().
108  */
109 #if defined(sparc)
110 #define	LE_BIT_XOR	((BITS_PER_LONG-1) & ~0x7)
111 #else
112 #define	LE_BIT_XOR	0
113 #endif
114 
115 #define	set_le_bit(b, p)	set_bit(b ^ LE_BIT_XOR, p)
116 #define	clear_le_bit(b, p)	clear_bit(b ^ LE_BIT_XOR, p)
117 #define	test_le_bit(b, p)	test_bit(b ^ LE_BIT_XOR, p)
118 
119 uint_t	rdsv3_one_sec_in_hz;
120 
121 #define	jiffies	100
122 #define	HZ	(drv_hztousec(1))
123 #define	container_of(m, s, name)			\
124 	(void *)((uintptr_t)(m) - (uintptr_t)offsetof(s, name))
125 #define	ARRAY_SIZE(x)	(sizeof (x) / sizeof (x[0]))
126 /* setting this to PAGESIZE throws build errors */
127 #define	PAGE_SIZE	4096 /* xxx - fix this */
128 #define	BITS_PER_LONG	(sizeof (unsigned long) * 8)
129 
130 /* debug */
131 #define	RDSV3_PANIC()		cmn_err(CE_PANIC, "Panic forced by RDSV3");
132 
133 /* ERR */
134 #define	MAX_ERRNO	4095
135 #define	ERR_PTR(x)	((void *)(uintptr_t)x)
136 #define	IS_ERR(ptr)	(((uintptr_t)ptr) >= (uintptr_t)-MAX_ERRNO)
137 #define	PTR_ERR(ptr)	(int)(uintptr_t)ptr
138 
139 /* cpu */
140 #define	NR_CPUS		1
141 #define	put_cpu()
142 #define	get_cpu()	0
143 
144 #define	MAX_SCHEDULE_TIMEOUT	(~0UL>>1)
145 
146 #define	RDMA_CM_EVENT_ADDR_CHANGE	14
147 
148 /* list */
149 /* copied and modified list_remove_node */
150 #define	list_remove_node(node)						\
151 	if ((node)->list_next != NULL) {				\
152 		(node)->list_prev->list_next = (node)->list_next;	\
153 		(node)->list_next->list_prev = (node)->list_prev;	\
154 		(node)->list_next = (node)->list_prev = NULL;		\
155 	}
156 
157 #define	list_splice(src, dst)	{				\
158 	list_create(dst, (src)->list_size, (src)->list_offset);	\
159 	list_move_tail(dst, src);				\
160 	}
161 
162 #define	RDSV3_FOR_EACH_LIST_NODE(objp, listp, member)	\
163 	for (objp = list_head(listp); objp; objp = list_next(listp, objp))
164 #define	RDSV3_FOR_EACH_LIST_NODE_SAFE(objp, tmp, listp, member)	\
165 	for (objp = list_head(listp), tmp = (objp != NULL) ?	\
166 	    list_next(listp, objp) : NULL;			\
167 	    objp;						\
168 	    objp = tmp, tmp = (objp != NULL) ?			\
169 	    list_next(listp, objp) : NULL)
170 
171 /* simulate wait_queue_head_t */
172 typedef struct rdsv3_wait_queue_s {
173 	kmutex_t	waitq_mutex;
174 	kcondvar_t	waitq_cv;
175 	uint_t		waitq_waiters;
176 } rdsv3_wait_queue_t;
177 
178 #define	rdsv3_init_waitqueue(waitqp)					\
179 	mutex_init(&(waitqp)->waitq_mutex, NULL, MUTEX_DRIVER, NULL);	\
180 	cv_init(&(waitqp)->waitq_cv, NULL, CV_DRIVER, NULL);		\
181 	(waitqp)->waitq_waiters = 0
182 
183 #define	rdsv3_exit_waitqueue(waitqp)					\
184 	ASSERT((waitqp)->waitq_waiters == 0);				\
185 	mutex_destroy(&(waitqp)->waitq_mutex);				\
186 	cv_destroy(&(waitqp)->waitq_cv)
187 
188 #define	rdsv3_wake_up(waitqp)	{					\
189 	mutex_enter(&(waitqp)->waitq_mutex);				\
190 	if ((waitqp)->waitq_waiters)					\
191 		cv_signal(&(waitqp)->waitq_cv);				\
192 	mutex_exit(&(waitqp)->waitq_mutex);				\
193 	}
194 
195 #define	rdsv3_wake_up_all(waitqp)	{				\
196 	mutex_enter(&(waitqp)->waitq_mutex);				\
197 	if ((waitqp)->waitq_waiters)					\
198 		cv_broadcast(&(waitqp)->waitq_cv);			\
199 	mutex_exit(&(waitqp)->waitq_mutex);				\
200 	}
201 
202 /* analogous to cv_wait */
203 #define	rdsv3_wait_event(waitq, condition)				\
204 {									\
205 	mutex_enter(&(waitq)->waitq_mutex);				\
206 	(waitq)->waitq_waiters++;					\
207 	while (!(condition)) {						\
208 		cv_wait(&(waitq)->waitq_cv, &(waitq)->waitq_mutex);	\
209 	}								\
210 	(waitq)->waitq_waiters--;					\
211 	mutex_exit(&(waitq)->waitq_mutex);				\
212 }
213 
214 /* analogous to cv_wait_sig */
215 #define	rdsv3_wait_sig(waitqp, condition)				\
216 (									\
217 {									\
218 	int cv_return = 1;						\
219 	mutex_enter(&(waitqp)->waitq_mutex);				\
220 	(waitqp)->waitq_waiters++;					\
221 	while (!(condition)) {						\
222 		cv_return = cv_wait_sig(&(waitqp)->waitq_cv,		\
223 		    &(waitqp)->waitq_mutex);				\
224 		if (cv_return == 0) {					\
225 			break;						\
226 		}							\
227 	}								\
228 	(waitqp)->waitq_waiters--;					\
229 	mutex_exit(&(waitqp)->waitq_mutex);				\
230 	cv_return;							\
231 }									\
232 )
233 
234 #define	SOCK_DEAD	1ul
235 
236 /* socket */
237 typedef struct rsock {
238 	sock_upper_handle_t	sk_upper_handle;
239 	sock_upcalls_t		*sk_upcalls;
240 
241 	kmutex_t		sk_lock;
242 	ulong_t			sk_flag;
243 	rdsv3_wait_queue_t	*sk_sleep; /* Also protected by rs_recv_lock */
244 	int			sk_sndbuf;
245 	int			sk_rcvbuf;
246 	atomic_t		sk_refcount;
247 
248 	struct rdsv3_sock	*sk_protinfo;
249 } rsock_t;
250 
251 typedef struct rdsv3_conn_info_s {
252 	uint32_be_t  c_laddr;
253 	uint32_be_t  c_faddr;
254 } rdsv3_conn_info_t;
255 
256 /* WQ */
257 typedef struct rdsv3_workqueue_struct_s {
258 	kmutex_t wq_lock;
259 	uint_t	wq_state;
260 	int	wq_pending;
261 	list_t	wq_queue;
262 } rdsv3_workqueue_struct_t;
263 
264 struct rdsv3_work_s;
265 typedef void (*rdsv3_work_func_t)(struct rdsv3_work_s *);
266 typedef struct rdsv3_work_s {
267 	list_node_t	work_item;
268 	rdsv3_work_func_t	func;
269 } rdsv3_work_t;
270 
271 /* simulate delayed_work */
272 typedef struct rdsv3_delayed_work_s {
273 	kmutex_t		lock;
274 	rdsv3_work_t		work;
275 	timeout_id_t		timeid;
276 	rdsv3_workqueue_struct_t	*wq;
277 } rdsv3_delayed_work_t;
278 
279 #define	RDSV3_INIT_WORK(wp, f)	(wp)->func = f
280 #define	RDSV3_INIT_DELAYED_WORK(dwp, f)				\
281 	(dwp)->work.func = f;					\
282 	mutex_init(&(dwp)->lock, NULL, MUTEX_DRIVER, NULL);	\
283 	(dwp)->timeid = 0
284 
285 /* simulate scatterlist */
286 struct rdsv3_scatterlist {
287 	caddr_t		vaddr;
288 	uint_t		length;
289 	ibt_wr_ds_t	*sgl;
290 	ibt_mi_hdl_t	mihdl;
291 };
292 #define	rdsv3_sg_page(scat)	(scat)->vaddr
293 #define	rdsv3_sg_len(scat)	(scat)->length
294 #define	rdsv3_sg_set_page(scat, pg, len, off)		\
295 	(scat)->vaddr = (caddr_t)(pg + off);		\
296 	(scat)->length = len
297 #define	rdsv3_ib_sg_dma_len(dev, scat)	rdsv3_sg_len(scat)
298 
299 /* copied from sys/socket.h */
300 #if defined(__sparc)
301 /* To maintain backward compatibility, alignment needs to be 8 on sparc. */
302 #define	_CMSG_HDR_ALIGNMENT	8
303 #else
304 /* for __i386 (and other future architectures) */
305 #define	_CMSG_HDR_ALIGNMENT	4
306 #endif	/* defined(__sparc) */
307 
308 /*
309  * The cmsg headers (and macros dealing with them) were made available as
310  * part of UNIX95 and hence need to be protected with a _XPG4_2 define.
311  */
312 #define	_CMSG_DATA_ALIGNMENT	(sizeof (int))
313 #define	_CMSG_HDR_ALIGN(x)	(((uintptr_t)(x) + _CMSG_HDR_ALIGNMENT - 1) & \
314 				    ~(_CMSG_HDR_ALIGNMENT - 1))
315 #define	_CMSG_DATA_ALIGN(x)	(((uintptr_t)(x) + _CMSG_DATA_ALIGNMENT - 1) & \
316 				    ~(_CMSG_DATA_ALIGNMENT - 1))
317 #define	CMSG_DATA(c)							\
318 	((unsigned char *)_CMSG_DATA_ALIGN((struct cmsghdr *)(c) + 1))
319 
320 #define	CMSG_FIRSTHDR(m)						\
321 	(((m)->msg_controllen < sizeof (struct cmsghdr)) ?		\
322 	    (struct cmsghdr *)0 : (struct cmsghdr *)((m)->msg_control))
323 
324 #define	CMSG_NXTHDR(m, c)						\
325 	(((c) == 0) ? CMSG_FIRSTHDR(m) :			\
326 	((((uintptr_t)_CMSG_HDR_ALIGN((char *)(c) +			\
327 	((struct cmsghdr *)(c))->cmsg_len) + sizeof (struct cmsghdr)) >	\
328 	(((uintptr_t)((struct msghdr *)(m))->msg_control) +		\
329 	((uintptr_t)((struct msghdr *)(m))->msg_controllen))) ?		\
330 	((struct cmsghdr *)0) :						\
331 	((struct cmsghdr *)_CMSG_HDR_ALIGN((char *)(c) +		\
332 	    ((struct cmsghdr *)(c))->cmsg_len))))
333 
334 /* Amount of space + padding needed for a message of length l */
335 #define	CMSG_SPACE(l)							\
336 	((unsigned int)_CMSG_HDR_ALIGN(sizeof (struct cmsghdr) + (l)))
337 
338 /* Value to be used in cmsg_len, does not include trailing padding */
339 #define	CMSG_LEN(l)							\
340 	((unsigned int)_CMSG_DATA_ALIGN(sizeof (struct cmsghdr)) + (l))
341 
342 /* OFUV -> IB */
343 #define	RDSV3_IBDEV2HCAHDL(device)	(device)->hca_hdl
344 #define	RDSV3_QP2CHANHDL(qp)		(qp)->ibt_qp
345 #define	RDSV3_PD2PDHDL(pd)		(pd)->ibt_pd
346 #define	RDSV3_CQ2CQHDL(cq)		(cq)->ibt_cq
347 
348 struct rdsv3_hdrs_mr {
349 	ibt_lkey_t	lkey;
350 	caddr_t		addr;
351 	size_t		size;
352 	ibt_mr_hdl_t	hdl;
353 };
354 
355 /* rdsv3_impl.c */
356 void rdsv3_trans_init();
357 boolean_t rdsv3_capable_interface(struct lifreq *lifrp);
358 int rdsv3_do_ip_ioctl(ksocket_t so4, void **ipaddrs, int *size, int *nifs);
359 int rdsv3_do_ip_ioctl_old(ksocket_t so4, void **ipaddrs, int *size, int *nifs);
360 boolean_t rdsv3_isloopback(ipaddr_t addr);
361 void rdsv3_cancel_delayed_work(rdsv3_delayed_work_t *dwp);
362 void rdsv3_flush_workqueue(rdsv3_workqueue_struct_t *wq);
363 void rdsv3_queue_work(rdsv3_workqueue_struct_t *wq, rdsv3_work_t *wp);
364 void rdsv3_queue_delayed_work(rdsv3_workqueue_struct_t *wq,
365     rdsv3_delayed_work_t *dwp, uint_t delay);
366 struct rsock *rdsv3_sk_alloc();
367 void rdsv3_sock_init_data(struct rsock *sk);
368 void rdsv3_sock_exit_data(struct rsock *sk);
369 void rdsv3_destroy_task_workqueue(rdsv3_workqueue_struct_t *wq);
370 rdsv3_workqueue_struct_t *rdsv3_create_task_workqueue(char *name);
371 int rdsv3_conn_constructor(void *buf, void *arg, int kmflags);
372 void rdsv3_conn_destructor(void *buf, void *arg);
373 int rdsv3_conn_compare(const void *conn1, const void *conn2);
374 void rdsv3_loop_init();
375 int rdsv3_mr_compare(const void *mr1, const void *mr2);
376 int rdsv3_put_cmsg(struct nmsghdr *msg, int level, int type, size_t size,
377     void *payload);
378 int rdsv3_verify_bind_address(ipaddr_t addr);
379 uint16_t rdsv3_ip_fast_csum(void *buffer, size_t length);
380 uint_t rdsv3_ib_dma_map_sg(struct ib_device *dev, struct rdsv3_scatterlist
381 	*scat, uint_t num);
382 void rdsv3_ib_dma_unmap_sg(ib_device_t *dev, struct rdsv3_scatterlist *scat,
383     uint_t num);
384 static inline void
385 rdsv3_sk_sock_hold(struct rsock *sk)
386 {
387 	atomic_add_32(&sk->sk_refcount, 1);
388 }
389 static inline void
390 rdsv3_sk_sock_put(struct rsock *sk)
391 {
392 	if (atomic_dec_and_test(&sk->sk_refcount))
393 		rdsv3_sock_exit_data(sk);
394 }
395 static inline int
396 rdsv3_sk_sock_flag(struct rsock *sk, uint_t flag)
397 {
398 	return (test_bit(flag, &sk->sk_flag));
399 }
400 static inline void
401 rdsv3_sk_sock_orphan(struct rsock *sk)
402 {
403 	set_bit(SOCK_DEAD, &sk->sk_flag);
404 }
405 
406 #define	rdsv3_sndtimeo(a, b)	b ? 0 : 3600	/* check this value on linux */
407 #define	rdsv3_rcvtimeo(a, b)	b ? 0 : 3600	/* check this value on linux */
408 
409 void rdsv3_ib_free_conn(void *arg);
410 
411 #ifdef	__cplusplus
412 }
413 #endif
414 
415 #endif /* _RDSV3_IMPL_H */
416