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 /*
23  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 
32 /*
33  *
34  * MODULE: dapl_lmr_util.c
35  *
36  * PURPOSE: Memory management support routines
37  * Description: Support routines for LMR functions
38  *
39  */
40 
41 #include <sys/ib/ibtl/ibtl_types.h>
42 #include "dapl_lmr_util.h"
43 #include "dapl_ia_util.h"
44 
45 DAPL_LMR *
dapl_lmr_alloc(IN DAPL_IA * ia,IN DAT_MEM_TYPE mem_type,IN DAT_REGION_DESCRIPTION region_desc,IN DAT_VLEN length,IN DAT_PZ_HANDLE pz_handle,IN DAT_MEM_PRIV_FLAGS mem_priv)46 dapl_lmr_alloc(IN DAPL_IA *ia,
47 	IN DAT_MEM_TYPE mem_type,
48 	IN DAT_REGION_DESCRIPTION region_desc,
49 	IN DAT_VLEN length,
50 	IN DAT_PZ_HANDLE pz_handle,
51 	IN DAT_MEM_PRIV_FLAGS mem_priv)
52 {
53 	DAPL_LMR *lmr;
54 
55 	/* Allocate LMR */
56 	lmr = (DAPL_LMR *)dapl_os_alloc(sizeof (DAPL_LMR));
57 	if (NULL == lmr) {
58 		return (NULL);
59 	}
60 
61 	/* zero the structure */
62 	(void) dapl_os_memzero(lmr, sizeof (DAPL_LMR));
63 
64 	/*
65 	 * initialize the header
66 	 */
67 	lmr->header.provider = ia->header.provider;
68 	lmr->header.magic = DAPL_MAGIC_LMR;
69 	lmr->header.handle_type = DAT_HANDLE_TYPE_LMR;
70 	lmr->header.owner_ia = ia;
71 	lmr->header.user_context.as_64 = 0;
72 	lmr->header.user_context.as_ptr = NULL;
73 	dapl_llist_init_entry(&lmr->header.ia_list_entry);
74 	dapl_ia_link_lmr(ia, lmr);
75 	dapl_os_lock_init(&lmr->header.lock);
76 
77 	/*
78 	 * initialize the body
79 	 */
80 	lmr->param.ia_handle = (DAT_IA_HANDLE)ia;
81 	lmr->param.mem_type = mem_type;
82 	lmr->param.region_desc = region_desc;
83 	lmr->param.length = length;
84 	lmr->param.pz_handle = pz_handle;
85 	lmr->param.mem_priv = mem_priv;
86 	lmr->lmr_ref_count = 0;
87 
88 	return (lmr);
89 }
90 
91 void
dapl_lmr_dealloc(IN DAPL_LMR * lmr)92 dapl_lmr_dealloc(IN DAPL_LMR *lmr)
93 {
94 	/* reset magic to prevent reuse */
95 	lmr->header.magic = DAPL_MAGIC_INVALID;
96 	dapl_ia_unlink_lmr(lmr->header.owner_ia, lmr);
97 	dapl_os_lock_destroy(&lmr->header.lock);
98 
99 	dapl_os_free((void *) lmr, sizeof (DAPL_LMR));
100 }
101 
102 int32_t
dapl_lmr_convert_privileges(IN DAT_MEM_PRIV_FLAGS privileges)103 dapl_lmr_convert_privileges(IN DAT_MEM_PRIV_FLAGS privileges)
104 {
105 	int32_t value = 0;
106 
107 	/*
108 	 *    if (DAT_MEM_PRIV_LOCAL_READ_FLAG & privileges)
109 	 *	do nothing
110 	 */
111 	if (DAT_MEM_PRIV_LOCAL_WRITE_FLAG & privileges) {
112 		value |= IB_ACCESS_LOCAL_WRITE;
113 	}
114 	if (DAT_MEM_PRIV_REMOTE_READ_FLAG & privileges) {
115 		value |= IB_ACCESS_REMOTE_READ;
116 	}
117 	if (DAT_MEM_PRIV_REMOTE_WRITE_FLAG & privileges) {
118 		value |= IB_ACCESS_REMOTE_WRITE;
119 	}
120 	if (DAT_MEM_PRIV_RO_DISABLE_FLAG & privileges) {
121 		value |= IBT_MR_DISABLE_RO;
122 	}
123 	return (value);
124 }
125