xref: /titanic_50/usr/src/uts/common/rpc/xdr_rdma.c (revision bcd6185e1c81586e6929dc444ef32123263dd51c)
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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Copyright (c) 2007, The Ohio State University. All rights reserved.
27  *
28  * Portions of this source code is developed by the team members of
29  * The Ohio State University's Network-Based Computing Laboratory (NBCL),
30  * headed by Professor Dhabaleswar K. (DK) Panda.
31  *
32  * Acknowledgements to contributions from developors:
33  *   Ranjit Noronha: noronha@cse.ohio-state.edu
34  *   Lei Chai      : chail@cse.ohio-state.edu
35  *   Weikuan Yu    : yuw@cse.ohio-state.edu
36  *
37  */
38 
39 /*
40  * xdr_rdma.c, XDR implementation using RDMA to move large chunks
41  */
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kmem.h>
47 #include <sys/sdt.h>
48 #include <sys/debug.h>
49 
50 #include <rpc/types.h>
51 #include <rpc/xdr.h>
52 #include <sys/cmn_err.h>
53 #include <rpc/rpc_sztypes.h>
54 #include <rpc/rpc_rdma.h>
55 #include <sys/sysmacros.h>
56 
57 /*
58  * RCP header and xdr encoding overhead.  The number was determined by
59  * tracing the msglen in svc_rdma_ksend for sec=sys,krb5,krb5i and krb5p.
60  * If the XDR_RDMA_BUF_OVERHEAD is not large enough the result is the trigger
61  * of the dtrace probe on the server "krpc-e-svcrdma-ksend-noreplycl" from
62  * svc_rdma_ksend.
63  */
64 #define	XDR_RDMA_BUF_OVERHEAD	300
65 
66 static bool_t   xdrrdma_getint32(XDR *, int32_t *);
67 static bool_t   xdrrdma_putint32(XDR *, int32_t *);
68 static bool_t   xdrrdma_getbytes(XDR *, caddr_t, int);
69 static bool_t   xdrrdma_putbytes(XDR *, caddr_t, int);
70 uint_t		xdrrdma_getpos(XDR *);
71 bool_t		xdrrdma_setpos(XDR *, uint_t);
72 static rpc_inline_t *xdrrdma_inline(XDR *, int);
73 void		xdrrdma_destroy(XDR *);
74 static bool_t   xdrrdma_control(XDR *, int, void *);
75 static bool_t  xdrrdma_read_a_chunk(XDR *, CONN **);
76 static void xdrrdma_free_xdr_chunks(CONN *, struct clist *);
77 
78 struct xdr_ops  xdrrdmablk_ops = {
79 	xdrrdma_getbytes,
80 	xdrrdma_putbytes,
81 	xdrrdma_getpos,
82 	xdrrdma_setpos,
83 	xdrrdma_inline,
84 	xdrrdma_destroy,
85 	xdrrdma_control,
86 	xdrrdma_getint32,
87 	xdrrdma_putint32
88 };
89 
90 struct xdr_ops  xdrrdma_ops = {
91 	xdrrdma_getbytes,
92 	xdrrdma_putbytes,
93 	xdrrdma_getpos,
94 	xdrrdma_setpos,
95 	xdrrdma_inline,
96 	xdrrdma_destroy,
97 	xdrrdma_control,
98 	xdrrdma_getint32,
99 	xdrrdma_putint32
100 };
101 
102 /*
103  * A chunk list entry identifies a chunk of opaque data to be moved
104  * separately from the rest of the RPC message. xp_min_chunk = 0, is a
105  * special case for ENCODING, which means do not chunk the incoming stream of
106  * data.
107  *
108  * A read chunk can contain part of the RPC message in addition to the
109  * inline message. In such a case, (xp_offp - x_base) will not provide
110  * the correct xdr offset of the entire message. xp_off is used in such
111  * a case to denote the offset or current position in the overall message
112  * covering both the inline and the chunk. This is used only in the case
113  * of decoding and useful to compare read chunk 'c_xdroff' offsets.
114  *
115  * An example for a read chunk containing an XDR message:
116  * An NFSv4 compound as following:
117  *
118  * PUTFH
119  * WRITE [4109 bytes]
120  * GETATTR
121  *
122  * Solaris Encoding is:
123  * -------------------
124  *
125  * <Inline message>: [PUTFH WRITE4args GETATTR]
126  *                                   |
127  *                                   v
128  * [RDMA_READ chunks]:               [write data]
129  *
130  *
131  * Linux encoding is:
132  * -----------------
133  *
134  * <Inline message>: [PUTFH WRITE4args]
135  *                                    |
136  *                                    v
137  * [RDMA_READ chunks]:                [Write data] [Write data2] [Getattr chunk]
138  *                                     chunk1       chunk2         chunk3
139  *
140  * where the READ chunks are as:
141  *
142  *             - chunk1 - 4k
143  * write data |
144  *             - chunk2 - 13 bytes(4109 - 4k)
145  * getattr op  - chunk3 - 19 bytes
146  * (getattr op starts at byte 4 after 3 bytes of roundup)
147  *
148  */
149 
150 typedef struct {
151 	caddr_t		xp_offp;
152 	int		xp_min_chunk;
153 	uint_t		xp_flags;	/* Controls setting for rdma xdr */
154 	int		xp_buf_size;	/* size of xdr buffer */
155 	int		xp_off;		/* overall offset */
156 	struct clist	*xp_rcl;	/* head of chunk list */
157 	struct clist	**xp_rcl_next;	/* location to place/find next chunk */
158 	struct clist	*xp_rcl_xdr;	/* copy of rcl containing RPC message */
159 	struct clist	*xp_wcl;	/* head of write chunk list */
160 	CONN		*xp_conn;	/* connection for chunk data xfer */
161 	uint_t		xp_reply_chunk_len;
162 	/* used to track length for security modes: integrity/privacy */
163 	uint_t		xp_reply_chunk_len_alt;
164 } xrdma_private_t;
165 
166 extern kmem_cache_t *clist_cache;
167 
168 bool_t
169 xdrrdma_getrdmablk(XDR *xdrs, struct clist **rlist, uint_t *sizep,
170     CONN **conn, const uint_t maxsize)
171 {
172 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
173 	struct clist	*cle = *(xdrp->xp_rcl_next);
174 	struct clist	*rdclist = NULL, *prev = NULL;
175 	bool_t		retval = TRUE;
176 	uint32_t	cur_offset = 0;
177 	uint32_t	total_segments = 0;
178 	uint32_t	actual_segments = 0;
179 	uint32_t	alen;
180 	uint_t		total_len;
181 
182 	ASSERT(xdrs->x_op != XDR_FREE);
183 
184 	/*
185 	 * first deal with the length since xdr bytes are counted
186 	 */
187 	if (!xdr_u_int(xdrs, sizep)) {
188 		DTRACE_PROBE(xdr__e__getrdmablk_sizep_fail);
189 		return (FALSE);
190 	}
191 	total_len = *sizep;
192 	if (total_len > maxsize) {
193 		DTRACE_PROBE2(xdr__e__getrdmablk_bad_size,
194 		    int, total_len, int, maxsize);
195 		return (FALSE);
196 	}
197 	(*conn) = xdrp->xp_conn;
198 
199 	/*
200 	 * if no data we are done
201 	 */
202 	if (total_len == 0)
203 		return (TRUE);
204 
205 	while (cle) {
206 		total_segments++;
207 		cle = cle->c_next;
208 	}
209 
210 	cle = *(xdrp->xp_rcl_next);
211 
212 	/*
213 	 * If there was a chunk at the current offset, then setup a read
214 	 * chunk list which records the destination address and length
215 	 * and will RDMA READ the data in later.
216 	 */
217 	if (cle == NULL)
218 		return (FALSE);
219 
220 	if (cle->c_xdroff != (xdrp->xp_offp - xdrs->x_base))
221 		return (FALSE);
222 
223 	/*
224 	 * Setup the chunk list with appropriate
225 	 * address (offset) and length
226 	 */
227 	for (actual_segments = 0;
228 	    actual_segments < total_segments; actual_segments++) {
229 
230 		DTRACE_PROBE3(krpc__i__xdrrdma_getrdmablk, uint32_t, cle->c_len,
231 		    uint32_t, total_len, uint32_t, cle->c_xdroff);
232 
233 		if (total_len <= 0)
234 			break;
235 
236 		/*
237 		 * not the first time in the loop
238 		 */
239 		if (actual_segments > 0)
240 			cle = cle->c_next;
241 
242 		cle->u.c_daddr = (uint64) cur_offset;
243 		alen = 0;
244 		if (cle->c_len > total_len) {
245 			alen = cle->c_len;
246 			cle->c_len = total_len;
247 		}
248 		if (!alen)
249 			xdrp->xp_rcl_next = &cle->c_next;
250 
251 		cur_offset += cle->c_len;
252 		total_len -= cle->c_len;
253 
254 		if ((total_segments - actual_segments - 1) == 0 &&
255 		    total_len > 0) {
256 			DTRACE_PROBE(krpc__e__xdrrdma_getblk_chunktooshort);
257 			retval = FALSE;
258 		}
259 
260 		if ((total_segments - actual_segments - 1) > 0 &&
261 		    total_len == 0) {
262 			DTRACE_PROBE2(krpc__e__xdrrdma_getblk_toobig,
263 			    int, total_segments, int, actual_segments);
264 		}
265 
266 		rdclist = clist_alloc();
267 		(*rdclist) = (*cle);
268 		if ((*rlist) == NULL)
269 			(*rlist) = rdclist;
270 		if (prev == NULL)
271 			prev = rdclist;
272 		else {
273 			prev->c_next = rdclist;
274 			prev = rdclist;
275 		}
276 
277 	}
278 
279 out:
280 	if (prev != NULL)
281 		prev->c_next = NULL;
282 
283 	/*
284 	 * Adjust the chunk length, if we read only a part of
285 	 * a chunk.
286 	 */
287 
288 	if (alen) {
289 		cle->w.c_saddr =
290 		    (uint64)(uintptr_t)cle->w.c_saddr + cle->c_len;
291 		cle->c_len = alen - cle->c_len;
292 	}
293 
294 	return (retval);
295 }
296 
297 /*
298  * The procedure xdrrdma_create initializes a stream descriptor for a memory
299  * buffer.
300  */
301 void
302 xdrrdma_create(XDR *xdrs, caddr_t addr, uint_t size,
303     int min_chunk, struct clist *cl, enum xdr_op op, CONN *conn)
304 {
305 	xrdma_private_t *xdrp;
306 	struct clist   *cle;
307 
308 	xdrs->x_op = op;
309 	xdrs->x_ops = &xdrrdma_ops;
310 	xdrs->x_base = addr;
311 	xdrs->x_handy = size;
312 	xdrs->x_public = NULL;
313 
314 	xdrp = (xrdma_private_t *)kmem_zalloc(sizeof (xrdma_private_t),
315 	    KM_SLEEP);
316 	xdrs->x_private = (caddr_t)xdrp;
317 	xdrp->xp_offp = addr;
318 	xdrp->xp_min_chunk = min_chunk;
319 	xdrp->xp_flags = 0;
320 	xdrp->xp_buf_size = size;
321 	xdrp->xp_rcl = cl;
322 	xdrp->xp_reply_chunk_len = 0;
323 	xdrp->xp_reply_chunk_len_alt = 0;
324 
325 	if (op == XDR_ENCODE && cl != NULL) {
326 		/* Find last element in chunk list and set xp_rcl_next */
327 		for (cle = cl; cle->c_next != NULL; cle = cle->c_next)
328 			continue;
329 
330 		xdrp->xp_rcl_next = &(cle->c_next);
331 	} else {
332 		xdrp->xp_rcl_next = &(xdrp->xp_rcl);
333 	}
334 
335 	xdrp->xp_wcl = NULL;
336 
337 	xdrp->xp_conn = conn;
338 	if (xdrp->xp_min_chunk != 0)
339 		xdrp->xp_flags |= XDR_RDMA_CHUNK;
340 }
341 
342 /* ARGSUSED */
343 void
344 xdrrdma_destroy(XDR * xdrs)
345 {
346 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
347 
348 	if (xdrp == NULL)
349 		return;
350 
351 	if (xdrp->xp_wcl) {
352 		if (xdrp->xp_flags & XDR_RDMA_WLIST_REG) {
353 			(void) clist_deregister(xdrp->xp_conn, xdrp->xp_wcl);
354 			rdma_buf_free(xdrp->xp_conn,
355 			    &xdrp->xp_wcl->rb_longbuf);
356 		}
357 		clist_free(xdrp->xp_wcl);
358 	}
359 
360 	if (xdrp->xp_rcl) {
361 		if (xdrp->xp_flags & XDR_RDMA_RLIST_REG) {
362 			(void) clist_deregister(xdrp->xp_conn, xdrp->xp_rcl);
363 			rdma_buf_free(xdrp->xp_conn,
364 			    &xdrp->xp_rcl->rb_longbuf);
365 		}
366 		clist_free(xdrp->xp_rcl);
367 	}
368 
369 	if (xdrp->xp_rcl_xdr)
370 		xdrrdma_free_xdr_chunks(xdrp->xp_conn, xdrp->xp_rcl_xdr);
371 
372 	(void) kmem_free(xdrs->x_private, sizeof (xrdma_private_t));
373 	xdrs->x_private = NULL;
374 }
375 
376 static	bool_t
377 xdrrdma_getint32(XDR *xdrs, int32_t *int32p)
378 {
379 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
380 	int chunked = 0;
381 
382 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0) {
383 		/*
384 		 * check if rest of the rpc message is in a chunk
385 		 */
386 		if (!xdrrdma_read_a_chunk(xdrs, &xdrp->xp_conn)) {
387 			return (FALSE);
388 		}
389 		chunked = 1;
390 	}
391 
392 	/* LINTED pointer alignment */
393 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrp->xp_offp))));
394 
395 	DTRACE_PROBE1(krpc__i__xdrrdma_getint32, int32_t, *int32p);
396 
397 	xdrp->xp_offp += sizeof (int32_t);
398 
399 	if (chunked)
400 		xdrs->x_handy -= (int)sizeof (int32_t);
401 
402 	if (xdrp->xp_off != 0) {
403 		xdrp->xp_off += sizeof (int32_t);
404 	}
405 
406 	return (TRUE);
407 }
408 
409 static	bool_t
410 xdrrdma_putint32(XDR *xdrs, int32_t *int32p)
411 {
412 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
413 
414 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
415 		return (FALSE);
416 
417 	/* LINTED pointer alignment */
418 	*(int32_t *)xdrp->xp_offp = (int32_t)htonl((uint32_t)(*int32p));
419 	xdrp->xp_offp += sizeof (int32_t);
420 
421 	return (TRUE);
422 }
423 
424 /*
425  * DECODE bytes from XDR stream for rdma.
426  * If the XDR stream contains a read chunk list,
427  * it will go through xdrrdma_getrdmablk instead.
428  */
429 static	bool_t
430 xdrrdma_getbytes(XDR *xdrs, caddr_t addr, int len)
431 {
432 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
433 	struct clist	*cle = *(xdrp->xp_rcl_next);
434 	struct clist	*cls = *(xdrp->xp_rcl_next);
435 	struct clist	cl;
436 	bool_t		retval = TRUE;
437 	uint32_t	total_len = len;
438 	uint32_t	cur_offset = 0;
439 	uint32_t	total_segments = 0;
440 	uint32_t	actual_segments = 0;
441 	uint32_t	status = RDMA_SUCCESS;
442 	uint32_t	alen = 0;
443 	uint32_t	xpoff;
444 
445 	while (cle) {
446 		total_segments++;
447 		cle = cle->c_next;
448 	}
449 
450 	cle = *(xdrp->xp_rcl_next);
451 
452 	if (xdrp->xp_off) {
453 		xpoff = xdrp->xp_off;
454 	} else {
455 		xpoff = (xdrp->xp_offp - xdrs->x_base);
456 	}
457 
458 	/*
459 	 * If there was a chunk at the current offset, then setup a read
460 	 * chunk list which records the destination address and length
461 	 * and will RDMA READ the data in later.
462 	 */
463 
464 	if (cle != NULL && cle->c_xdroff == xpoff) {
465 		for (actual_segments = 0;
466 		    actual_segments < total_segments; actual_segments++) {
467 
468 			if (total_len <= 0)
469 				break;
470 
471 			if (status != RDMA_SUCCESS)
472 				goto out;
473 
474 			cle->u.c_daddr = (uint64)(uintptr_t)addr + cur_offset;
475 			alen = 0;
476 			if (cle->c_len > total_len) {
477 				alen = cle->c_len;
478 				cle->c_len = total_len;
479 			}
480 			if (!alen)
481 				xdrp->xp_rcl_next = &cle->c_next;
482 
483 			cur_offset += cle->c_len;
484 			total_len -= cle->c_len;
485 
486 			if ((total_segments - actual_segments - 1) == 0 &&
487 			    total_len > 0) {
488 				DTRACE_PROBE(
489 				    krpc__e__xdrrdma_getbytes_chunktooshort);
490 				retval = FALSE;
491 			}
492 
493 			if ((total_segments - actual_segments - 1) > 0 &&
494 			    total_len == 0) {
495 				DTRACE_PROBE2(krpc__e__xdrrdma_getbytes_toobig,
496 				    int, total_segments, int, actual_segments);
497 			}
498 
499 			/*
500 			 * RDMA READ the chunk data from the remote end.
501 			 * First prep the destination buffer by registering
502 			 * it, then RDMA READ the chunk data. Since we are
503 			 * doing streaming memory, sync the destination
504 			 * buffer to CPU and deregister the buffer.
505 			 */
506 			if (xdrp->xp_conn == NULL) {
507 				return (FALSE);
508 			}
509 			cl = *cle;
510 			cl.c_next = NULL;
511 			status = clist_register(xdrp->xp_conn, &cl,
512 			    CLIST_REG_DST);
513 			if (status != RDMA_SUCCESS) {
514 				retval = FALSE;
515 				/*
516 				 * Deregister the previous chunks
517 				 * before return
518 				 */
519 				goto out;
520 			}
521 
522 			cle->c_dmemhandle = cl.c_dmemhandle;
523 			cle->c_dsynchandle = cl.c_dsynchandle;
524 
525 			/*
526 			 * Now read the chunk in
527 			 */
528 			if ((total_segments - actual_segments - 1) == 0 ||
529 			    total_len == 0) {
530 				status = RDMA_READ(xdrp->xp_conn, &cl, WAIT);
531 			} else {
532 				status = RDMA_READ(xdrp->xp_conn, &cl, NOWAIT);
533 			}
534 			if (status != RDMA_SUCCESS) {
535 				DTRACE_PROBE1(
536 				    krpc__i__xdrrdma_getblk_readfailed,
537 				    int, status);
538 				retval = FALSE;
539 			}
540 
541 			cle = cle->c_next;
542 
543 		}
544 
545 		/*
546 		 * sync the memory for cpu
547 		 */
548 		cl = *cls;
549 		cl.c_next = NULL;
550 		cl.c_len = cur_offset;
551 		if (clist_syncmem(
552 		    xdrp->xp_conn, &cl, CLIST_REG_DST) != RDMA_SUCCESS) {
553 			retval = FALSE;
554 		}
555 out:
556 
557 		/*
558 		 * Deregister the chunks
559 		 */
560 		cle = cls;
561 		while (actual_segments != 0) {
562 			cl = *cle;
563 			cl.c_next = NULL;
564 
565 			cl.c_regtype = CLIST_REG_DST;
566 			(void) clist_deregister(xdrp->xp_conn, &cl);
567 
568 			cle = cle->c_next;
569 			actual_segments--;
570 		}
571 
572 		if (alen) {
573 			cle = *(xdrp->xp_rcl_next);
574 			cle->w.c_saddr =
575 			    (uint64)(uintptr_t)cle->w.c_saddr + cle->c_len;
576 			cle->c_len = alen - cle->c_len;
577 		}
578 
579 		return (retval);
580 	}
581 
582 	if ((xdrs->x_handy -= len) < 0)
583 		return (FALSE);
584 
585 	bcopy(xdrp->xp_offp, addr, len);
586 
587 	xdrp->xp_offp += len;
588 
589 	if (xdrp->xp_off != 0)
590 		xdrp->xp_off += len;
591 
592 	return (TRUE);
593 }
594 
595 /*
596  * ENCODE some bytes into an XDR stream xp_min_chunk = 0, means the stream of
597  * bytes contain no chunks to seperate out, and if the bytes do not fit in
598  * the supplied buffer, grow the buffer and free the old buffer.
599  */
600 static	bool_t
601 xdrrdma_putbytes(XDR *xdrs, caddr_t addr, int len)
602 {
603 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
604 	/*
605 	 * Is this stream accepting chunks?
606 	 * If so, does the either of the two following conditions exist?
607 	 * - length of bytes to encode is greater than the min chunk size?
608 	 * - remaining space in this stream is shorter than length of
609 	 *   bytes to encode?
610 	 *
611 	 * If the above exists, then create a chunk for this encoding
612 	 * and save the addresses, etc.
613 	 */
614 	if (xdrp->xp_flags & XDR_RDMA_CHUNK &&
615 	    ((xdrp->xp_min_chunk != 0 &&
616 	    len >= xdrp->xp_min_chunk) ||
617 	    (xdrs->x_handy - len  < 0))) {
618 		struct clist	*cle;
619 		int		offset = xdrp->xp_offp - xdrs->x_base;
620 
621 		cle = clist_alloc();
622 		cle->c_xdroff = offset;
623 		cle->c_len = len;
624 		cle->w.c_saddr = (uint64)(uintptr_t)addr;
625 		cle->c_next = NULL;
626 
627 		*(xdrp->xp_rcl_next) = cle;
628 		xdrp->xp_rcl_next = &(cle->c_next);
629 
630 		return (TRUE);
631 	}
632 	/* Is there enough space to encode what is left? */
633 	if ((xdrs->x_handy -= len) < 0) {
634 		return (FALSE);
635 	}
636 	bcopy(addr, xdrp->xp_offp, len);
637 	xdrp->xp_offp += len;
638 
639 	return (TRUE);
640 }
641 
642 uint_t
643 xdrrdma_getpos(XDR *xdrs)
644 {
645 	xrdma_private_t *xdrp = (xrdma_private_t *)(xdrs->x_private);
646 
647 	return ((uint_t)((uintptr_t)xdrp->xp_offp - (uintptr_t)xdrs->x_base));
648 }
649 
650 bool_t
651 xdrrdma_setpos(XDR *xdrs, uint_t pos)
652 {
653 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
654 
655 	caddr_t		newaddr = xdrs->x_base + pos;
656 	caddr_t		lastaddr = xdrp->xp_offp + xdrs->x_handy;
657 	ptrdiff_t	diff;
658 
659 	if (newaddr > lastaddr)
660 		return (FALSE);
661 
662 	xdrp->xp_offp = newaddr;
663 	diff = lastaddr - newaddr;
664 	xdrs->x_handy = (int)diff;
665 
666 	return (TRUE);
667 }
668 
669 /* ARGSUSED */
670 static rpc_inline_t *
671 xdrrdma_inline(XDR *xdrs, int len)
672 {
673 	rpc_inline_t	*buf = NULL;
674 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
675 	struct clist	*cle = *(xdrp->xp_rcl_next);
676 
677 	if (xdrs->x_op == XDR_DECODE) {
678 		/*
679 		 * Since chunks aren't in-line, check to see whether there is
680 		 * a chunk in the inline range.
681 		 */
682 		if (cle != NULL &&
683 		    cle->c_xdroff <= (xdrp->xp_offp - xdrs->x_base + len))
684 			return (NULL);
685 	}
686 
687 	/* LINTED pointer alignment */
688 	buf = (rpc_inline_t *)xdrp->xp_offp;
689 	if (!IS_P2ALIGNED(buf, sizeof (int32_t)))
690 		return (NULL);
691 
692 	if ((xdrs->x_handy < len) || (xdrp->xp_min_chunk != 0 &&
693 	    len >= xdrp->xp_min_chunk)) {
694 		return (NULL);
695 	} else {
696 		xdrs->x_handy -= len;
697 		xdrp->xp_offp += len;
698 		return (buf);
699 	}
700 }
701 
702 static	bool_t
703 xdrrdma_control(XDR *xdrs, int request, void *info)
704 {
705 	int32_t		*int32p;
706 	int		len, i;
707 	uint_t		in_flags;
708 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
709 	rdma_chunkinfo_t *rcip = NULL;
710 	rdma_wlist_conn_info_t *rwcip = NULL;
711 	rdma_chunkinfo_lengths_t *rcilp = NULL;
712 	struct uio *uiop;
713 	struct clist	*rwl = NULL;
714 	struct clist	*prev = NULL;
715 
716 	switch (request) {
717 	case XDR_PEEK:
718 		/*
719 		 * Return the next 4 byte unit in the XDR stream.
720 		 */
721 		if (xdrs->x_handy < sizeof (int32_t))
722 			return (FALSE);
723 
724 		int32p = (int32_t *)info;
725 		*int32p = (int32_t)ntohl((uint32_t)
726 		    (*((int32_t *)(xdrp->xp_offp))));
727 
728 		return (TRUE);
729 
730 	case XDR_SKIPBYTES:
731 		/*
732 		 * Skip the next N bytes in the XDR stream.
733 		 */
734 		int32p = (int32_t *)info;
735 		len = RNDUP((int)(*int32p));
736 		if ((xdrs->x_handy -= len) < 0)
737 			return (FALSE);
738 		xdrp->xp_offp += len;
739 
740 		return (TRUE);
741 
742 	case XDR_RDMA_SET_FLAGS:
743 		/*
744 		 * Set the flags provided in the *info in xp_flags for rdma
745 		 * xdr stream control.
746 		 */
747 		int32p = (int32_t *)info;
748 		in_flags = (uint_t)(*int32p);
749 
750 		xdrp->xp_flags |= in_flags;
751 		return (TRUE);
752 
753 	case XDR_RDMA_GET_FLAGS:
754 		/*
755 		 * Get the flags provided in xp_flags return through *info
756 		 */
757 		int32p = (int32_t *)info;
758 
759 		*int32p = (int32_t)xdrp->xp_flags;
760 		return (TRUE);
761 
762 	case XDR_RDMA_GET_CHUNK_LEN:
763 		rcilp = (rdma_chunkinfo_lengths_t *)info;
764 		rcilp->rcil_len = xdrp->xp_reply_chunk_len;
765 		rcilp->rcil_len_alt = xdrp->xp_reply_chunk_len_alt;
766 
767 		return (TRUE);
768 
769 	case XDR_RDMA_ADD_CHUNK:
770 		/*
771 		 * Store wlist information
772 		 */
773 
774 		rcip = (rdma_chunkinfo_t *)info;
775 
776 		DTRACE_PROBE2(krpc__i__xdrrdma__control__add__chunk,
777 		    rci_type_t, rcip->rci_type, uint32, rcip->rci_len);
778 		switch (rcip->rci_type) {
779 		case RCI_WRITE_UIO_CHUNK:
780 			xdrp->xp_reply_chunk_len_alt += rcip->rci_len;
781 
782 			if ((rcip->rci_len + XDR_RDMA_BUF_OVERHEAD) <
783 			    xdrp->xp_min_chunk) {
784 				xdrp->xp_wcl = NULL;
785 				*(rcip->rci_clpp) = NULL;
786 				return (TRUE);
787 			}
788 			uiop = rcip->rci_a.rci_uiop;
789 
790 			for (i = 0; i < uiop->uio_iovcnt; i++) {
791 				rwl = clist_alloc();
792 				rwl->c_len = uiop->uio_iov[i].iov_len;
793 				rwl->u.c_daddr =
794 				    (uint64)(uintptr_t)
795 				    (uiop->uio_iov[i].iov_base);
796 				/*
797 				 * if userspace address, put adspace ptr in
798 				 * clist. If not, then do nothing since it's
799 				 * already set to NULL (from kmem_zalloc)
800 				 */
801 				if (uiop->uio_segflg == UIO_USERSPACE) {
802 					rwl->c_adspc = ttoproc(curthread)->p_as;
803 				}
804 
805 				if (prev == NULL)
806 					prev = rwl;
807 				else {
808 					prev->c_next = rwl;
809 					prev = rwl;
810 				}
811 			}
812 
813 			rwl->c_next = NULL;
814 			xdrp->xp_wcl = rwl;
815 			*(rcip->rci_clpp) = rwl;
816 
817 			break;
818 
819 		case RCI_WRITE_ADDR_CHUNK:
820 			rwl = clist_alloc();
821 
822 			rwl->c_len = rcip->rci_len;
823 			rwl->u.c_daddr3 = rcip->rci_a.rci_addr;
824 			rwl->c_next = NULL;
825 			xdrp->xp_reply_chunk_len_alt += rcip->rci_len;
826 
827 			xdrp->xp_wcl = rwl;
828 			*(rcip->rci_clpp) = rwl;
829 
830 			break;
831 
832 		case RCI_REPLY_CHUNK:
833 			xdrp->xp_reply_chunk_len += rcip->rci_len;
834 			break;
835 		}
836 		return (TRUE);
837 
838 	case XDR_RDMA_GET_WLIST:
839 		*((struct clist **)info) = xdrp->xp_wcl;
840 		return (TRUE);
841 
842 	case XDR_RDMA_SET_WLIST:
843 		xdrp->xp_wcl = (struct clist *)info;
844 		return (TRUE);
845 
846 	case XDR_RDMA_GET_RLIST:
847 		*((struct clist **)info) = xdrp->xp_rcl;
848 		return (TRUE);
849 
850 	case XDR_RDMA_GET_WCINFO:
851 		rwcip = (rdma_wlist_conn_info_t *)info;
852 
853 		rwcip->rwci_wlist = xdrp->xp_wcl;
854 		rwcip->rwci_conn = xdrp->xp_conn;
855 
856 		return (TRUE);
857 
858 	default:
859 		return (FALSE);
860 	}
861 }
862 
863 bool_t xdr_do_clist(XDR *, clist **);
864 
865 /*
866  * Not all fields in struct clist are interesting to the RPC over RDMA
867  * protocol. Only XDR the interesting fields.
868  */
869 bool_t
870 xdr_clist(XDR *xdrs, clist *objp)
871 {
872 	if (!xdr_uint32(xdrs, &objp->c_xdroff))
873 		return (FALSE);
874 	if (!xdr_uint32(xdrs, &objp->c_smemhandle.mrc_rmr))
875 		return (FALSE);
876 	if (!xdr_uint32(xdrs, &objp->c_len))
877 		return (FALSE);
878 	if (!xdr_uint64(xdrs, &objp->w.c_saddr))
879 		return (FALSE);
880 	if (!xdr_do_clist(xdrs, &objp->c_next))
881 		return (FALSE);
882 	return (TRUE);
883 }
884 
885 /*
886  * The following two functions are forms of xdr_pointer()
887  * and xdr_reference(). Since the generic versions just
888  * kmem_alloc() a new clist, we actually want to use the
889  * rdma_clist kmem_cache.
890  */
891 
892 /*
893  * Generate or free a clist structure from the
894  * kmem_cache "rdma_clist"
895  */
896 bool_t
897 xdr_ref_clist(XDR *xdrs, caddr_t *pp)
898 {
899 	caddr_t loc = *pp;
900 	bool_t stat;
901 
902 	if (loc == NULL) {
903 		switch (xdrs->x_op) {
904 		case XDR_FREE:
905 			return (TRUE);
906 
907 		case XDR_DECODE:
908 			*pp = loc = (caddr_t)clist_alloc();
909 			break;
910 
911 		case XDR_ENCODE:
912 			ASSERT(loc);
913 			break;
914 		}
915 	}
916 
917 	stat = xdr_clist(xdrs, (struct clist *)loc);
918 
919 	if (xdrs->x_op == XDR_FREE) {
920 		kmem_cache_free(clist_cache, loc);
921 		*pp = NULL;
922 	}
923 	return (stat);
924 }
925 
926 /*
927  * XDR a pointer to a possibly recursive clist. This differs
928  * with xdr_reference in that it can serialize/deserialiaze
929  * trees correctly.
930  *
931  *  What is sent is actually a union:
932  *
933  *  union object_pointer switch (boolean b) {
934  *  case TRUE: object_data data;
935  *  case FALSE: void nothing;
936  *  }
937  *
938  * > objpp: Pointer to the pointer to the object.
939  *
940  */
941 
942 bool_t
943 xdr_do_clist(XDR *xdrs, clist **objpp)
944 {
945 	bool_t more_data;
946 
947 	more_data = (*objpp != NULL);
948 	if (!xdr_bool(xdrs, &more_data))
949 		return (FALSE);
950 	if (!more_data) {
951 		*objpp = NULL;
952 		return (TRUE);
953 	}
954 	return (xdr_ref_clist(xdrs, (caddr_t *)objpp));
955 }
956 
957 uint_t
958 xdr_getbufsize(XDR *xdrs)
959 {
960 	xrdma_private_t *xdrp = (xrdma_private_t *)(xdrs->x_private);
961 
962 	return ((uint_t)xdrp->xp_buf_size);
963 }
964 
965 /* ARGSUSED */
966 bool_t
967 xdr_encode_rlist_svc(XDR *xdrs, clist *rlist)
968 {
969 	bool_t	vfalse = FALSE;
970 
971 	ASSERT(rlist == NULL);
972 	return (xdr_bool(xdrs, &vfalse));
973 }
974 
975 bool_t
976 xdr_encode_wlist(XDR *xdrs, clist *w)
977 {
978 	bool_t		vfalse = FALSE, vtrue = TRUE;
979 	int		i;
980 	uint_t		num_segment = 0;
981 	struct clist	*cl;
982 
983 	/* does a wlist exist? */
984 	if (w == NULL) {
985 		return (xdr_bool(xdrs, &vfalse));
986 	}
987 	/* Encode N consecutive segments, 1, N, HLOO, ..., HLOO, 0 */
988 	if (!xdr_bool(xdrs, &vtrue))
989 		return (FALSE);
990 
991 	for (cl = w; cl != NULL; cl = cl->c_next) {
992 		num_segment++;
993 	}
994 
995 	if (!xdr_uint32(xdrs, &num_segment))
996 		return (FALSE);
997 	for (i = 0; i < num_segment; i++) {
998 
999 		DTRACE_PROBE1(krpc__i__xdr_encode_wlist_len, uint_t, w->c_len);
1000 
1001 		if (!xdr_uint32(xdrs, &w->c_dmemhandle.mrc_rmr))
1002 			return (FALSE);
1003 
1004 		if (!xdr_uint32(xdrs, &w->c_len))
1005 			return (FALSE);
1006 
1007 		if (!xdr_uint64(xdrs, &w->u.c_daddr))
1008 			return (FALSE);
1009 
1010 		w = w->c_next;
1011 	}
1012 
1013 	if (!xdr_bool(xdrs, &vfalse))
1014 		return (FALSE);
1015 
1016 	return (TRUE);
1017 }
1018 
1019 
1020 /*
1021  * Conditionally decode a RDMA WRITE chunk list from XDR stream.
1022  *
1023  * If the next boolean in the XDR stream is false there is no
1024  * RDMA WRITE chunk list present. Otherwise iterate over the
1025  * array and for each entry: allocate a struct clist and decode.
1026  * Pass back an indication via wlist_exists if we have seen a
1027  * RDMA WRITE chunk list.
1028  */
1029 bool_t
1030 xdr_decode_wlist(XDR *xdrs, struct clist **w, bool_t *wlist_exists)
1031 {
1032 	struct clist	*tmp;
1033 	bool_t		more = FALSE;
1034 	uint32_t	seg_array_len;
1035 	uint32_t	i;
1036 
1037 	if (!xdr_bool(xdrs, &more))
1038 		return (FALSE);
1039 
1040 	/* is there a wlist? */
1041 	if (more == FALSE) {
1042 		*wlist_exists = FALSE;
1043 		return (TRUE);
1044 	}
1045 	*wlist_exists = TRUE;
1046 
1047 	if (!xdr_uint32(xdrs, &seg_array_len))
1048 		return (FALSE);
1049 
1050 	tmp = *w = clist_alloc();
1051 	for (i = 0; i < seg_array_len; i++) {
1052 
1053 		if (!xdr_uint32(xdrs, &tmp->c_dmemhandle.mrc_rmr))
1054 			return (FALSE);
1055 		if (!xdr_uint32(xdrs, &tmp->c_len))
1056 			return (FALSE);
1057 
1058 		DTRACE_PROBE1(krpc__i__xdr_decode_wlist_len,
1059 		    uint_t, tmp->c_len);
1060 
1061 		if (!xdr_uint64(xdrs, &tmp->u.c_daddr))
1062 			return (FALSE);
1063 		if (i < seg_array_len - 1) {
1064 			tmp->c_next = clist_alloc();
1065 			tmp = tmp->c_next;
1066 		} else {
1067 			tmp->c_next = NULL;
1068 		}
1069 	}
1070 
1071 	more = FALSE;
1072 	if (!xdr_bool(xdrs, &more))
1073 		return (FALSE);
1074 
1075 	return (TRUE);
1076 }
1077 
1078 /*
1079  * Server side RDMA WRITE list decode.
1080  * XDR context is memory ops
1081  */
1082 bool_t
1083 xdr_decode_wlist_svc(XDR *xdrs, struct clist **wclp, bool_t *wwl,
1084     uint32_t *total_length, CONN *conn)
1085 {
1086 	struct clist	*first, *ncl;
1087 	char		*memp;
1088 	uint32_t	num_wclist;
1089 	uint32_t	wcl_length = 0;
1090 	uint32_t	i;
1091 	bool_t		more = FALSE;
1092 
1093 	*wclp = NULL;
1094 	*wwl = FALSE;
1095 	*total_length = 0;
1096 
1097 	if (!xdr_bool(xdrs, &more)) {
1098 		return (FALSE);
1099 	}
1100 
1101 	if (more == FALSE) {
1102 		return (TRUE);
1103 	}
1104 
1105 	*wwl = TRUE;
1106 
1107 	if (!xdr_uint32(xdrs, &num_wclist)) {
1108 		DTRACE_PROBE(krpc__e__xdrrdma__wlistsvc__listlength);
1109 		return (FALSE);
1110 	}
1111 
1112 	first = ncl = clist_alloc();
1113 
1114 	for (i = 0; i < num_wclist; i++) {
1115 
1116 		if (!xdr_uint32(xdrs, &ncl->c_dmemhandle.mrc_rmr))
1117 			goto err_out;
1118 		if (!xdr_uint32(xdrs, &ncl->c_len))
1119 			goto err_out;
1120 		if (!xdr_uint64(xdrs, &ncl->u.c_daddr))
1121 			goto err_out;
1122 
1123 		if (ncl->c_len > MAX_SVC_XFER_SIZE) {
1124 			DTRACE_PROBE(
1125 			    krpc__e__xdrrdma__wlistsvc__chunklist_toobig);
1126 			ncl->c_len = MAX_SVC_XFER_SIZE;
1127 		}
1128 
1129 		DTRACE_PROBE1(krpc__i__xdr_decode_wlist_svc_len,
1130 		    uint_t, ncl->c_len);
1131 
1132 		wcl_length += ncl->c_len;
1133 
1134 		if (i < num_wclist - 1) {
1135 			ncl->c_next = clist_alloc();
1136 			ncl = ncl->c_next;
1137 		}
1138 	}
1139 
1140 	if (!xdr_bool(xdrs, &more))
1141 		goto err_out;
1142 
1143 	first->rb_longbuf.type = RDMA_LONG_BUFFER;
1144 	first->rb_longbuf.len =
1145 	    wcl_length > WCL_BUF_LEN ? wcl_length : WCL_BUF_LEN;
1146 
1147 	if (rdma_buf_alloc(conn, &first->rb_longbuf)) {
1148 		clist_free(first);
1149 		return (FALSE);
1150 	}
1151 
1152 	memp = first->rb_longbuf.addr;
1153 
1154 	ncl = first;
1155 	for (i = 0; i < num_wclist; i++) {
1156 		ncl->w.c_saddr3 = (caddr_t)memp;
1157 		memp += ncl->c_len;
1158 		ncl = ncl->c_next;
1159 	}
1160 
1161 	*wclp = first;
1162 	*total_length = wcl_length;
1163 	return (TRUE);
1164 
1165 err_out:
1166 	clist_free(first);
1167 	return (FALSE);
1168 }
1169 
1170 /*
1171  * XDR decode the long reply write chunk.
1172  */
1173 bool_t
1174 xdr_decode_reply_wchunk(XDR *xdrs, struct clist **clist)
1175 {
1176 	bool_t		have_rchunk = FALSE;
1177 	struct clist	*first = NULL, *ncl = NULL;
1178 	uint32_t	num_wclist;
1179 	uint32_t	i;
1180 
1181 	if (!xdr_bool(xdrs, &have_rchunk))
1182 		return (FALSE);
1183 
1184 	if (have_rchunk == FALSE)
1185 		return (TRUE);
1186 
1187 	if (!xdr_uint32(xdrs, &num_wclist)) {
1188 		DTRACE_PROBE(krpc__e__xdrrdma__replywchunk__listlength);
1189 		return (FALSE);
1190 	}
1191 
1192 	if (num_wclist == 0) {
1193 		return (FALSE);
1194 	}
1195 
1196 	first = ncl = clist_alloc();
1197 
1198 	for (i = 0; i < num_wclist; i++) {
1199 
1200 		if (i > 0) {
1201 			ncl->c_next = clist_alloc();
1202 			ncl = ncl->c_next;
1203 		}
1204 
1205 		if (!xdr_uint32(xdrs, &ncl->c_dmemhandle.mrc_rmr))
1206 			goto err_out;
1207 		if (!xdr_uint32(xdrs, &ncl->c_len))
1208 			goto err_out;
1209 		if (!xdr_uint64(xdrs, &ncl->u.c_daddr))
1210 			goto err_out;
1211 
1212 		if (ncl->c_len > MAX_SVC_XFER_SIZE) {
1213 			DTRACE_PROBE(
1214 			    krpc__e__xdrrdma__replywchunk__chunklist_toobig);
1215 			ncl->c_len = MAX_SVC_XFER_SIZE;
1216 		}
1217 		if (!(ncl->c_dmemhandle.mrc_rmr &&
1218 		    (ncl->c_len > 0) && ncl->u.c_daddr))
1219 			DTRACE_PROBE(
1220 			    krpc__e__xdrrdma__replywchunk__invalid_segaddr);
1221 
1222 		DTRACE_PROBE1(krpc__i__xdr_decode_reply_wchunk_c_len,
1223 		    uint32_t, ncl->c_len);
1224 
1225 	}
1226 	*clist = first;
1227 	return (TRUE);
1228 
1229 err_out:
1230 	clist_free(first);
1231 	return (FALSE);
1232 }
1233 
1234 
1235 bool_t
1236 xdr_encode_reply_wchunk(XDR *xdrs,
1237     struct clist *cl_longreply, uint32_t seg_array_len)
1238 {
1239 	int		i;
1240 	bool_t		long_reply_exists = TRUE;
1241 	uint32_t	length;
1242 	uint64		offset;
1243 
1244 	if (seg_array_len > 0) {
1245 		if (!xdr_bool(xdrs, &long_reply_exists))
1246 			return (FALSE);
1247 		if (!xdr_uint32(xdrs, &seg_array_len))
1248 			return (FALSE);
1249 
1250 		for (i = 0; i < seg_array_len; i++) {
1251 			if (!cl_longreply)
1252 				return (FALSE);
1253 			length = cl_longreply->c_len;
1254 			offset = (uint64) cl_longreply->u.c_daddr;
1255 
1256 			DTRACE_PROBE1(
1257 			    krpc__i__xdr_encode_reply_wchunk_c_len,
1258 			    uint32_t, length);
1259 
1260 			if (!xdr_uint32(xdrs,
1261 			    &cl_longreply->c_dmemhandle.mrc_rmr))
1262 				return (FALSE);
1263 			if (!xdr_uint32(xdrs, &length))
1264 				return (FALSE);
1265 			if (!xdr_uint64(xdrs, &offset))
1266 				return (FALSE);
1267 			cl_longreply = cl_longreply->c_next;
1268 		}
1269 	} else {
1270 		long_reply_exists = FALSE;
1271 		if (!xdr_bool(xdrs, &long_reply_exists))
1272 			return (FALSE);
1273 	}
1274 	return (TRUE);
1275 }
1276 bool_t
1277 xdrrdma_read_from_client(struct clist *rlist, CONN **conn, uint_t count)
1278 {
1279 	struct clist	*rdclist;
1280 	struct clist	cl;
1281 	uint_t		total_len = 0;
1282 	uint32_t	status;
1283 	bool_t		retval = TRUE;
1284 
1285 	rlist->rb_longbuf.type = RDMA_LONG_BUFFER;
1286 	rlist->rb_longbuf.len =
1287 	    count > RCL_BUF_LEN ? count : RCL_BUF_LEN;
1288 
1289 	if (rdma_buf_alloc(*conn, &rlist->rb_longbuf)) {
1290 		return (FALSE);
1291 	}
1292 
1293 	/*
1294 	 * The entire buffer is registered with the first chunk.
1295 	 * Later chunks will use the same registered memory handle.
1296 	 */
1297 
1298 	cl = *rlist;
1299 	cl.c_next = NULL;
1300 	if (clist_register(*conn, &cl, CLIST_REG_DST) != RDMA_SUCCESS) {
1301 		rdma_buf_free(*conn, &rlist->rb_longbuf);
1302 		DTRACE_PROBE(
1303 		    krpc__e__xdrrdma__readfromclient__clist__reg);
1304 		return (FALSE);
1305 	}
1306 
1307 	rlist->c_regtype = CLIST_REG_DST;
1308 	rlist->c_dmemhandle = cl.c_dmemhandle;
1309 	rlist->c_dsynchandle = cl.c_dsynchandle;
1310 
1311 	for (rdclist = rlist;
1312 	    rdclist != NULL; rdclist = rdclist->c_next) {
1313 		total_len += rdclist->c_len;
1314 #if (defined(OBJ32)||defined(DEBUG32))
1315 		rdclist->u.c_daddr3 =
1316 		    (caddr_t)((char *)rlist->rb_longbuf.addr +
1317 		    (uint32) rdclist->u.c_daddr3);
1318 #else
1319 		rdclist->u.c_daddr3 =
1320 		    (caddr_t)((char *)rlist->rb_longbuf.addr +
1321 		    (uint64) rdclist->u.c_daddr);
1322 
1323 #endif
1324 		cl = (*rdclist);
1325 		cl.c_next = NULL;
1326 
1327 		/*
1328 		 * Use the same memory handle for all the chunks
1329 		 */
1330 		cl.c_dmemhandle = rlist->c_dmemhandle;
1331 		cl.c_dsynchandle = rlist->c_dsynchandle;
1332 
1333 
1334 		DTRACE_PROBE1(krpc__i__xdrrdma__readfromclient__buflen,
1335 		    int, rdclist->c_len);
1336 
1337 		/*
1338 		 * Now read the chunk in
1339 		 */
1340 		if (rdclist->c_next == NULL) {
1341 			status = RDMA_READ(*conn, &cl, WAIT);
1342 		} else {
1343 			status = RDMA_READ(*conn, &cl, NOWAIT);
1344 		}
1345 		if (status != RDMA_SUCCESS) {
1346 			DTRACE_PROBE(
1347 			    krpc__e__xdrrdma__readfromclient__readfailed);
1348 			rdma_buf_free(*conn, &rlist->rb_longbuf);
1349 			return (FALSE);
1350 		}
1351 	}
1352 
1353 	cl = (*rlist);
1354 	cl.c_next = NULL;
1355 	cl.c_len = total_len;
1356 	if (clist_syncmem(*conn, &cl, CLIST_REG_DST) != RDMA_SUCCESS) {
1357 		retval = FALSE;
1358 	}
1359 	return (retval);
1360 }
1361 
1362 bool_t
1363 xdrrdma_free_clist(CONN *conn, struct clist *clp)
1364 {
1365 	rdma_buf_free(conn, &clp->rb_longbuf);
1366 	clist_free(clp);
1367 	return (TRUE);
1368 }
1369 
1370 bool_t
1371 xdrrdma_send_read_data(XDR *xdrs, uint_t data_len, struct clist *wcl)
1372 {
1373 	int status;
1374 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
1375 	struct xdr_ops *xops = xdrrdma_xops();
1376 	struct clist *tcl, *wrcl, *cl;
1377 	struct clist fcl;
1378 	int rndup_present, rnduplen;
1379 
1380 	rndup_present = 0;
1381 	wrcl = NULL;
1382 
1383 	/* caller is doing a sizeof */
1384 	if (xdrs->x_ops != &xdrrdma_ops || xdrs->x_ops == xops)
1385 		return (TRUE);
1386 
1387 	/* copy of the first chunk */
1388 	fcl = *wcl;
1389 	fcl.c_next = NULL;
1390 
1391 	/*
1392 	 * The entire buffer is registered with the first chunk.
1393 	 * Later chunks will use the same registered memory handle.
1394 	 */
1395 
1396 	status = clist_register(xdrp->xp_conn, &fcl, CLIST_REG_SOURCE);
1397 	if (status != RDMA_SUCCESS) {
1398 		return (FALSE);
1399 	}
1400 
1401 	wcl->c_regtype = CLIST_REG_SOURCE;
1402 	wcl->c_smemhandle = fcl.c_smemhandle;
1403 	wcl->c_ssynchandle = fcl.c_ssynchandle;
1404 
1405 	/*
1406 	 * Only transfer the read data ignoring any trailing
1407 	 * roundup chunks. A bit of work, but it saves an
1408 	 * unnecessary extra RDMA_WRITE containing only
1409 	 * roundup bytes.
1410 	 */
1411 
1412 	rnduplen = clist_len(wcl) - data_len;
1413 
1414 	if (rnduplen) {
1415 
1416 		tcl = wcl->c_next;
1417 
1418 		/*
1419 		 * Check if there is a trailing roundup chunk
1420 		 */
1421 		while (tcl) {
1422 			if ((tcl->c_next == NULL) && (tcl->c_len == rnduplen)) {
1423 				rndup_present = 1;
1424 				break;
1425 			}
1426 			tcl = tcl->c_next;
1427 		}
1428 
1429 		/*
1430 		 * Make a copy chunk list skipping the last chunk
1431 		 */
1432 		if (rndup_present) {
1433 			cl = wcl;
1434 			tcl = NULL;
1435 			while (cl) {
1436 				if (tcl == NULL) {
1437 					tcl = clist_alloc();
1438 					wrcl = tcl;
1439 				} else {
1440 					tcl->c_next = clist_alloc();
1441 					tcl = tcl->c_next;
1442 				}
1443 
1444 				*tcl = *cl;
1445 				cl = cl->c_next;
1446 				/* last chunk */
1447 				if (cl->c_next == NULL)
1448 					break;
1449 			}
1450 			tcl->c_next = NULL;
1451 		}
1452 	}
1453 
1454 	if (wrcl == NULL) {
1455 		/* No roundup chunks */
1456 		wrcl = wcl;
1457 	}
1458 
1459 	/*
1460 	 * Set the registered memory handles for the
1461 	 * rest of the chunks same as the first chunk.
1462 	 */
1463 	tcl = wrcl->c_next;
1464 	while (tcl) {
1465 		tcl->c_smemhandle = fcl.c_smemhandle;
1466 		tcl->c_ssynchandle = fcl.c_ssynchandle;
1467 		tcl = tcl->c_next;
1468 	}
1469 
1470 	/*
1471 	 * Sync the total len beginning from the first chunk.
1472 	 */
1473 	fcl.c_len = clist_len(wrcl);
1474 	status = clist_syncmem(xdrp->xp_conn, &fcl, CLIST_REG_SOURCE);
1475 	if (status != RDMA_SUCCESS) {
1476 		return (FALSE);
1477 	}
1478 
1479 	status = RDMA_WRITE(xdrp->xp_conn, wrcl, WAIT);
1480 
1481 	if (rndup_present)
1482 		clist_free(wrcl);
1483 
1484 	if (status != RDMA_SUCCESS) {
1485 		return (FALSE);
1486 	}
1487 
1488 	return (TRUE);
1489 }
1490 
1491 
1492 /*
1493  * Reads one chunk at a time
1494  */
1495 
1496 static bool_t
1497 xdrrdma_read_a_chunk(XDR *xdrs, CONN **conn)
1498 {
1499 	int status;
1500 	int32_t len = 0;
1501 	xrdma_private_t	*xdrp = (xrdma_private_t *)(xdrs->x_private);
1502 	struct clist *cle = *(xdrp->xp_rcl_next);
1503 	struct clist *rclp = xdrp->xp_rcl;
1504 	struct clist *clp;
1505 
1506 	/*
1507 	 * len is used later to decide xdr offset in
1508 	 * the chunk factoring any 4-byte XDR alignment
1509 	 * (See read chunk example top of this file)
1510 	 */
1511 	while (rclp != cle) {
1512 		len += rclp->c_len;
1513 		rclp = rclp->c_next;
1514 	}
1515 
1516 	len = RNDUP(len) - len;
1517 
1518 	ASSERT(xdrs->x_handy <= 0);
1519 
1520 	/*
1521 	 * If this is the first chunk to contain the RPC
1522 	 * message set xp_off to the xdr offset of the
1523 	 * inline message.
1524 	 */
1525 	if (xdrp->xp_off == 0)
1526 		xdrp->xp_off = (xdrp->xp_offp - xdrs->x_base);
1527 
1528 	if (cle == NULL || (cle->c_xdroff != xdrp->xp_off))
1529 		return (FALSE);
1530 
1531 	/*
1532 	 * Make a copy of the chunk to read from client.
1533 	 * Chunks are read on demand, so read only one
1534 	 * for now.
1535 	 */
1536 
1537 	rclp = clist_alloc();
1538 	*rclp = *cle;
1539 	rclp->c_next = NULL;
1540 
1541 	xdrp->xp_rcl_next = &cle->c_next;
1542 
1543 	/*
1544 	 * If there is a roundup present, then skip those
1545 	 * bytes when reading.
1546 	 */
1547 	if (len) {
1548 		rclp->w.c_saddr =
1549 		    (uint64)(uintptr_t)rclp->w.c_saddr + len;
1550 			rclp->c_len = rclp->c_len - len;
1551 	}
1552 
1553 	status = xdrrdma_read_from_client(rclp, conn, rclp->c_len);
1554 
1555 	if (status == FALSE) {
1556 		clist_free(rclp);
1557 		return (status);
1558 	}
1559 
1560 	xdrp->xp_offp = rclp->rb_longbuf.addr;
1561 	xdrs->x_base = xdrp->xp_offp;
1562 	xdrs->x_handy = rclp->c_len;
1563 
1564 	/*
1565 	 * This copy of read chunks containing the XDR
1566 	 * message is freed later in xdrrdma_destroy()
1567 	 */
1568 
1569 	if (xdrp->xp_rcl_xdr) {
1570 		/* Add the chunk to end of the list */
1571 		clp = xdrp->xp_rcl_xdr;
1572 		while (clp->c_next != NULL)
1573 			clp = clp->c_next;
1574 		clp->c_next = rclp;
1575 	} else {
1576 		xdrp->xp_rcl_xdr = rclp;
1577 	}
1578 	return (TRUE);
1579 }
1580 
1581 static void
1582 xdrrdma_free_xdr_chunks(CONN *conn, struct clist *xdr_rcl)
1583 {
1584 	struct clist *cl;
1585 
1586 	(void) clist_deregister(conn, xdr_rcl);
1587 
1588 	/*
1589 	 * Read chunks containing parts XDR message are
1590 	 * special: in case of multiple chunks each has
1591 	 * its own buffer.
1592 	 */
1593 
1594 	cl = xdr_rcl;
1595 	while (cl) {
1596 		rdma_buf_free(conn, &cl->rb_longbuf);
1597 		cl = cl->c_next;
1598 	}
1599 
1600 	clist_free(xdr_rcl);
1601 }
1602