xref: /freebsd/sys/ofed/drivers/infiniband/core/ib_uverbs_std_types_device.c (revision b633e08c705fe43180567eae26923d6f6f98c8d9)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
4  */
5 
6 #include <rdma/uverbs_std_types.h>
7 #include "rdma_core.h"
8 #include "uverbs.h"
9 #include <rdma/uverbs_ioctl.h>
10 
11 /*
12  * This ioctl method allows calling any defined write or write_ex
13  * handler. This essentially replaces the hdr/ex_hdr system with the ioctl
14  * marshalling, and brings the non-ex path into the same marshalling as the ex
15  * path.
16  */
UVERBS_HANDLER(UVERBS_METHOD_INVOKE_WRITE)17 static int UVERBS_HANDLER(UVERBS_METHOD_INVOKE_WRITE)(
18 	struct uverbs_attr_bundle *attrs)
19 {
20 	struct uverbs_api *uapi = attrs->ufile->device->uapi;
21 	const struct uverbs_api_write_method *method_elm;
22 	u32 cmd;
23 	int rc;
24 
25 	rc = uverbs_get_const(&cmd, attrs, UVERBS_ATTR_WRITE_CMD);
26 	if (rc)
27 		return rc;
28 
29 	method_elm = uapi_get_method(uapi, cmd);
30 	if (IS_ERR(method_elm))
31 		return PTR_ERR(method_elm);
32 
33 	uverbs_fill_udata(attrs, &attrs->ucore, UVERBS_ATTR_CORE_IN,
34 			  UVERBS_ATTR_CORE_OUT);
35 
36 	if (attrs->ucore.inlen < method_elm->req_size ||
37 	    attrs->ucore.outlen < method_elm->resp_size)
38 		return -ENOSPC;
39 
40 	return method_elm->handler(attrs);
41 }
42 
43 DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_INVOKE_WRITE,
44 			    UVERBS_ATTR_CONST_IN(UVERBS_ATTR_WRITE_CMD,
45 						 enum ib_uverbs_write_cmds,
46 						 UA_MANDATORY),
47 			    UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CORE_IN,
48 					       UVERBS_ATTR_MIN_SIZE(sizeof(u32)),
49 					       UA_OPTIONAL),
50 			    UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_CORE_OUT,
51 						UVERBS_ATTR_MIN_SIZE(0),
52 						UA_OPTIONAL),
53 			    UVERBS_ATTR_UHW());
54 
55 static uint32_t *
gather_objects_handle(struct ib_uverbs_file * ufile,const struct uverbs_api_object * uapi_object,struct uverbs_attr_bundle * attrs,ssize_t out_len,u64 * total)56 gather_objects_handle(struct ib_uverbs_file *ufile,
57 		      const struct uverbs_api_object *uapi_object,
58 		      struct uverbs_attr_bundle *attrs,
59 		      ssize_t out_len,
60 		      u64 *total)
61 {
62 	u64 max_count = out_len / sizeof(u32);
63 	struct ib_uobject *obj;
64 	u64 count = 0;
65 	u32 *handles;
66 
67 	/* Allocated memory that cannot page out where we gather
68 	 * all object ids under a spin_lock.
69 	 */
70 	handles = uverbs_zalloc(attrs, out_len);
71 	if (IS_ERR(handles))
72 		return handles;
73 
74 	spin_lock_irq(&ufile->uobjects_lock);
75 	list_for_each_entry(obj, &ufile->uobjects, list) {
76 		u32 obj_id = obj->id;
77 
78 		if (obj->uapi_object != uapi_object)
79 			continue;
80 
81 		if (count >= max_count)
82 			break;
83 
84 		handles[count] = obj_id;
85 		count++;
86 	}
87 	spin_unlock_irq(&ufile->uobjects_lock);
88 
89 	*total = count;
90 	return handles;
91 }
92 
UVERBS_HANDLER(UVERBS_METHOD_INFO_HANDLES)93 static int UVERBS_HANDLER(UVERBS_METHOD_INFO_HANDLES)(
94 	struct uverbs_attr_bundle *attrs)
95 {
96 	const struct uverbs_api_object *uapi_object;
97 	ssize_t out_len;
98 	u64 total = 0;
99 	u16 object_id;
100 	u32 *handles;
101 	int ret;
102 
103 	out_len = uverbs_attr_get_len(attrs, UVERBS_ATTR_INFO_HANDLES_LIST);
104 	if (out_len <= 0 || (out_len % sizeof(u32) != 0))
105 		return -EINVAL;
106 
107 	ret = uverbs_get_const(&object_id, attrs, UVERBS_ATTR_INFO_OBJECT_ID);
108 	if (ret)
109 		return ret;
110 
111 	uapi_object = uapi_get_object(attrs->ufile->device->uapi, object_id);
112 	if (!uapi_object)
113 		return -EINVAL;
114 
115 	handles = gather_objects_handle(attrs->ufile, uapi_object, attrs,
116 					out_len, &total);
117 	if (IS_ERR(handles))
118 		return PTR_ERR(handles);
119 
120 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_INFO_HANDLES_LIST, handles,
121 			     sizeof(u32) * total);
122 	if (ret)
123 		goto err;
124 
125 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_INFO_TOTAL_HANDLES, &total,
126 			     sizeof(total));
127 err:
128 	return ret;
129 }
130 
copy_port_attr_to_resp(struct ib_port_attr * attr,struct ib_uverbs_query_port_resp * resp,struct ib_device * ib_dev,u8 port_num)131 void copy_port_attr_to_resp(struct ib_port_attr *attr,
132 			    struct ib_uverbs_query_port_resp *resp,
133 			    struct ib_device *ib_dev, u8 port_num)
134 {
135 	resp->state = attr->state;
136 	resp->max_mtu = attr->max_mtu;
137 	resp->active_mtu = attr->active_mtu;
138 	resp->gid_tbl_len = attr->gid_tbl_len;
139 	resp->port_cap_flags = make_port_cap_flags(attr);
140 	resp->max_msg_sz = attr->max_msg_sz;
141 	resp->bad_pkey_cntr = attr->bad_pkey_cntr;
142 	resp->qkey_viol_cntr = attr->qkey_viol_cntr;
143 	resp->pkey_tbl_len = attr->pkey_tbl_len;
144 
145 	if (attr->grh_required)
146 		resp->flags |= IB_UVERBS_QPF_GRH_REQUIRED;
147 
148 	resp->lid = (u16)attr->lid;
149 	resp->sm_lid = (u16)attr->sm_lid;
150 	resp->lmc = attr->lmc;
151 	resp->max_vl_num = attr->max_vl_num;
152 	resp->sm_sl = attr->sm_sl;
153 	resp->subnet_timeout = attr->subnet_timeout;
154 	resp->init_type_reply = attr->init_type_reply;
155 	resp->active_width = attr->active_width;
156 	resp->active_speed = attr->active_speed;
157 	resp->phys_state = attr->phys_state;
158 	resp->link_layer = rdma_port_get_link_layer(ib_dev, port_num);
159 }
160 
UVERBS_HANDLER(UVERBS_METHOD_QUERY_PORT)161 static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_PORT)(
162 	struct uverbs_attr_bundle *attrs)
163 {
164 	struct ib_device *ib_dev;
165 	struct ib_port_attr attr = {};
166 	struct ib_uverbs_query_port_resp_ex resp = {};
167 	struct ib_ucontext *ucontext;
168 	int ret;
169 	u8 port_num;
170 
171 	ucontext = ib_uverbs_get_ucontext(attrs);
172 	if (IS_ERR(ucontext))
173 		return PTR_ERR(ucontext);
174 	ib_dev = ucontext->device;
175 
176 	/* FIXME: Extend the UAPI_DEF_OBJ_NEEDS_FN stuff.. */
177 	if (!ib_dev->query_port)
178 		return -EOPNOTSUPP;
179 
180 	ret = uverbs_get_const(&port_num, attrs,
181 			       UVERBS_ATTR_QUERY_PORT_PORT_NUM);
182 	if (ret)
183 		return ret;
184 
185 	ret = ib_query_port(ib_dev, port_num, &attr);
186 	if (ret)
187 		return ret;
188 
189 	copy_port_attr_to_resp(&attr, &resp.legacy_resp, ib_dev, port_num);
190 	resp.port_cap_flags2 = 0;
191 
192 	return uverbs_copy_to_struct_or_zero(attrs, UVERBS_ATTR_QUERY_PORT_RESP,
193 					     &resp, sizeof(resp));
194 }
195 
UVERBS_HANDLER(UVERBS_METHOD_GET_CONTEXT)196 static int UVERBS_HANDLER(UVERBS_METHOD_GET_CONTEXT)(
197 	struct uverbs_attr_bundle *attrs)
198 {
199 	u32 num_comp = attrs->ufile->device->num_comp_vectors;
200 	u64 core_support = IB_UVERBS_CORE_SUPPORT_OPTIONAL_MR_ACCESS;
201 	int ret;
202 
203 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_GET_CONTEXT_NUM_COMP_VECTORS,
204 			     &num_comp, sizeof(num_comp));
205 	if (IS_UVERBS_COPY_ERR(ret))
206 		return ret;
207 
208 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_GET_CONTEXT_CORE_SUPPORT,
209 			     &core_support, sizeof(core_support));
210 	if (IS_UVERBS_COPY_ERR(ret))
211 		return ret;
212 
213 	ret = ib_alloc_ucontext(attrs);
214 	if (ret)
215 		return ret;
216 	ret = ib_init_ucontext(attrs);
217 	if (ret) {
218 		kfree(attrs->context);
219 		attrs->context = NULL;
220 		return ret;
221 	}
222 	return 0;
223 }
224 
225 DECLARE_UVERBS_NAMED_METHOD(
226 	UVERBS_METHOD_GET_CONTEXT,
227 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_GET_CONTEXT_NUM_COMP_VECTORS,
228 			    UVERBS_ATTR_TYPE(u32), UA_OPTIONAL),
229 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_GET_CONTEXT_CORE_SUPPORT,
230 			    UVERBS_ATTR_TYPE(u64), UA_OPTIONAL),
231 	UVERBS_ATTR_UHW());
232 
233 DECLARE_UVERBS_NAMED_METHOD(
234 	UVERBS_METHOD_INFO_HANDLES,
235 	/* Also includes any device specific object ids */
236 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_INFO_OBJECT_ID,
237 			     enum uverbs_default_objects, UA_MANDATORY),
238 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_INFO_TOTAL_HANDLES,
239 			    UVERBS_ATTR_TYPE(u32), UA_OPTIONAL),
240 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_INFO_HANDLES_LIST,
241 			    UVERBS_ATTR_MIN_SIZE(sizeof(u32)), UA_OPTIONAL));
242 
243 DECLARE_UVERBS_NAMED_METHOD(
244 	UVERBS_METHOD_QUERY_PORT,
245 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_PORT_PORT_NUM, u8, UA_MANDATORY),
246 	UVERBS_ATTR_PTR_OUT(
247 		UVERBS_ATTR_QUERY_PORT_RESP,
248 		UVERBS_ATTR_STRUCT(struct ib_uverbs_query_port_resp_ex,
249 				   reserved),
250 		UA_MANDATORY));
251 
252 DECLARE_UVERBS_GLOBAL_METHODS(UVERBS_OBJECT_DEVICE,
253 			      &UVERBS_METHOD(UVERBS_METHOD_GET_CONTEXT),
254 			      &UVERBS_METHOD(UVERBS_METHOD_INVOKE_WRITE),
255 			      &UVERBS_METHOD(UVERBS_METHOD_INFO_HANDLES),
256 			      &UVERBS_METHOD(UVERBS_METHOD_QUERY_PORT));
257 
258 const struct uapi_definition uverbs_def_obj_device[] = {
259 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DEVICE),
260 	{},
261 };
262