xref: /illumos-gate/usr/src/uts/common/io/ib/clients/rdsv3/rdma.c (revision c0dd49bdd68c0d758a67d56f07826f3b45cfc664)
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 /*
26  * Copyright (c) 2007 Oracle.  All rights reserved.
27  *
28  * This software is available to you under a choice of one of two
29  * licenses.  You may choose to be licensed under the terms of the GNU
30  * General Public License (GPL) Version 2, available from the file
31  * COPYING in the main directory of this source tree, or the
32  * OpenIB.org BSD license below:
33  *
34  *     Redistribution and use in source and binary forms, with or
35  *     without modification, are permitted provided that the following
36  *     conditions are met:
37  *
38  *      - Redistributions of source code must retain the above
39  *        copyright notice, this list of conditions and the following
40  *        disclaimer.
41  *
42  *      - Redistributions in binary form must reproduce the above
43  *        copyright notice, this list of conditions and the following
44  *        disclaimer in the documentation and/or other materials
45  *        provided with the distribution.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
51  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
52  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
53  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
54  * SOFTWARE.
55  *
56  */
57 #include <sys/ib/clients/of/rdma/ib_verbs.h>
58 #include <sys/ib/clients/of/rdma/ib_addr.h>
59 #include <sys/ib/clients/of/rdma/rdma_cm.h>
60 
61 #include <sys/ib/clients/rdsv3/ib.h>
62 #include <sys/ib/clients/rdsv3/rdma.h>
63 #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
64 
65 #define	DMA_TO_DEVICE 0
66 #define	DMA_FROM_DEVICE 1
67 #define	RB_CLEAR_NODE(nodep) AVL_SETPARENT(nodep, nodep);
68 
69 /*
70  * XXX
71  *  - build with sparse
72  *  - should we limit the size of a mr region?  let transport return failure?
73  *  - should we detect duplicate keys on a socket?  hmm.
74  *  - an rdma is an mlock, apply rlimit?
75  */
76 
77 /*
78  * get the number of pages by looking at the page indices that the start and
79  * end addresses fall in.
80  *
81  * Returns 0 if the vec is invalid.  It is invalid if the number of bytes
82  * causes the address to wrap or overflows an unsigned int.  This comes
83  * from being stored in the 'length' member of 'struct rdsv3_scatterlist'.
84  */
85 static unsigned int
86 rdsv3_pages_in_vec(struct rdsv3_iovec *vec)
87 {
88 	if ((vec->addr + vec->bytes <= vec->addr) ||
89 	    (vec->bytes > (uint64_t)UINT_MAX)) {
90 		return (0);
91 	}
92 
93 	return (((vec->addr + vec->bytes + PAGESIZE - 1) >>
94 	    PAGESHIFT) - (vec->addr >> PAGESHIFT));
95 }
96 
97 static struct rdsv3_mr *
98 rdsv3_mr_tree_walk(struct avl_tree *root, uint32_t key,
99 	struct rdsv3_mr *insert)
100 {
101 	struct rdsv3_mr *mr;
102 	avl_index_t where;
103 
104 	mr = avl_find(root, &key, &where);
105 	if ((mr == NULL) && (insert != NULL)) {
106 		avl_insert(root, (void *)insert, where);
107 		atomic_add_32(&insert->r_refcount, 1);
108 		return (NULL);
109 	}
110 
111 	return (mr);
112 }
113 
114 /*
115  * Destroy the transport-specific part of a MR.
116  */
117 static void
118 rdsv3_destroy_mr(struct rdsv3_mr *mr)
119 {
120 	struct rdsv3_sock *rs = mr->r_sock;
121 	void *trans_private = NULL;
122 	avl_node_t *np;
123 
124 	RDSV3_DPRINTF5("rdsv3_destroy_mr",
125 	    "RDS: destroy mr key is %x refcnt %u",
126 	    mr->r_key, atomic_get(&mr->r_refcount));
127 
128 	if (test_and_set_bit(RDSV3_MR_DEAD, &mr->r_state))
129 		return;
130 
131 	mutex_enter(&rs->rs_rdma_lock);
132 	np = &mr->r_rb_node;
133 	if (AVL_XPARENT(np) != np)
134 		avl_remove(&rs->rs_rdma_keys, mr);
135 	trans_private = mr->r_trans_private;
136 	mr->r_trans_private = NULL;
137 	mutex_exit(&rs->rs_rdma_lock);
138 
139 	if (trans_private)
140 		mr->r_trans->free_mr(trans_private, mr->r_invalidate);
141 }
142 
143 void
144 __rdsv3_put_mr_final(struct rdsv3_mr *mr)
145 {
146 	rdsv3_destroy_mr(mr);
147 	kmem_free(mr, sizeof (*mr));
148 }
149 
150 /*
151  * By the time this is called we can't have any more ioctls called on
152  * the socket so we don't need to worry about racing with others.
153  */
154 void
155 rdsv3_rdma_drop_keys(struct rdsv3_sock *rs)
156 {
157 	struct rdsv3_mr *mr;
158 	struct avl_node *node;
159 
160 	/* Release any MRs associated with this socket */
161 	mutex_enter(&rs->rs_rdma_lock);
162 	while ((node = avl_first(&rs->rs_rdma_keys))) {
163 		mr = container_of(node, struct rdsv3_mr, r_rb_node);
164 		if (mr->r_trans == rs->rs_transport)
165 			mr->r_invalidate = 0;
166 		avl_remove(&rs->rs_rdma_keys, &mr->r_rb_node);
167 		RB_CLEAR_NODE(&mr->r_rb_node)
168 		mutex_exit(&rs->rs_rdma_lock);
169 		rdsv3_destroy_mr(mr);
170 		rdsv3_mr_put(mr);
171 		mutex_enter(&rs->rs_rdma_lock);
172 	}
173 	mutex_exit(&rs->rs_rdma_lock);
174 
175 	if (rs->rs_transport && rs->rs_transport->flush_mrs)
176 		rs->rs_transport->flush_mrs();
177 }
178 
179 /*
180  * Helper function to pin user pages.
181  */
182 #if 0
183 static int
184 rds_pin_pages(unsigned long user_addr, unsigned int nr_pages,
185 	struct page **pages, int write)
186 {
187 	unsigned long l_user_addr = user_addr;
188 	unsigned int l_nr_pages = nr_pages;
189 	struct page **l_pages = pages;
190 	int l_write = write;
191 
192 	/* memory pin in rds_ib_get_mr() */
193 	return (0);
194 }
195 #endif
196 
197 static int
198 __rdsv3_rdma_map(struct rdsv3_sock *rs, struct rdsv3_get_mr_args *args,
199 	uint64_t *cookie_ret, struct rdsv3_mr **mr_ret)
200 {
201 	struct rdsv3_mr *mr = NULL, *found;
202 	void *trans_private;
203 	rdsv3_rdma_cookie_t cookie;
204 	unsigned int nents = 0;
205 	int ret;
206 
207 	if (rs->rs_bound_addr == 0) {
208 		ret = -ENOTCONN; /* XXX not a great errno */
209 		goto out;
210 	}
211 
212 	if (rs->rs_transport->get_mr == NULL) {
213 		ret = -EOPNOTSUPP;
214 		goto out;
215 	}
216 
217 	mr = kmem_zalloc(sizeof (struct rdsv3_mr), KM_NOSLEEP);
218 	if (mr == NULL) {
219 		ret = -ENOMEM;
220 		goto out;
221 	}
222 
223 	mr->r_refcount = 1;
224 	RB_CLEAR_NODE(&mr->r_rb_node);
225 	mr->r_trans = rs->rs_transport;
226 	mr->r_sock = rs;
227 
228 	if (args->flags & RDSV3_RDMA_USE_ONCE)
229 		mr->r_use_once = 1;
230 	if (args->flags & RDSV3_RDMA_INVALIDATE)
231 		mr->r_invalidate = 1;
232 	if (args->flags & RDSV3_RDMA_READWRITE)
233 		mr->r_write = 1;
234 
235 	/*
236 	 * Obtain a transport specific MR. If this succeeds, the
237 	 * s/g list is now owned by the MR.
238 	 * Note that dma_map() implies that pending writes are
239 	 * flushed to RAM, so no dma_sync is needed here.
240 	 */
241 	trans_private = rs->rs_transport->get_mr(&args->vec, nents, rs,
242 	    &mr->r_key);
243 
244 	if (IS_ERR(trans_private)) {
245 		ret = PTR_ERR(trans_private);
246 		goto out;
247 	}
248 
249 	mr->r_trans_private = trans_private;
250 
251 	/*
252 	 * The user may pass us an unaligned address, but we can only
253 	 * map page aligned regions. So we keep the offset, and build
254 	 * a 64bit cookie containing <R_Key, offset> and pass that
255 	 * around.
256 	 */
257 	cookie = rdsv3_rdma_make_cookie(mr->r_key, args->vec.addr & ~PAGEMASK);
258 	if (cookie_ret)
259 		*cookie_ret = cookie;
260 
261 	/*
262 	 * copy value of cookie to user address at args->cookie_addr
263 	 */
264 	if (args->cookie_addr) {
265 		ret = ddi_copyout((void *)&cookie,
266 		    (void *)((intptr_t)args->cookie_addr),
267 		    sizeof (rdsv3_rdma_cookie_t), 0);
268 		if (ret != 0) {
269 			ret = -EFAULT;
270 			goto out;
271 		}
272 	}
273 
274 	RDSV3_DPRINTF5("__rdsv3_rdma_map",
275 	    "RDS: get_mr mr 0x%p addr 0x%llx key 0x%x",
276 	    mr, args->vec.addr, mr->r_key);
277 	/*
278 	 * Inserting the new MR into the rbtree bumps its
279 	 * reference count.
280 	 */
281 	mutex_enter(&rs->rs_rdma_lock);
282 	found = rdsv3_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr);
283 	mutex_exit(&rs->rs_rdma_lock);
284 
285 	ASSERT(!(found && found != mr));
286 
287 	if (mr_ret) {
288 		atomic_add_32(&mr->r_refcount, 1);
289 		*mr_ret = mr;
290 	}
291 
292 	ret = 0;
293 out:
294 	if (mr)
295 		rdsv3_mr_put(mr);
296 	return (ret);
297 }
298 
299 int
300 rdsv3_get_mr(struct rdsv3_sock *rs, const void *optval, int optlen)
301 {
302 	struct rdsv3_get_mr_args args;
303 
304 	if (optlen != sizeof (struct rdsv3_get_mr_args))
305 		return (-EINVAL);
306 
307 #if 1
308 	bcopy((struct rdsv3_get_mr_args *)optval, &args,
309 	    sizeof (struct rdsv3_get_mr_args));
310 #else
311 	if (ddi_copyin(optval, &args, optlen, 0))
312 		return (-EFAULT);
313 #endif
314 
315 	return (__rdsv3_rdma_map(rs, &args, NULL, NULL));
316 }
317 
318 /*
319  * Free the MR indicated by the given R_Key
320  */
321 int
322 rdsv3_free_mr(struct rdsv3_sock *rs, const void *optval, int optlen)
323 {
324 	struct rdsv3_free_mr_args args;
325 	struct rdsv3_mr *mr;
326 
327 	if (optlen != sizeof (struct rdsv3_free_mr_args))
328 		return (-EINVAL);
329 
330 #if 1
331 	bcopy((struct rdsv3_free_mr_args *)optval, &args,
332 	    sizeof (struct rdsv3_free_mr_args));
333 #else
334 	if (ddi_copyin((struct rdsv3_free_mr_args *)optval, &args,
335 	    sizeof (struct rdsv3_free_mr_args), 0))
336 		return (-EFAULT);
337 #endif
338 
339 	/* Special case - a null cookie means flush all unused MRs */
340 	if (args.cookie == 0) {
341 		if (!rs->rs_transport || !rs->rs_transport->flush_mrs)
342 			return (-EINVAL);
343 		rs->rs_transport->flush_mrs();
344 		return (0);
345 	}
346 
347 	/*
348 	 * Look up the MR given its R_key and remove it from the rbtree
349 	 * so nobody else finds it.
350 	 * This should also prevent races with rdsv3_rdma_unuse.
351 	 */
352 	mutex_enter(&rs->rs_rdma_lock);
353 	mr = rdsv3_mr_tree_walk(&rs->rs_rdma_keys,
354 	    rdsv3_rdma_cookie_key(args.cookie), NULL);
355 	if (mr) {
356 		avl_remove(&rs->rs_rdma_keys, &mr->r_rb_node);
357 		RB_CLEAR_NODE(&mr->r_rb_node);
358 		if (args.flags & RDSV3_RDMA_INVALIDATE)
359 			mr->r_invalidate = 1;
360 	}
361 	mutex_exit(&rs->rs_rdma_lock);
362 
363 	if (!mr)
364 		return (-EINVAL);
365 
366 	/*
367 	 * call rdsv3_destroy_mr() ourselves so that we're sure it's done
368 	 * by time we return.  If we let rdsv3_mr_put() do it it might not
369 	 * happen until someone else drops their ref.
370 	 */
371 	rdsv3_destroy_mr(mr);
372 	rdsv3_mr_put(mr);
373 	return (0);
374 }
375 
376 /*
377  * This is called when we receive an extension header that
378  * tells us this MR was used. It allows us to implement
379  * use_once semantics
380  */
381 void
382 rdsv3_rdma_unuse(struct rdsv3_sock *rs, uint32_t r_key, int force)
383 {
384 	struct rdsv3_mr *mr;
385 	int zot_me = 0;
386 
387 	RDSV3_DPRINTF4("rdsv3_rdma_unuse", "Enter rkey: 0x%x", r_key);
388 
389 	mutex_enter(&rs->rs_rdma_lock);
390 	mr = rdsv3_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
391 	if (mr && (mr->r_use_once || force)) {
392 		avl_remove(&rs->rs_rdma_keys, &mr->r_rb_node);
393 		RB_CLEAR_NODE(&mr->r_rb_node);
394 		zot_me = 1;
395 	} else if (mr)
396 		atomic_add_32(&mr->r_refcount, 1);
397 	mutex_exit(&rs->rs_rdma_lock);
398 
399 	/*
400 	 * May have to issue a dma_sync on this memory region.
401 	 * Note we could avoid this if the operation was a RDMA READ,
402 	 * but at this point we can't tell.
403 	 */
404 	if (mr != NULL) {
405 		RDSV3_DPRINTF4("rdsv3_rdma_unuse", "mr: %p zot_me %d",
406 		    mr, zot_me);
407 		if (mr->r_trans->sync_mr)
408 			mr->r_trans->sync_mr(mr->r_trans_private,
409 			    DMA_FROM_DEVICE);
410 
411 		/*
412 		 * If the MR was marked as invalidate, this will
413 		 * trigger an async flush.
414 		 */
415 		if (zot_me)
416 			rdsv3_destroy_mr(mr);
417 		rdsv3_mr_put(mr);
418 	}
419 	RDSV3_DPRINTF4("rdsv3_rdma_unuse", "Return");
420 }
421 
422 void
423 rdsv3_rdma_free_op(struct rdsv3_rdma_op *ro)
424 {
425 	unsigned int i;
426 
427 	/* deallocate RDMA resources on rdsv3_message */
428 
429 	for (i = 0; i < ro->r_nents; i++) {
430 		ddi_umem_unlock(ro->r_rdma_sg[i].umem_cookie);
431 	}
432 
433 	if (ro->r_notifier)
434 		kmem_free(ro->r_notifier, sizeof (*ro->r_notifier));
435 	kmem_free(ro, sizeof (*ro));
436 }
437 
438 extern struct umem_callback_ops rdsv3_umem_cbops;
439 /*
440  * args is a pointer to an in-kernel copy in the sendmsg cmsg.
441  */
442 static struct rdsv3_rdma_op *
443 rdsv3_rdma_prepare(struct rdsv3_sock *rs, struct rdsv3_rdma_args *args)
444 {
445 	struct rdsv3_iovec vec;
446 	struct rdsv3_rdma_op *op = NULL;
447 	unsigned int nr_bytes;
448 	struct rdsv3_iovec *local_vec;
449 	unsigned int nr;
450 	unsigned int i;
451 	ddi_umem_cookie_t umem_cookie;
452 	size_t umem_len;
453 	caddr_t umem_addr;
454 	int umem_flags;
455 	int ret;
456 
457 	if (rs->rs_bound_addr == 0) {
458 		ret = -ENOTCONN; /* XXX not a great errno */
459 		goto out;
460 	}
461 
462 	if (args->nr_local > (uint64_t)UINT_MAX) {
463 		ret = -EMSGSIZE;
464 		goto out;
465 	}
466 
467 	op = kmem_zalloc(offsetof(struct rdsv3_rdma_op,
468 	    r_rdma_sg[args->nr_local]), KM_NOSLEEP);
469 	if (op == NULL) {
470 		ret = -ENOMEM;
471 		goto out;
472 	}
473 
474 	op->r_write = !!(args->flags & RDSV3_RDMA_READWRITE);
475 	op->r_fence = !!(args->flags & RDSV3_RDMA_FENCE);
476 	op->r_notify = !!(args->flags & RDSV3_RDMA_NOTIFY_ME);
477 	op->r_recverr = rs->rs_recverr;
478 
479 	if (op->r_notify || op->r_recverr) {
480 		/*
481 		 * We allocate an uninitialized notifier here, because
482 		 * we don't want to do that in the completion handler. We
483 		 * would have to use GFP_ATOMIC there, and don't want to deal
484 		 * with failed allocations.
485 		 */
486 		op->r_notifier = kmem_alloc(sizeof (struct rdsv3_notifier),
487 		    KM_NOSLEEP);
488 		if (!op->r_notifier) {
489 			ret = -ENOMEM;
490 			goto out;
491 		}
492 		op->r_notifier->n_user_token = args->user_token;
493 		op->r_notifier->n_status = RDSV3_RDMA_SUCCESS;
494 	}
495 
496 	/*
497 	 * The cookie contains the R_Key of the remote memory region, and
498 	 * optionally an offset into it. This is how we implement RDMA into
499 	 * unaligned memory.
500 	 * When setting up the RDMA, we need to add that offset to the
501 	 * destination address (which is really an offset into the MR)
502 	 * FIXME: We may want to move this into ib_rdma.c
503 	 */
504 	op->r_key = rdsv3_rdma_cookie_key(args->cookie);
505 	op->r_remote_addr = args->remote_vec.addr +
506 	    rdsv3_rdma_cookie_offset(args->cookie);
507 
508 	nr_bytes = 0;
509 
510 	RDSV3_DPRINTF5("rdsv3_rdma_prepare",
511 	    "RDS: rdma prepare nr_local %llu rva %llx rkey %x",
512 	    (unsigned long long)args->nr_local,
513 	    (unsigned long long)args->remote_vec.addr,
514 	    op->r_key);
515 
516 	local_vec = (struct rdsv3_iovec *)(unsigned long) args->local_vec_addr;
517 
518 	/* pin the scatter list of user buffers */
519 	for (i = 0; i < args->nr_local; i++) {
520 		if (ddi_copyin(&local_vec[i], &vec,
521 		    sizeof (struct rdsv3_iovec), 0)) {
522 			ret = -EFAULT;
523 			goto out;
524 		}
525 
526 		nr = rdsv3_pages_in_vec(&vec);
527 		if (nr == 0) {
528 			RDSV3_DPRINTF2("rdsv3_rdma_prepare",
529 			    "rdsv3_pages_in_vec returned 0");
530 			ret = -EINVAL;
531 			goto out;
532 		}
533 
534 		rs->rs_user_addr = vec.addr;
535 		rs->rs_user_bytes = vec.bytes;
536 
537 		/* pin user memory pages */
538 		umem_len = ptob(btopr(vec.bytes +
539 		    ((uintptr_t)vec.addr & PAGEOFFSET)));
540 		umem_addr = (caddr_t)((uintptr_t)vec.addr & ~PAGEOFFSET);
541 		umem_flags = (DDI_UMEMLOCK_WRITE | DDI_UMEMLOCK_READ |
542 		    DDI_UMEMLOCK_LONGTERM);
543 		ret = umem_lockmemory(umem_addr, umem_len, umem_flags,
544 		    &umem_cookie, &rdsv3_umem_cbops, NULL);
545 		if (ret != 0) {
546 			RDSV3_DPRINTF2("rdsv3_rdma_prepare",
547 			    "umem_lockmemory() returned %d", ret);
548 			ret = -EFAULT;
549 			goto out;
550 		}
551 		op->r_rdma_sg[i].umem_cookie = umem_cookie;
552 		op->r_rdma_sg[i].iovec = vec;
553 		nr_bytes += vec.bytes;
554 
555 		RDSV3_DPRINTF5("rdsv3_rdma_prepare",
556 		    "RDS: nr_bytes %u nr %u vec.bytes %llu vec.addr %llx",
557 		    nr_bytes, nr, vec.bytes, vec.addr);
558 	}
559 	op->r_nents = i;
560 
561 	if (nr_bytes > args->remote_vec.bytes) {
562 		RDSV3_DPRINTF2("rdsv3_rdma_prepare",
563 		    "RDS nr_bytes %u remote_bytes %u do not match",
564 		    nr_bytes, (unsigned int) args->remote_vec.bytes);
565 		ret = -EINVAL;
566 		goto out;
567 	}
568 	op->r_bytes = nr_bytes;
569 
570 	ret = 0;
571 out:
572 	if (ret) {
573 		if (op)
574 			rdsv3_rdma_free_op(op);
575 		op = ERR_PTR(ret);
576 	}
577 	return (op);
578 }
579 
580 /*
581  * The application asks for a RDMA transfer.
582  * Extract all arguments and set up the rdma_op
583  */
584 int
585 rdsv3_cmsg_rdma_args(struct rdsv3_sock *rs, struct rdsv3_message *rm,
586 	struct cmsghdr *cmsg)
587 {
588 	struct rdsv3_rdma_op *op;
589 	struct rdsv3_rdma_args *ap;
590 
591 	if (cmsg->cmsg_len < CMSG_LEN(sizeof (struct rdsv3_rdma_args)) ||
592 	    rm->m_rdma_op != NULL)
593 		return (-EINVAL);
594 
595 	/* uint64_t alignment on struct rdsv3_get_mr_args */
596 	ap = (struct rdsv3_rdma_args *)kmem_alloc(cmsg->cmsg_len, KM_SLEEP);
597 	bcopy(CMSG_DATA(cmsg), ap, cmsg->cmsg_len);
598 	op = rdsv3_rdma_prepare(rs, ap);
599 	kmem_free(ap, cmsg->cmsg_len);
600 	if (IS_ERR(op))
601 		return (PTR_ERR(op));
602 	rdsv3_stats_inc(s_send_rdma);
603 	rm->m_rdma_op = op;
604 	return (0);
605 }
606 
607 /*
608  * The application wants us to pass an RDMA destination (aka MR)
609  * to the remote
610  */
611 int
612 rdsv3_cmsg_rdma_dest(struct rdsv3_sock *rs, struct rdsv3_message *rm,
613 	struct cmsghdr *cmsg)
614 {
615 	struct rdsv3_mr *mr;
616 	uint32_t r_key;
617 	int err = 0;
618 
619 	if (cmsg->cmsg_len < CMSG_LEN(sizeof (rdsv3_rdma_cookie_t)) ||
620 	    rm->m_rdma_cookie != 0)
621 		return (-EINVAL);
622 
623 	(void) memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg),
624 	    sizeof (rm->m_rdma_cookie));
625 
626 	/*
627 	 * We are reusing a previously mapped MR here. Most likely, the
628 	 * application has written to the buffer, so we need to explicitly
629 	 * flush those writes to RAM. Otherwise the HCA may not see them
630 	 * when doing a DMA from that buffer.
631 	 */
632 	r_key = rdsv3_rdma_cookie_key(rm->m_rdma_cookie);
633 
634 	mutex_enter(&rs->rs_rdma_lock);
635 	mr = rdsv3_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
636 	if (mr == NULL)
637 		err = -EINVAL;	/* invalid r_key */
638 	else
639 		atomic_add_32(&mr->r_refcount, 1);
640 	mutex_exit(&rs->rs_rdma_lock);
641 
642 	if (mr) {
643 		mr->r_trans->sync_mr(mr->r_trans_private, DMA_TO_DEVICE);
644 		rm->m_rdma_mr = mr;
645 	}
646 	return (err);
647 }
648 
649 /*
650  * The application passes us an address range it wants to enable RDMA
651  * to/from. We map the area, and save the <R_Key,offset> pair
652  * in rm->m_rdma_cookie. This causes it to be sent along to the peer
653  * in an extension header.
654  */
655 int
656 rdsv3_cmsg_rdma_map(struct rdsv3_sock *rs, struct rdsv3_message *rm,
657 	struct cmsghdr *cmsg)
658 {
659 	struct rdsv3_get_mr_args *mrp;
660 	int status;
661 
662 	if (cmsg->cmsg_len < CMSG_LEN(sizeof (struct rdsv3_get_mr_args)) ||
663 	    rm->m_rdma_cookie != 0)
664 		return (-EINVAL);
665 
666 	/* uint64_t alignment on struct rdsv3_get_mr_args */
667 	mrp = (struct rdsv3_get_mr_args *)kmem_alloc(cmsg->cmsg_len, KM_SLEEP);
668 	bcopy(CMSG_DATA(cmsg), mrp, cmsg->cmsg_len);
669 	status = __rdsv3_rdma_map(rs, mrp, &rm->m_rdma_cookie, &rm->m_rdma_mr);
670 	kmem_free(mrp, cmsg->cmsg_len);
671 	return (status);
672 }
673