14785860eSJason Gunthorpe // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
24785860eSJason Gunthorpe /*
34785860eSJason Gunthorpe  * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
44785860eSJason Gunthorpe  */
54785860eSJason Gunthorpe 
69f85cbe5SAvihai Horon #include <linux/overflow.h>
74785860eSJason Gunthorpe #include <rdma/uverbs_std_types.h>
84785860eSJason Gunthorpe #include "rdma_core.h"
94785860eSJason Gunthorpe #include "uverbs.h"
10641d1207SMichael Guralnik #include <rdma/uverbs_ioctl.h>
11641d1207SMichael Guralnik #include <rdma/opa_addr.h>
129f85cbe5SAvihai Horon #include <rdma/ib_cache.h>
134785860eSJason Gunthorpe 
144785860eSJason Gunthorpe /*
154785860eSJason Gunthorpe  * This ioctl method allows calling any defined write or write_ex
164785860eSJason Gunthorpe  * handler. This essentially replaces the hdr/ex_hdr system with the ioctl
174785860eSJason Gunthorpe  * marshalling, and brings the non-ex path into the same marshalling as the ex
184785860eSJason Gunthorpe  * path.
194785860eSJason Gunthorpe  */
UVERBS_HANDLER(UVERBS_METHOD_INVOKE_WRITE)204785860eSJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_INVOKE_WRITE)(
214785860eSJason Gunthorpe 	struct uverbs_attr_bundle *attrs)
224785860eSJason Gunthorpe {
234785860eSJason Gunthorpe 	struct uverbs_api *uapi = attrs->ufile->device->uapi;
244785860eSJason Gunthorpe 	const struct uverbs_api_write_method *method_elm;
254785860eSJason Gunthorpe 	u32 cmd;
264785860eSJason Gunthorpe 	int rc;
274785860eSJason Gunthorpe 
284785860eSJason Gunthorpe 	rc = uverbs_get_const(&cmd, attrs, UVERBS_ATTR_WRITE_CMD);
294785860eSJason Gunthorpe 	if (rc)
304785860eSJason Gunthorpe 		return rc;
314785860eSJason Gunthorpe 
324785860eSJason Gunthorpe 	method_elm = uapi_get_method(uapi, cmd);
334785860eSJason Gunthorpe 	if (IS_ERR(method_elm))
344785860eSJason Gunthorpe 		return PTR_ERR(method_elm);
354785860eSJason Gunthorpe 
364785860eSJason Gunthorpe 	uverbs_fill_udata(attrs, &attrs->ucore, UVERBS_ATTR_CORE_IN,
374785860eSJason Gunthorpe 			  UVERBS_ATTR_CORE_OUT);
384785860eSJason Gunthorpe 
394785860eSJason Gunthorpe 	if (attrs->ucore.inlen < method_elm->req_size ||
404785860eSJason Gunthorpe 	    attrs->ucore.outlen < method_elm->resp_size)
414785860eSJason Gunthorpe 		return -ENOSPC;
424785860eSJason Gunthorpe 
430f63ef1dSLeon Romanovsky 	attrs->uobject = NULL;
440f63ef1dSLeon Romanovsky 	rc = method_elm->handler(attrs);
450f63ef1dSLeon Romanovsky 	if (attrs->uobject)
460f63ef1dSLeon Romanovsky 		uverbs_finalize_object(attrs->uobject, UVERBS_ACCESS_NEW, true,
470f63ef1dSLeon Romanovsky 				       !rc, attrs);
480f63ef1dSLeon Romanovsky 	return rc;
494785860eSJason Gunthorpe }
504785860eSJason Gunthorpe 
514785860eSJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_INVOKE_WRITE,
524785860eSJason Gunthorpe 			    UVERBS_ATTR_CONST_IN(UVERBS_ATTR_WRITE_CMD,
534785860eSJason Gunthorpe 						 enum ib_uverbs_write_cmds,
544785860eSJason Gunthorpe 						 UA_MANDATORY),
554785860eSJason Gunthorpe 			    UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CORE_IN,
564785860eSJason Gunthorpe 					       UVERBS_ATTR_MIN_SIZE(sizeof(u32)),
574785860eSJason Gunthorpe 					       UA_OPTIONAL),
584785860eSJason Gunthorpe 			    UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_CORE_OUT,
594785860eSJason Gunthorpe 						UVERBS_ATTR_MIN_SIZE(0),
604785860eSJason Gunthorpe 						UA_OPTIONAL),
614785860eSJason Gunthorpe 			    UVERBS_ATTR_UHW());
624785860eSJason Gunthorpe 
63149d3845SJason Gunthorpe 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)64149d3845SJason Gunthorpe gather_objects_handle(struct ib_uverbs_file *ufile,
65149d3845SJason Gunthorpe 		      const struct uverbs_api_object *uapi_object,
66149d3845SJason Gunthorpe 		      struct uverbs_attr_bundle *attrs,
67149d3845SJason Gunthorpe 		      ssize_t out_len,
68149d3845SJason Gunthorpe 		      u64 *total)
69149d3845SJason Gunthorpe {
70149d3845SJason Gunthorpe 	u64 max_count = out_len / sizeof(u32);
71149d3845SJason Gunthorpe 	struct ib_uobject *obj;
72149d3845SJason Gunthorpe 	u64 count = 0;
73149d3845SJason Gunthorpe 	u32 *handles;
74149d3845SJason Gunthorpe 
75149d3845SJason Gunthorpe 	/* Allocated memory that cannot page out where we gather
76149d3845SJason Gunthorpe 	 * all object ids under a spin_lock.
77149d3845SJason Gunthorpe 	 */
78149d3845SJason Gunthorpe 	handles = uverbs_zalloc(attrs, out_len);
79149d3845SJason Gunthorpe 	if (IS_ERR(handles))
80149d3845SJason Gunthorpe 		return handles;
81149d3845SJason Gunthorpe 
82149d3845SJason Gunthorpe 	spin_lock_irq(&ufile->uobjects_lock);
83149d3845SJason Gunthorpe 	list_for_each_entry(obj, &ufile->uobjects, list) {
84149d3845SJason Gunthorpe 		u32 obj_id = obj->id;
85149d3845SJason Gunthorpe 
86149d3845SJason Gunthorpe 		if (obj->uapi_object != uapi_object)
87149d3845SJason Gunthorpe 			continue;
88149d3845SJason Gunthorpe 
89149d3845SJason Gunthorpe 		if (count >= max_count)
90149d3845SJason Gunthorpe 			break;
91149d3845SJason Gunthorpe 
92149d3845SJason Gunthorpe 		handles[count] = obj_id;
93149d3845SJason Gunthorpe 		count++;
94149d3845SJason Gunthorpe 	}
95149d3845SJason Gunthorpe 	spin_unlock_irq(&ufile->uobjects_lock);
96149d3845SJason Gunthorpe 
97149d3845SJason Gunthorpe 	*total = count;
98149d3845SJason Gunthorpe 	return handles;
99149d3845SJason Gunthorpe }
100149d3845SJason Gunthorpe 
UVERBS_HANDLER(UVERBS_METHOD_INFO_HANDLES)101149d3845SJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_INFO_HANDLES)(
102149d3845SJason Gunthorpe 	struct uverbs_attr_bundle *attrs)
103149d3845SJason Gunthorpe {
104149d3845SJason Gunthorpe 	const struct uverbs_api_object *uapi_object;
105149d3845SJason Gunthorpe 	ssize_t out_len;
106149d3845SJason Gunthorpe 	u64 total = 0;
107149d3845SJason Gunthorpe 	u16 object_id;
108149d3845SJason Gunthorpe 	u32 *handles;
109149d3845SJason Gunthorpe 	int ret;
110149d3845SJason Gunthorpe 
111149d3845SJason Gunthorpe 	out_len = uverbs_attr_get_len(attrs, UVERBS_ATTR_INFO_HANDLES_LIST);
112149d3845SJason Gunthorpe 	if (out_len <= 0 || (out_len % sizeof(u32) != 0))
113149d3845SJason Gunthorpe 		return -EINVAL;
114149d3845SJason Gunthorpe 
115149d3845SJason Gunthorpe 	ret = uverbs_get_const(&object_id, attrs, UVERBS_ATTR_INFO_OBJECT_ID);
116149d3845SJason Gunthorpe 	if (ret)
117149d3845SJason Gunthorpe 		return ret;
118149d3845SJason Gunthorpe 
119149d3845SJason Gunthorpe 	uapi_object = uapi_get_object(attrs->ufile->device->uapi, object_id);
120*463a3f66SDan Carpenter 	if (IS_ERR(uapi_object))
121*463a3f66SDan Carpenter 		return PTR_ERR(uapi_object);
122149d3845SJason Gunthorpe 
123149d3845SJason Gunthorpe 	handles = gather_objects_handle(attrs->ufile, uapi_object, attrs,
124149d3845SJason Gunthorpe 					out_len, &total);
125149d3845SJason Gunthorpe 	if (IS_ERR(handles))
126149d3845SJason Gunthorpe 		return PTR_ERR(handles);
127149d3845SJason Gunthorpe 
128149d3845SJason Gunthorpe 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_INFO_HANDLES_LIST, handles,
129149d3845SJason Gunthorpe 			     sizeof(u32) * total);
130149d3845SJason Gunthorpe 	if (ret)
131149d3845SJason Gunthorpe 		goto err;
132149d3845SJason Gunthorpe 
133149d3845SJason Gunthorpe 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_INFO_TOTAL_HANDLES, &total,
134149d3845SJason Gunthorpe 			     sizeof(total));
135149d3845SJason Gunthorpe err:
136149d3845SJason Gunthorpe 	return ret;
137149d3845SJason Gunthorpe }
138149d3845SJason Gunthorpe 
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)139641d1207SMichael Guralnik void copy_port_attr_to_resp(struct ib_port_attr *attr,
140641d1207SMichael Guralnik 			    struct ib_uverbs_query_port_resp *resp,
141641d1207SMichael Guralnik 			    struct ib_device *ib_dev, u8 port_num)
142641d1207SMichael Guralnik {
143641d1207SMichael Guralnik 	resp->state = attr->state;
144641d1207SMichael Guralnik 	resp->max_mtu = attr->max_mtu;
145641d1207SMichael Guralnik 	resp->active_mtu = attr->active_mtu;
146641d1207SMichael Guralnik 	resp->gid_tbl_len = attr->gid_tbl_len;
147641d1207SMichael Guralnik 	resp->port_cap_flags = make_port_cap_flags(attr);
148641d1207SMichael Guralnik 	resp->max_msg_sz = attr->max_msg_sz;
149641d1207SMichael Guralnik 	resp->bad_pkey_cntr = attr->bad_pkey_cntr;
150641d1207SMichael Guralnik 	resp->qkey_viol_cntr = attr->qkey_viol_cntr;
151641d1207SMichael Guralnik 	resp->pkey_tbl_len = attr->pkey_tbl_len;
152641d1207SMichael Guralnik 
153641d1207SMichael Guralnik 	if (rdma_is_grh_required(ib_dev, port_num))
154641d1207SMichael Guralnik 		resp->flags |= IB_UVERBS_QPF_GRH_REQUIRED;
155641d1207SMichael Guralnik 
156641d1207SMichael Guralnik 	if (rdma_cap_opa_ah(ib_dev, port_num)) {
157641d1207SMichael Guralnik 		resp->lid = OPA_TO_IB_UCAST_LID(attr->lid);
158641d1207SMichael Guralnik 		resp->sm_lid = OPA_TO_IB_UCAST_LID(attr->sm_lid);
159641d1207SMichael Guralnik 	} else {
160641d1207SMichael Guralnik 		resp->lid = ib_lid_cpu16(attr->lid);
161641d1207SMichael Guralnik 		resp->sm_lid = ib_lid_cpu16(attr->sm_lid);
162641d1207SMichael Guralnik 	}
163641d1207SMichael Guralnik 
164641d1207SMichael Guralnik 	resp->lmc = attr->lmc;
165641d1207SMichael Guralnik 	resp->max_vl_num = attr->max_vl_num;
166641d1207SMichael Guralnik 	resp->sm_sl = attr->sm_sl;
167641d1207SMichael Guralnik 	resp->subnet_timeout = attr->subnet_timeout;
168641d1207SMichael Guralnik 	resp->init_type_reply = attr->init_type_reply;
169641d1207SMichael Guralnik 	resp->active_width = attr->active_width;
170376ceb31SAharon Landau 	/* This ABI needs to be extended to provide any speed more than IB_SPEED_NDR */
171376ceb31SAharon Landau 	resp->active_speed = min_t(u16, attr->active_speed, IB_SPEED_NDR);
172641d1207SMichael Guralnik 	resp->phys_state = attr->phys_state;
173641d1207SMichael Guralnik 	resp->link_layer = rdma_port_get_link_layer(ib_dev, port_num);
174641d1207SMichael Guralnik }
175641d1207SMichael Guralnik 
UVERBS_HANDLER(UVERBS_METHOD_QUERY_PORT)176641d1207SMichael Guralnik static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_PORT)(
177641d1207SMichael Guralnik 	struct uverbs_attr_bundle *attrs)
178641d1207SMichael Guralnik {
179f8ade8e2SYishai Hadas 	struct ib_device *ib_dev;
180641d1207SMichael Guralnik 	struct ib_port_attr attr = {};
181641d1207SMichael Guralnik 	struct ib_uverbs_query_port_resp_ex resp = {};
182f8ade8e2SYishai Hadas 	struct ib_ucontext *ucontext;
183641d1207SMichael Guralnik 	int ret;
184641d1207SMichael Guralnik 	u8 port_num;
185641d1207SMichael Guralnik 
186f8ade8e2SYishai Hadas 	ucontext = ib_uverbs_get_ucontext(attrs);
187f8ade8e2SYishai Hadas 	if (IS_ERR(ucontext))
188f8ade8e2SYishai Hadas 		return PTR_ERR(ucontext);
189f8ade8e2SYishai Hadas 	ib_dev = ucontext->device;
190f8ade8e2SYishai Hadas 
191641d1207SMichael Guralnik 	/* FIXME: Extend the UAPI_DEF_OBJ_NEEDS_FN stuff.. */
192641d1207SMichael Guralnik 	if (!ib_dev->ops.query_port)
193641d1207SMichael Guralnik 		return -EOPNOTSUPP;
194641d1207SMichael Guralnik 
195641d1207SMichael Guralnik 	ret = uverbs_get_const(&port_num, attrs,
196641d1207SMichael Guralnik 			       UVERBS_ATTR_QUERY_PORT_PORT_NUM);
197641d1207SMichael Guralnik 	if (ret)
198641d1207SMichael Guralnik 		return ret;
199641d1207SMichael Guralnik 
200641d1207SMichael Guralnik 	ret = ib_query_port(ib_dev, port_num, &attr);
201641d1207SMichael Guralnik 	if (ret)
202641d1207SMichael Guralnik 		return ret;
203641d1207SMichael Guralnik 
204641d1207SMichael Guralnik 	copy_port_attr_to_resp(&attr, &resp.legacy_resp, ib_dev, port_num);
205641d1207SMichael Guralnik 	resp.port_cap_flags2 = attr.port_cap_flags2;
206641d1207SMichael Guralnik 
207641d1207SMichael Guralnik 	return uverbs_copy_to_struct_or_zero(attrs, UVERBS_ATTR_QUERY_PORT_RESP,
208641d1207SMichael Guralnik 					     &resp, sizeof(resp));
209641d1207SMichael Guralnik }
210641d1207SMichael Guralnik 
UVERBS_HANDLER(UVERBS_METHOD_GET_CONTEXT)211a1123418SJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_GET_CONTEXT)(
212a1123418SJason Gunthorpe 	struct uverbs_attr_bundle *attrs)
213a1123418SJason Gunthorpe {
214a1123418SJason Gunthorpe 	u32 num_comp = attrs->ufile->device->num_comp_vectors;
21581164699SMichael Guralnik 	u64 core_support = IB_UVERBS_CORE_SUPPORT_OPTIONAL_MR_ACCESS;
216a1123418SJason Gunthorpe 	int ret;
217a1123418SJason Gunthorpe 
218a1123418SJason Gunthorpe 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_GET_CONTEXT_NUM_COMP_VECTORS,
219a1123418SJason Gunthorpe 			     &num_comp, sizeof(num_comp));
220a1123418SJason Gunthorpe 	if (IS_UVERBS_COPY_ERR(ret))
221a1123418SJason Gunthorpe 		return ret;
222a1123418SJason Gunthorpe 
22381164699SMichael Guralnik 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_GET_CONTEXT_CORE_SUPPORT,
22481164699SMichael Guralnik 			     &core_support, sizeof(core_support));
22581164699SMichael Guralnik 	if (IS_UVERBS_COPY_ERR(ret))
22681164699SMichael Guralnik 		return ret;
22781164699SMichael Guralnik 
228a1123418SJason Gunthorpe 	ret = ib_alloc_ucontext(attrs);
229a1123418SJason Gunthorpe 	if (ret)
230a1123418SJason Gunthorpe 		return ret;
231a1123418SJason Gunthorpe 	ret = ib_init_ucontext(attrs);
232a1123418SJason Gunthorpe 	if (ret) {
233a1123418SJason Gunthorpe 		kfree(attrs->context);
234a1123418SJason Gunthorpe 		attrs->context = NULL;
235a1123418SJason Gunthorpe 		return ret;
236a1123418SJason Gunthorpe 	}
237a1123418SJason Gunthorpe 	return 0;
238a1123418SJason Gunthorpe }
239a1123418SJason Gunthorpe 
UVERBS_HANDLER(UVERBS_METHOD_QUERY_CONTEXT)2401c8fb1eaSYishai Hadas static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_CONTEXT)(
2411c8fb1eaSYishai Hadas 	struct uverbs_attr_bundle *attrs)
2421c8fb1eaSYishai Hadas {
2431c8fb1eaSYishai Hadas 	u64 core_support = IB_UVERBS_CORE_SUPPORT_OPTIONAL_MR_ACCESS;
2441c8fb1eaSYishai Hadas 	struct ib_ucontext *ucontext;
2451c8fb1eaSYishai Hadas 	struct ib_device *ib_dev;
2461c8fb1eaSYishai Hadas 	u32 num_comp;
2471c8fb1eaSYishai Hadas 	int ret;
2481c8fb1eaSYishai Hadas 
2491c8fb1eaSYishai Hadas 	ucontext = ib_uverbs_get_ucontext(attrs);
2501c8fb1eaSYishai Hadas 	if (IS_ERR(ucontext))
2511c8fb1eaSYishai Hadas 		return PTR_ERR(ucontext);
2521c8fb1eaSYishai Hadas 	ib_dev = ucontext->device;
2531c8fb1eaSYishai Hadas 
2541c8fb1eaSYishai Hadas 	if (!ib_dev->ops.query_ucontext)
2551c8fb1eaSYishai Hadas 		return -EOPNOTSUPP;
2561c8fb1eaSYishai Hadas 
2571c8fb1eaSYishai Hadas 	num_comp = attrs->ufile->device->num_comp_vectors;
2581c8fb1eaSYishai Hadas 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_QUERY_CONTEXT_NUM_COMP_VECTORS,
2591c8fb1eaSYishai Hadas 			     &num_comp, sizeof(num_comp));
2601c8fb1eaSYishai Hadas 	if (IS_UVERBS_COPY_ERR(ret))
2611c8fb1eaSYishai Hadas 		return ret;
2621c8fb1eaSYishai Hadas 
2631c8fb1eaSYishai Hadas 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_QUERY_CONTEXT_CORE_SUPPORT,
2641c8fb1eaSYishai Hadas 			     &core_support, sizeof(core_support));
2651c8fb1eaSYishai Hadas 	if (IS_UVERBS_COPY_ERR(ret))
2661c8fb1eaSYishai Hadas 		return ret;
2671c8fb1eaSYishai Hadas 
2681c8fb1eaSYishai Hadas 	return ucontext->device->ops.query_ucontext(ucontext, attrs);
2691c8fb1eaSYishai Hadas }
2701c8fb1eaSYishai Hadas 
copy_gid_entries_to_user(struct uverbs_attr_bundle * attrs,struct ib_uverbs_gid_entry * entries,size_t num_entries,size_t user_entry_size)2719f85cbe5SAvihai Horon static int copy_gid_entries_to_user(struct uverbs_attr_bundle *attrs,
2729f85cbe5SAvihai Horon 				    struct ib_uverbs_gid_entry *entries,
2739f85cbe5SAvihai Horon 				    size_t num_entries, size_t user_entry_size)
2749f85cbe5SAvihai Horon {
2759f85cbe5SAvihai Horon 	const struct uverbs_attr *attr;
2769f85cbe5SAvihai Horon 	void __user *user_entries;
2779f85cbe5SAvihai Horon 	size_t copy_len;
2789f85cbe5SAvihai Horon 	int ret;
2799f85cbe5SAvihai Horon 	int i;
2809f85cbe5SAvihai Horon 
2819f85cbe5SAvihai Horon 	if (user_entry_size == sizeof(*entries)) {
2829f85cbe5SAvihai Horon 		ret = uverbs_copy_to(attrs,
2839f85cbe5SAvihai Horon 				     UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES,
2849f85cbe5SAvihai Horon 				     entries, sizeof(*entries) * num_entries);
2859f85cbe5SAvihai Horon 		return ret;
2869f85cbe5SAvihai Horon 	}
2879f85cbe5SAvihai Horon 
2889f85cbe5SAvihai Horon 	copy_len = min_t(size_t, user_entry_size, sizeof(*entries));
2899f85cbe5SAvihai Horon 	attr = uverbs_attr_get(attrs, UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES);
2909f85cbe5SAvihai Horon 	if (IS_ERR(attr))
2919f85cbe5SAvihai Horon 		return PTR_ERR(attr);
2929f85cbe5SAvihai Horon 
2939f85cbe5SAvihai Horon 	user_entries = u64_to_user_ptr(attr->ptr_attr.data);
2949f85cbe5SAvihai Horon 	for (i = 0; i < num_entries; i++) {
2959f85cbe5SAvihai Horon 		if (copy_to_user(user_entries, entries, copy_len))
2969f85cbe5SAvihai Horon 			return -EFAULT;
2979f85cbe5SAvihai Horon 
2989f85cbe5SAvihai Horon 		if (user_entry_size > sizeof(*entries)) {
2999f85cbe5SAvihai Horon 			if (clear_user(user_entries + sizeof(*entries),
3009f85cbe5SAvihai Horon 				       user_entry_size - sizeof(*entries)))
3019f85cbe5SAvihai Horon 				return -EFAULT;
3029f85cbe5SAvihai Horon 		}
3039f85cbe5SAvihai Horon 
3049f85cbe5SAvihai Horon 		entries++;
3059f85cbe5SAvihai Horon 		user_entries += user_entry_size;
3069f85cbe5SAvihai Horon 	}
3079f85cbe5SAvihai Horon 
3089f85cbe5SAvihai Horon 	return uverbs_output_written(attrs,
3099f85cbe5SAvihai Horon 				     UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES);
3109f85cbe5SAvihai Horon }
3119f85cbe5SAvihai Horon 
UVERBS_HANDLER(UVERBS_METHOD_QUERY_GID_TABLE)3129f85cbe5SAvihai Horon static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_GID_TABLE)(
3139f85cbe5SAvihai Horon 	struct uverbs_attr_bundle *attrs)
3149f85cbe5SAvihai Horon {
3159f85cbe5SAvihai Horon 	struct ib_uverbs_gid_entry *entries;
3169f85cbe5SAvihai Horon 	struct ib_ucontext *ucontext;
3179f85cbe5SAvihai Horon 	struct ib_device *ib_dev;
3189f85cbe5SAvihai Horon 	size_t user_entry_size;
3199f85cbe5SAvihai Horon 	ssize_t num_entries;
320e0da6899SAvihai Horon 	int max_entries;
3219f85cbe5SAvihai Horon 	u32 flags;
3229f85cbe5SAvihai Horon 	int ret;
3239f85cbe5SAvihai Horon 
3249f85cbe5SAvihai Horon 	ret = uverbs_get_flags32(&flags, attrs,
3259f85cbe5SAvihai Horon 				 UVERBS_ATTR_QUERY_GID_TABLE_FLAGS, 0);
3269f85cbe5SAvihai Horon 	if (ret)
3279f85cbe5SAvihai Horon 		return ret;
3289f85cbe5SAvihai Horon 
3299f85cbe5SAvihai Horon 	ret = uverbs_get_const(&user_entry_size, attrs,
3309f85cbe5SAvihai Horon 			       UVERBS_ATTR_QUERY_GID_TABLE_ENTRY_SIZE);
3319f85cbe5SAvihai Horon 	if (ret)
3329f85cbe5SAvihai Horon 		return ret;
3339f85cbe5SAvihai Horon 
33454d87913SLeon Romanovsky 	if (!user_entry_size)
33554d87913SLeon Romanovsky 		return -EINVAL;
33654d87913SLeon Romanovsky 
3379f85cbe5SAvihai Horon 	max_entries = uverbs_attr_ptr_get_array_size(
3389f85cbe5SAvihai Horon 		attrs, UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES,
3399f85cbe5SAvihai Horon 		user_entry_size);
3409f85cbe5SAvihai Horon 	if (max_entries <= 0)
341e0da6899SAvihai Horon 		return max_entries ?: -EINVAL;
3429f85cbe5SAvihai Horon 
3439f85cbe5SAvihai Horon 	ucontext = ib_uverbs_get_ucontext(attrs);
3449f85cbe5SAvihai Horon 	if (IS_ERR(ucontext))
3459f85cbe5SAvihai Horon 		return PTR_ERR(ucontext);
3469f85cbe5SAvihai Horon 	ib_dev = ucontext->device;
3479f85cbe5SAvihai Horon 
348e0da6899SAvihai Horon 	entries = uverbs_kcalloc(attrs, max_entries, sizeof(*entries));
349e0da6899SAvihai Horon 	if (IS_ERR(entries))
350e0da6899SAvihai Horon 		return PTR_ERR(entries);
3519f85cbe5SAvihai Horon 
3529f85cbe5SAvihai Horon 	num_entries = rdma_query_gid_table(ib_dev, entries, max_entries);
3539f85cbe5SAvihai Horon 	if (num_entries < 0)
3549f85cbe5SAvihai Horon 		return -EINVAL;
3559f85cbe5SAvihai Horon 
3569f85cbe5SAvihai Horon 	ret = copy_gid_entries_to_user(attrs, entries, num_entries,
3579f85cbe5SAvihai Horon 				       user_entry_size);
3589f85cbe5SAvihai Horon 	if (ret)
3599f85cbe5SAvihai Horon 		return ret;
3609f85cbe5SAvihai Horon 
3619f85cbe5SAvihai Horon 	ret = uverbs_copy_to(attrs,
3629f85cbe5SAvihai Horon 			     UVERBS_ATTR_QUERY_GID_TABLE_RESP_NUM_ENTRIES,
3639f85cbe5SAvihai Horon 			     &num_entries, sizeof(num_entries));
3649f85cbe5SAvihai Horon 	return ret;
3659f85cbe5SAvihai Horon }
3669f85cbe5SAvihai Horon 
UVERBS_HANDLER(UVERBS_METHOD_QUERY_GID_ENTRY)3679f85cbe5SAvihai Horon static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_GID_ENTRY)(
3689f85cbe5SAvihai Horon 	struct uverbs_attr_bundle *attrs)
3699f85cbe5SAvihai Horon {
3709f85cbe5SAvihai Horon 	struct ib_uverbs_gid_entry entry = {};
3719f85cbe5SAvihai Horon 	const struct ib_gid_attr *gid_attr;
3729f85cbe5SAvihai Horon 	struct ib_ucontext *ucontext;
3739f85cbe5SAvihai Horon 	struct ib_device *ib_dev;
3749f85cbe5SAvihai Horon 	struct net_device *ndev;
3759f85cbe5SAvihai Horon 	u32 gid_index;
3769f85cbe5SAvihai Horon 	u32 port_num;
3779f85cbe5SAvihai Horon 	u32 flags;
3789f85cbe5SAvihai Horon 	int ret;
3799f85cbe5SAvihai Horon 
3809f85cbe5SAvihai Horon 	ret = uverbs_get_flags32(&flags, attrs,
3819f85cbe5SAvihai Horon 				 UVERBS_ATTR_QUERY_GID_ENTRY_FLAGS, 0);
3829f85cbe5SAvihai Horon 	if (ret)
3839f85cbe5SAvihai Horon 		return ret;
3849f85cbe5SAvihai Horon 
3859f85cbe5SAvihai Horon 	ret = uverbs_get_const(&port_num, attrs,
3869f85cbe5SAvihai Horon 			       UVERBS_ATTR_QUERY_GID_ENTRY_PORT);
3879f85cbe5SAvihai Horon 	if (ret)
3889f85cbe5SAvihai Horon 		return ret;
3899f85cbe5SAvihai Horon 
3909f85cbe5SAvihai Horon 	ret = uverbs_get_const(&gid_index, attrs,
3919f85cbe5SAvihai Horon 			       UVERBS_ATTR_QUERY_GID_ENTRY_GID_INDEX);
3929f85cbe5SAvihai Horon 	if (ret)
3939f85cbe5SAvihai Horon 		return ret;
3949f85cbe5SAvihai Horon 
3959f85cbe5SAvihai Horon 	ucontext = ib_uverbs_get_ucontext(attrs);
3969f85cbe5SAvihai Horon 	if (IS_ERR(ucontext))
3979f85cbe5SAvihai Horon 		return PTR_ERR(ucontext);
3989f85cbe5SAvihai Horon 	ib_dev = ucontext->device;
3999f85cbe5SAvihai Horon 
4009f85cbe5SAvihai Horon 	if (!rdma_is_port_valid(ib_dev, port_num))
4019f85cbe5SAvihai Horon 		return -EINVAL;
4029f85cbe5SAvihai Horon 
4039f85cbe5SAvihai Horon 	gid_attr = rdma_get_gid_attr(ib_dev, port_num, gid_index);
4049f85cbe5SAvihai Horon 	if (IS_ERR(gid_attr))
4059f85cbe5SAvihai Horon 		return PTR_ERR(gid_attr);
4069f85cbe5SAvihai Horon 
4079f85cbe5SAvihai Horon 	memcpy(&entry.gid, &gid_attr->gid, sizeof(gid_attr->gid));
4089f85cbe5SAvihai Horon 	entry.gid_index = gid_attr->index;
4099f85cbe5SAvihai Horon 	entry.port_num = gid_attr->port_num;
4109f85cbe5SAvihai Horon 	entry.gid_type = gid_attr->gid_type;
4119f85cbe5SAvihai Horon 
4129f85cbe5SAvihai Horon 	rcu_read_lock();
4139f85cbe5SAvihai Horon 	ndev = rdma_read_gid_attr_ndev_rcu(gid_attr);
4149f85cbe5SAvihai Horon 	if (IS_ERR(ndev)) {
4159f85cbe5SAvihai Horon 		if (PTR_ERR(ndev) != -ENODEV) {
4169f85cbe5SAvihai Horon 			ret = PTR_ERR(ndev);
4179f85cbe5SAvihai Horon 			rcu_read_unlock();
4189f85cbe5SAvihai Horon 			goto out;
4199f85cbe5SAvihai Horon 		}
4209f85cbe5SAvihai Horon 	} else {
4219f85cbe5SAvihai Horon 		entry.netdev_ifindex = ndev->ifindex;
4229f85cbe5SAvihai Horon 	}
4239f85cbe5SAvihai Horon 	rcu_read_unlock();
4249f85cbe5SAvihai Horon 
4259f85cbe5SAvihai Horon 	ret = uverbs_copy_to_struct_or_zero(
4269f85cbe5SAvihai Horon 		attrs, UVERBS_ATTR_QUERY_GID_ENTRY_RESP_ENTRY, &entry,
4279f85cbe5SAvihai Horon 		sizeof(entry));
4289f85cbe5SAvihai Horon out:
4299f85cbe5SAvihai Horon 	rdma_put_gid_attr(gid_attr);
4309f85cbe5SAvihai Horon 	return ret;
4319f85cbe5SAvihai Horon }
4329f85cbe5SAvihai Horon 
433a1123418SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
434a1123418SJason Gunthorpe 	UVERBS_METHOD_GET_CONTEXT,
435a1123418SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_GET_CONTEXT_NUM_COMP_VECTORS,
436a1123418SJason Gunthorpe 			    UVERBS_ATTR_TYPE(u32), UA_OPTIONAL),
43781164699SMichael Guralnik 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_GET_CONTEXT_CORE_SUPPORT,
43881164699SMichael Guralnik 			    UVERBS_ATTR_TYPE(u64), UA_OPTIONAL),
439a1123418SJason Gunthorpe 	UVERBS_ATTR_UHW());
440a1123418SJason Gunthorpe 
441149d3845SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
4421c8fb1eaSYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
4431c8fb1eaSYishai Hadas 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_CONTEXT_NUM_COMP_VECTORS,
4441c8fb1eaSYishai Hadas 			    UVERBS_ATTR_TYPE(u32), UA_OPTIONAL),
4451c8fb1eaSYishai Hadas 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_CONTEXT_CORE_SUPPORT,
4461c8fb1eaSYishai Hadas 			    UVERBS_ATTR_TYPE(u64), UA_OPTIONAL));
4471c8fb1eaSYishai Hadas 
4481c8fb1eaSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
449149d3845SJason Gunthorpe 	UVERBS_METHOD_INFO_HANDLES,
450149d3845SJason Gunthorpe 	/* Also includes any device specific object ids */
451149d3845SJason Gunthorpe 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_INFO_OBJECT_ID,
452149d3845SJason Gunthorpe 			     enum uverbs_default_objects, UA_MANDATORY),
453149d3845SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_INFO_TOTAL_HANDLES,
454149d3845SJason Gunthorpe 			    UVERBS_ATTR_TYPE(u32), UA_OPTIONAL),
455149d3845SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_INFO_HANDLES_LIST,
456149d3845SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(sizeof(u32)), UA_OPTIONAL));
457149d3845SJason Gunthorpe 
458641d1207SMichael Guralnik DECLARE_UVERBS_NAMED_METHOD(
459641d1207SMichael Guralnik 	UVERBS_METHOD_QUERY_PORT,
460641d1207SMichael Guralnik 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_PORT_PORT_NUM, u8, UA_MANDATORY),
461641d1207SMichael Guralnik 	UVERBS_ATTR_PTR_OUT(
462641d1207SMichael Guralnik 		UVERBS_ATTR_QUERY_PORT_RESP,
463641d1207SMichael Guralnik 		UVERBS_ATTR_STRUCT(struct ib_uverbs_query_port_resp_ex,
464641d1207SMichael Guralnik 				   reserved),
465641d1207SMichael Guralnik 		UA_MANDATORY));
466641d1207SMichael Guralnik 
4679f85cbe5SAvihai Horon DECLARE_UVERBS_NAMED_METHOD(
4689f85cbe5SAvihai Horon 	UVERBS_METHOD_QUERY_GID_TABLE,
4699f85cbe5SAvihai Horon 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_GID_TABLE_ENTRY_SIZE, u64,
4709f85cbe5SAvihai Horon 			     UA_MANDATORY),
4719f85cbe5SAvihai Horon 	UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_QUERY_GID_TABLE_FLAGS, u32,
4729f85cbe5SAvihai Horon 			     UA_OPTIONAL),
4739f85cbe5SAvihai Horon 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES,
4749f85cbe5SAvihai Horon 			    UVERBS_ATTR_MIN_SIZE(0), UA_MANDATORY),
4759f85cbe5SAvihai Horon 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_GID_TABLE_RESP_NUM_ENTRIES,
4769f85cbe5SAvihai Horon 			    UVERBS_ATTR_TYPE(u64), UA_MANDATORY));
4779f85cbe5SAvihai Horon 
4789f85cbe5SAvihai Horon DECLARE_UVERBS_NAMED_METHOD(
4799f85cbe5SAvihai Horon 	UVERBS_METHOD_QUERY_GID_ENTRY,
4809f85cbe5SAvihai Horon 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_GID_ENTRY_PORT, u32,
4819f85cbe5SAvihai Horon 			     UA_MANDATORY),
4829f85cbe5SAvihai Horon 	UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_GID_ENTRY_GID_INDEX, u32,
4839f85cbe5SAvihai Horon 			     UA_MANDATORY),
4849f85cbe5SAvihai Horon 	UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_QUERY_GID_ENTRY_FLAGS, u32,
4859f85cbe5SAvihai Horon 			     UA_MANDATORY),
4869f85cbe5SAvihai Horon 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_GID_ENTRY_RESP_ENTRY,
4879f85cbe5SAvihai Horon 			    UVERBS_ATTR_STRUCT(struct ib_uverbs_gid_entry,
4889f85cbe5SAvihai Horon 					       netdev_ifindex),
4899f85cbe5SAvihai Horon 			    UA_MANDATORY));
4909f85cbe5SAvihai Horon 
4914785860eSJason Gunthorpe DECLARE_UVERBS_GLOBAL_METHODS(UVERBS_OBJECT_DEVICE,
492a1123418SJason Gunthorpe 			      &UVERBS_METHOD(UVERBS_METHOD_GET_CONTEXT),
493149d3845SJason Gunthorpe 			      &UVERBS_METHOD(UVERBS_METHOD_INVOKE_WRITE),
494641d1207SMichael Guralnik 			      &UVERBS_METHOD(UVERBS_METHOD_INFO_HANDLES),
4951c8fb1eaSYishai Hadas 			      &UVERBS_METHOD(UVERBS_METHOD_QUERY_PORT),
4969f85cbe5SAvihai Horon 			      &UVERBS_METHOD(UVERBS_METHOD_QUERY_CONTEXT),
4979f85cbe5SAvihai Horon 			      &UVERBS_METHOD(UVERBS_METHOD_QUERY_GID_TABLE),
4989f85cbe5SAvihai Horon 			      &UVERBS_METHOD(UVERBS_METHOD_QUERY_GID_ENTRY));
4994785860eSJason Gunthorpe 
5004785860eSJason Gunthorpe const struct uapi_definition uverbs_def_obj_device[] = {
5014785860eSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DEVICE),
5024785860eSJason Gunthorpe 	{},
5034785860eSJason Gunthorpe };
504