xref: /linux/net/devlink/param.c (revision c0ef1446959101d23fdf1b1bdefc6613a83dba03)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5  */
6 
7 #include "devl_internal.h"
8 
9 static const struct devlink_param devlink_param_generic[] = {
10 	{
11 		.id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
12 		.name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
13 		.type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
14 	},
15 	{
16 		.id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
17 		.name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
18 		.type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
19 	},
20 	{
21 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
22 		.name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
23 		.type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
24 	},
25 	{
26 		.id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
27 		.name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
28 		.type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
29 	},
30 	{
31 		.id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
32 		.name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
33 		.type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
34 	},
35 	{
36 		.id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
37 		.name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
38 		.type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
39 	},
40 	{
41 		.id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
42 		.name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
43 		.type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
44 	},
45 	{
46 		.id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
47 		.name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
48 		.type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
49 	},
50 	{
51 		.id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
52 		.name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
53 		.type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
54 	},
55 	{
56 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
57 		.name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
58 		.type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
59 	},
60 	{
61 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
62 		.name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
63 		.type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
64 	},
65 	{
66 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
67 		.name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
68 		.type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
69 	},
70 	{
71 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
72 		.name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
73 		.type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
74 	},
75 	{
76 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
77 		.name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
78 		.type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
79 	},
80 	{
81 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
82 		.name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
83 		.type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
84 	},
85 	{
86 		.id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
87 		.name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
88 		.type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
89 	},
90 	{
91 		.id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
92 		.name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
93 		.type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
94 	},
95 	{
96 		.id = DEVLINK_PARAM_GENERIC_ID_ENABLE_PHC,
97 		.name = DEVLINK_PARAM_GENERIC_ENABLE_PHC_NAME,
98 		.type = DEVLINK_PARAM_GENERIC_ENABLE_PHC_TYPE,
99 	},
100 };
101 
102 static int devlink_param_generic_verify(const struct devlink_param *param)
103 {
104 	/* verify it match generic parameter by id and name */
105 	if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
106 		return -EINVAL;
107 	if (strcmp(param->name, devlink_param_generic[param->id].name))
108 		return -ENOENT;
109 
110 	WARN_ON(param->type != devlink_param_generic[param->id].type);
111 
112 	return 0;
113 }
114 
115 static int devlink_param_driver_verify(const struct devlink_param *param)
116 {
117 	int i;
118 
119 	if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
120 		return -EINVAL;
121 	/* verify no such name in generic params */
122 	for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
123 		if (!strcmp(param->name, devlink_param_generic[i].name))
124 			return -EEXIST;
125 
126 	return 0;
127 }
128 
129 static struct devlink_param_item *
130 devlink_param_find_by_name(struct xarray *params, const char *param_name)
131 {
132 	struct devlink_param_item *param_item;
133 	unsigned long param_id;
134 
135 	xa_for_each(params, param_id, param_item) {
136 		if (!strcmp(param_item->param->name, param_name))
137 			return param_item;
138 	}
139 	return NULL;
140 }
141 
142 static struct devlink_param_item *
143 devlink_param_find_by_id(struct xarray *params, u32 param_id)
144 {
145 	return xa_load(params, param_id);
146 }
147 
148 static bool
149 devlink_param_cmode_is_supported(const struct devlink_param *param,
150 				 enum devlink_param_cmode cmode)
151 {
152 	return test_bit(cmode, &param->supported_cmodes);
153 }
154 
155 static int devlink_param_get(struct devlink *devlink,
156 			     const struct devlink_param *param,
157 			     struct devlink_param_gset_ctx *ctx)
158 {
159 	if (!param->get)
160 		return -EOPNOTSUPP;
161 	return param->get(devlink, param->id, ctx);
162 }
163 
164 static int devlink_param_set(struct devlink *devlink,
165 			     const struct devlink_param *param,
166 			     struct devlink_param_gset_ctx *ctx,
167 			     struct netlink_ext_ack *extack)
168 {
169 	if (!param->set)
170 		return -EOPNOTSUPP;
171 	return param->set(devlink, param->id, ctx, extack);
172 }
173 
174 static int
175 devlink_nl_param_value_fill_one(struct sk_buff *msg,
176 				enum devlink_param_type type,
177 				enum devlink_param_cmode cmode,
178 				union devlink_param_value val)
179 {
180 	struct nlattr *param_value_attr;
181 
182 	param_value_attr = nla_nest_start_noflag(msg,
183 						 DEVLINK_ATTR_PARAM_VALUE);
184 	if (!param_value_attr)
185 		goto nla_put_failure;
186 
187 	if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
188 		goto value_nest_cancel;
189 
190 	switch (type) {
191 	case DEVLINK_PARAM_TYPE_U8:
192 		if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
193 			goto value_nest_cancel;
194 		break;
195 	case DEVLINK_PARAM_TYPE_U16:
196 		if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
197 			goto value_nest_cancel;
198 		break;
199 	case DEVLINK_PARAM_TYPE_U32:
200 		if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
201 			goto value_nest_cancel;
202 		break;
203 	case DEVLINK_PARAM_TYPE_U64:
204 		if (devlink_nl_put_u64(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
205 				       val.vu64))
206 			goto value_nest_cancel;
207 		break;
208 	case DEVLINK_PARAM_TYPE_STRING:
209 		if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
210 				   val.vstr))
211 			goto value_nest_cancel;
212 		break;
213 	case DEVLINK_PARAM_TYPE_BOOL:
214 		if (val.vbool &&
215 		    nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
216 			goto value_nest_cancel;
217 		break;
218 	}
219 
220 	nla_nest_end(msg, param_value_attr);
221 	return 0;
222 
223 value_nest_cancel:
224 	nla_nest_cancel(msg, param_value_attr);
225 nla_put_failure:
226 	return -EMSGSIZE;
227 }
228 
229 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
230 				 unsigned int port_index,
231 				 struct devlink_param_item *param_item,
232 				 enum devlink_command cmd,
233 				 u32 portid, u32 seq, int flags)
234 {
235 	union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
236 	bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
237 	const struct devlink_param *param = param_item->param;
238 	struct devlink_param_gset_ctx ctx;
239 	struct nlattr *param_values_list;
240 	struct nlattr *param_attr;
241 	void *hdr;
242 	int err;
243 	int i;
244 
245 	/* Get value from driver part to driverinit configuration mode */
246 	for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
247 		if (!devlink_param_cmode_is_supported(param, i))
248 			continue;
249 		if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
250 			if (param_item->driverinit_value_new_valid)
251 				param_value[i] = param_item->driverinit_value_new;
252 			else if (param_item->driverinit_value_valid)
253 				param_value[i] = param_item->driverinit_value;
254 			else
255 				return -EOPNOTSUPP;
256 		} else {
257 			ctx.cmode = i;
258 			err = devlink_param_get(devlink, param, &ctx);
259 			if (err)
260 				return err;
261 			param_value[i] = ctx.val;
262 		}
263 		param_value_set[i] = true;
264 	}
265 
266 	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
267 	if (!hdr)
268 		return -EMSGSIZE;
269 
270 	if (devlink_nl_put_handle(msg, devlink))
271 		goto genlmsg_cancel;
272 
273 	if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
274 	    cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
275 	    cmd == DEVLINK_CMD_PORT_PARAM_DEL)
276 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
277 			goto genlmsg_cancel;
278 
279 	param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
280 	if (!param_attr)
281 		goto genlmsg_cancel;
282 	if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
283 		goto param_nest_cancel;
284 	if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
285 		goto param_nest_cancel;
286 	if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, param->type))
287 		goto param_nest_cancel;
288 
289 	param_values_list = nla_nest_start_noflag(msg,
290 						  DEVLINK_ATTR_PARAM_VALUES_LIST);
291 	if (!param_values_list)
292 		goto param_nest_cancel;
293 
294 	for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
295 		if (!param_value_set[i])
296 			continue;
297 		err = devlink_nl_param_value_fill_one(msg, param->type,
298 						      i, param_value[i]);
299 		if (err)
300 			goto values_list_nest_cancel;
301 	}
302 
303 	nla_nest_end(msg, param_values_list);
304 	nla_nest_end(msg, param_attr);
305 	genlmsg_end(msg, hdr);
306 	return 0;
307 
308 values_list_nest_cancel:
309 	nla_nest_end(msg, param_values_list);
310 param_nest_cancel:
311 	nla_nest_cancel(msg, param_attr);
312 genlmsg_cancel:
313 	genlmsg_cancel(msg, hdr);
314 	return -EMSGSIZE;
315 }
316 
317 static void devlink_param_notify(struct devlink *devlink,
318 				 unsigned int port_index,
319 				 struct devlink_param_item *param_item,
320 				 enum devlink_command cmd)
321 {
322 	struct sk_buff *msg;
323 	int err;
324 
325 	WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
326 		cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
327 		cmd != DEVLINK_CMD_PORT_PARAM_DEL);
328 
329 	/* devlink_notify_register() / devlink_notify_unregister()
330 	 * will replay the notifications if the params are added/removed
331 	 * outside of the lifetime of the instance.
332 	 */
333 	if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink))
334 		return;
335 
336 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
337 	if (!msg)
338 		return;
339 	err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
340 				    0, 0, 0);
341 	if (err) {
342 		nlmsg_free(msg);
343 		return;
344 	}
345 
346 	devlink_nl_notify_send(devlink, msg);
347 }
348 
349 static void devlink_params_notify(struct devlink *devlink,
350 				  enum devlink_command cmd)
351 {
352 	struct devlink_param_item *param_item;
353 	unsigned long param_id;
354 
355 	xa_for_each(&devlink->params, param_id, param_item)
356 		devlink_param_notify(devlink, 0, param_item, cmd);
357 }
358 
359 void devlink_params_notify_register(struct devlink *devlink)
360 {
361 	devlink_params_notify(devlink, DEVLINK_CMD_PARAM_NEW);
362 }
363 
364 void devlink_params_notify_unregister(struct devlink *devlink)
365 {
366 	devlink_params_notify(devlink, DEVLINK_CMD_PARAM_DEL);
367 }
368 
369 static int devlink_nl_param_get_dump_one(struct sk_buff *msg,
370 					 struct devlink *devlink,
371 					 struct netlink_callback *cb,
372 					 int flags)
373 {
374 	struct devlink_nl_dump_state *state = devlink_dump_state(cb);
375 	struct devlink_param_item *param_item;
376 	unsigned long param_id;
377 	int err = 0;
378 
379 	xa_for_each_start(&devlink->params, param_id, param_item, state->idx) {
380 		err = devlink_nl_param_fill(msg, devlink, 0, param_item,
381 					    DEVLINK_CMD_PARAM_GET,
382 					    NETLINK_CB(cb->skb).portid,
383 					    cb->nlh->nlmsg_seq, flags);
384 		if (err == -EOPNOTSUPP) {
385 			err = 0;
386 		} else if (err) {
387 			state->idx = param_id;
388 			break;
389 		}
390 	}
391 
392 	return err;
393 }
394 
395 int devlink_nl_param_get_dumpit(struct sk_buff *skb,
396 				struct netlink_callback *cb)
397 {
398 	return devlink_nl_dumpit(skb, cb, devlink_nl_param_get_dump_one);
399 }
400 
401 static int
402 devlink_param_type_get_from_info(struct genl_info *info,
403 				 enum devlink_param_type *param_type)
404 {
405 	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
406 		return -EINVAL;
407 
408 	*param_type = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE]);
409 
410 	return 0;
411 }
412 
413 static int
414 devlink_param_value_get_from_info(const struct devlink_param *param,
415 				  struct genl_info *info,
416 				  union devlink_param_value *value)
417 {
418 	struct nlattr *param_data;
419 	int len;
420 
421 	param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
422 
423 	if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
424 		return -EINVAL;
425 
426 	switch (param->type) {
427 	case DEVLINK_PARAM_TYPE_U8:
428 		if (nla_len(param_data) != sizeof(u8))
429 			return -EINVAL;
430 		value->vu8 = nla_get_u8(param_data);
431 		break;
432 	case DEVLINK_PARAM_TYPE_U16:
433 		if (nla_len(param_data) != sizeof(u16))
434 			return -EINVAL;
435 		value->vu16 = nla_get_u16(param_data);
436 		break;
437 	case DEVLINK_PARAM_TYPE_U32:
438 		if (nla_len(param_data) != sizeof(u32))
439 			return -EINVAL;
440 		value->vu32 = nla_get_u32(param_data);
441 		break;
442 	case DEVLINK_PARAM_TYPE_U64:
443 		if (nla_len(param_data) != sizeof(u64))
444 			return -EINVAL;
445 		value->vu64 = nla_get_u64(param_data);
446 		break;
447 	case DEVLINK_PARAM_TYPE_STRING:
448 		len = strnlen(nla_data(param_data), nla_len(param_data));
449 		if (len == nla_len(param_data) ||
450 		    len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
451 			return -EINVAL;
452 		strcpy(value->vstr, nla_data(param_data));
453 		break;
454 	case DEVLINK_PARAM_TYPE_BOOL:
455 		if (param_data && nla_len(param_data))
456 			return -EINVAL;
457 		value->vbool = nla_get_flag(param_data);
458 		break;
459 	}
460 	return 0;
461 }
462 
463 static struct devlink_param_item *
464 devlink_param_get_from_info(struct xarray *params, struct genl_info *info)
465 {
466 	char *param_name;
467 
468 	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_NAME))
469 		return NULL;
470 
471 	param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
472 	return devlink_param_find_by_name(params, param_name);
473 }
474 
475 int devlink_nl_param_get_doit(struct sk_buff *skb,
476 			      struct genl_info *info)
477 {
478 	struct devlink *devlink = info->user_ptr[0];
479 	struct devlink_param_item *param_item;
480 	struct sk_buff *msg;
481 	int err;
482 
483 	param_item = devlink_param_get_from_info(&devlink->params, info);
484 	if (!param_item)
485 		return -EINVAL;
486 
487 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
488 	if (!msg)
489 		return -ENOMEM;
490 
491 	err = devlink_nl_param_fill(msg, devlink, 0, param_item,
492 				    DEVLINK_CMD_PARAM_GET,
493 				    info->snd_portid, info->snd_seq, 0);
494 	if (err) {
495 		nlmsg_free(msg);
496 		return err;
497 	}
498 
499 	return genlmsg_reply(msg, info);
500 }
501 
502 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
503 					   unsigned int port_index,
504 					   struct xarray *params,
505 					   struct genl_info *info,
506 					   enum devlink_command cmd)
507 {
508 	enum devlink_param_type param_type;
509 	struct devlink_param_gset_ctx ctx;
510 	enum devlink_param_cmode cmode;
511 	struct devlink_param_item *param_item;
512 	const struct devlink_param *param;
513 	union devlink_param_value value;
514 	int err = 0;
515 
516 	param_item = devlink_param_get_from_info(params, info);
517 	if (!param_item)
518 		return -EINVAL;
519 	param = param_item->param;
520 	err = devlink_param_type_get_from_info(info, &param_type);
521 	if (err)
522 		return err;
523 	if (param_type != param->type)
524 		return -EINVAL;
525 	err = devlink_param_value_get_from_info(param, info, &value);
526 	if (err)
527 		return err;
528 	if (param->validate) {
529 		err = param->validate(devlink, param->id, value, info->extack);
530 		if (err)
531 			return err;
532 	}
533 
534 	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_CMODE))
535 		return -EINVAL;
536 	cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
537 	if (!devlink_param_cmode_is_supported(param, cmode))
538 		return -EOPNOTSUPP;
539 
540 	if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
541 		param_item->driverinit_value_new = value;
542 		param_item->driverinit_value_new_valid = true;
543 	} else {
544 		if (!param->set)
545 			return -EOPNOTSUPP;
546 		ctx.val = value;
547 		ctx.cmode = cmode;
548 		err = devlink_param_set(devlink, param, &ctx, info->extack);
549 		if (err)
550 			return err;
551 	}
552 
553 	devlink_param_notify(devlink, port_index, param_item, cmd);
554 	return 0;
555 }
556 
557 int devlink_nl_param_set_doit(struct sk_buff *skb, struct genl_info *info)
558 {
559 	struct devlink *devlink = info->user_ptr[0];
560 
561 	return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->params,
562 					       info, DEVLINK_CMD_PARAM_NEW);
563 }
564 
565 int devlink_nl_port_param_get_dumpit(struct sk_buff *msg,
566 				     struct netlink_callback *cb)
567 {
568 	NL_SET_ERR_MSG(cb->extack, "Port params are not supported");
569 	return msg->len;
570 }
571 
572 int devlink_nl_port_param_get_doit(struct sk_buff *skb,
573 				   struct genl_info *info)
574 {
575 	NL_SET_ERR_MSG(info->extack, "Port params are not supported");
576 	return -EINVAL;
577 }
578 
579 int devlink_nl_port_param_set_doit(struct sk_buff *skb,
580 				   struct genl_info *info)
581 {
582 	NL_SET_ERR_MSG(info->extack, "Port params are not supported");
583 	return -EINVAL;
584 }
585 
586 static int devlink_param_verify(const struct devlink_param *param)
587 {
588 	if (!param || !param->name || !param->supported_cmodes)
589 		return -EINVAL;
590 	if (param->generic)
591 		return devlink_param_generic_verify(param);
592 	else
593 		return devlink_param_driver_verify(param);
594 }
595 
596 static int devlink_param_register(struct devlink *devlink,
597 				  const struct devlink_param *param)
598 {
599 	struct devlink_param_item *param_item;
600 	int err;
601 
602 	WARN_ON(devlink_param_verify(param));
603 	WARN_ON(devlink_param_find_by_name(&devlink->params, param->name));
604 
605 	if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
606 		WARN_ON(param->get || param->set);
607 	else
608 		WARN_ON(!param->get || !param->set);
609 
610 	param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
611 	if (!param_item)
612 		return -ENOMEM;
613 
614 	param_item->param = param;
615 
616 	err = xa_insert(&devlink->params, param->id, param_item, GFP_KERNEL);
617 	if (err)
618 		goto err_xa_insert;
619 
620 	devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
621 	return 0;
622 
623 err_xa_insert:
624 	kfree(param_item);
625 	return err;
626 }
627 
628 static void devlink_param_unregister(struct devlink *devlink,
629 				     const struct devlink_param *param)
630 {
631 	struct devlink_param_item *param_item;
632 
633 	param_item = devlink_param_find_by_id(&devlink->params, param->id);
634 	if (WARN_ON(!param_item))
635 		return;
636 	devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_DEL);
637 	xa_erase(&devlink->params, param->id);
638 	kfree(param_item);
639 }
640 
641 /**
642  *	devl_params_register - register configuration parameters
643  *
644  *	@devlink: devlink
645  *	@params: configuration parameters array
646  *	@params_count: number of parameters provided
647  *
648  *	Register the configuration parameters supported by the driver.
649  */
650 int devl_params_register(struct devlink *devlink,
651 			 const struct devlink_param *params,
652 			 size_t params_count)
653 {
654 	const struct devlink_param *param = params;
655 	int i, err;
656 
657 	lockdep_assert_held(&devlink->lock);
658 
659 	for (i = 0; i < params_count; i++, param++) {
660 		err = devlink_param_register(devlink, param);
661 		if (err)
662 			goto rollback;
663 	}
664 	return 0;
665 
666 rollback:
667 	if (!i)
668 		return err;
669 
670 	for (param--; i > 0; i--, param--)
671 		devlink_param_unregister(devlink, param);
672 	return err;
673 }
674 EXPORT_SYMBOL_GPL(devl_params_register);
675 
676 int devlink_params_register(struct devlink *devlink,
677 			    const struct devlink_param *params,
678 			    size_t params_count)
679 {
680 	int err;
681 
682 	devl_lock(devlink);
683 	err = devl_params_register(devlink, params, params_count);
684 	devl_unlock(devlink);
685 	return err;
686 }
687 EXPORT_SYMBOL_GPL(devlink_params_register);
688 
689 /**
690  *	devl_params_unregister - unregister configuration parameters
691  *	@devlink: devlink
692  *	@params: configuration parameters to unregister
693  *	@params_count: number of parameters provided
694  */
695 void devl_params_unregister(struct devlink *devlink,
696 			    const struct devlink_param *params,
697 			    size_t params_count)
698 {
699 	const struct devlink_param *param = params;
700 	int i;
701 
702 	lockdep_assert_held(&devlink->lock);
703 
704 	for (i = 0; i < params_count; i++, param++)
705 		devlink_param_unregister(devlink, param);
706 }
707 EXPORT_SYMBOL_GPL(devl_params_unregister);
708 
709 void devlink_params_unregister(struct devlink *devlink,
710 			       const struct devlink_param *params,
711 			       size_t params_count)
712 {
713 	devl_lock(devlink);
714 	devl_params_unregister(devlink, params, params_count);
715 	devl_unlock(devlink);
716 }
717 EXPORT_SYMBOL_GPL(devlink_params_unregister);
718 
719 /**
720  *	devl_param_driverinit_value_get - get configuration parameter
721  *					  value for driver initializing
722  *
723  *	@devlink: devlink
724  *	@param_id: parameter ID
725  *	@val: pointer to store the value of parameter in driverinit
726  *	      configuration mode
727  *
728  *	This function should be used by the driver to get driverinit
729  *	configuration for initialization after reload command.
730  *
731  *	Note that lockless call of this function relies on the
732  *	driver to maintain following basic sane behavior:
733  *	1) Driver ensures a call to this function cannot race with
734  *	   registering/unregistering the parameter with the same parameter ID.
735  *	2) Driver ensures a call to this function cannot race with
736  *	   devl_param_driverinit_value_set() call with the same parameter ID.
737  *	3) Driver ensures a call to this function cannot race with
738  *	   reload operation.
739  *	If the driver is not able to comply, it has to take the devlink->lock
740  *	while calling this.
741  */
742 int devl_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
743 				    union devlink_param_value *val)
744 {
745 	struct devlink_param_item *param_item;
746 
747 	if (WARN_ON(!devlink_reload_supported(devlink->ops)))
748 		return -EOPNOTSUPP;
749 
750 	param_item = devlink_param_find_by_id(&devlink->params, param_id);
751 	if (!param_item)
752 		return -EINVAL;
753 
754 	if (!param_item->driverinit_value_valid)
755 		return -EOPNOTSUPP;
756 
757 	if (WARN_ON(!devlink_param_cmode_is_supported(param_item->param,
758 						      DEVLINK_PARAM_CMODE_DRIVERINIT)))
759 		return -EOPNOTSUPP;
760 
761 	*val = param_item->driverinit_value;
762 
763 	return 0;
764 }
765 EXPORT_SYMBOL_GPL(devl_param_driverinit_value_get);
766 
767 /**
768  *	devl_param_driverinit_value_set - set value of configuration
769  *					  parameter for driverinit
770  *					  configuration mode
771  *
772  *	@devlink: devlink
773  *	@param_id: parameter ID
774  *	@init_val: value of parameter to set for driverinit configuration mode
775  *
776  *	This function should be used by the driver to set driverinit
777  *	configuration mode default value.
778  */
779 void devl_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
780 				     union devlink_param_value init_val)
781 {
782 	struct devlink_param_item *param_item;
783 
784 	devl_assert_locked(devlink);
785 
786 	param_item = devlink_param_find_by_id(&devlink->params, param_id);
787 	if (WARN_ON(!param_item))
788 		return;
789 
790 	if (WARN_ON(!devlink_param_cmode_is_supported(param_item->param,
791 						      DEVLINK_PARAM_CMODE_DRIVERINIT)))
792 		return;
793 
794 	param_item->driverinit_value = init_val;
795 	param_item->driverinit_value_valid = true;
796 
797 	devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
798 }
799 EXPORT_SYMBOL_GPL(devl_param_driverinit_value_set);
800 
801 void devlink_params_driverinit_load_new(struct devlink *devlink)
802 {
803 	struct devlink_param_item *param_item;
804 	unsigned long param_id;
805 
806 	xa_for_each(&devlink->params, param_id, param_item) {
807 		if (!devlink_param_cmode_is_supported(param_item->param,
808 						      DEVLINK_PARAM_CMODE_DRIVERINIT) ||
809 		    !param_item->driverinit_value_new_valid)
810 			continue;
811 		param_item->driverinit_value = param_item->driverinit_value_new;
812 		param_item->driverinit_value_valid = true;
813 		param_item->driverinit_value_new_valid = false;
814 	}
815 }
816 
817 /**
818  *	devl_param_value_changed - notify devlink on a parameter's value
819  *				   change. Should be called by the driver
820  *				   right after the change.
821  *
822  *	@devlink: devlink
823  *	@param_id: parameter ID
824  *
825  *	This function should be used by the driver to notify devlink on value
826  *	change, excluding driverinit configuration mode.
827  *	For driverinit configuration mode driver should use the function
828  */
829 void devl_param_value_changed(struct devlink *devlink, u32 param_id)
830 {
831 	struct devlink_param_item *param_item;
832 
833 	param_item = devlink_param_find_by_id(&devlink->params, param_id);
834 	WARN_ON(!param_item);
835 
836 	devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
837 }
838 EXPORT_SYMBOL_GPL(devl_param_value_changed);
839