OSDN Git Service

arm64: dts: clearfog-gt-8k: fix SGMII PHY reset signal
[uclinux-h8/linux.git] / drivers / infiniband / core / uverbs_std_types_device.c
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 #include <rdma/opa_addr.h>
11
12 /*
13  * This ioctl method allows calling any defined write or write_ex
14  * handler. This essentially replaces the hdr/ex_hdr system with the ioctl
15  * marshalling, and brings the non-ex path into the same marshalling as the ex
16  * path.
17  */
18 static int UVERBS_HANDLER(UVERBS_METHOD_INVOKE_WRITE)(
19         struct uverbs_attr_bundle *attrs)
20 {
21         struct uverbs_api *uapi = attrs->ufile->device->uapi;
22         const struct uverbs_api_write_method *method_elm;
23         u32 cmd;
24         int rc;
25
26         rc = uverbs_get_const(&cmd, attrs, UVERBS_ATTR_WRITE_CMD);
27         if (rc)
28                 return rc;
29
30         method_elm = uapi_get_method(uapi, cmd);
31         if (IS_ERR(method_elm))
32                 return PTR_ERR(method_elm);
33
34         uverbs_fill_udata(attrs, &attrs->ucore, UVERBS_ATTR_CORE_IN,
35                           UVERBS_ATTR_CORE_OUT);
36
37         if (attrs->ucore.inlen < method_elm->req_size ||
38             attrs->ucore.outlen < method_elm->resp_size)
39                 return -ENOSPC;
40
41         return method_elm->handler(attrs);
42 }
43
44 DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_INVOKE_WRITE,
45                             UVERBS_ATTR_CONST_IN(UVERBS_ATTR_WRITE_CMD,
46                                                  enum ib_uverbs_write_cmds,
47                                                  UA_MANDATORY),
48                             UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CORE_IN,
49                                                UVERBS_ATTR_MIN_SIZE(sizeof(u32)),
50                                                UA_OPTIONAL),
51                             UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_CORE_OUT,
52                                                 UVERBS_ATTR_MIN_SIZE(0),
53                                                 UA_OPTIONAL),
54                             UVERBS_ATTR_UHW());
55
56 static uint32_t *
57 gather_objects_handle(struct ib_uverbs_file *ufile,
58                       const struct uverbs_api_object *uapi_object,
59                       struct uverbs_attr_bundle *attrs,
60                       ssize_t out_len,
61                       u64 *total)
62 {
63         u64 max_count = out_len / sizeof(u32);
64         struct ib_uobject *obj;
65         u64 count = 0;
66         u32 *handles;
67
68         /* Allocated memory that cannot page out where we gather
69          * all object ids under a spin_lock.
70          */
71         handles = uverbs_zalloc(attrs, out_len);
72         if (IS_ERR(handles))
73                 return handles;
74
75         spin_lock_irq(&ufile->uobjects_lock);
76         list_for_each_entry(obj, &ufile->uobjects, list) {
77                 u32 obj_id = obj->id;
78
79                 if (obj->uapi_object != uapi_object)
80                         continue;
81
82                 if (count >= max_count)
83                         break;
84
85                 handles[count] = obj_id;
86                 count++;
87         }
88         spin_unlock_irq(&ufile->uobjects_lock);
89
90         *total = count;
91         return handles;
92 }
93
94 static int UVERBS_HANDLER(UVERBS_METHOD_INFO_HANDLES)(
95         struct uverbs_attr_bundle *attrs)
96 {
97         const struct uverbs_api_object *uapi_object;
98         ssize_t out_len;
99         u64 total = 0;
100         u16 object_id;
101         u32 *handles;
102         int ret;
103
104         out_len = uverbs_attr_get_len(attrs, UVERBS_ATTR_INFO_HANDLES_LIST);
105         if (out_len <= 0 || (out_len % sizeof(u32) != 0))
106                 return -EINVAL;
107
108         ret = uverbs_get_const(&object_id, attrs, UVERBS_ATTR_INFO_OBJECT_ID);
109         if (ret)
110                 return ret;
111
112         uapi_object = uapi_get_object(attrs->ufile->device->uapi, object_id);
113         if (!uapi_object)
114                 return -EINVAL;
115
116         handles = gather_objects_handle(attrs->ufile, uapi_object, attrs,
117                                         out_len, &total);
118         if (IS_ERR(handles))
119                 return PTR_ERR(handles);
120
121         ret = uverbs_copy_to(attrs, UVERBS_ATTR_INFO_HANDLES_LIST, handles,
122                              sizeof(u32) * total);
123         if (ret)
124                 goto err;
125
126         ret = uverbs_copy_to(attrs, UVERBS_ATTR_INFO_TOTAL_HANDLES, &total,
127                              sizeof(total));
128 err:
129         return ret;
130 }
131
132 void copy_port_attr_to_resp(struct ib_port_attr *attr,
133                             struct ib_uverbs_query_port_resp *resp,
134                             struct ib_device *ib_dev, u8 port_num)
135 {
136         resp->state = attr->state;
137         resp->max_mtu = attr->max_mtu;
138         resp->active_mtu = attr->active_mtu;
139         resp->gid_tbl_len = attr->gid_tbl_len;
140         resp->port_cap_flags = make_port_cap_flags(attr);
141         resp->max_msg_sz = attr->max_msg_sz;
142         resp->bad_pkey_cntr = attr->bad_pkey_cntr;
143         resp->qkey_viol_cntr = attr->qkey_viol_cntr;
144         resp->pkey_tbl_len = attr->pkey_tbl_len;
145
146         if (rdma_is_grh_required(ib_dev, port_num))
147                 resp->flags |= IB_UVERBS_QPF_GRH_REQUIRED;
148
149         if (rdma_cap_opa_ah(ib_dev, port_num)) {
150                 resp->lid = OPA_TO_IB_UCAST_LID(attr->lid);
151                 resp->sm_lid = OPA_TO_IB_UCAST_LID(attr->sm_lid);
152         } else {
153                 resp->lid = ib_lid_cpu16(attr->lid);
154                 resp->sm_lid = ib_lid_cpu16(attr->sm_lid);
155         }
156
157         resp->lmc = attr->lmc;
158         resp->max_vl_num = attr->max_vl_num;
159         resp->sm_sl = attr->sm_sl;
160         resp->subnet_timeout = attr->subnet_timeout;
161         resp->init_type_reply = attr->init_type_reply;
162         resp->active_width = attr->active_width;
163         resp->active_speed = attr->active_speed;
164         resp->phys_state = attr->phys_state;
165         resp->link_layer = rdma_port_get_link_layer(ib_dev, port_num);
166 }
167
168 static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_PORT)(
169         struct uverbs_attr_bundle *attrs)
170 {
171         struct ib_device *ib_dev = attrs->ufile->device->ib_dev;
172         struct ib_port_attr attr = {};
173         struct ib_uverbs_query_port_resp_ex resp = {};
174         int ret;
175         u8 port_num;
176
177         /* FIXME: Extend the UAPI_DEF_OBJ_NEEDS_FN stuff.. */
178         if (!ib_dev->ops.query_port)
179                 return -EOPNOTSUPP;
180
181         ret = uverbs_get_const(&port_num, attrs,
182                                UVERBS_ATTR_QUERY_PORT_PORT_NUM);
183         if (ret)
184                 return ret;
185
186         ret = ib_query_port(ib_dev, port_num, &attr);
187         if (ret)
188                 return ret;
189
190         copy_port_attr_to_resp(&attr, &resp.legacy_resp, ib_dev, port_num);
191         resp.port_cap_flags2 = attr.port_cap_flags2;
192
193         return uverbs_copy_to_struct_or_zero(attrs, UVERBS_ATTR_QUERY_PORT_RESP,
194                                              &resp, sizeof(resp));
195 }
196
197 DECLARE_UVERBS_NAMED_METHOD(
198         UVERBS_METHOD_INFO_HANDLES,
199         /* Also includes any device specific object ids */
200         UVERBS_ATTR_CONST_IN(UVERBS_ATTR_INFO_OBJECT_ID,
201                              enum uverbs_default_objects, UA_MANDATORY),
202         UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_INFO_TOTAL_HANDLES,
203                             UVERBS_ATTR_TYPE(u32), UA_OPTIONAL),
204         UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_INFO_HANDLES_LIST,
205                             UVERBS_ATTR_MIN_SIZE(sizeof(u32)), UA_OPTIONAL));
206
207 DECLARE_UVERBS_NAMED_METHOD(
208         UVERBS_METHOD_QUERY_PORT,
209         UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_PORT_PORT_NUM, u8, UA_MANDATORY),
210         UVERBS_ATTR_PTR_OUT(
211                 UVERBS_ATTR_QUERY_PORT_RESP,
212                 UVERBS_ATTR_STRUCT(struct ib_uverbs_query_port_resp_ex,
213                                    reserved),
214                 UA_MANDATORY));
215
216 DECLARE_UVERBS_GLOBAL_METHODS(UVERBS_OBJECT_DEVICE,
217                               &UVERBS_METHOD(UVERBS_METHOD_INVOKE_WRITE),
218                               &UVERBS_METHOD(UVERBS_METHOD_INFO_HANDLES),
219                               &UVERBS_METHOD(UVERBS_METHOD_QUERY_PORT));
220
221 const struct uapi_definition uverbs_def_obj_device[] = {
222         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DEVICE),
223         {},
224 };