1 /*
2  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/export.h>
34 #include <rdma/ib_marshall.h>
35 
36 void ib_copy_ah_attr_to_user(struct ib_uverbs_ah_attr *dst,
37 			     struct rdma_ah_attr *src)
38 {
39 	memset(&dst->grh.reserved, 0, sizeof(dst->grh.reserved));
40 	dst->dlid		   = rdma_ah_get_dlid(src);
41 	dst->sl			   = rdma_ah_get_sl(src);
42 	dst->src_path_bits	   = rdma_ah_get_path_bits(src);
43 	dst->static_rate	   = rdma_ah_get_static_rate(src);
44 	dst->is_global             = rdma_ah_get_ah_flags(src) &
45 					IB_AH_GRH ? 1 : 0;
46 	if (dst->is_global) {
47 		const struct ib_global_route *grh = rdma_ah_read_grh(src);
48 
49 		memcpy(dst->grh.dgid, grh->dgid.raw, sizeof(grh->dgid));
50 		dst->grh.flow_label        = grh->flow_label;
51 		dst->grh.sgid_index        = grh->sgid_index;
52 		dst->grh.hop_limit         = grh->hop_limit;
53 		dst->grh.traffic_class     = grh->traffic_class;
54 	}
55 	dst->port_num		   = rdma_ah_get_port_num(src);
56 	dst->reserved 		   = 0;
57 }
58 EXPORT_SYMBOL(ib_copy_ah_attr_to_user);
59 
60 void ib_copy_qp_attr_to_user(struct ib_uverbs_qp_attr *dst,
61 			     struct ib_qp_attr *src)
62 {
63 	dst->qp_state	        = src->qp_state;
64 	dst->cur_qp_state	= src->cur_qp_state;
65 	dst->path_mtu		= src->path_mtu;
66 	dst->path_mig_state	= src->path_mig_state;
67 	dst->qkey		= src->qkey;
68 	dst->rq_psn		= src->rq_psn;
69 	dst->sq_psn		= src->sq_psn;
70 	dst->dest_qp_num	= src->dest_qp_num;
71 	dst->qp_access_flags	= src->qp_access_flags;
72 
73 	dst->max_send_wr	= src->cap.max_send_wr;
74 	dst->max_recv_wr	= src->cap.max_recv_wr;
75 	dst->max_send_sge	= src->cap.max_send_sge;
76 	dst->max_recv_sge	= src->cap.max_recv_sge;
77 	dst->max_inline_data	= src->cap.max_inline_data;
78 
79 	ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
80 	ib_copy_ah_attr_to_user(&dst->alt_ah_attr, &src->alt_ah_attr);
81 
82 	dst->pkey_index		= src->pkey_index;
83 	dst->alt_pkey_index	= src->alt_pkey_index;
84 	dst->en_sqd_async_notify = src->en_sqd_async_notify;
85 	dst->sq_draining	= src->sq_draining;
86 	dst->max_rd_atomic	= src->max_rd_atomic;
87 	dst->max_dest_rd_atomic	= src->max_dest_rd_atomic;
88 	dst->min_rnr_timer	= src->min_rnr_timer;
89 	dst->port_num		= src->port_num;
90 	dst->timeout		= src->timeout;
91 	dst->retry_cnt		= src->retry_cnt;
92 	dst->rnr_retry		= src->rnr_retry;
93 	dst->alt_port_num	= src->alt_port_num;
94 	dst->alt_timeout	= src->alt_timeout;
95 	memset(dst->reserved, 0, sizeof(dst->reserved));
96 }
97 EXPORT_SYMBOL(ib_copy_qp_attr_to_user);
98 
99 static void __ib_copy_path_rec_to_user(struct ib_user_path_rec *dst,
100 				       struct sa_path_rec *src)
101 {
102 	memcpy(dst->dgid, src->dgid.raw, sizeof(src->dgid));
103 	memcpy(dst->sgid, src->sgid.raw, sizeof(src->sgid));
104 
105 	dst->dlid		= htons(ntohl(sa_path_get_dlid(src)));
106 	dst->slid		= htons(ntohl(sa_path_get_slid(src)));
107 	dst->raw_traffic	= sa_path_get_raw_traffic(src);
108 	dst->flow_label		= src->flow_label;
109 	dst->hop_limit		= src->hop_limit;
110 	dst->traffic_class	= src->traffic_class;
111 	dst->reversible		= src->reversible;
112 	dst->numb_path		= src->numb_path;
113 	dst->pkey		= src->pkey;
114 	dst->sl			= src->sl;
115 	dst->mtu_selector	= src->mtu_selector;
116 	dst->mtu		= src->mtu;
117 	dst->rate_selector	= src->rate_selector;
118 	dst->rate		= src->rate;
119 	dst->packet_life_time	= src->packet_life_time;
120 	dst->preference		= src->preference;
121 	dst->packet_life_time_selector = src->packet_life_time_selector;
122 }
123 
124 void ib_copy_path_rec_to_user(struct ib_user_path_rec *dst,
125 			      struct sa_path_rec *src)
126 {
127 	struct sa_path_rec rec;
128 
129 	if (src->rec_type == SA_PATH_REC_TYPE_OPA) {
130 		sa_convert_path_opa_to_ib(&rec, src);
131 		__ib_copy_path_rec_to_user(dst, &rec);
132 		return;
133 	}
134 	__ib_copy_path_rec_to_user(dst, src);
135 }
136 EXPORT_SYMBOL(ib_copy_path_rec_to_user);
137 
138 void ib_copy_path_rec_from_user(struct sa_path_rec *dst,
139 				struct ib_user_path_rec *src)
140 {
141 	__be32 slid, dlid;
142 
143 	memset(dst, 0, sizeof(*dst));
144 	if ((ib_is_opa_gid((union ib_gid *)src->sgid)) ||
145 	    (ib_is_opa_gid((union ib_gid *)src->dgid))) {
146 		dst->rec_type = SA_PATH_REC_TYPE_OPA;
147 		slid = htonl(opa_get_lid_from_gid((union ib_gid *)src->sgid));
148 		dlid = htonl(opa_get_lid_from_gid((union ib_gid *)src->dgid));
149 	} else {
150 		dst->rec_type = SA_PATH_REC_TYPE_IB;
151 		slid = htonl(ntohs(src->slid));
152 		dlid = htonl(ntohs(src->dlid));
153 	}
154 	memcpy(dst->dgid.raw, src->dgid, sizeof dst->dgid);
155 	memcpy(dst->sgid.raw, src->sgid, sizeof dst->sgid);
156 
157 	sa_path_set_dlid(dst, dlid);
158 	sa_path_set_slid(dst, slid);
159 	sa_path_set_raw_traffic(dst, src->raw_traffic);
160 	dst->flow_label		= src->flow_label;
161 	dst->hop_limit		= src->hop_limit;
162 	dst->traffic_class	= src->traffic_class;
163 	dst->reversible		= src->reversible;
164 	dst->numb_path		= src->numb_path;
165 	dst->pkey		= src->pkey;
166 	dst->sl			= src->sl;
167 	dst->mtu_selector	= src->mtu_selector;
168 	dst->mtu		= src->mtu;
169 	dst->rate_selector	= src->rate_selector;
170 	dst->rate		= src->rate;
171 	dst->packet_life_time	= src->packet_life_time;
172 	dst->preference		= src->preference;
173 	dst->packet_life_time_selector = src->packet_life_time_selector;
174 
175 	/* TODO: No need to set this */
176 	sa_path_set_dmac_zero(dst);
177 	sa_path_set_ndev(dst, NULL);
178 	sa_path_set_ifindex(dst, 0);
179 }
180 EXPORT_SYMBOL(ib_copy_path_rec_from_user);
181